[PATCH] odhcpd: add option for setting preferred lifetime

vincent at systemli.org vincent at systemli.org
Fri Jan 1 15:14:04 EST 2021


From: Nick Hainke <vincent at systemli.org>

"valid_lft" and "preferred_lft" are different. If the "preferred_lft"
is expired the prefix should be avoided in source prefix selection.
However, the interface is allowed to still receive downstream traffic.

ra_use_preferred_lft:
  Value for the preferred lifetime for a prefix

ra_preferred_lft:
  Use the limit for preferred lifetime of a prefix

If only "useleasetime" is set odhcpd will still set the "preferred_lft"
to the leasetime. If "ra_use_preferred_lft" is also set the
"preferred_lft" will be set to "ra_preferred_lft".

Signed-off-by: Nick Hainke <vincent at systemli.org>
---
 README          |  4 ++++
 src/config.c    | 15 +++++++++++++++
 src/dhcpv6-ia.c | 20 ++++++++++++++++----
 src/odhcpd.h    |  3 +++
 src/router.c    |  4 ++++
 5 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/README b/README
index a34a93c..6a5c6cf 100644
--- a/README
+++ b/README
@@ -129,6 +129,10 @@ ra_lifetime		integer	1800			Value to be placed in Router
 ra_useleasetime		bool	0			Use configured leasetime as
 							limit for the preferred and
 							valid lifetime of a prefix
+ra_preferred_lft	string	12h		Value for the preferred lifetime
+							for a prefix
+ra_use_preferred_lft	bool	0		Use the limit for preferred
+							lifetime of a prefix
 ra_reachabletime	integer	0			Reachable Time in milliseconds to be
 							advertised in RA messages
 ra_retranstime		integer	0			Retransmit Time in milliseconds to be
diff --git a/src/config.c b/src/config.c
index 015a716..cb06dfe 100644
--- a/src/config.c
+++ b/src/config.c
@@ -82,6 +82,8 @@ enum {
 	IFACE_ATTR_NDPROXY_ROUTING,
 	IFACE_ATTR_NDPROXY_SLAVE,
 	IFACE_ATTR_PREFIX_FILTER,
+	IFACE_ATTR_RA_USE_PREFERRED_LFT,
+	IFACE_ATTR_RA_PREFERRED_LFT,
 	IFACE_ATTR_MAX
 };
 
@@ -130,6 +132,8 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
 	[IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
 	[IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
 	[IFACE_ATTR_PREFIX_FILTER] = { .name = "prefix_filter", .type = BLOBMSG_TYPE_STRING },
+	[IFACE_ATTR_RA_USE_PREFERRED_LFT] = { .name = "ra_use_preferred_lft", .type = BLOBMSG_TYPE_BOOL },
+	[IFACE_ATTR_RA_PREFERRED_LFT] = { .name = "ra_preferred_lft", .type = BLOBMSG_TYPE_STRING },
 };
 
 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
@@ -525,6 +529,14 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
 		iface->dhcp_leasetime = time;
 	}
 
+	if ((c = tb[IFACE_ATTR_RA_PREFERRED_LFT])) {
+		double time = parse_leasetime(c);
+		if (time < 0)
+			goto err;
+
+		iface->ra_preferred_lft = time;
+	}
+
 	if ((c = tb[IFACE_ATTR_START])) {
 		iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
 		iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
@@ -795,6 +807,9 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
 	if ((c = tb[IFACE_ATTR_RA_USELEASETIME]))
 		iface->ra_useleasetime = blobmsg_get_bool(c);
 
+	if ((c = tb[IFACE_ATTR_RA_USE_PREFERRED_LFT]))
+		iface->ra_use_preferred_lft = blobmsg_get_bool(c);
+
 	if ((c = tb[IFACE_ATTR_RA_DNS]))
 		iface->ra_dns = blobmsg_get_bool(c);
 
diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c
index d7848de..8fffeb7 100644
--- a/src/dhcpv6-ia.c
+++ b/src/dhcpv6-ia.c
@@ -247,6 +247,9 @@ void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c,
 			addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
 		}
 
+		if (pref > (uint32_t)c->preferred_until)
+			pref = c->preferred_until;
+
 		if (pref > (uint32_t)c->valid_until)
 			pref = c->valid_until;
 
@@ -827,14 +830,18 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 	}
 
 	if (a) {
-		uint32_t leasetime;
+		uint32_t leasetime, pref;
 
 		if (a->leasetime)
 			leasetime = a->leasetime;
 		else
 			leasetime = iface->dhcp_leasetime;
+		
+		if (iface->ra_use_preferred_lft)
+			pref = iface->ra_preferred_lft;
+		else
+			pref = leasetime;
 
-		uint32_t pref = leasetime;
 		uint32_t valid = leasetime;
 
 		struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->addr6;
@@ -851,8 +858,8 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 			if (prefix_pref != UINT32_MAX)
 				prefix_pref -= now;
 
-			if (prefix_pref > leasetime)
-				prefix_pref = leasetime;
+			if (prefix_pref > pref)
+				prefix_pref = pref;
 
 			if (prefix_valid != UINT32_MAX)
 				prefix_valid -= now;
@@ -918,6 +925,10 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
 			/* UINT32_MAX is considered as infinite leasetime */
 			a->valid_until = (valid == UINT32_MAX) ? 0 : valid + now;
 
+		if (!INFINITE_VALID(a->preferred_until))
+			/* UINT32_MAX is considered as infinite leasetime */
+			a->preferred_until = (pref == UINT32_MAX) ? 0 : pref + now;
+
 		o_ia.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
 		o_ia.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
 
@@ -1261,6 +1272,7 @@ ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *ifac
 						a->peer = *addr;
 						a->assigned = is_na && l ? l->hostid : reqhint;
 						a->valid_until =  now;
+						a->preferred_until =  now;
 						a->dhcp_free_cb = dhcpv6_ia_free_assignment;
 						a->iface = iface;
 						a->flags = (is_pd ? OAF_DHCPV6_PD : OAF_DHCPV6_NA);
diff --git a/src/odhcpd.h b/src/odhcpd.h
index 2f7dd25..9df53a8 100644
--- a/src/odhcpd.h
+++ b/src/odhcpd.h
@@ -182,6 +182,7 @@ struct dhcp_assignment {
 
 	struct sockaddr_in6 peer;
 	time_t valid_until;
+	time_t preferred_until;
 
 #define fr_timer	reconf_timer
 	struct uloop_timeout reconf_timer;
@@ -286,6 +287,8 @@ struct interface {
 	uint32_t ra_retranstime;
 	uint32_t ra_hoplimit;
 	int ra_mtu;
+	bool ra_use_preferred_lft;
+	uint32_t ra_preferred_lft;
 
 	// DHCP
 	uint32_t dhcp_leasetime;
diff --git a/src/router.c b/src/router.c
index 06f3a66..244e600 100644
--- a/src/router.c
+++ b/src/router.c
@@ -554,6 +554,10 @@ static int send_router_advert(struct interface *iface, const struct in6_addr *fr
 			if (iface->ra_useleasetime &&
 			    preferred > iface->dhcp_leasetime)
 				preferred = iface->dhcp_leasetime;
+
+			if (iface->ra_use_preferred_lft &&
+			    preferred > iface->ra_preferred_lft)
+				preferred = iface->ra_preferred_lft;
 		}
 
 		valid = TIME_LEFT(addr->valid, now);
-- 
2.29.2




More information about the openwrt-devel mailing list