VirtualBox

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

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

NAT: FD_CLOSE(win) and POLLHUB(Unix) events are processed in common way.

Darwin specific in handling closing connection was added.

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

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