Skip to contentSkip to conversation

net: Rename l_net_subnet_matches to l_net_prefix_matches

PATCH New Andrew Zaborowski 1 participant 11 messages Apr 20, 2022

lore
Share & tools
lore

Andrew Zaborowski

author
#0 lore raw

PATCH 1/11 net: Rename l_net_subnet_matches to l_net_prefix_matches

---
 ell/net.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ell/net.h +1 -1
index eaaf14c..0521d75 100644
@@ -41,7 +41,7 @@ bool l_net_hostname_is_localhost(const char *hostname);
 bool l_net_get_address(int ifindex, struct in_addr *out);
 bool l_net_get_link_local_address(int ifindex, struct in6_addr *out);
 
-static inline bool l_net_subnet_matches(const void *a, const void *b,
+static inline bool l_net_prefix_matches(const void *a, const void *b,
 					uint8_t prefix_len)
 {
 	uint8_t bytes = prefix_len / 8;
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH 2/11 icmp6: Parse extra options

Parse Route Information and New Advertisement Interval options. Route Information options (RIOs) are supported by radvd and a few clients that I checked.

---
 ell/icmp6-private.h |   6 ++-
 ell/icmp6.c         | 118 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 110 insertions(+), 14 deletions(-)
ell/icmp6-private.h +4 -2
index 2433087..77df0b8 100644
@@ -22,6 +22,7 @@
 
 struct route_info {
 	uint8_t address[16];
+	bool onlink : 1;
 	uint8_t prefix_len;
 	uint8_t preference;
 	uint32_t preferred_lifetime;
@@ -36,8 +37,9 @@ struct l_icmp6_router {
 	uint64_t start_time;
 	uint16_t lifetime;
 	uint32_t mtu;
-	uint32_t n_prefixes;
-	struct route_info *prefixes;
+	uint32_t max_rtr_adv_interval_ms;
+	uint32_t n_routes;
+	struct route_info *routes;
 };
 
 struct l_icmp6_router *_icmp6_router_new();
ell/icmp6.c +106 -12

181 lines not on this page · raw message

Signature · 1 line
2.32.0
author
#2 lore raw

PATCH 3/11 netconfig: Add DHCP override and static IP API

In reply to Andrew Zaborowski

Add API to override things like DNS, domain names, etc. received from DHCP or disable DHCP completely by setting a static address.

---
 ell/ell.sym     |   8 +
 ell/netconfig.c | 428 +++++++++++++++++++++++++++++++++++++++++++++++-
 ell/netconfig.h |  16 ++
 3 files changed, 447 insertions(+), 5 deletions(-)
ell/ell.sym +8 -0
index 96869d5..60dd257 100644
@@ -738,6 +738,14 @@ global:
 	/* netconfig */
 	l_netconfig_new;
 	l_netconfig_destroy;
+	l_netconfig_set_family_enabled;
+	l_netconfig_set_hostname;
+	l_netconfig_set_route_priority;
+	l_netconfig_set_static_addr;
+	l_netconfig_set_gateway_override;
+	l_netconfig_set_dns_override;
+	l_netconfig_set_domain_names_override;
+	l_netconfig_check_config;
 	l_netconfig_start;
 	l_netconfig_stop;
 	l_netconfig_get_dhcp_client;
ell/netconfig.c +423 -5

474 lines not on this page · raw message

ell/netconfig.h +16 -0
index 682f4aa..f90ec81 100644
@@ -53,6 +53,22 @@ typedef void (*l_netconfig_destroy_cb_t)(void *user_data);
 
 struct l_netconfig *l_netconfig_new(uint32_t ifindex);
 void l_netconfig_destroy(struct l_netconfig *netconfig);
+bool l_netconfig_set_family_enabled(struct l_netconfig *netconfig,
+					uint8_t family, bool enabled);
+bool l_netconfig_set_hostname(struct l_netconfig *netconfig,
+				const char *hostname);
+bool l_netconfig_set_route_priority(struct l_netconfig *netconfig,
+					uint32_t priority);
+bool l_netconfig_set_static_addr(struct l_netconfig *netconfig, uint8_t family,
+					const struct l_rtnl_address *addr);
+bool l_netconfig_set_gateway_override(struct l_netconfig *netconfig,
+					uint8_t family,
+					const char *gateway_str);
+bool l_netconfig_set_dns_override(struct l_netconfig *netconfig, uint8_t family,
+					char **dns_list);
+bool l_netconfig_set_domain_names_override(struct l_netconfig *netconfig,
+						uint8_t family, char **names);
+bool l_netconfig_check_config(struct l_netconfig *netconfig);
 
 bool l_netconfig_start(struct l_netconfig *netconfig);
 void l_netconfig_stop(struct l_netconfig *netconfig);
Signature · 1 line
2.32.0
author
#3 lore raw

PATCH 4/11 netconfig: Support IPv6 static configurations

In reply to Andrew Zaborowski
---
 ell/netconfig.c | 179 +++++++++++++++++++++++++-----------------------
 1 file changed, 95 insertions(+), 84 deletions(-)
ell/netconfig.c +95 -84
index c0968aa..02a8b1d 100644
@@ -62,11 +62,13 @@ struct l_netconfig {
 	struct l_idle *do_static_work;
 	bool v4_configured;
 	struct l_dhcp_client *dhcp_client;
+	bool v6_configured;
 
 	/* These objects, if not NULL, are owned by @addresses and @routes */
 	struct l_rtnl_address *v4_address;
 	struct l_rtnl_route *v4_subnet_route;
 	struct l_rtnl_route *v4_default_route;
+	struct l_rtnl_address *v6_address;
 
 	struct {
 		struct l_queue *current;
@@ -159,6 +161,50 @@ static void netconfig_add_v4_routes(struct l_netconfig *nc, const char *ip,
 	l_queue_push_tail(nc->routes.added, nc->v4_default_route);
 }
 
+static void netconfig_add_v6_static_routes(struct l_netconfig *nc,
+						const char *ip,
+						uint8_t prefix_len)
+{
+	struct in6_addr in6_addr;
+	char network[INET6_ADDRSTRLEN];
+	struct l_rtnl_route *v6_subnet_route;
+	struct l_rtnl_route *v6_default_route;
+
+	/* Subnet route */
+
+	if (L_WARN_ON(inet_pton(AF_INET6, ip, &in6_addr) != 1))
+		return;
+
+	/* Zero out host address bits to produce network address */
+	if (prefix_len & 7)
+		in6_addr.s6_addr[prefix_len / 8] &= 0xff00 >> (prefix_len & 7);
+
+	if (prefix_len <= 120)
+		memset(in6_addr.s6_addr + (prefix_len + 7) / 8, 0,
+			16 - (prefix_len + 7) / 8);
+
+	if (L_WARN_ON(!inet_ntop(AF_INET6, &in6_addr, network,
+					INET6_ADDRSTRLEN)))
+		return;
+
+	v6_subnet_route = l_rtnl_route_new_prefix(network, prefix_len);
+	l_rtnl_route_set_protocol(v6_subnet_route, RTPROT_STATIC);
+	l_rtnl_route_set_priority(v6_subnet_route, nc->route_priority);
+	l_queue_push_tail(nc->routes.current, v6_subnet_route);
+	l_queue_push_tail(nc->routes.added, v6_subnet_route);
+
+	/* Gateway route */
+
+	if (!nc->v6_gateway_override)
+		return;
+
+	v6_default_route = l_rtnl_route_new_gateway(nc->v6_gateway_override);
+	l_rtnl_route_set_protocol(v6_default_route, RTPROT_STATIC);
+	L_WARN_ON(!l_rtnl_route_set_prefsrc(v6_default_route, ip));
+	l_queue_push_tail(nc->routes.current, v6_default_route);
+	l_queue_push_tail(nc->routes.added, v6_default_route);
+}
+
 static void netconfig_add_dhcp_address_routes(struct l_netconfig *nc)
 {
 	const struct l_dhcp_lease *lease =
@@ -607,90 +653,11 @@ static bool netconfig_check_v4_config(struct l_netconfig *netconfig)
 	return true;
 }
 
-static bool netconfig_check_v6_dns_list(const struct in6_addr *local,
-					uint8_t prefix_len,
-					const struct in6_addr *dns_list,
-					unsigned int dns_num)
-{
-	unsigned int i;
-
-	for (i = 0; i < dns_num; i++)
-		if (!l_net_prefix_matches(local, &dns_list[i], prefix_len) &&
-				!IN6_IS_ADDR_LINKLOCAL(&dns_list[i]))
-			return false;
-
-	return true;
-}
-
-static bool netconfig_check_v6_config(struct l_netconfig *netconfig)
-{
-	struct in6_addr local;
-	struct in6_addr gateway;
-	uint8_t prefix_len = 0;
-	unsigned int dns_num = 0;
-	_auto_(l_free) struct in6_addr *dns_list = NULL;
-
-	if (!netconfig->v6_enabled)
-		return true;
-
-	if (netconfig->v6_static_addr) {
-		char str[INET6_ADDRSTRLEN];
-
-		prefix_len = l_rtnl_address_get_prefix_length(
-						netconfig->v6_static_addr);
-		if (unlikely(prefix_len > 126))
-			return false;
-
-		l_rtnl_address_get_address(netconfig->v6_static_addr, str);
-		inet_pton(AF_INET6, str, &local);
-	}
-
-	if (netconfig->v6_gateway_override) {
-		if (unlikely(inet_pton(AF_INET6, netconfig->v6_gateway_override,
-					&gateway) != 1))
-			return false;
-	}
-
-	if (netconfig->v6_dns_override &&
-			(dns_num = l_strv_length(netconfig->v6_dns_override))) {
-		unsigned int i;
-
-		dns_list = l_new(struct in6_addr, dns_num);
-
-		for (i = 0; i < dns_num; i++)
-			if (inet_pton(AF_INET6, netconfig->v6_dns_override[i],
-					&dns_list[i]) != 1)
-				return false;
-	}
-
-	/*
-	 * If using a static IP, we can validate right now that the gateway IP
-	 * is link-local or is in the local subnet.
-	 */
-	if (netconfig->v6_static_addr && netconfig->v6_gateway_override)
-		if (!l_net_prefix_matches(&local, &gateway, prefix_len) &&
-				!IN6_IS_ADDR_LINKLOCAL(&gateway))
-			return false;
-
-	/*
-	 * If using a static IP and there's no gateway all the DNSes must also
-	 * have link-local addresses or be in our subnet.
-	 */
-	if (netconfig->v6_static_addr && !netconfig->v6_gateway_override &&
-			netconfig->v6_dns_override &&
-			!netconfig_check_v6_dns_list(&local, prefix_len,
-							dns_list, dns_num))
-		return false;
-
-	return true;
-}
-
 static bool netconfig_check_config(struct l_netconfig *netconfig)
 {
 	/* TODO: error reporting through a debug log handler or otherwise */
 
-	return netconfig_check_v4_config(netconfig) &&
-		netconfig_check_v6_config(netconfig);
+	return netconfig_check_v4_config(netconfig);
 }
 
 LIB_EXPORT bool l_netconfig_check_config(struct l_netconfig *netconfig)
@@ -701,7 +668,7 @@ LIB_EXPORT bool l_netconfig_check_config(struct l_netconfig *netconfig)
 	return netconfig_check_config(netconfig);
 }
 
-static void netconfig_add_static_address_routes(struct l_netconfig *nc)
+static void netconfig_add_v4_static_address_routes(struct l_netconfig *nc)
 {
 	char ip[INET_ADDRSTRLEN];
 	uint32_t prefix_len;
@@ -715,6 +682,29 @@ static void netconfig_add_static_address_routes(struct l_netconfig *nc)
 	netconfig_add_v4_routes(nc, ip, prefix_len, NULL, RTPROT_STATIC);
 }
 
+/*
+ * Just mirror the IPv4 behaviour with static IPv6 configuration.  It would
+ * be more logical to let the user choose between static IPv6 address and
+ * DHCPv6, and, completely independently, choose between static routes
+ * (if a static prefix length and/or gateway address is set) and ICMPv6.
+ * Yet a mechanism identical with IPv4 is easier to understand for a typical
+ * user so providing a static address just disables all automatic
+ * configuration.
+ */
+static void netconfig_add_v6_static_address_routes(struct l_netconfig *nc)
+{
+	char ip[INET6_ADDRSTRLEN];
+	uint32_t prefix_len;
+
+	nc->v6_address = l_rtnl_address_clone(nc->v6_static_addr);
+	l_queue_push_tail(nc->addresses.current, nc->v6_address);
+	l_queue_push_tail(nc->addresses.added, nc->v6_address);
+
+	l_rtnl_address_get_address(nc->v6_static_addr, ip);
+	prefix_len = l_rtnl_address_get_prefix_length(nc->v6_static_addr);
+	netconfig_add_v6_static_routes(nc, ip, prefix_len);
+}
+
 static void netconfig_do_static_config(struct l_idle *idle, void *user_data)
 {
 	struct l_netconfig *nc = user_data;
@@ -722,10 +712,16 @@ static void netconfig_do_static_config(struct l_idle *idle, void *user_data)
 	l_idle_remove(l_steal_ptr(nc->do_static_work));
 
 	if (nc->v4_static_addr && !nc->v4_configured) {
-		netconfig_add_static_address_routes(nc);
+		netconfig_add_v4_static_address_routes(nc);
 		nc->v4_configured = true;
 		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_CONFIGURE);
 	}
+
+	if (nc->v6_static_addr && !nc->v6_configured) {
+		netconfig_add_v6_static_address_routes(nc);
+		nc->v6_configured = true;
+		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_CONFIGURE);
+	}
 }
 
 LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
@@ -737,7 +733,7 @@ LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
 		return false;
 
 	if (!netconfig->v4_enabled)
-		goto done;
+		goto configure_ipv6;
 
 	if (netconfig->v4_static_addr) {
 		/*
@@ -747,12 +743,26 @@ LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
 		netconfig->do_static_work = l_idle_create(
 						netconfig_do_static_config,
 						netconfig, NULL);
-		goto done;
+		goto configure_ipv6;
 	}
 
 	if (!l_dhcp_client_start(netconfig->dhcp_client))
 		return false;
 
+configure_ipv6:
+	if (!netconfig->v6_enabled)
+		goto done;
+
+	if (netconfig->v6_static_addr && !netconfig->do_static_work) {
+		/*
+		 * We're basically ready to configure the interface
+		 * but do this in an idle callback.
+		 */
+		netconfig->do_static_work = l_idle_create(
+						netconfig_do_static_config,
+						netconfig, NULL);
+	}
+
 done:
 	netconfig->started = true;
 	return true;
@@ -776,6 +786,7 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)
 	netconfig->v4_address = NULL;
 	netconfig->v4_subnet_route = NULL;
 	netconfig->v4_default_route = NULL;
+	netconfig->v6_address = NULL;
 
 	l_dhcp_client_stop(netconfig->dhcp_client);
 }
