VirtualBox

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

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

slirp: comments, cosmetics

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

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