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