Statistics
| Branch: | Revision:

root / slirp / slirp.c @ 1de7afc9

History | View | Annotate | Download (31.1 kB)

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-common.h"
25
#include "qemu/timer.h"
26
#include "qemu-char.h"
27
#include "slirp.h"
28
#include "hw/hw.h"
29

    
30
/* host loopback address */
31
struct in_addr loopback_addr;
32
/* host loopback network mask */
33
unsigned long loopback_mask;
34

    
35
/* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
36
static const uint8_t special_ethaddr[ETH_ALEN] = {
37
    0x52, 0x55, 0x00, 0x00, 0x00, 0x00
38
};
39

    
40
static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
41

    
42
/* XXX: suppress those select globals */
43
fd_set *global_readfds, *global_writefds, *global_xfds;
44

    
45
u_int curtime;
46
static u_int time_fasttimo, last_slowtimo;
47
static int do_slowtimo;
48

    
49
static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
50
    QTAILQ_HEAD_INITIALIZER(slirp_instances);
51

    
52
static struct in_addr dns_addr;
53
static u_int dns_addr_time;
54

    
55
#ifdef _WIN32
56

    
57
int get_dns_addr(struct in_addr *pdns_addr)
58
{
59
    FIXED_INFO *FixedInfo=NULL;
60
    ULONG    BufLen;
61
    DWORD    ret;
62
    IP_ADDR_STRING *pIPAddr;
63
    struct in_addr tmp_addr;
64

    
65
    if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < 1000) {
66
        *pdns_addr = dns_addr;
67
        return 0;
68
    }
69

    
70
    FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
71
    BufLen = sizeof(FIXED_INFO);
72

    
73
    if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
74
        if (FixedInfo) {
75
            GlobalFree(FixedInfo);
76
            FixedInfo = NULL;
77
        }
78
        FixedInfo = GlobalAlloc(GPTR, BufLen);
79
    }
80

    
81
    if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
82
        printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
83
        if (FixedInfo) {
84
            GlobalFree(FixedInfo);
85
            FixedInfo = NULL;
86
        }
87
        return -1;
88
    }
89

    
90
    pIPAddr = &(FixedInfo->DnsServerList);
91
    inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
92
    *pdns_addr = tmp_addr;
93
    dns_addr = tmp_addr;
94
    dns_addr_time = curtime;
95
    if (FixedInfo) {
96
        GlobalFree(FixedInfo);
97
        FixedInfo = NULL;
98
    }
99
    return 0;
100
}
101

    
102
static void winsock_cleanup(void)
103
{
104
    WSACleanup();
105
}
106

    
107
#else
108

    
109
static struct stat dns_addr_stat;
110

    
111
int get_dns_addr(struct in_addr *pdns_addr)
112
{
113
    char buff[512];
114
    char buff2[257];
115
    FILE *f;
116
    int found = 0;
117
    struct in_addr tmp_addr;
118

    
119
    if (dns_addr.s_addr != 0) {
120
        struct stat old_stat;
121
        if ((curtime - dns_addr_time) < 1000) {
122
            *pdns_addr = dns_addr;
123
            return 0;
124
        }
125
        old_stat = dns_addr_stat;
126
        if (stat("/etc/resolv.conf", &dns_addr_stat) != 0)
127
            return -1;
128
        if ((dns_addr_stat.st_dev == old_stat.st_dev)
129
            && (dns_addr_stat.st_ino == old_stat.st_ino)
130
            && (dns_addr_stat.st_size == old_stat.st_size)
131
            && (dns_addr_stat.st_mtime == old_stat.st_mtime)) {
132
            *pdns_addr = dns_addr;
133
            return 0;
134
        }
135
    }
136

    
137
    f = fopen("/etc/resolv.conf", "r");
138
    if (!f)
139
        return -1;
140

    
141
#ifdef DEBUG
142
    lprint("IP address of your DNS(s): ");
143
#endif
144
    while (fgets(buff, 512, f) != NULL) {
145
        if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
146
            if (!inet_aton(buff2, &tmp_addr))
147
                continue;
148
            /* If it's the first one, set it to dns_addr */
149
            if (!found) {
150
                *pdns_addr = tmp_addr;
151
                dns_addr = tmp_addr;
152
                dns_addr_time = curtime;
153
            }
154
#ifdef DEBUG
155
            else
156
                lprint(", ");
157
#endif
158
            if (++found > 3) {
159
#ifdef DEBUG
160
                lprint("(more)");
161
#endif
162
                break;
163
            }
164
#ifdef DEBUG
165
            else
166
                lprint("%s", inet_ntoa(tmp_addr));
167
#endif
168
        }
169
    }
170
    fclose(f);
