]> git.proxmox.com Git - mirror_qemu.git/blob - slirp/slirp.c
e63c5e8fc6493cca13a330ba5592b8e5dbc34b45
[mirror_qemu.git] / slirp / slirp.c
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 */
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "qemu/timer.h"
27 #include "qemu/error-report.h"
28 #include "sysemu/char.h"
29 #include "slirp.h"
30 #include "hw/hw.h"
31 #include "qemu/cutils.h"
32
33 /* host loopback address */
34 struct in_addr loopback_addr;
35 /* host loopback network mask */
36 unsigned long loopback_mask;
37
38 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
39 static const uint8_t special_ethaddr[ETH_ALEN] = {
40 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
41 };
42
43 u_int curtime;
44
45 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
46 QTAILQ_HEAD_INITIALIZER(slirp_instances);
47
48 static struct in_addr dns_addr;
49 static u_int dns_addr_time;
50
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
56 #ifdef _WIN32
57
58 int get_dns_addr(struct in_addr *pdns_addr)
59 {
60 FIXED_INFO *FixedInfo=NULL;
61 ULONG BufLen;
62 DWORD ret;
63 IP_ADDR_STRING *pIPAddr;
64 struct in_addr tmp_addr;
65
66 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
67 *pdns_addr = dns_addr;
68 return 0;
69 }
70
71 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
72 BufLen = sizeof(FIXED_INFO);
73
74 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
75 if (FixedInfo) {
76 GlobalFree(FixedInfo);
77 FixedInfo = NULL;
78 }
79 FixedInfo = GlobalAlloc(GPTR, BufLen);
80 }
81
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 }
90
91 pIPAddr = &(FixedInfo->DnsServerList);
92 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
93 *pdns_addr = tmp_addr;
94 dns_addr = tmp_addr;
95 dns_addr_time = curtime;
96 if (FixedInfo) {
97 GlobalFree(FixedInfo);
98 FixedInfo = NULL;
99 }
100 return 0;
101 }
102
103 static void winsock_cleanup(void)
104 {
105 WSACleanup();
106 }
107
108 #else
109
110 static struct stat dns_addr_stat;
111
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)
134 {
135 char buff[512];
136 char buff2[257];
137 FILE *f;
138 int found = 0;
139 struct in_addr tmp_addr;
140
141 f = fopen("/etc/resolv.conf", "r");
142 if (!f)
143 return -1;
144
145 #ifdef DEBUG
146 fprintf(stderr, "IP address of your DNS(s): ");
147 #endif
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;
152 /* If it's the first one, set it to dns_addr */
153 if (!found) {
154 *pdns_addr = tmp_addr;
155 dns_addr = tmp_addr;
156 dns_addr_time = curtime;
157 }
158 #ifdef DEBUG
159 else
160 fprintf(stderr, ", ");
161 #endif
162 if (++found > 3) {
163 #ifdef DEBUG
164 fprintf(stderr, "(more)");
165 #endif
166 break;
167 }
168 #ifdef DEBUG
169 else
170 fprintf(stderr, "%s", inet_ntoa(tmp_addr));
171 #endif
172 }
173 }
174 fclose(f);
175 if (!found)
176 return -1;
177 return 0;
178 }
179
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
192 #endif
193
194 static void slirp_init_once(void)
195 {
196 static int initialized;
197 #ifdef _WIN32
198 WSADATA Data;
199 #endif
200
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);
212 loopback_mask = htonl(IN_CLASSA_NET);
213 }
214
215 static void slirp_state_save(QEMUFile *f, void *opaque);
216 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
217
218 Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
219 struct in_addr vnetmask, struct in_addr vhost,
220 bool in6_enabled,
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,
226 void *opaque)
227 {
228 Slirp *slirp = g_malloc0(sizeof(Slirp));
229
230 slirp_init_once();
231
232 slirp->grand = g_rand_new();
233 slirp->restricted = restricted;
234
235 slirp->in_enabled = in_enabled;
236 slirp->in6_enabled = in6_enabled;
237
238 if_init(slirp);
239 ip_init(slirp);
240 ip6_init(slirp);
241
242 /* Initialise mbufs *after* setting the MTU */
243 m_init(slirp);
244
245 slirp->vnetwork_addr = vnetwork;
246 slirp->vnetwork_mask = vnetmask;
247 slirp->vhost_addr = vhost;
248 slirp->vprefix_addr6 = vprefix_addr6;
249 slirp->vprefix_len = vprefix_len;
250 slirp->vhost_addr6 = vhost6;
251 if (vhostname) {
252 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
253 vhostname);
254 }
255 slirp->tftp_prefix = g_strdup(tftp_path);
256 slirp->bootp_filename = g_strdup(bootfile);
257 slirp->vdhcp_startaddr = vdhcp_start;
258 slirp->vnameserver_addr = vnameserver;
259 slirp->vnameserver_addr6 = vnameserver6;
260
261 if (vdnssearch) {
262 translate_dnssearch(slirp, vdnssearch);
263 }
264
265 slirp->opaque = opaque;
266
267 register_savevm(NULL, "slirp", 0, 4,
268 slirp_state_save, slirp_state_load, slirp);
269
270 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
271
272 return slirp;
273 }
274
275 void slirp_cleanup(Slirp *slirp)
276 {
277 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
278
279 unregister_savevm(NULL, "slirp", slirp);
280
281 ip_cleanup(slirp);
282 ip6_cleanup(slirp);
283 m_cleanup(slirp);
284
285 g_rand_free(slirp->grand);
286
287 g_free(slirp->vdnssearch);
288 g_free(slirp->tftp_prefix);
289 g_free(slirp->bootp_filename);
290 g_free(slirp);
291 }
292
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)
295
296 static void slirp_update_timeout(uint32_t *timeout)
297 {
298 Slirp *slirp;
299 uint32_t t;
300
301 if (*timeout <= TIMEOUT_FAST) {
302 return;
303 }
304
305 t = MIN(1000, *timeout);
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 }
318 }
319 *timeout = t;
320 }
321
322 void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
323 {
324 Slirp *slirp;
325 struct socket *so, *so_next;
326
327 if (QTAILQ_EMPTY(&slirp_instances)) {
328 return;
329 }
330
331 /*
332 * First, TCP sockets
333 */
334
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 */
340 slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
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) {
345 int events = 0;
346
347 so_next = so->so_next;
348
349 so->pollfds_idx = -1;
350
351 /*
352 * See if we need a tcp_fasttimo
353 */
354 if (slirp->time_fasttimo == 0 &&
355 so->so_tcpcb->t_flags & TF_DELACK) {
356 slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
357 }
358
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 }
366
367 /*
368 * Set for reading sockets which are accepting
369 */
370 if (so->so_state & SS_FACCEPTCONN) {
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);
377 continue;
378 }
379
380 /*
381 * Set for writing sockets which are connecting
382 */
383 if (so->so_state & SS_ISFCONNECTING) {
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);
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) {
398 events |= G_IO_OUT | G_IO_ERR;
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))) {
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);
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
427 so->pollfds_idx = -1;
428
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 {
437 slirp->do_slowtimo = true; /* Let socket expire */
438 }
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) {
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);
458 }
459 }
460
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
468 so->pollfds_idx = -1;
469
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 {
478 slirp->do_slowtimo = true; /* Let socket expire */
479 }
480 }
481
482 if (so->so_state & SS_ISFCONNECTED) {
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);
489 }
490 }
491 }
492 slirp_update_timeout(timeout);
493 }
494
495 void slirp_pollfds_poll(GArray *pollfds, int select_error)
496 {
497 Slirp *slirp;
498 struct socket *so, *so_next;
499 int ret;
500
501 if (QTAILQ_EMPTY(&slirp_instances)) {
502 return;
503 }
504
505 curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
506
507 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
508 /*
509 * See if anything has timed out
510 */
511 if (slirp->time_fasttimo &&
512 ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) {
513 tcp_fasttimo(slirp);
514 slirp->time_fasttimo = 0;
515 }
516 if (slirp->do_slowtimo &&
517 ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
518 ip_slowtimo(slirp);
519 tcp_slowtimo(slirp);
520 slirp->last_slowtimo = curtime;
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) {
532 int revents;
533
534 so_next = so->so_next;
535
536 revents = 0;
537 if (so->pollfds_idx != -1) {
538 revents = g_array_index(pollfds, GPollFD,
539 so->pollfds_idx).revents;
540 }
541
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
549 * test for G_IO_IN below if this succeeds
550 */
551 if (revents & G_IO_PRI) {
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 }
558 }
559 /*
560 * Check sockets for reading
561 */
562 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
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 }
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 }
581 }
582
583 /*
584 * Check sockets for writing
585 */
586 if (!(so->so_state & SS_NOFDREF) &&
587 (revents & (G_IO_OUT | G_IO_ERR))) {
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;
606 }
607 /* else so->so_state &= ~SS_ISFCONNECTING; */
608
609 /*
610 * Continue tcp_input
611 */
612 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
613 so->so_ffamily);
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 }
625
626 /*
627 * Probe a still-connecting, non-blocking socket
628 * to check if it's still alive
629 */
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 }
660
661 }
662 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
663 so->so_ffamily);
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) {
675 int revents;
676
677 so_next = so->so_next;
678
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))) {
687 sorecvfrom(so);
688 }
689 }
690
691 /*
692 * Check incoming ICMP relies.
693 */
694 for (so = slirp->icmp.so_next; so != &slirp->icmp;
695 so = so_next) {
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 }
705
706 if (so->s != -1 &&
707 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
708 icmp_receive(so);
709 }
710 }
711 }
712
713 if_start(slirp);
714 }
715 }
716
717 static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
718 {
719 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
720 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
721 struct ethhdr *reh = (struct ethhdr *)arp_reply;
722 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
723 int ar_op;
724 struct ex_list *ex_ptr;
725
726 if (!slirp->in_enabled) {
727 return;
728 }
729
730 ar_op = ntohs(ah->ar_op);
731 switch(ar_op) {
732 case ARPOP_REQUEST:
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
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)
743 goto arp_ok;
744 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
745 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
746 goto arp_ok;
747 }
748 return;
749 arp_ok:
750 memset(arp_reply, 0, sizeof(arp_reply));
751
752 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
753
754 /* ARP request for alias/dns mac address */
755 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
756 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
757 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
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);
766 rah->ar_sip = ah->ar_tip;
767 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
768 rah->ar_tip = ah->ar_sip;
769 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
770 }
771 break;
772 case ARPOP_REPLY:
773 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
774 break;
775 default:
776 break;
777 }
778 }
779
780 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
781 {
782 struct mbuf *m;
783 int proto;
784
785 if (pkt_len < ETH_HLEN)
786 return;
787
788 proto = ntohs(*(uint16_t *)(pkt + 12));
789 switch(proto) {
790 case ETH_P_ARP:
791 arp_input(slirp, pkt, pkt_len);
792 break;
793 case ETH_P_IP:
794 case ETH_P_IPV6:
795 m = m_get(slirp);
796 if (!m)
797 return;
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);
802 }
803 m->m_len = pkt_len + TCPIPHDR_DELTA + 2;
804 memcpy(m->m_data + TCPIPHDR_DELTA + 2, pkt, pkt_len);
805
806 m->m_data += TCPIPHDR_DELTA + 2 + ETH_HLEN;
807 m->m_len -= TCPIPHDR_DELTA + 2 + ETH_HLEN;
808
809 if (proto == ETH_P_IP) {
810 ip_input(m);
811 } else if (proto == ETH_P_IPV6) {
812 ip6_input(m);
813 }
814 break;
815
816 default:
817 break;
818 }
819 }
820
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.
824 */
825 static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
826 uint8_t ethaddr[ETH_ALEN])
827 {
828 const struct ip *iph = (const struct ip *)ifm->m_data;
829
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 }
835 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
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);
839
840 if (!ifm->resolution_requested) {
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));
866 ifm->resolution_requested = true;
867
868 /* Expire request and drop outgoing packet after 1 second */
869 ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
870 }
871 return 0;
872 } else {
873 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
874 /* XXX: not correct */
875 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
876 eh->h_proto = htons(ETH_P_IP);
877
878 /* Send this */
879 return 2;
880 }
881 }
882
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
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)) {
920 return 1;
921 }
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
931 case IP6VERSION:
932 ret = if_encap6(slirp, ifm, eh, ethaddr);
933 if (ret < 2) {
934 return ret;
935 }
936 break;
937
938 default:
939 g_assert_not_reached();
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;
953 }
954
955 /* Drop host forwarding rule, return 0 if found. */
956 int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
957 int host_port)
958 {
959 struct socket *so;
960 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
961 struct sockaddr_in addr;
962 int port = htons(host_port);
963 socklen_t addr_len;
964
965 for (so = head->so_next; so != head; so = so->so_next) {
966 addr_len = sizeof(addr);
967 if ((so->so_state & SS_HOSTFWD) &&
968 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
969 addr.sin_addr.s_addr == host_addr.s_addr &&
970 addr.sin_port == port) {
971 close(so->s);
972 sofree(so);
973 return 0;
974 }
975 }
976
977 return -1;
978 }
979
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)
982 {
983 if (!guest_addr.s_addr) {
984 guest_addr = slirp->vdhcp_startaddr;
985 }
986 if (is_udp) {
987 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
988 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
989 return -1;
990 } else {
991 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
992 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
993 return -1;
994 }
995 return 0;
996 }
997
998 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
999 struct in_addr *guest_addr, int guest_port)
1000 {
1001 if (!guest_addr->s_addr) {
1002 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
1003 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
1004 }
1005 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
1006 slirp->vnetwork_addr.s_addr ||
1007 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
1008 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
1009 return -1;
1010 }
1011 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
1012 htons(guest_port));
1013 }
1014
1015 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
1016 {
1017 if (so->s == -1 && so->extra) {
1018 qemu_chr_fe_write(so->extra, buf, len);
1019 return len;
1020 }
1021
1022 return send(so->s, buf, len, flags);
1023 }
1024
1025 static struct socket *
1026 slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
1027 {
1028 struct socket *so;
1029
1030 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
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;
1037 }
1038
1039 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
1040 int guest_port)
1041 {
1042 struct iovec iov[2];
1043 struct socket *so;
1044
1045 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1046
1047 if (!so || so->so_state & SS_NOFDREF) {
1048 return 0;
1049 }
1050
1051 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) {
1052 return 0;
1053 }
1054
1055 return sopreprbuf(so, iov, NULL);
1056 }
1057
1058 void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
1059 const uint8_t *buf, int size)
1060 {
1061 int ret;
1062 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1063
1064 if (!so)
1065 return;
1066
1067 ret = soreadbuf(so, (const char *)buf, size);
1068
1069 if (ret > 0)
1070 tcp_output(sototcpcb(so));
1071 }
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);
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 }
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
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
1175 static void slirp_state_save(QEMUFile *f, void *opaque)
1176 {
1177 Slirp *slirp = opaque;
1178 struct ex_list *ex_ptr;
1179
1180 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1181 if (ex_ptr->ex_pty == 3) {
1182 struct socket *so;
1183 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
1184 ntohs(ex_ptr->ex_fport));
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);
1192
1193 qemu_put_be16(f, slirp->ip_id);
1194
1195 slirp_bootp_save(f, slirp);
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
1269 static int slirp_socket_load(QEMUFile *f, struct socket *so, int version_id)
1270 {
1271 if (tcp_attach(so) < 0)
1272 return -ENOMEM;
1273
1274 so->so_urgc = qemu_get_be32(f);
1275 if (version_id <= 3) {
1276 so->so_ffamily = AF_INET;
1277 so->so_faddr.s_addr = qemu_get_be32(f);
1278 so->so_laddr.s_addr = qemu_get_be32(f);
1279 so->so_fport = qemu_get_be16(f);
1280 so->so_lport = qemu_get_be16(f);
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 }
1302 }
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
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
1326 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1327 {
1328 Slirp *slirp = opaque;
1329 struct ex_list *ex_ptr;
1330
1331 while (qemu_get_byte(f)) {
1332 int ret;
1333 struct socket *so = socreate(slirp);
1334
1335 if (!so)
1336 return -ENOMEM;
1337
1338 ret = slirp_socket_load(f, so, version_id);
1339
1340 if (ret < 0)
1341 return ret;
1342
1343 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1344 slirp->vnetwork_addr.s_addr) {
1345 return -EINVAL;
1346 }
1347 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1348 if (ex_ptr->ex_pty == 3 &&
1349 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1350 so->so_fport == ex_ptr->ex_fport) {
1351 break;
1352 }
1353 }
1354 if (!ex_ptr)
1355 return -EINVAL;
1356
1357 so->extra = (void *)ex_ptr->ex_exec;
1358 }
1359
1360 if (version_id >= 2) {
1361 slirp->ip_id = qemu_get_be16(f);
1362 }
1363
1364 if (version_id >= 3) {
1365 slirp_bootp_load(f, slirp);
1366 }
1367
1368 return 0;
1369 }