crypto: add support for PBKDF2 algorithm
[qemu/ar7.git] / slirp / slirp.c
blob9ccf4157d882a336ac520c45541f4a9f1d10ded9
1 /*
2 * libslirp glue
4 * Copyright (c) 2004-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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"
32 /* host loopback address */
33 struct in_addr loopback_addr;
34 /* host loopback network mask */
35 unsigned long loopback_mask;
37 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
38 static const uint8_t special_ethaddr[ETH_ALEN] = {
39 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
42 u_int curtime;
44 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
45 QTAILQ_HEAD_INITIALIZER(slirp_instances);
47 static struct in_addr dns_addr;
48 static u_int dns_addr_time;
50 #define TIMEOUT_FAST 2 /* milliseconds */
51 #define TIMEOUT_SLOW 499 /* milliseconds */
52 /* for the aging of certain requests like DNS */
53 #define TIMEOUT_DEFAULT 1000 /* milliseconds */
55 #ifdef _WIN32
57 int get_dns_addr(struct in_addr *pdns_addr)
59 FIXED_INFO *FixedInfo=NULL;
60 ULONG BufLen;
61 DWORD ret;
62 IP_ADDR_STRING *pIPAddr;
63 struct in_addr tmp_addr;
65 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
66 *pdns_addr = dns_addr;
67 return 0;
70 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
71 BufLen = sizeof(FIXED_INFO);
73 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
74 if (FixedInfo) {
75 GlobalFree(FixedInfo);
76 FixedInfo = NULL;
78 FixedInfo = GlobalAlloc(GPTR, BufLen);
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;
87 return -1;
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;
99 return 0;
102 static void winsock_cleanup(void)
104 WSACleanup();
107 #else
109 static struct stat dns_addr_stat;
111 int get_dns_addr(struct in_addr *pdns_addr)
113 char buff[512];
114 char buff2[257];
115 FILE *f;
116 int found = 0;
117 struct in_addr tmp_addr;
119 if (dns_addr.s_addr != 0) {
120 struct stat old_stat;
121 if ((curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
122 *pdns_addr = dns_addr;
123 return 0;
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;
137 f = fopen("/etc/resolv.conf", "r");
138 if (!f)
139 return -1;
141 #ifdef DEBUG
142 fprintf(stderr, "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;
154 #ifdef DEBUG
155 else
156 fprintf(stderr, ", ");
157 #endif
158 if (++found > 3) {
159 #ifdef DEBUG
160 fprintf(stderr, "(more)");
161 #endif
162 break;
164 #ifdef DEBUG
165 else
166 fprintf(stderr, "%s", inet_ntoa(tmp_addr));
167 #endif
170 fclose(f);
171 if (!found)
172 return -1;
173 return 0;
176 #endif
178 static void slirp_init_once(void)
180 static int initialized;
181 #ifdef _WIN32
182 WSADATA Data;
183 #endif
185 if (initialized) {
186 return;
188 initialized = 1;
190 #ifdef _WIN32
191 WSAStartup(MAKEWORD(2,0), &Data);
192 atexit(winsock_cleanup);
193 #endif
195 loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
196 loopback_mask = htonl(IN_CLASSA_NET);
199 static void slirp_state_save(QEMUFile *f, void *opaque);
200 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
202 Slirp *slirp_init(int restricted, struct in_addr vnetwork,
203 struct in_addr vnetmask, struct in_addr vhost,
204 struct in6_addr vprefix_addr6, uint8_t vprefix_len,
205 struct in6_addr vhost6, const char *vhostname,
206 const char *tftp_path, const char *bootfile,
207 struct in_addr vdhcp_start, struct in_addr vnameserver,
208 struct in6_addr vnameserver6, const char **vdnssearch,
209 void *opaque)
211 Slirp *slirp = g_malloc0(sizeof(Slirp));
213 slirp_init_once();
215 slirp->grand = g_rand_new();
216 slirp->restricted = restricted;
218 if_init(slirp);
219 ip_init(slirp);
220 ip6_init(slirp);
222 /* Initialise mbufs *after* setting the MTU */
223 m_init(slirp);
225 slirp->vnetwork_addr = vnetwork;
226 slirp->vnetwork_mask = vnetmask;
227 slirp->vhost_addr = vhost;
228 slirp->vprefix_addr6 = vprefix_addr6;
229 slirp->vprefix_len = vprefix_len;
230 slirp->vhost_addr6 = vhost6;
231 if (vhostname) {
232 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
233 vhostname);
235 slirp->tftp_prefix = g_strdup(tftp_path);
236 slirp->bootp_filename = g_strdup(bootfile);
237 slirp->vdhcp_startaddr = vdhcp_start;
238 slirp->vnameserver_addr = vnameserver;
239 slirp->vnameserver_addr6 = vnameserver6;
241 if (vdnssearch) {
242 translate_dnssearch(slirp, vdnssearch);
245 slirp->opaque = opaque;
247 register_savevm(NULL, "slirp", 0, 4,
248 slirp_state_save, slirp_state_load, slirp);
250 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
252 return slirp;
255 void slirp_cleanup(Slirp *slirp)
257 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
259 unregister_savevm(NULL, "slirp", slirp);
261 ip_cleanup(slirp);
262 ip6_cleanup(slirp);
263 m_cleanup(slirp);
265 g_rand_free(slirp->grand);
267 g_free(slirp->vdnssearch);
268 g_free(slirp->tftp_prefix);
269 g_free(slirp->bootp_filename);
270 g_free(slirp);
273 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
274 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
276 static void slirp_update_timeout(uint32_t *timeout)
278 Slirp *slirp;
279 uint32_t t;
281 if (*timeout <= TIMEOUT_FAST) {
282 return;
285 t = MIN(1000, *timeout);
287 /* If we have tcp timeout with slirp, then we will fill @timeout with
288 * more precise value.
290 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
291 if (slirp->time_fasttimo) {
292 *timeout = TIMEOUT_FAST;
293 return;
295 if (slirp->do_slowtimo) {
296 t = MIN(TIMEOUT_SLOW, t);
299 *timeout = t;
302 void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
304 Slirp *slirp;
305 struct socket *so, *so_next;
307 if (QTAILQ_EMPTY(&slirp_instances)) {
308 return;
312 * First, TCP sockets
315 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
317 * *_slowtimo needs calling if there are IP fragments
318 * in the fragment queue, or there are TCP connections active
320 slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
321 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
323 for (so = slirp->tcb.so_next; so != &slirp->tcb;
324 so = so_next) {
325 int events = 0;
327 so_next = so->so_next;
329 so->pollfds_idx = -1;
332 * See if we need a tcp_fasttimo
334 if (slirp->time_fasttimo == 0 &&
335 so->so_tcpcb->t_flags & TF_DELACK) {
336 slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
340 * NOFDREF can include still connecting to local-host,
341 * newly socreated() sockets etc. Don't want to select these.
343 if (so->so_state & SS_NOFDREF || so->s == -1) {
344 continue;
348 * Set for reading sockets which are accepting
350 if (so->so_state & SS_FACCEPTCONN) {
351 GPollFD pfd = {
352 .fd = so->s,
353 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
355 so->pollfds_idx = pollfds->len;
356 g_array_append_val(pollfds, pfd);
357 continue;
361 * Set for writing sockets which are connecting
363 if (so->so_state & SS_ISFCONNECTING) {
364 GPollFD pfd = {
365 .fd = so->s,
366 .events = G_IO_OUT | G_IO_ERR,
368 so->pollfds_idx = pollfds->len;
369 g_array_append_val(pollfds, pfd);
370 continue;
374 * Set for writing if we are connected, can send more, and
375 * we have something to send
377 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
378 events |= G_IO_OUT | G_IO_ERR;
382 * Set for reading (and urgent data) if we are connected, can
383 * receive more, and we have room for it XXX /2 ?
385 if (CONN_CANFRCV(so) &&
386 (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
387 events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
390 if (events) {
391 GPollFD pfd = {
392 .fd = so->s,
393 .events = events,
395 so->pollfds_idx = pollfds->len;
396 g_array_append_val(pollfds, pfd);
401 * UDP sockets
403 for (so = slirp->udb.so_next; so != &slirp->udb;
404 so = so_next) {
405 so_next = so->so_next;
407 so->pollfds_idx = -1;
410 * See if it's timed out
412 if (so->so_expire) {
413 if (so->so_expire <= curtime) {
414 udp_detach(so);
415 continue;
416 } else {
417 slirp->do_slowtimo = true; /* Let socket expire */
422 * When UDP packets are received from over the
423 * link, they're sendto()'d straight away, so
424 * no need for setting for writing
425 * Limit the number of packets queued by this session
426 * to 4. Note that even though we try and limit this
427 * to 4 packets, the session could have more queued
428 * if the packets needed to be fragmented
429 * (XXX <= 4 ?)
431 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
432 GPollFD pfd = {
433 .fd = so->s,
434 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
436 so->pollfds_idx = pollfds->len;
437 g_array_append_val(pollfds, pfd);
442 * ICMP sockets
444 for (so = slirp->icmp.so_next; so != &slirp->icmp;
445 so = so_next) {
446 so_next = so->so_next;
448 so->pollfds_idx = -1;
451 * See if it's timed out
453 if (so->so_expire) {
454 if (so->so_expire <= curtime) {
455 icmp_detach(so);
456 continue;
457 } else {
458 slirp->do_slowtimo = true; /* Let socket expire */
462 if (so->so_state & SS_ISFCONNECTED) {
463 GPollFD pfd = {
464 .fd = so->s,
465 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
467 so->pollfds_idx = pollfds->len;
468 g_array_append_val(pollfds, pfd);
472 slirp_update_timeout(timeout);
475 void slirp_pollfds_poll(GArray *pollfds, int select_error)
477 Slirp *slirp;
478 struct socket *so, *so_next;
479 int ret;
481 if (QTAILQ_EMPTY(&slirp_instances)) {
482 return;
485 curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
487 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
489 * See if anything has timed out
491 if (slirp->time_fasttimo &&
492 ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) {
493 tcp_fasttimo(slirp);
494 slirp->time_fasttimo = 0;
496 if (slirp->do_slowtimo &&
497 ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
498 ip_slowtimo(slirp);
499 tcp_slowtimo(slirp);
500 slirp->last_slowtimo = curtime;
504 * Check sockets
506 if (!select_error) {
508 * Check TCP sockets
510 for (so = slirp->tcb.so_next; so != &slirp->tcb;
511 so = so_next) {
512 int revents;
514 so_next = so->so_next;
516 revents = 0;
517 if (so->pollfds_idx != -1) {
518 revents = g_array_index(pollfds, GPollFD,
519 so->pollfds_idx).revents;
522 if (so->so_state & SS_NOFDREF || so->s == -1) {
523 continue;
527 * Check for URG data
528 * This will soread as well, so no need to
529 * test for G_IO_IN below if this succeeds
531 if (revents & G_IO_PRI) {
532 sorecvoob(so);
535 * Check sockets for reading
537 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
539 * Check for incoming connections
541 if (so->so_state & SS_FACCEPTCONN) {
542 tcp_connect(so);
543 continue;
544 } /* else */
545 ret = soread(so);
547 /* Output it if we read something */
548 if (ret > 0) {
549 tcp_output(sototcpcb(so));
554 * Check sockets for writing
556 if (!(so->so_state & SS_NOFDREF) &&
557 (revents & (G_IO_OUT | G_IO_ERR))) {
559 * Check for non-blocking, still-connecting sockets
561 if (so->so_state & SS_ISFCONNECTING) {
562 /* Connected */
563 so->so_state &= ~SS_ISFCONNECTING;
565 ret = send(so->s, (const void *) &ret, 0, 0);
566 if (ret < 0) {
567 /* XXXXX Must fix, zero bytes is a NOP */
568 if (errno == EAGAIN || errno == EWOULDBLOCK ||
569 errno == EINPROGRESS || errno == ENOTCONN) {
570 continue;
573 /* else failed */
574 so->so_state &= SS_PERSISTENT_MASK;
575 so->so_state |= SS_NOFDREF;
577 /* else so->so_state &= ~SS_ISFCONNECTING; */
580 * Continue tcp_input
582 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
583 so->so_ffamily);
584 /* continue; */
585 } else {
586 ret = sowrite(so);
589 * XXXXX If we wrote something (a lot), there
590 * could be a need for a window update.
591 * In the worst case, the remote will send
592 * a window probe to get things going again
597 * Probe a still-connecting, non-blocking socket
598 * to check if it's still alive
600 #ifdef PROBE_CONN
601 if (so->so_state & SS_ISFCONNECTING) {
602 ret = qemu_recv(so->s, &ret, 0, 0);
604 if (ret < 0) {
605 /* XXX */
606 if (errno == EAGAIN || errno == EWOULDBLOCK ||
607 errno == EINPROGRESS || errno == ENOTCONN) {
608 continue; /* Still connecting, continue */
611 /* else failed */
612 so->so_state &= SS_PERSISTENT_MASK;
613 so->so_state |= SS_NOFDREF;
615 /* tcp_input will take care of it */
616 } else {
617 ret = send(so->s, &ret, 0, 0);
618 if (ret < 0) {
619 /* XXX */
620 if (errno == EAGAIN || errno == EWOULDBLOCK ||
621 errno == EINPROGRESS || errno == ENOTCONN) {
622 continue;
624 /* else failed */
625 so->so_state &= SS_PERSISTENT_MASK;
626 so->so_state |= SS_NOFDREF;
627 } else {
628 so->so_state &= ~SS_ISFCONNECTING;
632 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
633 so->so_ffamily);
634 } /* SS_ISFCONNECTING */
635 #endif
639 * Now UDP sockets.
640 * Incoming packets are sent straight away, they're not buffered.
641 * Incoming UDP data isn't buffered either.
643 for (so = slirp->udb.so_next; so != &slirp->udb;
644 so = so_next) {
645 int revents;
647 so_next = so->so_next;
649 revents = 0;
650 if (so->pollfds_idx != -1) {
651 revents = g_array_index(pollfds, GPollFD,
652 so->pollfds_idx).revents;
655 if (so->s != -1 &&
656 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
657 sorecvfrom(so);
662 * Check incoming ICMP relies.
664 for (so = slirp->icmp.so_next; so != &slirp->icmp;
665 so = so_next) {
666 int revents;
668 so_next = so->so_next;
670 revents = 0;
671 if (so->pollfds_idx != -1) {
672 revents = g_array_index(pollfds, GPollFD,
673 so->pollfds_idx).revents;
676 if (so->s != -1 &&
677 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
678 icmp_receive(so);
683 if_start(slirp);
687 static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
689 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
690 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
691 struct ethhdr *reh = (struct ethhdr *)arp_reply;
692 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
693 int ar_op;
694 struct ex_list *ex_ptr;
696 ar_op = ntohs(ah->ar_op);
697 switch(ar_op) {
698 case ARPOP_REQUEST:
699 if (ah->ar_tip == ah->ar_sip) {
700 /* Gratuitous ARP */
701 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
702 return;
705 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
706 slirp->vnetwork_addr.s_addr) {
707 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
708 ah->ar_tip == slirp->vhost_addr.s_addr)
709 goto arp_ok;
710 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
711 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
712 goto arp_ok;
714 return;
715 arp_ok:
716 memset(arp_reply, 0, sizeof(arp_reply));
718 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
720 /* ARP request for alias/dns mac address */
721 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
722 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
723 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
724 reh->h_proto = htons(ETH_P_ARP);
726 rah->ar_hrd = htons(1);
727 rah->ar_pro = htons(ETH_P_IP);
728 rah->ar_hln = ETH_ALEN;
729 rah->ar_pln = 4;
730 rah->ar_op = htons(ARPOP_REPLY);
731 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
732 rah->ar_sip = ah->ar_tip;
733 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
734 rah->ar_tip = ah->ar_sip;
735 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
737 break;
738 case ARPOP_REPLY:
739 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
740 break;
741 default:
742 break;
746 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
748 struct mbuf *m;
749 int proto;
751 if (pkt_len < ETH_HLEN)
752 return;
754 proto = ntohs(*(uint16_t *)(pkt + 12));
755 switch(proto) {
756 case ETH_P_ARP:
757 arp_input(slirp, pkt, pkt_len);
758 break;
759 case ETH_P_IP:
760 case ETH_P_IPV6:
761 m = m_get(slirp);
762 if (!m)
763 return;
764 /* Note: we add 2 to align the IP header on 4 bytes,
765 * and add the margin for the tcpiphdr overhead */
766 if (M_FREEROOM(m) < pkt_len + TCPIPHDR_DELTA + 2) {
767 m_inc(m, pkt_len + TCPIPHDR_DELTA + 2);
769 m->m_len = pkt_len + TCPIPHDR_DELTA + 2;
770 memcpy(m->m_data + TCPIPHDR_DELTA + 2, pkt, pkt_len);
772 m->m_data += TCPIPHDR_DELTA + 2 + ETH_HLEN;
773 m->m_len -= TCPIPHDR_DELTA + 2 + ETH_HLEN;
775 if (proto == ETH_P_IP) {
776 ip_input(m);
777 } else if (proto == ETH_P_IPV6) {
778 ip6_input(m);
780 break;
782 default:
783 break;
787 /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no
788 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
789 * is ready to go.
791 static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
792 uint8_t ethaddr[ETH_ALEN])
794 const struct ip *iph = (const struct ip *)ifm->m_data;
796 if (iph->ip_dst.s_addr == 0) {
797 /* 0.0.0.0 can not be a destination address, something went wrong,
798 * avoid making it worse */
799 return 1;
801 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
802 uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
803 struct ethhdr *reh = (struct ethhdr *)arp_req;
804 struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
806 if (!ifm->resolution_requested) {
807 /* If the client addr is not known, send an ARP request */
808 memset(reh->h_dest, 0xff, ETH_ALEN);
809 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
810 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
811 reh->h_proto = htons(ETH_P_ARP);
812 rah->ar_hrd = htons(1);
813 rah->ar_pro = htons(ETH_P_IP);
814 rah->ar_hln = ETH_ALEN;
815 rah->ar_pln = 4;
816 rah->ar_op = htons(ARPOP_REQUEST);
818 /* source hw addr */
819 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
820 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
822 /* source IP */
823 rah->ar_sip = slirp->vhost_addr.s_addr;
825 /* target hw addr (none) */
826 memset(rah->ar_tha, 0, ETH_ALEN);
828 /* target IP */
829 rah->ar_tip = iph->ip_dst.s_addr;
830 slirp->client_ipaddr = iph->ip_dst;
831 slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
832 ifm->resolution_requested = true;
834 /* Expire request and drop outgoing packet after 1 second */
835 ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
837 return 0;
838 } else {
839 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
840 /* XXX: not correct */
841 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
842 eh->h_proto = htons(ETH_P_IP);
844 /* Send this */
845 return 2;
849 /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no
850 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
851 * is ready to go.
853 static int if_encap6(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
854 uint8_t ethaddr[ETH_ALEN])
856 const struct ip6 *ip6h = mtod(ifm, const struct ip6 *);
857 if (!ndp_table_search(slirp, ip6h->ip_dst, ethaddr)) {
858 if (!ifm->resolution_requested) {
859 ndp_send_ns(slirp, ip6h->ip_dst);
860 ifm->resolution_requested = true;
861 ifm->expiration_date =
862 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
864 return 0;
865 } else {
866 eh->h_proto = htons(ETH_P_IPV6);
867 in6_compute_ethaddr(ip6h->ip_src, eh->h_source);
869 /* Send this */
870 return 2;
874 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
875 * re-queued.
877 int if_encap(Slirp *slirp, struct mbuf *ifm)
879 uint8_t buf[1600];
880 struct ethhdr *eh = (struct ethhdr *)buf;
881 uint8_t ethaddr[ETH_ALEN];
882 const struct ip *iph = (const struct ip *)ifm->m_data;
883 int ret;
885 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
886 return 1;
889 switch (iph->ip_v) {
890 case IPVERSION:
891 ret = if_encap4(slirp, ifm, eh, ethaddr);
892 if (ret < 2) {
893 return ret;
895 break;
897 case IP6VERSION:
898 ret = if_encap6(slirp, ifm, eh, ethaddr);
899 if (ret < 2) {
900 return ret;
902 break;
904 default:
905 g_assert_not_reached();
906 break;
909 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
910 DEBUG_ARGS((dfd, " src = %02x:%02x:%02x:%02x:%02x:%02x\n",
911 eh->h_source[0], eh->h_source[1], eh->h_source[2],
912 eh->h_source[3], eh->h_source[4], eh->h_source[5]));
913 DEBUG_ARGS((dfd, " dst = %02x:%02x:%02x:%02x:%02x:%02x\n",
914 eh->h_dest[0], eh->h_dest[1], eh->h_dest[2],
915 eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]));
916 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
917 slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
918 return 1;
921 /* Drop host forwarding rule, return 0 if found. */
922 int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
923 int host_port)
925 struct socket *so;
926 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
927 struct sockaddr_in addr;
928 int port = htons(host_port);
929 socklen_t addr_len;
931 for (so = head->so_next; so != head; so = so->so_next) {
932 addr_len = sizeof(addr);
933 if ((so->so_state & SS_HOSTFWD) &&
934 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
935 addr.sin_addr.s_addr == host_addr.s_addr &&
936 addr.sin_port == port) {
937 close(so->s);
938 sofree(so);
939 return 0;
943 return -1;
946 int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
947 int host_port, struct in_addr guest_addr, int guest_port)
949 if (!guest_addr.s_addr) {
950 guest_addr = slirp->vdhcp_startaddr;
952 if (is_udp) {
953 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
954 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
955 return -1;
956 } else {
957 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
958 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
959 return -1;
961 return 0;
964 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
965 struct in_addr *guest_addr, int guest_port)
967 if (!guest_addr->s_addr) {
968 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
969 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
971 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
972 slirp->vnetwork_addr.s_addr ||
973 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
974 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
975 return -1;
977 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
978 htons(guest_port));
981 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
983 if (so->s == -1 && so->extra) {
984 qemu_chr_fe_write(so->extra, buf, len);
985 return len;
988 return send(so->s, buf, len, flags);
991 static struct socket *
992 slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
994 struct socket *so;
996 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
997 if (so->so_faddr.s_addr == guest_addr.s_addr &&
998 htons(so->so_fport) == guest_port) {
999 return so;
1002 return NULL;
1005 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
1006 int guest_port)
1008 struct iovec iov[2];
1009 struct socket *so;
1011 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1013 if (!so || so->so_state & SS_NOFDREF) {
1014 return 0;
1017 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) {
1018 return 0;
1021 return sopreprbuf(so, iov, NULL);
1024 void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
1025 const uint8_t *buf, int size)
1027 int ret;
1028 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1030 if (!so)
1031 return;
1033 ret = soreadbuf(so, (const char *)buf, size);
1035 if (ret > 0)
1036 tcp_output(sototcpcb(so));
1039 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
1041 int i;
1043 qemu_put_sbe16(f, tp->t_state);
1044 for (i = 0; i < TCPT_NTIMERS; i++)
1045 qemu_put_sbe16(f, tp->t_timer[i]);
1046 qemu_put_sbe16(f, tp->t_rxtshift);
1047 qemu_put_sbe16(f, tp->t_rxtcur);
1048 qemu_put_sbe16(f, tp->t_dupacks);
1049 qemu_put_be16(f, tp->t_maxseg);
1050 qemu_put_sbyte(f, tp->t_force);
1051 qemu_put_be16(f, tp->t_flags);
1052 qemu_put_be32(f, tp->snd_una);
1053 qemu_put_be32(f, tp->snd_nxt);
1054 qemu_put_be32(f, tp->snd_up);
1055 qemu_put_be32(f, tp->snd_wl1);
1056 qemu_put_be32(f, tp->snd_wl2);
1057 qemu_put_be32(f, tp->iss);
1058 qemu_put_be32(f, tp->snd_wnd);
1059 qemu_put_be32(f, tp->rcv_wnd);
1060 qemu_put_be32(f, tp->rcv_nxt);
1061 qemu_put_be32(f, tp->rcv_up);
1062 qemu_put_be32(f, tp->irs);
1063 qemu_put_be32(f, tp->rcv_adv);
1064 qemu_put_be32(f, tp->snd_max);
1065 qemu_put_be32(f, tp->snd_cwnd);
1066 qemu_put_be32(f, tp->snd_ssthresh);
1067 qemu_put_sbe16(f, tp->t_idle);
1068 qemu_put_sbe16(f, tp->t_rtt);
1069 qemu_put_be32(f, tp->t_rtseq);
1070 qemu_put_sbe16(f, tp->t_srtt);
1071 qemu_put_sbe16(f, tp->t_rttvar);
1072 qemu_put_be16(f, tp->t_rttmin);
1073 qemu_put_be32(f, tp->max_sndwnd);
1074 qemu_put_byte(f, tp->t_oobflags);
1075 qemu_put_byte(f, tp->t_iobc);
1076 qemu_put_sbe16(f, tp->t_softerror);
1077 qemu_put_byte(f, tp->snd_scale);
1078 qemu_put_byte(f, tp->rcv_scale);
1079 qemu_put_byte(f, tp->request_r_scale);
1080 qemu_put_byte(f, tp->requested_s_scale);
1081 qemu_put_be32(f, tp->ts_recent);
1082 qemu_put_be32(f, tp->ts_recent_age);
1083 qemu_put_be32(f, tp->last_ack_sent);
1086 static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
1088 uint32_t off;
1090 qemu_put_be32(f, sbuf->sb_cc);
1091 qemu_put_be32(f, sbuf->sb_datalen);
1092 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
1093 qemu_put_sbe32(f, off);
1094 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
1095 qemu_put_sbe32(f, off);
1096 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1099 static void slirp_socket_save(QEMUFile *f, struct socket *so)
1101 qemu_put_be32(f, so->so_urgc);
1102 qemu_put_be16(f, so->so_ffamily);
1103 switch (so->so_ffamily) {
1104 case AF_INET:
1105 qemu_put_be32(f, so->so_faddr.s_addr);
1106 qemu_put_be16(f, so->so_fport);
1107 break;
1108 default:
1109 error_report(
1110 "so_ffamily unknown, unable to save so_faddr and so_fport\n");
1112 qemu_put_be16(f, so->so_lfamily);
1113 switch (so->so_lfamily) {
1114 case AF_INET:
1115 qemu_put_be32(f, so->so_laddr.s_addr);
1116 qemu_put_be16(f, so->so_lport);
1117 break;
1118 default:
1119 error_report(
1120 "so_ffamily unknown, unable to save so_laddr and so_lport\n");
1122 qemu_put_byte(f, so->so_iptos);
1123 qemu_put_byte(f, so->so_emu);
1124 qemu_put_byte(f, so->so_type);
1125 qemu_put_be32(f, so->so_state);
1126 slirp_sbuf_save(f, &so->so_rcv);
1127 slirp_sbuf_save(f, &so->so_snd);
1128 slirp_tcp_save(f, so->so_tcpcb);
1131 static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
1133 int i;
1135 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1136 qemu_put_be16(f, slirp->bootp_clients[i].allocated);
1137 qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1141 static void slirp_state_save(QEMUFile *f, void *opaque)
1143 Slirp *slirp = opaque;
1144 struct ex_list *ex_ptr;
1146 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1147 if (ex_ptr->ex_pty == 3) {
1148 struct socket *so;
1149 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
1150 ntohs(ex_ptr->ex_fport));
1151 if (!so)
1152 continue;
1154 qemu_put_byte(f, 42);
1155 slirp_socket_save(f, so);
1157 qemu_put_byte(f, 0);
1159 qemu_put_be16(f, slirp->ip_id);
1161 slirp_bootp_save(f, slirp);
1164 static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
1166 int i;
1168 tp->t_state = qemu_get_sbe16(f);
1169 for (i = 0; i < TCPT_NTIMERS; i++)
1170 tp->t_timer[i] = qemu_get_sbe16(f);
1171 tp->t_rxtshift = qemu_get_sbe16(f);
1172 tp->t_rxtcur = qemu_get_sbe16(f);
1173 tp->t_dupacks = qemu_get_sbe16(f);
1174 tp->t_maxseg = qemu_get_be16(f);
1175 tp->t_force = qemu_get_sbyte(f);
1176 tp->t_flags = qemu_get_be16(f);
1177 tp->snd_una = qemu_get_be32(f);
1178 tp->snd_nxt = qemu_get_be32(f);
1179 tp->snd_up = qemu_get_be32(f);
1180 tp->snd_wl1 = qemu_get_be32(f);
1181 tp->snd_wl2 = qemu_get_be32(f);
1182 tp->iss = qemu_get_be32(f);
1183 tp->snd_wnd = qemu_get_be32(f);
1184 tp->rcv_wnd = qemu_get_be32(f);
1185 tp->rcv_nxt = qemu_get_be32(f);
1186 tp->rcv_up = qemu_get_be32(f);
1187 tp->irs = qemu_get_be32(f);
1188 tp->rcv_adv = qemu_get_be32(f);
1189 tp->snd_max = qemu_get_be32(f);
1190 tp->snd_cwnd = qemu_get_be32(f);
1191 tp->snd_ssthresh = qemu_get_be32(f);
1192 tp->t_idle = qemu_get_sbe16(f);
1193 tp->t_rtt = qemu_get_sbe16(f);
1194 tp->t_rtseq = qemu_get_be32(f);
1195 tp->t_srtt = qemu_get_sbe16(f);
1196 tp->t_rttvar = qemu_get_sbe16(f);
1197 tp->t_rttmin = qemu_get_be16(f);
1198 tp->max_sndwnd = qemu_get_be32(f);
1199 tp->t_oobflags = qemu_get_byte(f);
1200 tp->t_iobc = qemu_get_byte(f);
1201 tp->t_softerror = qemu_get_sbe16(f);
1202 tp->snd_scale = qemu_get_byte(f);
1203 tp->rcv_scale = qemu_get_byte(f);
1204 tp->request_r_scale = qemu_get_byte(f);
1205 tp->requested_s_scale = qemu_get_byte(f);
1206 tp->ts_recent = qemu_get_be32(f);
1207 tp->ts_recent_age = qemu_get_be32(f);
1208 tp->last_ack_sent = qemu_get_be32(f);
1209 tcp_template(tp);
1212 static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1214 uint32_t off, sb_cc, sb_datalen;
1216 sb_cc = qemu_get_be32(f);
1217 sb_datalen = qemu_get_be32(f);
1219 sbreserve(sbuf, sb_datalen);
1221 if (sbuf->sb_datalen != sb_datalen)
1222 return -ENOMEM;
1224 sbuf->sb_cc = sb_cc;
1226 off = qemu_get_sbe32(f);
1227 sbuf->sb_wptr = sbuf->sb_data + off;
1228 off = qemu_get_sbe32(f);
1229 sbuf->sb_rptr = sbuf->sb_data + off;
1230 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1232 return 0;
1235 static int slirp_socket_load(QEMUFile *f, struct socket *so)
1237 if (tcp_attach(so) < 0)
1238 return -ENOMEM;
1240 so->so_urgc = qemu_get_be32(f);
1241 so->so_ffamily = qemu_get_be16(f);
1242 switch (so->so_ffamily) {
1243 case AF_INET:
1244 so->so_faddr.s_addr = qemu_get_be32(f);
1245 so->so_fport = qemu_get_be16(f);
1246 break;
1247 default:
1248 error_report(
1249 "so_ffamily unknown, unable to restore so_faddr and so_lport\n");
1251 so->so_lfamily = qemu_get_be16(f);
1252 switch (so->so_lfamily) {
1253 case AF_INET:
1254 so->so_laddr.s_addr = qemu_get_be32(f);
1255 so->so_lport = qemu_get_be16(f);
1256 break;
1257 default:
1258 error_report(
1259 "so_ffamily unknown, unable to restore so_laddr and so_lport\n");
1261 so->so_iptos = qemu_get_byte(f);
1262 so->so_emu = qemu_get_byte(f);
1263 so->so_type = qemu_get_byte(f);
1264 so->so_state = qemu_get_be32(f);
1265 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1266 return -ENOMEM;
1267 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1268 return -ENOMEM;
1269 slirp_tcp_load(f, so->so_tcpcb);
1271 return 0;
1274 static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1276 int i;
1278 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1279 slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1280 qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1284 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1286 Slirp *slirp = opaque;
1287 struct ex_list *ex_ptr;
1289 while (qemu_get_byte(f)) {
1290 int ret;
1291 struct socket *so = socreate(slirp);
1293 if (!so)
1294 return -ENOMEM;
1296 ret = slirp_socket_load(f, so);
1298 if (ret < 0)
1299 return ret;
1301 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1302 slirp->vnetwork_addr.s_addr) {
1303 return -EINVAL;
1305 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1306 if (ex_ptr->ex_pty == 3 &&
1307 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1308 so->so_fport == ex_ptr->ex_fport) {
1309 break;
1312 if (!ex_ptr)
1313 return -EINVAL;
1315 so->extra = (void *)ex_ptr->ex_exec;
1318 if (version_id >= 2) {
1319 slirp->ip_id = qemu_get_be16(f);
1322 if (version_id >= 3) {
1323 slirp_bootp_load(f, slirp);
1326 return 0;