171
    if (!found)
172
        return -1;
173
    return 0;
174
}
175

    
176
#endif
177

    
178
static void slirp_init_once(void)
179
{
180
    static int initialized;
181
#ifdef _WIN32
182
    WSADATA Data;
183
#endif
184

    
185
    if (initialized) {
186
        return;
187
    }
188
    initialized = 1;
189

    
190
#ifdef _WIN32
191
    WSAStartup(MAKEWORD(2,0), &Data);
192
    atexit(winsock_cleanup);
193
#endif
194

    
195
    loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
196
    loopback_mask = htonl(IN_CLASSA_NET);
197
}
198

    
199
static void slirp_state_save(QEMUFile *f, void *opaque);
200
static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
201

    
202
Slirp *slirp_init(int restricted, struct in_addr vnetwork,
203
                  struct in_addr vnetmask, struct in_addr vhost,
204
                  const char *vhostname, const char *tftp_path,
205
                  const char *bootfile, struct in_addr vdhcp_start,
206
                  struct in_addr vnameserver, const char **vdnssearch,
207
                  void *opaque)
208
{
209
    Slirp *slirp = g_malloc0(sizeof(Slirp));
210

    
211
    slirp_init_once();
212

    
213
    slirp->restricted = restricted;
214

    
215
    if_init(slirp);
216
    ip_init(slirp);
217

    
218
    /* Initialise mbufs *after* setting the MTU */
219
    m_init(slirp);
220

    
221
    slirp->vnetwork_addr = vnetwork;
222
    slirp->vnetwork_mask = vnetmask;
223
    slirp->vhost_addr = vhost;
224
    if (vhostname) {
225
        pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
226
                vhostname);
227
    }
228
    if (tftp_path) {
229
        slirp->tftp_prefix = g_strdup(tftp_path);
230
    }
231
    if (bootfile) {
232
        slirp->bootp_filename = g_strdup(bootfile);
233
    }
234
    slirp->vdhcp_startaddr = vdhcp_start;
235
    slirp->vnameserver_addr = vnameserver;
236

    
237
    if (vdnssearch) {
238
        translate_dnssearch(slirp, vdnssearch);
239
    }
240

    
241
    slirp->opaque = opaque;
242

    
243
    register_savevm(NULL, "slirp", 0, 3,
244
                    slirp_state_save, slirp_state_load, slirp);
245

    
246
    QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
247

    
248
    return slirp;
249
}
250

    
251
void slirp_cleanup(Slirp *slirp)
252
{
253
    QTAILQ_REMOVE(&slirp_instances, slirp, entry);
254

    
255
    unregister_savevm(NULL, "slirp", slirp);
256

    
257
    ip_cleanup(slirp);
258
    m_cleanup(slirp);
259

    
260
    g_free(slirp->vdnssearch);
261
    g_free(slirp->tftp_prefix);
262
    g_free(slirp->bootp_filename);
263
    g_free(slirp);
264
}
265

    
266
#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
267
#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
268
#define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
269

    
270
void slirp_update_timeout(uint32_t *timeout)
271
{
272
    if (!QTAILQ_EMPTY(&slirp_instances)) {
273
        *timeout = MIN(1000, *timeout);
274
    }
275
}
276

    
277
void slirp_select_fill(int *pnfds,
278
                       fd_set *readfds, fd_set *writefds, fd_set *xfds)
279
{
280
    Slirp *slirp;
281
    struct socket *so, *so_next;
282
    int nfds;
283

    
284
    if (QTAILQ_EMPTY(&slirp_instances)) {
285
        return;
286
    }
287

    
288
    /* fail safe */
289
    global_readfds = NULL;
290
    global_writefds = NULL;
291
    global_xfds = NULL;
292

    
293
    nfds = *pnfds;
294
        /*
295
         * First, TCP sockets
296
         */
297
        do_slowtimo = 0;
298

    
299
        QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
300
                /*
301
                 * *_slowtimo needs calling if there are IP fragments
302
                 * in the fragment queue, or there are TCP connections active
303
                 */
304
                do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
305
                    (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
306

    
307
                for (so = slirp->tcb.so_next; so != &slirp->tcb;
308
                     so = so_next) {
309
                        so_next = so->so_next;
310

    
311
                        /*
312
                         * See if we need a tcp_fasttimo
313
                         */
314
                        if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
315
                           time_fasttimo = curtime; /* Flag when we want a fasttimo */
316

    
317
                        /*
318
                         * NOFDREF can include still connecting to local-host,
319
                         * newly socreated() sockets etc. Don't want to select these.
320
                          */
321
                        if (so->so_state & SS_NOFDREF || so->s == -1)
322
                           continue;
323

    
324
                        /*
325
                         * Set for reading sockets which are accepting
326
                         */
327
                        if (so->so_state & SS_FACCEPTCONN) {
328
                                FD_SET(so->s, readfds);
329
                                UPD_NFDS(so->s);
330
                                continue;
331
                        }
332

    
333
                        /*
334
                         * Set for writing sockets which are connecting
335
                         */
336
                        if (so->so_state & SS_ISFCONNECTING) {
337
                                FD_SET(so->s, writefds);
338
                                UPD_NFDS(so->s);
339
                                continue;
340
                        }
341

    
342
                        /*
343
                         * Set for writing if we are connected, can send more, and
344
                         * we have something to send
345
                         */
346
                        if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
347
                                FD_SET(so->s, writefds);
348
                                UPD_NFDS(so->s);
349
                        }
350

    
351
                        /*
352
                         * Set for reading (and urgent data) if we are connected, can
353
                         * receive more, and we have room for it XXX /2 ?
354
                         */
355
                        if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
356
                                FD_SET(so->s, readfds);
357
                                FD_SET(so->s, xfds);
358
                                UPD_NFDS(so->s);
359
                        }
360
                }
