Skip to contentSkip to conversation

netconfig: RA route, SLAAC lifetime and DNS fixes

PATCH New Ryan Walklin 1 participant 7 messages Nov 16

lore
Share & tools
lore
#0 lore raw

cover netconfig: RA route, SLAAC lifetime and DNS fixes

Hi,

I've been working on SLAAC + IPv6 (RA M+A mode) support for iwd and have identified a handful of bugs in l_netconfig's IPv6 handling.

Each was reproduced on 0.83 with an ASan build of ell and a small program running l_netconfig on a veth pair between two network namespaces, with a script sending RAs and dnsmasq for DHCPv6, and checked again with the patch applied.

1: routes and addresses were never put on the expired lists, as those
were never allocated.
2: 32 bytes leaked for every RA route withdrawn or expired. 3: an RA route that's not refreshed stays on the current list after the
kernel has removed it, and is reported as removed on unconfigure.
4: an RA cutting a prefix's valid lifetime to 60 seconds raised the
lifetime of an address with an hour left to 2 hours.
5: a DNS server in both the DHCPv6 lease and the RA's RDNSS option is
listed twice, so it's in resolv.conf twice.
6: 104 bytes leaked when the first RA can't be used and IPv6 fails.

AI was used to draft patches and commit messages, with all manually reviewed and tested on my dual-stack home network, and roaming on a ThinkPad with iwd (debian sid) and the mt7921e network driver.

Ryan Walklin (6):
netconfig: Allocate the expired lists
netconfig: Fix leak of RA route data
netconfig: Expire RA routes never refreshed
netconfig: Fix the RFC 4862 2-hour rule
netconfig: Skip duplicate DNS servers and domains
icmp6: Fix RA leak when stopped from a handler

 ell/icmp6.c     |  4 ++-
 ell/netconfig.c | 93 +++++++++++++++++++++++++++++++++++--------------
 2 files changed, 70 insertions(+), 27 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/6 netconfig: Allocate the expired lists

addresses.expired and routes.expired have never been allocated, so pushing to them silently does nothing. Expired addresses and routes are leaked and users never see them on the lists returned by l_netconfig_get_addresses() and l_netconfig_get_routes(). An IPv6 route that expires also never causes netconfig_signal_expired() to emit an update.

Fixes 18c4434324c2 ("netconfig: Expire addresses and routes")
---
 ell/netconfig.c | 4 ++++
 1 file changed, 4 insertions(+)
ell/netconfig.c +4 -0
index 9cebdc3..2444d97 100644
@@ -1353,10 +1353,12 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
 	nc->addresses.added = l_queue_new();
 	nc->addresses.updated = l_queue_new();
 	nc->addresses.removed = l_queue_new();
+	nc->addresses.expired = l_queue_new();
 	nc->routes.current = l_queue_new();
 	nc->routes.added = l_queue_new();
 	nc->routes.updated = l_queue_new();
 	nc->routes.removed = l_queue_new();
+	nc->routes.expired = l_queue_new();
 	nc->icmp_route_data = l_queue_new();
 
 	nc->dhcp_client = l_dhcp_client_new(ifindex);
@@ -1405,10 +1407,12 @@ LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)
 	l_queue_destroy(netconfig->addresses.added, NULL);
 	l_queue_destroy(netconfig->addresses.updated, NULL);
 	l_queue_destroy(netconfig->addresses.removed, NULL);
+	l_queue_destroy(netconfig->addresses.expired, NULL);
 	l_queue_destroy(netconfig->routes.current, NULL);
 	l_queue_destroy(netconfig->routes.added, NULL);
 	l_queue_destroy(netconfig->routes.updated, NULL);
 	l_queue_destroy(netconfig->routes.removed, NULL);
+	l_queue_destroy(netconfig->routes.expired, NULL);
 	l_queue_destroy(netconfig->icmp_route_data, NULL);
 	l_queue_destroy(netconfig->slaac_domains, NULL);
 	l_queue_destroy(netconfig->slaac_dnses, NULL);
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/6 netconfig: Fix leak of RA route data

In reply to Ryan Walklin

netconfig_remove_icmp6_route() and netconfig_check_route_expired() drop the struct netconfig_route_data from icmp_route_data without freeing it, leaking 32 bytes for every RA route that's withdrawn or expires. Free it in both places.

Also free the route itself if it was still on the added list, i.e. it was never passed to the user, since it's then on no other list that would free it.

Fixes 6eb8b8bccdcb ("netconfig: Ratelimit route lifetime updates")
---
 ell/netconfig.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
