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