devices: add device id for Realtek RTL8188CU and RTL8188FTV
[project/iwinfo.git] / iwinfo_nl80211.c
index 27765317d071dc2e9173a4fa4e2f2da4782fdaca..220024955acce647410a8992f7fa19ae183c6b86 100644 (file)
  * Parts of this code are derived from the Linux iw utility.
  */
 
+#include <sys/stat.h>
 #include <limits.h>
 #include <glob.h>
 #include <fnmatch.h>
 #include <stdarg.h>
+#include <stdlib.h>
 
 #include "iwinfo_nl80211.h"
 
@@ -151,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)
@@ -232,44 +251,123 @@ 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)
 {
-       const char *opt;
-       char buf[128];
+       static char path[PATH_MAX];
+       const char *prefix = "/sys/devices/";
+       int prefix_len = strlen(prefix);
+       int buf_len, offset;
+       struct dirent *e;
+       char buf[512], *link;
+       int phy_idx;
+       int seq = 0;
+       DIR *d;
+
+       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", phyname);
+       phy_idx = nl80211_readint(buf);
+       if (phy_idx < 0)
+               return NULL;
+
+       buf_len = snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", phyname);
+       link = realpath(buf, path);
+       if (!link)
+               return NULL;
+
+       if (strncmp(link, prefix, prefix_len) != 0)
+               return NULL;
+
+       link += prefix_len;
+
+       prefix = "platform/";
+       prefix_len = strlen(prefix);
+       if (!strncmp(link, prefix, prefix_len) && strstr(link, "/pci"))
+               link += prefix_len;
+
+       snprintf(buf + buf_len, sizeof(buf) - buf_len, "/ieee80211");
+       d = opendir(buf);
+       if (!d)
+               return link;
+
+       while ((e = readdir(d)) != NULL) {
+               int cur_idx;
+
+               snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
+               cur_idx = nl80211_readint(buf);
+               if (cur_idx < 0)
+                       continue;
+
+               if (cur_idx >= phy_idx)
+                       continue;
+
+               seq++;
+       }
+
+       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;
-       glob_t gl;
+       DIR *d;
 
-       opt = uci_lookup_option_string(uci_ctx, s, "path");
-       if (!opt)
+       if (!path)
                return -1;
 
-       snprintf(buf, sizeof(buf), "/sys/devices/%s/ieee80211/*/index", opt);  /**/
-       if (glob(buf, 0, NULL, &gl))
-               snprintf(buf, sizeof(buf), "/sys/devices/platform/%s/ieee80211/*/index", opt);  /**/
+       path_len = strlen(path);
+       if (!path_len)
+               return -1;
 
-       if (glob(buf, 0, NULL, &gl))
+       d = opendir("/sys/class/ieee80211");
+       if (!d)
                return -1;
 
-       if (gl.gl_pathc > 0)
-               idx = nl80211_readint(gl.gl_pathv[0]);
+       while ((e = readdir(d)) != NULL) {
+               cur_path = nl80211_phy_path_str(e->d_name);
+               if (!cur_path)
+                       continue;
 
-       globfree(&gl);
+               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;
 
-       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*");   /**/
+       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*");
        if (glob(buf, 0, NULL, &gl))
                return -1;
 
@@ -292,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;
 
@@ -308,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)
@@ -339,14 +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);
-       else if (!strncmp(ifname, "mon.", 4))
+       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))
@@ -356,10 +470,9 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
        if (!cv)
                return NULL;
 
-       if (ifidx > -1)
+       if (ifidx > 0)
                NLA_PUT_U32(cv->msg, NL80211_ATTR_IFINDEX, ifidx);
-
-       if (phyidx > -1)
+       else if (phyidx > -1)
                NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, phyidx);
 
        return cv;
@@ -510,7 +623,7 @@ static int nl80211_wait_cb(struct nl_msg *msg, void *arg)
        struct nl80211_event_conveyor *cv = arg;
        struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
 
-       if (gnlh->cmd == cv->wait)
+       if (cv->wait[gnlh->cmd / 32] & (1 << (gnlh->cmd % 32)))
                cv->recv = gnlh->cmd;
 
        return NL_SKIP;
@@ -521,11 +634,13 @@ static int nl80211_wait_seq_check(struct nl_msg *msg, void *arg)
        return NL_OK;
 }
 
-static int nl80211_wait(const char *family, const char *group, int cmd)
+static int __nl80211_wait(const char *family, const char *group, ...)
 {
-       struct nl80211_event_conveyor cv = { .wait = cmd };
+       struct nl80211_event_conveyor cv = { };
        struct nl_cb *cb;
        int err = 0;
+       int cmd;
+       va_list ap;
 
        if (nl80211_subscribe(family, group))
                return -ENOENT;
@@ -539,6 +654,13 @@ static int nl80211_wait(const char *family, const char *group, int cmd)
        nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_wait_seq_check, NULL);
        nl_cb_set(cb, NL_CB_VALID,     NL_CB_CUSTOM, nl80211_wait_cb,        &cv );
 
+       va_start(ap, group);
+
+       for (cmd = va_arg(ap, int); cmd != 0; cmd = va_arg(ap, int))
+               cv.wait[cmd / 32] |= (1 << (cmd % 32));
+
+       va_end(ap);
+
        while (!cv.recv && !err)
                nl_recvmsgs(nls->nl_sock, cb);
 
@@ -547,7 +669,16 @@ static int nl80211_wait(const char *family, const char *group, int cmd)
        return err;
 }
 
+#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)
@@ -556,12 +687,31 @@ static int nl80211_freq2channel(int freq)
                return (freq - 2407) / 5;
        else if (freq >= 4910 && freq <= 4980)
                return (freq - 4000) / 5;
-       else
+       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 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)
@@ -569,6 +719,18 @@ static int nl80211_channel2freq(int channel, const char *band)
                else if (channel < 14)
                        return (channel * 5) + 2407;
        }
+       else if (strcmp(band, "ad")  == 0)
+       {
+               if (channel < 7)
+                       return 56160 + 2160 * channel;
+       }
+       else if (ax)
+       {
+               if (channel == 2)
+                       return 5935;
+               if (channel < 233)
+                       return (channel * 5) + 5950;
+       }
        else
        {
                if (channel >= 182 && channel <= 196)
@@ -580,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);
@@ -601,57 +777,84 @@ 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 */
+       /* Only accept phy name in the form of 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)
+       if ((d = opendir("/sys/class/net")) != 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/phy80211/index", e->d_name);
+                       if (nl80211_readint(buffer) != phyidx)
+                               continue;
 
-                               if (nl80211_readint(buffer) == phyidx)
-                               {
-                                       snprintf(buffer, sizeof(buffer),
-                                                "/sys/class/net/%s/ifindex", e->d_name);
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/ifindex", e->d_name);
+                       cifidx = nl80211_readint(buffer);
 
-                                       if ((cifidx = nl80211_readint(buffer)) >= 0 &&
-                                           ((ifidx < 0) || (cifidx < ifidx)))
-                                       {
-                                               ifidx = cifidx;
-                                               strncpy(nif, e->d_name, sizeof(nif) - 1);
-                                       }
-                               }
-                       }
+                       if (cifidx < 0)
+                               continue;
 
-                       closedir(d);
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/link_mode", e->d_name);
+                       clmode = nl80211_readint(buffer);
+
+                       /* 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;
@@ -667,7 +870,7 @@ static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
                IWINFO_OPMODE_CLIENT,           /* managed */
                IWINFO_OPMODE_MASTER,           /* AP */
                IWINFO_OPMODE_AP_VLAN,          /* AP/VLAN */
