Sample Code

Windows Driver Samples/ Windows Filtering Platform Sample/ C++/ sys/ ClassifyFunctions_FastStreamInjectionCallouts.cpp/

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
////////////////////////////////////////////////////////////////////////////////////////////////////
//
//   Copyright (c) 2012 Microsoft Corporation.  All Rights Reserved.
//
//   Module Name:
//      ClassifyFunctions_FastStreamInjectionCallouts.cpp
//
//   Abstract:
//      This module contains WFP Classify functions that inject data back into the stream using
//         the clone / block / inject method.  This method is inline only, no modification,
//         and uses as little validation and error checking as possible. These functions are meant
//         for test performance purposes only.
//
//   Naming Convention:
//
//      <Module><Scenario>
// 
//      i.e.
//       ClassifyFastStreamInjection
//
//       <Module>
//          Classify             - Function is an FWPS_CALLOUT_CLASSIFY_FN
//       <Scenario>
//          FastStreamInjection  - Function demonstrates the clone / block / inject model in the
//                                    fastest form available (inline, no validation, etc.).
//
//   Private Functions:
//
//   Public Functions:
//      ClassifyFastStreamInjection(),
//
//   Author:
//      Dusty Harper      (DHarper)
//
//   Revision History:
//
//      [ Month ][Day] [Year] - [Revision]-[ Comments ]
//      May       01,   2010  -     1.0   -  Creation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
 
#include "Framework_WFPSamplerCalloutDriver.h"               /// .
#include "ClassifyFunctions_FastStreamInjectionCallouts.tmh" /// $(OBJ_PATH)\$(O)\
 
#if(NTDDI_VERSION >= NTDDI_WIN7)
 
