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