-               IWINFO_OPMODE_WDS,                      /* WDS */
+               IWINFO_OPMODE_WDS,              /* WDS */
                IWINFO_OPMODE_MONITOR,          /* monitor */
                IWINFO_OPMODE_MESHPOINT,        /* mesh point */
                IWINFO_OPMODE_P2P_CLIENT,       /* P2P-client */
@@ -808,8 +1011,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)
        {
@@ -819,14 +1033,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;
@@ -839,6 +1047,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;
 }
 
@@ -852,7 +1064,9 @@ static int __nl80211_wpactl_query(const char *ifname, ...)
        if (nl80211_get_mode(ifname, &mode))
                return 0;
 
-       if (mode != IWINFO_OPMODE_CLIENT && mode != IWINFO_OPMODE_ADHOC)
+       if (mode != IWINFO_OPMODE_CLIENT &&
+           mode != IWINFO_OPMODE_ADHOC &&
+           mode != IWINFO_OPMODE_MESHPOINT)
                return 0;
 
        sock = nl80211_wpactl_connect(ifname, &local);
@@ -966,7 +1180,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)
@@ -1014,6 +1227,20 @@ struct nl80211_ssid_bssid {
        unsigned char bssid[7];
 };
 
+static int nl80211_get_macaddr_cb(struct nl_msg *msg, void *arg)
+{
+       struct nl80211_ssid_bssid *sb = arg;
+       struct nlattr **tb = nl80211_parse(msg);
+
+       if (tb[NL80211_ATTR_MAC]) {
+               sb->bssid[0] = 1;
+               memcpy(sb->bssid + 1, nla_data(tb[NL80211_ATTR_MAC]),
+                      sizeof(sb->bssid) - 1);
+       }
+
+       return NL_SKIP;
+}
+
 static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
 {
        int ielen;
@@ -1022,7 +1249,7 @@ static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
        struct nlattr **tb = nl80211_parse(msg);
        struct nlattr *bss[NL80211_BSS_MAX + 1];
 
-       static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
+       static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
                [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
                [NL80211_BSS_STATUS]               = { .type = NLA_U32 },
        };
@@ -1089,6 +1316,11 @@ static int nl80211_get_ssid(const char *ifname, char *buf)
                nl80211_hostapd_query(ifname, "ssid", sb.ssid,
                                      IWINFO_ESSID_MAX_SIZE + 1);
 
+       /* failed, try to obtain Mesh ID */
+       if (sb.ssid[0] == 0)
+               iwinfo_ubus_query(res ? res : ifname, "mesh_id",
+                                 buf, IWINFO_ESSID_MAX_SIZE + 1);
+
        return (sb.ssid[0] == 0) ? -1 : 0;
 }
 
@@ -1097,11 +1329,17 @@ static int nl80211_get_bssid(const char *ifname, char *buf)
        char *res, bssid[sizeof("FF:FF:FF:FF:FF:FF\0")];
        struct nl80211_ssid_bssid sb = { };
 
-       /* try to find bssid from scan dump results */
        res = nl80211_phy2ifname(ifname);
 
-       nl80211_request(res ? res : ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
-                       nl80211_get_ssid_bssid_cb, &sb);
+       /* try to obtain mac address via NL80211_CMD_GET_INTERFACE */
+       nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
+                       nl80211_get_macaddr_cb, &sb);
+
+       /* failed, try to find bssid from scan dump results */
+       if (sb.bssid[0] == 0)
+               nl80211_request(res ? res : ifname,
+                               NL80211_CMD_GET_SCAN, NLM_F_DUMP,
+                               nl80211_get_ssid_bssid_cb, &sb);
 
        /* failed, try to find mac from hostapd info */
        if ((sb.bssid[0] == 0) &&
@@ -1135,7 +1373,7 @@ static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
        struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *binfo[NL80211_BSS_MAX + 1];
 
-       static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
+       static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
                [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
                [NL80211_BSS_STATUS]    = { .type = NLA_U32 },
        };
@@ -1164,7 +1402,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[2];
+       char *res, channel[4] = { 0 }, hwmode[3] = { 0 }, ax[2] = { 0 };
 
        /* try to find frequency from interface info */
        res = nl80211_phy2ifname(ifname);
@@ -1176,9 +1414,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 */
@@ -1193,6 +1432,56 @@ static int nl80211_get_frequency(const char *ifname, int *buf)
        return (*buf == 0) ? -1 : 0;
 }
 
+static int nl80211_get_center_freq1_cb(struct nl_msg *msg, void *arg)
+{
+       int *freq = arg;
+       struct nlattr **tb = nl80211_parse(msg);
+
+       if (tb[NL80211_ATTR_CENTER_FREQ1])
+               *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
+
+       return NL_SKIP;
+}
+
+static int nl80211_get_center_freq1(const char *ifname, int *buf)
+{
+       char *res;
+
+       /* try to find frequency from interface info */
+       res = nl80211_phy2ifname(ifname);
+       *buf = 0;
+
+       nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
+                       nl80211_get_center_freq1_cb, buf);
+
+       return (*buf == 0) ? -1 : 0;
+}
+
+static int nl80211_get_center_freq2_cb(struct nl_msg *msg, void *arg)
+{
+       int *freq = arg;
+       struct nlattr **tb = nl80211_parse(msg);
+
+       if (tb[NL80211_ATTR_CENTER_FREQ2])
+               *freq = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
+
+       return NL_SKIP;
+}
+
+static int nl80211_get_center_freq2(const char *ifname, int *buf)
+{
+       char *res;
+
+       /* try to find frequency from interface info */
+       res = nl80211_phy2ifname(ifname);
+       *buf = 0;
+
+       nl80211_request(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0,
+                       nl80211_get_center_freq2_cb, buf);
+
+       return (*buf == 0) ? -1 : 0;
+}
+
 static int nl80211_get_channel(const char *ifname, int *buf)
 {
        if (!nl80211_get_frequency(ifname, buf))
@@ -1204,6 +1493,28 @@ static int nl80211_get_channel(const char *ifname, int *buf)
        return -1;
 }
 
+static int nl80211_get_center_chan1(const char *ifname, int *buf)
+{
+       if (!nl80211_get_center_freq1(ifname, buf))
+       {
+               *buf = nl80211_freq2channel(*buf);
+               return 0;
+       }
+
+       return -1;
+}
+
+static int nl80211_get_center_chan2(const char *ifname, int *buf)
+{
+       if (!nl80211_get_center_freq2(ifname, buf))
+       {
+               *buf = nl80211_freq2channel(*buf);
+               return 0;
+       }
+
+       return -1;
+}
+
 static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
 {
        int *buf = arg;
@@ -1239,10 +1550,12 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
        struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
        struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
 
-       static struct nla_policy stats_policy[NL80211_STA_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_BYTES64]    = { .type = NLA_U64    },
+               [NL80211_STA_INFO_TX_BYTES64]    = { .type = NLA_U64    },
                [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
@@ -1252,7 +1565,7 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
        };
 
-       static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
+       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 },
@@ -1267,7 +1580,8 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
                        if (sinfo[NL80211_STA_INFO_SIGNAL])
                        {
                                dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
-                               rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
+                               rr->rssi = (rr->rssi * rr->rssi_samples + dbm) / (rr->rssi_samples + 1);
+                               rr->rssi_samples++;
                        }
 
                        if (sinfo[NL80211_STA_INFO_TX_BITRATE])
