VirtualBox

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

Last change on this file since 93668 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.9 KB
Line 
1/* $Id: VBoxNetFltInternal.h 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Internal Header.
4 */
5
6/*
7 * Copyright (C) 2008-2022 Oracle Corporation
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef VBOX_INCLUDED_SRC_VBoxNetFlt_VBoxNetFltInternal_h
28#define VBOX_INCLUDED_SRC_VBoxNetFlt_VBoxNetFltInternal_h
29#ifndef RT_WITHOUT_PRAGMA_ONCE
30# pragma once
31#endif
32
33#include <VBox/sup.h>
34#include <VBox/intnet.h>
35#include <iprt/semaphore.h>
36#include <iprt/assert.h>
37
38
39RT_C_DECLS_BEGIN
40
41/** Pointer to the globals. */
42typedef struct VBOXNETFLTGLOBALS *PVBOXNETFLTGLOBALS;
43
44
45/**
46 * The state of a filter driver instance.
47 *
48 * The state machine differs a bit between the platforms because of
49 * the way we hook into the stack. On some hosts we can dynamically
50 * attach when required (on CreateInstance) and on others we will
51 * have to connect when the network stack is bound up. These modes
52 * are called static and dynamic config and governed at compile time
53 * by the VBOXNETFLT_STATIC_CONFIG define.
54 *
55 * See sec_netflt_msc for more details on locking and synchronization.
56 */
57typedef enum VBOXNETFTLINSSTATE
58{
59 /** The usual invalid state. */
60 kVBoxNetFltInsState_Invalid = 0,
61 /** Initialization.
62 * We've reserved the interface name but need to attach to the actual
63 * network interface outside the lock to avoid deadlocks.
64 * In the dynamic case this happens during a Create(Instance) call.
65 * In the static case it happens during driver initialization. */
66 kVBoxNetFltInsState_Initializing,
67#ifdef VBOXNETFLT_STATIC_CONFIG
68 /** Unconnected, not hooked up to a switch (static only).
69 * The filter driver instance has been instantiated and hooked up,
70 * waiting to be connected to an internal network. */
71 kVBoxNetFltInsState_Unconnected,
72#endif
73 /** Connected to an internal network. */
74 kVBoxNetFltInsState_Connected,
75 /** Disconnecting from the internal network and possibly the host network interface.
76 * Partly for reasons of deadlock avoidance again. */
77 kVBoxNetFltInsState_Disconnecting,
78 /** The instance has been disconnected from both the host and the internal network. */
79 kVBoxNetFltInsState_Destroyed,
80
81 /** The habitual 32-bit enum hack. */
82 kVBoxNetFltInsState_32BitHack = 0x7fffffff
83} VBOXNETFTLINSSTATE;
84
85
86/**
87 * The per-instance data of the VBox filter driver.
88 *
89 * This is data associated with a network interface / NIC / wossname which
90 * the filter driver has been or may be attached to. When possible it is
91 * attached dynamically, but this may not be possible on all OSes so we have
92 * to be flexible about things.
93 *
94 * A network interface / NIC / wossname can only have one filter driver
95 * instance attached to it. So, attempts at connecting an internal network
96 * to an interface that's already in use (connected to another internal network)
97 * will result in a VERR_SHARING_VIOLATION.
98 *
99 * Only one internal network can connect to a filter driver instance.
100 */
101typedef struct VBOXNETFLTINS
102{
103 /** Pointer to the next interface in the list. (VBOXNETFLTGLOBAL::pInstanceHead) */
104 struct VBOXNETFLTINS *pNext;
105 /** Our RJ-45 port.
106 * This is what the internal network plugs into. */
107 INTNETTRUNKIFPORT MyPort;
108 /** The RJ-45 port on the INTNET "switch".
109 * This is what we're connected to. */
110 PINTNETTRUNKSWPORT pSwitchPort;
111 /** Pointer to the globals. */
112 PVBOXNETFLTGLOBALS pGlobals;
113
114 /** The spinlock protecting the state variables and host interface handle. */
115 RTSPINLOCK hSpinlock;
116 /** The current interface state. */
117 VBOXNETFTLINSSTATE volatile enmState;
118 /** The trunk state. */
119 INTNETTRUNKIFSTATE volatile enmTrunkState;
120 bool volatile fActive;
121 /** Disconnected from the host network interface. */
122 bool volatile fDisconnectedFromHost;
123 /** Rediscovery is pending.
124 * cBusy will never reach zero during rediscovery, so which
125 * takes care of serializing rediscovery and disconnecting. */
126 bool volatile fRediscoveryPending;
127 /** Whether we should not attempt to set promiscuous mode at all. */
128 bool fDisablePromiscuous;
129#if (ARCH_BITS == 32) && defined(__GNUC__)
130#if 0
131 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
132#endif
133#endif
134 /** The timestamp of the last rediscovery. */
135 uint64_t volatile NanoTSLastRediscovery;
136 /** Reference count. */
137 uint32_t volatile cRefs;
138 /** The busy count.
139 * This counts the number of current callers and pending packet. */
140 uint32_t volatile cBusy;
141 /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
142 RTSEMEVENT hEventIdle;
143
144 /** @todo move MacAddr out of this structure! */
145 union
146 {
147#ifdef VBOXNETFLT_OS_SPECFIC
148 struct
149 {
150# if defined(RT_OS_DARWIN)
151 /** @name Darwin instance data.
152 * @{ */
153 /** Pointer to the darwin network interface we're attached to.
154 * This is treated as highly volatile and should only be read and retained
155 * while owning hSpinlock. Releasing references to this should not be done
156 * while owning it though as we might end up destroying it in some paths. */
157 ifnet_t volatile pIfNet;
158 /** The interface filter handle.
159 * Same access rules as with pIfNet. */
160 interface_filter_t volatile pIfFilter;
161 /** Whether we've need to set promiscuous mode when the interface comes up. */
162 bool volatile fNeedSetPromiscuous;
163 /** Whether we've successfully put the interface into to promiscuous mode.
164 * This is for dealing with the ENETDOWN case. */
165 bool volatile fSetPromiscuous;
166 /** The MAC address of the interface. */
167 RTMAC MacAddr;
168 /** PF_SYSTEM socket to listen for events (XXX: globals?) */
169 socket_t pSysSock;
170 /** @} */
171# elif defined(RT_OS_LINUX)
172 /** @name Linux instance data
173 * @{ */
174 /** Pointer to the device. */
175 struct net_device * volatile pDev;
176 /** MTU of host's interface. */
177 uint32_t cbMtu;
178 /** Whether we've successfully put the interface into to promiscuous mode.
179 * This is for dealing with the ENETDOWN case. */
180 bool volatile fPromiscuousSet;
181 /** Whether device exists and physically attached. */
182 bool volatile fRegistered;
183 /** Whether our packet handler is installed. */
184 bool volatile fPacketHandler;
185 /** The MAC address of the interface. */
186 RTMAC MacAddr;
187 struct notifier_block Notifier; /* netdevice */
188 struct notifier_block NotifierIPv4;
189 struct notifier_block NotifierIPv6;
190 struct packet_type PacketType;
191# ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
192 struct sk_buff_head XmitQueue;
193 struct work_struct XmitTask;
194# endif
195 /** @} */
196# elif defined(RT_OS_SOLARIS)
197 /** @name Solaris instance data.
198 * @{ */
199# ifdef VBOX_WITH_NETFLT_CROSSBOW
200 /** Whether the underlying interface is a VNIC or not. */
201 bool fIsVNIC;
202 /** Whether the underlying interface is a VNIC template or not. */
203 bool fIsVNICTemplate;
204 /** Handle to list of created VNICs. */
205 list_t hVNICs;
206 /** The MAC address of the host interface. */
207 RTMAC MacAddr;
208 /** Handle of this interface (lower MAC). */
209 mac_handle_t hInterface;
210 /** Handle to link state notifier. */
211 mac_notify_handle_t hNotify;
212# else
213 /** Pointer to the bound IPv4 stream. */
214 struct vboxnetflt_stream_t * volatile pIp4Stream;
215 /** Pointer to the bound IPv6 stream. */
216 struct vboxnetflt_stream_t * volatile pIp6Stream;
217 /** Pointer to the bound ARP stream. */
218 struct vboxnetflt_stream_t * volatile pArpStream;
219 /** Pointer to the unbound promiscuous stream. */
220 struct vboxnetflt_promisc_stream_t * volatile pPromiscStream;
221 /** Whether we are attaching to IPv6 stream dynamically now. */
222 bool volatile fAttaching;
223 /** Whether this is a VLAN interface or not. */
224 bool volatile fVLAN;
225 /** Layered device handle to the interface. */
226 ldi_handle_t hIface;
227 /** The MAC address of the interface. */
228 RTMAC MacAddr;
229 /** Mutex protection used for loopback. */
230 kmutex_t hMtx;
231 /** Mutex protection used for dynamic IPv6 attaches. */
232 RTSEMFASTMUTEX hPollMtx;
233# endif
234 /** @} */
235# elif defined(RT_OS_FREEBSD)
236 /** @name FreeBSD instance data.
237 * @{ */
238 /** Interface handle */
239 struct ifnet *ifp;
240 /** Netgraph node handle */
241 node_p node;
242 /** Input hook */
243 hook_p input;
244 /** Output hook */
245 hook_p output;
246 /** Original interface flags */
247 unsigned int flags;
248 /** Input queue */
249 struct ifqueue inq;
250 /** Output queue */
251 struct ifqueue outq;
252 /** Input task */
253 struct task tskin;
254 /** Output task */
255 struct task tskout;
256 /** The MAC address of the interface. */
257 RTMAC MacAddr;
258 /** @} */
259# elif defined(RT_OS_WINDOWS)
260 /** @name Windows instance data.
261 * @{ */
262 /** Filter driver device context. */
263 VBOXNETFLTWIN WinIf;
264
265 volatile uint32_t cModeNetFltRefs;
266 volatile uint32_t cModePassThruRefs;
267#ifndef VBOXNETFLT_NO_PACKET_QUEUE
268 /** Packet worker thread info */
269 PACKET_QUEUE_WORKER PacketQueueWorker;
270#endif
271 /** The MAC address of the interface. Caching MAC for performance reasons. */
272 RTMAC MacAddr;
273 /** mutex used to synchronize WinIf init/deinit */
274 RTSEMMUTEX hWinIfMutex;
275 /** @} */
276# else
277# error "PORTME"
278# endif
279 } s;
280#endif
281 /** Padding. */
282#if defined(RT_OS_WINDOWS)
283# if defined(VBOX_NETFLT_ONDEMAND_BIND)
284 uint8_t abPadding[192];
285# elif defined(VBOXNETADP)
286 uint8_t abPadding[256];
287# else
288 uint8_t abPadding[1024];
289# endif
290#elif defined(RT_OS_LINUX)
291 uint8_t abPadding[320];
292#elif defined(RT_OS_FREEBSD)
293 uint8_t abPadding[320];
294#else
295 uint8_t abPadding[128];
296#endif
297 } u;
298
299 /** The interface name. */
300 char szName[1];
301} VBOXNETFLTINS;
302/** Pointer to the instance data of a host network filter driver. */
303typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
304
305AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
306#ifdef VBOXNETFLT_OS_SPECFIC
307AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
308#endif
309
310
311/**
312 * The global data of the VBox filter driver.
313 *
314 * This contains the bit required for communicating with support driver, VBoxDrv
315 * (start out as SupDrv).
316 */
317typedef struct VBOXNETFLTGLOBALS
318{
319 /** Mutex protecting the list of instances and state changes. */
320 RTSEMFASTMUTEX hFastMtx;
321 /** Pointer to a list of instance data. */
322 PVBOXNETFLTINS pInstanceHead;
323
324 /** The INTNET trunk network interface factory. */
325 INTNETTRUNKFACTORY TrunkFactory;
326 /** The SUPDRV component factory registration. */
327 SUPDRVFACTORY SupDrvFactory;
328 /** The number of current factory references. */
329 int32_t volatile cFactoryRefs;
330 /** Whether the IDC connection is open or not.
331 * This is only for cleaning up correctly after the separate IDC init on Windows. */
332 bool fIDCOpen;
333 /** The SUPDRV IDC handle (opaque struct). */
334 SUPDRVIDCHANDLE SupDrvIDC;
335} VBOXNETFLTGLOBALS;
336
337
338DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
339DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
340DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
341DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
342DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
343DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
344
345DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
346DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
347
348DECL_HIDDEN_CALLBACK(void) vboxNetFltPortReleaseBusy(PINTNETTRUNKIFPORT pIfPort);
349DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
350DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
351DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
352DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
353
354#ifdef VBOXNETFLT_STATIC_CONFIG
355DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
356#endif
357
358
359
360/** @name The OS specific interface.
361 * @{ */
362/**
363 * Try rediscover the host interface.
364 *
365 * This is called periodically from the transmit path if we're marked as
366 * disconnected from the host. There is no chance of a race here.
367 *
368 * @returns true if the interface was successfully rediscovered and reattach,
369 * otherwise false.
370 * @param pThis The new instance.
371 */
372DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
373
374/**
375 * Transmits a frame.
376 *
377 * @return IPRT status code.
378 * @param pThis The new instance.
379 * @param pvIfData Pointer to the host-private interface data.
380 * @param pSG The (scatter/)gather list.
381 * @param fDst The destination mask. At least one bit will be set.
382 *
383 * @remarks Owns the out-bound trunk port semaphore.
384 */
385DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
386
387/**
388 * This is called when activating or suspending the instance.
389 *
390 * Use this method to enable and disable promiscuous mode on
391 * the interface to prevent unnecessary interrupt load.
392 *
393 * It is only called when the state changes.
394 *
395 * @param pThis The instance.
396 * @param fActive Whether to active (@c true) or deactive.
397 *
398 * @remarks Owns the lock for the out-bound trunk port.
399 */
400DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
401
402/**
403 * This is called when a network interface has obtained a new MAC address.
404 *
405 * @param pThis The instance.
406 * @param pvIfData Pointer to the private interface data.
407 * @param pMac Pointer to the new MAC address.
408 */
409DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
410
411/**
412 * This is called when an interface is connected to the network.
413 *
414 * @return IPRT status code.
415 * @param pThis The instance.
416 * @param pvIf Pointer to the interface.
417 * @param ppvIfData Where to store the private interface data.
418 */
419DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
420
421/**
422 * This is called when a VM host disconnects from the network.
423 *
424 * @param pThis The instance.
425 * @param pvIfData Pointer to the private interface data.
426 */
427DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
428
429/**
430 * This is called to when disconnecting from a network.
431 *
432 * @return IPRT status code.
433 * @param pThis The new instance.
434 *
435 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
436 */
437DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
438
439/**
440 * This is called to when connecting to a network.
441 *
442 * @return IPRT status code.
443 * @param pThis The new instance.
444 *
445 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
446 */
447DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
448
449/**
450 * Counter part to vboxNetFltOsInitInstance().
451 *
452 * @return IPRT status code.
453 * @param pThis The new instance.
454 *
455 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
456 */
457DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
458
459/**
460 * This is called to attach to the actual host interface
461 * after linking the instance into the list.
462 *
463 * The MAC address as well promiscuousness and GSO capabilities should be
464 * reported by this function.
465 *
466 * @return IPRT status code.
467 * @param pThis The new instance.
468 * @param pvContext The user supplied context in the static config only.
469 * NULL in the dynamic config.
470 *
471 * @remarks Owns no locks.
472 */
473DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
474
475/**
476 * This is called to perform structure initializations.
477 *
478 * @return IPRT status code.
479 * @param pThis The new instance.
480 *
481 * @remarks Owns no locks.
482 */
483DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
484/** @} */
485
486
487RT_C_DECLS_END
488
489#endif /* !VBOX_INCLUDED_SRC_VBoxNetFlt_VBoxNetFltInternal_h */
490
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