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