diff --git a/src/devices/nm-device-tun.c b/src/devices/nm-device-tun.c index 15bcacb0b..ea70f1a53 100644 --- a/src/devices/nm-device-tun.c +++ b/src/devices/nm-device-tun.c @@ -80,7 +80,7 @@ reload_tun_properties (NMDeviceTun *self) ifindex = nm_device_get_ifindex (NM_DEVICE (self)); if (ifindex > 0) { - if (!nm_platform_link_tun_get_properties (NM_PLATFORM_GET, ifindex, &props)) { + if (!nm_platform_link_tun_get_properties (NM_PLATFORM_GET, ifindex, NULL, &props)) { _LOGD (LOGD_DEVICE, "tun-properties: cannot loading tun properties from platform for ifindex %d", ifindex); ifindex = 0; } else if (g_strcmp0 (priv->mode, props.mode) != 0) { @@ -181,7 +181,7 @@ update_connection (NMDevice *device, NMConnection *connection) nm_connection_add_setting (connection, (NMSetting *) s_tun); } - if (!nm_platform_link_tun_get_properties (NM_PLATFORM_GET, nm_device_get_ifindex (device), &props)) { + if (!nm_platform_link_tun_get_properties (NM_PLATFORM_GET, nm_device_get_ifindex (device), NULL, &props)) { _LOGW (LOGD_PLATFORM, "failed to get TUN interface info while updating connection."); return; } diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 82c644870..497f34e87 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -656,7 +656,7 @@ _linktype_get_type (NMPlatform *platform, NMPlatformTunProperties props; if ( platform - && nm_platform_link_tun_get_properties_ifname (platform, ifname, &props)) { + && nm_platform_link_tun_get_properties (platform, ifindex, ifname ?: "", &props)) { if (!g_strcmp0 (props.mode, "tap")) return NM_LINK_TYPE_TAP; if (!g_strcmp0 (props.mode, "tun")) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 726fe5190..7cc675d3a 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -2259,74 +2259,59 @@ nm_platform_link_veth_get_properties (NMPlatform *self, int ifindex, int *out_pe } gboolean -nm_platform_link_tun_get_properties_ifname (NMPlatform *self, const char *ifname, NMPlatformTunProperties *props) +nm_platform_link_tun_get_properties (NMPlatform *self, int ifindex, const char *ifname_guess, NMPlatformTunProperties *props) { - char path[256]; - char *val; + nm_auto_close int dirfd = -1; + const char *ifname; + char ifname_verified[IFNAMSIZ]; + gint64 flags; gboolean success = TRUE; - _CHECK_SELF (self, klass, FALSE); + g_return_val_if_fail (ifindex > 0, FALSE); g_return_val_if_fail (props, FALSE); + /* ifname_guess is an optional argument to find a guess for the ifname corresponding to + * ifindex. */ + if (!ifname_guess) { + /* if NULL, obtain the guess from the platform cache. */ + ifname = nm_platform_link_get_name (self, ifindex); + } else if (!ifname_guess[0]) { + /* if empty, don't use a guess. That means to use if_indextoname(). */ + ifname = NULL; + } else + ifname = ifname_guess; + memset (props, 0, sizeof (*props)); props->owner = -1; props->group = -1; - if (!ifname || !nm_utils_iface_valid_name (ifname)) + dirfd = nm_platform_sysctl_open_netdir (self, ifindex, ifname_verified); + if (dirfd < 0) return FALSE; - nm_sprintf_buf (path, "/sys/class/net/%s/owner", ifname); - val = nm_platform_sysctl_get (self, NMP_SYSCTL_PATHID_ABSOLUTE (path)); - if (val) { - props->owner = _nm_utils_ascii_str_to_int64 (val, 10, -1, G_MAXINT64, -1); - if (errno) - success = FALSE; - g_free (val); - } else + ifname = ifname_verified; + + props->owner = nm_platform_sysctl_get_int_checked (self, NMP_SYSCTL_PATHID_NETDIR (dirfd, ifname, "owner"), 10, -1, G_MAXINT64, -1); + if (errno) success = FALSE; - nm_sprintf_buf (path, "/sys/class/net/%s/group", ifname); - val = nm_platform_sysctl_get (self, NMP_SYSCTL_PATHID_ABSOLUTE (path)); - if (val) { - props->group = _nm_utils_ascii_str_to_int64 (val, 10, -1, G_MAXINT64, -1); - if (errno) - success = FALSE; - g_free (val); - } else + props->group = nm_platform_sysctl_get_int_checked (self, NMP_SYSCTL_PATHID_NETDIR (dirfd, ifname, "group"), 10, -1, G_MAXINT64, -1); + if (errno) success = FALSE; - nm_sprintf_buf (path, "/sys/class/net/%s/tun_flags", ifname); - val = nm_platform_sysctl_get (self, NMP_SYSCTL_PATHID_ABSOLUTE (path)); - if (val) { - gint64 flags; - - flags = _nm_utils_ascii_str_to_int64 (val, 16, 0, G_MAXINT64, 0); - if (!errno) { - props->mode = ((flags & (IFF_TUN | IFF_TAP)) == IFF_TUN) ? "tun" : "tap"; - props->no_pi = !!(flags & IFF_NO_PI); - props->vnet_hdr = !!(flags & IFF_VNET_HDR); - props->multi_queue = !!(flags & NM_IFF_MULTI_QUEUE); - } else - success = FALSE; - g_free (val); + flags = nm_platform_sysctl_get_int_checked (self, NMP_SYSCTL_PATHID_NETDIR (dirfd, ifname, "tun_flags"), 16, 0, G_MAXINT64, -1); + if (flags >= 0) { + props->mode = ((flags & (IFF_TUN | IFF_TAP)) == IFF_TUN) ? "tun" : "tap"; + props->no_pi = !!(flags & IFF_NO_PI); + props->vnet_hdr = !!(flags & IFF_VNET_HDR); + props->multi_queue = !!(flags & NM_IFF_MULTI_QUEUE); } else success = FALSE; return success; } -gboolean -nm_platform_link_tun_get_properties (NMPlatform *self, int ifindex, NMPlatformTunProperties *props) -{ - _CHECK_SELF (self, klass, FALSE); - - g_return_val_if_fail (ifindex > 0, FALSE); - g_return_val_if_fail (props != NULL, FALSE); - - return nm_platform_link_tun_get_properties_ifname (self, nm_platform_link_get_name (self, ifindex), props); -} - gboolean nm_platform_wifi_get_capabilities (NMPlatform *self, int ifindex, NMDeviceWifiCapabilities *caps) { diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index b2afce334..2e9dbcc84 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -865,9 +865,7 @@ NMPlatformError nm_platform_link_infiniband_delete (NMPlatform *self, gboolean nm_platform_link_infiniband_get_properties (NMPlatform *self, int ifindex, int *parent, int *p_key, const char **mode); gboolean nm_platform_link_veth_get_properties (NMPlatform *self, int ifindex, int *out_peer_ifindex); -gboolean nm_platform_link_tun_get_properties (NMPlatform *self, int ifindex, NMPlatformTunProperties *properties); - -gboolean nm_platform_link_tun_get_properties_ifname (NMPlatform *platform, const char *ifname, NMPlatformTunProperties *props); +gboolean nm_platform_link_tun_get_properties (NMPlatform *self, int ifindex, const char *ifname_guess, NMPlatformTunProperties *properties); gboolean nm_platform_wifi_get_capabilities (NMPlatform *self, int ifindex, NMDeviceWifiCapabilities *caps); gboolean nm_platform_wifi_get_bssid (NMPlatform *self, int ifindex, guint8 *bssid);