Avoid updating too often if ISP spams RAs
authorSteven Barth <steven@midlink.org>
Wed, 23 Oct 2013 08:13:00 +0000 (10:13 +0200)
committerSteven Barth <steven@midlink.org>
Wed, 23 Oct 2013 08:13:00 +0000 (10:13 +0200)
src/odhcp6c.c
src/odhcp6c.h
src/ra.c

index 5c433af31e7c90b1fcad9746be36d29734cc6aab..e81b15f92a5bcfc0a8b54895a336cdf46a8d6ace 100644 (file)
@@ -463,17 +463,23 @@ struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct
 }
 
 
-void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
+bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
 {
        size_t len;
        struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
        struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
+       bool changed = true;
 
        if (x && x->valid > new->valid && new->valid < safe)
                new->valid = safe;
 
        if (new->valid > 0) {
                if (x) {
+                       if (new->valid >= x->valid && new->valid - x->valid < 60 &&
+                                       new->preferred >= x->preferred &&
+                                       new->preferred - x->preferred < 60 &&
+                                       x->class == new->class)
+                               changed = false;
                        x->valid = new->valid;
                        x->preferred = new->preferred;
                        x->class = new->class;
@@ -483,12 +489,13 @@ void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *n
        } else if (x) {
                odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
        }
+       return changed;
 }
 
 
-void odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
+bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
 {
-       odhcp6c_update_entry_safe(state, new, 0);
+       return odhcp6c_update_entry_safe(state, new, 0);
 }
 
 
index 669b33d4149172b8240b3c90aedc422e92e546c2..61dee63ffcc9a57b5c918cc5d7e106ca289b4f97 100644 (file)
@@ -250,8 +250,8 @@ void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len);
 
 // Entry manipulation
 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new);
-void odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new);
-void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe);
+bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new);
+bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe);
 
 void odhcp6c_expire(void);
 uint32_t odhcp6c_elapsed(void);
index 83f17584cbe93f9a4587a0e095432fa295b91de1..f41602a587e00b87d10299bbac62df785b873f11 100644 (file)
--- a/src/ra.c
+++ b/src/ra.c
@@ -120,6 +120,7 @@ static void update_proc(const char *sect, const char *opt, uint32_t value)
 bool ra_process(void)
 {
        bool found = false;
+       bool changed = false;
        uint8_t buf[1500], cmsg_buf[128];
        struct nd_router_advert *adv = (struct nd_router_advert*)buf;
        struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0, IN6ADDR_ANY_INIT, 0, 0, 0};
@@ -183,7 +184,7 @@ bool ra_process(void)
                        entry.priority = pref_to_priority(0);
                entry.valid = router_valid;
                entry.preferred = entry.valid;
-               odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
+               changed |= odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
 
                // Parse ND parameters
                if (ntohl(adv->nd_ra_reachable) <= 3600000)
@@ -215,7 +216,7 @@ bool ra_process(void)
                                        continue;
 
                                if (entry.priority > 0)
-                                       odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
+                                       changed |= odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
                        } else if (opt->type == ND_OPT_PREFIX_INFORMATION && opt->len == 4) {
                                struct nd_opt_prefix_info *pinfo = (struct nd_opt_prefix_info*)opt;
                                entry.router = any;
@@ -232,7 +233,7 @@ bool ra_process(void)
                                        continue;
 
                                if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
-                                       odhcp6c_update_entry_safe(STATE_RA_ROUTE, &entry, 7200);
+                                       changed |= odhcp6c_update_entry_safe(STATE_RA_ROUTE, &entry, 7200);
 
                                if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
                                                pinfo->nd_opt_pi_prefix_len != 64)
@@ -241,7 +242,7 @@ bool ra_process(void)
                                entry.target.s6_addr32[2] = lladdr.s6_addr32[2];
                                entry.target.s6_addr32[3] = lladdr.s6_addr32[3];
 
-                               odhcp6c_update_entry_safe(STATE_RA_PREFIX, &entry, 7200);
+                               changed |= odhcp6c_update_entry_safe(STATE_RA_PREFIX, &entry, 7200);
                        } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 2) {
                                entry.router = from.sin6_addr;
                                entry.priority = 0;
@@ -253,7 +254,7 @@ bool ra_process(void)
                                for (ssize_t i = 0; i < (opt->len - 1) / 2; ++i) {
                                        memcpy(&entry.target, &opt->data[6 + i * sizeof(entry.target)],
                                                        sizeof(entry.target));
-                                       odhcp6c_update_entry(STATE_RA_DNS, &entry);
+                                       changed |= odhcp6c_update_entry(STATE_RA_DNS, &entry);
                                }
                        }
                }
@@ -269,5 +270,5 @@ bool ra_process(void)
        if (found)
                odhcp6c_expire();
 
-       return found;
+       return found && changed;
 }