SrvIntNetR0.cpp revision 984d7b1e36f54578e08e05c5a129d9bd6f9bbc0a
/* $Id$ */
/** @file
* Internal networking - The ring 0 service.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SRV_INTNET
#include <VBox/intnetinline.h>
#include <VBox/pdmnetinline.h>
#include <iprt/handletable.h>
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def INTNET_WITH_DHCP_SNOOPING
* Enabled DHCP snooping when in shared-mac-on-the-wire mode. */
#define INTNET_WITH_DHCP_SNOOPING
/** The maximum number of interface in a network. */
/** The number of entries to grow the destination tables with. */
#if 0
# define INTNET_GROW_DSTTAB_SIZE 16
#else
# define INTNET_GROW_DSTTAB_SIZE 1
#endif
/** The wakeup bit in the INTNETIF::cBusy and INTNETRUNKIF::cBusy counters. */
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* MAC address lookup table entry.
*/
typedef struct INTNETMACTABENTRY
{
/** The MAC address of this entry. */
/** Is it promiscuous. */
bool fPromiscuous;
/** Is it active.
* We ignore the entry if this is clear and may end up sending packets addressed
* to this interface onto the trunk. The reasoning for this is that this could
* be the interface of a VM that just has been teleported to a different host. */
bool fActive;
/** Pointer to the network interface. */
/** Pointer to a MAC address lookup table entry. */
typedef INTNETMACTABENTRY *PINTNETMACTABENTRY;
/**
* MAC address lookup table.
*
* @todo Having this in a separate structure didn't work out as well as it
* should. Consider merging it into INTNETNETWORK.
*/
typedef struct INTNETMACTAB
{
/** The current number of entries. */
/** The number of entries we've allocated space for. */
/** Table entries. */
/** The host MAC address (reported). */
/** The host promisucous setting (reported). */
bool fHostPromiscuous;
/** Whether the host is active. */
bool fHostActive;
/** Whether the wire is promiscuous (config). */
bool fWirePromiscuous;
/** Whether the wire is active. */
bool fWireActive;
/** Pointer to the the trunk interface. */
struct INTNETTRUNKIF *pTrunk;
} INTNETMACTAB;
/** Pointer to a MAC address . */
typedef INTNETMACTAB *PINTNETMACTAB;
/**
* Destination table.
*/
typedef struct INTNETDSTTAB
{
/** The trunk destinations. */
/** Pointer to the trunk interface (referenced) if fTrunkDst is non-zero. */
struct INTNETTRUNKIF *pTrunk;
/** The number of destination interfaces. */
/** The interfaces (referenced). Variable sized array. */
struct
{
/** The destination interface. */
/** Whether to replace the destination MAC address.
* This is used when sharing MAC address with the host on the wire(less). */
bool fReplaceDstMac;
} aIfs[1];
} INTNETDSTTAB;
/** Pointer to a destination table. */
typedef INTNETDSTTAB *PINTNETDSTTAB;
/** Pointer to a const destination table. */
typedef INTNETDSTTAB const *PCINTNETDSTTAB;
/** Network layer address type. */
typedef enum INTNETADDRTYPE
{
/** The invalid 0 entry. */
/** IP version 4. */
/** IP version 6. */
/** IPX. */
/** The end of the valid values. */
/** The usual 32-bit hack. */
kIntNetAddrType_32BitHack = 0x7fffffff
/** Pointer to a network layer address type. */
typedef INTNETADDRTYPE *PINTNETADDRTYPE;
/**
* Address and type.
*/
typedef struct INTNETADDR
{
/** The address type. */
/** The address. */
} INTNETADDR;
/** Pointer to an address. */
typedef INTNETADDR *PINTNETADDR;
/** Pointer to a const address. */
typedef INTNETADDR const *PCINTNETADDR;
/**
* Address cache for a specific network layer.
*/
typedef struct INTNETADDRCACHE
{
/** Pointer to the table of addresses. */
/** The number of valid address entries. */
/** The number of allocated address entries. */
/** The address size. */
/** The size of an entry. */
/** Pointer to an address cache. */
typedef INTNETADDRCACHE *PINTNETADDRCACHE;
/** Pointer to a const address cache. */
typedef INTNETADDRCACHE const *PCINTNETADDRCACHE;
/**
* A network interface.
*
* Unless explicitly stated, all members are protect by the network semaphore.
*/
typedef struct INTNETIF
{
/** The MAC address.
* This is shadowed by INTNETMACTABENTRY::MacAddr. */
/** Set if the INTNET::MacAddr member has been explicitly set. */
bool fMacSet;
/** Set if the interface is in promiscuous mode.
* This is shadowed by INTNETMACTABENTRY::fPromiscuous. */
bool fPromiscuous;
/** Whether the interface is active or not.
* This is shadowed by INTNETMACTABENTRY::fActive. */
bool fActive;
/** Whether someone is currently in the destructor or has indicated that
* the end is nigh by means of IntNetR0IfAbortWait. */
bool volatile fDestroying;
/** Number of yields done to try make the interface read pending data.
* We will stop yielding when this reaches a threshold assuming that the VM is
* paused or that it simply isn't worth all the delay. It is cleared when a
* successful send has been done. */
/** Pointer to the current exchange buffer (ring-0). */
/** Pointer to ring-3 mapping of the current exchange buffer. */
/** Pointer to the default exchange buffer for the interface. */
/** Pointer to ring-3 mapping of the default exchange buffer. */
* waiting for data to arrive. */
RTSEMEVENT volatile hRecvEvent;
/** Number of threads sleeping on the event semaphore. */
/** The interface handle.
* When this is INTNET_HANDLE_INVALID a sleeper which is waking up
* should return with the appropriate error condition. */
INTNETIFHANDLE volatile hIf;
/** Pointer to the network this interface is connected to.
* This is protected by the INTNET::hMtxCreateOpenDestroy. */
struct INTNETNETWORK *pNetwork;
/** The session this interface is associated with. */
/** The SUPR0 object id. */
void *pvObj;
/** The network layer address cache. (Indexed by type, 0 entry isn't used.)
* This is protected by the address spinlock of the network. */
/** Spinlock protecting the input (producer) side of the receive ring. */
/** Busy count for tracking destination table references and active sends.
* Usually incremented while owning the switch table spinlock. The 30th bit
* is used to indicate wakeup. */
/** The preallocated destination table.
* This is NULL when it's in use as a precaution against unserialized
* transmitting. This is grown when new interfaces are added to the network. */
PINTNETDSTTAB volatile pDstTab;
/** Pointer to the trunk's per interface data. Can be NULL. */
void *pvIfData;
} INTNETIF;
/** Pointer to an internal network interface. */
/**
* A trunk interface.
*/
typedef struct INTNETTRUNKIF
{
/** The port interface we present to the component. */
/** The port interface we get from the component. */
/** Pointer to the network we're connect to.
* This may be NULL if we're orphaned? */
struct INTNETNETWORK *pNetwork;
/** The current MAC address for the interface. (reported)
* Updated while owning the switch table spinlock. */
/** Whether to supply physical addresses with the outbound SGs. (reported) */
bool fPhysSG;
/** Explicit alignment. */
bool fUnused;
/** Busy count for tracking destination table references and active sends.
* Usually incremented while owning the switch table spinlock. The 30th bit
* is used to indicate wakeup. */
/** Mask of destinations that pfnXmit cope with disabled preemption for. */
/** The GSO capabilities of the wire destination. (reported) */
/** The GSO capabilities of the host destination. (reported)
* This is as bit map where each bit represents the GSO type with the same
* number. */
/** Header buffer for when we're carving GSO frames. */
/** The destination table spinlock, interrupt safe.
* Protects apTaskDstTabs and apIntDstTabs. */
/** The number of entries in apIntDstTabs. */
/** The task time destination tables.
* @remarks intnetR0NetworkEnsureTabSpace and others ASSUMES this immediately
* preceeds apIntDstTabs so that these two tables can be used as one
* contiguous one. */
/** The interrupt / disabled-preemption time destination tables.
* This is a variable sized array. */
/** Pointer to a trunk interface. */
typedef INTNETTRUNKIF *PINTNETTRUNKIF;
/** Converts a pointer to INTNETTRUNKIF::SwitchPort to a PINTNETTRUNKIF. */
/**
* Internal representation of a network.
*/
typedef struct INTNETNETWORK
{
/** The Next network in the chain.
* This is protected by the INTNET::hMtxCreateOpenDestroy. */
struct INTNETNETWORK *pNext;
/** The spinlock protecting MacTab and INTNETTRUNKIF::aAddrCache.
* Interrupt safe. */
/** MAC address table.
* This doubles as interface collection. */
/** Wait for an interface to stop being busy so it can be removed or have its
* destination table replaced. We have to wait upon this while owning the
* network mutex. Will only ever have one waiter because of the big mutex. */
/** Pointer to the instance data. */
/** The SUPR0 object id. */
void *pvObj;
/** Pointer to the temporary buffer that is used when snooping fragmented packets.
* This is allocated after this structure if we're sharing the MAC address with
* the host. The buffer is INTNETNETWORK_TMP_SIZE big and aligned on a 64-byte boundrary. */
/** Network creation flags (INTNET_OPEN_FLAGS_*). */
/** The number of active interfaces (excluding the trunk). */
/** The length of the network name. */
/** The network name. */
char szName[INTNET_MAX_NETWORK_NAME];
/** The trunk type. */
/** The trunk name. */
char szTrunk[INTNET_MAX_TRUNK_NAME];
/** Pointer to an internal network. */
typedef INTNETNETWORK *PINTNETNETWORK;
/** The size of the buffer INTNETNETWORK::pbTmp points at. */
#define INTNETNETWORK_TMP_SIZE 2048
/**
* Internal networking instance.
*/
typedef struct INTNET
{
/** Magic number (INTNET_MAGIC). */
/** Mutex protecting the creation, opening and destruction of both networks and
* interfaces. (This means all operations affecting the pNetworks list.) */
/** List of networks. Protected by INTNET::Spinlock. */
PINTNETNETWORK volatile pNetworks;
/** Handle table for the interfaces. */
} INTNET;
/** Pointer to an internal network ring-0 instance. */
/** Magic number for the internal network instance data (Hayao Miyazaki). */
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Pointer to the internal network instance data. */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Worker for intnetR0SgWritePart that deals with the case where the
* request doesn't fit into the first segment.
*
* @returns true, unless the request or SG invalid.
* @param pSG The SG list to write to.
* @param off Where to start writing (offset into the SG).
* @param cb How much to write.
* @param pvBuf The buffer to containing the bits to write.
*/
{
return false;
/*
* Skip ahead to the segment where off starts.
*/
unsigned iSeg = 0;
{
}
/*
* Copy the data, hoping that it's all from one segment...
*/
else
{
/* copy the portion in the current segment. */
/* copy the portions in the other segments. */
do
{
iSeg++;
} while (cb > 0);
}
return true;
}
/**
* Writes to a part of an SG.
*
* @returns true on success, false on failure (out of bounds).
* @param pSG The SG list to write to.
* @param off Where to start writing (offset into the SG).
* @param cb How much to write.
* @param pvBuf The buffer to containing the bits to write.
*/
{
/* The optimized case. */
{
return true;
}
}
/**
* Reads a byte from a SG list.
*
* @returns The byte on success. 0xff on failure.
* @param pSG The SG list to read.
* @param off The offset (into the SG) off the byte.
*/
{
{
}
return false;
}
/**
* Worker for intnetR0SgReadPart that deals with the case where the
* requested data isn't in the first segment.
*
* @returns true, unless the SG is invalid.
* @param pSG The SG list to read.
* @param off Where to start reading (offset into the SG).
* @param cb How much to read.
* @param pvBuf The buffer to read into.
*/
{
return false;
/*
* Skip ahead to the segment where off starts.
*/
unsigned iSeg = 0;
{
}
/*
* Copy the data, hoping that it's all from one segment...
*/
else
{
/* copy the portion in the current segment. */
/* copy the portions in the other segments. */
do
{
iSeg++;
} while (cb > 0);
}
return true;
}
/**
* Reads a part of an SG into a buffer.
*
* @returns true on success, false on failure (out of bounds).
* @param pSG The SG list to read.
* @param off Where to start reading (offset into the SG).
* @param cb How much to read.
* @param pvBuf The buffer to read into.
*/
{
/* The optimized case. */
{
return true;
}
}
/**
* Wait for a busy counter to reach zero.
*
* @param pNetwork The network.
* @param pcBusy The busy counter.
*/
{
if (ASMAtomicReadU32(pcBusy) == 0)
return;
/*
* We have to be a bit cautious here so we don't destroy the network or the
* semaphore before intnetR0BusyDec has signalled us.
*/
/* Reset the semaphore and flip the wakeup bit. */
do
{
if (cCurBusy == 0)
return;
/* Wait for the count to reach zero. */
do
{
//AssertMsg(RT_SUCCESS(rc2), ("rc=%Rrc *pcBusy=%#x (%#x)\n", rc2, ASMAtomicReadU32(pcBusy), cCurBusy ));
} while ( cCurBusy != INTNET_BUSY_WAKEUP_MASK
}
/**
* Decrements the busy counter and maybe wakes up any threads waiting for it to
* reach zero.
*
* @param pNetwork The network.
* @param pcBusy The busy counter.
*/
{
&& pNetwork))
}
/**
* Increments the busy count of the specified interface.
*
* The caller must own the MAC address table spinlock.
*
* @param pIf The interface.
*/
{
}
/**
* Increments the busy count of the specified interface.
*
* The caller must own the MAC address table spinlock or an explicity reference.
*
* @param pTrunk The trunk.
*/
{
}
/**
* Increments the busy count of the specified interface.
*
* The caller must own the MAC address table spinlock or an explicity reference.
*
* @param pIf The interface.
*/
{
}
/**
* Increments the busy count of the specified interface.
*
* The caller must own the MAC address table spinlock or an explicity reference.
*
* @param pTrunk The trunk.
*/
{
}
/**
* Retain an interface.
*
* @returns VBox status code, can assume success in most situations.
* @param pIf The interface instance.
* @param pSession The current session.
*/
{
return VINF_SUCCESS;
}
/**
* Release an interface previously retained by intnetR0IfRetain or
*
* @returns true if destroyed, false if not.
* @param pIf The interface instance.
* @param pSession The current session.
*/
{
return rc == VINF_OBJECT_DESTROYED;
}
/**
* RTHandleCreateEx callback that retains an object in the
* handle table before returning it.
*
* (Avoids racing the freeing of the handle.)
*
* @returns VBox status code.
* @param hHandleTable The handle table (ignored).
* @param pvObj The object (INTNETIF).
* @param pvCtx The context (SUPDRVSESSION).
* @param pvUser The user context (ignored).
*/
static DECLCALLBACK(int) intnetR0IfRetainHandle(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
{
return VINF_SUCCESS;
}
/**
* Checks if the interface has a usable MAC address or not.
*
* @returns true if MacAddr is usable, false if not.
* @param pIf The interface.
*/
{
}
/**
* Locates the MAC address table entry for the given interface.
*
* The caller holds the MAC address table spinlock, obviously.
*
* @returns Pointer to the entry on if found, NULL if not.
* @param pNetwork The network.
* @param pIf The interface.
*/
DECLINLINE(PINTNETMACTABENTRY) intnetR0NetworkFindMacAddrEntry(PINTNETNETWORK pNetwork, PINTNETIF pIf)
{
while (iIf-- > 0)
{
}
return NULL;
}
/**
* Checks if the IPv4 address is a broadcast address.
* @param Addr The address, network endian.
*/
{
/* Just check for 255.255.255.255 atm. */
return Addr.u == UINT32_MAX;
}
/**
* Checks if the IPv4 address is a good interface address.
* @param Addr The address, network endian.
*/
{
/* Usual suspects. */
return false;
/* Unusual suspects. */
))
return false;
return true;
}
/**
* Gets the address size of a network layer type.
*
* @returns size in bytes.
* @param enmType The type.
*/
{
switch (enmType)
{
case kIntNetAddrType_IPv4: return 4;
case kIntNetAddrType_IPv6: return 16;
default: AssertFailedReturn(0);
}
}
/**
* Compares two address to see if they are equal, assuming naturally align structures.
*
* @returns true if equal, false if not.
* @param pAddr1 The first address.
* @param pAddr2 The second address.
* @param cbAddr The address size.
*/
DECLINLINE(bool) intnetR0AddrUIsEqualEx(PCRTNETADDRU pAddr1, PCRTNETADDRU pAddr2, uint8_t const cbAddr)
{
switch (cbAddr)
{
case 4: /* IPv4 */
case 16: /* IPv6 */
case 10: /* IPX */
default:
AssertFailedReturn(false);
}
}
/**
* Worker for intnetR0IfAddrCacheLookup that performs the lookup
* in the remaining cache entries after the caller has check the
* most likely ones.
*
* @returns -1 if not found, the index of the cache entry if found.
* @param pCache The cache.
* @param pAddr The address.
* @param cbAddr The address size (optimization).
*/
static int intnetR0IfAddrCacheLookupSlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
{
while (i >= 1)
{
return i;
i--;
}
return -1;
}
/**
* Lookup an address in a cache without any expectations.
*
* @returns -1 if not found, the index of the cache entry if found.
* @param pCache The cache.
* @param pAddr The address.
* @param cbAddr The address size (optimization).
*/
DECLINLINE(int) intnetR0IfAddrCacheLookup(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
{
/*
* The optimized case is when there is one cache entry and
* it doesn't match.
*/
if ( i > 0
return 0;
if (i <= 1)
return -1;
/*
* Check the last entry.
*/
i--;
return i;
if (i <= 1)
return -1;
}
/** Same as intnetR0IfAddrCacheLookup except we expect the address to be present already. */
DECLINLINE(int) intnetR0IfAddrCacheLookupLikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
{
/** @todo implement this. */
}
/**
* Worker for intnetR0IfAddrCacheLookupUnlikely that performs
* the lookup in the remaining cache entries after the caller
* has check the most likely ones.
*
* The routine is expecting not to find the address.
*
* @returns -1 if not found, the index of the cache entry if found.
* @param pCache The cache.
* @param pAddr The address.
* @param cbAddr The address size (optimization).
*/
static int intnetR0IfAddrCacheInCacheUnlikelySlow(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
{
/*
* Perform a full table lookup.
*/
while (i >= 1)
{
return i;
i--;
}
return -1;
}
/**
* Lookup an address in a cache expecting not to find it.
*
* @returns -1 if not found, the index of the cache entry if found.
* @param pCache The cache.
* @param pAddr The address.
* @param cbAddr The address size (optimization).
*/
DECLINLINE(int) intnetR0IfAddrCacheLookupUnlikely(PCINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr)
{
/*
* The optimized case is when there is one cache entry and
* it doesn't match.
*/
if (RT_UNLIKELY( i > 0
return 0;
if (RT_LIKELY(i <= 1))
return -1;
/*
* Then check the last entry and return if there are just two cache entries.
*/
i--;
if (RT_UNLIKELY(intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr)))
return i;
if (i <= 1)
return -1;
}
/**
* Deletes a specific cache entry.
*
* Worker for intnetR0NetworkAddrCacheDelete and intnetR0NetworkAddrCacheDeleteMinusIf.
*
* @param pIf The interface (for logging).
* @param pCache The cache.
* @param iEntry The entry to delete.
* @param pszMsg Log message.
*/
static void intnetR0IfAddrCacheDeleteIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, int iEntry, const char *pszMsg)
{
AssertReturnVoid(iEntry >= 0);
#ifdef LOG_ENABLED
switch (enmAddrType)
{
case kIntNetAddrType_IPv4:
Log(("intnetR0IfAddrCacheDeleteIt: hIf=%#x MAC=%.6Rhxs IPv4 added #%d %d.%d.%d.%d %s\n",
pIf->hIf, &pIf->MacAddr, iEntry, pAddr->au8[0], pAddr->au8[1], pAddr->au8[2], pAddr->au8[3], pszMsg));
break;
default:
Log(("intnetR0IfAddrCacheDeleteIt: hIf=%RX32 MAC=%.6Rhxs type=%d #%d %.*Rhxs %s\n",
break;
}
#endif
}
/**
* Deletes an address from the cache, assuming it isn't actually in the cache.
*
* May or may not own the spinlock when calling this.
*
* @param pIf The interface (for logging).
* @param pCache The cache.
* @param pAddr The address.
* @param cbAddr The address size (optimization).
*/
DECLINLINE(void) intnetR0IfAddrCacheDelete(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
{
if (RT_UNLIKELY(i >= 0))
}
/**
* Deletes the address from all the interface caches.
*
* This is used to remove stale entries that has been reassigned to
* other machines on the network.
*
* @param pNetwork The network.
* @param pAddr The address.
* @param enmType The address type.
* @param cbAddr The address size (optimization).
* @param pszMsg Log message.
*/
DECLINLINE(void) intnetR0NetworkAddrCacheDelete(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType,
{
while (iIf--)
{
if (RT_UNLIKELY(i >= 0))
}
}
/**
* Deletes the address from all the interface caches except the specified one.
*
* This is used to remove stale entries that has been reassigned to
* other machines on the network.
*
* @param pNetwork The network.
* @param pAddr The address.
* @param enmType The address type.
* @param cbAddr The address size (optimization).
* @param pszMsg Log message.
*/
DECLINLINE(void) intnetR0NetworkAddrCacheDeleteMinusIf(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCRTNETADDRU pAddr,
{
while (iIf--)
{
{
if (RT_UNLIKELY(i >= 0))
}
}
}
/**
* Lookup an address on the network, returning the (first) interface having it
* in its address cache.
*
* @returns Pointer to the interface on success, NULL if not found. The caller
* must release the interface by calling intnetR0BusyDecIf.
* @param pNetwork The network.
* @param pAddr The address to lookup.
* @param enmType The address type.
* @param cbAddr The size of the address.
*/
DECLINLINE(PINTNETIF) intnetR0NetworkAddrCacheLookupIf(PINTNETNETWORK pNetwork, PCRTNETADDRU pAddr, INTNETADDRTYPE const enmType, uint8_t const cbAddr)
{
while (iIf--)
{
if (i >= 0)
{
return pIf;
}
}
return NULL;
}
/**
* Adds an address to the cache, the caller is responsible for making sure it's
* not already in the cache.
*
* The caller must not
*
* @param pIf The interface (for logging).
* @param pCache The address cache.
* @param pAddr The address.
* @param pszMsg log message.
*/
static void intnetR0IfAddrCacheAddIt(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, const char *pszMsg)
{
{
/* This shouldn't happen*/
return;
}
/* When the table is full, drop the older entry (FIFO). Do proper ageing? */
{
Log(("intnetR0IfAddrCacheAddIt: type=%d replacing %.*Rhxs\n",
memmove(pCache->pbEntries, pCache->pbEntries + pCache->cbEntry, pCache->cbEntry * (pCache->cEntries - 1));
}
/*
* Add the new entry to the end of the array.
*/
#ifdef LOG_ENABLED
switch (enmAddrType)
{
case kIntNetAddrType_IPv4:
Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs IPv4 added #%d %d.%d.%d.%d %s\n",
pIf->hIf, &pIf->MacAddr, pCache->cEntries, pAddr->au8[0], pAddr->au8[1], pAddr->au8[2], pAddr->au8[3], pszMsg));
break;
default:
Log(("intnetR0IfAddrCacheAddIt: hIf=%#x MAC=%.6Rhxs type=%d added #%d %.*Rhxs %s\n",
break;
}
#endif
}
/**
* A intnetR0IfAddrCacheAdd worker that performs the rest of the lookup.
*
* @param pIf The interface (for logging).
* @param pCache The address cache.
* @param pAddr The address.
* @param cbAddr The size of the address (optimization).
* @param pszMsg Log message.
*/
static void intnetR0IfAddrCacheAddSlow(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCRTNETADDRU pAddr, uint8_t const cbAddr, const char *pszMsg)
{
/*
* Check all but the first and last entries, the caller
* has already checked those.
*/
while (i >= 1)
{
return;
i--;
}
/*
* Not found, add it.
*/
}
/**
* Adds an address to the cache if it's not already there.
*
* Must not own any spinlocks when calling this function.
*
* @param pIf The interface (for logging).
* @param pCache The address cache.
* @param pAddr The address.
* @param cbAddr The size of the address (optimization).
* @param pszMsg Log message.
*/
{
/*
* The optimized case is when the address the first or last cache entry.
*/
if (RT_LIKELY( i > 0
|| (i > 1
&& intnetR0AddrUIsEqualEx((PCRTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))) ))
return;
}
/**
* Destroys the specified address cache.
* @param pCache The address cache.
*/
{
pCache->cEntriesAlloc = 0;
}
/**
* Initialize the address cache for the specified address type.
*
* The cache storage is preallocated and fixed size so that we can handle
* inserts from problematic contexts.
*
* @returns VINF_SUCCESS or VERR_NO_MEMORY.
* @param pCache The cache to initialize.
* @param enmAddrType The address type.
* @param fEnabled Whether the address cache is enabled or not.
*/
static int intnetR0IfAddrCacheInit(PINTNETADDRCACHE pCache, INTNETADDRTYPE enmAddrType, bool fEnabled)
{
if (fEnabled)
{
return VERR_NO_MEMORY;
}
else
{
pCache->cEntriesAlloc = 0;
}
return VINF_SUCCESS;
}
/**
* Is it a multicast or broadcast MAC address?
*
* @returns true if multicast, false if not.
* @param pMacAddr The address to inspect.
*/
{
}
/**
* Is it a dummy MAC address?
*
* We use dummy MAC addresses for interfaces which we don't know the MAC
* address of because they haven't sent anything (learning) or explicitly set
* it.
*
* @returns true if dummy, false if not.
* @param pMacAddr The address to inspect.
*/
{
/* The dummy address are broadcast addresses, don't bother check it all. */
}
/**
* Compares two MAC addresses.
*
* @returns true if equal, false if not.
* @param pDstAddr1 Address 1.
* @param pDstAddr2 Address 2.
*/
{
}
/**
* Switch a unicast frame based on the network layer address (OSI level 3) and
* return a destination table.
*
* @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
* INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
* @param pNetwork The network to switch on.
* @param pDstMacAddr The destination MAC address.
* @param enmL3AddrType The level-3 destination address type.
* @param pL3Addr The level-3 destination address.
* @param cbL3Addr The size of the level-3 destination address.
* @param fSrc The frame source (INTNETTRUNKDIR_WIRE).
* @param pDstTab The destination output table.
*/
{
/*
* Grab the spinlock first and do the switching.
*/
/* Find exactly matching or promiscuous interfaces. */
uint32_t cExactHits = 0;
while (iIfMac-- > 0)
{
{
{
cExactHits += fExact;
}
}
}
/* Does it match the host, or is the host promiscuous? */
if (pTab->fHostActive)
{
if ( fExact
|| pTab->fHostPromiscuous)
{
cExactHits += fExact;
}
}
/* Hit the wire if there are no exact matches or if it's in promiscuous mode. */
{
}
}
/**
* Pre-switch a unicast MAC address.
*
* @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
* INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
* @param pNetwork The network to switch on.
* @param fSrc The frame source.
* @param pSrcAddr The source address of the frame.
* @param pDstAddr The destination address of the frame.
*/
static INTNETSWDECISION intnetR0NetworkPreSwitchUnicast(PINTNETNETWORK pNetwork, uint32_t fSrc, PCRTMAC pSrcAddr,
{
/*
* Grab the spinlock first and do the switching.
*/
/* Iterate the internal network interfaces and look for matching source and
destination addresses. */
uint32_t cExactHits = 0;
while (iIfMac-- > 0)
{
{
/* Unknown interface address? */
break;
/* Paranoia - this shouldn't happen, right? */
if ( pSrcAddr
break;
/* Exact match? */
{
break;
}
}
}
return enmSwDecision;
}
/**
* Switch a unicast MAC address and return a destination table.
*
* @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
* INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
* @param pNetwork The network to switch on.
* @param fSrc The frame source.
* @param pIfSender The sender interface, NULL if trunk. Used to
* prevent sending an echo to the sender.
* @param pDstAddr The destination address of the frame.
* @param pDstTab The destination output table.
*/
static INTNETSWDECISION intnetR0NetworkSwitchUnicast(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETIF pIfSender,
{
/*
* Grab the spinlock first and do the switching.
*/
/* Find exactly matching or promiscuous interfaces. */
uint32_t cExactHits = 0;
while (iIfMac-- > 0)
{
{
if ( fExact
{
cExactHits += fExact;
{
}
}
}
}
/* Does it match the host, or is the host promiscuous? */
if ( fSrc != INTNETTRUNKDIR_HOST
&& pTab->fHostActive)
{
if ( fExact
|| pTab->fHostPromiscuous)
{
cExactHits += fExact;
}
}
/* Hit the wire if there are no exact matches or if it's in promiscuous mode. */
if ( fSrc != INTNETTRUNKDIR_WIRE
&& pTab->fWireActive
)
/* Grab the trunk if we're sending to it. */
{
}
}
/**
* Create a destination table for a broadcast frame.
*
* @returns INTNETSWDECISION_BROADCAST.
* @param pNetwork The network to switch on.
* @param fSrc The frame source.
* @param pIfSender The sender interface, NULL if trunk. Used to
* prevent sending an echo to the sender.
* @param pDstTab The destination output table.
*/
static INTNETSWDECISION intnetR0NetworkSwitchBroadcast(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETIF pIfSender,
{
/*
* Grab the spinlock first and record all active interfaces.
*/
/* Regular interfaces. */
while (iIfMac-- > 0)
{
{
{
}
}
}
/* The trunk interface. */
if (pTab->fHostActive)
if (pTab->fWireActive)
{
}
return INTNETSWDECISION_BROADCAST;
}
/**
* Create a destination table with the trunk and any promiscuous interfaces.
*
* This is only used in a fallback case of the level-3 switching, so we can
* assume the wire as source and skip the sender interface filtering.
*
* @returns INTNETSWDECISION_DROP, INTNETSWDECISION_TRUNK,
* INTNETSWDECISION_INTNET or INTNETSWDECISION_BROADCAST (misnomer).
* @param pNetwork The network to switch on.
* @param fSrc The frame source.
* @param pDstTab The destination output table.
*/
static INTNETSWDECISION intnetR0NetworkSwitchTrunkAndPromisc(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETDSTTAB pDstTab)
{
/*
* Grab the spinlock first and do the switching.
*/
/* Find promiscuous interfaces. */
while (iIfMac-- > 0)
{
{
}
}
/* The trunk interface. */
if (pTab->fHostActive)
if (pTab->fWireActive)
{
}
}
/**
* Create a destination table for a trunk frame.
*
* @returns INTNETSWDECISION_BROADCAST.
* @param pNetwork The network to switch on.
* @param fSrc The frame source.
* @param pDstTab The destination output table.
*/
static INTNETSWDECISION intnetR0NetworkSwitchTrunk(PINTNETNETWORK pNetwork, uint32_t fSrc, PINTNETDSTTAB pDstTab)
{
/*
* Grab the spinlock first and record all active interfaces.
*/
/* The trunk interface. */
if (pTab->fHostActive)
if (pTab->fWireActive)
{
}
}
/**
* Wrapper around RTMemAlloc for allocating a destination table.
*
* @returns VINF_SUCCESS or VERR_NO_MEMORY.
* @param cEntries The size given as an entry count.
* @param ppDstTab Where to store the pointer (always).
*/
{
if (RT_UNLIKELY(!pDstTab))
return VERR_NO_MEMORY;
return VINF_SUCCESS;
}
/**
* Ensures that there is space for another interface in the MAC address lookup
* table as well as all the destination tables.
*
*
* @returns VINF_SUCCESS, VERR_NO_MEMORY or VERR_OUT_OF_RANGE.
* @param pNetwork The network to operate on.
*/
{
/*
* The cEntries and cEntriesAllocated members are only updated while
* owning the big mutex, so we only need the spinlock when doing the
* actual table replacing.
*/
int rc = VINF_SUCCESS;
{
if (cAllocated <= INTNET_MAX_IFS)
{
/*
* Resize the destination tables first, this can be kind of tedious.
*/
{
if (RT_FAILURE(rc))
break;
for (;;)
{
if ( pOld
{
break;
}
}
}
/*
* The trunk.
*/
if ( RT_SUCCESS(rc)
{
ppDstTab++)
{
if (RT_FAILURE(rc))
break;
for (;;)
{
if (pvOld)
if (pvOld)
{
break;
}
}
}
}
/*
* The MAC Address table itself.
*/
if (RT_SUCCESS(rc))
{
if (paNew)
{
while (i-- > 0)
{
}
}
else
rc = VERR_NO_MEMORY;
}
}
else
}
return rc;
}
#ifdef INTNET_WITH_DHCP_SNOOPING
/**
* Snoops IP assignments and releases from the DHCPv4 traffic.
*
* The caller is responsible for making sure this traffic between the
* BOOTPS and BOOTPC ports and validate the IP header. The UDP packet
* need not be validated beyond the ports.
*
* @param pNetwork The network this frame was seen on.
* @param pIpHdr Pointer to a valid IP header. This is for pseudo
* header validation, so only the minimum header size
* needs to be available and valid here.
* @param pUdpHdr Pointer to the UDP header in the frame.
* @param cbUdpPkt What's left of the frame when starting at the UDP header.
* @param fGso Set if this is a GSO frame, clear if regular.
*/
static void intnetR0NetworkSnoopDhcp(PINTNETNETWORK pNetwork, PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, uint32_t cbUdpPkt)
{
/*
* Check if the DHCP message is valid and get the type.
*/
{
Log6(("Bad UDP packet\n"));
return;
}
{
Log6(("Bad DHCP packet\n"));
return;
}
#ifdef LOG_ENABLED
/*
* Log it.
*/
const char *pszType = "unknown";
switch (MsgType)
{
}
Log6(("DHCP msg: %d (%s) client %.6Rhxs ciaddr=%d.%d.%d.%d yiaddr=%d.%d.%d.%d\n", MsgType, pszType, &pDhcp->bp_chaddr,
pDhcp->bp_yiaddr.au8[0], pDhcp->bp_yiaddr.au8[1], pDhcp->bp_yiaddr.au8[2], pDhcp->bp_yiaddr.au8[3]));
#endif /* LOG_EANBLED */
/*
* Act upon the message.
*/
switch (MsgType)
{
#if 0
case RTNET_DHCP_MT_REQUEST:
/** @todo Check for valid non-broadcast requests w/ IP for any of the MACs we
* know, and add the IP to the cache. */
break;
#endif
/*
* Lookup the interface by its MAC address and insert the IPv4 address into the cache.
* Delete the old client address first, just in case it changed in a renewal.
*/
case RTNET_DHCP_MT_ACK:
{
while (iIf-- > 0)
{
if ( intnetR0IfHasMacAddr(pCur)
{
if (!pMatchingIf)
{
pMatchingIf = pCur;
}
}
}
if (pMatchingIf)
{
}
}
return;
/*
* Lookup the interface by its MAC address and remove the IPv4 address(es) from the cache.
*/
case RTNET_DHCP_MT_RELEASE:
{
while (iIf-- > 0)
{
if ( intnetR0IfHasMacAddr(pCur)
{
}
}
break;
}
}
}
/**
* Worker for intnetR0TrunkIfSnoopAddr that takes care of what
* is likely to be a DHCP message.
*
* The caller has already check that the UDP source and destination ports
* are BOOTPS or BOOTPC.
*
* @param pNetwork The network this frame was seen on.
* @param pSG The gather list for the frame.
*/
{
/*
* Get a pointer to a linear copy of the full packet, using the
* temporary buffer if necessary.
*/
{
return;
//pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
}
/*
* Validate the IP header and find the UDP packet.
*/
if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, pSG->cbTotal - sizeof(RTNETETHERHDR), true /*fChecksum*/))
{
Log(("intnetR0TrunkIfSnoopDhcp: bad ip header\n"));
return;
}
/*
* Hand it over to the common DHCP snooper.
*/
intnetR0NetworkSnoopDhcp(pNetwork, pIpHdr, (PCRTNETUDP)((uintptr_t)pIpHdr + cbIpHdr), cbPacket - cbIpHdr);
}
#endif /* INTNET_WITH_DHCP_SNOOPING */
/**
* Snoops up source addresses from ARP requests and purge these from the address
* caches.
*
* The purpose of this purging is to get rid of stale addresses.
*
* @param pNetwork The network this frame was seen on.
* @param pSG The gather list for the frame.
*/
{
/*
* Check the minimum size first.
*/
return;
/*
* Copy to temporary buffer if necessary.
*/
{
return;
}
/*
* Ignore packets which doesn't interest us or we perceive as malformed.
*/
return;
&& ar_oper != RTNET_ARPOP_REPLY))
{
return;
}
/*
* Delete the source address if it's OK.
*/
{
}
}
#ifdef INTNET_WITH_DHCP_SNOOPING
/**
* Snoop up addresses from ARP and DHCP traffic from frames comming
* over the trunk connection.
*
* The caller is responsible for do some basic filtering before calling
* this function.
* For IPv4 this means checking against the minimum DHCPv4 frame size.
*
* @param pNetwork The network.
* @param pSG The SG list for the frame.
* @param EtherType The Ethertype of the frame.
*/
{
switch (EtherType)
{
case RTNET_ETHERTYPE_IPV4:
{
uint8_t b;
Assert(pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN);
{
/* check if the protocol is UDP */
return;
/* get the TCP header length */
}
else
{
/* check if the protocol is UDP */
return;
/* get the TCP header length */
}
if (cbIpHdr < RTNETIPV4_MIN_LEN)
return;
/* compare the ports. */
{
PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t const *)pSG->aSegs[0].pv + sizeof(RTNETETHERHDR) + cbIpHdr);
return;
}
else
{
/* get the lower byte of the UDP source port number. */
if ( b != RTNETIPV4_PORT_BOOTPS
&& b != RTNETIPV4_PORT_BOOTPC)
return;
if (b)
return;
/* get the lower byte of the UDP destination port number. */
if ( b != RTNETIPV4_PORT_BOOTPS
&& b != RTNETIPV4_PORT_BOOTPC)
return;
if (b == SrcPort)
return;
if (b)
return;
}
break;
}
case RTNET_ETHERTYPE_IPV6:
{
/** @todo IPv6: Check for ICMPv6. It looks like type 133 (Router solicitation) might
* need to be edited. Check out how NDP works... */
break;
}
case RTNET_ETHERTYPE_ARP:
break;
}
}
#endif /* INTNET_WITH_DHCP_SNOOPING */
/**
* Deals with an IPv4 packet.
*
* This will fish out the source IP address and add it to the cache.
* Then it will look for DHCPRELEASE requests (?) and anything else
* that we migh find useful later.
*
* @param pIf The interface that's sending the frame.
* @param pIpHdr Pointer to the IPv4 header in the frame.
* @param cbPacket The size of the packet, or more correctly the
* size of the frame without the ethernet header.
* @param fGso Set if this is a GSO frame, clear if regular.
*/
static void intnetR0IfSnoopIPv4SourceAddr(PINTNETIF pIf, PCRTNETIPV4 pIpHdr, uint32_t cbPacket, bool fGso)
{
/*
* Check the header size first to prevent access invalid data.
*/
if (cbPacket < RTNETIPV4_MIN_LEN)
return;
if ( cbHdr < RTNETIPV4_MIN_LEN
return;
/*
* If the source address is good (not broadcast or my network) and
* not already in the address cache of the sender, add it. Validate
* the IP header before adding it.
*/
bool fValidatedIpHdr = false;
&& intnetR0IfAddrCacheLookupLikely(&pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, sizeof(Addr.IPv4)) < 0)
{
{
Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header\n"));
return;
}
fValidatedIpHdr = true;
}
#ifdef INTNET_WITH_DHCP_SNOOPING
/*
* Check for potential DHCP packets.
*/
&& !fGso) /* GSO is not applicable to DHCP traffic. */
{
{
if ( fValidatedIpHdr
else
Log(("intnetR0IfSnoopIPv4SourceAddr: bad ip header (dhcp)\n"));
}
}
#endif /* INTNET_WITH_DHCP_SNOOPING */
}
/**
* Snoop up source addresses from an ARP request or reply.
*
* @param pIf The interface that's sending the frame.
* @param pHdr The ARP header.
* @param cbPacket The size of the packet (migth be larger than the ARP
* request 'cause of min ethernet frame size).
* @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
* don't have to repeat the frame parsing in intnetR0TrunkIfSend.
*/
static void intnetR0IfSnoopArpAddr(PINTNETIF pIf, PCRTNETARPIPV4 pArpIPv4, uint32_t cbPacket, uint16_t *pfSgFlags)
{
/*
* Ignore packets which doesn't interest us or we perceive as malformed.
*/
return;
return;
&& ar_oper != RTNET_ARPOP_REPLY))
{
return;
}
/*
* Tag the SG as ARP IPv4 for later editing, then check for addresses
* which can be removed or added to the address cache of the sender.
*/
if ( ar_oper == RTNET_ARPOP_REPLY
}
/**
* Checks packets send by a normal interface for new network
* layer addresses.
*
* @param pIf The interface that's sending the frame.
* @param pbFrame The frame.
* @param cbFrame The size of the frame.
* @param fGso Set if this is a GSO frame, clear if regular.
* @param pfSgFlags Pointer to the SG flags. This is used to tag the packet so we
* don't have to repeat the frame parsing in intnetR0TrunkIfSend.
*/
static void intnetR0IfSnoopAddr(PINTNETIF pIf, uint8_t const *pbFrame, uint32_t cbFrame, bool fGso, uint16_t *pfSgFlags)
{
/*
* Fish out the ethertype and look for stuff we can handle.
*/
if (cbFrame <= sizeof(RTNETETHERHDR))
return;
cbFrame -= sizeof(RTNETETHERHDR);
switch (EtherType)
{
case RTNET_ETHERTYPE_IPV4:
break;
#if 0 /** @todo IntNet: implement IPv6 for wireless MAC sharing. */
case RTNET_ETHERTYPE_IPV6:
/** @todo IPv6: Check for ICMPv6. It looks like type 133 (Router solicitation) might
* need to be edited. Check out how NDP works... */
intnetR0IfSnoopIPv6SourceAddr(pIf, (PCINTNETIPV6)((PCRTNETETHERHDR)pbFrame + 1), cbFrame, fGso, pfSgFlags);
break;
#endif
#if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
case RTNET_ETHERTYPE_IPX_1:
case RTNET_ETHERTYPE_IPX_2:
case RTNET_ETHERTYPE_IPX_3:
break;
#endif
case RTNET_ETHERTYPE_ARP:
break;
}
}
/**
* Writes a frame packet to the ring buffer.
*
* @returns VBox status code.
* @param pBuf The buffer.
* @param pRingBuf The ring buffer to read from.
* @param pSG The gather list.
* @param pNewDstMac Set the destination MAC address to the address if specified.
*/
{
int rc;
else
if (RT_SUCCESS(rc))
{
if (pNewDstMac)
return VINF_SUCCESS;
}
return rc;
}
/**
* Sends a frame to a specific interface.
*
* @param pIf The interface.
* @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
* @param pSG The gather buffer which data is being sent to the interface.
* @param pNewDstMac Set the destination MAC address to the address if specified.
*/
{
/*
*/
if (RT_SUCCESS(rc))
{
return;
}
/*
* Scheduling hack, for unicore machines primarily.
*/
&& pIfSender /* but not if it's from the trunk */
)
{
unsigned cYields = 2;
while (--cYields > 0)
{
if (RT_SUCCESS(rc))
{
return;
}
}
}
/* ok, the frame is lost. */
}
/**
* Fallback path that does the GSO segmenting before passing the frame on to the
* trunk interface.
*
* The caller holds the trunk lock.
*
* @param pThis The trunk.
* @param pIfSender The IF sending the frame.
* @param pSG Pointer to the gather list.
* @param fDst The destination flags.
*/
static int intnetR0TrunkIfSendGsoFallback(PINTNETTRUNKIF pThis, PINTNETIF pIfSender, PINTNETSG pSG, uint32_t fDst)
{
/*
* Since we're only using this for GSO frame comming from the internal
* network interfaces and never the trunk, we can assume there is only
* one segment. This simplifies the code quite a bit.
*/
union
{
} u;
/*
* Carve out the frame segments with the header and frame in different
* scatter / gather segments.
*/
{
uint32_t offSegPayload = PDMNetGsoCarveSegment(&pSG->GsoCtx, (uint8_t *)pSG->aSegs[0].pv, pSG->cbTotal, iSeg, cSegs,
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/**
* Checks if any of the given trunk destinations can handle this kind of GSO SG.
*
* @returns true if it can, false if it cannot.
* @param pThis The trunk.
* @param pSG The scatter / gather buffer.
* @param fDst The destination mask.
*/
DECLINLINE(bool) intnetR0TrunkIfCanHandleGsoFrame(PINTNETTRUNKIF pThis, PINTNETSG pSG, uint32_t fDst)
{
if (fDst == INTNETTRUNKDIR_HOST)
if (fDst == INTNETTRUNKDIR_WIRE)
}
/**
* Sends a frame down the trunk.
*
* @param pThis The trunk.
* @param pNetwork The network the frame is being sent to.
* @param pIfSender The IF sending the frame. Used for MAC address
* checks in shared MAC mode.
* @param fDst The destination flags.
* @param pSG Pointer to the gather list.
*/
{
/*
* Quick sanity check.
*/
/*
* Edit the frame if we're sharing the MAC address with the host on the wire.
*
* If the frame is headed for both the host and the wire, we'll have to send
* it to the host before making any modifications, and force the OS specific
* backend to copy it. We do this by marking it as TEMP (which is always the
* case right now).
*/
&& (fDst & INTNETTRUNKDIR_WIRE))
{
/*
* Dispatch it to the host before making changes.
*/
if (fDst & INTNETTRUNKDIR_HOST)
{
fDst &= ~INTNETTRUNKDIR_HOST;
}
/*
* Edit the source address so that it it's the same as the host.
*/
/* ASSUME frame from IntNetR0IfSend! */
/*
* Deal with tags from the snooping phase.
*/
{
/*
* APR IPv4: replace hardware (MAC) addresses because these end up
* in ARP caches. So, if we don't the other machiens will
* send the packets to the MAC address of the guest
* instead of the one of the host, which won't work on
* wireless of course...
*/
{
}
{
}
}
//else if (pSG->fFlags & INTNETSG_FLAGS_ICMPV6_NDP)
//{ /// @todo move the editing into a different function
//}
}
/*
* Send the frame, handling the GSO fallback .
* .
* Note! The trunk implementation will re-check that the trunk is active .
* before sending, so we don't have to duplicate that effort here.
*/
int rc;
else
/** @todo failure statistics? */
}
/**
* Edits an ARP packet arriving from the wire via the trunk connection.
*
* @param pNetwork The network the frame is being sent to.
* @param pSG Pointer to the gather list for the frame.
* The flags and data content may be updated.
* @param pEthHdr Pointer to the ethernet header. This may also be
* updated if it's a unicast...
*/
static void intnetR0NetworkEditArpFromWire(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
{
/*
* Check the minimum size and get a linear copy of the thing to work on,
* using the temporary buffer if necessary.
*/
return;
{
return;
}
/*
* Ignore packets which doesn't interest us or we perceive as malformed.
*/
return;
&& ar_oper != RTNET_ARPOP_REPLY))
{
return;
}
/* Tag it as ARP IPv4. */
/*
* The thing we're interested in here is a reply to a query made by a guest
* since we modified the MAC in the initial request the guest made.
*/
if ( ar_oper == RTNET_ARPOP_REPLY
{
if (pIf)
{
{
}
/* Write back the packet if we've been making changes to a buffered copy. */
}
}
}
/**
* Detects and edits an DHCP packet arriving from the internal net.
*
* @param pNetwork The network the frame is being sent to.
* @param pSG Pointer to the gather list for the frame.
* The flags and data content may be updated.
* @param pEthHdr Pointer to the ethernet header. This may also be
* updated if it's a unicast...
*/
static void intnetR0NetworkEditDhcpFromIntNet(PINTNETNETWORK pNetwork, PINTNETSG pSG, PRTNETETHERHDR pEthHdr)
{
/*
* Check the minimum size and get a linear copy of the thing to work on,
* using the temporary buffer if necessary.
*/
if (RT_UNLIKELY(pSG->cbTotal < sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN))
return;
/*
* Get a pointer to a linear copy of the full packet, using the
* temporary buffer if necessary.
*/
{
return;
//pSG->fFlags |= INTNETSG_FLAGS_PKT_CP_IN_TMP;
}
/*
* Validate the IP header and find the UDP packet.
*/
if (!RTNetIPv4IsHdrValid(pIpHdr, cbPacket, pSG->cbTotal - sizeof(RTNETETHERHDR), true /*fCheckSum*/))
{
Log6(("intnetR0NetworkEditDhcpFromIntNet: bad ip header\n"));
return;
}
return;
/* We are only interested in DHCP packets coming from client to server. */
return;
/*
* Check if the DHCP message is valid and get the type.
*/
{
Log6(("intnetR0NetworkEditDhcpFromIntNet: Bad UDP packet\n"));
return;
}
{
Log6(("intnetR0NetworkEditDhcpFromIntNet: Bad DHCP packet\n"));
return;
}
switch (MsgType)
{
case RTNET_DHCP_MT_DISCOVER:
case RTNET_DHCP_MT_REQUEST:
Log6(("intnetR0NetworkEditDhcpFromIntNet: Setting broadcast flag in DHCP %#x, previously %x\n", MsgType, pDhcp->bp_flags));
{
/* Patch flags */
intnetR0SgWritePart(pSG, (uintptr_t)&pDhcp->bp_flags - (uintptr_t)pIpHdr + sizeof(RTNETETHERHDR), sizeof(uFlags), &uFlags);
/* Patch UDP checksum */
while (uChecksum >> 16)
intnetR0SgWritePart(pSG, (uintptr_t)&pUdpHdr->uh_sum - (uintptr_t)pIpHdr + sizeof(RTNETETHERHDR), sizeof(pUdpHdr->uh_sum), &uChecksum);
}
break;
}
}
/**
* Checks if the callers context is okay for sending to the specified
* destinations.
*
* @returns true if it's okay, false if it isn't.
* @param pNetwork The network.
* @param pIfSender The interface sending or NULL if it's the trunk.
* @param pDstTab The destination table.
*/
DECLINLINE(bool) intnetR0NetworkIsContextOk(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCINTNETDSTTAB pDstTab)
{
/* Sending to the trunk is the problematic path. If the trunk is the
sender we won't be sending to it, so no problem..
Note! fTrunkDst may be set event if if the trunk is the sender. */
if (!pIfSender)
return true;
if (!fTrunkDst)
return true;
/* ASSUMES: that the trunk won't change its report while we're checking. */
return true;
/* ASSUMES: That a preemption test detects HWACCM contexts. (Will work on
non-preemptive systems as well.) */
return true;
return false;
}
/**
* Checks if the callers context is okay for doing a broadcast given the
* specified source.
*
* @returns true if it's okay, false if it isn't.
* @param pNetwork The network.
* @param fSrc The source of the packet. (0 (intnet),
* INTNETTRUNKDIR_HOST or INTNETTRUNKDIR_WIRE).
*/
{
/* Sending to the trunk is the problematic path. If the trunk is the
sender we won't be sending to it, so no problem. */
if (fSrc)
return true;
/* ASSUMES: That a preemption test detects HWACCM contexts. (Will work on
non-preemptive systems as well.) */
return true;
/* PARANOIA: Grab the spinlock to make sure the trunk structure cannot be
freed while we're touching it. */
return fRc;
}
/**
* Check context, edit, snoop and switch a broadcast frame when sharing MAC
* address on the wire.
*
* The caller must hold at least one interface on the network busy to prevent it
* from destructing beath us.
*
* @param pNetwork The network the frame is being sent to.
* @param fSrc The source of the packet. (0 (intnet),
* INTNETTRUNKDIR_HOST or INTNETTRUNKDIR_WIRE).
* @param pIfSender The sender interface, NULL if trunk. Used to
* prevent sending an echo to the sender.
* @param pSG Pointer to the gather list.
* @param pEthHdr Pointer to the ethernet header.
* @param pDstTab The destination output table.
*/
{
/*
* Before doing any work here, we need to figure out if we can handle it
* in the current context. The restrictions are solely on the trunk.
*
* Note! Since at least one interface is busy, there won't be any changes
* to the parameters here (unless the trunk changes its capability
* report, which it shouldn't).
*/
return INTNETSWDECISION_BAD_CONTEXT;
/*
* Check for ARP packets from the wire since we'll have to make
* modification to them if we're sharing the MAC address with the host.
*/
if ( (fSrc & INTNETTRUNKDIR_WIRE)
/*
* Check for DHCP packets from the internal net since we'll have to set
* broadcast flag in DHCP requests if we're sharing the MAC address with
* the host. GSO is not applicable to DHCP traffic.
*/
if ( !fSrc
/*
* Snoop address info from packet orginating from the trunk connection.
*/
if (fSrc)
{
#ifdef INTNET_WITH_DHCP_SNOOPING
&& pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN
#else
#endif
}
/*
* Create the broadcast destination table.
*/
}
/**
* Check context, snoop and switch a unicast frame using the network layer
* address of the link layer one (when sharing MAC address on the wire).
*
* This function is only used for frames coming from the wire (trunk).
*
* @returns true if it's addressed to someone on the network, otherwise false.
* @param pNetwork The network the frame is being sent to.
* @param pSG Pointer to the gather list.
* @param pEthHdr Pointer to the ethernet header.
* @param pDstTab The destination output table.
*/
static INTNETSWDECISION intnetR0NetworkSharedMacFixAndSwitchUnicast(PINTNETNETWORK pNetwork, PINTNETSG pSG,
{
/*
* Extract the network address from the packet.
*/
{
case RTNET_ETHERTYPE_IPV4:
if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV4, ip_dst), sizeof(Addr.IPv4), &Addr)))
{
}
break;
#if 0 /** @todo IntNet: implement IPv6 for wireless MAC sharing. */
case RTNET_ETHERTYPE_IPV6
if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPV6, ip6_dst), sizeof(Addr.IPv6), &Addr)))
{
}
break;
#endif
#if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
case RTNET_ETHERTYPE_IPX_1:
case RTNET_ETHERTYPE_IPX_2:
case RTNET_ETHERTYPE_IPX_3:
if (RT_UNLIKELY(!intnetR0SgReadPart(pSG, sizeof(RTNETETHERHDR) + RT_OFFSETOF(RTNETIPX, ipx_dstnet), sizeof(Addr.IPX), &Addr)))
{
}
break;
#endif
/*
* Treat ARP as broadcast (it shouldn't end up here normally,
* so it goes last in the switch).
*/
case RTNET_ETHERTYPE_ARP:
Log6(("intnetshareduni: ARP\n"));
/** @todo revisit this broadcasting of unicast ARP frames! */
return intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, INTNETTRUNKDIR_WIRE, NULL, pSG, pEthHdr, pDstTab);
/*
* Unknown packets are sent to the trunk and any promiscuous interfaces.
*/
default:
{
}
}
/*
* Do level-3 switching.
*/
#ifdef INTNET_WITH_DHCP_SNOOPING
/*
* Perform DHCP snooping. GSO is not applicable to DHCP traffic
*/
if ( enmAddrType == kIntNetAddrType_IPv4
&& pSG->cbTotal >= sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN + RTNETBOOTP_DHCP_MIN_LEN
#endif /* INTNET_WITH_DHCP_SNOOPING */
return enmSwDecision;
}
/**
* Release all the interfaces in the destination table when we realize that
* we're in a context where we cannot get the job done.
*
* @param pNetwork The network.
* @param pDstTab The destination table.
*/
{
/* The trunk interface. */
{
}
/* Regular interfaces. */
while (iIf-- > 0)
{
}
}
/**
* Deliver the frame to the interfaces specified in the destination table.
*
* @param pNetwork The network.
* @param pDstTab The destination table.
* @param pSG The frame to send.
* @param pIfSender The sender interface. NULL if it origined via
* the trunk.
*/
static void intnetR0NetworkDeliver(PINTNETNETWORK pNetwork, PINTNETDSTTAB pDstTab, PINTNETSG pSG, PINTNETIF pIfSender)
{
/*
* Do the interfaces first before sending it to the wire and risk having to
* modify it.
*/
while (iIf-- > 0)
{
}
/*
* Send to the trunk.
*
* Note! The switching functions will include the trunk even when the frame
* source is the trunk. This is because we need it to figure out
* whether the other half of the trunk should see the frame or not
* and let the caller know.
*
* So, we'll ignore trunk sends here if the frame origin is
* INTNETTRUNKSWPORT::pfnRecv.
*/
{
if (pIfSender)
}
}
/**
* Sends a frame.
*
* This function will distribute the frame to the interfaces it is addressed to.
* It will also update the MAC address of the sender.
*
* The caller must own the network mutex.
*
* @returns The switching decision.
* @param pNetwork The network the frame is being sent to.
* @param pIfSender The interface sending the frame. This is NULL if it's the trunk.
* @param fSrc The source flags. This 0 if it's not from the trunk.
* @param pSG Pointer to the gather list.
* @param pDstTab The destination table to use.
*/
static INTNETSWDECISION intnetR0NetworkSend(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc,
{
/*
* Assert reality.
*/
return INTNETSWDECISION_INVALID;
/*
* Get the ethernet header (might theoretically involve multiple segments).
*/
return INTNETSWDECISION_INVALID;
Log2(("D=%.6Rhxs S=%.6Rhxs T=%04x f=%x z=%x\n",
/*
* Learn the MAC address of the sender. No re-learning as the interface
* user will normally tell us the right MAC address.
*
* Note! We don't notify the trunk about these mainly because of the
* problematic contexts we might be called in.
*/
if (RT_UNLIKELY( pIfSender
))
{
if (pIfEntry)
}
/*
* Deal with MAC address sharing as that may required editing of the
* packets before we dispatch them anywhere.
*/
{
enmSwDecision = intnetR0NetworkSharedMacFixAndSwitchBroadcast(pNetwork, fSrc, pIfSender, pSG, &EthHdr, pDstTab);
else if (fSrc & INTNETTRUNKDIR_WIRE)
else
}
else
/*
* Deliver to the destinations if we can.
*/
{
else
{
}
}
return enmSwDecision;
}
/**
* Sends one or more frames.
*
* The function will first the frame which is passed as the optional arguments
* pvFrame and cbFrame. These are optional since it also possible to chain
* together one or more frames in the send buffer which the function will
* process after considering it's arguments.
*
* The caller is responsible for making sure that there are no concurrent calls
* to this method (with the same handle).
*
* @returns VBox status code.
* @param hIf The interface handle.
* @param pSession The caller's session.
*/
{
/*
* Validate input and translate the handle.
*/
if (!pIf)
return VERR_INVALID_HANDLE;
/*
* Make sure we've got a network.
*/
int rc = VINF_SUCCESS;
{
/*
* Grab the destination table.
*/
{
/*
* Process the send buffer.
*/
* with buffer sharing for some OS or service. Darwin copies everything so
* I won't bother allocating and managing SGs rigth now. Sorry. */
{
if (u16Type == INTNETHDR_TYPE_FRAME)
{
/* Send regular frame. */
intnetR0IfSnoopAddr(pIf, (uint8_t *)pvCurFrame, pHdr->cbFrame, false /*fGso*/, (uint16_t *)&Sg.fFlags);
}
else if (u16Type == INTNETHDR_TYPE_GSO)
{
/* Send GSO frame if sane. */
{
}
else
{
}
}
/* Unless it's a padding frame, we're getting babble from the producer. */
else
{
if (u16Type != INTNETHDR_TYPE_PADDING)
}
{
rc = VERR_TRY_AGAIN;
break;
}
/* Skip to the next frame. */
}
/*
* Put back the destination table.
*/
}
else
}
else
/*
* Release the interface.
*/
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0IfSend.
*
* @returns see IntNetR0IfSend.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
{
return VERR_INVALID_PARAMETER;
}
/**
* Maps the default buffer into ring 3.
*
* @returns VBox status code.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param ppRing3Buf Where to store the address of the ring-3 mapping
* (optional).
* @param ppRing0Buf Where to store the address of the ring-0 mapping
* (optional).
*/
{
LogFlow(("IntNetR0IfGetBufferPtrs: hIf=%RX32 ppRing3Buf=%p ppRing0Buf=%p\n", hIf, ppRing3Buf, ppRing0Buf));
/*
* Validate input.
*/
if (ppRing3Buf)
*ppRing3Buf = 0;
if (ppRing0Buf)
*ppRing0Buf = 0;
if (!pIf)
return VERR_INVALID_HANDLE;
/*
* ASSUMES that only the process that created an interface can use it.
* ASSUMES that we created the ring-3 mapping when selecting or
* allocating the buffer.
*/
if (RT_SUCCESS(rc))
{
if (ppRing3Buf)
if (ppRing0Buf)
}
LogFlow(("IntNetR0IfGetBufferPtrs: returns %Rrc *ppRing3Buf=%p *ppRing0Buf=%p\n",
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0IfGetBufferPtrs.
*
* @returns see IntNetR0IfGetRing3Buffer.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) IntNetR0IfGetBufferPtrsReq(PSUPDRVSESSION pSession, PINTNETIFGETBUFFERPTRSREQ pReq)
{
return VERR_INVALID_PARAMETER;
}
#if 0
/**
* Gets the physical addresses of the default interface buffer.
*
* @returns VBox status code.
* @param hIF The interface handle.
* @param paPages Where to store the addresses. (The reserved fields will be set to zero.)
* @param cPages
*/
{
/*
* Validate input.
*/
if (!pIf)
return VERR_INVALID_HANDLE;
/*
* Grab the lock and get the data.
* ASSUMES that the handle isn't closed while we're here.
*/
if (RT_SUCCESS(rc))
{
/** @todo make a SUPR0 api for obtaining the array. SUPR0/IPRT is keeping track of everything, there
* is no need for any extra bookkeeping here.. */
}
return VERR_NOT_IMPLEMENTED;
}
#endif
/**
* Sets the promiscuous mode property of an interface.
*
* @returns VBox status code.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param fPromiscuous Set if the interface should be in promiscuous mode, clear if not.
*/
INTNETR0DECL(int) IntNetR0IfSetPromiscuousMode(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fPromiscuous)
{
/*
* Validate & translate input.
*/
if (!pIf)
{
Log(("IntNetR0IfSetPromiscuousMode: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
/*
* Get the network, take the address spinlock, and make the change.
* Paranoia^2: Mark ourselves busy to prevent anything from being destroyed.
*/
int rc = VINF_SUCCESS;
if (pNetwork)
{
{
Log(("IntNetR0IfSetPromiscuousMode: hIf=%RX32: Changed from %d -> %d\n",
}
}
else
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0IfSetPromiscuousMode.
*
* @returns see IntNetR0IfSetPromiscuousMode.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) IntNetR0IfSetPromiscuousModeReq(PSUPDRVSESSION pSession, PINTNETIFSETPROMISCUOUSMODEREQ pReq)
{
return VERR_INVALID_PARAMETER;
}
/**
* Sets the MAC address of an interface.
*
* @returns VBox status code.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param pMAC The new MAC address.
*/
INTNETR0DECL(int) IntNetR0IfSetMacAddress(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PCRTMAC pMac)
{
/*
* Validate & translate input.
*/
if (!pIf)
{
Log(("IntNetR0IfSetMacAddress: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
/*
* Get the network, take the address spinlock, and make the change.
* Paranoia^2: Mark ourselves busy to prevent anything from being destroyed.
*/
int rc = VINF_SUCCESS;
if (pNetwork)
{
{
Log(("IntNetR0IfSetMacAddress: hIf=%RX32: Changed from %.6Rhxs -> %.6Rhxs\n",
/* Update the two copies. */
/* Grab a busy reference to the trunk so we release the lock before notifying it. */
if (pTrunk)
}
if (pTrunk)
{
if (pIfPort)
}
}
else
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0IfSetMacAddress.
*
* @returns see IntNetR0IfSetMacAddress.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) IntNetR0IfSetMacAddressReq(PSUPDRVSESSION pSession, PINTNETIFSETMACADDRESSREQ pReq)
{
return VERR_INVALID_PARAMETER;
}
/**
* Worker for intnetR0IfSetActive and intnetR0IfDestruct.
*
* This function will update the active interface count on the network and
* activate or deactivate the trunk connection if necessary. Note that in
* order to do this it is necessary to abandond the network semaphore.
*
* @returns VBox status code.
* @param pNetwork The network.
* @param fIf The interface.
* @param fActive What to do.
*/
{
/* quick santiy check */
/*
* The address spinlock of the network protects the variables, while the
* big lock protects the calling of pfnSetState. Grab both lock at once
* to save us the extra hazzle.
*/
/*
* Do the update.
*/
{
{
if (fActive)
{
pNetwork->cActiveIFs++;
{
if (pTrunk)
{
}
}
}
else
{
pNetwork->cActiveIFs--;
if (pNetwork->cActiveIFs == 0)
{
}
}
}
}
/*
* Tell the trunk if necessary.
*/
pTrunk->pIfPort->pfnSetState(pTrunk->pIfPort, fActive ? INTNETTRUNKIFSTATE_ACTIVE : INTNETTRUNKIFSTATE_INACTIVE);
return VINF_SUCCESS;
}
/**
* Sets the active property of an interface.
*
* @returns VBox status code.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param fActive The new state.
*/
{
/*
* Validate & translate input.
*/
if (!pIf)
{
Log(("IntNetR0IfSetActive: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
/*
* Hand it to the network since it might involve the trunk and things are
* tricky there wrt to locking order.
*
* Note! We mark the interface busy so the network cannot be removed while
* we're working on it - paranoia strikes again.
*/
int rc;
if (pNetwork)
else
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0IfSetActive.
*
* @returns see IntNetR0IfSetActive.
* @param pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
{
return VERR_INVALID_PARAMETER;
}
/**
* Wait for the interface to get signaled.
* The interface will be signaled when is put into the receive buffer.
*
* @returns VBox status code.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param cMillies Number of milliseconds to wait. RT_INDEFINITE_WAIT should be
* used if indefinite wait is desired.
*/
{
/*
* Get and validate essential handles.
*/
if (!pIf)
{
Log(("IntNetR0IfWait: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
|| hRecvEvent == NIL_RTSEMEVENT
|| fDestroying
)
{
Log(("IntNetR0IfWait: returns VERR_SEM_DESTROYED\n"));
return VERR_SEM_DESTROYED;
}
/*
* It is tempting to check if there is data to be read here,
* but the problem with such an approach is that it will cause
* one unnecessary supervisor->user->supervisor trip. There is
* already a slight risk for such, so no need to increase it.
*/
/*
* Increment the number of waiters before starting the wait.
* Upon wakeup we must assert reality, checking that we're not
* already destroyed or in the process of being destroyed. This
* code must be aligned with the waiting code in intnetR0IfDestruct.
*/
{
if (!pIf->fDestroying)
{
}
else
}
else
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0IfWait.
*
* @returns see IntNetR0IfWait.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
{
return VERR_INVALID_PARAMETER;
}
/**
* Wake up any threads waiting on the interface.
*
* @returns VBox status code.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param fNoMoreWaits When set, no more waits are permitted.
*/
INTNETR0DECL(int) IntNetR0IfAbortWait(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fNoMoreWaits)
{
/*
* Get and validate essential handles.
*/
if (!pIf)
{
Log(("IntNetR0IfAbortWait: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
|| hRecvEvent == NIL_RTSEMEVENT
|| fDestroying
)
{
Log(("IntNetR0IfAbortWait: returns VERR_SEM_DESTROYED\n"));
return VERR_SEM_DESTROYED;
}
/*
* Set fDestroying if requested to do so and then wake up all the sleeping
* threads (usually just one). We leave the semaphore in the signalled
* state so the next caller will return immediately.
*/
if (fNoMoreWaits)
while (cSleepers-- > 0)
{
}
return VINF_SUCCESS;
}
/**
* VMMR0 request wrapper for IntNetR0IfAbortWait.
*
* @returns see IntNetR0IfWait.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
{
return VERR_INVALID_PARAMETER;
}
/**
* Close an interface.
*
* @returns VBox status code.
* @param pIntNet The instance handle.
* @param hIf The interface handle.
* @param pSession The caller's session.
*/
{
/*
* Validate and free the handle.
*/
if (!pIf)
return VERR_INVALID_HANDLE;
/* Mark the handle as freed so intnetR0IfDestruct won't free it again. */
/*
* Signal the event semaphore to wake up any threads in IntNetR0IfWait
* and give them a moment to get out and release the interface.
*/
while (i-- > 0)
{
}
/*
* Release the references to the interface object (handle + free lookup).
*/
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0IfCloseReq.
*
* @returns see IntNetR0IfClose.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
{
return VERR_INVALID_PARAMETER;
}
/**
* Interface destructor callback.
* This is called for reference counted objectes when the count reaches 0.
*
* @param pvObj The object pointer.
* @param pvUser1 Pointer to the interface.
* @param pvUser2 Pointer to the INTNET instance data.
*/
{
/*
* adding or removing interface while we're in here. For paranoid reasons
* we also mark the interface as destroyed here so any waiting threads can
* take evasive action (theoretical case).
*/
/*
* Delete the interface handle so the object no longer can be used.
* (Can happen if the client didn't close its session.)
*/
if (hIf != INTNET_HANDLE_INVALID)
{
}
/*
* If we've got a network deactivate and detach ourselves from it. Because
* of cleanup order we might have been orphaned by the network destructor.
*/
if (pNetwork)
{
/* set inactive. */
/* remove ourselves from the switch table. */
while (iIf-- > 0)
{
break;
}
/* Notify the trunk about the interface being destroyed. */
/* Wait for the interface to quiesce while we still can. */
/* Release our reference to the network. */
}
/*
* Wakeup anyone waiting on this interface.
*
* We *must* make sure they have woken up properly and realized
* that the interface is no longer valid.
*/
{
unsigned cMaxWait = 0x1000;
{
}
{
RTThreadSleep(1);
{
RTThreadSleep(10);
}
}
}
/*
* Unmap user buffer.
*/
{
/** @todo user buffer */
}
/*
* Unmap and Free the default buffer.
*/
if (pIf->pIntBufDefault)
{
pIf->pIntBufDefaultR3 = 0;
}
/*
* Free remaining resources
*/
}
/**
* Creates a new network interface.
*
* The call must have opened the network for the new interface and is
* responsible for closing it on failure. On success it must leave the network
* opened so the interface destructor can close it.
*
* @returns VBox status code.
* @param pNetwork The network, referenced. The reference is consumed on
* success.
* @param pSession The session handle.
* @param cbSend The size of the send buffer.
* @param cbRecv The size of the receive buffer.
* @param phIf Where to store the interface handle.
*/
static int intnetR0NetworkCreateIf(PINTNETNETWORK pNetwork, PSUPDRVSESSION pSession, unsigned cbSend, unsigned cbRecv,
{
LogFlow(("intnetR0NetworkCreateIf: pNetwork=%p pSession=%p cbSend=%u cbRecv=%u phIf=%p\n",
/*
* Assert input.
*/
/*
* Make sure that all destination tables as well as the have space of
*/
if (RT_FAILURE(rc))
return rc;
/*
* Allocate the interface and initalize it.
*/
if (!pIf)
return VERR_NO_MEMORY;
//pIf->fMacSet = false;
//pIf->fPromiscuous = false;
//pIf->fActive = false;
//pIf->fDestroying = false;
//pIf->cYields = 0;
//pIf->pIntBuf = 0;
//pIf->pIntBufR3 = NIL_RTR3PTR;
//pIf->pIntBufDefault = 0;
//pIf->pIntBufDefaultR3 = NIL_RTR3PTR;
//pIf->cSleepers = 0;
//pIf->pvObj = NULL;
//pIf->aAddrCache = {0};
//pIf->pDstTab = NULL;
//pIf->pvIfData = NULL;
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
/*
* Create the default buffer.
*/
/** @todo adjust with minimums and apply defaults here. */
rc = SUPR0MemAlloc(pIf->pSession, cbBuf, (PRTR0PTR)&pIf->pIntBufDefault, (PRTR3PTR)&pIf->pIntBufDefaultR3);
if (RT_SUCCESS(rc))
{
ASMMemZero32(pIf->pIntBufDefault, cbBuf); /** @todo I thought I specified these buggers as clearing the memory... */
/*
* Register the interface with the session and create a handle for it.
*/
{
if (RT_SUCCESS(rc))
{
/*
* Finally add the interface to the network, consuming the
* network reference of the caller.
*/
/*
* Grab a busy reference (paranoia) to the trunk before releaseing
* the spinlock and then notify it about the new interface.
*/
if (pTrunk)
if (pTrunk)
{
}
if (RT_SUCCESS(rc))
{
/*
* We're good!
*/
Log(("intnetR0NetworkCreateIf: returns VINF_SUCCESS *phIf=%RX32 cbSend=%u cbRecv=%u cbBuf=%u\n",
return VINF_SUCCESS;
}
}
return rc;
}
/* clean up */
}
}
return rc;
}
/** @copydoc INTNETTRUNKSWPORT::pfnSetSGPhys */
static DECLCALLBACK(bool) intnetR0TrunkIfPortSetSGPhys(PINTNETTRUNKSWPORT pSwitchPort, bool fEnable)
{
AssertMsgFailed(("Not implemented because it wasn't required on Darwin\n"));
}
/** @copydoc INTNETTRUNKSWPORT::pfnReportMacAddress */
static DECLCALLBACK(void) intnetR0TrunkIfPortReportMacAddress(PINTNETTRUNKSWPORT pSwitchPort, PCRTMAC pMacAddr)
{
/*
* Get the network instance and grab the address spinlock before making
* any changes.
*/
if (pNetwork)
{
}
else
}
/** @copydoc INTNETTRUNKSWPORT::pfnReportPromiscuousMode */
static DECLCALLBACK(void) intnetR0TrunkIfPortReportPromiscuousMode(PINTNETTRUNKSWPORT pSwitchPort, bool fPromiscuous)
{
/*
* Get the network instance and grab the address spinlock before making
* any changes.
*/
if (pNetwork)
{
}
}
/** @copydoc INTNETTRUNKSWPORT::pfnReportGsoCapabilities */
{
if (fDst & INTNETTRUNKDIR_HOST)
if (fDst & INTNETTRUNKDIR_WIRE)
}
/** @copydoc INTNETTRUNKSWPORT::pfnReportNoPreemptDsts */
static DECLCALLBACK(void) intnetR0TrunkIfPortReportNoPreemptDsts(PINTNETTRUNKSWPORT pSwitchPort, uint32_t fNoPreemptDsts)
{
}
/** @copydoc INTNETTRUNKSWPORT::pfnPreRecv */
{
/* assert some sanity */
/*
* Mark the trunk as busy, make sure we've got a network and that there are
* some active interfaces around.
*/
&& pNetwork->cActiveIFs > 0 ))
{
/*
* Lazy bird! No pre-switching of multicast and shared-MAC-on-wire.
*/
else
fSrc,
}
return enmSwDecision;
}
/** @copydoc INTNETTRUNKSWPORT::pfnRecv */
static DECLCALLBACK(bool) intnetR0TrunkIfPortRecv(PINTNETTRUNKSWPORT pSwitchPort, void *pvIf, PINTNETSG pSG, uint32_t fSrc)
{
/* assert some sanity */
/*
* Mark the trunk as busy, make sure we've got a network and that there are
* some active interfaces around.
*/
bool fRc = false /* don't drop it */;
&& pNetwork->cActiveIFs > 0 ))
{
/*
* Grab or allocate a destination table.
*/
unsigned iDstTab = 0;
if (fIntCtx)
{
/* Interrupt or restricted context. */
else
{
while (iDstTab-- > 0)
{
if (pDstTab)
{
break;
}
}
}
}
else
{
/* Task context, fallback is to allocate a table. */
if (!pDstTab)
if (pDstTab)
{
}
else
{
iDstTab = 65535;
}
}
{
/*
* Finally, get down to business of sending the frame.
*/
AssertMsg(enmSwDecision != INTNETSWDECISION_BAD_CONTEXT, ("fSrc=%#x fTrunkDst=%#x hdr=%.14Rhxs\n", fSrc, pDstTab->fTrunkDst, pSG->aSegs[0].pv));
if (enmSwDecision == INTNETSWDECISION_INTNET)
fRc = true; /* drop it */
/*
* Free the destination table.
*/
if (iDstTab == 65535)
else
{
else
{
/* this shouldn't happen! */
while (iDstTab-- > 0)
if (!papDstTabs[iDstTab])
{
break;
}
}
}
}
}
return fRc;
}
/** @copydoc INTNETTRUNKSWPORT::pfnSGRetain */
static DECLCALLBACK(void) intnetR0TrunkIfPortSGRetain(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
{
/* assert some sanity */
/* do it. */
}
/** @copydoc INTNETTRUNKSWPORT::pfnSGRelease */
static DECLCALLBACK(void) intnetR0TrunkIfPortSGRelease(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG)
{
/* assert some sanity */
/*
* Free it?
*/
{
/** @todo later */
}
}
/**
* Retain the trunk interface.
*
* @returns pThis if retained.
*
* @param pThis The trunk.
*
* @remarks Any locks.
*/
{
{
return pThis;
}
return NULL;
}
/**
* Release the trunk interface.
*
* @param pThis The trunk.
*/
{
}
/**
* Shutdown the trunk interface.
*
* @param pThis The trunk.
* @param pNetworks The network.
*
* @remarks The caller must hold the global lock.
*/
{
/* assert sanity */
if (!pThis)
return;
/*
* The interface has already been deactivated, we just to wait for
* it to become idle before we can disconnect and release it.
*/
if (pIfPort)
{
/* unset it */
/* wait in portions so we can complain ever now an then. */
if (RT_FAILURE(rc))
{
LogRel(("intnet: '%s' did't become idle in %RU64 ns (%Rrc).\n",
while ( RT_FAILURE(rc)
if (rc == VERR_TIMEOUT)
{
LogRel(("intnet: '%s' did't become idle in %RU64 ns (%Rrc).\n",
while ( rc == VERR_TIMEOUT
if (RT_FAILURE(rc))
{
LogRel(("intnet: '%s' did't become idle in %RU64 ns (%Rrc), giving up.\n",
}
}
}
/* disconnect & release it. */
}
/*
* Free up the resources.
*/
{
}
for (unsigned i = 0; i < pThis->cIntDstTabs; i++)
{
}
}
/**
* Creates the trunk connection (if any).
*
* @returns VBox status code.
*
* @param pNetwork The newly created network.
* @param pSession The session handle.
*/
{
const char *pszName;
switch (pNetwork->enmTrunkType)
{
/*
* The 'None' case, simple.
*/
case kIntNetTrunkType_None:
return VINF_SUCCESS;
/* Can't happen, but makes GCC happy. */
default:
return VERR_NOT_IMPLEMENTED;
/*
* Translate enum to component factory name.
*/
case kIntNetTrunkType_NetFlt:
pszName = "VBoxNetFlt";
break;
case kIntNetTrunkType_NetAdp:
#if defined(RT_OS_DARWIN) && !defined(VBOXNETADP_DO_NOT_USE_NETFLT)
pszName = "VBoxNetFlt";
#else /* VBOXNETADP_DO_NOT_USE_NETFLT */
pszName = "VBoxNetAdp";
#endif /* VBOXNETADP_DO_NOT_USE_NETFLT */
break;
case kIntNetTrunkType_SrvNat:
pszName = "VBoxSrvNat";
break;
}
/*
* Allocate the trunk interface and associated destination tables.
*
* We take a very optimistic view on the parallelism of the host
* network stack and NIC driver. So, we allocate one table for each
* possible CPU to deal with interrupt time requests and one for task
* time calls.
*/
PINTNETTRUNKIF pTrunk = (PINTNETTRUNKIF)RTMemAllocZ(RT_OFFSETOF(INTNETTRUNKIF, apIntDstTabs[cCpus]));
if (!pTrunk)
return VERR_NO_MEMORY;
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
//pTrunk->pIfPort = NULL;
//pTrunk->fPhysSG = false;
//pTrunk->fUnused = false;
//pTrunk->cBusy = 0;
//pTrunk->fNoPreemptDsts = 0;
//pTrunk->fWireGsoCapabilites = 0;
//pTrunk->fHostGsoCapabilites = 0;
//pTrunk->abGsoHdrs = {0};
//pTrunk->apTaskDstTabs = above;
//pTrunk->cIntDstTabs = above;
//pTrunk->apIntDstTabs = above;
/*
* Create the lock (we've NIL'ed the members above to simplify cleanup).
*/
if (RT_SUCCESS(rc))
{
/*
* There are a couple of bits in MacTab as well pertaining to the
* trunk. We have to set this before it's reported.
*
* Note! We don't need to lock the MacTab here - creation time.
*/
pNetwork->MacTab.fWirePromiscuous = false; /** @todo !!(fFlags & INTNET_OPEN_FLAGS_PROMISC_TRUNK_WIRE); */
#ifdef IN_RING0 /* (testcase is ring-3) */
/*
* Query the factory we want, then use it create and connect the trunk.
*/
rc = SUPR0ComponentQueryFactory(pSession, pszName, INTNETTRUNKFACTORY_UUID_STR, (void **)&pTrunkFactory);
if (RT_SUCCESS(rc))
{
&pTrunk->SwitchPort,
: 0,
if (RT_SUCCESS(rc))
{
Log(("intnetR0NetworkCreateTrunkIf: VINF_SUCCESS - pszName=%s szTrunk=%s%s Network=%s\n",
pszName, pNetwork->szTrunk, pNetwork->fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE ? " shared-mac" : "", pNetwork->szName));
return VINF_SUCCESS;
}
}
#else /* IN_RING3 */
#endif /* IN_RING3 */
}
/* bail out and clean up. */
}
for (unsigned i = 0; i < pTrunk->cIntDstTabs; i++)
LogFlow(("intnetR0NetworkCreateTrunkIf: %Rrc - pszName=%s szTrunk=%s Network=%s\n",
return rc;
}
/**
* Object destructor callback.
* This is called for reference counted objectes when the count reaches 0.
*
* @param pvObj The object pointer.
* @param pvUser1 Pointer to the network.
* @param pvUser2 Pointer to the INTNET instance data.
*/
{
Log(("intnetR0NetworkDestruct: pvObj=%p pNetwork=%p pIntNet=%p %s\n", pvObj, pNetwork, pIntNet, pNetwork->szName));
/*
* Tell the trunk, if present, that we're about to disconnect it and wish
* no further calls from it.
*/
if (pTrunk)
/*
* Deactivate and orphan any remaining interfaces and wait for them to idle.
*
* Note! Normally there are no more interfaces at this point, however, when
* supdrvCloseSession / supdrvCleanupSession release the objects the
* order is undefined. So, it's quite possible that the network will
* be dereference and destroyed before the interfaces.
*/
while (iIf-- > 0)
{
}
/* Wait for all the interfaces to quiesce. (Interfaces cannot be
removed / added since we're holding the big lock.) */
if (pTrunk)
while (iIf-- > 0)
/* Orphan the interfaces (not trunk). Don't bother with calling
pfnDisconnectInterface here since the networking is going away. */
{
{
}
}
/*
* Zap the trunk pointer while we still own the spinlock, destroy the
* trunk after we've left it. Note that this might take a while...
*/
if (pTrunk)
/*
* Unlink the network.
* Note that it needn't be in the list if we failed during creation.
*/
else
{
{
break;
}
}
/*
* Free resources.
*/
}
/**
* Opens an existing network.
*
* The call must own the INTNET::hMtxCreateOpenDestroy.
*
* @returns VBox status code.
* @param pIntNet The instance data.
* @param pSession The current session.
* @param pszNetwork The network name. This has a valid length.
* @param enmTrunkType The trunk type.
* @param pszTrunk The trunk name. Its meaning is specfic to the type.
* @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
* @param ppNetwork Where to store the pointer to the network on success.
*/
static int intnetR0OpenNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
{
LogFlow(("intnetR0OpenNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
/* just pro forma validation, the caller is internal. */
/*
* Search networks by name.
*/
while (pCur)
{
{
/*
* Found the network, now check that we have the same ideas
* about the trunk setup and security.
*/
int rc;
{
{
/*
* Increment the reference and check that the session
* can access this network.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
else
}
else if (rc == VERR_WRONG_ORDER)
}
else
}
else
return rc;
}
}
LogFlow(("intnetR0OpenNetwork: returns VERR_NOT_FOUND\n"));
return VERR_NOT_FOUND;
}
/**
* Creates a new network.
*
* The call must own the INTNET::hMtxCreateOpenDestroy and has already attempted
* opening the network and found it to be non-existing.
*
* @returns VBox status code.
* @param pIntNet The instance data.
* @param pSession The session handle.
* @param pszNetwork The name of the network. This must be at least one character long and no longer
* than the INTNETNETWORK::szName.
* @param enmTrunkType The trunk type.
* @param pszTrunk The trunk name. Its meaning is specfic to the type.
* @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
* @param ppNetwork Where to store the network. In the case of failure
* whatever is returned here should be dereferenced
* outside the INTNET::hMtxCreateOpenDestroy.
*/
static int intnetR0CreateNetwork(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
{
LogFlow(("intnetR0CreateNetwork: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x ppNetwork=%p\n",
/* just pro forma validation, the caller is internal. */
/*
* Allocate and initialize.
*/
if (!pNetwork)
return VERR_NO_MEMORY;
//pNetwork->pNext = NULL;
//pNetwork->pIfs = NULL;
//pNetwork->pvObj = NULL;
//else
// pNetwork->pbTmp = NULL;
//pNetwork->cActiveIFs = 0;
/*
* Create the semaphore, spinlock and allocate the interface table.
*/
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
pNetwork->MacTab.paEntries = (PINTNETMACTABENTRY)RTMemAlloc(sizeof(INTNETMACTABENTRY) * pNetwork->MacTab.cEntriesAllocated);
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
/*
* Register the object in the current session and link it into the network list.
*/
pNetwork->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK, intnetR0NetworkDestruct, pNetwork, pIntNet);
{
/*
* Check if the current session is actually allowed to create and
* open the network. It is possible to implement network name
* based policies and these must be checked now. SUPR0ObjRegister
* does no such checks.
*/
if (RT_SUCCESS(rc))
{
/*
* Connect the trunk.
*/
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
return rc;
}
/* cleanup */
rc = VERR_NO_MEMORY;
}
return rc;
}
/**
* Opens a network interface and connects it to the specified network.
*
* @returns VBox status code.
* @param pSession The session handle.
* @param pszNetwork The network name.
* @param enmTrunkType The trunk type.
* @param pszTrunk The trunk name. Its meaning is specfic to the type.
* @param fFlags Flags, see INTNET_OPEN_FLAGS_*.
* @param fRestrictAccess Whether new participants should be subjected to access check or not.
* @param cbSend The send buffer size.
* @param cbRecv The receive buffer size.
* @param phIf Where to store the handle to the network interface.
*/
{
LogFlow(("IntNetR0Open: pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x cbSend=%u cbRecv=%u phIf=%p\n",
/*
* Validate input.
*/
if (pszTrunk)
{
}
else
pszTrunk = "";
switch (enmTrunkType)
{
case kIntNetTrunkType_None:
if (*pszTrunk)
return VERR_INVALID_PARAMETER;
break;
case kIntNetTrunkType_NetFlt:
case kIntNetTrunkType_NetAdp:
if (!*pszTrunk)
return VERR_INVALID_PARAMETER;
break;
default:
return VERR_NOT_IMPLEMENTED;
}
/*
*/
if (RT_FAILURE(rc))
return rc;
/*
* Try open / create the network and create an interface on it for the
* caller to use.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
else
}
else if (rc == VERR_NOT_FOUND)
{
rc = intnetR0CreateNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
}
}
return rc;
}
/**
* VMMR0 request wrapper for IntNetR0Open.
*
* @returns see GMMR0MapUnmapChunk.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
{
return VERR_INVALID_PARAMETER;
}
/**
* Count the internal networks.
*
* This is mainly for providing the testcase with some introspection to validate
* behavior when closing interfaces.
*
* @returns The number of networks.
*/
{
/*
* Grab the instance.
*/
if (!pIntNet)
return 0;
AssertPtrReturn(pIntNet, 0);
/*
* Grab the mutex and count the networks.
*/
if (RT_FAILURE(rc))
return 0;
cNetworks++;
return cNetworks;
}
/**
* Destroys an instance of the Ring-0 internal networking service.
*/
INTNETR0DECL(void) IntNetR0Term(void)
{
LogFlow(("IntNetR0Term:\n"));
/*
* Zap the global pointer and validate it.
*/
if (!pIntNet)
return;
/*
* There is not supposed to be any networks hanging around at this time.
*/
{
}
{
/** @todo does it make sense to have a deleter here? */
}
}
/**
* Initalizes the internal network ring-0 service.
*
* @returns VBox status code.
*/
INTNETR0DECL(int) IntNetR0Init(void)
{
LogFlow(("IntNetR0Init:\n"));
int rc = VERR_NO_MEMORY;
if (pIntNet)
{
//pIntNet->pNetworks = NULL;
if (RT_SUCCESS(rc))
{
rc = RTHandleTableCreateEx(&pIntNet->hHtIfs, RTHANDLETABLE_FLAGS_LOCKED | RTHANDLETABLE_FLAGS_CONTEXT,
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
return rc;
}