VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/slirp.c@ 15106

Last change on this file since 15106 was 15095, checked in by vboxsync, 16 years ago

slirp: tabs

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette