1
/* $Id: VBoxNetFltInternal.h $ */
3
* VBoxNetFlt - Network Filter Driver (Host), Internal Header.
7
* Copyright (C) 2008 Oracle Corporation
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.
18
#ifndef ___VBoxNetFltInternal_h___
19
#define ___VBoxNetFltInternal_h___
22
#include <VBox/intnet.h>
23
#include <iprt/semaphore.h>
24
#include <iprt/assert.h>
29
/** Pointer to the globals. */
30
typedef struct VBOXNETFLTGLOBALS *PVBOXNETFLTGLOBALS;
34
* The state of a filter driver instance.
36
* The state machine differs a bit between the platforms because of
37
* the way we hook into the stack. On some hosts we can dynamically
38
* attach when required (on CreateInstance) and on others we will
39
* have to connect when the network stack is bound up. These modes
40
* are called static and dynamic config and governed at compile time
41
* by the VBOXNETFLT_STATIC_CONFIG define.
43
* See sec_netflt_msc for more details on locking and synchronization.
45
typedef enum VBOXNETFTLINSSTATE
47
/** The usual invalid state. */
48
kVBoxNetFltInsState_Invalid = 0,
50
* We've reserved the interface name but need to attach to the actual
51
* network interface outside the lock to avoid deadlocks.
52
* In the dynamic case this happens during a Create(Instance) call.
53
* In the static case it happens during driver initialization. */
54
kVBoxNetFltInsState_Initializing,
55
#ifdef VBOXNETFLT_STATIC_CONFIG
56
/** Unconnected, not hooked up to a switch (static only).
57
* The filter driver instance has been instantiated and hooked up,
58
* waiting to be connected to an internal network. */
59
kVBoxNetFltInsState_Unconnected,
61
/** Connected to an internal network. */
62
kVBoxNetFltInsState_Connected,
63
/** Disconnecting from the internal network and possibly the host network interface.
64
* Partly for reasons of deadlock avoidance again. */
65
kVBoxNetFltInsState_Disconnecting,
66
/** The instance has been disconnected from both the host and the internal network. */
67
kVBoxNetFltInsState_Destroyed,
69
/** The habitual 32-bit enum hack. */
70
kVBoxNetFltInsState_32BitHack = 0x7fffffff
75
* The per-instance data of the VBox filter driver.
77
* This is data associated with a network interface / NIC / wossname which
78
* the filter driver has been or may be attached to. When possible it is
79
* attached dynamically, but this may not be possible on all OSes so we have
80
* to be flexible about things.
82
* A network interface / NIC / wossname can only have one filter driver
83
* instance attached to it. So, attempts at connecting an internal network
84
* to an interface that's already in use (connected to another internal network)
85
* will result in a VERR_SHARING_VIOLATION.
87
* Only one internal network can connect to a filter driver instance.
89
typedef struct VBOXNETFLTINS
91
/** Pointer to the next interface in the list. (VBOXNETFLTGLOBAL::pInstanceHead) */
92
struct VBOXNETFLTINS *pNext;
94
* This is what the internal network plugs into. */
95
INTNETTRUNKIFPORT MyPort;
96
/** The RJ-45 port on the INTNET "switch".
97
* This is what we're connected to. */
98
PINTNETTRUNKSWPORT pSwitchPort;
99
/** Pointer to the globals. */
100
PVBOXNETFLTGLOBALS pGlobals;
102
/** The spinlock protecting the state variables and host interface handle. */
103
RTSPINLOCK hSpinlock;
104
/** The current interface state. */
105
VBOXNETFTLINSSTATE volatile enmState;
106
/** The trunk state. */
107
INTNETTRUNKIFSTATE volatile enmTrunkState;
108
bool volatile fActive;
109
/** Disconnected from the host network interface. */
110
bool volatile fDisconnectedFromHost;
111
/** Rediscovery is pending.
112
* cBusy will never reach zero during rediscovery, so which
113
* takes care of serializing rediscovery and disconnecting. */
114
bool volatile fRediscoveryPending;
115
/** Whether we should not attempt to set promiscuous mode at all. */
116
bool fDisablePromiscuous;
117
#if (ARCH_BITS == 32) && defined(__GNUC__)
119
uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
122
/** The timestamp of the last rediscovery. */
123
uint64_t volatile NanoTSLastRediscovery;
124
/** Reference count. */
125
uint32_t volatile cRefs;
127
* This counts the number of current callers and pending packet. */
128
uint32_t volatile cBusy;
129
/** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
130
RTSEMEVENT hEventIdle;
132
/** @todo move MacAddr out of this structure! */
135
#ifdef VBOXNETFLT_OS_SPECFIC
138
# if defined(RT_OS_DARWIN)
139
/** @name Darwin instance data.
141
/** Pointer to the darwin network interface we're attached to.
142
* This is treated as highly volatile and should only be read and retained
143
* while owning hSpinlock. Releasing references to this should not be done
144
* while owning it though as we might end up destroying it in some paths. */
145
ifnet_t volatile pIfNet;
146
/** The interface filter handle.
147
* Same access rules as with pIfNet. */
148
interface_filter_t volatile pIfFilter;
149
/** Whether we've need to set promiscuous mode when the interface comes up. */
150
bool volatile fNeedSetPromiscuous;
151
/** Whether we've successfully put the interface into to promiscuous mode.
152
* This is for dealing with the ENETDOWN case. */
153
bool volatile fSetPromiscuous;
154
/** The MAC address of the interface. */
157
# elif defined(RT_OS_LINUX)
158
/** @name Linux instance data
160
/** Pointer to the device. */
161
struct net_device * volatile pDev;
162
/** Whether we've successfully put the interface into to promiscuous mode.
163
* This is for dealing with the ENETDOWN case. */
164
bool volatile fPromiscuousSet;
165
/** Whether device exists and physically attached. */
166
bool volatile fRegistered;
167
/** The MAC address of the interface. */
169
struct notifier_block Notifier;
170
struct packet_type PacketType;
171
# ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
172
struct sk_buff_head XmitQueue;
173
struct work_struct XmitTask;
176
# elif defined(RT_OS_SOLARIS)
177
/** @name Solaris instance data.
179
# ifdef VBOX_WITH_NETFLT_CROSSBOW
180
/** Whether the underlying interface is a VNIC or not. */
182
/** Whether the underlying interface is a VNIC template or not. */
183
bool fIsVNICTemplate;
184
/** Handle to list of created VNICs. */
186
/** The MAC address of the host interface. */
188
/** Handle of this interface (lower MAC). */
189
mac_handle_t hInterface;
190
/** Handle to link state notifier. */
191
mac_notify_handle_t hNotify;
193
/** Pointer to the bound IPv4 stream. */
194
struct vboxnetflt_stream_t * volatile pIp4Stream;
195
/** Pointer to the bound IPv6 stream. */
196
struct vboxnetflt_stream_t * volatile pIp6Stream;
197
/** Pointer to the bound ARP stream. */
198
struct vboxnetflt_stream_t * volatile pArpStream;
199
/** Pointer to the unbound promiscuous stream. */
200
struct vboxnetflt_promisc_stream_t * volatile pPromiscStream;
201
/** Whether we are attaching to IPv6 stream dynamically now. */
202
bool volatile fAttaching;
203
/** Whether this is a VLAN interface or not. */
205
/** Layered device handle to the interface. */
207
/** The MAC address of the interface. */
209
/** Mutex protection used for loopback. */
211
/** Mutex protection used for dynamic IPv6 attaches. */
212
RTSEMFASTMUTEX hPollMtx;
215
# elif defined(RT_OS_FREEBSD)
216
/** @name FreeBSD instance data.
218
/** Interface handle */
220
/** Netgraph node handle */
226
/** Original interface flags */
236
/** The MAC address of the interface. */
239
# elif defined(RT_OS_WINDOWS)
240
/** @name Windows instance data.
242
/** Filter driver device context. */
245
volatile uint32_t cModeNetFltRefs;
246
volatile uint32_t cModePassThruRefs;
247
#ifndef VBOXNETFLT_NO_PACKET_QUEUE
248
/** Packet worker thread info */
249
PACKET_QUEUE_WORKER PacketQueueWorker;
251
/** The MAC address of the interface. Caching MAC for performance reasons. */
253
/** mutex used to synchronize WinIf init/deinit */
254
RTSEMMUTEX hWinIfMutex;
262
#if defined(RT_OS_WINDOWS)
263
# if defined(VBOX_NETFLT_ONDEMAND_BIND)
264
uint8_t abPadding[192];
265
# elif defined(VBOXNETADP)
266
uint8_t abPadding[256];
268
uint8_t abPadding[1024];
270
#elif defined(RT_OS_LINUX)
271
uint8_t abPadding[320];
272
#elif defined(RT_OS_FREEBSD)
273
uint8_t abPadding[320];
275
uint8_t abPadding[128];
279
/** The interface name. */
282
/** Pointer to the instance data of a host network filter driver. */
283
typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
285
AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
286
#ifdef VBOXNETFLT_OS_SPECFIC
287
AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
292
* The global data of the VBox filter driver.
294
* This contains the bit required for communicating with support driver, VBoxDrv
295
* (start out as SupDrv).
297
typedef struct VBOXNETFLTGLOBALS
299
/** Mutex protecting the list of instances and state changes. */
300
RTSEMFASTMUTEX hFastMtx;
301
/** Pointer to a list of instance data. */
302
PVBOXNETFLTINS pInstanceHead;
304
/** The INTNET trunk network interface factory. */
305
INTNETTRUNKFACTORY TrunkFactory;
306
/** The SUPDRV component factory registration. */
307
SUPDRVFACTORY SupDrvFactory;
308
/** The number of current factory references. */
309
int32_t volatile cFactoryRefs;
310
/** Whether the IDC connection is open or not.
311
* This is only for cleaning up correctly after the separate IDC init on Windows. */
313
/** The SUPDRV IDC handle (opaque struct). */
314
SUPDRVIDCHANDLE SupDrvIDC;
318
DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
319
DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
320
DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
321
DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
322
DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
323
DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
325
DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
326
DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
328
DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
329
DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
330
DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
331
DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
333
#ifdef VBOXNETFLT_STATIC_CONFIG
334
DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
339
/** @name The OS specific interface.
342
* Try rediscover the host interface.
344
* This is called periodically from the transmit path if we're marked as
345
* disconnected from the host. There is no chance of a race here.
347
* @returns true if the interface was successfully rediscovered and reattach,
349
* @param pThis The new instance.
351
DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
356
* @return IPRT status code.
357
* @param pThis The new instance.
358
* @param pvIfData Pointer to the host-private interface data.
359
* @param pSG The (scatter/)gather list.
360
* @param fDst The destination mask. At least one bit will be set.
362
* @remarks Owns the out-bound trunk port semaphore.
364
DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
367
* This is called when activating or suspending the instance.
369
* Use this method to enable and disable promiscuous mode on
370
* the interface to prevent unnecessary interrupt load.
372
* It is only called when the state changes.
374
* @param pThis The instance.
376
* @remarks Owns the lock for the out-bound trunk port.
378
DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
381
* This is called when a network interface has obtained a new MAC address.
383
* @param pThis The instance.
384
* @param pvIfData Pointer to the private interface data.
385
* @param pMac Pointer to the new MAC address.
387
DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
390
* This is called when an interface is connected to the network.
392
* @return IPRT status code.
393
* @param pThis The instance.
394
* @param pvIf Pointer to the interface.
395
* @param ppvIfData Where to store the private interface data.
397
DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
400
* This is called when a VM host disconnects from the network.
402
* @param pThis The instance.
403
* @param pvIfData Pointer to the private interface data.
405
DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
408
* This is called to when disconnecting from a network.
410
* @return IPRT status code.
411
* @param pThis The new instance.
413
* @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
415
DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
418
* This is called to when connecting to a network.
420
* @return IPRT status code.
421
* @param pThis The new instance.
423
* @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
425
DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
428
* Counter part to vboxNetFltOsInitInstance().
430
* @return IPRT status code.
431
* @param pThis The new instance.
433
* @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
435
DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
438
* This is called to attach to the actual host interface
439
* after linking the instance into the list.
441
* The MAC address as well promiscuousness and GSO capabilities should be
442
* reported by this function.
444
* @return IPRT status code.
445
* @param pThis The new instance.
446
* @param pvContext The user supplied context in the static config only.
447
* NULL in the dynamic config.
449
* @remarks Owns no locks.
451
DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
454
* This is called to perform structure initializations.
456
* @return IPRT status code.
457
* @param pThis The new instance.
459
* @remarks Owns no locks.
461
DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);