Signature · 1 line
2.32.0
author
#4 lore raw

PATCH 5/11 netconfig: Create routes from Router Advertisements

In reply to Andrew Zaborowski

If IPv6 is enabled, create the ICMP6 client and handle Router Advertisements received by creating, updating and removing routes per the data in the Router Advertisements. Timeouts aren't handled yet.

---
 ell/netconfig.c | 208 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 206 insertions(+), 2 deletions(-)
ell/netconfig.c +206 -2
index 02a8b1d..3c1ad32 100644
@@ -27,12 +27,16 @@
 #include <linux/types.h>
 #include <netinet/ip.h>
 #include <arpa/inet.h>
+#include <netinet/icmp6.h>
 
 #include "private.h"
 #include "useful.h"
 #include "log.h"
 #include "dhcp.h"
 #include "dhcp-private.h"
+#include "icmp6.h"
+#include "icmp6-private.h"
+#include "dhcp6.h"
 #include "netlink.h"
 #include "rtnl.h"
 #include "queue.h"
@@ -63,6 +67,8 @@ struct l_netconfig {
 	bool v4_configured;
 	struct l_dhcp_client *dhcp_client;
 	bool v6_configured;
+	struct l_icmp6_client *icmp6_client;
+	struct l_dhcp6_client *dhcp6_client;
 
 	/* These objects, if not NULL, are owned by @addresses and @routes */
 	struct l_rtnl_address *v4_address;
@@ -351,6 +357,189 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
 	}
 }
 
