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
36 /* Define to 1 if you want KEEPALIVE timers */
37 bool slirp_do_keepalive
;
39 /* host loopback address */
40 struct in_addr loopback_addr
;
41 /* host loopback network mask */
42 unsigned long loopback_mask
;
44 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
45 static const uint8_t special_ethaddr
[ETH_ALEN
] = {
46 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
51 static QTAILQ_HEAD(, Slirp
) slirp_instances
=
52 QTAILQ_HEAD_INITIALIZER(slirp_instances
);
54 static struct in_addr dns_addr
;
56 static struct in6_addr dns6_addr
;
58 static unsigned dns_addr_time
;
60 static unsigned dns6_addr_time
;
63 #define TIMEOUT_FAST 2 /* milliseconds */
64 #define TIMEOUT_SLOW 499 /* milliseconds */
65 /* for the aging of certain requests like DNS */
66 #define TIMEOUT_DEFAULT 1000 /* milliseconds */
70 int get_dns_addr(struct in_addr
*pdns_addr
)
72 FIXED_INFO
*FixedInfo
=NULL
;
75 IP_ADDR_STRING
*pIPAddr
;
76 struct in_addr tmp_addr
;
78 if (dns_addr
.s_addr
!= 0 && (curtime
- dns_addr_time
) < TIMEOUT_DEFAULT
) {
79 *pdns_addr
= dns_addr
;
83 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
84 BufLen
= sizeof(FIXED_INFO
);
86 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
88 GlobalFree(FixedInfo
);
91 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
94 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
95 printf("GetNetworkParams failed. ret = %08x\n", (unsigned)ret
);
97 GlobalFree(FixedInfo
);
103 pIPAddr
= &(FixedInfo
->DnsServerList
);
104 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
105 *pdns_addr
= tmp_addr
;
107 dns_addr_time
= curtime
;
109 GlobalFree(FixedInfo
);
115 int get_dns6_addr(struct in6_addr
*pdns6_addr
, uint32_t *scope_id
)
120 static void winsock_cleanup(void)
127 static int get_dns_addr_cached(void *pdns_addr
, void *cached_addr
,
129 struct stat
*cached_stat
, unsigned *cached_time
)
131 struct stat old_stat
;
132 if (curtime
- *cached_time
< TIMEOUT_DEFAULT
) {
133 memcpy(pdns_addr
, cached_addr
, addrlen
);
136 old_stat
= *cached_stat
;
137 if (stat("/etc/resolv.conf", cached_stat
) != 0) {
140 if (cached_stat
->st_dev
== old_stat
.st_dev
141 && cached_stat
->st_ino
== old_stat
.st_ino
142 && cached_stat
->st_size
== old_stat
.st_size
143 && cached_stat
->st_mtime
== old_stat
.st_mtime
) {
144 memcpy(pdns_addr
, cached_addr
, addrlen
);
150 static int get_dns_addr_resolv_conf(int af
, void *pdns_addr
, void *cached_addr
,
151 socklen_t addrlen
, uint32_t *scope_id
,
152 unsigned *cached_time
)
158 void *tmp_addr
= alloca(addrlen
);
161 f
= fopen("/etc/resolv.conf", "r");
165 DEBUG_MISC("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 DEBUG_MISC(" (more)");
192 } else if (slirp_debug
& DBG_MISC
) {
193 char s
[INET6_ADDRSTRLEN
];
194 const char *res
= inet_ntop(af
, tmp_addr
, s
, sizeof(s
));
196 res
= " (string conversion error)";
198 DEBUG_MISC(" %s", res
);
208 int get_dns_addr(struct in_addr
*pdns_addr
)
210 static struct stat dns_addr_stat
;
212 if (dns_addr
.s_addr
!= 0) {
214 ret
= get_dns_addr_cached(pdns_addr
, &dns_addr
, sizeof(dns_addr
),
215 &dns_addr_stat
, &dns_addr_time
);
220 return get_dns_addr_resolv_conf(AF_INET
, pdns_addr
, &dns_addr
,
221 sizeof(dns_addr
), NULL
, &dns_addr_time
);
224 int get_dns6_addr(struct in6_addr
*pdns6_addr
, uint32_t *scope_id
)
226 static struct stat dns6_addr_stat
;
228 if (!in6_zero(&dns6_addr
)) {
230 ret
= get_dns_addr_cached(pdns6_addr
, &dns6_addr
, sizeof(dns6_addr
),
231 &dns6_addr_stat
, &dns6_addr_time
);
236 return get_dns_addr_resolv_conf(AF_INET6
, pdns6_addr
, &dns6_addr
,
238 scope_id
, &dns6_addr_time
);
243 static void slirp_init_once(void)
245 static int initialized
;
257 WSAStartup(MAKEWORD(2,0), &Data
);
258 atexit(winsock_cleanup
);
261 loopback_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
262 loopback_mask
= htonl(IN_CLASSA_NET
);
264 debug
= g_getenv("SLIRP_DEBUG");
266 const GDebugKey keys
[] = {
267 { "call", DBG_CALL
},
268 { "misc", DBG_MISC
},
269 { "error", DBG_ERROR
},
270 { "tftp", DBG_TFTP
},
272 slirp_debug
= g_parse_debug_string(debug
, keys
, G_N_ELEMENTS(keys
));
278 Slirp
*slirp_init(int restricted
, bool in_enabled
, struct in_addr vnetwork
,
279 struct in_addr vnetmask
, struct in_addr vhost
,
281 struct in6_addr vprefix_addr6
, uint8_t vprefix_len
,
282 struct in6_addr vhost6
, const char *vhostname
,
283 const char *tftp_server_name
,
284 const char *tftp_path
, const char *bootfile
,
285 struct in_addr vdhcp_start
, struct in_addr vnameserver
,
286 struct in6_addr vnameserver6
, const char **vdnssearch
,
287 const char *vdomainname
,
288 const SlirpCb
*callbacks
,
291 Slirp
*slirp
= g_malloc0(sizeof(Slirp
));
295 slirp
->cb
= callbacks
;
296 slirp
->grand
= g_rand_new();
297 slirp
->restricted
= restricted
;
299 slirp
->in_enabled
= in_enabled
;
300 slirp
->in6_enabled
= in6_enabled
;
306 /* Initialise mbufs *after* setting the MTU */
309 slirp
->vnetwork_addr
= vnetwork
;
310 slirp
->vnetwork_mask
= vnetmask
;
311 slirp
->vhost_addr
= vhost
;
312 slirp
->vprefix_addr6
= vprefix_addr6
;
313 slirp
->vprefix_len
= vprefix_len
;
314 slirp
->vhost_addr6
= vhost6
;
316 slirp_pstrcpy(slirp
->client_hostname
, sizeof(slirp
->client_hostname
),
319 slirp
->tftp_prefix
= g_strdup(tftp_path
);
320 slirp
->bootp_filename
= g_strdup(bootfile
);
321 slirp
->vdomainname
= g_strdup(vdomainname
);
322 slirp
->vdhcp_startaddr
= vdhcp_start
;
323 slirp
->vnameserver_addr
= vnameserver
;
324 slirp
->vnameserver_addr6
= vnameserver6
;
325 slirp
->tftp_server_name
= g_strdup(tftp_server_name
);
328 translate_dnssearch(slirp
, vdnssearch
);
331 slirp
->opaque
= opaque
;
334 slirp_state_register(slirp
);
336 QTAILQ_INSERT_TAIL(&slirp_instances
, slirp
, entry
);
341 void slirp_cleanup(Slirp
*slirp
)
343 struct gfwd_list
*e
, *next
;
345 for (e
= slirp
->guestfwd_list
; e
; e
= next
) {
351 QTAILQ_REMOVE(&slirp_instances
, slirp
, entry
);
353 slirp_state_unregister(slirp
);
359 g_rand_free(slirp
->grand
);
361 g_free(slirp
->vdnssearch
);
362 g_free(slirp
->tftp_prefix
);
363 g_free(slirp
->bootp_filename
);
364 g_free(slirp
->vdomainname
);
368 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
369 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
371 static void slirp_update_timeout(uint32_t *timeout
)
376 if (*timeout
<= TIMEOUT_FAST
) {
380 t
= MIN(1000, *timeout
);
382 /* If we have tcp timeout with slirp, then we will fill @timeout with
383 * more precise value.
385 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
386 if (slirp
->time_fasttimo
) {
387 *timeout
= TIMEOUT_FAST
;
390 if (slirp
->do_slowtimo
) {
391 t
= MIN(TIMEOUT_SLOW
, t
);
397 void slirp_pollfds_fill(GArray
*pollfds
, uint32_t *timeout
)
400 struct socket
*so
, *so_next
;
402 if (QTAILQ_EMPTY(&slirp_instances
)) {
410 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
412 * *_slowtimo needs calling if there are IP fragments
413 * in the fragment queue, or there are TCP connections active
415 slirp
->do_slowtimo
= ((slirp
->tcb
.so_next
!= &slirp
->tcb
) ||
416 (&slirp
->ipq
.ip_link
!= slirp
->ipq
.ip_link
.next
));
418 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
422 so_next
= so
->so_next
;
424 so
->pollfds_idx
= -1;
427 * See if we need a tcp_fasttimo
429 if (slirp
->time_fasttimo
== 0 &&
430 so
->so_tcpcb
->t_flags
& TF_DELACK
) {
431 slirp
->time_fasttimo
= curtime
; /* Flag when want a fasttimo */
435 * NOFDREF can include still connecting to local-host,
436 * newly socreated() sockets etc. Don't want to select these.
438 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
443 * Set for reading sockets which are accepting
445 if (so
->so_state
& SS_FACCEPTCONN
) {
448 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
450 so
->pollfds_idx
= pollfds
->len
;
451 g_array_append_val(pollfds
, pfd
);
456 * Set for writing sockets which are connecting
458 if (so
->so_state
& SS_ISFCONNECTING
) {
461 .events
= G_IO_OUT
| G_IO_ERR
,
463 so
->pollfds_idx
= pollfds
->len
;
464 g_array_append_val(pollfds
, pfd
);
469 * Set for writing if we are connected, can send more, and
470 * we have something to send
472 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
473 events
|= G_IO_OUT
| G_IO_ERR
;
477 * Set for reading (and urgent data) if we are connected, can
478 * receive more, and we have room for it XXX /2 ?
480 if (CONN_CANFRCV(so
) &&
481 (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
482 events
|= G_IO_IN
| G_IO_HUP
| G_IO_ERR
| G_IO_PRI
;
490 so
->pollfds_idx
= pollfds
->len
;
491 g_array_append_val(pollfds
, pfd
);
498 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
500 so_next
= so
->so_next
;
502 so
->pollfds_idx
= -1;
505 * See if it's timed out
508 if (so
->so_expire
<= curtime
) {
512 slirp
->do_slowtimo
= true; /* Let socket expire */
517 * When UDP packets are received from over the
518 * link, they're sendto()'d straight away, so
519 * no need for setting for writing
520 * Limit the number of packets queued by this session
521 * to 4. Note that even though we try and limit this
522 * to 4 packets, the session could have more queued
523 * if the packets needed to be fragmented
526 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
529 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
531 so
->pollfds_idx
= pollfds
->len
;
532 g_array_append_val(pollfds
, pfd
);
539 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
541 so_next
= so
->so_next
;
543 so
->pollfds_idx
= -1;
546 * See if it's timed out
549 if (so
->so_expire
<= curtime
) {
553 slirp
->do_slowtimo
= true; /* Let socket expire */
557 if (so
->so_state
& SS_ISFCONNECTED
) {
560 .events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
562 so
->pollfds_idx
= pollfds
->len
;
563 g_array_append_val(pollfds
, pfd
);
567 slirp_update_timeout(timeout
);
570 void slirp_pollfds_poll(GArray
*pollfds
, int select_error
)
572 Slirp
*slirp
= QTAILQ_FIRST(&slirp_instances
);
573 struct socket
*so
, *so_next
;
580 curtime
= slirp
->cb
->clock_get_ns() / SCALE_MS
;
582 QTAILQ_FOREACH(slirp
, &slirp_instances
, entry
) {
584 * See if anything has timed out
586 if (slirp
->time_fasttimo
&&
587 ((curtime
- slirp
->time_fasttimo
) >= TIMEOUT_FAST
)) {
589 slirp
->time_fasttimo
= 0;
591 if (slirp
->do_slowtimo
&&
592 ((curtime
- slirp
->last_slowtimo
) >= TIMEOUT_SLOW
)) {
595 slirp
->last_slowtimo
= curtime
;
605 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
609 so_next
= so
->so_next
;
612 if (so
->pollfds_idx
!= -1) {
613 revents
= g_array_index(pollfds
, GPollFD
,
614 so
->pollfds_idx
).revents
;
617 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
623 * This will soread as well, so no need to
624 * test for G_IO_IN below if this succeeds
626 if (revents
& G_IO_PRI
) {
629 /* Socket error might have resulted in the socket being
630 * removed, do not try to do anything more with it. */
635 * Check sockets for reading
637 else if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
)) {
639 * Check for incoming connections
641 if (so
->so_state
& SS_FACCEPTCONN
) {
647 /* Output it if we read something */
649 tcp_output(sototcpcb(so
));
652 /* Socket error might have resulted in the socket being
653 * removed, do not try to do anything more with it. */
659 * Check sockets for writing
661 if (!(so
->so_state
& SS_NOFDREF
) &&
662 (revents
& (G_IO_OUT
| G_IO_ERR
))) {
664 * Check for non-blocking, still-connecting sockets
666 if (so
->so_state
& SS_ISFCONNECTING
) {
668 so
->so_state
&= ~SS_ISFCONNECTING
;
670 ret
= send(so
->s
, (const void *) &ret
, 0, 0);
672 /* XXXXX Must fix, zero bytes is a NOP */
673 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
674 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
679 so
->so_state
&= SS_PERSISTENT_MASK
;
680 so
->so_state
|= SS_NOFDREF
;
682 /* else so->so_state &= ~SS_ISFCONNECTING; */
687 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
,
693 /* Call tcp_output in case we need to send a window
694 * update to the guest, otherwise it will be stuck
695 * until it sends a window probe. */
696 tcp_output(sototcpcb(so
));
704 * Incoming packets are sent straight away, they're not buffered.
705 * Incoming UDP data isn't buffered either.
707 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
711 so_next
= so
->so_next
;
714 if (so
->pollfds_idx
!= -1) {
715 revents
= g_array_index(pollfds
, GPollFD
,
716 so
->pollfds_idx
).revents
;
720 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
726 * Check incoming ICMP relies.
728 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
732 so_next
= so
->so_next
;
735 if (so
->pollfds_idx
!= -1) {
736 revents
= g_array_index(pollfds
, GPollFD
,
737 so
->pollfds_idx
).revents
;
741 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
))) {
751 static void arp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
753 struct slirp_arphdr
*ah
= (struct slirp_arphdr
*)(pkt
+ ETH_HLEN
);
754 uint8_t arp_reply
[MAX(ETH_HLEN
+ sizeof(struct slirp_arphdr
), 64)];
755 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
756 struct slirp_arphdr
*rah
= (struct slirp_arphdr
*)(arp_reply
+ ETH_HLEN
);
758 struct gfwd_list
*ex_ptr
;
760 if (!slirp
->in_enabled
) {
764 ar_op
= ntohs(ah
->ar_op
);
767 if (ah
->ar_tip
== ah
->ar_sip
) {
769 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
773 if ((ah
->ar_tip
& slirp
->vnetwork_mask
.s_addr
) ==
774 slirp
->vnetwork_addr
.s_addr
) {
775 if (ah
->ar_tip
== slirp
->vnameserver_addr
.s_addr
||
776 ah
->ar_tip
== slirp
->vhost_addr
.s_addr
)
778 for (ex_ptr
= slirp
->guestfwd_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
779 if (ex_ptr
->ex_addr
.s_addr
== ah
->ar_tip
)
784 memset(arp_reply
, 0, sizeof(arp_reply
));
786 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
788 /* ARP request for alias/dns mac address */
789 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
790 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
791 memcpy(&reh
->h_source
[2], &ah
->ar_tip
, 4);
792 reh
->h_proto
= htons(ETH_P_ARP
);
794 rah
->ar_hrd
= htons(1);
795 rah
->ar_pro
= htons(ETH_P_IP
);
796 rah
->ar_hln
= ETH_ALEN
;
798 rah
->ar_op
= htons(ARPOP_REPLY
);
799 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
800 rah
->ar_sip
= ah
->ar_tip
;
801 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
802 rah
->ar_tip
= ah
->ar_sip
;
803 slirp_send_packet_all(slirp
, arp_reply
, sizeof(arp_reply
));
807 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
814 void slirp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
819 if (pkt_len
< ETH_HLEN
)
822 proto
= (((uint16_t) pkt
[12]) << 8) + pkt
[13];
825 arp_input(slirp
, pkt
, pkt_len
);
832 /* Note: we add 2 to align the IP header on 4 bytes,
833 * and add the margin for the tcpiphdr overhead */
834 if (M_FREEROOM(m
) < pkt_len
+ TCPIPHDR_DELTA
+ 2) {
835 m_inc(m
, pkt_len
+ TCPIPHDR_DELTA
+ 2);
837 m
->m_len
= pkt_len
+ TCPIPHDR_DELTA
+ 2;
838 memcpy(m
->m_data
+ TCPIPHDR_DELTA
+ 2, pkt
, pkt_len
);
840 m
->m_data
+= TCPIPHDR_DELTA
+ 2 + ETH_HLEN
;
841 m
->m_len
-= TCPIPHDR_DELTA
+ 2 + ETH_HLEN
;
843 if (proto
== ETH_P_IP
) {
845 } else if (proto
== ETH_P_IPV6
) {
851 ncsi_input(slirp
, pkt
, pkt_len
);
859 /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no
860 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
863 static int if_encap4(Slirp
*slirp
, struct mbuf
*ifm
, struct ethhdr
*eh
,
864 uint8_t ethaddr
[ETH_ALEN
])
866 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
868 if (iph
->ip_dst
.s_addr
== 0) {
869 /* 0.0.0.0 can not be a destination address, something went wrong,
870 * avoid making it worse */
873 if (!arp_table_search(slirp
, iph
->ip_dst
.s_addr
, ethaddr
)) {
874 uint8_t arp_req
[ETH_HLEN
+ sizeof(struct slirp_arphdr
)];
875 struct ethhdr
*reh
= (struct ethhdr
*)arp_req
;
876 struct slirp_arphdr
*rah
= (struct slirp_arphdr
*)(arp_req
+ ETH_HLEN
);
878 if (!ifm
->resolution_requested
) {
879 /* If the client addr is not known, send an ARP request */
880 memset(reh
->h_dest
, 0xff, ETH_ALEN
);
881 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
882 memcpy(&reh
->h_source
[2], &slirp
->vhost_addr
, 4);
883 reh
->h_proto
= htons(ETH_P_ARP
);
884 rah
->ar_hrd
= htons(1);
885 rah
->ar_pro
= htons(ETH_P_IP
);
886 rah
->ar_hln
= ETH_ALEN
;
888 rah
->ar_op
= htons(ARPOP_REQUEST
);
891 memcpy(rah
->ar_sha
, special_ethaddr
, ETH_ALEN
- 4);
892 memcpy(&rah
->ar_sha
[2], &slirp
->vhost_addr
, 4);
895 rah
->ar_sip
= slirp
->vhost_addr
.s_addr
;
897 /* target hw addr (none) */
898 memset(rah
->ar_tha
, 0, ETH_ALEN
);
901 rah
->ar_tip
= iph
->ip_dst
.s_addr
;
902 slirp
->client_ipaddr
= iph
->ip_dst
;
903 slirp_send_packet_all(slirp
, arp_req
, sizeof(arp_req
));
904 ifm
->resolution_requested
= true;
906 /* Expire request and drop outgoing packet after 1 second */
907 ifm
->expiration_date
= slirp
->cb
->clock_get_ns() + 1000000000ULL;
911 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
912 /* XXX: not correct */
913 memcpy(&eh
->h_source
[2], &slirp
->vhost_addr
, 4);
914 eh
->h_proto
= htons(ETH_P_IP
);
921 /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no
922 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
925 static int if_encap6(Slirp
*slirp
, struct mbuf
*ifm
, struct ethhdr
*eh
,
926 uint8_t ethaddr
[ETH_ALEN
])
928 const struct ip6
*ip6h
= mtod(ifm
, const struct ip6
*);
929 if (!ndp_table_search(slirp
, ip6h
->ip_dst
, ethaddr
)) {
930 if (!ifm
->resolution_requested
) {
931 ndp_send_ns(slirp
, ip6h
->ip_dst
);
932 ifm
->resolution_requested
= true;
933 ifm
->expiration_date
= slirp
->cb
->clock_get_ns() + 1000000000ULL;
937 eh
->h_proto
= htons(ETH_P_IPV6
);
938 in6_compute_ethaddr(ip6h
->ip_src
, eh
->h_source
);
945 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
948 int if_encap(Slirp
*slirp
, struct mbuf
*ifm
)
951 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
952 uint8_t ethaddr
[ETH_ALEN
];
953 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
956 if (ifm
->m_len
+ ETH_HLEN
> sizeof(buf
)) {
962 ret
= if_encap4(slirp
, ifm
, eh
, ethaddr
);
969 ret
= if_encap6(slirp
, ifm
, eh
, ethaddr
);
976 g_assert_not_reached();
980 memcpy(eh
->h_dest
, ethaddr
, ETH_ALEN
);
981 DEBUG_ARG("src = %02x:%02x:%02x:%02x:%02x:%02x",
982 eh
->h_source
[0], eh
->h_source
[1], eh
->h_source
[2],
983 eh
->h_source
[3], eh
->h_source
[4], eh
->h_source
[5]);
984 DEBUG_ARG("dst = %02x:%02x:%02x:%02x:%02x:%02x",
985 eh
->h_dest
[0], eh
->h_dest
[1], eh
->h_dest
[2],
986 eh
->h_dest
[3], eh
->h_dest
[4], eh
->h_dest
[5]);
987 memcpy(buf
+ sizeof(struct ethhdr
), ifm
->m_data
, ifm
->m_len
);
988 slirp_send_packet_all(slirp
, buf
, ifm
->m_len
+ ETH_HLEN
);
992 /* Drop host forwarding rule, return 0 if found. */
993 int slirp_remove_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
997 struct socket
*head
= (is_udp
? &slirp
->udb
: &slirp
->tcb
);
998 struct sockaddr_in addr
;
999 int port
= htons(host_port
);
1002 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
1003 addr_len
= sizeof(addr
);
1004 if ((so
->so_state
& SS_HOSTFWD
) &&
1005 getsockname(so
->s
, (struct sockaddr
*)&addr
, &addr_len
) == 0 &&
1006 addr
.sin_addr
.s_addr
== host_addr
.s_addr
&&
1007 addr
.sin_port
== port
) {
1008 so
->slirp
->cb
->unregister_poll_fd(so
->s
);
1009 slirp_closesocket(so
->s
);
1018 int slirp_add_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
1019 int host_port
, struct in_addr guest_addr
, int guest_port
)
1021 if (!guest_addr
.s_addr
) {
1022 guest_addr
= slirp
->vdhcp_startaddr
;
1025 if (!udp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
1026 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
1029 if (!tcp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
1030 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
1037 check_guestfwd(Slirp
*slirp
, struct in_addr
*guest_addr
, int guest_port
)
1039 struct gfwd_list
*tmp_ptr
;
1041 if (!guest_addr
->s_addr
) {
1042 guest_addr
->s_addr
= slirp
->vnetwork_addr
.s_addr
|
1043 (htonl(0x0204) & ~slirp
->vnetwork_mask
.s_addr
);
1045 if ((guest_addr
->s_addr
& slirp
->vnetwork_mask
.s_addr
) !=
1046 slirp
->vnetwork_addr
.s_addr
||
1047 guest_addr
->s_addr
== slirp
->vhost_addr
.s_addr
||
1048 guest_addr
->s_addr
== slirp
->vnameserver_addr
.s_addr
) {
1052 /* check if the port is "bound" */
1053 for (tmp_ptr
= slirp
->guestfwd_list
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
1054 if (guest_port
== tmp_ptr
->ex_fport
&&
1055 guest_addr
->s_addr
== tmp_ptr
->ex_addr
.s_addr
)
1062 int slirp_add_exec(Slirp
*slirp
, const char *cmdline
,
1063 struct in_addr
*guest_addr
, int guest_port
)
1065 if (!check_guestfwd(slirp
, guest_addr
, guest_port
)) {
1069 add_exec(&slirp
->guestfwd_list
, cmdline
, *guest_addr
, htons(guest_port
));
1073 int slirp_add_guestfwd(Slirp
*slirp
, SlirpWriteCb write_cb
, void *opaque
,
1074 struct in_addr
*guest_addr
, int guest_port
)
1076 if (!check_guestfwd(slirp
, guest_addr
, guest_port
)) {
1080 add_guestfwd(&slirp
->guestfwd_list
, write_cb
, opaque
,
1081 *guest_addr
, htons(guest_port
));
1085 ssize_t
slirp_send(struct socket
*so
, const void *buf
, size_t len
, int flags
)
1087 if (so
->s
== -1 && so
->guestfwd
) {
1088 /* XXX this blocks entire thread. Rewrite to use
1089 * qemu_chr_fe_write and background I/O callbacks */
1090 so
->guestfwd
->write_cb(buf
, len
, so
->guestfwd
->opaque
);
1096 * This should in theory not happen but it is hard to be
1097 * sure because some code paths will end up with so->s == -1
1098 * on a failure but don't dispose of the struct socket.
1099 * Check specifically, so we don't pass -1 to send().
1105 return send(so
->s
, buf
, len
, flags
);
1109 slirp_find_ctl_socket(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
)
1113 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
1114 if (so
->so_faddr
.s_addr
== guest_addr
.s_addr
&&
1115 htons(so
->so_fport
) == guest_port
) {
1122 size_t slirp_socket_can_recv(Slirp
*slirp
, struct in_addr guest_addr
,
1125 struct iovec iov
[2];
1128 so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
1130 if (!so
|| so
->so_state
& SS_NOFDREF
) {
1134 if (!CONN_CANFRCV(so
) || so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_datalen
/2)) {
1138 return sopreprbuf(so
, iov
, NULL
);
1141 void slirp_socket_recv(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
,
1142 const uint8_t *buf
, int size
)
1145 struct socket
*so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
1150 ret
= soreadbuf(so
, (const char *)buf
, size
);
1153 tcp_output(sototcpcb(so
));
1156 void slirp_send_packet_all(Slirp
*slirp
, const void *buf
, size_t len
)
1158 ssize_t ret
= slirp
->cb
->send_packet(buf
, len
, slirp
->opaque
);
1161 g_critical("Failed to send packet, ret: %ld", (long) ret
);
1162 } else if (ret
< len
) {
1163 DEBUG_ERROR("send_packet() didn't send all data: %ld < %lu",
1164 (long) ret
, (unsigned long) len
);