DrvNAT.cpp revision 7e74e5d9c5956c67a23435fe7d401a56c31c5ab5
/** @file
*
* VBox network devices:
* NAT network transport driver
*/
/*
* 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_DRV_NAT
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#include "Network/slirp/libslirp.h"
#include <iprt/critsect.h>
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* NAT network transport driver instance data.
*/
typedef struct DRVNAT
{
/** The network interface. */
/** The port we're attached to. */
/** The network config of the port we're attached to. */
/** Pointer to the driver instance. */
/** Slirp critical section. */
/** Link state */
/** NAT state for this instance. */
/** TFTP directory prefix. */
char *pszTFTPPrefix;
/** Boot file name to provide in the DHCP server response. */
char *pszBootFile;
/** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
#define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#if 0
/** If set the thread should terminate. */
static bool g_fThreadTerm = false;
/** The thread id of the select thread (drvNATSelectThread()). */
static RTTHREAD g_ThreadSelect;
#endif
/**
* Send data to the network.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pvBuf Data to send.
* @param cb Number of bytes to send.
* @thread EMT
*/
{
Log2(("drvNATSend: pvBuf=%p cb=%#x\n"
"%.*Vhxd\n",
LogFlow(("drvNATSend: end\n"));
return VINF_SUCCESS;
}
/**
* Set promiscuous mode.
*
* This is called when the promiscuous mode is set. This means that there doesn't have
* to be a mode change when it's called.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
* @thread EMT
*/
static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
{
/* nothing to do */
}
/**
* Notification on link status changes.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param enmLinkState The new link state.
* @thread EMT
*/
static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
switch (enmLinkState)
{
case PDMNETWORKLINKSTATE_UP:
LogRel(("NAT: link up\n"));
break;
case PDMNETWORKLINKSTATE_DOWN:
LogRel(("NAT: link down\n"));
break;
default:
}
}
/**
* Poller callback.
*/
{
int cFDs = -1;
if (cReadFDs >= 0)
}
/**
* Function called by slirp to check if it's possible to feed incoming data to the network port.
* @returns 1 if possible.
* @returns 0 if not possible.
*/
int slirp_can_output(void *pvUser)
{
/** Happens during termination */
return 0;
return RT_SUCCESS(rc);
}
/**
* Function called by slirp to feed incoming data to the network port.
*/
{
Log2(("slirp_output: pu8Buf=%p cb=%#x (pData=%p)\n"
"%.*Vhxd\n",
/** Happens during termination */
return;
}
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
* @thread Any thread.
*/
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pData->INetworkConnector;
default:
return NULL;
}
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
LogFlow(("drvNATDestruct:\n"));
}
/**
* Sets up the redirectors.
*
* @returns VBox status code.
* @param pCfgHandle The drivers configuration handle.
*/
static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pData, PCFGMNODE pCfgHandle, RTIPV4ADDR Network)
{
/*
* Enumerate redirections.
*/
{
/*
* Validate the port forwarding config.
*/
return PDMDRV_SET_ERROR(pData->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
/* protocol type */
bool fUDP;
char szProtocol[32];
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
fUDP = false;
else if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"UDP\" boolean failed"), iInstance);
}
else if (VBOX_SUCCESS(rc))
{
fUDP = false;
fUDP = true;
else
return PDMDrvHlpVMSetError(pData->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""), iInstance, szProtocol);
}
else
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Protocol\" string failed"), iInstance);
/* host port */
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"HostPort\" integer failed"), iInstance);
/* guest port */
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestPort\" integer failed"), iInstance);
/* guest address */
char szGuestIP[32];
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
(Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16, (Network & 0xFF00) >> 8, (Network & 0xE0) | 15);
else if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestIP\" string failed"), iInstance);
return PDMDrvHlpVMSetError(pData->pDrvIns, VERR_NAT_REDIR_GUEST_IP, RT_SRC_POS, N_("NAT#%d: configuration error: invalid \"GuestIP\"=\"%s\", inet_aton failed"), iInstance, szGuestIP);
/*
* Call slirp about it.
*/
return PDMDrvHlpVMSetError(pData->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS, N_("NAT#%d: configuration error: failed to set up redirection of %d to %s:%d. Probably a conflict with existing services or other rules"), iInstance, iHostPort, szGuestIP, iGuestPort);
} /* for each redir rule */
return VINF_SUCCESS;
}
/**
* Get the MAC address into the slirp stack.
*/
{
{
}
}
/**
* After loading we have to pass the MAC address of the ethernet device to the slirp stack.
* Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
* (usually done during guest boot).
*/
{
return VINF_SUCCESS;
}
/**
* Some guests might not use DHCP to retrieve an IP but use a static IP.
*/
{
}
/**
* Construct a NAT network transport driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
{
char szNetAddr[16];
LogFlow(("drvNATConstruct:\n"));
/*
* Validate the config.
*/
return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown NAT configuration option, only supports PassDomain, TFTPPrefix, BootFile and Network"));
/*
* Init the static parts.
*/
/* IBase */
/* INetwork */
/*
* Get the configuration settings.
*/
bool fPassDomain = true;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
fPassDomain = true;
else if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"PassDomain\" boolean failed"), pDrvIns->iInstance);
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"TFTPPrefix\" string failed"), pDrvIns->iInstance);
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"BootFile\" string failed"), pDrvIns->iInstance);
/*
* Query the network port interface.
*/
pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
pData->pConfig = (PPDMINETWORKCONFIG)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_CONFIG);
/* Generate a network address for this network card. */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"Network\" string failed"), pDrvIns->iInstance);
if (RT_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: network '%s' describes not a valid IPv4 network"), pDrvIns->iInstance, szNetwork);
/*
* The slirp lock..
*/
if (VBOX_FAILURE(rc))
return rc;
#if 0
if (VBOX_SUCCESS(rc))
{
/*
* Start the select thread. (it'll block on the sem)
*/
g_fThreadTerm = false;
if (VBOX_SUCCESS(rc))
{
#endif
/*
* Initialize slirp.
*/
rc = slirp_init(&pData->pNATState, &szNetAddr[0], Netmask, fPassDomain, pData->pszTFTPPrefix, pData->pszBootFile, pData);
if (VBOX_SUCCESS(rc))
{
if (VBOX_SUCCESS(rc2))
{
/*
* Register a load done notification to get the MAC address into the slirp
* engine after we loaded a guest state.
*/
#if 0
RTThreadSleep(0);
#endif
/* might return VINF_NAT_DNS */
return rc;
}
/* failure path */
}
else
{
}
#if 0
g_fThreadTerm = true;
RTThreadSleep(0);
}
g_EventSem = NULL;
}
#endif
return rc;
}
/**
* NAT network transport driver registration record.
*/
{
/* u32Version */
/* szDriverName */
"NAT",
/* pszDescription */
"NAT Network Transport Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
16,
/* cbInstance */
sizeof(DRVNAT),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
};