OSDN Git Service

Accumulative patch from commit 9e4d304ed7e2516b92081a5fba7288715ebb
[android-x86/external-wpa_supplicant_8.git] / wpa_supplicant / scan.c
index 8df21d9..91a436a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * WPA Supplicant - Scanning
- * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
@@ -11,6 +11,7 @@
 #include "utils/common.h"
 #include "utils/eloop.h"
 #include "common/ieee802_11_defs.h"
+#include "common/wpa_ctrl.h"
 #include "config.h"
 #include "wpa_supplicant_i.h"
 #include "driver_i.h"
@@ -66,7 +67,8 @@ static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
        }
 
 #ifdef CONFIG_P2P
-       if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) {
+       if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
+           !wpa_s->conf->p2p_disabled) {
                wpa_s->wps->dev.p2p = 1;
                if (!wps) {
                        wps = 1;
@@ -80,15 +82,33 @@ static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
 #endif /* CONFIG_WPS */
 
 
+/**
+ * wpa_supplicant_enabled_networks - Check whether there are enabled networks
+ * @wpa_s: Pointer to wpa_supplicant data
+ * Returns: 0 if no networks are enabled, >0 if networks are enabled
+ *
+ * This function is used to figure out whether any networks (or Interworking
+ * with enabled credentials and auto_interworking) are present in the current
+ * configuration.
+ */
 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
 {
        struct wpa_ssid *ssid = wpa_s->conf->ssid;
-       int count = 0;
+       int count = 0, disabled = 0;
        while (ssid) {
                if (!wpas_network_disabled(wpa_s, ssid))
                        count++;
+               else
+                       disabled++;
                ssid = ssid->next;
        }
+       if (wpa_s->conf->cred && wpa_s->conf->interworking &&
+           wpa_s->conf->auto_interworking)
+               count++;
+       if (count == 0 && disabled > 0) {
+               wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
+                       "networks)", disabled);
+       }
        return count;
 }
 
@@ -138,7 +158,7 @@ static void int_array_concat(int **res, const int *a)
        reslen = int_array_len(*res);
        alen = int_array_len(a);
 
-       n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
+       n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
        if (n == NULL) {
                os_free(*res);
                *res = NULL;
@@ -189,6 +209,12 @@ static void int_array_sort_unique(int *a)
 }
 
 
+/**
+ * wpa_supplicant_trigger_scan - Request driver to start a scan
+ * @wpa_s: Pointer to wpa_supplicant data
+ * @params: Scan parameters
+ * Returns: 0 on success, -1 on failure
+ */
 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
                                struct wpa_driver_scan_params *params)
 {
@@ -385,9 +411,7 @@ static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
 #endif /* CONFIG_INTERWORKING */
 
 
-static struct wpabuf *
-wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
-                        struct wpa_driver_scan_params *params)
+static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
 {
        struct wpabuf *extra_ie = NULL;
 #ifdef CONFIG_WPS
@@ -432,11 +456,78 @@ wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
 }
 
 
+#ifdef CONFIG_P2P
+
+/*
+ * Check whether there are any enabled networks or credentials that could be
+ * used for a non-P2P connection.
+ */
+static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
+{
+       struct wpa_ssid *ssid;
+
+       for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
+               if (wpas_network_disabled(wpa_s, ssid))
+                       continue;
+               if (!ssid->p2p_group)
+                       return 1;
+       }
+
+       if (wpa_s->conf->cred && wpa_s->conf->interworking &&
+           wpa_s->conf->auto_interworking)
+               return 1;
+
+       return 0;
+}
+
+
+/*
+ * Find the operating frequency of any other virtual interface that is using
+ * the same radio concurrently.
+ */
+static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
+{
+       const char *rn, *rn2;
+       struct wpa_supplicant *ifs;
+       u8 bssid[ETH_ALEN];
+
+       if (!wpa_s->driver->get_radio_name)
+               return -1;
+
+       rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
+       if (rn == NULL || rn[0] == '\0')
+               return -1;
+
+       for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
+               if (ifs == wpa_s || !ifs->driver->get_radio_name)
+                       continue;
+
+               rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
+               if (!rn2 || os_strcmp(rn, rn2) != 0)
+                       continue;
+
+               if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
+                       continue;
+
+               if (ifs->current_ssid->mode == WPAS_MODE_AP ||
+                   ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
+                       return ifs->current_ssid->frequency;
+               if (wpa_drv_get_bssid(ifs, bssid) == 0)
+                       return ifs->assoc_freq;
+       }
+
+       return 0;
+}
+
+#endif /* CONFIG_P2P */
+
+
 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
 {
        struct wpa_supplicant *wpa_s = eloop_ctx;
        struct wpa_ssid *ssid;
-       int scan_req = 0, ret;
+       enum scan_req_type scan_req = NORMAL_SCAN_REQ;
+       int ret;
        struct wpabuf *extra_ie = NULL;
        struct wpa_driver_scan_params params;
        struct wpa_driver_scan_params *scan_params;
@@ -448,15 +539,25 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
                return;
        }
 
