VirtualBox

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

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

NAT: logging

  • Property svn:eol-style set to native
File size: 51.3 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 <VBox/pdmdrv.h>
8#include <iprt/assert.h>
9#ifndef RT_OS_WINDOWS
10#include <sys/ioctl.h>
11#include <poll.h>
12#endif
13
14#if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
15
16# ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
17# define DO_ENGAGE_EVENT1(so, fdset, label) \
18 do { \
19 FD_SET((so)->s, (fdset)); \
20 UPD_NFDS((so)->s); \
21 } while(0)
22
23
24# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
25 do { \
26 FD_SET((so)->s, (fdset1)); \
27 FD_SET((so)->s, (fdset2)); \
28 UPD_NFDS((so)->s); \
29 } while(0)
30
31# define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
32
33# define DO_CHECK_FD_SET(so, events, fdset) (FD_ISSET((so)->s, fdset))
34# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 0 /*specific for Unix API */
35# else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
36# define DO_ENGAGE_EVENT1(so, fdset, label) \
37 do { \
38 if( so->so_poll_index != -1 \
39 && so->s == polls[so->so_poll_index].fd) { \
40 polls[so->so_poll_index].events |= N_(fdset ## _poll); \
41 break; /* out of this loop */ \
42 } \
43 AssertRelease(poll_index < (nfds)); \
44 AssertRelease(poll_index >= 0 && poll_index < (nfds)); \
45 polls[poll_index].fd = (so)->s; \
46 (so)->so_poll_index = poll_index; \
47 polls[poll_index].events = N_(fdset ## _poll); \
48 polls[poll_index].revents = 0; \
49 poll_index++; \
50 } while(0)
51
52
53# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
54 do { \
55 if( so->so_poll_index != -1 \
56 && so->s == polls[so->so_poll_index].fd) { \
57 polls[so->so_poll_index].events |= \
58 N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
59 break; /* out of this loop */ \
60 } \
61 AssertRelease(poll_index < (nfds)); \
62 polls[poll_index].fd = (so)->s; \
63 (so)->so_poll_index = poll_index; \
64 polls[poll_index].events = \
65 N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
66 poll_index++; \
67 } while(0)
68
69# define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
70
71# define DO_CHECK_FD_SET(so, events, fdset) ( ((so)->so_poll_index != -1) \
72 && ((so)->so_poll_index <= ndfs) \
73 && ((so)->s == polls[so->so_poll_index].fd) \
74 && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)))
75# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
76# define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
77# endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
78
79# ifndef RT_OS_WINDOWS
80
81# ifndef RT_OS_LINUX
82# define readfds_poll (POLLRDNORM)
83# define writefds_poll (POLLWRNORM)
84# define xfds_poll (POLLRDBAND|POLLWRBAND|POLLPRI)
85# else
86# define readfds_poll (POLLIN)
87# define writefds_poll (POLLOUT)
88# define xfds_poll (POLLPRI)
89# endif
90# define rderr_poll (POLLERR)
91# define rdhup_poll (POLLHUP)
92# define nval_poll (POLLNVAL)
93
94# define ICMP_ENGAGE_EVENT(so, fdset) \
95 do { \
96 if (pData->icmp_socket.s != -1) \
97 DO_ENGAGE_EVENT1((so), fdset, ICMP); \
98 } while (0)
99# else /* !RT_OS_WINDOWS */
100# ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
101# define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
102# else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
103# define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0
104# endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
105# define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
106#endif /* RT_OS_WINDOWS */
107
108#else /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
109
110/*
111 * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
112 * So no call to WSAEventSelect necessary.
113 */
114# define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
115
116# define DO_ENGAGE_EVENT1(so, fdset1, label) \
117 do { \
118 rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
119 if (rc == SOCKET_ERROR) \
120 { \
121 /* This should not happen */ \
122 error = WSAGetLastError(); \
123 LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
124 error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
125 } \
126 } while(0); \
127 CONTINUE(label)
128
129# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
130 DO_ENGAGE_EVENT1((so), (fdset1), label)
131
132# define DO_POLL_EVENTS(rc, error, so, events, label) \
133 (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
134 if ((rc) == SOCKET_ERROR) \
135 { \
136 (error) = WSAGetLastError(); \
137 LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
138 CONTINUE(label); \
139 }
140
141# define acceptds_win FD_ACCEPT
142# define acceptds_win_bit FD_ACCEPT_BIT
143
144# define readfds_win FD_READ
145# define readfds_win_bit FD_READ_BIT
146
147# define writefds_win FD_WRITE
148# define writefds_win_bit FD_WRITE_BIT
149
150# define xfds_win FD_OOB
151# define xfds_win_bit FD_OOB_BIT
152
153# define DO_CHECK_FD_SET(so, events, fdset) \
154 (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
155
156# define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
157# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 1 /*specific for Unix API */
158
159#endif /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
160
161#define TCP_ENGAGE_EVENT1(so, fdset) \
162 DO_ENGAGE_EVENT1((so), fdset, tcp)
163
164#define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
165 DO_ENGAGE_EVENT2((so), fdset1, fdset2, tcp)
166
167#define UDP_ENGAGE_EVENT(so, fdset) \
168 DO_ENGAGE_EVENT1((so), fdset, udp)
169
170#define POLL_TCP_EVENTS(rc, error, so, events) \
171 DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
172
173#define POLL_UDP_EVENTS(rc, error, so, events) \
174 DO_POLL_EVENTS((rc), (error), (so), (events), udp)
175
176#define CHECK_FD_SET(so, events, set) \
177 (DO_CHECK_FD_SET((so), (events), set))
178
179#define WIN_CHECK_FD_SET(so, events, set) \
180 (DO_WIN_CHECK_FD_SET((so), (events), set))
181#define UNIX_CHECK_FD_SET(so, events, set) \
182 (DO_UNIX_CHECK_FD_SET(so, events, set))
183
184/*
185 * Loging macros
186 */
187#if VBOX_WITH_DEBUG_NAT_SOCKETS
188# if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
189# if defined(RT_OS_WINDOWS)
190# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
191 do { \
192 LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
193 } while (0)
194# else /* RT_OS_WINDOWS */
195# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
196 do { \
197 LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
198 CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
199 CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
200 CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
201 CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
202 CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
203 CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
204 } while (0)
205# endif /* !RT_OS_WINDOWS */
206# else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
207# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
208 do { \
209 LogRel((" " #proto " %R[natsock] %s %s %s\n", (so), \
210 FD_ISSET((so)->s, (r_fdset))?"READ":"", \
211 FD_ISSET((so)->s, (w_fdset))?"WRITE":"", \
212 FD_ISSET((so)->s, (x_fdset))?"OOB":"")); \
213 } while (0)
214# endif
215#else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
216# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
217#endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
218
219#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)
220
221static const uint8_t special_ethaddr[6] =
222{
223 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
224};
225
226#ifdef RT_OS_WINDOWS
227
228static int get_dns_addr_domain(PNATState pData, bool fVerbose,
229 struct in_addr *pdns_addr,
230 const char **ppszDomain)
231{
232 int rc = 0;
233 FIXED_INFO *FixedInfo = NULL;
234 ULONG BufLen;
235 DWORD ret;
236 IP_ADDR_STRING *pIPAddr;
237 struct in_addr tmp_addr;
238
239 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
240 BufLen = sizeof(FIXED_INFO);
241
242 /** @todo: this API returns all DNS servers, no matter whether the
243 * corresponding network adapter is disabled or not. Maybe replace
244 * this by GetAdapterAddresses(), which is XP/Vista only though. */
245 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen))
246 {
247 if (FixedInfo)
248 {
249 GlobalFree(FixedInfo);
250 FixedInfo = NULL;
251 }
252 FixedInfo = GlobalAlloc(GPTR, BufLen);
253 }
254
255 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS)
256 {
257 Log(("GetNetworkParams failed. ret = %08x\n", (u_int)ret ));
258 if (FixedInfo)
259 {
260 GlobalFree(FixedInfo);
261 FixedInfo = NULL;
262 }
263 rc = -1;
264 goto get_dns_prefix;
265 }
266
267#ifndef VBOX_WITH_MULTI_DNS
268 pIPAddr = &(FixedInfo->DnsServerList);
269 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
270 Log(("nat: DNS Servers:\n"));
271 if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
272 LogRel(("NAT: DNS address: %s\n", pIPAddr->IpAddress.String));
273 *pdns_addr = tmp_addr;
274
275 pIPAddr = FixedInfo -> DnsServerList.Next;
276 while (pIPAddr)
277 {
278 if (fVerbose)
279 LogRel(("NAT: ignored DNS address: %s\n", pIPAddr ->IpAddress.String));
280 pIPAddr = pIPAddr ->Next;
281 }
282#else
283 /*localhost mask */
284 for (pIPAddr = &FixedInfo->DnsServerList; pIPAddr != NULL; pIPAddr = pIPAddr->Next)
285 {
286 struct dns_entry *da;
287 if(!inet_aton(pIPAddr->IpAddress.String, &tmp_addr))
288 continue;
289 da = RTMemAllocZ(sizeof (struct dns_entry));
290 if (da == NULL)
291 {
292 LogRel(("can't alloc memory for DNS entry\n"));
293 return -1;
294 }
295 /*check */
296 if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
297 da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
298 }
299 LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
300 }
301#endif
302 if (FixedInfo)
303 {
304 GlobalFree(FixedInfo);
305 FixedInfo = NULL;
306 }
307
308get_dns_prefix:
309 if (ppszDomain)
310 {
311 OSVERSIONINFO ver;
312 char szDnsDomain[256];
313 DWORD dwSize = sizeof(szDnsDomain);
314
315 *ppszDomain = NULL;
316 GetVersionEx(&ver);
317 if (ver.dwMajorVersion >= 5)
318 {
319 /* GetComputerNameEx exists in Windows versions starting with 2000. */
320 if (GetComputerNameEx(ComputerNameDnsDomain, szDnsDomain, &dwSize))
321 {
322 if (szDnsDomain[0])
323 {
324 /* Just non-empty strings are valid. */
325 *ppszDomain = RTStrDup(szDnsDomain);
326 if (pData->fPassDomain)
327 {
328 if (fVerbose)
329 LogRel(("NAT: passing domain name %s\n", szDnsDomain));
330 }
331 else
332 Log(("nat: ignoring domain %s\n", szDnsDomain));
333 }
334 }
335 else
336 Log(("nat: GetComputerNameEx failed (%d)\n", GetLastError()));
337 }
338 }
339 return rc;
340}
341
342#else
343
344static int get_dns_addr_domain(PNATState pData, bool fVerbose,
345 struct in_addr *pdns_addr,
346 const char **ppszDomain)
347{
348 char buff[512];
349 char buff2[256];
350 FILE *f;
351 int found = 0;
352 struct in_addr tmp_addr;
353
354#ifdef RT_OS_OS2
355 /* Try various locations. */
356 char *etc = getenv("ETC");
357 f = NULL;
358 if (etc)
359 {
360 snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
361 f = fopen(buff, "rt");
362 }
363 if (!f)
364 {
365 snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
366 f = fopen(buff, "rt");
367 }
368 if (!f)
369 {
370 snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
371 f = fopen(buff, "rt");
372 }
373#else
374 f = fopen("/etc/resolv.conf", "r");
375#endif
376 if (!f)
377 return -1;
378
379 if (ppszDomain)
380 *ppszDomain = NULL;
381 Log(("nat: DNS Servers:\n"));
382 while (fgets(buff, 512, f) != NULL)
383 {
384#ifdef VBOX_WITH_MULTI_DNS
385 struct dns_entry *da = NULL;
386#endif
387 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
388 {
389 if (!inet_aton(buff2, &tmp_addr))
390 continue;
391#ifndef VBOX_WITH_MULTI_DNS
392 /* If it's the first one, set it to dns_addr */
393 if (!found)
394 {
395 if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
396 LogRel(("NAT: DNS address: %s\n", buff2));
397 *pdns_addr = tmp_addr;
398 }
399 else
400 {
401 if (fVerbose)
402 LogRel(("NAT: ignored DNS address: %s\n", buff2));
403 }
404#else
405 /*localhost mask */
406 da = RTMemAllocZ(sizeof (struct dns_entry));
407 if (da == NULL)
408 {
409 LogRel(("can't alloc memory for DNS entry\n"));
410 return -1;
411 }
412 /*check */
413 da->de_addr.s_addr = tmp_addr.s_addr;
414 if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
415 da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
416 }
417 LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
418#endif
419 found++;
420 }
421 if ( ppszDomain
422 && (!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
423 {
424 /* Domain name/search list present. Pick first entry */
425 if (*ppszDomain == NULL)
426 {
427 char *tok;
428 char *saveptr;
429 tok = strtok_r(&buff[6], " \t\n", &saveptr);
430 if (tok)
431 {
432 *ppszDomain = RTStrDup(tok);
433 if (pData->fPassDomain)
434 {
435 if (fVerbose)
436 LogRel(("NAT: passing domain name %s\n", tok));
437 }
438 else
439 Log(("nat: ignoring domain %s\n", tok));
440 }
441 }
442 }
443 }
444 fclose(f);
445 if (!found)
446 return -1;
447 return 0;
448}
449
450#endif
451
452int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
453{
454 return get_dns_addr_domain(pData, false, pdns_addr, NULL);
455}
456
457int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
458 bool fPassDomain, const char *pszTFTPPrefix,
459 const char *pszBootFile, void *pvUser)
460{
461 int fNATfailed = 0;
462 int rc;
463 PNATState pData = RTMemAllocZ(sizeof(NATState));
464 *ppData = pData;
465 if (!pData)
466 return VERR_NO_MEMORY;
467 if (u32Netmask & 0x1f)
468 /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
469 return VERR_INVALID_PARAMETER;
470 pData->fPassDomain = fPassDomain;
471 pData->pvUser = pvUser;
472 tftp_prefix = pszTFTPPrefix;
473 bootp_filename = pszBootFile;
474 pData->netmask = u32Netmask;
475
476#ifdef RT_OS_WINDOWS
477 {
478 WSADATA Data;
479 WSAStartup(MAKEWORD(2,0), &Data);
480 }
481# if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
482 pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
483# endif
484#endif
485#ifdef VBOX_WITH_SLIRP_MT
486 QSOCKET_LOCK_CREATE(tcb);
487 QSOCKET_LOCK_CREATE(udb);
488 rc = RTReqCreateQueue(&pData->pReqQueue);
489 AssertReleaseRC(rc);
490#endif
491
492 link_up = 1;
493
494 debug_init();
495 if_init(pData);
496 ip_init(pData);
497 icmp_init(pData);
498
499 /* Initialise mbufs *after* setting the MTU */
500 m_init(pData);
501
502 inet_aton(pszNetAddr, &special_addr);
503 alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
504 /* @todo: add ability to configure this staff */
505 pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
506
507 /* set default addresses */
508 inet_aton("127.0.0.1", &loopback_addr);
509#ifndef VBOX_WITH_MULTI_DNS
510 inet_aton("127.0.0.1", &dns_addr);
511
512 if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
513#else
514 LIST_INIT(&pData->dns_list_head);
515 if (get_dns_addr_domain(pData, true, NULL, &pData->pszDomain) < 0)
516#endif
517 fNATfailed = 1;
518
519 getouraddr(pData);
520 return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
521}
522
523/**
524 * Statistics counters.
525 */
526void slirp_register_timers(PNATState pData, PPDMDRVINS pDrvIns)
527{
528#ifdef VBOX_WITH_STATISTICS
529 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFill, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
530 STAMUNIT_TICKS_PER_CALL, "Profiling slirp fills", "/Drivers/NAT%d/Fill", pDrvIns->iInstance);
531 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPoll, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
532 STAMUNIT_TICKS_PER_CALL, "Profiling slirp polls", "/Drivers/NAT%d/Poll", pDrvIns->iInstance);
533 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFastTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
534 STAMUNIT_TICKS_PER_CALL, "Profiling slirp fast timer", "/Drivers/NAT%d/TimerFast", pDrvIns->iInstance);
535 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatSlowTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
536 STAMUNIT_TICKS_PER_CALL, "Profiling slirp slow timer", "/Drivers/NAT%d/TimerSlow", pDrvIns->iInstance);
537 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
538 STAMUNIT_COUNT, "TCP sockets", "/Drivers/NAT%d/SockTCP", pDrvIns->iInstance);
539 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
540 STAMUNIT_COUNT, "TCP sockets active", "/Drivers/NAT%d/SockTCPHot", pDrvIns->iInstance);
541 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
542 STAMUNIT_COUNT, "UDP sockets", "/Drivers/NAT%d/SockUDP", pDrvIns->iInstance);
543 PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
544 STAMUNIT_COUNT, "UDP sockets active", "/Drivers/NAT%d/SockUDPHot", pDrvIns->iInstance);
545#endif /* VBOX_WITH_STATISTICS */
546}
547
548/**
549 * Marks the link as up, making it possible to establish new connections.
550 */
551void slirp_link_up(PNATState pData)
552{
553 link_up = 1;
554}
555
556/**
557 * Marks the link as down and cleans up the current connections.
558 */
559void slirp_link_down(PNATState pData)
560{
561 struct socket *so;
562
563 while ((so = tcb.so_next) != &tcb)
564 {
565 if (so->so_state & SS_NOFDREF || so->s == -1)
566 sofree(pData, so);
567 else
568 tcp_drop(pData, sototcpcb(so), 0);
569 }
570
571 while ((so = udb.so_next) != &udb)
572 udp_detach(pData, so);
573
574 link_up = 0;
575}
576
577/**
578 * Terminates the slirp component.
579 */
580void slirp_term(PNATState pData)
581{
582#ifdef VBOX_WITH_MULTI_DNS
583 struct dns_entry *de = NULL;
584#endif
585 if (pData->pszDomain)
586 RTStrFree((char *)(void *)pData->pszDomain);
587
588#ifdef RT_OS_WINDOWS
589 pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
590 FreeLibrary(pData->hmIcmpLibrary);
591 RTMemFree(pData->pvIcmpBuffer);
592# else
593 closesocket(pData->icmp_socket.s);
594#endif
595
596 slirp_link_down(pData);
597#ifdef VBOX_WITH_MULTI_DNS
598 while(!LIST_EMPTY(&pData->dns_list_head)) {
599 de = LIST_FIRST(&pData->dns_list_head);
600 LIST_REMOVE(de, de_list);
601 RTMemFree(de);
602 }
603#endif
604#ifdef RT_OS_WINDOWS
605 WSACleanup();
606#endif
607#ifdef LOG_ENABLED
608 Log(("\n"
609 "NAT statistics\n"
610 "--------------\n"
611 "\n"));
612 ipstats(pData);
613 tcpstats(pData);
614 udpstats(pData);
615 icmpstats(pData);
616 mbufstats(pData);
617 sockstats(pData);
618 Log(("\n"
619 "\n"
620 "\n"));
621#endif
622 RTMemFree(pData);
623}
624
625
626#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
627#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
628#define UPD_NFDS(x) do { if (nfds < (x)) nfds = (x); } while (0)
629
630/*
631 * curtime kept to an accuracy of 1ms
632 */
633static void updtime(PNATState pData)
634{
635#ifdef RT_OS_WINDOWS
636 struct _timeb tb;
637
638 _ftime(&tb);
639 curtime = (u_int)tb.time * (u_int)1000;
640 curtime += (u_int)tb.millitm;
641#else
642 gettimeofday(&tt, 0);
643
644 curtime = (u_int)tt.tv_sec * (u_int)1000;
645 curtime += (u_int)tt.tv_usec / (u_int)1000;
646
647 if ((tt.tv_usec % 1000) >= 500)
648 curtime++;
649#endif
650}
651
652#ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
653void slirp_select_fill(PNATState pData, int *pnfds,
654 fd_set *readfds, fd_set *writefds, fd_set *xfds)
655#else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
656# ifdef RT_OS_WINDOWS
657void slirp_select_fill(PNATState pData, int *pnfds)
658# else /* RT_OS_WINDOWS */
659void slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
660# endif /* !RT_OS_WINDOWS */
661#endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
662{
663 struct socket *so, *so_next;
664 int nfds;
665#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
666 int rc;
667 int error;
668#endif
669#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
670 int poll_index = 0;
671#endif
672 int i;
673
674 STAM_PROFILE_START(&pData->StatFill, a);
675
676 nfds = *pnfds;
677
678 /*
679 * First, TCP sockets
680 */
681 do_slowtimo = 0;
682 if (link_up)
683 {
684 /*
685 * *_slowtimo needs calling if there are IP fragments
686 * in the fragment queue, or there are TCP connections active
687 */
688 /* XXX:
689 * triggering of fragment expiration should be the same but use new macroses
690 */
691 do_slowtimo = (tcb.so_next != &tcb);
692 if (!do_slowtimo)
693 {
694 for (i = 0; i < IPREASS_NHASH; i++)
695 {
696 if (!TAILQ_EMPTY(&ipq[i]))
697 {
698 do_slowtimo = 1;
699 break;
700 }
701 }
702 }
703 ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
704
705 STAM_COUNTER_RESET(&pData->StatTCP);
706 STAM_COUNTER_RESET(&pData->StatTCPHot);
707
708 QSOCKET_FOREACH(so, so_next, tcp)
709 /* { */
710#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
711 so->so_poll_index = -1;
712#endif
713 STAM_COUNTER_INC(&pData->StatTCP);
714
715 /*
716 * See if we need a tcp_fasttimo
717 */
718 if ( time_fasttimo == 0
719 && so->so_tcpcb != NULL
720 && so->so_tcpcb->t_flags & TF_DELACK)
721 time_fasttimo = curtime; /* Flag when we want a fasttimo */
722
723 /*
724 * NOFDREF can include still connecting to local-host,
725 * newly socreated() sockets etc. Don't want to select these.
726 */
727 if (so->so_state & SS_NOFDREF || so->s == -1)
728 CONTINUE(tcp);
729
730 /*
731 * Set for reading sockets which are accepting
732 */
733 if (so->so_state & SS_FACCEPTCONN)
734 {
735 STAM_COUNTER_INC(&pData->StatTCPHot);
736 TCP_ENGAGE_EVENT1(so, readfds);
737 CONTINUE(tcp);
738 }
739
740 /*
741 * Set for writing sockets which are connecting
742 */
743 if (so->so_state & SS_ISFCONNECTING)
744 {
745 Log2(("connecting %R[natsock] engaged\n",so));
746 STAM_COUNTER_INC(&pData->StatTCPHot);
747 TCP_ENGAGE_EVENT1(so, writefds);
748 }
749
750 /*
751 * Set for writing if we are connected, can send more, and
752 * we have something to send
753 */
754 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
755 {
756 STAM_COUNTER_INC(&pData->StatTCPHot);
757 TCP_ENGAGE_EVENT1(so, writefds);
758 }
759
760 /*
761 * Set for reading (and urgent data) if we are connected, can
762 * receive more, and we have room for it XXX /2 ?
763 */
764 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
765 {
766 STAM_COUNTER_INC(&pData->StatTCPHot);
767 TCP_ENGAGE_EVENT2(so, readfds, xfds);
768 }
769 LOOP_LABEL(tcp, so, so_next);
770 }
771
772 /*
773 * UDP sockets
774 */
775 STAM_COUNTER_RESET(&pData->StatUDP);
776 STAM_COUNTER_RESET(&pData->StatUDPHot);
777
778 QSOCKET_FOREACH(so, so_next, udp)
779 /* { */
780
781 STAM_COUNTER_INC(&pData->StatUDP);
782#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
783 so->so_poll_index = -1;
784#endif
785
786 /*
787 * See if it's timed out
788 */
789 if (so->so_expire)
790 {
791 if (so->so_expire <= curtime)
792 {
793#ifdef VBOX_WITH_SLIRP_MT
794 /* we need so_next for continue our cycle*/
795 so_next = so->so_next;
796#endif
797 UDP_DETACH(pData, so, so_next);
798 CONTINUE_NO_UNLOCK(udp);
799 }
800 else
801 do_slowtimo = 1; /* Let socket expire */
802 }
803
804 /*
805 * When UDP packets are received from over the link, they're
806 * sendto()'d straight away, so no need for setting for writing
807 * Limit the number of packets queued by this session to 4.
808 * Note that even though we try and limit this to 4 packets,
809 * the session could have more queued if the packets needed
810 * to be fragmented.
811 *
812 * (XXX <= 4 ?)
813 */
814 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
815 {
816 STAM_COUNTER_INC(&pData->StatUDPHot);
817 UDP_ENGAGE_EVENT(so, readfds);
818 }
819 LOOP_LABEL(udp, so, so_next);
820 }
821
822 }
823
824#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
825# if defined(RT_OS_WINDOWS)
826 *pnfds = VBOX_EVENT_COUNT;
827# else /* RT_OS_WINDOWS */
828 AssertRelease(poll_index <= *pnfds);
829 *pnfds = poll_index;
830# endif /* !RT_OS_WINDOWS */
831#else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
832 *pnfds = nfds;
833#endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
834
835 STAM_PROFILE_STOP(&pData->StatFill, a);
836}
837
838#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
839# if defined(RT_OS_WINDOWS)
840void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
841# else /* RT_OS_WINDOWS */
842void slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
843# endif /* !RT_OS_WINDOWS */
844#else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
845void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
846#endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
847{
848 struct socket *so, *so_next;
849 int ret;
850#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
851 WSANETWORKEVENTS NetworkEvents;
852 int rc;
853 int error;
854#endif
855#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
856 int poll_index = 0;
857#endif
858
859 STAM_PROFILE_START(&pData->StatPoll, a);
860
861 /* Update time */
862 updtime(pData);
863
864 /*
865 * See if anything has timed out
866 */
867 if (link_up)
868 {
869 if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
870 {
871 STAM_PROFILE_START(&pData->StatFastTimer, a);
872 tcp_fasttimo(pData);
873 time_fasttimo = 0;
874 STAM_PROFILE_STOP(&pData->StatFastTimer, a);
875 }
876 if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
877 {
878 STAM_PROFILE_START(&pData->StatSlowTimer, a);
879 ip_slowtimo(pData);
880 tcp_slowtimo(pData);
881 last_slowtimo = curtime;
882 STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
883 }
884 }
885#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
886 if (fTimeout)
887 return; /* only timer update */
888#endif
889
890 /*
891 * Check sockets
892 */
893 if (link_up)
894 {
895#if defined(RT_OS_WINDOWS)
896 /*XXX: before renaming please make see define
897 * fIcmp in slirp_state.h
898 */
899 if (fIcmp)
900 sorecvfrom(pData, &pData->icmp_socket);
901#else
902 if ( (pData->icmp_socket.s != -1)
903 && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
904 sorecvfrom(pData, &pData->icmp_socket);
905#endif
906 /*
907 * Check TCP sockets
908 */
909 QSOCKET_FOREACH(so, so_next, tcp)
910 /* { */
911
912#ifdef VBOX_WITH_SLIRP_MT
913 if ( so->so_state & SS_NOFDREF
914 && so->so_deleted == 1)
915 {
916 struct socket *son, *sop = NULL;
917 QSOCKET_LOCK(tcb);
918 if (so->so_next != NULL)
919 {
920 if (so->so_next != &tcb)
921 SOCKET_LOCK(so->so_next);
922 son = so->so_next;
923 }
924 if ( so->so_prev != &tcb
925 && so->so_prev != NULL)
926 {
927 SOCKET_LOCK(so->so_prev);
928 sop = so->so_prev;
929 }
930 QSOCKET_UNLOCK(tcb);
931 remque(pData, so);
932 NSOCK_DEC();
933 SOCKET_UNLOCK(so);
934 SOCKET_LOCK_DESTROY(so);
935 RTMemFree(so);
936 so_next = son;
937 if (sop != NULL)
938 SOCKET_UNLOCK(sop);
939 CONTINUE_NO_UNLOCK(tcp);
940 }
941#endif
942 /*
943 * FD_ISSET is meaningless on these sockets
944 * (and they can crash the program)
945 */
946 if (so->so_state & SS_NOFDREF || so->s == -1)
947 CONTINUE(tcp);
948
949 POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
950
951 LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
952
953
954 /*
955 * Check for URG data
956 * This will soread as well, so no need to
957 * test for readfds below if this succeeds
958 */
959
960 /* out-of-band data */
961 if (CHECK_FD_SET(so, NetworkEvents, xfds))
962 {
963 sorecvoob(pData, so);
964 }
965
966 /*
967 * Check sockets for reading
968 */
969 else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
970 || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
971 {
972 /*
973 * Check for incoming connections
974 */
975 if (so->so_state & SS_FACCEPTCONN)
976 {
977 TCP_CONNECT(pData, so);
978#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
979 if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
980#endif
981 CONTINUE(tcp);
982 }
983
984 ret = soread(pData, so);
985 /* Output it if we read something */
986 if (ret > 0)
987 TCP_OUTPUT(pData, sototcpcb(so));
988 }
989
990#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
991 /*
992 * Check for FD_CLOSE events.
993 * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
994 */
995 if ( (NetworkEvents.lNetworkEvents & FD_CLOSE)
996 || (so->so_close == 1))
997 {
998 so->so_close = 1; /* mark it */
999 /*
1000 * drain the socket
1001 */
1002 for (;;)
1003 {
1004 ret = soread(pData, so);
1005 if (ret > 0)
1006 TCP_OUTPUT(pData, sototcpcb(so));
1007 else
1008 break;
1009 }
1010 CONTINUE(tcp);
1011 }
1012#endif
1013
1014 /*
1015 * Check sockets for writing
1016 */
1017 if (CHECK_FD_SET(so, NetworkEvents, writefds))
1018 {
1019 /*
1020 * Check for non-blocking, still-connecting sockets
1021 */
1022 if (so->so_state & SS_ISFCONNECTING)
1023 {
1024 Log2(("connecting %R[natsock] catched\n", so));
1025 /* Connected */
1026 so->so_state &= ~SS_ISFCONNECTING;
1027
1028 /*
1029 * This should be probably guarded by PROBE_CONN too. Anyway,
1030 * we disable it on OS/2 because the below send call returns
1031 * EFAULT which causes the opened TCP socket to close right
1032 * after it has been opened and connected.
1033 */
1034#ifndef RT_OS_OS2
1035 ret = send(so->s, (const char *)&ret, 0, 0);
1036 if (ret < 0)
1037 {
1038 /* XXXXX Must fix, zero bytes is a NOP */
1039 if ( errno == EAGAIN
1040 || errno == EWOULDBLOCK
1041 || errno == EINPROGRESS
1042 || errno == ENOTCONN)
1043 CONTINUE(tcp);
1044
1045 /* else failed */
1046 so->so_state = SS_NOFDREF;
1047 }
1048 /* else so->so_state &= ~SS_ISFCONNECTING; */
1049#endif
1050
1051 /*
1052 * Continue tcp_input
1053 */
1054 TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
1055 /* continue; */
1056 }
1057 else
1058 SOWRITE(ret, pData, so);
1059 /*
1060 * XXX If we wrote something (a lot), there could be the need
1061 * for a window update. In the worst case, the remote will send
1062 * a window probe to get things going again.
1063 */
1064 }
1065
1066 /*
1067 * Probe a still-connecting, non-blocking socket
1068 * to check if it's still alive
1069 */
1070#ifdef PROBE_CONN
1071 if (so->so_state & SS_ISFCONNECTING)
1072 {
1073 ret = recv(so->s, (char *)&ret, 0, 0);
1074
1075 if (ret < 0)
1076 {
1077 /* XXX */
1078 if ( errno == EAGAIN
1079 || errno == EWOULDBLOCK
1080 || errno == EINPROGRESS
1081 || errno == ENOTCONN)
1082 {
1083 CONTINUE(tcp); /* Still connecting, continue */
1084 }
1085
1086 /* else failed */
1087 so->so_state = SS_NOFDREF;
1088
1089 /* tcp_input will take care of it */
1090 }
1091 else
1092 {
1093 ret = send(so->s, &ret, 0, 0);
1094 if (ret < 0)
1095 {
1096 /* XXX */
1097 if ( errno == EAGAIN
1098 || errno == EWOULDBLOCK
1099 || errno == EINPROGRESS
1100 || errno == ENOTCONN)
1101 {
1102 CONTINUE(tcp);
1103 }
1104 /* else failed */
1105 so->so_state = SS_NOFDREF;
1106 }
1107 else
1108 so->so_state &= ~SS_ISFCONNECTING;
1109
1110 }
1111 TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
1112 } /* SS_ISFCONNECTING */
1113#endif
1114#ifndef RT_OS_WINDOWS
1115 if ( UNIX_CHECK_FD_SET(so, NetworkEvents, rdhup)
1116 || UNIX_CHECK_FD_SET(so, NetworkEvents, rderr))
1117 {
1118 int err;
1119 int inq = -1, outq;
1120 socklen_t optlen = sizeof(int);
1121 int status = getsockopt(so->s, SOL_SOCKET, SO_ERROR, &err, &optlen);
1122 if (status != 0)
1123 LogRel(("NAT: can't get error status from %R[natsock]\n", so));
1124#ifndef RT_OS_SOLARIS
1125 status = ioctl(so->s, FIONREAD, &inq); /* tcp(7) recommends SIOCINQ which is Linux specific */
1126 if (status != 0 || status != EINVAL)
1127 {
1128 /* EINVAL returned if socket in listen state tcp(7)*/
1129 LogRel(("NAT: can't get depth of IN queue status from %R[natsock]\n", so));
1130 }
1131 status = ioctl(so->s, TIOCOUTQ, &outq); /* SIOCOUTQ see previous comment */
1132 if (status != 0)
1133 LogRel(("NAT: can't get depth of OUT queue from %R[natsock]\n", so));
1134#else
1135 inq = outq = -1;
1136 /*
1137 * Solaris has bit different ioctl commands and its handlings
1138 * hint: streamio(7) I_NREAD
1139 */
1140#endif
1141 if ( so->so_state & SS_ISFCONNECTING
1142 || UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
1143 {
1144 /**
1145 * Check if we need here take care about gracefull connection
1146 * @todo try with proxy server
1147 */
1148 if (UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
1149 {
1150 /*
1151 * Never meet inq != 0 or outq != 0, anyway let it stay for a while
1152 * in case it happens we'll able to detect it.
1153 * Give TCP/IP stack wait or expire the socket.
1154 */
1155 LogRel(("NAT: %R[natsock] err(%d:%s) s(in:%d,out:%d)happens on read I/O, "
1156 "other side close connection \n", so, err, strerror(err), inq, outq));
1157 CONTINUE(tcp);
1158 }
1159 goto tcp_input_close;
1160 }
1161 if ( !UNIX_CHECK_FD_SET(so, NetworkEvents, readfds)
1162 && !UNIX_CHECK_FD_SET(so, NetworkEvents, writefds)
1163 && !UNIX_CHECK_FD_SET(so, NetworkEvents, xfds))
1164 {
1165 LogRel(("NAT: system expires the socket %R[natsock] err(%d:%s) s(in:%d,out:%d) happens on non-I/O. ",
1166 so, err, strerror(err), inq, outq));
1167 goto tcp_input_close;
1168 }
1169 LogRel(("NAT: %R[natsock] we've met(%d:%s) s(in:%d, out:%d) unhandled combination hup (%d) "
1170 "rederr(%d) on (r:%d, w:%d, x:%d)\n",
1171 so, err, strerror(err),
1172 inq, outq,
1173 UNIX_CHECK_FD_SET(so, ign, rdhup),
1174 UNIX_CHECK_FD_SET(so, ign, rderr),
1175 UNIX_CHECK_FD_SET(so, ign, readfds),
1176 UNIX_CHECK_FD_SET(so, ign, writefds),
1177 UNIX_CHECK_FD_SET(so, ign, xfds)));
1178 /*
1179 * Give OS's TCP/IP stack a chance to resolve an issue or expire the socket.
1180 */
1181 CONTINUE(tcp);
1182tcp_input_close:
1183 so->so_state = SS_NOFDREF; /*cause connection valid tcp connection termination and socket closing */
1184 TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
1185 CONTINUE(tcp);
1186 }
1187#endif
1188 LOOP_LABEL(tcp, so, so_next);
1189 }
1190
1191 /*
1192 * Now UDP sockets.
1193 * Incoming packets are sent straight away, they're not buffered.
1194 * Incoming UDP data isn't buffered either.
1195 */
1196 QSOCKET_FOREACH(so, so_next, udp)
1197 /* { */
1198#ifdef VBOX_WITH_SLIRP_MT
1199 if ( so->so_state & SS_NOFDREF
1200 && so->so_deleted == 1)
1201 {
1202 struct socket *son, *sop = NULL;
1203 QSOCKET_LOCK(udb);
1204 if (so->so_next != NULL)
1205 {
1206 if (so->so_next != &udb)
1207 SOCKET_LOCK(so->so_next);
1208 son = so->so_next;
1209 }
1210 if ( so->so_prev != &udb
1211 && so->so_prev != NULL)
1212 {
1213 SOCKET_LOCK(so->so_prev);
1214 sop = so->so_prev;
1215 }
1216 QSOCKET_UNLOCK(udb);
1217 remque(pData, so);
1218 NSOCK_DEC();
1219 SOCKET_UNLOCK(so);
1220 SOCKET_LOCK_DESTROY(so);
1221 RTMemFree(so);
1222 so_next = son;
1223 if (sop != NULL)
1224 SOCKET_UNLOCK(sop);
1225 CONTINUE_NO_UNLOCK(udp);
1226 }
1227#endif
1228 POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
1229
1230 LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
1231
1232 if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
1233 {
1234 SORECVFROM(pData, so);
1235 }
1236 LOOP_LABEL(udp, so, so_next);
1237 }
1238
1239 }
1240
1241#ifndef VBOX_WITH_SLIRP_MT
1242 /*
1243 * See if we can start outputting
1244 */
1245 if (if_queued && link_up)
1246 if_start(pData);
1247#endif
1248
1249 STAM_PROFILE_STOP(&pData->StatPoll, a);
1250}
1251
1252#define ETH_ALEN 6
1253#define ETH_HLEN 14
1254
1255#define ARPOP_REQUEST 1 /* ARP request */
1256#define ARPOP_REPLY 2 /* ARP reply */
1257
1258struct ethhdr
1259{
1260 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
1261 unsigned char h_source[ETH_ALEN]; /* source ether addr */
1262 unsigned short h_proto; /* packet type ID field */
1263};
1264AssertCompileSize(struct ethhdr, 14);
1265
1266struct arphdr
1267{
1268 unsigned short ar_hrd; /* format of hardware address */
1269 unsigned short ar_pro; /* format of protocol address */
1270 unsigned char ar_hln; /* length of hardware address */
1271 unsigned char ar_pln; /* length of protocol address */
1272 unsigned short ar_op; /* ARP opcode (command) */
1273
1274 /*
1275 * Ethernet looks like this : This bit is variable sized however...
1276 */
1277 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
1278 unsigned char ar_sip[4]; /* sender IP address */
1279 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
1280 unsigned char ar_tip[4]; /* target IP address */
1281};
1282AssertCompileSize(struct arphdr, 28);
1283
1284#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1285static void arp_input(PNATState pData, struct mbuf *m)
1286#else
1287static void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
1288#endif
1289{
1290 struct ethhdr *eh;
1291 struct ethhdr *reh;
1292 struct arphdr *ah;
1293 struct arphdr *rah;
1294 int ar_op;
1295 struct ex_list *ex_ptr;
1296 uint32_t htip;
1297#ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1298 uint8_t arp_reply[sizeof(struct arphdr) + ETH_HLEN];
1299 eh = (struct ethhdr *)pkt;
1300#else
1301 struct mbuf *mr;
1302 eh = mtod(m, struct ethhdr *);
1303#endif
1304 ah = (struct arphdr *)&eh[1];
1305 htip = ntohl(*(uint32_t*)ah->ar_tip);
1306
1307#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1308 mr = m_get(pData);
1309 mr->m_data += if_maxlinkhdr;
1310 mr->m_len = sizeof(struct arphdr);
1311 rah = mtod(mr, struct arphdr *);
1312#else
1313 reh = (struct ethhdr *)arp_reply;
1314 rah = (struct arphdr *)&reh[1];
1315#endif
1316
1317 ar_op = ntohs(ah->ar_op);
1318 switch(ar_op)
1319 {
1320 case ARPOP_REQUEST:
1321 if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
1322 {
1323 if ( CTL_CHECK(htip,CTL_DNS)
1324 || CTL_CHECK(htip, CTL_ALIAS)
1325 || CTL_CHECK(htip, CTL_TFTP))
1326 goto arp_ok;
1327 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1328 {
1329 if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
1330 goto arp_ok;
1331 }
1332 return;
1333 arp_ok:
1334
1335#ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1336 memcpy(reh->h_dest, eh->h_source, ETH_ALEN);
1337 memcpy(reh->h_source, &special_addr, ETH_ALEN);
1338 reh->h_source[5] = ah->ar_tip[3];
1339 reh->h_proto = htons(ETH_P_ARP);
1340#endif
1341 rah->ar_hrd = htons(1);
1342 rah->ar_pro = htons(ETH_P_IP);
1343 rah->ar_hln = ETH_ALEN;
1344 rah->ar_pln = 4;
1345 rah->ar_op = htons(ARPOP_REPLY);
1346 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
1347
1348 switch (htip & ~pData->netmask)
1349 {
1350 case CTL_DNS:
1351 case CTL_ALIAS:
1352 rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
1353 break;
1354 default:;
1355 }
1356
1357 memcpy(rah->ar_sip, ah->ar_tip, 4);
1358 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
1359 memcpy(rah->ar_tip, ah->ar_sip, 4);
1360#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1361 if_encap(pData, ETH_P_ARP, mr);
1362 m_free(pData, m);
1363#else
1364 slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
1365#endif
1366 }
1367 break;
1368 default:
1369 break;
1370 }
1371}
1372
1373void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
1374{
1375 struct mbuf *m;
1376 int proto;
1377 static bool fWarnedIpv6;
1378
1379 if (pkt_len < ETH_HLEN)
1380 {
1381 LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
1382 return;
1383 }
1384
1385 m = m_get(pData);
1386 if (!m)
1387 {
1388 LogRel(("NAT: can't allocate new mbuf\n"));
1389 return;
1390 }
1391
1392 /* Note: we add to align the IP header */
1393
1394 if (M_FREEROOM(m) < pkt_len)
1395 m_inc(m, pkt_len);
1396
1397 m->m_len = pkt_len ;
1398 memcpy(m->m_data, pkt, pkt_len);
1399
1400 proto = ntohs(*(uint16_t *)(pkt + 12));
1401 switch(proto)
1402 {
1403 case ETH_P_ARP:
1404#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1405 arp_input(pData, m);
1406#else
1407 arp_input(pData, pkt, pkt_len);
1408 m_free(pData, m);
1409#endif
1410 break;
1411 case ETH_P_IP:
1412 /* Update time. Important if the network is very quiet, as otherwise
1413 * the first outgoing connection gets an incorrect timestamp. */
1414 updtime(pData);
1415 m->m_data += ETH_HLEN;
1416 m->m_len -= ETH_HLEN;
1417 ip_input(pData, m);
1418 break;
1419 case ETH_P_IPV6:
1420 m_free(pData, m);
1421 if (!fWarnedIpv6)
1422 {
1423 LogRel(("NAT: IPv6 not supported\n"));
1424 fWarnedIpv6 = true;
1425 }
1426 break;
1427 default:
1428 LogRel(("NAT: Unsupported protocol %x\n", proto));
1429 m_free(pData, m);
1430 break;
1431 }
1432 RTMemFree((void *)pkt);
1433}
1434
1435/* output the IP packet to the ethernet device */
1436#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1437void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
1438#else
1439void if_encap(PNATState pData, uint8_t *ip_data, int ip_data_len)
1440#endif
1441{
1442#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1443 struct ethhdr *eh;
1444 uint8_t *buf = RTMemAlloc(1600);
1445 m->m_data -= if_maxlinkhdr;
1446 m->m_len += ETH_HLEN;
1447 eh = mtod(m, struct ethhdr *);
1448#else
1449 uint8_t buf[1600];
1450 struct ethhdr *eh = (struct ethhdr *)buf;
1451
1452 if (ip_data_len + ETH_HLEN > sizeof(buf))
1453 return;
1454
1455 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
1456#endif
1457
1458
1459 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
1460 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
1461 /* XXX: not correct */
1462 eh->h_source[5] = CTL_ALIAS;
1463#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
1464 eh->h_proto = htons(eth_proto);
1465#if 0
1466 slirp_output(pData->pvUser, m, mtod(m, uint8_t *), m->m_len);
1467#else
1468 memcpy(buf, mtod(m, uint8_t *), m->m_len);
1469 slirp_output(pData->pvUser, NULL, buf, m->m_len);
1470 m_free(pData, m);
1471#endif
1472#else
1473 eh->h_proto = htons(ETH_P_IP);
1474 slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
1475#endif
1476}
1477
1478int slirp_redir(PNATState pData, int is_udp, int host_port,
1479 struct in_addr guest_addr, int guest_port)
1480{
1481 if (is_udp)
1482 {
1483 if (!udp_listen(pData, htons(host_port), guest_addr.s_addr,
1484 htons(guest_port), 0))
1485 return -1;
1486 }
1487 else
1488 {
1489 if (!solisten(pData, htons(host_port), guest_addr.s_addr,
1490 htons(guest_port), 0))
1491 return -1;
1492 }
1493 return 0;
1494}
1495
1496int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
1497 int guest_port)
1498{
1499 return add_exec(&exec_list, do_pty, (char *)args,
1500 addr_low_byte, htons(guest_port));
1501}
1502
1503void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
1504{
1505 memcpy(client_ethaddr, ethaddr, ETH_ALEN);
1506}
1507
1508#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
1509HANDLE *slirp_get_events(PNATState pData)
1510{
1511 return pData->phEvents;
1512}
1513void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
1514{
1515 pData->phEvents[index] = hEvent;
1516}
1517#endif
1518
1519unsigned int slirp_get_timeout_ms(PNATState pData)
1520{
1521 if (link_up)
1522 {
1523 if (time_fasttimo)
1524 return 2;
1525 if (do_slowtimo)
1526 return 500; /* see PR_SLOWHZ */
1527 }
1528 return 0;
1529}
1530
1531#ifndef RT_OS_WINDOWS
1532int slirp_get_nsock(PNATState pData)
1533{
1534 return pData->nsock;
1535}
1536#endif
1537
1538/*
1539 * this function called from NAT thread
1540 */
1541void slirp_post_sent(PNATState pData, void *pvArg)
1542{
1543 struct socket *so = 0;
1544 struct tcpcb *tp = 0;
1545 struct mbuf *m = (struct mbuf *)pvArg;
1546 m_free(pData, m);
1547}
1548#ifdef VBOX_WITH_SLIRP_MT
1549void slirp_process_queue(PNATState pData)
1550{
1551 RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
1552}
1553void *slirp_get_queue(PNATState pData)
1554{
1555 return pData->pReqQueue;
1556}
1557#endif
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