361

    
362
                /*
363
                 * UDP sockets
364
                 */
365
                for (so = slirp->udb.so_next; so != &slirp->udb;
366
                     so = so_next) {
367
                        so_next = so->so_next;
368

    
369
                        /*
370
                         * See if it's timed out
371
                         */
372
                        if (so->so_expire) {
373
                                if (so->so_expire <= curtime) {
374
                                        udp_detach(so);
375
                                        continue;
376
                                } else
377
                                        do_slowtimo = 1; /* Let socket expire */
378
                        }
379

    
380
                        /*
381
                         * When UDP packets are received from over the
382
                         * link, they're sendto()'d straight away, so
383
                         * no need for setting for writing
384
                         * Limit the number of packets queued by this session
385
                         * to 4.  Note that even though we try and limit this
386
                         * to 4 packets, the session could have more queued
387
                         * if the packets needed to be fragmented
388
                         * (XXX <= 4 ?)
389
                         */
390
                        if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
391
                                FD_SET(so->s, readfds);
392
                                UPD_NFDS(so->s);
393
                        }
394
                }
395

    
396
                /*
397
                 * ICMP sockets
398
                 */
399
                for (so = slirp->icmp.so_next; so != &slirp->icmp;
400
                     so = so_next) {
401
                    so_next = so->so_next;
402

    
403
                    /*
404
                     * See if it's timed out
405
                     */
406
                    if (so->so_expire) {
407
                        if (so->so_expire <= curtime) {
408
                            icmp_detach(so);
409
                            continue;
410
                        } else {
411
                            do_slowtimo = 1; /* Let socket expire */
412
                        }
413
                    }
414

    
415
                    if (so->so_state & SS_ISFCONNECTED) {
416
                        FD_SET(so->s, readfds);
417
                        UPD_NFDS(so->s);
418
                    }
419
                }
420
        }
421

    
422
        *pnfds = nfds;
423
}
424

    
425
void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
426
                       int select_error)
427
{
428
    Slirp *slirp;
429
    struct socket *so, *so_next;
430
    int ret;
431

    
432
    if (QTAILQ_EMPTY(&slirp_instances)) {
433
        return;
434
    }
435

    
436
    global_readfds = readfds;
437
    global_writefds = writefds;
438
    global_xfds = xfds;
439

    
440
    curtime = qemu_get_clock_ms(rt_clock);
441

    
442
    QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
443
        /*
444
         * See if anything has timed out
445
         */
446
                if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
447
                        tcp_fasttimo(slirp);
448
                        time_fasttimo = 0;
449
                }
450
                if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
451
                        ip_slowtimo(slirp);
452
                        tcp_slowtimo(slirp);
453
                        last_slowtimo = curtime;
454
                }
455

    
456
        /*
457
         * Check sockets
458
         */
