VirtualBox

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

Last change on this file since 41196 was 41178, checked in by vboxsync, 13 years ago

NAT: attempt to fix xTracker/#6188.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.2 KB
Line 
1/* $Id: slirp.c 41178 2012-05-06 05:11:18Z vboxsync $ */
2/** @file
3 * NAT - slirp glue.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * libslirp glue
22 *
23 * Copyright (c) 2004-2008 Fabrice Bellard
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining a copy
26 * of this software and associated documentation files (the "Software"), to deal
27 * in the Software without restriction, including without limitation the rights
28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 * copies of the Software, and to permit persons to whom the Software is
30 * furnished to do so, subject to the following conditions:
31 *
32 * The above copyright notice and this permission notice shall be included in
33 * all copies or substantial portions of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
38 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 * THE SOFTWARE.
42 */
43
44#include "slirp.h"
45#ifdef RT_OS_OS2
46# include <paths.h>
47#endif
48
49#include <VBox/err.h>
50#include <VBox/vmm/pdmdrv.h>
51#include <iprt/assert.h>
52#include <iprt/file.h>
53#ifndef RT_OS_WINDOWS
54# include <sys/ioctl.h>
55# include <poll.h>
56# include <netinet/in.h>
57#else
58# include <Winnls.h>
59# define _WINSOCK2API_
60# include <IPHlpApi.h>
61#endif
62#include <alias.h>
63
64#ifndef RT_OS_WINDOWS
65
66# define DO_ENGAGE_EVENT1(so, fdset, label) \
67 do { \
68 if ( so->so_poll_index != -1 \
69 && so->s == polls[so->so_poll_index].fd) \
70 { \
71 polls[so->so_poll_index].events |= N_(fdset ## _poll); \
72 break; \
73 } \
74 AssertRelease(poll_index < (nfds)); \
75 AssertRelease(poll_index >= 0 && poll_index < (nfds)); \
76 polls[poll_index].fd = (so)->s; \
77 (so)->so_poll_index = poll_index; \
78 polls[poll_index].events = N_(fdset ## _poll); \
79 polls[poll_index].revents = 0; \
80 poll_index++; \
81 } while (0)
82
83# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
84 do { \
85 if ( so->so_poll_index != -1 \
86 && so->s == polls[so->so_poll_index].fd) \
87 { \
88 polls[so->so_poll_index].events |= \
89 N_(fdset1 ## _poll) | N_(fdset2 ## _poll); \
90 break; \
91 } \
92 AssertRelease(poll_index < (nfds)); \
93 polls[poll_index].fd = (so)->s; \
94 (so)->so_poll_index = poll_index; \
95 polls[poll_index].events = \
96 N_(fdset1 ## _poll) | N_(fdset2 ## _poll); \
97 poll_index++; \
98 } while (0)
99
100# define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
101
102/*
103 * DO_CHECK_FD_SET is used in dumping events on socket, including POLLNVAL.
104 * gcc warns about attempts to log POLLNVAL so construction in a last to lines
105 * used to catch POLLNVAL while logging and return false in case of error while
106 * normal usage.
107 */
108# define DO_CHECK_FD_SET(so, events, fdset) \
109 ( ((so)->so_poll_index != -1) \
110 && ((so)->so_poll_index <= ndfs) \
111 && ((so)->s == polls[so->so_poll_index].fd) \
112 && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)) \
113 && ( N_(fdset ## _poll) == POLLNVAL \
114 || !(polls[(so)->so_poll_index].revents & POLLNVAL)))
115
116 /* specific for Windows Winsock API */
117# define DO_WIN_CHECK_FD_SET(so, events, fdset) 0
118
119# ifndef RT_OS_LINUX
120# define readfds_poll (POLLRDNORM)
121# define writefds_poll (POLLWRNORM)
122# else
123# define readfds_poll (POLLIN)
124# define writefds_poll (POLLOUT)
125# endif
126# define xfds_poll (POLLPRI)
127# define closefds_poll (POLLHUP)
128# define rderr_poll (POLLERR)
129# if 0 /* unused yet */
130# define rdhup_poll (POLLHUP)
131# define nval_poll (POLLNVAL)
132# endif
133
134# define ICMP_ENGAGE_EVENT(so, fdset) \
135 do { \
136 if (pData->icmp_socket.s != -1) \
137 DO_ENGAGE_EVENT1((so), fdset, ICMP); \
138 } while (0)
139
140#else /* RT_OS_WINDOWS */
141
142/*
143 * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
144 * So no call to WSAEventSelect necessary.
145 */
146# define ICMP_ENGAGE_EVENT(so, fdset) do {} while (0)
147
148/*
149 * On Windows we use FD_ALL_EVENTS to ensure that we don't miss any event.
150 */
151# define DO_ENGAGE_EVENT1(so, fdset1, label) \
152 do { \
153 rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
154 if (rc == SOCKET_ERROR) \
155 { \
156 /* This should not happen */ \
157 error = WSAGetLastError(); \
158 LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
159 error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
160 } \
161 } while (0); \
162 CONTINUE(label)
163
164# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
165 DO_ENGAGE_EVENT1((so), (fdset1), label)
166
167# define DO_POLL_EVENTS(rc, error, so, events, label) \
168 (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
169 if ((rc) == SOCKET_ERROR) \
170 { \
171 (error) = WSAGetLastError(); \
172 LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
173 CONTINUE(label); \
174 }
175
176# define acceptds_win FD_ACCEPT
177# define acceptds_win_bit FD_ACCEPT_BIT
178# define readfds_win FD_READ
179# define readfds_win_bit FD_READ_BIT
180# define writefds_win FD_WRITE
181# define writefds_win_bit FD_WRITE_BIT
182# define xfds_win FD_OOB
183# define xfds_win_bit FD_OOB_BIT
184# define closefds_win FD_CLOSE
185# define closefds_win_bit FD_CLOSE_BIT
186# define connectfds_win FD_CONNECT
187# define connectfds_win_bit FD_CONNECT_BIT
188
189# define closefds_win FD_CLOSE
190# define closefds_win_bit FD_CLOSE_BIT
191
192# define DO_CHECK_FD_SET(so, events, fdset) \
193 (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
194
195# define DO_WIN_CHECK_FD_SET(so, events, fdset) DO_CHECK_FD_SET((so), (events), fdset)
196# define DO_UNIX_CHECK_FD_SET(so, events, fdset) 1 /*specific for Unix API */
197
198#endif /* RT_OS_WINDOWS */
199
200#define TCP_ENGAGE_EVENT1(so, fdset) \
201 DO_ENGAGE_EVENT1((so), fdset, tcp)
202
203#define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
204 DO_ENGAGE_EVENT2((so), fdset1, fdset2, tcp)
205
206#ifdef RT_OS_WINDOWS
207# define WIN_TCP_ENGAGE_EVENT2(so, fdset, fdset2) TCP_ENGAGE_EVENT2(so, fdset1, fdset2)
208#endif
209
210#define UDP_ENGAGE_EVENT(so, fdset) \
211 DO_ENGAGE_EVENT1((so), fdset, udp)
212
213#define POLL_TCP_EVENTS(rc, error, so, events) \
214 DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
215
216#define POLL_UDP_EVENTS(rc, error, so, events) \
217 DO_POLL_EVENTS((rc), (error), (so), (events), udp)
218
219#define CHECK_FD_SET(so, events, set) \
220 (DO_CHECK_FD_SET((so), (events), set))
221
222#define WIN_CHECK_FD_SET(so, events, set) \
223 (DO_WIN_CHECK_FD_SET((so), (events), set))
224
225/*
226 * Loging macros
227 */
228#if VBOX_WITH_DEBUG_NAT_SOCKETS
229# if defined(RT_OS_WINDOWS)
230# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
231 do { \
232 LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
233 } while (0)
234# else /* !RT_OS_WINDOWS */
235# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
236 do { \
237 LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
238 CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
239 CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
240 CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
241 CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
242 CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
243 CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
244 } while (0)
245# endif /* !RT_OS_WINDOWS */
246#else /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
247# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
248#endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
249
250#define LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
251 DO_LOG_NAT_SOCK((so), proto, (winevent), r_fdset, w_fdset, x_fdset)
252
253static void activate_port_forwarding(PNATState, const uint8_t *pEther);
254
255static const uint8_t special_ethaddr[6] =
256{
257 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
258};
259
260static const uint8_t broadcast_ethaddr[6] =
261{
262 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
263};
264
265const uint8_t zerro_ethaddr[6] =
266{
267 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
268};
269
270/**
271 * This helper routine do the checks in descriptions to
272 * ''fUnderPolling'' and ''fShouldBeRemoved'' flags
273 * @returns 1 if socket removed and 0 if no changes was made.
274 */
275static int slirpVerifyAndFreeSocket(PNATState pData, struct socket *pSocket)
276{
277 AssertPtrReturn(pData, 0);
278 AssertPtrReturn(pSocket, 0);
279 AssertReturn(pSocket->fUnderPolling, 0);
280 if (pSocket->fShouldBeRemoved)
281 {
282 pSocket->fUnderPolling = 0;
283 sofree(pData, pSocket);
284 /* so is PHANTOM, now */
285 return 1;
286 }
287 return 0;
288}
289
290#ifdef RT_OS_WINDOWS
291static int get_dns_addr_domain(PNATState pData,
292 const char **ppszDomain)
293{
294 ULONG flags = GAA_FLAG_INCLUDE_PREFIX; /*GAA_FLAG_INCLUDE_ALL_INTERFACES;*/ /* all interfaces registered in NDIS */
295 PIP_ADAPTER_ADDRESSES pAdapterAddr = NULL;
296 PIP_ADAPTER_ADDRESSES pAddr = NULL;
297 PIP_ADAPTER_DNS_SERVER_ADDRESS pDnsAddr = NULL;
298 ULONG size;
299 int wlen = 0;
300 char *pszSuffix;
301 struct dns_domain_entry *pDomain = NULL;
302 ULONG ret = ERROR_SUCCESS;
303
304 /* @todo add SKIPing flags to get only required information */
305
306 /* determine size of buffer */
307 size = 0;
308 ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, pAdapterAddr, &size);
309 if (ret != ERROR_BUFFER_OVERFLOW)
310 {
311 Log(("NAT: error %lu occurred on capacity detection operation\n", ret));
312 return -1;
313 }
314 if (size == 0)
315 {
316 Log(("NAT: Win socket API returns non capacity\n"));
317 return -1;
318 }
319
320 pAdapterAddr = RTMemAllocZ(size);
321 if (!pAdapterAddr)
322 {
323 Log(("NAT: No memory available\n"));
324 return -1;
325 }
326 ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, pAdapterAddr, &size);
327 if (ret != ERROR_SUCCESS)
328 {
329 Log(("NAT: error %lu occurred on fetching adapters info\n", ret));
330 RTMemFree(pAdapterAddr);
331 return -1;
332 }
333
334 for (pAddr = pAdapterAddr; pAddr != NULL; pAddr = pAddr->Next)
335 {
336 int found;
337 if (pAddr->OperStatus != IfOperStatusUp)
338 continue;
339
340 for (pDnsAddr = pAddr->FirstDnsServerAddress; pDnsAddr != NULL; pDnsAddr = pDnsAddr->Next)
341 {
342 struct sockaddr *SockAddr = pDnsAddr->Address.lpSockaddr;
343 struct in_addr InAddr;
344 struct dns_entry *pDns;
345
346 if (SockAddr->sa_family != AF_INET)
347 continue;
348
349 InAddr = ((struct sockaddr_in *)SockAddr)->sin_addr;
350
351 /* add dns server to list */
352 pDns = RTMemAllocZ(sizeof(struct dns_entry));
353 if (!pDns)
354 {
355 Log(("NAT: Can't allocate buffer for DNS entry\n"));
356 RTMemFree(pAdapterAddr);
357 return VERR_NO_MEMORY;
358 }
359
360 Log(("NAT: adding %RTnaipv4 to DNS server list\n", InAddr));
361 if ((InAddr.s_addr & RT_H2N_U32_C(IN_CLASSA_NET)) == RT_N2H_U32_C(INADDR_LOOPBACK & IN_CLASSA_NET))
362 pDns->de_addr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
363 else
364 pDns->de_addr.s_addr = InAddr.s_addr;
365
366 TAILQ_INSERT_HEAD(&pData->pDnsList, pDns, de_list);
367
368 if (pAddr->DnsSuffix == NULL)
369 continue;
370
371 /* uniq */
372 RTUtf16ToUtf8(pAddr->DnsSuffix, &pszSuffix);
373 if (!pszSuffix || strlen(pszSuffix) == 0)
374 {
375 RTStrFree(pszSuffix);
376 continue;
377 }
378
379 found = 0;
380 LIST_FOREACH(pDomain, &pData->pDomainList, dd_list)
381 {
382 if ( pDomain->dd_pszDomain != NULL
383 && strcmp(pDomain->dd_pszDomain, pszSuffix) == 0)
384 {
385 found = 1;
386 RTStrFree(pszSuffix);
387 break;
388 }
389 }
390 if (!found)
391 {
392 pDomain = RTMemAllocZ(sizeof(struct dns_domain_entry));
393 if (!pDomain)
394 {
395 Log(("NAT: not enough memory\n"));
396 RTStrFree(pszSuffix);
397 RTMemFree(pAdapterAddr);
398 return VERR_NO_MEMORY;
399 }
400 pDomain->dd_pszDomain = pszSuffix;
401 Log(("NAT: adding domain name %s to search list\n", pDomain->dd_pszDomain));
402 LIST_INSERT_HEAD(&pData->pDomainList, pDomain, dd_list);
403 }
404 }
405 }
406 RTMemFree(pAdapterAddr);
407 return 0;
408}
409
410#else /* !RT_OS_WINDOWS */
411
412static int RTFileGets(RTFILE File, void *pvBuf, size_t cbBufSize, size_t *pcbRead)
413{
414 size_t cbRead;
415 char bTest;
416 int rc = VERR_NO_MEMORY;
417 char *pu8Buf = (char *)pvBuf;
418 *pcbRead = 0;
419
420 while ( RT_SUCCESS(rc = RTFileRead(File, &bTest, 1, &cbRead))
421 && (pu8Buf - (char *)pvBuf) < cbBufSize)
422 {
423 if (cbRead == 0)
424 return VERR_EOF;
425
426 if (bTest == '\r' || bTest == '\n')
427 {
428 *pu8Buf = 0;
429 return VINF_SUCCESS;
430 }
431 *pu8Buf = bTest;
432 pu8Buf++;
433 (*pcbRead)++;
434 }
435 return rc;
436}
437
438static int get_dns_addr_domain(PNATState pData, const char **ppszDomain)
439{
440 char buff[512];
441 char buff2[256];
442 RTFILE f;
443 int cNameserversFound = 0;
444 bool fWarnTooManyDnsServers = false;
445 struct in_addr tmp_addr;
446 int rc;
447 size_t bytes;
448
449# ifdef RT_OS_OS2
450 /* Try various locations. */
451 char *etc = getenv("ETC");
452 if (etc)
453 {
454 RTStrmPrintf(buff, sizeof(buff), "%s/RESOLV2", etc);
455 rc = RTFileOpen(&f, buff, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
456 }
457 if (RT_FAILURE(rc))
458 {
459 RTStrmPrintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
460 rc = RTFileOpen(&f, buff, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
461 }
462 if (RT_FAILURE(rc))
463 {
464 RTStrmPrintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
465 rc = RTFileOpen(&f, buff, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
466 }
467# else /* !RT_OS_OS2 */
468# ifndef DEBUG_vvl
469 rc = RTFileOpen(&f, "/etc/resolv.conf", RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
470# else
471 char *home = getenv("HOME");
472 RTStrPrintf(buff, sizeof(buff), "%s/resolv.conf", home);
473 rc = RTFileOpen(&f, buff, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
474 if (RT_SUCCESS(rc))
475 {
476 Log(("NAT: DNS we're using %s\n", buff));
477 }
478 else
479 {
480 rc = RTFileOpen(&f, "/etc/resolv.conf", RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
481 Log(("NAT: DNS we're using %s\n", buff));
482 }
483# endif
484# endif /* !RT_OS_OS2 */
485 if (RT_FAILURE(rc))
486 return -1;
487
488 if (ppszDomain)
489 *ppszDomain = NULL;
490
491 Log(("NAT: DNS Servers:\n"));
492 while ( RT_SUCCESS(rc = RTFileGets(f, buff, sizeof(buff), &bytes))
493 && rc != VERR_EOF)
494 {
495 struct dns_entry *pDns = NULL;
496 if ( cNameserversFound == 4
497 && !fWarnTooManyDnsServers
498 && sscanf(buff, "nameserver%*[ \t]%255s", buff2) == 1)
499 {
500 fWarnTooManyDnsServers = true;
501 LogRel(("NAT: too many nameservers registered.\n"));
502 }
503 if ( sscanf(buff, "nameserver%*[ \t]%255s", buff2) == 1
504 && cNameserversFound < 4) /* Unix doesn't accept more than 4 name servers*/
505 {
506 if (!inet_aton(buff2, &tmp_addr))
507 continue;
508
509 /* localhost mask */
510 pDns = RTMemAllocZ(sizeof (struct dns_entry));
511 if (!pDns)
512 {
513 Log(("can't alloc memory for DNS entry\n"));
514 return -1;
515 }
516
517 /* check */
518 pDns->de_addr.s_addr = tmp_addr.s_addr;
519 if ((pDns->de_addr.s_addr & RT_H2N_U32_C(IN_CLASSA_NET)) == RT_N2H_U32_C(INADDR_LOOPBACK & IN_CLASSA_NET))
520 {
521 pDns->de_addr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
522 }
523 TAILQ_INSERT_HEAD(&pData->pDnsList, pDns, de_list);
524 cNameserversFound++;
525 }
526 if ((!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
527 {
528 char *tok;
529 char *saveptr;
530 struct dns_domain_entry *pDomain = NULL;
531 int fFoundDomain = 0;
532 tok = strtok_r(&buff[6], " \t\n", &saveptr);
533 LIST_FOREACH(pDomain, &pData->pDomainList, dd_list)
534 {
535 if ( tok != NULL
536 && strcmp(tok, pDomain->dd_pszDomain) == 0)
537 {
538 fFoundDomain = 1;
539 break;
540 }
541 }
542 if (tok != NULL && !fFoundDomain)
543 {
544 pDomain = RTMemAllocZ(sizeof(struct dns_domain_entry));
545 if (!pDomain)
546 {
547 Log(("NAT: not enought memory to add domain list\n"));
548 return VERR_NO_MEMORY;
549 }
550 pDomain->dd_pszDomain = RTStrDup(tok);
551 Log(("NAT: adding domain name %s to search list\n", pDomain->dd_pszDomain));
552 LIST_INSERT_HEAD(&pData->pDomainList, pDomain, dd_list);
553 }
554 }
555 }
556 RTFileClose(f);
557 if (!cNameserversFound)
558 return -1;
559 return 0;
560}
561
562#endif /* !RT_OS_WINDOWS */
563
564int slirp_init_dns_list(PNATState pData)
565{
566 TAILQ_INIT(&pData->pDnsList);
567 LIST_INIT(&pData->pDomainList);
568 return get_dns_addr_domain(pData, NULL);
569}
570
571void slirp_release_dns_list(PNATState pData)
572{
573 struct dns_entry *pDns = NULL;
574 struct dns_domain_entry *pDomain = NULL;
575
576 while (!TAILQ_EMPTY(&pData->pDnsList))
577 {
578 pDns = TAILQ_FIRST(&pData->pDnsList);
579 TAILQ_REMOVE(&pData->pDnsList, pDns, de_list);
580 RTMemFree(pDns);
581 }
582
583 while (!LIST_EMPTY(&pData->pDomainList))
584 {
585 pDomain = LIST_FIRST(&pData->pDomainList);
586 LIST_REMOVE(pDomain, dd_list);
587 if (pDomain->dd_pszDomain != NULL)
588 RTStrFree(pDomain->dd_pszDomain);
589 RTMemFree(pDomain);
590 }
591}
592
593int get_dns_addr(PNATState pData)
594{
595 return get_dns_addr_domain(pData, NULL);
596}
597
598int slirp_init(PNATState *ppData, uint32_t u32NetAddr, uint32_t u32Netmask,
599 bool fPassDomain, bool fUseHostResolver, int i32AliasMode,
600 int iIcmpCacheLimit, void *pvUser)
601{
602 int fNATfailed = 0;
603 int rc;
604 PNATState pData;
605 if (u32Netmask & 0x1f)
606 /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
607 return VERR_INVALID_PARAMETER;
608 pData = RTMemAllocZ(RT_ALIGN_Z(sizeof(NATState), sizeof(uint64_t)));
609 *ppData = pData;
610 if (!pData)
611 return VERR_NO_MEMORY;
612 pData->fPassDomain = !fUseHostResolver ? fPassDomain : false;
613 pData->fUseHostResolver = fUseHostResolver;
614 pData->pvUser = pvUser;
615 pData->netmask = u32Netmask;
616
617 /* sockets & TCP defaults */
618 pData->socket_rcv = 64 * _1K;
619 pData->socket_snd = 64 * _1K;
620 tcp_sndspace = 64 * _1K;
621 tcp_rcvspace = 64 * _1K;
622
623 /*
624 * Use the same default here as in DevNAT.cpp (SoMaxConnection CFGM value)
625 * to avoid release log noise.
626 */
627 pData->soMaxConn = 10;
628
629#ifdef RT_OS_WINDOWS
630 {
631 WSADATA Data;
632 WSAStartup(MAKEWORD(2, 0), &Data);
633 }
634 pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
635#endif
636#ifdef VBOX_WITH_SLIRP_MT
637 QSOCKET_LOCK_CREATE(tcb);
638 QSOCKET_LOCK_CREATE(udb);
639 rc = RTReqQueueCreate(&pData->pReqQueue);
640 AssertReleaseRC(rc);
641#endif
642
643 link_up = 1;
644
645 rc = bootp_dhcp_init(pData);
646 if (RT_FAILURE(rc))
647 {
648 Log(("NAT: DHCP server initialization failed\n"));
649 RTMemFree(pData);
650 *ppData = NULL;
651 return rc;
652 }
653 debug_init(pData);
654 if_init(pData);
655 ip_init(pData);
656 icmp_init(pData, iIcmpCacheLimit);
657
658 /* Initialise mbufs *after* setting the MTU */
659 mbuf_init(pData);
660
661 pData->special_addr.s_addr = u32NetAddr;
662 pData->slirp_ethaddr = &special_ethaddr[0];
663 alias_addr.s_addr = pData->special_addr.s_addr | RT_H2N_U32_C(CTL_ALIAS);
664 /* @todo: add ability to configure this staff */
665
666 /* set default addresses */
667 inet_aton("127.0.0.1", &loopback_addr);
668 if (!pData->fUseHostResolver)
669 {
670 if (slirp_init_dns_list(pData) < 0)
671 fNATfailed = 1;
672
673 dnsproxy_init(pData);
674 }
675 if (i32AliasMode & ~(PKT_ALIAS_LOG|PKT_ALIAS_SAME_PORTS|PKT_ALIAS_PROXY_ONLY))
676 {
677 Log(("NAT: alias mode %x is ignored\n", i32AliasMode));
678 i32AliasMode = 0;
679 }
680 pData->i32AliasMode = i32AliasMode;
681 getouraddr(pData);
682 {
683 int flags = 0;
684 struct in_addr proxy_addr;
685 pData->proxy_alias = LibAliasInit(pData, NULL);
686 if (pData->proxy_alias == NULL)
687 {
688 Log(("NAT: LibAlias default rule wasn't initialized\n"));
689 AssertMsgFailed(("NAT: LibAlias default rule wasn't initialized\n"));
690 }
691 flags = LibAliasSetMode(pData->proxy_alias, 0, 0);
692#ifndef NO_FW_PUNCH
693 flags |= PKT_ALIAS_PUNCH_FW;
694#endif
695 flags |= pData->i32AliasMode; /* do transparent proxying */
696 flags = LibAliasSetMode(pData->proxy_alias, flags, ~0);
697 proxy_addr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
698 LibAliasSetAddress(pData->proxy_alias, proxy_addr);
699 ftp_alias_load(pData);
700 nbt_alias_load(pData);
701 if (pData->fUseHostResolver)
702 dns_alias_load(pData);
703 }
704#ifdef VBOX_WITH_NAT_SEND2HOME
705 /* @todo: we should know all interfaces available on host. */
706 pData->pInSockAddrHomeAddress = RTMemAllocZ(sizeof(struct sockaddr));
707 pData->cInHomeAddressSize = 1;
708 inet_aton("192.168.1.25", &pData->pInSockAddrHomeAddress[0].sin_addr);
709 pData->pInSockAddrHomeAddress[0].sin_family = AF_INET;
710#ifdef RT_OS_DARWIN
711 pData->pInSockAddrHomeAddress[0].sin_len = sizeof(struct sockaddr_in);
712#endif
713#endif
714 return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
715}
716
717/**
718 * Register statistics.
719 */
720void slirp_register_statistics(PNATState pData, PPDMDRVINS pDrvIns)
721{
722#ifdef VBOX_WITH_STATISTICS
723# define PROFILE_COUNTER(name, dsc) REGISTER_COUNTER(name, pData, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
724# define COUNTING_COUNTER(name, dsc) REGISTER_COUNTER(name, pData, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
725# include "counters.h"
726# undef COUNTER
727/** @todo register statistics for the variables dumped by:
728 * ipstats(pData); tcpstats(pData); udpstats(pData); icmpstats(pData);
729 * mbufstats(pData); sockstats(pData); */
730#else /* VBOX_WITH_STATISTICS */
731 NOREF(pData);
732 NOREF(pDrvIns);
733#endif /* !VBOX_WITH_STATISTICS */
734}
735
736/**
737 * Deregister statistics.
738 */
739void slirp_deregister_statistics(PNATState pData, PPDMDRVINS pDrvIns)
740{
741 if (pData == NULL)
742 return;
743#ifdef VBOX_WITH_STATISTICS
744# define PROFILE_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pData)
745# define COUNTING_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pData)
746# include "counters.h"
747#else /* VBOX_WITH_STATISTICS */
748 NOREF(pData);
749 NOREF(pDrvIns);
750#endif /* !VBOX_WITH_STATISTICS */
751}
752
753/**
754 * Marks the link as up, making it possible to establish new connections.
755 */
756void slirp_link_up(PNATState pData)
757{
758 struct arp_cache_entry *ac;
759 link_up = 1;
760
761 if (LIST_EMPTY(&pData->arp_cache))
762 return;
763
764 LIST_FOREACH(ac, &pData->arp_cache, list)
765 {
766 activate_port_forwarding(pData, ac->ether);
767 }
768}
769
770/**
771 * Marks the link as down and cleans up the current connections.
772 */
773void slirp_link_down(PNATState pData)
774{
775 struct socket *so;
776 struct port_forward_rule *rule;
777
778 while ((so = tcb.so_next) != &tcb)
779 {
780 if (so->so_state & SS_NOFDREF || so->s == -1)
781 sofree(pData, so);
782 else
783 tcp_drop(pData, sototcpcb(so), 0);
784 }
785
786 while ((so = udb.so_next) != &udb)
787 udp_detach(pData, so);
788
789 /*
790 * Clear the active state of port-forwarding rules to force
791 * re-setup on restoration of communications.
792 */
793 LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
794 {
795 rule->activated = 0;
796 }
797 pData->cRedirectionsActive = 0;
798
799 link_up = 0;
800}
801
802/**
803 * Terminates the slirp component.
804 */
805void slirp_term(PNATState pData)
806{
807 if (pData == NULL)
808 return;
809 icmp_finit(pData);
810
811 slirp_link_down(pData);
812 slirp_release_dns_list(pData);
813 ftp_alias_unload(pData);
814 nbt_alias_unload(pData);
815 if (pData->fUseHostResolver)
816 {
817 dns_alias_unload(pData);
818#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
819 while (!LIST_EMPTY(&pData->DNSMapHead))
820 {
821 PDNSMAPPINGENTRY pDnsEntry = LIST_FIRST(&pData->DNSMapHead);
822 LIST_REMOVE(pDnsEntry, MapList);
823 RTStrFree(pDnsEntry->pszCName);
824 RTMemFree(pDnsEntry);
825 }
826#endif
827 }
828 while (!LIST_EMPTY(&instancehead))
829 {
830 struct libalias *la = LIST_FIRST(&instancehead);
831 /* libalias do all clean up */
832 LibAliasUninit(la);
833 }
834 while (!LIST_EMPTY(&pData->arp_cache))
835 {
836 struct arp_cache_entry *ac = LIST_FIRST(&pData->arp_cache);
837 LIST_REMOVE(ac, list);
838 RTMemFree(ac);
839 }
840 bootp_dhcp_fini(pData);
841 m_fini(pData);
842#ifdef RT_OS_WINDOWS
843 WSACleanup();
844#endif
845#ifndef VBOX_WITH_SLIRP_BSD_SBUF
846#ifdef LOG_ENABLED
847 Log(("\n"
848 "NAT statistics\n"
849 "--------------\n"
850 "\n"));
851 ipstats(pData);
852 tcpstats(pData);
853 udpstats(pData);
854 icmpstats(pData);
855 mbufstats(pData);
856 sockstats(pData);
857 Log(("\n"
858 "\n"
859 "\n"));
860#endif
861#endif
862 RTMemFree(pData);
863}
864
865
866#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
867#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
868
869/*
870 * curtime kept to an accuracy of 1ms
871 */
872static void updtime(PNATState pData)
873{
874#ifdef RT_OS_WINDOWS
875 struct _timeb tb;
876
877 _ftime(&tb);
878 curtime = (u_int)tb.time * (u_int)1000;
879 curtime += (u_int)tb.millitm;
880#else
881 gettimeofday(&tt, 0);
882
883 curtime = (u_int)tt.tv_sec * (u_int)1000;
884 curtime += (u_int)tt.tv_usec / (u_int)1000;
885
886 if ((tt.tv_usec % 1000) >= 500)
887 curtime++;
888#endif
889}
890
891#ifdef RT_OS_WINDOWS
892void slirp_select_fill(PNATState pData, int *pnfds)
893#else /* RT_OS_WINDOWS */
894void slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
895#endif /* !RT_OS_WINDOWS */
896{
897 struct socket *so, *so_next;
898 int nfds;
899#if defined(RT_OS_WINDOWS)
900 int rc;
901 int error;
902#else
903 int poll_index = 0;
904#endif
905 int i;
906
907 STAM_PROFILE_START(&pData->StatFill, a);
908
909 nfds = *pnfds;
910
911 /*
912 * First, TCP sockets
913 */
914 do_slowtimo = 0;
915 if (!link_up)
916 goto done;
917
918 /*
919 * *_slowtimo needs calling if there are IP fragments
920 * in the fragment queue, or there are TCP connections active
921 */
922 /* XXX:
923 * triggering of fragment expiration should be the same but use new macroses
924 */
925 do_slowtimo = (tcb.so_next != &tcb);
926 if (!do_slowtimo)
927 {
928 for (i = 0; i < IPREASS_NHASH; i++)
929 {
930 if (!TAILQ_EMPTY(&ipq[i]))
931 {
932 do_slowtimo = 1;
933 break;
934 }
935 }
936 }
937 /* always add the ICMP socket */
938#ifndef RT_OS_WINDOWS
939 pData->icmp_socket.so_poll_index = -1;
940#endif
941 ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
942
943 STAM_COUNTER_RESET(&pData->StatTCP);
944 STAM_COUNTER_RESET(&pData->StatTCPHot);
945
946 QSOCKET_FOREACH(so, so_next, tcp)
947 /* { */
948 Assert(so->so_type == IPPROTO_TCP);
949#if !defined(RT_OS_WINDOWS)
950 so->so_poll_index = -1;
951#endif
952 STAM_COUNTER_INC(&pData->StatTCP);
953#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
954 /* TCP socket can't be cloned */
955 Assert((!so->so_cloneOf));
956#endif
957 /*
958 * See if we need a tcp_fasttimo
959 */
960 if ( time_fasttimo == 0
961 && so->so_tcpcb != NULL
962 && so->so_tcpcb->t_flags & TF_DELACK)
963 {
964 time_fasttimo = curtime; /* Flag when we want a fasttimo */
965 }
966
967 /*
968 * NOFDREF can include still connecting to local-host,
969 * newly socreated() sockets etc. Don't want to select these.
970 */
971 if (so->so_state & SS_NOFDREF || so->s == -1)
972 CONTINUE(tcp);
973
974 /*
975 * Set for reading sockets which are accepting
976 */
977 if (so->so_state & SS_FACCEPTCONN)
978 {
979 STAM_COUNTER_INC(&pData->StatTCPHot);
980 TCP_ENGAGE_EVENT1(so, readfds);
981 CONTINUE(tcp);
982 }
983
984 /*
985 * Set for writing sockets which are connecting
986 */
987 if (so->so_state & SS_ISFCONNECTING)
988 {
989 Log2(("connecting %R[natsock] engaged\n",so));
990 STAM_COUNTER_INC(&pData->StatTCPHot);
991#ifdef RT_OS_WINDOWS
992 WIN_TCP_ENGAGE_EVENT2(so, writefds, connectfds);
993#else
994 TCP_ENGAGE_EVENT1(so, writefds);
995#endif
996 }
997
998 /*
999 * Set for writing if we are connected, can send more, and
1000 * we have something to send
1001 */
1002 if (CONN_CANFSEND(so) && SBUF_LEN(&so->so_rcv))
1003 {
1004 STAM_COUNTER_INC(&pData->StatTCPHot);
1005 TCP_ENGAGE_EVENT1(so, writefds);
1006 }
1007
1008 /*
1009 * Set for reading (and urgent data) if we are connected, can
1010 * receive more, and we have room for it XXX /2 ?
1011 */
1012 /* @todo: vvl - check which predicat here will be more useful here in rerm of new sbufs. */
1013 if ( CONN_CANFRCV(so)
1014 && (SBUF_LEN(&so->so_snd) < (SBUF_SIZE(&so->so_snd)/2))
1015#ifdef RT_OS_WINDOWS
1016 && !(so->so_state & SS_ISFCONNECTING)
1017#endif
1018 )
1019 {
1020 STAM_COUNTER_INC(&pData->StatTCPHot);
1021 TCP_ENGAGE_EVENT2(so, readfds, xfds);
1022 }
1023 LOOP_LABEL(tcp, so, so_next);
1024 }
1025
1026 /*
1027 * UDP sockets
1028 */
1029 STAM_COUNTER_RESET(&pData->StatUDP);
1030 STAM_COUNTER_RESET(&pData->StatUDPHot);
1031
1032 QSOCKET_FOREACH(so, so_next, udp)
1033 /* { */
1034
1035 Assert(so->so_type == IPPROTO_UDP);
1036 STAM_COUNTER_INC(&pData->StatUDP);
1037#if !defined(RT_OS_WINDOWS)
1038 so->so_poll_index = -1;
1039#endif
1040
1041 /*
1042 * See if it's timed out
1043 */
1044 if (so->so_expire)
1045 {
1046 if (so->so_expire <= curtime)
1047 {
1048 Log2(("NAT: %R[natsock] expired\n", so));
1049 if (so->so_timeout != NULL)
1050 {
1051 so->so_timeout(pData, so, so->so_timeout_arg);
1052 }
1053#ifdef VBOX_WITH_SLIRP_MT
1054 /* we need so_next for continue our cycle*/
1055 so_next = so->so_next;
1056#endif
1057 UDP_DETACH(pData, so, so_next);
1058 CONTINUE_NO_UNLOCK(udp);
1059 }
1060 }
1061#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
1062 if (so->so_cloneOf)
1063 CONTINUE_NO_UNLOCK(udp);
1064#endif
1065
1066 /*
1067 * When UDP packets are received from over the link, they're
1068 * sendto()'d straight away, so no need for setting for writing
1069 * Limit the number of packets queued by this session to 4.
1070 * Note that even though we try and limit this to 4 packets,
1071 * the session could have more queued if the packets needed
1072 * to be fragmented.
1073 *
1074 * (XXX <= 4 ?)
1075 */
1076 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
1077 {
1078 STAM_COUNTER_INC(&pData->StatUDPHot);
1079 UDP_ENGAGE_EVENT(so, readfds);
1080 }
1081 LOOP_LABEL(udp, so, so_next);
1082 }
1083done:
1084
1085#if defined(RT_OS_WINDOWS)
1086 *pnfds = VBOX_EVENT_COUNT;
1087#else /* RT_OS_WINDOWS */
1088 AssertRelease(poll_index <= *pnfds);
1089 *pnfds = poll_index;
1090#endif /* !RT_OS_WINDOWS */
1091
1092 STAM_PROFILE_STOP(&pData->StatFill, a);
1093}
1094
1095
1096static bool slirpConnectOrWrite(PNATState pData, struct socket *so, bool fConnectOnly)
1097{
1098 int ret;
1099 LogFlowFunc(("ENTER: so:%R[natsock], fConnectOnly:%RTbool\n", so, fConnectOnly));
1100 /*
1101 * Check for non-blocking, still-connecting sockets
1102 */
1103 if (so->so_state & SS_ISFCONNECTING)
1104 {
1105 Log2(("connecting %R[natsock] catched\n", so));
1106 /* Connected */
1107 so->so_state &= ~SS_ISFCONNECTING;
1108
1109 /*
1110 * This should be probably guarded by PROBE_CONN too. Anyway,
1111 * we disable it on OS/2 because the below send call returns
1112 * EFAULT which causes the opened TCP socket to close right
1113 * after it has been opened and connected.
1114 */
1115#ifndef RT_OS_OS2
1116 ret = send(so->s, (const char *)&ret, 0, 0);
1117 if (ret < 0)
1118 {
1119 /* XXXXX Must fix, zero bytes is a NOP */
1120 if ( errno == EAGAIN
1121 || errno == EWOULDBLOCK
1122 || errno == EINPROGRESS
1123 || errno == ENOTCONN)
1124 {
1125 LogFlowFunc(("LEAVE: false\n"));
1126 return false;
1127 }
1128
1129 /* else failed */
1130 so->so_state = SS_NOFDREF;
1131 }
1132 /* else so->so_state &= ~SS_ISFCONNECTING; */
1133#endif
1134
1135 /*
1136 * Continue tcp_input
1137 */
1138 TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
1139 /* continue; */
1140 }
1141 else if (!fConnectOnly)
1142 SOWRITE(ret, pData, so);
1143 /*
1144 * XXX If we wrote something (a lot), there could be the need
1145 * for a window update. In the worst case, the remote will send
1146 * a window probe to get things going again.
1147 */
1148 LogFlowFunc(("LEAVE: true\n"));
1149 return true;
1150}
1151
1152#if defined(RT_OS_WINDOWS)
1153void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
1154#else /* RT_OS_WINDOWS */
1155void slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
1156#endif /* !RT_OS_WINDOWS */
1157{
1158 struct socket *so, *so_next;
1159 int ret;
1160#if defined(RT_OS_WINDOWS)
1161 WSANETWORKEVENTS NetworkEvents;
1162 int rc;
1163 int error;
1164#endif
1165
1166 STAM_PROFILE_START(&pData->StatPoll, a);
1167
1168 /* Update time */
1169 updtime(pData);
1170
1171 /*
1172 * See if anything has timed out
1173 */
1174 if (link_up)
1175 {
1176 if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
1177 {
1178 STAM_PROFILE_START(&pData->StatFastTimer, b);
1179 tcp_fasttimo(pData);
1180 time_fasttimo = 0;
1181 STAM_PROFILE_STOP(&pData->StatFastTimer, b);
1182 }
1183 if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
1184 {
1185 STAM_PROFILE_START(&pData->StatSlowTimer, c);
1186 ip_slowtimo(pData);
1187 tcp_slowtimo(pData);
1188 last_slowtimo = curtime;
1189 STAM_PROFILE_STOP(&pData->StatSlowTimer, c);
1190 }
1191 }
1192#if defined(RT_OS_WINDOWS)
1193 if (fTimeout)
1194 return; /* only timer update */
1195#endif
1196
1197 /*
1198 * Check sockets
1199 */
1200 if (!link_up)
1201 goto done;
1202#if defined(RT_OS_WINDOWS)
1203 /*XXX: before renaming please make see define
1204 * fIcmp in slirp_state.h
1205 */
1206 if (fIcmp)
1207 sorecvfrom(pData, &pData->icmp_socket);
1208#else
1209 if ( (pData->icmp_socket.s != -1)
1210 && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
1211 sorecvfrom(pData, &pData->icmp_socket);
1212#endif
1213 /*
1214 * Check TCP sockets
1215 */
1216 QSOCKET_FOREACH(so, so_next, tcp)
1217 /* { */
1218
1219#ifdef VBOX_WITH_SLIRP_MT
1220 if ( so->so_state & SS_NOFDREF
1221 && so->so_deleted == 1)
1222 {
1223 struct socket *son, *sop = NULL;
1224 QSOCKET_LOCK(tcb);
1225 if (so->so_next != NULL)
1226 {
1227 if (so->so_next != &tcb)
1228 SOCKET_LOCK(so->so_next);
1229 son = so->so_next;
1230 }
1231 if ( so->so_prev != &tcb
1232 && so->so_prev != NULL)
1233 {
1234 SOCKET_LOCK(so->so_prev);
1235 sop = so->so_prev;
1236 }
1237 QSOCKET_UNLOCK(tcb);
1238 remque(pData, so);
1239 NSOCK_DEC();
1240 SOCKET_UNLOCK(so);
1241 SOCKET_LOCK_DESTROY(so);
1242 RTMemFree(so);
1243 so_next = son;
1244 if (sop != NULL)
1245 SOCKET_UNLOCK(sop);
1246 CONTINUE_NO_UNLOCK(tcp);
1247 }
1248#endif
1249 /* TCP socket can't be cloned */
1250#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
1251 Assert((!so->so_cloneOf));
1252#endif
1253 /*
1254 * FD_ISSET is meaningless on these sockets
1255 * (and they can crash the program)
1256 */
1257 if (so->so_state & SS_NOFDREF || so->s == -1)
1258 CONTINUE(tcp);
1259
1260 Assert(!so->fUnderPolling);
1261 so->fUnderPolling = 1;
1262 POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
1263
1264 LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
1265
1266
1267 /*
1268 * Check for URG data
1269 * This will soread as well, so no need to
1270 * test for readfds below if this succeeds
1271 */
1272
1273 /* out-of-band data */
1274 if ( CHECK_FD_SET(so, NetworkEvents, xfds)
1275#ifdef RT_OS_DARWIN
1276 /* Darwin and probably BSD hosts generates POLLPRI|POLLHUP event on receiving TCP.flags.{ACK|URG|FIN} this
1277 * combination on other Unixs hosts doesn't enter to this branch
1278 */
1279 && !CHECK_FD_SET(so, NetworkEvents, closefds)
1280#endif
1281#ifdef RT_OS_WINDOWS
1282 /**
1283 * In some cases FD_CLOSE comes with FD_OOB, that confuse tcp processing.
1284 */
1285 && !WIN_CHECK_FD_SET(so, NetworkEvents, closefds)
1286#endif
1287 )
1288 {
1289 sorecvoob(pData, so);
1290 if (slirpVerifyAndFreeSocket(pData, so))
1291 CONTINUE(tcp);
1292 }
1293
1294 /*
1295 * Check sockets for reading
1296 */
1297 else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
1298 || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
1299 {
1300
1301#ifdef RT_OS_WINDOWS
1302 if (WIN_CHECK_FD_SET(so, NetworkEvents, connectfds))
1303 {
1304 /* Finish connection first */
1305 /* should we ignore return value? */
1306 bool fRet = slirpConnectOrWrite(pData, so, true);
1307 LogFunc(("fRet:%RTbool\n", fRet));
1308 if (slirpVerifyAndFreeSocket(pData, so))
1309 CONTINUE(tcp);
1310 }
1311#endif
1312 /*
1313 * Check for incoming connections
1314 */
1315 if (so->so_state & SS_FACCEPTCONN)
1316 {
1317 TCP_CONNECT(pData, so);
1318 if (slirpVerifyAndFreeSocket(pData, so))
1319 CONTINUE(tcp);
1320 if (!CHECK_FD_SET(so, NetworkEvents, closefds))
1321 {
1322 so->fUnderPolling = 0;
1323 CONTINUE(tcp);
1324 }
1325 }
1326
1327 ret = soread(pData, so);
1328 if (slirpVerifyAndFreeSocket(pData, so))
1329 CONTINUE(tcp);
1330 /* Output it if we read something */
1331 if (RT_LIKELY(ret > 0))
1332 TCP_OUTPUT(pData, sototcpcb(so));
1333
1334 if (slirpVerifyAndFreeSocket(pData, so))
1335 CONTINUE(tcp);
1336 }
1337
1338 /*
1339 * Check for FD_CLOSE events.
1340 * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
1341 */
1342 if ( CHECK_FD_SET(so, NetworkEvents, closefds)
1343 || (so->so_close == 1))
1344 {
1345 /*
1346 * drain the socket
1347 */
1348 for (; so_next->so_prev == so
1349 && !slirpVerifyAndFreeSocket(pData, so);)
1350 {
1351 ret = soread(pData, so);
1352 if (slirpVerifyAndFreeSocket(pData, so))
1353 break;
1354
1355 if (ret > 0)
1356 TCP_OUTPUT(pData, sototcpcb(so));
1357 else if (so_next->so_prev == so)
1358 {
1359 Log2(("%R[natsock] errno %d (%s)\n", so, errno, strerror(errno)));
1360 break;
1361 }
1362 }
1363
1364 /* if socket freed ''so'' is PHANTOM and next socket isn't points on it */
1365 if (so_next->so_prev == so)
1366 {
1367 /* mark the socket for termination _after_ it was drained */
1368 so->so_close = 1;
1369 /* No idea about Windows but on Posix, POLLHUP means that we can't send more.
1370 * Actually in the specific error scenario, POLLERR is set as well. */
1371#ifndef RT_OS_WINDOWS
1372 if (CHECK_FD_SET(so, NetworkEvents, rderr))
1373 sofcantsendmore(so);
1374#endif
1375 }
1376 if (so_next->so_prev == so)
1377 so->fUnderPolling = 0;
1378 CONTINUE(tcp);
1379 }
1380
1381 /*
1382 * Check sockets for writing
1383 */
1384 if ( CHECK_FD_SET(so, NetworkEvents, writefds)
1385#ifdef RT_OS_WINDOWS
1386 || WIN_CHECK_FD_SET(so, NetworkEvents, connectfds)
1387#endif
1388 )
1389 {
1390 if(!slirpConnectOrWrite(pData, so, false))
1391 {
1392 if (!slirpVerifyAndFreeSocket(pData, so))
1393 so->fUnderPolling = 0;
1394 CONTINUE(tcp);
1395 }
1396 }
1397
1398 /*
1399 * Probe a still-connecting, non-blocking socket
1400 * to check if it's still alive
1401 */
1402#ifdef PROBE_CONN
1403 if (so->so_state & SS_ISFCONNECTING)
1404 {
1405 ret = recv(so->s, (char *)&ret, 0, 0);
1406
1407 if (ret < 0)
1408 {
1409 /* XXX */
1410 if ( errno == EAGAIN
1411 || errno == EWOULDBLOCK
1412 || errno == EINPROGRESS
1413 || errno == ENOTCONN)
1414 {
1415 CONTINUE(tcp); /* Still connecting, continue */
1416 }
1417
1418 /* else failed */
1419 so->so_state = SS_NOFDREF;
1420
1421 /* tcp_input will take care of it */
1422 }
1423 else
1424 {
1425 ret = send(so->s, &ret, 0, 0);
1426 if (ret < 0)
1427 {
1428 /* XXX */
1429 if ( errno == EAGAIN
1430 || errno == EWOULDBLOCK
1431 || errno == EINPROGRESS
1432 || errno == ENOTCONN)
1433 {
1434 CONTINUE(tcp);
1435 }
1436 /* else failed */
1437 so->so_state = SS_NOFDREF;
1438 }
1439 else
1440 so->so_state &= ~SS_ISFCONNECTING;
1441
1442 }
1443 TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
1444 } /* SS_ISFCONNECTING */
1445#endif
1446 if (!slirpVerifyAndFreeSocket(pData, so))
1447 so->fUnderPolling = 0;
1448 LOOP_LABEL(tcp, so, so_next);
1449 }
1450
1451 /*
1452 * Now UDP sockets.
1453 * Incoming packets are sent straight away, they're not buffered.
1454 * Incoming UDP data isn't buffered either.
1455 */
1456 QSOCKET_FOREACH(so, so_next, udp)
1457 /* { */
1458#ifdef VBOX_WITH_SLIRP_MT
1459 if ( so->so_state & SS_NOFDREF
1460 && so->so_deleted == 1)
1461 {
1462 struct socket *son, *sop = NULL;
1463 QSOCKET_LOCK(udb);
1464 if (so->so_next != NULL)
1465 {
1466 if (so->so_next != &udb)
1467 SOCKET_LOCK(so->so_next);
1468 son = so->so_next;
1469 }
1470 if ( so->so_prev != &udb
1471 && so->so_prev != NULL)
1472 {
1473 SOCKET_LOCK(so->so_prev);
1474 sop = so->so_prev;
1475 }
1476 QSOCKET_UNLOCK(udb);
1477 remque(pData, so);
1478 NSOCK_DEC();
1479 SOCKET_UNLOCK(so);
1480 SOCKET_LOCK_DESTROY(so);
1481 RTMemFree(so);
1482 so_next = son;
1483 if (sop != NULL)
1484 SOCKET_UNLOCK(sop);
1485 CONTINUE_NO_UNLOCK(udp);
1486 }
1487#endif
1488#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
1489 if (so->so_cloneOf)
1490 CONTINUE_NO_UNLOCK(udp);
1491#endif
1492 POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
1493
1494 LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
1495
1496 if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
1497 {
1498 SORECVFROM(pData, so);
1499 }
1500 LOOP_LABEL(udp, so, so_next);
1501 }
1502
1503done:
1504
1505 STAM_PROFILE_STOP(&pData->StatPoll, a);
1506}
1507
1508
1509struct arphdr
1510{
1511 unsigned short ar_hrd; /* format of hardware address */
1512 unsigned short ar_pro; /* format of protocol address */
1513 unsigned char ar_hln; /* length of hardware address */
1514 unsigned char ar_pln; /* length of protocol address */
1515 unsigned short ar_op; /* ARP opcode (command) */
1516
1517 /*
1518 * Ethernet looks like this : This bit is variable sized however...
1519 */
1520 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
1521 unsigned char ar_sip[4]; /* sender IP address */
1522 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
1523 unsigned char ar_tip[4]; /* target IP address */
1524};
1525AssertCompileSize(struct arphdr, 28);
1526
1527static void arp_output(PNATState pData, const uint8_t *pcu8EtherSource, const struct arphdr *pcARPHeaderSource, uint32_t ip4TargetAddress)
1528{
1529 struct ethhdr *pEtherHeaderResponse;
1530 struct arphdr *pARPHeaderResponse;
1531 uint32_t ip4TargetAddressInHostFormat;
1532 struct mbuf *pMbufResponse;
1533
1534 Assert((pcu8EtherSource));
1535 if (!pcu8EtherSource)
1536 return;
1537 ip4TargetAddressInHostFormat = RT_N2H_U32(ip4TargetAddress);
1538
1539 pMbufResponse = m_getcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR);
1540 if (!pMbufResponse)
1541 return;
1542 pEtherHeaderResponse = mtod(pMbufResponse, struct ethhdr *);
1543 /* @note: if_encap will swap src and dst*/
1544 memcpy(pEtherHeaderResponse->h_source, pcu8EtherSource, ETH_ALEN);
1545 pMbufResponse->m_data += ETH_HLEN;
1546 pARPHeaderResponse = mtod(pMbufResponse, struct arphdr *);
1547 pMbufResponse->m_len = sizeof(struct arphdr);
1548
1549 pARPHeaderResponse->ar_hrd = RT_H2N_U16_C(1);
1550 pARPHeaderResponse->ar_pro = RT_H2N_U16_C(ETH_P_IP);
1551 pARPHeaderResponse->ar_hln = ETH_ALEN;
1552 pARPHeaderResponse->ar_pln = 4;
1553 pARPHeaderResponse->ar_op = RT_H2N_U16_C(ARPOP_REPLY);
1554 memcpy(pARPHeaderResponse->ar_sha, special_ethaddr, ETH_ALEN);
1555
1556 if (!slirpMbufTagService(pData, pMbufResponse, (uint8_t)(ip4TargetAddressInHostFormat & ~pData->netmask)))
1557 {
1558 static bool fTagErrorReported;
1559 if (!fTagErrorReported)
1560 {
1561 LogRel(("NAT: couldn't add the tag(PACKET_SERVICE:%d)\n",
1562 (uint8_t)(ip4TargetAddressInHostFormat & ~pData->netmask)));
1563 fTagErrorReported = true;
1564 }
1565 }
1566 pARPHeaderResponse->ar_sha[5] = (uint8_t)(ip4TargetAddressInHostFormat & ~pData->netmask);
1567
1568 memcpy(pARPHeaderResponse->ar_sip, pcARPHeaderSource->ar_tip, 4);
1569 memcpy(pARPHeaderResponse->ar_tha, pcARPHeaderSource->ar_sha, ETH_ALEN);
1570 memcpy(pARPHeaderResponse->ar_tip, pcARPHeaderSource->ar_sip, 4);
1571 if_encap(pData, ETH_P_ARP, pMbufResponse, ETH_ENCAP_URG);
1572}
1573/**
1574 * @note This function will free m!
1575 */
1576static void arp_input(PNATState pData, struct mbuf *m)
1577{
1578 struct ethhdr *pEtherHeader;
1579 struct arphdr *pARPHeader;
1580 uint32_t ip4TargetAddress;
1581
1582 int ar_op;
1583 pEtherHeader = mtod(m, struct ethhdr *);
1584 pARPHeader = (struct arphdr *)&pEtherHeader[1];
1585
1586 ar_op = RT_N2H_U16(pARPHeader->ar_op);
1587 ip4TargetAddress = *(uint32_t*)pARPHeader->ar_tip;
1588
1589 switch (ar_op)
1590 {
1591 case ARPOP_REQUEST:
1592 if ( CTL_CHECK(ip4TargetAddress, CTL_DNS)
1593 || CTL_CHECK(ip4TargetAddress, CTL_ALIAS)
1594 || CTL_CHECK(ip4TargetAddress, CTL_TFTP))
1595 arp_output(pData, pEtherHeader->h_source, pARPHeader, ip4TargetAddress);
1596
1597 /* Gratuitous ARP */
1598 if ( *(uint32_t *)pARPHeader->ar_sip == *(uint32_t *)pARPHeader->ar_tip
1599 && memcmp(pARPHeader->ar_tha, broadcast_ethaddr, ETH_ALEN) == 0
1600 && memcmp(pEtherHeader->h_dest, broadcast_ethaddr, ETH_ALEN) == 0)
1601 {
1602 /* We've received an announce about address assignment,
1603 * let's do an ARP cache update
1604 */
1605 static bool fGratuitousArpReported;
1606 if (!fGratuitousArpReported)
1607 {
1608 LogRel(("NAT: Gratuitous ARP [IP:%RTnaipv4, ether:%RTmac]\n",
1609 pARPHeader->ar_sip, pARPHeader->ar_sha));
1610 fGratuitousArpReported = true;
1611 }
1612 slirp_arp_cache_update_or_add(pData, *(uint32_t *)pARPHeader->ar_sip, &pARPHeader->ar_sha[0]);
1613 }
1614 break;
1615
1616 case ARPOP_REPLY:
1617 slirp_arp_cache_update_or_add(pData, *(uint32_t *)pARPHeader->ar_sip, &pARPHeader->ar_sha[0]);
1618 break;
1619
1620 default:
1621 break;
1622 }
1623
1624 m_freem(pData, m);
1625}
1626
1627/**
1628 * Feed a packet into the slirp engine.
1629 *
1630 * @param m Data buffer, m_len is not valid.
1631 * @param cbBuf The length of the data in m.
1632 */
1633void slirp_input(PNATState pData, struct mbuf *m, size_t cbBuf)
1634{
1635 int proto;
1636 static bool fWarnedIpv6;
1637 struct ethhdr *eh;
1638 uint8_t au8Ether[ETH_ALEN];
1639
1640 m->m_len = cbBuf;
1641 if (cbBuf < ETH_HLEN)
1642 {
1643 Log(("NAT: packet having size %d has been ignored\n", m->m_len));
1644 m_freem(pData, m);
1645 return;
1646 }
1647 eh = mtod(m, struct ethhdr *);
1648 proto = RT_N2H_U16(eh->h_proto);
1649
1650 memcpy(au8Ether, eh->h_source, ETH_ALEN);
1651
1652 switch(proto)
1653 {
1654 case ETH_P_ARP:
1655 arp_input(pData, m);
1656 break;
1657
1658 case ETH_P_IP:
1659 /* Update time. Important if the network is very quiet, as otherwise
1660 * the first outgoing connection gets an incorrect timestamp. */
1661 updtime(pData);
1662 m_adj(m, ETH_HLEN);
1663 M_ASSERTPKTHDR(m);
1664 m->m_pkthdr.header = mtod(m, void *);
1665 ip_input(pData, m);
1666 break;
1667
1668 case ETH_P_IPV6:
1669 m_freem(pData, m);
1670 if (!fWarnedIpv6)
1671 {
1672 LogRel(("NAT: IPv6 not supported\n"));
1673 fWarnedIpv6 = true;
1674 }
1675 break;
1676
1677 default:
1678 Log(("NAT: Unsupported protocol %x\n", proto));
1679 m_freem(pData, m);
1680 break;
1681 }
1682
1683 if (pData->cRedirectionsActive != pData->cRedirectionsStored)
1684 activate_port_forwarding(pData, au8Ether);
1685}
1686
1687/**
1688 * Output the IP packet to the ethernet device.
1689 *
1690 * @note This function will free m!
1691 */
1692void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m, int flags)
1693{
1694 struct ethhdr *eh;
1695 uint8_t *mbuf = NULL;
1696 size_t mlen = 0;
1697 STAM_PROFILE_START(&pData->StatIF_encap, a);
1698 LogFlowFunc(("ENTER: pData:%p, eth_proto:%RX16, m:%p, flags:%d\n",
1699 pData, eth_proto, m, flags));
1700
1701 M_ASSERTPKTHDR(m);
1702 m->m_data -= ETH_HLEN;
1703 m->m_len += ETH_HLEN;
1704 eh = mtod(m, struct ethhdr *);
1705 mlen = m->m_len;
1706
1707 if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) != 0)
1708 {
1709 struct m_tag *t = m_tag_first(m);
1710 uint8_t u8ServiceId = CTL_ALIAS;
1711 memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
1712 memcpy(eh->h_source, special_ethaddr, ETH_ALEN);
1713 Assert(memcmp(eh->h_dest, special_ethaddr, ETH_ALEN) != 0);
1714 if (memcmp(eh->h_dest, zerro_ethaddr, ETH_ALEN) == 0)
1715 {
1716 /* don't do anything */
1717 m_freem(pData, m);
1718 goto done;
1719 }
1720 if ( t
1721 && (t = m_tag_find(m, PACKET_SERVICE, NULL)))
1722 {
1723 Assert(t);
1724 u8ServiceId = *(uint8_t *)&t[1];
1725 }
1726 eh->h_source[5] = u8ServiceId;
1727 }
1728 /*
1729 * we're processing the chain, that isn't not expected.
1730 */
1731 Assert((!m->m_next));
1732 if (m->m_next)
1733 {
1734 Log(("NAT: if_encap's recived the chain, dropping...\n"));
1735 m_freem(pData, m);
1736 goto done;
1737 }
1738 mbuf = mtod(m, uint8_t *);
1739 eh->h_proto = RT_H2N_U16(eth_proto);
1740 LogFunc(("eh(dst:%RTmac, src:%RTmac)\n", eh->h_dest, eh->h_source));
1741 if (flags & ETH_ENCAP_URG)
1742 slirp_urg_output(pData->pvUser, m, mbuf, mlen);
1743 else
1744 slirp_output(pData->pvUser, m, mbuf, mlen);
1745done:
1746 STAM_PROFILE_STOP(&pData->StatIF_encap, a);
1747 LogFlowFuncLeave();
1748}
1749
1750/**
1751 * Still we're using dhcp server leasing to map ether to IP
1752 * @todo see rt_lookup_in_cache
1753 */
1754static uint32_t find_guest_ip(PNATState pData, const uint8_t *eth_addr)
1755{
1756 uint32_t ip = INADDR_ANY;
1757 int rc;
1758
1759 if (eth_addr == NULL)
1760 return INADDR_ANY;
1761
1762 if ( memcmp(eth_addr, zerro_ethaddr, ETH_ALEN) == 0
1763 || memcmp(eth_addr, broadcast_ethaddr, ETH_ALEN) == 0)
1764 return INADDR_ANY;
1765
1766 rc = slirp_arp_lookup_ip_by_ether(pData, eth_addr, &ip);
1767 if (RT_SUCCESS(rc))
1768 return ip;
1769
1770 bootp_cache_lookup_ip_by_ether(pData, eth_addr, &ip);
1771 /* ignore return code, ip will be set to INADDR_ANY on error */
1772 return ip;
1773}
1774
1775/**
1776 * We need check if we've activated port forwarding
1777 * for specific machine ... that of course relates to
1778 * service mode
1779 * @todo finish this for service case
1780 */
1781static void activate_port_forwarding(PNATState pData, const uint8_t *h_source)
1782{
1783 struct port_forward_rule *rule, *tmp;
1784 const uint8_t *pu8EthSource = h_source;
1785
1786 /* check mac here */
1787 LIST_FOREACH_SAFE(rule, &pData->port_forward_rule_head, list, tmp)
1788 {
1789 struct socket *so;
1790 struct alias_link *alias_link;
1791 struct libalias *lib;
1792 int flags;
1793 struct sockaddr sa;
1794 struct sockaddr_in *psin;
1795 socklen_t socketlen;
1796 struct in_addr alias;
1797 int rc;
1798 uint32_t guest_addr; /* need to understand if we already give address to guest */
1799
1800 if (rule->activated)
1801 continue;
1802
1803#ifdef VBOX_WITH_NAT_SERVICE
1804 /**
1805 * case when guest ip is INADDR_ANY shouldn't appear in NAT service
1806 */
1807 Assert((rule->guest_addr.s_addr != INADDR_ANY));
1808 guest_addr = rule->guest_addr.s_addr;
1809#else /* VBOX_WITH_NAT_SERVICE */
1810 guest_addr = find_guest_ip(pData, pu8EthSource);
1811#endif /* !VBOX_WITH_NAT_SERVICE */
1812 if (guest_addr == INADDR_ANY)
1813 {
1814 /* the address wasn't granted */
1815 return;
1816 }
1817
1818#if !defined(VBOX_WITH_NAT_SERVICE)
1819 if ( rule->guest_addr.s_addr != guest_addr
1820 && rule->guest_addr.s_addr != INADDR_ANY)
1821 continue;
1822 if (rule->guest_addr.s_addr == INADDR_ANY)
1823 rule->guest_addr.s_addr = guest_addr;
1824#endif
1825
1826 LogRel(("NAT: set redirect %s host port %d => guest port %d @ %RTnaipv4\n",
1827 rule->proto == IPPROTO_UDP ? "UDP" : "TCP", rule->host_port, rule->guest_port, guest_addr));
1828
1829 if (rule->proto == IPPROTO_UDP)
1830 so = udp_listen(pData, rule->bind_ip.s_addr, RT_H2N_U16(rule->host_port), guest_addr,
1831 RT_H2N_U16(rule->guest_port), 0);
1832 else
1833 so = solisten(pData, rule->bind_ip.s_addr, RT_H2N_U16(rule->host_port), guest_addr,
1834 RT_H2N_U16(rule->guest_port), 0);
1835
1836 if (so == NULL)
1837 goto remove_port_forwarding;
1838
1839 psin = (struct sockaddr_in *)&sa;
1840 psin->sin_family = AF_INET;
1841 psin->sin_port = 0;
1842 psin->sin_addr.s_addr = INADDR_ANY;
1843 socketlen = sizeof(struct sockaddr);
1844
1845 rc = getsockname(so->s, &sa, &socketlen);
1846 if (rc < 0 || sa.sa_family != AF_INET)
1847 goto remove_port_forwarding;
1848
1849 psin = (struct sockaddr_in *)&sa;
1850
1851 lib = LibAliasInit(pData, NULL);
1852 flags = LibAliasSetMode(lib, 0, 0);
1853 flags |= pData->i32AliasMode;
1854 flags |= PKT_ALIAS_REVERSE; /* set reverse */
1855 flags = LibAliasSetMode(lib, flags, ~0);
1856
1857 alias.s_addr = RT_H2N_U32(RT_N2H_U32(guest_addr) | CTL_ALIAS);
1858 alias_link = LibAliasRedirectPort(lib, psin->sin_addr, RT_H2N_U16(rule->host_port),
1859 alias, RT_H2N_U16(rule->guest_port),
1860 pData->special_addr, -1, /* not very clear for now */
1861 rule->proto);
1862 if (!alias_link)
1863 goto remove_port_forwarding;
1864
1865 so->so_la = lib;
1866 rule->activated = 1;
1867 rule->so = so;
1868 pData->cRedirectionsActive++;
1869 continue;
1870
1871 remove_port_forwarding:
1872 LogRel(("NAT: failed to redirect %s %d => %d\n",
1873 (rule->proto == IPPROTO_UDP?"UDP":"TCP"), rule->host_port, rule->guest_port));
1874 LIST_REMOVE(rule, list);
1875 pData->cRedirectionsStored--;
1876 RTMemFree(rule);
1877 }
1878}
1879
1880/**
1881 * Changes in 3.1 instead of opening new socket do the following:
1882 * gain more information:
1883 * 1. bind IP
1884 * 2. host port
1885 * 3. guest port
1886 * 4. proto
1887 * 5. guest MAC address
1888 * the guest's MAC address is rather important for service, but we easily
1889 * could get it from VM configuration in DrvNAT or Service, the idea is activating
1890 * corresponding port-forwarding
1891 */
1892int slirp_add_redirect(PNATState pData, int is_udp, struct in_addr host_addr, int host_port,
1893 struct in_addr guest_addr, int guest_port, const uint8_t *ethaddr)
1894{
1895 struct port_forward_rule *rule = NULL;
1896 LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
1897 {
1898 if ( rule->proto == (is_udp ? IPPROTO_UDP : IPPROTO_TCP)
1899 && rule->host_port == host_port
1900 && rule->bind_ip.s_addr == host_addr.s_addr
1901 && rule->guest_port == guest_port
1902 && rule->guest_addr.s_addr == guest_addr.s_addr
1903 )
1904 return 0; /* rule has been already registered */
1905 }
1906
1907 rule = RTMemAllocZ(sizeof(struct port_forward_rule));
1908 if (rule == NULL)
1909 return 1;
1910
1911 rule->proto = (is_udp ? IPPROTO_UDP : IPPROTO_TCP);
1912 rule->host_port = host_port;
1913 rule->guest_port = guest_port;
1914 rule->guest_addr.s_addr = guest_addr.s_addr;
1915 rule->bind_ip.s_addr = host_addr.s_addr;
1916 if (ethaddr != NULL)
1917 memcpy(rule->mac_address, ethaddr, ETH_ALEN);
1918 /* @todo add mac address */
1919 LIST_INSERT_HEAD(&pData->port_forward_rule_head, rule, list);
1920 pData->cRedirectionsStored++;
1921 /* activate port-forwarding if guest has already got assigned IP */
1922 if ( ethaddr
1923 && memcmp(ethaddr, zerro_ethaddr, ETH_ALEN))
1924 activate_port_forwarding(pData, ethaddr);
1925 return 0;
1926}
1927
1928int slirp_remove_redirect(PNATState pData, int is_udp, struct in_addr host_addr, int host_port,
1929 struct in_addr guest_addr, int guest_port)
1930{
1931 struct port_forward_rule *rule = NULL;
1932 LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
1933 {
1934 if ( rule->proto == (is_udp ? IPPROTO_UDP : IPPROTO_TCP)
1935 && rule->host_port == host_port
1936 && rule->guest_port == guest_port
1937 && rule->bind_ip.s_addr == host_addr.s_addr
1938 && rule->guest_addr.s_addr == guest_addr.s_addr
1939 && rule->activated)
1940 {
1941 LogRel(("NAT: remove redirect %s host port %d => guest port %d @ %RTnaipv4\n",
1942 rule->proto == IPPROTO_UDP ? "UDP" : "TCP", rule->host_port, rule->guest_port, guest_addr));
1943
1944 LibAliasUninit(rule->so->so_la);
1945 if (is_udp)
1946 udp_detach(pData, rule->so);
1947 else
1948 tcp_close(pData, sototcpcb(rule->so));
1949 LIST_REMOVE(rule, list);
1950 RTMemFree(rule);
1951 pData->cRedirectionsStored--;
1952 break;
1953 }
1954
1955 }
1956 return 0;
1957}
1958
1959void slirp_set_ethaddr_and_activate_port_forwarding(PNATState pData, const uint8_t *ethaddr, uint32_t GuestIP)
1960{
1961#ifndef VBOX_WITH_NAT_SERVICE
1962 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
1963#endif
1964 if (GuestIP != INADDR_ANY)
1965 {
1966 slirp_arp_cache_update_or_add(pData, GuestIP, ethaddr);
1967 activate_port_forwarding(pData, ethaddr);
1968 }
1969}
1970
1971#if defined(RT_OS_WINDOWS)
1972HANDLE *slirp_get_events(PNATState pData)
1973{
1974 return pData->phEvents;
1975}
1976void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
1977{
1978 pData->phEvents[index] = hEvent;
1979}
1980#endif
1981
1982unsigned int slirp_get_timeout_ms(PNATState pData)
1983{
1984 if (link_up)
1985 {
1986 if (time_fasttimo)
1987 return 2;
1988 if (do_slowtimo)
1989 return 500; /* see PR_SLOWHZ */
1990 }
1991 return 3600*1000; /* one hour */
1992}
1993
1994#ifndef RT_OS_WINDOWS
1995int slirp_get_nsock(PNATState pData)
1996{
1997 return pData->nsock;
1998}
1999#endif
2000
2001/*
2002 * this function called from NAT thread
2003 */
2004void slirp_post_sent(PNATState pData, void *pvArg)
2005{
2006 struct mbuf *m = (struct mbuf *)pvArg;
2007 m_freem(pData, m);
2008}
2009#ifdef VBOX_WITH_SLIRP_MT
2010void slirp_process_queue(PNATState pData)
2011{
2012 RTReqQueueProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
2013}
2014void *slirp_get_queue(PNATState pData)
2015{
2016 return pData->pReqQueue;
2017}
2018#endif
2019
2020void slirp_set_dhcp_TFTP_prefix(PNATState pData, const char *tftpPrefix)
2021{
2022 Log2(("tftp_prefix: %s\n", tftpPrefix));
2023 tftp_prefix = tftpPrefix;
2024}
2025
2026void slirp_set_dhcp_TFTP_bootfile(PNATState pData, const char *bootFile)
2027{
2028 Log2(("bootFile: %s\n", bootFile));
2029 bootp_filename = bootFile;
2030}
2031
2032void slirp_set_dhcp_next_server(PNATState pData, const char *next_server)
2033{
2034 Log2(("next_server: %s\n", next_server));
2035 if (next_server == NULL)
2036 pData->tftp_server.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_TFTP);
2037 else
2038 inet_aton(next_server, &pData->tftp_server);
2039}
2040
2041int slirp_set_binding_address(PNATState pData, char *addr)
2042{
2043 if (addr == NULL || (inet_aton(addr, &pData->bindIP) == 0))
2044 {
2045 pData->bindIP.s_addr = INADDR_ANY;
2046 return 1;
2047 }
2048 return 0;
2049}
2050
2051void slirp_set_dhcp_dns_proxy(PNATState pData, bool fDNSProxy)
2052{
2053 if (!pData->fUseHostResolver)
2054 {
2055 Log2(("NAT: DNS proxy switched %s\n", (fDNSProxy ? "on" : "off")));
2056 pData->fUseDnsProxy = fDNSProxy;
2057 }
2058 else if (fDNSProxy)
2059 LogRel(("NAT: Host Resolver conflicts with DNS proxy, the last one was forcely ignored\n"));
2060}
2061
2062#define CHECK_ARG(name, val, lim_min, lim_max) \
2063 do { \
2064 if ((val) < (lim_min) || (val) > (lim_max)) \
2065 { \
2066 LogRel(("NAT: (" #name ":%d) has been ignored, " \
2067 "because out of range (%d, %d)\n", (val), (lim_min), (lim_max))); \
2068 return; \
2069 } \
2070 else \
2071 LogRel(("NAT: (" #name ":%d)\n", (val))); \
2072 } while (0)
2073
2074void slirp_set_somaxconn(PNATState pData, int iSoMaxConn)
2075{
2076 LogFlowFunc(("iSoMaxConn:d\n", iSoMaxConn));
2077 /* Conditions */
2078 if (iSoMaxConn > SOMAXCONN)
2079 {
2080 LogRel(("NAT: value of somaxconn(%d) bigger than SOMAXCONN(%d)\n", iSoMaxConn, SOMAXCONN));
2081 iSoMaxConn = SOMAXCONN;
2082 }
2083
2084 if (iSoMaxConn < 1)
2085 {
2086 LogRel(("NAT: proposed value(%d) of somaxconn is invalid, default value is used (%d)\n", iSoMaxConn, pData->soMaxConn));
2087 LogFlowFuncLeave();
2088 return;
2089 }
2090
2091 /* Asignment */
2092 if (pData->soMaxConn != iSoMaxConn)
2093 {
2094 LogRel(("NAT: value of somaxconn has been changed from %d to %d\n",
2095 pData->soMaxConn, iSoMaxConn));
2096 pData->soMaxConn = iSoMaxConn;
2097 }
2098 LogFlowFuncLeave();
2099}
2100/* don't allow user set less 8kB and more than 1M values */
2101#define _8K_1M_CHECK_ARG(name, val) CHECK_ARG(name, (val), 8, 1024)
2102void slirp_set_rcvbuf(PNATState pData, int kilobytes)
2103{
2104 _8K_1M_CHECK_ARG("SOCKET_RCVBUF", kilobytes);
2105 pData->socket_rcv = kilobytes;
2106}
2107void slirp_set_sndbuf(PNATState pData, int kilobytes)
2108{
2109 _8K_1M_CHECK_ARG("SOCKET_SNDBUF", kilobytes);
2110 pData->socket_snd = kilobytes * _1K;
2111}
2112void slirp_set_tcp_rcvspace(PNATState pData, int kilobytes)
2113{
2114 _8K_1M_CHECK_ARG("TCP_RCVSPACE", kilobytes);
2115 tcp_rcvspace = kilobytes * _1K;
2116}
2117void slirp_set_tcp_sndspace(PNATState pData, int kilobytes)
2118{
2119 _8K_1M_CHECK_ARG("TCP_SNDSPACE", kilobytes);
2120 tcp_sndspace = kilobytes * _1K;
2121}
2122
2123/*
2124 * Looking for Ether by ip in ARP-cache
2125 * Note: it´s responsible of caller to allocate buffer for result
2126 * @returns iprt status code
2127 */
2128int slirp_arp_lookup_ether_by_ip(PNATState pData, uint32_t ip, uint8_t *ether)
2129{
2130 struct arp_cache_entry *ac;
2131
2132 if (ether == NULL)
2133 return VERR_INVALID_PARAMETER;
2134
2135 if (LIST_EMPTY(&pData->arp_cache))
2136 return VERR_NOT_FOUND;
2137
2138 LIST_FOREACH(ac, &pData->arp_cache, list)
2139 {
2140 if ( ac->ip == ip
2141 && memcmp(ac->ether, broadcast_ethaddr, ETH_ALEN) != 0)
2142 {
2143 memcpy(ether, ac->ether, ETH_ALEN);
2144 return VINF_SUCCESS;
2145 }
2146 }
2147 return VERR_NOT_FOUND;
2148}
2149
2150/*
2151 * Looking for IP by Ether in ARP-cache
2152 * Note: it´s responsible of caller to allocate buffer for result
2153 * @returns 0 - if found, 1 - otherwise
2154 */
2155int slirp_arp_lookup_ip_by_ether(PNATState pData, const uint8_t *ether, uint32_t *ip)
2156{
2157 struct arp_cache_entry *ac;
2158 *ip = INADDR_ANY;
2159
2160 if (LIST_EMPTY(&pData->arp_cache))
2161 return VERR_NOT_FOUND;
2162
2163 LIST_FOREACH(ac, &pData->arp_cache, list)
2164 {
2165 if (memcmp(ether, ac->ether, ETH_ALEN) == 0)
2166 {
2167 *ip = ac->ip;
2168 return VINF_SUCCESS;
2169 }
2170 }
2171 return VERR_NOT_FOUND;
2172}
2173
2174void slirp_arp_who_has(PNATState pData, uint32_t dst)
2175{
2176 struct mbuf *m;
2177 struct ethhdr *ehdr;
2178 struct arphdr *ahdr;
2179 static bool fWarned = false;
2180 LogFlowFunc(("ENTER: %RTnaipv4\n", dst));
2181
2182 /* ARP request WHO HAS 0.0.0.0 is one of the signals
2183 * that something has been broken at Slirp. Investigating
2184 * pcap dumps it's easy to miss warning ARP requests being
2185 * focused on investigation of other protocols flow.
2186 */
2187#ifdef DEBUG_vvl
2188 Assert((dst != INADDR_ANY));
2189 NOREF(fWarned);
2190#else
2191 if ( dst == INADDR_ANY
2192 && !fWarned)
2193 {
2194 LogRel(("NAT:ARP: \"WHO HAS INADDR_ANY\" request has been detected\n"));
2195 fWarned = true;
2196 }
2197#endif /* !DEBUG_vvl */
2198
2199 m = m_getcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR);
2200 if (m == NULL)
2201 {
2202 Log(("NAT: Can't alloc mbuf for ARP request\n"));
2203 LogFlowFuncLeave();
2204 return;
2205 }
2206 ehdr = mtod(m, struct ethhdr *);
2207 memset(ehdr->h_source, 0xff, ETH_ALEN);
2208 ahdr = (struct arphdr *)&ehdr[1];
2209 ahdr->ar_hrd = RT_H2N_U16_C(1);
2210 ahdr->ar_pro = RT_H2N_U16_C(ETH_P_IP);
2211 ahdr->ar_hln = ETH_ALEN;
2212 ahdr->ar_pln = 4;
2213 ahdr->ar_op = RT_H2N_U16_C(ARPOP_REQUEST);
2214 memcpy(ahdr->ar_sha, special_ethaddr, ETH_ALEN);
2215 /* we assume that this request come from gw, but not from DNS or TFTP */
2216 ahdr->ar_sha[5] = CTL_ALIAS;
2217 *(uint32_t *)ahdr->ar_sip = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
2218 memset(ahdr->ar_tha, 0xff, ETH_ALEN); /*broadcast*/
2219 *(uint32_t *)ahdr->ar_tip = dst;
2220 /* warn!!! should falls in mbuf minimal size */
2221 m->m_len = sizeof(struct arphdr) + ETH_HLEN;
2222 m->m_data += ETH_HLEN;
2223 m->m_len -= ETH_HLEN;
2224 if_encap(pData, ETH_P_ARP, m, ETH_ENCAP_URG);
2225 LogFlowFuncLeave();
2226}
2227#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
2228void slirp_add_host_resolver_mapping(PNATState pData, const char *pszHostName, const char *pszHostNamePattern, uint32_t u32HostIP)
2229{
2230 LogFlowFunc(("ENTER: pszHostName:%s, pszHostNamePattern:%s u32HostIP:%RTnaipv4\n",
2231 pszHostName ? pszHostName : "(null)",
2232 pszHostNamePattern ? pszHostNamePattern : "(null)",
2233 u32HostIP));
2234 if ( ( pszHostName
2235 || pszHostNamePattern)
2236 && u32HostIP != INADDR_ANY
2237 && u32HostIP != INADDR_BROADCAST)
2238 {
2239 PDNSMAPPINGENTRY pDnsMapping = RTMemAllocZ(sizeof(DNSMAPPINGENTRY));
2240 if (!pDnsMapping)
2241 {
2242 LogFunc(("Can't allocate DNSMAPPINGENTRY\n"));
2243 LogFlowFuncLeave();
2244 return;
2245 }
2246 pDnsMapping->u32IpAddress = u32HostIP;
2247 if (pszHostName)
2248 pDnsMapping->pszCName = RTStrDup(pszHostName);
2249 else if (pszHostNamePattern)
2250 pDnsMapping->pszPattern = RTStrDup(pszHostNamePattern);
2251 if ( !pDnsMapping->pszCName
2252 && !pDnsMapping->pszPattern)
2253 {
2254 LogFunc(("Can't allocate enough room for %s\n", pszHostName ? pszHostName : pszHostNamePattern));
2255 RTMemFree(pDnsMapping);
2256 LogFlowFuncLeave();
2257 return;
2258 }
2259 LIST_INSERT_HEAD(&pData->DNSMapHead, pDnsMapping, MapList);
2260 LogRel(("NAT: user-defined mapping %s: %RTnaipv4 is registered\n",
2261 pDnsMapping->pszCName ? pDnsMapping->pszCName : pDnsMapping->pszPattern,
2262 pDnsMapping->u32IpAddress));
2263 }
2264 LogFlowFuncLeave();
2265}
2266#endif
2267
2268/* updates the arp cache
2269 * @note: this is helper function, slirp_arp_cache_update_or_add should be used.
2270 * @returns 0 - if has found and updated
2271 * 1 - if hasn't found.
2272 */
2273static inline int slirp_arp_cache_update(PNATState pData, uint32_t dst, const uint8_t *mac)
2274{
2275 struct arp_cache_entry *ac;
2276 Assert(( memcmp(mac, broadcast_ethaddr, ETH_ALEN)
2277 && memcmp(mac, zerro_ethaddr, ETH_ALEN)));
2278 LIST_FOREACH(ac, &pData->arp_cache, list)
2279 {
2280 if (!memcmp(ac->ether, mac, ETH_ALEN))
2281 {
2282 ac->ip = dst;
2283 return 0;
2284 }
2285 }
2286 return 1;
2287}
2288
2289/**
2290 * add entry to the arp cache
2291 * @note: this is helper function, slirp_arp_cache_update_or_add should be used.
2292 */
2293static inline void slirp_arp_cache_add(PNATState pData, uint32_t ip, const uint8_t *ether)
2294{
2295 struct arp_cache_entry *ac = NULL;
2296 Assert(( memcmp(ether, broadcast_ethaddr, ETH_ALEN)
2297 && memcmp(ether, zerro_ethaddr, ETH_ALEN)));
2298 ac = RTMemAllocZ(sizeof(struct arp_cache_entry));
2299 if (ac == NULL)
2300 {
2301 Log(("NAT: Can't allocate arp cache entry\n"));
2302 return;
2303 }
2304 ac->ip = ip;
2305 memcpy(ac->ether, ether, ETH_ALEN);
2306 LIST_INSERT_HEAD(&pData->arp_cache, ac, list);
2307}
2308
2309/* updates or adds entry to the arp cache
2310 * @returns 0 - if has found and updated
2311 * 1 - if hasn't found.
2312 */
2313int slirp_arp_cache_update_or_add(PNATState pData, uint32_t dst, const uint8_t *mac)
2314{
2315 if ( !memcmp(mac, broadcast_ethaddr, ETH_ALEN)
2316 || !memcmp(mac, zerro_ethaddr, ETH_ALEN))
2317 {
2318 static bool fBroadcastEtherAddReported;
2319 if (!fBroadcastEtherAddReported)
2320 {
2321 LogRel(("NAT: Attempt to add pair [%RTmac:%RTnaipv4] in ARP cache was ignored\n",
2322 mac, dst));
2323 fBroadcastEtherAddReported = true;
2324 }
2325 return 1;
2326 }
2327 if (slirp_arp_cache_update(pData, dst, mac))
2328 slirp_arp_cache_add(pData, dst, mac);
2329
2330 return 0;
2331}
2332
2333
2334void slirp_set_mtu(PNATState pData, int mtu)
2335{
2336 if (mtu < 20 || mtu >= 16000)
2337 {
2338 LogRel(("NAT: mtu(%d) is out of range (20;16000] mtu forcely assigned to 1500\n", mtu));
2339 mtu = 1500;
2340 }
2341 /* MTU is maximum transition unit on */
2342 if_mtu =
2343 if_mru = mtu;
2344}
2345
2346/**
2347 * Info handler.
2348 */
2349void slirp_info(PNATState pData, PCDBGFINFOHLP pHlp, const char *pszArgs)
2350{
2351 struct socket *so, *so_next;
2352 struct arp_cache_entry *ac;
2353 struct port_forward_rule *rule;
2354 NOREF(pszArgs);
2355
2356 pHlp->pfnPrintf(pHlp, "NAT parameters: MTU=%d\n", if_mtu);
2357 pHlp->pfnPrintf(pHlp, "NAT TCP ports:\n");
2358 QSOCKET_FOREACH(so, so_next, tcp)
2359 /* { */
2360 pHlp->pfnPrintf(pHlp, " %R[natsock]\n", so);
2361 }
2362
2363 pHlp->pfnPrintf(pHlp, "NAT UDP ports:\n");
2364 QSOCKET_FOREACH(so, so_next, udp)
2365 /* { */
2366 pHlp->pfnPrintf(pHlp, " %R[natsock]\n", so);
2367 }
2368
2369 pHlp->pfnPrintf(pHlp, "NAT ARP cache:\n");
2370 LIST_FOREACH(ac, &pData->arp_cache, list)
2371 {
2372 pHlp->pfnPrintf(pHlp, " %RTnaipv4 %RTmac\n", ac->ip, &ac->ether);
2373 }
2374
2375 pHlp->pfnPrintf(pHlp, "NAT rules:\n");
2376 LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
2377 {
2378 pHlp->pfnPrintf(pHlp, " %s %d => %RTnaipv4:%d %c\n",
2379 rule->proto == IPPROTO_UDP ? "UDP" : "TCP",
2380 rule->host_port, rule->guest_addr.s_addr, rule->guest_port,
2381 rule->activated ? ' ' : '*');
2382 }
2383}
Note: See TracBrowser for help on using the repository browser.

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