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_supplicant_assoc_try(wpa_s, ssid);
535 } else if (wpa_s->conf->ap_scan == 2) {
537 * User-initiated scan request in ap_scan == 2; scan with
542 struct wpa_ssid *start = ssid, *tssid;
544 if (ssid == NULL && max_ssids > 1)
545 ssid = wpa_s->conf->ssid;
547 if (!ssid->disabled && ssid->scan_ssid) {
548 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
549 ssid->ssid, ssid->ssid_len);
550 params.ssids[params.num_ssids].ssid =
552 params.ssids[params.num_ssids].ssid_len =
555 if (params.num_ssids + 1 >= max_ssids)
561 if (ssid == NULL && max_ssids > 1 &&
562 start != wpa_s->conf->ssid)
563 ssid = wpa_s->conf->ssid;
566 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
569 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
570 int_array_concat(¶ms.freqs,
573 os_free(params.freqs);
578 int_array_sort_unique(params.freqs);
582 wpa_s->prev_scan_ssid = ssid;
584 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
588 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
591 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
593 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
597 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
598 extra_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
600 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
601 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
602 "generated frequency list");
603 params.freqs = wpa_s->next_scan_freqs;
605 os_free(wpa_s->next_scan_freqs);
606 wpa_s->next_scan_freqs = NULL;
608 params.filter_ssids = wpa_supplicant_build_filter_ssids(
609 wpa_s->conf, ¶ms.num_filter_ssids);
611 params.extra_ies = wpabuf_head(extra_ie);
612 params.extra_ies_len = wpabuf_len(extra_ie);
616 if (wpa_s->p2p_in_provisioning) {
618 * The interface may not yet be in P2P mode, so we have to
619 * explicitly request P2P probe to disable CCK rates.
621 params.p2p_probe = 1;
623 #endif /* CONFIG_P2P */
625 ret = wpa_supplicant_trigger_scan(wpa_s, ¶ms);
627 wpabuf_free(extra_ie);
628 os_free(params.freqs);
629 os_free(params.filter_ssids);
632 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
633 if (prev_state != wpa_s->wpa_state)
634 wpa_supplicant_set_state(wpa_s, prev_state);
635 wpa_supplicant_req_scan(wpa_s, 1, 0);
641 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
642 * @wpa_s: Pointer to wpa_supplicant data
643 * @sec: Number of seconds after which to scan
644 * @usec: Number of microseconds after which to scan
646 * This function is used to schedule a scan for neighboring access points after
647 * the specified time.
649 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
651 /* If there's at least one network that should be specifically scanned
652 * then don't cancel the scan and reschedule. Some drivers do
653 * background scanning which generates frequent scan results, and that
654 * causes the specific SSID scan to get continually pushed back and
655 * never happen, which causes hidden APs to never get probe-scanned.
657 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
658 wpa_s->conf->ap_scan == 1) {
659 struct wpa_ssid *ssid = wpa_s->conf->ssid;
662 if (!ssid->disabled && ssid->scan_ssid)
667 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
668 "ensure that specific SSID scans occur");
673 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
675 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
676 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
681 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
682 * @wpa_s: Pointer to wpa_supplicant data
683 * @sec: Number of seconds after which to scan
684 * @usec: Number of microseconds after which to scan
686 * This function is used to schedule periodic scans for neighboring
687 * access points after the specified time.
689 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
692 if (!wpa_s->sched_scan_supported)
695 eloop_register_timeout(sec, usec,
696 wpa_supplicant_delayed_sched_scan_timeout,
704 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
705 * @wpa_s: Pointer to wpa_supplicant data
707 * This function is used to schedule periodic scans for neighboring
708 * access points repeating the scan continuously.
710 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
712 struct wpa_driver_scan_params params;
713 enum wpa_states prev_state;
714 struct wpa_ssid *ssid;
715 struct wpabuf *wps_ie = NULL;
717 unsigned int max_sched_scan_ssids;
721 if (!wpa_s->sched_scan_supported)
724 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
725 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
727 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
728 if (max_sched_scan_ssids < 1)
731 if (wpa_s->sched_scanning) {
732 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
737 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
738 if (!ssid->disabled && !ssid->scan_ssid) {
739 /* Use wildcard SSID to find this network */
741 } else if (!ssid->disabled && ssid->ssid_len)
747 if (wpa_s->normal_scans < 3 &&
748 (need_ssids <= wpa_s->max_scan_ssids ||
749 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
751 * When normal scan can speed up operations, use that for the
752 * first operations before starting the sched_scan to allow
753 * user space sleep more. We do this only if the normal scan
754 * has functionality that is suitable for this or if the
755 * sched_scan does not have better support for multiple SSIDs.
757 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
758 "sched_scan for initial scans (normal_scans=%d)",
759 wpa_s->normal_scans);
763 os_memset(¶ms, 0, sizeof(params));
765 /* If we can't allocate space for the filters, we just don't filter */
766 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
767 sizeof(struct wpa_driver_scan_filter));
769 prev_state = wpa_s->wpa_state;
770 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
771 wpa_s->wpa_state == WPA_INACTIVE)
772 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
774 /* Find the starting point from which to continue scanning */
775 ssid = wpa_s->conf->ssid;
776 if (wpa_s->prev_sched_ssid) {
778 if (ssid == wpa_s->prev_sched_ssid) {
786 if (!ssid || !wpa_s->prev_sched_ssid) {
787 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
789 wpa_s->sched_scan_interval = 10;
790 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
791 wpa_s->first_sched_scan = 1;
792 ssid = wpa_s->conf->ssid;
793 wpa_s->prev_sched_ssid = ssid;
797 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
805 if (params.num_filter_ssids < wpa_s->max_match_sets &&
806 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
807 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
808 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
809 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
810 ssid->ssid, ssid->ssid_len);
811 params.filter_ssids[params.num_filter_ssids].ssid_len =
813 params.num_filter_ssids++;
814 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
816 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
817 "filter for sched_scan - drop filter");
818 os_free(params.filter_ssids);
819 params.filter_ssids = NULL;
820 params.num_filter_ssids = 0;
823 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
824 if (params.num_ssids == max_sched_scan_ssids)
825 break; /* only room for broadcast SSID */
826 wpa_dbg(wpa_s, MSG_DEBUG,
827 "add to active scan ssid: %s",
828 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
829 params.ssids[params.num_ssids].ssid =
831 params.ssids[params.num_ssids].ssid_len =
834 if (params.num_ssids >= max_sched_scan_ssids) {
835 wpa_s->prev_sched_ssid = ssid;
841 wpa_s->prev_sched_ssid = ssid;
845 if (params.num_filter_ssids == 0) {
846 os_free(params.filter_ssids);
847 params.filter_ssids = NULL;
851 wps_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
853 if (ssid || !wpa_s->first_sched_scan) {
854 wpa_dbg(wpa_s, MSG_DEBUG,
855 "Starting sched scan: interval %d (no timeout)",
856 wpa_s->sched_scan_interval);
858 wpa_dbg(wpa_s, MSG_DEBUG,
859 "Starting sched scan: interval %d timeout %d",
860 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
863 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms,
864 wpa_s->sched_scan_interval);
866 os_free(params.filter_ssids);
868 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
869 if (prev_state != wpa_s->wpa_state)
870 wpa_supplicant_set_state(wpa_s, prev_state);
874 /* If we have more SSIDs to scan, add a timeout so we scan them too */
875 if (ssid || !wpa_s->first_sched_scan) {
876 wpa_s->sched_scan_timed_out = 0;
877 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
878 wpa_supplicant_sched_scan_timeout,
880 wpa_s->first_sched_scan = 0;
881 wpa_s->sched_scan_timeout /= 2;
882 wpa_s->sched_scan_interval *= 2;
890 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
891 * @wpa_s: Pointer to wpa_supplicant data
893 * This function is used to cancel a scan request scheduled with
894 * wpa_supplicant_req_scan().
896 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
898 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
899 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
904 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
905 * @wpa_s: Pointer to wpa_supplicant data
907 * This function is used to stop a periodic scheduled scan.
909 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
911 if (!wpa_s->sched_scanning)
914 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
915 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
916 wpa_supplicant_stop_sched_scan(wpa_s);
920 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
923 if (wpa_s->scanning != scanning) {
924 wpa_s->scanning = scanning;
925 wpas_notify_scanning(wpa_s);
930 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
936 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
937 for (i = 0; ie && i < ie[1]; i++) {
938 if ((ie[i + 2] & 0x7f) > rate)
939 rate = ie[i + 2] & 0x7f;
942 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
943 for (i = 0; ie && i < ie[1]; i++) {
944 if ((ie[i + 2] & 0x7f) > rate)
945 rate = ie[i + 2] & 0x7f;
952 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
956 pos = (const u8 *) (res + 1);
957 end = pos + res->ie_len;
959 while (pos + 1 < end) {
960 if (pos + 2 + pos[1] > end)
971 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
976 pos = (const u8 *) (res + 1);
977 end = pos + res->ie_len;
979 while (pos + 1 < end) {
980 if (pos + 2 + pos[1] > end)
982 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
983 vendor_type == WPA_GET_BE32(&pos[2]))
992 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
998 buf = wpabuf_alloc(res->ie_len);
1002 pos = (const u8 *) (res + 1);
1003 end = pos + res->ie_len;
1005 while (pos + 1 < end) {
1006 if (pos + 2 + pos[1] > end)
1008 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1009 vendor_type == WPA_GET_BE32(&pos[2]))
1010 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1014 if (wpabuf_len(buf) == 0) {
1023 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1024 const struct wpa_scan_res *res, u32 vendor_type)
1027 const u8 *end, *pos;
1029 if (res->beacon_ie_len == 0)
1031 buf = wpabuf_alloc(res->beacon_ie_len);
1035 pos = (const u8 *) (res + 1);
1037 end = pos + res->beacon_ie_len;
1039 while (pos + 1 < end) {
1040 if (pos + 2 + pos[1] > end)
1042 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1043 vendor_type == WPA_GET_BE32(&pos[2]))
1044 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1048 if (wpabuf_len(buf) == 0) {
1058 * Channels with a great SNR can operate at full rate. What is a great SNR?
1059 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1060 * rule of thumb is that any SNR above 20 is good." This one
1061 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1062 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1063 * conservative value.
1065 #define GREAT_SNR 30
1067 /* Compare function for sorting scan results. Return >0 if @b is considered
1069 static int wpa_scan_result_compar(const void *a, const void *b)
1071 #define IS_5GHZ(n) (n > 4000)
1072 #define MIN(a,b) a < b ? a : b
1073 struct wpa_scan_res **_wa = (void *) a;
1074 struct wpa_scan_res **_wb = (void *) b;
1075 struct wpa_scan_res *wa = *_wa;
1076 struct wpa_scan_res *wb = *_wb;
1077 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1080 /* WPA/WPA2 support preferred */
1081 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1082 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1083 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1084 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1086 if (wpa_b && !wpa_a)
1088 if (!wpa_b && wpa_a)
1091 /* privacy support preferred */
1092 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1093 (wb->caps & IEEE80211_CAP_PRIVACY))
1095 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1096 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1099 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1100 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1101 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1102 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1104 /* Not suitable information to calculate SNR, so use level */
1109 /* best/max rate preferred if SNR close enough */
1110 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1111 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1112 maxrate_a = wpa_scan_get_max_rate(wa);
1113 maxrate_b = wpa_scan_get_max_rate(wb);
1114 if (maxrate_a != maxrate_b)
1115 return maxrate_b - maxrate_a;
1116 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1117 return IS_5GHZ(wa->freq) ? -1 : 1;
1120 /* use freq for channel preference */
1122 /* all things being equal, use SNR; if SNRs are
1123 * identical, use quality values since some drivers may only report
1124 * that value and leave the signal level zero */
1126 return wb->qual - wa->qual;
1127 return snr_b - snr_a;
1134 /* Compare function for sorting scan results when searching a WPS AP for
1135 * provisioning. Return >0 if @b is considered better. */
1136 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1138 struct wpa_scan_res **_wa = (void *) a;
1139 struct wpa_scan_res **_wb = (void *) b;
1140 struct wpa_scan_res *wa = *_wa;
1141 struct wpa_scan_res *wb = *_wb;
1142 int uses_wps_a, uses_wps_b;
1143 struct wpabuf *wps_a, *wps_b;
1146 /* Optimization - check WPS IE existence before allocated memory and
1147 * doing full reassembly. */
1148 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1149 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1150 if (uses_wps_a && !uses_wps_b)
1152 if (!uses_wps_a && uses_wps_b)
1155 if (uses_wps_a && uses_wps_b) {
1156 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1157 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1158 res = wps_ap_priority_compar(wps_a, wps_b);
1166 * Do not use current AP security policy as a sorting criteria during
1167 * WPS provisioning step since the AP may get reconfigured at the
1168 * completion of provisioning.
1171 /* all things being equal, use signal level; if signal levels are
1172 * identical, use quality values since some drivers may only report
1173 * that value and leave the signal level zero */
1174 if (wb->level == wa->level)
1175 return wb->qual - wa->qual;
1176 return wb->level - wa->level;
1178 #endif /* CONFIG_WPS */
1181 static void dump_scan_res(struct wpa_scan_results *scan_res)
1183 #ifndef CONFIG_NO_STDOUT_DEBUG
1186 if (scan_res->res == NULL || scan_res->num == 0)
1189 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1191 for (i = 0; i < scan_res->num; i++) {
1192 struct wpa_scan_res *r = scan_res->res[i];
1193 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1194 == WPA_SCAN_LEVEL_DBM) {
1195 int snr = r->level - r->noise;
1196 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1197 "noise=%d level=%d snr=%d%s flags=0x%x",
1198 MAC2STR(r->bssid), r->freq, r->qual,
1199 r->noise, r->level, snr,
1200 snr >= GREAT_SNR ? "*" : "", r->flags);
1202 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1203 "noise=%d level=%d flags=0x%x",
1204 MAC2STR(r->bssid), r->freq, r->qual,
1205 r->noise, r->level, r->flags);
1208 #endif /* CONFIG_NO_STDOUT_DEBUG */
1213 * wpa_supplicant_get_scan_results - Get scan results
1214 * @wpa_s: Pointer to wpa_supplicant data
1215 * @info: Information about what was scanned or %NULL if not available
1216 * @new_scan: Whether a new scan was performed
1217 * Returns: Scan results, %NULL on failure
1219 * This function request the current scan results from the driver and updates
1220 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1221 * results with wpa_scan_results_free().
1223 struct wpa_scan_results *
1224 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1225 struct scan_info *info, int new_scan)
1227 struct wpa_scan_results *scan_res;
1229 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1231 scan_res = wpa_drv_get_scan_results2(wpa_s);
1232 if (scan_res == NULL) {
1233 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1238 if (wpas_wps_in_progress(wpa_s)) {
1239 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1240 "provisioning rules");
1241 compar = wpa_scan_result_wps_compar;
1243 #endif /* CONFIG_WPS */
1245 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1247 dump_scan_res(scan_res);
1249 wpa_bss_update_start(wpa_s);
1250 for (i = 0; i < scan_res->num; i++)
1251 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1252 wpa_bss_update_end(wpa_s, info, new_scan);
1258 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1260 struct wpa_scan_results *scan_res;
1261 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1262 if (scan_res == NULL)
1264 wpa_scan_results_free(scan_res);