1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014 Microsoft Corporation. All Rights Reserved.
//
// Module Name:
// CompletionFunctions_AdvancedPacketInjectionCallouts.cpp
//
// Abstract:
// This module contains WFP Completion functions for injecting packets back into the data path
// using the allocate / block / inject method.
//
// Naming Convention:
//
// <Module><Scenario>
//
// i.e.
// CompleteAdvancedPacketInjection
//
// <Module>
// Complete - Function is an FWPS_INJECT_COMPLETE function.
// <Scenario>
// AdvancedPacketInjection - Function demonstrates the allocate / block /
// inject model.
//
// <Object><Action>
//
// i.e.
// AdvancedPacketInjectionCompletionDataDestroy
//
// <Object>
// {
// AdvancedPacketInjectionCompletionData - pertains to
// ADVANCED_PACKET_INJECTION_COMPLETION_DATA.
// }
// <Action>
// Destroy - Cleans up and frees all memory in the object.
//
// Private Functions:
// AdvancedPacketInjectionCompletionDataDestroy(),
//
// Public Functions:
// CompleteAdvancedPacketInjection(),
//
// Author:
// Dusty Harper (DHarper)
//
// Revision History:
//
// [ Month ][Day] [Year] - [Revision]-[ Comments ]
// December 13, 2013 - 1.1 - Creation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Framework_WFPSamplerCalloutDriver.h" /// .
#include "CompletionFunctions_AdvancedPacketInjectionCallouts.tmh" /// $(OBJ_PATH)\$(O)\
#if DBG
INJECTION_COUNTERS g_apiTotalCompletions = {0};
/**
@function="AdvancedPacketInjectionCountersDecrement"
Purpose: Decrement the appropriate counters based on the injection handle. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: HTTP://MSDN.Microsoft.com/En-US/Library/Desktop/MS683581.aspx <br>
*/
VOID AdvancedPacketInjectionCountersDecrement(_In_ HANDLE injectionHandle,
_Inout_ INJECTION_COUNTERS* pCounters)
{
if(injectionHandle == g_pIPv4InboundNetworkInjectionHandles[0] ||
injectionHandle == g_pIPv4InboundNetworkInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundNetwork_IPv4));
else if(injectionHandle == g_pIPv6InboundNetworkInjectionHandles[0] ||
injectionHandle == g_pIPv6InboundNetworkInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundNetwork_IPv6));
else if(injectionHandle == g_pIPv4OutboundNetworkInjectionHandles[0] ||
injectionHandle == g_pIPv4OutboundNetworkInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundNetwork_IPv4));
else if(injectionHandle == g_pIPv6OutboundNetworkInjectionHandles[0] ||
injectionHandle == g_pIPv6OutboundNetworkInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundNetwork_IPv6));
else if(injectionHandle == g_pIPv4InboundForwardInjectionHandles[0] ||
injectionHandle == g_pIPv4InboundForwardInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundForward_IPv4));
else if(injectionHandle == g_pIPv6InboundForwardInjectionHandles[0] ||
injectionHandle == g_pIPv6InboundForwardInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundForward_IPv6));
else if(injectionHandle == g_pIPv4OutboundForwardInjectionHandles[0] ||
injectionHandle == g_pIPv4OutboundForwardInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundForward_IPv4));
else if(injectionHandle == g_pIPv6OutboundForwardInjectionHandles[0] ||
injectionHandle == g_pIPv6OutboundForwardInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundForward_IPv6));
else if(injectionHandle == g_pIPv4InboundTransportInjectionHandles[0] ||
injectionHandle == g_pIPv4InboundTransportInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundTransport_IPv4));
else if(injectionHandle == g_pIPv6InboundTransportInjectionHandles[0] ||
injectionHandle == g_pIPv6InboundTransportInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundTransport_IPv6));
else if(injectionHandle == g_pIPv4OutboundTransportInjectionHandles[0] ||
injectionHandle == g_pIPv4OutboundTransportInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundTransport_IPv4));
else if(injectionHandle == g_pIPv6OutboundTransportInjectionHandles[0] ||
injectionHandle == g_pIPv6OutboundTransportInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundTransport_IPv6));
#if(NTDDI_VERSION >= NTDDI_WIN8)
else if(injectionHandle == g_pIPv4InboundMACInjectionHandles[0] ||
injectionHandle == g_pIPv4InboundMACInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundMAC_IPv4));
else if(injectionHandle == g_pIPv6InboundMACInjectionHandles[0] ||
injectionHandle == g_pIPv6InboundMACInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundMAC_IPv6));
else if(injectionHandle == g_pInboundMACInjectionHandles[0] ||
injectionHandle == g_pInboundMACInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->inboundMAC_Unknown));
else if(injectionHandle == g_pIPv4OutboundMACInjectionHandles[0] ||
injectionHandle == g_pIPv4OutboundMACInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundMAC_IPv4));
else if(injectionHandle == g_pIPv6OutboundMACInjectionHandles[0] ||
injectionHandle == g_pIPv6OutboundMACInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundMAC_IPv6));
else if(injectionHandle == g_pOutboundMACInjectionHandles[0] ||
injectionHandle == g_pOutboundMACInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->outboundMAC_Unknown));
else if(injectionHandle == g_pIPv4IngressVSwitchEthernetInjectionHandles[0] ||
injectionHandle == g_pIPv4IngressVSwitchEthernetInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->ingressVSwitch_IPv4));
else if(injectionHandle == g_pIPv6IngressVSwitchEthernetInjectionHandles[0] ||
injectionHandle == g_pIPv6IngressVSwitchEthernetInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->ingressVSwitch_IPv6));
else if(injectionHandle == g_pIngressVSwitchEthernetInjectionHandles[0] ||
injectionHandle == g_pIngressVSwitchEthernetInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->ingressVSwitch_Unknown));
else if(injectionHandle == g_pIPv4EgressVSwitchEthernetInjectionHandles[0] ||
injectionHandle == g_pIPv4EgressVSwitchEthernetInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->egressVSwitch_IPv4));
else if(injectionHandle == g_pIPv6EgressVSwitchEthernetInjectionHandles[0] ||
injectionHandle == g_pIPv6EgressVSwitchEthernetInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->egressVSwitch_IPv6));
else if(injectionHandle == g_pEgressVSwitchEthernetInjectionHandles[0] ||
injectionHandle == g_pEgressVSwitchEthernetInjectionHandles[1])
InterlockedDecrement64((LONG64*)&(pCounters->egressVSwitch_Unknown));
#endif /// (NTDDI_VERSION >= NTDDI_WIN8)
return;
}
#endif /// DBG
/**
@private_function="AdvancedPacketInjectionCompletionDataDestroy"
Purpose: <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_At_(*ppCompletionData, _Pre_ _Notnull_)
_At_(*ppCompletionData, _Post_ _Null_ __drv_freesMem(Pool))
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
_Success_(*ppCompletionData == 0)
VOID AdvancedPacketInjectionCompletionDataDestroy(_Inout_ ADVANCED_PACKET_INJECTION_COMPLETION_DATA** ppCompletionData,
_In_ BOOLEAN override) /* FALSE */
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> AdvancedPacketInjectionCompletionDataDestroy()\n");
#endif /// DBG
NT_ASSERT(ppCompletionData);
NT_ASSERT(*ppCompletionData);
ADVANCED_PACKET_INJECTION_COMPLETION_DATA* pCompletionData = *ppCompletionData;
KIRQL originalIRQL = PASSIVE_LEVEL;
INT32 refCount = -1;
refCount = InterlockedDecrement((LONG*)&(pCompletionData->refCount));
if(refCount == 0 ||
override)
{
KeAcquireSpinLock(&(pCompletionData->spinLock),
&originalIRQL);
pCompletionData->refCount = -1;
if(pCompletionData->pClassifyData)
{
if(!(pCompletionData->performedInline))
KrnlHlprClassifyDataDestroyLocalCopy(&(pCompletionData->pClassifyData));
else
{
HLPR_DELETE(pCompletionData->pClassifyData,
WFPSAMPLER_CALLOUT_DRIVER_TAG);
}
}
if(pCompletionData->pInjectionData)
KrnlHlprInjectionDataDestroy(&(pCompletionData->pInjectionData));
if(pCompletionData->pSendParams)
{
HLPR_DELETE_ARRAY(pCompletionData->pSendParams->remoteAddress,
WFPSAMPLER_CALLOUT_DRIVER_TAG);
HLPR_DELETE(pCompletionData->pSendParams,
WFPSAMPLER_CALLOUT_DRIVER_TAG);
}
KrnlHlprNBLDestroyNew(0,
&(pCompletionData->pAllocatedMDL),
&(pCompletionData->pAllocatedBuffer));
KeReleaseSpinLock(&(pCompletionData->spinLock),
originalIRQL);
HLPR_DELETE(*ppCompletionData,
WFPSAMPLER_CALLOUT_DRIVER_TAG);
}
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- AdvancedPacketInjectionCompletionDataDestroy()\n");
#endif /// DBG
return;
}
/**
@completion_function="CompleteAdvancedPacketInjection"
Purpose: Cleanup injection objects and memory. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: HTTP://MSDN.Microsoft.com/En-Us/Library/Windows/Hardware/FF545018.aspx <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
VOID NTAPI CompleteAdvancedPacketInjection(_In_ VOID* pContext,
_Inout_ NET_BUFFER_LIST* pNetBufferList,
_In_ BOOLEAN dispatchLevel)
{
NT_ASSERT(pContext);
NT_ASSERT(((ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext)->pInjectionData);
NT_ASSERT(((ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext)->pClassifyData);
NT_ASSERT(((ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext)->pClassifyData->pClassifyValues);
NT_ASSERT(((ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext)->pClassifyData->pClassifyOut);
NT_ASSERT(((ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext)->pClassifyData->pFilter);
NT_ASSERT(((ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext)->pAllocatedMDL);
NT_ASSERT(((ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext)->pAllocatedBuffer);
NT_ASSERT(pNetBufferList);
NT_ASSERT(NT_SUCCESS(pNetBufferList->Status));
UNREFERENCED_PARAMETER(dispatchLevel);
NTSTATUS status = pNetBufferList->Status;
ADVANCED_PACKET_INJECTION_COMPLETION_DATA* pCompletionData = (ADVANCED_PACKET_INJECTION_COMPLETION_DATA*)pContext;
UINT32 layerID = pCompletionData->pClassifyData->pClassifyValues->layerId;
UINT64 filterID = pCompletionData->pClassifyData->pFilter->filterId;
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> CompleteAdvancedPacketInjection() [Layer: %s][FilterID: %#I64x][NBL->Status: %#x]",
KrnlHlprFwpsLayerIDToString(layerID),
filterID,
status);
if(status != STATUS_SUCCESS)
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_ERROR_LEVEL,
" !!!! CompleteAdvancedPacketInjection() [status: %#x]\n",
status);
FwpsFreeNetBufferList(pNetBufferList);
#if DBG
AdvancedPacketInjectionCountersIncrement(pCompletionData->pInjectionData->injectionHandle,
&g_apiTotalCompletions);
AdvancedPacketInjectionCountersDecrement(pCompletionData->pInjectionData->injectionHandle,
&g_apiOutstandingNewNBLs);
#endif // DBG
AdvancedPacketInjectionCompletionDataDestroy(&pCompletionData);
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- CompleteAdvancedPacketInjection() [Layer: %s][FilterID: %#I64x][NBL->Status: %#x]",
KrnlHlprFwpsLayerIDToString(layerID),
filterID,
status);
return;
}
|