slirp_state.h revision ca794f37ff137134ba3aa96994bdb45d54ac8e27
/** @file
* NAT state/configuration.
*/
/*
* 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;
* you can redistribute it and/or modify it under the terms of the GNU
* 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.
*/
#ifndef _slirp_state_h_
#define _slirp_state_h_
#include <iprt/req.h>
#include "ip_icmp.h"
/** Number of DHCP clients supported by NAT. */
#define NB_ADDR 16
/** Where to start DHCP IP number allocation. */
#define START_ADDR 15
/** DHCP Lease time. */
#define LEASE_TIME (24 * 3600)
/** Entry in the table of known DHCP clients. */
typedef struct
{
bool allocated;
uint8_t macaddr[6];
} BOOTPClient;
/** TFTP session entry. */
struct tftp_session
{
int in_use;
unsigned char filename[TFTP_FILENAME_MAX];
struct in_addr client_ip;
u_int16_t client_port;
int timestamp;
};
#ifdef VBOX_WITH_MULTI_DNS
struct dns_entry
{
struct in_addr de_addr;
LIST_ENTRY(dns_entry) de_list;
};
LIST_HEAD(dns_list_head, dns_entry);
#endif
/** Main state/configuration structure for slirp NAT. */
typedef struct NATState
{
/* Stuff from boot.c */
BOOTPClient bootp_clients[NB_ADDR];
const char *bootp_filename;
/* Stuff from if.c */
int if_mtu, if_mru;
int if_comp;
int if_maxlinkhdr;
int if_queued;
int if_thresh;
struct mbuf if_fastq;
struct mbuf if_batchq;
struct mbuf *next_m;
/* Stuff from icmp.c */
struct icmpstat_t icmpstat;
/* Stuff from ip_input.c */
struct ipstat_t ipstat;
struct ipqhead ipq[IPREASS_NHASH];
int maxnipq; /* Administrative limit on # of reass queues*/
int maxfragsperpacket; /* Maximum number of IPv4 fragments allowed per packet */
int nipq; /* total number of reass queues */
uint16_t ip_currid;
/* Stuff from mbuf.c */
int mbuf_alloced, mbuf_max;
int msize;
struct mbuf m_freelist, m_usedlist;
/* Stuff from slirp.c */
void *pvUser;
uint32_t curtime;
uint32_t time_fasttimo;
uint32_t last_slowtimo;
bool do_slowtimo;
bool link_up;
struct timeval tt;
struct in_addr our_addr;
struct in_addr alias_addr;
struct in_addr special_addr;
#ifdef VBOX_WITH_SLIRP_MT
PRTREQQUEUE pReqQueue;
#endif
#ifndef VBOX_WITH_MULTI_DNS
struct in_addr dns_addr;
#else
struct dns_list_head dns_list_head;
#endif
struct in_addr tftp_server;
struct in_addr loopback_addr;
uint32_t netmask;
uint8_t client_ethaddr[6];
struct ex_list *exec_list;
char slirp_hostname[33];
bool fPassDomain;
const char *pszDomain;
/* Stuff from tcp_input.c */
struct socket tcb;
#ifdef VBOX_WITH_SLIRP_MT
RTCRITSECT tcb_mutex;
#endif
struct socket *tcp_last_so;
tcp_seq tcp_iss;
/* Stuff from tcp_timer.c */
struct tcpstat_t tcpstat;
uint32_t tcp_now;
int tcp_reass_qsize;
int tcp_reass_maxqlen;
int tcp_reass_maxseg;
int tcp_reass_overflows;
/* Stuff from tftp.c */
struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
const char *tftp_prefix;
/* Stuff from udp.c */
struct udpstat_t udpstat;
struct socket udb;
#ifdef VBOX_WITH_SLIRP_MT
RTCRITSECT udb_mutex;
#endif
struct socket *udp_last_so;
struct socket icmp_socket;
struct icmp_storage icmp_msg_head;
# ifdef RT_OS_WINDOWS
void *pvIcmpBuffer;
size_t szIcmpBuffer;
/* Accordin MSDN specification IcmpParseReplies
* function should be detected in runtime
*/
long (WINAPI * pfIcmpParseReplies)(void *, long);
BOOL (WINAPI * pfIcmpCloseHandle)(HANDLE);
HMODULE hmIcmpLibrary;
# endif
#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
# define VBOX_SOCKET_EVENT (pData->phEvents[VBOX_SOCKET_EVENT_INDEX])
HANDLE phEvents[VBOX_EVENT_COUNT];
#endif
#if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
int fIcmp;
#endif
STAMPROFILE StatFill;
STAMPROFILE StatPoll;
STAMPROFILE StatFastTimer;
STAMPROFILE StatSlowTimer;
STAMCOUNTER StatTCP;
STAMCOUNTER StatUDP;
STAMCOUNTER StatTCPHot;
STAMCOUNTER StatUDPHot;
} NATState;
/** Default IP time to live. */
#define ip_defttl IPDEFTTL
/** Number of permanent buffers in mbuf. */
#define mbuf_thresh 30
/** Use a fixed time before sending keepalive. */
#define tcp_keepidle TCPTV_KEEP_IDLE
/** Use a fixed interval between keepalive. */
#define tcp_keepintvl TCPTV_KEEPINTVL
/** Maximum idle time before timing out a connection. */
#define tcp_maxidle (TCPTV_KEEPCNT * tcp_keepintvl)
/** Default TCP socket options. */
#define so_options DO_KEEPALIVE
/** Default TCP MSS value. */
#define tcp_mssdflt TCP_MSS
/** Default TCP round trip time. */
#define tcp_rttdflt (TCPTV_SRTTDFLT / PR_SLOWHZ)
/** Enable RFC1323 performance enhancements.
* @todo check if it really works, it was turned off before. */
#define tcp_do_rfc1323 1
/** TCP receive buffer size. */
#define tcp_rcvspace TCP_RCVSPACE
/** TCP receive buffer size. */
#define tcp_sndspace TCP_SNDSPACE
/* TCP duplicate ACK retransmit threshold. */
#define tcprexmtthresh 3
#define bootp_filename pData->bootp_filename
#define bootp_clients pData->bootp_clients
#define if_mtu pData->if_mtu
#define if_mru pData->if_mru
#define if_comp pData->if_comp
#define if_maxlinkhdr pData->if_maxlinkhdr
#define if_queued pData->if_queued
#define if_thresh pData->if_thresh
#define if_fastq pData->if_fastq
#define if_batchq pData->if_batchq
#define next_m pData->next_m
#define icmpstat pData->icmpstat
#define ipstat pData->ipstat
#define ipq pData->ipq
#define ip_currid pData->ip_currid
#define mbuf_alloced pData->mbuf_alloced
#define mbuf_max pData->mbuf_max
#define msize pData->msize
#define m_freelist pData->m_freelist
#define m_usedlist pData->m_usedlist
#define curtime pData->curtime
#define time_fasttimo pData->time_fasttimo
#define last_slowtimo pData->last_slowtimo
#define do_slowtimo pData->do_slowtimo
#define link_up pData->link_up
#define cUsers pData->cUsers
#define tt pData->tt
#define our_addr pData->our_addr
#define alias_addr pData->alias_addr
#define special_addr pData->special_addr
#define dns_addr pData->dns_addr
#define loopback_addr pData->loopback_addr
#define client_ethaddr pData->client_ethaddr
#define exec_list pData->exec_list
#define slirp_hostname pData->slirp_hostname
#define tcb pData->tcb
#define tcp_last_so pData->tcp_last_so
#define tcp_iss pData->tcp_iss
#define tcpstat pData->tcpstat
#define tcp_now pData->tcp_now
#define tftp_sessions pData->tftp_sessions
#define tftp_prefix pData->tftp_prefix
#define udpstat pData->udpstat
#define udb pData->udb
#define udp_last_so pData->udp_last_so
#define maxfragsperpacket pData->maxfragsperpacket
#define maxnipq pData->maxnipq
#define nipq pData->nipq
#define tcp_reass_qsize pData->tcp_reass_qsize
#define tcp_reass_maxqlen pData->tcp_reass_maxqlen
#define tcp_reass_maxseg pData->tcp_reass_maxseg
#define tcp_reass_overflows pData->tcp_reass_overflows
#if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
# define fIcmp pData->fIcmp
#endif
#define queue_tcp_label tcb
#define queue_udp_label udb
#define __X(x) x
#define _X(x) __X(x)
#define _str(x) #x
#define str(x) _str(x)
#ifdef VBOX_WITH_SLIRP_MT
#define QSOCKET_LOCK(queue) \
do { \
int rc; \
rc = RTCritSectEnter(&_X(queue) ## _mutex); \
AssertReleaseRC(rc); \
} while (0)
#define QSOCKET_UNLOCK(queue) \
do { \
int rc; \
rc = RTCritSectLeave(&_X(queue) ## _mutex); \
AssertReleaseRC(rc); \
} while (0)
#define QSOCKET_LOCK_CREATE(queue) \
do { \
int rc; \
rc = RTCritSectInit(&pData->queue ## _mutex); \
AssertReleaseRC(rc); \
} while (0)
#define QSOCKET_LOCK_DESTROY(queue) \
do { \
int rc = RTCritSectDelete(&pData->queue ## _mutex); \
AssertReleaseRC(rc); \
} while (0)
# define QSOCKET_FOREACH(so, sonext, label) \
QSOCKET_LOCK(__X(queue_## label ## _label)); \
(so) = (_X(queue_ ## label ## _label)).so_next; \
if ((so) != &(_X(queue_## label ## _label))) SOCKET_LOCK((so)); \
for(;;) \
{ \
if ((so) == &(_X(queue_## label ## _label))) \
{ \
QSOCKET_UNLOCK(__X(queue_## label ##_label)); \
break; \
} \
Log2(("%s:%d Processing so:%R[natsock]\n", __FUNCTION__, __LINE__, (so))); \
if ((so)->so_next != &(_X(queue_## label ## _label))) \
{ \
SOCKET_LOCK((so)->so_next); \
} \
(sonext) = (so)->so_next; \
QSOCKET_UNLOCK(__X(queue_## label ##_label));
# define CONTINUE_NO_UNLOCK(label) goto loop_end_ ## label ## _mt_nounlock
# define CONTINUE(label) goto loop_end_ ## label ## _mt
/* @todo replace queue parameter with macrodinition */
# define LOOP_LABEL(label, so, sonext) loop_end_ ## label ## _mt: \
SOCKET_UNLOCK(so); \
loop_end_ ## label ## _mt_nounlock: \
QSOCKET_LOCK(_X(queue_ ## label ## _label)); \
(so) = (sonext)
#define DO_TCP_OUTPUT(data, sotcb) \
do { \
PRTREQ pReq = NULL; \
int rc; \
rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
AssertReleaseRC(rc); \
pReq->u.Internal.pfn = (PFNRT)tcp_output; \
pReq->u.Internal.cArgs = 2; \
pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
pReq->u.Internal.aArgs[1] = (uintptr_t)(sotcb); \
pReq->fFlags = RTREQFLAGS_VOID; \
rc = RTReqQueue(pReq, 0); \
if (RT_LIKELY(rc) == VERR_TIMEOUT) \
{ \
SOCKET_UNLOCK(so); \
rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
AssertReleaseRC(rc); \
SOCKET_LOCK(so); \
RTReqFree(pReq); \
} \
else \
AssertReleaseRC(rc); \
} while(0)
#define DO_TCP_INPUT(data, mbuf, size, so) \
do { \
PRTREQ pReq = NULL; \
int rc; \
rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
AssertReleaseRC(rc); \
pReq->u.Internal.pfn = (PFNRT)tcp_input; \
pReq->u.Internal.cArgs = 4; \
pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
pReq->u.Internal.aArgs[1] = (uintptr_t)(mbuf); \
pReq->u.Internal.aArgs[2] = (uintptr_t)(size); \
pReq->u.Internal.aArgs[3] = (uintptr_t)(so); \
pReq->fFlags = RTREQFLAGS_VOID; \
rc = RTReqQueue(pReq, 0); \
if (RT_LIKELY(rc) == VERR_TIMEOUT) \
{ \
SOCKET_UNLOCK(so); \
rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
AssertReleaseRC(rc); \
SOCKET_LOCK(so); \
RTReqFree(pReq); \
} \
else \
AssertReleaseRC(rc); \
} while(0)
#define DO_TCP_CONNECT(data, so) \
do { \
PRTREQ pReq = NULL; \
int rc; \
rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
AssertReleaseRC(rc); \
pReq->u.Internal.pfn = (PFNRT)tcp_connect; \
pReq->u.Internal.cArgs = 2; \
pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
pReq->fFlags = RTREQFLAGS_VOID; \
rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
if (RT_LIKELY(rc) == VERR_TIMEOUT) \
{ \
SOCKET_UNLOCK(so); \
rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
AssertReleaseRC(rc); \
SOCKET_LOCK(so); \
RTReqFree(pReq); \
} \
else \
AssertReleaseRC(rc); \
} while(0)
#define DO_SOREAD(ret, data, so, ifclose) \
do { \
PRTREQ pReq = NULL; \
int rc; \
rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
AssertReleaseRC(rc); \
pReq->u.Internal.pfn = (PFNRT)soread_queue; \
pReq->u.Internal.cArgs = 4; \
pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
pReq->u.Internal.aArgs[2] = (uintptr_t)(ifclose); \
pReq->u.Internal.aArgs[3] = (uintptr_t)&(ret); \
pReq->fFlags = RTREQFLAGS_VOID; \
rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
if (RT_LIKELY(rc) == VERR_TIMEOUT) \
{ \
SOCKET_UNLOCK(so); \
rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
AssertReleaseRC(rc); \
SOCKET_LOCK(so); \
RTReqFree(pReq); \
} \
else \
AssertReleaseRC(rc); \
} while(0)
#define DO_SOWRITE(ret, data, so) \
do { \
PRTREQ pReq = NULL; \
int rc; \
rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
AssertReleaseRC(rc); \
pReq->u.Internal.pfn = (PFNRT)sowrite; \
pReq->u.Internal.cArgs = 2; \
pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
pReq->fFlags = RTREQFLAGS_RETURN_MASK; \
rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
if (RT_LIKELY(rc) == VERR_TIMEOUT) \
{ \
SOCKET_UNLOCK(so); \
rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
SOCKET_LOCK(so); \
ret = pReq->iStatus; \
RTReqFree(pReq); \
} \
else \
AssertReleaseRC(rc); \
} while(0)
#define DO_SORECFROM(data, so) \
do { \
PRTREQ pReq = NULL; \
int rc; \
rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
AssertReleaseRC(rc); \
pReq->u.Internal.pfn = (PFNRT)sorecvfrom; \
pReq->u.Internal.cArgs = 2; \
pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
pReq->fFlags = RTREQFLAGS_VOID; \
rc = RTReqQueue(pReq, 0); \
if (RT_LIKELY(rc) == VERR_TIMEOUT) \
{ \
SOCKET_UNLOCK(so); \
rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
AssertReleaseRC(rc); \
SOCKET_LOCK(so); \
RTReqFree(pReq); \
} \
else \
AssertReleaseRC(rc); \
} while(0)
#define SOLOOKUP(so, label, src, sport, dst, dport) \
do { \
struct socket *sonxt; \
(so) = NULL; \
QSOCKET_FOREACH(so, sonxt, label) \
/* { */ \
if ( so->so_lport == (sport) \
&& so->so_laddr.s_addr == (src).s_addr \
&& so->so_faddr.s_addr == (dst).s_addr \
&& so->so_fport == (dport)) \
{ \
if (sonxt != &__X(queue_ ## label ## _label)) \
SOCKET_UNLOCK(sonxt); \
break; /*so is locked*/ \
} \
LOOP_LABEL(so, sonxt, label); \
} \
} \
}while (0)
#else
#define QSOCKET_LOCK(queue) do {} while (0)
#define QSOCKET_UNLOCK(queue) do {} while (0)
#define QSOCKET_LOCK_CREATE(queue) do {} while (0)
#define QSOCKET_LOCK_DESTROY(queue) do {} while (0)
# define QSOCKET_FOREACH(so, sonext, label) \
for ((so) = __X(queue_ ## label ## _label).so_next; so != &(__X(queue_ ## label ## _label)); (so) = (sonext)) \
{ \
(sonext) = (so)->so_next;
# define CONTINUE(label) continue
# define CONTINUE_NO_UNLOCK(label) continue
# define LOOP_LABEL(label, so, sonext) /* empty*/
#define DO_TCP_OUTPUT(data, sotcb) tcp_output((data), (sotcb))
#define DO_TCP_INPUT(data, mbuf, size, so) tcp_input((data), (mbuf), (size), (so))
#define DO_TCP_CONNECT(data, so) tcp_connect((data), (so))
#define DO_SOREAD(ret, data, so, ifclose) do{(ret) = soread((data), (so), (ifclose));}while(0)
#define DO_SOWRITE(ret, data, so) do{(ret) = sowrite((data), (so));}while(0)
#define DO_SORECFROM(data, so) sorecvfrom((data), (so))
#define SOLOOKUP(so, label, src, sport, dst, dport) \
do { \
(so) = solookup(&__X(queue_ ## label ## _label), (src), (sport), (dst), (dport)); \
} while (0)
#endif
#define TCP_OUTPUT(data, sotcb) DO_TCP_OUTPUT((data), (sotcb))
#define TCP_INPUT(data, mbuf, size, so) DO_TCP_INPUT((data), (mbuf), (size), (so))
#define TCP_CONNECT(data, so) DO_TCP_CONNECT((data), (so))
#define SOREAD(ret, data, so, ifclose) DO_SOREAD((ret), (data), (so), (ifclose))
#define SOWRITE(ret, data, so) DO_SOWRITE((ret), (data), (so))
#define SORECVFROM(data, so) DO_SORECFROM((data), (so))
#endif /* !_slirp_state_h_ */