]> git.ipfire.org Git - thirdparty/hostap.git/blobdiff - wpa_supplicant/scan.c
Add support to request a scan with specific SSIDs
[thirdparty/hostap.git] / wpa_supplicant / scan.c
index 0653cc28a7b6e0c06810a9c95e4419573f9356a5..e81465c5b3150c568ed325ff3a706c0877f44573 100644 (file)
@@ -96,6 +96,10 @@ int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
 {
        struct wpa_ssid *ssid = wpa_s->conf->ssid;
        int count = 0, disabled = 0;
+
+       if (wpa_s->p2p_mgmt)
+               return 0; /* no normal network profiles on p2p_mgmt interface */
+
        while (ssid) {
                if (!wpas_network_disabled(wpa_s, ssid))
                        count++;
@@ -414,22 +418,6 @@ static void wpa_supplicant_optimize_freqs(
 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
                                           struct wpabuf *buf)
 {
-       if (wpa_s->conf->interworking == 0)
-               return;
-
-       wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
-       wpabuf_put_u8(buf, 6);
-       wpabuf_put_u8(buf, 0x00);
-       wpabuf_put_u8(buf, 0x00);
-       wpabuf_put_u8(buf, 0x00);
-       wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
-       wpabuf_put_u8(buf, 0x00);
-#ifdef CONFIG_HS20
-       wpabuf_put_u8(buf, 0x40); /* Bit 46 - WNM-Notification */
-#else /* CONFIG_HS20 */
-       wpabuf_put_u8(buf, 0x00);
-#endif /* CONFIG_HS20 */
-
        wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
        wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
                      1 + ETH_ALEN);
@@ -444,11 +432,19 @@ static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
 {
        struct wpabuf *extra_ie = NULL;
+       u8 ext_capab[18];
+       int ext_capab_len;
 #ifdef CONFIG_WPS
        int wps = 0;
        enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
 #endif /* CONFIG_WPS */
 
+       ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
+                                            sizeof(ext_capab));
+       if (ext_capab_len > 0 &&
+           wpabuf_resize(&extra_ie, ext_capab_len) == 0)
+               wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
+
 #ifdef CONFIG_INTERWORKING
        if (wpa_s->conf->interworking &&
            wpabuf_resize(&extra_ie, 100) == 0)
@@ -618,6 +614,37 @@ static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
 }
 
 
+static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
+                                      struct wpa_driver_scan_params *params,
+                                      size_t max_ssids)
+{
+       unsigned int i;
+
+       if (wpa_s->ssids_from_scan_req == NULL ||
+           wpa_s->num_ssids_from_scan_req == 0)
+               return 0;
+
+       if (wpa_s->num_ssids_from_scan_req > max_ssids) {
+               wpa_s->num_ssids_from_scan_req = max_ssids;
+               wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
+                          (unsigned int) max_ssids);
+       }
+
+       for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
+               params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
+               params->ssids[i].ssid_len =
+                       wpa_s->ssids_from_scan_req[i].ssid_len;
+               wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
+                                 params->ssids[i].ssid,
+                                 params->ssids[i].ssid_len);
+       }
+
+       params->num_ssids = wpa_s->num_ssids_from_scan_req;
+       wpa_s->num_ssids_from_scan_req = 0;
+       return 1;
+}
+
+
 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
 {
        struct wpa_supplicant *wpa_s = eloop_ctx;
@@ -627,6 +654,7 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
        struct wpa_driver_scan_params params;
        struct wpa_driver_scan_params *scan_params;
        size_t max_ssids;
+       int connect_without_scan = 0;
 
        if (wpa_s->pno || wpa_s->pno_sched_pending) {
                wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
@@ -674,8 +702,20 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
                return;
        }
 
+       ssid = NULL;
+       if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
+           wpa_s->connect_without_scan) {
+               connect_without_scan = 1;
+               for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
+                       if (ssid == wpa_s->connect_without_scan)
+                               break;
+               }
+       }
+
        p2p_in_prog = wpas_p2p_in_progress(wpa_s);
