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"
32 /* host loopback address */
33 struct in_addr loopback_addr
;
34 /* host loopback network mask */
35 unsigned long loopback_mask
;
37 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
38 static const uint8_t special_ethaddr
[ETH_ALEN
] = {
39 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
44 static QTAILQ_HEAD(slirp_instances
, Slirp
) slirp_instances
=
45 QTAILQ_HEAD_INITIALIZER(slirp_instances
);
47 static struct in_addr dns_addr
;
48 static u_int dns_addr_time
;
50 #define TIMEOUT_FAST 2 /* milliseconds */
51 #define TIMEOUT_SLOW 499 /* milliseconds */
52 /* for the aging of certain requests like DNS */
53 #define TIMEOUT_DEFAULT 1000 /* milliseconds */
57 int get_dns_addr(struct in_addr
*pdns_addr
)
59 FIXED_INFO
*FixedInfo
=NULL
;
62 IP_ADDR_STRING
*pIPAddr
;
63 struct in_addr tmp_addr
;
65 if (dns_addr
.s_addr
!= 0 && (curtime
- dns_addr_time
) < TIMEOUT_DEFAULT
) {
66 *pdns_addr
= dns_addr
;
70 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
71 BufLen
= sizeof(FIXED_INFO
);
73 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
75 GlobalFree(FixedInfo
);
78 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
81 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
82 printf("GetNetworkParams failed. ret = %08x\n", (u_int
)ret
);
84 GlobalFree(FixedInfo
);
90 pIPAddr
= &(FixedInfo
->DnsServerList
);
91 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
92 *pdns_addr
= tmp_addr
;
94 dns_addr_time
= curtime
;
96 GlobalFree(FixedInfo
);
102 static void winsock_cleanup(void)
109 static struct stat dns_addr_stat
;
111 int get_dns_addr(struct in_addr
*pdns_addr
)
117 struct in_addr tmp_addr
;
119 if (dns_addr
.s_addr
!= 0) {
120 struct stat old_stat
;
121 if ((curtime
- dns_addr_time
) < TIMEOUT_DEFAULT
) {
122 *pdns_addr
= dns_addr
;
125 old_stat
= dns_addr_stat
;
126 if (stat("/etc/resolv.conf", &dns_addr_stat
) != 0)
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
;
137 f
= fopen("/etc/resolv.conf", "r");
142 fprintf(stderr
, "IP address of your DNS(s): ");
144 while (fgets(buff
, 512, f
) != NULL
) {
145 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
146 if (!inet_aton(buff2
, &tmp_addr
))
148 /* If it's the first one, set it to dns_addr */
150 *pdns_addr
= tmp_addr
;
152 dns_addr_time
= curtime
;
156 fprintf(stderr
, ", ");
160 fprintf(stderr
, "(more)");
166 fprintf(stderr
, "%s", inet_ntoa(tmp_addr
));
178 static void slirp_init_once(void)
180 static int initialized
;
191 WSAStartup(MAKEWORD(2,0), &Data
);
192 atexit(winsock_cleanup
);
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
,
209 Slirp
*slirp
= g_malloc0(sizeof(Slirp
));
213 slirp
->restricted
= restricted
;
218 /* Initialise mbufs *after* setting the MTU */
221 slirp
->vnetwork_addr
= vnetwork
;
222 slirp
->vnetwork_mask
= vnetmask
;
223 slirp
->vhost_addr
= vhost
;
225 pstrcpy(slirp
->client_hostname
, sizeof(slirp
->client_hostname
),
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
;
234 translate_dnssearch(slirp
, vdnssearch
);
237 slirp
->opaque
= opaque
;
239 register_savevm(NULL
, "slirp", 0, 4,
240 slirp_state_save
, slirp_state_load
, slirp
);
242 QTAILQ_INSERT_TAIL(&slirp_instances
, slirp
, entry
);
247 void slirp_cleanup(Slirp
*slirp
)
249 QTAILQ_REMOVE(&slirp_instances
, slirp
, entry
);
251 unregister_savevm(NULL
, "slirp", slirp
);
256 g_free(slirp
->vdnssearch
);
257 g_free(slirp
->tftp_prefix
);
258 g_free(slirp
->bootp_filename
);
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)
265 static void slirp_update_timeout(uint32_t *timeout
)
270 if (*timeout
<= TIMEOUT_FAST
) {
274 t
= MIN(1000, *timeout
);
276 /* If we have tcp timeout with slirp, then we will fill @timeout with
277 * more precise value.
279 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
280 if (slirp
->time_fasttimo
) {
281 *timeout
= TIMEOUT_FAST
;
284 if (slirp
->do_slowtimo
) {
285 t
= MIN(TIMEOUT_SLOW
, t
);
291 void slirp_pollfds_fill(GArray
*pollfds
, uint32_t *timeout
)
294 struct socket
*so
, *so_next
;
296 if (QTAILQ_EMPTY(&slirp_instances
)) {
304 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
306 * *_slowtimo needs calling if there are IP fragments
307 * in the fragment queue, or there are TCP connections active
309 slirp
->do_slowtimo
= ((slirp
->tcb
.so_next
!= &slirp
->tcb
) ||
310 (&slirp
->ipq
.ip_link
!= slirp
->ipq
.ip_link
.next
));
312 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
316 so_next
= so
->so_next
;
318 so
->pollfds_idx
= -1;
321 * See if we need a tcp_fasttimo
323 if (slirp
->time_fasttimo
== 0 &&
324 so
->so_tcpcb
->t_flags
& TF_DELACK
) {
325 slirp
->time_fasttimo
= curtime
; /* Flag when want a fasttimo */
329 * NOFDREF can include still connecting to local-host,
330 * newly socreated() sockets etc. Don't want to select these.
332 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
337 * Set for reading sockets which are accepting
339 if (so
->so_state
& SS_FACCEPTCONN
) {
342 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
344 so
->pollfds_idx
= pollfds
->len
;
345 g_array_append_val(pollfds
, pfd
);
350 * Set for writing sockets which are connecting
352 if (so
->so_state
& SS_ISFCONNECTING
) {
355 .events
= G_IO_OUT
| G_IO_ERR
,
357 so
->pollfds_idx
= pollfds
->len
;
358 g_array_append_val(pollfds
, pfd
);
363 * Set for writing if we are connected, can send more, and
364 * we have something to send
366 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
367 events
|= G_IO_OUT
| G_IO_ERR
;
371 * Set for reading (and urgent data) if we are connected, can
372 * receive more, and we have room for it XXX /2 ?
374 if (CONN_CANFRCV(so
) &&
375 (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
376 events
|= G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_PRI
;
384 so
->pollfds_idx
= pollfds
->len
;
385 g_array_append_val(pollfds
, pfd
);
392 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
394 so_next
= so
->so_next
;
396 so
->pollfds_idx
= -1;
399 * See if it's timed out
402 if (so
->so_expire
<= curtime
) {
406 slirp
->do_slowtimo
= true; /* Let socket expire */
411 * When UDP packets are received from over the
412 * link, they're sendto()'d straight away, so
413 * no need for setting for writing
414 * Limit the number of packets queued by this session
415 * to 4. Note that even though we try and limit this
416 * to 4 packets, the session could have more queued
417 * if the packets needed to be fragmented
420 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
423 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
425 so
->pollfds_idx
= pollfds
->len
;
426 g_array_append_val(pollfds
, pfd
);
433 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
435 so_next
= so
->so_next
;
437 so
->pollfds_idx
= -1;
440 * See if it's timed out
443 if (so
->so_expire
<= curtime
) {
447 slirp
->do_slowtimo
= true; /* Let socket expire */
451 if (so
->so_state
& SS_ISFCONNECTED
) {
454 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
456 so
->pollfds_idx
= pollfds
->len
;
457 g_array_append_val(pollfds
, pfd
);
461 slirp_update_timeout(timeout
);
464 void slirp_pollfds_poll(GArray
*pollfds
, int select_error
)
467 struct socket
*so
, *so_next
;
470 if (QTAILQ_EMPTY(&slirp_instances
)) {
474 curtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
476 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
478 * See if anything has timed out
480 if (slirp
->time_fasttimo
&&
481 ((curtime
- slirp
->time_fasttimo
) >= TIMEOUT_FAST
)) {
483 slirp
->time_fasttimo
= 0;
485 if (slirp
->do_slowtimo
&&
486 ((curtime
- slirp
->last_slowtimo
) >= TIMEOUT_SLOW
)) {
489 slirp
->last_slowtimo
= curtime
;
499 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
503 so_next
= so
->so_next
;
506 if (so
->pollfds_idx
!= -1) {
507 revents
= g_array_index(pollfds
, GPollFD
,
508 so
->pollfds_idx
).revents
;
511 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
517 * This will soread as well, so no need to
518 * test for G_IO_IN below if this succeeds
520 if (revents
& G_IO_PRI
) {
524 * Check sockets for reading
526 else if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
)) {
528 * Check for incoming connections
530 if (so
->so_state
& SS_FACCEPTCONN
) {
536 /* Output it if we read something */
538 tcp_output(sototcpcb(so
));
543 * Check sockets for writing
545 if (!(so
->so_state
& SS_NOFDREF
) &&
546 (revents
& (G_IO_OUT
| G_IO_ERR
))) {
548 * Check for non-blocking, still-connecting sockets
550 if (so
->so_state
& SS_ISFCONNECTING
) {
552 so
->so_state
&= ~SS_ISFCONNECTING
;
554 ret
= send(so
->s
, (const void *) &ret
, 0, 0);
556 /* XXXXX Must fix, zero bytes is a NOP */
557 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
558 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
563 so
->so_state
&= SS_PERSISTENT_MASK
;
564 so
->so_state
|= SS_NOFDREF
;
566 /* else so->so_state &= ~SS_ISFCONNECTING; */
571 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
577 * XXXXX If we wrote something (a lot), there
578 * could be a need for a window update.
579 * In the worst case, the remote will send
580 * a window probe to get things going again
585 * Probe a still-connecting, non-blocking socket
586 * to check if it's still alive
589 if (so
->so_state
& SS_ISFCONNECTING
) {
590 ret
= qemu_recv(so
->s
, &ret
, 0, 0);
594 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
595 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
596 continue; /* Still connecting, continue */
600 so
->so_state
&= SS_PERSISTENT_MASK
;
601 so
->so_state
|= SS_NOFDREF
;
603 /* tcp_input will take care of it */
605 ret
= send(so
->s
, &ret
, 0, 0);
608 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
609 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
613 so
->so_state
&= SS_PERSISTENT_MASK
;
614 so
->so_state
|= SS_NOFDREF
;
616 so
->so_state
&= ~SS_ISFCONNECTING
;
620 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
621 } /* SS_ISFCONNECTING */
627 * Incoming packets are sent straight away, they're not buffered.
628 * Incoming UDP data isn't buffered either.
630 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
634 so_next
= so
->so_next
;
637 if (so
->pollfds_idx
!= -1) {
638 revents
= g_array_index(pollfds
, GPollFD
,
639 so
->pollfds_idx
).revents
;
643 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
649 * Check incoming ICMP relies.
651 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
655 so_next
= so
->so_next
;
658 if (so
->pollfds_idx
!= -1) {
659 revents
= g_array_index(pollfds
, GPollFD
,
660 so
->pollfds_idx
).revents
;
664 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
674 static void arp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
676 struct arphdr
*ah
= (struct arphdr
*)(pkt
+ ETH_HLEN
);
677 uint8_t arp_reply
[max(ETH_HLEN
+ sizeof(struct arphdr
), 64)];
678 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
679 struct arphdr
*rah
= (struct arphdr
*)(arp_reply
+ ETH_HLEN
);
681 struct ex_list
*ex_ptr
;
683 ar_op
= ntohs(ah
->ar_op
);
686 if (ah
->ar_tip
== ah
->ar_sip
) {
688 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
692 if ((ah
->ar_tip
& slirp
->vnetwork_mask
.s_addr
) ==
693 slirp
->vnetwork_addr
.s_addr
) {
694 if (ah
->ar_tip
== slirp
->vnameserver_addr
.s_addr
||
695 ah
->ar_tip
== slirp
->vhost_addr
.s_addr
)
697 for (ex_ptr
= slirp
->exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
698 if (ex_ptr
->ex_addr
.s_addr
== ah
->ar_tip
)
703 memset(arp_reply
, 0, sizeof(arp_reply
));
705 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
707 /* ARP request for alias/dns mac address */
708 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
709 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
710 memcpy(&reh
->h_source
[2], &ah
->ar_tip
, 4);
711 reh
->h_proto
= htons(ETH_P_ARP
);
713 rah
->ar_hrd
= htons(1);
714 rah
->ar_pro
= htons(ETH_P_IP
);
715 rah
->ar_hln
= ETH_ALEN
;
717 rah
->ar_op
= htons(ARPOP_REPLY
);
718 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
719 rah
->ar_sip
= ah
->ar_tip
;
720 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
721 rah
->ar_tip
= ah
->ar_sip
;
722 slirp_output(slirp
->opaque
, arp_reply
, sizeof(arp_reply
));
726 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
733 void slirp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
738 if (pkt_len
< ETH_HLEN
)
741 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
744 arp_input(slirp
, pkt
, pkt_len
);
750 /* Note: we add to align the IP header */
751 if (M_FREEROOM(m
) < pkt_len
+ 2) {
752 m_inc(m
, pkt_len
+ 2);
754 m
->m_len
= pkt_len
+ 2;
755 memcpy(m
->m_data
+ 2, pkt
, pkt_len
);
757 m
->m_data
+= 2 + ETH_HLEN
;
758 m
->m_len
-= 2 + ETH_HLEN
;
767 /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no
768 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
771 static int if_encap4(Slirp
*slirp
, struct mbuf
*ifm
, struct ethhdr
*eh
,
772 uint8_t ethaddr
[ETH_ALEN
])
774 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
776 if (iph
->ip_dst
.s_addr
== 0) {
777 /* 0.0.0.0 can not be a destination address, something went wrong,
778 * avoid making it worse */
781 if (!arp_table_search(slirp
, iph
->ip_dst
.s_addr
, ethaddr
)) {
782 uint8_t arp_req
[ETH_HLEN
+ sizeof(struct arphdr
)];
783 struct ethhdr
*reh
= (struct ethhdr
*)arp_req
;
784 struct arphdr
*rah
= (struct arphdr
*)(arp_req
+ ETH_HLEN
);
786 if (!ifm
->resolution_requested
) {
787 /* If the client addr is not known, send an ARP request */
788 memset(reh
->h_dest
, 0xff, ETH_ALEN
);
789 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
790 memcpy(&reh
->h_source
[2], &slirp
->vhost_addr
, 4);
791 reh
->h_proto
= htons(ETH_P_ARP
);
792 rah
->ar_hrd
= htons(1);
793 rah
->ar_pro
= htons(ETH_P_IP
);
794 rah
->ar_hln
= ETH_ALEN
;
796 rah
->ar_op
= htons(ARPOP_REQUEST
);
799 memcpy(rah
->ar_sha
, special_ethaddr
, ETH_ALEN
- 4);
800 memcpy(&rah
->ar_sha
[2], &slirp
->vhost_addr
, 4);
803 rah
->ar_sip
= slirp
->vhost_addr
.s_addr
;
805 /* target hw addr (none) */
806 memset(rah
->ar_tha
, 0, ETH_ALEN
);
809 rah
->ar_tip
= iph
->ip_dst
.s_addr
;
810 slirp
->client_ipaddr
= iph
->ip_dst
;
811 slirp_output(slirp
->opaque
, arp_req
, sizeof(arp_req
));
812 ifm
->resolution_requested
= true;
814 /* Expire request and drop outgoing packet after 1 second */
815 ifm
->expiration_date
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + 1000000000ULL;
819 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
820 /* XXX: not correct */
821 memcpy(&eh
->h_source
[2], &slirp
->vhost_addr
, 4);
822 eh
->h_proto
= htons(ETH_P_IP
);
829 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
832 int if_encap(Slirp
*slirp
, struct mbuf
*ifm
)
835 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
836 uint8_t ethaddr
[ETH_ALEN
];
837 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
840 if (ifm
->m_len
+ ETH_HLEN
> sizeof(buf
)) {
846 ret
= if_encap4(slirp
, ifm
, eh
, ethaddr
);
853 /* Do not assert while we don't manage IP6VERSION */
858 memcpy(eh
->h_dest
, ethaddr
, ETH_ALEN
);
859 DEBUG_ARGS((dfd
, " src = %02x:%02x:%02x:%02x:%02x:%02x\n",
860 eh
->h_source
[0], eh
->h_source
[1], eh
->h_source
[2],
861 eh
->h_source
[3], eh
->h_source
[4], eh
->h_source
[5]));
862 DEBUG_ARGS((dfd
, " dst = %02x:%02x:%02x:%02x:%02x:%02x\n",
863 eh
->h_dest
[0], eh
->h_dest
[1], eh
->h_dest
[2],
864 eh
->h_dest
[3], eh
->h_dest
[4], eh
->h_dest
[5]));
865 memcpy(buf
+ sizeof(struct ethhdr
), ifm
->m_data
, ifm
->m_len
);
866 slirp_output(slirp
->opaque
, buf
, ifm
->m_len
+ ETH_HLEN
);
870 /* Drop host forwarding rule, return 0 if found. */
871 int slirp_remove_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
875 struct socket
*head
= (is_udp
? &slirp
->udb
: &slirp
->tcb
);
876 struct sockaddr_in addr
;
877 int port
= htons(host_port
);
880 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
881 addr_len
= sizeof(addr
);
882 if ((so
->so_state
& SS_HOSTFWD
) &&
883 getsockname(so
->s
, (struct sockaddr
*)&addr
, &addr_len
) == 0 &&
884 addr
.sin_addr
.s_addr
== host_addr
.s_addr
&&
885 addr
.sin_port
== port
) {
895 int slirp_add_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
896 int host_port
, struct in_addr guest_addr
, int guest_port
)
898 if (!guest_addr
.s_addr
) {
899 guest_addr
= slirp
->vdhcp_startaddr
;
902 if (!udp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
903 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
906 if (!tcp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
907 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
913 int slirp_add_exec(Slirp
*slirp
, int do_pty
, const void *args
,
914 struct in_addr
*guest_addr
, int guest_port
)
916 if (!guest_addr
->s_addr
) {
917 guest_addr
->s_addr
= slirp
->vnetwork_addr
.s_addr
|
918 (htonl(0x0204) & ~slirp
->vnetwork_mask
.s_addr
);
920 if ((guest_addr
->s_addr
& slirp
->vnetwork_mask
.s_addr
) !=
921 slirp
->vnetwork_addr
.s_addr
||
922 guest_addr
->s_addr
== slirp
->vhost_addr
.s_addr
||
923 guest_addr
->s_addr
== slirp
->vnameserver_addr
.s_addr
) {
926 return add_exec(&slirp
->exec_list
, do_pty
, (char *)args
, *guest_addr
,
930 ssize_t
slirp_send(struct socket
*so
, const void *buf
, size_t len
, int flags
)
932 if (so
->s
== -1 && so
->extra
) {
933 qemu_chr_fe_write(so
->extra
, buf
, len
);
937 return send(so
->s
, buf
, len
, flags
);
940 static struct socket
*
941 slirp_find_ctl_socket(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
)
945 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
946 if (so
->so_faddr
.s_addr
== guest_addr
.s_addr
&&
947 htons(so
->so_fport
) == guest_port
) {
954 size_t slirp_socket_can_recv(Slirp
*slirp
, struct in_addr guest_addr
,
960 so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
962 if (!so
|| so
->so_state
& SS_NOFDREF
) {
966 if (!CONN_CANFRCV(so
) || so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_datalen
/2)) {
970 return sopreprbuf(so
, iov
, NULL
);
973 void slirp_socket_recv(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
,
974 const uint8_t *buf
, int size
)
977 struct socket
*so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
982 ret
= soreadbuf(so
, (const char *)buf
, size
);
985 tcp_output(sototcpcb(so
));
988 static void slirp_tcp_save(QEMUFile
*f
, struct tcpcb
*tp
)
992 qemu_put_sbe16(f
, tp
->t_state
);
993 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
994 qemu_put_sbe16(f
, tp
->t_timer
[i
]);
995 qemu_put_sbe16(f
, tp
->t_rxtshift
);
996 qemu_put_sbe16(f
, tp
->t_rxtcur
);
997 qemu_put_sbe16(f
, tp
->t_dupacks
);
998 qemu_put_be16(f
, tp
->t_maxseg
);
999 qemu_put_sbyte(f
, tp
->t_force
);
1000 qemu_put_be16(f
, tp
->t_flags
);
1001 qemu_put_be32(f
, tp
->snd_una
);
1002 qemu_put_be32(f
, tp
->snd_nxt
);
1003 qemu_put_be32(f
, tp
->snd_up
);
1004 qemu_put_be32(f
, tp
->snd_wl1
);
1005 qemu_put_be32(f
, tp
->snd_wl2
);
1006 qemu_put_be32(f
, tp
->iss
);
1007 qemu_put_be32(f
, tp
->snd_wnd
);
1008 qemu_put_be32(f
, tp
->rcv_wnd
);
1009 qemu_put_be32(f
, tp
->rcv_nxt
);
1010 qemu_put_be32(f
, tp
->rcv_up
);
1011 qemu_put_be32(f
, tp
->irs
);
1012 qemu_put_be32(f
, tp
->rcv_adv
);
1013 qemu_put_be32(f
, tp
->snd_max
);
1014 qemu_put_be32(f
, tp
->snd_cwnd
);
1015 qemu_put_be32(f
, tp
->snd_ssthresh
);
1016 qemu_put_sbe16(f
, tp
->t_idle
);
1017 qemu_put_sbe16(f
, tp
->t_rtt
);
1018 qemu_put_be32(f
, tp
->t_rtseq
);
1019 qemu_put_sbe16(f
, tp
->t_srtt
);
1020 qemu_put_sbe16(f
, tp
->t_rttvar
);
1021 qemu_put_be16(f
, tp
->t_rttmin
);
1022 qemu_put_be32(f
, tp
->max_sndwnd
);
1023 qemu_put_byte(f
, tp
->t_oobflags
);
1024 qemu_put_byte(f
, tp
->t_iobc
);
1025 qemu_put_sbe16(f
, tp
->t_softerror
);
1026 qemu_put_byte(f
, tp
->snd_scale
);
1027 qemu_put_byte(f
, tp
->rcv_scale
);
1028 qemu_put_byte(f
, tp
->request_r_scale
);
1029 qemu_put_byte(f
, tp
->requested_s_scale
);
1030 qemu_put_be32(f
, tp
->ts_recent
);
1031 qemu_put_be32(f
, tp
->ts_recent_age
);
1032 qemu_put_be32(f
, tp
->last_ack_sent
);
1035 static void slirp_sbuf_save(QEMUFile
*f
, struct sbuf
*sbuf
)
1039 qemu_put_be32(f
, sbuf
->sb_cc
);
1040 qemu_put_be32(f
, sbuf
->sb_datalen
);
1041 off
= (uint32_t)(sbuf
->sb_wptr
- sbuf
->sb_data
);
1042 qemu_put_sbe32(f
, off
);
1043 off
= (uint32_t)(sbuf
->sb_rptr
- sbuf
->sb_data
);
1044 qemu_put_sbe32(f
, off
);
1045 qemu_put_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
1048 static void slirp_socket_save(QEMUFile
*f
, struct socket
*so
)
1050 qemu_put_be32(f
, so
->so_urgc
);
1051 qemu_put_be16(f
, so
->so_ffamily
);
1052 switch (so
->so_ffamily
) {
1054 qemu_put_be32(f
, so
->so_faddr
.s_addr
);
1055 qemu_put_be16(f
, so
->so_fport
);
1059 "so_ffamily unknown, unable to save so_faddr and so_fport\n");
1061 qemu_put_be16(f
, so
->so_lfamily
);
1062 switch (so
->so_lfamily
) {
1064 qemu_put_be32(f
, so
->so_laddr
.s_addr
);
1065 qemu_put_be16(f
, so
->so_lport
);
1069 "so_ffamily unknown, unable to save so_laddr and so_lport\n");
1071 qemu_put_byte(f
, so
->so_iptos
);
1072 qemu_put_byte(f
, so
->so_emu
);
1073 qemu_put_byte(f
, so
->so_type
);
1074 qemu_put_be32(f
, so
->so_state
);
1075 slirp_sbuf_save(f
, &so
->so_rcv
);
1076 slirp_sbuf_save(f
, &so
->so_snd
);
1077 slirp_tcp_save(f
, so
->so_tcpcb
);
1080 static void slirp_bootp_save(QEMUFile
*f
, Slirp
*slirp
)
1084 for (i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
1085 qemu_put_be16(f
, slirp
->bootp_clients
[i
].allocated
);
1086 qemu_put_buffer(f
, slirp
->bootp_clients
[i
].macaddr
, 6);
1090 static void slirp_state_save(QEMUFile
*f
, void *opaque
)
1092 Slirp
*slirp
= opaque
;
1093 struct ex_list
*ex_ptr
;
1095 for (ex_ptr
= slirp
->exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
1096 if (ex_ptr
->ex_pty
== 3) {
1098 so
= slirp_find_ctl_socket(slirp
, ex_ptr
->ex_addr
,
1099 ntohs(ex_ptr
->ex_fport
));
1103 qemu_put_byte(f
, 42);
1104 slirp_socket_save(f
, so
);
1106 qemu_put_byte(f
, 0);
1108 qemu_put_be16(f
, slirp
->ip_id
);
1110 slirp_bootp_save(f
, slirp
);
1113 static void slirp_tcp_load(QEMUFile
*f
, struct tcpcb
*tp
)
1117 tp
->t_state
= qemu_get_sbe16(f
);
1118 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
1119 tp
->t_timer
[i
] = qemu_get_sbe16(f
);
1120 tp
->t_rxtshift
= qemu_get_sbe16(f
);
1121 tp
->t_rxtcur
= qemu_get_sbe16(f
);
1122 tp
->t_dupacks
= qemu_get_sbe16(f
);
1123 tp
->t_maxseg
= qemu_get_be16(f
);
1124 tp
->t_force
= qemu_get_sbyte(f
);
1125 tp
->t_flags
= qemu_get_be16(f
);
1126 tp
->snd_una
= qemu_get_be32(f
);
1127 tp
->snd_nxt
= qemu_get_be32(f
);
1128 tp
->snd_up
= qemu_get_be32(f
);
1129 tp
->snd_wl1
= qemu_get_be32(f
);
1130 tp
->snd_wl2
= qemu_get_be32(f
);
1131 tp
->iss
= qemu_get_be32(f
);
1132 tp
->snd_wnd
= qemu_get_be32(f
);
1133 tp
->rcv_wnd
= qemu_get_be32(f
);
1134 tp
->rcv_nxt
= qemu_get_be32(f
);
1135 tp
->rcv_up
= qemu_get_be32(f
);
1136 tp
->irs
= qemu_get_be32(f
);
1137 tp
->rcv_adv
= qemu_get_be32(f
);
1138 tp
->snd_max
= qemu_get_be32(f
);
1139 tp
->snd_cwnd
= qemu_get_be32(f
);
1140 tp
->snd_ssthresh
= qemu_get_be32(f
);
1141 tp
->t_idle
= qemu_get_sbe16(f
);
1142 tp
->t_rtt
= qemu_get_sbe16(f
);
1143 tp
->t_rtseq
= qemu_get_be32(f
);
1144 tp
->t_srtt
= qemu_get_sbe16(f
);
1145 tp
->t_rttvar
= qemu_get_sbe16(f
);
1146 tp
->t_rttmin
= qemu_get_be16(f
);
1147 tp
->max_sndwnd
= qemu_get_be32(f
);
1148 tp
->t_oobflags
= qemu_get_byte(f
);
1149 tp
->t_iobc
= qemu_get_byte(f
);
1150 tp
->t_softerror
= qemu_get_sbe16(f
);
1151 tp
->snd_scale
= qemu_get_byte(f
);
1152 tp
->rcv_scale
= qemu_get_byte(f
);
1153 tp
->request_r_scale
= qemu_get_byte(f
);
1154 tp
->requested_s_scale
= qemu_get_byte(f
);
1155 tp
->ts_recent
= qemu_get_be32(f
);
1156 tp
->ts_recent_age
= qemu_get_be32(f
);
1157 tp
->last_ack_sent
= qemu_get_be32(f
);
1161 static int slirp_sbuf_load(QEMUFile
*f
, struct sbuf
*sbuf
)
1163 uint32_t off
, sb_cc
, sb_datalen
;
1165 sb_cc
= qemu_get_be32(f
);
1166 sb_datalen
= qemu_get_be32(f
);
1168 sbreserve(sbuf
, sb_datalen
);
1170 if (sbuf
->sb_datalen
!= sb_datalen
)
1173 sbuf
->sb_cc
= sb_cc
;
1175 off
= qemu_get_sbe32(f
);
1176 sbuf
->sb_wptr
= sbuf
->sb_data
+ off
;
1177 off
= qemu_get_sbe32(f
);
1178 sbuf
->sb_rptr
= sbuf
->sb_data
+ off
;
1179 qemu_get_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
1184 static int slirp_socket_load(QEMUFile
*f
, struct socket
*so
)
1186 if (tcp_attach(so
) < 0)
1189 so
->so_urgc
= qemu_get_be32(f
);
1190 so
->so_ffamily
= qemu_get_be16(f
);
1191 switch (so
->so_ffamily
) {
1193 so
->so_faddr
.s_addr
= qemu_get_be32(f
);
1194 so
->so_fport
= qemu_get_be16(f
);
1198 "so_ffamily unknown, unable to restore so_faddr and so_lport\n");
1200 so
->so_lfamily
= qemu_get_be16(f
);
1201 switch (so
->so_lfamily
) {
1203 so
->so_laddr
.s_addr
= qemu_get_be32(f
);
1204 so
->so_lport
= qemu_get_be16(f
);
1208 "so_ffamily unknown, unable to restore so_laddr and so_lport\n");
1210 so
->so_iptos
= qemu_get_byte(f
);
1211 so
->so_emu
= qemu_get_byte(f
);
1212 so
->so_type
= qemu_get_byte(f
);
1213 so
->so_state
= qemu_get_be32(f
);
1214 if (slirp_sbuf_load(f
, &so
->so_rcv
) < 0)
1216 if (slirp_sbuf_load(f
, &so
->so_snd
) < 0)
1218 slirp_tcp_load(f
, so
->so_tcpcb
);
1223 static void slirp_bootp_load(QEMUFile
*f
, Slirp
*slirp
)
1227 for (i
= 0; i
< NB_BOOTP_CLIENTS
; i
++) {
1228 slirp
->bootp_clients
[i
].allocated
= qemu_get_be16(f
);
1229 qemu_get_buffer(f
, slirp
->bootp_clients
[i
].macaddr
, 6);
1233 static int slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
)
1235 Slirp
*slirp
= opaque
;
1236 struct ex_list
*ex_ptr
;
1238 while (qemu_get_byte(f
)) {
1240 struct socket
*so
= socreate(slirp
);
1245 ret
= slirp_socket_load(f
, so
);
1250 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) !=
1251 slirp
->vnetwork_addr
.s_addr
) {
1254 for (ex_ptr
= slirp
->exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
1255 if (ex_ptr
->ex_pty
== 3 &&
1256 so
->so_faddr
.s_addr
== ex_ptr
->ex_addr
.s_addr
&&
1257 so
->so_fport
== ex_ptr
->ex_fport
) {
1264 so
->extra
= (void *)ex_ptr
->ex_exec
;
1267 if (version_id
>= 2) {
1268 slirp
->ip_id
= qemu_get_be16(f
);
1271 if (version_id
>= 3) {
1272 slirp_bootp_load(f
, slirp
);