VirtualBox

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

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

#3419: Added fNoPromisc parameter to pfnCreateAndConnect to prevent going promisc on WiFi.

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