From d58d0a793e41e8740898929647a95f292b8ad9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8D=C3=B1igo=20Huguet?= Date: Fri, 2 May 2025 08:37:16 +0200 Subject: [PATCH] ip: restore IP forwarding on device deconfiguration With the ipv4.forwarding property we may modify the forwarding sysctl of the device on activation. In next commits, we will also modify it if the connection is shared, instead of modifying the global forwarding. Restore the forwarding value to the default one when the device is deconfigured for any reason. Fixes: a8a2e6d727f0 ('ip-config: Support configuring per-device IPv4 sysctl forwarding option') --- src/core/devices/nm-device.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index b9254516bd..dd481180df 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -13845,6 +13845,32 @@ _dev_ipshared6_start(NMDevice *self) /*****************************************************************************/ +static void +_dev_ipforwarding_cleanup(NMDevice *self, int addr_family, CleanupType cleanup_type) +{ + gs_free const char *default_forwarding = NULL; + gs_free const char *current_forwarding = NULL; + + if (!NM_IS_IPv4(addr_family)) + return; + + if (!NM_IN_SET(cleanup_type, CLEANUP_TYPE_DECONFIGURE, CLEANUP_TYPE_KEEP_REAPPLY)) + return; + + default_forwarding = nm_platform_sysctl_get( + nm_device_get_platform(self), + NMP_SYSCTL_PATHID_ABSOLUTE("/proc/sys/net/ipv4/conf/default/forwarding")); + + if (!default_forwarding) + return; /* Non fatal */ + + current_forwarding = nm_device_sysctl_ip_conf_get(self, AF_INET, "forwarding"); + if (!nm_streq0(current_forwarding, default_forwarding)) + nm_device_sysctl_ip_conf_set(self, AF_INET, "forwarding", default_forwarding); +} + +/*****************************************************************************/ + static void act_request_set(NMDevice *self, NMActRequest *act_request) { @@ -13957,6 +13983,8 @@ _cleanup_ip_pre(NMDevice *self, int addr_family, CleanupType cleanup_type, gbool NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE(self); gboolean keep_reapply = (cleanup_type == CLEANUP_TYPE_KEEP_REAPPLY); + _dev_ipforwarding_cleanup(self, addr_family, cleanup_type); + _dev_ipsharedx_cleanup(self, addr_family); _dev_ipdev_cleanup(self, AF_UNSPEC);