+static struct l_rtnl_route *netconfig_find_icmp6_route(
+						struct l_netconfig *nc,
+						const uint8_t *gateway,
+						const struct route_info *dst)
+{
+	const struct l_queue_entry *entry;
+
+	for (entry = l_queue_get_entries(nc->routes.current); entry;
+			entry = entry->next) {
+		struct l_rtnl_route *route = entry->data;
+		const uint8_t *route_gateway;
+		const uint8_t *route_dst;
+		uint8_t route_prefix_len = 0;
+
+		if (l_rtnl_route_get_family(route) != AF_INET6 ||
+				l_rtnl_route_get_protocol(route) != RTPROT_RA)
+			continue;
+
+		route_gateway = l_rtnl_route_get_gateway_in_addr(route);
+		if ((gateway || route_gateway) &&
+				(!gateway || !route_gateway ||
+				 memcmp(gateway, route_gateway, 16)))
+			continue;
+
+		route_dst = l_rtnl_route_get_dst_in_addr(route,
+							&route_prefix_len);
+		if ((dst || route_prefix_len) &&
+				(!dst || !route_prefix_len ||
+				 dst->prefix_len != route_prefix_len ||
+				 memcmp(dst->address, route_dst,
+					 (dst->prefix_len + 7) / 8)))
+			continue;
+
+		return route;
+	}
+
+	return NULL;
+}
+
+static struct l_rtnl_route *netconfig_add_icmp6_route(struct l_netconfig *nc,
+						const uint8_t *gateway,
+						const struct route_info *dst,
+						uint8_t preference)
+{
+	struct l_rtnl_route *rt;
+	char buf1[INET6_ADDRSTRLEN];
+	char buf2[INET6_ADDRSTRLEN];
+
+	if (gateway && !dst)
+		rt = l_rtnl_route_new_gateway(inet_ntop(AF_INET6, gateway,
+							buf1, sizeof(buf1)));
+	else if (dst && !gateway)
+		rt = l_rtnl_route_new_prefix(inet_ntop(AF_INET6, dst->address,
+							buf2, sizeof(buf2)),
+						dst->prefix_len);
+	else
+		rt = l_rtnl_route_new_static(inet_ntop(AF_INET6, gateway,
+							buf1, sizeof(buf1)),
+						inet_ntop(AF_INET6,
+							dst->address,
+							buf2, sizeof(buf2)),
+						dst->prefix_len);
+
+	if (L_WARN_ON(!rt))
+		return NULL;
+
+	l_rtnl_route_set_preference(rt, preference);
+	l_rtnl_route_set_protocol(rt, RTPROT_RA);
+	l_rtnl_route_set_priority(rt, nc->route_priority);
+	l_queue_push_tail(nc->routes.current, rt);
+	l_queue_push_tail(nc->routes.added, rt);
+	return rt;
+}
+
+static void netconfig_set_icmp6_route_data(struct l_netconfig *nc,
+						struct l_rtnl_route *rt,
+						uint64_t start_time,
+						uint32_t preferred_lifetime,
+						uint32_t valid_lifetime,
+						uint32_t mtu, bool updated)
+{
+	uint64_t expiry = start_time + valid_lifetime * L_USEC_PER_SEC;
+
+	l_rtnl_route_set_mtu(rt, mtu);
+	l_rtnl_route_set_lifetime(rt, valid_lifetime);
+	l_rtnl_route_set_expiry(rt, expiry);
+
+	if (updated)
+		l_queue_push_tail(nc->routes.updated, rt);
+}
+
+static void netconfig_remove_icmp6_route(struct l_netconfig *nc,
+						struct l_rtnl_route *route)
+{
+	l_queue_remove(nc->routes.current, route);
+	l_queue_push_tail(nc->routes.removed, route);
+}
+
+static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
+						enum l_icmp6_client_event event,
+						void *event_data,
+						void *user_data)
+{
+	struct l_netconfig *nc = user_data;
+	const struct l_icmp6_router *r;
+	struct l_rtnl_route *default_route;
+	unsigned int i;
+
+	if (event != L_ICMP6_CLIENT_EVENT_ROUTER_FOUND)
+		return;
+
+	r = event_data;
+
+	/*
+	 * Note: If this is the first RA received, the l_dhcp6_client
+	 * will have received the event before us and will be acting
+	 * on it by now.
+	 */
+
+	if (nc->v4_gateway_override)
+		return;
+
+	/* Process the default gateway information */
+	default_route = netconfig_find_icmp6_route(nc, r->address, NULL);
+
+	if (!default_route && r->lifetime) {
+		default_route = netconfig_add_icmp6_route(nc, r->address, NULL,
+								r->pref);
+		if (unlikely(!default_route))
+			return;
+
+		netconfig_set_icmp6_route_data(nc, default_route, r->start_time,
+						r->lifetime, r->lifetime,
+						r->mtu, false);
+	} else if (default_route && r->lifetime)
+		netconfig_set_icmp6_route_data(nc, default_route, r->start_time,
+						r->lifetime, r->lifetime,
+						r->mtu, true);
+	else if (default_route && !r->lifetime)
+		netconfig_remove_icmp6_route(nc, default_route);
+
+	/*
+	 * Process the onlink and offlink routes, from the Router
+	 * Advertisement's Prefix Information options and Route
+	 * Information options respectively.
+	 */
+	for (i = 0; i < r->n_routes; i++) {
+		const struct route_info *info = &r->routes[i];
+		const uint8_t *gateway = info->onlink ? NULL : r->address;
+		struct l_rtnl_route *route =
+			netconfig_find_icmp6_route(nc, gateway, info);
+
+		if (!route && info->valid_lifetime) {
+			route = netconfig_add_icmp6_route(nc, gateway, info,
+							info->preference);
+			if (unlikely(!route))
+				continue;
+
+			netconfig_set_icmp6_route_data(nc, route, r->start_time,
+						info->preferred_lifetime,
+						info->valid_lifetime,
+						gateway ? r->mtu : 0, false);
+		} else if (route && info->valid_lifetime)
+			netconfig_set_icmp6_route_data(nc, route, r->start_time,
+						info->preferred_lifetime,
+						info->valid_lifetime,
+						gateway ? r->mtu : 0, true);
+		else if (route && !info->valid_lifetime)
+			netconfig_remove_icmp6_route(nc, route);
+	}
+
+	/*
+	 * Note: we may be emitting this before L_NETCONFIG_EVENT_CONFIGURE.
+	 * We should probably instead save the affected routes in separate
+	 * lists and add them to the _CONFIGURE event, suppressing any _UPDATE
+	 * events while nc->v6_configured is false.
+	 */
+	if (!l_queue_isempty(nc->routes.added) ||
+			!l_queue_isempty(nc->routes.updated) ||
+			!l_queue_isempty(nc->routes.removed))
+		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
+}
+
 LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
 {
 	struct l_netconfig *nc;
@@ -373,6 +562,13 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
 					netconfig_dhcp_event_handler,
 					nc, NULL);
 
