Skip to contentSkip to conversation

slirp: Add dns6 support

PATCH In review Samuel Thibault 2 participants 6 messages Mar 20, 2016 – Mar 24, 2016

lore
Share & tools
lore
#0 lore raw

cover slirp: Add dns6 support

This adds support for DNS over IPv6 in slirp, which is notably useful when the host has only an IPv6 DNS server.

This series depends on "slirp: Allow to disable IPv4 or IPv6" which defines in6_zero.

Samuel Thibault (4):
slirp: Split get_dns_addr
slirp: Add dns6 resolution
slirp: Support link-local DNS addresses
slirp: Add RDNSS advertisement

 slirp/ip6_icmp.c |  19 +++++++--
 slirp/ip6_icmp.h |  12 +++++-
 slirp/libslirp.h |   1 +
 slirp/slirp.c    | 117 ++++++++++++++++++++++++++++++++++++++++++-------------
 slirp/socket.c   |   4 +-
 5 files changed, 120 insertions(+), 33 deletions(-)
Signature · 1 line
2.7.0
#1 lore raw

PATCH 1/4 slirp: Split get_dns_addr

Separate get_dns_addr into get_dns_addr_cached and get_dns_addr_resolv_conf to make conversion to IPv6 easier.

---
 slirp/slirp.c | 53 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 19 deletions(-)
slirp/slirp.c +34 -19
index e4a32ae..36534fc 100644
@@ -108,7 +108,28 @@ static void winsock_cleanup(void)
 
 static struct stat dns_addr_stat;
 
-int get_dns_addr(struct in_addr *pdns_addr)
+static int get_dns_addr_cached(struct in_addr *pdns_addr)
+{
+    struct stat old_stat;
+    if ((curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
+        *pdns_addr = dns_addr;
+        return 0;
+    }
+    old_stat = dns_addr_stat;
+    if (stat("/etc/resolv.conf", &dns_addr_stat) != 0) {
+        return -1;
+    }
+    if ((dns_addr_stat.st_dev == old_stat.st_dev)
+        && (dns_addr_stat.st_ino == old_stat.st_ino)
+        && (dns_addr_stat.st_size == old_stat.st_size)
+        && (dns_addr_stat.st_mtime == old_stat.st_mtime)) {
+        *pdns_addr = dns_addr;
+        return 0;
+    }
+    return 1;
+}
+
+static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr)
 {
     char buff[512];
     char buff2[257];
@@ -116,24 +137,6 @@ int get_dns_addr(struct in_addr *pdns_addr)
     int found = 0;
     struct in_addr tmp_addr;
 
-    if (dns_addr.s_addr != 0) {
-        struct stat old_stat;
-        if ((curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
-            *pdns_addr = dns_addr;
-            return 0;
-        }
-        old_stat = dns_addr_stat;
-        if (stat("/etc/resolv.conf", &dns_addr_stat) != 0)
-            return -1;
-        if ((dns_addr_stat.st_dev == old_stat.st_dev)
-            && (dns_addr_stat.st_ino == old_stat.st_ino)
-            && (dns_addr_stat.st_size == old_stat.st_size)
-            && (dns_addr_stat.st_mtime == old_stat.st_mtime)) {
-            *pdns_addr = dns_addr;
-            return 0;
-        }
-    }
-
     f = fopen("/etc/resolv.conf", "r");
     if (!f)
         return -1;
@@ -173,6 +176,18 @@ int get_dns_addr(struct in_addr *pdns_addr)
     return 0;
 }
 
+int get_dns_addr(struct in_addr *pdns_addr)
+{
+    if (dns_addr.s_addr != 0) {
+        int ret;
+        ret = get_dns_addr_cached(pdns_addr);
+        if (ret <= 0) {
+            return ret;
+        }
+    }
+    return get_dns_addr_resolv_conf(pdns_addr);
+}
+
 #endif
 
 static void slirp_init_once(void)
Signature · 1 line
2.7.0
#2 lore raw

PATCH 2/4 slirp: Add dns6 resolution

In reply to Samuel Thibault

This makes get_dns_addr address family-agnostic, thus allowing to add the IPv6 case.

---
 slirp/libslirp.h |  1 +
 slirp/slirp.c    | 72 ++++++++++++++++++++++++++++++++++++++++----------------
 slirp/socket.c   |  4 ++--
 3 files changed, 55 insertions(+), 22 deletions(-)
slirp/libslirp.h +1 -0
index c4b25c9..eea9be4 100644
@@ -7,6 +7,7 @@ struct Slirp;
 typedef struct Slirp Slirp;
 
 int get_dns_addr(struct in_addr *pdns_addr);
+int get_dns6_addr(struct in6_addr *pdns6_addr);
 
 Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   struct in_addr vnetmask, struct in_addr vhost,
slirp/slirp.c +52 -20

137 lines not on this page · raw message

slirp/socket.c +2 -2
index b836c42..653257d 100644
@@ -796,9 +796,9 @@ void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
         if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
                     slirp->vprefix_len)) {
             if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) {
-                /*if (get_dns_addr(&addr) < 0) {*/ /* TODO */
+                if (get_dns6_addr(&sin6->sin6_addr) < 0) {
                     sin6->sin6_addr = in6addr_loopback;
-                /*}*/
+                }
             } else {
                 sin6->sin6_addr = in6addr_loopback;
             }
