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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014 Microsoft Corporation. All Rights Reserved.
//
// Module Name:
// ClassifyFunctions_PendEndpointClosureCallouts.cpp
//
// Abstract:
// This module contains WFP Classify functions for pending and completing endpoint closures.
//
// Naming Convention:
//
// <Module><Scenario>
//
// i.e.
//
// ClassifyPendEndpointClosure
//
// <Module>
// Classify - Function is an FWPS_CALLOUT_CLASSIFY_FN.
// Perform - Function executes the desired scenario.
// Trigger - Function queues a worker thread for later execution of
// the the scenario.
// <Scenario>
// PendEndpointClosure - Function demonstates pending endpoint closure requests.
//
// Author:
// Dusty Harper (DHarper)
//
// Private Functions:
// PendEndpointClosureWorkItemRoutine(),
// PerformPendEndpointClosure(),
// TriggerPendEndpointClosureOutOfBand(),
//
// Public Functions:
// PendEndpointClosureDeferredProcedureCall(),
// ClassifyPendEndpointClosure(),
//
// Revision History:
//
// [ Month ][Day] [Year] - [Revision]-[ Comments ]
// December 13, 2013 - 1.1 - Creation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Framework_WFPSamplerCalloutDriver.h" /// .
#include "ClassifyFunctions_PendEndpointClosureCallouts.tmh" /// $(OBJ_PATH)\$(O)\
#if(NTDDI_VERSION >= NTDDI_WIN7)
#pragma warning(push)
#pragma warning(disable: 6101) /// *ppPendData are freed and set to 0 on successful completion
/**
@private_function="PerformPendEndpointClosure"
Purpose: Waits for the specified delay, then completes the pended classify, and frees the
memory. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
_Success_(return == STATUS_SUCCESS)
NTSTATUS PerformPendEndpointClosure(_Inout_ PEND_DATA** ppPendData)
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> PerformPendEndpointClosure()\n");
#endif /// DBG
NT_ASSERT(*ppPendData);
NT_ASSERT((*ppPendData)->pPendEndpointClosureData);
NTSTATUS status = STATUS_SUCCESS;
UINT32 delay = (*ppPendData)->pPendEndpointClosureData->delay;
if(delay &&
KeGetCurrentIrql() < DISPATCH_LEVEL)
{
#pragma warning(push)
#pragma warning(disable: 28118) /// IRQL check has already been performed
KrnlHlprWorkItemSleep(delay);
#pragma warning(pop)
}
#pragma warning(push)
#pragma warning(disable: 6001) /// *ppPendData initialized prior to call to this function
/// Completes the Pend
KrnlHlprPendDataDestroy(ppPendData);
#pragma warning(pop)
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- PerformPendEndpointClosure() [status: %#x]\n",
status);
#endif /// DBG
return status;
};
#pragma warning(pop)
/**
@private_function="PendEndpointClosureDeferredProcedureCall"
Purpose: Invokes the appropriate private routine to complete the pended classify. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: HTTP://MSDN.Microsoft.com/En-US/Library/Windows/Hardware/FF542972.aspx <br>
*/
_IRQL_requires_(DISPATCH_LEVEL)
_IRQL_requires_same_
_Function_class_(KDEFERRED_ROUTINE)
VOID PendEndpointClosureDeferredProcedureCall(_In_ KDPC* pDPC,
_In_opt_ PVOID pContext,
_In_opt_ PVOID pArg1,
_In_opt_ PVOID pArg2)
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> PendEndpointClosureDeferredProcedureCall()\n");
#endif /// DBG
UNREFERENCED_PARAMETER(pDPC);
UNREFERENCED_PARAMETER(pContext);
UNREFERENCED_PARAMETER(pArg2);
NT_ASSERT(pDPC);
NT_ASSERT(pArg1);
NT_ASSERT(((DPC_DATA*)pArg1)->pPendData);
DPC_DATA* pDPCData = (DPC_DATA*)pArg1;
if(pDPCData)
{
NTSTATUS status = STATUS_SUCCESS;
status = PerformPendEndpointClosure(&(pDPCData->pPendData));
if(status != STATUS_SUCCESS)
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_ERROR_LEVEL,
" !!!! PendEndpointClosureDeferredProcedureCall() [status: %#x]\n",
status);
KrnlHlprDPCDataDestroy(&pDPCData);
}
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- PendEndpointClosureDeferredProcedureCall()\n");
#endif /// DBG
return;
}
/**
@private_function="PendEndpointClosureWorkItemRoutine"
Purpose: Invokes the appropriate private routine to complete the pended classify at
PASSIVE_LEVEL. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: HTTP://MSDN.Microsoft.com/En-US/Library/Windows/Hardware/FF566380.aspx <br>
*/
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
_Function_class_(IO_WORKITEM_ROUTINE)
VOID PendEndpointClosureWorkItemRoutine(_In_ PDEVICE_OBJECT pDeviceObject,
_In_opt_ PVOID pContext)
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> PendEndpointClosureWorkItemRoutine()\n");
#endif /// DBG
UNREFERENCED_PARAMETER(pDeviceObject);
NT_ASSERT(pContext);
NT_ASSERT(((WORKITEM_DATA*)pContext)->pPendData);
WORKITEM_DATA* pWorkItemData = (WORKITEM_DATA*)pContext;
if(pWorkItemData)
{
NTSTATUS status = STATUS_SUCCESS;
status = PerformPendEndpointClosure(&(pWorkItemData->pPendData));
if(status != STATUS_SUCCESS)
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_ERROR_LEVEL,
" !!!! PendEndpointClosureWorkItemRoutine() [status: %#x]\n",
status);
KrnlHlprWorkItemDataDestroy(&pWorkItemData);
}
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- PendEndpointClosureWorkItemRoutine()\n");
#endif /// DBG
return;
}
/**
@private_function="TriggerPendEndpointClosure"
Purpose: <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
_Success_(return == STATUS_SUCCESS)
NTSTATUS TriggerPendEndpointClosure(_Inout_ PEND_DATA* pPendData,
_In_ PC_PEND_ENDPOINT_CLOSURE_DATA* pPCData)
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> TriggerPendEndpointClosure()\n");
#endif /// DBG
NT_ASSERT(pPendData);
NT_ASSERT(pPCData);
NTSTATUS status = STATUS_SUCCESS;
if(pPCData->useWorkItems ||
pPCData->delay)
{
/// introducing the delay requires PASSIVE_LEVEL, so force use of a Work Item
status = KrnlHlprWorkItemQueue(g_pWDMDevice,
PendEndpointClosureWorkItemRoutine,
pPendData);
}
else
{
if(pPCData->useThreadedDPC)
status = KrnlHlprThreadedDPCQueue(PendEndpointClosureDeferredProcedureCall,
pPendData);
else
status = KrnlHlprDPCQueue(PendEndpointClosureDeferredProcedureCall,
pPendData);
}
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- TriggerPendEndpointClosure() [status: %#x]\n",
status);
#endif /// DBG
return status;
}
/**
@classify_function="ClassifyPendEndpointClosure"
Purpose: Classify Function which will pend an endpoint closure request. If there is
appropriate flowContext, te request will be pended, and then exit, requiring a call
to the FlowDeleteFn to perform the pend completion. Otherwise a worker thread is
queued, and a will wait for the specified milliseconds before the pend is
completed. <br>
<br>
Notes: Applies to the following layers: <br>
FWPM_LAYER_ALE_ENDPOINT_CLOSURE_V{4/6} <br>
<br>
MSDN_Ref: HTTP://MSDN.Microsoft.com/En-US/Library/Windows/Hardware/FF551229.aspx <br>
HTTP://MSDN.Microsoft.com/En-US/Library/Windows/Hardware/FF544893.aspx <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
VOID NTAPI ClassifyPendEndpointClosure(_In_ const FWPS_INCOMING_VALUES* pClassifyValues,
_In_ const FWPS_INCOMING_METADATA_VALUES* pMetadata,
_Inout_opt_ VOID* pLayerData,
_In_opt_ const VOID* pClassifyContext,
_In_ const FWPS_FILTER* pFilter,
_In_ UINT64 flowContext,
_Inout_ FWPS_CLASSIFY_OUT* pClassifyOut)
{
NT_ASSERT(pClassifyValues);
NT_ASSERT(pMetadata);
NT_ASSERT(pFilter);
NT_ASSERT(pClassifyOut);
NT_ASSERT(pClassifyValues->layerId == FWPS_LAYER_ALE_ENDPOINT_CLOSURE_V4 ||
pClassifyValues->layerId == FWPS_LAYER_ALE_ENDPOINT_CLOSURE_V6);
NT_ASSERT(pFilter->providerContext);
NT_ASSERT(pFilter->providerContext->type == FWPM_GENERAL_CONTEXT);
NT_ASSERT(pFilter->providerContext->dataBuffer);
NT_ASSERT(pFilter->providerContext->dataBuffer->size == sizeof(PC_PEND_ENDPOINT_CLOSURE_DATA));
NT_ASSERT(pFilter->providerContext->dataBuffer->data);
UNREFERENCED_PARAMETER(pLayerData);
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> ClassifyPendEndpointClosure() [Layer: %s][FilterID: %#I64x][FlowContext: %#I64x][Rights: %#x]",
KrnlHlprFwpsLayerIDToString(pClassifyValues->layerId),
pFilter->filterId,
flowContext,
pClassifyOut->rights);
NTSTATUS status = STATUS_SUCCESS;
PEND_DATA* pPendData = 0;
#pragma warning(push)
#pragma warning(disable: 6014) /// pPendData will be freed in PerformPendEndpointClosure
status = KrnlHlprPendDataCreate(&pPendData,
pClassifyValues,
pMetadata,
0,
pFilter,
(VOID*)pClassifyContext,
pClassifyOut);
HLPR_BAIL_ON_FAILURE(status);
#pragma warning(pop)
if(flowContext)
{
NT_ASSERT(flowContext);
NT_ASSERT(((FLOW_CONTEXT*)flowContext)->contextType == CONTEXT_TYPE_ALE_ENDPOINT_CLOSURE);
NT_ASSERT(((FLOW_CONTEXT*)flowContext)->pALEEndpointClosureContext);
FLOW_CONTEXT* pFlowContext = (FLOW_CONTEXT*)flowContext;
KIRQL irql = PASSIVE_LEVEL;
KeAcquireSpinLock(&(pFlowContext->pALEEndpointClosureContext->spinLock),
&irql);
/// NotifyFlowDeleteNotification will free this memory
pFlowContext->pALEEndpointClosureContext->pPendData = pPendData;
pFlowContext->pALEEndpointClosureContext->filterID = pFilter->filterId;
KeReleaseSpinLock(&(pFlowContext->pALEEndpointClosureContext->spinLock),
irql);
}
else
{
PC_PEND_ENDPOINT_CLOSURE_DATA* pPendEndpointClosureData = (PC_PEND_ENDPOINT_CLOSURE_DATA*)pFilter->providerContext->dataBuffer->data;
status = TriggerPendEndpointClosure(pPendData,
pPendEndpointClosureData);
}
HLPR_BAIL_LABEL:
if(status != STATUS_SUCCESS)
{
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_ERROR_LEVEL,
" !!!! ClassifyPendEndpointClosure() [status: %#x]\n",
status);
KrnlHlprPendDataDestroy(&pPendData);
}
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- ClassifyPendEndpointClosure() [Layer: %s][FilterID: %#I64x][Action: %#x][Rights: %#x][Absorb: %s]\n",
KrnlHlprFwpsLayerIDToString(pClassifyValues->layerId),
pFilter->filterId,
pClassifyOut->actionType,
pClassifyOut->rights,
(pClassifyOut->flags & FWPS_CLASSIFY_OUT_FLAG_ABSORB) ? "TRUE" : "FALSE");
return;
}
#endif /// (NTDDI_VERSION >= NTDDI_WIN7)
|