+	nc->dhcp6_client = l_dhcp6_client_new(ifindex);
+
+	nc->icmp6_client = l_dhcp6_client_get_icmp6(nc->dhcp6_client);
+	l_icmp6_client_add_event_handler(nc->icmp6_client,
+					netconfig_icmp6_event_handler,
+					nc, NULL);
+
 	return nc;
 }
 
@@ -393,6 +589,7 @@ LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)
 	l_netconfig_set_domain_names_override(netconfig, AF_INET6, NULL);
 
 	l_dhcp_client_destroy(netconfig->dhcp_client);
+	l_dhcp6_client_destroy(netconfig->dhcp6_client);
 	l_netconfig_set_event_handler(netconfig, NULL, NULL, NULL);
 	l_queue_destroy(netconfig->addresses.current, NULL);
 	l_queue_destroy(netconfig->addresses.added, NULL);
@@ -753,16 +950,22 @@ configure_ipv6:
 	if (!netconfig->v6_enabled)
 		goto done;
 
-	if (netconfig->v6_static_addr && !netconfig->do_static_work) {
+	if (netconfig->v6_static_addr) {
 		/*
 		 * We're basically ready to configure the interface
 		 * but do this in an idle callback.
 		 */
-		netconfig->do_static_work = l_idle_create(
+		if (!netconfig->do_static_work)
+			netconfig->do_static_work = l_idle_create(
 						netconfig_do_static_config,
 						netconfig, NULL);
+
+		goto done;
 	}
 
+	if (!l_dhcp6_client_start(netconfig->dhcp6_client))
+		return false;
+
 done:
 	netconfig->started = true;
 	return true;
@@ -789,6 +992,7 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)
 	netconfig->v6_address = NULL;
 
 	l_dhcp_client_stop(netconfig->dhcp_client);
+	l_dhcp6_client_stop(netconfig->dhcp6_client);
 }
 
 LIB_EXPORT struct l_dhcp_client *l_netconfig_get_dhcp_client(
Signature · 1 line
2.32.0
author
#5 lore raw

PATCH 6/11 netconfig: Add l_netconfig_get_{dhcp6,icmp6}_client

In reply to Andrew Zaborowski
---
 ell/ell.sym     |  2 ++
 ell/netconfig.c | 18 ++++++++++++++++++
 ell/netconfig.h |  4 ++++
 3 files changed, 24 insertions(+)
ell/ell.sym +2 -0
index 60dd257..0f9f884 100644
@@ -749,6 +749,8 @@ global:
 	l_netconfig_start;
 	l_netconfig_stop;
 	l_netconfig_get_dhcp_client;
+	l_netconfig_get_dhcp6_client;
+	l_netconfig_get_icmp6_client;
 	l_netconfig_set_event_handler;
 	l_netconfig_apply_rtnl;
 	l_netconfig_get_addresses;
ell/netconfig.c +18 -0
index 3c1ad32..0419b0f 100644
@@ -1004,6 +1004,24 @@ LIB_EXPORT struct l_dhcp_client *l_netconfig_get_dhcp_client(
 	return netconfig->dhcp_client;
 }
 
+LIB_EXPORT struct l_dhcp6_client *l_netconfig_get_dhcp6_client(
+						struct l_netconfig *netconfig)
+{
+	if (unlikely(!netconfig))
+		return NULL;
+
+	return netconfig->dhcp6_client;
+}
+
+LIB_EXPORT struct l_icmp6_client *l_netconfig_get_icmp6_client(
+						struct l_netconfig *netconfig)
+{
+	if (unlikely(!netconfig))
+		return NULL;
+
+	return netconfig->icmp6_client;
+}
+
 LIB_EXPORT void l_netconfig_set_event_handler(struct l_netconfig *netconfig,
 					l_netconfig_event_cb_t handler,
 					void *user_data,
ell/netconfig.h +4 -0
index f90ec81..a50a895 100644
@@ -75,6 +75,10 @@ void l_netconfig_stop(struct l_netconfig *netconfig);
 
 struct l_dhcp_client *l_netconfig_get_dhcp_client(
 						struct l_netconfig *netconfig);
+struct l_dhcp6_client *l_netconfig_get_dhcp6_client(
+						struct l_netconfig *netconfig);
+struct l_icmp6_client *l_netconfig_get_icmp6_client(
+						struct l_netconfig *netconfig);
 
 void l_netconfig_set_event_handler(struct l_netconfig *netconfig,
 					l_netconfig_event_cb_t handler,
Signature · 1 line
2.32.0
author
#6 lore raw

PATCH 7/11 netconfig: Avoid returning internal l_queues

In reply to Andrew Zaborowski

Return const struct l_queue_entry pointers instead.

---
 ell/netconfig.c | 35 ++++++++++++++++++-----------------
 ell/netconfig.h | 20 +++++++++++---------
 2 files changed, 29 insertions(+), 26 deletions(-)
ell/netconfig.c +18 -17
index 0419b0f..fdd2705 100644
@@ -1076,37 +1076,38 @@ LIB_EXPORT void l_netconfig_apply_rtnl(struct l_netconfig *netconfig,
 					NULL, NULL, NULL);
 }
 
-LIB_EXPORT struct l_queue *l_netconfig_get_addresses(
-						struct l_netconfig *netconfig,
-						struct l_queue **out_added,
-						struct l_queue **out_updated,
-						struct l_queue **out_removed)
+LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses(
+					struct l_netconfig *netconfig,
+					const struct l_queue_entry **out_added,
+					const struct l_queue_entry **out_updated,
+					const struct l_queue_entry **out_removed)
 {
 	if (out_added)
-		*out_added = netconfig->addresses.added;
+		*out_added = l_queue_get_entries(netconfig->addresses.added);
 
 	if (out_updated)
-		*out_updated = netconfig->addresses.updated;
+		*out_updated = l_queue_get_entries(netconfig->addresses.updated);
 
 	if (out_removed)
-		*out_removed = netconfig->addresses.removed;
+		*out_removed = l_queue_get_entries(netconfig->addresses.removed);
 
-	return netconfig->addresses.current;
+	return l_queue_get_entries(netconfig->addresses.current);
 }
 
-LIB_EXPORT struct l_queue *l_netconfig_get_routes(struct l_netconfig *netconfig,
-						struct l_queue **out_added,
-						struct l_queue **out_updated,
-						struct l_queue **out_removed)
+LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes(
+					struct l_netconfig *netconfig,
+					const struct l_queue_entry **out_added,
+					const struct l_queue_entry **out_updated,
+					const struct l_queue_entry **out_removed)
 {
 	if (out_added)
-		*out_added = netconfig->routes.added;
+		*out_added = l_queue_get_entries(netconfig->routes.added);
 
 	if (out_updated)
-		*out_updated = netconfig->routes.updated;
+		*out_updated = l_queue_get_entries(netconfig->routes.updated);
 
 	if (out_removed)
-		*out_removed = netconfig->routes.removed;
+		*out_removed = l_queue_get_entries(netconfig->routes.removed);
 
-	return netconfig->routes.current;
+	return l_queue_get_entries(netconfig->routes.current);
 }
ell/netconfig.h +11 -9
index a50a895..7796a2f 100644
@@ -33,7 +33,7 @@ struct l_dhcp_client;
 struct l_dhcp6_client;
 struct l_icmp6_client;
 struct l_netlink;
-struct l_queue;
+struct l_queue_entry;
 struct l_netconfig;
 struct l_rtnl_address;
 struct l_rtnl_route;
@@ -87,14 +87,16 @@ void l_netconfig_set_event_handler(struct l_netconfig *netconfig,
 
 void l_netconfig_apply_rtnl(struct l_netconfig *netconfig,
 				struct l_netlink *rtnl);
-struct l_queue *l_netconfig_get_addresses(struct l_netconfig *netconfig,
-					struct l_queue **out_added,
-					struct l_queue **out_updated,
-					struct l_queue **out_removed);
-struct l_queue *l_netconfig_get_routes(struct l_netconfig *netconfig,
-					struct l_queue **out_added,
-					struct l_queue **out_updated,
-					struct l_queue **out_removed);
+const struct l_queue_entry *l_netconfig_get_addresses(
+				struct l_netconfig *netconfig,
+				const struct l_queue_entry **out_added,
+				const struct l_queue_entry **out_updated,
+				const struct l_queue_entry **out_removed);
+const struct l_queue_entry *l_netconfig_get_routes(
+				struct l_netconfig *netconfig,
+				const struct l_queue_entry **out_added,
+				const struct l_queue_entry **out_updated,
+				const struct l_queue_entry **out_removed);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.32.0
author
#7 lore raw

PATCH 8/11 netconfig: Handle DHCPv6 events

In reply to Andrew Zaborowski

Use l_dhcp6_client to support IPv6 address assignment through DHCPv6.

---
 ell/netconfig.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 133 insertions(+)
ell/netconfig.c +133 -0
index fdd2705..bc573d7 100644
@@ -357,6 +357,136 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
 	}
 }
 
+static void netconfig_add_dhcp6_address(struct l_netconfig *nc)
+{
+	const struct l_dhcp6_lease *lease =
+		l_dhcp6_client_get_lease(nc->dhcp6_client);
+	_auto_(l_free) char *ip = NULL;
+	uint32_t prefix_len;
+
+	if (L_WARN_ON(!lease))
+		return;
+
+	ip = l_dhcp6_lease_get_address(lease);
+	prefix_len = l_dhcp6_lease_get_prefix_length(lease);
+	nc->v6_address = l_rtnl_address_new(ip, prefix_len);
+
+	if (L_WARN_ON(!nc->v6_address))
+		return;
+
+	/*
+	 * TODO: check if we have a prefix route covering the IP,
+	 * l_dhcp6_client doesn't guarantee that the received IP is in
+	 * on of the subnets marked as on-link in the RA.  If it isn't,
+	 * maybe warn and set the below to false.  Or create a route using
+	 * the prefix length from lease, set RTPROT_DHCP to distinguish it
+	 * from other routes.
+	 */
+	l_rtnl_address_set_noprefixroute(nc->v6_address, true);
+
+	l_queue_push_tail(nc->addresses.current, nc->v6_address);
+	l_queue_push_tail(nc->addresses.added, nc->v6_address);
+}
+
+static void netconfig_set_dhcp6_address_lifetimes(struct l_netconfig *nc,
+							bool updated)
+{
+	const struct l_dhcp6_lease *lease =
+		l_dhcp6_client_get_lease(nc->dhcp6_client);
+	uint32_t p, v;
+	uint64_t start_time;
+
+	if (L_WARN_ON(!lease))
+		return;
+
+	p = l_dhcp6_lease_get_preferred_lifetime(lease);
+	v = l_dhcp6_lease_get_valid_lifetime(lease);
+	start_time = l_dhcp6_lease_get_start_time(lease);
+
+	l_rtnl_address_set_lifetimes(nc->v6_address, p, v);
+	l_rtnl_address_set_expiry(nc->v6_address,
+					start_time + p * L_USEC_PER_SEC,
+					start_time + v * L_USEC_PER_SEC);
+
+	if (updated)
+		l_queue_push_tail(nc->addresses.updated, nc->v6_address);
+}
+
+static void netconfig_remove_dhcp6_address(struct l_netconfig *nc)
+{
+	l_queue_remove(nc->addresses.current, nc->v6_address);
+	l_queue_push_tail(nc->addresses.removed, nc->v6_address);
+	nc->v6_address = NULL;
+}
+
+static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
+						enum l_dhcp6_client_event event,
+						void *user_data)
+{
+	struct l_netconfig *nc = user_data;
+
+	switch (event) {
+	case L_DHCP6_CLIENT_EVENT_LEASE_OBTAINED:
+		if (L_WARN_ON(nc->v6_configured))
+			break;
+
+		netconfig_add_dhcp6_address(nc);
+		netconfig_set_dhcp6_address_lifetimes(nc, false);
+		nc->v6_configured = true;
+		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_CONFIGURE);
+		break;
+	case L_DHCP6_CLIENT_EVENT_IP_CHANGED:
+		if (L_WARN_ON(!nc->v6_configured))
+			break;
+
+		netconfig_remove_dhcp6_address(nc);
+		netconfig_add_dhcp6_address(nc);
+		netconfig_set_dhcp6_address_lifetimes(nc, false);
+		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
+		break;
+	case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED:
+		if (L_WARN_ON(!nc->v6_configured))
+			break;
+
+		netconfig_remove_dhcp6_address(nc);
+		nc->v6_configured = false;
+
+		if (l_dhcp6_client_start(nc->dhcp6_client))
+			/* TODO: also start a new timeout */
+			netconfig_emit_event(nc, AF_INET6,
+						L_NETCONFIG_EVENT_UNCONFIGURE);
+		else
+			netconfig_emit_event(nc, AF_INET6,
+						L_NETCONFIG_EVENT_FAILED);
+
+		break;
+	case L_DHCP6_CLIENT_EVENT_LEASE_RENEWED:
+		if (L_WARN_ON(!nc->v6_configured))
+			break;
+
+		netconfig_set_dhcp6_address_lifetimes(nc, true);
+		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
+		break;
+	case L_DHCP6_CLIENT_EVENT_NO_LEASE:
+		if (L_WARN_ON(nc->v6_configured))
+			break;
+
+		/*
+		 * The requested address is no longer available, try to restart
+		 * the client.
+		 *
+		 * TODO: this may need to be delayed so we don't flood the
+		 * network with SOLICITs and DECLINEs.  Also add a retry limit
+		 * or better yet a configurable timeout.
+		 */
+		if (!l_dhcp6_client_start(nc->dhcp6_client))
+			netconfig_emit_event(nc, AF_INET6,
+						L_NETCONFIG_EVENT_FAILED);
+
+		break;
+	}
+}
+
 static struct l_rtnl_route *netconfig_find_icmp6_route(
 						struct l_netconfig *nc,
 						const uint8_t *gateway,
@@ -563,6 +693,9 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
 					nc, NULL);
 
 	nc->dhcp6_client = l_dhcp6_client_new(ifindex);
