nl80211: prefer non-supplicant-based devices
[project/iwinfo.git] / iwinfo_nl80211.c
index aeff7782266f4e44accff5362ab5a3324bcca5ca..f598ef5688a757cec5858b162420fcebbcc7ddbd 100644 (file)
@@ -22,6 +22,7 @@
  * Parts of this code are derived from the Linux iw utility.
  */
 
+#include <sys/stat.h>
 #include <limits.h>
 #include <glob.h>
 #include <fnmatch.h>
@@ -152,6 +153,23 @@ static int nl80211_readstr(const char *path, char *buffer, int length)
        return rv;
 }
 
+static int nl80211_get_band(int nla_type)
+{
+       switch (nla_type)
+       {
+       case NL80211_BAND_2GHZ:
+               return IWINFO_BAND_24;
+       case NL80211_BAND_5GHZ:
+               return IWINFO_BAND_5;
+       case NL80211_BAND_6GHZ:
+               return IWINFO_BAND_6;
+       case NL80211_BAND_60GHZ:
+               return IWINFO_BAND_60;
+       }
+
+       return 0;
+}
+
 
 static int nl80211_msg_error(struct sockaddr_nl *nla,
        struct nlmsgerr *err, void *arg)
@@ -233,63 +251,119 @@ static struct nl80211_msg_conveyor * nl80211_ctl(int cmd, int flags)
        return nl80211_new(nls->nlctrl, cmd, flags);
 }
 
