VirtualBox

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

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

NAT: english

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