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