-static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
+static const char *nl80211_phy_path_str(const char *phyname)
 {
-       size_t linklen, pathlen;
-       char buf[128], *link;
+       static char path[PATH_MAX];
+       const char *prefix = "/sys/devices/";
+       int prefix_len = strlen(prefix);
+       int buf_len, offset;
        struct dirent *e;
-       const char *path;
-       int idx = -1;
+       char buf[512], *link;
+       int phy_idx;
+       int seq = 0;
        DIR *d;
 
-       path = uci_lookup_option_string(uci_ctx, s, "path");
-       if (!path)
-               return -1;
+       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", phyname);
+       phy_idx = nl80211_readint(buf);
+       if (phy_idx < 0)
+               return NULL;
 
-       if ((d = opendir("/sys/class/ieee80211")) != NULL)
-       {
-               while ((e = readdir(d)) != NULL)
-               {
-                       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", e->d_name);
+       buf_len = snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", phyname);
+       link = realpath(buf, path);
+       if (!link)
+               return NULL;
 
-                       link = realpath(buf, NULL);
+       if (strncmp(link, prefix, prefix_len) != 0)
+               return NULL;
 
-                       if (link == NULL)
-                               continue;
+       link += prefix_len;
 
-                       linklen = strlen(link);
-                       pathlen = strlen(path);
+       prefix = "platform/";
+       prefix_len = strlen(prefix);
+       if (!strncmp(link, prefix, prefix_len) && strstr(link, "/pci"))
+               link += prefix_len;
 
-                       if (pathlen >= linklen || strcmp(link + (linklen - pathlen), path))
-                               linklen = 0;
+       snprintf(buf + buf_len, sizeof(buf) - buf_len, "/ieee80211");
+       d = opendir(buf);
+       if (!d)
+               return link;
 
-                       free(link);
+       while ((e = readdir(d)) != NULL) {
+               int cur_idx;
 
-                       if (linklen == 0)
-                               continue;
+               snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
+               cur_idx = nl80211_readint(buf);
+               if (cur_idx < 0)
+                       continue;
 
-                       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
+               if (cur_idx >= phy_idx)
+                       continue;
 
-                       idx = nl80211_readint(buf);
+               seq++;
+       }
 
-                       if (idx >= 0)
-                               break;
-               }
+       closedir(d);
 
-               closedir(d);
+       if (!seq)
+               return link;
+
+       offset = link - path + strlen(link);
+       snprintf(path + offset, sizeof(path) - offset, "+%d", seq);
+
+       return link;
+}
+
+static int nl80211_phy_idx_from_path(const char *path)
+{
+       char buf[512];
+       struct dirent *e;
+       const char *cur_path;
+       int cur_path_len;
+       int path_len;
+       int idx = -1;
+       DIR *d;
+
+       if (!path)
+               return -1;
+
+       path_len = strlen(path);
+       if (!path_len)
+               return -1;
+
+       d = opendir("/sys/class/ieee80211");
+       if (!d)
+               return -1;
+
+       while ((e = readdir(d)) != NULL) {
+               cur_path = nl80211_phy_path_str(e->d_name);
+               if (!cur_path)
+                       continue;
+
+               cur_path_len = strlen(cur_path);
+               if (cur_path_len < path_len)
+                       continue;
+
+               if (strcmp(cur_path + cur_path_len - path_len, path) != 0)
+                       continue;
+
+               snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
+               idx = nl80211_readint(buf);
+
+               if (idx >= 0)
+                       break;
        }
 
+       closedir(d);
+
        return idx;
 }
 
-static int nl80211_phy_idx_from_uci_macaddr(struct uci_section *s)
+static int nl80211_phy_idx_from_macaddr(const char *opt)
 {
-       const char *opt;
        char buf[128];
        int i, idx = -1;
        glob_t gl;
 
-       opt = uci_lookup_option_string(uci_ctx, s, "macaddr");
        if (!opt)
                return -1;
 
@@ -316,12 +390,10 @@ static int nl80211_phy_idx_from_uci_macaddr(struct uci_section *s)
        return idx;
 }
 
-static int nl80211_phy_idx_from_uci_phy(struct uci_section *s)
+static int nl80211_phy_idx_from_phy(const char *opt)
 {
-       const char *opt;
        char buf[128];
 
-       opt = uci_lookup_option_string(uci_ctx, s, "phy");
        if (!opt)
                return -1;
 
@@ -332,29 +404,45 @@ static int nl80211_phy_idx_from_uci_phy(struct uci_section *s)
 static int nl80211_phy_idx_from_uci(const char *name)
 {
        struct uci_section *s;
+       const char *opt;
        int idx = -1;
 
        s = iwinfo_uci_get_radio(name, "mac80211");
        if (!s)
-               goto free;
+               goto out;
 
-       idx = nl80211_phy_idx_from_uci_path(s);
+       opt = uci_lookup_option_string(uci_ctx, s, "path");
+       idx = nl80211_phy_idx_from_path(opt);
+       if (idx >= 0)
+               goto out;
 
-       if (idx < 0)
-               idx = nl80211_phy_idx_from_uci_macaddr(s);
+       opt = uci_lookup_option_string(uci_ctx, s, "macaddr");
+       idx = nl80211_phy_idx_from_macaddr(opt);
+       if (idx >= 0)
+               goto out;
 
-       if (idx < 0)
-               idx = nl80211_phy_idx_from_uci_phy(s);
+       opt = uci_lookup_option_string(uci_ctx, s, "phy");
+       idx = nl80211_phy_idx_from_phy(opt);
 
-free:
+out:
        iwinfo_uci_free();
        return idx;
 }
 
+static bool nl80211_is_ifname(const char *name)
+{
+       struct stat st;
+       char buffer[PATH_MAX];
+
+       snprintf(buffer, sizeof(buffer), "/sys/class/net/%s", name);
+       return !lstat(buffer, &st);
+}
+
 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
                                                  int cmd, int flags)
 {
-       int ifidx = -1, phyidx = -1;
+       unsigned int ifidx = 0;
+       int phyidx = -1;
        struct nl80211_msg_conveyor *cv;
 
        if (ifname == NULL)
@@ -363,15 +451,16 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
        if (nl80211_init() < 0)
                return NULL;
 
-       if (!strncmp(ifname, "phy", 3))
-               phyidx = atoi(&ifname[3]);
-       else if (!strncmp(ifname, "radio", 5))
-               phyidx = nl80211_phy_idx_from_uci(ifname);
-
        if (!strncmp(ifname, "mon.", 4))
                ifidx = if_nametoindex(&ifname[4]);
-       else
+       else if (nl80211_is_ifname(ifname))
                ifidx = if_nametoindex(ifname);
+       else
+       {
+               phyidx = nl80211_phy_idx_from_phy(ifname);
+               if (phyidx < 0)
+                       phyidx = nl80211_phy_idx_from_uci(ifname);
+       }
 
        /* Valid ifidx must be greater than 0 */
        if ((ifidx <= 0) && (phyidx < 0))
@@ -583,7 +672,13 @@ static int __nl80211_wait(const char *family, const char *group, ...)
 #define nl80211_wait(family, group, ...) \
        __nl80211_wait(family, group, __VA_ARGS__, 0)
 
-
+/* This is linux's ieee80211_freq_khz_to_channel() which is:
+ * SPDX-License-Identifier: GPL-2.0
+ * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
+ * Copyright 2013-2014 Intel Mobile Communications GmbH
+ * Copyright 2017 Intel Deutschland GmbH
+ * Copyright (C) 2018-2022 Intel Corporation
+ */
 static int nl80211_freq2channel(int freq)
 {
        if (freq == 2484)
@@ -592,14 +687,31 @@ static int nl80211_freq2channel(int freq)
                return (freq - 2407) / 5;
        else if (freq >= 4910 && freq <= 4980)
                return (freq - 4000) / 5;
-       else if(freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6)
+       else if (freq < 5925)
+               return (freq - 5000) / 5;
+       else if (freq == 5935)
+               return 2;
+       else if (freq <= 45000) /* DMG band lower limit */
+               /* see 802.11ax D6.1 27.3.22.2 */
+               return (freq - 5950) / 5;
+       else if (freq >= 58320 && freq <= 70200)
                return (freq - 56160) / 2160;
        else
-               return (freq - 5000) / 5;
+               return 0;
 }
 
-static int nl80211_channel2freq(int channel, const char *band)
+/* This is linux's ieee80211_channel_to_freq_khz() which is:
+ * SPDX-License-Identifier: GPL-2.0
+ * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
+ * Copyright 2013-2014 Intel Mobile Communications GmbH
+ * Copyright 2017 Intel Deutschland GmbH
+ * Copyright (C) 2018-2022 Intel Corporation
+ */
+static int nl80211_channel2freq(int channel, const char *band, bool ax)
 {
+       if (channel < 1)
+               return 0;
+
        if (!band || band[0] != 'a')
        {
                if (channel == 14)
@@ -607,9 +719,17 @@ static int nl80211_channel2freq(int channel, const char *band)
                else if (channel < 14)
                        return (channel * 5) + 2407;
        }
-       else if ( strcmp(band, "ad") == 0)
+       else if (strcmp(band, "ad")  == 0)
+       {
+               if (channel < 7)
+                       return 56160 + 2160 * channel;
+       }
+       else if (ax)
        {
-               return 56160 + 2160 * channel;
+               if (channel == 2)
+                       return 5935;
+               if (channel < 233)
+                       return (channel * 5) + 5950;
        }
        else
        {
@@ -622,7 +742,21 @@ static int nl80211_channel2freq(int channel, const char *band)
        return 0;
 }
 
-static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
+static uint8_t nl80211_freq2band(int freq)
+{
+       if (freq >= 2412 && freq <= 2484)
+               return IWINFO_BAND_24;
+       else if (freq >= 5160 && freq <= 5885)
+               return IWINFO_BAND_5;
+       else if (freq >= 5925 && freq <= 7125)
+               return IWINFO_BAND_6;
+       else if (freq >= 58320 && freq <= 69120)
+               return IWINFO_BAND_60;
+
+       return 0;
+}
+
+static int nl80211_phyname_cb(struct nl_msg *msg, void *arg)
 {
        char *buf = arg;
        struct nlattr **attr = nl80211_parse(msg);
@@ -643,57 +777,82 @@ static char * nl80211_ifname2phy(const char *ifname)
        memset(phy, 0, sizeof(phy));
 
        nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
-                       nl80211_ifname2phy_cb, phy);
+                       nl80211_phyname_cb, phy);
 
        return phy[0] ? phy : NULL;
 }
 
+static char * nl80211_phyidx2name(unsigned int idx)
+{
+       struct nl80211_msg_conveyor *cv;
+       static char phy[32] = { 0 };
+
+       if (nl80211_init() < 0)
+               return NULL;
+
+       cv = nl80211_new(nls->nl80211, NL80211_CMD_GET_WIPHY, 0);
+       if (!cv)
+               return NULL;
+
+       NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, idx);
+
+       memset(phy, 0, sizeof(phy));
+       nl80211_send(cv, nl80211_phyname_cb, phy);
+
+       return phy[0] ? phy : NULL;
+
+nla_put_failure:
+       return NULL;
+}
+
 static char * nl80211_phy2ifname(const char *ifname)
 {
-       int ifidx = -1, cifidx = -1, phyidx = -1;
-       char buffer[64];
+       int ifidx = -1, cifidx, lmode = 1, clmode, phyidx;
+       char buffer[512];
        static char nif[IFNAMSIZ] = { 0 };
-
        DIR *d;
        struct dirent *e;
 
        /* Only accept phy name of the form phy%d or radio%d */
        if (!ifname)
                return NULL;
-       else if (!strncmp(ifname, "phy", 3))
-               phyidx = atoi(&ifname[3]);
-       else if (!strncmp(ifname, "radio", 5))
-               phyidx = nl80211_phy_idx_from_uci(ifname);
-       else
+
+       phyidx = nl80211_phy_idx_from_phy(ifname);
+       if (phyidx < 0)
+               phyidx = nl80211_phy_idx_from_uci(ifname);;
+       if (phyidx < 0)
                return NULL;
 
        memset(nif, 0, sizeof(nif));
 
-       if (phyidx > -1)
+       snprintf(buffer, sizeof(buffer),
+                "/sys/class/ieee80211/phy%d/device/net", phyidx);
+
+       if ((d = opendir(buffer)) != NULL)
        {
-               if ((d = opendir("/sys/class/net")) != NULL)
+               while ((e = readdir(d)) != NULL)
                {
-                       while ((e = readdir(d)) != NULL)
-                       {
-                               snprintf(buffer, sizeof(buffer),
-                                        "/sys/class/net/%s/phy80211/index", e->d_name);
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/ifindex", e->d_name);
+                       cifidx = nl80211_readint(buffer);
 
-                               if (nl80211_readint(buffer) == phyidx)
-                               {
-                                       snprintf(buffer, sizeof(buffer),
-                                                "/sys/class/net/%s/ifindex", e->d_name);
+                       if (cifidx < 0)
+                               continue;
 
-                                       if ((cifidx = nl80211_readint(buffer)) >= 0 &&
-                                           ((ifidx < 0) || (cifidx < ifidx)))
-                                       {
-                                               ifidx = cifidx;
-                                               strncpy(nif, e->d_name, sizeof(nif) - 1);
-                                       }
-                               }
-                       }
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/link_mode", e->d_name);
+                       clmode = nl80211_readint(buffer);
 
-                       closedir(d);
+                       /* prefer non-supplicant-based devices */
+                       if ((ifidx < 0) || (cifidx < ifidx) || ((lmode == 1) && (clmode != 1)))
+                       {
+                               ifidx = cifidx;
+                               lmode = clmode;
+                               strncpy(nif, e->d_name, sizeof(nif) - 1);
+                       }
                }
+
+               closedir(d);
        }
 
        return nif[0] ? nif : NULL;
@@ -850,8 +1009,19 @@ static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
 
        remote.sun_family = AF_UNIX;
        remote_length = sizeof(remote.sun_family) +
-               sprintf(remote.sun_path, "/var/run/wpa_supplicant-%s/%s",
-                       ifname, ifname);
+               sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname);
+
+       /* Set client socket file permissions so that bind() creates the client
+       * socket with these permissions and there is no need to try to change
+       * them with chmod() after bind() which would have potential issues with
+       * race conditions. These permissions are needed to make sure the server
+       * side (wpa_supplicant or hostapd) can reply to the control interface
+       * messages.
+       *
+       * The lchown() calls below after bind() are also part of the needed
+       * operations to allow the response to go through. Those are using the
+       * no-deference-symlinks version to avoid races. */
+       fchmod(sock, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
 
        if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
        {
@@ -861,14 +1031,8 @@ static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
 
        if (connect(sock, (struct sockaddr *)&remote, remote_length))
        {
-               remote_length = sizeof(remote.sun_family) +
-                       sprintf(remote.sun_path, "/var/run/wpa_supplicant/%s", ifname);
-
-               if (connect(sock, (struct sockaddr *)&remote, remote_length))
-               {
-                       close(sock);
-                       return -1;
-               }
+               close(sock);
+               return -1;
        }
 
        local->sun_family = AF_UNIX;
@@ -881,6 +1045,10 @@ static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
                return -1;
        }
 
+       /* Set group even if we do not have privileges to change owner */
+       lchown(local->sun_path, -1, 101);
+       lchown(local->sun_path, 101, 101);
+
        return sock;
 }
 
