VirtualBox

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

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

NAT: zero-length strings in dhcp offer

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