-       if (p2p_in_prog && p2p_in_prog != 2) {
+       if (p2p_in_prog && p2p_in_prog != 2 &&
+           (!ssid ||
+            (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
                wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
                wpa_supplicant_req_scan(wpa_s, 5, 0);
                return;
@@ -692,6 +732,16 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
        wpa_s->last_scan_req = wpa_s->scan_req;
        wpa_s->scan_req = NORMAL_SCAN_REQ;
 
+       if (connect_without_scan) {
+               wpa_s->connect_without_scan = NULL;
+               if (ssid) {
+                       wpa_printf(MSG_DEBUG, "Start a pre-selected network "
+                                  "without scan step");
+                       wpa_supplicant_associate(wpa_s, NULL, ssid);
+                       return;
+               }
+       }
+
        os_memset(&params, 0, sizeof(params));
 
        wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
@@ -707,19 +757,10 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
                goto scan;
        }
 
-       if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
-           wpa_s->connect_without_scan) {
-               for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
-                       if (ssid == wpa_s->connect_without_scan)
-                               break;
-               }
-               wpa_s->connect_without_scan = NULL;
-               if (ssid) {
-                       wpa_printf(MSG_DEBUG, "Start a pre-selected network "
-                                  "without scan step");
-                       wpa_supplicant_associate(wpa_s, NULL, ssid);
-                       return;
-               }
+       if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
+           wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
+               wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
+               goto ssid_list_set;
        }
 
 #ifdef CONFIG_P2P
@@ -887,10 +928,8 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
                wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
                        "SSID");
        }
-#ifdef CONFIG_P2P
-ssid_list_set:
-#endif /* CONFIG_P2P */
 
+ssid_list_set:
        wpa_supplicant_optimize_freqs(wpa_s, &params);
        extra_ie = wpa_supplicant_extra_ies(wpa_s);
 
@@ -1066,8 +1105,17 @@ void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
  */
 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
 {
-       int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
-                                       NULL);
+       int res;
+
+       if (wpa_s->p2p_mgmt) {
+               wpa_dbg(wpa_s, MSG_DEBUG,
+                       "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
+                       sec, usec);
+               return;
+       }
+
+       res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
+                                   NULL);
        if (res == 1) {
                wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
                        sec, usec);
@@ -1594,18 +1642,19 @@ struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  */
 #define GREAT_SNR 30
 
+#define IS_5GHZ(n) (n > 4000)
+
 /* Compare function for sorting scan results. Return >0 if @b is considered
  * better. */
 static int wpa_scan_result_compar(const void *a, const void *b)
 {
-#define IS_5GHZ(n) (n > 4000)
 #define MIN(a,b) a < b ? a : b
        struct wpa_scan_res **_wa = (void *) a;
        struct wpa_scan_res **_wb = (void *) b;
        struct wpa_scan_res *wa = *_wa;
        struct wpa_scan_res *wb = *_wb;
-       int wpa_a, wpa_b, maxrate_a, maxrate_b;
-       int snr_a, snr_b;
+       int wpa_a, wpa_b;
+       int snr_a, snr_b, snr_a_full, snr_b_full;
 
        /* WPA/WPA2 support preferred */
        wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
@@ -1626,37 +1675,34 @@ static int wpa_scan_result_compar(const void *a, const void *b)
            (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
                return -1;
 
-       if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
-           !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
-               snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
-               snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
+       if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
+               snr_a_full = wa->snr;
+               snr_a = MIN(wa->snr, GREAT_SNR);
+               snr_b_full = wb->snr;
+               snr_b = MIN(wa->snr, GREAT_SNR);
        } else {
-               /* Not suitable information to calculate SNR, so use level */
-               snr_a = wa->level;
-               snr_b = wb->level;
+               /* Level is not in dBm, so we can't calculate
+                * SNR. Just use raw level (units unknown). */
+               snr_a = snr_a_full = wa->level;
+               snr_b = snr_b_full = wb->level;
        }
 
-       /* best/max rate preferred if SNR close enough */
-        if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
+       /* if SNR is close, decide by max rate or frequency band */
+       if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
            (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
-               maxrate_a = wpa_scan_get_max_rate(wa);
-               maxrate_b = wpa_scan_get_max_rate(wb);
-               if (maxrate_a != maxrate_b)
-                       return maxrate_b - maxrate_a;
+               if (wa->est_throughput != wb->est_throughput)
+                       return wb->est_throughput - wa->est_throughput;
                if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
                        return IS_5GHZ(wa->freq) ? -1 : 1;
        }
 
-       /* use freq for channel preference */
-
        /* all things being equal, use SNR; if SNRs are
         * identical, use quality values since some drivers may only report
         * that value and leave the signal level zero */
-       if (snr_b == snr_a)
+       if (snr_b_full == snr_a_full)
                return wb->qual - wa->qual;
-       return snr_b - snr_a;
+       return snr_b_full - snr_a_full;
 #undef MIN
-#undef IS_5GHZ
 }
 
 