@@ -1010,7 +1178,6 @@ static char * nl80211_ifadd(const char *ifname)
 static void nl80211_ifdel(const char *ifname)
 {
        struct nl80211_msg_conveyor *req;
-       int err;
 
        req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
        if (req)
@@ -1150,7 +1317,7 @@ static int nl80211_get_ssid(const char *ifname, char *buf)
        /* failed, try to obtain Mesh ID */
        if (sb.ssid[0] == 0)
                iwinfo_ubus_query(res ? res : ifname, "mesh_id",
-                                 sb.ssid, IWINFO_ESSID_MAX_SIZE + 1);
+                                 buf, IWINFO_ESSID_MAX_SIZE + 1);
 
        return (sb.ssid[0] == 0) ? -1 : 0;
 }
@@ -1233,7 +1400,7 @@ static int nl80211_get_frequency_info_cb(struct nl_msg *msg, void *arg)
 
 static int nl80211_get_frequency(const char *ifname, int *buf)
 {
-       char *res, channel[4], hwmode[3];
+       char *res, channel[4] = { 0 }, hwmode[3] = { 0 }, ax[2] = { 0 };
 
        /* try to find frequency from interface info */
        res = nl80211_phy2ifname(ifname);
@@ -1245,9 +1412,10 @@ static int nl80211_get_frequency(const char *ifname, int *buf)
        /* failed, try to find frequency from hostapd info */
        if ((*buf == 0) &&
            nl80211_hostapd_query(ifname, "hw_mode", hwmode, sizeof(hwmode),
-                                         "channel", channel, sizeof(channel)) == 2)
+                                         "channel", channel, sizeof(channel),
+                                         "ieee80211ax", ax, sizeof(ax)) >= 2)
        {
-               *buf = nl80211_channel2freq(atoi(channel), hwmode);
+               *buf = nl80211_channel2freq(atoi(channel), hwmode, ax[0] == '1');
        }
 
        /* failed, try to find frequency from scan results */
