slirp: improve send_packet() callback
[qemu/ar7.git] / slirp / slirp.c
blob2bc53e3e127eef0bd4f78dceaf90e29e1402d593
1 /*
2 * libslirp glue
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
22 * THE SOFTWARE.
24 #include "slirp.h"
26 #ifdef WITH_QEMU
27 #include "state.h"
28 #endif
30 #ifndef _WIN32
31 #include <net/if.h>
32 #endif
34 int slirp_debug;
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
49 unsigned curtime;
51 static QTAILQ_HEAD(, Slirp) slirp_instances =
52 QTAILQ_HEAD_INITIALIZER(slirp_instances);
54 static struct in_addr dns_addr;
55 #ifndef _WIN32
56 static struct in6_addr dns6_addr;
57 #endif
58 static unsigned dns_addr_time;
59 #ifndef _WIN32
60 static unsigned dns6_addr_time;
61 #endif
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 */
68 #ifdef _WIN32
70 int get_dns_addr(struct in_addr *pdns_addr)
72 FIXED_INFO *FixedInfo=NULL;
73 ULONG BufLen;
74 DWORD ret;
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;
80 return 0;
83 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
84 BufLen = sizeof(FIXED_INFO);
86 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
87 if (FixedInfo) {
88 GlobalFree(FixedInfo);
89 FixedInfo = NULL;
91 FixedInfo = GlobalAlloc(GPTR, BufLen);
94 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
95 printf("GetNetworkParams failed. ret = %08x\n", (unsigned)ret );
96 if (FixedInfo) {
97 GlobalFree(FixedInfo);
98 FixedInfo = NULL;
100 return -1;
103 pIPAddr = &(FixedInfo->DnsServerList);
104 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
105 *pdns_addr = tmp_addr;
106 dns_addr = tmp_addr;
107 dns_addr_time = curtime;
108 if (FixedInfo) {
109 GlobalFree(FixedInfo);
110 FixedInfo = NULL;
112 return 0;
115 int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id)
117 return -1;
120 static void winsock_cleanup(void)
122 WSACleanup();
125 #else
127 static int get_dns_addr_cached(void *pdns_addr, void *cached_addr,
128 socklen_t addrlen,
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);
134 return 0;
136 old_stat = *cached_stat;
137 if (stat("/etc/resolv.conf", cached_stat) != 0) {
138 return -1;
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);
145 return 0;
147 return 1;
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)
154 char buff[512];
155 char buff2[257];
156 FILE *f;
157 int found = 0;
158 void *tmp_addr = alloca(addrlen);
159 unsigned if_index;
161 f = fopen("/etc/resolv.conf", "r");
162 if (!f)
163 return -1;
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, '%');
169 if (c) {
170 if_index = if_nametoindex(c + 1);
171 *c = '\0';
172 } else {
173 if_index = 0;
176 if (!inet_pton(af, buff2, tmp_addr)) {
177 continue;
179 /* If it's the first one, set it to dns_addr */
180 if (!found) {
181 memcpy(pdns_addr, tmp_addr, addrlen);
182 memcpy(cached_addr, tmp_addr, addrlen);
183 if (scope_id) {
184 *scope_id = if_index;
186 *cached_time = curtime;
189 if (++found > 3) {
190 DEBUG_MISC(" (more)");
191 break;
192 } else if (slirp_debug & DBG_MISC) {
193 char s[INET6_ADDRSTRLEN];
194 const char *res = inet_ntop(af, tmp_addr, s, sizeof(s));
195 if (!res) {
196 res = " (string conversion error)";
198 DEBUG_MISC(" %s", res);
202 fclose(f);
203 if (!found)
204 return -1;
205 return 0;
208 int get_dns_addr(struct in_addr *pdns_addr)
210 static struct stat dns_addr_stat;
212 if (dns_addr.s_addr != 0) {
213 int ret;
214 ret = get_dns_addr_cached(pdns_addr, &dns_addr, sizeof(dns_addr),
215 &dns_addr_stat, &dns_addr_time);
216 if (ret <= 0) {
217 return ret;
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)) {
229 int ret;
230 ret = get_dns_addr_cached(pdns6_addr, &dns6_addr, sizeof(dns6_addr),
231 &dns6_addr_stat, &dns6_addr_time);
232 if (ret <= 0) {
233 return ret;
236 return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr,
237 sizeof(dns6_addr),
238 scope_id, &dns6_addr_time);
241 #endif
243 static void slirp_init_once(void)
245 static int initialized;
246 const char *debug;
247 #ifdef _WIN32
248 WSADATA Data;
249 #endif
251 if (initialized) {
252 return;
254 initialized = 1;
256 #ifdef _WIN32
257 WSAStartup(MAKEWORD(2,0), &Data);
258 atexit(winsock_cleanup);
259 #endif
261 loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
262 loopback_mask = htonl(IN_CLASSA_NET);
264 debug = g_getenv("SLIRP_DEBUG");
265 if (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,
280 bool in6_enabled,
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,
289 void *opaque)
291 Slirp *slirp = g_malloc0(sizeof(Slirp));
293 slirp_init_once();
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;
302 if_init(slirp);
303 ip_init(slirp);
304 ip6_init(slirp);
306 /* Initialise mbufs *after* setting the MTU */
307 m_init(slirp);
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;
315 if (vhostname) {
316 slirp_pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
317 vhostname);
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);
327 if (vdnssearch) {
328 translate_dnssearch(slirp, vdnssearch);
331 slirp->opaque = opaque;
333 #ifdef WITH_QEMU
334 slirp_state_register(slirp);
335 #endif
336 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
338 return slirp;
341 void slirp_cleanup(Slirp *slirp)
343 struct gfwd_list *e, *next;
345 for (e = slirp->guestfwd_list; e; e = next) {
346 next = e->ex_next;
347 g_free(e->ex_exec);
348 g_free(e);
351 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
352 #ifdef WITH_QEMU
353 slirp_state_unregister(slirp);
354 #endif
355 ip_cleanup(slirp);
356 ip6_cleanup(slirp);
357 m_cleanup(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);
365 g_free(slirp);
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)
373 Slirp *slirp;
374 uint32_t t;
376 if (*timeout <= TIMEOUT_FAST) {
377 return;
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;
388 return;
390 if (slirp->do_slowtimo) {
391 t = MIN(TIMEOUT_SLOW, t);
394 *timeout = t;
397 void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
399 Slirp *slirp;
400 struct socket *so, *so_next;
402 if (QTAILQ_EMPTY(&slirp_instances)) {
403 return;
407 * First, TCP sockets
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;
419 so = so_next) {
420 int events = 0;
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) {
439 continue;
443 * Set for reading sockets which are accepting
445 if (so->so_state & SS_FACCEPTCONN) {
446 GPollFD pfd = {
447 .fd = so->s,
448 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
450 so->pollfds_idx = pollfds->len;
451 g_array_append_val(pollfds, pfd);
452 continue;
456 * Set for writing sockets which are connecting
458 if (so->so_state & SS_ISFCONNECTING) {
459 GPollFD pfd = {
460 .fd = so->s,
461 .events = G_IO_OUT | G_IO_ERR,
463 so->pollfds_idx = pollfds->len;
464 g_array_append_val(pollfds, pfd);
465 continue;
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;
485 if (events) {
486 GPollFD pfd = {
487 .fd = so->s,
488 .events = events,
490 so->pollfds_idx = pollfds->len;
491 g_array_append_val(pollfds, pfd);
496 * UDP sockets
498 for (so = slirp->udb.so_next; so != &slirp->udb;
499 so = so_next) {
500 so_next = so->so_next;
502 so->pollfds_idx = -1;
505 * See if it's timed out
507 if (so->so_expire) {
508 if (so->so_expire <= curtime) {
509 udp_detach(so);
510 continue;
511 } else {
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
524 * (XXX <= 4 ?)
526 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
527 GPollFD pfd = {
528 .fd = so->s,
529 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
531 so->pollfds_idx = pollfds->len;
532 g_array_append_val(pollfds, pfd);
537 * ICMP sockets
539 for (so = slirp->icmp.so_next; so != &slirp->icmp;
540 so = so_next) {
541 so_next = so->so_next;
543 so->pollfds_idx = -1;
546 * See if it's timed out
548 if (so->so_expire) {
549 if (so->so_expire <= curtime) {
550 icmp_detach(so);
551 continue;
552 } else {
553 slirp->do_slowtimo = true; /* Let socket expire */
557 if (so->so_state & SS_ISFCONNECTED) {
558 GPollFD pfd = {
559 .fd = so->s,
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;
574 int ret;
576 if (!slirp) {
577 return;
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)) {
588 tcp_fasttimo(slirp);
589 slirp->time_fasttimo = 0;
591 if (slirp->do_slowtimo &&
592 ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
593 ip_slowtimo(slirp);
594 tcp_slowtimo(slirp);
595 slirp->last_slowtimo = curtime;
599 * Check sockets
601 if (!select_error) {
603 * Check TCP sockets
605 for (so = slirp->tcb.so_next; so != &slirp->tcb;
606 so = so_next) {
607 int revents;
609 so_next = so->so_next;
611 revents = 0;
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) {
618 continue;
622 * Check for URG data
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) {
627 ret = sorecvoob(so);
628 if (ret < 0) {
629 /* Socket error might have resulted in the socket being
630 * removed, do not try to do anything more with it. */
631 continue;
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) {
642 tcp_connect(so);
643 continue;
644 } /* else */
645 ret = soread(so);
647 /* Output it if we read something */
648 if (ret > 0) {
649 tcp_output(sototcpcb(so));
651 if (ret < 0) {
652 /* Socket error might have resulted in the socket being
653 * removed, do not try to do anything more with it. */
654 continue;
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) {
667 /* Connected */
668 so->so_state &= ~SS_ISFCONNECTING;
670 ret = send(so->s, (const void *) &ret, 0, 0);
671 if (ret < 0) {
672 /* XXXXX Must fix, zero bytes is a NOP */
673 if (errno == EAGAIN || errno == EWOULDBLOCK ||
674 errno == EINPROGRESS || errno == ENOTCONN) {
675 continue;
678 /* else failed */
679 so->so_state &= SS_PERSISTENT_MASK;
680 so->so_state |= SS_NOFDREF;
682 /* else so->so_state &= ~SS_ISFCONNECTING; */
685 * Continue tcp_input
687 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
688 so->so_ffamily);
689 /* continue; */
690 } else {
691 ret = sowrite(so);
692 if (ret > 0) {
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));
703 * Now UDP sockets.
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;
708 so = so_next) {
709 int revents;
711 so_next = so->so_next;
713 revents = 0;
714 if (so->pollfds_idx != -1) {
715 revents = g_array_index(pollfds, GPollFD,
716 so->pollfds_idx).revents;
719 if (so->s != -1 &&
720 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
721 sorecvfrom(so);
726 * Check incoming ICMP relies.
728 for (so = slirp->icmp.so_next; so != &slirp->icmp;
729 so = so_next) {
730 int revents;
732 so_next = so->so_next;
734 revents = 0;
735 if (so->pollfds_idx != -1) {
736 revents = g_array_index(pollfds, GPollFD,
737 so->pollfds_idx).revents;
740 if (so->s != -1 &&
741 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
742 icmp_receive(so);
747 if_start(slirp);
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);
757 int ar_op;
758 struct gfwd_list *ex_ptr;
760 if (!slirp->in_enabled) {
761 return;
764 ar_op = ntohs(ah->ar_op);
765 switch(ar_op) {
766 case ARPOP_REQUEST:
767 if (ah->ar_tip == ah->ar_sip) {
768 /* Gratuitous ARP */
769 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
770 return;
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)
777 goto arp_ok;
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)
780 goto arp_ok;
782 return;
783 arp_ok:
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;
797 rah->ar_pln = 4;
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));
805 break;
806 case ARPOP_REPLY:
807 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
808 break;
809 default:
810 break;
814 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
816 struct mbuf *m;
817 int proto;
819 if (pkt_len < ETH_HLEN)
820 return;
822 proto = (((uint16_t) pkt[12]) << 8) + pkt[13];
823 switch(proto) {
824 case ETH_P_ARP:
825 arp_input(slirp, pkt, pkt_len);
826 break;
827 case ETH_P_IP:
828 case ETH_P_IPV6:
829 m = m_get(slirp);
830 if (!m)
831 return;
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) {
844 ip_input(m);
845 } else if (proto == ETH_P_IPV6) {
846 ip6_input(m);
848 break;
850 case ETH_P_NCSI:
851 ncsi_input(slirp, pkt, pkt_len);
852 break;
854 default:
855 break;
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
861 * is ready to go.
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 */
871 return 1;
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;
887 rah->ar_pln = 4;
888 rah->ar_op = htons(ARPOP_REQUEST);
890 /* source hw addr */
891 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
892 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
894 /* source IP */
895 rah->ar_sip = slirp->vhost_addr.s_addr;
897 /* target hw addr (none) */
898 memset(rah->ar_tha, 0, ETH_ALEN);
900 /* target IP */
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;
909 return 0;
910 } else {
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);
916 /* Send this */
917 return 2;
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
923 * is ready to go.
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;
935 return 0;
936 } else {
937 eh->h_proto = htons(ETH_P_IPV6);
938 in6_compute_ethaddr(ip6h->ip_src, eh->h_source);
940 /* Send this */
941 return 2;
945 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
946 * re-queued.
948 int if_encap(Slirp *slirp, struct mbuf *ifm)
950 uint8_t buf[1600];
951 struct ethhdr *eh = (struct ethhdr *)buf;
952 uint8_t ethaddr[ETH_ALEN];
953 const struct ip *iph = (const struct ip *)ifm->m_data;
954 int ret;
956 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
957 return 1;
960 switch (iph->ip_v) {
961 case IPVERSION:
962 ret = if_encap4(slirp, ifm, eh, ethaddr);
963 if (ret < 2) {
964 return ret;
966 break;
968 case IP6VERSION:
969 ret = if_encap6(slirp, ifm, eh, ethaddr);
970 if (ret < 2) {
971 return ret;
973 break;
975 default:
976 g_assert_not_reached();
977 break;
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);
989 return 1;
992 /* Drop host forwarding rule, return 0 if found. */
993 int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
994 int host_port)
996 struct socket *so;
997 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
998 struct sockaddr_in addr;
999 int port = htons(host_port);
1000 socklen_t addr_len;
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);
1010 sofree(so);
1011 return 0;
1015 return -1;
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;
1024 if (is_udp) {
1025 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
1026 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
1027 return -1;
1028 } else {
1029 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
1030 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
1031 return -1;
1033 return 0;
1036 static bool
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) {
1049 return false;
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)
1056 return false;
1059 return true;
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)) {
1066 return -1;
1069 add_exec(&slirp->guestfwd_list, cmdline, *guest_addr, htons(guest_port));
1070 return 0;
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)) {
1077 return -1;
1080 add_guestfwd(&slirp->guestfwd_list, write_cb, opaque,
1081 *guest_addr, htons(guest_port));
1082 return 0;
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);
1091 return len;
1094 if (so->s == -1) {
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().
1101 errno = EBADF;
1102 return -1;
1105 return send(so->s, buf, len, flags);
1108 struct socket *
1109 slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
1111 struct socket *so;
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) {
1116 return so;
1119 return NULL;
1122 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
1123 int guest_port)
1125 struct iovec iov[2];
1126 struct socket *so;
1128 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1130 if (!so || so->so_state & SS_NOFDREF) {
1131 return 0;
1134 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) {
1135 return 0;
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)
1144 int ret;
1145 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
1147 if (!so)
1148 return;
1150 ret = soreadbuf(so, (const char *)buf, size);
1152 if (ret > 0)
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);
1160 if (ret < 0) {
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);