2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
15 #include "wpa_supplicant_i.h"
17 #include "wps_supplicant.h"
18 #include "p2p_supplicant.h"
20 #include "hs20_supplicant.h"
26 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
28 struct wpa_ssid *ssid;
29 union wpa_event_data data;
31 ssid = wpa_supplicant_get_ssid(wpa_s);
35 if (wpa_s->current_ssid == NULL) {
36 wpa_s->current_ssid = ssid;
37 if (wpa_s->current_ssid != NULL)
38 wpas_notify_network_changed(wpa_s);
40 wpa_supplicant_initiate_eapol(wpa_s);
41 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
42 "network - generating associated event");
43 os_memset(&data, 0, sizeof(data));
44 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
49 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
50 enum wps_request_type *req_type)
52 struct wpa_ssid *ssid;
55 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
56 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
60 *req_type = wpas_wps_get_req_type(ssid);
61 if (!ssid->eap.phase1)
64 if (os_strstr(ssid->eap.phase1, "pbc=1"))
69 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) {
70 wpa_s->wps->dev.p2p = 1;
73 *req_type = WPS_REQ_ENROLLEE_INFO;
76 #endif /* CONFIG_P2P */
80 #endif /* CONFIG_WPS */
83 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
85 struct wpa_ssid *ssid = wpa_s->conf->ssid;
88 if (!wpas_network_disabled(wpa_s, ssid))
96 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
97 struct wpa_ssid *ssid)
100 if (!wpas_network_disabled(wpa_s, ssid))
105 /* ap_scan=2 mode - try to associate with each SSID. */
107 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
108 "end of scan list - go back to beginning");
109 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
110 wpa_supplicant_req_scan(wpa_s, 0, 0);
114 /* Continue from the next SSID on the next attempt. */
115 wpa_s->prev_scan_ssid = ssid;
117 /* Start from the beginning of the SSID list. */
118 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
120 wpa_supplicant_associate(wpa_s, NULL, ssid);
124 static int int_array_len(const int *a)
127 for (i = 0; a && a[i]; i++)
133 static void int_array_concat(int **res, const int *a)
138 reslen = int_array_len(*res);
139 alen = int_array_len(a);
141 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
147 for (i = 0; i <= alen; i++)
148 n[reslen + i] = a[i];
153 static int freq_cmp(const void *a, const void *b)
166 static void int_array_sort_unique(int *a)
174 alen = int_array_len(a);
175 qsort(a, alen, sizeof(int), freq_cmp);
179 while (a[i] && a[j]) {
192 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
193 struct wpa_driver_scan_params *params)
197 wpa_supplicant_notify_scanning(wpa_s, 1);
199 ret = wpa_drv_scan(wpa_s, params);
201 wpa_supplicant_notify_scanning(wpa_s, 0);
202 wpas_notify_scan_done(wpa_s, 0);
205 wpa_s->normal_scans++;
213 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
215 struct wpa_supplicant *wpa_s = eloop_ctx;
217 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
219 if (wpa_supplicant_req_sched_scan(wpa_s))
220 wpa_supplicant_req_scan(wpa_s, 0, 0);
225 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
227 struct wpa_supplicant *wpa_s = eloop_ctx;
229 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
231 wpa_s->sched_scan_timed_out = 1;
232 wpa_supplicant_cancel_sched_scan(wpa_s);
237 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
238 struct wpa_driver_scan_params *params,
243 wpa_supplicant_notify_scanning(wpa_s, 1);
245 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
247 wpa_supplicant_notify_scanning(wpa_s, 0);
249 wpa_s->sched_scanning = 1;
251 wpa_supplicant_notify_scanning(wpa_s, 1);
259 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
263 ret = wpa_drv_stop_sched_scan(wpa_s);
265 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
266 /* TODO: what to do if stopping fails? */
274 static struct wpa_driver_scan_filter *
275 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
277 struct wpa_driver_scan_filter *ssids;
278 struct wpa_ssid *ssid;
282 if (!conf->filter_ssids)
285 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
286 if (ssid->ssid && ssid->ssid_len)
291 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
295 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
296 if (!ssid->ssid || !ssid->ssid_len)
298 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
299 ssids[*num_ssids].ssid_len = ssid->ssid_len;
307 static void wpa_supplicant_optimize_freqs(
308 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
311 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
313 /* Optimize provisioning state scan based on GO information */
314 if (wpa_s->p2p_in_provisioning < 5 &&
315 wpa_s->go_params->freq > 0) {
316 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
317 "preferred frequency %d MHz",
318 wpa_s->go_params->freq);
319 params->freqs = os_zalloc(2 * sizeof(int));
321 params->freqs[0] = wpa_s->go_params->freq;
322 } else if (wpa_s->p2p_in_provisioning < 8 &&
323 wpa_s->go_params->freq_list[0]) {
324 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
326 int_array_concat(¶ms->freqs,
327 wpa_s->go_params->freq_list);
329 int_array_sort_unique(params->freqs);
331 wpa_s->p2p_in_provisioning++;
333 #endif /* CONFIG_P2P */
336 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
338 * Optimize post-provisioning scan based on channel used
339 * during provisioning.
341 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
342 "that was used during provisioning", wpa_s->wps_freq);
343 params->freqs = os_zalloc(2 * sizeof(int));
345 params->freqs[0] = wpa_s->wps_freq;
349 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
351 /* Optimize provisioning scan based on already known channel */
352 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
354 params->freqs = os_zalloc(2 * sizeof(int));
356 params->freqs[0] = wpa_s->wps_freq;
357 wpa_s->known_wps_freq = 0; /* only do this once */
359 #endif /* CONFIG_WPS */
363 #ifdef CONFIG_INTERWORKING
364 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
367 if (wpa_s->conf->interworking == 0)
370 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
371 wpabuf_put_u8(buf, 4);
372 wpabuf_put_u8(buf, 0x00);
373 wpabuf_put_u8(buf, 0x00);
374 wpabuf_put_u8(buf, 0x00);
375 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
377 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
378 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
380 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
382 if (!is_zero_ether_addr(wpa_s->conf->hessid))
383 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
385 #endif /* CONFIG_INTERWORKING */
388 static struct wpabuf *
389 wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
390 struct wpa_driver_scan_params *params)
392 struct wpabuf *extra_ie = NULL;
395 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
396 #endif /* CONFIG_WPS */
398 #ifdef CONFIG_INTERWORKING
399 if (wpa_s->conf->interworking &&
400 wpabuf_resize(&extra_ie, 100) == 0)
401 wpas_add_interworking_elements(wpa_s, extra_ie);
402 #endif /* CONFIG_INTERWORKING */
405 wps = wpas_wps_in_use(wpa_s, &req_type);
408 struct wpabuf *wps_ie;
409 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
412 wpa_s->wps->uuid, req_type,
415 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
416 wpabuf_put_buf(extra_ie, wps_ie);
423 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
424 if (wpabuf_resize(&extra_ie, ielen) == 0)
425 wpas_p2p_scan_ie(wpa_s, extra_ie);
427 #endif /* CONFIG_P2P */
429 #endif /* CONFIG_WPS */
435 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
437 struct wpa_supplicant *wpa_s = eloop_ctx;
438 struct wpa_ssid *ssid;
439 int scan_req = 0, ret;
440 struct wpabuf *extra_ie = NULL;
441 struct wpa_driver_scan_params params;
442 struct wpa_driver_scan_params *scan_params;
444 enum wpa_states prev_state;
446 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
447 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
451 if (wpa_s->disconnected && !wpa_s->scan_req) {
452 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
456 if (!wpa_supplicant_enabled_networks(wpa_s) &&
458 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
459 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
463 if (wpa_s->conf->ap_scan != 0 &&
464 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
465 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
466 "overriding ap_scan configuration");
467 wpa_s->conf->ap_scan = 0;
468 wpas_notify_ap_scan_changed(wpa_s);
471 if (wpa_s->conf->ap_scan == 0) {
472 wpa_supplicant_gen_assoc_event(wpa_s);
477 if (wpas_p2p_in_progress(wpa_s)) {
478 if (wpa_s->wpa_state == WPA_SCANNING) {
479 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
480 "while P2P operation is in progress");
481 wpa_supplicant_req_scan(wpa_s, 5, 0);
483 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
484 "P2P operation is in progress");
488 #endif /* CONFIG_P2P */
490 if (wpa_s->conf->ap_scan == 2)
493 max_ssids = wpa_s->max_scan_ssids;
494 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
495 max_ssids = WPAS_MAX_SCAN_SSIDS;
498 scan_req = wpa_s->scan_req;
501 os_memset(¶ms, 0, sizeof(params));
503 prev_state = wpa_s->wpa_state;
504 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
505 wpa_s->wpa_state == WPA_INACTIVE)
506 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
509 * If autoscan has set its own scanning parameters
511 if (wpa_s->autoscan_params != NULL) {
512 scan_params = wpa_s->autoscan_params;
516 if (scan_req != 2 && wpa_s->connect_without_scan) {
517 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
518 if (ssid == wpa_s->connect_without_scan)
521 wpa_s->connect_without_scan = NULL;
523 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
524 "without scan step");
525 wpa_supplicant_associate(wpa_s, NULL, ssid);
531 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
533 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
534 "P2P group formation");
535 params.ssids[0].ssid = wpa_s->go_params->ssid;
536 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
537 params.num_ssids = 1;
540 #endif /* CONFIG_P2P */
542 /* Find the starting point from which to continue scanning */
543 ssid = wpa_s->conf->ssid;
544 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
546 if (ssid == wpa_s->prev_scan_ssid) {
554 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
555 wpa_s->connect_without_scan = NULL;
556 wpa_s->prev_scan_wildcard = 0;
557 wpa_supplicant_assoc_try(wpa_s, ssid);
560 } else if (wpa_s->conf->ap_scan == 2) {
562 * User-initiated scan request in ap_scan == 2; scan with
568 struct wpa_ssid *start = ssid, *tssid;
570 if (ssid == NULL && max_ssids > 1)
571 ssid = wpa_s->conf->ssid;
573 if (!wpas_network_disabled(wpa_s, ssid) &&
575 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
576 ssid->ssid, ssid->ssid_len);
577 params.ssids[params.num_ssids].ssid =
579 params.ssids[params.num_ssids].ssid_len =
582 if (params.num_ssids + 1 >= max_ssids)
588 if (ssid == NULL && max_ssids > 1 &&
589 start != wpa_s->conf->ssid)
590 ssid = wpa_s->conf->ssid;
593 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
594 if (wpas_network_disabled(wpa_s, tssid))
596 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
597 int_array_concat(¶ms.freqs,
600 os_free(params.freqs);
605 int_array_sort_unique(params.freqs);
608 if (ssid && max_ssids == 1) {
610 * If the driver is limited to 1 SSID at a time interleave
611 * wildcard SSID scans with specific SSID scans to avoid
612 * waiting a long time for a wildcard scan.
614 if (!wpa_s->prev_scan_wildcard) {
615 params.ssids[0].ssid = NULL;
616 params.ssids[0].ssid_len = 0;
617 wpa_s->prev_scan_wildcard = 1;
618 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
619 "wildcard SSID (Interleave with specific)");
621 wpa_s->prev_scan_ssid = ssid;
622 wpa_s->prev_scan_wildcard = 0;
623 wpa_dbg(wpa_s, MSG_DEBUG,
624 "Starting AP scan for specific SSID: %s",
625 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
630 wpa_s->prev_scan_ssid = ssid;
631 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
635 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
637 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
642 #endif /* CONFIG_P2P */
644 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
645 extra_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
648 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
649 wpas_hs20_add_indication(extra_ie);
650 #endif /* CONFIG_HS20 */
652 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
653 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
654 "generated frequency list");
655 params.freqs = wpa_s->next_scan_freqs;
657 os_free(wpa_s->next_scan_freqs);
658 wpa_s->next_scan_freqs = NULL;
660 params.filter_ssids = wpa_supplicant_build_filter_ssids(
661 wpa_s->conf, ¶ms.num_filter_ssids);
663 params.extra_ies = wpabuf_head(extra_ie);
664 params.extra_ies_len = wpabuf_len(extra_ie);
668 if (wpa_s->p2p_in_provisioning ||
669 (wpa_s->show_group_started && wpa_s->go_params)) {
671 * The interface may not yet be in P2P mode, so we have to
672 * explicitly request P2P probe to disable CCK rates.
674 params.p2p_probe = 1;
676 #endif /* CONFIG_P2P */
678 scan_params = ¶ms;
681 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
683 wpabuf_free(extra_ie);
684 os_free(params.freqs);
685 os_free(params.filter_ssids);
688 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
689 if (prev_state != wpa_s->wpa_state)
690 wpa_supplicant_set_state(wpa_s, prev_state);
691 wpa_supplicant_req_scan(wpa_s, 1, 0);
697 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
698 * @wpa_s: Pointer to wpa_supplicant data
699 * @sec: Number of seconds after which to scan
700 * @usec: Number of microseconds after which to scan
702 * This function is used to schedule a scan for neighboring access points after
703 * the specified time.
705 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
708 /* If there's at least one network that should be specifically scanned
709 * then don't cancel the scan and reschedule. Some drivers do
710 * background scanning which generates frequent scan results, and that
711 * causes the specific SSID scan to get continually pushed back and
712 * never happen, which causes hidden APs to never get probe-scanned.
714 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
715 wpa_s->conf->ap_scan == 1) {
716 struct wpa_ssid *ssid = wpa_s->conf->ssid;
719 if (!wpas_network_disabled(wpa_s, ssid) &&
725 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
726 "ensure that specific SSID scans occur");
731 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
733 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
734 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
739 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
740 * @wpa_s: Pointer to wpa_supplicant data
741 * @sec: Number of seconds after which to scan
742 * @usec: Number of microseconds after which to scan
744 * This function is used to schedule periodic scans for neighboring
745 * access points after the specified time.
747 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
750 if (!wpa_s->sched_scan_supported)
753 eloop_register_timeout(sec, usec,
754 wpa_supplicant_delayed_sched_scan_timeout,
762 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
763 * @wpa_s: Pointer to wpa_supplicant data
765 * This function is used to schedule periodic scans for neighboring
766 * access points repeating the scan continuously.
768 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
770 struct wpa_driver_scan_params params;
771 struct wpa_driver_scan_params *scan_params;
772 enum wpa_states prev_state;
773 struct wpa_ssid *ssid = NULL;
774 struct wpabuf *wps_ie = NULL;
776 unsigned int max_sched_scan_ssids;
780 if (!wpa_s->sched_scan_supported)
783 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
784 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
786 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
787 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
790 if (wpa_s->sched_scanning) {
791 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
796 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
797 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
798 /* Use wildcard SSID to find this network */
800 } else if (!wpas_network_disabled(wpa_s, ssid) &&
805 if (!wpas_network_disabled(wpa_s, ssid) &&
806 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
808 * Normal scan is more reliable and faster for WPS
809 * operations and since these are for short periods of
810 * time, the benefit of trying to use sched_scan would
813 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
814 "sched_scan for WPS");
817 #endif /* CONFIG_WPS */
822 if (wpa_s->normal_scans < 3 &&
823 (need_ssids <= wpa_s->max_scan_ssids ||
824 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
826 * When normal scan can speed up operations, use that for the
827 * first operations before starting the sched_scan to allow
828 * user space sleep more. We do this only if the normal scan
829 * has functionality that is suitable for this or if the
830 * sched_scan does not have better support for multiple SSIDs.
832 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
833 "sched_scan for initial scans (normal_scans=%d)",
834 wpa_s->normal_scans);
838 os_memset(¶ms, 0, sizeof(params));
840 /* If we can't allocate space for the filters, we just don't filter */
841 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
842 sizeof(struct wpa_driver_scan_filter));
844 prev_state = wpa_s->wpa_state;
846 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
847 wpa_s->wpa_state == WPA_INACTIVE)
848 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
851 if (wpa_s->autoscan_params != NULL) {
852 scan_params = wpa_s->autoscan_params;
856 /* Find the starting point from which to continue scanning */
857 ssid = wpa_s->conf->ssid;
858 if (wpa_s->prev_sched_ssid) {
860 if (ssid == wpa_s->prev_sched_ssid) {
868 if (!ssid || !wpa_s->prev_sched_ssid) {
869 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
871 if (wpa_s->sched_scan_interval == 0)
872 wpa_s->sched_scan_interval = 10;
873 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
874 wpa_s->first_sched_scan = 1;
875 ssid = wpa_s->conf->ssid;
876 wpa_s->prev_sched_ssid = ssid;
880 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
885 if (wpas_network_disabled(wpa_s, ssid))
888 if (params.num_filter_ssids < wpa_s->max_match_sets &&
889 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
890 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
891 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
892 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
893 ssid->ssid, ssid->ssid_len);
894 params.filter_ssids[params.num_filter_ssids].ssid_len =
896 params.num_filter_ssids++;
897 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
899 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
900 "filter for sched_scan - drop filter");
901 os_free(params.filter_ssids);
902 params.filter_ssids = NULL;
903 params.num_filter_ssids = 0;
906 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
907 if (params.num_ssids == max_sched_scan_ssids)
908 break; /* only room for broadcast SSID */
909 wpa_dbg(wpa_s, MSG_DEBUG,
910 "add to active scan ssid: %s",
911 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
912 params.ssids[params.num_ssids].ssid =
914 params.ssids[params.num_ssids].ssid_len =
917 if (params.num_ssids >= max_sched_scan_ssids) {
918 wpa_s->prev_sched_ssid = ssid;
922 (wpas_network_disabled(wpa_s, ssid) ||
929 wpa_s->prev_sched_ssid = ssid;
933 if (params.num_filter_ssids == 0) {
934 os_free(params.filter_ssids);
935 params.filter_ssids = NULL;
939 wps_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
941 scan_params = ¶ms;
944 if (ssid || !wpa_s->first_sched_scan) {
945 wpa_dbg(wpa_s, MSG_DEBUG,
946 "Starting sched scan: interval %d timeout %d",
947 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
949 wpa_dbg(wpa_s, MSG_DEBUG,
950 "Starting sched scan: interval %d (no timeout)",
951 wpa_s->sched_scan_interval);
954 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
955 wpa_s->sched_scan_interval);
957 os_free(params.filter_ssids);
959 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
960 if (prev_state != wpa_s->wpa_state)
961 wpa_supplicant_set_state(wpa_s, prev_state);
965 /* If we have more SSIDs to scan, add a timeout so we scan them too */
966 if (ssid || !wpa_s->first_sched_scan) {
967 wpa_s->sched_scan_timed_out = 0;
968 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
969 wpa_supplicant_sched_scan_timeout,
971 wpa_s->first_sched_scan = 0;
972 wpa_s->sched_scan_timeout /= 2;
973 wpa_s->sched_scan_interval *= 2;
981 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
982 * @wpa_s: Pointer to wpa_supplicant data
984 * This function is used to cancel a scan request scheduled with
985 * wpa_supplicant_req_scan().
987 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
989 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
990 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
992 wpa_supplicant_notify_scanning(wpa_s, 0);
998 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
999 * @wpa_s: Pointer to wpa_supplicant data
1001 * This function is used to stop a periodic scheduled scan.
1003 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1005 if (!wpa_s->sched_scanning)
1008 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1009 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1010 wpa_supplicant_stop_sched_scan(wpa_s);
1014 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1017 if (wpa_s->scanning != scanning) {
1019 if(!wpa_s->sched_scanning)
1020 wpa_s->scanning = scanning;
1022 wpa_s->scanning = scanning;
1024 wpas_notify_scanning(wpa_s);
1029 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1035 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1036 for (i = 0; ie && i < ie[1]; i++) {
1037 if ((ie[i + 2] & 0x7f) > rate)
1038 rate = ie[i + 2] & 0x7f;
1041 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1042 for (i = 0; ie && i < ie[1]; i++) {
1043 if ((ie[i + 2] & 0x7f) > rate)
1044 rate = ie[i + 2] & 0x7f;
1051 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1053 const u8 *end, *pos;
1055 pos = (const u8 *) (res + 1);
1056 end = pos + res->ie_len;
1058 while (pos + 1 < end) {
1059 if (pos + 2 + pos[1] > end)
1070 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1073 const u8 *end, *pos;
1075 pos = (const u8 *) (res + 1);
1076 end = pos + res->ie_len;
1078 while (pos + 1 < end) {
1079 if (pos + 2 + pos[1] > end)
1081 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1082 vendor_type == WPA_GET_BE32(&pos[2]))
1091 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1095 const u8 *end, *pos;
1097 buf = wpabuf_alloc(res->ie_len);
1101 pos = (const u8 *) (res + 1);
1102 end = pos + res->ie_len;
1104 while (pos + 1 < end) {
1105 if (pos + 2 + pos[1] > end)
1107 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1108 vendor_type == WPA_GET_BE32(&pos[2]))
1109 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1113 if (wpabuf_len(buf) == 0) {
1122 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1123 const struct wpa_scan_res *res, u32 vendor_type)
1126 const u8 *end, *pos;
1128 if (res->beacon_ie_len == 0)
1130 buf = wpabuf_alloc(res->beacon_ie_len);
1134 pos = (const u8 *) (res + 1);
1136 end = pos + res->beacon_ie_len;
1138 while (pos + 1 < end) {
1139 if (pos + 2 + pos[1] > end)
1141 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1142 vendor_type == WPA_GET_BE32(&pos[2]))
1143 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1147 if (wpabuf_len(buf) == 0) {
1157 * Channels with a great SNR can operate at full rate. What is a great SNR?
1158 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1159 * rule of thumb is that any SNR above 20 is good." This one
1160 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1161 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1162 * conservative value.
1164 #define GREAT_SNR 30
1166 /* Compare function for sorting scan results. Return >0 if @b is considered
1168 static int wpa_scan_result_compar(const void *a, const void *b)
1170 #define IS_5GHZ(n) (n > 4000)
1171 #define MIN(a,b) a < b ? a : b
1172 struct wpa_scan_res **_wa = (void *) a;
1173 struct wpa_scan_res **_wb = (void *) b;
1174 struct wpa_scan_res *wa = *_wa;
1175 struct wpa_scan_res *wb = *_wb;
1176 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1179 /* WPA/WPA2 support preferred */
1180 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1181 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1182 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1183 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1185 if (wpa_b && !wpa_a)
1187 if (!wpa_b && wpa_a)
1190 /* privacy support preferred */
1191 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1192 (wb->caps & IEEE80211_CAP_PRIVACY))
1194 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1195 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1198 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1199 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1200 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1201 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1203 /* Not suitable information to calculate SNR, so use level */
1208 /* best/max rate preferred if SNR close enough */
1209 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1210 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1211 maxrate_a = wpa_scan_get_max_rate(wa);
1212 maxrate_b = wpa_scan_get_max_rate(wb);
1213 if (maxrate_a != maxrate_b)
1214 return maxrate_b - maxrate_a;
1215 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1216 return IS_5GHZ(wa->freq) ? -1 : 1;
1219 /* use freq for channel preference */
1221 /* all things being equal, use SNR; if SNRs are
1222 * identical, use quality values since some drivers may only report
1223 * that value and leave the signal level zero */
1225 return wb->qual - wa->qual;
1226 return snr_b - snr_a;
1233 /* Compare function for sorting scan results when searching a WPS AP for
1234 * provisioning. Return >0 if @b is considered better. */
1235 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1237 struct wpa_scan_res **_wa = (void *) a;
1238 struct wpa_scan_res **_wb = (void *) b;
1239 struct wpa_scan_res *wa = *_wa;
1240 struct wpa_scan_res *wb = *_wb;
1241 int uses_wps_a, uses_wps_b;
1242 struct wpabuf *wps_a, *wps_b;
1245 /* Optimization - check WPS IE existence before allocated memory and
1246 * doing full reassembly. */
1247 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1248 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1249 if (uses_wps_a && !uses_wps_b)
1251 if (!uses_wps_a && uses_wps_b)
1254 if (uses_wps_a && uses_wps_b) {
1255 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1256 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1257 res = wps_ap_priority_compar(wps_a, wps_b);
1265 * Do not use current AP security policy as a sorting criteria during
1266 * WPS provisioning step since the AP may get reconfigured at the
1267 * completion of provisioning.
1270 /* all things being equal, use signal level; if signal levels are
1271 * identical, use quality values since some drivers may only report
1272 * that value and leave the signal level zero */
1273 if (wb->level == wa->level)
1274 return wb->qual - wa->qual;
1275 return wb->level - wa->level;
1277 #endif /* CONFIG_WPS */
1280 static void dump_scan_res(struct wpa_scan_results *scan_res)
1282 #ifndef CONFIG_NO_STDOUT_DEBUG
1285 if (scan_res->res == NULL || scan_res->num == 0)
1288 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1290 for (i = 0; i < scan_res->num; i++) {
1291 struct wpa_scan_res *r = scan_res->res[i];
1293 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1294 == WPA_SCAN_LEVEL_DBM) {
1295 int snr = r->level - r->noise;
1296 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1297 "noise=%d level=%d snr=%d%s flags=0x%x",
1298 MAC2STR(r->bssid), r->freq, r->qual,
1299 r->noise, r->level, snr,
1300 snr >= GREAT_SNR ? "*" : "", r->flags);
1302 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1303 "noise=%d level=%d flags=0x%x",
1304 MAC2STR(r->bssid), r->freq, r->qual,
1305 r->noise, r->level, r->flags);
1307 pos = (u8 *) (r + 1);
1309 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1311 if (r->beacon_ie_len)
1312 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1313 pos, r->beacon_ie_len);
1315 #endif /* CONFIG_NO_STDOUT_DEBUG */
1319 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1324 if (wpa_s->bssid_filter == NULL)
1327 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1328 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1337 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1338 struct wpa_scan_results *res)
1342 if (wpa_s->bssid_filter == NULL)
1345 for (i = 0, j = 0; i < res->num; i++) {
1346 if (wpa_supplicant_filter_bssid_match(wpa_s,
1347 res->res[i]->bssid)) {
1348 res->res[j++] = res->res[i];
1350 os_free(res->res[i]);
1355 if (res->num != j) {
1356 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1357 (int) (res->num - j));
1364 * wpa_supplicant_get_scan_results - Get scan results
1365 * @wpa_s: Pointer to wpa_supplicant data
1366 * @info: Information about what was scanned or %NULL if not available
1367 * @new_scan: Whether a new scan was performed
1368 * Returns: Scan results, %NULL on failure
1370 * This function request the current scan results from the driver and updates
1371 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1372 * results with wpa_scan_results_free().
1374 struct wpa_scan_results *
1375 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1376 struct scan_info *info, int new_scan)
1378 struct wpa_scan_results *scan_res;
1380 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1382 scan_res = wpa_drv_get_scan_results2(wpa_s);
1383 if (scan_res == NULL) {
1384 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1387 filter_scan_res(wpa_s, scan_res);
1390 if (wpas_wps_in_progress(wpa_s)) {
1391 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1392 "provisioning rules");
1393 compar = wpa_scan_result_wps_compar;
1395 #endif /* CONFIG_WPS */
1397 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1399 dump_scan_res(scan_res);
1401 wpa_bss_update_start(wpa_s);
1402 for (i = 0; i < scan_res->num; i++)
1403 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1404 wpa_bss_update_end(wpa_s, info, new_scan);
1410 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1412 struct wpa_scan_results *scan_res;
1413 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1414 if (scan_res == NULL)
1416 wpa_scan_results_free(scan_res);