router: allow RA prefix lifetime being set to leasetime value (FS#397)
authorHans Dedecker <dedeckeh@gmail.com>
Mon, 20 Feb 2017 14:31:48 +0000 (15:31 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Mon, 20 Feb 2017 15:25:18 +0000 (16:25 +0100)
Add config option ra_useleasetime which allows the RA prefix lifetime
being set to the leasetime in case the valid and/or preferred prefix
lifetime is bigger than the configured leasetime.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
README
src/config.c
src/odhcpd.h
src/router.c

diff --git a/README b/README
index bc52ee9..ebb2d99 100644 (file)
--- a/README
+++ b/README
@@ -111,6 +111,9 @@ ra_mininterval      integer 200                     Minimum time allowed between
                                                sending unsolicited RA
 ra_lifetime    integer 1800                    Value to be placed in Router
                                                Lifetime field of RA
                                                sending unsolicited RA
 ra_lifetime    integer 1800                    Value to be placed in Router
                                                Lifetime field of RA
+ra_useleasetime        bool    0                       Use configured leasetime as
+                                               limit for the preferred and
+                                               valid lifetime of a prefix
 ndproxy_routing        bool    1                       Learn routes from NDP
 ndproxy_slave  bool    0                       NDProxy external slave
 
 ndproxy_routing        bool    1                       Learn routes from NDP
 ndproxy_slave  bool    0                       NDProxy external slave
 
index df0cbc4..689d4ce 100644 (file)
@@ -48,6 +48,7 @@ enum {
        IFACE_ATTR_RA_MININTERVAL,
        IFACE_ATTR_RA_MAXINTERVAL,
        IFACE_ATTR_RA_LIFETIME,
        IFACE_ATTR_RA_MININTERVAL,
        IFACE_ATTR_RA_MAXINTERVAL,
        IFACE_ATTR_RA_LIFETIME,
+       IFACE_ATTR_RA_USELEASETIME,
        IFACE_ATTR_PD_MANAGER,
        IFACE_ATTR_PD_CER,
        IFACE_ATTR_NDPROXY_ROUTING,
        IFACE_ATTR_PD_MANAGER,
        IFACE_ATTR_PD_CER,
        IFACE_ATTR_NDPROXY_ROUTING,
@@ -85,6 +86,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_RA_MININTERVAL] = { .name = "ra_mininterval", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_RA_MAXINTERVAL] = { .name = "ra_maxinterval", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_RA_LIFETIME] = { .name = "ra_lifetime", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_RA_MININTERVAL] = { .name = "ra_mininterval", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_RA_MAXINTERVAL] = { .name = "ra_maxinterval", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_RA_LIFETIME] = { .name = "ra_lifetime", .type = BLOBMSG_TYPE_INT32 },
+       [IFACE_ATTR_RA_USELEASETIME] = { .name = "ra_useleasetime", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
 };
        [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
 };
@@ -599,6 +601,9 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        if ((c = tb[IFACE_ATTR_RA_LIFETIME]))
                iface->ra_lifetime = blobmsg_get_u32(c);
 
        if ((c = tb[IFACE_ATTR_RA_LIFETIME]))
                iface->ra_lifetime = blobmsg_get_u32(c);
 
+       if ((c = tb[IFACE_ATTR_RA_USELEASETIME]))
+               iface->ra_useleasetime = blobmsg_get_bool(c);
+
        if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
                const char *prio = blobmsg_get_string(c);
 
        if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
                const char *prio = blobmsg_get_string(c);
 
index a88eb08..538a7e5 100644 (file)
@@ -150,6 +150,7 @@ struct interface {
        bool always_rewrite_dns;
        bool ra_not_onlink;
        bool ra_advrouter;
        bool always_rewrite_dns;
        bool ra_not_onlink;
        bool ra_advrouter;
+       bool ra_useleasetime;
        bool no_dynamic_dhcp;
 
        // RA
        bool no_dynamic_dhcp;
 
        // RA
index 4ab06b6..dd3f09c 100644 (file)
@@ -321,6 +321,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
 
        for (ssize_t i = 0; i < ipcnt; ++i) {
                struct odhcpd_ipaddr *addr = &addrs[i];
 
        for (ssize_t i = 0; i < ipcnt; ++i) {
                struct odhcpd_ipaddr *addr = &addrs[i];
+               uint32_t preferred = 0;
+               uint32_t valid = 0;
 
                if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
                        char namebuf[INET6_ADDRSTRLEN];
 
                if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
                        char namebuf[INET6_ADDRSTRLEN];
@@ -346,9 +348,23 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                        p = &adv.prefix[cnt++];
                }
 
                        p = &adv.prefix[cnt++];
                }
 
-               if (addr->preferred > (uint32_t)now &&
-                               minvalid > TIME_LEFT(addr->valid, now))
-                       minvalid = TIME_LEFT(addr->valid, now);
+               if (addr->preferred > (uint32_t)now) {
+                       preferred = TIME_LEFT(addr->preferred, now);
+
+                       if (iface->ra_useleasetime &&
+                                       preferred > iface->dhcpv4_leasetime)
+                               preferred = iface->dhcpv4_leasetime;
+               }
+
+               valid = TIME_LEFT(addr->valid, now);
+               if (iface->ra_useleasetime) {
+                       if (valid > iface->dhcpv4_leasetime)
+                               valid = iface->dhcpv4_leasetime;
+               } else if (!preferred && valid < 7200)
+                       valid = 0;
+
+               if (minvalid > valid)
+                       minvalid = valid;
 
                if (!IN6_IS_ADDR_ULA(&addr->addr) || iface->default_router)
                        valid_prefix = true;
 
                if (!IN6_IS_ADDR_ULA(&addr->addr) || iface->default_router)
                        valid_prefix = true;
@@ -365,11 +381,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
                if (iface->ra_advrouter)
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
                if (iface->ra_advrouter)
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
-               p->nd_opt_pi_valid_time = htonl(TIME_LEFT(addr->valid, now));
-               if (addr->preferred > (uint32_t)now)
-                       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;
+               p->nd_opt_pi_preferred_time = htonl(preferred);
+               p->nd_opt_pi_valid_time = htonl(valid);
        }
 
        // Calculate periodic transmit
        }
 
        // Calculate periodic transmit