/**
 @classify_function="ClassifyFastStreamInjection"
  
   Purpose:  Blocks the current stream data and injects a clone back to the stack without
             modification.                                                                      <br>
                                                                                                <br>
   Notes:    Applies to the following layers:                                                   <br>
                FWPS_LAYER_STREAM_V{4/6}                                                        <br>
                                                                                                <br>
             Intended for test performance purposes only.                                       <br>
                                                                                                <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
VOID ClassifyFastStreamInjection(_In_ const FWPS_INCOMING_VALUES* pClassifyValues,
                                 _In_ const FWPS_INCOMING_METADATA_VALUES* pMetadata,
                                 _Inout_opt_ VOID* pStreamCalloutIOPacket,
                                 _In_opt_ const VOID* pClassifyContext,
                                 _In_ const FWPS_FILTER* pFilter,
                                 _In_ UINT64 flowContext,
                                 _Inout_ FWPS_CLASSIFY_OUT* pClassifyOut)
{
   UNREFERENCED_PARAMETER(pClassifyContext);
   UNREFERENCED_PARAMETER(flowContext);
 
   if(pStreamCalloutIOPacket &&
      pClassifyOut->rights & FWPS_RIGHT_ACTION_WRITE)
   {
      NTSTATUS                       status              = STATUS_SUCCESS;
      FWPS_STREAM_CALLOUT_IO_PACKET* pStreamPacket       = (FWPS_STREAM_CALLOUT_IO_PACKET*)pStreamCalloutIOPacket;
      FWP_DIRECTION                  direction           = (FWP_DIRECTION)pClassifyValues->incomingValue[FWPS_FIELD_STREAM_V4_DIRECTION].value.uint32;
      HANDLE                         injectionHandle     = 0;
      UINT64                         flowID              = 0;
      UINT32                         streamFlags         = pStreamPacket->streamData->flags;
      NET_BUFFER_LIST*               pNetBufferListChain = 0;
 
      if(FWPS_IS_METADATA_FIELD_PRESENT(pMetadata,
                                        FWPS_METADATA_FIELD_FLOW_HANDLE))
         flowID = pMetadata->flowHandle;
 
      if(pClassifyValues->layerId == FWPS_LAYER_STREAM_V4)
      {
         if(direction == FWP_DIRECTION_OUTBOUND)
            injectionHandle = g_IPv4OutboundStreamInjectionHandle;
         else
            injectionHandle = g_IPv4InboundStreamInjectionHandle;
      }
      else if(pClassifyValues->layerId == FWPS_LAYER_STREAM_V6)
      {
         if(direction == FWP_DIRECTION_OUTBOUND)
            injectionHandle = g_IPv6OutboundStreamInjectionHandle;
         else
            injectionHandle = g_IPv6InboundStreamInjectionHandle;
      }
      else
         HLPR_BAIL;
 
      if(!(streamFlags & FWPS_STREAM_FLAG_RECEIVE) &&
         !(streamFlags & FWPS_STREAM_FLAG_SEND))
         streamFlags |= direction ? FWPS_STREAM_FLAG_RECEIVE : FWPS_STREAM_FLAG_SEND;
 
      pStreamPacket->countBytesRequired = 0;
      pStreamPacket->countBytesEnforced = pStreamPacket->streamData->dataLength;
      pStreamPacket->streamAction       = FWPS_STREAM_ACTION_NONE;
 
      status = FwpsCloneStreamData(pStreamPacket->streamData,
                                   g_pNDISPoolData->nblPoolHandle,
                                   g_pNDISPoolData->nbPoolHandle,
                                   0,
                                   &pNetBufferListChain);
      if(status != STATUS_SUCCESS)
      {
         DbgPrintEx(DPFLTR_IHVNETWORK_ID,
                    DPFLTR_ERROR_LEVEL,
                    " !!!! PerformBasicStreamInjection : FwpsCloneStreamData() [status: %#x]\n",
                    status);
 
         HLPR_BAIL;
      }
 
      pClassifyOut->actionType  = FWP_ACTION_BLOCK;
      pClassifyOut->rights     ^= FWPS_RIGHT_ACTION_WRITE;
 
      status = FwpsStreamInjectAsync(injectionHandle,
                                     0,
                                     0,
                                     flowID,
                                     pFilter->action.calloutId,
                                     pClassifyValues->layerId,
                                     streamFlags,
                                     pNetBufferListChain,
                                     pStreamPacket->streamData->dataLength,
                                     CompleteFastStreamInjection,
                                     0);
 
      HLPR_BAIL_LABEL:
 
      if(status != STATUS_SUCCESS &&
         pNetBufferListChain)
      {
         KIRQL irql = KeGetCurrentIrql();
 
         FwpsDiscardClonedStreamData(pNetBufferListChain,
                                     0,
                                     irql == DISPATCH_LEVEL ? TRUE : FALSE);
      }
   }
 
   return;
}
 
#else
 
/**
 @classify_function="ClassifyFastStreamInjection"
  
   Purpose:  Blocks the current stream data and injects a clone back to the stack without
             modification.                                                                      <br>
                                                                                                <br>
   Notes:    Applies to the following layers:                                                   <br>
                FWPS_LAYER_STREAM_V{4/6}                                                        <br>
                                                                                                <br>
             Intended for test performance purposes only.                                       <br>
                                                                                                <br>
*/
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_same_
VOID ClassifyFastStreamInjection(_In_ const FWPS_INCOMING_VALUES* pClassifyValues,
                                 _In_ const FWPS_INCOMING_METADATA_VALUES0* pMetadata,
                                 _Inout_opt_ VOID* pStreamCalloutIOPacket,
                                 _In_ const FWPS_FILTER* pFilter,
                                 _In_ UINT64 flowContext,
                                 _Inout_ FWPS_CLASSIFY_OUT* pClassifyOut)
{
   UNREFERENCED_PARAMETER(flowContext);
 
   if(pStreamCalloutIOPacket &&
      pClassifyOut->rights & FWPS_RIGHT_ACTION_WRITE)
   {
      NTSTATUS                       status              = STATUS_SUCCESS;
      FWPS_STREAM_CALLOUT_IO_PACKET* pStreamPacket       = (FWPS_STREAM_CALLOUT_IO_PACKET*)pStreamCalloutIOPacket;
      FWP_DIRECTION                  direction           = (FWP_DIRECTION)pClassifyValues->incomingValue[FWPS_FIELD_STREAM_V4_DIRECTION].value.uint32;
      HANDLE                         injectionHandle     = 0;
      UINT64                         flowID              = 0;
      UINT32                         streamFlags         = pStreamPacket->streamData->flags;
      NET_BUFFER_LIST*               pNetBufferListChain = 0;
    
      if(FWPS_IS_METADATA_FIELD_PRESENT(pMetadata,
                                        FWPS_METADATA_FIELD_FLOW_HANDLE))
         flowID = pMetadata->flowHandle;
    
      if(pClassifyValues->layerId == FWPS_LAYER_STREAM_V4)
      {
         if(direction == FWP_DIRECTION_OUTBOUND)
            injectionHandle = g_IPv4OutboundStreamInjectionHandle;
         else
            injectionHandle = g_IPv4InboundStreamInjectionHandle;
      }
      else if(pClassifyValues->layerId == FWPS_LAYER_STREAM_V6)
      {
         if(direction == FWP_DIRECTION_OUTBOUND)
            injectionHandle = g_IPv6OutboundStreamInjectionHandle;
         else
            injectionHandle = g_IPv6InboundStreamInjectionHandle;
      }
      else
         HLPR_BAIL;
    
      if(!(streamFlags & FWPS_STREAM_FLAG_RECEIVE) &&
         !(streamFlags & FWPS_STREAM_FLAG_SEND))
         streamFlags |= direction ? FWPS_STREAM_FLAG_RECEIVE : FWPS_STREAM_FLAG_SEND;
    
      pStreamPacket->countBytesRequired = 0;
      pStreamPacket->countBytesEnforced = pStreamPacket->streamData->dataLength;
      pStreamPacket->streamAction       = FWPS_STREAM_ACTION_NONE;
    
      status = FwpsCloneStreamData(pStreamPacket->streamData,
                                   g_pNDISPoolData->nblPoolHandle,
                                   g_pNDISPoolData->nbPoolHandle,
                                   0,
                                   &pNetBufferListChain);
      if(status != STATUS_SUCCESS)
      {
         DbgPrintEx(DPFLTR_IHVNETWORK_ID,
                    DPFLTR_ERROR_LEVEL,
                    " !!!! PerformBasicStreamInjection : FwpsCloneStreamData() [status: %#x]\n",
                    status);
    
         HLPR_BAIL;
      }
 
      pClassifyOut->actionType  = FWP_ACTION_BLOCK;
      pClassifyOut->rights     ^= FWPS_RIGHT_ACTION_WRITE;
 
      status = FwpsStreamInjectAsync(injectionHandle,
                                     0,
                                     0,
                                     flowID,
                                     pFilter->action.calloutId,
                                     pClassifyValues->layerId,
                                     streamFlags,
                                     pNetBufferListChain,
                                     pStreamPacket->streamData->dataLength,
                                     CompleteFastStreamInjection,
                                     0);
    
      HLPR_BAIL_LABEL:
    
      if(status != STATUS_SUCCESS &&
         pNetBufferListChain)
      {
         KIRQL irql = KeGetCurrentIrql();
    
         FwpsDiscardClonedStreamData(pNetBufferListChain,
                                     0,
                                     irql == DISPATCH_LEVEL ? TRUE : FALSE);
      }
   }
    
   return;
}
 
#endif // (NTDDI_VERSION >= NTDDI_WIN7)

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS