-
Notifications
You must be signed in to change notification settings - Fork 0
/
DD_proxy.h
251 lines (199 loc) · 5.4 KB
/
DD_proxy.h
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
/*++
Copyright (c) Microsoft Corporation. All rights reserved
Abstract:
This header files declares common data types and function prototypes used
throughout the Datagram-Data transparent proxy sample.
Environment:
Kernel mode
--*/
#ifndef _DD_PROXY_H_
#define _DD_PROXY_H_
typedef enum DD_PROXY_FLOW_TYPE_
{
DD_PROXY_FLOW_ORIGINAL,
DD_PROXY_FLOW_PROXY
} DD_PROXY_FLOW_TYPE;
//
// DD_PROXY_FLOW_CONTEXT is the object type we used to stored information
// specific flow. This callout driver maintains two kind of flow contexts --
// the original flow and the flow being proxied to.
//
typedef struct DD_PROXY_FLOW_CONTEXT_
{
LIST_ENTRY listEntry;
BOOLEAN deleted;
DD_PROXY_FLOW_TYPE flowType;
ADDRESS_FAMILY addressFamily;
#pragma warning(push)
#pragma warning(disable: 4201) //NAMELESS_STRUCT_UNION
union
{
FWP_BYTE_ARRAY16 localAddr;
UINT32 ipv4LocalAddr;
};
#pragma warning(pop)
UINT8 protocol;
UINT64 flowId;
UINT16 layerId;
UINT32 calloutId;
UINT32 ipv4NetworkOrderStorage;
//
// For DD_PROXY_FLOW_ORIGINAL type, toRemote* is the new address/port
// we are proxing to. For DD_PROXY_FLOW_PROXY type, it is the address/
// port that we will need to revert to.
//
UINT8* toRemoteAddr;
UINT16 toRemotePort;
LONG refCount;
} DD_PROXY_FLOW_CONTEXT;
//
// DD_PROXY_PENDED_PACKET is the object type we used to store all information
// needed for out-of-band packet modification and re-injection. This type
// also points back to the flow context the packet belongs to.
typedef struct DD_PROXY_PENDED_PACKET_
{
LIST_ENTRY listEntry;
DD_PROXY_FLOW_CONTEXT* belongingFlow;
FWP_DIRECTION direction;
//
// Common fields for inbound and outbound traffic.
//
NET_BUFFER_LIST* netBufferList;
COMPARTMENT_ID compartmentId;
//
// Data fields for outbound packet re-injection.
//
UINT64 endpointHandle;
#pragma warning(push)
#pragma warning(disable: 4201) //NAMELESS_STRUCT_UNION
union
{
FWP_BYTE_ARRAY16 remoteAddr;
UINT32 ipv4RemoteAddr;
};
#pragma warning(pop)
SCOPE_ID remoteScopeId;
WSACMSGHDR* controlData;
ULONG controlDataLength;
//
// Data fields for inbound packet re-injection.
//
ULONG nblOffset;
UINT32 ipHeaderSize;
UINT32 transportHeaderSize;
IF_INDEX interfaceIndex;
IF_INDEX subInterfaceIndex;
} DD_PROXY_PENDED_PACKET;
//
// Pooltags used by this callout driver.
//
#define DD_PROXY_FLOW_CONTEXT_POOL_TAG 'olfD'
#define DD_PROXY_PENDED_PACKET_POOL_TAG 'kppD'
#define DD_PROXY_CONTROL_DATA_POOL_TAG 'dcdD'
//
// Shared global data.
//
extern UINT16 configInspectDestPort;
extern UINT8* configInspectDestAddrV4;
extern UINT8* configInspectDestAddrV6;
extern UINT16 configNewDestPort;
extern UINT8* configNewDestAddrV4;
extern UINT8* configNewDestAddrV6;
extern HANDLE gInjectionHandle;
extern LIST_ENTRY gFlowList;
extern KSPIN_LOCK gFlowListLock;
extern LIST_ENTRY gPacketQueue;
extern KSPIN_LOCK gPacketQueueLock;
extern KEVENT gPacketQueueEvent;
extern UINT32 gCalloutIdV4;
extern UINT32 gCalloutIdV6;
extern BOOLEAN gDriverUnloading;
//
// Utility functions
//
__inline void
DDProxyReferenceFlowContext(
_Inout_ DD_PROXY_FLOW_CONTEXT* flowContext
)
{
NT_ASSERT(flowContext->refCount > 0);
InterlockedIncrement(&flowContext->refCount);
}
_IRQL_requires_max_(DISPATCH_LEVEL)
__inline
void
DDProxyDereferenceFlowContext(
_Inout_ DD_PROXY_FLOW_CONTEXT* flowContext
)
{
NT_ASSERT(flowContext->refCount > 0);
InterlockedDecrement(&flowContext->refCount);
if (flowContext->refCount == 0)
{
ExFreePoolWithTag(flowContext, DD_PROXY_FLOW_CONTEXT_POOL_TAG);
}
}
//
// Shared function prototypes
//
#if(NTDDI_VERSION >= NTDDI_WIN7)
void
DDProxyFlowEstablishedClassify(
_In_ const FWPS_INCOMING_VALUES* inFixedValues,
_In_ const FWPS_INCOMING_METADATA_VALUES* inMetaValues,
_Inout_opt_ void* layerData,
_In_opt_ const void* classifyContext,
_In_ const FWPS_FILTER* filter,
_In_ UINT64 flowContext,
_Inout_ FWPS_CLASSIFY_OUT* classifyOut
);
void
DDProxyClassify(
_In_ const FWPS_INCOMING_VALUES* inFixedValues,
_In_ const FWPS_INCOMING_METADATA_VALUES* inMetaValues,
_Inout_opt_ void* layerData,
_In_opt_ const void* classifyContext,
_In_ const FWPS_FILTER* filter,
_In_ UINT64 flowContext,
_Inout_ FWPS_CLASSIFY_OUT* classifyOut
);
#else
void
DDProxyFlowEstablishedClassify(
_In_ const FWPS_INCOMING_VALUES* inFixedValues,
_In_ const FWPS_INCOMING_METADATA_VALUES* inMetaValues,
_Inout_opt_ void* layerData,
_In_ const FWPS_FILTER* filter,
_In_ UINT64 flowContext,
_Inout_ FWPS_CLASSIFY_OUT* classifyOut
);
void
DDProxyClassify(
_In_ const FWPS_INCOMING_VALUES* inFixedValues,
_In_ const FWPS_INCOMING_METADATA_VALUES* inMetaValues,
_Inout_opt_ void* layerData,
_In_ const FWPS_FILTER* filter,
_In_ UINT64 flowContext,
_Inout_ FWPS_CLASSIFY_OUT* classifyOut
);
#endif /// (NTDDI_VERSION >= NTDDI_WIN7)
void
DDProxyFlowDelete(
_In_ UINT16 layerId,
_In_ UINT32 calloutId,
_In_ UINT64 flowContext
);
NTSTATUS
DDProxyFlowEstablishedNotify(
_In_ FWPS_CALLOUT_NOTIFY_TYPE notifyType,
_In_ const GUID* filterKey,
_Inout_ const FWPS_FILTER* filter
);
NTSTATUS
DDProxyNotify(
_In_ FWPS_CALLOUT_NOTIFY_TYPE notifyType,
_In_ const GUID* filterKey,
_Inout_ const FWPS_FILTER* filter
);
KSTART_ROUTINE DDProxyWorker;
#endif // _DD_PROXY_H_