459
        if (!select_error) {
460
                /*
461
                 * Check TCP sockets
462
                 */
463
                for (so = slirp->tcb.so_next; so != &slirp->tcb;
464
                     so = so_next) {
465
                        so_next = so->so_next;
466

    
467
                        /*
468
                         * FD_ISSET is meaningless on these sockets
469
                         * (and they can crash the program)
470
                         */
471
                        if (so->so_state & SS_NOFDREF || so->s == -1)
472
                           continue;
473

    
474
                        /*
475
                         * Check for URG data
476
                         * This will soread as well, so no need to
477
                         * test for readfds below if this succeeds
478
                         */
479
                        if (FD_ISSET(so->s, xfds))
480
                           sorecvoob(so);
481
                        /*
482
                         * Check sockets for reading
483
                         */
484
                        else if (FD_ISSET(so->s, readfds)) {
485
                                /*
486
                                 * Check for incoming connections
487
                                 */
488
                                if (so->so_state & SS_FACCEPTCONN) {
489
                                        tcp_connect(so);
490
                                        continue;
491
                                } /* else */
492
                                ret = soread(so);
493

    
494
                                /* Output it if we read something */
495
                                if (ret > 0)
496
                                   tcp_output(sototcpcb(so));
497
                        }
498

    
499
                        /*
500
                         * Check sockets for writing
501
                         */
502
                        if (FD_ISSET(so->s, writefds)) {
503
                          /*
504
                           * Check for non-blocking, still-connecting sockets
505
                           */
506
                          if (so->so_state & SS_ISFCONNECTING) {
507
                            /* Connected */
508
                            so->so_state &= ~SS_ISFCONNECTING;
509

    
510
                            ret = send(so->s, (const void *) &ret, 0, 0);
511
                            if (ret < 0) {
512
                              /* XXXXX Must fix, zero bytes is a NOP */
513
                              if (errno == EAGAIN || errno == EWOULDBLOCK ||
514
                                  errno == EINPROGRESS || errno == ENOTCONN)
515
                                continue;
516

    
517
                              /* else failed */
518
                              so->so_state &= SS_PERSISTENT_MASK;
519
                              so->so_state |= SS_NOFDREF;
520
                            }
521
                            /* else so->so_state &= ~SS_ISFCONNECTING; */
522

    
523
                            /*
524
                             * Continue tcp_input
525
                             */
526
                            tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
527
                            /* continue; */
528
                          } else
529
                            ret = sowrite(so);
530
                          /*
531
                           * XXXXX If we wrote something (a lot), there
532
                           * could be a need for a window update.
533
                           * In the worst case, the remote will send
534
                           * a window probe to get things going again
535
                           */
536
                        }
537

    
538
                        /*
539
                         * Probe a still-connecting, non-blocking socket
540
                         * to check if it's still alive
541
                           */
542
#ifdef PROBE_CONN
543
                        if (so->so_state & SS_ISFCONNECTING) {
544
                          ret = qemu_recv(so->s, &ret, 0,0);
545

    
546
                          if (ret < 0) {
547
                            /* XXX */
548
                            if (errno == EAGAIN || errno == EWOULDBLOCK ||
549
                                errno == EINPROGRESS || errno == ENOTCONN)
550
                              continue; /* Still connecting, continue */
551

    
552
                            /* else failed */
553
                            so->so_state &= SS_PERSISTENT_MASK;
554
                            so->so_state |= SS_NOFDREF;
555

    
556
                            /* tcp_input will take care of it */
557
                          } else {
558
                            ret = send(so->s, &ret, 0,0);
559
                            if (ret < 0) {
560
                              /* XXX */
561
                              if (errno == EAGAIN || errno == EWOULDBLOCK ||
562
                                  errno == EINPROGRESS || errno == ENOTCONN)
563
                                continue;
564
                              /* else failed */
565
                              so->so_state &= SS_PERSISTENT_MASK;
566
                              so->so_state |= SS_NOFDREF;
567
                            } else
568
                              so->so_state &= ~SS_ISFCONNECTING;
569

    
570
                          }
571
                          tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
572
                        } /* SS_ISFCONNECTING */
573
#endif
574
                }
575

    
576
                /*
577
                 * Now UDP sockets.
578
                 * Incoming packets are sent straight away, they're not buffered.
579
                 * Incoming UDP data isn't buffered either.
580
                 */
581
                for (so = slirp->udb.so_next; so != &slirp->udb;
582
                     so = so_next) {
583
                        so_next = so->so_next;
584

    
585
                        if (so->s != -1 && FD_ISSET(so->s, readfds)) {
586
                            sorecvfrom(so);
587
                        }
588
                }
589

    
590
                /*
591
                 * Check incoming ICMP relies.
592
                 */
593
                for (so = slirp->icmp.so_next; so != &slirp->icmp;
594
                     so = so_next) {
595
                     so_next = so->so_next;
596

    
597
                    if (so->s != -1 && FD_ISSET(so->s, readfds)) {
598
                        icmp_receive(so);
599
                    }
600
                }
601
        }
602

    
603
        if_start(slirp);
604
    }
605

    
606
        /* clear global file descriptor sets.
607
         * these reside on the stack in vl.c
608
         * so they're unusable if we're not in
609
         * slirp_select_fill or slirp_select_poll.
610
         */
611
         global_readfds = NULL;
612
         global_writefds = NULL;
613
         global_xfds = NULL;
