VirtualBox

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

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

VBoxNetFlt: Fixed vboxNetFltOsInitInstance which had gotten out of sync.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.2 KB
Line 
1/* $Id: VBoxNetFltInternal.h 17186 2009-02-27 00:57:39Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Internal Header.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#ifndef ___VBoxNetFltInternal_h___
23#define ___VBoxNetFltInternal_h___
24
25#include <VBox/sup.h>
26#include <VBox/intnet.h>
27#include <iprt/semaphore.h>
28#include <iprt/assert.h>
29
30
31__BEGIN_DECLS
32
33/** Pointer to the globals. */
34typedef struct VBOXNETFLTGLOBALS *PVBOXNETFLTGLOBALS;
35
36
37/**
38 * The state of a filter driver instance.
39 *
40 * The state machine differs a bit between the platforms because of
41 * the way we hook into the stack. On some hosts we can dynamically
42 * attach when required (on CreateInstance) and on others we will
43 * have to connect when the network stack is bound up. These modes
44 * are called static and dynamic config and governed at compile time
45 * by the VBOXNETFLT_STATIC_CONFIG define.
46 *
47 * See sec_netflt_msc for more details on locking and synchronization.
48 */
49typedef enum VBOXNETFTLINSSTATE
50{
51 /** The usual invalid state. */
52 kVBoxNetFltInsState_Invalid = 0,
53 /** Initialization.
54 * We've reserved the interface name but need to attach to the actual
55 * network interface outside the lock to avoid deadlocks.
56 * In the dynamic case this happens during a Create(Instance) call.
57 * In the static case it happens during driver initialization. */
58 kVBoxNetFltInsState_Initializing,
59#ifdef VBOXNETFLT_STATIC_CONFIG
60 /** Unconnected, not hooked up to a switch (static only).
61 * The filter driver instance has been instantiated and hooked up,
62 * waiting to be connected to an internal network. */
63 kVBoxNetFltInsState_Unconnected,
64#endif
65 /** Connected to an internal network. */
66 kVBoxNetFltInsState_Connected,
67 /** Disconnecting from the internal network and possibly the host network interface.
68 * Partly for reasons of deadlock avoidance again. */
69 kVBoxNetFltInsState_Disconnecting,
70#ifdef VBOXNETFLT_STATIC_CONFIG
71 /** Destroying the instance
72 * Partly for reasons of deadlock avoidance again. */
73 kVBoxNetFltInsState_Destroying,
74#endif
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 /** Active / Suspended indicator. */
116 bool volatile fActive;
117 /** Disconnected from the host network interface. */
118 bool volatile fDisconnectedFromHost;
119 /** Rediscovery is pending.
120 * cBusy will never reach zero during rediscovery, so which
121 * takes care of serializing rediscovery and disconnecting. */
122 bool volatile fRediscoveryPending;
123 /** Whether we should not attempt to set promiscuous mode at all. */
124 bool fDisablePromiscuous;
125#if (ARCH_BITS == 32) && defined(__GNUC__)
126 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
127#endif
128 /** The timestamp of the last rediscovery. */
129 uint64_t volatile NanoTSLastRediscovery;
130 /** Reference count. */
131 uint32_t volatile cRefs;
132 /** The busy count.
133 * This counts the number of current callers and pending packet. */
134 uint32_t volatile cBusy;
135 /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
136 RTSEMEVENT hEventIdle;
137
138 union
139 {
140#ifdef VBOXNETFLT_OS_SPECFIC
141 struct
142 {
143# if defined(RT_OS_DARWIN)
144 /** @name Darwin instance data.
145 * @{ */
146 /** Pointer to the darwin network interface we're attached to.
147 * This is treated as highly volatile and should only be read and retained
148 * while owning hSpinlock. Releasing references to this should not be done
149 * while owning it though as we might end up destroying it in some paths. */
150 ifnet_t volatile pIfNet;
151 /** The interface filter handle.
152 * Same access rules as with pIfNet. */
153 interface_filter_t volatile pIfFilter;
154 /** Whether we've need to set promiscuous mode when the interface comes up. */
155 bool volatile fNeedSetPromiscuous;
156 /** Whether we've successfully put the interface into to promiscuous mode.
157 * This is for dealing with the ENETDOWN case. */
158 bool volatile fSetPromiscuous;
159 /** The MAC address of the interface. */
160 RTMAC Mac;
161 /** @} */
162# elif defined(RT_OS_LINUX)
163 /** @name Linux instance data
164 * @{ */
165 /** Pointer to the device. */
166 struct net_device volatile *pDev;
167 /** Whether we've successfully put the interface into to promiscuous mode.
168 * This is for dealing with the ENETDOWN case. */
169 bool volatile fPromiscuousSet;
170 /** Whether device exists and physically attached. */
171 bool volatile fRegistered;
172 /** The MAC address of the interface. */
173 RTMAC Mac;
174 struct notifier_block Notifier;
175 struct packet_type PacketType;
176 struct sk_buff_head XmitQueue;
177 struct work_struct XmitTask;
178 /** @} */
179# elif defined(RT_OS_SOLARIS)
180 /** @name Solaris instance data.
181 * @{ */
182 /** Pointer to the bound IPv4 stream. */
183 void volatile *pvIp4Stream;
184 /** Pointer to the bound IPv6 stream. */
185 void volatile *pvIp6Stream;
186 /** Pointer to the bound ARP stream. */
187 void volatile *pvArpStream;
188 /** Pointer to the unbound promiscuous stream. */
189 void volatile *pvPromiscStream;
190 /** Layered device handle to the interface. */
191 ldi_handle_t hIface;
192 /** The MAC address of the interface. */
193 RTMAC Mac;
194 /** Mutex protection used for loopback. */
195 RTSEMFASTMUTEX hFastMtx;
196 /** @} */
197# elif defined(RT_OS_WINDOWS)
198 /** @name Windows instance data.
199 * @{ */
200 /** Filter driver device context. */
201 ADAPT IfAdaptor;
202 /** Packet worker thread info */
203 PACKET_QUEUE_WORKER PacketQueueWorker;
204 /** The MAC address of the interface. Caching MAC for performance reasons. */
205 RTMAC Mac;
206 /** mutex used to synchronize ADAPT init/deinit */
207 RTSEMMUTEX hAdaptMutex;
208 /** @} */
209# else
210# error "PORTME"
211# endif
212 } s;
213#endif
214 /** Padding. */
215#if defined(RT_OS_WINDOWS)
216# if defined(VBOX_NETFLT_ONDEMAND_BIND)
217 uint8_t abPadding[192];
218# else
219 uint8_t abPadding[1024];
220# endif
221#elif defined(RT_OS_LINUX)
222 uint8_t abPadding[320];
223#else
224 uint8_t abPadding[64];
225#endif
226 } u;
227
228 /** The interface name. */
229 char szName[1];
230} VBOXNETFLTINS;
231/** Pointer to the instance data of a host network filter driver. */
232typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
233
234AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
235#ifdef VBOXNETFLT_OS_SPECFIC
236AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
237#endif
238
239
240/**
241 * The global data of the VBox filter driver.
242 *
243 * This contains the bit required for communicating with support driver, VBoxDrv
244 * (start out as SupDrv).
245 */
246typedef struct VBOXNETFLTGLOBALS
247{
248 /** Mutex protecting the list of instances and state changes. */
249 RTSEMFASTMUTEX hFastMtx;
250 /** Pointer to a list of instance data. */
251 PVBOXNETFLTINS pInstanceHead;
252
253 /** The INTNET trunk network interface factory. */
254 INTNETTRUNKFACTORY TrunkFactory;
255 /** The SUPDRV component factory registration. */
256 SUPDRVFACTORY SupDrvFactory;
257 /** The number of current factory references. */
258 int32_t volatile cFactoryRefs;
259 /** Whether the IDC connection is open or not.
260 * This is only for cleaning up correctly after the separate IDC init on Windows. */
261 bool fIDCOpen;
262 /** The SUPDRV IDC handle (opaque struct). */
263 SUPDRVIDCHANDLE SupDrvIDC;
264
265#ifdef VBOXNETFLT_STATIC_CONFIG
266 /** Something we can block on while waiting for an instance to be unlinked. */
267 RTSEMEVENT hBlockEvent;
268#endif
269} VBOXNETFLTGLOBALS;
270
271
272DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
273DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
274DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
275DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
276DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
277DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
278
279DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
280DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
281
282DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
283DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
284
285#ifdef VBOXNETFLT_STATIC_CONFIG
286DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
287#endif
288
289
290
291/** @name The OS specific interface.
292 * @{ */
293/**
294 * Try rediscover the host interface.
295 *
296 * This is called periodically from the transmit path if we're marked as
297 * disconnected from the host. There is no chance of a race here.
298 *
299 * @returns true if the interface was successfully rediscovered and reattach,
300 * otherwise false.
301 * @param pThis The new instance.
302 */
303DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
304
305/**
306 * Transmits a frame.
307 *
308 * @return IPRT status code.
309 * @param pThis The new instance.
310 * @param pSG The (scatter/)gather list.
311 * @param fDst The destination mask. At least one bit will be set.
312 *
313 * @remarks Owns the out-bound trunk port semaphore.
314 */
315DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst);
316
317/**
318 * Checks if the interface is in promiscuous mode from the host perspective.
319 *
320 * If it is, then the internal networking switch will send frames
321 * heading for the wire to the host as well.
322 *
323 * @see INTNETTRUNKIFPORT::pfnIsPromiscuous for more details.
324 *
325 * @returns true / false accordingly.
326 * @param pThis The instance.
327 *
328 * @remarks Owns the network lock and the out-bound trunk port semaphores.
329 */
330DECLHIDDEN(bool) vboxNetFltPortOsIsPromiscuous(PVBOXNETFLTINS pThis);
331
332/**
333 * Get the MAC address of the interface we're attached to.
334 *
335 * Used by the internal networking switch for implementing the
336 * shared-MAC-on-the-wire mode.
337 *
338 * @param pThis The instance.
339 * @param pMac Where to store the MAC address.
340 * If you don't know, set all the bits except the first (the multicast one).
341 *
342 * @remarks Owns the network lock and the out-bound trunk port semaphores.
343 */
344DECLHIDDEN(void) vboxNetFltPortOsGetMacAddress(PVBOXNETFLTINS pThis, PRTMAC pMac);
345
346/**
347 * Checks if the specified MAC address is for any of the host interfaces.
348 *
349 * Used by the internal networking switch to decide the destination(s)
350 * of a frame.
351 *
352 * @returns true / false accordingly.
353 * @param pThis The instance.
354 * @param pMac The MAC address.
355 *
356 * @remarks Owns the network lock and the out-bound trunk port semaphores.
357 */
358DECLHIDDEN(bool) vboxNetFltPortOsIsHostMac(PVBOXNETFLTINS pThis, PCRTMAC pMac);
359
360/**
361 * This is called when activating or suspending the instance.
362 *
363 * Use this method to enable and disable promiscuous mode on
364 * the interface to prevent unnecessary interrupt load.
365 *
366 * It is only called when the state changes.
367 *
368 * @param pThis The instance.
369 *
370 * @remarks Owns the lock for the out-bound trunk port.
371 */
372DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
373
374/**
375 * This is called to when disconnecting from a network.
376 *
377 * @return IPRT status code.
378 * @param pThis The new instance.
379 *
380 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
381 */
382DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
383
384/**
385 * This is called to when connecting to a network.
386 *
387 * @return IPRT status code.
388 * @param pThis The new instance.
389 *
390 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
391 */
392DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
393
394/**
395 * Counter part to vboxNetFltOsInitInstance().
396 *
397 * @return IPRT status code.
398 * @param pThis The new instance.
399 *
400 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
401 */
402DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
403
404/**
405 * This is called to attach to the actual host interface
406 * after linking the instance into the list.
407 *
408 * @return IPRT status code.
409 * @param pThis The new instance.
410 * @param pvContext The user supplied context in the static config only.
411 * NULL in the dynamic config.
412 *
413 * @remarks Owns no locks.
414 */
415DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
416
417/**
418 * This is called to perform structure initializations.
419 *
420 * @return IPRT status code.
421 * @param pThis The new instance.
422 *
423 * @remarks Owns no locks.
424 */
425DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
426/** @} */
427
428
429__END_DECLS
430
431#endif
432
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