From 5e3ebdedd46817464682fad129802cb6a396e76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8D=C3=B1igo=20Huguet?= Date: Tue, 31 Oct 2023 13:23:22 +0100 Subject: [PATCH] auto-default: check NM_AUTO_DEFAULT_LINK_LOCAL_ONLY from nm-device When creating default connections automatically, we check if udev has set the NM_AUTO_DEFAULT_LINK_LOCAL_ONLY variable, and if so, we create the connection with method=link-local. It was checked only for ethernet connection type, which works fine because it's the only device type that we create connections automatically for. However, link-local connections are not specific to Ethernet, and if we add auto-default connections for more devices in the future we should honor this configuration too. Do it from nm-device, but only if the device class has defined a "new_default_connection" method so the behaviour is identical as the current one, but will be used by future implementors of this method too. --- src/core/devices/nm-device-ethernet.c | 26 ------------------- src/core/devices/nm-device.c | 36 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/core/devices/nm-device-ethernet.c b/src/core/devices/nm-device-ethernet.c index aedacc248b..5d58b69d67 100644 --- a/src/core/devices/nm-device-ethernet.c +++ b/src/core/devices/nm-device-ethernet.c @@ -1711,10 +1711,8 @@ new_default_connection(NMDevice *self) NMSettingsConnection *const *connections; NMSetting *setting; gs_unref_hashtable GHashTable *existing_ids = NULL; - struct udev_device *dev; const char *perm_hw_addr; const char *iface; - const char *uprop = "0"; gs_free char *defname = NULL; gs_free char *uuid = NULL; guint i, n_connections; @@ -1760,30 +1758,6 @@ new_default_connection(NMDevice *self) iface, NULL); - /* Check if we should create a Link-Local only connection */ - dev = nm_platform_link_get_udev_device(nm_device_get_platform(NM_DEVICE(self)), - nm_device_get_ip_ifindex(self)); - if (dev) - uprop = udev_device_get_property_value(dev, "NM_AUTO_DEFAULT_LINK_LOCAL_ONLY"); - - if (_nm_utils_ascii_str_to_bool(uprop, FALSE)) { - setting = nm_setting_ip4_config_new(); - g_object_set(setting, - NM_SETTING_IP_CONFIG_METHOD, - NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, - NULL); - nm_connection_add_setting(connection, setting); - - setting = nm_setting_ip6_config_new(); - g_object_set(setting, - NM_SETTING_IP_CONFIG_METHOD, - NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, - NM_SETTING_IP_CONFIG_MAY_FAIL, - TRUE, - NULL); - nm_connection_add_setting(connection, setting); - } - return connection; } diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index 71b597fb34..c3886b7db0 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "libnm-std-aux/unaligned.h" #include "libnm-glib-aux/nm-uuid.h" @@ -8098,6 +8099,39 @@ nm_device_owns_iface(NMDevice *self, const char *iface) return FALSE; } +static void +apply_udev_auto_default_configs(NMDevice *self, NMConnection *connection) +{ + struct udev_device *dev; + const char *uprop; + NMSetting *setting; + + dev = nm_platform_link_get_udev_device(nm_device_get_platform(NM_DEVICE(self)), + nm_device_get_ip_ifindex(self)); + if (!dev) + return; + + uprop = udev_device_get_property_value(dev, "NM_AUTO_DEFAULT_LINK_LOCAL_ONLY"); + + if (_nm_utils_ascii_str_to_bool(uprop, FALSE)) { + setting = nm_setting_ip4_config_new(); + g_object_set(setting, + NM_SETTING_IP_CONFIG_METHOD, + NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, + NULL); + nm_connection_add_setting(connection, setting); + + setting = nm_setting_ip6_config_new(); + g_object_set(setting, + NM_SETTING_IP_CONFIG_METHOD, + NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, + TRUE, + NULL); + nm_connection_add_setting(connection, setting); + } +} + NMConnection * nm_device_new_default_connection(NMDevice *self) { @@ -8111,6 +8145,8 @@ nm_device_new_default_connection(NMDevice *self) if (!connection) return NULL; + apply_udev_auto_default_configs(self, connection); + if (!nm_connection_normalize(connection, NULL, NULL, &error)) { _LOGD(LOGD_DEVICE, "device generated an invalid default connection: %s", error->message); g_error_free(error);