libiwinfo: reuse existing temporary interface for scanning
[project/luci.git] / contrib / package / iwinfo / src / iwinfo_nl80211.c
index 7d2a96a6c131376e73d4062ebe2cf2a0372ea5b1..fe099fc7283bb364228024122dbbaff34fa06962 100644 (file)
@@ -77,17 +77,7 @@ static int nl80211_init(void)
 
 
 err:
-       if( nls && nls->nl_sock )
-               nl_socket_free(nls->nl_sock);
-
-       if( nls && nls->nl_cache )
-               nl_cache_free(nls->nl_cache);
-
-       if( nls )
-               free(nls);
-
-       nls = NULL;
-
+       nl80211_close();
        return err;
 }
 
@@ -148,19 +138,21 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, in
 {
        static struct nl80211_msg_conveyor cv;
 
-       int ifidx;
+       int ifidx = -1, phyidx = -1;
        struct nl_msg *req = NULL;
        struct nl_cb *cb = NULL;
 
        if( nl80211_init() < 0 )
                goto err;
 
-       if( !strncmp(ifname, "mon.", 4) )
+       if( !strncmp(ifname, "radio", 5) )
+               phyidx = atoi(&ifname[5]);
+       else if( !strncmp(ifname, "mon.", 4) )
                ifidx = if_nametoindex(&ifname[4]);
        else
                ifidx = if_nametoindex(ifname);
 
-       if( ifidx < 0 )
+       if( (ifidx < 0) && (phyidx < 0) )
                return NULL;
 
        req = nlmsg_alloc();
@@ -174,7 +166,11 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, in
        genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0,
                flags, cmd, 0);
 
-       NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
+       if( ifidx > -1 )
+               NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
+
+       if( phyidx > -1 )
+               NLA_PUT_U32(req, NL80211_ATTR_WIPHY, phyidx);
 
        nlmsg_get(req);
 
@@ -389,12 +385,114 @@ out:
        return rv;
 }
 
+static char * nl80211_phy2ifname(const char *ifname)
+{
+       int fd, phyidx = 0;
+       char buffer[64];
+       static char nif[IFNAMSIZ] = { 0 };
+
+       DIR *d;
+       struct dirent *e;
+
+       if( !strncmp(ifname, "radio", 5) )
+       {
+               phyidx = atoi(&ifname[5]);
+
+               if( (d = opendir("/sys/class/net")) != NULL )
+               {
+                       while( (e = readdir(d)) != NULL )
+                       {
+                               snprintf(buffer, sizeof(buffer),
+                                       "/sys/class/net/%s/phy80211/index", e->d_name);
+
+                               if( (fd = open(buffer, O_RDONLY)) > 0 )
+                               {
+                                       if( (read(fd, buffer, sizeof(buffer)) > 0) &&
+                                           (atoi(buffer) == phyidx) )
+                                       {
+                                               strncpy(nif, e->d_name, sizeof(nif));
+                                       }
+
+                                       close(fd);
+                               }
+
+                               if( nif[0] )
+                                       break;
+                       }
+
+                       closedir(d);
+               }
+       }
+
+       return nif[0] ? nif : NULL;
+}
+
+static char * nl80211_add_tempif(const char *ifname)
+{
+       int phyidx;
+       char *rv = NULL;
+       static char nif[IFNAMSIZ] = { 0 };
+       struct nl80211_msg_conveyor *req, *res;
+
+       req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
+       if( req )
+       {
+               snprintf(nif, sizeof(nif), "tmp.%s", ifname);
+
+               NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
+               NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
+
+               res = nl80211_send(req);
+               if( res )
+               {
+                       rv = nif;
+                       nl80211_free(res);
+               }
+
+       nla_put_failure:
+               nl80211_free(req);
+       }
+
+       return rv;
+}
+
+static void nl80211_del_tempif(const char *ifname)
+{
+       struct nl80211_msg_conveyor *req, *res;
+
+       req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
+       if( req )
+       {
+               NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
+
+               nl80211_free(nl80211_send(req));
+
+       nla_put_failure:
+               nl80211_free(req);
+       }
+}
+
 
 int nl80211_probe(const char *ifname)
 {
        return !!nl80211_ifname2phy(ifname);
 }
 
