VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/VBoxNetFltCommon-win.h@ 29826

Last change on this file since 29826 was 29682, checked in by vboxsync, 14 years ago

netflt/win: dbg: use ExAllocatePoolWithTag for debugging (to make verifier blame memory leaks)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1/* $Id: VBoxNetFltCommon-win.h 29682 2010-05-20 11:16:03Z vboxsync $ */
2/** @file
3 * VBoxNetFltCommon.h - Network Filter Driver (Host), Windows Specific Code. Common headeer with commonly used defines and decls
4 */
5
6/*
7 * Copyright (C) 2008 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17/*
18 * Based in part on Microsoft DDK sample code for Ndis Intermediate Miniport passthru driver sample.
19 * Copyright (c) 1993-1999, Microsoft Corporation
20 */
21
22#ifndef ___VBoxNetFltCommon_win_h___
23#define ___VBoxNetFltCommon_win_h___
24
25//#define NTSTRSAFE_LIB
26
27#ifdef DEBUG
28//# define DEBUG_NETFLT_PACKETS
29# ifndef DEBUG_misha
30# define DEBUG_NETFLT_NOASSERT
31# endif
32/* # define DEBUG_NETFLT_LOOPBACK */
33
34/* receive logic has several branches */
35/* the DEBUG_NETFLT_RECV* macros used to debug the ProtocolReceive callback
36 * which is typically not used in case the underlying miniport indicates the packets with NdisMIndicateReceivePacket
37 * the best way to debug the ProtocolReceive (which in turn has several branches) is to enable the DEBUG_NETFLT_RECV
38 * one by one in the below order, i.e.
39 * first DEBUG_NETFLT_RECV
40 * then DEBUG_NETFLT_RECV + DEBUG_NETFLT_RECV_NOPACKET */
41//# define DEBUG_NETFLT_RECV
42//# define DEBUG_NETFLT_RECV_NOPACKET
43//# define DEBUG_NETFLT_RECV_TRANSFERDATA
44
45//#define DEBUG_NETFLT_USE_EXALLOC
46#endif
47
48#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
49
50#include <VBox/intnet.h>
51#include <VBox/log.h>
52#include <VBox/err.h>
53#include <VBox/version.h>
54#include <iprt/initterm.h>
55#include <iprt/assert.h>
56#include <iprt/spinlock.h>
57#include <iprt/semaphore.h>
58#include <iprt/process.h>
59#include <iprt/alloc.h>
60#include <iprt/alloca.h>
61#include <iprt/time.h>
62#include <iprt/net.h>
63
64RT_C_DECLS_BEGIN
65#include <ndis.h>
66RT_C_DECLS_END
67
68
69
70#define VBOXNETFLT_OS_SPECFIC 1
71
72#ifdef VBOX_NETFLT_ONDEMAND_BIND
73# define VBOXNETFLT_PROTOCOL_NAME L"VBoxNetFltPt"
74#else
75# ifndef VBOXNETADP
76# define VBOXNETFLT_PROTOCOL_NAME L"VBoxNetFlt"
77
78/** this is to support ioctl interface */
79# define LINKNAME_STRING L"\\DosDevices\\Global\\VBoxNetFlt"
80# define NTDEVICE_STRING L"\\Device\\VBoxNetFlt"
81# else
82# define LINKNAME_STRING L"\\DosDevices\\Global\\VBoxNetAdp"
83# define NTDEVICE_STRING L"\\Device\\VBoxNetAdp"
84# endif
85//# define VBOXNETFLT_WIN_IOCTL_INIT CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_NEITHER, FILE_WRITE_ACCESS)
86//# define VBOXNETFLT_WIN_IOCTL_FINI CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_NEITHER, FILE_WRITE_ACCESS)
87#endif
88
89/** version
90 * NOTE: we are NOT using NDIS 5.1 features now, the code under "#ifdef NDIS51xxx" is not tested and may not work and should be removed soon */
91#ifdef NDIS51_MINIPORT
92# define VBOXNETFLT_MAJOR_NDIS_VERSION 5
93# define VBOXNETFLT_MINOR_NDIS_VERSION 1
94#else
95# define VBOXNETFLT_MAJOR_NDIS_VERSION 5
96# define VBOXNETFLT_MINOR_NDIS_VERSION 0
97#endif
98
99#ifdef NDIS51
100# define VBOXNETFLT_PROT_MAJOR_NDIS_VERSION 5
101# define VBOXNETFLT_PROT_MINOR_NDIS_VERSION 0
102#else
103# define VBOXNETFLT_PROT_MAJOR_NDIS_VERSION 5
104# define VBOXNETFLT_PROT_MINOR_NDIS_VERSION 0
105#endif
106
107/** advance declaration */
108typedef struct _ADAPT ADAPT, *PADAPT;
109
110typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
111
112/** configuration */
113
114/** received packets queue size. the queue is used when the driver is working in a pass-thru mode */
115#define MAX_RECEIVE_PACKET_ARRAY_SIZE 40
116
117/** Ndis Packet pool settings
118 * these are applied to both receive and send packet pools */
119#define MAX_PACKET_POOL_SIZE 0x0000FFFF
120#define MIN_PACKET_POOL_SIZE 0x000000FF
121
122/** packet queue size used when the driver is working in the "active" mode */
123#define PACKET_INFO_POOL_SIZE 0x0000FFFF
124
125#ifndef VBOXNETADP
126/** memory tag used for memory allocations
127 * (VBNF stands for VBox NetFlt) */
128# define MEM_TAG 'FNBV'
129#else
130/** memory tag used for memory allocations
131 * (VBNA stands for VBox NetAdp) */
132# define MEM_TAG 'ANBV'
133#endif
134
135/** receive and transmit Ndis buffer pool size */
136#define TX_BUFFER_POOL_SIZE 128
137#define RX_BUFFER_POOL_SIZE 128
138
139#define ETH_HEADER_SIZE 14
140
141#define PACKET_QUEUE_SG_SEGS_ALLOC 32
142
143#define VBOX_NETFLT_PACKET_HEADER_MATCH_SIZE 24
144
145#if defined(DEBUG_NETFLT_PACKETS) || !defined(VBOX_LOOPBACK_USEFLAGS)
146# define VBOXNETFLT_PACKETMATCH_LENGTH (ETH_HEADER_SIZE + 2)
147#endif
148
149#ifdef VBOXNETADP
150#define VBOXNETADP_HEADER_SIZE 14
151#define VBOXNETADP_MAX_DATA_SIZE 1500
152#define VBOXNETADP_MAX_PACKET_SIZE VBOXNETADP_HEADER_SIZE + VBOXNETADP_MAX_DATA_SIZE
153#define VBOXNETADP_MIN_PACKET_SIZE 60
154#define VBOXNETADP_LINK_SPEED 1000000 //The unit of measurement is 100 bps, 100Mbps
155#define VBOXNETADP_MAX_LOOKAHEAD_SIZE VBOXNETADP_MAX_DATA_SIZE
156#define VBOXNETADP_VENDOR_ID 0x080027
157#define VBOXNETADP_VENDOR_DRIVER_VERSION 0x00010000
158#define VBOXNETADP_VENDOR_DESC "Sun"
159#define VBOXNETADP_MAX_MCAST_LIST 32
160#define VBOXNETADP_ETH_ADDRESS_LENGTH 6
161
162//#define VBOXNETADP_REPORT_DISCONNECTED
163#endif
164/* type defs */
165
166/** Flag specifying that the type of enqueued packet
167 * if set the info contains the PINTNETSG packet
168 * if clear the packet info contains the PNDIS_PACKET packet
169 * Typically the packet queue we are maintaining contains PNDIS_PACKETs only,
170 * however in case the underlying miniport indicates a packet with the NDIS_STATUS_RESOURCES status
171 * we MUST return the packet back to the miniport immediately
172 * this is why we are creating the INTNETSG, copying the ndis packet info there and enqueueing it */
173#define PACKET_SG 0x00000001
174
175/** the flag specifying that the packet source
176 * if set the packet comes from the host (upperlying protocol)
177 * if clear the packet comes from the wire (underlying miniport) */
178#define PACKET_SRC_HOST 0x00000002
179
180#ifndef VBOXNETFLT_NO_PACKET_QUEUE
181/** flag specifying the packet was originated by our driver
182 * i.e. we could use it on our needs and should not return it
183 * we are enqueueing "our" packets on ProtocolReceive call-back when
184 * Ndis does not give us a receive acket (the driver below us has called NdisM..IndicateReceive)
185 * this is supported for Ndis Packet only */
186#define PACKET_MINE 0x00000004
187
188/** flag passed to vboxNetFltWinQuEnqueuePacket specifying that the packet should be copied
189 * this is supported for Ndis Packet only */
190#define PACKET_COPY 0x00000008
191#endif
192
193/** packet queue element containing the packet info */
194typedef struct _PACKET_INFO
195{
196 /** list entry used for enqueueing the info */
197 LIST_ENTRY ListEntry;
198 /** pointer to the pool containing this packet info */
199 struct _PACKET_INFO_POOL * pPool;
200 /** flags describing the referenced packet. Contains PACKET_xxx flags (i.e. PACKET_SG, PACKET_SRC_HOST) */
201 uint32_t fFlags;
202 /** pointer to the packet this info represents */
203 PVOID pPacket;
204}PACKET_INFO, *PPACKET_INFO;
205
206/* paranoid check to make sure the elements in the packet info array are properly aligned */
207C_ASSERT((sizeof(PACKET_INFO) & (sizeof(PVOID) - 1)) == 0);
208
209/** represents the packet queue */
210typedef LIST_ENTRY PACKET_QUEUE, *PPACKET_QUEUE;
211
212/*
213 * we are using non-interlocked versions of LIST_ENTRY-related operations macros and synchronize
214 * access to the queue and its elements by aquiring/releasing a spinlock using Ndis[Acquire,Release]Spinlock
215 *
216 * we are NOT using interlocked versions of insert/remove head/tail list functions because we need to iterate though
217 * the queue elements as well as remove elements from the midle of the queue
218 *
219 * * @todo: it seems that we can switch to using interlocked versions of list-entry functions
220 * since we have removed all functionality (mentioned above, i.e. queue elements iteration, etc.) that might prevent us from doing this
221 */
222typedef struct _INTERLOCKED_PACKET_QUEUE
223{
224 /** queue */
225 PACKET_QUEUE Queue;
226 /** queue lock */
227 NDIS_SPIN_LOCK Lock;
228}INTERLOCKED_PACKET_QUEUE, *PINTERLOCKED_PACKET_QUEUE;
229
230typedef struct _SINGLE_LIST
231{
232 /** queue */
233 SINGLE_LIST_ENTRY Head;
234 /** pointer to the list tail. used to enqueue elements to the tail of the list */
235 PSINGLE_LIST_ENTRY pTail;
236} SINGLE_LIST, *PSINGLE_LIST;
237
238typedef struct _INTERLOCKED_SINGLE_LIST
239{
240 /** queue */
241 SINGLE_LIST List;
242 /** queue lock */
243 NDIS_SPIN_LOCK Lock;
244} INTERLOCKED_SINGLE_LIST, *PINTERLOCKED_SINGLE_LIST;
245
246/** packet info pool contains free packet info elements to be used for the packet queue
247 * we are using the pool mechanism to allocate packet queue elements
248 * the pool mechanism is pretty simple now, we are allocating a bunch of memory
249 * for maintaining PACKET_INFO_POOL_SIZE queue elements and just returning null when the pool is exhausted
250 * This mechanism seems to be enough for now since we are using PACKET_INFO_POOL_SIZE = 0xffff which is
251 * the maximum size of packets the ndis packet pool supports */
252typedef struct _PACKET_INFO_POOL
253{
254 /** free packet info queue */
255 INTERLOCKED_PACKET_QUEUE Queue;
256 /** memory bugger used by the pool */
257 PVOID pBuffer;
258}PACKET_INFO_POOL, *PPACKET_INFO_POOL;
259
260typedef enum VBOXNETDEVOPSTATE
261{
262 kVBoxNetDevOpState_InvalidValue = 0,
263 kVBoxNetDevOpState_Initializing,
264 kVBoxNetDevOpState_Initialized,
265 kVBoxNetDevOpState_Deinitializing,
266 kVBoxNetDevOpState_Deinitialized,
267
268} VBOXNETDEVOPSTATE;
269
270typedef enum VBOXADAPTSTATE
271{
272 /** The usual invalid state. */
273 kVBoxAdaptState_Invalid = 0,
274 /** Initialization. */
275 kVBoxAdaptState_Connecting,
276 /** Connected fuly functional state */
277 kVBoxAdaptState_Connected,
278 /** Disconnecting */
279 kVBoxAdaptState_Disconnecting,
280 /** Disconnected */
281 kVBoxAdaptState_Disconnected,
282} VBOXADAPTSTATE;
283
284/** structure used to maintain the state and reference count of the miniport and protocol */
285typedef struct _ADAPT_DEVICE
286{
287 /** initialize state */
288 VBOXNETDEVOPSTATE OpState;
289 /** ndis power state */
290 NDIS_DEVICE_POWER_STATE PowerState;
291 /** reference count */
292 uint32_t cReferences;
293/* NDIS_HANDLE hHandle; */
294} ADAPT_DEVICE, *PADAPT_DEVICE;
295
296/* packet filter processing mode constants */
297#define VBOXNETFLT_PFP_NETFLT 1
298#define VBOXNETFLT_PFP_PASSTHRU 2
299
300/** represents filter driver device context*/
301typedef struct _ADAPT
302{
303#ifndef VBOXNETADP
304 /** handle the lower miniport */
305 NDIS_HANDLE hBindingHandle;
306 /** Protocol's Device state */
307 ADAPT_DEVICE PTState;
308#endif
309#ifndef VBOX_NETFLT_ONDEMAND_BIND
310 /** NDIS Handle to for miniport up-calls */
311 NDIS_HANDLE hMiniportHandle;
312 /** miniport device state */
313 ADAPT_DEVICE MPState;
314 /** ndis packet pool used for receives */
315 NDIS_HANDLE hRecvPacketPoolHandle;
316 /** ndis buffer pool used for receives */
317 NDIS_HANDLE hRecvBufferPoolHandle;
318#ifndef VBOXNETADP
319 /** This is used to wrap a request coming down to us.
320 * This exploits the fact that requests are serialized down to us.*/
321 NDIS_REQUEST Request;
322 /** Ndis Request Bytes needed */
323 PULONG BytesNeeded;
324 /** Ndis Request Bytes Read or Written */
325 PULONG BytesReadOrWritten;
326#else
327 volatile ULONG cTxSuccess;
328 volatile ULONG cRxSuccess;
329 volatile ULONG cTxError;
330 volatile ULONG cRxError;
331#endif
332 /** driver bind adapter state. */
333 VBOXADAPTSTATE enmState;
334#ifndef VBOXNETADP
335 /** true if we should indicate the receive complete used by the ProtocolReceeive mechanism */
336 bool bIndicateRcvComplete;
337
338 /** TRUE iff a request is pending at the miniport below */
339 bool bOutstandingRequests;
340 /** TRUE iff a request is queued at this IM miniport*/
341 bool bQueuedRequest;
342 /** @todo join all boolean states to one field treated as flags bitmap */
343 /** true iff we are processing Set packet filter OID */
344 uint8_t fProcessingPacketFilter;
345 /** true iff the upper protocol filter cache was initialized */
346 bool bUpperProtSetFilterInitialized;
347 /** trus if the adapter is closing */
348 bool bClosingAdapter;
349 /** Pending transfer data packet queue (i.e. packets that were indicated as pending on NdisTransferData call */
350 INTERLOCKED_SINGLE_LIST TransferDataList;
351 /* mac options initialized on OID_GEN_MAC_OPTIONS */
352 ULONG fMacOptions;
353 /** For initializing the miniport edge */
354 NDIS_STRING DeviceName;
355 /** For blocking UnbindAdapter while an IM Init is in progress.*/
356 NDIS_EVENT MiniportInitEvent;
357 /** The last indicated media status */
358 NDIS_STATUS LastIndicatedStatus;
359 /** The latest suppressed media status */
360 NDIS_STATUS LatestUnIndicateStatus;
361 /** when working in the passthru mode the driver puts the received packets to this array
362 * instead of passing them up immediately
363 * we are flushing the packets on ProtocolReceiveComplete or when the underlying miniport
364 * indicates NDIS_STATUS_RESOURCES or when this array is full */
365 PNDIS_PACKET aReceivedPackets[MAX_RECEIVE_PACKET_ARRAY_SIZE];
366 /** number of packets in the aReceivedPackets array*/
367 ULONG cReceivedPacketCount;
368 /** packet filter flags set by the upper protocols */
369 ULONG fUpperProtocolSetFilter;
370 /** packet filter flags set by the upper protocols */
371 ULONG fSetFilterBuffer;
372 /** packet filter flags set by us */
373 ULONG fOurSetFilter;
374#endif /* !VBOXNETADP */
375#endif /* !VBOX_NETFLT_ONDEMAND_BIND */
376
377#ifndef VBOXNETADP
378#if defined(DEBUG_NETFLT_LOOPBACK) || !defined(VBOX_LOOPBACK_USEFLAGS)
379 /** used for maintaining the pending send packets for handling packet loopback */
380 INTERLOCKED_SINGLE_LIST SendPacketQueue;
381#endif
382 /** used for serializing calls to the NdisRequest in the vboxNetFltWinSynchNdisRequest */
383 RTSEMFASTMUTEX hSynchRequestMutex;
384 /** event used to synchronize with the Ndis Request completion in the vboxNetFltWinSynchNdisRequest */
385 KEVENT hSynchCompletionEvent;
386 /** status of the Ndis Request initiated by the vboxNetFltWinSynchNdisRequest */
387 NDIS_STATUS volatile fSynchCompletionStatus;
388 /** pointer to the Ndis Request being executed by the vboxNetFltWinSynchNdisRequest */
389 PNDIS_REQUEST volatile pSynchRequest;
390 /** ndis packet pool used for sends */
391 NDIS_HANDLE hSendPacketPoolHandle;
392 /** ndis buffer pool used for sends */
393 NDIS_HANDLE hSendBufferPoolHandle;
394 /** open/close adapter status.
395 * Since ndis adapter open and close requests may complete assynchronously,
396 * we are using event mechanism to wait for open/close completion
397 * the status field is being set by the completion call-back */
398 NDIS_STATUS Status;
399 /** open/close adaptor completion event */
400 NDIS_EVENT hEvent;
401 /** medium we are attached to */
402 NDIS_MEDIUM Medium;
403// /** physical medium we are attached to */
404// NDIS_PHYSICAL_MEDIUM PhMedium;
405 /** True - When the miniport or protocol is transitioning from a D0 to Standby (>D0) State
406 * False - At all other times, - Flag is cleared after a transition to D0 */
407 BOOLEAN bStandingBy;
408#endif
409} ADAPT, *PADAPT;
410
411typedef struct _PACKET_QUEUE_WORKER
412{
413 /** this event is used to initiate a packet queue worker thread kill */
414 KEVENT KillEvent;
415 /** this event is used to notify a worker thread that the packets are added to the queue */
416 KEVENT NotifyEvent;
417 /** pointer to the packet queue worker thread object */
418 PKTHREAD pThread;
419 /** pointer to the SG used by the packet queue for IntNet receive notifications */
420 PINTNETSG pSG;
421 /** Packet queue */
422 INTERLOCKED_PACKET_QUEUE PacketQueue;
423 /** Packet info pool, i.e. the pool for the packet queue elements */
424 PACKET_INFO_POOL PacketInfoPool;
425} PACKET_QUEUE_WORKER, *PPACKET_QUEUE_WORKER;
426
427/** Protocol reserved part of a sent packet that is allocated by us. */
428typedef struct _SEND_RSVD
429{
430 /** original packet receiver from the upperlying protocol
431 * can be null if the packet was originated by intnet */
432 PNDIS_PACKET pOriginalPkt;
433 /** pointer to the buffer to be freed on send completion
434 * can be null if no buffer is to be freed */
435 PVOID pBufToFree;
436#if !defined(VBOX_LOOPBACK_USEFLAGS) || defined(DEBUG_NETFLT_PACKETS)
437 SINGLE_LIST_ENTRY ListEntry;
438 /* true if the packet is from IntNet */
439 bool bFromIntNet;
440#endif
441} SEND_RSVD, *PSEND_RSVD;
442
443/** represents the data stored in the protocol recerved field of ndis packet on NdisTransferData processing*/
444typedef struct _TRANSFERDATA_RSVD
445{
446 /** next packet in a list */
447 SINGLE_LIST_ENTRY ListEntry;
448 /* packet buffer start */
449 PNDIS_BUFFER pOriginalBuffer;
450} TRANSFERDATA_RSVD, *PTRANSFERDATA_RSVD;
451
452/** Miniport reserved part of a received packet that is allocated by
453 * us. Note that this should fit into the MiniportReserved space
454 * in an NDIS_PACKET. */
455typedef struct _RECV_RSVD
456{
457 /** original packet receiver from the underling miniport
458 * can be null if the packet was originated by intnet */
459 PNDIS_PACKET pOriginalPkt;
460 /** pointer to the buffer to be freed on receive completion
461 * can be null if no buffer is to be freed */
462 PVOID pBufToFree;
463} RECV_RSVD, *PRECV_RSVD;
464
465#ifndef VBOX_NETFLT_ONDEMAND_BIND
466
467C_ASSERT(sizeof(RECV_RSVD) <= sizeof(((PNDIS_PACKET)0)->MiniportReserved));
468C_ASSERT(sizeof(TRANSFERDATA_RSVD) <= PROTOCOL_RESERVED_SIZE_IN_PACKET);
469#endif
470
471C_ASSERT(sizeof(NDIS_DEVICE_POWER_STATE) == sizeof(uint32_t));
472C_ASSERT(sizeof(UINT) == sizeof(uint32_t));
473
474#define NDIS_FLAGS_SKIP_LOOPBACK_W2K 0x400
475
476#include "../VBoxNetFltInternal.h"
477#include "VBoxNetFlt-win.h"
478#ifndef VBOXNETADP
479#include "VBoxNetFltPt-win.h"
480#endif
481#ifndef VBOX_NETFLT_ONDEMAND_BIND
482# include "VBoxNetFltMp-win.h"
483#endif
484
485#ifdef DEBUG_NETFLT_NOASSERT
486# ifdef Assert
487# undef Assert
488# endif
489
490# define Assert(_expr) do {} while (0)
491#endif /* #ifdef DEBUG_NETFLT_NOASSERT */
492
493#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette