Statistics
| Branch: | Revision:

root / slirp / slirp.c @ 1a0ca1e1

History | View | Annotate | Download (30.6 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

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

    
38
static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
39

    
40
/* XXX: suppress those select globals */
41
fd_set *global_readfds, *global_writefds, *global_xfds;
42

    
43
u_int curtime;
44
static u_int time_fasttimo, last_slowtimo;
45
static int do_slowtimo;
46

    
47
static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
48
    QTAILQ_HEAD_INITIALIZER(slirp_instances);
49

    
50
static struct in_addr dns_addr;
51
static u_int dns_addr_time;
52

    
53
#ifdef _WIN32
54

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

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

    
68
    FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
69
    BufLen = sizeof(FIXED_INFO);
70

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

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

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

    
100
static void winsock_cleanup(void)
101
{
102
    WSACleanup();
103
}
104

    
105
#else
106

    
107
static struct stat dns_addr_stat;
108

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

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

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

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

    
174
#endif
175

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

    
183
    if (initialized) {
184
        return;
185
    }
186
    initialized = 1;
187

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

    
193
    loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
194
}
195

    
196
static void slirp_state_save(QEMUFile *f, void *opaque);
197
static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
198

    
199
Slirp *slirp_init(int restricted, struct in_addr vnetwork,
200
                  struct in_addr vnetmask, struct in_addr vhost,
201
                  const char *vhostname, const char *tftp_path,
202
                  const char *bootfile, struct in_addr vdhcp_start,
203
                  struct in_addr vnameserver, void *opaque)
204
{
205
    Slirp *slirp = qemu_mallocz(sizeof(Slirp));
206

    
207
    slirp_init_once();
208

    
209
    slirp->restricted = restricted;
210

    
211
    if_init(slirp);
212
    ip_init(slirp);
213

    
214
    /* Initialise mbufs *after* setting the MTU */
215
    m_init(slirp);
216

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

    
233
    slirp->opaque = opaque;
234

    
235
    register_savevm(NULL, "slirp", 0, 3,
236
                    slirp_state_save, slirp_state_load, slirp);
237

    
238
    QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
239

    
240
    return slirp;
241
}
242

    
243
void slirp_cleanup(Slirp *slirp)
244
{
245
    QTAILQ_REMOVE(&slirp_instances, slirp, entry);
246

    
247
    unregister_savevm(NULL, "slirp", slirp);
248

    
249
    qemu_free(slirp->tftp_prefix);
250
    qemu_free(slirp->bootp_filename);
251
    qemu_free(slirp);
252
}
253

    
254
#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
255
#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
256
#define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
257

    
258
void slirp_select_fill(int *pnfds,
259
                       fd_set *readfds, fd_set *writefds, fd_set *xfds)
260
{
261
    Slirp *slirp;
262
    struct socket *so, *so_next;
263
    int nfds;
264

    
265
    if (QTAILQ_EMPTY(&slirp_instances)) {
266
        return;
267
    }
268

    
269
    /* fail safe */
270
    global_readfds = NULL;
271
    global_writefds = NULL;
272
    global_xfds = NULL;
273

    
274
    nfds = *pnfds;
275
        /*
276
         * First, TCP sockets
277
         */
278
        do_slowtimo = 0;
279

    
280
        QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
281
                /*
282
                 * *_slowtimo needs calling if there are IP fragments
283
                 * in the fragment queue, or there are TCP connections active
284
                 */
285
                do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
286
                    (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
287

    
288
                for (so = slirp->tcb.so_next; so != &slirp->tcb;
289
                     so = so_next) {
290
                        so_next = so->so_next;
291

    
292
                        /*
293
                         * See if we need a tcp_fasttimo
294
                         */
295
                        if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
296
                           time_fasttimo = curtime; /* Flag when we want a fasttimo */
297

    
298
                        /*
299
                         * NOFDREF can include still connecting to local-host,
300
                         * newly socreated() sockets etc. Don't want to select these.
301
                          */
302
                        if (so->so_state & SS_NOFDREF || so->s == -1)
303
                           continue;
304

    
305
                        /*
306
                         * Set for reading sockets which are accepting
307
                         */
308
                        if (so->so_state & SS_FACCEPTCONN) {
309
                                FD_SET(so->s, readfds);
310
                                UPD_NFDS(so->s);
311
                                continue;
312
                        }
313

    
314
                        /*
315
                         * Set for writing sockets which are connecting
316
                         */
317
                        if (so->so_state & SS_ISFCONNECTING) {
318
                                FD_SET(so->s, writefds);
319
                                UPD_NFDS(so->s);
320
                                continue;
321
                        }
322

    
323
                        /*
324
                         * Set for writing if we are connected, can send more, and
325
                         * we have something to send
326
                         */
327
                        if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
328
                                FD_SET(so->s, writefds);
329
                                UPD_NFDS(so->s);
330
                        }
331

    
332
                        /*
333
                         * Set for reading (and urgent data) if we are connected, can
334
                         * receive more, and we have room for it XXX /2 ?
335
                         */
336
                        if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
337
                                FD_SET(so->s, readfds);
338
                                FD_SET(so->s, xfds);
339
                                UPD_NFDS(so->s);
340
                        }
341
                }
