2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
21 #include "wpa_supplicant_i.h"
23 #include "wps_supplicant.h"
24 #include "p2p_supplicant.h"
31 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
33 struct wpa_ssid *ssid;
34 union wpa_event_data data;
36 ssid = wpa_supplicant_get_ssid(wpa_s);
40 if (wpa_s->current_ssid == NULL) {
41 wpa_s->current_ssid = ssid;
42 if (wpa_s->current_ssid != NULL)
43 wpas_notify_network_changed(wpa_s);
45 wpa_supplicant_initiate_eapol(wpa_s);
46 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
47 "network - generating associated event");
48 os_memset(&data, 0, sizeof(data));
49 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
54 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
55 enum wps_request_type *req_type)
57 struct wpa_ssid *ssid;
60 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
61 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
65 *req_type = wpas_wps_get_req_type(ssid);
66 if (!ssid->eap.phase1)
69 if (os_strstr(ssid->eap.phase1, "pbc=1"))
74 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) {
75 wpa_s->wps->dev.p2p = 1;
78 *req_type = WPS_REQ_ENROLLEE_INFO;
81 #endif /* CONFIG_P2P */
85 #endif /* CONFIG_WPS */
88 int wpa_supplicant_enabled_networks(struct wpa_config *conf)
90 struct wpa_ssid *ssid = conf->ssid;
101 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
102 struct wpa_ssid *ssid)
110 /* ap_scan=2 mode - try to associate with each SSID. */
112 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
113 "end of scan list - go back to beginning");
114 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
115 wpa_supplicant_req_scan(wpa_s, 0, 0);
119 /* Continue from the next SSID on the next attempt. */
120 wpa_s->prev_scan_ssid = ssid;
122 /* Start from the beginning of the SSID list. */
123 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
125 wpa_supplicant_associate(wpa_s, NULL, ssid);
129 static int int_array_len(const int *a)
132 for (i = 0; a && a[i]; i++)
138 static void int_array_concat(int **res, const int *a)
143 reslen = int_array_len(*res);
144 alen = int_array_len(a);
146 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
152 for (i = 0; i <= alen; i++)
153 n[reslen + i] = a[i];
158 static int freq_cmp(const void *a, const void *b)
171 static void int_array_sort_unique(int *a)
179 alen = int_array_len(a);
180 qsort(a, alen, sizeof(int), freq_cmp);
184 while (a[i] && a[j]) {
197 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
198 struct wpa_driver_scan_params *params)
202 wpa_supplicant_notify_scanning(wpa_s, 1);
204 ret = wpa_drv_scan(wpa_s, params);
206 wpa_supplicant_notify_scanning(wpa_s, 0);
207 wpas_notify_scan_done(wpa_s, 0);
210 wpa_s->normal_scans++;
218 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
220 struct wpa_supplicant *wpa_s = eloop_ctx;
222 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
224 if (wpa_supplicant_req_sched_scan(wpa_s))
225 wpa_supplicant_req_scan(wpa_s, 0, 0);
230 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
232 struct wpa_supplicant *wpa_s = eloop_ctx;
234 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
236 wpa_s->sched_scan_timed_out = 1;
237 wpa_supplicant_cancel_sched_scan(wpa_s);
242 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
243 struct wpa_driver_scan_params *params,
248 wpa_supplicant_notify_scanning(wpa_s, 1);
249 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
251 wpa_supplicant_notify_scanning(wpa_s, 0);
253 wpa_s->sched_scanning = 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, &wpa_s->wps->dev,
410 wpa_s->wps->uuid, req_type,
413 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
414 wpabuf_put_buf(extra_ie, wps_ie);
421 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
422 if (wpabuf_resize(&extra_ie, ielen) == 0)
423 wpas_p2p_scan_ie(wpa_s, extra_ie);
425 #endif /* CONFIG_P2P */
427 #endif /* CONFIG_WPS */
433 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
435 struct wpa_supplicant *wpa_s = eloop_ctx;
436 struct wpa_ssid *ssid;
437 int scan_req = 0, ret;
438 struct wpabuf *extra_ie;
439 struct wpa_driver_scan_params params;
441 enum wpa_states prev_state;
443 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
444 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
448 if (wpa_s->disconnected && !wpa_s->scan_req) {
449 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
453 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
455 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
456 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
460 if (wpa_s->conf->ap_scan != 0 &&
461 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
462 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
463 "overriding ap_scan configuration");
464 wpa_s->conf->ap_scan = 0;
465 wpas_notify_ap_scan_changed(wpa_s);
468 if (wpa_s->conf->ap_scan == 0) {
469 wpa_supplicant_gen_assoc_event(wpa_s);
474 if (wpas_p2p_in_progress(wpa_s)) {
475 if (wpa_s->wpa_state == WPA_SCANNING) {
476 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
477 "while P2P operation is in progress");
478 wpa_supplicant_req_scan(wpa_s, 5, 0);
480 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
481 "P2P operation is in progress");
485 #endif /* CONFIG_P2P */
487 if (wpa_s->conf->ap_scan == 2)
490 max_ssids = wpa_s->max_scan_ssids;
491 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
492 max_ssids = WPAS_MAX_SCAN_SSIDS;
495 scan_req = wpa_s->scan_req;
498 os_memset(¶ms, 0, sizeof(params));
500 prev_state = wpa_s->wpa_state;
501 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
502 wpa_s->wpa_state == WPA_INACTIVE)
503 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
505 if (scan_req != 2 && wpa_s->connect_without_scan) {
506 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
507 if (ssid == wpa_s->connect_without_scan)
510 wpa_s->connect_without_scan = NULL;
512 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
513 "without scan step");
514 wpa_supplicant_associate(wpa_s, NULL, ssid);
519 /* Find the starting point from which to continue scanning */
520 ssid = wpa_s->conf->ssid;
521 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
523 if (ssid == wpa_s->prev_scan_ssid) {
531 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
532 wpa_s->connect_without_scan = NULL;
533 wpa_s->prev_scan_wildcard = 0;
534 wpa_supplicant_assoc_try(wpa_s, ssid);
536 } else if (wpa_s->conf->ap_scan == 2) {
538 * User-initiated scan request in ap_scan == 2; scan with
543 struct wpa_ssid *start = ssid, *tssid;
545 if (ssid == NULL && max_ssids > 1)
546 ssid = wpa_s->conf->ssid;
548 if (!ssid->disabled && ssid->scan_ssid) {
549 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
550 ssid->ssid, ssid->ssid_len);
551 params.ssids[params.num_ssids].ssid =
553 params.ssids[params.num_ssids].ssid_len =
556 if (params.num_ssids + 1 >= max_ssids)
562 if (ssid == NULL && max_ssids > 1 &&
563 start != wpa_s->conf->ssid)
564 ssid = wpa_s->conf->ssid;
567 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
570 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
571 int_array_concat(¶ms.freqs,
574 os_free(params.freqs);
579 int_array_sort_unique(params.freqs);
582 if (ssid && max_ssids == 1) {
584 * If the driver is limited to 1 SSID at a time interleave
585 * wildcard SSID scans with specific SSID scans to avoid
586 * waiting a long time for a wildcard scan.
588 if (!wpa_s->prev_scan_wildcard) {
589 params.ssids[0].ssid = NULL;
590 params.ssids[0].ssid_len = 0;
591 wpa_s->prev_scan_wildcard = 1;
592 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
593 "wildcard SSID (Interleave with specific)");
595 wpa_s->prev_scan_ssid = ssid;
596 wpa_s->prev_scan_wildcard = 0;
597 wpa_dbg(wpa_s, MSG_DEBUG,
598 "Starting AP scan for specific SSID: %s",
599 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
604 wpa_s->prev_scan_ssid = ssid;
605 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
609 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
611 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
615 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
616 extra_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
618 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
619 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
620 "generated frequency list");
621 params.freqs = wpa_s->next_scan_freqs;
623 os_free(wpa_s->next_scan_freqs);
624 wpa_s->next_scan_freqs = NULL;
626 params.filter_ssids = wpa_supplicant_build_filter_ssids(
627 wpa_s->conf, ¶ms.num_filter_ssids);
629 params.extra_ies = wpabuf_head(extra_ie);
630 params.extra_ies_len = wpabuf_len(extra_ie);
634 if (wpa_s->p2p_in_provisioning) {
636 * The interface may not yet be in P2P mode, so we have to
637 * explicitly request P2P probe to disable CCK rates.
639 params.p2p_probe = 1;
641 #endif /* CONFIG_P2P */
643 ret = wpa_supplicant_trigger_scan(wpa_s, ¶ms);
645 wpabuf_free(extra_ie);
646 os_free(params.freqs);
647 os_free(params.filter_ssids);
650 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
651 if (prev_state != wpa_s->wpa_state)
652 wpa_supplicant_set_state(wpa_s, prev_state);
653 wpa_supplicant_req_scan(wpa_s, 1, 0);
659 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
660 * @wpa_s: Pointer to wpa_supplicant data
661 * @sec: Number of seconds after which to scan
662 * @usec: Number of microseconds after which to scan
664 * This function is used to schedule a scan for neighboring access points after
665 * the specified time.
667 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
669 /* If there's at least one network that should be specifically scanned
670 * then don't cancel the scan and reschedule. Some drivers do
671 * background scanning which generates frequent scan results, and that
672 * causes the specific SSID scan to get continually pushed back and
673 * never happen, which causes hidden APs to never get probe-scanned.
675 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
676 wpa_s->conf->ap_scan == 1) {
677 struct wpa_ssid *ssid = wpa_s->conf->ssid;
680 if (!ssid->disabled && ssid->scan_ssid)
685 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
686 "ensure that specific SSID scans occur");
691 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
693 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
694 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
699 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
700 * @wpa_s: Pointer to wpa_supplicant data
701 * @sec: Number of seconds after which to scan
702 * @usec: Number of microseconds after which to scan
704 * This function is used to schedule periodic scans for neighboring
705 * access points after the specified time.
707 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
710 if (!wpa_s->sched_scan_supported)
713 eloop_register_timeout(sec, usec,
714 wpa_supplicant_delayed_sched_scan_timeout,
722 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
723 * @wpa_s: Pointer to wpa_supplicant data
725 * This function is used to schedule periodic scans for neighboring
726 * access points repeating the scan continuously.
728 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
730 struct wpa_driver_scan_params params;
731 enum wpa_states prev_state;
732 struct wpa_ssid *ssid;
733 struct wpabuf *wps_ie = NULL;
735 unsigned int max_sched_scan_ssids;
739 if (!wpa_s->sched_scan_supported)
742 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
743 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
745 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
746 if (max_sched_scan_ssids < 1)
749 if (wpa_s->sched_scanning) {
750 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
755 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
756 if (!ssid->disabled && !ssid->scan_ssid) {
757 /* Use wildcard SSID to find this network */
759 } else if (!ssid->disabled && ssid->ssid_len)
765 if (wpa_s->normal_scans < 3 &&
766 (need_ssids <= wpa_s->max_scan_ssids ||
767 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
769 * When normal scan can speed up operations, use that for the
770 * first operations before starting the sched_scan to allow
771 * user space sleep more. We do this only if the normal scan
772 * has functionality that is suitable for this or if the
773 * sched_scan does not have better support for multiple SSIDs.
775 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
776 "sched_scan for initial scans (normal_scans=%d)",
777 wpa_s->normal_scans);
781 os_memset(¶ms, 0, sizeof(params));
783 /* If we can't allocate space for the filters, we just don't filter */
784 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
785 sizeof(struct wpa_driver_scan_filter));
787 prev_state = wpa_s->wpa_state;
788 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
789 wpa_s->wpa_state == WPA_INACTIVE)
790 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
792 /* Find the starting point from which to continue scanning */
793 ssid = wpa_s->conf->ssid;
794 if (wpa_s->prev_sched_ssid) {
796 if (ssid == wpa_s->prev_sched_ssid) {
804 if (!ssid || !wpa_s->prev_sched_ssid) {
805 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
807 wpa_s->sched_scan_interval = 10;
808 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
809 wpa_s->first_sched_scan = 1;
810 ssid = wpa_s->conf->ssid;
811 wpa_s->prev_sched_ssid = ssid;
815 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
823 if (params.num_filter_ssids < wpa_s->max_match_sets &&
824 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
825 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
826 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
827 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
828 ssid->ssid, ssid->ssid_len);
829 params.filter_ssids[params.num_filter_ssids].ssid_len =
831 params.num_filter_ssids++;
832 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
834 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
835 "filter for sched_scan - drop filter");
836 os_free(params.filter_ssids);
837 params.filter_ssids = NULL;
838 params.num_filter_ssids = 0;
841 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
842 if (params.num_ssids == max_sched_scan_ssids)
843 break; /* only room for broadcast SSID */
844 wpa_dbg(wpa_s, MSG_DEBUG,
845 "add to active scan ssid: %s",
846 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
847 params.ssids[params.num_ssids].ssid =
849 params.ssids[params.num_ssids].ssid_len =
852 if (params.num_ssids >= max_sched_scan_ssids) {
853 wpa_s->prev_sched_ssid = ssid;
859 wpa_s->prev_sched_ssid = ssid;
863 if (params.num_filter_ssids == 0) {
864 os_free(params.filter_ssids);
865 params.filter_ssids = NULL;
869 wps_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
871 if (ssid || !wpa_s->first_sched_scan) {
872 wpa_dbg(wpa_s, MSG_DEBUG,
873 "Starting sched scan: interval %d (no timeout)",
874 wpa_s->sched_scan_interval);
876 wpa_dbg(wpa_s, MSG_DEBUG,
877 "Starting sched scan: interval %d timeout %d",
878 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
881 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms,
882 wpa_s->sched_scan_interval);
884 os_free(params.filter_ssids);
886 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
887 if (prev_state != wpa_s->wpa_state)
888 wpa_supplicant_set_state(wpa_s, prev_state);
892 /* If we have more SSIDs to scan, add a timeout so we scan them too */
893 if (ssid || !wpa_s->first_sched_scan) {
894 wpa_s->sched_scan_timed_out = 0;
895 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
896 wpa_supplicant_sched_scan_timeout,
898 wpa_s->first_sched_scan = 0;
899 wpa_s->sched_scan_timeout /= 2;
900 wpa_s->sched_scan_interval *= 2;
908 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
909 * @wpa_s: Pointer to wpa_supplicant data
911 * This function is used to cancel a scan request scheduled with
912 * wpa_supplicant_req_scan().
914 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
916 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
917 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
922 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
923 * @wpa_s: Pointer to wpa_supplicant data
925 * This function is used to stop a periodic scheduled scan.
927 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
929 if (!wpa_s->sched_scanning)
932 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
933 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
934 wpa_supplicant_stop_sched_scan(wpa_s);
938 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
941 if (wpa_s->scanning != scanning) {
942 wpa_s->scanning = scanning;
943 wpas_notify_scanning(wpa_s);
948 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
954 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
955 for (i = 0; ie && i < ie[1]; i++) {
956 if ((ie[i + 2] & 0x7f) > rate)
957 rate = ie[i + 2] & 0x7f;
960 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
961 for (i = 0; ie && i < ie[1]; i++) {
962 if ((ie[i + 2] & 0x7f) > rate)
963 rate = ie[i + 2] & 0x7f;
970 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
974 pos = (const u8 *) (res + 1);
975 end = pos + res->ie_len;
977 while (pos + 1 < end) {
978 if (pos + 2 + pos[1] > end)
989 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
994 pos = (const u8 *) (res + 1);
995 end = pos + res->ie_len;
997 while (pos + 1 < end) {
998 if (pos + 2 + pos[1] > end)
1000 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1001 vendor_type == WPA_GET_BE32(&pos[2]))
1010 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1014 const u8 *end, *pos;
1016 buf = wpabuf_alloc(res->ie_len);
1020 pos = (const u8 *) (res + 1);
1021 end = pos + res->ie_len;
1023 while (pos + 1 < end) {
1024 if (pos + 2 + pos[1] > end)
1026 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1027 vendor_type == WPA_GET_BE32(&pos[2]))
1028 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1032 if (wpabuf_len(buf) == 0) {
1041 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1042 const struct wpa_scan_res *res, u32 vendor_type)
1045 const u8 *end, *pos;
1047 if (res->beacon_ie_len == 0)
1049 buf = wpabuf_alloc(res->beacon_ie_len);
1053 pos = (const u8 *) (res + 1);
1055 end = pos + res->beacon_ie_len;
1057 while (pos + 1 < end) {
1058 if (pos + 2 + pos[1] > end)
1060 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1061 vendor_type == WPA_GET_BE32(&pos[2]))
1062 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1066 if (wpabuf_len(buf) == 0) {
1076 * Channels with a great SNR can operate at full rate. What is a great SNR?
1077 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1078 * rule of thumb is that any SNR above 20 is good." This one
1079 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1080 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1081 * conservative value.
1083 #define GREAT_SNR 30
1085 /* Compare function for sorting scan results. Return >0 if @b is considered
1087 static int wpa_scan_result_compar(const void *a, const void *b)
1089 #define IS_5GHZ(n) (n > 4000)
1090 #define MIN(a,b) a < b ? a : b
1091 struct wpa_scan_res **_wa = (void *) a;
1092 struct wpa_scan_res **_wb = (void *) b;
1093 struct wpa_scan_res *wa = *_wa;
1094 struct wpa_scan_res *wb = *_wb;
1095 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1098 /* WPA/WPA2 support preferred */
1099 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1100 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1101 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1102 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1104 if (wpa_b && !wpa_a)
1106 if (!wpa_b && wpa_a)
1109 /* privacy support preferred */
1110 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1111 (wb->caps & IEEE80211_CAP_PRIVACY))
1113 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1114 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1117 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1118 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1119 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1120 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1122 /* Not suitable information to calculate SNR, so use level */
1127 /* best/max rate preferred if SNR close enough */
1128 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1129 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1130 maxrate_a = wpa_scan_get_max_rate(wa);
1131 maxrate_b = wpa_scan_get_max_rate(wb);
1132 if (maxrate_a != maxrate_b)
1133 return maxrate_b - maxrate_a;
1134 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1135 return IS_5GHZ(wa->freq) ? -1 : 1;
1138 /* use freq for channel preference */
1140 /* all things being equal, use SNR; if SNRs are
1141 * identical, use quality values since some drivers may only report
1142 * that value and leave the signal level zero */
1144 return wb->qual - wa->qual;
1145 return snr_b - snr_a;
1152 /* Compare function for sorting scan results when searching a WPS AP for
1153 * provisioning. Return >0 if @b is considered better. */
1154 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1156 struct wpa_scan_res **_wa = (void *) a;
1157 struct wpa_scan_res **_wb = (void *) b;
1158 struct wpa_scan_res *wa = *_wa;
1159 struct wpa_scan_res *wb = *_wb;
1160 int uses_wps_a, uses_wps_b;
1161 struct wpabuf *wps_a, *wps_b;
1164 /* Optimization - check WPS IE existence before allocated memory and
1165 * doing full reassembly. */
1166 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1167 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1168 if (uses_wps_a && !uses_wps_b)
1170 if (!uses_wps_a && uses_wps_b)
1173 if (uses_wps_a && uses_wps_b) {
1174 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1175 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1176 res = wps_ap_priority_compar(wps_a, wps_b);
1184 * Do not use current AP security policy as a sorting criteria during
1185 * WPS provisioning step since the AP may get reconfigured at the
1186 * completion of provisioning.
1189 /* all things being equal, use signal level; if signal levels are
1190 * identical, use quality values since some drivers may only report
1191 * that value and leave the signal level zero */
1192 if (wb->level == wa->level)
1193 return wb->qual - wa->qual;
1194 return wb->level - wa->level;
1196 #endif /* CONFIG_WPS */
1199 static void dump_scan_res(struct wpa_scan_results *scan_res)
1201 #ifndef CONFIG_NO_STDOUT_DEBUG
1204 if (scan_res->res == NULL || scan_res->num == 0)
1207 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1209 for (i = 0; i < scan_res->num; i++) {
1210 struct wpa_scan_res *r = scan_res->res[i];
1211 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1212 == WPA_SCAN_LEVEL_DBM) {
1213 int snr = r->level - r->noise;
1214 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1215 "noise=%d level=%d snr=%d%s flags=0x%x",
1216 MAC2STR(r->bssid), r->freq, r->qual,
1217 r->noise, r->level, snr,
1218 snr >= GREAT_SNR ? "*" : "", r->flags);
1220 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1221 "noise=%d level=%d flags=0x%x",
1222 MAC2STR(r->bssid), r->freq, r->qual,
1223 r->noise, r->level, r->flags);
1226 #endif /* CONFIG_NO_STDOUT_DEBUG */
1231 * wpa_supplicant_get_scan_results - Get scan results
1232 * @wpa_s: Pointer to wpa_supplicant data
1233 * @info: Information about what was scanned or %NULL if not available
1234 * @new_scan: Whether a new scan was performed
1235 * Returns: Scan results, %NULL on failure
1237 * This function request the current scan results from the driver and updates
1238 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1239 * results with wpa_scan_results_free().
1241 struct wpa_scan_results *
1242 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1243 struct scan_info *info, int new_scan)
1245 struct wpa_scan_results *scan_res;
1247 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1249 scan_res = wpa_drv_get_scan_results2(wpa_s);
1250 if (scan_res == NULL) {
1251 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1256 if (wpas_wps_in_progress(wpa_s)) {
1257 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1258 "provisioning rules");
1259 compar = wpa_scan_result_wps_compar;
1261 #endif /* CONFIG_WPS */
1263 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1265 dump_scan_res(scan_res);
1267 wpa_bss_update_start(wpa_s);
1268 for (i = 0; i < scan_res->num; i++)
1269 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1270 wpa_bss_update_end(wpa_s, info, new_scan);
1276 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1278 struct wpa_scan_results *scan_res;
1279 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1280 if (scan_res == NULL)
1282 wpa_scan_results_free(scan_res);