SrvIntNetR0.cpp revision 1216aa894983959950add337f02d9dffe8468d47
/* $Id$ */
/** @file
* Internal networking - The ring 0 service.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SRV_INTNET
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
#include <iprt/handletable.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
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;
/**
* IPv4 address.
*/
typedef RTUINT32U INTNETADDRIPV4;
/** Pointer to a IPv4 address. */
typedef INTNETADDRIPV4 *PINTNETADDRIPV4;
/** Pointer to a const IPv4 address. */
typedef INTNETADDRIPV4 const *PCINTNETADDRIPV4;
/**
* IPv6 address.
*/
typedef RTUINT128U INTNETADDRIPV6;
/** Pointer to a IPv4 address. */
typedef INTNETADDRIPV6 *PINTNETADDRIPV6;
/** Pointer to a const IPv4 address. */
typedef INTNETADDRIPV6 const *PCINTNETADDRIPV6;
/**
* IPX address.
*/
typedef struct INTNETADDRIPX
{
/** The network ID. */
/** The node ID. (Defaults to the MAC address apparently.) */
/** Pointer to an IPX address. */
typedef INTNETADDRIPX *PINTNETADDRIPX;
/** Pointer to a const IPX address. */
typedef INTNETADDRIPX const *PCINTNETADDRIPX;
/**
* Address union.
*/
typedef union INTNETADDRU
{
/** 64-bit view. */
/** 32-bit view. */
/** 16-bit view. */
/** 8-bit view. */
/** IPv4 view. */
/** IPv6 view. */
/** IPX view. */
} INTNETADDRU;
/** Pointer to an address union. */
typedef INTNETADDRU *PINTNETADDRU;
/** Pointer to a const address union. */
typedef INTNETADDRU const *PCINTNETADDRU;
/**
* 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
{
/** Pointer to the next interface.
* This is protected by the INTNET::FastMutex. */
/** The current MAC address for the interface. */
/** Set if the INTNET::Mac member is valid. */
bool fMacSet;
/** Set if the interface is in promiscuous mode.
* In promiscuous mode the interface will receive all packages except the one it's sending. */
bool fPromiscuous;
/** Whether the interface is active or not. */
bool fActive;
/** Whether someone is currently in the destructor. */
bool volatile fDestroying;
/** Number of yields done to try make the interface read pending data.
* We will stop yeilding 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. */
/** Event semaphore which a receiver thread will sleep on while waiting for data to arrive. */
RTSEMEVENT volatile Event;
/** 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::FastMutex. */
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.) */
} 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. */
/** The trunk mutex that serializes all calls <b>to</b> the component. */
/** Pointer to the network we're connect to.
* This may be NULL if we're orphaned? */
struct INTNETNETWORK *pNetwork;
/** Whether to supply physical addresses with the outbound SGs. */
bool volatile fPhysSG;
/** Set if the 'wire' is in promiscuous mode.
* The state of the 'host' is queried each time. */
bool fPromiscuousWire;
/** 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::FastMutex. */
struct INTNETNETWORK *pNext;
/** List of interfaces connected to the network.
* This is protected by the INTNET::FastMutex. */
/** Pointer to the trunk interface.
* Can be NULL if there is no trunk connection. */
/** The network mutex.
* It protects everything dealing with this network. */
/** Pointer to the instance data. */
/** The SUPR0 object id. */
void *pvObj;
/** 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;
/**
* Internal networking instance.
*/
typedef struct INTNET
{
/** Mutex protecting the network creation, opening and destruction.
* (This means all operations affecting the pNetworks list.) */
/** List of networks. Protected by INTNET::Spinlock. */
PINTNETNETWORK volatile pNetworks;
/** Handle table for the interfaces. */
} INTNET;
/**
* Ethernet header.
*/
#pragma pack(1)
typedef struct INTNETETHERHDR
{
#pragma pack()
/** Pointer to an ethernet header. */
typedef INTNETETHERHDR *PINTNETETHERHDR;
/** Pointer to a const ethernet header. */
typedef INTNETETHERHDR const *PCINTNETETHERHDR;
/** @name EtherType
* @{ */
/** @} */
#pragma pack(1)
typedef struct INTNETIPV4
{
#ifdef RT_BIG_ENDIAN
unsigned int ip_v : 4;
unsigned int ip_hl : 4;
unsigned int ip_tos : 8;
unsigned int ip_len : 16;
#else
unsigned int ip_hl : 4;
unsigned int ip_v : 4;
unsigned int ip_tos : 8;
unsigned int ip_len : 16;
#endif
/* more */
} INTNETIPV4;
typedef INTNETIPV4 *PINTNETIPV4;
typedef INTNETIPV4 const *PCINTNETIPV4;
typedef struct INTNETUDPV4
{
} INTNETUDPV4;
typedef INTNETUDPV4 *PINTNETUDPV4;
typedef INTNETUDPV4 const *PCINTNETUDPV4;
typedef struct INTNETDHCP
{
} INTNETDHCP;
typedef INTNETDHCP *PINTNETDHCP;
typedef INTNETDHCP const *PCINTNETDHCP;
#pragma pack(0)
/** IPv4: UDP */
#define INTNETIPV4_PROT_UDP 7
/** ARP hardware type - ethernet. */
/** @name ARP operations
* @{ */
#define INTNET_ARPOP_REQUEST UINT16_C(1) /**< Request hardward address given a protocol address (ARP). */
#define INTNET_ARPOP_REVREQUEST UINT16_C(3) /**< Request protocol address given a hardware address (RARP). */
/** @} */
#pragma pack(1)
/**
* Ethernet ARP header.
*/
typedef struct INTNETARPHDR
{
/** The hardware type. */
/** The protocol type (ethertype). */
/** The hardware address length. */
/** The protocol address length. */
/** The operation. */
} INTNETARPHDR;
#pragma pack(0)
/** Pointer to an ethernet ARP header. */
typedef INTNETARPHDR *PINTNETARPHDR;
/** Pointer to a const ethernet ARP header. */
typedef INTNETARPHDR const *PCINTNETARPHDR;
#pragma pack(1)
/**
* Ethernet IPv4 + 6-byte MAC ARP request packet.
*/
typedef struct INTNETARPIPV4
{
/** The sender hardware address. */
/** The sender protocol address. */
/** The target hardware address. */
/** The arget protocol address. */
#pragma pack(0)
/** Pointer to an ethernet IPv4+MAC ARP request packet. */
typedef INTNETARPIPV4 *PINTNETARPIPV4;
/** Pointer to a const ethernet IPv4+MAC ARP request packet. */
typedef INTNETARPIPV4 const *PCINTNETARPIPV4;
#pragma pack(1)
/**
* Ethernet IPv6 + 6-byte MAC ARP request packet.
*/
typedef struct INTNETARPIPV6
{
/** The sender hardware address. */
/** The sender protocol address. */
/** The target hardware address. */
/** The arget protocol address. */
#pragma pack(0)
/** Pointer to an ethernet IPv6+MAC ARP request packet. */
typedef INTNETARPIPV6 *PINTNETARPIPV6;
/** Pointer to a const ethernet IPv6+MAC ARP request packet. */
typedef INTNETARPIPV6 const *PCINTNETARPIPV6;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* 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 VBox status code, can assume success in most situations.
* @param pIf The interface instance.
* @param pSession The current session.
*/
{
}
/**
* 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;
}
/**
* 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(PCINTNETADDRU pAddr1, PCINTNETADDRU 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, PCINTNETADDRU 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, PCINTNETADDRU 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--;
if (intnetR0AddrUIsEqualEx((PCINTNETADDRU)(pCache->pbEntries + pCache->cbEntry * i), pAddr, cbAddr))
return i;
if (i <= 1)
return -1;
}
/** Same as intnetR0IfAddrCacheLookup except we expect the address to be present already. */
DECLINLINE(int) intnetR0IfAddrCacheLookupLikely(PCINTNETADDRCACHE pCache, PCINTNETADDRU 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, PCINTNETADDRU 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, PCINTNETADDRU 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((PCINTNETADDRU)(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.
*/
{
(int)(intptr_t)(pCache - &pIf->aAddrCache[0]), iEntry, pCache->cbAddress, pCache->pbEntries + iEntry * pCache->cbEntry));
}
/**
* Deletes an address from the cache, assuming it isn't actually in the cache.
*
* @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, PCINTNETADDRU pAddr, uint8_t const cbAddr)
{
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).
*/
{
{
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).
*/
DECLINLINE(void) intnetR0NetworkAddrCacheDeleteMinusIf(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PCINTNETADDRU pAddr,
{
{
if (RT_UNLIKELY(i >= 0))
}
}
/**
* Adds an address to the cache, the caller is responsible for making sure it'
* s not already in the cache.
*
* @param pIf The interface (for logging).
* @param pCache The address cache.
* @param pAddr The address.
*/
{
if (!pCache->cEntriesAlloc)
{
/* Allocate the first array */
return;
}
else
{
bool fReplace = true;
{
if (pvNew)
{
fReplace = false;
}
}
if (fReplace)
{
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.
*/
Log(("intnetR0IfAddrCacheAddIt: type=%d added #%d %.*Rhxs\n",
}
/**
* 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).
*/
static void intnetR0IfAddrCacheAddSlow(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCINTNETADDRU pAddr, uint8_t const cbAddr)
{
/*
* Check all but the first and last entries, the caller
* has already checked those.
*/
while (--cLeft > 1)
{
{
return;
}
cLeft--;
}
/*
* Not found, add it.
*/
}
/**
* Adds an address to the cache if it's not already there.
*
* @param pIf The interface (for logging).
* @param pCache The address cache.
* @param pAddr The address.
* @param cbAddr The size of the address (optimization).
*/
DECLINLINE(void) intnetR0IfAddrCacheAdd(PINTNETIF pIf, PINTNETADDRCACHE pCache, PCINTNETADDRU pAddr, uint8_t const cbAddr)
{
/*
* The optimized case is when the address the first cache entry.
*/
if (RT_LIKELY( i > 0
{
return;
}
if (i <= 1)
return;
/*
* And the case where it's the last entry.
*/
i--;
{
return;
}
if (i <= 1)
return;
}
/**
* 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 pHdr 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.
*/
{
/*
* Check the header size.
*/
if (cbPacket < RT_UOFFSETOF(INTNETIPV4, ip_options)) /** @todo check minimum size requirements here. */
return;
return;
/*
* Ignore 255.255.255.255 (broadcast), 0.0.0.0 (null) and already cached addresses.
*/
return;
int i = -1;
|| (i = intnetR0IfAddrCacheLookupLikely(&pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, sizeof(pHdr->ip_src))) >= 0)
{
#if 0 /** @todo quick DHCP check? */
return;
#endif
return;
}
/*
* Check the header checksum.
*/
/** @todo IP checksumming */
/*
* Add the source address.
*/
if (i < 0)
/*
* Check for DHCP (properly this time).
*/
/** @todo DHCPRELEASE request? */
}
/**
* Sniff 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).
*/
{
/*
* Ignore malformed packets and packets which doesn't interrest us.
*/
/** @todo Read up on inverse ARP. */
)
)
)
return;
/*
* Deal with the protocols.
*/
{
/*
* IPv4.
*/
|| pReq->ar_spa.u == 0 /** @todo add an inline function for checking that an IP address is worth caching. */
return;
{
intnetR0IfAddrCacheDelete(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv4], &Addr, sizeof(pReq->ar_tpa));
}
}
{
/*
* IPv6.
*/
/** @todo IPv6 || pReq->ar_spa.au32[0] == 0 or something
|| pReq->ar_spa.au32[0] == UINT32_C(0xffffffff)*/))
return;
{
intnetR0IfAddrCacheDelete(pIf, &pIf->aAddrCache[kIntNetAddrType_IPv6], &Addr, sizeof(pReq->ar_tpa));
}
}
else
}
/**
* 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.
*/
{
/*
* Fish out the ethertype and look for stuff we can handle.
*/
if (cbFrame <= sizeof(INTNETETHERHDR))
return;
cbFrame -= sizeof(INTNETETHERHDR);
#if 0 /** @todo IntNet: implement IPv6 for wireless MAC sharing. */
#endif
#if 0 /** @todo IntNet: implement IPX for wireless MAC sharing? */
)
#endif
}
/**
* Initializes a scatter / gather buffer from a simple linear buffer.
*
* @returns Pointer to the start of the frame.
* @param pSG Pointer to the scatter / gather structure.
* (The pvOwnerData, fFlags, cUsers, and cSegsAlloc members are left untouched.)
* @param pvFrame Pointer to the frame
* @param cbFrame The size of the frame.
*/
{
}
/**
* Initializes a scatter / gather buffer from a internal networking packet.
*
* @returns Pointer to the start of the frame.
* @param pSG Pointer to the scatter / gather structure.
* (The pvOwnerData, fFlags, cUsers, and cSegsAlloc members are left untouched.)
* @param pHdr Pointer to the packet header.
* @param pBuf The buffer the header is within. Only used in strict builds.
*/
{
}
#ifdef IN_INTNET_TESTCASE
/**
* Reads the next frame in the buffer.
* The caller is responsible for ensuring that there is a valid frame in the buffer.
*
* @returns Size of the frame in bytes.
* @param pBuf The buffer.
* @param pRingBuff The ring buffer to read from.
* @param pvFrame Where to put the frame. The caller is responsible for
* ensuring that there is sufficient space for the frame.
*/
{
/* skip the frame */
return cb;
}
#endif /* IN_INTNET_TESTCASE */
/**
* Reads an entire SG into a fittingly size buffer.
*
* @param pSG The SG list to read.
* @param pvBuf The buffer to read into (at least pSG->cbTotal in size).
*/
{
{
}
else
{
{
}
}
}
/**
* Writes a frame packet to the buffer.
*
* @returns VBox status code.
* @param pBuf The buffer.
* @param pRingBuf The ring buffer to read from.
* @param pSG The gatter list.
*/
{
/*
* Validate input.
*/
{
/*
* Try fit it all before the end of the buffer.
*/
{
return VINF_SUCCESS;
}
/*
* Try fit the frame at the start of the buffer.
* (The header fits before the end of the buffer because of alignment.)
*/
AssertMsg(pRingBuf->offEnd - offWrite >= sizeof(INTNETHDR), ("offEnd=%x offWrite=%x\n", pRingBuf->offEnd, offWrite));
{
return VINF_SUCCESS;
}
}
/*
* The reader is ahead of the writer, try fit it into that space.
*/
{
return VINF_SUCCESS;
}
/* (it didn't fit) */
/** @todo stats */
return VERR_BUFFER_OVERFLOW;
}
/**
* 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.
*/
{
// LogFlow(("intnetR0IfSend: pIf=%p:{.hIf=%RX32}\n", pIf, pIf->hIf));
if (RT_SUCCESS(rc))
{
return;
}
#if 0 /* This is bad stuff now as we're blocking while locking down the network.
we really shouldn't delay the network traffic on the host just because
some bugger isn't responding. Will have to deal with this in a different
manner if required. */
/*
* Retry a few times, yielding the CPU in between.
* But don't let a unresponsive VM harm performance, so give up after a couple of tries.
*/
{
unsigned cYields = 10;
#else
/*
* Scheduling hack, for unicore machines primarily.
*/
&& pIfSender /* but not if it's from the trunk */)
{
unsigned cYields = 2;
#endif
while (--cYields > 0)
{
if (RT_SUCCESS(rc))
{
return;
}
}
}
/* ok, the frame is lost. */
}
/**
* Sends a frame down the trunk.
*
* The caller must own the network mutex, might be abandond temporarily.
* The fTrunkLock parameter indicates whether the trunk lock is held.
*
* @param pThis The trunk.
* @param pNetwork The network the frame is being sent to.
* @param fDst The destination flags.
* @param pSG Pointer to the gather list.
* @param fTrunkLocked Whether the caller owns the out-bound trunk lock.
*/
static void intnetR0TrunkIfSend(PINTNETTRUNKIF pThis, PINTNETNETWORK pNetwork, uint32_t fDst, PINTNETSG pSG, bool fTrunkLocked)
{
/*
* Quick sanity check.
*/
/*
* Temporarily leave the network lock while transmitting the frame.
*
* Note that we're relying on the out-bound lock to serialize threads down
* in INTNETR0IfSend. It's theoretically possible for there to be race now
* because I didn't implement async SG handling yet. Which is why we currently
* require the trunk to be locked, well, one of the reasons.
*
* Another reason is that the intnetR0NetworkSendUnicast code may have to
* call into the trunk interface component to do package switching.
*/
int rc;
if ( fTrunkLocked
{
if (RT_SUCCESS(rc))
{
if ( fTrunkLocked
{
if (!fTrunkLocked)
}
else
{
AssertFailed();
}
}
if (!fTrunkLocked)
}
else
{
AssertFailed();
}
/** @todo failure statistics? */
}
/**
* Sends a broadcast frame.
*
* The caller must own the network mutex, might be abandond temporarily.
* When pIfSender is not NULL, the caller must also own the trunk semaphore.
*
* @returns true if it's addressed to someone on the network, otherwise false.
* @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 fTrunkLocked Whether the caller owns the out-bound trunk lock.
*/
static bool intnetR0NetworkSendBroadcast(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, PINTNETSG pSG, bool fTrunkLocked)
{
/*
* This is a broadcast or multicast address. For the present we treat those
* two as the same - investigating multicast is left for later.
*
* Write the packet to all the interfaces and signal them.
*/
/*
* Unless the trunk is the origin, broadcast it to both the wire
* and the host as well.
*/
if ( pIfSender
&& pTrunkIf)
intnetR0TrunkIfSend(pTrunkIf, pNetwork, INTNETTRUNKDIR_HOST | INTNETTRUNKDIR_WIRE, pSG, fTrunkLocked);
return false; /* broadcast frames are never dropped */
}
/**
* Sends a multicast frame.
*
* The caller must own the network mutex, might be abandond temporarily.
*
* @returns true if it's addressed to someone on the network, otherwise false.
* @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 fTrunkLocked Whether the caller owns the out-bound trunk lock.
* @param pEthHdr Pointer to the ethernet header.
*/
static bool intnetR0NetworkSendMulticast(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc, PINTNETSG pSG, bool fTrunkLocked, PCINTNETETHERHDR pEthHdr)
{
/** @todo implement multicast */
}
/**
* Sends a unicast frame.
*
* The caller must own the network mutex, might be abandond temporarily.
*
* @returns true if it's addressed to someone on the network, otherwise false.
* @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 fTrunkLocked Whether the caller owns the out-bound trunk lock.
* @param pEthHdr Pointer to the ethernet header.
*/
static bool intnetR0NetworkSendUnicast(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc, PINTNETSG pSG, bool fTrunkLocked, PCINTNETETHERHDR pEthHdr)
{
/*
* Only send to the interfaces with matching a MAC address.
*/
bool fExactIntNetRecipient = false;
{
bool fIt = false;
|| ( pIf->fPromiscuous
&& !(pNetwork->fFlags & (INTNET_OPEN_FLAGS_IGNORE_PROMISC | INTNET_OPEN_FLAGS_QUIETLY_IGNORE_PROMISC))
{
}
}
/*
* Send it to the trunk?
* If we didn't find the recipient on the internal network the
* frame will hit the wire.
*/
if ( pIfSender
&& pTrunkIf
{
/* promiscuous checks first as they are cheaper than pfnIsHostMac. */
if ( pTrunkIf->fPromiscuousWire
&& !(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)) )
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))
&& !fExactIntNetRecipient /* if you have duplicate mac addresses, you're screwed. */ )
{
else
}
if (fDst)
}
/* log it */
if ( !fExactIntNetRecipient
&& !fDst
&& ( (pEthHdr->MacDst.au8[0] == 0x08 && pEthHdr->MacDst.au8[1] == 0x00 && pEthHdr->MacDst.au8[2] == 0x27)
|| (pEthHdr->MacSrc.au8[0] == 0x08 && pEthHdr->MacSrc.au8[1] == 0x00 && pEthHdr->MacSrc.au8[2] == 0x27)))
return fExactIntNetRecipient;
}
/**
* 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 true if it's addressed to someone on the network, otherwise false.
* @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 fTrunkLocked Whether the caller owns the out-bound trunk lock.
*/
static bool intnetR0NetworkSend(PINTNETNETWORK pNetwork, PINTNETIF pIfSender, uint32_t fSrc, PINTNETSG pSG, bool fTrunkLocked)
{
bool fRc = false;
/*
* Assert reality.
*/
return fRc;
/*
* Send statistics.
*/
if (pIfSender)
{
}
/*
* Get the ethernet header (might theoretically involve multiple segments).
*/
else
{
{
}
AssertReturn(!cbLeft, false);
}
Log2(("D=%.6Rhxs S=%.6Rhxs T=%04x f=%x z=%x\n",
/*
* Inspect the header updating the mac address of the sender in the process.
*/
if ( pIfSender
{
/** @todo stats */
}
/*
* Distribute the frame.
*/
else
return fRc;
}
/**
* 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.
*
* @returns VBox status code.
* @param pIntNet The instance data.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param pvFrame Pointer to the frame. Optional, please don't use.
* @param cbFrame Size of the frame. Optional, please don't use.
*/
INTNETR0DECL(int) INTNETR0IfSend(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, const void *pvFrame, unsigned cbFrame)
{
Log5(("INTNETR0IfSend: pIntNet=%p hIf=%RX32 pvFrame=%p cbFrame=%u\n", pIntNet, hIf, pvFrame, cbFrame));
/*
* Validate input and translate the handle.
*/
{
/* This is the better place to crash, probe the buffer. */
}
if (!pIf)
return VERR_INVALID_HANDLE;
/*
* Lock the network. If there is a trunk retain it and grab its
* out-bound lock (this requires leaving the network lock first).
* Grabbing the out-bound lock here simplifies things quite a bit
* later on, so while this is excessive and a bit expensive it's
* not worth caring about right now.
*/
if (RT_FAILURE(rc))
{
return rc;
}
if (pTrunkIf)
{
if (!intnetR0TrunkIfOutLock(pTrunkIf))
{
return VERR_SEM_DESTROYED;
}
if (RT_FAILURE(rc))
{
return rc;
}
}
* with buffer sharing for some OS or service. Darwin copies everything so
* I won't bother allocating and managing SGs rigth now. Sorry. */
/*
* Process the argument.
*/
{
}
/*
* Process the send buffer.
*/
{
/* Send the frame if the type is sane. */
{
if (pvCurFrame)
{
}
}
/* else: ignore the frame */
/* Skip to the next frame. */
}
/*
* Release the semaphore(s) and release references.
*/
if (pTrunkIf)
{
}
return rc;
}
/**
* VMMR0 request wrapper for INTNETR0IfSend.
*
* @returns see INTNETR0IfSend.
* @param pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) INTNETR0IfSendReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSENDREQ pReq)
{
return VERR_INVALID_PARAMETER;
}
/**
* Maps the default buffer into ring 3.
*
* @returns VBox status code.
* @param pIntNet The instance data.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param ppRing3Buf Where to store the address of the ring-3 mapping.
*/
INTNETR0DECL(int) INTNETR0IfGetRing3Buffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, R3PTRTYPE(PINTNETBUF) *ppRing3Buf)
{
LogFlow(("INTNETR0IfGetRing3Buffer: pIntNet=%p hIf=%RX32 ppRing3Buf=%p\n", pIntNet, hIf, ppRing3Buf));
/*
* Validate input.
*/
*ppRing3Buf = 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))
{
}
return rc;
}
/**
* VMMR0 request wrapper for INTNETR0IfGetRing3Buffer.
*
* @returns see INTNETR0IfGetRing3Buffer.
* @param pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) INTNETR0IfGetRing3BufferReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFGETRING3BUFFERREQ pReq)
{
return VERR_INVALID_PARAMETER;
}
/**
* Gets the ring-0 address of the current buffer.
*
* @returns VBox status code.
* @param pIntNet The instance data.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param ppRing0Buf Where to store the address of the ring-3 mapping.
*/
INTNETR0DECL(int) INTNETR0IfGetRing0Buffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF *ppRing0Buf)
{
LogFlow(("INTNETR0IfGetRing0Buffer: pIntNet=%p hIf=%RX32 ppRing0Buf=%p\n", pIntNet, hIf, ppRing0Buf));
/*
* Validate input.
*/
*ppRing0Buf = NULL;
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))
{
}
return rc;
}
#if 0
/**
* Gets the physical addresses of the default interface buffer.
*
* @returns VBox status code.
* @param pIntNet The instance data.
* @param hIF The interface handle.
* @param paPages Where to store the addresses. (The reserved fields will be set to zero.)
* @param cPages
*/
INTNETR0DECL(int) INTNETR0IfGetPhysBuffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPPAGE paPages, unsigned 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 pIntNet The instance handle.
* @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(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fPromiscuous)
{
LogFlow(("INTNETR0IfSetPromiscuousMode: pIntNet=%p hIf=%RX32 fPromiscuous=%d\n", pIntNet, hIf, fPromiscuous));
/*
* Validate & translate input.
*/
if (!pIf)
{
Log(("INTNETR0IfSetPromiscuousMode: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
/*
* Grab the network semaphore and make the change.
*/
int rc;
if (pNetwork)
{
if (RT_SUCCESS(rc))
{
{
Log(("INTNETR0IfSetPromiscuousMode: hIf=%RX32: Changed from %d -> %d\n",
}
}
}
else
return rc;
}
/**
* VMMR0 request wrapper for INTNETR0IfSetPromiscuousMode.
*
* @returns see INTNETR0IfSetPromiscuousMode.
* @param pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) INTNETR0IfSetPromiscuousModeReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSETPROMISCUOUSMODEREQ pReq)
{
return VERR_INVALID_PARAMETER;
}
/**
* Sets the MAC address of an interface.
*
* @returns VBox status code.
* @param pIntNet The instance handle.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param pMAC The new MAC address.
*/
INTNETR0DECL(int) INTNETR0IfSetMacAddress(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PCPDMMAC pMac)
{
LogFlow(("INTNETR0IfSetMacAddress: pIntNet=%p hIf=%RX32 pMac=%p:{%.6Rhxs}\n", pIntNet, hIf, pMac, pMac));
/*
* Validate & translate input.
*/
if (!pIf)
{
Log(("INTNETR0IfSetMacAddress: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
/*
* Grab the network semaphore and make the change.
*/
int rc;
if (pNetwork)
{
if (RT_SUCCESS(rc))
{
{
Log(("INTNETR0IfSetMacAddress: hIf=%RX32: Changed from %.6Rhxs -> %.6Rhxs\n",
}
}
}
else
return rc;
}
/**
* VMMR0 request wrapper for INTNETR0IfSetMacAddress.
*
* @returns see INTNETR0IfSetMacAddress.
* @param pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) INTNETR0IfSetMacAddressReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSETMACADDRESSREQ pReq)
{
return VERR_INVALID_PARAMETER;
}
/**
* Worker for intnetR0IfSetActive.
*
* 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 */
/*
* If we've got a trunk, lock it now in case we need to call out, and
* then lock the network.
*/
return VERR_SEM_DESTROYED;
if (RT_SUCCESS(rc))
{
bool fNetworkLocked = true;
/*
* Make the change if necessary.
*/
{
if ( pTrunkIf
&& ( !pNetwork->cActiveIFs
|| !cActiveIFs))
{
/*
* We'll have to change the trunk status, so, leave
* the network semaphore so we don't create any deadlocks.
*/
fNetworkLocked = false;
}
}
if (fNetworkLocked)
}
if (pTrunkIf)
return rc;
}
/**
* Activates or deactivates a interface.
*
* This is used to enable and disable the trunk connection on demans as well as
* know when not to expect an interface to want to receive packets.
*
* @returns VBox status code.
* @param pIf The interface.
* @param fActive What to do.
*/
{
/* quick sanity check */
/*
* Hand it to the network since it might involve the trunk
* and things are tricky there wrt to locking order.
*/
if (!pNetwork)
return VERR_WRONG_ORDER;
}
/**
* Sets the active property of an interface.
*
* @returns VBox status code.
* @param pIntNet The instance handle.
* @param hIf The interface handle.
* @param pSession The caller's session.
* @param fActive The new state.
*/
INTNETR0DECL(int) INTNETR0IfSetActive(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, bool fActive)
{
/*
* 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.
*/
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.
*/
INTNETR0DECL(int) INTNETR0IfSetActiveReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFSETACTIVEREQ pReq)
{
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 pIntNet The instance handle.
* @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.
*/
INTNETR0DECL(int) INTNETR0IfWait(PINTNET pIntNet, INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, uint32_t cMillies)
{
/*
* Get and validate essential handles.
*/
if (!pIf)
{
Log(("INTNETR0IfWait: returns VERR_INVALID_HANDLE\n"));
return VERR_INVALID_HANDLE;
}
&& Event != NIL_RTSEMEVENT)
{
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 pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) INTNETR0IfWaitReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFWAITREQ pReq)
{
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. */
/*
* Release the references to the interface object (handle + free lookup).
* But signal the event semaphore first so any waiter holding a reference
* will wake up too (he'll see hIf == invalid and return correctly).
*/
return rc;
}
/**
* VMMR0 request wrapper for INTNETR0IfCloseReq.
*
* @returns see INTNETR0IfClose.
* @param pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
INTNETR0DECL(int) INTNETR0IfCloseReq(PINTNET pIntNet, PSUPDRVSESSION pSession, PINTNETIFCLOSEREQ pReq)
{
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.
*/
{
/*
* Mark the interface as being destroyed so the waiter
* can behave appropriately (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 unlink ourselves from it.
* Because of cleanup order we might be an orphan now.
*/
if (pNetwork)
{
intnetR0IfSetActive(pIf, false);
else
{
while (pPrev)
{
{
break;
}
}
}
/*
* Release our reference to the network.
*/
}
else
/*
* 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;
}
/*
* The interface.
*/
}
/**
* 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.
* @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, bool *pfCloseNetwork, PINTNETIFHANDLE phIf)
{
LogFlow(("intnetR0NetworkCreateIf: pNetwork=%p pSession=%p cbSend=%u cbRecv=%u phIf=%p\n",
/*
* Assert input.
*/
*pfCloseNetwork = false;
/*
* Allocate and initialize the interface structure.
*/
if (!pIf)
return VERR_NO_MEMORY;
//pIf->pNext = NULL;
//pIf->fMacSet = false;
//pIf->fPromiscuous = false;
//pIf->fActive = false;
//pIf->fDestroying = false;
//pIf->pIntBuf = 0;
//pIf->pIntBufR3 = NIL_RTR3PTR;
//pIf->pIntBufDefault = 0;
//pIf->pIntBufDefaultR3 = NIL_RTR3PTR;
//pIf->cYields = 0;
//pIf->cSleepers = 0;
//pIf->pvObj = NULL;
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... */
/* receive ring buffer. */
/* send ring buffer. */
/*
* Link the interface to the network.
*/
if (RT_SUCCESS(rc))
{
/*
* Register the interface with the session.
*/
pIf->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE, intnetR0IfDestruct, pIf, pNetwork->pIntNet);
{
if (RT_SUCCESS(rc))
{
Log(("intnetR0NetworkCreateIf: returns VINF_SUCCESS *phIf=%RX32 cbSend=%u cbRecv=%u cbBuf=%u\n",
return VINF_SUCCESS;
}
return rc;
}
}
}
}
*pfCloseNetwork = true;
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::pfnRecv */
static DECLCALLBACK(bool) intnetR0TrunkIfPortRecv(PINTNETTRUNKSWPORT pSwitchPort, PINTNETSG pSG, uint32_t fSrc)
{
/* assert some sanity */
AssertPtrReturn(pNetwork, false);
/*
* Lock the network and send the frame to it.
*/
AssertRCReturn(rc, false);
bool fRc;
else
fRc = false; /* don't drop it */
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.
*/
{
}
/**
* Takes the out-bound trunk lock.
*
* This will ensure that pIfPort is valid.
*
* @returns success indicator.
* @param pThis The trunk.
*
*/
{
AssertPtrReturn(pThis, false);
if (RT_SUCCESS(rc))
{
return true;
}
else
return false;
}
/**
* Releases the out-bound trunk lock.
*
* @param pThis The trunk.
*/
{
if (pThis)
{
}
}
/**
* Activates the trunk interface.
*
* @param pThis The trunk.
* @param fActive What to do with it.
*
*/
{
if (intnetR0TrunkIfOutLock(pThis))
{
}
}
/**
* Shutdown the trunk interface.
*
* @param pThis The trunk.
* @param pNetworks The network.
*
* @remarks The caller must *NOT* hold the network lock. The global
*/
{
/* 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.
*/
}
/**
* 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_NetTap:
pszName = "VBoxNetTap";
break;
case kIntNetTrunkType_SrvNat:
pszName = "VBoxSrvNat";
break;
}
/*
* Allocate the trunk interface.
*/
if (!pTrunkIF)
return VERR_NO_MEMORY;
//pTrunkIF->pIfPort = NULL;
//pTrunkIF->fPhysSG = false;
//pTrunkIF->fPromiscuousWire = false;
if (RT_SUCCESS(rc))
{
#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))
{
rc = pTrunkFactory->pfnCreateAndConnect(pTrunkFactory, pNetwork->szTrunk, &pTrunkIF->SwitchPort, &pTrunkIF->pIfPort);
if (RT_SUCCESS(rc))
{
LogFlow(("intnetR0NetworkCreateTrunkIf: VINF_SUCCESS - pszName=%s szTrunk=%s Network=%s\n",
return VINF_SUCCESS;
}
}
#endif /* IN_RING0 */
}
LogFlow(("intnetR0NetworkCreateTrunkIf: %Rrc - pszName=%s szTrunk=%s Network=%s\n",
return rc;
}
/**
*
* @param pNetwork The network to close.
* @param pSession The session handle.
*/
{
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));
/*
* Deactivate the trunk connection first (if any).
*/
/*
* Unlink the network.
* Note that it needn't be in the list if we failed during creation.
*/
else
{
{
break;
}
}
/*
* Because of the undefined order of the per session object dereferencing when closing a session,
* we have to handle the case where the network is destroyed before the interfaces. We'll
* deal with this by simply orphaning the interfaces.
*/
while (pCur)
{
}
/* Grab and zap the trunk pointer before leaving the mutex. */
/*
* If there is a trunk, delete it.
* Note that this may tak a while if we're unlucky...
*/
if (pTrunkIF)
/*
* Free resources.
*/
}
/**
* Opens an existing network.
*
* @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::FastMutex 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::FastMutex.
*/
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 (!pNew)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
//pNew->pIFs = NULL;
//pNew->cActiveIFs = 0;
/*
* Register the object in the current session and link it into the network list.
*/
pNew->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_INTERNAL_NETWORK, intnetR0NetworkDestruct, pNew, 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;
}
}
/*
* We unlink it here so it cannot be opened when the caller leaves
* INTNET::FastMutex before dereferencing it.
*/
return rc;
}
rc = VERR_NO_MEMORY;
}
return rc;
}
/**
* Opens a network interface and connects it to the specified network.
*
* @returns VBox status code.
* @param pIntNet The internal network instance.
* @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: pIntNet=%p pSession=%p pszNetwork=%p:{%s} enmTrunkType=%d pszTrunk=%p:{%s} fFlags=%#x cbSend=%u cbRecv=%u phIf=%p\n",
pIntNet, pSession, pszNetwork, pszNetwork, pszTrunk, pszTrunk, enmTrunkType, fFlags, cbSend, cbRecv, phIf));
/*
* Validate input.
*/
if (pszTrunk)
{
}
else
pszTrunk = "";
switch (enmTrunkType)
{
case kIntNetTrunkType_None:
break;
case kIntNetTrunkType_NetFlt:
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.
*
* Note that because of the destructors grabbing INTNET::FastMutex and us being required
* to own this semaphore for the entire network opening / creation and interface creation
* sequence, intnetR0CreateNetwork will have to defer the network cleanup to us on failure.
*/
{
bool fCloseNetwork = true;
if (rc == VERR_NOT_FOUND)
rc = intnetR0CreateNetwork(pIntNet, pSession, pszNetwork, enmTrunkType, pszTrunk, fFlags, &pNetwork);
if (RT_SUCCESS(rc))
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0MapUnmapChunk.
*
* @returns see GMMR0MapUnmapChunk.
* @param pIntNet The internal networking instance.
* @param pSession The caller's session.
* @param pReq The request packet.
*/
{
return VERR_INVALID_PARAMETER;
}
/**
* Destroys an instance of the Ring-0 internal networking service.
*
* @param pIntNet Pointer to the instance data.
*/
{
/*
* Allow NULL pointers.
*/
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? */
}
}
/**
* Create an instance of the Ring-0 internal networking service.
*
* @returns VBox status code.
* @param ppIntNet Where to store the instance pointer.
*/
{
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;
}