Skip to contentSkip to conversation

net: Add l_net_subnet_matches

PATCH Applied Andrew Zaborowski 2 participants 32 messages Apr 11, 2022 – Apr 21, 2022

lore
Share & tools
lore

Andrew Zaborowski

author
#0 lore raw

PATCH 1/11 net: Add l_net_subnet_matches

Add inline function to check if two IP addresses are in the same subnet.

---
 ell/net.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
ell/net.h +21 -0
index c97d9c9..837209c 100644
@@ -25,6 +25,7 @@
 
 #include <stdbool.h>
 #include <stdint.h>
+#include <string.h>
 
 struct in_addr;
 struct in6_addr;
@@ -40,6 +41,26 @@ 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,
+					uint8_t prefix_len)
+{
+	uint8_t bytes = prefix_len / 8;
+	uint8_t bits = prefix_len & 7;
+
+	/*
+	 * @a and @b are network byte order IPv4 or IPv6 addresses.
+	 * We want to check if the initial (top) @prefix_len bits match.
+	 * memcmp the whole bytes, then compare the final byte's top
+	 * bits by anding with a mask.
+	 */
+	if (bytes && memcmp(a, b, bytes))
+		return false;
+
+	return !bits ||
+		((((const uint8_t *) a)[bytes] ^ ((const uint8_t *) b)[bytes]) &
+		 (0xff00u >> bits)) == 0;
+}
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.32.0
author
#1 lore raw

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

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

---
 ell/ell.sym     |   8 +
 ell/netconfig.c | 413 +++++++++++++++++++++++++++++++++++++++++++++++-
 ell/netconfig.h |  16 ++
 3 files changed, 432 insertions(+), 5 deletions(-)
ell/ell.sym +8 -0
index a03dc69..ec995fc 100644
@@ -735,6 +735,14 @@ global:
 	/* netconfig */
 	l_netconfig_new;
 	l_netconfig_destroy;
+	l_netconfig_set_af_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 +408 -5

459 lines not on this page · raw message

ell/netconfig.h +16 -0
index 682f4aa..aab9d3f 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_af_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
#2 lore raw

Re: [PATCH 02/11] netconfig: Add DHCP override and static IP API

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

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

---
  ell/ell.sym     |   8 +
  ell/netconfig.c | 413 +++++++++++++++++++++++++++++++++++++++++++++++-
  ell/netconfig.h |  16 ++
  3 files changed, 432 insertions(+), 5 deletions(-)

<snip>

Quoted 6 lines
ell/netconfig.c +6 -0
+LIB_EXPORT bool l_netconfig_set_static_addr(struct l_netconfig *netconfig,
+					uint8_t family,
+					const struct l_rtnl_address *addr)
+{
+	if (unlikely(!netconfig || netconfig->started))
+		return false;

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 424

Quoted 10 lines
ell/netconfig.c +10 -0
+
+	if (addr && l_rtnl_address_get_family(addr) != family)
+		return false;
+
+	switch (family) {
+	case AF_INET:
+		l_rtnl_address_free(l_steal_ptr(netconfig->v4_static_addr));
+
+		if (addr)
+			break;

I take it this should be !addr?

Quoted 7 lines
ell/netconfig.c +7 -0
+
+		netconfig->v4_static_addr = l_rtnl_address_clone(addr);
+		l_rtnl_address_set_lifetimes(netconfig->v4_static_addr, 0, 0);
+
+		/*
+		 * We could leave the decision about this flag up to the
+		 * caller but for simplicity override to true.

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 441

Quoted 10 lines
ell/netconfig.c +10 -0
+		 */
+		l_rtnl_address_set_noprefixroute(netconfig->v4_static_addr,
+							true);
+		return true;
+	case AF_INET6:
+		l_rtnl_address_free(l_steal_ptr(netconfig->v6_static_addr));
+
+		if (addr)
+			break;
+

And here as well?

Quoted 10 lines
ell/netconfig.c +10 -0
+		netconfig->v6_static_addr = l_rtnl_address_clone(addr);
+		l_rtnl_address_set_lifetimes(netconfig->v6_static_addr, 0, 0);
+		l_rtnl_address_set_noprefixroute(netconfig->v6_static_addr,
+							true);
+		return true;
+	}
+
+	return false;
+}
+

<snip>

Quoted 41 lines
ell/netconfig.c +41 -0
+static bool netconfig_check_v4_config(struct l_netconfig *netconfig)
+{
+	struct in_addr local;
+	struct in_addr gateway;
+	uint8_t prefix_len = 0;
+	unsigned int dns_num = 0;
+	_auto_(l_free) struct in_addr *dns_list = NULL;
+
+	if (!netconfig->v4_enabled)
+		return true;
+
+	if (netconfig->v4_static_addr) {
+		char str[INET_ADDRSTRLEN];
+
+		prefix_len = l_rtnl_address_get_prefix_length(
+						netconfig->v4_static_addr);
+		if (prefix_len > 30)
+			return false;
+
+		l_rtnl_address_get_address(netconfig->v4_static_addr, str);
+		inet_pton(AF_INET, str, &local);
+	}
+
+	if (netconfig->v4_gateway_override) {
+		if (inet_pton(AF_INET, netconfig->v4_gateway_override,
+				&gateway) != 1)
+			return false;
+	}
+
+	if (netconfig->v4_dns_override &&
+			(dns_num = l_strv_length(netconfig->v4_dns_override))) {
+		unsigned int i;
+
+		dns_list = l_new(struct in_addr, dns_num);
+
+		for (i = 0; i < dns_num; i++)
+			if (inet_pton(AF_INET, netconfig->v4_dns_override[i],
+					&dns_list[i]) != 1)
+				return false;
+	}
+

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 588

Quoted 10 lines
ell/netconfig.c +10 -0
+	/*
+	 * If using a static IP, we can validate right now that the gateway is
+	 * in the local subnet.
+	 */
+	if (netconfig->v4_static_addr && netconfig->v4_gateway_override)
+		if (!l_net_subnet_matches(&local, &gateway, prefix_len))
+			return false;
+
+	/*
+	 * If using a static IP and there's no gateway all the DNSes mut be on

typo: mut -> must

Quoted 53 lines
ell/netconfig.c +53 -0
+	 * the local subnet too.
+	 */
+	if (netconfig->v4_static_addr && !netconfig->v4_gateway_override &&
+			netconfig->v4_dns_override) {
+		unsigned int i;
+
+		for (i = 0; i < dns_num; i++)
+			if (!l_net_subnet_matches(&local, &dns_list[i],
+							prefix_len))
+				return false;
+	}
+
+	return true;
+}
+
+static bool netconfig_check_v6_config(struct l_netconfig *netconfig)
+{
+	struct in_addr local;
+	struct in_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 (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 (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)

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 651

Quoted 10 lines
ell/netconfig.c +10 -0
+				return false;
+	}
+
+	/*
+	 * If using a static IP, we can validate right now that the gateway is
+	 * in the local subnet.
+	 */
+	if (netconfig->v6_static_addr && netconfig->v6_gateway_override)
+		if (!l_net_subnet_matches(&local, &gateway, prefix_len))
+			return false;

Not sure this is true? AFAIK the default gateway could be a link local address...

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 588

Quoted 3 lines
ell/netconfig.c +3 -0
+
+	/*
+	 * If using a static IP and there's no gateway all the DNSes mut be on

Same typo as above.

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 664

Quoted 10 lines
ell/netconfig.c +10 -0
+	 * the local subnet too.
+	 */
+	if (netconfig->v6_static_addr && !netconfig->v6_gateway_override &&
+			netconfig->v6_dns_override) {
+		unsigned int i;
+
+		for (i = 0; i < dns_num; i++)
+			if (!l_net_subnet_matches(&local, &dns_list[i],
+							prefix_len))
+				return false;

This whole block maybe should be a static convenience function? In fact, much of these checks are the same as in v4 version of this function and could be combined?

Quoted 5 lines
ell/netconfig.c +5 -0
+	}
+
+	return true;
+}
+

