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