-       if (wpa_s->disconnected && !wpa_s->scan_req) {
+       if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
+               wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
                wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
                return;
        }
-
+#ifdef ANDROID
+       if (wpa_s->scanning) {
+               /* If we are already in scanning state, we shall ignore this new scan request*/
+               wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
+               return;
+       }
+#endif
        if (!wpa_supplicant_enabled_networks(wpa_s) &&
-           !wpa_s->scan_req) {
+           wpa_s->scan_req == NORMAL_SCAN_REQ) {
                wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
                wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
+#ifdef CONFIG_P2P
+               wpa_s->sta_scan_pending = 0;
+#endif /* CONFIG_P2P */
                return;
        }
 
@@ -475,15 +576,18 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
 
 #ifdef CONFIG_P2P
        if (wpas_p2p_in_progress(wpa_s)) {
-               if (wpa_s->wpa_state == WPA_SCANNING) {
+               if (wpa_s->sta_scan_pending &&
+                   wpas_p2p_in_progress(wpa_s) == 2 &&
+                   wpa_s->global->p2p_cb_on_scan_complete) {
+                       wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
+                               "mode scan during P2P search");
+               } else {
                        wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
                                "while P2P operation is in progress");
+                       wpa_s->sta_scan_pending = 1;
                        wpa_supplicant_req_scan(wpa_s, 5, 0);
-               } else {
-                       wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
-                               "P2P operation is in progress");
+                       return;
                }
-               return;
        }
 #endif /* CONFIG_P2P */
 
@@ -496,7 +600,7 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
        }
 
        scan_req = wpa_s->scan_req;
-       wpa_s->scan_req = 0;
+       wpa_s->scan_req = NORMAL_SCAN_REQ;
 
        os_memset(&params, 0, sizeof(params));
 
@@ -513,7 +617,7 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
                goto scan;
        }
 
-       if (scan_req != 2 && wpa_s->connect_without_scan) {
+       if (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;
@@ -551,7 +655,7 @@ static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
                }
        }
 
-       if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
+       if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
                wpa_s->connect_without_scan = NULL;
                wpa_s->prev_scan_wildcard = 0;
                wpa_supplicant_assoc_try(wpa_s, ssid);
@@ -642,10 +746,10 @@ ssid_list_set:
 #endif /* CONFIG_P2P */
 
        wpa_supplicant_optimize_freqs(wpa_s, &params);
-       extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
+       extra_ie = wpa_supplicant_extra_ies(wpa_s);
 
 #ifdef CONFIG_HS20
-       if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
+       if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
                wpas_hs20_add_indication(extra_ie);
 #endif /* CONFIG_HS20 */
 
@@ -678,6 +782,35 @@ ssid_list_set:
        scan_params = &params;
 
 scan:
+#ifdef CONFIG_P2P
+       /*
+        * If the driver does not support multi-channel concurrency and a
+        * virtual interface that shares the same radio with the wpa_s interface
+        * is operating there may not be need to scan other channels apart from
+        * the current operating channel on the other virtual interface. Filter
+        * out other channels in case we are trying to find a connection for a
+        * station interface when we are not configured to prefer station
+        * connection and a concurrent operation is already in process.
+        */
+       if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
+           !scan_params->freqs && !params.freqs &&
+           wpas_is_p2p_prioritized(wpa_s) &&
+           !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
+           wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
+           non_p2p_network_enabled(wpa_s)) {
+               int freq = shared_vif_oper_freq(wpa_s);
+               if (freq > 0) {
+                       wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
+                               "operating channel (%d MHz) since driver does "
+                               "not support multi-channel concurrency", freq);
+                       params.freqs = os_zalloc(sizeof(int) * 2);
+                       if (params.freqs)
+                               params.freqs[0] = freq;
+                       scan_params->freqs = params.freqs;
+               }
+       }
+#endif /* CONFIG_P2P */
+
        ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
 
        wpabuf_free(extra_ie);