614
}
615

    
616
static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
617
{
618
    struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
619
    uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
620
    struct ethhdr *reh = (struct ethhdr *)arp_reply;
621
    struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
622
    int ar_op;
623
    struct ex_list *ex_ptr;
624

    
625
    ar_op = ntohs(ah->ar_op);
626
    switch(ar_op) {
627
    case ARPOP_REQUEST:
628
        if (ah->ar_tip == ah->ar_sip) {
629
            /* Gratuitous ARP */
630
            arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
631
            return;
632
        }
633

    
634
        if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
635
            slirp->vnetwork_addr.s_addr) {
636
            if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
637
                ah->ar_tip == slirp->vhost_addr.s_addr)
638
                goto arp_ok;
639
            for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
640
                if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
641
                    goto arp_ok;
642
            }
643
            return;
644
        arp_ok:
645
            memset(arp_reply, 0, sizeof(arp_reply));
646

    
647
            arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
648

    
649
            /* ARP request for alias/dns mac address */
650
            memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
651
            memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
652
            memcpy(&reh->h_source[2], &ah->ar_tip, 4);
653
            reh->h_proto = htons(ETH_P_ARP);
654

    
655
            rah->ar_hrd = htons(1);
656
            rah->ar_pro = htons(ETH_P_IP);
657
            rah->ar_hln = ETH_ALEN;
658
            rah->ar_pln = 4;
659
            rah->ar_op = htons(ARPOP_REPLY);
660
            memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
661
            rah->ar_sip = ah->ar_tip;
662
            memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
663
            rah->ar_tip = ah->ar_sip;
664
            slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
665
        }
666
        break;
667
    case ARPOP_REPLY:
668
        arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
669
        break;
670
    default:
671
        break;
672
    }
673
}
674

    
675
void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
676
{
677
    struct mbuf *m;
678
    int proto;
679

    
680
    if (pkt_len < ETH_HLEN)
681
        return;
682

    
683
    proto = ntohs(*(uint16_t *)(pkt + 12));
684
    switch(proto) {
685
    case ETH_P_ARP:
686
        arp_input(slirp, pkt, pkt_len);
687
        break;
688
    case ETH_P_IP:
689
        m = m_get(slirp);
690
        if (!m)
691
            return;
692
        /* Note: we add to align the IP header */
693
        if (M_FREEROOM(m) < pkt_len + 2) {
694
            m_inc(m, pkt_len + 2);
695
        }
696
        m->m_len = pkt_len + 2;
697
        memcpy(m->m_data + 2, pkt, pkt_len);
698

    
699
        m->m_data += 2 + ETH_HLEN;
700
        m->m_len -= 2 + ETH_HLEN;
701

    
702
        ip_input(m);
703
        break;
704
    default:
705
        break;
706
    }
707
}
708

    
709
/* Output the IP packet to the ethernet device. Returns 0 if the packet must be
710
 * re-queued.
711
 */
712
int if_encap(Slirp *slirp, struct mbuf *ifm)
713
{
714
    uint8_t buf[1600];
715
    struct ethhdr *eh = (struct ethhdr *)buf;
716
    uint8_t ethaddr[ETH_ALEN];
717
    const struct ip *iph = (const struct ip *)ifm->m_data;
718

    
719
    if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
720
        return 1;
721
    }
722

    
723
    if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
724
        uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
725
        struct ethhdr *reh = (struct ethhdr *)arp_req;
726
        struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
727

    
728
        if (!ifm->arp_requested) {
729
            /* If the client addr is not known, send an ARP request */
730
            memset(reh->h_dest, 0xff, ETH_ALEN);
731
            memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
732
            memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
733
            reh->h_proto = htons(ETH_P_ARP);
734
            rah->ar_hrd = htons(1);
735
            rah->ar_pro = htons(ETH_P_IP);
736
            rah->ar_hln = ETH_ALEN;
737
            rah->ar_pln = 4;
738
            rah->ar_op = htons(ARPOP_REQUEST);
739

    
740
            /* source hw addr */
741
            memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
742
            memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
743

    
744
            /* source IP */
745
            rah->ar_sip = slirp->vhost_addr.s_addr;
746

    
747
            /* target hw addr (none) */
748
            memset(rah->ar_tha, 0, ETH_ALEN);
749

    
750
            /* target IP */
751
            rah->ar_tip = iph->ip_dst.s_addr;
752
            slirp->client_ipaddr = iph->ip_dst;
753
            slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
754
            ifm->arp_requested = true;
755

    
756
            /* Expire request and drop outgoing packet after 1 second */
757
            ifm->expiration_date = qemu_get_clock_ns(rt_clock) + 1000000000ULL;
758
        }