<snip>

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 719

Quoted 10 lines
  LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
  {
  	if (unlikely(!netconfig || netconfig->started))
  		return false;
ell/netconfig.c +1 -2
-	if (netconfig->v4_enabled &&
-			!l_dhcp_client_start(netconfig->dhcp_client))
+	if (!netconfig_check_config(netconfig))
  		return false;

Can we check that both v4_enabled and v6_enabled are false and return appropriately?

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 721

Quoted 2 lines
ell/netconfig.c +1 -1
-	netconfig->started = true;
+	if (netconfig->v4_enabled) {

I'd prefer less nesting, so something like:

if (!netconfig->v4_enabled)

	goto configure_ipv6;

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 729

Quoted 8 lines
ell/netconfig.c +8 -0
+		if (netconfig->v4_static_addr) {
+			/*
+			 * 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);

goto here..

Quoted 10 lines
ell/netconfig.c +6 -0
+		} else {
+			if (!l_dhcp_client_start(netconfig->dhcp_client))
+				return false;
+		}
+	}
 
+	netconfig->started = true;
  	return true;
  }

<snip>

ell/netconfig.h typedef void (*l_netconfig_destroy_cb_t)(void *user_data); · line 57

Quoted 10 lines
ell/netconfig.h +2 -0
index 682f4aa..aab9d3f 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_af_enabled(struct l_netconfig *netconfig, uint8_t family,
+				bool enabled);

Can we call it family_enabled?

ell/netconfig.h typedef void (*l_netconfig_destroy_cb_t)(void *user_data); · line 66

Quoted 5 lines
ell/netconfig.h +5 -0
+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);

Wonder why you use l_rtnl_address for 'set_static_addr' and a string for the gateway?

Regards,
-Denis

author
#3 lore raw

Re: [PATCH 02/11] netconfig: Add DHCP override and static IP API

Quoted 9 lines · Denis Kenzior

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 424

Quoted 6 lines · Andrew Zaborowski
ell/netconfig.c +6 -0
+     switch (family) {
+     case AF_INET:
+             l_rtnl_address_free(l_steal_ptr(netconfig->v4_static_addr));
+
+             if (addr)
+                     break;

I take it this should be !addr?

Yep.

Quoted 12 lines
Quoted 12 lines
ell/netconfig.c +11 -0
+
+             netconfig->v4_static_addr = l_rtnl_address_clone(addr);
+             l_rtnl_address_set_lifetimes(netconfig->v4_static_addr, 0, 0);
+
+             /*
+              * We could leave the decision about this flag up to the
+              * caller but for simplicity override to true.
+              */
+             l_rtnl_address_set_noprefixroute(netconfig->v4_static_addr,
+                                                     true);
+             return true;
Quoted 8 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 441

Quoted 6 lines
ell/netconfig.c +6 -0
+     case AF_INET6:
+             l_rtnl_address_free(l_steal_ptr(netconfig->v6_static_addr));
+
+             if (addr)
+                     break;
+

And here as well?

Yep.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+     /*
+      * If using a static IP and there's no gateway all the DNSes mut be on

typo: mut -> must

Right.

Quoted 10 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 651

Quoted 8 lines
ell/netconfig.c +7 -0
+     /*
+      * If using a static IP, we can validate right now that the gateway is
+      * in the local subnet.
+      */
+     if (netconfig->v6_static_addr && netconfig->v6_gateway_override)
+             if (!l_net_subnet_matches(&local, &gateway, prefix_len))
+                     return false;

Not sure this is true? AFAIK the default gateway could be a link local address...

True, that's also a good catch. I'll add a check for that (unless you prefer we don't validate this address.)

Quoted 18 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 588

Quoted 4 lines
ell/netconfig.c +3 -0
+
+     /*
+      * If using a static IP and there's no gateway all the DNSes mut be on

Same typo as above.

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 664

Quoted 10 lines
ell/netconfig.c +10 -0
+      * the local subnet too.
+      */
+     if (netconfig->v6_static_addr && !netconfig->v6_gateway_override &&
+                     netconfig->v6_dns_override) {
+             unsigned int i;
+
+             for (i = 0; i < dns_num; i++)
+                     if (!l_net_subnet_matches(&local, &dns_list[i],
+                                                     prefix_len))
+                             return false;
Quoted 3 lines

This whole block maybe should be a static convenience function? In fact, much of these checks are the same as in v4 version of this function and could be combined?

Ok, let me try to combine them.

Quoted 14 lines
Quoted 6 lines
ell/netconfig.c +5 -0
+     }
+
+     return true;
+}
+

<snip>

Quoted 5 lines
  LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
  {
      if (unlikely(!netconfig || netconfig->started))
              return false;
Quoted 7 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 719

Quoted 5 lines
ell/netconfig.c +1 -2
-     if (netconfig->v4_enabled &&
-                     !l_dhcp_client_start(netconfig->dhcp_client))
+     if (!netconfig_check_config(netconfig))
              return false;

Can we check that both v4_enabled and v6_enabled are false and return appropriately?

Ok. We effectively do but let me add one at the beginning.

Quoted 8 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 721

Quoted 3 lines
ell/netconfig.c +1 -1
-     netconfig->started = true;
+     if (netconfig->v4_enabled) {

I'd prefer less nesting, so something like:

if (!netconfig->v4_enabled)

        goto configure_ipv6;

Ok.

Quoted 31 lines

ell/netconfig.c LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig) · line 729

Quoted 9 lines
ell/netconfig.c +8 -0
+             if (netconfig->v4_static_addr) {
+                     /*
+                      * 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);

goto here..

Quoted 10 lines
ell/netconfig.c +6 -0
+             } else {
+                     if (!l_dhcp_client_start(netconfig->dhcp_client))
+                             return false;
+             }
+     }
 
+     netconfig->started = true;
      return true;
  }

<snip>

Quoted 6 lines
ell/netconfig.h +0 -0
index 682f4aa..aab9d3f 100644
@@ -53,6 +53,22 @@ typedef void (*l_netconfig_destroy_cb_t)(void *user_data);
 
Quoted 6 lines

ell/netconfig.h typedef void (*l_netconfig_destroy_cb_t)(void *user_data); · line 57

Quoted 4 lines
ell/netconfig.h +2 -0
  struct l_netconfig *l_netconfig_new(uint32_t ifindex);
  void l_netconfig_destroy(struct l_netconfig *netconfig);
+bool l_netconfig_set_af_enabled(struct l_netconfig *netconfig, uint8_t family,
+                             bool enabled);

Can we call it family_enabled?

Sure.

Quoted 9 lines

ell/netconfig.h typedef void (*l_netconfig_destroy_cb_t)(void *user_data); · line 66

Quoted 6 lines
ell/netconfig.h +5 -0
+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);

Wonder why you use l_rtnl_address for 'set_static_addr' and a string for the gateway?

So l_rtnl_address includes the prefix length and such, so the struct maps to our own addresses. The gateway would map to the next-hop address in the l_rtnl_route struct so it didn't seem fitting. Note that l_rtnl_route doesn't use l_rtnl_address for that in its API.

Best regards

author
#4 lore raw

PATCH 3/11 rtnl: Add l_rtnl_route_new_static

In reply to Andrew Zaborowski

Add a constructor for prefix routes through a specific gateway/router.
---
Name follows suggestion in
https://lists.01.org/hyperkitty/list/ell(a)lists.01.org/message/ZPJRBKRROCGZFVLLFUCL6GYHHG2JQZLW/

---
 ell/ell.sym |  1 +
 ell/rtnl.c  | 31 +++++++++++++++++++++++++++++++
 ell/rtnl.h  |  2 ++
 3 files changed, 34 insertions(+)
ell/ell.sym +1 -0
index ec995fc..3816e3f 100644
@@ -639,6 +639,7 @@ global:
 	l_rtnl_address_set_scope;
 	l_rtnl_route_new_gateway;
 	l_rtnl_route_new_prefix;
+	l_rtnl_route_new_static;
 	l_rtnl_route_free;
 	l_rtnl_route_get_family;
 	l_rtnl_route_get_gateway;
ell/rtnl.c +31 -0
index afbff9d..dbaf97b 100644
@@ -419,6 +419,37 @@ LIB_EXPORT struct l_rtnl_route *l_rtnl_route_new_prefix(const char *ip,
 	return rt;
 }
 
+LIB_EXPORT struct l_rtnl_route *l_rtnl_route_new_static(const char *gw,
+							const char *ip,
+							uint8_t prefix_len)
+{
+	struct in_addr gw_addr4;
+	struct in6_addr gw_addr6;
+	struct in_addr dst_addr4;
+	struct in6_addr dst_addr6;
+	int family;
+	struct l_rtnl_route *rt;
+
+	if ((family = address_get(gw, &gw_addr4, &gw_addr6)) < 0)
+		return NULL;
+
+	if (address_get(ip, &dst_addr4, &dst_addr6) != family)
+		return NULL;
+
+	if (prefix_len == 0 || prefix_len > (family == AF_INET ? 32 : 128))
+		return NULL;
+
+	rt = l_rtnl_route_new_gateway(gw);
+	rt->dst_prefix_len = prefix_len;
+
+	if (family == AF_INET6)
+		memcpy(&rt->dst.in6_addr, &dst_addr6, sizeof(dst_addr6));
+	else
+		memcpy(&rt->dst.in_addr, &dst_addr4, sizeof(dst_addr4));
+
+	return rt;
+}
+
 LIB_EXPORT void l_rtnl_route_free(struct l_rtnl_route *rt)
 {
 	l_free(rt);
ell/rtnl.h +2 -0
index bbd410e..55c4997 100644
@@ -71,6 +71,8 @@ bool l_rtnl_address_set_scope(struct l_rtnl_address *addr, uint8_t scope);
 struct l_rtnl_route *l_rtnl_route_new_gateway(const char *gw);
 struct l_rtnl_route *l_rtnl_route_new_prefix(const char *ip,
 							uint8_t prefix_len);
+struct l_rtnl_route *l_rtnl_route_new_static(const char *gw, const char *ip,
+							uint8_t prefix_len);
 void l_rtnl_route_free(struct l_rtnl_route *rt);
 DEFINE_CLEANUP_FUNC(l_rtnl_route_free);
 uint8_t l_rtnl_route_get_family(const struct l_rtnl_route *rt);
Signature · 1 line
2.32.0
Applied
#5 lore raw

Re: [PATCH 03/11] rtnl: Add l_rtnl_route_new_static

Hi Andrew,

Quoted 10 lines · Andrew Zaborowski

Add a constructor for prefix routes through a specific gateway/router.
---
Name follows suggestion in
https://lists.01.org/hyperkitty/list/ell(a)lists.01.org/message/ZPJRBKRROCGZFVLLFUCL6GYHHG2JQZLW/

---
  ell/ell.sym |  1 +
  ell/rtnl.c  | 31 +++++++++++++++++++++++++++++++
  ell/rtnl.h  |  2 ++
  3 files changed, 34 insertions(+)

Applied, thanks.

Regards,
-Denis

author
#6 lore raw

PATCH 4/11 rtnl: Add l_rtnl_route getters for raw address data

In reply to Andrew Zaborowski

Add l_rtnl_route_get_gateway_in_addr and l_rtnl_route_get_dst_in_addr for raw struct in_addr / struct in6_addr addresses, as opposed to string representations. This is to be used to avoid unneeded string conversions in both directions.

---
 ell/ell.sym |  2 ++
 ell/rtnl.c  | 30 ++++++++++++++++++++++++++++++
 ell/rtnl.h  |  3 +++
 3 files changed, 35 insertions(+)
ell/ell.sym +2 -0
index 3816e3f..9b3a6e1 100644
@@ -643,7 +643,9 @@ global:
 	l_rtnl_route_free;
 	l_rtnl_route_get_family;
 	l_rtnl_route_get_gateway;
+	l_rtnl_route_get_gateway_in_addr;
 	l_rtnl_route_get_dst;
+	l_rtnl_route_get_dst_in_addr;
 	l_rtnl_route_get_lifetime;
 	l_rtnl_route_set_lifetime;
 	l_rtnl_route_get_expiry;
ell/rtnl.c +30 -0
index dbaf97b..cbe9b87 100644
@@ -473,6 +473,21 @@ LIB_EXPORT bool l_rtnl_route_get_gateway(const struct l_rtnl_route *rt,
 					out_buf);
 }
 
+LIB_EXPORT const void *l_rtnl_route_get_gateway_in_addr(
+						const struct l_rtnl_route *rt)
+{
+	if (unlikely(!rt))
+		return NULL;
+
+	if (address_is_null(rt->family, &rt->gw.in_addr, &rt->gw.in6_addr))
+		return NULL;
+
+	if (rt->family == AF_INET)
+		return &rt->gw.in_addr;
+	else
+		return &rt->gw.in6_addr;
+}
+
 LIB_EXPORT bool l_rtnl_route_get_dst(const struct l_rtnl_route *rt,
 						char *out_buf,
 						uint8_t *out_prefix_len)
@@ -488,6 +503,21 @@ LIB_EXPORT bool l_rtnl_route_get_dst(const struct l_rtnl_route *rt,
 	return true;
 }
 
+LIB_EXPORT const void *l_rtnl_route_get_dst_in_addr(
+						const struct l_rtnl_route *rt,
+						uint8_t *out_prefix_len)
+{
+	if (unlikely(!rt))
+		return NULL;
+
+	*out_prefix_len = rt->dst_prefix_len;
+
+	if (rt->family == AF_INET)
+		return &rt->dst.in_addr;
+	else
+		return &rt->dst.in6_addr;
+}
+
 LIB_EXPORT uint32_t l_rtnl_route_get_lifetime(const struct l_rtnl_route *rt)
 {
 	if (unlikely(!rt))
ell/rtnl.h +3 -0
index 55c4997..2617b1c 100644
@@ -77,8 +77,11 @@ void l_rtnl_route_free(struct l_rtnl_route *rt);
 DEFINE_CLEANUP_FUNC(l_rtnl_route_free);
 uint8_t l_rtnl_route_get_family(const struct l_rtnl_route *rt);
 bool l_rtnl_route_get_gateway(const struct l_rtnl_route *rt, char *out_buf);
+const void *l_rtnl_route_get_gateway_in_addr(const struct l_rtnl_route *rt);
 bool l_rtnl_route_get_dst(const struct l_rtnl_route *rt, char *out_buf,
 				uint8_t *out_prefix_len);
+const void *l_rtnl_route_get_dst_in_addr(const struct l_rtnl_route *rt,
+						uint8_t *out_prefix_len);
 uint32_t l_rtnl_route_get_lifetime(const struct l_rtnl_route *rt);
 bool l_rtnl_route_set_lifetime(struct l_rtnl_route *rt, uint32_t lt);
 uint64_t l_rtnl_route_get_expiry(const struct l_rtnl_route *rt);
Signature · 1 line
2.32.0
Applied
#7 lore raw

Re: [PATCH 04/11] rtnl: Add l_rtnl_route getters for raw address data

Hi Andrew,

Quoted 10 lines · Andrew Zaborowski

Add l_rtnl_route_get_gateway_in_addr and l_rtnl_route_get_dst_in_addr for raw struct in_addr / struct in6_addr addresses, as opposed to string representations. This is to be used to avoid unneeded string conversions in both directions.

---
  ell/ell.sym |  2 ++
  ell/rtnl.c  | 30 ++++++++++++++++++++++++++++++
  ell/rtnl.h  |  3 +++
  3 files changed, 35 insertions(+)

Applied, thanks.

Regards,
-Denis

author
#8 lore raw

PATCH 5/11 rtnl: Minor cleanup

In reply to Andrew Zaborowski

In l_rtnl_route_get_gateway return NULL if route is for an onlink prefix (no gateway). Remove empty lines and use L_IN_SET in one instance.

---
 ell/rtnl.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
ell/rtnl.c +5 -5
index cbe9b87..58f2eb1 100644
@@ -469,6 +469,9 @@ LIB_EXPORT bool l_rtnl_route_get_gateway(const struct l_rtnl_route *rt,
 	if (unlikely(!rt))
 		return false;
 
+	if (address_is_null(rt->family, &rt->gw.in_addr, &rt->gw.in6_addr))
+		return false;
+
 	return !address_to_string(rt->family, &rt->gw.in_addr, &rt->gw.in6_addr,
 					out_buf);
 }
@@ -583,9 +586,8 @@ LIB_EXPORT bool l_rtnl_route_set_preference(struct l_rtnl_route *rt,
 	if (unlikely(!rt))
 		return false;
 
-	if (preference != ICMPV6_ROUTER_PREF_LOW &&
-			preference != ICMPV6_ROUTER_PREF_HIGH &&
-			preference != ICMPV6_ROUTER_PREF_MEDIUM)
+	if (!L_IN_SET(preference, ICMPV6_ROUTER_PREF_LOW,
+			ICMPV6_ROUTER_PREF_HIGH, ICMPV6_ROUTER_PREF_MEDIUM))
 		return false;
 
 	rt->preference = preference;
@@ -602,7 +604,6 @@ LIB_EXPORT bool l_rtnl_route_get_prefsrc(const struct l_rtnl_route *rt,
 					&rt->prefsrc.in6_addr))
 		return false;
 
-
 	return !address_to_string(rt->family, &rt->prefsrc.in_addr,
 						&rt->prefsrc.in6_addr,
 						out_address);
@@ -1079,7 +1080,6 @@ LIB_EXPORT void l_rtnl_ifaddr6_extract(const struct ifaddrmsg *ifa, int len,
 
 			if (!inet_ntop(AF_INET6, &in6_addr, address,
 							INET6_ADDRSTRLEN)) {
-
 				l_error("rtnl: Failed to extract IPv6 address");
 				break;
 			}
Signature · 1 line
2.32.0
Applied
#9 lore raw

Re: [PATCH 05/11] rtnl: Minor cleanup

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

In l_rtnl_route_get_gateway return NULL if route is for an onlink prefix (no gateway). Remove empty lines and use L_IN_SET in one instance.

---
  ell/rtnl.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)

Applied, thanks.

Regards,
-Denis

author
#10 lore raw

PATCH 6/11 icmp6: Parse extra options

In reply to Andrew Zaborowski

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         | 121 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 113 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 +109 -12

190 lines not on this page · raw message

Signature · 1 line
2.32.0
#11 lore raw

Re: [PATCH 06/11] icmp6: Parse extra options

Hi Andrew,

Quoted 8 lines · Andrew Zaborowski

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         | 121 +++++++++++++++++++++++++++++++++++++++-----
  2 files changed, 113 insertions(+), 14 deletions(-)

<snip>

Quoted 11 lines
ell/icmp6.c +6 -1
@@ -719,7 +732,35 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
  				return NULL;
 
  			if (opts[3] & ND_OPT_PI_FLAG_ONLINK)
-				n_prefixes += 1;
+				n_routes += 1;
+			break;
+		case ND_OPT_ROUTE_INFORMATION:
+			if (l < 8)
+				return NULL;
+

ell/icmp6.c struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, · line 750

Quoted 10 lines
ell/icmp6.c +10 -0
+			if (opts[2] > 128 || opts[2] > (l - 8) * 8)
+				return NULL;
+
+			/*
+			 * RFC 4191 Section 2.3:
+			 * "If the Reserved (10) value is received, the Route
+			 * Information Option MUST be ignored."
+			 */
+			if (((opts[3] >> 3) & 3) == 2)
+				break;

Can we use bit_field() from useful.h instead?

Quoted 17 lines
ell/icmp6.c +13 -0
+
+			/*
+			 * RFC 4191 Section 3.1:
+			 * "The Router Preference and Lifetime values in a ::/0
+			 * Route Information Option override the preference and
+			 * lifetime values in the Router Advertisement header."
+			 *
+			 * Don't count ::/0 routes.
+			 */
+			if (opts[2] == 0)
+				break;
+
+			n_routes += 1;
  			break;
  		}
 

ell/icmp6.c struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, · line 786

Quoted 7 lines
ell/icmp6.c +4 -0
@@ -739,6 +780,10 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
  		r->other = true;
 
  	r->pref = (ra->nd_ra_flags_reserved >> 3) & 0x3;
+	/*
+	 * "If the Reserved (10) value is received, the receiver MUST
+	 * treat the value as if it were (00).
+	 */

This might warrant a separate commit

Quoted 3 lines
ell/icmp6.c +0 -0
  	if (r->pref == 0x2) /* If invalid, reset to medium */
  		r->pref = 0;
 

<snip>

Quoted 21 lines
ell/icmp6.c +6 -2
@@ -765,17 +810,69 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
  			break;
  		case ND_OPT_PREFIX_INFORMATION:
  		{
-			struct route_info *i = &r->prefixes[n_prefixes];
+			struct route_info *i = &r->routes[n_routes];
 
  			if (!(opts[3] & ND_OPT_PI_FLAG_ONLINK))
  				break;
 
  			i->prefix_len = opts[2];
+			i->onlink = true;
  			i->valid_lifetime = l_get_be32(opts + 4);
  			i->preferred_lifetime = l_get_be32(opts + 8);
  			memcpy(i->address, opts + 16, 16);
 
-			n_prefixes += 1;
+			n_routes += 1;
+			break;
+		}
+		case ND_OPT_RTR_ADV_INTERVAL:

ell/icmp6.c struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, · line 837

Quoted 10 lines
ell/icmp6.c +10 -0
+			if (l < 8)
+				break;
+
+			r->max_rtr_adv_interval_ms = l_get_be32(opts + 4);
+			break;
+		case ND_OPT_ROUTE_INFORMATION:
+		{
+			struct route_info *i = &r->routes[n_routes++];
+
+			if (((opts[3] >> 3) & 3) == 2)

bit_field()?

Also, you access this value at least twice below, so maybe just use a variable and assign it.

Quoted 32 lines
ell/icmp6.c +31 -0
+				break;
+
+			/*
+			 * RFC 4191 Section 3.1:
+			 * "The Router Preference and Lifetime values in a ::/0
+			 * Route Information Option override the preference and
+			 * lifetime values in the Router Advertisement header."
+			 */
+			if (opts[2] == 0) {
+				if (r->lifetime == 0 && l_get_be32(opts + 4)) {
+					/*
+					 * A ::/0 route received from a
+					 * non-default router?  Should issue
+					 * a warning?
+					 */
+					break;
+				}
+
+				r->pref = (opts[3] >> 3) & 3;
+				r->lifetime = l_get_be16(opts + 4) ? 0xffff :
+					l_get_be16(opts + 6);
+				break;
+			}
+
+			/*
+			 * Don't check or warn if the route lifetime is longer
+			 * than the router lifetime because that refers to its
+			 * time as the default router.  It may be configured to
+			 * route packets for us for specific prefixes without
+			 * being a default router.
+			 */

ell/icmp6.c struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, · line 878

Quoted 10 lines
ell/icmp6.c +7 -0
+			i->prefix_len = opts[2];
+			i->onlink = false;
+			i->preference = (opts[3] >> 3) & 3;
+			i->valid_lifetime = l_get_be32(opts + 4);
+			memcpy(i->address, opts + 8, (i->prefix_len + 7) / 8);
+
+			n_routes += 1;
  			break;
  		}
  		}

Regards,
-Denis

author
#12 lore raw

Re: [PATCH 06/11] icmp6: Parse extra options

Hi Denis,

Quoted 5 lines · Denis Kenzior

Review

Quoted 2 lines · Andrew Zaborowski
(quoted diff) +2 -0
+                     if (((opts[3] >> 3) & 3) == 2)
+                             break;

Can we use bit_field() from useful.h instead?

Sure.

Quoted 20 lines
Quoted 20 lines
ell/icmp6.c +13 -0
+
+                     /*
+                      * RFC 4191 Section 3.1:
+                      * "The Router Preference and Lifetime values in a ::/0
+                      * Route Information Option override the preference and
+                      * lifetime values in the Router Advertisement header."
+                      *
+                      * Don't count ::/0 routes.
+                      */
+                     if (opts[2] == 0)
+                             break;
+
+                     n_routes += 1;
                      break;
              }
@@ -739,6 +780,10 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
              r->other = true;
 
Quoted 7 lines

ell/icmp6.c struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, · line 786

Quoted 5 lines
ell/icmp6.c +4 -0
      r->pref = (ra->nd_ra_flags_reserved >> 3) & 0x3;
+     /*
+      * "If the Reserved (10) value is received, the receiver MUST
+      * treat the value as if it were (00).
+      */

This might warrant a separate commit

Ok.

Quoted 41 lines
Quoted 4 lines
ell/icmp6.c +0 -0
      if (r->pref == 0x2) /* If invalid, reset to medium */
              r->pref = 0;
 

<snip>

Quoted 21 lines
ell/icmp6.c +6 -2
@@ -765,17 +810,69 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
                      break;
              case ND_OPT_PREFIX_INFORMATION:
              {
-                     struct route_info *i = &r->prefixes[n_prefixes];
+                     struct route_info *i = &r->routes[n_routes];
 
                      if (!(opts[3] & ND_OPT_PI_FLAG_ONLINK))
                              break;
 
                      i->prefix_len = opts[2];
+                     i->onlink = true;
                      i->valid_lifetime = l_get_be32(opts + 4);
                      i->preferred_lifetime = l_get_be32(opts + 8);
                      memcpy(i->address, opts + 16, 16);
 
-                     n_prefixes += 1;
+                     n_routes += 1;
+                     break;
+             }
+             case ND_OPT_RTR_ADV_INTERVAL:

ell/icmp6.c struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra, · line 837

Quoted 10 lines
ell/icmp6.c +10 -0
+                     if (l < 8)
+                             break;
+
+                     r->max_rtr_adv_interval_ms = l_get_be32(opts + 4);
+                     break;
+             case ND_OPT_ROUTE_INFORMATION:
+             {
+                     struct route_info *i = &r->routes[n_routes++];
+
+                     if (((opts[3] >> 3) & 3) == 2)

bit_field()?

Quoted 2 lines

Also, you access this value at least twice below, so maybe just use a variable and assign it.

Ok.

Best regards

author
#13 lore raw

PATCH 7/11 icmp6: Emit L_ICMP6_CLIENT_EVENT_ROUTER_FOUND for all RAs

In reply to Andrew Zaborowski

Emit an L_ICMP6_CLIENT_EVENT_ROUTER_FOUND event for ever Router Advertisement received instead of only the first one.

---
 ell/dhcp6.c | 10 +++++++---
 ell/icmp6.c | 17 ++++++++---------
 2 files changed, 15 insertions(+), 12 deletions(-)
ell/dhcp6.c +7 -3
index 424fad0..01d7eb3 100644
@@ -1473,9 +1473,6 @@ static void dhcp6_client_icmp6_event(struct l_icmp6_client *icmp6,
 {
 	struct l_dhcp6_client *client = user_data;
 
-	l_timeout_remove(client->timeout_send);
-	client->timeout_send = NULL;
-
 	switch (event) {
 	case L_ICMP6_CLIENT_EVENT_ROUTER_FOUND:
 	{
@@ -1488,6 +1485,13 @@ static void dhcp6_client_icmp6_event(struct l_icmp6_client *icmp6,
 				managed ? "yes" : "no",
 				other ? "yes" : "no");
 
+		/* We only process the first RA received for now */
+		if (!client->timeout_send)
+			return;
+
+		l_timeout_remove(client->timeout_send);
+		client->timeout_send = NULL;
+
 		if (!managed && !other) {
 			l_dhcp6_client_stop(client);
 			dhcp6_client_event_notify(client,
ell/icmp6.c +8 -9
index 4128f01..82ca997 100644
@@ -378,16 +378,16 @@ static int icmp6_client_handle_message(struct l_icmp6_client *client,
 	if (!r)
 		return -EBADMSG;
 
-	if (!client->ra) {
-		client->ra = r;
-		icmp6_client_event_notify(client,
+	icmp6_client_event_notify(client,
 					L_ICMP6_CLIENT_EVENT_ROUTER_FOUND,
-					NULL);
+					r);
 
-		/* DHCP6 client may have stopped us */
-		if (!client->ra)
-			return -ECANCELED;
+	/* DHCP6 client may have stopped us */
+	if (!client->io)
+		return -ECANCELED;
 
+	if (!client->ra) {
+		client->ra = r;
 		icmp6_client_setup_routes(client);
 		return 0;
 	}
@@ -396,8 +396,7 @@ static int icmp6_client_handle_message(struct l_icmp6_client *client,
 	 * TODO: Figure out if the RA has updated info and update routes
 	 * accordingly.
 	 */
-	_icmp6_router_free(client->ra);
-	client->ra = r;
+	_icmp6_router_free(r);
 	return 0;
 }
 
Signature · 1 line
2.32.0
Applied
#14 lore raw

Re: [PATCH 07/11] icmp6: Emit L_ICMP6_CLIENT_EVENT_ROUTER_FOUND for all RAs

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

Emit an L_ICMP6_CLIENT_EVENT_ROUTER_FOUND event for ever Router Advertisement received instead of only the first one.

---
  ell/dhcp6.c | 10 +++++++---
  ell/icmp6.c | 17 ++++++++---------
  2 files changed, 15 insertions(+), 12 deletions(-)

Applied, thanks.

Regards,
-Denis

author
#15 lore raw

PATCH 8/11 netconfig: Support IPv6 static configurations

In reply to Andrew Zaborowski
---
 ell/netconfig.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 90 insertions(+), 2 deletions(-)
ell/netconfig.c +90 -2
index d0f87b3..6641398 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 =
@@ -683,7 +729,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;
@@ -697,6 +743,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;
@@ -704,10 +773,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)
@@ -733,6 +808,18 @@ LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
 		}
 	}
 
+	if (netconfig->v6_enabled) {
+		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);
+		}
+	}
+
 	netconfig->started = true;
 	return true;
 }
@@ -755,6 +842,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
#16 lore raw

PATCH 9/11 netconfig: Exit on errors in netconfig_dhcp_event_handler

In reply to Andrew Zaborowski

Don't just warn on errors that could cause a crash later in the function even though in theory they can't happen.

---
 ell/netconfig.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
ell/netconfig.c +12 -6
index 6641398..030b2e1 100644
@@ -292,28 +292,34 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
 
 	switch (event) {
 	case L_DHCP_CLIENT_EVENT_IP_CHANGED:
-		L_WARN_ON(!nc->v4_configured);
+		if (L_WARN_ON(!nc->v4_configured))
+			break;
+
 		netconfig_remove_dhcp_address_routes(nc);
 		netconfig_add_dhcp_address_routes(nc);
 		netconfig_set_dhcp_lifetimes(nc, false);
 		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_UPDATE);
 		break;
 	case L_DHCP_CLIENT_EVENT_LEASE_OBTAINED:
-	{
-		L_WARN_ON(nc->v4_configured);
+		if (L_WARN_ON(nc->v4_configured))
+			break;
+
 		netconfig_add_dhcp_address_routes(nc);
 		netconfig_set_dhcp_lifetimes(nc, false);
 		nc->v4_configured = true;
 		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_CONFIGURE);
 		break;
-	}
 	case L_DHCP_CLIENT_EVENT_LEASE_RENEWED:
-		L_WARN_ON(!nc->v4_configured);
+		if (L_WARN_ON(!nc->v4_configured))
+			break;
+
 		netconfig_set_dhcp_lifetimes(nc, true);
 		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_UPDATE);
 		break;
 	case L_DHCP_CLIENT_EVENT_LEASE_EXPIRED:
-		L_WARN_ON(!nc->v4_configured);
+		if (L_WARN_ON(!nc->v4_configured))
+			break;
+
 		netconfig_remove_dhcp_address_routes(nc);
 		nc->v4_configured = false;
 
Signature · 1 line
2.32.0
Applied
#17 lore raw

Re: [PATCH 09/11] netconfig: Exit on errors in netconfig_dhcp_event_handler

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

Don't just warn on errors that could cause a crash later in the function even though in theory they can't happen.

---
  ell/netconfig.c | 18 ++++++++++++------
  1 file changed, 12 insertions(+), 6 deletions(-)

Applied, thanks.

Regards,
-Denis

author
#18 lore raw

PATCH 10/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 | 202 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 201 insertions(+), 1 deletion(-)
ell/netconfig.c +201 -1
index 030b2e1..15eb4ed 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,188 @@ 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)
+			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 +561,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 +588,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);
@@ -815,7 +1011,10 @@ LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
 	}
 
 	if (netconfig->v6_enabled) {
-		if (netconfig->v6_static_addr && !netconfig->do_static_work) {
+		if (!netconfig->v6_static_addr) {
+			if (!l_dhcp6_client_start(netconfig->dhcp6_client))
+				return false;
+		} else if (!netconfig->do_static_work) {
 			/*
 			 * We're basically ready to configure the interface
 			 * but do this in an idle callback.
@@ -851,6 +1050,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
#19 lore raw

PATCH 11/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 9b3a6e1..f35bf82 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 15eb4ed..72495ec 100644
@@ -1062,6 +1062,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 aab9d3f..dc56bcb 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
Applied
#20 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 24 lines · Andrew Zaborowski

Add inline function to check if two IP addresses are in the same subnet.

---
  ell/net.h | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
ell/net.h +4 -0
index c97d9c9..837209c 100644
@@ -25,6 +25,7 @@
 
  #include <stdbool.h>
  #include <stdint.h>
+#include <string.h>
 
  struct in_addr;
  struct in6_addr;
@@ -40,6 +41,26 @@ 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,
+					uint8_t prefix_len)
+{

ell/net.h bool l_net_hostname_is_localhost(const char *hostname); · line 56

Quoted 10 lines
ell/net.h +10 -0
+	uint8_t bytes = prefix_len / 8;
+	uint8_t bits = prefix_len & 7;
+
+	/*
+	 * @a and @b are network byte order IPv4 or IPv6 addresses.
+	 * We want to check if the initial (top) @prefix_len bits match.
+	 * memcmp the whole bytes, then compare the final byte's top
+	 * bits by anding with a mask.
+	 */
+	if (bytes && memcmp(a, b, bytes))

It looks like memcmp with a zero length is indeed guaranteed to return 0. So this was simplified to if (memcmp(..))

ell/net.h bool l_net_hostname_is_localhost(const char *hostname); · line 61

Quoted 5 lines
ell/net.h +5 -0
+		return false;
+
+	return !bits ||
+		((((const uint8_t *) a)[bytes] ^ ((const uint8_t *) b)[bytes]) &
+		 (0xff00u >> bits)) == 0;

There were a bit too many '()' for my liking. I moved the casts to the variable declaration block above which ended up being the same number of lines and looked a bit cleaner. Please double check that I didn't screw anything up.

ell/net.h bool l_net_hostname_is_localhost(const char *hostname); · line 66

Quoted 6 lines
ell/net.h +2 -0
+}
+
  #ifdef __cplusplus
  }
  #endif

Applied, thanks.

Regards,
-Denis

author
#21 lore raw

Hi Denis,

Quoted 7 lines · Denis Kenzior

ell/net.h bool l_net_hostname_is_localhost(const char *hostname); · line 61

Quoted 3 lines
ell/net.h +3 -0
+     return !bits ||
+             ((((const uint8_t *) a)[bytes] ^ ((const uint8_t *) b)[bytes]) &
+              (0xff00u >> bits)) == 0;

There were a bit too many '()' for my liking. I moved the casts to the variable declaration block above which ended up being the same number of lines and looked a bit cleaner.

Do we want to do the array[bytes] access after the !bits check to avoid accessing the byte after the end of the address for weird cases where the prefix_len is exactly the number of bits in the two buffers we received.

Best regards

#22 lore raw

Hi Andrew,

Quoted 7 lines · Andrew Zaborowski

Hi Denis,

Quoted 4 lines · Denis Kenzior
Quoted 3 lines
ell/net.h +3 -0
+     return !bits ||
+             ((((const uint8_t *) a)[bytes] ^ ((const uint8_t *) b)[bytes]) &
+              (0xff00u >> bits)) == 0;
Quoted 8 lines
Quoted 3 lines

There were a bit too many '()' for my liking. I moved the casts to the variable declaration block above which ended up being the same number of lines and looked a bit cleaner.

Do we want to do the array[bytes] access after the !bits check to avoid accessing the byte after the end of the address for weird cases where the prefix_len is exactly the number of bits in the two buffers we received.

Aren't we guaranteed to be operating on 4 byte values? So unless prefix_len == 32, I don't see how we could perform out-of-bounds access?

Regards,
-Denis

author
#23 lore raw
Quoted 5 lines · Denis Kenzior
Quoted 4 lines · Andrew Zaborowski
Quoted 3 lines

There were a bit too many '()' for my liking. I moved the casts to the variable declaration block above which ended up being the same number of lines and looked a bit cleaner.

Quoted 7 lines
Quoted 4 lines

Do we want to do the array[bytes] access after the !bits check to avoid accessing the byte after the end of the address for weird cases where the prefix_len is exactly the number of bits in the two buffers we received.

Aren't we guaranteed to be operating on 4 byte values? So unless prefix_len == 32, I don't see how we could perform out-of-bounds access?

4 or 16 bytes, in theory yes but I can imagine someone trying to use a buffer of just long enough for the subnet address, or passing a prefix_len of 32 / 128 for things like a route prefix.

Best regards

#24 lore raw

Hi Andrew,

Quoted 11 lines · Andrew Zaborowski
Quoted 10 lines · Denis Kenzior
Quoted 8 lines · Andrew Zaborowski
Quoted 3 lines

There were a bit too many '()' for my liking. I moved the casts to the variable declaration block above which ended up being the same number of lines and looked a bit cleaner.

Do we want to do the array[bytes] access after the !bits check to avoid accessing the byte after the end of the address for weird cases where the prefix_len is exactly the number of bits in the two buffers we received.

Quoted 4 lines
Quoted 2 lines

Aren't we guaranteed to be operating on 4 byte values? So unless prefix_len == 32, I don't see how we could perform out-of-bounds access?

4 or 16 bytes, in theory yes but I can imagine someone trying to use a

IPv6 doesn't really use subnets like IPv4 does though? I'm not even sure this function would be relevant for IPv6? Is it? Looks like we do this for FILS, but I'm not even certain that the check in ie.c in iwd is correct.

Quoted 2 lines

buffer of just long enough for the subnet address, or passing a prefix_len of 32 / 128 for things like a route prefix.

Well, prefix_len of 32/128 implies point-to-point routing (and even 31 in IPv4 would be special). So I'm not sure calling this function with such prefixes even makes sense?

Regards,
-Denis

author
#25 lore raw

Hi Denis,

Quoted 8 lines · Denis Kenzior
Quoted 7 lines · Andrew Zaborowski
Quoted 6 lines · Denis Kenzior
Quoted 4 lines · Andrew Zaborowski

Do we want to do the array[bytes] access after the !bits check to avoid accessing the byte after the end of the address for weird cases where the prefix_len is exactly the number of bits in the two buffers we received.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Aren't we guaranteed to be operating on 4 byte values? So unless prefix_len == 32, I don't see how we could perform out-of-bounds access?

4 or 16 bytes, in theory yes but I can imagine someone trying to use a

IPv6 doesn't really use subnets like IPv4 does though? I'm not even sure this function would be relevant for IPv6?

You need it to check if an address is within a subnet prefix?

Quoted 9 lines

Is it? Looks like we do this for FILS,
but I'm not even certain that the check in ie.c in iwd is correct.

Quoted 2 lines

buffer of just long enough for the subnet address, or passing a prefix_len of 32 / 128 for things like a route prefix.

Well, prefix_len of 32/128 implies point-to-point routing (and even 31 in IPv4 would be special). So I'm not sure calling this function with such prefixes even makes sense?

While an IPv4 31/32-bit subnet might not be a valid subnet, with IPv6 I don't know of a reason that single-address prefixes should be treated specially. RFC4191 for example says that an onlink prefix length "value ranges from 0 to 128".

Best regards

#26 lore raw

Hi Andrew,

Quoted 14 lines · Andrew Zaborowski

Hi Denis,

Quoted 11 lines · Denis Kenzior
Quoted 10 lines · Andrew Zaborowski
Quoted 8 lines · Denis Kenzior
Quoted 4 lines · Andrew Zaborowski

Do we want to do the array[bytes] access after the !bits check to avoid accessing the byte after the end of the address for weird cases where the prefix_len is exactly the number of bits in the two buffers we received.

Aren't we guaranteed to be operating on 4 byte values? So unless prefix_len == 32, I don't see how we could perform out-of-bounds access?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

4 or 16 bytes, in theory yes but I can imagine someone trying to use a

IPv6 doesn't really use subnets like IPv4 does though? I'm not even sure this function would be relevant for IPv6?

You need it to check if an address is within a subnet prefix?

IPv6 doesn't work the same as IPv4 though. I'm fairly certain that the gateway and the static address do not need to be part of the same subnet at all.

Quoted 6 lines
Quoted 6 lines

Is it? Looks like we do this for FILS,
but I'm not even certain that the check in ie.c in iwd is correct.

Quoted 2 lines

buffer of just long enough for the subnet address, or passing a prefix_len of 32 / 128 for things like a route prefix.

Quoted 8 lines
Quoted 3 lines

Well, prefix_len of 32/128 implies point-to-point routing (and even 31 in IPv4 would be special). So I'm not sure calling this function with such prefixes even makes sense?

While an IPv4 31/32-bit subnet might not be a valid subnet, with IPv6 I don't know of a reason that single-address prefixes should be treated specially. RFC4191 for example says that an onlink prefix length "value ranges from 0 to 128".

Sure, but I still don't see how this figures into the subnet validation?

An on-link with a prefix 128 just means that the host is on the local link. You simply add it to the routing table and treat it as directly reachable (not via the default gateway). I don't think there would be anything to validate?

Regards,
-Denis

author
#27 lore raw
Quoted 10 lines · Denis Kenzior
Quoted 6 lines · Andrew Zaborowski
Quoted 2 lines · Denis Kenzior

IPv6 doesn't really use subnets like IPv4 does though? I'm not even sure this function would be relevant for IPv6?

You need it to check if an address is within a subnet prefix?

IPv6 doesn't work the same as IPv4 though. I'm fairly certain that the gateway and the static address do not need to be part of the same subnet at all.

Well, I was trying not to concentrate on just one use case since this is public API.

But they kinda do work the same way. The gateway always needs to be in one of the on-link subnets or prefixes.

With static configuration (IPv4 and IPv6) or DHCP v4 you generally have one on-link subnet, the one defined by your IP + prefix length.

With IPv6 Router Advertisements you can have knowledge of multiple on-link prefixes. The gateway needs to match one of them in exactly the same way as with IPv4.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines

Is it? Looks like we do this for FILS,
but I'm not even certain that the check in ie.c in iwd is correct.

Quoted 2 lines

buffer of just long enough for the subnet address, or passing a prefix_len of 32 / 128 for things like a route prefix.

Quoted 8 lines
Quoted 3 lines

Well, prefix_len of 32/128 implies point-to-point routing (and even 31 in IPv4 would be special). So I'm not sure calling this function with such prefixes even makes sense?

While an IPv4 31/32-bit subnet might not be a valid subnet, with IPv6 I don't know of a reason that single-address prefixes should be treated specially. RFC4191 for example says that an onlink prefix length "value ranges from 0 to 128".

Quoted 5 lines

Sure, but I still don't see how this figures into the subnet validation?

An on-link with a prefix 128 just means that the host is on the local link. You simply add it to the routing table and treat it as directly reachable (not via the default gateway). I don't think there would be anything to validate?

Well, if that address you're trying to validate (e.g. let's say your DNS IP) is on-link, which you'd find out from the RA, then you don't need to add anything to the routing table because your RA handler will already have added that route. If it's not and there's no default router, then either it's an error or you might want to implicitly add a route for it.

