VirtualBox

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

最後變更 在這個檔案從15365是 15355,由 vboxsync 提交於 16 年 前

slirp:icmp: enabling UDP traceroute on Unix

  • 屬性 svn:eol-style 設為 native
檔案大小: 35.8 KB
 
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 static uint32_t stat_time;
694
695 STAM_REL_PROFILE_START(&pData->StatPoll, a);
696
697 /* Update time */
698 updtime(pData);
699
700#ifdef LOG_ENABLED
701 if (curtime - stat_time > 10000)
702 {
703 stat_time = curtime;
704 sockstats(pData);
705 }
706#endif
707
708 /*
709 * See if anything has timed out
710 */
711 if (link_up)
712 {
713 if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
714 {
715 STAM_REL_PROFILE_START(&pData->StatFastTimer, a);
716 tcp_fasttimo(pData);
717 time_fasttimo = 0;
718 STAM_REL_PROFILE_STOP(&pData->StatFastTimer, a);
719 }
720 if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
721 {
722 STAM_REL_PROFILE_START(&pData->StatSlowTimer, a);
723 ip_slowtimo(pData);
724 tcp_slowtimo(pData);
725 last_slowtimo = curtime;
726 STAM_REL_PROFILE_STOP(&pData->StatSlowTimer, a);
727 }
728 }
729#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
730 if (fTimeout)
731 return; /* only timer update */
732#endif
733
734 /*
735 * Check sockets
736 */
737 if (link_up)
738 {
739#if defined(VBOX_WITH_SLIRP_ICMP)
740# if defined(RT_OS_WINDOWS)
741 if (fIcmp)
742 sorecvfrom(pData, &pData->icmp_socket);
743# else
744 if (pData->icmp_socket.s != -1 && FD_ISSET(pData->icmp_socket.s, readfds))
745 sorecvfrom(pData, &pData->icmp_socket);
746# endif
747#endif
748 /*
749 * Check TCP sockets
750 */
751 for (so = tcb.so_next; so != &tcb; so = so_next)
752 {
753 so_next = so->so_next;
754
755 /*
756 * FD_ISSET is meaningless on these sockets
757 * (and they can crash the program)
758 */
759 if (so->so_state & SS_NOFDREF || so->s == -1)
760 continue;
761
762 POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
763
764 LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
765
766 /*
767 * Check for URG data
768 * This will soread as well, so no need to
769 * test for readfds below if this succeeds
770 */
771
772 /* out-of-band data */
773 if (CHECK_FD_SET(so, NetworkEvents, xfds))
774 {
775 sorecvoob(pData, so);
776 }
777
778 /*
779 * Check sockets for reading
780 */
781 else if (CHECK_FD_SET(so, NetworkEvents, readfds))
782 {
783 /*
784 * Check for incoming connections
785 */
786 if (so->so_state & SS_FACCEPTCONN)
787 {
788 tcp_connect(pData, so);
789#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
790 if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
791#endif
792 continue;
793 }
794
795 ret = soread(pData, so, /*fCloseIfNothingRead=*/false);
796 /* Output it if we read something */
797 if (ret > 0)
798 tcp_output(pData, sototcpcb(so));
799 }
800
801#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
802 /*
803 * Check for FD_CLOSE events.
804 */
805 if (NetworkEvents.lNetworkEvents & FD_CLOSE)
806 {
807 /*
808 * drain the socket
809 */
810 for (;;)
811 {
812 ret = soread(pData, so, /*fCloseIfNothingRead=*/true);
813 if (ret > 0)
814 tcp_output(pData, sototcpcb(so));
815 else
816 break;
817 }
818 }
819#endif
820
821 /*
822 * Check sockets for writing
823 */
824 if (CHECK_FD_SET(so, NetworkEvents, writefds))
825 {
826 /*
827 * Check for non-blocking, still-connecting sockets
828 */
829 if (so->so_state & SS_ISFCONNECTING)
830 {
831 /* Connected */
832 so->so_state &= ~SS_ISFCONNECTING;
833
834 /*
835 * This should be probably guarded by PROBE_CONN too. Anyway,
836 * we disable it on OS/2 because the below send call returns
837 * EFAULT which causes the opened TCP socket to close right
838 * after it has been opened and connected.
839 */
840#ifndef RT_OS_OS2
841 ret = send(so->s, (const char *)&ret, 0, 0);
842 if (ret < 0)
843 {
844 /* XXXXX Must fix, zero bytes is a NOP */
845 if ( errno == EAGAIN
846 || errno == EWOULDBLOCK
847 || errno == EINPROGRESS
848 || errno == ENOTCONN)
849 continue;
850
851 /* else failed */
852 so->so_state = SS_NOFDREF;
853 }
854 /* else so->so_state &= ~SS_ISFCONNECTING; */
855#endif
856
857 /*
858 * Continue tcp_input
859 */
860 tcp_input(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
861 /* continue; */
862 }
863 else
864 ret = sowrite(pData, so);
865 /*
866 * XXX If we wrote something (a lot), there could be the need
867 * for a window update. In the worst case, the remote will send
868 * a window probe to get things going again.
869 */
870 }
871
872 /*
873 * Probe a still-connecting, non-blocking socket
874 * to check if it's still alive
875 */
876#ifdef PROBE_CONN
877 if (so->so_state & SS_ISFCONNECTING)
878 {
879 ret = recv(so->s, (char *)&ret, 0, 0);
880
881 if (ret < 0)
882 {
883 /* XXX */
884 if ( errno == EAGAIN
885 || errno == EWOULDBLOCK
886 || errno == EINPROGRESS
887 || errno == ENOTCONN)
888 {
889 continue; /* Still connecting, continue */
890 }
891
892 /* else failed */
893 so->so_state = SS_NOFDREF;
894
895 /* tcp_input will take care of it */
896 }
897 else
898 {
899 ret = send(so->s, &ret, 0, 0);
900 if (ret < 0)
901 {
902 /* XXX */
903 if ( errno == EAGAIN
904 || errno == EWOULDBLOCK
905 || errno == EINPROGRESS
906 || errno == ENOTCONN)
907 {
908 continue;
909 }
910 /* else failed */
911 so->so_state = SS_NOFDREF;
912 }
913 else
914 so->so_state &= ~SS_ISFCONNECTING;
915
916 }
917 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
918 } /* SS_ISFCONNECTING */
919#endif
920 }
921
922 /*
923 * Now UDP sockets.
924 * Incoming packets are sent straight away, they're not buffered.
925 * Incoming UDP data isn't buffered either.
926 */
927 for (so = udb.so_next; so != &udb; so = so_next)
928 {
929 so_next = so->so_next;
930
931 POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
932
933 LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
934
935 if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
936 {
937 sorecvfrom(pData, so);
938 }
939 }
940
941#if 0
942#if defined(VBOX_WITH_SLIRP_ICMP)
943# if defined(RT_OS_WINDOWS)
944 if (fIcmp)
945 sorecvfrom(pData, &pData->icmp_socket);
946# else
947 if (pData->icmp_socket.s != -1 && FD_ISSET(pData->icmp_socket.s, readfds))
948 sorecvfrom(pData, &pData->icmp_socket);
949# endif
950#endif
951#endif
952 }
953
954 /*
955 * See if we can start outputting
956 */
957 if (if_queued && link_up)
958 if_start(pData);
959
960 STAM_REL_PROFILE_STOP(&pData->StatPoll, a);
961}
962
963#define ETH_ALEN 6
964#define ETH_HLEN 14
965
966#define ETH_P_IP 0x0800 /* Internet Protocol packet */
967#define ETH_P_ARP 0x0806 /* Address Resolution packet */
968
969#define ARPOP_REQUEST 1 /* ARP request */
970#define ARPOP_REPLY 2 /* ARP reply */
971
972struct ethhdr
973{
974 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
975 unsigned char h_source[ETH_ALEN]; /* source ether addr */
976 unsigned short h_proto; /* packet type ID field */
977};
978
979struct arphdr
980{
981 unsigned short ar_hrd; /* format of hardware address */
982 unsigned short ar_pro; /* format of protocol address */
983 unsigned char ar_hln; /* length of hardware address */
984 unsigned char ar_pln; /* length of protocol address */
985 unsigned short ar_op; /* ARP opcode (command) */
986
987 /*
988 * Ethernet looks like this : This bit is variable sized however...
989 */
990 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
991 unsigned char ar_sip[4]; /* sender IP address */
992 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
993 unsigned char ar_tip[4]; /* target IP address */
994};
995
996static
997void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
998{
999 struct ethhdr *eh = (struct ethhdr *)pkt;
1000 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
1001 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
1002 struct ethhdr *reh = (struct ethhdr *)arp_reply;
1003 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
1004 int ar_op;
1005 struct ex_list *ex_ptr;
1006 uint32_t htip = ntohl(*(uint32_t*)ah->ar_tip);
1007
1008 ar_op = ntohs(ah->ar_op);
1009 switch(ar_op)
1010 {
1011 case ARPOP_REQUEST:
1012 if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
1013 {
1014 if ( (htip & ~pData->netmask) == CTL_DNS
1015 || (htip & ~pData->netmask) == CTL_ALIAS)
1016 goto arp_ok;
1017 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1018 {
1019 if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
1020 goto arp_ok;
1021 }
1022 return;
1023 arp_ok:
1024 /* XXX: make an ARP request to have the client address */
1025 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
1026
1027 /* ARP request for alias/dns mac address */
1028 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
1029 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
1030 reh->h_source[5] = ah->ar_tip[3];
1031 reh->h_proto = htons(ETH_P_ARP);
1032
1033 rah->ar_hrd = htons(1);
1034 rah->ar_pro = htons(ETH_P_IP);
1035 rah->ar_hln = ETH_ALEN;
1036 rah->ar_pln = 4;
1037 rah->ar_op = htons(ARPOP_REPLY);
1038 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
1039 memcpy(rah->ar_sip, ah->ar_tip, 4);
1040 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
1041 memcpy(rah->ar_tip, ah->ar_sip, 4);
1042 slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
1043 }
1044 break;
1045 default:
1046 break;
1047 }
1048}
1049
1050void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
1051{
1052 struct mbuf *m;
1053 int proto;
1054
1055 if (pkt_len < ETH_HLEN)
1056 return;
1057
1058 proto = ntohs(*(uint16_t *)(pkt + 12));
1059 switch(proto)
1060 {
1061 case ETH_P_ARP:
1062 arp_input(pData, pkt, pkt_len);
1063 break;
1064 case ETH_P_IP:
1065 /* Update time. Important if the network is very quiet, as otherwise
1066 * the first outgoing connection gets an incorrect timestamp. */
1067 updtime(pData);
1068
1069 m = m_get(pData);
1070 if (!m)
1071 return;
1072 /* Note: we add to align the IP header */
1073 if (M_FREEROOM(m) < pkt_len + 2)
1074 {
1075 m_inc(m, pkt_len + 2);
1076 }
1077 m->m_len = pkt_len + 2;
1078 memcpy(m->m_data + 2, pkt, pkt_len);
1079
1080 m->m_data += 2 + ETH_HLEN;
1081 m->m_len -= 2 + ETH_HLEN;
1082
1083 ip_input(pData, m);
1084 break;
1085 default:
1086 break;
1087 }
1088}
1089
1090/* output the IP packet to the ethernet device */
1091void if_encap(PNATState pData, const uint8_t *ip_data, int ip_data_len)
1092{
1093 uint8_t buf[1600];
1094 struct ethhdr *eh = (struct ethhdr *)buf;
1095
1096 if (ip_data_len + ETH_HLEN > sizeof(buf))
1097 return;
1098
1099 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
1100 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
1101 /* XXX: not correct */
1102 eh->h_source[5] = CTL_ALIAS;
1103 eh->h_proto = htons(ETH_P_IP);
1104 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
1105 slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
1106}
1107
1108int slirp_redir(PNATState pData, int is_udp, int host_port,
1109 struct in_addr guest_addr, int guest_port)
1110{
1111 if (is_udp)
1112 {
1113 if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
1114 htons(guest_port), 0))
1115 return -1;
1116 }
1117 else
1118 {
1119 if (!solisten(pData, htons(host_port), guest_addr.s_addr,
1120 htons(guest_port), 0))
1121 return -1;
1122 }
1123 return 0;
1124}
1125
1126int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
1127 int guest_port)
1128{
1129 return add_exec(&exec_list, do_pty, (char *)args,
1130 addr_low_byte, htons(guest_port));
1131}
1132
1133void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
1134{
1135 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
1136}
1137
1138#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
1139HANDLE *slirp_get_events(PNATState pData)
1140{
1141 return pData->phEvents;
1142}
1143void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
1144{
1145 pData->phEvents[index] = hEvent;
1146}
1147#endif
1148
1149unsigned int slirp_get_timeout_ms(PNATState pData)
1150{
1151 if (link_up)
1152 {
1153 if (time_fasttimo)
1154 return 2;
1155 if (do_slowtimo)
1156 return 500; /* see PR_SLOWHZ */
1157 }
1158 return 0;
1159}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette