VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/SrvIntNetR0.cpp@ 46904

Last change on this file since 46904 was 46904, checked in by vboxsync, 12 years ago

IntNet, VirtioNet, NetFilter: Large frame support + drop oversized + UFO fix (#6821)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 227.0 KB
Line 
1/* $Id: SrvIntNetR0.cpp 46904 2013-07-02 12:59:56Z vboxsync $ */
2/** @file
3 * Internal networking - The ring 0 service.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_SRV_INTNET
23#include <VBox/intnet.h>
24#include <VBox/intnetinline.h>
25#include <VBox/vmm/pdmnetinline.h>
26#include <VBox/sup.h>
27#include <VBox/vmm/pdm.h>
28#include <VBox/log.h>
29
30#include <iprt/asm.h>
31#include <iprt/assert.h>
32#include <iprt/handletable.h>
33#include <iprt/mp.h>
34#include <iprt/mem.h>
35#include <iprt/net.h>
36#include <iprt/semaphore.h>
37#include <iprt/spinlock.h>
38#include <iprt/string.h>
39#include <iprt/thread.h>
40#include <iprt/time.h>
41
42
43/*******************************************************************************
44* Defined Constants And Macros *
45*******************************************************************************/
46/** @def INTNET_WITH_DHCP_SNOOPING
47 * Enabled DHCP snooping when in shared-mac-on-the-wire mode. */
48#define INTNET_WITH_DHCP_SNOOPING
49
50/** The maximum number of interface in a network. */
51#define INTNET_MAX_IFS (1023 + 1 + 16)
52
53/** The number of entries to grow the destination tables with. */
54#if 0
55# define INTNET_GROW_DSTTAB_SIZE 16
56#else
57# define INTNET_GROW_DSTTAB_SIZE 1
58#endif
59
60/** The wakeup bit in the INTNETIF::cBusy and INTNETRUNKIF::cBusy counters. */
61#define INTNET_BUSY_WAKEUP_MASK RT_BIT_32(30)
62
63
64/*******************************************************************************
65* Structures and Typedefs *
66*******************************************************************************/
67/**
68 * MAC address lookup table entry.
69 */
70typedef struct INTNETMACTABENTRY
71{
72 /** The MAC address of this entry. */
73 RTMAC MacAddr;
74 /** Is it is effectively promiscuous mode. */
75 bool fPromiscuousEff;
76 /** Is it promiscuous and should it see unrelated trunk traffic. */
77 bool fPromiscuousSeeTrunk;
78 /** Is it active.
79 * We ignore the entry if this is clear and may end up sending packets addressed
80 * to this interface onto the trunk. The reasoning for this is that this could
81 * be the interface of a VM that just has been teleported to a different host. */
82 bool fActive;
83 /** Pointer to the network interface. */
84 struct INTNETIF *pIf;
85} INTNETMACTABENTRY;
86/** Pointer to a MAC address lookup table entry. */
87typedef INTNETMACTABENTRY *PINTNETMACTABENTRY;
88
89/**
90 * MAC address lookup table.
91 *
92 * @todo Having this in a separate structure didn't work out as well as it
93 * should. Consider merging it into INTNETNETWORK.
94 */
95typedef struct INTNETMACTAB
96{
97 /** The current number of entries. */
98 uint32_t cEntries;
99 /** The number of entries we've allocated space for. */
100 uint32_t cEntriesAllocated;
101 /** Table entries. */
102 PINTNETMACTABENTRY paEntries;
103
104 /** The number of interface entries currently in promicuous mode. */
105 uint32_t cPromiscuousEntries;
106 /** The number of interface entries currently in promicuous mode that
107 * shall not see unrelated trunk traffic. */
108 uint32_t cPromiscuousNoTrunkEntries;
109
110 /** The host MAC address (reported). */
111 RTMAC HostMac;
112 /** The effective host promiscuous setting (reported). */
113 bool fHostPromiscuousEff;
114 /** The real host promiscuous setting (reported). */
115 bool fHostPromiscuousReal;
116 /** Whether the host is active. */
117 bool fHostActive;
118
119 /** Whether the wire is promiscuous (config). */
120 bool fWirePromiscuousEff;
121 /** Whether the wire is promiscuous (config).
122 * (Shadows INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE in
123 * INTNETNETWORK::fFlags.) */
124 bool fWirePromiscuousReal;
125 /** Whether the wire is active. */
126 bool fWireActive;
127
128 /** Pointer to the trunk interface. */
129 struct INTNETTRUNKIF *pTrunk;
130} INTNETMACTAB;
131/** Pointer to a MAC address . */
132typedef INTNETMACTAB *PINTNETMACTAB;
133
134/**
135 * Destination table.
136 */
137typedef struct INTNETDSTTAB
138{
139 /** The trunk destinations. */
140 uint32_t fTrunkDst;
141 /** Pointer to the trunk interface (referenced) if fTrunkDst is non-zero. */
142 struct INTNETTRUNKIF *pTrunk;
143 /** The number of destination interfaces. */
144 uint32_t cIfs;
145 /** The interfaces (referenced). Variable sized array. */
146 struct
147 {
148 /** The destination interface. */
149 struct INTNETIF *pIf;
150 /** Whether to replace the destination MAC address.
151 * This is used when sharing MAC address with the host on the wire(less). */
152 bool fReplaceDstMac;
153 } aIfs[1];
154} INTNETDSTTAB;
155/** Pointer to a destination table. */
156typedef INTNETDSTTAB *PINTNETDSTTAB;
157/** Pointer to a const destination table. */
158typedef INTNETDSTTAB const *PCINTNETDSTTAB;
159
160
161/** Network layer address type. */
162typedef enum INTNETADDRTYPE
163{
164 /** The invalid 0 entry. */
165 kIntNetAddrType_Invalid = 0,
166 /** IP version 4. */
167 kIntNetAddrType_IPv4,
168 /** IP version 6. */
169 kIntNetAddrType_IPv6,
170 /** IPX. */
171 kIntNetAddrType_IPX,
172 /** The end of the valid values. */
173 kIntNetAddrType_End,
174 /** The usual 32-bit hack. */
175 kIntNetAddrType_32BitHack = 0x7fffffff
176} INTNETADDRTYPE;
177/** Pointer to a network layer address type. */
178typedef INTNETADDRTYPE *PINTNETADDRTYPE;
179
180
181/**
182 * Address and type.
183 */
184typedef struct INTNETADDR
185{
186 /** The address type. */
187 INTNETADDRTYPE enmType;
188 /** The address. */
189 RTNETADDRU Addr;
190} INTNETADDR;
191/** Pointer to an address. */
192typedef INTNETADDR *PINTNETADDR;
193/** Pointer to a const address. */
194typedef INTNETADDR const *PCINTNETADDR;
195
196
197/**
198 * Address cache for a specific network layer.
199 */
200typedef struct INTNETADDRCACHE
201{
202 /** Pointer to the table of addresses. */
203 uint8_t *pbEntries;
204 /** The number of valid address entries. */
205 uint8_t cEntries;
206 /** The number of allocated address entries. */
207 uint8_t cEntriesAlloc;
208 /** The address size. */
209 uint8_t cbAddress;
210 /** The size of an entry. */
211 uint8_t cbEntry;
212} INTNETADDRCACHE;
213/** Pointer to an address cache. */
214typedef INTNETADDRCACHE *PINTNETADDRCACHE;
215/** Pointer to a const address cache. */
216typedef INTNETADDRCACHE const *PCINTNETADDRCACHE;
217
218
219/**
220 * A network interface.
221 *
222 * Unless explicitly stated, all members are protect by the network semaphore.
223 */
224typedef struct INTNETIF
225{
226 /** The MAC address.
227 * This is shadowed by INTNETMACTABENTRY::MacAddr. */
228 RTMAC MacAddr;
229 /** Set if the INTNET::MacAddr member has been explicitly set. */
230 bool fMacSet;
231 /** Tracks the desired promiscuous setting of the interface. */
232 bool fPromiscuousReal;
233 /** Whether the interface is active or not.
234 * This is shadowed by INTNETMACTABENTRY::fActive. */
235 bool fActive;
236 /** Whether someone is currently in the destructor or has indicated that
237 * the end is nigh by means of IntNetR0IfAbortWait. */
238 bool volatile fDestroying;
239 /** The flags specified when opening this interface. */
240 uint32_t fOpenFlags;
241 /** Number of yields done to try make the interface read pending data.
242 * We will stop yielding when this reaches a threshold assuming that the VM is
243 * paused or that it simply isn't worth all the delay. It is cleared when a
244 * successful send has been done. */
245 uint32_t cYields;
246 /** Pointer to the current exchange buffer (ring-0). */
247 PINTNETBUF pIntBuf;
248 /** Pointer to ring-3 mapping of the current exchange buffer. */
249 R3PTRTYPE(PINTNETBUF) pIntBufR3;
250 /** Pointer to the default exchange buffer for the interface. */
251 PINTNETBUF pIntBufDefault;
252 /** Pointer to ring-3 mapping of the default exchange buffer. */
253 R3PTRTYPE(PINTNETBUF) pIntBufDefaultR3;
254 /** Event semaphore which a receiver/consumer thread will sleep on while
255 * waiting for data to arrive. */
256 RTSEMEVENT volatile hRecvEvent;
257 /** Number of threads sleeping on the event semaphore. */
258 uint32_t cSleepers;
259 /** The interface handle.
260 * When this is INTNET_HANDLE_INVALID a sleeper which is waking up
261 * should return with the appropriate error condition. */
262 INTNETIFHANDLE volatile hIf;
263 /** Pointer to the network this interface is connected to.
264 * This is protected by the INTNET::hMtxCreateOpenDestroy. */
265 struct INTNETNETWORK *pNetwork;
266 /** The session this interface is associated with. */
267 PSUPDRVSESSION pSession;
268 /** The SUPR0 object id. */
269 void *pvObj;
270 /** The network layer address cache. (Indexed by type, 0 entry isn't used.)
271 * This is protected by the address spinlock of the network. */
272 INTNETADDRCACHE aAddrCache[kIntNetAddrType_End];
273 /** Spinlock protecting the input (producer) side of the receive ring. */
274 RTSPINLOCK hRecvInSpinlock;
275 /** Busy count for tracking destination table references and active sends.
276 * Usually incremented while owning the switch table spinlock. The 30th bit
277 * is used to indicate wakeup. */
278 uint32_t volatile cBusy;
279 /** The preallocated destination table.
280 * This is NULL when it's in use as a precaution against unserialized
281 * transmitting. This is grown when new interfaces are added to the network. */
282 PINTNETDSTTAB volatile pDstTab;
283 /** Pointer to the trunk's per interface data. Can be NULL. */
284 void *pvIfData;
285 /** Header buffer for when we're carving GSO frames. */
286 uint8_t abGsoHdrs[256];
287} INTNETIF;
288/** Pointer to an internal network interface. */
289typedef INTNETIF *PINTNETIF;
290
291
292/**
293 * A trunk interface.
294 */
295typedef struct INTNETTRUNKIF
296{
297 /** The port interface we present to the component. */
298 INTNETTRUNKSWPORT SwitchPort;
299 /** The port interface we get from the component. */
300 PINTNETTRUNKIFPORT pIfPort;
301 /** Pointer to the network we're connect to.
302 * This may be NULL if we're orphaned? */
303 struct INTNETNETWORK *pNetwork;
304 /** The current MAC address for the interface. (reported)
305 * Updated while owning the switch table spinlock. */
306 RTMAC MacAddr;
307 /** Whether to supply physical addresses with the outbound SGs. (reported) */
308 bool fPhysSG;
309 /** Explicit alignment. */
310 bool fUnused;
311 /** Busy count for tracking destination table references and active sends.
312 * Usually incremented while owning the switch table spinlock. The 30th bit
313 * is used to indicate wakeup. */
314 uint32_t volatile cBusy;
315 /** Mask of destinations that pfnXmit cope with disabled preemption for. */
316 uint32_t fNoPreemptDsts;
317 /** The GSO capabilities of the wire destination. (reported) */
318 uint32_t fWireGsoCapabilites;
319 /** The GSO capabilities of the host destination. (reported)
320 * This is as bit map where each bit represents the GSO type with the same
321 * number. */
322 uint32_t fHostGsoCapabilites;
323 /** The destination table spinlock, interrupt safe.
324 * Protects apTaskDstTabs and apIntDstTabs. */
325 RTSPINLOCK hDstTabSpinlock;
326 /** The number of entries in apIntDstTabs. */
327 uint32_t cIntDstTabs;
328 /** The task time destination tables.
329 * @remarks intnetR0NetworkEnsureTabSpace and others ASSUMES this immediately
330 * precedes apIntDstTabs so that these two tables can be used as one
331 * contiguous one. */
332 PINTNETDSTTAB apTaskDstTabs[2];
333 /** The interrupt / disabled-preemption time destination tables.
334 * This is a variable sized array. */
335 PINTNETDSTTAB apIntDstTabs[1];
336} INTNETTRUNKIF;
337/** Pointer to a trunk interface. */
338typedef INTNETTRUNKIF *PINTNETTRUNKIF;
339
340/** Converts a pointer to INTNETTRUNKIF::SwitchPort to a PINTNETTRUNKIF. */
341#define INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort) ((PINTNETTRUNKIF)(pSwitchPort))
342
343
344/**
345 * Internal representation of a network.
346 */
347typedef struct INTNETNETWORK
348{
349 /** The Next network in the chain.
350 * This is protected by the INTNET::hMtxCreateOpenDestroy. */
351 struct INTNETNETWORK *pNext;
352
353 /** The spinlock protecting MacTab and INTNETTRUNKIF::aAddrCache.
354 * Interrupt safe. */
355 RTSPINLOCK hAddrSpinlock;
356 /** MAC address table.
357 * This doubles as interface collection. */
358 INTNETMACTAB MacTab;
359
360 /** Wait for an interface to stop being busy so it can be removed or have its
361 * destination table replaced. We have to wait upon this while owning the
362 * network mutex. Will only ever have one waiter because of the big mutex. */
363 RTSEMEVENT hEvtBusyIf;
364 /** Pointer to the instance data. */
365 struct INTNET *pIntNet;
366 /** The SUPR0 object id. */
367 void *pvObj;
368 /** Pointer to the temporary buffer that is used when snooping fragmented packets.
369 * This is allocated after this structure if we're sharing the MAC address with
370 * the host. The buffer is INTNETNETWORK_TMP_SIZE big and aligned on a 64-byte boundary. */
371 uint8_t *pbTmp;
372 /** Network creation flags (INTNET_OPEN_FLAGS_*). */
373 uint32_t fFlags;
374 /** Any restrictive policies required as a minimum by some interface.
375 * (INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES) */
376 uint32_t fMinFlags;
377 /** The number of active interfaces (excluding the trunk). */
378 uint32_t cActiveIFs;
379 /** The length of the network name. */
380 uint8_t cchName;
381 /** The network name. */
382 char szName[INTNET_MAX_NETWORK_NAME];
383 /** The trunk type. */
384 INTNETTRUNKTYPE enmTrunkType;
385 /** The trunk name. */
386 char szTrunk[INTNET_MAX_TRUNK_NAME];
387} INTNETNETWORK;
388/** Pointer to an internal network. */
389typedef INTNETNETWORK *PINTNETNETWORK;
390/** Pointer to a const internal network. */
391typedef const INTNETNETWORK *PCINTNETNETWORK;
392
393/** The size of the buffer INTNETNETWORK::pbTmp points at. */
394#define INTNETNETWORK_TMP_SIZE 2048
395
396
397/**
398 * Internal networking instance.
399 */
400typedef struct INTNET
401{
402 /** Magic number (INTNET_MAGIC). */
403 uint32_t volatile u32Magic;
404 /** Mutex protecting the creation, opening and destruction of both networks and
405 * interfaces. (This means all operations affecting the pNetworks list.) */
406 RTSEMMUTEX hMtxCreateOpenDestroy;
407 /** List of networks. Protected by INTNET::Spinlock. */
408 PINTNETNETWORK volatile pNetworks;
409 /** Handle table for the interfaces. */
410 RTHANDLETABLE hHtIfs;
411} INTNET;
412/** Pointer to an internal network ring-0 instance. */
413typedef struct INTNET *PINTNET;
414
415/** Magic number for the internal network instance data (Hayao Miyazaki). */
416#define INTNET_MAGIC UINT32_C(0x19410105)
417
418
419/*******************************************************************************
420* Global Variables *
421*******************************************************************************/
422/** Pointer to the internal network instance data. */
423static PINTNET volatile g_pIntNet = NULL;
424
425static const struct INTNETOPENNETWORKFLAGS
426{
427 uint32_t fRestrictive; /**< The restrictive flag (deny/disabled). */
428 uint32_t fRelaxed; /**< The relaxed flag (allow/enabled). */
429 uint32_t fFixed; /**< The config-fixed flag. */
430 uint32_t fPair; /**< The pair of restrictive and relaxed flags. */
431}
432/** Open network policy flags relating to the network. */
433g_afIntNetOpenNetworkNetFlags[] =
434{
435 { INTNET_OPEN_FLAGS_ACCESS_RESTRICTED, INTNET_OPEN_FLAGS_ACCESS_PUBLIC, INTNET_OPEN_FLAGS_ACCESS_FIXED, INTNET_OPEN_FLAGS_ACCESS_RESTRICTED | INTNET_OPEN_FLAGS_ACCESS_PUBLIC },
436 { INTNET_OPEN_FLAGS_PROMISC_DENY_CLIENTS, INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS, INTNET_OPEN_FLAGS_PROMISC_FIXED, INTNET_OPEN_FLAGS_PROMISC_DENY_CLIENTS | INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS },
437 { INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_HOST, INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST, INTNET_OPEN_FLAGS_PROMISC_FIXED, INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_HOST | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST },
438 { INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_WIRE, INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE, INTNET_OPEN_FLAGS_PROMISC_FIXED, INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_WIRE | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE },
439 { INTNET_OPEN_FLAGS_TRUNK_HOST_DISABLED, INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_HOST_DISABLED | INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED },
440 { INTNET_OPEN_FLAGS_TRUNK_HOST_CHASTE_MODE, INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_HOST_CHASTE_MODE | INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE },
441 { INTNET_OPEN_FLAGS_TRUNK_WIRE_DISABLED, INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_WIRE_DISABLED | INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED },
442 { INTNET_OPEN_FLAGS_TRUNK_WIRE_CHASTE_MODE, INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE, INTNET_OPEN_FLAGS_TRUNK_FIXED, INTNET_OPEN_FLAGS_TRUNK_WIRE_CHASTE_MODE | INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE },
443},
444/** Open network policy flags relating to the new interface. */
445g_afIntNetOpenNetworkIfFlags[] =
446{
447 { INTNET_OPEN_FLAGS_IF_PROMISC_DENY, INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW, INTNET_OPEN_FLAGS_IF_FIXED, INTNET_OPEN_FLAGS_IF_PROMISC_DENY | INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW },
448 { INTNET_OPEN_FLAGS_IF_PROMISC_NO_TRUNK, INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK, INTNET_OPEN_FLAGS_IF_FIXED, INTNET_OPEN_FLAGS_IF_PROMISC_NO_TRUNK | INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK },
449};
450
451
452
453/**
454 * Worker for intnetR0SgWritePart that deals with the case where the
455 * request doesn't fit into the first segment.
456 *
457 * @returns true, unless the request or SG invalid.
458 * @param pSG The SG list to write to.
459 * @param off Where to start writing (offset into the SG).
460 * @param cb How much to write.
461 * @param pvBuf The buffer to containing the bits to write.
462 */
463static bool intnetR0SgWritePartSlow(PCINTNETSG pSG, uint32_t off, uint32_t cb, void const *pvBuf)
464{
465 if (RT_UNLIKELY(off + cb > pSG->cbTotal))
466 return false;
467
468 /*
469 * Skip ahead to the segment where off starts.
470 */
471 unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
472 unsigned iSeg = 0;
473 while (off > pSG->aSegs[iSeg].cb)
474 {
475 off -= pSG->aSegs[iSeg++].cb;
476 AssertReturn(iSeg < cSegs, false);
477 }
478
479 /*
480 * Copy the data, hoping that it's all from one segment...
481 */
482 uint32_t cbCanCopy = pSG->aSegs[iSeg].cb - off;
483 if (cbCanCopy >= cb)
484 memcpy((uint8_t *)pSG->aSegs[iSeg].pv + off, pvBuf, cb);
485 else
486 {
487 /* copy the portion in the current segment. */
488 memcpy((uint8_t *)pSG->aSegs[iSeg].pv + off, pvBuf, cbCanCopy);
489 cb -= cbCanCopy;
490
491 /* copy the portions in the other segments. */
492 do
493 {
494 pvBuf = (uint8_t const *)pvBuf + cbCanCopy;
495 iSeg++;
496 AssertReturn(iSeg < cSegs, false);
497
498 cbCanCopy = RT_MIN(cb, pSG->aSegs[iSeg].cb);
499 memcpy(pSG->aSegs[iSeg].pv, pvBuf, cbCanCopy);
500
501 cb -= cbCanCopy;
502 } while (cb > 0);
503 }
504
505 return true;
506}
507
508
509/**
510 * Writes to a part of an SG.
511 *
512 * @returns true on success, false on failure (out of bounds).
513 * @param pSG The SG list to write to.
514 * @param off Where to start writing (offset into the SG).
515 * @param cb How much to write.
516 * @param pvBuf The buffer to containing the bits to write.
517 */
518DECLINLINE(bool) intnetR0SgWritePart(PCINTNETSG pSG, uint32_t off, uint32_t cb, void const *pvBuf)
519{
520 Assert(off + cb > off);
521
522 /* The optimized case. */
523 if (RT_LIKELY( pSG->cSegsUsed == 1
524 || pSG->aSegs[0].cb >= off + cb))
525 {
526 Assert(pSG->cbTotal == pSG->aSegs[0].cb);
527 memcpy((uint8_t *)pSG->aSegs[0].pv + off, pvBuf, cb);
528 return true;
529 }
530 return intnetR0SgWritePartSlow(pSG, off, cb, pvBuf);
531}
532
533
534/**
535 * Reads a byte from a SG list.
536 *
537 * @returns The byte on success. 0xff on failure.
538 * @param pSG The SG list to read.
539 * @param off The offset (into the SG) off the byte.
540 */
541DECLINLINE(uint8_t) intnetR0SgReadByte(PCINTNETSG pSG, uint32_t off)
542{
543 if (RT_LIKELY(pSG->aSegs[0].cb > off))
544 return ((uint8_t const *)pSG->aSegs[0].pv)[off];
545
546 off -= pSG->aSegs[0].cb;
547 unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
548 for (unsigned iSeg = 1; iSeg < cSegs; iSeg++)
549 {
550 if (pSG->aSegs[iSeg].cb > off)
551 return ((uint8_t const *)pSG->aSegs[iSeg].pv)[off];
552 off -= pSG->aSegs[iSeg].cb;
553 }
554 return false;
555}
556
557
558/**
559 * Worker for intnetR0SgReadPart that deals with the case where the
560 * requested data isn't in the first segment.
561 *
562 * @returns true, unless the SG is invalid.
563 * @param pSG The SG list to read.
564 * @param off Where to start reading (offset into the SG).
565 * @param cb How much to read.
566 * @param pvBuf The buffer to read into.
567 */
568static bool intnetR0SgReadPartSlow(PCINTNETSG pSG, uint32_t off, uint32_t cb, void *pvBuf)
569{
570 if (RT_UNLIKELY(off + cb > pSG->cbTotal))
571 return false;
572
573 /*
574 * Skip ahead to the segment where off starts.
575 */
576 unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
577 unsigned iSeg = 0;
578 while (off > pSG->aSegs[iSeg].cb)
579 {
580 off -= pSG->aSegs[iSeg++].cb;
581 AssertReturn(iSeg < cSegs, false);
582 }
583
584 /*
585 * Copy the data, hoping that it's all from one segment...
586 */
587 uint32_t cbCanCopy = pSG->aSegs[iSeg].cb - off;
588 if (cbCanCopy >= cb)
589 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv + off, cb);
590 else
591 {
592 /* copy the portion in the current segment. */
593 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv + off, cbCanCopy);
594 cb -= cbCanCopy;
595
596 /* copy the portions in the other segments. */
597 do
598 {
599 pvBuf = (uint8_t *)pvBuf + cbCanCopy;
600 iSeg++;
601 AssertReturn(iSeg < cSegs, false);
602
603 cbCanCopy = RT_MIN(cb, pSG->aSegs[iSeg].cb);
604 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv, cbCanCopy);
605
606 cb -= cbCanCopy;
607 } while (cb > 0);
608 }
609
610 return true;
611}
612
613
614/**
615 * Reads a part of an SG into a buffer.
616 *
617 * @returns true on success, false on failure (out of bounds).
618 * @param pSG The SG list to read.
619 * @param off Where to start reading (offset into the SG).
620 * @param cb How much to read.
621 * @param pvBuf The buffer to read into.
622 */
623DECLINLINE(bool) intnetR0SgReadPart(PCINTNETSG pSG, uint32_t off, uint32_t cb, void *pvBuf)
624{
625 Assert(off + cb > off);
626
627 /* The optimized case. */
628 if (RT_LIKELY( pSG->cSegsUsed == 1
629 || pSG->aSegs[0].cb >= off + cb))
630 {
631 Assert(pSG->cbTotal == pSG->aSegs[0].cb);
632 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[0].pv + off, cb);
633 return true;
634 }
635 return intnetR0SgReadPartSlow(pSG, off, cb, pvBuf);
636}
637
638
639/**
640 * Wait for a busy counter to reach zero.
641 *
642 * @param pNetwork The network.
643 * @param pcBusy The busy counter.
644 */
645static void intnetR0BusyWait(PINTNETNETWORK pNetwork, uint32_t volatile *pcBusy)
646{
647 if (ASMAtomicReadU32(pcBusy) == 0)
648 return;
649
650 /*
651 * We have to be a bit cautious here so we don't destroy the network or the
652 * semaphore before intnetR0BusyDec has signalled us.
653 */
654
655 /* Reset the semaphore and flip the wakeup bit. */
656 RTSemEventWait(pNetwork->hEvtBusyIf, 0); /* clear it */
657 uint32_t cCurBusy = ASMAtomicReadU32(pcBusy);
658 do
659 {
660 if (cCurBusy == 0)
661 return;
662 AssertMsg(!(cCurBusy & INTNET_BUSY_WAKEUP_MASK), ("%#x\n", cCurBusy));
663 AssertMsg((cCurBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cCurBusy));
664 } while (!ASMAtomicCmpXchgExU32(pcBusy, cCurBusy | INTNET_BUSY_WAKEUP_MASK, cCurBusy, &cCurBusy));
665
666 /* Wait for the count to reach zero. */
667 do
668 {
669 int rc2 = RTSemEventWait(pNetwork->hEvtBusyIf, 30000); NOREF(rc2);
670 //AssertMsg(RT_SUCCESS(rc2), ("rc=%Rrc *pcBusy=%#x (%#x)\n", rc2, ASMAtomicReadU32(pcBusy), cCurBusy ));
671 cCurBusy = ASMAtomicReadU32(pcBusy);
672 AssertMsg((cCurBusy & INTNET_BUSY_WAKEUP_MASK), ("%#x\n", cCurBusy));
673 AssertMsg((cCurBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cCurBusy));
674 } while ( cCurBusy != INTNET_BUSY_WAKEUP_MASK
675 || !ASMAtomicCmpXchgU32(pcBusy, 0, INTNET_BUSY_WAKEUP_MASK));
676}
677
678
679/**
680 * Decrements the busy counter and maybe wakes up any threads waiting for it to
681 * reach zero.
682 *
683 * @param pNetwork The network.
684 * @param pcBusy The busy counter.
685 */
686DECLINLINE(void) intnetR0BusyDec(PINTNETNETWORK pNetwork, uint32_t volatile *pcBusy)
687{
688 uint32_t cNewBusy = ASMAtomicDecU32(pcBusy);
689 if (RT_UNLIKELY( cNewBusy == INTNET_BUSY_WAKEUP_MASK
690 && pNetwork))
691 RTSemEventSignal(pNetwork->hEvtBusyIf);
692 AssertMsg((cNewBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cNewBusy));
693}
694
695
696/**
697 * Increments the busy count of the specified interface.
698 *
699 * The caller must own the MAC address table spinlock.
700 *
701 * @param pIf The interface.
702 */
703DECLINLINE(void) intnetR0BusyDecIf(PINTNETIF pIf)
704{
705 intnetR0BusyDec(pIf->pNetwork, &pIf->cBusy);
706}
707
708
709/**
710 * Increments the busy count of the specified interface.
711 *
712 * The caller must own the MAC address table spinlock or an explicity reference.
713 *
714 * @param pTrunk The trunk.
715 */
716DECLINLINE(void) intnetR0BusyDecTrunk(PINTNETTRUNKIF pTrunk)
717{
718 intnetR0BusyDec(pTrunk->pNetwork, &pTrunk->cBusy);
719}
720
721
722/**
723 * Increments the busy count of the specified interface.
724 *
725 * The caller must own the MAC address table spinlock or an explicity reference.
726 *
727 * @param pIf The interface.
728 */
729DECLINLINE(void) intnetR0BusyIncIf(PINTNETIF pIf)
730{
731 uint32_t cNewBusy = ASMAtomicIncU32(&pIf->cBusy);
732 AssertMsg((cNewBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cNewBusy));
733 NOREF(cNewBusy);
734}
735
736
737/**
738 * Increments the busy count of the specified interface.
739 *
740 * The caller must own the MAC address table spinlock or an explicity reference.
741 *
742 * @param pTrunk The trunk.
743 */
744DECLINLINE(void) intnetR0BusyIncTrunk(PINTNETTRUNKIF pTrunk)
745{
746 uint32_t cNewBusy = ASMAtomicIncU32(&pTrunk->cBusy);
747 AssertMsg((cNewBusy & ~INTNET_BUSY_WAKEUP_MASK) < INTNET_MAX_IFS * 3, ("%#x\n", cNewBusy));
748 NOREF(cNewBusy);
749}
750
751
752/**
753 * Retain an interface.
754 *
755 * @returns VBox status code, can assume success in most situations.
756 * @param pIf The interface instance.
757 * @param pSession The current session.
758 */
759DECLINLINE(int) intnetR0IfRetain(PINTNETIF pIf, PSUPDRVSESSION pSession)
760{
761 int rc = SUPR0ObjAddRefEx(pIf->pvObj, pSession, true /* fNoBlocking */);
762 AssertRCReturn(rc, rc);
763 return VINF_SUCCESS;
764}
765
766
767/**
768 * Release an interface previously retained by intnetR0IfRetain or
769 * by handle lookup/freeing.
770 *
771 * @returns true if destroyed, false if not.
772 * @param pIf The interface instance.
773 * @param pSession The current session.
774 */
775DECLINLINE(bool) intnetR0IfRelease(PINTNETIF pIf, PSUPDRVSESSION pSession)
776{
777 int rc = SUPR0ObjRelease(pIf->pvObj, pSession);
778 AssertRC(rc);
779 return rc == VINF_OBJECT_DESTROYED;
780}
781
782
783/**
784 * RTHandleCreateEx callback that retains an object in the
785 * handle table before returning it.
786 *
787 * (Avoids racing the freeing of the handle.)
788 *
789 * @returns VBox status code.
790 * @param hHandleTable The handle table (ignored).
791 * @param pvObj The object (INTNETIF).
792 * @param pvCtx The context (SUPDRVSESSION).
793 * @param pvUser The user context (ignored).
794 */
795static DECLCALLBACK(int) intnetR0IfRetainHandle(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
796{
797 NOREF(pvUser);
798 NOREF(hHandleTable);
799 PINTNETIF pIf = (PINTNETIF)pvObj;
800 if (pIf->hIf != INTNET_HANDLE_INVALID) /* Don't try retain it if called from intnetR0IfDestruct. */
801 return intnetR0IfRetain(pIf, (PSUPDRVSESSION)pvCtx);
802 return VINF_SUCCESS;
803}
804
805
806
807/**
808 * Checks if the interface has a usable MAC address or not.
809 *
810 * @returns true if MacAddr is usable, false if not.
811 * @param pIf The interface.
812 */
813DECL_FORCE_INLINE(bool) intnetR0IfHasMacAddr(PINTNETIF pIf)
814{
815 return pIf->fMacSet || !(pIf->MacAddr.au8[0] & 1);
816}
817
818
819/**
820 * Locates the MAC address table entry for the given interface.
821 *
822 * The caller holds the MAC address table spinlock, obviously.
823 *
824 * @returns Pointer to the entry on if found, NULL if not.
825 * @param pNetwork The network.
826 * @param pIf The interface.
827 */
828DECLINLINE(PINTNETMACTABENTRY) intnetR0NetworkFindMacAddrEntry(PINTNETNETWORK pNetwork, PINTNETIF pIf)
829{
830 uint32_t iIf = pNetwork->MacTab.cEntries;
831 while (iIf-- > 0)
832 {
833 if (pNetwork->MacTab.paEntries[iIf].pIf == pIf)
834 return &pNetwork->MacTab.paEntries[iIf];
835 }
836 return NULL;
837}
838
839
840/**
841 * Checks if the IPv6 address is a good interface address.
842 * @returns true/false.
843 * @param addr The address, network endian.
844 */
845DECLINLINE(bool) intnetR0IPv6AddrIsGood(RTNETADDRIPV6 addr)
846{
847 return !( ( addr.QWords.qw0 == 0 && addr.QWords.qw1 == 0) /* :: */
848 || ( (addr.Words.w0 & RT_H2BE_U16(0xff00)) == RT_H2BE_U16(0xff00)) /* multicast */
849 || ( addr.Words.w0 == 0 && addr.Words.w1 == 0
850 && addr.Words.w2 == 0 && addr.Words.w3 == 0
851 && addr.Words.w4 == 0 && addr.Words.w5 == 0
852 && addr.Words.w6 == 0 && addr.Words.w7 == RT_H2BE_U16(0x0001))); /* ::1 */
853}
854
855
856/**
857 * Checks if the IPv4 address is a broadcast address.
858 * @returns true/false.
859 * @param Addr The address, network endian.
860 */
861DECLINLINE(bool) intnetR0IPv4AddrIsBroadcast(RTNETADDRIPV4 Addr)
862{
863 /* Just check for 255.255.255.255 atm. */
864 return Addr.u == UINT32_MAX;
865}
866
867
868/**
869 * Checks if the IPv4 address is a good interface address.
870 * @returns true/false.
871 * @param Addr The address, network endian.
872 */
873DECLINLINE(bool) intnetR0IPv4AddrIsGood(RTNETADDRIPV4 Addr)
874{
875 /* Usual suspects. */
876 if ( Addr.u == UINT32_MAX /* 255.255.255.255 - broadcast. */
877 || Addr.au8[0] == 0) /* Current network, can be used as source address. */
878 return false;
879
880 /* Unusual suspects. */
881 if (RT_UNLIKELY( Addr.au8[0] == 127 /* Loopback */
882 || (Addr.au8[0] & 0xf0) == 224 /* Multicast */
883 ))
884 return false;
885 return true;
886}
887
888
889/**
890 * Gets the address size of a network layer type.
891 *
892 * @returns size in bytes.
893 * @param enmType The type.
894 */
895DECLINLINE(uint8_t) intnetR0AddrSize(INTNETADDRTYPE enmType)
896{
897 switch (enmType)
898 {
899 case kIntNetAddrType_IPv4: return 4;
900 case kIntNetAddrType_IPv6: return 16;
901 case kIntNetAddrType_IPX: return 4 + 6;
902 default: AssertFailedReturn(0);
903 }
904}
905
906
907/**
908 * Compares two address to see if they are equal, assuming naturally align structures.
909 *
910 * @returns true if equal, false if not.
911 * @param pAddr1 The first address.
912 * @param pAddr2 The second address.
913 * @param cbAddr The address size.
914 */
915DECLINLINE(bool) intnetR0AddrUIsEqualEx(PCRTNETADDRU pAddr1, PCRTNETADDRU pAddr2, uint8_t const cbAddr)
916{
917 switch (cbAddr)
918 {
919 case 4: /* IPv4 */
920 return pAddr1->au32[0] == pAddr2->au32[0];
921 case 16: /* IPv6 */
922 return pAddr1->au64[0] == pAddr2->au64[0]
923 && pAddr1->au64[1] == pAddr2->au64[1];
924 case 10: /* IPX */
925 return pAddr1->au64[0] == pAddr2->au64[0]
926 && pAddr1->au16[4] == pAddr2->au16[4];
927 default:
928 AssertFailedReturn(false);
929 }
930}
931
932
933/**
934 * Worker for intnetR0IfAddrCacheLookup that performs the lookup
935 * in the remaining cache entries after the caller has check the
936 * most likely ones.
937 *
938 * @returns -1 if not found, the index of the cache entry if found.
939 * @param pCache The cache.
940 * @param pAddr The address.
941 * @param cbAddr The address size (optimization).
942 */
943static int intnetR0IfAddrCacheLookupSlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
944{
945 unsigned i = pCache->cEntries - 2;
946 uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry * i;
947 while (i >= 1)
948 {
949 if (intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr))
950 return i;
951 pbEntry -= pCache->cbEntry;
952 i--;
953 }
954
955 return -1;
956}
957
958/**
959 * Lookup an address in a cache without any expectations.
960 *
961 * @returns -1 if not found, the index of the cache entry if found.
962 * @param pCache The cache.
963 * @param pAddr The address.
964 * @param cbAddr The address size (optimization).
965 */
966DECLINLINE(int) intnetR0IfAddrCacheLookup(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
967{
968 Assert(pCache->cbAddress == cbAddr);
969
970 /*
971 * The optimized case is when there is one cache entry and
972 * it doesn't match.
973 */
974 unsigned i = pCache->cEntries;
975 if ( i > 0
976 && intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr))
977 return 0;
978 if (i <= 1)
979 return -1;
980
981 /*
982 * Check the last entry.
983 */
984 i--;
985 if (intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))
986 return i;
987 if (i <= 1)
988 return -1;
989
990 return intnetR0IfAddrCacheLookupSlow(pCache, pAddr, cbAddr);
991}
992
993
994/** Same as intnetR0IfAddrCacheLookup except we expect the address to be present already. */
995DECLINLINE(int) intnetR0IfAddrCacheLookupLikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
996{
997 /** @todo implement this. */
998 return intnetR0IfAddrCacheLookup(pCache, pAddr, cbAddr);
999}
1000
1001
1002/**
1003 * Worker for intnetR0IfAddrCacheLookupUnlikely that performs
1004 * the lookup in the remaining cache entries after the caller
1005 * has check the most likely ones.
1006 *
1007 * The routine is expecting not to find the address.
1008 *
1009 * @returns -1 if not found, the index of the cache entry if found.
1010 * @param pCache The cache.
1011 * @param pAddr The address.
1012 * @param cbAddr The address size (optimization).
1013 */
1014static int intnetR0IfAddrCacheInCacheUnlikelySlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
1015{
1016 /*
1017 * Perform a full table lookup.
1018 */
1019 unsigned i = pCache->cEntries - 2;
1020 uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry * i;
1021 while (i >= 1)
1022 {
1023 if (RT_UNLIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr)))
1024 return i;
1025 pbEntry -= pCache->cbEntry;
1026 i--;
1027 }
1028
1029 return -1;
1030}
1031
1032
1033/**
1034 * Lookup an address in a cache expecting not to find it.
1035 *
1036 * @returns -1 if not found, the index of the cache entry if found.
1037 * @param pCache The cache.
1038 * @param pAddr The address.
1039 * @param cbAddr The address size (optimization).
1040 */
1041DECLINLINE(int) intnetR0IfAddrCacheLookupUnlikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
1042{
1043 Assert(pCache->cbAddress == cbAddr);
1044
1045 /*
1046 * The optimized case is when there is one cache entry and
1047 * it doesn't match.
1048 */
1049 unsigned i = pCache->cEntries;
1050 if (RT_UNLIKELY( i > 0
1051 && intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr)))
1052 return 0;
1053 if (RT_LIKELY(i <= 1))
1054 return -1;
1055
1056 /*
1057 * Then check the last entry and return if there are just two cache entries.
1058 */
1059 i--;
1060 if (RT_UNLIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr)))
1061 return i;
1062 if (i <= 1)
1063 return -1;
1064
1065 return intnetR0IfAddrCacheInCacheUnlikelySlow(pCache, pAddr, cbAddr);
1066}
1067
1068
1069/**
1070 * Deletes a specific cache entry.
1071 *
1072 * Worker for intnetR0NetworkAddrCacheDelete and intnetR0NetworkAddrCacheDeleteMinusIf.
1073 *
1074 * @param pIf The interface (for logging).
1075 * @param pCache The cache.
1076 * @param iEntry The entry to delete.
1077 * @param pszMsg Log message.
1078 */
1079static void intnetR0IfAddrCacheDeleteIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, int iEntry, const char *pszMsg)
1080{
1081 AssertReturnVoid(iEntry < pCache->cEntries);
1082 AssertReturnVoid(iEntry >= 0);
1083#ifdef LOG_ENABLED
1084 INTNETADDRTYPE enmAddrType = (INTNETADDRTYPE)(uintptr_t)(pCache - &pIf->aAddrCache[0]);
1085 PCRTNETADDRU pAddr = (PCRTNETADDRU)(pCache->pbEntries + iEntry * pCache->cbEntry);
1086 switch (enmAddrType)
1087 {
1088 case kIntNetAddrType_IPv4:
1089 Log(("intnetR0IfAddrCacheDeleteIt: hIf=%#x MAC=%.6Rhxs IPv4 deleted #%d %RTnaipv4 %s\n",
1090 pIf->hIf, &pIf->MacAddr, iEntry, pAddr->IPv4, pszMsg));
1091 break;
1092 case kIntNetAddrType_IPv6:
1093 Log(("intnetR0IfAddrCacheDeleteIt: hIf=%#x MAC=%.6Rhxs IPv6 deleted #%d %RTnaipv6 %s\n",
1094 pIf->hIf, &pIf->MacAddr, iEntry, pAddr->IPv6, pszMsg));
1095 break;
1096 default:
1097 Log(("intnetR0IfAddrCacheDeleteIt: hIf=%RX32 MAC=%.6Rhxs type=%d #%d %.*Rhxs %s\n",
1098 pIf->hIf, &pIf->MacAddr, enmAddrType, iEntry, pCache->cbAddress, pAddr, pszMsg));
1099 break;
1100 }
1101#endif
1102
1103 pCache->cEntries--;
1104 if (iEntry < pCache->cEntries)
1105 memmove(pCache->pbEntries + iEntry * pCache->cbEntry,
1106 pCache->pbEntries + (iEntry + 1) * pCache->cbEntry,
1107 (pCache->cEntries - iEntry) * pCache->cbEntry);
1108}
1109
1110
1111/**
1112 * Deletes an address from the cache, assuming it isn't actually in the cache.
1113 *
1114 * May or may not own the spinlock when calling this.
1115 *
1116 * @param pIf The interface (for logging).
1117 * @param pCache The cache.
1118 * @param pAddr The address.
1119 * @param cbAddr The address size (optimization).
1120 */
1121DECLINLINE(void) intnetR0IfAddrCacheDelete(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
1122{
1123 int i = intnetR0IfAddrCacheLookup(pCache, pAddr, cbAddr);
1124 if (RT_UNLIKELY(i >= 0))
1125 intnetR0IfAddrCacheDeleteIt(pIf, pCache, i, pszMsg);
1126}
1127
1128
1129/**
1130 * Deletes the address from all the interface caches.
1131 *
1132 * This is used to remove stale entries that has been reassigned to
1133 * other machines on the network.
1134 *
1135 * @param pNetwork The network.
1136 * @param pAddr The address.
1137 * @param enmType The address type.
1138 * @param cbAddr The address size (optimization).
1139 * @param pszMsg Log message.
1140 */
1141DECLINLINE(void) intnetR0NetworkAddrCacheDelete(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType,
1142 uint8_t const cbAddr, const char *pszMsg)
1143{
1144 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1145
1146 uint32_t iIf = pNetwork->MacTab.cEntries;
1147 while (iIf--)
1148 {
1149 PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf].pIf;
1150 int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
1151 if (RT_UNLIKELY(i >= 0))
1152 intnetR0IfAddrCacheDeleteIt(pIf, &pIf->aAddrCache[enmType], i, pszMsg);
1153 }
1154
1155 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1156}
1157
1158
1159/**
1160 * Deletes the address from all the interface caches except the specified one.
1161 *
1162 * This is used to remove stale entries that has been reassigned to
1163 * other machines on the network.
1164 *
1165 * @param pNetwork The network.
1166 * @param pAddr The address.
1167 * @param enmType The address type.
1168 * @param cbAddr The address size (optimization).
1169 * @param pszMsg Log message.
1170 */
1171DECLINLINE(void) intnetR0NetworkAddrCacheDeleteMinusIf(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCRTNETADDRU pAddr,
1172 INTNETADDRTYPE const enmType, uint8_t const cbAddr, const char *pszMsg)
1173{
1174 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1175
1176 uint32_t iIf = pNetwork->MacTab.cEntries;
1177 while (iIf--)
1178 {
1179 PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf].pIf;
1180 if (pIf != pIfSender)
1181 {
1182 int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
1183 if (RT_UNLIKELY(i >= 0))
1184 intnetR0IfAddrCacheDeleteIt(pIf, &pIf->aAddrCache[enmType], i, pszMsg);
1185 }
1186 }
1187
1188 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1189}
1190
1191
1192/**
1193 * Lookup an address on the network, returning the (first) interface having it
1194 * in its address cache.
1195 *
1196 * @returns Pointer to the interface on success, NULL if not found. The caller
1197 * must release the interface by calling intnetR0BusyDecIf.
1198 * @param pNetwork The network.
1199 * @param pAddr The address to lookup.
1200 * @param enmType The address type.
1201 * @param cbAddr The size of the address.
1202 */
1203DECLINLINE(PINTNETIF) intnetR0NetworkAddrCacheLookupIf(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType, uint8_t const cbAddr)
1204{
1205 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1206
1207 uint32_t iIf = pNetwork->MacTab.cEntries;
1208 while (iIf--)
1209 {
1210 PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf].pIf;
1211 int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
1212 if (i >= 0)
1213 {
1214 intnetR0BusyIncIf(pIf);
1215 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1216 return pIf;
1217 }
1218 }
1219
1220 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1221 return NULL;
1222}
1223
1224
1225/**
1226 * Adds an address to the cache, the caller is responsible for making sure it's
1227 * not already in the cache.
1228 *
1229 * The caller must not
1230 *
1231 * @param pIf The interface (for logging).
1232 * @param pCache The address cache.
1233 * @param pAddr The address.
1234 * @param pszMsg log message.
1235 */
1236static void intnetR0IfAddrCacheAddIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, const char *pszMsg)
1237{
1238 PINTNETNETWORK pNetwork = pIf->pNetwork;
1239 AssertReturnVoid(pNetwork);
1240 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1241
1242 if (RT_UNLIKELY(!pCache->cEntriesAlloc))
1243 {
1244 /* This shouldn't happen*/
1245 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1246 return;
1247 }
1248
1249 /* When the table is full, drop the older entry (FIFO). Do proper ageing? */
1250 if (pCache->cEntries >= pCache->cEntriesAlloc)
1251 {
1252 Log(("intnetR0IfAddrCacheAddIt: type=%d replacing %.*Rhxs\n",
1253 (int)(uintptr_t)(pCache - &pIf->aAddrCache[0]), pCache->cbAddress, pCache->pbEntries));
1254 memmove(pCache->pbEntries, pCache->pbEntries + pCache->cbEntry, pCache->cbEntry * (pCache->cEntries - 1));
1255 pCache->cEntries--;
1256 Assert(pCache->cEntries < pCache->cEntriesAlloc);
1257 }
1258
1259 /*
1260 * Add the new entry to the end of the array.
1261 */
1262 uint8_t *pbEntry = pCache->pbEntries + pCache->cEntries * pCache->cbEntry;
1263 memcpy(pbEntry, pAddr, pCache->cbAddress);
1264 memset(pbEntry + pCache->cbAddress, '\0', pCache->cbEntry - pCache->cbAddress);
1265#ifdef LOG_ENABLED
1266 INTNETADDRTYPE enmAddrType = (INTNETADDRTYPE)(uintptr_t)(pCache - &pIf->aAddrCache[0]);
1267 switch (enmAddrType)
1268 {
1269 case kIntNetAddrType_IPv4:
1270 Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs IPv4 added #%d %RTnaipv4 %s\n",
1271 pIf->hIf, &pIf->MacAddr, pCache->cEntries, pAddr->IPv4, pszMsg));
1272 break;
1273 case kIntNetAddrType_IPv6:
1274 Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs IPv6 added #%d %RTnaipv6 %s\n",
1275 pIf->hIf, &pIf->MacAddr, pCache->cEntries, pAddr->IPv6, pszMsg));
1276 break;
1277 default:
1278 Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs type=%d added #%d %.*Rhxs %s\n",
1279 pIf->hIf, &pIf->MacAddr, enmAddrType, pCache->cEntries, pCache->cbAddress, pAddr, pszMsg));
1280 break;
1281 }
1282#endif
1283 pCache->cEntries++;
1284 Assert(pCache->cEntries <= pCache->cEntriesAlloc);
1285
1286 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1287}
1288
1289
1290/**
1291 * A intnetR0IfAddrCacheAdd worker that performs the rest of the lookup.
1292 *
1293 * @param pIf The interface (for logging).
1294 * @param pCache The address cache.
1295 * @param pAddr The address.
1296 * @param cbAddr The size of the address (optimization).
1297 * @param pszMsg Log message.
1298 */
1299static void intnetR0IfAddrCacheAddSlow(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
1300{
1301 /*
1302 * Check all but the first and last entries, the caller
1303 * has already checked those.
1304 */
1305 int i = pCache->cEntries - 2;
1306 uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry;
1307 while (i >= 1)
1308 {
1309 if (RT_LIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr)))
1310 return;
1311 pbEntry += pCache->cbEntry;
1312 i--;
1313 }
1314
1315 /*
1316 * Not found, add it.
1317 */
1318 intnetR0IfAddrCacheAddIt(pIf, pCache, pAddr, pszMsg);
1319}
1320
1321
1322/**
1323 * Adds an address to the cache if it's not already there.
1324 *
1325 * Must not own any spinlocks when calling this function.
1326 *
1327 * @param pIf The interface (for logging).
1328 * @param pCache The address cache.
1329 * @param pAddr The address.
1330 * @param cbAddr The size of the address (optimization).
1331 * @param pszMsg Log message.
1332 */
1333DECLINLINE(void) intnetR0IfAddrCacheAdd(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr,
1334 uint8_t const cbAddr, const char *pszMsg)
1335{
1336 Assert(pCache->cbAddress == cbAddr);
1337
1338 /*
1339 * The optimized case is when the address the first or last cache entry.
1340 */
1341 unsigned i = pCache->cEntries;
1342 if (RT_LIKELY( i > 0
1343 && ( intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr)
1344 || (i > 1
1345 && intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))) ))
1346 return;
1347 intnetR0IfAddrCacheAddSlow(pIf, pCache, pAddr, cbAddr, pszMsg);
1348}
1349
1350
1351/**
1352 * Destroys the specified address cache.
1353 * @param pCache The address cache.
1354 */
1355static void intnetR0IfAddrCacheDestroy(PINTNETADDRCACHE pCache)
1356{
1357 void *pvFree = pCache->pbEntries;
1358 pCache->pbEntries = NULL;
1359 pCache->cEntries = 0;
1360 pCache->cEntriesAlloc = 0;
1361 RTMemFree(pvFree);
1362}
1363
1364
1365/**
1366 * Initialize the address cache for the specified address type.
1367 *
1368 * The cache storage is preallocated and fixed size so that we can handle
1369 * inserts from problematic contexts.
1370 *
1371 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
1372 * @param pCache The cache to initialize.
1373 * @param enmAddrType The address type.
1374 * @param fEnabled Whether the address cache is enabled or not.
1375 */
1376static int intnetR0IfAddrCacheInit(PINTNETADDRCACHE pCache, INTNETADDRTYPE enmAddrType, bool fEnabled)
1377{
1378 pCache->cEntries = 0;
1379 pCache->cbAddress = intnetR0AddrSize(enmAddrType);
1380 pCache->cbEntry = RT_ALIGN(pCache->cbAddress, 4);
1381 if (fEnabled)
1382 {
1383 pCache->cEntriesAlloc = 32;
1384 pCache->pbEntries = (uint8_t *)RTMemAllocZ(pCache->cEntriesAlloc * pCache->cbEntry);
1385 if (!pCache->pbEntries)
1386 return VERR_NO_MEMORY;
1387 }
1388 else
1389 {
1390 pCache->cEntriesAlloc = 0;
1391 pCache->pbEntries = NULL;
1392 }
1393 return VINF_SUCCESS;
1394}
1395
1396
1397/**
1398 * Is it a multicast or broadcast MAC address?
1399 *
1400 * @returns true if multicast, false if not.
1401 * @param pMacAddr The address to inspect.
1402 */
1403DECL_FORCE_INLINE(bool) intnetR0IsMacAddrMulticast(PCRTMAC pMacAddr)
1404{
1405 return !!(pMacAddr->au8[0] & 0x01);
1406}
1407
1408
1409/**
1410 * Is it a dummy MAC address?
1411 *
1412 * We use dummy MAC addresses for interfaces which we don't know the MAC
1413 * address of because they haven't sent anything (learning) or explicitly set
1414 * it.
1415 *
1416 * @returns true if dummy, false if not.
1417 * @param pMacAddr The address to inspect.
1418 */
1419DECL_FORCE_INLINE(bool) intnetR0IsMacAddrDummy(PCRTMAC pMacAddr)
1420{
1421 /* The dummy address are broadcast addresses, don't bother check it all. */
1422 return pMacAddr->au16[0] == 0xffff;
1423}
1424
1425
1426/**
1427 * Compares two MAC addresses.
1428 *
1429 * @returns true if equal, false if not.
1430 * @param pDstAddr1 Address 1.
1431 * @param pDstAddr2 Address 2.
1432 */
1433DECL_FORCE_INLINE(bool) intnetR0AreMacAddrsEqual(PCRTMAC pDstAddr1, PCRTMAC pDstAddr2)
1434{
1435 return pDstAddr1->au16[2] == pDstAddr2->au16[2]
1436 && pDstAddr1->au16[1] == pDstAddr2->au16[1]
1437 && pDstAddr1->au16[0] == pDstAddr2->au16[0];
1438}
1439
1440
1441/**
1442 * Switch a unicast frame based on the network layer address (OSI level 3) and
1443 * return a destination table.
1444 *
1445 * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
1446 * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
1447 * @param pNetwork The network to switch on.
1448 * @param pDstMacAddr The destination MAC address.
1449 * @param enmL3AddrType The level-3 destination address type.
1450 * @param pL3Addr The level-3 destination address.
1451 * @param cbL3Addr The size of the level-3 destination address.
1452 * @param fSrc The frame source (INTNETTRUNKDIR_WIRE).
1453 * @param pDstTab The destination output table.
1454 */
1455static INTNETSWDECISION intnetR0NetworkSwitchLevel3(PINTNETNETWORK pNetwork, PCRTMAC pDstMacAddr,
1456 INTNETADDRTYPE enmL3AddrType, PCRTNETADDRU pL3Addr, uint8_t cbL3Addr,
1457 uint32_t fSrc, PINTNETDSTTAB pDstTab)
1458{
1459 Assert(fSrc == INTNETTRUNKDIR_WIRE);
1460
1461 /*
1462 * Grab the spinlock first and do the switching.
1463 */
1464 PINTNETMACTAB pTab = &pNetwork->MacTab;
1465 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1466
1467 pDstTab->fTrunkDst = 0;
1468 pDstTab->pTrunk = 0;
1469 pDstTab->cIfs = 0;
1470
1471 /* Find exactly matching or promiscuous interfaces. */
1472 uint32_t cExactHits = 0;
1473 uint32_t iIfMac = pTab->cEntries;
1474 while (iIfMac-- > 0)
1475 {
1476 if (pTab->paEntries[iIfMac].fActive)
1477 {
1478 PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
1479 bool fExact = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmL3AddrType], pL3Addr, cbL3Addr) >= 0;
1480 if (fExact || pTab->paEntries[iIfMac].fPromiscuousSeeTrunk)
1481 {
1482 cExactHits += fExact;
1483
1484 uint32_t iIfDst = pDstTab->cIfs++;
1485 pDstTab->aIfs[iIfDst].pIf = pIf;
1486 pDstTab->aIfs[iIfDst].fReplaceDstMac = fExact;
1487 intnetR0BusyIncIf(pIf);
1488
1489 if (fExact)
1490 pDstMacAddr = &pIf->MacAddr; /* Avoids duplicates being sent to the host. */
1491 }
1492 }
1493 }
1494
1495 /* Network only promicuous mode ifs should see related trunk traffic. */
1496 if ( cExactHits
1497 && fSrc
1498 && pNetwork->MacTab.cPromiscuousNoTrunkEntries)
1499 {
1500 iIfMac = pTab->cEntries;
1501 while (iIfMac-- > 0)
1502 {
1503 if ( pTab->paEntries[iIfMac].fActive
1504 && pTab->paEntries[iIfMac].fPromiscuousEff
1505 && !pTab->paEntries[iIfMac].fPromiscuousSeeTrunk)
1506 {
1507 PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
1508 if (intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmL3AddrType], pL3Addr, cbL3Addr) < 0)
1509 {
1510 uint32_t iIfDst = pDstTab->cIfs++;
1511 pDstTab->aIfs[iIfDst].pIf = pIf;
1512 pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
1513 intnetR0BusyIncIf(pIf);
1514 }
1515 }
1516 }
1517 }
1518
1519 /* Does it match the host, or is the host promiscuous? */
1520 if (pTab->fHostActive)
1521 {
1522 bool fExact = intnetR0AreMacAddrsEqual(&pTab->HostMac, pDstMacAddr);
1523 if ( fExact
1524 || intnetR0IsMacAddrDummy(&pTab->HostMac)
1525 || pTab->fHostPromiscuousEff)
1526 {
1527 cExactHits += fExact;
1528 pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
1529 }
1530 }
1531
1532 /* Hit the wire if there are no exact matches or if it's in promiscuous mode. */
1533 if (pTab->fWireActive && (!cExactHits || pTab->fWirePromiscuousEff))
1534 pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
1535 pDstTab->fTrunkDst &= ~fSrc;
1536 if (pDstTab->fTrunkDst)
1537 {
1538 PINTNETTRUNKIF pTrunk = pTab->pTrunk;
1539 pDstTab->pTrunk = pTrunk;
1540 intnetR0BusyIncTrunk(pTrunk);
1541 }
1542
1543 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1544 return pDstTab->cIfs
1545 ? (!pDstTab->fTrunkDst ? INTNETSWDECISION_INTNET : INTNETSWDECISION_BROADCAST)
1546 : (!pDstTab->fTrunkDst ? INTNETSWDECISION_DROP : INTNETSWDECISION_TRUNK);
1547}
1548
1549
1550/**
1551 * Pre-switch a unicast MAC address.
1552 *
1553 * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
1554 * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
1555 * @param pNetwork The network to switch on.
1556 * @param fSrc The frame source.
1557 * @param pSrcAddr The source address of the frame.
1558 * @param pDstAddr The destination address of the frame.
1559 */
1560static INTNETSWDECISION intnetR0NetworkPreSwitchUnicast(PINTNETNETWORK pNetwork, uint32_t fSrc, PCRTMAC pSrcAddr,
1561 PCRTMAC pDstAddr)
1562{
1563 Assert(!intnetR0IsMacAddrMulticast(pDstAddr));
1564 Assert(fSrc);
1565
1566 /*
1567 * Grab the spinlock first and do the switching.
1568 */
1569 INTNETSWDECISION enmSwDecision = INTNETSWDECISION_BROADCAST;
1570 PINTNETMACTAB pTab = &pNetwork->MacTab;
1571 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1572
1573 /* Iterate the internal network interfaces and look for matching source and
1574 destination addresses. */
1575 uint32_t iIfMac = pTab->cEntries;
1576 while (iIfMac-- > 0)
1577 {
1578 if (pTab->paEntries[iIfMac].fActive)
1579 {
1580 /* Unknown interface address? */
1581 if (intnetR0IsMacAddrDummy(&pTab->paEntries[iIfMac].MacAddr))
1582 break;
1583
1584 /* Promiscuous mode? */
1585 if (pTab->paEntries[iIfMac].fPromiscuousSeeTrunk)
1586 break;
1587
1588 /* Paranoia - this shouldn't happen, right? */
1589 if ( pSrcAddr
1590 && intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pSrcAddr))
1591 break;
1592
1593 /* Exact match? */
1594 if (intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pDstAddr))
1595 {
1596 enmSwDecision = pTab->fHostPromiscuousEff && fSrc == INTNETTRUNKDIR_WIRE
1597 ? INTNETSWDECISION_BROADCAST
1598 : INTNETSWDECISION_INTNET;
1599 break;
1600 }
1601 }
1602 }
1603
1604 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1605 return enmSwDecision;
1606}
1607
1608
1609/**
1610 * Switch a unicast MAC address and return a destination table.
1611 *
1612 * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
1613 * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
1614 * @param pNetwork The network to switch on.
1615 * @param fSrc The frame source.
1616 * @param pIfSender The sender interface, NULL if trunk. Used to
1617 * prevent sending an echo to the sender.
1618 * @param pDstAddr The destination address of the frame.
1619 * @param pDstTab The destination output table.
1620 */
1621static INTNETSWDECISION intnetR0NetworkSwitchUnicast(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETIF pIfSender,
1622 PCRTMAC pDstAddr, PINTNETDSTTAB pDstTab)
1623{
1624 AssertPtr(pDstTab);
1625 Assert(!intnetR0IsMacAddrMulticast(pDstAddr));
1626
1627 /*
1628 * Grab the spinlock first and do the switching.
1629 */
1630 PINTNETMACTAB pTab = &pNetwork->MacTab;
1631 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1632
1633 pDstTab->fTrunkDst = 0;
1634 pDstTab->pTrunk = 0;
1635 pDstTab->cIfs = 0;
1636
1637 /* Find exactly matching or promiscuous interfaces. */
1638 uint32_t cExactHits = 0;
1639 uint32_t iIfMac = pTab->cEntries;
1640 while (iIfMac-- > 0)
1641 {
1642 if (pTab->paEntries[iIfMac].fActive)
1643 {
1644 bool fExact = intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pDstAddr);
1645 if ( fExact
1646 || intnetR0IsMacAddrDummy(&pTab->paEntries[iIfMac].MacAddr)
1647 || ( pTab->paEntries[iIfMac].fPromiscuousSeeTrunk
1648 || (!fSrc && pTab->paEntries[iIfMac].fPromiscuousEff) )
1649 )
1650 {
1651 cExactHits += fExact;
1652
1653 PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
1654 if (RT_LIKELY(pIf != pIfSender)) /* paranoia */
1655 {
1656 uint32_t iIfDst = pDstTab->cIfs++;
1657 pDstTab->aIfs[iIfDst].pIf = pIf;
1658 pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
1659 intnetR0BusyIncIf(pIf);
1660 }
1661 }
1662 }
1663 }
1664
1665 /* Network only promicuous mode ifs should see related trunk traffic. */
1666 if ( cExactHits
1667 && fSrc
1668 && pNetwork->MacTab.cPromiscuousNoTrunkEntries)
1669 {
1670 iIfMac = pTab->cEntries;
1671 while (iIfMac-- > 0)
1672 {
1673 if ( pTab->paEntries[iIfMac].fPromiscuousEff
1674 && !pTab->paEntries[iIfMac].fPromiscuousSeeTrunk
1675 && pTab->paEntries[iIfMac].fActive
1676 && !intnetR0AreMacAddrsEqual(&pTab->paEntries[iIfMac].MacAddr, pDstAddr)
1677 && !intnetR0IsMacAddrDummy(&pTab->paEntries[iIfMac].MacAddr) )
1678 {
1679 PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
1680 uint32_t iIfDst = pDstTab->cIfs++;
1681 pDstTab->aIfs[iIfDst].pIf = pIf;
1682 pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
1683 intnetR0BusyIncIf(pIf);
1684 }
1685 }
1686 }
1687
1688 /* Does it match the host, or is the host promiscuous? */
1689 if ( fSrc != INTNETTRUNKDIR_HOST
1690 && pTab->fHostActive)
1691 {
1692 bool fExact = intnetR0AreMacAddrsEqual(&pTab->HostMac, pDstAddr);
1693 if ( fExact
1694 || intnetR0IsMacAddrDummy(&pTab->HostMac)
1695 || pTab->fHostPromiscuousEff)
1696 {
1697 cExactHits += fExact;
1698 pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
1699 }
1700 }
1701
1702 /* Hit the wire if there are no exact matches or if it's in promiscuous mode. */
1703 if ( fSrc != INTNETTRUNKDIR_WIRE
1704 && pTab->fWireActive
1705 && (!cExactHits || pTab->fWirePromiscuousEff)
1706 )
1707 pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
1708
1709 /* Grab the trunk if we're sending to it. */
1710 if (pDstTab->fTrunkDst)
1711 {
1712 PINTNETTRUNKIF pTrunk = pTab->pTrunk;
1713 pDstTab->pTrunk = pTrunk;
1714 intnetR0BusyIncTrunk(pTrunk);
1715 }
1716
1717 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1718 return pDstTab->cIfs
1719 ? (!pDstTab->fTrunkDst ? INTNETSWDECISION_INTNET : INTNETSWDECISION_BROADCAST)
1720 : (!pDstTab->fTrunkDst ? INTNETSWDECISION_DROP : INTNETSWDECISION_TRUNK);
1721}
1722
1723
1724/**
1725 * Create a destination table for a broadcast frame.
1726 *
1727 * @returns INTNETSWDECISION_BROADCAST.
1728 * @param pNetwork The network to switch on.
1729 * @param fSrc The frame source.
1730 * @param pIfSender The sender interface, NULL if trunk. Used to
1731 * prevent sending an echo to the sender.
1732 * @param pDstTab The destination output table.
1733 */
1734static INTNETSWDECISION intnetR0NetworkSwitchBroadcast(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETIF pIfSender,
1735 PINTNETDSTTAB pDstTab)
1736{
1737 AssertPtr(pDstTab);
1738
1739 /*
1740 * Grab the spinlock first and record all active interfaces.
1741 */
1742 PINTNETMACTAB pTab = &pNetwork->MacTab;
1743 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1744
1745 pDstTab->fTrunkDst = 0;
1746 pDstTab->pTrunk = 0;
1747 pDstTab->cIfs = 0;
1748
1749 /* Regular interfaces. */
1750 uint32_t iIfMac = pTab->cEntries;
1751 while (iIfMac-- > 0)
1752 {
1753 if (pTab->paEntries[iIfMac].fActive)
1754 {
1755 PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
1756 if (pIf != pIfSender)
1757 {
1758 uint32_t iIfDst = pDstTab->cIfs++;
1759 pDstTab->aIfs[iIfDst].pIf = pIf;
1760 pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
1761 intnetR0BusyIncIf(pIf);
1762 }
1763 }
1764 }
1765
1766 /* The trunk interface. */
1767 if (pTab->fHostActive)
1768 pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
1769 if (pTab->fWireActive)
1770 pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
1771 pDstTab->fTrunkDst &= ~fSrc;
1772 if (pDstTab->fTrunkDst)
1773 {
1774 PINTNETTRUNKIF pTrunk = pTab->pTrunk;
1775 pDstTab->pTrunk = pTrunk;
1776 intnetR0BusyIncTrunk(pTrunk);
1777 }
1778
1779 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1780 return INTNETSWDECISION_BROADCAST;
1781}
1782
1783
1784/**
1785 * Create a destination table with the trunk and any promiscuous interfaces.
1786 *
1787 * This is only used in a fallback case of the level-3 switching, so we can
1788 * assume the wire as source and skip the sender interface filtering.
1789 *
1790 * @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
1791 * INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
1792 * @param pNetwork The network to switch on.
1793 * @param fSrc The frame source.
1794 * @param pDstTab The destination output table.
1795 */
1796static INTNETSWDECISION intnetR0NetworkSwitchTrunkAndPromisc(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETDSTTAB pDstTab)
1797{
1798 Assert(fSrc == INTNETTRUNKDIR_WIRE);
1799
1800 /*
1801 * Grab the spinlock first and do the switching.
1802 */
1803 PINTNETMACTAB pTab = &pNetwork->MacTab;
1804 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1805
1806 pDstTab->fTrunkDst = 0;
1807 pDstTab->pTrunk = 0;
1808 pDstTab->cIfs = 0;
1809
1810 /* Find promiscuous interfaces. */
1811 uint32_t iIfMac = pTab->cEntries;
1812 while (iIfMac-- > 0)
1813 {
1814 if ( pTab->paEntries[iIfMac].fActive
1815 && ( pTab->paEntries[iIfMac].fPromiscuousSeeTrunk
1816 || (!fSrc && pTab->paEntries[iIfMac].fPromiscuousEff) )
1817 )
1818 {
1819 PINTNETIF pIf = pTab->paEntries[iIfMac].pIf; AssertPtr(pIf); Assert(pIf->pNetwork == pNetwork);
1820 uint32_t iIfDst = pDstTab->cIfs++;
1821 pDstTab->aIfs[iIfDst].pIf = pIf;
1822 pDstTab->aIfs[iIfDst].fReplaceDstMac = false;
1823 intnetR0BusyIncIf(pIf);
1824 }
1825 }
1826
1827 /* The trunk interface. */
1828 if (pTab->fHostActive)
1829 pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
1830 if (pTab->fWireActive)
1831 pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
1832 pDstTab->fTrunkDst &= ~fSrc;
1833 if (pDstTab->fTrunkDst)
1834 {
1835 PINTNETTRUNKIF pTrunk = pTab->pTrunk;
1836 pDstTab->pTrunk = pTrunk;
1837 intnetR0BusyIncTrunk(pTrunk);
1838 }
1839
1840 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1841 return !pDstTab->cIfs
1842 ? (!pDstTab->fTrunkDst ? INTNETSWDECISION_DROP : INTNETSWDECISION_TRUNK)
1843 : (!pDstTab->fTrunkDst ? INTNETSWDECISION_INTNET : INTNETSWDECISION_BROADCAST);
1844}
1845
1846
1847/**
1848 * Create a destination table for a trunk frame.
1849 *
1850 * @returns INTNETSWDECISION_BROADCAST.
1851 * @param pNetwork The network to switch on.
1852 * @param fSrc The frame source.
1853 * @param pDstTab The destination output table.
1854 */
1855static INTNETSWDECISION intnetR0NetworkSwitchTrunk(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETDSTTAB pDstTab)
1856{
1857 AssertPtr(pDstTab);
1858
1859 /*
1860 * Grab the spinlock first and record all active interfaces.
1861 */
1862 PINTNETMACTAB pTab= &pNetwork->MacTab;
1863 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1864
1865 pDstTab->fTrunkDst = 0;
1866 pDstTab->pTrunk = 0;
1867 pDstTab->cIfs = 0;
1868
1869 /* The trunk interface. */
1870 if (pTab->fHostActive)
1871 pDstTab->fTrunkDst |= INTNETTRUNKDIR_HOST;
1872 if (pTab->fWireActive)
1873 pDstTab->fTrunkDst |= INTNETTRUNKDIR_WIRE;
1874 pDstTab->fTrunkDst &= ~fSrc;
1875 if (pDstTab->fTrunkDst)
1876 {
1877 PINTNETTRUNKIF pTrunk = pTab->pTrunk;
1878 pDstTab->pTrunk = pTrunk;
1879 intnetR0BusyIncTrunk(pTrunk);
1880 }
1881
1882 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
1883 return pDstTab->fTrunkDst ? INTNETSWDECISION_TRUNK : INTNETSWDECISION_DROP;
1884}
1885
1886
1887/**
1888 * Wrapper around RTMemAlloc for allocating a destination table.
1889 *
1890 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
1891 * @param cEntries The size given as an entry count.
1892 * @param ppDstTab Where to store the pointer (always).
1893 */
1894DECLINLINE(int) intnetR0AllocDstTab(uint32_t cEntries, PINTNETDSTTAB *ppDstTab)
1895{
1896 PINTNETDSTTAB pDstTab;
1897 *ppDstTab = pDstTab = (PINTNETDSTTAB)RTMemAlloc(RT_OFFSETOF(INTNETDSTTAB, aIfs[cEntries]));
1898 if (RT_UNLIKELY(!pDstTab))
1899 return VERR_NO_MEMORY;
1900 return VINF_SUCCESS;
1901}
1902
1903
1904/**
1905 * Ensures that there is space for another interface in the MAC address lookup
1906 * table as well as all the destination tables.
1907 *
1908 * The caller must own the create/open/destroy mutex.
1909 *
1910 * @returns VINF_SUCCESS, VERR_NO_MEMORY or VERR_OUT_OF_RANGE.
1911 * @param pNetwork The network to operate on.
1912 */
1913static int intnetR0NetworkEnsureTabSpace(PINTNETNETWORK pNetwork)
1914{
1915 /*
1916 * The cEntries and cEntriesAllocated members are only updated while
1917 * owning the big mutex, so we only need the spinlock when doing the
1918 * actual table replacing.
1919 */
1920 PINTNETMACTAB pTab = &pNetwork->MacTab;
1921 int rc = VINF_SUCCESS;
1922 AssertReturn(pTab->cEntries <= pTab->cEntriesAllocated, VERR_INTERNAL_ERROR_2);
1923 if (pTab->cEntries + 1 > pTab->cEntriesAllocated)
1924 {
1925 uint32_t const cAllocated = pTab->cEntriesAllocated + INTNET_GROW_DSTTAB_SIZE;
1926 if (cAllocated <= INTNET_MAX_IFS)
1927 {
1928 /*
1929 * Resize the destination tables first, this can be kind of tedious.
1930 */
1931 for (uint32_t i = 0; i < pTab->cEntries; i++)
1932 {
1933 PINTNETIF pIf = pTab->paEntries[i].pIf; AssertPtr(pIf);
1934 PINTNETDSTTAB pNew;
1935 rc = intnetR0AllocDstTab(cAllocated, &pNew);
1936 if (RT_FAILURE(rc))
1937 break;
1938
1939 for (;;)
1940 {
1941 PINTNETDSTTAB pOld = pIf->pDstTab;
1942 if ( pOld
1943 && ASMAtomicCmpXchgPtr(&pIf->pDstTab, pNew, pOld))
1944 {
1945 RTMemFree(pOld);
1946 break;
1947 }
1948 intnetR0BusyWait(pNetwork, &pIf->cBusy);
1949 }
1950 }
1951
1952 /*
1953 * The trunk.
1954 */
1955 if ( RT_SUCCESS(rc)
1956 && pNetwork->MacTab.pTrunk)
1957 {
1958 AssertCompileAdjacentMembers(INTNETTRUNKIF, apTaskDstTabs, apIntDstTabs);
1959 PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
1960 PINTNETDSTTAB * const ppEndDstTab = &pTrunk->apIntDstTabs[pTrunk->cIntDstTabs];
1961 for (PINTNETDSTTAB *ppDstTab = &pTrunk->apTaskDstTabs[0];
1962 ppDstTab != ppEndDstTab && RT_SUCCESS(rc);
1963 ppDstTab++)
1964 {
1965 PINTNETDSTTAB pNew;
1966 rc = intnetR0AllocDstTab(cAllocated, &pNew);
1967 if (RT_FAILURE(rc))
1968 break;
1969
1970 for (;;)
1971 {
1972 RTSpinlockAcquire(pTrunk->hDstTabSpinlock);
1973 void *pvOld = *ppDstTab;
1974 if (pvOld)
1975 *ppDstTab = pNew;
1976 RTSpinlockReleaseNoInts(pTrunk->hDstTabSpinlock);
1977 if (pvOld)
1978 {
1979 RTMemFree(pvOld);
1980 break;
1981 }
1982 intnetR0BusyWait(pNetwork, &pTrunk->cBusy);
1983 }
1984 }
1985 }
1986
1987 /*
1988 * The MAC Address table itself.
1989 */
1990 if (RT_SUCCESS(rc))
1991 {
1992 PINTNETMACTABENTRY paNew = (PINTNETMACTABENTRY)RTMemAlloc(sizeof(INTNETMACTABENTRY) * cAllocated);
1993 if (paNew)
1994 {
1995 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
1996
1997 PINTNETMACTABENTRY paOld = pTab->paEntries;
1998 uint32_t i = pTab->cEntries;
1999 while (i-- > 0)
2000 {
2001 paNew[i] = paOld[i];
2002
2003 paOld[i].fActive = false;
2004 paOld[i].pIf = NULL;
2005 }
2006
2007 pTab->paEntries = paNew;
2008 pTab->cEntriesAllocated = cAllocated;
2009
2010 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
2011
2012 RTMemFree(paOld);
2013 }
2014 else
2015 rc = VERR_NO_MEMORY;
2016 }
2017 }
2018 else
2019 rc = VERR_OUT_OF_RANGE;
2020 }
2021 return rc;
2022}
2023
2024
2025
2026
2027#ifdef INTNET_WITH_DHCP_SNOOPING
2028
2029/**
2030 * Snoops IP assignments and releases from the DHCPv4 traffic.
2031 *
2032 * The caller is responsible for making sure this traffic between the
2033 * BOOTPS and BOOTPC ports and validate the IP header. The UDP packet
2034 * need not be validated beyond the ports.
2035 *
2036 * @param pNetwork The network this frame was seen on.
2037 * @param pIpHdr Pointer to a valid IP header. This is for pseudo
2038 * header validation, so only the minimum header size
2039 * needs to be available and valid here.
2040 * @param pUdpHdr Pointer to the UDP header in the frame.
2041 * @param cbUdpPkt What's left of the frame when starting at the UDP header.
2042 * @param fGso Set if this is a GSO frame, clear if regular.
2043 */
2044static void intnetR0NetworkSnoopDhcp(PINTNETNETWORK pNetwork, PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, uint32_t cbUdpPkt)
2045{
2046 /*
2047 * Check if the DHCP message is valid and get the type.
2048 */
2049 if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbUdpPkt, true /*fCheckSum*/))
2050 {
2051 Log6(("Bad UDP packet\n"));
2052 return;
2053 }
2054 PCRTNETBOOTP pDhcp = (PCRTNETBOOTP)(pUdpHdr + 1);
2055 uint8_t MsgType;
2056 if (!RTNetIPv4IsDHCPValid(pUdpHdr, pDhcp, cbUdpPkt - sizeof(*pUdpHdr), &MsgType))
2057 {
2058 Log6(("Bad DHCP packet\n"));
2059 return;
2060 }
2061
2062#ifdef LOG_ENABLED
2063 /*
2064 * Log it.
2065 */
2066 const char *pszType = "unknown";
2067 switch (MsgType)
2068 {
2069 case RTNET_DHCP_MT_DISCOVER: pszType = "discover"; break;
2070 case RTNET_DHCP_MT_OFFER: pszType = "offer"; break;
2071 case RTNET_DHCP_MT_REQUEST: pszType = "request"; break;
2072 case RTNET_DHCP_MT_DECLINE: pszType = "decline"; break;
2073 case RTNET_DHCP_MT_ACK: pszType = "ack"; break;
2074 case RTNET_DHCP_MT_NAC: pszType = "nac"; break;
2075 case RTNET_DHCP_MT_RELEASE: pszType = "release"; break;
2076 case RTNET_DHCP_MT_INFORM: pszType = "inform"; break;
2077 }
2078 Log6(("DHCP msg: %d (%s) client %.6Rhxs ciaddr=%d.%d.%d.%d yiaddr=%d.%d.%d.%d\n", MsgType, pszType, &pDhcp->bp_chaddr,
2079 pDhcp->bp_ciaddr.au8[0], pDhcp->bp_ciaddr.au8[1], pDhcp->bp_ciaddr.au8[2], pDhcp->bp_ciaddr.au8[3],
2080 pDhcp->bp_yiaddr.au8[0], pDhcp->bp_yiaddr.au8[1], pDhcp->bp_yiaddr.au8[2], pDhcp->bp_yiaddr.au8[3]));
2081#endif /* LOG_EANBLED */
2082
2083 /*
2084 * Act upon the message.
2085 */
2086 switch (MsgType)
2087 {
2088#if 0
2089 case RTNET_DHCP_MT_REQUEST:
2090 /** @todo Check for valid non-broadcast requests w/ IP for any of the MACs we
2091 * know, and add the IP to the cache. */
2092 break;
2093#endif
2094
2095
2096 /*
2097 * Lookup the interface by its MAC address and insert the IPv4 address into the cache.
2098 * Delete the old client address first, just in case it changed in a renewal.
2099 */
2100 case RTNET_DHCP_MT_ACK:
2101 if (intnetR0IPv4AddrIsGood(pDhcp->bp_yiaddr))
2102 {
2103 PINTNETIF pMatchingIf = NULL;
2104 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
2105
2106 uint32_t iIf = pNetwork->MacTab.cEntries;
2107 while (iIf-- > 0)
2108 {
2109 PINTNETIF pCur = pNetwork->MacTab.paEntries[iIf].pIf;
2110 if ( intnetR0IfHasMacAddr(pCur)
2111 && !memcmp(&pCur->MacAddr, &pDhcp->bp_chaddr, sizeof(RTMAC)))
2112 {
2113 intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
2114 (PCRTNETADDRU)&pDhcp->bp_ciaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_ACK");
2115 if (!pMatchingIf)
2116 {
2117 pMatchingIf = pCur;
2118 intnetR0BusyIncIf(pMatchingIf);
2119 }
2120 }
2121 }
2122
2123 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
2124
2125 if (pMatchingIf)
2126 {
2127 intnetR0IfAddrCacheAdd(pMatchingIf, &pMatchingIf->aAddrCache[kIntNetAddrType_IPv4],
2128 (PCRTNETADDRU)&pDhcp->bp_yiaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_ACK");
2129 intnetR0BusyDecIf(pMatchingIf);
2130 }
2131 }
2132 return;
2133
2134
2135 /*
2136 * Lookup the interface by its MAC address and remove the IPv4 address(es) from the cache.
2137 */
2138 case RTNET_DHCP_MT_RELEASE:
2139 {
2140 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
2141
2142 uint32_t iIf = pNetwork->MacTab.cEntries;
2143 while (iIf-- > 0)
2144 {
2145 PINTNETIF pCur = pNetwork->MacTab.paEntries[iIf].pIf;
2146 if ( intnetR0IfHasMacAddr(pCur)
2147 && !memcmp(&pCur->MacAddr, &pDhcp->bp_chaddr, sizeof(RTMAC)))
2148 {
2149 intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
2150 (PCRTNETADDRU)&pDhcp->bp_ciaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_RELEASE");
2151 intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
2152 (PCRTNETADDRU)&pDhcp->bp_yiaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_RELEASE");
2153 }
2154 }
2155
2156 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
2157 break;
2158 }
2159 }
2160
2161}
2162
2163
2164/**
2165 * Worker for intnetR0TrunkIfSnoopAddr that takes care of what
2166 * is likely to be a DHCP message.
2167 *
2168 * The caller has already check that the UDP source and destination ports
2169 * are BOOTPS or BOOTPC.
2170 *
2171 * @param pNetwork The network this frame was seen on.
2172 * @param pSG The gather list for the frame.
2173 */
2174static void intnetR0TrunkIfSnoopDhcp(PINTNETNETWORK pNetwork, PCINTNETSG pSG)
2175{
2176 /*
2177 * Get a pointer to a linear copy of the full packet, using the
2178 * temporary buffer if necessary.
2179 */
2180 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((PCRTNETETHERHDR)pSG->aSegs[0].pv + 1);
2181 uint32_t cbPacket = pSG->cbTotal - sizeof(RTNETETHERHDR);
2182 if (pSG->cSegsUsed > 1)
2183 {
2184 cbPacket = RT_MIN(cbPacket, INTNETNETWORK_TMP_SIZE);
2185 Log6(("intnetR0TrunkIfSnoopDhcp: Copying IPv4/UDP/DHCP pkt %u\n", cbPacket));
2186 if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
2187 return;
2188 //pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
2189 pIpHdr = (PCRTNETIPV4)pNetwork->pbTmp;
2190 }
2191
2192 /*
2193 * Validate the IP header and find the UDP packet.
2194 */
2195 if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, pSG->cbTotal - sizeof(RTNETETHERHDR), true /*fChecksum*/))
2196 {
2197 Log(("intnetR0TrunkIfSnoopDhcp: bad ip header\n"));
2198 return;
2199 }
2200 uint32_t cbIpHdr = pIpHdr->ip_hl * 4;
2201
2202 /*
2203 * Hand it over to the common DHCP snooper.
2204 */
2205 intnetR0NetworkSnoopDhcp(pNetwork, pIpHdr, (PCRTNETUDP)((uintptr_t)pIpHdr + cbIpHdr), cbPacket - cbIpHdr);
2206}
2207
2208#endif /* INTNET_WITH_DHCP_SNOOPING */
2209
2210
2211/**
2212 * Snoops up source addresses from ARP requests and purge these from the address
2213 * caches.
2214 *
2215 * The purpose of this purging is to get rid of stale addresses.
2216 *
2217 * @param pNetwork The network this frame was seen on.
2218 * @param pSG The gather list for the frame.
2219 */
2220static void intnetR0TrunkIfSnoopArp(PINTNETNETWORK pNetwork, PCINTNETSG pSG)
2221{
2222 /*
2223 * Check the minimum size first.
2224 */
2225 if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4)))
2226 return;
2227
2228 /*
2229 * Copy to temporary buffer if necessary.
2230 */
2231 uint32_t cbPacket = RT_MIN(pSG->cbTotal, sizeof(RTNETARPIPV4));
2232 PCRTNETARPIPV4 pArpIPv4 = (PCRTNETARPIPV4)((uintptr_t)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
2233 if ( pSG->cSegsUsed != 1
2234 && pSG->aSegs[0].cb < cbPacket)
2235 {
2236 if ( (pSG->fFlags & (INTNETSG_FLAGS_ARP_IPV4 | INTNETSG_FLAGS_PKT_CP_IN_TMP))
2237 != (INTNETSG_FLAGS_ARP_IPV4 | INTNETSG_FLAGS_PKT_CP_IN_TMP)
2238 && !intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
2239 return;
2240 pArpIPv4 = (PCRTNETARPIPV4)pNetwork->pbTmp;
2241 }
2242
2243 /*
2244 * Ignore packets which doesn't interest us or we perceive as malformed.
2245 */
2246 if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
2247 || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
2248 || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
2249 || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
2250 return;
2251 uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
2252 if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
2253 && ar_oper != RTNET_ARPOP_REPLY))
2254 {
2255 Log6(("ts-ar: op=%#x\n", ar_oper));
2256 return;
2257 }
2258
2259 /*
2260 * Delete the source address if it's OK.
2261 */
2262 if ( !intnetR0IsMacAddrMulticast(&pArpIPv4->ar_sha)
2263 && ( pArpIPv4->ar_sha.au16[0]
2264 || pArpIPv4->ar_sha.au16[1]
2265 || pArpIPv4->ar_sha.au16[2])
2266 && intnetR0IPv4AddrIsGood(pArpIPv4->ar_spa))
2267 {
2268 Log6(("ts-ar: %d.%d.%d.%d / %.6Rhxs\n", pArpIPv4->ar_spa.au8[0], pArpIPv4->ar_spa.au8[1],
2269 pArpIPv4->ar_spa.au8[2], pArpIPv4->ar_spa.au8[3], &pArpIPv4->ar_sha));
2270 intnetR0NetworkAddrCacheDelete(pNetwork, (PCRTNETADDRU)&pArpIPv4->ar_spa,
2271 kIntNetAddrType_IPv4, sizeof(pArpIPv4->ar_spa), "tif/arp");
2272 }
2273}
2274
2275
2276#ifdef INTNET_WITH_DHCP_SNOOPING
2277/**
2278 * Snoop up addresses from ARP and DHCP traffic from frames coming
2279 * over the trunk connection.
2280 *
2281 * The caller is responsible for do some basic filtering before calling
2282 * this function.
2283 * For IPv4 this means checking against the minimum DHCPv4 frame size.
2284 *
2285 * @param pNetwork The network.
2286 * @param pSG The SG list for the frame.
2287 * @param EtherType The Ethertype of the frame.
2288 */
2289static void intnetR0TrunkIfSnoopAddr(PINTNETNETWORK pNetwork, PCINTNETSG pSG, uint16_t EtherType)
2290{
2291 switch (EtherType)
2292 {
2293 case RTNET_ETHERTYPE_IPV4:
2294 {
2295 uint32_t cbIpHdr;
2296 uint8_t b;
2297
2298 Assert(pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN);
2299 if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN)
2300 {
2301 /* check if the protocol is UDP */
2302 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((uint8_t const *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
2303 if (pIpHdr->ip_p != RTNETIPV4_PROT_UDP)
2304 return;
2305
2306 /* get the TCP header length */
2307 cbIpHdr = pIpHdr->ip_hl * 4;
2308 }
2309 else
2310 {
2311 /* check if the protocol is UDP */
2312 if ( intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_p))
2313 != RTNETIPV4_PROT_UDP)
2314 return;
2315
2316 /* get the TCP header length */
2317 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + 0); /* (IPv4 first byte, a bitfield) */
2318 cbIpHdr = (b & 0x0f) * 4;
2319 }
2320 if (cbIpHdr < RTNETIPV4_MIN_LEN)
2321 return;
2322
2323 /* compare the ports. */
2324 if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR) + cbIpHdr + RTNETUDP_MIN_LEN)
2325 {
2326 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t const *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR) + cbIpHdr);
2327 if ( ( RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPS
2328 && RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPS)
2329 || ( RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPC
2330 && RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPC))
2331 return;
2332 }
2333 else
2334 {
2335 /* get the lower byte of the UDP source port number. */
2336 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_sport) + 1);
2337 if ( b != RTNETIPV4_PORT_BOOTPS
2338 && b != RTNETIPV4_PORT_BOOTPC)
2339 return;
2340 uint8_t SrcPort = b;
2341 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_sport));
2342 if (b)
2343 return;
2344
2345 /* get the lower byte of the UDP destination port number. */
2346 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_dport) + 1);
2347 if ( b != RTNETIPV4_PORT_BOOTPS
2348 && b != RTNETIPV4_PORT_BOOTPC)
2349 return;
2350 if (b == SrcPort)
2351 return;
2352 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_dport));
2353 if (b)
2354 return;
2355 }
2356 intnetR0TrunkIfSnoopDhcp(pNetwork, pSG);
2357 break;
2358 }
2359
2360 case RTNET_ETHERTYPE_ARP:
2361 intnetR0TrunkIfSnoopArp(pNetwork, pSG);
2362 break;
2363 }
2364}
2365#endif /* INTNET_WITH_DHCP_SNOOPING */
2366
2367/**
2368 * Deals with an IPv6 packet.
2369 *
2370 * This will fish out the source IP address and add it to the cache.
2371 * Then it will look for DHCPRELEASE requests (?) and anything else
2372 * that we might find useful later.
2373 *
2374 * @param pIf The interface that's sending the frame.
2375 * @param pIpHdr Pointer to the IPv4 header in the frame.
2376 * @param cbPacket The size of the packet, or more correctly the
2377 * size of the frame without the ethernet header.
2378 * @param fGso Set if this is a GSO frame, clear if regular.
2379 */
2380static void intnetR0IfSnoopIPv6SourceAddr(PINTNETIF pIf, PCRTNETIPV6 pIpHdr, uint32_t cbPacket, bool fGso)
2381{
2382 /*
2383 * Check the header size first to prevent access invalid data.
2384 */
2385 if (cbPacket < RTNETIPV6_MIN_LEN)
2386 return;
2387
2388 /*
2389 * If the source address is good (not multicast) and
2390 * not already in the address cache of the sender, add it.
2391 */
2392 RTNETADDRU Addr;
2393 Addr.IPv6 = pIpHdr->ip6_src;
2394
2395 if ( intnetR0IPv6AddrIsGood(Addr.IPv6) && (pIpHdr->ip6_hlim == 0xff)
2396 && intnetR0IfAddrCacheLookupLikely(&pIf->aAddrCache[kIntNetAddrType_IPv6], &Addr, sizeof(Addr.IPv6)) < 0)
2397 {
2398 intnetR0IfAddrCacheAddIt(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv6], &Addr, "if/ipv6");
2399 }
2400}
2401
2402
2403/**
2404 * Deals with an IPv4 packet.
2405 *
2406 * This will fish out the source IP address and add it to the cache.
2407 * Then it will look for DHCPRELEASE requests (?) and anything else
2408 * that we might find useful later.
2409 *
2410 * @param pIf The interface that's sending the frame.
2411 * @param pIpHdr Pointer to the IPv4 header in the frame.
2412 * @param cbPacket The size of the packet, or more correctly the
2413 * size of the frame without the ethernet header.
2414 * @param fGso Set if this is a GSO frame, clear if regular.
2415 */
2416static void intnetR0IfSnoopIPv4SourceAddr(PINTNETIF pIf, PCRTNETIPV4 pIpHdr, uint32_t cbPacket, bool fGso)
2417{
2418 /*
2419 * Check the header size first to prevent access invalid data.
2420 */
2421 if (cbPacket < RTNETIPV4_MIN_LEN)
2422 return;
2423 uint32_t cbHdr = (uint32_t)pIpHdr->ip_hl * 4;
2424 if ( cbHdr < RTNETIPV4_MIN_LEN
2425 || cbPacket < cbHdr)
2426 return;
2427
2428 /*
2429 * If the source address is good (not broadcast or my network) and
2430 * not already in the address cache of the sender, add it. Validate
2431 * the IP header before adding it.
2432 */
2433 bool fValidatedIpHdr = false;
2434 RTNETADDRU Addr;
2435 Addr.IPv4 = pIpHdr->ip_src;
2436 if ( intnetR0IPv4AddrIsGood(Addr.IPv4)
2437 && intnetR0IfAddrCacheLookupLikely(&pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, sizeof(Addr.IPv4)) < 0)
2438 {
2439 if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, cbPacket, !fGso /*fChecksum*/))
2440 {
2441 Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header\n"));
2442 return;
2443 }
2444 intnetR0IfAddrCacheAddIt(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, "if/ipv4");
2445 fValidatedIpHdr = true;
2446 }
2447
2448#ifdef INTNET_WITH_DHCP_SNOOPING
2449 /*
2450 * Check for potential DHCP packets.
2451 */
2452 if ( pIpHdr->ip_p == RTNETIPV4_PROT_UDP /* DHCP is UDP. */
2453 && cbPacket >= cbHdr + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN /* Min DHCP packet len. */
2454 && !fGso) /* GSO is not applicable to DHCP traffic. */
2455 {
2456 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t const *)pIpHdr + cbHdr);
2457 if ( ( RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPS
2458 || RT_BE2H_U16(pUdpHdr->uh_sport) == RTNETIPV4_PORT_BOOTPS)
2459 && ( RT_BE2H_U16(pUdpHdr->uh_sport) == RTNETIPV4_PORT_BOOTPC
2460 || RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPC))
2461 {
2462 if ( fValidatedIpHdr
2463 || RTNetIPv4IsHdrValid(pIpHdr, cbPacket, cbPacket, !fGso /*fChecksum*/))
2464 intnetR0NetworkSnoopDhcp(pIf->pNetwork, pIpHdr, pUdpHdr, cbPacket - cbHdr);
2465 else
2466 Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header (dhcp)\n"));
2467 }
2468 }
2469#endif /* INTNET_WITH_DHCP_SNOOPING */
2470}
2471
2472
2473/**
2474 * Snoop up source addresses from an ARP request or reply.
2475 *
2476 * @param pIf The interface that's sending the frame.
2477 * @param pHdr The ARP header.
2478 * @param cbPacket The size of the packet (might be larger than the ARP
2479 * request 'cause of min ethernet frame size).
2480 * @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
2481 * don't have to repeat the frame parsing in intnetR0TrunkIfSend.
2482 */
2483static void intnetR0IfSnoopArpAddr(PINTNETIF pIf, PCRTNETARPIPV4 pArpIPv4, uint32_t cbPacket, uint16_t *pfSgFlags)
2484{
2485 /*
2486 * Ignore packets which doesn't interest us or we perceive as malformed.
2487 */
2488 if (RT_UNLIKELY(cbPacket < sizeof(RTNETARPIPV4)))
2489 return;
2490 if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
2491 || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
2492 || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
2493 || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
2494 return;
2495 uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
2496 if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
2497 && ar_oper != RTNET_ARPOP_REPLY))
2498 {
2499 Log6(("ar_oper=%#x\n", ar_oper));
2500 return;
2501 }
2502
2503 /*
2504 * Tag the SG as ARP IPv4 for later editing, then check for addresses
2505 * which can be removed or added to the address cache of the sender.
2506 */
2507 *pfSgFlags |= INTNETSG_FLAGS_ARP_IPV4;
2508
2509 if ( ar_oper == RTNET_ARPOP_REPLY
2510 && !intnetR0IsMacAddrMulticast(&pArpIPv4->ar_tha)
2511 && ( pArpIPv4->ar_tha.au16[0]
2512 || pArpIPv4->ar_tha.au16[1]
2513 || pArpIPv4->ar_tha.au16[2])
2514 && intnetR0IPv4AddrIsGood(pArpIPv4->ar_tpa))
2515 intnetR0IfAddrCacheDelete(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4],
2516 (PCRTNETADDRU)&pArpIPv4->ar_tpa, sizeof(RTNETADDRIPV4), "if/arp");
2517
2518 if ( !memcmp(&pArpIPv4->ar_sha, &pIf->MacAddr, sizeof(RTMAC))
2519 && intnetR0IPv4AddrIsGood(pArpIPv4->ar_spa))
2520 intnetR0IfAddrCacheAdd(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4],
2521 (PCRTNETADDRU)&pArpIPv4->ar_spa, sizeof(RTNETADDRIPV4), "if/arp");
2522}
2523
2524
2525
2526/**
2527 * Checks packets send by a normal interface for new network
2528 * layer addresses.
2529 *
2530 * @param pIf The interface that's sending the frame.
2531 * @param pbFrame The frame.
2532 * @param cbFrame The size of the frame.
2533 * @param fGso Set if this is a GSO frame, clear if regular.
2534 * @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
2535 * don't have to repeat the frame parsing in intnetR0TrunkIfSend.
2536 */
2537static void intnetR0IfSnoopAddr(PINTNETIF pIf, uint8_t const *pbFrame, uint32_t cbFrame, bool fGso, uint16_t *pfSgFlags)
2538{
2539 /*
2540 * Fish out the ethertype and look for stuff we can handle.
2541 */
2542 if (cbFrame <= sizeof(RTNETETHERHDR))
2543 return;
2544 cbFrame -= sizeof(RTNETETHERHDR);
2545
2546 uint16_t EtherType = RT_H2BE_U16(((PCRTNETETHERHDR)pbFrame)->EtherType);
2547 switch (EtherType)
2548 {
2549 case RTNET_ETHERTYPE_IPV4:
2550 intnetR0IfSnoopIPv4SourceAddr(pIf, (PCRTNETIPV4)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, fGso);
2551 break;
2552
2553 case RTNET_ETHERTYPE_IPV6:
2554 intnetR0IfSnoopIPv6SourceAddr(pIf, (PCRTNETIPV6)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, fGso);
2555 break;
2556
2557#if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
2558 case RTNET_ETHERTYPE_IPX_1:
2559 case RTNET_ETHERTYPE_IPX_2:
2560 case RTNET_ETHERTYPE_IPX_3:
2561 intnetR0IfSnoopIpxSourceAddr(pIf, (PCINTNETIPX)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, pfSgFlags);
2562 break;
2563#endif
2564 case RTNET_ETHERTYPE_ARP:
2565 intnetR0IfSnoopArpAddr(pIf, (PCRTNETARPIPV4)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, pfSgFlags);
2566 break;
2567 }
2568}
2569
2570
2571/**
2572 * Writes a frame packet to the ring buffer.
2573 *
2574 * @returns VBox status code.
2575 * @param pBuf The buffer.
2576 * @param pRingBuf The ring buffer to read from.
2577 * @param pSG The gather list.
2578 * @param pNewDstMac Set the destination MAC address to the address if specified.
2579 */
2580static int intnetR0RingWriteFrame(PINTNETRINGBUF pRingBuf, PCINTNETSG pSG, PCRTMAC pNewDstMac)
2581{
2582 PINTNETHDR pHdr = NULL; /* shut up gcc*/
2583 void *pvDst = NULL; /* ditto */
2584 int rc;
2585 if (pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
2586 rc = IntNetRingAllocateFrame(pRingBuf, pSG->cbTotal, &pHdr, &pvDst);
2587 else
2588 rc = IntNetRingAllocateGsoFrame(pRingBuf, pSG->cbTotal, &pSG->GsoCtx, &pHdr, &pvDst);
2589 if (RT_SUCCESS(rc))
2590 {
2591 IntNetSgRead(pSG, pvDst);
2592 if (pNewDstMac)
2593 ((PRTNETETHERHDR)pvDst)->DstMac = *pNewDstMac;
2594
2595 IntNetRingCommitFrame(pRingBuf, pHdr);
2596 return VINF_SUCCESS;
2597 }
2598 return rc;
2599}
2600
2601
2602/**
2603 * Sends a frame to a specific interface.
2604 *
2605 * @param pIf The interface.
2606 * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
2607 * @param pSG The gather buffer which data is being sent to the interface.
2608 * @param pNewDstMac Set the destination MAC address to the address if specified.
2609 */
2610static void intnetR0IfSend(PINTNETIF pIf, PINTNETIF pIfSender, PINTNETSG pSG, PCRTMAC pNewDstMac)
2611{
2612 /*
2613 * Grab the receive/producer lock and copy over the frame.
2614 */
2615 RTSpinlockAcquire(pIf->hRecvInSpinlock);
2616 int rc = intnetR0RingWriteFrame(&pIf->pIntBuf->Recv, pSG, pNewDstMac);
2617 RTSpinlockReleaseNoInts(pIf->hRecvInSpinlock);
2618 if (RT_SUCCESS(rc))
2619 {
2620 pIf->cYields = 0;
2621 RTSemEventSignal(pIf->hRecvEvent);
2622 return;
2623 }
2624
2625 Log(("intnetR0IfSend: overflow cb=%d hIf=%RX32\n", pSG->cbTotal, pIf->hIf));
2626
2627 /*
2628 * Scheduling hack, for unicore machines primarily.
2629 */
2630 if ( pIf->fActive
2631 && pIf->cYields < 4 /* just twice */
2632 && pIfSender /* but not if it's from the trunk */
2633 && RTThreadPreemptIsEnabled(NIL_RTTHREAD)
2634 )
2635 {
2636 unsigned cYields = 2;
2637 while (--cYields > 0)
2638 {
2639 RTSemEventSignal(pIf->hRecvEvent);
2640 RTThreadYield();
2641
2642 RTSpinlockAcquire(pIf->hRecvInSpinlock);
2643 rc = intnetR0RingWriteFrame(&pIf->pIntBuf->Recv, pSG, pNewDstMac);
2644 RTSpinlockReleaseNoInts(pIf->hRecvInSpinlock);
2645 if (RT_SUCCESS(rc))
2646 {
2647 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatYieldsOk);
2648 RTSemEventSignal(pIf->hRecvEvent);
2649 return;
2650 }
2651 pIf->cYields++;
2652 }
2653 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatYieldsNok);
2654 }
2655
2656 /* ok, the frame is lost. */
2657 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatLost);
2658 RTSemEventSignal(pIf->hRecvEvent);
2659}
2660
2661
2662/**
2663 * Fallback path that does the GSO segmenting before passing the frame on to the
2664 * trunk interface.
2665 *
2666 * The caller holds the trunk lock.
2667 *
2668 * @param pThis The trunk.
2669 * @param pIfSender The IF sending the frame.
2670 * @param pSG Pointer to the gather list.
2671 * @param fDst The destination flags.
2672 */
2673static int intnetR0TrunkIfSendGsoFallback(PINTNETTRUNKIF pThis, PINTNETIF pIfSender, PINTNETSG pSG, uint32_t fDst)
2674{
2675 /*
2676 * Since we're only using this for GSO frame coming from the internal
2677 * network interfaces and never the trunk, we can assume there is only
2678 * one segment. This simplifies the code quite a bit.
2679 */
2680 Assert(PDMNetGsoIsValid(&pSG->GsoCtx, sizeof(pSG->GsoCtx), pSG->cbTotal));
2681 AssertReturn(pSG->cSegsUsed == 1, VERR_INTERNAL_ERROR_4);
2682
2683 union
2684 {
2685 uint8_t abBuf[sizeof(INTNETSG) + sizeof(INTNETSEG)];
2686 INTNETSG SG;
2687 } u;
2688
2689 /*
2690 * @todo: We have to adjust MSS so it does not exceed the value configured
2691 * for the host's interface.
2692 */
2693
2694 /*
2695 * Carve out the frame segments with the header and frame in different
2696 * scatter / gather segments.
2697 */
2698 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(&pSG->GsoCtx, pSG->cbTotal);
2699 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
2700 {
2701 uint32_t cbSegPayload, cbSegHdrs;
2702 uint32_t offSegPayload = PDMNetGsoCarveSegment(&pSG->GsoCtx, (uint8_t *)pSG->aSegs[0].pv, pSG->cbTotal, iSeg, cSegs,
2703 pIfSender->abGsoHdrs, &cbSegHdrs, &cbSegPayload);
2704
2705 IntNetSgInitTempSegs(&u.SG, cbSegHdrs + cbSegPayload, 2, 2);
2706 u.SG.aSegs[0].Phys = NIL_RTHCPHYS;
2707 u.SG.aSegs[0].pv = pIfSender->abGsoHdrs;
2708 u.SG.aSegs[0].cb = cbSegHdrs;
2709 u.SG.aSegs[1].Phys = NIL_RTHCPHYS;
2710 u.SG.aSegs[1].pv = (uint8_t *)pSG->aSegs[0].pv + offSegPayload;
2711 u.SG.aSegs[1].cb = (uint32_t)cbSegPayload;
2712
2713 int rc = pThis->pIfPort->pfnXmit(pThis->pIfPort, pIfSender->pvIfData, &u.SG, fDst);
2714 if (RT_FAILURE(rc))
2715 return rc;
2716 }
2717 return VINF_SUCCESS;
2718}
2719
2720
2721/**
2722 * Checks if any of the given trunk destinations can handle this kind of GSO SG.
2723 *
2724 * @returns true if it can, false if it cannot.
2725 * @param pThis The trunk.
2726 * @param pSG The scatter / gather buffer.
2727 * @param fDst The destination mask.
2728 */
2729DECLINLINE(bool) intnetR0TrunkIfCanHandleGsoFrame(PINTNETTRUNKIF pThis, PINTNETSG pSG, uint32_t fDst)
2730{
2731 uint8_t u8Type = pSG->GsoCtx.u8Type;
2732 AssertReturn(u8Type < 32, false); /* paranoia */
2733 uint32_t fMask = RT_BIT_32(u8Type);
2734
2735 if (fDst == INTNETTRUNKDIR_HOST)
2736 return !!(pThis->fHostGsoCapabilites & fMask);
2737 if (fDst == INTNETTRUNKDIR_WIRE)
2738 return !!(pThis->fWireGsoCapabilites & fMask);
2739 Assert(fDst == (INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST));
2740 return !!(pThis->fHostGsoCapabilites & pThis->fWireGsoCapabilites & fMask);
2741}
2742
2743
2744/**
2745 * Calculates the checksum of a full ipv6 frame.
2746 *
2747 * @returns 16-bit hecksum value.
2748 * @param pIpHdr The IPv6 header (network endian (big)).
2749 * @param bProtocol The protocol number. This can be the same as the
2750 * ip6_nxt field, but doesn't need to be.
2751 * @param cbPkt The packet size (host endian of course). This can
2752 * be the same as the ip6_plen field, but as with @a
2753 * bProtocol it won't be when extension headers are
2754 * present. For UDP this will be uh_ulen converted to
2755 * host endian.
2756 */
2757static uint16_t computeIPv6FullChecksum(PCRTNETIPV6 pIpHdr)
2758{
2759 uint16_t const *data;
2760 int len = RT_BE2H_U16(pIpHdr->ip6_plen);
2761 uint32_t sum = RTNetIPv6PseudoChecksum(pIpHdr);
2762
2763 /* add the payload */
2764 data = (uint16_t *) (pIpHdr + 1);
2765 while(len > 1)
2766 {
2767 sum += *(data);
2768 data++;
2769 len -= 2;
2770 }
2771
2772 if(len > 0)
2773 sum += *((uint8_t *) data);
2774
2775 while(sum >> 16)
2776 sum = (sum & 0xffff) + (sum >> 16);
2777
2778 return (uint16_t) ~sum;
2779}
2780
2781/**
2782 * Sends a frame down the trunk.
2783 *
2784 * @param pThis The trunk.
2785 * @param pNetwork The network the frame is being sent to.
2786 * @param pIfSender The IF sending the frame. Used for MAC address
2787 * checks in shared MAC mode.
2788 * @param fDst The destination flags.
2789 * @param pSG Pointer to the gather list.
2790 */
2791static void intnetR0TrunkIfSend(PINTNETTRUNKIF pThis, PINTNETNETWORK pNetwork, PINTNETIF pIfSender,
2792 uint32_t fDst, PINTNETSG pSG)
2793{
2794 /*
2795 * Quick sanity check.
2796 */
2797 AssertPtr(pThis);
2798 AssertPtr(pNetwork);
2799 AssertPtr(pIfSender);
2800 AssertPtr(pSG);
2801 Assert(fDst);
2802 AssertReturnVoid(pThis->pIfPort);
2803
2804 /*
2805 * Edit the frame if we're sharing the MAC address with the host on the wire.
2806 *
2807 * If the frame is headed for both the host and the wire, we'll have to send
2808 * it to the host before making any modifications, and force the OS specific
2809 * backend to copy it. We do this by marking it as TEMP (which is always the
2810 * case right now).
2811 */
2812 if ( (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
2813 && (fDst & INTNETTRUNKDIR_WIRE))
2814 {
2815 /*
2816 * Dispatch it to the host before making changes.
2817 */
2818 if (fDst & INTNETTRUNKDIR_HOST)
2819 {
2820 Assert(pSG->fFlags & INTNETSG_FLAGS_TEMP); /* make sure copy is forced */
2821 intnetR0TrunkIfSend(pThis, pNetwork, pIfSender, INTNETTRUNKDIR_HOST, pSG);
2822 fDst &= ~INTNETTRUNKDIR_HOST;
2823 }
2824
2825 /*
2826 * Edit the source address so that it it's the same as the host.
2827 */
2828 /* ASSUME frame from IntNetR0IfSend! */
2829 AssertReturnVoid(pSG->cSegsUsed == 1);
2830 AssertReturnVoid(pSG->cbTotal >= sizeof(RTNETETHERHDR));
2831 AssertReturnVoid(pIfSender);
2832 PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)pSG->aSegs[0].pv;
2833
2834 pEthHdr->SrcMac = pThis->MacAddr;
2835
2836 /*
2837 * Deal with tags from the snooping phase.
2838 */
2839 if (pSG->fFlags & INTNETSG_FLAGS_ARP_IPV4)
2840 {
2841 /*
2842 * APR IPv4: replace hardware (MAC) addresses because these end up
2843 * in ARP caches. So, if we don't the other machines will
2844 * send the packets to the MAC address of the guest
2845 * instead of the one of the host, which won't work on
2846 * wireless of course...
2847 */
2848 PRTNETARPIPV4 pArp = (PRTNETARPIPV4)(pEthHdr + 1);
2849 if (!memcmp(&pArp->ar_sha, &pIfSender->MacAddr, sizeof(RTMAC)))
2850 {
2851 Log6(("tw: ar_sha %.6Rhxs -> %.6Rhxs\n", &pArp->ar_sha, &pThis->MacAddr));
2852 pArp->ar_sha = pThis->MacAddr;
2853 }
2854 if (!memcmp(&pArp->ar_tha, &pIfSender->MacAddr, sizeof(RTMAC))) /* just in case... */
2855 {
2856 Log6(("tw: ar_tha %.6Rhxs -> %.6Rhxs\n", &pArp->ar_tha, &pThis->MacAddr));
2857 pArp->ar_tha = pThis->MacAddr;
2858 }
2859 }
2860 else if (pEthHdr->EtherType == RT_H2BE_U16(RTNET_ETHERTYPE_IPV6))
2861 {
2862 /*
2863 * IPV6 ICMP Neighbor Discovery : replace
2864 * 1) the advertised source mac address in outgoing neighbor sollicitations
2865 * with the HW MAC address of the trunk interface,
2866 * 2) the advertised target mac address in outgoing neighbor advertisements
2867 * with the HW mac address of the trunk interface.
2868 *
2869 * Note that this only applies to traffic going out on the trunk. Incoming
2870 * NS/NA will never advertise any VM mac address, so we do not need to touch
2871 * them. Other VMs on this bridge as well as the host will see and use the VM's
2872 * actual mac addresses.
2873 *
2874 */
2875
2876 PRTNETIPV6 pIPv6 = (PRTNETIPV6)(pEthHdr + 1);
2877 PRTNETNDP pNd = (PRTNETNDP)(pIPv6 + 1);
2878 PRTNETNDP_SLLA_OPT pLLAOpt = (PRTNETNDP_SLLA_OPT)(pNd + 1);
2879
2880 /* make sure we have enough bytes to work with */
2881 if(pSG->cbTotal >= (RTNETIPV6_MIN_LEN + RTNETIPV6_ICMPV6_ND_WITH_LLA_OPT_MIN_LEN) &&
2882 /* ensure the packet came from our LAN (not gone through any router) */
2883 pIPv6->ip6_hlim == 0xff &&
2884 /* protocol has to be icmpv6 */
2885 pIPv6->ip6_nxt == RTNETIPV6_PROT_ICMPV6 &&
2886 /* we either have a sollicitation with source link layer addr. opt, or */
2887 ((pNd->icmp6_type == RTNETIPV6_ICMP_NS_TYPE &&
2888 pNd->icmp6_code == RTNETIPV6_ICMPV6_CODE_0 &&
2889 pLLAOpt->type == RTNETIPV6_ICMP_ND_SLLA_OPT) ||
2890 /* an advertisement with target link layer addr. option */
2891 ((pNd->icmp6_type == RTNETIPV6_ICMP_NA_TYPE &&
2892 pNd->icmp6_code == RTNETIPV6_ICMPV6_CODE_0 &&
2893 pLLAOpt->type == RTNETIPV6_ICMP_ND_TLLA_OPT)) ) &&
2894 pLLAOpt->len == RTNETIPV6_ICMP_ND_LLA_LEN)
2895 {
2896 /* swap the advertised VM MAC address with the trunk's */
2897 pLLAOpt->slla = pThis->MacAddr;
2898
2899 /* recompute the checksum since we changed the packet */
2900 pNd->icmp6_cksum = 0;
2901 pNd->icmp6_cksum = computeIPv6FullChecksum(pIPv6);
2902 }
2903
2904 }
2905 }
2906
2907 /*
2908 * Send the frame, handling the GSO fallback .
2909 * .
2910 * Note! The trunk implementation will re-check that the trunk is active .
2911 * before sending, so we don't have to duplicate that effort here.
2912 */
2913 STAM_REL_PROFILE_START(&pIfSender->pIntBuf->StatSend2, a);
2914 int rc;
2915 if ( pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID
2916 || intnetR0TrunkIfCanHandleGsoFrame(pThis, pSG, fDst) )
2917 rc = pThis->pIfPort->pfnXmit(pThis->pIfPort, pIfSender->pvIfData, pSG, fDst);
2918 else
2919 rc = intnetR0TrunkIfSendGsoFallback(pThis, pIfSender, pSG, fDst);
2920 STAM_REL_PROFILE_STOP(&pIfSender->pIntBuf->StatSend2, a);
2921
2922 /** @todo failure statistics? */
2923 Log2(("intnetR0TrunkIfSend: %Rrc fDst=%d\n", rc, fDst)); NOREF(rc);
2924}
2925
2926
2927/**
2928 * Work around the issue with WiFi routers that replace IPv6 multicast
2929 * Ethernet addresses with unicast ones. We check IPv6 destination address
2930 * to determine if the packet originally had a multicast address, and if so
2931 * we restore the original address and treat the modified packet as being a
2932 * broadcast.
2933 *
2934 * @param pNetwork The network the frame is being sent to.
2935 * @param pSG Pointer to the gather list for the frame.
2936 * @param pEthHdr Pointer to the ethernet header.
2937 */
2938static bool intnetR0NetworkDetectAndFixNdBroadcast(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
2939{
2940 if (RT_BE2H_U16(pEthHdr->EtherType) != RTNET_ETHERTYPE_IPV6)
2941 return false;
2942 /*
2943 * Check the minimum size and get a linear copy of the thing to work on,
2944 * using the temporary buffer if necessary.
2945 */
2946 if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
2947 sizeof(RTNETNDP)))
2948 return false;
2949 uint8_t bTmp[sizeof(RTNETIPV6) + sizeof(RTNETNDP)];
2950 PRTNETIPV6 pIPv6 = (PRTNETIPV6)((uint8_t *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
2951 if ( pSG->cSegsUsed != 1
2952 && pSG->aSegs[0].cb < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
2953 sizeof(RTNETNDP))
2954 {
2955 Log6(("fw: Copying IPv6 pkt %u\n", sizeof(RTNETIPV6)));
2956 if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), sizeof(RTNETIPV6)
2957 + sizeof(RTNETNDP), bTmp))
2958 return false;
2959 pIPv6 = (PRTNETIPV6)bTmp;
2960 }
2961
2962 PCRTNETNDP pNd = (PCRTNETNDP) (pIPv6 + 1);
2963
2964 /* Check IPv6 destination address if it is a multicast address. */
2965 static uint8_t auSolicitedNodeMulticastPrefix[] =
2966 {
2967 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2968 0x00, 0x00, 0x00, 0x01, 0xff
2969 };
2970 if (memcmp(pIPv6->ip6_dst.au8, auSolicitedNodeMulticastPrefix,
2971 sizeof(auSolicitedNodeMulticastPrefix)) == 0)
2972 {
2973 /*
2974 * The original must have been composed of 0x3333 followed by the last
2975 * four bytes of the solicited-node multicast address.
2976 */
2977 if (pSG->aSegs[0].cb < sizeof(RTNETETHERHDR))
2978 {
2979 RTMAC DstMac;
2980 DstMac.au16[0] = 0x3333;
2981 DstMac.au16[1] = pIPv6->ip6_dst.au16[6];
2982 DstMac.au16[2] = pIPv6->ip6_dst.au16[7];
2983 return intnetR0SgWritePart(pSG, RT_OFFSETOF(RTNETETHERHDR, DstMac), sizeof(RTMAC), &DstMac);
2984 }
2985 pEthHdr = (PRTNETETHERHDR)pSG->aSegs[0].pv;
2986 pEthHdr->DstMac.au16[0] = 0x3333;
2987 pEthHdr->DstMac.au16[1] = pIPv6->ip6_dst.au16[6];
2988 pEthHdr->DstMac.au16[2] = pIPv6->ip6_dst.au16[7];
2989 return true;
2990 }
2991
2992 return false;
2993}
2994
2995
2996/**
2997 * Snoops a multicast ICMPv6 ND DAD from the wire via the trunk connection.
2998 *
2999 * @param pNetwork The network the frame is being sent to.
3000 * @param pSG Pointer to the gather list for the frame.
3001 * @param pEthHdr Pointer to the ethernet header.
3002 */
3003static void intnetR0NetworkSnoopNAFromWire(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
3004{
3005 /*
3006 * Check the minimum size and get a linear copy of the thing to work on,
3007 * using the temporary buffer if necessary.
3008 */
3009 if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
3010 sizeof(RTNETNDP)))
3011 return;
3012 PRTNETIPV6 pIPv6 = (PRTNETIPV6)((uint8_t *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
3013 if ( pSG->cSegsUsed != 1
3014 && pSG->aSegs[0].cb < sizeof(RTNETETHERHDR) + sizeof(RTNETIPV6) +
3015 sizeof(RTNETNDP))
3016 {
3017 Log6(("fw: Copying IPv6 pkt %u\n", sizeof(RTNETIPV6)));
3018 if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), sizeof(RTNETIPV6)
3019 + sizeof(RTNETNDP), pNetwork->pbTmp))
3020 return;
3021 pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
3022 pIPv6 = (PRTNETIPV6)pNetwork->pbTmp;
3023 }
3024
3025 PCRTNETNDP pNd = (PCRTNETNDP) (pIPv6 + 1);
3026
3027 /*
3028 * a multicast NS with :: as source address means a DAD packet.
3029 * if it comes from the wire and we have the DAD'd address in our cache,
3030 * flush the entry as the address is being acquired by someone else on
3031 * the network.
3032 */
3033 if ( pIPv6->ip6_hlim == 0xff
3034 && pIPv6->ip6_nxt == RTNETIPV6_PROT_ICMPV6
3035 && pNd->icmp6_type == RTNETIPV6_ICMP_NS_TYPE
3036 && pNd->icmp6_code == RTNETIPV6_ICMPV6_CODE_0
3037 && pIPv6->ip6_src.QWords.qw0 == 0
3038 && pIPv6->ip6_src.QWords.qw1 == 0)
3039 {
3040
3041 intnetR0NetworkAddrCacheDelete(pNetwork, (PCRTNETADDRU) &pNd->target_address,
3042 kIntNetAddrType_IPv6, sizeof(RTNETADDRIPV6), "tif/ip6");
3043 }
3044}
3045/**
3046 * Edits an ARP packet arriving from the wire via the trunk connection.
3047 *
3048 * @param pNetwork The network the frame is being sent to.
3049 * @param pSG Pointer to the gather list for the frame.
3050 * The flags and data content may be updated.
3051 * @param pEthHdr Pointer to the ethernet header. This may also be
3052 * updated if it's a unicast...
3053 */
3054static void intnetR0NetworkEditArpFromWire(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
3055{
3056 /*
3057 * Check the minimum size and get a linear copy of the thing to work on,
3058 * using the temporary buffer if necessary.
3059 */
3060 if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4)))
3061 return;
3062 PRTNETARPIPV4 pArpIPv4 = (PRTNETARPIPV4)((uint8_t *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
3063 if ( pSG->cSegsUsed != 1
3064 && pSG->aSegs[0].cb < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4))
3065 {
3066 Log6(("fw: Copying ARP pkt %u\n", sizeof(RTNETARPIPV4)));
3067 if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), sizeof(RTNETARPIPV4), pNetwork->pbTmp))
3068 return;
3069 pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
3070 pArpIPv4 = (PRTNETARPIPV4)pNetwork->pbTmp;
3071 }
3072
3073 /*
3074 * Ignore packets which doesn't interest us or we perceive as malformed.
3075 */
3076 if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
3077 || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
3078 || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
3079 || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
3080 return;
3081 uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
3082 if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
3083 && ar_oper != RTNET_ARPOP_REPLY))
3084 {
3085 Log6(("ar_oper=%#x\n", ar_oper));
3086 return;
3087 }
3088
3089 /* Tag it as ARP IPv4. */
3090 pSG->fFlags |= INTNETSG_FLAGS_ARP_IPV4;
3091
3092 /*
3093 * The thing we're interested in here is a reply to a query made by a guest
3094 * since we modified the MAC in the initial request the guest made.
3095 */
3096 if ( ar_oper == RTNET_ARPOP_REPLY
3097 && !memcmp(&pArpIPv4->ar_tha, &pNetwork->MacTab.pTrunk->MacAddr, sizeof(RTMAC)))
3098 {
3099 PINTNETIF pIf = intnetR0NetworkAddrCacheLookupIf(pNetwork, (PCRTNETADDRU)&pArpIPv4->ar_tpa,
3100 kIntNetAddrType_IPv4, sizeof(pArpIPv4->ar_tpa));
3101 if (pIf)
3102 {
3103 Log6(("fw: ar_tha %.6Rhxs -> %.6Rhxs\n", &pArpIPv4->ar_tha, &pIf->MacAddr));
3104 pArpIPv4->ar_tha = pIf->MacAddr;
3105 if (!memcmp(&pEthHdr->DstMac, &pNetwork->MacTab.pTrunk->MacAddr, sizeof(RTMAC)))
3106 {
3107 Log6(("fw: DstMac %.6Rhxs -> %.6Rhxs\n", &pEthHdr->DstMac, &pIf->MacAddr));
3108 pEthHdr->DstMac = pIf->MacAddr;
3109 if ((void *)pEthHdr != pSG->aSegs[0].pv)
3110 intnetR0SgWritePart(pSG, RT_OFFSETOF(RTNETETHERHDR, DstMac), sizeof(RTMAC), &pIf->MacAddr);
3111 }
3112 intnetR0BusyDecIf(pIf);
3113
3114 /* Write back the packet if we've been making changes to a buffered copy. */
3115 if (pSG->fFlags & INTNETSG_FLAGS_PKT_CP_IN_TMP)
3116 intnetR0SgWritePart(pSG, sizeof(RTNETETHERHDR), sizeof(PRTNETARPIPV4), pArpIPv4);
3117 }
3118 }
3119}
3120
3121
3122/**
3123 * Detects and edits an DHCP packet arriving from the internal net.
3124 *
3125 * @param pNetwork The network the frame is being sent to.
3126 * @param pSG Pointer to the gather list for the frame.
3127 * The flags and data content may be updated.
3128 * @param pEthHdr Pointer to the ethernet header. This may also be
3129 * updated if it's a unicast...
3130 */
3131static void intnetR0NetworkEditDhcpFromIntNet(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
3132{
3133 NOREF(pEthHdr);
3134
3135 /*
3136 * Check the minimum size and get a linear copy of the thing to work on,
3137 * using the temporary buffer if necessary.
3138 */
3139 if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN))
3140 return;
3141 /*
3142 * Get a pointer to a linear copy of the full packet, using the
3143 * temporary buffer if necessary.
3144 */
3145 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((PCRTNETETHERHDR)pSG->aSegs[0].pv + 1);
3146 uint32_t cbPacket = pSG->cbTotal - sizeof(RTNETETHERHDR);
3147 if (pSG->cSegsUsed > 1)
3148 {
3149 cbPacket = RT_MIN(cbPacket, INTNETNETWORK_TMP_SIZE);
3150 Log6(("intnetR0NetworkEditDhcpFromIntNet: Copying IPv4/UDP/DHCP pkt %u\n", cbPacket));
3151 if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
3152 return;
3153 //pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
3154 pIpHdr = (PCRTNETIPV4)pNetwork->pbTmp;
3155 }
3156
3157 /*
3158 * Validate the IP header and find the UDP packet.
3159 */
3160 if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, pSG->cbTotal - sizeof(RTNETETHERHDR), true /*fCheckSum*/))
3161 {
3162 Log6(("intnetR0NetworkEditDhcpFromIntNet: bad ip header\n"));
3163 return;
3164 }
3165 size_t cbIpHdr = pIpHdr->ip_hl * 4;
3166 if ( pIpHdr->ip_p != RTNETIPV4_PROT_UDP /* DHCP is UDP. */
3167 || cbPacket < cbIpHdr + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN) /* Min DHCP packet len */
3168 return;
3169
3170 size_t cbUdpPkt = cbPacket - cbIpHdr;
3171 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uintptr_t)pIpHdr + cbIpHdr);
3172 /* We are only interested in DHCP packets coming from client to server. */
3173 if ( RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPS
3174 || RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPC)
3175 return;
3176
3177 /*
3178 * Check if the DHCP message is valid and get the type.
3179 */
3180 if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbUdpPkt, true /*fCheckSum*/))
3181 {
3182 Log6(("intnetR0NetworkEditDhcpFromIntNet: Bad UDP packet\n"));
3183 return;
3184 }
3185 PCRTNETBOOTP pDhcp = (PCRTNETBOOTP)(pUdpHdr + 1);
3186 uint8_t bMsgType;
3187 if (!RTNetIPv4IsDHCPValid(pUdpHdr, pDhcp, cbUdpPkt - sizeof(*pUdpHdr), &bMsgType))
3188 {
3189 Log6(("intnetR0NetworkEditDhcpFromIntNet: Bad DHCP packet\n"));
3190 return;
3191 }
3192
3193 switch (bMsgType)
3194 {
3195 case RTNET_DHCP_MT_DISCOVER:
3196 case RTNET_DHCP_MT_REQUEST:
3197 /*
3198 * Must set the broadcast flag or we won't catch the respons.
3199 */
3200 if (!(pDhcp->bp_flags & RT_H2BE_U16_C(RTNET_DHCP_FLAG_BROADCAST)))
3201 {
3202 Log6(("intnetR0NetworkEditDhcpFromIntNet: Setting broadcast flag in DHCP %#x, previously %x\n",
3203 bMsgType, pDhcp->bp_flags));
3204
3205 /* Patch flags */
3206 uint16_t uFlags = pDhcp->bp_flags | RT_H2BE_U16_C(RTNET_DHCP_FLAG_BROADCAST);
3207 intnetR0SgWritePart(pSG, (uintptr_t)&pDhcp->bp_flags - (uintptr_t)pIpHdr + sizeof(RTNETETHERHDR), sizeof(uFlags), &uFlags);
3208
3209 /* Patch UDP checksum */
3210 uint32_t uChecksum = (uint32_t)~pUdpHdr->uh_sum + RT_H2BE_U16_C(RTNET_DHCP_FLAG_BROADCAST);
3211 while (uChecksum >> 16)
3212 uChecksum = (uChecksum >> 16) + (uChecksum & 0xFFFF);
3213 uChecksum = ~uChecksum;
3214 intnetR0SgWritePart(pSG, (uintptr_t)&pUdpHdr->uh_sum - (uintptr_t)pIpHdr + sizeof(RTNETETHERHDR), sizeof(pUdpHdr->uh_sum), &uChecksum);
3215 }
3216
3217#ifdef RT_OS_DARWIN
3218 /*
3219 * Work around little endian checksum issue in mac os x 10.7.0 GM.
3220 */
3221 if ( pIpHdr->ip_tos
3222 && (pNetwork->fFlags & INTNET_OPEN_FLAGS_WORKAROUND_1))
3223 {
3224 /* Patch it. */
3225 uint8_t uTos = pIpHdr->ip_tos;
3226 uint8_t uZero = 0;
3227 intnetR0SgWritePart(pSG, sizeof(RTNETETHERHDR) + 1, sizeof(uZero), &uZero);
3228
3229 /* Patch the IP header checksum. */
3230 uint32_t uChecksum = (uint32_t)~pIpHdr->ip_sum - (uTos << 8);
3231 while (uChecksum >> 16)
3232 uChecksum = (uChecksum >> 16) + (uChecksum & 0xFFFF);
3233 uChecksum = ~uChecksum;
3234
3235 Log(("intnetR0NetworkEditDhcpFromIntNet: cleared ip_tos (was %#04x); ip_sum=%#06x -> %#06x\n",
3236 uTos, RT_BE2H_U16(pIpHdr->ip_sum), RT_BE2H_U16(uChecksum) ));
3237 intnetR0SgWritePart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_sum),
3238 sizeof(pIpHdr->ip_sum), &uChecksum);
3239 }
3240#endif
3241 break;
3242 }
3243}
3244
3245
3246/**
3247 * Checks if the callers context is okay for sending to the specified
3248 * destinations.
3249 *
3250 * @returns true if it's okay, false if it isn't.
3251 * @param pNetwork The network.
3252 * @param pIfSender The interface sending or NULL if it's the trunk.
3253 * @param pDstTab The destination table.
3254 */
3255DECLINLINE(bool) intnetR0NetworkIsContextOk(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCINTNETDSTTAB pDstTab)
3256{
3257 NOREF(pNetwork);
3258
3259 /* Sending to the trunk is the problematic path. If the trunk is the
3260 sender we won't be sending to it, so no problem..
3261 Note! fTrunkDst may be set event if if the trunk is the sender. */
3262 if (!pIfSender)
3263 return true;
3264
3265 uint32_t const fTrunkDst = pDstTab->fTrunkDst;
3266 if (!fTrunkDst)
3267 return true;
3268
3269 /* ASSUMES: that the trunk won't change its report while we're checking. */
3270 PINTNETTRUNKIF pTrunk = pDstTab->pTrunk;
3271 if ((fTrunkDst & pTrunk->fNoPreemptDsts) == fTrunkDst)
3272 return true;
3273
3274 /* ASSUMES: That a preemption test detects HM contexts. (Will work on
3275 non-preemptive systems as well.) */
3276 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
3277 return true;
3278 return false;
3279}
3280
3281
3282/**
3283 * Checks if the callers context is okay for doing a broadcast given the
3284 * specified source.
3285 *
3286 * @returns true if it's okay, false if it isn't.
3287 * @param pNetwork The network.
3288 * @param fSrc The source of the packet. (0 (intnet),
3289 * INTNETTRUNKDIR_HOST or INTNETTRUNKDIR_WIRE).
3290 */
3291DECLINLINE(bool) intnetR0NetworkIsContextOkForBroadcast(PINTNETNETWORK pNetwork, uint32_t fSrc)
3292{
3293 /* Sending to the trunk is the problematic path. If the trunk is the
3294 sender we won't be sending to it, so no problem. */
3295 if (fSrc)
3296 return true;
3297
3298 /* ASSUMES: That a preemption test detects HM contexts. (Will work on
3299 non-preemptive systems as well.) */
3300 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
3301 return true;
3302
3303 /* PARANOIA: Grab the spinlock to make sure the trunk structure cannot be
3304 freed while we're touching it. */
3305 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
3306 PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
3307
3308 bool fRc = !pTrunk
3309 || pTrunk->fNoPreemptDsts == (INTNETTRUNKDIR_HOST | INTNETTRUNKDIR_WIRE)
3310 || ( (!pNetwork->MacTab.fHostActive || (pTrunk->fNoPreemptDsts & INTNETTRUNKDIR_HOST) )
3311 && (!pNetwork->MacTab.fWireActive || (pTrunk->fNoPreemptDsts & INTNETTRUNKDIR_WIRE) ) );
3312
3313 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
3314
3315 return fRc;
3316}
3317
3318
3319/**
3320 * Check context, edit, snoop and switch a broadcast frame when sharing MAC
3321 * address on the wire.
3322 *
3323 * The caller must hold at least one interface on the network busy to prevent it
3324 * from destructing beath us.
3325 *
3326 * @param pNetwork The network the frame is being sent to.
3327 * @param fSrc The source of the packet. (0 (intnet),
3328 * INTNETTRUNKDIR_HOST or INTNETTRUNKDIR_WIRE).
3329 * @param pIfSender The sender interface, NULL if trunk. Used to
3330 * prevent sending an echo to the sender.
3331 * @param pSG Pointer to the gather list.
3332 * @param pEthHdr Pointer to the ethernet header.
3333 * @param pDstTab The destination output table.
3334 */
3335static INTNETSWDECISION intnetR0NetworkSharedMacFixAndSwitchBroadcast(PINTNETNETWORK pNetwork,
3336 uint32_t fSrc, PINTNETIF pIfSender,
3337 PINTNETSG pSG, PRTNETETHERHDR pEthHdr,
3338 PINTNETDSTTAB pDstTab)
3339{
3340 /*
3341 * Before doing any work here, we need to figure out if we can handle it
3342 * in the current context. The restrictions are solely on the trunk.
3343 *
3344 * Note! Since at least one interface is busy, there won't be any changes
3345 * to the parameters here (unless the trunk changes its capability
3346 * report, which it shouldn't).
3347 */
3348 if (!intnetR0NetworkIsContextOkForBroadcast(pNetwork, fSrc))
3349 return INTNETSWDECISION_BAD_CONTEXT;
3350
3351 /*
3352 * Check for ICMPv6 Neighbor Advertisements coming from the trunk.
3353 * If we see an advertisement for an IP in our cache, we can safely remove
3354 * it as the IP has probably moved.
3355 */
3356 if ( (fSrc & INTNETTRUNKDIR_WIRE)
3357 && RT_BE2H_U16(pEthHdr->EtherType) == RTNET_ETHERTYPE_IPV6
3358 && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
3359 intnetR0NetworkSnoopNAFromWire(pNetwork, pSG, pEthHdr);
3360
3361
3362 /*
3363 * Check for ARP packets from the wire since we'll have to make
3364 * modification to them if we're sharing the MAC address with the host.
3365 */
3366 if ( (fSrc & INTNETTRUNKDIR_WIRE)
3367 && RT_BE2H_U16(pEthHdr->EtherType) == RTNET_ETHERTYPE_ARP
3368 && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
3369 intnetR0NetworkEditArpFromWire(pNetwork, pSG, pEthHdr);
3370
3371 /*
3372 * Check for DHCP packets from the internal net since we'll have to set
3373 * broadcast flag in DHCP requests if we're sharing the MAC address with
3374 * the host. GSO is not applicable to DHCP traffic.
3375 */
3376 if ( !fSrc
3377 && RT_BE2H_U16(pEthHdr->EtherType) == RTNET_ETHERTYPE_IPV4
3378 && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
3379 intnetR0NetworkEditDhcpFromIntNet(pNetwork, pSG, pEthHdr);
3380
3381 /*
3382 * Snoop address info from packet originating from the trunk connection.
3383 */
3384 if (fSrc)
3385 {
3386#ifdef INTNET_WITH_DHCP_SNOOPING
3387 uint16_t EtherType = RT_BE2H_U16(pEthHdr->EtherType);
3388 if ( ( EtherType == RTNET_ETHERTYPE_IPV4 /* for DHCP */
3389 && pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN
3390 && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID )
3391 || (pSG->fFlags & INTNETSG_FLAGS_ARP_IPV4) )
3392 intnetR0TrunkIfSnoopAddr(pNetwork, pSG, EtherType);
3393#else
3394 if (pSG->fFlags & INTNETSG_FLAGS_ARP_IPV4)
3395 intnetR0TrunkIfSnoopArp(pNetwork, pSG);
3396#endif
3397 }
3398
3399 /*
3400 * Create the broadcast destination table.
3401 */
3402 return intnetR0NetworkSwitchBroadcast(pNetwork, fSrc, pIfSender, pDstTab);
3403}
3404
3405
3406/**
3407 * Check context, snoop and switch a unicast frame using the network layer
3408 * address of the link layer one (when sharing MAC address on the wire).
3409 *
3410 * This function is only used for frames coming from the wire (trunk).
3411 *
3412 * @returns true if it's addressed to someone on the network, otherwise false.
3413 * @param pNetwork The network the frame is being sent to.
3414 * @param pSG Pointer to the gather list.
3415 * @param pEthHdr Pointer to the ethernet header.
3416 * @param pDstTab The destination output table.
3417 */
3418static INTNETSWDECISION intnetR0NetworkSharedMacFixAndSwitchUnicast(PINTNETNETWORK pNetwork, PINTNETSG pSG,
3419 PRTNETETHERHDR pEthHdr, PINTNETDSTTAB pDstTab)
3420{
3421 /*
3422 * Extract the network address from the packet.
3423 */
3424 RTNETADDRU Addr;
3425 INTNETADDRTYPE enmAddrType;
3426 uint8_t cbAddr;
3427 switch (RT_BE2H_U16(pEthHdr->EtherType))
3428 {
3429 case RTNET_ETHERTYPE_IPV4:
3430 if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_dst), sizeof(Addr.IPv4), &Addr)))
3431 {
3432 Log(("intnetshareduni: failed to read ip_dst! cbTotal=%#x\n", pSG->cbTotal));
3433 return intnetR0NetworkSwitchTrunk(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
3434 }
3435 enmAddrType = kIntNetAddrType_IPv4;
3436 cbAddr = sizeof(Addr.IPv4);
3437 Log6(("intnetshareduni: IPv4 %d.%d.%d.%d\n", Addr.au8[0], Addr.au8[1], Addr.au8[2], Addr.au8[3]));
3438 break;
3439
3440 case RTNET_ETHERTYPE_IPV6:
3441 if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV6, ip6_dst), sizeof(Addr.IPv6), &Addr)))
3442 {
3443 Log(("intnetshareduni: failed to read ip6_dst! cbTotal=%#x\n", pSG->cbTotal));
3444 return intnetR0NetworkSwitchTrunk(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
3445 }
3446 enmAddrType = kIntNetAddrType_IPv6;
3447 cbAddr = sizeof(Addr.IPv6);
3448 break;
3449#if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
3450 case RTNET_ETHERTYPE_IPX_1:
3451 case RTNET_ETHERTYPE_IPX_2:
3452 case RTNET_ETHERTYPE_IPX_3:
3453 if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPX, ipx_dstnet), sizeof(Addr.IPX), &Addr)))
3454 {
3455 Log(("intnetshareduni: failed to read ipx_dstnet! cbTotal=%#x\n", pSG->cbTotal));
3456 return intnetR0NetworkSwitchTrunk(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
3457 }
3458 enmAddrType = kIntNetAddrType_IPX;
3459 cbAddr = sizeof(Addr.IPX);
3460 break;
3461#endif
3462
3463 /*
3464 * Treat ARP as broadcast (it shouldn't end up here normally,
3465 * so it goes last in the switch).
3466 */
3467 case RTNET_ETHERTYPE_ARP:
3468 Log6(("intnetshareduni: ARP\n"));
3469 /** @todo revisit this broadcasting of unicast ARP frames! */
3470 return intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, INTNETTRUNKDIR_WIRE, NULL, pSG, pEthHdr, pDstTab);
3471
3472 /*
3473 * Unknown packets are sent to the trunk and any promiscuous interfaces.
3474 */
3475 default:
3476 {
3477 Log6(("intnetshareduni: unknown ethertype=%#x\n", RT_BE2H_U16(pEthHdr->EtherType)));
3478 return intnetR0NetworkSwitchTrunkAndPromisc(pNetwork, INTNETTRUNKDIR_WIRE, pDstTab);
3479 }
3480 }
3481
3482 /*
3483 * Do level-3 switching.
3484 */
3485 INTNETSWDECISION enmSwDecision = intnetR0NetworkSwitchLevel3(pNetwork, &pEthHdr->DstMac,
3486 enmAddrType, &Addr, cbAddr,
3487 INTNETTRUNKDIR_WIRE, pDstTab);
3488
3489#ifdef INTNET_WITH_DHCP_SNOOPING
3490 /*
3491 * Perform DHCP snooping. GSO is not applicable to DHCP traffic
3492 */
3493 if ( enmAddrType == kIntNetAddrType_IPv4
3494 && pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN
3495 && pSG->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
3496 intnetR0TrunkIfSnoopAddr(pNetwork, pSG, RT_BE2H_U16(pEthHdr->EtherType));
3497#endif /* INTNET_WITH_DHCP_SNOOPING */
3498
3499 return enmSwDecision;
3500}
3501
3502
3503/**
3504 * Release all the interfaces in the destination table when we realize that
3505 * we're in a context where we cannot get the job done.
3506 *
3507 * @param pNetwork The network.
3508 * @param pDstTab The destination table.
3509 */
3510static void intnetR0NetworkReleaseDstTab(PINTNETNETWORK pNetwork, PINTNETDSTTAB pDstTab)
3511{
3512 /* The trunk interface. */
3513 if (pDstTab->fTrunkDst)
3514 {
3515 PINTNETTRUNKIF pTrunk = pDstTab->pTrunk;
3516 intnetR0BusyDec(pNetwork, &pTrunk->cBusy);
3517 pDstTab->pTrunk = NULL;
3518 pDstTab->fTrunkDst = 0;
3519 }
3520
3521 /* Regular interfaces. */
3522 uint32_t iIf = pDstTab->cIfs;
3523 while (iIf-- > 0)
3524 {
3525 PINTNETIF pIf = pDstTab->aIfs[iIf].pIf;
3526 intnetR0BusyDecIf(pIf);
3527 pDstTab->aIfs[iIf].pIf = NULL;
3528 }
3529 pDstTab->cIfs = 0;
3530}
3531
3532
3533/**
3534 * Deliver the frame to the interfaces specified in the destination table.
3535 *
3536 * @param pNetwork The network.
3537 * @param pDstTab The destination table.
3538 * @param pSG The frame to send.
3539 * @param pIfSender The sender interface. NULL if it originated via
3540 * the trunk.
3541 */
3542static void intnetR0NetworkDeliver(PINTNETNETWORK pNetwork, PINTNETDSTTAB pDstTab, PINTNETSG pSG, PINTNETIF pIfSender)
3543{
3544 /*
3545 * Do the interfaces first before sending it to the wire and risk having to
3546 * modify it.
3547 */
3548 uint32_t iIf = pDstTab->cIfs;
3549 while (iIf-- > 0)
3550 {
3551 PINTNETIF pIf = pDstTab->aIfs[iIf].pIf;
3552 intnetR0IfSend(pIf, pIfSender, pSG,
3553 pDstTab->aIfs[iIf].fReplaceDstMac ? &pIf->MacAddr: NULL);
3554 intnetR0BusyDecIf(pIf);
3555 pDstTab->aIfs[iIf].pIf = NULL;
3556 }
3557 pDstTab->cIfs = 0;
3558
3559 /*
3560 * Send to the trunk.
3561 *
3562 * Note! The switching functions will include the trunk even when the frame
3563 * source is the trunk. This is because we need it to figure out
3564 * whether the other half of the trunk should see the frame or not
3565 * and let the caller know.
3566 *
3567 * So, we'll ignore trunk sends here if the frame origin is
3568 * INTNETTRUNKSWPORT::pfnRecv.
3569 */
3570 if (pDstTab->fTrunkDst)
3571 {
3572 PINTNETTRUNKIF pTrunk = pDstTab->pTrunk;
3573 if (pIfSender)
3574 intnetR0TrunkIfSend(pTrunk, pNetwork, pIfSender, pDstTab->fTrunkDst, pSG);
3575 intnetR0BusyDec(pNetwork, &pTrunk->cBusy);
3576 pDstTab->pTrunk = NULL;
3577 pDstTab->fTrunkDst = 0;
3578 }
3579}
3580
3581
3582/**
3583 * Sends a frame.
3584 *
3585 * This function will distribute the frame to the interfaces it is addressed to.
3586 * It will also update the MAC address of the sender.
3587 *
3588 * The caller must own the network mutex.
3589 *
3590 * @returns The switching decision.
3591 * @param pNetwork The network the frame is being sent to.
3592 * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
3593 * @param fSrc The source flags. This 0 if it's not from the trunk.
3594 * @param pSG Pointer to the gather list.
3595 * @param pDstTab The destination table to use.
3596 */
3597static INTNETSWDECISION intnetR0NetworkSend(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc,
3598 PINTNETSG pSG, PINTNETDSTTAB pDstTab)
3599{
3600 /*
3601 * Assert reality.
3602 */
3603 AssertPtr(pNetwork);
3604 AssertPtrNull(pIfSender);
3605 Assert(pIfSender ? fSrc == 0 : fSrc != 0);
3606 Assert(!pIfSender || pNetwork == pIfSender->pNetwork);
3607 AssertPtr(pSG);
3608 Assert(pSG->cSegsUsed >= 1);
3609 Assert(pSG->cSegsUsed <= pSG->cSegsAlloc);
3610 if (pSG->cbTotal < sizeof(RTNETETHERHDR))
3611 return INTNETSWDECISION_INVALID;
3612
3613 /*
3614 * Get the ethernet header (might theoretically involve multiple segments).
3615 */
3616 RTNETETHERHDR EthHdr;
3617 if (pSG->aSegs[0].cb >= sizeof(EthHdr))
3618 EthHdr = *(PCRTNETETHERHDR)pSG->aSegs[0].pv;
3619 else if (!intnetR0SgReadPart(pSG, 0, sizeof(EthHdr), &EthHdr))
3620 return INTNETSWDECISION_INVALID;
3621 if ( (EthHdr.DstMac.au8[0] == 0x08 && EthHdr.DstMac.au8[1] == 0x00 && EthHdr.DstMac.au8[2] == 0x27)
3622 || (EthHdr.SrcMac.au8[0] == 0x08 && EthHdr.SrcMac.au8[1] == 0x00 && EthHdr.SrcMac.au8[2] == 0x27)
3623 || (EthHdr.DstMac.au8[0] == 0x00 && EthHdr.DstMac.au8[1] == 0x16 && EthHdr.DstMac.au8[2] == 0xcb)
3624 || (EthHdr.SrcMac.au8[0] == 0x00 && EthHdr.SrcMac.au8[1] == 0x16 && EthHdr.SrcMac.au8[2] == 0xcb)
3625 || EthHdr.DstMac.au8[0] == 0xff
3626 || EthHdr.SrcMac.au8[0] == 0xff)
3627 Log2(("D=%.6Rhxs S=%.6Rhxs T=%04x f=%x z=%x\n",
3628 &EthHdr.DstMac, &EthHdr.SrcMac, RT_BE2H_U16(EthHdr.EtherType), fSrc, pSG->cbTotal));
3629
3630 /*
3631 * Learn the MAC address of the sender. No re-learning as the interface
3632 * user will normally tell us the right MAC address.
3633 *
3634 * Note! We don't notify the trunk about these mainly because of the
3635 * problematic contexts we might be called in.
3636 */
3637 if (RT_UNLIKELY( pIfSender
3638 && !pIfSender->fMacSet
3639 && memcmp(&EthHdr.SrcMac, &pIfSender->MacAddr, sizeof(pIfSender->MacAddr))
3640 && !intnetR0IsMacAddrMulticast(&EthHdr.SrcMac)
3641 ))
3642 {
3643 Log2(("IF MAC: %.6Rhxs -> %.6Rhxs\n", &pIfSender->MacAddr, &EthHdr.SrcMac));
3644 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
3645
3646 PINTNETMACTABENTRY pIfEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIfSender);
3647 if (pIfEntry)
3648 pIfEntry->MacAddr = EthHdr.SrcMac;
3649 pIfSender->MacAddr = EthHdr.SrcMac;
3650
3651 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
3652 }
3653
3654 /*
3655 * Deal with MAC address sharing as that may required editing of the
3656 * packets before we dispatch them anywhere.
3657 */
3658 INTNETSWDECISION enmSwDecision;
3659 if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
3660 {
3661 if (intnetR0IsMacAddrMulticast(&EthHdr.DstMac))
3662 enmSwDecision = intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, fSrc, pIfSender, pSG, &EthHdr, pDstTab);
3663 else if (fSrc & INTNETTRUNKDIR_WIRE)
3664 {
3665 if (intnetR0NetworkDetectAndFixNdBroadcast(pNetwork, pSG, &EthHdr))
3666 enmSwDecision = intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, fSrc, pIfSender, pSG, &EthHdr, pDstTab);
3667 else
3668 enmSwDecision = intnetR0NetworkSharedMacFixAndSwitchUnicast(pNetwork, pSG, &EthHdr, pDstTab);
3669 }
3670 else
3671 enmSwDecision = intnetR0NetworkSwitchUnicast(pNetwork, fSrc, pIfSender, &EthHdr.DstMac, pDstTab);
3672 }
3673 else if (intnetR0IsMacAddrMulticast(&EthHdr.DstMac))
3674 enmSwDecision = intnetR0NetworkSwitchBroadcast(pNetwork, fSrc, pIfSender, pDstTab);
3675 else
3676 enmSwDecision = intnetR0NetworkSwitchUnicast(pNetwork, fSrc, pIfSender, &EthHdr.DstMac, pDstTab);
3677
3678 /*
3679 * Deliver to the destinations if we can.
3680 */
3681 if (enmSwDecision != INTNETSWDECISION_BAD_CONTEXT)
3682 {
3683 if (intnetR0NetworkIsContextOk(pNetwork, pIfSender, pDstTab))
3684 intnetR0NetworkDeliver(pNetwork, pDstTab, pSG, pIfSender);
3685 else
3686 {
3687 intnetR0NetworkReleaseDstTab(pNetwork, pDstTab);
3688 enmSwDecision = INTNETSWDECISION_BAD_CONTEXT;
3689 }
3690 }
3691
3692 return enmSwDecision;
3693}
3694
3695
3696/**
3697 * Sends one or more frames.
3698 *
3699 * The function will first the frame which is passed as the optional arguments
3700 * pvFrame and cbFrame. These are optional since it also possible to chain
3701 * together one or more frames in the send buffer which the function will
3702 * process after considering it's arguments.
3703 *
3704 * The caller is responsible for making sure that there are no concurrent calls
3705 * to this method (with the same handle).
3706 *
3707 * @returns VBox status code.
3708 * @param hIf The interface handle.
3709 * @param pSession The caller's session.
3710 */
3711INTNETR0DECL(int) IntNetR0IfSend(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession)
3712{
3713 Log5(("IntNetR0IfSend: hIf=%RX32\n", hIf));
3714
3715 /*
3716 * Validate input and translate the handle.
3717 */
3718 PINTNET pIntNet = g_pIntNet;
3719 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
3720 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
3721
3722 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
3723 if (!pIf)
3724 return VERR_INVALID_HANDLE;
3725 STAM_REL_PROFILE_START(&pIf->pIntBuf->StatSend1, a);
3726
3727 /*
3728 * Make sure we've got a network.
3729 */
3730 int rc = VINF_SUCCESS;
3731 intnetR0BusyIncIf(pIf);
3732 PINTNETNETWORK pNetwork = pIf->pNetwork;
3733 if (RT_LIKELY(pNetwork))
3734 {
3735 /*
3736 * Grab the destination table.
3737 */
3738 PINTNETDSTTAB pDstTab = ASMAtomicXchgPtrT(&pIf->pDstTab, NULL, PINTNETDSTTAB);
3739 if (RT_LIKELY(pDstTab))
3740 {
3741 /*
3742 * Process the send buffer.
3743 */
3744 INTNETSWDECISION enmSwDecision = INTNETSWDECISION_BROADCAST;
3745 INTNETSG Sg; /** @todo this will have to be changed if we're going to use async sending
3746 * with buffer sharing for some OS or service. Darwin copies everything so
3747 * I won't bother allocating and managing SGs right now. Sorry. */
3748 PINTNETHDR pHdr;
3749 while ((pHdr = IntNetRingGetNextFrameToRead(&pIf->pIntBuf->Send)) != NULL)
3750 {
3751 uint8_t const u8Type = pHdr->u8Type;
3752 if (u8Type == INTNETHDR_TYPE_FRAME)
3753 {
3754 /* Send regular frame. */
3755 void *pvCurFrame = IntNetHdrGetFramePtr(pHdr, pIf->pIntBuf);
3756 IntNetSgInitTemp(&Sg, pvCurFrame, pHdr->cbFrame);
3757 if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
3758 intnetR0IfSnoopAddr(pIf, (uint8_t *)pvCurFrame, pHdr->cbFrame, false /*fGso*/, (uint16_t *)&Sg.fFlags);
3759 enmSwDecision = intnetR0NetworkSend(pNetwork, pIf, 0 /*fSrc*/, &Sg, pDstTab);
3760 }
3761 else if (u8Type == INTNETHDR_TYPE_GSO)
3762 {
3763 /* Send GSO frame if sane. */
3764 PPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr, pIf->pIntBuf);
3765 uint32_t cbFrame = pHdr->cbFrame - sizeof(*pGso);
3766 if (RT_LIKELY(PDMNetGsoIsValid(pGso, pHdr->cbFrame, cbFrame)))
3767 {
3768 void *pvCurFrame = pGso + 1;
3769 IntNetSgInitTempGso(&Sg, pvCurFrame, cbFrame, pGso);
3770 if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
3771 intnetR0IfSnoopAddr(pIf, (uint8_t *)pvCurFrame, cbFrame, true /*fGso*/, (uint16_t *)&Sg.fFlags);
3772 enmSwDecision = intnetR0NetworkSend(pNetwork, pIf, 0 /*fSrc*/, &Sg, pDstTab);
3773 }
3774 else
3775 {
3776 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatBadFrames); /* ignore */
3777 enmSwDecision = INTNETSWDECISION_DROP;
3778 }
3779 }
3780 /* Unless it's a padding frame, we're getting babble from the producer. */
3781 else
3782 {
3783 if (u8Type != INTNETHDR_TYPE_PADDING)
3784 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatBadFrames); /* ignore */
3785 enmSwDecision = INTNETSWDECISION_DROP;
3786 }
3787 if (enmSwDecision == INTNETSWDECISION_BAD_CONTEXT)
3788 {
3789 rc = VERR_TRY_AGAIN;
3790 break;
3791 }
3792
3793 /* Skip to the next frame. */
3794 IntNetRingSkipFrame(&pIf->pIntBuf->Send);
3795 }
3796
3797 /*
3798 * Put back the destination table.
3799 */
3800 Assert(!pIf->pDstTab);
3801 ASMAtomicWritePtr(&pIf->pDstTab, pDstTab);
3802 }
3803 else
3804 rc = VERR_INTERNAL_ERROR_4;
3805 }
3806 else
3807 rc = VERR_INTERNAL_ERROR_3;
3808
3809 /*
3810 * Release the interface.
3811 */
3812 intnetR0BusyDecIf(pIf);
3813 STAM_REL_PROFILE_STOP(&pIf->pIntBuf->StatSend1, a);
3814 intnetR0IfRelease(pIf, pSession);
3815 return rc;
3816}
3817
3818
3819/**
3820 * VMMR0 request wrapper for IntNetR0IfSend.
3821 *
3822 * @returns see IntNetR0IfSend.
3823 * @param pSession The caller's session.
3824 * @param pReq The request packet.
3825 */
3826INTNETR0DECL(int) IntNetR0IfSendReq(PSUPDRVSESSION pSession, PINTNETIFSENDREQ pReq)
3827{
3828 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
3829 return VERR_INVALID_PARAMETER;
3830 return IntNetR0IfSend(pReq->hIf, pSession);
3831}
3832
3833
3834/**
3835 * Maps the default buffer into ring 3.
3836 *
3837 * @returns VBox status code.
3838 * @param hIf The interface handle.
3839 * @param pSession The caller's session.
3840 * @param ppRing3Buf Where to store the address of the ring-3 mapping
3841 * (optional).
3842 * @param ppRing0Buf Where to store the address of the ring-0 mapping
3843 * (optional).
3844 */
3845INTNETR0DECL(int) IntNetR0IfGetBufferPtrs(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession,
3846 R3PTRTYPE(PINTNETBUF) *ppRing3Buf, R0PTRTYPE(PINTNETBUF) *ppRing0Buf)
3847{
3848 LogFlow(("IntNetR0IfGetBufferPtrs: hIf=%RX32 ppRing3Buf=%p ppRing0Buf=%p\n", hIf, ppRing3Buf, ppRing0Buf));
3849
3850 /*
3851 * Validate input.
3852 */
3853 PINTNET pIntNet = g_pIntNet;
3854 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
3855 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
3856
3857 AssertPtrNullReturn(ppRing3Buf, VERR_INVALID_PARAMETER);
3858 AssertPtrNullReturn(ppRing0Buf, VERR_INVALID_PARAMETER);
3859 if (ppRing3Buf)
3860 *ppRing3Buf = 0;
3861 if (ppRing0Buf)
3862 *ppRing0Buf = 0;
3863
3864 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
3865 if (!pIf)
3866 return VERR_INVALID_HANDLE;
3867
3868 /*
3869 * ASSUMES that only the process that created an interface can use it.
3870 * ASSUMES that we created the ring-3 mapping when selecting or
3871 * allocating the buffer.
3872 */
3873 int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
3874 if (RT_SUCCESS(rc))
3875 {
3876 if (ppRing3Buf)
3877 *ppRing3Buf = pIf->pIntBufR3;
3878 if (ppRing0Buf)
3879 *ppRing0Buf = (R0PTRTYPE(PINTNETBUF))pIf->pIntBuf; /* tstIntNetR0 mess */
3880
3881 rc = RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
3882 }
3883
3884 intnetR0IfRelease(pIf, pSession);
3885 LogFlow(("IntNetR0IfGetBufferPtrs: returns %Rrc *ppRing3Buf=%p *ppRing0Buf=%p\n",
3886 rc, ppRing3Buf ? *ppRing3Buf : NIL_RTR3PTR, ppRing0Buf ? *ppRing0Buf : NIL_RTR0PTR));
3887 return rc;
3888}
3889
3890
3891/**
3892 * VMMR0 request wrapper for IntNetR0IfGetBufferPtrs.
3893 *
3894 * @returns see IntNetR0IfGetRing3Buffer.
3895 * @param pSession The caller's session.
3896 * @param pReq The request packet.
3897 */
3898INTNETR0DECL(int) IntNetR0IfGetBufferPtrsReq(PSUPDRVSESSION pSession, PINTNETIFGETBUFFERPTRSREQ pReq)
3899{
3900 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
3901 return VERR_INVALID_PARAMETER;
3902 return IntNetR0IfGetBufferPtrs(pReq->hIf, pSession, &pReq->pRing3Buf, &pReq->pRing0Buf);
3903}
3904
3905
3906#if 0
3907/**
3908 * Gets the physical addresses of the default interface buffer.
3909 *
3910 * @returns VBox status code.
3911 * @param hIF The interface handle.
3912 * @param paPages Where to store the addresses. (The reserved fields will be set to zero.)
3913 * @param cPages
3914 */
3915INTNETR0DECL(int) IntNetR0IfGetPhysBuffer(INTNETIFHANDLE hIf, PSUPPAGE paPages, unsigned cPages)
3916{
3917 /*
3918 * Validate input.
3919 */
3920 PINTNET pIntNet = g_pIntNet;
3921 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
3922 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
3923
3924 AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
3925 AssertPtrReturn((uint8_t *)&paPages[cPages] - 1, VERR_INVALID_PARAMETER);
3926 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
3927 if (!pIf)
3928 return VERR_INVALID_HANDLE;
3929
3930 /*
3931 * Grab the lock and get the data.
3932 * ASSUMES that the handle isn't closed while we're here.
3933 */
3934 int rc = RTSemFastMutexRequest(pIf->pNetwork->FastMutex);
3935 if (RT_SUCCESS(rc))
3936 {
3937 /** @todo make a SUPR0 api for obtaining the array. SUPR0/IPRT is keeping track of everything, there
3938 * is no need for any extra bookkeeping here.. */
3939
3940 rc = RTSemFastMutexRelease(pIf->pNetwork->FastMutex);
3941 }
3942 intnetR0IfRelease(pIf, pSession);
3943 return VERR_NOT_IMPLEMENTED;
3944}
3945#endif
3946
3947
3948/**
3949 * Sets the promiscuous mode property of an interface.
3950 *
3951 * @returns VBox status code.
3952 * @param hIf The interface handle.
3953 * @param pSession The caller's session.
3954 * @param fPromiscuous Set if the interface should be in promiscuous mode, clear if not.
3955 */
3956INTNETR0DECL(int) IntNetR0IfSetPromiscuousMode(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fPromiscuous)
3957{
3958 LogFlow(("IntNetR0IfSetPromiscuousMode: hIf=%RX32 fPromiscuous=%d\n", hIf, fPromiscuous));
3959
3960 /*
3961 * Validate & translate input.
3962 */
3963 PINTNET pIntNet = g_pIntNet;
3964 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
3965 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
3966
3967 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
3968 if (!pIf)
3969 {
3970 Log(("IntNetR0IfSetPromiscuousMode: returns VERR_INVALID_HANDLE\n"));
3971 return VERR_INVALID_HANDLE;
3972 }
3973
3974 /*
3975 * Get the network, take the address spinlock, and make the change.
3976 * Paranoia^2: Mark ourselves busy to prevent anything from being destroyed.
3977 */
3978 int rc = VINF_SUCCESS;
3979 intnetR0BusyIncIf(pIf);
3980 PINTNETNETWORK pNetwork = pIf->pNetwork;
3981 if (pNetwork)
3982 {
3983 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
3984
3985 if (pIf->fPromiscuousReal != fPromiscuous)
3986 {
3987 const bool fPromiscuousEff = fPromiscuous
3988 && (pIf->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW)
3989 && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS);
3990 Log(("IntNetR0IfSetPromiscuousMode: hIf=%RX32: Changed from %d -> %d (%d)\n",
3991 hIf, !fPromiscuous, !!fPromiscuous, fPromiscuousEff));
3992
3993 pIf->fPromiscuousReal = fPromiscuous;
3994
3995 PINTNETMACTABENTRY pEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIf); Assert(pEntry);
3996 if (RT_LIKELY(pEntry))
3997 {
3998 if (pEntry->fPromiscuousEff)
3999 {
4000 pNetwork->MacTab.cPromiscuousEntries--;
4001 if (!pEntry->fPromiscuousSeeTrunk)
4002 pNetwork->MacTab.cPromiscuousNoTrunkEntries--;
4003 Assert(pNetwork->MacTab.cPromiscuousEntries < pNetwork->MacTab.cEntries);
4004 Assert(pNetwork->MacTab.cPromiscuousNoTrunkEntries < pNetwork->MacTab.cEntries);
4005 }
4006
4007 pEntry->fPromiscuousEff = fPromiscuousEff;
4008 pEntry->fPromiscuousSeeTrunk = fPromiscuousEff
4009 && (pIf->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK);
4010
4011 if (pEntry->fPromiscuousEff)
4012 {
4013 pNetwork->MacTab.cPromiscuousEntries++;
4014 if (!pEntry->fPromiscuousSeeTrunk)
4015 pNetwork->MacTab.cPromiscuousNoTrunkEntries++;
4016 }
4017 Assert(pNetwork->MacTab.cPromiscuousEntries <= pNetwork->MacTab.cEntries);
4018 Assert(pNetwork->MacTab.cPromiscuousNoTrunkEntries <= pNetwork->MacTab.cEntries);
4019 }
4020 }
4021
4022 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4023 }
4024 else
4025 rc = VERR_WRONG_ORDER;
4026
4027 intnetR0BusyDecIf(pIf);
4028 intnetR0IfRelease(pIf, pSession);
4029 return rc;
4030}
4031
4032
4033/**
4034 * VMMR0 request wrapper for IntNetR0IfSetPromiscuousMode.
4035 *
4036 * @returns see IntNetR0IfSetPromiscuousMode.
4037 * @param pSession The caller's session.
4038 * @param pReq The request packet.
4039 */
4040INTNETR0DECL(int) IntNetR0IfSetPromiscuousModeReq(PSUPDRVSESSION pSession, PINTNETIFSETPROMISCUOUSMODEREQ pReq)
4041{
4042 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
4043 return VERR_INVALID_PARAMETER;
4044 return IntNetR0IfSetPromiscuousMode(pReq->hIf, pSession, pReq->fPromiscuous);
4045}
4046
4047
4048/**
4049 * Sets the MAC address of an interface.
4050 *
4051 * @returns VBox status code.
4052 * @param hIf The interface handle.
4053 * @param pSession The caller's session.
4054 * @param pMAC The new MAC address.
4055 */
4056INTNETR0DECL(int) IntNetR0IfSetMacAddress(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PCRTMAC pMac)
4057{
4058 LogFlow(("IntNetR0IfSetMacAddress: hIf=%RX32 pMac=%p:{%.6Rhxs}\n", hIf, pMac, pMac));
4059
4060 /*
4061 * Validate & translate input.
4062 */
4063 PINTNET pIntNet = g_pIntNet;
4064 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
4065 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
4066
4067 AssertPtrReturn(pMac, VERR_INVALID_PARAMETER);
4068 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
4069 if (!pIf)
4070 {
4071 Log(("IntNetR0IfSetMacAddress: returns VERR_INVALID_HANDLE\n"));
4072 return VERR_INVALID_HANDLE;
4073 }
4074
4075 /*
4076 * Get the network, take the address spinlock, and make the change.
4077 * Paranoia^2: Mark ourselves busy to prevent anything from being destroyed.
4078 */
4079 int rc = VINF_SUCCESS;
4080 intnetR0BusyIncIf(pIf);
4081 PINTNETNETWORK pNetwork = pIf->pNetwork;
4082 if (pNetwork)
4083 {
4084 PINTNETTRUNKIF pTrunk = NULL;
4085
4086 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
4087
4088 if (memcmp(&pIf->MacAddr, pMac, sizeof(pIf->MacAddr)))
4089 {
4090 Log(("IntNetR0IfSetMacAddress: hIf=%RX32: Changed from %.6Rhxs -> %.6Rhxs\n",
4091 hIf, &pIf->MacAddr, pMac));
4092
4093 /* Update the two copies. */
4094 PINTNETMACTABENTRY pEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIf); Assert(pEntry);
4095 if (RT_LIKELY(pEntry))
4096 pEntry->MacAddr = *pMac;
4097 pIf->MacAddr = *pMac;
4098 pIf->fMacSet = true;
4099
4100 /* Grab a busy reference to the trunk so we release the lock before notifying it. */
4101 pTrunk = pNetwork->MacTab.pTrunk;
4102 if (pTrunk)
4103 intnetR0BusyIncTrunk(pTrunk);
4104 }
4105
4106 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4107
4108 if (pTrunk)
4109 {
4110 Log(("IntNetR0IfSetMacAddress: pfnNotifyMacAddress hIf=%RX32\n", hIf));
4111 PINTNETTRUNKIFPORT pIfPort = pTrunk->pIfPort;
4112 if (pIfPort)
4113 pIfPort->pfnNotifyMacAddress(pIfPort, pIf->pvIfData, pMac);
4114 intnetR0BusyDecTrunk(pTrunk);
4115 }
4116 }
4117 else
4118 rc = VERR_WRONG_ORDER;
4119
4120 intnetR0BusyDecIf(pIf);
4121 intnetR0IfRelease(pIf, pSession);
4122 return rc;
4123}
4124
4125
4126/**
4127 * VMMR0 request wrapper for IntNetR0IfSetMacAddress.
4128 *
4129 * @returns see IntNetR0IfSetMacAddress.
4130 * @param pSession The caller's session.
4131 * @param pReq The request packet.
4132 */
4133INTNETR0DECL(int) IntNetR0IfSetMacAddressReq(PSUPDRVSESSION pSession, PINTNETIFSETMACADDRESSREQ pReq)
4134{
4135 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
4136 return VERR_INVALID_PARAMETER;
4137 return IntNetR0IfSetMacAddress(pReq->hIf, pSession, &pReq->Mac);
4138}
4139
4140
4141/**
4142 * Worker for intnetR0IfSetActive and intnetR0IfDestruct.
4143 *
4144 * This function will update the active interface count on the network and
4145 * activate or deactivate the trunk connection if necessary.
4146 *
4147 * The call must own the giant lock (we cannot take it here).
4148 *
4149 * @returns VBox status code.
4150 * @param pNetwork The network.
4151 * @param fIf The interface.
4152 * @param fActive What to do.
4153 */
4154static int intnetR0NetworkSetIfActive(PINTNETNETWORK pNetwork, PINTNETIF pIf, bool fActive)
4155{
4156 /* quick sanity check */
4157 AssertPtr(pNetwork);
4158 AssertPtr(pIf);
4159
4160 /*
4161 * The address spinlock of the network protects the variables, while the
4162 * big lock protects the calling of pfnSetState. Grab both lock at once
4163 * to save us the extra hassle.
4164 */
4165 PINTNETTRUNKIF pTrunk = NULL;
4166 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
4167
4168 /*
4169 * Do the update.
4170 */
4171 if (pIf->fActive != fActive)
4172 {
4173 PINTNETMACTABENTRY pEntry = intnetR0NetworkFindMacAddrEntry(pNetwork, pIf); Assert(pEntry);
4174 if (RT_LIKELY(pEntry))
4175 {
4176 pEntry->fActive = fActive;
4177 pIf->fActive = fActive;
4178
4179 if (fActive)
4180 {
4181 pNetwork->cActiveIFs++;
4182 if (pNetwork->cActiveIFs == 1)
4183 {
4184 pTrunk = pNetwork->MacTab.pTrunk;
4185 if (pTrunk)
4186 {
4187 pNetwork->MacTab.fHostActive = RT_BOOL(pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED);
4188 pNetwork->MacTab.fWireActive = RT_BOOL(pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED);
4189 }
4190 }
4191 }
4192 else
4193 {
4194 pNetwork->cActiveIFs--;
4195 if (pNetwork->cActiveIFs == 0)
4196 {
4197 pTrunk = pNetwork->MacTab.pTrunk;
4198 pNetwork->MacTab.fHostActive = false;
4199 pNetwork->MacTab.fWireActive = false;
4200 }
4201 }
4202 }
4203 }
4204
4205 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4206
4207 /*
4208 * Tell the trunk if necessary.
4209 * The wait for !busy is for the Solaris streams trunk driver (mostly).
4210 */
4211 if (pTrunk && pTrunk->pIfPort)
4212 {
4213 if (!fActive)
4214 intnetR0BusyWait(pNetwork, &pTrunk->cBusy);
4215
4216 pTrunk->pIfPort->pfnSetState(pTrunk->pIfPort, fActive ? INTNETTRUNKIFSTATE_ACTIVE : INTNETTRUNKIFSTATE_INACTIVE);
4217 }
4218
4219 return VINF_SUCCESS;
4220}
4221
4222
4223/**
4224 * Sets the active property of an interface.
4225 *
4226 * @returns VBox status code.
4227 * @param hIf The interface handle.
4228 * @param pSession The caller's session.
4229 * @param fActive The new state.
4230 */
4231INTNETR0DECL(int) IntNetR0IfSetActive(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fActive)
4232{
4233 LogFlow(("IntNetR0IfSetActive: hIf=%RX32 fActive=%RTbool\n", hIf, fActive));
4234
4235 /*
4236 * Validate & translate input.
4237 */
4238 PINTNET pIntNet = g_pIntNet;
4239 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
4240 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
4241
4242 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
4243 if (!pIf)
4244 {
4245 Log(("IntNetR0IfSetActive: returns VERR_INVALID_HANDLE\n"));
4246 return VERR_INVALID_HANDLE;
4247 }
4248
4249 /*
4250 * Hand it to the network since it might involve the trunk and things are
4251 * tricky there wrt to locking order.
4252 *
4253 * 1. We take the giant lock here. This makes sure nobody is re-enabling
4254 * the network while we're pausing it and vice versa. This also enables
4255 * us to wait for the network to become idle before telling the trunk.
4256 * (Important on Solaris.)
4257 *
4258 * 2. For paranoid reasons, we grab a busy reference to the calling
4259 * interface. This is totally unnecessary but should hurt (when done
4260 * after grabbing the giant lock).
4261 */
4262 int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
4263 if (RT_SUCCESS(rc))
4264 {
4265 intnetR0BusyIncIf(pIf);
4266
4267 PINTNETNETWORK pNetwork = pIf->pNetwork;
4268 if (pNetwork)
4269 rc = intnetR0NetworkSetIfActive(pNetwork, pIf, fActive);
4270 else
4271 rc = VERR_WRONG_ORDER;
4272
4273 intnetR0BusyDecIf(pIf);
4274 RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
4275 }
4276
4277 intnetR0IfRelease(pIf, pSession);
4278 LogFlow(("IntNetR0IfSetActive: returns %Rrc\n", rc));
4279 return rc;
4280}
4281
4282
4283/**
4284 * VMMR0 request wrapper for IntNetR0IfSetActive.
4285 *
4286 * @returns see IntNetR0IfSetActive.
4287 * @param pIntNet The internal networking instance.
4288 * @param pSession The caller's session.
4289 * @param pReq The request packet.
4290 */
4291INTNETR0DECL(int) IntNetR0IfSetActiveReq(PSUPDRVSESSION pSession, PINTNETIFSETACTIVEREQ pReq)
4292{
4293 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
4294 return VERR_INVALID_PARAMETER;
4295 return IntNetR0IfSetActive(pReq->hIf, pSession, pReq->fActive);
4296}
4297
4298
4299/**
4300 * Wait for the interface to get signaled.
4301 * The interface will be signaled when is put into the receive buffer.
4302 *
4303 * @returns VBox status code.
4304 * @param hIf The interface handle.
4305 * @param pSession The caller's session.
4306 * @param cMillies Number of milliseconds to wait. RT_INDEFINITE_WAIT should be
4307 * used if indefinite wait is desired.
4308 */
4309INTNETR0DECL(int) IntNetR0IfWait(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, uint32_t cMillies)
4310{
4311 Log4(("IntNetR0IfWait: hIf=%RX32 cMillies=%u\n", hIf, cMillies));
4312
4313 /*
4314 * Get and validate essential handles.
4315 */
4316 PINTNET pIntNet = g_pIntNet;
4317 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
4318 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
4319
4320 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
4321 if (!pIf)
4322 {
4323 Log(("IntNetR0IfWait: returns VERR_INVALID_HANDLE\n"));
4324 return VERR_INVALID_HANDLE;
4325 }
4326
4327 const INTNETIFHANDLE hIfSelf = pIf->hIf;
4328 const RTSEMEVENT hRecvEvent = pIf->hRecvEvent;
4329 const bool fDestroying = ASMAtomicReadBool(&pIf->fDestroying);
4330 if ( hIfSelf != hIf /* paranoia */
4331 || hRecvEvent == NIL_RTSEMEVENT
4332 || fDestroying
4333 )
4334 {
4335 Log(("IntNetR0IfWait: returns VERR_SEM_DESTROYED\n"));
4336 return VERR_SEM_DESTROYED;
4337 }
4338
4339 /*
4340 * It is tempting to check if there is data to be read here,
4341 * but the problem with such an approach is that it will cause
4342 * one unnecessary supervisor->user->supervisor trip. There is
4343 * already a slight risk for such, so no need to increase it.
4344 */
4345
4346 /*
4347 * Increment the number of waiters before starting the wait.
4348 * Upon wakeup we must assert reality, checking that we're not
4349 * already destroyed or in the process of being destroyed. This
4350 * code must be aligned with the waiting code in intnetR0IfDestruct.
4351 */
4352 ASMAtomicIncU32(&pIf->cSleepers);
4353 int rc = RTSemEventWaitNoResume(hRecvEvent, cMillies);
4354 if (pIf->hRecvEvent == hRecvEvent)
4355 {
4356 ASMAtomicDecU32(&pIf->cSleepers);
4357 if (!pIf->fDestroying)
4358 {
4359 if (intnetR0IfRelease(pIf, pSession))
4360 rc = VERR_SEM_DESTROYED;
4361 }
4362 else
4363 rc = VERR_SEM_DESTROYED;
4364 }
4365 else
4366 rc = VERR_SEM_DESTROYED;
4367 Log4(("IntNetR0IfWait: returns %Rrc\n", rc));
4368 return rc;
4369}
4370
4371
4372/**
4373 * VMMR0 request wrapper for IntNetR0IfWait.
4374 *
4375 * @returns see IntNetR0IfWait.
4376 * @param pSession The caller's session.
4377 * @param pReq The request packet.
4378 */
4379INTNETR0DECL(int) IntNetR0IfWaitReq(PSUPDRVSESSION pSession, PINTNETIFWAITREQ pReq)
4380{
4381 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
4382 return VERR_INVALID_PARAMETER;
4383 return IntNetR0IfWait(pReq->hIf, pSession, pReq->cMillies);
4384}
4385
4386
4387/**
4388 * Wake up any threads waiting on the interface.
4389 *
4390 * @returns VBox status code.
4391 * @param hIf The interface handle.
4392 * @param pSession The caller's session.
4393 * @param fNoMoreWaits When set, no more waits are permitted.
4394 */
4395INTNETR0DECL(int) IntNetR0IfAbortWait(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fNoMoreWaits)
4396{
4397 Log4(("IntNetR0IfAbortWait: hIf=%RX32 fNoMoreWaits=%RTbool\n", hIf, fNoMoreWaits));
4398
4399 /*
4400 * Get and validate essential handles.
4401 */
4402 PINTNET pIntNet = g_pIntNet;
4403 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
4404 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
4405
4406 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
4407 if (!pIf)
4408 {
4409 Log(("IntNetR0IfAbortWait: returns VERR_INVALID_HANDLE\n"));
4410 return VERR_INVALID_HANDLE;
4411 }
4412
4413 const INTNETIFHANDLE hIfSelf = pIf->hIf;
4414 const RTSEMEVENT hRecvEvent = pIf->hRecvEvent;
4415 const bool fDestroying = ASMAtomicReadBool(&pIf->fDestroying);
4416 if ( hIfSelf != hIf /* paranoia */
4417 || hRecvEvent == NIL_RTSEMEVENT
4418 || fDestroying
4419 )
4420 {
4421 Log(("IntNetR0IfAbortWait: returns VERR_SEM_DESTROYED\n"));
4422 return VERR_SEM_DESTROYED;
4423 }
4424
4425 /*
4426 * Set fDestroying if requested to do so and then wake up all the sleeping
4427 * threads (usually just one). We leave the semaphore in the signalled
4428 * state so the next caller will return immediately.
4429 */
4430 if (fNoMoreWaits)
4431 ASMAtomicWriteBool(&pIf->fDestroying, true);
4432
4433 uint32_t cSleepers = ASMAtomicReadU32(&pIf->cSleepers) + 1;
4434 while (cSleepers-- > 0)
4435 {
4436 int rc = RTSemEventSignal(pIf->hRecvEvent);
4437 AssertRC(rc);
4438 }
4439
4440 Log4(("IntNetR0IfWait: returns %Rrc\n", VINF_SUCCESS));
4441 return VINF_SUCCESS;
4442}
4443
4444
4445/**
4446 * VMMR0 request wrapper for IntNetR0IfAbortWait.
4447 *
4448 * @returns see IntNetR0IfWait.
4449 * @param pSession The caller's session.
4450 * @param pReq The request packet.
4451 */
4452INTNETR0DECL(int) IntNetR0IfAbortWaitReq(PSUPDRVSESSION pSession, PINTNETIFABORTWAITREQ pReq)
4453{
4454 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
4455 return VERR_INVALID_PARAMETER;
4456 return IntNetR0IfAbortWait(pReq->hIf, pSession, pReq->fNoMoreWaits);
4457}
4458
4459
4460/**
4461 * Close an interface.
4462 *
4463 * @returns VBox status code.
4464 * @param pIntNet The instance handle.
4465 * @param hIf The interface handle.
4466 * @param pSession The caller's session.
4467 */
4468INTNETR0DECL(int) IntNetR0IfClose(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession)
4469{
4470 LogFlow(("IntNetR0IfClose: hIf=%RX32\n", hIf));
4471
4472 /*
4473 * Validate and free the handle.
4474 */
4475 PINTNET pIntNet = g_pIntNet;
4476 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
4477 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
4478
4479 PINTNETIF pIf = (PINTNETIF)RTHandleTableFreeWithCtx(pIntNet->hHtIfs, hIf, pSession);
4480 if (!pIf)
4481 return VERR_INVALID_HANDLE;
4482
4483 /* Mark the handle as freed so intnetR0IfDestruct won't free it again. */
4484 ASMAtomicWriteU32(&pIf->hIf, INTNET_HANDLE_INVALID);
4485
4486 /*
4487 * Signal the event semaphore to wake up any threads in IntNetR0IfWait
4488 * and give them a moment to get out and release the interface.
4489 */
4490 uint32_t i = pIf->cSleepers;
4491 while (i-- > 0)
4492 {
4493 RTSemEventSignal(pIf->hRecvEvent);
4494 RTThreadYield();
4495 }
4496 RTSemEventSignal(pIf->hRecvEvent);
4497
4498 /*
4499 * Release the references to the interface object (handle + free lookup).
4500 */
4501 void *pvObj = pIf->pvObj;
4502 intnetR0IfRelease(pIf, pSession); /* (RTHandleTableFreeWithCtx) */
4503
4504 int rc = SUPR0ObjRelease(pvObj, pSession);
4505 LogFlow(("IntNetR0IfClose: returns %Rrc\n", rc));
4506 return rc;
4507}
4508
4509
4510/**
4511 * VMMR0 request wrapper for IntNetR0IfCloseReq.
4512 *
4513 * @returns see IntNetR0IfClose.
4514 * @param pSession The caller's session.
4515 * @param pReq The request packet.
4516 */
4517INTNETR0DECL(int) IntNetR0IfCloseReq(PSUPDRVSESSION pSession, PINTNETIFCLOSEREQ pReq)
4518{
4519 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
4520 return VERR_INVALID_PARAMETER;
4521 return IntNetR0IfClose(pReq->hIf, pSession);
4522}
4523
4524
4525/**
4526 * Interface destructor callback.
4527 * This is called for reference counted objectes when the count reaches 0.
4528 *
4529 * @param pvObj The object pointer.
4530 * @param pvUser1 Pointer to the interface.
4531 * @param pvUser2 Pointer to the INTNET instance data.
4532 */
4533static DECLCALLBACK(void) intnetR0IfDestruct(void *pvObj, void *pvUser1, void *pvUser2)
4534{
4535 PINTNETIF pIf = (PINTNETIF)pvUser1;
4536 PINTNET pIntNet = (PINTNET)pvUser2;
4537 Log(("intnetR0IfDestruct: pvObj=%p pIf=%p pIntNet=%p hIf=%RX32\n", pvObj, pIf, pIntNet, pIf->hIf));
4538
4539 /*
4540 * We grab the INTNET create/open/destroy semaphore to make sure nobody is
4541 * adding or removing interface while we're in here. For paranoid reasons
4542 * we also mark the interface as destroyed here so any waiting threads can
4543 * take evasive action (theoretical case).
4544 */
4545 RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
4546 ASMAtomicWriteBool(&pIf->fDestroying, true);
4547
4548 /*
4549 * Delete the interface handle so the object no longer can be used.
4550 * (Can happen if the client didn't close its session.)
4551 */
4552 INTNETIFHANDLE hIf = ASMAtomicXchgU32(&pIf->hIf, INTNET_HANDLE_INVALID);
4553 if (hIf != INTNET_HANDLE_INVALID)
4554 {
4555 void *pvObj2 = RTHandleTableFreeWithCtx(pIntNet->hHtIfs, hIf, pIf->pSession); NOREF(pvObj2);
4556 AssertMsg(pvObj2 == pIf, ("%p, %p, hIf=%RX32 pSession=%p\n", pvObj2, pIf, hIf, pIf->pSession));
4557 }
4558
4559 /*
4560 * If we've got a network deactivate and detach ourselves from it. Because
4561 * of cleanup order we might have been orphaned by the network destructor.
4562 */
4563 PINTNETNETWORK pNetwork = pIf->pNetwork;
4564 if (pNetwork)
4565 {
4566 /* set inactive. */
4567 intnetR0NetworkSetIfActive(pNetwork, pIf, false /*fActive*/);
4568
4569 /* remove ourselves from the switch table. */
4570 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
4571
4572 uint32_t iIf = pNetwork->MacTab.cEntries;
4573 while (iIf-- > 0)
4574 if (pNetwork->MacTab.paEntries[iIf].pIf == pIf)
4575 {
4576 if (pNetwork->MacTab.paEntries[iIf].fPromiscuousEff)
4577 {
4578 pNetwork->MacTab.cPromiscuousEntries--;
4579 if (!pNetwork->MacTab.paEntries[iIf].fPromiscuousSeeTrunk)
4580 pNetwork->MacTab.cPromiscuousNoTrunkEntries--;
4581 }
4582 Assert(pNetwork->MacTab.cPromiscuousEntries < pNetwork->MacTab.cEntries);
4583 Assert(pNetwork->MacTab.cPromiscuousNoTrunkEntries < pNetwork->MacTab.cEntries);
4584
4585 if (iIf + 1 < pNetwork->MacTab.cEntries)
4586 memmove(&pNetwork->MacTab.paEntries[iIf],
4587 &pNetwork->MacTab.paEntries[iIf + 1],
4588 (pNetwork->MacTab.cEntries - iIf - 1) * sizeof(pNetwork->MacTab.paEntries[0]));
4589 pNetwork->MacTab.cEntries--;
4590 break;
4591 }
4592
4593 /* recalc the min flags. */
4594 if (pIf->fOpenFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES)
4595 {
4596 uint32_t fMinFlags = 0;
4597 iIf = pNetwork->MacTab.cEntries;
4598 while (iIf-- > 0)
4599 {
4600 PINTNETIF pIf2 = pNetwork->MacTab.paEntries[iIf].pIf;
4601 if ( pIf2 /* paranoia */
4602 && (pIf2->fOpenFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES))
4603 fMinFlags |= pIf2->fOpenFlags & INTNET_OPEN_FLAGS_STRICT_MASK;
4604 }
4605 pNetwork->fMinFlags = fMinFlags;
4606 }
4607
4608 PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
4609
4610 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4611
4612 /* Notify the trunk about the interface being destroyed. */
4613 if (pTrunk && pTrunk->pIfPort)
4614 pTrunk->pIfPort->pfnDisconnectInterface(pTrunk->pIfPort, pIf->pvIfData);
4615
4616 /* Wait for the interface to quiesce while we still can. */
4617 intnetR0BusyWait(pNetwork, &pIf->cBusy);
4618
4619 /* Release our reference to the network. */
4620 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
4621 pIf->pNetwork = NULL;
4622 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4623
4624 SUPR0ObjRelease(pNetwork->pvObj, pIf->pSession);
4625 }
4626
4627 RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
4628
4629 /*
4630 * Wakeup anyone waiting on this interface.
4631 *
4632 * We *must* make sure they have woken up properly and realized
4633 * that the interface is no longer valid.
4634 */
4635 if (pIf->hRecvEvent != NIL_RTSEMEVENT)
4636 {
4637 RTSEMEVENT hRecvEvent = pIf->hRecvEvent;
4638 unsigned cMaxWait = 0x1000;
4639 while (pIf->cSleepers && cMaxWait-- > 0)
4640 {
4641 RTSemEventSignal(hRecvEvent);
4642 RTThreadYield();
4643 }
4644 if (pIf->cSleepers)
4645 {
4646 RTThreadSleep(1);
4647
4648 cMaxWait = pIf->cSleepers;
4649 while (pIf->cSleepers && cMaxWait-- > 0)
4650 {
4651 RTSemEventSignal(hRecvEvent);
4652 RTThreadSleep(10);
4653 }
4654 }
4655
4656 RTSemEventDestroy(hRecvEvent);
4657 pIf->hRecvEvent = NIL_RTSEMEVENT;
4658 }
4659
4660 /*
4661 * Unmap user buffer.
4662 */
4663 if (pIf->pIntBuf != pIf->pIntBufDefault)
4664 {
4665 /** @todo user buffer */
4666 }
4667
4668 /*
4669 * Unmap and Free the default buffer.
4670 */
4671 if (pIf->pIntBufDefault)
4672 {
4673 SUPR0MemFree(pIf->pSession, (RTHCUINTPTR)pIf->pIntBufDefault);
4674 pIf->pIntBufDefault = NULL;
4675 pIf->pIntBufDefaultR3 = 0;
4676 pIf->pIntBuf = NULL;
4677 pIf->pIntBufR3 = 0;
4678 }
4679
4680 /*
4681 * Free remaining resources
4682 */
4683 RTSpinlockDestroy(pIf->hRecvInSpinlock);
4684 pIf->hRecvInSpinlock = NIL_RTSPINLOCK;
4685
4686 RTMemFree(pIf->pDstTab);
4687 pIf->pDstTab = NULL;
4688
4689 for (int i = kIntNetAddrType_Invalid + 1; i < kIntNetAddrType_End; i++)
4690 intnetR0IfAddrCacheDestroy(&pIf->aAddrCache[i]);
4691
4692 pIf->pvObj = NULL;
4693 RTMemFree(pIf);
4694}
4695
4696
4697/**
4698 * Creates a new network interface.
4699 *
4700 * The call must have opened the network for the new interface and is
4701 * responsible for closing it on failure. On success it must leave the network
4702 * opened so the interface destructor can close it.
4703 *
4704 * @returns VBox status code.
4705 * @param pNetwork The network, referenced. The reference is consumed on
4706 * success.
4707 * @param pSession The session handle.
4708 * @param cbSend The size of the send buffer.
4709 * @param cbRecv The size of the receive buffer.
4710 * @param fFlags The open network flags.
4711 * @param phIf Where to store the interface handle.
4712 */
4713static int intnetR0NetworkCreateIf(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession,
4714 unsigned cbSend, unsigned cbRecv, uint32_t fFlags,
4715 PINTNETIFHANDLE phIf)
4716{
4717 LogFlow(("intnetR0NetworkCreateIf: pNetwork=%p pSession=%p cbSend=%u cbRecv=%u fFlags=%#x phIf=%p\n",
4718 pNetwork, pSession, cbSend, cbRecv, fFlags, phIf));
4719
4720 /*
4721 * Assert input.
4722 */
4723 AssertPtr(pNetwork);
4724 AssertPtr(phIf);
4725
4726 /*
4727 * Adjust the flags with defaults for the interface policies.
4728 * Note: Main restricts promiscuous mode per interface.
4729 */
4730 uint32_t const fDefFlags = INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW
4731 | INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK;
4732 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkIfFlags); i++)
4733 if (!(fFlags & g_afIntNetOpenNetworkIfFlags[i].fPair))
4734 fFlags |= g_afIntNetOpenNetworkIfFlags[i].fPair & fDefFlags;
4735
4736 /*
4737 * Make sure that all destination tables as well as the have space of
4738 */
4739 int rc = intnetR0NetworkEnsureTabSpace(pNetwork);
4740 if (RT_FAILURE(rc))
4741 return rc;
4742
4743 /*
4744 * Allocate the interface and initialize it.
4745 */
4746 PINTNETIF pIf = (PINTNETIF)RTMemAllocZ(sizeof(*pIf));
4747 if (!pIf)
4748 return VERR_NO_MEMORY;
4749
4750 memset(&pIf->MacAddr, 0xff, sizeof(pIf->MacAddr)); /* broadcast */
4751 //pIf->fMacSet = false;
4752 //pIf->fPromiscuousReal = false;
4753 //pIf->fActive = false;
4754 //pIf->fDestroying = false;
4755 pIf->fOpenFlags = fFlags;
4756 //pIf->cYields = 0;
4757 //pIf->pIntBuf = 0;
4758 //pIf->pIntBufR3 = NIL_RTR3PTR;
4759 //pIf->pIntBufDefault = 0;
4760 //pIf->pIntBufDefaultR3 = NIL_RTR3PTR;
4761 pIf->hRecvEvent = NIL_RTSEMEVENT;
4762 //pIf->cSleepers = 0;
4763 pIf->hIf = INTNET_HANDLE_INVALID;
4764 pIf->pNetwork = pNetwork;
4765 pIf->pSession = pSession;
4766 //pIf->pvObj = NULL;
4767 //pIf->aAddrCache = {0};
4768 pIf->hRecvInSpinlock = NIL_RTSPINLOCK;
4769 pIf->cBusy = 0;
4770 //pIf->pDstTab = NULL;
4771 //pIf->pvIfData = NULL;
4772
4773 for (int i = kIntNetAddrType_Invalid + 1; i < kIntNetAddrType_End && RT_SUCCESS(rc); i++)
4774 rc = intnetR0IfAddrCacheInit(&pIf->aAddrCache[i], (INTNETADDRTYPE)i,
4775 !!(pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE));
4776 if (RT_SUCCESS(rc))
4777 rc = intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, (PINTNETDSTTAB *)&pIf->pDstTab);
4778 if (RT_SUCCESS(rc))
4779 rc = RTSemEventCreate((PRTSEMEVENT)&pIf->hRecvEvent);
4780 if (RT_SUCCESS(rc))
4781 rc = RTSpinlockCreate(&pIf->hRecvInSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "hRecvInSpinlock");
4782 if (RT_SUCCESS(rc))
4783 {
4784 /*
4785 * Create the default buffer.
4786 */
4787 /** @todo adjust with minimums and apply defaults here. */
4788 cbRecv = RT_ALIGN(RT_MAX(cbRecv, sizeof(INTNETHDR) * 4), INTNETRINGBUF_ALIGNMENT);
4789 cbSend = RT_ALIGN(RT_MAX(cbSend, sizeof(INTNETHDR) * 4), INTNETRINGBUF_ALIGNMENT);
4790 const unsigned cbBuf = RT_ALIGN(sizeof(*pIf->pIntBuf), INTNETRINGBUF_ALIGNMENT) + cbRecv + cbSend;
4791 rc = SUPR0MemAlloc(pIf->pSession, cbBuf, (PRTR0PTR)&pIf->pIntBufDefault, (PRTR3PTR)&pIf->pIntBufDefaultR3);
4792 if (RT_SUCCESS(rc))
4793 {
4794 ASMMemZero32(pIf->pIntBufDefault, cbBuf); /** @todo I thought I specified these buggers as clearing the memory... */
4795
4796 pIf->pIntBuf = pIf->pIntBufDefault;
4797 pIf->pIntBufR3 = pIf->pIntBufDefaultR3;
4798 IntNetBufInit(pIf->pIntBuf, cbBuf, cbRecv, cbSend);
4799
4800 /*
4801 * Register the interface with the session and create a handle for it.
4802 */
4803 pIf->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE,
4804 intnetR0IfDestruct, pIf, pNetwork->pIntNet);
4805 if (pIf->pvObj)
4806 {
4807 rc = RTHandleTableAllocWithCtx(pNetwork->pIntNet->hHtIfs, pIf, pSession, (uint32_t *)&pIf->hIf);
4808 if (RT_SUCCESS(rc))
4809 {
4810 /*
4811 * Finally add the interface to the network, consuming the
4812 * network reference of the caller.
4813 */
4814 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
4815
4816 uint32_t iIf = pNetwork->MacTab.cEntries;
4817 Assert(iIf + 1 <= pNetwork->MacTab.cEntriesAllocated);
4818
4819 pNetwork->MacTab.paEntries[iIf].MacAddr = pIf->MacAddr;
4820 pNetwork->MacTab.paEntries[iIf].fActive = false;
4821 pNetwork->MacTab.paEntries[iIf].fPromiscuousEff = false;
4822 pNetwork->MacTab.paEntries[iIf].fPromiscuousSeeTrunk = false;
4823 pNetwork->MacTab.paEntries[iIf].pIf = pIf;
4824
4825 pNetwork->MacTab.cEntries = iIf + 1;
4826 pIf->pNetwork = pNetwork;
4827
4828 /*
4829 * Grab a busy reference (paranoia) to the trunk before releasing
4830 * the spinlock and then notify it about the new interface.
4831 */
4832 PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
4833 if (pTrunk)
4834 intnetR0BusyIncTrunk(pTrunk);
4835
4836 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4837
4838 if (pTrunk)
4839 {
4840 Log(("intnetR0NetworkCreateIf: pfnConnectInterface hIf=%RX32\n", pIf->hIf));
4841 if (pTrunk->pIfPort)
4842 rc = pTrunk->pIfPort->pfnConnectInterface(pTrunk->pIfPort, pIf, &pIf->pvIfData);
4843 intnetR0BusyDecTrunk(pTrunk);
4844 }
4845 if (RT_SUCCESS(rc))
4846 {
4847 /*
4848 * We're good!
4849 */
4850 *phIf = pIf->hIf;
4851 Log(("intnetR0NetworkCreateIf: returns VINF_SUCCESS *phIf=%RX32 cbSend=%u cbRecv=%u cbBuf=%u\n",
4852 *phIf, pIf->pIntBufDefault->cbSend, pIf->pIntBufDefault->cbRecv, pIf->pIntBufDefault->cbBuf));
4853 return VINF_SUCCESS;
4854 }
4855 }
4856
4857 SUPR0ObjRelease(pIf->pvObj, pSession);
4858 LogFlow(("intnetR0NetworkCreateIf: returns %Rrc\n", rc));
4859 return rc;
4860 }
4861
4862 /* clean up */
4863 SUPR0MemFree(pIf->pSession, (RTHCUINTPTR)pIf->pIntBufDefault);
4864 pIf->pIntBufDefault = NULL;
4865 pIf->pIntBuf = NULL;
4866 }
4867 }
4868
4869 RTSpinlockDestroy(pIf->hRecvInSpinlock);
4870 pIf->hRecvInSpinlock = NIL_RTSPINLOCK;
4871 RTSemEventDestroy(pIf->hRecvEvent);
4872 pIf->hRecvEvent = NIL_RTSEMEVENT;
4873 RTMemFree(pIf->pDstTab);
4874 for (int i = kIntNetAddrType_Invalid + 1; i < kIntNetAddrType_End; i++)
4875 intnetR0IfAddrCacheDestroy(&pIf->aAddrCache[i]);
4876 RTMemFree(pIf);
4877 LogFlow(("intnetR0NetworkCreateIf: returns %Rrc\n", rc));
4878 return rc;
4879}
4880
4881
4882/** @copydoc INTNETTRUNKSWPORT::pfnSetSGPhys */
4883static DECLCALLBACK(bool) intnetR0TrunkIfPortSetSGPhys(PINTNETTRUNKSWPORT pSwitchPort, bool fEnable)
4884{
4885 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
4886 AssertMsgFailed(("Not implemented because it wasn't required on Darwin\n"));
4887 return ASMAtomicXchgBool(&pThis->fPhysSG, fEnable);
4888}
4889
4890
4891/** @copydoc INTNETTRUNKSWPORT::pfnReportMacAddress */
4892static DECLCALLBACK(void) intnetR0TrunkIfPortReportMacAddress(PINTNETTRUNKSWPORT pSwitchPort, PCRTMAC pMacAddr)
4893{
4894 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
4895
4896 /*
4897 * Get the network instance and grab the address spinlock before making
4898 * any changes.
4899 */
4900 intnetR0BusyIncTrunk(pThis);
4901 PINTNETNETWORK pNetwork = pThis->pNetwork;
4902 if (pNetwork)
4903 {
4904 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
4905
4906 pNetwork->MacTab.HostMac = *pMacAddr;
4907 pThis->MacAddr = *pMacAddr;
4908
4909 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4910 }
4911 else
4912 pThis->MacAddr = *pMacAddr;
4913 intnetR0BusyDecTrunk(pThis);
4914}
4915
4916
4917/** @copydoc INTNETTRUNKSWPORT::pfnReportPromiscuousMode */
4918static DECLCALLBACK(void) intnetR0TrunkIfPortReportPromiscuousMode(PINTNETTRUNKSWPORT pSwitchPort, bool fPromiscuous)
4919{
4920 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
4921
4922 /*
4923 * Get the network instance and grab the address spinlock before making
4924 * any changes.
4925 */
4926 intnetR0BusyIncTrunk(pThis);
4927 PINTNETNETWORK pNetwork = pThis->pNetwork;
4928 if (pNetwork)
4929 {
4930 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
4931
4932 pNetwork->MacTab.fHostPromiscuousReal = fPromiscuous
4933 || (pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE);
4934 pNetwork->MacTab.fHostPromiscuousEff = pNetwork->MacTab.fHostPromiscuousReal
4935 && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST);
4936
4937 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
4938 }
4939 intnetR0BusyDecTrunk(pThis);
4940}
4941
4942
4943/** @copydoc INTNETTRUNKSWPORT::pfnReportGsoCapabilities */
4944static DECLCALLBACK(void) intnetR0TrunkIfPortReportGsoCapabilities(PINTNETTRUNKSWPORT pSwitchPort,
4945 uint32_t fGsoCapabilities, uint32_t fDst)
4946{
4947 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
4948
4949 for (unsigned iBit = PDMNETWORKGSOTYPE_END; iBit < 32; iBit++)
4950 Assert(!(fGsoCapabilities & RT_BIT_32(iBit)));
4951 Assert(!(fDst & ~INTNETTRUNKDIR_VALID_MASK));
4952 Assert(fDst);
4953
4954 if (fDst & INTNETTRUNKDIR_HOST)
4955 pThis->fHostGsoCapabilites = fGsoCapabilities;
4956
4957 if (fDst & INTNETTRUNKDIR_WIRE)
4958 pThis->fWireGsoCapabilites = fGsoCapabilities;
4959}
4960
4961
4962/** @copydoc INTNETTRUNKSWPORT::pfnReportNoPreemptDsts */
4963static DECLCALLBACK(void) intnetR0TrunkIfPortReportNoPreemptDsts(PINTNETTRUNKSWPORT pSwitchPort, uint32_t fNoPreemptDsts)
4964{
4965 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
4966 Assert(!(fNoPreemptDsts & ~INTNETTRUNKDIR_VALID_MASK));
4967
4968 pThis->fNoPreemptDsts = fNoPreemptDsts;
4969}
4970
4971
4972/** @copydoc INTNETTRUNKSWPORT::pfnPreRecv */
4973static DECLCALLBACK(INTNETSWDECISION) intnetR0TrunkIfPortPreRecv(PINTNETTRUNKSWPORT pSwitchPort,
4974 void const *pvSrc, size_t cbSrc, uint32_t fSrc)
4975{
4976 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
4977
4978 /* assert some sanity */
4979 AssertPtr(pvSrc);
4980 AssertReturn(cbSrc >= 6, INTNETSWDECISION_BROADCAST);
4981 Assert(fSrc);
4982
4983 /*
4984 * Mark the trunk as busy, make sure we've got a network and that there are
4985 * some active interfaces around.
4986 */
4987 INTNETSWDECISION enmSwDecision = INTNETSWDECISION_TRUNK;
4988 intnetR0BusyIncTrunk(pThis);
4989 PINTNETNETWORK pNetwork = pThis->pNetwork;
4990 if (RT_LIKELY( pNetwork
4991 && pNetwork->cActiveIFs > 0 ))
4992 {
4993 /*
4994 * Lazy bird! No pre-switching of multicast and shared-MAC-on-wire.
4995 */
4996 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvSrc;
4997 if (intnetR0IsMacAddrMulticast(&pEthHdr->DstMac))
4998 enmSwDecision = INTNETSWDECISION_BROADCAST;
4999 else if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
5000 enmSwDecision = INTNETSWDECISION_BROADCAST;
5001 else
5002 enmSwDecision = intnetR0NetworkPreSwitchUnicast(pNetwork,
5003 fSrc,
5004 cbSrc >= 12 ? &pEthHdr->SrcMac : NULL,
5005 &pEthHdr->DstMac);
5006 }
5007
5008 intnetR0BusyDecTrunk(pThis);
5009 return enmSwDecision;
5010}
5011
5012
5013/** @copydoc INTNETTRUNKSWPORT::pfnRecv */
5014static DECLCALLBACK(bool) intnetR0TrunkIfPortRecv(PINTNETTRUNKSWPORT pSwitchPort, void *pvIf, PINTNETSG pSG, uint32_t fSrc)
5015{
5016 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
5017
5018 /* assert some sanity */
5019 AssertPtr(pSG);
5020 Assert(fSrc);
5021 NOREF(pvIf); /* later */
5022
5023 /*
5024 * Mark the trunk as busy, make sure we've got a network and that there are
5025 * some active interfaces around.
5026 */
5027 bool fRc = false /* don't drop it */;
5028 intnetR0BusyIncTrunk(pThis);
5029 PINTNETNETWORK pNetwork = pThis->pNetwork;
5030 if (RT_LIKELY( pNetwork
5031 && pNetwork->cActiveIFs > 0 ))
5032 {
5033 /*
5034 * Grab or allocate a destination table.
5035 */
5036 bool const fIntCtx = RTThreadPreemptIsEnabled(NIL_RTTHREAD) || RTThreadIsInInterrupt(NIL_RTTHREAD);
5037 unsigned iDstTab = 0;
5038 PINTNETDSTTAB pDstTab = NULL;
5039 RTSpinlockAcquire(pThis->hDstTabSpinlock);
5040 if (fIntCtx)
5041 {
5042 /* Interrupt or restricted context. */
5043 iDstTab = RTMpCpuIdToSetIndex(RTMpCpuId());
5044 iDstTab %= pThis->cIntDstTabs;
5045 pDstTab = pThis->apIntDstTabs[iDstTab];
5046 if (RT_LIKELY(pDstTab))
5047 pThis->apIntDstTabs[iDstTab] = NULL;
5048 else
5049 {
5050 iDstTab = pThis->cIntDstTabs;
5051 while (iDstTab-- > 0)
5052 {
5053 pDstTab = pThis->apIntDstTabs[iDstTab];
5054 if (pDstTab)
5055 {
5056 pThis->apIntDstTabs[iDstTab] = NULL;
5057 break;
5058 }
5059 }
5060 }
5061 RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
5062 Assert(!pDstTab || iDstTab < pThis->cIntDstTabs);
5063 }
5064 else
5065 {
5066 /* Task context, fallback is to allocate a table. */
5067 AssertCompile(RT_ELEMENTS(pThis->apTaskDstTabs) == 2); /* for loop rollout */
5068 pDstTab = pThis->apIntDstTabs[iDstTab = 0];
5069 if (!pDstTab)
5070 pDstTab = pThis->apIntDstTabs[iDstTab = 1];
5071 if (pDstTab)
5072 {
5073 pThis->apIntDstTabs[iDstTab] = NULL;
5074 RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
5075 Assert(iDstTab < RT_ELEMENTS(pThis->apTaskDstTabs));
5076 }
5077 else
5078 {
5079 RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
5080 intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, &pDstTab);
5081 iDstTab = 65535;
5082 }
5083 }
5084 if (RT_LIKELY(pDstTab))
5085 {
5086 /*
5087 * Finally, get down to business of sending the frame.
5088 */
5089 INTNETSWDECISION enmSwDecision = intnetR0NetworkSend(pNetwork, NULL, fSrc, pSG, pDstTab);
5090 AssertMsg(enmSwDecision != INTNETSWDECISION_BAD_CONTEXT, ("fSrc=%#x fTrunkDst=%#x hdr=%.14Rhxs\n", fSrc, pDstTab->fTrunkDst, pSG->aSegs[0].pv));
5091 if (enmSwDecision == INTNETSWDECISION_INTNET)
5092 fRc = true; /* drop it */
5093
5094 /*
5095 * Free the destination table.
5096 */
5097 if (iDstTab == 65535)
5098 RTMemFree(pDstTab);
5099 else
5100 {
5101 RTSpinlockAcquire(pThis->hDstTabSpinlock);
5102 if (fIntCtx && !pThis->apIntDstTabs[iDstTab])
5103 pThis->apIntDstTabs[iDstTab] = pDstTab;
5104 else if (!fIntCtx && !pThis->apTaskDstTabs[iDstTab])
5105 pThis->apTaskDstTabs[iDstTab] = pDstTab;
5106 else
5107 {
5108 /* this shouldn't happen! */
5109 PINTNETDSTTAB *papDstTabs = fIntCtx ? &pThis->apIntDstTabs[0] : &pThis->apTaskDstTabs[0];
5110 iDstTab = fIntCtx ? pThis->cIntDstTabs : RT_ELEMENTS(pThis->apTaskDstTabs);
5111 while (iDstTab-- > 0)
5112 if (!papDstTabs[iDstTab])
5113 {
5114 papDstTabs[iDstTab] = pDstTab;
5115 break;
5116 }
5117 }
5118 RTSpinlockReleaseNoInts(pThis->hDstTabSpinlock);
5119 Assert(iDstTab < RT_MAX(RT_ELEMENTS(pThis->apTaskDstTabs), pThis->cIntDstTabs));
5120 }
5121 }
5122 }
5123
5124 intnetR0BusyDecTrunk(pThis);
5125 return fRc;
5126}
5127
5128
5129/** @copydoc INTNETTRUNKSWPORT::pfnSGRetain */
5130static DECLCALLBACK(void) intnetR0TrunkIfPortSGRetain(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
5131{
5132 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
5133 PINTNETNETWORK pNetwork = pThis->pNetwork;
5134
5135 /* assert some sanity */
5136 AssertPtrReturnVoid(pNetwork);
5137 AssertReturnVoid(pNetwork->hEvtBusyIf != NIL_RTSEMEVENT);
5138 AssertPtr(pSG);
5139 Assert(pSG->cUsers > 0 && pSG->cUsers < 256);
5140
5141 /* do it. */
5142 ++pSG->cUsers;
5143}
5144
5145
5146/** @copydoc INTNETTRUNKSWPORT::pfnSGRelease */
5147static DECLCALLBACK(void) intnetR0TrunkIfPortSGRelease(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
5148{
5149 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
5150 PINTNETNETWORK pNetwork = pThis->pNetwork;
5151
5152 /* assert some sanity */
5153 AssertPtrReturnVoid(pNetwork);
5154 AssertReturnVoid(pNetwork->hEvtBusyIf != NIL_RTSEMEVENT);
5155 AssertPtr(pSG);
5156 Assert(pSG->cUsers > 0);
5157
5158 /*
5159 * Free it?
5160 */
5161 if (!--pSG->cUsers)
5162 {
5163 /** @todo later */
5164 }
5165}
5166
5167
5168/**
5169 * Shutdown the trunk interface.
5170 *
5171 * @param pThis The trunk.
5172 * @param pNetworks The network.
5173 *
5174 * @remarks The caller must hold the global lock.
5175 */
5176static void intnetR0TrunkIfDestroy(PINTNETTRUNKIF pThis, PINTNETNETWORK pNetwork)
5177{
5178 /* assert sanity */
5179 if (!pThis)
5180 return;
5181 AssertPtr(pThis);
5182 Assert(pThis->pNetwork == pNetwork);
5183 AssertPtrNull(pThis->pIfPort);
5184
5185 /*
5186 * The interface has already been deactivated, we just to wait for
5187 * it to become idle before we can disconnect and release it.
5188 */
5189 PINTNETTRUNKIFPORT pIfPort = pThis->pIfPort;
5190 if (pIfPort)
5191 {
5192 /* unset it */
5193 pThis->pIfPort = NULL;
5194
5195 /* wait in portions so we can complain ever now an then. */
5196 uint64_t StartTS = RTTimeSystemNanoTS();
5197 int rc = pIfPort->pfnWaitForIdle(pIfPort, 10*1000);
5198 if (RT_FAILURE(rc))
5199 {
5200 LogRel(("intnet: '%s' didn't become idle in %RU64 ns (%Rrc).\n",
5201 pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
5202 Assert(rc == VERR_TIMEOUT);
5203 while ( RT_FAILURE(rc)
5204 && RTTimeSystemNanoTS() - StartTS < UINT64_C(30000000000)) /* 30 sec */
5205 rc = pIfPort->pfnWaitForIdle(pIfPort, 10*1000);
5206 if (rc == VERR_TIMEOUT)
5207 {
5208 LogRel(("intnet: '%s' didn't become idle in %RU64 ns (%Rrc).\n",
5209 pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
5210 while ( rc == VERR_TIMEOUT
5211 && RTTimeSystemNanoTS() - StartTS < UINT64_C(360000000000)) /* 360 sec */
5212 rc = pIfPort->pfnWaitForIdle(pIfPort, 30*1000);
5213 if (RT_FAILURE(rc))
5214 {
5215 LogRel(("intnet: '%s' didn't become idle in %RU64 ns (%Rrc), giving up.\n",
5216 pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
5217 AssertRC(rc);
5218 }
5219 }
5220 }
5221
5222 /* disconnect & release it. */
5223 pIfPort->pfnDisconnectAndRelease(pIfPort);
5224 }
5225
5226 /*
5227 * Free up the resources.
5228 */
5229 pThis->pNetwork = NULL;
5230 RTSpinlockDestroy(pThis->hDstTabSpinlock);
5231 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apTaskDstTabs); i++)
5232 {
5233 Assert(pThis->apTaskDstTabs[i]);
5234 RTMemFree(pThis->apTaskDstTabs[i]);
5235 pThis->apTaskDstTabs[i] = NULL;
5236 }
5237 for (unsigned i = 0; i < pThis->cIntDstTabs; i++)
5238 {
5239 Assert(pThis->apIntDstTabs[i]);
5240 RTMemFree(pThis->apIntDstTabs[i]);
5241 pThis->apIntDstTabs[i] = NULL;
5242 }
5243 RTMemFree(pThis);
5244}
5245
5246
5247/**
5248 * Creates the trunk connection (if any).
5249 *
5250 * @returns VBox status code.
5251 *
5252 * @param pNetwork The newly created network.
5253 * @param pSession The session handle.
5254 */
5255static int intnetR0NetworkCreateTrunkIf(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession)
5256{
5257 const char *pszName;
5258 switch (pNetwork->enmTrunkType)
5259 {
5260 /*
5261 * The 'None' case, simple.
5262 */
5263 case kIntNetTrunkType_None:
5264 case kIntNetTrunkType_WhateverNone:
5265#ifdef VBOX_WITH_NAT_SERVICE
5266 /*
5267 * Well, here we don't want load anything special,
5268 * just communicate between processes via internal network.
5269 */
5270 case kIntNetTrunkType_SrvNat:
5271#endif
5272 return VINF_SUCCESS;
5273
5274 /* Can't happen, but makes GCC happy. */
5275 default:
5276 return VERR_NOT_IMPLEMENTED;
5277
5278 /*
5279 * Translate enum to component factory name.
5280 */
5281 case kIntNetTrunkType_NetFlt:
5282 pszName = "VBoxNetFlt";
5283 break;
5284 case kIntNetTrunkType_NetAdp:
5285#if defined(RT_OS_DARWIN) && !defined(VBOXNETADP_DO_NOT_USE_NETFLT)
5286 pszName = "VBoxNetFlt";
5287#else /* VBOXNETADP_DO_NOT_USE_NETFLT */
5288 pszName = "VBoxNetAdp";
5289#endif /* VBOXNETADP_DO_NOT_USE_NETFLT */
5290 break;
5291#ifndef VBOX_WITH_NAT_SERVICE
5292 case kIntNetTrunkType_SrvNat:
5293 pszName = "VBoxSrvNat";
5294 break;
5295#endif
5296 }
5297
5298 /*
5299 * Allocate the trunk interface and associated destination tables.
5300 *
5301 * We take a very optimistic view on the parallelism of the host
5302 * network stack and NIC driver. So, we allocate one table for each
5303 * possible CPU to deal with interrupt time requests and one for task
5304 * time calls.
5305 */
5306 RTCPUID cCpus = RTMpGetCount(); Assert(cCpus > 0);
5307 PINTNETTRUNKIF pTrunk = (PINTNETTRUNKIF)RTMemAllocZ(RT_OFFSETOF(INTNETTRUNKIF, apIntDstTabs[cCpus]));
5308 if (!pTrunk)
5309 return VERR_NO_MEMORY;
5310
5311 Assert(pNetwork->MacTab.cEntriesAllocated > 0);
5312 int rc = VINF_SUCCESS;
5313 pTrunk->cIntDstTabs = cCpus;
5314 for (unsigned i = 0; i < cCpus && RT_SUCCESS(rc); i++)
5315 rc = intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, &pTrunk->apIntDstTabs[i]);
5316 for (unsigned i = 0; i < RT_ELEMENTS(pTrunk->apTaskDstTabs) && RT_SUCCESS(rc); i++)
5317 rc = intnetR0AllocDstTab(pNetwork->MacTab.cEntriesAllocated, &pTrunk->apTaskDstTabs[i]);
5318
5319 if (RT_SUCCESS(rc))
5320 {
5321 pTrunk->SwitchPort.u32Version = INTNETTRUNKSWPORT_VERSION;
5322 pTrunk->SwitchPort.pfnPreRecv = intnetR0TrunkIfPortPreRecv;
5323 pTrunk->SwitchPort.pfnRecv = intnetR0TrunkIfPortRecv;
5324 pTrunk->SwitchPort.pfnSGRetain = intnetR0TrunkIfPortSGRetain;
5325 pTrunk->SwitchPort.pfnSGRelease = intnetR0TrunkIfPortSGRelease;
5326 pTrunk->SwitchPort.pfnSetSGPhys = intnetR0TrunkIfPortSetSGPhys;
5327 pTrunk->SwitchPort.pfnReportMacAddress = intnetR0TrunkIfPortReportMacAddress;
5328 pTrunk->SwitchPort.pfnReportPromiscuousMode = intnetR0TrunkIfPortReportPromiscuousMode;
5329 pTrunk->SwitchPort.pfnReportGsoCapabilities = intnetR0TrunkIfPortReportGsoCapabilities;
5330 pTrunk->SwitchPort.pfnReportNoPreemptDsts = intnetR0TrunkIfPortReportNoPreemptDsts;
5331 pTrunk->SwitchPort.u32VersionEnd = INTNETTRUNKSWPORT_VERSION;
5332 //pTrunk->pIfPort = NULL;
5333 pTrunk->pNetwork = pNetwork;
5334 pTrunk->MacAddr.au8[0] = 0xff;
5335 pTrunk->MacAddr.au8[1] = 0xff;
5336 pTrunk->MacAddr.au8[2] = 0xff;
5337 pTrunk->MacAddr.au8[3] = 0xff;
5338 pTrunk->MacAddr.au8[4] = 0xff;
5339 pTrunk->MacAddr.au8[5] = 0xff;
5340 //pTrunk->fPhysSG = false;
5341 //pTrunk->fUnused = false;
5342 //pTrunk->cBusy = 0;
5343 //pTrunk->fNoPreemptDsts = 0;
5344 //pTrunk->fWireGsoCapabilites = 0;
5345 //pTrunk->fHostGsoCapabilites = 0;
5346 //pTrunk->abGsoHdrs = {0};
5347 pTrunk->hDstTabSpinlock = NIL_RTSPINLOCK;
5348 //pTrunk->apTaskDstTabs = above;
5349 //pTrunk->cIntDstTabs = above;
5350 //pTrunk->apIntDstTabs = above;
5351
5352 /*
5353 * Create the lock (we've NIL'ed the members above to simplify cleanup).
5354 */
5355 rc = RTSpinlockCreate(&pTrunk->hDstTabSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "hDstTabSpinlock");
5356 if (RT_SUCCESS(rc))
5357 {
5358 /*
5359 * There are a couple of bits in MacTab as well pertaining to the
5360 * trunk. We have to set this before it's reported.
5361 *
5362 * Note! We don't need to lock the MacTab here - creation time.
5363 */
5364 pNetwork->MacTab.pTrunk = pTrunk;
5365 pNetwork->MacTab.HostMac = pTrunk->MacAddr;
5366 pNetwork->MacTab.fHostPromiscuousReal = false;
5367 pNetwork->MacTab.fHostPromiscuousEff = (pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE)
5368 && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST);
5369 pNetwork->MacTab.fHostActive = false;
5370 pNetwork->MacTab.fWirePromiscuousReal = RT_BOOL(pNetwork->fFlags & INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE);
5371 pNetwork->MacTab.fWirePromiscuousEff = pNetwork->MacTab.fWirePromiscuousReal
5372 && (pNetwork->fFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE);
5373 pNetwork->MacTab.fWireActive = false;
5374
5375#ifdef IN_RING0 /* (testcase is ring-3) */
5376 /*
5377 * Query the factory we want, then use it create and connect the trunk.
5378 */
5379 PINTNETTRUNKFACTORY pTrunkFactory = NULL;
5380 rc = SUPR0ComponentQueryFactory(pSession, pszName, INTNETTRUNKFACTORY_UUID_STR, (void **)&pTrunkFactory);
5381 if (RT_SUCCESS(rc))
5382 {
5383 rc = pTrunkFactory->pfnCreateAndConnect(pTrunkFactory,
5384 pNetwork->szTrunk,
5385 &pTrunk->SwitchPort,
5386 pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE
5387 ? INTNETTRUNKFACTORY_FLAG_NO_PROMISC
5388 : 0,
5389 &pTrunk->pIfPort);
5390 pTrunkFactory->pfnRelease(pTrunkFactory);
5391 if (RT_SUCCESS(rc))
5392 {
5393 Assert(pTrunk->pIfPort);
5394
5395 Log(("intnetR0NetworkCreateTrunkIf: VINF_SUCCESS - pszName=%s szTrunk=%s%s Network=%s\n",
5396 pszName, pNetwork->szTrunk, pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE ? " shared-mac" : "", pNetwork->szName));
5397 return VINF_SUCCESS;
5398 }
5399 }
5400#else /* IN_RING3 */
5401 NOREF(pSession);
5402 rc = VERR_NOT_SUPPORTED;
5403#endif /* IN_RING3 */
5404
5405 pNetwork->MacTab.pTrunk = NULL;
5406 }
5407
5408 /* bail out and clean up. */
5409 RTSpinlockDestroy(pTrunk->hDstTabSpinlock);
5410 }
5411
5412 for (unsigned i = 0; i < RT_ELEMENTS(pTrunk->apTaskDstTabs); i++)
5413 RTMemFree(pTrunk->apTaskDstTabs[i]);
5414 for (unsigned i = 0; i < pTrunk->cIntDstTabs; i++)
5415 RTMemFree(pTrunk->apIntDstTabs[i]);
5416 RTMemFree(pTrunk);
5417
5418 LogFlow(("intnetR0NetworkCreateTrunkIf: %Rrc - pszName=%s szTrunk=%s Network=%s\n",
5419 rc, pszName, pNetwork->szTrunk, pNetwork->szName));
5420 return rc;
5421}
5422
5423
5424
5425/**
5426 * Object destructor callback.
5427 * This is called for reference counted objectes when the count reaches 0.
5428 *
5429 * @param pvObj The object pointer.
5430 * @param pvUser1 Pointer to the network.
5431 * @param pvUser2 Pointer to the INTNET instance data.
5432 */
5433static DECLCALLBACK(void) intnetR0NetworkDestruct(void *pvObj, void *pvUser1, void *pvUser2)
5434{
5435 PINTNETNETWORK pNetwork = (PINTNETNETWORK)pvUser1;
5436 PINTNET pIntNet = (PINTNET)pvUser2;
5437 Log(("intnetR0NetworkDestruct: pvObj=%p pNetwork=%p pIntNet=%p %s\n", pvObj, pNetwork, pIntNet, pNetwork->szName));
5438 Assert(pNetwork->pIntNet == pIntNet);
5439
5440 /* Take the big create/open/destroy sem. */
5441 RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
5442
5443 /*
5444 * Tell the trunk, if present, that we're about to disconnect it and wish
5445 * no further calls from it.
5446 */
5447 PINTNETTRUNKIF pTrunk = pNetwork->MacTab.pTrunk;
5448 if (pTrunk)
5449 pTrunk->pIfPort->pfnSetState(pTrunk->pIfPort, INTNETTRUNKIFSTATE_DISCONNECTING);
5450
5451 /*
5452 * Deactivate and orphan any remaining interfaces and wait for them to idle.
5453 *
5454 * Note! Normally there are no more interfaces at this point, however, when
5455 * supdrvCloseSession / supdrvCleanupSession release the objects the
5456 * order is undefined. So, it's quite possible that the network will
5457 * be dereference and destroyed before the interfaces.
5458 */
5459 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
5460
5461 uint32_t iIf = pNetwork->MacTab.cEntries;
5462 while (iIf-- > 0)
5463 {
5464 pNetwork->MacTab.paEntries[iIf].fActive = false;
5465 pNetwork->MacTab.paEntries[iIf].pIf->fActive = false;
5466 }
5467
5468 pNetwork->MacTab.fHostActive = false;
5469 pNetwork->MacTab.fWireActive = false;
5470
5471 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
5472
5473 /* Wait for all the interfaces to quiesce. (Interfaces cannot be
5474 removed / added since we're holding the big lock.) */
5475 if (pTrunk)
5476 intnetR0BusyWait(pNetwork, &pTrunk->cBusy);
5477
5478 iIf = pNetwork->MacTab.cEntries;
5479 while (iIf-- > 0)
5480 intnetR0BusyWait(pNetwork, &pNetwork->MacTab.paEntries[iIf].pIf->cBusy);
5481
5482 /* Orphan the interfaces (not trunk). Don't bother with calling
5483 pfnDisconnectInterface here since the networking is going away. */
5484 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
5485 while ((iIf = pNetwork->MacTab.cEntries) > 0)
5486 {
5487 PINTNETIF pIf = pNetwork->MacTab.paEntries[iIf - 1].pIf;
5488 RTSpinlockRelease(pNetwork->hAddrSpinlock);
5489
5490 intnetR0BusyWait(pNetwork, &pIf->cBusy);
5491
5492 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
5493 if ( iIf == pNetwork->MacTab.cEntries /* paranoia */
5494 && pIf->cBusy)
5495 {
5496 pIf->pNetwork = NULL;
5497 pNetwork->MacTab.cEntries--;
5498 }
5499 }
5500
5501 /*
5502 * Zap the trunk pointer while we still own the spinlock, destroy the
5503 * trunk after we've left it. Note that this might take a while...
5504 */
5505 pNetwork->MacTab.pTrunk = NULL;
5506
5507 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
5508
5509 if (pTrunk)
5510 intnetR0TrunkIfDestroy(pTrunk, pNetwork);
5511
5512 /*
5513 * Unlink the network.
5514 * Note that it needn't be in the list if we failed during creation.
5515 */
5516 PINTNETNETWORK pPrev = pIntNet->pNetworks;
5517 if (pPrev == pNetwork)
5518 pIntNet->pNetworks = pNetwork->pNext;
5519 else
5520 {
5521 for (; pPrev; pPrev = pPrev->pNext)
5522 if (pPrev->pNext == pNetwork)
5523 {
5524 pPrev->pNext = pNetwork->pNext;
5525 break;
5526 }
5527 }
5528 pNetwork->pNext = NULL;
5529 pNetwork->pvObj = NULL;
5530
5531 /*
5532 * Free resources.
5533 */
5534 RTSemEventDestroy(pNetwork->hEvtBusyIf);
5535 pNetwork->hEvtBusyIf = NIL_RTSEMEVENT;
5536 RTSpinlockDestroy(pNetwork->hAddrSpinlock);
5537 pNetwork->hAddrSpinlock = NIL_RTSPINLOCK;
5538 RTMemFree(pNetwork->MacTab.paEntries);
5539 pNetwork->MacTab.paEntries = NULL;
5540 RTMemFree(pNetwork);
5541
5542 /* Release the create/destroy sem. */
5543 RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
5544}
5545
5546
5547/**
5548 * Checks if the open network flags are compatible.
5549 *
5550 * @returns VBox status code.
5551 * @param pNetwork The network.
5552 * @param fFlags The open network flags.
5553 */
5554static int intnetR0CheckOpenNetworkFlags(PINTNETNETWORK pNetwork, uint32_t fFlags)
5555{
5556 uint32_t const fNetFlags = pNetwork->fFlags;
5557
5558 if ( (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
5559 ^ (fNetFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE))
5560 return VERR_INTNET_INCOMPATIBLE_FLAGS;
5561
5562 if (fFlags & INTNET_OPEN_FLAGS_REQUIRE_EXACT)
5563 {
5564 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
5565 if ( (fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair)
5566 && (fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair)
5567 != (fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fPair) )
5568 return VERR_INTNET_INCOMPATIBLE_FLAGS;
5569 }
5570
5571 if (fFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES)
5572 {
5573 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
5574 if ( (fFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive)
5575 && !(fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive)
5576 && (fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fFixed) )
5577 return VERR_INTNET_INCOMPATIBLE_FLAGS;
5578 }
5579
5580 return VINF_SUCCESS;
5581}
5582
5583
5584/**
5585 * Adapts flag changes on network opening.
5586 *
5587 * @returns VBox status code.
5588 * @param pNetwork The network.
5589 * @param fFlags The open network flags.
5590 */
5591static int intnetR0AdaptOpenNetworkFlags(PINTNETNETWORK pNetwork, uint32_t fFlags)
5592{
5593 /*
5594 * Upgrade the minimum policy flags.
5595 */
5596 uint32_t fNetMinFlags = pNetwork->fMinFlags;
5597 Assert(!(fNetMinFlags & INTNET_OPEN_FLAGS_RELAXED_MASK));
5598 if (fFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES)
5599 {
5600 fNetMinFlags |= fFlags & INTNET_OPEN_FLAGS_STRICT_MASK;
5601 if (fNetMinFlags != pNetwork->fMinFlags)
5602 {
5603 LogRel(("INTNET: %s - min flags changed %#x -> %#x\n", pNetwork->szName, pNetwork->fMinFlags, fNetMinFlags));
5604 pNetwork->fMinFlags = fNetMinFlags;
5605 }
5606 }
5607
5608 /*
5609 * Calculate the new network flags.
5610 * (Depends on fNetMinFlags being recalculated first.)
5611 */
5612 uint32_t fNetFlags = pNetwork->fFlags;
5613
5614 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
5615 {
5616 Assert(fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fPair);
5617 Assert(!(fNetMinFlags & g_afIntNetOpenNetworkNetFlags[i].fRelaxed));
5618
5619 if (!(fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair))
5620 continue;
5621 if (fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fFixed)
5622 continue;
5623
5624 if ( (fNetMinFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive)
5625 || (fFlags & g_afIntNetOpenNetworkNetFlags[i].fRestrictive) )
5626 {
5627 fNetFlags &= ~g_afIntNetOpenNetworkNetFlags[i].fPair;
5628 fNetFlags |= g_afIntNetOpenNetworkNetFlags[i].fRestrictive;
5629 }
5630 else if (!(fFlags & INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES))
5631 {
5632 fNetFlags &= ~g_afIntNetOpenNetworkNetFlags[i].fPair;
5633 fNetFlags |= g_afIntNetOpenNetworkNetFlags[i].fRelaxed;
5634 }
5635 }
5636
5637 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
5638 {
5639 Assert(fNetFlags & g_afIntNetOpenNetworkNetFlags[i].fPair);
5640 fNetFlags |= fFlags & g_afIntNetOpenNetworkNetFlags[i].fFixed;
5641 }
5642
5643 /*
5644 * Apply the flags if they changed.
5645 */
5646 uint32_t const fOldNetFlags = pNetwork->fFlags;
5647 if (fOldNetFlags != fNetFlags)
5648 {
5649 LogRel(("INTNET: %s - flags changed %#x -> %#x\n", pNetwork->szName, fOldNetFlags, fNetFlags));
5650
5651 RTSpinlockAcquire(pNetwork->hAddrSpinlock);
5652
5653 pNetwork->fFlags = fNetFlags;
5654
5655 /* Recalculate some derived switcher variables. */
5656 bool fActiveTrunk = pNetwork->MacTab.pTrunk
5657 && pNetwork->cActiveIFs > 0;
5658 pNetwork->MacTab.fHostActive = fActiveTrunk
5659 && (fNetFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED);
5660 pNetwork->MacTab.fHostPromiscuousEff = ( pNetwork->MacTab.fHostPromiscuousReal
5661 || (fNetFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE))
5662 && (fNetFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST);
5663
5664 pNetwork->MacTab.fWireActive = fActiveTrunk
5665 && (fNetFlags & INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED);
5666 pNetwork->MacTab.fWirePromiscuousReal= RT_BOOL(fNetFlags & INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE);
5667 pNetwork->MacTab.fWirePromiscuousEff = pNetwork->MacTab.fWirePromiscuousReal
5668 && (fNetFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE);
5669
5670 if ((fOldNetFlags ^ fNetFlags) & INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS)
5671 {
5672 pNetwork->MacTab.cPromiscuousEntries = 0;
5673 pNetwork->MacTab.cPromiscuousNoTrunkEntries = 0;
5674
5675 uint32_t iIf = pNetwork->MacTab.cEntries;
5676 while (iIf-- > 0)
5677 {
5678 PINTNETMACTABENTRY pEntry = &pNetwork->MacTab.paEntries[iIf];
5679 PINTNETIF pIf2 = pEntry->pIf;
5680 if ( pIf2 /* paranoia */
5681 && pIf2->fPromiscuousReal)
5682 {
5683 bool fPromiscuousEff = (fNetFlags & INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS)
5684 && (pIf2->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW);
5685 pEntry->fPromiscuousEff = fPromiscuousEff;
5686 pEntry->fPromiscuousSeeTrunk = fPromiscuousEff
5687 && (pIf2->fOpenFlags & INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK);
5688
5689 if (pEntry->fPromiscuousEff)
5690 {
5691 pNetwork->MacTab.cPromiscuousEntries++;
5692 if (!pEntry->fPromiscuousSeeTrunk)
5693 pNetwork->MacTab.cPromiscuousNoTrunkEntries++;
5694 }
5695 }
5696 }
5697 }
5698
5699 RTSpinlockReleaseNoInts(pNetwork->hAddrSpinlock);
5700 }
5701
5702 return VINF_SUCCESS;
5703}
5704
5705
5706/**
5707 * Opens an existing network.
5708 *
5709 * The call must own the INTNET::hMtxCreateOpenDestroy.
5710 *
5711 * @returns VBox status code.
5712 * @param pIntNet The instance data.
5713 * @param pSession The current session.
5714 * @param pszNetwork The network name. This has a valid length.
5715 * @param enmTrunkType The trunk type.
5716 * @param pszTrunk The trunk name. Its meaning is specific to the type.
5717 * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
5718 * @param ppNetwork Where to store the pointer to the network on success.
5719 */
5720static int intnetR0OpenNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
5721 const char *pszTrunk, uint32_t fFlags, PINTNETNETWORK *ppNetwork)
5722{
5723 LogFlow(("intnetR0OpenNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
5724 pIntNet, pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, ppNetwork));
5725
5726 /* just pro forma validation, the caller is internal. */
5727 AssertPtr(pIntNet);
5728 AssertPtr(pSession);
5729 AssertPtr(pszNetwork);
5730 Assert(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End);
5731 AssertPtr(pszTrunk);
5732 Assert(!(fFlags & ~INTNET_OPEN_FLAGS_MASK));
5733 AssertPtr(ppNetwork);
5734 *ppNetwork = NULL;
5735
5736 /*
5737 * Search networks by name.
5738 */
5739 PINTNETNETWORK pCur;
5740 uint8_t cchName = (uint8_t)strlen(pszNetwork);
5741 Assert(cchName && cchName < sizeof(pCur->szName)); /* caller ensures this */
5742
5743 pCur = pIntNet->pNetworks;
5744 while (pCur)
5745 {
5746 if ( pCur->cchName == cchName
5747 && !memcmp(pCur->szName, pszNetwork, cchName))
5748 {
5749 /*
5750 * Found the network, now check that we have the same ideas
5751 * about the trunk setup and security.
5752 */
5753 int rc;
5754 if ( enmTrunkType == kIntNetTrunkType_WhateverNone
5755#ifdef VBOX_WITH_NAT_SERVICE
5756 || enmTrunkType == kIntNetTrunkType_SrvNat /* @todo: what does it mean */
5757#endif
5758 || ( pCur->enmTrunkType == enmTrunkType
5759 && !strcmp(pCur->szTrunk, pszTrunk)))
5760 {
5761 rc = intnetR0CheckOpenNetworkFlags(pCur, fFlags);
5762 if (RT_SUCCESS(rc))
5763 {
5764 /*
5765 * Increment the reference and check that the session
5766 * can access this network.
5767 */
5768 rc = SUPR0ObjAddRef(pCur->pvObj, pSession);
5769 if (RT_SUCCESS(rc))
5770 {
5771 if (pCur->fFlags & INTNET_OPEN_FLAGS_ACCESS_RESTRICTED)
5772 rc = SUPR0ObjVerifyAccess(pCur->pvObj, pSession, pCur->szName);
5773 if (RT_SUCCESS(rc))
5774 *ppNetwork = pCur;
5775 else
5776 SUPR0ObjRelease(pCur->pvObj, pSession);
5777 }
5778 else if (rc == VERR_WRONG_ORDER)
5779 rc = VERR_NOT_FOUND; /* destruction race, pretend the other isn't there. */
5780 }
5781 }
5782 else
5783 {
5784 rc = VERR_INTNET_INCOMPATIBLE_TRUNK;
5785 LogRel(("intnetR0OpenNetwork failed. rc=%Rrc pCur->szTrunk=%s pszTrunk=%s pCur->enmTrunkType=%d enmTrunkType=%d\n",
5786 rc, pCur->szTrunk, pszTrunk, pCur->enmTrunkType, enmTrunkType));
5787 }
5788
5789 LogFlow(("intnetR0OpenNetwork: returns %Rrc *ppNetwork=%p\n", rc, *ppNetwork));
5790 return rc;
5791 }
5792
5793 pCur = pCur->pNext;
5794 }
5795
5796 LogFlow(("intnetR0OpenNetwork: returns VERR_NOT_FOUND\n"));
5797 return VERR_NOT_FOUND;
5798}
5799
5800
5801/**
5802 * Creates a new network.
5803 *
5804 * The call must own the INTNET::hMtxCreateOpenDestroy and has already attempted
5805 * opening the network and found it to be non-existing.
5806 *
5807 * @returns VBox status code.
5808 * @param pIntNet The instance data.
5809 * @param pSession The session handle.
5810 * @param pszNetwork The name of the network. This must be at least one character long and no longer
5811 * than the INTNETNETWORK::szName.
5812 * @param enmTrunkType The trunk type.
5813 * @param pszTrunk The trunk name. Its meaning is specific to the type.
5814 * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
5815 * @param ppNetwork Where to store the network. In the case of failure
5816 * whatever is returned here should be dereferenced
5817 * outside the INTNET::hMtxCreateOpenDestroy.
5818 */
5819static int intnetR0CreateNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
5820 const char *pszTrunk, uint32_t fFlags, PINTNETNETWORK *ppNetwork)
5821{
5822 LogFlow(("intnetR0CreateNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
5823 pIntNet, pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, ppNetwork));
5824
5825 /* just pro forma validation, the caller is internal. */
5826 AssertPtr(pIntNet);
5827 AssertPtr(pSession);
5828 AssertPtr(pszNetwork);
5829 Assert(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End);
5830 AssertPtr(pszTrunk);
5831 Assert(!(fFlags & ~INTNET_OPEN_FLAGS_MASK));
5832 AssertPtr(ppNetwork);
5833
5834 *ppNetwork = NULL;
5835
5836 /*
5837 * Adjust the flags with defaults for the network policies.
5838 * Note: Main restricts promiscuous mode on the per interface level.
5839 */
5840 fFlags &= ~( INTNET_OPEN_FLAGS_IF_FIXED
5841 | INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW
5842 | INTNET_OPEN_FLAGS_IF_PROMISC_DENY
5843 | INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK
5844 | INTNET_OPEN_FLAGS_IF_PROMISC_NO_TRUNK
5845 | INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES
5846 | INTNET_OPEN_FLAGS_REQUIRE_EXACT);
5847 uint32_t fDefFlags = INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS
5848 | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST
5849 | INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE
5850 | INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED
5851 | INTNET_OPEN_FLAGS_TRUNK_HOST_CHASTE_MODE
5852 | INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED
5853 | INTNET_OPEN_FLAGS_TRUNK_WIRE_CHASTE_MODE;
5854 if ( enmTrunkType == kIntNetTrunkType_WhateverNone
5855#ifdef VBOX_WITH_NAT_SERVICE
5856 || enmTrunkType == kIntNetTrunkType_SrvNat /* simialar security */
5857#endif
5858 || enmTrunkType == kIntNetTrunkType_None)
5859 fDefFlags |= INTNET_OPEN_FLAGS_ACCESS_RESTRICTED;
5860 else
5861 fDefFlags |= INTNET_OPEN_FLAGS_ACCESS_PUBLIC;
5862 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
5863 if (!(fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair))
5864 fFlags |= g_afIntNetOpenNetworkNetFlags[i].fPair & fDefFlags;
5865
5866 /*
5867 * Allocate and initialize.
5868 */
5869 size_t cb = sizeof(INTNETNETWORK);
5870 if (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
5871 cb += INTNETNETWORK_TMP_SIZE + 64;
5872 PINTNETNETWORK pNetwork = (PINTNETNETWORK)RTMemAllocZ(cb);
5873 if (!pNetwork)
5874 return VERR_NO_MEMORY;
5875 //pNetwork->pNext = NULL;
5876 //pNetwork->pIfs = NULL;
5877 pNetwork->hAddrSpinlock = NIL_RTSPINLOCK;
5878 pNetwork->MacTab.cEntries = 0;
5879 pNetwork->MacTab.cEntriesAllocated = INTNET_GROW_DSTTAB_SIZE;
5880 //pNetwork->MacTab.cPromiscuousEntries = 0;
5881 //pNetwork->MacTab.cPromiscuousNoTrunkEntries = 0;
5882 pNetwork->MacTab.paEntries = NULL;
5883 pNetwork->MacTab.fHostPromiscuousReal = false;
5884 pNetwork->MacTab.fHostPromiscuousEff = false;
5885 pNetwork->MacTab.fHostActive = false;
5886 pNetwork->MacTab.fWirePromiscuousReal = false;
5887 pNetwork->MacTab.fWirePromiscuousEff = false;
5888 pNetwork->MacTab.fWireActive = false;
5889 pNetwork->MacTab.pTrunk = NULL;
5890 pNetwork->hEvtBusyIf = NIL_RTSEMEVENT;
5891 pNetwork->pIntNet = pIntNet;
5892 //pNetwork->pvObj = NULL;
5893 if (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
5894 pNetwork->pbTmp = RT_ALIGN_PT(pNetwork + 1, 64, uint8_t *);
5895 //else
5896 // pNetwork->pbTmp = NULL;
5897 pNetwork->fFlags = fFlags;
5898 //pNetwork->fMinFlags = 0;
5899 //pNetwork->cActiveIFs = 0;
5900 size_t cchName = strlen(pszNetwork);
5901 pNetwork->cchName = (uint8_t)cchName;
5902 Assert(cchName && cchName < sizeof(pNetwork->szName)); /* caller's responsibility. */
5903 memcpy(pNetwork->szName, pszNetwork, cchName); /* '\0' at courtesy of alloc. */
5904 pNetwork->enmTrunkType = enmTrunkType;
5905 Assert(strlen(pszTrunk) < sizeof(pNetwork->szTrunk)); /* caller's responsibility. */
5906 strcpy(pNetwork->szTrunk, pszTrunk);
5907
5908 /*
5909 * Create the semaphore, spinlock and allocate the interface table.
5910 */
5911 int rc = RTSemEventCreate(&pNetwork->hEvtBusyIf);
5912 if (RT_SUCCESS(rc))
5913 rc = RTSpinlockCreate(&pNetwork->hAddrSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "hAddrSpinlock");
5914 if (RT_SUCCESS(rc))
5915 {
5916 pNetwork->MacTab.paEntries = (PINTNETMACTABENTRY)RTMemAlloc(sizeof(INTNETMACTABENTRY) * pNetwork->MacTab.cEntriesAllocated);
5917 if (!pNetwork->MacTab.paEntries)
5918 rc = VERR_NO_MEMORY;
5919 }
5920 if (RT_SUCCESS(rc))
5921 {
5922 /*
5923 * Register the object in the current session and link it into the network list.
5924 */
5925 pNetwork->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK, intnetR0NetworkDestruct, pNetwork, pIntNet);
5926 if (pNetwork->pvObj)
5927 {
5928 pNetwork->pNext = pIntNet->pNetworks;
5929 pIntNet->pNetworks = pNetwork;
5930
5931 /*
5932 * Check if the current session is actually allowed to create and
5933 * open the network. It is possible to implement network name
5934 * based policies and these must be checked now. SUPR0ObjRegister
5935 * does no such checks.
5936 */
5937 rc = SUPR0ObjVerifyAccess(pNetwork->pvObj, pSession, pNetwork->szName);
5938 if (RT_SUCCESS(rc))
5939 {
5940 /*
5941 * Connect the trunk.
5942 */
5943 rc = intnetR0NetworkCreateTrunkIf(pNetwork, pSession);
5944 if (RT_SUCCESS(rc))
5945 {
5946 *ppNetwork = pNetwork;
5947 LogFlow(("intnetR0CreateNetwork: returns VINF_SUCCESS *ppNetwork=%p\n", pNetwork));
5948 return VINF_SUCCESS;
5949 }
5950 }
5951
5952 SUPR0ObjRelease(pNetwork->pvObj, pSession);
5953 LogFlow(("intnetR0CreateNetwork: returns %Rrc\n", rc));
5954 return rc;
5955 }
5956
5957 /* cleanup */
5958 rc = VERR_NO_MEMORY;
5959 }
5960
5961 RTSemEventDestroy(pNetwork->hEvtBusyIf);
5962 pNetwork->hEvtBusyIf = NIL_RTSEMEVENT;
5963 RTSpinlockDestroy(pNetwork->hAddrSpinlock);
5964 pNetwork->hAddrSpinlock = NIL_RTSPINLOCK;
5965 RTMemFree(pNetwork->MacTab.paEntries);
5966 pNetwork->MacTab.paEntries = NULL;
5967 RTMemFree(pNetwork);
5968
5969 LogFlow(("intnetR0CreateNetwork: returns %Rrc\n", rc));
5970 return rc;
5971}
5972
5973
5974/**
5975 * Opens a network interface and connects it to the specified network.
5976 *
5977 * @returns VBox status code.
5978 * @param pSession The session handle.
5979 * @param pszNetwork The network name.
5980 * @param enmTrunkType The trunk type.
5981 * @param pszTrunk The trunk name. Its meaning is specific to the type.
5982 * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
5983 * @param fRestrictAccess Whether new participants should be subjected to access check or not.
5984 * @param cbSend The send buffer size.
5985 * @param cbRecv The receive buffer size.
5986 * @param phIf Where to store the handle to the network interface.
5987 */
5988INTNETR0DECL(int) IntNetR0Open(PSUPDRVSESSION pSession, const char *pszNetwork,
5989 INTNETTRUNKTYPE enmTrunkType, const char *pszTrunk, uint32_t fFlags,
5990 uint32_t cbSend, uint32_t cbRecv, PINTNETIFHANDLE phIf)
5991{
5992 LogFlow(("IntNetR0Open: pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x cbSend=%u cbRecv=%u phIf=%p\n",
5993 pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, cbSend, cbRecv, phIf));
5994
5995 /*
5996 * Validate input.
5997 */
5998 PINTNET pIntNet = g_pIntNet;
5999 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
6000 AssertReturn(pIntNet->u32Magic, VERR_INVALID_MAGIC);
6001
6002 AssertPtrReturn(pszNetwork, VERR_INVALID_PARAMETER);
6003 const char *pszNetworkEnd = RTStrEnd(pszNetwork, INTNET_MAX_NETWORK_NAME);
6004 AssertReturn(pszNetworkEnd, VERR_INVALID_PARAMETER);
6005 size_t cchNetwork = pszNetworkEnd - pszNetwork;
6006 AssertReturn(cchNetwork, VERR_INVALID_PARAMETER);
6007
6008 if (pszTrunk)
6009 {
6010 AssertPtrReturn(pszTrunk, VERR_INVALID_PARAMETER);
6011 const char *pszTrunkEnd = RTStrEnd(pszTrunk, INTNET_MAX_TRUNK_NAME);
6012 AssertReturn(pszTrunkEnd, VERR_INVALID_PARAMETER);
6013 }
6014 else
6015 pszTrunk = "";
6016
6017 AssertMsgReturn(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End,
6018 ("%d\n", enmTrunkType), VERR_INVALID_PARAMETER);
6019 switch (enmTrunkType)
6020 {
6021 case kIntNetTrunkType_None:
6022 case kIntNetTrunkType_WhateverNone:
6023#ifdef VBOX_WITH_NAT_SERVICE
6024 case kIntNetTrunkType_SrvNat:
6025#endif
6026 if (*pszTrunk)
6027 return VERR_INVALID_PARAMETER;
6028 break;
6029
6030 case kIntNetTrunkType_NetFlt:
6031 case kIntNetTrunkType_NetAdp:
6032 if (!*pszTrunk)
6033 return VERR_INVALID_PARAMETER;
6034 break;
6035
6036 default:
6037 return VERR_NOT_IMPLEMENTED;
6038 }
6039
6040 AssertMsgReturn(!(fFlags & ~INTNET_OPEN_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
6041 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkNetFlags); i++)
6042 AssertMsgReturn((fFlags & g_afIntNetOpenNetworkNetFlags[i].fPair) != g_afIntNetOpenNetworkNetFlags[i].fPair,
6043 ("%#x (%#x)\n", fFlags, g_afIntNetOpenNetworkNetFlags[i].fPair), VERR_INVALID_PARAMETER);
6044 for (uint32_t i = 0; i < RT_ELEMENTS(g_afIntNetOpenNetworkIfFlags); i++)
6045 AssertMsgReturn((fFlags & g_afIntNetOpenNetworkIfFlags[i].fPair) != g_afIntNetOpenNetworkIfFlags[i].fPair,
6046 ("%#x (%#x)\n", fFlags, g_afIntNetOpenNetworkIfFlags[i].fPair), VERR_INVALID_PARAMETER);
6047 AssertPtrReturn(phIf, VERR_INVALID_PARAMETER);
6048
6049 /*
6050 * Acquire the mutex to serialize open/create/close.
6051 */
6052 int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
6053 if (RT_FAILURE(rc))
6054 return rc;
6055
6056 /*
6057 * Try open / create the network and create an interface on it for the
6058 * caller to use.
6059 */
6060 PINTNETNETWORK pNetwork = NULL;
6061 rc = intnetR0OpenNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
6062 if (RT_SUCCESS(rc))
6063 {
6064 rc = intnetR0NetworkCreateIf(pNetwork, pSession, cbSend, cbRecv, fFlags, phIf);
6065 if (RT_SUCCESS(rc))
6066 {
6067 intnetR0AdaptOpenNetworkFlags(pNetwork, fFlags);
6068 rc = VINF_ALREADY_INITIALIZED;
6069 }
6070 else
6071 SUPR0ObjRelease(pNetwork->pvObj, pSession);
6072 }
6073 else if (rc == VERR_NOT_FOUND)
6074 {
6075 rc = intnetR0CreateNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
6076 if (RT_SUCCESS(rc))
6077 {
6078 rc = intnetR0NetworkCreateIf(pNetwork, pSession, cbSend, cbRecv, fFlags, phIf);
6079 if (RT_FAILURE(rc))
6080 SUPR0ObjRelease(pNetwork->pvObj, pSession);
6081 }
6082 }
6083
6084 RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
6085 LogFlow(("IntNetR0Open: return %Rrc *phIf=%RX32\n", rc, *phIf));
6086 return rc;
6087}
6088
6089
6090/**
6091 * VMMR0 request wrapper for IntNetR0Open.
6092 *
6093 * @returns see GMMR0MapUnmapChunk.
6094 * @param pSession The caller's session.
6095 * @param pReq The request packet.
6096 */
6097INTNETR0DECL(int) IntNetR0OpenReq(PSUPDRVSESSION pSession, PINTNETOPENREQ pReq)
6098{
6099 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
6100 return VERR_INVALID_PARAMETER;
6101 return IntNetR0Open(pSession, &pReq->szNetwork[0], pReq->enmTrunkType, pReq->szTrunk,
6102 pReq->fFlags, pReq->cbSend, pReq->cbRecv, &pReq->hIf);
6103}
6104
6105
6106/**
6107 * Count the internal networks.
6108 *
6109 * This is mainly for providing the testcase with some introspection to validate
6110 * behavior when closing interfaces.
6111 *
6112 * @returns The number of networks.
6113 */
6114INTNETR0DECL(uint32_t) IntNetR0GetNetworkCount(void)
6115{
6116 /*
6117 * Grab the instance.
6118 */
6119 PINTNET pIntNet = g_pIntNet;
6120 if (!pIntNet)
6121 return 0;
6122 AssertPtrReturn(pIntNet, 0);
6123 AssertReturn(pIntNet->u32Magic == INTNET_MAGIC, 0);
6124
6125 /*
6126 * Grab the mutex and count the networks.
6127 */
6128 int rc = RTSemMutexRequest(pIntNet->hMtxCreateOpenDestroy, RT_INDEFINITE_WAIT);
6129 if (RT_FAILURE(rc))
6130 return 0;
6131
6132 uint32_t cNetworks = 0;
6133 for (PINTNETNETWORK pCur = pIntNet->pNetworks; pCur; pCur = pCur->pNext)
6134 cNetworks++;
6135
6136 RTSemMutexRelease(pIntNet->hMtxCreateOpenDestroy);
6137
6138 return cNetworks;
6139}
6140
6141
6142
6143/**
6144 * Destroys an instance of the Ring-0 internal networking service.
6145 */
6146INTNETR0DECL(void) IntNetR0Term(void)
6147{
6148 LogFlow(("IntNetR0Term:\n"));
6149
6150 /*
6151 * Zap the global pointer and validate it.
6152 */
6153 PINTNET pIntNet = g_pIntNet;
6154 g_pIntNet = NULL;
6155 if (!pIntNet)
6156 return;
6157 AssertPtrReturnVoid(pIntNet);
6158 AssertReturnVoid(pIntNet->u32Magic == INTNET_MAGIC);
6159
6160 /*
6161 * There is not supposed to be any networks hanging around at this time.
6162 */
6163 AssertReturnVoid(ASMAtomicCmpXchgU32(&pIntNet->u32Magic, ~INTNET_MAGIC, INTNET_MAGIC));
6164 Assert(pIntNet->pNetworks == NULL);
6165 if (pIntNet->hMtxCreateOpenDestroy != NIL_RTSEMMUTEX)
6166 {
6167 RTSemMutexDestroy(pIntNet->hMtxCreateOpenDestroy);
6168 pIntNet->hMtxCreateOpenDestroy = NIL_RTSEMMUTEX;
6169 }
6170 if (pIntNet->hHtIfs != NIL_RTHANDLETABLE)
6171 {
6172 /** @todo does it make sense to have a deleter here? */
6173 RTHandleTableDestroy(pIntNet->hHtIfs, NULL, NULL);
6174 pIntNet->hHtIfs = NIL_RTHANDLETABLE;
6175 }
6176
6177 RTMemFree(pIntNet);
6178}
6179
6180
6181/**
6182 * Initializes the internal network ring-0 service.
6183 *
6184 * @returns VBox status code.
6185 */
6186INTNETR0DECL(int) IntNetR0Init(void)
6187{
6188 LogFlow(("IntNetR0Init:\n"));
6189 int rc = VERR_NO_MEMORY;
6190 PINTNET pIntNet = (PINTNET)RTMemAllocZ(sizeof(*pIntNet));
6191 if (pIntNet)
6192 {
6193 //pIntNet->pNetworks = NULL;
6194
6195 rc = RTSemMutexCreate(&pIntNet->hMtxCreateOpenDestroy);
6196 if (RT_SUCCESS(rc))
6197 {
6198 rc = RTHandleTableCreateEx(&pIntNet->hHtIfs, RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT,
6199 UINT32_C(0x8ffe0000), 4096, intnetR0IfRetainHandle, NULL);
6200 if (RT_SUCCESS(rc))
6201 {
6202 pIntNet->u32Magic = INTNET_MAGIC;
6203 g_pIntNet = pIntNet;
6204 LogFlow(("IntNetR0Init: returns VINF_SUCCESS pIntNet=%p\n", pIntNet));
6205 return VINF_SUCCESS;
6206 }
6207
6208 RTSemMutexDestroy(pIntNet->hMtxCreateOpenDestroy);
6209 }
6210 RTMemFree(pIntNet);
6211 }
6212 LogFlow(("IntNetR0Init: returns %Rrc\n", rc));
6213 return rc;
6214}
6215
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