DrvNAT.cpp revision da957c069c2a3c582fe265ff88170ce4c42b499d
/** @file
*
* VBox network devices:
* NAT network transport driver
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_NAT
#include "Network/slirp/libslirp.h"
#include <iprt/critsect.h>
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Block driver instance data.
*/
typedef struct DRVNAT
{
/** The network interface. */
/** The port we're attached to. */
/** Pointer to the driver instance. */
/** Slirp critical section. */
/** Link state */
/** NAT state for this instance. */
/** 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:
}
}
/**
* More receive buffer has become available.
*
* This is called when the NIC frees up receive buffers.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @thread EMT
*/
{
LogFlow(("drvNATNotifyCanReceive:\n"));
/** @todo do something useful here. */
}
/**
* 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;
}
/**
* 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.
*/
{
/*
* 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 returned %Vrc"), iInstance, rc);
}
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 returned %Vrc"), iInstance, rc);
/* host port */
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"HostPort\" integer returned %Vrc"), iInstance, rc);
/* guest port */
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestPort\" integer returned %Vrc"), iInstance, rc);
/* guest address */
char szGuestIP[32];
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"GuestIP\" string returned %Vrc"), iInstance, rc);
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;
}
/**
* 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, "Unknown NAT configuration option, only supports PassDomain");
/*
* Get the configuration settings.
*/
bool fPassDomain = true;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
fPassDomain = true;
else if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pData->pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: configuration query for \"PassDomain\" boolean returned %Vrc"), pDrvIns->iInstance, rc);
/*
* Init the static parts.
*/
/* IBase */
/* INetwork */
/*
* Query the network port interface.
*/
pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
/* Generate a network address for this network card. */
/*
* 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.
*/
if (VBOX_SUCCESS(rc))
{
if (VBOX_SUCCESS(rc2))
{
#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 */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
};