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-common.h"
25 #include "qemu/timer.h"
26 #include "qemu/error-report.h"
27 #include "sysemu/char.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
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 */
56 int get_dns_addr(struct in_addr
*pdns_addr
)
58 FIXED_INFO
*FixedInfo
=NULL
;
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
;
69 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
70 BufLen
= sizeof(FIXED_INFO
);
72 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
74 GlobalFree(FixedInfo
);
77 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
80 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
81 printf("GetNetworkParams failed. ret = %08x\n", (u_int
)ret
);
83 GlobalFree(FixedInfo
);
89 pIPAddr
= &(FixedInfo
->DnsServerList
);
90 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
91 *pdns_addr
= tmp_addr
;
93 dns_addr_time
= curtime
;
95 GlobalFree(FixedInfo
);
101 static void winsock_cleanup(void)
108 static struct stat dns_addr_stat
;
110 int get_dns_addr(struct in_addr
*pdns_addr
)
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
;
124 old_stat
= dns_addr_stat
;
125 if (stat("/etc/resolv.conf", &dns_addr_stat
) != 0)
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
;
136 f
= fopen("/etc/resolv.conf", "r");
141 fprintf(stderr
, "IP address of your DNS(s): ");
143 while (fgets(buff
, 512, f
) != NULL
) {
144 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
145 if (!inet_aton(buff2
, &tmp_addr
))
147 /* If it's the first one, set it to dns_addr */
149 *pdns_addr
= tmp_addr
;
151 dns_addr_time
= curtime
;
155 fprintf(stderr
, ", ");
159 fprintf(stderr
, "(more)");
165 fprintf(stderr
, "%s", inet_ntoa(tmp_addr
));
177 static void slirp_init_once(void)
179 static int initialized
;
190 WSAStartup(MAKEWORD(2,0), &Data
);
191 atexit(winsock_cleanup
);
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
,
208 Slirp
*slirp
= g_malloc0(sizeof(Slirp
));
212 slirp
->restricted
= restricted
;
217 /* Initialise mbufs *after* setting the MTU */
220 slirp
->vnetwork_addr
= vnetwork
;
221 slirp
->vnetwork_mask
= vnetmask
;
222 slirp
->vhost_addr
= vhost
;
224 pstrcpy(slirp
->client_hostname
, sizeof(slirp
->client_hostname
),
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
;
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
);
246 void slirp_cleanup(Slirp
*slirp
)
248 QTAILQ_REMOVE(&slirp_instances
, slirp
, entry
);
250 unregister_savevm(NULL
, "slirp", slirp
);
255 g_free(slirp
->vdnssearch
);
256 g_free(slirp
->tftp_prefix
);
257 g_free(slirp
->bootp_filename
);
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
)
269 if (*timeout
<= TIMEOUT_FAST
) {
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
;
283 if (slirp
->do_slowtimo
) {
284 t
= MIN(TIMEOUT_SLOW
, t
);
290 void slirp_pollfds_fill(GArray
*pollfds
, uint32_t *timeout
)
293 struct socket
*so
, *so_next
;
295 if (QTAILQ_EMPTY(&slirp_instances
)) {
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
;
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) {
336 * Set for reading sockets which are accepting
338 if (so
->so_state
& SS_FACCEPTCONN
) {
341 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
343 so
->pollfds_idx
= pollfds
->len
;
344 g_array_append_val(pollfds
, pfd
);
349 * Set for writing sockets which are connecting
351 if (so
->so_state
& SS_ISFCONNECTING
) {
354 .events
= G_IO_OUT
| G_IO_ERR
,
356 so
->pollfds_idx
= pollfds
->len
;
357 g_array_append_val(pollfds
, pfd
);
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
;
383 so
->pollfds_idx
= pollfds
->len
;
384 g_array_append_val(pollfds
, pfd
);
391 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
393 so_next
= so
->so_next
;
395 so
->pollfds_idx
= -1;
398 * See if it's timed out
401 if (so
->so_expire
<= curtime
) {
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
419 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
422 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
424 so
->pollfds_idx
= pollfds
->len
;
425 g_array_append_val(pollfds
, pfd
);
432 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
434 so_next
= so
->so_next
;
436 so
->pollfds_idx
= -1;
439 * See if it's timed out
442 if (so
->so_expire
<= curtime
) {
446 slirp
->do_slowtimo
= true; /* Let socket expire */
450 if (so
->so_state
& SS_ISFCONNECTED
) {
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
)
466 struct socket
*so
, *so_next
;
469 if (QTAILQ_EMPTY(&slirp_instances
)) {
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
)) {
482 slirp
->time_fasttimo
= 0;
484 if (slirp
->do_slowtimo
&&
485 ((curtime
- slirp
->last_slowtimo
) >= TIMEOUT_SLOW
)) {
488 slirp
->last_slowtimo
= curtime
;
498 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
502 so_next
= so
->so_next
;
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) {
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
) {
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
) {
535 /* Output it if we read something */
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
) {
551 so
->so_state
&= ~SS_ISFCONNECTING
;
553 ret
= send(so
->s
, (const void *) &ret
, 0, 0);
555 /* XXXXX Must fix, zero bytes is a NOP */
556 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
557 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
562 so
->so_state
&= SS_PERSISTENT_MASK
;
563 so
->so_state
|= SS_NOFDREF
;
565 /* else so->so_state &= ~SS_ISFCONNECTING; */
570 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), 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
588 if (so
->so_state
& SS_ISFCONNECTING
) {
589 ret
= qemu_recv(so
->s
, &ret
, 0, 0);
593 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
594 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
595 continue; /* Still connecting, continue */
599 so
->so_state
&= SS_PERSISTENT_MASK
;
600 so
->so_state
|= SS_NOFDREF
;
602 /* tcp_input will take care of it */
604 ret
= send(so
->s
, &ret
, 0, 0);
607 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
608 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
612 so
->so_state
&= SS_PERSISTENT_MASK
;
613 so
->so_state
|= SS_NOFDREF
;
615 so
->so_state
&= ~SS_ISFCONNECTING
;
619 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
620 } /* SS_ISFCONNECTING */
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
;
633 so_next
= so
->so_next
;
636 if (so
->pollfds_idx
!= -1) {
637 revents
= g_array_index(pollfds
, GPollFD
,
638 so
->pollfds_idx
).revents
;
642 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
648 * Check incoming ICMP relies.
650 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
654 so_next
= so
->so_next
;
657 if (so
->pollfds_idx
!= -1) {
658 revents
= g_array_index(pollfds
, GPollFD
,
659 so
->pollfds_idx
).revents
;
663 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
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
);
680 struct ex_list
*ex_ptr
;
682 ar_op
= ntohs(ah
->ar_op
);
685 if (ah
->ar_tip
== ah
->ar_sip
) {
687 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
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
)
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
)
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
;
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
));
725 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
732 void slirp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
737 if (pkt_len
< ETH_HLEN
)
740 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
743 arp_input(slirp
, pkt
, pkt_len
);
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
;
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
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 */
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
;
795 rah
->ar_op
= htons(ARPOP_REQUEST
);
798 memcpy(rah
->ar_sha
, special_ethaddr
, ETH_ALEN
- 4);
799 memcpy(&rah
->ar_sha
[2], &slirp
->vhost_addr
, 4);
802 rah
->ar_sip
= slirp
->vhost_addr
.s_addr
;
804 /* target hw addr (none) */
805 memset(rah
->ar_tha
, 0, ETH_ALEN
);
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;
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
);
828 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
831 int if_encap(Slirp
*slirp
, struct mbuf
*ifm
)
834 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
835 uint8_t ethaddr
[ETH_ALEN
];
836 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
839 if (ifm
->m_len
+ ETH_HLEN
> sizeof(buf
)) {
845 ret
= if_encap4(slirp
, ifm
, eh
, ethaddr
);
852 /* Do not assert while we don't manage IP6VERSION */
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
);
869 /* Drop host forwarding rule, return 0 if found. */
870 int slirp_remove_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
874 struct socket
*head
= (is_udp
? &slirp
->udb
: &slirp
->tcb
);
875 struct sockaddr_in addr
;
876 int port
= htons(host_port
);
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
) {
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
;
901 if (!udp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
902 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
905 if (!tcp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
906 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
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
) {
925 return add_exec(&slirp
->exec_list
, do_pty
, (char *)args
, *guest_addr
,
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
);
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
)
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
) {
953 size_t slirp_socket_can_recv(Slirp
*slirp
, struct in_addr guest_addr
,
959 so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
961 if (!so
|| so
->so_state
& SS_NOFDREF
) {
965 if (!CONN_CANFRCV(so
) || so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_datalen
/2)) {
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
)
976 struct socket
*so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
981 ret
= soreadbuf(so
, (const char *)buf
, size
);
984 tcp_output(sototcpcb(so
));
987 static void slirp_tcp_save(QEMUFile
*f
, struct tcpcb
*tp
)
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
)
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
) {
1053 qemu_put_be32(f
, so
->so_faddr
.s_addr
);
1054 qemu_put_be16(f
, so
->so_fport
);
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
) {
1063 qemu_put_be32(f
, so
->so_laddr
.s_addr
);
1064 qemu_put_be16(f
, so
->so_lport
);
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
)
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) {
1097 so
= slirp_find_ctl_socket(slirp
, ex_ptr
->ex_addr
,
1098 ntohs(ex_ptr
->ex_fport
));
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
)
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
);
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
)
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
);
1183 static int slirp_socket_load(QEMUFile
*f
, struct socket
*so
)
1185 if (tcp_attach(so
) < 0)
1188 so
->so_urgc
= qemu_get_be32(f
);
1189 so
->so_ffamily
= qemu_get_be16(f
);
1190 switch (so
->so_ffamily
) {
1192 so
->so_faddr
.s_addr
= qemu_get_be32(f
);
1193 so
->so_fport
= qemu_get_be16(f
);
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
) {
1202 so
->so_laddr
.s_addr
= qemu_get_be32(f
);
1203 so
->so_lport
= qemu_get_be16(f
);
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)
1215 if (slirp_sbuf_load(f
, &so
->so_snd
) < 0)
1217 slirp_tcp_load(f
, so
->so_tcpcb
);
1222 static void slirp_bootp_load(QEMUFile
*f
, Slirp
*slirp
)
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
)) {
1239 struct socket
*so
= socreate(slirp
);
1244 ret
= slirp_socket_load(f
, so
);
1249 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) !=
1250 slirp
->vnetwork_addr
.s_addr
) {
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
) {
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
);