maint: Allow for EXAMPLES in texi2pod
[qemu/ar7.git] / slirp / slirp.c
bloba9674ab0909960f0ab25076f62271b996ec97642
1 /*
2 * libslirp glue
4 * Copyright (c) 2004-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "qemu/timer.h"
27 #include "qemu/error-report.h"
28 #include "chardev/char-fe.h"
29 #include "migration/register.h"
30 #include "slirp.h"
31 #include "hw/hw.h"
32 #include "qemu/cutils.h"
34 #ifndef _WIN32
35 #include <net/if.h>
36 #endif
38 int slirp_debug;
40 /* Define to 1 if you want KEEPALIVE timers */
41 bool slirp_do_keepalive;
43 /* host loopback address */
44 struct in_addr loopback_addr;
45 /* host loopback network mask */
46 unsigned long loopback_mask;
48 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
49 static const uint8_t special_ethaddr[ETH_ALEN] = {
50 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
53 u_int curtime;
55 static QTAILQ_HEAD(, Slirp) slirp_instances =
56 QTAILQ_HEAD_INITIALIZER(slirp_instances);
58 static struct in_addr dns_addr;
59 #ifndef _WIN32
60 static struct in6_addr dns6_addr;
61 #endif
62 static u_int dns_addr_time;
63 #ifndef _WIN32
64 static u_int dns6_addr_time;
65 #endif
67 #define TIMEOUT_FAST 2 /* milliseconds */
68 #define TIMEOUT_SLOW 499 /* milliseconds */
69 /* for the aging of certain requests like DNS */
70 #define TIMEOUT_DEFAULT 1000 /* milliseconds */
72 #ifdef _WIN32
74 int get_dns_addr(struct in_addr *pdns_addr)
76 FIXED_INFO *FixedInfo=NULL;
77 ULONG BufLen;
78 DWORD ret;
79 IP_ADDR_STRING *pIPAddr;
80 struct in_addr tmp_addr;
82 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
83 *pdns_addr = dns_addr;
84 return 0;
87 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
88 BufLen = sizeof(FIXED_INFO);
90 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
91 if (FixedInfo) {
92 GlobalFree(FixedInfo);
93 FixedInfo = NULL;
95 FixedInfo = GlobalAlloc(GPTR, BufLen);
98 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
99 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
100 if (FixedInfo) {
101 GlobalFree(FixedInfo);
102 FixedInfo = NULL;
104 return -1;
107 pIPAddr = &(FixedInfo->DnsServerList);
108 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
109 *pdns_addr = tmp_addr;
110 dns_addr = tmp_addr;
111 dns_addr_time = curtime;
112 if (FixedInfo) {
113 GlobalFree(FixedInfo);
114 FixedInfo = NULL;
116 return 0;
119 int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id)
121 return -1;
124 static void winsock_cleanup(void)
126 WSACleanup();
129 #else
131 static int get_dns_addr_cached(void *pdns_addr, void *cached_addr,
132 socklen_t addrlen,
133 struct stat *cached_stat, u_int *cached_time)
135 struct stat old_stat;
136 if (curtime - *cached_time < TIMEOUT_DEFAULT) {
137 memcpy(pdns_addr, cached_addr, addrlen);
138 return 0;
140 old_stat = *cached_stat;
141 if (stat("/etc/resolv.conf", cached_stat) != 0) {
142 return -1;
144 if (cached_stat->st_dev == old_stat.st_dev
145 && cached_stat->st_ino == old_stat.st_ino
146 && cached_stat->st_size == old_stat.st_size
147 && cached_stat->st_mtime == old_stat.st_mtime) {
148 memcpy(pdns_addr, cached_addr, addrlen);
149 return 0;
151 return 1;
154 static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
155 socklen_t addrlen, uint32_t *scope_id,
156 u_int *cached_time)
158 char buff[512];
159 char buff2[257];
160 FILE *f;
161 int found = 0;
162 void *tmp_addr = alloca(addrlen);
163 unsigned if_index;
165 f = fopen("/etc/resolv.conf", "r");
166 if (!f)
167 return -1;
169 DEBUG_MISC("IP address of your DNS(s):");
170 while (fgets(buff, 512, f) != NULL) {
171 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
172 char *c = strchr(buff2, '%');
173 if (c) {
174 if_index = if_nametoindex(c + 1);
175 *c = '\0';
176 } else {
177 if_index = 0;
180 if (!inet_pton(af, buff2, tmp_addr)) {
181 continue;
183 /* If it's the first one, set it to dns_addr */
184 if (!found) {
185 memcpy(pdns_addr, tmp_addr, addrlen);
186 memcpy(cached_addr, tmp_addr, addrlen);
187 if (scope_id) {
188 *scope_id = if_index;
190 *cached_time = curtime;
193 if (++found > 3) {
194 DEBUG_MISC(" (more)");
195 break;
196 } else if (slirp_debug & DBG_MISC) {
197 char s[INET6_ADDRSTRLEN];
198 const char *res = inet_ntop(af, tmp_addr, s, sizeof(s));
199 if (!res) {
200 res = " (string conversion error)";
202 DEBUG_MISC(" %s", res);
206 fclose(f);
207 if (!found)
208 return -1;
209 return 0;
212 int get_dns_addr(struct in_addr *pdns_addr)
214 static struct stat dns_addr_stat;
216 if (dns_addr.s_addr != 0) {
217 int ret;
218 ret = get_dns_addr_cached(pdns_addr, &dns_addr, sizeof(dns_addr),
219 &dns_addr_stat, &dns_addr_time);
220 if (ret <= 0) {
221 return ret;
224 return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr,
225 sizeof(dns_addr), NULL, &dns_addr_time);
228 int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id)
230 static struct stat dns6_addr_stat;
232 if (!in6_zero(&dns6_addr)) {
233 int ret;
234 ret = get_dns_addr_cached(pdns6_addr, &dns6_addr, sizeof(dns6_addr),
235 &dns6_addr_stat, &dns6_addr_time);
236 if (ret <= 0) {
237 return ret;
240 return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr,
241 sizeof(dns6_addr),
242 scope_id, &dns6_addr_time);
245 #endif
247 static void slirp_init_once(void)
249 static int initialized;
250 const char *debug;
251 #ifdef _WIN32
252 WSADATA Data;
253 #endif
255 if (initialized) {
256 return;
258 initialized = 1;
260 #ifdef _WIN32
261 WSAStartup(MAKEWORD(2,0), &Data);
262 atexit(winsock_cleanup);
263 #endif
265 loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
266 loopback_mask = htonl(IN_CLASSA_NET);
268 debug = g_getenv("SLIRP_DEBUG");
269 if (debug) {
270 const GDebugKey keys[] = {
271 { "call", DBG_CALL },
272 { "misc", DBG_MISC },
273 { "error", DBG_ERROR },
275 slirp_debug = g_parse_debug_string(debug, keys, G_N_ELEMENTS(keys));
281 static void slirp_state_save(QEMUFile *f, void *opaque);
282 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
284 static SaveVMHandlers savevm_slirp_state = {
285 .save_state = slirp_state_save,
286 .load_state = slirp_state_load,
289 Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
290 struct in_addr vnetmask, struct in_addr vhost,
291 bool in6_enabled,
292 struct in6_addr vprefix_addr6, uint8_t vprefix_len,
293 struct in6_addr vhost6, const char *vhostname,
294 const char *tftp_server_name,
295 const char *tftp_path, const char *bootfile,
296 struct in_addr vdhcp_start, struct in_addr vnameserver,
297 struct in6_addr vnameserver6, const char **vdnssearch,
298 const char *vdomainname,
299 const SlirpCb *callbacks,
300 void *opaque)
302 Slirp *slirp = g_malloc0(sizeof(Slirp));
304 slirp_init_once();
306 slirp->cb = callbacks;
307 slirp->grand = g_rand_new();
308 slirp->restricted = restricted;
310 slirp->in_enabled = in_enabled;
311 slirp->in6_enabled = in6_enabled;
313 if_init(slirp);
314 ip_init(slirp);
315 ip6_init(slirp);
317 /* Initialise mbufs *after* setting the MTU */
318 m_init(slirp);
320 slirp->vnetwork_addr = vnetwork;
321 slirp->vnetwork_mask = vnetmask;
322 slirp->vhost_addr = vhost;
323 slirp->vprefix_addr6 = vprefix_addr6;
324 slirp->vprefix_len = vprefix_len;
325 slirp->vhost_addr6 = vhost6;
326 if (vhostname) {
327 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
328 vhostname);
330 slirp->tftp_prefix = g_strdup(tftp_path);
331 slirp->bootp_filename = g_strdup(bootfile);
332 slirp->vdomainname = g_strdup(vdomainname);
333 slirp->vdhcp_startaddr = vdhcp_start;
334 slirp->vnameserver_addr = vnameserver;
335 slirp->vnameserver_addr6 = vnameserver6;
336 slirp->tftp_server_name = g_strdup(tftp_server_name);
338 if (vdnssearch) {
339 translate_dnssearch(slirp, vdnssearch);
342 slirp->opaque = opaque;
344 register_savevm_live(NULL, "slirp", 0, 4, &savevm_slirp_state, slirp);
346 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
348 return slirp;
351 void slirp_cleanup(Slirp *slirp)
353 struct gfwd_list *e, *next;
355 for (e = slirp->guestfwd_list; e; e = next) {
356 next = e->ex_next;
357 g_free(e->ex_exec);
358 g_free(e);
361 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
363 unregister_savevm(NULL, "slirp", slirp);
365 ip_cleanup(slirp);
366 ip6_cleanup(slirp);
367 m_cleanup(slirp);
369 g_rand_free(slirp->grand);
371 g_free(slirp->vdnssearch);
372 g_free(slirp->tftp_prefix);
373 g_free(slirp->bootp_filename);
374 g_free(slirp->vdomainname);
375 g_free(slirp);
378 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
379 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
381 static void slirp_update_timeout(uint32_t *timeout)
383 Slirp *slirp;
384 uint32_t t;
386 if (*timeout <= TIMEOUT_FAST) {
387 return;
390 t = MIN(1000, *timeout);
392 /* If we have tcp timeout with slirp, then we will fill @timeout with
393 * more precise value.
395 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
396 if (slirp->time_fasttimo) {
397 *timeout = TIMEOUT_FAST;
398 return;
400 if (slirp->do_slowtimo) {
401 t = MIN(TIMEOUT_SLOW, t);
404 *timeout = t;
407 void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
409 Slirp *slirp;
410 struct socket *so, *so_next;
412 if (QTAILQ_EMPTY(&slirp_instances)) {
413 return;
417 * First, TCP sockets
420 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
422 * *_slowtimo needs calling if there are IP fragments
423 * in the fragment queue, or there are TCP connections active
425 slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
426 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
428 for (so = slirp->tcb.so_next; so != &slirp->tcb;
429 so = so_next) {
430 int events = 0;
432 so_next = so->so_next;
434 so->pollfds_idx = -1;
437 * See if we need a tcp_fasttimo
439 if (slirp->time_fasttimo == 0 &&
440 so->so_tcpcb->t_flags & TF_DELACK) {
441 slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
445 * NOFDREF can include still connecting to local-host,
446 * newly socreated() sockets etc. Don't want to select these.
448 if (so->so_state & SS_NOFDREF || so->s == -1) {
449 continue;
453 * Set for reading sockets which are accepting
455 if (so->so_state & SS_FACCEPTCONN) {
456 GPollFD pfd = {
457 .fd = so->s,
458 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
460 so->pollfds_idx = pollfds->len;
461 g_array_append_val(pollfds, pfd);
462 continue;
466 * Set for writing sockets which are connecting
468 if (so->so_state & SS_ISFCONNECTING) {
469 GPollFD pfd = {
470 .fd = so->s,
471 .events = G_IO_OUT | G_IO_ERR,
473 so->pollfds_idx = pollfds->len;
474 g_array_append_val(pollfds, pfd);
475 continue;
479 * Set for writing if we are connected, can send more, and
480 * we have something to send
482 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
483 events |= G_IO_OUT | G_IO_ERR;
487 * Set for reading (and urgent data) if we are connected, can
488 * receive more, and we have room for it XXX /2 ?
490 if (CONN_CANFRCV(so) &&
491 (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
492 events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
495 if (events) {
496 GPollFD pfd = {
497 .fd = so->s,
498 .events = events,
500 so->pollfds_idx = pollfds->len;
501 g_array_append_val(pollfds, pfd);
506 * UDP sockets
508 for (so = slirp->udb.so_next; so != &slirp->udb;
509 so = so_next) {
510 so_next = so->so_next;
512 so->pollfds_idx = -1;
515 * See if it's timed out
517 if (so->so_expire) {
518 if (so->so_expire <= curtime) {
519 udp_detach(so);
520 continue;
521 } else {
522 slirp->do_slowtimo = true; /* Let socket expire */
527 * When UDP packets are received from over the
528 * link, they're sendto()'d straight away, so
529 * no need for setting for writing
530 * Limit the number of packets queued by this session
531 * to 4. Note that even though we try and limit this
532 * to 4 packets, the session could have more queued
533 * if the packets needed to be fragmented
534 * (XXX <= 4 ?)
536 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
537 GPollFD pfd = {
538 .fd = so->s,
539 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
541 so->pollfds_idx = pollfds->len;
542 g_array_append_val(pollfds, pfd);
547 * ICMP sockets
549 for (so = slirp->icmp.so_next; so != &slirp->icmp;
550 so = so_next) {
551 so_next = so->so_next;
553 so->pollfds_idx = -1;
556 * See if it's timed out
558 if (so->so_expire) {
559 if (so->so_expire <= curtime) {
560 icmp_detach(so);
561 continue;
562 } else {
563 slirp->do_slowtimo = true; /* Let socket expire */
567 if (so->so_state & SS_ISFCONNECTED) {
568 GPollFD pfd = {
569 .fd = so->s,
570 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
572 so->pollfds_idx = pollfds->len;
573 g_array_append_val(pollfds, pfd);
577 slirp_update_timeout(timeout);
580 void slirp_pollfds_poll(GArray *pollfds, int select_error)
582 Slirp *slirp = QTAILQ_FIRST(&slirp_instances);
583 struct socket *so, *so_next;
584 int ret;
586 if (!slirp) {
587 return;
590 curtime = slirp->cb->clock_get_ns() / SCALE_MS;
592 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
594 * See if anything has timed out
596 if (slirp->time_fasttimo &&
597 ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) {
598 tcp_fasttimo(slirp);
599 slirp->time_fasttimo = 0;
601 if (slirp->do_slowtimo &&
602 ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
603 ip_slowtimo(slirp);
604 tcp_slowtimo(slirp);
605 slirp->last_slowtimo = curtime;
609 * Check sockets
611 if (!select_error) {
613 * Check TCP sockets
615 for (so = slirp->tcb.so_next; so != &slirp->tcb;
616 so = so_next) {
617 int revents;
619 so_next = so->so_next;
621 revents = 0;
622 if (so->pollfds_idx != -1) {
623 revents = g_array_index(pollfds, GPollFD,
624 so->pollfds_idx).revents;
627 if (so->so_state & SS_NOFDREF || so->s == -1) {
628 continue;
632 * Check for URG data
633 * This will soread as well, so no need to
634 * test for G_IO_IN below if this succeeds
636 if (revents & G_IO_PRI) {
637 ret = sorecvoob(so);
638 if (ret < 0) {
639 /* Socket error might have resulted in the socket being
640 * removed, do not try to do anything more with it. */
641 continue;
645 * Check sockets for reading
647 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
649 * Check for incoming connections
651 if (so->so_state & SS_FACCEPTCONN) {
652 tcp_connect(so);
653 continue;
654 } /* else */
655 ret = soread(so);
657 /* Output it if we read something */
658 if (ret > 0) {
659 tcp_output(sototcpcb(so));
661 if (ret < 0) {
662 /* Socket error might have resulted in the socket being
663 * removed, do not try to do anything more with it. */
664 continue;
669 * Check sockets for writing
671 if (!(so->so_state & SS_NOFDREF) &&
672 (revents & (G_IO_OUT | G_IO_ERR))) {
674 * Check for non-blocking, still-connecting sockets
676 if (so->so_state & SS_ISFCONNECTING) {
677 /* Connected */
678 so->so_state &= ~SS_ISFCONNECTING;
680 ret = send(so->s, (const void *) &ret, 0, 0);
681 if (ret < 0) {
682 /* XXXXX Must fix, zero bytes is a NOP */
683 if (errno == EAGAIN || errno == EWOULDBLOCK ||
684 errno == EINPROGRESS || errno == ENOTCONN) {
685 continue;
688 /* else failed */
689 so->so_state &= SS_PERSISTENT_MASK;
690 so->so_state |= SS_NOFDREF;
692 /* else so->so_state &= ~SS_ISFCONNECTING; */
695 * Continue tcp_input
697 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
698 so->so_ffamily);
699 /* continue; */
700 } else {
701 ret = sowrite(so);
702 if (ret > 0) {
703 /* Call tcp_output in case we need to send a window
704 * update to the guest, otherwise it will be stuck
705 * until it sends a window probe. */
706 tcp_output(sototcpcb(so));
713 * Now UDP sockets.
714 * Incoming packets are sent straight away, they're not buffered.
715 * Incoming UDP data isn't buffered either.
717 for (so = slirp->udb.so_next; so != &slirp->udb;
718 so = so_next) {
719 int revents;
721 so_next = so->so_next;
723 revents = 0;
724 if (so->pollfds_idx != -1) {
725 revents = g_array_index(pollfds, GPollFD,
726 so->pollfds_idx).revents;
729 if (so->s != -1 &&
730 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
731 sorecvfrom(so);
736 * Check incoming ICMP relies.
738 for (so = slirp->icmp.so_next; so != &slirp->icmp;
739 so = so_next) {
740 int revents;
742 so_next = so->so_next;
744 revents = 0;
745 if (so->pollfds_idx != -1) {
746 revents = g_array_index(pollfds, GPollFD,
747 so->pollfds_idx).revents;
750 if (so->s != -1 &&
751 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
752 icmp_receive(so);
757 if_start(slirp);
761 static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
763 struct slirp_arphdr *ah = (struct slirp_arphdr *)(pkt + ETH_HLEN);
764 uint8_t arp_reply[MAX(ETH_HLEN + sizeof(struct slirp_arphdr), 64)];
765 struct ethhdr *reh = (struct ethhdr *)arp_reply;
766 struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_reply + ETH_HLEN);
767 int ar_op;
768 struct gfwd_list *ex_ptr;
770 if (!slirp->in_enabled) {
771 return;
774 ar_op = ntohs(ah->ar_op);
775 switch(ar_op) {
776 case ARPOP_REQUEST:
777 if (ah->ar_tip == ah->ar_sip) {
778 /* Gratuitous ARP */
779 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
780 return;
783 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
784 slirp->vnetwork_addr.s_addr) {
785 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
786 ah->ar_tip == slirp->vhost_addr.s_addr)
787 goto arp_ok;
788 for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
789 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
790 goto arp_ok;
792 return;
793 arp_ok:
794 memset(arp_reply, 0, sizeof(arp_reply));
796 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
798 /* ARP request for alias/dns mac address */
799 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
800 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
801 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
802 reh->h_proto = htons(ETH_P_ARP);
804 rah->ar_hrd = htons(1);
805 rah->ar_pro = htons(ETH_P_IP);
806 rah->ar_hln = ETH_ALEN;
807 rah->ar_pln = 4;
808 rah->ar_op = htons(ARPOP_REPLY);
809 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
810 rah->ar_sip = ah->ar_tip;
811 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
812 rah->ar_tip = ah->ar_sip;
813 slirp->cb->output(slirp->opaque, arp_reply, sizeof(arp_reply));
815 break;
816 case ARPOP_REPLY:
817 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
818 break;
819 default:
820 break;
824 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
826 struct mbuf *m;
827 int proto;
829 if (pkt_len < ETH_HLEN)
830 return;
832 proto = ntohs(*(uint16_t *)(pkt + 12));
833 switch(proto) {
834 case ETH_P_ARP:
835 arp_input(slirp, pkt, pkt_len);
836 break;
837 case ETH_P_IP:
838 case ETH_P_IPV6:
839 m = m_get(slirp);
840 if (!m)
841 return;
842 /* Note: we add 2 to align the IP header on 4 bytes,
843 * and add the margin for the tcpiphdr overhead */
844 if (M_FREEROOM(m) < pkt_len + TCPIPHDR_DELTA + 2) {
845 m_inc(m, pkt_len + TCPIPHDR_DELTA + 2);
847 m->m_len = pkt_len + TCPIPHDR_DELTA + 2;
848 memcpy(m->m_data + TCPIPHDR_DELTA + 2, pkt, pkt_len);
850 m->m_data += TCPIPHDR_DELTA + 2 + ETH_HLEN;
851 m->m_len -= TCPIPHDR_DELTA + 2 + ETH_HLEN;
853 if (proto == ETH_P_IP) {
854 ip_input(m);
855 } else if (proto == ETH_P_IPV6) {
856 ip6_input(m);
858 break;
860 case ETH_P_NCSI:
861 ncsi_input(slirp, pkt, pkt_len);
862 break;
864 default:
865 break;
869 /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no
870 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
871 * is ready to go.
873 static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
874 uint8_t ethaddr[ETH_ALEN])
876 const struct ip *iph = (const struct ip *)ifm->m_data;
878 if (iph->ip_dst.s_addr == 0) {
879 /* 0.0.0.0 can not be a destination address, something went wrong,
880 * avoid making it worse */
881 return 1;
883 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
884 uint8_t arp_req[ETH_HLEN + sizeof(struct slirp_arphdr)];
885 struct ethhdr *reh = (struct ethhdr *)arp_req;
886 struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_req + ETH_HLEN);
888 if (!ifm->resolution_requested) {
889 /* If the client addr is not known, send an ARP request */
890 memset(reh->h_dest, 0xff, ETH_ALEN);
891 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
892 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
893 reh->h_proto = htons(ETH_P_ARP);
894 rah->ar_hrd = htons(1);
895 rah->ar_pro = htons(ETH_P_IP);
896 rah->ar_hln = ETH_ALEN;
897 rah->ar_pln = 4;
898 rah->ar_op = htons(ARPOP_REQUEST);
900 /* source hw addr */
901 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
902 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
904 /* source IP */
905 rah->ar_sip = slirp->vhost_addr.s_addr;
907 /* target hw addr (none) */
908 memset(rah->ar_tha, 0, ETH_ALEN);
910 /* target IP */
911 rah->ar_tip = iph->ip_dst.s_addr;
912 slirp->client_ipaddr = iph->ip_dst;
913 slirp->cb->output(slirp->opaque, arp_req, sizeof(arp_req));
914 ifm->resolution_requested = true;
916 /* Expire request and drop outgoing packet after 1 second */
917 ifm->expiration_date = slirp->cb->clock_get_ns() + 1000000000ULL;
919 return 0;
920 } else {
921 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
922 /* XXX: not correct */
923 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
924 eh->h_proto = htons(ETH_P_IP);
926 /* Send this */
927 return 2;
931 /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no
932 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
933 * is ready to go.
935 static int if_encap6(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
936 uint8_t ethaddr[ETH_ALEN])
938 const struct ip6 *ip6h = mtod(ifm, const struct ip6 *);
939 if (!ndp_table_search(slirp, ip6h->ip_dst, ethaddr)) {
940 if (!ifm->resolution_requested) {
941 ndp_send_ns(slirp, ip6h->ip_dst);
942 ifm->resolution_requested = true;
943 ifm->expiration_date = slirp->cb->clock_get_ns() + 1000000000ULL;
945 return 0;
946 } else {
947 eh->h_proto = htons(ETH_P_IPV6);
948 in6_compute_ethaddr(ip6h->ip_src, eh->h_source);
950 /* Send this */
951 return 2;
955 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
956 * re-queued.
958 int if_encap(Slirp *slirp, struct mbuf *ifm)
960 uint8_t buf[1600];
961 struct ethhdr *eh = (struct ethhdr *)buf;
962 uint8_t ethaddr[ETH_ALEN];
963 const struct ip *iph = (const struct ip *)ifm->m_data;
964 int ret;
966 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
967 return 1;
970 switch (iph->ip_v) {
971 case IPVERSION:
972 ret = if_encap4(slirp, ifm, eh, ethaddr);
973 if (ret < 2) {
974 return ret;
976 break;
978 case IP6VERSION:
979 ret = if_encap6(slirp, ifm, eh, ethaddr);
980 if (ret < 2) {
981 return ret;
983 break;
985 default:
986 g_assert_not_reached();
987 break;
990 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
991 DEBUG_ARG("src = %02x:%02x:%02x:%02x:%02x:%02x",
992 eh->h_source[0], eh->h_source[1], eh->h_source[2],
993 eh->h_source[3], eh->h_source[4], eh->h_source[5]);
994 DEBUG_ARG("dst = %02x:%02x:%02x:%02x:%02x:%02x",
995 eh->h_dest[0], eh->h_dest[1], eh->h_dest[2],
996 eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]);
997 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
998 slirp->cb->output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
999 return 1;
1002 /* Drop host forwarding rule, return 0 if found. */
1003 int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
1004 int host_port)
1006 struct socket *so;
1007 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
1008 struct sockaddr_in addr;
1009 int port = htons(host_port);
1010 socklen_t addr_len;
1012 for (so = head->so_next; so != head; so = so->so_next) {
1013 addr_len = sizeof(addr);
1014 if ((so->so_state & SS_HOSTFWD) &&
1015 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
1016 addr.sin_addr.s_addr == host_addr.s_addr &&
1017 addr.sin_port == port) {
1018 close(so->s);
1019 sofree(so);
1020 return 0;
1024 return -1;
1027 int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
1028 int host_port, struct in_addr guest_addr, int guest_port)
1030 if (!guest_addr.s_addr) {
1031 guest_addr = slirp->vdhcp_startaddr;
1033 if (is_udp) {
1034 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
1035 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
1036 return -1;
1037 } else {
1038 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
1039 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
1040 return -1;
1042 return 0;
1045 static bool
1046 check_guestfwd(Slirp *slirp, struct in_addr *guest_addr, int guest_port)
1048 struct gfwd_list *tmp_ptr;
1050 if (!guest_addr->s_addr) {
1051 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
1052 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
1054 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
1055 slirp->vnetwork_addr.s_addr ||
1056 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
1057 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
1058 return false;
1061 /* check if the port is "bound" */
1062 for (tmp_ptr = slirp->guestfwd_list; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
1063 if (guest_port == tmp_ptr->ex_fport &&
1064 guest_addr->s_addr == tmp_ptr->ex_addr.s_addr)
1065 return false;
1068 return true;
1071 int slirp_add_exec(Slirp *slirp, void *chardev, const char *cmdline,
1072 struct in_addr *guest_addr, int guest_port)
1074 if (!check_guestfwd(slirp, guest_addr, guest_port)) {
1075 return -1;
1078 return add_exec(&slirp->guestfwd_list, chardev, cmdline, *guest_addr,
1079 htons(guest_port));
1082 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
1084 if (so->s == -1 && so->chardev) {
1085 /* XXX this blocks entire thread. Rewrite to use
1086 * qemu_chr_fe_write and background I/O callbacks */
1087 qemu_chr_fe_write_all(so->chardev, buf, len);
1088 return len;
1091 if (so->s == -1) {
1093 * This should in theory not happen but it is hard to be
1094 * sure because some code paths will end up with so->s == -1
1095 * on a failure but don't dispose of the struct socket.
1096 * Check specifically, so we don't pass -1 to send().
1098 errno = EBADF;
1099 return -1;
1102 return send(so->s, buf, len, flags);
1105 static struct socket *
1106 slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
1108 struct socket *so;
1110 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
1111 if (so->so_faddr.s_addr == guest_addr.s_addr &&
1112 htons(so->so_fport) == guest_port) {
1113 return so;
1116 return NULL;
1119 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
1120 int guest_port)
1122 struct iovec iov[2];
1123 struct socket *so;
1125 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1127 if (!so || so->so_state & SS_NOFDREF) {
1128 return 0;
1131 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) {
1132 return 0;
1135 return sopreprbuf(so, iov, NULL);
1138 void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
1139 const uint8_t *buf, int size)
1141 int ret;
1142 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1144 if (!so)
1145 return;
1147 ret = soreadbuf(so, (const char *)buf, size);
1149 if (ret > 0)
1150 tcp_output(sototcpcb(so));
1153 static int slirp_tcp_post_load(void *opaque, int version)
1155 tcp_template((struct tcpcb *)opaque);
1157 return 0;
1160 static const VMStateDescription vmstate_slirp_tcp = {
1161 .name = "slirp-tcp",
1162 .version_id = 0,
1163 .post_load = slirp_tcp_post_load,
1164 .fields = (VMStateField[]) {
1165 VMSTATE_INT16(t_state, struct tcpcb),
1166 VMSTATE_INT16_ARRAY(t_timer, struct tcpcb, TCPT_NTIMERS),
1167 VMSTATE_INT16(t_rxtshift, struct tcpcb),
1168 VMSTATE_INT16(t_rxtcur, struct tcpcb),
1169 VMSTATE_INT16(t_dupacks, struct tcpcb),
1170 VMSTATE_UINT16(t_maxseg, struct tcpcb),
1171 VMSTATE_UINT8(t_force, struct tcpcb),
1172 VMSTATE_UINT16(t_flags, struct tcpcb),
1173 VMSTATE_UINT32(snd_una, struct tcpcb),
1174 VMSTATE_UINT32(snd_nxt, struct tcpcb),
1175 VMSTATE_UINT32(snd_up, struct tcpcb),
1176 VMSTATE_UINT32(snd_wl1, struct tcpcb),
1177 VMSTATE_UINT32(snd_wl2, struct tcpcb),
1178 VMSTATE_UINT32(iss, struct tcpcb),
1179 VMSTATE_UINT32(snd_wnd, struct tcpcb),
1180 VMSTATE_UINT32(rcv_wnd, struct tcpcb),
1181 VMSTATE_UINT32(rcv_nxt, struct tcpcb),
1182 VMSTATE_UINT32(rcv_up, struct tcpcb),
1183 VMSTATE_UINT32(irs, struct tcpcb),
1184 VMSTATE_UINT32(rcv_adv, struct tcpcb),
1185 VMSTATE_UINT32(snd_max, struct tcpcb),
1186 VMSTATE_UINT32(snd_cwnd, struct tcpcb),
1187 VMSTATE_UINT32(snd_ssthresh, struct tcpcb),
1188 VMSTATE_INT16(t_idle, struct tcpcb),
1189 VMSTATE_INT16(t_rtt, struct tcpcb),
1190 VMSTATE_UINT32(t_rtseq, struct tcpcb),
1191 VMSTATE_INT16(t_srtt, struct tcpcb),
1192 VMSTATE_INT16(t_rttvar, struct tcpcb),
1193 VMSTATE_UINT16(t_rttmin, struct tcpcb),
1194 VMSTATE_UINT32(max_sndwnd, struct tcpcb),
1195 VMSTATE_UINT8(t_oobflags, struct tcpcb),
1196 VMSTATE_UINT8(t_iobc, struct tcpcb),
1197 VMSTATE_INT16(t_softerror, struct tcpcb),
1198 VMSTATE_UINT8(snd_scale, struct tcpcb),
1199 VMSTATE_UINT8(rcv_scale, struct tcpcb),
1200 VMSTATE_UINT8(request_r_scale, struct tcpcb),
1201 VMSTATE_UINT8(requested_s_scale, struct tcpcb),
1202 VMSTATE_UINT32(ts_recent, struct tcpcb),
1203 VMSTATE_UINT32(ts_recent_age, struct tcpcb),
1204 VMSTATE_UINT32(last_ack_sent, struct tcpcb),
1205 VMSTATE_END_OF_LIST()
1209 /* The sbuf has a pair of pointers that are migrated as offsets;
1210 * we calculate the offsets and restore the pointers using
1211 * pre_save/post_load on a tmp structure.
1213 struct sbuf_tmp {
1214 struct sbuf *parent;
1215 uint32_t roff, woff;
1218 static int sbuf_tmp_pre_save(void *opaque)
1220 struct sbuf_tmp *tmp = opaque;
1221 tmp->woff = tmp->parent->sb_wptr - tmp->parent->sb_data;
1222 tmp->roff = tmp->parent->sb_rptr - tmp->parent->sb_data;
1224 return 0;
1227 static int sbuf_tmp_post_load(void *opaque, int version)
1229 struct sbuf_tmp *tmp = opaque;
1230 uint32_t requested_len = tmp->parent->sb_datalen;
1232 /* Allocate the buffer space used by the field after the tmp */
1233 sbreserve(tmp->parent, tmp->parent->sb_datalen);
1235 if (tmp->parent->sb_datalen != requested_len) {
1236 return -ENOMEM;
1238 if (tmp->woff >= requested_len ||
1239 tmp->roff >= requested_len) {
1240 g_critical("invalid sbuf offsets r/w=%u/%u len=%u",
1241 tmp->roff, tmp->woff, requested_len);
1242 return -EINVAL;
1245 tmp->parent->sb_wptr = tmp->parent->sb_data + tmp->woff;
1246 tmp->parent->sb_rptr = tmp->parent->sb_data + tmp->roff;
1248 return 0;
1252 static const VMStateDescription vmstate_slirp_sbuf_tmp = {
1253 .name = "slirp-sbuf-tmp",
1254 .post_load = sbuf_tmp_post_load,
1255 .pre_save = sbuf_tmp_pre_save,
1256 .version_id = 0,
1257 .fields = (VMStateField[]) {
1258 VMSTATE_UINT32(woff, struct sbuf_tmp),
1259 VMSTATE_UINT32(roff, struct sbuf_tmp),
1260 VMSTATE_END_OF_LIST()
1264 static const VMStateDescription vmstate_slirp_sbuf = {
1265 .name = "slirp-sbuf",
1266 .version_id = 0,
1267 .fields = (VMStateField[]) {
1268 VMSTATE_UINT32(sb_cc, struct sbuf),
1269 VMSTATE_UINT32(sb_datalen, struct sbuf),
1270 VMSTATE_WITH_TMP(struct sbuf, struct sbuf_tmp, vmstate_slirp_sbuf_tmp),
1271 VMSTATE_VBUFFER_UINT32(sb_data, struct sbuf, 0, NULL, sb_datalen),
1272 VMSTATE_END_OF_LIST()
1276 static bool slirp_older_than_v4(void *opaque, int version_id)
1278 return version_id < 4;
1281 static bool slirp_family_inet(void *opaque, int version_id)
1283 union slirp_sockaddr *ssa = (union slirp_sockaddr *)opaque;
1284 return ssa->ss.ss_family == AF_INET;
1287 static int slirp_socket_pre_load(void *opaque)
1289 struct socket *so = opaque;
1290 if (tcp_attach(so) < 0) {
1291 return -ENOMEM;
1293 /* Older versions don't load these fields */
1294 so->so_ffamily = AF_INET;
1295 so->so_lfamily = AF_INET;
1296 return 0;
1299 #ifndef _WIN32
1300 #define VMSTATE_SIN4_ADDR(f, s, t) VMSTATE_UINT32_TEST(f, s, t)
1301 #else
1302 /* Win uses u_long rather than uint32_t - but it's still 32bits long */
1303 #define VMSTATE_SIN4_ADDR(f, s, t) VMSTATE_SINGLE_TEST(f, s, t, 0, \
1304 vmstate_info_uint32, u_long)
1305 #endif
1307 /* The OS provided ss_family field isn't that portable; it's size
1308 * and type varies (16/8 bit, signed, unsigned)
1309 * and the values it contains aren't fully portable.
1311 typedef struct SS_FamilyTmpStruct {
1312 union slirp_sockaddr *parent;
1313 uint16_t portable_family;
1314 } SS_FamilyTmpStruct;
1316 #define SS_FAMILY_MIG_IPV4 2 /* Linux, BSD, Win... */
1317 #define SS_FAMILY_MIG_IPV6 10 /* Linux */
1318 #define SS_FAMILY_MIG_OTHER 0xffff
1320 static int ss_family_pre_save(void *opaque)
1322 SS_FamilyTmpStruct *tss = opaque;
1324 tss->portable_family = SS_FAMILY_MIG_OTHER;
1326 if (tss->parent->ss.ss_family == AF_INET) {
1327 tss->portable_family = SS_FAMILY_MIG_IPV4;
1328 } else if (tss->parent->ss.ss_family == AF_INET6) {
1329 tss->portable_family = SS_FAMILY_MIG_IPV6;
1332 return 0;
1335 static int ss_family_post_load(void *opaque, int version_id)
1337 SS_FamilyTmpStruct *tss = opaque;
1339 switch (tss->portable_family) {
1340 case SS_FAMILY_MIG_IPV4:
1341 tss->parent->ss.ss_family = AF_INET;
1342 break;
1343 case SS_FAMILY_MIG_IPV6:
1344 case 23: /* compatibility: AF_INET6 from mingw */
1345 case 28: /* compatibility: AF_INET6 from FreeBSD sys/socket.h */
1346 tss->parent->ss.ss_family = AF_INET6;
1347 break;
1348 default:
1349 g_critical("invalid ss_family type %x", tss->portable_family);
1350 return -EINVAL;
1353 return 0;
1356 static const VMStateDescription vmstate_slirp_ss_family = {
1357 .name = "slirp-socket-addr/ss_family",
1358 .pre_save = ss_family_pre_save,
1359 .post_load = ss_family_post_load,
1360 .fields = (VMStateField[]) {
1361 VMSTATE_UINT16(portable_family, SS_FamilyTmpStruct),
1362 VMSTATE_END_OF_LIST()
1366 static const VMStateDescription vmstate_slirp_socket_addr = {
1367 .name = "slirp-socket-addr",
1368 .version_id = 4,
1369 .fields = (VMStateField[]) {
1370 VMSTATE_WITH_TMP(union slirp_sockaddr, SS_FamilyTmpStruct,
1371 vmstate_slirp_ss_family),
1372 VMSTATE_SIN4_ADDR(sin.sin_addr.s_addr, union slirp_sockaddr,
1373 slirp_family_inet),
1374 VMSTATE_UINT16_TEST(sin.sin_port, union slirp_sockaddr,
1375 slirp_family_inet),
1377 #if 0
1378 /* Untested: Needs checking by someone with IPv6 test */
1379 VMSTATE_BUFFER_TEST(sin6.sin6_addr, union slirp_sockaddr,
1380 slirp_family_inet6),
1381 VMSTATE_UINT16_TEST(sin6.sin6_port, union slirp_sockaddr,
1382 slirp_family_inet6),
1383 VMSTATE_UINT32_TEST(sin6.sin6_flowinfo, union slirp_sockaddr,
1384 slirp_family_inet6),
1385 VMSTATE_UINT32_TEST(sin6.sin6_scope_id, union slirp_sockaddr,
1386 slirp_family_inet6),
1387 #endif
1389 VMSTATE_END_OF_LIST()
1393 static const VMStateDescription vmstate_slirp_socket = {
1394 .name = "slirp-socket",
1395 .version_id = 4,
1396 .pre_load = slirp_socket_pre_load,
1397 .fields = (VMStateField[]) {
1398 VMSTATE_UINT32(so_urgc, struct socket),
1399 /* Pre-v4 versions */
1400 VMSTATE_SIN4_ADDR(so_faddr.s_addr, struct socket,
1401 slirp_older_than_v4),
1402 VMSTATE_SIN4_ADDR(so_laddr.s_addr, struct socket,
1403 slirp_older_than_v4),
1404 VMSTATE_UINT16_TEST(so_fport, struct socket, slirp_older_than_v4),
1405 VMSTATE_UINT16_TEST(so_lport, struct socket, slirp_older_than_v4),
1406 /* v4 and newer */
1407 VMSTATE_STRUCT(fhost, struct socket, 4, vmstate_slirp_socket_addr,
1408 union slirp_sockaddr),
1409 VMSTATE_STRUCT(lhost, struct socket, 4, vmstate_slirp_socket_addr,
1410 union slirp_sockaddr),
1412 VMSTATE_UINT8(so_iptos, struct socket),
1413 VMSTATE_UINT8(so_emu, struct socket),
1414 VMSTATE_UINT8(so_type, struct socket),
1415 VMSTATE_INT32(so_state, struct socket),
1416 VMSTATE_STRUCT(so_rcv, struct socket, 0, vmstate_slirp_sbuf,
1417 struct sbuf),
1418 VMSTATE_STRUCT(so_snd, struct socket, 0, vmstate_slirp_sbuf,
1419 struct sbuf),
1420 VMSTATE_STRUCT_POINTER(so_tcpcb, struct socket, vmstate_slirp_tcp,
1421 struct tcpcb),
1422 VMSTATE_END_OF_LIST()
1426 static const VMStateDescription vmstate_slirp_bootp_client = {
1427 .name = "slirp_bootpclient",
1428 .fields = (VMStateField[]) {
1429 VMSTATE_UINT16(allocated, BOOTPClient),
1430 VMSTATE_BUFFER(macaddr, BOOTPClient),
1431 VMSTATE_END_OF_LIST()
1435 static const VMStateDescription vmstate_slirp = {
1436 .name = "slirp",
1437 .version_id = 4,
1438 .fields = (VMStateField[]) {
1439 VMSTATE_UINT16_V(ip_id, Slirp, 2),
1440 VMSTATE_STRUCT_ARRAY(bootp_clients, Slirp, NB_BOOTP_CLIENTS, 3,
1441 vmstate_slirp_bootp_client, BOOTPClient),
1442 VMSTATE_END_OF_LIST()
1446 static void slirp_state_save(QEMUFile *f, void *opaque)
1448 Slirp *slirp = opaque;
1449 struct gfwd_list *ex_ptr;
1451 for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1452 if (ex_ptr->ex_chardev) {
1453 struct socket *so;
1454 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
1455 ntohs(ex_ptr->ex_fport));
1456 if (!so)
1457 continue;
1459 qemu_put_byte(f, 42);
1460 vmstate_save_state(f, &vmstate_slirp_socket, so, NULL);
1462 qemu_put_byte(f, 0);
1464 vmstate_save_state(f, &vmstate_slirp, slirp, NULL);
1468 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1470 Slirp *slirp = opaque;
1471 struct gfwd_list *ex_ptr;
1473 while (qemu_get_byte(f)) {
1474 int ret;
1475 struct socket *so = socreate(slirp);
1477 ret = vmstate_load_state(f, &vmstate_slirp_socket, so, version_id);
1479 if (ret < 0)
1480 return ret;
1482 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1483 slirp->vnetwork_addr.s_addr) {
1484 return -EINVAL;
1486 for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1487 if (ex_ptr->ex_chardev &&
1488 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1489 so->so_fport == ex_ptr->ex_fport) {
1490 break;
1493 if (!ex_ptr)
1494 return -EINVAL;
1497 return vmstate_load_state(f, &vmstate_slirp, slirp, version_id);