[PATCH 3/6] add device setting for controlling ipv6 routing header

Joerg Vehlow lkml at jv-coder.de
Thu Nov 3 23:20:50 PDT 2022


From: Joerg Vehlow <joerg.vehlow at aox.de>

ip6_accept_routing_header with values:
 - all: Allow all routing headers
 - rh2: Allow only routing header 2
 - none: Ignore all routing headers
---
 device.c       | 31 +++++++++++++++++++++++++++++++
 device.h       |  3 +++
 system-linux.c | 20 ++++++++++++++++++++
 3 files changed, 54 insertions(+)

diff --git a/device.c b/device.c
index 9ed50ef..0860c55 100644
--- a/device.c
+++ b/device.c
@@ -12,6 +12,7 @@
  * GNU General Public License for more details.
  */
 #include <string.h>
+#include <strings.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
@@ -66,6 +67,7 @@ static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
 	[DEV_ATTR_IP_FORWARDING] = { .name = "ip_forwarding", .type = BLOBMSG_TYPE_BOOL},
 	[DEV_ATTR_IP6_FORWARDING] = { .name = "ip6_forwarding", .type = BLOBMSG_TYPE_BOOL},
 	[DEV_ATTR_ARP] = { .name = "arp", .type = BLOBMSG_TYPE_BOOL},
+	[DEV_ATTR_IP6_ACCEPT_ROUTING_HEADER] = { .name = "ip6_accept_routing_header", .type = BLOBMSG_TYPE_STRING },
 };
 
 const struct uci_blob_param_list device_attr_list = {
@@ -286,6 +288,7 @@ device_merge_settings(struct device *dev, struct device_settings *n)
 	n->ip_forwarding = s->flags & DEV_OPT_IP_FORWARDING ? s->ip_forwarding : os->ip_forwarding;
 	n->ip6_forwarding = s->flags & DEV_OPT_IP6_FORWARDING ? s->ip6_forwarding : os->ip6_forwarding;
 	n->arp = s->flags & DEV_OPT_ARP ? s->arp : os->arp;
+	n->accept_routing_header = s->flags & DEV_OPT_IP6_ACCEPT_ROUTING_HEADER ? s->accept_routing_header : os->accept_routing_header;
 	n->flags = s->flags | os->flags | os->valid_flags;
 }
 
@@ -485,6 +488,22 @@ device_init_settings(struct device *dev, struct blob_attr **tb)
 		s->flags |= DEV_OPT_ARP;
 	}
 
+	if ((cur = tb[DEV_ATTR_IP6_ACCEPT_ROUTING_HEADER])) {
+		const char *val = blobmsg_get_string(cur);
+		if (strcasecmp(val, "all") == 0) {
+			s->accept_routing_header = 1;
+			s->flags |= DEV_OPT_IP6_ACCEPT_ROUTING_HEADER;
+		} else if (strcasecmp(val, "rh2") == 0) {
+			s->accept_routing_header = 0;
+			s->flags |= DEV_OPT_IP6_ACCEPT_ROUTING_HEADER;
+		} else if (strcasecmp(val, "none") == 0) {
+			s->accept_routing_header = -1;
+			s->flags |= DEV_OPT_IP6_ACCEPT_ROUTING_HEADER;
+		} else {
+			DPRINTF("Invalid value: %s - (use 'all', 'rh2' or 'none')\n", val);
+		}
+	}
+
 	device_set_disabled(dev, disabled);
 }
 
@@ -1235,6 +1254,18 @@ device_dump_status(struct blob_buf *b, struct device *dev)
 			blobmsg_add_u8(b, "ip_forwarding", st.ip_forwarding);
 		if (st.flags & DEV_OPT_IP6_FORWARDING)
 			blobmsg_add_u8(b, "ip6_forwarding", st.ip6_forwarding);