342

    
343
                /*
344
                 * UDP sockets
345
                 */
346
                for (so = slirp->udb.so_next; so != &slirp->udb;
347
                     so = so_next) {
348
                        so_next = so->so_next;
349

    
350
                        /*
351
                         * See if it's timed out
352
                         */
353
                        if (so->so_expire) {
354
                                if (so->so_expire <= curtime) {
355
                                        udp_detach(so);
356
                                        continue;
357
                                } else
358
                                        do_slowtimo = 1; /* Let socket expire */
359
                        }
360

    
361
                        /*
362
                         * When UDP packets are received from over the
363
                         * link, they're sendto()'d straight away, so
364
                         * no need for setting for writing
365
                         * Limit the number of packets queued by this session
366
                         * to 4.  Note that even though we try and limit this
367
                         * to 4 packets, the session could have more queued
368
                         * if the packets needed to be fragmented
369
                         * (XXX <= 4 ?)
370
                         */
371
                        if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
372
                                FD_SET(so->s, readfds);
373
                                UPD_NFDS(so->s);
374
                        }
375
                }
376

    
377
                /*
378
                 * ICMP sockets
379
                 */
380
                for (so = slirp->icmp.so_next; so != &slirp->icmp;
381
                     so = so_next) {
382
                    so_next = so->so_next;
383

    
384
                    /*
385
                     * See if it's timed out
386
                     */
387
                    if (so->so_expire) {
388
                        if (so->so_expire <= curtime) {
389
                            icmp_detach(so);
390
                            continue;
391
                        } else {
392
                            do_slowtimo = 1; /* Let socket expire */
393
                        }
394
                    }
395

    
396
                    if (so->so_state & SS_ISFCONNECTED) {
397
                        FD_SET(so->s, readfds);
398
                        UPD_NFDS(so->s);
399
                    }
400
                }
401
        }
402

    
403
        *pnfds = nfds;
404
}
405

    
406
void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
407
                       int select_error)
408
{
409
    Slirp *slirp;
410
    struct socket *so, *so_next;
411
    int ret;
412

    
413
    if (QTAILQ_EMPTY(&slirp_instances)) {
414
        return;
415
    }
416

    
417
    global_readfds = readfds;
418
    global_writefds = writefds;
419
    global_xfds = xfds;
420

    
421
    curtime = qemu_get_clock_ms(rt_clock);
422

    
423
    QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
424
        /*
425
         * See if anything has timed out
426
         */
427
                if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
428
                        tcp_fasttimo(slirp);
429
                        time_fasttimo = 0;
430
                }
431
                if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
432
                        ip_slowtimo(slirp);
433
                        tcp_slowtimo(slirp);
434
                        last_slowtimo = curtime;
435
                }
436

    
437
        /*
438
         * Check sockets
439
         */
440
        if (!select_error) {
441
                /*
442
                 * Check TCP sockets
443
                 */
444
                for (so = slirp->tcb.so_next; so != &slirp->tcb;
445
                     so = so_next) {
446
                        so_next = so->so_next;
447

    
448
                        /*
449
                         * FD_ISSET is meaningless on these sockets
450
                         * (and they can crash the program)
451
                         */
452
                        if (so->so_state & SS_NOFDREF || so->s == -1)
453
                           continue;
454

    
455
                        /*
456
                         * Check for URG data
457
                         * This will soread as well, so no need to
458
                         * test for readfds below if this succeeds
459
                         */
460
                        if (FD_ISSET(so->s, xfds))
461
                           sorecvoob(so);
462
                        /*
463
                         * Check sockets for reading
464
                         */
465
                        else if (FD_ISSET(so->s, readfds)) {
466
                                /*
467
                                 * Check for incoming connections
468
                                 */
469
                                if (so->so_state & SS_FACCEPTCONN) {
470
                                        tcp_connect(so);
471
                                        continue;
472
                                } /* else */
473
                                ret = soread(so);
474

    
475
                                /* Output it if we read something */
476
                                if (ret > 0)
477
                                   tcp_output(sototcpcb(so));
478
                        }
479

    
480
                        /*
481
                         * Check sockets for writing
482
                         */
483
                        if (FD_ISSET(so->s, writefds)) {
484
                          /*
485
                           * Check for non-blocking, still-connecting sockets
486
                           */
487
                          if (so->so_state & SS_ISFCONNECTING) {
488
                            /* Connected */
489
                            so->so_state &= ~SS_ISFCONNECTING;
490

    
491
                            ret = send(so->s, (const void *) &ret, 0, 0);
492
                            if (ret < 0) {
493
                              /* XXXXX Must fix, zero bytes is a NOP */
494
                              if (errno == EAGAIN || errno == EWOULDBLOCK ||
495
                                  errno == EINPROGRESS || errno == ENOTCONN)
496
                                continue;
497

    
498
                              /* else failed */
499
                              so->so_state &= SS_PERSISTENT_MASK;
500
                              so->so_state |= SS_NOFDREF;
501
                            }
502
                            /* else so->so_state &= ~SS_ISFCONNECTING; */
503

    
504
                            /*
505
                             * Continue tcp_input
506
                             */
507
                            tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
508
                            /* continue; */
509
                          } else
510
                            ret = sowrite(so);
511
                          /*
512
                           * XXXXX If we wrote something (a lot), there
513
                           * could be a need for a window update.
514
                           * In the worst case, the remote will send
515
                           * a window probe to get things going again
516
                           */
517
                        }
518

    
519
                        /*
520
                         * Probe a still-connecting, non-blocking socket
521
                         * to check if it's still alive
522
                           */
523
#ifdef PROBE_CONN
524
                        if (so->so_state & SS_ISFCONNECTING) {
525
                          ret = qemu_recv(so->s, &ret, 0,0);
526

    
527
                          if (ret < 0) {
528
                            /* XXX */
529
                            if (errno == EAGAIN || errno == EWOULDBLOCK ||
530
                                errno == EINPROGRESS || errno == ENOTCONN)
531
                              continue; /* Still connecting, continue */
532

    
533
                            /* else failed */
534
                            so->so_state &= SS_PERSISTENT_MASK;
535
                            so->so_state |= SS_NOFDREF;
536

    
537
                            /* tcp_input will take care of it */
538
                          } else {
539
                            ret = send(so->s, &ret, 0,0);
540
                            if (ret < 0) {
541
                              /* XXX */
542
                              if (errno == EAGAIN || errno == EWOULDBLOCK ||
543
                                  errno == EINPROGRESS || errno == ENOTCONN)
544
                                continue;
545
                              /* else failed */
546
                              so->so_state &= SS_PERSISTENT_MASK;
547
                              so->so_state |= SS_NOFDREF;
548
                            } else
549
                              so->so_state &= ~SS_ISFCONNECTING;
550

    
551
                          }
552
                          tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
553
                        } /* SS_ISFCONNECTING */
554
#endif
555
                }
556

    
557
                /*
558
                 * Now UDP sockets.
559
                 * Incoming packets are sent straight away, they're not buffered.
560
                 * Incoming UDP data isn't buffered either.
561
                 */
562
                for (so = slirp->udb.so_next; so != &slirp->udb;
563
                     so = so_next) {
564
                        so_next = so->so_next;
565

    
566
                        if (so->s != -1 && FD_ISSET(so->s, readfds)) {
567
                            sorecvfrom(so);
568
                        }
569
                }
570

    
571
                /*
572
                 * Check incoming ICMP relies.
573
                 */
574
                for (so = slirp->icmp.so_next; so != &slirp->icmp;
575
                     so = so_next) {
576
                     so_next = so->so_next;
577

    
578
                    if (so->s != -1 && FD_ISSET(so->s, readfds)) {
579
                        icmp_receive(so);
580
                    }
581
                }
582
        }
