VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/VBoxNetFltInternal.h@ 75690

Last change on this file since 75690 was 75690, checked in by vboxsync, 6 years ago

VBoxNetFlt: win/x86 build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.7 KB
Line 
1/* $Id: VBoxNetFltInternal.h 75690 2018-11-23 14:50:38Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Internal Header.
4 */
5
6/*
7 * Copyright (C) 2008-2017 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBoxNetFltInternal_h___
28#define ___VBoxNetFltInternal_h___
29
30#include <VBox/sup.h>
31#include <VBox/intnet.h>
32#include <iprt/semaphore.h>
33#include <iprt/assert.h>
34
35
36RT_C_DECLS_BEGIN
37
38/** Pointer to the globals. */
39typedef struct VBOXNETFLTGLOBALS *PVBOXNETFLTGLOBALS;
40
41
42/**
43 * The state of a filter driver instance.
44 *
45 * The state machine differs a bit between the platforms because of
46 * the way we hook into the stack. On some hosts we can dynamically
47 * attach when required (on CreateInstance) and on others we will
48 * have to connect when the network stack is bound up. These modes
49 * are called static and dynamic config and governed at compile time
50 * by the VBOXNETFLT_STATIC_CONFIG define.
51 *
52 * See sec_netflt_msc for more details on locking and synchronization.
53 */
54typedef enum VBOXNETFTLINSSTATE
55{
56 /** The usual invalid state. */
57 kVBoxNetFltInsState_Invalid = 0,
58 /** Initialization.
59 * We've reserved the interface name but need to attach to the actual
60 * network interface outside the lock to avoid deadlocks.
61 * In the dynamic case this happens during a Create(Instance) call.
62 * In the static case it happens during driver initialization. */
63 kVBoxNetFltInsState_Initializing,
64#ifdef VBOXNETFLT_STATIC_CONFIG
65 /** Unconnected, not hooked up to a switch (static only).
66 * The filter driver instance has been instantiated and hooked up,
67 * waiting to be connected to an internal network. */
68 kVBoxNetFltInsState_Unconnected,
69#endif
70 /** Connected to an internal network. */
71 kVBoxNetFltInsState_Connected,
72 /** Disconnecting from the internal network and possibly the host network interface.
73 * Partly for reasons of deadlock avoidance again. */
74 kVBoxNetFltInsState_Disconnecting,
75 /** The instance has been disconnected from both the host and the internal network. */
76 kVBoxNetFltInsState_Destroyed,
77
78 /** The habitual 32-bit enum hack. */
79 kVBoxNetFltInsState_32BitHack = 0x7fffffff
80} VBOXNETFTLINSSTATE;
81
82
83/**
84 * The per-instance data of the VBox filter driver.
85 *
86 * This is data associated with a network interface / NIC / wossname which
87 * the filter driver has been or may be attached to. When possible it is
88 * attached dynamically, but this may not be possible on all OSes so we have
89 * to be flexible about things.
90 *
91 * A network interface / NIC / wossname can only have one filter driver
92 * instance attached to it. So, attempts at connecting an internal network
93 * to an interface that's already in use (connected to another internal network)
94 * will result in a VERR_SHARING_VIOLATION.
95 *
96 * Only one internal network can connect to a filter driver instance.
97 */
98typedef struct VBOXNETFLTINS
99{
100 /** Pointer to the next interface in the list. (VBOXNETFLTGLOBAL::pInstanceHead) */
101 struct VBOXNETFLTINS *pNext;
102 /** Our RJ-45 port.
103 * This is what the internal network plugs into. */
104 INTNETTRUNKIFPORT MyPort;
105 /** The RJ-45 port on the INTNET "switch".
106 * This is what we're connected to. */
107 PINTNETTRUNKSWPORT pSwitchPort;
108 /** Pointer to the globals. */
109 PVBOXNETFLTGLOBALS pGlobals;
110
111 /** The spinlock protecting the state variables and host interface handle. */
112 RTSPINLOCK hSpinlock;
113 /** The current interface state. */
114 VBOXNETFTLINSSTATE volatile enmState;
115 /** The trunk state. */
116 INTNETTRUNKIFSTATE volatile enmTrunkState;
117 bool volatile fActive;
118 /** Disconnected from the host network interface. */
119 bool volatile fDisconnectedFromHost;
120 /** Rediscovery is pending.
121 * cBusy will never reach zero during rediscovery, so which
122 * takes care of serializing rediscovery and disconnecting. */
123 bool volatile fRediscoveryPending;
124 /** Whether we should not attempt to set promiscuous mode at all. */
125 bool fDisablePromiscuous;
126#if (ARCH_BITS == 32) && defined(__GNUC__)
127#if 0
128 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
129#endif
130#endif
131 /** The timestamp of the last rediscovery. */
132 uint64_t volatile NanoTSLastRediscovery;
133 /** Reference count. */
134 uint32_t volatile cRefs;
135 /** The busy count.
136 * This counts the number of current callers and pending packet. */
137 uint32_t volatile cBusy;
138 /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
139 RTSEMEVENT hEventIdle;
140
141 /** @todo move MacAddr out of this structure! */
142 union
143 {
144#ifdef VBOXNETFLT_OS_SPECFIC
145 struct
146 {
147# if defined(RT_OS_DARWIN)
148 /** @name Darwin instance data.
149 * @{ */
150 /** Pointer to the darwin network interface we're attached to.
151 * This is treated as highly volatile and should only be read and retained
152 * while owning hSpinlock. Releasing references to this should not be done
153 * while owning it though as we might end up destroying it in some paths. */
154 ifnet_t volatile pIfNet;
155 /** The interface filter handle.
156 * Same access rules as with pIfNet. */
157 interface_filter_t volatile pIfFilter;
158 /** Whether we've need to set promiscuous mode when the interface comes up. */
159 bool volatile fNeedSetPromiscuous;
160 /** Whether we've successfully put the interface into to promiscuous mode.
161 * This is for dealing with the ENETDOWN case. */
162 bool volatile fSetPromiscuous;
163 /** The MAC address of the interface. */
164 RTMAC MacAddr;
165 /** PF_SYSTEM socket to listen for events (XXX: globals?) */
166 socket_t pSysSock;
167 /** @} */
168# elif defined(RT_OS_LINUX)
169 /** @name Linux instance data
170 * @{ */
171 /** Pointer to the device. */
172 struct net_device * volatile pDev;
173 /** MTU of host's interface. */
174 uint32_t cbMtu;
175 /** Whether we've successfully put the interface into to promiscuous mode.
176 * This is for dealing with the ENETDOWN case. */
177 bool volatile fPromiscuousSet;
178 /** Whether device exists and physically attached. */
179 bool volatile fRegistered;
180 /** Whether our packet handler is installed. */
181 bool volatile fPacketHandler;
182 /** The MAC address of the interface. */
183 RTMAC MacAddr;
184 struct notifier_block Notifier; /* netdevice */
185 struct notifier_block NotifierIPv4;
186 struct notifier_block NotifierIPv6;
187 struct packet_type PacketType;
188# ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
189 struct sk_buff_head XmitQueue;
190 struct work_struct XmitTask;
191# endif
192 /** @} */
193# elif defined(RT_OS_SOLARIS)
194 /** @name Solaris instance data.
195 * @{ */
196# ifdef VBOX_WITH_NETFLT_CROSSBOW
197 /** Whether the underlying interface is a VNIC or not. */
198 bool fIsVNIC;
199 /** Whether the underlying interface is a VNIC template or not. */
200 bool fIsVNICTemplate;
201 /** Handle to list of created VNICs. */
202 list_t hVNICs;
203 /** The MAC address of the host interface. */
204 RTMAC MacAddr;
205 /** Handle of this interface (lower MAC). */
206 mac_handle_t hInterface;
207 /** Handle to link state notifier. */
208 mac_notify_handle_t hNotify;
209# else
210 /** Pointer to the bound IPv4 stream. */
211 struct vboxnetflt_stream_t * volatile pIp4Stream;
212 /** Pointer to the bound IPv6 stream. */
213 struct vboxnetflt_stream_t * volatile pIp6Stream;
214 /** Pointer to the bound ARP stream. */
215 struct vboxnetflt_stream_t * volatile pArpStream;
216 /** Pointer to the unbound promiscuous stream. */
217 struct vboxnetflt_promisc_stream_t * volatile pPromiscStream;
218 /** Whether we are attaching to IPv6 stream dynamically now. */
219 bool volatile fAttaching;
220 /** Whether this is a VLAN interface or not. */
221 bool volatile fVLAN;
222 /** Layered device handle to the interface. */
223 ldi_handle_t hIface;
224 /** The MAC address of the interface. */
225 RTMAC MacAddr;
226 /** Mutex protection used for loopback. */
227 kmutex_t hMtx;
228 /** Mutex protection used for dynamic IPv6 attaches. */
229 RTSEMFASTMUTEX hPollMtx;
230# endif
231 /** @} */
232# elif defined(RT_OS_FREEBSD)
233 /** @name FreeBSD instance data.
234 * @{ */
235 /** Interface handle */
236 struct ifnet *ifp;
237 /** Netgraph node handle */
238 node_p node;
239 /** Input hook */
240 hook_p input;
241 /** Output hook */
242 hook_p output;
243 /** Original interface flags */
244 unsigned int flags;
245 /** Input queue */
246 struct ifqueue inq;
247 /** Output queue */
248 struct ifqueue outq;
249 /** Input task */
250 struct task tskin;
251 /** Output task */
252 struct task tskout;
253 /** The MAC address of the interface. */
254 RTMAC MacAddr;
255 /** @} */
256# elif defined(RT_OS_WINDOWS)
257 /** @name Windows instance data.
258 * @{ */
259 /** Filter driver device context. */
260 VBOXNETFLTWIN WinIf;
261
262 volatile uint32_t cModeNetFltRefs;
263 volatile uint32_t cModePassThruRefs;
264#ifndef VBOXNETFLT_NO_PACKET_QUEUE
265 /** Packet worker thread info */
266 PACKET_QUEUE_WORKER PacketQueueWorker;
267#endif
268 /** The MAC address of the interface. Caching MAC for performance reasons. */
269 RTMAC MacAddr;
270 /** mutex used to synchronize WinIf init/deinit */
271 RTSEMMUTEX hWinIfMutex;
272 /** @} */
273# else
274# error "PORTME"
275# endif
276 } s;
277#endif
278 /** Padding. */
279#if defined(RT_OS_WINDOWS)
280# if defined(VBOX_NETFLT_ONDEMAND_BIND)
281 uint8_t abPadding[192];
282# elif defined(VBOXNETADP)
283 uint8_t abPadding[256];
284# else
285 uint8_t abPadding[1024];
286# endif
287#elif defined(RT_OS_LINUX)
288 uint8_t abPadding[320];
289#elif defined(RT_OS_FREEBSD)
290 uint8_t abPadding[320];
291#else
292 uint8_t abPadding[128];
293#endif
294 } u;
295
296 /** The interface name. */
297 char szName[1];
298} VBOXNETFLTINS;
299/** Pointer to the instance data of a host network filter driver. */
300typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
301
302AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
303#ifdef VBOXNETFLT_OS_SPECFIC
304AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
305#endif
306
307
308/**
309 * The global data of the VBox filter driver.
310 *
311 * This contains the bit required for communicating with support driver, VBoxDrv
312 * (start out as SupDrv).
313 */
314typedef struct VBOXNETFLTGLOBALS
315{
316 /** Mutex protecting the list of instances and state changes. */
317 RTSEMFASTMUTEX hFastMtx;
318 /** Pointer to a list of instance data. */
319 PVBOXNETFLTINS pInstanceHead;
320
321 /** The INTNET trunk network interface factory. */
322 INTNETTRUNKFACTORY TrunkFactory;
323 /** The SUPDRV component factory registration. */
324 SUPDRVFACTORY SupDrvFactory;
325 /** The number of current factory references. */
326 int32_t volatile cFactoryRefs;
327 /** Whether the IDC connection is open or not.
328 * This is only for cleaning up correctly after the separate IDC init on Windows. */
329 bool fIDCOpen;
330 /** The SUPDRV IDC handle (opaque struct). */
331 SUPDRVIDCHANDLE SupDrvIDC;
332} VBOXNETFLTGLOBALS;
333
334
335DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
336DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
337DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
338DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
339DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
340DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
341
342DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
343DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
344
345DECLHIDDEN(DECLCALLBACK(void)) vboxNetFltPortReleaseBusy(PINTNETTRUNKIFPORT pIfPort);
346DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
347DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
348DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
349DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
350
351#ifdef VBOXNETFLT_STATIC_CONFIG
352DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
353#endif
354
355
356
357/** @name The OS specific interface.
358 * @{ */
359/**
360 * Try rediscover the host interface.
361 *
362 * This is called periodically from the transmit path if we're marked as
363 * disconnected from the host. There is no chance of a race here.
364 *
365 * @returns true if the interface was successfully rediscovered and reattach,
366 * otherwise false.
367 * @param pThis The new instance.
368 */
369DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
370
371/**
372 * Transmits a frame.
373 *
374 * @return IPRT status code.
375 * @param pThis The new instance.
376 * @param pvIfData Pointer to the host-private interface data.
377 * @param pSG The (scatter/)gather list.
378 * @param fDst The destination mask. At least one bit will be set.
379 *
380 * @remarks Owns the out-bound trunk port semaphore.
381 */
382DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
383
384/**
385 * This is called when activating or suspending the instance.
386 *
387 * Use this method to enable and disable promiscuous mode on
388 * the interface to prevent unnecessary interrupt load.
389 *
390 * It is only called when the state changes.
391 *
392 * @param pThis The instance.
393 * @param fActive Whether to active (@c true) or deactive.
394 *
395 * @remarks Owns the lock for the out-bound trunk port.
396 */
397DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
398
399/**
400 * This is called when a network interface has obtained a new MAC address.
401 *
402 * @param pThis The instance.
403 * @param pvIfData Pointer to the private interface data.
404 * @param pMac Pointer to the new MAC address.
405 */
406DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
407
408/**
409 * This is called when an interface is connected to the network.
410 *
411 * @return IPRT status code.
412 * @param pThis The instance.
413 * @param pvIf Pointer to the interface.
414 * @param ppvIfData Where to store the private interface data.
415 */
416DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
417
418/**
419 * This is called when a VM host disconnects from the network.
420 *
421 * @param pThis The instance.
422 * @param pvIfData Pointer to the private interface data.
423 */
424DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
425
426/**
427 * This is called to when disconnecting from a network.
428 *
429 * @return IPRT status code.
430 * @param pThis The new instance.
431 *
432 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
433 */
434DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
435
436/**
437 * This is called to when connecting to a network.
438 *
439 * @return IPRT status code.
440 * @param pThis The new instance.
441 *
442 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
443 */
444DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
445
446/**
447 * Counter part to vboxNetFltOsInitInstance().
448 *
449 * @return IPRT status code.
450 * @param pThis The new instance.
451 *
452 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
453 */
454DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
455
456/**
457 * This is called to attach to the actual host interface
458 * after linking the instance into the list.
459 *
460 * The MAC address as well promiscuousness and GSO capabilities should be
461 * reported by this function.
462 *
463 * @return IPRT status code.
464 * @param pThis The new instance.
465 * @param pvContext The user supplied context in the static config only.
466 * NULL in the dynamic config.
467 *
468 * @remarks Owns no locks.
469 */
470DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
471
472/**
473 * This is called to perform structure initializations.
474 *
475 * @return IPRT status code.
476 * @param pThis The new instance.
477 *
478 * @remarks Owns no locks.
479 */
480DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
481/** @} */
482
483
484RT_C_DECLS_END
485
486#endif
487
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