+void nl80211_close(void)
+{
+       if( nls )
+       {
+               if( nls->nl_sock )
+                       nl_socket_free(nls->nl_sock);
+
+               if( nls->nl_cache )
+                       nl_cache_free(nls->nl_cache);
+
+               free(nls);
+               nls = NULL;
+       }
+}
+
 int nl80211_get_mode(const char *ifname, char *buf)
 {
        return wext_get_mode(ifname, buf);
@@ -1040,13 +1138,31 @@ static void nl80211_get_scancrypto(const char *spec,
 
 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
 {
-       int freq, rssi, qmax, count;
+       int freq, rssi, qmax, count, is_tmp = 0;
        char *res;
        char cmd[256];
        char ssid[128] = { 0 };
        char bssid[18] = { 0 };
        char cipher[256] = { 0 };
 
+       /* 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 )
+               {
+                       return nl80211_get_scanlist(res, buf, len);
+               }
+
+               /* Need to spawn a temporary iface for scanning */
+               else if( (res = nl80211_add_tempif(ifname)) != NULL )
+               {
+                       count = nl80211_get_scanlist(res, buf, len);
+                       nl80211_del_tempif(res);
+                       return count;
+               }
+       }
+
        struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
 
        /* WPA supplicant */
@@ -1129,40 +1245,55 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
        {
                if( (res = nl80211_ifname2phy(ifname)) != NULL )
                {
-                       /*
-                        * This is a big ugly hack, just look away.
-                        */
+                       /* Got a temp interface, don't create yet another one */
+                       if( !strncmp(ifname, "tmp.", 4) )
+                       {
+                               sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
+                               if( WEXITSTATUS(system(cmd)) )
+                                       return -1;
+
+                               wext_get_scanlist(ifname, buf, len);
+
+                               sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
+                               (void) WEXITSTATUS(system(cmd));
 
-                       sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
-                       if( WEXITSTATUS(system(cmd)) )
-                               goto out;
+                               return 0;
+                       }
 
-                       sprintf(cmd, "iw phy %s interface add scan.%s "
-                               "type station 2>/dev/null", res, ifname);
-                       if( WEXITSTATUS(system(cmd)) )
-                               goto out;
+                       /* Spawn a new scan interface */
+                       else
+                       {
+                               sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
+                               if( WEXITSTATUS(system(cmd)) )
+                                       goto out;
 
-                       sprintf(cmd, "ifconfig scan.%s up 2>/dev/null", ifname);
-                       if( WEXITSTATUS(system(cmd)) )
-                               goto out;
+                               sprintf(cmd, "iw phy %s interface add scan.%s "
+                                       "type station 2>/dev/null", res, ifname);
+                               if( WEXITSTATUS(system(cmd)) )
+                                       goto out;
 
-                       sprintf(cmd, "scan.%s", ifname);
-                       wext_get_scanlist(cmd, buf, len);
+                               sprintf(cmd, "ifconfig scan.%s up 2>/dev/null", ifname);
+                               if( WEXITSTATUS(system(cmd)) )
+                                       goto out;
 
-       out:
-                       sprintf(cmd, "ifconfig scan.%s down 2>/dev/null", ifname);
-                       (void) WEXITSTATUS(system(cmd));
+                               sprintf(cmd, "scan.%s", ifname);
+                               wext_get_scanlist(cmd, buf, len);
 
-                       sprintf(cmd, "iw dev scan.%s del 2>/dev/null", ifname);
-                       (void) WEXITSTATUS(system(cmd));
+                       out:
+                               sprintf(cmd, "ifconfig scan.%s down 2>/dev/null", ifname);
+                               (void) WEXITSTATUS(system(cmd));
 
-                       sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
-                       (void) WEXITSTATUS(system(cmd));
+                               sprintf(cmd, "iw dev scan.%s del 2>/dev/null", ifname);
+                               (void) WEXITSTATUS(system(cmd));
 
-                       sprintf(cmd, "killall -HUP hostapd 2>/dev/null");
-                       (void) WEXITSTATUS(system(cmd));
+                               sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
+                               (void) WEXITSTATUS(system(cmd));
 
-                       return 0;
+                               sprintf(cmd, "killall -HUP hostapd 2>/dev/null");
+                               (void) WEXITSTATUS(system(cmd));
+
+                               return 0;
+                       }
                }
        }
 
@@ -1171,7 +1302,63 @@ int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
 
 int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
 {
-       return wext_get_freqlist(ifname, buf, len);
+       char *phy;
+       int count = 0, bands_remain, freqs_remain;
+       struct nl80211_msg_conveyor *req, *res;
+       struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
+       struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
+       struct nlattr *band, *freq;
+       struct iwinfo_freqlist_entry *e = (struct iwinfo_freqlist_entry *)buf;
+
+       static 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 },
+               [NL80211_FREQUENCY_ATTR_NO_IBSS]      = { .type = NLA_FLAG },
+               [NL80211_FREQUENCY_ATTR_RADAR]        = { .type = NLA_FLAG },
+               [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32  },
+       };
+
+       if( !wext_get_freqlist(ifname, buf, len) )
+               return 0;
+
+       req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
+       if( req )
+       {
+               res = nl80211_send(req);
+               if( res )
+               {
+                       nla_for_each_nested(band,
+                               res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
+                       {
+                               nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
+                                         nla_len(band), NULL);
+
+                               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), freq_policy);
+
+                                       e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
+                                       e->channel = nl80211_freq2channel(e->mhz);
+
+                                       e++;
+                                       count++;
+                               }
+                       }
+                       nl80211_free(res);
+               }
+               nl80211_free(req);
+       }
+
+       if( count > 0 )
+       {
+               *len = count * sizeof(struct iwinfo_freqlist_entry);
+               return 0;
+       }
+
+       return -1;
 }
 
 int nl80211_get_country(const char *ifname, char *buf)