@@ -1279,8 +1593,8 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
                                        if (rinfo[NL80211_RATE_INFO_BITRATE])
                                        {
                                                mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
-                                               rr->rate = rr->rate
-                                                       ? (int16_t)((rr->rate + mbit) / 2) : mbit;
+                                               rr->rate = (rr->rate * rr->rate_samples + mbit) / (rr->rate_samples + 1);
+                                               rr->rate_samples++;
                                        }
                                }
                        }
@@ -1295,8 +1609,7 @@ static void nl80211_fill_signal(const char *ifname, struct nl80211_rssi_rate *r)
        DIR *d;
        struct dirent *de;
 
-       r->rssi = 0;
-       r->rate = 0;
+       memset(r, 0, sizeof(*r));
 
        if ((d = opendir("/sys/class/net")) != NULL)
        {
@@ -1321,7 +1634,7 @@ static int nl80211_get_bitrate(const char *ifname, int *buf)
 
        nl80211_fill_signal(ifname, &rr);
 
-       if (rr.rate)
+       if (rr.rate_samples)
        {
                *buf = (rr.rate * 100);
                return 0;
@@ -1336,7 +1649,7 @@ static int nl80211_get_signal(const char *ifname, int *buf)
 
        nl80211_fill_signal(ifname, &rr);
 
-       if (rr.rssi)
+       if (rr.rssi_samples)
        {
                *buf = rr.rssi;
                return 0;
@@ -1351,7 +1664,7 @@ static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
        struct nlattr **tb = nl80211_parse(msg);
        struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
 
-       static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
+       static const struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
                [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
                [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
        };
@@ -1449,10 +1762,116 @@ static int nl80211_check_wepkey(const char *key)
        return 0;
 }
 
+static const struct {
+       const char *match;
+       int version;
+       int suite;
+} wpa_key_mgmt_strings[] = {
+       { "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 },
+       { "None",            0, IWINFO_KMGMT_NONE },
+       { "PSK",             0, IWINFO_KMGMT_PSK },
+       { "EAP",             0, IWINFO_KMGMT_8021x },
+       { "SAE",             4, IWINFO_KMGMT_SAE },
+       { "OWE",             4, IWINFO_KMGMT_OWE }
+};
+
+static void parse_wpa_suites(const char *str, int defversion,
+                             uint8_t *versions, uint8_t *suites)
+{
+       size_t l;
+       int i, version;
+       const char *p, *q, *m, *sep = " \t\n,-+/";
+
+       for (p = str; *p; )
+       {
+               q = p;
+
+               for (i = 0; i < ARRAY_SIZE(wpa_key_mgmt_strings); i++)
+               {
+                       m = wpa_key_mgmt_strings[i].match;
+                       l = strlen(m);
+
+                       if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
+                       {
+                               if (wpa_key_mgmt_strings[i].version != 0)
+                                       version = wpa_key_mgmt_strings[i].version;
+                               else
+                                       version = defversion;
+
+                               *versions |= version;
+                               *suites |= wpa_key_mgmt_strings[i].suite;
+
+                               q += l;
+                               break;
+                       }
+               }
+
+               if (q == p)
+                       q += strcspn(q, sep);
+
+               p = q + strspn(q, sep);
+       }
+}
+
+static const 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-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)
+{
+       int i;
+       size_t l;
+       const char *m, *p, *q, *sep = " \t\n,-+/";
+
+       for (p = str; *p; )
+       {
+               q = p;
+
+               for (i = 0; i < ARRAY_SIZE(wpa_cipher_strings); i++)
+               {
+                       m = wpa_cipher_strings[i].match;
+                       l = strlen(m);
+
+                       if (!strncmp(q, m, l) && (!q[l] || strchr(sep, q[l])))
+                       {
+                               *ciphers |= wpa_cipher_strings[i].cipher;
+
+                               q += l;
+                               break;
+                       }
+               }
+
+               if (q == p)
+                       q += strcspn(q, sep);
+
+               p = q + strspn(q, sep);
+       }
+}
+
 static int nl80211_get_encryption(const char *ifname, char *buf)
 {
-       char wpa[2], wpa_key_mgmt[16], wpa_pairwise[16], wpa_groupwise[16];
+       char *p;
+       int opmode;
+       uint8_t wpa_version = 0;
+       char wpa[2], wpa_key_mgmt[64], wpa_pairwise[16], wpa_groupwise[16];
        char auth_algs[2], wep_key0[27], wep_key1[27], wep_key2[27], wep_key3[27];
+       char mode[16];
 
        struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
 
@@ -1460,69 +1879,62 @@ static int nl80211_get_encryption(const char *ifname, char *buf)
        if (nl80211_wpactl_query(ifname,
                        "pairwise_cipher", wpa_pairwise,  sizeof(wpa_pairwise),
                        "group_cipher",    wpa_groupwise, sizeof(wpa_groupwise),
-                       "key_mgmt",        wpa_key_mgmt,  sizeof(wpa_key_mgmt)))
+                       "key_mgmt",        wpa_key_mgmt,  sizeof(wpa_key_mgmt),
+                       "mode",            mode,          sizeof(mode)))
        {
-               /* WEP */
+               /* WEP or Open */
                if (!strcmp(wpa_key_mgmt, "NONE"))
                {
-                       if (strstr(wpa_pairwise, "WEP-40"))
-                               c->pair_ciphers |= IWINFO_CIPHER_WEP40;
-                       else if (strstr(wpa_pairwise, "WEP-104"))
-                               c->pair_ciphers |= IWINFO_CIPHER_WEP104;
-
-                       if (strstr(wpa_groupwise, "WEP-40"))
-                               c->group_ciphers |= IWINFO_CIPHER_WEP40;
-                       else if (strstr(wpa_groupwise, "WEP-104"))
-                               c->group_ciphers |= IWINFO_CIPHER_WEP104;
-
-                       c->enabled      = !!(c->pair_ciphers | c->group_ciphers);
-                       c->auth_suites |= IWINFO_KMGMT_NONE;
-                       c->auth_algs   |= IWINFO_AUTH_OPEN; /* XXX: assumption */
+                       parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
+                       parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
+
+                       if (c->pair_ciphers != 0 && c->pair_ciphers != IWINFO_CIPHER_NONE) {
+                               c->enabled     = 1;
+                               c->auth_suites = IWINFO_KMGMT_NONE;
+                               c->auth_algs   = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
+                       }
+                       else {
+                               c->pair_ciphers = 0;
+                               c->group_ciphers = 0;
+                       }
                }
 
-               /* WPA */
-               else if (strstr(wpa_key_mgmt, "WPA"))
+               /* MESH with SAE */
+               else if (!strcmp(mode, "mesh") && !strcmp(wpa_key_mgmt, "UNKNOWN"))
                {
-                       if (strstr(wpa_pairwise, "TKIP"))
-                               c->pair_ciphers |= IWINFO_CIPHER_TKIP;
-                       else if (strstr(wpa_pairwise, "CCMP"))
-                               c->pair_ciphers |= IWINFO_CIPHER_CCMP;
-                       else if (strstr(wpa_pairwise, "NONE"))
-                               c->pair_ciphers |= IWINFO_CIPHER_NONE;
-                       else if (strstr(wpa_pairwise, "WEP-40"))
-                               c->pair_ciphers |= IWINFO_CIPHER_WEP40;
-                       else if (strstr(wpa_pairwise, "WEP-104"))
-                               c->pair_ciphers |= IWINFO_CIPHER_WEP104;
-
-                       if (strstr(wpa_groupwise, "TKIP"))
-                               c->group_ciphers |= IWINFO_CIPHER_TKIP;
-                       else if (strstr(wpa_groupwise, "CCMP"))
-                               c->group_ciphers |= IWINFO_CIPHER_CCMP;
-                       else if (strstr(wpa_groupwise, "NONE"))
-                               c->group_ciphers |= IWINFO_CIPHER_NONE;
-                       else if (strstr(wpa_groupwise, "WEP-40"))
-                               c->group_ciphers |= IWINFO_CIPHER_WEP40;
-                       else if (strstr(wpa_groupwise, "WEP-104"))
-                               c->group_ciphers |= IWINFO_CIPHER_WEP104;
-
-                       if (strstr(wpa_key_mgmt, "WPA2"))
-                               c->wpa_version = 2;
-                       else if (strstr(wpa_key_mgmt, "WPA"))
-                               c->wpa_version = 1;
-
-                       if (strstr(wpa_key_mgmt, "PSK"))
-                               c->auth_suites |= IWINFO_KMGMT_PSK;
-                       else if (strstr(wpa_key_mgmt, "EAP") ||
-                                strstr(wpa_key_mgmt, "802.1X"))
-                               c->auth_suites |= IWINFO_KMGMT_8021x;
-                       else if (strstr(wpa_key_mgmt, "NONE"))
-                               c->auth_suites |= IWINFO_KMGMT_NONE;
-
-                       c->enabled = !!(c->wpa_version && c->auth_suites);
+                       c->enabled = 1;
+                       c->wpa_version = 4;
+                       c->auth_suites = IWINFO_KMGMT_SAE;
+                       c->pair_ciphers = IWINFO_CIPHER_CCMP;
+                       c->group_ciphers = IWINFO_CIPHER_CCMP;
                }
 
-               return 0;
-       }
+               /* WPA */
+               else
+               {
+                       parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
+                       parse_wpa_ciphers(wpa_groupwise, &c->group_ciphers);
+
+                       p = wpa_key_mgmt;
+
+                       if (!strncmp(p, "WPA2-", 5) || !strncmp(p, "WPA2/", 5))
+                       {
+                               p += 5;
+                               wpa_version = 2;
+                       }
+                       else if (!strncmp(p, "WPA-", 4))
+                       {
+                               p += 4;
+                               wpa_version = 1;
+                       }
+
+                       parse_wpa_suites(p, wpa_version, &c->wpa_version, &c->auth_suites);
+
+                       c->enabled = !!(c->wpa_version && c->auth_suites);
+               }
+
+               return 0;
+       }
 
        /* Hostapd */
        else if (nl80211_hostapd_query(ifname,
@@ -1535,39 +1947,30 @@ static int nl80211_get_encryption(const char *ifname, char *buf)
                                "wep_key2",     wep_key2,     sizeof(wep_key2),
                                "wep_key3",     wep_key3,     sizeof(wep_key3)))
        {
-               c->wpa_version = wpa[0] ? atoi(wpa) : 0;
+               c->wpa_version = 0;
 
                if (wpa_key_mgmt[0])
                {
-                       if (strstr(wpa_key_mgmt, "PSK"))
-                               c->auth_suites |= IWINFO_KMGMT_PSK;
+                       for (p = strtok(wpa_key_mgmt, " \t"); p != NULL; p = strtok(NULL, " \t"))
+                       {
+                               if (!strncmp(p, "WPA-", 4))
+                                       p += 4;
 
-                       if (strstr(wpa_key_mgmt, "EAP"))
-                               c->auth_suites |= IWINFO_KMGMT_8021x;
+                               if (!strncmp(p, "FT-", 3))
+                                       p += 3;
 
-                       if (strstr(wpa_key_mgmt, "NONE"))
-                               c->auth_suites |= IWINFO_KMGMT_NONE;
-               }
-               else
-               {
-                       c->auth_suites |= IWINFO_KMGMT_PSK;
+                               parse_wpa_suites(p, atoi(wpa), &c->wpa_version, &c->auth_suites);
+                       }
+
+                       c->enabled = c->wpa_version ? 1 : 0;
                }
 
                if (wpa_pairwise[0])
-               {
-                       if (strstr(wpa_pairwise, "TKIP"))
-                               c->pair_ciphers |= IWINFO_CIPHER_TKIP;
-
-                       if (strstr(wpa_pairwise, "CCMP"))
-                               c->pair_ciphers |= IWINFO_CIPHER_CCMP;
-
-                       if (strstr(wpa_pairwise, "NONE"))
-                               c->pair_ciphers |= IWINFO_CIPHER_NONE;
-               }
+                       parse_wpa_ciphers(wpa_pairwise, &c->pair_ciphers);
 
                if (auth_algs[0])
                {
-                       switch(atoi(auth_algs))
+                       switch (atoi(auth_algs))
                        {
                        case 1:
                                c->auth_algs |= IWINFO_AUTH_OPEN;
@@ -1587,14 +1990,26 @@ static int nl80211_get_encryption(const char *ifname, char *buf)
                        c->pair_ciphers |= nl80211_check_wepkey(wep_key1);
                        c->pair_ciphers |= nl80211_check_wepkey(wep_key2);
                        c->pair_ciphers |= nl80211_check_wepkey(wep_key3);
+
+                       c->enabled = (c->auth_algs && c->pair_ciphers) ? 1 : 0;
                }
 
                c->group_ciphers = c->pair_ciphers;
-               c->enabled = (c->wpa_version || c->pair_ciphers) ? 1 : 0;
 
                return 0;
        }
 
+       /* Ad-Hoc or Mesh interfaces without wpa_supplicant are open */
+       else if (!nl80211_get_mode(ifname, &opmode) &&
+                (opmode == IWINFO_OPMODE_ADHOC ||
+                 opmode == IWINFO_OPMODE_MESHPOINT))
+       {
+               c->enabled = 0;
+
+               return 0;
+       }
+
+
        return -1;
 }
 
@@ -1632,7 +2047,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]);
@@ -1666,6 +2093,110 @@ static void nl80211_parse_rateinfo(struct nlattr **ri,
        re->is_40mhz = (re->mhz == 40);
 }
 