ell/netconfig.c +13 -4
index 2444d97..1be26ad 100644
@@ -953,12 +953,16 @@ static bool netconfig_check_route_expired(void *data, void *user_data)
 	 * If for some reason the route is still on the added list, drop it
 	 * from there and there's nothing to notify the user of.
 	 */
-	if (!l_queue_remove(nc->routes.added, rd->route))
-		l_queue_push_tail(nc->routes.expired, rd->route);
-
 	l_queue_remove(nc->routes.current, rd->route);
 	l_queue_remove(nc->routes.updated, rd->route);
 	l_queue_remove(nc->routes.removed, rd->route);
+
+	if (l_queue_remove(nc->routes.added, rd->route))
+		l_rtnl_route_free(rd->route);
+	else
+		l_queue_push_tail(nc->routes.expired, rd->route);
+
+	l_free(rd);
 	return true;
 }
 
@@ -1137,8 +1141,13 @@ static void netconfig_remove_icmp6_route(struct l_netconfig *nc,
 	l_queue_remove(nc->routes.current, rd->route);
 	l_queue_remove(nc->routes.updated, rd->route);
 
-	if (!l_queue_remove(nc->routes.added, rd->route))
+	/* Not committed yet so there's nothing for the user to remove */
+	if (l_queue_remove(nc->routes.added, rd->route))
+		l_rtnl_route_free(rd->route);
+	else
 		l_queue_push_tail(nc->routes.removed, rd->route);
+
+	l_free(rd);
 }
 
 static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
Signature · 1 line
2.55.0
#3 lore raw

PATCH 3/6 netconfig: Expire RA routes never refreshed

In reply to Ryan Walklin

rd->kernel_expiry is only set when an existing route is updated, so a route that's added and never appears in another RA, e.g. because the router went away, keeps kernel_expiry at 0 and is never expired by netconfig_check_route_expired(). The kernel deletes it when its lifetime ends but it stays on the current list, counts towards max_icmp6_routes and is reported as removed on unconfigure.

Set kernel_expiry for routes on the added list too, since those are committed with the lifetime from the latest RA.

Fixes 6eb8b8bccdcb ("netconfig: Ratelimit route lifetime updates")
---
 ell/netconfig.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
ell/netconfig.c +8 -2
index 1be26ad..7e20de6 100644
@@ -1127,8 +1127,14 @@ static void netconfig_set_icmp6_route_data(struct l_netconfig *nc,
 							expiry, old_expiry);
 	}
 
-	if (updated && differs && !netconfig_route_exists(nc->routes.added,
-								rd->route)) {
+	/*
+	 * A route on the added list will be committed with the current
+	 * lifetime, whether it's new or was updated again before the user
+	 * got to it, so it expires in the kernel at the current expiry time.
+	 */
+	if (netconfig_route_exists(nc->routes.added, rd->route))
+		rd->kernel_expiry = expiry;
+	else if (updated && differs) {
 		l_queue_push_tail(nc->routes.updated, rd->route);
 		rd->kernel_expiry = expiry;
 	}
Signature · 1 line
2.55.0
#4 lore raw

PATCH 4/6 netconfig: Fix the RFC 4862 2-hour rule

In reply to Ryan Walklin

RFC 4862 Section 5.5.3 e) says that when an RA's Valid Lifetime is no longer than 2 hours or the address's remaining lifetime, the remaining lifetime is kept if it's 2 hours or less and cut to 2 hours otherwise. netconfig sets it to 2 hours in both cases, so a prefix being withdrawn with short lifetimes gets its address's lifetime raised to 2 hours whenever less than that remained.

Fixes 6adf0f5db455 ("netconfig: Create SLAAC address")
---
 ell/netconfig.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
