84 SCLogError(
"Error creating thread %s: you do not have support for WinDivert "
85 "enabled; please recompile with --enable-windivert",
94 typedef struct WinDivertThreadVars_ {
95 WinDivertHandle filter_handle;
98 int64_t qpc_start_time;
99 int64_t qpc_start_count;
100 int64_t qpc_freq_usec;
104 bool offload_enabled;
107 } WinDivertThreadVars;
109 #define WINDIVERT_MAX_QUEUE 16
110 static WinDivertThreadVars g_wd_tv[WINDIVERT_MAX_QUEUE];
111 static WinDivertQueueVars g_wd_qv[WINDIVERT_MAX_QUEUE];
112 static uint16_t g_wd_num = 0;
115 void *WinDivertGetThread(
int n)
120 return (
void *)&g_wd_tv[n];
123 void *WinDivertGetQueue(
int n)
128 return (
void *)&g_wd_qv[n];
132 #ifndef ERROR_INVALID_IMAGE_HASH
133 #define ERROR_INVALID_IMAGE_HASH 577L
135 #ifndef ERROR_DATA_NOT_ACCEPTED
136 #define ERROR_DATA_NOT_ACCEPTED 592L
143 static const char *WinDivertGetErrorString(DWORD error_code)
145 switch (error_code) {
147 case ERROR_FILE_NOT_FOUND:
148 return "The driver files WinDivert32.sys or WinDivert64.sys were "
150 case ERROR_ACCESS_DENIED:
151 return "Suricata must be run with Administrator privileges.";
152 case ERROR_INVALID_PARAMETER:
153 return "The WinDivert packet filter string is invalid.";
154 case ERROR_INVALID_IMAGE_HASH:
155 return "The WinDivert32.sys or WinDivert64.sys driver does not "
156 "have a valid digital signature, or your copy of Windows is "
157 "not up-to-date. Windows 7 and Server 2008 users need to "
158 "run Windows Update or install the following patch from "
159 "Microsoft: http://support.microsoft.com/kb/2949927";
160 case ERROR_DRIVER_BLOCKED:
161 return "This error occurs for various reasons, including: "
162 "attempting to load the 32-bit WinDivert.sys driver on a "
163 "64-bit system (or vice versa); the WinDivert.sys driver is "
164 "blocked by security software; or you are using a "
165 "virtualization environment that does not support "
167 case EPT_S_NOT_REGISTERED:
168 return "This error occurs when the Base Filtering Engine service "
169 "has been disabled.";
170 case ERROR_PROC_NOT_FOUND:
171 return "The error may occur for Windows Vista users. The "
172 "solution is to install the following patch from Microsoft: "
173 "http://support.microsoft.com/kb/2761494.";
176 case ERROR_HOST_UNREACHABLE:
177 return "This error occurs when an impostor packet (with "
178 "pAddr->Impostor set to 1) is injected and the ip.TTL or "
179 "ipv6.HopLimit field goes to zero. This is a defense of "
180 "last resort against infinite loops caused by impostor "
182 case ERROR_DATA_NOT_ACCEPTED:
183 return "This error is returned when the user application attempts "
184 "to inject a malformed packet. It may also be returned for "
185 "valid inbound packets, and the Windows TCP/IP stack "
186 "rejects the packet for some reason.";
188 return "The underlying cause of this error is unknown. However, "
189 "this error usually occurs when certain kinds of "
190 "anti-virus/firewall/security software is installed, and "
191 "the error message usually resolves once the offending "
192 "program is uninstalled. This suggests a software "
193 "compatibility problem.";
202 #define WinDivertLogError(err_code) \
204 const char *win_err_str = Win32GetErrorString((err_code), NULL); \
205 SCLogError("WinDivertOpen failed, error %" PRId32 " (0x%08" PRIx32 "): %s %s", \
206 (uint32_t)(err_code), (uint32_t)(err_code), win_err_str, \
207 WinDivertGetErrorString(err_code)); \
208 LocalFree((LPVOID)win_err_str); \
215 static void WinDivertInitQPCValues(WinDivertThreadVars *wd_tv)
218 (void)QueryPerformanceCounter((LARGE_INTEGER *)&wd_tv->qpc_start_count);
222 (void)QueryPerformanceFrequency((LARGE_INTEGER *)&wd_tv->qpc_freq_usec);
224 wd_tv->qpc_freq_usec /= 1000 * 1000;
230 static SCTime_t WinDivertTimestampToTimeStamp(WinDivertThreadVars *wd_tv, INT64 timestamp_count)
234 int64_t qpc_delta = (int64_t)timestamp_count - wd_tv->qpc_start_count;
235 int64_t unix_usec = wd_tv->qpc_start_time;
236 if (wd_tv->qpc_freq_usec) {
237 unix_usec += qpc_delta / wd_tv->qpc_freq_usec;
240 tv.tv_sec = (long)(unix_usec / (1000 * 1000));
241 tv.tv_usec = (long)(unix_usec - (int64_t)
tv.tv_sec * (1000 * 1000));
255 int WinDivertRegisterQueue(
bool forward,
char *filter_str)
260 WINDIVERT_LAYER layer =
261 forward ? WINDIVERT_LAYER_NETWORK_FORWARD : WINDIVERT_LAYER_NETWORK;
264 const char *error_str;
266 bool valid = WinDivertHelperCompileFilter(filter_str, layer, 0, 0, &error_str, &error_pos);
268 SCLogWarning(
"Invalid filter \"%s\" supplied to WinDivert: %s at position "
270 filter_str, error_str, error_pos);
277 if (g_wd_num >= WINDIVERT_MAX_QUEUE) {
278 SCLogError(
"Too many WinDivert queues specified %" PRId32
"", g_wd_num);
284 memset(&g_wd_tv, 0,
sizeof(g_wd_tv));
285 memset(&g_wd_qv, 0,
sizeof(g_wd_qv));
289 WinDivertThreadVars *wd_tv = &g_wd_tv[g_wd_num];
290 wd_tv->thread_num = g_wd_num;
293 WinDivertQueueVars *wd_qv = &g_wd_qv[g_wd_num];
294 wd_qv->queue_num = g_wd_num;
296 WinDivertInitQPCValues(wd_tv);
299 size_t filter_len = strlen(filter_str);
301 strlcpy(wd_qv->filter_str, filter_str,
sizeof(wd_qv->filter_str));
302 if (filter_len > copy_len) {
303 SCLogWarning(
"Queue length exceeds storage by %" PRId32
" bytes",
304 (int32_t)(filter_len - copy_len));
309 wd_qv->layer = layer;
325 wd_num_str[
sizeof(wd_num_str) - 1] = 0;
326 snprintf(wd_num_str,
sizeof(wd_num_str),
"%" PRId16
"", g_wd_num);
330 SCLogDebug(
"Queue %" PRId16
" registered", wd_qv->queue_num);
341 void ReceiveWinDivertThreadExitStats(
ThreadVars *,
void *);
356 static TmEcode WinDivertCloseHelper(WinDivertThreadVars *);
358 static TmEcode WinDivertCollectFilterDevices(WinDivertThreadVars *,
359 WinDivertQueueVars *);
360 static bool WinDivertIfaceMatchFilter(
const char *filter_string,
int if_index);
361 static void WinDivertDisableOffloading(WinDivertThreadVars *);
362 static void WinDivertRestoreOffloading(WinDivertThreadVars *);
368 tm_ptr->
name =
"ReceiveWinDivert";
369 tm_ptr->
ThreadInit = ReceiveWinDivertThreadInit;
380 tm_ptr->
name =
"VerdictWinDivert";
381 tm_ptr->
ThreadInit = VerdictWinDivertThreadInit;
382 tm_ptr->
Func = VerdictWinDivert;
391 tm_ptr->
name =
"DecodeWinDivert";
392 tm_ptr->
ThreadInit = DecodeWinDivertThreadInit;
393 tm_ptr->
Func = DecodeWinDivert;
405 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
406 wd_tv->slot = ((
TmSlot *)slot)->slot_next;
432 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
444 "PacketGetFromQueueOrAlloc() - failed to obtain Packet buffer");
452 bool success =
false;
454 if (wd_tv->offload_enabled) {
458 success = WinDivertRecv(
477 SCLogInfo(
"WinDivertRecv failed: error %" PRIu32
"",
478 (uint32_t)(GetLastError()));
483 p->
ts = WinDivertTimestampToTimeStamp(wd_tv,
p->windivert_v.addr.Timestamp);
484 p->windivert_v.thread_num = wd_tv->thread_num;
519 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)initdata;
526 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
535 if (wd_qv->filter_handle != NULL &&
536 wd_qv->filter_handle != INVALID_HANDLE_VALUE) {
542 if (WinDivertCollectFilterDevices(wd_tv, wd_qv) ==
TM_ECODE_OK) {
543 WinDivertDisableOffloading(wd_tv);
545 SCLogWarning(
"Failed to obtain network devices for WinDivert filter");
551 wd_qv->filter_handle = WinDivertOpen(wd_qv->filter_str, wd_qv->layer,
552 wd_qv->priority, wd_qv->flags);
553 if (wd_qv->filter_handle == INVALID_HANDLE_VALUE) {
554 WinDivertLogError(GetLastError());
561 wd_tv->filter_handle = wd_qv->filter_handle;
578 static TmEcode WinDivertCollectFilterDevices(WinDivertThreadVars *wd_tv,
579 WinDivertQueueVars *wd_qv)
584 IP_ADAPTER_ADDRESSES *if_info_list;
585 DWORD err = (DWORD)Win32GetAdaptersAddresses(&if_info_list);
586 if (err != NO_ERROR) {
591 for (IP_ADAPTER_ADDRESSES *if_info = if_info_list; if_info != NULL;
592 if_info = if_info->Next) {
594 if (WinDivertIfaceMatchFilter(wd_qv->filter_str, if_info->IfIndex)) {
595 SCLogConfig(
"Found adapter %s matching WinDivert filter %s",
596 if_info->AdapterName, wd_qv->filter_str);
599 if (new_ldev == NULL) {
604 if (new_ldev->
dev == NULL) {
610 SCLogDebug(
"Adapter %s does not match WinDivert filter %s",
611 if_info->AdapterName, wd_qv->filter_str);
624 static bool WinDivertIfaceMatchFilter(
const char *filter_string,
int if_index)
628 WINDIVERT_ADDRESS if_addr = {};
629 if_addr.Network.IfIdx = if_index;
631 uint8_t dummy[4] = {4, 4, 4, 4};
633 match = WinDivertHelperEvalFilter(filter_string, dummy,
sizeof(dummy), &if_addr);
635 int err = GetLastError();
637 SCLogWarning(
"Failed to evaluate filter: 0x%" PRIx32, err);
649 static void WinDivertDisableOffloading(WinDivertThreadVars *wd_tv)
656 wd_tv->offload_enabled =
true;
660 wd_tv->offload_enabled =
true;
671 static void WinDivertRestoreOffloading(WinDivertThreadVars *wd_tv)
690 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
692 SCReturnCT(WinDivertCloseHelper(wd_tv),
"TmEcode");
702 void ReceiveWinDivertThreadExitStats(
ThreadVars *
tv,
void *data)
706 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
707 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
715 SCLogInfo(
"(%s) Packets %" PRIu32
", Bytes %" PRIu64
", Errors %" PRIu32
"",
716 tv->
name, wd_qv->pkts, wd_qv->bytes, wd_qv->errs);
717 SCLogInfo(
"(%s) Verdict: Accepted %" PRIu32
", Dropped %" PRIu32
718 ", Replaced %" PRIu32
"",
719 tv->
name, wd_qv->accepted, wd_qv->dropped, wd_qv->replaced);
734 ret = WinDivertVerdictHelper(
tv,
p);
748 WinDivertThreadVars *wd_tv = WinDivertGetThread(
p->windivert_v.thread_num);
751 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
754 p->windivert_v.verdicted =
true;
762 if (wd_tv->filter_handle == INVALID_HANDLE_VALUE ||
763 wd_tv->filter_handle == NULL) {
769 if (PacketIsTunnel(
p)) {
770 bool finalVerdict = VerdictTunnelPacket(
p);
776 if (
p->
root != NULL) {
793 bool success = WinDivertSend(
797 WinDivertLogError(GetLastError());
819 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)initdata;
833 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
835 SCReturnCT(WinDivertCloseHelper(wd_tv),
"TmEcode");
865 SCLogDebug(
"packet unsupported by WinDivert, first byte: %02x",
905 static TmEcode WinDivertCloseHelper(WinDivertThreadVars *wd_tv)
910 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
912 SCLogDebug(
"No queue could be found for thread num %" PRId32
"",
920 if (wd_qv->filter_handle == INVALID_HANDLE_VALUE ||
921 wd_qv->filter_handle == NULL) {
925 if (!WinDivertClose(wd_qv->filter_handle)) {
926 SCLogError(
"WinDivertClose failed: error %" PRIu32
"", (uint32_t)(GetLastError()));
931 (void)WinDivertRestoreOffloading(wd_tv);
933 wd_qv->filter_handle = NULL;
947 static int SourceWinDivertTestIfaceMatchFilter(
void)
955 struct testdata tests[] = {
957 {
"ifIdx=11", 11,
true},
958 {
"ifIdx==11", 11,
true},
959 {
"ifIdx!=11", 1,
true},
960 {
"ifIdx!=11", 11,
false},
961 {
"ifIdx=3", 4,
false},
962 {
"ifIdx=11 || ifIdx=5", 5,
true},
963 {
"ifIdx=11 || ifIdx=4", 5,
false},
964 {
"ifIdx<3 || ifIdx>7", 8,
true},
965 {
"ifIdx<3 || ifIdx>7", 5,
false},
966 {
"ifIdx>3 or ifIdx<7", 5,
true},
967 {
"ifIdx>3 && ifIdx<7", 5,
true},
968 {
"ifIdx>3 && ifIdx<7", 1,
false},
969 {
"(ifIdx > 3 && ifIdx < 7) or ifIdx == 11", 11,
true}};
971 size_t count = (
sizeof(tests) /
sizeof(tests[0]));
973 for (
size_t i = 0; i < count; i++) {
974 struct testdata test = tests[i];
976 bool actual = WinDivertIfaceMatchFilter(test.filter, test.if_index);
977 if (actual != test.expected) {
978 printf(
"WinDivertIfaceMatchFilter(\"%s\", %d) == %d, expected %d\n",
979 test.filter, test.if_index, actual, test.expected);
990 void SourceWinDivertRegisterTests(
void)
994 SourceWinDivertTestIfaceMatchFilter);