VirtualBox

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

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

NAT: don't compile else branch if logging is disabled.

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

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