VirtualBox

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

最後變更 在這個檔案從23158是 23158,由 vboxsync 提交於 15 年 前

NAT: UseHostResolver extradata was introduced
and its relations with PassDomain and UseDNSProxy
have been clarified.

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

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