Signature · 1 line
2.7.0
#3 lore raw

PATCH 3/4 slirp: Support link-local DNS addresses

In reply to Samuel Thibault

They look like fe80::%eth0

---
 slirp/libslirp.h |  2 +-
 slirp/slirp.c    | 26 ++++++++++++++++++++++----
 slirp/socket.c   |  2 +-
 3 files changed, 24 insertions(+), 6 deletions(-)
slirp/libslirp.h +1 -1
index eea9be4..bf3a420 100644
@@ -7,7 +7,7 @@ struct Slirp;
 typedef struct Slirp Slirp;
 
 int get_dns_addr(struct in_addr *pdns_addr);
-int get_dns6_addr(struct in6_addr *pdns6_addr);
+int get_dns6_addr(struct in6_addr *pdns6_addr, unsigned *scope_id);
 
 Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   struct in_addr vnetmask, struct in_addr vhost,
slirp/slirp.c +22 -4
index 3deb4c2..5e328ff 100644
@@ -29,6 +29,10 @@
 #include "slirp.h"
 #include "hw/hw.h"
 
+#ifndef _WIN32
+#include <net/if.h>
+#endif
+
 /* host loopback address */
 struct in_addr loopback_addr;
 /* host loopback network mask */
@@ -137,13 +141,15 @@ static int get_dns_addr_cached(void *pdns_addr, void *cached_addr,
 }
 
 static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
-                                    socklen_t addrlen, u_int *cached_time)
+                                    socklen_t addrlen, unsigned *scope_id,
+                                    u_int *cached_time)
 {
     char buff[512];
     char buff2[257];
     FILE *f;
     int found = 0;
     void *tmp_addr = alloca(addrlen);
+    unsigned if_index;
 #ifdef DEBUG
     char s[INET6_ADDRSTRLEN];
 #endif
@@ -157,6 +163,14 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
 #endif
     while (fgets(buff, 512, f) != NULL) {
         if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
+            char *c = strchr(buff2, '%');
+            if (c) {
+                if_index = if_nametoindex(c + 1);
+                *c = '\0';
+            } else {
+                if_index = 0;
+            }
+
             if (!inet_pton(af, buff2, tmp_addr)) {
                 continue;
             }
@@ -164,6 +178,9 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
             if (!found) {
                 memcpy(pdns_addr, tmp_addr, addrlen);
                 memcpy(cached_addr, tmp_addr, addrlen);
+                if (scope_id) {
+                    *scope_id = if_index;
+                }
                 *cached_time = curtime;
             }
 #ifdef DEBUG
@@ -201,12 +218,12 @@ int get_dns_addr(struct in_addr *pdns_addr)
         }
     }
     return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr,
