devices: add usb device MediaTek MT7921AU
[project/iwinfo.git] / iwinfo_nl80211.c
index ba925306486bfece54b516dd086f2164d54122d9..50b1ab4085d9b86e66b3066180fa5b96488e03ef 100644 (file)
@@ -240,7 +240,7 @@ 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;
+       char buf[512], *link;
        int phy_idx;
        int seq = 0;
        DIR *d;
@@ -297,7 +297,7 @@ static const char *nl80211_phy_path_str(const char *phyname)
 
 static int nl80211_phy_idx_from_path(const char *path)
 {
-       char buf[128];
+       char buf[512];
        struct dirent *e;
        const char *cur_path;
        int cur_path_len;
@@ -736,7 +736,7 @@ nla_put_failure:
 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;
@@ -935,8 +935,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)
        {
@@ -946,14 +957,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;
@@ -966,6 +971,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;
 }
 
@@ -1095,7 +1104,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)
@@ -1235,7 +1243,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;
 }
@@ -2371,7 +2379,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))
@@ -2901,6 +2908,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;
@@ -3052,7 +3061,7 @@ struct nl80211_modes
        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)
@@ -3119,8 +3128,6 @@ 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];
@@ -3390,7 +3397,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);
@@ -3462,7 +3468,9 @@ static int nl80211_get_hardware_id(const char *ifname, char *buf)
                { "vendor", &id->vendor_id },
                { "device", &id->device_id },
                { "subsystem_vendor", &id->subsystem_vendor_id },
-               { "subsystem_device", &id->subsystem_device_id }
+               { "subsystem_device", &id->subsystem_device_id },
+               { "../idVendor", &id->subsystem_vendor_id },
+               { "../idProduct", &id->subsystem_device_id }
        };
 
        memset(id, 0, sizeof(*id));
@@ -3481,12 +3489,14 @@ static int nl80211_get_hardware_id(const char *ifname, char *buf)
        }
 
        /* Failed to obtain hardware IDs, try FDT */
-       if (id->vendor_id == 0 || id->device_id == 0)
+       if (id->vendor_id == 0 && id->device_id == 0 &&
+           id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0)
                if (!nl80211_hardware_id_from_fdt(id, ifname))
                        return 0;
 
        /* Failed to obtain hardware IDs, search board config */
-       if (id->vendor_id == 0 || id->device_id == 0)
+       if (id->vendor_id == 0 && id->device_id == 0 &&
+           id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0)
                return iwinfo_hardware_id_from_mtd(id);
 
        return 0;