libiwinfo: properly detect open network reported by wpa_supplicant
[project/luci.git] / contrib / package / iwinfo / src / iwinfo_nl80211.c
index 91ede2e..4868db1 100644 (file)
@@ -29,7 +29,6 @@
 
 extern struct iwinfo_iso3166_label ISO3166_Names[];
 static struct nl80211_state *nls = NULL;
-static int nl80211_ioctlsock = -1;
 
 static int nl80211_init(void)
 {
@@ -37,19 +36,6 @@ static int nl80211_init(void)
 
        if( !nls )
        {
-               nl80211_ioctlsock = socket(AF_INET, SOCK_DGRAM, 0);
-               if( nl80211_ioctlsock < 0 )
-               {
-                       err = -ENOLINK;
-                       goto err;
-               }
-               else if( fcntl(nl80211_ioctlsock, F_SETFD,
-                                          fcntl(nl80211_ioctlsock, F_GETFD) | FD_CLOEXEC) < 0 )
-               {
-                       err = -EINVAL;
-                       goto err;
-               }
-
                nls = malloc(sizeof(struct nl80211_state));
                if( !nls ) {
                        err = -ENOMEM;
@@ -414,16 +400,35 @@ out:
        return rv;
 }
 
+static inline int nl80211_readint(const char *path)
+{
+       int fd;
+       int rv = -1;
+       char buffer[16];
+
+       if( (fd = open(path, O_RDONLY)) > -1 )
+       {
+               if( read(fd, buffer, sizeof(buffer)) > 0 )
+                       rv = atoi(buffer);
+
+               close(fd);
+       }
+
+       return rv;
+}
+
 static char * nl80211_phy2ifname(const char *ifname)
 {
-       int fd, phyidx = -1;
+       int fd, ifidx = -1, cifidx = -1, phyidx = -1;
        char buffer[64];
        static char nif[IFNAMSIZ] = { 0 };
 
        DIR *d;
        struct dirent *e;
 
-       if( !strncmp(ifname, "phy", 3) )
+       if( !ifname )
+               return NULL;
+       else if( !strncmp(ifname, "phy", 3) )
                phyidx = atoi(&ifname[3]);
        else if( !strncmp(ifname, "radio", 5) )
                phyidx = atoi(&ifname[5]);
@@ -437,19 +442,18 @@ static char * nl80211_phy2ifname(const char *ifname)
                                snprintf(buffer, sizeof(buffer),
                                        "/sys/class/net/%s/phy80211/index", e->d_name);
 
-                               if( (fd = open(buffer, O_RDONLY)) > 0 )
+                               if( nl80211_readint(buffer) == phyidx )
                                {
-                                       if( (read(fd, buffer, sizeof(buffer)) > 0) &&
-                                           (atoi(buffer) == phyidx) )
+                                       snprintf(buffer, sizeof(buffer),
+                                               "/sys/class/net/%s/ifindex", e->d_name);
+
+                                       if( (cifidx = nl80211_readint(buffer)) >= 0 &&
+                                           ((ifidx < 0) || (cifidx < ifidx)) )
                                        {
+                                               ifidx = cifidx;
                                                strncpy(nif, e->d_name, sizeof(nif));
                                        }
-
-                                       close(fd);
                                }
-
-                               if( nif[0] )
-                                       break;
                        }
 
                        closedir(d);
@@ -504,49 +508,6 @@ static void nl80211_ifdel(const char *ifname)
        }
 }
 
