VirtualBox

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

Last change on this file since 57358 was 56992, checked in by vboxsync, 9 years ago

Devices: Log & Assertion formatting fixes.

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