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