+static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg)
+{
+       struct nl80211_array_buf *arr = arg;
+       struct iwinfo_survey_entry *e = arr->buf;
+       struct nlattr **attr = nl80211_parse(msg);
+       struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
+       int rc;
+
+       static const struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
+               [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
+               [NL80211_SURVEY_INFO_NOISE]  = { .type = NLA_U8     },
+               [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64   },
+               [NL80211_SURVEY_INFO_TIME_BUSY] = { .type = NLA_U64   },
+               [NL80211_SURVEY_INFO_TIME_EXT_BUSY] = { .type = NLA_U64   },
+               [NL80211_SURVEY_INFO_TIME_RX] = { .type = NLA_U64   },
+               [NL80211_SURVEY_INFO_TIME_TX] = { .type = NLA_U64   },
+       };
+
+       rc = nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
+                               attr[NL80211_ATTR_SURVEY_INFO],
+                               survey_policy);
+       if (rc)
+               return NL_SKIP;
+
+       /* advance to end of array */
+       e += arr->count;
+       memset(e, 0, sizeof(*e));
+
+       if (sinfo[NL80211_SURVEY_INFO_FREQUENCY])
+               e->mhz = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
+
+       if (sinfo[NL80211_SURVEY_INFO_NOISE])
+               e->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
+
+       if (sinfo[NL80211_SURVEY_INFO_TIME])
+               e->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME]);
+
+       if (sinfo[NL80211_SURVEY_INFO_TIME_BUSY])
+               e->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_BUSY]);
+
+       if (sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY])
+               e->busy_time_ext = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_EXT_BUSY]);
+
+       if (sinfo[NL80211_SURVEY_INFO_TIME_RX])
+               e->rxtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_RX]);
+
+       if (sinfo[NL80211_SURVEY_INFO_TIME_TX])
+               e->txtime = nla_get_u64(sinfo[NL80211_SURVEY_INFO_TIME_TX]);
+
+       arr->count++;
+       return NL_SKIP;
+}
+
+
+static void plink_state_to_str(char *dst, unsigned state)
+{
+       switch (state) {
+       case NL80211_PLINK_LISTEN:
+               strcpy(dst, "LISTEN");
+               break;
+       case NL80211_PLINK_OPN_SNT:
+               strcpy(dst, "OPN_SNT");
+               break;
+       case NL80211_PLINK_OPN_RCVD:
+               strcpy(dst, "OPN_RCVD");
+               break;
+       case NL80211_PLINK_CNF_RCVD:
+               strcpy(dst, "CNF_RCVD");
+               break;
+       case NL80211_PLINK_ESTAB:
+               strcpy(dst, "ESTAB");
+               break;
+       case NL80211_PLINK_HOLDING:
+               strcpy(dst, "HOLDING");
+               break;
+       case NL80211_PLINK_BLOCKED:
+               strcpy(dst, "BLOCKED");
+               break;
+       default:
+               strcpy(dst, "UNKNOWN");
+               break;
+       }
+}
+
+static void power_mode_to_str(char *dst, struct nlattr *a)
+{
+       enum nl80211_mesh_power_mode pm = nla_get_u32(a);
+
+       switch (pm) {
+       case NL80211_MESH_POWER_ACTIVE:
+               strcpy(dst, "ACTIVE");
+               break;
+       case NL80211_MESH_POWER_LIGHT_SLEEP:
+               strcpy(dst, "LIGHT SLEEP");
+               break;
+       case NL80211_MESH_POWER_DEEP_SLEEP:
+               strcpy(dst, "DEEP SLEEP");
+               break;
+       default:
+               strcpy(dst, "UNKNOWN");
+               break;
+       }
+}
+
 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
 {
        struct nl80211_array_buf *arr = arg;
@@ -1675,23 +2206,36 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
        struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
        struct nl80211_sta_flag_update *sta_flags;
 
-       static struct nla_policy stats_policy[NL80211_STA_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_PACKETS]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_RX_BITRATE]    = { .type = NLA_NESTED },
                [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
                [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
+               [NL80211_STA_INFO_SIGNAL_AVG]    = { .type = NLA_U8     },
                [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
+               [NL80211_STA_INFO_RX_BYTES64]    = { .type = NLA_U64    },
+               [NL80211_STA_INFO_TX_BYTES64]    = { .type = NLA_U64    },
                [NL80211_STA_INFO_TX_RETRIES]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_FAILED]     = { .type = NLA_U32    },
+               [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32    },
+               [NL80211_STA_INFO_RX_DROP_MISC]  = { .type = NLA_U64    },
                [NL80211_STA_INFO_T_OFFSET]      = { .type = NLA_U64    },
                [NL80211_STA_INFO_STA_FLAGS] =
                        { .minlen = sizeof(struct nl80211_sta_flag_update) },
+               [NL80211_STA_INFO_EXPECTED_THROUGHPUT]   = { .type = NLA_U32    },
+               /* mesh */
+               [NL80211_STA_INFO_LLID]          = { .type = NLA_U16    },
+               [NL80211_STA_INFO_PLID]          = { .type = NLA_U16    },
+               [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
+               [NL80211_STA_INFO_LOCAL_PM]      = { .type = NLA_U32    },
+               [NL80211_STA_INFO_PEER_PM]       = { .type = NLA_U32    },
+               [NL80211_STA_INFO_NONPEER_PM]    = { .type = NLA_U32    },
        };
 
-       static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
+       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   },
@@ -1712,9 +2256,15 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                if (sinfo[NL80211_STA_INFO_SIGNAL])
                        e->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
 
+               if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
+                       e->signal_avg = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
+
                if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
                        e->inactive = nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]);
 