@@ -1598,6 +1766,7 @@ static struct {
        { "IEEE 802.1X/EAP", 0, IWINFO_KMGMT_8021x },
        { "EAP-SUITE-B-192", 4, IWINFO_KMGMT_8021x },
        { "EAP-SUITE-B",     4, IWINFO_KMGMT_8021x },
+       { "EAP-SHA384",      4, IWINFO_KMGMT_8021x },
        { "EAP-SHA256",      0, IWINFO_KMGMT_8021x },
        { "PSK-SHA256",      0, IWINFO_KMGMT_PSK },
        { "NONE",            0, IWINFO_KMGMT_NONE },
@@ -1650,12 +1819,14 @@ static struct {
        const char *match;
        int cipher;
 } wpa_cipher_strings[] = {
-       { "WEP-104", IWINFO_CIPHER_WEP104 },
-       { "WEP-40",  IWINFO_CIPHER_WEP40 },
-       { "NONE",    IWINFO_CIPHER_NONE },
-       { "TKIP",    IWINFO_CIPHER_TKIP },
-       { "CCMP",    IWINFO_CIPHER_CCMP },
-       { "GCMP",    IWINFO_CIPHER_GCMP }
+       { "WEP-104", IWINFO_CIPHER_WEP104  },
+       { "WEP-40",  IWINFO_CIPHER_WEP40   },
+       { "NONE",    IWINFO_CIPHER_NONE    },
+       { "TKIP",    IWINFO_CIPHER_TKIP    },
+       { "CCMP-256",IWINFO_CIPHER_CCMP256 },
+       { "CCMP",    IWINFO_CIPHER_CCMP    },
+       { "GCMP-256",IWINFO_CIPHER_GCMP256 },
+       { "GCMP",    IWINFO_CIPHER_GCMP    }
 };
 
 static void parse_wpa_ciphers(const char *str, uint16_t *ciphers)
@@ -1781,6 +1952,9 @@ static int nl80211_get_encryption(const char *ifname, char *buf)
                                if (!strncmp(p, "WPA-", 4))
                                        p += 4;
 
+                               if (!strncmp(p, "FT-", 3))
+                                       p += 3;
+
                                parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites);
                        }
 
