VirtualBox

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

Last change on this file since 52343 was 52283, checked in by vboxsync, 10 years ago

NAT: Do pay attention to POLLERR and WSANETWORKEVENTS::iErrorCode and
abort TCP connections on socket errors. That makes RST propagated to
the guest properly (tickets 10525, 11696).

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