slirp: Make Socket structure IPv6 compatible
[qemu.git] / slirp / slirp.c
blobb900775effd6cb78cc490e599c7a87389405012b
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-common.h"
25 #include "qemu/timer.h"
26 #include "qemu/error-report.h"
27 #include "sysemu/char.h"
28 #include "slirp.h"
29 #include "hw/hw.h"
31 /* host loopback address */
32 struct in_addr loopback_addr;
33 /* host loopback network mask */
34 unsigned long loopback_mask;
36 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
37 static const uint8_t special_ethaddr[ETH_ALEN] = {
38 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
41 u_int curtime;
43 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
44 QTAILQ_HEAD_INITIALIZER(slirp_instances);
46 static struct in_addr dns_addr;
47 static u_int dns_addr_time;
49 #define TIMEOUT_FAST 2 /* milliseconds */
50 #define TIMEOUT_SLOW 499 /* milliseconds */
51 /* for the aging of certain requests like DNS */
52 #define TIMEOUT_DEFAULT 1000 /* milliseconds */
54 #ifdef _WIN32
56 int get_dns_addr(struct in_addr *pdns_addr)
58 FIXED_INFO *FixedInfo=NULL;
59 ULONG BufLen;
60 DWORD ret;
61 IP_ADDR_STRING *pIPAddr;
62 struct in_addr tmp_addr;
64 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
65 *pdns_addr = dns_addr;
66 return 0;
69 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
70 BufLen = sizeof(FIXED_INFO);
72 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
73 if (FixedInfo) {
74 GlobalFree(FixedInfo);
75 FixedInfo = NULL;
77 FixedInfo = GlobalAlloc(GPTR, BufLen);
80 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
81 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
82 if (FixedInfo) {
83 GlobalFree(FixedInfo);
84 FixedInfo = NULL;
86 return -1;
89 pIPAddr = &(FixedInfo->DnsServerList);
90 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
91 *pdns_addr = tmp_addr;
92 dns_addr = tmp_addr;
93 dns_addr_time = curtime;
94 if (FixedInfo) {
95 GlobalFree(FixedInfo);
96 FixedInfo = NULL;
98 return 0;
101 static void winsock_cleanup(void)
103 WSACleanup();
106 #else
108 static struct stat dns_addr_stat;
110 int get_dns_addr(struct in_addr *pdns_addr)
112 char buff[512];
113 char buff2[257];
114 FILE *f;
115 int found = 0;
116 struct in_addr tmp_addr;
118 if (dns_addr.s_addr != 0) {
119 struct stat old_stat;
120 if ((curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
121 *pdns_addr = dns_addr;
122 return 0;
124 old_stat = dns_addr_stat;
125 if (stat("/etc/resolv.conf", &dns_addr_stat) != 0)
126 return -1;
127 if ((dns_addr_stat.st_dev == old_stat.st_dev)
128 && (dns_addr_stat.st_ino == old_stat.st_ino)
129 && (dns_addr_stat.st_size == old_stat.st_size)
130 && (dns_addr_stat.st_mtime == old_stat.st_mtime)) {
131 *pdns_addr = dns_addr;
132 return 0;
136 f = fopen("/etc/resolv.conf", "r");
137 if (!f)
138 return -1;
140 #ifdef DEBUG
141 fprintf(stderr, "IP address of your DNS(s): ");
142 #endif
143 while (fgets(buff, 512, f) != NULL) {
144 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
145 if (!inet_aton(buff2, &tmp_addr))
146 continue;
147 /* If it's the first one, set it to dns_addr */
148 if (!found) {
149 *pdns_addr = tmp_addr;
150 dns_addr = tmp_addr;
151 dns_addr_time = curtime;
153 #ifdef DEBUG
154 else
155 fprintf(stderr, ", ");
156 #endif
157 if (++found > 3) {
158 #ifdef DEBUG
159 fprintf(stderr, "(more)");
160 #endif
161 break;
163 #ifdef DEBUG
164 else
165 fprintf(stderr, "%s", inet_ntoa(tmp_addr));
166 #endif
169 fclose(f);
170 if (!found)
171 return -1;
172 return 0;
175 #endif
177 static void slirp_init_once(void)
179 static int initialized;
180 #ifdef _WIN32
181 WSADATA Data;
182 #endif
184 if (initialized) {
185 return;
187 initialized = 1;
189 #ifdef _WIN32
190 WSAStartup(MAKEWORD(2,0), &Data);
191 atexit(winsock_cleanup);
192 #endif
194 loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
195 loopback_mask = htonl(IN_CLASSA_NET);
198 static void slirp_state_save(QEMUFile *f, void *opaque);
199 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
201 Slirp *slirp_init(int restricted, struct in_addr vnetwork,
202 struct in_addr vnetmask, struct in_addr vhost,
203 const char *vhostname, const char *tftp_path,
204 const char *bootfile, struct in_addr vdhcp_start,
205 struct in_addr vnameserver, const char **vdnssearch,
206 void *opaque)
208 Slirp *slirp = g_malloc0(sizeof(Slirp));
210 slirp_init_once();
212 slirp->restricted = restricted;
214 if_init(slirp);
215 ip_init(slirp);
217 /* Initialise mbufs *after* setting the MTU */
218 m_init(slirp);
220 slirp->vnetwork_addr = vnetwork;
221 slirp->vnetwork_mask = vnetmask;
222 slirp->vhost_addr = vhost;
223 if (vhostname) {
224 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
225 vhostname);
227 slirp->tftp_prefix = g_strdup(tftp_path);
228 slirp->bootp_filename = g_strdup(bootfile);
229 slirp->vdhcp_startaddr = vdhcp_start;
230 slirp->vnameserver_addr = vnameserver;
232 if (vdnssearch) {
233 translate_dnssearch(slirp, vdnssearch);
236 slirp->opaque = opaque;
238 register_savevm(NULL, "slirp", 0, 4,
239 slirp_state_save, slirp_state_load, slirp);
241 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
243 return slirp;
246 void slirp_cleanup(Slirp *slirp)
248 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
250 unregister_savevm(NULL, "slirp", slirp);
252 ip_cleanup(slirp);
253 m_cleanup(slirp);
255 g_free(slirp->vdnssearch);
256 g_free(slirp->tftp_prefix);
257 g_free(slirp->bootp_filename);
258 g_free(slirp);
261 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
262 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
264 static void slirp_update_timeout(uint32_t *timeout)
266 Slirp *slirp;
267 uint32_t t;
269 if (*timeout <= TIMEOUT_FAST) {
270 return;
273 t = MIN(1000, *timeout);
275 /* If we have tcp timeout with slirp, then we will fill @timeout with
276 * more precise value.
278 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
279 if (slirp->time_fasttimo) {
280 *timeout = TIMEOUT_FAST;
281 return;
283 if (slirp->do_slowtimo) {
284 t = MIN(TIMEOUT_SLOW, t);
287 *timeout = t;
290 void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
292 Slirp *slirp;
293 struct socket *so, *so_next;
295 if (QTAILQ_EMPTY(&slirp_instances)) {
296 return;
300 * First, TCP sockets
303 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
305 * *_slowtimo needs calling if there are IP fragments
306 * in the fragment queue, or there are TCP connections active
308 slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
309 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
311 for (so = slirp->tcb.so_next; so != &slirp->tcb;
312 so = so_next) {
313 int events = 0;
315 so_next = so->so_next;
317 so->pollfds_idx = -1;
320 * See if we need a tcp_fasttimo
322 if (slirp->time_fasttimo == 0 &&
323 so->so_tcpcb->t_flags & TF_DELACK) {
324 slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
328 * NOFDREF can include still connecting to local-host,
329 * newly socreated() sockets etc. Don't want to select these.
331 if (so->so_state & SS_NOFDREF || so->s == -1) {
332 continue;
336 * Set for reading sockets which are accepting
338 if (so->so_state & SS_FACCEPTCONN) {
339 GPollFD pfd = {
340 .fd = so->s,
341 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
343 so->pollfds_idx = pollfds->len;
344 g_array_append_val(pollfds, pfd);
345 continue;
349 * Set for writing sockets which are connecting
351 if (so->so_state & SS_ISFCONNECTING) {
352 GPollFD pfd = {
353 .fd = so->s,
354 .events = G_IO_OUT | G_IO_ERR,
356 so->pollfds_idx = pollfds->len;
357 g_array_append_val(pollfds, pfd);
358 continue;
362 * Set for writing if we are connected, can send more, and
363 * we have something to send
365 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
366 events |= G_IO_OUT | G_IO_ERR;
370 * Set for reading (and urgent data) if we are connected, can
371 * receive more, and we have room for it XXX /2 ?
373 if (CONN_CANFRCV(so) &&
374 (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
375 events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
378 if (events) {
379 GPollFD pfd = {
380 .fd = so->s,
381 .events = events,
383 so->pollfds_idx = pollfds->len;
384 g_array_append_val(pollfds, pfd);
389 * UDP sockets
391 for (so = slirp->udb.so_next; so != &slirp->udb;
392 so = so_next) {
393 so_next = so->so_next;
395 so->pollfds_idx = -1;
398 * See if it's timed out
400 if (so->so_expire) {
401 if (so->so_expire <= curtime) {
402 udp_detach(so);
403 continue;
404 } else {
405 slirp->do_slowtimo = true; /* Let socket expire */
410 * When UDP packets are received from over the
411 * link, they're sendto()'d straight away, so
412 * no need for setting for writing
413 * Limit the number of packets queued by this session
414 * to 4. Note that even though we try and limit this
415 * to 4 packets, the session could have more queued
416 * if the packets needed to be fragmented
417 * (XXX <= 4 ?)
419 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
420 GPollFD pfd = {
421 .fd = so->s,
422 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
424 so->pollfds_idx = pollfds->len;
425 g_array_append_val(pollfds, pfd);
430 * ICMP sockets
432 for (so = slirp->icmp.so_next; so != &slirp->icmp;
433 so = so_next) {
434 so_next = so->so_next;
436 so->pollfds_idx = -1;
439 * See if it's timed out
441 if (so->so_expire) {
442 if (so->so_expire <= curtime) {
443 icmp_detach(so);
444 continue;
445 } else {
446 slirp->do_slowtimo = true; /* Let socket expire */
450 if (so->so_state & SS_ISFCONNECTED) {
451 GPollFD pfd = {
452 .fd = so->s,
453 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
455 so->pollfds_idx = pollfds->len;
456 g_array_append_val(pollfds, pfd);
460 slirp_update_timeout(timeout);
463 void slirp_pollfds_poll(GArray *pollfds, int select_error)
465 Slirp *slirp;
466 struct socket *so, *so_next;
467 int ret;
469 if (QTAILQ_EMPTY(&slirp_instances)) {
470 return;
473 curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
475 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
477 * See if anything has timed out
479 if (slirp->time_fasttimo &&
480 ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) {
481 tcp_fasttimo(slirp);
482 slirp->time_fasttimo = 0;
484 if (slirp->do_slowtimo &&
485 ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
486 ip_slowtimo(slirp);
487 tcp_slowtimo(slirp);
488 slirp->last_slowtimo = curtime;
492 * Check sockets
494 if (!select_error) {
496 * Check TCP sockets
498 for (so = slirp->tcb.so_next; so != &slirp->tcb;
499 so = so_next) {
500 int revents;
502 so_next = so->so_next;
504 revents = 0;
505 if (so->pollfds_idx != -1) {
506 revents = g_array_index(pollfds, GPollFD,
507 so->pollfds_idx).revents;
510 if (so->so_state & SS_NOFDREF || so->s == -1) {
511 continue;
515 * Check for URG data
516 * This will soread as well, so no need to
517 * test for G_IO_IN below if this succeeds
519 if (revents & G_IO_PRI) {
520 sorecvoob(so);
523 * Check sockets for reading
525 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
527 * Check for incoming connections
529 if (so->so_state & SS_FACCEPTCONN) {
530 tcp_connect(so);
531 continue;
532 } /* else */
533 ret = soread(so);
535 /* Output it if we read something */
536 if (ret > 0) {
537 tcp_output(sototcpcb(so));
542 * Check sockets for writing
544 if (!(so->so_state & SS_NOFDREF) &&
545 (revents & (G_IO_OUT | G_IO_ERR))) {
547 * Check for non-blocking, still-connecting sockets
549 if (so->so_state & SS_ISFCONNECTING) {
550 /* Connected */
551 so->so_state &= ~SS_ISFCONNECTING;
553 ret = send(so->s, (const void *) &ret, 0, 0);
554 if (ret < 0) {
555 /* XXXXX Must fix, zero bytes is a NOP */
556 if (errno == EAGAIN || errno == EWOULDBLOCK ||
557 errno == EINPROGRESS || errno == ENOTCONN) {
558 continue;
561 /* else failed */
562 so->so_state &= SS_PERSISTENT_MASK;
563 so->so_state |= SS_NOFDREF;
565 /* else so->so_state &= ~SS_ISFCONNECTING; */
568 * Continue tcp_input
570 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
571 /* continue; */
572 } else {
573 ret = sowrite(so);
576 * XXXXX If we wrote something (a lot), there
577 * could be a need for a window update.
578 * In the worst case, the remote will send
579 * a window probe to get things going again
584 * Probe a still-connecting, non-blocking socket
585 * to check if it's still alive
587 #ifdef PROBE_CONN
588 if (so->so_state & SS_ISFCONNECTING) {
589 ret = qemu_recv(so->s, &ret, 0, 0);
591 if (ret < 0) {
592 /* XXX */
593 if (errno == EAGAIN || errno == EWOULDBLOCK ||
594 errno == EINPROGRESS || errno == ENOTCONN) {
595 continue; /* Still connecting, continue */
598 /* else failed */
599 so->so_state &= SS_PERSISTENT_MASK;
600 so->so_state |= SS_NOFDREF;
602 /* tcp_input will take care of it */
603 } else {
604 ret = send(so->s, &ret, 0, 0);
605 if (ret < 0) {
606 /* XXX */
607 if (errno == EAGAIN || errno == EWOULDBLOCK ||
608 errno == EINPROGRESS || errno == ENOTCONN) {
609 continue;
611 /* else failed */
612 so->so_state &= SS_PERSISTENT_MASK;
613 so->so_state |= SS_NOFDREF;
614 } else {
615 so->so_state &= ~SS_ISFCONNECTING;
619 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
620 } /* SS_ISFCONNECTING */
621 #endif
625 * Now UDP sockets.
626 * Incoming packets are sent straight away, they're not buffered.
627 * Incoming UDP data isn't buffered either.
629 for (so = slirp->udb.so_next; so != &slirp->udb;
630 so = so_next) {
631 int revents;
633 so_next = so->so_next;
635 revents = 0;
636 if (so->pollfds_idx != -1) {
637 revents = g_array_index(pollfds, GPollFD,
638 so->pollfds_idx).revents;
641 if (so->s != -1 &&
642 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
643 sorecvfrom(so);
648 * Check incoming ICMP relies.
650 for (so = slirp->icmp.so_next; so != &slirp->icmp;
651 so = so_next) {
652 int revents;
654 so_next = so->so_next;
656 revents = 0;
657 if (so->pollfds_idx != -1) {
658 revents = g_array_index(pollfds, GPollFD,
659 so->pollfds_idx).revents;
662 if (so->s != -1 &&
663 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
664 icmp_receive(so);
669 if_start(slirp);
673 static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
675 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
676 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
677 struct ethhdr *reh = (struct ethhdr *)arp_reply;
678 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
679 int ar_op;
680 struct ex_list *ex_ptr;
682 ar_op = ntohs(ah->ar_op);
683 switch(ar_op) {
684 case ARPOP_REQUEST:
685 if (ah->ar_tip == ah->ar_sip) {
686 /* Gratuitous ARP */
687 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
688 return;
691 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
692 slirp->vnetwork_addr.s_addr) {
693 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
694 ah->ar_tip == slirp->vhost_addr.s_addr)
695 goto arp_ok;
696 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
697 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
698 goto arp_ok;
700 return;
701 arp_ok:
702 memset(arp_reply, 0, sizeof(arp_reply));
704 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
706 /* ARP request for alias/dns mac address */
707 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
708 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
709 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
710 reh->h_proto = htons(ETH_P_ARP);
712 rah->ar_hrd = htons(1);
713 rah->ar_pro = htons(ETH_P_IP);
714 rah->ar_hln = ETH_ALEN;
715 rah->ar_pln = 4;
716 rah->ar_op = htons(ARPOP_REPLY);
717 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
718 rah->ar_sip = ah->ar_tip;
719 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
720 rah->ar_tip = ah->ar_sip;
721 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
723 break;
724 case ARPOP_REPLY:
725 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
726 break;
727 default:
728 break;
732 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
734 struct mbuf *m;
735 int proto;
737 if (pkt_len < ETH_HLEN)
738 return;
740 proto = ntohs(*(uint16_t *)(pkt + 12));
741 switch(proto) {
742 case ETH_P_ARP:
743 arp_input(slirp, pkt, pkt_len);
744 break;
745 case ETH_P_IP:
746 m = m_get(slirp);
747 if (!m)
748 return;
749 /* Note: we add to align the IP header */
750 if (M_FREEROOM(m) < pkt_len + 2) {
751 m_inc(m, pkt_len + 2);
753 m->m_len = pkt_len + 2;
754 memcpy(m->m_data + 2, pkt, pkt_len);
756 m->m_data += 2 + ETH_HLEN;
757 m->m_len -= 2 + ETH_HLEN;
759 ip_input(m);
760 break;
761 default:
762 break;
766 /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no
767 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
768 * is ready to go.
770 static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh,
771 uint8_t ethaddr[ETH_ALEN])
773 const struct ip *iph = (const struct ip *)ifm->m_data;
775 if (iph->ip_dst.s_addr == 0) {
776 /* 0.0.0.0 can not be a destination address, something went wrong,
777 * avoid making it worse */
778 return 1;
780 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
781 uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
782 struct ethhdr *reh = (struct ethhdr *)arp_req;
783 struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
785 if (!ifm->resolution_requested) {
786 /* If the client addr is not known, send an ARP request */
787 memset(reh->h_dest, 0xff, ETH_ALEN);
788 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
789 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
790 reh->h_proto = htons(ETH_P_ARP);
791 rah->ar_hrd = htons(1);
792 rah->ar_pro = htons(ETH_P_IP);
793 rah->ar_hln = ETH_ALEN;
794 rah->ar_pln = 4;
795 rah->ar_op = htons(ARPOP_REQUEST);
797 /* source hw addr */
798 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
799 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
801 /* source IP */
802 rah->ar_sip = slirp->vhost_addr.s_addr;
804 /* target hw addr (none) */
805 memset(rah->ar_tha, 0, ETH_ALEN);
807 /* target IP */
808 rah->ar_tip = iph->ip_dst.s_addr;
809 slirp->client_ipaddr = iph->ip_dst;
810 slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
811 ifm->resolution_requested = true;
813 /* Expire request and drop outgoing packet after 1 second */
814 ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
816 return 0;
817 } else {
818 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
819 /* XXX: not correct */
820 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
821 eh->h_proto = htons(ETH_P_IP);
823 /* Send this */
824 return 2;
828 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
829 * re-queued.
831 int if_encap(Slirp *slirp, struct mbuf *ifm)
833 uint8_t buf[1600];
834 struct ethhdr *eh = (struct ethhdr *)buf;
835 uint8_t ethaddr[ETH_ALEN];
836 const struct ip *iph = (const struct ip *)ifm->m_data;
837 int ret;
839 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
840 return 1;
843 switch (iph->ip_v) {
844 case IPVERSION:
845 ret = if_encap4(slirp, ifm, eh, ethaddr);
846 if (ret < 2) {
847 return ret;
849 break;
851 default:
852 /* Do not assert while we don't manage IP6VERSION */
853 /* assert(0); */
854 break;
857 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
858 DEBUG_ARGS((dfd, " src = %02x:%02x:%02x:%02x:%02x:%02x\n",
859 eh->h_source[0], eh->h_source[1], eh->h_source[2],
860 eh->h_source[3], eh->h_source[4], eh->h_source[5]));
861 DEBUG_ARGS((dfd, " dst = %02x:%02x:%02x:%02x:%02x:%02x\n",
862 eh->h_dest[0], eh->h_dest[1], eh->h_dest[2],
863 eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]));
864 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
865 slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
866 return 1;
869 /* Drop host forwarding rule, return 0 if found. */
870 int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
871 int host_port)
873 struct socket *so;
874 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
875 struct sockaddr_in addr;
876 int port = htons(host_port);
877 socklen_t addr_len;
879 for (so = head->so_next; so != head; so = so->so_next) {
880 addr_len = sizeof(addr);
881 if ((so->so_state & SS_HOSTFWD) &&
882 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
883 addr.sin_addr.s_addr == host_addr.s_addr &&
884 addr.sin_port == port) {
885 close(so->s);
886 sofree(so);
887 return 0;
891 return -1;
894 int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
895 int host_port, struct in_addr guest_addr, int guest_port)
897 if (!guest_addr.s_addr) {
898 guest_addr = slirp->vdhcp_startaddr;
900 if (is_udp) {
901 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
902 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
903 return -1;
904 } else {
905 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
906 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
907 return -1;
909 return 0;
912 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
913 struct in_addr *guest_addr, int guest_port)
915 if (!guest_addr->s_addr) {
916 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
917 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
919 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
920 slirp->vnetwork_addr.s_addr ||
921 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
922 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
923 return -1;
925 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
926 htons(guest_port));
929 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
931 if (so->s == -1 && so->extra) {
932 qemu_chr_fe_write(so->extra, buf, len);
933 return len;
936 return send(so->s, buf, len, flags);
939 static struct socket *
940 slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
942 struct socket *so;
944 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
945 if (so->so_faddr.s_addr == guest_addr.s_addr &&
946 htons(so->so_fport) == guest_port) {
947 return so;
950 return NULL;
953 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
954 int guest_port)
956 struct iovec iov[2];
957 struct socket *so;
959 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
961 if (!so || so->so_state & SS_NOFDREF) {
962 return 0;
965 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) {
966 return 0;
969 return sopreprbuf(so, iov, NULL);
972 void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
973 const uint8_t *buf, int size)
975 int ret;
976 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
978 if (!so)
979 return;
981 ret = soreadbuf(so, (const char *)buf, size);
983 if (ret > 0)
984 tcp_output(sototcpcb(so));
987 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
989 int i;
991 qemu_put_sbe16(f, tp->t_state);
992 for (i = 0; i < TCPT_NTIMERS; i++)
993 qemu_put_sbe16(f, tp->t_timer[i]);
994 qemu_put_sbe16(f, tp->t_rxtshift);
995 qemu_put_sbe16(f, tp->t_rxtcur);
996 qemu_put_sbe16(f, tp->t_dupacks);
997 qemu_put_be16(f, tp->t_maxseg);
998 qemu_put_sbyte(f, tp->t_force);
999 qemu_put_be16(f, tp->t_flags);
1000 qemu_put_be32(f, tp->snd_una);
1001 qemu_put_be32(f, tp->snd_nxt);
1002 qemu_put_be32(f, tp->snd_up);
1003 qemu_put_be32(f, tp->snd_wl1);
1004 qemu_put_be32(f, tp->snd_wl2);
1005 qemu_put_be32(f, tp->iss);
1006 qemu_put_be32(f, tp->snd_wnd);
1007 qemu_put_be32(f, tp->rcv_wnd);
1008 qemu_put_be32(f, tp->rcv_nxt);
1009 qemu_put_be32(f, tp->rcv_up);
1010 qemu_put_be32(f, tp->irs);
1011 qemu_put_be32(f, tp->rcv_adv);
1012 qemu_put_be32(f, tp->snd_max);
1013 qemu_put_be32(f, tp->snd_cwnd);
1014 qemu_put_be32(f, tp->snd_ssthresh);
1015 qemu_put_sbe16(f, tp->t_idle);
1016 qemu_put_sbe16(f, tp->t_rtt);
1017 qemu_put_be32(f, tp->t_rtseq);
1018 qemu_put_sbe16(f, tp->t_srtt);
1019 qemu_put_sbe16(f, tp->t_rttvar);
1020 qemu_put_be16(f, tp->t_rttmin);
1021 qemu_put_be32(f, tp->max_sndwnd);
1022 qemu_put_byte(f, tp->t_oobflags);
1023 qemu_put_byte(f, tp->t_iobc);
1024 qemu_put_sbe16(f, tp->t_softerror);
1025 qemu_put_byte(f, tp->snd_scale);
1026 qemu_put_byte(f, tp->rcv_scale);
1027 qemu_put_byte(f, tp->request_r_scale);
1028 qemu_put_byte(f, tp->requested_s_scale);
1029 qemu_put_be32(f, tp->ts_recent);
1030 qemu_put_be32(f, tp->ts_recent_age);
1031 qemu_put_be32(f, tp->last_ack_sent);
1034 static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
1036 uint32_t off;
1038 qemu_put_be32(f, sbuf->sb_cc);
1039 qemu_put_be32(f, sbuf->sb_datalen);
1040 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
1041 qemu_put_sbe32(f, off);
1042 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
1043 qemu_put_sbe32(f, off);
1044 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1047 static void slirp_socket_save(QEMUFile *f, struct socket *so)
1049 qemu_put_be32(f, so->so_urgc);
1050 qemu_put_be16(f, so->so_ffamily);
1051 switch (so->so_ffamily) {
1052 case AF_INET:
1053 qemu_put_be32(f, so->so_faddr.s_addr);
1054 qemu_put_be16(f, so->so_fport);
1055 break;
1056 default:
1057 error_report(
1058 "so_ffamily unknown, unable to save so_faddr and so_fport\n");
1060 qemu_put_be16(f, so->so_lfamily);
1061 switch (so->so_lfamily) {
1062 case AF_INET:
1063 qemu_put_be32(f, so->so_laddr.s_addr);
1064 qemu_put_be16(f, so->so_lport);
1065 break;
1066 default:
1067 error_report(
1068 "so_ffamily unknown, unable to save so_laddr and so_lport\n");
1070 qemu_put_byte(f, so->so_iptos);
1071 qemu_put_byte(f, so->so_emu);
1072 qemu_put_byte(f, so->so_type);
1073 qemu_put_be32(f, so->so_state);
1074 slirp_sbuf_save(f, &so->so_rcv);
1075 slirp_sbuf_save(f, &so->so_snd);
1076 slirp_tcp_save(f, so->so_tcpcb);
1079 static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
1081 int i;
1083 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1084 qemu_put_be16(f, slirp->bootp_clients[i].allocated);
1085 qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1089 static void slirp_state_save(QEMUFile *f, void *opaque)
1091 Slirp *slirp = opaque;
1092 struct ex_list *ex_ptr;
1094 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1095 if (ex_ptr->ex_pty == 3) {
1096 struct socket *so;
1097 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
1098 ntohs(ex_ptr->ex_fport));
1099 if (!so)
1100 continue;
1102 qemu_put_byte(f, 42);
1103 slirp_socket_save(f, so);
1105 qemu_put_byte(f, 0);
1107 qemu_put_be16(f, slirp->ip_id);
1109 slirp_bootp_save(f, slirp);
1112 static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
1114 int i;
1116 tp->t_state = qemu_get_sbe16(f);
1117 for (i = 0; i < TCPT_NTIMERS; i++)
1118 tp->t_timer[i] = qemu_get_sbe16(f);
1119 tp->t_rxtshift = qemu_get_sbe16(f);
1120 tp->t_rxtcur = qemu_get_sbe16(f);
1121 tp->t_dupacks = qemu_get_sbe16(f);
1122 tp->t_maxseg = qemu_get_be16(f);
1123 tp->t_force = qemu_get_sbyte(f);
1124 tp->t_flags = qemu_get_be16(f);
1125 tp->snd_una = qemu_get_be32(f);
1126 tp->snd_nxt = qemu_get_be32(f);
1127 tp->snd_up = qemu_get_be32(f);
1128 tp->snd_wl1 = qemu_get_be32(f);
1129 tp->snd_wl2 = qemu_get_be32(f);
1130 tp->iss = qemu_get_be32(f);
1131 tp->snd_wnd = qemu_get_be32(f);
1132 tp->rcv_wnd = qemu_get_be32(f);
1133 tp->rcv_nxt = qemu_get_be32(f);
1134 tp->rcv_up = qemu_get_be32(f);
1135 tp->irs = qemu_get_be32(f);
1136 tp->rcv_adv = qemu_get_be32(f);
1137 tp->snd_max = qemu_get_be32(f);
1138 tp->snd_cwnd = qemu_get_be32(f);
1139 tp->snd_ssthresh = qemu_get_be32(f);
1140 tp->t_idle = qemu_get_sbe16(f);
1141 tp->t_rtt = qemu_get_sbe16(f);
1142 tp->t_rtseq = qemu_get_be32(f);
1143 tp->t_srtt = qemu_get_sbe16(f);
1144 tp->t_rttvar = qemu_get_sbe16(f);
1145 tp->t_rttmin = qemu_get_be16(f);
1146 tp->max_sndwnd = qemu_get_be32(f);
1147 tp->t_oobflags = qemu_get_byte(f);
1148 tp->t_iobc = qemu_get_byte(f);
1149 tp->t_softerror = qemu_get_sbe16(f);
1150 tp->snd_scale = qemu_get_byte(f);
1151 tp->rcv_scale = qemu_get_byte(f);
1152 tp->request_r_scale = qemu_get_byte(f);
1153 tp->requested_s_scale = qemu_get_byte(f);
1154 tp->ts_recent = qemu_get_be32(f);
1155 tp->ts_recent_age = qemu_get_be32(f);
1156 tp->last_ack_sent = qemu_get_be32(f);
1157 tcp_template(tp);
1160 static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1162 uint32_t off, sb_cc, sb_datalen;
1164 sb_cc = qemu_get_be32(f);
1165 sb_datalen = qemu_get_be32(f);
1167 sbreserve(sbuf, sb_datalen);
1169 if (sbuf->sb_datalen != sb_datalen)
1170 return -ENOMEM;
1172 sbuf->sb_cc = sb_cc;
1174 off = qemu_get_sbe32(f);
1175 sbuf->sb_wptr = sbuf->sb_data + off;
1176 off = qemu_get_sbe32(f);
1177 sbuf->sb_rptr = sbuf->sb_data + off;
1178 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1180 return 0;
1183 static int slirp_socket_load(QEMUFile *f, struct socket *so)
1185 if (tcp_attach(so) < 0)
1186 return -ENOMEM;
1188 so->so_urgc = qemu_get_be32(f);
1189 so->so_ffamily = qemu_get_be16(f);
1190 switch (so->so_ffamily) {
1191 case AF_INET:
1192 so->so_faddr.s_addr = qemu_get_be32(f);
1193 so->so_fport = qemu_get_be16(f);
1194 break;
1195 default:
1196 error_report(
1197 "so_ffamily unknown, unable to restore so_faddr and so_lport\n");
1199 so->so_lfamily = qemu_get_be16(f);
1200 switch (so->so_lfamily) {
1201 case AF_INET:
1202 so->so_laddr.s_addr = qemu_get_be32(f);
1203 so->so_lport = qemu_get_be16(f);
1204 break;
1205 default:
1206 error_report(
1207 "so_ffamily unknown, unable to restore so_laddr and so_lport\n");
1209 so->so_iptos = qemu_get_byte(f);
1210 so->so_emu = qemu_get_byte(f);
1211 so->so_type = qemu_get_byte(f);
1212 so->so_state = qemu_get_be32(f);
1213 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1214 return -ENOMEM;
1215 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1216 return -ENOMEM;
1217 slirp_tcp_load(f, so->so_tcpcb);
1219 return 0;
1222 static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1224 int i;
1226 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1227 slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1228 qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1232 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1234 Slirp *slirp = opaque;
1235 struct ex_list *ex_ptr;
1237 while (qemu_get_byte(f)) {
1238 int ret;
1239 struct socket *so = socreate(slirp);
1241 if (!so)
1242 return -ENOMEM;
1244 ret = slirp_socket_load(f, so);
1246 if (ret < 0)
1247 return ret;
1249 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1250 slirp->vnetwork_addr.s_addr) {
1251 return -EINVAL;
1253 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1254 if (ex_ptr->ex_pty == 3 &&
1255 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1256 so->so_fport == ex_ptr->ex_fport) {
1257 break;
1260 if (!ex_ptr)
1261 return -EINVAL;
1263 so->extra = (void *)ex_ptr->ex_exec;
1266 if (version_id >= 2) {
1267 slirp->ip_id = qemu_get_be16(f);
1270 if (version_id >= 3) {
1271 slirp_bootp_load(f, slirp);
1274 return 0;