-static int nl80211_ifup(const char *ifname)
-{
-       struct ifreq ifr;
-
-       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
-       if( ioctl(nl80211_ioctlsock, SIOCGIFFLAGS, &ifr) )
-               return 0;
-
-       ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
-
-       return !ioctl(nl80211_ioctlsock, SIOCSIFFLAGS, &ifr);
-}
-
-static int nl80211_ifdown(const char *ifname)
-{
-       struct ifreq ifr;
-
-       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
-       if( ioctl(nl80211_ioctlsock, SIOCGIFFLAGS, &ifr) )
-               return 0;
-
-       ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
-
-       return !ioctl(nl80211_ioctlsock, SIOCSIFFLAGS, &ifr);
-}
-
-static int nl80211_ifmac(const char *ifname)
-{
-       struct ifreq ifr;
-
-       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
-       if( ioctl(nl80211_ioctlsock, SIOCGIFHWADDR, &ifr) )
-               return 0;
-
-       ifr.ifr_hwaddr.sa_data[1]++;
-       ifr.ifr_hwaddr.sa_data[2]++;
-
-       return !ioctl(nl80211_ioctlsock, SIOCSIFHWADDR, &ifr);
-}
-
 static void nl80211_hostapd_hup(const char *ifname)
 {
        int fd, pid = 0;
@@ -577,11 +538,6 @@ int nl80211_probe(const char *ifname)
 
 void nl80211_close(void)
 {
-       if( nl80211_ioctlsock > -1 )
-       {
-               close(nl80211_ioctlsock);
-       }
-
        if( nls )
        {
                if( nls->nl_sock )
@@ -648,12 +604,28 @@ int nl80211_get_bssid(const char *ifname, char *buf)
 
 int nl80211_get_channel(const char *ifname, int *buf)
 {
-       return wext_get_channel(ifname, buf);
+       char *first;
+
+       if( !wext_get_channel(ifname, buf) )
+               return 0;
+
+       else if( (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL )
+               return wext_get_channel(first, buf);
+
+       return -1;
 }
 
 int nl80211_get_frequency(const char *ifname, int *buf)
 {
-       return wext_get_frequency(ifname, buf);
+       char *first;
+
+       if( !wext_get_frequency(ifname, buf) )
+               return 0;
+
+       else if( (first = nl80211_phy2ifname(nl80211_ifname2phy(ifname))) != NULL )
+               return wext_get_frequency(first, buf);
+
+       return -1;
 }
 
 int nl80211_get_txpower(const char *ifname, int *buf)
@@ -782,10 +754,11 @@ int nl80211_get_signal(const char *ifname, int *buf)
        return -1;
 }
 
-int nl80211_get_noise(const char *ifname, int *buf)
+static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
 {
-       int rv = -1;
-       struct nl80211_msg_conveyor *req, *res;
+       int8_t *noise = arg;
+       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+       struct nlattr *tb[NL80211_ATTR_MAX + 1];
        struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
 
        static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
@@ -793,28 +766,48 @@ int nl80211_get_noise(const char *ifname, int *buf)
                [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
        };
 
+       nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
+               genlmsg_attrlen(gnlh, 0), NULL);
+
+       if (!tb[NL80211_ATTR_SURVEY_INFO])
+               return NL_SKIP;
+
+       if (nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
+                                                tb[NL80211_ATTR_SURVEY_INFO], sp))
+               return NL_SKIP;
+
+       if (!si[NL80211_SURVEY_INFO_NOISE])
+               return NL_SKIP;
+
+       if (!*noise || si[NL80211_SURVEY_INFO_IN_USE])
+               *noise = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
+
+       return NL_SKIP;
+}
+
+
+int nl80211_get_noise(const char *ifname, int *buf)
+{
+       int8_t noise;
+       struct nl80211_msg_conveyor *req;
+
        req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
-       if( req )
+       if (req)
        {
-               res = nl80211_send(req);
-               if( res )
+               noise = 0;
+
+               nl80211_cb(req, nl80211_get_noise_cb, &noise);
+               nl80211_send(req);
+               nl80211_free(req);
+
+               if (noise)
                {
-                       if( res->attr[NL80211_ATTR_SURVEY_INFO] )
-                       {
-                               if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
-                                               res->attr[NL80211_ATTR_SURVEY_INFO], sp) &&
-                                       si[NL80211_SURVEY_INFO_NOISE] )
-                               {
-                                       *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
-                                       rv = 0;
-                               }
-                       }
-                       nl80211_free(res);
+                       *buf = noise;
+                       return 0;
                }
-               nl80211_free(req);
        }
 
-       return rv;
+       return -1;
 }
 
 int nl80211_get_quality(const char *ifname, int *buf)
@@ -872,10 +865,34 @@ int nl80211_get_encryption(const char *ifname, char *buf)
        /* Hostapd */
        if( (res = nl80211_hostapd_info(ifname)) )
        {
-               if( (val = nl80211_getval(ifname, res, "auth_algs")) && (val > 0) )
-               {
+               if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
+                       c->wpa_version = atoi(val);
+
+               val = nl80211_getval(ifname, res, "wpa_key_mgmt");
+
+               if( !val || strstr(val, "PSK") )
+                       c->auth_suites |= IWINFO_KMGMT_PSK;
+
+               if( val && strstr(val, "EAP") )
+                       c->auth_suites |= IWINFO_KMGMT_8021x;
+
+               if( val && strstr(val, "NONE") )
                        c->auth_suites |= IWINFO_KMGMT_NONE;
 
+               if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
+               {
+                       if( strstr(val, "TKIP") )
+                               c->pair_ciphers |= IWINFO_CIPHER_TKIP;
+
+                       if( strstr(val, "CCMP") )
+                               c->pair_ciphers |= IWINFO_CIPHER_CCMP;
+
+                       if( strstr(val, "NONE") )
+                               c->pair_ciphers |= IWINFO_CIPHER_NONE;
+               }
+
+               if( (val = nl80211_getval(ifname, res, "auth_algs")) != NULL )
+               {
                        switch(atoi(val)) {
                                case 1:
                                        c->auth_algs |= IWINFO_AUTH_OPEN;
@@ -907,42 +924,8 @@ int nl80211_get_encryption(const char *ifname, char *buf)
                                                c->pair_ciphers |= IWINFO_CIPHER_WEP104;
                                }
                        }
-
-                       c->group_ciphers = c->pair_ciphers;
-
-                       return 0;
                }
 
-
-               if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
-                       c->wpa_version = atoi(val);
-
-
-               val = nl80211_getval(ifname, res, "wpa_key_mgmt");
-
-               if( !val || strstr(val, "PSK") )
-                       c->auth_suites |= IWINFO_KMGMT_PSK;
-
-               if( val && strstr(val, "EAP") )
-                       c->auth_suites |= IWINFO_KMGMT_8021x;
-
-               if( val && strstr(val, "NONE") )
-                       c->auth_suites |= IWINFO_KMGMT_NONE;
-
-
-               if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
-               {
-                       if( strstr(val, "TKIP") )
-                               c->pair_ciphers |= IWINFO_CIPHER_TKIP;
-
-                       if( strstr(val, "CCMP") )
-                               c->pair_ciphers |= IWINFO_CIPHER_CCMP;
-
-                       if( strstr(val, "NONE") )
-                               c->pair_ciphers |= IWINFO_CIPHER_NONE;
-               }
-
-
                c->group_ciphers = c->pair_ciphers;
                c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
 
@@ -1169,13 +1152,13 @@ int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
                     dbm_cur += 2, dbm_cnt++ )
                {
                        entry.dbm = dbm_cur;
-                       entry.mw  = wext_dbm2mw(dbm_cur);
+                       entry.mw  = iwinfo_dbm2mw(dbm_cur);
 
                        memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
                }
 
                entry.dbm = dbm_max;
