diff options
Diffstat (limited to 'src/platform/wifi')
| -rw-r--r-- | src/platform/wifi/nm-wifi-utils-nl80211.c | 395 | ||||
| -rw-r--r-- | src/platform/wifi/nm-wifi-utils-wext.c | 44 | ||||
| -rw-r--r-- | src/platform/wifi/nm-wifi-utils.c | 1 | ||||
| -rw-r--r-- | src/platform/wifi/nm-wifi-utils.h | 2 |
4 files changed, 216 insertions, 226 deletions
diff --git a/src/platform/wifi/nm-wifi-utils-nl80211.c b/src/platform/wifi/nm-wifi-utils-nl80211.c index 39e3f971..4f7ede97 100644 --- a/src/platform/wifi/nm-wifi-utils-nl80211.c +++ b/src/platform/wifi/nm-wifi-utils-nl80211.c @@ -24,8 +24,6 @@ #include "nm-wifi-utils-nl80211.h" -#include <errno.h> -#include <string.h> #include <sys/ioctl.h> #include <net/ethernet.h> #include <unistd.h> @@ -39,11 +37,16 @@ #include "nm-utils.h" #define _NMLOG_PREFIX_NAME "wifi-nl80211" -#define _NMLOG(level, domain, ...) \ +#define _NMLOG_DOMAIN LOGD_PLATFORM | LOGD_WIFI +#define _NMLOG(level, ...) \ G_STMT_START { \ - nm_log ((level), (domain), NULL, NULL, \ - "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ - _NMLOG_PREFIX_NAME \ + char _ifname_buf[IFNAMSIZ]; \ + const char *_ifname = self ? nmp_utils_if_indextoname (self->parent.ifindex, _ifname_buf) : NULL; \ + \ + nm_log ((level), _NMLOG_DOMAIN, _ifname ?: NULL, NULL, \ + "%s%s%s%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ + _NMLOG_PREFIX_NAME, \ + NM_PRINT_FMT_QUOTED (_ifname, " (", _ifname, ")", "") \ _NM_UTILS_MACRO_REST(__VA_ARGS__)); \ } G_STMT_END @@ -104,16 +107,16 @@ nla_put_failure: } static struct nl_msg * -nl80211_alloc_msg (NMWifiUtilsNl80211 *nl80211, guint32 cmd, guint32 flags) +nl80211_alloc_msg (NMWifiUtilsNl80211 *self, guint32 cmd, guint32 flags) { - return _nl80211_alloc_msg (nl80211->id, nl80211->parent.ifindex, nl80211->phy, cmd, flags); + return _nl80211_alloc_msg (self->id, self->parent.ifindex, self->phy, cmd, flags); } static int -_nl80211_send_and_recv (struct nl_sock *nl_sock, - struct nl_msg *msg, - int (*valid_handler) (struct nl_msg *, void *), - void *valid_data) +nl80211_send_and_recv (NMWifiUtilsNl80211 *self, + struct nl_msg *msg, + int (*valid_handler) (struct nl_msg *, void *), + void *valid_data) { int err; int done = 0; @@ -130,7 +133,7 @@ _nl80211_send_and_recv (struct nl_sock *nl_sock, g_return_val_if_fail (msg != NULL, -ENOMEM); - err = nl_send_auto (nl_sock, msg); + err = nl_send_auto (self->nl_sock, msg); if (err < 0) return err; @@ -138,19 +141,18 @@ _nl80211_send_and_recv (struct nl_sock *nl_sock, * done will be 1, on error it will be < 0. */ while (!done) { - err = nl_recvmsgs (nl_sock, &cb); + err = nl_recvmsgs (self->nl_sock, &cb); if (err < 0 && err != -EAGAIN) { /* Kernel scan list can change while we are dumping it, as new scan * results from H/W can arrive. BSS info is assured to be consistent * and we don't need consistent view of whole scan list. Hence do * not warn on DUMP_INTR error for get scan command. */ - if (err == -NLE_DUMP_INTR && + if (err == -NME_NL_DUMP_INTR && genlmsg_hdr (nlmsg_hdr (msg))->cmd == NL80211_CMD_GET_SCAN) break; - _LOGW (LOGD_WIFI, "nl_recvmsgs() error: (%d) %s", - err, nl_geterror (err)); + _LOGW ("nl_recvmsgs() error: (%d) %s", err, nm_strerror (err)); break; } } @@ -160,22 +162,12 @@ _nl80211_send_and_recv (struct nl_sock *nl_sock, return err; } -static int -nl80211_send_and_recv (NMWifiUtilsNl80211 *nl80211, - struct nl_msg *msg, - int (*valid_handler) (struct nl_msg *, void *), - void *valid_data) -{ - return _nl80211_send_and_recv (nl80211->nl_sock, msg, - valid_handler, valid_data); -} - static void dispose (GObject *object) { - NMWifiUtilsNl80211 *nl80211 = NM_WIFI_UTILS_NL80211 (object); + NMWifiUtilsNl80211 *self = NM_WIFI_UTILS_NL80211 (object); - g_clear_pointer (&nl80211->freqs, g_free); + g_clear_pointer (&self->freqs, g_free); } struct nl80211_iface_info { @@ -189,8 +181,10 @@ nl80211_iface_info_handler (struct nl_msg *msg, void *arg) struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); struct nlattr *tb[NL80211_ATTR_MAX + 1]; - if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), - genlmsg_attrlen (gnlh, 0), NULL) < 0) + if (nla_parse_arr (tb, + genlmsg_attrdata (gnlh, 0), + genlmsg_attrlen (gnlh, 0), + NULL) < 0) return NL_SKIP; if (!tb[NL80211_ATTR_IFTYPE]) @@ -214,15 +208,15 @@ nl80211_iface_info_handler (struct nl_msg *msg, void *arg) static NM80211Mode wifi_nl80211_get_mode (NMWifiUtils *data) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; struct nl80211_iface_info iface_info = { .mode = NM_802_11_MODE_UNKNOWN, }; nm_auto_nlmsg struct nl_msg *msg = NULL; - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_INTERFACE, 0); + msg = nl80211_alloc_msg (self, NL80211_CMD_GET_INTERFACE, 0); - if (nl80211_send_and_recv (nl80211, msg, nl80211_iface_info_handler, + if (nl80211_send_and_recv (self, msg, nl80211_iface_info_handler, &iface_info) < 0) return NM_802_11_MODE_UNKNOWN; @@ -232,11 +226,11 @@ wifi_nl80211_get_mode (NMWifiUtils *data) static gboolean wifi_nl80211_set_mode (NMWifiUtils *data, const NM80211Mode mode) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; nm_auto_nlmsg struct nl_msg *msg = NULL; int err; - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_SET_INTERFACE, 0); + msg = nl80211_alloc_msg (self, NL80211_CMD_SET_INTERFACE, 0); switch (mode) { case NM_802_11_MODE_INFRA: @@ -252,7 +246,7 @@ wifi_nl80211_set_mode (NMWifiUtils *data, const NM80211Mode mode) g_assert_not_reached (); } - err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); + err = nl80211_send_and_recv (self, msg, NULL, NULL); return err >= 0; nla_put_failure: @@ -262,14 +256,14 @@ nla_put_failure: static gboolean wifi_nl80211_set_powersave (NMWifiUtils *data, guint32 powersave) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; nm_auto_nlmsg struct nl_msg *msg = NULL; int err; - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_SET_POWER_SAVE, 0); + msg = nl80211_alloc_msg (self, NL80211_CMD_SET_POWER_SAVE, 0); NLA_PUT_U32 (msg, NL80211_ATTR_PS_STATE, powersave == 1 ? NL80211_PS_ENABLED : NL80211_PS_DISABLED); - err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); + err = nl80211_send_and_recv (self, msg, NULL, NULL); return err >= 0; nla_put_failure: @@ -284,16 +278,18 @@ nl80211_get_wake_on_wlan_handler (struct nl_msg *msg, void *arg) struct nlattr *trig[NUM_NL80211_WOWLAN_TRIG]; struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); - nla_parse (attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), - genlmsg_attrlen(gnlh, 0), NULL); + nla_parse_arr (attrs, + genlmsg_attrdata(gnlh, 0), + genlmsg_attrlen(gnlh, 0), + NULL); if (!attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) return NL_SKIP; - nla_parse (trig, MAX_NL80211_WOWLAN_TRIG, - nla_data (attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), - nla_len (attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), - NULL); + nla_parse_arr (trig, + nla_data (attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), + nla_len (attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), + NULL); *wowl = NM_SETTING_WIRELESS_WAKE_ON_WLAN_NONE; if (trig[NL80211_WOWLAN_TRIG_ANY]) @@ -319,13 +315,13 @@ nl80211_get_wake_on_wlan_handler (struct nl_msg *msg, void *arg) static NMSettingWirelessWakeOnWLan wifi_nl80211_get_wake_on_wlan (NMWifiUtils *data) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; NMSettingWirelessWakeOnWLan wowl = NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE; nm_auto_nlmsg struct nl_msg *msg = NULL; - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_WOWLAN, 0); + msg = nl80211_alloc_msg (self, NL80211_CMD_GET_WOWLAN, 0); - nl80211_send_and_recv (nl80211, msg, nl80211_get_wake_on_wlan_handler, &wowl); + nl80211_send_and_recv (self, msg, nl80211_get_wake_on_wlan_handler, &wowl); return wowl; } @@ -333,7 +329,7 @@ wifi_nl80211_get_wake_on_wlan (NMWifiUtils *data) static gboolean wifi_nl80211_set_wake_on_wlan (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wowl) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; nm_auto_nlmsg struct nl_msg *msg = NULL; struct nlattr *triggers; int err; @@ -341,7 +337,7 @@ wifi_nl80211_set_wake_on_wlan (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wo if (wowl == NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE) return TRUE; - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_SET_WOWLAN, 0); + msg = nl80211_alloc_msg (self, NL80211_CMD_SET_WOWLAN, 0); if (!msg) return FALSE; @@ -364,7 +360,7 @@ wifi_nl80211_set_wake_on_wlan (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wo nla_nest_end(msg, triggers); - err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); + err = nl80211_send_and_recv (self, msg, NULL, NULL); return err >= 0; @@ -420,33 +416,35 @@ find_ssid (guint8 *ies, guint32 ies_len, static int nl80211_bss_dump_handler (struct nl_msg *msg, void *arg) { + static const struct nla_policy bss_policy[] = { + [NL80211_BSS_TSF] = { .type = NLA_U64 }, + [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, + [NL80211_BSS_BSSID] = { .minlen = ETH_ALEN }, + [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, + [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, + [NL80211_BSS_INFORMATION_ELEMENTS] = { }, + [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, + [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, + [NL80211_BSS_STATUS] = { .type = NLA_U32 }, + }; struct nl80211_bss_info *info = arg; struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); struct nlattr *tb[NL80211_ATTR_MAX + 1]; - struct nlattr *bss[NL80211_BSS_MAX + 1]; - static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { - [NL80211_BSS_TSF] = { .type = NLA_U64 }, - [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, - [NL80211_BSS_BSSID] = { }, - [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, - [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, - [NL80211_BSS_INFORMATION_ELEMENTS] = { }, - [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, - [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, - [NL80211_BSS_STATUS] = { .type = NLA_U32 }, - }; + struct nlattr *bss[G_N_ELEMENTS (bss_policy)]; guint32 status; - if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), - genlmsg_attrlen (gnlh, 0), NULL) < 0) + if (nla_parse_arr (tb, + genlmsg_attrdata (gnlh, 0), + genlmsg_attrlen (gnlh, 0), + NULL) < 0) return NL_SKIP; if (tb[NL80211_ATTR_BSS] == NULL) return NL_SKIP; - if (nla_parse_nested (bss, NL80211_BSS_MAX, - tb[NL80211_ATTR_BSS], - bss_policy)) + if (nla_parse_nested_arr (bss, + tb[NL80211_ATTR_BSS], + bss_policy)) return NL_SKIP; if (bss[NL80211_BSS_STATUS] == NULL) @@ -466,21 +464,22 @@ nl80211_bss_dump_handler (struct nl_msg *msg, void *arg) info->freq = nla_get_u32 (bss[NL80211_BSS_FREQUENCY]); if (bss[NL80211_BSS_SIGNAL_UNSPEC]) - info->beacon_signal = - nla_get_u8 (bss[NL80211_BSS_SIGNAL_UNSPEC]); + info->beacon_signal = nla_get_u8 (bss[NL80211_BSS_SIGNAL_UNSPEC]); if (bss[NL80211_BSS_SIGNAL_MBM]) - info->beacon_signal = - nl80211_xbm_to_percent (nla_get_u32 (bss[NL80211_BSS_SIGNAL_MBM]), 100); + info->beacon_signal = nl80211_xbm_to_percent (nla_get_u32 (bss[NL80211_BSS_SIGNAL_MBM]), 100); if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) { guint8 *ssid; guint32 ssid_len; find_ssid (nla_data (bss[NL80211_BSS_INFORMATION_ELEMENTS]), - nla_len (bss[NL80211_BSS_INFORMATION_ELEMENTS]), - &ssid, &ssid_len); - if (ssid && ssid_len && ssid_len <= sizeof (info->ssid)) { + nla_len (bss[NL80211_BSS_INFORMATION_ELEMENTS]), + &ssid, + &ssid_len); + if ( ssid + && ssid_len + && ssid_len <= sizeof (info->ssid)) { memcpy (info->ssid, ssid, ssid_len); info->ssid_len = ssid_len; } @@ -492,25 +491,25 @@ nl80211_bss_dump_handler (struct nl_msg *msg, void *arg) } static void -nl80211_get_bss_info (NMWifiUtilsNl80211 *nl80211, +nl80211_get_bss_info (NMWifiUtilsNl80211 *self, struct nl80211_bss_info *bss_info) { nm_auto_nlmsg struct nl_msg *msg = NULL; memset (bss_info, 0, sizeof (*bss_info)); - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_SCAN, NLM_F_DUMP); + msg = nl80211_alloc_msg (self, NL80211_CMD_GET_SCAN, NLM_F_DUMP); - nl80211_send_and_recv (nl80211, msg, nl80211_bss_dump_handler, bss_info); + nl80211_send_and_recv (self, msg, nl80211_bss_dump_handler, bss_info); } static guint32 wifi_nl80211_get_freq (NMWifiUtils *data) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; struct nl80211_bss_info bss_info; - nl80211_get_bss_info (nl80211, &bss_info); + nl80211_get_bss_info (self, &bss_info); return bss_info.freq; } @@ -518,12 +517,12 @@ wifi_nl80211_get_freq (NMWifiUtils *data) static guint32 wifi_nl80211_find_freq (NMWifiUtils *data, const guint32 *freqs) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; int i; - for (i = 0; i < nl80211->num_freqs; i++) { + for (i = 0; i < self->num_freqs; i++) { while (*freqs) { - if (nl80211->freqs[i] == *freqs) + if (self->freqs[i] == *freqs) return *freqs; freqs++; } @@ -534,10 +533,10 @@ wifi_nl80211_find_freq (NMWifiUtils *data, const guint32 *freqs) static gboolean wifi_nl80211_get_bssid (NMWifiUtils *data, guint8 *out_bssid) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; struct nl80211_bss_info bss_info; - nl80211_get_bss_info (nl80211, &bss_info); + nl80211_get_bss_info (self, &bss_info); if (bss_info.valid) memcpy (out_bssid, bss_info.bssid, ETH_ALEN); @@ -555,49 +554,50 @@ struct nl80211_station_info { static int nl80211_station_handler (struct nl_msg *msg, void *arg) { - struct nl80211_station_info *info = arg; - struct nlattr *tb[NL80211_ATTR_MAX + 1]; - struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); - struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; - struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; - static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { + static const struct nla_policy stats_policy[] = { [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, - [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, - [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, - [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, - [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, - [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, - [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, - [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, - [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, - [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, + [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, + [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, + [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, + [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, + [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, + [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED }, + [NL80211_STA_INFO_LLID] = { .type = NLA_U16 }, + [NL80211_STA_INFO_PLID] = { .type = NLA_U16 }, + [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, }; - - static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { - [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, - [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, + static const struct nla_policy rate_policy[] = { + [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, + [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, - [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, + [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, }; + struct nlattr *rinfo[G_N_ELEMENTS (rate_policy)]; + struct nlattr *sinfo[G_N_ELEMENTS (stats_policy)]; + struct nl80211_station_info *info = arg; + struct nlattr *tb[NL80211_ATTR_MAX + 1]; + struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); - if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), - genlmsg_attrlen (gnlh, 0), NULL) < 0) + if (nla_parse_arr (tb, + genlmsg_attrdata (gnlh, 0), + genlmsg_attrlen (gnlh, 0), + NULL) < 0) return NL_SKIP; if (tb[NL80211_ATTR_STA_INFO] == NULL) return NL_SKIP; - if (nla_parse_nested (sinfo, NL80211_STA_INFO_MAX, - tb[NL80211_ATTR_STA_INFO], - stats_policy)) + if (nla_parse_nested_arr (sinfo, + tb[NL80211_ATTR_STA_INFO], + stats_policy)) return NL_SKIP; if (sinfo[NL80211_STA_INFO_TX_BITRATE] == NULL) return NL_SKIP; - if (nla_parse_nested (rinfo, NL80211_RATE_INFO_MAX, - sinfo[NL80211_STA_INFO_TX_BITRATE], - rate_policy)) + if (nla_parse_nested_arr (rinfo, + sinfo[NL80211_STA_INFO_TX_BITRATE], + rate_policy)) return NL_SKIP; if (rinfo[NL80211_RATE_INFO_BITRATE] == NULL) @@ -616,7 +616,7 @@ nl80211_station_handler (struct nl_msg *msg, void *arg) } static void -nl80211_get_ap_info (NMWifiUtilsNl80211 *nl80211, +nl80211_get_ap_info (NMWifiUtilsNl80211 *self, struct nl80211_station_info *sta_info) { nm_auto_nlmsg struct nl_msg *msg = NULL; @@ -624,15 +624,15 @@ nl80211_get_ap_info (NMWifiUtilsNl80211 *nl80211, memset (sta_info, 0, sizeof (*sta_info)); - nl80211_get_bss_info (nl80211, &bss_info); + nl80211_get_bss_info (self, &bss_info); if (!bss_info.valid) return; - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_STATION, 0); + msg = nl80211_alloc_msg (self, NL80211_CMD_GET_STATION, 0); if (msg) { NLA_PUT (msg, NL80211_ATTR_MAC, ETH_ALEN, bss_info.bssid); - nl80211_send_and_recv (nl80211, msg, nl80211_station_handler, sta_info); + nl80211_send_and_recv (self, msg, nl80211_station_handler, sta_info); if (!sta_info->signal_valid) { /* Fall back to bss_info signal quality (both are in percent) */ sta_info->signal = bss_info.beacon_signal; @@ -648,10 +648,10 @@ nla_put_failure: static guint32 wifi_nl80211_get_rate (NMWifiUtils *data) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; struct nl80211_station_info sta_info; - nl80211_get_ap_info (nl80211, &sta_info); + nl80211_get_ap_info (self, &sta_info); return sta_info.txrate; } @@ -659,21 +659,21 @@ wifi_nl80211_get_rate (NMWifiUtils *data) static int wifi_nl80211_get_qual (NMWifiUtils *data) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; struct nl80211_station_info sta_info; - nl80211_get_ap_info (nl80211, &sta_info); + nl80211_get_ap_info (self, &sta_info); return sta_info.signal; } static gboolean wifi_nl80211_indicate_addressing_running (NMWifiUtils *data, gboolean running) { - NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + NMWifiUtilsNl80211 *self = (NMWifiUtilsNl80211 *) data; nm_auto_nlmsg struct nl_msg *msg = NULL; int err; - msg = nl80211_alloc_msg (nl80211, + msg = nl80211_alloc_msg (self, running ? 98 /* NL80211_CMD_CRIT_PROTOCOL_START */ : 99 /* NL80211_CMD_CRIT_PROTOCOL_STOP */, @@ -691,7 +691,7 @@ wifi_nl80211_indicate_addressing_running (NMWifiUtils *data, gboolean running) 5000); } - err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); + err = nl80211_send_and_recv (self, msg, NULL, NULL); return err >= 0; nla_put_failure: @@ -699,6 +699,7 @@ nla_put_failure: } struct nl80211_device_info { + NMWifiUtilsNl80211 *self; int phy; guint32 *freqs; int num_freqs; @@ -721,36 +722,40 @@ struct nl80211_device_info { static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg) { + static const struct nla_policy freq_policy[] = { + [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, + [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, +#ifdef NL80211_FREQUENCY_ATTR_NO_IR + [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG }, +#else + [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, + [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, +#endif + [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, + [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, + }; struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); struct nl80211_device_info *info = arg; + NMWifiUtilsNl80211 *self = info->self; struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; - struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; + struct nlattr *tb_freq[G_N_ELEMENTS (freq_policy)]; struct nlattr *nl_band; struct nlattr *nl_freq; int rem_freq; int rem_band; int freq_idx; - static const struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { - [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, - [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, -#ifdef NL80211_FREQUENCY_ATTR_NO_IR - [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG }, -#else - [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, - [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, -#endif - [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, - [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, - }; + #ifdef NL80211_FREQUENCY_ATTR_NO_IR - G_STATIC_ASSERT (NL80211_FREQUENCY_ATTR_PASSIVE_SCAN == NL80211_FREQUENCY_ATTR_NO_IR && NL80211_FREQUENCY_ATTR_NO_IBSS == NL80211_FREQUENCY_ATTR_NO_IR); + G_STATIC_ASSERT_EXPR (NL80211_FREQUENCY_ATTR_PASSIVE_SCAN == NL80211_FREQUENCY_ATTR_NO_IR && NL80211_FREQUENCY_ATTR_NO_IBSS == NL80211_FREQUENCY_ATTR_NO_IR); #else - G_STATIC_ASSERT (NL80211_FREQUENCY_ATTR_PASSIVE_SCAN != NL80211_FREQUENCY_ATTR_NO_IBSS); + G_STATIC_ASSERT_EXPR (NL80211_FREQUENCY_ATTR_PASSIVE_SCAN != NL80211_FREQUENCY_ATTR_NO_IBSS); #endif - if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), - genlmsg_attrlen (gnlh, 0), NULL) < 0) + if (nla_parse_arr (tb, + genlmsg_attrdata (gnlh, 0), + genlmsg_attrlen (gnlh, 0), + NULL) < 0) return NL_SKIP; if ( tb[NL80211_ATTR_WIPHY] == NULL @@ -796,14 +801,17 @@ static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg) info->num_freqs = 0; nla_for_each_nested (nl_band, tb[NL80211_ATTR_WIPHY_BANDS], rem_band) { - if (nla_parse_nested (tb_band, NL80211_BAND_ATTR_MAX, nl_band, - NULL) < 0) + if (nla_parse_nested_arr (tb_band, + nl_band, + NULL) < 0) return NL_SKIP; - nla_for_each_nested (nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], + nla_for_each_nested (nl_freq, + tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { - if (nla_parse_nested (tb_freq, NL80211_FREQUENCY_ATTR_MAX, - nl_freq, freq_policy) < 0) + if (nla_parse_nested_arr (tb_freq, + nl_freq, + freq_policy) < 0) continue; if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) @@ -818,21 +826,22 @@ static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg) freq_idx = 0; nla_for_each_nested (nl_band, tb[NL80211_ATTR_WIPHY_BANDS], rem_band) { - if (nla_parse_nested (tb_band, NL80211_BAND_ATTR_MAX, nl_band, - NULL) < 0) + if (nla_parse_nested_arr (tb_band, + nl_band, + NULL) < 0) return NL_SKIP; nla_for_each_nested (nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { - if (nla_parse_nested (tb_freq, NL80211_FREQUENCY_ATTR_MAX, - nl_freq, freq_policy) < 0) + if (nla_parse_nested_arr (tb_freq, + nl_freq, + freq_policy) < 0) continue; if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) continue; - info->freqs[freq_idx] = - nla_get_u32 (tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); + info->freqs[freq_idx] = nla_get_u32 (tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); info->caps |= NM_WIFI_DEVICE_CAP_FREQ_VALID; @@ -847,11 +856,10 @@ static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg) /* Read security/encryption support */ if (tb[NL80211_ATTR_CIPHER_SUITES]) { - int num; - int i; - __u32 *ciphers = nla_data (tb[NL80211_ATTR_CIPHER_SUITES]); + guint32 *ciphers = nla_data (tb[NL80211_ATTR_CIPHER_SUITES]); + guint i, num; - num = nla_len (tb[NL80211_ATTR_CIPHER_SUITES]) / sizeof (__u32); + num = nla_len (tb[NL80211_ATTR_CIPHER_SUITES]) / sizeof (guint32); for (i = 0; i < num; i++) { switch (ciphers[i]) { case WLAN_CIPHER_SUITE_WEP40: @@ -873,8 +881,7 @@ static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg) case WLAN_CIPHER_SUITE_SMS4: break; default: - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "don't know the meaning of NL80211_ATTR_CIPHER_SUITE %#8.8x.", + _LOGD ("don't know the meaning of NL80211_ATTR_CIPHER_SUITE %#8.8x.", ciphers[i]); break; } @@ -930,86 +937,66 @@ nm_wifi_utils_nl80211_class_init (NMWifiUtilsNl80211Class *klass) NMWifiUtils * nm_wifi_utils_nl80211_new (int ifindex, struct nl_sock *genl) { - gs_unref_object NMWifiUtilsNl80211 *nl80211 = NULL; + gs_unref_object NMWifiUtilsNl80211 *self = NULL; nm_auto_nlmsg struct nl_msg *msg = NULL; - struct nl80211_device_info device_info = {}; - char ifname[IFNAMSIZ]; + struct nl80211_device_info device_info = { }; if (!genl) return NULL; - if (!nmp_utils_if_indextoname (ifindex, ifname)) { - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "can't determine interface name for ifindex %d", ifindex); - nm_sprintf_buf (ifname, "if %d", ifindex); - } - - nl80211 = g_object_new (NM_TYPE_WIFI_UTILS_NL80211, NULL); + self = g_object_new (NM_TYPE_WIFI_UTILS_NL80211, NULL); - nl80211->parent.ifindex = ifindex; - nl80211->nl_sock = genl; + self->parent.ifindex = ifindex; + self->nl_sock = genl; - nl80211->id = genl_ctrl_resolve (nl80211->nl_sock, "nl80211"); - if (nl80211->id < 0) { - _LOGD (LOGD_WIFI, "genl_ctrl_resolve: failed to resolve \"nl80211\""); + self->id = genl_ctrl_resolve (self->nl_sock, "nl80211"); + if (self->id < 0) { + _LOGD ("genl_ctrl_resolve: failed to resolve \"nl80211\""); return NULL; } - nl80211->phy = -1; + self->phy = -1; - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_WIPHY, 0); + msg = nl80211_alloc_msg (self, NL80211_CMD_GET_WIPHY, 0); - if (nl80211_send_and_recv (nl80211, msg, nl80211_wiphy_info_handler, + device_info.self = self; + if (nl80211_send_and_recv (self, msg, nl80211_wiphy_info_handler, &device_info) < 0) { - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "(%s): NL80211_CMD_GET_WIPHY request failed", - ifname); + _LOGD ("NL80211_CMD_GET_WIPHY request failed"); return NULL; } if (!device_info.success) { - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "(%s): NL80211_CMD_GET_WIPHY request indicated failure", - ifname); + _LOGD ("NL80211_CMD_GET_WIPHY request indicated failure"); return NULL; } if (!device_info.supported) { - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver does not fully support nl80211, falling back to WEXT", - ifname); + _LOGD ("driver does not fully support nl80211, falling back to WEXT"); return NULL; } if (!device_info.can_scan_ssid) { - _LOGE (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver does not support SSID scans", - ifname); + _LOGE ("driver does not support SSID scans"); return NULL; } if (device_info.num_freqs == 0 || device_info.freqs == NULL) { - nm_log_err (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver reports no supported frequencies", - ifname); + _LOGE ("driver reports no supported frequencies"); return NULL; } if (device_info.caps == 0) { - _LOGE (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver doesn't report support of any encryption", - ifname); + _LOGE ("driver doesn't report support of any encryption"); return NULL; } - nl80211->phy = device_info.phy; - nl80211->freqs = device_info.freqs; - nl80211->num_freqs = device_info.num_freqs; - nl80211->parent.caps = device_info.caps; - nl80211->can_wowlan = device_info.can_wowlan; + self->phy = device_info.phy; + self->freqs = device_info.freqs; + self->num_freqs = device_info.num_freqs; + self->parent.caps = device_info.caps; + self->can_wowlan = device_info.can_wowlan; - _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): using nl80211 for WiFi device control", - ifname); - return (NMWifiUtils *) g_steal_pointer (&nl80211); + _LOGD ("using nl80211 for Wi-Fi device control"); + return (NMWifiUtils *) g_steal_pointer (&self); } diff --git a/src/platform/wifi/nm-wifi-utils-wext.c b/src/platform/wifi/nm-wifi-utils-wext.c index e52ae5a3..3aa1720a 100644 --- a/src/platform/wifi/nm-wifi-utils-wext.c +++ b/src/platform/wifi/nm-wifi-utils-wext.c @@ -23,8 +23,6 @@ #include "nm-wifi-utils-wext.h" -#include <errno.h> -#include <string.h> #include <sys/ioctl.h> #include <net/ethernet.h> #include <unistd.h> @@ -117,7 +115,7 @@ get_ifname (int ifindex, char *buffer, const char *op) errsv = errno; _LOGW (LOGD_PLATFORM | LOGD_WIFI, "error getting interface name for ifindex %d, operation '%s': %s (%d)", - ifindex, op, g_strerror (errsv), errsv); + ifindex, op, nm_strerror_native (errsv), errsv); return FALSE; } @@ -129,15 +127,17 @@ wifi_wext_get_mode_ifname (NMWifiUtils *data, const char *ifname) { NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; struct iwreq wrq; + int errsv; memset (&wrq, 0, sizeof (struct iwreq)); nm_utils_ifname_cpy (wrq.ifr_name, ifname); if (ioctl (wext->fd, SIOCGIWMODE, &wrq) < 0) { - if (errno != ENODEV) { + errsv = errno; + if (errsv != ENODEV) { _LOGW (LOGD_PLATFORM | LOGD_WIFI, "(%s): error %d getting card mode", - ifname, errno); + ifname, errsv); } return NM_802_11_MODE_UNKNOWN; } @@ -253,7 +253,7 @@ wifi_wext_get_freq (NMWifiUtils *data) if (ioctl (wext->fd, SIOCGIWFREQ, &wrq) < 0) { _LOGW (LOGD_PLATFORM | LOGD_WIFI, "(%s): error getting frequency: %s", - ifname, strerror (errno)); + ifname, nm_strerror_native (errno)); return 0; } @@ -291,7 +291,7 @@ wifi_wext_get_bssid (NMWifiUtils *data, guint8 *out_bssid) if (ioctl (wext->fd, SIOCGIWAP, &wrq) < 0) { _LOGW (LOGD_PLATFORM | LOGD_WIFI, "(%s): error getting associated BSSID: %s", - ifname, strerror (errno)); + ifname, nm_strerror_native (errno)); return FALSE; } memcpy (out_bssid, &(wrq.u.ap_addr.sa_data), ETH_ALEN); @@ -429,7 +429,7 @@ wifi_wext_get_qual (NMWifiUtils *data) if (ioctl (wext->fd, SIOCGIWSTATS, &wrq) < 0) { _LOGW (LOGD_PLATFORM | LOGD_WIFI, "(%s): error getting signal strength: %s", - ifname, strerror (errno)); + ifname, nm_strerror_native (errno)); return -1; } @@ -476,7 +476,7 @@ wifi_wext_set_mesh_channel (NMWifiUtils *data, guint32 channel) if (ioctl (wext->fd, SIOCSIWFREQ, &wrq) < 0) { _LOGE (LOGD_PLATFORM | LOGD_WIFI | LOGD_OLPC, "(%s): error setting channel to %d: %s", - ifname, channel, strerror (errno)); + ifname, channel, nm_strerror_native (errno)); return FALSE; } @@ -506,15 +506,15 @@ wifi_wext_set_mesh_ssid (NMWifiUtils *data, const guint8 *ssid, gsize len) if (ioctl (wext->fd, SIOCSIWESSID, &wrq) == 0) return TRUE; - if (errno != ENODEV) { + errsv = errno; + if (errsv != ENODEV) { gs_free char *ssid_str = NULL; - errsv = errno; _LOGE (LOGD_PLATFORM | LOGD_WIFI | LOGD_OLPC, - "(%s): error setting SSID to '%s': %s", + "(%s): error setting SSID to %s: %s", ifname, (ssid_str = _nm_utils_ssid_to_string_arr (ssid, len)), - strerror (errsv)); + nm_strerror_native (errsv)); } return FALSE; @@ -545,6 +545,7 @@ wext_get_range_ifname (NMWifiUtilsWext *wext, int i = 26; gboolean success = FALSE; struct iwreq wrq; + int errsv; memset (&wrq, 0, sizeof (struct iwreq)); nm_utils_ifname_cpy (wrq.ifr_name, ifname); @@ -561,11 +562,14 @@ wext_get_range_ifname (NMWifiUtilsWext *wext, *response_len = wrq.u.data.length; success = TRUE; break; - } else if (errno != EAGAIN) { - _LOGE (LOGD_PLATFORM | LOGD_WIFI, - "(%s): couldn't get driver range information (%d).", - ifname, errno); - break; + } else { + errsv = errno; + if (errsv != EAGAIN) { + _LOGE (LOGD_PLATFORM | LOGD_WIFI, + "(%s): couldn't get driver range information (%d).", + ifname, errsv); + break; + } } g_usleep (G_USEC_PER_SEC / 4); @@ -751,7 +755,7 @@ nm_wifi_utils_wext_new (int ifindex, gboolean check_scan) wext->parent.caps |= NM_WIFI_DEVICE_CAP_FREQ_5GHZ; _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): using WEXT for WiFi device control", + "(%s): using WEXT for Wi-Fi device control", ifname); return (NMWifiUtils *) wext; @@ -771,7 +775,7 @@ nm_wifi_utils_wext_is_wifi (const char *iface) /* performing an ioctl on a non-existing name may cause the automatic * loading of kernel modules, which should be avoided. * - * Usually, we should thus make sure that an inteface with this name + * Usually, we should thus make sure that an interface with this name * exists. * * Note that wifi_wext_is_wifi() has only one caller which just verified diff --git a/src/platform/wifi/nm-wifi-utils.c b/src/platform/wifi/nm-wifi-utils.c index 25d71c6a..96071faa 100644 --- a/src/platform/wifi/nm-wifi-utils.c +++ b/src/platform/wifi/nm-wifi-utils.c @@ -25,7 +25,6 @@ #include <sys/stat.h> #include <stdio.h> -#include <string.h> #include <fcntl.h> #include "nm-wifi-utils-private.h" diff --git a/src/platform/wifi/nm-wifi-utils.h b/src/platform/wifi/nm-wifi-utils.h index 6cd178bd..36148b5a 100644 --- a/src/platform/wifi/nm-wifi-utils.h +++ b/src/platform/wifi/nm-wifi-utils.h @@ -62,7 +62,7 @@ gboolean nm_wifi_utils_get_bssid (NMWifiUtils *data, guint8 *out_bssid); /* Returns current bitrate in Kbps */ guint32 nm_wifi_utils_get_rate (NMWifiUtils *data); -/* Returns quality 0 - 100% on succes, or -1 on error */ +/* Returns quality 0 - 100% on success, or -1 on error */ int nm_wifi_utils_get_qual (NMWifiUtils *data); /* Tells the driver DHCP or SLAAC is running */ |