@@ -1869,7 +2043,19 @@ static void nl80211_parse_rateinfo(struct nlattr **ri,
        else if (ri[NL80211_RATE_INFO_BITRATE])
                re->rate = nla_get_u16(ri[NL80211_RATE_INFO_BITRATE]) * 100;
 
-       if (ri[NL80211_RATE_INFO_VHT_MCS])
+       if (ri[NL80211_RATE_INFO_HE_MCS])
+       {
+               re->is_he = 1;
+               re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_HE_MCS]);
+
+               if (ri[NL80211_RATE_INFO_HE_NSS])
+                       re->nss = nla_get_u8(ri[NL80211_RATE_INFO_HE_NSS]);
+               if (ri[NL80211_RATE_INFO_HE_GI])
+                       re->he_gi = nla_get_u8(ri[NL80211_RATE_INFO_HE_GI]);
+               if (ri[NL80211_RATE_INFO_HE_DCM])
+                       re->he_dcm = nla_get_u8(ri[NL80211_RATE_INFO_HE_DCM]);
+       }
+       else if (ri[NL80211_RATE_INFO_VHT_MCS])
        {
                re->is_vht = 1;
                re->mcs = nla_get_u8(ri[NL80211_RATE_INFO_VHT_MCS]);
@@ -2268,7 +2454,6 @@ static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
 {
        int err, ch_cur;
        int dbm_max = -1, dbm_cur, dbm_cnt;
-       struct nl80211_msg_conveyor *req;
        struct iwinfo_txpwrlist_entry entry;
 
        if (nl80211_get_channel(ifname, &ch_cur))
@@ -2450,8 +2635,11 @@ static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
                sl->e->crypto.enabled = 1;
 
        if (bss[NL80211_BSS_FREQUENCY])
-               sl->e->channel = nl80211_freq2channel(nla_get_u32(
-                       bss[NL80211_BSS_FREQUENCY]));
+       {
+               sl->e->mhz = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
+               sl->e->band = nl80211_freq2band(sl->e->mhz);
+               sl->e->channel = nl80211_freq2channel(sl->e->mhz);
+       }
 
        if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
                nl80211_get_scanlist_ie(bss, sl->e);
@@ -2673,7 +2861,9 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
                                e->mode = IWINFO_OPMODE_MASTER;
 
                        /* Channel */
-                       e->channel = nl80211_freq2channel(atoi(freq));
+                       e->mhz = atoi(freq);
+                       e->band = nl80211_freq2band(e->mhz);
+                       e->channel = nl80211_freq2channel(e->mhz);
 
                        /* Signal */
                        rssi = atoi(signal);
@@ -2798,6 +2988,8 @@ static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
                                iwinfo_ifup(ifname);
                                nl80211_hostapd_hup(ifname);
                        }
+                       else
+                               rv = -1;
 
                        nl80211_ifdel(res);
                        return rv;
@@ -2838,14 +3030,10 @@ static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
                                            freqs[NL80211_FREQUENCY_ATTR_DISABLED])
                                                continue;
 
