VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/slirp.h@ 88525

Last change on this file since 88525 was 87126, checked in by vboxsync, 4 years ago

slirp: arp_input - more checks on incoming packets.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/* $Id: slirp.h 87126 2020-12-25 02:51:44Z vboxsync $ */
2/** @file
3 * NAT - slirp (declarations/defines).
4 */
5
6/*
7 * Copyright (C) 2006-2020 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/vmm/stam.h>
22
23#ifdef RT_OS_WINDOWS
24# include <iprt/win/winsock2.h>
25# include <iprt/win/ws2tcpip.h>
26typedef 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
32typedef 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 <iprt/win/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 <iprt/net.h>
58#include <VBox/types.h>
59
60#undef malloc
61#define malloc dont_use_malloc
62#undef free
63#define free dont_use_free
64#undef realloc
65#define realloc dont_use_realloc
66#undef strdup
67#define strdup dont_use_strdup
68
69#include "slirp_config.h"
70
71#ifdef RT_OS_WINDOWS
72
73# ifndef _MSC_VER
74# include <inttypes.h>
75# endif
76
77
78# include <sys/timeb.h>
79# include <iprt/win/iphlpapi.h>
80
81/* We don't want the errno.h versions of these error defines. */
82# if defined(_MSC_VER) && _MSC_VER >= 1600
83# include <errno.h>
84# undef ECONNREFUSED
85# undef ECONNRESET
86# undef EHOSTDOWN
87# undef EHOSTUNREACH
88# undef EINPROGRESS
89# undef ENETDOWN
90# undef ENETUNREACH
91# undef ENOTCONN
92# undef ESHUTDOWN
93# undef EWOULDBLOCK
94# endif
95# define ECONNREFUSED WSAECONNREFUSED
96# define ECONNRESET WSAECONNRESET
97# define EHOSTDOWN WSAEHOSTDOWN
98# define EHOSTUNREACH WSAEHOSTUNREACH
99# define EINPROGRESS WSAEINPROGRESS
100# define ENETDOWN WSAENETDOWN
101# define ENETUNREACH WSAENETUNREACH
102# define ENOTCONN WSAENOTCONN
103# define ESHUTDOWN WSAESHUTDOWN
104# define EWOULDBLOCK WSAEWOULDBLOCK
105
106/* standard names for the shutdown() "how" argument */
107#define SHUT_RD SD_RECEIVE
108#define SHUT_WR SD_SEND
109#define SHUT_RDWR SD_BOTH
110
111typedef uint8_t u_int8_t;
112typedef uint16_t u_int16_t;
113typedef uint32_t u_int32_t;
114
115#else /* !RT_OS_WINDOWS */
116
117# define ioctlsocket ioctl
118# define closesocket(s) close(s)
119# define O_BINARY 0
120
121#endif /* !RT_OS_WINDOWS */
122
123#if defined(RT_OS_WINDOWS) || defined (RT_OS_SOLARIS)
124typedef uint64_t u_int64_t;
125typedef char *caddr_t;
126#endif
127
128#include <sys/types.h>
129#ifdef HAVE_SYS_BITYPES_H
130# include <sys/bitypes.h>
131#endif
132
133#ifdef _MSC_VER
134# include <time.h>
135#else /* !_MSC_VER */
136# include <sys/time.h>
137#endif /* !_MSC_VER */
138
139#ifdef NEED_TYPEDEFS
140typedef char int8_t;
141typedef unsigned char u_int8_t;
142
143# if SIZEOF_SHORT == 2
144 typedef short int16_t;
145 typedef unsigned short u_int16_t;
146# else
147# if SIZEOF_INT == 2
148 typedef int int16_t;
149 typedef unsigned int u_int16_t;
150# else
151 #error Cannot find a type with sizeof() == 2
152# endif
153# endif
154
155# if SIZEOF_SHORT == 4
156 typedef short int32_t;
157 typedef unsigned short u_int32_t;
158# else
159# if SIZEOF_INT == 4
160 typedef int int32_t;
161 typedef unsigned int u_int32_t;
162# else
163 #error Cannot find a type with sizeof() == 4
164# endif
165# endif
166#endif /* NEED_TYPEDEFS */
167
168#ifdef HAVE_UNISTD_H
169# include <unistd.h>
170#endif
171
172#ifdef HAVE_STDLIB_H
173# include <stdlib.h>
174#endif
175
176#include <errno.h>
177
178
179#ifndef HAVE_MEMMOVE
180# define memmove(x, y, z) bcopy(y, x, z)
181#endif
182
183#if TIME_WITH_SYS_TIME
184# include <sys/time.h>
185# include <time.h>
186#else
187# ifndef HAVE_SYS_TIME_H
188# define HAVE_SYS_TIME_H 0
189# endif
190# if HAVE_SYS_TIME_H
191# include <sys/time.h>
192# else
193# include <time.h>
194# endif
195#endif
196
197#ifdef HAVE_STRING_H
198# include <string.h>
199#else
200# include <strings.h>
201#endif
202
203#ifndef RT_OS_WINDOWS
204# include <sys/uio.h>
205#endif
206
207#ifndef RT_OS_WINDOWS
208# include <netinet/in.h>
209# include <arpa/inet.h>
210#endif
211
212#ifdef GETTIMEOFDAY_ONE_ARG
213# define gettimeofday(x, y) gettimeofday(x)
214#endif
215
216#ifndef HAVE_INET_ATON
217int inet_aton (const char *cp, struct in_addr *ia);
218#endif
219
220#include <fcntl.h>
221#ifndef NO_UNIX_SOCKETS
222# include <sys/un.h>
223#endif
224#include <signal.h>
225#ifdef HAVE_SYS_SIGNAL_H
226# include <sys/signal.h>
227#endif
228#ifndef RT_OS_WINDOWS
229# include <sys/socket.h>
230#endif
231
232#if defined(HAVE_SYS_IOCTL_H)
233# include <sys/ioctl.h>
234#endif
235
236#ifdef HAVE_SYS_SELECT_H
237# include <sys/select.h>
238#endif
239
240#ifdef HAVE_SYS_WAIT_H
241# include <sys/wait.h>
242#endif
243
244#ifdef HAVE_SYS_FILIO_H
245# include <sys/filio.h>
246#endif
247
248#if defined(__STDC__) || defined(_MSC_VER)
249# include <stdarg.h>
250#else
251# include <varargs.h>
252#endif
253
254#include <sys/stat.h>
255
256/* Avoid conflicting with the libc insque() and remque(), which
257 * have different prototypes. */
258#define insque slirp_insque
259#define remque slirp_remque
260
261#ifdef HAVE_SYS_STROPTS_H
262# include <sys/stropts.h>
263#endif
264
265#include "libslirp.h"
266
267#include "debug.h"
268
269#include "ip.h"
270#include "tcp.h"
271#include "tcp_timer.h"
272#include "tcp_var.h"
273#include "tcpip.h"
274#include "udp.h"
275#include "icmp_var.h"
276#include "mbuf.h"
277#include "if.h"
278#include "sbuf.h"
279#include "socket.h"
280#include "main.h"
281#include "misc.h"
282#include "ctl.h"
283#include "bootp.h"
284#include "tftp.h"
285
286#include "slirp_state.h"
287#include "slirp_dns.h"
288
289#undef PVM /* XXX Mac OS X hack */
290
291#ifndef NULL
292# define NULL (void *)0
293#endif
294
295void if_start (PNATState);
296
297#ifndef HAVE_INDEX
298 char *index (const char *, int);
299#endif
300
301#ifndef HAVE_GETHOSTID
302 long gethostid (void);
303#endif
304
305#ifndef RT_OS_WINDOWS
306#include <netdb.h>
307#endif
308
309#include "dnsproxy/dnsproxy.h"
310
311#define DEFAULT_BAUD 115200
312
313int get_dns_addr(PNATState pData);
314
315/* cksum.c */
316typedef uint16_t u_short;
317typedef unsigned int u_int;
318#include "in_cksum.h"
319
320/* if.c */
321void if_init (PNATState);
322void if_output (PNATState, struct socket *, struct mbuf *);
323
324/* ip_input.c */
325void ip_init (PNATState);
326void ip_input (PNATState, struct mbuf *);
327struct mbuf * ip_reass (PNATState, register struct mbuf *);
328void ip_freef (PNATState, struct ipqhead *, struct ipq_t *);
329void ip_slowtimo (PNATState);
330void ip_stripoptions (register struct mbuf *, struct mbuf *);
331
332/* ip_output.c */
333int ip_output (PNATState, struct socket *, struct mbuf *);
334int ip_output0 (PNATState, struct socket *, struct mbuf *, int urg);
335
336/* tcp_input.c */
337int tcp_reass (PNATState, struct tcpcb *, struct tcphdr *, int *, struct mbuf *);
338void tcp_input (PNATState, register struct mbuf *, int, struct socket *);
339void tcp_fconnect_failed(PNATState, struct socket *, int);
340void tcp_dooptions (PNATState, struct tcpcb *, u_char *, int, struct tcpiphdr *);
341void tcp_xmit_timer (PNATState, register struct tcpcb *, int);
342int tcp_mss (PNATState, register struct tcpcb *, u_int);
343
344/* tcp_output.c */
345int tcp_output (PNATState, register struct tcpcb *);
346void tcp_setpersist (register struct tcpcb *);
347
348/* tcp_subr.c */
349void tcp_init (PNATState);
350void tcp_template (struct tcpcb *);
351void tcp_respond (PNATState, struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int);
352struct tcpcb * tcp_newtcpcb (PNATState, struct socket *);
353struct tcpcb * tcp_close (PNATState, register struct tcpcb *);
354void tcp_drain (void);
355void tcp_sockclosed (PNATState, struct tcpcb *);
356int tcp_fconnect (PNATState, struct socket *);
357void tcp_connect (PNATState, struct socket *);
358int tcp_attach (PNATState, struct socket *);
359u_int8_t tcp_tos (struct socket *);
360int tcp_ctl (PNATState, struct socket *);
361struct tcpcb *tcp_drop(PNATState, struct tcpcb *tp, int err);
362
363/* hostres.c */
364struct mbuf *hostresolver(PNATState, struct mbuf *, uint32_t src, uint16_t sport);
365
366/*slirp.c*/
367void slirp_arp_who_has(PNATState pData, uint32_t dst);
368int slirp_arp_cache_update_or_add(PNATState pData, uint32_t dst, const uint8_t *mac);
369int slirp_init_dns_list(PNATState pData);
370void slirp_release_dns_list(PNATState pData);
371#define MIN_MRU 128
372#define MAX_MRU 16384
373
374#ifndef RT_OS_WINDOWS
375# define min(x, y) ((x) < (y) ? (x) : (y))
376# define max(x, y) ((x) > (y) ? (x) : (y))
377#endif
378
379#ifdef RT_OS_WINDOWS
380# undef errno
381# if 0 /* debugging */
382int errno_func(const char *file, int line);
383# define errno (errno_func(__FILE__, __LINE__))
384# else
385# define errno (WSAGetLastError())
386# endif
387#endif
388
389# define ETH_ALEN 6
390# define ETH_HLEN 14
391
392struct ethhdr
393{
394 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
395 unsigned char h_source[ETH_ALEN]; /* source ether addr */
396 unsigned short h_proto; /* packet type ID field */
397};
398AssertCompileSize(struct ethhdr, 14);
399
400/*
401 * (vvl) externing of sscanf.
402 */
403int sscanf(const char *s, const char *format, ...);
404
405#if defined(VBOX_SLIRP_ALIAS) || defined(VBOX_SLIRP_BSD)
406
407# define ip_next(ip) (void *)((uint8_t *)(ip) + ((ip)->ip_hl << 2))
408# define udp_next(udp) (void *)((uint8_t *)&((struct udphdr *)(udp))[1])
409# undef bcopy
410# define bcopy(src, dst, len) memcpy((dst), (src), (len))
411# undef bcmp
412# define bcmp(a1, a2, len) memcmp((a1), (a2), (len))
413# define NO_FW_PUNCH
414/* Two wrongs don't make a right, but this at least averts harm. */
415# define NO_USE_SOCKETS
416
417# ifdef alias_addr
418# ifndef VBOX_SLIRP_BSD
419# error alias_addr has already defined!!!
420# else
421# undef alias_addr
422# endif
423# endif
424
425# define arc4random() RTRandU32()
426# undef malloc
427# undef calloc
428# undef free
429# define malloc(x) RTMemAlloc((x))
430# define calloc(x, n) RTMemAllocZ((x)*(n))
431# define free(x) RTMemFree((x))
432# ifndef __unused
433# define __unused
434# endif
435
436# define strncasecmp RTStrNICmp
437# define stderr NULL
438# define stdout NULL
439
440# ifdef VBOX_WITH_DEBUG_LIBALIAS
441# define LIBALIAS_DEBUG
442# endif
443
444# define fflush(x) do{} while(0)
445# include "ext.h"
446#endif /*VBOX_SLIRP_ALIAS*/
447
448/**
449 * @todo might be useful to make it configurable, especially in terms of Intnet behind NAT
450 */
451# define maxusers 32
452# define max_protohdr 0
453/**
454 * @todo (vvl) for now ignore these values, later perhaps initialize tuning parameters
455 */
456# define TUNABLE_INT_FETCH(name, pval) do { } while (0)
457# define SYSCTL_PROC(a0, a1, a2, a3, a4, a5, a6, a7, a8) const int dummy_ ## a6 = 0
458# define SYSCTL_STRUCT(a0, a1, a2, a3, a4, a5, a6) const int dummy_ ## a5 = 0
459# define SYSINIT(a0, a1, a2, a3, a4) const int dummy_ ## a3 = 0
460# define sysctl_handle_int(a0, a1, a2, a3) 0
461# define EVENTHANDLER_INVOKE(a) do{}while(0)
462# define EVENTHANDLER_REGISTER(a0, a1, a2, a3) do{}while(0)
463# define KASSERT AssertMsg
464
465struct dummy_req
466{
467 void *newptr;
468};
469
470#define SYSCTL_HANDLER_ARGS PNATState pData, void *oidp, struct dummy_req *req
471
472void mbuf_init(void *);
473# define cksum(m, len) in_cksum_skip((m), (len), 0)
474
475int ftp_alias_load(PNATState);
476int ftp_alias_unload(PNATState);
477int nbt_alias_load(PNATState);
478int nbt_alias_unload(PNATState);
479int slirp_arp_lookup_ip_by_ether(PNATState, const uint8_t *, uint32_t *);
480int slirp_arp_lookup_ether_by_ip(PNATState, uint32_t, uint8_t *);
481
482DECLINLINE(unsigned) slirp_size(PNATState pData)
483{
484 if (if_mtu < MSIZE)
485 return MCLBYTES;
486 else if (if_mtu < MCLBYTES)
487 return MCLBYTES;
488 else if (if_mtu < MJUM9BYTES)
489 return MJUM9BYTES;
490 else if (if_mtu < MJUM16BYTES)
491 return MJUM16BYTES;
492 else
493 AssertMsgFailed(("Unsupported size"));
494 return 0;
495}
496
497static inline bool slirpMbufTagService(PNATState pData, struct mbuf *m, uint8_t u8ServiceId)
498{
499 struct m_tag * t = NULL;
500 NOREF(pData);
501 /* if_encap assumes that all packets goes through aliased address(gw) */
502 if (u8ServiceId == CTL_ALIAS)
503 return true;
504 t = m_tag_get(PACKET_SERVICE, sizeof(uint8_t), 0);
505 if (!t)
506 return false;
507 *(uint8_t *)&t[1] = u8ServiceId;
508 m_tag_prepend(m, t);
509 return true;
510}
511
512/**
513 * This function tags mbuf allocated for special services.
514 * @todo: add service id verification.
515 */
516static inline struct mbuf *slirpServiceMbufAlloc(PNATState pData, uint8_t u8ServiceId)
517{
518 struct mbuf *m = NULL;
519 m = m_getcl(pData, M_DONTWAIT, MT_HEADER, M_PKTHDR);
520 if (!m)
521 return m;
522 if(!slirpMbufTagService(pData, m, u8ServiceId))
523 {
524 m_freem(pData, m);
525 return NULL;
526 }
527 return m;
528}
529
530static inline struct mbuf *slirpDnsMbufAlloc(PNATState pData)
531{
532 return slirpServiceMbufAlloc(pData, CTL_DNS);
533}
534
535DECLINLINE(bool) slirpIsWideCasting(PNATState pData, uint32_t u32Addr)
536{
537 bool fWideCasting;
538 LogFlowFunc(("Enter: u32Addr:%RTnaipv4\n", u32Addr));
539 fWideCasting = ( u32Addr == INADDR_BROADCAST
540 || (u32Addr & RT_H2N_U32_C(~pData->netmask)) == RT_H2N_U32_C(~pData->netmask));
541 LogFlowFunc(("Leave: %RTbool\n", fWideCasting));
542 return fWideCasting;
543}
544#endif
545
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette