VirtualBox

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

Last change on this file since 25316 was 25302, checked in by vboxsync, 15 years ago

Solaris/VBoxBow: crossbow netfilter initial commit, rx works, no tx.

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