1 | /** @file
|
---|
2 | * NAT state/configuration.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
17 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
18 | * additional information or have any questions.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef _slirp_state_h_
|
---|
22 | #define _slirp_state_h_
|
---|
23 | #include "ip_icmp.h"
|
---|
24 |
|
---|
25 | /** Number of DHCP clients supported by NAT. */
|
---|
26 | #define NB_ADDR 16
|
---|
27 |
|
---|
28 | /** Where to start DHCP IP number allocation. */
|
---|
29 | #define START_ADDR 15
|
---|
30 |
|
---|
31 | /** DHCP Lease time. */
|
---|
32 | #define LEASE_TIME (24 * 3600)
|
---|
33 |
|
---|
34 | /** Entry in the table of known DHCP clients. */
|
---|
35 | typedef struct
|
---|
36 | {
|
---|
37 | bool allocated;
|
---|
38 | uint8_t macaddr[6];
|
---|
39 | } BOOTPClient;
|
---|
40 |
|
---|
41 |
|
---|
42 | /** TFTP session entry. */
|
---|
43 | struct tftp_session
|
---|
44 | {
|
---|
45 | int in_use;
|
---|
46 | unsigned char filename[TFTP_FILENAME_MAX];
|
---|
47 |
|
---|
48 | struct in_addr client_ip;
|
---|
49 | u_int16_t client_port;
|
---|
50 |
|
---|
51 | int timestamp;
|
---|
52 | };
|
---|
53 |
|
---|
54 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
55 | struct dns_entry
|
---|
56 | {
|
---|
57 | struct in_addr de_addr;
|
---|
58 | LIST_ENTRY(dns_entry) de_list;
|
---|
59 | };
|
---|
60 | LIST_HEAD(dns_list_head, dns_entry);
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | /** Main state/configuration structure for slirp NAT. */
|
---|
64 | typedef struct NATState
|
---|
65 | {
|
---|
66 | /* Stuff from boot.c */
|
---|
67 | BOOTPClient bootp_clients[NB_ADDR];
|
---|
68 | const char *bootp_filename;
|
---|
69 | /* Stuff from if.c */
|
---|
70 | int if_mtu, if_mru;
|
---|
71 | int if_comp;
|
---|
72 | int if_maxlinkhdr;
|
---|
73 | int if_queued;
|
---|
74 | int if_thresh;
|
---|
75 | struct mbuf if_fastq;
|
---|
76 | struct mbuf if_batchq;
|
---|
77 | struct mbuf *next_m;
|
---|
78 | /* Stuff from icmp.c */
|
---|
79 | struct icmpstat_t icmpstat;
|
---|
80 | /* Stuff from ip_input.c */
|
---|
81 | struct ipstat_t ipstat;
|
---|
82 | struct ipqhead ipq[IPREASS_NHASH];
|
---|
83 | int maxnipq; /* Administrative limit on # of reass queues*/
|
---|
84 | int maxfragsperpacket; /* Maximum number of IPv4 fragments allowed per packet */
|
---|
85 | int nipq; /* total number of reass queues */
|
---|
86 | uint16_t ip_currid;
|
---|
87 | /* Stuff from mbuf.c */
|
---|
88 | int mbuf_alloced, mbuf_max;
|
---|
89 | int msize;
|
---|
90 | struct mbuf m_freelist, m_usedlist;
|
---|
91 | /* Stuff from slirp.c */
|
---|
92 | void *pvUser;
|
---|
93 | uint32_t curtime;
|
---|
94 | uint32_t time_fasttimo;
|
---|
95 | uint32_t last_slowtimo;
|
---|
96 | bool do_slowtimo;
|
---|
97 | bool link_up;
|
---|
98 | struct timeval tt;
|
---|
99 | struct in_addr our_addr;
|
---|
100 | struct in_addr alias_addr;
|
---|
101 | struct in_addr special_addr;
|
---|
102 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
103 | struct in_addr dns_addr;
|
---|
104 | #else
|
---|
105 | struct dns_list_head dns_list_head;
|
---|
106 | #endif
|
---|
107 | struct in_addr tftp_server;
|
---|
108 | struct in_addr loopback_addr;
|
---|
109 | uint32_t netmask;
|
---|
110 | uint8_t client_ethaddr[6];
|
---|
111 | struct ex_list *exec_list;
|
---|
112 | char slirp_hostname[33];
|
---|
113 | bool fPassDomain;
|
---|
114 | const char *pszDomain;
|
---|
115 | /* Stuff from tcp_input.c */
|
---|
116 | struct socket tcb;
|
---|
117 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
118 | RTSEMMUTEX tcb_mutex;
|
---|
119 | #endif
|
---|
120 | struct socket *tcp_last_so;
|
---|
121 | tcp_seq tcp_iss;
|
---|
122 | /* Stuff from tcp_timer.c */
|
---|
123 | struct tcpstat_t tcpstat;
|
---|
124 | uint32_t tcp_now;
|
---|
125 | int tcp_reass_qsize;
|
---|
126 | int tcp_reass_maxqlen;
|
---|
127 | int tcp_reass_maxseg;
|
---|
128 | int tcp_reass_overflows;
|
---|
129 | /* Stuff from tftp.c */
|
---|
130 | struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
|
---|
131 | const char *tftp_prefix;
|
---|
132 | /* Stuff from udp.c */
|
---|
133 | struct udpstat_t udpstat;
|
---|
134 | struct socket udb;
|
---|
135 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
136 | RTSEMMUTEX udb_mutex;
|
---|
137 | #endif
|
---|
138 | struct socket *udp_last_so;
|
---|
139 | struct socket icmp_socket;
|
---|
140 | struct icmp_storage icmp_msg_head;
|
---|
141 | # ifdef RT_OS_WINDOWS
|
---|
142 | void *pvIcmpBuffer;
|
---|
143 | size_t szIcmpBuffer;
|
---|
144 | /* Accordin MSDN specification IcmpParseReplies
|
---|
145 | * function should be detected in runtime
|
---|
146 | */
|
---|
147 | long (WINAPI * pfIcmpParseReplies)(void *, long);
|
---|
148 | BOOL (WINAPI * pfIcmpCloseHandle)(HANDLE);
|
---|
149 | HMODULE hmIcmpLibrary;
|
---|
150 | # endif
|
---|
151 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
152 | # define VBOX_SOCKET_EVENT (pData->phEvents[VBOX_SOCKET_EVENT_INDEX])
|
---|
153 | HANDLE phEvents[VBOX_EVENT_COUNT];
|
---|
154 | #endif
|
---|
155 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
156 | int fIcmp;
|
---|
157 | #endif
|
---|
158 | STAMPROFILE StatFill;
|
---|
159 | STAMPROFILE StatPoll;
|
---|
160 | STAMPROFILE StatFastTimer;
|
---|
161 | STAMPROFILE StatSlowTimer;
|
---|
162 | STAMCOUNTER StatTCP;
|
---|
163 | STAMCOUNTER StatUDP;
|
---|
164 | STAMCOUNTER StatTCPHot;
|
---|
165 | STAMCOUNTER StatUDPHot;
|
---|
166 | } NATState;
|
---|
167 |
|
---|
168 |
|
---|
169 | /** Default IP time to live. */
|
---|
170 | #define ip_defttl IPDEFTTL
|
---|
171 |
|
---|
172 | /** Number of permanent buffers in mbuf. */
|
---|
173 | #define mbuf_thresh 30
|
---|
174 |
|
---|
175 | /** Use a fixed time before sending keepalive. */
|
---|
176 | #define tcp_keepidle TCPTV_KEEP_IDLE
|
---|
177 |
|
---|
178 | /** Use a fixed interval between keepalive. */
|
---|
179 | #define tcp_keepintvl TCPTV_KEEPINTVL
|
---|
180 |
|
---|
181 | /** Maximum idle time before timing out a connection. */
|
---|
182 | #define tcp_maxidle (TCPTV_KEEPCNT * tcp_keepintvl)
|
---|
183 |
|
---|
184 | /** Default TCP socket options. */
|
---|
185 | #define so_options DO_KEEPALIVE
|
---|
186 |
|
---|
187 | /** Default TCP MSS value. */
|
---|
188 | #define tcp_mssdflt TCP_MSS
|
---|
189 |
|
---|
190 | /** Default TCP round trip time. */
|
---|
191 | #define tcp_rttdflt (TCPTV_SRTTDFLT / PR_SLOWHZ)
|
---|
192 |
|
---|
193 | /** Enable RFC1323 performance enhancements.
|
---|
194 | * @todo check if it really works, it was turned off before. */
|
---|
195 | #define tcp_do_rfc1323 1
|
---|
196 |
|
---|
197 | /** TCP receive buffer size. */
|
---|
198 | #define tcp_rcvspace TCP_RCVSPACE
|
---|
199 |
|
---|
200 | /** TCP receive buffer size. */
|
---|
201 | #define tcp_sndspace TCP_SNDSPACE
|
---|
202 |
|
---|
203 | /* TCP duplicate ACK retransmit threshold. */
|
---|
204 | #define tcprexmtthresh 3
|
---|
205 |
|
---|
206 |
|
---|
207 | #define bootp_filename pData->bootp_filename
|
---|
208 | #define bootp_clients pData->bootp_clients
|
---|
209 |
|
---|
210 | #define if_mtu pData->if_mtu
|
---|
211 | #define if_mru pData->if_mru
|
---|
212 | #define if_comp pData->if_comp
|
---|
213 | #define if_maxlinkhdr pData->if_maxlinkhdr
|
---|
214 | #define if_queued pData->if_queued
|
---|
215 | #define if_thresh pData->if_thresh
|
---|
216 | #define if_fastq pData->if_fastq
|
---|
217 | #define if_batchq pData->if_batchq
|
---|
218 | #define next_m pData->next_m
|
---|
219 |
|
---|
220 | #define icmpstat pData->icmpstat
|
---|
221 |
|
---|
222 | #define ipstat pData->ipstat
|
---|
223 | #define ipq pData->ipq
|
---|
224 | #define ip_currid pData->ip_currid
|
---|
225 |
|
---|
226 | #define mbuf_alloced pData->mbuf_alloced
|
---|
227 | #define mbuf_max pData->mbuf_max
|
---|
228 | #define msize pData->msize
|
---|
229 | #define m_freelist pData->m_freelist
|
---|
230 | #define m_usedlist pData->m_usedlist
|
---|
231 |
|
---|
232 | #define curtime pData->curtime
|
---|
233 | #define time_fasttimo pData->time_fasttimo
|
---|
234 | #define last_slowtimo pData->last_slowtimo
|
---|
235 | #define do_slowtimo pData->do_slowtimo
|
---|
236 | #define link_up pData->link_up
|
---|
237 | #define cUsers pData->cUsers
|
---|
238 | #define tt pData->tt
|
---|
239 | #define our_addr pData->our_addr
|
---|
240 | #define alias_addr pData->alias_addr
|
---|
241 | #define special_addr pData->special_addr
|
---|
242 | #define dns_addr pData->dns_addr
|
---|
243 | #define loopback_addr pData->loopback_addr
|
---|
244 | #define client_ethaddr pData->client_ethaddr
|
---|
245 | #define exec_list pData->exec_list
|
---|
246 | #define slirp_hostname pData->slirp_hostname
|
---|
247 |
|
---|
248 | #define tcb pData->tcb
|
---|
249 | #define tcp_last_so pData->tcp_last_so
|
---|
250 | #define tcp_iss pData->tcp_iss
|
---|
251 |
|
---|
252 | #define tcpstat pData->tcpstat
|
---|
253 | #define tcp_now pData->tcp_now
|
---|
254 |
|
---|
255 | #define tftp_sessions pData->tftp_sessions
|
---|
256 | #define tftp_prefix pData->tftp_prefix
|
---|
257 |
|
---|
258 | #define udpstat pData->udpstat
|
---|
259 | #define udb pData->udb
|
---|
260 | #define udp_last_so pData->udp_last_so
|
---|
261 |
|
---|
262 | #define maxfragsperpacket pData->maxfragsperpacket
|
---|
263 | #define maxnipq pData->maxnipq
|
---|
264 | #define nipq pData->nipq
|
---|
265 |
|
---|
266 | #define tcp_reass_qsize pData->tcp_reass_qsize
|
---|
267 | #define tcp_reass_maxqlen pData->tcp_reass_maxqlen
|
---|
268 | #define tcp_reass_maxseg pData->tcp_reass_maxseg
|
---|
269 | #define tcp_reass_overflows pData->tcp_reass_overflows
|
---|
270 |
|
---|
271 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
272 | # define fIcmp pData->fIcmp
|
---|
273 | #endif
|
---|
274 |
|
---|
275 | #define queue_tcp_label tcb
|
---|
276 | #define queue_udp_label udb
|
---|
277 | #define __X(x) x
|
---|
278 | #define _X(x) __X(x)
|
---|
279 |
|
---|
280 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
281 | #define QSOCKET_LOCK(queue) \
|
---|
282 | do { \
|
---|
283 | int rc = RTSemMutexRequest(_X(queue) ## _mutex, RT_INDEFINITE_WAIT); \
|
---|
284 | AssertReleaseRC(rc); \
|
---|
285 | } while (0)
|
---|
286 | #define QSOCKET_UNLOCK(queue) \
|
---|
287 | do { \
|
---|
288 | int rc = RTSemMutexRelease(_X(queue) ## _mutex); \
|
---|
289 | AssertReleaseRC(rc); \
|
---|
290 | } while (0)
|
---|
291 | #define QSOCKET_LOCK_CREATE(queue) \
|
---|
292 | do { \
|
---|
293 | int rc = RTSemMutexCreate(&pData->queue ## _mutex); \
|
---|
294 | AssertReleaseRC(rc); \
|
---|
295 | } while (0)
|
---|
296 | #define QSOCKET_LOCK_DESTROY(queue) \
|
---|
297 | do { \
|
---|
298 | int rc = RTSemMutexDestroy(pData->queue ## _mutex); \
|
---|
299 | AssertReleaseRC(rc); \
|
---|
300 | } while (0)
|
---|
301 |
|
---|
302 | # define QSOCKET_FOREACH(so, sonext, label) \
|
---|
303 | QSOCKET_LOCK(__X(queue_## label ## _label)); \
|
---|
304 | (so) = (_X(queue_ ## label ## _label)).so_next; \
|
---|
305 | SOCKET_LOCK((so)); \
|
---|
306 | for(;;) \
|
---|
307 | { \
|
---|
308 | if ((so) == &(_X(queue_## label ## _label))) \
|
---|
309 | { \
|
---|
310 | QSOCKET_UNLOCK(__X(queue_## label ##_label)); \
|
---|
311 | break; \
|
---|
312 | } \
|
---|
313 | if ((so)->so_next != &(_X(queue_## label ## _label))) \
|
---|
314 | { \
|
---|
315 | SOCKET_LOCK((so)->so_next); \
|
---|
316 | } \
|
---|
317 | (sonext) = (so)->so_next; \
|
---|
318 | QSOCKET_UNLOCK(__X(queue_## label ##_label));
|
---|
319 | #else
|
---|
320 | #define QSOCKET_LOCK(queue) do {} while (0)
|
---|
321 | #define QSOCKET_UNLOCK(queue) do {} while (0)
|
---|
322 | #define QSOCKET_LOCK_CREATE(queue) do {} while (0)
|
---|
323 | #define QSOCKET_LOCK_DESTROY(queue) do {} while (0)
|
---|
324 | # define QSOCKET_FOREACH(so, sonext, label) \
|
---|
325 | for ((so) = __X(queue_ ## label ## _label).so_next; so != &(__X(queue_ ## label ## _label)); (so) = (sonext)) \
|
---|
326 | { \
|
---|
327 | (sonext) = (so)->so_next;
|
---|
328 | #endif
|
---|
329 |
|
---|
330 | #endif /* !_slirp_state_h_ */
|
---|