583

    
584
        /*
585
         * See if we can start outputting
586
         */
587
        if (slirp->if_queued) {
588
            if_start(slirp);
589
        }
590
    }
591

    
592
        /* clear global file descriptor sets.
593
         * these reside on the stack in vl.c
594
         * so they're unusable if we're not in
595
         * slirp_select_fill or slirp_select_poll.
596
         */
597
         global_readfds = NULL;
598
         global_writefds = NULL;
599
         global_xfds = NULL;
600
}
601

    
602
static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
603
{
604
    struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
605
    uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
606
    struct ethhdr *reh = (struct ethhdr *)arp_reply;
607
    struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
608
    int ar_op;
609
    struct ex_list *ex_ptr;
610

    
611
    ar_op = ntohs(ah->ar_op);
612
    switch(ar_op) {
613
    case ARPOP_REQUEST:
614
        if (ah->ar_tip == ah->ar_sip) {
615
            /* Gratuitous ARP */
616
            arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
617
            return;
618
        }
619

    
620
        if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
621
            slirp->vnetwork_addr.s_addr) {
622
            if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
623
                ah->ar_tip == slirp->vhost_addr.s_addr)
624
                goto arp_ok;
625
            for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
626
                if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
627
                    goto arp_ok;
628
            }
629
            return;
630
        arp_ok:
631
            memset(arp_reply, 0, sizeof(arp_reply));
632

    
633
            arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
634

    
635
            /* ARP request for alias/dns mac address */
636
            memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
637
            memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
638
            memcpy(&reh->h_source[2], &ah->ar_tip, 4);
639
            reh->h_proto = htons(ETH_P_ARP);
640

    
641
            rah->ar_hrd = htons(1);
642
            rah->ar_pro = htons(ETH_P_IP);
643
            rah->ar_hln = ETH_ALEN;
644
            rah->ar_pln = 4;
645
            rah->ar_op = htons(ARPOP_REPLY);
646
            memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
647
            rah->ar_sip = ah->ar_tip;
648
            memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
649
            rah->ar_tip = ah->ar_sip;
650
            slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
651
        }
652
        break;
653
    case ARPOP_REPLY:
654
        arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
655
        break;
656
    default:
657
        break;
658
    }
