VirtualBox

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

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

lock order fixed

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