+                                       e->band = nl80211_get_band(band->nla_type);
                                        e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
                                        e->channel = nl80211_freq2channel(e->mhz);
 
-                                       e->restricted = (
-                                               freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
-                                               !freqs[NL80211_FREQUENCY_ATTR_RADAR]
-                                       ) ? 1 : 0;
-
                                        if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
                                                e->flags |= IWINFO_FREQ_NO_HT40MINUS;
                                        if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
@@ -2858,6 +3046,16 @@ static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
                                                e->flags |= IWINFO_FREQ_NO_20MHZ;
                                        if (freqs[NL80211_FREQUENCY_ATTR_NO_10MHZ])
                                                e->flags |= IWINFO_FREQ_NO_10MHZ;
+                                       if (freqs[NL80211_FREQUENCY_ATTR_NO_HE])
+                                               e->flags |= IWINFO_FREQ_NO_HE;
+                                       if (freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
+                                           !freqs[NL80211_FREQUENCY_ATTR_RADAR])
+                                               e->flags |= IWINFO_FREQ_NO_IR;
+                                       if (freqs[NL80211_FREQUENCY_ATTR_INDOOR_ONLY])
+                                               e->flags |= IWINFO_FREQ_INDOOR_ONLY;
+
+                                       /* keep backwards compatibility */
+                                       e->restricted = (e->flags & IWINFO_FREQ_NO_IR) ? 1 : 0;
 
                                        e++;
                                        arr->count++;
@@ -2942,13 +3140,14 @@ struct nl80211_modes
        uint32_t hw;
        uint32_t ht;
 
-       uint32_t nl_freq;
+       uint8_t bands;
+
        uint16_t nl_ht;
        uint32_t nl_vht;
        uint16_t he_phy_cap[6];
 };
 
-static int nl80211_eval_modelist(struct nl80211_modes *m)
+static void nl80211_eval_modelist(struct nl80211_modes *m)
 {
        /* Treat any nonzero capability as 11n */
        if (m->nl_ht > 0)
@@ -2974,12 +3173,13 @@ static int nl80211_eval_modelist(struct nl80211_modes *m)
                        m->ht |= IWINFO_HTMODE_HE160 | IWINFO_HTMODE_HE80_80;
        }
 
-       if (m->nl_freq < 2485)
+       if (m->bands & IWINFO_BAND_24)
        {
                m->hw |= IWINFO_80211_B;
                m->hw |= IWINFO_80211_G;
        }
-       else if (m->nl_vht)
+
+       if (m->bands & IWINFO_BAND_5)
        {
                /* Treat any nonzero capability as 11ac */
                if (m->nl_vht > 0)
@@ -2997,32 +3197,33 @@ static int nl80211_eval_modelist(struct nl80211_modes *m)
                                m->ht |= IWINFO_HTMODE_VHT160;
                        }
                }
