1 | /* $Id: slirp.h 28807 2010-04-27 10:43:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - slirp (declarations/defines).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef __COMMON_H__
|
---|
19 | #define __COMMON_H__
|
---|
20 |
|
---|
21 | #include <VBox/stam.h>
|
---|
22 |
|
---|
23 | #ifdef RT_OS_WINDOWS
|
---|
24 | # include <winsock2.h>
|
---|
25 | # include <ws2tcpip.h>
|
---|
26 | typedef int socklen_t;
|
---|
27 | #endif
|
---|
28 | #ifdef RT_OS_OS2 /* temporary workaround, see ticket #127 */
|
---|
29 | # define mbstat mbstat_os2
|
---|
30 | # include <sys/socket.h>
|
---|
31 | # undef mbstat
|
---|
32 | typedef int socklen_t;
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #define CONFIG_QEMU
|
---|
36 |
|
---|
37 | #ifdef DEBUG
|
---|
38 | # undef DEBUG
|
---|
39 | # define DEBUG 1
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #ifndef CONFIG_QEMU
|
---|
43 | # include "version.h"
|
---|
44 | #endif
|
---|
45 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
46 | #include <VBox/log.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #ifdef RT_OS_WINDOWS
|
---|
49 | # include <windows.h>
|
---|
50 | # include <io.h>
|
---|
51 | #endif
|
---|
52 | #include <iprt/asm.h>
|
---|
53 | #include <iprt/assert.h>
|
---|
54 | #include <iprt/string.h>
|
---|
55 | #include <iprt/dir.h>
|
---|
56 | #include <iprt/rand.h>
|
---|
57 | #include <VBox/types.h>
|
---|
58 |
|
---|
59 | #undef malloc
|
---|
60 | #define malloc dont_use_malloc
|
---|
61 | #undef free
|
---|
62 | #define free dont_use_free
|
---|
63 | #undef realloc
|
---|
64 | #define realloc dont_use_realloc
|
---|
65 | #undef strdup
|
---|
66 | #define strdup dont_use_strdup
|
---|
67 |
|
---|
68 | #include "slirp_config.h"
|
---|
69 |
|
---|
70 | #ifdef RT_OS_WINDOWS
|
---|
71 |
|
---|
72 | # ifndef _MSC_VER
|
---|
73 | # include <inttypes.h>
|
---|
74 | # endif
|
---|
75 |
|
---|
76 |
|
---|
77 | # include <sys/timeb.h>
|
---|
78 | # include <iphlpapi.h>
|
---|
79 |
|
---|
80 | # define EWOULDBLOCK WSAEWOULDBLOCK
|
---|
81 | # define EINPROGRESS WSAEINPROGRESS
|
---|
82 | # define ENOTCONN WSAENOTCONN
|
---|
83 | # define EHOSTUNREACH WSAEHOSTUNREACH
|
---|
84 | # define ENETUNREACH WSAENETUNREACH
|
---|
85 | # define ECONNREFUSED WSAECONNREFUSED
|
---|
86 |
|
---|
87 | typedef uint8_t u_int8_t;
|
---|
88 | typedef uint16_t u_int16_t;
|
---|
89 | typedef uint32_t u_int32_t;
|
---|
90 |
|
---|
91 | #else /* !RT_OS_WINDOWS */
|
---|
92 |
|
---|
93 | # define ioctlsocket ioctl
|
---|
94 | # define closesocket(s) close(s)
|
---|
95 | # define O_BINARY 0
|
---|
96 |
|
---|
97 | #endif /* !RT_OS_WINDOWS */
|
---|
98 |
|
---|
99 | #if defined(RT_OS_WINDOWS) || defined (RT_OS_SOLARIS)
|
---|
100 | typedef uint64_t u_int64_t;
|
---|
101 | typedef char *caddr_t;
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | #include <sys/types.h>
|
---|
105 | #ifdef HAVE_SYS_BITYPES_H
|
---|
106 | # include <sys/bitypes.h>
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | #ifdef _MSC_VER
|
---|
110 | # include <time.h>
|
---|
111 | #else /* !_MSC_VER */
|
---|
112 | # include <sys/time.h>
|
---|
113 | #endif /* !_MSC_VER */
|
---|
114 |
|
---|
115 | #ifdef NEED_TYPEDEFS
|
---|
116 | typedef char int8_t;
|
---|
117 | typedef unsigned char u_int8_t;
|
---|
118 |
|
---|
119 | # if SIZEOF_SHORT == 2
|
---|
120 | typedef short int16_t;
|
---|
121 | typedef unsigned short u_int16_t;
|
---|
122 | # else
|
---|
123 | # if SIZEOF_INT == 2
|
---|
124 | typedef int int16_t;
|
---|
125 | typedef unsigned int u_int16_t;
|
---|
126 | # else
|
---|
127 | #error Cannot find a type with sizeof() == 2
|
---|
128 | # endif
|
---|
129 | # endif
|
---|
130 |
|
---|
131 | # if SIZEOF_SHORT == 4
|
---|
132 | typedef short int32_t;
|
---|
133 | typedef unsigned short u_int32_t;
|
---|
134 | # else
|
---|
135 | # if SIZEOF_INT == 4
|
---|
136 | typedef int int32_t;
|
---|
137 | typedef unsigned int u_int32_t;
|
---|
138 | # else
|
---|
139 | #error Cannot find a type with sizeof() == 4
|
---|
140 | # endif
|
---|
141 | # endif
|
---|
142 | #endif /* NEED_TYPEDEFS */
|
---|
143 |
|
---|
144 | #ifdef HAVE_UNISTD_H
|
---|
145 | # include <unistd.h>
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | #ifdef HAVE_STDLIB_H
|
---|
149 | # include <stdlib.h>
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | #include <errno.h>
|
---|
153 |
|
---|
154 |
|
---|
155 | #ifndef HAVE_MEMMOVE
|
---|
156 | # define memmove(x, y, z) bcopy(y, x, z)
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | #if TIME_WITH_SYS_TIME
|
---|
160 | # include <sys/time.h>
|
---|
161 | # include <time.h>
|
---|
162 | #else
|
---|
163 | # if HAVE_SYS_TIME_H
|
---|
164 | # include <sys/time.h>
|
---|
165 | # else
|
---|
166 | # include <time.h>
|
---|
167 | # endif
|
---|
168 | #endif
|
---|
169 |
|
---|
170 | #ifdef HAVE_STRING_H
|
---|
171 | # include <string.h>
|
---|
172 | #else
|
---|
173 | # include <strings.h>
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | #ifndef RT_OS_WINDOWS
|
---|
177 | # include <sys/uio.h>
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | #ifndef RT_OS_WINDOWS
|
---|
181 | # include <netinet/in.h>
|
---|
182 | # include <arpa/inet.h>
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | #ifdef GETTIMEOFDAY_ONE_ARG
|
---|
186 | # define gettimeofday(x, y) gettimeofday(x)
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | #ifndef HAVE_INET_ATON
|
---|
190 | int inet_aton (const char *cp, struct in_addr *ia);
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | #include <fcntl.h>
|
---|
194 | #ifndef NO_UNIX_SOCKETS
|
---|
195 | # include <sys/un.h>
|
---|
196 | #endif
|
---|
197 | #include <signal.h>
|
---|
198 | #ifdef HAVE_SYS_SIGNAL_H
|
---|
199 | # include <sys/signal.h>
|
---|
200 | #endif
|
---|
201 | #ifndef RT_OS_WINDOWS
|
---|
202 | # include <sys/socket.h>
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | #if defined(HAVE_SYS_IOCTL_H)
|
---|
206 | # include <sys/ioctl.h>
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | #ifdef HAVE_SYS_SELECT_H
|
---|
210 | # include <sys/select.h>
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | #ifdef HAVE_SYS_WAIT_H
|
---|
214 | # include <sys/wait.h>
|
---|
215 | #endif
|
---|
216 |
|
---|
217 | #ifdef HAVE_SYS_FILIO_H
|
---|
218 | # include <sys/filio.h>
|
---|
219 | #endif
|
---|
220 |
|
---|
221 | #if defined(__STDC__) || defined(_MSC_VER)
|
---|
222 | # include <stdarg.h>
|
---|
223 | #else
|
---|
224 | # include <varargs.h>
|
---|
225 | #endif
|
---|
226 |
|
---|
227 | #include <sys/stat.h>
|
---|
228 |
|
---|
229 | /* Avoid conflicting with the libc insque() and remque(), which
|
---|
230 | * have different prototypes. */
|
---|
231 | #define insque slirp_insque
|
---|
232 | #define remque slirp_remque
|
---|
233 |
|
---|
234 | #ifdef HAVE_SYS_STROPTS_H
|
---|
235 | # include <sys/stropts.h>
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | #include "libslirp.h"
|
---|
239 |
|
---|
240 | #include "debug.h"
|
---|
241 |
|
---|
242 | #include "ip.h"
|
---|
243 | #include "tcp.h"
|
---|
244 | #include "tcp_timer.h"
|
---|
245 | #include "tcp_var.h"
|
---|
246 | #include "tcpip.h"
|
---|
247 | #include "udp.h"
|
---|
248 | #include "icmp_var.h"
|
---|
249 | #include "mbuf.h"
|
---|
250 | #include "sbuf.h"
|
---|
251 | #include "socket.h"
|
---|
252 | #include "if.h"
|
---|
253 | #include "main.h"
|
---|
254 | #include "misc.h"
|
---|
255 | #include "ctl.h"
|
---|
256 | #include "bootp.h"
|
---|
257 | #include "tftp.h"
|
---|
258 |
|
---|
259 | #include "slirp_state.h"
|
---|
260 |
|
---|
261 | #undef PVM /* XXX Mac OS X hack */
|
---|
262 |
|
---|
263 | #ifndef NULL
|
---|
264 | # define NULL (void *)0
|
---|
265 | #endif
|
---|
266 |
|
---|
267 | void if_start (PNATState);
|
---|
268 |
|
---|
269 | #ifndef HAVE_INDEX
|
---|
270 | char *index (const char *, int);
|
---|
271 | #endif
|
---|
272 |
|
---|
273 | #ifndef HAVE_GETHOSTID
|
---|
274 | long gethostid (void);
|
---|
275 | #endif
|
---|
276 |
|
---|
277 | #ifndef RT_OS_WINDOWS
|
---|
278 | #include <netdb.h>
|
---|
279 | #endif
|
---|
280 |
|
---|
281 | #include "dnsproxy/dnsproxy.h"
|
---|
282 |
|
---|
283 | #define DEFAULT_BAUD 115200
|
---|
284 |
|
---|
285 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr);
|
---|
286 |
|
---|
287 | /* cksum.c */
|
---|
288 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
289 | int cksum(struct mbuf *m, int len);
|
---|
290 | #else
|
---|
291 | typedef uint16_t u_short;
|
---|
292 | typedef unsigned int u_int;
|
---|
293 | #include "in_cksum.h"
|
---|
294 | #endif
|
---|
295 |
|
---|
296 | /* if.c */
|
---|
297 | void if_init (PNATState);
|
---|
298 | void if_output (PNATState, struct socket *, struct mbuf *);
|
---|
299 |
|
---|
300 | /* ip_input.c */
|
---|
301 | void ip_init (PNATState);
|
---|
302 | void ip_input (PNATState, struct mbuf *);
|
---|
303 | struct mbuf * ip_reass (PNATState, register struct mbuf *);
|
---|
304 | void ip_freef (PNATState, struct ipqhead *, struct ipq_t *);
|
---|
305 | void ip_slowtimo (PNATState);
|
---|
306 | void ip_stripoptions (register struct mbuf *, struct mbuf *);
|
---|
307 |
|
---|
308 | /* ip_output.c */
|
---|
309 | int ip_output (PNATState, struct socket *, struct mbuf *);
|
---|
310 | int ip_output0 (PNATState, struct socket *, struct mbuf *, int urg);
|
---|
311 |
|
---|
312 | /* tcp_input.c */
|
---|
313 | int tcp_reass (PNATState, struct tcpcb *, struct tcphdr *, int *, struct mbuf *);
|
---|
314 | void tcp_input (PNATState, register struct mbuf *, int, struct socket *);
|
---|
315 | void tcp_dooptions (PNATState, struct tcpcb *, u_char *, int, struct tcpiphdr *);
|
---|
316 | void tcp_xmit_timer (PNATState, register struct tcpcb *, int);
|
---|
317 | int tcp_mss (PNATState, register struct tcpcb *, u_int);
|
---|
318 |
|
---|
319 | /* tcp_output.c */
|
---|
320 | int tcp_output (PNATState, register struct tcpcb *);
|
---|
321 | void tcp_setpersist (register struct tcpcb *);
|
---|
322 |
|
---|
323 | /* tcp_subr.c */
|
---|
324 | void tcp_init (PNATState);
|
---|
325 | void tcp_template (struct tcpcb *);
|
---|
326 | void tcp_respond (PNATState, struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int);
|
---|
327 | struct tcpcb * tcp_newtcpcb (PNATState, struct socket *);
|
---|
328 | struct tcpcb * tcp_close (PNATState, register struct tcpcb *);
|
---|
329 | void tcp_drain (void);
|
---|
330 | void tcp_sockclosed (PNATState, struct tcpcb *);
|
---|
331 | int tcp_fconnect (PNATState, struct socket *);
|
---|
332 | void tcp_connect (PNATState, struct socket *);
|
---|
333 | int tcp_attach (PNATState, struct socket *);
|
---|
334 | u_int8_t tcp_tos (struct socket *);
|
---|
335 | int tcp_ctl (PNATState, struct socket *);
|
---|
336 | struct tcpcb *tcp_drop(PNATState, struct tcpcb *tp, int err);
|
---|
337 |
|
---|
338 | /*slirp.c*/
|
---|
339 | void slirp_arp_who_has(PNATState pData, uint32_t dst);
|
---|
340 | int slirp_arp_cache_update(PNATState pData, uint32_t dst, const uint8_t *mac);
|
---|
341 | void slirp_arp_cache_add(PNATState pData, uint32_t ip, const uint8_t *ether);
|
---|
342 | int slirp_arp_cache_update_or_add(PNATState pData, uint32_t dst, const uint8_t *mac);
|
---|
343 | #define MIN_MRU 128
|
---|
344 | #define MAX_MRU 16384
|
---|
345 |
|
---|
346 | #ifndef RT_OS_WINDOWS
|
---|
347 | # define min(x, y) ((x) < (y) ? (x) : (y))
|
---|
348 | # define max(x, y) ((x) > (y) ? (x) : (y))
|
---|
349 | #endif
|
---|
350 |
|
---|
351 | #ifdef RT_OS_WINDOWS
|
---|
352 | # undef errno
|
---|
353 | # if 0 /* debugging */
|
---|
354 | int errno_func(const char *file, int line);
|
---|
355 | # define errno (errno_func(__FILE__, __LINE__))
|
---|
356 | # else
|
---|
357 | # define errno (WSAGetLastError())
|
---|
358 | # endif
|
---|
359 | #endif
|
---|
360 |
|
---|
361 | # define ETH_ALEN 6
|
---|
362 | # define ETH_HLEN 14
|
---|
363 |
|
---|
364 | # define ARPOP_REQUEST 1 /* ARP request */
|
---|
365 | # define ARPOP_REPLY 2 /* ARP reply */
|
---|
366 |
|
---|
367 | struct ethhdr
|
---|
368 | {
|
---|
369 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
370 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
371 | unsigned short h_proto; /* packet type ID field */
|
---|
372 | };
|
---|
373 | AssertCompileSize(struct ethhdr, 14);
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * (vvl) externing of sscanf.
|
---|
377 | */
|
---|
378 | int sscanf(const char *s, const char *format, ...);
|
---|
379 |
|
---|
380 | #if defined(VBOX_SLIRP_ALIAS) || defined(VBOX_SLIRP_BSD)
|
---|
381 |
|
---|
382 | # define ip_next(ip) (void *)((uint8_t *)(ip) + ((ip)->ip_hl << 2))
|
---|
383 | # define udp_next(udp) (void *)((uint8_t *)&((struct udphdr *)(udp))[1] )
|
---|
384 | # define bcopy(src, dst, len) memcpy((dst), (src), (len))
|
---|
385 | # define bcmp(a1, a2, len) memcmp((a1), (a2), (len))
|
---|
386 | # define NO_FW_PUNCH
|
---|
387 |
|
---|
388 | # ifdef alias_addr
|
---|
389 | # ifndef VBOX_SLIRP_BSD
|
---|
390 | # error alias_addr has already defined!!!
|
---|
391 | # else
|
---|
392 | # undef alias_addr
|
---|
393 | # endif
|
---|
394 | # endif
|
---|
395 |
|
---|
396 | # define arc4random() RTRandU32()
|
---|
397 | # undef malloc
|
---|
398 | # undef calloc
|
---|
399 | # undef free
|
---|
400 | # define malloc(x) RTMemAlloc((x))
|
---|
401 | # define calloc(x, n) RTMemAllocZ((x)*(n))
|
---|
402 | # define free(x) RTMemFree((x))
|
---|
403 | # ifndef __unused
|
---|
404 | # define __unused
|
---|
405 | # endif
|
---|
406 |
|
---|
407 | # define strncasecmp RTStrNICmp
|
---|
408 | # define stderr NULL
|
---|
409 | # define stdout NULL
|
---|
410 |
|
---|
411 | # ifdef VBOX_WITH_DEBUG_LIBALIAS
|
---|
412 | # define LIBALIAS_DEBUG
|
---|
413 | # endif
|
---|
414 |
|
---|
415 | # define fflush(x) do{} while(0)
|
---|
416 | # include "ext.h"
|
---|
417 | #endif /*VBOX_SLIRP_ALIAS*/
|
---|
418 |
|
---|
419 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
420 | /**
|
---|
421 | * @todo might be useful to make it configurable, especially in terms of Intnet behind NAT
|
---|
422 | */
|
---|
423 | # define maxusers 32
|
---|
424 | # define max_protohdr 0
|
---|
425 | /**
|
---|
426 | * @todo (vvl) for now ignore these values, later perhaps initialize tuning parameters
|
---|
427 | */
|
---|
428 | # define TUNABLE_INT_FETCH(name, pval) do { } while (0)
|
---|
429 | # define SYSCTL_PROC(a0, a1, a2, a3, a4, a5, a6, a7, a8) const int dummy_ ## a6 = 0
|
---|
430 | # define SYSCTL_STRUCT(a0, a1, a2, a3, a4, a5, a6) const int dummy_ ## a5 = 0
|
---|
431 | # define SYSINIT(a0, a1, a2, a3, a4) const int dummy_ ## a3 = 0
|
---|
432 | # define sysctl_handle_int(a0, a1, a2, a3) 0
|
---|
433 | # define EVENTHANDLER_INVOKE(a) do{}while(0)
|
---|
434 | # define EVENTHANDLER_REGISTER(a0, a1, a2, a3) do{}while(0)
|
---|
435 | # define KASSERT AssertMsg
|
---|
436 |
|
---|
437 | struct dummy_req
|
---|
438 | {
|
---|
439 | void *newptr;
|
---|
440 | };
|
---|
441 |
|
---|
442 | #define SYSCTL_HANDLER_ARGS PNATState pData, void *oidp, struct dummy_req *req
|
---|
443 |
|
---|
444 | void mbuf_init(void *);
|
---|
445 | # define cksum(m, len) in_cksum_skip((m), (len), 0)
|
---|
446 | #endif
|
---|
447 |
|
---|
448 | int ftp_alias_load(PNATState);
|
---|
449 | int ftp_alias_unload(PNATState);
|
---|
450 | int nbt_alias_load(PNATState);
|
---|
451 | int nbt_alias_unload(PNATState);
|
---|
452 | int dns_alias_load(PNATState);
|
---|
453 | int dns_alias_unload(PNATState);
|
---|
454 | int slirp_arp_lookup_ip_by_ether(PNATState, const uint8_t *, uint32_t *);
|
---|
455 | int slirp_arp_lookup_ether_by_ip(PNATState, uint32_t, uint8_t *);
|
---|
456 | #endif
|
---|
457 |
|
---|