659
}
660

    
661
void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
662
{
663
    struct mbuf *m;
664
    int proto;
665

    
666
    if (pkt_len < ETH_HLEN)
667
        return;
668

    
669
    proto = ntohs(*(uint16_t *)(pkt + 12));
670
    switch(proto) {
671
    case ETH_P_ARP:
672
        arp_input(slirp, pkt, pkt_len);
673
        break;
674
    case ETH_P_IP:
675
        m = m_get(slirp);
676
        if (!m)
677
            return;
678
        /* Note: we add to align the IP header */
679
        if (M_FREEROOM(m) < pkt_len + 2) {
680
            m_inc(m, pkt_len + 2);
681
        }
682
        m->m_len = pkt_len + 2;
683
        memcpy(m->m_data + 2, pkt, pkt_len);
684

    
685
        m->m_data += 2 + ETH_HLEN;
686
        m->m_len -= 2 + ETH_HLEN;
687

    
688
        ip_input(m);
689
        break;
690
    default:
691
        break;
692
    }
693
}
694

    
695
/* output the IP packet to the ethernet device */
696
void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len)
697
{
698
    uint8_t buf[1600];
699
    struct ethhdr *eh = (struct ethhdr *)buf;
700
    uint8_t ethaddr[ETH_ALEN];
701
    const struct ip *iph = (const struct ip *)ip_data;
702

    
703
    if (ip_data_len + ETH_HLEN > sizeof(buf))
704
        return;
705

    
706
    if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
707
        uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
708
        struct ethhdr *reh = (struct ethhdr *)arp_req;
709
        struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
710

    
711
        /* If the client addr is not known, there is no point in
712
           sending the packet to it. Normally the sender should have
713
           done an ARP request to get its MAC address. Here we do it
714
           in place of sending the packet and we hope that the sender
715
           will retry sending its packet. */
716
        memset(reh->h_dest, 0xff, ETH_ALEN);
717
        memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
718
        memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
719
        reh->h_proto = htons(ETH_P_ARP);
720
        rah->ar_hrd = htons(1);
721
        rah->ar_pro = htons(ETH_P_IP);
722
        rah->ar_hln = ETH_ALEN;
723
        rah->ar_pln = 4;
724
        rah->ar_op = htons(ARPOP_REQUEST);
725
        /* source hw addr */
726
        memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
727
        memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
728
        /* source IP */
729
        rah->ar_sip = slirp->vhost_addr.s_addr;
730
        /* target hw addr (none) */
731
        memset(rah->ar_tha, 0, ETH_ALEN);
732
        /* target IP */
733
        rah->ar_tip = iph->ip_dst.s_addr;
734
        slirp->client_ipaddr = iph->ip_dst;
735
        slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
736
    } else {
737
        memcpy(eh->h_dest, ethaddr, ETH_ALEN);
738
        memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
739
        /* XXX: not correct */
740
        memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
741
        eh->h_proto = htons(ETH_P_IP);
742
        memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
743
        slirp_output(slirp->opaque, buf, ip_data_len + ETH_HLEN);
744
    }
745
}
746

    
747
/* Drop host forwarding rule, return 0 if found. */
748
int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
749
                         int host_port)
750
{
751
    struct socket *so;
752
    struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
753
    struct sockaddr_in addr;
754
    int port = htons(host_port);
755
    socklen_t addr_len;
756

    
757
    for (so = head->so_next; so != head; so = so->so_next) {
758
        addr_len = sizeof(addr);
759
        if ((so->so_state & SS_HOSTFWD) &&
760
            getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
761
            addr.sin_addr.s_addr == host_addr.s_addr &&
762
            addr.sin_port == port) {
763
            close(so->s);
764
            sofree(so);
765
            return 0;
766
        }
767
    }
768

    
769
    return -1;
770
}
771

    
772
int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
773
                      int host_port, struct in_addr guest_addr, int guest_port)
774
{
775
    if (!guest_addr.s_addr) {
776
        guest_addr = slirp->vdhcp_startaddr;
777
    }
778
    if (is_udp) {
779
        if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
780
                        guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
781
            return -1;
782
    } else {
783
        if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
784
                        guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
785
            return -1;
786
    }
787
    return 0;
788
}
789

    
790
int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
791
                   struct in_addr *guest_addr, int guest_port)
792
{
793
    if (!guest_addr->s_addr) {
794
        guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
795
            (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
796
    }
797
    if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
798
        slirp->vnetwork_addr.s_addr ||
799
        guest_addr->s_addr == slirp->vhost_addr.s_addr ||
800
        guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
801
        return -1;
802
    }
803
    return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
804
                    htons(guest_port));