@@ -688,8 +821,33 @@ scan:
                wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
                if (prev_state != wpa_s->wpa_state)
                        wpa_supplicant_set_state(wpa_s, prev_state);
+               /* Restore scan_req since we will try to scan again */
+               wpa_s->scan_req = scan_req;
                wpa_supplicant_req_scan(wpa_s, 1, 0);
+       } else {
+               wpa_s->scan_for_connection = 0;
+       }
+}
+
+
+void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
+{
+       struct os_time remaining, new_int;
+       int cancelled;
+
+       cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
+                                            &remaining);
+
+       new_int.sec = sec;
+       new_int.usec = 0;
+       if (cancelled && os_time_before(&remaining, &new_int)) {
+               new_int.sec = remaining.sec;
+               new_int.usec = remaining.usec;
        }
+
+       eloop_register_timeout(new_int.sec, new_int.usec, wpa_supplicant_scan,
+                              wpa_s, NULL);
+       wpa_s->scan_interval = sec;
 }
 
 
@@ -740,6 +898,7 @@ void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  * @wpa_s: Pointer to wpa_supplicant data
  * @sec: Number of seconds after which to scan
  * @usec: Number of microseconds after which to scan
+ * Returns: 0 on success or -1 otherwise
  *
  * This function is used to schedule periodic scans for neighboring
  * access points after the specified time.
@@ -761,6 +920,7 @@ int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
 /**
  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  * @wpa_s: Pointer to wpa_supplicant data
+ * Returns: 0 is sched_scan was started or -1 otherwise
  *
  * This function is used to schedule periodic scans for neighboring
  * access points repeating the scan continuously.
@@ -771,7 +931,7 @@ int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
        struct wpa_driver_scan_params *scan_params;
        enum wpa_states prev_state;
        struct wpa_ssid *ssid = NULL;
-       struct wpabuf *wps_ie = NULL;
+       struct wpabuf *extra_ie = NULL;
        int ret;
        unsigned int max_sched_scan_ssids;
        int wildcard = 0;
@@ -935,8 +1095,11 @@ int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
                params.filter_ssids = NULL;
        }
 
-       if (wpa_s->wps)
-               wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
+       extra_ie = wpa_supplicant_extra_ies(wpa_s);
+       if (extra_ie) {
+               params.extra_ies = wpabuf_head(extra_ie);
+               params.extra_ies_len = wpabuf_len(extra_ie);
+       }
 
        scan_params = &params;
 
@@ -953,7 +1116,7 @@ scan:
 
        ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
                                              wpa_s->sched_scan_interval);
-       wpabuf_free(wps_ie);
+       wpabuf_free(extra_ie);
        os_free(params.filter_ssids);
        if (ret) {
                wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
@@ -1011,6 +1174,16 @@ void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
 }
 
 
+/**
+ * wpa_supplicant_notify_scanning - Indicate possible scan state change
+ * @wpa_s: Pointer to wpa_supplicant data
+ * @scanning: Whether scanning is currently in progress
+ *
+ * This function is to generate scanning notifycations. It is called whenever
+ * there may have been a change in scanning (scan started, completed, stopped).
+ * wpas_notify_scanning() is called whenever the scanning state changed from the
+ * previously notified state.
+ */
 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
                                    int scanning)
 {
@@ -1048,6 +1221,15 @@ static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
 }
 
 
+/**
+ * wpa_scan_get_ie - Fetch a specified information element from a scan result
+ * @res: Scan result entry
+ * @ie: Information element identitifier (WLAN_EID_*)
+ * Returns: Pointer to the information element (id field) or %NULL if not found
+ *
+ * This function returns the first matching information element in the scan
+ * result.
+ */
 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
 {
        const u8 *end, *pos;
@@ -1067,6 +1249,15 @@ const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
 }
 
 
