85 SCLogError(
"Error creating thread %s: you do not have support for WinDivert "
86 "enabled; please recompile with --enable-windivert",
95 typedef struct WinDivertThreadVars_ {
96 WinDivertHandle filter_handle;
99 int64_t qpc_start_time;
100 int64_t qpc_start_count;
101 int64_t qpc_freq_usec;
105 bool offload_enabled;
108 } WinDivertThreadVars;
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;
116 void *WinDivertGetThread(
int n)
121 return (
void *)&g_wd_tv[n];
124 void *WinDivertGetQueue(
int n)
129 return (
void *)&g_wd_qv[n];
133 #ifndef ERROR_INVALID_IMAGE_HASH
134 #define ERROR_INVALID_IMAGE_HASH 577L
136 #ifndef ERROR_DATA_NOT_ACCEPTED
137 #define ERROR_DATA_NOT_ACCEPTED 592L
144 static const char *WinDivertGetErrorString(DWORD error_code)
146 switch (error_code) {
148 case ERROR_FILE_NOT_FOUND:
149 return "The driver files WinDivert32.sys or WinDivert64.sys were "
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 "
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.";
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 "
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.";
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.";
203 #define WinDivertLogError(err_code) \
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); \
216 static void WinDivertInitQPCValues(WinDivertThreadVars *wd_tv)
219 (void)QueryPerformanceCounter((LARGE_INTEGER *)&wd_tv->qpc_start_count);
223 (void)QueryPerformanceFrequency((LARGE_INTEGER *)&wd_tv->qpc_freq_usec);
225 wd_tv->qpc_freq_usec /= 1000 * 1000;
231 static SCTime_t WinDivertTimestampToTimeStamp(WinDivertThreadVars *wd_tv, INT64 timestamp_count)
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;
241 tv.tv_sec = (long)(unix_usec / (1000 * 1000));
242 tv.tv_usec = (long)(unix_usec - (int64_t)
tv.tv_sec * (1000 * 1000));
256 int WinDivertRegisterQueue(
bool forward,
char *filter_str)
261 WINDIVERT_LAYER layer =
262 forward ? WINDIVERT_LAYER_NETWORK_FORWARD : WINDIVERT_LAYER_NETWORK;
265 const char *error_str;
267 bool valid = WinDivertHelperCompileFilter(filter_str, layer, 0, 0, &error_str, &error_pos);
269 SCLogWarning(
"Invalid filter \"%s\" supplied to WinDivert: %s at position "
271 filter_str, error_str, error_pos);
278 if (g_wd_num >= WINDIVERT_MAX_QUEUE) {
279 SCLogError(
"Too many WinDivert queues specified %" PRId32
"", g_wd_num);
285 memset(&g_wd_tv, 0,
sizeof(g_wd_tv));
286 memset(&g_wd_qv, 0,
sizeof(g_wd_qv));
290 WinDivertThreadVars *wd_tv = &g_wd_tv[g_wd_num];
291 wd_tv->thread_num = g_wd_num;
294 WinDivertQueueVars *wd_qv = &g_wd_qv[g_wd_num];
295 wd_qv->queue_num = g_wd_num;
297 WinDivertInitQPCValues(wd_tv);
300 size_t filter_len = strlen(filter_str);
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));
310 wd_qv->layer = layer;
326 wd_num_str[
sizeof(wd_num_str) - 1] = 0;
327 snprintf(wd_num_str,
sizeof(wd_num_str),
"%" PRId16
"", g_wd_num);
331 SCLogDebug(
"Queue %" PRId16
" registered", wd_qv->queue_num);
342 void ReceiveWinDivertThreadExitStats(
ThreadVars *,
void *);
357 static TmEcode WinDivertCloseHelper(WinDivertThreadVars *);
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 *);
369 tm_ptr->
name =
"ReceiveWinDivert";
370 tm_ptr->
ThreadInit = ReceiveWinDivertThreadInit;
381 tm_ptr->
name =
"VerdictWinDivert";
382 tm_ptr->
ThreadInit = VerdictWinDivertThreadInit;
383 tm_ptr->
Func = VerdictWinDivert;
392 tm_ptr->
name =
"DecodeWinDivert";
393 tm_ptr->
ThreadInit = DecodeWinDivertThreadInit;
394 tm_ptr->
Func = DecodeWinDivert;
406 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
407 wd_tv->slot = ((
TmSlot *)slot)->slot_next;
433 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
445 "PacketGetFromQueueOrAlloc() - failed to obtain Packet buffer");
453 bool success =
false;
455 if (wd_tv->offload_enabled) {
459 success = WinDivertRecv(
478 SCLogInfo(
"WinDivertRecv failed: error %" PRIu32
"",
479 (uint32_t)(GetLastError()));
484 p->
ts = WinDivertTimestampToTimeStamp(wd_tv, p->windivert_v.addr.Timestamp);
485 p->windivert_v.thread_num = wd_tv->thread_num;
498 if (TmThreadsSlotProcessPkt(
tv, wd_tv->slot, p) !=
TM_ECODE_OK) {
520 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)initdata;
527 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
536 if (wd_qv->filter_handle != NULL &&
537 wd_qv->filter_handle != INVALID_HANDLE_VALUE) {
543 if (WinDivertCollectFilterDevices(wd_tv, wd_qv) ==
TM_ECODE_OK) {
544 WinDivertDisableOffloading(wd_tv);
546 SCLogWarning(
"Failed to obtain network devices for WinDivert filter");
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());
562 wd_tv->filter_handle = wd_qv->filter_handle;
579 static TmEcode WinDivertCollectFilterDevices(WinDivertThreadVars *wd_tv,
580 WinDivertQueueVars *wd_qv)
585 IP_ADAPTER_ADDRESSES *if_info_list;
586 DWORD err = (DWORD)Win32GetAdaptersAddresses(&if_info_list);
587 if (err != NO_ERROR) {
592 for (IP_ADAPTER_ADDRESSES *if_info = if_info_list; if_info != NULL;
593 if_info = if_info->Next) {
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);
600 if (new_ldev == NULL) {
605 if (new_ldev->
dev == NULL) {
611 SCLogDebug(
"Adapter %s does not match WinDivert filter %s",
612 if_info->AdapterName, wd_qv->filter_str);
625 static bool WinDivertIfaceMatchFilter(
const char *filter_string,
int if_index)
629 WINDIVERT_ADDRESS if_addr = {};
630 if_addr.Network.IfIdx = if_index;
632 uint8_t dummy[4] = {4, 4, 4, 4};
634 match = WinDivertHelperEvalFilter(filter_string, dummy,
sizeof(dummy), &if_addr);
636 int err = GetLastError();
638 SCLogWarning(
"Failed to evaluate filter: 0x%" PRIx32, err);
650 static void WinDivertDisableOffloading(WinDivertThreadVars *wd_tv)
657 wd_tv->offload_enabled =
true;
661 wd_tv->offload_enabled =
true;
672 static void WinDivertRestoreOffloading(WinDivertThreadVars *wd_tv)
691 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
693 SCReturnCT(WinDivertCloseHelper(wd_tv),
"TmEcode");
703 void ReceiveWinDivertThreadExitStats(
ThreadVars *
tv,
void *data)
707 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
708 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
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);
735 ret = WinDivertVerdictHelper(
tv, p);
749 WinDivertThreadVars *wd_tv = WinDivertGetThread(p->windivert_v.thread_num);
752 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
755 p->windivert_v.verdicted =
true;
763 if (wd_tv->filter_handle == INVALID_HANDLE_VALUE ||
764 wd_tv->filter_handle == NULL) {
770 if (PacketIsTunnel(p)) {
771 bool finalVerdict = VerdictTunnelPacket(p);
777 if (p->
root != NULL) {
794 bool success = WinDivertSend(
798 WinDivertLogError(GetLastError());
820 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)initdata;
834 WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)data;
836 SCReturnCT(WinDivertCloseHelper(wd_tv),
"TmEcode");
866 SCLogDebug(
"packet unsupported by WinDivert, first byte: %02x",
906 static TmEcode WinDivertCloseHelper(WinDivertThreadVars *wd_tv)
911 WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num);
913 SCLogDebug(
"No queue could be found for thread num %" PRId32
"",
921 if (wd_qv->filter_handle == INVALID_HANDLE_VALUE ||
922 wd_qv->filter_handle == NULL) {
926 if (!WinDivertClose(wd_qv->filter_handle)) {
927 SCLogError(
"WinDivertClose failed: error %" PRIu32
"", (uint32_t)(GetLastError()));
932 (void)WinDivertRestoreOffloading(wd_tv);
934 wd_qv->filter_handle = NULL;
948 static int SourceWinDivertTestIfaceMatchFilter(
void)
956 struct testdata tests[] = {
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}};
972 size_t count = (
sizeof(tests) /
sizeof(tests[0]));
974 for (
size_t i = 0; i < count; i++) {
975 struct testdata test = tests[i];
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);
991 void SourceWinDivertRegisterTests(
void)
995 SourceWinDivertTestIfaceMatchFilter);