VirtualBox

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

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

NAT: if_encap: assert that mbuf has enough M_LEADINGSPACE to
accomodate ethernet header.

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