Skip to contentSkip to conversation

rtnlutil: Add IPv6 route deletion helper

PATCH In review Tim Kourt 2 participants 5 messages Oct 2, 2019 – Oct 3, 2019

lore
Share & tools
lore

Tim Kourt

author
#0 lore raw

PATCH 1/4 rtnlutil: Add IPv6 route deletion helper

---
 src/rtnlutil.c | 31 +++++++++++++++++++++++++++++--
 src/rtnlutil.h |  7 +++++++
 2 files changed, 36 insertions(+), 2 deletions(-)
src/rtnlutil.c +29 -2
index 4090d311..f90e9dcf 100644
@@ -533,7 +533,8 @@ uint32_t rtnl_ifaddr_ipv6_delete(struct l_netlink *rtnl, int ifindex,
 						ip, cb, user_data, destroy);
 }
 
-uint32_t rtnl_route_ipv6_add_gateway(struct l_netlink *rtnl, int ifindex,
+static uint32_t rtnl_route_ipv6_change(struct l_netlink *rtnl,
+					uint16_t nlmsg_type, int ifindex,
 					const char *gateway,
 					uint32_t priority_offset,
 					uint8_t proto,
@@ -581,7 +582,33 @@ uint32_t rtnl_route_ipv6_add_gateway(struct l_netlink *rtnl, int ifindex,
 						sizeof(struct in6_addr));
 	}
 
-	return l_netlink_send(rtnl, RTM_NEWROUTE, flags, rtmmsg,
+	return l_netlink_send(rtnl, nlmsg_type, flags, rtmmsg,
 				rta_buf - (void *) rtmmsg, cb, user_data,
 								destroy);
 }
+
+uint32_t rtnl_route_ipv6_add_gateway(struct l_netlink *rtnl, int ifindex,
+					const char *gateway,
+					uint32_t priority_offset,
+					uint8_t proto,
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy)
+{
+	return rtnl_route_ipv6_change(rtnl, RTM_NEWROUTE, ifindex, gateway,
+					priority_offset, proto, cb,
+					user_data, destroy);
+}
+
+uint32_t rtnl_route_ipv6_delete_gateway(struct l_netlink *rtnl, int ifindex,
+					const char *gateway,
+					uint32_t priority_offset,
+					uint8_t proto,
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy)
+{
+	return rtnl_route_ipv6_change(rtnl, RTM_DELROUTE, ifindex, gateway,
+					priority_offset, proto, cb,
+					user_data, destroy);
+}
src/rtnlutil.h +7 -0
index 6689e16e..383d562f 100644
@@ -90,3 +90,10 @@ uint32_t rtnl_route_ipv6_add_gateway(struct l_netlink *rtnl, int ifindex,
 					l_netlink_command_func_t cb,
 					void *user_data,
 					l_netlink_destroy_func_t destroy);
+uint32_t rtnl_route_ipv6_delete_gateway(struct l_netlink *rtnl, int ifindex,
+					const char *gateway,
+					uint32_t priority_offset,
+					uint8_t proto,
+					l_netlink_command_func_t cb,
+					void *user_data,
+					l_netlink_destroy_func_t destroy);
Signature · 1 line
2.13.6
author
#1 lore raw

PATCH 2/4 netconfig: Remove IPv6 default route

The IPv6 default route needs to be explicitly revoked. Unlike in IPv4, there is no SRC address associated with the route and it will not be removed on address removal.

---
 src/netconfig.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
src/netconfig.c +15 -0
index 90cd6bc2..cf951245 100644
@@ -958,6 +958,7 @@ static void netconfig_ipv6_select_and_install(struct netconfig *netconfig)
 static void netconfig_ipv6_select_and_uninstall(struct netconfig *netconfig)
 {
 	struct netconfig_ifaddr *ifaddr;
+	char *gateway;
 
 	ifaddr = netconfig_ipv6_get_ifaddr(netconfig,
 						netconfig->rtm_v6_protocol);
@@ -970,6 +971,20 @@ static void netconfig_ipv6_select_and_uninstall(struct netconfig *netconfig)
 	 * TODO
 	 * l_dhcp_v6_client_stop(netconfig->l_dhcp_v6_client);
 	 */
+
+	gateway = netconfig_ipv6_get_gateway(netconfig);
+	if (!gateway)
+		return;
+
+	if (!rtnl_route_ipv6_delete_gateway(rtnl, netconfig->ifindex,
+			gateway, ROUTE_PRIORITY_OFFSET,
+			netconfig->rtm_v6_protocol,
+			netconfig_route_cmd_cb, NULL, NULL)) {
+		l_error("netconfig: Failed to delete route for: %s gateway.",
+								gateway);
+	}
+
+	l_free(gateway);
 }
 
 bool netconfig_configure(struct netconfig *netconfig,
Signature · 1 line
2.13.6
author
#2 lore raw

PATCH 3/4 netconfig: Optimize IPv4 address deletion

In reply to Tim Kourt

Decrease the queue traversals to a single pass

---
 src/netconfig.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
src/netconfig.c +7 -7
index cf951245..2175d9be 100644
@@ -427,22 +427,22 @@ static void netconfig_ifaddr_deleted(struct netconfig *netconfig,
 					uint32_t len)
 {
 	struct netconfig_ifaddr *ifaddr;
-	char *ip;
+	struct netconfig_ifaddr query;
 
-	rtnl_ifaddr_extract(ifa, len, NULL, &ip, NULL);
+	rtnl_ifaddr_extract(ifa, len, NULL, &query.ip, NULL);
 
-	ifaddr = netconfig_ifaddr_find(netconfig, ifa->ifa_family,
-							ifa->ifa_prefixlen, ip);
+	query.family = ifa->ifa_family;
+	query.prefix_len = ifa->ifa_prefixlen;
 
-	l_free(ip);
+	ifaddr = l_queue_remove_if(netconfig->ifaddr_list,
+						netconfig_ifaddr_match, &query);
+	l_free(query.ip);
 
 	if (!ifaddr)
 		return;
 
 	l_debug("ifaddr %s/%u", ifaddr->ip, ifaddr->prefix_len);
 
-	l_queue_remove(netconfig->ifaddr_list, ifaddr);
-
 	netconfig_ifaddr_destroy(ifaddr);
 }
 
Signature · 1 line
2.13.6
author
#3 lore raw

PATCH 4/4 netconfig: Fix return type for module init

In reply to Tim Kourt
---
 src/netconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/netconfig.c +1 -1
index 2175d9be..285d5fef 100644
@@ -1087,7 +1087,7 @@ static int netconfig_init(void)
 								!enabled) {
 		l_warn("netconfig: Network configuration with the IP addresses "
 								"is disabled.");
-		return false;
+		return 0;
 	}
 
 	rtnl = l_netlink_new(NETLINK_ROUTE);
Signature · 1 line
2.13.6
#4 lore raw
In reply to Tim Kourt

Hi Tim,

Quoted 5 lines · Tim Kourt
---
  src/rtnlutil.c | 31 +++++++++++++++++++++++++++++--
  src/rtnlutil.h |  7 +++++++
  2 files changed, 36 insertions(+), 2 deletions(-)

All applied, thanks.

Regards,
-Denis