Skip to contentSkip to conversation

rtnl: Add l_rtnl_route_set_gateway

PATCH Applied Andrew Zaborowski 2 participants 9 messages Jan 3, 2022 – Jan 5, 2022

lore
Share & tools
lore

Andrew Zaborowski

author
#0 lore raw

PATCH 1/4 rtnl: Add l_rtnl_route_set_gateway

Allow creating prefix routes for off-link prefixes, i.e. ones that should be routed through a gateway.

---
 ell/ell.sym |  1 +
 ell/rtnl.c  | 16 ++++++++++++++++
 ell/rtnl.h  |  1 +
 3 files changed, 18 insertions(+)
ell/ell.sym +1 -0
index 51db458..e581add 100644
@@ -633,6 +633,7 @@ global:
 	l_rtnl_route_free;
 	l_rtnl_route_get_family;
 	l_rtnl_route_get_gateway;
+	l_rtnl_route_set_gateway;
 	l_rtnl_route_get_lifetime;
 	l_rtnl_route_set_lifetime;
 	l_rtnl_route_get_mtu;
ell/rtnl.c +16 -0
index 4a1a248..ee6dc9a 100644
@@ -403,6 +403,22 @@ LIB_EXPORT bool l_rtnl_route_get_gateway(const struct l_rtnl_route *rt,
 					out_buf);
 }
 
+LIB_EXPORT bool l_rtnl_route_set_gateway(struct l_rtnl_route *rt,
+						const char *address)
+{
+	if (unlikely(!rt))
+		return false;
+
+	switch (rt->family) {
+	case AF_INET:
+		return inet_pton(AF_INET, address, &rt->gw.in_addr) == 1;
+	case AF_INET6:
+		return inet_pton(AF_INET6, address, &rt->gw.in6_addr) == 1;
+	default:
+		return  false;
+	}
+}
+
 LIB_EXPORT uint32_t l_rtnl_route_get_lifetime(const struct l_rtnl_route *rt)
 {
 	if (unlikely(!rt))
ell/rtnl.h +1 -0
index 274816c..70cbaec 100644
@@ -69,6 +69,7 @@ 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);
+bool l_rtnl_route_set_gateway(struct l_rtnl_route *rt, const char *address);
 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);
 uint32_t l_rtnl_route_get_mtu(const struct l_rtnl_route *rt);
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH 2/4 dhcp6: Minor optimzation in dhcp6_client_send_initial

---
 ell/dhcp6.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ell/dhcp6.c +1 -1
index 3321043..3e38ff2 100644
@@ -1493,7 +1493,7 @@ static void dhcp6_client_icmp6_event(struct l_icmp6_client *icmp6,
 			return;
 		}
 
-		if (l_icmp6_router_get_managed(r))
+		if (managed)
 			dhcp6_client_send_initial(client);
 
 		break;
Signature · 1 line
2.32.0
Applied
#2 lore raw

Re: [PATCH 2/4] dhcp6: Minor optimzation in dhcp6_client_send_initial

Hi Andrew,

Quoted 4 lines · Andrew Zaborowski
---
  ell/dhcp6.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Applied, thanks.

Regards,
-Denis

author
#3 lore raw

PATCH 3/4 icmp6: Respect lifetime values in a RA

In reply to Andrew Zaborowski

If the router lifetime is 0 in a RA that router is not supposed to be added to the Default Router List in RFC4861:

"A value of zero indicates that the router is not to be used as a default router."

"A router might want to send Router Advertisements without advertising itself as a default router. For instance, a router might advertise prefixes for stateless address autoconfiguration while not wishing to forward packets. Such a router sets the Router Lifetime field in outgoing advertisements to zero."

Also set the lifetime values on the routes to those received so that the user of, say, l_icmp6_client_get_router(), can also determine whether there is a default router.

---
 ell/icmp6.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ell/icmp6.c +3 -1
index 42fe67d..93c1701 100644
@@ -299,9 +299,10 @@ static void icmp6_client_setup_routes(struct l_icmp6_client *client)
 	l_rtnl_route_set_protocol(rt, RTPROT_RA);
 	l_rtnl_route_set_mtu(rt, ra->mtu);
 	l_rtnl_route_set_priority(rt, client->route_priority);
