VirtualBox

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

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

build fix

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