VirtualBox

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

Last change on this file since 36100 was 34109, checked in by vboxsync, 14 years ago

NetFlt/win: comments & assertions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 KB
Line 
1/* $Id: VBoxNetFltCommon-win.h 34109 2010-11-16 12:02:12Z vboxsync $ */
2/** @file
3 * VBoxNetFltCommon.h - Network Filter Driver (Host), Windows Specific Code. Common header 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 packet (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 acquiring/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 ProtocolReceive mechanism.
336 * We need to indicate it only with the ProtocolReceive + NdisMEthIndicateReceive path.
337 * There is no guarantee in the docs that the ProtocolReceive & ProtocolReceiveComplete
338 * for one transfer are called on one same CPU, however this is how the latest passthru
339 * sample handles this
340 * Note: we're using KeGetCurrentProcessorNumber, which is not entirely correct in case
341 * we're running on 64bit win7+, which can handle > 64 CPUs, however since KeGetCurrentProcessorNumber
342 * always returns the number < than the number of CPUs in the first group, we're guaranteed to have CPU index < 64
343 * @todo: use KeGetCurrentProcessorNumberEx for Win7+ 64 and dynamically extended array */
344 bool abIndicateRcvComplete[64];
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 asynchronously,
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 reserved 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#define NDIS_FLAGS_SKIP_LOOPBACK_W2K 0x400
483
484#include "../VBoxNetFltInternal.h"
485#include "VBoxNetFlt-win.h"
486#ifndef VBOXNETADP
487#include "VBoxNetFltPt-win.h"
488#endif
489#ifndef VBOX_NETFLT_ONDEMAND_BIND
490# include "VBoxNetFltMp-win.h"
491#endif
492
493#ifdef DEBUG_NETFLT_NOASSERT
494# ifdef Assert
495# undef Assert
496# endif
497
498# define Assert(_expr) do {} while (0)
499#endif /* #ifdef DEBUG_NETFLT_NOASSERT */
500
501#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