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
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"
31 #include "qemu/cutils.h"
37 /* host loopback address */
38 struct in_addr loopback_addr
;
39 /* host loopback network mask */
40 unsigned long loopback_mask
;
42 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
43 static const uint8_t special_ethaddr
[ETH_ALEN
] = {
44 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
49 static QTAILQ_HEAD(slirp_instances
, Slirp
) slirp_instances
=
50 QTAILQ_HEAD_INITIALIZER(slirp_instances
);
52 static struct in_addr dns_addr
;
54 static struct in6_addr dns6_addr
;
56 static u_int dns_addr_time
;
58 static u_int dns6_addr_time
;
61 #define TIMEOUT_FAST 2 /* milliseconds */
62 #define TIMEOUT_SLOW 499 /* milliseconds */
63 /* for the aging of certain requests like DNS */
64 #define TIMEOUT_DEFAULT 1000 /* milliseconds */
68 int get_dns_addr(struct in_addr
*pdns_addr
)
70 FIXED_INFO
*FixedInfo
=NULL
;
73 IP_ADDR_STRING
*pIPAddr
;
74 struct in_addr tmp_addr
;
76 if (dns_addr
.s_addr
!= 0 && (curtime
- dns_addr_time
) < TIMEOUT_DEFAULT
) {
77 *pdns_addr
= dns_addr
;
81 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
82 BufLen
= sizeof(FIXED_INFO
);
84 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
86 GlobalFree(FixedInfo
);
89 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
92 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
93 printf("GetNetworkParams failed. ret = %08x\n", (u_int
)ret
);
95 GlobalFree(FixedInfo
);
101 pIPAddr
= &(FixedInfo
->DnsServerList
);
102 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
103 *pdns_addr
= tmp_addr
;
105 dns_addr_time
= curtime
;
107 GlobalFree(FixedInfo
);
113 int get_dns6_addr(struct in6_addr
*pdns6_addr
, uint32_t *scope_id
)
118 static void winsock_cleanup(void)
125 static int get_dns_addr_cached(void *pdns_addr
, void *cached_addr
,
127 struct stat
*cached_stat
, u_int
*cached_time
)
129 struct stat old_stat
;
130 if (curtime
- *cached_time
< TIMEOUT_DEFAULT
) {
131 memcpy(pdns_addr
, cached_addr
, addrlen
);
134 old_stat
= *cached_stat
;
135 if (stat("/etc/resolv.conf", cached_stat
) != 0) {
138 if (cached_stat
->st_dev
== old_stat
.st_dev
139 && cached_stat
->st_ino
== old_stat
.st_ino
140 && cached_stat
->st_size
== old_stat
.st_size
141 && cached_stat
->st_mtime
== old_stat
.st_mtime
) {
142 memcpy(pdns_addr
, cached_addr
, addrlen
);
148 static int get_dns_addr_resolv_conf(int af
, void *pdns_addr
, void *cached_addr
,
149 socklen_t addrlen
, uint32_t *scope_id
,
156 void *tmp_addr
= alloca(addrlen
);
159 f
= fopen("/etc/resolv.conf", "r");
164 fprintf(stderr
, "IP address of your DNS(s): ");
166 while (fgets(buff
, 512, f
) != NULL
) {
167 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
168 char *c
= strchr(buff2
, '%');
170 if_index
= if_nametoindex(c
+ 1);
176 if (!inet_pton(af
, buff2
, tmp_addr
)) {
179 /* If it's the first one, set it to dns_addr */
181 memcpy(pdns_addr
, tmp_addr
, addrlen
);
182 memcpy(cached_addr
, tmp_addr
, addrlen
);
184 *scope_id
= if_index
;
186 *cached_time
= curtime
;
190 fprintf(stderr
, ", ");
194 fprintf(stderr
, "(more)");
200 char s
[INET6_ADDRSTRLEN
];
201 const char *res
= inet_ntop(af
, tmp_addr
, s
, sizeof(s
));
203 res
= "(string conversion error)";
205 fprintf(stderr
, "%s", res
);
216 int get_dns_addr(struct in_addr
*pdns_addr
)
218 static struct stat dns_addr_stat
;
220 if (dns_addr
.s_addr
!= 0) {
222 ret
= get_dns_addr_cached(pdns_addr
, &dns_addr
, sizeof(dns_addr
),
223 &dns_addr_stat
, &dns_addr_time
);
228 return get_dns_addr_resolv_conf(AF_INET
, pdns_addr
, &dns_addr
,
229 sizeof(dns_addr
), NULL
, &dns_addr_time
);
232 int get_dns6_addr(struct in6_addr
*pdns6_addr
, uint32_t *scope_id
)
234 static struct stat dns6_addr_stat
;
236 if (!in6_zero(&dns6_addr
)) {
238 ret
= get_dns_addr_cached(pdns6_addr
, &dns6_addr
, sizeof(dns6_addr
),
239 &dns6_addr_stat
, &dns6_addr_time
);
244 return get_dns_addr_resolv_conf(AF_INET6
, pdns6_addr
, &dns6_addr
,
246 scope_id
, &dns6_addr_time
);
251 static void slirp_init_once(void)
253 static int initialized
;
264 WSAStartup(MAKEWORD(2,0), &Data
);
265 atexit(winsock_cleanup
);
268 loopback_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
269 loopback_mask
= htonl(IN_CLASSA_NET
);
272 static void slirp_state_save(QEMUFile
*f
, void *opaque
);
273 static int slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
);
275 Slirp
*slirp_init(int restricted
, bool in_enabled
, struct in_addr vnetwork
,
276 struct in_addr vnetmask
, struct in_addr vhost
,
278 struct in6_addr vprefix_addr6
, uint8_t vprefix_len
,
279 struct in6_addr vhost6
, const char *vhostname
,
280 const char *tftp_path
, const char *bootfile
,
281 struct in_addr vdhcp_start
, struct in_addr vnameserver
,
282 struct in6_addr vnameserver6
, const char **vdnssearch
,
285 Slirp
*slirp
= g_malloc0(sizeof(Slirp
));
289 slirp
->grand
= g_rand_new();
290 slirp
->restricted
= restricted
;
292 slirp
->in_enabled
= in_enabled
;
293 slirp
->in6_enabled
= in6_enabled
;
299 /* Initialise mbufs *after* setting the MTU */
302 slirp
->vnetwork_addr
= vnetwork
;
303 slirp
->vnetwork_mask
= vnetmask
;
304 slirp
->vhost_addr
= vhost
;
305 slirp
->vprefix_addr6
= vprefix_addr6
;
306 slirp
->vprefix_len
= vprefix_len
;
307 slirp
->vhost_addr6
= vhost6
;
309 pstrcpy(slirp
->client_hostname
, sizeof(slirp
->client_hostname
),
312 slirp
->tftp_prefix
= g_strdup(tftp_path
);
313 slirp
->bootp_filename
= g_strdup(bootfile
);
314 slirp
->vdhcp_startaddr
= vdhcp_start
;
315 slirp
->vnameserver_addr
= vnameserver
;
316 slirp
->vnameserver_addr6
= vnameserver6
;
319 translate_dnssearch(slirp
, vdnssearch
);
322 slirp
->opaque
= opaque
;
324 register_savevm(NULL
, "slirp", 0, 4,
325 slirp_state_save
, slirp_state_load
, slirp
);
327 QTAILQ_INSERT_TAIL(&slirp_instances
, slirp
, entry
);
332 void slirp_cleanup(Slirp
*slirp
)
334 QTAILQ_REMOVE(&slirp_instances
, slirp
, entry
);
336 unregister_savevm(NULL
, "slirp", slirp
);
342 g_rand_free(slirp
->grand
);
344 g_free(slirp
->vdnssearch
);
345 g_free(slirp
->tftp_prefix
);
346 g_free(slirp
->bootp_filename
);
350 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
351 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
353 static void slirp_update_timeout(uint32_t *timeout
)
358 if (*timeout
<= TIMEOUT_FAST
) {
362 t
= MIN(1000, *timeout
);
364 /* If we have tcp timeout with slirp, then we will fill @timeout with
365 * more precise value.
367 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
368 if (slirp
->time_fasttimo
) {
369 *timeout
= TIMEOUT_FAST
;
372 if (slirp
->do_slowtimo
) {
373 t
= MIN(TIMEOUT_SLOW
, t
);
379 void slirp_pollfds_fill(GArray
*pollfds
, uint32_t *timeout
)
382 struct socket
*so
, *so_next
;
384 if (QTAILQ_EMPTY(&slirp_instances
)) {
392 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
394 * *_slowtimo needs calling if there are IP fragments
395 * in the fragment queue, or there are TCP connections active
397 slirp
->do_slowtimo
= ((slirp
->tcb
.so_next
!= &slirp
->tcb
) ||
398 (&slirp
->ipq
.ip_link
!= slirp
->ipq
.ip_link
.next
));
400 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
404 so_next
= so
->so_next
;
406 so
->pollfds_idx
= -1;
409 * See if we need a tcp_fasttimo
411 if (slirp
->time_fasttimo
== 0 &&
412 so
->so_tcpcb
->t_flags
& TF_DELACK
) {
413 slirp
->time_fasttimo
= curtime
; /* Flag when want a fasttimo */
417 * NOFDREF can include still connecting to local-host,
418 * newly socreated() sockets etc. Don't want to select these.
420 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
425 * Set for reading sockets which are accepting
427 if (so
->so_state
& SS_FACCEPTCONN
) {
430 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
432 so
->pollfds_idx
= pollfds
->len
;
433 g_array_append_val(pollfds
, pfd
);
438 * Set for writing sockets which are connecting
440 if (so
->so_state
& SS_ISFCONNECTING
) {
443 .events
= G_IO_OUT
| G_IO_ERR
,
445 so
->pollfds_idx
= pollfds
->len
;
446 g_array_append_val(pollfds
, pfd
);
451 * Set for writing if we are connected, can send more, and
452 * we have something to send
454 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
455 events
|= G_IO_OUT
| G_IO_ERR
;
459 * Set for reading (and urgent data) if we are connected, can
460 * receive more, and we have room for it XXX /2 ?
462 if (CONN_CANFRCV(so
) &&
463 (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
464 events
|= G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_PRI
;
472 so
->pollfds_idx
= pollfds
->len
;
473 g_array_append_val(pollfds
, pfd
);
480 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
482 so_next
= so
->so_next
;
484 so
->pollfds_idx
= -1;
487 * See if it's timed out
490 if (so
->so_expire
<= curtime
) {
494 slirp
->do_slowtimo
= true; /* Let socket expire */
499 * When UDP packets are received from over the
500 * link, they're sendto()'d straight away, so
501 * no need for setting for writing
502 * Limit the number of packets queued by this session
503 * to 4. Note that even though we try and limit this
504 * to 4 packets, the session could have more queued
505 * if the packets needed to be fragmented
508 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
511 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
513 so
->pollfds_idx
= pollfds
->len
;
514 g_array_append_val(pollfds
, pfd
);
521 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
523 so_next
= so
->so_next
;
525 so
->pollfds_idx
= -1;
528 * See if it's timed out
531 if (so
->so_expire
<= curtime
) {
535 slirp
->do_slowtimo
= true; /* Let socket expire */
539 if (so
->so_state
& SS_ISFCONNECTED
) {
542 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
544 so
->pollfds_idx
= pollfds
->len
;
545 g_array_append_val(pollfds
, pfd
);
549 slirp_update_timeout(timeout
);
552 void slirp_pollfds_poll(GArray
*pollfds
, int select_error
)
555 struct socket
*so
, *so_next
;
558 if (QTAILQ_EMPTY(&slirp_instances
)) {
562 curtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
564 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
566 * See if anything has timed out
568 if (slirp
->time_fasttimo
&&
569 ((curtime
- slirp
->time_fasttimo
) >= TIMEOUT_FAST
)) {
571 slirp
->time_fasttimo
= 0;
573 if (slirp
->do_slowtimo
&&
574 ((curtime
- slirp
->last_slowtimo
) >= TIMEOUT_SLOW
)) {
577 slirp
->last_slowtimo
= curtime
;
587 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
591 so_next
= so
->so_next
;
594 if (so
->pollfds_idx
!= -1) {
595 revents
= g_array_index(pollfds
, GPollFD
,
596 so
->pollfds_idx
).revents
;
599 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
605 * This will soread as well, so no need to
606 * test for G_IO_IN below if this succeeds
608 if (revents
& G_IO_PRI
) {
611 /* Socket error might have resulted in the socket being
612 * removed, do not try to do anything more with it. */
617 * Check sockets for reading
619 else if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
)) {
621 * Check for incoming connections
623 if (so
->so_state
& SS_FACCEPTCONN
) {
629 /* Output it if we read something */
631 tcp_output(sototcpcb(so
));
634 /* Socket error might have resulted in the socket being
635 * removed, do not try to do anything more with it. */
641 * Check sockets for writing
643 if (!(so
->so_state
& SS_NOFDREF
) &&
644 (revents
& (G_IO_OUT
| G_IO_ERR
))) {
646 * Check for non-blocking, still-connecting sockets
648 if (so
->so_state
& SS_ISFCONNECTING
) {
650 so
->so_state
&= ~SS_ISFCONNECTING
;
652 ret
= send(so
->s
, (const void *) &ret
, 0, 0);
654 /* XXXXX Must fix, zero bytes is a NOP */
655 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
656 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
661 so
->so_state
&= SS_PERSISTENT_MASK
;
662 so
->so_state
|= SS_NOFDREF
;
664 /* else so->so_state &= ~SS_ISFCONNECTING; */
669 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
,
676 * XXXXX If we wrote something (a lot), there
677 * could be a need for a window update.
678 * In the worst case, the remote will send
679 * a window probe to get things going again
684 * Probe a still-connecting, non-blocking socket
685 * to check if it's still alive
688 if (so
->so_state
& SS_ISFCONNECTING
) {
689 ret
= qemu_recv(so
->s
, &ret
, 0, 0);
693 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
694 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
695 continue; /* Still connecting, continue */
699 so
->so_state
&= SS_PERSISTENT_MASK
;
700 so
->so_state
|= SS_NOFDREF
;
702 /* tcp_input will take care of it */
704 ret
= send(so
->s
, &ret
, 0, 0);
707 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
708 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
712 so
->so_state
&= SS_PERSISTENT_MASK
;
713 so
->so_state
|= SS_NOFDREF
;
715 so
->so_state
&= ~SS_ISFCONNECTING
;
719 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
,
721 } /* SS_ISFCONNECTING */
727 * Incoming packets are sent straight away, they're not buffered.
728 * Incoming UDP data isn't buffered either.
730 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
734 so_next
= so
->so_next
;
737 if (so
->pollfds_idx
!= -1) {
738 revents
= g_array_index(pollfds
, GPollFD
,
739 so
->pollfds_idx
).revents
;
743 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
749 * Check incoming ICMP relies.
751 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
755 so_next
= so
->so_next
;
758 if (so
->pollfds_idx
!= -1) {
759 revents
= g_array_index(pollfds
, GPollFD
,
760 so
->pollfds_idx
).revents
;
764 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
774 static void arp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
776 struct slirp_arphdr
*ah
= (struct slirp_arphdr
*)(pkt
+ ETH_HLEN
);
777 uint8_t arp_reply
[MAX(ETH_HLEN
+ sizeof(struct slirp_arphdr
), 64)];
778 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
779 struct slirp_arphdr
*rah
= (struct slirp_arphdr
*)(arp_reply
+ ETH_HLEN
);
781 struct ex_list
*ex_ptr
;
783 if (!slirp
->in_enabled
) {
787 ar_op
= ntohs(ah
->ar_op
);
790 if (ah
->ar_tip
== ah
->ar_sip
) {
792 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
796 if ((ah
->ar_tip
& slirp
->vnetwork_mask
.s_addr
) ==
797 slirp
->vnetwork_addr
.s_addr
) {
798 if (ah
->ar_tip
== slirp
->vnameserver_addr
.s_addr
||
799 ah
->ar_tip
== slirp
->vhost_addr
.s_addr
)
801 for (ex_ptr
= slirp
->exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
802 if (ex_ptr
->ex_addr
.s_addr
== ah
->ar_tip
)
807 memset(arp_reply
, 0, sizeof(arp_reply
));
809 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
811 /* ARP request for alias/dns mac address */
812 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
813 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
814 memcpy(&reh
->h_source
[2], &ah
->ar_tip
, 4);
815 reh
->h_proto
= htons(ETH_P_ARP
);
817 rah
->ar_hrd
= htons(1);
818 rah
->ar_pro
= htons(ETH_P_IP
);
819 rah
->ar_hln
= ETH_ALEN
;
821 rah
->ar_op
= htons(ARPOP_REPLY
);
822 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
823 rah
->ar_sip
= ah
->ar_tip
;
824 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
825 rah
->ar_tip
= ah
->ar_sip
;
826 slirp_output(slirp
->opaque
, arp_reply
, sizeof(arp_reply
));
830 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
837 void slirp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
842 if (pkt_len
< ETH_HLEN
)
845 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
848 arp_input(slirp
, pkt
, pkt_len
);
855 /* Note: we add 2 to align the IP header on 4 bytes,
856 * and add the margin for the tcpiphdr overhead */
857 if (M_FREEROOM(m
) < pkt_len
+ TCPIPHDR_DELTA
+ 2) {
858 m_inc(m
, pkt_len
+ TCPIPHDR_DELTA
+ 2);
860 m
->m_len
= pkt_len
+ TCPIPHDR_DELTA
+ 2;
861 memcpy(m
->m_data
+ TCPIPHDR_DELTA
+ 2, pkt
, pkt_len
);
863 m
->m_data
+= TCPIPHDR_DELTA
+ 2 + ETH_HLEN
;
864 m
->m_len
-= TCPIPHDR_DELTA
+ 2 + ETH_HLEN
;
866 if (proto
== ETH_P_IP
) {
868 } else if (proto
== ETH_P_IPV6
) {
874 ncsi_input(slirp
, pkt
, pkt_len
);
882 /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no
883 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
886 static int if_encap4(Slirp
*slirp
, struct mbuf
*ifm
, struct ethhdr
*eh
,
887 uint8_t ethaddr
[ETH_ALEN
])
889 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
891 if (iph
->ip_dst
.s_addr
== 0) {
892 /* 0.0.0.0 can not be a destination address, something went wrong,
893 * avoid making it worse */
896 if (!arp_table_search(slirp
, iph
->ip_dst
.s_addr
, ethaddr
)) {
897 uint8_t arp_req
[ETH_HLEN
+ sizeof(struct slirp_arphdr
)];
898 struct ethhdr
*reh
= (struct ethhdr
*)arp_req
;
899 struct slirp_arphdr
*rah
= (struct slirp_arphdr
*)(arp_req
+ ETH_HLEN
);
901 if (!ifm
->resolution_requested
) {
902 /* If the client addr is not known, send an ARP request */
903 memset(reh
->h_dest
, 0xff, ETH_ALEN
);
904 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
905 memcpy(&reh
->h_source
[2], &slirp
->vhost_addr
, 4);
906 reh
->h_proto
= htons(ETH_P_ARP
);
907 rah
->ar_hrd
= htons(1);
908 rah
->ar_pro
= htons(ETH_P_IP
);
909 rah
->ar_hln
= ETH_ALEN
;
911 rah
->ar_op
= htons(ARPOP_REQUEST
);
914 memcpy(rah
->ar_sha
, special_ethaddr
, ETH_ALEN
- 4);
915 memcpy(&rah
->ar_sha
[2], &slirp
->vhost_addr
, 4);
918 rah
->ar_sip
= slirp
->vhost_addr
.s_addr
;
920 /* target hw addr (none) */
921 memset(rah
->ar_tha
, 0, ETH_ALEN
);
924 rah
->ar_tip
= iph
->ip_dst
.s_addr
;
925 slirp
->client_ipaddr
= iph
->ip_dst
;
926 slirp_output(slirp
->opaque
, arp_req
, sizeof(arp_req
));
927 ifm
->resolution_requested
= true;
929 /* Expire request and drop outgoing packet after 1 second */
930 ifm
->expiration_date
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + 1000000000ULL;
934 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
935 /* XXX: not correct */
936 memcpy(&eh
->h_source
[2], &slirp
->vhost_addr
, 4);
937 eh
->h_proto
= htons(ETH_P_IP
);
944 /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no
945 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
948 static int if_encap6(Slirp
*slirp
, struct mbuf
*ifm
, struct ethhdr
*eh
,
949 uint8_t ethaddr
[ETH_ALEN
])
951 const struct ip6
*ip6h
= mtod(ifm
, const struct ip6
*);
952 if (!ndp_table_search(slirp
, ip6h
->ip_dst
, ethaddr
)) {
953 if (!ifm
->resolution_requested
) {
954 ndp_send_ns(slirp
, ip6h
->ip_dst
);
955 ifm
->resolution_requested
= true;
956 ifm
->expiration_date
=
957 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + 1000000000ULL;
961 eh
->h_proto
= htons(ETH_P_IPV6
);
962 in6_compute_ethaddr(ip6h
->ip_src
, eh
->h_source
);
969 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
972 int if_encap(Slirp
*slirp
, struct mbuf
*ifm
)
975 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
976 uint8_t ethaddr
[ETH_ALEN
];
977 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
980 if (ifm
->m_len
+ ETH_HLEN
> sizeof(buf
)) {
986 ret
= if_encap4(slirp
, ifm
, eh
, ethaddr
);
993 ret
= if_encap6(slirp
, ifm
, eh
, ethaddr
);
1000 g_assert_not_reached();
1004 memcpy(eh
->h_dest
, ethaddr
, ETH_ALEN
);
1005 DEBUG_ARGS((dfd
, " src = %02x:%02x:%02x:%02x:%02x:%02x\n",
1006 eh
->h_source
[0], eh
->h_source
[1], eh
->h_source
[2],
1007 eh
->h_source
[3], eh
->h_source
[4], eh
->h_source
[5]));
1008 DEBUG_ARGS((dfd
, " dst = %02x:%02x:%02x:%02x:%02x:%02x\n",
1009 eh
->h_dest
[0], eh
->h_dest
[1], eh
->h_dest
[2],
1010 eh
->h_dest
[3], eh
->h_dest
[4], eh
->h_dest
[5]));
1011 memcpy(buf
+ sizeof(struct ethhdr
), ifm
->m_data
, ifm
->m_len
);
1012 slirp_output(slirp
->opaque
, buf
, ifm
->m_len
+ ETH_HLEN
);
1016 /* Drop host forwarding rule, return 0 if found. */
1017 int slirp_remove_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
1021 struct socket
*head
= (is_udp
? &slirp
->udb
: &slirp
->tcb
);
1022 struct sockaddr_in addr
;
1023 int port
= htons(host_port
);
1026 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
1027 addr_len
= sizeof(addr
);
1028 if ((so
->so_state
& SS_HOSTFWD
) &&
1029 getsockname(so
->s
, (struct sockaddr
*)&addr
, &addr_len
) == 0 &&
1030 addr
.sin_addr
.s_addr
== host_addr
.s_addr
&&
1031 addr
.sin_port
== port
) {
1041 int slirp_add_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
1042 int host_port
, struct in_addr guest_addr
, int guest_port
)
1044 if (!guest_addr
.s_addr
) {
1045 guest_addr
= slirp
->vdhcp_startaddr
;
1048 if (!udp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
1049 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
1052 if (!tcp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
1053 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
1059 int slirp_add_exec(Slirp
*slirp
, int do_pty
, const void *args
,
1060 struct in_addr
*guest_addr
, int guest_port
)
1062 if (!guest_addr
->s_addr
) {
1063 guest_addr
->s_addr
= slirp
->vnetwork_addr
.s_addr
|
1064 (htonl(0x0204) & ~slirp
->vnetwork_mask
.s_addr
);
1066 if ((guest_addr
->s_addr
& slirp
->vnetwork_mask
.s_addr
) !=
1067 slirp
->vnetwork_addr
.s_addr
||
1068 guest_addr
->s_addr
== slirp
->vhost_addr
.s_addr
||
1069 guest_addr
->s_addr
== slirp
->vnameserver_addr
.s_addr
) {
1072 return add_exec(&slirp
->exec_list
, do_pty
, (char *)args
, *guest_addr
,
1076 ssize_t
slirp_send(struct socket
*so
, const void *buf
, size_t len
, int flags
)
1078 if (so
->s
== -1 && so
->extra
) {
1079 /* XXX this blocks entire thread. Rewrite to use
1080 * qemu_chr_fe_write and background I/O callbacks */
1081 qemu_chr_fe_write_all(so
->extra
, buf
, len
);
1085 return send(so
->s
, buf
, len
, flags
);
1088 static struct socket
*
1089 slirp_find_ctl_socket(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
)
1093 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
1094 if (so
->so_faddr
.s_addr
== guest_addr
.s_addr
&&
1095 htons(so
->so_fport
) == guest_port
) {
1102 size_t slirp_socket_can_recv(Slirp
*slirp
, struct in_addr guest_addr
,
1105 struct iovec iov
[2];
1108 so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
1110 if (!so
|| so
->so_state
& SS_NOFDREF
) {
1114 if (!CONN_CANFRCV(so
) || so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_datalen
/2)) {
1118 return sopreprbuf(so
, iov
, NULL
);
1121 void slirp_socket_recv(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
,
1122 const uint8_t *buf
, int size
)
1125 struct socket
*so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
1130 ret
= soreadbuf(so
, (const char *)buf
, size
);
1133 tcp_output(sototcpcb(so
));
1136 static void slirp_tcp_save(QEMUFile
*f
, struct tcpcb
*tp
)
1140 qemu_put_sbe16(f
, tp
->t_state
);
1141 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
1142 qemu_put_sbe16(f
, tp
->t_timer
[i
]);
1143 qemu_put_sbe16(f
, tp
->t_rxtshift
);
1144 qemu_put_sbe16(f
, tp
->t_rxtcur
);
1145 qemu_put_sbe16(f
, tp
->t_dupacks
);
1146 qemu_put_be16(f
, tp
->t_maxseg
);
1147 qemu_put_sbyte(f
, tp
->t_force
);
1148 qemu_put_be16(f
, tp
->t_flags
);
1149 qemu_put_be32(f
, tp
->snd_una
);
1150 qemu_put_be32(f
, tp
->snd_nxt
);
1151 qemu_put_be32(f
, tp
->snd_up
);
1152 qemu_put_be32(f
, tp
->snd_wl1
);
1153 qemu_put_be32(f
, tp
->snd_wl2
);
1154 qemu_put_be32(f
, tp
->iss
);
1155 qemu_put_be32(f
, tp
->snd_wnd
);
1156 qemu_put_be32(f
, tp
->rcv_wnd
);
1157 qemu_put_be32(f
, tp
->rcv_nxt
);
1158 qemu_put_be32(f
, tp
->rcv_up
);
1159 qemu_put_be32(f
, tp
->irs
);
1160 qemu_put_be32(f
, tp
->rcv_adv
);
1161 qemu_put_be32(f
, tp
->snd_max
);
1162 qemu_put_be32(f
, tp
->snd_cwnd
);
1163 qemu_put_be32(f
, tp
->snd_ssthresh
);
1164 qemu_put_sbe16(f
, tp
->t_idle
);
1165 qemu_put_sbe16(f
, tp
->t_rtt
);
1166 qemu_put_be32(f
, tp
->t_rtseq
);
1167 qemu_put_sbe16(f
, tp
->t_srtt
);
1168 qemu_put_sbe16(f
, tp
->t_rttvar
);
1169 qemu_put_be16(f
, tp
->t_rttmin
);
1170 qemu_put_be32(f
, tp
->max_sndwnd
);
1171 qemu_put_byte(f
, tp
->t_oobflags
);
1172 qemu_put_byte(f
, tp
->t_iobc
);
1173 qemu_put_sbe16(f
, tp
->t_softerror
);
1174 qemu_put_byte(f
, tp
->snd_scale
);
1175 qemu_put_byte(f
, tp
->rcv_scale
);
1176 qemu_put_byte(f
, tp
->request_r_scale
);
1177 qemu_put_byte(f
, tp
->requested_s_scale
);
1178 qemu_put_be32(f
, tp
->ts_recent
);
1179 qemu_put_be32(f
, tp
->ts_recent_age
);
1180 qemu_put_be32(f
, tp
->last_ack_sent
);
1183 static void slirp_sbuf_save(QEMUFile
*f
, struct sbuf
*sbuf
)
1187 qemu_put_be32(f
, sbuf
->sb_cc
);
1188 qemu_put_be32(f
, sbuf
->sb_datalen
);
1189 off
= (uint32_t)(sbuf
->sb_wptr
- sbuf
->sb_data
);
1190 qemu_put_sbe32(f
, off
);
1191 off
= (uint32_t)(sbuf
->sb_rptr
- sbuf
->sb_data
);
1192 qemu_put_sbe32(f
, off
);
1193 qemu_put_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
1196 static void slirp_socket_save(QEMUFile
*f
, struct socket
*so
)
1198 qemu_put_be32(f
, so
->so_urgc
);
1199 qemu_put_be16(f
, so
->so_ffamily
);
1200 switch (so
->so_ffamily
) {
1202 qemu_put_be32(f
, so
->so_faddr
.s_addr
);
1203 qemu_put_be16(f
, so
->so_fport
);
1206 error_report("so_ffamily unknown, unable to save so_faddr and"
1209 qemu_put_be16(f
, so
->so_lfamily
);
1210 switch (so
->so_lfamily
) {
1212 qemu_put_be32(f
, so
->so_laddr
.s_addr
);
1213 qemu_put_be16(f
, so
->so_lport
);
1216 error_report("so_ffamily unknown, unable to save so_laddr and"
1219 qemu_put_byte(f
, so
->so_iptos
);
1220 qemu_put_byte(f
, so
->so_emu
);
1221 qemu_put_byte(f
, so
->so_type
);
1222 qemu_put_be32(f
, so
->so_state
);
1223 slirp_sbuf_save(f
, &so
->so_rcv
);
1224 slirp_sbuf_save(f
, &so
->so_snd
);
1225 slirp_tcp_save(f
, so
->so_tcpcb
);
1228 static void slirp_bootp_save(QEMUFile
*f
, Slirp
*slirp
)
1232 for (i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
1233 qemu_put_be16(f
, slirp
->bootp_clients
[i
].allocated
);
1234 qemu_put_buffer(f
, slirp
->bootp_clients
[i
].macaddr
, 6);
1238 static void slirp_state_save(QEMUFile
*f
, void *opaque
)
1240 Slirp
*slirp
= opaque
;
1241 struct ex_list
*ex_ptr
;
1243 for (ex_ptr
= slirp
->exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
1244 if (ex_ptr
->ex_pty
== 3) {
1246 so
= slirp_find_ctl_socket(slirp
, ex_ptr
->ex_addr
,
1247 ntohs(ex_ptr
->ex_fport
));
1251 qemu_put_byte(f
, 42);
1252 slirp_socket_save(f
, so
);
1254 qemu_put_byte(f
, 0);
1256 qemu_put_be16(f
, slirp
->ip_id
);
1258 slirp_bootp_save(f
, slirp
);
1261 static void slirp_tcp_load(QEMUFile
*f
, struct tcpcb
*tp
)
1265 tp
->t_state
= qemu_get_sbe16(f
);
1266 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
1267 tp
->t_timer
[i
] = qemu_get_sbe16(f
);
1268 tp
->t_rxtshift
= qemu_get_sbe16(f
);
1269 tp
->t_rxtcur
= qemu_get_sbe16(f
);
1270 tp
->t_dupacks
= qemu_get_sbe16(f
);
1271 tp
->t_maxseg
= qemu_get_be16(f
);
1272 tp
->t_force
= qemu_get_sbyte(f
);
1273 tp
->t_flags
= qemu_get_be16(f
);
1274 tp
->snd_una
= qemu_get_be32(f
);
1275 tp
->snd_nxt
= qemu_get_be32(f
);
1276 tp
->snd_up
= qemu_get_be32(f
);
1277 tp
->snd_wl1
= qemu_get_be32(f
);
1278 tp
->snd_wl2
= qemu_get_be32(f
);
1279 tp
->iss
= qemu_get_be32(f
);
1280 tp
->snd_wnd
= qemu_get_be32(f
);
1281 tp
->rcv_wnd
= qemu_get_be32(f
);
1282 tp
->rcv_nxt
= qemu_get_be32(f
);
1283 tp
->rcv_up
= qemu_get_be32(f
);
1284 tp
->irs
= qemu_get_be32(f
);
1285 tp
->rcv_adv
= qemu_get_be32(f
);
1286 tp
->snd_max
= qemu_get_be32(f
);
1287 tp
->snd_cwnd
= qemu_get_be32(f
);
1288 tp
->snd_ssthresh
= qemu_get_be32(f
);
1289 tp
->t_idle
= qemu_get_sbe16(f
);
1290 tp
->t_rtt
= qemu_get_sbe16(f
);
1291 tp
->t_rtseq
= qemu_get_be32(f
);
1292 tp
->t_srtt
= qemu_get_sbe16(f
);
1293 tp
->t_rttvar
= qemu_get_sbe16(f
);
1294 tp
->t_rttmin
= qemu_get_be16(f
);
1295 tp
->max_sndwnd
= qemu_get_be32(f
);
1296 tp
->t_oobflags
= qemu_get_byte(f
);
1297 tp
->t_iobc
= qemu_get_byte(f
);
1298 tp
->t_softerror
= qemu_get_sbe16(f
);
1299 tp
->snd_scale
= qemu_get_byte(f
);
1300 tp
->rcv_scale
= qemu_get_byte(f
);
1301 tp
->request_r_scale
= qemu_get_byte(f
);
1302 tp
->requested_s_scale
= qemu_get_byte(f
);
1303 tp
->ts_recent
= qemu_get_be32(f
);
1304 tp
->ts_recent_age
= qemu_get_be32(f
);
1305 tp
->last_ack_sent
= qemu_get_be32(f
);
1309 static int slirp_sbuf_load(QEMUFile
*f
, struct sbuf
*sbuf
)
1311 uint32_t off
, sb_cc
, sb_datalen
;
1313 sb_cc
= qemu_get_be32(f
);
1314 sb_datalen
= qemu_get_be32(f
);
1316 sbreserve(sbuf
, sb_datalen
);
1318 if (sbuf
->sb_datalen
!= sb_datalen
)
1321 sbuf
->sb_cc
= sb_cc
;
1323 off
= qemu_get_sbe32(f
);
1324 sbuf
->sb_wptr
= sbuf
->sb_data
+ off
;
1325 off
= qemu_get_sbe32(f
);
1326 sbuf
->sb_rptr
= sbuf
->sb_data
+ off
;
1327 qemu_get_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
1332 static int slirp_socket_load(QEMUFile
*f
, struct socket
*so
, int version_id
)
1334 if (tcp_attach(so
) < 0)
1337 so
->so_urgc
= qemu_get_be32(f
);
1338 if (version_id
<= 3) {
1339 so
->so_ffamily
= AF_INET
;
1340 so
->so_faddr
.s_addr
= qemu_get_be32(f
);
1341 so
->so_laddr
.s_addr
= qemu_get_be32(f
);
1342 so
->so_fport
= qemu_get_be16(f
);
1343 so
->so_lport
= qemu_get_be16(f
);
1345 so
->so_ffamily
= qemu_get_be16(f
);
1346 switch (so
->so_ffamily
) {
1348 so
->so_faddr
.s_addr
= qemu_get_be32(f
);
1349 so
->so_fport
= qemu_get_be16(f
);
1353 "so_ffamily unknown, unable to restore so_faddr and so_lport");
1355 so
->so_lfamily
= qemu_get_be16(f
);
1356 switch (so
->so_lfamily
) {
1358 so
->so_laddr
.s_addr
= qemu_get_be32(f
);
1359 so
->so_lport
= qemu_get_be16(f
);
1363 "so_ffamily unknown, unable to restore so_laddr and so_lport");
1366 so
->so_iptos
= qemu_get_byte(f
);
1367 so
->so_emu
= qemu_get_byte(f
);
1368 so
->so_type
= qemu_get_byte(f
);
1369 so
->so_state
= qemu_get_be32(f
);
1370 if (slirp_sbuf_load(f
, &so
->so_rcv
) < 0)
1372 if (slirp_sbuf_load(f
, &so
->so_snd
) < 0)
1374 slirp_tcp_load(f
, so
->so_tcpcb
);
1379 static void slirp_bootp_load(QEMUFile
*f
, Slirp
*slirp
)
1383 for (i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
1384 slirp
->bootp_clients
[i
].allocated
= qemu_get_be16(f
);
1385 qemu_get_buffer(f
, slirp
->bootp_clients
[i
].macaddr
, 6);
1389 static int slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
)
1391 Slirp
*slirp
= opaque
;
1392 struct ex_list
*ex_ptr
;
1394 while (qemu_get_byte(f
)) {
1396 struct socket
*so
= socreate(slirp
);
1401 ret
= slirp_socket_load(f
, so
, version_id
);
1406 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) !=
1407 slirp
->vnetwork_addr
.s_addr
) {
1410 for (ex_ptr
= slirp
->exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
1411 if (ex_ptr
->ex_pty
== 3 &&
1412 so
->so_faddr
.s_addr
== ex_ptr
->ex_addr
.s_addr
&&
1413 so
->so_fport
== ex_ptr
->ex_fport
) {
1420 so
->extra
= (void *)ex_ptr
->ex_exec
;
1423 if (version_id
>= 2) {
1424 slirp
->ip_id
= qemu_get_be16(f
);
1427 if (version_id
>= 3) {
1428 slirp_bootp_load(f
, slirp
);