805
}
806

    
807
ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
808
{
809
        if (so->s == -1 && so->extra) {
810
                qemu_chr_write(so->extra, buf, len);
811
                return len;
812
        }
813

    
814
        return send(so->s, buf, len, flags);
815
}
816

    
817
static struct socket *
818
slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
819
{
820
    struct socket *so;
821

    
822
    for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
823
        if (so->so_faddr.s_addr == guest_addr.s_addr &&
824
            htons(so->so_fport) == guest_port) {
825
            return so;
826
        }
827
    }
828
    return NULL;
829
}
830

    
831
size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
832
                             int guest_port)
833
{
834
        struct iovec iov[2];
835
        struct socket *so;
836

    
837
        so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
838

    
839
        if (!so || so->so_state & SS_NOFDREF)
840
                return 0;
841

    
842
        if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2))
843
                return 0;
844

    
845
        return sopreprbuf(so, iov, NULL);
846
}
847

    
848
void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
849
                       const uint8_t *buf, int size)
850
{
851
    int ret;
852
    struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
853

    
854
    if (!so)
855
        return;
856

    
857
    ret = soreadbuf(so, (const char *)buf, size);
858

    
859
    if (ret > 0)
860
        tcp_output(sototcpcb(so));
861
}
862

    
863
static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
864
{
865
    int i;
866

    
867
    qemu_put_sbe16(f, tp->t_state);
868
    for (i = 0; i < TCPT_NTIMERS; i++)
869
        qemu_put_sbe16(f, tp->t_timer[i]);
870
    qemu_put_sbe16(f, tp->t_rxtshift);
871
    qemu_put_sbe16(f, tp->t_rxtcur);
872
    qemu_put_sbe16(f, tp->t_dupacks);
873
    qemu_put_be16(f, tp->t_maxseg);
874
    qemu_put_sbyte(f, tp->t_force);
875
    qemu_put_be16(f, tp->t_flags);
876
    qemu_put_be32(f, tp->snd_una);
877
    qemu_put_be32(f, tp->snd_nxt);
878
    qemu_put_be32(f, tp->snd_up);
879
    qemu_put_be32(f, tp->snd_wl1);
880
    qemu_put_be32(f, tp->snd_wl2);
881
    qemu_put_be32(f, tp->iss);
882
    qemu_put_be32(f, tp->snd_wnd);
883
    qemu_put_be32(f, tp->rcv_wnd);
884
    qemu_put_be32(f, tp->rcv_nxt);
885
    qemu_put_be32(f, tp->rcv_up);
886
    qemu_put_be32(f, tp->irs);
887
    qemu_put_be32(f, tp->rcv_adv);
888
    qemu_put_be32(f, tp->snd_max);
889
    qemu_put_be32(f, tp->snd_cwnd);
890
    qemu_put_be32(f, tp->snd_ssthresh);
891
    qemu_put_sbe16(f, tp->t_idle);
892
    qemu_put_sbe16(f, tp->t_rtt);
893
    qemu_put_be32(f, tp->t_rtseq);
894
    qemu_put_sbe16(f, tp->t_srtt);
895
    qemu_put_sbe16(f, tp->t_rttvar);
896
    qemu_put_be16(f, tp->t_rttmin);
897
    qemu_put_be32(f, tp->max_sndwnd);
898
    qemu_put_byte(f, tp->t_oobflags);
899
    qemu_put_byte(f, tp->t_iobc);
900
    qemu_put_sbe16(f, tp->t_softerror);
901
    qemu_put_byte(f, tp->snd_scale);
902
    qemu_put_byte(f, tp->rcv_scale);
903
    qemu_put_byte(f, tp->request_r_scale);
904
    qemu_put_byte(f, tp->requested_s_scale);
905
    qemu_put_be32(f, tp->ts_recent);
906
    qemu_put_be32(f, tp->ts_recent_age);
907
    qemu_put_be32(f, tp->last_ack_sent);
908
}
909

    
910
static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
911
{
912
    uint32_t off;
913

    
914
    qemu_put_be32(f, sbuf->sb_cc);
915
    qemu_put_be32(f, sbuf->sb_datalen);
916
    off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
917
    qemu_put_sbe32(f, off);
918
    off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
919
    qemu_put_sbe32(f, off);
920
    qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
921
}
922

    
923
static void slirp_socket_save(QEMUFile *f, struct socket *so)
924
{
925
    qemu_put_be32(f, so->so_urgc);
926
    qemu_put_be32(f, so->so_faddr.s_addr);
927
    qemu_put_be32(f, so->so_laddr.s_addr);
928
    qemu_put_be16(f, so->so_fport);
929
    qemu_put_be16(f, so->so_lport);
930
    qemu_put_byte(f, so->so_iptos);
931
    qemu_put_byte(f, so->so_emu);
932
    qemu_put_byte(f, so->so_type);
933
    qemu_put_be32(f, so->so_state);
934
    slirp_sbuf_save(f, &so->so_rcv);
935
    slirp_sbuf_save(f, &so->so_snd);
936
    slirp_tcp_save(f, so->so_tcpcb);
937
}
938

    
939
static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
940
{
941
    int i;
942

    
943
    for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
944
        qemu_put_be16(f, slirp->bootp_clients[i].allocated);
945
        qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
946
    }
