diff options
| author | Michael Biebl <biebl@debian.org> | 2017-01-17 20:25:09 +0100 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2017-01-17 20:25:09 +0100 |
| commit | 58f8be580039b0575b197b9573a1c92745d96d30 (patch) | |
| tree | 2c226233f623a0dcb529be0eb8cdf97e4a2ae0c0 /src/platform/nm-platform-utils.c | |
| parent | 45cb5bb3c0e6edb887cf69b417fcaf7053814a9b (diff) | |
New upstream version 1.5.90 upstream/1.5.90
Diffstat (limited to 'src/platform/nm-platform-utils.c')
| -rw-r--r-- | src/platform/nm-platform-utils.c | 370 |
1 files changed, 245 insertions, 125 deletions
diff --git a/src/platform/nm-platform-utils.c b/src/platform/nm-platform-utils.c index b939e783..65fb01bc 100644 --- a/src/platform/nm-platform-utils.c +++ b/src/platform/nm-platform-utils.c @@ -38,60 +38,139 @@ #include "nm-core-utils.h" +/****************************************************************** + * utils + ******************************************************************/ + extern char *if_indextoname (unsigned int __ifindex, char *__ifname); +unsigned int if_nametoindex (const char *__ifname); + +const char * +nmp_utils_if_indextoname (int ifindex, char *out_ifname/*IFNAMSIZ*/) +{ + g_return_val_if_fail (ifindex > 0, NULL); + g_return_val_if_fail (out_ifname, NULL); + + return if_indextoname (ifindex, out_ifname); +} + +int +nmp_utils_if_nametoindex (const char *ifname) +{ + g_return_val_if_fail (ifname, 0); + + return if_nametoindex (ifname); +} /****************************************************************** * ethtool ******************************************************************/ -static gboolean -ethtool_get (const char *name, gpointer edata) +NM_UTILS_ENUM2STR_DEFINE_STATIC (_ethtool_cmd_to_string, guint32, + NM_UTILS_ENUM2STR (ETHTOOL_GDRVINFO, "ETHTOOL_GDRVINFO"), + NM_UTILS_ENUM2STR (ETHTOOL_GFEATURES, "ETHTOOL_GFEATURES"), + NM_UTILS_ENUM2STR (ETHTOOL_GLINK, "ETHTOOL_GLINK"), + NM_UTILS_ENUM2STR (ETHTOOL_GPERMADDR, "ETHTOOL_GPERMADDR"), + NM_UTILS_ENUM2STR (ETHTOOL_GSET, "ETHTOOL_GSET"), + NM_UTILS_ENUM2STR (ETHTOOL_GSSET_INFO, "ETHTOOL_GSSET_INFO"), + NM_UTILS_ENUM2STR (ETHTOOL_GSTATS, "ETHTOOL_GSTATS"), + NM_UTILS_ENUM2STR (ETHTOOL_GSTRINGS, "ETHTOOL_GSTRINGS"), + NM_UTILS_ENUM2STR (ETHTOOL_GWOL, "ETHTOOL_GWOL"), + NM_UTILS_ENUM2STR (ETHTOOL_SSET, "ETHTOOL_SSET"), + NM_UTILS_ENUM2STR (ETHTOOL_SWOL, "ETHTOOL_SWOL"), +); + +static const char * +_ethtool_data_to_string (gconstpointer edata, char *buf, gsize len) { - struct ifreq ifr; - int fd; + return _ethtool_cmd_to_string (*((guint32 *) edata), buf, len); +} - if (!name || !*name) - return FALSE; +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27) +#define ethtool_cmd_speed(pedata) ((pedata)->speed) - if (!nmp_utils_device_exists (name)) - return FALSE; +#define ethtool_cmd_speed_set(pedata, speed) \ + G_STMT_START { (pedata)->speed = (guint16) (speed); } G_STMT_END +#endif - /* nmp_utils_device_exists() already errors out if @name is invalid. */ - nm_assert (strlen (name) < IFNAMSIZ); +static gboolean +ethtool_get (int ifindex, gpointer edata) +{ + char ifname[IFNAMSIZ]; + char sbuf[50]; - memset (&ifr, 0, sizeof (ifr)); - nm_utils_ifname_cpy (ifr.ifr_name, name); - ifr.ifr_data = edata; + nm_assert (ifindex > 0); - fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (fd < 0) { - nm_log_err (LOGD_PLATFORM, "ethtool: Could not open socket."); + /* ethtool ioctl API uses the ifname to refer to an interface. That is racy + * as interfaces can be renamed *sigh*. + * + * Note that we anyway have to verify whether the interface exists, before + * calling ioctl for a non-existing ifname. This is to prevent autoloading + * of kernel modules *sigh*. + * Thus, as we anyway verify the existence of ifname before doing the call, + * go one step further and lookup the ifname everytime anew. + * + * This does not solve the renaming race, but it minimizes the time for + * the race to happen as much as possible. */ + + if (!nmp_utils_if_indextoname (ifindex, ifname)) { + nm_log_trace (LOGD_PLATFORM, "ethtool[%d]: %s: request fails resolving ifindex: %s", + ifindex, + _ethtool_data_to_string (edata, sbuf, sizeof (sbuf)), + g_strerror (errno)); return FALSE; } - if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) { - nm_log_dbg (LOGD_PLATFORM, "ethtool: Request failed: %s", strerror (errno)); - close (fd); - return FALSE; - } + { + nm_auto_close int fd = -1; + struct ifreq ifr = { + .ifr_data = edata, + }; + + memcpy (ifr.ifr_name, ifname, sizeof (ifname)); + + fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (fd < 0) { + nm_log_trace (LOGD_PLATFORM, "ethtool[%d]: %s, %s: failed creating socket for ioctl: %s", + ifindex, + _ethtool_data_to_string (edata, sbuf, sizeof (sbuf)), + ifname, + g_strerror (errno)); + return FALSE; + } - close (fd); - return TRUE; + if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) { + nm_log_trace (LOGD_PLATFORM, "ethtool[%d]: %s, %s: failed: %s", + ifindex, + _ethtool_data_to_string (edata, sbuf, sizeof (sbuf)), + ifname, + strerror (errno)); + return FALSE; + } + + nm_log_trace (LOGD_PLATFORM, "ethtool[%d]: %s, %s: success", + ifindex, + _ethtool_data_to_string (edata, sbuf, sizeof (sbuf)), + ifname); + return TRUE; + } } static int -ethtool_get_stringset_index (const char *ifname, int stringset_id, const char *string) +ethtool_get_stringset_index (int ifindex, int stringset_id, const char *string) { gs_free struct ethtool_sset_info *info = NULL; gs_free struct ethtool_gstrings *strings = NULL; guint32 len, i; + g_return_val_if_fail (ifindex > 0, -1); + info = g_malloc0 (sizeof (*info) + sizeof (guint32)); info->cmd = ETHTOOL_GSSET_INFO; info->reserved = 0; info->sset_mask = 1ULL << stringset_id; - if (!ethtool_get (ifname, info)) + if (!ethtool_get (ifindex, info)) return -1; if (!info->sset_mask) return -1; @@ -102,7 +181,7 @@ ethtool_get_stringset_index (const char *ifname, int stringset_id, const char *s strings->cmd = ETHTOOL_GSTRINGS; strings->string_set = stringset_id; strings->len = len; - if (!ethtool_get (ifname, strings)) + if (!ethtool_get (ifindex, strings)) return -1; for (i = 0; i < len; i++) { @@ -114,32 +193,30 @@ ethtool_get_stringset_index (const char *ifname, int stringset_id, const char *s } gboolean -nmp_utils_ethtool_get_driver_info (const char *ifname, - char **out_driver_name, - char **out_driver_version, - char **out_fw_version) +nmp_utils_ethtool_get_driver_info (int ifindex, + NMPUtilsEthtoolDriverInfo *data) { - struct ethtool_drvinfo drvinfo = { 0 }; - - if (!ifname) - return FALSE; - - drvinfo.cmd = ETHTOOL_GDRVINFO; - if (!ethtool_get (ifname, &drvinfo)) - return FALSE; - - if (out_driver_name) - *out_driver_name = g_strdup (drvinfo.driver); - if (out_driver_version) - *out_driver_version = g_strdup (drvinfo.version); - if (out_fw_version) - *out_fw_version = g_strdup (drvinfo.fw_version); - - return TRUE; + struct ethtool_drvinfo *drvinfo; + G_STATIC_ASSERT (sizeof (*data) == sizeof (*drvinfo)); + G_STATIC_ASSERT (offsetof (NMPUtilsEthtoolDriverInfo, driver) == offsetof (struct ethtool_drvinfo, driver)); + G_STATIC_ASSERT (offsetof (NMPUtilsEthtoolDriverInfo, version) == offsetof (struct ethtool_drvinfo, version)); + G_STATIC_ASSERT (offsetof (NMPUtilsEthtoolDriverInfo, fw_version) == offsetof (struct ethtool_drvinfo, fw_version)); + G_STATIC_ASSERT (sizeof (data->driver) == sizeof (drvinfo->driver)); + G_STATIC_ASSERT (sizeof (data->version) == sizeof (drvinfo->version)); + G_STATIC_ASSERT (sizeof (data->fw_version) == sizeof (drvinfo->fw_version)); + + g_return_val_if_fail (ifindex > 0, FALSE); + g_return_val_if_fail (data, FALSE); + + drvinfo = (struct ethtool_drvinfo *) data; + + memset (drvinfo, 0, sizeof (*drvinfo)); + drvinfo->cmd = ETHTOOL_GDRVINFO; + return ethtool_get (ifindex, drvinfo); } gboolean -nmp_utils_ethtool_get_permanent_address (const char *ifname, +nmp_utils_ethtool_get_permanent_address (int ifindex, guint8 *buf, size_t *length) { @@ -149,14 +226,13 @@ nmp_utils_ethtool_get_permanent_address (const char *ifname, } edata; guint i; - if (!ifname) - return FALSE; + g_return_val_if_fail (ifindex > 0, FALSE); memset (&edata, 0, sizeof (edata)); edata.e.cmd = ETHTOOL_GPERMADDR; edata.e.size = NM_UTILS_HWADDR_LEN_MAX; - if (!ethtool_get (ifname, &edata.e)) + if (!ethtool_get (ifindex, &edata.e)) return FALSE; if (edata.e.size > NM_UTILS_HWADDR_LEN_MAX) @@ -183,29 +259,30 @@ not_all_0or1: } gboolean -nmp_utils_ethtool_supports_carrier_detect (const char *ifname) +nmp_utils_ethtool_supports_carrier_detect (int ifindex) { struct ethtool_cmd edata = { .cmd = ETHTOOL_GLINK }; + g_return_val_if_fail (ifindex > 0, FALSE); + /* We ignore the result. If the ETHTOOL_GLINK call succeeded, then we * assume the device supports carrier-detect, otherwise we assume it * doesn't. */ - return ethtool_get (ifname, &edata); + return ethtool_get (ifindex, &edata); } gboolean -nmp_utils_ethtool_supports_vlans (const char *ifname) +nmp_utils_ethtool_supports_vlans (int ifindex) { gs_free struct ethtool_gfeatures *features = NULL; int idx, block, bit, size; - if (!ifname) - return FALSE; + g_return_val_if_fail (ifindex > 0, FALSE); - idx = ethtool_get_stringset_index (ifname, ETH_SS_FEATURES, "vlan-challenged"); + idx = ethtool_get_stringset_index (ifindex, ETH_SS_FEATURES, "vlan-challenged"); if (idx == -1) { - nm_log_dbg (LOGD_PLATFORM, "ethtool: vlan-challenged ethtool feature does not exist for %s?", ifname); + nm_log_dbg (LOGD_PLATFORM, "ethtool: vlan-challenged ethtool feature does not exist for %d?", ifindex); return FALSE; } @@ -217,83 +294,148 @@ nmp_utils_ethtool_supports_vlans (const char *ifname) features->cmd = ETHTOOL_GFEATURES; features->size = size; - if (!ethtool_get (ifname, features)) + if (!ethtool_get (ifindex, features)) return FALSE; return !(features->features[block].active & (1 << bit)); } int -nmp_utils_ethtool_get_peer_ifindex (const char *ifname) +nmp_utils_ethtool_get_peer_ifindex (int ifindex) { gs_free struct ethtool_stats *stats = NULL; int peer_ifindex_stat; - if (!ifname) - return 0; + g_return_val_if_fail (ifindex > 0, 0); - peer_ifindex_stat = ethtool_get_stringset_index (ifname, ETH_SS_STATS, "peer_ifindex"); + peer_ifindex_stat = ethtool_get_stringset_index (ifindex, ETH_SS_STATS, "peer_ifindex"); if (peer_ifindex_stat == -1) { - nm_log_dbg (LOGD_PLATFORM, "ethtool: peer_ifindex stat for %s does not exist?", ifname); + nm_log_dbg (LOGD_PLATFORM, "ethtool: peer_ifindex stat for %d does not exist?", ifindex); return FALSE; } stats = g_malloc0 (sizeof (*stats) + (peer_ifindex_stat + 1) * sizeof (guint64)); stats->cmd = ETHTOOL_GSTATS; stats->n_stats = peer_ifindex_stat + 1; - if (!ethtool_get (ifname, stats)) + if (!ethtool_get (ifindex, stats)) return 0; return stats->data[peer_ifindex_stat]; } gboolean -nmp_utils_ethtool_get_wake_on_lan (const char *ifname) +nmp_utils_ethtool_get_wake_on_lan (int ifindex) { struct ethtool_wolinfo wol; - if (!ifname) - return FALSE; + g_return_val_if_fail (ifindex > 0, FALSE); memset (&wol, 0, sizeof (wol)); wol.cmd = ETHTOOL_GWOL; - if (!ethtool_get (ifname, &wol)) + if (!ethtool_get (ifindex, &wol)) return FALSE; return wol.wolopts != 0; } gboolean -nmp_utils_ethtool_get_link_speed (const char *ifname, guint32 *out_speed) +nmp_utils_ethtool_get_link_settings (int ifindex, + gboolean *out_autoneg, + guint32 *out_speed, + NMPlatformLinkDuplexType *out_duplex) { struct ethtool_cmd edata = { .cmd = ETHTOOL_GSET, }; - guint32 speed; - if (!ethtool_get (ifname, &edata)) + g_return_val_if_fail (ifindex > 0, FALSE); + + if (!ethtool_get (ifindex, &edata)) return FALSE; -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27) - speed = edata.speed; -#else - speed = ethtool_cmd_speed (&edata); -#endif - if (speed == G_MAXUINT16 || speed == G_MAXUINT32) - speed = 0; + if (out_autoneg) + *out_autoneg = (edata.autoneg == AUTONEG_ENABLE); + + if (out_speed) { + guint32 speed; + + speed = ethtool_cmd_speed (&edata); + if (speed == G_MAXUINT16 || speed == G_MAXUINT32) + speed = 0; - if (out_speed) *out_speed = speed; + } + + if (out_duplex) { + switch (edata.duplex) { + case DUPLEX_HALF: + *out_duplex = NM_PLATFORM_LINK_DUPLEX_HALF; + break; + case DUPLEX_FULL: + *out_duplex = NM_PLATFORM_LINK_DUPLEX_FULL; + break; + default: /* DUPLEX_UNKNOWN */ + *out_duplex = NM_PLATFORM_LINK_DUPLEX_UNKNOWN; + break; + } + } + return TRUE; } gboolean -nmp_utils_ethtool_set_wake_on_lan (const char *ifname, +nmp_utils_ethtool_set_link_settings (int ifindex, + gboolean autoneg, + guint32 speed, + NMPlatformLinkDuplexType duplex) +{ + struct ethtool_cmd edata = { + .cmd = ETHTOOL_GSET, + }; + + g_return_val_if_fail (ifindex > 0, FALSE); + + /* retrieve first current settings */ + if (!ethtool_get (ifindex, &edata)) + return FALSE; + + /* then change the needed ones */ + edata.cmd = ETHTOOL_SSET; + if (autoneg) { + edata.autoneg = AUTONEG_ENABLE; + edata.advertising = edata.supported; + } else { + edata.autoneg = AUTONEG_DISABLE; + + if (speed) + ethtool_cmd_speed_set (&edata, speed); + + switch (duplex) { + case NM_PLATFORM_LINK_DUPLEX_HALF: + edata.duplex = DUPLEX_HALF; + break; + case NM_PLATFORM_LINK_DUPLEX_FULL: + edata.duplex = DUPLEX_FULL; + break; + case NM_PLATFORM_LINK_DUPLEX_UNKNOWN: + break; + default: + g_return_val_if_reached (FALSE); + } + } + + return ethtool_get (ifindex, &edata); +} + +gboolean +nmp_utils_ethtool_set_wake_on_lan (int ifindex, NMSettingWiredWakeOnLan wol, const char *wol_password) { struct ethtool_wolinfo wol_info = { }; + g_return_val_if_fail (ifindex > 0, FALSE); + if (wol == NM_SETTING_WIRED_WAKE_ON_LAN_IGNORE) return TRUE; @@ -324,7 +466,7 @@ nmp_utils_ethtool_set_wake_on_lan (const char *ifname, wol_info.wolopts |= WAKE_MAGICSECURE; } - return ethtool_get (ifname, &wol_info); + return ethtool_get (ifindex, &wol_info); } /****************************************************************** @@ -332,51 +474,45 @@ nmp_utils_ethtool_set_wake_on_lan (const char *ifname, ******************************************************************/ gboolean -nmp_utils_mii_supports_carrier_detect (const char *ifname) +nmp_utils_mii_supports_carrier_detect (int ifindex) { - int fd, errsv; + char ifname[IFNAMSIZ]; + nm_auto_close int fd = -1; struct ifreq ifr; struct mii_ioctl_data *mii; - gboolean supports_mii = FALSE; - if (!ifname) - return FALSE; + g_return_val_if_fail (ifindex > 0, FALSE); - if (!nmp_utils_device_exists (ifname)) + if (!nmp_utils_if_indextoname (ifindex, ifname)) { + nm_log_trace (LOGD_PLATFORM, "mii[%d]: carrier-detect no: request fails resolving ifindex: %s", ifindex, g_strerror (errno)); return FALSE; + } fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (fd < 0) { - nm_log_err (LOGD_PLATFORM, "mii: couldn't open control socket (%s)", ifname); + nm_log_trace (LOGD_PLATFORM, "mii[%d,%s]: carrier-detect no: couldn't open control socket: %s", ifindex, ifname, g_strerror (errno)); return FALSE; } memset (&ifr, 0, sizeof (struct ifreq)); - nm_utils_ifname_cpy (ifr.ifr_name, ifname); + memcpy (ifr.ifr_name, ifname, IFNAMSIZ); - errno = 0; if (ioctl (fd, SIOCGMIIPHY, &ifr) < 0) { - errsv = errno; - nm_log_dbg (LOGD_PLATFORM, "mii: SIOCGMIIPHY failed: %s (%d) (%s)", strerror (errsv), errsv, ifname); - goto out; + nm_log_trace (LOGD_PLATFORM, "mii[%d,%s]: carrier-detect no: SIOCGMIIPHY failed: %s", ifindex, ifname, strerror (errno)); + return FALSE; } /* If we can read the BMSR register, we assume that the card supports MII link detection */ mii = (struct mii_ioctl_data *) &ifr.ifr_ifru; mii->reg_num = MII_BMSR; - if (ioctl (fd, SIOCGMIIREG, &ifr) == 0) { - nm_log_dbg (LOGD_PLATFORM, "mii: SIOCGMIIREG result 0x%X (%s)", mii->val_out, ifname); - supports_mii = TRUE; - } else { - errsv = errno; - nm_log_dbg (LOGD_PLATFORM, "mii: SIOCGMIIREG failed: %s (%d) (%s)", strerror (errsv), errsv, ifname); + if (ioctl (fd, SIOCGMIIREG, &ifr) != 0) { + nm_log_trace (LOGD_PLATFORM, "mii[%d,%s]: carrier-detect no: SIOCGMIIREG failed: %s", ifindex, ifname, strerror (errno)); + return FALSE; } -out: - close (fd); - nm_log_dbg (LOGD_PLATFORM, "mii: MII %s supported (%s)", supports_mii ? "is" : "not", ifname); - return supports_mii; + nm_log_trace (LOGD_PLATFORM, "mii[%d,%s]: carrier-detect yes: SIOCGMIIREG result 0x%X", ifindex, ifname, mii->val_out); + return TRUE; } /****************************************************************** @@ -424,22 +560,6 @@ out: * utils *****************************************************************************/ -gboolean -nmp_utils_device_exists (const char *name) -{ -#define SYS_CLASS_NET "/sys/class/net/" - char sysdir[NM_STRLEN (SYS_CLASS_NET) + IFNAMSIZ]; - - if ( !name - || strlen (name) >= IFNAMSIZ - || !nm_utils_is_valid_path_component (name)) - g_return_val_if_reached (FALSE); - - memcpy (sysdir, SYS_CLASS_NET, NM_STRLEN (SYS_CLASS_NET)); - nm_utils_ifname_cpy (&sysdir[NM_STRLEN (SYS_CLASS_NET)], name); - return g_file_test (sysdir, G_FILE_TEST_EXISTS); -} - NMIPConfigSource nmp_utils_ip_config_source_from_rtprot (guint8 rtprot) { @@ -472,7 +592,7 @@ nmp_utils_ip_config_source_coerce_to_rtprot (NMIPConfigSource source) return RTPROT_KERNEL; case NM_IP_CONFIG_SOURCE_DHCP: return RTPROT_DHCP; - case NM_IP_CONFIG_SOURCE_RDISC: + case NM_IP_CONFIG_SOURCE_NDISC: return RTPROT_RA; default: @@ -504,7 +624,7 @@ nmp_utils_ip_config_source_coerce_from_rtprot (NMIPConfigSource source) return NM_IP_CONFIG_SOURCE_KERNEL; case NM_IP_CONFIG_SOURCE_RTPROT_RA: - return NM_IP_CONFIG_SOURCE_RDISC; + return NM_IP_CONFIG_SOURCE_NDISC; case NM_IP_CONFIG_SOURCE_RTPROT_DHCP: return NM_IP_CONFIG_SOURCE_DHCP; @@ -541,7 +661,7 @@ nmp_utils_ip_config_source_to_string (NMIPConfigSource source, char *buf, gsize case NM_IP_CONFIG_SOURCE_WWAN: s = "wwan"; break; case NM_IP_CONFIG_SOURCE_VPN: s = "vpn"; break; case NM_IP_CONFIG_SOURCE_DHCP: s = "dhcp"; break; - case NM_IP_CONFIG_SOURCE_RDISC: s = "rdisc"; break; + case NM_IP_CONFIG_SOURCE_NDISC: s = "ndisc"; break; case NM_IP_CONFIG_SOURCE_USER: s = "user"; break; default: break; @@ -598,12 +718,12 @@ nmp_utils_sysctl_open_netdir (int ifindex, int fd; if (!ifname) { - ifname = if_indextoname (ifindex, ifname_buf); + ifname = nmp_utils_if_indextoname (ifindex, ifname_buf); if (!ifname) return -1; } - nm_assert (nm_utils_iface_valid_name (ifname)); + nm_assert (nm_utils_is_valid_iface_name (ifname, NULL)); if (g_strlcpy (&sysdir[NM_STRLEN (SYS_CLASS_NET)], ifname, IFNAMSIZ) >= IFNAMSIZ) g_return_val_if_reached (-1); |