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