But the way you'd find out whether given IP is on-link is by calling l_net_subnet_matches for each prefix. Their lengths vary from 0 to 128 like the RFC says.

Best regards

#28 lore raw

Hi Andrew,

Quoted 3 lines

With IPv6 Router Advertisements you can have knowledge of multiple on-link prefixes. The gateway needs to match one of them in exactly the same way as with IPv4.

But netconfig is not doing this matching? The kernel is? icmp6 simply puts in the default gateway info and the prefix routes into the routing table. I don't think it is 'our problem' if the prefixes and route info from the RA results in the RA address not being routable.

Quoted 6 lines

Well, if that address you're trying to validate (e.g. let's say your DNS IP) is on-link, which you'd find out from the RA, then you don't need to add anything to the routing table because your RA handler will already have added that route. If it's not and there's no default router, then either it's an error or you might want to implicitly add a route for it.

If there is no gateway and the DNS address is not 'onlink', what 'implicit' route can we add that would make any sense? Isn't the address simply unreachable?

Why would we be 'validating' the DNS address in the first place? How would we validate it?

Quoted 4 lines

But the way you'd find out whether given IP is on-link is by calling l_net_subnet_matches for each prefix. Their lengths vary from 0 to 128 like the RFC says.

Anyhow, just rename this function l_net_prefix_matches, since that is what you really intend.

