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