VirtualBox

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

Last change on this file since 21559 was 21343, checked in by vboxsync, 16 years ago

export vboxnetflt for Windows to OSE

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