suricata
source-pcap.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Live pcap packet acquisition support
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "decode.h"
29 #include "packet-queue.h"
30 #include "threads.h"
31 #include "threadvars.h"
32 #include "tm-queuehandlers.h"
33 #include "tm-threads.h"
34 #include "source-pcap.h"
35 #include "conf.h"
36 #include "util-bpf.h"
37 #include "util-debug.h"
38 #include "util-error.h"
39 #include "util-privs.h"
40 #include "util-datalink.h"
41 #include "util-device-private.h"
42 #include "util-checksum.h"
43 #include "util-ioctl.h"
44 #include "tmqh-packetpool.h"
45 
46 #define PCAP_STATE_DOWN 0
47 #define PCAP_STATE_UP 1
48 
49 #define PCAP_RECONNECT_TIMEOUT 500000
50 
51 /**
52  * \brief 64bit pcap stats counters.
53  *
54  * libpcap only supports 32bit counters. They will eventually wrap around.
55  *
56  * Keep track of libpcap counters as 64bit counters to keep on counting even
57  * if libpcap's 32bit counters wrap around.
58  * Requires pcap_stats() to be called before 32bit stats wrap around twice,
59  * which we do.
60  */
61 typedef struct PcapStats64_ {
62  uint64_t ps_recv;
63  uint64_t ps_drop;
64  uint64_t ps_ifdrop;
66 
67 /**
68  * \brief Structure to hold thread specific variables.
69  */
70 typedef struct PcapThreadVars_
71 {
72  /* thread specific handle */
73  pcap_t *pcap_handle;
74  /* handle state */
75  unsigned char pcap_state;
76  /* thread specific bpf */
77  struct bpf_program filter;
78  /* ptr to string from config */
79  const char *bpf_filter;
80 
82 
83  /* data link type for the thread */
84  int datalink;
85 
86  /* counters */
87  uint64_t pkts;
88  uint64_t bytes;
89 
93 
96 
97  /** callback result -- set if one of the thread module failed. */
98  int cb_result;
99 
100  /* pcap buffer size */
103  int promisc;
104 
106 
108 
111 
112 static TmEcode ReceivePcapThreadInit(ThreadVars *, const void *, void **);
113 static TmEcode ReceivePcapThreadDeinit(ThreadVars *tv, void *data);
114 static void ReceivePcapThreadExitStats(ThreadVars *, void *);
115 static TmEcode ReceivePcapLoop(ThreadVars *tv, void *data, void *slot);
116 static TmEcode ReceivePcapBreakLoop(ThreadVars *tv, void *data);
117 
118 static TmEcode DecodePcapThreadInit(ThreadVars *, const void *, void **);
119 static TmEcode DecodePcapThreadDeinit(ThreadVars *tv, void *data);
120 static TmEcode DecodePcap(ThreadVars *, Packet *, void *);
121 
122 #ifdef UNITTESTS
123 static void SourcePcapRegisterTests(void);
124 #endif
125 
126 /** protect pcap_compile and pcap_setfilter, as they are not thread safe:
127  * http://seclists.org/tcpdump/2009/q1/62 */
128 static SCMutex pcap_bpf_compile_lock = SCMUTEX_INITIALIZER;
129 
130 /**
131  * \brief Registration Function for ReceivePcap.
132  */
134 {
135  tmm_modules[TMM_RECEIVEPCAP].name = "ReceivePcap";
136  tmm_modules[TMM_RECEIVEPCAP].ThreadInit = ReceivePcapThreadInit;
137  tmm_modules[TMM_RECEIVEPCAP].ThreadDeinit = ReceivePcapThreadDeinit;
138  tmm_modules[TMM_RECEIVEPCAP].PktAcqLoop = ReceivePcapLoop;
139  tmm_modules[TMM_RECEIVEPCAP].PktAcqBreakLoop = ReceivePcapBreakLoop;
140  tmm_modules[TMM_RECEIVEPCAP].ThreadExitPrintStats = ReceivePcapThreadExitStats;
143 #ifdef UNITTESTS
144  tmm_modules[TMM_RECEIVEPCAP].RegisterTests = SourcePcapRegisterTests;
145 #endif
146 }
147 
148 /**
149  * \brief Registration Function for DecodePcap.
150  */
152 {
153  tmm_modules[TMM_DECODEPCAP].name = "DecodePcap";
154  tmm_modules[TMM_DECODEPCAP].ThreadInit = DecodePcapThreadInit;
155  tmm_modules[TMM_DECODEPCAP].Func = DecodePcap;
156  tmm_modules[TMM_DECODEPCAP].ThreadDeinit = DecodePcapThreadDeinit;
158 }
159 
160 /**
161  * \brief Update 64 bit |last| value from |current32| value taking one
162  * wrap-around into account.
163  */
164 static inline void UpdatePcapStatsValue64(uint64_t *last, uint32_t current32)
165 {
166  /* uint64_t -> uint32_t is defined behaviour. It slices lower 32bits. */
167  uint32_t last32 = (uint32_t)*last;
168 
169  /* Branchless code as wrap-around is defined for unsigned */
170  *last += (uint32_t)(current32 - last32);
171 
172  /* Same calculation as:
173  if (likely(current32 >= last32)) {
174  *last += current32 - last32;
175  } else {
176  *last += (1ull << 32) + current32 - last32;
177  }
178  */
179 }
180 
181 /**
182  * \brief Update 64 bit |last| stat values with values from |current|
183  * 32 bit pcap_stat.
184  */
185 static inline void UpdatePcapStats64(
186  PcapStats64 *last, const struct pcap_stat *current)
187 {
188  UpdatePcapStatsValue64(&last->ps_recv, current->ps_recv);
189  UpdatePcapStatsValue64(&last->ps_drop, current->ps_drop);
190  UpdatePcapStatsValue64(&last->ps_ifdrop, current->ps_ifdrop);
191 }
192 
193 static inline void PcapDumpCounters(PcapThreadVars *ptv)
194 {
195  struct pcap_stat pcap_s;
196  if (likely((pcap_stats(ptv->pcap_handle, &pcap_s) >= 0))) {
197  UpdatePcapStats64(&ptv->last_stats64, &pcap_s);
198 
201  (void)SC_ATOMIC_SET(ptv->livedev->drop, ptv->last_stats64.ps_drop);
204  }
205 }
206 
207 static int PcapOpenInterface(PcapThreadVars *ptv)
208 {
209  const char *iface = ptv->livedev->dev;
210 
211  if (ptv->pcap_handle) {
212  pcap_close(ptv->pcap_handle);
213  ptv->pcap_handle = NULL;
214  if (ptv->filter.bf_insns) {
215  SCBPFFree(&ptv->filter);
216  }
217  }
218 
219  if (LiveGetOffload() == 0) {
220  (void)GetIfaceOffloading(iface, 1, 1);
221  } else {
222  DisableIfaceOffloading(ptv->livedev, 1, 1);
223  }
224 
225  char errbuf[PCAP_ERRBUF_SIZE];
226  ptv->pcap_handle = pcap_create(iface, errbuf);
227  if (ptv->pcap_handle == NULL) {
228  if (strlen(errbuf)) {
229  SCLogError("%s: could not create a new pcap handler, error %s", iface, errbuf);
230  } else {
231  SCLogError("%s: could not create a new pcap handler", iface);
232  }
234  }
235 
236  if (ptv->pcap_snaplen > 0) {
237  /* set Snaplen. Must be called before pcap_activate */
238  int pcap_set_snaplen_r = pcap_set_snaplen(ptv->pcap_handle, ptv->pcap_snaplen);
239  if (pcap_set_snaplen_r != 0) {
240  SCLogError(
241  "%s: could not set snaplen, error: %s", iface, pcap_geterr(ptv->pcap_handle));
243  }
244  SCLogInfo("%s: snaplen set to %d", iface, ptv->pcap_snaplen);
245  }
246 
247  if (ptv->promisc) {
248  /* set Promisc, and Timeout. Must be called before pcap_activate */
249  int pcap_set_promisc_r = pcap_set_promisc(ptv->pcap_handle, ptv->promisc);
250  if (pcap_set_promisc_r != 0) {
251  SCLogError("%s: could not set promisc mode, error %s", iface,
252  pcap_geterr(ptv->pcap_handle));
254  }
255  }
256 
257  int pcap_set_timeout_r = pcap_set_timeout(ptv->pcap_handle, LIBPCAP_COPYWAIT);
258  if (pcap_set_timeout_r != 0) {
259  SCLogError("%s: could not set timeout, error %s", iface, pcap_geterr(ptv->pcap_handle));
261  }
262 #ifdef HAVE_PCAP_SET_BUFF
263  if (ptv->pcap_buffer_size > 0) {
264  SCLogInfo("%s: going to use pcap buffer size of %" PRId32, iface, ptv->pcap_buffer_size);
265 
266  int pcap_set_buffer_size_r = pcap_set_buffer_size(ptv->pcap_handle, ptv->pcap_buffer_size);
267  if (pcap_set_buffer_size_r != 0) {
268  SCLogError("%s: could not set pcap buffer size, error %s", iface,
269  pcap_geterr(ptv->pcap_handle));
271  }
272  }
273 #endif /* HAVE_PCAP_SET_BUFF */
274 
275  /* activate the handle */
276  int pcap_activate_r = pcap_activate(ptv->pcap_handle);
277  if (pcap_activate_r != 0) {
278  SCLogError("%s: could not activate the pcap handler, error %s", iface,
279  pcap_geterr(ptv->pcap_handle));
280  pcap_close(ptv->pcap_handle);
281  ptv->pcap_handle = NULL;
283  }
284  ptv->pcap_state = PCAP_STATE_UP;
285 
286  /* set bpf filter if we have one */
287  if (ptv->bpf_filter) {
288  SCMutexLock(&pcap_bpf_compile_lock);
289 
290  if (pcap_compile(ptv->pcap_handle, &ptv->filter, (char *)ptv->bpf_filter, 1, 0) < 0) {
291  SCLogError("%s: bpf compilation error %s", iface, pcap_geterr(ptv->pcap_handle));
292  SCMutexUnlock(&pcap_bpf_compile_lock);
293  return TM_ECODE_FAILED;
294  }
295 
296  if (pcap_setfilter(ptv->pcap_handle, &ptv->filter) < 0) {
297  SCLogError("%s: could not set bpf filter %s", iface, pcap_geterr(ptv->pcap_handle));
298  SCMutexUnlock(&pcap_bpf_compile_lock);
299  return TM_ECODE_FAILED;
300  }
301 
302  SCMutexUnlock(&pcap_bpf_compile_lock);
303  }
304 
305  /* no offloading supported at all */
306  (void)GetIfaceOffloading(iface, 1, 1);
307  return TM_ECODE_OK;
308 }
309 
310 static int PcapTryReopen(PcapThreadVars *ptv)
311 {
313 
314  if (PcapOpenInterface(ptv) != TM_ECODE_OK)
315  return -1;
316 
317  SCLogInfo("%s: interface recovered, state is now \"up\"", ptv->livedev->dev);
318  ptv->pcap_state = PCAP_STATE_UP;
319  return 0;
320 }
321 
322 static void PcapCallbackLoop(char *user, struct pcap_pkthdr *h, u_char *pkt)
323 {
324  SCEnter();
325 
326  PcapThreadVars *ptv = (PcapThreadVars *)user;
328 
329  if (unlikely(p == NULL)) {
330  SCReturn;
331  }
332 
334  p->ts = SCTIME_FROM_TIMEVAL(&h->ts);
335  SCLogDebug("p->ts.tv_sec %" PRIuMAX "", (uintmax_t)SCTIME_SECS(p->ts));
336  p->datalink = ptv->datalink;
337 
338  ptv->pkts++;
339  ptv->bytes += h->caplen;
340  (void) SC_ATOMIC_ADD(ptv->livedev->pkts, 1);
341  p->livedev_id = ptv->livedev->id;
342 
343  if (unlikely(PacketCopyData(p, pkt, h->caplen))) {
344  TmqhOutputPacketpool(ptv->tv, p);
345  SCReturn;
346  }
347 
348  switch (ptv->checksum_mode) {
350  if (ChecksumAutoModeCheck(ptv->pkts,
351  SC_ATOMIC_GET(ptv->livedev->pkts),
352  SC_ATOMIC_GET(ptv->livedev->invalid_checksums))) {
355  }
356  break;
359  break;
360  default:
361  break;
362  }
363 
364  if (TmThreadsSlotProcessPkt(ptv->tv, ptv->slot, p) != TM_ECODE_OK) {
365  pcap_breakloop(ptv->pcap_handle);
366  ptv->cb_result = TM_ECODE_FAILED;
367  }
368 
369  /* Trigger one dump of stats every second */
370  SCTime_t current_time = TimeGet();
371  if ((time_t)SCTIME_SECS(current_time) != ptv->last_stats_dump) {
372  PcapDumpCounters(ptv);
373  ptv->last_stats_dump = SCTIME_SECS(current_time);
374  }
375 
376  SCReturn;
377 }
378 
379 #ifndef PCAP_ERROR_BREAK
380 #define PCAP_ERROR_BREAK -2
381 #endif
382 
383 /**
384  * \brief Main PCAP reading Loop function
385  */
386 static TmEcode ReceivePcapLoop(ThreadVars *tv, void *data, void *slot)
387 {
388  SCEnter();
389 
390  int packet_q_len = 64;
391  PcapThreadVars *ptv = (PcapThreadVars *)data;
392  TmSlot *s = (TmSlot *)slot;
393 
394  ptv->slot = s->slot_next;
395  ptv->cb_result = TM_ECODE_OK;
396 
397  // Indicate that the thread is actually running its application level code (i.e., it can poll
398  // packets)
400 
401  while (1) {
404  }
405 
406  /* make sure we have at least one packet in the packet pool, to prevent
407  * us from alloc'ing packets at line rate */
408  PacketPoolWait();
409 
410  int r = pcap_dispatch(ptv->pcap_handle, packet_q_len,
411  (pcap_handler)PcapCallbackLoop, (u_char *)ptv);
412  if (unlikely(r == 0 || r == PCAP_ERROR_BREAK || (r > 0 && r < packet_q_len))) {
413  if (r == PCAP_ERROR_BREAK && ptv->cb_result == TM_ECODE_FAILED) {
415  }
416  TmThreadsCaptureHandleTimeout(tv, NULL);
417  } else if (unlikely(r < 0)) {
418  int dbreak = 0;
419  SCLogError("error code %" PRId32 " %s", r, pcap_geterr(ptv->pcap_handle));
420  do {
422  if (suricata_ctl_flags != 0) {
423  dbreak = 1;
424  break;
425  }
426  r = PcapTryReopen(ptv);
427  } while (r < 0);
428  if (dbreak) {
429  break;
430  }
431  } else if (ptv->cb_result == TM_ECODE_FAILED) {
432  SCLogError("Pcap callback PcapCallbackLoop failed");
434  }
435 
437  }
438 
439  PcapDumpCounters(ptv);
442 }
443 
444 /**
445  * \brief PCAP Break Loop function.
446  */
447 static TmEcode ReceivePcapBreakLoop(ThreadVars *tv, void *data)
448 {
449  SCEnter();
450  PcapThreadVars *ptv = (PcapThreadVars *)data;
451  if (ptv->pcap_handle == NULL) {
453  }
454  pcap_breakloop(ptv->pcap_handle);
456 }
457 
458 /**
459  * \brief Init function for ReceivePcap.
460  *
461  * This is a setup function for receiving packets
462  * via libpcap. There are two versions of this function
463  * depending on the major version of libpcap used.
464  * For versions prior to 1.x we use open_pcap_live,
465  * for versions 1.x and greater we use pcap_create + pcap_activate.
466  *
467  * \param tv pointer to ThreadVars
468  * \param initdata pointer to the interface passed from the user
469  * \param data pointer gets populated with PcapThreadVars
470  *
471  * \todo Create a general pcap setup function.
472  */
473 static TmEcode ReceivePcapThreadInit(ThreadVars *tv, const void *initdata, void **data)
474 {
475  SCEnter();
476  PcapIfaceConfig *pcapconfig = (PcapIfaceConfig *)initdata;
477 
478  if (initdata == NULL) {
479  SCLogError("initdata == NULL");
481  }
482 
483  PcapThreadVars *ptv = SCCalloc(1, sizeof(PcapThreadVars));
484  if (unlikely(ptv == NULL)) {
485  pcapconfig->DerefFunc(pcapconfig);
487  }
488 
489  ptv->tv = tv;
490 
491  ptv->livedev = LiveGetDevice(pcapconfig->iface);
492  if (ptv->livedev == NULL) {
493  SCLogError("unable to find Live device");
494  ReceivePcapThreadDeinit(tv, ptv);
496  }
497 
498  if (LiveGetOffload() == 0) {
499  (void)GetIfaceOffloading((char *)pcapconfig->iface, 1, 1);
500  } else {
501  DisableIfaceOffloading(ptv->livedev, 1, 1);
502  }
503 
504  ptv->checksum_mode = pcapconfig->checksum_mode;
506  SCLogInfo("%s: running in 'auto' checksum mode. Detection of interface "
507  "state will require %llu packets",
509  }
510 
511  if (pcapconfig->snaplen == 0) {
512  /* We set snaplen if we can get the MTU */
514  } else {
515  ptv->pcap_snaplen = pcapconfig->snaplen;
516  }
517 
518  ptv->promisc = pcapconfig->promisc;
519  ptv->pcap_buffer_size = pcapconfig->buffer_size;
520  ptv->bpf_filter = pcapconfig->bpf_filter;
521 
522  if (PcapOpenInterface(ptv) != TM_ECODE_OK) {
523  ReceivePcapThreadDeinit(tv, ptv);
524  pcapconfig->DerefFunc(pcapconfig);
526  }
527  ptv->pcap_state = PCAP_STATE_UP;
528 
529  ptv->datalink = pcap_datalink(ptv->pcap_handle);
531 
532  pcapconfig->DerefFunc(pcapconfig);
533 
534  ptv->capture_kernel_packets = StatsRegisterCounter("capture.kernel_packets", &ptv->tv->stats);
535  ptv->capture_kernel_drops = StatsRegisterCounter("capture.kernel_drops", &ptv->tv->stats);
536  ptv->capture_kernel_ifdrops = StatsRegisterCounter("capture.kernel_ifdrops", &ptv->tv->stats);
537 
538  *data = (void *)ptv;
540 }
541 
542 /**
543  * \brief This function prints stats to the screen at exit.
544  * \param tv pointer to ThreadVars
545  * \param data pointer that gets cast into PcapThreadVars for ptv
546  */
547 static void ReceivePcapThreadExitStats(ThreadVars *tv, void *data)
548 {
549  SCEnter();
550  PcapThreadVars *ptv = (PcapThreadVars *)data;
551  struct pcap_stat pcap_s;
552 
553  if (pcap_stats(ptv->pcap_handle, &pcap_s) < 0) {
554  SCLogError("%s: failed to get pcap_stats: %s", ptv->livedev->dev,
555  pcap_geterr(ptv->pcap_handle));
556  SCLogInfo("%s: packets %" PRIu64 ", bytes %" PRIu64 "", ptv->livedev->dev, ptv->pkts,
557  ptv->bytes);
558  } else {
559  SCLogInfo("%s: packets %" PRIu64 ", bytes %" PRIu64 "", ptv->livedev->dev, ptv->pkts,
560  ptv->bytes);
561 
562  /* these numbers are not entirely accurate as ps_recv contains packets
563  * that are still waiting to be processed at exit. ps_drop only contains
564  * packets dropped by the driver and not any packets dropped by the interface.
565  * Additionally see http://tracker.icir.org/bro/ticket/18
566  *
567  * Note: ps_recv includes dropped packets and should be considered total.
568  * Unless we start to look at ps_ifdrop which isn't supported everywhere.
569  */
570  UpdatePcapStats64(&ptv->last_stats64, &pcap_s);
571  float drop_percent =
572  likely(ptv->last_stats64.ps_recv > 0)
573  ? (((float)ptv->last_stats64.ps_drop) /
574  (float)ptv->last_stats64.ps_recv) *
575  100
576  : 0;
577  SCLogInfo("%s: pcap total:%" PRIu64 " recv:%" PRIu64 " drop:%" PRIu64 " (%02.1f%%)",
578  ptv->livedev->dev, ptv->last_stats64.ps_recv,
580  drop_percent);
581  }
582 }
583 
584 static TmEcode ReceivePcapThreadDeinit(ThreadVars *tv, void *data)
585 {
586  SCEnter();
587  PcapThreadVars *ptv = (PcapThreadVars *)data;
588  if (ptv != NULL) {
589  if (ptv->pcap_handle != NULL) {
590  pcap_close(ptv->pcap_handle);
591  }
592  if (ptv->filter.bf_insns) {
593  SCBPFFree(&ptv->filter);
594  }
595  SCFree(ptv);
596  }
598 }
599 
600 /**
601  * \brief This function passes off to link type decoders.
602  *
603  * DecodePcap decodes packets from libpcap and passes
604  * them off to the proper link type decoder.
605  *
606  * \param t pointer to ThreadVars
607  * \param p pointer to the current packet
608  * \param data pointer that gets cast into PcapThreadVars for ptv
609  */
610 static TmEcode DecodePcap(ThreadVars *tv, Packet *p, void *data)
611 {
612  SCEnter();
614 
616 
617  /* update counters */
619 
620  DecodeLinkLayer(tv, dtv, p->datalink, p, GET_PKT_DATA(p), GET_PKT_LEN(p));
621 
623 
625 }
626 
627 static TmEcode DecodePcapThreadInit(ThreadVars *tv, const void *initdata, void **data)
628 {
629  SCEnter();
630 
632  if (dtv == NULL)
634 
636 
637  *data = (void *)dtv;
638 
640 }
641 
642 static TmEcode DecodePcapThreadDeinit(ThreadVars *tv, void *data)
643 {
644  if (data != NULL)
645  DecodeThreadVarsFree(tv, data);
647 }
648 
649 void PcapTranslateIPToDevice(char *pcap_dev, size_t len)
650 {
651  char errbuf[PCAP_ERRBUF_SIZE];
652  pcap_if_t *alldevsp = NULL;
653 
654  struct addrinfo ai_hints;
655  struct addrinfo *ai_list = NULL;
656 
657  memset(&ai_hints, 0, sizeof(ai_hints));
658  ai_hints.ai_family = AF_UNSPEC;
659  ai_hints.ai_flags = AI_NUMERICHOST;
660 
661  /* try to translate IP */
662  if (getaddrinfo(pcap_dev, NULL, &ai_hints, &ai_list) != 0) {
663  return;
664  }
665 
666  if (pcap_findalldevs(&alldevsp, errbuf)) {
667  freeaddrinfo(ai_list);
668  return;
669  }
670 
671  for (pcap_if_t *devsp = alldevsp; devsp ; devsp = devsp->next) {
672  for (pcap_addr_t *ip = devsp->addresses; ip ; ip = ip->next) {
673 
674  if (ai_list->ai_family != ip->addr->sa_family) {
675  continue;
676  }
677 
678  if (ip->addr->sa_family == AF_INET) {
679  if (memcmp(&((struct sockaddr_in*)ai_list->ai_addr)->sin_addr,
680  &((struct sockaddr_in*)ip->addr)->sin_addr,
681  sizeof(struct in_addr)))
682  {
683  continue;
684  }
685  } else if (ip->addr->sa_family == AF_INET6) {
686  if (memcmp(&((struct sockaddr_in6*)ai_list->ai_addr)->sin6_addr,
687  &((struct sockaddr_in6*)ip->addr)->sin6_addr,
688  sizeof(struct in6_addr)))
689  {
690  continue;
691  }
692  } else {
693  continue;
694  }
695 
696  freeaddrinfo(ai_list);
697 
698  memset(pcap_dev, 0, len);
699  strlcpy(pcap_dev, devsp->name, len);
700 
701  pcap_freealldevs(alldevsp);
702  return;
703  }
704  }
705 
706  freeaddrinfo(ai_list);
707 
708  pcap_freealldevs(alldevsp);
709 }
710 
711 /*
712  * unittests
713  */
714 
715 #ifdef UNITTESTS
716 #include "tests/source-pcap.c"
717 /**
718  * \brief Register the Unit tests for pcap source
719  */
720 static void SourcePcapRegisterTests(void)
721 {
722  SourcePcapRegisterStatsTests();
723 }
724 #endif /* UNITTESTS */
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:77
PcapThreadVars_::cb_result
int cb_result
Definition: source-pcap.c:98
util-device-private.h
tm-threads.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
source-pcap.h
PcapThreadVars_::livedev
LiveDevice * livedev
Definition: source-pcap.c:107
StatsSyncCountersIfSignalled
void StatsSyncCountersIfSignalled(StatsThreadContext *stats)
Definition: counters.c:481
PcapThreadVars_::pcap_buffer_size
int pcap_buffer_size
Definition: source-pcap.c:101
PcapThreadVars_::filter
struct bpf_program filter
Definition: source-pcap.c:77
PacketCopyData
int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Copy data to Packet payload and set packet length.
Definition: decode.c:386
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1364
util-bpf.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
PcapThreadVars_::promisc
int promisc
Definition: source-pcap.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
StatsRegisterCounter
StatsCounterId StatsRegisterCounter(const char *name, StatsThreadContext *stats)
Registers a normal, unqualified counter.
Definition: counters.c:1038
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
Definition: tm-threads.c:102
LiveGetOffload
int LiveGetOffload(void)
Definition: util-device.c:87
PcapIfaceConfig_::promisc
int promisc
Definition: source-pcap.h:57
util-checksum.h
Packet_::flags
uint32_t flags
Definition: decode.h:562
threads.h
PcapIfaceConfig_::bpf_filter
const char * bpf_filter
Definition: source-pcap.h:59
PcapThreadVars_::bpf_filter
const char * bpf_filter
Definition: source-pcap.c:79
LiveDevice_
Definition: util-device-private.h:32
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:332
LiveDevice_::id
uint16_t id
Definition: util-device-private.h:38
THV_RUNNING
#define THV_RUNNING
Definition: threadvars.h:54
CHECKSUM_SAMPLE_COUNT
#define CHECKSUM_SAMPLE_COUNT
Definition: util-checksum.h:35
packet-queue.h
PcapStats64_::ps_drop
uint64_t ps_drop
Definition: source-pcap.c:63
SURICATA_STOP
#define SURICATA_STOP
Definition: suricata.h:94
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
util-privs.h
CHECKSUM_VALIDATION_DISABLE
@ CHECKSUM_VALIDATION_DISABLE
Definition: decode.h:43
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
p
Packet * p
Definition: fuzz_dataset.c:30
StatsCounterId
Definition: counters.h:30
PacketDecodeFinalize
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:234
SleepUsec
#define SleepUsec(usec)
Definition: tm-threads.h:44
PcapThreadVars_::bytes
uint64_t bytes
Definition: source-pcap.c:88
TmqhOutputPacketpool
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
Definition: tmqh-packetpool.c:305
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
PcapThreadVars_::capture_kernel_drops
StatsCounterId capture_kernel_drops
Definition: source-pcap.c:91
PcapThreadVars_::capture_kernel_packets
StatsCounterId capture_kernel_packets
Definition: source-pcap.c:90
ChecksumAutoModeCheck
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.
Definition: util-checksum.c:69
tmqh-packetpool.h
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:58
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
source-pcap.c
PcapThreadVars_::pkts
uint64_t pkts
Definition: source-pcap.c:87
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
PcapTranslateIPToDevice
void PcapTranslateIPToDevice(char *pcap_dev, size_t len)
Definition: source-pcap.c:649
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:53
Packet_::datalink
int datalink
Definition: decode.h:652
PcapIfaceConfig_::snaplen
int snaplen
Definition: source-pcap.h:55
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1366
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:647
CHECKSUM_VALIDATION_AUTO
@ CHECKSUM_VALIDATION_AUTO
Definition: decode.h:45
LIBPCAP_COPYWAIT
#define LIBPCAP_COPYWAIT
Definition: source-pcap.h:33
GetIfaceMaxPacketSize
int GetIfaceMaxPacketSize(LiveDevice *ld)
output max packet size for a link
Definition: util-ioctl.c:122
decode.h
util-debug.h
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:52
util-error.h
TmModule_::PktAcqBreakLoop
TmEcode(* PktAcqBreakLoop)(ThreadVars *, void *)
Definition: tm-modules.h:61
PcapThreadVars_::slot
TmSlot * slot
Definition: source-pcap.c:95
PcapStats64_::ps_recv
uint64_t ps_recv
Definition: source-pcap.c:62
PCAP_STATE_DOWN
#define PCAP_STATE_DOWN
Definition: source-pcap.c:46
Packet_::ts
SCTime_t ts
Definition: decode.h:570
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
LiveGetDevice
LiveDevice * LiveGetDevice(const char *name)
Get a pointer to the device at idx.
Definition: util-device.c:269
PCAP_RECONNECT_TIMEOUT
#define PCAP_RECONNECT_TIMEOUT
Definition: source-pcap.c:49
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:210
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
PCAP_STATE_UP
#define PCAP_STATE_UP
Definition: source-pcap.c:47
SCTIME_FROM_TIMEVAL
#define SCTIME_FROM_TIMEVAL(tv)
Definition: util-time.h:79
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:56
LiveDevice_::dev
char * dev
Definition: util-device-private.h:33
PcapThreadVars
struct PcapThreadVars_ PcapThreadVars
Structure to hold thread specific variables.
PcapIfaceConfig_::buffer_size
int buffer_size
Definition: source-pcap.h:53
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:322
PacketPoolWait
void PacketPoolWait(void)
Definition: tmqh-packetpool.c:71
SCReturn
#define SCReturn
Definition: util-debug.h:286
Packet_
Definition: decode.h:516
TM_FLAG_DECODE_TM
#define TM_FLAG_DECODE_TM
Definition: tm-modules.h:33
PcapIfaceConfig_::DerefFunc
void(* DerefFunc)(void *)
Definition: source-pcap.h:62
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:29
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:209
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
conf.h
TmModule_::RegisterTests
void(* RegisterTests)(void)
Definition: tm-modules.h:75
TmSlot_
Definition: tm-threads.h:53
PKT_IGNORE_CHECKSUM
#define PKT_IGNORE_CHECKSUM
Definition: decode.h:1327
SCTime_t
Definition: util-time.h:40
TmEcode
TmEcode
Definition: tm-threads-common.h:80
GetIfaceOffloading
int GetIfaceOffloading(const char *dev, int csum, int other)
output offloading status of the link
Definition: util-ioctl.c:677
DisableIfaceOffloading
int DisableIfaceOffloading(LiveDevice *dev, int csum, int other)
Definition: util-ioctl.c:690
TmModule_::name
const char * name
Definition: tm-modules.h:48
PcapIfaceConfig_
Definition: source-pcap.h:48
PcapThreadVars_::capture_kernel_ifdrops
StatsCounterId capture_kernel_ifdrops
Definition: source-pcap.c:92
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
PcapThreadVars_::tv
ThreadVars * tv
Definition: source-pcap.c:94
TM_FLAG_RECEIVE_TM
#define TM_FLAG_RECEIVE_TM
Definition: tm-modules.h:32
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:35
tm-queuehandlers.h
DecodeThreadVarsFree
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:861
ChecksumValidationMode
ChecksumValidationMode
Definition: decode.h:42
suricata-common.h
PcapThreadVars_::last_stats64
PcapStats64 last_stats64
Definition: source-pcap.c:109
PcapThreadVars_::pcap_handle
pcap_t * pcap_handle
Definition: source-pcap.c:73
PcapThreadVars_::pcap_state
unsigned char pcap_state
Definition: source-pcap.c:75
Packet_::livedev_id
uint16_t livedev_id
Definition: decode.h:633
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
SCBPFFree
void SCBPFFree(struct bpf_program *program)
Definition: util-bpf.c:56
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:51
PcapStats64_
64bit pcap stats counters.
Definition: source-pcap.c:61
PcapIfaceConfig_::iface
char iface[PCAP_IFACE_NAME_LENGTH]
Definition: source-pcap.h:49
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:52
threadvars.h
TMM_DECODEPCAP
@ TMM_DECODEPCAP
Definition: tm-threads-common.h:40
StatsCounterSetI64
void StatsCounterSetI64(StatsThreadContext *stats, StatsCounterId id, int64_t x)
set, so overwrite, the value of the local counter
Definition: counters.c:202
TmModuleDecodePcapRegister
void TmModuleDecodePcapRegister(void)
Registration Function for DecodePcap.
Definition: source-pcap.c:151
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:995
TmModuleReceivePcapRegister
void TmModuleReceivePcapRegister(void)
Registration Function for ReceivePcap.
Definition: source-pcap.c:133
util-ioctl.h
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:843
PcapStats64_::ps_ifdrop
uint64_t ps_ifdrop
Definition: source-pcap.c:64
suricata.h
PcapIfaceConfig_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-pcap.h:60
PcapThreadVars_::last_stats_dump
time_t last_stats_dump
Definition: source-pcap.c:81
TMM_RECEIVEPCAP
@ TMM_RECEIVEPCAP
Definition: tm-threads-common.h:38
likely
#define likely(expr)
Definition: util-optimize.h:32
TmSlot_::slot_next
struct TmSlot_ * slot_next
Definition: tm-threads.h:62
PcapStats64
struct PcapStats64_ PcapStats64
64bit pcap stats counters.
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
PcapThreadVars_
Structure to hold thread specific variables.
Definition: source-pcap.c:71
PcapThreadVars_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-pcap.c:105
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
PcapThreadVars_::datalink
int datalink
Definition: source-pcap.c:84
PCAP_ERROR_BREAK
#define PCAP_ERROR_BREAK
Definition: source-pcap.c:380
SCMutex
#define SCMutex
Definition: threads-debug.h:114
PacketGetFromQueueOrAlloc
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...
Definition: decode.c:298
SC_CAP_NET_RAW
#define SC_CAP_NET_RAW
Definition: util-privs.h:32
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:80
DecodeUpdatePacketCounters
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:811
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:177
PcapThreadVars_::pcap_snaplen
int pcap_snaplen
Definition: source-pcap.c:102