xref: /reactos/drivers/network/afd/include/afd.h (revision 4f0b8d3d)
1 /*
2  * COPYRIGHT:        See COPYING in the top level directory
3  * PROJECT:          ReactOS kernel
4  * FILE:             drivers/net/afd/include/afd.h
5  * PURPOSE:          Ancillary functions driver -- constants and structures
6  * PROGRAMMER:       Art Yerkes (ayerkes@speakeasy.net)
7  * UPDATE HISTORY:
8  * 20040630 Created
9  */
10 
11 #ifndef _AFD_H
12 #define _AFD_H
13 
14 #include <ntddk.h>
15 #include <ntifs.h>
16 #include <ndk/obtypes.h>
17 #include <ndk/obfuncs.h>
18 #include <ndk/mmtypes.h>
19 #include <ndk/mmfuncs.h>
20 #include <tdi.h>
21 #include <tdikrnl.h>
22 #include <tdiinfo.h>
23 #include <string.h>
24 #define _WINBASE_
25 #define _WINDOWS_H
26 #define _INC_WINDOWS
27 #include <windef.h>
28 #include <winsock2.h>
29 #include <afd/shared.h>
30 #include <pseh/pseh2.h>
31 
32 #include "tdi_proto.h"
33 #include "tdiconn.h"
34 #include "debug.h"
35 
36 #ifndef MIN
37 #define MIN(x,y) (((x)<(y))?(x):(y))
38 #endif
39 
40 #define IOCTL_TCP_QUERY_INFORMATION_EX \
41 	CTL_CODE(FILE_DEVICE_NETWORK, 0, METHOD_NEITHER, FILE_ANY_ACCESS)
42 
43 #define TL_INSTANCE 0
44 #define	IP_MIB_STATS_ID 1
45 #define	IP_MIB_ADDRTABLE_ENTRY_ID 0x102
46 
47 typedef struct IPSNMP_INFO {
48 	ULONG Forwarding;
49 	ULONG DefaultTTL;
50 	ULONG InReceives;
51 	ULONG InHdrErrors;
52 	ULONG InAddrErrors;
53 	ULONG ForwDatagrams;
54 	ULONG InUnknownProtos;
55 	ULONG InDiscards;
56 	ULONG InDelivers;
57 	ULONG OutRequests;
58 	ULONG RoutingDiscards;
59 	ULONG OutDiscards;
60 	ULONG OutNoRoutes;
61 	ULONG ReasmTimeout;
62 	ULONG ReasmReqds;
63 	ULONG ReasmOks;
64 	ULONG ReasmFails;
65 	ULONG FragOks;
66 	ULONG FragFails;
67 	ULONG FragCreates;
68 	ULONG NumIf;
69 	ULONG NumAddr;
70 	ULONG NumRoutes;
71 } IPSNMP_INFO, *PIPSNMP_INFO;
72 
73 typedef struct IPADDR_ENTRY {
74 	ULONG  Addr;
75 	ULONG  Index;
76 	ULONG  Mask;
77 	ULONG  BcastAddr;
78 	ULONG  ReasmSize;
79 	USHORT Context;
80 	USHORT Pad;
81 } IPADDR_ENTRY, *PIPADDR_ENTRY;
82 
83 #define DN2H(dw) \
84     ((((dw) & 0xFF000000L) >> 24) | \
85 	 (((dw) & 0x00FF0000L) >> 8) | \
86 	 (((dw) & 0x0000FF00L) << 8) | \
87 	 (((dw) & 0x000000FFL) << 24))
88 
89 #define SOCKET_STATE_INVALID_TRANSITION ((DWORD)-1)
90 #define SOCKET_STATE_CREATED            0
91 #define SOCKET_STATE_BOUND              1
92 #define SOCKET_STATE_CONNECTING         2
93 #define SOCKET_STATE_CONNECTED          3
94 #define SOCKET_STATE_LISTENING          4
95 #define SOCKET_STATE_MASK               0x0000ffff
96 #define SOCKET_STATE_EOF_READ           0x20000000
97 #define SOCKET_STATE_LOCKED             0x40000000
98 #define SOCKET_STATE_NEW                0x80000000
99 #define SOCKET_STATE_CLOSED             0x00000100
100 
101 #define FUNCTION_CONNECT                0
102 #define FUNCTION_RECV                   1
103 #define FUNCTION_SEND                   2
104 #define FUNCTION_PREACCEPT              3
105 #define FUNCTION_ACCEPT                 4
106 #define FUNCTION_DISCONNECT             5
107 #define FUNCTION_CLOSE                  6
108 #define MAX_FUNCTIONS                   7
109 
110 #define IN_FLIGHT_REQUESTS              5
111 
112 #define EXTRA_LOCK_BUFFERS              2 /* Number of extra buffers needed
113 					   * for ancillary data on packet
114 					   * requests. */
115 
116 /* XXX This is a hack we should clean up later
117  * We do this in order to get some storage for the locked handle table
118  * Maybe I'll use some tail item in the irp instead */
119 #define AFD_HANDLES(x) ((PAFD_HANDLE)(x)->Exclusive)
120 #define SET_AFD_HANDLES(x,y) (((x)->Exclusive) = (ULONG_PTR)(y))
121 
122 typedef struct _AFD_MAPBUF {
123     PVOID BufferAddress;
124     PMDL  Mdl;
125 } AFD_MAPBUF, *PAFD_MAPBUF;
126 
127 typedef struct _AFD_DEVICE_EXTENSION {
128     PDEVICE_OBJECT DeviceObject;
129     LIST_ENTRY Polls;
130     KSPIN_LOCK Lock;
131 } AFD_DEVICE_EXTENSION, *PAFD_DEVICE_EXTENSION;
132 
133 typedef struct _AFD_ACTIVE_POLL {
134     LIST_ENTRY ListEntry;
135     PIRP Irp;
136     PAFD_DEVICE_EXTENSION DeviceExt;
137     KDPC TimeoutDpc;
138     KTIMER Timer;
139     PKEVENT EventObject;
140     BOOLEAN Exclusive;
141 } AFD_ACTIVE_POLL, *PAFD_ACTIVE_POLL;
142 
143 typedef struct _IRP_LIST {
144     LIST_ENTRY ListEntry;
145     PIRP Irp;
146 } IRP_LIST, *PIRP_LIST;
147 
148 typedef struct _AFD_TDI_OBJECT {
149     PFILE_OBJECT Object;
150     HANDLE Handle;
151 } AFD_TDI_OBJECT, *PAFD_TDI_OBJECT;
152 
153 typedef struct _AFD_TDI_OBJECT_QELT {
154     LIST_ENTRY ListEntry;
155     UINT Seq;
156     PTDI_CONNECTION_INFORMATION ConnInfo;
157     AFD_TDI_OBJECT Object;
158 } AFD_TDI_OBJECT_QELT, *PAFD_TDI_OBJECT_QELT;
159 
160 typedef struct _AFD_IN_FLIGHT_REQUEST {
161     PIRP InFlightRequest;
162     IO_STATUS_BLOCK Iosb;
163     PTDI_CONNECTION_INFORMATION ConnectionCallInfo;
164     PTDI_CONNECTION_INFORMATION ConnectionReturnInfo;
165 } AFD_IN_FLIGHT_REQUEST, *PAFD_IN_FLIGHT_REQUEST;
166 
167 typedef struct _AFD_DATA_WINDOW {
168     PCHAR Window;
169     UINT BytesUsed, Size, Content;
170 } AFD_DATA_WINDOW, *PAFD_DATA_WINDOW;
171 
172 typedef struct _AFD_STORED_DATAGRAM {
173     LIST_ENTRY ListEntry;
174     UINT Len;
175     PTRANSPORT_ADDRESS Address;
176     CHAR Buffer[1];
177 } AFD_STORED_DATAGRAM, *PAFD_STORED_DATAGRAM;
178 
179 typedef struct _AFD_FCB {
180     BOOLEAN Locked, Critical, Overread, NonBlocking, OobInline, TdiReceiveClosed, SendClosed;
181     UINT State, Flags, GroupID, GroupType;
182     KIRQL OldIrql;
183     UINT LockCount;
184     PVOID CurrentThread;
185     PFILE_OBJECT FileObject;
186     PAFD_DEVICE_EXTENSION DeviceExt;
187     BOOLEAN DelayedAccept;
188     UINT ConnSeq;
189     USHORT DisconnectFlags;
190     BOOLEAN DisconnectPending;
191     LARGE_INTEGER DisconnectTimeout;
192     PTRANSPORT_ADDRESS LocalAddress, RemoteAddress;
193     PTDI_CONNECTION_INFORMATION AddressFrom, ConnectCallInfo, ConnectReturnInfo;
194     AFD_TDI_OBJECT AddressFile, Connection;
195     AFD_IN_FLIGHT_REQUEST ConnectIrp, ListenIrp, ReceiveIrp, SendIrp, DisconnectIrp;
196     AFD_DATA_WINDOW Send, Recv;
197     KMUTEX Mutex;
198     PKEVENT EventSelect;
199     DWORD EventSelectTriggers;
200     DWORD EventSelectDisabled;
201     UNICODE_STRING TdiDeviceName;
202     PVOID Context;
203     DWORD PollState;
204     NTSTATUS PollStatus[FD_MAX_EVENTS];
205     NTSTATUS LastReceiveStatus;
206     UINT ContextSize;
207     PVOID ConnectData;
208     UINT FilledConnectData;
209     UINT ConnectDataSize;
210     PVOID DisconnectData;
211     UINT FilledDisconnectData;
212     UINT DisconnectDataSize;
213     PVOID ConnectOptions;
214     UINT FilledConnectOptions;
215     UINT ConnectOptionsSize;
216     PVOID DisconnectOptions;
217     UINT FilledDisconnectOptions;
218     UINT DisconnectOptionsSize;
219     LIST_ENTRY PendingIrpList[MAX_FUNCTIONS];
220     LIST_ENTRY DatagramList;
221     LIST_ENTRY PendingConnections;
222 } AFD_FCB, *PAFD_FCB;
223 
224 /* bind.c */
225 
226 NTSTATUS WarmSocketForBind( PAFD_FCB FCB, ULONG ShareType );
227 NTSTATUS NTAPI
228 AfdBindSocket(PDEVICE_OBJECT DeviceObject, PIRP Irp,
229 	      PIO_STACK_LOCATION IrpSp);
230 
231 /* connect.c */
232 
233 NTSTATUS MakeSocketIntoConnection( PAFD_FCB FCB );
234 NTSTATUS WarmSocketForConnection( PAFD_FCB FCB );
235 NTSTATUS NTAPI
236 AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
237 		       PIO_STACK_LOCATION IrpSp);
238 NTSTATUS NTAPI
239 AfdGetConnectData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
240 	          PIO_STACK_LOCATION IrpSp);
241 NTSTATUS NTAPI
242 AfdSetConnectData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
243                   PIO_STACK_LOCATION IrpSp);
244 NTSTATUS NTAPI
245 AfdSetConnectDataSize(PDEVICE_OBJECT DeviceObject, PIRP Irp,
246                       PIO_STACK_LOCATION IrpSp);
247 NTSTATUS NTAPI
248 AfdGetConnectOptions(PDEVICE_OBJECT DeviceObject, PIRP Irp,
249 	             PIO_STACK_LOCATION IrpSp);
250 NTSTATUS NTAPI
251 AfdSetConnectOptions(PDEVICE_OBJECT DeviceObject, PIRP Irp,
252                      PIO_STACK_LOCATION IrpSp);
253 NTSTATUS NTAPI
254 AfdSetConnectOptionsSize(PDEVICE_OBJECT DeviceObject, PIRP Irp,
255                          PIO_STACK_LOCATION IrpSp);
256 
257 /* context.c */
258 
259 NTSTATUS NTAPI
260 AfdGetContext( PDEVICE_OBJECT DeviceObject, PIRP Irp,
261 	       PIO_STACK_LOCATION IrpSp );
262 NTSTATUS NTAPI
263 AfdGetContextSize( PDEVICE_OBJECT DeviceObject, PIRP Irp,
264 	           PIO_STACK_LOCATION IrpSp );
265 NTSTATUS NTAPI
266 AfdSetContext( PDEVICE_OBJECT DeviceObject, PIRP Irp,
267 	       PIO_STACK_LOCATION IrpSp );
268 
269 /* info.c */
270 
271 NTSTATUS NTAPI
272 AfdGetInfo( PDEVICE_OBJECT DeviceObject, PIRP Irp,
273 	    PIO_STACK_LOCATION IrpSp );
274 
275 NTSTATUS NTAPI
276 AfdSetInfo( PDEVICE_OBJECT DeviceObject, PIRP Irp,
277 	    PIO_STACK_LOCATION IrpSp );
278 
279 NTSTATUS NTAPI
280 AfdGetSockName( PDEVICE_OBJECT DeviceObject, PIRP Irp,
281                 PIO_STACK_LOCATION IrpSp );
282 
283 NTSTATUS NTAPI
284 AfdGetPeerName( PDEVICE_OBJECT DeviceObject, PIRP Irp,
285                 PIO_STACK_LOCATION IrpSp );
286 
287 /* listen.c */
288 NTSTATUS AfdWaitForListen( PDEVICE_OBJECT DeviceObject, PIRP Irp,
289 			   PIO_STACK_LOCATION IrpSp );
290 
291 NTSTATUS AfdListenSocket(PDEVICE_OBJECT DeviceObject, PIRP Irp,
292 			 PIO_STACK_LOCATION IrpSp);
293 
294 NTSTATUS AfdAccept( PDEVICE_OBJECT DeviceObject, PIRP Irp,
295 		    PIO_STACK_LOCATION IrpSp );
296 
297 /* lock.c */
298 
299 PAFD_WSABUF LockBuffers( PAFD_WSABUF Buf, UINT Count,
300 			 PVOID AddressBuf, PINT AddressLen,
301 			 BOOLEAN Write, BOOLEAN LockAddress,
302              KPROCESSOR_MODE LockMode );
303 VOID UnlockBuffers( PAFD_WSABUF Buf, UINT Count, BOOL Address );
304 BOOLEAN SocketAcquireStateLock( PAFD_FCB FCB );
305 NTSTATUS NTAPI UnlockAndMaybeComplete
306 ( PAFD_FCB FCB, NTSTATUS Status, PIRP Irp,
307   UINT Information );
308 VOID SocketStateUnlock( PAFD_FCB FCB );
309 NTSTATUS LostSocket( PIRP Irp );
310 PAFD_HANDLE LockHandles( PAFD_HANDLE HandleArray, UINT HandleCount );
311 VOID UnlockHandles( PAFD_HANDLE HandleArray, UINT HandleCount );
312 PVOID LockRequest( PIRP Irp, PIO_STACK_LOCATION IrpSp, BOOLEAN Output, KPROCESSOR_MODE *LockMode );
313 VOID UnlockRequest( PIRP Irp, PIO_STACK_LOCATION IrpSp );
314 PVOID GetLockedData( PIRP Irp, PIO_STACK_LOCATION IrpSp );
315 NTSTATUS LeaveIrpUntilLater( PAFD_FCB FCB, PIRP Irp, UINT Function );
316 NTSTATUS QueueUserModeIrp(PAFD_FCB FCB, PIRP Irp, UINT Function);
317 
318 /* main.c */
319 
320 VOID OskitDumpBuffer( PCHAR Buffer, UINT Len );
321 VOID DestroySocket( PAFD_FCB FCB );
322 DRIVER_CANCEL AfdCancelHandler;
323 VOID RetryDisconnectCompletion(PAFD_FCB FCB);
324 BOOLEAN CheckUnlockExtraBuffers(PAFD_FCB FCB, PIO_STACK_LOCATION IrpSp);
325 
326 /* read.c */
327 
328 IO_COMPLETION_ROUTINE ReceiveComplete;
329 
330 IO_COMPLETION_ROUTINE PacketSocketRecvComplete;
331 
332 NTSTATUS NTAPI
333 AfdConnectedSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp, PIO_STACK_LOCATION IrpSp, BOOLEAN Short);
334 NTSTATUS NTAPI
335 AfdPacketSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
336 			PIO_STACK_LOCATION IrpSp );
337 
338 /* select.c */
339 
340 NTSTATUS NTAPI
341 AfdSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
342 	   PIO_STACK_LOCATION IrpSp );
343 NTSTATUS NTAPI
344 AfdEventSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
345 		PIO_STACK_LOCATION IrpSp );
346 NTSTATUS NTAPI
347 AfdEnumEvents( PDEVICE_OBJECT DeviceObject, PIRP Irp,
348 	       PIO_STACK_LOCATION IrpSp );
349 VOID PollReeval( PAFD_DEVICE_EXTENSION DeviceObject, PFILE_OBJECT FileObject );
350 VOID KillSelectsForFCB( PAFD_DEVICE_EXTENSION DeviceExt,
351                         PFILE_OBJECT FileObject, BOOLEAN ExclusiveOnly );
352 VOID ZeroEvents( PAFD_HANDLE HandleArray,
353 		 UINT HandleCount );
354 VOID SignalSocket(
355    PAFD_ACTIVE_POLL Poll OPTIONAL, PIRP _Irp OPTIONAL,
356    PAFD_POLL_INFO PollReq, NTSTATUS Status);
357 
358 /* tdi.c */
359 
360 NTSTATUS TdiOpenAddressFile(
361     PUNICODE_STRING DeviceName,
362     PTRANSPORT_ADDRESS Name,
363     ULONG ShareType,
364     PHANDLE AddressHandle,
365     PFILE_OBJECT *AddressObject);
366 
367 NTSTATUS TdiAssociateAddressFile(
368   HANDLE AddressHandle,
369   PFILE_OBJECT ConnectionObject);
370 
371 NTSTATUS TdiDisassociateAddressFile(
372   PFILE_OBJECT ConnectionObject);
373 
374 NTSTATUS TdiListen
375 ( PIRP *Irp,
376   PFILE_OBJECT ConnectionObject,
377   PTDI_CONNECTION_INFORMATION *RequestConnectionInfo,
378   PTDI_CONNECTION_INFORMATION *ReturnConnectionInfo,
379   PIO_STATUS_BLOCK Iosb,
380   PIO_COMPLETION_ROUTINE  CompletionRoutine,
381   PVOID CompletionContext);
382 
383 NTSTATUS TdiReceive
384 ( PIRP *Irp,
385   PFILE_OBJECT ConnectionObject,
386   USHORT Flags,
387   PCHAR Buffer,
388   UINT BufferLength,
389   PIO_STATUS_BLOCK Iosb,
390   PIO_COMPLETION_ROUTINE  CompletionRoutine,
391   PVOID CompletionContext);
392 
393 NTSTATUS TdiSend
394 ( PIRP *Irp,
395   PFILE_OBJECT ConnectionObject,
396   USHORT Flags,
397   PCHAR Buffer,
398   UINT BufferLength,
399   PIO_STATUS_BLOCK Iosb,
400   PIO_COMPLETION_ROUTINE  CompletionRoutine,
401   PVOID CompletionContext);
402 
403 NTSTATUS TdiReceiveDatagram(
404     PIRP *Irp,
405     PFILE_OBJECT TransportObject,
406     USHORT Flags,
407     PCHAR Buffer,
408     UINT BufferLength,
409     PTDI_CONNECTION_INFORMATION From,
410     PIO_STATUS_BLOCK Iosb,
411     PIO_COMPLETION_ROUTINE CompletionRoutine,
412     PVOID CompletionContext);
413 
414 NTSTATUS TdiSendDatagram(
415     PIRP *Irp,
416     PFILE_OBJECT TransportObject,
417     PCHAR Buffer,
418     UINT BufferLength,
419     PTDI_CONNECTION_INFORMATION To,
420     PIO_STATUS_BLOCK Iosb,
421     PIO_COMPLETION_ROUTINE CompletionRoutine,
422     PVOID CompletionContext);
423 
424 NTSTATUS TdiQueryMaxDatagramLength(
425         PFILE_OBJECT FileObject,
426         PUINT MaxDatagramLength);
427 
428 /* write.c */
429 
430 NTSTATUS NTAPI
431 AfdConnectedSocketWriteData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
432 			    PIO_STACK_LOCATION IrpSp, BOOLEAN Short);
433 NTSTATUS NTAPI
434 AfdPacketSocketWriteData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
435 			 PIO_STACK_LOCATION IrpSp);
436 
437 #endif/*_AFD_H*/
438