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 | #include <iprt/file.h>
|
---|
10 | #ifndef RT_OS_WINDOWS
|
---|
11 | # include <sys/ioctl.h>
|
---|
12 | # include <poll.h>
|
---|
13 | #else
|
---|
14 | # include <Winnls.h>
|
---|
15 | # define _WINSOCK2API_
|
---|
16 | # include <IPHlpApi.h>
|
---|
17 | #endif
|
---|
18 | #include <alias.h>
|
---|
19 |
|
---|
20 | #if !defined(RT_OS_WINDOWS)
|
---|
21 |
|
---|
22 | # define DO_ENGAGE_EVENT1(so, fdset, label) \
|
---|
23 | do { \
|
---|
24 | if( so->so_poll_index != -1 \
|
---|
25 | && so->s == polls[so->so_poll_index].fd) { \
|
---|
26 | polls[so->so_poll_index].events |= N_(fdset ## _poll); \
|
---|
27 | break; /* out of this loop */ \
|
---|
28 | } \
|
---|
29 | AssertRelease(poll_index < (nfds)); \
|
---|
30 | AssertRelease(poll_index >= 0 && poll_index < (nfds)); \
|
---|
31 | polls[poll_index].fd = (so)->s; \
|
---|
32 | (so)->so_poll_index = poll_index; \
|
---|
33 | polls[poll_index].events = N_(fdset ## _poll); \
|
---|
34 | polls[poll_index].revents = 0; \
|
---|
35 | poll_index++; \
|
---|
36 | } while(0)
|
---|
37 |
|
---|
38 |
|
---|
39 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
40 | do { \
|
---|
41 | if( so->so_poll_index != -1 \
|
---|
42 | && so->s == polls[so->so_poll_index].fd) { \
|
---|
43 | polls[so->so_poll_index].events |= \
|
---|
44 | N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
|
---|
45 | break; /* out of this loop */ \
|
---|
46 | } \
|
---|
47 | AssertRelease(poll_index < (nfds)); \
|
---|
48 | polls[poll_index].fd = (so)->s; \
|
---|
49 | (so)->so_poll_index = poll_index; \
|
---|
50 | polls[poll_index].events = \
|
---|
51 | N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
|
---|
52 | poll_index++; \
|
---|
53 | } while(0)
|
---|
54 |
|
---|
55 | # define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
|
---|
56 |
|
---|
57 | # define DO_CHECK_FD_SET(so, events, fdset) ( ((so)->so_poll_index != -1) \
|
---|
58 | && ((so)->so_poll_index <= ndfs) \
|
---|
59 | && ((so)->s == polls[so->so_poll_index].fd) \
|
---|
60 | && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)))
|
---|
61 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
|
---|
62 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
|
---|
63 |
|
---|
64 | # ifndef RT_OS_WINDOWS
|
---|
65 |
|
---|
66 | # ifndef RT_OS_LINUX
|
---|
67 | # define readfds_poll (POLLRDNORM)
|
---|
68 | # define writefds_poll (POLLWRNORM)
|
---|
69 | # define xfds_poll (POLLRDBAND|POLLWRBAND|POLLPRI)
|
---|
70 | # else
|
---|
71 | # define readfds_poll (POLLIN)
|
---|
72 | # define writefds_poll (POLLOUT)
|
---|
73 | # define xfds_poll (POLLPRI)
|
---|
74 | # endif
|
---|
75 | # define rderr_poll (POLLERR)
|
---|
76 | # define rdhup_poll (POLLHUP)
|
---|
77 | # define nval_poll (POLLNVAL)
|
---|
78 |
|
---|
79 | # define ICMP_ENGAGE_EVENT(so, fdset) \
|
---|
80 | do { \
|
---|
81 | if (pData->icmp_socket.s != -1) \
|
---|
82 | DO_ENGAGE_EVENT1((so), fdset, ICMP); \
|
---|
83 | } while (0)
|
---|
84 | # else /* !RT_OS_WINDOWS */
|
---|
85 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
|
---|
86 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
87 | #endif /* RT_OS_WINDOWS */
|
---|
88 |
|
---|
89 | #else /* defined(RT_OS_WINDOWS) */
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
|
---|
93 | * So no call to WSAEventSelect necessary.
|
---|
94 | */
|
---|
95 | # define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
|
---|
96 |
|
---|
97 | # define DO_ENGAGE_EVENT1(so, fdset1, label) \
|
---|
98 | do { \
|
---|
99 | rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
|
---|
100 | if (rc == SOCKET_ERROR) \
|
---|
101 | { \
|
---|
102 | /* This should not happen */ \
|
---|
103 | error = WSAGetLastError(); \
|
---|
104 | LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
|
---|
105 | error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
|
---|
106 | } \
|
---|
107 | } while(0); \
|
---|
108 | CONTINUE(label)
|
---|
109 |
|
---|
110 | # define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
|
---|
111 | DO_ENGAGE_EVENT1((so), (fdset1), label)
|
---|
112 |
|
---|
113 | # define DO_POLL_EVENTS(rc, error, so, events, label) \
|
---|
114 | (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
|
---|
115 | if ((rc) == SOCKET_ERROR) \
|
---|
116 | { \
|
---|
117 | (error) = WSAGetLastError(); \
|
---|
118 | LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
|
---|
119 | CONTINUE(label); \
|
---|
120 | }
|
---|
121 |
|
---|
122 | # define acceptds_win FD_ACCEPT
|
---|
123 | # define acceptds_win_bit FD_ACCEPT_BIT
|
---|
124 |
|
---|
125 | # define readfds_win FD_READ
|
---|
126 | # define readfds_win_bit FD_READ_BIT
|
---|
127 |
|
---|
128 | # define writefds_win FD_WRITE
|
---|
129 | # define writefds_win_bit FD_WRITE_BIT
|
---|
130 |
|
---|
131 | # define xfds_win FD_OOB
|
---|
132 | # define xfds_win_bit FD_OOB_BIT
|
---|
133 |
|
---|
134 | # define DO_CHECK_FD_SET(so, events, fdset) \
|
---|
135 | (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
|
---|
136 |
|
---|
137 | # define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
|
---|
138 | # define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 1 /*specific for Unix API */
|
---|
139 |
|
---|
140 | #endif /* defined(RT_OS_WINDOWS) */
|
---|
141 |
|
---|
142 | #define TCP_ENGAGE_EVENT1(so, fdset) \
|
---|
143 | DO_ENGAGE_EVENT1((so), fdset, tcp)
|
---|
144 |
|
---|
145 | #define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
|
---|
146 | DO_ENGAGE_EVENT2((so), fdset1, fdset2, tcp)
|
---|
147 |
|
---|
148 | #define UDP_ENGAGE_EVENT(so, fdset) \
|
---|
149 | DO_ENGAGE_EVENT1((so), fdset, udp)
|
---|
150 |
|
---|
151 | #define POLL_TCP_EVENTS(rc, error, so, events) \
|
---|
152 | DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
|
---|
153 |
|
---|
154 | #define POLL_UDP_EVENTS(rc, error, so, events) \
|
---|
155 | DO_POLL_EVENTS((rc), (error), (so), (events), udp)
|
---|
156 |
|
---|
157 | #define CHECK_FD_SET(so, events, set) \
|
---|
158 | (DO_CHECK_FD_SET((so), (events), set))
|
---|
159 |
|
---|
160 | #define WIN_CHECK_FD_SET(so, events, set) \
|
---|
161 | (DO_WIN_CHECK_FD_SET((so), (events), set))
|
---|
162 | #define UNIX_CHECK_FD_SET(so, events, set) \
|
---|
163 | (DO_UNIX_CHECK_FD_SET(so, events, set))
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Loging macros
|
---|
167 | */
|
---|
168 | #if VBOX_WITH_DEBUG_NAT_SOCKETS
|
---|
169 | # if defined(RT_OS_WINDOWS)
|
---|
170 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
171 | do { \
|
---|
172 | LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
|
---|
173 | } while (0)
|
---|
174 | # else /* RT_OS_WINDOWS */
|
---|
175 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
|
---|
176 | do { \
|
---|
177 | LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
|
---|
178 | CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
|
---|
179 | CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
|
---|
180 | CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
|
---|
181 | CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
|
---|
182 | CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
|
---|
183 | CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
|
---|
184 | } while (0)
|
---|
185 | # endif /* !RT_OS_WINDOWS */
|
---|
186 | #else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
187 | # define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
|
---|
188 | #endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
|
---|
189 |
|
---|
190 | #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)
|
---|
191 |
|
---|
192 | static void activate_port_forwarding(PNATState, struct ethhdr *);
|
---|
193 | static uint32_t find_guest_ip(PNATState, const uint8_t *);
|
---|
194 |
|
---|
195 | static const uint8_t special_ethaddr[6] =
|
---|
196 | {
|
---|
197 | 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
|
---|
198 | };
|
---|
199 |
|
---|
200 | static const uint8_t broadcast_ethaddr[6] =
|
---|
201 | {
|
---|
202 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
---|
203 | };
|
---|
204 |
|
---|
205 | const uint8_t zerro_ethaddr[6] =
|
---|
206 | {
|
---|
207 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
|
---|
208 | };
|
---|
209 |
|
---|
210 | #ifdef RT_OS_WINDOWS
|
---|
211 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
212 | struct in_addr *pdns_addr,
|
---|
213 | const char **ppszDomain)
|
---|
214 | {
|
---|
215 | /* Get amount of memory required for operation */
|
---|
216 | ULONG flags = GAA_FLAG_INCLUDE_PREFIX; /*GAA_FLAG_INCLUDE_ALL_INTERFACES;*/ /* all interfaces registered in NDIS */
|
---|
217 | PIP_ADAPTER_ADDRESSES addresses = NULL;
|
---|
218 | PIP_ADAPTER_ADDRESSES addr = NULL;
|
---|
219 | PIP_ADAPTER_DNS_SERVER_ADDRESS dns = NULL;
|
---|
220 | ULONG size = 0;
|
---|
221 | int wlen = 0;
|
---|
222 | char *suffix;
|
---|
223 | struct dns_entry *da = NULL;
|
---|
224 | struct dns_domain_entry *dd = NULL;
|
---|
225 | ULONG ret = ERROR_SUCCESS;
|
---|
226 |
|
---|
227 | /* @todo add SKIPing flags to get only required information */
|
---|
228 |
|
---|
229 | ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
|
---|
230 | if (ret != ERROR_BUFFER_OVERFLOW)
|
---|
231 | {
|
---|
232 | LogRel(("NAT: error %lu occurred on capacity detection operation\n", ret));
|
---|
233 | return -1;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (size == 0)
|
---|
237 | {
|
---|
238 | LogRel(("NAT: Win socket API returns non capacity\n"));
|
---|
239 | return -1;
|
---|
240 | }
|
---|
241 |
|
---|
242 | addresses = RTMemAllocZ(size);
|
---|
243 | if (addresses == NULL)
|
---|
244 | {
|
---|
245 | LogRel(("NAT: No memory available \n"));
|
---|
246 | return -1;
|
---|
247 | }
|
---|
248 |
|
---|
249 | ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
|
---|
250 | if (ret != ERROR_SUCCESS)
|
---|
251 | {
|
---|
252 | LogRel(("NAT: error %lu occurred on fetching adapters info\n", ret));
|
---|
253 | RTMemFree(addresses);
|
---|
254 | return -1;
|
---|
255 | }
|
---|
256 | addr = addresses;
|
---|
257 | while(addr != NULL)
|
---|
258 | {
|
---|
259 | int found;
|
---|
260 | if (addr->OperStatus != IfOperStatusUp)
|
---|
261 | goto next;
|
---|
262 | dns = addr->FirstDnsServerAddress;
|
---|
263 | while (dns != NULL)
|
---|
264 | {
|
---|
265 | struct sockaddr *saddr = dns->Address.lpSockaddr;
|
---|
266 | if (saddr->sa_family != AF_INET)
|
---|
267 | goto next_dns;
|
---|
268 | /* add dns server to list */
|
---|
269 | da = RTMemAllocZ(sizeof(struct dns_entry));
|
---|
270 | if (da == NULL)
|
---|
271 | {
|
---|
272 | LogRel(("NAT: Can't allocate buffer for DNS entry\n"));
|
---|
273 | RTMemFree(addresses);
|
---|
274 | return VERR_NO_MEMORY;
|
---|
275 | }
|
---|
276 | LogRel(("NAT: adding %R[IP4] to DNS server list\n", &((struct sockaddr_in *)saddr)->sin_addr));
|
---|
277 | if ((((struct sockaddr_in *)saddr)->sin_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
278 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
279 | }
|
---|
280 | else
|
---|
281 | {
|
---|
282 | da->de_addr.s_addr = ((struct sockaddr_in *)saddr)->sin_addr.s_addr;
|
---|
283 | }
|
---|
284 | TAILQ_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
285 |
|
---|
286 | if (addr->DnsSuffix == NULL)
|
---|
287 | goto next_dns;
|
---|
288 |
|
---|
289 | /*uniq*/
|
---|
290 | RTUtf16ToUtf8(addr->DnsSuffix, &suffix);
|
---|
291 |
|
---|
292 | if (!suffix || strlen(suffix) == 0) {
|
---|
293 | RTStrFree(suffix);
|
---|
294 | goto next_dns;
|
---|
295 | }
|
---|
296 |
|
---|
297 | found = 0;
|
---|
298 | LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
|
---|
299 | {
|
---|
300 | if ( dd->dd_pszDomain != NULL
|
---|
301 | && strcmp(dd->dd_pszDomain, suffix) == 0)
|
---|
302 | {
|
---|
303 | found = 1;
|
---|
304 | RTStrFree(suffix);
|
---|
305 | break;
|
---|
306 | }
|
---|
307 | }
|
---|
308 | if (found == 0)
|
---|
309 | {
|
---|
310 | dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
|
---|
311 | if (dd == NULL)
|
---|
312 | {
|
---|
313 | LogRel(("NAT: not enough memory\n"));
|
---|
314 | RTStrFree(suffix);
|
---|
315 | RTMemFree(addresses);
|
---|
316 | return VERR_NO_MEMORY;
|
---|
317 | }
|
---|
318 | dd->dd_pszDomain = suffix;
|
---|
319 | LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
|
---|
320 | LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
|
---|
321 | }
|
---|
322 | next_dns:
|
---|
323 | dns = dns->Next;
|
---|
324 | }
|
---|
325 | next:
|
---|
326 | addr = addr->Next;
|
---|
327 | }
|
---|
328 | RTMemFree(addresses);
|
---|
329 | return 0;
|
---|
330 | }
|
---|
331 |
|
---|
332 | #else /* !RT_OS_WINDOWS */
|
---|
333 |
|
---|
334 | static int RTFileGets(RTFILE File, void *pvBuf, size_t cbBufSize, size_t *pcbRead)
|
---|
335 | {
|
---|
336 | size_t cbRead;
|
---|
337 | char bTest;
|
---|
338 | int rc = VERR_NO_MEMORY;
|
---|
339 | char *pu8Buf = (char *)pvBuf;
|
---|
340 | *pcbRead = 0;
|
---|
341 | while( RT_SUCCESS(rc = RTFileRead(File, &bTest, 1, &cbRead))
|
---|
342 | && (pu8Buf - (char *)pvBuf) < cbBufSize)
|
---|
343 | {
|
---|
344 | if (cbRead == 0)
|
---|
345 | return VERR_EOF;
|
---|
346 | if (bTest == '\r' || bTest == '\n')
|
---|
347 | {
|
---|
348 | *pu8Buf = 0;
|
---|
349 | return VINF_SUCCESS;
|
---|
350 | }
|
---|
351 | *pu8Buf = bTest;
|
---|
352 | pu8Buf++;
|
---|
353 | (*pcbRead)++;
|
---|
354 | }
|
---|
355 | return rc;
|
---|
356 | }
|
---|
357 | static int get_dns_addr_domain(PNATState pData, bool fVerbose,
|
---|
358 | struct in_addr *pdns_addr,
|
---|
359 | const char **ppszDomain)
|
---|
360 | {
|
---|
361 | char buff[512];
|
---|
362 | char buff2[256];
|
---|
363 | RTFILE f;
|
---|
364 | int found = 0;
|
---|
365 | struct in_addr tmp_addr;
|
---|
366 | int rc;
|
---|
367 | size_t bytes;
|
---|
368 |
|
---|
369 | #ifdef RT_OS_OS2
|
---|
370 | /* Try various locations. */
|
---|
371 | char *etc = getenv("ETC");
|
---|
372 | if (etc)
|
---|
373 | {
|
---|
374 | RTStrmPrintf(buff, sizeof(buff), "%s/RESOLV2", etc);
|
---|
375 | rc = RTFileOpen(&f, buff, RTFILE_O_READ);
|
---|
376 | }
|
---|
377 | if (RT_FAILURE(rc))
|
---|
378 | {
|
---|
379 | RTStrmPrintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
|
---|
380 | rc = RTFileOpen(&f, buff, RTFILE_O_READ);
|
---|
381 | }
|
---|
382 | if (RT_FAILURE(rc))
|
---|
383 | {
|
---|
384 | RTStrmPrintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
|
---|
385 | rc = RTFileOpen(&f, buff, RTFILE_O_READ);
|
---|
386 | }
|
---|
387 | #else
|
---|
388 | # ifndef DEBUG_vvl
|
---|
389 | rc = RTFileOpen(&f, "/etc/resolv.conf", RTFILE_O_READ);
|
---|
390 | # else
|
---|
391 | char *home = getenv("HOME");
|
---|
392 | RTStrPrintf(buff, sizeof(buff), "%s/resolv.conf", home);
|
---|
393 | rc = RTFileOpen(&f, buff, RTFILE_O_READ);
|
---|
394 | if (RT_SUCCESS(rc))
|
---|
395 | {
|
---|
396 | Log(("NAT: DNS we're using %s\n", buff));
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | rc = RTFileOpen(&f, "/etc/resolv.conf", RTFILE_O_READ);
|
---|
401 | Log(("NAT: DNS we're using %s\n", buff));
|
---|
402 | }
|
---|
403 | # endif
|
---|
404 | #endif
|
---|
405 | if (RT_FAILURE(rc))
|
---|
406 | return -1;
|
---|
407 |
|
---|
408 | if (ppszDomain)
|
---|
409 | *ppszDomain = NULL;
|
---|
410 | Log(("nat: DNS Servers:\n"));
|
---|
411 | while ( RT_SUCCESS(rc = RTFileGets(f, buff, 512, &bytes))
|
---|
412 | && rc != VERR_EOF)
|
---|
413 | {
|
---|
414 | struct dns_entry *da = NULL;
|
---|
415 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
|
---|
416 | {
|
---|
417 | if (!inet_aton(buff2, &tmp_addr))
|
---|
418 | continue;
|
---|
419 | /*localhost mask */
|
---|
420 | da = RTMemAllocZ(sizeof (struct dns_entry));
|
---|
421 | if (da == NULL)
|
---|
422 | {
|
---|
423 | LogRel(("can't alloc memory for DNS entry\n"));
|
---|
424 | return -1;
|
---|
425 | }
|
---|
426 | /*check */
|
---|
427 | da->de_addr.s_addr = tmp_addr.s_addr;
|
---|
428 | if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
|
---|
429 | da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
430 | }
|
---|
431 | TAILQ_INSERT_HEAD(&pData->dns_list_head, da, de_list);
|
---|
432 | found++;
|
---|
433 | }
|
---|
434 | if ((!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
|
---|
435 | {
|
---|
436 | char *tok;
|
---|
437 | char *saveptr;
|
---|
438 | struct dns_domain_entry *dd = NULL;
|
---|
439 | int found = 0;
|
---|
440 | tok = strtok_r(&buff[6], " \t\n", &saveptr);
|
---|
441 | LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
|
---|
442 | {
|
---|
443 | if( tok != NULL
|
---|
444 | && strcmp(tok, dd->dd_pszDomain) == 0)
|
---|
445 | {
|
---|
446 | found = 1;
|
---|
447 | break;
|
---|
448 | }
|
---|
449 | }
|
---|
450 | if (tok != NULL && found == 0) {
|
---|
451 | dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
|
---|
452 | if (dd == NULL)
|
---|
453 | {
|
---|
454 | LogRel(("NAT: not enought memory to add domain list\n"));
|
---|
455 | return VERR_NO_MEMORY;
|
---|
456 | }
|
---|
457 | dd->dd_pszDomain = RTStrDup(tok);
|
---|
458 | LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
|
---|
459 | LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
|
---|
460 | }
|
---|
461 | }
|
---|
462 | }
|
---|
463 | RTFileClose(f);
|
---|
464 | if (!found)
|
---|
465 | return -1;
|
---|
466 | return 0;
|
---|
467 | }
|
---|
468 |
|
---|
469 | #endif
|
---|
470 |
|
---|
471 | static int slirp_init_dns_list(PNATState pData)
|
---|
472 | {
|
---|
473 | TAILQ_INIT(&pData->dns_list_head);
|
---|
474 | LIST_INIT(&pData->dns_domain_list_head);
|
---|
475 | return get_dns_addr_domain(pData, true, NULL, NULL);
|
---|
476 | }
|
---|
477 |
|
---|
478 | static void slirp_release_dns_list(PNATState pData)
|
---|
479 | {
|
---|
480 | struct dns_entry *de = NULL;
|
---|
481 | struct dns_domain_entry *dd = NULL;
|
---|
482 | while(!TAILQ_EMPTY(&pData->dns_list_head)) {
|
---|
483 | de = TAILQ_FIRST(&pData->dns_list_head);
|
---|
484 | TAILQ_REMOVE(&pData->dns_list_head, de, de_list);
|
---|
485 | RTMemFree(de);
|
---|
486 | }
|
---|
487 | while(!LIST_EMPTY(&pData->dns_domain_list_head)) {
|
---|
488 | dd = LIST_FIRST(&pData->dns_domain_list_head);
|
---|
489 | LIST_REMOVE(dd, dd_list);
|
---|
490 | if (dd->dd_pszDomain != NULL)
|
---|
491 | RTStrFree(dd->dd_pszDomain);
|
---|
492 | RTMemFree(dd);
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
|
---|
497 | {
|
---|
498 | return get_dns_addr_domain(pData, false, pdns_addr, NULL);
|
---|
499 | }
|
---|
500 |
|
---|
501 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
502 | int slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
|
---|
503 | bool fPassDomain, void *pvUser)
|
---|
504 | #else
|
---|
505 | int slirp_init(PNATState *ppData, uint32_t u32NetAddr, uint32_t u32Netmask,
|
---|
506 | bool fPassDomain, void *pvUser)
|
---|
507 | #endif
|
---|
508 | {
|
---|
509 | int fNATfailed = 0;
|
---|
510 | int rc;
|
---|
511 | PNATState pData = RTMemAllocZ(sizeof(NATState));
|
---|
512 | *ppData = pData;
|
---|
513 | if (!pData)
|
---|
514 | return VERR_NO_MEMORY;
|
---|
515 | if (u32Netmask & 0x1f)
|
---|
516 | /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
|
---|
517 | return VERR_INVALID_PARAMETER;
|
---|
518 | pData->fPassDomain = fPassDomain;
|
---|
519 | pData->pvUser = pvUser;
|
---|
520 | pData->netmask = u32Netmask;
|
---|
521 |
|
---|
522 | /* sockets & TCP defaults */
|
---|
523 | pData->socket_rcv = 64 * _1K;
|
---|
524 | pData->socket_snd = 64 * _1K;
|
---|
525 | tcp_sndspace = 64 * _1K;
|
---|
526 | tcp_rcvspace = 64 * _1K;
|
---|
527 |
|
---|
528 | #ifdef RT_OS_WINDOWS
|
---|
529 | {
|
---|
530 | WSADATA Data;
|
---|
531 | WSAStartup(MAKEWORD(2, 0), &Data);
|
---|
532 | }
|
---|
533 | pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
534 | #endif
|
---|
535 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
536 | QSOCKET_LOCK_CREATE(tcb);
|
---|
537 | QSOCKET_LOCK_CREATE(udb);
|
---|
538 | rc = RTReqCreateQueue(&pData->pReqQueue);
|
---|
539 | AssertReleaseRC(rc);
|
---|
540 | #endif
|
---|
541 |
|
---|
542 | link_up = 1;
|
---|
543 |
|
---|
544 | rc = bootp_dhcp_init(pData);
|
---|
545 | if (rc != 0)
|
---|
546 | {
|
---|
547 | LogRel(("NAT: DHCP server initialization was failed\n"));
|
---|
548 | return VINF_NAT_DNS;
|
---|
549 | }
|
---|
550 | debug_init();
|
---|
551 | if_init(pData);
|
---|
552 | ip_init(pData);
|
---|
553 | icmp_init(pData);
|
---|
554 |
|
---|
555 | /* Initialise mbufs *after* setting the MTU */
|
---|
556 | m_init(pData);
|
---|
557 |
|
---|
558 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
559 | inet_aton(pszNetAddr, &special_addr);
|
---|
560 | #else
|
---|
561 | special_addr.s_addr = u32NetAddr;
|
---|
562 | #endif
|
---|
563 | pData->slirp_ethaddr = &special_ethaddr[0];
|
---|
564 | alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
|
---|
565 | /* @todo: add ability to configure this staff */
|
---|
566 |
|
---|
567 | /* set default addresses */
|
---|
568 | inet_aton("127.0.0.1", &loopback_addr);
|
---|
569 | if (slirp_init_dns_list(pData) < 0)
|
---|
570 | fNATfailed = 1;
|
---|
571 |
|
---|
572 | dnsproxy_init(pData);
|
---|
573 |
|
---|
574 | getouraddr(pData);
|
---|
575 | {
|
---|
576 | int flags = 0;
|
---|
577 | struct in_addr proxy_addr;
|
---|
578 | pData->proxy_alias = LibAliasInit(pData, NULL);
|
---|
579 | if (pData->proxy_alias == NULL)
|
---|
580 | {
|
---|
581 | LogRel(("NAT: LibAlias default rule wasn't initialized\n"));
|
---|
582 | AssertMsgFailed(("NAT: LibAlias default rule wasn't initialized\n"));
|
---|
583 | }
|
---|
584 | flags = LibAliasSetMode(pData->proxy_alias, 0, 0);
|
---|
585 | #ifndef NO_FW_PUNCH
|
---|
586 | flags |= PKT_ALIAS_PUNCH_FW;
|
---|
587 | #endif
|
---|
588 | flags |= PKT_ALIAS_LOG; /* set logging */
|
---|
589 | flags = LibAliasSetMode(pData->proxy_alias, flags, ~0);
|
---|
590 | proxy_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
591 | LibAliasSetAddress(pData->proxy_alias, proxy_addr);
|
---|
592 | ftp_alias_load(pData);
|
---|
593 | nbt_alias_load(pData);
|
---|
594 | dns_alias_load(pData);
|
---|
595 | }
|
---|
596 | return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Register statistics.
|
---|
601 | */
|
---|
602 | void slirp_register_statistics(PNATState pData, PPDMDRVINS pDrvIns)
|
---|
603 | {
|
---|
604 | #ifdef VBOX_WITH_STATISTICS
|
---|
605 | # define PROFILE_COUNTER(name, dsc) REGISTER_COUNTER(name, pData, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
|
---|
606 | # define COUNTING_COUNTER(name, dsc) REGISTER_COUNTER(name, pData, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
|
---|
607 | # include "counters.h"
|
---|
608 | # undef COUNTER
|
---|
609 | /** @todo register statistics for the variables dumped by:
|
---|
610 | * ipstats(pData); tcpstats(pData); udpstats(pData); icmpstats(pData);
|
---|
611 | * mbufstats(pData); sockstats(pData); */
|
---|
612 | #endif /* VBOX_WITH_STATISTICS */
|
---|
613 | }
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Deregister statistics.
|
---|
617 | */
|
---|
618 | void slirp_deregister_statistics(PNATState pData, PPDMDRVINS pDrvIns)
|
---|
619 | {
|
---|
620 | #ifdef VBOX_WITH_STATISTICS
|
---|
621 | # define PROFILE_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pData)
|
---|
622 | # define COUNTING_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pData)
|
---|
623 | # include "counters.h"
|
---|
624 | #endif /* VBOX_WITH_STATISTICS */
|
---|
625 | }
|
---|
626 |
|
---|
627 | /**
|
---|
628 | * Marks the link as up, making it possible to establish new connections.
|
---|
629 | */
|
---|
630 | void slirp_link_up(PNATState pData)
|
---|
631 | {
|
---|
632 | link_up = 1;
|
---|
633 | }
|
---|
634 |
|
---|
635 | /**
|
---|
636 | * Marks the link as down and cleans up the current connections.
|
---|
637 | */
|
---|
638 | void slirp_link_down(PNATState pData)
|
---|
639 | {
|
---|
640 | struct socket *so;
|
---|
641 |
|
---|
642 | while ((so = tcb.so_next) != &tcb)
|
---|
643 | {
|
---|
644 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
645 | sofree(pData, so);
|
---|
646 | else
|
---|
647 | tcp_drop(pData, sototcpcb(so), 0);
|
---|
648 | }
|
---|
649 |
|
---|
650 | while ((so = udb.so_next) != &udb)
|
---|
651 | udp_detach(pData, so);
|
---|
652 |
|
---|
653 | link_up = 0;
|
---|
654 | }
|
---|
655 |
|
---|
656 | /**
|
---|
657 | * Terminates the slirp component.
|
---|
658 | */
|
---|
659 | void slirp_term(PNATState pData)
|
---|
660 | {
|
---|
661 | #ifdef RT_OS_WINDOWS
|
---|
662 | pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
|
---|
663 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
664 | RTMemFree(pData->pvIcmpBuffer);
|
---|
665 | #else
|
---|
666 | closesocket(pData->icmp_socket.s);
|
---|
667 | #endif
|
---|
668 |
|
---|
669 | slirp_link_down(pData);
|
---|
670 | slirp_release_dns_list(pData);
|
---|
671 | ftp_alias_unload(pData);
|
---|
672 | nbt_alias_unload(pData);
|
---|
673 | dns_alias_unload(pData);
|
---|
674 | while(!LIST_EMPTY(&instancehead))
|
---|
675 | {
|
---|
676 | struct libalias *la = LIST_FIRST(&instancehead);
|
---|
677 | /* libalias do all clean up */
|
---|
678 | LibAliasUninit(la);
|
---|
679 | }
|
---|
680 | while(!LIST_EMPTY(&pData->arp_cache))
|
---|
681 | {
|
---|
682 | struct arp_cache_entry *ac = LIST_FIRST(&pData->arp_cache);
|
---|
683 | LIST_REMOVE(ac, list);
|
---|
684 | RTMemFree(ac);
|
---|
685 | }
|
---|
686 | bootp_dhcp_fini(pData);
|
---|
687 | #ifdef RT_OS_WINDOWS
|
---|
688 | WSACleanup();
|
---|
689 | #endif
|
---|
690 | #ifdef LOG_ENABLED
|
---|
691 | Log(("\n"
|
---|
692 | "NAT statistics\n"
|
---|
693 | "--------------\n"
|
---|
694 | "\n"));
|
---|
695 | ipstats(pData);
|
---|
696 | tcpstats(pData);
|
---|
697 | udpstats(pData);
|
---|
698 | icmpstats(pData);
|
---|
699 | mbufstats(pData);
|
---|
700 | sockstats(pData);
|
---|
701 | Log(("\n"
|
---|
702 | "\n"
|
---|
703 | "\n"));
|
---|
704 | #endif
|
---|
705 | RTMemFree(pData);
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
710 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
|
---|
711 |
|
---|
712 | /*
|
---|
713 | * curtime kept to an accuracy of 1ms
|
---|
714 | */
|
---|
715 | static void updtime(PNATState pData)
|
---|
716 | {
|
---|
717 | #ifdef RT_OS_WINDOWS
|
---|
718 | struct _timeb tb;
|
---|
719 |
|
---|
720 | _ftime(&tb);
|
---|
721 | curtime = (u_int)tb.time * (u_int)1000;
|
---|
722 | curtime += (u_int)tb.millitm;
|
---|
723 | #else
|
---|
724 | gettimeofday(&tt, 0);
|
---|
725 |
|
---|
726 | curtime = (u_int)tt.tv_sec * (u_int)1000;
|
---|
727 | curtime += (u_int)tt.tv_usec / (u_int)1000;
|
---|
728 |
|
---|
729 | if ((tt.tv_usec % 1000) >= 500)
|
---|
730 | curtime++;
|
---|
731 | #endif
|
---|
732 | }
|
---|
733 |
|
---|
734 | #ifdef RT_OS_WINDOWS
|
---|
735 | void slirp_select_fill(PNATState pData, int *pnfds)
|
---|
736 | #else /* RT_OS_WINDOWS */
|
---|
737 | void slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
|
---|
738 | #endif /* !RT_OS_WINDOWS */
|
---|
739 | {
|
---|
740 | struct socket *so, *so_next;
|
---|
741 | int nfds;
|
---|
742 | #if defined(RT_OS_WINDOWS)
|
---|
743 | int rc;
|
---|
744 | int error;
|
---|
745 | #else
|
---|
746 | int poll_index = 0;
|
---|
747 | #endif
|
---|
748 | int i;
|
---|
749 |
|
---|
750 | STAM_PROFILE_START(&pData->StatFill, a);
|
---|
751 |
|
---|
752 | nfds = *pnfds;
|
---|
753 |
|
---|
754 | /*
|
---|
755 | * First, TCP sockets
|
---|
756 | */
|
---|
757 | do_slowtimo = 0;
|
---|
758 | if (!link_up)
|
---|
759 | goto done;
|
---|
760 | /*
|
---|
761 | * *_slowtimo needs calling if there are IP fragments
|
---|
762 | * in the fragment queue, or there are TCP connections active
|
---|
763 | */
|
---|
764 | /* XXX:
|
---|
765 | * triggering of fragment expiration should be the same but use new macroses
|
---|
766 | */
|
---|
767 | do_slowtimo = (tcb.so_next != &tcb);
|
---|
768 | if (!do_slowtimo)
|
---|
769 | {
|
---|
770 | for (i = 0; i < IPREASS_NHASH; i++)
|
---|
771 | {
|
---|
772 | if (!TAILQ_EMPTY(&ipq[i]))
|
---|
773 | {
|
---|
774 | do_slowtimo = 1;
|
---|
775 | break;
|
---|
776 | }
|
---|
777 | }
|
---|
778 | }
|
---|
779 | ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
|
---|
780 |
|
---|
781 | STAM_COUNTER_RESET(&pData->StatTCP);
|
---|
782 | STAM_COUNTER_RESET(&pData->StatTCPHot);
|
---|
783 |
|
---|
784 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
785 | /* { */
|
---|
786 | #if !defined(RT_OS_WINDOWS)
|
---|
787 | so->so_poll_index = -1;
|
---|
788 | #endif
|
---|
789 | STAM_COUNTER_INC(&pData->StatTCP);
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * See if we need a tcp_fasttimo
|
---|
793 | */
|
---|
794 | if ( time_fasttimo == 0
|
---|
795 | && so->so_tcpcb != NULL
|
---|
796 | && so->so_tcpcb->t_flags & TF_DELACK)
|
---|
797 | time_fasttimo = curtime; /* Flag when we want a fasttimo */
|
---|
798 |
|
---|
799 | /*
|
---|
800 | * NOFDREF can include still connecting to local-host,
|
---|
801 | * newly socreated() sockets etc. Don't want to select these.
|
---|
802 | */
|
---|
803 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
804 | CONTINUE(tcp);
|
---|
805 |
|
---|
806 | /*
|
---|
807 | * Set for reading sockets which are accepting
|
---|
808 | */
|
---|
809 | if (so->so_state & SS_FACCEPTCONN)
|
---|
810 | {
|
---|
811 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
812 | TCP_ENGAGE_EVENT1(so, readfds);
|
---|
813 | CONTINUE(tcp);
|
---|
814 | }
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * Set for writing sockets which are connecting
|
---|
818 | */
|
---|
819 | if (so->so_state & SS_ISFCONNECTING)
|
---|
820 | {
|
---|
821 | Log2(("connecting %R[natsock] engaged\n",so));
|
---|
822 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
823 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
824 | }
|
---|
825 |
|
---|
826 | /*
|
---|
827 | * Set for writing if we are connected, can send more, and
|
---|
828 | * we have something to send
|
---|
829 | */
|
---|
830 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
|
---|
831 | {
|
---|
832 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
833 | TCP_ENGAGE_EVENT1(so, writefds);
|
---|
834 | }
|
---|
835 |
|
---|
836 | /*
|
---|
837 | * Set for reading (and urgent data) if we are connected, can
|
---|
838 | * receive more, and we have room for it XXX /2 ?
|
---|
839 | */
|
---|
840 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
|
---|
841 | {
|
---|
842 | STAM_COUNTER_INC(&pData->StatTCPHot);
|
---|
843 | TCP_ENGAGE_EVENT2(so, readfds, xfds);
|
---|
844 | }
|
---|
845 | LOOP_LABEL(tcp, so, so_next);
|
---|
846 | }
|
---|
847 |
|
---|
848 | /*
|
---|
849 | * UDP sockets
|
---|
850 | */
|
---|
851 | STAM_COUNTER_RESET(&pData->StatUDP);
|
---|
852 | STAM_COUNTER_RESET(&pData->StatUDPHot);
|
---|
853 |
|
---|
854 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
855 | /* { */
|
---|
856 |
|
---|
857 | STAM_COUNTER_INC(&pData->StatUDP);
|
---|
858 | #if !defined(RT_OS_WINDOWS)
|
---|
859 | so->so_poll_index = -1;
|
---|
860 | #endif
|
---|
861 |
|
---|
862 | /*
|
---|
863 | * See if it's timed out
|
---|
864 | */
|
---|
865 | if (so->so_expire)
|
---|
866 | {
|
---|
867 | if (so->so_expire <= curtime)
|
---|
868 | {
|
---|
869 | Log2(("NAT: %R[natsock] expired\n", so));
|
---|
870 | if (so->so_timeout != NULL)
|
---|
871 | {
|
---|
872 | so->so_timeout(pData, so, so->so_timeout_arg);
|
---|
873 | }
|
---|
874 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
875 | /* we need so_next for continue our cycle*/
|
---|
876 | so_next = so->so_next;
|
---|
877 | #endif
|
---|
878 | UDP_DETACH(pData, so, so_next);
|
---|
879 | CONTINUE_NO_UNLOCK(udp);
|
---|
880 | }
|
---|
881 | else
|
---|
882 | do_slowtimo = 1; /* Let socket expire */
|
---|
883 | }
|
---|
884 |
|
---|
885 | /*
|
---|
886 | * When UDP packets are received from over the link, they're
|
---|
887 | * sendto()'d straight away, so no need for setting for writing
|
---|
888 | * Limit the number of packets queued by this session to 4.
|
---|
889 | * Note that even though we try and limit this to 4 packets,
|
---|
890 | * the session could have more queued if the packets needed
|
---|
891 | * to be fragmented.
|
---|
892 | *
|
---|
893 | * (XXX <= 4 ?)
|
---|
894 | */
|
---|
895 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
|
---|
896 | {
|
---|
897 | STAM_COUNTER_INC(&pData->StatUDPHot);
|
---|
898 | UDP_ENGAGE_EVENT(so, readfds);
|
---|
899 | }
|
---|
900 | LOOP_LABEL(udp, so, so_next);
|
---|
901 | }
|
---|
902 | done:
|
---|
903 |
|
---|
904 | #if defined(RT_OS_WINDOWS)
|
---|
905 | *pnfds = VBOX_EVENT_COUNT;
|
---|
906 | #else /* RT_OS_WINDOWS */
|
---|
907 | AssertRelease(poll_index <= *pnfds);
|
---|
908 | *pnfds = poll_index;
|
---|
909 | #endif /* !RT_OS_WINDOWS */
|
---|
910 |
|
---|
911 | STAM_PROFILE_STOP(&pData->StatFill, a);
|
---|
912 | }
|
---|
913 |
|
---|
914 | #if defined(RT_OS_WINDOWS)
|
---|
915 | void slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
|
---|
916 | #else /* RT_OS_WINDOWS */
|
---|
917 | void slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
|
---|
918 | #endif /* !RT_OS_WINDOWS */
|
---|
919 | {
|
---|
920 | struct socket *so, *so_next;
|
---|
921 | int ret;
|
---|
922 | #if defined(RT_OS_WINDOWS)
|
---|
923 | WSANETWORKEVENTS NetworkEvents;
|
---|
924 | int rc;
|
---|
925 | int error;
|
---|
926 | #else
|
---|
927 | int poll_index = 0;
|
---|
928 | #endif
|
---|
929 |
|
---|
930 | STAM_PROFILE_START(&pData->StatPoll, a);
|
---|
931 |
|
---|
932 | /* Update time */
|
---|
933 | updtime(pData);
|
---|
934 |
|
---|
935 | /*
|
---|
936 | * See if anything has timed out
|
---|
937 | */
|
---|
938 | if (link_up)
|
---|
939 | {
|
---|
940 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
|
---|
941 | {
|
---|
942 | STAM_PROFILE_START(&pData->StatFastTimer, a);
|
---|
943 | tcp_fasttimo(pData);
|
---|
944 | time_fasttimo = 0;
|
---|
945 | STAM_PROFILE_STOP(&pData->StatFastTimer, a);
|
---|
946 | }
|
---|
947 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
|
---|
948 | {
|
---|
949 | STAM_PROFILE_START(&pData->StatSlowTimer, a);
|
---|
950 | ip_slowtimo(pData);
|
---|
951 | tcp_slowtimo(pData);
|
---|
952 | last_slowtimo = curtime;
|
---|
953 | STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
|
---|
954 | }
|
---|
955 | }
|
---|
956 | #if defined(RT_OS_WINDOWS)
|
---|
957 | if (fTimeout)
|
---|
958 | return; /* only timer update */
|
---|
959 | #endif
|
---|
960 |
|
---|
961 | /*
|
---|
962 | * Check sockets
|
---|
963 | */
|
---|
964 | if (!link_up)
|
---|
965 | goto done;
|
---|
966 | #if defined(RT_OS_WINDOWS)
|
---|
967 | /*XXX: before renaming please make see define
|
---|
968 | * fIcmp in slirp_state.h
|
---|
969 | */
|
---|
970 | if (fIcmp)
|
---|
971 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
972 | #else
|
---|
973 | if ( (pData->icmp_socket.s != -1)
|
---|
974 | && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
|
---|
975 | sorecvfrom(pData, &pData->icmp_socket);
|
---|
976 | #endif
|
---|
977 | /*
|
---|
978 | * Check TCP sockets
|
---|
979 | */
|
---|
980 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
981 | /* { */
|
---|
982 |
|
---|
983 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
984 | if ( so->so_state & SS_NOFDREF
|
---|
985 | && so->so_deleted == 1)
|
---|
986 | {
|
---|
987 | struct socket *son, *sop = NULL;
|
---|
988 | QSOCKET_LOCK(tcb);
|
---|
989 | if (so->so_next != NULL)
|
---|
990 | {
|
---|
991 | if (so->so_next != &tcb)
|
---|
992 | SOCKET_LOCK(so->so_next);
|
---|
993 | son = so->so_next;
|
---|
994 | }
|
---|
995 | if ( so->so_prev != &tcb
|
---|
996 | && so->so_prev != NULL)
|
---|
997 | {
|
---|
998 | SOCKET_LOCK(so->so_prev);
|
---|
999 | sop = so->so_prev;
|
---|
1000 | }
|
---|
1001 | QSOCKET_UNLOCK(tcb);
|
---|
1002 | remque(pData, so);
|
---|
1003 | NSOCK_DEC();
|
---|
1004 | SOCKET_UNLOCK(so);
|
---|
1005 | SOCKET_LOCK_DESTROY(so);
|
---|
1006 | RTMemFree(so);
|
---|
1007 | so_next = son;
|
---|
1008 | if (sop != NULL)
|
---|
1009 | SOCKET_UNLOCK(sop);
|
---|
1010 | CONTINUE_NO_UNLOCK(tcp);
|
---|
1011 | }
|
---|
1012 | #endif
|
---|
1013 | /*
|
---|
1014 | * FD_ISSET is meaningless on these sockets
|
---|
1015 | * (and they can crash the program)
|
---|
1016 | */
|
---|
1017 | if (so->so_state & SS_NOFDREF || so->s == -1)
|
---|
1018 | CONTINUE(tcp);
|
---|
1019 |
|
---|
1020 | POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
1021 |
|
---|
1022 | LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
|
---|
1023 |
|
---|
1024 |
|
---|
1025 | /*
|
---|
1026 | * Check for URG data
|
---|
1027 | * This will soread as well, so no need to
|
---|
1028 | * test for readfds below if this succeeds
|
---|
1029 | */
|
---|
1030 |
|
---|
1031 | /* out-of-band data */
|
---|
1032 | if (CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
1033 | {
|
---|
1034 | sorecvoob(pData, so);
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | /*
|
---|
1038 | * Check sockets for reading
|
---|
1039 | */
|
---|
1040 | else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
1041 | || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
|
---|
1042 | {
|
---|
1043 | /*
|
---|
1044 | * Check for incoming connections
|
---|
1045 | */
|
---|
1046 | if (so->so_state & SS_FACCEPTCONN)
|
---|
1047 | {
|
---|
1048 | TCP_CONNECT(pData, so);
|
---|
1049 | #if defined(RT_OS_WINDOWS)
|
---|
1050 | if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
|
---|
1051 | #endif
|
---|
1052 | CONTINUE(tcp);
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | ret = soread(pData, so);
|
---|
1056 | /* Output it if we read something */
|
---|
1057 | if (RT_LIKELY(ret > 0))
|
---|
1058 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | #if defined(RT_OS_WINDOWS)
|
---|
1062 | /*
|
---|
1063 | * Check for FD_CLOSE events.
|
---|
1064 | * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
|
---|
1065 | */
|
---|
1066 | if ( (NetworkEvents.lNetworkEvents & FD_CLOSE)
|
---|
1067 | || (so->so_close == 1))
|
---|
1068 | {
|
---|
1069 | so->so_close = 1; /* mark it */
|
---|
1070 | /*
|
---|
1071 | * drain the socket
|
---|
1072 | */
|
---|
1073 | for (;;)
|
---|
1074 | {
|
---|
1075 | ret = soread(pData, so);
|
---|
1076 | if (ret > 0)
|
---|
1077 | TCP_OUTPUT(pData, sototcpcb(so));
|
---|
1078 | else
|
---|
1079 | break;
|
---|
1080 | }
|
---|
1081 | CONTINUE(tcp);
|
---|
1082 | }
|
---|
1083 | #endif
|
---|
1084 |
|
---|
1085 | /*
|
---|
1086 | * Check sockets for writing
|
---|
1087 | */
|
---|
1088 | if (CHECK_FD_SET(so, NetworkEvents, writefds))
|
---|
1089 | {
|
---|
1090 | /*
|
---|
1091 | * Check for non-blocking, still-connecting sockets
|
---|
1092 | */
|
---|
1093 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1094 | {
|
---|
1095 | Log2(("connecting %R[natsock] catched\n", so));
|
---|
1096 | /* Connected */
|
---|
1097 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
1098 |
|
---|
1099 | /*
|
---|
1100 | * This should be probably guarded by PROBE_CONN too. Anyway,
|
---|
1101 | * we disable it on OS/2 because the below send call returns
|
---|
1102 | * EFAULT which causes the opened TCP socket to close right
|
---|
1103 | * after it has been opened and connected.
|
---|
1104 | */
|
---|
1105 | #ifndef RT_OS_OS2
|
---|
1106 | ret = send(so->s, (const char *)&ret, 0, 0);
|
---|
1107 | if (ret < 0)
|
---|
1108 | {
|
---|
1109 | /* XXXXX Must fix, zero bytes is a NOP */
|
---|
1110 | if ( errno == EAGAIN
|
---|
1111 | || errno == EWOULDBLOCK
|
---|
1112 | || errno == EINPROGRESS
|
---|
1113 | || errno == ENOTCONN)
|
---|
1114 | CONTINUE(tcp);
|
---|
1115 |
|
---|
1116 | /* else failed */
|
---|
1117 | so->so_state = SS_NOFDREF;
|
---|
1118 | }
|
---|
1119 | /* else so->so_state &= ~SS_ISFCONNECTING; */
|
---|
1120 | #endif
|
---|
1121 |
|
---|
1122 | /*
|
---|
1123 | * Continue tcp_input
|
---|
1124 | */
|
---|
1125 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1126 | /* continue; */
|
---|
1127 | }
|
---|
1128 | else
|
---|
1129 | SOWRITE(ret, pData, so);
|
---|
1130 | /*
|
---|
1131 | * XXX If we wrote something (a lot), there could be the need
|
---|
1132 | * for a window update. In the worst case, the remote will send
|
---|
1133 | * a window probe to get things going again.
|
---|
1134 | */
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | /*
|
---|
1138 | * Probe a still-connecting, non-blocking socket
|
---|
1139 | * to check if it's still alive
|
---|
1140 | */
|
---|
1141 | #ifdef PROBE_CONN
|
---|
1142 | if (so->so_state & SS_ISFCONNECTING)
|
---|
1143 | {
|
---|
1144 | ret = recv(so->s, (char *)&ret, 0, 0);
|
---|
1145 |
|
---|
1146 | if (ret < 0)
|
---|
1147 | {
|
---|
1148 | /* XXX */
|
---|
1149 | if ( errno == EAGAIN
|
---|
1150 | || errno == EWOULDBLOCK
|
---|
1151 | || errno == EINPROGRESS
|
---|
1152 | || errno == ENOTCONN)
|
---|
1153 | {
|
---|
1154 | CONTINUE(tcp); /* Still connecting, continue */
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | /* else failed */
|
---|
1158 | so->so_state = SS_NOFDREF;
|
---|
1159 |
|
---|
1160 | /* tcp_input will take care of it */
|
---|
1161 | }
|
---|
1162 | else
|
---|
1163 | {
|
---|
1164 | ret = send(so->s, &ret, 0, 0);
|
---|
1165 | if (ret < 0)
|
---|
1166 | {
|
---|
1167 | /* XXX */
|
---|
1168 | if ( errno == EAGAIN
|
---|
1169 | || errno == EWOULDBLOCK
|
---|
1170 | || errno == EINPROGRESS
|
---|
1171 | || errno == ENOTCONN)
|
---|
1172 | {
|
---|
1173 | CONTINUE(tcp);
|
---|
1174 | }
|
---|
1175 | /* else failed */
|
---|
1176 | so->so_state = SS_NOFDREF;
|
---|
1177 | }
|
---|
1178 | else
|
---|
1179 | so->so_state &= ~SS_ISFCONNECTING;
|
---|
1180 |
|
---|
1181 | }
|
---|
1182 | TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
|
---|
1183 | } /* SS_ISFCONNECTING */
|
---|
1184 | #endif
|
---|
1185 | #ifndef RT_OS_WINDOWS
|
---|
1186 | if ( UNIX_CHECK_FD_SET(so, NetworkEvents, rdhup)
|
---|
1187 | || UNIX_CHECK_FD_SET(so, NetworkEvents, rderr))
|
---|
1188 | {
|
---|
1189 | int err;
|
---|
1190 | int inq, outq;
|
---|
1191 | int status;
|
---|
1192 | socklen_t optlen = sizeof(int);
|
---|
1193 | inq = outq = 0;
|
---|
1194 | status = getsockopt(so->s, SOL_SOCKET, SO_ERROR, &err, &optlen);
|
---|
1195 | if (status != 0)
|
---|
1196 | Log(("NAT: can't get error status from %R[natsock]\n", so));
|
---|
1197 | #ifndef RT_OS_SOLARIS
|
---|
1198 | status = ioctl(so->s, FIONREAD, &inq); /* tcp(7) recommends SIOCINQ which is Linux specific */
|
---|
1199 | if (status != 0 || status != EINVAL)
|
---|
1200 | {
|
---|
1201 | /* EINVAL returned if socket in listen state tcp(7)*/
|
---|
1202 | Log(("NAT: can't get depth of IN queue status from %R[natsock]\n", so));
|
---|
1203 | }
|
---|
1204 | status = ioctl(so->s, TIOCOUTQ, &outq); /* SIOCOUTQ see previous comment */
|
---|
1205 | if (status != 0)
|
---|
1206 | Log(("NAT: can't get depth of OUT queue from %R[natsock]\n", so));
|
---|
1207 | #else
|
---|
1208 | /*
|
---|
1209 | * Solaris has bit different ioctl commands and its handlings
|
---|
1210 | * hint: streamio(7) I_NREAD
|
---|
1211 | */
|
---|
1212 | #endif
|
---|
1213 | if ( so->so_state & SS_ISFCONNECTING
|
---|
1214 | || UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1215 | {
|
---|
1216 | /**
|
---|
1217 | * Check if we need here take care about gracefull connection
|
---|
1218 | * @todo try with proxy server
|
---|
1219 | */
|
---|
1220 | if (UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1221 | {
|
---|
1222 | /*
|
---|
1223 | * Never meet inq != 0 or outq != 0, anyway let it stay for a while
|
---|
1224 | * in case it happens we'll able to detect it.
|
---|
1225 | * Give TCP/IP stack wait or expire the socket.
|
---|
1226 | */
|
---|
1227 | Log(("NAT: %R[natsock] err(%d:%s) s(in:%d,out:%d)happens on read I/O, "
|
---|
1228 | "other side close connection \n", so, err, strerror(err), inq, outq));
|
---|
1229 | CONTINUE(tcp);
|
---|
1230 | }
|
---|
1231 | goto tcp_input_close;
|
---|
1232 | }
|
---|
1233 | if ( !UNIX_CHECK_FD_SET(so, NetworkEvents, readfds)
|
---|
1234 | && !UNIX_CHECK_FD_SET(so, NetworkEvents, writefds)
|
---|
1235 | && !UNIX_CHECK_FD_SET(so, NetworkEvents, xfds))
|
---|
1236 | {
|
---|
1237 | Log(("NAT: system expires the socket %R[natsock] err(%d:%s) s(in:%d,out:%d) happens on non-I/O. ",
|
---|
1238 | so, err, strerror(err), inq, outq));
|
---|
1239 | goto tcp_input_close;
|
---|
1240 | }
|
---|
1241 | Log(("NAT: %R[natsock] we've met(%d:%s) s(in:%d, out:%d) unhandled combination hup (%d) "
|
---|
1242 | "rederr(%d) on (r:%d, w:%d, x:%d)\n",
|
---|
1243 | so, err, strerror(err),
|
---|
1244 | inq, outq,
|
---|
1245 | UNIX_CHECK_FD_SET(so, ign, rdhup),
|
---|
1246 | UNIX_CHECK_FD_SET(so, ign, rderr),
|
---|
1247 | UNIX_CHECK_FD_SET(so, ign, readfds),
|
---|
1248 | UNIX_CHECK_FD_SET(so, ign, writefds),
|
---|
1249 | UNIX_CHECK_FD_SET(so, ign, xfds)));
|
---|
1250 | /*
|
---|
1251 | * Give OS's TCP/IP stack a chance to resolve an issue or expire the socket.
|
---|
1252 | */
|
---|
1253 | CONTINUE(tcp);
|
---|
1254 | tcp_input_close:
|
---|
1255 | so->so_state = SS_NOFDREF; /*cause connection valid tcp connection termination and socket closing */
|
---|
1256 | TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
|
---|
1257 | CONTINUE(tcp);
|
---|
1258 | }
|
---|
1259 | #endif
|
---|
1260 | LOOP_LABEL(tcp, so, so_next);
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | /*
|
---|
1264 | * Now UDP sockets.
|
---|
1265 | * Incoming packets are sent straight away, they're not buffered.
|
---|
1266 | * Incoming UDP data isn't buffered either.
|
---|
1267 | */
|
---|
1268 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
1269 | /* { */
|
---|
1270 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1271 | if ( so->so_state & SS_NOFDREF
|
---|
1272 | && so->so_deleted == 1)
|
---|
1273 | {
|
---|
1274 | struct socket *son, *sop = NULL;
|
---|
1275 | QSOCKET_LOCK(udb);
|
---|
1276 | if (so->so_next != NULL)
|
---|
1277 | {
|
---|
1278 | if (so->so_next != &udb)
|
---|
1279 | SOCKET_LOCK(so->so_next);
|
---|
1280 | son = so->so_next;
|
---|
1281 | }
|
---|
1282 | if ( so->so_prev != &udb
|
---|
1283 | && so->so_prev != NULL)
|
---|
1284 | {
|
---|
1285 | SOCKET_LOCK(so->so_prev);
|
---|
1286 | sop = so->so_prev;
|
---|
1287 | }
|
---|
1288 | QSOCKET_UNLOCK(udb);
|
---|
1289 | remque(pData, so);
|
---|
1290 | NSOCK_DEC();
|
---|
1291 | SOCKET_UNLOCK(so);
|
---|
1292 | SOCKET_LOCK_DESTROY(so);
|
---|
1293 | RTMemFree(so);
|
---|
1294 | so_next = son;
|
---|
1295 | if (sop != NULL)
|
---|
1296 | SOCKET_UNLOCK(sop);
|
---|
1297 | CONTINUE_NO_UNLOCK(udp);
|
---|
1298 | }
|
---|
1299 | #endif
|
---|
1300 | POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
|
---|
1301 |
|
---|
1302 | LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
|
---|
1303 |
|
---|
1304 | if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
|
---|
1305 | {
|
---|
1306 | SORECVFROM(pData, so);
|
---|
1307 | }
|
---|
1308 | LOOP_LABEL(udp, so, so_next);
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | done:
|
---|
1312 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
1313 | /*
|
---|
1314 | * See if we can start outputting
|
---|
1315 | */
|
---|
1316 | if (if_queued && link_up)
|
---|
1317 | if_start(pData);
|
---|
1318 | #endif
|
---|
1319 |
|
---|
1320 | STAM_PROFILE_STOP(&pData->StatPoll, a);
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 |
|
---|
1324 | struct arphdr
|
---|
1325 | {
|
---|
1326 | unsigned short ar_hrd; /* format of hardware address */
|
---|
1327 | unsigned short ar_pro; /* format of protocol address */
|
---|
1328 | unsigned char ar_hln; /* length of hardware address */
|
---|
1329 | unsigned char ar_pln; /* length of protocol address */
|
---|
1330 | unsigned short ar_op; /* ARP opcode (command) */
|
---|
1331 |
|
---|
1332 | /*
|
---|
1333 | * Ethernet looks like this : This bit is variable sized however...
|
---|
1334 | */
|
---|
1335 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
|
---|
1336 | unsigned char ar_sip[4]; /* sender IP address */
|
---|
1337 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
|
---|
1338 | unsigned char ar_tip[4]; /* target IP address */
|
---|
1339 | };
|
---|
1340 | AssertCompileSize(struct arphdr, 28);
|
---|
1341 |
|
---|
1342 | static void arp_input(PNATState pData, struct mbuf *m)
|
---|
1343 | {
|
---|
1344 | struct ethhdr *eh;
|
---|
1345 | struct ethhdr *reh;
|
---|
1346 | struct arphdr *ah;
|
---|
1347 | struct arphdr *rah;
|
---|
1348 | int ar_op;
|
---|
1349 | struct ex_list *ex_ptr;
|
---|
1350 | uint32_t htip;
|
---|
1351 | uint32_t tip;
|
---|
1352 | struct mbuf *mr;
|
---|
1353 | eh = mtod(m, struct ethhdr *);
|
---|
1354 | ah = (struct arphdr *)&eh[1];
|
---|
1355 | htip = ntohl(*(uint32_t*)ah->ar_tip);
|
---|
1356 | tip = *(uint32_t*)ah->ar_tip;
|
---|
1357 |
|
---|
1358 |
|
---|
1359 | ar_op = ntohs(ah->ar_op);
|
---|
1360 | switch(ar_op)
|
---|
1361 | {
|
---|
1362 | case ARPOP_REQUEST:
|
---|
1363 | mr = m_get(pData);
|
---|
1364 |
|
---|
1365 | reh = mtod(mr, struct ethhdr *);
|
---|
1366 | memcpy(reh->h_source, eh->h_source, ETH_ALEN); /* XXX: if_encap will swap src and dst*/
|
---|
1367 | Log4(("NAT: arp:%R[ether]->%R[ether]\n",
|
---|
1368 | reh->h_source, reh->h_dest));
|
---|
1369 | Log4(("NAT: arp: %R[IP4]\n", &tip));
|
---|
1370 |
|
---|
1371 | mr->m_data += if_maxlinkhdr;
|
---|
1372 | mr->m_len = sizeof(struct arphdr);
|
---|
1373 | rah = mtod(mr, struct arphdr *);
|
---|
1374 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
1375 | if (tip == special_addr.s_addr) goto arp_ok;
|
---|
1376 | #endif
|
---|
1377 | if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
|
---|
1378 | {
|
---|
1379 | if ( CTL_CHECK(htip, CTL_DNS)
|
---|
1380 | || CTL_CHECK(htip, CTL_ALIAS)
|
---|
1381 | || CTL_CHECK(htip, CTL_TFTP))
|
---|
1382 | goto arp_ok;
|
---|
1383 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
|
---|
1384 | {
|
---|
1385 | if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
|
---|
1386 | {
|
---|
1387 | goto arp_ok;
|
---|
1388 | }
|
---|
1389 | }
|
---|
1390 | return;
|
---|
1391 | arp_ok:
|
---|
1392 | rah->ar_hrd = htons(1);
|
---|
1393 | rah->ar_pro = htons(ETH_P_IP);
|
---|
1394 | rah->ar_hln = ETH_ALEN;
|
---|
1395 | rah->ar_pln = 4;
|
---|
1396 | rah->ar_op = htons(ARPOP_REPLY);
|
---|
1397 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
|
---|
1398 |
|
---|
1399 | switch (htip & ~pData->netmask)
|
---|
1400 | {
|
---|
1401 | case CTL_DNS:
|
---|
1402 | case CTL_ALIAS:
|
---|
1403 | rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
|
---|
1404 | break;
|
---|
1405 | default:;
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | memcpy(rah->ar_sip, ah->ar_tip, 4);
|
---|
1409 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
|
---|
1410 | memcpy(rah->ar_tip, ah->ar_sip, 4);
|
---|
1411 | if_encap(pData, ETH_P_ARP, mr);
|
---|
1412 | m_free(pData, m);
|
---|
1413 | }
|
---|
1414 | /*Gratuitous ARP*/
|
---|
1415 | if ( *(uint32_t *)ah->ar_sip == *(uint32_t *)ah->ar_tip
|
---|
1416 | && memcmp(ah->ar_tha, broadcast_ethaddr, ETH_ALEN) == 0
|
---|
1417 | && memcmp(eh->h_dest, broadcast_ethaddr, ETH_ALEN) == 0)
|
---|
1418 | {
|
---|
1419 | /* we've received anounce about address asignment
|
---|
1420 | * Let's do ARP cache update
|
---|
1421 | */
|
---|
1422 | if (slirp_arp_cache_update(pData, *(uint32_t *)ah->ar_tip, &eh->h_dest[0]) == 0)
|
---|
1423 | {
|
---|
1424 | m_free(pData, mr);
|
---|
1425 | m_free(pData, m);
|
---|
1426 | break;
|
---|
1427 | }
|
---|
1428 | slirp_arp_cache_add(pData, *(uint32_t *)ah->ar_tip, &eh->h_dest[0]);
|
---|
1429 | /* good opportunity to activate port-forwarding on address (self)asignment*/
|
---|
1430 | activate_port_forwarding(pData, eh);
|
---|
1431 | }
|
---|
1432 | break;
|
---|
1433 | case ARPOP_REPLY:
|
---|
1434 | {
|
---|
1435 | if (slirp_arp_cache_update(pData, *(uint32_t *)ah->ar_sip, &ah->ar_sha[0]) == 0)
|
---|
1436 | {
|
---|
1437 | m_free(pData, m);
|
---|
1438 | break;
|
---|
1439 | }
|
---|
1440 | slirp_arp_cache_add(pData, *(uint32_t *)ah->ar_sip, ah->ar_sha);
|
---|
1441 | /*after/save restore we need up port forwarding again*/
|
---|
1442 | if (pData->port_forwarding_activated == 0)
|
---|
1443 | activate_port_forwarding(pData, eh);
|
---|
1444 | m_free(pData, m);
|
---|
1445 | }
|
---|
1446 | break;
|
---|
1447 | default:
|
---|
1448 | break;
|
---|
1449 | }
|
---|
1450 | }
|
---|
1451 |
|
---|
1452 | void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
|
---|
1453 | {
|
---|
1454 | struct mbuf *m;
|
---|
1455 | int proto;
|
---|
1456 | static bool fWarnedIpv6;
|
---|
1457 | struct ethhdr *eh = (struct ethhdr*)pkt;
|
---|
1458 |
|
---|
1459 | Log2(("NAT: slirp_input %d\n", pkt_len));
|
---|
1460 | if (pkt_len < ETH_HLEN)
|
---|
1461 | {
|
---|
1462 | LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
|
---|
1463 | return;
|
---|
1464 | }
|
---|
1465 | Log4(("NAT: in:%R[ether]->%R[ether]\n", &eh->h_source, &eh->h_dest));
|
---|
1466 |
|
---|
1467 | if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) == 0)
|
---|
1468 | {
|
---|
1469 | /* @todo vasily: add ether logging routine in debug.c */
|
---|
1470 | Log(("NAT: packet was addressed to other MAC\n"));
|
---|
1471 | RTMemFree((void *)pkt);
|
---|
1472 | return;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | m = m_get(pData);
|
---|
1476 | if (!m)
|
---|
1477 | {
|
---|
1478 | LogRel(("NAT: can't allocate new mbuf\n"));
|
---|
1479 | return;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | /* Note: we add to align the IP header */
|
---|
1483 |
|
---|
1484 | if (M_FREEROOM(m) < pkt_len)
|
---|
1485 | m_inc(m, pkt_len);
|
---|
1486 |
|
---|
1487 | m->m_len = pkt_len ;
|
---|
1488 | memcpy(m->m_data, pkt, pkt_len);
|
---|
1489 |
|
---|
1490 | #if 1
|
---|
1491 | if (pData->port_forwarding_activated == 0)
|
---|
1492 | activate_port_forwarding(pData, mtod(m, struct ethhdr *));
|
---|
1493 | #endif
|
---|
1494 |
|
---|
1495 | proto = ntohs(*(uint16_t *)(pkt + 12));
|
---|
1496 | switch(proto)
|
---|
1497 | {
|
---|
1498 | case ETH_P_ARP:
|
---|
1499 | arp_input(pData, m);
|
---|
1500 | break;
|
---|
1501 | case ETH_P_IP:
|
---|
1502 | /* Update time. Important if the network is very quiet, as otherwise
|
---|
1503 | * the first outgoing connection gets an incorrect timestamp. */
|
---|
1504 | updtime(pData);
|
---|
1505 | m_adj(m, ETH_HLEN);
|
---|
1506 | ip_input(pData, m);
|
---|
1507 | break;
|
---|
1508 | case ETH_P_IPV6:
|
---|
1509 | m_free(pData, m);
|
---|
1510 | if (!fWarnedIpv6)
|
---|
1511 | {
|
---|
1512 | LogRel(("NAT: IPv6 not supported\n"));
|
---|
1513 | fWarnedIpv6 = true;
|
---|
1514 | }
|
---|
1515 | break;
|
---|
1516 | default:
|
---|
1517 | Log(("NAT: Unsupported protocol %x\n", proto));
|
---|
1518 | m_free(pData, m);
|
---|
1519 | break;
|
---|
1520 | }
|
---|
1521 | RTMemFree((void *)pkt);
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | /* output the IP packet to the ethernet device */
|
---|
1525 | void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
|
---|
1526 | {
|
---|
1527 | struct ethhdr *eh;
|
---|
1528 | uint8_t *buf = NULL;
|
---|
1529 | STAM_PROFILE_START(&pData->StatIF_encap, a);
|
---|
1530 |
|
---|
1531 | m->m_data -= if_maxlinkhdr;
|
---|
1532 | m->m_len += ETH_HLEN;
|
---|
1533 | eh = mtod(m, struct ethhdr *);
|
---|
1534 |
|
---|
1535 | if(MBUF_HEAD(m) != m->m_data)
|
---|
1536 | {
|
---|
1537 | LogRel(("NAT: ethernet detects corruption of the packet"));
|
---|
1538 | AssertMsgFailed(("!!Ethernet frame corrupted!!"));
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) != 0)
|
---|
1542 | {
|
---|
1543 | memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
|
---|
1544 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN);
|
---|
1545 | Assert(memcmp(eh->h_dest, special_ethaddr, ETH_ALEN) != 0);
|
---|
1546 | if (memcmp(eh->h_dest, zerro_ethaddr, ETH_ALEN) == 0)
|
---|
1547 | {
|
---|
1548 | /* don't do anything */
|
---|
1549 | goto done;
|
---|
1550 | }
|
---|
1551 | }
|
---|
1552 | buf = RTMemAlloc(1600);
|
---|
1553 | if (buf == NULL)
|
---|
1554 | {
|
---|
1555 | LogRel(("NAT: Can't alloc memory for outgoing buffer\n"));
|
---|
1556 | goto done;
|
---|
1557 | }
|
---|
1558 | eh->h_proto = htons(eth_proto);
|
---|
1559 | memcpy(buf, mtod(m, uint8_t *), m->m_len);
|
---|
1560 | slirp_output(pData->pvUser, NULL, buf, m->m_len);
|
---|
1561 | done:
|
---|
1562 | STAM_PROFILE_STOP(&pData->StatIF_encap, a);
|
---|
1563 | m_free(pData, m);
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | /**
|
---|
1567 | * Still we're using dhcp server leasing to map ether to IP
|
---|
1568 | * @todo see rt_lookup_in_cache
|
---|
1569 | */
|
---|
1570 | static uint32_t find_guest_ip(PNATState pData, const uint8_t *eth_addr)
|
---|
1571 | {
|
---|
1572 | uint32_t ip = INADDR_ANY;
|
---|
1573 | if (eth_addr == NULL)
|
---|
1574 | goto done;
|
---|
1575 | if (memcmp(eth_addr, zerro_ethaddr, ETH_ALEN) == 0
|
---|
1576 | || memcmp(eth_addr, broadcast_ethaddr, ETH_ALEN) == 0)
|
---|
1577 | goto done;
|
---|
1578 | if(slirp_arp_lookup_ip_by_ether(pData, eth_addr, &ip) == 0)
|
---|
1579 | goto done;
|
---|
1580 | bootp_cache_lookup_ip_by_ether(pData, eth_addr, &ip);
|
---|
1581 | done:
|
---|
1582 | return ip;
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | /**
|
---|
1586 | * We need check if we've activated port forwarding
|
---|
1587 | * for specific machine ... that of course relates to
|
---|
1588 | * service mode
|
---|
1589 | * @todo finish this for service case
|
---|
1590 | */
|
---|
1591 | static void activate_port_forwarding(PNATState pData, struct ethhdr *ethdr)
|
---|
1592 | {
|
---|
1593 | struct port_forward_rule *rule = NULL;
|
---|
1594 |
|
---|
1595 | pData->port_forwarding_activated = 1;
|
---|
1596 | /* check mac here */
|
---|
1597 | LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
|
---|
1598 | {
|
---|
1599 | struct socket *so;
|
---|
1600 | struct alias_link *link;
|
---|
1601 | struct libalias *lib;
|
---|
1602 | int flags;
|
---|
1603 | struct sockaddr sa;
|
---|
1604 | struct sockaddr_in *psin;
|
---|
1605 | socklen_t socketlen;
|
---|
1606 | struct in_addr alias;
|
---|
1607 | int rc;
|
---|
1608 | uint32_t guest_addr; /* need to understand if we already give address to guest */
|
---|
1609 |
|
---|
1610 | if (rule->activated)
|
---|
1611 | continue; /*already activated */
|
---|
1612 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
1613 | if (memcmp(rule->mac_address, ethdr->h_source, ETH_ALEN) != 0)
|
---|
1614 | continue; /*not right mac, @todo: it'd be better do the list port forwarding per mac */
|
---|
1615 | guest_addr = find_guest_ip(pData, ethdr->h_source);
|
---|
1616 | #else
|
---|
1617 | #if 0
|
---|
1618 | if (memcmp(client_ethaddr, ethdr->h_source, ETH_ALEN) != 0)
|
---|
1619 | continue;
|
---|
1620 | #endif
|
---|
1621 | guest_addr = find_guest_ip(pData, ethdr->h_source);
|
---|
1622 | #endif
|
---|
1623 | if (guest_addr == INADDR_ANY)
|
---|
1624 | {
|
---|
1625 | /* the address wasn't granted */
|
---|
1626 | pData->port_forwarding_activated = 0;
|
---|
1627 | return;
|
---|
1628 | }
|
---|
1629 | #if defined(DEBUG_vvl) && !defined(VBOX_WITH_NAT_SERVICE)
|
---|
1630 | Assert(rule->guest_addr.s_addr == guest_addr);
|
---|
1631 | #endif
|
---|
1632 |
|
---|
1633 | LogRel(("NAT: set redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
|
---|
1634 | rule->host_port, rule->guest_port));
|
---|
1635 | if (rule->proto == IPPROTO_UDP)
|
---|
1636 | {
|
---|
1637 | so = udp_listen(pData, rule->bind_ip.s_addr, htons(rule->host_port), guest_addr,
|
---|
1638 | htons(rule->guest_port), 0);
|
---|
1639 | }
|
---|
1640 | else
|
---|
1641 | {
|
---|
1642 | so = solisten(pData, rule->bind_ip.s_addr, htons(rule->host_port), guest_addr,
|
---|
1643 | htons(rule->guest_port), 0);
|
---|
1644 | }
|
---|
1645 | if (so == NULL)
|
---|
1646 | {
|
---|
1647 | LogRel(("NAT: failed redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
|
---|
1648 | rule->host_port, rule->guest_port));
|
---|
1649 | goto remove_port_forwarding;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | psin = (struct sockaddr_in *)&sa;
|
---|
1653 | psin->sin_family = AF_INET;
|
---|
1654 | psin->sin_port = 0;
|
---|
1655 | psin->sin_addr.s_addr = INADDR_ANY;
|
---|
1656 | socketlen = sizeof(struct sockaddr);
|
---|
1657 |
|
---|
1658 | rc = getsockname(so->s, &sa, &socketlen);
|
---|
1659 | if (rc < 0 || sa.sa_family != AF_INET)
|
---|
1660 | {
|
---|
1661 | LogRel(("NAT: failed redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
|
---|
1662 | rule->host_port, rule->guest_port));
|
---|
1663 | goto remove_port_forwarding;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | psin = (struct sockaddr_in *)&sa;
|
---|
1667 |
|
---|
1668 | lib = LibAliasInit(pData, NULL);
|
---|
1669 | flags = LibAliasSetMode(lib, 0, 0);
|
---|
1670 | flags |= PKT_ALIAS_LOG; /* set logging */
|
---|
1671 | flags |= PKT_ALIAS_REVERSE; /* set logging */
|
---|
1672 | flags = LibAliasSetMode(lib, flags, ~0);
|
---|
1673 |
|
---|
1674 | alias.s_addr = htonl(ntohl(guest_addr) | CTL_ALIAS);
|
---|
1675 | link = LibAliasRedirectPort(lib, psin->sin_addr, htons(rule->host_port),
|
---|
1676 | alias, htons(rule->guest_port),
|
---|
1677 | special_addr, -1, /* not very clear for now*/
|
---|
1678 | rule->proto);
|
---|
1679 | if (link == NULL)
|
---|
1680 | {
|
---|
1681 | LogRel(("NAT: failed redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
|
---|
1682 | rule->host_port, rule->guest_port));
|
---|
1683 | goto remove_port_forwarding;
|
---|
1684 | }
|
---|
1685 | so->so_la = lib;
|
---|
1686 | rule->activated = 1;
|
---|
1687 | continue;
|
---|
1688 | remove_port_forwarding:
|
---|
1689 | LIST_REMOVE(rule, list);
|
---|
1690 | RTMemFree(rule);
|
---|
1691 | }
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | /**
|
---|
1695 | * Changes in 3.1 instead of opening new socket do the following:
|
---|
1696 | * gain more information:
|
---|
1697 | * 1. bind IP
|
---|
1698 | * 2. host port
|
---|
1699 | * 3. guest port
|
---|
1700 | * 4. proto
|
---|
1701 | * 5. guest MAC address
|
---|
1702 | * the guest's MAC address is rather important for service, but we easily
|
---|
1703 | * could get it from VM configuration in DrvNAT or Service, the idea is activating
|
---|
1704 | * corresponding port-forwarding
|
---|
1705 | */
|
---|
1706 | int slirp_redir(PNATState pData, int is_udp, struct in_addr host_addr, int host_port,
|
---|
1707 | struct in_addr guest_addr, int guest_port, const uint8_t *ethaddr)
|
---|
1708 | {
|
---|
1709 | struct port_forward_rule *rule = NULL;
|
---|
1710 | Assert(memcmp(ethaddr, zerro_ethaddr, ETH_ALEN) == 0);
|
---|
1711 | rule = RTMemAllocZ(sizeof(struct port_forward_rule));
|
---|
1712 | if (rule == NULL)
|
---|
1713 | return 1;
|
---|
1714 | rule->proto = (is_udp ? IPPROTO_UDP : IPPROTO_TCP);
|
---|
1715 | rule->host_port = host_port;
|
---|
1716 | rule->guest_port = guest_port;
|
---|
1717 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
1718 | rule->guest_addr.s_addr = guest_addr.s_addr;
|
---|
1719 | #endif
|
---|
1720 | rule->bind_ip.s_addr = host_addr.s_addr;
|
---|
1721 | memcpy(rule->mac_address, ethaddr, ETH_ALEN);
|
---|
1722 | /* @todo add mac address */
|
---|
1723 | LIST_INSERT_HEAD(&pData->port_forward_rule_head, rule, list);
|
---|
1724 | return 0;
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
|
---|
1728 | int guest_port)
|
---|
1729 | {
|
---|
1730 | return add_exec(&exec_list, do_pty, (char *)args,
|
---|
1731 | addr_low_byte, htons(guest_port));
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 | void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
|
---|
1735 | {
|
---|
1736 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
1737 | memcpy(client_ethaddr, ethaddr, ETH_ALEN);
|
---|
1738 | #endif
|
---|
1739 | }
|
---|
1740 |
|
---|
1741 | #if defined(RT_OS_WINDOWS)
|
---|
1742 | HANDLE *slirp_get_events(PNATState pData)
|
---|
1743 | {
|
---|
1744 | return pData->phEvents;
|
---|
1745 | }
|
---|
1746 | void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
|
---|
1747 | {
|
---|
1748 | pData->phEvents[index] = hEvent;
|
---|
1749 | }
|
---|
1750 | #endif
|
---|
1751 |
|
---|
1752 | unsigned int slirp_get_timeout_ms(PNATState pData)
|
---|
1753 | {
|
---|
1754 | if (link_up)
|
---|
1755 | {
|
---|
1756 | if (time_fasttimo)
|
---|
1757 | return 2;
|
---|
1758 | if (do_slowtimo)
|
---|
1759 | return 500; /* see PR_SLOWHZ */
|
---|
1760 | }
|
---|
1761 | return 0;
|
---|
1762 | }
|
---|
1763 |
|
---|
1764 | #ifndef RT_OS_WINDOWS
|
---|
1765 | int slirp_get_nsock(PNATState pData)
|
---|
1766 | {
|
---|
1767 | return pData->nsock;
|
---|
1768 | }
|
---|
1769 | #endif
|
---|
1770 |
|
---|
1771 | /*
|
---|
1772 | * this function called from NAT thread
|
---|
1773 | */
|
---|
1774 | void slirp_post_sent(PNATState pData, void *pvArg)
|
---|
1775 | {
|
---|
1776 | struct socket *so = 0;
|
---|
1777 | struct tcpcb *tp = 0;
|
---|
1778 | struct mbuf *m = (struct mbuf *)pvArg;
|
---|
1779 | m_free(pData, m);
|
---|
1780 | }
|
---|
1781 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
1782 | void slirp_process_queue(PNATState pData)
|
---|
1783 | {
|
---|
1784 | RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
|
---|
1785 | }
|
---|
1786 | void *slirp_get_queue(PNATState pData)
|
---|
1787 | {
|
---|
1788 | return pData->pReqQueue;
|
---|
1789 | }
|
---|
1790 | #endif
|
---|
1791 |
|
---|
1792 | void slirp_set_dhcp_TFTP_prefix(PNATState pData, const char *tftpPrefix)
|
---|
1793 | {
|
---|
1794 | Log2(("tftp_prefix:%s\n", tftpPrefix));
|
---|
1795 | tftp_prefix = tftpPrefix;
|
---|
1796 | }
|
---|
1797 |
|
---|
1798 | void slirp_set_dhcp_TFTP_bootfile(PNATState pData, const char *bootFile)
|
---|
1799 | {
|
---|
1800 | Log2(("bootFile:%s\n", bootFile));
|
---|
1801 | bootp_filename = bootFile;
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | void slirp_set_dhcp_next_server(PNATState pData, const char *next_server)
|
---|
1805 | {
|
---|
1806 | Log2(("next_server:%s\n", next_server));
|
---|
1807 | if (next_server == NULL)
|
---|
1808 | pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
|
---|
1809 | else
|
---|
1810 | inet_aton(next_server, &pData->tftp_server);
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | int slirp_set_binding_address(PNATState pData, char *addr)
|
---|
1814 | {
|
---|
1815 | if (addr == NULL || (inet_aton(addr, &pData->bindIP) == 0))
|
---|
1816 | {
|
---|
1817 | pData->bindIP.s_addr = INADDR_ANY;
|
---|
1818 | return 1;
|
---|
1819 | }
|
---|
1820 | return 0;
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | void slirp_set_dhcp_dns_proxy(PNATState pData, bool fDNSProxy)
|
---|
1824 | {
|
---|
1825 | Log2(("NAT: DNS proxy switched %s\n", (fDNSProxy ? "on" : "off")));
|
---|
1826 | pData->use_dns_proxy = fDNSProxy;
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | #define CHECK_ARG(name, val, lim_min, lim_max) \
|
---|
1830 | do { \
|
---|
1831 | if ((val) < (lim_min) || (val) > (lim_max)) \
|
---|
1832 | { \
|
---|
1833 | LogRel(("NAT: (" #name ":%d) has been ignored, " \
|
---|
1834 | "because out of range (%d, %d)\n", (val), (lim_min), (lim_max))); \
|
---|
1835 | return; \
|
---|
1836 | } \
|
---|
1837 | else \
|
---|
1838 | { \
|
---|
1839 | LogRel(("NAT: (" #name ":%d)\n", (val))); \
|
---|
1840 | } \
|
---|
1841 | } while (0)
|
---|
1842 |
|
---|
1843 | /* don't allow user set less 8kB and more than 1M values */
|
---|
1844 | #define _8K_1M_CHECK_ARG(name, val) CHECK_ARG(name, (val), 8, 1024)
|
---|
1845 | void slirp_set_rcvbuf(PNATState pData, int kilobytes)
|
---|
1846 | {
|
---|
1847 | _8K_1M_CHECK_ARG("SOCKET_RCVBUF", kilobytes);
|
---|
1848 | pData->socket_rcv = kilobytes;
|
---|
1849 | }
|
---|
1850 | void slirp_set_sndbuf(PNATState pData, int kilobytes)
|
---|
1851 | {
|
---|
1852 | _8K_1M_CHECK_ARG("SOCKET_SNDBUF", kilobytes);
|
---|
1853 | pData->socket_snd = kilobytes * _1K;
|
---|
1854 | }
|
---|
1855 | void slirp_set_tcp_rcvspace(PNATState pData, int kilobytes)
|
---|
1856 | {
|
---|
1857 | _8K_1M_CHECK_ARG("TCP_RCVSPACE", kilobytes);
|
---|
1858 | tcp_rcvspace = kilobytes * _1K;
|
---|
1859 | }
|
---|
1860 | void slirp_set_tcp_sndspace(PNATState pData, int kilobytes)
|
---|
1861 | {
|
---|
1862 | _8K_1M_CHECK_ARG("TCP_SNDSPACE", kilobytes);
|
---|
1863 | tcp_sndspace = kilobytes * _1K;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | /*
|
---|
1867 | * Looking for Ether by ip in ARP-cache
|
---|
1868 | * Note: it´s responsible of caller to allocate buffer for result
|
---|
1869 | * @returns 0 - if found, 1 - otherwise
|
---|
1870 | */
|
---|
1871 | int slirp_arp_lookup_ether_by_ip(PNATState pData, uint32_t ip, uint8_t *ether)
|
---|
1872 | {
|
---|
1873 | struct arp_cache_entry *ac = NULL;
|
---|
1874 | int rc = 1;
|
---|
1875 | if (ether == NULL)
|
---|
1876 | return rc;
|
---|
1877 |
|
---|
1878 | if (LIST_EMPTY(&pData->arp_cache))
|
---|
1879 | return rc;
|
---|
1880 |
|
---|
1881 | LIST_FOREACH(ac, &pData->arp_cache, list)
|
---|
1882 | {
|
---|
1883 | if (ac->ip == ip)
|
---|
1884 | {
|
---|
1885 | memcpy(ether, ac->ether, ETH_ALEN);
|
---|
1886 | rc = 0;
|
---|
1887 | return rc;
|
---|
1888 | }
|
---|
1889 | }
|
---|
1890 | return rc;
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 | /*
|
---|
1894 | * Looking for IP by Ether in ARP-cache
|
---|
1895 | * Note: it´s responsible of caller to allocate buffer for result
|
---|
1896 | * @returns 0 - if found, 1 - otherwise
|
---|
1897 | */
|
---|
1898 | int slirp_arp_lookup_ip_by_ether(PNATState pData, const uint8_t *ether, uint32_t *ip)
|
---|
1899 | {
|
---|
1900 | struct arp_cache_entry *ac = NULL;
|
---|
1901 | int rc = 1;
|
---|
1902 | *ip = INADDR_ANY;
|
---|
1903 | if (LIST_EMPTY(&pData->arp_cache))
|
---|
1904 | return rc;
|
---|
1905 | LIST_FOREACH(ac, &pData->arp_cache, list)
|
---|
1906 | {
|
---|
1907 | if (memcmp(ether, ac->ether, ETH_ALEN))
|
---|
1908 | {
|
---|
1909 | *ip = ac->ip;
|
---|
1910 | rc = 0;
|
---|
1911 | return rc;
|
---|
1912 | }
|
---|
1913 | }
|
---|
1914 | return rc;
|
---|
1915 | }
|
---|
1916 |
|
---|
1917 | void slirp_arp_who_has(PNATState pData, uint32_t dst)
|
---|
1918 | {
|
---|
1919 | struct mbuf *m;
|
---|
1920 | struct ethhdr *ehdr;
|
---|
1921 | struct arphdr *ahdr;
|
---|
1922 |
|
---|
1923 | m = m_get(pData);
|
---|
1924 | if (m == NULL)
|
---|
1925 | {
|
---|
1926 | LogRel(("NAT: Can't alloc mbuf for ARP request\n"));
|
---|
1927 | return;
|
---|
1928 | }
|
---|
1929 | ehdr = mtod(m, struct ethhdr *);
|
---|
1930 | memset(ehdr->h_source, 0xff, ETH_ALEN);
|
---|
1931 | ahdr = (struct arphdr *)&ehdr[1];
|
---|
1932 | ahdr->ar_hrd = htons(1);
|
---|
1933 | ahdr->ar_pro = htons(ETH_P_IP);
|
---|
1934 | ahdr->ar_hln = ETH_ALEN;
|
---|
1935 | ahdr->ar_pln = 4;
|
---|
1936 | ahdr->ar_op = htons(ARPOP_REQUEST);
|
---|
1937 | memcpy(ahdr->ar_sha, special_ethaddr, ETH_ALEN);
|
---|
1938 | *(uint32_t *)ahdr->ar_sip = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
|
---|
1939 | memset(ahdr->ar_tha, 0xff, ETH_ALEN); /*broadcast*/
|
---|
1940 | *(uint32_t *)ahdr->ar_tip = dst;
|
---|
1941 | m->m_data += if_maxlinkhdr;
|
---|
1942 | m->m_len = sizeof(struct arphdr);
|
---|
1943 | if_encap(pData, ETH_P_ARP, m);
|
---|
1944 | LogRel(("NAT: ARP request sent\n"));
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 | /* updates the arp cache
|
---|
1948 | * @returns 0 - if has found and updated
|
---|
1949 | * 1 - if hasn't found.
|
---|
1950 | */
|
---|
1951 | int slirp_arp_cache_update(PNATState pData, uint32_t dst, const uint8_t *mac)
|
---|
1952 | {
|
---|
1953 | struct arp_cache_entry *ac;
|
---|
1954 | LIST_FOREACH(ac, &pData->arp_cache, list)
|
---|
1955 | {
|
---|
1956 | if (memcmp(ac->ether, mac, ETH_ALEN) == 0)
|
---|
1957 | {
|
---|
1958 | ac->ip = dst;
|
---|
1959 | return 0;
|
---|
1960 | }
|
---|
1961 | }
|
---|
1962 | return 1;
|
---|
1963 | }
|
---|
1964 |
|
---|
1965 | void slirp_arp_cache_add(PNATState pData, uint32_t ip, const uint8_t *ether)
|
---|
1966 | {
|
---|
1967 | struct arp_cache_entry *ac = NULL;
|
---|
1968 | ac = RTMemAllocZ(sizeof(struct arp_cache_entry));
|
---|
1969 | if (ac == NULL)
|
---|
1970 | {
|
---|
1971 | LogRel(("NAT: Can't allocate arp cache entry\n"));
|
---|
1972 | return;
|
---|
1973 | }
|
---|
1974 | ac->ip = ip;
|
---|
1975 | memcpy(ac->ether, ether, ETH_ALEN);
|
---|
1976 | LIST_INSERT_HEAD(&pData->arp_cache, ac, list);
|
---|
1977 | }
|
---|