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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014 Microsoft Corporation. All Rights Reserved.
//
// Module Name:
// HelperFunctions_NetBuffer.cpp
//
// Abstract:
// This module contains kernel helper functions that assist with NET_BUFFER and NET_BUFFER_LIST.
//
// Naming Convention:
//
// <Module><Object><Action><Modifier>
//
// i.e.
//
// KrnlHlprNBLGetRequiredRefCount
//
// <Module>
// KrnlHlpr - Function is located in syslib\ and applies to kernel mode.
// <Object>
// NBL - Function pertains to NET_BUFFER_LIST objects.
// <Action>
// {
// Get - Function retrieves data.
// }
// <Modifier>
// {
// RequiredRefCount - Function returns a refCount for the NBL / NBL chain.
// }
//
// Private Functions:
//
// Public Functions:
// KrnlHlprNBLGetRequiredRefCount(),
//
// Author:
// Dusty Harper (DHarper)
//
// Revision History:
//
// [ Month ][Day] [Year] - [Revision]-[ Comments ]
// May 01, 2010 - 1.0 - Creation
// December 13, 2013 - 1.1 - Add KrnlHlprNBLCreateFromBuffer,
// KrnlHlprNBLCopyToBuffer,
// KrnlHlprNBLDestroyNew,
// KrnlHlprNBLCreateNew
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "HelperFunctions_Include.h" /// .
#include "HelperFunctions_NetBuffer.tmh" /// $(OBJ_PATH)\$(O)\
/**
@kernel_helper_function="KrnlHlprNBLCreateFromBuffer"
Purpose: Creates a new NBL from the data contained in the supplied buffer. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
_Success_(return != 0)
NET_BUFFER_LIST* KrnlHlprNBLCreateFromBuffer(_In_ NDIS_HANDLE nblPoolHandle,
_In_reads_(bufferSize) BYTE* pBuffer,
_In_ UINT32 bufferSize,
_Outptr_opt_result_maybenull_ PMDL* ppMDL)
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> KrnlHlprNBLCreateFromBuffer()\n");
#endif /// DBG
NTSTATUS status = STATUS_SUCCESS;
NET_BUFFER_LIST* pNBL = 0;
PMDL pMDL = 0;
pMDL = IoAllocateMdl(pBuffer,
bufferSize,
FALSE,
FALSE,
0);
if(!pMDL)
{
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_ERROR_LEVEL,
" !!!! KrnlHlprNBLCreateFromBuffer : IoAllocateMdl() [pMDL: %#p]\n",
pMDL);
HLPR_BAIL;
}
MmBuildMdlForNonPagedPool(pMDL);
status = FwpsAllocateNetBufferAndNetBufferList(nblPoolHandle,
0,
0,
pMDL,
0,
bufferSize,
&pNBL);
if(status != STATUS_SUCCESS)
{
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_ERROR_LEVEL,
" !!!! KrnlHlprNBLCreateFromBuffer : FwpsAllocateNetBufferAndNetBufferList() [status: %#x]\n",
status);
HLPR_BAIL;
}
HLPR_BAIL_LABEL:
if(status != STATUS_SUCCESS)
{
IoFreeMdl(pMDL);
pMDL = 0;
}
if(ppMDL)
*ppMDL = pMDL;
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- KrnlHlprNBLCreateFromBuffer() [pNBL: %#p]\n",
pNBL);
#endif /// DBG
return pNBL;
}
/**
@kernel_helper_function="KrnlHlprNBLCopyToBuffer"
Purpose: Copies the NBL to a buffer. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
_Success_(return != 0)
BYTE* KrnlHlprNBLCopyToBuffer(_In_opt_ NET_BUFFER_LIST* pTemplateNBL,
_Out_ UINT32* pSize,
_In_ UINT32 additionalSpace) /* 0 */
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> KrnlHlprNBLCopyToBuffer()\n");
#endif /// DBG
NTSTATUS status = STATUS_SUCCESS;
BYTE* pBuffer = 0;
UINT32 numBytes = additionalSpace;
*pSize = 0;
if(pTemplateNBL)
{
for(NET_BUFFER* pNB = NET_BUFFER_LIST_FIRST_NB(pTemplateNBL);
pNB;
pNB = NET_BUFFER_NEXT_NB(pNB))
{
numBytes += NET_BUFFER_DATA_LENGTH(pNB);
}
}
if(numBytes)
{
HLPR_NEW_ARRAY(pBuffer,
BYTE,
numBytes,
WFPSAMPLER_SYSLIB_TAG);
HLPR_BAIL_ON_ALLOC_FAILURE(pBuffer,
status);
if(pTemplateNBL)
{
NET_BUFFER* pNB = NET_BUFFER_LIST_FIRST_NB(pTemplateNBL);
for(UINT32 bytesCopied = 0;
bytesCopied < numBytes &&
pNB;
pNB = NET_BUFFER_NEXT_NB(pNB))
{
BYTE* pContiguousBuffer = 0;
BYTE* pAllocatedBuffer = 0;
UINT32 bytesNeeded = NET_BUFFER_DATA_LENGTH(pNB);
if(bytesNeeded)
{
HLPR_NEW_ARRAY(pAllocatedBuffer,
BYTE,
bytesNeeded,
WFPSAMPLER_SYSLIB_TAG);
HLPR_BAIL_ON_ALLOC_FAILURE(pAllocatedBuffer,
status);
pContiguousBuffer = (BYTE*)NdisGetDataBuffer(pNB,
bytesNeeded,
pAllocatedBuffer,
1,
0);
RtlCopyMemory(&(pBuffer[bytesCopied]),
pContiguousBuffer ? pContiguousBuffer : pAllocatedBuffer,
bytesNeeded);
bytesCopied += bytesNeeded;
HLPR_DELETE_ARRAY(pAllocatedBuffer,
WFPSAMPLER_SYSLIB_TAG);
}
}
}
HLPR_BAIL_LABEL:
if(status != STATUS_SUCCESS)
{
HLPR_DELETE_ARRAY(pBuffer,
WFPSAMPLER_SYSLIB_TAG);
}
else
*pSize = numBytes;
}
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- KrnlHlprNBLCopyToBuffer() [pBuffer: %#p]\n",
pBuffer);
#endif /// DBG
return pBuffer;
}
/**
@kernel_helper_function="KrnlHlprNBLDestroyNew"
Purpose: Destroys a new NBL. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_Success_(*ppNetBufferList == 0 && *ppMDL == 0 && *ppAllocatedBuffer == 0)
VOID KrnlHlprNBLDestroyNew(_Inout_opt_ NET_BUFFER_LIST** ppNetBufferList,
_Inout_opt_ PMDL* ppMDL,
_Inout_opt_ BYTE** ppAllocatedBuffer)
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> KrnlHlprNBLDestroyNew()\n");
#endif /// DBG
if(ppNetBufferList)
{
FwpsFreeNetBufferList(*ppNetBufferList);
*ppNetBufferList = 0;
}
if(ppMDL)
{
IoFreeMdl(*ppMDL);
*ppMDL = 0;
}
if(ppAllocatedBuffer)
{
HLPR_DELETE_ARRAY(*ppAllocatedBuffer,
WFPSAMPLER_SYSLIB_TAG);
}
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- KrnlHlprNBLDestroyNew()\n");
#endif /// DBG
return;
}
/**
@kernel_helper_function="KrnlHlprNBLCreateNew"
Purpose: Creates a new NBL. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
_Success_(return != 0)
NET_BUFFER_LIST* KrnlHlprNBLCreateNew(_In_ NDIS_HANDLE nblPoolHandle,
_In_opt_ NET_BUFFER_LIST* pTemplateNBL,
_Outptr_opt_result_buffer_maybenull_(*pSize) BYTE** ppAllocatedBuffer,
_Out_ UINT32* pSize,
_Outptr_opt_result_maybenull_ PMDL* ppMDL,
_In_ UINT32 additionalSpace, /* 0 */
_In_ BOOLEAN isOutbound) /* FALSE */
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> KrnlHlprNBLCreateNew()\n");
#endif /// DBG
NET_BUFFER_LIST* pNBL = 0;
UINT32 size = 0;
BYTE* pBuffer = KrnlHlprNBLCopyToBuffer(pTemplateNBL,
&size,
additionalSpace);
if(pBuffer &&
size)
{
pNBL = KrnlHlprNBLCreateFromBuffer(nblPoolHandle,
pBuffer,
size,
ppMDL);
if(pNBL &&
pTemplateNBL)
{
if(isOutbound)
NdisCopySendNetBufferListInfo(pNBL,
pTemplateNBL);
else
NdisCopyReceiveNetBufferListInfo(pNBL,
pTemplateNBL);
}
else
{
HLPR_DELETE_ARRAY(pBuffer,
WFPSAMPLER_SYSLIB_TAG);
size = 0;
}
}
if(ppAllocatedBuffer)
*ppAllocatedBuffer = pBuffer;
*pSize = size;
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- KrnlHlprNBLCreateNew() [pNBL: %#p]\n",
pNBL);
#endif /// DBG
return pNBL;
}
/**
@kernel_helper_function="KrnlHlprNBLGetRequiredRefCount"
Purpose: Return a count of how many NBLs are within the NBL chain. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
UINT32 KrnlHlprNBLGetRequiredRefCount(_In_ const NET_BUFFER_LIST* pNBL,
_In_ BOOLEAN isChained) /* FALSE */
{
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" ---> KrnlHlprNBLGetRequiredRefCount()\n");
#endif /// DBG
NT_ASSERT(pNBL);
UINT32 requiredRefCount = 0;
if(isChained)
{
for(NET_BUFFER_LIST* pCurrentNBL = (NET_BUFFER_LIST*)pNBL;
pCurrentNBL;
pCurrentNBL = NET_BUFFER_LIST_NEXT_NBL(pCurrentNBL))
{
requiredRefCount++;
}
}
else
requiredRefCount = 1;
#if DBG
DbgPrintEx(DPFLTR_IHVNETWORK_ID,
DPFLTR_INFO_LEVEL,
" <--- KrnlHlprNBLGetRequiredRefCount() [refCount: %#d]\n",
requiredRefCount);
#endif /// DBG
return requiredRefCount;
}
|