Skip to content

Commit 20382a9

Browse files
committed
Reverse order of WinDivert handle priorities.
Higher values now correspond to higher priorities.
1 parent 81ce9b7 commit 20382a9

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,7 @@ WinDivert 2.0.0-rc
276276
ip.TTL/ipv6.HopLimit field of a packet.
277277
- Add new WinDivertHelperNto*()/WinDivertHelperHton*() helper functions
278278
for swapping from network to host byte ordering, and vice versa.
279+
- WinDivertOpen() priorities now are ascending, meaning that higher
280+
values correspond to higher priorities.
281+
- The last two arguments for WinDivertRecv() and WinDivertSend() have been
282+
swapped.

include/windivert.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ extern WINDIVERTEXPORT BOOL WinDivertGetParam(
300300
/*
301301
* WinDivert constants.
302302
*/
303-
#define WINDIVERT_PRIORITY_LOWEST 30000
304-
#define WINDIVERT_PRIORITY_HIGHEST (-WINDIVERT_PRIORITY_LOWEST)
303+
#define WINDIVERT_PRIORITY_HIGHEST 30000
304+
#define WINDIVERT_PRIORITY_LOWEST (-WINDIVERT_PRIORITY_HIGHEST)
305305
#define WINDIVERT_PARAM_QUEUE_LENGTH_DEFAULT 4096
306306
#define WINDIVERT_PARAM_QUEUE_LENGTH_MIN 32
307307
#define WINDIVERT_PARAM_QUEUE_LENGTH_MAX 16384

include/windivert_device.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@
219219
/*
220220
* WinDivert priorities.
221221
*/
222-
#define WINDIVERT_PRIORITY_MAX WINDIVERT_PRIORITY_LOWEST
223-
#define WINDIVERT_PRIORITY_MIN WINDIVERT_PRIORITY_HIGHEST
222+
#define WINDIVERT_PRIORITY_MAX WINDIVERT_PRIORITY_HIGHEST
223+
#define WINDIVERT_PRIORITY_MIN WINDIVERT_PRIORITY_LOWEST
224224

225225
/*
226226
* WinDivert message definitions.

sys/windivert.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ static MM_PAGE_PRIORITY no_write_flag = 0;
317317
static MM_PAGE_PRIORITY no_exec_flag = 0;
318318

319319
/*
320-
* Priorities & weights.
320+
* Priorities.
321321
*/
322322
static UINT32 windivert_context_priority(UINT32 priority)
323323
{
@@ -328,9 +328,6 @@ static UINT32 windivert_context_priority(UINT32 priority)
328328
return priority;
329329
}
330330

331-
#define WINDIVERT_FILTER_WEIGHT(priority) \
332-
((UINT64)((UINT64)UINT32_MAX - (priority)))
333-
334331
/*
335332
* Prototypes.
336333
*/
@@ -1674,7 +1671,7 @@ static NTSTATUS windivert_install_callout(context_t context, UINT idx,
16741671
engine_handle = context->engine_handle;
16751672
KeReleaseInStackQueuedSpinLock(&lock_handle);
16761673

1677-
weight = WINDIVERT_FILTER_WEIGHT(priority);
1674+
weight = (UINT64)priority;
16781675

16791676
RtlZeroMemory(&scallout, sizeof(scallout));
16801677
scallout.calloutKey = callout_guid;
@@ -1876,7 +1873,6 @@ extern VOID windivert_cleanup(IN WDFFILEOBJECT object)
18761873
WDFWORKITEM worker;
18771874
LONGLONG timestamp;
18781875
BOOL sniff_mode, timeout, forward;
1879-
UINT priority;
18801876
NTSTATUS status;
18811877

18821878
DEBUG("CLEANUP: cleaning up WinDivert context (context=%p)", context);
@@ -1897,7 +1893,6 @@ extern VOID windivert_cleanup(IN WDFFILEOBJECT object)
18971893
context->state = WINDIVERT_CONTEXT_STATE_CLOSING;
18981894
sniff_mode = ((context->flags & WINDIVERT_FLAG_SNIFF) != 0);
18991895
forward = (context->layer == WINDIVERT_LAYER_NETWORK_FORWARD);
1900-
priority = context->priority;
19011896
while (!IsListEmpty(&context->flow_set))
19021897
{
19031898
entry = RemoveHeadList(&context->flow_set);
@@ -3664,7 +3659,7 @@ static void windivert_network_classify(context_t context,
36643659
packet_state == FWPS_PACKET_PREVIOUSLY_INJECTED_BY_SELF)
36653660
{
36663661
packet_priority = (UINT32)packet_context;
3667-
if (packet_priority >= priority)
3662+
if (packet_priority <= priority)
36683663
{
36693664
WdfObjectDereference(object);
36703665
return;

test/test.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -855,9 +855,9 @@ int main(void)
855855
// Open handles to:
856856
// (1) stop normal traffic from interacting with the tests; and
857857
// (2) stop test packets escaping to the Internet or TCP/IP stack.
858-
upper_handle = WinDivertOpen("true", WINDIVERT_LAYER_NETWORK, -999,
858+
upper_handle = WinDivertOpen("true", WINDIVERT_LAYER_NETWORK, 9999,
859859
WINDIVERT_FLAG_DROP);
860-
lower_handle = WinDivertOpen("true", WINDIVERT_LAYER_NETWORK, 999,
860+
lower_handle = WinDivertOpen("true", WINDIVERT_LAYER_NETWORK, -9999,
861861
WINDIVERT_FLAG_DROP);
862862
if (upper_handle == INVALID_HANDLE_VALUE ||
863863
lower_handle == INVALID_HANDLE_VALUE)
@@ -1018,14 +1018,14 @@ static BOOL run_test(HANDLE inject_handle, const char *filter,
10181018
}
10191019

10201020
// (1) Open WinDivert handles:
1021-
handle[0] = WinDivertOpen(object, WINDIVERT_LAYER_NETWORK, 777, 0);
1021+
handle[0] = WinDivertOpen(object, WINDIVERT_LAYER_NETWORK, 8888, 0);
10221022
if (handle[0] == INVALID_HANDLE_VALUE)
10231023
{
10241024
fprintf(stderr, "error: failed to open WinDivert handle for filter "
10251025
"\"%s\" (err = %d)\n", filter, GetLastError());
10261026
goto failed;
10271027
}
1028-
handle[1] = WinDivertOpen("true", WINDIVERT_LAYER_NETWORK, 888, 0);
1028+
handle[1] = WinDivertOpen("true", WINDIVERT_LAYER_NETWORK, 7777, 0);
10291029
if (handle[1] == INVALID_HANDLE_VALUE)
10301030
{
10311031
fprintf(stderr, "error: failed to open WinDivert handle "
@@ -1244,7 +1244,7 @@ static DWORD monitor_worker(LPVOID arg)
12441244
PWINDIVERT_IPHDR iphdr;
12451245
UINT i;
12461246

1247-
snprintf(filter, sizeof(filter), "processId=%d and priority=777 and "
1247+
snprintf(filter, sizeof(filter), "processId=%d and priority=8888 and "
12481248
"event=OPEN", GetCurrentProcessId());
12491249
HANDLE handle = WinDivertOpen(filter, WINDIVERT_LAYER_REFLECT, 0,
12501250
WINDIVERT_FLAG_SNIFF | WINDIVERT_FLAG_RECV_ONLY);

0 commit comments

Comments
 (0)