VirtualBox

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

Last change on this file since 23621 was 22599, checked in by vboxsync, 15 years ago

netflt-adp/win: additional bugfix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1/* $Id: VBoxNetFltCommon-win.h 22599 2009-08-31 12:10:12Z 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/* packet filter processing mode constants */
301#define VBOXNETFLT_PFP_NETFLT 1
302#define VBOXNETFLT_PFP_PASSTHRU 2
303
304/** represents filter driver device context*/
305typedef struct _ADAPT
306{
307#ifndef VBOXNETADP
308 /** handle the lower miniport */
309 NDIS_HANDLE hBindingHandle;
310 /** Protocol's Device state */
311 ADAPT_DEVICE PTState;
312#endif
313#ifndef VBOX_NETFLT_ONDEMAND_BIND
314 /** NDIS Handle to for miniport up-calls */
315 NDIS_HANDLE hMiniportHandle;
316 /** miniport device state */
317 ADAPT_DEVICE MPState;
318#ifdef DEBUG_NETFLT_LOOPBACK
319# error "implement (see comments in the sources below this #error:)"
320 /* @todo FIXME no need for the PPACKET_INFO mechanism here;
321 instead the the NDIS_PACKET.ProtocolReserved + INTERLOCKED_SINGLE_LIST mechanism \
322 similar to that used in TrasferData handling should be used;
323 */
324 /** Pending receive packet queue (i.e. packets that were indicated to the upperlying protocols, but not completed yet)*/
325// INTERLOCKED_PACKET_QUEUE RecvPacketQueue;
326#endif
327 /** ndis packet pool used for receives */
328 NDIS_HANDLE hRecvPacketPoolHandle;
329 /** ndis buffer pool used for receives */
330 NDIS_HANDLE hRecvBufferPoolHandle;
331#ifndef VBOXNETADP
332 /** This is used to wrap a request coming down to us.
333 * This exploits the fact that requests are serialized down to us.*/
334 NDIS_REQUEST Request;
335 /** Ndis Request Bytes needed */
336 PULONG BytesNeeded;
337 /** Ndis Request Bytes Read or Written */
338 PULONG BytesReadOrWritten;
339#else
340 volatile ULONG cTxSuccess;
341 volatile ULONG cRxSuccess;
342 volatile ULONG cTxError;
343 volatile ULONG cRxError;
344#endif
345 /** driver bind adapter state. */
346 VBOXADAPTSTATE enmState;
347#ifndef VBOXNETADP
348 /** true if we should indicate the receive complete used by the ProtocolReceeive mechanism */
349 bool bIndicateRcvComplete;
350
351 /** TRUE iff a request is pending at the miniport below */
352 bool bOutstandingRequests;
353 /** TRUE iff a request is queued at this IM miniport*/
354 bool bQueuedRequest;
355 /** @todo join all boolean states to one field treated as flags bitmap */
356 /** true iff we are processing Set packet filter OID */
357 uint8_t fProcessingPacketFilter;
358 /** true iff the upper protocol filter cache was initialized */
359 bool bUpperProtSetFilterInitialized;
360 /** trus if the adapter is closing */
361 bool bClosingAdapter;
362 /** Pending transfer data packet queue (i.e. packets that were indicated as pending on NdisTransferData call */
363 INTERLOCKED_SINGLE_LIST TransferDataList;
364 /* mac options initialized on OID_GEN_MAC_OPTIONS */
365 ULONG fMacOptions;
366 /** For initializing the miniport edge */
367 NDIS_STRING DeviceName;
368 /** For blocking UnbindAdapter while an IM Init is in progress.*/
369 NDIS_EVENT MiniportInitEvent;
370 /** The last indicated media status */
371 NDIS_STATUS LastIndicatedStatus;
372 /** The latest suppressed media status */
373 NDIS_STATUS LatestUnIndicateStatus;
374 /** when working in the passthru mode the driver puts the received packets to this array
375 * instead of passing them up immediately
376 * we are flushing the packets on ProtocolReceiveComplete or when the underlying miniport
377 * indicates NDIS_STATUS_RESOURCES or when this array is full */
378 PNDIS_PACKET aReceivedPackets[MAX_RECEIVE_PACKET_ARRAY_SIZE];
379 /** number of packets in the aReceivedPackets array*/
380 ULONG cReceivedPacketCount;
381 /** packet filter flags set by the upper protocols */
382 ULONG fUpperProtocolSetFilter;
383 /** packet filter flags set by the upper protocols */
384 ULONG fSetFilterBuffer;
385 /** packet filter flags set by us */
386 ULONG fOurSetFilter;
387#endif /* !VBOXNETADP */
388#endif /* !VBOX_NETFLT_ONDEMAND_BIND */
389
390#ifndef VBOXNETADP
391 /** used for serializing calls to the NdisRequest in the vboxNetFltWinSynchNdisRequest */
392 RTSEMFASTMUTEX hSynchRequestMutex;
393
394 /** event used to synchronize with the Ndis Request completion in the vboxNetFltWinSynchNdisRequest */
395 KEVENT hSynchCompletionEvent;
396 /** status of the Ndis Request initiated by the vboxNetFltWinSynchNdisRequest */
397 NDIS_STATUS volatile fSynchCompletionStatus;
398 /** pointer to the Ndis Request being executed by the vboxNetFltWinSynchNdisRequest */
399 PNDIS_REQUEST volatile pSynchRequest;
400#endif
401#ifdef DEBUG_NETFLT_LOOPBACK
402# error "implement (see comments in the sources below this #error:)"
403 /* @todo FIXME no need for the PPACKET_INFO mechanism here;
404 instead the the NDIS_PACKET.ProtocolReserved + INTERLOCKED_SINGLE_LIST mechanism \
405 similar to that used in TrasferData handling should be used;
406 */
407 /** Pending send packet queue (i.e. packets that were sent to the underlying miniport, but not completed yet)*/
408
409 INTERLOCKED_PACKET_QUEUE SendPacketQueue;
410 /** Packet info pool, i.e. the pool for the packet queue elements */
411#endif
412#ifndef VBOXNETADP
413 /** ndis packet pool used for sends */
414 NDIS_HANDLE hSendPacketPoolHandle;
415 /** ndis buffer pool used for sends */
416 NDIS_HANDLE hSendBufferPoolHandle;
417 /** open/close adapter status.
418 * Since ndis adapter open and close requests may complete assynchronously,
419 * we are using event mechanism to wait for open/close completion
420 * the status field is being set by the completion call-back */
421 NDIS_STATUS Status;
422 /** open/close adaptor completion event */
423 NDIS_EVENT hEvent;
424 /** medium we are attached to */
425 NDIS_MEDIUM Medium;
426// /** physical medium we are attached to */
427// NDIS_PHYSICAL_MEDIUM PhMedium;
428 /** True - When the miniport or protocol is transitioning from a D0 to Standby (>D0) State
429 * False - At all other times, - Flag is cleared after a transition to D0 */
430 BOOLEAN bStandingBy;
431#endif
432} ADAPT, *PADAPT;
433
434typedef struct _PACKET_QUEUE_WORKER
435{
436 /** this event is used to initiate a packet queue worker thread kill */
437 KEVENT KillEvent;
438 /** this event is used to notify a worker thread that the packets are added to the queue */
439 KEVENT NotifyEvent;
440 /** pointer to the packet queue worker thread object */
441 PKTHREAD pThread;
442 /** pointer to the SG used by the packet queue for IntNet receive notifications */
443 PINTNETSG pSG;
444 /** Packet queue */
445 INTERLOCKED_PACKET_QUEUE PacketQueue;
446 /** Packet info pool, i.e. the pool for the packet queue elements */
447 PACKET_INFO_POOL PacketInfoPool;
448} PACKET_QUEUE_WORKER, *PPACKET_QUEUE_WORKER;
449
450/** Protocol reserved part of a sent packet that is allocated by us. */
451typedef struct _SEND_RSVD
452{
453 /** original packet receiver from the upperlying protocol
454 * can be null if the packet was originated by intnet */
455 PNDIS_PACKET pOriginalPkt;
456
457 /** pointer to the buffer to be freed on send completion
458 * can be null if no buffer is to be freed */
459 PVOID pBufToFree;
460} SEND_RSVD, *PSEND_RSVD;
461
462/** represents the data stored in the protocol recerved field of ndis packet on NdisTransferData processing*/
463typedef struct _TRANSFERDATA_RSVD
464{
465 /** next packet in a list */
466 SINGLE_LIST_ENTRY ListEntry;
467 /* packet buffer start */
468 PNDIS_BUFFER pOriginalBuffer;
469} TRANSFERDATA_RSVD, *PTRANSFERDATA_RSVD;
470
471/** Miniport reserved part of a received packet that is allocated by
472 * us. Note that this should fit into the MiniportReserved space
473 * in an NDIS_PACKET. */
474typedef struct _RECV_RSVD
475{
476 /** original packet receiver from the underling miniport
477 * can be null if the packet was originated by intnet */
478 PNDIS_PACKET pOriginalPkt;
479 /** pointer to the buffer to be freed on receive completion
480 * can be null if no buffer is to be freed */
481 PVOID pBufToFree;
482} RECV_RSVD, *PRECV_RSVD;
483
484typedef struct _PT_RSVD
485{
486 union
487 {
488 RECV_RSVD RecvRsvd;
489 TRANSFERDATA_RSVD TransferDataRsvd;
490 } u;
491} PT_RSVD, *PPT_RSVD;
492
493
494#ifndef VBOX_NETFLT_ONDEMAND_BIND
495
496C_ASSERT(sizeof(RECV_RSVD) <= sizeof(((PNDIS_PACKET)0)->MiniportReserved));
497#endif
498
499C_ASSERT(sizeof(NDIS_DEVICE_POWER_STATE) == sizeof(uint32_t));
500C_ASSERT(sizeof(UINT) == sizeof(uint32_t));
501
502#define NDIS_FLAGS_SKIP_LOOPBACK_W2K 0x400
503
504#include "../VBoxNetFltInternal.h"
505#include "VBoxNetFlt-win.h"
506#ifndef VBOXNETADP
507#include "VBoxNetFltPt-win.h"
508#endif
509#ifndef VBOX_NETFLT_ONDEMAND_BIND
510# include "VBoxNetFltMp-win.h"
511#endif
512
513#ifdef DEBUG_NETFLT_NOASSERT
514# ifdef Assert
515# undef Assert
516# endif
517
518# define Assert(_expr) do {} while (0)
519#endif /* #ifdef DEBUG_NETFLT_NOASSERT */
520
521#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