+	l_dhcp6_client_set_event_handler(nc->dhcp6_client,
+					netconfig_dhcp6_event_handler,
+					nc, NULL);
 
 	nc->icmp6_client = l_dhcp6_client_get_icmp6(nc->dhcp6_client);
 	l_icmp6_client_add_event_handler(nc->icmp6_client,
Signature · 1 line
2.32.0
author
#8 lore raw

PATCH 9/11 netconfig: Add getters for DNS addresses and domain names

In reply to Andrew Zaborowski
---
 ell/ell.sym     |  2 ++
 ell/netconfig.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++
 ell/netconfig.h |  2 ++
 3 files changed, 68 insertions(+)
ell/ell.sym +2 -0
index 0f9f884..0ce604b 100644
@@ -755,6 +755,8 @@ global:
 	l_netconfig_apply_rtnl;
 	l_netconfig_get_addresses;
 	l_netconfig_get_routes;
+	l_netconfig_get_dns_list;
+	l_netconfig_get_domain_names;
 local:
 	*;
 };
ell/netconfig.c +64 -0

67 lines not on this page · raw message

ell/netconfig.h +2 -0
index 7796a2f..8bb0c0e 100644
@@ -97,6 +97,8 @@ const struct l_queue_entry *l_netconfig_get_routes(
 				const struct l_queue_entry **out_added,
 				const struct l_queue_entry **out_updated,
 				const struct l_queue_entry **out_removed);
