VirtualBox

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

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

NAT: dump more inforamtion about the socket if error is appear.

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

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