VirtualBox

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

Last change on this file since 106401 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

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