VirtualBox

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

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

removing extra if/ifndefs
introduced defered socket removing, to prevent deletion of socket being in lock

  • Property svn:eol-style set to native
File size: 26.0 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#ifdef VBOX_WITH_SYNC_SLIRP
589 if (so->so_destroy == 1) {
590 VBOX_SLIRP_LOCK_DESTROY(so->so_mutex);
591 free(so);
592 so = tcb.so_next;
593 so_next = so->so_next;
594 }
595 VBOX_SLIRP_LOCK(so->so_mutex);
596#endif
597 VBOX_SLIRP_UNLOCK(pData->tcb_mutex);
598
599 /*
600 * FD_ISSET is meaningless on these sockets
601 * (and they can crash the program)
602 */
603 if (so->so_state & SS_NOFDREF || so->s == -1)
604 goto before_loop_ends;
605
606 /*
607 * Check for URG data
608 * This will soread as well, so no need to
609 * test for readfds below if this succeeds
610 */
611 if (FD_ISSET(so->s, xfds))
612 sorecvoob(pData, so);
613 /*
614 * Check sockets for reading
615 */
616 else if (FD_ISSET(so->s, readfds)) {
617 /*
618 * Check for incoming connections
619 */
620 if (so->so_state & SS_FACCEPTCONN) {
621 tcp_connect(pData, so);
622 goto before_loop_ends;
623 } /* else */
624 ret = soread(pData, so);
625
626 /* Output it if we read something */
627 if (ret > 0)
628 tcp_output(pData, sototcpcb(so));
629 }
630
631 /*
632 * Check sockets for writing
633 */
634 if (FD_ISSET(so->s, writefds)) {
635 /*
636 * Check for non-blocking, still-connecting sockets
637 */
638 if (so->so_state & SS_ISFCONNECTING) {
639 /* Connected */
640 so->so_state &= ~SS_ISFCONNECTING;
641
642 /*
643 * This should be probably guarded by PROBE_CONN too. Anyway,
644 * we disable it on OS/2 because the below send call returns
645 * EFAULT which causes the opened TCP socket to close right
646 * after it has been opened and connected.
647 */
648#ifndef RT_OS_OS2
649 ret = send(so->s, (const char *)&ret, 0, 0);
650 if (ret < 0) {
651 /* XXXXX Must fix, zero bytes is a NOP */
652 if (errno == EAGAIN || errno == EWOULDBLOCK ||
653 errno == EINPROGRESS || errno == ENOTCONN)
654 goto before_loop_ends;
655
656 /* else failed */
657 so->so_state = SS_NOFDREF;
658 }
659 /* else so->so_state &= ~SS_ISFCONNECTING; */
660#endif
661
662 /*
663 * Continue tcp_input
664 */
665 tcp_input(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
666 /* continue; */
667 } else
668 ret = sowrite(pData, so);
669 /*
670 * XXXXX If we wrote something (a lot), there
671 * could be a need for a window update.
672 * In the worst case, the remote will send
673 * a window probe to get things going again
674 */
675 }
676
677 /*
678 * Probe a still-connecting, non-blocking socket
679 * to check if it's still alive
680 */
681#ifdef PROBE_CONN
682 if (so->so_state & SS_ISFCONNECTING) {
683 ret = recv(so->s, (char *)&ret, 0,0);
684
685 if (ret < 0) {
686 /* XXX */
687 if (errno == EAGAIN || errno == EWOULDBLOCK ||
688 errno == EINPROGRESS || errno == ENOTCONN)
689 goto before_loop_ends;/* Still connecting, continue */
690
691 /* else failed */
692 so->so_state = SS_NOFDREF;
693
694 /* tcp_input will take care of it */
695 } else {
696 ret = send(so->s, &ret, 0,0);
697 if (ret < 0) {
698 /* XXX */
699 if (errno == EAGAIN || errno == EWOULDBLOCK ||
700 errno == EINPROGRESS || errno == ENOTCONN)
701 goto before_loop_ends;
702 /* else failed */
703 so->so_state = SS_NOFDREF;
704 } else
705 so->so_state &= ~SS_ISFCONNECTING;
706
707 }
708 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
709 } /* SS_ISFCONNECTING */
710#endif
711 before_loop_ends:
712 VBOX_SLIRP_UNLOCK(so->so_mutex);
713 VBOX_SLIRP_LOCK(pData->tcb_mutex);
714#ifdef VBOX_WITH_SYNC_SLIRP
715 so = so_next;
716#endif
717 }
718
719 /*
720 * Now UDP sockets.
721 * Incoming packets are sent straight away, they're not buffered.
722 * Incoming UDP data isn't buffered either.
723 */
724 VBOX_SLIRP_LOCK(pData->udb_mutex);
725 so = udb.so_next;
726#ifndef VBOX_WITH_SYNC_SLIRP
727 for (so = udb.so_next; so != &udb; so = so_next) {
728#else
729 while(1) {
730 if (so == &udb) {
731 VBOX_SLIRP_UNLOCK(pData->udb_mutex);
732 break;
733 }
734#endif
735 so_next = so->so_next;
736 VBOX_SLIRP_LOCK(so->so_mutex);
737 VBOX_SLIRP_UNLOCK(pData->udb_mutex);
738
739 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
740 sorecvfrom(pData, so);
741 }
742 VBOX_SLIRP_UNLOCK(so->so_mutex);
743 VBOX_SLIRP_LOCK(pData->udb_mutex);
744#ifdef VBOX_WITH_SYNC_SLIRP
745 so = so_next;
746#endif
747 }
748 }
749
750 /*
751 * See if we can start outputting
752 */
753#ifndef VBOX_WITH_SYNC_SLIRP
754 if (if_queued && link_up)
755 if_start(pData);
756#else
757#if 0
758 if (link_up) {
759 VBOX_SLIRP_LOCK(pData->if_queued_mutex);
760 if (if_queued > 0){
761VBOX_SLIRP_UNLOCK(pData->if_queued_mutex);
762 if_start(pData);
763 }
764 else {
765VBOX_SLIRP_UNLOCK(pData->if_queued_mutex);
766 }
767 }
768#endif
769#endif
770}
771
772#define ETH_ALEN 6
773#define ETH_HLEN 14
774
775#define ETH_P_IP 0x0800 /* Internet Protocol packet */
776#define ETH_P_ARP 0x0806 /* Address Resolution packet */
777
778#define ARPOP_REQUEST 1 /* ARP request */
779#define ARPOP_REPLY 2 /* ARP reply */
780
781struct ethhdr
782{
783 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
784 unsigned char h_source[ETH_ALEN]; /* source ether addr */
785 unsigned short h_proto; /* packet type ID field */
786};
787
788struct arphdr
789{
790 unsigned short ar_hrd; /* format of hardware address */
791 unsigned short ar_pro; /* format of protocol address */
792 unsigned char ar_hln; /* length of hardware address */
793 unsigned char ar_pln; /* length of protocol address */
794 unsigned short ar_op; /* ARP opcode (command) */
795
796 /*
797 * Ethernet looks like this : This bit is variable sized however...
798 */
799 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
800 unsigned char ar_sip[4]; /* sender IP address */
801 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
802 unsigned char ar_tip[4]; /* target IP address */
803};
804
805static
806void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
807{
808 struct ethhdr *eh = (struct ethhdr *)pkt;
809 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
810 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
811 struct ethhdr *reh = (struct ethhdr *)arp_reply;
812 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
813 int ar_op;
814 struct ex_list *ex_ptr;
815 uint32_t htip = ntohl(*(uint32_t*)ah->ar_tip);
816
817 ar_op = ntohs(ah->ar_op);
818 switch(ar_op) {
819 case ARPOP_REQUEST:
820 if ((htip & pData->netmask) == ntohl(special_addr.s_addr)) {
821 if ( (htip & ~pData->netmask) == CTL_DNS
822 || (htip & ~pData->netmask) == CTL_ALIAS)
823 goto arp_ok;
824 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
825 if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
826 goto arp_ok;
827 }
828 return;
829 arp_ok:
830 /* XXX: make an ARP request to have the client address */
831 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
832
833 /* ARP request for alias/dns mac address */
834 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
835 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
836 reh->h_source[5] = ah->ar_tip[3];
837 reh->h_proto = htons(ETH_P_ARP);
838
839 rah->ar_hrd = htons(1);
840 rah->ar_pro = htons(ETH_P_IP);
841 rah->ar_hln = ETH_ALEN;
842 rah->ar_pln = 4;
843 rah->ar_op = htons(ARPOP_REPLY);
844 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
845 memcpy(rah->ar_sip, ah->ar_tip, 4);
846 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
847 memcpy(rah->ar_tip, ah->ar_sip, 4);
848 slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
849 }
850 break;
851 default:
852 break;
853 }
854}
855
856void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
857{
858 struct mbuf *m;
859 int proto;
860
861 if (pkt_len < ETH_HLEN)
862 return;
863
864 proto = ntohs(*(uint16_t *)(pkt + 12));
865 switch(proto) {
866 case ETH_P_ARP:
867 arp_input(pData, pkt, pkt_len);
868 break;
869 case ETH_P_IP:
870 /* Update time. Important if the network is very quiet, as otherwise
871 * the first outgoing connection gets an incorrect timestamp. */
872 updtime(pData);
873
874 m = m_get(pData);
875 if (!m)
876 return;
877 VBOX_SLIRP_LOCK(m->m_mutex);
878 /* Note: we add to align the IP header */
879 if (M_FREEROOM(m) < pkt_len + 2) {
880 m_inc(m, pkt_len + 2);
881 }
882 m->m_len = pkt_len + 2;
883 memcpy(m->m_data + 2, pkt, pkt_len);
884
885 m->m_data += 2 + ETH_HLEN;
886 m->m_len -= 2 + ETH_HLEN;
887
888 ip_input(pData, m);
889 VBOX_SLIRP_UNLOCK(m->m_mutex);
890 break;
891 default:
892 break;
893 }
894}
895
896/* output the IP packet to the ethernet device */
897void if_encap(PNATState pData, const uint8_t *ip_data, int ip_data_len)
898{
899 uint8_t buf[1600];
900 struct ethhdr *eh = (struct ethhdr *)buf;
901
902 if (ip_data_len + ETH_HLEN > sizeof(buf))
903 return;
904
905 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
906 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
907 /* XXX: not correct */
908 eh->h_source[5] = CTL_ALIAS;
909 eh->h_proto = htons(ETH_P_IP);
910 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
911 slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
912}
913
914int slirp_redir(PNATState pData, int is_udp, int host_port,
915 struct in_addr guest_addr, int guest_port)
916{
917 if (is_udp) {
918 if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
919 htons(guest_port), 0))
920 return -1;
921 } else {
922 if (!solisten(pData, htons(host_port), guest_addr.s_addr,
923 htons(guest_port), 0))
924 return -1;
925 }
926 return 0;
927}
928
929int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
930 int guest_port)
931{
932 return add_exec(&exec_list, do_pty, (char *)args,
933 addr_low_byte, htons(guest_port));
934}
935
936void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
937{
938 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
939}
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