summaryrefslogtreecommitdiff
path: root/network/trans/msnmntr/sys/notify.c
blob: 935cd161258999b778bbad391ea156f25b2873a0 (plain)
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
/*++

Copyright (c) Microsoft Corporation. All rights reserved

Abstract:

    Monitor Sample driver notification routines

Environment:

    Kernel mode
    
--*/

#include <ntddk.h>

#include <fwpmk.h>

#pragma warning(push)
#pragma warning(disable:4201)       // unnamed struct/union

#include <fwpsk.h>

#pragma warning(pop)


#include "ioctl.h"

#include "msnmntr.h"

#include "notify.h"

//
// Software Tracing Definitions 
//
#define WPP_CONTROL_GUIDS \
    WPP_DEFINE_CONTROL_GUID(MsnMntrNotify,(aca2f74a, 7a0d, 4f47, be4b, 66900813b8e5),  \
        WPP_DEFINE_BIT(TRACE_CLIENT_SERVER)                 \
        WPP_DEFINE_BIT(TRACE_PEER_TO_PEER)                  \
        WPP_DEFINE_BIT(TRACE_UNKNOWN)                       \
        WPP_DEFINE_BIT(TRACE_ALL_TRAFFIC) )

#include "notify.tmh" //  This file will be auto generated


#define TAG_NAME_NOTIFY 'oNnM'

NTSTATUS
MonitorNfInitialize(
   _In_ DEVICE_OBJECT* deviceObject)
{
   UNREFERENCED_PARAMETER(deviceObject);

   return STATUS_SUCCESS;
}

NTSTATUS
MonitorNfUninitialize(void)
{
   return STATUS_SUCCESS;
}

__forceinline
void*
MonitorNfpFindCharacters(
   _In_reads_bytes_(streamLength) const char* stream,
   _In_ size_t streamLength,
   _In_reads_bytes_(subStreamLength) const char* subStream,
   _In_ size_t subStreamLength,
   _Out_ size_t* bytesLeft)
{
   size_t currentOffset = 0;
   void* subStreamPtr = NULL;
   
   *bytesLeft = streamLength;

   if (subStreamLength > streamLength)
   {
      return NULL;
   }

   while (currentOffset+subStreamLength <= streamLength)
   {
      if (0 == memcmp((void*)(stream+currentOffset), subStream, subStreamLength))
      {
         subStreamPtr = (void*)(char*)(stream+currentOffset);
         *bytesLeft = streamLength;
         *bytesLeft -= currentOffset;
         *bytesLeft -= subStreamLength;
         break;
      }
      currentOffset += subStreamLength;
   }
   
   return subStreamPtr;
}

NTSTATUS
MonitorNfParseMessageInbound(
   _In_reads_bytes_(streamLength) BYTE* stream,
   _In_ size_t streamLength,
   _In_ USHORT localPort,
   _In_ USHORT remotePort)
{
   UNREFERENCED_PARAMETER(stream);

   DoTraceMessage(TRACE_CLIENT_SERVER,
               "%Id bytes received. Local Port: %d Remote Port: %d.",
               streamLength,
               localPort,
               remotePort);
   return STATUS_SUCCESS;
}

NTSTATUS
MonitorNfParseMessageInboundHttpHeader(
   _In_reads_bytes_(streamLength) BYTE* stream,
   _In_ size_t streamLength,
   _In_ USHORT localPort,
   _In_ USHORT remotePort)
{
   BYTE* msgStart = NULL;
   size_t bytesLeft;
   NTSTATUS status = STATUS_INVALID_PARAMETER;
   
   // Walk past the HTTP header.
   msgStart = (BYTE*) MonitorNfpFindCharacters((char*)stream, 
                                               streamLength,
                                               "\r\n\r\n",
                                               (ULONG)strlen("\r\n\r\n"),
                                               &bytesLeft);
   if (msgStart && (bytesLeft > 0))
   {
      size_t msgLength;

      msgStart += 4; // step past \r\n\r\n.

      msgLength = streamLength - (ULONG)(ULONG_PTR)(msgStart - stream);
      
      // Do the final inbound message processing.
      status = MonitorNfParseMessageInbound(msgStart,
                                            msgLength,
                                            localPort,
                                            remotePort);
   }

   return status;
}