+char **l_netconfig_get_dns_list(struct l_netconfig *netconfig);
+char **l_netconfig_get_domain_names(struct l_netconfig *netconfig);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.32.0
author
#9 lore raw

PATCH 10/11 netconfig: Expire addresses and routes

In reply to Andrew Zaborowski

Start differentiating between routes/addresses being removed and those expiring. Generally if we're committing the routes through RTNL, the lifetimes are submitted to the kernel and the kernel should expire them for us on the kernel side. In that case there's no need to send an RTM_DELROUTE/RTM_DELADDR. That should apply to routes received in Router Advertisements as well as addresses and routes from expiring DHCP leases and DHCPv6 leases.

For routes/addresses being removed because a router is going offline and signals that by setting the lifetime to 0 in an RA, or as a result of L_DHCP_CLIENT_EVENT_IP_CHANGED and similar, us or the user do need to send RTM_DELROUTE/RTM_DELADDR. To differentiate between the two cases (removal and expiry) pass two separate lists to the user.

Add checks for expired IPv6 routes in the ICMPv6 event handler to avoid using timers for that. We have no bound on the number of routes (yet) so we avoid keeping potentially many timers or keeping one timer tracking multiple expiry times which would add a little complexity (but might be acceptable.)

---
 ell/netconfig.c | 160 ++++++++++++++++++++++++++++++++++++++++++------
 ell/netconfig.h |   6 +-
 2 files changed, 145 insertions(+), 21 deletions(-)
ell/netconfig.c +141 -19
index 6bad38e..baafec2 100644
@@ -69,6 +69,7 @@ struct l_netconfig {
 	bool v6_configured;
 	struct l_icmp6_client *icmp6_client;
 	struct l_dhcp6_client *dhcp6_client;
+	struct l_idle *signal_expired_work;
 
 	/* These objects, if not NULL, are owned by @addresses and @routes */
 	struct l_rtnl_address *v4_address;
@@ -89,10 +90,14 @@ struct l_netconfig {
 		 * for example those that only have their lifetime updated.
 		 *
 		 * Any entries in @added and @updated are owned by @current.
+		 * Entries in @removed need to be removed with an
+		 * RTM_DELADD/RTM_DELROUTE while those in @expired are only
+		 * informative as the kernel will have removed them already.
 		 */
 		struct l_queue *added;
 		struct l_queue *updated;
 		struct l_queue *removed;
+		struct l_queue *expired;
 	} addresses, routes;
 
 	struct {
@@ -108,10 +113,14 @@ static void netconfig_update_cleanup(struct l_netconfig *nc)
 	l_queue_clear(nc->addresses.updated, NULL);
 	l_queue_clear(nc->addresses.removed,
 			(l_queue_destroy_func_t) l_rtnl_address_free);
+	l_queue_clear(nc->addresses.expired,
+			(l_queue_destroy_func_t) l_rtnl_address_free);
 	l_queue_clear(nc->routes.added, NULL);
 	l_queue_clear(nc->routes.updated, NULL);
 	l_queue_clear(nc->routes.removed,
 			(l_queue_destroy_func_t) l_rtnl_route_free);
+	l_queue_clear(nc->routes.expired,
+			(l_queue_destroy_func_t) l_rtnl_route_free);
 }
 
 static void netconfig_emit_event(struct l_netconfig *nc, uint8_t family,
@@ -128,6 +137,24 @@ static void netconfig_emit_event(struct l_netconfig *nc, uint8_t family,
 		netconfig_update_cleanup(nc);
 }
 
+static void netconfig_signal_expired(struct l_idle *idle, void *user_data)
+{
+	struct l_netconfig *nc = user_data;
+
+	l_idle_remove(l_steal_ptr(nc->signal_expired_work));
+
+	/*
+	 * If the idle work was scheduled from within l_netconfig_get_routes
+	 * or netconfig_icmp6_event_handler, the user is likely to have
+	 * already received an event and had a chance to process the expired
+	 * routes list.  In that case there's no need to emit a new event,
+	 * and the list will have been emptied in netconfig_update_cleanup()
+	 * anyway.
+	 */
+	if (!l_queue_isempty(nc->routes.expired))
+		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
+}
+
 static void netconfig_add_v4_routes(struct l_netconfig *nc, const char *ip,
 					uint8_t prefix_len, const char *gateway,
 					uint8_t rtm_protocol)
@@ -211,6 +238,11 @@ static void netconfig_add_v6_static_routes(struct l_netconfig *nc,
 	l_queue_push_tail(nc->routes.added, v6_default_route);
 }
 
+static bool netconfig_match_ptr(const void *a, const void *b)
+{
+	return a == b;
+}
+
 static void netconfig_add_dhcp_address_routes(struct l_netconfig *nc)
 {
 	const struct l_dhcp_lease *lease =
@@ -254,13 +286,15 @@ static void netconfig_set_dhcp_lifetimes(struct l_netconfig *nc, bool updated)
 	l_rtnl_address_set_lifetimes(nc->v4_address, 0, lifetime);
 	l_rtnl_address_set_expiry(nc->v4_address, 0, expiry);
 
-	if (updated)
+	if (updated && !l_queue_find(nc->addresses.added, netconfig_match_ptr,
+				nc->v4_address))
 		l_queue_push_tail(nc->addresses.updated, nc->v4_address);
 
 	l_rtnl_route_set_lifetime(nc->v4_subnet_route, lifetime);
 	l_rtnl_route_set_expiry(nc->v4_subnet_route, expiry);
 
-	if (updated)
+	if (updated && !l_queue_find(nc->routes.added, netconfig_match_ptr,
+				nc->v4_subnet_route))
 		l_queue_push_tail(nc->routes.updated, nc->v4_subnet_route);
 
 	if (!nc->v4_default_route)
@@ -269,23 +303,42 @@ static void netconfig_set_dhcp_lifetimes(struct l_netconfig *nc, bool updated)
 	l_rtnl_route_set_lifetime(nc->v4_default_route, lifetime);
 	l_rtnl_route_set_expiry(nc->v4_default_route, expiry);
 
-	if (updated)
+	if (updated && !l_queue_find(nc->routes.added, netconfig_match_ptr,
+				nc->v4_default_route))
 		l_queue_push_tail(nc->routes.updated, nc->v4_default_route);
 }
 
-static void netconfig_remove_dhcp_address_routes(struct l_netconfig *nc)
+static void netconfig_remove_dhcp_address_routes(struct l_netconfig *nc,
+							bool expired)
 {
+	struct l_queue *routes =
+		expired ? nc->routes.expired : nc->routes.removed;
+
 	l_queue_remove(nc->addresses.current, nc->v4_address);
-	l_queue_push_tail(nc->addresses.removed, nc->v4_address);
+	l_queue_remove(nc->addresses.updated, nc->v4_address);
+
+	if (!l_queue_remove(nc->addresses.added, nc->v4_address))
+		l_queue_push_tail(
+			expired ? nc->addresses.expired : nc->addresses.removed,
+			nc->v4_address);
+
 	nc->v4_address = NULL;
 
 	l_queue_remove(nc->routes.current, nc->v4_subnet_route);
-	l_queue_push_tail(nc->routes.removed, nc->v4_subnet_route);
+	l_queue_remove(nc->routes.updated, nc->v4_subnet_route);
+
+	if (!l_queue_remove(nc->routes.added, nc->v4_subnet_route))
+		l_queue_push_tail(routes, nc->v4_subnet_route);
+
 	nc->v4_subnet_route = NULL;
 
 	if (nc->v4_default_route) {
 		l_queue_remove(nc->routes.current, nc->v4_default_route);
-		l_queue_push_tail(nc->routes.removed, nc->v4_default_route);
+		l_queue_remove(nc->routes.updated, nc->v4_default_route);
+
+		if (!l_queue_remove(nc->routes.added, nc->v4_default_route))
+			l_queue_push_tail(routes, nc->v4_default_route);
+
 		nc->v4_default_route = NULL;
 	}
 }
@@ -301,7 +354,7 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
 		if (L_WARN_ON(!nc->v4_configured))
 			break;
 
-		netconfig_remove_dhcp_address_routes(nc);
+		netconfig_remove_dhcp_address_routes(nc, false);
 		netconfig_add_dhcp_address_routes(nc);
 		netconfig_set_dhcp_lifetimes(nc, false);
 		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_UPDATE);
@@ -326,7 +379,7 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
 		if (L_WARN_ON(!nc->v4_configured))
 			break;
 
-		netconfig_remove_dhcp_address_routes(nc);
+		netconfig_remove_dhcp_address_routes(nc, true);
 		nc->v4_configured = false;
 
 		if (l_dhcp_client_start(nc->dhcp_client))
@@ -408,14 +461,21 @@ static void netconfig_set_dhcp6_address_lifetimes(struct l_netconfig *nc,
 					start_time + p * L_USEC_PER_SEC,
 					start_time + v * L_USEC_PER_SEC);
 
-	if (updated)
+	if (updated && !l_queue_find(nc->addresses.added, netconfig_match_ptr,
+					nc->v6_address))
 		l_queue_push_tail(nc->addresses.updated, nc->v6_address);
 }
 
