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