[PATCH 5/6] add device setting for ipv6 hop limit

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


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

---
 device.c       |  9 +++++++++
 device.h       |  3 +++
 system-linux.c | 21 +++++++++++++++++++++
 3 files changed, 33 insertions(+)

diff --git a/device.c b/device.c
index 0860c55..80fa359 100644
--- a/device.c
+++ b/device.c
@@ -68,6 +68,7 @@ static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
 	[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 },
+	[DEV_ATTR_IP6_HOP_LIMIT] = { .name = "ip6_hop_limit", .type = BLOBMSG_TYPE_INT32},
 };
 
 const struct uci_blob_param_list device_attr_list = {
@@ -289,6 +290,7 @@ device_merge_settings(struct device *dev, struct device_settings *n)
 	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->hop_limit = s->flags & DEV_OPT_IP6_HOP_LIMIT ? s->hop_limit : os->hop_limit;
 	n->flags = s->flags | os->flags | os->valid_flags;
 }
 
@@ -504,6 +506,11 @@ device_init_settings(struct device *dev, struct blob_attr **tb)
 		}
 	}
 
+	if ((cur = tb[DEV_ATTR_IP6_HOP_LIMIT])) {
+		s->hop_limit = blobmsg_get_u32(cur);
+		s->flags |= DEV_OPT_IP6_HOP_LIMIT;
+	}
+
 	device_set_disabled(dev, disabled);
 }
 
@@ -1266,6 +1273,8 @@ device_dump_status(struct blob_buf *b, struct device *dev)
 
 			blobmsg_add_string(b, "ip6_accept_routing_header", val);
 		}
+		if (st.flags & DEV_OPT_IP6_HOP_LIMIT)
+			blobmsg_add_u32(b, "ip6_hop_limit", st.hop_limit);
 	}
 
 	s = blobmsg_open_table(b, "statistics");
diff --git a/device.h b/device.h
index c94a32f..63be0ae 100644
--- a/device.h
+++ b/device.h
@@ -66,6 +66,7 @@ enum {
 	DEV_ATTR_IP6_FORWARDING,
 	DEV_ATTR_ARP,
 	DEV_ATTR_IP6_ACCEPT_ROUTING_HEADER,
+	DEV_ATTR_IP6_HOP_LIMIT,
 	__DEV_ATTR_MAX,
 };
 
@@ -134,6 +135,7 @@ enum {
 	DEV_OPT_IP6_FORWARDING  = (1ULL << 33),
 	DEV_OPT_ARP             = (1ULL << 34),
 	DEV_OPT_IP6_ACCEPT_ROUTING_HEADER = (1ULL << 35),
+	DEV_OPT_IP6_HOP_LIMIT   = (1ULL << 36),
 };
 
 /* events broadcasted to all users of a device */
@@ -215,6 +217,7 @@ struct device_settings {
 	bool ip6_forwarding;
 	bool arp;
 	int accept_routing_header;
+	int hop_limit;
 };
 
 /*
diff --git a/system-linux.c b/system-linux.c
index 66470b6..462a8cc 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -477,6 +477,11 @@ 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_hop_limit(struct device *dev, const char *val)
+{
+	system_set_dev_sysctl("ipv6/conf", "hop_limit", dev->ifname, val);
+}
+
 static void system_set_ip6_accept_source_route(struct device *dev, int val)
 {
 	char sval[10];
@@ -663,6 +668,12 @@ static int system_get_accept_source_route(struct device *dev, char *buf, const s
 			dev->ifname, buf, buf_sz);
 }
 
+static int system_get_ip6_hop_limit(struct device *dev, char *buf, const size_t buf_sz)
+{
+	return system_get_dev_sysctl("ipv6/conf", "hop_limit",
+			dev->ifname, buf, buf_sz);
+}
+
 /* Evaluate netlink messages */
 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
 {
@@ -1855,6 +1866,11 @@ system_if_get_settings(struct device *dev, struct device_settings *s)
 		s->accept_routing_header = strtoul(buf, NULL, 0);
 		s->flags |= DEV_OPT_IP6_ACCEPT_ROUTING_HEADER;
 	}
+
+	if (!system_get_ip6_hop_limit(dev, buf, sizeof(buf))) {
+		s->hop_limit = strtoul(buf, NULL, 0);
+		s->flags |= DEV_OPT_IP6_HOP_LIMIT;
+	}
 }
 
 void
@@ -1964,6 +1980,11 @@ system_if_apply_settings(struct device *dev, struct device_settings *s, uint64_t
 	if (apply_mask & DEV_OPT_IP6_ACCEPT_ROUTING_HEADER)
 		system_set_ip6_accept_source_route(dev, s->accept_routing_header);
 
+	if (apply_mask & DEV_OPT_IP6_HOP_LIMIT) {
+		snprintf(buf, sizeof(buf), "%d", s->hop_limit);
+		system_set_ip6_hop_limit(dev, buf);
+	}
+
 	system_set_ethtool_settings(dev, s);
 }
 
-- 
2.25.1




More information about the openwrt-devel mailing list