VirtualBox

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

Last change on this file since 59350 was 59219, checked in by vboxsync, 9 years ago

NAT: Improve dns mapping in host resolver - pattern match should
ignore case; make literal match take priority over pattern match if
both are applicable.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 KB
Line 
1/** @file
2 * NAT - slirp state/configuration.
3 */
4
5/*
6 * Copyright (C) 2006-2015 Oracle Corporation
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
17#ifndef ___slirp_state_h
18#define ___slirp_state_h
19
20#include <iprt/req.h>
21#include <iprt/critsect.h>
22
23#define COUNTERS_INIT
24#include "counters.h"
25
26#include "ip_icmp.h"
27#include "dnsproxy/dnsproxy.h"
28
29
30/** Where to start DHCP IP number allocation. */
31#define START_ADDR 15
32
33/** DHCP Lease time. */
34#define LEASE_TIME (24 * 3600)
35
36/*
37 * ARP cache this is naive implementaion of ARP
38 * cache of mapping 4 byte IPv4 address to 6 byte
39 * ethernet one.
40 */
41struct arp_cache_entry
42{
43 uint32_t ip;
44 uint8_t ether[6];
45 LIST_ENTRY(arp_cache_entry) list;
46};
47LIST_HEAD(arp_cache_head, arp_cache_entry);
48
49/** TFTP session entry. */
50struct dns_domain_entry
51{
52 char *dd_pszDomain;
53 LIST_ENTRY(dns_domain_entry) dd_list;
54};
55LIST_HEAD(dns_domain_list_head, dns_domain_entry);
56
57#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
58typedef struct DNSMAPPINGENTRY
59{
60 /** Literal or pattern. */
61 bool fPattern;
62 /** Host name or pattern to map. */
63 char *pszName;
64 /** The IP Address. */
65 uint32_t u32IpAddress;
66 /** List entry. */
67 STAILQ_ENTRY(DNSMAPPINGENTRY) MapList;
68} DNSMAPPINGENTRY, *PDNSMAPPINGENTRY;
69typedef STAILQ_HEAD(DNSMAPPINGHEAD, DNSMAPPINGENTRY) DNSMAPPINGHEAD;
70#endif
71
72struct dns_entry
73{
74 struct in_addr de_addr;
75 TAILQ_ENTRY(dns_entry) de_list;
76};
77TAILQ_HEAD(dns_list_head, dns_entry);
78TAILQ_HEAD(if_queue, mbuf);
79
80struct port_forward_rule
81{
82 uint16_t proto;
83 uint16_t host_port;
84 uint16_t guest_port;
85 struct in_addr guest_addr;
86 struct in_addr bind_ip;
87 int activated;
88 struct socket *so;
89 LIST_ENTRY(port_forward_rule) list;
90};
91LIST_HEAD(port_forward_rule_list, port_forward_rule);
92
93
94#ifdef RT_OS_WINDOWS
95struct pong;
96TAILQ_HEAD(pong_tailq, pong);
97#endif
98
99/* forward declaration */
100struct proto_handler;
101
102/** Main state/configuration structure for slirp NAT. */
103typedef struct NATState
104{
105#define PROFILE_COUNTER(name, dsc) STAMPROFILE Stat ## name
106#define COUNTING_COUNTER(name, dsc) STAMCOUNTER Stat ## name
107#include "counters.h"
108 /* Stuff from boot.c */
109 void *pbootp_clients;
110 const char *bootp_filename;
111 /* Stuff from if.c */
112 int if_mtu, if_mru;
113 int if_comp;
114 int if_maxlinkhdr;
115 int if_queued;
116 int if_thresh;
117 /* Stuff from icmp.c */
118 struct icmpstat_t icmpstat;
119 /* Stuff from ip_input.c */
120 struct ipstat_t ipstat;
121 struct ipqhead ipq[IPREASS_NHASH];
122 int maxnipq; /* Administrative limit on # of reass queues*/
123 int maxfragsperpacket; /* Maximum number of IPv4 fragments allowed per packet */
124 int nipq; /* total number of reass queues */
125 uint16_t ip_currid;
126 /* Stuff from mbuf.c */
127 /* Stuff from slirp.c */
128 void *pvUser;
129 uint32_t curtime;
130 uint32_t time_fasttimo;
131 uint32_t last_slowtimo;
132 bool do_slowtimo;
133 bool link_up;
134 struct timeval tt;
135 struct in_addr our_addr;
136 struct in_addr alias_addr;
137 struct in_addr special_addr;
138 struct in_addr guest_addr_guess;
139
140 int tcp_rcvspace;
141 int tcp_sndspace;
142 int socket_rcv;
143 int socket_snd;
144 int soMaxConn;
145#ifdef RT_OS_WINDOWS
146 ULONG (WINAPI * pfGetAdaptersAddresses)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG);
147#endif
148 struct dns_list_head pDnsList;
149 struct dns_domain_list_head pDomainList;
150 uint32_t dnsgen; /* XXX: merge with dnsLastUpdate? */
151 struct in_addr tftp_server;
152 struct in_addr loopback_addr;
153 uint32_t dnsLastUpdate;
154 uint32_t netmask;
155 uint8_t client_ethaddr[6];
156 const uint8_t *slirp_ethaddr;
157 char slirp_hostname[33];
158 bool fPassDomain;
159 struct in_addr bindIP;
160 /* Stuff from tcp_input.c */
161 struct socket tcb;
162
163 struct socket *tcp_last_so;
164 tcp_seq tcp_iss;
165 /* Stuff from tcp_timer.c */
166 struct tcpstat_t tcpstat;
167 uint32_t tcp_now;
168 int tcp_reass_qsize;
169 int tcp_reass_maxqlen;
170 int tcp_reass_maxseg;
171 int tcp_reass_overflows;
172 /* Stuff from tftp.c */
173 void *pvTftpSessions;
174 int cTftpSession;
175 const char *tftp_prefix;
176 /* Stuff from udp.c */
177 struct udpstat_t udpstat;
178 struct socket udb;
179 struct socket *udp_last_so;
180
181# ifndef RT_OS_WINDOWS
182 /* counter of sockets needed for allocation enough room to
183 * process sockets with poll/epoll
184 *
185 * NSOCK_INC/DEC should be injected before every
186 * operation on socket queue (tcb, udb)
187 */
188 int nsock;
189# define NSOCK_INC() do {pData->nsock++;} while (0)
190# define NSOCK_DEC() do {pData->nsock--;} while (0)
191# define NSOCK_INC_EX(ex) do {ex->pData->nsock++;} while (0)
192# define NSOCK_DEC_EX(ex) do {ex->pData->nsock--;} while (0)
193# else
194# define NSOCK_INC() do {} while (0)
195# define NSOCK_DEC() do {} while (0)
196# define NSOCK_INC_EX(ex) do {} while (0)
197# define NSOCK_DEC_EX(ex) do {} while (0)
198# endif
199
200 struct socket icmp_socket;
201# if !defined(RT_OS_WINDOWS)
202 struct icmp_storage icmp_msg_head;
203 int cIcmpCacheSize;
204 int iIcmpCacheLimit;
205# else
206 struct pong_tailq pongs_expected;
207 struct pong_tailq pongs_received;
208 size_t cbIcmpPending;
209# endif
210
211#if defined(RT_OS_WINDOWS)
212# define VBOX_SOCKET_EVENT (pData->phEvents[VBOX_SOCKET_EVENT_INDEX])
213 HANDLE phEvents[VBOX_EVENT_COUNT];
214#endif
215#ifdef zone_mbuf
216# undef zone_mbuf
217#endif
218 uma_zone_t zone_mbuf;
219#ifdef zone_clust
220# undef zone_clust
221#endif
222 uma_zone_t zone_clust;
223#ifdef zone_pack
224# undef zone_pack
225#endif
226 uma_zone_t zone_pack;
227#ifdef zone_jumbop
228# undef zone_jumbop
229#endif
230 uma_zone_t zone_jumbop;
231#ifdef zone_jumbo9
232# undef zone_jumbo9
233#endif
234 uma_zone_t zone_jumbo9;
235#ifdef zone_jumbo16
236# undef zone_jumbo16
237#endif
238 uma_zone_t zone_jumbo16;
239#ifdef zone_ext_refcnt
240# undef zone_ext_refcnt
241 int nmbclusters; /* limits number of mbuf clusters */
242 int nmbjumbop; /* limits number of page size jumbo clusters */
243 int nmbjumbo9; /* limits number of 9k jumbo clusters */
244 int nmbjumbo16; /* limits number of 16k jumbo clusters */
245 struct mbstat mbstat;
246#endif
247 uma_zone_t zone_ext_refcnt;
248 /**
249 * in (r89055) using of this behaviour has been changed and mean that Slirp
250 * can't parse hosts strucutures/files to provide to guest host name-resolving
251 * configuration, instead Slirp provides .{interface-number + 1}.3 as a nameserver
252 * and proxies DNS queiries to Host's Name Resolver API.
253 */
254 bool fUseHostResolver;
255 /**
256 * Flag whether using the host resolver mode is permanent
257 * because the user configured it that way.
258 */
259 bool fUseHostResolverPermanent;
260 /* from dnsproxy/dnsproxy.h*/
261 unsigned int authoritative_port;
262 unsigned int authoritative_timeout;
263 unsigned int recursive_port;
264 unsigned int recursive_timeout;
265 unsigned int stats_timeout;
266 unsigned int port;
267
268 unsigned long active_queries;
269 unsigned long all_queries;
270 unsigned long authoritative_queries;
271 unsigned long recursive_queries;
272 unsigned long removed_queries;
273 unsigned long dropped_queries;
274 unsigned long answered_queries;
275 unsigned long dropped_answers;
276 unsigned long late_answers;
277 unsigned long hash_collisions;
278 /*dnsproxy/dnsproxy.c*/
279 unsigned short queryid;
280 struct sockaddr_in authoritative_addr;
281 struct sockaddr_in recursive_addr;
282 int sock_query;
283 int sock_answer;
284 /* dnsproxy/hash.c */
285#define HASHSIZE 10
286#define HASH(id) (id & ((1 << HASHSIZE) - 1))
287 struct request *request_hash[1 << HASHSIZE];
288 /* this field control behaviour of DHCP server */
289 bool fUseDnsProxy;
290
291 LIST_HEAD(RT_NOTHING, libalias) instancehead;
292 int i32AliasMode;
293 struct libalias *proxy_alias;
294 LIST_HEAD(handler_chain, proto_handler) handler_chain;
295 /** Critical R/W section to protect the handler chain list. */
296 RTCRITSECTRW CsRwHandlerChain;
297 struct port_forward_rule_list port_forward_rule_head;
298 struct arp_cache_head arp_cache;
299 /* libalis modules' handlers*/
300 struct proto_handler *ftp_module;
301 struct proto_handler *nbt_module;
302#ifdef VBOX_WITH_NAT_SEND2HOME
303 /* array of home addresses */
304 struct sockaddr_in *pInSockAddrHomeAddress;
305 /* size of pInSockAddrHomeAddress in elements */
306 int cInHomeAddressSize;
307#endif
308#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
309 DNSMAPPINGHEAD DNSMapNames;
310 DNSMAPPINGHEAD DNSMapPatterns;
311#endif
312} NATState;
313
314
315/** Default IP time to live. */
316#define ip_defttl IPDEFTTL
317
318/** Number of permanent buffers in mbuf. */
319#define mbuf_thresh 30
320
321/** Use a fixed time before sending keepalive. */
322#define tcp_keepidle TCPTV_KEEP_IDLE
323
324/** Use a fixed interval between keepalive. */
325#define tcp_keepintvl TCPTV_KEEPINTVL
326
327/** Maximum idle time before timing out a connection. */
328#define tcp_maxidle (TCPTV_KEEPCNT * tcp_keepintvl)
329
330/** Default TCP socket options. */
331#define so_options DO_KEEPALIVE
332
333/** Default TCP MSS value. */
334#define tcp_mssdflt TCP_MSS
335
336/** Default TCP round trip time. */
337#define tcp_rttdflt (TCPTV_SRTTDFLT / PR_SLOWHZ)
338
339/** Enable RFC1323 performance enhancements.
340 * @todo check if it really works, it was turned off before. */
341#define tcp_do_rfc1323 1
342
343/** TCP receive buffer size. */
344#define tcp_rcvspace pData->tcp_rcvspace
345
346/** TCP receive buffer size. */
347#define tcp_sndspace pData->tcp_sndspace
348
349/* TCP duplicate ACK retransmit threshold. */
350#define tcprexmtthresh 3
351
352
353#define bootp_filename pData->bootp_filename
354
355#define if_mtu pData->if_mtu
356#define if_mru pData->if_mru
357#define if_comp pData->if_comp
358#define if_maxlinkhdr pData->if_maxlinkhdr
359#define if_queued pData->if_queued
360#define if_thresh pData->if_thresh
361
362#define icmpstat pData->icmpstat
363
364#define ipstat pData->ipstat
365#define ipq pData->ipq
366#define ip_currid pData->ip_currid
367
368#define mbuf_alloced pData->mbuf_alloced
369#define mbuf_max pData->mbuf_max
370#define msize pData->msize
371#define m_freelist pData->m_freelist
372#define m_usedlist pData->m_usedlist
373
374#define curtime pData->curtime
375#define time_fasttimo pData->time_fasttimo
376#define last_slowtimo pData->last_slowtimo
377#define do_slowtimo pData->do_slowtimo
378#define link_up pData->link_up
379#define cUsers pData->cUsers
380#define tt pData->tt
381#define our_addr pData->our_addr
382#ifndef VBOX_SLIRP_ALIAS
383# define alias_addr pData->alias_addr
384#else
385# define handler_chain pData->handler_chain
386#endif
387#define dns_addr pData->dns_addr
388#define loopback_addr pData->loopback_addr
389#define client_ethaddr pData->client_ethaddr
390#define slirp_hostname pData->slirp_hostname
391
392#define tcb pData->tcb
393#define tcp_last_so pData->tcp_last_so
394#define tcp_iss pData->tcp_iss
395
396#define tcpstat pData->tcpstat
397#define tcp_now pData->tcp_now
398
399#define tftp_prefix pData->tftp_prefix
400
401#define udpstat pData->udpstat
402#define udb pData->udb
403#define udp_last_so pData->udp_last_so
404
405#define maxfragsperpacket pData->maxfragsperpacket
406#define maxnipq pData->maxnipq
407#define nipq pData->nipq
408
409#define tcp_reass_qsize pData->tcp_reass_qsize
410#define tcp_reass_maxqlen pData->tcp_reass_maxqlen
411#define tcp_reass_maxseg pData->tcp_reass_maxseg
412#define tcp_reass_overflows pData->tcp_reass_overflows
413
414#define queue_tcp_label tcb
415#define queue_udp_label udb
416#define VBOX_X2(x) x
417#define VBOX_X(x) VBOX_X2(x)
418
419#if 1
420
421# define QSOCKET_LOCK(queue) do {} while (0)
422# define QSOCKET_UNLOCK(queue) do {} while (0)
423# define QSOCKET_LOCK_CREATE(queue) do {} while (0)
424# define QSOCKET_LOCK_DESTROY(queue) do {} while (0)
425# define QSOCKET_FOREACH(so, sonext, label) \
426 for ((so) = VBOX_X2(queue_ ## label ## _label).so_next; \
427 (so) != &(VBOX_X2(queue_ ## label ## _label)); \
428 (so) = (sonext)) \
429 { \
430 (sonext) = (so)->so_next; \
431 Log5(("%s:%d Processing so:%R[natsock]\n", RT_GCC_EXTENSION __FUNCTION__, __LINE__, (so)));
432# define CONTINUE(label) continue
433# define CONTINUE_NO_UNLOCK(label) continue
434# define LOOP_LABEL(label, so, sonext) /* empty*/
435# define DO_TCP_OUTPUT(data, sotcb) tcp_output((data), (sotcb))
436# define DO_TCP_INPUT(data, mbuf, size, so) tcp_input((data), (mbuf), (size), (so))
437# define DO_TCP_CONNECT(data, so) tcp_connect((data), (so))
438# define DO_SOREAD(ret, data, so, ifclose) \
439 do { \
440 (ret) = soread((data), (so), (ifclose)); \
441 } while(0)
442# define DO_SOWRITE(ret, data, so) \
443 do { \
444 (ret) = sowrite((data), (so)); \
445 } while(0)
446# define DO_SORECFROM(data, so) sorecvfrom((data), (so))
447# define SOLOOKUP(so, label, src, sport, dst, dport) \
448 do { \
449 (so) = solookup(&VBOX_X2(queue_ ## label ## _label), (src), (sport), (dst), (dport)); \
450 } while (0)
451# define DO_UDP_DETACH(data, so, ignored) udp_detach((data), (so))
452
453#endif
454
455#define TCP_OUTPUT(data, sotcb) DO_TCP_OUTPUT((data), (sotcb))
456#define TCP_INPUT(data, mbuf, size, so) DO_TCP_INPUT((data), (mbuf), (size), (so))
457#define TCP_CONNECT(data, so) DO_TCP_CONNECT((data), (so))
458#define SOREAD(ret, data, so, ifclose) DO_SOREAD((ret), (data), (so), (ifclose))
459#define SOWRITE(ret, data, so) DO_SOWRITE((ret), (data), (so))
460#define SORECVFROM(data, so) DO_SORECFROM((data), (so))
461#define UDP_DETACH(data, so, so_next) DO_UDP_DETACH((data), (so), (so_next))
462
463/* dnsproxy/dnsproxy.c */
464#define authoritative_port pData->authoritative_port
465#define authoritative_timeout pData->authoritative_timeout
466#define recursive_port pData->recursive_port
467#define recursive_timeout pData->recursive_timeout
468#define stats_timeout pData->stats_timeout
469/* dnsproxy/hash.c */
470#define dns_port pData->port
471#define request_hash pData->request_hash
472#define hash_collisions pData->hash_collisions
473#define active_queries pData->active_queries
474#define all_queries pData->all_queries
475#define authoritative_queries pData->authoritative_queries
476#define recursive_queries pData->recursive_queries
477#define removed_queries pData->removed_queries
478#define dropped_queries pData->dropped_queries
479#define answered_queries pData->answered_queries
480#define dropped_answers pData->dropped_answers
481#define late_answers pData->late_answers
482
483/* dnsproxy/dnsproxy.c */
484#define queryid pData->queryid
485#define authoritative_addr pData->authoritative_addr
486#define recursive_addr pData->recursive_addr
487#define sock_query pData->sock_query
488#define sock_answer pData->sock_answer
489
490#define instancehead pData->instancehead
491
492#define nmbclusters pData->nmbclusters
493#define nmbjumbop pData->nmbjumbop
494#define nmbjumbo9 pData->nmbjumbo9
495#define nmbjumbo16 pData->nmbjumbo16
496#define mbstat pData->mbstat
497#include "ext.h"
498#undef zone_mbuf
499#undef zone_clust
500#undef zone_pack
501#undef zone_jumbop
502#undef zone_jumbo9
503#undef zone_jumbo16
504#undef zone_ext_refcnt
505static inline uma_zone_t slirp_zone_pack(PNATState pData)
506{
507 return pData->zone_pack;
508}
509static inline uma_zone_t slirp_zone_jumbop(PNATState pData)
510{
511 return pData->zone_jumbop;
512}
513static inline uma_zone_t slirp_zone_jumbo9(PNATState pData)
514{
515 return pData->zone_jumbo9;
516}
517static inline uma_zone_t slirp_zone_jumbo16(PNATState pData)
518{
519 return pData->zone_jumbo16;
520}
521static inline uma_zone_t slirp_zone_ext_refcnt(PNATState pData)
522{
523 return pData->zone_ext_refcnt;
524}
525static inline uma_zone_t slirp_zone_mbuf(PNATState pData)
526{
527 return pData->zone_mbuf;
528}
529static inline uma_zone_t slirp_zone_clust(PNATState pData)
530{
531 return pData->zone_clust;
532}
533#ifndef VBOX_SLIRP_BSD
534# define m_adj(m, len) m_adj(pData, (m), (len))
535#endif
536
537#endif /* !___slirp_state_h */
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