+		if (st.flags & DEV_OPT_IP6_ACCEPT_ROUTING_HEADER) {
+			const char *val = NULL;
+			if (st.accept_routing_header == 0) {
+				val = "rh2";
+			} else if (st.accept_routing_header < 0) {
+				val = "none";
+			} else {
+				val = "all";
+			}
+
+			blobmsg_add_string(b, "ip6_accept_routing_header", val);
+		}
 	}
 
 	s = blobmsg_open_table(b, "statistics");
diff --git a/device.h b/device.h
index f78bbcb..c94a32f 100644
--- a/device.h
+++ b/device.h
@@ -65,6 +65,7 @@ enum {
 	DEV_ATTR_IP_FORWARDING,
 	DEV_ATTR_IP6_FORWARDING,
 	DEV_ATTR_ARP,
+	DEV_ATTR_IP6_ACCEPT_ROUTING_HEADER,
 	__DEV_ATTR_MAX,
 };
 
@@ -132,6 +133,7 @@ enum {
 	DEV_OPT_IP_FORWARDING   = (1ULL << 32),
 	DEV_OPT_IP6_FORWARDING  = (1ULL << 33),
 	DEV_OPT_ARP             = (1ULL << 34),
+	DEV_OPT_IP6_ACCEPT_ROUTING_HEADER = (1ULL << 35),
 };
 
 /* events broadcasted to all users of a device */
@@ -212,6 +214,7 @@ struct device_settings {
 	bool ip_forwarding;
 	bool ip6_forwarding;
 	bool arp;
+	int accept_routing_header;
 };
 
 /*
diff --git a/system-linux.c b/system-linux.c
index 7b82e17..12a7e3f 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -470,6 +470,13 @@ static void system_set_ip6_forwarding(struct device *dev, const char *val)
 	system_set_dev_sysctl("ipv6/conf", "forwarding", dev->ifname, val);
 }
 
+static void system_set_ip6_accept_source_route(struct device *dev, int val)
+{
+	char sval[10];
+	snprintf(sval, sizeof(sval), "%d", val);
+	system_set_dev_sysctl("ipv6/conf", "accept_source_route", dev->ifname, sval);
+}
+
 static void system_bridge_set_multicast_to_unicast(struct device *dev, const char *val)
 {
 	system_set_dev_sysfs("brport/multicast_to_unicast", dev->ifname, val);
@@ -643,6 +650,12 @@ static int system_get_ip6_forwarding(struct device *dev, char *buf, const size_t
 			dev->ifname, buf, buf_sz);
 }
 
+static int system_get_accept_source_route(struct device *dev, char *buf, const size_t buf_sz)
+{
+	return system_get_dev_sysctl("ipv6/conf", "accept_source_route",
+			dev->ifname, buf, buf_sz);
+}
+
 /* Evaluate netlink messages */
 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
 {
@@ -1830,6 +1843,11 @@ system_if_get_settings(struct device *dev, struct device_settings *s)
 		s->ip6_forwarding = strtoul(buf, NULL, 0);
 		s->flags |= DEV_OPT_IP6_FORWARDING;
 	}
+
+	if (!system_get_accept_source_route(dev, buf, sizeof(buf))) {
+		s->accept_routing_header = strtoul(buf, NULL, 0);
+		s->flags |= DEV_OPT_IP6_ACCEPT_ROUTING_HEADER;
+	}
 }
 
 void
@@ -1936,6 +1954,8 @@ system_if_apply_settings(struct device *dev, struct device_settings *s, uint64_t
 		if (system_if_flags(dev->ifname, !s->arp ? IFF_NOARP : 0, s->arp ? IFF_NOARP : 0) < 0)
 			s->flags &= ~DEV_OPT_ARP;
 	}
+	if (apply_mask & DEV_OPT_IP6_ACCEPT_ROUTING_HEADER)
+		system_set_ip6_accept_source_route(dev, s->accept_routing_header);
 
 	system_set_ethtool_settings(dev, s);
 }
-- 
2.25.1




More information about the openwrt-devel mailing list