@@ -294,8 +294,12 @@ typedef struct flow_s *flow_t;
294
294
/*
295
295
* Global state.
296
296
*/
297
- static HANDLE inject_handle = NULL ;
298
- static HANDLE injectv6_handle = NULL ;
297
+ static HANDLE inject_handle_forward = NULL ;
298
+ static HANDLE injectv6_handle_forward = NULL ;
299
+ static HANDLE inject_handle_in = NULL ;
300
+ static HANDLE inject_handle_out = NULL ;
301
+ static HANDLE injectv6_handle_in = NULL ;
302
+ static HANDLE injectv6_handle_out = NULL ;
299
303
static NDIS_HANDLE nbl_pool_handle = NULL ;
300
304
static NDIS_HANDLE nb_pool_handle = NULL ;
301
305
static HANDLE engine_handle = NULL ;
@@ -1050,20 +1054,55 @@ extern NTSTATUS DriverEntry(IN PDRIVER_OBJECT driver_obj,
1050
1054
WdfControlFinishInitializing (device );
1051
1055
1052
1056
// Create the packet injection handles.
1053
- status = FwpsInjectionHandleCreate0 (AF_INET ,
1057
+ status = FwpsInjectionHandleCreate0 (AF_INET ,
1054
1058
FWPS_INJECTION_TYPE_NETWORK | FWPS_INJECTION_TYPE_FORWARD ,
1055
- & inject_handle );
1059
+ & inject_handle_forward );
1056
1060
if (!NT_SUCCESS (status ))
1057
1061
{
1058
- DEBUG_ERROR ("failed to create WFP packet injection handle" , status );
1062
+ DEBUG_ERROR ("failed to create WFP forward packet injection handle" , status );
1059
1063
goto driver_entry_exit ;
1060
1064
}
1061
- status = FwpsInjectionHandleCreate0 (AF_INET6 ,
1065
+ status = FwpsInjectionHandleCreate0 (AF_INET6 ,
1062
1066
FWPS_INJECTION_TYPE_NETWORK | FWPS_INJECTION_TYPE_FORWARD ,
1063
- & injectv6_handle );
1067
+ & injectv6_handle_forward );
1064
1068
if (!NT_SUCCESS (status ))
1065
1069
{
1066
- DEBUG_ERROR ("failed to create WFP ipv6 packet injection handle" ,
1070
+ DEBUG_ERROR ("failed to create WFP ipv6 forward packet injection handle" , status );
1071
+ goto driver_entry_exit ;
1072
+ }
1073
+
1074
+ status = FwpsInjectionHandleCreate0 (AF_INET ,
1075
+ FWPS_INJECTION_TYPE_NETWORK | FWPS_INJECTION_TYPE_FORWARD ,
1076
+ & inject_handle_in );
1077
+ if (!NT_SUCCESS (status ))
1078
+ {
1079
+ DEBUG_ERROR ("failed to create WFP inbound packet injection handle" , status );
1080
+ goto driver_entry_exit ;
1081
+ }
1082
+ status = FwpsInjectionHandleCreate0 (AF_INET ,
1083
+ FWPS_INJECTION_TYPE_NETWORK | FWPS_INJECTION_TYPE_FORWARD ,
1084
+ & inject_handle_out );
1085
+ if (!NT_SUCCESS (status ))
1086
+ {
1087
+ DEBUG_ERROR ("failed to create WFP outbound packet injection handle" , status );
1088
+ goto driver_entry_exit ;
1089
+ }
1090
+
1091
+ status = FwpsInjectionHandleCreate0 (AF_INET6 ,
1092
+ FWPS_INJECTION_TYPE_NETWORK | FWPS_INJECTION_TYPE_FORWARD ,
1093
+ & injectv6_handle_in );
1094
+ if (!NT_SUCCESS (status ))
1095
+ {
1096
+ DEBUG_ERROR ("failed to create WFP ipv6 inbound packet injection handle" ,
1097
+ status );
1098
+ goto driver_entry_exit ;
1099
+ }
1100
+ status = FwpsInjectionHandleCreate0 (AF_INET6 ,
1101
+ FWPS_INJECTION_TYPE_NETWORK | FWPS_INJECTION_TYPE_FORWARD ,
1102
+ & injectv6_handle_out );
1103
+ if (!NT_SUCCESS (status ))
1104
+ {
1105
+ DEBUG_ERROR ("failed to create WFP ipv6 outbound packet injection handle" ,
1067
1106
status );
1068
1107
goto driver_entry_exit ;
1069
1108
}
@@ -1264,13 +1303,29 @@ static void windivert_driver_unload(void)
1264
1303
1265
1304
DEBUG ("UNLOAD: unloading the WinDivert driver" );
1266
1305
1267
- if (inject_handle != NULL )
1306
+ if (inject_handle_forward != NULL )
1307
+ {
1308
+ FwpsInjectionHandleDestroy0 (inject_handle_forward );
1309
+ }
1310
+ if (injectv6_handle_forward != NULL )
1311
+ {
1312
+ FwpsInjectionHandleDestroy0 (injectv6_handle_forward );
1313
+ }
1314
+ if (inject_handle_in != NULL )
1315
+ {
1316
+ FwpsInjectionHandleDestroy0 (inject_handle_in );
1317
+ }
1318
+ if (inject_handle_out != NULL )
1319
+ {
1320
+ FwpsInjectionHandleDestroy0 (inject_handle_out );
1321
+ }
1322
+ if (injectv6_handle_in != NULL )
1268
1323
{
1269
- FwpsInjectionHandleDestroy0 (inject_handle );
1324
+ FwpsInjectionHandleDestroy0 (injectv6_handle_in );
1270
1325
}
1271
- if (injectv6_handle != NULL )
1326
+ if (injectv6_handle_out != NULL )
1272
1327
{
1273
- FwpsInjectionHandleDestroy0 (injectv6_handle );
1328
+ FwpsInjectionHandleDestroy0 (injectv6_handle_out );
1274
1329
}
1275
1330
if (nbl_pool_handle != NULL )
1276
1331
{
@@ -2671,22 +2726,24 @@ static NTSTATUS windivert_write(context_t context, WDFREQUEST request,
2671
2726
}
2672
2727
2673
2728
// Inject packet:
2674
- handle = (ipv4 ? inject_handle : injectv6_handle );
2675
2729
if (layer == WINDIVERT_LAYER_NETWORK_FORWARD )
2676
2730
{
2731
+ handle = (ipv4 ? inject_handle_forward : injectv6_handle_forward );
2677
2732
status = FwpsInjectForwardAsync0 (handle , (HANDLE )priority , 0 ,
2678
2733
(ipv4 ? AF_INET : AF_INET6 ), UNSPECIFIED_COMPARTMENT_ID ,
2679
2734
addr [i ].Network .IfIdx , buffers , windivert_inject_complete ,
2680
2735
data_copy );
2681
2736
}
2682
2737
else if (addr [i ].Outbound != 0 )
2683
2738
{
2739
+ handle = (ipv4 ? inject_handle_out : injectv6_handle_out );
2684
2740
status = FwpsInjectNetworkSendAsync0 (handle , (HANDLE )priority , 0 ,
2685
2741
UNSPECIFIED_COMPARTMENT_ID , buffers , windivert_inject_complete ,
2686
2742
data_copy );
2687
2743
}
2688
2744
else
2689
2745
{
2746
+ handle = (ipv4 ? inject_handle_in : injectv6_handle_in );
2690
2747
status = FwpsInjectNetworkReceiveAsync0 (handle , (HANDLE )priority , 0 ,
2691
2748
UNSPECIFIED_COMPARTMENT_ID , addr [i ].Network .IfIdx ,
2692
2749
addr [i ].Network .SubIfIdx , buffers , windivert_inject_complete ,
@@ -3701,13 +3758,33 @@ static void windivert_network_classify(context_t context,
3701
3758
}
3702
3759
if (ipv4 )
3703
3760
{
3704
- packet_state = FwpsQueryPacketInjectionState0 (inject_handle , buffers ,
3705
- & packet_context );
3761
+ if ( context -> layer == WINDIVERT_LAYER_NETWORK_FORWARD ) {
3762
+ packet_state = FwpsQueryPacketInjectionState0 (inject_handle_forward , buffers ,
3763
+ & packet_context );
3764
+ }
3765
+ else if ( outbound ) {
3766
+ packet_state = FwpsQueryPacketInjectionState0 (inject_handle_out , buffers ,
3767
+ & packet_context );
3768
+ }
3769
+ else {
3770
+ packet_state = FwpsQueryPacketInjectionState0 (inject_handle_in , buffers ,
3771
+ & packet_context );
3772
+ }
3706
3773
}
3707
3774
else
3708
3775
{
3709
- packet_state = FwpsQueryPacketInjectionState0 (injectv6_handle ,
3710
- buffers , & packet_context );
3776
+ if ( context -> layer == WINDIVERT_LAYER_NETWORK_FORWARD ) {
3777
+ packet_state = FwpsQueryPacketInjectionState0 (injectv6_handle_forward , buffers ,
3778
+ & packet_context );
3779
+ }
3780
+ else if ( outbound ) {
3781
+ packet_state = FwpsQueryPacketInjectionState0 (injectv6_handle_out ,
3782
+ buffers , & packet_context );
3783
+ }
3784
+ else {
3785
+ packet_state = FwpsQueryPacketInjectionState0 (injectv6_handle_in ,
3786
+ buffers , & packet_context );
3787
+ }
3711
3788
}
3712
3789
3713
3790
KeAcquireInStackQueuedSpinLock (& context -> lock , & lock_handle );
@@ -5142,22 +5219,24 @@ static void windivert_reinject_packet(packet_t packet)
5142
5219
return ;
5143
5220
}
5144
5221
priority = packet -> priority ;
5145
- handle = (packet -> ipv6 ? injectv6_handle : inject_handle );
5146
5222
if (packet -> layer == WINDIVERT_LAYER_NETWORK_FORWARD )
5147
5223
{
5224
+ handle = (packet -> ipv6 ? injectv6_handle_forward : inject_handle_forward );
5148
5225
status = FwpsInjectForwardAsync0 (handle , (HANDLE )priority , 0 ,
5149
5226
(packet -> ipv6 ? AF_INET6 : AF_INET ), UNSPECIFIED_COMPARTMENT_ID ,
5150
5227
network_data -> IfIdx , buffers , windivert_reinject_complete ,
5151
5228
(HANDLE )packet );
5152
5229
}
5153
5230
else if (packet -> outbound )
5154
5231
{
5232
+ handle = (packet -> ipv6 ? injectv6_handle_out : inject_handle_out );
5155
5233
status = FwpsInjectNetworkSendAsync0 (handle , (HANDLE )priority , 0 ,
5156
5234
UNSPECIFIED_COMPARTMENT_ID , buffers , windivert_reinject_complete ,
5157
5235
(HANDLE )packet );
5158
5236
}
5159
5237
else
5160
5238
{
5239
+ handle = (packet -> ipv6 ? injectv6_handle_in : inject_handle_in );
5161
5240
status = FwpsInjectNetworkReceiveAsync0 (handle , (HANDLE )priority , 0 ,
5162
5241
UNSPECIFIED_COMPARTMENT_ID , network_data -> IfIdx ,
5163
5242
network_data -> SubIfIdx , buffers , windivert_reinject_complete ,
0 commit comments