759
        return 0;
760
    } else {
761
        memcpy(eh->h_dest, ethaddr, ETH_ALEN);
762
        memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
763
        /* XXX: not correct */
764
        memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
765
        eh->h_proto = htons(ETH_P_IP);
766
        memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
767
        slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
768
        return 1;
769
    }
770
}
771

    
772
/* Drop host forwarding rule, return 0 if found. */
773
int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
774
                         int host_port)
775
{
776
    struct socket *so;
777
    struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
778
    struct sockaddr_in addr;
779
    int port = htons(host_port);
780
    socklen_t addr_len;
781

    
782
    for (so = head->so_next; so != head; so = so->so_next) {
783
        addr_len = sizeof(addr);
784
        if ((so->so_state & SS_HOSTFWD) &&
785
            getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
786
            addr.sin_addr.s_addr == host_addr.s_addr &&
787
            addr.sin_port == port) {
788
            close(so->s);
789
            sofree(so);
790
            return 0;
791
        }
792
    }
793

    
794
    return -1;
795
}
796

    
797
int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
798
                      int host_port, struct in_addr guest_addr, int guest_port)
799
{
800
    if (!guest_addr.s_addr) {
801
        guest_addr = slirp->vdhcp_startaddr;
802
    }
803
    if (is_udp) {
804
        if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
805
                        guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
806
            return -1;
807
    } else {
808
        if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
809
                        guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
810
            return -1;
811
    }
812
    return 0;
813
}
814

    
815
int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
816
                   struct in_addr *guest_addr, int guest_port)
817
{
818
    if (!guest_addr->s_addr) {
819
        guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
820
            (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
821
    }
822
    if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
823
        slirp->vnetwork_addr.s_addr ||
824
        guest_addr->s_addr == slirp->vhost_addr.s_addr ||
825
        guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
826
        return -1;
827
    }
828
    return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
829
                    htons(guest_port));
830
}
831

    
832
ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
833
{
834
        if (so->s == -1 && so->extra) {
835
                qemu_chr_fe_write(so->extra, buf, len);
836
                return len;
837
        }
838

    
839
        return send(so->s, buf, len, flags);
840
}
841

    
842
static struct socket *
843
slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
844
{
845
    struct socket *so;
846

    
847
    for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
848
        if (so->so_faddr.s_addr == guest_addr.s_addr &&
849
            htons(so->so_fport) == guest_port) {
850
            return so;
851
        }
852
    }
853
    return NULL;
854
}
855

    
856
size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
857
                             int guest_port)
858
{
859
        struct iovec iov[2];
860
        struct socket *so;
861

    
862
        so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
863

    
864
        if (!so || so->so_state & SS_NOFDREF)
865
                return 0;
866

    
867
        if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2))
868
                return 0;
869

    
870
        return sopreprbuf(so, iov, NULL);
871
}
872

    
873
void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
874
                       const uint8_t *buf, int size)
