suricata
source-ipfw.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2014 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 Nick Rogness <nick@rogness.net>
22  * \author Eric Leblond <eric@regit.org>
23  *
24  * IPFW packet acquisition support
25  */
26 
27 #include "suricata-common.h"
28 #include "suricata.h"
29 #include "decode.h"
30 #include "packet-queue.h"
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-queuehandlers.h"
34 #include "tm-threads.h"
35 #include "source-ipfw.h"
36 #include "util-debug.h"
37 #include "conf.h"
38 #include "util-byte.h"
39 #include "util-privs.h"
40 #include "util-device.h"
41 #include "runmodes.h"
42 
43 #define IPFW_ACCEPT 0
44 #define IPFW_DROP 1
45 
46 #define IPFW_SOCKET_POLL_MSEC 300
47 
48 #ifndef IP_MAXPACKET
49 #define IP_MAXPACKET 65535
50 #endif
51 
52 #ifndef IPFW
53 /* Handle the case if --enable-ipfw was not used
54  *
55  */
56 
57 TmEcode NoIPFWSupportExit(ThreadVars *, const void *, void **);
58 
60 {
61 
62  tmm_modules[TMM_RECEIVEIPFW].name = "ReceiveIPFW";
63  tmm_modules[TMM_RECEIVEIPFW].ThreadInit = NoIPFWSupportExit;
68 }
69 
71 {
72  tmm_modules[TMM_VERDICTIPFW].name = "VerdictIPFW";
73  tmm_modules[TMM_VERDICTIPFW].ThreadInit = NoIPFWSupportExit;
77 }
78 
80 {
81  tmm_modules[TMM_DECODEIPFW].name = "DecodeIPFW";
82  tmm_modules[TMM_DECODEIPFW].ThreadInit = NoIPFWSupportExit;
88 }
89 
90 TmEcode NoIPFWSupportExit(ThreadVars *tv, const void *initdata, void **data)
91 {
92 
93  SCLogError(SC_ERR_IPFW_NOSUPPORT,"Error creating thread %s: you do not have support for ipfw "
94  "enabled please recompile with --enable-ipfw", tv->name);
95  exit(EXIT_FAILURE);
96 }
97 
98 #else /* We have IPFW compiled in */
99 
100 extern int max_pending_packets;
101 
102 /**
103  * \brief Structure to hold thread specific variables.
104  */
105 typedef struct IPFWThreadVars_
106 {
107  /* data link type for the thread, probably not needed */
108  int datalink;
109 
110  /* this one should be not changing after init */
111  uint16_t port_num;
112  /* position into the NFQ queue var array */
113  uint16_t ipfw_index;
114 
115  /* counters */
116  uint32_t pkts;
117  uint64_t bytes;
118  uint32_t errs;
119  uint32_t accepted;
120  uint32_t dropped;
122 
123 static IPFWThreadVars ipfw_t[IPFW_MAX_QUEUE];
124 static IPFWQueueVars ipfw_q[IPFW_MAX_QUEUE];
125 static uint16_t receive_port_num = 0;
126 static SCMutex ipfw_init_lock;
127 
128 /* IPFW Prototypes */
129 static void *IPFWGetQueue(int number);
130 static TmEcode ReceiveIPFWThreadInit(ThreadVars *, const void *, void **);
131 static TmEcode ReceiveIPFW(ThreadVars *, Packet *, void *);
132 static TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot);
133 static void ReceiveIPFWThreadExitStats(ThreadVars *, void *);
134 static TmEcode ReceiveIPFWThreadDeinit(ThreadVars *, void *);
135 
136 static TmEcode IPFWSetVerdict(ThreadVars *, IPFWThreadVars *, Packet *);
137 static TmEcode VerdictIPFW(ThreadVars *, Packet *, void *);
138 static TmEcode VerdictIPFWThreadInit(ThreadVars *, const void *, void **);
139 static void VerdictIPFWThreadExitStats(ThreadVars *, void *);
140 static TmEcode VerdictIPFWThreadDeinit(ThreadVars *, void *);
141 
142 static TmEcode DecodeIPFWThreadInit(ThreadVars *, const void *, void **);
143 static TmEcode DecodeIPFWThreadDeinit(ThreadVars *tv, void *data);
144 static TmEcode DecodeIPFW(ThreadVars *, Packet *, void *);
145 
146 /**
147  * \brief Registration Function for RecieveIPFW.
148  * \todo Unit tests are needed for this module.
149  */
151 {
152  SCMutexInit(&ipfw_init_lock, NULL);
153 
154  tmm_modules[TMM_RECEIVEIPFW].name = "ReceiveIPFW";
155  tmm_modules[TMM_RECEIVEIPFW].ThreadInit = ReceiveIPFWThreadInit;
157  tmm_modules[TMM_RECEIVEIPFW].PktAcqLoop = ReceiveIPFWLoop;
159  tmm_modules[TMM_RECEIVEIPFW].ThreadExitPrintStats = ReceiveIPFWThreadExitStats;
160  tmm_modules[TMM_RECEIVEIPFW].ThreadDeinit = ReceiveIPFWThreadDeinit;
163  SC_CAP_NET_BROADCAST; /** \todo untested */
165 }
166 
167 /**
168  * \brief Registration Function for VerdictIPFW.
169  * \todo Unit tests are needed for this module.
170  */
172 {
173  tmm_modules[TMM_VERDICTIPFW].name = "VerdictIPFW";
174  tmm_modules[TMM_VERDICTIPFW].ThreadInit = VerdictIPFWThreadInit;
175  tmm_modules[TMM_VERDICTIPFW].Func = VerdictIPFW;
176  tmm_modules[TMM_VERDICTIPFW].ThreadExitPrintStats = VerdictIPFWThreadExitStats;
177  tmm_modules[TMM_VERDICTIPFW].ThreadDeinit = VerdictIPFWThreadDeinit;
179  SC_CAP_NET_BIND_SERVICE; /** \todo untested */
180 }
181 
182 /**
183  * \brief Registration Function for DecodeIPFW.
184  * \todo Unit tests are needed for this module.
185  */
187 {
188  tmm_modules[TMM_DECODEIPFW].name = "DecodeIPFW";
189  tmm_modules[TMM_DECODEIPFW].ThreadInit = DecodeIPFWThreadInit;
190  tmm_modules[TMM_DECODEIPFW].Func = DecodeIPFW;
192  tmm_modules[TMM_DECODEIPFW].ThreadDeinit = DecodeIPFWThreadDeinit;
194 }
195 
196 static inline void IPFWMutexInit(IPFWQueueVars *nq)
197 {
198  char *active_runmode = RunmodeGetActive();
199 
200  if (active_runmode && !strcmp("workers", active_runmode)) {
201  nq->use_mutex = 0;
202  SCLogInfo("IPFW running in 'workers' runmode, will not use mutex.");
203  } else {
204  nq->use_mutex = 1;
205  }
206  if (nq->use_mutex)
207  SCMutexInit(&nq->socket_lock, NULL);
208 }
209 
210 static inline void IPFWMutexLock(IPFWQueueVars *nq)
211 {
212  if (nq->use_mutex)
213  SCMutexLock(&nq->socket_lock);
214 }
215 
216 static inline void IPFWMutexUnlock(IPFWQueueVars *nq)
217 {
218  if (nq->use_mutex)
220 }
221 
222 TmEcode ReceiveIPFWLoop(ThreadVars *tv, void *data, void *slot)
223 {
224  SCEnter();
225 
226  IPFWThreadVars *ptv = (IPFWThreadVars *)data;
227  IPFWQueueVars *nq = NULL;
228  uint8_t pkt[IP_MAXPACKET];
229  int pktlen=0;
230  struct pollfd IPFWpoll;
231  struct timeval IPFWts;
232  Packet *p = NULL;
233 
234  nq = IPFWGetQueue(ptv->ipfw_index);
235  if (nq == NULL) {
236  SCLogWarning(SC_ERR_INVALID_ARGUMENT, "Can't get thread variable");
238  }
239 
240  SCLogInfo("Thread '%s' will run on port %d (item %d)",
241  tv->name, nq->port_num, ptv->ipfw_index);
242  while (1) {
243  if (unlikely(suricata_ctl_flags != 0)) {
245  }
246 
247  IPFWpoll.fd = nq->fd;
248  IPFWpoll.events = POLLRDNORM;
249  /* Poll the socket for status */
250  if ( (poll(&IPFWpoll, 1, IPFW_SOCKET_POLL_MSEC)) > 0) {
251  if (!(IPFWpoll.revents & (POLLRDNORM | POLLERR)))
252  continue;
253  }
254 
255  if ((pktlen = recvfrom(nq->fd, pkt, sizeof(pkt), 0,
256  (struct sockaddr *)&nq->ipfw_sin,
257  &nq->ipfw_sinlen)) == -1) {
258  /* We received an error on socket read */
259  if (errno == EINTR || errno == EWOULDBLOCK) {
260  /* Nothing for us to process */
261  continue;
262  } else {
264  "Read from IPFW divert socket failed: %s",
265  strerror(errno));
267  }
268  }
269  /* We have a packet to process */
270  memset (&IPFWts, 0, sizeof(struct timeval));
271  gettimeofday(&IPFWts, NULL);
272 
273  /* make sure we have at least one packet in the packet pool, to prevent
274  * us from alloc'ing packets at line rate */
275  PacketPoolWait();
276 
278  if (p == NULL) {
280  }
282 
283  SCLogDebug("Received Packet Len: %d", pktlen);
284 
285  p->ts.tv_sec = IPFWts.tv_sec;
286  p->ts.tv_usec = IPFWts.tv_usec;
287 
288  ptv->pkts++;
289  ptv->bytes += pktlen;
290 
291  p->datalink = ptv->datalink;
292 
293  p->ipfw_v.ipfw_index = ptv->ipfw_index;
294 
295  PacketCopyData(p, pkt, pktlen);
296  SCLogDebug("Packet info: pkt_len: %" PRIu32 " (pkt %02x, pkt_data %02x)",
297  GET_PKT_LEN(p), *pkt, *(GET_PKT_DATA(p)));
298 
299  if (TmThreadsSlotProcessPkt(tv, ((TmSlot *) slot)->slot_next, p)
300  != TM_ECODE_OK) {
302  }
303 
305  }
306 
308 }
309 
310 /**
311  * \brief Init function for RecieveIPFW.
312  *
313  * This is a setup function for recieving packets
314  * via ipfw divert, binds a socket, and prepares to
315  * to read from it.
316  *
317  * \param tv pointer to ThreadVars
318  * \param initdata pointer to the divert port passed from the user
319  * \param data pointer gets populated with IPFWThreadVars
320  *
321  */
322 TmEcode ReceiveIPFWThreadInit(ThreadVars *tv, const void *initdata, void **data)
323 {
324  struct timeval timev;
325  int flag;
326  IPFWThreadVars *ntv = (IPFWThreadVars *) initdata;
327  IPFWQueueVars *nq = IPFWGetQueue(ntv->ipfw_index);
328 
329  sigset_t sigs;
330  sigfillset(&sigs);
331  pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
332 
333  SCEnter();
334 
335  IPFWMutexInit(nq);
336  /* We need a divert socket to play with */
337  if ((nq->fd = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT)) == -1) {
338  SCLogError(SC_ERR_IPFW_SOCK,"Can't create divert socket: %s", strerror(errno));
340  }
341 
342  /* set a timeout to the socket so we can check for a signal
343  * in case we don't get packets for a longer period. */
344  timev.tv_sec = 1;
345  timev.tv_usec = 0;
346 
347  if (setsockopt(nq->fd, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev)) == -1) {
348  SCLogError(SC_ERR_IPFW_SETSOCKOPT,"Can't set IPFW divert socket timeout: %s", strerror(errno));
350  }
351 
352  /* set SO_BROADCAST on the divert socket, otherwise sendto()
353  * returns EACCES when reinjecting broadcast packets. */
354  flag = 1;
355 
356  if (setsockopt(nq->fd, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag)) == -1) {
357  SCLogError(SC_ERR_IPFW_SETSOCKOPT,"Can't set IPFW divert socket broadcast flag: %s", strerror(errno));
359  }
360 
361  nq->ipfw_sinlen=sizeof(nq->ipfw_sin);
362  memset(&nq->ipfw_sin, 0, nq->ipfw_sinlen);
363  nq->ipfw_sin.sin_family = PF_INET;
364  nq->ipfw_sin.sin_addr.s_addr = INADDR_ANY;
365  nq->ipfw_sin.sin_port = htons(nq->port_num);
366 
367  /* Bind that SOB */
368  if (bind(nq->fd, (struct sockaddr *)&nq->ipfw_sin, nq->ipfw_sinlen) == -1) {
369  SCLogError(SC_ERR_IPFW_BIND,"Can't bind divert socket on port %d: %s",nq->port_num,strerror(errno));
371  }
372 
373  ntv->datalink = DLT_RAW;
374 
375  *data = (void *)ntv;
376 
378 }
379 
380 /**
381  * \brief This function prints stats to the screen at exit.
382  * \todo Unit tests are needed for this module.
383  * \param tv pointer to ThreadVars
384  * \param data pointer that gets cast into IPFWThreadVars for ptv
385  */
386 void ReceiveIPFWThreadExitStats(ThreadVars *tv, void *data)
387 {
388  IPFWThreadVars *ptv = (IPFWThreadVars *)data;
389 
390  SCEnter();
391 
392  SCLogNotice("(%s) Treated: Pkts %" PRIu32 ", Bytes %" PRIu64 ", Errors %" PRIu32 "",
393  tv->name, ptv->pkts, ptv->bytes, ptv->errs);
394  SCLogNotice("(%s) Verdict: Accepted %"PRIu32", Dropped %"PRIu32 "",
395  tv->name, ptv->accepted, ptv->dropped);
396 
397 
398  SCReturn;
399 }
400 
401 /**
402  * \brief DeInit function closes divert socket at exit.
403  * \todo Unit tests are needed for this module.
404  * \param tv pointer to ThreadVars
405  * \param data pointer that gets cast into IPFWThreadVars for ptv
406  */
407 TmEcode ReceiveIPFWThreadDeinit(ThreadVars *tv, void *data)
408 {
409  IPFWThreadVars *ptv = (IPFWThreadVars *)data;
410  IPFWQueueVars *nq = IPFWGetQueue(ptv->ipfw_index);
411 
412  SCEnter();
413 
414  /* Attempt to shut the socket down...close instead? */
415  if (shutdown(nq->fd, SHUT_RD) < 0) {
416  SCLogWarning(SC_WARN_IPFW_UNBIND,"Unable to disable ipfw socket: %s",strerror(errno));
418  }
419 
421 }
422 
423 /**
424  * \brief This function passes off to link type decoders.
425  * \todo Unit tests are needed for this module.
426  *
427  * DecodeIPFW decodes packets from IPFW and passes
428  * them off to the proper link type decoder.
429  *
430  * \param tv pointer to ThreadVars
431  * \param p pointer to the current packet
432  * \param data pointer that gets cast into IPFWThreadVars for ptv
433  */
434 TmEcode DecodeIPFW(ThreadVars *tv, Packet *p, void *data)
435 {
436  IPV4Hdr *ip4h = (IPV4Hdr *)GET_PKT_DATA(p);
437  IPV6Hdr *ip6h = (IPV6Hdr *)GET_PKT_DATA(p);
439 
440  SCEnter();
441 
443 
444  /* update counters */
446 
447  /* Process IP packets */
448  if (IPV4_GET_RAW_VER(ip4h) == 4) {
449  if (unlikely(GET_PKT_LEN(p) > USHRT_MAX)) {
450  return TM_ECODE_FAILED;
451  }
452  SCLogDebug("DecodeIPFW ip4 processing");
454 
455  } else if(IPV6_GET_RAW_VER(ip6h) == 6) {
456  if (unlikely(GET_PKT_LEN(p) > USHRT_MAX)) {
457  return TM_ECODE_FAILED;
458  }
459  SCLogDebug("DecodeIPFW ip6 processing");
461 
462  } else {
463  /* We don't support anything besides IP packets for now, bridged packets? */
464  SCLogInfo("IPFW unknown protocol support %02x", *GET_PKT_DATA(p));
466  }
467 
469 
471 }
472 
473 /**
474  * \brief This function initializes the DecodeThreadVariables
475  *
476  *
477  * \param tv pointer to ThreadVars
478  * \param initdata pointer for passing in args
479  * \param data pointer that gets cast into IPFWThreadVars for ptv
480  */
481 TmEcode DecodeIPFWThreadInit(ThreadVars *tv, const void *initdata, void **data)
482 {
483  DecodeThreadVars *dtv = NULL;
485 
486  if (dtv == NULL)
488 
490 
491  *data = (void *)dtv;
492 
494 }
495 
496 TmEcode DecodeIPFWThreadDeinit(ThreadVars *tv, void *data)
497 {
498  if (data != NULL)
499  DecodeThreadVarsFree(tv, data);
501 }
502 
503 /**
504  * \brief This function sets the Verdict and processes the packet
505  *
506  *
507  * \param tv pointer to ThreadVars
508  * \param p pointer to the Packet
509  */
510 TmEcode IPFWSetVerdict(ThreadVars *tv, IPFWThreadVars *ptv, Packet *p)
511 {
512  uint32_t verdict;
513 #if 0
514  struct pollfd IPFWpoll;
515 #endif
516  IPFWQueueVars *nq = NULL;
517 
518  SCEnter();
519 
520  if (p == NULL) {
521  SCLogWarning(SC_ERR_INVALID_ARGUMENT, "Packet is NULL");
523  }
524 
525  nq = IPFWGetQueue(p->ipfw_v.ipfw_index);
526  if (nq == NULL) {
527  SCLogWarning(SC_ERR_INVALID_ARGUMENT, "No thread found");
529  }
530 
531 #if 0
532  IPFWpoll.fd = nq->fd;
533  IPFWpoll.events = POLLWRNORM;
534 #endif
535 
537  verdict = IPFW_DROP;
538  } else {
539  verdict = IPFW_ACCEPT;
540  }
541 
542  if (verdict == IPFW_ACCEPT) {
543  SCLogDebug("IPFW Verdict is to Accept");
544  ptv->accepted++;
545 
546  /* For divert sockets, accepting means writing the
547  * packet back to the socket for ipfw to pick up
548  */
549  SCLogDebug("IPFWSetVerdict writing to socket %d, %p, %u", nq->fd, GET_PKT_DATA(p),GET_PKT_LEN(p));
550 
551 #if 0
552  while ((poll(&IPFWpoll,1,IPFW_SOCKET_POLL_MSEC)) < 1) {
553  /* Did we receive a signal to shutdown */
555  SCLogInfo("Received ThreadShutdown: IPFW divert socket writing interrupted");
557  }
558  }
559 #endif
560 
561  IPFWMutexLock(nq);
562  if (sendto(nq->fd, GET_PKT_DATA(p), GET_PKT_LEN(p), 0,(struct sockaddr *)&nq->ipfw_sin, nq->ipfw_sinlen) == -1) {
563  int r = errno;
564  switch (r) {
565  default:
566  SCLogWarning(SC_WARN_IPFW_XMIT,"Write to ipfw divert socket failed: %s",strerror(r));
567  IPFWMutexUnlock(nq);
569  case EHOSTDOWN:
570  case ENETDOWN:
571  break;
572  }
573  }
574 
575  IPFWMutexUnlock(nq);
576 
577  SCLogDebug("Sent Packet back into IPFW Len: %d",GET_PKT_LEN(p));
578 
579  } /* end IPFW_ACCEPT */
580 
581 
582  if (verdict == IPFW_DROP) {
583  SCLogDebug("IPFW SetVerdict is to DROP");
584  ptv->dropped++;
585 
586  /** \todo For divert sockets, dropping means not writing the packet back to the socket.
587  * Need to see if there is some better way to free the packet from the queue */
588 
589  } /* end IPFW_DROP */
590 
592 }
593 
594 
595 /**
596  * \brief This function handles the Verdict processing
597  * \todo Unit tests are needed for this module.
598  *
599  *
600  * \param tv pointer to ThreadVars
601  * \param p pointer to the Packet
602  * \param data pointer that gets cast into IPFWThreadVars for ptv
603  */
604 TmEcode VerdictIPFW(ThreadVars *tv, Packet *p, void *data)
605 {
606  IPFWThreadVars *ptv = (IPFWThreadVars *)data;
607  TmEcode retval = TM_ECODE_OK;
608 
609  SCEnter();
610 
611  /* can't verdict a "fake" packet */
612  if (p->flags & PKT_PSEUDO_STREAM_END) {
614  }
615 
616  /* This came from NFQ.
617  * if this is a tunnel packet we check if we are ready to verdict
618  * already. */
619  if (IS_TUNNEL_PKT(p)) {
620  bool verdict = VerdictTunnelPacket(p);
621 
622  /* don't verdict if we are not ready */
623  if (verdict == true) {
624  SCLogDebug("Setting verdict on tunnel");
625  retval = IPFWSetVerdict(tv, ptv, p->root ? p->root : p);
626  }
627  } else {
628  /* no tunnel, verdict normally */
629  SCLogDebug("Setting verdict on non-tunnel");
630  retval = IPFWSetVerdict(tv, ptv, p);
631  } /* IS_TUNNEL_PKT end */
632 
633  SCReturnInt(retval);
634 }
635 
636 /**
637  * \brief This function initializes the VerdictThread
638  *
639  *
640  * \param t pointer to ThreadVars
641  * \param initdata pointer for passing in args
642  * \param data pointer that gets cast into IPFWThreadVars for ptv
643  */
644 TmEcode VerdictIPFWThreadInit(ThreadVars *tv, const void *initdata, void **data)
645 {
646 
647  IPFWThreadVars *ptv = NULL;
648 
649  SCEnter();
650 
651  /* Setup Thread vars */
652  if ( (ptv = SCMalloc(sizeof(IPFWThreadVars))) == NULL)
654  memset(ptv, 0, sizeof(IPFWThreadVars));
655 
656 
657  *data = (void *)ptv;
658 
660 }
661 
662 /**
663  * \brief This function deinitializes the VerdictThread
664  *
665  *
666  * \param tv pointer to ThreadVars
667  * \param data pointer that gets cast into IPFWThreadVars for ptv
668  */
669 TmEcode VerdictIPFWThreadDeinit(ThreadVars *tv, void *data)
670 {
671 
672  SCEnter();
673 
674  /* We don't need to do anything...not sure quite yet */
675 
676 
678 }
679 
680 /**
681  * \brief This function prints stats for the VerdictThread
682  *
683  *
684  * \param tv pointer to ThreadVars
685  * \param data pointer that gets cast into IPFWThreadVars for ptv
686  */
687 void VerdictIPFWThreadExitStats(ThreadVars *tv, void *data)
688 {
689  IPFWThreadVars *ptv = (IPFWThreadVars *)data;
690  SCLogInfo("IPFW Processing: - (%s) Pkts accepted %" PRIu32 ", dropped %" PRIu32 "", tv->name, ptv->accepted, ptv->dropped);
691 }
692 
693 /**
694  * \brief Add an IPFW divert
695  *
696  * \param string with the queue name
697  *
698  * \retval 0 on success.
699  * \retval -1 on failure.
700  */
701 int IPFWRegisterQueue(char *queue)
702 {
703  IPFWThreadVars *ntv = NULL;
704  IPFWQueueVars *nq = NULL;
705  /* Extract the queue number from the specified command line argument */
706  uint16_t port_num = 0;
707  if ((StringParseUint16(&port_num, 10, strlen(queue), queue)) < 0)
708  {
709  SCLogError(SC_ERR_INVALID_ARGUMENT, "specified queue number %s is not "
710  "valid", queue);
711  return -1;
712  }
713 
714  SCMutexLock(&ipfw_init_lock);
715  if (receive_port_num >= IPFW_MAX_QUEUE) {
717  "too much IPFW divert port registered (%d)",
718  receive_port_num);
719  SCMutexUnlock(&ipfw_init_lock);
720  return -1;
721  }
722  if (receive_port_num == 0) {
723  memset(&ipfw_t, 0, sizeof(ipfw_t));
724  memset(&ipfw_q, 0, sizeof(ipfw_q));
725  }
726 
727  ntv = &ipfw_t[receive_port_num];
728  ntv->ipfw_index = receive_port_num;
729 
730  nq = &ipfw_q[receive_port_num];
731  nq->port_num = port_num;
732  receive_port_num++;
733  SCMutexUnlock(&ipfw_init_lock);
734  LiveRegisterDeviceName(queue);
735 
736  SCLogDebug("Queue \"%s\" registered.", queue);
737  return 0;
738 }
739 
740 /**
741  * \brief Get a pointer to the IPFW queue at index
742  *
743  * \param number idx of the queue in our array
744  *
745  * \retval ptr pointer to the IPFWThreadVars at index
746  * \retval NULL on error
747  */
748 void *IPFWGetQueue(int number)
749 {
750  if (number >= receive_port_num)
751  return NULL;
752 
753  return (void *)&ipfw_q[number];
754 }
755 
756 /**
757  * \brief Get a pointer to the IPFW thread at index
758  *
759  * This function is temporary used as configuration parser.
760  *
761  * \param number idx of the queue in our array
762  *
763  * \retval ptr pointer to the IPFWThreadVars at index
764  * \retval NULL on error
765  */
766 void *IPFWGetThread(int number)
767 {
768  if (number >= receive_port_num)
769  return NULL;
770 
771  return (void *)&ipfw_t[number];
772 }
773 
774 #endif /* End ifdef IPFW */
775 
776 /* eof */
777 
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:67
util-byte.h
tm-threads.h
TmModuleReceiveIPFWRegister
void TmModuleReceiveIPFWRegister(void)
Registration Function for RecieveIPFW.
Definition: source-ipfw.c:150
IPFWQueueVars_::socket_lock
SCMutex socket_lock
Definition: source-ipfw.h:39
StringParseUint16
int StringParseUint16(uint16_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:336
max_pending_packets
int max_pending_packets
Definition: suricata.c:212
IPV6_GET_RAW_VER
#define IPV6_GET_RAW_VER(ip6h)
Definition: decode-ipv6.h:62
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
PacketCopyData
int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Copy data to Packet payload and set packet length.
Definition: decode.c:262
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1147
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
IPFWThreadVars_::dropped
uint32_t dropped
Definition: source-ipfw.c:120
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Packet_::flags
uint32_t flags
Definition: decode.h:449
threads.h
SC_ERR_IPFW_NOSUPPORT
@ SC_ERR_IPFW_NOSUPPORT
Definition: util-error.h:109
Packet_::ipfw_v
IPFWPacketVars ipfw_v
Definition: decode.h:468
packet-queue.h
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
util-privs.h
StatsSyncCountersIfSignalled
#define StatsSyncCountersIfSignalled(tv)
Definition: counters.h:137
SC_CAP_NET_BROADCAST
#define SC_CAP_NET_BROADCAST
Definition: util-privs.h:34
PacketDecodeFinalize
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:118
RunmodeGetActive
char * RunmodeGetActive(void)
Definition: runmodes.c:192
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
IPFW_MAX_QUEUE
#define IPFW_MAX_QUEUE
Definition: source-ipfw.h:28
THV_PAUSE
#define THV_PAUSE
Definition: threadvars.h:39
IPFW_DROP
#define IPFW_DROP
Definition: source-ipfw.c:44
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:54
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1150
SC_CAP_NET_BIND_SERVICE
#define SC_CAP_NET_BIND_SERVICE
Definition: util-privs.h:33
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:484
IPFWThreadVars_::accepted
uint32_t accepted
Definition: source-ipfw.c:119
SC_ERR_IPFW_BIND
@ SC_ERR_IPFW_BIND
Definition: util-error.h:110
decode.h
util-device.h
util-debug.h
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:51
TmModule_::PktAcqBreakLoop
TmEcode(* PktAcqBreakLoop)(ThreadVars *, void *)
Definition: tm-modules.h:57
IPFWQueueVars_::fd
int fd
Definition: source-ipfw.h:38
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
PKT_PSEUDO_STREAM_END
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1110
SC_ERR_IPFW_SETSOCKOPT
@ SC_ERR_IPFW_SETSOCKOPT
Definition: util-error.h:258
SC_WARN_IPFW_UNBIND
@ SC_WARN_IPFW_UNBIND
Definition: util-error.h:116
TmModuleVerdictIPFWRegister
void TmModuleVerdictIPFWRegister(void)
Registration Function for VerdictIPFW.
Definition: source-ipfw.c:171
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:231
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
IPFWThreadVars_::errs
uint32_t errs
Definition: source-ipfw.c:118
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:52
THV_KILL
#define THV_KILL
Definition: threadvars.h:41
SC_CAP_NET_ADMIN
#define SC_CAP_NET_ADMIN
Definition: util-privs.h:31
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:282
IPFWRegisterQueue
int IPFWRegisterQueue(char *queue)
Add an IPFW divert.
Definition: source-ipfw.c:701
IPFWQueueVars_::ipfw_sin
struct sockaddr_in ipfw_sin
Definition: source-ipfw.h:45
PacketPoolWait
void PacketPoolWait(void)
Definition: tmqh-packetpool.c:84
SCReturn
#define SCReturn
Definition: util-debug.h:302
IPV6Hdr_
Definition: decode-ipv6.h:32
Packet_
Definition: decode.h:414
TM_FLAG_DECODE_TM
#define TM_FLAG_DECODE_TM
Definition: tm-modules.h:32
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:33
DecodeIPV6
int DecodeIPV6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-ipv6.c:580
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:230
conf.h
source-ipfw.h
IPV4_GET_RAW_VER
#define IPV4_GET_RAW_VER(ip4h)
Definition: decode-ipv4.h:94
TmSlot_
Definition: tm-threads.h:52
TmEcode
TmEcode
Definition: tm-threads-common.h:79
IS_TUNNEL_PKT
#define IS_TUNNEL_PKT(p)
Definition: decode.h:894
IPFWThreadVars_::ipfw_index
uint16_t ipfw_index
Definition: source-ipfw.c:113
IPFWThreadVars
struct IPFWThreadVars_ IPFWThreadVars
Structure to hold thread specific variables.
TmModule_::name
const char * name
Definition: tm-modules.h:44
SC_WARN_IPFW_XMIT
@ SC_WARN_IPFW_XMIT
Definition: util-error.h:114
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
TM_FLAG_RECEIVE_TM
#define TM_FLAG_RECEIVE_TM
Definition: tm-modules.h:31
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:30
IP_MAXPACKET
#define IP_MAXPACKET
Definition: source-ipfw.c:49
IPV4Hdr_
Definition: decode-ipv4.h:71
tm-queuehandlers.h
IPFWQueueVars_::use_mutex
uint8_t use_mutex
Definition: source-ipfw.h:40
IPFWThreadVars_
Structure to hold thread specific variables.
Definition: source-ipfw.c:106
DecodeThreadVarsFree
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:658
IPFW_SOCKET_POLL_MSEC
#define IPFW_SOCKET_POLL_MSEC
Definition: source-ipfw.c:46
suricata-common.h
IPFWGetThread
void * IPFWGetThread(int number)
Get a pointer to the IPFW thread at index.
Definition: source-ipfw.c:766
IPFW_ACCEPT
#define IPFW_ACCEPT
Definition: source-ipfw.c:43
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
IPFWThreadVars_::pkts
uint32_t pkts
Definition: source-ipfw.c:116
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
IPFWThreadVars_::datalink
int datalink
Definition: source-ipfw.c:108
TMM_RECEIVEIPFW
@ TMM_RECEIVEIPFW
Definition: tm-threads-common.h:49
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
TMM_VERDICTIPFW
@ TMM_VERDICTIPFW
Definition: tm-threads-common.h:48
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:48
threadvars.h
TmModuleDecodeIPFWRegister
void TmModuleDecodeIPFWRegister(void)
Registration Function for DecodeIPFW.
Definition: source-ipfw.c:186
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
Packet_::root
struct Packet_ * root
Definition: decode.h:586
IPFWQueueVars_::port_num
uint16_t port_num
Definition: source-ipfw.h:42
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:631
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:639
IPFWQueueVars_::ipfw_sinlen
socklen_t ipfw_sinlen
Definition: source-ipfw.h:46
IPFWThreadVars_::bytes
uint64_t bytes
Definition: source-ipfw.c:117
suricata.h
SC_ERR_IPFW_SOCK
@ SC_ERR_IPFW_SOCK
Definition: util-error.h:111
IPFWPacketVars_::ipfw_index
int ipfw_index
Definition: source-ipfw.h:33
TMM_DECODEIPFW
@ TMM_DECODEIPFW
Definition: tm-threads-common.h:47
IPFWQueueVars_
Definition: source-ipfw.h:37
LiveRegisterDeviceName
int LiveRegisterDeviceName(const char *dev)
Add a device for monitoring.
Definition: util-device.c:95
TmThreadsCheckFlag
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
Definition: tm-threads.c:89
DecodeIPV4
int DecodeIPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-ipv4.c:517
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:232
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
SCMutex
#define SCMutex
Definition: threads-debug.h:114
IPFWThreadVars_::port_num
uint16_t port_num
Definition: source-ipfw.c:111
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:180
SC_CAP_NET_RAW
#define SC_CAP_NET_RAW
Definition: util-privs.h:32
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:70
SC_WARN_IPFW_RECV
@ SC_WARN_IPFW_RECV
Definition: util-error.h:113
DecodeUpdatePacketCounters
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:605
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:198
PACKET_TEST_ACTION
#define PACKET_TEST_ACTION(p, a)
Definition: decode.h:870