+               if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
+                       e->connected_time = nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]);
+
                if (sinfo[NL80211_STA_INFO_RX_PACKETS])
                        e->rx_packets = nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]);
 
@@ -1731,10 +2281,14 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                                      sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
                        nl80211_parse_rateinfo(rinfo, &e->tx_rate);
 
-               if (sinfo[NL80211_STA_INFO_RX_BYTES])
+               if (sinfo[NL80211_STA_INFO_RX_BYTES64])
+                       e->rx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]);
+               else if (sinfo[NL80211_STA_INFO_RX_BYTES])
                        e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
 
-               if (sinfo[NL80211_STA_INFO_TX_BYTES])
+               if (sinfo[NL80211_STA_INFO_TX_BYTES64])
+                       e->tx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]);
+               else if (sinfo[NL80211_STA_INFO_TX_BYTES])
                        e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
 
                if (sinfo[NL80211_STA_INFO_TX_RETRIES])
@@ -1746,6 +2300,30 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                if (sinfo[NL80211_STA_INFO_T_OFFSET])
                        e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
 
+               if (sinfo[NL80211_STA_INFO_RX_DROP_MISC])
+                       e->rx_drop_misc = nla_get_u64(sinfo[NL80211_STA_INFO_RX_DROP_MISC]);
+
+               if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
+                       e->thr = nla_get_u32(sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
+
+               /* mesh */
+               if (sinfo[NL80211_STA_INFO_LLID])
+                       e->llid = nla_get_u16(sinfo[NL80211_STA_INFO_LLID]);
+
+               if (sinfo[NL80211_STA_INFO_PLID])
+                       e->plid = nla_get_u16(sinfo[NL80211_STA_INFO_PLID]);
+
+               if (sinfo[NL80211_STA_INFO_PLINK_STATE])
+                       plink_state_to_str(e->plink_state,
+                               nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE]));
+
+               if (sinfo[NL80211_STA_INFO_LOCAL_PM])
+                       power_mode_to_str(e->local_ps, sinfo[NL80211_STA_INFO_LOCAL_PM]);
+               if (sinfo[NL80211_STA_INFO_PEER_PM])
+                       power_mode_to_str(e->peer_ps, sinfo[NL80211_STA_INFO_PEER_PM]);
+               if (sinfo[NL80211_STA_INFO_NONPEER_PM])
+                       power_mode_to_str(e->nonpeer_ps, sinfo[NL80211_STA_INFO_NONPEER_PM]);
+
                /* Station flags */
                if (sinfo[NL80211_STA_INFO_STA_FLAGS])
                {
@@ -1784,6 +2362,21 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
        return NL_SKIP;
 }
 