875
{
876
    int ret;
877
    struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
878

    
879
    if (!so)
880
        return;
881

    
882
    ret = soreadbuf(so, (const char *)buf, size);
883

    
884
    if (ret > 0)
885
        tcp_output(sototcpcb(so));
886
}
887

    
888
static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
889
{
890
    int i;
891

    
892
    qemu_put_sbe16(f, tp->t_state);
893
    for (i = 0; i < TCPT_NTIMERS; i++)
894
        qemu_put_sbe16(f, tp->t_timer[i]);
895
    qemu_put_sbe16(f, tp->t_rxtshift);
896
    qemu_put_sbe16(f, tp->t_rxtcur);
897
    qemu_put_sbe16(f, tp->t_dupacks);
898
    qemu_put_be16(f, tp->t_maxseg);
899
    qemu_put_sbyte(f, tp->t_force);
900
    qemu_put_be16(f, tp->t_flags);
901
    qemu_put_be32(f, tp->snd_una);
902
    qemu_put_be32(f, tp->snd_nxt);
903
    qemu_put_be32(f, tp->snd_up);
904
    qemu_put_be32(f, tp->snd_wl1);
905
    qemu_put_be32(f, tp->snd_wl2);
906
    qemu_put_be32(f, tp->iss);
907
    qemu_put_be32(f, tp->snd_wnd);
908
    qemu_put_be32(f, tp->rcv_wnd);
909
    qemu_put_be32(f, tp->rcv_nxt);
910
    qemu_put_be32(f, tp->rcv_up);
911
    qemu_put_be32(f, tp->irs);
912
    qemu_put_be32(f, tp->rcv_adv);
913
    qemu_put_be32(f, tp->snd_max);
914
    qemu_put_be32(f, tp->snd_cwnd);
915
    qemu_put_be32(f, tp->snd_ssthresh);
916
    qemu_put_sbe16(f, tp->t_idle);
917
    qemu_put_sbe16(f, tp->t_rtt);
918
    qemu_put_be32(f, tp->t_rtseq);
919
    qemu_put_sbe16(f, tp->t_srtt);
920
    qemu_put_sbe16(f, tp->t_rttvar);
921
    qemu_put_be16(f, tp->t_rttmin);
922
    qemu_put_be32(f, tp->max_sndwnd);
923
    qemu_put_byte(f, tp->t_oobflags);
924
    qemu_put_byte(f, tp->t_iobc);
925
    qemu_put_sbe16(f, tp->t_softerror);
926
    qemu_put_byte(f, tp->snd_scale);
927
    qemu_put_byte(f, tp->rcv_scale);
928
    qemu_put_byte(f, tp->request_r_scale);
929
    qemu_put_byte(f, tp->requested_s_scale);
930
    qemu_put_be32(f, tp->ts_recent);
931
    qemu_put_be32(f, tp->ts_recent_age);
932
    qemu_put_be32(f, tp->last_ack_sent);
933
}
934

    
935
static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
936
{
937
    uint32_t off;
938

    
939
    qemu_put_be32(f, sbuf->sb_cc);
940
    qemu_put_be32(f, sbuf->sb_datalen);
941
    off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
942
    qemu_put_sbe32(f, off);
943
    off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
944
    qemu_put_sbe32(f, off);
945
    qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
946
}
947

    
948
static void slirp_socket_save(QEMUFile *f, struct socket *so)
949
{
950
    qemu_put_be32(f, so->so_urgc);
951
    qemu_put_be32(f, so->so_faddr.s_addr);
952
    qemu_put_be32(f, so->so_laddr.s_addr);
953
    qemu_put_be16(f, so->so_fport);
954
    qemu_put_be16(f, so->so_lport);
955
    qemu_put_byte(f, so->so_iptos);
956
    qemu_put_byte(f, so->so_emu);
957
    qemu_put_byte(f, so->so_type);
958
    qemu_put_be32(f, so->so_state);
959
    slirp_sbuf_save(f, &so->so_rcv);
960
    slirp_sbuf_save(f, &so->so_snd);
961
    slirp_tcp_save(f, so->so_tcpcb);
962
}
963

    
964
static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
965
{
966
    int i;
967

    
968
    for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
969
        qemu_put_be16(f, slirp->bootp_clients[i].allocated);
970
        qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
971
    }
972
}
973

    
974
static void slirp_state_save(QEMUFile *f, void *opaque)
975
{
976
    Slirp *slirp = opaque;
977
    struct ex_list *ex_ptr;
978

    
979
    for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
980
        if (ex_ptr->ex_pty == 3) {
981
            struct socket *so;
982
            so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
983
                                       ntohs(ex_ptr->ex_fport));
984
            if (!so)
985
                continue;
986

    
987
            qemu_put_byte(f, 42);
988
            slirp_socket_save(f, so);
989
        }
990
    qemu_put_byte(f, 0);
991

    
992
    qemu_put_be16(f, slirp->ip_id);
993

    
994
    slirp_bootp_save(f, slirp);
