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 | #ifndef RT_OS_WINDOWS
|
---|
10 | # include <sys/ioctl.h>
|
---|
11 | # include <poll.h>
|
---|
12 | #else
|
---|
13 | # include <Winnls.h>
|
---|
14 | # define _WINSOCK2API_
|
---|
15 | # include <IPHlpApi.h>
|
---|
16 | #endif
|
---|
17 | #ifdef VBOX_WITH_SLIRP_ALIAS
|
---|
18 | # include <alias.h>
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | #if !defined(RT_OS_WINDOWS)
|
---|
22 |
|
---|
23 | # define DO_ENGAGE_EVENT1(so, fdset, label) \
|
---|
24 | do { \
|
---|
25 | if( so->so_poll_index != -1 \
|
---|
26 | && so->s == polls[so->so_poll_index].fd) { \
|
---|
27 | polls[so->so_poll_index].events |= N_(fdset ## _poll); \
|
---|
28 | break; /* out of this loop */ \
|
---|
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 |
|
---|
40 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
41 | do { \
|
---|
42 | if( so->so_poll_index != -1 \
|
---|
43 | && so->s == polls[so->so_poll_index].fd) { \
|
---|
44 | polls[so->so_poll_index].events |= \
|
---|
45 | N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
|
---|
46 | break; /* out of this loop */ \
|
---|
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_(fdset1 ## _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) ( ((so)->so_poll_index != -1) \
|
---|
59 | && ((so)->so_poll_index <= ndfs) \
|
---|
60 | && ((so)->s == polls[so->so_poll_index].fd) \
|
---|
61 | && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)))
|
---|
62 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
|
---|
63 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
|
---|
64 |
|
---|
65 | # ifndef RT_OS_WINDOWS
|
---|
66 |
|
---|
67 | # ifndef RT_OS_LINUX
|
---|
68 | # define readfds_poll (POLLRDNORM)
|
---|
69 | # define writefds_poll (POLLWRNORM)
|
---|
70 | # define xfds_poll (POLLRDBAND|POLLWRBAND|POLLPRI)
|
---|
71 | # else
|
---|
72 | # define readfds_poll (POLLIN)
|
---|
73 | # define writefds_poll (POLLOUT)
|
---|
74 | # define xfds_poll (POLLPRI)
|
---|
75 | # endif
|
---|
76 | # define rderr_poll (POLLERR)
|
---|
77 | # define rdhup_poll (POLLHUP)
|
---|
78 | # define nval_poll (POLLNVAL)
|
---|
79 |
|
---|
80 | # define ICMP_ENGAGE_EVENT(so, fdset) \
|
---|
81 | do { \
|
---|
82 | if (pData->icmp_socket.s != -1) \
|
---|
83 | DO_ENGAGE_EVENT1((so), fdset, ICMP); \
|
---|
84 | } while (0)
|
---|
85 | # else /* !RT_OS_WINDOWS */
|
---|
86 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
|
---|
87 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
88 | #endif /* RT_OS_WINDOWS */
|
---|
89 |
|
---|
90 | #else /* defined(RT_OS_WINDOWS) */
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
|
---|
94 | * So no call to WSAEventSelect necessary.
|
---|
95 | */
|
---|
96 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
97 |
|
---|
98 | # define DO_ENGAGE_EVENT1(so, fdset1, label) \
|
---|
99 | do { \
|
---|
100 | rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
|
---|
101 | if (rc == SOCKET_ERROR) \
|
---|
102 | { \
|
---|
103 | /* This should not happen */ \
|
---|
104 | error = WSAGetLastError(); \
|
---|
105 | LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
|
---|
106 | error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
|
---|
107 | } \
|
---|
108 | } while(0); \
|
---|
109 | CONTINUE(label)
|
---|
110 |
|
---|
111 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
112 | DO_ENGAGE_EVENT1((so), (fdset1), label)
|
---|
113 |
|
---|
114 | # define DO_POLL_EVENTS(rc, error, so, events, label) \
|
---|
115 | (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
|
---|
116 | if ((rc) == SOCKET_ERROR) \
|
---|
117 | { \
|
---|
118 | (error) = WSAGetLastError(); \
|
---|
119 | LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
|
---|
120 | CONTINUE(label); \
|
---|
121 | }
|
---|
122 |
|
---|
123 | # define acceptds_win FD_ACCEPT
|
---|
124 | # define acceptds_win_bit FD_ACCEPT_BIT
|
---|
125 |
|
---|
126 | # define readfds_win FD_READ
|
---|
127 | # define readfds_win_bit FD_READ_BIT
|
---|
128 |
|
---|
129 | # define writefds_win FD_WRITE
|
---|
130 | # define writefds_win_bit FD_WRITE_BIT
|
---|
131 |
|
---|
132 | # define xfds_win FD_OOB
|
---|
133 | # define xfds_win_bit FD_OOB_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 /* defined(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 | #define UNIX_CHECK_FD_SET(so, events, set) \
|
---|
164 | (DO_UNIX_CHECK_FD_SET(so, events, set))
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * Loging macros
|
---|
168 | */
|
---|
169 | #if VBOX_WITH_DEBUG_NAT_SOCKETS
|
---|
170 | # if defined(RT_OS_WINDOWS)
|
---|
171 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
172 | do { \
|
---|
173 | LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
|
---|
174 | } while (0)
|
---|
175 | # else /* RT_OS_WINDOWS */
|
---|
176 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
177 | do { \
|
---|
178 | LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
|
---|
179 | CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
|
---|
180 | CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
|
---|
181 | CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
|
---|
182 | CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
|
---|
183 | CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
|
---|
184 | CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
|
---|
185 | } while (0)
|
---|
186 | # endif /* !RT_OS_WINDOWS */
|
---|
187 | #else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
188 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
|
---|
189 | #endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
190 |
|
---|
191 | #define LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) DO_LOG_NAT_SOCK((so), proto, (winevent), r_fdset, w_fdset, x_fdset)
|
---|
192 |
|
---|
193 | static const uint8_t special_ethaddr[6] =
|
---|
194 | {
|
---|
195 | 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
|
---|
196 | };
|
---|
197 |
|
---|
198 | static const uint8_t broadcast_ethaddr[6] =
|
---|
199 | {
|
---|
200 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
---|
201 | };
|
---|
202 |
|
---|
203 | const uint8_t zerro_ethaddr[6] =
|
---|
204 | {
|
---|
205 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
|
---|
206 | };
|
---|
207 |
|
---|
208 | #ifdef RT_OS_WINDOWS
|
---|
209 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
210 | struct in_addr *pdns_addr,
|
---|
211 | const char **ppszDomain)
|
---|
212 | {
|
---|
213 | /* Get amount of memory required for operation */
|
---|
214 | ULONG flags = GAA_FLAG_INCLUDE_PREFIX; /*GAA_FLAG_INCLUDE_ALL_INTERFACES;*/ /* all interfaces registered in NDIS */
|
---|
215 | PIP_ADAPTER_ADDRESSES addresses = NULL;
|
---|
216 | PIP_ADAPTER_ADDRESSES addr = NULL;
|
---|
217 | PIP_ADAPTER_DNS_SERVER_ADDRESS dns = NULL;
|
---|
218 | ULONG size = 0;
|
---|
219 | int wlen = 0;
|
---|
220 | char *suffix;
|
---|
221 | struct dns_entry *da = NULL;
|
---|
222 | struct dns_domain_entry *dd = NULL;
|
---|
223 | ULONG ret = ERROR_SUCCESS;
|
---|
224 |
|
---|
225 | /* @todo add SKIPing flags to get only required information */
|
---|
226 |
|
---|
227 | ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
|
---|
228 | if (ret != ERROR_BUFFER_OVERFLOW)
|
---|
229 | {
|
---|
230 | LogRel(("NAT: error %lu occurred on capacity detection operation\n", ret));
|
---|
231 | return -1;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (size == 0)
|
---|
235 | {
|
---|
236 | LogRel(("NAT: Win socket API returns non capacity\n"));
|
---|
237 | return -1;
|
---|
238 | }
|
---|
239 |
|
---|
240 | addresses = RTMemAllocZ(size);
|
---|
241 | if (addresses == NULL)
|
---|
242 | {
|
---|
243 | LogRel(("NAT: No memory available \n"));
|
---|
244 | return -1;
|
---|
245 | }
|
---|
246 |
|
---|
247 | ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
|
---|
248 | if (ret != ERROR_SUCCESS)
|
---|
249 | {
|
---|
250 | LogRel(("NAT: error %lu occurred on fetching adapters info\n", ret));
|
---|
251 | RTMemFree(addresses);
|
---|
252 | return -1;
|
---|
253 | }
|
---|
254 | addr = addresses;
|
---|
255 | while(addr != NULL)
|
---|
256 | {
|
---|
257 | int found;
|
---|
258 | if (addr->OperStatus != IfOperStatusUp)
|
---|
259 | goto next;
|
---|
260 | dns = addr->FirstDnsServerAddress;
|
---|
261 | while (dns != NULL)
|
---|
262 | {
|
---|
263 | struct sockaddr *saddr = dns->Address.lpSockaddr;
|
---|
264 | if (saddr->sa_family != AF_INET)
|
---|
265 | goto next_dns;
|
---|
266 | /* add dns server to list */
|
---|
267 | da = RTMemAllocZ(sizeof(struct dns_entry));
|
---|
268 | if (da == NULL)
|
---|
269 | {
|
---|
270 | LogRel(("NAT: Can't allocate buffer for DNS entry\n"));
|
---|
271 | RTMemFree(addresses);
|
---|
272 | return VERR_NO_MEMORY;
|
---|
273 | }
|
---|
274 | LogRel(("NAT: adding %R[IP4] to DNS server list\n", &((struct sockaddr_in *)saddr)->sin_addr));
|
---|
275 | if ((((struct sockaddr_in *)saddr)->sin_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
276 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
277 | }
|
---|
278 | else
|
---|
279 | {
|
---|
280 | da->de_addr.s_addr = ((struct sockaddr_in *)saddr)->sin_addr.s_addr;
|
---|
281 | }
|
---|
282 | TAILQ_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
283 |
|
---|
284 | if (addr->DnsSuffix == NULL)
|
---|
285 | goto next_dns;
|
---|
286 |
|
---|
287 | /*uniq*/
|
---|
288 | RTUtf16ToUtf8(addr->DnsSuffix, &suffix);
|
---|
289 |
|
---|
290 | if (!suffix || strlen(suffix) == 0) {
|
---|
291 | RTStrFree(suffix);
|
---|
292 | goto next_dns;
|
---|
293 | }
|
---|
294 |
|
---|
295 | found = 0;
|
---|
296 | LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
|
---|
297 | {
|
---|
298 | if ( dd->dd_pszDomain != NULL
|
---|
299 | && strcmp(dd->dd_pszDomain, suffix) == 0)
|
---|
300 | {
|
---|
301 | found = 1;
|
---|
302 | RTStrFree(suffix);
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | if (found == 0)
|
---|
307 | {
|
---|
308 | dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
|
---|
309 | if (dd == NULL)
|
---|
310 | {
|
---|
311 | LogRel(("NAT: not enough memory\n"));
|
---|
312 | RTStrFree(suffix);
|
---|
313 | RTMemFree(addresses);
|
---|
314 | return VERR_NO_MEMORY;
|
---|
315 | }
|
---|
316 | dd->dd_pszDomain = suffix;
|
---|
317 | LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
|
---|
318 | LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
|
---|
319 | }
|
---|
320 | next_dns:
|
---|
321 | dns = dns->Next;
|
---|
322 | }
|
---|
323 | next:
|
---|
324 | addr = addr->Next;
|
---|
325 | }
|
---|
326 | RTMemFree(addresses);
|
---|
327 | return 0;
|
---|
328 | }
|
---|
329 |
|
---|
330 | #else /* !RT_OS_WINDOWS */
|
---|
331 |
|
---|
332 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
333 | struct in_addr *pdns_addr,
|
---|
334 | const char **ppszDomain)
|
---|
335 | {
|
---|
336 | char buff[512];
|
---|
337 | char buff2[256];
|
---|
338 | FILE *f = NULL;
|
---|
339 | int found = 0;
|
---|
340 | struct in_addr tmp_addr;
|
---|
341 |
|
---|
342 | #ifdef RT_OS_OS2
|
---|
343 | /* Try various locations. */
|
---|
344 | char *etc = getenv("ETC");
|
---|
345 | if (etc)
|
---|
346 | {
|
---|
347 | snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
|
---|
348 | f = fopen(buff, "rt");
|
---|
349 | }
|
---|
350 | if (!f)
|
---|
351 | {
|
---|
352 | snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
|
---|
353 | f = fopen(buff, "rt");
|
---|
354 | }
|
---|
355 | if (!f)
|
---|
356 | {
|
---|
357 | snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
|
---|
358 | f = fopen(buff, "rt");
|
---|
359 | }
|
---|
360 | #else
|
---|
361 | #ifndef DEBUG_vvl
|
---|
362 | f = fopen("/etc/resolv.conf", "r");
|
---|
363 | #else
|
---|
364 | char *home = getenv("HOME");
|
---|
365 | snprintf(buff, sizeof(buff), "%s/resolv.conf", home);
|
---|
366 | f = fopen(buff, "r");
|
---|
367 | if (f != NULL)
|
---|
368 | {
|
---|
369 | Log(("NAT: DNS we're using %s\n", buff));
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | f = fopen("/etc/resolv.conf", "r");
|
---|
374 | Log(("NAT: DNS we're using %s\n", buff));
|
---|
375 | }
|
---|
376 | #endif
|
---|
377 | #endif
|
---|
378 | if (!f)
|
---|
379 | return -1;
|
---|
380 |
|
---|
381 | if (ppszDomain)
|
---|
382 | *ppszDomain = NULL;
|
---|
383 | Log(("nat: DNS Servers:\n"));
|
---|
384 | while (fgets(buff, 512, f) != NULL)
|
---|
385 | {
|
---|
386 | struct dns_entry *da = NULL;
|
---|
387 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
|
---|
388 | {
|
---|
389 | if (!inet_aton(buff2, &tmp_addr))
|
---|
390 | continue;
|
---|
391 | /*localhost mask */
|
---|
392 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
393 | if (da == NULL)
|
---|
394 | {
|
---|
395 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
396 | return -1;
|
---|
397 | }
|
---|
398 | /*check */
|
---|
399 | da->de_addr.s_addr = tmp_addr.s_addr;
|
---|
400 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
401 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
402 | }
|
---|
403 | TAILQ_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
404 | found++;
|
---|
405 | }
|
---|
406 | if ((!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
|
---|
407 | {
|
---|
408 | char *tok;
|
---|
409 | char *saveptr;
|
---|
410 | struct dns_domain_entry *dd = NULL;
|
---|
411 | int found = 0;
|
---|
412 | tok = strtok_r(&buff[6], " \t\n", &saveptr);
|
---|
413 | LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
|
---|
414 | {
|
---|
415 | if( tok != NULL
|
---|
416 | && strcmp(tok, dd->dd_pszDomain) == 0)
|
---|
417 | {
|
---|
418 | found = 1;
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | }
|
---|
422 | if (tok != NULL && found == 0) {
|
---|
423 | dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
|
---|
424 | if (dd == NULL)
|
---|
425 | {
|
---|
426 | LogRel(("NAT: not enought memory to add domain list\n"));
|
---|
427 | return VERR_NO_MEMORY;
|
---|
428 | }
|
---|
429 | dd->dd_pszDomain = RTStrDup(tok);
|
---|
430 | LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
|
---|
431 | LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
|
---|
432 | }
|
---|
433 | }
|
---|
434 | }
|
---|
435 | fclose(f);
|
---|
436 | if (!found)
|
---|
437 | return -1;
|
---|
438 | return 0;
|
---|
439 | }
|
---|
440 |
|
---|
441 | #endif
|
---|
442 |
|
---|
443 | static int slirp_init_dns_list(PNATState pData)
|
---|
444 | {
|
---|
445 | TAILQ_INIT(&pData->dns_list_head);
|
---|
446 | LIST_INIT(&pData->dns_domain_list_head);
|
---|
447 | return get_dns_addr_domain(pData, true, NULL, NULL);
|
---|
448 | }
|
---|
449 |
|
---|
450 | static void slirp_release_dns_list(PNATState pData)
|
---|
451 | {
|
---|
452 | struct dns_entry *de = NULL;
|
---|
453 | struct dns_domain_entry *dd = NULL;
|
---|
454 | while(!TAILQ_EMPTY(&pData->dns_list_head)) {
|
---|
455 | de = TAILQ_FIRST(&pData->dns_list_head);
|
---|
456 | TAILQ_REMOVE(&pData->dns_list_head, de, de_list);
|
---|
457 | RTMemFree(de);
|
---|
458 | }
|
---|
459 | while(!LIST_EMPTY(&pData->dns_domain_list_head)) {
|
---|
460 | dd = LIST_FIRST(&pData->dns_domain_list_head);
|
---|
461 | LIST_REMOVE(dd, dd_list);
|
---|
462 | if (dd->dd_pszDomain != NULL)
|
---|
463 | RTStrFree(dd->dd_pszDomain);
|
---|
464 | RTMemFree(dd);
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
|
---|
469 | {
|
---|
470 | return get_dns_addr_domain(pData, false, pdns_addr, NULL);
|
---|
471 | }
|
---|
472 |
|
---|
473 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
474 | int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
|
---|
475 | bool fPassDomain, void *pvUser)
|
---|
476 | #else
|
---|
477 | int slirp_init(PNATState *ppData, uint32_t u32NetAddr, uint32_t u32Netmask,
|
---|
478 | bool fPassDomain, void *pvUser)
|
---|
479 | #endif
|
---|
480 | {
|
---|
481 | int fNATfailed = 0;
|
---|
482 | int rc;
|
---|
483 | PNATState pData = RTMemAllocZ(sizeof(NATState));
|
---|
484 | *ppData = pData;
|
---|
485 | if (!pData)
|
---|
486 | return VERR_NO_MEMORY;
|
---|
487 | if (u32Netmask & 0x1f)
|
---|
488 | /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
|
---|
489 | return VERR_INVALID_PARAMETER;
|
---|
490 | pData->fPassDomain = fPassDomain;
|
---|
491 | pData->pvUser = pvUser;
|
---|
492 | pData->netmask = u32Netmask;
|
---|
493 |
|
---|
494 | /* sockets & TCP defaults */
|
---|
495 | pData->socket_rcv = 64 * _1K;
|
---|
496 | pData->socket_snd = 64 * _1K;
|
---|
497 | tcp_sndspace = 64 * _1K;
|
---|
498 | tcp_rcvspace = 64 * _1K;
|
---|
499 |
|
---|
500 | #ifdef RT_OS_WINDOWS
|
---|
501 | {
|
---|
502 | WSADATA Data;
|
---|
503 | WSAStartup(MAKEWORD(2, 0), &Data);
|
---|
504 | }
|
---|
505 | pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
506 | #endif
|
---|
507 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
508 | QSOCKET_LOCK_CREATE(tcb);
|
---|
509 | QSOCKET_LOCK_CREATE(udb);
|
---|
510 | rc = RTReqCreateQueue(&pData->pReqQueue);
|
---|
511 | AssertReleaseRC(rc);
|
---|
512 | #endif
|
---|
513 |
|
---|
514 | link_up = 1;
|
---|
515 |
|
---|
516 | debug_init();
|
---|
517 | if_init(pData);
|
---|
518 | ip_init(pData);
|
---|
519 | icmp_init(pData);
|
---|
520 |
|
---|
521 | /* Initialise mbufs *after* setting the MTU */
|
---|
522 | m_init(pData);
|
---|
523 |
|
---|
524 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
525 | inet_aton(pszNetAddr, &special_addr);
|
---|
526 | #else
|
---|
527 | special_addr.s_addr = u32NetAddr;
|
---|
528 | #endif
|
---|
529 | #ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
|
---|
530 | pData->slirp_ethaddr = &special_ethaddr[0];
|
---|
531 | #endif
|
---|
532 | alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
|
---|
533 | /* @todo: add ability to configure this staff */
|
---|
534 |
|
---|
535 | /* set default addresses */
|
---|
536 | inet_aton("127.0.0.1", &loopback_addr);
|
---|
537 | if (slirp_init_dns_list(pData) < 0)
|
---|
538 | fNATfailed = 1;
|
---|
539 |
|
---|
540 | dnsproxy_init(pData);
|
---|
541 |
|
---|
542 | getouraddr(pData);
|
---|
543 |
|
---|
544 | #ifdef VBOX_WITH_SLIRP_ALIAS
|
---|
545 | {
|
---|
546 | int flags = 0;
|
---|
547 | struct in_addr proxy_addr;
|
---|
548 | pData->proxy_alias = LibAliasInit(pData, NULL);
|
---|
549 | if (pData->proxy_alias == NULL)
|
---|
550 | {
|
---|
551 | LogRel(("NAT: LibAlias default rule wasn't initialized\n"));
|
---|
552 | AssertMsgFailed(("NAT: LibAlias default rule wasn't initialized\n"));
|
---|
553 | }
|
---|
554 | flags = LibAliasSetMode(pData->proxy_alias, 0, 0);
|
---|
555 | flags |= PKT_ALIAS_LOG; /* set logging */
|
---|
556 | flags = LibAliasSetMode(pData->proxy_alias, flags, ~0);
|
---|
557 | proxy_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
558 | LibAliasSetAddress(pData->proxy_alias, proxy_addr);
|
---|
559 | ftp_alias_load();
|
---|
560 | nbt_alias_load();
|
---|
561 |
|
---|
562 | }
|
---|
563 | #endif
|
---|
564 | return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Register statistics.
|
---|
569 | */
|
---|
570 | void slirp_register_statistics(PNATState pData, PPDMDRVINS pDrvIns)
|
---|
571 | {
|
---|
572 | #ifdef VBOX_WITH_STATISTICS
|
---|
573 | # define COUNTER(name, type, units, dsc) \
|
---|
574 | do { \
|
---|
575 | PDMDrvHlpSTAMRegisterF(pDrvIns, \
|
---|
576 | &pData->Stat ## name, \
|
---|
577 | type, \
|
---|
578 | STAMVISIBILITY_ALWAYS, \
|
---|
579 | units, \
|
---|
580 | dsc, \
|
---|
581 | "/Drivers/NAT%u/" #name, \
|
---|
582 | pDrvIns->iInstance); \
|
---|
583 | } while (0)
|
---|
584 |
|
---|
585 | # define PROFILE_COUNTER(name, dsc) COUNTER(name, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
|
---|
586 | # define COUNTING_COUNTER(name, dsc) COUNTER(name, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
|
---|
587 |
|
---|
588 | # include "counters.h"
|
---|
589 |
|
---|
590 | # undef COUNTER
|
---|
591 | # undef PROFILE_COUNTER
|
---|
592 | # undef COUNTING_COUNTER
|
---|
593 | /** @todo register statistics for the variables dumped by:
|
---|
594 | * ipstats(pData); tcpstats(pData); udpstats(pData); icmpstats(pData);
|
---|
595 | * mbufstats(pData); sockstats(pData); */
|
---|
596 | #endif /* VBOX_WITH_STATISTICS */
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Deregister statistics.
|
---|
601 | */
|
---|
602 | void slirp_deregister_statistics(PNATState pData, PPDMDRVINS pDrvIns)
|
---|
603 | {
|
---|
604 | #ifdef VBOX_WITH_STATISTICS
|
---|
605 | # define PROFILE_COUNTER(name, dsc) PDMDrvHlpSTAMDeregister(pDrvIns, &pData->Stat ## name)
|
---|
606 | # define COUNTING_COUNTER(name, dsc) PDMDrvHlpSTAMDeregister(pDrvIns, &pData->Stat ## name)
|
---|
607 |
|
---|
608 | # include "counters.h"
|
---|
609 |
|
---|
610 | # undef COUNTING_COUNTER
|
---|
611 | # undef PROFILE_COUNTER
|
---|
612 | #endif /* VBOX_WITH_STATISTICS */
|
---|
613 | }
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Marks the link as up, making it possible to establish new connections.
|
---|
617 | */
|
---|
618 | void slirp_link_up(PNATState pData)
|
---|
619 | {
|
---|
620 | link_up = 1;
|
---|
621 | }
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * Marks the link as down and cleans up the current connections.
|
---|
625 | */
|
---|
626 | void slirp_link_down(PNATState pData)
|
---|
627 | {
|
---|
628 | struct socket *so;
|
---|
629 |
|
---|
630 | while ((so = tcb.so_next) != &tcb)
|
---|
631 | {
|
---|
632 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
633 | sofree(pData, so);
|
---|
634 | else
|
---|
635 | tcp_drop(pData, sototcpcb(so), 0);
|
---|
636 | }
|
---|
637 |
|
---|
638 | while ((so = udb.so_next) != &udb)
|
---|
639 | udp_detach(pData, so);
|
---|
640 |
|
---|
641 | link_up = 0;
|
---|
642 | }
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Terminates the slirp component.
|
---|
646 | */
|
---|
647 | void slirp_term(PNATState pData)
|
---|
648 | {
|
---|
649 | #ifdef RT_OS_WINDOWS
|
---|
650 | pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
|
---|
651 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
652 | RTMemFree(pData->pvIcmpBuffer);
|
---|
653 | # else
|
---|
654 | closesocket(pData->icmp_socket.s);
|
---|
655 | #endif
|
---|
656 |
|
---|
657 | slirp_link_down(pData);
|
---|
658 | slirp_release_dns_list(pData);
|
---|
659 | #ifdef RT_OS_WINDOWS
|
---|
660 | WSACleanup();
|
---|
661 | #endif
|
---|
662 | #ifdef VBOX_WITH_SLIRP_ALIAS
|
---|
663 | while(!LIST_EMPTY(&instancehead)) {
|
---|
664 | struct libalias *la = LIST_FIRST(&instancehead);
|
---|
665 | /* libalias do all clean up */
|
---|
666 | LibAliasUninit(la);
|
---|
667 | }
|
---|
668 | #endif
|
---|
669 | #ifdef LOG_ENABLED
|
---|
670 | Log(("\n"
|
---|
671 | "NAT statistics\n"
|
---|
672 | "--------------\n"
|
---|
673 | "\n"));
|
---|
674 | ipstats(pData);
|
---|
675 | tcpstats(pData);
|
---|
676 | udpstats(pData);
|
---|
677 | icmpstats(pData);
|
---|
678 | mbufstats(pData);
|
---|
679 | sockstats(pData);
|
---|
680 | Log(("\n"
|
---|
681 | "\n"
|
---|
682 | "\n"));
|
---|
683 | #endif
|
---|
684 | RTMemFree(pData);
|
---|
685 | }
|
---|
686 |
|
---|
687 |
|
---|
688 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
689 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
690 | #define UPD_NFDS(x) do { if (nfds < (x)) nfds = (x); } while (0)
|
---|
691 |
|
---|
692 | /*
|
---|
693 | * curtime kept to an accuracy of 1ms
|
---|
694 | */
|
---|
695 | static void updtime(PNATState pData)
|
---|
696 | {
|
---|
697 | #ifdef RT_OS_WINDOWS
|
---|
698 | struct _timeb tb;
|
---|
699 |
|
---|
700 | _ftime(&tb);
|
---|
701 | curtime = (u_int)tb.time * (u_int)1000;
|
---|
702 | curtime += (u_int)tb.millitm;
|
---|
703 | #else
|
---|
704 | gettimeofday(&tt, 0);
|
---|
705 |
|
---|
706 | curtime = (u_int)tt.tv_sec * (u_int)1000;
|
---|
707 | curtime += (u_int)tt.tv_usec / (u_int)1000;
|
---|
708 |
|
---|
709 | if ((tt.tv_usec % 1000) >= 500)
|
---|
710 | curtime++;
|
---|
711 | #endif
|
---|
712 | }
|
---|
713 |
|
---|
714 | #ifdef RT_OS_WINDOWS
|
---|
715 | void slirp_select_fill(PNATState pData, int *pnfds)
|
---|
716 | #else /* RT_OS_WINDOWS */
|
---|
717 | void slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
|
---|
718 | #endif /* !RT_OS_WINDOWS */
|
---|
719 | {
|
---|
720 | struct socket *so, *so_next;
|
---|
721 | int nfds;
|
---|
722 | #if defined(RT_OS_WINDOWS)
|
---|
723 | int rc;
|
---|
724 | int error;
|
---|
725 | #else
|
---|
726 | int poll_index = 0;
|
---|
727 | #endif
|
---|
728 | int i;
|
---|
729 |
|
---|
730 | STAM_PROFILE_START(&pData->StatFill, a);
|
---|
731 |
|
---|
732 | nfds = *pnfds;
|
---|
733 |
|
---|
734 | /*
|
---|
735 | * First, TCP sockets
|
---|
736 | */
|
---|
737 | do_slowtimo = 0;
|
---|
738 | if (!link_up)
|
---|
739 | goto done;
|
---|
740 | /*
|
---|
741 | * *_slowtimo needs calling if there are IP fragments
|
---|
742 | * in the fragment queue, or there are TCP connections active
|
---|
743 | */
|
---|
744 | /* XXX:
|
---|
745 | * triggering of fragment expiration should be the same but use new macroses
|
---|
746 | */
|
---|
747 | do_slowtimo = (tcb.so_next != &tcb);
|
---|
748 | if (!do_slowtimo)
|
---|
749 | {
|
---|
750 | for (i = 0; i < IPREASS_NHASH; i++)
|
---|
751 | {
|
---|
752 | if (!TAILQ_EMPTY(&ipq[i]))
|
---|
753 | {
|
---|
754 | do_slowtimo = 1;
|
---|
755 | break;
|
---|
756 | }
|
---|
757 | }
|
---|
758 | }
|
---|
759 | ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
|
---|
760 |
|
---|
761 | STAM_COUNTER_RESET(&pData->StatTCP);
|
---|
762 | STAM_COUNTER_RESET(&pData->StatTCPHot);
|
---|
763 |
|
---|
764 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
765 | /* { */
|
---|
766 | #if !defined(RT_OS_WINDOWS)
|
---|
767 | so->so_poll_index = -1;
|
---|
768 | #endif
|
---|
769 | STAM_COUNTER_INC(&pData->StatTCP);
|
---|
770 |
|
---|
771 | /*
|
---|
772 | * See if we need a tcp_fasttimo
|
---|
773 | */
|
---|
774 | if ( time_fasttimo == 0
|
---|
775 | && so->so_tcpcb != NULL
|
---|
776 | && so->so_tcpcb->t_flags & TF_DELACK)
|
---|
777 | time_fasttimo = curtime; /* Flag when we want a fasttimo */
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * NOFDREF can include still connecting to local-host,
|
---|
781 | * newly socreated() sockets etc. Don't want to select these.
|
---|
782 | */
|
---|
783 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
784 | CONTINUE(tcp);
|
---|
785 |
|
---|
786 | /*
|
---|
787 | * Set for reading sockets which are accepting
|
---|
788 | */
|
---|
789 | if (so->so_state & SS_FACCEPTCONN)
|
---|
790 | {
|
---|
791 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
792 | TCP_ENGAGE_EVENT1(so, readfds);
|
---|
793 | CONTINUE(tcp);
|
---|
794 | }
|
---|
795 |
|
---|
796 | /*
|
---|
797 | * Set for writing sockets which are connecting
|
---|
798 | */
|
---|
799 | if (so->so_state & SS_ISFCONNECTING)
|
---|
800 | {
|
---|
801 | Log2(("connecting %R[natsock] engaged\n",so));
|
---|
802 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
803 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
804 | }
|
---|
805 |
|
---|
806 | /*
|
---|
807 | * Set for writing if we are connected, can send more, and
|
---|
808 | * we have something to send
|
---|
809 | */
|
---|
810 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
|
---|
811 | {
|
---|
812 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
813 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
814 | }
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * Set for reading (and urgent data) if we are connected, can
|
---|
818 | * receive more, and we have room for it XXX /2 ?
|
---|
819 | */
|
---|
820 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
|
---|
821 | {
|
---|
822 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
823 | TCP_ENGAGE_EVENT2(so, readfds, xfds);
|
---|
824 | }
|
---|
825 | LOOP_LABEL(tcp, so, so_next);
|
---|
826 | }
|
---|
827 |
|
---|
828 | /*
|
---|
829 | * UDP sockets
|
---|
830 | */
|
---|
831 | STAM_COUNTER_RESET(&pData->StatUDP);
|
---|
832 | STAM_COUNTER_RESET(&pData->StatUDPHot);
|
---|
833 |
|
---|
834 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
835 | /* { */
|
---|
836 |
|
---|
837 | STAM_COUNTER_INC(&pData->StatUDP);
|
---|
838 | #if !defined(RT_OS_WINDOWS)
|
---|
839 | so->so_poll_index = -1;
|
---|
840 | #endif
|
---|
841 |
|
---|
842 | /*
|
---|
843 | * See if it's timed out
|
---|
844 | */
|
---|
845 | if (so->so_expire)
|
---|
846 | {
|
---|
847 | if (so->so_expire <= curtime)
|
---|
848 | {
|
---|
849 | Log2(("NAT: %R[natsock] expired\n", so));
|
---|
850 | if (so->so_timeout != NULL)
|
---|
851 | {
|
---|
852 | so->so_timeout(pData, so, so->so_timeout_arg);
|
---|
853 | }
|
---|
854 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
855 | /* we need so_next for continue our cycle*/
|
---|
856 | so_next = so->so_next;
|
---|
857 | #endif
|
---|
858 | UDP_DETACH(pData, so, so_next);
|
---|
859 | CONTINUE_NO_UNLOCK(udp);
|
---|
860 | }
|
---|
861 | else
|
---|
862 | do_slowtimo = 1; /* Let socket expire */
|
---|
863 | }
|
---|
864 |
|
---|
865 | /*
|
---|
866 | * When UDP packets are received from over the link, they're
|
---|
867 | * sendto()'d straight away, so no need for setting for writing
|
---|
868 | * Limit the number of packets queued by this session to 4.
|
---|
869 | * Note that even though we try and limit this to 4 packets,
|
---|
870 | * the session could have more queued if the packets needed
|
---|
871 | * to be fragmented.
|
---|
872 | *
|
---|
873 | * (XXX <= 4 ?)
|
---|
874 | */
|
---|
875 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
|
---|
876 | {
|
---|
877 | STAM_COUNTER_INC(&pData->StatUDPHot);
|
---|
878 | UDP_ENGAGE_EVENT(so, readfds);
|
---|
879 | }
|
---|
880 | LOOP_LABEL(udp, so, so_next);
|
---|
881 | }
|
---|
882 | done:
|
---|
883 |
|
---|
884 | #if defined(RT_OS_WINDOWS)
|
---|
885 | *pnfds = VBOX_EVENT_COUNT;
|
---|
886 | #else /* RT_OS_WINDOWS */
|
---|
887 | AssertRelease(poll_index <= *pnfds);
|
---|
888 | *pnfds = poll_index;
|
---|
889 | #endif /* !RT_OS_WINDOWS */
|
---|
890 |
|
---|
891 | STAM_PROFILE_STOP(&pData->StatFill, a);
|
---|
892 | }
|
---|
893 |
|
---|
894 | #if defined(RT_OS_WINDOWS)
|
---|
895 | void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
|
---|
896 | #else /* RT_OS_WINDOWS */
|
---|
897 | void slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
|
---|
898 | #endif /* !RT_OS_WINDOWS */
|
---|
899 | {
|
---|
900 | struct socket *so, *so_next;
|
---|
901 | int ret;
|
---|
902 | #if defined(RT_OS_WINDOWS)
|
---|
903 | WSANETWORKEVENTS NetworkEvents;
|
---|
904 | int rc;
|
---|
905 | int error;
|
---|
906 | #else
|
---|
907 | int poll_index = 0;
|
---|
908 | #endif
|
---|
909 |
|
---|
910 | STAM_PROFILE_START(&pData->StatPoll, a);
|
---|
911 |
|
---|
912 | /* Update time */
|
---|
913 | updtime(pData);
|
---|
914 |
|
---|
915 | /*
|
---|
916 | * See if anything has timed out
|
---|
917 | */
|
---|
918 | if (link_up)
|
---|
919 | {
|
---|
920 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
|
---|
921 | {
|
---|
922 | STAM_PROFILE_START(&pData->StatFastTimer, a);
|
---|
923 | tcp_fasttimo(pData);
|
---|
924 | time_fasttimo = 0;
|
---|
925 | STAM_PROFILE_STOP(&pData->StatFastTimer, a);
|
---|
926 | }
|
---|
927 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
|
---|
928 | {
|
---|
929 | STAM_PROFILE_START(&pData->StatSlowTimer, a);
|
---|
930 | ip_slowtimo(pData);
|
---|
931 | tcp_slowtimo(pData);
|
---|
932 | last_slowtimo = curtime;
|
---|
933 | STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
|
---|
934 | }
|
---|
935 | }
|
---|
936 | #if defined(RT_OS_WINDOWS)
|
---|
937 | if (fTimeout)
|
---|
938 | return; /* only timer update */
|
---|
939 | #endif
|
---|
940 |
|
---|
941 | /*
|
---|
942 | * Check sockets
|
---|
943 | */
|
---|
944 | if (!link_up)
|
---|
945 | goto done;
|
---|
946 | #if defined(RT_OS_WINDOWS)
|
---|
947 | /*XXX: before renaming please make see define
|
---|
948 | * fIcmp in slirp_state.h
|
---|
949 | */
|
---|
950 | if (fIcmp)
|
---|
951 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
952 | #else
|
---|
953 | if ( (pData->icmp_socket.s != -1)
|
---|
954 | && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
|
---|
955 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
956 | #endif
|
---|
957 | /*
|
---|
958 | * Check TCP sockets
|
---|
959 | */
|
---|
960 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
961 | /* { */
|
---|
962 |
|
---|
963 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
964 | if ( so->so_state & SS_NOFDREF
|
---|
965 | && so->so_deleted == 1)
|
---|
966 | {
|
---|
967 | struct socket *son, *sop = NULL;
|
---|
968 | QSOCKET_LOCK(tcb);
|
---|
969 | if (so->so_next != NULL)
|
---|
970 | {
|
---|
971 | if (so->so_next != &tcb)
|
---|
972 | SOCKET_LOCK(so->so_next);
|
---|
973 | son = so->so_next;
|
---|
974 | }
|
---|
975 | if ( so->so_prev != &tcb
|
---|
976 | && so->so_prev != NULL)
|
---|
977 | {
|
---|
978 | SOCKET_LOCK(so->so_prev);
|
---|
979 | sop = so->so_prev;
|
---|
980 | }
|
---|
981 | QSOCKET_UNLOCK(tcb);
|
---|
982 | remque(pData, so);
|
---|
983 | NSOCK_DEC();
|
---|
984 | SOCKET_UNLOCK(so);
|
---|
985 | SOCKET_LOCK_DESTROY(so);
|
---|
986 | RTMemFree(so);
|
---|
987 | so_next = son;
|
---|
988 | if (sop != NULL)
|
---|
989 | SOCKET_UNLOCK(sop);
|
---|
990 | CONTINUE_NO_UNLOCK(tcp);
|
---|
991 | }
|
---|
992 | #endif
|
---|
993 | /*
|
---|
994 | * FD_ISSET is meaningless on these sockets
|
---|
995 | * (and they can crash the program)
|
---|
996 | */
|
---|
997 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
998 | CONTINUE(tcp);
|
---|
999 |
|
---|
1000 | POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
1001 |
|
---|
1002 | LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
|
---|
1003 |
|
---|
1004 |
|
---|
1005 | /*
|
---|
1006 | * Check for URG data
|
---|
1007 | * This will soread as well, so no need to
|
---|
1008 | * test for readfds below if this succeeds
|
---|
1009 | */
|
---|
1010 |
|
---|
1011 | /* out-of-band data */
|
---|
1012 | if (CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
1013 | {
|
---|
1014 | sorecvoob(pData, so);
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | /*
|
---|
1018 | * Check sockets for reading
|
---|
1019 | */
|
---|
1020 | else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
1021 | || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
|
---|
1022 | {
|
---|
1023 | /*
|
---|
1024 | * Check for incoming connections
|
---|
1025 | */
|
---|
1026 | if (so->so_state & SS_FACCEPTCONN)
|
---|
1027 | {
|
---|
1028 | TCP_CONNECT(pData, so);
|
---|
1029 | #if defined(RT_OS_WINDOWS)
|
---|
1030 | if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
|
---|
1031 | #endif
|
---|
1032 | CONTINUE(tcp);
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | ret = soread(pData, so);
|
---|
1036 | /* Output it if we read something */
|
---|
1037 | if (RT_LIKELY(ret > 0))
|
---|
1038 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | #if defined(RT_OS_WINDOWS)
|
---|
1042 | /*
|
---|
1043 | * Check for FD_CLOSE events.
|
---|
1044 | * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
|
---|
1045 | */
|
---|
1046 | if ( (NetworkEvents.lNetworkEvents & FD_CLOSE)
|
---|
1047 | || (so->so_close == 1))
|
---|
1048 | {
|
---|
1049 | so->so_close = 1; /* mark it */
|
---|
1050 | /*
|
---|
1051 | * drain the socket
|
---|
1052 | */
|
---|
1053 | for (;;)
|
---|
1054 | {
|
---|
1055 | ret = soread(pData, so);
|
---|
1056 | if (ret > 0)
|
---|
1057 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
1058 | else
|
---|
1059 | break;
|
---|
1060 | }
|
---|
1061 | CONTINUE(tcp);
|
---|
1062 | }
|
---|
1063 | #endif
|
---|
1064 |
|
---|
1065 | /*
|
---|
1066 | * Check sockets for writing
|
---|
1067 | */
|
---|
1068 | if (CHECK_FD_SET(so, NetworkEvents, writefds))
|
---|
1069 | {
|
---|
1070 | /*
|
---|
1071 | * Check for non-blocking, still-connecting sockets
|
---|
1072 | */
|
---|
1073 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1074 | {
|
---|
1075 | Log2(("connecting %R[natsock] catched\n", so));
|
---|
1076 | /* Connected */
|
---|
1077 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
1078 |
|
---|
1079 | /*
|
---|
1080 | * This should be probably guarded by PROBE_CONN too. Anyway,
|
---|
1081 | * we disable it on OS/2 because the below send call returns
|
---|
1082 | * EFAULT which causes the opened TCP socket to close right
|
---|
1083 | * after it has been opened and connected.
|
---|
1084 | */
|
---|
1085 | #ifndef RT_OS_OS2
|
---|
1086 | ret = send(so->s, (const char *)&ret, 0, 0);
|
---|
1087 | if (ret < 0)
|
---|
1088 | {
|
---|
1089 | /* XXXXX Must fix, zero bytes is a NOP */
|
---|
1090 | if ( errno == EAGAIN
|
---|
1091 | || errno == EWOULDBLOCK
|
---|
1092 | || errno == EINPROGRESS
|
---|
1093 | || errno == ENOTCONN)
|
---|
1094 | CONTINUE(tcp);
|
---|
1095 |
|
---|
1096 | /* else failed */
|
---|
1097 | so->so_state = SS_NOFDREF;
|
---|
1098 | }
|
---|
1099 | /* else so->so_state &= ~SS_ISFCONNECTING; */
|
---|
1100 | #endif
|
---|
1101 |
|
---|
1102 | /*
|
---|
1103 | * Continue tcp_input
|
---|
1104 | */
|
---|
1105 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1106 | /* continue; */
|
---|
1107 | }
|
---|
1108 | else
|
---|
1109 | SOWRITE(ret, pData, so);
|
---|
1110 | /*
|
---|
1111 | * XXX If we wrote something (a lot), there could be the need
|
---|
1112 | * for a window update. In the worst case, the remote will send
|
---|
1113 | * a window probe to get things going again.
|
---|
1114 | */
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | /*
|
---|
1118 | * Probe a still-connecting, non-blocking socket
|
---|
1119 | * to check if it's still alive
|
---|
1120 | */
|
---|
1121 | #ifdef PROBE_CONN
|
---|
1122 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1123 | {
|
---|
1124 | ret = recv(so->s, (char *)&ret, 0, 0);
|
---|
1125 |
|
---|
1126 | if (ret < 0)
|
---|
1127 | {
|
---|
1128 | /* XXX */
|
---|
1129 | if ( errno == EAGAIN
|
---|
1130 | || errno == EWOULDBLOCK
|
---|
1131 | || errno == EINPROGRESS
|
---|
1132 | || errno == ENOTCONN)
|
---|
1133 | {
|
---|
1134 | CONTINUE(tcp); /* Still connecting, continue */
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | /* else failed */
|
---|
1138 | so->so_state = SS_NOFDREF;
|
---|
1139 |
|
---|
1140 | /* tcp_input will take care of it */
|
---|
1141 | }
|
---|
1142 | else
|
---|
1143 | {
|
---|
1144 | ret = send(so->s, &ret, 0, 0);
|
---|
1145 | if (ret < 0)
|
---|
1146 | {
|
---|
1147 | /* XXX */
|
---|
1148 | if ( errno == EAGAIN
|
---|
1149 | || errno == EWOULDBLOCK
|
---|
1150 | || errno == EINPROGRESS
|
---|
1151 | || errno == ENOTCONN)
|
---|
1152 | {
|
---|
1153 | CONTINUE(tcp);
|
---|
1154 | }
|
---|
1155 | /* else failed */
|
---|
1156 | so->so_state = SS_NOFDREF;
|
---|
1157 | }
|
---|
1158 | else
|
---|
1159 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
1160 |
|
---|
1161 | }
|
---|
1162 | TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
|
---|
1163 | } /* SS_ISFCONNECTING */
|
---|
1164 | #endif
|
---|
1165 | #ifndef RT_OS_WINDOWS
|
---|
1166 | if ( UNIX_CHECK_FD_SET(so, NetworkEvents, rdhup)
|
---|
1167 | || UNIX_CHECK_FD_SET(so, NetworkEvents, rderr))
|
---|
1168 | {
|
---|
1169 | int err;
|
---|
1170 | int inq, outq;
|
---|
1171 | int status;
|
---|
1172 | socklen_t optlen = sizeof(int);
|
---|
1173 | inq = outq = 0;
|
---|
1174 | status = getsockopt(so->s, SOL_SOCKET, SO_ERROR, &err, &optlen);
|
---|
1175 | if (status != 0)
|
---|
1176 | Log(("NAT: can't get error status from %R[natsock]\n", so));
|
---|
1177 | #ifndef RT_OS_SOLARIS
|
---|
1178 | status = ioctl(so->s, FIONREAD, &inq); /* tcp(7) recommends SIOCINQ which is Linux specific */
|
---|
1179 | if (status != 0 || status != EINVAL)
|
---|
1180 | {
|
---|
1181 | /* EINVAL returned if socket in listen state tcp(7)*/
|
---|
1182 | Log(("NAT: can't get depth of IN queue status from %R[natsock]\n", so));
|
---|
1183 | }
|
---|
1184 | status = ioctl(so->s, TIOCOUTQ, &outq); /* SIOCOUTQ see previous comment */
|
---|
1185 | if (status != 0)
|
---|
1186 | Log(("NAT: can't get depth of OUT queue from %R[natsock]\n", so));
|
---|
1187 | #else
|
---|
1188 | /*
|
---|
1189 | * Solaris has bit different ioctl commands and its handlings
|
---|
1190 | * hint: streamio(7) I_NREAD
|
---|
1191 | */
|
---|
1192 | #endif
|
---|
1193 | if ( so->so_state & SS_ISFCONNECTING
|
---|
1194 | || UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1195 | {
|
---|
1196 | /**
|
---|
1197 | * Check if we need here take care about gracefull connection
|
---|
1198 | * @todo try with proxy server
|
---|
1199 | */
|
---|
1200 | if (UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1201 | {
|
---|
1202 | /*
|
---|
1203 | * Never meet inq != 0 or outq != 0, anyway let it stay for a while
|
---|
1204 | * in case it happens we'll able to detect it.
|
---|
1205 | * Give TCP/IP stack wait or expire the socket.
|
---|
1206 | */
|
---|
1207 | Log(("NAT: %R[natsock] err(%d:%s) s(in:%d,out:%d)happens on read I/O, "
|
---|
1208 | "other side close connection \n", so, err, strerror(err), inq, outq));
|
---|
1209 | CONTINUE(tcp);
|
---|
1210 | }
|
---|
1211 | goto tcp_input_close;
|
---|
1212 | }
|
---|
1213 | if ( !UNIX_CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
1214 | && !UNIX_CHECK_FD_SET(so, NetworkEvents, writefds)
|
---|
1215 | && !UNIX_CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
1216 | {
|
---|
1217 | Log(("NAT: system expires the socket %R[natsock] err(%d:%s) s(in:%d,out:%d) happens on non-I/O. ",
|
---|
1218 | so, err, strerror(err), inq, outq));
|
---|
1219 | goto tcp_input_close;
|
---|
1220 | }
|
---|
1221 | Log(("NAT: %R[natsock] we've met(%d:%s) s(in:%d, out:%d) unhandled combination hup (%d) "
|
---|
1222 | "rederr(%d) on (r:%d, w:%d, x:%d)\n",
|
---|
1223 | so, err, strerror(err),
|
---|
1224 | inq, outq,
|
---|
1225 | UNIX_CHECK_FD_SET(so, ign, rdhup),
|
---|
1226 | UNIX_CHECK_FD_SET(so, ign, rderr),
|
---|
1227 | UNIX_CHECK_FD_SET(so, ign, readfds),
|
---|
1228 | UNIX_CHECK_FD_SET(so, ign, writefds),
|
---|
1229 | UNIX_CHECK_FD_SET(so, ign, xfds)));
|
---|
1230 | /*
|
---|
1231 | * Give OS's TCP/IP stack a chance to resolve an issue or expire the socket.
|
---|
1232 | */
|
---|
1233 | CONTINUE(tcp);
|
---|
1234 | tcp_input_close:
|
---|
1235 | so->so_state = SS_NOFDREF; /*cause connection valid tcp connection termination and socket closing */
|
---|
1236 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1237 | CONTINUE(tcp);
|
---|
1238 | }
|
---|
1239 | #endif
|
---|
1240 | LOOP_LABEL(tcp, so, so_next);
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | /*
|
---|
1244 | * Now UDP sockets.
|
---|
1245 | * Incoming packets are sent straight away, they're not buffered.
|
---|
1246 | * Incoming UDP data isn't buffered either.
|
---|
1247 | */
|
---|
1248 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
1249 | /* { */
|
---|
1250 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1251 | if ( so->so_state & SS_NOFDREF
|
---|
1252 | && so->so_deleted == 1)
|
---|
1253 | {
|
---|
1254 | struct socket *son, *sop = NULL;
|
---|
1255 | QSOCKET_LOCK(udb);
|
---|
1256 | if (so->so_next != NULL)
|
---|
1257 | {
|
---|
1258 | if (so->so_next != &udb)
|
---|
1259 | SOCKET_LOCK(so->so_next);
|
---|
1260 | son = so->so_next;
|
---|
1261 | }
|
---|
1262 | if ( so->so_prev != &udb
|
---|
1263 | && so->so_prev != NULL)
|
---|
1264 | {
|
---|
1265 | SOCKET_LOCK(so->so_prev);
|
---|
1266 | sop = so->so_prev;
|
---|
1267 | }
|
---|
1268 | QSOCKET_UNLOCK(udb);
|
---|
1269 | remque(pData, so);
|
---|
1270 | NSOCK_DEC();
|
---|
1271 | SOCKET_UNLOCK(so);
|
---|
1272 | SOCKET_LOCK_DESTROY(so);
|
---|
1273 | RTMemFree(so);
|
---|
1274 | so_next = son;
|
---|
1275 | if (sop != NULL)
|
---|
1276 | SOCKET_UNLOCK(sop);
|
---|
1277 | CONTINUE_NO_UNLOCK(udp);
|
---|
1278 | }
|
---|
1279 | #endif
|
---|
1280 | POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
1281 |
|
---|
1282 | LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
|
---|
1283 |
|
---|
1284 | if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1285 | {
|
---|
1286 | SORECVFROM(pData, so);
|
---|
1287 | }
|
---|
1288 | LOOP_LABEL(udp, so, so_next);
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | done:
|
---|
1292 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
1293 | /*
|
---|
1294 | * See if we can start outputting
|
---|
1295 | */
|
---|
1296 | if (if_queued && link_up)
|
---|
1297 | if_start(pData);
|
---|
1298 | #endif
|
---|
1299 |
|
---|
1300 | STAM_PROFILE_STOP(&pData->StatPoll, a);
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | #ifndef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
|
---|
1304 | #define ETH_ALEN 6
|
---|
1305 | #define ETH_HLEN 14
|
---|
1306 |
|
---|
1307 | #define ARPOP_REQUEST 1 /* ARP request */
|
---|
1308 | #define ARPOP_REPLY 2 /* ARP reply */
|
---|
1309 |
|
---|
1310 | struct ethhdr
|
---|
1311 | {
|
---|
1312 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
1313 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
1314 | unsigned short h_proto; /* packet type ID field */
|
---|
1315 | };
|
---|
1316 | AssertCompileSize(struct ethhdr, 14);
|
---|
1317 | #endif
|
---|
1318 |
|
---|
1319 | struct arphdr
|
---|
1320 | {
|
---|
1321 | unsigned short ar_hrd; /* format of hardware address */
|
---|
1322 | unsigned short ar_pro; /* format of protocol address */
|
---|
1323 | unsigned char ar_hln; /* length of hardware address */
|
---|
1324 | unsigned char ar_pln; /* length of protocol address */
|
---|
1325 | unsigned short ar_op; /* ARP opcode (command) */
|
---|
1326 |
|
---|
1327 | /*
|
---|
1328 | * Ethernet looks like this : This bit is variable sized however...
|
---|
1329 | */
|
---|
1330 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
|
---|
1331 | unsigned char ar_sip[4]; /* sender IP address */
|
---|
1332 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
|
---|
1333 | unsigned char ar_tip[4]; /* target IP address */
|
---|
1334 | };
|
---|
1335 | AssertCompileSize(struct arphdr, 28);
|
---|
1336 |
|
---|
1337 | static void arp_input(PNATState pData, struct mbuf *m)
|
---|
1338 | {
|
---|
1339 | struct ethhdr *eh;
|
---|
1340 | struct ethhdr *reh;
|
---|
1341 | struct arphdr *ah;
|
---|
1342 | struct arphdr *rah;
|
---|
1343 | int ar_op;
|
---|
1344 | struct ex_list *ex_ptr;
|
---|
1345 | uint32_t htip;
|
---|
1346 | uint32_t tip;
|
---|
1347 | struct mbuf *mr;
|
---|
1348 | eh = mtod(m, struct ethhdr *);
|
---|
1349 | ah = (struct arphdr *)&eh[1];
|
---|
1350 | htip = ntohl(*(uint32_t*)ah->ar_tip);
|
---|
1351 | tip = *(uint32_t*)ah->ar_tip;
|
---|
1352 |
|
---|
1353 | mr = m_get(pData);
|
---|
1354 | #ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
|
---|
1355 | reh = mtod(mr, struct ethhdr *);
|
---|
1356 | memcpy(reh->h_source, eh->h_source, ETH_ALEN); /* XXX: if_encap will swap src and dst*/
|
---|
1357 | Log4(("NAT: arp:%R[ether]->%R[ether]\n",
|
---|
1358 | reh->h_source, reh->h_dest));
|
---|
1359 | Log4(("NAT: arp: %R[IP4]\n", &tip));
|
---|
1360 | #endif
|
---|
1361 | mr->m_data += if_maxlinkhdr;
|
---|
1362 | mr->m_len = sizeof(struct arphdr);
|
---|
1363 | rah = mtod(mr, struct arphdr *);
|
---|
1364 |
|
---|
1365 | ar_op = ntohs(ah->ar_op);
|
---|
1366 | switch(ar_op)
|
---|
1367 | {
|
---|
1368 | case ARPOP_REQUEST:
|
---|
1369 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
1370 | if (tip == special_addr.s_addr) goto arp_ok;
|
---|
1371 | #endif
|
---|
1372 | if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
|
---|
1373 | {
|
---|
1374 | if ( CTL_CHECK(htip, CTL_DNS)
|
---|
1375 | || CTL_CHECK(htip, CTL_ALIAS)
|
---|
1376 | || CTL_CHECK(htip, CTL_TFTP))
|
---|
1377 | goto arp_ok;
|
---|
1378 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
|
---|
1379 | {
|
---|
1380 | if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
|
---|
1381 | {
|
---|
1382 | goto arp_ok;
|
---|
1383 | }
|
---|
1384 | }
|
---|
1385 | return;
|
---|
1386 | arp_ok:
|
---|
1387 | rah->ar_hrd = htons(1);
|
---|
1388 | rah->ar_pro = htons(ETH_P_IP);
|
---|
1389 | rah->ar_hln = ETH_ALEN;
|
---|
1390 | rah->ar_pln = 4;
|
---|
1391 | rah->ar_op = htons(ARPOP_REPLY);
|
---|
1392 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
|
---|
1393 |
|
---|
1394 | switch (htip & ~pData->netmask)
|
---|
1395 | {
|
---|
1396 | case CTL_DNS:
|
---|
1397 | case CTL_ALIAS:
|
---|
1398 | rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
|
---|
1399 | break;
|
---|
1400 | default:;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | memcpy(rah->ar_sip, ah->ar_tip, 4);
|
---|
1404 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
|
---|
1405 | memcpy(rah->ar_tip, ah->ar_sip, 4);
|
---|
1406 | if_encap(pData, ETH_P_ARP, mr);
|
---|
1407 | m_free(pData, m);
|
---|
1408 | }
|
---|
1409 | break;
|
---|
1410 | default:
|
---|
1411 | break;
|
---|
1412 | }
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1416 | {
|
---|
1417 | struct mbuf *m;
|
---|
1418 | int proto;
|
---|
1419 | static bool fWarnedIpv6;
|
---|
1420 | struct ethhdr *eh = (struct ethhdr*)pkt;
|
---|
1421 |
|
---|
1422 | Log2(("NAT: slirp_input %d\n", pkt_len));
|
---|
1423 | if (pkt_len < ETH_HLEN)
|
---|
1424 | {
|
---|
1425 | LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
|
---|
1426 | return;
|
---|
1427 | }
|
---|
1428 | Log4(("NAT: in:%R[ether]->%R[ether]\n", &eh->h_source, &eh->h_dest));
|
---|
1429 | #ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
|
---|
1430 | if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) == 0)
|
---|
1431 | {
|
---|
1432 | /* @todo vasily: add ether logging routine in debug.c */
|
---|
1433 | Log(("NAT: packet was addressed to other MAC\n"));
|
---|
1434 | RTMemFree((void *)pkt);
|
---|
1435 | return;
|
---|
1436 | }
|
---|
1437 | #endif
|
---|
1438 |
|
---|
1439 | m = m_get(pData);
|
---|
1440 | if (!m)
|
---|
1441 | {
|
---|
1442 | LogRel(("NAT: can't allocate new mbuf\n"));
|
---|
1443 | return;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | /* Note: we add to align the IP header */
|
---|
1447 |
|
---|
1448 | if (M_FREEROOM(m) < pkt_len)
|
---|
1449 | m_inc(m, pkt_len);
|
---|
1450 |
|
---|
1451 | m->m_len = pkt_len ;
|
---|
1452 | memcpy(m->m_data, pkt, pkt_len);
|
---|
1453 |
|
---|
1454 | proto = ntohs(*(uint16_t *)(pkt + 12));
|
---|
1455 | switch(proto)
|
---|
1456 | {
|
---|
1457 | case ETH_P_ARP:
|
---|
1458 | arp_input(pData, m);
|
---|
1459 | break;
|
---|
1460 | case ETH_P_IP:
|
---|
1461 | /* Update time. Important if the network is very quiet, as otherwise
|
---|
1462 | * the first outgoing connection gets an incorrect timestamp. */
|
---|
1463 | updtime(pData);
|
---|
1464 | m_adj(m, ETH_HLEN);
|
---|
1465 | ip_input(pData, m);
|
---|
1466 | break;
|
---|
1467 | case ETH_P_IPV6:
|
---|
1468 | m_free(pData, m);
|
---|
1469 | if (!fWarnedIpv6)
|
---|
1470 | {
|
---|
1471 | LogRel(("NAT: IPv6 not supported\n"));
|
---|
1472 | fWarnedIpv6 = true;
|
---|
1473 | }
|
---|
1474 | break;
|
---|
1475 | default:
|
---|
1476 | Log(("NAT: Unsupported protocol %x\n", proto));
|
---|
1477 | m_free(pData, m);
|
---|
1478 | break;
|
---|
1479 | }
|
---|
1480 | RTMemFree((void *)pkt);
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | /* output the IP packet to the ethernet device */
|
---|
1484 | void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
|
---|
1485 | {
|
---|
1486 | struct ethhdr *eh;
|
---|
1487 | uint8_t *buf = RTMemAlloc(1600);
|
---|
1488 |
|
---|
1489 | m->m_data -= if_maxlinkhdr;
|
---|
1490 | m->m_len += ETH_HLEN;
|
---|
1491 | eh = mtod(m, struct ethhdr *);
|
---|
1492 |
|
---|
1493 | if(MBUF_HEAD(m) != m->m_data)
|
---|
1494 | {
|
---|
1495 | LogRel(("NAT: ethernet detects corruption of the packet"));
|
---|
1496 | AssertMsgFailed(("!!Ethernet frame corrupted!!"));
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | #ifndef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
|
---|
1500 | memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
|
---|
1501 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
1502 | /* XXX: not correct */
|
---|
1503 | eh->h_source[5] = CTL_ALIAS;
|
---|
1504 | #else
|
---|
1505 | if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) != 0)
|
---|
1506 | {
|
---|
1507 | memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
|
---|
1508 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN);
|
---|
1509 | Assert(memcmp(eh->h_dest, special_ethaddr, ETH_ALEN) != 0);
|
---|
1510 | if (memcmp(eh->h_dest, zerro_ethaddr, ETH_ALEN) == 0)
|
---|
1511 | {
|
---|
1512 | /* don't do anything */
|
---|
1513 | goto done;
|
---|
1514 | }
|
---|
1515 | }
|
---|
1516 | #endif
|
---|
1517 | eh->h_proto = htons(eth_proto);
|
---|
1518 | #if 0
|
---|
1519 | slirp_output(pData->pvUser, m, mtod(m, uint8_t *), m->m_len);
|
---|
1520 | #else
|
---|
1521 | memcpy(buf, mtod(m, uint8_t *), m->m_len);
|
---|
1522 | slirp_output(pData->pvUser, NULL, buf, m->m_len);
|
---|
1523 | done:
|
---|
1524 | m_free(pData, m);
|
---|
1525 | #endif
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | int slirp_redir(PNATState pData, int is_udp, struct in_addr host_addr, int host_port,
|
---|
1529 | struct in_addr guest_addr, int guest_port)
|
---|
1530 | {
|
---|
1531 | struct socket *so;
|
---|
1532 | #ifdef VBOX_WITH_SLIRP_ALIAS
|
---|
1533 | struct alias_link *link;
|
---|
1534 | struct libalias *lib;
|
---|
1535 | int flags;
|
---|
1536 | struct sockaddr sa;
|
---|
1537 | struct sockaddr_in *psin;
|
---|
1538 | socklen_t socketlen;
|
---|
1539 | struct in_addr alias;
|
---|
1540 | int rc;
|
---|
1541 | #endif
|
---|
1542 | Log2(("NAT: set redirect %s hp:%d gp:%d\n", (is_udp?"UDP":"TCP"), host_port, guest_port));
|
---|
1543 | if (is_udp)
|
---|
1544 | {
|
---|
1545 | so = udp_listen(pData, host_addr.s_addr, htons(host_port), guest_addr.s_addr,
|
---|
1546 | htons(guest_port), 0);
|
---|
1547 | }
|
---|
1548 | else
|
---|
1549 | {
|
---|
1550 | so = solisten(pData, host_addr.s_addr, htons(host_port), guest_addr.s_addr,
|
---|
1551 | htons(guest_port), 0);
|
---|
1552 | }
|
---|
1553 | if (so == NULL)
|
---|
1554 | {
|
---|
1555 | return -1;
|
---|
1556 | }
|
---|
1557 | #ifndef VBOX_WITH_SLIRP_ALIAS
|
---|
1558 | Log2(("NAT: redirecting socket %R[natsock]\n", so));
|
---|
1559 | return (so != NULL ? 0 : -1);
|
---|
1560 | #else
|
---|
1561 |
|
---|
1562 | psin = (struct sockaddr_in *)&sa;
|
---|
1563 | psin->sin_family = AF_INET;
|
---|
1564 | psin->sin_port = 0;
|
---|
1565 | psin->sin_addr.s_addr = INADDR_ANY;
|
---|
1566 | socketlen = sizeof(struct sockaddr);
|
---|
1567 |
|
---|
1568 | rc = getsockname(so->s, &sa, &socketlen);
|
---|
1569 | if (rc < 0 || sa.sa_family != AF_INET)
|
---|
1570 | {
|
---|
1571 | Log(("NAT: can't get socket's name\n"));
|
---|
1572 | return 1;
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | psin = (struct sockaddr_in *)&sa;
|
---|
1576 |
|
---|
1577 | #if 1
|
---|
1578 | lib = LibAliasInit(pData, NULL);
|
---|
1579 | flags = LibAliasSetMode(lib, 0, 0);
|
---|
1580 | flags |= PKT_ALIAS_LOG; /* set logging */
|
---|
1581 | flags |= PKT_ALIAS_REVERSE; /* set logging */
|
---|
1582 | flags = LibAliasSetMode(lib, flags, ~0);
|
---|
1583 | #else
|
---|
1584 | lib = LIST_FIRST(&instancehead);
|
---|
1585 | #endif
|
---|
1586 |
|
---|
1587 | alias.s_addr = htonl(ntohl(guest_addr.s_addr) | CTL_ALIAS);
|
---|
1588 | link = LibAliasRedirectPort(lib, psin->sin_addr, htons(host_port),
|
---|
1589 | alias, htons(guest_port),
|
---|
1590 | special_addr, -1, /* not very clear for now*/
|
---|
1591 | (is_udp ? IPPROTO_UDP : IPPROTO_TCP));
|
---|
1592 | if (link == NULL)
|
---|
1593 | {
|
---|
1594 | Log(("NAT: can't create redirect\n"));
|
---|
1595 | return 1;
|
---|
1596 | }
|
---|
1597 | so->so_la = lib;
|
---|
1598 |
|
---|
1599 | return 0;
|
---|
1600 | #endif
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
|
---|
1604 | int guest_port)
|
---|
1605 | {
|
---|
1606 | return add_exec(&exec_list, do_pty, (char *)args,
|
---|
1607 | addr_low_byte, htons(guest_port));
|
---|
1608 | }
|
---|
1609 |
|
---|
1610 | void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
|
---|
1611 | {
|
---|
1612 | #ifndef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
|
---|
1613 | memcpy(client_ethaddr, ethaddr, ETH_ALEN);
|
---|
1614 | #endif
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | #if defined(RT_OS_WINDOWS)
|
---|
1618 | HANDLE *slirp_get_events(PNATState pData)
|
---|
1619 | {
|
---|
1620 | return pData->phEvents;
|
---|
1621 | }
|
---|
1622 | void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
|
---|
1623 | {
|
---|
1624 | pData->phEvents[index] = hEvent;
|
---|
1625 | }
|
---|
1626 | #endif
|
---|
1627 |
|
---|
1628 | unsigned int slirp_get_timeout_ms(PNATState pData)
|
---|
1629 | {
|
---|
1630 | if (link_up)
|
---|
1631 | {
|
---|
1632 | if (time_fasttimo)
|
---|
1633 | return 2;
|
---|
1634 | if (do_slowtimo)
|
---|
1635 | return 500; /* see PR_SLOWHZ */
|
---|
1636 | }
|
---|
1637 | return 0;
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | #ifndef RT_OS_WINDOWS
|
---|
1641 | int slirp_get_nsock(PNATState pData)
|
---|
1642 | {
|
---|
1643 | return pData->nsock;
|
---|
1644 | }
|
---|
1645 | #endif
|
---|
1646 |
|
---|
1647 | /*
|
---|
1648 | * this function called from NAT thread
|
---|
1649 | */
|
---|
1650 | void slirp_post_sent(PNATState pData, void *pvArg)
|
---|
1651 | {
|
---|
1652 | struct socket *so = 0;
|
---|
1653 | struct tcpcb *tp = 0;
|
---|
1654 | struct mbuf *m = (struct mbuf *)pvArg;
|
---|
1655 | m_free(pData, m);
|
---|
1656 | }
|
---|
1657 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1658 | void slirp_process_queue(PNATState pData)
|
---|
1659 | {
|
---|
1660 | RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
|
---|
1661 | }
|
---|
1662 | void *slirp_get_queue(PNATState pData)
|
---|
1663 | {
|
---|
1664 | return pData->pReqQueue;
|
---|
1665 | }
|
---|
1666 | #endif
|
---|
1667 |
|
---|
1668 | uint16_t slirp_get_service(int proto, uint16_t dport, uint16_t sport)
|
---|
1669 | {
|
---|
1670 | uint16_t hdport, hsport, service;
|
---|
1671 | hdport = ntohs(dport);
|
---|
1672 | hsport = ntohs(sport);
|
---|
1673 | Log2(("proto: %d, dport: %d sport: %d\n", proto, hdport, hsport));
|
---|
1674 | service = 0;
|
---|
1675 | #if 0
|
---|
1676 | /* Always return 0 here */
|
---|
1677 | switch (hdport)
|
---|
1678 | {
|
---|
1679 | case 500:
|
---|
1680 | if (hsport != 500) /* vpnc by default try operate in src:500/dst:500 mode*/
|
---|
1681 | /* Not sure why this make Cisco VPN client's connection more stable,
|
---|
1682 | * at least on some servers
|
---|
1683 | */
|
---|
1684 | service = sport;
|
---|
1685 | break;
|
---|
1686 | }
|
---|
1687 | #endif
|
---|
1688 | Log2(("service : %d\n", service));
|
---|
1689 | return htons(service);
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | void slirp_set_dhcp_TFTP_prefix(PNATState pData, const char *tftpPrefix)
|
---|
1693 | {
|
---|
1694 | Log2(("tftp_prefix:%s\n", tftpPrefix));
|
---|
1695 | tftp_prefix = tftpPrefix;
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | void slirp_set_dhcp_TFTP_bootfile(PNATState pData, const char *bootFile)
|
---|
1699 | {
|
---|
1700 | Log2(("bootFile:%s\n", bootFile));
|
---|
1701 | bootp_filename = bootFile;
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | void slirp_set_dhcp_next_server(PNATState pData, const char *next_server)
|
---|
1705 | {
|
---|
1706 | Log2(("next_server:%s\n", next_server));
|
---|
1707 | if (next_server == NULL)
|
---|
1708 | pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
|
---|
1709 | else
|
---|
1710 | inet_aton(next_server, &pData->tftp_server);
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | int slirp_set_binding_address(PNATState pData, char *addr)
|
---|
1714 | {
|
---|
1715 | if (addr == NULL || (inet_aton(addr, &pData->bindIP) == 0))
|
---|
1716 | {
|
---|
1717 | pData->bindIP.s_addr = INADDR_ANY;
|
---|
1718 | return 1;
|
---|
1719 | }
|
---|
1720 | return 0;
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | void slirp_set_dhcp_dns_proxy(PNATState pData, bool fDNSProxy)
|
---|
1724 | {
|
---|
1725 | Log2(("NAT: DNS proxy switched %s\n", (fDNSProxy ? "on" : "off")));
|
---|
1726 | pData->use_dns_proxy = fDNSProxy;
|
---|
1727 | }
|
---|
1728 |
|
---|
1729 | #define CHECK_ARG(name, val, lim_min, lim_max) \
|
---|
1730 | do { \
|
---|
1731 | if ((val) < (lim_min) || (val) > (lim_max)) \
|
---|
1732 | { \
|
---|
1733 | LogRel(("NAT: (" #name ":%d) has been ignored, " \
|
---|
1734 | "because out of range (%d, %d)\n", (val), (lim_min), (lim_max))); \
|
---|
1735 | return; \
|
---|
1736 | } \
|
---|
1737 | else \
|
---|
1738 | { \
|
---|
1739 | LogRel(("NAT: (" #name ":%d)\n", (val))); \
|
---|
1740 | } \
|
---|
1741 | } while (0)
|
---|
1742 |
|
---|
1743 | /* don't allow user set less 8kB and more than 1M values */
|
---|
1744 | #define _8K_1M_CHECK_ARG(name, val) CHECK_ARG(name, (val), 8, 1024)
|
---|
1745 | void slirp_set_rcvbuf(PNATState pData, int kilobytes)
|
---|
1746 | {
|
---|
1747 | _8K_1M_CHECK_ARG("SOCKET_RCVBUF", kilobytes);
|
---|
1748 | pData->socket_rcv = kilobytes;
|
---|
1749 | }
|
---|
1750 | void slirp_set_sndbuf(PNATState pData, int kilobytes)
|
---|
1751 | {
|
---|
1752 | _8K_1M_CHECK_ARG("SOCKET_SNDBUF", kilobytes);
|
---|
1753 | pData->socket_snd = kilobytes * _1K;
|
---|
1754 | }
|
---|
1755 | void slirp_set_tcp_rcvspace(PNATState pData, int kilobytes)
|
---|
1756 | {
|
---|
1757 | _8K_1M_CHECK_ARG("TCP_RCVSPACE", kilobytes);
|
---|
1758 | tcp_rcvspace = kilobytes * _1K;
|
---|
1759 | }
|
---|
1760 | void slirp_set_tcp_sndspace(PNATState pData, int kilobytes)
|
---|
1761 | {
|
---|
1762 | _8K_1M_CHECK_ARG("TCP_SNDSPACE", kilobytes);
|
---|
1763 | tcp_sndspace = kilobytes * _1K;
|
---|
1764 | }
|
---|
1765 |
|
---|