acpi_piix4: fix segfault migrating from 1.2
[qemu/ar7.git] / slirp / slirp.c
blob0e6e232789cdd5b52c2ca698bfcda00c713a490b
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 "char/char.h"
27 #include "slirp.h"
28 #include "hw/hw.h"
30 /* host loopback address */
31 struct in_addr loopback_addr;
32 /* host loopback network mask */
33 unsigned long loopback_mask;
35 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
36 static const uint8_t special_ethaddr[ETH_ALEN] = {
37 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
40 static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
42 /* XXX: suppress those select globals */
43 fd_set *global_readfds, *global_writefds, *global_xfds;
45 u_int curtime;
46 static u_int time_fasttimo, last_slowtimo;
47 static int do_slowtimo;
49 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
50 QTAILQ_HEAD_INITIALIZER(slirp_instances);
52 static struct in_addr dns_addr;
53 static u_int dns_addr_time;
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) < 1000) {
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) < 1000) {
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 lprint("IP address of your DNS(s): ");
143 #endif
144 while (fgets(buff, 512, f) != NULL) {
145 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
146 if (!inet_aton(buff2, &tmp_addr))
147 continue;
148 /* If it's the first one, set it to dns_addr */
149 if (!found) {
150 *pdns_addr = tmp_addr;
151 dns_addr = tmp_addr;
152 dns_addr_time = curtime;
154 #ifdef DEBUG
155 else
156 lprint(", ");
157 #endif
158 if (++found > 3) {
159 #ifdef DEBUG
160 lprint("(more)");
161 #endif
162 break;
164 #ifdef DEBUG
165 else
166 lprint("%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 const char *vhostname, const char *tftp_path,
205 const char *bootfile, struct in_addr vdhcp_start,
206 struct in_addr vnameserver, const char **vdnssearch,
207 void *opaque)
209 Slirp *slirp = g_malloc0(sizeof(Slirp));
211 slirp_init_once();
213 slirp->restricted = restricted;
215 if_init(slirp);
216 ip_init(slirp);
218 /* Initialise mbufs *after* setting the MTU */
219 m_init(slirp);
221 slirp->vnetwork_addr = vnetwork;
222 slirp->vnetwork_mask = vnetmask;
223 slirp->vhost_addr = vhost;
224 if (vhostname) {
225 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
226 vhostname);
228 slirp->tftp_prefix = g_strdup(tftp_path);
229 slirp->bootp_filename = g_strdup(bootfile);
230 slirp->vdhcp_startaddr = vdhcp_start;
231 slirp->vnameserver_addr = vnameserver;
233 if (vdnssearch) {
234 translate_dnssearch(slirp, vdnssearch);
237 slirp->opaque = opaque;
239 register_savevm(NULL, "slirp", 0, 3,
240 slirp_state_save, slirp_state_load, slirp);
242 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
244 return slirp;
247 void slirp_cleanup(Slirp *slirp)
249 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
251 unregister_savevm(NULL, "slirp", slirp);
253 ip_cleanup(slirp);
254 m_cleanup(slirp);
256 g_free(slirp->vdnssearch);
257 g_free(slirp->tftp_prefix);
258 g_free(slirp->bootp_filename);
259 g_free(slirp);
262 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
263 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
264 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
266 void slirp_update_timeout(uint32_t *timeout)
268 if (!QTAILQ_EMPTY(&slirp_instances)) {
269 *timeout = MIN(1000, *timeout);
273 void slirp_select_fill(int *pnfds,
274 fd_set *readfds, fd_set *writefds, fd_set *xfds)
276 Slirp *slirp;
277 struct socket *so, *so_next;
278 int nfds;
280 if (QTAILQ_EMPTY(&slirp_instances)) {
281 return;
284 /* fail safe */
285 global_readfds = NULL;
286 global_writefds = NULL;
287 global_xfds = NULL;
289 nfds = *pnfds;
291 * First, TCP sockets
293 do_slowtimo = 0;
295 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
297 * *_slowtimo needs calling if there are IP fragments
298 * in the fragment queue, or there are TCP connections active
300 do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
301 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
303 for (so = slirp->tcb.so_next; so != &slirp->tcb;
304 so = so_next) {
305 so_next = so->so_next;
308 * See if we need a tcp_fasttimo
310 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
311 time_fasttimo = curtime; /* Flag when we want a fasttimo */
314 * NOFDREF can include still connecting to local-host,
315 * newly socreated() sockets etc. Don't want to select these.
317 if (so->so_state & SS_NOFDREF || so->s == -1)
318 continue;
321 * Set for reading sockets which are accepting
323 if (so->so_state & SS_FACCEPTCONN) {
324 FD_SET(so->s, readfds);
325 UPD_NFDS(so->s);
326 continue;
330 * Set for writing sockets which are connecting
332 if (so->so_state & SS_ISFCONNECTING) {
333 FD_SET(so->s, writefds);
334 UPD_NFDS(so->s);
335 continue;
339 * Set for writing if we are connected, can send more, and
340 * we have something to send
342 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
343 FD_SET(so->s, writefds);
344 UPD_NFDS(so->s);
348 * Set for reading (and urgent data) if we are connected, can
349 * receive more, and we have room for it XXX /2 ?
351 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
352 FD_SET(so->s, readfds);
353 FD_SET(so->s, xfds);
354 UPD_NFDS(so->s);
359 * UDP sockets
361 for (so = slirp->udb.so_next; so != &slirp->udb;
362 so = so_next) {
363 so_next = so->so_next;
366 * See if it's timed out
368 if (so->so_expire) {
369 if (so->so_expire <= curtime) {
370 udp_detach(so);
371 continue;
372 } else
373 do_slowtimo = 1; /* Let socket expire */
377 * When UDP packets are received from over the
378 * link, they're sendto()'d straight away, so
379 * no need for setting for writing
380 * Limit the number of packets queued by this session
381 * to 4. Note that even though we try and limit this
382 * to 4 packets, the session could have more queued
383 * if the packets needed to be fragmented
384 * (XXX <= 4 ?)
386 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
387 FD_SET(so->s, readfds);
388 UPD_NFDS(so->s);
393 * ICMP sockets
395 for (so = slirp->icmp.so_next; so != &slirp->icmp;
396 so = so_next) {
397 so_next = so->so_next;
400 * See if it's timed out
402 if (so->so_expire) {
403 if (so->so_expire <= curtime) {
404 icmp_detach(so);
405 continue;
406 } else {
407 do_slowtimo = 1; /* Let socket expire */
411 if (so->so_state & SS_ISFCONNECTED) {
412 FD_SET(so->s, readfds);
413 UPD_NFDS(so->s);
418 *pnfds = nfds;
421 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
422 int select_error)
424 Slirp *slirp;
425 struct socket *so, *so_next;
426 int ret;
428 if (QTAILQ_EMPTY(&slirp_instances)) {
429 return;
432 global_readfds = readfds;
433 global_writefds = writefds;
434 global_xfds = xfds;
436 curtime = qemu_get_clock_ms(rt_clock);
438 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
440 * See if anything has timed out
442 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
443 tcp_fasttimo(slirp);
444 time_fasttimo = 0;
446 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
447 ip_slowtimo(slirp);
448 tcp_slowtimo(slirp);
449 last_slowtimo = curtime;
453 * Check sockets
455 if (!select_error) {
457 * Check TCP sockets
459 for (so = slirp->tcb.so_next; so != &slirp->tcb;
460 so = so_next) {
461 so_next = so->so_next;
464 * FD_ISSET is meaningless on these sockets
465 * (and they can crash the program)
467 if (so->so_state & SS_NOFDREF || so->s == -1)
468 continue;
471 * Check for URG data
472 * This will soread as well, so no need to
473 * test for readfds below if this succeeds
475 if (FD_ISSET(so->s, xfds))
476 sorecvoob(so);
478 * Check sockets for reading
480 else if (FD_ISSET(so->s, readfds)) {
482 * Check for incoming connections
484 if (so->so_state & SS_FACCEPTCONN) {
485 tcp_connect(so);
486 continue;
487 } /* else */
488 ret = soread(so);
490 /* Output it if we read something */
491 if (ret > 0)
492 tcp_output(sototcpcb(so));
496 * Check sockets for writing
498 if (FD_ISSET(so->s, writefds)) {
500 * Check for non-blocking, still-connecting sockets
502 if (so->so_state & SS_ISFCONNECTING) {
503 /* Connected */
504 so->so_state &= ~SS_ISFCONNECTING;
506 ret = send(so->s, (const void *) &ret, 0, 0);
507 if (ret < 0) {
508 /* XXXXX Must fix, zero bytes is a NOP */
509 if (errno == EAGAIN || errno == EWOULDBLOCK ||
510 errno == EINPROGRESS || errno == ENOTCONN)
511 continue;
513 /* else failed */
514 so->so_state &= SS_PERSISTENT_MASK;
515 so->so_state |= SS_NOFDREF;
517 /* else so->so_state &= ~SS_ISFCONNECTING; */
520 * Continue tcp_input
522 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
523 /* continue; */
524 } else
525 ret = sowrite(so);
527 * XXXXX If we wrote something (a lot), there
528 * could be a need for a window update.
529 * In the worst case, the remote will send
530 * a window probe to get things going again
535 * Probe a still-connecting, non-blocking socket
536 * to check if it's still alive
538 #ifdef PROBE_CONN
539 if (so->so_state & SS_ISFCONNECTING) {
540 ret = qemu_recv(so->s, &ret, 0,0);
542 if (ret < 0) {
543 /* XXX */
544 if (errno == EAGAIN || errno == EWOULDBLOCK ||
545 errno == EINPROGRESS || errno == ENOTCONN)
546 continue; /* Still connecting, continue */
548 /* else failed */
549 so->so_state &= SS_PERSISTENT_MASK;
550 so->so_state |= SS_NOFDREF;
552 /* tcp_input will take care of it */
553 } else {
554 ret = send(so->s, &ret, 0,0);
555 if (ret < 0) {
556 /* XXX */
557 if (errno == EAGAIN || errno == EWOULDBLOCK ||
558 errno == EINPROGRESS || errno == ENOTCONN)
559 continue;
560 /* else failed */
561 so->so_state &= SS_PERSISTENT_MASK;
562 so->so_state |= SS_NOFDREF;
563 } else
564 so->so_state &= ~SS_ISFCONNECTING;
567 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
568 } /* SS_ISFCONNECTING */
569 #endif
573 * Now UDP sockets.
574 * Incoming packets are sent straight away, they're not buffered.
575 * Incoming UDP data isn't buffered either.
577 for (so = slirp->udb.so_next; so != &slirp->udb;
578 so = so_next) {
579 so_next = so->so_next;
581 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
582 sorecvfrom(so);
587 * Check incoming ICMP relies.
589 for (so = slirp->icmp.so_next; so != &slirp->icmp;
590 so = so_next) {
591 so_next = so->so_next;
593 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
594 icmp_receive(so);
599 if_start(slirp);
602 /* clear global file descriptor sets.
603 * these reside on the stack in vl.c
604 * so they're unusable if we're not in
605 * slirp_select_fill or slirp_select_poll.
607 global_readfds = NULL;
608 global_writefds = NULL;
609 global_xfds = NULL;
612 static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
614 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
615 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
616 struct ethhdr *reh = (struct ethhdr *)arp_reply;
617 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
618 int ar_op;
619 struct ex_list *ex_ptr;
621 ar_op = ntohs(ah->ar_op);
622 switch(ar_op) {
623 case ARPOP_REQUEST:
624 if (ah->ar_tip == ah->ar_sip) {
625 /* Gratuitous ARP */
626 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
627 return;
630 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
631 slirp->vnetwork_addr.s_addr) {
632 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
633 ah->ar_tip == slirp->vhost_addr.s_addr)
634 goto arp_ok;
635 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
636 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
637 goto arp_ok;
639 return;
640 arp_ok:
641 memset(arp_reply, 0, sizeof(arp_reply));
643 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
645 /* ARP request for alias/dns mac address */
646 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
647 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
648 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
649 reh->h_proto = htons(ETH_P_ARP);
651 rah->ar_hrd = htons(1);
652 rah->ar_pro = htons(ETH_P_IP);
653 rah->ar_hln = ETH_ALEN;
654 rah->ar_pln = 4;
655 rah->ar_op = htons(ARPOP_REPLY);
656 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
657 rah->ar_sip = ah->ar_tip;
658 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
659 rah->ar_tip = ah->ar_sip;
660 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
662 break;
663 case ARPOP_REPLY:
664 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
665 break;
666 default:
667 break;
671 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
673 struct mbuf *m;
674 int proto;
676 if (pkt_len < ETH_HLEN)
677 return;
679 proto = ntohs(*(uint16_t *)(pkt + 12));
680 switch(proto) {
681 case ETH_P_ARP:
682 arp_input(slirp, pkt, pkt_len);
683 break;
684 case ETH_P_IP:
685 m = m_get(slirp);
686 if (!m)
687 return;
688 /* Note: we add to align the IP header */
689 if (M_FREEROOM(m) < pkt_len + 2) {
690 m_inc(m, pkt_len + 2);
692 m->m_len = pkt_len + 2;
693 memcpy(m->m_data + 2, pkt, pkt_len);
695 m->m_data += 2 + ETH_HLEN;
696 m->m_len -= 2 + ETH_HLEN;
698 ip_input(m);
699 break;
700 default:
701 break;
705 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
706 * re-queued.
708 int if_encap(Slirp *slirp, struct mbuf *ifm)
710 uint8_t buf[1600];
711 struct ethhdr *eh = (struct ethhdr *)buf;
712 uint8_t ethaddr[ETH_ALEN];
713 const struct ip *iph = (const struct ip *)ifm->m_data;
715 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
716 return 1;
719 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
720 uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
721 struct ethhdr *reh = (struct ethhdr *)arp_req;
722 struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
724 if (!ifm->arp_requested) {
725 /* If the client addr is not known, send an ARP request */
726 memset(reh->h_dest, 0xff, ETH_ALEN);
727 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
728 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
729 reh->h_proto = htons(ETH_P_ARP);
730 rah->ar_hrd = htons(1);
731 rah->ar_pro = htons(ETH_P_IP);
732 rah->ar_hln = ETH_ALEN;
733 rah->ar_pln = 4;
734 rah->ar_op = htons(ARPOP_REQUEST);
736 /* source hw addr */
737 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
738 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
740 /* source IP */
741 rah->ar_sip = slirp->vhost_addr.s_addr;
743 /* target hw addr (none) */
744 memset(rah->ar_tha, 0, ETH_ALEN);
746 /* target IP */
747 rah->ar_tip = iph->ip_dst.s_addr;
748 slirp->client_ipaddr = iph->ip_dst;
749 slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
750 ifm->arp_requested = true;
752 /* Expire request and drop outgoing packet after 1 second */
753 ifm->expiration_date = qemu_get_clock_ns(rt_clock) + 1000000000ULL;
755 return 0;
756 } else {
757 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
758 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
759 /* XXX: not correct */
760 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
761 eh->h_proto = htons(ETH_P_IP);
762 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
763 slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
764 return 1;
768 /* Drop host forwarding rule, return 0 if found. */
769 int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
770 int host_port)
772 struct socket *so;
773 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
774 struct sockaddr_in addr;
775 int port = htons(host_port);
776 socklen_t addr_len;
778 for (so = head->so_next; so != head; so = so->so_next) {
779 addr_len = sizeof(addr);
780 if ((so->so_state & SS_HOSTFWD) &&
781 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
782 addr.sin_addr.s_addr == host_addr.s_addr &&
783 addr.sin_port == port) {
784 close(so->s);
785 sofree(so);
786 return 0;
790 return -1;
793 int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
794 int host_port, struct in_addr guest_addr, int guest_port)
796 if (!guest_addr.s_addr) {
797 guest_addr = slirp->vdhcp_startaddr;
799 if (is_udp) {
800 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
801 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
802 return -1;
803 } else {
804 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
805 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
806 return -1;
808 return 0;
811 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
812 struct in_addr *guest_addr, int guest_port)
814 if (!guest_addr->s_addr) {
815 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
816 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
818 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
819 slirp->vnetwork_addr.s_addr ||
820 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
821 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
822 return -1;
824 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
825 htons(guest_port));
828 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
830 if (so->s == -1 && so->extra) {
831 qemu_chr_fe_write(so->extra, buf, len);
832 return len;
835 return send(so->s, buf, len, flags);
838 static struct socket *
839 slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
841 struct socket *so;
843 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
844 if (so->so_faddr.s_addr == guest_addr.s_addr &&
845 htons(so->so_fport) == guest_port) {
846 return so;
849 return NULL;
852 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
853 int guest_port)
855 struct iovec iov[2];
856 struct socket *so;
858 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
860 if (!so || so->so_state & SS_NOFDREF)
861 return 0;
863 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2))
864 return 0;
866 return sopreprbuf(so, iov, NULL);
869 void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
870 const uint8_t *buf, int size)
872 int ret;
873 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
875 if (!so)
876 return;
878 ret = soreadbuf(so, (const char *)buf, size);
880 if (ret > 0)
881 tcp_output(sototcpcb(so));
884 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
886 int i;
888 qemu_put_sbe16(f, tp->t_state);
889 for (i = 0; i < TCPT_NTIMERS; i++)
890 qemu_put_sbe16(f, tp->t_timer[i]);
891 qemu_put_sbe16(f, tp->t_rxtshift);
892 qemu_put_sbe16(f, tp->t_rxtcur);
893 qemu_put_sbe16(f, tp->t_dupacks);
894 qemu_put_be16(f, tp->t_maxseg);
895 qemu_put_sbyte(f, tp->t_force);
896 qemu_put_be16(f, tp->t_flags);
897 qemu_put_be32(f, tp->snd_una);
898 qemu_put_be32(f, tp->snd_nxt);
899 qemu_put_be32(f, tp->snd_up);
900 qemu_put_be32(f, tp->snd_wl1);
901 qemu_put_be32(f, tp->snd_wl2);
902 qemu_put_be32(f, tp->iss);
903 qemu_put_be32(f, tp->snd_wnd);
904 qemu_put_be32(f, tp->rcv_wnd);
905 qemu_put_be32(f, tp->rcv_nxt);
906 qemu_put_be32(f, tp->rcv_up);
907 qemu_put_be32(f, tp->irs);
908 qemu_put_be32(f, tp->rcv_adv);
909 qemu_put_be32(f, tp->snd_max);
910 qemu_put_be32(f, tp->snd_cwnd);
911 qemu_put_be32(f, tp->snd_ssthresh);
912 qemu_put_sbe16(f, tp->t_idle);
913 qemu_put_sbe16(f, tp->t_rtt);
914 qemu_put_be32(f, tp->t_rtseq);
915 qemu_put_sbe16(f, tp->t_srtt);
916 qemu_put_sbe16(f, tp->t_rttvar);
917 qemu_put_be16(f, tp->t_rttmin);
918 qemu_put_be32(f, tp->max_sndwnd);
919 qemu_put_byte(f, tp->t_oobflags);
920 qemu_put_byte(f, tp->t_iobc);
921 qemu_put_sbe16(f, tp->t_softerror);
922 qemu_put_byte(f, tp->snd_scale);
923 qemu_put_byte(f, tp->rcv_scale);
924 qemu_put_byte(f, tp->request_r_scale);
925 qemu_put_byte(f, tp->requested_s_scale);
926 qemu_put_be32(f, tp->ts_recent);
927 qemu_put_be32(f, tp->ts_recent_age);
928 qemu_put_be32(f, tp->last_ack_sent);
931 static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
933 uint32_t off;
935 qemu_put_be32(f, sbuf->sb_cc);
936 qemu_put_be32(f, sbuf->sb_datalen);
937 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
938 qemu_put_sbe32(f, off);
939 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
940 qemu_put_sbe32(f, off);
941 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
944 static void slirp_socket_save(QEMUFile *f, struct socket *so)
946 qemu_put_be32(f, so->so_urgc);
947 qemu_put_be32(f, so->so_faddr.s_addr);
948 qemu_put_be32(f, so->so_laddr.s_addr);
949 qemu_put_be16(f, so->so_fport);
950 qemu_put_be16(f, so->so_lport);
951 qemu_put_byte(f, so->so_iptos);
952 qemu_put_byte(f, so->so_emu);
953 qemu_put_byte(f, so->so_type);
954 qemu_put_be32(f, so->so_state);
955 slirp_sbuf_save(f, &so->so_rcv);
956 slirp_sbuf_save(f, &so->so_snd);
957 slirp_tcp_save(f, so->so_tcpcb);
960 static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
962 int i;
964 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
965 qemu_put_be16(f, slirp->bootp_clients[i].allocated);
966 qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
970 static void slirp_state_save(QEMUFile *f, void *opaque)
972 Slirp *slirp = opaque;
973 struct ex_list *ex_ptr;
975 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
976 if (ex_ptr->ex_pty == 3) {
977 struct socket *so;
978 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
979 ntohs(ex_ptr->ex_fport));
980 if (!so)
981 continue;
983 qemu_put_byte(f, 42);
984 slirp_socket_save(f, so);
986 qemu_put_byte(f, 0);
988 qemu_put_be16(f, slirp->ip_id);
990 slirp_bootp_save(f, slirp);
993 static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
995 int i;
997 tp->t_state = qemu_get_sbe16(f);
998 for (i = 0; i < TCPT_NTIMERS; i++)
999 tp->t_timer[i] = qemu_get_sbe16(f);
1000 tp->t_rxtshift = qemu_get_sbe16(f);
1001 tp->t_rxtcur = qemu_get_sbe16(f);
1002 tp->t_dupacks = qemu_get_sbe16(f);
1003 tp->t_maxseg = qemu_get_be16(f);
1004 tp->t_force = qemu_get_sbyte(f);
1005 tp->t_flags = qemu_get_be16(f);
1006 tp->snd_una = qemu_get_be32(f);
1007 tp->snd_nxt = qemu_get_be32(f);
1008 tp->snd_up = qemu_get_be32(f);
1009 tp->snd_wl1 = qemu_get_be32(f);
1010 tp->snd_wl2 = qemu_get_be32(f);
1011 tp->iss = qemu_get_be32(f);
1012 tp->snd_wnd = qemu_get_be32(f);
1013 tp->rcv_wnd = qemu_get_be32(f);
1014 tp->rcv_nxt = qemu_get_be32(f);
1015 tp->rcv_up = qemu_get_be32(f);
1016 tp->irs = qemu_get_be32(f);
1017 tp->rcv_adv = qemu_get_be32(f);
1018 tp->snd_max = qemu_get_be32(f);
1019 tp->snd_cwnd = qemu_get_be32(f);
1020 tp->snd_ssthresh = qemu_get_be32(f);
1021 tp->t_idle = qemu_get_sbe16(f);
1022 tp->t_rtt = qemu_get_sbe16(f);
1023 tp->t_rtseq = qemu_get_be32(f);
1024 tp->t_srtt = qemu_get_sbe16(f);
1025 tp->t_rttvar = qemu_get_sbe16(f);
1026 tp->t_rttmin = qemu_get_be16(f);
1027 tp->max_sndwnd = qemu_get_be32(f);
1028 tp->t_oobflags = qemu_get_byte(f);
1029 tp->t_iobc = qemu_get_byte(f);
1030 tp->t_softerror = qemu_get_sbe16(f);
1031 tp->snd_scale = qemu_get_byte(f);
1032 tp->rcv_scale = qemu_get_byte(f);
1033 tp->request_r_scale = qemu_get_byte(f);
1034 tp->requested_s_scale = qemu_get_byte(f);
1035 tp->ts_recent = qemu_get_be32(f);
1036 tp->ts_recent_age = qemu_get_be32(f);
1037 tp->last_ack_sent = qemu_get_be32(f);
1038 tcp_template(tp);
1041 static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1043 uint32_t off, sb_cc, sb_datalen;
1045 sb_cc = qemu_get_be32(f);
1046 sb_datalen = qemu_get_be32(f);
1048 sbreserve(sbuf, sb_datalen);
1050 if (sbuf->sb_datalen != sb_datalen)
1051 return -ENOMEM;
1053 sbuf->sb_cc = sb_cc;
1055 off = qemu_get_sbe32(f);
1056 sbuf->sb_wptr = sbuf->sb_data + off;
1057 off = qemu_get_sbe32(f);
1058 sbuf->sb_rptr = sbuf->sb_data + off;
1059 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1061 return 0;
1064 static int slirp_socket_load(QEMUFile *f, struct socket *so)
1066 if (tcp_attach(so) < 0)
1067 return -ENOMEM;
1069 so->so_urgc = qemu_get_be32(f);
1070 so->so_faddr.s_addr = qemu_get_be32(f);
1071 so->so_laddr.s_addr = qemu_get_be32(f);
1072 so->so_fport = qemu_get_be16(f);
1073 so->so_lport = qemu_get_be16(f);
1074 so->so_iptos = qemu_get_byte(f);
1075 so->so_emu = qemu_get_byte(f);
1076 so->so_type = qemu_get_byte(f);
1077 so->so_state = qemu_get_be32(f);
1078 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1079 return -ENOMEM;
1080 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1081 return -ENOMEM;
1082 slirp_tcp_load(f, so->so_tcpcb);
1084 return 0;
1087 static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1089 int i;
1091 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1092 slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1093 qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1097 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1099 Slirp *slirp = opaque;
1100 struct ex_list *ex_ptr;
1102 while (qemu_get_byte(f)) {
1103 int ret;
1104 struct socket *so = socreate(slirp);
1106 if (!so)
1107 return -ENOMEM;
1109 ret = slirp_socket_load(f, so);
1111 if (ret < 0)
1112 return ret;
1114 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1115 slirp->vnetwork_addr.s_addr) {
1116 return -EINVAL;
1118 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1119 if (ex_ptr->ex_pty == 3 &&
1120 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1121 so->so_fport == ex_ptr->ex_fport) {
1122 break;
1125 if (!ex_ptr)
1126 return -EINVAL;
1128 so->extra = (void *)ex_ptr->ex_exec;
1131 if (version_id >= 2) {
1132 slirp->ip_id = qemu_get_be16(f);
1135 if (version_id >= 3) {
1136 slirp_bootp_load(f, slirp);
1139 return 0;