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 |
|
---|
10 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
11 |
|
---|
12 | # define DO_ENGAGE_EVENT1(so, fdset, label) \
|
---|
13 | do { \
|
---|
14 | FD_SET((so)->s, (fdset)); \
|
---|
15 | UPD_NFDS((so)->s); \
|
---|
16 | } while(0)
|
---|
17 |
|
---|
18 |
|
---|
19 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
20 | do { \
|
---|
21 | FD_SET((so)->s, (fdset1)); \
|
---|
22 | FD_SET((so)->s, (fdset2)); \
|
---|
23 | UPD_NFDS((so)->s); \
|
---|
24 | } while(0)
|
---|
25 |
|
---|
26 | # define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
|
---|
27 |
|
---|
28 | # define DO_CHECK_FD_SET(so, events, fdset) (FD_ISSET((so)->s, (fdset)))
|
---|
29 |
|
---|
30 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
|
---|
31 |
|
---|
32 | # ifndef RT_OS_WINDOWS
|
---|
33 | # define ICMP_ENGAGE_EVENT(so, fdset) \
|
---|
34 | do { \
|
---|
35 | if (pData->icmp_socket.s != -1) \
|
---|
36 | DO_ENGAGE_EVENT1((so), (fdset), ICMP); \
|
---|
37 | } while (0)
|
---|
38 | # else /* !RT_OS_WINDOWS */
|
---|
39 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
40 | #endif /* RT_OS_WINDOWS */
|
---|
41 |
|
---|
42 | #else /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
|
---|
46 | * So no call to WSAEventSelect necessary.
|
---|
47 | */
|
---|
48 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
49 |
|
---|
50 | # define DO_ENGAGE_EVENT1(so, fdset1, label) \
|
---|
51 | do { \
|
---|
52 | rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
|
---|
53 | if (rc == SOCKET_ERROR) \
|
---|
54 | { \
|
---|
55 | /* This should not happen */ \
|
---|
56 | error = WSAGetLastError(); \
|
---|
57 | LogRel(("WSAEventSelector (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
|
---|
58 | error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
|
---|
59 | } \
|
---|
60 | } while(0); \
|
---|
61 | CONTINUE(label)
|
---|
62 |
|
---|
63 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
64 | DO_ENGAGE_EVENT1((so), (fdset1), label)
|
---|
65 |
|
---|
66 | # define DO_POLL_EVENTS(rc, error, so, events, label) \
|
---|
67 | (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
|
---|
68 | if ((rc) == SOCKET_ERROR) \
|
---|
69 | { \
|
---|
70 | (error) = WSAGetLastError(); \
|
---|
71 | LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
|
---|
72 | CONTINUE(label); \
|
---|
73 | }
|
---|
74 |
|
---|
75 | # define acceptds_win FD_ACCEPT
|
---|
76 | # define acceptds_win_bit FD_ACCEPT_BIT
|
---|
77 |
|
---|
78 | # define readfds_win FD_READ
|
---|
79 | # define readfds_win_bit FD_READ_BIT
|
---|
80 |
|
---|
81 | # define writefds_win FD_WRITE
|
---|
82 | # define writefds_win_bit FD_WRITE_BIT
|
---|
83 |
|
---|
84 | # define xfds_win FD_OOB
|
---|
85 | # define xfds_win_bit FD_OOB_BIT
|
---|
86 |
|
---|
87 | # define DO_CHECK_FD_SET(so, events, fdset) \
|
---|
88 | (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
|
---|
89 |
|
---|
90 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
|
---|
91 |
|
---|
92 | #endif /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
|
---|
93 |
|
---|
94 | #define TCP_ENGAGE_EVENT1(so, fdset) \
|
---|
95 | DO_ENGAGE_EVENT1((so), (fdset), tcp)
|
---|
96 |
|
---|
97 | #define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
|
---|
98 | DO_ENGAGE_EVENT2((so), (fdset1), (fdset2), tcp)
|
---|
99 |
|
---|
100 | #define UDP_ENGAGE_EVENT(so, fdset) \
|
---|
101 | DO_ENGAGE_EVENT1((so), (fdset), udp)
|
---|
102 |
|
---|
103 | #define POLL_TCP_EVENTS(rc, error, so, events) \
|
---|
104 | DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
|
---|
105 |
|
---|
106 | #define POLL_UDP_EVENTS(rc, error, so, events) \
|
---|
107 | DO_POLL_EVENTS((rc), (error), (so), (events), udp)
|
---|
108 |
|
---|
109 | #define CHECK_FD_SET(so, events, set) \
|
---|
110 | (DO_CHECK_FD_SET((so), (events), set))
|
---|
111 |
|
---|
112 | #define WIN_CHECK_FD_SET(so, events, set) \
|
---|
113 | (DO_WIN_CHECK_FD_SET((so), (events), set))
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Loging macros
|
---|
117 | */
|
---|
118 | #if VBOX_WITH_DEBUG_NAT_SOCKETS
|
---|
119 | # if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
120 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
121 | do { \
|
---|
122 | LogRel((" " #proto "%R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
|
---|
123 | } while (0)
|
---|
124 | # else
|
---|
125 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
126 | do { \
|
---|
127 | LogRel((" " #proto " %R[natsock] %s %s %s\n", (so), FD_ISSET((so)->s, (r_fdset))?"READ":"",\
|
---|
128 | FD_ISSET((so)->s, (w_fdset))?"WRITE":"", FD_ISSET((so)->s, (x_fdset))?"OOB":"")); \
|
---|
129 | } while (0)
|
---|
130 | # endif /* VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
131 | #else
|
---|
132 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
|
---|
133 | #endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
134 |
|
---|
135 | #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))
|
---|
136 |
|
---|
137 | static const uint8_t special_ethaddr[6] =
|
---|
138 | {
|
---|
139 | 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
|
---|
140 | };
|
---|
141 |
|
---|
142 | #ifdef RT_OS_WINDOWS
|
---|
143 |
|
---|
144 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
145 | struct in_addr *pdns_addr,
|
---|
146 | const char **ppszDomain)
|
---|
147 | {
|
---|
148 | int rc = 0;
|
---|
149 | FIXED_INFO *FixedInfo = NULL;
|
---|
150 | ULONG BufLen;
|
---|
151 | DWORD ret;
|
---|
152 | IP_ADDR_STRING *pIPAddr;
|
---|
153 | struct in_addr tmp_addr;
|
---|
154 |
|
---|
155 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
|
---|
156 | BufLen = sizeof(FIXED_INFO);
|
---|
157 |
|
---|
158 | /** @todo: this API returns all DNS servers, no matter whether the
|
---|
159 | * corresponding network adapter is disabled or not. Maybe replace
|
---|
160 | * this by GetAdapterAddresses(), which is XP/Vista only though. */
|
---|
161 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen))
|
---|
162 | {
|
---|
163 | if (FixedInfo)
|
---|
164 | {
|
---|
165 | GlobalFree(FixedInfo);
|
---|
166 | FixedInfo = NULL;
|
---|
167 | }
|
---|
168 | FixedInfo = GlobalAlloc(GPTR, BufLen);
|
---|
169 | }
|
---|
170 |
|
---|
171 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS)
|
---|
172 | {
|
---|
173 | Log(("GetNetworkParams failed. ret = %08x\n", (u_int)ret ));
|
---|
174 | if (FixedInfo)
|
---|
175 | {
|
---|
176 | GlobalFree(FixedInfo);
|
---|
177 | FixedInfo = NULL;
|
---|
178 | }
|
---|
179 | rc = -1;
|
---|
180 | goto get_dns_prefix;
|
---|
181 | }
|
---|
182 |
|
---|
183 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
184 | pIPAddr = &(FixedInfo->DnsServerList);
|
---|
185 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
|
---|
186 | Log(("nat: DNS Servers:\n"));
|
---|
187 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
188 | LogRel(("NAT: DNS address: %s\n", pIPAddr->IpAddress.String));
|
---|
189 | *pdns_addr = tmp_addr;
|
---|
190 |
|
---|
191 | pIPAddr = FixedInfo -> DnsServerList.Next;
|
---|
192 | while (pIPAddr)
|
---|
193 | {
|
---|
194 | if (fVerbose)
|
---|
195 | LogRel(("NAT: ignored DNS address: %s\n", pIPAddr ->IpAddress.String));
|
---|
196 | pIPAddr = pIPAddr ->Next;
|
---|
197 | }
|
---|
198 | #else
|
---|
199 | /*localhost mask */
|
---|
200 | for (pIPAddr = &FixedInfo->DnsServerList; pIPAddr != NULL; pIPAddr = pIPAddr->Next)
|
---|
201 | {
|
---|
202 | struct dns_entry *da;
|
---|
203 | if(!inet_aton(pIPAddr->IpAddress.String, &tmp_addr))
|
---|
204 | continue;
|
---|
205 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
206 | if (da == NULL)
|
---|
207 | {
|
---|
208 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
209 | return -1;
|
---|
210 | }
|
---|
211 | /*check */
|
---|
212 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
213 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
214 | }
|
---|
215 | LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
216 | }
|
---|
217 | #endif
|
---|
218 | if (FixedInfo)
|
---|
219 | {
|
---|
220 | GlobalFree(FixedInfo);
|
---|
221 | FixedInfo = NULL;
|
---|
222 | }
|
---|
223 |
|
---|
224 | get_dns_prefix:
|
---|
225 | if (ppszDomain)
|
---|
226 | {
|
---|
227 | OSVERSIONINFO ver;
|
---|
228 | char szDnsDomain[256];
|
---|
229 | DWORD dwSize = sizeof(szDnsDomain);
|
---|
230 |
|
---|
231 | *ppszDomain = NULL;
|
---|
232 | GetVersionEx(&ver);
|
---|
233 | if (ver.dwMajorVersion >= 5)
|
---|
234 | {
|
---|
235 | /* GetComputerNameEx exists in Windows versions starting with 2000. */
|
---|
236 | if (GetComputerNameEx(ComputerNameDnsDomain, szDnsDomain, &dwSize))
|
---|
237 | {
|
---|
238 | if (szDnsDomain[0])
|
---|
239 | {
|
---|
240 | /* Just non-empty strings are valid. */
|
---|
241 | *ppszDomain = RTStrDup(szDnsDomain);
|
---|
242 | if (pData->fPassDomain)
|
---|
243 | {
|
---|
244 | if (fVerbose)
|
---|
245 | LogRel(("NAT: passing domain name %s\n", szDnsDomain));
|
---|
246 | }
|
---|
247 | else
|
---|
248 | Log(("nat: ignoring domain %s\n", szDnsDomain));
|
---|
249 | }
|
---|
250 | }
|
---|
251 | else
|
---|
252 | Log(("nat: GetComputerNameEx failed (%d)\n", GetLastError()));
|
---|
253 | }
|
---|
254 | }
|
---|
255 | return rc;
|
---|
256 | }
|
---|
257 |
|
---|
258 | #else
|
---|
259 |
|
---|
260 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
261 | struct in_addr *pdns_addr,
|
---|
262 | const char **ppszDomain)
|
---|
263 | {
|
---|
264 | char buff[512];
|
---|
265 | char buff2[256];
|
---|
266 | FILE *f;
|
---|
267 | int found = 0;
|
---|
268 | struct in_addr tmp_addr;
|
---|
269 |
|
---|
270 | #ifdef RT_OS_OS2
|
---|
271 | /* Try various locations. */
|
---|
272 | char *etc = getenv("ETC");
|
---|
273 | f = NULL;
|
---|
274 | if (etc)
|
---|
275 | {
|
---|
276 | snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
|
---|
277 | f = fopen(buff, "rt");
|
---|
278 | }
|
---|
279 | if (!f)
|
---|
280 | {
|
---|
281 | snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
|
---|
282 | f = fopen(buff, "rt");
|
---|
283 | }
|
---|
284 | if (!f)
|
---|
285 | {
|
---|
286 | snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
|
---|
287 | f = fopen(buff, "rt");
|
---|
288 | }
|
---|
289 | #else
|
---|
290 | f = fopen("/etc/resolv.conf", "r");
|
---|
291 | #endif
|
---|
292 | if (!f)
|
---|
293 | return -1;
|
---|
294 |
|
---|
295 | if (ppszDomain)
|
---|
296 | *ppszDomain = NULL;
|
---|
297 | Log(("nat: DNS Servers:\n"));
|
---|
298 | while (fgets(buff, 512, f) != NULL)
|
---|
299 | {
|
---|
300 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
301 | struct dns_entry *da = NULL;
|
---|
302 | #endif
|
---|
303 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
|
---|
304 | {
|
---|
305 | if (!inet_aton(buff2, &tmp_addr))
|
---|
306 | continue;
|
---|
307 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
308 | /* If it's the first one, set it to dns_addr */
|
---|
309 | if (!found)
|
---|
310 | {
|
---|
311 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
312 | LogRel(("NAT: DNS address: %s\n", buff2));
|
---|
313 | *pdns_addr = tmp_addr;
|
---|
314 | }
|
---|
315 | else
|
---|
316 | {
|
---|
317 | if (fVerbose)
|
---|
318 | LogRel(("NAT: ignored DNS address: %s\n", buff2));
|
---|
319 | }
|
---|
320 | #else
|
---|
321 | /*localhost mask */
|
---|
322 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
323 | if (da == NULL)
|
---|
324 | {
|
---|
325 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
326 | return -1;
|
---|
327 | }
|
---|
328 | /*check */
|
---|
329 | da->de_addr.s_addr = tmp_addr.s_addr;
|
---|
330 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
331 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
332 | }
|
---|
333 | LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
334 | #endif
|
---|
335 | found++;
|
---|
336 | }
|
---|
337 | if ( ppszDomain
|
---|
338 | && (!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
|
---|
339 | {
|
---|
340 | /* Domain name/search list present. Pick first entry */
|
---|
341 | if (*ppszDomain == NULL)
|
---|
342 | {
|
---|
343 | char *tok;
|
---|
344 | char *saveptr;
|
---|
345 | tok = strtok_r(&buff[6], " \t\n", &saveptr);
|
---|
346 | if (tok)
|
---|
347 | {
|
---|
348 | *ppszDomain = RTStrDup(tok);
|
---|
349 | if (pData->fPassDomain)
|
---|
350 | {
|
---|
351 | if (fVerbose)
|
---|
352 | LogRel(("NAT: passing domain name %s\n", tok));
|
---|
353 | }
|
---|
354 | else
|
---|
355 | Log(("nat: ignoring domain %s\n", tok));
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|
359 | }
|
---|
360 | fclose(f);
|
---|
361 | if (!found)
|
---|
362 | return -1;
|
---|
363 | return 0;
|
---|
364 | }
|
---|
365 |
|
---|
366 | #endif
|
---|
367 |
|
---|
368 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
|
---|
369 | {
|
---|
370 | return get_dns_addr_domain(pData, false, pdns_addr, NULL);
|
---|
371 | }
|
---|
372 |
|
---|
373 | int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
|
---|
374 | bool fPassDomain, const char *pszTFTPPrefix,
|
---|
375 | const char *pszBootFile, void *pvUser)
|
---|
376 | {
|
---|
377 | int fNATfailed = 0;
|
---|
378 | int rc;
|
---|
379 | PNATState pData = RTMemAlloc(sizeof(NATState));
|
---|
380 | *ppData = pData;
|
---|
381 | if (!pData)
|
---|
382 | return VERR_NO_MEMORY;
|
---|
383 | if (u32Netmask & 0x1f)
|
---|
384 | /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
|
---|
385 | return VERR_INVALID_PARAMETER;
|
---|
386 | memset(pData, '\0', sizeof(NATState));
|
---|
387 | pData->fPassDomain = fPassDomain;
|
---|
388 | pData->pvUser = pvUser;
|
---|
389 | tftp_prefix = pszTFTPPrefix;
|
---|
390 | bootp_filename = pszBootFile;
|
---|
391 | pData->netmask = u32Netmask;
|
---|
392 |
|
---|
393 | #ifdef RT_OS_WINDOWS
|
---|
394 | {
|
---|
395 | WSADATA Data;
|
---|
396 | WSAStartup(MAKEWORD(2,0), &Data);
|
---|
397 | }
|
---|
398 | # if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
399 | pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
400 | # endif
|
---|
401 | #endif
|
---|
402 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
403 | QSOCKET_LOCK_CREATE(tcb);
|
---|
404 | QSOCKET_LOCK_CREATE(udb);
|
---|
405 | rc = RTReqCreateQueue(&pData->pReqQueue);
|
---|
406 | AssertReleaseRC(rc);
|
---|
407 | #endif
|
---|
408 |
|
---|
409 | link_up = 1;
|
---|
410 |
|
---|
411 | debug_init();
|
---|
412 | if_init(pData);
|
---|
413 | ip_init(pData);
|
---|
414 | icmp_init(pData);
|
---|
415 |
|
---|
416 | /* Initialise mbufs *after* setting the MTU */
|
---|
417 | m_init(pData);
|
---|
418 |
|
---|
419 | inet_aton(pszNetAddr, &special_addr);
|
---|
420 | alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
|
---|
421 | /* @todo: add ability to configure this staff */
|
---|
422 | pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
|
---|
423 |
|
---|
424 | /* set default addresses */
|
---|
425 | inet_aton("127.0.0.1", &loopback_addr);
|
---|
426 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
427 | inet_aton("127.0.0.1", &dns_addr);
|
---|
428 |
|
---|
429 | if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
|
---|
430 | #else
|
---|
431 | LIST_INIT(&pData->dns_list_head);
|
---|
432 | if (get_dns_addr_domain(pData, true, NULL, &pData->pszDomain) < 0)
|
---|
433 | #endif
|
---|
434 | fNATfailed = 1;
|
---|
435 |
|
---|
436 | getouraddr(pData);
|
---|
437 | return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Statistics counters.
|
---|
442 | */
|
---|
443 | void slirp_register_timers(PNATState pData, PPDMDRVINS pDrvIns)
|
---|
444 | {
|
---|
445 | #ifdef VBOX_WITH_STATISTICS
|
---|
446 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFill, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
447 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp fills", "/Drivers/NAT%d/Fill", pDrvIns->iInstance);
|
---|
448 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPoll, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
449 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp polls", "/Drivers/NAT%d/Poll", pDrvIns->iInstance);
|
---|
450 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFastTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
451 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp fast timer", "/Drivers/NAT%d/TimerFast", pDrvIns->iInstance);
|
---|
452 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatSlowTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
453 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp slow timer", "/Drivers/NAT%d/TimerSlow", pDrvIns->iInstance);
|
---|
454 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
455 | STAMUNIT_COUNT, "TCP sockets", "/Drivers/NAT%d/SockTCP", pDrvIns->iInstance);
|
---|
456 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
457 | STAMUNIT_COUNT, "TCP sockets active", "/Drivers/NAT%d/SockTCPHot", pDrvIns->iInstance);
|
---|
458 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
459 | STAMUNIT_COUNT, "UDP sockets", "/Drivers/NAT%d/SockUDP", pDrvIns->iInstance);
|
---|
460 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
461 | STAMUNIT_COUNT, "UDP sockets active", "/Drivers/NAT%d/SockUDPHot", pDrvIns->iInstance);
|
---|
462 | #endif /* VBOX_WITH_STATISTICS */
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Marks the link as up, making it possible to establish new connections.
|
---|
467 | */
|
---|
468 | void slirp_link_up(PNATState pData)
|
---|
469 | {
|
---|
470 | link_up = 1;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Marks the link as down and cleans up the current connections.
|
---|
475 | */
|
---|
476 | void slirp_link_down(PNATState pData)
|
---|
477 | {
|
---|
478 | struct socket *so;
|
---|
479 |
|
---|
480 | while ((so = tcb.so_next) != &tcb)
|
---|
481 | {
|
---|
482 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
483 | sofree(pData, so);
|
---|
484 | else
|
---|
485 | tcp_drop(pData, sototcpcb(so), 0);
|
---|
486 | }
|
---|
487 |
|
---|
488 | while ((so = udb.so_next) != &udb)
|
---|
489 | udp_detach(pData, so);
|
---|
490 |
|
---|
491 | link_up = 0;
|
---|
492 | }
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * Terminates the slirp component.
|
---|
496 | */
|
---|
497 | void slirp_term(PNATState pData)
|
---|
498 | {
|
---|
499 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
500 | struct dns_entry *de = NULL;
|
---|
501 | #endif
|
---|
502 | if (pData->pszDomain)
|
---|
503 | RTStrFree((char *)(void *)pData->pszDomain);
|
---|
504 |
|
---|
505 | #ifdef RT_OS_WINDOWS
|
---|
506 | pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
|
---|
507 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
508 | RTMemFree(pData->pvIcmpBuffer);
|
---|
509 | # else
|
---|
510 | closesocket(pData->icmp_socket.s);
|
---|
511 | #endif
|
---|
512 |
|
---|
513 | slirp_link_down(pData);
|
---|
514 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
515 | while(!LIST_EMPTY(&pData->dns_list_head)) {
|
---|
516 | de = LIST_FIRST(&pData->dns_list_head);
|
---|
517 | LIST_REMOVE(de, de_list);
|
---|
518 | RTMemFree(de);
|
---|
519 | }
|
---|
520 | #endif
|
---|
521 | #ifdef RT_OS_WINDOWS
|
---|
522 | WSACleanup();
|
---|
523 | #endif
|
---|
524 | #ifdef LOG_ENABLED
|
---|
525 | Log(("\n"
|
---|
526 | "NAT statistics\n"
|
---|
527 | "--------------\n"
|
---|
528 | "\n"));
|
---|
529 | ipstats(pData);
|
---|
530 | tcpstats(pData);
|
---|
531 | udpstats(pData);
|
---|
532 | icmpstats(pData);
|
---|
533 | mbufstats(pData);
|
---|
534 | sockstats(pData);
|
---|
535 | Log(("\n"
|
---|
536 | "\n"
|
---|
537 | "\n"));
|
---|
538 | #endif
|
---|
539 | RTMemFree(pData);
|
---|
540 | }
|
---|
541 |
|
---|
542 |
|
---|
543 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
544 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
545 | #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
|
---|
546 |
|
---|
547 | /*
|
---|
548 | * curtime kept to an accuracy of 1ms
|
---|
549 | */
|
---|
550 | #ifdef RT_OS_WINDOWS
|
---|
551 | static void updtime(PNATState pData)
|
---|
552 | {
|
---|
553 | struct _timeb tb;
|
---|
554 |
|
---|
555 | _ftime(&tb);
|
---|
556 | curtime = (u_int)tb.time * (u_int)1000;
|
---|
557 | curtime += (u_int)tb.millitm;
|
---|
558 | }
|
---|
559 | #else
|
---|
560 | static void updtime(PNATState pData)
|
---|
561 | {
|
---|
562 | gettimeofday(&tt, 0);
|
---|
563 |
|
---|
564 | curtime = (u_int)tt.tv_sec * (u_int)1000;
|
---|
565 | curtime += (u_int)tt.tv_usec / (u_int)1000;
|
---|
566 |
|
---|
567 | if ((tt.tv_usec % 1000) >= 500)
|
---|
568 | curtime++;
|
---|
569 | }
|
---|
570 | #endif
|
---|
571 |
|
---|
572 | void slirp_select_fill(PNATState pData, int *pnfds,
|
---|
573 | fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
574 | {
|
---|
575 | struct socket *so, *so_next;
|
---|
576 | int nfds;
|
---|
577 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
578 | int rc;
|
---|
579 | int error;
|
---|
580 | #endif
|
---|
581 | int i;
|
---|
582 |
|
---|
583 | STAM_PROFILE_START(&pData->StatFill, a);
|
---|
584 |
|
---|
585 | nfds = *pnfds;
|
---|
586 |
|
---|
587 | /*
|
---|
588 | * First, TCP sockets
|
---|
589 | */
|
---|
590 | do_slowtimo = 0;
|
---|
591 | if (link_up)
|
---|
592 | {
|
---|
593 | /*
|
---|
594 | * *_slowtimo needs calling if there are IP fragments
|
---|
595 | * in the fragment queue, or there are TCP connections active
|
---|
596 | */
|
---|
597 | /* XXX:
|
---|
598 | * triggering of fragment expiration should be the same but use new macroses
|
---|
599 | */
|
---|
600 | do_slowtimo = (tcb.so_next != &tcb);
|
---|
601 | if (!do_slowtimo)
|
---|
602 | {
|
---|
603 | for (i = 0; i < IPREASS_NHASH; i++)
|
---|
604 | {
|
---|
605 | if (!TAILQ_EMPTY(&ipq[i]))
|
---|
606 | {
|
---|
607 | do_slowtimo = 1;
|
---|
608 | break;
|
---|
609 | }
|
---|
610 | }
|
---|
611 | }
|
---|
612 | ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
|
---|
613 |
|
---|
614 | STAM_COUNTER_RESET(&pData->StatTCP);
|
---|
615 | STAM_COUNTER_RESET(&pData->StatTCPHot);
|
---|
616 |
|
---|
617 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
618 | /* { */
|
---|
619 | STAM_COUNTER_INC(&pData->StatTCP);
|
---|
620 |
|
---|
621 | /*
|
---|
622 | * See if we need a tcp_fasttimo
|
---|
623 | */
|
---|
624 | if ( time_fasttimo == 0
|
---|
625 | && so->so_tcpcb != NULL
|
---|
626 | && so->so_tcpcb->t_flags & TF_DELACK)
|
---|
627 | time_fasttimo = curtime; /* Flag when we want a fasttimo */
|
---|
628 |
|
---|
629 | /*
|
---|
630 | * NOFDREF can include still connecting to local-host,
|
---|
631 | * newly socreated() sockets etc. Don't want to select these.
|
---|
632 | */
|
---|
633 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
634 | CONTINUE(tcp);
|
---|
635 |
|
---|
636 | /*
|
---|
637 | * Set for reading sockets which are accepting
|
---|
638 | */
|
---|
639 | if (so->so_state & SS_FACCEPTCONN)
|
---|
640 | {
|
---|
641 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
642 | TCP_ENGAGE_EVENT1(so, readfds);
|
---|
643 | CONTINUE(tcp);
|
---|
644 | }
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Set for writing sockets which are connecting
|
---|
648 | */
|
---|
649 | if (so->so_state & SS_ISFCONNECTING)
|
---|
650 | {
|
---|
651 | Log2(("connecting %R[natsock] engaged\n",so));
|
---|
652 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
653 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
654 | }
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * Set for writing if we are connected, can send more, and
|
---|
658 | * we have something to send
|
---|
659 | */
|
---|
660 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
|
---|
661 | {
|
---|
662 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
663 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
664 | }
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * Set for reading (and urgent data) if we are connected, can
|
---|
668 | * receive more, and we have room for it XXX /2 ?
|
---|
669 | */
|
---|
670 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
|
---|
671 | {
|
---|
672 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
673 | TCP_ENGAGE_EVENT2(so, readfds, xfds);
|
---|
674 | }
|
---|
675 | LOOP_LABEL(tcp, so, so_next);
|
---|
676 | }
|
---|
677 |
|
---|
678 | /*
|
---|
679 | * UDP sockets
|
---|
680 | */
|
---|
681 | STAM_COUNTER_RESET(&pData->StatUDP);
|
---|
682 | STAM_COUNTER_RESET(&pData->StatUDPHot);
|
---|
683 |
|
---|
684 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
685 | /* { */
|
---|
686 |
|
---|
687 | STAM_COUNTER_INC(&pData->StatUDP);
|
---|
688 |
|
---|
689 | /*
|
---|
690 | * See if it's timed out
|
---|
691 | */
|
---|
692 | if (so->so_expire)
|
---|
693 | {
|
---|
694 | if (so->so_expire <= curtime)
|
---|
695 | {
|
---|
696 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
697 | /* we need so_next for continue our cycle*/
|
---|
698 | so_next = so->so_next;
|
---|
699 | #endif
|
---|
700 | UDP_DETACH(pData, so, so_next);
|
---|
701 | CONTINUE_NO_UNLOCK(udp);
|
---|
702 | }
|
---|
703 | else
|
---|
704 | do_slowtimo = 1; /* Let socket expire */
|
---|
705 | }
|
---|
706 |
|
---|
707 | /*
|
---|
708 | * When UDP packets are received from over the link, they're
|
---|
709 | * sendto()'d straight away, so no need for setting for writing
|
---|
710 | * Limit the number of packets queued by this session to 4.
|
---|
711 | * Note that even though we try and limit this to 4 packets,
|
---|
712 | * the session could have more queued if the packets needed
|
---|
713 | * to be fragmented.
|
---|
714 | *
|
---|
715 | * (XXX <= 4 ?)
|
---|
716 | */
|
---|
717 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
|
---|
718 | {
|
---|
719 | STAM_COUNTER_INC(&pData->StatUDPHot);
|
---|
720 | UDP_ENGAGE_EVENT(so, readfds);
|
---|
721 | }
|
---|
722 | LOOP_LABEL(udp, so, so_next);
|
---|
723 | }
|
---|
724 |
|
---|
725 | }
|
---|
726 |
|
---|
727 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
728 | *pnfds = nfds;
|
---|
729 | #else
|
---|
730 | *pnfds = VBOX_EVENT_COUNT;
|
---|
731 | #endif
|
---|
732 |
|
---|
733 | STAM_PROFILE_STOP(&pData->StatFill, a);
|
---|
734 | }
|
---|
735 |
|
---|
736 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
737 | void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
|
---|
738 | #else
|
---|
739 | void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
740 | #endif
|
---|
741 | {
|
---|
742 | struct socket *so, *so_next;
|
---|
743 | int ret;
|
---|
744 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
745 | WSANETWORKEVENTS NetworkEvents;
|
---|
746 | int rc;
|
---|
747 | int error;
|
---|
748 | #endif
|
---|
749 |
|
---|
750 | STAM_PROFILE_START(&pData->StatPoll, a);
|
---|
751 |
|
---|
752 | /* Update time */
|
---|
753 | updtime(pData);
|
---|
754 |
|
---|
755 | /*
|
---|
756 | * See if anything has timed out
|
---|
757 | */
|
---|
758 | if (link_up)
|
---|
759 | {
|
---|
760 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
|
---|
761 | {
|
---|
762 | STAM_PROFILE_START(&pData->StatFastTimer, a);
|
---|
763 | tcp_fasttimo(pData);
|
---|
764 | time_fasttimo = 0;
|
---|
765 | STAM_PROFILE_STOP(&pData->StatFastTimer, a);
|
---|
766 | }
|
---|
767 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
|
---|
768 | {
|
---|
769 | STAM_PROFILE_START(&pData->StatSlowTimer, a);
|
---|
770 | ip_slowtimo(pData);
|
---|
771 | tcp_slowtimo(pData);
|
---|
772 | last_slowtimo = curtime;
|
---|
773 | STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
|
---|
774 | }
|
---|
775 | }
|
---|
776 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
777 | if (fTimeout)
|
---|
778 | return; /* only timer update */
|
---|
779 | #endif
|
---|
780 |
|
---|
781 | /*
|
---|
782 | * Check sockets
|
---|
783 | */
|
---|
784 | if (link_up)
|
---|
785 | {
|
---|
786 | #if defined(RT_OS_WINDOWS)
|
---|
787 | /*XXX: before renaming please make see define
|
---|
788 | * fIcmp in slirp_state.h
|
---|
789 | */
|
---|
790 | if (fIcmp)
|
---|
791 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
792 | #else
|
---|
793 | if (pData->icmp_socket.s != -1 && FD_ISSET(pData->icmp_socket.s, readfds))
|
---|
794 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
795 | #endif
|
---|
796 | /*
|
---|
797 | * Check TCP sockets
|
---|
798 | */
|
---|
799 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
800 | /* { */
|
---|
801 |
|
---|
802 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
803 | if ( so->so_state & SS_NOFDREF
|
---|
804 | && so->so_deleted == 1)
|
---|
805 | {
|
---|
806 | struct socket *son, *sop = NULL;
|
---|
807 | QSOCKET_LOCK(tcb);
|
---|
808 | if (so->so_next != NULL)
|
---|
809 | {
|
---|
810 | if (so->so_next != &tcb)
|
---|
811 | SOCKET_LOCK(so->so_next);
|
---|
812 | son = so->so_next;
|
---|
813 | }
|
---|
814 | if ( so->so_prev != &tcb
|
---|
815 | && so->so_prev != NULL)
|
---|
816 | {
|
---|
817 | SOCKET_LOCK(so->so_prev);
|
---|
818 | sop = so->so_prev;
|
---|
819 | }
|
---|
820 | QSOCKET_UNLOCK(tcb);
|
---|
821 | remque(pData, so);
|
---|
822 | SOCKET_UNLOCK(so);
|
---|
823 | SOCKET_LOCK_DESTROY(so);
|
---|
824 | RTMemFree(so);
|
---|
825 | so_next = son;
|
---|
826 | if (sop != NULL)
|
---|
827 | SOCKET_UNLOCK(sop);
|
---|
828 | CONTINUE_NO_UNLOCK(tcp);
|
---|
829 | }
|
---|
830 | #endif
|
---|
831 | /*
|
---|
832 | * FD_ISSET is meaningless on these sockets
|
---|
833 | * (and they can crash the program)
|
---|
834 | */
|
---|
835 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
836 | CONTINUE(tcp);
|
---|
837 |
|
---|
838 | POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
839 |
|
---|
840 | LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
|
---|
841 |
|
---|
842 | /*
|
---|
843 | * Check for URG data
|
---|
844 | * This will soread as well, so no need to
|
---|
845 | * test for readfds below if this succeeds
|
---|
846 | */
|
---|
847 |
|
---|
848 | /* out-of-band data */
|
---|
849 | if (CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
850 | {
|
---|
851 | sorecvoob(pData, so);
|
---|
852 | }
|
---|
853 |
|
---|
854 | /*
|
---|
855 | * Check sockets for reading
|
---|
856 | */
|
---|
857 | else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
858 | || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
|
---|
859 | {
|
---|
860 | /*
|
---|
861 | * Check for incoming connections
|
---|
862 | */
|
---|
863 | if (so->so_state & SS_FACCEPTCONN)
|
---|
864 | {
|
---|
865 | TCP_CONNECT(pData, so);
|
---|
866 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
867 | if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
|
---|
868 | #endif
|
---|
869 | CONTINUE(tcp);
|
---|
870 | }
|
---|
871 |
|
---|
872 | SOREAD(ret, pData, so, /*fCloseIfNothingRead=*/false);
|
---|
873 | /* Output it if we read something */
|
---|
874 | if (ret > 0)
|
---|
875 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
876 | }
|
---|
877 |
|
---|
878 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
879 | /*
|
---|
880 | * Check for FD_CLOSE events.
|
---|
881 | */
|
---|
882 | if (NetworkEvents.lNetworkEvents & FD_CLOSE)
|
---|
883 | {
|
---|
884 | /*
|
---|
885 | * drain the socket
|
---|
886 | */
|
---|
887 | for (;;)
|
---|
888 | {
|
---|
889 | SOREAD(ret, pData, so, /*fCloseIfNothingRead=*/true);
|
---|
890 | if (ret > 0)
|
---|
891 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
892 | else
|
---|
893 | break;
|
---|
894 | }
|
---|
895 | }
|
---|
896 | #endif
|
---|
897 |
|
---|
898 | /*
|
---|
899 | * Check sockets for writing
|
---|
900 | */
|
---|
901 | if (CHECK_FD_SET(so, NetworkEvents, writefds))
|
---|
902 | {
|
---|
903 | /*
|
---|
904 | * Check for non-blocking, still-connecting sockets
|
---|
905 | */
|
---|
906 | if (so->so_state & SS_ISFCONNECTING)
|
---|
907 | {
|
---|
908 | Log2(("connecting %R[natsock] catched\n", so));
|
---|
909 | /* Connected */
|
---|
910 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
911 |
|
---|
912 | /*
|
---|
913 | * This should be probably guarded by PROBE_CONN too. Anyway,
|
---|
914 | * we disable it on OS/2 because the below send call returns
|
---|
915 | * EFAULT which causes the opened TCP socket to close right
|
---|
916 | * after it has been opened and connected.
|
---|
917 | */
|
---|
918 | #ifndef RT_OS_OS2
|
---|
919 | ret = send(so->s, (const char *)&ret, 0, 0);
|
---|
920 | if (ret < 0)
|
---|
921 | {
|
---|
922 | /* XXXXX Must fix, zero bytes is a NOP */
|
---|
923 | if ( errno == EAGAIN
|
---|
924 | || errno == EWOULDBLOCK
|
---|
925 | || errno == EINPROGRESS
|
---|
926 | || errno == ENOTCONN)
|
---|
927 | CONTINUE(tcp);
|
---|
928 |
|
---|
929 | /* else failed */
|
---|
930 | so->so_state = SS_NOFDREF;
|
---|
931 | }
|
---|
932 | /* else so->so_state &= ~SS_ISFCONNECTING; */
|
---|
933 | #endif
|
---|
934 |
|
---|
935 | /*
|
---|
936 | * Continue tcp_input
|
---|
937 | */
|
---|
938 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
939 | /* continue; */
|
---|
940 | }
|
---|
941 | else
|
---|
942 | SOWRITE(ret, pData, so);
|
---|
943 | /*
|
---|
944 | * XXX If we wrote something (a lot), there could be the need
|
---|
945 | * for a window update. In the worst case, the remote will send
|
---|
946 | * a window probe to get things going again.
|
---|
947 | */
|
---|
948 | }
|
---|
949 |
|
---|
950 | /*
|
---|
951 | * Probe a still-connecting, non-blocking socket
|
---|
952 | * to check if it's still alive
|
---|
953 | */
|
---|
954 | #ifdef PROBE_CONN
|
---|
955 | if (so->so_state & SS_ISFCONNECTING)
|
---|
956 | {
|
---|
957 | ret = recv(so->s, (char *)&ret, 0, 0);
|
---|
958 |
|
---|
959 | if (ret < 0)
|
---|
960 | {
|
---|
961 | /* XXX */
|
---|
962 | if ( errno == EAGAIN
|
---|
963 | || errno == EWOULDBLOCK
|
---|
964 | || errno == EINPROGRESS
|
---|
965 | || errno == ENOTCONN)
|
---|
966 | {
|
---|
967 | CONTINUE(tcp); /* Still connecting, continue */
|
---|
968 | }
|
---|
969 |
|
---|
970 | /* else failed */
|
---|
971 | so->so_state = SS_NOFDREF;
|
---|
972 |
|
---|
973 | /* tcp_input will take care of it */
|
---|
974 | }
|
---|
975 | else
|
---|
976 | {
|
---|
977 | ret = send(so->s, &ret, 0, 0);
|
---|
978 | if (ret < 0)
|
---|
979 | {
|
---|
980 | /* XXX */
|
---|
981 | if ( errno == EAGAIN
|
---|
982 | || errno == EWOULDBLOCK
|
---|
983 | || errno == EINPROGRESS
|
---|
984 | || errno == ENOTCONN)
|
---|
985 | {
|
---|
986 | CONTINUE(tcp);
|
---|
987 | }
|
---|
988 | /* else failed */
|
---|
989 | so->so_state = SS_NOFDREF;
|
---|
990 | }
|
---|
991 | else
|
---|
992 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
993 |
|
---|
994 | }
|
---|
995 | TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
|
---|
996 | } /* SS_ISFCONNECTING */
|
---|
997 | #endif
|
---|
998 | LOOP_LABEL(tcp, so, so_next);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | /*
|
---|
1002 | * Now UDP sockets.
|
---|
1003 | * Incoming packets are sent straight away, they're not buffered.
|
---|
1004 | * Incoming UDP data isn't buffered either.
|
---|
1005 | */
|
---|
1006 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
1007 | /* { */
|
---|
1008 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1009 | if ( so->so_state & SS_NOFDREF
|
---|
1010 | && so->so_deleted == 1)
|
---|
1011 | {
|
---|
1012 | struct socket *son, *sop = NULL;
|
---|
1013 | QSOCKET_LOCK(udb);
|
---|
1014 | if (so->so_next != NULL)
|
---|
1015 | {
|
---|
1016 | if (so->so_next != &udb)
|
---|
1017 | SOCKET_LOCK(so->so_next);
|
---|
1018 | son = so->so_next;
|
---|
1019 | }
|
---|
1020 | if ( so->so_prev != &udb
|
---|
1021 | && so->so_prev != NULL)
|
---|
1022 | {
|
---|
1023 | SOCKET_LOCK(so->so_prev);
|
---|
1024 | sop = so->so_prev;
|
---|
1025 | }
|
---|
1026 | QSOCKET_UNLOCK(udb);
|
---|
1027 | remque(pData, so);
|
---|
1028 | SOCKET_UNLOCK(so);
|
---|
1029 | SOCKET_LOCK_DESTROY(so);
|
---|
1030 | RTMemFree(so);
|
---|
1031 | so_next = son;
|
---|
1032 | if (sop != NULL)
|
---|
1033 | SOCKET_UNLOCK(sop);
|
---|
1034 | CONTINUE_NO_UNLOCK(udp);
|
---|
1035 | }
|
---|
1036 | #endif
|
---|
1037 | POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
1038 |
|
---|
1039 | LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
|
---|
1040 |
|
---|
1041 | if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1042 | {
|
---|
1043 | SORECVFROM(pData, so);
|
---|
1044 | }
|
---|
1045 | LOOP_LABEL(udp, so, so_next);
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
1051 | /*
|
---|
1052 | * See if we can start outputting
|
---|
1053 | */
|
---|
1054 | if (if_queued && link_up)
|
---|
1055 | if_start(pData);
|
---|
1056 | #endif
|
---|
1057 |
|
---|
1058 | STAM_PROFILE_STOP(&pData->StatPoll, a);
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | #define ETH_ALEN 6
|
---|
1062 | #define ETH_HLEN 14
|
---|
1063 |
|
---|
1064 | #define ARPOP_REQUEST 1 /* ARP request */
|
---|
1065 | #define ARPOP_REPLY 2 /* ARP reply */
|
---|
1066 |
|
---|
1067 | struct ethhdr
|
---|
1068 | {
|
---|
1069 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
1070 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
1071 | unsigned short h_proto; /* packet type ID field */
|
---|
1072 | };
|
---|
1073 |
|
---|
1074 | struct arphdr
|
---|
1075 | {
|
---|
1076 | unsigned short ar_hrd; /* format of hardware address */
|
---|
1077 | unsigned short ar_pro; /* format of protocol address */
|
---|
1078 | unsigned char ar_hln; /* length of hardware address */
|
---|
1079 | unsigned char ar_pln; /* length of protocol address */
|
---|
1080 | unsigned short ar_op; /* ARP opcode (command) */
|
---|
1081 |
|
---|
1082 | /*
|
---|
1083 | * Ethernet looks like this : This bit is variable sized however...
|
---|
1084 | */
|
---|
1085 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
|
---|
1086 | unsigned char ar_sip[4]; /* sender IP address */
|
---|
1087 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
|
---|
1088 | unsigned char ar_tip[4]; /* target IP address */
|
---|
1089 | };
|
---|
1090 |
|
---|
1091 | static
|
---|
1092 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1093 | void arp_input(PNATState pData, struct mbuf *m)
|
---|
1094 | #else
|
---|
1095 | void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1096 | #endif
|
---|
1097 | {
|
---|
1098 | struct ethhdr *eh;
|
---|
1099 | struct ethhdr *reh;
|
---|
1100 | struct arphdr *ah;
|
---|
1101 | struct arphdr *rah;
|
---|
1102 | int ar_op;
|
---|
1103 | struct ex_list *ex_ptr;
|
---|
1104 | uint32_t htip;
|
---|
1105 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1106 | uint8_t arp_reply[sizeof(struct arphdr) + ETH_HLEN];
|
---|
1107 | eh = (struct ethhdr *)pkt;
|
---|
1108 | #else
|
---|
1109 | struct mbuf *mr;
|
---|
1110 | eh = mtod(m, struct ethhdr *);
|
---|
1111 | #endif
|
---|
1112 | ah = (struct arphdr *)&eh[1];
|
---|
1113 | htip = ntohl(*(uint32_t*)ah->ar_tip);
|
---|
1114 |
|
---|
1115 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1116 | mr = m_get(pData);
|
---|
1117 | mr->m_data += if_maxlinkhdr;
|
---|
1118 | mr->m_len = sizeof(struct arphdr);
|
---|
1119 | rah = mtod(mr, struct arphdr *);
|
---|
1120 | #else
|
---|
1121 | reh = (struct ethhdr *)arp_reply;
|
---|
1122 | rah = (struct arphdr *)&reh[1];
|
---|
1123 | #endif
|
---|
1124 |
|
---|
1125 | ar_op = ntohs(ah->ar_op);
|
---|
1126 | switch(ar_op)
|
---|
1127 | {
|
---|
1128 | case ARPOP_REQUEST:
|
---|
1129 | if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
|
---|
1130 | {
|
---|
1131 | if ( CTL_CHECK(htip,CTL_DNS)
|
---|
1132 | || CTL_CHECK(htip, CTL_ALIAS)
|
---|
1133 | || CTL_CHECK(htip, CTL_TFTP))
|
---|
1134 | goto arp_ok;
|
---|
1135 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
|
---|
1136 | {
|
---|
1137 | if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
|
---|
1138 | goto arp_ok;
|
---|
1139 | }
|
---|
1140 | return;
|
---|
1141 | arp_ok:
|
---|
1142 |
|
---|
1143 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1144 | memcpy(reh->h_dest, eh->h_source, ETH_ALEN);
|
---|
1145 | memcpy(reh->h_source, &special_addr, ETH_ALEN);
|
---|
1146 | reh->h_source[5] = ah->ar_tip[3];
|
---|
1147 | reh->h_proto = htons(ETH_P_ARP);
|
---|
1148 | #endif
|
---|
1149 | rah->ar_hrd = htons(1);
|
---|
1150 | rah->ar_pro = htons(ETH_P_IP);
|
---|
1151 | rah->ar_hln = ETH_ALEN;
|
---|
1152 | rah->ar_pln = 4;
|
---|
1153 | rah->ar_op = htons(ARPOP_REPLY);
|
---|
1154 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
|
---|
1155 |
|
---|
1156 | switch (htip & ~pData->netmask)
|
---|
1157 | {
|
---|
1158 | case CTL_DNS:
|
---|
1159 | case CTL_ALIAS:
|
---|
1160 | rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
|
---|
1161 | break;
|
---|
1162 | default:;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | memcpy(rah->ar_sip, ah->ar_tip, 4);
|
---|
1166 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
|
---|
1167 | memcpy(rah->ar_tip, ah->ar_sip, 4);
|
---|
1168 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1169 | if_encap(pData, ETH_P_ARP, mr);
|
---|
1170 | m_free(pData, m);
|
---|
1171 | #else
|
---|
1172 | slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
|
---|
1173 | #endif
|
---|
1174 | }
|
---|
1175 | break;
|
---|
1176 | default:
|
---|
1177 | break;
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1182 | {
|
---|
1183 | struct mbuf *m;
|
---|
1184 | int proto;
|
---|
1185 | static bool fWarnedIpv6;
|
---|
1186 |
|
---|
1187 | if (pkt_len < ETH_HLEN)
|
---|
1188 | {
|
---|
1189 | LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
|
---|
1190 | return;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | m = m_get(pData);
|
---|
1194 | if (!m)
|
---|
1195 | {
|
---|
1196 | LogRel(("can't allocate new mbuf\n"));
|
---|
1197 | return;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | /* Note: we add to align the IP header */
|
---|
1201 |
|
---|
1202 | if (M_FREEROOM(m) < pkt_len)
|
---|
1203 | m_inc(m, pkt_len);
|
---|
1204 |
|
---|
1205 | m->m_len = pkt_len ;
|
---|
1206 | memcpy(m->m_data, pkt, pkt_len);
|
---|
1207 |
|
---|
1208 | proto = ntohs(*(uint16_t *)(pkt + 12));
|
---|
1209 | switch(proto)
|
---|
1210 | {
|
---|
1211 | case ETH_P_ARP:
|
---|
1212 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1213 | arp_input(pData, m);
|
---|
1214 | #else
|
---|
1215 | arp_input(pData, pkt, pkt_len);
|
---|
1216 | m_free(pData, m);
|
---|
1217 | #endif
|
---|
1218 | break;
|
---|
1219 | case ETH_P_IP:
|
---|
1220 | /* Update time. Important if the network is very quiet, as otherwise
|
---|
1221 | * the first outgoing connection gets an incorrect timestamp. */
|
---|
1222 | updtime(pData);
|
---|
1223 | m->m_data += ETH_HLEN;
|
---|
1224 | m->m_len -= ETH_HLEN;
|
---|
1225 | ip_input(pData, m);
|
---|
1226 | break;
|
---|
1227 | case ETH_P_IPV6:
|
---|
1228 | m_free(pData, m);
|
---|
1229 | if (!fWarnedIpv6)
|
---|
1230 | {
|
---|
1231 | LogRel(("NAT: IPv6 not supported\n"));
|
---|
1232 | fWarnedIpv6 = true;
|
---|
1233 | }
|
---|
1234 | break;
|
---|
1235 | default:
|
---|
1236 | LogRel(("NAT: Unsupported protocol %x\n", proto));
|
---|
1237 | m_free(pData, m);
|
---|
1238 | break;
|
---|
1239 | }
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | /* output the IP packet to the ethernet device */
|
---|
1243 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1244 | void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
|
---|
1245 | #else
|
---|
1246 | void if_encap(PNATState pData, uint8_t *ip_data, int ip_data_len)
|
---|
1247 | #endif
|
---|
1248 | {
|
---|
1249 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1250 | struct ethhdr *eh;
|
---|
1251 | uint8_t *buf = RTMemAlloc(1600);
|
---|
1252 | m->m_data -= if_maxlinkhdr;
|
---|
1253 | m->m_len += ETH_HLEN;
|
---|
1254 | eh = mtod(m, struct ethhdr *);
|
---|
1255 | #else
|
---|
1256 | uint8_t buf[1600];
|
---|
1257 | struct ethhdr *eh = (struct ethhdr *)buf;
|
---|
1258 |
|
---|
1259 | if (ip_data_len + ETH_HLEN > sizeof(buf))
|
---|
1260 | return;
|
---|
1261 |
|
---|
1262 | memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
|
---|
1263 | #endif
|
---|
1264 |
|
---|
1265 |
|
---|
1266 | memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
|
---|
1267 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
1268 | /* XXX: not correct */
|
---|
1269 | eh->h_source[5] = CTL_ALIAS;
|
---|
1270 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1271 | eh->h_proto = htons(eth_proto);
|
---|
1272 | #if 0
|
---|
1273 | slirp_output(pData->pvUser, m, mtod(m, uint8_t *), m->m_len);
|
---|
1274 | #else
|
---|
1275 | memcpy(buf, mtod(m, uint8_t *), m->m_len);
|
---|
1276 | slirp_output(pData->pvUser, NULL, buf, m->m_len);
|
---|
1277 | m_free(pData, m);
|
---|
1278 | #endif
|
---|
1279 | #else
|
---|
1280 | eh->h_proto = htons(ETH_P_IP);
|
---|
1281 | slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
|
---|
1282 | #endif
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | int slirp_redir(PNATState pData, int is_udp, int host_port,
|
---|
1286 | struct in_addr guest_addr, int guest_port)
|
---|
1287 | {
|
---|
1288 | if (is_udp)
|
---|
1289 | {
|
---|
1290 | if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
|
---|
1291 | htons(guest_port), 0))
|
---|
1292 | return -1;
|
---|
1293 | }
|
---|
1294 | else
|
---|
1295 | {
|
---|
1296 | if (!solisten(pData, htons(host_port), guest_addr.s_addr,
|
---|
1297 | htons(guest_port), 0))
|
---|
1298 | return -1;
|
---|
1299 | }
|
---|
1300 | return 0;
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
|
---|
1304 | int guest_port)
|
---|
1305 | {
|
---|
1306 | return add_exec(&exec_list, do_pty, (char *)args,
|
---|
1307 | addr_low_byte, htons(guest_port));
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
|
---|
1311 | {
|
---|
1312 | memcpy(client_ethaddr, ethaddr, ETH_ALEN);
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
1316 | HANDLE *slirp_get_events(PNATState pData)
|
---|
1317 | {
|
---|
1318 | return pData->phEvents;
|
---|
1319 | }
|
---|
1320 | void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
|
---|
1321 | {
|
---|
1322 | pData->phEvents[index] = hEvent;
|
---|
1323 | }
|
---|
1324 | #endif
|
---|
1325 |
|
---|
1326 | unsigned int slirp_get_timeout_ms(PNATState pData)
|
---|
1327 | {
|
---|
1328 | if (link_up)
|
---|
1329 | {
|
---|
1330 | if (time_fasttimo)
|
---|
1331 | return 2;
|
---|
1332 | if (do_slowtimo)
|
---|
1333 | return 500; /* see PR_SLOWHZ */
|
---|
1334 | }
|
---|
1335 | return 0;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /*
|
---|
1339 | * this function called from NAT thread
|
---|
1340 | */
|
---|
1341 | void slirp_post_sent(PNATState pData, void *pvArg)
|
---|
1342 | {
|
---|
1343 | struct socket *so = 0;
|
---|
1344 | struct tcpcb *tp = 0;
|
---|
1345 | struct mbuf *m = (struct mbuf *)pvArg;
|
---|
1346 | m_free(pData, m);
|
---|
1347 | }
|
---|
1348 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1349 | void slirp_process_queue(PNATState pData)
|
---|
1350 | {
|
---|
1351 | RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
|
---|
1352 | }
|
---|
1353 | void *slirp_get_queue(PNATState pData)
|
---|
1354 | {
|
---|
1355 | return pData->pReqQueue;
|
---|
1356 | }
|
---|
1357 | #endif
|
---|