VirtualBox

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

Last change on this file since 13654 was 13654, checked in by vboxsync, 16 years ago

Solaris/VBoxNetFlt: Added IPv6 support (untested).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.8 KB
Line 
1/* $Id: VBoxNetFltInternal.h 13654 2008-10-29 15:01:22Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Internal Header.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
8 *
9 * Sun Microsystems, Inc. confidential
10 * All rights reserved
11 */
12
13#ifndef ___VBoxNetFltInternal_h___
14#define ___VBoxNetFltInternal_h___
15
16#include <VBox/sup.h>
17#include <VBox/intnet.h>
18#include <iprt/semaphore.h>
19#include <iprt/assert.h>
20
21
22__BEGIN_DECLS
23
24/** Pointer to the globals. */
25typedef struct VBOXNETFLTGLOBALS *PVBOXNETFLTGLOBALS;
26
27
28/**
29 * The state of a filter driver instance.
30 *
31 * The state machine differs a bit between the platforms because of
32 * the way we hook into the stack. On some hosts we can dynamically
33 * attach when required (on CreateInstance) and on others we will
34 * have to connect when the network stack is bound up. These modes
35 * are called static and dynamic config and governed at compile time
36 * by the VBOXNETFLT_STATIC_CONFIG define.
37 *
38 * See sec_netflt_msc for more details on locking and synchronization.
39 */
40typedef enum VBOXNETFTLINSSTATE
41{
42 /** The usual invalid state. */
43 kVBoxNetFltInsState_Invalid = 0,
44 /** Initialization.
45 * We've reserved the interface name but need to attach to the actual
46 * network interface outside the lock to avoid deadlocks.
47 * In the dynamic case this happens during a Create(Instance) call.
48 * In the static case it happens during driver initialization. */
49 kVBoxNetFltInsState_Initializing,
50#ifdef VBOXNETFLT_STATIC_CONFIG
51 /** Unconnected, not hooked up to a switch (static only).
52 * The filter driver instance has been instantiated and hooked up,
53 * waiting to be connected to an internal network. */
54 kVBoxNetFltInsState_Unconnected,
55#endif
56 /** Connected to an internal network. */
57 kVBoxNetFltInsState_Connected,
58 /** Disconnecting from the internal network and possibly the host network interface.
59 * Partly for reasons of deadlock avoidance again. */
60 kVBoxNetFltInsState_Disconnecting,
61 /** The instance has been disconnected from both the host and the internal network. */
62 kVBoxNetFltInsState_Destroyed,
63
64 /** The habitual 32-bit enum hack. */
65 kVBoxNetFltInsState_32BitHack = 0x7fffffff
66} VBOXNETFTLINSSTATE;
67
68
69/**
70 * The per-instance data of the VBox filter driver.
71 *
72 * This is data associated with a network interface / NIC / wossname which
73 * the filter driver has been or may be attached to. When possible it is
74 * attached dynamically, but this may not be possible on all OSes so we have
75 * to be flexible about things.
76 *
77 * A network interface / NIC / wossname can only have one filter driver
78 * instance attached to it. So, attempts at connecting an internal network
79 * to an interface that's already in use (connected to another internal network)
80 * will result in a VERR_SHARING_VIOLATION.
81 *
82 * Only one internal network can connect to a filter driver instance.
83 */
84typedef struct VBOXNETFLTINS
85{
86 /** Pointer to the next interface in the list. (VBOXNETFLTGLOBAL::pInstanceHead) */
87 struct VBOXNETFLTINS *pNext;
88 /** Our RJ-45 port.
89 * This is what the internal network plugs into. */
90 INTNETTRUNKIFPORT MyPort;
91 /** The RJ-45 port on the INTNET "switch".
92 * This is what we're connected to. */
93 PINTNETTRUNKSWPORT pSwitchPort;
94 /** Pointer to the globals. */
95 PVBOXNETFLTGLOBALS pGlobals;
96
97 /** The spinlock protecting the state variables and host interface handle. */
98 RTSPINLOCK hSpinlock;
99 /** The current interface state. */
100 VBOXNETFTLINSSTATE volatile enmState;
101 /** Active / Suspended indicator. */
102 bool volatile fActive;
103 /** Disconnected from the host network interface. */
104 bool volatile fDisconnectedFromHost;
105 /** Rediscovery is pending.
106 * cBusy will never reach zero during rediscovery, so which
107 * takes care of serializing rediscovery and disconnecting. */
108 bool volatile fRediscoveryPending;
109#if (ARCH_BITS == 32) && defined(__GNUC__)
110 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
111#endif
112 /** The timestamp of the last rediscovery. */
113 uint64_t volatile NanoTSLastRediscovery;
114 /** Reference count. */
115 uint32_t volatile cRefs;
116 /** The busy count.
117 * This counts the number of current callers and pending packet. */
118 uint32_t volatile cBusy;
119 /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
120 RTSEMEVENT hEventIdle;
121
122 union
123 {
124#ifdef VBOXNETFLT_OS_SPECFIC
125 struct
126 {
127# if defined(RT_OS_DARWIN)
128 /** @name Darwin instance data.
129 * @{ */
130 /** Pointer to the darwin network interface we're attached to.
131 * This is treated as highly volatile and should only be read and retained
132 * while owning hSpinlock. Releasing references to this should not be done
133 * while owning it though as we might end up destroying it in some paths. */
134 ifnet_t volatile pIfNet;
135 /** The interface filter handle.
136 * Same access rules as with pIfNet. */
137 interface_filter_t volatile pIfFilter;
138 /** Whether we've need to set promiscuous mode when the interface comes up. */
139 bool volatile fNeedSetPromiscuous;
140 /** Whether we've successfully put the interface into to promiscuous mode.
141 * This is for dealing with the ENETDOWN case. */
142 bool volatile fSetPromiscuous;
143 /** The MAC address of the interface. */
144 RTMAC Mac;
145 /** @} */
146# elif defined(RT_OS_LINUX)
147 /** @name Linux instance data
148 * @{ */
149 /** Pointer to the device. */
150 struct net_device volatile *pDev;
151 /** @} */
152# elif defined(RT_OS_SOLARIS)
153 /** @name Solaris instance data.
154 * @{ */
155 /** Pointer to the bound IPv4 stream. */
156 void volatile *pvIp4Stream;
157 /** Pointer to the bound IPv6 stream. */
158 void volatile *pvIp6Stream;
159 /** Pointer to the bound ARP stream. */
160 void volatile *pvArpStream;
161 /** Pointer to the unbound promiscuous stream. */
162 void volatile *pvPromiscStream;
163 /** Layered device handle to the interface. */
164 ldi_handle_t hIface;
165 /** The MAC address of the interface. */
166 RTMAC Mac;
167 /** Mutex protection used for loopback. */
168 RTSEMFASTMUTEX hFastMtx;
169# elif defined(RT_OS_WINDOWS)
170# ifdef VBOX_NETFLT_ONDEMAND_BIND
171 /** filter driver device context */
172 ADAPT IfAdaptor;
173# else
174 /** pointer to the filter driver device context */
175 PADAPT volatile pIfAdaptor;
176# endif
177 /** The MAC address of the interface. Caching MAC for performance reasons */
178 RTMAC Mac;
179# else
180# error "PORTME"
181# endif
182 } s;
183#endif
184 /** Padding. */
185#if defined(RT_OS_WINDOWS) && defined(VBOX_NETFLT_ONDEMAND_BIND)
186 /* windows AND protocol-based approach :
187 * we include the ADAPT into the VBOXNETFLTINS,
188 * and we do not feet into the 64 bytes padding,
189 * make it bigger */
190 uint8_t abPadding[192];
191#else
192 uint8_t abPadding[64];
193#endif
194 } u;
195
196 /** The interface name. */
197 char szName[1];
198} VBOXNETFLTINS;
199/** Pointer to the instance data of a host network filter driver. */
200typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
201
202AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
203#ifdef VBOXNETFLT_OS_SPECFIC
204AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
205#endif
206
207
208/**
209 * The global data of the VBox filter driver.
210 *
211 * This contains the bit required for communicating with support driver, VBoxDrv
212 * (start out as SupDrv).
213 */
214typedef struct VBOXNETFLTGLOBALS
215{
216 /** Mutex protecting the list of instances and state changes. */
217 RTSEMFASTMUTEX hFastMtx;
218 /** Pointer to a list of instance data. */
219 PVBOXNETFLTINS pInstanceHead;
220
221 /** The INTNET trunk network interface factory. */
222 INTNETTRUNKFACTORY TrunkFactory;
223 /** The SUPDRV component factory registration. */
224 SUPDRVFACTORY SupDrvFactory;
225 /** The number of current factory references. */
226 int32_t volatile cFactoryRefs;
227
228 /** The SUPDRV IDC handle (opaque struct). */
229 SUPDRVIDCHANDLE SupDrvIDC;
230} VBOXNETFLTGLOBALS;
231
232
233DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
234DECLHIDDEN(int) vboxNetFltTryDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
235DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
236DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
237
238DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
239DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
240
241
242/** @name The OS specific interface.
243 * @{ */
244/**
245 * Try rediscover the host interface.
246 *
247 * This is called periodically from the transmit path if we're marked as
248 * disconnected from the host. There is no chance of a race here.
249 *
250 * @returns true if the interface was successfully rediscovered and reattach,
251 * otherwise false.
252 * @param pThis The new instance.
253 */
254DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
255
256/**
257 * Transmits a frame.
258 *
259 * @return IPRT status code.
260 * @param pThis The new instance.
261 * @param pSG The (scatter/)gather list.
262 * @param fDst The destination mask. At least one bit will be set.
263 *
264 * @remarks Owns the out-bound trunk port semaphore.
265 */
266DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst);
267
268/**
269 * Checks if the interface is in promiscuous mode from the host perspective.
270 *
271 * If it is, then the internal networking switch will send frames
272 * heading for the wire to the host as well.
273 *
274 * @returns true / false accordingly.
275 * @param pThis The instance.
276 *
277 * @remarks Owns the network lock and the out-bound trunk port semaphores.
278 */
279DECLHIDDEN(bool) vboxNetFltPortOsIsPromiscuous(PVBOXNETFLTINS pThis);
280
281/**
282 * Get the MAC address of the interface we're attached to.
283 *
284 * Used by the internal networking switch for implementing the
285 * shared-MAC-on-the-wire mode.
286 *
287 * @param pThis The instance.
288 * @param pMac Where to store the MAC address.
289 * If you don't know, set all the bits except the first (the multicast one).
290 *
291 * @remarks Owns the network lock and the out-bound trunk port semaphores.
292 */
293DECLHIDDEN(void) vboxNetFltPortOsGetMacAddress(PVBOXNETFLTINS pThis, PRTMAC pMac);
294
295/**
296 * Checks if the specified MAC address is for any of the host interfaces.
297 *
298 * Used by the internal networking switch to decide the destination(s)
299 * of a frame.
300 *
301 * @returns true / false accordingly.
302 * @param pThis The instance.
303 * @param pMac The MAC address.
304 *
305 * @remarks Owns the network lock and the out-bound trunk port semaphores.
306 */
307DECLHIDDEN(bool) vboxNetFltPortOsIsHostMac(PVBOXNETFLTINS pThis, PCRTMAC pMac);
308
309/**
310 * This is called when activating or suspending the instance.
311 *
312 * Use this method to enable and disable promiscuous mode on
313 * the interface to prevent unnecessary interrupt load.
314 *
315 * It is only called when the state changes.
316 *
317 * @param pThis The instance.
318 *
319 * @remarks Owns the lock for the out-bound trunk port.
320 */
321DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
322
323/**
324 * This is called to when disconnecting from a network.
325 *
326 * @return IPRT status code.
327 * @param pThis The new instance.
328 *
329 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
330 */
331DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
332
333/**
334 * This is called to when connecting to a network.
335 *
336 * @return IPRT status code.
337 * @param pThis The new instance.
338 *
339 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
340 */
341DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
342
343/**
344 * Counter part to vboxNetFltOsInitInstance().
345 *
346 * @return IPRT status code.
347 * @param pThis The new instance.
348 *
349 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
350 */
351DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
352
353/**
354 * This is called to attach to the actual host interface
355 * after linking the instance into the list.
356 *
357 * @return IPRT status code.
358 * @param pThis The new instance.
359 *
360 * @remarks Owns no locks.
361 */
362DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis);
363
364/**
365 * This is called to perform structure initializations.
366 *
367 * @return IPRT status code.
368 * @param pThis The new instance.
369 *
370 * @remarks Owns no locks.
371 */
372DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
373/** @} */
374
375
376__END_DECLS
377
378#endif
379
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