Regards,
-Denis

author
#29 lore raw

Hi Denis,

Quoted 5 lines · Denis Kenzior
Quoted 3 lines

With IPv6 Router Advertisements you can have knowledge of multiple on-link prefixes. The gateway needs to match one of them in exactly the same way as with IPv4.

But netconfig is not doing this matching?

I put the following comment in one of the patches:

+        * 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.

So it's not currently doing this but like I said l_net_subnet_match is a public function so it's irrelevant whether netconfig is doing this. We don't want to make public functions only work for some use cases (without documenting which ones...) but rather for the generally useful scenarios.

Quoted 4 lines

The kernel is? icmp6 simply puts in
the default gateway info and the prefix routes into the routing table. I don't think it is 'our problem' if the prefixes and route info from the RA results in the RA address not being routable.

That's a different discussion. We might want to warn or directly return an error from the D-Bus .Connect call if the connection is unusable, rather than ignore it and make the user discover it the hard way. You could argue that it's usable for local traffic though.

Quoted 11 lines
Quoted 8 lines

Well, if that address you're trying to validate (e.g. let's say your DNS IP) is on-link, which you'd find out from the RA, then you don't need to add anything to the routing table because your RA handler will already have added that route. If it's not and there's no default router, then either it's an error or you might want to implicitly add a route for it.