-                                    sizeof(dns_addr), &dns_addr_time);
+                                    sizeof(dns_addr), NULL, &dns_addr_time);
 }
 
 static struct stat dns6_addr_stat;
 
-int get_dns6_addr(struct in6_addr *pdns6_addr)
+int get_dns6_addr(struct in6_addr *pdns6_addr, unsigned *scope_id)
 {
     if (!in6_zero(&dns6_addr)) {
         int ret;
@@ -217,7 +234,8 @@ int get_dns6_addr(struct in6_addr *pdns6_addr)
         }
     }
     return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr,
-                                    sizeof(dns6_addr), &dns6_addr_time);
+                                    sizeof(dns6_addr),
+                                    scope_id, &dns6_addr_time);
 }
 
 #endif
slirp/socket.c +1 -1
index 653257d..896c27e 100644
@@ -796,7 +796,7 @@ void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
         if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
                     slirp->vprefix_len)) {
             if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) {
-                if (get_dns6_addr(&sin6->sin6_addr) < 0) {
+                if (get_dns6_addr(&sin6->sin6_addr, &sin6->sin6_scope_id) < 0) {
                     sin6->sin6_addr = in6addr_loopback;
                 }
             } else {
Signature · 1 line
2.7.0
#4 lore raw

PATCH 4/4 slirp: Add RDNSS advertisement

In reply to Samuel Thibault

This adds the RDNSS option to IPv6 router advertisements, so that the guest can autoconfigure the DNS server address.

---
 slirp/ip6_icmp.c | 19 ++++++++++++++++---
 slirp/ip6_icmp.h | 12 ++++++++++--
 2 files changed, 26 insertions(+), 5 deletions(-)
slirp/ip6_icmp.c +16 -3
index 69c0a16..c25d423 100644
@@ -151,7 +151,8 @@ void ndp_send_ra(Slirp *slirp)
     rip->ip_nh = IPPROTO_ICMPV6;
     rip->ip_pl = htons(ICMP6_NDP_RA_MINLEN
                         + NDPOPT_LINKLAYER_LEN
-                        + NDPOPT_PREFIXINFO_LEN);
+                        + NDPOPT_PREFIXINFO_LEN
+                        + NDPOPT_RDNSS_LEN);
     t->m_len = sizeof(struct ip6) + ntohs(rip->ip_pl);
 
     /* Build ICMPv6 packet */
@@ -169,16 +170,16 @@ void ndp_send_ra(Slirp *slirp)
     ricmp->icmp6_nra.lifetime = htons(NDP_AdvDefaultLifetime);
     ricmp->icmp6_nra.reach_time = htonl(NDP_AdvReachableTime);
     ricmp->icmp6_nra.retrans_time = htonl(NDP_AdvRetransTime);
+    t->m_data += ICMP6_NDP_RA_MINLEN;
 
     /* Source link-layer address (NDP option) */
-    t->m_data += ICMP6_NDP_RA_MINLEN;
     struct ndpopt *opt = mtod(t, struct ndpopt *);
     opt->ndpopt_type = NDPOPT_LINKLAYER_SOURCE;
     opt->ndpopt_len = NDPOPT_LINKLAYER_LEN / 8;
     in6_compute_ethaddr(rip->ip_src, opt->ndpopt_linklayer);
+    t->m_data += NDPOPT_LINKLAYER_LEN;
 
     /* Prefix information (NDP option) */
-    t->m_data += NDPOPT_LINKLAYER_LEN;
     struct ndpopt *opt2 = mtod(t, struct ndpopt *);
     opt2->ndpopt_type = NDPOPT_PREFIX_INFO;
     opt2->ndpopt_len = NDPOPT_PREFIXINFO_LEN / 8;
@@ -190,8 +191,20 @@ void ndp_send_ra(Slirp *slirp)
     opt2->ndpopt_prefixinfo.pref_lt = htonl(NDP_AdvPrefLifetime);
     opt2->ndpopt_prefixinfo.reserved2 = 0;
     opt2->ndpopt_prefixinfo.prefix = slirp->vprefix_addr6;
+    t->m_data += NDPOPT_PREFIXINFO_LEN;
+
+    /* Prefix information (NDP option) */
+    struct ndpopt *opt3 = mtod(t, struct ndpopt *);
+    opt3->ndpopt_type = NDPOPT_RDNSS;
+    opt3->ndpopt_len = NDPOPT_RDNSS_LEN / 8;
+    opt3->ndpopt_rdnss.reserved = 0;
+    opt3->ndpopt_rdnss.lifetime = htonl(2 * NDP_MaxRtrAdvInterval);
+    opt3->ndpopt_rdnss.addr = slirp->vnameserver_addr6;
+    t->m_data += NDPOPT_RDNSS_LEN;
 
     /* ICMPv6 Checksum */
+    t->m_data -= NDPOPT_RDNSS_LEN;
+    t->m_data -= NDPOPT_PREFIXINFO_LEN;
     t->m_data -= NDPOPT_LINKLAYER_LEN;
     t->m_data -= ICMP6_NDP_RA_MINLEN;
     t->m_data -= sizeof(struct ip6);
slirp/ip6_icmp.h +10 -2
index 9460bf8..2282d29 100644
@@ -122,6 +122,7 @@ struct ndpopt {
     uint8_t     ndpopt_len;                     /* /!\ In units of 8 octets */
     union {
         unsigned char   linklayer_addr[6];      /* Source/Target Link-layer */
+#define ndpopt_linklayer ndpopt_body.linklayer_addr
         struct prefixinfo {                     /* Prefix Information */
             uint8_t     prefix_length;
 #ifdef HOST_WORDS_BIGENDIAN
@@ -134,19 +135,26 @@ struct ndpopt {
             uint32_t    reserved2;
             struct in6_addr prefix;
         } QEMU_PACKED prefixinfo;
-    } ndpopt_body;
-#define ndpopt_linklayer ndpopt_body.linklayer_addr
 #define ndpopt_prefixinfo ndpopt_body.prefixinfo
+        struct rdnss {
+            uint16_t reserved;
+            uint32_t lifetime;
+            struct in6_addr addr;
+        } QEMU_PACKED rdnss;
+#define ndpopt_rdnss ndpopt_body.rdnss
+    } ndpopt_body;
 } QEMU_PACKED;
 
 /* NDP options type */
 #define NDPOPT_LINKLAYER_SOURCE     1   /* Source Link-Layer Address */
 #define NDPOPT_LINKLAYER_TARGET     2   /* Target Link-Layer Address */
 #define NDPOPT_PREFIX_INFO          3   /* Prefix Information */
+#define NDPOPT_RDNSS                25  /* Recursive DNS Server Address */
 
 /* NDP options size, in octets. */
 #define NDPOPT_LINKLAYER_LEN    8
 #define NDPOPT_PREFIXINFO_LEN   32
+#define NDPOPT_RDNSS_LEN        24
 
 /*
  * Definition of type and code field values.
Signature · 1 line
2.7.0
#5 lore raw
In reply to Samuel Thibault

Hello,

Could somebody review this? This will be needed for ipv6-only setups.

Samuel

Quoted 22 lines · Samuel Thibault, on Sun 20 Mar 2016 16:26:24 +0100, wrote:

This adds support for DNS over IPv6 in slirp, which is notably useful when the host has only an IPv6 DNS server.

This series depends on "slirp: Allow to disable IPv4 or IPv6" which defines in6_zero.

Samuel Thibault (4):
slirp: Split get_dns_addr
slirp: Add dns6 resolution
slirp: Support link-local DNS addresses
slirp: Add RDNSS advertisement

 slirp/ip6_icmp.c |  19 +++++++--
 slirp/ip6_icmp.h |  12 +++++-
 slirp/libslirp.h |   1 +
 slirp/slirp.c    | 117 ++++++++++++++++++++++++++++++++++++++++++-------------
 slirp/socket.c   |   4 +-
 5 files changed, 120 insertions(+), 33 deletions(-)
Signature · 1 line
2.7.0
Signature · 4 lines
Samuel
Progress (n.): The process through which the Internet has evolved from
smart people in front of dumb terminals to dumb people in front of smart
terminals.