VirtualBox

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

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

INTNETR0IfWait fix, NetFlt/win fixes

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette