VirtualBox

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

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

NAT: G/c socket:so_la - per-socket libalias instance used for
port-forwarding - and concomitant code. It is never actually used as
PACKET_TAG_ALIAS that is supposed to select it is never set. Its
LibAliasRedirectPort() arguments were wrong too.

  • 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 52755 2014-09-15 21:31:50Z 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 if (so->so_state & SS_ISFCONNECTING)
970 {
971 bool reject = false;
972#if !defined(RT_OS_WINDOWS)
973 {
974 /*
975 * Failed connect(2) is reported by poll(2) on
976 * different OSes with different combinations of
977 * POLLERR, POLLHUP, and POLLOUT.
978 */
979 if ( CHECK_FD_SET(so, NetworkEvents, closefds) /* POLLHUP */
980 || CHECK_FD_SET(so, NetworkEvents, rderr)) /* POLLERR */
981 {
982 reject = true;
983 }
984#if defined(RT_OS_SOLARIS) || defined(RT_OS_NETBSD)
985 else if (CHECK_FD_SET(so, NetworkEvents, writefds)) /* POLLOUT */
986 {
987 /* Solaris and NetBSD report plain POLLOUT even on error */
988 int sockerr;
989 socklen_t optlen = (socklen_t)sizeof(sockerr);
990 ret = getsockopt(so->s, SOL_SOCKET, SO_ERROR, &sockerr, &optlen);
991 if (ret < 0)
992 sockerr = ENETDOWN;
993 if (sockerr != 0)
994 reject = true;
995 }
996#endif
997 }
998#else /* RT_OS_WINDOWS */
999 {
1000 /*
1001 * XXX: CHECK_FD_SET is defined to return false when
1002 * there's an error pending, which makes it unusable
1003 * here.
1004 */
1005 if ( (NetworkEvents.lNetworkEvents & FD_CONNECT)
1006 && NetworkEvents.iErrorCode[FD_CONNECT_BIT] != 0)
1007 {
1008 reject = true;
1009 }
1010 }
1011#endif
1012 if (reject)
1013 {
1014 /* "continue" tcp_input() to reject connection from guest */
1015 so->so_state = SS_NOFDREF;
1016 TCP_INPUT(pData, NULL, 0, so);
1017 ret = slirpVerifyAndFreeSocket(pData, so);
1018 Assert(ret == 1); /* freed */
1019 CONTINUE(tcp);
1020 }
1021
1022 /*
1023 * XXX: For now just fall through to the old code to
1024 * handle successful connect(2).
1025 */
1026 }
1027
1028 /*
1029 * Check for URG data
1030 * This will soread as well, so no need to
1031 * test for readfds below if this succeeds
1032 */
1033
1034 /* out-of-band data */
1035 if ( CHECK_FD_SET(so, NetworkEvents, xfds)
1036#ifdef RT_OS_DARWIN
1037 /* Darwin and probably BSD hosts generates POLLPRI|POLLHUP event on receiving TCP.flags.{ACK|URG|FIN} this
1038 * combination on other Unixs hosts doesn't enter to this branch
1039 */
1040 && !CHECK_FD_SET(so, NetworkEvents, closefds)
1041#endif
1042#ifdef RT_OS_WINDOWS
1043 /**
1044 * In some cases FD_CLOSE comes with FD_OOB, that confuse tcp processing.
1045 */
1046 && !WIN_CHECK_FD_SET(so, NetworkEvents, closefds)
1047#endif
1048 )
1049 {
1050 sorecvoob(pData, so);
1051 if (slirpVerifyAndFreeSocket(pData, so))
1052 CONTINUE(tcp);
1053 }
1054
1055 /*
1056 * Check sockets for reading
1057 */
1058 else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
1059 || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
1060 {
1061
1062#ifdef RT_OS_WINDOWS
1063 if (WIN_CHECK_FD_SET(so, NetworkEvents, connectfds))
1064 {
1065 /* Finish connection first */
1066 /* should we ignore return value? */
1067 bool fRet = slirpConnectOrWrite(pData, so, true);
1068 LogFunc(("fRet:%RTbool\n", fRet));
1069 if (slirpVerifyAndFreeSocket(pData, so))
1070 CONTINUE(tcp);
1071 }
1072#endif
1073 /*
1074 * Check for incoming connections
1075 */
1076 if (so->so_state & SS_FACCEPTCONN)
1077 {
1078 TCP_CONNECT(pData, so);
1079 if (slirpVerifyAndFreeSocket(pData, so))
1080 CONTINUE(tcp);
1081 if (!CHECK_FD_SET(so, NetworkEvents, closefds))
1082 {
1083 so->fUnderPolling = 0;
1084 CONTINUE(tcp);
1085 }
1086 }
1087
1088 ret = soread(pData, so);
1089 if (slirpVerifyAndFreeSocket(pData, so))
1090 CONTINUE(tcp);
1091 /* Output it if we read something */
1092 if (RT_LIKELY(ret > 0))
1093 TCP_OUTPUT(pData, sototcpcb(so));
1094
1095 if (slirpVerifyAndFreeSocket(pData, so))
1096 CONTINUE(tcp);
1097 }
1098
1099 /*
1100 * Check for FD_CLOSE events.
1101 * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
1102 */
1103 if ( CHECK_FD_SET(so, NetworkEvents, closefds)
1104 || (so->so_close == 1))
1105 {
1106 /*
1107 * drain the socket
1108 */
1109 for (; so_next->so_prev == so
1110 && !slirpVerifyAndFreeSocket(pData, so);)
1111 {
1112 ret = soread(pData, so);
1113 if (slirpVerifyAndFreeSocket(pData, so))
1114 break;
1115
1116 if (ret > 0)
1117 TCP_OUTPUT(pData, sototcpcb(so));
1118 else if (so_next->so_prev == so)
1119 {
1120 Log2(("%R[natsock] errno %d (%s)\n", so, errno, strerror(errno)));
1121 break;
1122 }
1123 }
1124
1125 /* if socket freed ''so'' is PHANTOM and next socket isn't points on it */
1126 if (so_next->so_prev == so)
1127 {
1128 /* mark the socket for termination _after_ it was drained */
1129 so->so_close = 1;
1130 /* No idea about Windows but on Posix, POLLHUP means that we can't send more.
1131 * Actually in the specific error scenario, POLLERR is set as well. */
1132#ifndef RT_OS_WINDOWS
1133 if (CHECK_FD_SET(so, NetworkEvents, rderr))
1134 sofcantsendmore(so);
1135#endif
1136 }
1137 if (so_next->so_prev == so)
1138 so->fUnderPolling = 0;
1139 CONTINUE(tcp);
1140 }
1141
1142 /*
1143 * Check sockets for writing
1144 */
1145 if ( CHECK_FD_SET(so, NetworkEvents, writefds)
1146#ifdef RT_OS_WINDOWS
1147 || WIN_CHECK_FD_SET(so, NetworkEvents, connectfds)
1148#endif
1149 )
1150 {
1151 int fConnectOrWriteSuccess = slirpConnectOrWrite(pData, so, false);
1152 /* slirpConnectOrWrite could return true even if tcp_input called tcp_drop,
1153 * so we should be ready to such situations.
1154 */
1155 if (slirpVerifyAndFreeSocket(pData, so))
1156 CONTINUE(tcp);
1157 else if (!fConnectOrWriteSuccess)
1158 {
1159 so->fUnderPolling = 0;
1160 CONTINUE(tcp);
1161 }
1162 /* slirpConnectionOrWrite succeeded and socket wasn't dropped */
1163 }
1164
1165 /*
1166 * Probe a still-connecting, non-blocking socket
1167 * to check if it's still alive
1168 */
1169#ifdef PROBE_CONN
1170 if (so->so_state & SS_ISFCONNECTING)
1171 {
1172 ret = recv(so->s, (char *)&ret, 0, 0);
1173
1174 if (ret < 0)
1175 {
1176 /* XXX */
1177 if ( soIgnorableErrorCode(errno)
1178 || errno == ENOTCONN)
1179 {
1180 CONTINUE(tcp); /* Still connecting, continue */
1181 }
1182
1183 /* else failed */
1184 so->so_state = SS_NOFDREF;
1185
1186 /* tcp_input will take care of it */
1187 }
1188 else
1189 {
1190 ret = send(so->s, &ret, 0, 0);
1191 if (ret < 0)
1192 {
1193 /* XXX */
1194 if ( soIgnorableErrorCode(errno)
1195 || errno == ENOTCONN)
1196 {
1197 CONTINUE(tcp);
1198 }
1199 /* else failed */
1200 so->so_state = SS_NOFDREF;
1201 }
1202 else
1203 so->so_state &= ~SS_ISFCONNECTING;
1204
1205 }
1206 TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
1207 } /* SS_ISFCONNECTING */
1208#endif
1209 if (!slirpVerifyAndFreeSocket(pData, so))
1210 so->fUnderPolling = 0;
1211 LOOP_LABEL(tcp, so, so_next);
1212 }
1213
1214 /*
1215 * Now UDP sockets.
1216 * Incoming packets are sent straight away, they're not buffered.
1217 * Incoming UDP data isn't buffered either.
1218 */
1219 QSOCKET_FOREACH(so, so_next, udp)
1220 /* { */
1221#ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
1222 if (so->so_cloneOf)
1223 CONTINUE_NO_UNLOCK(udp);
1224#endif
1225#if 0
1226 so->fUnderPolling = 1;
1227 if(slirpVerifyAndFreeSocket(pData, so));
1228 CONTINUE(udp);
1229 so->fUnderPolling = 0;
1230#endif
1231
1232 POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
1233
1234 LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
1235
1236 if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
1237 {
1238 SORECVFROM(pData, so);
1239 }
1240 LOOP_LABEL(udp, so, so_next);
1241 }
1242
1243done:
1244
1245 STAM_PROFILE_STOP(&pData->StatPoll, a);
1246}
1247
1248
1249struct arphdr
1250{
1251 unsigned short ar_hrd; /* format of hardware address */
1252 unsigned short ar_pro; /* format of protocol address */
1253 unsigned char ar_hln; /* length of hardware address */
1254 unsigned char ar_pln; /* length of protocol address */
1255 unsigned short ar_op; /* ARP opcode (command) */
1256
1257 /*
1258 * Ethernet looks like this : This bit is variable sized however...
1259 */
1260 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
1261 unsigned char ar_sip[4]; /* sender IP address */
1262 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
1263 unsigned char ar_tip[4]; /* target IP address */
1264};
1265AssertCompileSize(struct arphdr, 28);
1266
1267static void arp_output(PNATState pData, const uint8_t *pcu8EtherSource, const struct arphdr *pcARPHeaderSource, uint32_t ip4TargetAddress)
1268{
1269 struct ethhdr *pEtherHeaderResponse;
1270 struct arphdr *pARPHeaderResponse;
1271 uint32_t ip4TargetAddressInHostFormat;
1272 struct mbuf *pMbufResponse;
1273
1274 Assert((pcu8EtherSource));
1275 if (!pcu8EtherSource)
1276 return;
1277 ip4TargetAddressInHostFormat = RT_N2H_U32(ip4TargetAddress);
1278
1279 pMbufResponse = m_getcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR);
1280 if (!pMbufResponse)
1281 return;
1282 pEtherHeaderResponse = mtod(pMbufResponse, struct ethhdr *);
1283 /* @note: if_encap will swap src and dst*/
1284 memcpy(pEtherHeaderResponse->h_source, pcu8EtherSource, ETH_ALEN);
1285 pMbufResponse->m_data += ETH_HLEN;
1286 pARPHeaderResponse = mtod(pMbufResponse, struct arphdr *);
1287 pMbufResponse->m_len = sizeof(struct arphdr);
1288
1289 pARPHeaderResponse->ar_hrd = RT_H2N_U16_C(1);
1290 pARPHeaderResponse->ar_pro = RT_H2N_U16_C(ETH_P_IP);
1291 pARPHeaderResponse->ar_hln = ETH_ALEN;
1292 pARPHeaderResponse->ar_pln = 4;
1293 pARPHeaderResponse->ar_op = RT_H2N_U16_C(ARPOP_REPLY);
1294 memcpy(pARPHeaderResponse->ar_sha, special_ethaddr, ETH_ALEN);
1295
1296 if (!slirpMbufTagService(pData, pMbufResponse, (uint8_t)(ip4TargetAddressInHostFormat & ~pData->netmask)))
1297 {
1298 static bool fTagErrorReported;
1299 if (!fTagErrorReported)
1300 {
1301 LogRel(("NAT: couldn't add the tag(PACKET_SERVICE:%d)\n",
1302 (uint8_t)(ip4TargetAddressInHostFormat & ~pData->netmask)));
1303 fTagErrorReported = true;
1304 }
1305 }
1306 pARPHeaderResponse->ar_sha[5] = (uint8_t)(ip4TargetAddressInHostFormat & ~pData->netmask);
1307
1308 memcpy(pARPHeaderResponse->ar_sip, pcARPHeaderSource->ar_tip, 4);
1309 memcpy(pARPHeaderResponse->ar_tha, pcARPHeaderSource->ar_sha, ETH_ALEN);
1310 memcpy(pARPHeaderResponse->ar_tip, pcARPHeaderSource->ar_sip, 4);
1311 if_encap(pData, ETH_P_ARP, pMbufResponse, ETH_ENCAP_URG);
1312}
1313/**
1314 * @note This function will free m!
1315 */
1316static void arp_input(PNATState pData, struct mbuf *m)
1317{
1318 struct ethhdr *pEtherHeader;
1319 struct arphdr *pARPHeader;
1320 uint32_t ip4TargetAddress;
1321
1322 int ar_op;
1323 pEtherHeader = mtod(m, struct ethhdr *);
1324 pARPHeader = (struct arphdr *)&pEtherHeader[1];
1325
1326 ar_op = RT_N2H_U16(pARPHeader->ar_op);
1327 ip4TargetAddress = *(uint32_t*)pARPHeader->ar_tip;
1328
1329 switch (ar_op)
1330 {
1331 case ARPOP_REQUEST:
1332 if ( CTL_CHECK(ip4TargetAddress, CTL_DNS)
1333 || CTL_CHECK(ip4TargetAddress, CTL_ALIAS)
1334 || CTL_CHECK(ip4TargetAddress, CTL_TFTP))
1335 arp_output(pData, pEtherHeader->h_source, pARPHeader, ip4TargetAddress);
1336
1337 /* Gratuitous ARP */
1338 if ( *(uint32_t *)pARPHeader->ar_sip == *(uint32_t *)pARPHeader->ar_tip
1339 && memcmp(pARPHeader->ar_tha, broadcast_ethaddr, ETH_ALEN) == 0
1340 && memcmp(pEtherHeader->h_dest, broadcast_ethaddr, ETH_ALEN) == 0)
1341 {
1342 /* We've received an announce about address assignment,
1343 * let's do an ARP cache update
1344 */
1345 static bool fGratuitousArpReported;
1346 if (!fGratuitousArpReported)
1347 {
1348 LogRel(("NAT: Gratuitous ARP [IP:%RTnaipv4, ether:%RTmac]\n",
1349 pARPHeader->ar_sip, pARPHeader->ar_sha));
1350 fGratuitousArpReported = true;
1351 }
1352 slirp_arp_cache_update_or_add(pData, *(uint32_t *)pARPHeader->ar_sip, &pARPHeader->ar_sha[0]);
1353 }
1354 break;
1355
1356 case ARPOP_REPLY:
1357 slirp_arp_cache_update_or_add(pData, *(uint32_t *)pARPHeader->ar_sip, &pARPHeader->ar_sha[0]);
1358 break;
1359
1360 default:
1361 break;
1362 }
1363
1364 m_freem(pData, m);
1365}
1366
1367/**
1368 * Feed a packet into the slirp engine.
1369 *
1370 * @param m Data buffer, m_len is not valid.
1371 * @param cbBuf The length of the data in m.
1372 */
1373void slirp_input(PNATState pData, struct mbuf *m, size_t cbBuf)
1374{
1375 int proto;
1376 static bool fWarnedIpv6;
1377 struct ethhdr *eh;
1378 uint8_t au8Ether[ETH_ALEN];
1379
1380 m->m_len = cbBuf;
1381 if (cbBuf < ETH_HLEN)
1382 {
1383 Log(("NAT: packet having size %d has been ignored\n", m->m_len));
1384 m_freem(pData, m);
1385 return;
1386 }
1387 eh = mtod(m, struct ethhdr *);
1388 proto = RT_N2H_U16(eh->h_proto);
1389
1390 memcpy(au8Ether, eh->h_source, ETH_ALEN);
1391
1392 switch(proto)
1393 {
1394 case ETH_P_ARP:
1395 arp_input(pData, m);
1396 break;
1397
1398 case ETH_P_IP:
1399 /* Update time. Important if the network is very quiet, as otherwise
1400 * the first outgoing connection gets an incorrect timestamp. */
1401 updtime(pData);
1402 m_adj(m, ETH_HLEN);
1403 M_ASSERTPKTHDR(m);
1404 m->m_pkthdr.header = mtod(m, void *);
1405 ip_input(pData, m);
1406 break;
1407
1408 case ETH_P_IPV6:
1409 m_freem(pData, m);
1410 if (!fWarnedIpv6)
1411 {
1412 LogRel(("NAT: IPv6 not supported\n"));
1413 fWarnedIpv6 = true;
1414 }
1415 break;
1416
1417 default:
1418 Log(("NAT: Unsupported protocol %x\n", proto));
1419 m_freem(pData, m);
1420 break;
1421 }
1422
1423 if (pData->cRedirectionsActive != pData->cRedirectionsStored)
1424 activate_port_forwarding(pData, au8Ether);
1425}
1426
1427/**
1428 * Output the IP packet to the ethernet device.
1429 *
1430 * @note This function will free m!
1431 */
1432void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m, int flags)
1433{
1434 struct ethhdr *eh;
1435 uint8_t *mbuf = NULL;
1436 size_t mlen = 0;
1437 STAM_PROFILE_START(&pData->StatIF_encap, a);
1438 LogFlowFunc(("ENTER: pData:%p, eth_proto:%RX16, m:%p, flags:%d\n",
1439 pData, eth_proto, m, flags));
1440
1441 M_ASSERTPKTHDR(m);
1442 m->m_data -= ETH_HLEN;
1443 m->m_len += ETH_HLEN;
1444 eh = mtod(m, struct ethhdr *);
1445 mlen = m->m_len;
1446
1447 if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) != 0)
1448 {
1449 struct m_tag *t = m_tag_first(m);
1450 uint8_t u8ServiceId = CTL_ALIAS;
1451 memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
1452 memcpy(eh->h_source, special_ethaddr, ETH_ALEN);
1453 Assert(memcmp(eh->h_dest, special_ethaddr, ETH_ALEN) != 0);
1454 if (memcmp(eh->h_dest, zerro_ethaddr, ETH_ALEN) == 0)
1455 {
1456 /* don't do anything */
1457 m_freem(pData, m);
1458 goto done;
1459 }
1460 if ( t
1461 && (t = m_tag_find(m, PACKET_SERVICE, NULL)))
1462 {
1463 Assert(t);
1464 u8ServiceId = *(uint8_t *)&t[1];
1465 }
1466 eh->h_source[5] = u8ServiceId;
1467 }
1468 /*
1469 * we're processing the chain, that isn't not expected.
1470 */
1471 Assert((!m->m_next));
1472 if (m->m_next)
1473 {
1474 Log(("NAT: if_encap's recived the chain, dropping...\n"));
1475 m_freem(pData, m);
1476 goto done;
1477 }
1478 mbuf = mtod(m, uint8_t *);
1479 eh->h_proto = RT_H2N_U16(eth_proto);
1480 LogFunc(("eh(dst:%RTmac, src:%RTmac)\n", eh->h_dest, eh->h_source));
1481 if (flags & ETH_ENCAP_URG)
1482 slirp_urg_output(pData->pvUser, m, mbuf, mlen);
1483 else
1484 slirp_output(pData->pvUser, m, mbuf, mlen);
1485done:
1486 STAM_PROFILE_STOP(&pData->StatIF_encap, a);
1487 LogFlowFuncLeave();
1488}
1489
1490/**
1491 * Still we're using dhcp server leasing to map ether to IP
1492 * @todo see rt_lookup_in_cache
1493 */
1494static uint32_t find_guest_ip(PNATState pData, const uint8_t *eth_addr)
1495{
1496 uint32_t ip = INADDR_ANY;
1497 int rc;
1498
1499 if (eth_addr == NULL)
1500 return INADDR_ANY;
1501
1502 if ( memcmp(eth_addr, zerro_ethaddr, ETH_ALEN) == 0
1503 || memcmp(eth_addr, broadcast_ethaddr, ETH_ALEN) == 0)
1504 return INADDR_ANY;
1505
1506 rc = slirp_arp_lookup_ip_by_ether(pData, eth_addr, &ip);
1507 if (RT_SUCCESS(rc))
1508 return ip;
1509
1510 bootp_cache_lookup_ip_by_ether(pData, eth_addr, &ip);
1511 /* ignore return code, ip will be set to INADDR_ANY on error */
1512 return ip;
1513}
1514
1515/**
1516 * We need check if we've activated port forwarding
1517 * for specific machine ... that of course relates to
1518 * service mode
1519 * @todo finish this for service case
1520 */
1521static void activate_port_forwarding(PNATState pData, const uint8_t *h_source)
1522{
1523 struct port_forward_rule *rule, *tmp;
1524 const uint8_t *pu8EthSource = h_source;
1525
1526 /* check mac here */
1527 LIST_FOREACH_SAFE(rule, &pData->port_forward_rule_head, list, tmp)
1528 {
1529 struct socket *so;
1530 struct sockaddr sa;
1531 struct sockaddr_in *psin;
1532 socklen_t socketlen;
1533 int rc;
1534 uint32_t guest_addr; /* need to understand if we already give address to guest */
1535
1536 if (rule->activated)
1537 continue;
1538
1539#ifdef VBOX_WITH_NAT_SERVICE
1540 /**
1541 * case when guest ip is INADDR_ANY shouldn't appear in NAT service
1542 */
1543 Assert((rule->guest_addr.s_addr != INADDR_ANY));
1544 guest_addr = rule->guest_addr.s_addr;
1545#else /* VBOX_WITH_NAT_SERVICE */
1546 guest_addr = find_guest_ip(pData, pu8EthSource);
1547#endif /* !VBOX_WITH_NAT_SERVICE */
1548 if (guest_addr == INADDR_ANY)
1549 {
1550 /* the address wasn't granted */
1551 return;
1552 }
1553
1554#if !defined(VBOX_WITH_NAT_SERVICE)
1555 if ( rule->guest_addr.s_addr != guest_addr
1556 && rule->guest_addr.s_addr != INADDR_ANY)
1557 continue;
1558 if (rule->guest_addr.s_addr == INADDR_ANY)
1559 rule->guest_addr.s_addr = guest_addr;
1560#endif
1561
1562 LogRel(("NAT: set redirect %s host %RTnaipv4:%d => guest %RTnaipv4:%d\n",
1563 rule->proto == IPPROTO_UDP ? "UDP" : "TCP",
1564 rule->bind_ip.s_addr, rule->host_port,
1565 guest_addr, rule->guest_port));
1566
1567 if (rule->proto == IPPROTO_UDP)
1568 so = udp_listen(pData, rule->bind_ip.s_addr, RT_H2N_U16(rule->host_port), guest_addr,
1569 RT_H2N_U16(rule->guest_port), 0);
1570 else
1571 so = solisten(pData, rule->bind_ip.s_addr, RT_H2N_U16(rule->host_port), guest_addr,
1572 RT_H2N_U16(rule->guest_port), 0);
1573
1574 if (so == NULL)
1575 goto remove_port_forwarding;
1576
1577 psin = (struct sockaddr_in *)&sa;
1578 psin->sin_family = AF_INET;
1579 psin->sin_port = 0;
1580 psin->sin_addr.s_addr = INADDR_ANY;
1581 socketlen = sizeof(struct sockaddr);
1582
1583 rc = getsockname(so->s, &sa, &socketlen);
1584 if (rc < 0 || sa.sa_family != AF_INET)
1585 goto remove_port_forwarding;
1586
1587 rule->activated = 1;
1588 rule->so = so;
1589 pData->cRedirectionsActive++;
1590 continue;
1591
1592 remove_port_forwarding:
1593 LogRel(("NAT: failed to redirect %s %RTnaipv4:%d => %RTnaipv4:%d\n",
1594 (rule->proto == IPPROTO_UDP ? "UDP" : "TCP"),
1595 rule->bind_ip.s_addr, rule->host_port,
1596 guest_addr, rule->guest_port));
1597 LIST_REMOVE(rule, list);
1598 pData->cRedirectionsStored--;
1599 RTMemFree(rule);
1600 }
1601}
1602
1603/**
1604 * Changes in 3.1 instead of opening new socket do the following:
1605 * gain more information:
1606 * 1. bind IP
1607 * 2. host port
1608 * 3. guest port
1609 * 4. proto
1610 * 5. guest MAC address
1611 * the guest's MAC address is rather important for service, but we easily
1612 * could get it from VM configuration in DrvNAT or Service, the idea is activating
1613 * corresponding port-forwarding
1614 */
1615int slirp_add_redirect(PNATState pData, int is_udp, struct in_addr host_addr, int host_port,
1616 struct in_addr guest_addr, int guest_port, const uint8_t *ethaddr)
1617{
1618 struct port_forward_rule *rule = NULL;
1619 LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
1620 {
1621 if ( rule->proto == (is_udp ? IPPROTO_UDP : IPPROTO_TCP)
1622 && rule->host_port == host_port
1623 && rule->bind_ip.s_addr == host_addr.s_addr
1624 && rule->guest_port == guest_port
1625 && rule->guest_addr.s_addr == guest_addr.s_addr
1626 )
1627 return 0; /* rule has been already registered */
1628 }
1629
1630 rule = RTMemAllocZ(sizeof(struct port_forward_rule));
1631 if (rule == NULL)
1632 return 1;
1633
1634 rule->proto = (is_udp ? IPPROTO_UDP : IPPROTO_TCP);
1635 rule->host_port = host_port;
1636 rule->guest_port = guest_port;
1637 rule->guest_addr.s_addr = guest_addr.s_addr;
1638 rule->bind_ip.s_addr = host_addr.s_addr;
1639 if (ethaddr != NULL)
1640 memcpy(rule->mac_address, ethaddr, ETH_ALEN);
1641 /* @todo add mac address */
1642 LIST_INSERT_HEAD(&pData->port_forward_rule_head, rule, list);
1643 pData->cRedirectionsStored++;
1644 /* activate port-forwarding if guest has already got assigned IP */
1645 if ( ethaddr
1646 && memcmp(ethaddr, zerro_ethaddr, ETH_ALEN))
1647 activate_port_forwarding(pData, ethaddr);
1648 return 0;
1649}
1650
1651int slirp_remove_redirect(PNATState pData, int is_udp, struct in_addr host_addr, int host_port,
1652 struct in_addr guest_addr, int guest_port)
1653{
1654 struct port_forward_rule *rule = NULL;
1655 LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
1656 {
1657 if ( rule->proto == (is_udp ? IPPROTO_UDP : IPPROTO_TCP)
1658 && rule->host_port == host_port
1659 && rule->guest_port == guest_port
1660 && rule->bind_ip.s_addr == host_addr.s_addr
1661 && rule->guest_addr.s_addr == guest_addr.s_addr
1662 && rule->activated)
1663 {
1664 LogRel(("NAT: remove redirect %s host %RTnaipv4:%d => guest %RTnaipv4:%d\n",
1665 rule->proto == IPPROTO_UDP ? "UDP" : "TCP",
1666 rule->bind_ip.s_addr, rule->host_port,
1667 guest_addr.s_addr, rule->guest_port));
1668
1669 if (is_udp)
1670 udp_detach(pData, rule->so);
1671 else
1672 tcp_close(pData, sototcpcb(rule->so));
1673 LIST_REMOVE(rule, list);
1674 RTMemFree(rule);
1675 pData->cRedirectionsStored--;
1676 break;
1677 }
1678
1679 }
1680 return 0;
1681}
1682
1683void slirp_set_ethaddr_and_activate_port_forwarding(PNATState pData, const uint8_t *ethaddr, uint32_t GuestIP)
1684{
1685#ifndef VBOX_WITH_NAT_SERVICE
1686 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
1687#endif
1688 if (GuestIP != INADDR_ANY)
1689 {
1690 slirp_arp_cache_update_or_add(pData, GuestIP, ethaddr);
1691 activate_port_forwarding(pData, ethaddr);
1692 }
1693}
1694
1695#if defined(RT_OS_WINDOWS)
1696HANDLE *slirp_get_events(PNATState pData)
1697{
1698 return pData->phEvents;
1699}
1700void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
1701{
1702 pData->phEvents[index] = hEvent;
1703}
1704#endif
1705
1706unsigned int slirp_get_timeout_ms(PNATState pData)
1707{
1708 if (link_up)
1709 {
1710 if (time_fasttimo)
1711 return 2;
1712 if (do_slowtimo)
1713 return 500; /* see PR_SLOWHZ */
1714 }
1715 return 3600*1000; /* one hour */
1716}
1717
1718#ifndef RT_OS_WINDOWS
1719int slirp_get_nsock(PNATState pData)
1720{
1721 return pData->nsock;
1722}
1723#endif
1724
1725/*
1726 * this function called from NAT thread
1727 */
1728void slirp_post_sent(PNATState pData, void *pvArg)
1729{
1730 struct mbuf *m = (struct mbuf *)pvArg;
1731 m_freem(pData, m);
1732}
1733
1734void slirp_set_dhcp_TFTP_prefix(PNATState pData, const char *tftpPrefix)
1735{
1736 Log2(("tftp_prefix: %s\n", tftpPrefix));
1737 tftp_prefix = tftpPrefix;
1738}
1739
1740void slirp_set_dhcp_TFTP_bootfile(PNATState pData, const char *bootFile)
1741{
1742 Log2(("bootFile: %s\n", bootFile));
1743 bootp_filename = bootFile;
1744}
1745
1746void slirp_set_dhcp_next_server(PNATState pData, const char *next_server)
1747{
1748 Log2(("next_server: %s\n", next_server));
1749 if (next_server == NULL)
1750 pData->tftp_server.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_TFTP);
1751 else
1752 inet_aton(next_server, &pData->tftp_server);
1753}
1754
1755int slirp_set_binding_address(PNATState pData, char *addr)
1756{
1757 if (addr == NULL || (inet_aton(addr, &pData->bindIP) == 0))
1758 {
1759 pData->bindIP.s_addr = INADDR_ANY;
1760 return 1;
1761 }
1762 return 0;
1763}
1764
1765void slirp_set_dhcp_dns_proxy(PNATState pData, bool fDNSProxy)
1766{
1767 if (!pData->fUseHostResolver)
1768 {
1769 Log2(("NAT: DNS proxy switched %s\n", (fDNSProxy ? "on" : "off")));
1770 pData->fUseDnsProxy = fDNSProxy;
1771 }
1772 else if (fDNSProxy)
1773 LogRel(("NAT: Host Resolver conflicts with DNS proxy, the last one was forcely ignored\n"));
1774}
1775
1776#define CHECK_ARG(name, val, lim_min, lim_max) \
1777 do { \
1778 if ((val) < (lim_min) || (val) > (lim_max)) \
1779 { \
1780 LogRel(("NAT: (" #name ":%d) has been ignored, " \
1781 "because out of range (%d, %d)\n", (val), (lim_min), (lim_max))); \
1782 return; \
1783 } \
1784 else \
1785 LogRel(("NAT: (" #name ":%d)\n", (val))); \
1786 } while (0)
1787
1788void slirp_set_somaxconn(PNATState pData, int iSoMaxConn)
1789{
1790 LogFlowFunc(("iSoMaxConn:d\n", iSoMaxConn));
1791 /* Conditions */
1792 if (iSoMaxConn > SOMAXCONN)
1793 {
1794 LogRel(("NAT: value of somaxconn(%d) bigger than SOMAXCONN(%d)\n", iSoMaxConn, SOMAXCONN));
1795 iSoMaxConn = SOMAXCONN;
1796 }
1797
1798 if (iSoMaxConn < 1)
1799 {
1800 LogRel(("NAT: proposed value(%d) of somaxconn is invalid, default value is used (%d)\n", iSoMaxConn, pData->soMaxConn));
1801 LogFlowFuncLeave();
1802 return;
1803 }
1804
1805 /* Asignment */
1806 if (pData->soMaxConn != iSoMaxConn)
1807 {
1808 LogRel(("NAT: value of somaxconn has been changed from %d to %d\n",
1809 pData->soMaxConn, iSoMaxConn));
1810 pData->soMaxConn = iSoMaxConn;
1811 }
1812 LogFlowFuncLeave();
1813}
1814/* don't allow user set less 8kB and more than 1M values */
1815#define _8K_1M_CHECK_ARG(name, val) CHECK_ARG(name, (val), 8, 1024)
1816void slirp_set_rcvbuf(PNATState pData, int kilobytes)
1817{
1818 _8K_1M_CHECK_ARG("SOCKET_RCVBUF", kilobytes);
1819 pData->socket_rcv = kilobytes;
1820}
1821void slirp_set_sndbuf(PNATState pData, int kilobytes)
1822{
1823 _8K_1M_CHECK_ARG("SOCKET_SNDBUF", kilobytes);
1824 pData->socket_snd = kilobytes * _1K;
1825}
1826void slirp_set_tcp_rcvspace(PNATState pData, int kilobytes)
1827{
1828 _8K_1M_CHECK_ARG("TCP_RCVSPACE", kilobytes);
1829 tcp_rcvspace = kilobytes * _1K;
1830}
1831void slirp_set_tcp_sndspace(PNATState pData, int kilobytes)
1832{
1833 _8K_1M_CHECK_ARG("TCP_SNDSPACE", kilobytes);
1834 tcp_sndspace = kilobytes * _1K;
1835}
1836
1837/*
1838 * Looking for Ether by ip in ARP-cache
1839 * Note: it´s responsible of caller to allocate buffer for result
1840 * @returns iprt status code
1841 */
1842int slirp_arp_lookup_ether_by_ip(PNATState pData, uint32_t ip, uint8_t *ether)
1843{
1844 struct arp_cache_entry *ac;
1845
1846 if (ether == NULL)
1847 return VERR_INVALID_PARAMETER;
1848
1849 if (LIST_EMPTY(&pData->arp_cache))
1850 return VERR_NOT_FOUND;
1851
1852 LIST_FOREACH(ac, &pData->arp_cache, list)
1853 {
1854 if ( ac->ip == ip
1855 && memcmp(ac->ether, broadcast_ethaddr, ETH_ALEN) != 0)
1856 {
1857 memcpy(ether, ac->ether, ETH_ALEN);
1858 return VINF_SUCCESS;
1859 }
1860 }
1861 return VERR_NOT_FOUND;
1862}
1863
1864/*
1865 * Looking for IP by Ether in ARP-cache
1866 * Note: it´s responsible of caller to allocate buffer for result
1867 * @returns 0 - if found, 1 - otherwise
1868 */
1869int slirp_arp_lookup_ip_by_ether(PNATState pData, const uint8_t *ether, uint32_t *ip)
1870{
1871 struct arp_cache_entry *ac;
1872 *ip = INADDR_ANY;
1873
1874 if (LIST_EMPTY(&pData->arp_cache))
1875 return VERR_NOT_FOUND;
1876
1877 LIST_FOREACH(ac, &pData->arp_cache, list)
1878 {
1879 if (memcmp(ether, ac->ether, ETH_ALEN) == 0)
1880 {
1881 *ip = ac->ip;
1882 return VINF_SUCCESS;
1883 }
1884 }
1885 return VERR_NOT_FOUND;
1886}
1887
1888void slirp_arp_who_has(PNATState pData, uint32_t dst)
1889{
1890 struct mbuf *m;
1891 struct ethhdr *ehdr;
1892 struct arphdr *ahdr;
1893 static bool fWarned = false;
1894 LogFlowFunc(("ENTER: %RTnaipv4\n", dst));
1895
1896 /* ARP request WHO HAS 0.0.0.0 is one of the signals
1897 * that something has been broken at Slirp. Investigating
1898 * pcap dumps it's easy to miss warning ARP requests being
1899 * focused on investigation of other protocols flow.
1900 */
1901#ifdef DEBUG_vvl
1902 Assert((dst != INADDR_ANY));
1903 NOREF(fWarned);
1904#else
1905 if ( dst == INADDR_ANY
1906 && !fWarned)
1907 {
1908 LogRel(("NAT:ARP: \"WHO HAS INADDR_ANY\" request has been detected\n"));
1909 fWarned = true;
1910 }
1911#endif /* !DEBUG_vvl */
1912
1913 m = m_getcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR);
1914 if (m == NULL)
1915 {
1916 Log(("NAT: Can't alloc mbuf for ARP request\n"));
1917 LogFlowFuncLeave();
1918 return;
1919 }
1920 ehdr = mtod(m, struct ethhdr *);
1921 memset(ehdr->h_source, 0xff, ETH_ALEN);
1922 ahdr = (struct arphdr *)&ehdr[1];
1923 ahdr->ar_hrd = RT_H2N_U16_C(1);
1924 ahdr->ar_pro = RT_H2N_U16_C(ETH_P_IP);
1925 ahdr->ar_hln = ETH_ALEN;
1926 ahdr->ar_pln = 4;
1927 ahdr->ar_op = RT_H2N_U16_C(ARPOP_REQUEST);
1928 memcpy(ahdr->ar_sha, special_ethaddr, ETH_ALEN);
1929 /* we assume that this request come from gw, but not from DNS or TFTP */
1930 ahdr->ar_sha[5] = CTL_ALIAS;
1931 *(uint32_t *)ahdr->ar_sip = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
1932 memset(ahdr->ar_tha, 0xff, ETH_ALEN); /*broadcast*/
1933 *(uint32_t *)ahdr->ar_tip = dst;
1934 /* warn!!! should falls in mbuf minimal size */
1935 m->m_len = sizeof(struct arphdr) + ETH_HLEN;
1936 m->m_data += ETH_HLEN;
1937 m->m_len -= ETH_HLEN;
1938 if_encap(pData, ETH_P_ARP, m, ETH_ENCAP_URG);
1939 LogFlowFuncLeave();
1940}
1941#ifdef VBOX_WITH_DNSMAPPING_IN_HOSTRESOLVER
1942void slirp_add_host_resolver_mapping(PNATState pData, const char *pszHostName, const char *pszHostNamePattern, uint32_t u32HostIP)
1943{
1944 LogFlowFunc(("ENTER: pszHostName:%s, pszHostNamePattern:%s u32HostIP:%RTnaipv4\n",
1945 pszHostName ? pszHostName : "(null)",
1946 pszHostNamePattern ? pszHostNamePattern : "(null)",
1947 u32HostIP));
1948 if ( ( pszHostName
1949 || pszHostNamePattern)
1950 && u32HostIP != INADDR_ANY
1951 && u32HostIP != INADDR_BROADCAST)
1952 {
1953 PDNSMAPPINGENTRY pDnsMapping = RTMemAllocZ(sizeof(DNSMAPPINGENTRY));
1954 if (!pDnsMapping)
1955 {
1956 LogFunc(("Can't allocate DNSMAPPINGENTRY\n"));
1957 LogFlowFuncLeave();
1958 return;
1959 }
1960 pDnsMapping->u32IpAddress = u32HostIP;
1961 if (pszHostName)
1962 pDnsMapping->pszCName = RTStrDup(pszHostName);
1963 else if (pszHostNamePattern)
1964 pDnsMapping->pszPattern = RTStrDup(pszHostNamePattern);
1965 if ( !pDnsMapping->pszCName
1966 && !pDnsMapping->pszPattern)
1967 {
1968 LogFunc(("Can't allocate enough room for %s\n", pszHostName ? pszHostName : pszHostNamePattern));
1969 RTMemFree(pDnsMapping);
1970 LogFlowFuncLeave();
1971 return;
1972 }
1973 LIST_INSERT_HEAD(&pData->DNSMapHead, pDnsMapping, MapList);
1974 LogRel(("NAT: user-defined mapping %s: %RTnaipv4 is registered\n",
1975 pDnsMapping->pszCName ? pDnsMapping->pszCName : pDnsMapping->pszPattern,
1976 pDnsMapping->u32IpAddress));
1977 }
1978 LogFlowFuncLeave();
1979}
1980#endif
1981
1982/* updates the arp cache
1983 * @note: this is helper function, slirp_arp_cache_update_or_add should be used.
1984 * @returns 0 - if has found and updated
1985 * 1 - if hasn't found.
1986 */
1987static inline int slirp_arp_cache_update(PNATState pData, uint32_t dst, const uint8_t *mac)
1988{
1989 struct arp_cache_entry *ac;
1990 Assert(( memcmp(mac, broadcast_ethaddr, ETH_ALEN)
1991 && memcmp(mac, zerro_ethaddr, ETH_ALEN)));
1992 LIST_FOREACH(ac, &pData->arp_cache, list)
1993 {
1994 if (ac->ip == dst)
1995 {
1996 memcpy(ac->ether, mac, ETH_ALEN);
1997 return 0;
1998 }
1999 }
2000 return 1;
2001}
2002
2003/**
2004 * add entry to the arp cache
2005 * @note: this is helper function, slirp_arp_cache_update_or_add should be used.
2006 */
2007static inline void slirp_arp_cache_add(PNATState pData, uint32_t ip, const uint8_t *ether)
2008{
2009 struct arp_cache_entry *ac = NULL;
2010 Assert(( memcmp(ether, broadcast_ethaddr, ETH_ALEN)
2011 && memcmp(ether, zerro_ethaddr, ETH_ALEN)));
2012 ac = RTMemAllocZ(sizeof(struct arp_cache_entry));
2013 if (ac == NULL)
2014 {
2015 Log(("NAT: Can't allocate arp cache entry\n"));
2016 return;
2017 }
2018 ac->ip = ip;
2019 memcpy(ac->ether, ether, ETH_ALEN);
2020 LIST_INSERT_HEAD(&pData->arp_cache, ac, list);
2021}
2022
2023/* updates or adds entry to the arp cache
2024 * @returns 0 - if has found and updated
2025 * 1 - if hasn't found.
2026 */
2027int slirp_arp_cache_update_or_add(PNATState pData, uint32_t dst, const uint8_t *mac)
2028{
2029 if ( !memcmp(mac, broadcast_ethaddr, ETH_ALEN)
2030 || !memcmp(mac, zerro_ethaddr, ETH_ALEN))
2031 {
2032 static bool fBroadcastEtherAddReported;
2033 if (!fBroadcastEtherAddReported)
2034 {
2035 LogRel(("NAT: Attempt to add pair [%RTmac:%RTnaipv4] in ARP cache was ignored\n",
2036 mac, dst));
2037 fBroadcastEtherAddReported = true;
2038 }
2039 return 1;
2040 }
2041 if (slirp_arp_cache_update(pData, dst, mac))
2042 slirp_arp_cache_add(pData, dst, mac);
2043
2044 return 0;
2045}
2046
2047
2048void slirp_set_mtu(PNATState pData, int mtu)
2049{
2050 if (mtu < 20 || mtu >= 16000)
2051 {
2052 LogRel(("NAT: mtu(%d) is out of range (20;16000] mtu forcely assigned to 1500\n", mtu));
2053 mtu = 1500;
2054 }
2055 /* MTU is maximum transition unit on */
2056 if_mtu =
2057 if_mru = mtu;
2058}
2059
2060/**
2061 * Info handler.
2062 */
2063void slirp_info(PNATState pData, const void *pvArg, const char *pszArgs)
2064{
2065 struct socket *so, *so_next;
2066 struct arp_cache_entry *ac;
2067 struct port_forward_rule *rule;
2068 PCDBGFINFOHLP pHlp = (PCDBGFINFOHLP)pvArg;
2069 NOREF(pszArgs);
2070
2071 pHlp->pfnPrintf(pHlp, "NAT parameters: MTU=%d\n", if_mtu);
2072 pHlp->pfnPrintf(pHlp, "NAT TCP ports:\n");
2073 QSOCKET_FOREACH(so, so_next, tcp)
2074 /* { */
2075 pHlp->pfnPrintf(pHlp, " %R[natsock]\n", so);
2076 }
2077
2078 pHlp->pfnPrintf(pHlp, "NAT UDP ports:\n");
2079 QSOCKET_FOREACH(so, so_next, udp)
2080 /* { */
2081 pHlp->pfnPrintf(pHlp, " %R[natsock]\n", so);
2082 }
2083
2084 pHlp->pfnPrintf(pHlp, "NAT ARP cache:\n");
2085 LIST_FOREACH(ac, &pData->arp_cache, list)
2086 {
2087 pHlp->pfnPrintf(pHlp, " %RTnaipv4 %RTmac\n", ac->ip, &ac->ether);
2088 }
2089
2090 pHlp->pfnPrintf(pHlp, "NAT rules:\n");
2091 LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
2092 {
2093 pHlp->pfnPrintf(pHlp, " %s %d => %RTnaipv4:%d %c\n",
2094 rule->proto == IPPROTO_UDP ? "UDP" : "TCP",
2095 rule->host_port, rule->guest_addr.s_addr, rule->guest_port,
2096 rule->activated ? ' ' : '*');
2097 }
2098}
2099
2100/**
2101 * @note: NATState::fUseHostResolver could be changed in bootp.c::dhcp_decode
2102 * @note: this function is executed on GUI/VirtualBox or main/VBoxHeadless thread.
2103 * @note: this function can potentially race with bootp.c::dhcp_decode (except Darwin)
2104 */
2105int slirp_host_network_configuration_change_strategy_selector(const PNATState pData)
2106{
2107 if (pData->fUseHostResolverPermanent)
2108 return VBOX_NAT_DNS_HOSTRESOLVER;
2109
2110 if (pData->fUseDnsProxy) {
2111#if HAVE_NOTIFICATION_FOR_DNS_UPDATE
2112 /* We dont conflict with bootp.c::dhcp_decode */
2113 struct rcp_state rcp_state;
2114 int rc;
2115
2116 rcp_state.rcps_flags |= RCPSF_IGNORE_IPV6;
2117 rc = rcp_parse(&rcp_state, RESOLV_CONF_FILE);
2118 LogRelFunc(("NAT: rcp_parse:%Rrc old domain:%s new domain:%s\n",
2119 rc, LIST_FIRST(&pData->pDomainList)->dd_pszDomain,
2120 rcp_state.rcps_domain));
2121 if ( RT_FAILURE(rc)
2122 || LIST_EMPTY(&pData->pDomainList))
2123 return VBOX_NAT_DNS_DNSPROXY;
2124
2125 if ( rcp_state.rcps_domain
2126 && strcmp(rcp_state.rcps_domain, LIST_FIRST(&pData->pDomainList)->dd_pszDomain) == 0)
2127 return VBOX_NAT_DNS_DNSPROXY;
2128 else
2129 return VBOX_NAT_DNS_EXTERNAL;
2130#else
2131 /* copy domain name */
2132 /* domain only compare with coy version */
2133 return VBOX_NAT_DNS_DNSPROXY;
2134#endif
2135 }
2136 return VBOX_NAT_DNS_EXTERNAL;
2137}
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