[PATCH 4/6] add global option for ipv4 default ttl

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


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

---
 config.c       | 21 +++++++++++++++++----
 system-dummy.c |  4 ++++
 system-linux.c | 18 ++++++++++++++++++
 system.h       | 10 ++++++++++
 4 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/config.c b/config.c
index 9bbda39..93df20e 100644
--- a/config.c
+++ b/config.c
@@ -27,6 +27,7 @@
 #include "proto.h"
 #include "wireless.h"
 #include "config.h"
+#include "system.h"
 
 bool config_init = false;
 
@@ -528,14 +529,26 @@ config_init_rules(void)
 static void
 config_init_globals(void)
 {
-	struct uci_section *globals = uci_lookup_section(
-			uci_ctx, uci_network, "globals");
+	struct uci_section *globals = uci_lookup_section(uci_ctx, uci_network, "globals");
 	if (!globals)
 		return;
 
-	const char *ula_prefix = uci_lookup_option_string(
-			uci_ctx, globals, "ula_prefix");
+	const char *ula_prefix = uci_lookup_option_string(uci_ctx, globals, "ula_prefix");
 	interface_ip_set_ula_prefix(ula_prefix);
+
+	struct global_settings config = {};
+
+	const char *default_ttl = uci_lookup_option_string(uci_ctx, globals, "ip_default_ttl");
+	if (default_ttl) {
+		config.ttl = strtoul(default_ttl, NULL, 10);
+		if (config.ttl < 1 || config.ttl > 255) {
+			netifd_log_message(L_WARNING, "Invalid value '%d' for ip4_default_ttl (allowed 1-255)\n");
+		} else {
+			config.flags |= GLOBAL_OPT_TTL;
+		}
+	}
+
+	system_globals_apply_settings(&config);
 }
 
 static void
diff --git a/system-dummy.c b/system-dummy.c
index b13bc87..f47b1af 100644
--- a/system-dummy.c
+++ b/system-dummy.c
@@ -379,3 +379,7 @@ int system_vlandev_del(struct device *vlandev)
 {
 	return 0;
 }
+
+void system_globals_apply_settings(const struct global_settings *settings)
+{
+}
diff --git a/system-linux.c b/system-linux.c
index 12a7e3f..66470b6 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -332,6 +332,13 @@ dev_sysfs_path(const char *ifname, const char *file)
 	return dev_buf;
 }
 
+static void
+system_set_sysctl(const char *file, const char *val)
+{
+	snprintf(dev_buf, sizeof(dev_buf), "%s/sys/net/%s", proc_path, file);
+	write_file(dev_buf, val);
+}
+
 static void
 system_set_dev_sysctl(const char *prefix, const char *file, const char *ifname,
 		      const char *val)
@@ -4101,3 +4108,14 @@ int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr)
 
 	return 0;
 }
+
+void system_globals_apply_settings(const struct global_settings *settings)
+{
+	uint64_t flags = settings->flags;
+	char buf[12];
+
+	if (flags & GLOBAL_OPT_TTL) {
+		snprintf(buf, sizeof(buf), "%d", settings->ttl);
+		system_set_sysctl("ipv4/ip_default_ttl", buf);
+	}
+}
diff --git a/system.h b/system.h
index 0f08c26..ee3c03e 100644
--- a/system.h
+++ b/system.h
@@ -291,6 +291,15 @@ struct bonding_config {
 	int downdelay;
 };
 
+enum {
+	GLOBAL_OPT_TTL = (1ULL << 0),
+};
+
+struct global_settings {
+	uint64_t flags;
+	int ttl;
+};
+
 static inline int system_get_addr_family(unsigned int flags)
 {
 	if ((flags & DEVADDR_FAMILY) == DEVADDR_INET6)
@@ -383,4 +392,5 @@ int system_link_netns_move(struct device *dev, const pid_t target_ns, const char
 int system_netns_open(const pid_t target_ns);
 int system_netns_set(int netns_fd);
 
+void system_globals_apply_settings(const struct global_settings *settings);
 #endif
-- 
2.25.1




More information about the openwrt-devel mailing list