-               entry.mw  = wext_dbm2mw(dbm_max);
+               entry.mw  = iwinfo_dbm2mw(dbm_max);
 
                memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
                dbm_cnt++;
@@ -1190,11 +1173,7 @@ int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
 static void nl80211_get_scancrypto(const char *spec,
        struct iwinfo_crypto_entry *c)
 {
-       if( strstr(spec, "OPEN") )
-       {
-               c->enabled = 0;
-       }
-       else
+       if( strstr(spec, "WPA") || strstr(spec, "WEP") )
        {
                c->enabled = 1;
 
@@ -1235,6 +1214,10 @@ static void nl80211_get_scancrypto(const char *spec,
 
                c->group_ciphers = c->pair_ciphers;
        }
+       else
+       {
+               c->enabled = 0;
+       }
 }
 
 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
@@ -1266,8 +1249,7 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
        struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
 
        /* WPA supplicant */
-       if( (res = nl80211_wpasupp_info(ifname, "SCAN")) &&
-           !strcmp(res, "OK\n") )
+       if( (res = nl80211_wpasupp_info(ifname, "SCAN")) && !strcmp(res, "OK\n") )
        {
                sleep(2);
 
@@ -1346,11 +1328,11 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
                /* Got a temp interface, don't create yet another one */
                if( !strncmp(ifname, "tmp.", 4) )
                {
-                       if( !nl80211_ifup(ifname) )
+                       if( !iwinfo_ifup(ifname) )
                                return -1;
 
                        wext_get_scanlist(ifname, buf, len);
-                       nl80211_ifdown(ifname);
+                       iwinfo_ifdown(ifname);
                        return 0;
                }
 
@@ -1360,24 +1342,24 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
                        if( !(res = nl80211_ifadd(ifname)) )
                                goto out;
 
-                       if( !nl80211_ifmac(res) )
+                       if( !iwinfo_ifmac(res) )
                                goto out;
 
                        /* if we can take the new interface up, the driver supports an
                         * additional interface and there's no need to tear down the ap */
-                       if( nl80211_ifup(res) )
+                       if( iwinfo_ifup(res) )
                        {
                                wext_get_scanlist(res, buf, len);
-                               nl80211_ifdown(res);
+                               iwinfo_ifdown(res);
                        }
 
                        /* driver cannot create secondary interface, take down ap
                         * during scan */
-                       else if( nl80211_ifdown(ifname) && nl80211_ifup(res) )
+                       else if( iwinfo_ifdown(ifname) && iwinfo_ifup(res) )
                        {
                                wext_get_scanlist(res, buf, len);
-                               nl80211_ifdown(res);
-                               nl80211_ifup(ifname);
+                               iwinfo_ifdown(res);
+                               iwinfo_ifup(ifname);
                                nl80211_hostapd_hup(ifname);
                        }
 
@@ -1552,9 +1534,9 @@ int nl80211_get_mbssid_support(const char *ifname, int *buf)
 
        if( nif )
        {
-               *buf = (nl80211_ifmac(nif) && nl80211_ifup(nif));
+               *buf = (iwinfo_ifmac(nif) && iwinfo_ifup(nif));
 
-               nl80211_ifdown(nif);
+               iwinfo_ifdown(nif);
                nl80211_ifdel(nif);
 
                return 0;