VirtualBox

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

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

Hint for #2949.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 145.3 KB
Line 
1/* $Id: SrvIntNetR0.cpp 14609 2008-11-25 23:23:14Z vboxsync $ */
2/** @file
3 * Internal networking - The ring 0 service.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_SRV_INTNET
27#include <VBox/intnet.h>
28#include <VBox/sup.h>
29#include <VBox/pdm.h>
30#include <VBox/log.h>
31#include <iprt/asm.h>
32#include <iprt/alloc.h>
33#include <iprt/semaphore.h>
34#include <iprt/spinlock.h>
35#include <iprt/thread.h>
36#include <iprt/assert.h>
37#include <iprt/string.h>
38#include <iprt/time.h>
39#include <iprt/handletable.h>
40#include <iprt/net.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
51/*******************************************************************************
52* Structures and Typedefs *
53*******************************************************************************/
54typedef enum INTNETADDRTYPE
55{
56 /** The invalid 0 entry. */
57 kIntNetAddrType_Invalid = 0,
58 /** IP version 4. */
59 kIntNetAddrType_IPv4,
60 /** IP version 6. */
61 kIntNetAddrType_IPv6,
62 /** IPX. */
63 kIntNetAddrType_IPX,
64 /** The end of the valid values. */
65 kIntNetAddrType_End,
66 /** The usual 32-bit hack. */
67 kIntNetAddrType_32BitHack = 0x7fffffff
68} INTNETADDRTYPE;
69/** Pointer to a network layer address type. */
70typedef INTNETADDRTYPE *PINTNETADDRTYPE;
71
72
73/**
74 * Address and type.
75 */
76typedef struct INTNETADDR
77{
78 /** The address type. */
79 INTNETADDRTYPE enmType;
80 /** The address. */
81 RTNETADDRU Addr;
82} INTNETADDR;
83/** Pointer to an address. */
84typedef INTNETADDR *PINTNETADDR;
85/** Pointer to a const address. */
86typedef INTNETADDR const *PCINTNETADDR;
87
88
89/**
90 * Address cache for a specific network layer.
91 */
92typedef struct INTNETADDRCACHE
93{
94 /** Pointer to the table of addresses. */
95 uint8_t *pbEntries;
96 /** The number of valid address entries. */
97 uint8_t cEntries;
98 /** The number of allocated address entries. */
99 uint8_t cEntriesAlloc;
100 /** The address size. */
101 uint8_t cbAddress;
102 /** The size of an entry. */
103 uint8_t cbEntry;
104} INTNETADDRCACHE;
105/** Pointer to an address cache. */
106typedef INTNETADDRCACHE *PINTNETADDRCACHE;
107/** Pointer to a const address cache. */
108typedef INTNETADDRCACHE const *PCINTNETADDRCACHE;
109
110
111/**
112 * A network interface.
113 *
114 * Unless explicitly stated, all members are protect by the network semaphore.
115 */
116typedef struct INTNETIF
117{
118 /** Pointer to the next interface.
119 * This is protected by the INTNET::FastMutex. */
120 struct INTNETIF *pNext;
121 /** The current MAC address for the interface. */
122 RTMAC Mac;
123 /** Set if the INTNET::Mac member is valid. */
124 bool fMacSet;
125 /** Set if the interface is in promiscuous mode.
126 * In promiscuous mode the interface will receive all packages except the one it's sending. */
127 bool fPromiscuous;
128 /** Whether the interface is active or not. */
129 bool fActive;
130 /** Whether someone is currently in the destructor. */
131 bool volatile fDestroying;
132 /** Number of yields done to try make the interface read pending data.
133 * We will stop yeilding when this reaches a threshold assuming that the VM is paused or
134 * that it simply isn't worth all the delay. It is cleared when a successful send has been done.
135 */
136 uint32_t cYields;
137 /** Pointer to the current exchange buffer (ring-0). */
138 PINTNETBUF pIntBuf;
139 /** Pointer to ring-3 mapping of the current exchange buffer. */
140 R3PTRTYPE(PINTNETBUF) pIntBufR3;
141 /** Pointer to the default exchange buffer for the interface. */
142 PINTNETBUF pIntBufDefault;
143 /** Pointer to ring-3 mapping of the default exchange buffer. */
144 R3PTRTYPE(PINTNETBUF) pIntBufDefaultR3;
145 /** Event semaphore which a receiver thread will sleep on while waiting for data to arrive. */
146 RTSEMEVENT volatile Event;
147 /** Number of threads sleeping on the Event semaphore. */
148 uint32_t cSleepers;
149 /** The interface handle.
150 * When this is INTNET_HANDLE_INVALID a sleeper which is waking up
151 * should return with the appropriate error condition. */
152 INTNETIFHANDLE volatile hIf;
153 /** Pointer to the network this interface is connected to.
154 * This is protected by the INTNET::FastMutex. */
155 struct INTNETNETWORK *pNetwork;
156 /** The session this interface is associated with. */
157 PSUPDRVSESSION pSession;
158 /** The SUPR0 object id. */
159 void *pvObj;
160 /** The network layer address cache. (Indexed by type, 0 entry isn't used.) */
161 INTNETADDRCACHE aAddrCache[kIntNetAddrType_End];
162} INTNETIF;
163/** Pointer to an internal network interface. */
164typedef INTNETIF *PINTNETIF;
165
166
167/**
168 * A trunk interface.
169 */
170typedef struct INTNETTRUNKIF
171{
172 /** The port interface we present to the component. */
173 INTNETTRUNKSWPORT SwitchPort;
174 /** The port interface we get from the component. */
175 PINTNETTRUNKIFPORT pIfPort;
176 /** The trunk mutex that serializes all calls <b>to</b> the component. */
177 RTSEMFASTMUTEX FastMutex;
178 /** Pointer to the network we're connect to.
179 * This may be NULL if we're orphaned? */
180 struct INTNETNETWORK *pNetwork;
181 /** The cached MAC address of the interface the trunk is attached to.
182 * This is for the situations where we cannot take the out-bound
183 * semaphore (the recv case) but need to make frame edits (ARP). */
184 RTMAC CachedMac;
185 /** Whether to supply physical addresses with the outbound SGs. */
186 bool volatile fPhysSG;
187 /** Set if the 'wire' is in promiscuous mode.
188 * The state of the 'host' is queried each time. */
189 bool fPromiscuousWire;
190} INTNETTRUNKIF;
191/** Pointer to a trunk interface. */
192typedef INTNETTRUNKIF *PINTNETTRUNKIF;
193
194/** Converts a pointer to INTNETTRUNKIF::SwitchPort to a PINTNETTRUNKIF. */
195#define INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort) ((PINTNETTRUNKIF)(pSwitchPort))
196
197
198/**
199 * Internal representation of a network.
200 */
201typedef struct INTNETNETWORK
202{
203 /** The Next network in the chain.
204 * This is protected by the INTNET::FastMutex. */
205 struct INTNETNETWORK *pNext;
206 /** List of interfaces connected to the network.
207 * This is protected by the INTNET::FastMutex. */
208 PINTNETIF pIFs;
209 /** Pointer to the trunk interface.
210 * Can be NULL if there is no trunk connection. */
211 PINTNETTRUNKIF pTrunkIF;
212 /** The network mutex.
213 * It protects everything dealing with this network. */
214 RTSEMFASTMUTEX FastMutex;
215 /** Pointer to the instance data. */
216 struct INTNET *pIntNet;
217 /** The SUPR0 object id. */
218 void *pvObj;
219 /** Pointer to the temporary buffer that is used when snooping fragmented packets.
220 * This is allocated after this structure if we're sharing the MAC address with
221 * the host. The buffer is INTNETNETWORK_TMP_SIZE big and aligned on a 64-byte boundrary. */
222 uint8_t *pbTmp;
223 /** Network creation flags (INTNET_OPEN_FLAGS_*). */
224 uint32_t fFlags;
225 /** The number of active interfaces (excluding the trunk). */
226 uint32_t cActiveIFs;
227 /** The length of the network name. */
228 uint8_t cchName;
229 /** The network name. */
230 char szName[INTNET_MAX_NETWORK_NAME];
231 /** The trunk type. */
232 INTNETTRUNKTYPE enmTrunkType;
233 /** The trunk name. */
234 char szTrunk[INTNET_MAX_TRUNK_NAME];
235} INTNETNETWORK;
236/** Pointer to an internal network. */
237typedef INTNETNETWORK *PINTNETNETWORK;
238
239/** The size of the buffer INTNETNETWORK::pbTmp points at. */
240#define INTNETNETWORK_TMP_SIZE 2048
241
242
243/**
244 * Internal networking instance.
245 */
246typedef struct INTNET
247{
248 /** Mutex protecting the network creation, opening and destruction.
249 * (This means all operations affecting the pNetworks list.) */
250 RTSEMFASTMUTEX FastMutex;
251 /** List of networks. Protected by INTNET::Spinlock. */
252 PINTNETNETWORK volatile pNetworks;
253 /** Handle table for the interfaces. */
254 RTHANDLETABLE hHtIfs;
255} INTNET;
256
257
258
259/*******************************************************************************
260* Internal Functions *
261*******************************************************************************/
262static PINTNETTRUNKIF intnetR0TrunkIfRetain(PINTNETTRUNKIF pThis);
263static void intnetR0TrunkIfRelease(PINTNETTRUNKIF pThis);
264static bool intnetR0TrunkIfOutLock(PINTNETTRUNKIF pThis);
265static void intnetR0TrunkIfOutUnlock(PINTNETTRUNKIF pThis);
266
267
268/**
269 * Initializes a scatter / gather buffer from a simple linear buffer.
270 *
271 * @returns Pointer to the start of the frame.
272 * @param pSG Pointer to the scatter / gather structure.
273 * (The pvOwnerData, fFlags, cUsers, and cSegsAlloc members are left untouched.)
274 * @param pvFrame Pointer to the frame
275 * @param cbFrame The size of the frame.
276 */
277DECLINLINE(void) intnetR0SgInitTemp(PINTNETSG pSG, void *pvFrame, uint32_t cbFrame)
278{
279 pSG->pvOwnerData = NULL;
280 pSG->pvUserData = NULL;
281 pSG->pvUserData2 = NULL;
282 pSG->cbTotal = cbFrame;
283 pSG->cUsers = 1;
284 pSG->fFlags = INTNETSG_FLAGS_TEMP;
285 pSG->cSegsAlloc = 1;
286 pSG->cSegsUsed = 1;
287 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
288 pSG->aSegs[0].pv = pvFrame;
289 pSG->aSegs[0].cb = cbFrame;
290}
291
292
293/**
294 * Initializes a scatter / gather buffer from a internal networking packet.
295 *
296 * @returns Pointer to the start of the frame.
297 * @param pSG Pointer to the scatter / gather structure.
298 * (The pvOwnerData, fFlags, cUsers, and cSegsAlloc members are left untouched.)
299 * @param pHdr Pointer to the packet header.
300 * @param pBuf The buffer the header is within. Only used in strict builds.
301 */
302DECLINLINE(void) intnetR0SgInitFromPkt(PINTNETSG pSG, PCINTNETHDR pPktHdr, PCINTNETBUF pBuf)
303{
304 pSG->cSegsUsed = 1;
305 pSG->cbTotal = pSG->aSegs[0].cb = pPktHdr->cbFrame;
306 pSG->aSegs[0].pv = INTNETHdrGetFramePtr(pPktHdr, pBuf);
307 pSG->aSegs[0].Phys = NIL_RTHCPHYS;
308}
309
310
311/**
312 * Worker for intnetR0SgWritePart that deals with the case where the
313 * request doesn't fit into the first segment.
314 *
315 * @returns true, unless the request or SG invalid.
316 * @param pSG The SG list to write to.
317 * @param off Where to start writing (offset into the SG).
318 * @param cb How much to write.
319 * @param pvBuf The buffer to containing the bits to write.
320 */
321static bool intnetR0SgWritePartSlow(PCINTNETSG pSG, uint32_t off, uint32_t cb, void const *pvBuf)
322{
323 if (RT_UNLIKELY(off + cb > pSG->cbTotal))
324 return false;
325
326 /*
327 * Skip ahead to the segment where off starts.
328 */
329 unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
330 unsigned iSeg = 0;
331 while (off > pSG->aSegs[iSeg].cb)
332 {
333 off -= pSG->aSegs[iSeg++].cb;
334 AssertReturn(iSeg < cSegs, false);
335 }
336
337 /*
338 * Copy the data, hoping that it's all from one segment...
339 */
340 uint32_t cbCanCopy = pSG->aSegs[iSeg].cb - off;
341 if (cbCanCopy >= cb)
342 memcpy((uint8_t *)pSG->aSegs[iSeg].pv + off, pvBuf, cb);
343 else
344 {
345 /* copy the portion in the current segment. */
346 memcpy((uint8_t *)pSG->aSegs[iSeg].pv + off, pvBuf, cbCanCopy);
347 cb -= cbCanCopy;
348
349 /* copy the portions in the other segments. */
350 do
351 {
352 pvBuf = (uint8_t const *)pvBuf + cbCanCopy;
353 iSeg++;
354 AssertReturn(iSeg < cSegs, false);
355
356 cbCanCopy = RT_MIN(cb, pSG->aSegs[iSeg].cb);
357 memcpy(pSG->aSegs[iSeg].pv, pvBuf, cbCanCopy);
358
359 cb -= cbCanCopy;
360 } while (cb > 0);
361 }
362
363 return true;
364}
365
366
367/**
368 * Writes to a part of an SG.
369 *
370 * @returns true on success, false on failure (out of bounds).
371 * @param pSG The SG list to write to.
372 * @param off Where to start writing (offset into the SG).
373 * @param cb How much to write.
374 * @param pvBuf The buffer to containing the bits to write.
375 */
376DECLINLINE(bool) intnetR0SgWritePart(PCINTNETSG pSG, uint32_t off, uint32_t cb, void const *pvBuf)
377{
378 Assert(off + cb > off);
379
380 /* The optimized case. */
381 if (RT_LIKELY( pSG->cSegsUsed == 1
382 || pSG->aSegs[0].cb >= off + cb))
383 {
384 Assert(pSG->cbTotal == pSG->aSegs[0].cb);
385 memcpy((uint8_t *)pSG->aSegs[0].pv + off, pvBuf, cb);
386 return true;
387 }
388 return intnetR0SgWritePartSlow(pSG, off, cb, pvBuf);
389}
390
391
392/**
393 * Reads a byte from a SG list.
394 *
395 * @returns The byte on success. 0xff on failure.
396 * @param pSG The SG list to read.
397 * @param off The offset (into the SG) off the byte.
398 */
399DECLINLINE(uint8_t) intnetR0SgReadByte(PCINTNETSG pSG, uint32_t off)
400{
401 if (RT_LIKELY(pSG->aSegs[0].cb > off))
402 return ((uint8_t const *)pSG->aSegs[0].pv)[off];
403
404 off -= pSG->aSegs[0].cb;
405 unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
406 for (unsigned iSeg = 1; iSeg < cSegs; iSeg++)
407 {
408 if (pSG->aSegs[iSeg].cb > off)
409 return ((uint8_t const *)pSG->aSegs[iSeg].pv)[off];
410 off -= pSG->aSegs[iSeg].cb;
411 }
412 return false;
413}
414
415
416/**
417 * Worker for intnetR0SgReadPart that deals with the case where the
418 * requested data isn't in the first segment.
419 *
420 * @returns true, unless the SG is invalid.
421 * @param pSG The SG list to read.
422 * @param off Where to start reading (offset into the SG).
423 * @param cb How much to read.
424 * @param pvBuf The buffer to read into.
425 */
426static bool intnetR0SgReadPartSlow(PCINTNETSG pSG, uint32_t off, uint32_t cb, void *pvBuf)
427{
428 if (RT_UNLIKELY(off + cb > pSG->cbTotal))
429 return false;
430
431 /*
432 * Skip ahead to the segment where off starts.
433 */
434 unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
435 unsigned iSeg = 0;
436 while (off > pSG->aSegs[iSeg].cb)
437 {
438 off -= pSG->aSegs[iSeg++].cb;
439 AssertReturn(iSeg < cSegs, false);
440 }
441
442 /*
443 * Copy the data, hoping that it's all from one segment...
444 */
445 uint32_t cbCanCopy = pSG->aSegs[iSeg].cb - off;
446 if (cbCanCopy >= cb)
447 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv + off, cb);
448 else
449 {
450 /* copy the portion in the current segment. */
451 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv + off, cbCanCopy);
452 cb -= cbCanCopy;
453
454 /* copy the portions in the other segments. */
455 do
456 {
457 pvBuf = (uint8_t *)pvBuf + cbCanCopy;
458 iSeg++;
459 AssertReturn(iSeg < cSegs, false);
460
461 cbCanCopy = RT_MIN(cb, pSG->aSegs[iSeg].cb);
462 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[iSeg].pv, cbCanCopy);
463
464 cb -= cbCanCopy;
465 } while (cb > 0);
466 }
467
468 return true;
469}
470
471
472/**
473 * Reads a part of an SG into a buffer.
474 *
475 * @returns true on success, false on failure (out of bounds).
476 * @param pSG The SG list to read.
477 * @param off Where to start reading (offset into the SG).
478 * @param cb How much to read.
479 * @param pvBuf The buffer to read into.
480 */
481DECLINLINE(bool) intnetR0SgReadPart(PCINTNETSG pSG, uint32_t off, uint32_t cb, void *pvBuf)
482{
483 Assert(off + cb > off);
484
485 /* The optimized case. */
486 if (RT_LIKELY( pSG->cSegsUsed == 1
487 || pSG->aSegs[0].cb >= off + cb))
488 {
489 Assert(pSG->cbTotal == pSG->aSegs[0].cb);
490 memcpy(pvBuf, (uint8_t const *)pSG->aSegs[0].pv + off, cb);
491 return true;
492 }
493 return intnetR0SgReadPartSlow(pSG, off, cb, pvBuf);
494}
495
496
497/**
498 * Reads an entire SG into a fittingly size buffer.
499 *
500 * @param pSG The SG list to read.
501 * @param pvBuf The buffer to read into (at least pSG->cbTotal in size).
502 */
503DECLINLINE(void) intnetR0SgRead(PCINTNETSG pSG, void *pvBuf)
504{
505 if (pSG->cSegsUsed == 1)
506 {
507 Assert(pSG->cbTotal == pSG->aSegs[0].cb);
508 memcpy(pvBuf, pSG->aSegs[0].pv, pSG->cbTotal);
509 }
510 else
511 {
512 uint8_t *pbDst = (uint8_t *)pvBuf;
513 unsigned const cSegs = pSG->cSegsUsed; Assert(cSegs == pSG->cSegsUsed);
514 for (unsigned iSeg = 0; iSeg < cSegs; iSeg++)
515 {
516 uint32_t cbSeg = pSG->aSegs[iSeg].cb;
517 Assert(cbSeg <= pSG->cbTotal && (uintptr_t)(pbDst - (uint8_t *)pvBuf) + cbSeg <= pSG->cbTotal);
518 memcpy(pbDst, pSG->aSegs[iSeg].pv, cbSeg);
519 pbDst += cbSeg;
520 }
521 }
522}
523
524
525
526
527
528
529
530/**
531 * Retain an interface.
532 *
533 * @returns VBox status code, can assume success in most situations.
534 * @param pIf The interface instance.
535 * @param pSession The current session.
536 */
537DECLINLINE(int) intnetR0IfRetain(PINTNETIF pIf, PSUPDRVSESSION pSession)
538{
539 int rc = SUPR0ObjAddRef(pIf->pvObj, pSession);
540 AssertRCReturn(rc, rc);
541 return VINF_SUCCESS;
542}
543
544
545/**
546 * Release an interface previously retained by intnetR0IfRetain or
547 * by handle lookup/freeing.
548 *
549 * @returns VBox status code, can assume success in most situations.
550 * @param pIf The interface instance.
551 * @param pSession The current session.
552 */
553DECLINLINE(void) intnetR0IfRelease(PINTNETIF pIf, PSUPDRVSESSION pSession)
554{
555 int rc = SUPR0ObjRelease(pIf->pvObj, pSession);
556 AssertRC(rc);
557}
558
559
560/**
561 * RTHandleCreateEx callback that retains an object in the
562 * handle table before returning it.
563 *
564 * (Avoids racing the freeing of the handle.)
565 *
566 * @returns VBox status code.
567 * @param hHandleTable The handle table (ignored).
568 * @param pvObj The object (INTNETIF).
569 * @param pvCtx The context (SUPDRVSESSION).
570 * @param pvUser The user context (ignored).
571 */
572static DECLCALLBACK(int) intnetR0IfRetainHandle(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
573{
574 NOREF(pvUser);
575 NOREF(hHandleTable);
576 PINTNETIF pIf = (PINTNETIF)pvObj;
577 if (pIf->hIf != INTNET_HANDLE_INVALID) /* Don't try retain it if called from intnetR0IfDestruct. */
578 return intnetR0IfRetain(pIf, (PSUPDRVSESSION)pvCtx);
579 return VINF_SUCCESS;
580}
581
582
583
584
585
586
587/**
588 * Checks if the IPv4 address is a broadcast address.
589 * @returns true/false.
590 * @param Addr The address, network endian.
591 */
592DECLINLINE(bool) intnetR0IPv4AddrIsBroadcast(RTNETADDRIPV4 Addr)
593{
594 /* Just check for 255.255.255.255 atm. */
595 return Addr.u == UINT32_MAX;
596}
597
598
599/**
600 * Checks if the IPv4 address is a good interface address.
601 * @returns true/false.
602 * @param Addr The address, network endian.
603 */
604DECLINLINE(bool) intnetR0IPv4AddrIsGood(RTNETADDRIPV4 Addr)
605{
606 /* Usual suspects. */
607 if ( Addr.u == UINT32_MAX /* 255.255.255.255 - broadcast. */
608 || Addr.au8[0] == 0) /* Current network, can be used as source address. */
609 return false;
610
611 /* Unusual suspects. */
612 if (RT_UNLIKELY( Addr.au8[0] == 127 /* Loopback */
613 || (Addr.au8[0] & 0xf0) == 224 /* Multicast */
614 ))
615 return false;
616 return true;
617}
618
619
620/**
621 * Gets the address size of a network layer type.
622 *
623 * @returns size in bytes.
624 * @param enmType The type.
625 */
626DECLINLINE(uint8_t) intnetR0AddrSize(INTNETADDRTYPE enmType)
627{
628 switch (enmType)
629 {
630 case kIntNetAddrType_IPv4: return 4;
631 case kIntNetAddrType_IPv6: return 16;
632 case kIntNetAddrType_IPX: return 4 + 6;
633 default: AssertFailedReturn(0);
634 }
635}
636
637
638/**
639 * Compares two address to see if they are equal, assuming naturally align structures.
640 *
641 * @returns true if equal, false if not.
642 * @param pAddr1 The first address.
643 * @param pAddr2 The second address.
644 * @param cbAddr The address size.
645 */
646DECLINLINE(bool) intnetR0AddrUIsEqualEx(PCRTNETADDRU pAddr1, PCRTNETADDRU pAddr2, uint8_t const cbAddr)
647{
648 switch (cbAddr)
649 {
650 case 4: /* IPv4 */
651 return pAddr1->au32[0] == pAddr2->au32[0];
652 case 16: /* IPv6 */
653 return pAddr1->au64[0] == pAddr2->au64[0]
654 && pAddr1->au64[1] == pAddr2->au64[1];
655 case 10: /* IPX */
656 return pAddr1->au64[0] == pAddr2->au64[0]
657 && pAddr1->au16[4] == pAddr2->au16[4];
658 default:
659 AssertFailedReturn(false);
660 }
661}
662
663
664/**
665 * Worker for intnetR0IfAddrCacheLookup that performs the lookup
666 * in the remaining cache entries after the caller has check the
667 * most likely ones.
668 *
669 * @returns -1 if not found, the index of the cache entry if found.
670 * @param pCache The cache.
671 * @param pAddr The address.
672 * @param cbAddr The address size (optimization).
673 */
674static int intnetR0IfAddrCacheLookupSlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
675{
676 unsigned i = pCache->cEntries - 2;
677 uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry * i;
678 while (i >= 1)
679 {
680 if (intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr))
681 return i;
682 pbEntry -= pCache->cbEntry;
683 i--;
684 }
685
686 return -1;
687}
688
689/**
690 * Lookup an address in a cache without any expectations.
691 *
692 * @returns -1 if not found, the index of the cache entry if found.
693 * @param pCache The cache.
694 * @param pAddr The address.
695 * @param cbAddr The address size (optimization).
696 */
697DECLINLINE(int) intnetR0IfAddrCacheLookup(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
698{
699 Assert(pCache->cbAddress == cbAddr);
700
701 /*
702 * The optimized case is when there is one cache entry and
703 * it doesn't match.
704 */
705 unsigned i = pCache->cEntries;
706 if ( i > 0
707 && intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr))
708 return 0;
709 if (i <= 1)
710 return -1;
711
712 /*
713 * Check the last entry.
714 */
715 i--;
716 if (intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))
717 return i;
718 if (i <= 1)
719 return -1;
720
721 return intnetR0IfAddrCacheLookupSlow(pCache, pAddr, cbAddr);
722}
723
724
725/** Same as intnetR0IfAddrCacheLookup except we expect the address to be present already. */
726DECLINLINE(int) intnetR0IfAddrCacheLookupLikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
727{
728 /** @todo implement this. */
729 return intnetR0IfAddrCacheLookup(pCache, pAddr, cbAddr);
730}
731
732
733/**
734 * Worker for intnetR0IfAddrCacheLookupUnlikely that performs
735 * the lookup in the remaining cache entries after the caller
736 * has check the most likely ones.
737 *
738 * The routine is expecting not to find the address.
739 *
740 * @returns -1 if not found, the index of the cache entry if found.
741 * @param pCache The cache.
742 * @param pAddr The address.
743 * @param cbAddr The address size (optimization).
744 */
745static int intnetR0IfAddrCacheInCacheUnlikelySlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
746{
747 /*
748 * Perform a full table lookup.
749 */
750 unsigned i = pCache->cEntries - 2;
751 uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry * i;
752 while (i >= 1)
753 {
754 if (RT_UNLIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr)))
755 return i;
756 pbEntry -= pCache->cbEntry;
757 i--;
758 }
759
760 return -1;
761}
762
763
764/**
765 * Lookup an address in a cache expecting not to find it.
766 *
767 * @returns -1 if not found, the index of the cache entry if found.
768 * @param pCache The cache.
769 * @param pAddr The address.
770 * @param cbAddr The address size (optimization).
771 */
772DECLINLINE(int) intnetR0IfAddrCacheLookupUnlikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
773{
774 Assert(pCache->cbAddress == cbAddr);
775
776 /*
777 * The optimized case is when there is one cache entry and
778 * it doesn't match.
779 */
780 unsigned i = pCache->cEntries;
781 if (RT_UNLIKELY( i > 0
782 && intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr)))
783 return 0;
784 if (RT_LIKELY(i <= 1))
785 return -1;
786
787 /*
788 * Then check the last entry and return if there are just two cache entries.
789 */
790 i--;
791 if (RT_UNLIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr)))
792 return i;
793 if (i <= 1)
794 return -1;
795
796 return intnetR0IfAddrCacheInCacheUnlikelySlow(pCache, pAddr, cbAddr);
797}
798
799
800/**
801 * Deletes a specific cache entry.
802 *
803 * Worker for intnetR0NetworkAddrCacheDelete and intnetR0NetworkAddrCacheDeleteMinusIf.
804 *
805 * @param pIf The interface (for logging).
806 * @param pCache The cache.
807 * @param iEntry The entry to delete.
808 * @param pszMsg Log message.
809 */
810static void intnetR0IfAddrCacheDeleteIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, int iEntry, const char *pszMsg)
811{
812 AssertReturnVoid(iEntry < pCache->cEntries);
813 AssertReturnVoid(iEntry >= 0);
814#ifdef LOG_ENABLED
815 INTNETADDRTYPE enmAddrType = (INTNETADDRTYPE)(uintptr_t)(pCache - &pIf->aAddrCache[0]);
816 PCRTNETADDRU pAddr = (PCRTNETADDRU)(pCache->pbEntries + iEntry * pCache->cbEntry);
817 switch (enmAddrType)
818 {
819 case kIntNetAddrType_IPv4:
820 Log(("intnetR0IfAddrCacheDeleteIt: hIf=%#x MAC=%.6Rhxs IPv4 added #%d %d.%d.%d.%d %s\n",
821 pIf->hIf, &pIf->Mac, iEntry, pAddr->au8[0], pAddr->au8[1], pAddr->au8[2], pAddr->au8[3], pszMsg));
822 break;
823 default:
824 Log(("intnetR0IfAddrCacheDeleteIt: hIf=%RX32 MAC=%.6Rhxs type=%d #%d %.*Rhxs %s\n",
825 pIf->hIf, &pIf->Mac, enmAddrType, iEntry, pCache->cbAddress, pAddr, pszMsg));
826 break;
827 }
828#endif
829
830 pCache->cEntries--;
831 if (iEntry < pCache->cEntries)
832 memmove(pCache->pbEntries + iEntry * pCache->cbEntry,
833 pCache->pbEntries + (iEntry + 1) * pCache->cbEntry,
834 (pCache->cEntries - iEntry) * pCache->cbEntry);
835}
836
837
838/**
839 * Deletes an address from the cache, assuming it isn't actually in the cache.
840 *
841 * @param pIf The interface (for logging).
842 * @param pCache The cache.
843 * @param pAddr The address.
844 * @param cbAddr The address size (optimization).
845 */
846DECLINLINE(void) intnetR0IfAddrCacheDelete(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
847{
848 int i = intnetR0IfAddrCacheLookup(pCache, pAddr, cbAddr);
849 if (RT_UNLIKELY(i >= 0))
850 intnetR0IfAddrCacheDeleteIt(pIf, pCache, i, pszMsg);
851}
852
853
854/**
855 * Deletes the address from all the interface caches.
856 *
857 * This is used to remove stale entries that has been reassigned to
858 * other machines on the network.
859 *
860 * @param pNetwork The network.
861 * @param pAddr The address.
862 * @param enmType The address type.
863 * @param cbAddr The address size (optimization).
864 * @param pszMsg Log message.
865 */
866DECLINLINE(void) intnetR0NetworkAddrCacheDelete(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType,
867 uint8_t const cbAddr, const char *pszMsg)
868{
869 for (PINTNETIF pIf = pNetwork->pIFs; pIf; pIf = pIf->pNext)
870 {
871 int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
872 if (RT_UNLIKELY(i >= 0))
873 intnetR0IfAddrCacheDeleteIt(pIf, &pIf->aAddrCache[enmType], i, pszMsg);
874 }
875}
876
877
878/**
879 * Deletes the address from all the interface caches except the specified one.
880 *
881 * This is used to remove stale entries that has been reassigned to
882 * other machines on the network.
883 *
884 * @param pNetwork The network.
885 * @param pAddr The address.
886 * @param enmType The address type.
887 * @param cbAddr The address size (optimization).
888 * @param pszMsg Log message.
889 */
890DECLINLINE(void) intnetR0NetworkAddrCacheDeleteMinusIf(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCRTNETADDRU pAddr,
891 INTNETADDRTYPE const enmType, uint8_t const cbAddr, const char *pszMsg)
892{
893 for (PINTNETIF pIf = pNetwork->pIFs; pIf; pIf = pIf->pNext)
894 if (pIf != pIfSender)
895 {
896 int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
897 if (RT_UNLIKELY(i >= 0))
898 intnetR0IfAddrCacheDeleteIt(pIf, &pIf->aAddrCache[enmType], i, pszMsg);
899 }
900}
901
902
903/**
904 * Lookup an address on the network, returning the (first) interface
905 * having it in its address cache.
906 *
907 * @returns Pointer to the interface on success, NULL if not found.
908 * @param pNetwork The network.
909 * @param pAddr The address to lookup.
910 * @param enmType The address type.
911 * @param cbAddr The size of the address.
912 */
913DECLINLINE(PINTNETIF) intnetR0NetworkAddrCacheLookupIf(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType, uint8_t const cbAddr)
914{
915 for (PINTNETIF pIf = pNetwork->pIFs; pIf; pIf = pIf->pNext)
916 {
917 int i = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmType], pAddr, cbAddr);
918 if (i >= 0)
919 return pIf;
920 }
921 return NULL;
922}
923
924
925/**
926 * Adds an address to the cache, the caller is responsible for making sure it'
927 * s not already in the cache.
928 *
929 * @param pIf The interface (for logging).
930 * @param pCache The address cache.
931 * @param pAddr The address.
932 * @param pszMsg log message.
933 */
934static void intnetR0IfAddrCacheAddIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, const char *pszMsg)
935{
936 if (!pCache->cEntriesAlloc)
937 {
938 /* Allocate the first array */
939 pCache->pbEntries = (uint8_t *)RTMemAllocZ(pCache->cbEntry * 16);
940 if (!pCache->pbEntries)
941 return;
942 pCache->cEntriesAlloc = 16;
943 }
944 else if (pCache->cEntries >= pCache->cEntriesAlloc)
945 {
946 bool fReplace = true;
947 if (pCache->cEntriesAlloc < 64)
948 {
949 uint8_t cEntriesAlloc = pCache->cEntriesAlloc + 16;
950 void *pvNew = RTMemRealloc(pCache->pbEntries, pCache->cbEntry * cEntriesAlloc);
951 if (pvNew)
952 {
953 pCache->pbEntries = (uint8_t *)pvNew;
954 pCache->cEntriesAlloc = cEntriesAlloc;
955 fReplace = false;
956 }
957 }
958 if (fReplace)
959 {
960 /* simple FIFO, might consider usage/ageing here... */
961 Log(("intnetR0IfAddrCacheAddIt: type=%d replacing %.*Rhxs\n",
962 (int)(uintptr_t)(pCache - &pIf->aAddrCache[0]), pCache->cbAddress, pCache->pbEntries));
963 memmove(pCache->pbEntries, pCache->pbEntries + pCache->cbEntry, pCache->cbEntry * (pCache->cEntries - 1));
964 pCache->cEntries--;
965 }
966 }
967
968 /*
969 * Add the new entry to the end of the array.
970 */
971 uint8_t *pbEntry = pCache->pbEntries + pCache->cEntries * pCache->cbEntry;
972 memcpy(pbEntry, pAddr, pCache->cbAddress);
973 memset(pbEntry + pCache->cbAddress, '\0', pCache->cbEntry - pCache->cbAddress);
974#ifdef LOG_ENABLED
975 INTNETADDRTYPE enmAddrType = (INTNETADDRTYPE)(uintptr_t)(pCache - &pIf->aAddrCache[0]);
976 switch (enmAddrType)
977 {
978 case kIntNetAddrType_IPv4:
979 Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs IPv4 added #%d %d.%d.%d.%d %s\n",
980 pIf->hIf, &pIf->Mac, pCache->cEntries, pAddr->au8[0], pAddr->au8[1], pAddr->au8[2], pAddr->au8[3], pszMsg));
981 break;
982 default:
983 Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs type=%d added #%d %.*Rhxs %s\n",
984 pIf->hIf, &pIf->Mac, enmAddrType, pCache->cEntries, pCache->cbAddress, pAddr, pszMsg));
985 break;
986 }
987#endif
988 pCache->cEntries++;
989 Assert(pCache->cEntries <= pCache->cEntriesAlloc);
990}
991
992
993/**
994 * A intnetR0IfAddrCacheAdd worker that performs the rest of the lookup.
995 *
996 * @param pIf The interface (for logging).
997 * @param pCache The address cache.
998 * @param pAddr The address.
999 * @param cbAddr The size of the address (optimization).
1000 * @param pszMsg Log message.
1001 */
1002static void intnetR0IfAddrCacheAddSlow(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
1003{
1004 /*
1005 * Check all but the first and last entries, the caller
1006 * has already checked those.
1007 */
1008 int i = pCache->cEntries - 2;
1009 uint8_t const *pbEntry = pCache->pbEntries + pCache->cbEntry;
1010 while (i >= 1)
1011 {
1012 if (RT_LIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)pbEntry, pAddr, cbAddr)))
1013 return;
1014 pbEntry += pCache->cbEntry;
1015 i--;
1016 }
1017
1018 /*
1019 * Not found, add it.
1020 */
1021 intnetR0IfAddrCacheAddIt(pIf, pCache, pAddr, pszMsg);
1022}
1023
1024
1025/**
1026 * Adds an address to the cache if it's not already there.
1027 *
1028 * @param pIf The interface (for logging).
1029 * @param pCache The address cache.
1030 * @param pAddr The address.
1031 * @param cbAddr The size of the address (optimization).
1032 * @param pszMsg Log message.
1033 */
1034DECLINLINE(void) intnetR0IfAddrCacheAdd(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
1035{
1036 Assert(pCache->cbAddress == cbAddr);
1037
1038 /*
1039 * The optimized case is when the address the first or last cache entry.
1040 */
1041 unsigned i = pCache->cEntries;
1042 if (RT_LIKELY( i > 0
1043 && ( intnetR0AddrUIsEqualEx((PCRTNETADDRU)pCache->pbEntries, pAddr, cbAddr)
1044 || (i > 1
1045 && intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))) ))
1046 return;
1047 intnetR0IfAddrCacheAddSlow(pIf, pCache, pAddr, cbAddr, pszMsg);
1048}
1049
1050
1051#ifdef INTNET_WITH_DHCP_SNOOPING
1052
1053/**
1054 * Snoops IP assignments and releases from the DHCPv4 traffic.
1055 *
1056 * The caller is responsible for making sure this traffic between the
1057 * BOOTPS and BOOTPC ports and validate the IP header. The UDP packet
1058 * need not be validated beyond the ports.
1059 *
1060 * @param pNetwork The network this frame was seen on.
1061 * @param pIpHdr Pointer to a valid IP header. This is for pseudo
1062 * header validation, so only the minimum header size
1063 * needs to be available and valid here.
1064 * @param pUdpHdr Pointer to the UDP header in the frame.
1065 * @param cbUdpPkt What's left of the frame when starting at the UDP header.
1066 */
1067static void intnetR0NetworkSnoopDhcp(PINTNETNETWORK pNetwork, PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, uint32_t cbUdpPkt)
1068{
1069 /*
1070 * Check if the DHCP message is valid and get the type.
1071 */
1072 if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbUdpPkt))
1073 {
1074 Log6(("Bad UDP packet\n"));
1075 return;
1076 }
1077 PCRTNETBOOTP pDhcp = (PCRTNETBOOTP)(pUdpHdr + 1);
1078 uint8_t MsgType;
1079 if (!RTNetIPv4IsDHCPValid(pUdpHdr, pDhcp, cbUdpPkt - sizeof(*pUdpHdr), &MsgType))
1080 {
1081 Log6(("Bad DHCP packet\n"));
1082 return;
1083 }
1084
1085#ifdef LOG_ENABLED
1086 /*
1087 * Log it.
1088 */
1089 const char *pszType = "unknown";
1090 switch (MsgType)
1091 {
1092 case RTNET_DHCP_MT_DISCOVER: pszType = "discover"; break;
1093 case RTNET_DHCP_MT_OFFER: pszType = "offer"; break;
1094 case RTNET_DHCP_MT_REQUEST: pszType = "request"; break;
1095 case RTNET_DHCP_MT_DECLINE: pszType = "decline"; break;
1096 case RTNET_DHCP_MT_ACK: pszType = "ack";break;
1097 case RTNET_DHCP_MT_NAC: pszType = "nac"; break;
1098 case RTNET_DHCP_MT_RELEASE: pszType = "release"; break;
1099 case RTNET_DHCP_MT_INFORM: pszType = "inform"; break;
1100 }
1101 Log6(("DHCP msg: %d (%s) client %.6Rhxs ciaddr=%d.%d.%d.%d yiaddr=%d.%d.%d.%d\n", MsgType, pszType, &pDhcp->bp_chaddr,
1102 pDhcp->bp_ciaddr.au8[0], pDhcp->bp_ciaddr.au8[1], pDhcp->bp_ciaddr.au8[2], pDhcp->bp_ciaddr.au8[3],
1103 pDhcp->bp_yiaddr.au8[0], pDhcp->bp_yiaddr.au8[1], pDhcp->bp_yiaddr.au8[2], pDhcp->bp_yiaddr.au8[3]));
1104#endif /* LOG_EANBLED */
1105
1106 /*
1107 * Act upon the message.
1108 */
1109 switch (MsgType)
1110 {
1111#if 0
1112 case RTNET_DHCP_MT_REQUEST:
1113 /** @todo Check for valid non-broadcast requests w/ IP for any of the MACs we
1114 * know, and add the IP to the cache. */
1115 break;
1116#endif
1117
1118
1119 /*
1120 * Lookup the interface by its MAC address and insert the IPv4 address into the cache.
1121 * Delete the old client address first, just in case it changed in a renewal.
1122 */
1123 case RTNET_DHCP_MT_ACK:
1124 if (intnetR0IPv4AddrIsGood(pDhcp->bp_yiaddr))
1125 for (PINTNETIF pCur = pNetwork->pIFs; pCur; pCur = pCur->pNext)
1126 if ( pCur->fMacSet
1127 && !memcmp(&pCur->Mac, &pDhcp->bp_chaddr, sizeof(RTMAC)))
1128 {
1129 intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
1130 (PCRTNETADDRU)&pDhcp->bp_ciaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_ACK");
1131 intnetR0IfAddrCacheAdd(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
1132 (PCRTNETADDRU)&pDhcp->bp_yiaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_ACK");
1133 break;
1134 }
1135 break;
1136
1137
1138 /*
1139 * Lookup the interface by its MAC address and remove the IPv4 address(es) from the cache.
1140 */
1141 case RTNET_DHCP_MT_RELEASE:
1142 {
1143 for (PINTNETIF pCur = pNetwork->pIFs; pCur; pCur = pCur->pNext)
1144 if ( pCur->fMacSet
1145 && !memcmp(&pCur->Mac, &pDhcp->bp_chaddr, sizeof(RTMAC)))
1146 {
1147 intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
1148 (PCRTNETADDRU)&pDhcp->bp_ciaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_RELEASE");
1149 intnetR0IfAddrCacheDelete(pCur, &pCur->aAddrCache[kIntNetAddrType_IPv4],
1150 (PCRTNETADDRU)&pDhcp->bp_yiaddr, sizeof(RTNETADDRIPV4), "DHCP_MT_RELEASE");
1151 }
1152 break;
1153 }
1154 }
1155
1156}
1157
1158
1159/**
1160 * Worker for intnetR0TrunkIfSnoopAddr that takes care of what
1161 * is likely to be a DHCP message.
1162 *
1163 * The caller has already check that the UDP source and destination ports
1164 * are BOOTPS or BOOTPC.
1165 *
1166 * @param pNetwork The network this frame was seen on.
1167 * @param pSG The gather list for the frame.
1168 */
1169static void intnetR0TrunkIfSnoopDhcp(PINTNETNETWORK pNetwork, PCINTNETSG pSG)
1170{
1171 /*
1172 * Get a pointer to a linear copy of the full packet, using the
1173 * temporary buffer if necessary.
1174 */
1175 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((PCRTNETETHERHDR)pSG->aSegs[0].pv + 1);
1176 size_t cbPacket = pSG->cbTotal - sizeof(RTNETETHERHDR);
1177 if (pSG->cSegsUsed > 1)
1178 {
1179 cbPacket = RT_MIN(cbPacket, INTNETNETWORK_TMP_SIZE);
1180 Log6(("intnetR0TrunkIfSnoopDhcp: Copying IPv4/UDP/DHCP pkt %u\n", cbPacket));
1181 if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
1182 return;
1183 //pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
1184 pIpHdr = (PCRTNETIPV4)pNetwork->pbTmp;
1185 }
1186
1187 /*
1188 * Validate the IP header and find the UDP packet.
1189 */
1190 if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, pSG->cbTotal - sizeof(RTNETETHERHDR)))
1191 {
1192 Log(("intnetR0TrunkIfSnoopDhcp: bad ip header\n"));
1193 return;
1194 }
1195 size_t cbIpHdr = pIpHdr->ip_hl * 4;
1196
1197 /*
1198 * Hand it over to the common DHCP snooper.
1199 */
1200 intnetR0NetworkSnoopDhcp(pNetwork, pIpHdr, (PCRTNETUDP)((uintptr_t)pIpHdr + cbIpHdr), cbPacket - cbIpHdr);
1201}
1202
1203#endif /* INTNET_WITH_DHCP_SNOOPING */
1204
1205
1206/**
1207 * Snoops up source addresses from ARP requests and purge these
1208 * from the address caches.
1209 *
1210 * The purpose of this purging is to get rid of stale addresses.
1211 *
1212 * @param pNetwork The network this frame was seen on.
1213 * @param pSG The gather list for the frame.
1214 */
1215static void intnetR0TrunkIfSnoopArp(PINTNETNETWORK pNetwork, PCINTNETSG pSG)
1216{
1217 /*
1218 * Check the minimum size first.
1219 */
1220 if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4)))
1221 return;
1222
1223 /*
1224 * Copy to temporary buffer if necessary.
1225 */
1226 size_t cbPacket = RT_MIN(pSG->cbTotal, sizeof(RTNETARPIPV4));
1227 PCRTNETARPIPV4 pArpIPv4 = (PCRTNETARPIPV4)((uintptr_t)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
1228 if ( pSG->cSegsUsed != 1
1229 && pSG->aSegs[0].cb < cbPacket)
1230 {
1231 if ( (pSG->fFlags & (INTNETSG_FLAGS_ARP_IPV4 | INTNETSG_FLAGS_PKT_CP_IN_TMP))
1232 != (INTNETSG_FLAGS_ARP_IPV4 | INTNETSG_FLAGS_PKT_CP_IN_TMP)
1233 && !intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), cbPacket, pNetwork->pbTmp))
1234 return;
1235 pArpIPv4 = (PCRTNETARPIPV4)pNetwork->pbTmp;
1236 }
1237
1238 /*
1239 * Ignore packets which doesn't interest us or we perceive as malformed.
1240 */
1241 if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
1242 || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
1243 || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
1244 || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
1245 return;
1246 uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
1247 if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
1248 && ar_oper != RTNET_ARPOP_REPLY))
1249 {
1250 Log6(("ts-ar: op=%#x\n", ar_oper));
1251 return;
1252 }
1253
1254 /*
1255 * Delete the source address if it's OK.
1256 */
1257 if ( !(pArpIPv4->ar_sha.au8[0] & 1)
1258 && ( pArpIPv4->ar_sha.au16[0]
1259 || pArpIPv4->ar_sha.au16[1]
1260 || pArpIPv4->ar_sha.au16[2])
1261 && intnetR0IPv4AddrIsGood(pArpIPv4->ar_spa))
1262 {
1263 Log6(("ts-ar: %d.%d.%d.%d / %.6Rhxs\n", pArpIPv4->ar_spa.au8[0], pArpIPv4->ar_spa.au8[1],
1264 pArpIPv4->ar_spa.au8[2], pArpIPv4->ar_spa.au8[3], &pArpIPv4->ar_sha));
1265 intnetR0NetworkAddrCacheDelete(pNetwork, (PCRTNETADDRU)&pArpIPv4->ar_spa,
1266 kIntNetAddrType_IPv4, sizeof(pArpIPv4->ar_spa), "tif/arp");
1267 }
1268}
1269
1270
1271#ifdef INTNET_WITH_DHCP_SNOOPING
1272/**
1273 * Snoop up addresses from ARP and DHCP traffic from frames comming
1274 * over the trunk connection.
1275 *
1276 * The caller is responsible for do some basic filtering before calling
1277 * this function.
1278 * For IPv4 this means checking against the minimum DHCPv4 frame size.
1279 *
1280 * @param pNetwork The network.
1281 * @param pSG The SG list for the frame.
1282 * @param EtherType The Ethertype of the frame.
1283 */
1284static void intnetR0TrunkIfSnoopAddr(PINTNETNETWORK pNetwork, PCINTNETSG pSG, uint16_t EtherType)
1285{
1286 switch (EtherType)
1287 {
1288 case RTNET_ETHERTYPE_IPV4:
1289 {
1290 uint32_t cbIpHdr;
1291 uint8_t b;
1292
1293 Assert(pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN);
1294 if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN)
1295 {
1296 /* check if the protocol is UDP */
1297 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)((uint8_t const *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
1298 if (pIpHdr->ip_p != RTNETIPV4_PROT_UDP)
1299 return;
1300
1301 /* get the TCP header length */
1302 cbIpHdr = pIpHdr->ip_hl * 4;
1303 }
1304 else
1305 {
1306 /* check if the protocol is UDP */
1307 if ( intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_p))
1308 != RTNETIPV4_PROT_UDP)
1309 return;
1310
1311 /* get the TCP header length */
1312 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + 0); /* (IPv4 first byte, a bitfield) */
1313 cbIpHdr = (b & 0x0f) * 4;
1314 }
1315 if (cbIpHdr < RTNETIPV4_MIN_LEN)
1316 return;
1317
1318 /* compare the ports. */
1319 if (pSG->aSegs[0].cb >= sizeof(RTNETETHERHDR) + cbIpHdr + RTNETUDP_MIN_LEN)
1320 {
1321 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t const *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR) + cbIpHdr);
1322 if ( ( RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPS
1323 && RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPS)
1324 || ( RT_BE2H_U16(pUdpHdr->uh_dport) != RTNETIPV4_PORT_BOOTPC
1325 && RT_BE2H_U16(pUdpHdr->uh_sport) != RTNETIPV4_PORT_BOOTPC))
1326 return;
1327 }
1328 else
1329 {
1330 /* get the lower byte of the UDP source port number. */
1331 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_sport) + 1);
1332 if ( b != RTNETIPV4_PORT_BOOTPS
1333 && b != RTNETIPV4_PORT_BOOTPC)
1334 return;
1335 uint8_t SrcPort = b;
1336 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_sport));
1337 if (b)
1338 return;
1339
1340 /* get the lower byte of the UDP destination port number. */
1341 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_dport) + 1);
1342 if ( b != RTNETIPV4_PORT_BOOTPS
1343 && b != RTNETIPV4_PORT_BOOTPC)
1344 return;
1345 if (b == SrcPort)
1346 return;
1347 b = intnetR0SgReadByte(pSG, sizeof(RTNETETHERHDR) + cbIpHdr + RT_OFFSETOF(RTNETUDP, uh_dport));
1348 if (b)
1349 return;
1350 }
1351 intnetR0TrunkIfSnoopDhcp(pNetwork, pSG);
1352 break;
1353 }
1354
1355 case RTNET_ETHERTYPE_IPV6:
1356 {
1357 /** @todo IPv6: Check for ICMPv6. It looks like type 133 (Router solicitation) might
1358 * need to be edited. Check out how NDP works... */
1359 break;
1360 }
1361
1362 case RTNET_ETHERTYPE_ARP:
1363 intnetR0TrunkIfSnoopArp(pNetwork, pSG);
1364 break;
1365 }
1366}
1367#endif /* INTNET_WITH_DHCP_SNOOPING */
1368
1369
1370/**
1371 * Deals with an IPv4 packet.
1372 *
1373 * This will fish out the source IP address and add it to the cache.
1374 * Then it will look for DHCPRELEASE requests (?) and anything else
1375 * that we migh find useful later.
1376 *
1377 * @param pIf The interface that's sending the frame.
1378 * @param pIpHdr Pointer to the IPv4 header in the frame.
1379 * @param cbPacket The size of the packet, or more correctly the
1380 * size of the frame without the ethernet header.
1381 */
1382static void intnetR0IfSnoopIPv4SourceAddr(PINTNETIF pIf, PCRTNETIPV4 pIpHdr, uint32_t cbPacket)
1383{
1384 /*
1385 * Check the header size first to prevent access invalid data.
1386 */
1387 if (cbPacket < RTNETIPV4_MIN_LEN)
1388 return;
1389 uint32_t cbHdr = (uint32_t)pIpHdr->ip_hl * 4;
1390 if ( cbHdr < RTNETIPV4_MIN_LEN
1391 || cbPacket < cbHdr)
1392 return;
1393
1394 /*
1395 * If the source address is good (not broadcast or my network) and
1396 * not already in the address cache of the sender, add it. Validate
1397 * the IP header before adding it.
1398 */
1399 bool fValidatedIpHdr = false;
1400 RTNETADDRU Addr;
1401 Addr.IPv4 = pIpHdr->ip_src;
1402 if ( intnetR0IPv4AddrIsGood(Addr.IPv4)
1403 && intnetR0IfAddrCacheLookupLikely(&pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, sizeof(Addr.IPv4)) < 0)
1404 {
1405 if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, cbPacket))
1406 {
1407 Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header\n"));
1408 return;
1409 }
1410 intnetR0IfAddrCacheAddIt(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, "if/ipv4");
1411 fValidatedIpHdr = true;
1412 }
1413
1414#ifdef INTNET_WITH_DHCP_SNOOPING
1415 /*
1416 * Check for potential DHCP packets.
1417 */
1418 if ( pIpHdr->ip_p == RTNETIPV4_PROT_UDP /* DHCP is UDP. */
1419 && cbPacket >= cbHdr + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN) /* Min DHCP packet len */
1420 {
1421 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t const *)pIpHdr + cbHdr);
1422 if ( ( RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPS
1423 || RT_BE2H_U16(pUdpHdr->uh_sport) == RTNETIPV4_PORT_BOOTPS)
1424 && ( RT_BE2H_U16(pUdpHdr->uh_sport) == RTNETIPV4_PORT_BOOTPC
1425 || RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPC))
1426 {
1427 if ( fValidatedIpHdr
1428 || RTNetIPv4IsHdrValid(pIpHdr, cbPacket, cbPacket))
1429 intnetR0NetworkSnoopDhcp(pIf->pNetwork, pIpHdr, pUdpHdr, cbPacket - cbHdr);
1430 else
1431 Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header (dhcp)\n"));
1432 }
1433 }
1434#endif /* INTNET_WITH_DHCP_SNOOPING */
1435}
1436
1437
1438/**
1439 * Snoop up source addresses from an ARP request or reply.
1440 *
1441 * @param pIf The interface that's sending the frame.
1442 * @param pHdr The ARP header.
1443 * @param cbPacket The size of the packet (migth be larger than the ARP
1444 * request 'cause of min ethernet frame size).
1445 * @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
1446 * don't have to repeat the frame parsing in intnetR0TrunkIfSend.
1447 */
1448static void intnetR0IfSnoopArpAddr(PINTNETIF pIf, PCRTNETARPIPV4 pArpIPv4, uint32_t cbPacket, uint16_t *pfSgFlags)
1449{
1450 /*
1451 * Ignore packets which doesn't interest us or we perceive as malformed.
1452 */
1453 if (RT_UNLIKELY(cbPacket < sizeof(RTNETARPIPV4)))
1454 return;
1455 if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
1456 || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
1457 || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
1458 || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
1459 return;
1460 uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
1461 if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
1462 && ar_oper != RTNET_ARPOP_REPLY))
1463 {
1464 Log6(("ar_oper=%#x\n", ar_oper));
1465 return;
1466 }
1467
1468 /*
1469 * Tag the SG as ARP IPv4 for later editing, then check for addresses
1470 * which can be removed or added to the address cache of the sender.
1471 */
1472 *pfSgFlags |= INTNETSG_FLAGS_ARP_IPV4;
1473
1474 if ( ar_oper == RTNET_ARPOP_REPLY
1475 && !(pArpIPv4->ar_tha.au8[0] & 1)
1476 && ( pArpIPv4->ar_tha.au16[0]
1477 || pArpIPv4->ar_tha.au16[1]
1478 || pArpIPv4->ar_tha.au16[2])
1479 && intnetR0IPv4AddrIsGood(pArpIPv4->ar_tpa))
1480 intnetR0IfAddrCacheDelete(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4],
1481 (PCRTNETADDRU)&pArpIPv4->ar_tpa, sizeof(RTNETADDRIPV4), "if/arp");
1482
1483 if ( !memcmp(&pArpIPv4->ar_sha, &pIf->Mac, sizeof(RTMAC))
1484 && intnetR0IPv4AddrIsGood(pArpIPv4->ar_spa))
1485 intnetR0IfAddrCacheAdd(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4],
1486 (PCRTNETADDRU)&pArpIPv4->ar_spa, sizeof(RTNETADDRIPV4), "if/arp");
1487}
1488
1489
1490
1491/**
1492 * Checks packets send by a normal interface for new network
1493 * layer addresses.
1494 *
1495 * @param pIf The interface that's sending the frame.
1496 * @param pbFrame The frame.
1497 * @param cbFrame The size of the frame.
1498 * @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
1499 * don't have to repeat the frame parsing in intnetR0TrunkIfSend.
1500 */
1501static void intnetR0IfSnoopAddr(PINTNETIF pIf, uint8_t const *pbFrame, uint32_t cbFrame, uint16_t *pfSgFlags)
1502{
1503 /*
1504 * Fish out the ethertype and look for stuff we can handle.
1505 */
1506 if (cbFrame <= sizeof(RTNETETHERHDR))
1507 return;
1508 cbFrame -= sizeof(RTNETETHERHDR);
1509
1510 uint16_t EtherType = RT_H2BE_U16(((PCRTNETETHERHDR)pbFrame)->EtherType);
1511 switch (EtherType)
1512 {
1513 case RTNET_ETHERTYPE_IPV4:
1514 intnetR0IfSnoopIPv4SourceAddr(pIf, (PCRTNETIPV4)((PCRTNETETHERHDR)pbFrame + 1), cbFrame);
1515 break;
1516#if 0 /** @todo IntNet: implement IPv6 for wireless MAC sharing. */
1517 case RTNET_ETHERTYPE_IPV6:
1518 /** @todo IPv6: Check for ICMPv6. It looks like type 133 (Router solicitation) might
1519 * need to be edited. Check out how NDP works... */
1520 intnetR0IfSnoopIPv6SourceAddr(pIf, (PCINTNETIPV6)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, pfSgFlags);
1521 break;
1522#endif
1523#if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
1524 case RTNET_ETHERTYPE_IPX_1:
1525 case RTNET_ETHERTYPE_IPX_2:
1526 case RTNET_ETHERTYPE_IPX_3:
1527 intnetR0IfSnoopIpxSourceAddr(pIf, (PCINTNETIPX)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, pfSgFlags);
1528 break;
1529#endif
1530 case RTNET_ETHERTYPE_ARP:
1531 intnetR0IfSnoopArpAddr(pIf, (PCRTNETARPIPV4)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, pfSgFlags);
1532 break;
1533 }
1534}
1535
1536
1537#ifdef IN_INTNET_TESTCASE
1538/**
1539 * Reads the next frame in the buffer.
1540 * The caller is responsible for ensuring that there is a valid frame in the buffer.
1541 *
1542 * @returns Size of the frame in bytes.
1543 * @param pBuf The buffer.
1544 * @param pRingBuff The ring buffer to read from.
1545 * @param pvFrame Where to put the frame. The caller is responsible for
1546 * ensuring that there is sufficient space for the frame.
1547 */
1548static unsigned intnetR0RingReadFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, void *pvFrame)
1549{
1550 Assert(pRingBuf->offRead < pBuf->cbBuf);
1551 Assert(pRingBuf->offRead >= pRingBuf->offStart);
1552 Assert(pRingBuf->offRead < pRingBuf->offEnd);
1553 uint32_t offRead = pRingBuf->offRead;
1554 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offRead);
1555 const void *pvFrameIn = INTNETHdrGetFramePtr(pHdr, pBuf);
1556 unsigned cb = pHdr->cbFrame;
1557 memcpy(pvFrame, pvFrameIn, cb);
1558
1559 /* skip the frame */
1560 offRead += pHdr->offFrame + cb;
1561 offRead = RT_ALIGN_32(offRead, sizeof(INTNETHDR));
1562 Assert(offRead <= pRingBuf->offEnd && offRead >= pRingBuf->offStart);
1563 if (offRead >= pRingBuf->offEnd)
1564 offRead = pRingBuf->offStart;
1565 ASMAtomicXchgU32(&pRingBuf->offRead, offRead);
1566 return cb;
1567}
1568#endif /* IN_INTNET_TESTCASE */
1569
1570
1571/**
1572 * Writes a frame packet to the buffer.
1573 *
1574 * @returns VBox status code.
1575 * @param pBuf The buffer.
1576 * @param pRingBuf The ring buffer to read from.
1577 * @param pSG The gather list.
1578 * @param pNewDstMac Set the destination MAC address to the address if specified.
1579 */
1580static int intnetR0RingWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, PCINTNETSG pSG, PCRTMAC pNewDstMac)
1581{
1582 /*
1583 * Validate input.
1584 */
1585 AssertPtr(pBuf);
1586 AssertPtr(pRingBuf);
1587 AssertPtr(pSG);
1588 Assert(pSG->cbTotal >= sizeof(RTMAC) * 2);
1589 uint32_t offWrite = pRingBuf->offWrite;
1590 Assert(offWrite == RT_ALIGN_32(offWrite, sizeof(INTNETHDR)));
1591 uint32_t offRead = pRingBuf->offRead;
1592 Assert(offRead == RT_ALIGN_32(offRead, sizeof(INTNETHDR)));
1593
1594 const uint32_t cb = RT_ALIGN_32(pSG->cbTotal, sizeof(INTNETHDR));
1595 if (offRead <= offWrite)
1596 {
1597 /*
1598 * Try fit it all before the end of the buffer.
1599 */
1600 if (pRingBuf->offEnd - offWrite >= cb + sizeof(INTNETHDR))
1601 {
1602 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
1603 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
1604 pHdr->cbFrame = pSG->cbTotal;
1605 pHdr->offFrame = sizeof(INTNETHDR);
1606
1607 intnetR0SgRead(pSG, pHdr + 1);
1608 if (pNewDstMac)
1609 ((PRTNETETHERHDR)(pHdr + 1))->DstMac = *pNewDstMac;
1610
1611 offWrite += cb + sizeof(INTNETHDR);
1612 Assert(offWrite <= pRingBuf->offEnd && offWrite >= pRingBuf->offStart);
1613 if (offWrite >= pRingBuf->offEnd)
1614 offWrite = pRingBuf->offStart;
1615 Log2(("WriteFrame: offWrite: %#x -> %#x (1)\n", pRingBuf->offWrite, offWrite));
1616 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
1617 return VINF_SUCCESS;
1618 }
1619
1620 /*
1621 * Try fit the frame at the start of the buffer.
1622 * (The header fits before the end of the buffer because of alignment.)
1623 */
1624 AssertMsg(pRingBuf->offEnd - offWrite >= sizeof(INTNETHDR), ("offEnd=%x offWrite=%x\n", pRingBuf->offEnd, offWrite));
1625 if (offRead - pRingBuf->offStart > cb) /* not >= ! */
1626 {
1627 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
1628 void *pvFrameOut = (PINTNETHDR)((uint8_t *)pBuf + pRingBuf->offStart);
1629 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
1630 pHdr->cbFrame = pSG->cbTotal;
1631 pHdr->offFrame = (intptr_t)pvFrameOut - (intptr_t)pHdr;
1632
1633 intnetR0SgRead(pSG, pvFrameOut);
1634 if (pNewDstMac)
1635 ((PRTNETETHERHDR)pvFrameOut)->DstMac = *pNewDstMac;
1636
1637 offWrite = pRingBuf->offStart + cb;
1638 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
1639 Log2(("WriteFrame: offWrite: %#x -> %#x (2)\n", pRingBuf->offWrite, offWrite));
1640 return VINF_SUCCESS;
1641 }
1642 }
1643 /*
1644 * The reader is ahead of the writer, try fit it into that space.
1645 */
1646 else if (offRead - offWrite > cb + sizeof(INTNETHDR)) /* not >= ! */
1647 {
1648 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
1649 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
1650 pHdr->cbFrame = pSG->cbTotal;
1651 pHdr->offFrame = sizeof(INTNETHDR);
1652
1653 intnetR0SgRead(pSG, pHdr + 1);
1654 if (pNewDstMac)
1655 ((PRTNETETHERHDR)(pHdr + 1))->DstMac = *pNewDstMac;
1656
1657 offWrite += cb + sizeof(INTNETHDR);
1658 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
1659 Log2(("WriteFrame: offWrite: %#x -> %#x (3)\n", pRingBuf->offWrite, offWrite));
1660 return VINF_SUCCESS;
1661 }
1662
1663 /* (it didn't fit) */
1664 /** @todo stats */
1665 return VERR_BUFFER_OVERFLOW;
1666}
1667
1668
1669/**
1670 * Sends a frame to a specific interface.
1671 *
1672 * @param pIf The interface.
1673 * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
1674 * @param pSG The gather buffer which data is being sent to the interface.
1675 * @param pNewDstMac Set the destination MAC address to the address if specified.
1676 */
1677static void intnetR0IfSend(PINTNETIF pIf, PINTNETIF pIfSender, PINTNETSG pSG, PCRTMAC pNewDstMac)
1678{
1679// LogFlow(("intnetR0IfSend: pIf=%p:{.hIf=%RX32}\n", pIf, pIf->hIf));
1680 int rc = intnetR0RingWriteFrame(pIf->pIntBuf, &pIf->pIntBuf->Recv, pSG, pNewDstMac);
1681 if (RT_SUCCESS(rc))
1682 {
1683 pIf->cYields = 0;
1684 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatRecvs);
1685 STAM_REL_COUNTER_ADD(&pIf->pIntBuf->cbStatRecv, pSG->cbTotal);
1686 RTSemEventSignal(pIf->Event);
1687 return;
1688 }
1689
1690 Log(("intnetR0IfSend: overflow cb=%d hIf=%RX32\n", pSG->cbTotal, pIf->hIf));
1691
1692#if 0 /* This is bad stuff now as we're blocking while locking down the network.
1693 we really shouldn't delay the network traffic on the host just because
1694 some bugger isn't responding. Will have to deal with this in a different
1695 manner if required. */
1696 /*
1697 * Retry a few times, yielding the CPU in between.
1698 * But don't let a unresponsive VM harm performance, so give up after a couple of tries.
1699 */
1700 if ( pIf->fActive
1701 && pIf->cYields < 100)
1702 {
1703 unsigned cYields = 10;
1704#else
1705 /*
1706 * Scheduling hack, for unicore machines primarily.
1707 */
1708 if ( pIf->fActive
1709 && pIf->cYields < 4 /* just twice */
1710 && pIfSender /* but not if it's from the trunk */)
1711 {
1712 unsigned cYields = 2;
1713#endif
1714 while (--cYields > 0)
1715 {
1716 RTSemEventSignal(pIf->Event);
1717 RTThreadYield();
1718 rc = intnetR0RingWriteFrame(pIf->pIntBuf, &pIf->pIntBuf->Recv, pSG, pNewDstMac);
1719 if (RT_SUCCESS(rc))
1720 {
1721 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatYieldsOk);
1722 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatRecvs);
1723 STAM_REL_COUNTER_ADD(&pIf->pIntBuf->cbStatRecv, pSG->cbTotal);
1724 RTSemEventSignal(pIf->Event);
1725 return;
1726 }
1727 pIf->cYields++;
1728 }
1729 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatYieldsNok);
1730 }
1731
1732 /* ok, the frame is lost. */
1733 STAM_REL_COUNTER_INC(&pIf->pIntBuf->cStatLost);
1734 RTSemEventSignal(pIf->Event);
1735}
1736
1737
1738/**
1739 * Sends a frame down the trunk.
1740 *
1741 * The caller must own the network mutex, might be abandond temporarily.
1742 * The fTrunkLock parameter indicates whether the trunk lock is held.
1743 *
1744 * @param pThis The trunk.
1745 * @param pNetwork The network the frame is being sent to.
1746 * @param pIfSender The IF sending the frame. Used for MAC address checks in shared MAC mode.
1747 * @param fDst The destination flags.
1748 * @param pSG Pointer to the gather list.
1749 * @param fTrunkLocked Whether the caller owns the out-bound trunk lock.
1750 */
1751static void intnetR0TrunkIfSend(PINTNETTRUNKIF pThis, PINTNETNETWORK pNetwork, PINTNETIF pIfSender,
1752 uint32_t fDst, PINTNETSG pSG, bool fTrunkLocked)
1753{
1754 /*
1755 * Quick sanity check.
1756 */
1757 AssertPtr(pThis);
1758 AssertPtr(pNetwork);
1759 AssertPtr(pSG);
1760 Assert(fDst);
1761 AssertReturnVoid(pThis->pIfPort);
1762
1763 /*
1764 * Edit the frame if we're sharing the MAC address with the host on the wire.
1765 *
1766 * If the frame is headed for both the host and the wire, we'll have to send
1767 * it to the host before making any modifications, and force the OS specific
1768 * backend to copy it. We do this by marking it as TEMP (which is always the
1769 * case right now).
1770 */
1771 if ( (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
1772 && (fDst & INTNETTRUNKDIR_WIRE))
1773 {
1774 /* Dispatch it to the host before making changes. */
1775 if (fDst & INTNETTRUNKDIR_HOST)
1776 {
1777 Assert(pSG->fFlags & INTNETSG_FLAGS_TEMP); /* make sure copy is forced */
1778 intnetR0TrunkIfSend(pThis, pNetwork, pIfSender, INTNETTRUNKDIR_HOST, pSG, fTrunkLocked);
1779 fDst &= ~INTNETTRUNKDIR_HOST;
1780 }
1781
1782 /* ASSUME frame from INTNETR0IfSend! */
1783 AssertReturnVoid(pSG->cSegsUsed == 1);
1784 AssertReturnVoid(pSG->cbTotal >= sizeof(RTNETETHERHDR));
1785 AssertReturnVoid(fTrunkLocked);
1786 AssertReturnVoid(pIfSender);
1787 PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)pSG->aSegs[0].pv;
1788
1789 /*
1790 * Get the host mac address and update the ethernet header.
1791 *
1792 * The reason for caching it in the trunk structure is because
1793 * we cannot take the trunk out-bound semaphore when we make
1794 * edits in the intnetR0TrunkIfPortRecv path.
1795 */
1796 pThis->pIfPort->pfnGetMacAddress(pThis->pIfPort, &pThis->CachedMac);
1797 if (!memcmp(&pEthHdr->SrcMac, &pIfSender->Mac, sizeof(RTMAC)))
1798 pEthHdr->SrcMac = pThis->CachedMac;
1799
1800 /*
1801 * Deal with tags from the snooping phase.
1802 */
1803 if (pSG->fFlags & INTNETSG_FLAGS_ARP_IPV4)
1804 {
1805 /*
1806 * APR IPv4: replace hardware (MAC) addresses because these end up
1807 * in ARP caches. So, if we don't the other machiens will
1808 * send the packets to the MAC address of the guest
1809 * instead of the one of the host, which won't work on
1810 * wireless of course...
1811 */
1812 PRTNETARPIPV4 pArp = (PRTNETARPIPV4)(pEthHdr + 1);
1813 if (!memcmp(&pArp->ar_sha, &pIfSender->Mac, sizeof(RTMAC)))
1814 {
1815 Log6(("tw: ar_sha %.6Rhxs -> %.6Rhxs\n", &pArp->ar_sha, &pThis->CachedMac));
1816 pArp->ar_sha = pThis->CachedMac;
1817 }
1818 if (!memcmp(&pArp->ar_tha, &pIfSender->Mac, sizeof(RTMAC))) /* just in case... */
1819 {
1820 Log6(("tw: ar_tha %.6Rhxs -> %.6Rhxs\n", &pArp->ar_tha, &pThis->CachedMac));
1821 pArp->ar_tha = pThis->CachedMac;
1822 }
1823 }
1824 //else if (pSG->fFlags & INTNETSG_FLAGS_ICMPV6_NDP)
1825 //{ /// @todo move the editing into a different function
1826 //}
1827 }
1828
1829 /*
1830 * Temporarily leave the network lock while transmitting the frame.
1831 *
1832 * Note that we're relying on the out-bound lock to serialize threads down
1833 * in INTNETR0IfSend. It's theoretically possible for there to be race now
1834 * because I didn't implement async SG handling yet. Which is why we currently
1835 * require the trunk to be locked, well, one of the reasons.
1836 *
1837 * Another reason is that the intnetR0NetworkSendUnicast code may have to
1838 * call into the trunk interface component to do package switching.
1839 */
1840 AssertReturnVoid(fTrunkLocked); /* to be removed. */
1841
1842 int rc;
1843 if ( fTrunkLocked
1844 || intnetR0TrunkIfRetain(pThis))
1845 {
1846 rc = RTSemFastMutexRelease(pNetwork->FastMutex);
1847 AssertRC(rc);
1848 if (RT_SUCCESS(rc))
1849 {
1850 if ( fTrunkLocked
1851 || intnetR0TrunkIfOutLock(pThis))
1852 {
1853 rc = pThis->pIfPort->pfnXmit(pThis->pIfPort, pSG, fDst);
1854
1855 if (!fTrunkLocked)
1856 intnetR0TrunkIfOutUnlock(pThis);
1857 }
1858 else
1859 {
1860 AssertFailed();
1861 rc = VERR_SEM_DESTROYED;
1862 }
1863
1864 int rc2 = RTSemFastMutexRequest(pNetwork->FastMutex);
1865 AssertRC(rc2);
1866 }
1867
1868 if (!fTrunkLocked)
1869 intnetR0TrunkIfRelease(pThis);
1870 }
1871 else
1872 {
1873 AssertFailed();
1874 rc = VERR_SEM_DESTROYED;
1875 }
1876
1877 /** @todo failure statistics? */
1878 Log2(("intnetR0TrunkIfSend: %Rrc fDst=%d\n", rc, fDst));
1879}
1880
1881
1882/**
1883 * Edits an ARP packet arriving from the wire via the trunk connection.
1884 *
1885 * @param pNetwork The network the frame is being sent to.
1886 * @param pSG Pointer to the gather list for the frame.
1887 * The flags and data content may be updated.
1888 * @param pEthHdr Pointer to the ethernet header. This may also be
1889 * updated if it's a unicast...
1890 */
1891static void intnetR0NetworkEditArpFromWire(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
1892{
1893 /*
1894 * Check the minimum size and get a linear copy of the thing to work on,
1895 * using the temporary buffer if necessary.
1896 */
1897 if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4)))
1898 return;
1899 PRTNETARPIPV4 pArpIPv4 = (PRTNETARPIPV4)((uint8_t *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR));
1900 if ( pSG->cSegsUsed != 1
1901 && pSG->aSegs[0].cb < sizeof(RTNETETHERHDR) + sizeof(RTNETARPIPV4))
1902 {
1903 Log6(("fw: Copying ARP pkt %u\n", sizeof(RTNETARPIPV4)));
1904 if (!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR), sizeof(RTNETARPIPV4), pNetwork->pbTmp))
1905 return;
1906 pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
1907 pArpIPv4 = (PRTNETARPIPV4)pNetwork->pbTmp;
1908 }
1909
1910 /*
1911 * Ignore packets which doesn't interest us or we perceive as malformed.
1912 */
1913 if (RT_UNLIKELY( pArpIPv4->Hdr.ar_hlen != sizeof(RTMAC)
1914 || pArpIPv4->Hdr.ar_plen != sizeof(RTNETADDRIPV4)
1915 || pArpIPv4->Hdr.ar_htype != RT_H2BE_U16(RTNET_ARP_ETHER)
1916 || pArpIPv4->Hdr.ar_ptype != RT_H2BE_U16(RTNET_ETHERTYPE_IPV4)))
1917 return;
1918 uint16_t ar_oper = RT_H2BE_U16(pArpIPv4->Hdr.ar_oper);
1919 if (RT_UNLIKELY( ar_oper != RTNET_ARPOP_REQUEST
1920 && ar_oper != RTNET_ARPOP_REPLY))
1921 {
1922 Log6(("ar_oper=%#x\n", ar_oper));
1923 return;
1924 }
1925
1926 /* Tag it as ARP IPv4. */
1927 pSG->fFlags |= INTNETSG_FLAGS_ARP_IPV4;
1928
1929 /*
1930 * The thing we're interested in here is a reply to a query made by a guest
1931 * since we modified the MAC in the initial request the guest made.
1932 */
1933 if ( ar_oper == RTNET_ARPOP_REPLY
1934 && !memcmp(&pArpIPv4->ar_tha, &pNetwork->pTrunkIF->CachedMac, sizeof(RTMAC)))
1935 {
1936 PINTNETIF pIf = intnetR0NetworkAddrCacheLookupIf(pNetwork, (PCRTNETADDRU)&pArpIPv4->ar_tpa,
1937 kIntNetAddrType_IPv4, sizeof(pArpIPv4->ar_tpa));
1938 if (pIf)
1939 {
1940 Log6(("fw: ar_tha %.6Rhxs -> %.6Rhxs\n", &pArpIPv4->ar_tha, &pIf->Mac));
1941 pArpIPv4->ar_tha = pIf->Mac;
1942 if (!memcmp(&pEthHdr->DstMac, &pNetwork->pTrunkIF->CachedMac, sizeof(RTMAC)))
1943 {
1944 Log6(("fw: DstMac %.6Rhxs -> %.6Rhxs\n", &pEthHdr->DstMac, &pIf->Mac));
1945 pEthHdr->DstMac = pIf->Mac;
1946 if ((void *)pEthHdr != pSG->aSegs[0].pv)
1947 intnetR0SgWritePart(pSG, RT_OFFSETOF(RTNETETHERHDR, DstMac), sizeof(RTMAC), &pIf->Mac);
1948 }
1949
1950 /* Write back the packet if we've been making changes to a buffered copy. */
1951 if (pSG->fFlags & INTNETSG_FLAGS_PKT_CP_IN_TMP)
1952 intnetR0SgWritePart(pSG, sizeof(RTNETETHERHDR), sizeof(PRTNETARPIPV4), pArpIPv4);
1953 }
1954 }
1955}
1956
1957
1958/**
1959 * Sends a broadcast frame.
1960 *
1961 * The caller must own the network mutex, might be abandond temporarily.
1962 * When pIfSender is not NULL, the caller must also own the trunk semaphore.
1963 *
1964 * @returns true if it's addressed to someone on the network, otherwise false.
1965 * @param pNetwork The network the frame is being sent to.
1966 * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
1967 * @param fSrc The source flags. This 0 if it's not from the trunk.
1968 * @param pSG Pointer to the gather list.
1969 * @param fTrunkLocked Whether the caller owns the out-bound trunk lock.
1970 * @param pEthHdr Pointer to the ethernet header.
1971 */
1972static bool intnetR0NetworkSendBroadcast(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc,
1973 PINTNETSG pSG, bool fTrunkLocked, PRTNETETHERHDR pEthHdr)
1974{
1975 /*
1976 * Check for ARP packets from the wire since we'll have to make
1977 * modification to them if we're sharing the MAC address with the host.
1978 */
1979 if ( (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
1980 && (fSrc & INTNETTRUNKDIR_WIRE)
1981 && RT_BE2H_U16(pEthHdr->EtherType) == RTNET_ETHERTYPE_ARP)
1982 intnetR0NetworkEditArpFromWire(pNetwork, pSG, pEthHdr);
1983
1984 /*
1985 * This is a broadcast or multicast address. For the present we treat those
1986 * two as the same - investigating multicast is left for later.
1987 *
1988 * Write the packet to all the interfaces and signal them.
1989 */
1990 for (PINTNETIF pIf = pNetwork->pIFs; pIf; pIf = pIf->pNext)
1991 if (pIf != pIfSender)
1992 intnetR0IfSend(pIf, pIfSender, pSG, NULL);
1993
1994 /*
1995 * Unless the trunk is the origin, broadcast it to both the wire
1996 * and the host as well.
1997 */
1998 PINTNETTRUNKIF pTrunkIf = pNetwork->pTrunkIF;
1999 if ( pIfSender
2000 && pTrunkIf)
2001 intnetR0TrunkIfSend(pTrunkIf, pNetwork, pIfSender, INTNETTRUNKDIR_HOST | INTNETTRUNKDIR_WIRE, pSG, fTrunkLocked);
2002
2003 /*
2004 * Snoop address info from packet orginating from the trunk connection.
2005 */
2006 else if ( (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
2007 && !pIfSender)
2008 {
2009#ifdef INTNET_WITH_DHCP_SNOOPING
2010 uint16_t EtherType = RT_BE2H_U16(pEthHdr->EtherType);
2011 if ( ( EtherType == RTNET_ETHERTYPE_IPV4 /* for DHCP */
2012 && pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN)
2013 || (pSG->fFlags & (INTNETSG_FLAGS_ARP_IPV4)) )
2014 intnetR0TrunkIfSnoopAddr(pNetwork, pSG, EtherType);
2015#else
2016 if (pSG->fFlags & (INTNETSG_FLAGS_ARP_IPV4))
2017 intnetR0TrunkIfSnoopArp(pNetwork, pSG);
2018#endif
2019 }
2020
2021 return false; /* broadcast frames are never dropped */
2022}
2023
2024
2025/**
2026 * Sends a multicast frame.
2027 *
2028 * The caller must own the network mutex, might be abandond temporarily.
2029 *
2030 * @returns true if it's addressed to someone on the network, otherwise false.
2031 * @param pNetwork The network the frame is being sent to.
2032 * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
2033 * @param fSrc The source flags. This 0 if it's not from the trunk.
2034 * @param pSG Pointer to the gather list.
2035 * @param fTrunkLocked Whether the caller owns the out-bound trunk lock.
2036 * @param pEthHdr Pointer to the ethernet header.
2037 */
2038static bool intnetR0NetworkSendMulticast(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc, PINTNETSG pSG, bool fTrunkLocked, PRTNETETHERHDR pEthHdr)
2039{
2040 /** @todo implement multicast */
2041 return intnetR0NetworkSendBroadcast(pNetwork, pIfSender, fSrc, pSG, fTrunkLocked, pEthHdr);
2042}
2043
2044
2045/**
2046 * Sends a unicast frame using the network layer address instead
2047 * of the link layer one.
2048 *
2049 * The caller must own the network mutex, might be abandond temporarily.
2050 *
2051 * @returns true if it's addressed to someone on the network, otherwise false.
2052 * @param pNetwork The network the frame is being sent to.
2053 * @param pSG Pointer to the gather list.
2054 * @param fTrunkLocked Whether the caller owns the out-bound trunk lock.
2055 * @param pEthHdr Pointer to the ethernet header.
2056 */
2057static bool intnetR0NetworkSendUnicastWithSharedMac(PINTNETNETWORK pNetwork, PINTNETSG pSG, bool fTrunkLocked, PRTNETETHERHDR pEthHdr)
2058{
2059 /*
2060 * Extract the network address from the packet.
2061 */
2062 RTNETADDRU Addr;
2063 INTNETADDRTYPE enmAddrType;
2064 uint8_t cbAddr;
2065 switch (RT_BE2H_U16(pEthHdr->EtherType))
2066 {
2067 case RTNET_ETHERTYPE_IPV4:
2068 if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_dst), sizeof(Addr.IPv4), &Addr)))
2069 {
2070 Log(("intnetshareduni: failed to read ip_dst! cbTotal=%#x\n", pSG->cbTotal));
2071 return false;
2072 }
2073 enmAddrType = kIntNetAddrType_IPv4;
2074 cbAddr = sizeof(Addr.IPv4);
2075 Log6(("intnetshareduni: IPv4 %d.%d.%d.%d\n", Addr.au8[0], Addr.au8[1], Addr.au8[2], Addr.au8[3]));
2076 break;
2077
2078#if 0 /** @todo IntNet: implement IPv6 for wireless MAC sharing. */
2079 case RTNET_ETHERTYPE_IPV6
2080 if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV6, ip6_dst), sizeof(Addr.IPv6), &Addr)))
2081 {
2082 Log(("intnetshareduni: failed to read ip6_dst! cbTotal=%#x\n", pSG->cbTotal));
2083 return false;
2084 }
2085 enmAddrType = kIntNetAddrType_IPv6;
2086 cbAddr = sizeof(Addr.IPv6);
2087 break;
2088#endif
2089#if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
2090 case RTNET_ETHERTYPE_IPX_1:
2091 case RTNET_ETHERTYPE_IPX_2:
2092 case RTNET_ETHERTYPE_IPX_3:
2093 if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPX, ipx_dstnet), sizeof(Addr.IPX), &Addr)))
2094 {
2095 Log(("intnetshareduni: failed to read ipx_dstnet! cbTotal=%#x\n", pSG->cbTotal));
2096 return false;
2097 }
2098 enmAddrType = kIntNetAddrType_IPX;
2099 cbAddr = sizeof(Addr.IPX);
2100 break;
2101#endif
2102
2103 /*
2104 * Treat ARP is broadcast (it shouldn't end up here normally,
2105 * so it goes last in the switch).
2106 */
2107 case RTNET_ETHERTYPE_ARP:
2108 Log6(("intnetshareduni: ARP\n"));
2109 /** @todo revisit this broadcasting of unicast ARP frames! */
2110 return intnetR0NetworkSendBroadcast(pNetwork, NULL, INTNETTRUNKDIR_WIRE, pSG, fTrunkLocked, pEthHdr);
2111
2112 /*
2113 * Unknown packets are sent do all interfaces that are in promiscuous mode.
2114 */
2115 default:
2116 {
2117 Log6(("intnetshareduni: unknown ethertype=%#x\n", RT_BE2H_U16(pEthHdr->EtherType)));
2118 if (!(pNetwork->fFlags & (INTNET_OPEN_FLAGS_IGNORE_PROMISC | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC)))
2119 {
2120 for (PINTNETIF pIf = pNetwork->pIFs; pIf; pIf = pIf->pNext)
2121 if (pIf->fPromiscuous)
2122 {
2123 Log2(("Dst=%.6Rhxs => %.6Rhxs\n", &pEthHdr->DstMac, &pIf->Mac));
2124 intnetR0IfSend(pIf, NULL, pSG, NULL);
2125 }
2126 }
2127 return false;
2128 }
2129 }
2130
2131 /*
2132 * Send it to interfaces with matching network addresses.
2133 */
2134 bool fExactIntNetRecipient = false;
2135 for (PINTNETIF pIf = pNetwork->pIFs; pIf; pIf = pIf->pNext)
2136 {
2137 bool fIt = intnetR0IfAddrCacheLookup(&pIf->aAddrCache[enmAddrType], &Addr, cbAddr) >= 0;
2138 if ( fIt
2139 || ( pIf->fPromiscuous
2140 && !(pNetwork->fFlags & (INTNET_OPEN_FLAGS_IGNORE_PROMISC | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC))))
2141 {
2142 Log2(("Dst=%.6Rhxs => %.6Rhxs\n", &pEthHdr->DstMac, &pIf->Mac));
2143 fExactIntNetRecipient |= fIt;
2144 intnetR0IfSend(pIf, NULL, pSG, fIt ? &pIf->Mac : NULL);
2145 }
2146 }
2147
2148#ifdef INTNET_WITH_DHCP_SNOOPING
2149 /*
2150 * Perform DHCP snooping.
2151 */
2152 if ( enmAddrType == kIntNetAddrType_IPv4
2153 && pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN)
2154 intnetR0TrunkIfSnoopAddr(pNetwork, pSG, RT_BE2H_U16(pEthHdr->EtherType));
2155#endif /* INTNET_WITH_DHCP_SNOOPING */
2156
2157 return fExactIntNetRecipient;
2158}
2159
2160
2161/**
2162 * Sends a unicast frame.
2163 *
2164 * The caller must own the network mutex, might be abandond temporarily.
2165 *
2166 * @returns true if it's addressed to someone on the network, otherwise false.
2167 * @param pNetwork The network the frame is being sent to.
2168 * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
2169 * @param fSrc The source flags. This 0 if it's not from the trunk.
2170 * @param pSG Pointer to the gather list.
2171 * @param fTrunkLocked Whether the caller owns the out-bound trunk lock.
2172 * @param pEthHdr Pointer to the ethernet header.
2173 */
2174static bool intnetR0NetworkSendUnicast(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc, PINTNETSG pSG, bool fTrunkLocked, PCRTNETETHERHDR pEthHdr)
2175{
2176 /*
2177 * Only send to the interfaces with matching a MAC address.
2178 */
2179 bool fExactIntNetRecipient = false;
2180 for (PINTNETIF pIf = pNetwork->pIFs; pIf; pIf = pIf->pNext)
2181 {
2182 bool fIt = false;
2183 if ( ( !pIf->fMacSet
2184 || (fIt = !memcmp(&pIf->Mac, &pEthHdr->DstMac, sizeof(pIf->Mac))) )
2185 || ( pIf->fPromiscuous
2186 && !(pNetwork->fFlags & (INTNET_OPEN_FLAGS_IGNORE_PROMISC | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC))
2187 && pIf != pIfSender /* promiscuous mode: omit the sender */))
2188 {
2189 Log2(("Dst=%.6Rhxs => %.6Rhxs\n", &pEthHdr->DstMac, &pIf->Mac));
2190 fExactIntNetRecipient |= fIt;
2191 intnetR0IfSend(pIf, pIfSender, pSG, NULL);
2192 }
2193 }
2194
2195 /*
2196 * Send it to the trunk?
2197 * If we didn't find the recipient on the internal network the
2198 * frame will hit the wire.
2199 */
2200 uint32_t fDst = 0;
2201 PINTNETTRUNKIF pTrunkIf = pNetwork->pTrunkIF;
2202 if ( pIfSender
2203 && pTrunkIf
2204 && pTrunkIf->pIfPort)
2205 {
2206 Assert(!fSrc);
2207
2208 /* promiscuous checks first as they are cheaper than pfnIsHostMac. */
2209 if ( pTrunkIf->fPromiscuousWire
2210 && !(pNetwork->fFlags & (INTNET_OPEN_FLAGS_IGNORE_PROMISC | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC | INTNET_OPEN_FLAGS_IGNORE_PROMISC_TRUNK_WIRE | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC_TRUNK_WIRE)) )
2211 fDst |= INTNETTRUNKDIR_WIRE;
2212 if ( !(pNetwork->fFlags & (INTNET_OPEN_FLAGS_IGNORE_PROMISC | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC | INTNET_OPEN_FLAGS_IGNORE_PROMISC_TRUNK_HOST | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC_TRUNK_HOST))
2213 || pTrunkIf->pIfPort->pfnIsPromiscuous(pTrunkIf->pIfPort) )
2214 fDst |= INTNETTRUNKDIR_HOST;
2215
2216 if ( fDst != (INTNETTRUNKDIR_HOST | INTNETTRUNKDIR_WIRE)
2217 && !fExactIntNetRecipient /* if you have duplicate mac addresses, you're screwed. */ )
2218 {
2219 if (pTrunkIf->pIfPort->pfnIsHostMac(pTrunkIf->pIfPort, &pEthHdr->DstMac))
2220 fDst |= INTNETTRUNKDIR_HOST;
2221 else
2222 fDst |= INTNETTRUNKDIR_WIRE;
2223 }
2224
2225 if (fDst)
2226 intnetR0TrunkIfSend(pTrunkIf, pNetwork, pIfSender, fDst, pSG, fTrunkLocked);
2227 }
2228
2229 /* log it */
2230 if ( !fExactIntNetRecipient
2231 && !fDst
2232 && ( (pEthHdr->DstMac.au8[0] == 0x08 && pEthHdr->DstMac.au8[1] == 0x00 && pEthHdr->DstMac.au8[2] == 0x27)
2233 || (pEthHdr->SrcMac.au8[0] == 0x08 && pEthHdr->SrcMac.au8[1] == 0x00 && pEthHdr->SrcMac.au8[2] == 0x27)))
2234 Log2(("Dst=%.6Rhxs ??\n", &pEthHdr->DstMac));
2235
2236 return fExactIntNetRecipient;
2237}
2238
2239
2240/**
2241 * Sends a frame.
2242 *
2243 * This function will distribute the frame to the interfaces it is addressed to.
2244 * It will also update the MAC address of the sender.
2245 *
2246 * The caller must own the network mutex.
2247 *
2248 * @returns true if it's addressed to someone on the network, otherwise false.
2249 * @param pNetwork The network the frame is being sent to.
2250 * @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
2251 * @param fSrc The source flags. This 0 if it's not from the trunk.
2252 * @param pSG Pointer to the gather list.
2253 * @param fTrunkLocked Whether the caller owns the out-bound trunk lock.
2254 */
2255static bool intnetR0NetworkSend(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc, PINTNETSG pSG, bool fTrunkLocked)
2256{
2257 bool fRc = false;
2258
2259 /*
2260 * Assert reality.
2261 */
2262 AssertPtr(pNetwork);
2263 AssertPtrNull(pIfSender);
2264 Assert(pIfSender ? fSrc == 0 : fSrc != 0);
2265 Assert(!pIfSender || pNetwork == pIfSender->pNetwork);
2266 AssertPtr(pSG);
2267 Assert(pSG->cSegsUsed >= 1);
2268 Assert(pSG->cSegsUsed <= pSG->cSegsAlloc);
2269 if (pSG->cbTotal < sizeof(RTNETETHERHDR))
2270 return fRc;
2271
2272 /*
2273 * Send statistics.
2274 */
2275 if (pIfSender)
2276 {
2277 STAM_REL_COUNTER_INC(&pIfSender->pIntBuf->cStatSends);
2278 STAM_REL_COUNTER_ADD(&pIfSender->pIntBuf->cbStatSend, pSG->cbTotal);
2279 }
2280
2281 /*
2282 * Get the ethernet header (might theoretically involve multiple segments).
2283 */
2284 RTNETETHERHDR EthHdr;
2285 if (pSG->aSegs[0].cb >= sizeof(EthHdr))
2286 EthHdr = *(PCRTNETETHERHDR)pSG->aSegs[0].pv;
2287 else if (!intnetR0SgReadPart(pSG, 0, sizeof(EthHdr), &EthHdr))
2288 return false;
2289 if ( (EthHdr.DstMac.au8[0] == 0x08 && EthHdr.DstMac.au8[1] == 0x00 && EthHdr.DstMac.au8[2] == 0x27)
2290 || (EthHdr.SrcMac.au8[0] == 0x08 && EthHdr.SrcMac.au8[1] == 0x00 && EthHdr.SrcMac.au8[2] == 0x27)
2291 || (EthHdr.DstMac.au8[0] == 0x00 && EthHdr.DstMac.au8[1] == 0x16 && EthHdr.DstMac.au8[2] == 0xcb)
2292 || (EthHdr.SrcMac.au8[0] == 0x00 && EthHdr.SrcMac.au8[1] == 0x16 && EthHdr.SrcMac.au8[2] == 0xcb)
2293 || EthHdr.DstMac.au8[0] == 0xff
2294 || EthHdr.SrcMac.au8[0] == 0xff)
2295 Log2(("D=%.6Rhxs S=%.6Rhxs T=%04x f=%x z=%x\n",
2296 &EthHdr.DstMac, &EthHdr.SrcMac, RT_BE2H_U16(EthHdr.EtherType), fSrc, pSG->cbTotal));
2297
2298 /*
2299 * Inspect the header updating the mac address of the sender in the process.
2300 */
2301 if ( pIfSender
2302 && memcmp(&EthHdr.SrcMac, &pIfSender->Mac, sizeof(pIfSender->Mac)))
2303 {
2304 /** @todo stats */
2305 Log2(("IF MAC: %.6Rhxs -> %.6Rhxs\n", &pIfSender->Mac, &EthHdr.SrcMac));
2306 pIfSender->Mac = EthHdr.SrcMac;
2307 pIfSender->fMacSet = true;
2308 }
2309
2310 /*
2311 * Distribute the frame.
2312 */
2313 if ( EthHdr.DstMac.au16[0] == 0xffff /* broadcast address. */
2314 && EthHdr.DstMac.au16[1] == 0xffff
2315 && EthHdr.DstMac.au16[2] == 0xffff)
2316 fRc = intnetR0NetworkSendBroadcast(pNetwork, pIfSender, fSrc, pSG, fTrunkLocked, &EthHdr);
2317 else if (RT_UNLIKELY(EthHdr.DstMac.au8[0] & 1)) /* multicast address */
2318 fRc = intnetR0NetworkSendMulticast(pNetwork, pIfSender, fSrc, pSG, fTrunkLocked, &EthHdr);
2319 else if ( !(pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
2320 || !(fSrc & INTNETTRUNKDIR_WIRE))
2321 fRc = intnetR0NetworkSendUnicast(pNetwork, pIfSender, fSrc, pSG, fTrunkLocked, &EthHdr);
2322 else
2323 fRc = intnetR0NetworkSendUnicastWithSharedMac(pNetwork, pSG, fTrunkLocked, &EthHdr);
2324 return fRc;
2325}
2326
2327
2328/**
2329 * Sends one or more frames.
2330 *
2331 * The function will first the frame which is passed as the optional
2332 * arguments pvFrame and cbFrame. These are optional since it also
2333 * possible to chain together one or more frames in the send buffer
2334 * which the function will process after considering it's arguments.
2335 *
2336 * @returns VBox status code.
2337 * @param pIntNet The instance data.
2338 * @param hIf The interface handle.
2339 * @param pSession The caller's session.
2340 * @param pvFrame Pointer to the frame. Optional, please don't use.
2341 * @param cbFrame Size of the frame. Optional, please don't use.
2342 */
2343INTNETR0DECL(int) INTNETR0IfSend(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, const void *pvFrame, unsigned cbFrame)
2344{
2345 Log5(("INTNETR0IfSend: pIntNet=%p hIf=%RX32 pvFrame=%p cbFrame=%u\n", pIntNet, hIf, pvFrame, cbFrame));
2346
2347 /*
2348 * Validate input and translate the handle.
2349 */
2350 AssertReturn(pIntNet, VERR_INVALID_PARAMETER);
2351 if (pvFrame && cbFrame)
2352 {
2353 AssertReturn(cbFrame < 0x8000, VERR_INVALID_PARAMETER);
2354 AssertPtrReturn(pvFrame, VERR_INVALID_PARAMETER);
2355 AssertPtrReturn((uint8_t *)pvFrame + cbFrame - 1, VERR_INVALID_PARAMETER);
2356
2357 /* This is the better place to crash, probe the buffer. */
2358 ASMProbeReadBuffer(pvFrame, cbFrame);
2359 }
2360 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2361 if (!pIf)
2362 return VERR_INVALID_HANDLE;
2363
2364 /*
2365 * Lock the network. If there is a trunk retain it and grab its
2366 * out-bound lock (this requires leaving the network lock first).
2367 * Grabbing the out-bound lock here simplifies things quite a bit
2368 * later on, so while this is excessive and a bit expensive it's
2369 * not worth caring about right now.
2370 */
2371 PINTNETNETWORK pNetwork = pIf->pNetwork;
2372 int rc = RTSemFastMutexRequest(pNetwork->FastMutex);
2373 if (RT_FAILURE(rc))
2374 {
2375 intnetR0IfRelease(pIf, pSession);
2376 return rc;
2377 }
2378 PINTNETTRUNKIF pTrunkIf = intnetR0TrunkIfRetain(pNetwork->pTrunkIF);
2379 if (pTrunkIf)
2380 {
2381 RTSemFastMutexRelease(pIf->pNetwork->FastMutex);
2382
2383 if (!intnetR0TrunkIfOutLock(pTrunkIf))
2384 {
2385 intnetR0TrunkIfRelease(pTrunkIf);
2386 intnetR0IfRelease(pIf, pSession);
2387 return VERR_SEM_DESTROYED;
2388 }
2389
2390 rc = RTSemFastMutexRequest(pNetwork->FastMutex);
2391 if (RT_FAILURE(rc))
2392 {
2393 intnetR0TrunkIfOutUnlock(pTrunkIf);
2394 intnetR0TrunkIfRelease(pTrunkIf);
2395 intnetR0IfRelease(pIf, pSession);
2396 return rc;
2397 }
2398 }
2399
2400 INTNETSG Sg; /** @todo this will have to be changed if we're going to use async sending
2401 * with buffer sharing for some OS or service. Darwin copies everything so
2402 * I won't bother allocating and managing SGs rigth now. Sorry. */
2403
2404 /*
2405 * Process the argument.
2406 */
2407 if (pvFrame && cbFrame)
2408 {
2409 intnetR0SgInitTemp(&Sg, (void *)pvFrame, cbFrame);
2410 if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
2411 intnetR0IfSnoopAddr(pIf, (uint8_t *)pvFrame, cbFrame, (uint16_t *)&Sg.fFlags);
2412 intnetR0NetworkSend(pNetwork, pIf, 0, &Sg, !!pTrunkIf);
2413 }
2414
2415 /*
2416 * Process the send buffer.
2417 */
2418 while (pIf->pIntBuf->Send.offRead != pIf->pIntBuf->Send.offWrite)
2419 {
2420 /* Send the frame if the type is sane. */
2421 PINTNETHDR pHdr = (PINTNETHDR)((uintptr_t)pIf->pIntBuf + pIf->pIntBuf->Send.offRead);
2422 if (pHdr->u16Type == INTNETHDR_TYPE_FRAME)
2423 {
2424 void *pvCurFrame = INTNETHdrGetFramePtr(pHdr, pIf->pIntBuf);
2425 if (pvCurFrame)
2426 {
2427 intnetR0SgInitTemp(&Sg, pvCurFrame, pHdr->cbFrame);
2428 if (pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
2429 intnetR0IfSnoopAddr(pIf, (uint8_t *)pvCurFrame, pHdr->cbFrame, (uint16_t *)&Sg.fFlags);
2430 intnetR0NetworkSend(pNetwork, pIf, 0, &Sg, !!pTrunkIf);
2431 }
2432 }
2433 /* else: ignore the frame */
2434
2435 /* Skip to the next frame. */
2436 INTNETRingSkipFrame(pIf->pIntBuf, &pIf->pIntBuf->Send);
2437 }
2438
2439 /*
2440 * Release the semaphore(s) and release references.
2441 */
2442 rc = RTSemFastMutexRelease(pNetwork->FastMutex);
2443 if (pTrunkIf)
2444 {
2445 intnetR0TrunkIfOutUnlock(pTrunkIf);
2446 intnetR0TrunkIfRelease(pTrunkIf);
2447 }
2448
2449 intnetR0IfRelease(pIf, pSession);
2450 return rc;
2451}
2452
2453
2454/**
2455 * VMMR0 request wrapper for INTNETR0IfSend.
2456 *
2457 * @returns see INTNETR0IfSend.
2458 * @param pIntNet The internal networking instance.
2459 * @param pSession The caller's session.
2460 * @param pReq The request packet.
2461 */
2462INTNETR0DECL(int) INTNETR0IfSendReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSENDREQ pReq)
2463{
2464 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
2465 return VERR_INVALID_PARAMETER;
2466 return INTNETR0IfSend(pIntNet, pReq->hIf, pSession, NULL, 0);
2467}
2468
2469
2470/**
2471 * Maps the default buffer into ring 3.
2472 *
2473 * @returns VBox status code.
2474 * @param pIntNet The instance data.
2475 * @param hIf The interface handle.
2476 * @param pSession The caller's session.
2477 * @param ppRing3Buf Where to store the address of the ring-3 mapping.
2478 */
2479INTNETR0DECL(int) INTNETR0IfGetRing3Buffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, R3PTRTYPE(PINTNETBUF) *ppRing3Buf)
2480{
2481 LogFlow(("INTNETR0IfGetRing3Buffer: pIntNet=%p hIf=%RX32 ppRing3Buf=%p\n", pIntNet, hIf, ppRing3Buf));
2482
2483 /*
2484 * Validate input.
2485 */
2486 AssertReturn(pIntNet, VERR_INVALID_PARAMETER);
2487 AssertPtrReturn(ppRing3Buf, VERR_INVALID_PARAMETER);
2488 *ppRing3Buf = 0;
2489 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2490 if (!pIf)
2491 return VERR_INVALID_HANDLE;
2492
2493 /*
2494 * ASSUMES that only the process that created an interface can use it.
2495 * ASSUMES that we created the ring-3 mapping when selecting or
2496 * allocating the buffer.
2497 */
2498 int rc = RTSemFastMutexRequest(pIf->pNetwork->FastMutex);
2499 if (RT_SUCCESS(rc))
2500 {
2501 *ppRing3Buf = pIf->pIntBufR3;
2502 rc = RTSemFastMutexRelease(pIf->pNetwork->FastMutex);
2503 }
2504
2505 intnetR0IfRelease(pIf, pSession);
2506 LogFlow(("INTNETR0IfGetRing3Buffer: returns %Rrc *ppRing3Buf=%p\n", rc, *ppRing3Buf));
2507 return rc;
2508}
2509
2510
2511/**
2512 * VMMR0 request wrapper for INTNETR0IfGetRing3Buffer.
2513 *
2514 * @returns see INTNETR0IfGetRing3Buffer.
2515 * @param pIntNet The internal networking instance.
2516 * @param pSession The caller's session.
2517 * @param pReq The request packet.
2518 */
2519INTNETR0DECL(int) INTNETR0IfGetRing3BufferReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFGETRING3BUFFERREQ pReq)
2520{
2521 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
2522 return VERR_INVALID_PARAMETER;
2523 return INTNETR0IfGetRing3Buffer(pIntNet, pReq->hIf, pSession, &pReq->pRing3Buf);
2524}
2525
2526
2527/**
2528 * Gets the ring-0 address of the current buffer.
2529 *
2530 * @returns VBox status code.
2531 * @param pIntNet The instance data.
2532 * @param hIf The interface handle.
2533 * @param pSession The caller's session.
2534 * @param ppRing0Buf Where to store the address of the ring-3 mapping.
2535 */
2536INTNETR0DECL(int) INTNETR0IfGetRing0Buffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF *ppRing0Buf)
2537{
2538 LogFlow(("INTNETR0IfGetRing0Buffer: pIntNet=%p hIf=%RX32 ppRing0Buf=%p\n", pIntNet, hIf, ppRing0Buf));
2539
2540 /*
2541 * Validate input.
2542 */
2543 AssertPtrReturn(ppRing0Buf, VERR_INVALID_PARAMETER);
2544 *ppRing0Buf = NULL;
2545 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
2546 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2547 if (!pIf)
2548 return VERR_INVALID_HANDLE;
2549
2550 /*
2551 * Grab the lock and get the data.
2552 * ASSUMES that the handle isn't closed while we're here.
2553 */
2554 int rc = RTSemFastMutexRequest(pIf->pNetwork->FastMutex);
2555 if (RT_SUCCESS(rc))
2556 {
2557 *ppRing0Buf = pIf->pIntBuf;
2558
2559 rc = RTSemFastMutexRelease(pIf->pNetwork->FastMutex);
2560 }
2561 intnetR0IfRelease(pIf, pSession);
2562 LogFlow(("INTNETR0IfGetRing0Buffer: returns %Rrc *ppRing0Buf=%p\n", rc, *ppRing0Buf));
2563 return rc;
2564}
2565
2566
2567#if 0
2568/**
2569 * Gets the physical addresses of the default interface buffer.
2570 *
2571 * @returns VBox status code.
2572 * @param pIntNet The instance data.
2573 * @param hIF The interface handle.
2574 * @param paPages Where to store the addresses. (The reserved fields will be set to zero.)
2575 * @param cPages
2576 */
2577INTNETR0DECL(int) INTNETR0IfGetPhysBuffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPPAGE paPages, unsigned cPages)
2578{
2579 /*
2580 * Validate input.
2581 */
2582 AssertReturn(pIntNet, VERR_INVALID_PARAMETER);
2583 AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
2584 AssertPtrReturn((uint8_t *)&paPages[cPages] - 1, VERR_INVALID_PARAMETER);
2585 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2586 if (!pIf)
2587 return VERR_INVALID_HANDLE;
2588
2589 /*
2590 * Grab the lock and get the data.
2591 * ASSUMES that the handle isn't closed while we're here.
2592 */
2593 int rc = RTSemFastMutexRequest(pIf->pNetwork->FastMutex);
2594 if (RT_SUCCESS(rc))
2595 {
2596 /** @todo make a SUPR0 api for obtaining the array. SUPR0/IPRT is keeping track of everything, there
2597 * is no need for any extra bookkeeping here.. */
2598
2599 rc = RTSemFastMutexRelease(pIf->pNetwork->FastMutex);
2600 }
2601 intnetR0IfRelease(pIf, pSession);
2602 return VERR_NOT_IMPLEMENTED;
2603}
2604#endif
2605
2606
2607/**
2608 * Sets the promiscuous mode property of an interface.
2609 *
2610 * @returns VBox status code.
2611 * @param pIntNet The instance handle.
2612 * @param hIf The interface handle.
2613 * @param pSession The caller's session.
2614 * @param fPromiscuous Set if the interface should be in promiscuous mode, clear if not.
2615 */
2616INTNETR0DECL(int) INTNETR0IfSetPromiscuousMode(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fPromiscuous)
2617{
2618 LogFlow(("INTNETR0IfSetPromiscuousMode: pIntNet=%p hIf=%RX32 fPromiscuous=%d\n", pIntNet, hIf, fPromiscuous));
2619
2620 /*
2621 * Validate & translate input.
2622 */
2623 AssertReturn(pIntNet, VERR_INVALID_PARAMETER);
2624 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2625 if (!pIf)
2626 {
2627 Log(("INTNETR0IfSetPromiscuousMode: returns VERR_INVALID_HANDLE\n"));
2628 return VERR_INVALID_HANDLE;
2629 }
2630
2631 /*
2632 * Grab the network semaphore and make the change.
2633 */
2634 int rc;
2635 PINTNETNETWORK pNetwork = pIf->pNetwork;
2636 if (pNetwork)
2637 {
2638 rc = RTSemFastMutexRequest(pNetwork->FastMutex);
2639 if (RT_SUCCESS(rc))
2640 {
2641 if (pIf->fPromiscuous != fPromiscuous)
2642 {
2643 Log(("INTNETR0IfSetPromiscuousMode: hIf=%RX32: Changed from %d -> %d\n",
2644 hIf, !fPromiscuous, !!fPromiscuous));
2645 ASMAtomicUoWriteBool(&pIf->fPromiscuous, fPromiscuous);
2646 }
2647
2648 rc = RTSemFastMutexRelease(pNetwork->FastMutex);
2649 }
2650 }
2651 else
2652 rc = VERR_WRONG_ORDER;
2653
2654 intnetR0IfRelease(pIf, pSession);
2655 return rc;
2656}
2657
2658
2659/**
2660 * VMMR0 request wrapper for INTNETR0IfSetPromiscuousMode.
2661 *
2662 * @returns see INTNETR0IfSetPromiscuousMode.
2663 * @param pIntNet The internal networking instance.
2664 * @param pSession The caller's session.
2665 * @param pReq The request packet.
2666 */
2667INTNETR0DECL(int) INTNETR0IfSetPromiscuousModeReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSETPROMISCUOUSMODEREQ pReq)
2668{
2669 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
2670 return VERR_INVALID_PARAMETER;
2671 return INTNETR0IfSetPromiscuousMode(pIntNet, pReq->hIf, pSession, pReq->fPromiscuous);
2672}
2673
2674
2675/**
2676 * Sets the MAC address of an interface.
2677 *
2678 * @returns VBox status code.
2679 * @param pIntNet The instance handle.
2680 * @param hIf The interface handle.
2681 * @param pSession The caller's session.
2682 * @param pMAC The new MAC address.
2683 */
2684INTNETR0DECL(int) INTNETR0IfSetMacAddress(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PCRTMAC pMac)
2685{
2686 LogFlow(("INTNETR0IfSetMacAddress: pIntNet=%p hIf=%RX32 pMac=%p:{%.6Rhxs}\n", pIntNet, hIf, pMac, pMac));
2687
2688 /*
2689 * Validate & translate input.
2690 */
2691 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
2692 AssertPtrReturn(pMac, VERR_INVALID_PARAMETER);
2693 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2694 if (!pIf)
2695 {
2696 Log(("INTNETR0IfSetMacAddress: returns VERR_INVALID_HANDLE\n"));
2697 return VERR_INVALID_HANDLE;
2698 }
2699
2700 /*
2701 * Grab the network semaphore and make the change.
2702 */
2703 int rc;
2704 PINTNETNETWORK pNetwork = pIf->pNetwork;
2705 if (pNetwork)
2706 {
2707 rc = RTSemFastMutexRequest(pNetwork->FastMutex);
2708 if (RT_SUCCESS(rc))
2709 {
2710 if (memcmp(&pIf->Mac, pMac, sizeof(pIf->Mac)))
2711 {
2712 Log(("INTNETR0IfSetMacAddress: hIf=%RX32: Changed from %.6Rhxs -> %.6Rhxs\n",
2713 hIf, &pIf->Mac, pMac));
2714 pIf->Mac = *pMac;
2715 pIf->fMacSet = true;
2716 }
2717
2718 rc = RTSemFastMutexRelease(pNetwork->FastMutex);
2719 }
2720 }
2721 else
2722 rc = VERR_WRONG_ORDER;
2723
2724 intnetR0IfRelease(pIf, pSession);
2725 return rc;
2726}
2727
2728
2729/**
2730 * VMMR0 request wrapper for INTNETR0IfSetMacAddress.
2731 *
2732 * @returns see INTNETR0IfSetMacAddress.
2733 * @param pIntNet The internal networking instance.
2734 * @param pSession The caller's session.
2735 * @param pReq The request packet.
2736 */
2737INTNETR0DECL(int) INTNETR0IfSetMacAddressReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSETMACADDRESSREQ pReq)
2738{
2739 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
2740 return VERR_INVALID_PARAMETER;
2741 return INTNETR0IfSetMacAddress(pIntNet, pReq->hIf, pSession, &pReq->Mac);
2742}
2743
2744
2745/**
2746 * Worker for intnetR0IfSetActive.
2747 *
2748 * This function will update the active interface count on the network and
2749 * activate or deactivate the trunk connection if necessary. Note that in
2750 * order to do this it is necessary to abandond the network semaphore.
2751 *
2752 * @returns VBox status code.
2753 * @param pNetwork The network.
2754 * @param fIf The interface.
2755 * @param fActive What to do.
2756 */
2757static int intnetR0NetworkSetIfActive(PINTNETNETWORK pNetwork, PINTNETIF pIf, bool fActive)
2758{
2759 /* quick santiy check */
2760 AssertPtr(pNetwork);
2761 AssertPtr(pIf);
2762
2763 /*
2764 * If we've got a trunk, lock it now in case we need to call out, and
2765 * then lock the network.
2766 */
2767 PINTNETTRUNKIF pTrunkIf = pNetwork->pTrunkIF;
2768 if (pTrunkIf && !intnetR0TrunkIfOutLock(pTrunkIf))
2769 return VERR_SEM_DESTROYED;
2770
2771 int rc = RTSemFastMutexRequest(pNetwork->FastMutex); AssertRC(rc);
2772 if (RT_SUCCESS(rc))
2773 {
2774 bool fNetworkLocked = true;
2775
2776 /*
2777 * Make the change if necessary.
2778 */
2779 if (pIf->fActive != fActive)
2780 {
2781 pIf->fActive = fActive;
2782
2783 uint32_t const cActiveIFs = pNetwork->cActiveIFs;
2784 Assert((int32_t)cActiveIFs + (fActive ? 1 : -1) >= 0);
2785 pNetwork->cActiveIFs += fActive ? 1 : -1;
2786
2787 if ( pTrunkIf
2788 && ( !pNetwork->cActiveIFs
2789 || !cActiveIFs))
2790 {
2791 /*
2792 * We'll have to change the trunk status, so, leave
2793 * the network semaphore so we don't create any deadlocks.
2794 */
2795 int rc2 = RTSemFastMutexRelease(pNetwork->FastMutex); AssertRC(rc2);
2796 fNetworkLocked = false;
2797
2798 if (pTrunkIf->pIfPort)
2799 pTrunkIf->pIfPort->pfnSetActive(pTrunkIf->pIfPort, fActive);
2800 }
2801 }
2802
2803 if (fNetworkLocked)
2804 RTSemFastMutexRelease(pNetwork->FastMutex);
2805 }
2806 if (pTrunkIf)
2807 intnetR0TrunkIfOutUnlock(pTrunkIf);
2808 return rc;
2809}
2810
2811
2812/**
2813 * Activates or deactivates a interface.
2814 *
2815 * This is used to enable and disable the trunk connection on demans as well as
2816 * know when not to expect an interface to want to receive packets.
2817 *
2818 * @returns VBox status code.
2819 * @param pIf The interface.
2820 * @param fActive What to do.
2821 */
2822static int intnetR0IfSetActive(PINTNETIF pIf, bool fActive)
2823{
2824 /* quick sanity check */
2825 AssertPtrReturn(pIf, VERR_INVALID_POINTER);
2826
2827 /*
2828 * Hand it to the network since it might involve the trunk
2829 * and things are tricky there wrt to locking order.
2830 */
2831 PINTNETNETWORK pNetwork = pIf->pNetwork;
2832 if (!pNetwork)
2833 return VERR_WRONG_ORDER;
2834 return intnetR0NetworkSetIfActive(pNetwork, pIf, fActive);
2835}
2836
2837
2838/**
2839 * Sets the active property of an interface.
2840 *
2841 * @returns VBox status code.
2842 * @param pIntNet The instance handle.
2843 * @param hIf The interface handle.
2844 * @param pSession The caller's session.
2845 * @param fActive The new state.
2846 */
2847INTNETR0DECL(int) INTNETR0IfSetActive(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fActive)
2848{
2849 LogFlow(("INTNETR0IfSetActive: pIntNet=%p hIf=%RX32 fActive=%RTbool\n", pIntNet, hIf, fActive));
2850
2851 /*
2852 * Validate & translate input.
2853 */
2854 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
2855 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2856 if (!pIf)
2857 {
2858 Log(("INTNETR0IfSetActive: returns VERR_INVALID_HANDLE\n"));
2859 return VERR_INVALID_HANDLE;
2860 }
2861
2862 /*
2863 * Hand it to the network since it might involve the trunk
2864 * and things are tricky there wrt to locking order.
2865 */
2866 int rc;
2867 PINTNETNETWORK pNetwork = pIf->pNetwork;
2868 if (pNetwork)
2869 rc = intnetR0NetworkSetIfActive(pNetwork, pIf, fActive);
2870 else
2871 rc = VERR_WRONG_ORDER;
2872
2873 intnetR0IfRelease(pIf, pSession);
2874 return rc;
2875}
2876
2877
2878/**
2879 * VMMR0 request wrapper for INTNETR0IfSetActive.
2880 *
2881 * @returns see INTNETR0IfSetActive.
2882 * @param pIntNet The internal networking instance.
2883 * @param pSession The caller's session.
2884 * @param pReq The request packet.
2885 */
2886INTNETR0DECL(int) INTNETR0IfSetActiveReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSETACTIVEREQ pReq)
2887{
2888 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
2889 return VERR_INVALID_PARAMETER;
2890 return INTNETR0IfSetActive(pIntNet, pReq->hIf, pSession, pReq->fActive);
2891}
2892
2893
2894/**
2895 * Wait for the interface to get signaled.
2896 * The interface will be signaled when is put into the receive buffer.
2897 *
2898 * @returns VBox status code.
2899 * @param pIntNet The instance handle.
2900 * @param hIf The interface handle.
2901 * @param pSession The caller's session.
2902 * @param cMillies Number of milliseconds to wait. RT_INDEFINITE_WAIT should be
2903 * used if indefinite wait is desired.
2904 */
2905INTNETR0DECL(int) INTNETR0IfWait(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, uint32_t cMillies)
2906{
2907 Log4(("INTNETR0IfWait: pIntNet=%p hIf=%RX32 cMillies=%u\n", pIntNet, hIf, cMillies));
2908
2909 /*
2910 * Get and validate essential handles.
2911 */
2912 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
2913 PINTNETIF pIf = (PINTNETIF)RTHandleTableLookupWithCtx(pIntNet->hHtIfs, hIf, pSession);
2914 if (!pIf)
2915 {
2916 Log(("INTNETR0IfWait: returns VERR_INVALID_HANDLE\n"));
2917 return VERR_INVALID_HANDLE;
2918 }
2919 const INTNETIFHANDLE hIfSelf = pIf->hIf;
2920 const RTSEMEVENT Event = pIf->Event;
2921 if ( hIfSelf != hIf /* paranoia */
2922 && Event != NIL_RTSEMEVENT)
2923 {
2924 Log(("INTNETR0IfWait: returns VERR_SEM_DESTROYED\n"));
2925 return VERR_SEM_DESTROYED;
2926 }
2927
2928 /*
2929 * It is tempting to check if there is data to be read here,
2930 * but the problem with such an approach is that it will cause
2931 * one unnecessary supervisor->user->supervisor trip. There is
2932 * already a slight risk for such, so no need to increase it.
2933 */
2934
2935 /*
2936 * Increment the number of waiters before starting the wait.
2937 * Upon wakeup we must assert reality, checking that we're not
2938 * already destroyed or in the process of being destroyed. This
2939 * code must be aligned with the waiting code in intnetR0IfDestruct.
2940 */
2941 ASMAtomicIncU32(&pIf->cSleepers);
2942 int rc = RTSemEventWaitNoResume(Event, cMillies);
2943 if (pIf->Event == Event)
2944 {
2945 ASMAtomicDecU32(&pIf->cSleepers);
2946 if (!pIf->fDestroying)
2947 {
2948 intnetR0IfRelease(pIf, pSession);
2949 if (pIf->hIf != hIf)
2950 rc = VERR_SEM_DESTROYED;
2951 }
2952 else
2953 rc = VERR_SEM_DESTROYED;
2954 }
2955 else
2956 rc = VERR_SEM_DESTROYED;
2957 Log4(("INTNETR0IfWait: returns %Rrc\n", rc));
2958 return rc;
2959}
2960
2961
2962/**
2963 * VMMR0 request wrapper for INTNETR0IfWait.
2964 *
2965 * @returns see INTNETR0IfWait.
2966 * @param pIntNet The internal networking instance.
2967 * @param pSession The caller's session.
2968 * @param pReq The request packet.
2969 */
2970INTNETR0DECL(int) INTNETR0IfWaitReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFWAITREQ pReq)
2971{
2972 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
2973 return VERR_INVALID_PARAMETER;
2974 return INTNETR0IfWait(pIntNet, pReq->hIf, pSession, pReq->cMillies);
2975}
2976
2977
2978/**
2979 * Close an interface.
2980 *
2981 * @returns VBox status code.
2982 * @param pIntNet The instance handle.
2983 * @param hIf The interface handle.
2984 * @param pSession The caller's session.
2985 */
2986INTNETR0DECL(int) INTNETR0IfClose(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession)
2987{
2988 LogFlow(("INTNETR0IfClose: pIntNet=%p hIf=%RX32\n", pIntNet, hIf));
2989
2990 /*
2991 * Validate and free the handle.
2992 */
2993 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
2994 PINTNETIF pIf = (PINTNETIF)RTHandleTableFreeWithCtx(pIntNet->hHtIfs, hIf, pSession);
2995 if (!pIf)
2996 return VERR_INVALID_HANDLE;
2997
2998 /* mark the handle as freed so intnetR0IfDestruct won't free it again. */
2999 ASMAtomicWriteU32(&pIf->hIf, INTNET_HANDLE_INVALID);
3000
3001
3002 /*
3003 * Release the references to the interface object (handle + free lookup).
3004 * But signal the event semaphore first so any waiter holding a reference
3005 * will wake up too (he'll see hIf == invalid and return correctly).
3006 */
3007 RTSemEventSignal(pIf->Event);
3008
3009 void *pvObj = pIf->pvObj;
3010 intnetR0IfRelease(pIf, pSession); /* (RTHandleTableFreeWithCtx) */
3011
3012 int rc = SUPR0ObjRelease(pvObj, pSession);
3013 LogFlow(("INTNETR0IfClose: returns %Rrc\n", rc));
3014 return rc;
3015}
3016
3017
3018/**
3019 * VMMR0 request wrapper for INTNETR0IfCloseReq.
3020 *
3021 * @returns see INTNETR0IfClose.
3022 * @param pIntNet The internal networking instance.
3023 * @param pSession The caller's session.
3024 * @param pReq The request packet.
3025 */
3026INTNETR0DECL(int) INTNETR0IfCloseReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFCLOSEREQ pReq)
3027{
3028 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
3029 return VERR_INVALID_PARAMETER;
3030 return INTNETR0IfClose(pIntNet, pReq->hIf, pSession);
3031}
3032
3033
3034/**
3035 * Interface destructor callback.
3036 * This is called for reference counted objectes when the count reaches 0.
3037 *
3038 * @param pvObj The object pointer.
3039 * @param pvUser1 Pointer to the interface.
3040 * @param pvUser2 Pointer to the INTNET instance data.
3041 */
3042static DECLCALLBACK(void) intnetR0IfDestruct(void *pvObj, void *pvUser1, void *pvUser2)
3043{
3044 PINTNETIF pIf = (PINTNETIF)pvUser1;
3045 PINTNET pIntNet = (PINTNET)pvUser2;
3046 Log(("intnetR0IfDestruct: pvObj=%p pIf=%p pIntNet=%p hIf=%RX32\n", pvObj, pIf, pIntNet, pIf->hIf));
3047
3048 RTSemFastMutexRequest(pIntNet->FastMutex);
3049
3050 /*
3051 * Mark the interface as being destroyed so the waiter
3052 * can behave appropriately (theoretical case).
3053 */
3054 ASMAtomicWriteBool(&pIf->fDestroying, true);
3055
3056 /*
3057 * Delete the interface handle so the object no longer can be used.
3058 * (Can happen if the client didn't close its session.)
3059 */
3060 INTNETIFHANDLE hIf = ASMAtomicXchgU32(&pIf->hIf, INTNET_HANDLE_INVALID);
3061 if (hIf != INTNET_HANDLE_INVALID)
3062 {
3063 void *pvObj2 = RTHandleTableFreeWithCtx(pIntNet->hHtIfs, hIf, pIf->pSession); NOREF(pvObj2);
3064 AssertMsg(pvObj2 == pIf, ("%p, %p, hIf=%RX32 pSession=%p\n", pvObj2, pIf, hIf, pIf->pSession));
3065 }
3066
3067 /*
3068 * If we've got a network deactivate and unlink ourselves from it.
3069 * Because of cleanup order we might be an orphan now.
3070 */
3071 PINTNETNETWORK pNetwork = pIf->pNetwork;
3072 if (pNetwork)
3073 {
3074 intnetR0IfSetActive(pIf, false);
3075
3076 if (pNetwork->pIFs == pIf)
3077 pNetwork->pIFs = pIf->pNext;
3078 else
3079 {
3080 PINTNETIF pPrev = pNetwork->pIFs;
3081 while (pPrev)
3082 {
3083 if (pPrev->pNext == pIf)
3084 {
3085 pPrev->pNext = pIf->pNext;
3086 break;
3087 }
3088 pPrev = pPrev->pNext;
3089 }
3090 Assert(pPrev);
3091 }
3092 pIf->pNext = NULL;
3093
3094 /*
3095 * Release our reference to the network.
3096 */
3097 RTSemFastMutexRelease(pIntNet->FastMutex);
3098
3099 SUPR0ObjRelease(pNetwork->pvObj, pIf->pSession);
3100 pIf->pNetwork = NULL;
3101 }
3102 else
3103 RTSemFastMutexRelease(pIntNet->FastMutex);
3104
3105 /*
3106 * Wakeup anyone waiting on this interface.
3107 *
3108 * We *must* make sure they have woken up properly and realized
3109 * that the interface is no longer valid.
3110 */
3111 if (pIf->Event != NIL_RTSEMEVENT)
3112 {
3113 RTSEMEVENT Event = pIf->Event;
3114 unsigned cMaxWait = 0x1000;
3115 while (pIf->cSleepers && cMaxWait-- > 0)
3116 {
3117 RTSemEventSignal(Event);
3118 RTThreadYield();
3119 }
3120 if (pIf->cSleepers)
3121 {
3122 RTThreadSleep(1);
3123
3124 cMaxWait = pIf->cSleepers;
3125 while (pIf->cSleepers && cMaxWait-- > 0)
3126 {
3127 RTSemEventSignal(Event);
3128 RTThreadSleep(10);
3129 }
3130 }
3131
3132 RTSemEventDestroy(Event);
3133 pIf->Event = NIL_RTSEMEVENT;
3134 }
3135
3136 /*
3137 * Unmap user buffer.
3138 */
3139 if (pIf->pIntBuf != pIf->pIntBufDefault)
3140 {
3141 /** @todo user buffer */
3142 }
3143
3144 /*
3145 * Unmap and Free the default buffer.
3146 */
3147 if (pIf->pIntBufDefault)
3148 {
3149 SUPR0MemFree(pIf->pSession, (RTHCUINTPTR)pIf->pIntBufDefault);
3150 pIf->pIntBufDefault = NULL;
3151 pIf->pIntBufDefaultR3 = 0;
3152 pIf->pIntBuf = NULL;
3153 pIf->pIntBufR3 = 0;
3154 }
3155
3156 /*
3157 * The interface.
3158 */
3159 pIf->pvObj = NULL;
3160 RTMemFree(pIf);
3161}
3162
3163
3164/**
3165 * Creates a new network interface.
3166 *
3167 * The call must have opened the network for the new interface
3168 * and is responsible for closing it on failure. On success
3169 * it must leave the network opened so the interface destructor
3170 * can close it.
3171 *
3172 * @returns VBox status code.
3173 * @param pNetwork The network.
3174 * @param pSession The session handle.
3175 * @param cbSend The size of the send buffer.
3176 * @param cbRecv The size of the receive buffer.
3177 * @param phIf Where to store the interface handle.
3178 */
3179static int intnetR0NetworkCreateIf(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession, unsigned cbSend, unsigned cbRecv, bool *pfCloseNetwork, PINTNETIFHANDLE phIf)
3180{
3181 LogFlow(("intnetR0NetworkCreateIf: pNetwork=%p pSession=%p cbSend=%u cbRecv=%u phIf=%p\n",
3182 pNetwork, pSession, cbSend, cbRecv, phIf));
3183
3184 /*
3185 * Assert input.
3186 */
3187 AssertPtr(pNetwork);
3188 AssertPtr(phIf);
3189 AssertPtr(pfCloseNetwork);
3190 *pfCloseNetwork = false;
3191
3192 /*
3193 * Allocate and initialize the interface structure.
3194 */
3195 PINTNETIF pIf = (PINTNETIF)RTMemAllocZ(sizeof(*pIf));
3196 if (!pIf)
3197 return VERR_NO_MEMORY;
3198 //pIf->pNext = NULL;
3199 memset(&pIf->Mac, 0xff, sizeof(pIf->Mac)); /* broadcast */
3200 //pIf->fMacSet = false;
3201 //pIf->fPromiscuous = false;
3202 //pIf->fActive = false;
3203 //pIf->fDestroying = false;
3204 //pIf->pIntBuf = 0;
3205 //pIf->pIntBufR3 = NIL_RTR3PTR;
3206 //pIf->pIntBufDefault = 0;
3207 //pIf->pIntBufDefaultR3 = NIL_RTR3PTR;
3208 //pIf->cYields = 0;
3209 pIf->Event = NIL_RTSEMEVENT;
3210 //pIf->cSleepers = 0;
3211 pIf->hIf = INTNET_HANDLE_INVALID;
3212 pIf->pNetwork = pNetwork;
3213 pIf->pSession = pSession;
3214 //pIf->pvObj = NULL;
3215 //pIf->aAddrCache[kIntNetAddrType_Invalid] = {0};
3216 //pIf->aAddrCache[kIntNetAddrType_IPv4].pbEntries = NULL;
3217 //pIf->aAddrCache[kIntNetAddrType_IPv4].cEntries = 0;
3218 //pIf->aAddrCache[kIntNetAddrType_IPv4].cEntriesAlloc = 0;
3219 pIf->aAddrCache[kIntNetAddrType_IPv4].cbAddress = intnetR0AddrSize(kIntNetAddrType_IPv4);
3220 pIf->aAddrCache[kIntNetAddrType_IPv4].cbEntry = intnetR0AddrSize(kIntNetAddrType_IPv4);
3221 //pIf->aAddrCache[kIntNetAddrType_IPv6].pbEntries = NULL;
3222 //pIf->aAddrCache[kIntNetAddrType_IPv6].cEntries = 0;
3223 //pIf->aAddrCache[kIntNetAddrType_IPv6].cEntriesAlloc = 0;
3224 pIf->aAddrCache[kIntNetAddrType_IPv6].cbAddress = intnetR0AddrSize(kIntNetAddrType_IPv6);
3225 pIf->aAddrCache[kIntNetAddrType_IPv6].cbEntry = intnetR0AddrSize(kIntNetAddrType_IPv6);
3226 //pIf->aAddrCache[kIntNetAddrType_IPX].pbEntries = NULL;
3227 //pIf->aAddrCache[kIntNetAddrType_IPX].cEntries = 0;
3228 //pIf->aAddrCache[kIntNetAddrType_IPX].cEntriesAlloc = 0;
3229 pIf->aAddrCache[kIntNetAddrType_IPX].cbAddress = intnetR0AddrSize(kIntNetAddrType_IPX);
3230 pIf->aAddrCache[kIntNetAddrType_IPX].cbEntry = RT_ALIGN_32(intnetR0AddrSize(kIntNetAddrType_IPv4), 16);
3231 int rc = RTSemEventCreate((PRTSEMEVENT)&pIf->Event);
3232 if (RT_SUCCESS(rc))
3233 {
3234 /*
3235 * Create the default buffer.
3236 */
3237 /** @todo adjust with minimums and apply defaults here. */
3238 cbRecv = RT_ALIGN(RT_MAX(cbRecv, sizeof(INTNETHDR) * 4), sizeof(INTNETHDR));
3239 cbSend = RT_ALIGN(RT_MAX(cbSend, sizeof(INTNETHDR) * 4), sizeof(INTNETHDR));
3240 const unsigned cbBuf = RT_ALIGN(sizeof(*pIf->pIntBuf), sizeof(INTNETHDR)) + cbRecv + cbSend;
3241 rc = SUPR0MemAlloc(pIf->pSession, cbBuf, (PRTR0PTR)&pIf->pIntBufDefault, (PRTR3PTR)&pIf->pIntBufDefaultR3);
3242 if (RT_SUCCESS(rc))
3243 {
3244 ASMMemZero32(pIf->pIntBufDefault, cbBuf); /** @todo I thought I specified these buggers as clearing the memory... */
3245
3246 pIf->pIntBuf = pIf->pIntBufDefault;
3247 pIf->pIntBufR3 = pIf->pIntBufDefaultR3;
3248 pIf->pIntBuf->cbBuf = cbBuf;
3249 pIf->pIntBuf->cbRecv = cbRecv;
3250 pIf->pIntBuf->cbSend = cbSend;
3251 /* receive ring buffer. */
3252 pIf->pIntBuf->Recv.offStart = RT_ALIGN_32(sizeof(*pIf->pIntBuf), sizeof(INTNETHDR));
3253 pIf->pIntBuf->Recv.offRead = pIf->pIntBuf->Recv.offStart;
3254 pIf->pIntBuf->Recv.offWrite = pIf->pIntBuf->Recv.offStart;
3255 pIf->pIntBuf->Recv.offEnd = pIf->pIntBuf->Recv.offStart + cbRecv;
3256 /* send ring buffer. */
3257 pIf->pIntBuf->Send.offStart = pIf->pIntBuf->Recv.offEnd;
3258 pIf->pIntBuf->Send.offRead = pIf->pIntBuf->Send.offStart;
3259 pIf->pIntBuf->Send.offWrite = pIf->pIntBuf->Send.offStart;
3260 pIf->pIntBuf->Send.offEnd = pIf->pIntBuf->Send.offStart + cbSend;
3261
3262 /*
3263 * Link the interface to the network.
3264 */
3265 rc = RTSemFastMutexRequest(pNetwork->FastMutex);
3266 if (RT_SUCCESS(rc))
3267 {
3268 pIf->pNext = pNetwork->pIFs;
3269 pNetwork->pIFs = pIf;
3270 RTSemFastMutexRelease(pNetwork->FastMutex);
3271
3272 /*
3273 * Register the interface with the session.
3274 */
3275 pIf->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE, intnetR0IfDestruct, pIf, pNetwork->pIntNet);
3276 if (pIf->pvObj)
3277 {
3278 rc = RTHandleTableAllocWithCtx(pNetwork->pIntNet->hHtIfs, pIf, pSession, (uint32_t *)&pIf->hIf);
3279 if (RT_SUCCESS(rc))
3280 {
3281 *phIf = pIf->hIf;
3282 Log(("intnetR0NetworkCreateIf: returns VINF_SUCCESS *phIf=%RX32 cbSend=%u cbRecv=%u cbBuf=%u\n",
3283 *phIf, pIf->pIntBufDefault->cbSend, pIf->pIntBufDefault->cbRecv, pIf->pIntBufDefault->cbBuf));
3284 return VINF_SUCCESS;
3285 }
3286
3287 SUPR0ObjRelease(pIf->pvObj, pSession);
3288 LogFlow(("intnetR0NetworkCreateIf: returns %Rrc\n", rc));
3289 return rc;
3290 }
3291
3292 RTSemFastMutexDestroy(pNetwork->FastMutex);
3293 pNetwork->FastMutex = NIL_RTSEMFASTMUTEX;
3294 }
3295
3296 SUPR0MemFree(pIf->pSession, (RTHCUINTPTR)pIf->pIntBufDefault);
3297 pIf->pIntBufDefault = NULL;
3298 pIf->pIntBuf = NULL;
3299 }
3300
3301 RTSemEventDestroy(pIf->Event);
3302 pIf->Event = NIL_RTSEMEVENT;
3303 }
3304 RTMemFree(pIf);
3305 LogFlow(("intnetR0NetworkCreateIf: returns %Rrc\n", rc));
3306 *pfCloseNetwork = true;
3307 return rc;
3308}
3309
3310
3311#ifdef RT_WITH_W64_UNWIND_HACK
3312# if defined(RT_OS_WINDOWS) && defined(RT_ARCH_AMD64)
3313# define INTNET_DECL_CALLBACK(type) DECLASM(DECLHIDDEN(type))
3314# define INTNET_CALLBACK(_n) intnetNtWrap##_n
3315
3316 /* wrapper callback declarations */
3317 INTNET_DECL_CALLBACK(bool) INTNET_CALLBACK(intnetR0TrunkIfPortSetSGPhys)(PINTNETTRUNKSWPORT pSwitchPort, bool fEnable);
3318 INTNET_DECL_CALLBACK(bool) INTNET_CALLBACK(intnetR0TrunkIfPortRecv)(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG, uint32_t fSrc);
3319 INTNET_DECL_CALLBACK(void) INTNET_CALLBACK(intnetR0TrunkIfPortSGRetain)(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG);
3320 INTNET_DECL_CALLBACK(void) INTNET_CALLBACK(intnetR0TrunkIfPortSGRelease)(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG);
3321
3322# else
3323# error "UNSUPPORTED (RT_WITH_W64_UNWIND_HACK)"
3324# endif
3325#else
3326# define INTNET_DECL_CALLBACK(_t) static DECLCALLBACK(_t)
3327# define INTNET_CALLBACK(_n) _n
3328#endif
3329
3330/** @copydoc INTNETTRUNKSWPORT::pfnSetSGPhys */
3331INTNET_DECL_CALLBACK(bool) intnetR0TrunkIfPortSetSGPhys(PINTNETTRUNKSWPORT pSwitchPort, bool fEnable)
3332{
3333 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
3334 AssertMsgFailed(("Not implemented because it wasn't required on Darwin\n"));
3335 return ASMAtomicXchgBool(&pThis->fPhysSG, fEnable);
3336}
3337
3338
3339/** @copydoc INTNETTRUNKSWPORT::pfnRecv */
3340INTNET_DECL_CALLBACK(bool) intnetR0TrunkIfPortRecv(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG, uint32_t fSrc)
3341{
3342 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
3343 PINTNETNETWORK pNetwork = pThis->pNetwork;
3344
3345 /* assert some sanity */
3346 AssertPtrReturn(pNetwork, false);
3347 AssertReturn(pNetwork->FastMutex != NIL_RTSEMFASTMUTEX, false);
3348 AssertPtr(pSG);
3349 Assert(fSrc);
3350
3351 /*
3352 * Lock the network and send the frame to it.
3353 */
3354 int rc = RTSemFastMutexRequest(pNetwork->FastMutex);
3355 AssertRCReturn(rc, false);
3356
3357 bool fRc;
3358 if (RT_LIKELY(pNetwork->cActiveIFs > 0))
3359 fRc = intnetR0NetworkSend(pNetwork, NULL, fSrc, pSG, false /* fTrunkLocked */);
3360 else
3361 fRc = false; /* don't drop it */
3362
3363 rc = RTSemFastMutexRelease(pNetwork->FastMutex);
3364 AssertRC(rc);
3365
3366 return fRc;
3367}
3368
3369
3370/** @copydoc INTNETTRUNKSWPORT::pfnSGRetain */
3371INTNET_DECL_CALLBACK(void) intnetR0TrunkIfPortSGRetain(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
3372{
3373 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
3374 PINTNETNETWORK pNetwork = pThis->pNetwork;
3375
3376 /* assert some sanity */
3377 AssertPtrReturnVoid(pNetwork);
3378 AssertReturnVoid(pNetwork->FastMutex != NIL_RTSEMFASTMUTEX);
3379 AssertPtr(pSG);
3380 Assert(pSG->cUsers > 0);
3381
3382 /* do it. */
3383 ++pSG->cUsers;
3384}
3385
3386
3387/** @copydoc INTNETTRUNKSWPORT::pfnSGRelease */
3388INTNET_DECL_CALLBACK(void) intnetR0TrunkIfPortSGRelease(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
3389{
3390 PINTNETTRUNKIF pThis = INTNET_SWITCHPORT_2_TRUNKIF(pSwitchPort);
3391 PINTNETNETWORK pNetwork = pThis->pNetwork;
3392
3393 /* assert some sanity */
3394 AssertPtrReturnVoid(pNetwork);
3395 AssertReturnVoid(pNetwork->FastMutex != NIL_RTSEMFASTMUTEX);
3396 AssertPtr(pSG);
3397 Assert(pSG->cUsers > 0);
3398
3399 /*
3400 * Free it?
3401 */
3402 if (!--pSG->cUsers)
3403 {
3404 /** @todo later */
3405 }
3406}
3407
3408
3409/**
3410 * Retain the trunk interface.
3411 *
3412 * @returns pThis if retained.
3413 *
3414 * @param pThis The trunk.
3415 *
3416 * @remarks Any locks.
3417 */
3418static PINTNETTRUNKIF intnetR0TrunkIfRetain(PINTNETTRUNKIF pThis)
3419{
3420 if (pThis && pThis->pIfPort)
3421 {
3422 pThis->pIfPort->pfnRetain(pThis->pIfPort);
3423 return pThis;
3424 }
3425 return NULL;
3426}
3427
3428
3429/**
3430 * Release the trunk interface.
3431 *
3432 * @param pThis The trunk.
3433 */
3434static void intnetR0TrunkIfRelease(PINTNETTRUNKIF pThis)
3435{
3436 if (pThis && pThis->pIfPort)
3437 pThis->pIfPort->pfnRelease(pThis->pIfPort);
3438}
3439
3440
3441/**
3442 * Takes the out-bound trunk lock.
3443 *
3444 * This will ensure that pIfPort is valid.
3445 *
3446 * @returns success indicator.
3447 * @param pThis The trunk.
3448 *
3449 * @remarks No locks other than the create/destroy one.
3450 */
3451static bool intnetR0TrunkIfOutLock(PINTNETTRUNKIF pThis)
3452{
3453 AssertPtrReturn(pThis, false);
3454 int rc = RTSemFastMutexRequest(pThis->FastMutex);
3455 if (RT_SUCCESS(rc))
3456 {
3457 if (RT_LIKELY(pThis->pIfPort))
3458 return true;
3459 RTSemFastMutexRelease(pThis->FastMutex);
3460 }
3461 else
3462 AssertMsg(rc == VERR_SEM_DESTROYED, ("%Rrc\n", rc));
3463 return false;
3464}
3465
3466
3467/**
3468 * Releases the out-bound trunk lock.
3469 *
3470 * @param pThis The trunk.
3471 */
3472static void intnetR0TrunkIfOutUnlock(PINTNETTRUNKIF pThis)
3473{
3474 if (pThis)
3475 {
3476 int rc = RTSemFastMutexRelease(pThis->FastMutex);
3477 AssertRC(rc);
3478 }
3479}
3480
3481
3482/**
3483 * Activates the trunk interface.
3484 *
3485 * @param pThis The trunk.
3486 * @param fActive What to do with it.
3487 *
3488 * @remarks Caller may only own the create/destroy lock.
3489 */
3490static void intnetR0TrunkIfActivate(PINTNETTRUNKIF pThis, bool fActive)
3491{
3492 if (intnetR0TrunkIfOutLock(pThis))
3493 {
3494 pThis->pIfPort->pfnSetActive(pThis->pIfPort, fActive);
3495 intnetR0TrunkIfOutUnlock(pThis);
3496 }
3497}
3498
3499
3500/**
3501 * Shutdown the trunk interface.
3502 *
3503 * @param pThis The trunk.
3504 * @param pNetworks The network.
3505 *
3506 * @remarks The caller must *NOT* hold the network lock. The global
3507 * create/destroy lock is fine though.
3508 */
3509static void intnetR0TrunkIfDestroy(PINTNETTRUNKIF pThis, PINTNETNETWORK pNetwork)
3510{
3511 /* assert sanity */
3512 if (!pThis)
3513 return;
3514 AssertPtr(pThis);
3515 Assert(pThis->pNetwork == pNetwork);
3516 AssertPtrNull(pThis->pIfPort);
3517
3518 /*
3519 * The interface has already been deactivated, we just to wait for
3520 * it to become idle before we can disconnect and release it.
3521 */
3522 PINTNETTRUNKIFPORT pIfPort = pThis->pIfPort;
3523 if (pIfPort)
3524 {
3525 intnetR0TrunkIfOutLock(pThis);
3526
3527 /* unset it */
3528 pThis->pIfPort = NULL;
3529
3530 /* wait in portions so we can complain ever now an then. */
3531 uint64_t StartTS = RTTimeSystemNanoTS();
3532 int rc = pIfPort->pfnWaitForIdle(pIfPort, 10*1000);
3533 if (RT_FAILURE(rc))
3534 {
3535 LogRel(("intnet: '%s' did't become idle in %RU64 ns (%Rrc).\n",
3536 pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
3537 Assert(rc == VERR_TIMEOUT);
3538 while ( RT_FAILURE(rc)
3539 && RTTimeSystemNanoTS() - StartTS < UINT64_C(30000000000)) /* 30 sec */
3540 rc = pIfPort->pfnWaitForIdle(pIfPort, 10*1000);
3541 if (rc == VERR_TIMEOUT)
3542 {
3543 LogRel(("intnet: '%s' did't become idle in %RU64 ns (%Rrc).\n",
3544 pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
3545 while ( rc == VERR_TIMEOUT
3546 && RTTimeSystemNanoTS() - StartTS < UINT64_C(360000000000)) /* 360 sec */
3547 rc = pIfPort->pfnWaitForIdle(pIfPort, 30*1000);
3548 if (RT_FAILURE(rc))
3549 {
3550 LogRel(("intnet: '%s' did't become idle in %RU64 ns (%Rrc), giving up.\n",
3551 pNetwork->szName, RTTimeSystemNanoTS() - StartTS, rc));
3552 AssertRC(rc);
3553 }
3554 }
3555 }
3556
3557 /* disconnect & release it. */
3558 pIfPort->pfnDisconnectAndRelease(pIfPort);
3559 }
3560
3561 /*
3562 * Free up the resources.
3563 */
3564 RTSEMFASTMUTEX FastMutex = pThis->FastMutex;
3565 pThis->FastMutex = NIL_RTSEMFASTMUTEX;
3566 pThis->pNetwork = NULL;
3567 RTSemFastMutexRelease(FastMutex);
3568 RTSemFastMutexDestroy(FastMutex);
3569 RTMemFree(pThis);
3570}
3571
3572
3573/**
3574 * Creates the trunk connection (if any).
3575 *
3576 * @returns VBox status code.
3577 *
3578 * @param pNetwork The newly created network.
3579 * @param pSession The session handle.
3580 */
3581static int intnetR0NetworkCreateTrunkIf(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession)
3582{
3583 const char *pszName;
3584 switch (pNetwork->enmTrunkType)
3585 {
3586 /*
3587 * The 'None' case, simple.
3588 */
3589 case kIntNetTrunkType_None:
3590 case kIntNetTrunkType_WhateverNone:
3591 return VINF_SUCCESS;
3592
3593 /* Can't happen, but makes GCC happy. */
3594 default:
3595 return VERR_NOT_IMPLEMENTED;
3596
3597 /*
3598 * Translate enum to component factory name.
3599 */
3600 case kIntNetTrunkType_NetFlt:
3601 pszName = "VBoxNetFlt";
3602 break;
3603 case kIntNetTrunkType_NetTap:
3604 pszName = "VBoxNetTap";
3605 break;
3606 case kIntNetTrunkType_SrvNat:
3607 pszName = "VBoxSrvNat";
3608 break;
3609 }
3610
3611 /*
3612 * Allocate the trunk interface.
3613 */
3614 PINTNETTRUNKIF pTrunkIF = (PINTNETTRUNKIF)RTMemAllocZ(sizeof(*pTrunkIF));
3615 if (!pTrunkIF)
3616 return VERR_NO_MEMORY;
3617 pTrunkIF->SwitchPort.u32Version = INTNETTRUNKSWPORT_VERSION;
3618 pTrunkIF->SwitchPort.pfnSetSGPhys = INTNET_CALLBACK(intnetR0TrunkIfPortSetSGPhys);
3619 pTrunkIF->SwitchPort.pfnRecv = INTNET_CALLBACK(intnetR0TrunkIfPortRecv);
3620 pTrunkIF->SwitchPort.pfnSGRetain = INTNET_CALLBACK(intnetR0TrunkIfPortSGRetain);
3621 pTrunkIF->SwitchPort.pfnSGRelease = INTNET_CALLBACK(intnetR0TrunkIfPortSGRelease);
3622 pTrunkIF->SwitchPort.u32VersionEnd = INTNETTRUNKSWPORT_VERSION;
3623 //pTrunkIF->pIfPort = NULL;
3624 pTrunkIF->pNetwork = pNetwork;
3625 //pTrunkIF->fPhysSG = false;
3626 //pTrunkIF->fPromiscuousWire = false;
3627 pTrunkIF->CachedMac.au8[0] = 0xfe;
3628 pTrunkIF->CachedMac.au8[1] = 0xff;
3629 pTrunkIF->CachedMac.au8[2] = 0xff;
3630 pTrunkIF->CachedMac.au8[3] = 0xff;
3631 pTrunkIF->CachedMac.au8[4] = 0xff;
3632 pTrunkIF->CachedMac.au8[5] = 0xff;
3633 int rc = RTSemFastMutexCreate(&pTrunkIF->FastMutex);
3634 if (RT_SUCCESS(rc))
3635 {
3636#ifdef IN_RING0 /* (testcase is ring-3) */
3637 /*
3638 * Query the factory we want, then use it create and connect the trunk.
3639 */
3640 PINTNETTRUNKFACTORY pTrunkFactory = NULL;
3641 rc = SUPR0ComponentQueryFactory(pSession, pszName, INTNETTRUNKFACTORY_UUID_STR, (void **)&pTrunkFactory);
3642 if (RT_SUCCESS(rc))
3643 {
3644 rc = pTrunkFactory->pfnCreateAndConnect(pTrunkFactory, pNetwork->szTrunk, &pTrunkIF->SwitchPort, &pTrunkIF->pIfPort);
3645 pTrunkFactory->pfnRelease(pTrunkFactory);
3646 if (RT_SUCCESS(rc))
3647 {
3648 Assert(pTrunkIF->pIfPort);
3649 pNetwork->pTrunkIF = pTrunkIF;
3650 Log(("intnetR0NetworkCreateTrunkIf: VINF_SUCCESS - pszName=%s szTrunk=%s%s Network=%s\n",
3651 pszName, pNetwork->szTrunk, pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE ? " shared-mac" : "", pNetwork->szName));
3652 return VINF_SUCCESS;
3653 }
3654 }
3655#endif /* IN_RING0 */
3656 RTSemFastMutexDestroy(pTrunkIF->FastMutex);
3657 }
3658 RTMemFree(pTrunkIF);
3659 LogFlow(("intnetR0NetworkCreateTrunkIf: %Rrc - pszName=%s szTrunk=%s Network=%s\n",
3660 rc, pszName, pNetwork->szTrunk, pNetwork->szName));
3661 return rc;
3662}
3663
3664
3665
3666/**
3667 * Close a network which was opened/created using intnetR0OpenNetwork()/intnetR0CreateNetwork().
3668 *
3669 * @param pNetwork The network to close.
3670 * @param pSession The session handle.
3671 */
3672static int intnetR0NetworkClose(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession)
3673{
3674 LogFlow(("intnetR0NetworkClose: pNetwork=%p pSession=%p\n", pNetwork, pSession));
3675 AssertPtrReturn(pSession, VERR_INVALID_PARAMETER);
3676 AssertPtrReturn(pNetwork, VERR_INVALID_PARAMETER);
3677
3678 int rc = SUPR0ObjRelease(pNetwork->pvObj, pSession);
3679 LogFlow(("intnetR0NetworkClose: return %Rrc\n", rc));
3680 return rc;
3681}
3682
3683
3684/**
3685 * Object destructor callback.
3686 * This is called for reference counted objectes when the count reaches 0.
3687 *
3688 * @param pvObj The object pointer.
3689 * @param pvUser1 Pointer to the network.
3690 * @param pvUser2 Pointer to the INTNET instance data.
3691 */
3692static DECLCALLBACK(void) intnetR0NetworkDestruct(void *pvObj, void *pvUser1, void *pvUser2)
3693{
3694 PINTNETNETWORK pNetwork = (PINTNETNETWORK)pvUser1;
3695 PINTNET pIntNet = (PINTNET)pvUser2;
3696 Log(("intnetR0NetworkDestruct: pvObj=%p pNetwork=%p pIntNet=%p %s\n", pvObj, pNetwork, pIntNet, pNetwork->szName));
3697 Assert(pNetwork->pIntNet == pIntNet);
3698
3699 /* take the create/destroy sem. */
3700 RTSemFastMutexRequest(pIntNet->FastMutex);
3701
3702 /*
3703 * Deactivate the trunk connection first (if any).
3704 */
3705 if (pNetwork->pTrunkIF)
3706 intnetR0TrunkIfActivate(pNetwork->pTrunkIF, false /* fActive */);
3707
3708 /*
3709 * Unlink the network.
3710 * Note that it needn't be in the list if we failed during creation.
3711 */
3712 PINTNETNETWORK pPrev = pIntNet->pNetworks;
3713 if (pPrev == pNetwork)
3714 pIntNet->pNetworks = pNetwork->pNext;
3715 else
3716 {
3717 for (; pPrev; pPrev = pPrev->pNext)
3718 if (pPrev->pNext == pNetwork)
3719 {
3720 pPrev->pNext = pNetwork->pNext;
3721 break;
3722 }
3723 }
3724 pNetwork->pNext = NULL;
3725 pNetwork->pvObj = NULL;
3726
3727 /*
3728 * Because of the undefined order of the per session object dereferencing when closing a session,
3729 * we have to handle the case where the network is destroyed before the interfaces. We'll
3730 * deal with this by simply orphaning the interfaces.
3731 */
3732 RTSemFastMutexRequest(pNetwork->FastMutex);
3733
3734 PINTNETIF pCur = pNetwork->pIFs;
3735 while (pCur)
3736 {
3737 PINTNETIF pNext = pCur->pNext;
3738 pCur->pNext = NULL;
3739 pCur->pNetwork = NULL;
3740 pCur = pNext;
3741 }
3742
3743 /* Grab and zap the trunk pointer before leaving the mutex. */
3744 PINTNETTRUNKIF pTrunkIF = pNetwork->pTrunkIF;
3745 pNetwork->pTrunkIF = NULL;
3746
3747 RTSemFastMutexRelease(pNetwork->FastMutex);
3748
3749 /*
3750 * If there is a trunk, delete it.
3751 * Note that this may tak a while if we're unlucky...
3752 */
3753 if (pTrunkIF)
3754 intnetR0TrunkIfDestroy(pTrunkIF, pNetwork);
3755
3756 /*
3757 * Free resources.
3758 */
3759 RTSemFastMutexDestroy(pNetwork->FastMutex);
3760 pNetwork->FastMutex = NIL_RTSEMFASTMUTEX;
3761 RTMemFree(pNetwork);
3762
3763 /* release the create/destroy sem. (can be done before trunk destruction.) */
3764 RTSemFastMutexRelease(pIntNet->FastMutex);
3765}
3766
3767
3768/**
3769 * Opens an existing network.
3770 *
3771 * @returns VBox status code.
3772 * @param pIntNet The instance data.
3773 * @param pSession The current session.
3774 * @param pszNetwork The network name. This has a valid length.
3775 * @param enmTrunkType The trunk type.
3776 * @param pszTrunk The trunk name. Its meaning is specfic to the type.
3777 * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
3778 * @param ppNetwork Where to store the pointer to the network on success.
3779 */
3780static int intnetR0OpenNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
3781 const char *pszTrunk, uint32_t fFlags, PINTNETNETWORK *ppNetwork)
3782{
3783 LogFlow(("intnetR0OpenNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
3784 pIntNet, pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, ppNetwork));
3785
3786 /* just pro forma validation, the caller is internal. */
3787 AssertPtr(pIntNet);
3788 AssertPtr(pSession);
3789 AssertPtr(pszNetwork);
3790 Assert(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End);
3791 AssertPtr(pszTrunk);
3792 Assert(!(fFlags & ~(INTNET_OPEN_FLAGS_MASK)));
3793 AssertPtr(ppNetwork);
3794 *ppNetwork = NULL;
3795
3796 /*
3797 * Search networks by name.
3798 */
3799 PINTNETNETWORK pCur;
3800 uint8_t cchName = strlen(pszNetwork);
3801 Assert(cchName && cchName < sizeof(pCur->szName)); /* caller ensures this */
3802
3803 pCur = pIntNet->pNetworks;
3804 while (pCur)
3805 {
3806 if ( pCur->cchName == cchName
3807 && !memcmp(pCur->szName, pszNetwork, cchName))
3808 {
3809 /*
3810 * Found the network, now check that we have the same ideas
3811 * about the trunk setup and security.
3812 */
3813 int rc;
3814 if ( enmTrunkType == kIntNetTrunkType_WhateverNone
3815 || ( pCur->enmTrunkType == enmTrunkType
3816 && !strcmp(pCur->szTrunk, pszTrunk)))
3817 {
3818 if (!((pCur->fFlags ^ fFlags) & INTNET_OPEN_FLAGS_COMPATIBILITY_XOR_MASK))
3819 {
3820
3821 /*
3822 * Increment the reference and check that the session
3823 * can access this network.
3824 */
3825 rc = SUPR0ObjAddRef(pCur->pvObj, pSession);
3826 if (RT_SUCCESS(rc))
3827 {
3828 if (!(pCur->fFlags & INTNET_OPEN_FLAGS_PUBLIC))
3829 rc = SUPR0ObjVerifyAccess(pCur->pvObj, pSession, pCur->szName);
3830 if (RT_SUCCESS(rc))
3831 {
3832 pCur->fFlags |= fFlags & INTNET_OPEN_FLAGS_SECURITY_OR_MASK;
3833
3834 *ppNetwork = pCur;
3835 }
3836 else
3837 SUPR0ObjRelease(pCur->pvObj, pSession);
3838 }
3839 else if (rc == VERR_WRONG_ORDER)
3840 rc = VERR_NOT_FOUND; /* destruction race, pretend the other isn't there. */
3841 }
3842 else
3843 rc = VERR_INTNET_INCOMPATIBLE_FLAGS;
3844 }
3845 else
3846 rc = VERR_INTNET_INCOMPATIBLE_TRUNK;
3847
3848 LogFlow(("intnetR0OpenNetwork: returns %Rrc *ppNetwork=%p\n", rc, *ppNetwork));
3849 return rc;
3850 }
3851 pCur = pCur->pNext;
3852 }
3853
3854 LogFlow(("intnetR0OpenNetwork: returns VERR_NOT_FOUND\n"));
3855 return VERR_NOT_FOUND;
3856}
3857
3858
3859/**
3860 * Creates a new network.
3861 *
3862 * The call must own the INTNET::FastMutex and has already attempted
3863 * opening the network and found it to be non-existing.
3864 *
3865 * @returns VBox status code.
3866 * @param pIntNet The instance data.
3867 * @param pSession The session handle.
3868 * @param pszNetwork The name of the network. This must be at least one character long and no longer
3869 * than the INTNETNETWORK::szName.
3870 * @param enmTrunkType The trunk type.
3871 * @param pszTrunk The trunk name. Its meaning is specfic to the type.
3872 * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
3873 * @param ppNetwork Where to store the network. In the case of failure whatever is returned
3874 * here should be dereferenced outside the INTNET::FastMutex.
3875 */
3876static int intnetR0CreateNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
3877 const char *pszTrunk, uint32_t fFlags, PINTNETNETWORK *ppNetwork)
3878{
3879 LogFlow(("intnetR0CreateNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
3880 pIntNet, pSession, pszNetwork, pszNetwork, enmTrunkType, pszTrunk, pszTrunk, fFlags, ppNetwork));
3881
3882 /* just pro forma validation, the caller is internal. */
3883 AssertPtr(pIntNet);
3884 AssertPtr(pSession);
3885 AssertPtr(pszNetwork);
3886 Assert(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End);
3887 AssertPtr(pszTrunk);
3888 Assert(!(fFlags & ~INTNET_OPEN_FLAGS_MASK));
3889 AssertPtr(ppNetwork);
3890 *ppNetwork = NULL;
3891
3892 /*
3893 * Allocate and initialize.
3894 */
3895 size_t cb = sizeof(INTNETNETWORK);
3896 if (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
3897 cb += INTNETNETWORK_TMP_SIZE + 64;
3898 PINTNETNETWORK pNew = (PINTNETNETWORK)RTMemAllocZ(cb);
3899 if (!pNew)
3900 return VERR_NO_MEMORY;
3901 int rc = RTSemFastMutexCreate(&pNew->FastMutex);
3902 if (RT_SUCCESS(rc))
3903 {
3904 //pNew->pIFs = NULL;
3905 pNew->pIntNet = pIntNet;
3906 //pNew->cActiveIFs = 0;
3907 pNew->fFlags = fFlags;
3908 size_t cchName = strlen(pszNetwork);
3909 pNew->cchName = cchName;
3910 Assert(cchName && cchName < sizeof(pNew->szName)); /* caller's responsibility. */
3911 memcpy(pNew->szName, pszNetwork, cchName); /* '\0' by alloc. */
3912 pNew->enmTrunkType = enmTrunkType;
3913 Assert(strlen(pszTrunk) < sizeof(pNew->szTrunk)); /* caller's responsibility. */
3914 strcpy(pNew->szTrunk, pszTrunk);
3915 if (fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
3916 pNew->pbTmp = RT_ALIGN_PT(pNew + 1, 64, uint8_t *);
3917 //else
3918 // pNew->pbTmp = NULL;
3919
3920 /*
3921 * Register the object in the current session and link it into the network list.
3922 */
3923 pNew->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK, intnetR0NetworkDestruct, pNew, pIntNet);
3924 if (pNew->pvObj)
3925 {
3926 pNew->pNext = pIntNet->pNetworks;
3927 pIntNet->pNetworks = pNew;
3928
3929 /*
3930 * Check if the current session is actually allowed to create and open
3931 * the network. It is possible to implement network name based policies
3932 * and these must be checked now. SUPR0ObjRegister does no such checks.
3933 */
3934 rc = SUPR0ObjVerifyAccess(pNew->pvObj, pSession, pNew->szName);
3935 if (RT_SUCCESS(rc))
3936 {
3937 /*
3938 * Connect the trunk.
3939 */
3940 rc = intnetR0NetworkCreateTrunkIf(pNew, pSession);
3941 if (RT_SUCCESS(rc))
3942 {
3943 *ppNetwork = pNew;
3944 LogFlow(("intnetR0CreateNetwork: returns VINF_SUCCESS *ppNetwork=%p\n", pNew));
3945 return VINF_SUCCESS;
3946 }
3947 }
3948
3949 /*
3950 * We unlink it here so it cannot be opened when the caller leaves
3951 * INTNET::FastMutex before dereferencing it.
3952 */
3953 Assert(pIntNet->pNetworks == pNew);
3954 pIntNet->pNetworks = pNew->pNext;
3955 pNew->pNext = NULL;
3956
3957 *ppNetwork = pNew;
3958 LogFlow(("intnetR0CreateNetwork: returns %Rrc\n", rc));
3959 return rc;
3960 }
3961 rc = VERR_NO_MEMORY;
3962
3963 RTSemFastMutexDestroy(pNew->FastMutex);
3964 pNew->FastMutex = NIL_RTSEMFASTMUTEX;
3965 }
3966 RTMemFree(pNew);
3967 LogFlow(("intnetR0CreateNetwork: returns %Rrc\n", rc));
3968 return rc;
3969}
3970
3971
3972/**
3973 * Opens a network interface and connects it to the specified network.
3974 *
3975 * @returns VBox status code.
3976 * @param pIntNet The internal network instance.
3977 * @param pSession The session handle.
3978 * @param pszNetwork The network name.
3979 * @param enmTrunkType The trunk type.
3980 * @param pszTrunk The trunk name. Its meaning is specfic to the type.
3981 * @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
3982 * @param fRestrictAccess Whether new participants should be subjected to access check or not.
3983 * @param cbSend The send buffer size.
3984 * @param cbRecv The receive buffer size.
3985 * @param phIf Where to store the handle to the network interface.
3986 */
3987INTNETR0DECL(int) INTNETR0Open(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork,
3988 INTNETTRUNKTYPE enmTrunkType, const char *pszTrunk, uint32_t fFlags,
3989 unsigned cbSend, unsigned cbRecv, PINTNETIFHANDLE phIf)
3990{
3991 LogFlow(("INTNETR0Open: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x cbSend=%u cbRecv=%u phIf=%p\n",
3992 pIntNet, pSession, pszNetwork, pszNetwork, pszTrunk, pszTrunk, enmTrunkType, fFlags, cbSend, cbRecv, phIf));
3993
3994 /*
3995 * Validate input.
3996 */
3997 AssertPtrReturn(pIntNet, VERR_INVALID_PARAMETER);
3998
3999 AssertPtrReturn(pszNetwork, VERR_INVALID_PARAMETER);
4000 const char *pszNetworkEnd = (const char *)memchr(pszNetwork, '\0', INTNET_MAX_NETWORK_NAME);
4001 AssertReturn(pszNetworkEnd, VERR_INVALID_PARAMETER);
4002 size_t cchNetwork = pszNetworkEnd - pszNetwork;
4003 AssertReturn(cchNetwork, VERR_INVALID_PARAMETER);
4004
4005 if (pszTrunk)
4006 {
4007 AssertPtrReturn(pszTrunk, VERR_INVALID_PARAMETER);
4008 const char *pszTrunkEnd = (const char *)memchr(pszTrunk, '\0', INTNET_MAX_TRUNK_NAME);
4009 AssertReturn(pszTrunkEnd, VERR_INVALID_PARAMETER);
4010 }
4011 else
4012 pszTrunk = "";
4013
4014 AssertMsgReturn(enmTrunkType > kIntNetTrunkType_Invalid && enmTrunkType < kIntNetTrunkType_End,
4015 ("%d\n", enmTrunkType), VERR_INVALID_PARAMETER);
4016 switch (enmTrunkType)
4017 {
4018 case kIntNetTrunkType_None:
4019 case kIntNetTrunkType_WhateverNone:
4020 AssertReturn(!*pszTrunk, VERR_INVALID_PARAMETER);
4021 break;
4022
4023 case kIntNetTrunkType_NetFlt:
4024 AssertReturn(pszTrunk, VERR_INVALID_PARAMETER);
4025 break;
4026
4027 default:
4028 return VERR_NOT_IMPLEMENTED;
4029 }
4030
4031 AssertMsgReturn(!(fFlags & ~INTNET_OPEN_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
4032 AssertPtrReturn(phIf, VERR_INVALID_PARAMETER);
4033
4034 /*
4035 * Acquire the mutex to serialize open/create.
4036 */
4037 int rc = RTSemFastMutexRequest(pIntNet->FastMutex);
4038 if (RT_FAILURE(rc))
4039 return rc;
4040
4041 /*
4042 * Try open / create the network and create an interface on it for the caller to use.
4043 *
4044 * Note that because of the destructors grabbing INTNET::FastMutex and us being required
4045 * to own this semaphore for the entire network opening / creation and interface creation
4046 * sequence, intnetR0CreateNetwork will have to defer the network cleanup to us on failure.
4047 */
4048 PINTNETNETWORK pNetwork = NULL;
4049 rc = intnetR0OpenNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
4050 if (RT_SUCCESS(rc) || rc == VERR_NOT_FOUND)
4051 {
4052 bool fCloseNetwork = true;
4053 if (rc == VERR_NOT_FOUND)
4054 rc = intnetR0CreateNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
4055 if (RT_SUCCESS(rc))
4056 rc = intnetR0NetworkCreateIf(pNetwork, pSession, cbSend, cbRecv, &fCloseNetwork, phIf);
4057
4058 RTSemFastMutexRelease(pIntNet->FastMutex);
4059
4060 if (RT_FAILURE(rc) && pNetwork && fCloseNetwork)
4061 intnetR0NetworkClose(pNetwork, pSession);
4062 }
4063 else
4064 RTSemFastMutexRelease(pIntNet->FastMutex);
4065
4066 LogFlow(("INTNETR0Open: return %Rrc *phIf=%RX32\n", rc, *phIf));
4067 return rc;
4068}
4069
4070
4071/**
4072 * VMMR0 request wrapper for GMMR0MapUnmapChunk.
4073 *
4074 * @returns see GMMR0MapUnmapChunk.
4075 * @param pIntNet The internal networking instance.
4076 * @param pSession The caller's session.
4077 * @param pReq The request packet.
4078 */
4079INTNETR0DECL(int) INTNETR0OpenReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETOPENREQ pReq)
4080{
4081 if (RT_UNLIKELY(pReq->Hdr.cbReq != sizeof(*pReq)))
4082 return VERR_INVALID_PARAMETER;
4083 return INTNETR0Open(pIntNet, pSession, &pReq->szNetwork[0], pReq->enmTrunkType, pReq->szTrunk,
4084 pReq->fFlags, pReq->cbSend, pReq->cbRecv, &pReq->hIf);
4085}
4086
4087
4088/**
4089 * Destroys an instance of the Ring-0 internal networking service.
4090 *
4091 * @param pIntNet Pointer to the instance data.
4092 */
4093INTNETR0DECL(void) INTNETR0Destroy(PINTNET pIntNet)
4094{
4095 LogFlow(("INTNETR0Destroy: pIntNet=%p\n", pIntNet));
4096
4097 /*
4098 * Allow NULL pointers.
4099 */
4100 if (!pIntNet)
4101 return;
4102 AssertPtrReturnVoid(pIntNet);
4103
4104 /*
4105 * There is not supposed to be any networks hanging around at this time.
4106 */
4107 Assert(pIntNet->pNetworks == NULL);
4108 if (pIntNet->FastMutex != NIL_RTSEMFASTMUTEX)
4109 {
4110 RTSemFastMutexDestroy(pIntNet->FastMutex);
4111 pIntNet->FastMutex = NIL_RTSEMFASTMUTEX;
4112 }
4113 if (pIntNet->hHtIfs != NIL_RTHANDLETABLE)
4114 {
4115 /** @todo does it make sense to have a deleter here? */
4116 RTHandleTableDestroy(pIntNet->hHtIfs, NULL, NULL);
4117 pIntNet->hHtIfs = NIL_RTHANDLETABLE;
4118 }
4119
4120 RTMemFree(pIntNet);
4121}
4122
4123
4124/**
4125 * Create an instance of the Ring-0 internal networking service.
4126 *
4127 * @returns VBox status code.
4128 * @param ppIntNet Where to store the instance pointer.
4129 */
4130INTNETR0DECL(int) INTNETR0Create(PINTNET *ppIntNet)
4131{
4132 LogFlow(("INTNETR0Create: ppIntNet=%p\n", ppIntNet));
4133 int rc = VERR_NO_MEMORY;
4134 PINTNET pIntNet = (PINTNET)RTMemAllocZ(sizeof(*pIntNet));
4135 if (pIntNet)
4136 {
4137 //pIntNet->pNetworks = NULL;
4138
4139 rc = RTSemFastMutexCreate(&pIntNet->FastMutex);
4140 if (RT_SUCCESS(rc))
4141 {
4142 rc = RTHandleTableCreateEx(&pIntNet->hHtIfs, RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT,
4143 UINT32_C(0x8ffe0000), 4096, intnetR0IfRetainHandle, NULL);
4144 if (RT_SUCCESS(rc))
4145 {
4146 *ppIntNet = pIntNet;
4147 LogFlow(("INTNETR0Create: returns VINF_SUCCESS *ppIntNet=%p\n", pIntNet));
4148 return VINF_SUCCESS;
4149 }
4150
4151 RTSemFastMutexDestroy(pIntNet->FastMutex);
4152 }
4153 RTMemFree(pIntNet);
4154 }
4155 *ppIntNet = NULL;
4156 LogFlow(("INTNETR0Create: returns %Rrc\n", rc));
4157 return rc;
4158}
4159
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