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