VirtualBox

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

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

The rest of macro replacment was added

  • Property svn:eol-style set to native
File size: 25.5 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 VBOX_SLIRP_LOCK(so->so_mutex);
386 VBOX_SLIRP_UNLOCK(pData->tcb_mutex);
387
388 /*
389 * See if we need a tcp_fasttimo
390 */
391 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
392 time_fasttimo = curtime; /* Flag when we want a fasttimo */
393
394 /*
395 * NOFDREF can include still connecting to local-host,
396 * newly socreated() sockets etc. Don't want to select these.
397 */
398 if (so->so_state & SS_NOFDREF || so->s == -1)
399 goto before_loop_ends;
400
401 /*
402 * Set for reading sockets which are accepting
403 */
404 if (so->so_state & SS_FACCEPTCONN) {
405 FD_SET(so->s, readfds);
406 UPD_NFDS(so->s);
407 goto before_loop_ends;
408 }
409
410 /*
411 * Set for writing sockets which are connecting
412 */
413 if (so->so_state & SS_ISFCONNECTING) {
414 FD_SET(so->s, writefds);
415 UPD_NFDS(so->s);
416 goto before_loop_ends;
417 }
418
419 /*
420 * Set for writing if we are connected, can send more, and
421 * we have something to send
422 */
423 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
424 FD_SET(so->s, writefds);
425 UPD_NFDS(so->s);
426 }
427
428 /*
429 * Set for reading (and urgent data) if we are connected, can
430 * receive more, and we have room for it XXX /2 ?
431 */
432 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
433 FD_SET(so->s, readfds);
434 FD_SET(so->s, xfds);
435 UPD_NFDS(so->s);
436 }
437 before_loop_ends:
438 /*Release of global tcb mutex happens in the head of loop*/
439 VBOX_SLIRP_UNLOCK(so->so_mutex);
440 VBOX_SLIRP_LOCK(pData->tcb_mutex);
441#ifdef VBOX_WITH_SYNC_SLIRP
442 so = so_next;
443#endif
444 }
445
446 /*
447 * UDP sockets
448 */
449 VBOX_SLIRP_LOCK(pData->udb_mutex);
450 so = udb.so_next;
451#ifndef VBOX_WITH_SYNC_SLIRP
452 for (so = udb.so_next; so != &udb; so = so_next) {
453#else
454 while(1) {
455 if (so == &udb) {
456 VBOX_SLIRP_UNLOCK(pData->udb_mutex);
457 break;
458 }
459#endif
460 so_next = so->so_next;
461 VBOX_SLIRP_LOCK(so->so_mutex);
462 VBOX_SLIRP_UNLOCK(pData->udb_mutex);
463
464 /*
465 * See if it's timed out
466 */
467 if (so->so_expire) {
468 if (so->so_expire <= curtime) {
469 udp_detach(pData, so);
470 goto before_udp_loop_end;
471 } else
472 do_slowtimo = 1; /* Let socket expire */
473 }
474
475 /*
476 * When UDP packets are received from over the
477 * link, they're sendto()'d straight away, so
478 * no need for setting for writing
479 * Limit the number of packets queued by this session
480 * to 4. Note that even though we try and limit this
481 * to 4 packets, the session could have more queued
482 * if the packets needed to be fragmented
483 * (XXX <= 4 ?)
484 */
485 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
486 FD_SET(so->s, readfds);
487 UPD_NFDS(so->s);
488 }
489 before_udp_loop_end:
490 VBOX_SLIRP_UNLOCK(so->so_mutex);
491 VBOX_SLIRP_LOCK(pData->udb_mutex);
492#ifdef VBOX_WITH_SYNC_SLIRP
493 so = so_next;
494#endif
495 }
496 }
497
498 /*
499 * Setup timeout to use minimum CPU usage, especially when idle
500 */
501
502 /*
503 * First, see the timeout needed by *timo
504 */
505 timeout.tv_sec = 0;
506 timeout.tv_usec = -1;
507 /*
508 * If a slowtimo is needed, set timeout to 500ms from the last
509 * slow timeout. If a fast timeout is needed, set timeout within
510 * 200ms of when it was requested.
511 */
512 if (do_slowtimo) {
513 /* XXX + 10000 because some select()'s aren't that accurate */
514 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
515 if (timeout.tv_usec < 0)
516 timeout.tv_usec = 0;
517 else if (timeout.tv_usec > 510000)
518 timeout.tv_usec = 510000;
519
520 /* Can only fasttimo if we also slowtimo */
521 if (time_fasttimo) {
522 tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
523 if (tmp_time < 0)
524 tmp_time = 0;
525
526 /* Choose the smallest of the 2 */
527 if (tmp_time < timeout.tv_usec)
528 timeout.tv_usec = (u_int)tmp_time;
529 }
530 }
531 *pnfds = nfds;
532}
533
534void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
535{
536 struct socket *so, *so_next;
537 int ret;
538
539 /* Update time */
540 updtime(pData);
541
542 /*
543 * See if anything has timed out
544 */
545 if (link_up) {
546 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
547 tcp_fasttimo(pData);
548 time_fasttimo = 0;
549 }
550 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
551 ip_slowtimo(pData);
552 tcp_slowtimo(pData);
553 last_slowtimo = curtime;
554 }
555 }
556
557 /*
558 * Check sockets
559 */
560 if (link_up) {
561 /*
562 * Check TCP sockets
563 */
564 VBOX_SLIRP_LOCK(pData->tcb_mutex);
565 so = tcb.so_next;
566#ifndef VBOX_WITH_SYNC_SLIRP
567 for (so = tcb.so_next; so != &tcb; so = so_next) {
568 so_next = so->so_next;
569#else
570 while (1) {
571 if (so == &tcb) {
572 VBOX_SLIRP_UNLOCK(pData->tcb_mutex);
573 break;
574 }
575 so_next = so->so_next;
576
577 AssertRelease(so->so_mutex != NULL);
578 AssertRelease(so->so_next != NULL && so->so_prev != NULL);
579#endif
580 VBOX_SLIRP_LOCK(so->so_mutex);
581 VBOX_SLIRP_UNLOCK(pData->tcb_mutex);
582
583 /*
584 * FD_ISSET is meaningless on these sockets
585 * (and they can crash the program)
586 */
587 if (so->so_state & SS_NOFDREF || so->s == -1)
588 goto before_loop_ends;
589
590 /*
591 * Check for URG data
592 * This will soread as well, so no need to
593 * test for readfds below if this succeeds
594 */
595 if (FD_ISSET(so->s, xfds))
596 sorecvoob(pData, so);
597 /*
598 * Check sockets for reading
599 */
600 else if (FD_ISSET(so->s, readfds)) {
601 /*
602 * Check for incoming connections
603 */
604 if (so->so_state & SS_FACCEPTCONN) {
605 tcp_connect(pData, so);
606 goto before_loop_ends;
607 } /* else */
608 ret = soread(pData, so);
609
610 /* Output it if we read something */
611 if (ret > 0)
612 tcp_output(pData, sototcpcb(so));
613 }
614
615 /*
616 * Check sockets for writing
617 */
618 if (FD_ISSET(so->s, writefds)) {
619 /*
620 * Check for non-blocking, still-connecting sockets
621 */
622 if (so->so_state & SS_ISFCONNECTING) {
623 /* Connected */
624 so->so_state &= ~SS_ISFCONNECTING;
625
626 /*
627 * This should be probably guarded by PROBE_CONN too. Anyway,
628 * we disable it on OS/2 because the below send call returns
629 * EFAULT which causes the opened TCP socket to close right
630 * after it has been opened and connected.
631 */
632#ifndef RT_OS_OS2
633 ret = send(so->s, (const char *)&ret, 0, 0);
634 if (ret < 0) {
635 /* XXXXX Must fix, zero bytes is a NOP */
636 if (errno == EAGAIN || errno == EWOULDBLOCK ||
637 errno == EINPROGRESS || errno == ENOTCONN)
638 goto before_loop_ends;
639
640 /* else failed */
641 so->so_state = SS_NOFDREF;
642 }
643 /* else so->so_state &= ~SS_ISFCONNECTING; */
644#endif
645
646 /*
647 * Continue tcp_input
648 */
649 tcp_input(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
650 /* continue; */
651 } else
652 ret = sowrite(pData, so);
653 /*
654 * XXXXX If we wrote something (a lot), there
655 * could be a need for a window update.
656 * In the worst case, the remote will send
657 * a window probe to get things going again
658 */
659 }
660
661 /*
662 * Probe a still-connecting, non-blocking socket
663 * to check if it's still alive
664 */
665#ifdef PROBE_CONN
666 if (so->so_state & SS_ISFCONNECTING) {
667 ret = recv(so->s, (char *)&ret, 0,0);
668
669 if (ret < 0) {
670 /* XXX */
671 if (errno == EAGAIN || errno == EWOULDBLOCK ||
672 errno == EINPROGRESS || errno == ENOTCONN)
673 goto before_loop_ends;/* Still connecting, continue */
674
675 /* else failed */
676 so->so_state = SS_NOFDREF;
677
678 /* tcp_input will take care of it */
679 } else {
680 ret = send(so->s, &ret, 0,0);
681 if (ret < 0) {
682 /* XXX */
683 if (errno == EAGAIN || errno == EWOULDBLOCK ||
684 errno == EINPROGRESS || errno == ENOTCONN)
685 goto before_loop_ends;
686 /* else failed */
687 so->so_state = SS_NOFDREF;
688 } else
689 so->so_state &= ~SS_ISFCONNECTING;
690
691 }
692 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
693 } /* SS_ISFCONNECTING */
694#endif
695 before_loop_ends:
696 VBOX_SLIRP_UNLOCK(so->so_mutex);
697 VBOX_SLIRP_LOCK(pData->tcb_mutex);
698#ifdef VBOX_WITH_SYNC_SLIRP
699 so = so_next;
700#endif
701 }
702
703 /*
704 * Now UDP sockets.
705 * Incoming packets are sent straight away, they're not buffered.
706 * Incoming UDP data isn't buffered either.
707 */
708 VBOX_SLIRP_LOCK(pData->udb_mutex);
709 so = udb.so_next;
710#ifndef VBOX_WITH_SYNC_SLIRP
711 for (so = udb.so_next; so != &udb; so = so_next) {
712#else
713 while(1) {
714 if (so == &udb) {
715 VBOX_SLIRP_UNLOCK(pData->udb_mutex);
716 break;
717 }
718#endif
719 so_next = so->so_next;
720 VBOX_SLIRP_LOCK(so->so_mutex);
721 VBOX_SLIRP_UNLOCK(pData->udb_mutex);
722
723 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
724 sorecvfrom(pData, so);
725 }
726 VBOX_SLIRP_UNLOCK(so->so_mutex);
727 VBOX_SLIRP_LOCK(pData->udb_mutex);
728#ifdef VBOX_WITH_SYNC_SLIRP
729 so = so_next;
730#endif
731 }
732 }
733
734 /*
735 * See if we can start outputting
736 */
737#ifndef VBOX_WITH_SYNC_SLIRP
738 if (if_queued && link_up)
739 if_start(pData);
740#else
741#if 0
742 if (link_up) {
743 VBOX_SLIRP_LOCK(pData->if_queued_mutex);
744 if (if_queued > 0){
745VBOX_SLIRP_UNLOCK(pData->if_queued_mutex);
746 if_start(pData);
747 }
748 else {
749VBOX_SLIRP_UNLOCK(pData->if_queued_mutex);
750 }
751 }
752#endif
753#endif
754}
755
756#define ETH_ALEN 6
757#define ETH_HLEN 14
758
759#define ETH_P_IP 0x0800 /* Internet Protocol packet */
760#define ETH_P_ARP 0x0806 /* Address Resolution packet */
761
762#define ARPOP_REQUEST 1 /* ARP request */
763#define ARPOP_REPLY 2 /* ARP reply */
764
765struct ethhdr
766{
767 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
768 unsigned char h_source[ETH_ALEN]; /* source ether addr */
769 unsigned short h_proto; /* packet type ID field */
770};
771
772struct arphdr
773{
774 unsigned short ar_hrd; /* format of hardware address */
775 unsigned short ar_pro; /* format of protocol address */
776 unsigned char ar_hln; /* length of hardware address */
777 unsigned char ar_pln; /* length of protocol address */
778 unsigned short ar_op; /* ARP opcode (command) */
779
780 /*
781 * Ethernet looks like this : This bit is variable sized however...
782 */
783 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
784 unsigned char ar_sip[4]; /* sender IP address */
785 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
786 unsigned char ar_tip[4]; /* target IP address */
787};
788
789static
790void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
791{
792 struct ethhdr *eh = (struct ethhdr *)pkt;
793 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
794 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
795 struct ethhdr *reh = (struct ethhdr *)arp_reply;
796 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
797 int ar_op;
798 struct ex_list *ex_ptr;
799 uint32_t htip = ntohl(*(uint32_t*)ah->ar_tip);
800
801 ar_op = ntohs(ah->ar_op);
802 switch(ar_op) {
803 case ARPOP_REQUEST:
804 if ((htip & pData->netmask) == ntohl(special_addr.s_addr)) {
805 if ( (htip & ~pData->netmask) == CTL_DNS
806 || (htip & ~pData->netmask) == CTL_ALIAS)
807 goto arp_ok;
808 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
809 if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
810 goto arp_ok;
811 }
812 return;
813 arp_ok:
814 /* XXX: make an ARP request to have the client address */
815 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
816
817 /* ARP request for alias/dns mac address */
818 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
819 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
820 reh->h_source[5] = ah->ar_tip[3];
821 reh->h_proto = htons(ETH_P_ARP);
822
823 rah->ar_hrd = htons(1);
824 rah->ar_pro = htons(ETH_P_IP);
825 rah->ar_hln = ETH_ALEN;
826 rah->ar_pln = 4;
827 rah->ar_op = htons(ARPOP_REPLY);
828 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
829 memcpy(rah->ar_sip, ah->ar_tip, 4);
830 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
831 memcpy(rah->ar_tip, ah->ar_sip, 4);
832 slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
833 }
834 break;
835 default:
836 break;
837 }
838}
839
840void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
841{
842 struct mbuf *m;
843 int proto;
844
845 if (pkt_len < ETH_HLEN)
846 return;
847
848 proto = ntohs(*(uint16_t *)(pkt + 12));
849 switch(proto) {
850 case ETH_P_ARP:
851 arp_input(pData, pkt, pkt_len);
852 break;
853 case ETH_P_IP:
854 /* Update time. Important if the network is very quiet, as otherwise
855 * the first outgoing connection gets an incorrect timestamp. */
856 updtime(pData);
857
858 m = m_get(pData);
859 if (!m)
860 return;
861 VBOX_SLIRP_LOCK(m->m_mutex);
862 /* Note: we add to align the IP header */
863 if (M_FREEROOM(m) < pkt_len + 2) {
864 m_inc(m, pkt_len + 2);
865 }
866 m->m_len = pkt_len + 2;
867 memcpy(m->m_data + 2, pkt, pkt_len);
868
869 m->m_data += 2 + ETH_HLEN;
870 m->m_len -= 2 + ETH_HLEN;
871
872 ip_input(pData, m);
873 VBOX_SLIRP_UNLOCK(m->m_mutex);
874 break;
875 default:
876 break;
877 }
878}
879
880/* output the IP packet to the ethernet device */
881void if_encap(PNATState pData, const uint8_t *ip_data, int ip_data_len)
882{
883 uint8_t buf[1600];
884 struct ethhdr *eh = (struct ethhdr *)buf;
885
886 if (ip_data_len + ETH_HLEN > sizeof(buf))
887 return;
888
889 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
890 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
891 /* XXX: not correct */
892 eh->h_source[5] = CTL_ALIAS;
893 eh->h_proto = htons(ETH_P_IP);
894 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
895 slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
896}
897
898int slirp_redir(PNATState pData, int is_udp, int host_port,
899 struct in_addr guest_addr, int guest_port)
900{
901 if (is_udp) {
902 if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
903 htons(guest_port), 0))
904 return -1;
905 } else {
906 if (!solisten(pData, htons(host_port), guest_addr.s_addr,
907 htons(guest_port), 0))
908 return -1;
909 }
910 return 0;
911}
912
913int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
914 int guest_port)
915{
916 return add_exec(&exec_list, do_pty, (char *)args,
917 addr_low_byte, htons(guest_port));
918}
919
920void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
921{
922 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
923}
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