+	l_rtnl_route_set_lifetime(rt, ra->lifetime);
 	l_queue_push_tail(client->routes, rt);
 
-	if (client->rtnl)
+	if (client->rtnl && ra->lifetime)
 		l_rtnl_route_add(client->rtnl, client->ifindex, rt,
 					NULL, NULL, NULL);
 
@@ -322,6 +323,7 @@ static void icmp6_client_setup_routes(struct l_icmp6_client *client)
 		l_rtnl_route_set_protocol(rt, RTPROT_RA);
 		l_rtnl_route_set_mtu(rt, ra->mtu);
 		l_rtnl_route_set_priority(rt, client->route_priority);
+		l_rtnl_route_set_lifetime(rt, info->valid_lifetime);
 		l_queue_push_tail(client->routes, rt);
 
 		if (client->rtnl)
Signature · 1 line
2.32.0
#4 lore raw

Re: [PATCH 3/4] icmp6: Respect lifetime values in a RA

Quoted 6 lines · Andrew Zaborowski

If the router lifetime is 0 in a RA that router is not supposed to be added to the Default Router List in RFC4861:

"A value of zero indicates that the router is not to be used as a default router."

You may want to put this into the code directly.

Quoted 6 lines

"A router might want to send Router Advertisements without advertising itself as a default router. For instance, a router might advertise prefixes for stateless address autoconfiguration while not wishing to forward packets. Such a router sets the Router Lifetime field in outgoing advertisements to zero."

But yes, this part makes sense and no objections here.

Quoted 3 lines

Also set the lifetime values on the routes to those received so that the user of, say, l_icmp6_client_get_router(), can also determine whether there is a default router.

Note that we do not re-upload the routes to the kernel on every RA received. See the TODO comment in icmp6_client_handle_message(). Once lifetime expires, the route will be removed -> internet is broke. Please at least sanity test the changes you're sending out or label them as RFC if you haven't done so.

Quoted 4 lines
---
  ell/icmp6.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Regards,
-Denis

author
#5 lore raw

Re: [PATCH 3/4] icmp6: Respect lifetime values in a RA

Quoted 6 lines · Denis Kenzior
Quoted 3 lines · Andrew Zaborowski

"A value of zero indicates that the router is not to be used as a default router."

You may want to put this into the code directly.

Ok, for some reason we don't quote the RFC in this file.

Quoted 9 lines
Quoted 4 lines

Also set the lifetime values on the routes to those received so that the user of, say, l_icmp6_client_get_router(), can also determine whether there is a default router.

Note that we do not re-upload the routes to the kernel on every RA received. See the TODO comment in icmp6_client_handle_message(). Once lifetime expires, the route will be removed -> internet is broke. Please at least sanity test the changes you're sending out or label them as RFC if you haven't done so.

Ok, I've dropped the l_icmp6_client_set_rtnl() use locally in IWD so this wouldn't have shown up even if the router had set a short lifetime. But I need some way for the user to check if there's a default gateway.

Best regards

author
#6 lore raw

PATCH 4/4 icmp6: Respect the on-link bit in RA prefixes

In reply to Andrew Zaborowski

When the on-link bit is not 1 in a prefix option, the packets addressed to that prefix should go through the router who sent the RA.

---
 ell/icmp6-private.h | 1 +
 ell/icmp6.c         | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