ell/netconfig.c +8 -3
index 7e20de6..2eb9cae 100644
@@ -823,9 +823,14 @@ static void netconfig_set_slaac_address_lifetimes(struct l_netconfig *nc,
 		if (memcmp(prefix->prefix, addr, 8))
 			continue;
 
-		/* RFC4862 Section 5.5.3 e) */
-		if (v < 120 * 60 && v < remaining)
-			v = 120 * 60; /* 2 hours */
+		/*
+		 * RFC4862 Section 5.5.3 e): use the received Valid Lifetime
+		 * if it's over 2 hours or over the remaining lifetime.
+		 * Otherwise keep the remaining lifetime if that's 2 hours or
+		 * less, else cut it to 2 hours.
+		 */
+		if (v <= 120 * 60 && v <= remaining)
+			v = remaining < 120 * 60 ? remaining : 120 * 60;
 
 		l_rtnl_address_set_lifetimes(nc->v6_address,
 						p != 0xffffffff ? p : 0,
Signature · 1 line
2.55.0
#5 lore raw

PATCH 5/6 netconfig: Skip duplicate DNS servers and domains

In reply to Ryan Walklin

A network that hands out the same DNS server in DHCPv6 and in the RA's RDNSS option, or the same search domain over more than one of DHCPv4, DHCPv6 and the RA's DNSSL option, gets it listed twice by l_netconfig_get_dns_list() or l_netconfig_get_domain_names(), and then twice in resolv.conf. Skip strings that are already in the list.

Fixes abeb8590791b ("netconfig: Handle DNS data from l_icmp6_router")
---
 ell/netconfig.c | 51 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 17 deletions(-)
ell/netconfig.c +34 -17
index 2eb9cae..fb18ec3 100644
@@ -2376,27 +2376,38 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes(
 	return l_queue_get_entries(netconfig->routes.current);
 }
 
+/*
+ * Append the strings from @src that aren't in @dest yet, e.g. a DNS server
+ * that's both in the DHCPv6 lease and the RA.  Takes ownership of @src if
+ * @free is true.
+ */
 static void netconfig_strv_cat(char ***dest, char **src, bool free)
 {
 	unsigned int dest_len;
-	unsigned int src_len;
+	char **i;
 
 	if (!src)
 		return;
 
-	if (!free)
-		src = l_strv_copy(src);
+	dest_len = l_strv_length(*dest);
+	*dest = l_realloc(*dest, sizeof(char *) *
+				(dest_len + l_strv_length(src) + 1));
+	(*dest)[dest_len] = NULL;
 
-	if (!*dest) {
-		*dest = src;
-		return;
+	for (i = src; *i; i++) {
+		if (l_strv_contains(*dest, *i)) {
+			if (free)
+				l_free(*i);
+
+			continue;
+		}
+
+		(*dest)[dest_len++] = free ? *i : l_strdup(*i);
+		(*dest)[dest_len] = NULL;
 	}
 
-	dest_len = l_strv_length(*dest);
-	src_len = l_strv_length(src);
-	*dest = l_realloc(*dest, sizeof(char *) * (dest_len + src_len + 1));
-	memcpy(*dest + dest_len, src, sizeof(char *) * (src_len + 1));
-	l_free(src);
+	if (free)
+		l_free(src);
 }
 
 /* Returns a new strv array to be freed by the caller */
@@ -2438,17 +2449,19 @@ append_v6:
 
 		ret = l_realloc(ret, sizeof(char *) * (dest_len + src_len + 1));
 		i = ret + dest_len;
+		*i = NULL;
 
 		for (entry = l_queue_get_entries(netconfig->slaac_dnses);
 				entry; entry = entry->next) {
 			char addr_str[INET6_ADDRSTRLEN];
 
 			if (inet_ntop(AF_INET6, entry->data, addr_str,
-					sizeof(addr_str)))
+					sizeof(addr_str)) &&
+					!l_strv_contains(ret, addr_str)) {
 				*i++ = l_strdup(addr_str);
+				*i = NULL;
+			}
 		}
-
-		*i = NULL;
 	}
 
 done:
@@ -2501,12 +2514,16 @@ append_v6:
 
 		ret = l_realloc(ret, sizeof(char *) * (dest_len + src_len + 1));
 		i = ret + dest_len;
+		*i = NULL;
 
 		for (entry = l_queue_get_entries(netconfig->slaac_domains);
-				entry; entry = entry->next)
-			*i++ = l_strdup(entry->data);
+				entry; entry = entry->next) {
+			if (l_strv_contains(ret, entry->data))
+				continue;
 
-		*i = NULL;
+			*i++ = l_strdup(entry->data);
+			*i = NULL;
+		}
 	}
 
 done:
Signature · 1 line
2.55.0
#6 lore raw

PATCH 6/6 icmp6: Fix RA leak when stopped from a handler

In reply to Ryan Walklin

If the ROUTER_FOUND event handler stops the client, which l_netconfig does when the first RA can't be used for configuration, the parsed l_icmp6_router is neither kept nor freed, leaking it.

Fixes 90784c747ce4 ("icmp6: Emit L_ICMP6_CLIENT_EVENT_ROUTER_FOUND for all RAs")
---
 ell/icmp6.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ell/icmp6.c +3 -1
index d352955..2a8798e 100644
@@ -457,8 +457,10 @@ static int icmp6_client_handle_message(struct l_icmp6_client *client,
 					r);
 
 	/* DHCP6 client may have stopped us */
-	if (!client->io)
+	if (!client->io) {
+		_icmp6_router_free(r);
 		return -ECANCELED;
+	}
 
 	if (!client->ra) {
 		client->ra = r;
Signature · 1 line
2.55.0