VirtualBox

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

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

slirp: removed debugging code

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