router/dhcpv6: use link-local addresses for DNS
authorSteven Barth <steven@midlink.org>
Thu, 20 Aug 2015 09:33:23 +0000 (11:33 +0200)
committerSteven Barth <steven@midlink.org>
Thu, 20 Aug 2015 09:33:23 +0000 (11:33 +0200)
src/dhcpv6.c
src/odhcpd.c
src/odhcpd.h
src/router.c

index c62a08f9a761f3c58ace52e24d4eb8287b98e47b..9f9116232d050ff9965d10aec0713a1f03863324 100644 (file)
@@ -228,7 +228,7 @@ static void handle_client_request(void *addr, void *data, size_t len,
        size_t dns_cnt = iface->dns_cnt;
 
        if ((dns_cnt == 0) &&
-               odhcpd_get_preferred_interface_address(iface->ifindex, &dns_addr)) {
+               !odhcpd_get_linklocal_interface_address(iface->ifindex, &dns_addr)) {
                dns_addr_ptr = &dns_addr;
                dns_cnt = 1;
        }
@@ -460,7 +460,7 @@ static void relay_server_response(uint8_t *data, size_t len)
                size_t rewrite_cnt = iface->dns_cnt;
 
                if (rewrite_cnt == 0) {
-                       if (odhcpd_get_preferred_interface_address(iface->ifindex, &addr) < 1)
+                       if (odhcpd_get_linklocal_interface_address(iface->ifindex, &addr))
                                return; // Unable to get interface address
 
                        rewrite = &addr;
index 5774a4f5d399006bb2a2b60df219fde88c6cfdc9..9c7f27c4f4ac43b206e8030a05fae44ddba27e2b 100644 (file)
@@ -254,24 +254,21 @@ ssize_t odhcpd_get_interface_addresses(int ifindex,
        return ret;
 }
 
-int odhcpd_get_preferred_interface_address(int ifindex, struct in6_addr *addr)
+int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
 {
-       struct odhcpd_ipaddr ipaddrs[8];
-       ssize_t ip_cnt = odhcpd_get_interface_addresses(ifindex, ipaddrs, ARRAY_SIZE(ipaddrs));
-       uint32_t preferred = 0;
-       int ret = 0;
-
-       for (ssize_t i = 0; i < ip_cnt; i++) {
-               struct odhcpd_ipaddr *ipaddr = &ipaddrs[i];
-
-               if (ipaddr->preferred > preferred || !preferred) {
-                       preferred = ipaddr->preferred;
-                       *addr = ipaddr->addr;
-                       ret = 1;
+               int status = -1;
+               struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, ifindex};
+               socklen_t alen = sizeof(addr);
+               int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+
+               if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
+                               !getsockname(sock, (struct sockaddr*)&addr, &alen)) {
+                       *lladdr = addr.sin6_addr;
+                       status = 0;
                }
-       }
 
-       return ret;
+               close(sock);
+               return status;
 }
 
 void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
index ab24674e5f152c0e301333dbaf04160b2c3acbb6..81f4bee774f3cdf5e6ade2264fe11ec79660032c 100644 (file)
@@ -185,7 +185,7 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
                const struct interface *iface);
 ssize_t odhcpd_get_interface_addresses(int ifindex,
                struct odhcpd_ipaddr *addrs, size_t cnt);
-int odhcpd_get_preferred_interface_address(int ifindex, struct in6_addr *addr);
+int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr);
 struct interface* odhcpd_get_interface_by_name(const char *name);
 int odhcpd_get_interface_config(const char *ifname, const char *what);
 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]);
index 1ae8d1aa7543d0ef1fff0e6c02dcc44ee69fbb33..853b9e8700e6d762f42f8312cb14c397e92430c5 100644 (file)
@@ -264,10 +264,11 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
        // Construct Prefix Information options
        size_t cnt = 0;
 
-       struct in6_addr dns_pref = IN6ADDR_ANY_INIT, *dns_addr = &dns_pref;
-       uint32_t dns_time = 0;
+       struct in6_addr dns_pref, *dns_addr = &dns_pref;
        size_t dns_cnt = 1;
 
+       odhcpd_get_linklocal_interface_address(iface->ifindex, &dns_pref);
+
        for (ssize_t i = 0; i < ipcnt; ++i) {
                struct odhcpd_ipaddr *addr = &addrs[i];
                if (addr->prefix > 96 || addr->valid <= (uint32_t)now)
@@ -316,12 +317,6 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                        p->nd_opt_pi_preferred_time = htonl(TIME_LEFT(addr->preferred, now));
                else if (addr->valid - now < 7200)
                        p->nd_opt_pi_valid_time = 0;
-
-
-               if (TIME_LEFT(addr->preferred, now) > dns_time) {
-                       dns_time = TIME_LEFT(addr->preferred, now);
-                       dns_pref = addr->addr;
-               }
        }
 
        if (!iface->default_router && ntohs(adv.h.nd_ra_router_lifetime) == 1) {
@@ -334,7 +329,6 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
        if (iface->dns_cnt > 0) {
                dns_addr = iface->dns;
                dns_cnt = iface->dns_cnt;
-               dns_time = 0;
        }
 
        if (!dns_addr || IN6_IS_ADDR_UNSPECIFIED(dns_addr))
@@ -346,7 +340,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                uint8_t pad;
                uint8_t pad2;
                uint32_t lifetime;
-       } dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, htonl(dns_time)};
+       } dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, 0};
 
 
 
@@ -440,9 +434,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
        minival = (maxival * 3) / 4;
 
        search->lifetime = htonl(maxvalid / 1000);
-
-       if (!dns.lifetime)
-               dns.lifetime = search->lifetime;
+       dns.lifetime = search->lifetime;
 
        odhcpd_urandom(&msecs, sizeof(msecs));
        msecs = (labs(msecs) % (maxival - minival)) + minival;
@@ -569,7 +561,7 @@ static void forward_router_advertisement(uint8_t *data, size_t len)
                        size_t rewrite_cnt = iface->dns_cnt;
 
                        if (rewrite_cnt == 0) {
-                               if (odhcpd_get_preferred_interface_address(iface->ifindex, &addr) < 1)
+                               if (odhcpd_get_linklocal_interface_address(iface->ifindex, &addr))
                                        continue; // Unable to comply
 
                                rewrite = &addr;