From 6518e361171f64bcaaa4bf868139362ed95cc2e0 Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Sat, 20 Oct 2018 01:30:31 +0200 Subject: New upstream version 1.14.2 --- src/platform/wifi/nm-wifi-utils-nl80211.c | 1014 +++++++++++++++++++++++++++++ src/platform/wifi/nm-wifi-utils-nl80211.h | 39 ++ src/platform/wifi/nm-wifi-utils-private.h | 79 +++ src/platform/wifi/nm-wifi-utils-wext.c | 789 ++++++++++++++++++++++ src/platform/wifi/nm-wifi-utils-wext.h | 39 ++ src/platform/wifi/nm-wifi-utils.c | 240 +++++++ src/platform/wifi/nm-wifi-utils.h | 84 +++ src/platform/wifi/wifi-utils-nl80211.c | 988 ---------------------------- src/platform/wifi/wifi-utils-nl80211.h | 28 - src/platform/wifi/wifi-utils-private.h | 81 --- src/platform/wifi/wifi-utils-wext.c | 766 ---------------------- src/platform/wifi/wifi-utils-wext.h | 30 - src/platform/wifi/wifi-utils.c | 231 ------- src/platform/wifi/wifi-utils.h | 79 --- 14 files changed, 2284 insertions(+), 2203 deletions(-) create mode 100644 src/platform/wifi/nm-wifi-utils-nl80211.c create mode 100644 src/platform/wifi/nm-wifi-utils-nl80211.h create mode 100644 src/platform/wifi/nm-wifi-utils-private.h create mode 100644 src/platform/wifi/nm-wifi-utils-wext.c create mode 100644 src/platform/wifi/nm-wifi-utils-wext.h create mode 100644 src/platform/wifi/nm-wifi-utils.c create mode 100644 src/platform/wifi/nm-wifi-utils.h delete mode 100644 src/platform/wifi/wifi-utils-nl80211.c delete mode 100644 src/platform/wifi/wifi-utils-nl80211.h delete mode 100644 src/platform/wifi/wifi-utils-private.h delete mode 100644 src/platform/wifi/wifi-utils-wext.c delete mode 100644 src/platform/wifi/wifi-utils-wext.h delete mode 100644 src/platform/wifi/wifi-utils.c delete mode 100644 src/platform/wifi/wifi-utils.h (limited to 'src/platform/wifi') diff --git a/src/platform/wifi/nm-wifi-utils-nl80211.c b/src/platform/wifi/nm-wifi-utils-nl80211.c new file mode 100644 index 00000000..b3bb2bb6 --- /dev/null +++ b/src/platform/wifi/nm-wifi-utils-nl80211.c @@ -0,0 +1,1014 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2005 - 2018 Red Hat, Inc. + * Copyright (C) 2006 - 2008 Novell, Inc. + * Copyright (C) 2011 Intel Corporation. All rights reserved. + */ + +#include "nm-default.h" + +#include "nm-wifi-utils-nl80211.h" + +#include +#include +#include +#include +#include +#include + +#include "platform/nm-netlink.h" +#include "nm-wifi-utils-private.h" +#include "platform/nm-platform.h" +#include "platform/nm-platform-utils.h" +#include "nm-utils.h" + +#define _NMLOG_PREFIX_NAME "wifi-nl80211" +#define _NMLOG(level, domain, ...) \ + G_STMT_START { \ + nm_log ((level), (domain), NULL, NULL, \ + "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ + _NMLOG_PREFIX_NAME \ + _NM_UTILS_MACRO_REST(__VA_ARGS__)); \ + } G_STMT_END + +typedef struct { + NMWifiUtils parent; + struct nl_sock *nl_sock; + guint32 *freqs; + int id; + int num_freqs; + int phy; + bool can_wowlan:1; +} NMWifiUtilsNl80211; + +typedef struct { + NMWifiUtilsClass parent; +} NMWifiUtilsNl80211Class; + +G_DEFINE_TYPE (NMWifiUtilsNl80211, nm_wifi_utils_nl80211, NM_TYPE_WIFI_UTILS) + +static int +ack_handler (struct nl_msg *msg, void *arg) +{ + int *done = arg; + *done = 1; + return NL_STOP; +} + +static int +finish_handler (struct nl_msg *msg, void *arg) +{ + int *done = arg; + *done = 1; + return NL_SKIP; +} + +static int +error_handler (struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg) +{ + int *done = arg; + *done = err->error; + return NL_SKIP; +} + +static struct nl_msg * +_nl80211_alloc_msg (int id, int ifindex, int phy, guint32 cmd, guint32 flags) +{ + nm_auto_nlmsg struct nl_msg *msg = NULL; + + msg = nlmsg_alloc (); + genlmsg_put (msg, 0, 0, id, 0, flags, cmd, 0); + NLA_PUT_U32 (msg, NL80211_ATTR_IFINDEX, ifindex); + if (phy != -1) + NLA_PUT_U32 (msg, NL80211_ATTR_WIPHY, phy); + return g_steal_pointer (&msg); + +nla_put_failure: + return NULL; +} + +static struct nl_msg * +nl80211_alloc_msg (NMWifiUtilsNl80211 *nl80211, guint32 cmd, guint32 flags) +{ + return _nl80211_alloc_msg (nl80211->id, nl80211->parent.ifindex, nl80211->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) +{ + int err; + int done = 0; + const struct nl_cb cb = { + .err_cb = error_handler, + .err_arg = &done, + .finish_cb = finish_handler, + .finish_arg = &done, + .ack_cb = ack_handler, + .ack_arg = &done, + .valid_cb = valid_handler, + .valid_arg = valid_data, + }; + + g_return_val_if_fail (msg != NULL, -ENOMEM); + + err = nl_send_auto (nl_sock, msg); + if (err < 0) + return err; + + /* Loop until one of our NL callbacks says we're done; on success + * done will be 1, on error it will be < 0. + */ + while (!done) { + err = nl_recvmsgs (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 && + genlmsg_hdr (nlmsg_hdr (msg))->cmd == NL80211_CMD_GET_SCAN) + break; + + _LOGW (LOGD_WIFI, "nl_recvmsgs() error: (%d) %s", + err, nl_geterror (err)); + break; + } + } + + if (err >= 0 && done < 0) + err = done; + 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); + + g_clear_pointer (&nl80211->freqs, g_free); +} + +struct nl80211_iface_info { + NM80211Mode mode; +}; + +static int +nl80211_iface_info_handler (struct nl_msg *msg, void *arg) +{ + struct nl80211_iface_info *info = 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) + return NL_SKIP; + + if (!tb[NL80211_ATTR_IFTYPE]) + return NL_SKIP; + + switch (nla_get_u32 (tb[NL80211_ATTR_IFTYPE])) { + case NL80211_IFTYPE_ADHOC: + info->mode = NM_802_11_MODE_ADHOC; + break; + case NL80211_IFTYPE_AP: + info->mode = NM_802_11_MODE_AP; + break; + case NL80211_IFTYPE_STATION: + info->mode = NM_802_11_MODE_INFRA; + break; + } + + return NL_SKIP; +} + +static NM80211Mode +wifi_nl80211_get_mode (NMWifiUtils *data) +{ + NMWifiUtilsNl80211 *nl80211 = (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); + + if (nl80211_send_and_recv (nl80211, msg, nl80211_iface_info_handler, + &iface_info) < 0) + return NM_802_11_MODE_UNKNOWN; + + return iface_info.mode; +} + +static gboolean +wifi_nl80211_set_mode (NMWifiUtils *data, const NM80211Mode mode) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + nm_auto_nlmsg struct nl_msg *msg = NULL; + int err; + + msg = nl80211_alloc_msg (nl80211, NL80211_CMD_SET_INTERFACE, 0); + + switch (mode) { + case NM_802_11_MODE_INFRA: + NLA_PUT_U32 (msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION); + break; + case NM_802_11_MODE_ADHOC: + NLA_PUT_U32 (msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_ADHOC); + break; + case NM_802_11_MODE_AP: + NLA_PUT_U32 (msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP); + break; + default: + g_assert_not_reached (); + } + + err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); + return err >= 0; + +nla_put_failure: + return FALSE; +} + +static gboolean +wifi_nl80211_set_powersave (NMWifiUtils *data, guint32 powersave) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + nm_auto_nlmsg struct nl_msg *msg = NULL; + int err; + + msg = nl80211_alloc_msg (nl80211, 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); + return err >= 0; + +nla_put_failure: + return FALSE; +} + +static int +nl80211_get_wake_on_wlan_handler (struct nl_msg *msg, void *arg) +{ + NMSettingWirelessWakeOnWLan *wowl = arg; + struct nlattr *attrs[NL80211_ATTR_MAX + 1]; + 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); + + 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); + + *wowl = NM_SETTING_WIRELESS_WAKE_ON_WLAN_NONE; + if (trig[NL80211_WOWLAN_TRIG_ANY]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_ANY; + if (trig[NL80211_WOWLAN_TRIG_DISCONNECT]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_DISCONNECT; + if (trig[NL80211_WOWLAN_TRIG_MAGIC_PKT]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_MAGIC; + if (trig[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_GTK_REKEY_FAILURE; + if (trig[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_EAP_IDENTITY_REQUEST; + if (trig[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_4WAY_HANDSHAKE; + if (trig[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_RFKILL_RELEASE; + if (trig[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) + *wowl |= NM_SETTING_WIRELESS_WAKE_ON_WLAN_TCP; + + return NL_SKIP; +} + +static NMSettingWirelessWakeOnWLan +wifi_nl80211_get_wake_on_wlan (NMWifiUtils *data) +{ + NMWifiUtilsNl80211 *nl80211 = (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); + + nl80211_send_and_recv (nl80211, msg, nl80211_get_wake_on_wlan_handler, &wowl); + + return wowl; +} + +static gboolean +wifi_nl80211_set_wake_on_wlan (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wowl) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + nm_auto_nlmsg struct nl_msg *msg = NULL; + struct nlattr *triggers; + int err; + + if (wowl == NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE) + return TRUE; + + msg = nl80211_alloc_msg (nl80211, NL80211_CMD_SET_WOWLAN, 0); + if (!msg) + return FALSE; + + triggers = nla_nest_start (msg, NL80211_ATTR_WOWLAN_TRIGGERS); + + if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_ANY)) + NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_ANY); + if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_DISCONNECT)) + NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_DISCONNECT); + if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_MAGIC)) + NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_MAGIC_PKT); + if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_GTK_REKEY_FAILURE)) + NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE); + if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_EAP_IDENTITY_REQUEST)) + NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST); + if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_4WAY_HANDSHAKE)) + NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE); + if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_RFKILL_RELEASE)) + NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE); + + nla_nest_end(msg, triggers); + + err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); + + return err >= 0; + +nla_put_failure: + return FALSE; +} + +/* @divisor: pass what value @xbm should be divided by to get dBm */ +static guint32 +nl80211_xbm_to_percent (gint32 xbm, guint32 divisor) +{ +#define NOISE_FLOOR_DBM -90 +#define SIGNAL_MAX_DBM -20 + + xbm /= divisor; + xbm = CLAMP (xbm, NOISE_FLOOR_DBM, SIGNAL_MAX_DBM); + + return 100 - 70 * (((float) SIGNAL_MAX_DBM - (float) xbm) / + ((float) SIGNAL_MAX_DBM - (float) NOISE_FLOOR_DBM)); +} + +struct nl80211_bss_info { + guint32 freq; + guint8 bssid[ETH_ALEN]; + guint8 ssid[32]; + guint32 ssid_len; + guint32 beacon_signal; + gboolean valid; +}; + +#define WLAN_EID_SSID 0 + +static void +find_ssid (guint8 *ies, guint32 ies_len, + guint8 **ssid, guint32 *ssid_len) +{ + *ssid = NULL; + *ssid_len = 0; + + while (ies_len > 2 && ies[0] != WLAN_EID_SSID) { + ies_len -= ies[1] + 2; + ies += ies[1] + 2; + } + if (ies_len < 2) + return; + if (ies_len < 2 + ies[1]) + return; + + *ssid_len = ies[1]; + *ssid = ies + 2; +} + +static int +nl80211_bss_dump_handler (struct nl_msg *msg, void *arg) +{ + 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 }, + }; + guint32 status; + + if (nla_parse (tb, NL80211_ATTR_MAX, 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)) + return NL_SKIP; + + if (bss[NL80211_BSS_STATUS] == NULL) + return NL_SKIP; + + status = nla_get_u32 (bss[NL80211_BSS_STATUS]); + + if (status != NL80211_BSS_STATUS_ASSOCIATED && + status != NL80211_BSS_STATUS_IBSS_JOINED) + return NL_SKIP; + + if (bss[NL80211_BSS_BSSID] == NULL) + return NL_SKIP; + memcpy (info->bssid, nla_data (bss[NL80211_BSS_BSSID]), ETH_ALEN); + + if (bss[NL80211_BSS_FREQUENCY]) + 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]); + + if (bss[NL80211_BSS_SIGNAL_MBM]) + 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)) { + memcpy (info->ssid, ssid, ssid_len); + info->ssid_len = ssid_len; + } + } + + info->valid = TRUE; + + return NL_SKIP; +} + +static void +nl80211_get_bss_info (NMWifiUtilsNl80211 *nl80211, + 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); + + nl80211_send_and_recv (nl80211, msg, nl80211_bss_dump_handler, bss_info); +} + +static guint32 +wifi_nl80211_get_freq (NMWifiUtils *data) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + struct nl80211_bss_info bss_info; + + nl80211_get_bss_info (nl80211, &bss_info); + + return bss_info.freq; +} + +static guint32 +wifi_nl80211_find_freq (NMWifiUtils *data, const guint32 *freqs) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + int i; + + for (i = 0; i < nl80211->num_freqs; i++) { + while (*freqs) { + if (nl80211->freqs[i] == *freqs) + return *freqs; + freqs++; + } + } + return 0; +} + +static gboolean +wifi_nl80211_get_bssid (NMWifiUtils *data, guint8 *out_bssid) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + struct nl80211_bss_info bss_info; + + nl80211_get_bss_info (nl80211, &bss_info); + + if (bss_info.valid) + memcpy (out_bssid, bss_info.bssid, ETH_ALEN); + + return bss_info.valid; +} + +struct nl80211_station_info { + guint32 txrate; + gboolean txrate_valid; + guint8 signal; + gboolean signal_valid; +}; + +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] = { + [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 }, + }; + + 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 }, + [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, + [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, + }; + + if (nla_parse (tb, NL80211_ATTR_MAX, 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)) + 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)) + return NL_SKIP; + + if (rinfo[NL80211_RATE_INFO_BITRATE] == NULL) + return NL_SKIP; + + /* convert from nl80211's units of 100kbps to NM's kbps */ + info->txrate = nla_get_u16 (rinfo[NL80211_RATE_INFO_BITRATE]) * 100; + info->txrate_valid = TRUE; + + if (sinfo[NL80211_STA_INFO_SIGNAL] != NULL) { + info->signal = nl80211_xbm_to_percent ((gint8) nla_get_u8 (sinfo[NL80211_STA_INFO_SIGNAL]), 1); + info->signal_valid = TRUE; + } + + return NL_SKIP; +} + +static void +nl80211_get_ap_info (NMWifiUtilsNl80211 *nl80211, + struct nl80211_station_info *sta_info) +{ + nm_auto_nlmsg struct nl_msg *msg = NULL; + struct nl80211_bss_info bss_info; + + memset (sta_info, 0, sizeof (*sta_info)); + + nl80211_get_bss_info (nl80211, &bss_info); + if (!bss_info.valid) + return; + + msg = nl80211_alloc_msg (nl80211, 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); + if (!sta_info->signal_valid) { + /* Fall back to bss_info signal quality (both are in percent) */ + sta_info->signal = bss_info.beacon_signal; + } + } + + return; + +nla_put_failure: + return; +} + +static guint32 +wifi_nl80211_get_rate (NMWifiUtils *data) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + struct nl80211_station_info sta_info; + + nl80211_get_ap_info (nl80211, &sta_info); + + return sta_info.txrate; +} + +static int +wifi_nl80211_get_qual (NMWifiUtils *data) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + struct nl80211_station_info sta_info; + + nl80211_get_ap_info (nl80211, &sta_info); + return sta_info.signal; +} + +static gboolean +wifi_nl80211_indicate_addressing_running (NMWifiUtils *data, gboolean running) +{ + NMWifiUtilsNl80211 *nl80211 = (NMWifiUtilsNl80211 *) data; + nm_auto_nlmsg struct nl_msg *msg = NULL; + int err; + + msg = nl80211_alloc_msg (nl80211, + running + ? 98 /* NL80211_CMD_CRIT_PROTOCOL_START */ + : 99 /* NL80211_CMD_CRIT_PROTOCOL_STOP */, + 0); + /* Despite the DHCP name, we're using this for any type of IP addressing, + * DHCPv4, DHCPv6, and IPv6 SLAAC. + */ + NLA_PUT_U16 (msg, + 179 /* NL80211_ATTR_CRIT_PROT_ID */, + 1 /* NL80211_CRIT_PROTO_DHCP */); + if (running) { + /* Give DHCP 5 seconds to complete */ + NLA_PUT_U16 (msg, + 180 /* NL80211_ATTR_MAX_CRIT_PROT_DURATION */, + 5000); + } + + err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); + return err >= 0; + +nla_put_failure: + return FALSE; +} + +struct nl80211_device_info { + int phy; + guint32 *freqs; + int num_freqs; + guint32 caps; + gboolean can_scan; + gboolean can_scan_ssid; + gboolean supported; + gboolean success; + gboolean can_wowlan; +}; + +#define WLAN_CIPHER_SUITE_USE_GROUP 0x000FAC00 +#define WLAN_CIPHER_SUITE_WEP40 0x000FAC01 +#define WLAN_CIPHER_SUITE_TKIP 0x000FAC02 +#define WLAN_CIPHER_SUITE_CCMP 0x000FAC04 +#define WLAN_CIPHER_SUITE_WEP104 0x000FAC05 +#define WLAN_CIPHER_SUITE_AES_CMAC 0x000FAC06 +#define WLAN_CIPHER_SUITE_GCMP 0x000FAC08 +#define WLAN_CIPHER_SUITE_SMS4 0x00147201 + +static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg) +{ + struct nlattr *tb[NL80211_ATTR_MAX + 1]; + struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); + struct nl80211_device_info *info = arg; + struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; + struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; + 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); +#else + G_STATIC_ASSERT (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) + return NL_SKIP; + + if ( tb[NL80211_ATTR_WIPHY] == NULL + || tb[NL80211_ATTR_WIPHY_BANDS] == NULL) + return NL_SKIP; + + info->phy = nla_get_u32 (tb[NL80211_ATTR_WIPHY]); + + if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) { + info->can_scan_ssid = + nla_get_u8 (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) > 0; + } else { + /* old kernel that only had mac80211, so assume it can */ + info->can_scan_ssid = TRUE; + } + + if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) { + struct nlattr *nl_cmd; + int i; + + nla_for_each_nested (nl_cmd, tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) { + switch (nla_get_u32 (nl_cmd)) { + case NL80211_CMD_TRIGGER_SCAN: + info->can_scan = TRUE; + break; + case NL80211_CMD_CONNECT: + case NL80211_CMD_AUTHENTICATE: + /* Only devices that support CONNECT or AUTH actually support + * 802.11, unlike say ipw2x00 (up to at least kernel 3.4) which + * has minimal info support, but no actual command support. + * This check mirrors what wpa_supplicant does to determine + * whether or not to use the nl80211 driver. + */ + info->supported = TRUE; + break; + default: + break; + } + } + } + + /* Find number of supported frequencies */ + 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) + 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) + continue; + + if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) + continue; + + info->num_freqs++; + } + } + + /* Read supported frequencies */ + info->freqs = g_malloc0 (sizeof (guint32) * info->num_freqs); + + 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) + 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) + continue; + + if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) + continue; + + info->freqs[freq_idx] = + nla_get_u32 (tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); + + info->caps |= NM_WIFI_DEVICE_CAP_FREQ_VALID; + + if (info->freqs[freq_idx] > 2400 && info->freqs[freq_idx] < 2500) + info->caps |= NM_WIFI_DEVICE_CAP_FREQ_2GHZ; + if (info->freqs[freq_idx] > 4900 && info->freqs[freq_idx] < 6000) + info->caps |= NM_WIFI_DEVICE_CAP_FREQ_5GHZ; + + freq_idx++; + } + } + + /* Read security/encryption support */ + if (tb[NL80211_ATTR_CIPHER_SUITES]) { + int num; + int i; + __u32 *ciphers = nla_data (tb[NL80211_ATTR_CIPHER_SUITES]); + + num = nla_len (tb[NL80211_ATTR_CIPHER_SUITES]) / sizeof (__u32); + for (i = 0; i < num; i++) { + switch (ciphers[i]) { + case WLAN_CIPHER_SUITE_WEP40: + info->caps |= NM_WIFI_DEVICE_CAP_CIPHER_WEP40; + break; + case WLAN_CIPHER_SUITE_WEP104: + info->caps |= NM_WIFI_DEVICE_CAP_CIPHER_WEP104; + break; + case WLAN_CIPHER_SUITE_TKIP: + info->caps |= (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | + NM_WIFI_DEVICE_CAP_WPA); + break; + case WLAN_CIPHER_SUITE_CCMP: + info->caps |= (NM_WIFI_DEVICE_CAP_CIPHER_CCMP | + NM_WIFI_DEVICE_CAP_RSN); + break; + case WLAN_CIPHER_SUITE_AES_CMAC: + case WLAN_CIPHER_SUITE_GCMP: + case WLAN_CIPHER_SUITE_SMS4: + break; + default: + _LOGD (LOGD_PLATFORM | LOGD_WIFI, + "don't know the meaning of NL80211_ATTR_CIPHER_SUITE %#8.8x.", + ciphers[i]); + break; + } + } + } + + if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) { + struct nlattr *nl_mode; + int i; + + nla_for_each_nested (nl_mode, tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) { + if (nla_type (nl_mode) == NL80211_IFTYPE_AP) + info->caps |= NM_WIFI_DEVICE_CAP_AP; + else if (nla_type (nl_mode) == NL80211_IFTYPE_ADHOC) + info->caps |= NM_WIFI_DEVICE_CAP_ADHOC; + } + } + + if (tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]) + info->can_wowlan = TRUE; + + info->success = TRUE; + + return NL_SKIP; +} + +static void +nm_wifi_utils_nl80211_init (NMWifiUtilsNl80211 *self) +{ +} + +static void +nm_wifi_utils_nl80211_class_init (NMWifiUtilsNl80211Class *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + NMWifiUtilsClass *wifi_utils_class = NM_WIFI_UTILS_CLASS (klass); + + object_class->dispose = dispose; + + wifi_utils_class->get_mode = wifi_nl80211_get_mode; + wifi_utils_class->set_mode = wifi_nl80211_set_mode; + wifi_utils_class->set_powersave = wifi_nl80211_set_powersave; + wifi_utils_class->get_wake_on_wlan = wifi_nl80211_get_wake_on_wlan, + wifi_utils_class->set_wake_on_wlan = wifi_nl80211_set_wake_on_wlan, + wifi_utils_class->get_freq = wifi_nl80211_get_freq; + wifi_utils_class->find_freq = wifi_nl80211_find_freq; + wifi_utils_class->get_bssid = wifi_nl80211_get_bssid; + wifi_utils_class->get_rate = wifi_nl80211_get_rate; + wifi_utils_class->get_qual = wifi_nl80211_get_qual; + wifi_utils_class->indicate_addressing_running = wifi_nl80211_indicate_addressing_running; +} + +NMWifiUtils * +nm_wifi_utils_nl80211_new (int ifindex, struct nl_sock *genl) +{ + gs_unref_object NMWifiUtilsNl80211 *nl80211 = NULL; + nm_auto_nlmsg struct nl_msg *msg = NULL; + struct nl80211_device_info device_info = {}; + char ifname[IFNAMSIZ]; + + 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); + + nl80211->parent.ifindex = ifindex; + nl80211->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\""); + return NULL; + } + + nl80211->phy = -1; + + msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_WIPHY, 0); + + if (nl80211_send_and_recv (nl80211, msg, nl80211_wiphy_info_handler, + &device_info) < 0) { + _LOGD (LOGD_PLATFORM | LOGD_WIFI, + "(%s): NL80211_CMD_GET_WIPHY request failed", + ifname); + return NULL; + } + + if (!device_info.success) { + _LOGD (LOGD_PLATFORM | LOGD_WIFI, + "(%s): NL80211_CMD_GET_WIPHY request indicated failure", + ifname); + return NULL; + } + + if (!device_info.supported) { + _LOGD (LOGD_PLATFORM | LOGD_WIFI, + "(%s): driver does not fully support nl80211, falling back to WEXT", + ifname); + return NULL; + } + + if (!device_info.can_scan_ssid) { + _LOGE (LOGD_PLATFORM | LOGD_WIFI, + "(%s): driver does not support SSID scans", + ifname); + 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); + return NULL; + } + + if (device_info.caps == 0) { + _LOGE (LOGD_PLATFORM | LOGD_WIFI, + "(%s): driver doesn't report support of any encryption", + ifname); + 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; + + _LOGI (LOGD_PLATFORM | LOGD_WIFI, + "(%s): using nl80211 for WiFi device control", + ifname); + return (NMWifiUtils *) g_steal_pointer (&nl80211); +} diff --git a/src/platform/wifi/nm-wifi-utils-nl80211.h b/src/platform/wifi/nm-wifi-utils-nl80211.h new file mode 100644 index 00000000..27f67697 --- /dev/null +++ b/src/platform/wifi/nm-wifi-utils-nl80211.h @@ -0,0 +1,39 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2011 Intel Corporation. All rights reserved. + * Copyright (C) 2018 Red Hat, Inc. + */ + +#ifndef __WIFI_UTILS_NL80211_H__ +#define __WIFI_UTILS_NL80211_H__ + +#include "nm-wifi-utils.h" +#include "platform/nm-netlink.h" + +#define NM_TYPE_WIFI_UTILS_NL80211 (nm_wifi_utils_nl80211_get_type ()) +#define NM_WIFI_UTILS_NL80211(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_WIFI_UTILS_NL80211, NMWifiUtilsNl80211)) +#define NM_WIFI_UTILS_NL80211_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_WIFI_UTILS_NL80211, NMWifiUtilsNl80211Class)) +#define NM_IS_WIFI_UTILS_NL80211(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_WIFI_UTILS_NL80211)) +#define NM_IS_WIFI_UTILS_NL80211_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_WIFI_UTILS_NL80211)) +#define NM_WIFI_UTILS_NL80211_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_WIFI_UTILS_NL80211, NMWifiUtilsNl80211Class)) + +GType nm_wifi_utils_nl80211_get_type (void); + +NMWifiUtils *nm_wifi_utils_nl80211_new (int ifindex, struct nl_sock *genl); + +#endif /* __WIFI_UTILS_NL80211_H__ */ diff --git a/src/platform/wifi/nm-wifi-utils-private.h b/src/platform/wifi/nm-wifi-utils-private.h new file mode 100644 index 00000000..bcbc1c51 --- /dev/null +++ b/src/platform/wifi/nm-wifi-utils-private.h @@ -0,0 +1,79 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2011 - 2018 Red Hat, Inc. + */ + +#ifndef __WIFI_UTILS_PRIVATE_H__ +#define __WIFI_UTILS_PRIVATE_H__ + +#include "nm-dbus-interface.h" +#include "nm-wifi-utils.h" + +typedef struct { + GObjectClass parent; + + NM80211Mode (*get_mode) (NMWifiUtils *data); + + gboolean (*set_mode) (NMWifiUtils *data, const NM80211Mode mode); + + /* Set power saving mode on an interface */ + gboolean (*set_powersave) (NMWifiUtils *data, guint32 powersave); + + /* Get WakeOnWLAN configuration on an interface */ + NMSettingWirelessWakeOnWLan (*get_wake_on_wlan) (NMWifiUtils *data); + + /* Set WakeOnWLAN mode on an interface */ + gboolean (*set_wake_on_wlan) (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wowl); + + /* Return current frequency in MHz (really associated BSS frequency) */ + guint32 (*get_freq) (NMWifiUtils *data); + + /* Return first supported frequency in the zero-terminated list */ + guint32 (*find_freq) (NMWifiUtils *data, const guint32 *freqs); + + /* Return current bitrate in Kbps */ + guint32 (*get_rate) (NMWifiUtils *data); + + gboolean (*get_bssid) (NMWifiUtils *data, guint8 *out_bssid); + + /* Return a signal strength percentage 0 - 100% for the current BSSID; + * return -1 on errors or if not associated. + */ + int (*get_qual) (NMWifiUtils *data); + + /* OLPC Mesh-only functions */ + + guint32 (*get_mesh_channel) (NMWifiUtils *data); + + /* channel == 0 means "auto channel" */ + gboolean (*set_mesh_channel) (NMWifiUtils *data, guint32 channel); + + /* ssid == NULL means "auto SSID" */ + gboolean (*set_mesh_ssid) (NMWifiUtils *data, const guint8 *ssid, gsize len); + + gboolean (*indicate_addressing_running) (NMWifiUtils *data, gboolean running); +} NMWifiUtilsClass; + +struct NMWifiUtils { + GObject parent; + + int ifindex; + NMDeviceWifiCapabilities caps; +}; + +#endif /* __WIFI_UTILS_PRIVATE_H__ */ diff --git a/src/platform/wifi/nm-wifi-utils-wext.c b/src/platform/wifi/nm-wifi-utils-wext.c new file mode 100644 index 00000000..e52ae5a3 --- /dev/null +++ b/src/platform/wifi/nm-wifi-utils-wext.c @@ -0,0 +1,789 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2005 - 2018 Red Hat, Inc. + * Copyright (C) 2006 - 2008 Novell, Inc. + */ + +#include "nm-default.h" + +#include "nm-wifi-utils-wext.h" + +#include +#include +#include +#include +#include + +/* Hacks necessary to #include wireless.h; yay for WEXT */ +#ifndef __user +#define __user +#endif +#include +#include +#include +#include + +#include "nm-wifi-utils-private.h" +#include "nm-utils.h" +#include "platform/nm-platform-utils.h" +#include "nm-core-internal.h" + +typedef struct { + NMWifiUtils parent; + int fd; + struct iw_quality max_qual; + gint8 num_freqs; + guint32 freqs[IW_MAX_FREQUENCIES]; +} NMWifiUtilsWext; + +typedef struct { + NMWifiUtilsClass parent; +} NMWifiUtilsWextClass; + +G_DEFINE_TYPE (NMWifiUtilsWext, nm_wifi_utils_wext, NM_TYPE_WIFI_UTILS) + +/* Until a new wireless-tools comes out that has the defs and the structure, + * need to copy them here. + */ +/* Scan capability flags - in (struct iw_range *)->scan_capa */ +#define NM_IW_SCAN_CAPA_NONE 0x00 +#define NM_IW_SCAN_CAPA_ESSID 0x01 + +struct iw_range_with_scan_capa +{ + guint32 throughput; + guint32 min_nwid; + guint32 max_nwid; + guint16 old_num_channels; + guint8 old_num_frequency; + + guint8 scan_capa; + /* don't need the rest... */ +}; + +#define _NMLOG_PREFIX_NAME "wifi-wext" +#define _NMLOG(level, domain, ...) \ + G_STMT_START { \ + nm_log ((level), (domain), NULL, NULL, \ + "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ + _NMLOG_PREFIX_NAME \ + _NM_UTILS_MACRO_REST(__VA_ARGS__)); \ + } G_STMT_END + +static guint32 +iw_freq_to_uint32 (const struct iw_freq *freq) +{ + if (freq->e == 0) { + /* Some drivers report channel not frequency. Convert to a + * frequency; but this assumes that the device is in b/g mode. + */ + if ((freq->m >= 1) && (freq->m <= 13)) + return 2407 + (5 * freq->m); + else if (freq->m == 14) + return 2484; + } + return (guint32) ((((double) freq->m) * nm_utils_exp10 (freq->e)) / 1000000.0); +} + +static void +dispose (GObject *object) +{ + NMWifiUtilsWext *wext = NM_WIFI_UTILS_WEXT (object); + + wext->fd = nm_close (wext->fd); +} + +static gboolean +get_ifname (int ifindex, char *buffer, const char *op) +{ + int errsv; + + if (!nmp_utils_if_indextoname (ifindex, buffer)) { + errsv = errno; + _LOGW (LOGD_PLATFORM | LOGD_WIFI, + "error getting interface name for ifindex %d, operation '%s': %s (%d)", + ifindex, op, g_strerror (errsv), errsv); + return FALSE; + } + + return TRUE; +} + +static NM80211Mode +wifi_wext_get_mode_ifname (NMWifiUtils *data, const char *ifname) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + + memset (&wrq, 0, sizeof (struct iwreq)); + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + + if (ioctl (wext->fd, SIOCGIWMODE, &wrq) < 0) { + if (errno != ENODEV) { + _LOGW (LOGD_PLATFORM | LOGD_WIFI, + "(%s): error %d getting card mode", + ifname, errno); + } + return NM_802_11_MODE_UNKNOWN; + } + + switch (wrq.u.mode) { + case IW_MODE_ADHOC: + return NM_802_11_MODE_ADHOC; + case IW_MODE_MASTER: + return NM_802_11_MODE_AP; + case IW_MODE_INFRA: + case IW_MODE_AUTO: /* hack for WEXT devices reporting IW_MODE_AUTO */ + return NM_802_11_MODE_INFRA; + default: + break; + } + return NM_802_11_MODE_UNKNOWN; +} + +static NM80211Mode +wifi_wext_get_mode (NMWifiUtils *data) +{ + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "get-mode")) + return FALSE; + + return wifi_wext_get_mode_ifname (data, ifname); +} + +static gboolean +wifi_wext_set_mode (NMWifiUtils *data, const NM80211Mode mode) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "set-mode")) + return FALSE; + + if (wifi_wext_get_mode_ifname (data, ifname) == mode) + return TRUE; + + memset (&wrq, 0, sizeof (struct iwreq)); + switch (mode) { + case NM_802_11_MODE_ADHOC: + wrq.u.mode = IW_MODE_ADHOC; + break; + case NM_802_11_MODE_AP: + wrq.u.mode = IW_MODE_MASTER; + break; + case NM_802_11_MODE_INFRA: + wrq.u.mode = IW_MODE_INFRA; + break; + default: + g_warn_if_reached (); + return FALSE; + } + + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + if (ioctl (wext->fd, SIOCSIWMODE, &wrq) < 0) { + if (errno != ENODEV) { + _LOGE (LOGD_PLATFORM | LOGD_WIFI, + "(%s): error setting mode %d", + ifname, mode); + } + return FALSE; + } + + return TRUE; +} + +static gboolean +wifi_wext_set_powersave (NMWifiUtils *data, guint32 powersave) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "set-powersave")) + return FALSE; + + memset (&wrq, 0, sizeof (struct iwreq)); + if (powersave == 1) { + wrq.u.power.flags = IW_POWER_ALL_R; + } else + wrq.u.power.disabled = 1; + + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + if (ioctl (wext->fd, SIOCSIWPOWER, &wrq) < 0) { + if (errno != ENODEV) { + _LOGE (LOGD_PLATFORM | LOGD_WIFI, + "(%s): error setting powersave %" G_GUINT32_FORMAT, + ifname, powersave); + } + return FALSE; + } + + return TRUE; +} + +static guint32 +wifi_wext_get_freq (NMWifiUtils *data) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "get-freq")) + return FALSE; + + memset (&wrq, 0, sizeof (struct iwreq)); + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + if (ioctl (wext->fd, SIOCGIWFREQ, &wrq) < 0) { + _LOGW (LOGD_PLATFORM | LOGD_WIFI, + "(%s): error getting frequency: %s", + ifname, strerror (errno)); + return 0; + } + + return iw_freq_to_uint32 (&wrq.u.freq); +} + +static guint32 +wifi_wext_find_freq (NMWifiUtils *data, const guint32 *freqs) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + int i; + + for (i = 0; i < wext->num_freqs; i++) { + while (*freqs) { + if (wext->freqs[i] == *freqs) + return *freqs; + freqs++; + } + } + return 0; +} + +static gboolean +wifi_wext_get_bssid (NMWifiUtils *data, guint8 *out_bssid) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "get-bssid")) + return FALSE; + + memset (&wrq, 0, sizeof (wrq)); + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + if (ioctl (wext->fd, SIOCGIWAP, &wrq) < 0) { + _LOGW (LOGD_PLATFORM | LOGD_WIFI, + "(%s): error getting associated BSSID: %s", + ifname, strerror (errno)); + return FALSE; + } + memcpy (out_bssid, &(wrq.u.ap_addr.sa_data), ETH_ALEN); + return TRUE; +} + +static guint32 +wifi_wext_get_rate (NMWifiUtils *data) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + int err; + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "get-rate")) + return FALSE; + + memset (&wrq, 0, sizeof (wrq)); + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + err = ioctl (wext->fd, SIOCGIWRATE, &wrq); + return ((err == 0) ? wrq.u.bitrate.value / 1000 : 0); +} + +static int +wext_qual_to_percent (const struct iw_quality *qual, + const struct iw_quality *max_qual) +{ + int percent = -1; + int level_percent = -1; + + g_return_val_if_fail (qual != NULL, -1); + g_return_val_if_fail (max_qual != NULL, -1); + + /* Magically convert the many different WEXT quality representations to a percentage */ + + _LOGD (LOGD_WIFI, + "QL: qual %d/%u/0x%X, level %d/%u/0x%X, noise %d/%u/0x%X, updated: 0x%X ** MAX: qual %d/%u/0x%X, level %d/%u/0x%X, noise %d/%u/0x%X, updated: 0x%X", + (__s8) qual->qual, qual->qual, qual->qual, + (__s8) qual->level, qual->level, qual->level, + (__s8) qual->noise, qual->noise, qual->noise, + qual->updated, + (__s8) max_qual->qual, max_qual->qual, max_qual->qual, + (__s8) max_qual->level, max_qual->level, max_qual->level, + (__s8) max_qual->noise, max_qual->noise, max_qual->noise, + max_qual->updated); + + /* Try using the card's idea of the signal quality first as long as it tells us what the max quality is. + * Drivers that fill in quality values MUST treat them as percentages, ie the "Link Quality" MUST be + * bounded by 0 and max_qual->qual, and MUST change in a linear fashion. Within those bounds, drivers + * are free to use whatever they want to calculate "Link Quality". + */ + if ((max_qual->qual != 0) && !(max_qual->updated & IW_QUAL_QUAL_INVALID) && !(qual->updated & IW_QUAL_QUAL_INVALID)) + percent = (int)(100 * ((double)qual->qual / (double)max_qual->qual)); + + /* If the driver doesn't specify a complete and valid quality, we have two options: + * + * 1) dBm: driver must specify max_qual->level = 0, and have valid values for + * qual->level and (qual->noise OR max_qual->noise) + * 2) raw RSSI: driver must specify max_qual->level > 0, and have valid values for + * qual->level and max_qual->level + * + * This is the WEXT spec. If this interpretation is wrong, I'll fix it. Otherwise, + * If drivers don't conform to it, they are wrong and need to be fixed. + */ + + if ( (max_qual->level == 0) && !(max_qual->updated & IW_QUAL_LEVEL_INVALID) /* Valid max_qual->level == 0 */ + && !(qual->updated & IW_QUAL_LEVEL_INVALID) /* Must have valid qual->level */ + && ( ((max_qual->noise > 0) && !(max_qual->updated & IW_QUAL_NOISE_INVALID)) /* Must have valid max_qual->noise */ + || ((qual->noise > 0) && !(qual->updated & IW_QUAL_NOISE_INVALID))) /* OR valid qual->noise */ + ) { + /* Absolute power values (dBm) */ + + /* Reasonable fallbacks for dumb drivers that don't specify either level. */ + #define FALLBACK_NOISE_FLOOR_DBM -90 + #define FALLBACK_SIGNAL_MAX_DBM -20 + int max_level = FALLBACK_SIGNAL_MAX_DBM; + int noise = FALLBACK_NOISE_FLOOR_DBM; + int level = qual->level - 0x100; + + level = CLAMP (level, FALLBACK_NOISE_FLOOR_DBM, FALLBACK_SIGNAL_MAX_DBM); + + if ((qual->noise > 0) && !(qual->updated & IW_QUAL_NOISE_INVALID)) + noise = qual->noise - 0x100; + else if ((max_qual->noise > 0) && !(max_qual->updated & IW_QUAL_NOISE_INVALID)) + noise = max_qual->noise - 0x100; + noise = CLAMP (noise, FALLBACK_NOISE_FLOOR_DBM, FALLBACK_SIGNAL_MAX_DBM - 1); + + /* A sort of signal-to-noise ratio calculation */ + level_percent = (int) (100 - 70 * (((double)max_level - (double)level) / + ((double)max_level - (double)noise))); + _LOGD (LOGD_WIFI, "QL1: level_percent is %d. max_level %d, level %d, noise_floor %d.", + level_percent, max_level, level, noise); + } else if ( (max_qual->level != 0) + && !(max_qual->updated & IW_QUAL_LEVEL_INVALID) /* Valid max_qual->level as upper bound */ + && !(qual->updated & IW_QUAL_LEVEL_INVALID)) { + /* Relative power values (RSSI) */ + + int level = qual->level; + + /* Signal level is relavtive (0 -> max_qual->level) */ + level = CLAMP (level, 0, max_qual->level); + level_percent = (int)(100 * ((double)level / (double)max_qual->level)); + _LOGD (LOGD_WIFI, "QL2: level_percent is %d. max_level %d, level %d.", + level_percent, max_qual->level, level); + } else if (percent == -1) { + _LOGD (LOGD_WIFI, "QL: Could not get quality %% value from driver. Driver is probably buggy."); + } + + /* If the quality percent was 0 or doesn't exist, then try to use signal levels instead */ + if ((percent < 1) && (level_percent >= 0)) + percent = level_percent; + + _LOGD (LOGD_WIFI, "QL: Final quality percent is %d (%d).", + percent, CLAMP (percent, 0, 100)); + return (CLAMP (percent, 0, 100)); +} + +static int +wifi_wext_get_qual (NMWifiUtils *data) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + struct iw_statistics stats; + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "get-qual")) + return FALSE; + + memset (&stats, 0, sizeof (stats)); + wrq.u.data.pointer = &stats; + wrq.u.data.length = sizeof (stats); + wrq.u.data.flags = 1; /* Clear updated flag */ + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + + if (ioctl (wext->fd, SIOCGIWSTATS, &wrq) < 0) { + _LOGW (LOGD_PLATFORM | LOGD_WIFI, + "(%s): error getting signal strength: %s", + ifname, strerror (errno)); + return -1; + } + + return wext_qual_to_percent (&stats.qual, &wext->max_qual); +} + +/*****************************************************************************/ +/* OLPC Mesh-only functions */ + +static guint32 +wifi_wext_get_mesh_channel (NMWifiUtils *data) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + guint32 freq; + int i; + + freq = nm_wifi_utils_get_freq (data); + for (i = 0; i < wext->num_freqs; i++) { + if (freq == wext->freqs[i]) + return i + 1; + } + return 0; +} + +static gboolean +wifi_wext_set_mesh_channel (NMWifiUtils *data, guint32 channel) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + char ifname[IFNAMSIZ]; + + if (!get_ifname (data->ifindex, ifname, "set-mesh-channel")) + return FALSE; + + memset (&wrq, 0, sizeof (struct iwreq)); + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + + if (channel > 0) { + wrq.u.freq.flags = IW_FREQ_FIXED; + wrq.u.freq.e = 0; + wrq.u.freq.m = 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)); + return FALSE; + } + + return TRUE; +} + +static gboolean +wifi_wext_set_mesh_ssid (NMWifiUtils *data, const guint8 *ssid, gsize len) +{ + NMWifiUtilsWext *wext = (NMWifiUtilsWext *) data; + struct iwreq wrq; + char buf[IW_ESSID_MAX_SIZE + 1]; + char ifname[IFNAMSIZ]; + int errsv; + + if (!get_ifname (data->ifindex, ifname, "set-mesh-ssid")) + return FALSE; + + memset (buf, 0, sizeof (buf)); + memcpy (buf, ssid, MIN (sizeof (buf) - 1, len)); + + wrq.u.essid.pointer = (caddr_t) buf; + wrq.u.essid.length = len; + wrq.u.essid.flags = (len > 0) ? 1 : 0; /* 1=enable SSID, 0=disable/any */ + + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + if (ioctl (wext->fd, SIOCSIWESSID, &wrq) == 0) + return TRUE; + + if (errno != ENODEV) { + gs_free char *ssid_str = NULL; + + errsv = errno; + _LOGE (LOGD_PLATFORM | LOGD_WIFI | LOGD_OLPC, + "(%s): error setting SSID to '%s': %s", + ifname, + (ssid_str = _nm_utils_ssid_to_string_arr (ssid, len)), + strerror (errsv)); + } + + return FALSE; +} + +/*****************************************************************************/ + +static gboolean +wext_can_scan_ifname (NMWifiUtilsWext *wext, const char *ifname) +{ + struct iwreq wrq; + + memset (&wrq, 0, sizeof (struct iwreq)); + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + if (ioctl (wext->fd, SIOCSIWSCAN, &wrq) < 0) { + if (errno == EOPNOTSUPP) + return FALSE; + } + return TRUE; +} + +static gboolean +wext_get_range_ifname (NMWifiUtilsWext *wext, + const char *ifname, + struct iw_range *range, + guint32 *response_len) +{ + int i = 26; + gboolean success = FALSE; + struct iwreq wrq; + + memset (&wrq, 0, sizeof (struct iwreq)); + nm_utils_ifname_cpy (wrq.ifr_name, ifname); + wrq.u.data.pointer = (caddr_t) range; + wrq.u.data.length = sizeof (struct iw_range); + + /* Need to give some drivers time to recover after suspend/resume + * (ex ipw3945 takes a few seconds to talk to its regulatory daemon; + * see rh bz#362421) + */ + while (i-- > 0) { + if (ioctl (wext->fd, SIOCGIWRANGE, &wrq) == 0) { + if (response_len) + *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; + } + + g_usleep (G_USEC_PER_SEC / 4); + } + + if (i <= 0) { + _LOGW (LOGD_PLATFORM | LOGD_WIFI, + "(%s): driver took too long to respond to IWRANGE query.", + ifname); + } + + return success; +} + +#define WPA_CAPS (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | \ + NM_WIFI_DEVICE_CAP_CIPHER_CCMP | \ + NM_WIFI_DEVICE_CAP_WPA | \ + NM_WIFI_DEVICE_CAP_RSN) + +static guint32 +wext_get_caps (NMWifiUtilsWext *wext, const char *ifname, struct iw_range *range) +{ + guint32 caps = NM_WIFI_DEVICE_CAP_NONE; + + g_return_val_if_fail (wext != NULL, NM_WIFI_DEVICE_CAP_NONE); + g_return_val_if_fail (range != NULL, NM_WIFI_DEVICE_CAP_NONE); + + /* All drivers should support WEP by default */ + caps |= NM_WIFI_DEVICE_CAP_CIPHER_WEP40 | NM_WIFI_DEVICE_CAP_CIPHER_WEP104; + + if (range->enc_capa & IW_ENC_CAPA_CIPHER_TKIP) + caps |= NM_WIFI_DEVICE_CAP_CIPHER_TKIP; + + if (range->enc_capa & IW_ENC_CAPA_CIPHER_CCMP) + caps |= NM_WIFI_DEVICE_CAP_CIPHER_CCMP; + + if (range->enc_capa & IW_ENC_CAPA_WPA) + caps |= NM_WIFI_DEVICE_CAP_WPA; + + if (range->enc_capa & IW_ENC_CAPA_WPA2) + caps |= NM_WIFI_DEVICE_CAP_RSN; + + /* Check for cipher support but not WPA support */ + if ( (caps & (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | NM_WIFI_DEVICE_CAP_CIPHER_CCMP)) + && !(caps & (NM_WIFI_DEVICE_CAP_WPA | NM_WIFI_DEVICE_CAP_RSN))) { + _LOGW (LOGD_WIFI, + "%s: device supports WPA ciphers but not WPA protocol; WPA unavailable.", + ifname); + caps &= ~WPA_CAPS; + } + + /* Check for WPA support but not cipher support */ + if ( (caps & (NM_WIFI_DEVICE_CAP_WPA | NM_WIFI_DEVICE_CAP_RSN)) + && !(caps & (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | NM_WIFI_DEVICE_CAP_CIPHER_CCMP))) { + _LOGW (LOGD_WIFI, + "%s: device supports WPA protocol but not WPA ciphers; WPA unavailable.", + ifname); + caps &= ~WPA_CAPS; + } + + /* There's no way to detect Ad-Hoc/AP mode support with WEXT + * (other than actually trying to do it), so just assume that + * Ad-Hoc is supported and AP isn't. + */ + caps |= NM_WIFI_DEVICE_CAP_ADHOC; + + return caps; +} + +/*****************************************************************************/ + +static void +nm_wifi_utils_wext_init (NMWifiUtilsWext *self) +{ +} + +static void +nm_wifi_utils_wext_class_init (NMWifiUtilsWextClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + NMWifiUtilsClass *wifi_utils_class = NM_WIFI_UTILS_CLASS (klass); + + object_class->dispose = dispose; + + wifi_utils_class->get_mode = wifi_wext_get_mode; + wifi_utils_class->set_mode = wifi_wext_set_mode; + wifi_utils_class->set_powersave = wifi_wext_set_powersave; + wifi_utils_class->get_freq = wifi_wext_get_freq; + wifi_utils_class->find_freq = wifi_wext_find_freq; + wifi_utils_class->get_bssid = wifi_wext_get_bssid; + wifi_utils_class->get_rate = wifi_wext_get_rate; + wifi_utils_class->get_qual = wifi_wext_get_qual; + wifi_utils_class->get_mesh_channel = wifi_wext_get_mesh_channel; + wifi_utils_class->set_mesh_channel = wifi_wext_set_mesh_channel; + wifi_utils_class->set_mesh_ssid = wifi_wext_set_mesh_ssid; +} + +NMWifiUtils * +nm_wifi_utils_wext_new (int ifindex, gboolean check_scan) +{ + NMWifiUtilsWext *wext; + struct iw_range range; + guint32 response_len = 0; + struct iw_range_with_scan_capa *scan_capa_range; + int i; + gboolean freq_valid = FALSE, has_5ghz = FALSE, has_2ghz = FALSE; + char ifname[IFNAMSIZ]; + + if (!nmp_utils_if_indextoname (ifindex, ifname)) { + _LOGW (LOGD_PLATFORM | LOGD_WIFI, + "can't determine interface name for ifindex %d", ifindex); + return NULL; + } + + wext = g_object_new (NM_TYPE_WIFI_UTILS_WEXT, NULL); + + wext->parent.ifindex = ifindex; + wext->fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (wext->fd < 0) + goto error; + + memset (&range, 0, sizeof (struct iw_range)); + if (wext_get_range_ifname (wext, ifname, &range, &response_len) == FALSE) { + _LOGI (LOGD_PLATFORM | LOGD_WIFI, "(%s): driver WEXT range request failed", + ifname); + goto error; + } + + if ((response_len < 300) || (range.we_version_compiled < 21)) { + _LOGI (LOGD_PLATFORM | LOGD_WIFI, + "(%s): driver WEXT version too old (got %d, expected >= 21)", + ifname, + range.we_version_compiled); + goto error; + } + + wext->max_qual.qual = range.max_qual.qual; + wext->max_qual.level = range.max_qual.level; + wext->max_qual.noise = range.max_qual.noise; + wext->max_qual.updated = range.max_qual.updated; + + wext->num_freqs = MIN (range.num_frequency, IW_MAX_FREQUENCIES); + for (i = 0; i < wext->num_freqs; i++) { + wext->freqs[i] = iw_freq_to_uint32 (&range.freq[i]); + freq_valid = TRUE; + if (wext->freqs[i] > 2400 && wext->freqs[i] < 2500) + has_2ghz = TRUE; + else if (wext->freqs[i] > 4900 && wext->freqs[i] < 6000) + has_5ghz = TRUE; + } + + /* Check for scanning capability; cards that can't scan are not supported */ + if (check_scan && (wext_can_scan_ifname (wext, ifname) == FALSE)) { + _LOGI (LOGD_PLATFORM | LOGD_WIFI, + "(%s): drivers that cannot scan are unsupported", + ifname); + goto error; + } + + /* Check for the ability to scan specific SSIDs. Until the scan_capa + * field gets added to wireless-tools, need to work around that by casting + * to the custom structure. + */ + scan_capa_range = (struct iw_range_with_scan_capa *) ⦥ + if (scan_capa_range->scan_capa & NM_IW_SCAN_CAPA_ESSID) { + _LOGI (LOGD_PLATFORM | LOGD_WIFI, + "(%s): driver supports SSID scans (scan_capa 0x%02X).", + ifname, + scan_capa_range->scan_capa); + } else { + _LOGI (LOGD_PLATFORM | LOGD_WIFI, + "(%s): driver does not support SSID scans (scan_capa 0x%02X).", + ifname, + scan_capa_range->scan_capa); + } + + wext->parent.caps = wext_get_caps (wext, ifname, &range); + if (freq_valid) + wext->parent.caps |= NM_WIFI_DEVICE_CAP_FREQ_VALID; + if (has_2ghz) + wext->parent.caps |= NM_WIFI_DEVICE_CAP_FREQ_2GHZ; + if (has_5ghz) + wext->parent.caps |= NM_WIFI_DEVICE_CAP_FREQ_5GHZ; + + _LOGI (LOGD_PLATFORM | LOGD_WIFI, + "(%s): using WEXT for WiFi device control", + ifname); + + return (NMWifiUtils *) wext; + +error: + g_object_unref (wext); + return NULL; +} + +gboolean +nm_wifi_utils_wext_is_wifi (const char *iface) +{ + int fd; + struct iwreq iwr; + gboolean is_wifi = FALSE; + + /* 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 + * exists. + * + * Note that wifi_wext_is_wifi() has only one caller which just verified + * that an interface with this name exists. + */ + + fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (fd >= 0) { + nm_utils_ifname_cpy (iwr.ifr_ifrn.ifrn_name, iface); + if (ioctl (fd, SIOCGIWNAME, &iwr) == 0) + is_wifi = TRUE; + nm_close (fd); + } + return is_wifi; +} diff --git a/src/platform/wifi/nm-wifi-utils-wext.h b/src/platform/wifi/nm-wifi-utils-wext.h new file mode 100644 index 00000000..44b11afb --- /dev/null +++ b/src/platform/wifi/nm-wifi-utils-wext.h @@ -0,0 +1,39 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2011 - 2018 Red Hat, Inc. + */ + +#ifndef __WIFI_UTILS_WEXT_H__ +#define __WIFI_UTILS_WEXT_H__ + +#include "nm-wifi-utils.h" + +#define NM_TYPE_WIFI_UTILS_WEXT (nm_wifi_utils_wext_get_type ()) +#define NM_WIFI_UTILS_WEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_WIFI_UTILS_WEXT, NMWifiUtilsWext)) +#define NM_WIFI_UTILS_WEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_WIFI_UTILS_WEXT, NMWifiUtilsWextClass)) +#define NM_IS_WIFI_UTILS_WEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_WIFI_UTILS_WEXT)) +#define NM_IS_WIFI_UTILS_WEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_WIFI_UTILS_WEXT)) +#define NM_WIFI_UTILS_WEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_WIFI_UTILS_WEXT, NMWifiUtilsWextClass)) + +GType nm_wifi_utils_wext_get_type (void); + +NMWifiUtils *nm_wifi_utils_wext_new (int ifindex, gboolean check_scan); + +gboolean nm_wifi_utils_wext_is_wifi (const char *iface); + +#endif /* __WIFI_UTILS_WEXT_H__ */ diff --git a/src/platform/wifi/nm-wifi-utils.c b/src/platform/wifi/nm-wifi-utils.c new file mode 100644 index 00000000..25d71c6a --- /dev/null +++ b/src/platform/wifi/nm-wifi-utils.c @@ -0,0 +1,240 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2005 - 2018 Red Hat, Inc. + * Copyright (C) 2006 - 2008 Novell, Inc. + */ + +#include "nm-default.h" + +#include "nm-wifi-utils.h" + +#include +#include +#include +#include + +#include "nm-wifi-utils-private.h" +#include "nm-wifi-utils-nl80211.h" +#if HAVE_WEXT +#include "nm-wifi-utils-wext.h" +#endif +#include "nm-core-utils.h" + +#include "platform/nm-platform-utils.h" + +G_DEFINE_ABSTRACT_TYPE (NMWifiUtils, nm_wifi_utils, G_TYPE_OBJECT) + +/*****************************************************************************/ + +static void +nm_wifi_utils_init (NMWifiUtils *self) +{ +} + +static void +nm_wifi_utils_class_init (NMWifiUtilsClass *klass) +{ +} + +NMWifiUtils * +nm_wifi_utils_new (int ifindex, struct nl_sock *genl, gboolean check_scan) +{ + NMWifiUtils *ret; + + g_return_val_if_fail (ifindex > 0, NULL); + + ret = nm_wifi_utils_nl80211_new (ifindex, genl); + +#if HAVE_WEXT + if (ret == NULL) + ret = nm_wifi_utils_wext_new (ifindex, check_scan); +#endif + + return ret; +} + +NMDeviceWifiCapabilities +nm_wifi_utils_get_caps (NMWifiUtils *data) +{ + g_return_val_if_fail (data != NULL, NM_WIFI_DEVICE_CAP_NONE); + + return data->caps; +} + +NM80211Mode +nm_wifi_utils_get_mode (NMWifiUtils *data) +{ + g_return_val_if_fail (data != NULL, NM_802_11_MODE_UNKNOWN); + return NM_WIFI_UTILS_GET_CLASS (data)->get_mode (data); +} + +gboolean +nm_wifi_utils_set_mode (NMWifiUtils *data, const NM80211Mode mode) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, FALSE); + g_return_val_if_fail ( (mode == NM_802_11_MODE_INFRA) + || (mode == NM_802_11_MODE_AP) + || (mode == NM_802_11_MODE_ADHOC), FALSE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + + /* nl80211 probably doesn't need this */ + return klass->set_mode ? klass->set_mode (data, mode) : TRUE; +} + +gboolean +nm_wifi_utils_set_powersave (NMWifiUtils *data, guint32 powersave) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, FALSE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + return klass->set_powersave ? klass->set_powersave (data, powersave) : TRUE; +} + +NMSettingWirelessWakeOnWLan +nm_wifi_utils_get_wake_on_wlan (NMWifiUtils *data) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + + return klass->get_wake_on_wlan ? klass->get_wake_on_wlan (data) : NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE; +} + +gboolean +nm_wifi_utils_set_wake_on_wlan (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wowl) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, FALSE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + return klass->set_wake_on_wlan ? klass->set_wake_on_wlan (data, wowl) : FALSE; +} + +guint32 +nm_wifi_utils_get_freq (NMWifiUtils *data) +{ + g_return_val_if_fail (data != NULL, 0); + return NM_WIFI_UTILS_GET_CLASS (data)->get_freq (data); +} + +guint32 +nm_wifi_utils_find_freq (NMWifiUtils *data, const guint32 *freqs) +{ + g_return_val_if_fail (data != NULL, 0); + g_return_val_if_fail (freqs != NULL, 0); + return NM_WIFI_UTILS_GET_CLASS (data)->find_freq (data, freqs); +} + +gboolean +nm_wifi_utils_get_bssid (NMWifiUtils *data, guint8 *out_bssid) +{ + g_return_val_if_fail (data != NULL, FALSE); + g_return_val_if_fail (out_bssid != NULL, FALSE); + + memset (out_bssid, 0, ETH_ALEN); + return NM_WIFI_UTILS_GET_CLASS (data)->get_bssid (data, out_bssid); +} + +guint32 +nm_wifi_utils_get_rate (NMWifiUtils *data) +{ + g_return_val_if_fail (data != NULL, 0); + return NM_WIFI_UTILS_GET_CLASS (data)->get_rate (data); +} + +int +nm_wifi_utils_get_qual (NMWifiUtils *data) +{ + g_return_val_if_fail (data != NULL, 0); + return NM_WIFI_UTILS_GET_CLASS (data)->get_qual (data); +} + +gboolean +nm_wifi_utils_is_wifi (int dirfd, const char *ifname) +{ + g_return_val_if_fail (dirfd >= 0, FALSE); + + if (faccessat (dirfd, "phy80211", F_OK, 0) == 0) + return TRUE; +#if HAVE_WEXT + if (nm_wifi_utils_wext_is_wifi (ifname)) + return TRUE; +#endif + return FALSE; +} + +/* OLPC Mesh-only functions */ + +guint32 +nm_wifi_utils_get_mesh_channel (NMWifiUtils *data) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, FALSE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + g_return_val_if_fail (klass->get_mesh_channel != NULL, FALSE); + + return klass->get_mesh_channel (data); +} + +gboolean +nm_wifi_utils_set_mesh_channel (NMWifiUtils *data, guint32 channel) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, FALSE); + g_return_val_if_fail (channel <= 13, FALSE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + g_return_val_if_fail (klass->set_mesh_channel != NULL, FALSE); + + return klass->set_mesh_channel (data, channel); +} + +gboolean +nm_wifi_utils_set_mesh_ssid (NMWifiUtils *data, const guint8 *ssid, gsize len) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, FALSE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + g_return_val_if_fail (klass->set_mesh_ssid != NULL, FALSE); + + return klass->set_mesh_ssid (data, ssid, len); +} + +gboolean +nm_wifi_utils_indicate_addressing_running (NMWifiUtils *data, gboolean running) +{ + NMWifiUtilsClass *klass; + + g_return_val_if_fail (data != NULL, FALSE); + + klass = NM_WIFI_UTILS_GET_CLASS (data); + return klass->indicate_addressing_running ? klass->indicate_addressing_running (data, running) : FALSE; +} diff --git a/src/platform/wifi/nm-wifi-utils.h b/src/platform/wifi/nm-wifi-utils.h new file mode 100644 index 00000000..6cd178bd --- /dev/null +++ b/src/platform/wifi/nm-wifi-utils.h @@ -0,0 +1,84 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2005 - 2018 Red Hat, Inc. + * Copyright (C) 2006 - 2008 Novell, Inc. + */ + +#ifndef __WIFI_UTILS_H__ +#define __WIFI_UTILS_H__ + +#include + +#include "nm-dbus-interface.h" +#include "nm-setting-wireless.h" +#include "platform/nm-netlink.h" + +typedef struct NMWifiUtils NMWifiUtils; + +#define NM_TYPE_WIFI_UTILS (nm_wifi_utils_get_type ()) +#define NM_WIFI_UTILS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_WIFI_UTILS, NMWifiUtils)) +#define NM_WIFI_UTILS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_WIFI_UTILS, NMWifiUtilsClass)) +#define NM_IS_WIFI_UTILS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_WIFI_UTILS)) +#define NM_IS_WIFI_UTILS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_WIFI_UTILS)) +#define NM_WIFI_UTILS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_WIFI_UTILS, NMWifiUtilsClass)) + +GType nm_wifi_utils_get_type (void); + +gboolean nm_wifi_utils_is_wifi (int dirfd, const char *ifname); + +NMWifiUtils *nm_wifi_utils_new (int ifindex, struct nl_sock *genl, gboolean check_scan); + +NMDeviceWifiCapabilities nm_wifi_utils_get_caps (NMWifiUtils *data); + +NM80211Mode nm_wifi_utils_get_mode (NMWifiUtils *data); + +gboolean nm_wifi_utils_set_mode (NMWifiUtils *data, const NM80211Mode mode); + +/* Returns frequency in MHz */ +guint32 nm_wifi_utils_get_freq (NMWifiUtils *data); + +/* Return the first supported frequency in the zero-terminated list. + * Frequencies are specified in MHz. */ +guint32 nm_wifi_utils_find_freq (NMWifiUtils *data, const guint32 *freqs); + +/* out_bssid must be ETH_ALEN bytes */ +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 */ +int nm_wifi_utils_get_qual (NMWifiUtils *data); + +/* Tells the driver DHCP or SLAAC is running */ +gboolean nm_wifi_utils_indicate_addressing_running (NMWifiUtils *data, gboolean running); + +gboolean nm_wifi_utils_set_powersave (NMWifiUtils *data, guint32 powersave); + +NMSettingWirelessWakeOnWLan nm_wifi_utils_get_wake_on_wlan (NMWifiUtils *data); + +gboolean nm_wifi_utils_set_wake_on_wlan (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wowl); + +/* OLPC Mesh-only functions */ +guint32 nm_wifi_utils_get_mesh_channel (NMWifiUtils *data); + +gboolean nm_wifi_utils_set_mesh_channel (NMWifiUtils *data, guint32 channel); + +gboolean nm_wifi_utils_set_mesh_ssid (NMWifiUtils *data, const guint8 *ssid, gsize len); + +#endif /* __WIFI_UTILS_H__ */ diff --git a/src/platform/wifi/wifi-utils-nl80211.c b/src/platform/wifi/wifi-utils-nl80211.c deleted file mode 100644 index 6c2ff3f5..00000000 --- a/src/platform/wifi/wifi-utils-nl80211.c +++ /dev/null @@ -1,988 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2005 - 2011 Red Hat, Inc. - * Copyright (C) 2006 - 2008 Novell, Inc. - * Copyright (C) 2011 Intel Corporation. All rights reserved. - */ - -#include "nm-default.h" - -#include "wifi-utils-nl80211.h" - -#include -#include -#include -#include -#include -#include - -#include "platform/nm-netlink.h" -#include "wifi-utils-private.h" -#include "platform/nm-platform.h" -#include "platform/nm-platform-utils.h" -#include "nm-utils.h" - -#define _NMLOG_PREFIX_NAME "wifi-nl80211" -#define _NMLOG(level, domain, ...) \ - G_STMT_START { \ - nm_log ((level), (domain), NULL, NULL, \ - "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ - _NMLOG_PREFIX_NAME \ - _NM_UTILS_MACRO_REST(__VA_ARGS__)); \ - } G_STMT_END - -typedef struct { - WifiData parent; - struct nl_sock *nl_sock; - guint32 *freqs; - int id; - int num_freqs; - int phy; - bool can_wowlan:1; -} WifiDataNl80211; - -static int -ack_handler (struct nl_msg *msg, void *arg) -{ - int *done = arg; - *done = 1; - return NL_STOP; -} - -static int -finish_handler (struct nl_msg *msg, void *arg) -{ - int *done = arg; - *done = 1; - return NL_SKIP; -} - -static int -error_handler (struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg) -{ - int *done = arg; - *done = err->error; - return NL_SKIP; -} - -static struct nl_msg * -_nl80211_alloc_msg (int id, int ifindex, int phy, guint32 cmd, guint32 flags) -{ - nm_auto_nlmsg struct nl_msg *msg = NULL; - - msg = nlmsg_alloc (); - genlmsg_put (msg, 0, 0, id, 0, flags, cmd, 0); - NLA_PUT_U32 (msg, NL80211_ATTR_IFINDEX, ifindex); - if (phy != -1) - NLA_PUT_U32 (msg, NL80211_ATTR_WIPHY, phy); - return g_steal_pointer (&msg); - -nla_put_failure: - return NULL; -} - -static struct nl_msg * -nl80211_alloc_msg (WifiDataNl80211 *nl80211, guint32 cmd, guint32 flags) -{ - return _nl80211_alloc_msg (nl80211->id, nl80211->parent.ifindex, nl80211->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) -{ - int err; - int done = 0; - const struct nl_cb cb = { - .err_cb = error_handler, - .err_arg = &done, - .finish_cb = finish_handler, - .finish_arg = &done, - .ack_cb = ack_handler, - .ack_arg = &done, - .valid_cb = valid_handler, - .valid_arg = valid_data, - }; - - g_return_val_if_fail (msg != NULL, -ENOMEM); - - err = nl_send_auto (nl_sock, msg); - if (err < 0) - return err; - - /* Loop until one of our NL callbacks says we're done; on success - * done will be 1, on error it will be < 0. - */ - while (!done) { - err = nl_recvmsgs (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 && - genlmsg_hdr (nlmsg_hdr (msg))->cmd == NL80211_CMD_GET_SCAN) - break; - - _LOGW (LOGD_WIFI, "nl_recvmsgs() error: (%d) %s", - err, nl_geterror (err)); - break; - } - } - - if (err >= 0 && done < 0) - err = done; - return err; -} - -static int -nl80211_send_and_recv (WifiDataNl80211 *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 -wifi_nl80211_deinit (WifiData *parent) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) parent; - - if (nl80211->nl_sock) - nl_socket_free (nl80211->nl_sock); - g_free (nl80211->freqs); -} - -struct nl80211_iface_info { - NM80211Mode mode; -}; - -static int -nl80211_iface_info_handler (struct nl_msg *msg, void *arg) -{ - struct nl80211_iface_info *info = 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) - return NL_SKIP; - - if (!tb[NL80211_ATTR_IFTYPE]) - return NL_SKIP; - - switch (nla_get_u32 (tb[NL80211_ATTR_IFTYPE])) { - case NL80211_IFTYPE_ADHOC: - info->mode = NM_802_11_MODE_ADHOC; - break; - case NL80211_IFTYPE_AP: - info->mode = NM_802_11_MODE_AP; - break; - case NL80211_IFTYPE_STATION: - info->mode = NM_802_11_MODE_INFRA; - break; - } - - return NL_SKIP; -} - -static NM80211Mode -wifi_nl80211_get_mode (WifiData *data) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) 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); - - if (nl80211_send_and_recv (nl80211, msg, nl80211_iface_info_handler, - &iface_info) < 0) - return NM_802_11_MODE_UNKNOWN; - - return iface_info.mode; -} - -static gboolean -wifi_nl80211_set_mode (WifiData *data, const NM80211Mode mode) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - nm_auto_nlmsg struct nl_msg *msg = NULL; - int err; - - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_SET_INTERFACE, 0); - - switch (mode) { - case NM_802_11_MODE_INFRA: - NLA_PUT_U32 (msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION); - break; - case NM_802_11_MODE_ADHOC: - NLA_PUT_U32 (msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_ADHOC); - break; - case NM_802_11_MODE_AP: - NLA_PUT_U32 (msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP); - break; - default: - g_assert_not_reached (); - } - - err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); - return err >= 0; - -nla_put_failure: - return FALSE; -} - -static gboolean -wifi_nl80211_set_powersave (WifiData *data, guint32 powersave) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - nm_auto_nlmsg struct nl_msg *msg = NULL; - int err; - - msg = nl80211_alloc_msg (nl80211, 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); - return err >= 0; - -nla_put_failure: - return FALSE; -} - -static gboolean -wifi_nl80211_set_wake_on_wlan (WifiData *data, NMSettingWirelessWakeOnWLan wowl) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - nm_auto_nlmsg struct nl_msg *msg = NULL; - struct nlattr *triggers; - int err; - - if (wowl == NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE) - return TRUE; - - msg = nl80211_alloc_msg(nl80211, NL80211_CMD_SET_WOWLAN, 0); - if (!msg) - return FALSE; - - triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS); - - if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_ANY)) - NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_ANY); - if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_DISCONNECT)) - NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_DISCONNECT); - if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_MAGIC)) - NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_MAGIC_PKT); - if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_GTK_REKEY_FAILURE)) - NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE); - if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_EAP_IDENTITY_REQUEST)) - NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST); - if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_4WAY_HANDSHAKE)) - NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE); - if (NM_FLAGS_HAS (wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_RFKILL_RELEASE)) - NLA_PUT_FLAG (msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE); - - nla_nest_end(msg, triggers); - - err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); - return err ? FALSE : TRUE; - -nla_put_failure: - return FALSE; -} - -/* @divisor: pass what value @xbm should be divided by to get dBm */ -static guint32 -nl80211_xbm_to_percent (gint32 xbm, guint32 divisor) -{ -#define NOISE_FLOOR_DBM -90 -#define SIGNAL_MAX_DBM -20 - - xbm /= divisor; - xbm = CLAMP (xbm, NOISE_FLOOR_DBM, SIGNAL_MAX_DBM); - - return 100 - 70 * (((float) SIGNAL_MAX_DBM - (float) xbm) / - ((float) SIGNAL_MAX_DBM - (float) NOISE_FLOOR_DBM)); -} - -struct nl80211_bss_info { - guint32 freq; - guint8 bssid[ETH_ALEN]; - guint8 ssid[32]; - guint32 ssid_len; - guint32 beacon_signal; - gboolean valid; -}; - -#define WLAN_EID_SSID 0 - -static void -find_ssid (guint8 *ies, guint32 ies_len, - guint8 **ssid, guint32 *ssid_len) -{ - *ssid = NULL; - *ssid_len = 0; - - while (ies_len > 2 && ies[0] != WLAN_EID_SSID) { - ies_len -= ies[1] + 2; - ies += ies[1] + 2; - } - if (ies_len < 2) - return; - if (ies_len < 2 + ies[1]) - return; - - *ssid_len = ies[1]; - *ssid = ies + 2; -} - -static int -nl80211_bss_dump_handler (struct nl_msg *msg, void *arg) -{ - 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 }, - }; - guint32 status; - - if (nla_parse (tb, NL80211_ATTR_MAX, 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)) - return NL_SKIP; - - if (bss[NL80211_BSS_STATUS] == NULL) - return NL_SKIP; - - status = nla_get_u32 (bss[NL80211_BSS_STATUS]); - - if (status != NL80211_BSS_STATUS_ASSOCIATED && - status != NL80211_BSS_STATUS_IBSS_JOINED) - return NL_SKIP; - - if (bss[NL80211_BSS_BSSID] == NULL) - return NL_SKIP; - memcpy (info->bssid, nla_data (bss[NL80211_BSS_BSSID]), ETH_ALEN); - - if (bss[NL80211_BSS_FREQUENCY]) - 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]); - - if (bss[NL80211_BSS_SIGNAL_MBM]) - 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)) { - memcpy (info->ssid, ssid, ssid_len); - info->ssid_len = ssid_len; - } - } - - info->valid = TRUE; - - return NL_SKIP; -} - -static void -nl80211_get_bss_info (WifiDataNl80211 *nl80211, - 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); - - nl80211_send_and_recv (nl80211, msg, nl80211_bss_dump_handler, bss_info); -} - -static guint32 -wifi_nl80211_get_freq (WifiData *data) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - struct nl80211_bss_info bss_info; - - nl80211_get_bss_info (nl80211, &bss_info); - - return bss_info.freq; -} - -static guint32 -wifi_nl80211_find_freq (WifiData *data, const guint32 *freqs) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - int i; - - for (i = 0; i < nl80211->num_freqs; i++) { - while (*freqs) { - if (nl80211->freqs[i] == *freqs) - return *freqs; - freqs++; - } - } - return 0; -} - -static gboolean -wifi_nl80211_get_bssid (WifiData *data, guint8 *out_bssid) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - struct nl80211_bss_info bss_info; - - nl80211_get_bss_info (nl80211, &bss_info); - - if (bss_info.valid) - memcpy (out_bssid, bss_info.bssid, ETH_ALEN); - - return bss_info.valid; -} - -struct nl80211_station_info { - guint32 txrate; - gboolean txrate_valid; - guint8 signal; - gboolean signal_valid; -}; - -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] = { - [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 }, - }; - - 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 }, - [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, - [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, - }; - - if (nla_parse (tb, NL80211_ATTR_MAX, 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)) - 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)) - return NL_SKIP; - - if (rinfo[NL80211_RATE_INFO_BITRATE] == NULL) - return NL_SKIP; - - /* convert from nl80211's units of 100kbps to NM's kbps */ - info->txrate = nla_get_u16 (rinfo[NL80211_RATE_INFO_BITRATE]) * 100; - info->txrate_valid = TRUE; - - if (sinfo[NL80211_STA_INFO_SIGNAL] != NULL) { - info->signal = nl80211_xbm_to_percent ((gint8) nla_get_u8 (sinfo[NL80211_STA_INFO_SIGNAL]), 1); - info->signal_valid = TRUE; - } - - return NL_SKIP; -} - -static void -nl80211_get_ap_info (WifiDataNl80211 *nl80211, - struct nl80211_station_info *sta_info) -{ - nm_auto_nlmsg struct nl_msg *msg = NULL; - struct nl80211_bss_info bss_info; - - memset (sta_info, 0, sizeof (*sta_info)); - - nl80211_get_bss_info (nl80211, &bss_info); - if (!bss_info.valid) - return; - - msg = nl80211_alloc_msg (nl80211, 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); - if (!sta_info->signal_valid) { - /* Fall back to bss_info signal quality (both are in percent) */ - sta_info->signal = bss_info.beacon_signal; - } - } - - return; - -nla_put_failure: - return; -} - -static guint32 -wifi_nl80211_get_rate (WifiData *data) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - struct nl80211_station_info sta_info; - - nl80211_get_ap_info (nl80211, &sta_info); - - return sta_info.txrate; -} - -static int -wifi_nl80211_get_qual (WifiData *data) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - struct nl80211_station_info sta_info; - - nl80211_get_ap_info (nl80211, &sta_info); - return sta_info.signal; -} - -static gboolean -wifi_nl80211_indicate_addressing_running (WifiData *data, gboolean running) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - nm_auto_nlmsg struct nl_msg *msg = NULL; - int err; - - msg = nl80211_alloc_msg (nl80211, - running - ? 98 /* NL80211_CMD_CRIT_PROTOCOL_START */ - : 99 /* NL80211_CMD_CRIT_PROTOCOL_STOP */, - 0); - /* Despite the DHCP name, we're using this for any type of IP addressing, - * DHCPv4, DHCPv6, and IPv6 SLAAC. - */ - NLA_PUT_U16 (msg, - 179 /* NL80211_ATTR_CRIT_PROT_ID */, - 1 /* NL80211_CRIT_PROTO_DHCP */); - if (running) { - /* Give DHCP 5 seconds to complete */ - NLA_PUT_U16 (msg, - 180 /* NL80211_ATTR_MAX_CRIT_PROT_DURATION */, - 5000); - } - - err = nl80211_send_and_recv (nl80211, msg, NULL, NULL); - return err >= 0; - -nla_put_failure: - return FALSE; -} - -struct nl80211_wowlan_info { - gboolean enabled; -}; - -static int -nl80211_wowlan_handler (struct nl_msg *msg, void *arg) -{ - struct nlattr *tb[NL80211_ATTR_MAX + 1]; - struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); - struct nl80211_wowlan_info *info = arg; - - info->enabled = FALSE; - - if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), - genlmsg_attrlen (gnlh, 0), NULL) < 0) - return NL_SKIP; - - if (tb[NL80211_ATTR_WOWLAN_TRIGGERS]) - info->enabled = TRUE; - - return NL_SKIP; -} - -static gboolean -wifi_nl80211_get_wowlan (WifiData *data) -{ - WifiDataNl80211 *nl80211 = (WifiDataNl80211 *) data; - nm_auto_nlmsg struct nl_msg *msg = NULL; - struct nl80211_wowlan_info info; - - if (!nl80211->can_wowlan) - return FALSE; - - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_WOWLAN, 0); - nl80211_send_and_recv (nl80211, msg, nl80211_wowlan_handler, &info); - return info.enabled; -} - -struct nl80211_device_info { - int phy; - guint32 *freqs; - int num_freqs; - guint32 caps; - gboolean can_scan; - gboolean can_scan_ssid; - gboolean supported; - gboolean success; - gboolean can_wowlan; -}; - -#define WLAN_CIPHER_SUITE_USE_GROUP 0x000FAC00 -#define WLAN_CIPHER_SUITE_WEP40 0x000FAC01 -#define WLAN_CIPHER_SUITE_TKIP 0x000FAC02 -#define WLAN_CIPHER_SUITE_CCMP 0x000FAC04 -#define WLAN_CIPHER_SUITE_WEP104 0x000FAC05 -#define WLAN_CIPHER_SUITE_AES_CMAC 0x000FAC06 -#define WLAN_CIPHER_SUITE_GCMP 0x000FAC08 -#define WLAN_CIPHER_SUITE_SMS4 0x00147201 - -static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg) -{ - struct nlattr *tb[NL80211_ATTR_MAX + 1]; - struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); - struct nl80211_device_info *info = arg; - struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; - struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; - 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); -#else - G_STATIC_ASSERT (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) - return NL_SKIP; - - if ( tb[NL80211_ATTR_WIPHY] == NULL - || tb[NL80211_ATTR_WIPHY_BANDS] == NULL) - return NL_SKIP; - - info->phy = nla_get_u32 (tb[NL80211_ATTR_WIPHY]); - - if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) { - info->can_scan_ssid = - nla_get_u8 (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) > 0; - } else { - /* old kernel that only had mac80211, so assume it can */ - info->can_scan_ssid = TRUE; - } - - if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) { - struct nlattr *nl_cmd; - int i; - - nla_for_each_nested (nl_cmd, tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) { - switch (nla_get_u32 (nl_cmd)) { - case NL80211_CMD_TRIGGER_SCAN: - info->can_scan = TRUE; - break; - case NL80211_CMD_CONNECT: - case NL80211_CMD_AUTHENTICATE: - /* Only devices that support CONNECT or AUTH actually support - * 802.11, unlike say ipw2x00 (up to at least kernel 3.4) which - * has minimal info support, but no actual command support. - * This check mirrors what wpa_supplicant does to determine - * whether or not to use the nl80211 driver. - */ - info->supported = TRUE; - break; - default: - break; - } - } - } - - /* Find number of supported frequencies */ - 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) - 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) - continue; - - if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) - continue; - - info->num_freqs++; - } - } - - /* Read supported frequencies */ - info->freqs = g_malloc0 (sizeof (guint32) * info->num_freqs); - - 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) - 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) - continue; - - if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) - continue; - - info->freqs[freq_idx] = - nla_get_u32 (tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); - - info->caps |= NM_WIFI_DEVICE_CAP_FREQ_VALID; - - if (info->freqs[freq_idx] > 2400 && info->freqs[freq_idx] < 2500) - info->caps |= NM_WIFI_DEVICE_CAP_FREQ_2GHZ; - if (info->freqs[freq_idx] > 4900 && info->freqs[freq_idx] < 6000) - info->caps |= NM_WIFI_DEVICE_CAP_FREQ_5GHZ; - - freq_idx++; - } - } - - /* Read security/encryption support */ - if (tb[NL80211_ATTR_CIPHER_SUITES]) { - int num; - int i; - __u32 *ciphers = nla_data (tb[NL80211_ATTR_CIPHER_SUITES]); - - num = nla_len (tb[NL80211_ATTR_CIPHER_SUITES]) / sizeof (__u32); - for (i = 0; i < num; i++) { - switch (ciphers[i]) { - case WLAN_CIPHER_SUITE_WEP40: - info->caps |= NM_WIFI_DEVICE_CAP_CIPHER_WEP40; - break; - case WLAN_CIPHER_SUITE_WEP104: - info->caps |= NM_WIFI_DEVICE_CAP_CIPHER_WEP104; - break; - case WLAN_CIPHER_SUITE_TKIP: - info->caps |= (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | - NM_WIFI_DEVICE_CAP_WPA); - break; - case WLAN_CIPHER_SUITE_CCMP: - info->caps |= (NM_WIFI_DEVICE_CAP_CIPHER_CCMP | - NM_WIFI_DEVICE_CAP_RSN); - break; - case WLAN_CIPHER_SUITE_AES_CMAC: - case WLAN_CIPHER_SUITE_GCMP: - case WLAN_CIPHER_SUITE_SMS4: - break; - default: - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "don't know the meaning of NL80211_ATTR_CIPHER_SUITE %#8.8x.", - ciphers[i]); - break; - } - } - } - - if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) { - struct nlattr *nl_mode; - int i; - - nla_for_each_nested (nl_mode, tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) { - if (nla_type (nl_mode) == NL80211_IFTYPE_AP) - info->caps |= NM_WIFI_DEVICE_CAP_AP; - else if (nla_type (nl_mode) == NL80211_IFTYPE_ADHOC) - info->caps |= NM_WIFI_DEVICE_CAP_ADHOC; - } - } - - if (tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]) - info->can_wowlan = TRUE; - - info->success = TRUE; - - return NL_SKIP; -} - -WifiData * -wifi_nl80211_init (int ifindex) -{ - static const WifiDataClass klass = { - .struct_size = sizeof (WifiDataNl80211), - .get_mode = wifi_nl80211_get_mode, - .set_mode = wifi_nl80211_set_mode, - .set_powersave = wifi_nl80211_set_powersave, - .set_wake_on_wlan = wifi_nl80211_set_wake_on_wlan, - .get_freq = wifi_nl80211_get_freq, - .find_freq = wifi_nl80211_find_freq, - .get_bssid = wifi_nl80211_get_bssid, - .get_rate = wifi_nl80211_get_rate, - .get_qual = wifi_nl80211_get_qual, - .get_wowlan = wifi_nl80211_get_wowlan, - .indicate_addressing_running = wifi_nl80211_indicate_addressing_running, - .deinit = wifi_nl80211_deinit, - }; - WifiDataNl80211 *nl80211; - nm_auto_nlmsg struct nl_msg *msg = NULL; - struct nl80211_device_info device_info = {}; - char ifname[IFNAMSIZ]; - - 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 = wifi_data_new (&klass, ifindex); - - nl80211->nl_sock = nl_socket_alloc (); - if (nl80211->nl_sock == NULL) - goto error; - - if (nl_connect (nl80211->nl_sock, NETLINK_GENERIC)) - goto error; - - nl80211->id = genl_ctrl_resolve (nl80211->nl_sock, "nl80211"); - if (nl80211->id < 0) { - _LOGD (LOGD_WIFI, "genl_ctrl_resolve: failed to resolve \"nl80211\""); - goto error; - } - - nl80211->phy = -1; - - msg = nl80211_alloc_msg (nl80211, NL80211_CMD_GET_WIPHY, 0); - - if (nl80211_send_and_recv (nl80211, msg, nl80211_wiphy_info_handler, - &device_info) < 0) { - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "(%s): NL80211_CMD_GET_WIPHY request failed", - ifname); - goto error; - } - - if (!device_info.success) { - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "(%s): NL80211_CMD_GET_WIPHY request indicated failure", - ifname); - goto error; - } - - if (!device_info.supported) { - _LOGD (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver does not fully support nl80211, falling back to WEXT", - ifname); - goto error; - } - - if (!device_info.can_scan_ssid) { - _LOGE (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver does not support SSID scans", - ifname); - goto error; - } - - if (device_info.num_freqs == 0 || device_info.freqs == NULL) { - nm_log_err (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver reports no supported frequencies", - ifname); - goto error; - } - - if (device_info.caps == 0) { - _LOGE (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver doesn't report support of any encryption", - ifname); - goto error; - } - - 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; - - _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): using nl80211 for WiFi device control", - ifname); - return (WifiData *) nl80211; - -error: - wifi_utils_unref ((WifiData *) nl80211); - return NULL; -} - diff --git a/src/platform/wifi/wifi-utils-nl80211.h b/src/platform/wifi/wifi-utils-nl80211.h deleted file mode 100644 index aff24555..00000000 --- a/src/platform/wifi/wifi-utils-nl80211.h +++ /dev/null @@ -1,28 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2011 Intel Corporation. All rights reserved. - */ - -#ifndef __WIFI_UTILS_NL80211_H__ -#define __WIFI_UTILS_NL80211_H__ - -#include "wifi-utils.h" - -WifiData *wifi_nl80211_init (int ifindex); - -#endif /* __WIFI_UTILS_NL80211_H__ */ diff --git a/src/platform/wifi/wifi-utils-private.h b/src/platform/wifi/wifi-utils-private.h deleted file mode 100644 index 627108cb..00000000 --- a/src/platform/wifi/wifi-utils-private.h +++ /dev/null @@ -1,81 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2011 Red Hat, Inc. - */ - -#ifndef __WIFI_UTILS_PRIVATE_H__ -#define __WIFI_UTILS_PRIVATE_H__ - -#include "nm-dbus-interface.h" -#include "wifi-utils.h" - -typedef struct { - gsize struct_size; - - NM80211Mode (*get_mode) (WifiData *data); - - gboolean (*set_mode) (WifiData *data, const NM80211Mode mode); - - /* Set power saving mode on an interface */ - gboolean (*set_powersave) (WifiData *data, guint32 powersave); - - /* Set WakeOnWLAN mode on an interface */ - gboolean (*set_wake_on_wlan) (WifiData *data, NMSettingWirelessWakeOnWLan wowl); - - /* Return current frequency in MHz (really associated BSS frequency) */ - guint32 (*get_freq) (WifiData *data); - - /* Return first supported frequency in the zero-terminated list */ - guint32 (*find_freq) (WifiData *data, const guint32 *freqs); - - /* Return current bitrate in Kbps */ - guint32 (*get_rate) (WifiData *data); - - gboolean (*get_bssid) (WifiData *data, guint8 *out_bssid); - - /* Return a signal strength percentage 0 - 100% for the current BSSID; - * return -1 on errors or if not associated. - */ - int (*get_qual) (WifiData *data); - - void (*deinit) (WifiData *data); - - gboolean (*get_wowlan) (WifiData *data); - - /* OLPC Mesh-only functions */ - - guint32 (*get_mesh_channel) (WifiData *data); - - /* channel == 0 means "auto channel" */ - gboolean (*set_mesh_channel) (WifiData *data, guint32 channel); - - /* ssid == NULL means "auto SSID" */ - gboolean (*set_mesh_ssid) (WifiData *data, const guint8 *ssid, gsize len); - - gboolean (*indicate_addressing_running) (WifiData *data, gboolean running); -} WifiDataClass; - -struct WifiData { - const WifiDataClass *klass; - int ifindex; - NMDeviceWifiCapabilities caps; -}; - -gpointer wifi_data_new (const WifiDataClass *klass, int ifindex); - -#endif /* __WIFI_UTILS_PRIVATE_H__ */ diff --git a/src/platform/wifi/wifi-utils-wext.c b/src/platform/wifi/wifi-utils-wext.c deleted file mode 100644 index c8744f79..00000000 --- a/src/platform/wifi/wifi-utils-wext.c +++ /dev/null @@ -1,766 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2005 - 2011 Red Hat, Inc. - * Copyright (C) 2006 - 2008 Novell, Inc. - */ - -#include "nm-default.h" - -#include "wifi-utils-wext.h" - -#include -#include -#include -#include -#include - -/* Hacks necessary to #include wireless.h; yay for WEXT */ -#ifndef __user -#define __user -#endif -#include -#include -#include -#include - -#include "wifi-utils-private.h" -#include "nm-utils.h" -#include "platform/nm-platform-utils.h" - -typedef struct { - WifiData parent; - int fd; - struct iw_quality max_qual; - gint8 num_freqs; - guint32 freqs[IW_MAX_FREQUENCIES]; -} WifiDataWext; - -/* Until a new wireless-tools comes out that has the defs and the structure, - * need to copy them here. - */ -/* Scan capability flags - in (struct iw_range *)->scan_capa */ -#define NM_IW_SCAN_CAPA_NONE 0x00 -#define NM_IW_SCAN_CAPA_ESSID 0x01 - -struct iw_range_with_scan_capa -{ - guint32 throughput; - guint32 min_nwid; - guint32 max_nwid; - guint16 old_num_channels; - guint8 old_num_frequency; - - guint8 scan_capa; - /* don't need the rest... */ -}; - -#define _NMLOG_PREFIX_NAME "wifi-wext" -#define _NMLOG(level, domain, ...) \ - G_STMT_START { \ - nm_log ((level), (domain), NULL, NULL, \ - "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ - _NMLOG_PREFIX_NAME \ - _NM_UTILS_MACRO_REST(__VA_ARGS__)); \ - } G_STMT_END - -static guint32 -iw_freq_to_uint32 (const struct iw_freq *freq) -{ - if (freq->e == 0) { - /* Some drivers report channel not frequency. Convert to a - * frequency; but this assumes that the device is in b/g mode. - */ - if ((freq->m >= 1) && (freq->m <= 13)) - return 2407 + (5 * freq->m); - else if (freq->m == 14) - return 2484; - } - return (guint32) ((((double) freq->m) * nm_utils_exp10 (freq->e)) / 1000000.0); -} - -static void -wifi_wext_deinit (WifiData *parent) -{ - WifiDataWext *wext = (WifiDataWext *) parent; - - nm_close (wext->fd); -} - -static gboolean -get_ifname (int ifindex, char *buffer, const char *op) -{ - int errsv; - - if (!nmp_utils_if_indextoname (ifindex, buffer)) { - errsv = errno; - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "error getting interface name for ifindex %d, operation '%s': %s (%d)", - ifindex, op, g_strerror (errsv), errsv); - return FALSE; - } - - return TRUE; -} - -static NM80211Mode -wifi_wext_get_mode_ifname (WifiData *data, const char *ifname) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - - memset (&wrq, 0, sizeof (struct iwreq)); - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - - if (ioctl (wext->fd, SIOCGIWMODE, &wrq) < 0) { - if (errno != ENODEV) { - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "(%s): error %d getting card mode", - ifname, errno); - } - return NM_802_11_MODE_UNKNOWN; - } - - switch (wrq.u.mode) { - case IW_MODE_ADHOC: - return NM_802_11_MODE_ADHOC; - case IW_MODE_MASTER: - return NM_802_11_MODE_AP; - case IW_MODE_INFRA: - case IW_MODE_AUTO: /* hack for WEXT devices reporting IW_MODE_AUTO */ - return NM_802_11_MODE_INFRA; - default: - break; - } - return NM_802_11_MODE_UNKNOWN; -} - -static NM80211Mode -wifi_wext_get_mode (WifiData *data) -{ - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "get-mode")) - return FALSE; - - return wifi_wext_get_mode_ifname (data, ifname); -} - -static gboolean -wifi_wext_set_mode (WifiData *data, const NM80211Mode mode) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "set-mode")) - return FALSE; - - if (wifi_wext_get_mode_ifname (data, ifname) == mode) - return TRUE; - - memset (&wrq, 0, sizeof (struct iwreq)); - switch (mode) { - case NM_802_11_MODE_ADHOC: - wrq.u.mode = IW_MODE_ADHOC; - break; - case NM_802_11_MODE_AP: - wrq.u.mode = IW_MODE_MASTER; - break; - case NM_802_11_MODE_INFRA: - wrq.u.mode = IW_MODE_INFRA; - break; - default: - g_warn_if_reached (); - return FALSE; - } - - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - if (ioctl (wext->fd, SIOCSIWMODE, &wrq) < 0) { - if (errno != ENODEV) { - _LOGE (LOGD_PLATFORM | LOGD_WIFI, - "(%s): error setting mode %d", - ifname, mode); - } - return FALSE; - } - - return TRUE; -} - -static gboolean -wifi_wext_set_powersave (WifiData *data, guint32 powersave) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "set-powersave")) - return FALSE; - - memset (&wrq, 0, sizeof (struct iwreq)); - if (powersave == 1) { - wrq.u.power.flags = IW_POWER_ALL_R; - } else - wrq.u.power.disabled = 1; - - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - if (ioctl (wext->fd, SIOCSIWPOWER, &wrq) < 0) { - if (errno != ENODEV) { - _LOGE (LOGD_PLATFORM | LOGD_WIFI, - "(%s): error setting powersave %" G_GUINT32_FORMAT, - ifname, powersave); - } - return FALSE; - } - - return TRUE; -} - -static guint32 -wifi_wext_get_freq (WifiData *data) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "get-freq")) - return FALSE; - - memset (&wrq, 0, sizeof (struct iwreq)); - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - if (ioctl (wext->fd, SIOCGIWFREQ, &wrq) < 0) { - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "(%s): error getting frequency: %s", - ifname, strerror (errno)); - return 0; - } - - return iw_freq_to_uint32 (&wrq.u.freq); -} - -static guint32 -wifi_wext_find_freq (WifiData *data, const guint32 *freqs) -{ - WifiDataWext *wext = (WifiDataWext *) data; - int i; - - for (i = 0; i < wext->num_freqs; i++) { - while (*freqs) { - if (wext->freqs[i] == *freqs) - return *freqs; - freqs++; - } - } - return 0; -} - -static gboolean -wifi_wext_get_bssid (WifiData *data, guint8 *out_bssid) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "get-bssid")) - return FALSE; - - memset (&wrq, 0, sizeof (wrq)); - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - if (ioctl (wext->fd, SIOCGIWAP, &wrq) < 0) { - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "(%s): error getting associated BSSID: %s", - ifname, strerror (errno)); - return FALSE; - } - memcpy (out_bssid, &(wrq.u.ap_addr.sa_data), ETH_ALEN); - return TRUE; -} - -static guint32 -wifi_wext_get_rate (WifiData *data) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - int err; - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "get-rate")) - return FALSE; - - memset (&wrq, 0, sizeof (wrq)); - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - err = ioctl (wext->fd, SIOCGIWRATE, &wrq); - return ((err == 0) ? wrq.u.bitrate.value / 1000 : 0); -} - -static int -wext_qual_to_percent (const struct iw_quality *qual, - const struct iw_quality *max_qual) -{ - int percent = -1; - int level_percent = -1; - - g_return_val_if_fail (qual != NULL, -1); - g_return_val_if_fail (max_qual != NULL, -1); - - /* Magically convert the many different WEXT quality representations to a percentage */ - - _LOGD (LOGD_WIFI, - "QL: qual %d/%u/0x%X, level %d/%u/0x%X, noise %d/%u/0x%X, updated: 0x%X ** MAX: qual %d/%u/0x%X, level %d/%u/0x%X, noise %d/%u/0x%X, updated: 0x%X", - (__s8) qual->qual, qual->qual, qual->qual, - (__s8) qual->level, qual->level, qual->level, - (__s8) qual->noise, qual->noise, qual->noise, - qual->updated, - (__s8) max_qual->qual, max_qual->qual, max_qual->qual, - (__s8) max_qual->level, max_qual->level, max_qual->level, - (__s8) max_qual->noise, max_qual->noise, max_qual->noise, - max_qual->updated); - - /* Try using the card's idea of the signal quality first as long as it tells us what the max quality is. - * Drivers that fill in quality values MUST treat them as percentages, ie the "Link Quality" MUST be - * bounded by 0 and max_qual->qual, and MUST change in a linear fashion. Within those bounds, drivers - * are free to use whatever they want to calculate "Link Quality". - */ - if ((max_qual->qual != 0) && !(max_qual->updated & IW_QUAL_QUAL_INVALID) && !(qual->updated & IW_QUAL_QUAL_INVALID)) - percent = (int)(100 * ((double)qual->qual / (double)max_qual->qual)); - - /* If the driver doesn't specify a complete and valid quality, we have two options: - * - * 1) dBm: driver must specify max_qual->level = 0, and have valid values for - * qual->level and (qual->noise OR max_qual->noise) - * 2) raw RSSI: driver must specify max_qual->level > 0, and have valid values for - * qual->level and max_qual->level - * - * This is the WEXT spec. If this interpretation is wrong, I'll fix it. Otherwise, - * If drivers don't conform to it, they are wrong and need to be fixed. - */ - - if ( (max_qual->level == 0) && !(max_qual->updated & IW_QUAL_LEVEL_INVALID) /* Valid max_qual->level == 0 */ - && !(qual->updated & IW_QUAL_LEVEL_INVALID) /* Must have valid qual->level */ - && ( ((max_qual->noise > 0) && !(max_qual->updated & IW_QUAL_NOISE_INVALID)) /* Must have valid max_qual->noise */ - || ((qual->noise > 0) && !(qual->updated & IW_QUAL_NOISE_INVALID))) /* OR valid qual->noise */ - ) { - /* Absolute power values (dBm) */ - - /* Reasonable fallbacks for dumb drivers that don't specify either level. */ - #define FALLBACK_NOISE_FLOOR_DBM -90 - #define FALLBACK_SIGNAL_MAX_DBM -20 - int max_level = FALLBACK_SIGNAL_MAX_DBM; - int noise = FALLBACK_NOISE_FLOOR_DBM; - int level = qual->level - 0x100; - - level = CLAMP (level, FALLBACK_NOISE_FLOOR_DBM, FALLBACK_SIGNAL_MAX_DBM); - - if ((qual->noise > 0) && !(qual->updated & IW_QUAL_NOISE_INVALID)) - noise = qual->noise - 0x100; - else if ((max_qual->noise > 0) && !(max_qual->updated & IW_QUAL_NOISE_INVALID)) - noise = max_qual->noise - 0x100; - noise = CLAMP (noise, FALLBACK_NOISE_FLOOR_DBM, FALLBACK_SIGNAL_MAX_DBM - 1); - - /* A sort of signal-to-noise ratio calculation */ - level_percent = (int) (100 - 70 * (((double)max_level - (double)level) / - ((double)max_level - (double)noise))); - _LOGD (LOGD_WIFI, "QL1: level_percent is %d. max_level %d, level %d, noise_floor %d.", - level_percent, max_level, level, noise); - } else if ( (max_qual->level != 0) - && !(max_qual->updated & IW_QUAL_LEVEL_INVALID) /* Valid max_qual->level as upper bound */ - && !(qual->updated & IW_QUAL_LEVEL_INVALID)) { - /* Relative power values (RSSI) */ - - int level = qual->level; - - /* Signal level is relavtive (0 -> max_qual->level) */ - level = CLAMP (level, 0, max_qual->level); - level_percent = (int)(100 * ((double)level / (double)max_qual->level)); - _LOGD (LOGD_WIFI, "QL2: level_percent is %d. max_level %d, level %d.", - level_percent, max_qual->level, level); - } else if (percent == -1) { - _LOGD (LOGD_WIFI, "QL: Could not get quality %% value from driver. Driver is probably buggy."); - } - - /* If the quality percent was 0 or doesn't exist, then try to use signal levels instead */ - if ((percent < 1) && (level_percent >= 0)) - percent = level_percent; - - _LOGD (LOGD_WIFI, "QL: Final quality percent is %d (%d).", - percent, CLAMP (percent, 0, 100)); - return (CLAMP (percent, 0, 100)); -} - -static int -wifi_wext_get_qual (WifiData *data) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - struct iw_statistics stats; - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "get-qual")) - return FALSE; - - memset (&stats, 0, sizeof (stats)); - wrq.u.data.pointer = &stats; - wrq.u.data.length = sizeof (stats); - wrq.u.data.flags = 1; /* Clear updated flag */ - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - - if (ioctl (wext->fd, SIOCGIWSTATS, &wrq) < 0) { - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "(%s): error getting signal strength: %s", - ifname, strerror (errno)); - return -1; - } - - return wext_qual_to_percent (&stats.qual, &wext->max_qual); -} - -/*****************************************************************************/ -/* OLPC Mesh-only functions */ - -static guint32 -wifi_wext_get_mesh_channel (WifiData *data) -{ - WifiDataWext *wext = (WifiDataWext *) data; - guint32 freq; - int i; - - freq = wifi_utils_get_freq (data); - for (i = 0; i < wext->num_freqs; i++) { - if (freq == wext->freqs[i]) - return i + 1; - } - return 0; -} - -static gboolean -wifi_wext_set_mesh_channel (WifiData *data, guint32 channel) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - char ifname[IFNAMSIZ]; - - if (!get_ifname (data->ifindex, ifname, "set-mesh-channel")) - return FALSE; - - memset (&wrq, 0, sizeof (struct iwreq)); - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - - if (channel > 0) { - wrq.u.freq.flags = IW_FREQ_FIXED; - wrq.u.freq.e = 0; - wrq.u.freq.m = 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)); - return FALSE; - } - - return TRUE; -} - -static gboolean -wifi_wext_set_mesh_ssid (WifiData *data, const guint8 *ssid, gsize len) -{ - WifiDataWext *wext = (WifiDataWext *) data; - struct iwreq wrq; - char buf[IW_ESSID_MAX_SIZE + 1]; - char ifname[IFNAMSIZ]; - int errsv; - - if (!get_ifname (data->ifindex, ifname, "set-mesh-ssid")) - return FALSE; - - memset (buf, 0, sizeof (buf)); - memcpy (buf, ssid, MIN (sizeof (buf) - 1, len)); - - wrq.u.essid.pointer = (caddr_t) buf; - wrq.u.essid.length = len; - wrq.u.essid.flags = (len > 0) ? 1 : 0; /* 1=enable SSID, 0=disable/any */ - - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - if (ioctl (wext->fd, SIOCSIWESSID, &wrq) == 0) - return TRUE; - - if (errno != ENODEV) { - errsv = errno; - _LOGE (LOGD_PLATFORM | LOGD_WIFI | LOGD_OLPC, - "(%s): error setting SSID to '%s': %s", - ifname, - ssid ? nm_utils_escape_ssid (ssid, len) : "(null)", - strerror (errsv)); - } - - return FALSE; -} - -/*****************************************************************************/ - -static gboolean -wext_can_scan_ifname (WifiDataWext *wext, const char *ifname) -{ - struct iwreq wrq; - - memset (&wrq, 0, sizeof (struct iwreq)); - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - if (ioctl (wext->fd, SIOCSIWSCAN, &wrq) < 0) { - if (errno == EOPNOTSUPP) - return FALSE; - } - return TRUE; -} - -static gboolean -wext_get_range_ifname (WifiDataWext *wext, - const char *ifname, - struct iw_range *range, - guint32 *response_len) -{ - int i = 26; - gboolean success = FALSE; - struct iwreq wrq; - - memset (&wrq, 0, sizeof (struct iwreq)); - nm_utils_ifname_cpy (wrq.ifr_name, ifname); - wrq.u.data.pointer = (caddr_t) range; - wrq.u.data.length = sizeof (struct iw_range); - - /* Need to give some drivers time to recover after suspend/resume - * (ex ipw3945 takes a few seconds to talk to its regulatory daemon; - * see rh bz#362421) - */ - while (i-- > 0) { - if (ioctl (wext->fd, SIOCGIWRANGE, &wrq) == 0) { - if (response_len) - *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; - } - - g_usleep (G_USEC_PER_SEC / 4); - } - - if (i <= 0) { - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver took too long to respond to IWRANGE query.", - ifname); - } - - return success; -} - -#define WPA_CAPS (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | \ - NM_WIFI_DEVICE_CAP_CIPHER_CCMP | \ - NM_WIFI_DEVICE_CAP_WPA | \ - NM_WIFI_DEVICE_CAP_RSN) - -static guint32 -wext_get_caps (WifiDataWext *wext, const char *ifname, struct iw_range *range) -{ - guint32 caps = NM_WIFI_DEVICE_CAP_NONE; - - g_return_val_if_fail (wext != NULL, NM_WIFI_DEVICE_CAP_NONE); - g_return_val_if_fail (range != NULL, NM_WIFI_DEVICE_CAP_NONE); - - /* All drivers should support WEP by default */ - caps |= NM_WIFI_DEVICE_CAP_CIPHER_WEP40 | NM_WIFI_DEVICE_CAP_CIPHER_WEP104; - - if (range->enc_capa & IW_ENC_CAPA_CIPHER_TKIP) - caps |= NM_WIFI_DEVICE_CAP_CIPHER_TKIP; - - if (range->enc_capa & IW_ENC_CAPA_CIPHER_CCMP) - caps |= NM_WIFI_DEVICE_CAP_CIPHER_CCMP; - - if (range->enc_capa & IW_ENC_CAPA_WPA) - caps |= NM_WIFI_DEVICE_CAP_WPA; - - if (range->enc_capa & IW_ENC_CAPA_WPA2) - caps |= NM_WIFI_DEVICE_CAP_RSN; - - /* Check for cipher support but not WPA support */ - if ( (caps & (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | NM_WIFI_DEVICE_CAP_CIPHER_CCMP)) - && !(caps & (NM_WIFI_DEVICE_CAP_WPA | NM_WIFI_DEVICE_CAP_RSN))) { - _LOGW (LOGD_WIFI, - "%s: device supports WPA ciphers but not WPA protocol; WPA unavailable.", - ifname); - caps &= ~WPA_CAPS; - } - - /* Check for WPA support but not cipher support */ - if ( (caps & (NM_WIFI_DEVICE_CAP_WPA | NM_WIFI_DEVICE_CAP_RSN)) - && !(caps & (NM_WIFI_DEVICE_CAP_CIPHER_TKIP | NM_WIFI_DEVICE_CAP_CIPHER_CCMP))) { - _LOGW (LOGD_WIFI, - "%s: device supports WPA protocol but not WPA ciphers; WPA unavailable.", - ifname); - caps &= ~WPA_CAPS; - } - - /* There's no way to detect Ad-Hoc/AP mode support with WEXT - * (other than actually trying to do it), so just assume that - * Ad-Hoc is supported and AP isn't. - */ - caps |= NM_WIFI_DEVICE_CAP_ADHOC; - - return caps; -} - -WifiData * -wifi_wext_init (int ifindex, gboolean check_scan) -{ - static const WifiDataClass klass = { - .struct_size = sizeof (WifiDataWext), - .get_mode = wifi_wext_get_mode, - .set_mode = wifi_wext_set_mode, - .set_powersave = wifi_wext_set_powersave, - .get_freq = wifi_wext_get_freq, - .find_freq = wifi_wext_find_freq, - .get_bssid = wifi_wext_get_bssid, - .get_rate = wifi_wext_get_rate, - .get_qual = wifi_wext_get_qual, - .deinit = wifi_wext_deinit, - .get_mesh_channel = wifi_wext_get_mesh_channel, - .set_mesh_channel = wifi_wext_set_mesh_channel, - .set_mesh_ssid = wifi_wext_set_mesh_ssid, - }; - WifiDataWext *wext; - struct iw_range range; - guint32 response_len = 0; - struct iw_range_with_scan_capa *scan_capa_range; - int i; - gboolean freq_valid = FALSE, has_5ghz = FALSE, has_2ghz = FALSE; - char ifname[IFNAMSIZ]; - - if (!nmp_utils_if_indextoname (ifindex, ifname)) { - _LOGW (LOGD_PLATFORM | LOGD_WIFI, - "can't determine interface name for ifindex %d", ifindex); - return NULL; - } - - wext = wifi_data_new (&klass, ifindex); - - wext->fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (wext->fd < 0) - goto error; - - memset (&range, 0, sizeof (struct iw_range)); - if (wext_get_range_ifname (wext, ifname, &range, &response_len) == FALSE) { - _LOGI (LOGD_PLATFORM | LOGD_WIFI, "(%s): driver WEXT range request failed", - ifname); - goto error; - } - - if ((response_len < 300) || (range.we_version_compiled < 21)) { - _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver WEXT version too old (got %d, expected >= 21)", - ifname, - range.we_version_compiled); - goto error; - } - - wext->max_qual.qual = range.max_qual.qual; - wext->max_qual.level = range.max_qual.level; - wext->max_qual.noise = range.max_qual.noise; - wext->max_qual.updated = range.max_qual.updated; - - wext->num_freqs = MIN (range.num_frequency, IW_MAX_FREQUENCIES); - for (i = 0; i < wext->num_freqs; i++) { - wext->freqs[i] = iw_freq_to_uint32 (&range.freq[i]); - freq_valid = TRUE; - if (wext->freqs[i] > 2400 && wext->freqs[i] < 2500) - has_2ghz = TRUE; - else if (wext->freqs[i] > 4900 && wext->freqs[i] < 6000) - has_5ghz = TRUE; - } - - /* Check for scanning capability; cards that can't scan are not supported */ - if (check_scan && (wext_can_scan_ifname (wext, ifname) == FALSE)) { - _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): drivers that cannot scan are unsupported", - ifname); - goto error; - } - - /* Check for the ability to scan specific SSIDs. Until the scan_capa - * field gets added to wireless-tools, need to work around that by casting - * to the custom structure. - */ - scan_capa_range = (struct iw_range_with_scan_capa *) ⦥ - if (scan_capa_range->scan_capa & NM_IW_SCAN_CAPA_ESSID) { - _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver supports SSID scans (scan_capa 0x%02X).", - ifname, - scan_capa_range->scan_capa); - } else { - _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): driver does not support SSID scans (scan_capa 0x%02X).", - ifname, - scan_capa_range->scan_capa); - } - - wext->parent.caps = wext_get_caps (wext, ifname, &range); - if (freq_valid) - wext->parent.caps |= NM_WIFI_DEVICE_CAP_FREQ_VALID; - if (has_2ghz) - wext->parent.caps |= NM_WIFI_DEVICE_CAP_FREQ_2GHZ; - if (has_5ghz) - wext->parent.caps |= NM_WIFI_DEVICE_CAP_FREQ_5GHZ; - - _LOGI (LOGD_PLATFORM | LOGD_WIFI, - "(%s): using WEXT for WiFi device control", - ifname); - - return (WifiData *) wext; - -error: - wifi_utils_unref ((WifiData *) wext); - return NULL; -} - -gboolean -wifi_wext_is_wifi (const char *iface) -{ - int fd; - struct iwreq iwr; - gboolean is_wifi = FALSE; - - /* 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 - * exists. - * - * Note that wifi_wext_is_wifi() has only one caller which just verified - * that an interface with this name exists. - */ - - fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (fd >= 0) { - nm_utils_ifname_cpy (iwr.ifr_ifrn.ifrn_name, iface); - if (ioctl (fd, SIOCGIWNAME, &iwr) == 0) - is_wifi = TRUE; - nm_close (fd); - } - return is_wifi; -} diff --git a/src/platform/wifi/wifi-utils-wext.h b/src/platform/wifi/wifi-utils-wext.h deleted file mode 100644 index 3ef5a173..00000000 --- a/src/platform/wifi/wifi-utils-wext.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2011 Red Hat, Inc. - */ - -#ifndef __WIFI_UTILS_WEXT_H__ -#define __WIFI_UTILS_WEXT_H__ - -#include "wifi-utils.h" - -WifiData *wifi_wext_init (int ifindex, gboolean check_scan); - -gboolean wifi_wext_is_wifi (const char *iface); - -#endif /* __WIFI_UTILS_WEXT_H__ */ diff --git a/src/platform/wifi/wifi-utils.c b/src/platform/wifi/wifi-utils.c deleted file mode 100644 index b3dd92ba..00000000 --- a/src/platform/wifi/wifi-utils.c +++ /dev/null @@ -1,231 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2005 - 2011 Red Hat, Inc. - * Copyright (C) 2006 - 2008 Novell, Inc. - */ - -#include "nm-default.h" - -#include "wifi-utils.h" - -#include -#include -#include -#include - -#include "wifi-utils-private.h" -#include "wifi-utils-nl80211.h" -#if HAVE_WEXT -#include "wifi-utils-wext.h" -#endif -#include "nm-core-utils.h" - -#include "platform/nm-platform-utils.h" - -gpointer -wifi_data_new (const WifiDataClass *klass, int ifindex) -{ - WifiData *data; - - nm_assert (klass); - nm_assert (klass->struct_size > sizeof (WifiData)); - - data = g_malloc0 (klass->struct_size); - data->klass = klass; - data->ifindex = ifindex; - return data; -} - -/*****************************************************************************/ - -WifiData * -wifi_utils_init (int ifindex, gboolean check_scan) -{ - WifiData *ret; - - g_return_val_if_fail (ifindex > 0, NULL); - - ret = wifi_nl80211_init (ifindex); - if (ret == NULL) { -#if HAVE_WEXT - ret = wifi_wext_init (ifindex, check_scan); -#endif - } - return ret; -} - -int -wifi_utils_get_ifindex (WifiData *data) -{ - g_return_val_if_fail (data != NULL, -1); - - return data->ifindex; -} - -NMDeviceWifiCapabilities -wifi_utils_get_caps (WifiData *data) -{ - g_return_val_if_fail (data != NULL, NM_WIFI_DEVICE_CAP_NONE); - - return data->caps; -} - -NM80211Mode -wifi_utils_get_mode (WifiData *data) -{ - g_return_val_if_fail (data != NULL, NM_802_11_MODE_UNKNOWN); - return data->klass->get_mode (data); -} - -gboolean -wifi_utils_set_mode (WifiData *data, const NM80211Mode mode) -{ - g_return_val_if_fail (data != NULL, FALSE); - g_return_val_if_fail ( (mode == NM_802_11_MODE_INFRA) - || (mode == NM_802_11_MODE_AP) - || (mode == NM_802_11_MODE_ADHOC), FALSE); - - /* nl80211 probably doesn't need this */ - return data->klass->set_mode ? data->klass->set_mode (data, mode) : TRUE; -} - -gboolean -wifi_utils_set_powersave (WifiData *data, guint32 powersave) -{ - g_return_val_if_fail (data != NULL, FALSE); - - return data->klass->set_powersave ? data->klass->set_powersave (data, powersave) : TRUE; -} - -gboolean -wifi_utils_set_wake_on_wlan (WifiData *data, NMSettingWirelessWakeOnWLan wowl) -{ - g_return_val_if_fail (data != NULL, FALSE); - - return data->klass->set_wake_on_wlan ? - data->klass->set_wake_on_wlan (data, wowl) : FALSE; -} - -guint32 -wifi_utils_get_freq (WifiData *data) -{ - g_return_val_if_fail (data != NULL, 0); - return data->klass->get_freq (data); -} - -guint32 -wifi_utils_find_freq (WifiData *data, const guint32 *freqs) -{ - g_return_val_if_fail (data != NULL, 0); - g_return_val_if_fail (freqs != NULL, 0); - return data->klass->find_freq (data, freqs); -} - -gboolean -wifi_utils_get_bssid (WifiData *data, guint8 *out_bssid) -{ - g_return_val_if_fail (data != NULL, FALSE); - g_return_val_if_fail (out_bssid != NULL, FALSE); - - memset (out_bssid, 0, ETH_ALEN); - return data->klass->get_bssid (data, out_bssid); -} - -guint32 -wifi_utils_get_rate (WifiData *data) -{ - g_return_val_if_fail (data != NULL, 0); - return data->klass->get_rate (data); -} - -int -wifi_utils_get_qual (WifiData *data) -{ - g_return_val_if_fail (data != NULL, 0); - return data->klass->get_qual (data); -} - -gboolean -wifi_utils_get_wowlan (WifiData *data) -{ - g_return_val_if_fail (data != NULL, 0); - - if (!data->klass->get_wowlan) - return FALSE; - return data->klass->get_wowlan (data); -} - -void -wifi_utils_unref (WifiData *data) -{ - g_return_if_fail (data != NULL); - - data->klass->deinit (data); - g_free (data); -} - -gboolean -wifi_utils_is_wifi (int dirfd, const char *ifname) -{ - g_return_val_if_fail (dirfd >= 0, FALSE); - - if (faccessat (dirfd, "phy80211", F_OK, 0) == 0) - return TRUE; -#if HAVE_WEXT - if (wifi_wext_is_wifi (ifname)) - return TRUE; -#endif - return FALSE; -} - -/* OLPC Mesh-only functions */ - -guint32 -wifi_utils_get_mesh_channel (WifiData *data) -{ - g_return_val_if_fail (data != NULL, FALSE); - g_return_val_if_fail (data->klass->get_mesh_channel != NULL, FALSE); - return data->klass->get_mesh_channel (data); -} - -gboolean -wifi_utils_set_mesh_channel (WifiData *data, guint32 channel) -{ - g_return_val_if_fail (data != NULL, FALSE); - g_return_val_if_fail (channel <= 13, FALSE); - g_return_val_if_fail (data->klass->set_mesh_channel != NULL, FALSE); - return data->klass->set_mesh_channel (data, channel); -} - -gboolean -wifi_utils_set_mesh_ssid (WifiData *data, const guint8 *ssid, gsize len) -{ - g_return_val_if_fail (data != NULL, FALSE); - g_return_val_if_fail (data->klass->set_mesh_ssid != NULL, FALSE); - return data->klass->set_mesh_ssid (data, ssid, len); -} - -gboolean -wifi_utils_indicate_addressing_running (WifiData *data, gboolean running) -{ - g_return_val_if_fail (data != NULL, FALSE); - if (data->klass->indicate_addressing_running) - return data->klass->indicate_addressing_running (data, running); - return FALSE; -} - diff --git a/src/platform/wifi/wifi-utils.h b/src/platform/wifi/wifi-utils.h deleted file mode 100644 index c69561de..00000000 --- a/src/platform/wifi/wifi-utils.h +++ /dev/null @@ -1,79 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2005 - 2011 Red Hat, Inc. - * Copyright (C) 2006 - 2008 Novell, Inc. - */ - -#ifndef __WIFI_UTILS_H__ -#define __WIFI_UTILS_H__ - -#include - -#include "nm-dbus-interface.h" -#include "nm-setting-wireless.h" - -typedef struct WifiData WifiData; - -gboolean wifi_utils_is_wifi (int dirfd, const char *ifname); - -WifiData *wifi_utils_init (int ifindex, gboolean check_scan); - -int wifi_utils_get_ifindex (WifiData *data); - -void wifi_utils_unref (WifiData *data); - -NMDeviceWifiCapabilities wifi_utils_get_caps (WifiData *data); - -NM80211Mode wifi_utils_get_mode (WifiData *data); - -gboolean wifi_utils_set_mode (WifiData *data, const NM80211Mode mode); - -/* Returns frequency in MHz */ -guint32 wifi_utils_get_freq (WifiData *data); - -/* Return the first supported frequency in the zero-terminated list. - * Frequencies are specified in MHz. */ -guint32 wifi_utils_find_freq (WifiData *data, const guint32 *freqs); - -/* out_bssid must be ETH_ALEN bytes */ -gboolean wifi_utils_get_bssid (WifiData *data, guint8 *out_bssid); - -/* Returns current bitrate in Kbps */ -guint32 wifi_utils_get_rate (WifiData *data); - -/* Returns quality 0 - 100% on succes, or -1 on error */ -int wifi_utils_get_qual (WifiData *data); - -/* Tells the driver DHCP or SLAAC is running */ -gboolean wifi_utils_indicate_addressing_running (WifiData *data, gboolean running); - -/* Returns true if WoWLAN is enabled on device */ -gboolean wifi_utils_get_wowlan (WifiData *data); - -gboolean wifi_utils_set_powersave (WifiData *data, guint32 powersave); - -gboolean wifi_utils_set_wake_on_wlan (WifiData *data, NMSettingWirelessWakeOnWLan wowl); - -/* OLPC Mesh-only functions */ -guint32 wifi_utils_get_mesh_channel (WifiData *data); - -gboolean wifi_utils_set_mesh_channel (WifiData *data, guint32 channel); - -gboolean wifi_utils_set_mesh_ssid (WifiData *data, const guint8 *ssid, gsize len); - -#endif /* __WIFI_UTILS_H__ */ -- cgit 1.3.0-6-gf8a5