]>
Commit | Line | Data |
---|---|---|
d75a0b97 FB |
1 | /* |
2 | * libslirp glue | |
3 | * | |
4 | * Copyright (c) 2004-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
7df7482b | 24 | #include "qemu/osdep.h" |
e1c5a2b3 | 25 | #include "qemu-common.h" |
1de7afc9 | 26 | #include "qemu/timer.h" |
eae303ff | 27 | #include "qemu/error-report.h" |
dccfcd0e | 28 | #include "sysemu/char.h" |
f0cbd3ec | 29 | #include "slirp.h" |
062e5527 | 30 | #include "hw/hw.h" |
f348b6d1 | 31 | #include "qemu/cutils.h" |
f0cbd3ec | 32 | |
ef763fa4 ST |
33 | #ifndef _WIN32 |
34 | #include <net/if.h> | |
35 | #endif | |
36 | ||
f0cbd3ec FB |
37 | /* host loopback address */ |
38 | struct in_addr loopback_addr; | |
648cd33e | 39 | /* host loopback network mask */ |
0b8db8fe | 40 | unsigned long loopback_mask; |
f0cbd3ec | 41 | |
a13a4126 | 42 | /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */ |
1a0ca1e1 | 43 | static const uint8_t special_ethaddr[ETH_ALEN] = { |
a13a4126 | 44 | 0x52, 0x55, 0x00, 0x00, 0x00, 0x00 |
f0cbd3ec FB |
45 | }; |
46 | ||
f1d99bbd | 47 | u_int curtime; |
f1d99bbd | 48 | |
72cf2d4f BS |
49 | static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances = |
50 | QTAILQ_HEAD_INITIALIZER(slirp_instances); | |
115defd1 | 51 | |
9e3a95ef | 52 | static struct in_addr dns_addr; |
ef763fa4 | 53 | #ifndef _WIN32 |
1d17654e | 54 | static struct in6_addr dns6_addr; |
ef763fa4 | 55 | #endif |
9e3a95ef | 56 | static u_int dns_addr_time; |
ef763fa4 | 57 | #ifndef _WIN32 |
1d17654e | 58 | static u_int dns6_addr_time; |
ef763fa4 | 59 | #endif |
df7a86ed | 60 | |
9b0ca6cc LPF |
61 | #define TIMEOUT_FAST 2 /* milliseconds */ |
62 | #define TIMEOUT_SLOW 499 /* milliseconds */ | |
63 | /* for the aging of certain requests like DNS */ | |
64 | #define TIMEOUT_DEFAULT 1000 /* milliseconds */ | |
65 | ||
f0cbd3ec FB |
66 | #ifdef _WIN32 |
67 | ||
df7a86ed | 68 | int get_dns_addr(struct in_addr *pdns_addr) |
f0cbd3ec | 69 | { |
379ff53d FB |
70 | FIXED_INFO *FixedInfo=NULL; |
71 | ULONG BufLen; | |
72 | DWORD ret; | |
73 | IP_ADDR_STRING *pIPAddr; | |
74 | struct in_addr tmp_addr; | |
3b46e624 | 75 | |
9b0ca6cc | 76 | if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) { |
df7a86ed ES |
77 | *pdns_addr = dns_addr; |
78 | return 0; | |
79 | } | |
80 | ||
379ff53d FB |
81 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO)); |
82 | BufLen = sizeof(FIXED_INFO); | |
3b46e624 | 83 | |
379ff53d FB |
84 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) { |
85 | if (FixedInfo) { | |
86 | GlobalFree(FixedInfo); | |
87 | FixedInfo = NULL; | |
88 | } | |
89 | FixedInfo = GlobalAlloc(GPTR, BufLen); | |
90 | } | |
5fafdf24 | 91 | |
379ff53d FB |
92 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) { |
93 | printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret ); | |
94 | if (FixedInfo) { | |
95 | GlobalFree(FixedInfo); | |
96 | FixedInfo = NULL; | |
97 | } | |
98 | return -1; | |
99 | } | |
3b46e624 | 100 | |
379ff53d FB |
101 | pIPAddr = &(FixedInfo->DnsServerList); |
102 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr); | |
103 | *pdns_addr = tmp_addr; | |
df7a86ed ES |
104 | dns_addr = tmp_addr; |
105 | dns_addr_time = curtime; | |
379ff53d FB |
106 | if (FixedInfo) { |
107 | GlobalFree(FixedInfo); | |
108 | FixedInfo = NULL; | |
109 | } | |
110 | return 0; | |
f0cbd3ec FB |
111 | } |
112 | ||
ef763fa4 | 113 | int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) |
1d17654e ST |
114 | { |
115 | return -1; | |
116 | } | |
117 | ||
df461894 JK |
118 | static void winsock_cleanup(void) |
119 | { | |
120 | WSACleanup(); | |
121 | } | |
122 | ||
f0cbd3ec FB |
123 | #else |
124 | ||
1d17654e ST |
125 | static int get_dns_addr_cached(void *pdns_addr, void *cached_addr, |
126 | socklen_t addrlen, | |
127 | struct stat *cached_stat, u_int *cached_time) | |
972487b8 ST |
128 | { |
129 | struct stat old_stat; | |
1d17654e ST |
130 | if (curtime - *cached_time < TIMEOUT_DEFAULT) { |
131 | memcpy(pdns_addr, cached_addr, addrlen); | |
972487b8 ST |
132 | return 0; |
133 | } | |
1d17654e ST |
134 | old_stat = *cached_stat; |
135 | if (stat("/etc/resolv.conf", cached_stat) != 0) { | |
972487b8 ST |
136 | return -1; |
137 | } | |
1d17654e ST |
138 | if (cached_stat->st_dev == old_stat.st_dev |
139 | && cached_stat->st_ino == old_stat.st_ino | |
140 | && cached_stat->st_size == old_stat.st_size | |
141 | && cached_stat->st_mtime == old_stat.st_mtime) { | |
142 | memcpy(pdns_addr, cached_addr, addrlen); | |
972487b8 ST |
143 | return 0; |
144 | } | |
145 | return 1; | |
146 | } | |
147 | ||
1d17654e | 148 | static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr, |
ef763fa4 ST |
149 | socklen_t addrlen, uint32_t *scope_id, |
150 | u_int *cached_time) | |
f0cbd3ec FB |
151 | { |
152 | char buff[512]; | |
363a37d5 | 153 | char buff2[257]; |
f0cbd3ec FB |
154 | FILE *f; |
155 | int found = 0; | |
1d17654e | 156 | void *tmp_addr = alloca(addrlen); |
ef763fa4 | 157 | unsigned if_index; |
3b46e624 | 158 | |
f0cbd3ec FB |
159 | f = fopen("/etc/resolv.conf", "r"); |
160 | if (!f) | |
161 | return -1; | |
162 | ||
31a60e22 | 163 | #ifdef DEBUG |
02d16089 | 164 | fprintf(stderr, "IP address of your DNS(s): "); |
31a60e22 | 165 | #endif |
f0cbd3ec FB |
166 | while (fgets(buff, 512, f) != NULL) { |
167 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { | |
ef763fa4 ST |
168 | char *c = strchr(buff2, '%'); |
169 | if (c) { | |
170 | if_index = if_nametoindex(c + 1); | |
171 | *c = '\0'; | |
172 | } else { | |
173 | if_index = 0; | |
174 | } | |
175 | ||
1d17654e | 176 | if (!inet_pton(af, buff2, tmp_addr)) { |
f0cbd3ec | 177 | continue; |
1d17654e | 178 | } |
f0cbd3ec | 179 | /* If it's the first one, set it to dns_addr */ |
df7a86ed | 180 | if (!found) { |
1d17654e ST |
181 | memcpy(pdns_addr, tmp_addr, addrlen); |
182 | memcpy(cached_addr, tmp_addr, addrlen); | |
ef763fa4 ST |
183 | if (scope_id) { |
184 | *scope_id = if_index; | |
185 | } | |
1d17654e | 186 | *cached_time = curtime; |
df7a86ed | 187 | } |
31a60e22 | 188 | #ifdef DEBUG |
f0cbd3ec | 189 | else |
02d16089 | 190 | fprintf(stderr, ", "); |
31a60e22 | 191 | #endif |
f0cbd3ec | 192 | if (++found > 3) { |
31a60e22 | 193 | #ifdef DEBUG |
02d16089 | 194 | fprintf(stderr, "(more)"); |
31a60e22 | 195 | #endif |
f0cbd3ec | 196 | break; |
31a60e22 BS |
197 | } |
198 | #ifdef DEBUG | |
1d17654e ST |
199 | else { |
200 | char s[INET6_ADDRSTRLEN]; | |
51149a2a | 201 | const char *res = inet_ntop(af, tmp_addr, s, sizeof(s)); |
1d17654e ST |
202 | if (!res) { |
203 | res = "(string conversion error)"; | |
204 | } | |
205 | fprintf(stderr, "%s", res); | |
206 | } | |
31a60e22 | 207 | #endif |
f0cbd3ec FB |
208 | } |
209 | } | |
1d43a717 | 210 | fclose(f); |
f0cbd3ec FB |
211 | if (!found) |
212 | return -1; | |
213 | return 0; | |
214 | } | |
215 | ||
972487b8 ST |
216 | int get_dns_addr(struct in_addr *pdns_addr) |
217 | { | |
1d17654e ST |
218 | static struct stat dns_addr_stat; |
219 | ||
972487b8 ST |
220 | if (dns_addr.s_addr != 0) { |
221 | int ret; | |
1d17654e ST |
222 | ret = get_dns_addr_cached(pdns_addr, &dns_addr, sizeof(dns_addr), |
223 | &dns_addr_stat, &dns_addr_time); | |
224 | if (ret <= 0) { | |
225 | return ret; | |
226 | } | |
227 | } | |
228 | return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr, | |
ef763fa4 | 229 | sizeof(dns_addr), NULL, &dns_addr_time); |
1d17654e ST |
230 | } |
231 | ||
ef763fa4 | 232 | int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) |
1d17654e ST |
233 | { |
234 | static struct stat dns6_addr_stat; | |
235 | ||
236 | if (!in6_zero(&dns6_addr)) { | |
237 | int ret; | |
238 | ret = get_dns_addr_cached(pdns6_addr, &dns6_addr, sizeof(dns6_addr), | |
239 | &dns6_addr_stat, &dns6_addr_time); | |
972487b8 ST |
240 | if (ret <= 0) { |
241 | return ret; | |
242 | } | |
243 | } | |
1d17654e | 244 | return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr, |
ef763fa4 ST |
245 | sizeof(dns6_addr), |
246 | scope_id, &dns6_addr_time); | |
972487b8 ST |
247 | } |
248 | ||
f0cbd3ec FB |
249 | #endif |
250 | ||
df461894 | 251 | static void slirp_init_once(void) |
379ff53d | 252 | { |
df461894 | 253 | static int initialized; |
df461894 JK |
254 | #ifdef _WIN32 |
255 | WSADATA Data; | |
379ff53d FB |
256 | #endif |
257 | ||
df461894 JK |
258 | if (initialized) { |
259 | return; | |
260 | } | |
261 | initialized = 1; | |
262 | ||
263 | #ifdef _WIN32 | |
264 | WSAStartup(MAKEWORD(2,0), &Data); | |
265 | atexit(winsock_cleanup); | |
266 | #endif | |
267 | ||
268 | loopback_addr.s_addr = htonl(INADDR_LOOPBACK); | |
648cd33e | 269 | loopback_mask = htonl(IN_CLASSA_NET); |
df461894 JK |
270 | } |
271 | ||
062e5527 AL |
272 | static void slirp_state_save(QEMUFile *f, void *opaque); |
273 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id); | |
274 | ||
0b11c036 | 275 | Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork, |
9f8bd042 | 276 | struct in_addr vnetmask, struct in_addr vhost, |
0b11c036 | 277 | bool in6_enabled, |
7aac531e YB |
278 | struct in6_addr vprefix_addr6, uint8_t vprefix_len, |
279 | struct in6_addr vhost6, const char *vhostname, | |
280 | const char *tftp_path, const char *bootfile, | |
281 | struct in_addr vdhcp_start, struct in_addr vnameserver, | |
282 | struct in6_addr vnameserver6, const char **vdnssearch, | |
63d2960b | 283 | void *opaque) |
f0cbd3ec | 284 | { |
7267c094 | 285 | Slirp *slirp = g_malloc0(sizeof(Slirp)); |
460fec67 | 286 | |
df461894 | 287 | slirp_init_once(); |
379ff53d | 288 | |
0d6ff71a | 289 | slirp->grand = g_rand_new(); |
460fec67 | 290 | slirp->restricted = restricted; |
f0cbd3ec | 291 | |
0b11c036 ST |
292 | slirp->in_enabled = in_enabled; |
293 | slirp->in6_enabled = in6_enabled; | |
294 | ||
460fec67 JK |
295 | if_init(slirp); |
296 | ip_init(slirp); | |
0d6ff71a | 297 | ip6_init(slirp); |
f0cbd3ec FB |
298 | |
299 | /* Initialise mbufs *after* setting the MTU */ | |
460fec67 | 300 | m_init(slirp); |
f0cbd3ec | 301 | |
460fec67 JK |
302 | slirp->vnetwork_addr = vnetwork; |
303 | slirp->vnetwork_mask = vnetmask; | |
304 | slirp->vhost_addr = vhost; | |
7aac531e YB |
305 | slirp->vprefix_addr6 = vprefix_addr6; |
306 | slirp->vprefix_len = vprefix_len; | |
307 | slirp->vhost_addr6 = vhost6; | |
c92ef6a2 | 308 | if (vhostname) { |
460fec67 JK |
309 | pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname), |
310 | vhostname); | |
a13a4126 | 311 | } |
c64f50d1 MA |
312 | slirp->tftp_prefix = g_strdup(tftp_path); |
313 | slirp->bootp_filename = g_strdup(bootfile); | |
460fec67 JK |
314 | slirp->vdhcp_startaddr = vdhcp_start; |
315 | slirp->vnameserver_addr = vnameserver; | |
7aac531e | 316 | slirp->vnameserver_addr6 = vnameserver6; |
ad196a9d | 317 | |
63d2960b KS |
318 | if (vdnssearch) { |
319 | translate_dnssearch(slirp, vdnssearch); | |
320 | } | |
321 | ||
9f8bd042 JK |
322 | slirp->opaque = opaque; |
323 | ||
eae303ff | 324 | register_savevm(NULL, "slirp", 0, 4, |
0be71e32 | 325 | slirp_state_save, slirp_state_load, slirp); |
9f8bd042 | 326 | |
72cf2d4f | 327 | QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry); |
ad0d8c4c | 328 | |
9f8bd042 | 329 | return slirp; |
f0cbd3ec FB |
330 | } |
331 | ||
ad0d8c4c JK |
332 | void slirp_cleanup(Slirp *slirp) |
333 | { | |
72cf2d4f | 334 | QTAILQ_REMOVE(&slirp_instances, slirp, entry); |
b1c99fcd | 335 | |
0be71e32 | 336 | unregister_savevm(NULL, "slirp", slirp); |
ad0d8c4c | 337 | |
a68adc22 | 338 | ip_cleanup(slirp); |
0d6ff71a | 339 | ip6_cleanup(slirp); |
a68adc22 JK |
340 | m_cleanup(slirp); |
341 | ||
0d6ff71a GS |
342 | g_rand_free(slirp->grand); |
343 | ||
63d2960b | 344 | g_free(slirp->vdnssearch); |
7267c094 AL |
345 | g_free(slirp->tftp_prefix); |
346 | g_free(slirp->bootp_filename); | |
347 | g_free(slirp); | |
ad0d8c4c JK |
348 | } |
349 | ||
f0cbd3ec FB |
350 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) |
351 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) | |
f0cbd3ec | 352 | |
a42e9c41 | 353 | static void slirp_update_timeout(uint32_t *timeout) |
7c7db755 | 354 | { |
a42e9c41 LPF |
355 | Slirp *slirp; |
356 | uint32_t t; | |
357 | ||
358 | if (*timeout <= TIMEOUT_FAST) { | |
359 | return; | |
360 | } | |
426e3e6c JK |
361 | |
362 | t = MIN(1000, *timeout); | |
a42e9c41 LPF |
363 | |
364 | /* If we have tcp timeout with slirp, then we will fill @timeout with | |
365 | * more precise value. | |
366 | */ | |
367 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { | |
368 | if (slirp->time_fasttimo) { | |
369 | *timeout = TIMEOUT_FAST; | |
370 | return; | |
371 | } | |
372 | if (slirp->do_slowtimo) { | |
373 | t = MIN(TIMEOUT_SLOW, t); | |
374 | } | |
7c7db755 | 375 | } |
a42e9c41 | 376 | *timeout = t; |
7c7db755 SS |
377 | } |
378 | ||
a42e9c41 | 379 | void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout) |
f0cbd3ec | 380 | { |
b1c99fcd | 381 | Slirp *slirp; |
f0cbd3ec | 382 | struct socket *so, *so_next; |
f0cbd3ec | 383 | |
72cf2d4f | 384 | if (QTAILQ_EMPTY(&slirp_instances)) { |
d918f23e JK |
385 | return; |
386 | } | |
387 | ||
cf1d078e SH |
388 | /* |
389 | * First, TCP sockets | |
390 | */ | |
e6d43cfb | 391 | |
cf1d078e SH |
392 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
393 | /* | |
394 | * *_slowtimo needs calling if there are IP fragments | |
395 | * in the fragment queue, or there are TCP connections active | |
396 | */ | |
fe0ff43c | 397 | slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) || |
cf1d078e SH |
398 | (&slirp->ipq.ip_link != slirp->ipq.ip_link.next)); |
399 | ||
400 | for (so = slirp->tcb.so_next; so != &slirp->tcb; | |
401 | so = so_next) { | |
8917c3bd SH |
402 | int events = 0; |
403 | ||
cf1d078e SH |
404 | so_next = so->so_next; |
405 | ||
8917c3bd SH |
406 | so->pollfds_idx = -1; |
407 | ||
cf1d078e SH |
408 | /* |
409 | * See if we need a tcp_fasttimo | |
410 | */ | |
fe0ff43c LPF |
411 | if (slirp->time_fasttimo == 0 && |
412 | so->so_tcpcb->t_flags & TF_DELACK) { | |
413 | slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */ | |
cf1d078e | 414 | } |
e6d43cfb | 415 | |
cf1d078e SH |
416 | /* |
417 | * NOFDREF can include still connecting to local-host, | |
418 | * newly socreated() sockets etc. Don't want to select these. | |
419 | */ | |
420 | if (so->so_state & SS_NOFDREF || so->s == -1) { | |
421 | continue; | |
422 | } | |
e6d43cfb | 423 | |
cf1d078e SH |
424 | /* |
425 | * Set for reading sockets which are accepting | |
426 | */ | |
427 | if (so->so_state & SS_FACCEPTCONN) { | |
8917c3bd SH |
428 | GPollFD pfd = { |
429 | .fd = so->s, | |
430 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, | |
431 | }; | |
432 | so->pollfds_idx = pollfds->len; | |
433 | g_array_append_val(pollfds, pfd); | |
cf1d078e SH |
434 | continue; |
435 | } | |
436 | ||
437 | /* | |
438 | * Set for writing sockets which are connecting | |
439 | */ | |
440 | if (so->so_state & SS_ISFCONNECTING) { | |
8917c3bd SH |
441 | GPollFD pfd = { |
442 | .fd = so->s, | |
443 | .events = G_IO_OUT | G_IO_ERR, | |
444 | }; | |
445 | so->pollfds_idx = pollfds->len; | |
446 | g_array_append_val(pollfds, pfd); | |
cf1d078e SH |
447 | continue; |
448 | } | |
449 | ||
450 | /* | |
451 | * Set for writing if we are connected, can send more, and | |
452 | * we have something to send | |
453 | */ | |
454 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) { | |
8917c3bd | 455 | events |= G_IO_OUT | G_IO_ERR; |
cf1d078e SH |
456 | } |
457 | ||
458 | /* | |
459 | * Set for reading (and urgent data) if we are connected, can | |
460 | * receive more, and we have room for it XXX /2 ? | |
461 | */ | |
462 | if (CONN_CANFRCV(so) && | |
463 | (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) { | |
8917c3bd SH |
464 | events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI; |
465 | } | |
466 | ||
467 | if (events) { | |
468 | GPollFD pfd = { | |
469 | .fd = so->s, | |
470 | .events = events, | |
471 | }; | |
472 | so->pollfds_idx = pollfds->len; | |
473 | g_array_append_val(pollfds, pfd); | |
cf1d078e SH |
474 | } |
475 | } | |
476 | ||
477 | /* | |
478 | * UDP sockets | |
479 | */ | |
480 | for (so = slirp->udb.so_next; so != &slirp->udb; | |
481 | so = so_next) { | |
482 | so_next = so->so_next; | |
483 | ||
8917c3bd SH |
484 | so->pollfds_idx = -1; |
485 | ||
cf1d078e SH |
486 | /* |
487 | * See if it's timed out | |
488 | */ | |
489 | if (so->so_expire) { | |
490 | if (so->so_expire <= curtime) { | |
491 | udp_detach(so); | |
492 | continue; | |
493 | } else { | |
fe0ff43c | 494 | slirp->do_slowtimo = true; /* Let socket expire */ |
e6d43cfb | 495 | } |
cf1d078e SH |
496 | } |
497 | ||
498 | /* | |
499 | * When UDP packets are received from over the | |
500 | * link, they're sendto()'d straight away, so | |
501 | * no need for setting for writing | |
502 | * Limit the number of packets queued by this session | |
503 | * to 4. Note that even though we try and limit this | |
504 | * to 4 packets, the session could have more queued | |
505 | * if the packets needed to be fragmented | |
506 | * (XXX <= 4 ?) | |
507 | */ | |
508 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) { | |
8917c3bd SH |
509 | GPollFD pfd = { |
510 | .fd = so->s, | |
511 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, | |
512 | }; | |
513 | so->pollfds_idx = pollfds->len; | |
514 | g_array_append_val(pollfds, pfd); | |
cf1d078e SH |
515 | } |
516 | } | |
5fafdf24 | 517 | |
cf1d078e SH |
518 | /* |
519 | * ICMP sockets | |
520 | */ | |
521 | for (so = slirp->icmp.so_next; so != &slirp->icmp; | |
522 | so = so_next) { | |
523 | so_next = so->so_next; | |
524 | ||
8917c3bd SH |
525 | so->pollfds_idx = -1; |
526 | ||
cf1d078e SH |
527 | /* |
528 | * See if it's timed out | |
529 | */ | |
530 | if (so->so_expire) { | |
531 | if (so->so_expire <= curtime) { | |
532 | icmp_detach(so); | |
533 | continue; | |
534 | } else { | |
fe0ff43c | 535 | slirp->do_slowtimo = true; /* Let socket expire */ |
cf1d078e SH |
536 | } |
537 | } | |
538 | ||
539 | if (so->so_state & SS_ISFCONNECTED) { | |
8917c3bd SH |
540 | GPollFD pfd = { |
541 | .fd = so->s, | |
542 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, | |
543 | }; | |
544 | so->pollfds_idx = pollfds->len; | |
545 | g_array_append_val(pollfds, pfd); | |
cf1d078e SH |
546 | } |
547 | } | |
548 | } | |
a42e9c41 | 549 | slirp_update_timeout(timeout); |
5fafdf24 | 550 | } |
f0cbd3ec | 551 | |
8917c3bd | 552 | void slirp_pollfds_poll(GArray *pollfds, int select_error) |
f0cbd3ec | 553 | { |
b1c99fcd | 554 | Slirp *slirp; |
f0cbd3ec FB |
555 | struct socket *so, *so_next; |
556 | int ret; | |
557 | ||
72cf2d4f | 558 | if (QTAILQ_EMPTY(&slirp_instances)) { |
d918f23e JK |
559 | return; |
560 | } | |
561 | ||
bc72ad67 | 562 | curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
5fafdf24 | 563 | |
72cf2d4f | 564 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
cf1d078e SH |
565 | /* |
566 | * See if anything has timed out | |
567 | */ | |
9b0ca6cc LPF |
568 | if (slirp->time_fasttimo && |
569 | ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) { | |
cf1d078e | 570 | tcp_fasttimo(slirp); |
fe0ff43c | 571 | slirp->time_fasttimo = 0; |
cf1d078e | 572 | } |
9b0ca6cc LPF |
573 | if (slirp->do_slowtimo && |
574 | ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) { | |
cf1d078e SH |
575 | ip_slowtimo(slirp); |
576 | tcp_slowtimo(slirp); | |
fe0ff43c | 577 | slirp->last_slowtimo = curtime; |
cf1d078e SH |
578 | } |
579 | ||
580 | /* | |
581 | * Check sockets | |
582 | */ | |
583 | if (!select_error) { | |
584 | /* | |
585 | * Check TCP sockets | |
586 | */ | |
587 | for (so = slirp->tcb.so_next; so != &slirp->tcb; | |
588 | so = so_next) { | |
8917c3bd SH |
589 | int revents; |
590 | ||
cf1d078e SH |
591 | so_next = so->so_next; |
592 | ||
8917c3bd SH |
593 | revents = 0; |
594 | if (so->pollfds_idx != -1) { | |
595 | revents = g_array_index(pollfds, GPollFD, | |
596 | so->pollfds_idx).revents; | |
597 | } | |
598 | ||
cf1d078e SH |
599 | if (so->so_state & SS_NOFDREF || so->s == -1) { |
600 | continue; | |
601 | } | |
602 | ||
603 | /* | |
604 | * Check for URG data | |
605 | * This will soread as well, so no need to | |
8917c3bd | 606 | * test for G_IO_IN below if this succeeds |
cf1d078e | 607 | */ |
8917c3bd | 608 | if (revents & G_IO_PRI) { |
bfb1ac14 SL |
609 | ret = sorecvoob(so); |
610 | if (ret < 0) { | |
611 | /* Socket error might have resulted in the socket being | |
612 | * removed, do not try to do anything more with it. */ | |
613 | continue; | |
614 | } | |
cf1d078e SH |
615 | } |
616 | /* | |
617 | * Check sockets for reading | |
618 | */ | |
8917c3bd | 619 | else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) { |
cf1d078e SH |
620 | /* |
621 | * Check for incoming connections | |
622 | */ | |
623 | if (so->so_state & SS_FACCEPTCONN) { | |
624 | tcp_connect(so); | |
625 | continue; | |
626 | } /* else */ | |
627 | ret = soread(so); | |
628 | ||
629 | /* Output it if we read something */ | |
630 | if (ret > 0) { | |
631 | tcp_output(sototcpcb(so)); | |
632 | } | |
bfb1ac14 SL |
633 | if (ret < 0) { |
634 | /* Socket error might have resulted in the socket being | |
635 | * removed, do not try to do anything more with it. */ | |
636 | continue; | |
637 | } | |
cf1d078e SH |
638 | } |
639 | ||
640 | /* | |
641 | * Check sockets for writing | |
642 | */ | |
8917c3bd SH |
643 | if (!(so->so_state & SS_NOFDREF) && |
644 | (revents & (G_IO_OUT | G_IO_ERR))) { | |
cf1d078e SH |
645 | /* |
646 | * Check for non-blocking, still-connecting sockets | |
647 | */ | |
648 | if (so->so_state & SS_ISFCONNECTING) { | |
649 | /* Connected */ | |
650 | so->so_state &= ~SS_ISFCONNECTING; | |
651 | ||
652 | ret = send(so->s, (const void *) &ret, 0, 0); | |
653 | if (ret < 0) { | |
654 | /* XXXXX Must fix, zero bytes is a NOP */ | |
655 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
656 | errno == EINPROGRESS || errno == ENOTCONN) { | |
657 | continue; | |
658 | } | |
659 | ||
660 | /* else failed */ | |
661 | so->so_state &= SS_PERSISTENT_MASK; | |
662 | so->so_state |= SS_NOFDREF; | |
f0cbd3ec | 663 | } |
cf1d078e SH |
664 | /* else so->so_state &= ~SS_ISFCONNECTING; */ |
665 | ||
666 | /* | |
667 | * Continue tcp_input | |
668 | */ | |
9dfbf250 GS |
669 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so, |
670 | so->so_ffamily); | |
cf1d078e SH |
671 | /* continue; */ |
672 | } else { | |
673 | ret = sowrite(so); | |
674 | } | |
675 | /* | |
676 | * XXXXX If we wrote something (a lot), there | |
677 | * could be a need for a window update. | |
678 | * In the worst case, the remote will send | |
679 | * a window probe to get things going again | |
680 | */ | |
681 | } | |
e6d43cfb JK |
682 | |
683 | /* | |
cf1d078e SH |
684 | * Probe a still-connecting, non-blocking socket |
685 | * to check if it's still alive | |
e6d43cfb | 686 | */ |
cf1d078e SH |
687 | #ifdef PROBE_CONN |
688 | if (so->so_state & SS_ISFCONNECTING) { | |
689 | ret = qemu_recv(so->s, &ret, 0, 0); | |
690 | ||
691 | if (ret < 0) { | |
692 | /* XXX */ | |
693 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
694 | errno == EINPROGRESS || errno == ENOTCONN) { | |
695 | continue; /* Still connecting, continue */ | |
696 | } | |
697 | ||
698 | /* else failed */ | |
699 | so->so_state &= SS_PERSISTENT_MASK; | |
700 | so->so_state |= SS_NOFDREF; | |
701 | ||
702 | /* tcp_input will take care of it */ | |
703 | } else { | |
704 | ret = send(so->s, &ret, 0, 0); | |
705 | if (ret < 0) { | |
706 | /* XXX */ | |
707 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
708 | errno == EINPROGRESS || errno == ENOTCONN) { | |
709 | continue; | |
710 | } | |
711 | /* else failed */ | |
712 | so->so_state &= SS_PERSISTENT_MASK; | |
713 | so->so_state |= SS_NOFDREF; | |
714 | } else { | |
715 | so->so_state &= ~SS_ISFCONNECTING; | |
716 | } | |
e6d43cfb | 717 | |
e6d43cfb | 718 | } |
9dfbf250 GS |
719 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so, |
720 | so->so_ffamily); | |
cf1d078e SH |
721 | } /* SS_ISFCONNECTING */ |
722 | #endif | |
723 | } | |
724 | ||
725 | /* | |
726 | * Now UDP sockets. | |
727 | * Incoming packets are sent straight away, they're not buffered. | |
728 | * Incoming UDP data isn't buffered either. | |
729 | */ | |
730 | for (so = slirp->udb.so_next; so != &slirp->udb; | |
731 | so = so_next) { | |
8917c3bd SH |
732 | int revents; |
733 | ||
cf1d078e SH |
734 | so_next = so->so_next; |
735 | ||
8917c3bd SH |
736 | revents = 0; |
737 | if (so->pollfds_idx != -1) { | |
738 | revents = g_array_index(pollfds, GPollFD, | |
739 | so->pollfds_idx).revents; | |
740 | } | |
741 | ||
742 | if (so->s != -1 && | |
743 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) { | |
cf1d078e | 744 | sorecvfrom(so); |
e6d43cfb | 745 | } |
cf1d078e SH |
746 | } |
747 | ||
748 | /* | |
749 | * Check incoming ICMP relies. | |
750 | */ | |
751 | for (so = slirp->icmp.so_next; so != &slirp->icmp; | |
752 | so = so_next) { | |
8917c3bd SH |
753 | int revents; |
754 | ||
755 | so_next = so->so_next; | |
756 | ||
757 | revents = 0; | |
758 | if (so->pollfds_idx != -1) { | |
759 | revents = g_array_index(pollfds, GPollFD, | |
760 | so->pollfds_idx).revents; | |
761 | } | |
cf1d078e | 762 | |
8917c3bd SH |
763 | if (so->s != -1 && |
764 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) { | |
cf1d078e SH |
765 | icmp_receive(so); |
766 | } | |
767 | } | |
768 | } | |
5fafdf24 | 769 | |
f3734319 | 770 | if_start(slirp); |
b1c99fcd | 771 | } |
f0cbd3ec FB |
772 | } |
773 | ||
460fec67 | 774 | static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
f0cbd3ec | 775 | { |
1f8b56e7 | 776 | struct slirp_arphdr *ah = (struct slirp_arphdr *)(pkt + ETH_HLEN); |
893dcdbf | 777 | uint8_t arp_reply[MAX(ETH_HLEN + sizeof(struct slirp_arphdr), 64)]; |
f0cbd3ec | 778 | struct ethhdr *reh = (struct ethhdr *)arp_reply; |
1f8b56e7 | 779 | struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_reply + ETH_HLEN); |
f0cbd3ec | 780 | int ar_op; |
a3d4af03 | 781 | struct ex_list *ex_ptr; |
f0cbd3ec | 782 | |
0b11c036 ST |
783 | if (!slirp->in_enabled) { |
784 | return; | |
785 | } | |
786 | ||
f0cbd3ec FB |
787 | ar_op = ntohs(ah->ar_op); |
788 | switch(ar_op) { | |
789 | case ARPOP_REQUEST: | |
1a0ca1e1 FC |
790 | if (ah->ar_tip == ah->ar_sip) { |
791 | /* Gratuitous ARP */ | |
792 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); | |
793 | return; | |
794 | } | |
795 | ||
460fec67 JK |
796 | if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) == |
797 | slirp->vnetwork_addr.s_addr) { | |
798 | if (ah->ar_tip == slirp->vnameserver_addr.s_addr || | |
799 | ah->ar_tip == slirp->vhost_addr.s_addr) | |
a3d4af03 | 800 | goto arp_ok; |
460fec67 | 801 | for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
a13a4126 | 802 | if (ex_ptr->ex_addr.s_addr == ah->ar_tip) |
a3d4af03 FB |
803 | goto arp_ok; |
804 | } | |
805 | return; | |
806 | arp_ok: | |
dbf3c4b4 | 807 | memset(arp_reply, 0, sizeof(arp_reply)); |
1a0ca1e1 FC |
808 | |
809 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); | |
f0cbd3ec FB |
810 | |
811 | /* ARP request for alias/dns mac address */ | |
812 | memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN); | |
a13a4126 JK |
813 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
814 | memcpy(&reh->h_source[2], &ah->ar_tip, 4); | |
f0cbd3ec FB |
815 | reh->h_proto = htons(ETH_P_ARP); |
816 | ||
817 | rah->ar_hrd = htons(1); | |
818 | rah->ar_pro = htons(ETH_P_IP); | |
819 | rah->ar_hln = ETH_ALEN; | |
820 | rah->ar_pln = 4; | |
821 | rah->ar_op = htons(ARPOP_REPLY); | |
822 | memcpy(rah->ar_sha, reh->h_source, ETH_ALEN); | |
a13a4126 | 823 | rah->ar_sip = ah->ar_tip; |
f0cbd3ec | 824 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN); |
a13a4126 | 825 | rah->ar_tip = ah->ar_sip; |
9f8bd042 | 826 | slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply)); |
f0cbd3ec FB |
827 | } |
828 | break; | |
de806f07 | 829 | case ARPOP_REPLY: |
1a0ca1e1 | 830 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
de806f07 | 831 | break; |
f0cbd3ec FB |
832 | default: |
833 | break; | |
834 | } | |
835 | } | |
836 | ||
9f8bd042 | 837 | void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
f0cbd3ec FB |
838 | { |
839 | struct mbuf *m; | |
840 | int proto; | |
841 | ||
842 | if (pkt_len < ETH_HLEN) | |
843 | return; | |
3b46e624 | 844 | |
f0cbd3ec FB |
845 | proto = ntohs(*(uint16_t *)(pkt + 12)); |
846 | switch(proto) { | |
847 | case ETH_P_ARP: | |
460fec67 | 848 | arp_input(slirp, pkt, pkt_len); |
f0cbd3ec FB |
849 | break; |
850 | case ETH_P_IP: | |
0d6ff71a | 851 | case ETH_P_IPV6: |
460fec67 | 852 | m = m_get(slirp); |
f0cbd3ec FB |
853 | if (!m) |
854 | return; | |
98c63057 GS |
855 | /* Note: we add 2 to align the IP header on 4 bytes, |
856 | * and add the margin for the tcpiphdr overhead */ | |
857 | if (M_FREEROOM(m) < pkt_len + TCPIPHDR_DELTA + 2) { | |
858 | m_inc(m, pkt_len + TCPIPHDR_DELTA + 2); | |
e8e880a7 | 859 | } |
98c63057 GS |
860 | m->m_len = pkt_len + TCPIPHDR_DELTA + 2; |
861 | memcpy(m->m_data + TCPIPHDR_DELTA + 2, pkt, pkt_len); | |
f0cbd3ec | 862 | |
98c63057 GS |
863 | m->m_data += TCPIPHDR_DELTA + 2 + ETH_HLEN; |
864 | m->m_len -= TCPIPHDR_DELTA + 2 + ETH_HLEN; | |
f0cbd3ec | 865 | |
0d6ff71a GS |
866 | if (proto == ETH_P_IP) { |
867 | ip_input(m); | |
868 | } else if (proto == ETH_P_IPV6) { | |
869 | ip6_input(m); | |
870 | } | |
f0cbd3ec | 871 | break; |
0d6ff71a | 872 | |
f0cbd3ec FB |
873 | default: |
874 | break; | |
875 | } | |
876 | } | |
877 | ||
18137fba GS |
878 | /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no |
879 | * packet should be sent, 0 if the packet must be re-queued, 2 if the packet | |
880 | * is ready to go. | |
1ab74cea | 881 | */ |
18137fba GS |
882 | static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh, |
883 | uint8_t ethaddr[ETH_ALEN]) | |
f0cbd3ec | 884 | { |
1ab74cea | 885 | const struct ip *iph = (const struct ip *)ifm->m_data; |
f0cbd3ec | 886 | |
959e4147 ST |
887 | if (iph->ip_dst.s_addr == 0) { |
888 | /* 0.0.0.0 can not be a destination address, something went wrong, | |
889 | * avoid making it worse */ | |
890 | return 1; | |
891 | } | |
1a0ca1e1 | 892 | if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) { |
1f8b56e7 | 893 | uint8_t arp_req[ETH_HLEN + sizeof(struct slirp_arphdr)]; |
de806f07 | 894 | struct ethhdr *reh = (struct ethhdr *)arp_req; |
1f8b56e7 | 895 | struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_req + ETH_HLEN); |
de806f07 | 896 | |
fc3779a1 | 897 | if (!ifm->resolution_requested) { |
1ab74cea FC |
898 | /* If the client addr is not known, send an ARP request */ |
899 | memset(reh->h_dest, 0xff, ETH_ALEN); | |
900 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); | |
901 | memcpy(&reh->h_source[2], &slirp->vhost_addr, 4); | |
902 | reh->h_proto = htons(ETH_P_ARP); | |
903 | rah->ar_hrd = htons(1); | |
904 | rah->ar_pro = htons(ETH_P_IP); | |
905 | rah->ar_hln = ETH_ALEN; | |
906 | rah->ar_pln = 4; | |
907 | rah->ar_op = htons(ARPOP_REQUEST); | |
908 | ||
909 | /* source hw addr */ | |
910 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4); | |
911 | memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4); | |
912 | ||
913 | /* source IP */ | |
914 | rah->ar_sip = slirp->vhost_addr.s_addr; | |
915 | ||
916 | /* target hw addr (none) */ | |
917 | memset(rah->ar_tha, 0, ETH_ALEN); | |
918 | ||
919 | /* target IP */ | |
920 | rah->ar_tip = iph->ip_dst.s_addr; | |
921 | slirp->client_ipaddr = iph->ip_dst; | |
922 | slirp_output(slirp->opaque, arp_req, sizeof(arp_req)); | |
fc3779a1 | 923 | ifm->resolution_requested = true; |
e3a110b5 JK |
924 | |
925 | /* Expire request and drop outgoing packet after 1 second */ | |
bc72ad67 | 926 | ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL; |
1ab74cea FC |
927 | } |
928 | return 0; | |
de806f07 | 929 | } else { |
a13a4126 | 930 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4); |
de806f07 | 931 | /* XXX: not correct */ |
460fec67 | 932 | memcpy(&eh->h_source[2], &slirp->vhost_addr, 4); |
de806f07 | 933 | eh->h_proto = htons(ETH_P_IP); |
18137fba GS |
934 | |
935 | /* Send this */ | |
936 | return 2; | |
937 | } | |
938 | } | |
939 | ||
0d6ff71a GS |
940 | /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no |
941 | * packet should be sent, 0 if the packet must be re-queued, 2 if the packet | |
942 | * is ready to go. | |
943 | */ | |
944 | static int if_encap6(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh, | |
945 | uint8_t ethaddr[ETH_ALEN]) | |
946 | { | |
947 | const struct ip6 *ip6h = mtod(ifm, const struct ip6 *); | |
948 | if (!ndp_table_search(slirp, ip6h->ip_dst, ethaddr)) { | |
949 | if (!ifm->resolution_requested) { | |
950 | ndp_send_ns(slirp, ip6h->ip_dst); | |
951 | ifm->resolution_requested = true; | |
952 | ifm->expiration_date = | |
953 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL; | |
954 | } | |
955 | return 0; | |
956 | } else { | |
957 | eh->h_proto = htons(ETH_P_IPV6); | |
958 | in6_compute_ethaddr(ip6h->ip_src, eh->h_source); | |
959 | ||
960 | /* Send this */ | |
961 | return 2; | |
962 | } | |
963 | } | |
964 | ||
18137fba GS |
965 | /* Output the IP packet to the ethernet device. Returns 0 if the packet must be |
966 | * re-queued. | |
967 | */ | |
968 | int if_encap(Slirp *slirp, struct mbuf *ifm) | |
969 | { | |
970 | uint8_t buf[1600]; | |
971 | struct ethhdr *eh = (struct ethhdr *)buf; | |
972 | uint8_t ethaddr[ETH_ALEN]; | |
973 | const struct ip *iph = (const struct ip *)ifm->m_data; | |
974 | int ret; | |
975 | ||
976 | if (ifm->m_len + ETH_HLEN > sizeof(buf)) { | |
1ab74cea | 977 | return 1; |
de806f07 | 978 | } |
18137fba GS |
979 | |
980 | switch (iph->ip_v) { | |
981 | case IPVERSION: | |
982 | ret = if_encap4(slirp, ifm, eh, ethaddr); | |
983 | if (ret < 2) { | |
984 | return ret; | |
985 | } | |
986 | break; | |
987 | ||
0d6ff71a GS |
988 | case IP6VERSION: |
989 | ret = if_encap6(slirp, ifm, eh, ethaddr); | |
990 | if (ret < 2) { | |
991 | return ret; | |
992 | } | |
993 | break; | |
994 | ||
18137fba | 995 | default: |
0d6ff71a | 996 | g_assert_not_reached(); |
18137fba GS |
997 | break; |
998 | } | |
999 | ||
1000 | memcpy(eh->h_dest, ethaddr, ETH_ALEN); | |
1001 | DEBUG_ARGS((dfd, " src = %02x:%02x:%02x:%02x:%02x:%02x\n", | |
1002 | eh->h_source[0], eh->h_source[1], eh->h_source[2], | |
1003 | eh->h_source[3], eh->h_source[4], eh->h_source[5])); | |
1004 | DEBUG_ARGS((dfd, " dst = %02x:%02x:%02x:%02x:%02x:%02x\n", | |
1005 | eh->h_dest[0], eh->h_dest[1], eh->h_dest[2], | |
1006 | eh->h_dest[3], eh->h_dest[4], eh->h_dest[5])); | |
1007 | memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len); | |
1008 | slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN); | |
1009 | return 1; | |
f0cbd3ec | 1010 | } |
9bf05444 | 1011 | |
9c12a6f2 | 1012 | /* Drop host forwarding rule, return 0 if found. */ |
9f8bd042 JK |
1013 | int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
1014 | int host_port) | |
c1261d8d AG |
1015 | { |
1016 | struct socket *so; | |
460fec67 | 1017 | struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb); |
2ad82cf9 JK |
1018 | struct sockaddr_in addr; |
1019 | int port = htons(host_port); | |
1020 | socklen_t addr_len; | |
c1261d8d | 1021 | |
c1261d8d | 1022 | for (so = head->so_next; so != head; so = so->so_next) { |
2ad82cf9 | 1023 | addr_len = sizeof(addr); |
9c12a6f2 JK |
1024 | if ((so->so_state & SS_HOSTFWD) && |
1025 | getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && | |
3c6a0580 | 1026 | addr.sin_addr.s_addr == host_addr.s_addr && |
2ad82cf9 | 1027 | addr.sin_port == port) { |
c1261d8d AG |
1028 | close(so->s); |
1029 | sofree(so); | |
9c12a6f2 | 1030 | return 0; |
c1261d8d AG |
1031 | } |
1032 | } | |
1033 | ||
9c12a6f2 | 1034 | return -1; |
c1261d8d AG |
1035 | } |
1036 | ||
9f8bd042 JK |
1037 | int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
1038 | int host_port, struct in_addr guest_addr, int guest_port) | |
9bf05444 | 1039 | { |
a13a4126 | 1040 | if (!guest_addr.s_addr) { |
460fec67 | 1041 | guest_addr = slirp->vdhcp_startaddr; |
a13a4126 | 1042 | } |
9bf05444 | 1043 | if (is_udp) { |
460fec67 JK |
1044 | if (!udp_listen(slirp, host_addr.s_addr, htons(host_port), |
1045 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) | |
9bf05444 FB |
1046 | return -1; |
1047 | } else { | |
460fec67 JK |
1048 | if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port), |
1049 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) | |
9bf05444 FB |
1050 | return -1; |
1051 | } | |
1052 | return 0; | |
1053 | } | |
a3d4af03 | 1054 | |
9f8bd042 | 1055 | int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, |
bb53fc53 | 1056 | struct in_addr *guest_addr, int guest_port) |
a3d4af03 | 1057 | { |
bb53fc53 JK |
1058 | if (!guest_addr->s_addr) { |
1059 | guest_addr->s_addr = slirp->vnetwork_addr.s_addr | | |
460fec67 | 1060 | (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr); |
c92ef6a2 | 1061 | } |
bb53fc53 | 1062 | if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) != |
460fec67 | 1063 | slirp->vnetwork_addr.s_addr || |
bb53fc53 JK |
1064 | guest_addr->s_addr == slirp->vhost_addr.s_addr || |
1065 | guest_addr->s_addr == slirp->vnameserver_addr.s_addr) { | |
a13a4126 JK |
1066 | return -1; |
1067 | } | |
bb53fc53 | 1068 | return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr, |
a13a4126 | 1069 | htons(guest_port)); |
a3d4af03 | 1070 | } |
e1c5a2b3 AL |
1071 | |
1072 | ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags) | |
1073 | { | |
cf1d078e | 1074 | if (so->s == -1 && so->extra) { |
6ab3fc32 DB |
1075 | /* XXX this blocks entire thread. Rewrite to use |
1076 | * qemu_chr_fe_write and background I/O callbacks */ | |
1077 | qemu_chr_fe_write_all(so->extra, buf, len); | |
cf1d078e SH |
1078 | return len; |
1079 | } | |
e1c5a2b3 | 1080 | |
cf1d078e | 1081 | return send(so->s, buf, len, flags); |
e1c5a2b3 AL |
1082 | } |
1083 | ||
a13a4126 | 1084 | static struct socket * |
460fec67 | 1085 | slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port) |
e1c5a2b3 | 1086 | { |
a13a4126 | 1087 | struct socket *so; |
e1c5a2b3 | 1088 | |
460fec67 | 1089 | for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) { |
a13a4126 JK |
1090 | if (so->so_faddr.s_addr == guest_addr.s_addr && |
1091 | htons(so->so_fport) == guest_port) { | |
1092 | return so; | |
1093 | } | |
1094 | } | |
1095 | return NULL; | |
e1c5a2b3 AL |
1096 | } |
1097 | ||
9f8bd042 JK |
1098 | size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr, |
1099 | int guest_port) | |
e1c5a2b3 | 1100 | { |
cf1d078e SH |
1101 | struct iovec iov[2]; |
1102 | struct socket *so; | |
e1c5a2b3 | 1103 | |
cf1d078e | 1104 | so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
e1c5a2b3 | 1105 | |
cf1d078e SH |
1106 | if (!so || so->so_state & SS_NOFDREF) { |
1107 | return 0; | |
1108 | } | |
e1c5a2b3 | 1109 | |
cf1d078e SH |
1110 | if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) { |
1111 | return 0; | |
1112 | } | |
e1c5a2b3 | 1113 | |
cf1d078e | 1114 | return sopreprbuf(so, iov, NULL); |
e1c5a2b3 AL |
1115 | } |
1116 | ||
9f8bd042 | 1117 | void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, |
c92ef6a2 | 1118 | const uint8_t *buf, int size) |
e1c5a2b3 AL |
1119 | { |
1120 | int ret; | |
460fec67 | 1121 | struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
a13a4126 | 1122 | |
e1c5a2b3 AL |
1123 | if (!so) |
1124 | return; | |
1125 | ||
0580ac91 | 1126 | ret = soreadbuf(so, (const char *)buf, size); |
e1c5a2b3 AL |
1127 | |
1128 | if (ret > 0) | |
1129 | tcp_output(sototcpcb(so)); | |
1130 | } | |
062e5527 AL |
1131 | |
1132 | static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp) | |
1133 | { | |
1134 | int i; | |
1135 | ||
1136 | qemu_put_sbe16(f, tp->t_state); | |
1137 | for (i = 0; i < TCPT_NTIMERS; i++) | |
1138 | qemu_put_sbe16(f, tp->t_timer[i]); | |
1139 | qemu_put_sbe16(f, tp->t_rxtshift); | |
1140 | qemu_put_sbe16(f, tp->t_rxtcur); | |
1141 | qemu_put_sbe16(f, tp->t_dupacks); | |
1142 | qemu_put_be16(f, tp->t_maxseg); | |
1143 | qemu_put_sbyte(f, tp->t_force); | |
1144 | qemu_put_be16(f, tp->t_flags); | |
1145 | qemu_put_be32(f, tp->snd_una); | |
1146 | qemu_put_be32(f, tp->snd_nxt); | |
1147 | qemu_put_be32(f, tp->snd_up); | |
1148 | qemu_put_be32(f, tp->snd_wl1); | |
1149 | qemu_put_be32(f, tp->snd_wl2); | |
1150 | qemu_put_be32(f, tp->iss); | |
1151 | qemu_put_be32(f, tp->snd_wnd); | |
1152 | qemu_put_be32(f, tp->rcv_wnd); | |
1153 | qemu_put_be32(f, tp->rcv_nxt); | |
1154 | qemu_put_be32(f, tp->rcv_up); | |
1155 | qemu_put_be32(f, tp->irs); | |
1156 | qemu_put_be32(f, tp->rcv_adv); | |
1157 | qemu_put_be32(f, tp->snd_max); | |
1158 | qemu_put_be32(f, tp->snd_cwnd); | |
1159 | qemu_put_be32(f, tp->snd_ssthresh); | |
1160 | qemu_put_sbe16(f, tp->t_idle); | |
1161 | qemu_put_sbe16(f, tp->t_rtt); | |
1162 | qemu_put_be32(f, tp->t_rtseq); | |
1163 | qemu_put_sbe16(f, tp->t_srtt); | |
1164 | qemu_put_sbe16(f, tp->t_rttvar); | |
1165 | qemu_put_be16(f, tp->t_rttmin); | |
1166 | qemu_put_be32(f, tp->max_sndwnd); | |
1167 | qemu_put_byte(f, tp->t_oobflags); | |
1168 | qemu_put_byte(f, tp->t_iobc); | |
1169 | qemu_put_sbe16(f, tp->t_softerror); | |
1170 | qemu_put_byte(f, tp->snd_scale); | |
1171 | qemu_put_byte(f, tp->rcv_scale); | |
1172 | qemu_put_byte(f, tp->request_r_scale); | |
1173 | qemu_put_byte(f, tp->requested_s_scale); | |
1174 | qemu_put_be32(f, tp->ts_recent); | |
1175 | qemu_put_be32(f, tp->ts_recent_age); | |
1176 | qemu_put_be32(f, tp->last_ack_sent); | |
1177 | } | |
1178 | ||
1179 | static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf) | |
1180 | { | |
1181 | uint32_t off; | |
1182 | ||
1183 | qemu_put_be32(f, sbuf->sb_cc); | |
1184 | qemu_put_be32(f, sbuf->sb_datalen); | |
1185 | off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data); | |
1186 | qemu_put_sbe32(f, off); | |
1187 | off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data); | |
1188 | qemu_put_sbe32(f, off); | |
1189 | qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen); | |
1190 | } | |
1191 | ||
1192 | static void slirp_socket_save(QEMUFile *f, struct socket *so) | |
1193 | { | |
1194 | qemu_put_be32(f, so->so_urgc); | |
eae303ff GS |
1195 | qemu_put_be16(f, so->so_ffamily); |
1196 | switch (so->so_ffamily) { | |
1197 | case AF_INET: | |
1198 | qemu_put_be32(f, so->so_faddr.s_addr); | |
1199 | qemu_put_be16(f, so->so_fport); | |
1200 | break; | |
1201 | default: | |
df3c286c MA |
1202 | error_report("so_ffamily unknown, unable to save so_faddr and" |
1203 | " so_fport"); | |
eae303ff GS |
1204 | } |
1205 | qemu_put_be16(f, so->so_lfamily); | |
1206 | switch (so->so_lfamily) { | |
1207 | case AF_INET: | |
1208 | qemu_put_be32(f, so->so_laddr.s_addr); | |
1209 | qemu_put_be16(f, so->so_lport); | |
1210 | break; | |
1211 | default: | |
df3c286c MA |
1212 | error_report("so_ffamily unknown, unable to save so_laddr and" |
1213 | " so_lport"); | |
eae303ff | 1214 | } |
062e5527 AL |
1215 | qemu_put_byte(f, so->so_iptos); |
1216 | qemu_put_byte(f, so->so_emu); | |
1217 | qemu_put_byte(f, so->so_type); | |
1218 | qemu_put_be32(f, so->so_state); | |
1219 | slirp_sbuf_save(f, &so->so_rcv); | |
1220 | slirp_sbuf_save(f, &so->so_snd); | |
1221 | slirp_tcp_save(f, so->so_tcpcb); | |
1222 | } | |
1223 | ||
0a1f851e JK |
1224 | static void slirp_bootp_save(QEMUFile *f, Slirp *slirp) |
1225 | { | |
1226 | int i; | |
1227 | ||
1228 | for (i = 0; i < NB_BOOTP_CLIENTS; i++) { | |
1229 | qemu_put_be16(f, slirp->bootp_clients[i].allocated); | |
1230 | qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6); | |
1231 | } | |
1232 | } | |
1233 | ||
062e5527 AL |
1234 | static void slirp_state_save(QEMUFile *f, void *opaque) |
1235 | { | |
460fec67 | 1236 | Slirp *slirp = opaque; |
062e5527 AL |
1237 | struct ex_list *ex_ptr; |
1238 | ||
460fec67 | 1239 | for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) |
062e5527 AL |
1240 | if (ex_ptr->ex_pty == 3) { |
1241 | struct socket *so; | |
460fec67 JK |
1242 | so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr, |
1243 | ntohs(ex_ptr->ex_fport)); | |
062e5527 AL |
1244 | if (!so) |
1245 | continue; | |
1246 | ||
1247 | qemu_put_byte(f, 42); | |
1248 | slirp_socket_save(f, so); | |
1249 | } | |
1250 | qemu_put_byte(f, 0); | |
285f7a62 | 1251 | |
460fec67 | 1252 | qemu_put_be16(f, slirp->ip_id); |
0a1f851e JK |
1253 | |
1254 | slirp_bootp_save(f, slirp); | |
062e5527 AL |
1255 | } |
1256 | ||
1257 | static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp) | |
1258 | { | |
1259 | int i; | |
1260 | ||
1261 | tp->t_state = qemu_get_sbe16(f); | |
1262 | for (i = 0; i < TCPT_NTIMERS; i++) | |
1263 | tp->t_timer[i] = qemu_get_sbe16(f); | |
1264 | tp->t_rxtshift = qemu_get_sbe16(f); | |
1265 | tp->t_rxtcur = qemu_get_sbe16(f); | |
1266 | tp->t_dupacks = qemu_get_sbe16(f); | |
1267 | tp->t_maxseg = qemu_get_be16(f); | |
1268 | tp->t_force = qemu_get_sbyte(f); | |
1269 | tp->t_flags = qemu_get_be16(f); | |
1270 | tp->snd_una = qemu_get_be32(f); | |
1271 | tp->snd_nxt = qemu_get_be32(f); | |
1272 | tp->snd_up = qemu_get_be32(f); | |
1273 | tp->snd_wl1 = qemu_get_be32(f); | |
1274 | tp->snd_wl2 = qemu_get_be32(f); | |
1275 | tp->iss = qemu_get_be32(f); | |
1276 | tp->snd_wnd = qemu_get_be32(f); | |
1277 | tp->rcv_wnd = qemu_get_be32(f); | |
1278 | tp->rcv_nxt = qemu_get_be32(f); | |
1279 | tp->rcv_up = qemu_get_be32(f); | |
1280 | tp->irs = qemu_get_be32(f); | |
1281 | tp->rcv_adv = qemu_get_be32(f); | |
1282 | tp->snd_max = qemu_get_be32(f); | |
1283 | tp->snd_cwnd = qemu_get_be32(f); | |
1284 | tp->snd_ssthresh = qemu_get_be32(f); | |
1285 | tp->t_idle = qemu_get_sbe16(f); | |
1286 | tp->t_rtt = qemu_get_sbe16(f); | |
1287 | tp->t_rtseq = qemu_get_be32(f); | |
1288 | tp->t_srtt = qemu_get_sbe16(f); | |
1289 | tp->t_rttvar = qemu_get_sbe16(f); | |
1290 | tp->t_rttmin = qemu_get_be16(f); | |
1291 | tp->max_sndwnd = qemu_get_be32(f); | |
1292 | tp->t_oobflags = qemu_get_byte(f); | |
1293 | tp->t_iobc = qemu_get_byte(f); | |
1294 | tp->t_softerror = qemu_get_sbe16(f); | |
1295 | tp->snd_scale = qemu_get_byte(f); | |
1296 | tp->rcv_scale = qemu_get_byte(f); | |
1297 | tp->request_r_scale = qemu_get_byte(f); | |
1298 | tp->requested_s_scale = qemu_get_byte(f); | |
1299 | tp->ts_recent = qemu_get_be32(f); | |
1300 | tp->ts_recent_age = qemu_get_be32(f); | |
1301 | tp->last_ack_sent = qemu_get_be32(f); | |
1302 | tcp_template(tp); | |
1303 | } | |
1304 | ||
1305 | static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf) | |
1306 | { | |
1307 | uint32_t off, sb_cc, sb_datalen; | |
1308 | ||
1309 | sb_cc = qemu_get_be32(f); | |
1310 | sb_datalen = qemu_get_be32(f); | |
1311 | ||
1312 | sbreserve(sbuf, sb_datalen); | |
1313 | ||
1314 | if (sbuf->sb_datalen != sb_datalen) | |
1315 | return -ENOMEM; | |
1316 | ||
1317 | sbuf->sb_cc = sb_cc; | |
1318 | ||
1319 | off = qemu_get_sbe32(f); | |
1320 | sbuf->sb_wptr = sbuf->sb_data + off; | |
1321 | off = qemu_get_sbe32(f); | |
1322 | sbuf->sb_rptr = sbuf->sb_data + off; | |
1323 | qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen); | |
1324 | ||
1325 | return 0; | |
1326 | } | |
1327 | ||
eaf136f9 | 1328 | static int slirp_socket_load(QEMUFile *f, struct socket *so, int version_id) |
062e5527 AL |
1329 | { |
1330 | if (tcp_attach(so) < 0) | |
1331 | return -ENOMEM; | |
1332 | ||
1333 | so->so_urgc = qemu_get_be32(f); | |
eaf136f9 TH |
1334 | if (version_id <= 3) { |
1335 | so->so_ffamily = AF_INET; | |
eae303ff | 1336 | so->so_faddr.s_addr = qemu_get_be32(f); |
eae303ff | 1337 | so->so_laddr.s_addr = qemu_get_be32(f); |
eaf136f9 | 1338 | so->so_fport = qemu_get_be16(f); |
eae303ff | 1339 | so->so_lport = qemu_get_be16(f); |
eaf136f9 TH |
1340 | } else { |
1341 | so->so_ffamily = qemu_get_be16(f); | |
1342 | switch (so->so_ffamily) { | |
1343 | case AF_INET: | |
1344 | so->so_faddr.s_addr = qemu_get_be32(f); | |
1345 | so->so_fport = qemu_get_be16(f); | |
1346 | break; | |
1347 | default: | |
1348 | error_report( | |
1349 | "so_ffamily unknown, unable to restore so_faddr and so_lport"); | |
1350 | } | |
1351 | so->so_lfamily = qemu_get_be16(f); | |
1352 | switch (so->so_lfamily) { | |
1353 | case AF_INET: | |
1354 | so->so_laddr.s_addr = qemu_get_be32(f); | |
1355 | so->so_lport = qemu_get_be16(f); | |
1356 | break; | |
1357 | default: | |
1358 | error_report( | |
1359 | "so_ffamily unknown, unable to restore so_laddr and so_lport"); | |
1360 | } | |
eae303ff | 1361 | } |
062e5527 AL |
1362 | so->so_iptos = qemu_get_byte(f); |
1363 | so->so_emu = qemu_get_byte(f); | |
1364 | so->so_type = qemu_get_byte(f); | |
1365 | so->so_state = qemu_get_be32(f); | |
1366 | if (slirp_sbuf_load(f, &so->so_rcv) < 0) | |
1367 | return -ENOMEM; | |
1368 | if (slirp_sbuf_load(f, &so->so_snd) < 0) | |
1369 | return -ENOMEM; | |
1370 | slirp_tcp_load(f, so->so_tcpcb); | |
1371 | ||
1372 | return 0; | |
1373 | } | |
1374 | ||
0a1f851e JK |
1375 | static void slirp_bootp_load(QEMUFile *f, Slirp *slirp) |
1376 | { | |
1377 | int i; | |
1378 | ||
1379 | for (i = 0; i < NB_BOOTP_CLIENTS; i++) { | |
1380 | slirp->bootp_clients[i].allocated = qemu_get_be16(f); | |
1381 | qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6); | |
1382 | } | |
1383 | } | |
1384 | ||
062e5527 AL |
1385 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) |
1386 | { | |
460fec67 | 1387 | Slirp *slirp = opaque; |
062e5527 | 1388 | struct ex_list *ex_ptr; |
062e5527 | 1389 | |
b0e04867 | 1390 | while (qemu_get_byte(f)) { |
062e5527 | 1391 | int ret; |
460fec67 | 1392 | struct socket *so = socreate(slirp); |
062e5527 AL |
1393 | |
1394 | if (!so) | |
1395 | return -ENOMEM; | |
1396 | ||
eaf136f9 | 1397 | ret = slirp_socket_load(f, so, version_id); |
062e5527 AL |
1398 | |
1399 | if (ret < 0) | |
1400 | return ret; | |
1401 | ||
460fec67 JK |
1402 | if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) != |
1403 | slirp->vnetwork_addr.s_addr) { | |
062e5527 | 1404 | return -EINVAL; |
a13a4126 | 1405 | } |
460fec67 | 1406 | for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
062e5527 | 1407 | if (ex_ptr->ex_pty == 3 && |
a13a4126 JK |
1408 | so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr && |
1409 | so->so_fport == ex_ptr->ex_fport) { | |
062e5527 | 1410 | break; |
a13a4126 JK |
1411 | } |
1412 | } | |
062e5527 AL |
1413 | if (!ex_ptr) |
1414 | return -EINVAL; | |
1415 | ||
0580ac91 | 1416 | so->extra = (void *)ex_ptr->ex_exec; |
062e5527 AL |
1417 | } |
1418 | ||
285f7a62 | 1419 | if (version_id >= 2) { |
460fec67 | 1420 | slirp->ip_id = qemu_get_be16(f); |
285f7a62 JK |
1421 | } |
1422 | ||
0a1f851e JK |
1423 | if (version_id >= 3) { |
1424 | slirp_bootp_load(f, slirp); | |
1425 | } | |
1426 | ||
062e5527 AL |
1427 | return 0; |
1428 | } |