ell.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH 09/14] netconfig: Write /proc/sys/net files to enable IPv6
@ 2022-06-15 22:47 Andrew Zaborowski
  0 siblings, 0 replies; only message in thread
From: Andrew Zaborowski @ 2022-06-15 22:47 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 3621 bytes --]

Write the accept_ra setting to 0.  On l_netconfig_start if the existing
address dump command finds no link-local address then also try resetting
disable_ipv6 and toggling addr_gen_mode to trigger the kernel to
generate a link-local address.
---
 ell/netconfig.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 2 deletions(-)

diff --git a/ell/netconfig.c b/ell/netconfig.c
index b8f98d0..8d0425d 100644
--- a/ell/netconfig.c
+++ b/ell/netconfig.c
@@ -28,6 +28,12 @@
 #include <netinet/ip.h>
 #include <arpa/inet.h>
 #include <netinet/icmp6.h>
+#include <net/if.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "private.h"
 #include "useful.h"
@@ -858,6 +864,30 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
 		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
 }
 
+static int netconfig_proc_write_ipv6_setting(struct l_netconfig *nc,
+						const char *setting,
+						const char *value)
+{
+	char ifname[IF_NAMESIZE];
+	_auto_(l_free) char *filename = NULL;
+	int fd;
+	int r;
+
+	if (unlikely(!if_indextoname(nc->ifindex, ifname)))
+		return -errno;
+
+	filename = l_strdup_printf("/proc/sys/net/ipv6/conf/%s/%s",
+					ifname, setting);
+
+	fd = L_TFR(open(filename, O_WRONLY));
+	if (unlikely(fd < 0))
+		return -errno;
+
+	r = L_TFR(write(fd, value, strlen(value)));
+	L_TFR(close(fd));
+	return r;
+}
+
 LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
 {
 	struct l_netconfig *nc;
@@ -890,6 +920,9 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
 					netconfig_icmp6_event_handler,
 					nc, NULL);
 
+	/* Disable in-kernel autoconfiguration for the interface */
+	netconfig_proc_write_ipv6_setting(nc, "accept_ra", "0");
+
 	return nc;
 }
 
@@ -1330,6 +1363,34 @@ static void netconfig_ifaddr_ipv6_dump_cb(int error, uint16_t type,
 	netconfig_ifaddr_ipv6_notify(type, data, len, user_data);
 }
 
+static void netconfig_ifaddr_ipv6_dump_done_cb(void *user_data)
+{
+	struct l_netconfig *nc = user_data;
+
+	/*
+	 * Handle the case of no link-local address having been found during
+	 * the dump.  If nc->ifaddr6_dump_cmd_id is 0, we have found one or
+	 * the dump is being cancelled.  Otherwise try disabing the
+	 * "disable_ipv6" setting for the interface since it may have been
+	 * enabled.  Also write "addr_gen_mode" which triggers regerating
+	 * the link-local addresss on the interface in the kernel if it
+	 * was previously removed.
+	 */
+	if (!nc->ifaddr6_dump_cmd_id || !nc->started)
+		return;
+
+	nc->ifaddr6_dump_cmd_id = 0;
+
+	/* "do not generate a link-local address" */
+	netconfig_proc_write_ipv6_setting(nc, "addr_gen_mode", "1");
+	/* "generate address based on EUI64 (default)" */
+	netconfig_proc_write_ipv6_setting(nc, "addr_gen_mode", "0");
+	/* "enable IPv6 operation" */
+	netconfig_proc_write_ipv6_setting(nc, "disable_ipv6", "0");
+
+	/* TODO: save original values and reset in l_netconfig_stop() */
+}
+
 LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
 {
 	if (unlikely(!netconfig || netconfig->started))
@@ -1390,8 +1451,9 @@ configure_ipv6:
 	}
 
 	netconfig->ifaddr6_dump_cmd_id = l_rtnl_ifaddr6_dump(l_rtnl_get(),
-						netconfig_ifaddr_ipv6_dump_cb,
-						netconfig, NULL);
+					netconfig_ifaddr_ipv6_dump_cb,
+					netconfig,
+					netconfig_ifaddr_ipv6_dump_done_cb);
 	if (!netconfig->ifaddr6_dump_cmd_id)
 		goto unregister;
 
-- 
2.34.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-15 22:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-15 22:47 [PATCH 09/14] netconfig: Write /proc/sys/net files to enable IPv6 Andrew Zaborowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).