VirtualBox

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

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

slirp.c: Attempt at fixing #4811.

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

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