+               else
+               {
+                       m->hw |= IWINFO_80211_A;
+               }
        }
-       else if (m->nl_freq >= 56160)
+
+       if (m->bands & IWINFO_BAND_60)
        {
                m->hw |= IWINFO_80211_AD;
        }
-       else if (!(m->hw & IWINFO_80211_AC))
-       {
-               m->hw |= IWINFO_80211_A;
-       }
+
 }
 
 static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
 {
        struct nl80211_modes *m = arg;
-       int bands_remain, freqs_remain;
-       uint16_t caps = 0;
-       uint32_t vht_caps = 0;
+       int bands_remain;
        struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
-       struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
-       struct nlattr *band, *freq;
+       struct nlattr *band;
 
        if (attr[NL80211_ATTR_WIPHY_BANDS])
        {
                nla_for_each_nested(band, attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
                {
+                       m->bands |= nl80211_get_band(band->nla_type);
+
                        nla_parse(bands, NL80211_BAND_ATTR_MAX,
                                  nla_data(band), nla_len(band), NULL);
 
@@ -3052,20 +3253,6 @@ static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
                                        }
                                }
                        }
-
-                       if (bands[NL80211_BAND_ATTR_FREQS]) {
-                               nla_for_each_nested(freq, bands[NL80211_BAND_ATTR_FREQS],
-                                                   freqs_remain)
-                               {
-                                       nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
-                                               nla_data(freq), nla_len(freq), NULL);
-
-                                       if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
-                                               continue;
-
-                                       m->nl_freq = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
-                               }
-                       }
                }
 
                m->ok = 1;
