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"
25 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
27 struct wpa_ssid *ssid;
28 union wpa_event_data data;
30 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (wpa_s->current_ssid == NULL) {
35 wpa_s->current_ssid = ssid;
36 if (wpa_s->current_ssid != NULL)
37 wpas_notify_network_changed(wpa_s);
39 wpa_supplicant_initiate_eapol(wpa_s);
40 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
41 "network - generating associated event");
42 os_memset(&data, 0, sizeof(data));
43 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
48 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
49 enum wps_request_type *req_type)
51 struct wpa_ssid *ssid;
54 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
55 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
59 *req_type = wpas_wps_get_req_type(ssid);
60 if (!ssid->eap.phase1)
63 if (os_strstr(ssid->eap.phase1, "pbc=1"))
68 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) {
69 wpa_s->wps->dev.p2p = 1;
72 *req_type = WPS_REQ_ENROLLEE_INFO;
75 #endif /* CONFIG_P2P */
79 #endif /* CONFIG_WPS */
82 int wpa_supplicant_enabled_networks(struct wpa_config *conf)
84 struct wpa_ssid *ssid = conf->ssid;
87 if (!wpas_network_disabled(ssid))
95 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
96 struct wpa_ssid *ssid)
99 if (!wpas_network_disabled(ssid))
104 /* ap_scan=2 mode - try to associate with each SSID. */
106 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
107 "end of scan list - go back to beginning");
108 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
109 wpa_supplicant_req_scan(wpa_s, 0, 0);
113 /* Continue from the next SSID on the next attempt. */
114 wpa_s->prev_scan_ssid = ssid;
116 /* Start from the beginning of the SSID list. */
117 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
119 wpa_supplicant_associate(wpa_s, NULL, ssid);
123 static int int_array_len(const int *a)
126 for (i = 0; a && a[i]; i++)
132 static void int_array_concat(int **res, const int *a)
137 reslen = int_array_len(*res);
138 alen = int_array_len(a);
140 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
146 for (i = 0; i <= alen; i++)
147 n[reslen + i] = a[i];
152 static int freq_cmp(const void *a, const void *b)
165 static void int_array_sort_unique(int *a)
173 alen = int_array_len(a);
174 qsort(a, alen, sizeof(int), freq_cmp);
178 while (a[i] && a[j]) {
191 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
192 struct wpa_driver_scan_params *params)
196 wpa_supplicant_notify_scanning(wpa_s, 1);
198 ret = wpa_drv_scan(wpa_s, params);
200 wpa_supplicant_notify_scanning(wpa_s, 0);
201 wpas_notify_scan_done(wpa_s, 0);
204 wpa_s->normal_scans++;
212 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
214 struct wpa_supplicant *wpa_s = eloop_ctx;
216 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
218 if (wpa_supplicant_req_sched_scan(wpa_s))
219 wpa_supplicant_req_scan(wpa_s, 0, 0);
224 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
226 struct wpa_supplicant *wpa_s = eloop_ctx;
228 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
230 wpa_s->sched_scan_timed_out = 1;
231 wpa_supplicant_cancel_sched_scan(wpa_s);
236 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
237 struct wpa_driver_scan_params *params,
242 wpa_supplicant_notify_scanning(wpa_s, 1);
243 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
245 wpa_supplicant_notify_scanning(wpa_s, 0);
247 wpa_s->sched_scanning = 1;
253 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
257 ret = wpa_drv_stop_sched_scan(wpa_s);
259 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
260 /* TODO: what to do if stopping fails? */
268 static struct wpa_driver_scan_filter *
269 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
271 struct wpa_driver_scan_filter *ssids;
272 struct wpa_ssid *ssid;
276 if (!conf->filter_ssids)
279 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
280 if (ssid->ssid && ssid->ssid_len)
285 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
289 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
290 if (!ssid->ssid || !ssid->ssid_len)
292 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
293 ssids[*num_ssids].ssid_len = ssid->ssid_len;
301 static void wpa_supplicant_optimize_freqs(
302 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
305 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
307 /* Optimize provisioning state scan based on GO information */
308 if (wpa_s->p2p_in_provisioning < 5 &&
309 wpa_s->go_params->freq > 0) {
310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
311 "preferred frequency %d MHz",
312 wpa_s->go_params->freq);
313 params->freqs = os_zalloc(2 * sizeof(int));
315 params->freqs[0] = wpa_s->go_params->freq;
316 } else if (wpa_s->p2p_in_provisioning < 8 &&
317 wpa_s->go_params->freq_list[0]) {
318 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
320 int_array_concat(¶ms->freqs,
321 wpa_s->go_params->freq_list);
323 int_array_sort_unique(params->freqs);
325 wpa_s->p2p_in_provisioning++;
327 #endif /* CONFIG_P2P */
330 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
332 * Optimize post-provisioning scan based on channel used
333 * during provisioning.
335 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
336 "that was used during provisioning", wpa_s->wps_freq);
337 params->freqs = os_zalloc(2 * sizeof(int));
339 params->freqs[0] = wpa_s->wps_freq;
343 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
345 /* Optimize provisioning scan based on already known channel */
346 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
348 params->freqs = os_zalloc(2 * sizeof(int));
350 params->freqs[0] = wpa_s->wps_freq;
351 wpa_s->known_wps_freq = 0; /* only do this once */
353 #endif /* CONFIG_WPS */
357 #ifdef CONFIG_INTERWORKING
358 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
361 if (wpa_s->conf->interworking == 0)
364 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
365 wpabuf_put_u8(buf, 4);
366 wpabuf_put_u8(buf, 0x00);
367 wpabuf_put_u8(buf, 0x00);
368 wpabuf_put_u8(buf, 0x00);
369 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
371 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
372 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
374 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
376 if (!is_zero_ether_addr(wpa_s->conf->hessid))
377 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
379 #endif /* CONFIG_INTERWORKING */
382 static struct wpabuf *
383 wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
384 struct wpa_driver_scan_params *params)
386 struct wpabuf *extra_ie = NULL;
389 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
390 #endif /* CONFIG_WPS */
392 #ifdef CONFIG_INTERWORKING
393 if (wpa_s->conf->interworking &&
394 wpabuf_resize(&extra_ie, 100) == 0)
395 wpas_add_interworking_elements(wpa_s, extra_ie);
396 #endif /* CONFIG_INTERWORKING */
399 wps = wpas_wps_in_use(wpa_s, &req_type);
402 struct wpabuf *wps_ie;
403 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
406 wpa_s->wps->uuid, req_type,
409 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
410 wpabuf_put_buf(extra_ie, wps_ie);
417 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
418 if (wpabuf_resize(&extra_ie, ielen) == 0)
419 wpas_p2p_scan_ie(wpa_s, extra_ie);
421 #endif /* CONFIG_P2P */
423 #endif /* CONFIG_WPS */
429 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
431 struct wpa_supplicant *wpa_s = eloop_ctx;
432 struct wpa_ssid *ssid;
433 int scan_req = 0, ret;
434 struct wpabuf *extra_ie;
435 struct wpa_driver_scan_params params;
437 enum wpa_states prev_state;
439 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
440 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
444 if (wpa_s->disconnected && !wpa_s->scan_req) {
445 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
449 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
451 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
452 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
456 if (wpa_s->conf->ap_scan != 0 &&
457 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
458 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
459 "overriding ap_scan configuration");
460 wpa_s->conf->ap_scan = 0;
461 wpas_notify_ap_scan_changed(wpa_s);
464 if (wpa_s->conf->ap_scan == 0) {
465 wpa_supplicant_gen_assoc_event(wpa_s);
470 if (wpas_p2p_in_progress(wpa_s)) {
471 if (wpa_s->wpa_state == WPA_SCANNING) {
472 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
473 "while P2P operation is in progress");
474 wpa_supplicant_req_scan(wpa_s, 5, 0);
476 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
477 "P2P operation is in progress");
481 #endif /* CONFIG_P2P */
483 if (wpa_s->conf->ap_scan == 2)
486 max_ssids = wpa_s->max_scan_ssids;
487 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
488 max_ssids = WPAS_MAX_SCAN_SSIDS;
491 scan_req = wpa_s->scan_req;
494 os_memset(¶ms, 0, sizeof(params));
496 prev_state = wpa_s->wpa_state;
497 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
498 wpa_s->wpa_state == WPA_INACTIVE)
499 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
501 if (scan_req != 2 && wpa_s->connect_without_scan) {
502 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
503 if (ssid == wpa_s->connect_without_scan)
506 wpa_s->connect_without_scan = NULL;
508 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
509 "without scan step");
510 wpa_supplicant_associate(wpa_s, NULL, ssid);
516 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
518 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
519 "P2P group formation");
520 params.ssids[0].ssid = wpa_s->go_params->ssid;
521 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
522 params.num_ssids = 1;
525 #endif /* CONFIG_P2P */
527 /* Find the starting point from which to continue scanning */
528 ssid = wpa_s->conf->ssid;
529 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
531 if (ssid == wpa_s->prev_scan_ssid) {
539 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
540 wpa_s->connect_without_scan = NULL;
541 wpa_s->prev_scan_wildcard = 0;
542 wpa_supplicant_assoc_try(wpa_s, ssid);
544 } else if (wpa_s->conf->ap_scan == 2) {
546 * User-initiated scan request in ap_scan == 2; scan with
551 struct wpa_ssid *start = ssid, *tssid;
553 if (ssid == NULL && max_ssids > 1)
554 ssid = wpa_s->conf->ssid;
556 if (!wpas_network_disabled(ssid) && ssid->scan_ssid) {
557 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
558 ssid->ssid, ssid->ssid_len);
559 params.ssids[params.num_ssids].ssid =
561 params.ssids[params.num_ssids].ssid_len =
564 if (params.num_ssids + 1 >= max_ssids)
570 if (ssid == NULL && max_ssids > 1 &&
571 start != wpa_s->conf->ssid)
572 ssid = wpa_s->conf->ssid;
575 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
576 if (wpas_network_disabled(tssid))
578 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
579 int_array_concat(¶ms.freqs,
582 os_free(params.freqs);
587 int_array_sort_unique(params.freqs);
590 if (ssid && max_ssids == 1) {
592 * If the driver is limited to 1 SSID at a time interleave
593 * wildcard SSID scans with specific SSID scans to avoid
594 * waiting a long time for a wildcard scan.
596 if (!wpa_s->prev_scan_wildcard) {
597 params.ssids[0].ssid = NULL;
598 params.ssids[0].ssid_len = 0;
599 wpa_s->prev_scan_wildcard = 1;
600 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
601 "wildcard SSID (Interleave with specific)");
603 wpa_s->prev_scan_ssid = ssid;
604 wpa_s->prev_scan_wildcard = 0;
605 wpa_dbg(wpa_s, MSG_DEBUG,
606 "Starting AP scan for specific SSID: %s",
607 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
612 wpa_s->prev_scan_ssid = ssid;
613 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
617 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
619 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
624 #endif /* CONFIG_P2P */
626 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
627 extra_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
629 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
630 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
631 "generated frequency list");
632 params.freqs = wpa_s->next_scan_freqs;
634 os_free(wpa_s->next_scan_freqs);
635 wpa_s->next_scan_freqs = NULL;
637 params.filter_ssids = wpa_supplicant_build_filter_ssids(
638 wpa_s->conf, ¶ms.num_filter_ssids);
640 params.extra_ies = wpabuf_head(extra_ie);
641 params.extra_ies_len = wpabuf_len(extra_ie);
645 if (wpa_s->p2p_in_provisioning ||
646 (wpa_s->show_group_started && wpa_s->go_params)) {
648 * The interface may not yet be in P2P mode, so we have to
649 * explicitly request P2P probe to disable CCK rates.
651 params.p2p_probe = 1;
653 #endif /* CONFIG_P2P */
655 ret = wpa_supplicant_trigger_scan(wpa_s, ¶ms);
657 wpabuf_free(extra_ie);
658 os_free(params.freqs);
659 os_free(params.filter_ssids);
662 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
663 if (prev_state != wpa_s->wpa_state)
664 wpa_supplicant_set_state(wpa_s, prev_state);
665 wpa_supplicant_req_scan(wpa_s, 1, 0);
671 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
672 * @wpa_s: Pointer to wpa_supplicant data
673 * @sec: Number of seconds after which to scan
674 * @usec: Number of microseconds after which to scan
676 * This function is used to schedule a scan for neighboring access points after
677 * the specified time.
679 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
681 /* If there's at least one network that should be specifically scanned
682 * then don't cancel the scan and reschedule. Some drivers do
683 * background scanning which generates frequent scan results, and that
684 * causes the specific SSID scan to get continually pushed back and
685 * never happen, which causes hidden APs to never get probe-scanned.
687 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
688 wpa_s->conf->ap_scan == 1) {
689 struct wpa_ssid *ssid = wpa_s->conf->ssid;
692 if (!wpas_network_disabled(ssid) && ssid->scan_ssid)
697 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
698 "ensure that specific SSID scans occur");
703 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
705 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
706 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
711 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
712 * @wpa_s: Pointer to wpa_supplicant data
713 * @sec: Number of seconds after which to scan
714 * @usec: Number of microseconds after which to scan
716 * This function is used to schedule periodic scans for neighboring
717 * access points after the specified time.
719 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
722 if (!wpa_s->sched_scan_supported)
725 eloop_register_timeout(sec, usec,
726 wpa_supplicant_delayed_sched_scan_timeout,
734 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
735 * @wpa_s: Pointer to wpa_supplicant data
737 * This function is used to schedule periodic scans for neighboring
738 * access points repeating the scan continuously.
740 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
742 struct wpa_driver_scan_params params;
743 enum wpa_states prev_state;
744 struct wpa_ssid *ssid;
745 struct wpabuf *wps_ie = NULL;
747 unsigned int max_sched_scan_ssids;
751 if (!wpa_s->sched_scan_supported)
754 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
755 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
757 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
758 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
761 if (wpa_s->sched_scanning) {
762 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
767 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
768 if (!wpas_network_disabled(ssid) && !ssid->scan_ssid) {
769 /* Use wildcard SSID to find this network */
771 } else if (!wpas_network_disabled(ssid) && ssid->ssid_len)
775 if (!wpas_network_disabled(ssid) &&
776 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
778 * Normal scan is more reliable and faster for WPS
779 * operations and since these are for short periods of
780 * time, the benefit of trying to use sched_scan would
783 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
784 "sched_scan for WPS");
787 #endif /* CONFIG_WPS */
792 if (wpa_s->normal_scans < 3 &&
793 (need_ssids <= wpa_s->max_scan_ssids ||
794 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
796 * When normal scan can speed up operations, use that for the
797 * first operations before starting the sched_scan to allow
798 * user space sleep more. We do this only if the normal scan
799 * has functionality that is suitable for this or if the
800 * sched_scan does not have better support for multiple SSIDs.
802 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
803 "sched_scan for initial scans (normal_scans=%d)",
804 wpa_s->normal_scans);
808 os_memset(¶ms, 0, sizeof(params));
810 /* If we can't allocate space for the filters, we just don't filter */
811 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
812 sizeof(struct wpa_driver_scan_filter));
814 prev_state = wpa_s->wpa_state;
815 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
816 wpa_s->wpa_state == WPA_INACTIVE)
817 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
819 /* Find the starting point from which to continue scanning */
820 ssid = wpa_s->conf->ssid;
821 if (wpa_s->prev_sched_ssid) {
823 if (ssid == wpa_s->prev_sched_ssid) {
831 if (!ssid || !wpa_s->prev_sched_ssid) {
832 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
834 wpa_s->sched_scan_interval = 10;
835 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
836 wpa_s->first_sched_scan = 1;
837 ssid = wpa_s->conf->ssid;
838 wpa_s->prev_sched_ssid = ssid;
842 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
847 if (wpas_network_disabled(ssid))
850 if (params.num_filter_ssids < wpa_s->max_match_sets &&
851 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
852 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
853 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
854 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
855 ssid->ssid, ssid->ssid_len);
856 params.filter_ssids[params.num_filter_ssids].ssid_len =
858 params.num_filter_ssids++;
859 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
861 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
862 "filter for sched_scan - drop filter");
863 os_free(params.filter_ssids);
864 params.filter_ssids = NULL;
865 params.num_filter_ssids = 0;
868 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
869 if (params.num_ssids == max_sched_scan_ssids)
870 break; /* only room for broadcast SSID */
871 wpa_dbg(wpa_s, MSG_DEBUG,
872 "add to active scan ssid: %s",
873 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
874 params.ssids[params.num_ssids].ssid =
876 params.ssids[params.num_ssids].ssid_len =
879 if (params.num_ssids >= max_sched_scan_ssids) {
880 wpa_s->prev_sched_ssid = ssid;
884 (wpas_network_disabled(ssid) ||
891 wpa_s->prev_sched_ssid = ssid;
895 if (params.num_filter_ssids == 0) {
896 os_free(params.filter_ssids);
897 params.filter_ssids = NULL;
901 wps_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
903 if (ssid || !wpa_s->first_sched_scan) {
904 wpa_dbg(wpa_s, MSG_DEBUG,
905 "Starting sched scan: interval %d timeout %d",
906 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
908 wpa_dbg(wpa_s, MSG_DEBUG,
909 "Starting sched scan: interval %d (no timeout)",
910 wpa_s->sched_scan_interval);
913 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms,
914 wpa_s->sched_scan_interval);
916 os_free(params.filter_ssids);
918 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
919 if (prev_state != wpa_s->wpa_state)
920 wpa_supplicant_set_state(wpa_s, prev_state);
924 /* If we have more SSIDs to scan, add a timeout so we scan them too */
925 if (ssid || !wpa_s->first_sched_scan) {
926 wpa_s->sched_scan_timed_out = 0;
927 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
928 wpa_supplicant_sched_scan_timeout,
930 wpa_s->first_sched_scan = 0;
931 wpa_s->sched_scan_timeout /= 2;
932 wpa_s->sched_scan_interval *= 2;
940 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
941 * @wpa_s: Pointer to wpa_supplicant data
943 * This function is used to cancel a scan request scheduled with
944 * wpa_supplicant_req_scan().
946 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
948 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
949 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
954 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
955 * @wpa_s: Pointer to wpa_supplicant data
957 * This function is used to stop a periodic scheduled scan.
959 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
961 if (!wpa_s->sched_scanning)
964 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
965 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
966 wpa_supplicant_stop_sched_scan(wpa_s);
970 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
973 if (wpa_s->scanning != scanning) {
974 wpa_s->scanning = scanning;
975 wpas_notify_scanning(wpa_s);
980 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
986 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
987 for (i = 0; ie && i < ie[1]; i++) {
988 if ((ie[i + 2] & 0x7f) > rate)
989 rate = ie[i + 2] & 0x7f;
992 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
993 for (i = 0; ie && i < ie[1]; i++) {
994 if ((ie[i + 2] & 0x7f) > rate)
995 rate = ie[i + 2] & 0x7f;
1002 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1004 const u8 *end, *pos;
1006 pos = (const u8 *) (res + 1);
1007 end = pos + res->ie_len;
1009 while (pos + 1 < end) {
1010 if (pos + 2 + pos[1] > end)
1021 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1024 const u8 *end, *pos;
1026 pos = (const u8 *) (res + 1);
1027 end = pos + res->ie_len;
1029 while (pos + 1 < end) {
1030 if (pos + 2 + pos[1] > end)
1032 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1033 vendor_type == WPA_GET_BE32(&pos[2]))
1042 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1046 const u8 *end, *pos;
1048 buf = wpabuf_alloc(res->ie_len);
1052 pos = (const u8 *) (res + 1);
1053 end = pos + res->ie_len;
1055 while (pos + 1 < end) {
1056 if (pos + 2 + pos[1] > end)
1058 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1059 vendor_type == WPA_GET_BE32(&pos[2]))
1060 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1064 if (wpabuf_len(buf) == 0) {
1073 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1074 const struct wpa_scan_res *res, u32 vendor_type)
1077 const u8 *end, *pos;
1079 if (res->beacon_ie_len == 0)
1081 buf = wpabuf_alloc(res->beacon_ie_len);
1085 pos = (const u8 *) (res + 1);
1087 end = pos + res->beacon_ie_len;
1089 while (pos + 1 < end) {
1090 if (pos + 2 + pos[1] > end)
1092 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1093 vendor_type == WPA_GET_BE32(&pos[2]))
1094 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1098 if (wpabuf_len(buf) == 0) {
1108 * Channels with a great SNR can operate at full rate. What is a great SNR?
1109 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1110 * rule of thumb is that any SNR above 20 is good." This one
1111 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1112 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1113 * conservative value.
1115 #define GREAT_SNR 30
1117 /* Compare function for sorting scan results. Return >0 if @b is considered
1119 static int wpa_scan_result_compar(const void *a, const void *b)
1121 #define IS_5GHZ(n) (n > 4000)
1122 #define MIN(a,b) a < b ? a : b
1123 struct wpa_scan_res **_wa = (void *) a;
1124 struct wpa_scan_res **_wb = (void *) b;
1125 struct wpa_scan_res *wa = *_wa;
1126 struct wpa_scan_res *wb = *_wb;
1127 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1130 /* WPA/WPA2 support preferred */
1131 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1132 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1133 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1134 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1136 if (wpa_b && !wpa_a)
1138 if (!wpa_b && wpa_a)
1141 /* privacy support preferred */
1142 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1143 (wb->caps & IEEE80211_CAP_PRIVACY))
1145 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1146 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1149 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1150 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1151 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1152 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1154 /* Not suitable information to calculate SNR, so use level */
1159 /* best/max rate preferred if SNR close enough */
1160 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1161 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1162 maxrate_a = wpa_scan_get_max_rate(wa);
1163 maxrate_b = wpa_scan_get_max_rate(wb);
1164 if (maxrate_a != maxrate_b)
1165 return maxrate_b - maxrate_a;
1166 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1167 return IS_5GHZ(wa->freq) ? -1 : 1;
1170 /* use freq for channel preference */
1172 /* all things being equal, use SNR; if SNRs are
1173 * identical, use quality values since some drivers may only report
1174 * that value and leave the signal level zero */
1176 return wb->qual - wa->qual;
1177 return snr_b - snr_a;
1184 /* Compare function for sorting scan results when searching a WPS AP for
1185 * provisioning. Return >0 if @b is considered better. */
1186 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1188 struct wpa_scan_res **_wa = (void *) a;
1189 struct wpa_scan_res **_wb = (void *) b;
1190 struct wpa_scan_res *wa = *_wa;
1191 struct wpa_scan_res *wb = *_wb;
1192 int uses_wps_a, uses_wps_b;
1193 struct wpabuf *wps_a, *wps_b;
1196 /* Optimization - check WPS IE existence before allocated memory and
1197 * doing full reassembly. */
1198 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1199 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1200 if (uses_wps_a && !uses_wps_b)
1202 if (!uses_wps_a && uses_wps_b)
1205 if (uses_wps_a && uses_wps_b) {
1206 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1207 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1208 res = wps_ap_priority_compar(wps_a, wps_b);
1216 * Do not use current AP security policy as a sorting criteria during
1217 * WPS provisioning step since the AP may get reconfigured at the
1218 * completion of provisioning.
1221 /* all things being equal, use signal level; if signal levels are
1222 * identical, use quality values since some drivers may only report
1223 * that value and leave the signal level zero */
1224 if (wb->level == wa->level)
1225 return wb->qual - wa->qual;
1226 return wb->level - wa->level;
1228 #endif /* CONFIG_WPS */
1231 static void dump_scan_res(struct wpa_scan_results *scan_res)
1233 #ifndef CONFIG_NO_STDOUT_DEBUG
1236 if (scan_res->res == NULL || scan_res->num == 0)
1239 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1241 for (i = 0; i < scan_res->num; i++) {
1242 struct wpa_scan_res *r = scan_res->res[i];
1243 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1244 == WPA_SCAN_LEVEL_DBM) {
1245 int snr = r->level - r->noise;
1246 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1247 "noise=%d level=%d snr=%d%s flags=0x%x",
1248 MAC2STR(r->bssid), r->freq, r->qual,
1249 r->noise, r->level, snr,
1250 snr >= GREAT_SNR ? "*" : "", r->flags);
1252 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1253 "noise=%d level=%d flags=0x%x",
1254 MAC2STR(r->bssid), r->freq, r->qual,
1255 r->noise, r->level, r->flags);
1258 #endif /* CONFIG_NO_STDOUT_DEBUG */
1262 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1267 if (wpa_s->bssid_filter == NULL)
1270 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1271 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1280 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1281 struct wpa_scan_results *res)
1285 if (wpa_s->bssid_filter == NULL)
1288 for (i = 0, j = 0; i < res->num; i++) {
1289 if (wpa_supplicant_filter_bssid_match(wpa_s,
1290 res->res[i]->bssid)) {
1291 res->res[j++] = res->res[i];
1293 os_free(res->res[i]);
1298 if (res->num != j) {
1299 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1300 (int) (res->num - j));
1307 * wpa_supplicant_get_scan_results - Get scan results
1308 * @wpa_s: Pointer to wpa_supplicant data
1309 * @info: Information about what was scanned or %NULL if not available
1310 * @new_scan: Whether a new scan was performed
1311 * Returns: Scan results, %NULL on failure
1313 * This function request the current scan results from the driver and updates
1314 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1315 * results with wpa_scan_results_free().
1317 struct wpa_scan_results *
1318 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1319 struct scan_info *info, int new_scan)
1321 struct wpa_scan_results *scan_res;
1323 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1325 scan_res = wpa_drv_get_scan_results2(wpa_s);
1326 if (scan_res == NULL) {
1327 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1330 filter_scan_res(wpa_s, scan_res);
1333 if (wpas_wps_in_progress(wpa_s)) {
1334 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1335 "provisioning rules");
1336 compar = wpa_scan_result_wps_compar;
1338 #endif /* CONFIG_WPS */
1340 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1342 dump_scan_res(scan_res);
1344 wpa_bss_update_start(wpa_s);
1345 for (i = 0; i < scan_res->num; i++)
1346 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1347 wpa_bss_update_end(wpa_s, info, new_scan);
1353 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1355 struct wpa_scan_results *scan_res;
1356 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1357 if (scan_res == NULL)
1359 wpa_scan_results_free(scan_res);