VirtualBox

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

Last change on this file since 25391 was 24217, checked in by vboxsync, 15 years ago

netflt/win: fix Protocol/Miniport reserved field usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/* $Id: VBoxNetFltCommon-win.h 24217 2009-10-30 20:24:36Z 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#if defined(DEBUG_NETFLT_PACKETS) || !defined(VBOX_LOOPBACK_USEFLAGS)
156# define VBOXNETFLT_PACKETMATCH_LENGTH (ETH_HEADER_SIZE + 2)
157#endif
158
159#ifdef VBOXNETADP
160#define VBOXNETADP_HEADER_SIZE 14
161#define VBOXNETADP_MAX_DATA_SIZE 1500
162#define VBOXNETADP_MAX_PACKET_SIZE VBOXNETADP_HEADER_SIZE + VBOXNETADP_MAX_DATA_SIZE
163#define VBOXNETADP_MIN_PACKET_SIZE 60
164#define VBOXNETADP_LINK_SPEED 1000000 //The unit of measurement is 100 bps, 100Mbps
165#define VBOXNETADP_MAX_LOOKAHEAD_SIZE VBOXNETADP_MAX_DATA_SIZE
166#define VBOXNETADP_VENDOR_ID 0x080027
167#define VBOXNETADP_VENDOR_DRIVER_VERSION 0x00010000
168#define VBOXNETADP_VENDOR_DESC "Sun"
169#define VBOXNETADP_MAX_MCAST_LIST 32
170#define VBOXNETADP_ETH_ADDRESS_LENGTH 6
171
172//#define VBOXNETADP_REPORT_DISCONNECTED
173#endif
174/* type defs */
175
176/** Flag specifying that the type of enqueued packet
177 * if set the info contains the PINTNETSG packet
178 * if clear the packet info contains the PNDIS_PACKET packet
179 * Typically the packet queue we are maintaining contains PNDIS_PACKETs only,
180 * however in case the underlying miniport indicates a packet with the NDIS_STATUS_RESOURCES status
181 * we MUST return the packet back to the miniport immediately
182 * this is why we are creating the INTNETSG, copying the ndis packet info there and enqueueing it */
183#define PACKET_SG 0x00000001
184
185/** the flag specifying that the packet source
186 * if set the packet comes from the host (upperlying protocol)
187 * if clear the packet comes from the wire (underlying miniport) */
188#define PACKET_SRC_HOST 0x00000002
189
190/** flag specifying the packet was originated by our driver
191 * i.e. we could use it on our needs and should not return it
192 * we are enqueueing "our" packets on ProtocolReceive call-back when
193 * Ndis does not give us a receive acket (the driver below us has called NdisM..IndicateReceive)
194 * this is supported for Ndis Packet only */
195#define PACKET_MINE 0x00000004
196
197/** flag passed to vboxNetFltWinQuEnqueuePacket specifying that the packet should be copied
198 * this is supported for Ndis Packet only */
199#define PACKET_COPY 0x00000008
200
201/** packet queue element containing the packet info */
202typedef struct _PACKET_INFO
203{
204 /** list entry used for enqueueing the info */
205 LIST_ENTRY ListEntry;
206 /** pointer to the pool containing this packet info */
207 struct _PACKET_INFO_POOL * pPool;
208 /** flags describing the referenced packet. Contains PACKET_xxx flags (i.e. PACKET_SG, PACKET_SRC_HOST) */
209 uint32_t fFlags;
210 /** pointer to the packet this info represents */
211 PVOID pPacket;
212}PACKET_INFO, *PPACKET_INFO;
213
214/* paranoid check to make sure the elements in the packet info array are properly aligned */
215C_ASSERT((sizeof(PACKET_INFO) & (sizeof(PVOID) - 1)) == 0);
216
217/** represents the packet queue */
218typedef LIST_ENTRY PACKET_QUEUE, *PPACKET_QUEUE;
219
220/*
221 * we are using non-interlocked versions of LIST_ENTRY-related operations macros and synchronize
222 * access to the queue and its elements by aquiring/releasing a spinlock using Ndis[Acquire,Release]Spinlock
223 *
224 * we are NOT using interlocked versions of insert/remove head/tail list functions because we need to iterate though
225 * the queue elements as well as remove elements from the midle of the queue
226 *
227 * * @todo: it seems that we can switch to using interlocked versions of list-entry functions
228 * since we have removed all functionality (mentioned above, i.e. queue elements iteration, etc.) that might prevent us from doing this
229 */
230typedef struct _INTERLOCKED_PACKET_QUEUE
231{
232 /** queue */
233 PACKET_QUEUE Queue;
234 /** queue lock */
235 NDIS_SPIN_LOCK Lock;
236}INTERLOCKED_PACKET_QUEUE, *PINTERLOCKED_PACKET_QUEUE;
237
238typedef struct _SINGLE_LIST
239{
240 /** queue */
241 SINGLE_LIST_ENTRY Head;
242 /** pointer to the list tail. used to enqueue elements to the tail of the list */
243 PSINGLE_LIST_ENTRY pTail;
244} SINGLE_LIST, *PSINGLE_LIST;
245
246typedef struct _INTERLOCKED_SINGLE_LIST
247{
248 /** queue */
249 SINGLE_LIST List;
250 /** queue lock */
251 NDIS_SPIN_LOCK Lock;
252} INTERLOCKED_SINGLE_LIST, *PINTERLOCKED_SINGLE_LIST;
253
254/** packet info pool contains free packet info elements to be used for the packet queue
255 * we are using the pool mechanism to allocate packet queue elements
256 * the pool mechanism is pretty simple now, we are allocating a bunch of memory
257 * for maintaining PACKET_INFO_POOL_SIZE queue elements and just returning null when the pool is exhausted
258 * This mechanism seems to be enough for now since we are using PACKET_INFO_POOL_SIZE = 0xffff which is
259 * the maximum size of packets the ndis packet pool supports */
260typedef struct _PACKET_INFO_POOL
261{
262 /** free packet info queue */
263 INTERLOCKED_PACKET_QUEUE Queue;
264 /** memory bugger used by the pool */
265 PVOID pBuffer;
266}PACKET_INFO_POOL, *PPACKET_INFO_POOL;
267
268typedef enum VBOXNETDEVOPSTATE
269{
270 kVBoxNetDevOpState_InvalidValue = 0,
271 kVBoxNetDevOpState_Initializing,
272 kVBoxNetDevOpState_Initialized,
273 kVBoxNetDevOpState_Deinitializing,
274 kVBoxNetDevOpState_Deinitialized,
275
276} VBOXNETDEVOPSTATE;
277
278typedef enum VBOXADAPTSTATE
279{
280 /** The usual invalid state. */
281 kVBoxAdaptState_Invalid = 0,
282 /** Initialization. */
283 kVBoxAdaptState_Connecting,
284 /** Connected fuly functional state */
285 kVBoxAdaptState_Connected,
286 /** Disconnecting */
287 kVBoxAdaptState_Disconnecting,
288 /** Disconnected */
289 kVBoxAdaptState_Disconnected,
290} VBOXADAPTSTATE;
291
292/** structure used to maintain the state and reference count of the miniport and protocol */
293typedef struct _ADAPT_DEVICE
294{
295 /** initialize state */
296 VBOXNETDEVOPSTATE OpState;
297 /** ndis power state */
298 NDIS_DEVICE_POWER_STATE PowerState;
299 /** reference count */
300 uint32_t cReferences;
301/* NDIS_HANDLE hHandle; */
302} ADAPT_DEVICE, *PADAPT_DEVICE;
303
304/* packet filter processing mode constants */
305#define VBOXNETFLT_PFP_NETFLT 1
306#define VBOXNETFLT_PFP_PASSTHRU 2
307
308/** represents filter driver device context*/
309typedef struct _ADAPT
310{
311#ifndef VBOXNETADP
312 /** handle the lower miniport */
313 NDIS_HANDLE hBindingHandle;
314 /** Protocol's Device state */
315 ADAPT_DEVICE PTState;
316#endif
317#ifndef VBOX_NETFLT_ONDEMAND_BIND
318 /** NDIS Handle to for miniport up-calls */
319 NDIS_HANDLE hMiniportHandle;
320 /** miniport device state */
321 ADAPT_DEVICE MPState;
322 /** ndis packet pool used for receives */
323 NDIS_HANDLE hRecvPacketPoolHandle;
324 /** ndis buffer pool used for receives */
325 NDIS_HANDLE hRecvBufferPoolHandle;
326#ifndef VBOXNETADP
327 /** This is used to wrap a request coming down to us.
328 * This exploits the fact that requests are serialized down to us.*/
329 NDIS_REQUEST Request;
330 /** Ndis Request Bytes needed */
331 PULONG BytesNeeded;
332 /** Ndis Request Bytes Read or Written */
333 PULONG BytesReadOrWritten;
334#else
335 volatile ULONG cTxSuccess;
336 volatile ULONG cRxSuccess;
337 volatile ULONG cTxError;
338 volatile ULONG cRxError;
339#endif
340 /** driver bind adapter state. */
341 VBOXADAPTSTATE enmState;
342#ifndef VBOXNETADP
343 /** true if we should indicate the receive complete used by the ProtocolReceeive mechanism */
344 bool bIndicateRcvComplete;
345
346 /** TRUE iff a request is pending at the miniport below */
347 bool bOutstandingRequests;
348 /** TRUE iff a request is queued at this IM miniport*/
349 bool bQueuedRequest;
350 /** @todo join all boolean states to one field treated as flags bitmap */
351 /** true iff we are processing Set packet filter OID */
352 uint8_t fProcessingPacketFilter;
353 /** true iff the upper protocol filter cache was initialized */
354 bool bUpperProtSetFilterInitialized;
355 /** trus if the adapter is closing */
356 bool bClosingAdapter;
357 /** Pending transfer data packet queue (i.e. packets that were indicated as pending on NdisTransferData call */
358 INTERLOCKED_SINGLE_LIST TransferDataList;
359 /* mac options initialized on OID_GEN_MAC_OPTIONS */
360 ULONG fMacOptions;
361 /** For initializing the miniport edge */
362 NDIS_STRING DeviceName;
363 /** For blocking UnbindAdapter while an IM Init is in progress.*/
364 NDIS_EVENT MiniportInitEvent;
365 /** The last indicated media status */
366 NDIS_STATUS LastIndicatedStatus;
367 /** The latest suppressed media status */
368 NDIS_STATUS LatestUnIndicateStatus;
369 /** when working in the passthru mode the driver puts the received packets to this array
370 * instead of passing them up immediately
371 * we are flushing the packets on ProtocolReceiveComplete or when the underlying miniport
372 * indicates NDIS_STATUS_RESOURCES or when this array is full */
373 PNDIS_PACKET aReceivedPackets[MAX_RECEIVE_PACKET_ARRAY_SIZE];
374 /** number of packets in the aReceivedPackets array*/
375 ULONG cReceivedPacketCount;
376 /** packet filter flags set by the upper protocols */
377 ULONG fUpperProtocolSetFilter;
378 /** packet filter flags set by the upper protocols */
379 ULONG fSetFilterBuffer;
380 /** packet filter flags set by us */
381 ULONG fOurSetFilter;
382#endif /* !VBOXNETADP */
383#endif /* !VBOX_NETFLT_ONDEMAND_BIND */
384
385#ifndef VBOXNETADP
386#if defined(DEBUG_NETFLT_LOOPBACK) || !defined(VBOX_LOOPBACK_USEFLAGS)
387 /** used for maintaining the pending send packets for handling packet loopback */
388 INTERLOCKED_SINGLE_LIST SendPacketQueue;
389#endif
390 /** used for serializing calls to the NdisRequest in the vboxNetFltWinSynchNdisRequest */
391 RTSEMFASTMUTEX hSynchRequestMutex;
392 /** event used to synchronize with the Ndis Request completion in the vboxNetFltWinSynchNdisRequest */
393 KEVENT hSynchCompletionEvent;
394 /** status of the Ndis Request initiated by the vboxNetFltWinSynchNdisRequest */
395 NDIS_STATUS volatile fSynchCompletionStatus;
396 /** pointer to the Ndis Request being executed by the vboxNetFltWinSynchNdisRequest */
397 PNDIS_REQUEST volatile pSynchRequest;
398 /** ndis packet pool used for sends */
399 NDIS_HANDLE hSendPacketPoolHandle;
400 /** ndis buffer pool used for sends */
401 NDIS_HANDLE hSendBufferPoolHandle;
402 /** open/close adapter status.
403 * Since ndis adapter open and close requests may complete assynchronously,
404 * we are using event mechanism to wait for open/close completion
405 * the status field is being set by the completion call-back */
406 NDIS_STATUS Status;
407 /** open/close adaptor completion event */
408 NDIS_EVENT hEvent;
409 /** medium we are attached to */
410 NDIS_MEDIUM Medium;
411// /** physical medium we are attached to */
412// NDIS_PHYSICAL_MEDIUM PhMedium;
413 /** True - When the miniport or protocol is transitioning from a D0 to Standby (>D0) State
414 * False - At all other times, - Flag is cleared after a transition to D0 */
415 BOOLEAN bStandingBy;
416#endif
417} ADAPT, *PADAPT;
418
419typedef struct _PACKET_QUEUE_WORKER
420{
421 /** this event is used to initiate a packet queue worker thread kill */
422 KEVENT KillEvent;
423 /** this event is used to notify a worker thread that the packets are added to the queue */
424 KEVENT NotifyEvent;
425 /** pointer to the packet queue worker thread object */
426 PKTHREAD pThread;
427 /** pointer to the SG used by the packet queue for IntNet receive notifications */
428 PINTNETSG pSG;
429 /** Packet queue */
430 INTERLOCKED_PACKET_QUEUE PacketQueue;
431 /** Packet info pool, i.e. the pool for the packet queue elements */
432 PACKET_INFO_POOL PacketInfoPool;
433} PACKET_QUEUE_WORKER, *PPACKET_QUEUE_WORKER;
434
435/** Protocol reserved part of a sent packet that is allocated by us. */
436typedef struct _SEND_RSVD
437{
438 /** original packet receiver from the upperlying protocol
439 * can be null if the packet was originated by intnet */
440 PNDIS_PACKET pOriginalPkt;
441 /** pointer to the buffer to be freed on send completion
442 * can be null if no buffer is to be freed */
443 PVOID pBufToFree;
444#if !defined(VBOX_LOOPBACK_USEFLAGS) || defined(DEBUG_NETFLT_PACKETS)
445 SINGLE_LIST_ENTRY ListEntry;
446 /* true if the packet is from IntNet */
447 bool bFromIntNet;
448#endif
449} SEND_RSVD, *PSEND_RSVD;
450
451/** represents the data stored in the protocol recerved field of ndis packet on NdisTransferData processing*/
452typedef struct _TRANSFERDATA_RSVD
453{
454 /** next packet in a list */
455 SINGLE_LIST_ENTRY ListEntry;
456 /* packet buffer start */
457 PNDIS_BUFFER pOriginalBuffer;
458} TRANSFERDATA_RSVD, *PTRANSFERDATA_RSVD;
459
460/** Miniport reserved part of a received packet that is allocated by
461 * us. Note that this should fit into the MiniportReserved space
462 * in an NDIS_PACKET. */
463typedef struct _RECV_RSVD
464{
465 /** original packet receiver from the underling miniport
466 * can be null if the packet was originated by intnet */
467 PNDIS_PACKET pOriginalPkt;
468 /** pointer to the buffer to be freed on receive completion
469 * can be null if no buffer is to be freed */
470 PVOID pBufToFree;
471} RECV_RSVD, *PRECV_RSVD;
472
473#ifndef VBOX_NETFLT_ONDEMAND_BIND
474
475C_ASSERT(sizeof(RECV_RSVD) <= sizeof(((PNDIS_PACKET)0)->MiniportReserved));
476C_ASSERT(sizeof(TRANSFERDATA_RSVD) <= PROTOCOL_RESERVED_SIZE_IN_PACKET);
477#endif
478
479C_ASSERT(sizeof(NDIS_DEVICE_POWER_STATE) == sizeof(uint32_t));
480C_ASSERT(sizeof(UINT) == sizeof(uint32_t));
481
482#ifdef VBOX_LOOPBACK_USEFLAGS
483#define NDIS_FLAGS_SKIP_LOOPBACK_W2K 0x400
484#endif
485
486#include "../VBoxNetFltInternal.h"
487#include "VBoxNetFlt-win.h"
488#ifndef VBOXNETADP
489#include "VBoxNetFltPt-win.h"
490#endif
491#ifndef VBOX_NETFLT_ONDEMAND_BIND
492# include "VBoxNetFltMp-win.h"
493#endif
494
495#ifdef DEBUG_NETFLT_NOASSERT
496# ifdef Assert
497# undef Assert
498# endif
499
500# define Assert(_expr) do {} while (0)
501#endif /* #ifdef DEBUG_NETFLT_NOASSERT */
502
503#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