+static int nl80211_get_survey(const char *ifname, char *buf, int *len)
+{
+       struct nl80211_array_buf arr = { .buf = buf, .count = 0 };
+       int rc;
+
+       rc = nl80211_request(ifname, NL80211_CMD_GET_SURVEY,
+                       NLM_F_DUMP, nl80211_get_survey_cb, &arr);
+       if (!rc)
+               *len = (arr.count * sizeof(struct iwinfo_survey_entry));
+       else
+               *len = 0;
+
+       return 0;
+}
+
 static int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
 {
        DIR *d;
@@ -1828,7 +2421,7 @@ static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
        struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
        struct nlattr *band, *freq;
 
-       static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
+       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 },
                [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
@@ -1871,7 +2464,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))
@@ -1908,53 +2500,39 @@ static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
        return -1;
 }
 
-static void nl80211_get_scancrypto(const char *spec,
-       struct iwinfo_crypto_entry *c)
+static void nl80211_get_scancrypto(char *spec, struct iwinfo_crypto_entry *c)
 {
-       if (strstr(spec, "WPA") || strstr(spec, "WEP"))
-       {
-               c->enabled = 1;
-
-               if (strstr(spec, "WPA2-") && strstr(spec, "WPA-"))
-                       c->wpa_version = 3;
-
-               else if (strstr(spec, "WPA2"))
-                       c->wpa_version = 2;
-
-               else if (strstr(spec, "WPA"))
-                       c->wpa_version = 1;
+       int wpa_version = 0;
+       char *p, *q, *proto, *suites;
 
-               else if (strstr(spec, "WEP"))
-                       c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
-
-
-               if (strstr(spec, "PSK"))
-                       c->auth_suites |= IWINFO_KMGMT_PSK;
-
-               if (strstr(spec, "802.1X") || strstr(spec, "EAP"))
-                       c->auth_suites |= IWINFO_KMGMT_8021x;
-
-               if (strstr(spec, "WPA-NONE"))
-                       c->auth_suites |= IWINFO_KMGMT_NONE;
+       c->enabled = 0;
 
+       for (p = strtok_r(spec, "[]", &q); p; p = strtok_r(NULL, "[]", &q)) {
+               if (!strcmp(p, "WEP")) {
+                       c->enabled      = 1;
+                       c->auth_suites  = IWINFO_KMGMT_NONE;
+                       c->auth_algs    = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
+                       c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
+                       break;
+               }
 
-               if (strstr(spec, "TKIP"))
-                       c->pair_ciphers |= IWINFO_CIPHER_TKIP;
+               proto = strtok(p, "-");
+               suites = strtok(NULL, "]");
 
-               if (strstr(spec, "CCMP"))
-                       c->pair_ciphers |= IWINFO_CIPHER_CCMP;
+               if (!proto || !suites)
+                       continue;
 
-               if (strstr(spec, "WEP-40"))
-                       c->pair_ciphers |= IWINFO_CIPHER_WEP40;
+               if (!strcmp(proto, "WPA2") || !strcmp(proto, "RSN"))
+                       wpa_version = 2;
+               else if (!strcmp(proto, "WPA"))
+                       wpa_version = 1;
+               else
+                       continue;
 
-               if (strstr(spec, "WEP-104"))
-                       c->pair_ciphers |= IWINFO_CIPHER_WEP104;
+               c->enabled = 1;
 
-               c->group_ciphers = c->pair_ciphers;
-       }
-       else
-       {
-               c->enabled = 0;
+               parse_wpa_suites(suites, wpa_version, &c->wpa_version, &c->auth_suites);
+               parse_wpa_ciphers(suites, &c->pair_ciphers);
        }
 }
 
@@ -1978,9 +2556,12 @@ static void nl80211_get_scanlist_ie(struct nlattr **bss,
                switch (ie[0])
                {
                case 0: /* SSID */
-                       len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
-                       memcpy(e->ssid, ie + 2, len);
-                       e->ssid[len] = 0;
+               case 114: /* Mesh ID */
+                       if (e->ssid[0] == 0) {
+                               len = min(ie[1], IWINFO_ESSID_MAX_SIZE);
+                               memcpy(e->ssid, ie + 2, len);
+                               e->ssid[len] = 0;
+                       }
                        break;
 
                case 48: /* RSN */
@@ -1993,6 +2574,20 @@ static void nl80211_get_scanlist_ie(struct nlattr **bss,
                                iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
                                                 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
                        break;
+               case 61: /* HT operation */
+                       if (ie[1] >= 3) {
+                               e->ht_chan_info.primary_chan = ie[2];
+                               e->ht_chan_info.secondary_chan_off = ie[3] & 0x3;
+                               e->ht_chan_info.chan_width = (ie[4] & 0x4)>>2;
+                       }
+                       break;
+               case 192: /* VHT operation */
+                       if (ie[1] >= 3) {
+                               e->vht_chan_info.chan_width = ie[2];
+                               e->vht_chan_info.center_chan_1 = ie[3];
+                               e->vht_chan_info.center_chan_2 = ie[4];
+                       }
+                       break;
                }
 
                ielen -= ie[1] + 2;
@@ -2009,7 +2604,7 @@ static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
        struct nlattr **tb = nl80211_parse(msg);
        struct nlattr *bss[NL80211_BSS_MAX + 1];
 
-       static struct nla_policy bss_policy[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]                = { 0 },
@@ -2050,8 +2645,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);
@@ -2091,7 +2689,8 @@ static int nl80211_get_scanlist_nl(const char *ifname, char *buf, int *len)
        if (nl80211_request(ifname, NL80211_CMD_TRIGGER_SCAN, 0, NULL, NULL))
                goto out;
 
-       if (nl80211_wait("nl80211", "scan", NL80211_CMD_NEW_SCAN_RESULTS))
+       if (nl80211_wait("nl80211", "scan",
+                        NL80211_CMD_NEW_SCAN_RESULTS, NL80211_CMD_SCAN_ABORTED))
                goto out;
 
        if (nl80211_request(ifname, NL80211_CMD_GET_SCAN, NLM_F_DUMP,
@@ -2206,11 +2805,27 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
                        /* is another unrelated event, retry */
                        tries--;
                }
+
+               /* scanning already in progress, keep awaiting results */
+               else if (!strcmp(reply, "FAIL-BUSY\n"))
+               {
+                       tries--;
+               }
+
+               /* another failure, abort */
+               else if (!strncmp(reply, "FAIL-", 5))
+               {
+                       break;
+               }
        }
 
        /* receive and parse scan results if the wait above didn't time out */
-       if (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
+       while (ready && nl80211_wpactl_recv(sock, reply, sizeof(reply)) > 0)
        {
+               /* received an event notification, receive again */
+               if (reply[0] == '<')
+                       continue;
+
                nl80211_get_quality_max(ifname, &qmax);
 
                for (line = strtok_r(reply, "\n", &pos);
@@ -2230,7 +2845,7 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
                        flags  = strtok(NULL, "\t");
                        ssid   = strtok(NULL, "\n");
 
-                       if (!bssid || !freq || !signal || !flags || !ssid)
+                       if (!bssid || !freq || !signal || !flags)
                                continue;
 
                        /* BSSID */
@@ -2242,7 +2857,10 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
                        e->mac[5] = strtol(&bssid[15], NULL, 16);
 
                        /* SSID */
-                       wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
+                       if (ssid)
+                               wpasupp_ssid_decode(ssid, e->ssid, sizeof(e->ssid));
+                       else
+                               e->ssid[0] = 0;
 
                        /* Mode */
                        if (strstr(flags, "[MESH]"))
@@ -2253,7 +2871,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);
@@ -2288,6 +2908,7 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
                }
 
                *len = count * sizeof(struct iwinfo_scanlist_entry);
+               break;
        }
 
        close(sock);
@@ -2377,6 +2998,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;
@@ -2417,14 +3040,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])
@@ -2437,6 +3056,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++;
@@ -2520,78 +3149,119 @@ struct nl80211_modes
        bool ok;
        uint32_t hw;
        uint32_t ht;
