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