VirtualBox

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

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

VBoxNetFlt: Better assert some darwin alignment assumptions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.8 KB
Line 
1/* $Id: VBoxNetFltInternal.h 75687 2018-11-23 13:19:11Z 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);
303AssertCompileMemberAlignment(VBOXNETFLTINS, u, 8);
304#ifdef VBOXNETFLT_OS_SPECFIC
305AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
306#endif
307
308
309/**
310 * The global data of the VBox filter driver.
311 *
312 * This contains the bit required for communicating with support driver, VBoxDrv
313 * (start out as SupDrv).
314 */
315typedef struct VBOXNETFLTGLOBALS
316{
317 /** Mutex protecting the list of instances and state changes. */
318 RTSEMFASTMUTEX hFastMtx;
319 /** Pointer to a list of instance data. */
320 PVBOXNETFLTINS pInstanceHead;
321
322 /** The INTNET trunk network interface factory. */
323 INTNETTRUNKFACTORY TrunkFactory;
324 /** The SUPDRV component factory registration. */
325 SUPDRVFACTORY SupDrvFactory;
326 /** The number of current factory references. */
327 int32_t volatile cFactoryRefs;
328 /** Whether the IDC connection is open or not.
329 * This is only for cleaning up correctly after the separate IDC init on Windows. */
330 bool fIDCOpen;
331 /** The SUPDRV IDC handle (opaque struct). */
332 SUPDRVIDCHANDLE SupDrvIDC;
333} VBOXNETFLTGLOBALS;
334
335
336DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
337DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
338DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
339DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
340DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
341DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
342
343DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
344DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
345
346DECLHIDDEN(DECLCALLBACK(void)) vboxNetFltPortReleaseBusy(PINTNETTRUNKIFPORT pIfPort);
347DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
348DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
349DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
350DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
351
352#ifdef VBOXNETFLT_STATIC_CONFIG
353DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
354#endif
355
356
357
358/** @name The OS specific interface.
359 * @{ */
360/**
361 * Try rediscover the host interface.
362 *
363 * This is called periodically from the transmit path if we're marked as
364 * disconnected from the host. There is no chance of a race here.
365 *
366 * @returns true if the interface was successfully rediscovered and reattach,
367 * otherwise false.
368 * @param pThis The new instance.
369 */
370DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
371
372/**
373 * Transmits a frame.
374 *
375 * @return IPRT status code.
376 * @param pThis The new instance.
377 * @param pvIfData Pointer to the host-private interface data.
378 * @param pSG The (scatter/)gather list.
379 * @param fDst The destination mask. At least one bit will be set.
380 *
381 * @remarks Owns the out-bound trunk port semaphore.
382 */
383DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
384
385/**
386 * This is called when activating or suspending the instance.
387 *
388 * Use this method to enable and disable promiscuous mode on
389 * the interface to prevent unnecessary interrupt load.
390 *
391 * It is only called when the state changes.
392 *
393 * @param pThis The instance.
394 * @param fActive Whether to active (@c true) or deactive.
395 *
396 * @remarks Owns the lock for the out-bound trunk port.
397 */
398DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
399
400/**
401 * This is called when a network interface has obtained a new MAC address.
402 *
403 * @param pThis The instance.
404 * @param pvIfData Pointer to the private interface data.
405 * @param pMac Pointer to the new MAC address.
406 */
407DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
408
409/**
410 * This is called when an interface is connected to the network.
411 *
412 * @return IPRT status code.
413 * @param pThis The instance.
414 * @param pvIf Pointer to the interface.
415 * @param ppvIfData Where to store the private interface data.
416 */
417DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
418
419/**
420 * This is called when a VM host disconnects from the network.
421 *
422 * @param pThis The instance.
423 * @param pvIfData Pointer to the private interface data.
424 */
425DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
426
427/**
428 * This is called to when disconnecting from a network.
429 *
430 * @return IPRT status code.
431 * @param pThis The new instance.
432 *
433 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
434 */
435DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
436
437/**
438 * This is called to when connecting to a network.
439 *
440 * @return IPRT status code.
441 * @param pThis The new instance.
442 *
443 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
444 */
445DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
446
447/**
448 * Counter part to vboxNetFltOsInitInstance().
449 *
450 * @return IPRT status code.
451 * @param pThis The new instance.
452 *
453 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
454 */
455DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
456
457/**
458 * This is called to attach to the actual host interface
459 * after linking the instance into the list.
460 *
461 * The MAC address as well promiscuousness and GSO capabilities should be
462 * reported by this function.
463 *
464 * @return IPRT status code.
465 * @param pThis The new instance.
466 * @param pvContext The user supplied context in the static config only.
467 * NULL in the dynamic config.
468 *
469 * @remarks Owns no locks.
470 */
471DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
472
473/**
474 * This is called to perform structure initializations.
475 *
476 * @return IPRT status code.
477 * @param pThis The new instance.
478 *
479 * @remarks Owns no locks.
480 */
481DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
482/** @} */
483
484
485RT_C_DECLS_END
486
487#endif
488
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