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