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