]> git.decadent.org.uk Git - odhcp6c.git/blobdiff - src/dhcpv6.c
First hnet-based version.
[odhcp6c.git] / src / dhcpv6.c
index 4bca6e78e6f85b34383215335c825d8426d17232..013e215c6555d1a0f2ffa7cf794efb5f04799c73 100644 (file)
@@ -44,7 +44,7 @@
 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
                const uint8_t transaction[3], enum dhcpv6_msg type);
 
-static time_t dhcpv6_parse_ia(void *opt, void *end);
+static uint32_t dhcpv6_parse_ia(void *opt, void *end);
 
 static reply_handler dhcpv6_handle_reply;
 static reply_handler dhcpv6_handle_advert;
@@ -76,7 +76,7 @@ static struct dhcpv6_retx dhcpv6_retx[_DHCPV6_MSG_MAX] = {
 // Sockets
 static int sock = -1;
 static int ifindex = -1;
-static time_t t1 = 0, t2 = 0, t3 = 0;
+static int64_t t1 = 0, t2 = 0, t3 = 0;
 
 // IA states
 static int request_prefix = -1;
@@ -135,7 +135,8 @@ int init_dhcpv6(const char *ifname, int request_pd)
                        htons(DHCPV6_OPT_DNS_DOMAIN),
                        htons(DHCPV6_OPT_NTP_SERVER),
                        htons(DHCPV6_OPT_SIP_SERVER_A),
-                       htons(DHCPV6_OPT_SIP_SERVER_D)};
+                          htons(DHCPV6_OPT_PREFIX_CLASS)
+                         };
        odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
 
 
@@ -207,8 +208,8 @@ static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
        for (size_t i = 0; i < ia_pd_entries; ++i) {
                p[i].type = htons(DHCPV6_OPT_IA_PREFIX);
                p[i].len = htons(sizeof(p[i]) - 4U);
-               p[i].preferred = htonl(e[i].preferred);
-               p[i].valid = htonl(e[i].valid);
+               p[i].preferred = 0;
+               p[i].valid = 0;
                p[i].prefix = e[i].length;
                p[i].addr = e[i].target;
        }
@@ -241,8 +242,8 @@ static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
                pa[i].type = htons(DHCPV6_OPT_IA_ADDR);
                pa[i].len = htons(sizeof(pa[i]) - 4U);
                pa[i].addr = e[i].target;
-               pa[i].preferred = htonl(e[i].preferred);
-               pa[i].valid = htonl(e[i].valid);
+               pa[i].preferred = 0;
+               pa[i].valid = 0;
        }
 
        ia_na = pa;
@@ -520,7 +521,7 @@ static int dhcpv6_handle_advert(_unused enum dhcpv6_msg orig,
                        cand.preference -= 2000;
                } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
                                cand.preference >= 0) {
-                       cand.preference = odata[1];
+                       cand.preference = odata[0];
                } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
                        cand.wants_reconfigure = true;
                } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
@@ -623,7 +624,7 @@ static int dhcpv6_handle_reply(enum dhcpv6_msg orig,
                if (t3 < 0)
                        t3 = 0;
        } else {
-               t1 = t2 = t3 = 86400;
+               t1 = t2 = t3 = UINT32_MAX;
        }
 
        if (opt) {
@@ -640,8 +641,8 @@ static int dhcpv6_handle_reply(enum dhcpv6_msg orig,
                if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
                                && olen > sizeof(struct dhcpv6_ia_hdr)) {
                        struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
-                       time_t l_t1 = ntohl(ia_hdr->t1);
-                       time_t l_t2 = ntohl(ia_hdr->t2);
+                       uint32_t l_t1 = ntohl(ia_hdr->t1);
+                       uint32_t l_t2 = ntohl(ia_hdr->t2);
 
                        // Test ID and T1-T2 validity
                        if (ia_hdr->iaid != 1 || l_t2 < l_t1)
@@ -663,7 +664,7 @@ static int dhcpv6_handle_reply(enum dhcpv6_msg orig,
                        if (l_t2 > 0 && t2 > l_t2)
                                t2 = l_t2;
 
-                       time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
+                       uint32_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
 
                        if (n < t1)
                                t1 = n;
@@ -674,6 +675,12 @@ static int dhcpv6_handle_reply(enum dhcpv6_msg orig,
                        if (n < t3)
                                t3 = n;
 
+                       if (t2 >= t3)
+                               t2 = 8 * t3 / 10;
+
+                       if (t1 >= t2)
+                               t1 = 5 * t2 / 8;
+
                } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
                        if (olen % 16 == 0)
                                odhcp6c_add_state(STATE_DNS, odata, olen);
@@ -713,17 +720,17 @@ static int dhcpv6_handle_reply(enum dhcpv6_msg orig,
 }
 
 
-static time_t dhcpv6_parse_ia(void *opt, void *end)
+static uint32_t dhcpv6_parse_ia(void *opt, void *end)
 {
        uint32_t timeout = UINT32_MAX; // Minimum timeout
        uint16_t otype, olen;
        uint8_t *odata;
 
-       struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT,
-                       0, 0, IN6ADDR_ANY_INIT, 0, 0};
-
        // Update address IA
        dhcpv6_for_each_option(opt, end, otype, olen, odata) {
+          struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT,
+                                        0, 0, IN6ADDR_ANY_INIT, 0, 0, 0};
+
                if (otype == DHCPV6_OPT_IA_PREFIX) {
                        struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
                        if (olen + 4U < sizeof(*prefix))
@@ -737,6 +744,14 @@ static time_t dhcpv6_parse_ia(void *opt, void *end)
 
                        entry.length = prefix->prefix;
                        entry.target = prefix->addr;
+                       uint16_t stype, slen;
+                       uint8_t *sdata;
+
+                        // Find prefix class, if any
+                       dhcpv6_for_each_option(odata, odata + olen,
+                                               stype, slen, sdata)
+                          if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2) 
+                            entry.prefix_class = ntohs(*((uint16_t*)sdata));
 
                        odhcp6c_update_entry(STATE_IA_PD, &entry);
                } else if (otype == DHCPV6_OPT_IA_ADDR) {