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