Go to the documentation of this file.
48 #define PCAP_STATE_DOWN 0
49 #define PCAP_STATE_UP 1
51 #define PCAP_RECONNECT_TIMEOUT 500000
116 static void ReceivePcapThreadExitStats(
ThreadVars *,
void *);
125 static void SourcePcapRegisterTests(
void);
166 static inline void UpdatePcapStatsValue64(uint64_t *last, uint32_t current32)
169 uint32_t last32 = (uint32_t)*last;
172 *last += (uint32_t)(current32 - last32);
187 static inline void UpdatePcapStats64(
188 PcapStats64 *last,
const struct pcap_stat *current)
190 UpdatePcapStatsValue64(&last->
ps_recv, current->ps_recv);
191 UpdatePcapStatsValue64(&last->
ps_drop, current->ps_drop);
192 UpdatePcapStatsValue64(&last->
ps_ifdrop, current->ps_ifdrop);
197 struct pcap_stat pcap_s;
218 if (ptv->
filter.bf_insns) {
229 char errbuf[PCAP_ERRBUF_SIZE];
232 if (strlen(errbuf)) {
233 SCLogError(
"%s: could not create a new pcap handler, error %s", iface, errbuf);
235 SCLogError(
"%s: could not create a new pcap handler", iface);
243 if (pcap_set_snaplen_r != 0) {
245 "%s: could not set snaplen, error: %s", iface, pcap_geterr(ptv->
pcap_handle));
254 if (pcap_set_promisc_r != 0) {
255 SCLogError(
"%s: could not set promisc mode, error %s", iface,
262 if (pcap_set_timeout_r != 0) {
266 #ifdef HAVE_PCAP_SET_BUFF
271 if (pcap_set_buffer_size_r != 0) {
272 SCLogError(
"%s: could not set pcap buffer size, error %s", iface,
280 int pcap_activate_r = pcap_activate(ptv->
pcap_handle);
281 if (pcap_activate_r != 0) {
282 SCLogError(
"%s: could not activate the pcap handler, error %s", iface,
326 static void PcapCallbackLoop(
char *user,
struct pcap_pkthdr *h, u_char *pkt)
343 ptv->
bytes += h->caplen;
376 PcapDumpCounters(ptv);
383 #ifndef PCAP_ERROR_BREAK
384 #define PCAP_ERROR_BREAK -2
394 int packet_q_len = 64;
414 int r = pcap_dispatch(ptv->
pcap_handle, packet_q_len,
415 (pcap_handler)PcapCallbackLoop, (u_char *)ptv);
420 TmThreadsCaptureHandleTimeout(
tv, NULL);
423 SCLogError(
"error code %" PRId32
" %s", r, pcap_geterr(ptv->pcap_handle));
430 r = PcapTryReopen(ptv);
436 SCLogError(
"Pcap callback PcapCallbackLoop failed");
443 PcapDumpCounters(ptv);
482 if (initdata == NULL) {
498 ReceivePcapThreadDeinit(
tv, ptv);
510 SCLogInfo(
"%s: running in 'auto' checksum mode. Detection of interface "
511 "state will require %llu packets",
515 if (pcapconfig->
snaplen == 0) {
527 ReceivePcapThreadDeinit(
tv, ptv);
554 static void ReceivePcapThreadExitStats(
ThreadVars *
tv,
void *data)
558 struct pcap_stat pcap_s;
584 SCLogInfo(
"%s: pcap total:%" PRIu64
" recv:%" PRIu64
" drop:%" PRIu64
" (%02.1f%%)",
599 if (ptv->
filter.bf_insns) {
658 char errbuf[PCAP_ERRBUF_SIZE];
659 pcap_if_t *alldevsp = NULL;
661 struct addrinfo ai_hints;
662 struct addrinfo *ai_list = NULL;
664 memset(&ai_hints, 0,
sizeof(ai_hints));
665 ai_hints.ai_family = AF_UNSPEC;
666 ai_hints.ai_flags = AI_NUMERICHOST;
669 if (getaddrinfo(pcap_dev, NULL, &ai_hints, &ai_list) != 0) {
673 if (pcap_findalldevs(&alldevsp, errbuf)) {
674 freeaddrinfo(ai_list);
678 for (pcap_if_t *devsp = alldevsp; devsp ; devsp = devsp->next) {
679 for (pcap_addr_t *ip = devsp->addresses; ip ; ip = ip->next) {
681 if (ai_list->ai_family != ip->addr->sa_family) {
685 if (ip->addr->sa_family == AF_INET) {
686 if (memcmp(&((
struct sockaddr_in*)ai_list->ai_addr)->sin_addr,
687 &((
struct sockaddr_in*)ip->addr)->sin_addr,
688 sizeof(
struct in_addr)))
692 }
else if (ip->addr->sa_family == AF_INET6) {
693 if (memcmp(&((
struct sockaddr_in6*)ai_list->ai_addr)->sin6_addr,
694 &((
struct sockaddr_in6*)ip->addr)->sin6_addr,
695 sizeof(
struct in6_addr)))
703 freeaddrinfo(ai_list);
705 memset(pcap_dev, 0,
len);
708 pcap_freealldevs(alldevsp);
713 freeaddrinfo(ai_list);
715 pcap_freealldevs(alldevsp);
727 static void SourcePcapRegisterTests(
void)
729 SourcePcapRegisterStatsTests();
uint16_t capture_kernel_ifdrops
struct bpf_program filter
int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Copy data to Packet payload and set packet length.
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
void StatsSetUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Sets a value of type double to the local counter.
#define CHECKSUM_SAMPLE_COUNT
@ CHECKSUM_VALIDATION_DISABLE
#define SCMUTEX_INITIALIZER
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
int ChecksumAutoModeCheck(uint64_t thread_count, uint64_t iface_count, uint64_t iface_fail)
Check if the number of invalid checksums indicate checksum offloading in place.
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
size_t strlcpy(char *dst, const char *src, size_t siz)
void PcapTranslateIPToDevice(char *pcap_dev, size_t len)
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
#define PKT_SET_SRC(p, src_val)
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
@ CHECKSUM_VALIDATION_AUTO
int GetIfaceMaxPacketSize(LiveDevice *ld)
output max packet size for a link
TmEcode(* PktAcqBreakLoop)(ThreadVars *, void *)
uint16_t capture_kernel_packets
#define SCMutexUnlock(mut)
LiveDevice * LiveGetDevice(const char *name)
Get a pointer to the device at idx.
#define PCAP_RECONNECT_TIMEOUT
Per thread variable structure.
#define SCTIME_FROM_TIMEVAL(tv)
TmEcode(* Func)(ThreadVars *, Packet *, void *)
struct PcapThreadVars_ PcapThreadVars
Structure to hold thread specific variables.
uint16_t capture_kernel_drops
void PacketPoolWait(void)
#define TM_FLAG_DECODE_TM
void(* DerefFunc)(void *)
TmModule tmm_modules[TMM_SIZE]
void(* RegisterTests)(void)
#define PKT_IGNORE_CHECKSUM
struct LiveDevice_ * livedev
int GetIfaceOffloading(const char *dev, int csum, int other)
output offloading status of the link
int DisableIfaceOffloading(LiveDevice *dev, int csum, int other)
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
#define TM_FLAG_RECEIVE_TM
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
void SCBPFFree(struct bpf_program *program)
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
64bit pcap stats counters.
char iface[PCAP_IFACE_NAME_LENGTH]
void(* ThreadExitPrintStats)(ThreadVars *, void *)
void TmModuleDecodePcapRegister(void)
Registration Function for DecodePcap.
#define SCLogError(...)
Macro used to log ERROR messages.
void DatalinkSetGlobalType(int datalink)
Structure to hold thread specific data for all decode modules.
void TmModuleReceivePcapRegister(void)
Registration Function for ReceivePcap.
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
ChecksumValidationMode checksum_mode
struct TmSlot_ * slot_next
struct PcapStats64_ PcapStats64
64bit pcap stats counters.
void StatsSyncCountersIfSignalled(ThreadVars *tv)
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Structure to hold thread specific variables.
ChecksumValidationMode checksum_mode
uint16_t StatsRegisterCounter(const char *name, struct ThreadVars_ *tv)
Registers a normal, unqualified counter.
Packet * PacketGetFromQueueOrAlloc(void)
Get a packet. We try to get a packet from the packetpool first, but if that is empty we alloc a packe...
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
volatile uint8_t suricata_ctl_flags