@@ -3124,9 +3311,10 @@ static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg)
 
 static int nl80211_get_htmode(const char *ifname, int *buf)
 {
-       struct chan_info chn = { .width = 0, .mode = 0 };
-       char *res;
+       struct chan_info chn = { 0 };
+       char *res, b[2] = { 0 };
        int err;
+       bool he = false;
 
        res = nl80211_phy2ifname(ifname);
        *buf = 0;
@@ -3137,27 +3325,45 @@ static int nl80211_get_htmode(const char *ifname, int *buf)
        if (err)
                return -1;
 
+       if (nl80211_hostapd_query(res ? res : ifname, "ieee80211ax", b, sizeof(b)))
+               he = b[0] == '1';
+       else if (nl80211_wpactl_query(res ? res : ifname, "wifi_generation", b, sizeof(b)))
+               he = b[0] == '6';
+
        switch (chn.width) {
        case NL80211_CHAN_WIDTH_20:
-               if (chn.mode == -1)
+               if (he)
+                       *buf = IWINFO_HTMODE_HE20;
+               else if (chn.mode == -1)
                        *buf = IWINFO_HTMODE_VHT20;
                else
                        *buf = IWINFO_HTMODE_HT20;
                break;
        case NL80211_CHAN_WIDTH_40:
-               if (chn.mode == -1)
+               if (he)
+                       *buf = IWINFO_HTMODE_HE40;
+               else if (chn.mode == -1)
                        *buf = IWINFO_HTMODE_VHT40;
                else
                        *buf = IWINFO_HTMODE_HT40;
                break;
        case NL80211_CHAN_WIDTH_80:
-               *buf = IWINFO_HTMODE_VHT80;
+               if (he)
+                       *buf = IWINFO_HTMODE_HE80;
+               else
+                       *buf = IWINFO_HTMODE_VHT80;
                break;
        case NL80211_CHAN_WIDTH_80P80:
-               *buf = IWINFO_HTMODE_VHT80_80;
+               if (he)
+                       *buf = IWINFO_HTMODE_HE80_80;
+               else
+                       *buf = IWINFO_HTMODE_VHT80_80;
                break;
        case NL80211_CHAN_WIDTH_160:
-               *buf = IWINFO_HTMODE_VHT160;
+               if (he)
+                       *buf = IWINFO_HTMODE_HE160;
+               else
+                       *buf = IWINFO_HTMODE_VHT160;
                break;
        case NL80211_CHAN_WIDTH_5:
        case NL80211_CHAN_WIDTH_10:
@@ -3264,8 +3470,7 @@ static int nl80211_get_mbssid_support(const char *ifname, int *buf)
 
 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname)
 {
-       char *phy, compat[64], path[PATH_MAX];
-       int i;
+       char *phy, path[PATH_MAX];
 
        /* Try to determine the phy name from the given interface */
        phy = nl80211_ifname2phy(ifname);
@@ -3273,51 +3478,10 @@ static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const cha
        snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible",
                 phy ? "ieee80211" : "net", phy ? phy : ifname);
 
-       if (nl80211_readstr(path, compat, sizeof(compat)) <= 0)
+       if (nl80211_readstr(path, id->compatible, sizeof(id->compatible)) <= 0)
                return -1;
 
-       if (!strcmp(compat, "qca,ar9130-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0029;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9130;
-       } else if (!strcmp(compat, "qca,ar9330-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0030;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9330;
-       } else if (!strcmp(compat, "qca,ar9340-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0030;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9340;
-       } else if (!strcmp(compat, "qca,qca9530-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0033;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9530;
-       } else if (!strcmp(compat, "qca,qca9550-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0033;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9550;
-       } else if (!strcmp(compat, "qca,qca9560-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0033;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9560;
-       } else if (!strcmp(compat, "qcom,ipq4019-wifi")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x003c;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x4019;
-       } else if (!strcmp(compat, "mediatek,mt7622-wmac")) {
-               id->vendor_id = 0x14c3;
-               id->device_id = 0x7622;
-               id->subsystem_vendor_id = 0x14c3;
-               id->subsystem_device_id = 0x7622;
-       }
-       return (id->vendor_id && id->device_id) ? 0 : -1;
+       return 0;
 }
 
 
@@ -3331,7 +3495,9 @@ static int nl80211_get_hardware_id(const char *ifname, char *buf)
                { "vendor", &id->vendor_id },
                { "device", &id->device_id },
                { "subsystem_vendor", &id->subsystem_vendor_id },
-               { "subsystem_device", &id->subsystem_device_id }
+               { "subsystem_device", &id->subsystem_device_id },
+               { "../idVendor", &id->subsystem_vendor_id },
+               { "../idProduct", &id->subsystem_device_id }
        };
 
        memset(id, 0, sizeof(*id));
@@ -3349,14 +3515,13 @@ static int nl80211_get_hardware_id(const char *ifname, char *buf)
                        *lookup[i].dest = strtoul(num, NULL, 16);
        }
 
-       /* Failed to obtain hardware IDs, try FDT */
-       if (id->vendor_id == 0 || id->device_id == 0)
-               if (!nl80211_hardware_id_from_fdt(id, ifname))
-                       return 0;
-
-       /* Failed to obtain hardware IDs, search board config */
-       if (id->vendor_id == 0 || id->device_id == 0)
-               return iwinfo_hardware_id_from_mtd(id);
+       /* Failed to obtain hardware PCI/USB IDs... */
+       if (id->vendor_id == 0 && id->device_id == 0 &&
+           id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0)
+               /* ... first fallback to FDT ... */
+               if (nl80211_hardware_id_from_fdt(id, ifname) == -1)
+                       /* ... then board config */
+                       return iwinfo_hardware_id_from_mtd(id);
 
        return 0;
 }
@@ -3408,12 +3573,36 @@ static int nl80211_get_frequency_offset(const char *ifname, int *buf)
 
 static int nl80211_lookup_phyname(const char *section, char *buf)
 {
+       const char *name;
        int idx;
 
-       if ((idx = nl80211_phy_idx_from_uci(section)) < 0)
+       if (!strncmp(section, "path=", 5))
+               idx = nl80211_phy_idx_from_path(section + 5);
+       else if (!strncmp(section, "macaddr=", 8))
+               idx = nl80211_phy_idx_from_macaddr(section + 8);
+       else
+               idx = nl80211_phy_idx_from_uci(section);
+
+       if (idx < 0)
+               return -1;
+
+       name = nl80211_phyidx2name(idx);
+       if (!name)
+               return -1;
+
+       strcpy(buf, name);
+       return 0;
+}
+
+static int nl80211_phy_path(const char *phyname, const char **path)
+{
+       if (strchr(phyname, '/'))
+               return -1;
+
+       *path = nl80211_phy_path_str(phyname);
+       if (!*path)
                return -1;
 
-       sprintf(buf, "phy%d", idx);
        return 0;
 }
 
@@ -3451,5 +3640,6 @@ const struct iwinfo_ops nl80211_ops = {
        .countrylist      = nl80211_get_countrylist,
        .survey           = nl80211_get_survey,
        .lookup_phy       = nl80211_lookup_phyname,
+       .phy_path         = nl80211_phy_path,
        .close            = nl80211_close
 };