NetIf-generic.cpp revision 4a4d0b7611a832602800898d4ec5da6d588b535a
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync/* $Id$ */
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync/** @file
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * VirtualBox Main - Generic NetIf implementation.
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync */
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync/*
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * Copyright (C) 2009-2012 Oracle Corporation
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync *
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * available from http://www.virtualbox.org. This file is free software;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * General Public License (GPL) as published by the Free Software
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync */
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <VBox/err.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <VBox/log.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <iprt/process.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <iprt/env.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <iprt/path.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <iprt/param.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <sys/ioctl.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <netinet/in.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <net/if.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <errno.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include <unistd.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#if defined(RT_OS_SOLARIS)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync# include <sys/sockio.h>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#endif
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync# include <cstdio>
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#endif
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include "HostNetworkInterfaceImpl.h"
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include "ProgressImpl.h"
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include "VirtualBoxImpl.h"
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#include "netif.h"
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync#define VBOXNETADPCTL_NAME "VBoxNetAdpCtl"
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsyncstatic int NetIfAdpCtl(const char * pcszIfName, const char *pszAddr, const char *pszOption, const char *pszMask)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync{
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync const char *args[] = { NULL, pcszIfName, pszAddr, pszOption, pszMask, NULL };
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync char szAdpCtl[RTPATH_MAX];
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (RT_FAILURE(rc))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync LogRel(("NetIfAdpCtl: failed to get program path, rc=%Rrc.\n", rc));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync return rc;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME);
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync args[0] = szAdpCtl;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (!RTPathExists(szAdpCtl))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync LogRel(("NetIfAdpCtl: path %s does not exist. Failed to run " VBOXNETADPCTL_NAME " helper.\n",
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync szAdpCtl));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync return VERR_FILE_NOT_FOUND;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync RTPROCESS pid;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync rc = RTProcCreate(szAdpCtl, args, RTENV_DEFAULT, 0, &pid);
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (RT_SUCCESS(rc))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync RTPROCSTATUS Status;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync rc = RTProcWait(pid, 0, &Status);
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if ( RT_SUCCESS(rc)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync && Status.iStatus == 0
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync && Status.enmReason == RTPROCEXITREASON_NORMAL)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync return VINF_SUCCESS;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync else
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync LogRel(("NetIfAdpCtl: failed to create process for %.\n",
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync szAdpCtl));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync return rc;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync}
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsyncstatic int NetIfAdpCtl(HostNetworkInterface * pIf, const char *pszAddr, const char *pszOption, const char *pszMask)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync{
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync Bstr interfaceName;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync pIf->COMGETTER(Name)(interfaceName.asOutParam());
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync Utf8Str strName(interfaceName);
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync return NetIfAdpCtl(strName.c_str(), pszAddr, pszOption, pszMask);
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync}
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsyncint NetIfAdpCtlOut(const char * pcszName, const char * pcszCmd, char *pszBuffer, size_t cBufSize)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync{
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync char szAdpCtl[RTPATH_MAX];
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " ") - strlen(pcszCmd));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (RT_FAILURE(rc))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync LogRel(("NetIfAdpCtlOut: Failed to get program path, rc=%Rrc\n", rc));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync return VERR_INVALID_PARAMETER;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - 1 - strlen(pcszCmd))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync strcat(szAdpCtl, pcszName);
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync strcat(szAdpCtl, " ");
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync strcat(szAdpCtl, pcszCmd);
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync else
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync LogRel(("NetIfAdpCtlOut: Command line is too long: %s%s %s\n", szAdpCtl, pcszName, pcszCmd));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync return VERR_INVALID_PARAMETER;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync strcat(szAdpCtl, " 2>&1");
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync FILE *fp = popen(szAdpCtl, "r");
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (fp)
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (fgets(pszBuffer, cBufSize, fp))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync if (!strncmp(VBOXNETADPCTL_NAME ":", pszBuffer, sizeof(VBOXNETADPCTL_NAME)))
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync {
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync LogRel(("NetIfAdpCtlOut: %s", pszBuffer));
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync rc = VERR_INTERNAL_ERROR;
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync }
a5e7ae69e440f6816420fc99599f044e79e716b6vboxsync else
{
LogRel(("NetIfAdpCtlOut: No output from " VBOXNETADPCTL_NAME));
rc = VERR_INTERNAL_ERROR;
}
pclose(fp);
}
return rc;
}
int NetIfEnableStaticIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * pIf, ULONG aOldIp, ULONG aNewIp, ULONG aMask)
{
const char *pszOption, *pszMask;
char szAddress[16]; /* 4*3 + 3*1 + 1 */
char szNetMask[16]; /* 4*3 + 3*1 + 1 */
uint8_t *pu8Addr = (uint8_t *)&aNewIp;
uint8_t *pu8Mask = (uint8_t *)&aMask;
if (aNewIp == 0)
{
pu8Addr = (uint8_t *)&aOldIp;
pszOption = "remove";
pszMask = NULL;
}
else
{
pszOption = "netmask";
pszMask = szNetMask;
RTStrPrintf(szNetMask, sizeof(szNetMask), "%d.%d.%d.%d",
pu8Mask[0], pu8Mask[1], pu8Mask[2], pu8Mask[3]);
}
RTStrPrintf(szAddress, sizeof(szAddress), "%d.%d.%d.%d",
pu8Addr[0], pu8Addr[1], pu8Addr[2], pu8Addr[3]);
return NetIfAdpCtl(pIf, szAddress, pszOption, pszMask);
}
int NetIfEnableStaticIpConfigV6(VirtualBox * /* vBox */, HostNetworkInterface * pIf, IN_BSTR aOldIPV6Address, IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
{
char szAddress[5*8 + 1 + 5 + 1];
if (Bstr(aIPV6Address).length())
{
RTStrPrintf(szAddress, sizeof(szAddress), "%ls/%d",
aIPV6Address, aIPV6MaskPrefixLength);
return NetIfAdpCtl(pIf, szAddress, NULL, NULL);
}
else
{
RTStrPrintf(szAddress, sizeof(szAddress), "%ls",
aOldIPV6Address);
return NetIfAdpCtl(pIf, szAddress, "remove", NULL);
}
}
int NetIfEnableDynamicIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * /* pIf */)
{
return VERR_NOT_IMPLEMENTED;
}
int NetIfCreateHostOnlyNetworkInterface(VirtualBox *pVBox,
IHostNetworkInterface **aHostNetworkInterface,
IProgress **aProgress,
const char *pcszName)
{
#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
/* create a progress object */
ComObjPtr<Progress> progress;
progress.createObject();
ComPtr<IHost> host;
HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
if (SUCCEEDED(hrc))
{
hrc = progress->init(pVBox, host,
Bstr("Creating host only network interface").raw(),
FALSE /* aCancelable */);
if (SUCCEEDED(hrc))
{
progress.queryInterfaceTo(aProgress);
char szAdpCtl[RTPATH_MAX];
int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
if (RT_FAILURE(rc))
{
progress->notifyComplete(E_FAIL,
COM_IIDOF(IHostNetworkInterface),
HostNetworkInterface::getStaticComponentName(),
"Failed to get program path, rc=%Rrc\n", rc);
return rc;
}
strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - sizeof(" add"))
{
strcat(szAdpCtl, pcszName);
strcat(szAdpCtl, " add");
}
else
strcat(szAdpCtl, "add");
if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
strcat(szAdpCtl, " 2>&1");
FILE *fp = popen(szAdpCtl, "r");
if (fp)
{
char szBuf[128]; /* We are not interested in long error messages. */
if (fgets(szBuf, sizeof(szBuf), fp))
{
/* Remove trailing new line characters. */
char *pLast = szBuf + strlen(szBuf) - 1;
if (pLast >= szBuf && *pLast == '\n')
*pLast = 0;
if (!strncmp(VBOXNETADPCTL_NAME ":", szBuf, sizeof(VBOXNETADPCTL_NAME)))
{
progress->notifyComplete(E_FAIL,
COM_IIDOF(IHostNetworkInterface),
HostNetworkInterface::getStaticComponentName(),
"%s", szBuf);
pclose(fp);
return E_FAIL;
}
size_t cbNameLen = strlen(szBuf) + 1;
PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
if (!pInfo)
rc = VERR_NO_MEMORY;
else
{
strcpy(pInfo->szShortName, szBuf);
strcpy(pInfo->szName, szBuf);
rc = NetIfGetConfigByName(pInfo);
if (RT_FAILURE(rc))
{
progress->notifyComplete(E_FAIL,
COM_IIDOF(IHostNetworkInterface),
HostNetworkInterface::getStaticComponentName(),
"Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add')\n", szBuf);
}
else
{
Bstr IfName(szBuf);
/* create a new uninitialized host interface object */
ComObjPtr<HostNetworkInterface> iface;
iface.createObject();
iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
iface->i_setVirtualBox(pVBox);
iface.queryInterfaceTo(aHostNetworkInterface);
}
RTMemFree(pInfo);
}
if ((rc = pclose(fp)) != 0)
{
progress->notifyComplete(E_FAIL,
COM_IIDOF(IHostNetworkInterface),
HostNetworkInterface::getStaticComponentName(),
"Failed to execute '" VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
rc = VERR_INTERNAL_ERROR;
}
}
else
{
/* Failed to add an interface */
rc = VERR_PERMISSION_DENIED;
progress->notifyComplete(E_FAIL,
COM_IIDOF(IHostNetworkInterface),
HostNetworkInterface::getStaticComponentName(),
"Failed to execute '" VBOXNETADPCTL_NAME " add' (exit status: %d). Check permissions!", rc);
pclose(fp);
}
}
if (RT_SUCCESS(rc))
progress->notifyComplete(rc);
else
hrc = E_FAIL;
}
}
return hrc;
#else
NOREF(pVBox);
NOREF(aHostNetworkInterface);
NOREF(aProgress);
NOREF(pcszName);
return VERR_NOT_IMPLEMENTED;
#endif
}
int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVBox, IN_GUID aId,
IProgress **aProgress)
{
#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
/* create a progress object */
ComObjPtr<Progress> progress;
progress.createObject();
ComPtr<IHost> host;
int rc = VINF_SUCCESS;
HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
if (SUCCEEDED(hr))
{
Bstr ifname;
ComPtr<IHostNetworkInterface> iface;
if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
return VERR_INVALID_PARAMETER;
iface->COMGETTER(Name)(ifname.asOutParam());
if (ifname.isEmpty())
return VERR_INTERNAL_ERROR;
rc = progress->init(pVBox, host,
Bstr("Removing host network interface").raw(),
FALSE /* aCancelable */);
if (SUCCEEDED(rc))
{
progress.queryInterfaceTo(aProgress);
rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
if (RT_FAILURE(rc))
progress->notifyComplete(E_FAIL,
COM_IIDOF(IHostNetworkInterface),
HostNetworkInterface::getStaticComponentName(),
"Failed to execute '" VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
else
progress->notifyComplete(S_OK);
}
}
else
{
progress->notifyComplete(hr);
rc = VERR_INTERNAL_ERROR;
}
return rc;
#else
NOREF(pVBox);
NOREF(aId);
NOREF(aProgress);
return VERR_NOT_IMPLEMENTED;
#endif
}
int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
{
return VERR_NOT_IMPLEMENTED;
}
int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
{
return VERR_NOT_IMPLEMENTED;
}
/**
* Obtain the current state of the interface.
*
* @returns VBox status code.
*
* @param pcszIfName Interface name.
* @param penmState Where to store the retrieved state.
*/
int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
{
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0)
return VERR_OUT_OF_RESOURCES;
struct ifreq Req;
RT_ZERO(Req);
RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pcszIfName);
if (ioctl(sock, SIOCGIFFLAGS, &Req) < 0)
{
Log(("NetIfGetState: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
*penmState = NETIF_S_UNKNOWN;
}
else
*penmState = (Req.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
close(sock);
return VINF_SUCCESS;
}