947
}
948

    
949
static void slirp_state_save(QEMUFile *f, void *opaque)
950
{
951
    Slirp *slirp = opaque;
952
    struct ex_list *ex_ptr;
953

    
954
    for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
955
        if (ex_ptr->ex_pty == 3) {
956
            struct socket *so;
957
            so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
958
                                       ntohs(ex_ptr->ex_fport));
959
            if (!so)
960
                continue;
961

    
962
            qemu_put_byte(f, 42);
963
            slirp_socket_save(f, so);
964
        }
965
    qemu_put_byte(f, 0);
966

    
967
    qemu_put_be16(f, slirp->ip_id);
968

    
969
    slirp_bootp_save(f, slirp);
970
}
971

    
972
static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
973
{
974
    int i;
975

    
976
    tp->t_state = qemu_get_sbe16(f);
977
    for (i = 0; i < TCPT_NTIMERS; i++)
978
        tp->t_timer[i] = qemu_get_sbe16(f);
979
    tp->t_rxtshift = qemu_get_sbe16(f);
980
    tp->t_rxtcur = qemu_get_sbe16(f);
981
    tp->t_dupacks = qemu_get_sbe16(f);
982
    tp->t_maxseg = qemu_get_be16(f);
983
    tp->t_force = qemu_get_sbyte(f);
984
    tp->t_flags = qemu_get_be16(f);
985
    tp->snd_una = qemu_get_be32(f);
986
    tp->snd_nxt = qemu_get_be32(f);
987
    tp->snd_up = qemu_get_be32(f);
988
    tp->snd_wl1 = qemu_get_be32(f);
989
    tp->snd_wl2 = qemu_get_be32(f);
990
    tp->iss = qemu_get_be32(f);
991
    tp->snd_wnd = qemu_get_be32(f);
992
    tp->rcv_wnd = qemu_get_be32(f);
993
    tp->rcv_nxt = qemu_get_be32(f);
994
    tp->rcv_up = qemu_get_be32(f);
995
    tp->irs = qemu_get_be32(f);
996
    tp->rcv_adv = qemu_get_be32(f);
997
    tp->snd_max = qemu_get_be32(f);
998
    tp->snd_cwnd = qemu_get_be32(f);
999
    tp->snd_ssthresh = qemu_get_be32(f);
1000
    tp->t_idle = qemu_get_sbe16(f);
1001
    tp->t_rtt = qemu_get_sbe16(f);
1002
    tp->t_rtseq = qemu_get_be32(f);
1003
    tp->t_srtt = qemu_get_sbe16(f);
1004
    tp->t_rttvar = qemu_get_sbe16(f);
1005
    tp->t_rttmin = qemu_get_be16(f);
1006
    tp->max_sndwnd = qemu_get_be32(f);
1007
    tp->t_oobflags = qemu_get_byte(f);
1008
    tp->t_iobc = qemu_get_byte(f);
1009
    tp->t_softerror = qemu_get_sbe16(f);
1010
    tp->snd_scale = qemu_get_byte(f);
1011
    tp->rcv_scale = qemu_get_byte(f);
1012
    tp->request_r_scale = qemu_get_byte(f);
1013
    tp->requested_s_scale = qemu_get_byte(f);
1014
    tp->ts_recent = qemu_get_be32(f);
1015
    tp->ts_recent_age = qemu_get_be32(f);
1016
    tp->last_ack_sent = qemu_get_be32(f);
1017
    tcp_template(tp);
1018
}
1019

    
1020
static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1021
{
1022
    uint32_t off, sb_cc, sb_datalen;
1023

    
1024
    sb_cc = qemu_get_be32(f);
1025
    sb_datalen = qemu_get_be32(f);
1026

    
1027
    sbreserve(sbuf, sb_datalen);
1028

    
1029
    if (sbuf->sb_datalen != sb_datalen)
1030
        return -ENOMEM;
1031

    
1032
    sbuf->sb_cc = sb_cc;
1033

    
1034
    off = qemu_get_sbe32(f);
1035
    sbuf->sb_wptr = sbuf->sb_data + off;
1036
    off = qemu_get_sbe32(f);
1037
    sbuf->sb_rptr = sbuf->sb_data + off;
1038
    qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1039

    
1040
    return 0;
1041
}
1042

    
1043
static int slirp_socket_load(QEMUFile *f, struct socket *so)
1044
{
1045
    if (tcp_attach(so) < 0)
1046
        return -ENOMEM;
1047

    
1048
    so->so_urgc = qemu_get_be32(f);
1049
    so->so_faddr.s_addr = qemu_get_be32(f);
1050
    so->so_laddr.s_addr = qemu_get_be32(f);
1051
    so->so_fport = qemu_get_be16(f);
1052
    so->so_lport = qemu_get_be16(f);
1053
    so->so_iptos = qemu_get_byte(f);
1054
    so->so_emu = qemu_get_byte(f);
1055
    so->so_type = qemu_get_byte(f);
1056
    so->so_state = qemu_get_be32(f);
1057
    if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1058
        return -ENOMEM;
1059
    if (slirp_sbuf_load(f, &so->so_snd) < 0)
1060
        return -ENOMEM;
1061
    slirp_tcp_load(f, so->so_tcpcb);
1062

    
1063
    return 0;
1064
}
1065

    
1066
static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1067
{
1068
    int i;
1069

    
1070
    for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1071
        slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1072
        qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1073
    }