+
+       uint8_t bands;
+
+       uint16_t nl_ht;
+       uint32_t nl_vht;
+       uint16_t he_phy_cap[6];
 };
 
+static void nl80211_eval_modelist(struct nl80211_modes *m)
+{
+       /* Treat any nonzero capability as 11n */
+       if (m->nl_ht > 0)
+       {
+               m->hw |= IWINFO_80211_N;
+               m->ht |= IWINFO_HTMODE_HT20;
+
+               if (m->nl_ht & (1 << 1))
+                       m->ht |= IWINFO_HTMODE_HT40;
+       }
+
+       if (m->he_phy_cap[0] != 0) {
+               m->hw |= IWINFO_80211_AX;
+               m->ht |= IWINFO_HTMODE_HE20;
+
+               if (m->he_phy_cap[0] & BIT(9))
+                       m->ht |= IWINFO_HTMODE_HE40;
+               if (m->he_phy_cap[0] & BIT(10))
+                       m->ht |= IWINFO_HTMODE_HE40 | IWINFO_HTMODE_HE80;
+               if (m->he_phy_cap[0] & BIT(11))
+                       m->ht |= IWINFO_HTMODE_HE160;
+               if (m->he_phy_cap[0] & BIT(12))
+                       m->ht |= IWINFO_HTMODE_HE160 | IWINFO_HTMODE_HE80_80;
+       }
+
+       if (m->bands & IWINFO_BAND_24)
+       {
+               m->hw |= IWINFO_80211_B;
+               m->hw |= IWINFO_80211_G;
+       }
+
+       if (m->bands & IWINFO_BAND_5)
+       {
+               /* Treat any nonzero capability as 11ac */
+               if (m->nl_vht > 0)
+               {
+                       m->hw |= IWINFO_80211_AC;
+                       m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
+
+                       switch ((m->nl_vht >> 2) & 3)
+                       {
+                       case 2:
+                               m->ht |= IWINFO_HTMODE_VHT80_80;
+                               /* fall through */
+
+                       case 1:
+                               m->ht |= IWINFO_HTMODE_VHT160;
+                       }
+               }
+               else
+               {
+                       m->hw |= IWINFO_80211_A;
+               }
+       }
+
+       if (m->bands & IWINFO_BAND_60)
+       {
+               m->hw |= IWINFO_80211_AD;
+       }
+
+}
+
 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);
 
                        if (bands[NL80211_BAND_ATTR_HT_CAPA])
-                               caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
-
-                       /* Treat any nonzero capability as 11n */
-                       if (caps > 0)
-                       {
-                               m->hw |= IWINFO_80211_N;
-                               m->ht |= IWINFO_HTMODE_HT20;
-
-                               if (caps & (1 << 1))
-                                       m->ht |= IWINFO_HTMODE_HT40;
-                       }
-
-                       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;
-
-                               if (nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485)
-                               {
-                                       m->hw |= IWINFO_80211_B;
-                                       m->hw |= IWINFO_80211_G;
-                               }
-                               else if (bands[NL80211_BAND_ATTR_VHT_CAPA])
-                               {
-                                       vht_caps = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
-
-                                       /* Treat any nonzero capability as 11ac */
-                                       if (vht_caps > 0)
-                                       {
-                                               m->hw |= IWINFO_80211_AC;
-                                               m->ht |= IWINFO_HTMODE_VHT20 | IWINFO_HTMODE_VHT40 | IWINFO_HTMODE_VHT80;
-
-                                               switch ((vht_caps >> 2) & 3)
-                                               {
-                                               case 2:
-                                                       m->ht |= IWINFO_HTMODE_VHT80_80;
-                                                       /* fall through */
-
-                                               case 1:
-                                                       m->ht |= IWINFO_HTMODE_VHT160;
-                                               }
+                               m->nl_ht = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
+
+                       if (bands[NL80211_BAND_ATTR_VHT_CAPA])
+                               m->nl_vht = nla_get_u32(bands[NL80211_BAND_ATTR_VHT_CAPA]);
+
+                       if (bands[NL80211_BAND_ATTR_IFTYPE_DATA]) {
+                               struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
+                               struct nlattr *nl_iftype;
+                               int rem_band;
+                               int len;
+
+                               nla_for_each_nested(nl_iftype, bands[NL80211_BAND_ATTR_IFTYPE_DATA], rem_band) {
+                                       nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
+                                                 nla_data(nl_iftype), nla_len(nl_iftype), NULL);
+                                       if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
+                                               len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
+
+                                               if (len > sizeof(m->he_phy_cap) - 1)
+                                                       len = sizeof(m->he_phy_cap) - 1;
+                                               memcpy(&((__u8 *)m->he_phy_cap)[1],
+                                                       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
+                                                       len);
                                        }
                                }
-                               else if (!(m->hw & IWINFO_80211_AC))
-                               {
-                                       m->hw |= IWINFO_80211_A;
-                               }
                        }
                }
 
@@ -2603,39 +3273,145 @@ static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
 
 static int nl80211_get_hwmodelist(const char *ifname, int *buf)
 {
-       struct nl80211_modes m = { 0 };
+       struct nl80211_msg_conveyor *cv;
+       struct nl80211_modes m = {};
+       uint32_t features = nl80211_get_protocol_features(ifname);
+       int flags;
 
-       if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
-                           nl80211_get_modelist_cb, &m))
+       flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
+       cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
+       if (!cv)
                goto out;
 
-       if (!m.ok)
-               goto out;
+       NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
+       if (nl80211_send(cv, nl80211_get_modelist_cb, &m))
+               goto nla_put_failure;
+
+       nl80211_eval_modelist(&m);
 
        *buf = m.hw;
+
        return 0;
 
+nla_put_failure:
+       nl80211_free(cv);
 out:
-       *buf = 0;
        return -1;
 }
 
