nl80211: fix wpa supplicant ctrl socket permissions
[project/iwinfo.git] / iwinfo_nl80211.c
index 8decdb0ad9be06d528650d76bbcb5e3cef6f9d1c..aceef18230ffd9e419dd1ddaf4ff758004eb9390 100644 (file)
@@ -240,15 +240,16 @@ static const char *nl80211_phy_path_str(const char *phyname)
        int prefix_len = strlen(prefix);
        int buf_len, offset;
        struct dirent *e;
-       char buf[128], *link;
-       int phy_id;
+       char buf[512], *link;
+       int phy_idx;
        int seq = 0;
        DIR *d;
 
-       if (strncmp(phyname, "phy", 3) != 0)
+       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", phyname);
+       phy_idx = nl80211_readint(buf);
+       if (phy_idx < 0)
                return NULL;
 
-       phy_id = atoi(phyname + 3);
        buf_len = snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/device", phyname);
        link = realpath(buf, path);
        if (!link)
@@ -270,13 +271,14 @@ static const char *nl80211_phy_path_str(const char *phyname)
                return link;
 
        while ((e = readdir(d)) != NULL) {
-               int cur_id;
+               int cur_idx;
 
-               if (strncmp(e->d_name, "phy", 3) != 0)
+               snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", e->d_name);
+               cur_idx = nl80211_readint(buf);
+               if (cur_idx < 0)
                        continue;
 
-               cur_id = atoi(e->d_name + 3);
-               if (cur_id >= phy_id)
+               if (cur_idx >= phy_idx)
                        continue;
 
                seq++;
@@ -293,18 +295,23 @@ static const char *nl80211_phy_path_str(const char *phyname)
        return link;
 }
 
-static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
+static int nl80211_phy_idx_from_path(const char *path)
 {
-       char buf[128];
+       char buf[512];
        struct dirent *e;
-       const char *path, *cur_path;
+       const char *cur_path;
+       int cur_path_len;
+       int path_len;
        int idx = -1;
        DIR *d;
 
-       path = uci_lookup_option_string(uci_ctx, s, "path");
        if (!path)
                return -1;
 
+       path_len = strlen(path);
+       if (!path_len)
+               return -1;
+
        d = opendir("/sys/class/ieee80211");
        if (!d)
                return -1;
@@ -314,7 +321,11 @@ static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
                if (!cur_path)
                        continue;
 
-               if (strcmp(cur_path, path) != 0)
+               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);
@@ -329,14 +340,12 @@ static int nl80211_phy_idx_from_uci_path(struct uci_section *s)
        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;
 
@@ -363,12 +372,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;
 
@@ -379,21 +386,27 @@ 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;
 }
@@ -401,7 +414,8 @@ free:
 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)
@@ -410,16 +424,17 @@ 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
                ifidx = if_nametoindex(ifname);
 
+       if (!ifidx) {
+               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))
                return NULL;
@@ -669,7 +684,7 @@ static int nl80211_channel2freq(int channel, const char *band)
        return 0;
 }
 
-static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
+static int nl80211_phyname_cb(struct nl_msg *msg, void *arg)
 {
        char *buf = arg;
        struct nlattr **attr = nl80211_parse(msg);
@@ -690,15 +705,38 @@ 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];
+       char buffer[512];
        static char nif[IFNAMSIZ] = { 0 };
 
        DIR *d;
@@ -707,11 +745,11 @@ static char * nl80211_phy2ifname(const char *ifname)
        /* 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));
@@ -900,6 +938,18 @@ static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
                sprintf(remote.sun_path, "/var/run/wpa_supplicant-%s/%s",
                        ifname, 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)
        {
                close(sock);
@@ -928,6 +978,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;
 }
 
@@ -1057,7 +1111,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)
@@ -1197,7 +1250,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;
 }
@@ -1645,6 +1698,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 },
@@ -1697,12 +1751,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)
@@ -1828,6 +1884,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);
                        }
 
@@ -2327,7 +2386,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))
@@ -2857,6 +2915,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;
@@ -3001,13 +3061,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)
@@ -3033,12 +3094,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)
@@ -3056,27 +3118,28 @@ 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;
        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;
+       uint32_t freq_mhz;
 
        if (attr[NL80211_ATTR_WIPHY_BANDS])
        {
@@ -3122,7 +3185,24 @@ static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
                                        if (!freqs[NL80211_FREQUENCY_ATTR_FREQ])
                                                continue;
 
-                                       m->nl_freq = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
+                                       freq_mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
+
+                                       if (freq_mhz > 2400 && freq_mhz < 2485)
+                                       {
+                                               m->bands |= IWINFO_BAND_24;
+                                       }
+                                       else if (freq_mhz > 5000 && freq_mhz < 5850)
+                                       {
+                                               m->bands |= IWINFO_BAND_5;
+                                       }
+                                       else if (freq_mhz > 6000 && freq_mhz < 7120)
+                                       {
+                                               m->bands |= IWINFO_BAND_6;
+                                       }
+                                       else if (freq_mhz >= 56160)
+                                       {
+                                               m->bands |= IWINFO_BAND_60;
+                                       }
                                }
                        }
                }
@@ -3324,7 +3404,6 @@ 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;
 
        /* Try to determine the phy name from the given interface */
        phy = nl80211_ifname2phy(ifname);
@@ -3375,7 +3454,13 @@ static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const cha
                id->device_id = 0x7622;
                id->subsystem_vendor_id = 0x14c3;
                id->subsystem_device_id = 0x7622;
+       } else if (!strcmp(compat, "mediatek,mt7986-wmac")) {
+               id->vendor_id = 0x14c3;
+               id->device_id = 0x7986;
+               id->subsystem_vendor_id = 0x14c3;
+               id->subsystem_device_id = 0x7986;
        }
+
        return (id->vendor_id && id->device_id) ? 0 : -1;
 }
 
@@ -3467,20 +3552,29 @@ 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;
 
-       sprintf(buf, "phy%d", idx);
+       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 (strncmp(phyname, "phy", 3) != 0)
-               return -1;
-
        if (strchr(phyname, '/'))
                return -1;