1074
}
1075

    
1076
static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1077
{
1078
    Slirp *slirp = opaque;
1079
    struct ex_list *ex_ptr;
1080

    
1081
    while (qemu_get_byte(f)) {
1082
        int ret;
1083
        struct socket *so = socreate(slirp);
1084

    
1085
        if (!so)
1086
            return -ENOMEM;
1087

    
1088
        ret = slirp_socket_load(f, so);
1089

    
1090
        if (ret < 0)
1091
            return ret;
1092

    
1093
        if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1094
            slirp->vnetwork_addr.s_addr) {
1095
            return -EINVAL;
1096
        }
1097
        for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1098
            if (ex_ptr->ex_pty == 3 &&
1099
                so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1100
                so->so_fport == ex_ptr->ex_fport) {
1101
                break;
1102
            }
1103
        }
1104
        if (!ex_ptr)
1105
            return -EINVAL;
1106

    
1107
        so->extra = (void *)ex_ptr->ex_exec;
1108
    }
1109

    
1110
    if (version_id >= 2) {
1111
        slirp->ip_id = qemu_get_be16(f);
1112
    }
1113

    
1114
    if (version_id >= 3) {
1115
        slirp_bootp_load(f, slirp);
1116
    }
1117

    
1118
    return 0;
1119
}