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