suricata
source-windivert.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 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 Jacob Masen-Smith <jacob@evengx.com>
22  *
23  * WinDivert emulation of netfilter_queue functionality to hook into Suricata's
24  * IPS mode. Supported solely on Windows.
25  *
26  */
27 
28 #include "suricata-common.h"
29 #include "suricata.h"
30 #include "tm-threads.h"
31 #include "packet.h"
32 #include "util-byte.h"
33 #include "util-debug.h"
34 #include "util-device-private.h"
35 #include "util-error.h"
36 #include "util-ioctl.h"
37 #include "util-privs.h"
38 #include "util-unittest.h"
39 
40 #include "runmodes.h"
41 
42 #include "queue.h"
43 
45 #include "source-windivert.h"
46 
47 #ifdef WINDIVERT
48 // clang-format off
49 #include <winsock2.h>
50 #include <windows.h>
51 #include <iptypes.h>
52 #include <winerror.h>
53 // clang-format on
54 #endif
55 
56 #ifndef WINDIVERT
57 /* Gracefully handle the case where no WinDivert support is compiled in */
58 
59 TmEcode NoWinDivertSupportExit(ThreadVars *, const void *, void **);
60 
62 {
63  tmm_modules[TMM_RECEIVEWINDIVERT].name = "ReceiveWinDivert";
66 }
67 
69 {
70  tmm_modules[TMM_VERDICTWINDIVERT].name = "VerdictWinDivert";
73 }
74 
76 {
77  tmm_modules[TMM_DECODEWINDIVERT].name = "DecodeWinDivert";
80 }
81 
83  void **data)
84 {
85  SCLogError("Error creating thread %s: you do not have support for WinDivert "
86  "enabled; please recompile with --enable-windivert",
87  tv->name);
88  exit(EXIT_FAILURE);
89 }
90 
91 #else /* implied we do have WinDivert support */
92 #include "action-globals.h"
93 #include "win32-syscall.h"
94 
95 typedef struct WinDivertThreadVars_ {
96  WinDivertHandle filter_handle;
97 
98  int thread_num;
99  int64_t qpc_start_time;
100  int64_t qpc_start_count;
101  int64_t qpc_freq_usec;
102 
103  TmSlot *slot;
104 
105  bool offload_enabled;
106 
107  TAILQ_HEAD(, LiveDevice_) live_devices;
108 } WinDivertThreadVars;
109 
110 #define WINDIVERT_MAX_QUEUE 16
111 static WinDivertThreadVars g_wd_tv[WINDIVERT_MAX_QUEUE];
112 static WinDivertQueueVars g_wd_qv[WINDIVERT_MAX_QUEUE];
113 static uint16_t g_wd_num = 0;
114 static SCMutex g_wd_init_lock = SCMUTEX_INITIALIZER;
115 
116 void *WinDivertGetThread(int n)
117 {
118  if (n >= g_wd_num) {
119  return NULL;
120  }
121  return (void *)&g_wd_tv[n];
122 }
123 
124 void *WinDivertGetQueue(int n)
125 {
126  if (n >= g_wd_num) {
127  return NULL;
128  }
129  return (void *)&g_wd_qv[n];
130 }
131 
132 // not defined in MinGW winerror.h
133 #ifndef ERROR_INVALID_IMAGE_HASH
134 #define ERROR_INVALID_IMAGE_HASH 577L
135 #endif
136 #ifndef ERROR_DATA_NOT_ACCEPTED
137 #define ERROR_DATA_NOT_ACCEPTED 592L
138 #endif
139 
140 /**
141  * \brief return an error description for Win32 error values commonly returned
142  * by WinDivert
143  */
144 static const char *WinDivertGetErrorString(DWORD error_code)
145 {
146  switch (error_code) {
147  // WinDivertOpen errors
148  case ERROR_FILE_NOT_FOUND:
149  return "The driver files WinDivert32.sys or WinDivert64.sys were "
150  "not found.";
151  case ERROR_ACCESS_DENIED:
152  return "Suricata must be run with Administrator privileges.";
153  case ERROR_INVALID_PARAMETER:
154  return "The WinDivert packet filter string is invalid.";
155  case ERROR_INVALID_IMAGE_HASH:
156  return "The WinDivert32.sys or WinDivert64.sys driver does not "
157  "have a valid digital signature, or your copy of Windows is "
158  "not up-to-date. Windows 7 and Server 2008 users need to "
159  "run Windows Update or install the following patch from "
160  "Microsoft: http://support.microsoft.com/kb/2949927";
161  case ERROR_DRIVER_BLOCKED:
162  return "This error occurs for various reasons, including: "
163  "attempting to load the 32-bit WinDivert.sys driver on a "
164  "64-bit system (or vice versa); the WinDivert.sys driver is "
165  "blocked by security software; or you are using a "
166  "virtualization environment that does not support "
167  "drivers.";
168  case EPT_S_NOT_REGISTERED:
169  return "This error occurs when the Base Filtering Engine service "
170  "has been disabled.";
171  case ERROR_PROC_NOT_FOUND:
172  return "The error may occur for Windows Vista users. The "
173  "solution is to install the following patch from Microsoft: "
174  "http://support.microsoft.com/kb/2761494.";
175 
176  // WinDivertSend errors
177  case ERROR_HOST_UNREACHABLE:
178  return "This error occurs when an impostor packet (with "
179  "pAddr->Impostor set to 1) is injected and the ip.TTL or "
180  "ipv6.HopLimit field goes to zero. This is a defense of "
181  "last resort against infinite loops caused by impostor "
182  "packets.";
183  case ERROR_DATA_NOT_ACCEPTED:
184  return "This error is returned when the user application attempts "
185  "to inject a malformed packet. It may also be returned for "
186  "valid inbound packets, and the Windows TCP/IP stack "
187  "rejects the packet for some reason.";
188  case ERROR_RETRY:
189  return "The underlying cause of this error is unknown. However, "
190  "this error usually occurs when certain kinds of "
191  "anti-virus/firewall/security software is installed, and "
192  "the error message usually resolves once the offending "
193  "program is uninstalled. This suggests a software "
194  "compatibility problem.";
195  default:
196  return "";
197  }
198 }
199 
200 /**
201  * \brief logs a WinDivert error at Error level.
202  */
203 #define WinDivertLogError(err_code) \
204  do { \
205  const char *win_err_str = Win32GetErrorString((err_code), NULL); \
206  SCLogError("WinDivertOpen failed, error %" PRId32 " (0x%08" PRIx32 "): %s %s", \
207  (uint32_t)(err_code), (uint32_t)(err_code), win_err_str, \
208  WinDivertGetErrorString(err_code)); \
209  LocalFree((LPVOID)win_err_str); \
210  } while (0);
211 
212 /**
213  * \brief initializes QueryPerformanceCounter values so we can get
214  * absolute time from WinDivert timestamps.
215  */
216 static void WinDivertInitQPCValues(WinDivertThreadVars *wd_tv)
217 {
218  SCTime_t now = TimeGet();
219  (void)QueryPerformanceCounter((LARGE_INTEGER *)&wd_tv->qpc_start_count);
220 
221  wd_tv->qpc_start_time = (uint64_t)SCTIME_SECS(now) * (1000 * 1000) + (uint64_t)SCTIME_SECS(now);
222 
223  (void)QueryPerformanceFrequency((LARGE_INTEGER *)&wd_tv->qpc_freq_usec);
224  /* \bug: clock drift? */
225  wd_tv->qpc_freq_usec /= 1000 * 1000;
226 }
227 
228 /**
229  * \brief WinDivert timestamp to a SCTime_t
230  */
231 static SCTime_t WinDivertTimestampToTimeStamp(WinDivertThreadVars *wd_tv, INT64 timestamp_count)
232 {
233  struct timeval tv;
234 
235  int64_t qpc_delta = (int64_t)timestamp_count - wd_tv->qpc_start_count;
236  int64_t unix_usec = wd_tv->qpc_start_time;
237  if (wd_tv->qpc_freq_usec) {
238  unix_usec += qpc_delta / wd_tv->qpc_freq_usec;
239  }
240 
241  tv.tv_sec = (long)(unix_usec / (1000 * 1000));
242  tv.tv_usec = (long)(unix_usec - (int64_t)tv.tv_sec * (1000 * 1000));
243 
244  return SCTIME_FROM_TIMEVAL(&tv);
245 }
246 
247 /**
248  * \brief initialize a WinDivert filter
249  *
250  * \param filter a WinDivert filter string as defined at
251  * https://www.reqrypt.org/windivert-doc.html#filter_language
252  *
253  * \retval 0 on success
254  * \retval -1 on failure
255  */
256 int WinDivertRegisterQueue(bool forward, char *filter_str)
257 {
258  SCEnter();
259  int ret = 0;
260 
261  WINDIVERT_LAYER layer =
262  forward ? WINDIVERT_LAYER_NETWORK_FORWARD : WINDIVERT_LAYER_NETWORK;
263 
264  /* validate the filter string */
265  const char *error_str;
266  uint32_t error_pos;
267  bool valid = WinDivertHelperCompileFilter(filter_str, layer, 0, 0, &error_str, &error_pos);
268  if (!valid) {
269  SCLogWarning("Invalid filter \"%s\" supplied to WinDivert: %s at position "
270  "%" PRId32 "",
271  filter_str, error_str, error_pos);
272  SCReturnInt(-1);
273  }
274 
275  /* initialize the queue */
276  SCMutexLock(&g_wd_init_lock);
277 
278  if (g_wd_num >= WINDIVERT_MAX_QUEUE) {
279  SCLogError("Too many WinDivert queues specified %" PRId32 "", g_wd_num);
280  ret = -1;
281  goto unlock;
282  }
283  if (g_wd_num == 0) {
284  /* on first registration, zero-initialize all array structs */
285  memset(&g_wd_tv, 0, sizeof(g_wd_tv));
286  memset(&g_wd_qv, 0, sizeof(g_wd_qv));
287  }
288 
289  /* init thread vars */
290  WinDivertThreadVars *wd_tv = &g_wd_tv[g_wd_num];
291  wd_tv->thread_num = g_wd_num;
292 
293  /* init queue vars */
294  WinDivertQueueVars *wd_qv = &g_wd_qv[g_wd_num];
295  wd_qv->queue_num = g_wd_num;
296 
297  WinDivertInitQPCValues(wd_tv);
298 
299  /* copy filter to persistent storage */
300  size_t filter_len = strlen(filter_str);
301  size_t copy_len =
302  strlcpy(wd_qv->filter_str, filter_str, sizeof(wd_qv->filter_str));
303  if (filter_len > copy_len) {
304  SCLogWarning("Queue length exceeds storage by %" PRId32 " bytes",
305  (int32_t)(filter_len - copy_len));
306  ret = -1;
307  goto unlock;
308  }
309 
310  wd_qv->layer = layer;
311  wd_qv->priority =
312  g_wd_num; /* priority set in the order filters are defined */
313  wd_qv->flags = 0; /* normal inline function */
314 
315  SCMutexInit(&wd_qv->filter_init_mutex, NULL);
316  SCMutexInit(&wd_qv->counters_mutex, NULL);
317 
318  g_wd_num++;
319 
320 unlock:
321  SCMutexUnlock(&g_wd_init_lock);
322 
323  if (ret == 0) {
324  // stringify queue index to use as thread name descriptor
325  char wd_num_str[6];
326  wd_num_str[sizeof(wd_num_str) - 1] = 0;
327  snprintf(wd_num_str, sizeof(wd_num_str), "%" PRId16 "", g_wd_num);
328 
329  LiveRegisterDevice(wd_num_str);
330 
331  SCLogDebug("Queue %" PRId16 " registered", wd_qv->queue_num);
332  }
333 
334  return ret;
335 }
336 
337 /* forward declarations of internal functions */
338 /* Receive functions */
339 TmEcode ReceiveWinDivertLoop(ThreadVars *, void *, void *);
340 TmEcode ReceiveWinDivertThreadInit(ThreadVars *, const void *, void **);
341 TmEcode ReceiveWinDivertThreadDeinit(ThreadVars *, void *);
342 void ReceiveWinDivertThreadExitStats(ThreadVars *, void *);
343 
344 /* Verdict functions */
345 TmEcode VerdictWinDivert(ThreadVars *, Packet *, void *);
346 TmEcode VerdictWinDivertThreadInit(ThreadVars *, const void *, void **);
347 TmEcode VerdictWinDivertThreadDeinit(ThreadVars *, void *);
348 
349 /* Decode functions */
350 TmEcode DecodeWinDivert(ThreadVars *, Packet *, void *);
351 TmEcode DecodeWinDivertThreadInit(ThreadVars *, const void *, void **);
352 TmEcode DecodeWinDivertThreadDeinit(ThreadVars *, void *);
353 
354 /* internal helper functions */
355 static TmEcode WinDivertRecvHelper(ThreadVars *tv, WinDivertThreadVars *);
356 static TmEcode WinDivertVerdictHelper(ThreadVars *tv, Packet *p);
357 static TmEcode WinDivertCloseHelper(WinDivertThreadVars *);
358 
359 static TmEcode WinDivertCollectFilterDevices(WinDivertThreadVars *,
360  WinDivertQueueVars *);
361 static bool WinDivertIfaceMatchFilter(const char *filter_string, int if_index);
362 static void WinDivertDisableOffloading(WinDivertThreadVars *);
363 static void WinDivertRestoreOffloading(WinDivertThreadVars *);
364 
366 {
368 
369  tm_ptr->name = "ReceiveWinDivert";
370  tm_ptr->ThreadInit = ReceiveWinDivertThreadInit;
371  tm_ptr->PktAcqLoop = ReceiveWinDivertLoop;
372  tm_ptr->ThreadExitPrintStats = ReceiveWinDivertThreadExitStats;
373  tm_ptr->ThreadDeinit = ReceiveWinDivertThreadDeinit;
374  tm_ptr->flags = TM_FLAG_RECEIVE_TM;
375 }
376 
378 {
380 
381  tm_ptr->name = "VerdictWinDivert";
382  tm_ptr->ThreadInit = VerdictWinDivertThreadInit;
383  tm_ptr->Func = VerdictWinDivert;
384  tm_ptr->ThreadDeinit = VerdictWinDivertThreadDeinit;
385  tm_ptr->flags = TM_FLAG_VERDICT_TM;
386 }
387 
389 {
391 
392  tm_ptr->name = "DecodeWinDivert";
393  tm_ptr->ThreadInit = DecodeWinDivertThreadInit;
394  tm_ptr->Func = DecodeWinDivert;
395  tm_ptr->ThreadDeinit = DecodeWinDivertThreadDeinit;
396  tm_ptr->flags = TM_FLAG_DECODE_TM;
397 }
398 
399 /**
400  * \brief Main WinDivert packet receive pump
401  */
402 TmEcode ReceiveWinDivertLoop(ThreadVars *tv, void *data, void *slot)
403 {
404  SCEnter();
405 
406  WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
407  wd_tv->slot = ((TmSlot *)slot)->slot_next;
408 
409  // Indicate that the thread is actually running its application level code (i.e., it can poll
410  // packets)
412 
413  while (true) {
416  }
417 
418  if (unlikely(WinDivertRecvHelper(tv, wd_tv) != TM_ECODE_OK)) {
420  }
421 
423  }
424 
426 }
427 
428 static TmEcode WinDivertRecvHelper(ThreadVars *tv, WinDivertThreadVars *wd_tv)
429 {
430  SCEnter();
431 
432 #ifdef COUNTERS
433  WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
434 #endif /* COUNTERS */
435 
436  /* make sure we have at least one packet in the packet pool, to prevent us
437  * from alloc'ing packets at line rate
438  */
439  PacketPoolWait();
440 
441  /* obtain a packet buffer */
443  if (unlikely(p == NULL)) {
444  SCLogDebug(
445  "PacketGetFromQueueOrAlloc() - failed to obtain Packet buffer");
447  }
449 
450  /* receive packet, depending on offload status. MTU is used as an estimator
451  * for direct data alloc size, and this is meaningless if large segments are
452  * coalesced before they reach WinDivert */
453  bool success = false;
454  uint32_t pktlen = 0;
455  if (wd_tv->offload_enabled) {
456  /* allocate external, if not already */
458 
459  success = WinDivertRecv(
460  wd_tv->filter_handle, p->ext_pkt, MAX_PAYLOAD_SIZE, &pktlen, &p->windivert_v.addr);
461  } else {
462  success = WinDivertRecv(wd_tv->filter_handle, GET_PKT_DIRECT_DATA(p),
463  GET_PKT_DIRECT_MAX_SIZE(p), &pktlen, &p->windivert_v.addr);
464  }
465  SET_PKT_LEN(p, pktlen);
466 
467  if (!success) {
468 #ifdef COUNTERS
469  SCMutexLock(&wd_qv->counters_mutex);
470  wd_qv->errs++;
471  SCMutexUnlock(&wd_qv->counters_mutex);
472 #endif /* COUNTERS */
473 
474  /* ensure packet length is zero to trigger an error in packet decoding
475  */
476  SET_PKT_LEN(p, 0);
477 
478  SCLogInfo("WinDivertRecv failed: error %" PRIu32 "",
479  (uint32_t)(GetLastError()));
481  }
482  SCLogDebug("Packet received, length %" PRId32 "", GET_PKT_LEN(p));
483 
484  p->ts = WinDivertTimestampToTimeStamp(wd_tv, p->windivert_v.addr.Timestamp);
485  p->windivert_v.thread_num = wd_tv->thread_num;
486 
487 #ifdef COUNTERS
488  SCMutexLock(&wd_qv->counters_mutex);
489  wd_qv->pkts++;
490  wd_qv->bytes += GET_PKT_LEN(p);
491  SCMutexUnlock(&wd_qv->counters_mutex);
492 #endif /* COUNTERS */
493 
494  /* Do the packet processing by calling TmThreadsSlotProcessPkt, this will,
495  * depending on the running mode, pass the packet to the treatment functions
496  * or push it to a packet pool. So processing time can vary.
497  */
498  if (TmThreadsSlotProcessPkt(tv, wd_tv->slot, p) != TM_ECODE_OK) {
500  }
501 
503 }
504 
505 /**
506  * \brief Init function for ReceiveWinDivert
507  *
508  * ReceiveWinDivertThreadInit sets up receiving packets via WinDivert.
509  *
510  * \param tv pointer to generic thread vars
511  * \param initdata pointer to the interface passed from the user
512  * \param data out-pointer to the WinDivert-specific thread vars
513  */
514 TmEcode ReceiveWinDivertThreadInit(ThreadVars *tv, const void *initdata,
515  void **data)
516 {
517  SCEnter();
518  TmEcode ret = TM_ECODE_OK;
519 
520  WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)initdata;
521 
522  if (wd_tv == NULL) {
523  SCLogError("initdata == NULL");
525  }
526 
527  WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
528 
529  if (wd_qv == NULL) {
530  SCLogError("queue == NULL");
532  }
533 
534  SCMutexLock(&wd_qv->filter_init_mutex);
535  /* does the queue already have an active handle? */
536  if (wd_qv->filter_handle != NULL &&
537  wd_qv->filter_handle != INVALID_HANDLE_VALUE) {
538  goto unlock;
539  }
540 
541  TAILQ_INIT(&wd_tv->live_devices);
542 
543  if (WinDivertCollectFilterDevices(wd_tv, wd_qv) == TM_ECODE_OK) {
544  WinDivertDisableOffloading(wd_tv);
545  } else {
546  SCLogWarning("Failed to obtain network devices for WinDivert filter");
547  }
548 
549  /* we open now so that we can immediately start handling packets,
550  * instead of losing however many would occur between registering the
551  * queue and starting a receive thread. */
552  wd_qv->filter_handle = WinDivertOpen(wd_qv->filter_str, wd_qv->layer,
553  wd_qv->priority, wd_qv->flags);
554  if (wd_qv->filter_handle == INVALID_HANDLE_VALUE) {
555  WinDivertLogError(GetLastError());
556  ret = TM_ECODE_FAILED;
557  goto unlock;
558  }
559 
560 unlock:
561  if (ret == 0) { /* success */
562  wd_tv->filter_handle = wd_qv->filter_handle;
563 
564  /* set our return context */
565  *data = wd_tv;
566  }
567 
568  SCMutexUnlock(&wd_qv->filter_init_mutex);
569  SCReturnInt(ret);
570 }
571 
572 /**
573  * \brief collect all devices covered by this filter in the thread vars'
574  * live devices list
575  *
576  * \param wd_tv pointer to WinDivert thread vars
577  * \param wd_qv pointer to WinDivert queue vars
578  */
579 static TmEcode WinDivertCollectFilterDevices(WinDivertThreadVars *wd_tv,
580  WinDivertQueueVars *wd_qv)
581 {
582  SCEnter();
583  TmEcode ret = TM_ECODE_OK;
584 
585  IP_ADAPTER_ADDRESSES *if_info_list;
586  DWORD err = (DWORD)Win32GetAdaptersAddresses(&if_info_list);
587  if (err != NO_ERROR) {
588  ret = TM_ECODE_FAILED;
589  goto release;
590  }
591 
592  for (IP_ADAPTER_ADDRESSES *if_info = if_info_list; if_info != NULL;
593  if_info = if_info->Next) {
594 
595  if (WinDivertIfaceMatchFilter(wd_qv->filter_str, if_info->IfIndex)) {
596  SCLogConfig("Found adapter %s matching WinDivert filter %s",
597  if_info->AdapterName, wd_qv->filter_str);
598 
599  LiveDevice *new_ldev = SCCalloc(1, sizeof(LiveDevice));
600  if (new_ldev == NULL) {
601  ret = TM_ECODE_FAILED;
602  goto release;
603  }
604  new_ldev->dev = SCStrdup(if_info->AdapterName);
605  if (new_ldev->dev == NULL) {
606  ret = TM_ECODE_FAILED;
607  goto release;
608  }
609  TAILQ_INSERT_TAIL(&wd_tv->live_devices, new_ldev, next);
610  } else {
611  SCLogDebug("Adapter %s does not match WinDivert filter %s",
612  if_info->AdapterName, wd_qv->filter_str);
613  }
614  }
615 
616 release:
617  SCFree(if_info_list);
618 
619  SCReturnInt(ret);
620 }
621 
622 /**
623  * \brief test if the specified interface index matches the filter
624  */
625 static bool WinDivertIfaceMatchFilter(const char *filter_string, int if_index)
626 {
627  bool match = false;
628 
629  WINDIVERT_ADDRESS if_addr = {};
630  if_addr.Network.IfIdx = if_index;
631 
632  uint8_t dummy[4] = {4, 4, 4, 4};
633 
634  match = WinDivertHelperEvalFilter(filter_string, dummy, sizeof(dummy), &if_addr);
635  if (!match) {
636  int err = GetLastError();
637  if (err != 0) {
638  SCLogWarning("Failed to evaluate filter: 0x%" PRIx32, err);
639  }
640  }
641 
642  return match;
643 }
644 
645 /**
646  * \brief disable offload status on devices for this filter
647  *
648  * \param wd_tv pointer to WinDivert thread vars
649  */
650 static void WinDivertDisableOffloading(WinDivertThreadVars *wd_tv)
651 {
652  for (LiveDevice *ldev = TAILQ_FIRST(&wd_tv->live_devices); ldev != NULL;
653  ldev = TAILQ_NEXT(ldev, next)) {
654 
655  if (LiveGetOffload() == 0) {
656  if (GetIfaceOffloading(ldev->dev, 1, 1) == 1) {
657  wd_tv->offload_enabled = true;
658  }
659  } else {
660  if (DisableIfaceOffloading(ldev, 1, 1) != 1) {
661  wd_tv->offload_enabled = true;
662  }
663  }
664  }
665 }
666 
667 /**
668  * \brief enable offload status on devices for this filter
669  *
670  * \param wd_tv pointer to WinDivert thread vars
671  */
672 static void WinDivertRestoreOffloading(WinDivertThreadVars *wd_tv)
673 {
674  for (LiveDevice *ldev = TAILQ_FIRST(&wd_tv->live_devices); ldev != NULL;
675  ldev = TAILQ_NEXT(ldev, next)) {
676 
678  }
679 }
680 
681 /**
682  * \brief Deinit function releases resources at exit.
683  *
684  * \param tv pointer to generic thread vars
685  * \param data pointer to WinDivert-specific thread vars
686  */
687 TmEcode ReceiveWinDivertThreadDeinit(ThreadVars *tv, void *data)
688 {
689  SCEnter();
690 
691  WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
692 
693  SCReturnCT(WinDivertCloseHelper(wd_tv), "TmEcode");
694 }
695 
696 /**
697  * \brief ExitStats prints stats to stdout at exit
698  *
699  *
700  * \param tv pointer to generic thread vars
701  * \param data pointer to WinDivert-specific thread vars
702  */
703 void ReceiveWinDivertThreadExitStats(ThreadVars *tv, void *data)
704 {
705  SCEnter();
706 
707  WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
708  WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
709  if (wd_qv == NULL) {
710  SCLogError("queue == NULL");
711  SCReturn;
712  }
713 
714  SCMutexLock(&wd_qv->counters_mutex);
715 
716  SCLogInfo("(%s) Packets %" PRIu32 ", Bytes %" PRIu64 ", Errors %" PRIu32 "",
717  tv->name, wd_qv->pkts, wd_qv->bytes, wd_qv->errs);
718  SCLogInfo("(%s) Verdict: Accepted %" PRIu32 ", Dropped %" PRIu32
719  ", Replaced %" PRIu32 "",
720  tv->name, wd_qv->accepted, wd_qv->dropped, wd_qv->replaced);
721 
722  SCMutexUnlock(&wd_qv->counters_mutex);
723  SCReturn;
724 }
725 
726 /**
727  * \brief WinDivert verdict module packet entry function
728  */
729 TmEcode VerdictWinDivert(ThreadVars *tv, Packet *p, void *data)
730 {
731  SCEnter();
732 
733  TmEcode ret = TM_ECODE_OK;
734 
735  ret = WinDivertVerdictHelper(tv, p);
736  if (ret != TM_ECODE_OK) {
737  SCReturnInt(ret);
738  }
739 
741 }
742 
743 /**
744  * \brief internal helper function to do the bulk of verdict work
745  */
746 static TmEcode WinDivertVerdictHelper(ThreadVars *tv, Packet *p)
747 {
748  SCEnter();
749  WinDivertThreadVars *wd_tv = WinDivertGetThread(p->windivert_v.thread_num);
750 
751 #ifdef COUNTERS
752  WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
753 #endif /* COUNTERS */
754 
755  p->windivert_v.verdicted = true;
756 
757  /* can't verdict a "fake" packet */
758  if (PKT_IS_PSEUDOPKT(p)) {
760  }
761 
762  /* the handle has been closed and we can no longer use it */
763  if (wd_tv->filter_handle == INVALID_HANDLE_VALUE ||
764  wd_tv->filter_handle == NULL) {
766  }
767 
768  /* we can't verdict tunnel packets without ensuring all encapsulated
769  * packets are verdicted */
770  if (PacketIsTunnel(p)) {
771  bool finalVerdict = VerdictTunnelPacket(p);
772  if (!finalVerdict) {
774  }
775 
776  // the action needs to occur on the root packet.
777  if (p->root != NULL) {
778  p = p->root;
779  }
780  }
781 
782  /* DROP simply means we do nothing; the WinDivert driver does the rest.
783  */
784  if (PacketCheckAction(p, ACTION_DROP)) {
785 #ifdef COUNTERS
786  SCMutexLock(&wd_qv->counters_mutex);
787  wd_qv->dropped++;
788  SCMutexUnlock(&wd_qv->counters_mutex);
789 #endif /* counters */
790 
792  }
793 
794  bool success = WinDivertSend(
795  wd_tv->filter_handle, GET_PKT_DATA(p), GET_PKT_LEN(p), NULL, &p->windivert_v.addr);
796 
797  if (unlikely(!success)) {
798  WinDivertLogError(GetLastError());
800  }
801 
802 #ifdef COUNTERS
803  SCMutexLock(&wd_qv->counters_mutex);
804  wd_qv->accepted++;
805  SCMutexUnlock(&wd_qv->counters_mutex);
806 #endif /* counters */
807 
809 }
810 
811 /**
812  * \brief init the verdict thread, which is piggybacked off the receive
813  * thread
814  */
815 TmEcode VerdictWinDivertThreadInit(ThreadVars *tv, const void *initdata,
816  void **data)
817 {
818  SCEnter();
819 
820  WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)initdata;
821  *data = wd_tv;
822 
824 }
825 
826 /**
827  * \brief deinit the verdict thread and shut down the WinDivert driver if
828  * it's still up.
829  */
830 TmEcode VerdictWinDivertThreadDeinit(ThreadVars *tv, void *data)
831 {
832  SCEnter();
833 
834  WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
835 
836  SCReturnCT(WinDivertCloseHelper(wd_tv), "TmEcode");
837 }
838 
839 /**
840  * \brief decode a raw packet submitted to suricata from the WinDivert
841  * driver
842  *
843  * All WinDivert packets are IPv4/v6, but do not include the network layer
844  * to differentiate the two, so instead we must check the version and go
845  * from there.
846  */
847 TmEcode DecodeWinDivert(ThreadVars *tv, Packet *p, void *data)
848 {
849  SCEnter();
850 
851  IPV4Hdr *ip4h = (IPV4Hdr *)GET_PKT_DATA(p);
852  IPV6Hdr *ip6h = (IPV6Hdr *)GET_PKT_DATA(p);
853  DecodeThreadVars *d_tv = (DecodeThreadVars *)data;
854 
856 
857  DecodeUpdatePacketCounters(tv, d_tv, p);
858 
859  if (IPV4_GET_RAW_VER(ip4h) == 4) {
860  SCLogDebug("IPv4 packet");
861  DecodeIPV4(tv, d_tv, p, GET_PKT_DATA(p), GET_PKT_LEN(p));
862  } else if (IPV6_GET_RAW_VER(ip6h) == 6) {
863  SCLogDebug("IPv6 packet");
864  DecodeIPV6(tv, d_tv, p, GET_PKT_DATA(p), GET_PKT_LEN(p));
865  } else {
866  SCLogDebug("packet unsupported by WinDivert, first byte: %02x",
867  *GET_PKT_DATA(p));
868  }
869 
870  PacketDecodeFinalize(tv, d_tv, p);
871 
873 }
874 
875 TmEcode DecodeWinDivertThreadInit(ThreadVars *tv, const void *initdata,
876  void **data)
877 {
878  SCEnter();
879 
881  if (d_tv == NULL) {
883  }
884 
886 
887  *data = d_tv;
888 
890 }
891 
892 TmEcode DecodeWinDivertThreadDeinit(ThreadVars *tv, void *data)
893 {
894  SCEnter();
895 
896  if (data != NULL) {
897  DecodeThreadVarsFree(tv, data);
898  }
899 
901 }
902 
903 /**
904  * \brief helper function for use with ThreadDeinit functions
905  */
906 static TmEcode WinDivertCloseHelper(WinDivertThreadVars *wd_tv)
907 {
908  SCEnter();
909  TmEcode ret = TM_ECODE_OK;
910 
911  WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
912  if (wd_qv == NULL) {
913  SCLogDebug("No queue could be found for thread num %" PRId32 "",
914  wd_tv->thread_num);
916  }
917 
918  SCMutexLock(&wd_qv->filter_init_mutex);
919 
920  /* check if there's nothing to close */
921  if (wd_qv->filter_handle == INVALID_HANDLE_VALUE ||
922  wd_qv->filter_handle == NULL) {
923  goto unlock;
924  }
925 
926  if (!WinDivertClose(wd_qv->filter_handle)) {
927  SCLogError("WinDivertClose failed: error %" PRIu32 "", (uint32_t)(GetLastError()));
928  ret = TM_ECODE_FAILED;
929  goto unlock;
930  }
931 
932  (void)WinDivertRestoreOffloading(wd_tv);
933 
934  wd_qv->filter_handle = NULL;
935 
936 unlock:
937  SCMutexUnlock(&wd_qv->filter_init_mutex);
938 
939  if (ret == TM_ECODE_OK) {
940  SCMutexDestroy(&wd_qv->filter_init_mutex);
941  SCMutexDestroy(&wd_qv->counters_mutex);
942  }
943 
944  SCReturnInt(ret);
945 }
946 
947 #ifdef UNITTESTS
948 static int SourceWinDivertTestIfaceMatchFilter(void)
949 {
950  struct testdata {
951  const char *filter;
952  int if_index;
953  bool expected;
954  };
955 
956  struct testdata tests[] = {
957  {"true", 11, true},
958  {"ifIdx=11", 11, true},
959  {"ifIdx==11", 11, true},
960  {"ifIdx!=11", 1, true},
961  {"ifIdx!=11", 11, false},
962  {"ifIdx=3", 4, false},
963  {"ifIdx=11 || ifIdx=5", 5, true},
964  {"ifIdx=11 || ifIdx=4", 5, false},
965  {"ifIdx<3 || ifIdx>7", 8, true},
966  {"ifIdx<3 || ifIdx>7", 5, false},
967  {"ifIdx>3 or ifIdx<7", 5, true},
968  {"ifIdx>3 && ifIdx<7", 5, true},
969  {"ifIdx>3 && ifIdx<7", 1, false},
970  {"(ifIdx > 3 && ifIdx < 7) or ifIdx == 11", 11, true}};
971 
972  size_t count = (sizeof(tests) / sizeof(tests[0]));
973 
974  for (size_t i = 0; i < count; i++) {
975  struct testdata test = tests[i];
976 
977  bool actual = WinDivertIfaceMatchFilter(test.filter, test.if_index);
978  if (actual != test.expected) {
979  printf("WinDivertIfaceMatchFilter(\"%s\", %d) == %d, expected %d\n",
980  test.filter, test.if_index, actual, test.expected);
981  FAIL;
982  }
983  }
984  PASS;
985 }
986 #endif
987 
988 /**
989  * \brief this function registers unit tests for the WinDivert Source
990  */
991 void SourceWinDivertRegisterTests(void)
992 {
993 #ifdef UNITTESTS
994  UtRegisterTest("SourceWinDivertTestIfaceMatchFilter",
995  SourceWinDivertTestIfaceMatchFilter);
996 #endif
997 }
998 
999 #endif /* WINDIVERT */
PacketCheckAction
bool PacketCheckAction(const Packet *p, const uint8_t a)
Definition: packet.c:50
util-device-private.h
util-byte.h
tm-threads.h
win32-syscall.h
LiveRegisterDevice
int LiveRegisterDevice(const char *dev)
Add a pcap device for monitoring and create structure.
Definition: util-device.c:132
IPV6_GET_RAW_VER
#define IPV6_GET_RAW_VER(ip6h)
Definition: decode-ipv6.h:62
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
StatsSyncCountersIfSignalled
void StatsSyncCountersIfSignalled(StatsThreadContext *stats)
Definition: counters.c:483
TAILQ_INIT
#define TAILQ_INIT(head)
Definition: queue.h:262
TmModuleDecodeWinDivertRegister
void TmModuleDecodeWinDivertRegister(void)
Definition: source-windivert.c:75
TM_FLAG_VERDICT_TM
#define TM_FLAG_VERDICT_TM
Definition: tm-modules.h:35
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1323
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
Definition: tm-threads.c:101
LiveGetOffload
int LiveGetOffload(void)
Definition: util-device.c:87
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
action-globals.h
LiveDevice_
Definition: util-device-private.h:32
source-windivert-prototypes.h
THV_RUNNING
#define THV_RUNNING
Definition: threadvars.h:55
SURICATA_STOP
#define SURICATA_STOP
Definition: suricata.h:94
NoWinDivertSupportExit
TmEcode NoWinDivertSupportExit(ThreadVars *, const void *, void **)
Definition: source-windivert.c:82
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
TmModuleVerdictWinDivertRegister
void TmModuleVerdictWinDivertRegister(void)
Definition: source-windivert.c:68
util-privs.h
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
PacketDecodeFinalize
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:236
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
GET_PKT_DIRECT_MAX_SIZE
#define GET_PKT_DIRECT_MAX_SIZE(p)
Definition: decode.h:211
util-unittest.h
TMM_DECODEWINDIVERT
@ TMM_DECODEWINDIVERT
Definition: tm-threads-common.h:66
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:58
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:53
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1327
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:632
SET_PKT_LEN
#define SET_PKT_LEN(p, len)
Definition: decode.h:213
util-debug.h
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:52
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
Packet_::ts
SCTime_t ts
Definition: decode.h:555
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:209
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
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
RestoreIfaceOffloading
void RestoreIfaceOffloading(LiveDevice *dev)
Definition: util-ioctl.c:700
LiveDevice_::dev
char * dev
Definition: util-device-private.h:33
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
PacketCallocExtPkt
int PacketCallocExtPkt(Packet *p, int datalen)
Definition: decode.c:313
PacketPoolWait
void PacketPoolWait(void)
Definition: tmqh-packetpool.c:71
SCReturn
#define SCReturn
Definition: util-debug.h:286
IPV6Hdr_
Definition: decode-ipv6.h:32
Packet_
Definition: decode.h:501
TM_FLAG_DECODE_TM
#define TM_FLAG_DECODE_TM
Definition: tm-modules.h:33
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:29
DecodeIPV6
int DecodeIPV6(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-ipv6.c:549
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:208
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
IPV4_GET_RAW_VER
#define IPV4_GET_RAW_VER(ip4h)
Definition: decode-ipv4.h:95
TmSlot_
Definition: tm-threads.h:53
SCTime_t
Definition: util-time.h:40
TmEcode
TmEcode
Definition: tm-threads-common.h:80
source-windivert.h
GetIfaceOffloading
int GetIfaceOffloading(const char *dev, int csum, int other)
output offloading status of the link
Definition: util-ioctl.c:670
queue.h
DisableIfaceOffloading
int DisableIfaceOffloading(LiveDevice *dev, int csum, int other)
Definition: util-ioctl.c:683
TmModule_::name
const char * name
Definition: tm-modules.h:48
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
TM_FLAG_RECEIVE_TM
#define TM_FLAG_RECEIVE_TM
Definition: tm-modules.h:32
TmModule_
Definition: tm-modules.h:47
IPV4Hdr_
Definition: decode-ipv4.h:72
DecodeThreadVarsFree
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:834
suricata-common.h
packet.h
TAILQ_NEXT
#define TAILQ_NEXT(elm, field)
Definition: queue.h:307
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
Packet_::ext_pkt
uint8_t * ext_pkt
Definition: decode.h:615
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:51
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
TMM_RECEIVEWINDIVERT
@ TMM_RECEIVEWINDIVERT
Definition: tm-threads-common.h:64
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:52
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
Packet_::root
struct Packet_ * root
Definition: decode.h:653
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:963
MAX_PAYLOAD_SIZE
#define MAX_PAYLOAD_SIZE
Definition: decode.h:701
util-ioctl.h
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:816
TAILQ_HEAD
#define TAILQ_HEAD(name, type)
Definition: queue.h:230
GET_PKT_DIRECT_DATA
#define GET_PKT_DIRECT_DATA(p)
Definition: decode.h:210
SCReturnCT
#define SCReturnCT(x, type)
Definition: util-debug.h:298
suricata.h
TMM_VERDICTWINDIVERT
@ TMM_VERDICTWINDIVERT
Definition: tm-threads-common.h:65
DecodeIPV4
int DecodeIPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
Definition: decode-ipv4.c:515
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:121
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:297
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:80
DecodeUpdatePacketCounters
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:783
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:176
TmModuleReceiveWinDivertRegister
void TmModuleReceiveWinDivertRegister(void)
Definition: source-windivert.c:61