ell/icmp6-private.h +1 -0
index 3846bad..38d640f 100644
@@ -23,6 +23,7 @@
 struct route_info {
 	uint8_t address[16];
 	uint8_t prefix_len;
+	bool onlink : 1;
 	uint8_t preference;
 	uint32_t preferred_lifetime;
 	uint32_t valid_lifetime;
ell/icmp6.c +7 -2
index 93c1701..197aa4a 100644
@@ -285,11 +285,11 @@ static void icmp6_client_setup_routes(struct l_icmp6_client *client)
 {
 	struct l_icmp6_router *ra = client->ra;
 	struct l_rtnl_route *rt;
-	char buf[INET6_ADDRSTRLEN];
+	char gw_buf[INET6_ADDRSTRLEN];
 	unsigned int i;
 
 	rt = l_rtnl_route_new_gateway(inet_ntop(AF_INET6, ra->address,
-							buf, sizeof(buf)));
+						gw_buf, sizeof(gw_buf)));
 	if (!rt) {
 		CLIENT_DEBUG("Unable to parse RA 'from' address");
 		return;
@@ -308,6 +308,7 @@ static void icmp6_client_setup_routes(struct l_icmp6_client *client)
 
 	for (i = 0; i < ra->n_prefixes; i++) {
 		struct route_info *info = &ra->prefixes[i];
+		char buf[INET6_ADDRSTRLEN];
 
 		if (info->valid_lifetime == 0)
 			continue;
@@ -319,6 +320,9 @@ static void icmp6_client_setup_routes(struct l_icmp6_client *client)
 		if (!rt)
 			continue;
 
+		if (!info->onlink)
+			l_rtnl_route_set_gateway(rt, gw_buf);
+
 		l_rtnl_route_set_preference(rt, info->preference);
 		l_rtnl_route_set_protocol(rt, RTPROT_RA);
 		l_rtnl_route_set_mtu(rt, ra->mtu);
@@ -729,6 +733,7 @@ struct l_icmp6_router *_icmp6_router_parse(const struct nd_router_advert *ra,
 				break;
 
 			i->prefix_len = opts[2];
+			i->onlink = opts[3] & 1;
 			i->valid_lifetime = l_get_be32(opts + 4);
 			i->preferred_lifetime = l_get_be32(opts + 8);
 			memcpy(i->address, opts + 16, 16);
Signature · 1 line
2.32.0
#7 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 8 lines · Andrew Zaborowski

Allow creating prefix routes for off-link prefixes, i.e. ones that should be routed through a gateway.

---
  ell/ell.sym |  1 +
  ell/rtnl.c  | 16 ++++++++++++++++
  ell/rtnl.h  |  1 +
  3 files changed, 18 insertions(+)

<snip>

Quoted 7 lines
ell/rtnl.c +6 -0
+LIB_EXPORT bool l_rtnl_route_set_gateway(struct l_rtnl_route *rt,
+						const char *address)
+{
+	if (unlikely(!rt))
+		return false;
+

ell/rtnl.c LIB_EXPORT bool l_rtnl_route_get_gateway(const struct l_rtnl_route *rt, · line 421

Quoted 10 lines
ell/rtnl.c +10 -0
+	switch (rt->family) {
+	case AF_INET:
+		return inet_pton(AF_INET, address, &rt->gw.in_addr) == 1;
+	case AF_INET6:
+		return inet_pton(AF_INET6, address, &rt->gw.in6_addr) == 1;
+	default:
+		return  false;
+	}
+}
+

Since we already have l_rtnl_route_new_gateway, would this make more sense as a dedicated constructor? Something like l_rtnl_route_new_static?

Quoted 3 lines
  LIB_EXPORT uint32_t l_rtnl_route_get_lifetime(const struct l_rtnl_route *rt)
  {
  	if (unlikely(!rt))

Regards,
-Denis

author
#8 lore raw

Hi Denis,

Quoted 24 lines · Denis Kenzior
Quoted 8 lines · Andrew Zaborowski

Allow creating prefix routes for off-link prefixes, i.e. ones that should be routed through a gateway.

---
  ell/ell.sym |  1 +
  ell/rtnl.c  | 16 ++++++++++++++++
  ell/rtnl.h  |  1 +
  3 files changed, 18 insertions(+)

<snip>

Quoted 12 lines
ell/rtnl.c +11 -0
+LIB_EXPORT bool l_rtnl_route_set_gateway(struct l_rtnl_route *rt,
+                                             const char *address)
+{
+     if (unlikely(!rt))
+             return false;
+
+     switch (rt->family) {
+     case AF_INET:
+             return inet_pton(AF_INET, address, &rt->gw.in_addr) == 1;
+     case AF_INET6:
+             return inet_pton(AF_INET6, address, &rt->gw.in6_addr) == 1;
Quoted 8 lines

ell/rtnl.c LIB_EXPORT bool l_rtnl_route_get_gateway(const struct l_rtnl_route *rt, · line 421

Quoted 5 lines
ell/rtnl.c +5 -0
+     default:
+             return  false;
+     }
+}
+

Since we already have l_rtnl_route_new_gateway, would this make more sense as a dedicated constructor? Something like l_rtnl_route_new_static?

Ok.

Best regards