1 | #include "slirp.h"
|
---|
2 | #ifdef RT_OS_OS2
|
---|
3 | # include <paths.h>
|
---|
4 | #endif
|
---|
5 |
|
---|
6 | #include <VBox/err.h>
|
---|
7 | #include <VBox/pdmdrv.h>
|
---|
8 | #include <iprt/assert.h>
|
---|
9 | #ifndef RT_OS_WINDOWS
|
---|
10 | #include <sys/ioctl.h>
|
---|
11 | #include <poll.h>
|
---|
12 | #endif
|
---|
13 |
|
---|
14 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) || !defined(RT_OS_WINDOWS)
|
---|
15 |
|
---|
16 | # ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
17 | # define DO_ENGAGE_EVENT1(so, fdset, label) \
|
---|
18 | do { \
|
---|
19 | FD_SET((so)->s, (fdset)); \
|
---|
20 | UPD_NFDS((so)->s); \
|
---|
21 | } while(0)
|
---|
22 |
|
---|
23 |
|
---|
24 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
25 | do { \
|
---|
26 | FD_SET((so)->s, (fdset1)); \
|
---|
27 | FD_SET((so)->s, (fdset2)); \
|
---|
28 | UPD_NFDS((so)->s); \
|
---|
29 | } while(0)
|
---|
30 |
|
---|
31 | # define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
|
---|
32 |
|
---|
33 | # define DO_CHECK_FD_SET(so, events, fdset) (FD_ISSET((so)->s, fdset))
|
---|
34 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 0 /*specific for Unix API */
|
---|
35 | # else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
36 | # define DO_ENGAGE_EVENT1(so, fdset, label) \
|
---|
37 | do { \
|
---|
38 | if( so->so_poll_index != -1 \
|
---|
39 | && so->s == polls[so->so_poll_index].fd) { \
|
---|
40 | polls[so->so_poll_index].events |= N_(fdset ## _poll); \
|
---|
41 | break; /* out of this loop */ \
|
---|
42 | } \
|
---|
43 | AssertRelease(poll_index < (nfds)); \
|
---|
44 | AssertRelease(poll_index >= 0 && poll_index < (nfds)); \
|
---|
45 | polls[poll_index].fd = (so)->s; \
|
---|
46 | (so)->so_poll_index = poll_index; \
|
---|
47 | polls[poll_index].events = N_(fdset ## _poll); \
|
---|
48 | polls[poll_index].revents = 0; \
|
---|
49 | poll_index++; \
|
---|
50 | } while(0)
|
---|
51 |
|
---|
52 |
|
---|
53 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
54 | do { \
|
---|
55 | if( so->so_poll_index != -1 \
|
---|
56 | && so->s == polls[so->so_poll_index].fd) { \
|
---|
57 | polls[so->so_poll_index].events |= \
|
---|
58 | N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
|
---|
59 | break; /* out of this loop */ \
|
---|
60 | } \
|
---|
61 | AssertRelease(poll_index < (nfds)); \
|
---|
62 | polls[poll_index].fd = (so)->s; \
|
---|
63 | (so)->so_poll_index = poll_index; \
|
---|
64 | polls[poll_index].events = \
|
---|
65 | N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
|
---|
66 | poll_index++; \
|
---|
67 | } while(0)
|
---|
68 |
|
---|
69 | # define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
|
---|
70 |
|
---|
71 | # define DO_CHECK_FD_SET(so, events, fdset) ( ((so)->so_poll_index != -1) \
|
---|
72 | && ((so)->so_poll_index <= ndfs) \
|
---|
73 | && ((so)->s == polls[so->so_poll_index].fd) \
|
---|
74 | && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)))
|
---|
75 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
|
---|
76 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
|
---|
77 | # endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
78 |
|
---|
79 | # ifndef RT_OS_WINDOWS
|
---|
80 |
|
---|
81 | # ifndef RT_OS_LINUX
|
---|
82 | # define readfds_poll (POLLRDNORM)
|
---|
83 | # define writefds_poll (POLLWRNORM)
|
---|
84 | # define xfds_poll (POLLRDBAND|POLLWRBAND|POLLPRI)
|
---|
85 | # else
|
---|
86 | # define readfds_poll (POLLIN)
|
---|
87 | # define writefds_poll (POLLOUT)
|
---|
88 | # define xfds_poll (POLLPRI)
|
---|
89 | # endif
|
---|
90 | # define rderr_poll (POLLERR)
|
---|
91 | # define rdhup_poll (POLLHUP)
|
---|
92 | # define nval_poll (POLLNVAL)
|
---|
93 |
|
---|
94 | # define ICMP_ENGAGE_EVENT(so, fdset) \
|
---|
95 | do { \
|
---|
96 | if (pData->icmp_socket.s != -1) \
|
---|
97 | DO_ENGAGE_EVENT1((so), fdset, ICMP); \
|
---|
98 | } while (0)
|
---|
99 | # else /* !RT_OS_WINDOWS */
|
---|
100 | # ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
101 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
|
---|
102 | # else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
103 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0
|
---|
104 | # endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
105 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
106 | #endif /* RT_OS_WINDOWS */
|
---|
107 |
|
---|
108 | #else /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
|
---|
112 | * So no call to WSAEventSelect necessary.
|
---|
113 | */
|
---|
114 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
115 |
|
---|
116 | # define DO_ENGAGE_EVENT1(so, fdset1, label) \
|
---|
117 | do { \
|
---|
118 | rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
|
---|
119 | if (rc == SOCKET_ERROR) \
|
---|
120 | { \
|
---|
121 | /* This should not happen */ \
|
---|
122 | error = WSAGetLastError(); \
|
---|
123 | LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
|
---|
124 | error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
|
---|
125 | } \
|
---|
126 | } while(0); \
|
---|
127 | CONTINUE(label)
|
---|
128 |
|
---|
129 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
130 | DO_ENGAGE_EVENT1((so), (fdset1), label)
|
---|
131 |
|
---|
132 | # define DO_POLL_EVENTS(rc, error, so, events, label) \
|
---|
133 | (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
|
---|
134 | if ((rc) == SOCKET_ERROR) \
|
---|
135 | { \
|
---|
136 | (error) = WSAGetLastError(); \
|
---|
137 | LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
|
---|
138 | CONTINUE(label); \
|
---|
139 | }
|
---|
140 |
|
---|
141 | # define acceptds_win FD_ACCEPT
|
---|
142 | # define acceptds_win_bit FD_ACCEPT_BIT
|
---|
143 |
|
---|
144 | # define readfds_win FD_READ
|
---|
145 | # define readfds_win_bit FD_READ_BIT
|
---|
146 |
|
---|
147 | # define writefds_win FD_WRITE
|
---|
148 | # define writefds_win_bit FD_WRITE_BIT
|
---|
149 |
|
---|
150 | # define xfds_win FD_OOB
|
---|
151 | # define xfds_win_bit FD_OOB_BIT
|
---|
152 |
|
---|
153 | # define DO_CHECK_FD_SET(so, events, fdset) \
|
---|
154 | (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
|
---|
155 |
|
---|
156 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
|
---|
157 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 1 /*specific for Unix API */
|
---|
158 |
|
---|
159 | #endif /* defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS) */
|
---|
160 |
|
---|
161 | #define TCP_ENGAGE_EVENT1(so, fdset) \
|
---|
162 | DO_ENGAGE_EVENT1((so), fdset, tcp)
|
---|
163 |
|
---|
164 | #define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
|
---|
165 | DO_ENGAGE_EVENT2((so), fdset1, fdset2, tcp)
|
---|
166 |
|
---|
167 | #define UDP_ENGAGE_EVENT(so, fdset) \
|
---|
168 | DO_ENGAGE_EVENT1((so), fdset, udp)
|
---|
169 |
|
---|
170 | #define POLL_TCP_EVENTS(rc, error, so, events) \
|
---|
171 | DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
|
---|
172 |
|
---|
173 | #define POLL_UDP_EVENTS(rc, error, so, events) \
|
---|
174 | DO_POLL_EVENTS((rc), (error), (so), (events), udp)
|
---|
175 |
|
---|
176 | #define CHECK_FD_SET(so, events, set) \
|
---|
177 | (DO_CHECK_FD_SET((so), (events), set))
|
---|
178 |
|
---|
179 | #define WIN_CHECK_FD_SET(so, events, set) \
|
---|
180 | (DO_WIN_CHECK_FD_SET((so), (events), set))
|
---|
181 | #define UNIX_CHECK_FD_SET(so, events, set) \
|
---|
182 | (DO_UNIX_CHECK_FD_SET(so, events, set))
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Loging macros
|
---|
186 | */
|
---|
187 | #if VBOX_WITH_DEBUG_NAT_SOCKETS
|
---|
188 | # if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
189 | # if defined(RT_OS_WINDOWS)
|
---|
190 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
191 | do { \
|
---|
192 | LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
|
---|
193 | } while (0)
|
---|
194 | # else /* RT_OS_WINDOWS */
|
---|
195 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
196 | do { \
|
---|
197 | LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
|
---|
198 | CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
|
---|
199 | CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
|
---|
200 | CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
|
---|
201 | CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
|
---|
202 | CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
|
---|
203 | CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
|
---|
204 | } while (0)
|
---|
205 | # endif /* !RT_OS_WINDOWS */
|
---|
206 | # else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
207 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
208 | do { \
|
---|
209 | LogRel((" " #proto " %R[natsock] %s %s %s\n", (so), \
|
---|
210 | FD_ISSET((so)->s, (r_fdset))?"READ":"", \
|
---|
211 | FD_ISSET((so)->s, (w_fdset))?"WRITE":"", \
|
---|
212 | FD_ISSET((so)->s, (x_fdset))?"OOB":"")); \
|
---|
213 | } while (0)
|
---|
214 | # endif
|
---|
215 | #else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
216 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
|
---|
217 | #endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
218 |
|
---|
219 | #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)
|
---|
220 |
|
---|
221 | static const uint8_t special_ethaddr[6] =
|
---|
222 | {
|
---|
223 | 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
|
---|
224 | };
|
---|
225 |
|
---|
226 | #ifdef RT_OS_WINDOWS
|
---|
227 |
|
---|
228 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
229 | struct in_addr *pdns_addr,
|
---|
230 | const char **ppszDomain)
|
---|
231 | {
|
---|
232 | int rc = 0;
|
---|
233 | FIXED_INFO *FixedInfo = NULL;
|
---|
234 | ULONG BufLen;
|
---|
235 | DWORD ret;
|
---|
236 | IP_ADDR_STRING *pIPAddr;
|
---|
237 | struct in_addr tmp_addr;
|
---|
238 |
|
---|
239 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
|
---|
240 | BufLen = sizeof(FIXED_INFO);
|
---|
241 |
|
---|
242 | /** @todo: this API returns all DNS servers, no matter whether the
|
---|
243 | * corresponding network adapter is disabled or not. Maybe replace
|
---|
244 | * this by GetAdapterAddresses(), which is XP/Vista only though. */
|
---|
245 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen))
|
---|
246 | {
|
---|
247 | if (FixedInfo)
|
---|
248 | {
|
---|
249 | GlobalFree(FixedInfo);
|
---|
250 | FixedInfo = NULL;
|
---|
251 | }
|
---|
252 | FixedInfo = GlobalAlloc(GPTR, BufLen);
|
---|
253 | }
|
---|
254 |
|
---|
255 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS)
|
---|
256 | {
|
---|
257 | Log(("GetNetworkParams failed. ret = %08x\n", (u_int)ret ));
|
---|
258 | if (FixedInfo)
|
---|
259 | {
|
---|
260 | GlobalFree(FixedInfo);
|
---|
261 | FixedInfo = NULL;
|
---|
262 | }
|
---|
263 | rc = -1;
|
---|
264 | goto get_dns_prefix;
|
---|
265 | }
|
---|
266 |
|
---|
267 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
268 | pIPAddr = &(FixedInfo->DnsServerList);
|
---|
269 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
|
---|
270 | Log(("nat: DNS Servers:\n"));
|
---|
271 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
272 | LogRel(("NAT: DNS address: %s\n", pIPAddr->IpAddress.String));
|
---|
273 | *pdns_addr = tmp_addr;
|
---|
274 |
|
---|
275 | pIPAddr = FixedInfo -> DnsServerList.Next;
|
---|
276 | while (pIPAddr)
|
---|
277 | {
|
---|
278 | if (fVerbose)
|
---|
279 | LogRel(("NAT: ignored DNS address: %s\n", pIPAddr ->IpAddress.String));
|
---|
280 | pIPAddr = pIPAddr ->Next;
|
---|
281 | }
|
---|
282 | #else
|
---|
283 | /*localhost mask */
|
---|
284 | for (pIPAddr = &FixedInfo->DnsServerList; pIPAddr != NULL; pIPAddr = pIPAddr->Next)
|
---|
285 | {
|
---|
286 | struct dns_entry *da;
|
---|
287 | if(!inet_aton(pIPAddr->IpAddress.String, &tmp_addr))
|
---|
288 | continue;
|
---|
289 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
290 | if (da == NULL)
|
---|
291 | {
|
---|
292 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
293 | return -1;
|
---|
294 | }
|
---|
295 | /*check */
|
---|
296 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
297 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
298 | }
|
---|
299 | LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
300 | }
|
---|
301 | #endif
|
---|
302 | if (FixedInfo)
|
---|
303 | {
|
---|
304 | GlobalFree(FixedInfo);
|
---|
305 | FixedInfo = NULL;
|
---|
306 | }
|
---|
307 |
|
---|
308 | get_dns_prefix:
|
---|
309 | if (ppszDomain)
|
---|
310 | {
|
---|
311 | OSVERSIONINFO ver;
|
---|
312 | char szDnsDomain[256];
|
---|
313 | DWORD dwSize = sizeof(szDnsDomain);
|
---|
314 |
|
---|
315 | *ppszDomain = NULL;
|
---|
316 | GetVersionEx(&ver);
|
---|
317 | if (ver.dwMajorVersion >= 5)
|
---|
318 | {
|
---|
319 | /* GetComputerNameEx exists in Windows versions starting with 2000. */
|
---|
320 | if (GetComputerNameEx(ComputerNameDnsDomain, szDnsDomain, &dwSize))
|
---|
321 | {
|
---|
322 | if (szDnsDomain[0])
|
---|
323 | {
|
---|
324 | /* Just non-empty strings are valid. */
|
---|
325 | *ppszDomain = RTStrDup(szDnsDomain);
|
---|
326 | if (pData->fPassDomain)
|
---|
327 | {
|
---|
328 | if (fVerbose)
|
---|
329 | LogRel(("NAT: passing domain name %s\n", szDnsDomain));
|
---|
330 | }
|
---|
331 | else
|
---|
332 | Log(("nat: ignoring domain %s\n", szDnsDomain));
|
---|
333 | }
|
---|
334 | }
|
---|
335 | else
|
---|
336 | Log(("nat: GetComputerNameEx failed (%d)\n", GetLastError()));
|
---|
337 | }
|
---|
338 | }
|
---|
339 | return rc;
|
---|
340 | }
|
---|
341 |
|
---|
342 | #else
|
---|
343 |
|
---|
344 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
345 | struct in_addr *pdns_addr,
|
---|
346 | const char **ppszDomain)
|
---|
347 | {
|
---|
348 | char buff[512];
|
---|
349 | char buff2[256];
|
---|
350 | FILE *f;
|
---|
351 | int found = 0;
|
---|
352 | struct in_addr tmp_addr;
|
---|
353 |
|
---|
354 | #ifdef RT_OS_OS2
|
---|
355 | /* Try various locations. */
|
---|
356 | char *etc = getenv("ETC");
|
---|
357 | f = NULL;
|
---|
358 | if (etc)
|
---|
359 | {
|
---|
360 | snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
|
---|
361 | f = fopen(buff, "rt");
|
---|
362 | }
|
---|
363 | if (!f)
|
---|
364 | {
|
---|
365 | snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
|
---|
366 | f = fopen(buff, "rt");
|
---|
367 | }
|
---|
368 | if (!f)
|
---|
369 | {
|
---|
370 | snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
|
---|
371 | f = fopen(buff, "rt");
|
---|
372 | }
|
---|
373 | #else
|
---|
374 | f = fopen("/etc/resolv.conf", "r");
|
---|
375 | #endif
|
---|
376 | if (!f)
|
---|
377 | return -1;
|
---|
378 |
|
---|
379 | if (ppszDomain)
|
---|
380 | *ppszDomain = NULL;
|
---|
381 | Log(("nat: DNS Servers:\n"));
|
---|
382 | while (fgets(buff, 512, f) != NULL)
|
---|
383 | {
|
---|
384 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
385 | struct dns_entry *da = NULL;
|
---|
386 | #endif
|
---|
387 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
|
---|
388 | {
|
---|
389 | if (!inet_aton(buff2, &tmp_addr))
|
---|
390 | continue;
|
---|
391 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
392 | /* If it's the first one, set it to dns_addr */
|
---|
393 | if (!found)
|
---|
394 | {
|
---|
395 | if (fVerbose || pdns_addr->s_addr != tmp_addr.s_addr)
|
---|
396 | LogRel(("NAT: DNS address: %s\n", buff2));
|
---|
397 | *pdns_addr = tmp_addr;
|
---|
398 | }
|
---|
399 | else
|
---|
400 | {
|
---|
401 | if (fVerbose)
|
---|
402 | LogRel(("NAT: ignored DNS address: %s\n", buff2));
|
---|
403 | }
|
---|
404 | #else
|
---|
405 | /*localhost mask */
|
---|
406 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
407 | if (da == NULL)
|
---|
408 | {
|
---|
409 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
410 | return -1;
|
---|
411 | }
|
---|
412 | /*check */
|
---|
413 | da->de_addr.s_addr = tmp_addr.s_addr;
|
---|
414 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
415 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
416 | }
|
---|
417 | LIST_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
418 | #endif
|
---|
419 | found++;
|
---|
420 | }
|
---|
421 | if ( ppszDomain
|
---|
422 | && (!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
|
---|
423 | {
|
---|
424 | /* Domain name/search list present. Pick first entry */
|
---|
425 | if (*ppszDomain == NULL)
|
---|
426 | {
|
---|
427 | char *tok;
|
---|
428 | char *saveptr;
|
---|
429 | tok = strtok_r(&buff[6], " \t\n", &saveptr);
|
---|
430 | if (tok)
|
---|
431 | {
|
---|
432 | *ppszDomain = RTStrDup(tok);
|
---|
433 | if (pData->fPassDomain)
|
---|
434 | {
|
---|
435 | if (fVerbose)
|
---|
436 | LogRel(("NAT: passing domain name %s\n", tok));
|
---|
437 | }
|
---|
438 | else
|
---|
439 | Log(("nat: ignoring domain %s\n", tok));
|
---|
440 | }
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 | fclose(f);
|
---|
445 | if (!found)
|
---|
446 | return -1;
|
---|
447 | return 0;
|
---|
448 | }
|
---|
449 |
|
---|
450 | #endif
|
---|
451 |
|
---|
452 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
|
---|
453 | {
|
---|
454 | return get_dns_addr_domain(pData, false, pdns_addr, NULL);
|
---|
455 | }
|
---|
456 |
|
---|
457 | int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
|
---|
458 | bool fPassDomain, const char *pszTFTPPrefix,
|
---|
459 | const char *pszBootFile, void *pvUser)
|
---|
460 | {
|
---|
461 | int fNATfailed = 0;
|
---|
462 | int rc;
|
---|
463 | PNATState pData = RTMemAllocZ(sizeof(NATState));
|
---|
464 | *ppData = pData;
|
---|
465 | if (!pData)
|
---|
466 | return VERR_NO_MEMORY;
|
---|
467 | if (u32Netmask & 0x1f)
|
---|
468 | /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
|
---|
469 | return VERR_INVALID_PARAMETER;
|
---|
470 | pData->fPassDomain = fPassDomain;
|
---|
471 | pData->pvUser = pvUser;
|
---|
472 | tftp_prefix = pszTFTPPrefix;
|
---|
473 | bootp_filename = pszBootFile;
|
---|
474 | pData->netmask = u32Netmask;
|
---|
475 |
|
---|
476 | #ifdef RT_OS_WINDOWS
|
---|
477 | {
|
---|
478 | WSADATA Data;
|
---|
479 | WSAStartup(MAKEWORD(2, 0), &Data);
|
---|
480 | }
|
---|
481 | # if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
482 | pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
483 | # endif
|
---|
484 | #endif
|
---|
485 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
486 | QSOCKET_LOCK_CREATE(tcb);
|
---|
487 | QSOCKET_LOCK_CREATE(udb);
|
---|
488 | rc = RTReqCreateQueue(&pData->pReqQueue);
|
---|
489 | AssertReleaseRC(rc);
|
---|
490 | #endif
|
---|
491 |
|
---|
492 | link_up = 1;
|
---|
493 |
|
---|
494 | debug_init();
|
---|
495 | if_init(pData);
|
---|
496 | ip_init(pData);
|
---|
497 | icmp_init(pData);
|
---|
498 |
|
---|
499 | /* Initialise mbufs *after* setting the MTU */
|
---|
500 | m_init(pData);
|
---|
501 |
|
---|
502 | inet_aton(pszNetAddr, &special_addr);
|
---|
503 | alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
|
---|
504 | /* @todo: add ability to configure this staff */
|
---|
505 | pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
|
---|
506 |
|
---|
507 | /* set default addresses */
|
---|
508 | inet_aton("127.0.0.1", &loopback_addr);
|
---|
509 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
510 | inet_aton("127.0.0.1", &dns_addr);
|
---|
511 |
|
---|
512 | if (get_dns_addr_domain(pData, true, &dns_addr, &pData->pszDomain) < 0)
|
---|
513 | #else
|
---|
514 | LIST_INIT(&pData->dns_list_head);
|
---|
515 | if (get_dns_addr_domain(pData, true, NULL, &pData->pszDomain) < 0)
|
---|
516 | #endif
|
---|
517 | fNATfailed = 1;
|
---|
518 |
|
---|
519 | getouraddr(pData);
|
---|
520 | return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * Statistics counters.
|
---|
525 | */
|
---|
526 | void slirp_register_timers(PNATState pData, PPDMDRVINS pDrvIns)
|
---|
527 | {
|
---|
528 | #ifdef VBOX_WITH_STATISTICS
|
---|
529 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFill, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
530 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp fills", "/Drivers/NAT%d/Fill", pDrvIns->iInstance);
|
---|
531 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPoll, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
532 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp polls", "/Drivers/NAT%d/Poll", pDrvIns->iInstance);
|
---|
533 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatFastTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
534 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp fast timer", "/Drivers/NAT%d/TimerFast", pDrvIns->iInstance);
|
---|
535 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatSlowTimer, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
|
---|
536 | STAMUNIT_TICKS_PER_CALL, "Profiling slirp slow timer", "/Drivers/NAT%d/TimerSlow", pDrvIns->iInstance);
|
---|
537 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
538 | STAMUNIT_COUNT, "TCP sockets", "/Drivers/NAT%d/SockTCP", pDrvIns->iInstance);
|
---|
539 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTCPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
540 | STAMUNIT_COUNT, "TCP sockets active", "/Drivers/NAT%d/SockTCPHot", pDrvIns->iInstance);
|
---|
541 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDP, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
542 | STAMUNIT_COUNT, "UDP sockets", "/Drivers/NAT%d/SockUDP", pDrvIns->iInstance);
|
---|
543 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatUDPHot, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
544 | STAMUNIT_COUNT, "UDP sockets active", "/Drivers/NAT%d/SockUDPHot", pDrvIns->iInstance);
|
---|
545 | #endif /* VBOX_WITH_STATISTICS */
|
---|
546 | }
|
---|
547 |
|
---|
548 | /**
|
---|
549 | * Marks the link as up, making it possible to establish new connections.
|
---|
550 | */
|
---|
551 | void slirp_link_up(PNATState pData)
|
---|
552 | {
|
---|
553 | link_up = 1;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Marks the link as down and cleans up the current connections.
|
---|
558 | */
|
---|
559 | void slirp_link_down(PNATState pData)
|
---|
560 | {
|
---|
561 | struct socket *so;
|
---|
562 |
|
---|
563 | while ((so = tcb.so_next) != &tcb)
|
---|
564 | {
|
---|
565 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
566 | sofree(pData, so);
|
---|
567 | else
|
---|
568 | tcp_drop(pData, sototcpcb(so), 0);
|
---|
569 | }
|
---|
570 |
|
---|
571 | while ((so = udb.so_next) != &udb)
|
---|
572 | udp_detach(pData, so);
|
---|
573 |
|
---|
574 | link_up = 0;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /**
|
---|
578 | * Terminates the slirp component.
|
---|
579 | */
|
---|
580 | void slirp_term(PNATState pData)
|
---|
581 | {
|
---|
582 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
583 | struct dns_entry *de = NULL;
|
---|
584 | #endif
|
---|
585 | if (pData->pszDomain)
|
---|
586 | RTStrFree((char *)(void *)pData->pszDomain);
|
---|
587 |
|
---|
588 | #ifdef RT_OS_WINDOWS
|
---|
589 | pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
|
---|
590 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
591 | RTMemFree(pData->pvIcmpBuffer);
|
---|
592 | # else
|
---|
593 | closesocket(pData->icmp_socket.s);
|
---|
594 | #endif
|
---|
595 |
|
---|
596 | slirp_link_down(pData);
|
---|
597 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
598 | while(!LIST_EMPTY(&pData->dns_list_head)) {
|
---|
599 | de = LIST_FIRST(&pData->dns_list_head);
|
---|
600 | LIST_REMOVE(de, de_list);
|
---|
601 | RTMemFree(de);
|
---|
602 | }
|
---|
603 | #endif
|
---|
604 | #ifdef RT_OS_WINDOWS
|
---|
605 | WSACleanup();
|
---|
606 | #endif
|
---|
607 | #ifdef LOG_ENABLED
|
---|
608 | Log(("\n"
|
---|
609 | "NAT statistics\n"
|
---|
610 | "--------------\n"
|
---|
611 | "\n"));
|
---|
612 | ipstats(pData);
|
---|
613 | tcpstats(pData);
|
---|
614 | udpstats(pData);
|
---|
615 | icmpstats(pData);
|
---|
616 | mbufstats(pData);
|
---|
617 | sockstats(pData);
|
---|
618 | Log(("\n"
|
---|
619 | "\n"
|
---|
620 | "\n"));
|
---|
621 | #endif
|
---|
622 | RTMemFree(pData);
|
---|
623 | }
|
---|
624 |
|
---|
625 |
|
---|
626 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
627 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
628 | #define UPD_NFDS(x) do { if (nfds < (x)) nfds = (x); } while (0)
|
---|
629 |
|
---|
630 | /*
|
---|
631 | * curtime kept to an accuracy of 1ms
|
---|
632 | */
|
---|
633 | static void updtime(PNATState pData)
|
---|
634 | {
|
---|
635 | #ifdef RT_OS_WINDOWS
|
---|
636 | struct _timeb tb;
|
---|
637 |
|
---|
638 | _ftime(&tb);
|
---|
639 | curtime = (u_int)tb.time * (u_int)1000;
|
---|
640 | curtime += (u_int)tb.millitm;
|
---|
641 | #else
|
---|
642 | gettimeofday(&tt, 0);
|
---|
643 |
|
---|
644 | curtime = (u_int)tt.tv_sec * (u_int)1000;
|
---|
645 | curtime += (u_int)tt.tv_usec / (u_int)1000;
|
---|
646 |
|
---|
647 | if ((tt.tv_usec % 1000) >= 500)
|
---|
648 | curtime++;
|
---|
649 | #endif
|
---|
650 | }
|
---|
651 |
|
---|
652 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
653 | void slirp_select_fill(PNATState pData, int *pnfds,
|
---|
654 | fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
655 | #else /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
656 | # ifdef RT_OS_WINDOWS
|
---|
657 | void slirp_select_fill(PNATState pData, int *pnfds)
|
---|
658 | # else /* RT_OS_WINDOWS */
|
---|
659 | void slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
|
---|
660 | # endif /* !RT_OS_WINDOWS */
|
---|
661 | #endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
662 | {
|
---|
663 | struct socket *so, *so_next;
|
---|
664 | int nfds;
|
---|
665 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
666 | int rc;
|
---|
667 | int error;
|
---|
668 | #endif
|
---|
669 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
|
---|
670 | int poll_index = 0;
|
---|
671 | #endif
|
---|
672 | int i;
|
---|
673 |
|
---|
674 | STAM_PROFILE_START(&pData->StatFill, a);
|
---|
675 |
|
---|
676 | nfds = *pnfds;
|
---|
677 |
|
---|
678 | /*
|
---|
679 | * First, TCP sockets
|
---|
680 | */
|
---|
681 | do_slowtimo = 0;
|
---|
682 | if (link_up)
|
---|
683 | {
|
---|
684 | /*
|
---|
685 | * *_slowtimo needs calling if there are IP fragments
|
---|
686 | * in the fragment queue, or there are TCP connections active
|
---|
687 | */
|
---|
688 | /* XXX:
|
---|
689 | * triggering of fragment expiration should be the same but use new macroses
|
---|
690 | */
|
---|
691 | do_slowtimo = (tcb.so_next != &tcb);
|
---|
692 | if (!do_slowtimo)
|
---|
693 | {
|
---|
694 | for (i = 0; i < IPREASS_NHASH; i++)
|
---|
695 | {
|
---|
696 | if (!TAILQ_EMPTY(&ipq[i]))
|
---|
697 | {
|
---|
698 | do_slowtimo = 1;
|
---|
699 | break;
|
---|
700 | }
|
---|
701 | }
|
---|
702 | }
|
---|
703 | ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
|
---|
704 |
|
---|
705 | STAM_COUNTER_RESET(&pData->StatTCP);
|
---|
706 | STAM_COUNTER_RESET(&pData->StatTCPHot);
|
---|
707 |
|
---|
708 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
709 | /* { */
|
---|
710 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
|
---|
711 | so->so_poll_index = -1;
|
---|
712 | #endif
|
---|
713 | STAM_COUNTER_INC(&pData->StatTCP);
|
---|
714 |
|
---|
715 | /*
|
---|
716 | * See if we need a tcp_fasttimo
|
---|
717 | */
|
---|
718 | if ( time_fasttimo == 0
|
---|
719 | && so->so_tcpcb != NULL
|
---|
720 | && so->so_tcpcb->t_flags & TF_DELACK)
|
---|
721 | time_fasttimo = curtime; /* Flag when we want a fasttimo */
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * NOFDREF can include still connecting to local-host,
|
---|
725 | * newly socreated() sockets etc. Don't want to select these.
|
---|
726 | */
|
---|
727 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
728 | CONTINUE(tcp);
|
---|
729 |
|
---|
730 | /*
|
---|
731 | * Set for reading sockets which are accepting
|
---|
732 | */
|
---|
733 | if (so->so_state & SS_FACCEPTCONN)
|
---|
734 | {
|
---|
735 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
736 | TCP_ENGAGE_EVENT1(so, readfds);
|
---|
737 | CONTINUE(tcp);
|
---|
738 | }
|
---|
739 |
|
---|
740 | /*
|
---|
741 | * Set for writing sockets which are connecting
|
---|
742 | */
|
---|
743 | if (so->so_state & SS_ISFCONNECTING)
|
---|
744 | {
|
---|
745 | Log2(("connecting %R[natsock] engaged\n",so));
|
---|
746 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
747 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
748 | }
|
---|
749 |
|
---|
750 | /*
|
---|
751 | * Set for writing if we are connected, can send more, and
|
---|
752 | * we have something to send
|
---|
753 | */
|
---|
754 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
|
---|
755 | {
|
---|
756 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
757 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
758 | }
|
---|
759 |
|
---|
760 | /*
|
---|
761 | * Set for reading (and urgent data) if we are connected, can
|
---|
762 | * receive more, and we have room for it XXX /2 ?
|
---|
763 | */
|
---|
764 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
|
---|
765 | {
|
---|
766 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
767 | TCP_ENGAGE_EVENT2(so, readfds, xfds);
|
---|
768 | }
|
---|
769 | LOOP_LABEL(tcp, so, so_next);
|
---|
770 | }
|
---|
771 |
|
---|
772 | /*
|
---|
773 | * UDP sockets
|
---|
774 | */
|
---|
775 | STAM_COUNTER_RESET(&pData->StatUDP);
|
---|
776 | STAM_COUNTER_RESET(&pData->StatUDPHot);
|
---|
777 |
|
---|
778 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
779 | /* { */
|
---|
780 |
|
---|
781 | STAM_COUNTER_INC(&pData->StatUDP);
|
---|
782 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
|
---|
783 | so->so_poll_index = -1;
|
---|
784 | #endif
|
---|
785 |
|
---|
786 | /*
|
---|
787 | * See if it's timed out
|
---|
788 | */
|
---|
789 | if (so->so_expire)
|
---|
790 | {
|
---|
791 | if (so->so_expire <= curtime)
|
---|
792 | {
|
---|
793 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
794 | /* we need so_next for continue our cycle*/
|
---|
795 | so_next = so->so_next;
|
---|
796 | #endif
|
---|
797 | UDP_DETACH(pData, so, so_next);
|
---|
798 | CONTINUE_NO_UNLOCK(udp);
|
---|
799 | }
|
---|
800 | else
|
---|
801 | do_slowtimo = 1; /* Let socket expire */
|
---|
802 | }
|
---|
803 |
|
---|
804 | /*
|
---|
805 | * When UDP packets are received from over the link, they're
|
---|
806 | * sendto()'d straight away, so no need for setting for writing
|
---|
807 | * Limit the number of packets queued by this session to 4.
|
---|
808 | * Note that even though we try and limit this to 4 packets,
|
---|
809 | * the session could have more queued if the packets needed
|
---|
810 | * to be fragmented.
|
---|
811 | *
|
---|
812 | * (XXX <= 4 ?)
|
---|
813 | */
|
---|
814 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
|
---|
815 | {
|
---|
816 | STAM_COUNTER_INC(&pData->StatUDPHot);
|
---|
817 | UDP_ENGAGE_EVENT(so, readfds);
|
---|
818 | }
|
---|
819 | LOOP_LABEL(udp, so, so_next);
|
---|
820 | }
|
---|
821 |
|
---|
822 | }
|
---|
823 |
|
---|
824 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
825 | # if defined(RT_OS_WINDOWS)
|
---|
826 | *pnfds = VBOX_EVENT_COUNT;
|
---|
827 | # else /* RT_OS_WINDOWS */
|
---|
828 | AssertRelease(poll_index <= *pnfds);
|
---|
829 | *pnfds = poll_index;
|
---|
830 | # endif /* !RT_OS_WINDOWS */
|
---|
831 | #else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
832 | *pnfds = nfds;
|
---|
833 | #endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
834 |
|
---|
835 | STAM_PROFILE_STOP(&pData->StatFill, a);
|
---|
836 | }
|
---|
837 |
|
---|
838 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
|
---|
839 | # if defined(RT_OS_WINDOWS)
|
---|
840 | void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
|
---|
841 | # else /* RT_OS_WINDOWS */
|
---|
842 | void slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
|
---|
843 | # endif /* !RT_OS_WINDOWS */
|
---|
844 | #else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
845 | void slirp_select_poll(PNATState pData, fd_set *readfds, fd_set *writefds, fd_set *xfds)
|
---|
846 | #endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
|
---|
847 | {
|
---|
848 | struct socket *so, *so_next;
|
---|
849 | int ret;
|
---|
850 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
851 | WSANETWORKEVENTS NetworkEvents;
|
---|
852 | int rc;
|
---|
853 | int error;
|
---|
854 | #endif
|
---|
855 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && !defined(RT_OS_WINDOWS)
|
---|
856 | int poll_index = 0;
|
---|
857 | #endif
|
---|
858 |
|
---|
859 | STAM_PROFILE_START(&pData->StatPoll, a);
|
---|
860 |
|
---|
861 | /* Update time */
|
---|
862 | updtime(pData);
|
---|
863 |
|
---|
864 | /*
|
---|
865 | * See if anything has timed out
|
---|
866 | */
|
---|
867 | if (link_up)
|
---|
868 | {
|
---|
869 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
|
---|
870 | {
|
---|
871 | STAM_PROFILE_START(&pData->StatFastTimer, a);
|
---|
872 | tcp_fasttimo(pData);
|
---|
873 | time_fasttimo = 0;
|
---|
874 | STAM_PROFILE_STOP(&pData->StatFastTimer, a);
|
---|
875 | }
|
---|
876 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
|
---|
877 | {
|
---|
878 | STAM_PROFILE_START(&pData->StatSlowTimer, a);
|
---|
879 | ip_slowtimo(pData);
|
---|
880 | tcp_slowtimo(pData);
|
---|
881 | last_slowtimo = curtime;
|
---|
882 | STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
|
---|
883 | }
|
---|
884 | }
|
---|
885 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
886 | if (fTimeout)
|
---|
887 | return; /* only timer update */
|
---|
888 | #endif
|
---|
889 |
|
---|
890 | /*
|
---|
891 | * Check sockets
|
---|
892 | */
|
---|
893 | if (link_up)
|
---|
894 | {
|
---|
895 | #if defined(RT_OS_WINDOWS)
|
---|
896 | /*XXX: before renaming please make see define
|
---|
897 | * fIcmp in slirp_state.h
|
---|
898 | */
|
---|
899 | if (fIcmp)
|
---|
900 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
901 | #else
|
---|
902 | if ( (pData->icmp_socket.s != -1)
|
---|
903 | && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
|
---|
904 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
905 | #endif
|
---|
906 | /*
|
---|
907 | * Check TCP sockets
|
---|
908 | */
|
---|
909 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
910 | /* { */
|
---|
911 |
|
---|
912 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
913 | if ( so->so_state & SS_NOFDREF
|
---|
914 | && so->so_deleted == 1)
|
---|
915 | {
|
---|
916 | struct socket *son, *sop = NULL;
|
---|
917 | QSOCKET_LOCK(tcb);
|
---|
918 | if (so->so_next != NULL)
|
---|
919 | {
|
---|
920 | if (so->so_next != &tcb)
|
---|
921 | SOCKET_LOCK(so->so_next);
|
---|
922 | son = so->so_next;
|
---|
923 | }
|
---|
924 | if ( so->so_prev != &tcb
|
---|
925 | && so->so_prev != NULL)
|
---|
926 | {
|
---|
927 | SOCKET_LOCK(so->so_prev);
|
---|
928 | sop = so->so_prev;
|
---|
929 | }
|
---|
930 | QSOCKET_UNLOCK(tcb);
|
---|
931 | remque(pData, so);
|
---|
932 | NSOCK_DEC();
|
---|
933 | SOCKET_UNLOCK(so);
|
---|
934 | SOCKET_LOCK_DESTROY(so);
|
---|
935 | RTMemFree(so);
|
---|
936 | so_next = son;
|
---|
937 | if (sop != NULL)
|
---|
938 | SOCKET_UNLOCK(sop);
|
---|
939 | CONTINUE_NO_UNLOCK(tcp);
|
---|
940 | }
|
---|
941 | #endif
|
---|
942 | /*
|
---|
943 | * FD_ISSET is meaningless on these sockets
|
---|
944 | * (and they can crash the program)
|
---|
945 | */
|
---|
946 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
947 | CONTINUE(tcp);
|
---|
948 |
|
---|
949 | POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
950 |
|
---|
951 | LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
|
---|
952 |
|
---|
953 |
|
---|
954 | /*
|
---|
955 | * Check for URG data
|
---|
956 | * This will soread as well, so no need to
|
---|
957 | * test for readfds below if this succeeds
|
---|
958 | */
|
---|
959 |
|
---|
960 | /* out-of-band data */
|
---|
961 | if (CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
962 | {
|
---|
963 | sorecvoob(pData, so);
|
---|
964 | }
|
---|
965 |
|
---|
966 | /*
|
---|
967 | * Check sockets for reading
|
---|
968 | */
|
---|
969 | else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
970 | || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
|
---|
971 | {
|
---|
972 | /*
|
---|
973 | * Check for incoming connections
|
---|
974 | */
|
---|
975 | if (so->so_state & SS_FACCEPTCONN)
|
---|
976 | {
|
---|
977 | TCP_CONNECT(pData, so);
|
---|
978 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
979 | if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
|
---|
980 | #endif
|
---|
981 | CONTINUE(tcp);
|
---|
982 | }
|
---|
983 |
|
---|
984 | ret = soread(pData, so);
|
---|
985 | /* Output it if we read something */
|
---|
986 | if (ret > 0)
|
---|
987 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
988 | }
|
---|
989 |
|
---|
990 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
991 | /*
|
---|
992 | * Check for FD_CLOSE events.
|
---|
993 | * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
|
---|
994 | */
|
---|
995 | if ( (NetworkEvents.lNetworkEvents & FD_CLOSE)
|
---|
996 | || (so->so_close == 1))
|
---|
997 | {
|
---|
998 | so->so_close = 1; /* mark it */
|
---|
999 | /*
|
---|
1000 | * drain the socket
|
---|
1001 | */
|
---|
1002 | for (;;)
|
---|
1003 | {
|
---|
1004 | ret = soread(pData, so);
|
---|
1005 | if (ret > 0)
|
---|
1006 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
1007 | else
|
---|
1008 | break;
|
---|
1009 | }
|
---|
1010 | CONTINUE(tcp);
|
---|
1011 | }
|
---|
1012 | #endif
|
---|
1013 |
|
---|
1014 | /*
|
---|
1015 | * Check sockets for writing
|
---|
1016 | */
|
---|
1017 | if (CHECK_FD_SET(so, NetworkEvents, writefds))
|
---|
1018 | {
|
---|
1019 | /*
|
---|
1020 | * Check for non-blocking, still-connecting sockets
|
---|
1021 | */
|
---|
1022 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1023 | {
|
---|
1024 | Log2(("connecting %R[natsock] catched\n", so));
|
---|
1025 | /* Connected */
|
---|
1026 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
1027 |
|
---|
1028 | /*
|
---|
1029 | * This should be probably guarded by PROBE_CONN too. Anyway,
|
---|
1030 | * we disable it on OS/2 because the below send call returns
|
---|
1031 | * EFAULT which causes the opened TCP socket to close right
|
---|
1032 | * after it has been opened and connected.
|
---|
1033 | */
|
---|
1034 | #ifndef RT_OS_OS2
|
---|
1035 | ret = send(so->s, (const char *)&ret, 0, 0);
|
---|
1036 | if (ret < 0)
|
---|
1037 | {
|
---|
1038 | /* XXXXX Must fix, zero bytes is a NOP */
|
---|
1039 | if ( errno == EAGAIN
|
---|
1040 | || errno == EWOULDBLOCK
|
---|
1041 | || errno == EINPROGRESS
|
---|
1042 | || errno == ENOTCONN)
|
---|
1043 | CONTINUE(tcp);
|
---|
1044 |
|
---|
1045 | /* else failed */
|
---|
1046 | so->so_state = SS_NOFDREF;
|
---|
1047 | }
|
---|
1048 | /* else so->so_state &= ~SS_ISFCONNECTING; */
|
---|
1049 | #endif
|
---|
1050 |
|
---|
1051 | /*
|
---|
1052 | * Continue tcp_input
|
---|
1053 | */
|
---|
1054 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1055 | /* continue; */
|
---|
1056 | }
|
---|
1057 | else
|
---|
1058 | SOWRITE(ret, pData, so);
|
---|
1059 | /*
|
---|
1060 | * XXX If we wrote something (a lot), there could be the need
|
---|
1061 | * for a window update. In the worst case, the remote will send
|
---|
1062 | * a window probe to get things going again.
|
---|
1063 | */
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | /*
|
---|
1067 | * Probe a still-connecting, non-blocking socket
|
---|
1068 | * to check if it's still alive
|
---|
1069 | */
|
---|
1070 | #ifdef PROBE_CONN
|
---|
1071 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1072 | {
|
---|
1073 | ret = recv(so->s, (char *)&ret, 0, 0);
|
---|
1074 |
|
---|
1075 | if (ret < 0)
|
---|
1076 | {
|
---|
1077 | /* XXX */
|
---|
1078 | if ( errno == EAGAIN
|
---|
1079 | || errno == EWOULDBLOCK
|
---|
1080 | || errno == EINPROGRESS
|
---|
1081 | || errno == ENOTCONN)
|
---|
1082 | {
|
---|
1083 | CONTINUE(tcp); /* Still connecting, continue */
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | /* else failed */
|
---|
1087 | so->so_state = SS_NOFDREF;
|
---|
1088 |
|
---|
1089 | /* tcp_input will take care of it */
|
---|
1090 | }
|
---|
1091 | else
|
---|
1092 | {
|
---|
1093 | ret = send(so->s, &ret, 0, 0);
|
---|
1094 | if (ret < 0)
|
---|
1095 | {
|
---|
1096 | /* XXX */
|
---|
1097 | if ( errno == EAGAIN
|
---|
1098 | || errno == EWOULDBLOCK
|
---|
1099 | || errno == EINPROGRESS
|
---|
1100 | || errno == ENOTCONN)
|
---|
1101 | {
|
---|
1102 | CONTINUE(tcp);
|
---|
1103 | }
|
---|
1104 | /* else failed */
|
---|
1105 | so->so_state = SS_NOFDREF;
|
---|
1106 | }
|
---|
1107 | else
|
---|
1108 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
1109 |
|
---|
1110 | }
|
---|
1111 | TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
|
---|
1112 | } /* SS_ISFCONNECTING */
|
---|
1113 | #endif
|
---|
1114 | #ifndef RT_OS_WINDOWS
|
---|
1115 | if ( UNIX_CHECK_FD_SET(so, NetworkEvents, rdhup)
|
---|
1116 | || UNIX_CHECK_FD_SET(so, NetworkEvents, rderr))
|
---|
1117 | {
|
---|
1118 | int err;
|
---|
1119 | int inq, outq;
|
---|
1120 | int status;
|
---|
1121 | socklen_t optlen = sizeof(int);
|
---|
1122 | inq = outq = 0;
|
---|
1123 | status = getsockopt(so->s, SOL_SOCKET, SO_ERROR, &err, &optlen);
|
---|
1124 | if (status != 0)
|
---|
1125 | LogRel(("NAT: can't get error status from %R[natsock]\n", so));
|
---|
1126 | #ifndef RT_OS_SOLARIS
|
---|
1127 | status = ioctl(so->s, FIONREAD, &inq); /* tcp(7) recommends SIOCINQ which is Linux specific */
|
---|
1128 | if (status != 0 || status != EINVAL)
|
---|
1129 | {
|
---|
1130 | /* EINVAL returned if socket in listen state tcp(7)*/
|
---|
1131 | LogRel(("NAT: can't get depth of IN queue status from %R[natsock]\n", so));
|
---|
1132 | }
|
---|
1133 | status = ioctl(so->s, TIOCOUTQ, &outq); /* SIOCOUTQ see previous comment */
|
---|
1134 | if (status != 0)
|
---|
1135 | LogRel(("NAT: can't get depth of OUT queue from %R[natsock]\n", so));
|
---|
1136 | #else
|
---|
1137 | /*
|
---|
1138 | * Solaris has bit different ioctl commands and its handlings
|
---|
1139 | * hint: streamio(7) I_NREAD
|
---|
1140 | */
|
---|
1141 | #endif
|
---|
1142 | if ( so->so_state & SS_ISFCONNECTING
|
---|
1143 | || UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1144 | {
|
---|
1145 | /**
|
---|
1146 | * Check if we need here take care about gracefull connection
|
---|
1147 | * @todo try with proxy server
|
---|
1148 | */
|
---|
1149 | if (UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1150 | {
|
---|
1151 | /*
|
---|
1152 | * Never meet inq != 0 or outq != 0, anyway let it stay for a while
|
---|
1153 | * in case it happens we'll able to detect it.
|
---|
1154 | * Give TCP/IP stack wait or expire the socket.
|
---|
1155 | */
|
---|
1156 | LogRel(("NAT: %R[natsock] err(%d:%s) s(in:%d,out:%d)happens on read I/O, "
|
---|
1157 | "other side close connection \n", so, err, strerror(err), inq, outq));
|
---|
1158 | CONTINUE(tcp);
|
---|
1159 | }
|
---|
1160 | goto tcp_input_close;
|
---|
1161 | }
|
---|
1162 | if ( !UNIX_CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
1163 | && !UNIX_CHECK_FD_SET(so, NetworkEvents, writefds)
|
---|
1164 | && !UNIX_CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
1165 | {
|
---|
1166 | LogRel(("NAT: system expires the socket %R[natsock] err(%d:%s) s(in:%d,out:%d) happens on non-I/O. ",
|
---|
1167 | so, err, strerror(err), inq, outq));
|
---|
1168 | goto tcp_input_close;
|
---|
1169 | }
|
---|
1170 | LogRel(("NAT: %R[natsock] we've met(%d:%s) s(in:%d, out:%d) unhandled combination hup (%d) "
|
---|
1171 | "rederr(%d) on (r:%d, w:%d, x:%d)\n",
|
---|
1172 | so, err, strerror(err),
|
---|
1173 | inq, outq,
|
---|
1174 | UNIX_CHECK_FD_SET(so, ign, rdhup),
|
---|
1175 | UNIX_CHECK_FD_SET(so, ign, rderr),
|
---|
1176 | UNIX_CHECK_FD_SET(so, ign, readfds),
|
---|
1177 | UNIX_CHECK_FD_SET(so, ign, writefds),
|
---|
1178 | UNIX_CHECK_FD_SET(so, ign, xfds)));
|
---|
1179 | /*
|
---|
1180 | * Give OS's TCP/IP stack a chance to resolve an issue or expire the socket.
|
---|
1181 | */
|
---|
1182 | CONTINUE(tcp);
|
---|
1183 | tcp_input_close:
|
---|
1184 | so->so_state = SS_NOFDREF; /*cause connection valid tcp connection termination and socket closing */
|
---|
1185 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1186 | CONTINUE(tcp);
|
---|
1187 | }
|
---|
1188 | #endif
|
---|
1189 | LOOP_LABEL(tcp, so, so_next);
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /*
|
---|
1193 | * Now UDP sockets.
|
---|
1194 | * Incoming packets are sent straight away, they're not buffered.
|
---|
1195 | * Incoming UDP data isn't buffered either.
|
---|
1196 | */
|
---|
1197 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
1198 | /* { */
|
---|
1199 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1200 | if ( so->so_state & SS_NOFDREF
|
---|
1201 | && so->so_deleted == 1)
|
---|
1202 | {
|
---|
1203 | struct socket *son, *sop = NULL;
|
---|
1204 | QSOCKET_LOCK(udb);
|
---|
1205 | if (so->so_next != NULL)
|
---|
1206 | {
|
---|
1207 | if (so->so_next != &udb)
|
---|
1208 | SOCKET_LOCK(so->so_next);
|
---|
1209 | son = so->so_next;
|
---|
1210 | }
|
---|
1211 | if ( so->so_prev != &udb
|
---|
1212 | && so->so_prev != NULL)
|
---|
1213 | {
|
---|
1214 | SOCKET_LOCK(so->so_prev);
|
---|
1215 | sop = so->so_prev;
|
---|
1216 | }
|
---|
1217 | QSOCKET_UNLOCK(udb);
|
---|
1218 | remque(pData, so);
|
---|
1219 | NSOCK_DEC();
|
---|
1220 | SOCKET_UNLOCK(so);
|
---|
1221 | SOCKET_LOCK_DESTROY(so);
|
---|
1222 | RTMemFree(so);
|
---|
1223 | so_next = son;
|
---|
1224 | if (sop != NULL)
|
---|
1225 | SOCKET_UNLOCK(sop);
|
---|
1226 | CONTINUE_NO_UNLOCK(udp);
|
---|
1227 | }
|
---|
1228 | #endif
|
---|
1229 | POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
1230 |
|
---|
1231 | LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
|
---|
1232 |
|
---|
1233 | if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1234 | {
|
---|
1235 | SORECVFROM(pData, so);
|
---|
1236 | }
|
---|
1237 | LOOP_LABEL(udp, so, so_next);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
1243 | /*
|
---|
1244 | * See if we can start outputting
|
---|
1245 | */
|
---|
1246 | if (if_queued && link_up)
|
---|
1247 | if_start(pData);
|
---|
1248 | #endif
|
---|
1249 |
|
---|
1250 | STAM_PROFILE_STOP(&pData->StatPoll, a);
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | #define ETH_ALEN 6
|
---|
1254 | #define ETH_HLEN 14
|
---|
1255 |
|
---|
1256 | #define ARPOP_REQUEST 1 /* ARP request */
|
---|
1257 | #define ARPOP_REPLY 2 /* ARP reply */
|
---|
1258 |
|
---|
1259 | struct ethhdr
|
---|
1260 | {
|
---|
1261 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
1262 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
1263 | unsigned short h_proto; /* packet type ID field */
|
---|
1264 | };
|
---|
1265 | AssertCompileSize(struct ethhdr, 14);
|
---|
1266 |
|
---|
1267 | struct arphdr
|
---|
1268 | {
|
---|
1269 | unsigned short ar_hrd; /* format of hardware address */
|
---|
1270 | unsigned short ar_pro; /* format of protocol address */
|
---|
1271 | unsigned char ar_hln; /* length of hardware address */
|
---|
1272 | unsigned char ar_pln; /* length of protocol address */
|
---|
1273 | unsigned short ar_op; /* ARP opcode (command) */
|
---|
1274 |
|
---|
1275 | /*
|
---|
1276 | * Ethernet looks like this : This bit is variable sized however...
|
---|
1277 | */
|
---|
1278 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
|
---|
1279 | unsigned char ar_sip[4]; /* sender IP address */
|
---|
1280 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
|
---|
1281 | unsigned char ar_tip[4]; /* target IP address */
|
---|
1282 | };
|
---|
1283 | AssertCompileSize(struct arphdr, 28);
|
---|
1284 |
|
---|
1285 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1286 | static void arp_input(PNATState pData, struct mbuf *m)
|
---|
1287 | #else
|
---|
1288 | static void arp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1289 | #endif
|
---|
1290 | {
|
---|
1291 | struct ethhdr *eh;
|
---|
1292 | struct ethhdr *reh;
|
---|
1293 | struct arphdr *ah;
|
---|
1294 | struct arphdr *rah;
|
---|
1295 | int ar_op;
|
---|
1296 | struct ex_list *ex_ptr;
|
---|
1297 | uint32_t htip;
|
---|
1298 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1299 | uint8_t arp_reply[sizeof(struct arphdr) + ETH_HLEN];
|
---|
1300 | eh = (struct ethhdr *)pkt;
|
---|
1301 | #else
|
---|
1302 | struct mbuf *mr;
|
---|
1303 | eh = mtod(m, struct ethhdr *);
|
---|
1304 | #endif
|
---|
1305 | ah = (struct arphdr *)&eh[1];
|
---|
1306 | htip = ntohl(*(uint32_t*)ah->ar_tip);
|
---|
1307 |
|
---|
1308 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1309 | mr = m_get(pData);
|
---|
1310 | mr->m_data += if_maxlinkhdr;
|
---|
1311 | mr->m_len = sizeof(struct arphdr);
|
---|
1312 | rah = mtod(mr, struct arphdr *);
|
---|
1313 | #else
|
---|
1314 | reh = (struct ethhdr *)arp_reply;
|
---|
1315 | rah = (struct arphdr *)&reh[1];
|
---|
1316 | #endif
|
---|
1317 |
|
---|
1318 | ar_op = ntohs(ah->ar_op);
|
---|
1319 | switch(ar_op)
|
---|
1320 | {
|
---|
1321 | case ARPOP_REQUEST:
|
---|
1322 | if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
|
---|
1323 | {
|
---|
1324 | if ( CTL_CHECK(htip, CTL_DNS)
|
---|
1325 | || CTL_CHECK(htip, CTL_ALIAS)
|
---|
1326 | || CTL_CHECK(htip, CTL_TFTP))
|
---|
1327 | goto arp_ok;
|
---|
1328 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
|
---|
1329 | {
|
---|
1330 | if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
|
---|
1331 | goto arp_ok;
|
---|
1332 | }
|
---|
1333 | return;
|
---|
1334 | arp_ok:
|
---|
1335 |
|
---|
1336 | #ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1337 | memcpy(reh->h_dest, eh->h_source, ETH_ALEN);
|
---|
1338 | memcpy(reh->h_source, &special_addr, ETH_ALEN);
|
---|
1339 | reh->h_source[5] = ah->ar_tip[3];
|
---|
1340 | reh->h_proto = htons(ETH_P_ARP);
|
---|
1341 | #endif
|
---|
1342 | rah->ar_hrd = htons(1);
|
---|
1343 | rah->ar_pro = htons(ETH_P_IP);
|
---|
1344 | rah->ar_hln = ETH_ALEN;
|
---|
1345 | rah->ar_pln = 4;
|
---|
1346 | rah->ar_op = htons(ARPOP_REPLY);
|
---|
1347 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
|
---|
1348 |
|
---|
1349 | switch (htip & ~pData->netmask)
|
---|
1350 | {
|
---|
1351 | case CTL_DNS:
|
---|
1352 | case CTL_ALIAS:
|
---|
1353 | rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
|
---|
1354 | break;
|
---|
1355 | default:;
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | memcpy(rah->ar_sip, ah->ar_tip, 4);
|
---|
1359 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
|
---|
1360 | memcpy(rah->ar_tip, ah->ar_sip, 4);
|
---|
1361 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1362 | if_encap(pData, ETH_P_ARP, mr);
|
---|
1363 | m_free(pData, m);
|
---|
1364 | #else
|
---|
1365 | slirp_output(pData->pvUser, arp_reply, sizeof(arp_reply));
|
---|
1366 | #endif
|
---|
1367 | }
|
---|
1368 | break;
|
---|
1369 | default:
|
---|
1370 | break;
|
---|
1371 | }
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1375 | {
|
---|
1376 | struct mbuf *m;
|
---|
1377 | int proto;
|
---|
1378 | static bool fWarnedIpv6;
|
---|
1379 |
|
---|
1380 | if (pkt_len < ETH_HLEN)
|
---|
1381 | {
|
---|
1382 | LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
|
---|
1383 | return;
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | m = m_get(pData);
|
---|
1387 | if (!m)
|
---|
1388 | {
|
---|
1389 | LogRel(("NAT: can't allocate new mbuf\n"));
|
---|
1390 | return;
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | /* Note: we add to align the IP header */
|
---|
1394 |
|
---|
1395 | if (M_FREEROOM(m) < pkt_len)
|
---|
1396 | m_inc(m, pkt_len);
|
---|
1397 |
|
---|
1398 | m->m_len = pkt_len ;
|
---|
1399 | memcpy(m->m_data, pkt, pkt_len);
|
---|
1400 |
|
---|
1401 | proto = ntohs(*(uint16_t *)(pkt + 12));
|
---|
1402 | switch(proto)
|
---|
1403 | {
|
---|
1404 | case ETH_P_ARP:
|
---|
1405 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1406 | arp_input(pData, m);
|
---|
1407 | #else
|
---|
1408 | arp_input(pData, pkt, pkt_len);
|
---|
1409 | m_free(pData, m);
|
---|
1410 | #endif
|
---|
1411 | break;
|
---|
1412 | case ETH_P_IP:
|
---|
1413 | /* Update time. Important if the network is very quiet, as otherwise
|
---|
1414 | * the first outgoing connection gets an incorrect timestamp. */
|
---|
1415 | updtime(pData);
|
---|
1416 | m->m_data += ETH_HLEN;
|
---|
1417 | m->m_len -= ETH_HLEN;
|
---|
1418 | ip_input(pData, m);
|
---|
1419 | break;
|
---|
1420 | case ETH_P_IPV6:
|
---|
1421 | m_free(pData, m);
|
---|
1422 | if (!fWarnedIpv6)
|
---|
1423 | {
|
---|
1424 | LogRel(("NAT: IPv6 not supported\n"));
|
---|
1425 | fWarnedIpv6 = true;
|
---|
1426 | }
|
---|
1427 | break;
|
---|
1428 | default:
|
---|
1429 | LogRel(("NAT: Unsupported protocol %x\n", proto));
|
---|
1430 | m_free(pData, m);
|
---|
1431 | break;
|
---|
1432 | }
|
---|
1433 | RTMemFree((void *)pkt);
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | /* output the IP packet to the ethernet device */
|
---|
1437 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1438 | void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
|
---|
1439 | #else
|
---|
1440 | void if_encap(PNATState pData, uint8_t *ip_data, int ip_data_len)
|
---|
1441 | #endif
|
---|
1442 | {
|
---|
1443 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1444 | struct ethhdr *eh;
|
---|
1445 | uint8_t *buf = RTMemAlloc(1600);
|
---|
1446 | m->m_data -= if_maxlinkhdr;
|
---|
1447 | m->m_len += ETH_HLEN;
|
---|
1448 | eh = mtod(m, struct ethhdr *);
|
---|
1449 | #else
|
---|
1450 | uint8_t buf[1600];
|
---|
1451 | struct ethhdr *eh = (struct ethhdr *)buf;
|
---|
1452 |
|
---|
1453 | if (ip_data_len + ETH_HLEN > sizeof(buf))
|
---|
1454 | return;
|
---|
1455 |
|
---|
1456 | memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
|
---|
1457 | #endif
|
---|
1458 |
|
---|
1459 |
|
---|
1460 | memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
|
---|
1461 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
|
---|
1462 | /* XXX: not correct */
|
---|
1463 | eh->h_source[5] = CTL_ALIAS;
|
---|
1464 | #ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
|
---|
1465 | eh->h_proto = htons(eth_proto);
|
---|
1466 | #if 0
|
---|
1467 | slirp_output(pData->pvUser, m, mtod(m, uint8_t *), m->m_len);
|
---|
1468 | #else
|
---|
1469 | memcpy(buf, mtod(m, uint8_t *), m->m_len);
|
---|
1470 | slirp_output(pData->pvUser, NULL, buf, m->m_len);
|
---|
1471 | m_free(pData, m);
|
---|
1472 | #endif
|
---|
1473 | #else
|
---|
1474 | eh->h_proto = htons(ETH_P_IP);
|
---|
1475 | slirp_output(pData->pvUser, buf, ip_data_len + ETH_HLEN);
|
---|
1476 | #endif
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | int slirp_redir(PNATState pData, int is_udp, int host_port,
|
---|
1480 | struct in_addr guest_addr, int guest_port)
|
---|
1481 | {
|
---|
1482 | struct socket *so;
|
---|
1483 | Log2(("NAT: set redirect %s hp:%d gp:%d\n", (is_udp?"UDP":"TCP"), host_port, guest_port));
|
---|
1484 | if (is_udp)
|
---|
1485 | {
|
---|
1486 | so = udp_listen(pData, htons(host_port), guest_addr.s_addr,
|
---|
1487 | htons(guest_port), 0);
|
---|
1488 | }
|
---|
1489 | else
|
---|
1490 | {
|
---|
1491 | so = solisten(pData, htons(host_port), guest_addr.s_addr,
|
---|
1492 | htons(guest_port), 0);
|
---|
1493 | }
|
---|
1494 | Log2(("NAT: redirecting socket %R[natsock]\n", so));
|
---|
1495 | return (so != NULL ? 0 : -1);
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
|
---|
1499 | int guest_port)
|
---|
1500 | {
|
---|
1501 | return add_exec(&exec_list, do_pty, (char *)args,
|
---|
1502 | addr_low_byte, htons(guest_port));
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
|
---|
1506 | {
|
---|
1507 | memcpy(client_ethaddr, ethaddr, ETH_ALEN);
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
1511 | HANDLE *slirp_get_events(PNATState pData)
|
---|
1512 | {
|
---|
1513 | return pData->phEvents;
|
---|
1514 | }
|
---|
1515 | void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
|
---|
1516 | {
|
---|
1517 | pData->phEvents[index] = hEvent;
|
---|
1518 | }
|
---|
1519 | #endif
|
---|
1520 |
|
---|
1521 | unsigned int slirp_get_timeout_ms(PNATState pData)
|
---|
1522 | {
|
---|
1523 | if (link_up)
|
---|
1524 | {
|
---|
1525 | if (time_fasttimo)
|
---|
1526 | return 2;
|
---|
1527 | if (do_slowtimo)
|
---|
1528 | return 500; /* see PR_SLOWHZ */
|
---|
1529 | }
|
---|
1530 | return 0;
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | #ifndef RT_OS_WINDOWS
|
---|
1534 | int slirp_get_nsock(PNATState pData)
|
---|
1535 | {
|
---|
1536 | return pData->nsock;
|
---|
1537 | }
|
---|
1538 | #endif
|
---|
1539 |
|
---|
1540 | /*
|
---|
1541 | * this function called from NAT thread
|
---|
1542 | */
|
---|
1543 | void slirp_post_sent(PNATState pData, void *pvArg)
|
---|
1544 | {
|
---|
1545 | struct socket *so = 0;
|
---|
1546 | struct tcpcb *tp = 0;
|
---|
1547 | struct mbuf *m = (struct mbuf *)pvArg;
|
---|
1548 | m_free(pData, m);
|
---|
1549 | }
|
---|
1550 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1551 | void slirp_process_queue(PNATState pData)
|
---|
1552 | {
|
---|
1553 | RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
|
---|
1554 | }
|
---|
1555 | void *slirp_get_queue(PNATState pData)
|
---|
1556 | {
|
---|
1557 | return pData->pReqQueue;
|
---|
1558 | }
|
---|
1559 | #endif
|
---|
1560 |
|
---|
1561 | uint16_t slirp_get_service(int proto, uint16_t dport, uint16_t sport)
|
---|
1562 | {
|
---|
1563 | uint16_t hdport, hsport, service;
|
---|
1564 | hdport = ntohs(dport);
|
---|
1565 | hsport = ntohs(sport);
|
---|
1566 | Log2(("proto: %d, dport: %d sport: %d\n", proto, hdport, hsport));
|
---|
1567 | service = 0;
|
---|
1568 | switch (hdport)
|
---|
1569 | {
|
---|
1570 | case 500:
|
---|
1571 | if (hsport == 500) service = 500;
|
---|
1572 | break;
|
---|
1573 | }
|
---|
1574 | Log2(("service : %d\n", service));
|
---|
1575 | return htons(service);
|
---|
1576 | }
|
---|