995
}
996

    
997
static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
998
{
999
    int i;
1000

    
1001
    tp->t_state = qemu_get_sbe16(f);
1002
    for (i = 0; i < TCPT_NTIMERS; i++)
1003
        tp->t_timer[i] = qemu_get_sbe16(f);
1004
    tp->t_rxtshift = qemu_get_sbe16(f);
1005
    tp->t_rxtcur = qemu_get_sbe16(f);
1006
    tp->t_dupacks = qemu_get_sbe16(f);
1007
    tp->t_maxseg = qemu_get_be16(f);
1008
    tp->t_force = qemu_get_sbyte(f);
1009
    tp->t_flags = qemu_get_be16(f);
1010
    tp->snd_una = qemu_get_be32(f);
1011
    tp->snd_nxt = qemu_get_be32(f);
1012
    tp->snd_up = qemu_get_be32(f);
1013
    tp->snd_wl1 = qemu_get_be32(f);
1014
    tp->snd_wl2 = qemu_get_be32(f);
1015
    tp->iss = qemu_get_be32(f);
1016
    tp->snd_wnd = qemu_get_be32(f);
1017
    tp->rcv_wnd = qemu_get_be32(f);
1018
    tp->rcv_nxt = qemu_get_be32(f);
1019
    tp->rcv_up = qemu_get_be32(f);
1020
    tp->irs = qemu_get_be32(f);
1021
    tp->rcv_adv = qemu_get_be32(f);
1022
    tp->snd_max = qemu_get_be32(f);
1023
    tp->snd_cwnd = qemu_get_be32(f);
1024
    tp->snd_ssthresh = qemu_get_be32(f);
1025
    tp->t_idle = qemu_get_sbe16(f);
1026
    tp->t_rtt = qemu_get_sbe16(f);
1027
    tp->t_rtseq = qemu_get_be32(f);
1028
    tp->t_srtt = qemu_get_sbe16(f);
1029
    tp->t_rttvar = qemu_get_sbe16(f);
1030
    tp->t_rttmin = qemu_get_be16(f);
1031
    tp->max_sndwnd = qemu_get_be32(f);
1032
    tp->t_oobflags = qemu_get_byte(f);
1033
    tp->t_iobc = qemu_get_byte(f);
1034
    tp->t_softerror = qemu_get_sbe16(f);
1035
    tp->snd_scale = qemu_get_byte(f);
1036
    tp->rcv_scale = qemu_get_byte(f);
1037
    tp->request_r_scale = qemu_get_byte(f);
1038
    tp->requested_s_scale = qemu_get_byte(f);
1039
    tp->ts_recent = qemu_get_be32(f);
1040
    tp->ts_recent_age = qemu_get_be32(f);
1041
    tp->last_ack_sent = qemu_get_be32(f);
1042
    tcp_template(tp);
1043
}
1044

    
1045
static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1046
{
1047
    uint32_t off, sb_cc, sb_datalen;
1048

    
1049
    sb_cc = qemu_get_be32(f);
1050
    sb_datalen = qemu_get_be32(f);
1051

    
1052
    sbreserve(sbuf, sb_datalen);
1053

    
1054
    if (sbuf->sb_datalen != sb_datalen)
1055
        return -ENOMEM;
1056

    
1057
    sbuf->sb_cc = sb_cc;
1058

    
1059
    off = qemu_get_sbe32(f);
1060
    sbuf->sb_wptr = sbuf->sb_data + off;
1061
    off = qemu_get_sbe32(f);
1062
    sbuf->sb_rptr = sbuf->sb_data + off;
1063
    qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1064

    
1065
    return 0;
1066
}
1067

    
1068
static int slirp_socket_load(QEMUFile *f, struct socket *so)
1069
{
1070
    if (tcp_attach(so) < 0)
1071
        return -ENOMEM;
1072

    
1073
    so->so_urgc = qemu_get_be32(f);
1074
    so->so_faddr.s_addr = qemu_get_be32(f);
1075
    so->so_laddr.s_addr = qemu_get_be32(f);
1076
    so->so_fport = qemu_get_be16(f);
1077
    so->so_lport = qemu_get_be16(f);
1078
    so->so_iptos = qemu_get_byte(f);
1079
    so->so_emu = qemu_get_byte(f);
1080
    so->so_type = qemu_get_byte(f);
1081
    so->so_state = qemu_get_be32(f);
1082
    if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1083
        return -ENOMEM;
1084
    if (slirp_sbuf_load(f, &so->so_snd) < 0)
1085
        return -ENOMEM;
1086
    slirp_tcp_load(f, so->so_tcpcb);
1087

    
1088
    return 0;
1089
}
1090

    
1091
static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1092
{
1093
    int i;
1094

    
1095
    for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1096
        slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1097
        qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1098
    }
1099
}
1100

    
1101
static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1102
{
1103
    Slirp *slirp = opaque;
1104
    struct ex_list *ex_ptr;
1105

    
1106
    while (qemu_get_byte(f)) {
1107
        int ret;
1108
        struct socket *so = socreate(slirp);
1109

    
1110
        if (!so)
1111
            return -ENOMEM;
1112

    
1113
        ret = slirp_socket_load(f, so);
1114

    
1115
        if (ret < 0)
1116
            return ret;
1117

    
1118
        if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1119
            slirp->vnetwork_addr.s_addr) {
1120
            return -EINVAL;
1121
        }
1122
        for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1123
            if (ex_ptr->ex_pty == 3 &&
1124
                so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1125
                so->so_fport == ex_ptr->ex_fport) {
1126
                break;
1127
            }
1128
        }
1129
        if (!ex_ptr)
1130
            return -EINVAL;
1131

    
1132
        so->extra = (void *)ex_ptr->ex_exec;
1133
    }
1134

    
1135
    if (version_id >= 2) {
1136
        slirp->ip_id = qemu_get_be16(f);
1137
    }
1138

    
1139
    if (version_id >= 3) {
1140
        slirp_bootp_load(f, slirp);
1141
    }
1142

    
1143
    return 0;
1144
}