VirtualBox

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

Last change on this file since 56712 was 56293, checked in by vboxsync, 10 years ago

HostDrivers: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.2 KB
Line 
1/* $Id: VBoxNetFltInternal.h 56293 2015-06-09 14:23:56Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Internal Header.
4 */
5
6/*
7 * Copyright (C) 2008-2015 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#ifndef ___VBoxNetFltInternal_h___
19#define ___VBoxNetFltInternal_h___
20
21#include <VBox/sup.h>
22#include <VBox/intnet.h>
23#include <iprt/semaphore.h>
24#include <iprt/assert.h>
25
26
27RT_C_DECLS_BEGIN
28
29/** Pointer to the globals. */
30typedef struct VBOXNETFLTGLOBALS *PVBOXNETFLTGLOBALS;
31
32
33/**
34 * The state of a filter driver instance.
35 *
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.
42 *
43 * See sec_netflt_msc for more details on locking and synchronization.
44 */
45typedef enum VBOXNETFTLINSSTATE
46{
47 /** The usual invalid state. */
48 kVBoxNetFltInsState_Invalid = 0,
49 /** Initialization.
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,
60#endif
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,
68
69 /** The habitual 32-bit enum hack. */
70 kVBoxNetFltInsState_32BitHack = 0x7fffffff
71} VBOXNETFTLINSSTATE;
72
73
74/**
75 * The per-instance data of the VBox filter driver.
76 *
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.
81 *
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.
86 *
87 * Only one internal network can connect to a filter driver instance.
88 */
89typedef struct VBOXNETFLTINS
90{
91 /** Pointer to the next interface in the list. (VBOXNETFLTGLOBAL::pInstanceHead) */
92 struct VBOXNETFLTINS *pNext;
93 /** Our RJ-45 port.
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;
101
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__)
118#if 0
119 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
120#endif
121#endif
122 /** The timestamp of the last rediscovery. */
123 uint64_t volatile NanoTSLastRediscovery;
124 /** Reference count. */
125 uint32_t volatile cRefs;
126 /** The busy count.
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;
131
132 /** @todo move MacAddr out of this structure! */
133 union
134 {
135#ifdef VBOXNETFLT_OS_SPECFIC
136 struct
137 {
138# if defined(RT_OS_DARWIN)
139 /** @name Darwin instance data.
140 * @{ */
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. */
155 RTMAC MacAddr;
156 /** PF_SYSTEM socket to listen for events (XXX: globals?) */
157 socket_t pSysSock;
158 /** @} */
159# elif defined(RT_OS_LINUX)
160 /** @name Linux instance data
161 * @{ */
162 /** Pointer to the device. */
163 struct net_device * volatile pDev;
164 /** MTU of host's interface. */
165 uint32_t cbMtu;
166 /** Whether we've successfully put the interface into to promiscuous mode.
167 * This is for dealing with the ENETDOWN case. */
168 bool volatile fPromiscuousSet;
169 /** Whether device exists and physically attached. */
170 bool volatile fRegistered;
171 /** Whether our packet handler is installed. */
172 bool volatile fPacketHandler;
173 /** The MAC address of the interface. */
174 RTMAC MacAddr;
175 struct notifier_block Notifier; /* netdevice */
176 struct notifier_block NotifierIPv4;
177 struct notifier_block NotifierIPv6;
178 struct packet_type PacketType;
179# ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
180 struct sk_buff_head XmitQueue;
181 struct work_struct XmitTask;
182# endif
183 /** @} */
184# elif defined(RT_OS_SOLARIS)
185 /** @name Solaris instance data.
186 * @{ */
187# ifdef VBOX_WITH_NETFLT_CROSSBOW
188 /** Whether the underlying interface is a VNIC or not. */
189 bool fIsVNIC;
190 /** Whether the underlying interface is a VNIC template or not. */
191 bool fIsVNICTemplate;
192 /** Handle to list of created VNICs. */
193 list_t hVNICs;
194 /** The MAC address of the host interface. */
195 RTMAC MacAddr;
196 /** Handle of this interface (lower MAC). */
197 mac_handle_t hInterface;
198 /** Handle to link state notifier. */
199 mac_notify_handle_t hNotify;
200# else
201 /** Pointer to the bound IPv4 stream. */
202 struct vboxnetflt_stream_t * volatile pIp4Stream;
203 /** Pointer to the bound IPv6 stream. */
204 struct vboxnetflt_stream_t * volatile pIp6Stream;
205 /** Pointer to the bound ARP stream. */
206 struct vboxnetflt_stream_t * volatile pArpStream;
207 /** Pointer to the unbound promiscuous stream. */
208 struct vboxnetflt_promisc_stream_t * volatile pPromiscStream;
209 /** Whether we are attaching to IPv6 stream dynamically now. */
210 bool volatile fAttaching;
211 /** Whether this is a VLAN interface or not. */
212 bool volatile fVLAN;
213 /** Layered device handle to the interface. */
214 ldi_handle_t hIface;
215 /** The MAC address of the interface. */
216 RTMAC MacAddr;
217 /** Mutex protection used for loopback. */
218 kmutex_t hMtx;
219 /** Mutex protection used for dynamic IPv6 attaches. */
220 RTSEMFASTMUTEX hPollMtx;
221# endif
222 /** @} */
223# elif defined(RT_OS_FREEBSD)
224 /** @name FreeBSD instance data.
225 * @{ */
226 /** Interface handle */
227 struct ifnet *ifp;
228 /** Netgraph node handle */
229 node_p node;
230 /** Input hook */
231 hook_p input;
232 /** Output hook */
233 hook_p output;
234 /** Original interface flags */
235 unsigned int flags;
236 /** Input queue */
237 struct ifqueue inq;
238 /** Output queue */
239 struct ifqueue outq;
240 /** Input task */
241 struct task tskin;
242 /** Output task */
243 struct task tskout;
244 /** The MAC address of the interface. */
245 RTMAC MacAddr;
246 /** @} */
247# elif defined(RT_OS_WINDOWS)
248 /** @name Windows instance data.
249 * @{ */
250 /** Filter driver device context. */
251 VBOXNETFLTWIN WinIf;
252
253 volatile uint32_t cModeNetFltRefs;
254 volatile uint32_t cModePassThruRefs;
255#ifndef VBOXNETFLT_NO_PACKET_QUEUE
256 /** Packet worker thread info */
257 PACKET_QUEUE_WORKER PacketQueueWorker;
258#endif
259 /** The MAC address of the interface. Caching MAC for performance reasons. */
260 RTMAC MacAddr;
261 /** mutex used to synchronize WinIf init/deinit */
262 RTSEMMUTEX hWinIfMutex;
263 /** @} */
264# else
265# error "PORTME"
266# endif
267 } s;
268#endif
269 /** Padding. */
270#if defined(RT_OS_WINDOWS)
271# if defined(VBOX_NETFLT_ONDEMAND_BIND)
272 uint8_t abPadding[192];
273# elif defined(VBOXNETADP)
274 uint8_t abPadding[256];
275# else
276 uint8_t abPadding[1024];
277# endif
278#elif defined(RT_OS_LINUX)
279 uint8_t abPadding[320];
280#elif defined(RT_OS_FREEBSD)
281 uint8_t abPadding[320];
282#else
283 uint8_t abPadding[128];
284#endif
285 } u;
286
287 /** The interface name. */
288 char szName[1];
289} VBOXNETFLTINS;
290/** Pointer to the instance data of a host network filter driver. */
291typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
292
293AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
294#ifdef VBOXNETFLT_OS_SPECFIC
295AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
296#endif
297
298
299/**
300 * The global data of the VBox filter driver.
301 *
302 * This contains the bit required for communicating with support driver, VBoxDrv
303 * (start out as SupDrv).
304 */
305typedef struct VBOXNETFLTGLOBALS
306{
307 /** Mutex protecting the list of instances and state changes. */
308 RTSEMFASTMUTEX hFastMtx;
309 /** Pointer to a list of instance data. */
310 PVBOXNETFLTINS pInstanceHead;
311
312 /** The INTNET trunk network interface factory. */
313 INTNETTRUNKFACTORY TrunkFactory;
314 /** The SUPDRV component factory registration. */
315 SUPDRVFACTORY SupDrvFactory;
316 /** The number of current factory references. */
317 int32_t volatile cFactoryRefs;
318 /** Whether the IDC connection is open or not.
319 * This is only for cleaning up correctly after the separate IDC init on Windows. */
320 bool fIDCOpen;
321 /** The SUPDRV IDC handle (opaque struct). */
322 SUPDRVIDCHANDLE SupDrvIDC;
323} VBOXNETFLTGLOBALS;
324
325
326DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
327DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
328DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
329DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
330DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
331DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
332
333DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
334DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
335
336DECLHIDDEN(DECLCALLBACK(void)) vboxNetFltPortReleaseBusy(PINTNETTRUNKIFPORT pIfPort);
337DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
338DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
339DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
340DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
341
342#ifdef VBOXNETFLT_STATIC_CONFIG
343DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
344#endif
345
346
347
348/** @name The OS specific interface.
349 * @{ */
350/**
351 * Try rediscover the host interface.
352 *
353 * This is called periodically from the transmit path if we're marked as
354 * disconnected from the host. There is no chance of a race here.
355 *
356 * @returns true if the interface was successfully rediscovered and reattach,
357 * otherwise false.
358 * @param pThis The new instance.
359 */
360DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
361
362/**
363 * Transmits a frame.
364 *
365 * @return IPRT status code.
366 * @param pThis The new instance.
367 * @param pvIfData Pointer to the host-private interface data.
368 * @param pSG The (scatter/)gather list.
369 * @param fDst The destination mask. At least one bit will be set.
370 *
371 * @remarks Owns the out-bound trunk port semaphore.
372 */
373DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
374
375/**
376 * This is called when activating or suspending the instance.
377 *
378 * Use this method to enable and disable promiscuous mode on
379 * the interface to prevent unnecessary interrupt load.
380 *
381 * It is only called when the state changes.
382 *
383 * @param pThis The instance.
384 *
385 * @remarks Owns the lock for the out-bound trunk port.
386 */
387DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
388
389/**
390 * This is called when a network interface has obtained a new MAC address.
391 *
392 * @param pThis The instance.
393 * @param pvIfData Pointer to the private interface data.
394 * @param pMac Pointer to the new MAC address.
395 */
396DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
397
398/**
399 * This is called when an interface is connected to the network.
400 *
401 * @return IPRT status code.
402 * @param pThis The instance.
403 * @param pvIf Pointer to the interface.
404 * @param ppvIfData Where to store the private interface data.
405 */
406DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
407
408/**
409 * This is called when a VM host disconnects from the network.
410 *
411 * @param pThis The instance.
412 * @param pvIfData Pointer to the private interface data.
413 */
414DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
415
416/**
417 * This is called to when disconnecting from a network.
418 *
419 * @return IPRT status code.
420 * @param pThis The new instance.
421 *
422 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
423 */
424DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
425
426/**
427 * This is called to when connecting to a network.
428 *
429 * @return IPRT status code.
430 * @param pThis The new instance.
431 *
432 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
433 */
434DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
435
436/**
437 * Counter part to vboxNetFltOsInitInstance().
438 *
439 * @return IPRT status code.
440 * @param pThis The new instance.
441 *
442 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
443 */
444DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
445
446/**
447 * This is called to attach to the actual host interface
448 * after linking the instance into the list.
449 *
450 * The MAC address as well promiscuousness and GSO capabilities should be
451 * reported by this function.
452 *
453 * @return IPRT status code.
454 * @param pThis The new instance.
455 * @param pvContext The user supplied context in the static config only.
456 * NULL in the dynamic config.
457 *
458 * @remarks Owns no locks.
459 */
460DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
461
462/**
463 * This is called to perform structure initializations.
464 *
465 * @return IPRT status code.
466 * @param pThis The new instance.
467 *
468 * @remarks Owns no locks.
469 */
470DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
471/** @} */
472
473
474RT_C_DECLS_END
475
476#endif
477
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