+/**
+ * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
+ * @res: Scan result entry
+ * @vendor_type: Vendor type (four octets starting the IE payload)
+ * Returns: Pointer to the information element (id field) or %NULL if not found
+ *
+ * This function returns the first matching information element in the scan
+ * result.
+ */
 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
                                  u32 vendor_type)
 {
@@ -1088,6 +1279,16 @@ const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
 }
 
 
+/**
+ * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
+ * @res: Scan result entry
+ * @vendor_type: Vendor type (four octets starting the IE payload)
+ * Returns: Pointer to the information element payload or %NULL if not found
+ *
+ * This function returns concatenated payload of possibly fragmented vendor
+ * specific information elements in the scan result. The caller is responsible
+ * for freeing the returned buffer.
+ */
 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
                                             u32 vendor_type)
 {
@@ -1119,40 +1320,6 @@ 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;
-}
-
-
 /*
  * Channels with a great SNR can operate at full rate. What is a great SNR?
  * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
@@ -1294,15 +1461,17 @@ static void dump_scan_res(struct wpa_scan_results *scan_res)
                    == WPA_SCAN_LEVEL_DBM) {
                        int snr = r->level - r->noise;
                        wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
-                                  "noise=%d level=%d snr=%d%s flags=0x%x",
+                                  "noise=%d level=%d snr=%d%s flags=0x%x "
+                                  "age=%u",
                                   MAC2STR(r->bssid), r->freq, r->qual,
                                   r->noise, r->level, snr,
-                                  snr >= GREAT_SNR ? "*" : "", r->flags);
+                                  snr >= GREAT_SNR ? "*" : "", r->flags,
+                                  r->age);
                } else {
                        wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
-                                  "noise=%d level=%d flags=0x%x",
+                                  "noise=%d level=%d flags=0x%x age=%u",
                                   MAC2STR(r->bssid), r->freq, r->qual,
-                                  r->noise, r->level, r->flags);
+                                  r->noise, r->level, r->flags, r->age);
                }
                pos = (u8 *) (r + 1);
                if (r->ie_len)
@@ -1316,6 +1485,15 @@ static void dump_scan_res(struct wpa_scan_results *scan_res)
 }
 
 
+/**
+ * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
+ * @wpa_s: Pointer to wpa_supplicant data
+ * @bssid: BSSID to check
+ * Returns: 0 if the BSSID is filtered or 1 if not
+ *
+ * This function is used to filter out specific BSSIDs from scan reslts mainly
+ * for testing purposes (SET bssid_filter ctrl_iface command).
+ */
 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
                                      const u8 *bssid)
 {
@@ -1384,6 +1562,13 @@ wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
                wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
                return NULL;
        }
+       if (scan_res->fetch_time.sec == 0) {
+               /*
+                * Make sure we have a valid timestamp if the driver wrapper
+                * does not set this.
+                */
+               os_get_time(&scan_res->fetch_time);
+       }
        filter_scan_res(wpa_s, scan_res);
 
 #ifdef CONFIG_WPS
@@ -1400,13 +1585,26 @@ wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
 
        wpa_bss_update_start(wpa_s);
        for (i = 0; i < scan_res->num; i++)
-               wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
+               wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
+                                       &scan_res->fetch_time);
        wpa_bss_update_end(wpa_s, info, new_scan);
 
        return scan_res;
 }
 
 
+/**
+ * wpa_supplicant_update_scan_results - Update scan results from the driver
+ * @wpa_s: Pointer to wpa_supplicant data
+ * Returns: 0 on success, -1 on failure
+ *
+ * This function updates the BSS table within wpa_supplicant based on the
+ * currently available scan results from the driver without requesting a new
+ * scan. This is used in cases where the driver indicates an association
+ * (including roaming within ESS) and wpa_supplicant does not yet have the
+ * needed information to complete the connection (e.g., to perform validation
+ * steps in 4-way handshake).
+ */
 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
 {
        struct wpa_scan_results *scan_res;
@@ -1417,3 +1615,16 @@ int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
 
        return 0;
 }
+
+
+/**
+ * scan_only_handler - Reports scan results
+ */
+void scan_only_handler(struct wpa_supplicant *wpa_s,
+                      struct wpa_scan_results *scan_res)
+{
+       wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
+       wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
+       wpas_notify_scan_results(wpa_s);
+       wpas_notify_scan_done(wpa_s, 1);
+}