slirp.c revision 54fe561ef34bc91e6d39efe592aa5fc2d31a0cbd
#include "slirp.h"
#ifdef RT_OS_OS2
# include <paths.h>
#endif
#ifdef VBOX_WITH_SLIRP_DNS_PROXY
#include "dnsproxy/dnsproxy.h"
#endif
#ifndef RT_OS_WINDOWS
# include <poll.h>
#else
# include <Winnls.h>
# define _WINSOCK2API_
# include <IPHlpApi.h>
#endif
#ifdef VBOX_WITH_SLIRP_ALIAS
# include<alias.h>
#endif
#if !defined(RT_OS_WINDOWS)
do { \
break; /* out of this loop */ \
} \
poll_index++; \
} while(0)
do { \
break; /* out of this loop */ \
} \
poll_index++; \
} while(0)
# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
# ifndef RT_OS_WINDOWS
# ifndef RT_OS_LINUX
# define readfds_poll (POLLRDNORM)
# define writefds_poll (POLLWRNORM)
# else
# define readfds_poll (POLLIN)
# define writefds_poll (POLLOUT)
# endif
# define rderr_poll (POLLERR)
# define rdhup_poll (POLLHUP)
do { \
} while (0)
# else /* !RT_OS_WINDOWS */
#endif /* RT_OS_WINDOWS */
#else /* defined(RT_OS_WINDOWS) */
/*
* On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
* So no call to WSAEventSelect necessary.
*/
do { \
if (rc == SOCKET_ERROR) \
{ \
/* This should not happen */ \
error = WSAGetLastError(); \
} \
} while(0); \
if ((rc) == SOCKET_ERROR) \
{ \
(error) = WSAGetLastError(); \
}
# define acceptds_win FD_ACCEPT
# define acceptds_win_bit FD_ACCEPT_BIT
# define readfds_win FD_READ
# define readfds_win_bit FD_READ_BIT
# define writefds_win FD_WRITE
# define writefds_win_bit FD_WRITE_BIT
# define xfds_win_bit FD_OOB_BIT
#endif /* defined(RT_OS_WINDOWS) */
/*
* Loging macros
*/
# if defined(RT_OS_WINDOWS)
do { \
} while (0)
# else /* RT_OS_WINDOWS */
do { \
} while (0)
# endif /* !RT_OS_WINDOWS */
#else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
#endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
#define LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) DO_LOG_NAT_SOCK((so), proto, (winevent), r_fdset, w_fdset, x_fdset)
{
0x52, 0x54, 0x00, 0x12, 0x35, 0x00
};
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0
};
#ifdef RT_OS_WINDOWS
# ifndef VBOX_WITH_MULTI_DNS
const char **ppszDomain)
{
int rc = 0;
BufLen = sizeof(FIXED_INFO);
/** @todo: this API returns all DNS servers, no matter whether the
* corresponding network adapter is disabled or not. Maybe replace
{
if (FixedInfo)
{
}
}
{
if (FixedInfo)
{
}
rc = -1;
goto get_dns_prefix;
}
Log(("nat: DNS Servers:\n"));
while (pIPAddr)
{
if (fVerbose)
}
if (FixedInfo)
{
}
if (ppszDomain)
{
char szDnsDomain[256];
*ppszDomain = NULL;
GetVersionEx(&ver);
{
/* GetComputerNameEx exists in Windows versions starting with 2000. */
{
if (szDnsDomain[0])
{
/* Just non-empty strings are valid. */
if (pData->fPassDomain)
{
if (fVerbose)
}
else
}
}
else
}
}
return rc;
}
# else /* !VBOX_WITH_MULTI_DNS */
const char **ppszDomain)
{
/* Get amount of memory required for operation */
ULONG flags = GAA_FLAG_INCLUDE_PREFIX; /*GAA_FLAG_INCLUDE_ALL_INTERFACES;*/ /* all interfaces registered in NDIS */
int wlen = 0;
char *suffix;
/* @todo add SKIPing flags to get only required information */
if (ret != ERROR_BUFFER_OVERFLOW)
{
return -1;
}
if (size == 0)
{
LogRel(("NAT: Win socket API returns non capacity\n"));
return -1;
}
{
LogRel(("NAT: No memory available \n"));
return -1;
}
if (ret != ERROR_SUCCESS)
{
return -1;
}
{
int found;
goto next;
{
goto next_dns;
/* add dns server to list */
{
LogRel(("NAT: Can't allocate buffer for DNS entry\n"));
return VERR_NO_MEMORY;
}
if ((((struct sockaddr_in *)saddr)->sin_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
}
else
{
}
goto next_dns;
/*uniq*/
goto next_dns;
}
found = 0;
{
{
found = 1;
break;
}
}
if (found == 0)
{
{
LogRel(("NAT: not enough memory\n"));
return VERR_NO_MEMORY;
}
}
}
next:
}
return 0;
}
# endif /* VBOX_WITH_MULTI_DNS */
#else /* !RT_OS_WINDOWS */
const char **ppszDomain)
{
char buff[512];
char buff2[256];
int found = 0;
#ifdef RT_OS_OS2
/* Try various locations. */
if (etc)
{
}
if (!f)
{
}
if (!f)
{
}
#else
#ifndef DEBUG_vvl
#else
if (f != NULL)
{
}
else
{
}
#endif
#endif
if (!f)
return -1;
if (ppszDomain)
*ppszDomain = NULL;
Log(("nat: DNS Servers:\n"));
{
#ifdef VBOX_WITH_MULTI_DNS
#endif
{
continue;
#ifndef VBOX_WITH_MULTI_DNS
/* If it's the first one, set it to dns_addr */
if (!found)
{
}
else
{
if (fVerbose)
}
#else
/*localhost mask */
{
LogRel(("can't alloc memory for DNS entry\n"));
return -1;
}
/*check */
}
#endif
found++;
}
#ifndef VBOX_WITH_MULTI_DNS
if ( ppszDomain
{
if (*ppszDomain == NULL)
{
char *tok;
char *saveptr;
if (tok)
{
if (pData->fPassDomain)
{
if (fVerbose)
}
else
}
}
}
#else
{
char *tok;
char *saveptr;
int found = 0;
{
{
found = 1;
break;
}
}
{
LogRel(("NAT: not enought memory to add domain list\n"));
return VERR_NO_MEMORY;
}
}
}
#endif
}
fclose(f);
if (!found)
return -1;
return 0;
}
#endif
#ifdef VBOX_WITH_MULTI_DNS
{
}
{
}
}
}
#endif
{
}
#ifndef VBOX_WITH_NAT_SERVICE
bool fPassDomain, void *pvUser)
#else
bool fPassDomain, void *pvUser)
#endif
{
int fNATfailed = 0;
int rc;
if (!pData)
return VERR_NO_MEMORY;
if (u32Netmask & 0x1f)
/* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
return VERR_INVALID_PARAMETER;
#ifdef RT_OS_WINDOWS
{
}
#endif
#ifdef VBOX_WITH_SLIRP_MT
#endif
link_up = 1;
debug_init();
/* Initialise mbufs *after* setting the MTU */
#ifndef VBOX_WITH_NAT_SERVICE
#else
#endif
#endif
/* @todo: add ability to configure this staff */
/* set default addresses */
#ifndef VBOX_WITH_MULTI_DNS
#else
if (slirp_init_dns_list(pData) < 0)
#endif
fNATfailed = 1;
#ifdef VBOX_WITH_SLIRP_DNS_PROXY
#endif
#ifdef VBOX_WITH_SLIRP_ALIAS
{
{
LogRel(("NAT: LibAlias default rule wasn't initialized\n"));
AssertMsgFailed(("NAT: LibAlias default rule wasn't initialized\n"));
}
}
#endif
}
/**
* Statistics counters.
*/
{
#ifdef VBOX_WITH_STATISTICS
STAMUNIT_TICKS_PER_CALL, "Profiling slirp fast timer", "/Drivers/NAT%d/TimerFast", pDrvIns->iInstance);
STAMUNIT_TICKS_PER_CALL, "Profiling slirp slow timer", "/Drivers/NAT%d/TimerSlow", pDrvIns->iInstance);
#endif /* VBOX_WITH_STATISTICS */
}
/**
* Marks the link as up, making it possible to establish new connections.
*/
{
link_up = 1;
}
/**
* Marks the link as down and cleans up the current connections.
*/
{
{
else
}
link_up = 0;
}
/**
* Terminates the slirp component.
*/
{
#ifndef VBOX_WITH_MULTI_DNS
#endif
#ifdef RT_OS_WINDOWS
# else
#endif
#ifdef VBOX_WITH_MULTI_DNS
#endif
#ifdef RT_OS_WINDOWS
WSACleanup();
#endif
#ifdef LOG_ENABLED
Log(("\n"
"NAT statistics\n"
"--------------\n"
"\n"));
Log(("\n"
"\n"
"\n"));
#endif
}
#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
/*
* curtime kept to an accuracy of 1ms
*/
{
#ifdef RT_OS_WINDOWS
#else
gettimeofday(&tt, 0);
curtime++;
#endif
}
#ifdef RT_OS_WINDOWS
#else /* RT_OS_WINDOWS */
#endif /* !RT_OS_WINDOWS */
{
int nfds;
#if defined(RT_OS_WINDOWS)
int rc;
int error;
#else
int poll_index = 0;
#endif
int i;
/*
* First, TCP sockets
*/
do_slowtimo = 0;
if (link_up)
{
/*
* *_slowtimo needs calling if there are IP fragments
* in the fragment queue, or there are TCP connections active
*/
/* XXX:
* triggering of fragment expiration should be the same but use new macroses
*/
if (!do_slowtimo)
{
for (i = 0; i < IPREASS_NHASH; i++)
{
if (!TAILQ_EMPTY(&ipq[i]))
{
do_slowtimo = 1;
break;
}
}
}
/* { */
#if !defined(RT_OS_WINDOWS)
#endif
/*
* See if we need a tcp_fasttimo
*/
if ( time_fasttimo == 0
/*
* NOFDREF can include still connecting to local-host,
* newly socreated() sockets etc. Don't want to select these.
*/
/*
* Set for reading sockets which are accepting
*/
{
}
/*
* Set for writing sockets which are connecting
*/
{
}
/*
* Set for writing if we are connected, can send more, and
* we have something to send
*/
{
}
/*
* Set for reading (and urgent data) if we are connected, can
* receive more, and we have room for it XXX /2 ?
*/
{
}
}
/*
* UDP sockets
*/
/* { */
#if !defined(RT_OS_WINDOWS)
#endif
/*
* See if it's timed out
*/
{
{
#ifdef VBOX_WITH_SLIRP_DNS_PROXY
{
}
#endif
#ifdef VBOX_WITH_SLIRP_MT
/* we need so_next for continue our cycle*/
#endif
}
else
}
/*
* When UDP packets are received from over the link, they're
* sendto()'d straight away, so no need for setting for writing
* Limit the number of packets queued by this session to 4.
* Note that even though we try and limit this to 4 packets,
* the session could have more queued if the packets needed
* to be fragmented.
*
* (XXX <= 4 ?)
*/
{
}
}
}
#if defined(RT_OS_WINDOWS)
#else /* RT_OS_WINDOWS */
*pnfds = poll_index;
#endif /* !RT_OS_WINDOWS */
}
#if defined(RT_OS_WINDOWS)
#else /* RT_OS_WINDOWS */
#endif /* !RT_OS_WINDOWS */
{
int ret;
#if defined(RT_OS_WINDOWS)
int rc;
int error;
#else
int poll_index = 0;
#endif
/* Update time */
/*
* See if anything has timed out
*/
if (link_up)
{
{
time_fasttimo = 0;
}
{
}
}
#if defined(RT_OS_WINDOWS)
if (fTimeout)
return; /* only timer update */
#endif
/*
* Check sockets
*/
if (link_up)
{
#if defined(RT_OS_WINDOWS)
/*XXX: before renaming please make see define
* fIcmp in slirp_state.h
*/
if (fIcmp)
#else
#endif
/*
* Check TCP sockets
*/
/* { */
#ifdef VBOX_WITH_SLIRP_MT
{
{
}
{
}
NSOCK_DEC();
}
#endif
/*
* FD_ISSET is meaningless on these sockets
* (and they can crash the program)
*/
/*
* Check for URG data
* This will soread as well, so no need to
* test for readfds below if this succeeds
*/
/* out-of-band data */
{
}
/*
* Check sockets for reading
*/
{
/*
* Check for incoming connections
*/
{
#if defined(RT_OS_WINDOWS)
#endif
}
/* Output it if we read something */
if (ret > 0)
}
#if defined(RT_OS_WINDOWS)
/*
* Check for FD_CLOSE events.
* in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
*/
{
/*
* drain the socket
*/
for (;;)
{
if (ret > 0)
else
break;
}
}
#endif
/*
* Check sockets for writing
*/
{
/*
* Check for non-blocking, still-connecting sockets
*/
{
/* Connected */
/*
* This should be probably guarded by PROBE_CONN too. Anyway,
* we disable it on OS/2 because the below send call returns
* EFAULT which causes the opened TCP socket to close right
* after it has been opened and connected.
*/
#ifndef RT_OS_OS2
if (ret < 0)
{
/* XXXXX Must fix, zero bytes is a NOP */
|| errno == EWOULDBLOCK
|| errno == EINPROGRESS
/* else failed */
}
/* else so->so_state &= ~SS_ISFCONNECTING; */
#endif
/*
* Continue tcp_input
*/
/* continue; */
}
else
/*
* XXX If we wrote something (a lot), there could be the need
* for a window update. In the worst case, the remote will send
* a window probe to get things going again.
*/
}
/*
* Probe a still-connecting, non-blocking socket
* to check if it's still alive
*/
#ifdef PROBE_CONN
{
if (ret < 0)
{
/* XXX */
|| errno == EWOULDBLOCK
|| errno == EINPROGRESS
{
}
/* else failed */
/* tcp_input will take care of it */
}
else
{
if (ret < 0)
{
/* XXX */
|| errno == EWOULDBLOCK
|| errno == EINPROGRESS
{
}
/* else failed */
}
else
}
} /* SS_ISFCONNECTING */
#endif
#ifndef RT_OS_WINDOWS
{
int err;
int status;
if (status != 0)
#ifndef RT_OS_SOLARIS
{
/* EINVAL returned if socket in listen state tcp(7)*/
}
if (status != 0)
#else
/*
* Solaris has bit different ioctl commands and its handlings
* hint: streamio(7) I_NREAD
*/
#endif
{
/**
* Check if we need here take care about gracefull connection
* @todo try with proxy server
*/
{
/*
* Never meet inq != 0 or outq != 0, anyway let it stay for a while
* in case it happens we'll able to detect it.
*/
Log(("NAT: %R[natsock] err(%d:%s) s(in:%d,out:%d)happens on read I/O, "
}
goto tcp_input_close;
}
{
Log(("NAT: system expires the socket %R[natsock] err(%d:%s) s(in:%d,out:%d) happens on non-I/O. ",
goto tcp_input_close;
}
Log(("NAT: %R[natsock] we've met(%d:%s) s(in:%d, out:%d) unhandled combination hup (%d) "
"rederr(%d) on (r:%d, w:%d, x:%d)\n",
/*
*/
so->so_state = SS_NOFDREF; /*cause connection valid tcp connection termination and socket closing */
}
#endif
}
/*
* Now UDP sockets.
* Incoming packets are sent straight away, they're not buffered.
* Incoming UDP data isn't buffered either.
*/
/* { */
#ifdef VBOX_WITH_SLIRP_MT
{
{
}
{
}
NSOCK_DEC();
}
#endif
{
}
}
}
#ifndef VBOX_WITH_SLIRP_MT
/*
* See if we can start outputting
*/
#endif
}
#ifndef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
#define ETH_ALEN 6
#define ETH_HLEN 14
struct ethhdr
{
unsigned short h_proto; /* packet type ID field */
};
#endif
struct arphdr
{
unsigned short ar_hrd; /* format of hardware address */
unsigned short ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
unsigned short ar_op; /* ARP opcode (command) */
/*
* Ethernet looks like this : This bit is variable sized however...
*/
};
{
int ar_op;
Log4(("NAT: arp:%R[ether]->%R[ether]\n",
#endif
switch(ar_op)
{
case ARPOP_REQUEST:
#ifdef VBOX_WITH_NAT_SERVICE
#endif
{
goto arp_ok;
{
{
goto arp_ok;
}
}
return;
{
case CTL_DNS:
case CTL_ALIAS:
break;
default:;
}
}
break;
default:
break;
}
}
{
struct mbuf *m;
int proto;
static bool fWarnedIpv6;
{
return;
}
{
/* @todo vasily: add ether logging routine in debug.c */
Log(("NAT: packet was addressed to other MAC\n"));
return;
}
#endif
if (!m)
{
LogRel(("NAT: can't allocate new mbuf\n"));
return;
}
/* Note: we add to align the IP header */
if (M_FREEROOM(m) < pkt_len)
switch(proto)
{
case ETH_P_ARP:
break;
case ETH_P_IP:
/* Update time. Important if the network is very quiet, as otherwise
* the first outgoing connection gets an incorrect timestamp. */
break;
case ETH_P_IPV6:
if (!fWarnedIpv6)
{
LogRel(("NAT: IPv6 not supported\n"));
fWarnedIpv6 = true;
}
break;
default:
break;
}
}
/* output the IP packet to the ethernet device */
{
m->m_data -= if_maxlinkhdr;
{
LogRel(("NAT: ethernet detects corruption of the packet"));
AssertMsgFailed(("!!Ethernet frame corrupted!!"));
}
#ifndef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
/* XXX: not correct */
#else
{
}
#endif
#if 0
#else
#endif
}
{
if (is_udp)
{
htons(guest_port), 0);
}
else
{
htons(guest_port), 0);
}
}
int guest_port)
{
}
{
#ifndef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
#endif
}
#if defined(RT_OS_WINDOWS)
{
}
{
}
#endif
{
if (link_up)
{
if (time_fasttimo)
return 2;
if (do_slowtimo)
return 500; /* see PR_SLOWHZ */
}
return 0;
}
#ifndef RT_OS_WINDOWS
{
}
#endif
/*
* this function called from NAT thread
*/
{
}
#ifdef VBOX_WITH_SLIRP_MT
{
}
{
}
#endif
{
service = 0;
#if 1
/* Always return 0 here */
switch (hdport)
{
case 500:
/* Not sure why this make Cisco VPN client's connection more stable,
* at least on some servers
*/
break;
}
#endif
}
{
}
{
}
{
if (next_server == NULL)
else
}
#ifdef VBOX_WITH_SLIRP_DNS_PROXY
{
}
#endif