VirtualBox

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

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

NetFlt/win: concurency issue fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.7 KB
Line 
1/* $Id: VBoxNetFltInternal.h 15917 2009-01-13 14:45:18Z 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 /** Destroying the instance
71 * Partly for reasons of deadlock avoidance again. */
72 kVBoxNetFltInsState_Destroying,
73 /** The instance has been disconnected from both the host and the internal network. */
74 kVBoxNetFltInsState_Destroyed,
75
76 /** The habitual 32-bit enum hack. */
77 kVBoxNetFltInsState_32BitHack = 0x7fffffff
78} VBOXNETFTLINSSTATE;
79
80
81/**
82 * The per-instance data of the VBox filter driver.
83 *
84 * This is data associated with a network interface / NIC / wossname which
85 * the filter driver has been or may be attached to. When possible it is
86 * attached dynamically, but this may not be possible on all OSes so we have
87 * to be flexible about things.
88 *
89 * A network interface / NIC / wossname can only have one filter driver
90 * instance attached to it. So, attempts at connecting an internal network
91 * to an interface that's already in use (connected to another internal network)
92 * will result in a VERR_SHARING_VIOLATION.
93 *
94 * Only one internal network can connect to a filter driver instance.
95 */
96typedef struct VBOXNETFLTINS
97{
98 /** Pointer to the next interface in the list. (VBOXNETFLTGLOBAL::pInstanceHead) */
99 struct VBOXNETFLTINS *pNext;
100 /** Our RJ-45 port.
101 * This is what the internal network plugs into. */
102 INTNETTRUNKIFPORT MyPort;
103 /** The RJ-45 port on the INTNET "switch".
104 * This is what we're connected to. */
105 PINTNETTRUNKSWPORT pSwitchPort;
106 /** Pointer to the globals. */
107 PVBOXNETFLTGLOBALS pGlobals;
108
109 /** The spinlock protecting the state variables and host interface handle. */
110 RTSPINLOCK hSpinlock;
111 /** The current interface state. */
112 VBOXNETFTLINSSTATE volatile enmState;
113 /** Active / Suspended indicator. */
114 bool volatile fActive;
115 /** Disconnected from the host network interface. */
116 bool volatile fDisconnectedFromHost;
117 /** Rediscovery is pending.
118 * cBusy will never reach zero during rediscovery, so which
119 * takes care of serializing rediscovery and disconnecting. */
120 bool volatile fRediscoveryPending;
121#if (ARCH_BITS == 32) && defined(__GNUC__)
122 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
123#endif
124 /** The timestamp of the last rediscovery. */
125 uint64_t volatile NanoTSLastRediscovery;
126 /** Reference count. */
127 uint32_t volatile cRefs;
128 /** The busy count.
129 * This counts the number of current callers and pending packet. */
130 uint32_t volatile cBusy;
131 /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
132 RTSEMEVENT hEventIdle;
133
134 union
135 {
136#ifdef VBOXNETFLT_OS_SPECFIC
137 struct
138 {
139# if defined(RT_OS_DARWIN)
140 /** @name Darwin instance data.
141 * @{ */
142 /** Pointer to the darwin network interface we're attached to.
143 * This is treated as highly volatile and should only be read and retained
144 * while owning hSpinlock. Releasing references to this should not be done
145 * while owning it though as we might end up destroying it in some paths. */
146 ifnet_t volatile pIfNet;
147 /** The interface filter handle.
148 * Same access rules as with pIfNet. */
149 interface_filter_t volatile pIfFilter;
150 /** Whether we've need to set promiscuous mode when the interface comes up. */
151 bool volatile fNeedSetPromiscuous;
152 /** Whether we've successfully put the interface into to promiscuous mode.
153 * This is for dealing with the ENETDOWN case. */
154 bool volatile fSetPromiscuous;
155 /** The MAC address of the interface. */
156 RTMAC Mac;
157 /** @} */
158# elif defined(RT_OS_LINUX)
159 /** @name Linux instance data
160 * @{ */
161 /** Pointer to the device. */
162 struct net_device volatile *pDev;
163 /** Whether we've successfully put the interface into to promiscuous mode.
164 * This is for dealing with the ENETDOWN case. */
165 bool volatile fPromiscuousSet;
166 /** Whether device exists and physically attached. */
167 bool volatile fRegistered;
168 /** The MAC address of the interface. */
169 RTMAC Mac;
170 struct notifier_block Notifier;
171 struct packet_type PacketType;
172 struct sk_buff_head XmitQueue;
173 struct work_struct XmitTask;
174 /** @} */
175# elif defined(RT_OS_SOLARIS)
176 /** @name Solaris instance data.
177 * @{ */
178 /** Pointer to the bound IPv4 stream. */
179 void volatile *pvIp4Stream;
180 /** Pointer to the bound IPv6 stream. */
181 void volatile *pvIp6Stream;
182 /** Pointer to the bound ARP stream. */
183 void volatile *pvArpStream;
184 /** Pointer to the unbound promiscuous stream. */
185 void volatile *pvPromiscStream;
186 /** Layered device handle to the interface. */
187 ldi_handle_t hIface;
188 /** The MAC address of the interface. */
189 RTMAC Mac;
190 /** Mutex protection used for loopback. */
191 RTSEMFASTMUTEX hFastMtx;
192 /** @} */
193# elif defined(RT_OS_WINDOWS)
194 /** @name Windows instance data.
195 * @{ */
196 /** Filter driver device context. */
197 ADAPT IfAdaptor;
198 /** Packet worker thread info */
199 PACKET_QUEUE_WORKER PacketQueueWorker;
200 /** The MAC address of the interface. Caching MAC for performance reasons. */
201 RTMAC Mac;
202 /** @} */
203# else
204# error "PORTME"
205# endif
206 } s;
207#endif
208 /** Padding. */
209#if defined(RT_OS_WINDOWS)
210# if defined(VBOX_NETFLT_ONDEMAND_BIND)
211 uint8_t abPadding[192];
212# else
213 uint8_t abPadding[1024];
214# endif
215#elif defined(RT_OS_LINUX)
216 uint8_t abPadding[320];
217#else
218 uint8_t abPadding[64];
219#endif
220 } u;
221
222 /** The interface name. */
223 char szName[1];
224} VBOXNETFLTINS;
225/** Pointer to the instance data of a host network filter driver. */
226typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
227
228AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
229#ifdef VBOXNETFLT_OS_SPECFIC
230AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
231#endif
232
233
234/**
235 * The global data of the VBox filter driver.
236 *
237 * This contains the bit required for communicating with support driver, VBoxDrv
238 * (start out as SupDrv).
239 */
240typedef struct VBOXNETFLTGLOBALS
241{
242 /** Mutex protecting the list of instances and state changes. */
243 RTSEMFASTMUTEX hFastMtx;
244 /** Pointer to a list of instance data. */
245 PVBOXNETFLTINS pInstanceHead;
246
247 /** The INTNET trunk network interface factory. */
248 INTNETTRUNKFACTORY TrunkFactory;
249 /** The SUPDRV component factory registration. */
250 SUPDRVFACTORY SupDrvFactory;
251 /** The number of current factory references. */
252 int32_t volatile cFactoryRefs;
253#ifdef VBOXNETFLT_STATIC_CONFIG
254 /* wait timer event */
255 RTSEMEVENT hTimerEvent;
256#endif
257 /** The SUPDRV IDC handle (opaque struct). */
258 SUPDRVIDCHANDLE SupDrvIDC;
259} VBOXNETFLTGLOBALS;
260
261
262DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
263DECLHIDDEN(int) vboxNetFltTryDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
264DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
265DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
266
267DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
268DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
269
270#ifdef VBOXNETFLT_STATIC_CONFIG
271DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
272DECLHIDDEN(int) vboxNetFltInitGlobalsBase(PVBOXNETFLTGLOBALS pGlobals);
273DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
274DECLHIDDEN(void) vboxNetFltDeleteGlobalsBase(PVBOXNETFLTGLOBALS pGlobals);
275DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
276#endif
277
278
279/** @name The OS specific interface.
280 * @{ */
281/**
282 * Try rediscover the host interface.
283 *
284 * This is called periodically from the transmit path if we're marked as
285 * disconnected from the host. There is no chance of a race here.
286 *
287 * @returns true if the interface was successfully rediscovered and reattach,
288 * otherwise false.
289 * @param pThis The new instance.
290 */
291DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
292
293/**
294 * Transmits a frame.
295 *
296 * @return IPRT status code.
297 * @param pThis The new instance.
298 * @param pSG The (scatter/)gather list.
299 * @param fDst The destination mask. At least one bit will be set.
300 *
301 * @remarks Owns the out-bound trunk port semaphore.
302 */
303DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst);
304
305/**
306 * Checks if the interface is in promiscuous mode from the host perspective.
307 *
308 * If it is, then the internal networking switch will send frames
309 * heading for the wire to the host as well.
310 *
311 * @see INTNETTRUNKIFPORT::pfnIsPromiscuous for more details.
312 *
313 * @returns true / false accordingly.
314 * @param pThis The instance.
315 *
316 * @remarks Owns the network lock and the out-bound trunk port semaphores.
317 */
318DECLHIDDEN(bool) vboxNetFltPortOsIsPromiscuous(PVBOXNETFLTINS pThis);
319
320/**
321 * Get the MAC address of the interface we're attached to.
322 *
323 * Used by the internal networking switch for implementing the
324 * shared-MAC-on-the-wire mode.
325 *
326 * @param pThis The instance.
327 * @param pMac Where to store the MAC address.
328 * If you don't know, set all the bits except the first (the multicast one).
329 *
330 * @remarks Owns the network lock and the out-bound trunk port semaphores.
331 */
332DECLHIDDEN(void) vboxNetFltPortOsGetMacAddress(PVBOXNETFLTINS pThis, PRTMAC pMac);
333
334/**
335 * Checks if the specified MAC address is for any of the host interfaces.
336 *
337 * Used by the internal networking switch to decide the destination(s)
338 * of a frame.
339 *
340 * @returns true / false accordingly.
341 * @param pThis The instance.
342 * @param pMac The MAC address.
343 *
344 * @remarks Owns the network lock and the out-bound trunk port semaphores.
345 */
346DECLHIDDEN(bool) vboxNetFltPortOsIsHostMac(PVBOXNETFLTINS pThis, PCRTMAC pMac);
347
348/**
349 * This is called when activating or suspending the instance.
350 *
351 * Use this method to enable and disable promiscuous mode on
352 * the interface to prevent unnecessary interrupt load.
353 *
354 * It is only called when the state changes.
355 *
356 * @param pThis The instance.
357 *
358 * @remarks Owns the lock for the out-bound trunk port.
359 */
360DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
361
362/**
363 * This is called to when disconnecting from a network.
364 *
365 * @return IPRT status code.
366 * @param pThis The new instance.
367 *
368 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
369 */
370DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
371
372/**
373 * This is called to when connecting to a network.
374 *
375 * @return IPRT status code.
376 * @param pThis The new instance.
377 *
378 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
379 */
380DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
381
382/**
383 * Counter part to vboxNetFltOsInitInstance().
384 *
385 * @return IPRT status code.
386 * @param pThis The new instance.
387 *
388 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
389 */
390DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
391
392/**
393 * This is called to attach to the actual host interface
394 * after linking the instance into the list.
395 *
396 * @return IPRT status code.
397 * @param pThis The new instance.
398 *
399 * @remarks Owns no locks.
400 */
401DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis
402#ifdef VBOXNETFLT_STATIC_CONFIG
403 , void * pContext
404#endif
405 );
406
407/**
408 * This is called to perform structure initializations.
409 *
410 * @return IPRT status code.
411 * @param pThis The new instance.
412 *
413 * @remarks Owns no locks.
414 */
415DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
416/** @} */
417
418
419__END_DECLS
420
421#endif
422
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