VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/slirp.c@ 19748

Last change on this file since 19748 was 19689, checked in by vboxsync, 16 years ago

NAT: debug give slirp custom resolv.conf for debug need

  • Property svn:eol-style set to native
File size: 56.6 KB
Line 
1#include "slirp.h"
2#ifdef RT_OS_OS2
3# include <paths.h>
4#endif
5#ifdef VBOX_WITH_SLIRP_DNS_PROXY
6#include "dnsproxy/dnsproxy.h"
7#endif
8
9#include <VBox/err.h>
10#include <VBox/pdmdrv.h>
11#include <iprt/assert.h>
12#ifndef RT_OS_WINDOWS
13# include <sys/ioctl.h>
14# include <poll.h>
15#else
16# include <Winnls.h>
17# define _WINSOCK2API_
18# include <IPHlpApi.h>
19#endif
20
21#if !defined(RT_OS_WINDOWS)
22
23# define DO_ENGAGE_EVENT1(so, fdset, label) \
24 do { \
25 if( so->so_poll_index != -1 \
26 && so->s == polls[so->so_poll_index].fd) { \
27 polls[so->so_poll_index].events |= N_(fdset ## _poll); \
28 break; /* out of this loop */ \
29 } \
30 AssertRelease(poll_index < (nfds)); \
31 AssertRelease(poll_index >= 0 && poll_index < (nfds)); \
32 polls[poll_index].fd = (so)->s; \
33 (so)->so_poll_index = poll_index; \
34 polls[poll_index].events = N_(fdset ## _poll); \
35 polls[poll_index].revents = 0; \
36 poll_index++; \
37 } while(0)
38
39
40# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
41 do { \
42 if( so->so_poll_index != -1 \
43 && so->s == polls[so->so_poll_index].fd) { \
44 polls[so->so_poll_index].events |= \
45 N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
46 break; /* out of this loop */ \
47 } \
48 AssertRelease(poll_index < (nfds)); \
49 polls[poll_index].fd = (so)->s; \
50 (so)->so_poll_index = poll_index; \
51 polls[poll_index].events = \
52 N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
53 poll_index++; \
54 } while(0)
55
56# define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
57
58# define DO_CHECK_FD_SET(so, events, fdset) ( ((so)->so_poll_index != -1) \
59 && ((so)->so_poll_index <= ndfs) \
60 && ((so)->s == polls[so->so_poll_index].fd) \
61 && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)))
62# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
63# define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
64
65# ifndef RT_OS_WINDOWS
66
67# ifndef RT_OS_LINUX
68# define readfds_poll (POLLRDNORM)
69# define writefds_poll (POLLWRNORM)
70# define xfds_poll (POLLRDBAND|POLLWRBAND|POLLPRI)
71# else
72# define readfds_poll (POLLIN)
73# define writefds_poll (POLLOUT)
74# define xfds_poll (POLLPRI)
75# endif
76# define rderr_poll (POLLERR)
77# define rdhup_poll (POLLHUP)
78# define nval_poll (POLLNVAL)
79
80# define ICMP_ENGAGE_EVENT(so, fdset) \
81 do { \
82 if (pData->icmp_socket.s != -1) \
83 DO_ENGAGE_EVENT1((so), fdset, ICMP); \
84 } while (0)
85# else /* !RT_OS_WINDOWS */
86# define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
87# define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
88#endif /* RT_OS_WINDOWS */
89
90#else /* defined(RT_OS_WINDOWS) */
91
92/*
93 * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
94 * So no call to WSAEventSelect necessary.
95 */
96# define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
97
98# define DO_ENGAGE_EVENT1(so, fdset1, label) \
99 do { \
100 rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
101 if (rc == SOCKET_ERROR) \
102 { \
103 /* This should not happen */ \
104 error = WSAGetLastError(); \
105 LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
106 error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
107 } \
108 } while(0); \
109 CONTINUE(label)
110
111# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
112 DO_ENGAGE_EVENT1((so), (fdset1), label)
113
114# define DO_POLL_EVENTS(rc, error, so, events, label) \
115 (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
116 if ((rc) == SOCKET_ERROR) \
117 { \
118 (error) = WSAGetLastError(); \
119 LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
120 CONTINUE(label); \
121 }
122
123# define acceptds_win FD_ACCEPT
124# define acceptds_win_bit FD_ACCEPT_BIT
125
126# define readfds_win FD_READ
127# define readfds_win_bit FD_READ_BIT
128
129# define writefds_win FD_WRITE
130# define writefds_win_bit FD_WRITE_BIT
131
132# define xfds_win FD_OOB
133# define xfds_win_bit FD_OOB_BIT
134
135# define DO_CHECK_FD_SET(so, events, fdset) \
136 (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
137
138# define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
139# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 1 /*specific for Unix API */
140
141#endif /* defined(RT_OS_WINDOWS) */
142
143#define TCP_ENGAGE_EVENT1(so, fdset) \
144 DO_ENGAGE_EVENT1((so), fdset, tcp)
145
146#define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
147 DO_ENGAGE_EVENT2((so), fdset1, fdset2, tcp)
148
149#define UDP_ENGAGE_EVENT(so, fdset) \
150 DO_ENGAGE_EVENT1((so), fdset, udp)
151
152#define POLL_TCP_EVENTS(rc, error, so, events) \
153 DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
154
155#define POLL_UDP_EVENTS(rc, error, so, events) \
156 DO_POLL_EVENTS((rc), (error), (so), (events), udp)
157
158#define CHECK_FD_SET(so, events, set) \
159 (DO_CHECK_FD_SET((so), (events), set))
160
161#define WIN_CHECK_FD_SET(so, events, set) \
162 (DO_WIN_CHECK_FD_SET((so), (events), set))
163#define UNIX_CHECK_FD_SET(so, events, set) \
164 (DO_UNIX_CHECK_FD_SET(so, events, set))
165
166/*
167 * Loging macros
168 */
169#if VBOX_WITH_DEBUG_NAT_SOCKETS
170# if defined(RT_OS_WINDOWS)
171# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
172 do { \
173 LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
174 } while (0)
175# else /* RT_OS_WINDOWS */
176# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
177 do { \
178 LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
179 CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
180 CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
181 CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
182 CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
183 CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
184 CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
185 } while (0)
186# endif /* !RT_OS_WINDOWS */
187#else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
188# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
189#endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
190
191#define LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) DO_LOG_NAT_SOCK((so), proto, (winevent), r_fdset, w_fdset, x_fdset)
192
193static const uint8_t special_ethaddr[6] =
194{
195 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
196};
197
198static const uint8_t broadcast_ethaddr[6] =
199{
200 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
201};
202
203static const uint8_t zerro_ethaddr[6] =
204{
205 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
206};
207
208#ifdef RT_OS_WINDOWS
209# ifndef VBOX_WITH_MULTI_DNS
210static int get_dns_addr_domain(PNATState pData, bool fVerbose,
211 struct in_addr *pdns_addr,
212 const char **ppszDomain)
213{
214 int rc = 0;
215 FIXED_INFO *FixedInfo = NULL;
216 ULONG BufLen;
217 DWORD ret;
218 IP_ADDR_STRING *pIPAddr;
219 struct in_addr tmp_addr;
220
221 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
222 BufLen = sizeof(FIXED_INFO);
223
224 /** @todo: this API returns all DNS servers, no matter whether the
225 * corresponding network adapter is disabled or not. Maybe replace
226 * this by GetAdapterAddresses(), which is XP/Vista only though. */
227 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen))
228 {
229 if (FixedInfo)
230 {
231 GlobalFree(FixedInfo);
232 FixedInfo = NULL;
233 }
234 FixedInfo = GlobalAlloc(GPTR, BufLen);
235 }
236
237 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS)
238 {
239 Log(("GetNetworkParams failed. ret = %08x\n", (u_int)ret ));
240 if (FixedInfo)
241 {
242 GlobalFree(FixedInfo);
243 FixedInfo = NULL;
244 }
245 rc = -1;
246 goto get_dns_prefix;
247 }
248
249 pIPAddr = &(FixedInfo->DnsServerList);
250 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
251 Log(("nat: DNS Servers:\n"));
252 if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
253 LogRel(("NAT: DNS address: %s\n", pIPAddr->IpAddress.String));
254 *pdns_addr = tmp_addr;
255
256 pIPAddr = FixedInfo -> DnsServerList.Next;
257 while (pIPAddr)
258 {
259 if (fVerbose)
260 LogRel(("NAT: ignored DNS address: %s\n", pIPAddr ->IpAddress.String));
261 pIPAddr = pIPAddr ->Next;
262 }
263 if (FixedInfo)
264 {
265 GlobalFree(FixedInfo);
266 FixedInfo = NULL;
267 }
268
269get_dns_prefix:
270 if (ppszDomain)
271 {
272 OSVERSIONINFO ver;
273 char szDnsDomain[256];
274 DWORD dwSize = sizeof(szDnsDomain);
275
276 *ppszDomain = NULL;
277 GetVersionEx(&ver);
278 if (ver.dwMajorVersion >= 5)
279 {
280 /* GetComputerNameEx exists in Windows versions starting with 2000. */
281 if (GetComputerNameEx(ComputerNameDnsDomain, szDnsDomain, &dwSize))
282 {
283 if (szDnsDomain[0])
284 {
285 /* Just non-empty strings are valid. */
286 *ppszDomain = RTStrDup(szDnsDomain);
287 if (pData->fPassDomain)
288 {
289 if (fVerbose)
290 LogRel(("NAT: passing domain name %s\n", szDnsDomain));
291 }
292 else
293 Log(("nat: ignoring domain %s\n", szDnsDomain));
294 }
295 }
296 else
297 Log(("nat: GetComputerNameEx failed (%d)\n", GetLastError()));
298 }
299 }
300 return rc;
301}
302# else /* !VBOX_WITH_MULTI_DNS */
303static int get_dns_addr_domain(PNATState pData, bool fVerbose,
304 struct in_addr *pdns_addr,
305 const char **ppszDomain)
306{
307 /* Get amount of memory required for operation */
308 ULONG flags = GAA_FLAG_INCLUDE_PREFIX; /*GAA_FLAG_INCLUDE_ALL_INTERFACES;*/ /* all interfaces registered in NDIS */
309 PIP_ADAPTER_ADDRESSES addresses = NULL;
310 PIP_ADAPTER_ADDRESSES addr = NULL;
311 PIP_ADAPTER_DNS_SERVER_ADDRESS dns = NULL;
312 ULONG size = 0;
313 int wlen = 0;
314 char *suffix;
315 struct dns_entry *da = NULL;
316 struct dns_domain_entry *dd = NULL;
317 ULONG ret = ERROR_SUCCESS;
318
319 /* @todo add SKIPing flags to get only required information */
320
321 ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
322 if (ret != ERROR_BUFFER_OVERFLOW)
323 {
324 LogRel(("NAT: error %lu occured on capacity detection operation\n", ret));
325 return -1;
326 }
327
328 if (size == 0)
329 {
330 LogRel(("NAT: Win socket API returns non capacity\n"));
331 return -1;
332 }
333
334 addresses = RTMemAllocZ(size);
335 if (addresses == NULL)
336 {
337 LogRel(("NAT: No memory available \n"));
338 return -1;
339 }
340
341 ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
342 if (ret != ERROR_SUCCESS)
343 {
344 LogRel(("NAT: error %lu occured on fetching adapters info\n", ret));
345 RTMemFree(addresses);
346 return -1;
347 }
348 addr = addresses;
349 while(addr != NULL)
350 {
351 int found;
352 if (addr->OperStatus != IfOperStatusUp)
353 goto next;
354 dns = addr->FirstDnsServerAddress;
355 while (dns != NULL)
356 {
357 struct sockaddr *saddr = dns->Address.lpSockaddr;
358 if (saddr->sa_family != AF_INET)
359 goto next_dns;
360 /* add dns server to list */
361 da = RTMemAllocZ(sizeof(struct dns_entry));
362 if (da == NULL)
363 {
364 LogRel(("NAT: Can't allocate buffer for DNS entry\n"));
365 RTMemFree(addresses);
366 return VERR_NO_MEMORY;
367 }
368 LogRel(("NAT: adding %R[IP4] to DNS server list\n", &((struct sockaddr_in *)saddr)->sin_addr));
369 if ((((struct sockaddr_in *)saddr)->sin_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
370 da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
371 }
372 else
373 {
374 da->de_addr.s_addr = ((struct sockaddr_in *)saddr)->sin_addr.s_addr;
375 }
376 LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
377
378 if (addr->DnsSuffix == NULL)
379 goto next_dns;
380
381 /*uniq*/
382 RTUtf16ToUtf8(addr->DnsSuffix, &suffix);
383
384 if (!suffix || strlen(suffix) == 0) {
385 RTStrFree(suffix);
386 goto next_dns;
387 }
388
389 found = 0;
390 LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
391 {
392 if ( dd->dd_pszDomain != NULL
393 && strcmp(dd->dd_pszDomain, suffix) == 0)
394 {
395 found = 1;
396 RTStrFree(suffix);
397 break;
398 }
399 }
400 if (found == 0)
401 {
402 dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
403 if (dd == NULL)
404 {
405 LogRel(("NAT: not enough memory\n"));
406 RTStrFree(suffix);
407 RTMemFree(addresses);
408 return VERR_NO_MEMORY;
409 }
410 dd->dd_pszDomain = suffix;
411 LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
412 LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
413 }
414 next_dns:
415 dns = dns->Next;
416 }
417 next:
418 addr = addr->Next;
419 }
420 RTMemFree(addresses);
421 return 0;
422}
423# endif /* VBOX_WITH_MULTI_DNS */
424
425#else /* !RT_OS_WINDOWS */
426
427static int get_dns_addr_domain(PNATState pData, bool fVerbose,
428 struct in_addr *pdns_addr,
429 const char **ppszDomain)
430{
431 char buff[512];
432 char buff2[256];
433 FILE *f = NULL;
434 int found = 0;
435 struct in_addr tmp_addr;
436
437#ifdef RT_OS_OS2
438 /* Try various locations. */
439 char *etc = getenv("ETC");
440 if (etc)
441 {
442 snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
443 f = fopen(buff, "rt");
444 }
445 if (!f)
446 {
447 snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
448 f = fopen(buff, "rt");
449 }
450 if (!f)
451 {
452 snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
453 f = fopen(buff, "rt");
454 }
455#else
456#ifndef DEBUG_vvl
457 f = fopen("/etc/resolv.conf", "r");
458#else
459 char *home = getenv("HOME");
460 snprintf(buff, sizeof(buff), "%s/resolv.conf", home);
461 f = fopen(buff, "r");
462 if (f != NULL)
463 {
464 Log(("NAT: DNS we're using %s\n", buff));
465 }
466 else
467 {
468 f = fopen("/etc/resolv.conf", "r");
469 Log(("NAT: DNS we're using %s\n", buff));
470 }
471#endif
472#endif
473 if (!f)
474 return -1;
475
476 if (ppszDomain)
477 *ppszDomain = NULL;
478 Log(("nat: DNS Servers:\n"));
479 while (fgets(buff, 512, f) != NULL)
480 {
481#ifdef VBOX_WITH_MULTI_DNS
482 struct dns_entry *da = NULL;
483#endif
484 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
485 {
486 if (!inet_aton(buff2, &tmp_addr))
487 continue;
488#ifndef VBOX_WITH_MULTI_DNS
489 /* If it's the first one, set it to dns_addr */
490 if (!found)
491 {
492 if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
493 LogRel(("NAT: DNS address: %s\n", buff2));
494 *pdns_addr = tmp_addr;
495 }
496 else
497 {
498 if (fVerbose)
499 LogRel(("NAT: ignored DNS address: %s\n", buff2));
500 }
501#else
502 /*localhost mask */
503 da = RTMemAllocZ(sizeof (struct dns_entry));
504 if (da == NULL)
505 {
506 LogRel(("can't alloc memory for DNS entry\n"));
507 return -1;
508 }
509 /*check */
510 da->de_addr.s_addr = tmp_addr.s_addr;
511 if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
512 da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
513 }
514 LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
515#endif
516 found++;
517 }
518#ifndef VBOX_WITH_MULTI_DNS
519 if ( ppszDomain
520 && (!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
521 {
522 /* Domain name/search list present. Pick first entry */
523 if (*ppszDomain == NULL)
524 {
525 char *tok;
526 char *saveptr;
527 tok = strtok_r(&buff[6], " \t\n", &saveptr);
528 if (tok)
529 {
530 *ppszDomain = RTStrDup(tok);
531 if (pData->fPassDomain)
532 {
533 if (fVerbose)
534 LogRel(("NAT: passing domain name %s\n", tok));
535 }
536 else
537 Log(("nat: ignoring domain %s\n", tok));
538 }
539 }
540 }
541#else
542 if ((!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
543 {
544 char *tok;
545 char *saveptr;
546 struct dns_domain_entry *dd = NULL;
547 int found = 0;
548 tok = strtok_r(&buff[6], " \t\n", &saveptr);
549 LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
550 {
551 if( tok != NULL
552 && strcmp(tok, dd->dd_pszDomain) == 0)
553 {
554 found = 1;
555 break;
556 }
557 }
558 if (tok != NULL && found == 0) {
559 dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
560 if (dd == NULL)
561 {
562 LogRel(("NAT: not enought memory to add domain list\n"));
563 return VERR_NO_MEMORY;
564 }
565 dd->dd_pszDomain = RTStrDup(tok);
566 LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
567 LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
568 }
569 }
570#endif
571 }
572 fclose(f);
573 if (!found)
574 return -1;
575 return 0;
576}
577
578#endif
579#ifdef VBOX_WITH_MULTI_DNS
580static int slirp_init_dns_list(PNATState pData)
581{
582 LIST_INIT(&pData->dns_list_head);
583 LIST_INIT(&pData->dns_domain_list_head);
584 return get_dns_addr_domain(pData, true, NULL, NULL);
585}
586
587static void slirp_release_dns_list(PNATState pData)
588{
589 struct dns_entry *de = NULL;
590 struct dns_domain_entry *dd = NULL;
591 while(!LIST_EMPTY(&pData->dns_domain_list_head)) {
592 dd = LIST_FIRST(&pData->dns_domain_list_head);
593 LIST_REMOVE(dd, dd_list);
594 if (dd->dd_pszDomain != NULL)
595 RTStrFree(dd->dd_pszDomain);
596 RTMemFree(dd);
597 }
598 while(!LIST_EMPTY(&pData->dns_domain_list_head)) {
599 dd = LIST_FIRST(&pData->dns_domain_list_head);
600 LIST_REMOVE(dd, dd_list);
601 if (dd->dd_pszDomain != NULL)
602 RTStrFree(dd->dd_pszDomain);
603 RTMemFree(dd);
604 }
605}
606#endif
607
608int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
609{
610 return get_dns_addr_domain(pData, false, pdns_addr, NULL);
611}
612
613#ifndef VBOX_WITH_NAT_SERVICE
614int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
615 bool fPassDomain, void *pvUser)
616#else
617int slirp_init(PNATState *ppData, uint32_t u32NetAddr, uint32_t u32Netmask,
618 bool fPassDomain, void *pvUser)
619#endif
620{
621 int fNATfailed = 0;
622 int rc;
623 PNATState pData = RTMemAllocZ(sizeof(NATState));
624 *ppData = pData;
625 if (!pData)
626 return VERR_NO_MEMORY;
627 if (u32Netmask & 0x1f)
628 /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
629 return VERR_INVALID_PARAMETER;
630 pData->fPassDomain = fPassDomain;
631 pData->pvUser = pvUser;
632 pData->netmask = u32Netmask;
633
634#ifdef RT_OS_WINDOWS
635 {
636 WSADATA Data;
637 WSAStartup(MAKEWORD(2, 0), &Data);
638 }
639 pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
640#endif
641#ifdef VBOX_WITH_SLIRP_MT
642 QSOCKET_LOCK_CREATE(tcb);
643 QSOCKET_LOCK_CREATE(udb);
644 rc = RTReqCreateQueue(&pData->pReqQueue);
645 AssertReleaseRC(rc);
646#endif
647
648 link_up = 1;
649
650 debug_init();
651 if_init(pData);
652 ip_init(pData);
653 icmp_init(pData);
654
655 /* Initialise mbufs *after* setting the MTU */
656 m_init(pData);
657
658#ifndef VBOX_WITH_NAT_SERVICE
659 inet_aton(pszNetAddr, &special_addr);
660#else
661 special_addr.s_addr = u32NetAddr;
662#endif
663 alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
664 /* @todo: add ability to configure this staff */
665
666 /* set default addresses */
667 inet_aton("127.0.0.1", &loopback_addr);
668#ifndef VBOX_WITH_MULTI_DNS
669 inet_aton("127.0.0.1", &dns_addr);
670
671 if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
672#else
673 if (slirp_init_dns_list(pData) < 0)
674#endif
675 fNATfailed = 1;
676#ifdef VBOX_WITH_SLIRP_DNS_PROXY
677 dnsproxy_init(pData);
678#endif
679
680 getouraddr(pData);
681 return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
682}
683
684/**
685 * Statistics counters.
686 */
687void slirp_register_timers(PNATState pData, PPDMDRVINS pDrvIns)
688{
689#ifdef VBOX_WITH_STATISTICS
690 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFill, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
691 STAMUNIT_TICKS_PER_CALL, "Profiling slirp fills", "/Drivers/NAT%d/Fill", pDrvIns->iInstance);
692 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPoll, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
693 STAMUNIT_TICKS_PER_CALL, "Profiling slirp polls", "/Drivers/NAT%d/Poll", pDrvIns->iInstance);
694 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFastTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
695 STAMUNIT_TICKS_PER_CALL, "Profiling slirp fast timer", "/Drivers/NAT%d/TimerFast", pDrvIns->iInstance);
696 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatSlowTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
697 STAMUNIT_TICKS_PER_CALL, "Profiling slirp slow timer", "/Drivers/NAT%d/TimerSlow", pDrvIns->iInstance);
698 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
699 STAMUNIT_COUNT, "TCP sockets", "/Drivers/NAT%d/SockTCP", pDrvIns->iInstance);
700 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
701 STAMUNIT_COUNT, "TCP sockets active", "/Drivers/NAT%d/SockTCPHot", pDrvIns->iInstance);
702 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
703 STAMUNIT_COUNT, "UDP sockets", "/Drivers/NAT%d/SockUDP", pDrvIns->iInstance);
704 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
705 STAMUNIT_COUNT, "UDP sockets active", "/Drivers/NAT%d/SockUDPHot", pDrvIns->iInstance);
706#endif /* VBOX_WITH_STATISTICS */
707}
708
709/**
710 * Marks the link as up, making it possible to establish new connections.
711 */
712void slirp_link_up(PNATState pData)
713{
714 link_up = 1;
715}
716
717/**
718 * Marks the link as down and cleans up the current connections.
719 */
720void slirp_link_down(PNATState pData)
721{
722 struct socket *so;
723
724 while ((so = tcb.so_next) != &tcb)
725 {
726 if (so->so_state & SS_NOFDREF || so->s == -1)
727 sofree(pData, so);
728 else
729 tcp_drop(pData, sototcpcb(so), 0);
730 }
731
732 while ((so = udb.so_next) != &udb)
733 udp_detach(pData, so);
734
735 link_up = 0;
736}
737
738/**
739 * Terminates the slirp component.
740 */
741void slirp_term(PNATState pData)
742{
743#ifndef VBOX_WITH_MULTI_DNS
744 if (pData->pszDomain)
745 RTStrFree((char *)(void *)pData->pszDomain);
746#endif
747
748#ifdef RT_OS_WINDOWS
749 pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
750 FreeLibrary(pData->hmIcmpLibrary);
751 RTMemFree(pData->pvIcmpBuffer);
752# else
753 closesocket(pData->icmp_socket.s);
754#endif
755
756 slirp_link_down(pData);
757#ifdef VBOX_WITH_MULTI_DNS
758 slirp_release_dns_list(pData);
759#endif
760#ifdef RT_OS_WINDOWS
761 WSACleanup();
762#endif
763#ifdef LOG_ENABLED
764 Log(("\n"
765 "NAT statistics\n"
766 "--------------\n"
767 "\n"));
768 ipstats(pData);
769 tcpstats(pData);
770 udpstats(pData);
771 icmpstats(pData);
772 mbufstats(pData);
773 sockstats(pData);
774 Log(("\n"
775 "\n"
776 "\n"));
777#endif
778 RTMemFree(pData);
779}
780
781
782#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
783#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
784#define UPD_NFDS(x) do { if (nfds < (x)) nfds = (x); } while (0)
785
786/*
787 * curtime kept to an accuracy of 1ms
788 */
789static void updtime(PNATState pData)
790{
791#ifdef RT_OS_WINDOWS
792 struct _timeb tb;
793
794 _ftime(&tb);
795 curtime = (u_int)tb.time * (u_int)1000;
796 curtime += (u_int)tb.millitm;
797#else
798 gettimeofday(&tt, 0);
799
800 curtime = (u_int)tt.tv_sec * (u_int)1000;
801 curtime += (u_int)tt.tv_usec / (u_int)1000;
802
803 if ((tt.tv_usec % 1000) >= 500)
804 curtime++;
805#endif
806}
807
808#ifdef RT_OS_WINDOWS
809void slirp_select_fill(PNATState pData, int *pnfds)
810#else /* RT_OS_WINDOWS */
811void slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
812#endif /* !RT_OS_WINDOWS */
813{
814 struct socket *so, *so_next;
815 int nfds;
816#if defined(RT_OS_WINDOWS)
817 int rc;
818 int error;
819#else
820 int poll_index = 0;
821#endif
822 int i;
823
824 STAM_PROFILE_START(&pData->StatFill, a);
825
826 nfds = *pnfds;
827
828 /*
829 * First, TCP sockets
830 */
831 do_slowtimo = 0;
832 if (link_up)
833 {
834 /*
835 * *_slowtimo needs calling if there are IP fragments
836 * in the fragment queue, or there are TCP connections active
837 */
838 /* XXX:
839 * triggering of fragment expiration should be the same but use new macroses
840 */
841 do_slowtimo = (tcb.so_next != &tcb);
842 if (!do_slowtimo)
843 {
844 for (i = 0; i < IPREASS_NHASH; i++)
845 {
846 if (!TAILQ_EMPTY(&ipq[i]))
847 {
848 do_slowtimo = 1;
849 break;
850 }
851 }
852 }
853 ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
854
855 STAM_COUNTER_RESET(&pData->StatTCP);
856 STAM_COUNTER_RESET(&pData->StatTCPHot);
857
858 QSOCKET_FOREACH(so, so_next, tcp)
859 /* { */
860#if !defined(RT_OS_WINDOWS)
861 so->so_poll_index = -1;
862#endif
863 STAM_COUNTER_INC(&pData->StatTCP);
864
865 /*
866 * See if we need a tcp_fasttimo
867 */
868 if ( time_fasttimo == 0
869 && so->so_tcpcb != NULL
870 && so->so_tcpcb->t_flags & TF_DELACK)
871 time_fasttimo = curtime; /* Flag when we want a fasttimo */
872
873 /*
874 * NOFDREF can include still connecting to local-host,
875 * newly socreated() sockets etc. Don't want to select these.
876 */
877 if (so->so_state & SS_NOFDREF || so->s == -1)
878 CONTINUE(tcp);
879
880 /*
881 * Set for reading sockets which are accepting
882 */
883 if (so->so_state & SS_FACCEPTCONN)
884 {
885 STAM_COUNTER_INC(&pData->StatTCPHot);
886 TCP_ENGAGE_EVENT1(so, readfds);
887 CONTINUE(tcp);
888 }
889
890 /*
891 * Set for writing sockets which are connecting
892 */
893 if (so->so_state & SS_ISFCONNECTING)
894 {
895 Log2(("connecting %R[natsock] engaged\n",so));
896 STAM_COUNTER_INC(&pData->StatTCPHot);
897 TCP_ENGAGE_EVENT1(so, writefds);
898 }
899
900 /*
901 * Set for writing if we are connected, can send more, and
902 * we have something to send
903 */
904 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
905 {
906 STAM_COUNTER_INC(&pData->StatTCPHot);
907 TCP_ENGAGE_EVENT1(so, writefds);
908 }
909
910 /*
911 * Set for reading (and urgent data) if we are connected, can
912 * receive more, and we have room for it XXX /2 ?
913 */
914 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
915 {
916 STAM_COUNTER_INC(&pData->StatTCPHot);
917 TCP_ENGAGE_EVENT2(so, readfds, xfds);
918 }
919 LOOP_LABEL(tcp, so, so_next);
920 }
921
922 /*
923 * UDP sockets
924 */
925 STAM_COUNTER_RESET(&pData->StatUDP);
926 STAM_COUNTER_RESET(&pData->StatUDPHot);
927
928 QSOCKET_FOREACH(so, so_next, udp)
929 /* { */
930
931 STAM_COUNTER_INC(&pData->StatUDP);
932#if !defined(RT_OS_WINDOWS)
933 so->so_poll_index = -1;
934#endif
935
936 /*
937 * See if it's timed out
938 */
939 if (so->so_expire)
940 {
941 if (so->so_expire <= curtime)
942 {
943#ifdef VBOX_WITH_SLIRP_DNS_PROXY
944 Log2(("NAT: %R[natsock] expired\n", so));
945 if (so->so_timeout != NULL)
946 {
947 so->so_timeout(pData, so, so->so_timeout_arg);
948 }
949#endif
950#ifdef VBOX_WITH_SLIRP_MT
951 /* we need so_next for continue our cycle*/
952 so_next = so->so_next;
953#endif
954 UDP_DETACH(pData, so, so_next);
955 CONTINUE_NO_UNLOCK(udp);
956 }
957 else
958 do_slowtimo = 1; /* Let socket expire */
959 }
960
961 /*
962 * When UDP packets are received from over the link, they're
963 * sendto()'d straight away, so no need for setting for writing
964 * Limit the number of packets queued by this session to 4.
965 * Note that even though we try and limit this to 4 packets,
966 * the session could have more queued if the packets needed
967 * to be fragmented.
968 *
969 * (XXX <= 4 ?)
970 */
971 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
972 {
973 STAM_COUNTER_INC(&pData->StatUDPHot);
974 UDP_ENGAGE_EVENT(so, readfds);
975 }
976 LOOP_LABEL(udp, so, so_next);
977 }
978
979 }
980
981#if defined(RT_OS_WINDOWS)
982 *pnfds = VBOX_EVENT_COUNT;
983#else /* RT_OS_WINDOWS */
984 AssertRelease(poll_index <= *pnfds);
985 *pnfds = poll_index;
986#endif /* !RT_OS_WINDOWS */
987
988 STAM_PROFILE_STOP(&pData->StatFill, a);
989}
990
991#if defined(RT_OS_WINDOWS)
992void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
993#else /* RT_OS_WINDOWS */
994void slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
995#endif /* !RT_OS_WINDOWS */
996{
997 struct socket *so, *so_next;
998 int ret;
999#if defined(RT_OS_WINDOWS)
1000 WSANETWORKEVENTS NetworkEvents;
1001 int rc;
1002 int error;
1003#else
1004 int poll_index = 0;
1005#endif
1006
1007 STAM_PROFILE_START(&pData->StatPoll, a);
1008
1009 /* Update time */
1010 updtime(pData);
1011
1012 /*
1013 * See if anything has timed out
1014 */
1015 if (link_up)
1016 {
1017 if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
1018 {
1019 STAM_PROFILE_START(&pData->StatFastTimer, a);
1020 tcp_fasttimo(pData);
1021 time_fasttimo = 0;
1022 STAM_PROFILE_STOP(&pData->StatFastTimer, a);
1023 }
1024 if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
1025 {
1026 STAM_PROFILE_START(&pData->StatSlowTimer, a);
1027 ip_slowtimo(pData);
1028 tcp_slowtimo(pData);
1029 last_slowtimo = curtime;
1030 STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
1031 }
1032 }
1033#if defined(RT_OS_WINDOWS)
1034 if (fTimeout)
1035 return; /* only timer update */
1036#endif
1037
1038 /*
1039 * Check sockets
1040 */
1041 if (link_up)
1042 {
1043#if defined(RT_OS_WINDOWS)
1044 /*XXX: before renaming please make see define
1045 * fIcmp in slirp_state.h
1046 */
1047 if (fIcmp)
1048 sorecvfrom(pData, &pData->icmp_socket);
1049#else
1050 if ( (pData->icmp_socket.s != -1)
1051 && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
1052 sorecvfrom(pData, &pData->icmp_socket);
1053#endif
1054 /*
1055 * Check TCP sockets
1056 */
1057 QSOCKET_FOREACH(so, so_next, tcp)
1058 /* { */
1059
1060#ifdef VBOX_WITH_SLIRP_MT
1061 if ( so->so_state & SS_NOFDREF
1062 && so->so_deleted == 1)
1063 {
1064 struct socket *son, *sop = NULL;
1065 QSOCKET_LOCK(tcb);
1066 if (so->so_next != NULL)
1067 {
1068 if (so->so_next != &tcb)
1069 SOCKET_LOCK(so->so_next);
1070 son = so->so_next;
1071 }
1072 if ( so->so_prev != &tcb
1073 && so->so_prev != NULL)
1074 {
1075 SOCKET_LOCK(so->so_prev);
1076 sop = so->so_prev;
1077 }
1078 QSOCKET_UNLOCK(tcb);
1079 remque(pData, so);
1080 NSOCK_DEC();
1081 SOCKET_UNLOCK(so);
1082 SOCKET_LOCK_DESTROY(so);
1083 RTMemFree(so);
1084 so_next = son;
1085 if (sop != NULL)
1086 SOCKET_UNLOCK(sop);
1087 CONTINUE_NO_UNLOCK(tcp);
1088 }
1089#endif
1090 /*
1091 * FD_ISSET is meaningless on these sockets
1092 * (and they can crash the program)
1093 */
1094 if (so->so_state & SS_NOFDREF || so->s == -1)
1095 CONTINUE(tcp);
1096
1097 POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
1098
1099 LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
1100
1101
1102 /*
1103 * Check for URG data
1104 * This will soread as well, so no need to
1105 * test for readfds below if this succeeds
1106 */
1107
1108 /* out-of-band data */
1109 if (CHECK_FD_SET(so, NetworkEvents, xfds))
1110 {
1111 sorecvoob(pData, so);
1112 }
1113
1114 /*
1115 * Check sockets for reading
1116 */
1117 else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
1118 || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
1119 {
1120 /*
1121 * Check for incoming connections
1122 */
1123 if (so->so_state & SS_FACCEPTCONN)
1124 {
1125 TCP_CONNECT(pData, so);
1126#if defined(RT_OS_WINDOWS)
1127 if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
1128#endif
1129 CONTINUE(tcp);
1130 }
1131
1132 ret = soread(pData, so);
1133 /* Output it if we read something */
1134 if (ret > 0)
1135 TCP_OUTPUT(pData, sototcpcb(so));
1136 }
1137
1138#if defined(RT_OS_WINDOWS)
1139 /*
1140 * Check for FD_CLOSE events.
1141 * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
1142 */
1143 if ( (NetworkEvents.lNetworkEvents & FD_CLOSE)
1144 || (so->so_close == 1))
1145 {
1146 so->so_close = 1; /* mark it */
1147 /*
1148 * drain the socket
1149 */
1150 for (;;)
1151 {
1152 ret = soread(pData, so);
1153 if (ret > 0)
1154 TCP_OUTPUT(pData, sototcpcb(so));
1155 else
1156 break;
1157 }
1158 CONTINUE(tcp);
1159 }
1160#endif
1161
1162 /*
1163 * Check sockets for writing
1164 */
1165 if (CHECK_FD_SET(so, NetworkEvents, writefds))
1166 {
1167 /*
1168 * Check for non-blocking, still-connecting sockets
1169 */
1170 if (so->so_state & SS_ISFCONNECTING)
1171 {
1172 Log2(("connecting %R[natsock] catched\n", so));
1173 /* Connected */
1174 so->so_state &= ~SS_ISFCONNECTING;
1175
1176 /*
1177 * This should be probably guarded by PROBE_CONN too. Anyway,
1178 * we disable it on OS/2 because the below send call returns
1179 * EFAULT which causes the opened TCP socket to close right
1180 * after it has been opened and connected.
1181 */
1182#ifndef RT_OS_OS2
1183 ret = send(so->s, (const char *)&ret, 0, 0);
1184 if (ret < 0)
1185 {
1186 /* XXXXX Must fix, zero bytes is a NOP */
1187 if ( errno == EAGAIN
1188 || errno == EWOULDBLOCK
1189 || errno == EINPROGRESS
1190 || errno == ENOTCONN)
1191 CONTINUE(tcp);
1192
1193 /* else failed */
1194 so->so_state = SS_NOFDREF;
1195 }
1196 /* else so->so_state &= ~SS_ISFCONNECTING; */
1197#endif
1198
1199 /*
1200 * Continue tcp_input
1201 */
1202 TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
1203 /* continue; */
1204 }
1205 else
1206 SOWRITE(ret, pData, so);
1207 /*
1208 * XXX If we wrote something (a lot), there could be the need
1209 * for a window update. In the worst case, the remote will send
1210 * a window probe to get things going again.
1211 */
1212 }
1213
1214 /*
1215 * Probe a still-connecting, non-blocking socket
1216 * to check if it's still alive
1217 */
1218#ifdef PROBE_CONN
1219 if (so->so_state & SS_ISFCONNECTING)
1220 {
1221 ret = recv(so->s, (char *)&ret, 0, 0);
1222
1223 if (ret < 0)
1224 {
1225 /* XXX */
1226 if ( errno == EAGAIN
1227 || errno == EWOULDBLOCK
1228 || errno == EINPROGRESS
1229 || errno == ENOTCONN)
1230 {
1231 CONTINUE(tcp); /* Still connecting, continue */
1232 }
1233
1234 /* else failed */
1235 so->so_state = SS_NOFDREF;
1236
1237 /* tcp_input will take care of it */
1238 }
1239 else
1240 {
1241 ret = send(so->s, &ret, 0, 0);
1242 if (ret < 0)
1243 {
1244 /* XXX */
1245 if ( errno == EAGAIN
1246 || errno == EWOULDBLOCK
1247 || errno == EINPROGRESS
1248 || errno == ENOTCONN)
1249 {
1250 CONTINUE(tcp);
1251 }
1252 /* else failed */
1253 so->so_state = SS_NOFDREF;
1254 }
1255 else
1256 so->so_state &= ~SS_ISFCONNECTING;
1257
1258 }
1259 TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
1260 } /* SS_ISFCONNECTING */
1261#endif
1262#ifndef RT_OS_WINDOWS
1263 if ( UNIX_CHECK_FD_SET(so, NetworkEvents, rdhup)
1264 || UNIX_CHECK_FD_SET(so, NetworkEvents, rderr))
1265 {
1266 int err;
1267 int inq, outq;
1268 int status;
1269 socklen_t optlen = sizeof(int);
1270 inq = outq = 0;
1271 status = getsockopt(so->s, SOL_SOCKET, SO_ERROR, &err, &optlen);
1272 if (status != 0)
1273 Log(("NAT: can't get error status from %R[natsock]\n", so));
1274#ifndef RT_OS_SOLARIS
1275 status = ioctl(so->s, FIONREAD, &inq); /* tcp(7) recommends SIOCINQ which is Linux specific */
1276 if (status != 0 || status != EINVAL)
1277 {
1278 /* EINVAL returned if socket in listen state tcp(7)*/
1279 Log(("NAT: can't get depth of IN queue status from %R[natsock]\n", so));
1280 }
1281 status = ioctl(so->s, TIOCOUTQ, &outq); /* SIOCOUTQ see previous comment */
1282 if (status != 0)
1283 Log(("NAT: can't get depth of OUT queue from %R[natsock]\n", so));
1284#else
1285 /*
1286 * Solaris has bit different ioctl commands and its handlings
1287 * hint: streamio(7) I_NREAD
1288 */
1289#endif
1290 if ( so->so_state & SS_ISFCONNECTING
1291 || UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
1292 {
1293 /**
1294 * Check if we need here take care about gracefull connection
1295 * @todo try with proxy server
1296 */
1297 if (UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
1298 {
1299 /*
1300 * Never meet inq != 0 or outq != 0, anyway let it stay for a while
1301 * in case it happens we'll able to detect it.
1302 * Give TCP/IP stack wait or expire the socket.
1303 */
1304 Log(("NAT: %R[natsock] err(%d:%s) s(in:%d,out:%d)happens on read I/O, "
1305 "other side close connection \n", so, err, strerror(err), inq, outq));
1306 CONTINUE(tcp);
1307 }
1308 goto tcp_input_close;
1309 }
1310 if ( !UNIX_CHECK_FD_SET(so, NetworkEvents, readfds)
1311 && !UNIX_CHECK_FD_SET(so, NetworkEvents, writefds)
1312 && !UNIX_CHECK_FD_SET(so, NetworkEvents, xfds))
1313 {
1314 Log(("NAT: system expires the socket %R[natsock] err(%d:%s) s(in:%d,out:%d) happens on non-I/O. ",
1315 so, err, strerror(err), inq, outq));
1316 goto tcp_input_close;
1317 }
1318 Log(("NAT: %R[natsock] we've met(%d:%s) s(in:%d, out:%d) unhandled combination hup (%d) "
1319 "rederr(%d) on (r:%d, w:%d, x:%d)\n",
1320 so, err, strerror(err),
1321 inq, outq,
1322 UNIX_CHECK_FD_SET(so, ign, rdhup),
1323 UNIX_CHECK_FD_SET(so, ign, rderr),
1324 UNIX_CHECK_FD_SET(so, ign, readfds),
1325 UNIX_CHECK_FD_SET(so, ign, writefds),
1326 UNIX_CHECK_FD_SET(so, ign, xfds)));
1327 /*
1328 * Give OS's TCP/IP stack a chance to resolve an issue or expire the socket.
1329 */
1330 CONTINUE(tcp);
1331tcp_input_close:
1332 so->so_state = SS_NOFDREF; /*cause connection valid tcp connection termination and socket closing */
1333 TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
1334 CONTINUE(tcp);
1335 }
1336#endif
1337 LOOP_LABEL(tcp, so, so_next);
1338 }
1339
1340 /*
1341 * Now UDP sockets.
1342 * Incoming packets are sent straight away, they're not buffered.
1343 * Incoming UDP data isn't buffered either.
1344 */
1345 QSOCKET_FOREACH(so, so_next, udp)
1346 /* { */
1347#ifdef VBOX_WITH_SLIRP_MT
1348 if ( so->so_state & SS_NOFDREF
1349 && so->so_deleted == 1)
1350 {
1351 struct socket *son, *sop = NULL;
1352 QSOCKET_LOCK(udb);
1353 if (so->so_next != NULL)
1354 {
1355 if (so->so_next != &udb)
1356 SOCKET_LOCK(so->so_next);
1357 son = so->so_next;
1358 }
1359 if ( so->so_prev != &udb
1360 && so->so_prev != NULL)
1361 {
1362 SOCKET_LOCK(so->so_prev);
1363 sop = so->so_prev;
1364 }
1365 QSOCKET_UNLOCK(udb);
1366 remque(pData, so);
1367 NSOCK_DEC();
1368 SOCKET_UNLOCK(so);
1369 SOCKET_LOCK_DESTROY(so);
1370 RTMemFree(so);
1371 so_next = son;
1372 if (sop != NULL)
1373 SOCKET_UNLOCK(sop);
1374 CONTINUE_NO_UNLOCK(udp);
1375 }
1376#endif
1377 POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
1378
1379 LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
1380
1381 if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
1382 {
1383 SORECVFROM(pData, so);
1384 }
1385 LOOP_LABEL(udp, so, so_next);
1386 }
1387
1388 }
1389
1390#ifndef VBOX_WITH_SLIRP_MT
1391 /*
1392 * See if we can start outputting
1393 */
1394 if (if_queued && link_up)
1395 if_start(pData);
1396#endif
1397
1398 STAM_PROFILE_STOP(&pData->StatPoll, a);
1399}
1400
1401#ifndef VBOX_WITH_NAT_SERVICE
1402#define ETH_ALEN 6
1403#define ETH_HLEN 14
1404
1405#define ARPOP_REQUEST 1 /* ARP request */
1406#define ARPOP_REPLY 2 /* ARP reply */
1407
1408struct ethhdr
1409{
1410 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
1411 unsigned char h_source[ETH_ALEN]; /* source ether addr */
1412 unsigned short h_proto; /* packet type ID field */
1413};
1414AssertCompileSize(struct ethhdr, 14);
1415#endif
1416
1417struct arphdr
1418{
1419 unsigned short ar_hrd; /* format of hardware address */
1420 unsigned short ar_pro; /* format of protocol address */
1421 unsigned char ar_hln; /* length of hardware address */
1422 unsigned char ar_pln; /* length of protocol address */
1423 unsigned short ar_op; /* ARP opcode (command) */
1424
1425 /*
1426 * Ethernet looks like this : This bit is variable sized however...
1427 */
1428 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
1429 unsigned char ar_sip[4]; /* sender IP address */
1430 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
1431 unsigned char ar_tip[4]; /* target IP address */
1432};
1433AssertCompileSize(struct arphdr, 28);
1434
1435static void arp_input(PNATState pData, struct mbuf *m)
1436{
1437 struct ethhdr *eh;
1438 struct ethhdr *reh;
1439 struct arphdr *ah;
1440 struct arphdr *rah;
1441 int ar_op;
1442 struct ex_list *ex_ptr;
1443 uint32_t htip;
1444 uint32_t tip;
1445 struct mbuf *mr;
1446 eh = mtod(m, struct ethhdr *);
1447 ah = (struct arphdr *)&eh[1];
1448 htip = ntohl(*(uint32_t*)ah->ar_tip);
1449 tip = *(uint32_t*)ah->ar_tip;
1450
1451 mr = m_get(pData);
1452#ifdef VBOX_WITH_NAT_SERVICE
1453 reh = mtod(mr, struct ethhdr *);
1454 memcpy(reh->h_source, eh->h_source, ETH_ALEN); /* XXX: if_encap will swap src and dst*/
1455 Log4(("NAT: arp:[%hhx:%hhx:%hhx:%hhx:%hhx:%hhx]->[%hhx:%hhx:%hhx:%hhx:%hhx:%hhx]\n",
1456 reh->h_source[0], reh->h_source[1], reh->h_source[2], reh->h_source[3], reh->h_source[4], reh->h_source[5],
1457 reh->h_dest[0], reh->h_dest[1], reh->h_dest[2], reh->h_dest[3], reh->h_dest[4], reh->h_dest[5]));
1458 Log4(("NAT: arp: %R[IP4]\n", &tip));
1459#endif
1460 mr->m_data += if_maxlinkhdr;
1461 mr->m_len = sizeof(struct arphdr);
1462 rah = mtod(mr, struct arphdr *);
1463
1464 ar_op = ntohs(ah->ar_op);
1465 switch(ar_op)
1466 {
1467 case ARPOP_REQUEST:
1468#ifdef VBOX_WITH_NAT_SERVICE
1469 if (tip == special_addr.s_addr) goto arp_ok;
1470#endif
1471 if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
1472 {
1473 if ( CTL_CHECK(htip, CTL_DNS)
1474 || CTL_CHECK(htip, CTL_ALIAS)
1475 || CTL_CHECK(htip, CTL_TFTP))
1476 goto arp_ok;
1477 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1478 {
1479 if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
1480 {
1481 goto arp_ok;
1482 }
1483 }
1484 return;
1485 arp_ok:
1486 rah->ar_hrd = htons(1);
1487 rah->ar_pro = htons(ETH_P_IP);
1488 rah->ar_hln = ETH_ALEN;
1489 rah->ar_pln = 4;
1490 rah->ar_op = htons(ARPOP_REPLY);
1491 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
1492
1493 switch (htip & ~pData->netmask)
1494 {
1495 case CTL_DNS:
1496 case CTL_ALIAS:
1497 rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
1498 break;
1499 default:;
1500 }
1501
1502 memcpy(rah->ar_sip, ah->ar_tip, 4);
1503 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
1504 memcpy(rah->ar_tip, ah->ar_sip, 4);
1505 if_encap(pData, ETH_P_ARP, mr);
1506 m_free(pData, m);
1507 }
1508 break;
1509 default:
1510 break;
1511 }
1512}
1513
1514void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
1515{
1516 struct mbuf *m;
1517 int proto;
1518 static bool fWarnedIpv6;
1519 struct ethhdr *eh = (struct ethhdr*)pkt;
1520
1521 if (pkt_len < ETH_HLEN)
1522 {
1523 LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
1524 return;
1525 }
1526 Log4(("NAT: in:[%hhx:%hhx:%hhx:%hhx:%hhx:%hhx]->[%hhx:%hhx:%hhx:%hhx:%hhx:%hhx]\n",
1527 eh->h_source[0], eh->h_source[1], eh->h_source[2], eh->h_source[3], eh->h_source[4], eh->h_source[5],
1528 eh->h_dest[0], eh->h_dest[1], eh->h_dest[2], eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]));
1529#ifdef VBOX_WITH_NAT_SERVICE
1530 if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) == 0)
1531 {
1532 /* @todo vasily: add ether logging routine in debug.c */
1533 Log(("NAT: packet was addressed to other MAC\n"));
1534 RTMemFree((void *)pkt);
1535 return;
1536 }
1537#endif
1538
1539 m = m_get(pData);
1540 if (!m)
1541 {
1542 LogRel(("NAT: can't allocate new mbuf\n"));
1543 return;
1544 }
1545
1546 /* Note: we add to align the IP header */
1547
1548 if (M_FREEROOM(m) < pkt_len)
1549 m_inc(m, pkt_len);
1550
1551 m->m_len = pkt_len ;
1552 memcpy(m->m_data, pkt, pkt_len);
1553
1554 proto = ntohs(*(uint16_t *)(pkt + 12));
1555 switch(proto)
1556 {
1557 case ETH_P_ARP:
1558 arp_input(pData, m);
1559 break;
1560 case ETH_P_IP:
1561 /* Update time. Important if the network is very quiet, as otherwise
1562 * the first outgoing connection gets an incorrect timestamp. */
1563 updtime(pData);
1564 m->m_data += ETH_HLEN;
1565 m->m_len -= ETH_HLEN;
1566 ip_input(pData, m);
1567 break;
1568 case ETH_P_IPV6:
1569 m_free(pData, m);
1570 if (!fWarnedIpv6)
1571 {
1572 LogRel(("NAT: IPv6 not supported\n"));
1573 fWarnedIpv6 = true;
1574 }
1575 break;
1576 default:
1577 LogRel(("NAT: Unsupported protocol %x\n", proto));
1578 m_free(pData, m);
1579 break;
1580 }
1581 RTMemFree((void *)pkt);
1582}
1583
1584/* output the IP packet to the ethernet device */
1585void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
1586{
1587 struct ethhdr *eh;
1588 uint8_t *buf = RTMemAlloc(1600);
1589
1590 m->m_data -= if_maxlinkhdr;
1591 m->m_len += ETH_HLEN;
1592 eh = mtod(m, struct ethhdr *);
1593
1594 if(MBUF_HEAD(m) != m->m_data)
1595 {
1596 LogRel(("NAT: ethernet detects corruption of the packet"));
1597 AssertMsgFailed(("!!Ethernet frame corrupted!!"));
1598 }
1599
1600#ifndef VBOX_WITH_NAT_SERVICE
1601 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
1602 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
1603 /* XXX: not correct */
1604 eh->h_source[5] = CTL_ALIAS;
1605#else
1606 if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) != 0)
1607 {
1608 memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
1609 memcpy(eh->h_source, special_ethaddr, ETH_ALEN);
1610 Assert(memcmp(eh->h_dest, special_ethaddr, ETH_ALEN) != 0);
1611 Assert(memcmp(eh->h_dest, zerro_ethaddr, ETH_ALEN) != 0);
1612 }
1613#endif
1614 eh->h_proto = htons(eth_proto);
1615#if 0
1616 slirp_output(pData->pvUser, m, mtod(m, uint8_t *), m->m_len);
1617#else
1618 memcpy(buf, mtod(m, uint8_t *), m->m_len);
1619 slirp_output(pData->pvUser, NULL, buf, m->m_len);
1620 m_free(pData, m);
1621#endif
1622}
1623
1624int slirp_redir(PNATState pData, int is_udp, int host_port,
1625 struct in_addr guest_addr, int guest_port)
1626{
1627 struct socket *so;
1628 Log2(("NAT: set redirect %s hp:%d gp:%d\n", (is_udp?"UDP":"TCP"), host_port, guest_port));
1629 if (is_udp)
1630 {
1631 so = udp_listen(pData, htons(host_port), guest_addr.s_addr,
1632 htons(guest_port), 0);
1633 }
1634 else
1635 {
1636 so = solisten(pData, htons(host_port), guest_addr.s_addr,
1637 htons(guest_port), 0);
1638 }
1639 Log2(("NAT: redirecting socket %R[natsock]\n", so));
1640 return (so != NULL ? 0 : -1);
1641}
1642
1643int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
1644 int guest_port)
1645{
1646 return add_exec(&exec_list, do_pty, (char *)args,
1647 addr_low_byte, htons(guest_port));
1648}
1649
1650void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
1651{
1652#ifndef VBOX_WITH_NAT_SERVICE
1653 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
1654#endif
1655}
1656
1657#if defined(RT_OS_WINDOWS)
1658HANDLE *slirp_get_events(PNATState pData)
1659{
1660 return pData->phEvents;
1661}
1662void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
1663{
1664 pData->phEvents[index] = hEvent;
1665}
1666#endif
1667
1668unsigned int slirp_get_timeout_ms(PNATState pData)
1669{
1670 if (link_up)
1671 {
1672 if (time_fasttimo)
1673 return 2;
1674 if (do_slowtimo)
1675 return 500; /* see PR_SLOWHZ */
1676 }
1677 return 0;
1678}
1679
1680#ifndef RT_OS_WINDOWS
1681int slirp_get_nsock(PNATState pData)
1682{
1683 return pData->nsock;
1684}
1685#endif
1686
1687/*
1688 * this function called from NAT thread
1689 */
1690void slirp_post_sent(PNATState pData, void *pvArg)
1691{
1692 struct socket *so = 0;
1693 struct tcpcb *tp = 0;
1694 struct mbuf *m = (struct mbuf *)pvArg;
1695 m_free(pData, m);
1696}
1697#ifdef VBOX_WITH_SLIRP_MT
1698void slirp_process_queue(PNATState pData)
1699{
1700 RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
1701}
1702void *slirp_get_queue(PNATState pData)
1703{
1704 return pData->pReqQueue;
1705}
1706#endif
1707
1708uint16_t slirp_get_service(int proto, uint16_t dport, uint16_t sport)
1709{
1710 uint16_t hdport, hsport, service;
1711 hdport = ntohs(dport);
1712 hsport = ntohs(sport);
1713 Log2(("proto: %d, dport: %d sport: %d\n", proto, hdport, hsport));
1714 service = 0;
1715#if 1
1716 /* Always return 0 here */
1717 switch (hdport)
1718 {
1719 case 500:
1720 if (hsport != 500) /* vpnc by default try operate in src:500/dst:500 mode*/
1721 /* Not sure why this make Cisco VPN client's connection more stable,
1722 * at least on some servers
1723 */
1724 service = sport;
1725 break;
1726 }
1727#endif
1728 Log2(("service : %d\n", service));
1729 return htons(service);
1730}
1731
1732void slirp_set_dhcp_TFTP_prefix(PNATState pData, const char *tftpPrefix)
1733{
1734 Log2(("tftp_prefix:%s\n", tftpPrefix));
1735 tftp_prefix = tftpPrefix;
1736}
1737
1738void slirp_set_dhcp_TFTP_bootfile(PNATState pData, const char *bootFile)
1739{
1740 Log2(("bootFile:%s\n", bootFile));
1741 bootp_filename = bootFile;
1742}
1743
1744void slirp_set_dhcp_next_server(PNATState pData, const char *next_server)
1745{
1746 Log2(("next_server:%s\n", next_server));
1747 if (next_server == NULL)
1748 pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
1749 else
1750 inet_aton(next_server, &pData->tftp_server);
1751}
1752#ifdef VBOX_WITH_SLIRP_DNS_PROXY
1753void slirp_set_dhcp_dns_proxy(PNATState pData, bool fDNSProxy)
1754{
1755 Log2(("NAT: DNS proxy switched %s\n", (fDNSProxy ? "on" : "off")));
1756 pData->use_dns_proxy = fDNSProxy;
1757}
1758#endif
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