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