@@ -1721,21 +1767,22 @@ static void dump_scan_res(struct wpa_scan_results *scan_res)
        for (i = 0; i < scan_res->num; i++) {
                struct wpa_scan_res *r = scan_res->res[i];
                u8 *pos;
-               if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
-                   == WPA_SCAN_LEVEL_DBM) {
-                       int snr = r->level - r->noise;
+               if (r->flags & WPA_SCAN_LEVEL_DBM) {
+                       int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
+
                        wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
-                                  "noise=%d level=%d snr=%d%s flags=0x%x "
-                                  "age=%u",
+                                  "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
                                   MAC2STR(r->bssid), r->freq, r->qual,
-                                  r->noise, r->level, snr,
-                                  snr >= GREAT_SNR ? "*" : "", r->flags,
-                                  r->age);
+                                  r->noise, noise_valid ? "" : "~", r->level,
+                                  r->snr, r->snr >= GREAT_SNR ? "*" : "",
+                                  r->flags,
+                                  r->age, r->est_throughput);
                } else {
                        wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
-                                  "noise=%d level=%d flags=0x%x age=%u",
+                                  "noise=%d level=%d flags=0x%x age=%u est=%u",
                                   MAC2STR(r->bssid), r->freq, r->qual,
-                                  r->noise, r->level, r->flags, r->age);
+                                  r->noise, r->level, r->flags, r->age,
+                                  r->est_throughput);
                }
                pos = (u8 *) (r + 1);
                if (r->ie_len)
@@ -1802,6 +1849,188 @@ static void filter_scan_res(struct wpa_supplicant *wpa_s,
 }
 
 