-static void netconfig_remove_dhcp6_address(struct l_netconfig *nc)
+static void netconfig_remove_dhcp6_address(struct l_netconfig *nc, bool expired)
 {
 	l_queue_remove(nc->addresses.current, nc->v6_address);
-	l_queue_push_tail(nc->addresses.removed, nc->v6_address);
+	l_queue_remove(nc->addresses.updated, nc->v6_address);
+
+	if (!l_queue_remove(nc->addresses.added, nc->v6_address))
+		l_queue_push_tail(
+			expired ? nc->addresses.expired : nc->addresses.removed,
+			nc->v6_address);
+
 	nc->v6_address = NULL;
 }
 
@@ -439,7 +499,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 		if (L_WARN_ON(!nc->v6_configured))
 			break;
 
-		netconfig_remove_dhcp6_address(nc);
+		netconfig_remove_dhcp6_address(nc, false);
 		netconfig_add_dhcp6_address(nc);
 		netconfig_set_dhcp6_address_lifetimes(nc, false);
 		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
@@ -448,7 +508,7 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 		if (L_WARN_ON(!nc->v6_configured))
 			break;
 
-		netconfig_remove_dhcp6_address(nc);
+		netconfig_remove_dhcp6_address(nc, true);
 		nc->v6_configured = false;
 
 		if (l_dhcp6_client_start(nc->dhcp6_client))
@@ -487,6 +547,49 @@ static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
 	}
 }
 
+static uint64_t now;
+
+static bool netconfig_check_route_expired(void *data, void *user_data)
+{
+	struct l_netconfig *nc = user_data;
+	struct l_rtnl_route *route = data;
+	uint64_t expiry_time = l_rtnl_route_get_expiry(route);
+
+	if (!expiry_time || now < expiry_time)
+		return false;
+
+	/*
+	 * Since we set lifetimes on the routes we submit to the kernel with
+	 * RTM_NEWROUTE, we count on them being deleted automatically so no
+	 * need to send an RTM_DELROUTE.  We signal the fact that the route
+	 * expired to the user by having it on the expired list but there's
+	 * nothing that the user needs to do with the routes on that list
+	 * like they do with the added, updated and removed lists.
+	 *
+	 * 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, route))
+		l_queue_push_tail(nc->routes.expired, route);
+
+	l_queue_remove(nc->routes.updated, route);
+	l_queue_remove(nc->routes.removed, route);
+	return true;
+}
+
+static void netconfig_expire_routes(struct l_netconfig *nc)
+{
+	now = l_time_now();
+
+	if (l_queue_foreach_remove(nc->routes.current,
+					netconfig_check_route_expired, nc) &&
+			!l_queue_isempty(nc->routes.expired) &&
+			!nc->signal_expired_work)
+		nc->signal_expired_work = l_idle_create(
+						netconfig_signal_expired,
+						nc, NULL);
+}
+
 static struct l_rtnl_route *netconfig_find_icmp6_route(
 						struct l_netconfig *nc,
 						const uint8_t *gateway,
@@ -574,7 +677,7 @@ static void netconfig_set_icmp6_route_data(struct l_netconfig *nc,
 	l_rtnl_route_set_lifetime(rt, valid_lifetime);
 	l_rtnl_route_set_expiry(rt, expiry);
 
-	if (updated)
+	if (updated && !l_queue_find(nc->routes.added, netconfig_match_ptr, rt))
 		l_queue_push_tail(nc->routes.updated, rt);
 }
 
@@ -582,7 +685,10 @@ static void netconfig_remove_icmp6_route(struct l_netconfig *nc,
 						struct l_rtnl_route *route)
 {
 	l_queue_remove(nc->routes.current, route);
-	l_queue_push_tail(nc->routes.removed, route);
+	l_queue_remove(nc->routes.updated, route);
+
+	if (!l_queue_remove(nc->routes.added, route))
+		l_queue_push_tail(nc->routes.removed, route);
 }
 
 static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
@@ -609,6 +715,8 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
 	if (nc->v4_gateway_override)
 		return;
 
+	netconfig_expire_routes(nc);
+
 	/* Process the default gateway information */
 	default_route = netconfig_find_icmp6_route(nc, r->address, NULL);
 
@@ -666,7 +774,8 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
 	 */
 	if (!l_queue_isempty(nc->routes.added) ||
 			!l_queue_isempty(nc->routes.updated) ||
-			!l_queue_isempty(nc->routes.removed))
+			!l_queue_isempty(nc->routes.removed) ||
+			!l_queue_isempty(nc->routes.expired))
 		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
 }
 
