1 /* SPDX-License-Identifier: MIT */
5 * Copyright (c) 2004-2008 Fabrice Bellard
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 /* Define to 1 if you want KEEPALIVE timers */
35 bool slirp_do_keepalive
;
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 struct in_addr dns_addr
;
51 static struct in6_addr dns6_addr
;
53 static unsigned dns_addr_time
;
55 static unsigned dns6_addr_time
;
58 #define TIMEOUT_FAST 2 /* milliseconds */
59 #define TIMEOUT_SLOW 499 /* milliseconds */
60 /* for the aging of certain requests like DNS */
61 #define TIMEOUT_DEFAULT 1000 /* milliseconds */
65 int get_dns_addr(struct in_addr
*pdns_addr
)
67 FIXED_INFO
*FixedInfo
=NULL
;
70 IP_ADDR_STRING
*pIPAddr
;
71 struct in_addr tmp_addr
;
73 if (dns_addr
.s_addr
!= 0 && (curtime
- dns_addr_time
) < TIMEOUT_DEFAULT
) {
74 *pdns_addr
= dns_addr
;
78 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
79 BufLen
= sizeof(FIXED_INFO
);
81 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
83 GlobalFree(FixedInfo
);
86 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
89 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
90 printf("GetNetworkParams failed. ret = %08x\n", (unsigned)ret
);
92 GlobalFree(FixedInfo
);
98 pIPAddr
= &(FixedInfo
->DnsServerList
);
99 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
100 *pdns_addr
= tmp_addr
;
102 dns_addr_time
= curtime
;
104 GlobalFree(FixedInfo
);
110 int get_dns6_addr(struct in6_addr
*pdns6_addr
, uint32_t *scope_id
)
115 static void winsock_cleanup(void)
122 static int get_dns_addr_cached(void *pdns_addr
, void *cached_addr
,
124 struct stat
*cached_stat
, unsigned *cached_time
)
126 struct stat old_stat
;
127 if (curtime
- *cached_time
< TIMEOUT_DEFAULT
) {
128 memcpy(pdns_addr
, cached_addr
, addrlen
);
131 old_stat
= *cached_stat
;
132 if (stat("/etc/resolv.conf", cached_stat
) != 0) {
135 if (cached_stat
->st_dev
== old_stat
.st_dev
136 && cached_stat
->st_ino
== old_stat
.st_ino
137 && cached_stat
->st_size
== old_stat
.st_size
138 && cached_stat
->st_mtime
== old_stat
.st_mtime
) {
139 memcpy(pdns_addr
, cached_addr
, addrlen
);
145 static int get_dns_addr_resolv_conf(int af
, void *pdns_addr
, void *cached_addr
,
146 socklen_t addrlen
, uint32_t *scope_id
,
147 unsigned *cached_time
)
153 void *tmp_addr
= alloca(addrlen
);
156 f
= fopen("/etc/resolv.conf", "r");
160 DEBUG_MISC("IP address of your DNS(s):");
161 while (fgets(buff
, 512, f
) != NULL
) {
162 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
163 char *c
= strchr(buff2
, '%');
165 if_index
= if_nametoindex(c
+ 1);
171 if (!inet_pton(af
, buff2
, tmp_addr
)) {
174 /* If it's the first one, set it to dns_addr */
176 memcpy(pdns_addr
, tmp_addr
, addrlen
);
177 memcpy(cached_addr
, tmp_addr
, addrlen
);
179 *scope_id
= if_index
;
181 *cached_time
= curtime
;
185 DEBUG_MISC(" (more)");
187 } else if (slirp_debug
& DBG_MISC
) {
188 char s
[INET6_ADDRSTRLEN
];
189 const char *res
= inet_ntop(af
, tmp_addr
, s
, sizeof(s
));
191 res
= " (string conversion error)";
193 DEBUG_MISC(" %s", res
);
203 int get_dns_addr(struct in_addr
*pdns_addr
)
205 static struct stat dns_addr_stat
;
207 if (dns_addr
.s_addr
!= 0) {
209 ret
= get_dns_addr_cached(pdns_addr
, &dns_addr
, sizeof(dns_addr
),
210 &dns_addr_stat
, &dns_addr_time
);
215 return get_dns_addr_resolv_conf(AF_INET
, pdns_addr
, &dns_addr
,
216 sizeof(dns_addr
), NULL
, &dns_addr_time
);
219 int get_dns6_addr(struct in6_addr
*pdns6_addr
, uint32_t *scope_id
)
221 static struct stat dns6_addr_stat
;
223 if (!in6_zero(&dns6_addr
)) {
225 ret
= get_dns_addr_cached(pdns6_addr
, &dns6_addr
, sizeof(dns6_addr
),
226 &dns6_addr_stat
, &dns6_addr_time
);
231 return get_dns_addr_resolv_conf(AF_INET6
, pdns6_addr
, &dns6_addr
,
233 scope_id
, &dns6_addr_time
);
238 static void slirp_init_once(void)
240 static int initialized
;
252 WSAStartup(MAKEWORD(2,0), &Data
);
253 atexit(winsock_cleanup
);
256 loopback_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
257 loopback_mask
= htonl(IN_CLASSA_NET
);
259 debug
= g_getenv("SLIRP_DEBUG");
261 const GDebugKey keys
[] = {
262 { "call", DBG_CALL
},
263 { "misc", DBG_MISC
},
264 { "error", DBG_ERROR
},
265 { "tftp", DBG_TFTP
},
267 slirp_debug
= g_parse_debug_string(debug
, keys
, G_N_ELEMENTS(keys
));
273 Slirp
*slirp_init(int restricted
, bool in_enabled
, struct in_addr vnetwork
,
274 struct in_addr vnetmask
, struct in_addr vhost
,
276 struct in6_addr vprefix_addr6
, uint8_t vprefix_len
,
277 struct in6_addr vhost6
, const char *vhostname
,
278 const char *tftp_server_name
,
279 const char *tftp_path
, const char *bootfile
,
280 struct in_addr vdhcp_start
, struct in_addr vnameserver
,
281 struct in6_addr vnameserver6
, const char **vdnssearch
,
282 const char *vdomainname
,
283 const SlirpCb
*callbacks
,
286 Slirp
*slirp
= g_malloc0(sizeof(Slirp
));
290 slirp
->opaque
= opaque
;
291 slirp
->cb
= callbacks
;
292 slirp
->grand
= g_rand_new();
293 slirp
->restricted
= restricted
;
295 slirp
->in_enabled
= in_enabled
;
296 slirp
->in6_enabled
= in6_enabled
;
302 /* Initialise mbufs *after* setting the MTU */
305 slirp
->vnetwork_addr
= vnetwork
;
306 slirp
->vnetwork_mask
= vnetmask
;
307 slirp
->vhost_addr
= vhost
;
308 slirp
->vprefix_addr6
= vprefix_addr6
;
309 slirp
->vprefix_len
= vprefix_len
;
310 slirp
->vhost_addr6
= vhost6
;
312 slirp_pstrcpy(slirp
->client_hostname
, sizeof(slirp
->client_hostname
),
315 slirp
->tftp_prefix
= g_strdup(tftp_path
);
316 slirp
->bootp_filename
= g_strdup(bootfile
);
317 slirp
->vdomainname
= g_strdup(vdomainname
);
318 slirp
->vdhcp_startaddr
= vdhcp_start
;
319 slirp
->vnameserver_addr
= vnameserver
;
320 slirp
->vnameserver_addr6
= vnameserver6
;
321 slirp
->tftp_server_name
= g_strdup(tftp_server_name
);
324 translate_dnssearch(slirp
, vdnssearch
);
330 void slirp_cleanup(Slirp
*slirp
)
332 struct gfwd_list
*e
, *next
;
334 for (e
= slirp
->guestfwd_list
; e
; e
= next
) {
344 g_rand_free(slirp
->grand
);
346 g_free(slirp
->vdnssearch
);
347 g_free(slirp
->tftp_prefix
);
348 g_free(slirp
->bootp_filename
);
349 g_free(slirp
->vdomainname
);
353 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
354 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
356 static void slirp_update_timeout(Slirp
*slirp
, uint32_t *timeout
)
360 if (*timeout
<= TIMEOUT_FAST
) {
364 t
= MIN(1000, *timeout
);
366 /* If we have tcp timeout with slirp, then we will fill @timeout with
367 * more precise value.
369 if (slirp
->time_fasttimo
) {
370 *timeout
= TIMEOUT_FAST
;
373 if (slirp
->do_slowtimo
) {
374 t
= MIN(TIMEOUT_SLOW
, t
);
379 void slirp_pollfds_fill(Slirp
*slirp
, uint32_t *timeout
,
380 SlirpAddPollCb add_poll
, void *opaque
)
382 struct socket
*so
, *so_next
;
389 * *_slowtimo needs calling if there are IP fragments
390 * in the fragment queue, or there are TCP connections active
392 slirp
->do_slowtimo
= ((slirp
->tcb
.so_next
!= &slirp
->tcb
) ||
393 (&slirp
->ipq
.ip_link
!= slirp
->ipq
.ip_link
.next
));
395 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so_next
) {
398 so_next
= so
->so_next
;
400 so
->pollfds_idx
= -1;
403 * See if we need a tcp_fasttimo
405 if (slirp
->time_fasttimo
== 0 &&
406 so
->so_tcpcb
->t_flags
& TF_DELACK
) {
407 slirp
->time_fasttimo
= curtime
; /* Flag when want a fasttimo */
411 * NOFDREF can include still connecting to local-host,
412 * newly socreated() sockets etc. Don't want to select these.
414 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
419 * Set for reading sockets which are accepting
421 if (so
->so_state
& SS_FACCEPTCONN
) {
422 so
->pollfds_idx
= add_poll(so
->s
,
423 SLIRP_POLL_IN
| SLIRP_POLL_HUP
| SLIRP_POLL_ERR
, opaque
);
428 * Set for writing sockets which are connecting
430 if (so
->so_state
& SS_ISFCONNECTING
) {
431 so
->pollfds_idx
= add_poll(so
->s
,
432 SLIRP_POLL_OUT
| SLIRP_POLL_ERR
, opaque
);
437 * Set for writing if we are connected, can send more, and
438 * we have something to send
440 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
441 events
|= SLIRP_POLL_OUT
| SLIRP_POLL_ERR
;
445 * Set for reading (and urgent data) if we are connected, can
446 * receive more, and we have room for it XXX /2 ?
448 if (CONN_CANFRCV(so
) &&
449 (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
450 events
|= SLIRP_POLL_IN
| SLIRP_POLL_HUP
|
451 SLIRP_POLL_ERR
| SLIRP_POLL_PRI
;
455 so
->pollfds_idx
= add_poll(so
->s
, events
, opaque
);
462 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
; so
= so_next
) {
463 so_next
= so
->so_next
;
465 so
->pollfds_idx
= -1;
468 * See if it's timed out
471 if (so
->so_expire
<= curtime
) {
475 slirp
->do_slowtimo
= true; /* Let socket expire */
480 * When UDP packets are received from over the
481 * link, they're sendto()'d straight away, so
482 * no need for setting for writing
483 * Limit the number of packets queued by this session
484 * to 4. Note that even though we try and limit this
485 * to 4 packets, the session could have more queued
486 * if the packets needed to be fragmented
489 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
490 so
->pollfds_idx
= add_poll(so
->s
,
491 SLIRP_POLL_IN
| SLIRP_POLL_HUP
| SLIRP_POLL_ERR
, opaque
);
498 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
; so
= so_next
) {
499 so_next
= so
->so_next
;
501 so
->pollfds_idx
= -1;
504 * See if it's timed out
507 if (so
->so_expire
<= curtime
) {
511 slirp
->do_slowtimo
= true; /* Let socket expire */
515 if (so
->so_state
& SS_ISFCONNECTED
) {
516 so
->pollfds_idx
= add_poll(so
->s
,
517 SLIRP_POLL_IN
| SLIRP_POLL_HUP
| SLIRP_POLL_ERR
, opaque
);
521 slirp_update_timeout(slirp
, timeout
);
524 void slirp_pollfds_poll(Slirp
*slirp
, int select_error
,
525 SlirpGetREventsCb get_revents
, void *opaque
)
527 struct socket
*so
, *so_next
;
530 curtime
= slirp
->cb
->clock_get_ns(slirp
->opaque
) / SCALE_MS
;
533 * See if anything has timed out
535 if (slirp
->time_fasttimo
&&
536 ((curtime
- slirp
->time_fasttimo
) >= TIMEOUT_FAST
)) {
538 slirp
->time_fasttimo
= 0;
540 if (slirp
->do_slowtimo
&&
541 ((curtime
- slirp
->last_slowtimo
) >= TIMEOUT_SLOW
)) {
544 slirp
->last_slowtimo
= curtime
;
554 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
;
558 so_next
= so
->so_next
;
561 if (so
->pollfds_idx
!= -1) {
562 revents
= get_revents(so
->pollfds_idx
, opaque
);
565 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1) {
571 * This will soread as well, so no need to
572 * test for SLIRP_POLL_IN below if this succeeds
574 if (revents
& SLIRP_POLL_PRI
) {
577 /* Socket error might have resulted in the socket being
578 * removed, do not try to do anything more with it. */
583 * Check sockets for reading
586 (SLIRP_POLL_IN
| SLIRP_POLL_HUP
| SLIRP_POLL_ERR
)) {
588 * Check for incoming connections
590 if (so
->so_state
& SS_FACCEPTCONN
) {
596 /* Output it if we read something */
598 tcp_output(sototcpcb(so
));
601 /* Socket error might have resulted in the socket being
602 * removed, do not try to do anything more with it. */
608 * Check sockets for writing
610 if (!(so
->so_state
& SS_NOFDREF
) &&
611 (revents
& (SLIRP_POLL_OUT
| SLIRP_POLL_ERR
))) {
613 * Check for non-blocking, still-connecting sockets
615 if (so
->so_state
& SS_ISFCONNECTING
) {
617 so
->so_state
&= ~SS_ISFCONNECTING
;
619 ret
= send(so
->s
, (const void *) &ret
, 0, 0);
621 /* XXXXX Must fix, zero bytes is a NOP */
622 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
623 errno
== EINPROGRESS
|| errno
== ENOTCONN
) {
628 so
->so_state
&= SS_PERSISTENT_MASK
;
629 so
->so_state
|= SS_NOFDREF
;
631 /* else so->so_state &= ~SS_ISFCONNECTING; */
636 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
,
642 /* Call tcp_output in case we need to send a window
643 * update to the guest, otherwise it will be stuck
644 * until it sends a window probe. */
645 tcp_output(sototcpcb(so
));
653 * Incoming packets are sent straight away, they're not buffered.
654 * Incoming UDP data isn't buffered either.
656 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
;
660 so_next
= so
->so_next
;
663 if (so
->pollfds_idx
!= -1) {
664 revents
= get_revents(so
->pollfds_idx
, opaque
);
668 (revents
& (SLIRP_POLL_IN
| SLIRP_POLL_HUP
| SLIRP_POLL_ERR
))) {
674 * Check incoming ICMP relies.
676 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
;
680 so_next
= so
->so_next
;
683 if (so
->pollfds_idx
!= -1) {
684 revents
= get_revents(so
->pollfds_idx
, opaque
);
688 (revents
& (SLIRP_POLL_IN
| SLIRP_POLL_HUP
| SLIRP_POLL_ERR
))) {
697 static void arp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
699 struct slirp_arphdr
*ah
= (struct slirp_arphdr
*)(pkt
+ ETH_HLEN
);
700 uint8_t arp_reply
[MAX(ETH_HLEN
+ sizeof(struct slirp_arphdr
), 64)];
701 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
702 struct slirp_arphdr
*rah
= (struct slirp_arphdr
*)(arp_reply
+ ETH_HLEN
);
704 struct gfwd_list
*ex_ptr
;
706 if (!slirp
->in_enabled
) {
710 ar_op
= ntohs(ah
->ar_op
);
713 if (ah
->ar_tip
== ah
->ar_sip
) {
715 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
719 if ((ah
->ar_tip
& slirp
->vnetwork_mask
.s_addr
) ==
720 slirp
->vnetwork_addr
.s_addr
) {
721 if (ah
->ar_tip
== slirp
->vnameserver_addr
.s_addr
||
722 ah
->ar_tip
== slirp
->vhost_addr
.s_addr
)
725 for (ex_ptr
= slirp
->guestfwd_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
726 if (ex_ptr
->ex_addr
.s_addr
== ah
->ar_tip
)
731 memset(arp_reply
, 0, sizeof(arp_reply
));
733 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
735 /* ARP request for alias/dns mac address */
736 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
737 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
738 memcpy(&reh
->h_source
[2], &ah
->ar_tip
, 4);
739 reh
->h_proto
= htons(ETH_P_ARP
);
741 rah
->ar_hrd
= htons(1);
742 rah
->ar_pro
= htons(ETH_P_IP
);
743 rah
->ar_hln
= ETH_ALEN
;
745 rah
->ar_op
= htons(ARPOP_REPLY
);
746 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
747 rah
->ar_sip
= ah
->ar_tip
;
748 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
749 rah
->ar_tip
= ah
->ar_sip
;
750 slirp_send_packet_all(slirp
, arp_reply
, sizeof(arp_reply
));
754 arp_table_add(slirp
, ah
->ar_sip
, ah
->ar_sha
);
761 void slirp_input(Slirp
*slirp
, const uint8_t *pkt
, int pkt_len
)
766 if (pkt_len
< ETH_HLEN
)
769 proto
= (((uint16_t) pkt
[12]) << 8) + pkt
[13];
772 arp_input(slirp
, pkt
, pkt_len
);
779 /* Note: we add 2 to align the IP header on 4 bytes,
780 * and add the margin for the tcpiphdr overhead */
781 if (M_FREEROOM(m
) < pkt_len
+ TCPIPHDR_DELTA
+ 2) {
782 m_inc(m
, pkt_len
+ TCPIPHDR_DELTA
+ 2);
784 m
->m_len
= pkt_len
+ TCPIPHDR_DELTA
+ 2;
785 memcpy(m
->m_data
+ TCPIPHDR_DELTA
+ 2, pkt
, pkt_len
);
787 m
->m_data
+= TCPIPHDR_DELTA
+ 2 + ETH_HLEN
;
788 m
->m_len
-= TCPIPHDR_DELTA
+ 2 + ETH_HLEN
;
790 if (proto
== ETH_P_IP
) {
792 } else if (proto
== ETH_P_IPV6
) {
798 ncsi_input(slirp
, pkt
, pkt_len
);
806 /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no
807 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
810 static int if_encap4(Slirp
*slirp
, struct mbuf
*ifm
, struct ethhdr
*eh
,
811 uint8_t ethaddr
[ETH_ALEN
])
813 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
815 if (iph
->ip_dst
.s_addr
== 0) {
816 /* 0.0.0.0 can not be a destination address, something went wrong,
817 * avoid making it worse */
820 if (!arp_table_search(slirp
, iph
->ip_dst
.s_addr
, ethaddr
)) {
821 uint8_t arp_req
[ETH_HLEN
+ sizeof(struct slirp_arphdr
)];
822 struct ethhdr
*reh
= (struct ethhdr
*)arp_req
;
823 struct slirp_arphdr
*rah
= (struct slirp_arphdr
*)(arp_req
+ ETH_HLEN
);
825 if (!ifm
->resolution_requested
) {
826 /* If the client addr is not known, send an ARP request */
827 memset(reh
->h_dest
, 0xff, ETH_ALEN
);
828 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
829 memcpy(&reh
->h_source
[2], &slirp
->vhost_addr
, 4);
830 reh
->h_proto
= htons(ETH_P_ARP
);
831 rah
->ar_hrd
= htons(1);
832 rah
->ar_pro
= htons(ETH_P_IP
);
833 rah
->ar_hln
= ETH_ALEN
;
835 rah
->ar_op
= htons(ARPOP_REQUEST
);
838 memcpy(rah
->ar_sha
, special_ethaddr
, ETH_ALEN
- 4);
839 memcpy(&rah
->ar_sha
[2], &slirp
->vhost_addr
, 4);
842 rah
->ar_sip
= slirp
->vhost_addr
.s_addr
;
844 /* target hw addr (none) */
845 memset(rah
->ar_tha
, 0, ETH_ALEN
);
848 rah
->ar_tip
= iph
->ip_dst
.s_addr
;
849 slirp
->client_ipaddr
= iph
->ip_dst
;
850 slirp_send_packet_all(slirp
, arp_req
, sizeof(arp_req
));
851 ifm
->resolution_requested
= true;
853 /* Expire request and drop outgoing packet after 1 second */
854 ifm
->expiration_date
=
855 slirp
->cb
->clock_get_ns(slirp
->opaque
) + 1000000000ULL;
859 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
860 /* XXX: not correct */
861 memcpy(&eh
->h_source
[2], &slirp
->vhost_addr
, 4);
862 eh
->h_proto
= htons(ETH_P_IP
);
869 /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no
870 * packet should be sent, 0 if the packet must be re-queued, 2 if the packet
873 static int if_encap6(Slirp
*slirp
, struct mbuf
*ifm
, struct ethhdr
*eh
,
874 uint8_t ethaddr
[ETH_ALEN
])
876 const struct ip6
*ip6h
= mtod(ifm
, const struct ip6
*);
877 if (!ndp_table_search(slirp
, ip6h
->ip_dst
, ethaddr
)) {
878 if (!ifm
->resolution_requested
) {
879 ndp_send_ns(slirp
, ip6h
->ip_dst
);
880 ifm
->resolution_requested
= true;
881 ifm
->expiration_date
= slirp
->cb
->clock_get_ns(slirp
->opaque
) + 1000000000ULL;
885 eh
->h_proto
= htons(ETH_P_IPV6
);
886 in6_compute_ethaddr(ip6h
->ip_src
, eh
->h_source
);
893 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
896 int if_encap(Slirp
*slirp
, struct mbuf
*ifm
)
899 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
900 uint8_t ethaddr
[ETH_ALEN
];
901 const struct ip
*iph
= (const struct ip
*)ifm
->m_data
;
904 if (ifm
->m_len
+ ETH_HLEN
> sizeof(buf
)) {
910 ret
= if_encap4(slirp
, ifm
, eh
, ethaddr
);
917 ret
= if_encap6(slirp
, ifm
, eh
, ethaddr
);
924 g_assert_not_reached();
928 memcpy(eh
->h_dest
, ethaddr
, ETH_ALEN
);
929 DEBUG_ARG("src = %02x:%02x:%02x:%02x:%02x:%02x",
930 eh
->h_source
[0], eh
->h_source
[1], eh
->h_source
[2],
931 eh
->h_source
[3], eh
->h_source
[4], eh
->h_source
[5]);
932 DEBUG_ARG("dst = %02x:%02x:%02x:%02x:%02x:%02x",
933 eh
->h_dest
[0], eh
->h_dest
[1], eh
->h_dest
[2],
934 eh
->h_dest
[3], eh
->h_dest
[4], eh
->h_dest
[5]);
935 memcpy(buf
+ sizeof(struct ethhdr
), ifm
->m_data
, ifm
->m_len
);
936 slirp_send_packet_all(slirp
, buf
, ifm
->m_len
+ ETH_HLEN
);
940 /* Drop host forwarding rule, return 0 if found. */
942 int slirp_remove_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
946 struct socket
*head
= (is_udp
? &slirp
->udb
: &slirp
->tcb
);
947 struct sockaddr_in addr
;
948 int port
= htons(host_port
);
951 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
952 addr_len
= sizeof(addr
);
953 if ((so
->so_state
& SS_HOSTFWD
) &&
954 getsockname(so
->s
, (struct sockaddr
*)&addr
, &addr_len
) == 0 &&
955 addr
.sin_addr
.s_addr
== host_addr
.s_addr
&&
956 addr
.sin_port
== port
) {
957 so
->slirp
->cb
->unregister_poll_fd(so
->s
, so
->slirp
->opaque
);
968 int slirp_add_hostfwd(Slirp
*slirp
, int is_udp
, struct in_addr host_addr
,
969 int host_port
, struct in_addr guest_addr
, int guest_port
)
971 if (!guest_addr
.s_addr
) {
972 guest_addr
= slirp
->vdhcp_startaddr
;
975 if (!udp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
976 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
979 if (!tcp_listen(slirp
, host_addr
.s_addr
, htons(host_port
),
980 guest_addr
.s_addr
, htons(guest_port
), SS_HOSTFWD
))
988 check_guestfwd(Slirp
*slirp
, struct in_addr
*guest_addr
, int guest_port
)
990 struct gfwd_list
*tmp_ptr
;
992 if (!guest_addr
->s_addr
) {
993 guest_addr
->s_addr
= slirp
->vnetwork_addr
.s_addr
|
994 (htonl(0x0204) & ~slirp
->vnetwork_mask
.s_addr
);
996 if ((guest_addr
->s_addr
& slirp
->vnetwork_mask
.s_addr
) !=
997 slirp
->vnetwork_addr
.s_addr
||
998 guest_addr
->s_addr
== slirp
->vhost_addr
.s_addr
||
999 guest_addr
->s_addr
== slirp
->vnameserver_addr
.s_addr
) {
1003 /* check if the port is "bound" */
1004 for (tmp_ptr
= slirp
->guestfwd_list
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
1005 if (guest_port
== tmp_ptr
->ex_fport
&&
1006 guest_addr
->s_addr
== tmp_ptr
->ex_addr
.s_addr
)
1013 int slirp_add_exec(Slirp
*slirp
, const char *cmdline
,
1014 struct in_addr
*guest_addr
, int guest_port
)
1016 if (!check_guestfwd(slirp
, guest_addr
, guest_port
)) {
1020 add_exec(&slirp
->guestfwd_list
, cmdline
, *guest_addr
, htons(guest_port
));
1024 int slirp_add_guestfwd(Slirp
*slirp
, SlirpWriteCb write_cb
, void *opaque
,
1025 struct in_addr
*guest_addr
, int guest_port
)
1027 if (!check_guestfwd(slirp
, guest_addr
, guest_port
)) {
1031 add_guestfwd(&slirp
->guestfwd_list
, write_cb
, opaque
,
1032 *guest_addr
, htons(guest_port
));
1036 ssize_t
slirp_send(struct socket
*so
, const void *buf
, size_t len
, int flags
)
1038 if (so
->s
== -1 && so
->guestfwd
) {
1039 /* XXX this blocks entire thread. Rewrite to use
1040 * qemu_chr_fe_write and background I/O callbacks */
1041 so
->guestfwd
->write_cb(buf
, len
, so
->guestfwd
->opaque
);
1047 * This should in theory not happen but it is hard to be
1048 * sure because some code paths will end up with so->s == -1
1049 * on a failure but don't dispose of the struct socket.
1050 * Check specifically, so we don't pass -1 to send().
1056 return send(so
->s
, buf
, len
, flags
);
1060 slirp_find_ctl_socket(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
)
1065 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
1066 if (so
->so_faddr
.s_addr
== guest_addr
.s_addr
&&
1067 htons(so
->so_fport
) == guest_port
) {
1074 size_t slirp_socket_can_recv(Slirp
*slirp
, struct in_addr guest_addr
,
1077 struct iovec iov
[2];
1080 so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
1082 if (!so
|| so
->so_state
& SS_NOFDREF
) {
1086 if (!CONN_CANFRCV(so
) || so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_datalen
/2)) {
1090 return sopreprbuf(so
, iov
, NULL
);
1093 void slirp_socket_recv(Slirp
*slirp
, struct in_addr guest_addr
, int guest_port
,
1094 const uint8_t *buf
, int size
)
1097 struct socket
*so
= slirp_find_ctl_socket(slirp
, guest_addr
, guest_port
);
1102 ret
= soreadbuf(so
, (const char *)buf
, size
);
1105 tcp_output(sototcpcb(so
));
1108 void slirp_send_packet_all(Slirp
*slirp
, const void *buf
, size_t len
)
1110 ssize_t ret
= slirp
->cb
->send_packet(buf
, len
, slirp
->opaque
);
1113 g_critical("Failed to send packet, ret: %ld", (long) ret
);
1114 } else if (ret
< len
) {
1115 DEBUG_ERROR("send_packet() didn't send all data: %ld < %lu",
1116 (long) ret
, (unsigned long) len
);