]> git.decadent.org.uk Git - odhcp6c.git/blobdiff - src/dhcpv6.c
Avoid updating too often if ISP spams RAs
[odhcp6c.git] / src / dhcpv6.c
index f8a0385cac9d735f77be9b96cfa59beb3653ae79..f905a7836d2ba601d5b98c5a10d72e77bd53da34 100644 (file)
@@ -60,7 +60,7 @@ static int dhcpv6_commit_advert(void);
 static struct dhcpv6_retx dhcpv6_retx[_DHCPV6_MSG_MAX] = {
        [DHCPV6_MSG_UNKNOWN] = {false, 1, 120, "<POLL>",
                        dhcpv6_handle_reconfigure, NULL},
-       [DHCPV6_MSG_SOLICIT] = {true, 1, 3600, "SOLICIT",
+       [DHCPV6_MSG_SOLICIT] = {true, 1, 120, "SOLICIT",
                        dhcpv6_handle_advert, dhcpv6_commit_advert},
        [DHCPV6_MSG_REQUEST] = {true, 1, 30, "REQUEST",
                        dhcpv6_handle_reply, NULL},
@@ -90,9 +90,10 @@ static uint8_t reconf_key[16];
 
 
 
-int init_dhcpv6(const char *ifname, int request_pd)
+int init_dhcpv6(const char *ifname, int request_pd, int sol_timeout)
 {
        request_prefix = request_pd;
+       dhcpv6_retx[DHCPV6_MSG_SOLICIT].max_timeo = sol_timeout;
 
        sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
 
@@ -570,7 +571,8 @@ 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;
+       bool have_na = false;
+       int have_pd = 0;
 
        dhcpv6_for_each_option(opt, end, otype, olen, odata) {
                if (orig == DHCPV6_MSG_SOLICIT &&
@@ -594,9 +596,13 @@ 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)
-                               if (otype == DHCPV6_OPT_IA_PREFIX)
-                                       have_pd = true;
+                       dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
+                               if (otype == DHCPV6_OPT_IA_PREFIX && (olen + 4) >=
+                                               (uint16_t)sizeof(struct dhcpv6_ia_prefix)) {
+                                       struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&d[-4];
+                                       have_pd = p->prefix;
+                               }
+                       }
                } else if (otype == DHCPV6_OPT_IA_NA) {
                        struct dhcpv6_ia_hdr *h = (struct dhcpv6_ia_hdr*)&odata[-4];
                        uint8_t *oend = odata + olen, *d;
@@ -617,7 +623,7 @@ static int dhcpv6_handle_advert(enum dhcpv6_msg orig,
 
        if (pd_mode != IA_MODE_NONE) {
                if (have_pd)
-                       cand.preference += 2000;
+                       cand.preference += 2000 + (128 - have_pd);
                else
                        cand.preference -= 2000;
        }