VirtualBox

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

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

fixed OSE headers for linux/darwin/solaris netfilter code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.5 KB
Line 
1/* $Id: VBoxNetFltInternal.h 15527 2008-12-15 18:11:08Z 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//# ifdef VBOX_NETFLT_ONDEMAND_BIND
194 /** Filter driver device context. */
195 ADAPT IfAdaptor;
196//# else
197// /** Pointer to the filter driver device context. */
198// PADAPT volatile pIfAdaptor;
199//# endif
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
254 /** The SUPDRV IDC handle (opaque struct). */
255 SUPDRVIDCHANDLE SupDrvIDC;
256} VBOXNETFLTGLOBALS;
257
258
259DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
260DECLHIDDEN(int) vboxNetFltTryDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
261DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
262DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
263
264DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
265DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
266
267#ifdef VBOXNETFLT_STATIC_CONFIG
268DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
269DECLHIDDEN(int) vboxNetFltInitGlobalsBase(PVBOXNETFLTGLOBALS pGlobals);
270DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
271DECLHIDDEN(void) vboxNetFltDeleteGlobalsBase(PVBOXNETFLTGLOBALS pGlobals);
272DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
273#endif
274
275
276/** @name The OS specific interface.
277 * @{ */
278/**
279 * Try rediscover the host interface.
280 *
281 * This is called periodically from the transmit path if we're marked as
282 * disconnected from the host. There is no chance of a race here.
283 *
284 * @returns true if the interface was successfully rediscovered and reattach,
285 * otherwise false.
286 * @param pThis The new instance.
287 */
288DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
289
290/**
291 * Transmits a frame.
292 *
293 * @return IPRT status code.
294 * @param pThis The new instance.
295 * @param pSG The (scatter/)gather list.
296 * @param fDst The destination mask. At least one bit will be set.
297 *
298 * @remarks Owns the out-bound trunk port semaphore.
299 */
300DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst);
301
302/**
303 * Checks if the interface is in promiscuous mode from the host perspective.
304 *
305 * If it is, then the internal networking switch will send frames
306 * heading for the wire to the host as well.
307 *
308 * @see INTNETTRUNKIFPORT::pfnIsPromiscuous for more details.
309 *
310 * @returns true / false accordingly.
311 * @param pThis The instance.
312 *
313 * @remarks Owns the network lock and the out-bound trunk port semaphores.
314 */
315DECLHIDDEN(bool) vboxNetFltPortOsIsPromiscuous(PVBOXNETFLTINS pThis);
316
317/**
318 * Get the MAC address of the interface we're attached to.
319 *
320 * Used by the internal networking switch for implementing the
321 * shared-MAC-on-the-wire mode.
322 *
323 * @param pThis The instance.
324 * @param pMac Where to store the MAC address.
325 * If you don't know, set all the bits except the first (the multicast one).
326 *
327 * @remarks Owns the network lock and the out-bound trunk port semaphores.
328 */
329DECLHIDDEN(void) vboxNetFltPortOsGetMacAddress(PVBOXNETFLTINS pThis, PRTMAC pMac);
330
331/**
332 * Checks if the specified MAC address is for any of the host interfaces.
333 *
334 * Used by the internal networking switch to decide the destination(s)
335 * of a frame.
336 *
337 * @returns true / false accordingly.
338 * @param pThis The instance.
339 * @param pMac The MAC address.
340 *
341 * @remarks Owns the network lock and the out-bound trunk port semaphores.
342 */
343DECLHIDDEN(bool) vboxNetFltPortOsIsHostMac(PVBOXNETFLTINS pThis, PCRTMAC pMac);
344
345/**
346 * This is called when activating or suspending the instance.
347 *
348 * Use this method to enable and disable promiscuous mode on
349 * the interface to prevent unnecessary interrupt load.
350 *
351 * It is only called when the state changes.
352 *
353 * @param pThis The instance.
354 *
355 * @remarks Owns the lock for the out-bound trunk port.
356 */
357DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
358
359/**
360 * This is called to when disconnecting from a network.
361 *
362 * @return IPRT status code.
363 * @param pThis The new instance.
364 *
365 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
366 */
367DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
368
369/**
370 * This is called to when connecting to a network.
371 *
372 * @return IPRT status code.
373 * @param pThis The new instance.
374 *
375 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
376 */
377DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
378
379/**
380 * Counter part to vboxNetFltOsInitInstance().
381 *
382 * @return IPRT status code.
383 * @param pThis The new instance.
384 *
385 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
386 */
387DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
388
389/**
390 * This is called to attach to the actual host interface
391 * after linking the instance into the list.
392 *
393 * @return IPRT status code.
394 * @param pThis The new instance.
395 *
396 * @remarks Owns no locks.
397 */
398DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis
399#ifdef VBOXNETFLT_STATIC_CONFIG
400 , void * pContext
401#endif
402 );
403
404/**
405 * This is called to perform structure initializations.
406 *
407 * @return IPRT status code.
408 * @param pThis The new instance.
409 *
410 * @remarks Owns no locks.
411 */
412DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
413/** @} */
414
415
416__END_DECLS
417
418#endif
419
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