1 | #include "slirp.h"
|
---|
2 | #ifdef RT_OS_OS2
|
---|
3 | # include <paths.h>
|
---|
4 | #endif
|
---|
5 |
|
---|
6 | #include <VBox/err.h>
|
---|
7 | #include <iprt/assert.h>
|
---|
8 |
|
---|
9 | static const uint8_t special_ethaddr[6] = {
|
---|
10 | 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
|
---|
11 | };
|
---|
12 |
|
---|
13 | #ifdef _WIN32
|
---|
14 |
|
---|
15 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
16 | struct in_addr *pdns_addr,
|
---|
17 | const char **ppszDomain)
|
---|
18 | {
|
---|
19 | int rc = 0;
|
---|
20 | FIXED_INFO *FixedInfo=NULL;
|
---|
21 | ULONG BufLen;
|
---|
22 | DWORD ret;
|
---|
23 | IP_ADDR_STRING *pIPAddr;
|
---|
24 | struct in_addr tmp_addr;
|
---|
25 |
|
---|
26 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
|
---|
27 | BufLen = sizeof(FIXED_INFO);
|
---|
28 |
|
---|
29 | /** @todo: this API returns all DNS servers, no matter whether the
|
---|
30 | * corresponding network adapter is disabled or not. Maybe replace
|
---|
31 | * this by GetAdapterAddresses(), which is XP/Vista only though. */
|
---|
32 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
|
---|
33 | if (FixedInfo) {
|
---|
34 | GlobalFree(FixedInfo);
|
---|
35 | FixedInfo = NULL;
|
---|
36 | }
|
---|
37 | FixedInfo = GlobalAlloc(GPTR, BufLen);
|
---|
38 | }
|
---|
39 |
|
---|
40 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
|
---|
41 | Log(("GetNetworkParams failed. ret = %08x\n", (u_int)ret ));
|
---|
42 | if (FixedInfo) {
|
---|
43 | GlobalFree(FixedInfo);
|
---|
44 | FixedInfo = NULL;
|
---|
45 | }
|
---|
46 | rc = -1;
|
---|
47 | goto get_dns_prefix;
|
---|
48 | }
|
---|
49 |
|
---|
50 | pIPAddr = &(FixedInfo->DnsServerList);
|
---|
51 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
|
---|
52 | Log(("nat: DNS Servers:\n"));
|
---|
53 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
54 | LogRel(("NAT: DNS address: %s\n", pIPAddr->IpAddress.String));
|
---|
55 | *pdns_addr = tmp_addr;
|
---|
56 |
|
---|
57 | pIPAddr = FixedInfo -> DnsServerList.Next;
|
---|
58 | while ( pIPAddr )
|
---|
59 | {
|
---|
60 | if (fVerbose)
|
---|
61 | LogRel(("NAT: ignored DNS address: %s\n", pIPAddr ->IpAddress.String));
|
---|
62 | pIPAddr = pIPAddr ->Next;
|
---|
63 | }
|
---|
64 | if (FixedInfo) {
|
---|
65 | GlobalFree(FixedInfo);
|
---|
66 | FixedInfo = NULL;
|
---|
67 | }
|
---|
68 |
|
---|
69 | get_dns_prefix:
|
---|
70 | if (ppszDomain)
|
---|
71 | {
|
---|
72 | OSVERSIONINFO ver;
|
---|
73 | char szDnsDomain[256];
|
---|
74 | DWORD dwSize = sizeof(szDnsDomain);
|
---|
75 |
|
---|
76 | *ppszDomain = NULL;
|
---|
77 | GetVersionEx(&ver);
|
---|
78 | if (ver.dwMajorVersion >= 5)
|
---|
79 | {
|
---|
80 | /* GetComputerNameEx exists in Windows versions starting with 2000. */
|
---|
81 | if (GetComputerNameEx(ComputerNameDnsDomain, szDnsDomain, &dwSize))
|
---|
82 | {
|
---|
83 | if (szDnsDomain[0])
|
---|
84 | {
|
---|
85 | /* Just non-empty strings are valid. */
|
---|
86 | *ppszDomain = RTStrDup(szDnsDomain);
|
---|
87 | if (pData->fPassDomain)
|
---|
88 | {
|
---|
89 | if (fVerbose)
|
---|
90 | LogRel(("NAT: passing domain name %s\n", szDnsDomain));
|
---|
91 | }
|
---|
92 | else
|
---|
93 | Log(("nat: ignoring domain %s\n", szDnsDomain));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | else
|
---|
97 | Log(("nat: GetComputerNameEx failed (%d)\n", GetLastError()));
|
---|
98 | }
|
---|
99 | }
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 |
|
---|
103 | #else
|
---|
104 |
|
---|
105 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
106 | struct in_addr *pdns_addr,
|
---|
107 | const char **ppszDomain)
|
---|
108 | {
|
---|
109 | char buff[512];
|
---|
110 | char buff2[256];
|
---|
111 | FILE *f;
|
---|
112 | int found = 0;
|
---|
113 | struct in_addr tmp_addr;
|
---|
114 |
|
---|
115 | #ifdef RT_OS_OS2
|
---|
116 | /* Try various locations. */
|
---|
117 | char *etc = getenv("ETC");
|
---|
118 | f = NULL;
|
---|
119 | if (etc)
|
---|
120 | {
|
---|
121 | snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
|
---|
122 | f = fopen(buff, "rt");
|
---|
123 | }
|
---|
124 | if (!f) {
|
---|
125 | snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
|
---|
126 | f = fopen(buff, "rt");
|
---|
127 | }
|
---|
128 | if (!f) {
|
---|
129 | snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
|
---|
130 | f = fopen(buff, "rt");
|
---|
131 | }
|
---|
132 | #else
|
---|
133 | f = fopen("/etc/resolv.conf", "r");
|
---|
134 | #endif
|
---|
135 | if (!f)
|
---|
136 | return -1;
|
---|
137 |
|
---|
138 | if (ppszDomain)
|
---|
139 | *ppszDomain = NULL;
|
---|
140 | Log(("nat: DNS Servers:\n"));
|
---|
141 | while (fgets(buff, 512, f) != NULL) {
|
---|
142 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
|
---|
143 | if (!inet_aton(buff2, &tmp_addr))
|
---|
144 | continue;
|
---|
145 | if (tmp_addr.s_addr == loopback_addr.s_addr)
|
---|
146 | tmp_addr = our_addr;
|
---|
147 | /* If it's the first one, set it to dns_addr */
|
---|
148 | if (!found)
|
---|
149 | {
|
---|
150 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
151 | LogRel(("NAT: DNS address: %s\n", buff2));
|
---|
152 | *pdns_addr = tmp_addr;
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | if (fVerbose)
|
---|
157 | LogRel(("NAT: ignored DNS address: %s\n", buff2));
|
---|
158 | }
|
---|
159 | found++;
|
---|
160 | }
|
---|
161 | if ( ppszDomain
|
---|
162 | && (!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
|
---|
163 | {
|
---|
164 | /* Domain name/search list present. Pick first entry */
|
---|
165 | if (*ppszDomain == NULL)
|
---|
166 | {
|
---|
167 | char *tok;
|
---|
168 | char *saveptr;
|
---|
169 | tok = strtok_r(&buff[6], " \t\n", &saveptr);
|
---|
170 | if (tok)
|
---|
171 | {
|
---|
172 | *ppszDomain = RTStrDup(tok);
|
---|
173 | if (pData->fPassDomain)
|
---|
174 | {
|
---|
175 | if (fVerbose)
|
---|
176 | LogRel(("NAT: passing domain name %s\n", tok));
|
---|
177 | }
|
---|
178 | else
|
---|
179 | Log(("nat: ignoring domain %s\n", tok));
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | fclose(f);
|
---|
185 | if (!found)
|
---|
186 | return -1;
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
|
---|
193 | {
|
---|
194 | return get_dns_addr_domain(pData, false, pdns_addr, NULL);
|
---|
195 | }
|
---|
196 |
|
---|
197 | int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
|
---|
198 | bool fPassDomain, const char *pszTFTPPrefix,
|
---|
199 | const char *pszBootFile, void *pvUser)
|
---|
200 | {
|
---|
201 | int fNATfailed = 0;
|
---|
202 | PNATState pData = malloc(sizeof(NATState));
|
---|
203 | *ppData = pData;
|
---|
204 | if (!pData)
|
---|
205 | return VERR_NO_MEMORY;
|
---|
206 | if (u32Netmask & 0x1f)
|
---|
207 | /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
|
---|
208 | return VERR_INVALID_PARAMETER;
|
---|
209 | memset(pData, '\0', sizeof(NATState));
|
---|
210 | pData->fPassDomain = fPassDomain;
|
---|
211 | pData->pvUser = pvUser;
|
---|
212 | #if ARCH_BITS == 64
|
---|
213 | pData->cpvHashUsed = 1;
|
---|
214 | #endif
|
---|
215 | tftp_prefix = pszTFTPPrefix;
|
---|
216 | bootp_filename = pszBootFile;
|
---|
217 | pData->netmask = u32Netmask;
|
---|
218 |
|
---|
219 | #ifdef _WIN32
|
---|
220 | {
|
---|
221 | WSADATA Data;
|
---|
222 | WSAStartup(MAKEWORD(2,0), &Data);
|
---|
223 | }
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | Assert(sizeof(struct ip) == 20);
|
---|
227 | link_up = 1;
|
---|
228 |
|
---|
229 | if_init(pData);
|
---|
230 | ip_init(pData);
|
---|
231 |
|
---|
232 | /* Initialise mbufs *after* setting the MTU */
|
---|
233 | m_init(pData);
|
---|
234 |
|
---|
235 | /* set default addresses */
|
---|
236 | inet_aton("127.0.0.1", &loopback_addr);
|
---|
237 | inet_aton("127.0.0.1", &dns_addr);
|
---|
238 |
|
---|
239 | if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
|
---|
240 | fNATfailed = 1;
|
---|
241 |
|
---|
242 | inet_aton(pszNetAddr, &special_addr);
|
---|
243 | alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
|
---|
244 | getouraddr(pData);
|
---|
245 | return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Marks the link as up, making it possible to establish new connections.
|
---|
250 | */
|
---|
251 | void slirp_link_up(PNATState pData)
|
---|
252 | {
|
---|
253 | link_up = 1;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Marks the link as down and cleans up the current connections.
|
---|
258 | */
|
---|
259 | void slirp_link_down(PNATState pData)
|
---|
260 | {
|
---|
261 | struct socket *so;
|
---|
262 |
|
---|
263 | while ((so = tcb.so_next) != &tcb)
|
---|
264 | {
|
---|
265 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
266 | sofree(pData, so);
|
---|
267 | else
|
---|
268 | tcp_drop(pData, sototcpcb(so), 0);
|
---|
269 | }
|
---|
270 |
|
---|
271 | while ((so = udb.so_next) != &udb)
|
---|
272 | udp_detach(pData, so);
|
---|
273 |
|
---|
274 | link_up = 0;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Terminates the slirp component.
|
---|
279 | */
|
---|
280 | void slirp_term(PNATState pData)
|
---|
281 | {
|
---|
282 | if (pData->pszDomain)
|
---|
283 | RTStrFree((char *)(void *)pData->pszDomain);
|
---|
284 |
|
---|
285 | #if ARCH_BITS == 64
|
---|
286 | LogRel(("NAT: cpvHashUsed=%RU32 cpvHashCollisions=%RU32 cpvHashInserts=%RU64 cpvHashDone=%RU64\n",
|
---|
287 | pData->cpvHashUsed, pData->cpvHashCollisions, pData->cpvHashInserts, pData->cpvHashDone));
|
---|
288 | #endif
|
---|
289 |
|
---|
290 | slirp_link_down(pData);
|
---|
291 | #ifdef WIN32
|
---|
292 | WSACleanup();
|
---|
293 | #endif
|
---|
294 | #ifdef LOG_ENABLED
|
---|
295 | Log(("\n"
|
---|
296 | "NAT statistics\n"
|
---|
297 | "--------------\n"
|
---|
298 | "\n"));
|
---|
299 | ipstats(pData);
|
---|
300 | tcpstats(pData);
|
---|
301 | udpstats(pData);
|
---|
302 | icmpstats(pData);
|
---|
303 | mbufstats(pData);
|
---|
304 | sockstats(pData);
|
---|
305 | Log(("\n"
|
---|
306 | "\n"
|
---|
307 | "\n"));
|
---|
308 | #endif
|
---|
309 | free(pData);
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
314 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
315 | #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * curtime kept to an accuracy of 1ms
|
---|
319 | */
|
---|
320 | #ifdef _WIN32
|
---|
321 | static void updtime(PNATState pData)
|
---|
322 | {
|
---|
323 | struct _timeb tb;
|
---|
324 |
|
---|
325 | _ftime(&tb);
|
---|
326 | curtime = (u_int)tb.time * (u_int)1000;
|
---|
327 | curtime += (u_int)tb.millitm;
|
---|
328 | }
|
---|
329 | #else
|
---|
330 | static void updtime(PNATState pData)
|
---|
331 | {
|
---|
332 | gettimeofday(&tt, 0);
|
---|
333 |
|
---|
334 | curtime = (u_int)tt.tv_sec * (u_int)1000;
|
---|
335 | curtime += (u_int)tt.tv_usec / (u_int)1000;
|
---|
336 |
|
---|
337 | if ((tt.tv_usec % 1000) >= 500)
|
---|
338 | curtime++;
|
---|
339 | }
|
---|
340 | #endif
|
---|
341 |
|
---|
342 | void slirp_select_fill(PNATState pData, int *pnfds,
|
---|
343 | fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
344 | {
|
---|
345 | struct socket *so, *so_next;
|
---|
346 | struct timeval timeout;
|
---|
347 | int nfds;
|
---|
348 | int tmp_time;
|
---|
349 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
350 | int rc;
|
---|
351 | /* Number of valid entries.
|
---|
352 | * 1st event for drvNATSend() */
|
---|
353 | int cElements;
|
---|
354 | int cEvents = 1;
|
---|
355 | #endif
|
---|
356 |
|
---|
357 | nfds = *pnfds;
|
---|
358 | /*
|
---|
359 | * First, TCP sockets
|
---|
360 | */
|
---|
361 | do_slowtimo = 0;
|
---|
362 | if (link_up) {
|
---|
363 | /*
|
---|
364 | * *_slowtimo needs calling if there are IP fragments
|
---|
365 | * in the fragment queue, or there are TCP connections active
|
---|
366 | */
|
---|
367 | do_slowtimo = ((tcb.so_next != &tcb) ||
|
---|
368 | ((struct ipasfrag *)&ipq != u32_to_ptr(pData, ipq.next, struct ipasfrag *)));
|
---|
369 |
|
---|
370 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
371 | /*
|
---|
372 | * Make this array static with a fixed maximum
|
---|
373 | * 1st event for drvNATSend()
|
---|
374 | */
|
---|
375 | cElements = 1;
|
---|
376 | for (so = tcb.so_next; so != &tcb; so = so_next, cElements++)
|
---|
377 | so_next = so->so_next;
|
---|
378 | for (so = udb.so_next; so != &udb; so = so_next, cElements++)
|
---|
379 | so_next = so->so_next;
|
---|
380 | if (pData->phEvents != NULL)
|
---|
381 | free(pData->phEvents);
|
---|
382 | pData->phEvents = malloc(sizeof(HANDLE) * cElements);
|
---|
383 | #endif
|
---|
384 |
|
---|
385 | for (so = tcb.so_next; so != &tcb; so = so_next) {
|
---|
386 | so_next = so->so_next;
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * See if we need a tcp_fasttimo
|
---|
390 | */
|
---|
391 | if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
|
---|
392 | time_fasttimo = curtime; /* Flag when we want a fasttimo */
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * NOFDREF can include still connecting to local-host,
|
---|
396 | * newly socreated() sockets etc. Don't want to select these.
|
---|
397 | */
|
---|
398 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
399 | continue;
|
---|
400 |
|
---|
401 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
402 | WSAResetEvent(so->hNetworkEvent);
|
---|
403 | #endif
|
---|
404 | /*
|
---|
405 | * Set for reading sockets which are accepting
|
---|
406 | */
|
---|
407 | if (so->so_state & SS_FACCEPTCONN) {
|
---|
408 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
409 | FD_SET(so->s, readfds);
|
---|
410 | UPD_NFDS(so->s);
|
---|
411 | #else
|
---|
412 | rc = WSAEventSelect(so->s, so->hNetworkEvent, FD_READ|FD_WRITE|FD_ACCEPT|FD_CONNECT|FD_OOB);
|
---|
413 | AssertRelease(rc != SOCKET_ERROR);
|
---|
414 | pData->phEvents[cEvents] = so->hNetworkEvent;
|
---|
415 | cEvents++;
|
---|
416 | #endif
|
---|
417 | continue;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /*
|
---|
421 | * Set for writing sockets which are connecting
|
---|
422 | */
|
---|
423 | if (so->so_state & SS_ISFCONNECTING) {
|
---|
424 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
425 | FD_SET(so->s, writefds);
|
---|
426 | UPD_NFDS(so->s);
|
---|
427 | #else
|
---|
428 | rc = WSAEventSelect(so->s, so->hNetworkEvent, FD_READ|FD_WRITE|FD_ACCEPT|FD_CONNECT|FD_OOB);
|
---|
429 | AssertRelease(rc != SOCKET_ERROR);
|
---|
430 | pData->phEvents[cEvents] = so->hNetworkEvent;
|
---|
431 | cEvents++;
|
---|
432 | #endif
|
---|
433 | continue;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Set for writing if we are connected, can send more, and
|
---|
438 | * we have something to send
|
---|
439 | */
|
---|
440 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
|
---|
441 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
442 | FD_SET(so->s, writefds);
|
---|
443 | UPD_NFDS(so->s);
|
---|
444 | #else
|
---|
445 | rc = WSAEventSelect(so->s, so->hNetworkEvent, FD_READ|FD_WRITE|FD_ACCEPT|FD_CONNECT|FD_OOB);
|
---|
446 | AssertRelease(rc != SOCKET_ERROR);
|
---|
447 | pData->phEvents[cEvents] = so->hNetworkEvent;
|
---|
448 | cEvents++;
|
---|
449 | #endif
|
---|
450 | }
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * Set for reading (and urgent data) if we are connected, can
|
---|
454 | * receive more, and we have room for it XXX /2 ?
|
---|
455 | */
|
---|
456 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
|
---|
457 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
458 | FD_SET(so->s, readfds);
|
---|
459 | FD_SET(so->s, xfds);
|
---|
460 | UPD_NFDS(so->s);
|
---|
461 | #else
|
---|
462 | rc = WSAEventSelect(so->s, so->hNetworkEvent, FD_OOB|FD_READ|FD_WRITE|FD_ACCEPT|FD_CONNECT);
|
---|
463 | AssertRelease(rc != SOCKET_ERROR);
|
---|
464 | pData->phEvents[cEvents] = so->hNetworkEvent;
|
---|
465 | cEvents++;
|
---|
466 | #endif
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | /*
|
---|
471 | * UDP sockets
|
---|
472 | */
|
---|
473 | for (so = udb.so_next; so != &udb; so = so_next) {
|
---|
474 | so_next = so->so_next;
|
---|
475 |
|
---|
476 | /*
|
---|
477 | * See if it's timed out
|
---|
478 | */
|
---|
479 | if (so->so_expire) {
|
---|
480 | if (so->so_expire <= curtime) {
|
---|
481 | udp_detach(pData, so);
|
---|
482 | continue;
|
---|
483 | } else
|
---|
484 | do_slowtimo = 1; /* Let socket expire */
|
---|
485 | }
|
---|
486 |
|
---|
487 | /*
|
---|
488 | * When UDP packets are received from over the
|
---|
489 | * link, they're sendto()'d straight away, so
|
---|
490 | * no need for setting for writing
|
---|
491 | * Limit the number of packets queued by this session
|
---|
492 | * to 4. Note that even though we try and limit this
|
---|
493 | * to 4 packets, the session could have more queued
|
---|
494 | * if the packets needed to be fragmented
|
---|
495 | * (XXX <= 4 ?)
|
---|
496 | */
|
---|
497 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
|
---|
498 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
499 | FD_SET(so->s, readfds);
|
---|
500 | UPD_NFDS(so->s);
|
---|
501 | #else
|
---|
502 | WSAResetEvent(so->hNetworkEvent);
|
---|
503 | rc = WSAEventSelect(so->s, so->hNetworkEvent, FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT);
|
---|
504 | AssertRelease(rc != SOCKET_ERROR);
|
---|
505 | pData->phEvents[cEvents] = so->hNetworkEvent;
|
---|
506 | cEvents++;
|
---|
507 | #endif
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * Setup timeout to use minimum CPU usage, especially when idle
|
---|
514 | */
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * First, see the timeout needed by *timo
|
---|
518 | */
|
---|
519 | timeout.tv_sec = 0;
|
---|
520 | timeout.tv_usec = -1;
|
---|
521 | /*
|
---|
522 | * If a slowtimo is needed, set timeout to 500ms from the last
|
---|
523 | * slow timeout. If a fast timeout is needed, set timeout within
|
---|
524 | * 200ms of when it was requested.
|
---|
525 | */
|
---|
526 | if (do_slowtimo) {
|
---|
527 | /* XXX + 10000 because some select()'s aren't that accurate */
|
---|
528 | timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
|
---|
529 | if (timeout.tv_usec < 0)
|
---|
530 | timeout.tv_usec = 0;
|
---|
531 | else if (timeout.tv_usec > 510000)
|
---|
532 | timeout.tv_usec = 510000;
|
---|
533 |
|
---|
534 | /* Can only fasttimo if we also slowtimo */
|
---|
535 | if (time_fasttimo) {
|
---|
536 | tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
|
---|
537 | if (tmp_time < 0)
|
---|
538 | tmp_time = 0;
|
---|
539 |
|
---|
540 | /* Choose the smallest of the 2 */
|
---|
541 | if (tmp_time < timeout.tv_usec)
|
---|
542 | timeout.tv_usec = (u_int)tmp_time;
|
---|
543 | }
|
---|
544 | }
|
---|
545 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
546 | *pnfds = nfds;
|
---|
547 | #else
|
---|
548 | *pnfds = cEvents;
|
---|
549 | #endif
|
---|
550 | }
|
---|
551 |
|
---|
552 | void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
553 | {
|
---|
554 | struct socket *so, *so_next;
|
---|
555 | int ret;
|
---|
556 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
557 | WSANETWORKEVENTS NetworkEvents;
|
---|
558 | int rc;
|
---|
559 | #endif
|
---|
560 |
|
---|
561 | /* Update time */
|
---|
562 | updtime(pData);
|
---|
563 |
|
---|
564 | /*
|
---|
565 | * See if anything has timed out
|
---|
566 | */
|
---|
567 | if (link_up) {
|
---|
568 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
|
---|
569 | tcp_fasttimo(pData);
|
---|
570 | time_fasttimo = 0;
|
---|
571 | }
|
---|
572 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
|
---|
573 | ip_slowtimo(pData);
|
---|
574 | tcp_slowtimo(pData);
|
---|
575 | last_slowtimo = curtime;
|
---|
576 | }
|
---|
577 | }
|
---|
578 |
|
---|
579 | /*
|
---|
580 | * Check sockets
|
---|
581 | */
|
---|
582 | if (link_up) {
|
---|
583 | /*
|
---|
584 | * Check TCP sockets
|
---|
585 | */
|
---|
586 | for (so = tcb.so_next; so != &tcb; so = so_next) {
|
---|
587 | so_next = so->so_next;
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * FD_ISSET is meaningless on these sockets
|
---|
591 | * (and they can crash the program)
|
---|
592 | */
|
---|
593 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
594 | continue;
|
---|
595 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
596 | rc = WSAEnumNetworkEvents(so->s, so->hNetworkEvent, &NetworkEvents);
|
---|
597 | AssertRelease(rc != SOCKET_ERROR);
|
---|
598 | #endif
|
---|
599 |
|
---|
600 | /*
|
---|
601 | * Check for URG data
|
---|
602 | * This will soread as well, so no need to
|
---|
603 | * test for readfds below if this succeeds
|
---|
604 | */
|
---|
605 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
606 | if (FD_ISSET(so->s, xfds))
|
---|
607 | #else
|
---|
608 | if ((NetworkEvents.lNetworkEvents & FD_OOB) && NetworkEvents.iErrorCode[FD_OOB_BIT] == 0)
|
---|
609 | #endif
|
---|
610 | sorecvoob(pData, so);
|
---|
611 | /*
|
---|
612 | * Check sockets for reading
|
---|
613 | */
|
---|
614 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
615 | else if (FD_ISSET(so->s, readfds)) {
|
---|
616 | #else
|
---|
617 | else if ((NetworkEvents.lNetworkEvents & FD_READ) && (NetworkEvents.iErrorCode[FD_READ_BIT] == 0)) {
|
---|
618 | #endif
|
---|
619 | /*
|
---|
620 | * Check for incoming connections
|
---|
621 | */
|
---|
622 | if (so->so_state & SS_FACCEPTCONN) {
|
---|
623 | tcp_connect(pData, so);
|
---|
624 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
625 | WSAResetEvent(so->hNetworkEvent);
|
---|
626 | #endif
|
---|
627 | continue;
|
---|
628 | } /* else */
|
---|
629 | ret = soread(pData, so);
|
---|
630 |
|
---|
631 | /* Output it if we read something */
|
---|
632 | if (ret > 0)
|
---|
633 | tcp_output(pData, sototcpcb(so));
|
---|
634 | }
|
---|
635 |
|
---|
636 | /*
|
---|
637 | * Check sockets for writing
|
---|
638 | */
|
---|
639 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
640 | if (FD_ISSET(so->s, writefds)) {
|
---|
641 | #else
|
---|
642 | if ((NetworkEvents.lNetworkEvents & FD_WRITE) && (NetworkEvents.iErrorCode[FD_WRITE_BIT] == 0)) {
|
---|
643 | #endif
|
---|
644 | /*
|
---|
645 | * Check for non-blocking, still-connecting sockets
|
---|
646 | */
|
---|
647 | if (so->so_state & SS_ISFCONNECTING) {
|
---|
648 | /* Connected */
|
---|
649 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
650 |
|
---|
651 | /*
|
---|
652 | * This should be probably guarded by PROBE_CONN too. Anyway,
|
---|
653 | * we disable it on OS/2 because the below send call returns
|
---|
654 | * EFAULT which causes the opened TCP socket to close right
|
---|
655 | * after it has been opened and connected.
|
---|
656 | */
|
---|
657 | #ifndef RT_OS_OS2
|
---|
658 | ret = send(so->s, (const char *)&ret, 0, 0);
|
---|
659 | if (ret < 0) {
|
---|
660 | /* XXXXX Must fix, zero bytes is a NOP */
|
---|
661 | if (errno == EAGAIN || errno == EWOULDBLOCK ||
|
---|
662 | errno == EINPROGRESS || errno == ENOTCONN) {
|
---|
663 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
664 | WSAResetEvent(so->hNetworkEvent);
|
---|
665 | #endif
|
---|
666 | continue;
|
---|
667 | }
|
---|
668 |
|
---|
669 | /* else failed */
|
---|
670 | so->so_state = SS_NOFDREF;
|
---|
671 | }
|
---|
672 | /* else so->so_state &= ~SS_ISFCONNECTING; */
|
---|
673 | #endif
|
---|
674 |
|
---|
675 | /*
|
---|
676 | * Continue tcp_input
|
---|
677 | */
|
---|
678 | tcp_input(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
679 | /* continue; */
|
---|
680 | } else
|
---|
681 | ret = sowrite(pData, so);
|
---|
682 | /*
|
---|
683 | * XXXXX If we wrote something (a lot), there
|
---|
684 | * could be a need for a window update.
|
---|
685 | * In the worst case, the remote will send
|
---|
686 | * a window probe to get things going again
|
---|
687 | */
|
---|
688 | }
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * Probe a still-connecting, non-blocking socket
|
---|
692 | * to check if it's still alive
|
---|
693 | */
|
---|
694 | #ifdef PROBE_CONN
|
---|
695 | if (so->so_state & SS_ISFCONNECTING) {
|
---|
696 | ret = recv(so->s, (char *)&ret, 0,0);
|
---|
697 |
|
---|
698 | if (ret < 0) {
|
---|
699 | /* XXX */
|
---|
700 | if (errno == EAGAIN || errno == EWOULDBLOCK ||
|
---|
701 | errno == EINPROGRESS || errno == ENOTCONN) {
|
---|
702 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
703 | WSAResetEvent(so->hNetworkEvent);
|
---|
704 | #endif
|
---|
705 | continue; /* Still connecting, continue */
|
---|
706 | }
|
---|
707 |
|
---|
708 | /* else failed */
|
---|
709 | so->so_state = SS_NOFDREF;
|
---|
710 |
|
---|
711 | /* tcp_input will take care of it */
|
---|
712 | } else {
|
---|
713 | ret = send(so->s, &ret, 0,0);
|
---|
714 | if (ret < 0) {
|
---|
715 | /* XXX */
|
---|
716 | if (errno == EAGAIN || errno == EWOULDBLOCK ||
|
---|
717 | errno == EINPROGRESS || errno == ENOTCONN) {
|
---|
718 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
719 | WSAResetEvent(so->hNetworkEvent);
|
---|
720 | #endif
|
---|
721 | continue;
|
---|
722 | }
|
---|
723 | /* else failed */
|
---|
724 | so->so_state = SS_NOFDREF;
|
---|
725 | } else
|
---|
726 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
727 |
|
---|
728 | }
|
---|
729 | tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
|
---|
730 | } /* SS_ISFCONNECTING */
|
---|
731 | #endif
|
---|
732 | }
|
---|
733 |
|
---|
734 | /*
|
---|
735 | * Now UDP sockets.
|
---|
736 | * Incoming packets are sent straight away, they're not buffered.
|
---|
737 | * Incoming UDP data isn't buffered either.
|
---|
738 | */
|
---|
739 | for (so = udb.so_next; so != &udb; so = so_next) {
|
---|
740 | so_next = so->so_next;
|
---|
741 |
|
---|
742 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
743 | rc = WSAEnumNetworkEvents(so->s, so->hNetworkEvent, &NetworkEvents);
|
---|
744 | AssertRelease(rc != SOCKET_ERROR);
|
---|
745 | #endif
|
---|
746 | #if !defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
747 | if (so->s != -1 && FD_ISSET(so->s, readfds)) {
|
---|
748 | #else
|
---|
749 | if ((NetworkEvents.lNetworkEvents & FD_READ) && (NetworkEvents.iErrorCode[FD_READ_BIT] == 0)) {
|
---|
750 | #endif
|
---|
751 | sorecvfrom(pData, so);
|
---|
752 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
753 | WSAResetEvent(so->hNetworkEvent);
|
---|
754 | #endif
|
---|
755 | }
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 | /*
|
---|
760 | * See if we can start outputting
|
---|
761 | */
|
---|
762 | if (if_queued && link_up)
|
---|
763 | if_start(pData);
|
---|
764 | }
|
---|
765 |
|
---|
766 | #define ETH_ALEN 6
|
---|
767 | #define ETH_HLEN 14
|
---|
768 |
|
---|
769 | #define ETH_P_IP 0x0800 /* Internet Protocol packet */
|
---|
770 | #define ETH_P_ARP 0x0806 /* Address Resolution packet */
|
---|
771 |
|
---|
772 | #define ARPOP_REQUEST 1 /* ARP request */
|
---|
773 | #define ARPOP_REPLY 2 /* ARP reply */
|
---|
774 |
|
---|
775 | struct ethhdr
|
---|
776 | {
|
---|
777 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
778 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
779 | unsigned short h_proto; /* packet type ID field */
|
---|
780 | };
|
---|
781 |
|
---|
782 | struct arphdr
|
---|
783 | {
|
---|
784 | unsigned short ar_hrd; /* format of hardware address */
|
---|
785 | unsigned short ar_pro; /* format of protocol address */
|
---|
786 | unsigned char ar_hln; /* length of hardware address */
|
---|
787 | unsigned char ar_pln; /* length of protocol address */
|
---|
788 | unsigned short ar_op; /* ARP opcode (command) */
|
---|
789 |
|
---|
790 | /*
|
---|
791 | * Ethernet looks like this : This bit is variable sized however...
|
---|
792 | */
|
---|
793 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
|
---|
794 | unsigned char ar_sip[4]; /* sender IP address */
|
---|
795 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
|
---|
796 | unsigned char ar_tip[4]; /* target IP address */
|
---|
797 | };
|
---|
798 |
|
---|
799 | static
|
---|
800 | void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
801 | {
|
---|
802 | struct ethhdr *eh = (struct ethhdr *)pkt;
|
---|
803 | struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
|
---|
804 | uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
|
---|
805 | struct ethhdr *reh = (struct ethhdr *)arp_reply;
|
---|
806 | struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
|
---|
807 | int ar_op;
|
---|
808 | struct ex_list *ex_ptr;
|
---|
809 | uint32_t htip = ntohl(*(uint32_t*)ah->ar_tip);
|
---|
810 |
|
---|
811 | ar_op = ntohs(ah->ar_op);
|
---|
812 | switch(ar_op) {
|
---|
813 | case ARPOP_REQUEST:
|
---|
814 | if ((htip & pData->netmask) == ntohl(special_addr.s_addr)) {
|
---|
815 | if ( (htip & ~pData->netmask) == CTL_DNS
|
---|
816 | || (htip & ~pData->netmask) == CTL_ALIAS)
|
---|
817 | goto arp_ok;
|
---|
818 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
|
---|
819 | if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
|
---|
820 | goto arp_ok;
|
---|
821 | }
|
---|
822 | return;
|
---|
823 | arp_ok:
|
---|
824 | /* XXX: make an ARP request to have the client address */
|
---|
825 | memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
|
---|
826 |
|
---|
827 | /* ARP request for alias/dns mac address */
|
---|
828 | memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
|
---|
829 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
830 | reh->h_source[5] = ah->ar_tip[3];
|
---|
831 | reh->h_proto = htons(ETH_P_ARP);
|
---|
832 |
|
---|
833 | rah->ar_hrd = htons(1);
|
---|
834 | rah->ar_pro = htons(ETH_P_IP);
|
---|
835 | rah->ar_hln = ETH_ALEN;
|
---|
836 | rah->ar_pln = 4;
|
---|
837 | rah->ar_op = htons(ARPOP_REPLY);
|
---|
838 | memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
|
---|
839 | memcpy(rah->ar_sip, ah->ar_tip, 4);
|
---|
840 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
|
---|
841 | memcpy(rah->ar_tip, ah->ar_sip, 4);
|
---|
842 | slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
|
---|
843 | }
|
---|
844 | break;
|
---|
845 | default:
|
---|
846 | break;
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
851 | {
|
---|
852 | struct mbuf *m;
|
---|
853 | int proto;
|
---|
854 |
|
---|
855 | if (pkt_len < ETH_HLEN)
|
---|
856 | return;
|
---|
857 |
|
---|
858 | proto = ntohs(*(uint16_t *)(pkt + 12));
|
---|
859 | switch(proto) {
|
---|
860 | case ETH_P_ARP:
|
---|
861 | arp_input(pData, pkt, pkt_len);
|
---|
862 | break;
|
---|
863 | case ETH_P_IP:
|
---|
864 | /* Update time. Important if the network is very quiet, as otherwise
|
---|
865 | * the first outgoing connection gets an incorrect timestamp. */
|
---|
866 | updtime(pData);
|
---|
867 |
|
---|
868 | m = m_get(pData);
|
---|
869 | if (!m)
|
---|
870 | return;
|
---|
871 | /* Note: we add to align the IP header */
|
---|
872 | if (M_FREEROOM(m) < pkt_len + 2) {
|
---|
873 | m_inc(m, pkt_len + 2);
|
---|
874 | }
|
---|
875 | m->m_len = pkt_len + 2;
|
---|
876 | memcpy(m->m_data + 2, pkt, pkt_len);
|
---|
877 |
|
---|
878 | m->m_data += 2 + ETH_HLEN;
|
---|
879 | m->m_len -= 2 + ETH_HLEN;
|
---|
880 |
|
---|
881 | ip_input(pData, m);
|
---|
882 | break;
|
---|
883 | default:
|
---|
884 | break;
|
---|
885 | }
|
---|
886 | }
|
---|
887 |
|
---|
888 | /* output the IP packet to the ethernet device */
|
---|
889 | void if_encap(PNATState pData, const uint8_t *ip_data, int ip_data_len)
|
---|
890 | {
|
---|
891 | uint8_t buf[1600];
|
---|
892 | struct ethhdr *eh = (struct ethhdr *)buf;
|
---|
893 |
|
---|
894 | if (ip_data_len + ETH_HLEN > sizeof(buf))
|
---|
895 | return;
|
---|
896 |
|
---|
897 | memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
|
---|
898 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
899 | /* XXX: not correct */
|
---|
900 | eh->h_source[5] = CTL_ALIAS;
|
---|
901 | eh->h_proto = htons(ETH_P_IP);
|
---|
902 | memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
|
---|
903 | slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
|
---|
904 | }
|
---|
905 |
|
---|
906 | int slirp_redir(PNATState pData, int is_udp, int host_port,
|
---|
907 | struct in_addr guest_addr, int guest_port)
|
---|
908 | {
|
---|
909 | if (is_udp) {
|
---|
910 | if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
|
---|
911 | htons(guest_port), 0))
|
---|
912 | return -1;
|
---|
913 | } else {
|
---|
914 | if (!solisten(pData, htons(host_port), guest_addr.s_addr,
|
---|
915 | htons(guest_port), 0))
|
---|
916 | return -1;
|
---|
917 | }
|
---|
918 | return 0;
|
---|
919 | }
|
---|
920 |
|
---|
921 | int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
|
---|
922 | int guest_port)
|
---|
923 | {
|
---|
924 | return add_exec(&exec_list, do_pty, (char *)args,
|
---|
925 | addr_low_byte, htons(guest_port));
|
---|
926 | }
|
---|
927 |
|
---|
928 | void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
|
---|
929 | {
|
---|
930 | memcpy(client_ethaddr, ethaddr, ETH_ALEN);
|
---|
931 | }
|
---|
932 |
|
---|
933 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
934 | HANDLE *slirp_get_events(PNATState pData)
|
---|
935 | {
|
---|
936 | return (pData->phEvents);
|
---|
937 | }
|
---|
938 | #endif
|
---|