Merge branch 'moonshot' of ssh://moonshot.suchdamage.org:822/srv/git/libeap into...
[libeap.git] / wpa_supplicant / scan.c
index fd01bb8..ee105d9 100644 (file)
@@ -22,6 +22,8 @@
 #include "driver_i.h"
 #include "mlme.h"
 #include "wps_supplicant.h"
+#include "p2p_supplicant.h"
+#include "p2p/p2p.h"
 #include "notify.h"
 #include "bss.h"
 #include "scan.h"
@@ -243,8 +245,8 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
        struct wpa_ssid *ssid;
        int scan_req = 0, ret;
        struct wpabuf *wps_ie = NULL;
-       int wps = 0;
 #ifdef CONFIG_WPS
+       int wps = 0;
        enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
 #endif /* CONFIG_WPS */
        struct wpa_driver_scan_params params;
@@ -294,18 +296,6 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
        wps = wpas_wps_in_use(wpa_s->conf, &req_type);
 #endif /* CONFIG_WPS */
 
-       if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
-           !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
-           wps != 2 && !wpa_s->conf->filter_ssids &&
-           !wpa_s->connect_without_scan) {
-               wpa_s->scan_res_tried++;
-               wpa_printf(MSG_DEBUG, "Trying to get current scan results "
-                          "first without requesting a new scan to speed up "
-                          "initial association");
-               wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
-               return;
-       }
-
        scan_req = wpa_s->scan_req;
        wpa_s->scan_req = 0;
 
@@ -393,6 +383,36 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
                wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
        }
 
+#ifdef CONFIG_P2P
+       wpa_s->wps->dev.p2p = 1;
+       if (!wps) {
+               wps = 1;
+               req_type = WPS_REQ_ENROLLEE_INFO;
+       }
+
+       if (params.freqs == NULL && wpa_s->p2p_in_provisioning &&
+           wpa_s->go_params) {
+               /* Optimize provisioning state scan based on GO information */
+               if (wpa_s->p2p_in_provisioning < 5 &&
+                   wpa_s->go_params->freq > 0) {
+                       wpa_printf(MSG_DEBUG, "P2P: Scan only GO preferred "
+                                  "frequency %d MHz",
+                                  wpa_s->go_params->freq);
+                       params.freqs = os_zalloc(2 * sizeof(int));
+                       if (params.freqs)
+                               params.freqs[0] = wpa_s->go_params->freq;
+               } else if (wpa_s->go_params->freq_list[0]) {
+                       wpa_printf(MSG_DEBUG, "P2P: Scan only common "
+                                  "channels");
+                       int_array_concat(&params.freqs,
+                                        wpa_s->go_params->freq_list);
+                       if (params.freqs)
+                               int_array_sort_unique(params.freqs);
+               }
+               wpa_s->p2p_in_provisioning++;
+       }
+#endif /* CONFIG_P2P */
+
 #ifdef CONFIG_WPS
        if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
                /*
@@ -417,6 +437,16 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
        }
 #endif /* CONFIG_WPS */
 
+#ifdef CONFIG_P2P
+       if (wps_ie) {
+               if (wpabuf_resize(&wps_ie, 100) == 0) {
+                       wpas_p2p_scan_ie(wpa_s, wps_ie);
+                       params.extra_ies = wpabuf_head(wps_ie);
+                       params.extra_ies_len = wpabuf_len(wps_ie);
+               }
+       }
+#endif /* CONFIG_P2P */
+
        params.filter_ssids = wpa_supplicant_build_filter_ssids(
                wpa_s->conf, &params.num_filter_ssids);
 
@@ -592,6 +622,40 @@ struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
 }
 
 
+struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
+       const struct wpa_scan_res *res, u32 vendor_type)
+{
+       struct wpabuf *buf;
+       const u8 *end, *pos;
+
+       if (res->beacon_ie_len == 0)
+               return NULL;
+       buf = wpabuf_alloc(res->beacon_ie_len);
+       if (buf == NULL)
+               return NULL;
+
+       pos = (const u8 *) (res + 1);
+       pos += res->ie_len;
+       end = pos + res->beacon_ie_len;
+
+       while (pos + 1 < end) {
+               if (pos + 2 + pos[1] > end)
+                       break;
+               if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
+                   vendor_type == WPA_GET_BE32(&pos[2]))
+                       wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
+               pos += 2 + pos[1];
+       }
+
+       if (wpabuf_len(buf) == 0) {
+               wpabuf_free(buf);
+               buf = NULL;
+       }
+
+       return buf;
+}
+
+
 /* 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)
@@ -641,6 +705,54 @@ static int wpa_scan_result_compar(const void *a, const void *b)
 }
 
 
+#ifdef CONFIG_WPS
+/* Compare function for sorting scan results when searching a WPS AP for
+ * provisioning. Return >0 if @b is considered better. */
+static int wpa_scan_result_wps_compar(const void *a, const void *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 uses_wps_a, uses_wps_b;
+       struct wpabuf *wps_a, *wps_b;
+       int res;
+
+       /* Optimization - check WPS IE existence before allocated memory and
+        * doing full reassembly. */
+       uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
+       uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
+       if (uses_wps_a && !uses_wps_b)
+               return -1;
+       if (!uses_wps_a && uses_wps_b)
+               return 1;
+
+       if (uses_wps_a && uses_wps_b) {
+               wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
+               wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
+               res = wps_ap_priority_compar(wps_a, wps_b);
+               wpabuf_free(wps_a);
+               wpabuf_free(wps_b);
+               if (res)
+                       return res;
+       }
+
+       /*
+        * Do not use current AP security policy as a sorting criteria during
+        * WPS provisioning step since the AP may get reconfigured at the
+        * completion of provisioning.
+        */
+
+       /* all things being equal, use signal level; if signal levels are
+        * identical, use quality values since some drivers may only report
+        * that value and leave the signal level zero */
+       if (wb->level == wa->level)
+               return wb->qual - wa->qual;
+       return wb->level - wa->level;
+}
+#endif /* CONFIG_WPS */
+
+
 /**
  * wpa_supplicant_get_scan_results - Get scan results
  * @wpa_s: Pointer to wpa_supplicant data
@@ -658,6 +770,7 @@ wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
 {
        struct wpa_scan_results *scan_res;
        size_t i;
+       int (*compar)(const void *, const void *) = wpa_scan_result_compar;
 
        if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
                scan_res = ieee80211_sta_get_scan_results(wpa_s);
@@ -668,8 +781,16 @@ wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
                return NULL;
        }
 
+#ifdef CONFIG_WPS
+       if (wpas_wps_in_progress(wpa_s)) {
+               wpa_printf(MSG_DEBUG, "WPS: Order scan results with WPS "
+                          "provisioning rules");
+               compar = wpa_scan_result_wps_compar;
+       }
+#endif /* CONFIG_WPS */
+
        qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
-             wpa_scan_result_compar);
+             compar);
 
        wpa_bss_update_start(wpa_s);
        for (i = 0; i < scan_res->num; i++)