VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use