VirtualBox

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

Last change on this file since 36943 was 36943, checked in by vboxsync, 14 years ago

NAT: correct ar_sha format in who-is requests initiated by Slirp.

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