+struct chan_info {
+       int width;
+       int mode;
+};
+
+static int nl80211_get_htmode_cb(struct nl_msg *msg, void *arg)
+{
+       struct nlattr **tb = nl80211_parse(msg);
+       struct nlattr *cur;
+       struct chan_info *chn = arg;
+
+       if ((cur = tb[NL80211_ATTR_CHANNEL_WIDTH]))
+               chn->width = nla_get_u32(cur);
+
+       if ((cur = tb[NL80211_ATTR_BSS_HT_OPMODE]))
+               chn->mode = nla_get_u32(cur);
+
+       return NL_SKIP;
+}
+
+static int nl80211_get_htmode(const char *ifname, int *buf)
+{
+       struct chan_info chn = { 0 };
+       char *res, b[2] = { 0 };
+       int err;
+       bool he = false;
+
+       res = nl80211_phy2ifname(ifname);
+       *buf = 0;
+
+       err =  nl80211_request(res ? res : ifname,
+                               NL80211_CMD_GET_INTERFACE, 0,
+                               nl80211_get_htmode_cb, &chn);
+       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 (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 (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:
+               if (he)
+                       *buf = IWINFO_HTMODE_HE80;
+               else
+                       *buf = IWINFO_HTMODE_VHT80;
+               break;
+       case NL80211_CHAN_WIDTH_80P80:
+               if (he)
+                       *buf = IWINFO_HTMODE_HE80_80;
+               else
+                       *buf = IWINFO_HTMODE_VHT80_80;
+               break;
+       case NL80211_CHAN_WIDTH_160:
+               if (he)
+                       *buf = IWINFO_HTMODE_HE160;
+               else
+                       *buf = IWINFO_HTMODE_VHT160;
+               break;
+       case NL80211_CHAN_WIDTH_5:
+       case NL80211_CHAN_WIDTH_10:
+       case NL80211_CHAN_WIDTH_20_NOHT:
+               *buf = IWINFO_HTMODE_NOHT;
+               break;
+       default:
+               return -1;
+       }
+
+       return 0;
+}
+
 static int nl80211_get_htmodelist(const char *ifname, int *buf)
 {
-       struct nl80211_modes m = { 0 };
+       struct nl80211_msg_conveyor *cv;
+       struct nl80211_modes m = {};
+       uint32_t features = nl80211_get_protocol_features(ifname);
+       int flags;
 
-       if (nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
-                           nl80211_get_modelist_cb, &m))
+       flags = features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP ? NLM_F_DUMP : 0;
+       cv = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, flags);
+       if (!cv)
                goto out;
 
-       if (!m.ok)
-               goto out;
+       NLA_PUT_FLAG(cv->msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
+       if (nl80211_send(cv, nl80211_get_modelist_cb, &m))
+               goto nla_put_failure;
+
+       nl80211_eval_modelist(&m);
 
        *buf = m.ht;
+
        return 0;
 
+nla_put_failure:
+       nl80211_free(cv);
 out:
-       *buf = 0;
        return -1;
 }
 
@@ -2653,12 +3429,12 @@ static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
 
        nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
        {
-               static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
+               static const struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
                        [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
                        [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
                };
                struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
-               static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
+               static const struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
                        [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
                        [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
                };
@@ -2702,39 +3478,62 @@ static int nl80211_get_mbssid_support(const char *ifname, int *buf)
        return 0;
 }
 
+static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname)
+{
+       char *phy, path[PATH_MAX];
+
+       /* Try to determine the phy name from the given interface */
+       phy = nl80211_ifname2phy(ifname);
+
+       snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible",
+                phy ? "ieee80211" : "net", phy ? phy : ifname);
+
+       if (nl80211_readstr(path, id->compatible, sizeof(id->compatible)) <= 0)
+               return -1;
+
+       return 0;
+}
+
+
 static int nl80211_get_hardware_id(const char *ifname, char *buf)
 {
-       int rv = -1;
-       char *res;
+       struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
+       char *phy, num[8], path[PATH_MAX];
+       int i;
+
+       struct { const char *path; uint16_t *dest; } lookup[] = {
+               { "vendor", &id->vendor_id },
+               { "device", &id->device_id },
+               { "subsystem_vendor", &id->subsystem_vendor_id },
+               { "subsystem_device", &id->subsystem_device_id },
+               { "../idVendor", &id->subsystem_vendor_id },
+               { "../idProduct", &id->subsystem_device_id }
+       };
 
-       /* Got a radioX pseudo interface, find some interface on it or create one */
-       if (!strncmp(ifname, "radio", 5))
-       {
-               /* Reuse existing interface */
-               if ((res = nl80211_phy2ifname(ifname)) != NULL)
-               {
-                       rv = wext_ops.hardware_id(res, buf);
-               }
+       memset(id, 0, sizeof(*id));
 
-               /* Need to spawn a temporary iface for finding IDs */
-               else if ((res = nl80211_ifadd(ifname)) != NULL)
-               {
-                       rv = wext_ops.hardware_id(res, buf);
-                       nl80211_ifdel(res);
-               }
-       }
-       else
-       {
-               rv = wext_ops.hardware_id(ifname, buf);
-       }
+       /* Try to determine the phy name from the given interface */
+       phy = nl80211_ifname2phy(ifname);
 
-       /* Failed to obtain hardware IDs, search board config */
-       if (rv)
+       for (i = 0; i < ARRAY_SIZE(lookup); i++)
        {
-               rv = iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
+               snprintf(path, sizeof(path), "/sys/class/%s/%s/device/%s",
+                        phy ? "ieee80211" : "net",
+                        phy ? phy : ifname, lookup[i].path);
+
+               if (nl80211_readstr(path, num, sizeof(num)) > 0)
+                       *lookup[i].dest = strtoul(num, NULL, 16);
        }
 
-       return rv;
+       /* 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;
 }
 
 static const struct iwinfo_hardware_entry *
@@ -2784,12 +3583,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;
 }
 
@@ -2797,6 +3620,8 @@ const struct iwinfo_ops nl80211_ops = {
        .name             = "nl80211",
        .probe            = nl80211_probe,
        .channel          = nl80211_get_channel,
+       .center_chan1     = nl80211_get_center_chan1,
+       .center_chan2     = nl80211_get_center_chan2,
        .frequency        = nl80211_get_frequency,
        .frequency_offset = nl80211_get_frequency_offset,
        .txpower          = nl80211_get_txpower,
@@ -2809,6 +3634,7 @@ const struct iwinfo_ops nl80211_ops = {
        .mbssid_support   = nl80211_get_mbssid_support,
        .hwmodelist       = nl80211_get_hwmodelist,
        .htmodelist       = nl80211_get_htmodelist,
+       .htmode           = nl80211_get_htmode,
        .mode             = nl80211_get_mode,
        .ssid             = nl80211_get_ssid,
        .bssid            = nl80211_get_bssid,
@@ -2822,6 +3648,8 @@ const struct iwinfo_ops nl80211_ops = {
        .scanlist         = nl80211_get_scanlist,
        .freqlist         = nl80211_get_freqlist,
        .countrylist      = nl80211_get_countrylist,
+       .survey           = nl80211_get_survey,
        .lookup_phy       = nl80211_lookup_phyname,
+       .phy_path         = nl80211_phy_path,
        .close            = nl80211_close
 };