VirtualBox

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

Last change on this file since 27436 was 25582, checked in by vboxsync, 15 years ago

NetFlt|Adp/win: some code cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 KB
Line 
1/* $Id: VBoxNetFltCommon-win.h 25582 2009-12-25 10:25:46Z 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 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21/*
22 * Based in part on Microsoft DDK sample code for Ndis Intermediate Miniport passthru driver sample.
23 * Copyright (c) 1993-1999, Microsoft Corporation
24 */
25
26#ifndef ___VBoxNetFltCommon_win_h___
27#define ___VBoxNetFltCommon_win_h___
28
29//#define NTSTRSAFE_LIB
30
31#ifdef DEBUG
32//# define DEBUG_NETFLT_PACKETS
33# ifndef DEBUG_misha
34# define DEBUG_NETFLT_NOASSERT
35# endif
36/* # define DEBUG_NETFLT_LOOPBACK */
37
38/* receive logic has several branches */
39/* the DEBUG_NETFLT_RECV* macros used to debug the ProtocolReceive callback
40 * which is typically not used in case the underlying miniport indicates the packets with NdisMIndicateReceivePacket
41 * the best way to debug the ProtocolReceive (which in turn has several branches) is to enable the DEBUG_NETFLT_RECV
42 * one by one in the below order, i.e.
43 * first DEBUG_NETFLT_RECV
44 * then DEBUG_NETFLT_RECV + DEBUG_NETFLT_RECV_NOPACKET */
45//# define DEBUG_NETFLT_RECV
46//# define DEBUG_NETFLT_RECV_NOPACKET
47//# define DEBUG_NETFLT_RECV_TRANSFERDATA
48#endif
49
50#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
51
52#include <VBox/intnet.h>
53#include <VBox/log.h>
54#include <VBox/err.h>
55#include <VBox/version.h>
56#include <iprt/initterm.h>
57#include <iprt/assert.h>
58#include <iprt/spinlock.h>
59#include <iprt/semaphore.h>
60#include <iprt/process.h>
61#include <iprt/alloc.h>
62#include <iprt/alloca.h>
63#include <iprt/time.h>
64#include <iprt/net.h>
65
66RT_C_DECLS_BEGIN
67#include <ndis.h>
68RT_C_DECLS_END
69
70
71
72#define VBOXNETFLT_OS_SPECFIC 1
73
74#ifdef VBOX_NETFLT_ONDEMAND_BIND
75# define VBOXNETFLT_PROTOCOL_NAME L"VBoxNetFltPt"
76#else
77# ifndef VBOXNETADP
78# define VBOXNETFLT_PROTOCOL_NAME L"VBoxNetFlt"
79
80/** this is to support ioctl interface */
81# define LINKNAME_STRING L"\\DosDevices\\Global\\VBoxNetFlt"
82# define NTDEVICE_STRING L"\\Device\\VBoxNetFlt"
83# else
84# define LINKNAME_STRING L"\\DosDevices\\Global\\VBoxNetAdp"
85# define NTDEVICE_STRING L"\\Device\\VBoxNetAdp"
86# endif
87//# define VBOXNETFLT_WIN_IOCTL_INIT CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_NEITHER, FILE_WRITE_ACCESS)
88//# define VBOXNETFLT_WIN_IOCTL_FINI CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_NEITHER, FILE_WRITE_ACCESS)
89#endif
90
91/** version
92 * 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 */
93#ifdef NDIS51_MINIPORT
94# define VBOXNETFLT_MAJOR_NDIS_VERSION 5
95# define VBOXNETFLT_MINOR_NDIS_VERSION 1
96#else
97# define VBOXNETFLT_MAJOR_NDIS_VERSION 5
98# define VBOXNETFLT_MINOR_NDIS_VERSION 0
99#endif
100
101#ifdef NDIS51
102# define VBOXNETFLT_PROT_MAJOR_NDIS_VERSION 5
103# define VBOXNETFLT_PROT_MINOR_NDIS_VERSION 0
104#else
105# define VBOXNETFLT_PROT_MAJOR_NDIS_VERSION 5
106# define VBOXNETFLT_PROT_MINOR_NDIS_VERSION 0
107#endif
108
109/** advance declaration */
110typedef struct _ADAPT ADAPT, *PADAPT;
111
112typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
113
114/** configuration */
115
116/** received packets queue size. the queue is used when the driver is working in a pass-thru mode */
117#define MAX_RECEIVE_PACKET_ARRAY_SIZE 40
118
119/** Ndis Packet pool settings
120 * these are applied to both receive and send packet pools */
121#define MAX_PACKET_POOL_SIZE 0x0000FFFF
122#define MIN_PACKET_POOL_SIZE 0x000000FF
123
124/** packet queue size used when the driver is working in the "active" mode */
125#define PACKET_INFO_POOL_SIZE 0x0000FFFF
126
127#ifndef VBOXNETADP
128/** memory tag used for memory allocations
129 * (VBNF stands for VBox NetFlt) */
130# define MEM_TAG 'FNBV'
131#else
132/** memory tag used for memory allocations
133 * (VBNA stands for VBox NetAdp) */
134# define MEM_TAG 'ANBV'
135#endif
136
137/** receive and transmit Ndis buffer pool size */
138#define TX_BUFFER_POOL_SIZE 128
139#define RX_BUFFER_POOL_SIZE 128
140
141#define ETH_HEADER_SIZE 14
142
143#define PACKET_QUEUE_SG_SEGS_ALLOC 32
144
145#define VBOX_NETFLT_PACKET_HEADER_MATCH_SIZE 24
146
147#if defined(DEBUG_NETFLT_PACKETS) || !defined(VBOX_LOOPBACK_USEFLAGS)
148# define VBOXNETFLT_PACKETMATCH_LENGTH (ETH_HEADER_SIZE + 2)
149#endif
150
151#ifdef VBOXNETADP
152#define VBOXNETADP_HEADER_SIZE 14
153#define VBOXNETADP_MAX_DATA_SIZE 1500
154#define VBOXNETADP_MAX_PACKET_SIZE VBOXNETADP_HEADER_SIZE + VBOXNETADP_MAX_DATA_SIZE
155#define VBOXNETADP_MIN_PACKET_SIZE 60
156#define VBOXNETADP_LINK_SPEED 1000000 //The unit of measurement is 100 bps, 100Mbps
157#define VBOXNETADP_MAX_LOOKAHEAD_SIZE VBOXNETADP_MAX_DATA_SIZE
158#define VBOXNETADP_VENDOR_ID 0x080027
159#define VBOXNETADP_VENDOR_DRIVER_VERSION 0x00010000
160#define VBOXNETADP_VENDOR_DESC "Sun"
161#define VBOXNETADP_MAX_MCAST_LIST 32
162#define VBOXNETADP_ETH_ADDRESS_LENGTH 6
163
164//#define VBOXNETADP_REPORT_DISCONNECTED
165#endif
166/* type defs */
167
168/** Flag specifying that the type of enqueued packet
169 * if set the info contains the PINTNETSG packet
170 * if clear the packet info contains the PNDIS_PACKET packet
171 * Typically the packet queue we are maintaining contains PNDIS_PACKETs only,
172 * however in case the underlying miniport indicates a packet with the NDIS_STATUS_RESOURCES status
173 * we MUST return the packet back to the miniport immediately
174 * this is why we are creating the INTNETSG, copying the ndis packet info there and enqueueing it */
175#define PACKET_SG 0x00000001
176
177/** the flag specifying that the packet source
178 * if set the packet comes from the host (upperlying protocol)
179 * if clear the packet comes from the wire (underlying miniport) */
180#define PACKET_SRC_HOST 0x00000002
181
182/** flag specifying the packet was originated by our driver
183 * i.e. we could use it on our needs and should not return it
184 * we are enqueueing "our" packets on ProtocolReceive call-back when
185 * Ndis does not give us a receive acket (the driver below us has called NdisM..IndicateReceive)
186 * this is supported for Ndis Packet only */
187#define PACKET_MINE 0x00000004
188
189/** flag passed to vboxNetFltWinQuEnqueuePacket specifying that the packet should be copied
190 * this is supported for Ndis Packet only */
191#define PACKET_COPY 0x00000008
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#ifdef VBOX_LOOPBACK_USEFLAGS
475#define NDIS_FLAGS_SKIP_LOOPBACK_W2K 0x400
476#endif
477
478#include "../VBoxNetFltInternal.h"
479#include "VBoxNetFlt-win.h"
480#ifndef VBOXNETADP
481#include "VBoxNetFltPt-win.h"
482#endif
483#ifndef VBOX_NETFLT_ONDEMAND_BIND
484# include "VBoxNetFltMp-win.h"
485#endif
486
487#ifdef DEBUG_NETFLT_NOASSERT
488# ifdef Assert
489# undef Assert
490# endif
491
492# define Assert(_expr) do {} while (0)
493#endif /* #ifdef DEBUG_NETFLT_NOASSERT */
494
495#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