NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
windivert.h
1/*
2 * windivert.h
3 * (C) 2019, all rights reserved,
4 *
5 * This file is part of WinDivert.
6 *
7 * WinDivert is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * WinDivert is free software; you can redistribute it and/or modify it under
21 * the terms of the GNU General Public License as published by the Free
22 * Software Foundation; either version 2 of the License, or (at your option)
23 * any later version.
24 *
25 * This program is distributed in the hope that it will be useful, but
26 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 * for more details.
29 *
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc., 51
32 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 */
34
35#ifndef __WINDIVERT_H
36#define __WINDIVERT_H
37
38#ifndef WINDIVERT_KERNEL
39#include <windows.h>
40#endif /* WINDIVERT_KERNEL */
41
42#ifndef WINDIVERTEXPORT
43#define WINDIVERTEXPORT __declspec(dllimport)
44#endif /* WINDIVERTEXPORT */
45
46#ifdef __MINGW32__
47#define __in
48#define __in_opt
49#define __out
50#define __out_opt
51#define __inout
52#define __inout_opt
53#include <stdint.h>
54#define INT8 int8_t
55#define UINT8 uint8_t
56#define INT16 int16_t
57#define UINT16 uint16_t
58#define INT32 int32_t
59#define UINT32 uint32_t
60#define INT64 int64_t
61#define UINT64 uint64_t
62#endif /* __MINGW32__ */
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68/****************************************************************************/
69/* WINDIVERT API */
70/****************************************************************************/
71
72/*
73 * WinDivert layers.
74 */
75typedef enum
76{
77 WINDIVERT_LAYER_NETWORK = 0, /* Network layer. */
78 WINDIVERT_LAYER_NETWORK_FORWARD = 1,/* Network layer (forwarded packets) */
79 WINDIVERT_LAYER_FLOW = 2, /* Flow layer. */
80 WINDIVERT_LAYER_SOCKET = 3, /* Socket layer. */
81 WINDIVERT_LAYER_REFLECT = 4, /* Reflect layer. */
82} WINDIVERT_LAYER, *PWINDIVERT_LAYER;
83
84/*
85 * WinDivert NETWORK and NETWORK_FORWARD layer data.
86 */
87typedef struct
88{
89 UINT32 IfIdx; /* Packet's interface index. */
90 UINT32 SubIfIdx; /* Packet's sub-interface index. */
91} WINDIVERT_DATA_NETWORK, *PWINDIVERT_DATA_NETWORK;
92
93/*
94 * WinDivert FLOW layer data.
95 */
96typedef struct
97{
98 UINT64 EndpointId; /* Endpoint ID. */
99 UINT64 ParentEndpointId; /* Parent endpoint ID. */
100 UINT32 ProcessId; /* Process ID. */
101 UINT32 LocalAddr[4]; /* Local address. */
102 UINT32 RemoteAddr[4]; /* Remote address. */
103 UINT16 LocalPort; /* Local port. */
104 UINT16 RemotePort; /* Remote port. */
105 UINT8 Protocol; /* Protocol. */
106} WINDIVERT_DATA_FLOW, *PWINDIVERT_DATA_FLOW;
107
108/*
109 * WinDivert SOCKET layer data.
110 */
111typedef struct
112{
113 UINT64 EndpointId; /* Endpoint ID. */
114 UINT64 ParentEndpointId; /* Parent Endpoint ID. */
115 UINT32 ProcessId; /* Process ID. */
116 UINT32 LocalAddr[4]; /* Local address. */
117 UINT32 RemoteAddr[4]; /* Remote address. */
118 UINT16 LocalPort; /* Local port. */
119 UINT16 RemotePort; /* Remote port. */
120 UINT8 Protocol; /* Protocol. */
121} WINDIVERT_DATA_SOCKET, *PWINDIVERT_DATA_SOCKET;
122
123/*
124 * WinDivert REFLECTION layer data.
125 */
126typedef struct
127{
128 INT64 Timestamp; /* Handle open time. */
129 UINT32 ProcessId; /* Handle process ID. */
130 WINDIVERT_LAYER Layer; /* Handle layer. */
131 UINT64 Flags; /* Handle flags. */
132 INT16 Priority; /* Handle priority. */
133} WINDIVERT_DATA_REFLECT, *PWINDIVERT_DATA_REFLECT;
134
135/*
136 * WinDivert address.
137 */
138typedef struct
139{
140 INT64 Timestamp; /* Packet's timestamp. */
141 UINT64 Layer:8; /* Packet's layer. */
142 UINT64 Event:8; /* Packet event. */
143 UINT64 Sniffed:1; /* Packet was sniffed? */
144 UINT64 Outbound:1; /* Packet is outound? */
145 UINT64 Loopback:1; /* Packet is loopback? */
146 UINT64 Impostor:1; /* Packet is impostor? */
147 UINT64 IPv6:1; /* Packet is IPv6? */
148 UINT64 IPChecksum:1; /* Packet has valid IPv4 checksum? */
149 UINT64 TCPChecksum:1; /* Packet has valid TCP checksum? */
150 UINT64 UDPChecksum:1; /* Packet has valid UDP checksum? */
151 UINT64 Reserved1:40;
152 union
153 {
154 WINDIVERT_DATA_NETWORK Network; /* Network layer data. */
155 WINDIVERT_DATA_FLOW Flow; /* Flow layer data. */
156 WINDIVERT_DATA_SOCKET Socket; /* Socket layer data. */
157 WINDIVERT_DATA_REFLECT Reflect; /* Reflect layer data. */
158 UINT8 Reserved2[64];
159 };
160} WINDIVERT_ADDRESS, *PWINDIVERT_ADDRESS;
161
162/*
163 * WinDivert events.
164 */
165typedef enum
166{
167 WINDIVERT_EVENT_NETWORK_PACKET = 0, /* Network packet. */
168 WINDIVERT_EVENT_FLOW_ESTABLISHED = 1,
169 /* Flow established. */
170 WINDIVERT_EVENT_FLOW_DELETED = 2, /* Flow deleted. */
171 WINDIVERT_EVENT_SOCKET_BIND = 3, /* Socket bind. */
172 WINDIVERT_EVENT_SOCKET_CONNECT = 4, /* Socket connect. */
173 WINDIVERT_EVENT_SOCKET_LISTEN = 5, /* Socket listen. */
174 WINDIVERT_EVENT_SOCKET_ACCEPT = 6, /* Socket accept. */
175 WINDIVERT_EVENT_SOCKET_CLOSE = 7, /* Socket close. */
176 WINDIVERT_EVENT_REFLECT_OPEN = 8, /* WinDivert handle opened. */
177 WINDIVERT_EVENT_REFLECT_CLOSE = 9, /* WinDivert handle closed. */
178} WINDIVERT_EVENT, *PWINDIVERT_EVENT;
179
180/*
181 * WinDivert flags.
182 */
183#define WINDIVERT_FLAG_SNIFF 0x0001
184#define WINDIVERT_FLAG_DROP 0x0002
185#define WINDIVERT_FLAG_RECV_ONLY 0x0004
186#define WINDIVERT_FLAG_READ_ONLY WINDIVERT_FLAG_RECV_ONLY
187#define WINDIVERT_FLAG_SEND_ONLY 0x0008
188#define WINDIVERT_FLAG_WRITE_ONLY WINDIVERT_FLAG_SEND_ONLY
189#define WINDIVERT_FLAG_NO_INSTALL 0x0010
190
191/*
192 * WinDivert parameters.
193 */
194typedef enum
195{
196 WINDIVERT_PARAM_QUEUE_LENGTH = 0, /* Packet queue length. */
197 WINDIVERT_PARAM_QUEUE_TIME = 1, /* Packet queue time. */
198 WINDIVERT_PARAM_QUEUE_SIZE = 2, /* Packet queue size. */
199 WINDIVERT_PARAM_VERSION_MAJOR = 3, /* Driver version (major). */
200 WINDIVERT_PARAM_VERSION_MINOR = 4, /* Driver version (minor). */
201} WINDIVERT_PARAM, *PWINDIVERT_PARAM;
202#define WINDIVERT_PARAM_MAX WINDIVERT_PARAM_VERSION_MINOR
203
204/*
205 * WinDivert shutdown parameter.
206 */
207typedef enum
208{
209 WINDIVERT_SHUTDOWN_RECV = 0x1, /* Shutdown recv. */
210 WINDIVERT_SHUTDOWN_SEND = 0x2, /* Shutdown send. */
211 WINDIVERT_SHUTDOWN_BOTH = 0x3, /* Shutdown recv and send. */
212} WINDIVERT_SHUTDOWN, *PWINDIVERT_SHUTDOWN;
213#define WINDIVERT_SHUTDOWN_MAX WINDIVERT_SHUTDOWN_BOTH
214
215#ifndef WINDIVERT_KERNEL
216
217/*
218 * Open a WinDivert handle.
219 */
220extern WINDIVERTEXPORT HANDLE WinDivertOpen(
221 __in const char *filter,
222 __in WINDIVERT_LAYER layer,
223 __in INT16 priority,
224 __in UINT64 flags);
225
226/*
227 * Receive (read) a packet from a WinDivert handle.
228 */
229extern WINDIVERTEXPORT BOOL WinDivertRecv(
230 __in HANDLE handle,
231 __out_opt VOID *pPacket,
232 __in UINT packetLen,
233 __out_opt UINT *pRecvLen,
234 __out_opt WINDIVERT_ADDRESS *pAddr);
235
236/*
237 * Receive (read) a packet from a WinDivert handle.
238 */
239extern WINDIVERTEXPORT BOOL WinDivertRecvEx(
240 __in HANDLE handle,
241 __out_opt VOID *pPacket,
242 __in UINT packetLen,
243 __out_opt UINT *pRecvLen,
244 __in UINT64 flags,
245 __out WINDIVERT_ADDRESS *pAddr,
246 __inout_opt UINT *pAddrLen,
247 __inout_opt LPOVERLAPPED lpOverlapped);
248
249/*
250 * Send (write/inject) a packet to a WinDivert handle.
251 */
252extern WINDIVERTEXPORT BOOL WinDivertSend(
253 __in HANDLE handle,
254 __in const VOID *pPacket,
255 __in UINT packetLen,
256 __out_opt UINT *pSendLen,
257 __in const WINDIVERT_ADDRESS *pAddr);
258
259/*
260 * Send (write/inject) a packet to a WinDivert handle.
261 */
262extern WINDIVERTEXPORT BOOL WinDivertSendEx(
263 __in HANDLE handle,
264 __in const VOID *pPacket,
265 __in UINT packetLen,
266 __out_opt UINT *pSendLen,
267 __in UINT64 flags,
268 __in const WINDIVERT_ADDRESS *pAddr,
269 __in UINT addrLen,
270 __inout_opt LPOVERLAPPED lpOverlapped);
271
272/*
273 * Shutdown a WinDivert handle.
274 */
275extern WINDIVERTEXPORT BOOL WinDivertShutdown(
276 __in HANDLE handle,
277 __in WINDIVERT_SHUTDOWN how);
278
279/*
280 * Close a WinDivert handle.
281 */
282extern WINDIVERTEXPORT BOOL WinDivertClose(
283 __in HANDLE handle);
284
285/*
286 * Set a WinDivert handle parameter.
287 */
288extern WINDIVERTEXPORT BOOL WinDivertSetParam(
289 __in HANDLE handle,
290 __in WINDIVERT_PARAM param,
291 __in UINT64 value);
292
293/*
294 * Get a WinDivert handle parameter.
295 */
296extern WINDIVERTEXPORT BOOL WinDivertGetParam(
297 __in HANDLE handle,
298 __in WINDIVERT_PARAM param,
299 __out UINT64 *pValue);
300
301#endif /* WINDIVERT_KERNEL */
302
303/*
304 * WinDivert constants.
305 */
306#define WINDIVERT_PRIORITY_HIGHEST 30000
307#define WINDIVERT_PRIORITY_LOWEST (-WINDIVERT_PRIORITY_HIGHEST)
308#define WINDIVERT_PARAM_QUEUE_LENGTH_DEFAULT 4096
309#define WINDIVERT_PARAM_QUEUE_LENGTH_MIN 32
310#define WINDIVERT_PARAM_QUEUE_LENGTH_MAX 16384
311#define WINDIVERT_PARAM_QUEUE_TIME_DEFAULT 2000 /* 2s */
312#define WINDIVERT_PARAM_QUEUE_TIME_MIN 100 /* 100ms */
313#define WINDIVERT_PARAM_QUEUE_TIME_MAX 16000 /* 16s */
314#define WINDIVERT_PARAM_QUEUE_SIZE_DEFAULT 4194304 /* 4MB */
315#define WINDIVERT_PARAM_QUEUE_SIZE_MIN 65535 /* 64KB */
316#define WINDIVERT_PARAM_QUEUE_SIZE_MAX 33554432 /* 32MB */
317#define WINDIVERT_BATCH_MAX 0xFF /* 255 */
318#define WINDIVERT_MTU_MAX (40 + 0xFFFF)
319
320/****************************************************************************/
321/* WINDIVERT HELPER API */
322/****************************************************************************/
323
324/*
325 * IPv4/IPv6/ICMP/ICMPv6/TCP/UDP header definitions.
326 */
327typedef struct
328{
329 UINT8 HdrLength:4;
330 UINT8 Version:4;
331 UINT8 TOS;
332 UINT16 Length;
333 UINT16 Id;
334 UINT16 FragOff0;
335 UINT8 TTL;
336 UINT8 Protocol;
337 UINT16 Checksum;
338 UINT32 SrcAddr;
339 UINT32 DstAddr;
340} WINDIVERT_IPHDR, *PWINDIVERT_IPHDR;
341
342#define WINDIVERT_IPHDR_GET_FRAGOFF(hdr) \
343 (((hdr)->FragOff0) & 0xFF1F)
344#define WINDIVERT_IPHDR_GET_MF(hdr) \
345 ((((hdr)->FragOff0) & 0x0020) != 0)
346#define WINDIVERT_IPHDR_GET_DF(hdr) \
347 ((((hdr)->FragOff0) & 0x0040) != 0)
348#define WINDIVERT_IPHDR_GET_RESERVED(hdr) \
349 ((((hdr)->FragOff0) & 0x0080) != 0)
350
351#define WINDIVERT_IPHDR_SET_FRAGOFF(hdr, val) \
352 do \
353 { \
354 (hdr)->FragOff0 = (((hdr)->FragOff0) & 0x00E0) | \
355 ((val) & 0xFF1F); \
356 } \
357 while (FALSE)
358#define WINDIVERT_IPHDR_SET_MF(hdr, val) \
359 do \
360 { \
361 (hdr)->FragOff0 = (((hdr)->FragOff0) & 0xFFDF) | \
362 (((val) & 0x0001) << 5); \
363 } \
364 while (FALSE)
365#define WINDIVERT_IPHDR_SET_DF(hdr, val) \
366 do \
367 { \
368 (hdr)->FragOff0 = (((hdr)->FragOff0) & 0xFFBF) | \
369 (((val) & 0x0001) << 6); \
370 } \
371 while (FALSE)
372#define WINDIVERT_IPHDR_SET_RESERVED(hdr, val) \
373 do \
374 { \
375 (hdr)->FragOff0 = (((hdr)->FragOff0) & 0xFF7F) | \
376 (((val) & 0x0001) << 7); \
377 } \
378 while (FALSE)
379
380typedef struct
381{
382 UINT8 TrafficClass0:4;
383 UINT8 Version:4;
384 UINT8 FlowLabel0:4;
385 UINT8 TrafficClass1:4;
386 UINT16 FlowLabel1;
387 UINT16 Length;
388 UINT8 NextHdr;
389 UINT8 HopLimit;
390 UINT32 SrcAddr[4];
391 UINT32 DstAddr[4];
392} WINDIVERT_IPV6HDR, *PWINDIVERT_IPV6HDR;
393
394#define WINDIVERT_IPV6HDR_GET_TRAFFICCLASS(hdr) \
395 ((((hdr)->TrafficClass0) << 4) | ((hdr)->TrafficClass1))
396#define WINDIVERT_IPV6HDR_GET_FLOWLABEL(hdr) \
397 ((((UINT32)(hdr)->FlowLabel0) << 16) | ((UINT32)(hdr)->FlowLabel1))
398
399#define WINDIVERT_IPV6HDR_SET_TRAFFICCLASS(hdr, val) \
400 do \
401 { \
402 (hdr)->TrafficClass0 = ((UINT8)(val) >> 4); \
403 (hdr)->TrafficClass1 = (UINT8)(val); \
404 } \
405 while (FALSE)
406#define WINDIVERT_IPV6HDR_SET_FLOWLABEL(hdr, val) \
407 do \
408 { \
409 (hdr)->FlowLabel0 = (UINT8)((val) >> 16); \
410 (hdr)->FlowLabel1 = (UINT16)(val); \
411 } \
412 while (FALSE)
413
414typedef struct
415{
416 UINT8 Type;
417 UINT8 Code;
418 UINT16 Checksum;
419 UINT32 Body;
420} WINDIVERT_ICMPHDR, *PWINDIVERT_ICMPHDR;
421
422typedef struct
423{
424 UINT8 Type;
425 UINT8 Code;
426 UINT16 Checksum;
427 UINT32 Body;
428} WINDIVERT_ICMPV6HDR, *PWINDIVERT_ICMPV6HDR;
429
430typedef struct
431{
432 UINT16 SrcPort;
433 UINT16 DstPort;
434 UINT32 SeqNum;
435 UINT32 AckNum;
436 UINT16 Reserved1:4;
437 UINT16 HdrLength:4;
438 UINT16 Fin:1;
439 UINT16 Syn:1;
440 UINT16 Rst:1;
441 UINT16 Psh:1;
442 UINT16 Ack:1;
443 UINT16 Urg:1;
444 UINT16 Reserved2:2;
445 UINT16 Window;
446 UINT16 Checksum;
447 UINT16 UrgPtr;
448} WINDIVERT_TCPHDR, *PWINDIVERT_TCPHDR;
449
450typedef struct
451{
452 UINT16 SrcPort;
453 UINT16 DstPort;
454 UINT16 Length;
455 UINT16 Checksum;
456} WINDIVERT_UDPHDR, *PWINDIVERT_UDPHDR;
457
458/*
459 * Flags for WinDivertHelperCalcChecksums()
460 */
461#define WINDIVERT_HELPER_NO_IP_CHECKSUM 1
462#define WINDIVERT_HELPER_NO_ICMP_CHECKSUM 2
463#define WINDIVERT_HELPER_NO_ICMPV6_CHECKSUM 4
464#define WINDIVERT_HELPER_NO_TCP_CHECKSUM 8
465#define WINDIVERT_HELPER_NO_UDP_CHECKSUM 16
466
467#ifndef WINDIVERT_KERNEL
468
469/*
470 * Hash a packet.
471 */
472extern WINDIVERTEXPORT UINT64 WinDivertHelperHashPacket(
473 __in const VOID *pPacket,
474 __in UINT packetLen,
475 __in UINT64 seed
476#ifdef __cplusplus
477 = 0
478#endif
479);
480
481/*
482 * Parse IPv4/IPv6/ICMP/ICMPv6/TCP/UDP headers from a raw packet.
483 */
484extern WINDIVERTEXPORT BOOL WinDivertHelperParsePacket(
485 __in const VOID *pPacket,
486 __in UINT packetLen,
487 __out_opt PWINDIVERT_IPHDR *ppIpHdr,
488 __out_opt PWINDIVERT_IPV6HDR *ppIpv6Hdr,
489 __out_opt UINT8 *pProtocol,
490 __out_opt PWINDIVERT_ICMPHDR *ppIcmpHdr,
491 __out_opt PWINDIVERT_ICMPV6HDR *ppIcmpv6Hdr,
492 __out_opt PWINDIVERT_TCPHDR *ppTcpHdr,
493 __out_opt PWINDIVERT_UDPHDR *ppUdpHdr,
494 __out_opt PVOID *ppData,
495 __out_opt UINT *pDataLen,
496 __out_opt PVOID *ppNext,
497 __out_opt UINT *pNextLen);
498
499/*
500 * Parse an IPv4 address.
501 */
502extern WINDIVERTEXPORT BOOL WinDivertHelperParseIPv4Address(
503 __in const char *addrStr,
504 __out_opt UINT32 *pAddr);
505
506/*
507 * Parse an IPv6 address.
508 */
509extern WINDIVERTEXPORT BOOL WinDivertHelperParseIPv6Address(
510 __in const char *addrStr,
511 __out_opt UINT32 *pAddr);
512
513/*
514 * Format an IPv4 address.
515 */
516extern WINDIVERTEXPORT BOOL WinDivertHelperFormatIPv4Address(
517 __in UINT32 addr,
518 __out char *buffer,
519 __in UINT bufLen);
520
521/*
522 * Format an IPv6 address.
523 */
524extern WINDIVERTEXPORT BOOL WinDivertHelperFormatIPv6Address(
525 __in const UINT32 *pAddr,
526 __out char *buffer,
527 __in UINT bufLen);
528
529/*
530 * Calculate IPv4/IPv6/ICMP/ICMPv6/TCP/UDP checksums.
531 */
532extern WINDIVERTEXPORT BOOL WinDivertHelperCalcChecksums(
533 __inout VOID *pPacket,
534 __in UINT packetLen,
535 __out_opt WINDIVERT_ADDRESS *pAddr,
536 __in UINT64 flags);
537
538/*
539 * Decrement the TTL/HopLimit.
540 */
541extern WINDIVERTEXPORT BOOL WinDivertHelperDecrementTTL(
542 __inout VOID *pPacket,
543 __in UINT packetLen);
544
545/*
546 * Compile the given filter string.
547 */
548extern WINDIVERTEXPORT BOOL WinDivertHelperCompileFilter(
549 __in const char *filter,
550 __in WINDIVERT_LAYER layer,
551 __out_opt char *object,
552 __in UINT objLen,
553 __out_opt const char **errorStr,
554 __out_opt UINT *errorPos);
555
556/*
557 * Evaluate the given filter string.
558 */
559extern WINDIVERTEXPORT BOOL WinDivertHelperEvalFilter(
560 __in const char *filter,
561 __in const VOID *pPacket,
562 __in UINT packetLen,
563 __in const WINDIVERT_ADDRESS *pAddr);
564
565/*
566 * Format the given filter string.
567 */
568extern WINDIVERTEXPORT BOOL WinDivertHelperFormatFilter(
569 __in const char *filter,
570 __in WINDIVERT_LAYER layer,
571 __out char *buffer,
572 __in UINT bufLen);
573
574/*
575 * Byte ordering.
576 */
577extern WINDIVERTEXPORT UINT16 WinDivertHelperNtohs(
578 __in UINT16 x);
579extern WINDIVERTEXPORT UINT16 WinDivertHelperHtons(
580 __in UINT16 x);
581extern WINDIVERTEXPORT UINT32 WinDivertHelperNtohl(
582 __in UINT32 x);
583extern WINDIVERTEXPORT UINT32 WinDivertHelperHtonl(
584 __in UINT32 x);
585extern WINDIVERTEXPORT UINT64 WinDivertHelperNtohll(
586 __in UINT64 x);
587extern WINDIVERTEXPORT UINT64 WinDivertHelperHtonll(
588 __in UINT64 x);
589extern WINDIVERTEXPORT void WinDivertHelperNtohIpv6Address(
590 __in const UINT *inAddr,
591 __out UINT *outAddr);
592extern WINDIVERTEXPORT void WinDivertHelperHtonIpv6Address(
593 __in const UINT *inAddr,
594 __out UINT *outAddr);
595
596#endif /* WINDIVERT_KERNEL */
597
598#ifdef __cplusplus
599}
600#endif
601
602#endif /* __WINDIVERT_H */