]> git.decadent.org.uk Git - odhcp6c.git/commitdiff
odhcp6c: improve server selection
authorSteven Barth <steven@midlink.org>
Mon, 7 Oct 2013 08:21:34 +0000 (10:21 +0200)
committerSteven Barth <steven@midlink.org>
Mon, 7 Oct 2013 08:21:34 +0000 (10:21 +0200)
src/dhcpv6.c
src/odhcp6c.c
src/odhcp6c.h

index f128ed88fa52e82a064c9c4065a4dbc14dd4a4a7..0285923a0190dd56d1e783067f3c005c22d18cdf 100644 (file)
@@ -82,7 +82,7 @@ static int64_t t1 = 0, t2 = 0, t3 = 0;
 
 // IA states
 static int request_prefix = -1;
-static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE;
+static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE, pd_mode = IA_MODE_NONE;
 static bool accept_reconfig = false;
 
 // Reconfigure key
@@ -167,9 +167,10 @@ int init_dhcpv6(const char *ifname, int request_pd)
 }
 
 
-void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
+void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd)
 {
-       na_mode = mode;
+       na_mode = na;
+       pd_mode = pd;
 }
 
 
@@ -247,9 +248,7 @@ static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
                .type = htons(DHCPV6_OPT_IA_PREFIX),
                .len = htons(25), .prefix = request_prefix
        };
-       if (request_prefix > 0 && ia_pd_len == 0 &&
-                       (type == DHCPV6_MSG_SOLICIT ||
-                       type == DHCPV6_MSG_REQUEST)) {
+       if (request_prefix > 0 && ia_pd_len == 0 && type == DHCPV6_MSG_SOLICIT) {
                ia_pd = (uint8_t*)&pref;
                ia_pd_len = sizeof(pref);
        }
@@ -331,7 +330,7 @@ static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
        }
 
        // Disable IAs if not used
-       if (type != DHCPV6_MSG_REQUEST && type != DHCPV6_MSG_SOLICIT) {
+       if (type != DHCPV6_MSG_SOLICIT) {
                iov[5].iov_len = 0;
                if (ia_na_len == 0)
                        iov[7].iov_len = 0;
@@ -446,7 +445,7 @@ int dhcpv6_request(enum dhcpv6_msg type)
                                        len = retx->handler_reply(
                                                        type, opt, opt_end);
 
-                               if (round_end - round_start > 1000)
+                               if (len > 0 && round_end - round_start > 1000)
                                        round_end = 1000 + round_start;
                        }
                }
@@ -569,6 +568,7 @@ static int dhcpv6_handle_advert(enum dhcpv6_msg orig,
        uint16_t olen, otype;
        uint8_t *odata;
        struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}, NULL, NULL, 0, 0};
+       bool have_na = false, have_pd = false;
 
        dhcpv6_for_each_option(opt, end, otype, olen, odata) {
                if (orig == DHCPV6_MSG_SOLICIT &&
@@ -581,14 +581,6 @@ static int dhcpv6_handle_advert(enum dhcpv6_msg orig,
                if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
                        memcpy(cand.duid, odata, olen);
                        cand.duid_len = olen;
-               } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
-                               && odata[1] == DHCPV6_NoAddrsAvail) {
-                       if (na_mode == IA_MODE_FORCE) {
-                               return -1;
-                       } else {
-                               cand.has_noaddravail = true;
-                               cand.preference -= 1000;
-                       }
                } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
                                && odata[1] == DHCPV6_NoPrefixAvail) {
                        cand.preference -= 2000;
@@ -600,17 +592,34 @@ static int dhcpv6_handle_advert(enum dhcpv6_msg orig,
                } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
                        struct dhcpv6_ia_hdr *h = (struct dhcpv6_ia_hdr*)&odata[-4];
                        uint8_t *oend = odata + olen, *d;
-                       dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
+                       dhcpv6_for_each_option(&h[1], oend, otype, olen, d)
                                if (otype == DHCPV6_OPT_IA_PREFIX)
-                                       cand.preference += 2000;
-                               else if (otype == DHCPV6_OPT_STATUS &&
-                                               olen >= 2 && d[0] == 0 &&
-                                               d[1] == DHCPV6_NoPrefixAvail)
-                                       cand.preference -= 2000;
-                       }
+                                       have_pd = true;
+               } else if (otype == DHCPV6_OPT_IA_NA) {
+                       struct dhcpv6_ia_hdr *h = (struct dhcpv6_ia_hdr*)&odata[-4];
+                       uint8_t *oend = odata + olen, *d;
+                       dhcpv6_for_each_option(&h[1], oend, otype, olen, d)
+                               if (otype == DHCPV6_OPT_IA_ADDR)
+                                       have_na = true;
                }
        }
 