+/*
+ * Noise floor values to use when we have signal strength
+ * measurements, but no noise floor measurments. These values were
+ * measured in an office environment with many APs.
+ */
+#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
+#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
+
+static void scan_snr(struct wpa_scan_res *res)
+{
+       if (res->flags & WPA_SCAN_NOISE_INVALID) {
+               res->noise = IS_5GHZ(res->freq) ?
+                       DEFAULT_NOISE_FLOOR_5GHZ :
+                       DEFAULT_NOISE_FLOOR_2GHZ;
+       }
+
+       if (res->flags & WPA_SCAN_LEVEL_DBM) {
+               res->snr = res->level - res->noise;
+       } else {
+               /* Level is not in dBm, so we can't calculate
+                * SNR. Just use raw level (units unknown). */
+               res->snr = res->level;
+       }
+}
+
+
+static unsigned int max_ht20_rate(int snr)
+{
+       if (snr < 6)
+               return 6500; /* HT20 MCS0 */
+       if (snr < 8)
+               return 13000; /* HT20 MCS1 */
+       if (snr < 13)
+               return 19500; /* HT20 MCS2 */
+       if (snr < 17)
+               return 26000; /* HT20 MCS3 */
+       if (snr < 20)
+               return 39000; /* HT20 MCS4 */
+       if (snr < 23)
+               return 52000; /* HT20 MCS5 */
+       if (snr < 24)
+               return 58500; /* HT20 MCS6 */
+       return 65000; /* HT20 MCS7 */
+}
+
+
+static unsigned int max_ht40_rate(int snr)
+{
+       if (snr < 3)
+               return 13500; /* HT40 MCS0 */
+       if (snr < 6)
+               return 27000; /* HT40 MCS1 */
+       if (snr < 10)
+               return 40500; /* HT40 MCS2 */
+       if (snr < 15)
+               return 54000; /* HT40 MCS3 */
+       if (snr < 17)
+               return 81000; /* HT40 MCS4 */
+       if (snr < 22)
+               return 108000; /* HT40 MCS5 */
+       if (snr < 24)
+               return 121500; /* HT40 MCS6 */
+       return 135000; /* HT40 MCS7 */
+}
+
+
+static unsigned int max_vht80_rate(int snr)
+{
+       if (snr < 1)
+               return 0;
+       if (snr < 2)
+               return 29300; /* VHT80 MCS0 */
+       if (snr < 5)
+               return 58500; /* VHT80 MCS1 */
+       if (snr < 9)
+               return 87800; /* VHT80 MCS2 */
+       if (snr < 11)
+               return 117000; /* VHT80 MCS3 */
+       if (snr < 15)
+               return 175500; /* VHT80 MCS4 */
+       if (snr < 16)
+               return 234000; /* VHT80 MCS5 */
+       if (snr < 18)
+               return 263300; /* VHT80 MCS6 */
+       if (snr < 20)
+               return 292500; /* VHT80 MCS7 */
+       if (snr < 22)
+               return 351000; /* VHT80 MCS8 */
+       return 390000; /* VHT80 MCS9 */
+}
+
+
+static void scan_est_throughput(struct wpa_supplicant *wpa_s,
+                               struct wpa_scan_res *res)
+{
+       enum local_hw_capab capab = wpa_s->hw_capab;
+       int rate; /* max legacy rate in 500 kb/s units */
+       const u8 *ie;
+       unsigned int est, tmp;
+       int snr = res->snr;
+
+       if (res->est_throughput)
+               return;
+
+       /* Get maximum legacy rate */
+       rate = wpa_scan_get_max_rate(res);
+
+       /* Limit based on estimated SNR */
+       if (rate > 1 * 2 && snr < 1)
+               rate = 1 * 2;
+       else if (rate > 2 * 2 && snr < 4)
+               rate = 2 * 2;
+       else if (rate > 6 * 2 && snr < 5)
+               rate = 6 * 2;
+       else if (rate > 9 * 2 && snr < 6)
+               rate = 9 * 2;
+       else if (rate > 12 * 2 && snr < 7)
+               rate = 12 * 2;
+       else if (rate > 18 * 2 && snr < 10)
+               rate = 18 * 2;
+       else if (rate > 24 * 2 && snr < 11)
+               rate = 24 * 2;
+       else if (rate > 36 * 2 && snr < 15)
+               rate = 36 * 2;
+       else if (rate > 48 * 2 && snr < 19)
+               rate = 48 * 2;
+       else if (rate > 54 * 2 && snr < 21)
+               rate = 54 * 2;
+       est = rate * 500;
+
+       if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
+               ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
+               if (ie) {
+                       tmp = max_ht20_rate(snr);
+                       if (tmp > est)
+                               est = tmp;
+               }
+       }
+
+       if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
+               ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
+               if (ie && ie[1] >= 2 &&
+                   (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
+                       tmp = max_ht40_rate(snr);
+                       if (tmp > est)
+                               est = tmp;
+               }
+       }
+
+       if (capab == CAPAB_VHT) {
+               /* Use +1 to assume VHT is always faster than HT */
+               ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
+               if (ie) {
+                       tmp = max_ht20_rate(snr) + 1;
+                       if (tmp > est)
+                               est = tmp;
+
+                       ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
+                       if (ie && ie[1] >= 2 &&
+                           (ie[3] &
+                            HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
+                               tmp = max_ht40_rate(snr) + 1;
+                               if (tmp > est)
+                                       est = tmp;
+                       }
+
+                       ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
+                       if (ie && ie[1] >= 1 &&
+                           (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
+                               tmp = max_vht80_rate(snr) + 1;
+                               if (tmp > est)
+                                       est = tmp;
+                       }
+               }
+       }
+
+       /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
+
+       res->est_throughput = est;
+}
+
+
 /**
  * wpa_supplicant_get_scan_results - Get scan results
  * @wpa_s: Pointer to wpa_supplicant data
@@ -1835,6 +2064,13 @@ wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
        }
        filter_scan_res(wpa_s, scan_res);
 
+       for (i = 0; i < scan_res->num; i++) {
+               struct wpa_scan_res *scan_res_item = scan_res->res[i];
+
+               scan_snr(scan_res_item);
+               scan_est_throughput(wpa_s, scan_res_item);
+       }
+
 #ifdef CONFIG_WPS
        if (wpas_wps_searching(wpa_s)) {
                wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
@@ -2019,7 +2255,7 @@ void wpa_scan_free_params(struct wpa_driver_scan_params *params)
 
 int wpas_start_pno(struct wpa_supplicant *wpa_s)
 {
-       int ret, interval;
+       int ret, interval, prio;
        size_t i, num_ssid, num_match_ssid;
        struct wpa_ssid *ssid;
        struct wpa_driver_scan_params params;
@@ -2084,8 +2320,10 @@ int wpas_start_pno(struct wpa_supplicant *wpa_s)
                                        sizeof(struct wpa_driver_scan_filter));
        if (params.filter_ssids == NULL)
                return -1;
+
        i = 0;
-       ssid = wpa_s->conf->ssid;
+       prio = 0;
+       ssid = wpa_s->conf->pssid[prio];
        while (ssid) {
                if (!wpas_network_disabled(wpa_s, ssid)) {
                        if (ssid->scan_ssid && params.num_ssids < num_ssid) {
@@ -2103,7 +2341,12 @@ int wpas_start_pno(struct wpa_supplicant *wpa_s)
                        if (i == num_match_ssid)
                                break;
                }
-               ssid = ssid->next;
+               if (ssid->pnext)
+                       ssid = ssid->pnext;
+               else if (prio + 1 == wpa_s->conf->num_prio)
+                       break;
+               else
+                       ssid = wpa_s->conf->pssid[++prio];
        }
 
        if (wpa_s->conf->filter_rssi)