If there is no gateway and the DNS address is not 'onlink', what 'implicit' route can we add that would make any sense?

You could add a direct route for that ip, assume it's 'onlink'. But it's really an error situation and should rather be treated that way IMHO.

Quoted 3 lines

Isn't the address simply unreachable?

Why would we be 'validating' the DNS address in the first place?

To avoid wasting the user's time connecting to an unusable SSID and possibly try the next network available. But again that's a different topic.

Quoted 2 lines

How would we
validate it?

By checking if we have a route covering the address. Basically same as how we validate static configurations.

Best regards

#30 lore raw

Hi Andrew,

Review

Quoted 6 lines

I put the following comment in one of the patches:

(quoted diff) +3 -0
+        * 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.

You keep repeating this, but I still don't believe this is true. You could have RA gateway address being link-local, or subnet C. The prefix info in the RA could be subnet B and the DHCPv6 address obtained could be in subnet A with no overlap. Why would you want to? Who knows ;)

The only 'requirement' is that the gateway is routable. If it isn't, then obviously no traffic can get outside.

So I would simply drop this TODO, and any hope of doing any sort of validation.
Let the kernel do this.

Quoted 5 lines

That's a different discussion. We might want to warn or directly return an error from the D-Bus .Connect call if the connection is unusable, rather than ignore it and make the user discover it the hard way. You could argue that it's usable for local traffic though.

Let the higher layers deal with it. If they need internet connectivity, then services like connman already implement these types of checks.

Quoted 4 lines
Quoted 2 lines

If there is no gateway and the DNS address is not 'onlink', what 'implicit' route can we add that would make any sense?

You could add a direct route for that ip, assume it's 'onlink'. But

Yeah, no :)

Quoted 2 lines

it's really an error situation and should rather be treated that way IMHO.

Sure, but I'd still let the higher levels deal with it.

Regards,
-Denis

author
#31 lore raw

Hi Denis,

Quoted 11 lines · Denis Kenzior

Review

Quoted 6 lines

I put the following comment in one of the patches:

(quoted diff) +3 -0
+        * 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.

You keep repeating this, but I still don't believe this is true. You could have RA gateway address being link-local, or subnet C. The prefix info in the RA could be subnet B and the DHCPv6 address obtained could be in subnet A with no overlap. Why would you want to? Who knows ;)

However complicated the setup is, you have access to all the routes on the interface so it's not a problem.

But the point really is that whoever implements a check like this, and other possible scenarios, if they choose to use ell they might end up with an out of bounds access.

Best regards