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