@@ -1114,6 +1223,9 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)
 	if (netconfig->do_static_work)
 		l_idle_remove(l_steal_ptr(netconfig->do_static_work));
 
+	if (netconfig->signal_expired_work)
+		l_idle_remove(l_steal_ptr(netconfig->signal_expired_work));
+
 	netconfig_update_cleanup(netconfig);
 	l_queue_clear(netconfig->routes.current,
 			(l_queue_destroy_func_t) l_rtnl_route_free);
@@ -1213,7 +1325,8 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses(
 					struct l_netconfig *netconfig,
 					const struct l_queue_entry **out_added,
 					const struct l_queue_entry **out_updated,
-					const struct l_queue_entry **out_removed)
+					const struct l_queue_entry **out_removed,
+					const struct l_queue_entry **out_expired)
 {
 	if (out_added)
 		*out_added = l_queue_get_entries(netconfig->addresses.added);
@@ -1224,6 +1337,9 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses(
 	if (out_removed)
 		*out_removed = l_queue_get_entries(netconfig->addresses.removed);
 
+	if (out_expired)
+		*out_expired = l_queue_get_entries(netconfig->addresses.expired);
+
 	return l_queue_get_entries(netconfig->addresses.current);
 }
 
@@ -1231,8 +1347,11 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes(
 					struct l_netconfig *netconfig,
 					const struct l_queue_entry **out_added,
 					const struct l_queue_entry **out_updated,
-					const struct l_queue_entry **out_removed)
+					const struct l_queue_entry **out_removed,
+					const struct l_queue_entry **out_expired)
 {
+	netconfig_expire_routes(netconfig);
+
 	if (out_added)
 		*out_added = l_queue_get_entries(netconfig->routes.added);
 
@@ -1242,6 +1361,9 @@ LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes(
 	if (out_removed)
 		*out_removed = l_queue_get_entries(netconfig->routes.removed);
 
+	if (out_expired)
+		*out_expired = l_queue_get_entries(netconfig->routes.expired);
+
 	return l_queue_get_entries(netconfig->routes.current);
 }
 
ell/netconfig.h +4 -2
index 8bb0c0e..d95e013 100644
@@ -91,12 +91,14 @@ const struct l_queue_entry *l_netconfig_get_addresses(
 				struct l_netconfig *netconfig,
 				const struct l_queue_entry **out_added,
 				const struct l_queue_entry **out_updated,
-				const struct l_queue_entry **out_removed);
+				const struct l_queue_entry **out_removed,
+				const struct l_queue_entry **out_expired);
 const struct l_queue_entry *l_netconfig_get_routes(
 				struct l_netconfig *netconfig,
 				const struct l_queue_entry **out_added,
 				const struct l_queue_entry **out_updated,
-				const struct l_queue_entry **out_removed);
+				const struct l_queue_entry **out_removed,
+				const struct l_queue_entry **out_expired);
 char **l_netconfig_get_dns_list(struct l_netconfig *netconfig);
 char **l_netconfig_get_domain_names(struct l_netconfig *netconfig);
 
Signature · 1 line
2.32.0
author
#10 lore raw

PATCH 11/11 examples: Update l_netconfig_get_{addresses, routes} calls

In reply to Andrew Zaborowski
---
 examples/netconfig-test.c | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
examples/netconfig-test.c +19 -17
index c9d02ab..573bf86 100644
@@ -61,16 +61,14 @@ static void signal_handler(uint32_t signo, void *user_data)
 }
 
 static void log_addresses(const char *af_str, const char *action,
-				struct l_queue *list)
+				const struct l_queue_entry *entry)
 {
-	const struct l_queue_entry *entry;
-
-	if (l_queue_isempty(list))
+	if (!entry)
 		return;
 
 	l_info("[netconfig%s] Addresses %s:", af_str, action);
 
-	for (entry = l_queue_get_entries(list); entry; entry = entry->next) {
+	for (; entry; entry = entry->next) {
 		struct l_rtnl_address *addr = entry->data;
 		char ip_str[INET6_ADDRSTRLEN];
 
@@ -83,25 +81,26 @@ static void log_addresses(const char *af_str, const char *action,
 }
 
 static void log_routes(const char *af_str, const char *action,
-			struct l_queue *list)
+			const struct l_queue_entry *entry)
 {
-	const struct l_queue_entry *entry;
-
-	if (l_queue_isempty(list))
+	if (!entry)
 		return;
 
 	l_info("[netconfig%s] Routes %s:", af_str, action);
 
-	for (entry = l_queue_get_entries(list); entry; entry = entry->next) {
+	for (; entry; entry = entry->next) {
 		struct l_rtnl_route *rt = entry->data;
-		char subnet_str[INET6_ADDRSTRLEN];
+		char subnet_str[INET6_ADDRSTRLEN] = "unknown";
 		char gateway_str[INET6_ADDRSTRLEN];
 		uint8_t prefix_len;
+		bool onlink;
 
 		l_rtnl_route_get_dst(rt, subnet_str, &prefix_len);
-		l_rtnl_route_get_gateway(rt, gateway_str);
-		l_info("[netconfig%s] \t%s/%i, gateway %s, orig lifetime %i s",
-			af_str, subnet_str, prefix_len, gateway_str,
+		onlink = !l_rtnl_route_get_gateway(rt, gateway_str);
+		l_info("[netconfig%s] \t%s/%i, %s%s, orig lifetime %i s",
+			af_str, subnet_str, prefix_len,
+			onlink ? "onlink" : "next-hop ",
+			onlink ? "" : gateway_str,
 			l_rtnl_route_get_lifetime(rt));
 	}
 }
@@ -110,7 +109,7 @@ static void event_handler(struct l_netconfig *netconfig, uint8_t family,
 				enum l_netconfig_event event, void *user_data)
 {
 	const char *af_str = family == AF_INET ? "v4" : "v6";
-	struct l_queue *added, *updated, *removed;
+	const struct l_queue_entry *added, *updated, *removed, *expired;
 
 	switch (event) {
 	case L_NETCONFIG_EVENT_CONFIGURE:
@@ -128,15 +127,18 @@ static void event_handler(struct l_netconfig *netconfig, uint8_t family,
 		return;
 	}
 
-	l_netconfig_get_addresses(netconfig, &added, &updated, &removed);
+	l_netconfig_get_addresses(netconfig, &added, &updated,
+					&removed, &expired);
 	log_addresses(af_str, "added", added);
 	log_addresses(af_str, "updated", updated);
 	log_addresses(af_str, "removed", removed);
+	log_addresses(af_str, "expired", expired);
 
-	l_netconfig_get_routes(netconfig, &added, &updated, &removed);
+	l_netconfig_get_routes(netconfig, &added, &updated, &removed, &expired);
 	log_routes(af_str, "added", added);
 	log_routes(af_str, "updated", updated);
 	log_routes(af_str, "removed", removed);
+	log_routes(af_str, "expired", expired);
 
 	if (apply)
 		l_netconfig_apply_rtnl(netconfig, rtnl);
Signature · 1 line
2.32.0