+       if ((!have_na && na_mode == IA_MODE_FORCE) ||
+                       (!have_pd && pd_mode == IA_MODE_FORCE))
+               return -1;
+
+       if (na_mode != IA_MODE_NONE && !have_na) {
+               cand.has_noaddravail = true;
+               cand.preference -= 1000;
+       }
+
+       if (pd_mode != IA_MODE_NONE) {
+               if (have_pd)
+                       cand.preference += 2000;
+               else
+                       cand.preference -= 2000;
+       }
+
        if (cand.duid_len > 0) {
                cand.ia_na = odhcp6c_move_state(STATE_IA_NA, &cand.ia_na_len);
                cand.ia_pd = odhcp6c_move_state(STATE_IA_PD, &cand.ia_pd_len);
index d35da2e7a08be0ab66072e01190bb640c9d2a652..7e0710b7e1a10a5d59bb72bd13b48cdebcd8a01b 100644 (file)
@@ -56,26 +56,29 @@ int main(_unused int argc, char* const argv[])
        char *optpos;
        uint16_t opttype;
        enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
+       enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_TRY;
        static struct in6_addr ifid = IN6ADDR_ANY_INIT;
 
        bool help = false, daemonize = false;
        int logopt = LOG_PID;
        int c, request_pd = 0;
-       while ((c = getopt(argc, argv, "S::N:P:c:i:r:s:khedp:")) != -1) {
+       while ((c = getopt(argc, argv, "S::N:P:Fc:i:r:s:khedp:")) != -1) {
                switch (c) {
                case 'S':
                        allow_slaac_only = (optarg) ? atoi(optarg) : -1;
                        break;
 
                case 'N':
-                       if (!strcmp(optarg, "force"))
+                       if (!strcmp(optarg, "force")) {
                                ia_na_mode = IA_MODE_FORCE;
-                       else if (!strcmp(optarg, "none"))
+                               allow_slaac_only = -1;
+                       } else if (!strcmp(optarg, "none")) {
                                ia_na_mode = IA_MODE_NONE;
-                       else if (!strcmp(optarg, "try"))
+                       } else if (!strcmp(optarg, "try")) {
                                ia_na_mode = IA_MODE_TRY;
-                       else
+                       } else{
                                help = true;
+                       }
                        break;
 
                case 'P':
@@ -85,6 +88,13 @@ int main(_unused int argc, char* const argv[])
                        request_pd = strtoul(optarg, NULL, 10);
                        if (request_pd == 0)
                                request_pd = -1;
+
+                       ia_pd_mode = IA_MODE_TRY;
+                       break;
+
+               case 'F':
+                       allow_slaac_only = -1;
+                       ia_pd_mode = IA_MODE_FORCE;
                        break;
 
                case 'c':
@@ -198,7 +208,7 @@ int main(_unused int argc, char* const argv[])
                odhcp6c_clear_state(STATE_SNTP_FQDN);
                odhcp6c_clear_state(STATE_SIP_IP);
                odhcp6c_clear_state(STATE_SIP_FQDN);
-               dhcpv6_set_ia_na_mode(ia_na_mode);
+               dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
                bound = false;
 
                // Server candidates need deep-delete
@@ -329,6 +339,7 @@ static int usage(void)
        "       -S <time>       Wait at least <time> sec for a DHCP-server (0)\n"
        "       -N <mode>       Mode for requesting addresses [try|force|none]\n"
        "       -P <length>     Request IPv6-Prefix (0 = auto)\n"
+       "       -F              Force IPv6-Prefix\n"
        "       -c <clientid>   Override client-ID (base-16 encoded)\n"
        "       -i <iface-id>   Use a custom interface identifier for RA handling\n"
        "       -r <options>    Options to be requested (comma-separated)\n"
index eb76b6b497ecd794d5c68eee1b45e4ab40393c76..b6f0ff2331d33588b39d9ee0273d6e4a066b8c43 100644 (file)
@@ -224,7 +224,7 @@ struct odhcp6c_entry {
 
 
 int init_dhcpv6(const char *ifname, int request_pd);
-void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode);
+void dhcpv6_set_ia_mode(enum odhcp6c_ia_mode na, enum odhcp6c_ia_mode pd);
 int dhcpv6_request(enum dhcpv6_msg type);
 int dhcpv6_poll_reconfigure(void);