NTSTATUS
MonitorNfParseMessageOutbound(
   _In_reads_bytes_(streamLength) BYTE* stream,
   _In_ size_t streamLength,
   _In_ USHORT localPort,
   _In_ USHORT remotePort)
{
   UNREFERENCED_PARAMETER(stream);

   DoTraceMessage(TRACE_CLIENT_SERVER,
               "%Id bytes sent. Local Port: %d Remote Port: %d.",
               streamLength,
               localPort,
               remotePort);
   return STATUS_SUCCESS;
}

NTSTATUS
MonitorNfParseMessageOutboundHttpHeader(
   _In_reads_bytes_(streamLength) BYTE* stream,
   _In_ size_t streamLength,
   _In_ USHORT localPort,
   _In_ USHORT remotePort)
{
   BYTE* msgStart = NULL;
   size_t bytesLeft;
   NTSTATUS status = STATUS_SUCCESS;
   
   // Walk past the HTTP header.
   msgStart = (BYTE*) MonitorNfpFindCharacters((char*)stream, 
                                               streamLength,
                                               "\r\n\r\n",
                                               (ULONG)strlen("\r\n\r\n"),
                                               &bytesLeft);
   if (msgStart && (bytesLeft > 0))
   {
      size_t msgLength;

      msgStart += 4; // step past \r\n\r\n.

      msgLength = streamLength - (ULONG)(ULONG_PTR)(msgStart - stream);
      status = MonitorNfParseMessageOutbound(msgStart,
                                             msgLength,
                                             localPort,
                                             remotePort);
   }
   
   return status;
}

NTSTATUS
MonitorNfParseStreamAndTraceMessage(
   _In_reads_bytes_(streamLength) BYTE* stream,
   _In_ size_t streamLength,
   _In_ BOOLEAN inbound,
   _In_ USHORT localPort,
   _In_ USHORT remotePort)
{
   NTSTATUS status;

   if (!inbound)
   {
      if ((_strnicmp((const char*)stream, "POST", streamLength) == 0)
          || (_strnicmp((const char*)stream, "GET", streamLength) == 0))
      {
         if ((MonitorNfParseMessageOutboundHttpHeader(stream,
                                                      streamLength,
                                                      localPort,
                                                      remotePort)) != STATUS_SUCCESS)
           return STATUS_INSUFFICIENT_RESOURCES; 
      }
      else
      {
         if ((MonitorNfParseMessageOutbound(stream,
                                            streamLength,
                                            localPort,
                                            remotePort)!= STATUS_SUCCESS))
           return STATUS_INSUFFICIENT_RESOURCES;
      }
   }
   else
   {
      if (_strnicmp((const char*)stream, "HTTP", streamLength) == 0)
      {
         if ((MonitorNfParseMessageInboundHttpHeader(stream,
                                                     streamLength,
                                                     localPort,
                                                     remotePort)) != STATUS_SUCCESS)
            return STATUS_INSUFFICIENT_RESOURCES;
      }
      else
      {
         if ((MonitorNfParseMessageInbound(stream,
                                           streamLength,
                                           localPort,
                                           remotePort)) != STATUS_SUCCESS)
            return STATUS_INSUFFICIENT_RESOURCES;
      }
   }

   {
      status = STATUS_SUCCESS;
   }

   return status;
}


NTSTATUS MonitorNfNotifyMessage(
   _In_ const FWPS_STREAM_DATA* streamBuffer,
   _In_ BOOLEAN inbound,
   _In_ USHORT localPort,
   _In_ USHORT remotePort
)
{
   NTSTATUS status = STATUS_SUCCESS;
   BYTE* stream = NULL;
   SIZE_T streamLength = streamBuffer->dataLength;
   SIZE_T bytesCopied = 0;

   if(streamLength == 0)
      return status;

   stream = ExAllocatePoolZero(NonPagedPool,
                               streamLength,
                               TAG_NAME_NOTIFY);
   if (!stream)
      return STATUS_INSUFFICIENT_RESOURCES;

   RtlZeroMemory(stream,streamLength);

   FwpsCopyStreamDataToBuffer(
      streamBuffer,
      stream,
      streamLength,
      &bytesCopied);

   NT_ASSERT(bytesCopied == streamLength);

   status = MonitorNfParseStreamAndTraceMessage(stream, streamLength, inbound, localPort, remotePort);

   ExFreePoolWithTag(stream, TAG_NAME_NOTIFY);

   return status;
}