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 wpa_s->wps->dev.p2p = 1;
77 *req_type = WPS_REQ_ENROLLEE_INFO;
79 #endif /* CONFIG_P2P */
83 #endif /* CONFIG_WPS */
86 int wpa_supplicant_enabled_networks(struct wpa_config *conf)
88 struct wpa_ssid *ssid = conf->ssid;
99 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
100 struct wpa_ssid *ssid)
108 /* ap_scan=2 mode - try to associate with each SSID. */
110 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
111 "end of scan list - go back to beginning");
112 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
113 wpa_supplicant_req_scan(wpa_s, 0, 0);
117 /* Continue from the next SSID on the next attempt. */
118 wpa_s->prev_scan_ssid = ssid;
120 /* Start from the beginning of the SSID list. */
121 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
123 wpa_supplicant_associate(wpa_s, NULL, ssid);
127 static int int_array_len(const int *a)
130 for (i = 0; a && a[i]; i++)
136 static void int_array_concat(int **res, const int *a)
141 reslen = int_array_len(*res);
142 alen = int_array_len(a);
144 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
150 for (i = 0; i <= alen; i++)
151 n[reslen + i] = a[i];
156 static int freq_cmp(const void *a, const void *b)
169 static void int_array_sort_unique(int *a)
177 alen = int_array_len(a);
178 qsort(a, alen, sizeof(int), freq_cmp);
182 while (a[i] && a[j]) {
195 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
196 struct wpa_driver_scan_params *params)
200 wpa_supplicant_notify_scanning(wpa_s, 1);
202 ret = wpa_drv_scan(wpa_s, params);
204 wpa_supplicant_notify_scanning(wpa_s, 0);
205 wpas_notify_scan_done(wpa_s, 0);
208 wpa_s->normal_scans++;
216 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
218 struct wpa_supplicant *wpa_s = eloop_ctx;
220 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
222 if (wpa_supplicant_req_sched_scan(wpa_s))
223 wpa_supplicant_req_scan(wpa_s, 0, 0);
228 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
230 struct wpa_supplicant *wpa_s = eloop_ctx;
232 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
234 wpa_s->sched_scan_timed_out = 1;
235 wpa_supplicant_cancel_sched_scan(wpa_s);
240 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
241 struct wpa_driver_scan_params *params,
246 wpa_supplicant_notify_scanning(wpa_s, 1);
247 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
249 wpa_supplicant_notify_scanning(wpa_s, 0);
251 wpa_s->sched_scanning = 1;
257 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
261 ret = wpa_drv_stop_sched_scan(wpa_s);
263 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
264 /* TODO: what to do if stopping fails? */
272 static struct wpa_driver_scan_filter *
273 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
275 struct wpa_driver_scan_filter *ssids;
276 struct wpa_ssid *ssid;
280 if (!conf->filter_ssids)
283 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
284 if (ssid->ssid && ssid->ssid_len)
289 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
293 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
294 if (!ssid->ssid || !ssid->ssid_len)
296 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
297 ssids[*num_ssids].ssid_len = ssid->ssid_len;
305 static void wpa_supplicant_optimize_freqs(
306 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
309 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
311 /* Optimize provisioning state scan based on GO information */
312 if (wpa_s->p2p_in_provisioning < 5 &&
313 wpa_s->go_params->freq > 0) {
314 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
315 "preferred frequency %d MHz",
316 wpa_s->go_params->freq);
317 params->freqs = os_zalloc(2 * sizeof(int));
319 params->freqs[0] = wpa_s->go_params->freq;
320 } else if (wpa_s->p2p_in_provisioning < 8 &&
321 wpa_s->go_params->freq_list[0]) {
322 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
324 int_array_concat(¶ms->freqs,
325 wpa_s->go_params->freq_list);
327 int_array_sort_unique(params->freqs);
329 wpa_s->p2p_in_provisioning++;
331 #endif /* CONFIG_P2P */
334 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
336 * Optimize post-provisioning scan based on channel used
337 * during provisioning.
339 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
340 "that was used during provisioning", wpa_s->wps_freq);
341 params->freqs = os_zalloc(2 * sizeof(int));
343 params->freqs[0] = wpa_s->wps_freq;
347 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
349 /* Optimize provisioning scan based on already known channel */
350 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
352 params->freqs = os_zalloc(2 * sizeof(int));
354 params->freqs[0] = wpa_s->wps_freq;
355 wpa_s->known_wps_freq = 0; /* only do this once */
357 #endif /* CONFIG_WPS */
361 #ifdef CONFIG_INTERWORKING
362 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
365 if (wpa_s->conf->interworking == 0)
368 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
369 wpabuf_put_u8(buf, 4);
370 wpabuf_put_u8(buf, 0x00);
371 wpabuf_put_u8(buf, 0x00);
372 wpabuf_put_u8(buf, 0x00);
373 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
375 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
376 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
378 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
380 if (!is_zero_ether_addr(wpa_s->conf->hessid))
381 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
383 #endif /* CONFIG_INTERWORKING */
386 static struct wpabuf *
387 wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
388 struct wpa_driver_scan_params *params)
390 struct wpabuf *extra_ie = NULL;
393 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
394 #endif /* CONFIG_WPS */
396 #ifdef CONFIG_INTERWORKING
397 if (wpa_s->conf->interworking &&
398 wpabuf_resize(&extra_ie, 100) == 0)
399 wpas_add_interworking_elements(wpa_s, extra_ie);
400 #endif /* CONFIG_INTERWORKING */
403 wps = wpas_wps_in_use(wpa_s, &req_type);
406 struct wpabuf *wps_ie;
407 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
408 wpa_s->wps->uuid, req_type,
411 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
412 wpabuf_put_buf(extra_ie, wps_ie);
419 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
420 if (wpabuf_resize(&extra_ie, ielen) == 0)
421 wpas_p2p_scan_ie(wpa_s, extra_ie);
423 #endif /* CONFIG_P2P */
425 #endif /* CONFIG_WPS */
431 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
433 struct wpa_supplicant *wpa_s = eloop_ctx;
434 struct wpa_ssid *ssid;
435 int scan_req = 0, ret;
436 struct wpabuf *extra_ie;
437 struct wpa_driver_scan_params params;
439 enum wpa_states prev_state;
441 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
442 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
446 if (wpa_s->disconnected && !wpa_s->scan_req) {
447 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
451 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
453 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
454 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
458 if (wpa_s->conf->ap_scan != 0 &&
459 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
460 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
461 "overriding ap_scan configuration");
462 wpa_s->conf->ap_scan = 0;
463 wpas_notify_ap_scan_changed(wpa_s);
466 if (wpa_s->conf->ap_scan == 0) {
467 wpa_supplicant_gen_assoc_event(wpa_s);
472 if (wpas_p2p_in_progress(wpa_s)) {
473 if (wpa_s->wpa_state == WPA_SCANNING) {
474 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
475 "while P2P operation is in progress");
476 wpa_supplicant_req_scan(wpa_s, 5, 0);
478 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
479 "P2P operation is in progress");
483 #endif /* CONFIG_P2P */
485 if (wpa_s->conf->ap_scan == 2)
488 max_ssids = wpa_s->max_scan_ssids;
489 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
490 max_ssids = WPAS_MAX_SCAN_SSIDS;
493 scan_req = wpa_s->scan_req;
496 os_memset(¶ms, 0, sizeof(params));
498 prev_state = wpa_s->wpa_state;
499 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
500 wpa_s->wpa_state == WPA_INACTIVE)
501 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
503 if (scan_req != 2 && wpa_s->connect_without_scan) {
504 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
505 if (ssid == wpa_s->connect_without_scan)
508 wpa_s->connect_without_scan = NULL;
510 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
511 "without scan step");
512 wpa_supplicant_associate(wpa_s, NULL, ssid);
517 /* Find the starting point from which to continue scanning */
518 ssid = wpa_s->conf->ssid;
519 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
521 if (ssid == wpa_s->prev_scan_ssid) {
529 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
530 wpa_s->connect_without_scan = NULL;
531 wpa_supplicant_assoc_try(wpa_s, ssid);
533 } else if (wpa_s->conf->ap_scan == 2) {
535 * User-initiated scan request in ap_scan == 2; scan with
540 struct wpa_ssid *start = ssid, *tssid;
542 if (ssid == NULL && max_ssids > 1)
543 ssid = wpa_s->conf->ssid;
545 if (!ssid->disabled && ssid->scan_ssid) {
546 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
547 ssid->ssid, ssid->ssid_len);
548 params.ssids[params.num_ssids].ssid =
550 params.ssids[params.num_ssids].ssid_len =
553 if (params.num_ssids + 1 >= max_ssids)
559 if (ssid == NULL && max_ssids > 1 &&
560 start != wpa_s->conf->ssid)
561 ssid = wpa_s->conf->ssid;
564 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
567 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
568 int_array_concat(¶ms.freqs,
571 os_free(params.freqs);
576 int_array_sort_unique(params.freqs);
580 wpa_s->prev_scan_ssid = ssid;
582 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
586 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
589 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
591 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
595 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
596 extra_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
598 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
599 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
600 "generated frequency list");
601 params.freqs = wpa_s->next_scan_freqs;
603 os_free(wpa_s->next_scan_freqs);
604 wpa_s->next_scan_freqs = NULL;
606 params.filter_ssids = wpa_supplicant_build_filter_ssids(
607 wpa_s->conf, ¶ms.num_filter_ssids);
609 params.extra_ies = wpabuf_head(extra_ie);
610 params.extra_ies_len = wpabuf_len(extra_ie);
614 if (wpa_s->p2p_in_provisioning) {
616 * The interface may not yet be in P2P mode, so we have to
617 * explicitly request P2P probe to disable CCK rates.
619 params.p2p_probe = 1;
621 #endif /* CONFIG_P2P */
623 ret = wpa_supplicant_trigger_scan(wpa_s, ¶ms);
625 wpabuf_free(extra_ie);
626 os_free(params.freqs);
627 os_free(params.filter_ssids);
630 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
631 if (prev_state != wpa_s->wpa_state)
632 wpa_supplicant_set_state(wpa_s, prev_state);
633 wpa_supplicant_req_scan(wpa_s, 1, 0);
639 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
640 * @wpa_s: Pointer to wpa_supplicant data
641 * @sec: Number of seconds after which to scan
642 * @usec: Number of microseconds after which to scan
644 * This function is used to schedule a scan for neighboring access points after
645 * the specified time.
647 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
649 /* If there's at least one network that should be specifically scanned
650 * then don't cancel the scan and reschedule. Some drivers do
651 * background scanning which generates frequent scan results, and that
652 * causes the specific SSID scan to get continually pushed back and
653 * never happen, which causes hidden APs to never get probe-scanned.
655 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
656 wpa_s->conf->ap_scan == 1) {
657 struct wpa_ssid *ssid = wpa_s->conf->ssid;
660 if (!ssid->disabled && ssid->scan_ssid)
665 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
666 "ensure that specific SSID scans occur");
671 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
673 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
674 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
679 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
680 * @wpa_s: Pointer to wpa_supplicant data
681 * @sec: Number of seconds after which to scan
682 * @usec: Number of microseconds after which to scan
684 * This function is used to schedule periodic scans for neighboring
685 * access points after the specified time.
687 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
690 if (!wpa_s->sched_scan_supported)
693 eloop_register_timeout(sec, usec,
694 wpa_supplicant_delayed_sched_scan_timeout,
702 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
703 * @wpa_s: Pointer to wpa_supplicant data
705 * This function is used to schedule periodic scans for neighboring
706 * access points repeating the scan continuously.
708 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
710 struct wpa_driver_scan_params params;
711 enum wpa_states prev_state;
712 struct wpa_ssid *ssid;
713 struct wpabuf *wps_ie = NULL;
715 unsigned int max_sched_scan_ssids;
719 if (!wpa_s->sched_scan_supported)
722 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
723 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
725 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
726 if (max_sched_scan_ssids < 1)
729 if (wpa_s->sched_scanning) {
730 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
735 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
736 if (!ssid->disabled && !ssid->scan_ssid) {
737 /* Use wildcard SSID to find this network */
739 } else if (!ssid->disabled && ssid->ssid_len)
745 if (wpa_s->normal_scans < 3 &&
746 (need_ssids <= wpa_s->max_scan_ssids ||
747 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
749 * When normal scan can speed up operations, use that for the
750 * first operations before starting the sched_scan to allow
751 * user space sleep more. We do this only if the normal scan
752 * has functionality that is suitable for this or if the
753 * sched_scan does not have better support for multiple SSIDs.
755 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
756 "sched_scan for initial scans (normal_scans=%d)",
757 wpa_s->normal_scans);
761 os_memset(¶ms, 0, sizeof(params));
763 /* If we can't allocate space for the filters, we just don't filter */
764 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
765 sizeof(struct wpa_driver_scan_filter));
767 prev_state = wpa_s->wpa_state;
768 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
769 wpa_s->wpa_state == WPA_INACTIVE)
770 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
772 /* Find the starting point from which to continue scanning */
773 ssid = wpa_s->conf->ssid;
774 if (wpa_s->prev_sched_ssid) {
776 if (ssid == wpa_s->prev_sched_ssid) {
784 if (!ssid || !wpa_s->prev_sched_ssid) {
785 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
787 wpa_s->sched_scan_interval = 10;
788 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
789 wpa_s->first_sched_scan = 1;
790 ssid = wpa_s->conf->ssid;
791 wpa_s->prev_sched_ssid = ssid;
795 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
803 if (params.num_filter_ssids < wpa_s->max_match_sets &&
804 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
805 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
806 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
807 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
808 ssid->ssid, ssid->ssid_len);
809 params.filter_ssids[params.num_filter_ssids].ssid_len =
811 params.num_filter_ssids++;
812 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
814 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
815 "filter for sched_scan - drop filter");
816 os_free(params.filter_ssids);
817 params.filter_ssids = NULL;
818 params.num_filter_ssids = 0;
821 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
822 if (params.num_ssids == max_sched_scan_ssids)
823 break; /* only room for broadcast SSID */
824 wpa_dbg(wpa_s, MSG_DEBUG,
825 "add to active scan ssid: %s",
826 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
827 params.ssids[params.num_ssids].ssid =
829 params.ssids[params.num_ssids].ssid_len =
832 if (params.num_ssids >= max_sched_scan_ssids) {
833 wpa_s->prev_sched_ssid = ssid;
839 wpa_s->prev_sched_ssid = ssid;
843 if (params.num_filter_ssids == 0) {
844 os_free(params.filter_ssids);
845 params.filter_ssids = NULL;
849 wps_ie = wpa_supplicant_extra_ies(wpa_s, ¶ms);
851 if (ssid || !wpa_s->first_sched_scan) {
852 wpa_dbg(wpa_s, MSG_DEBUG,
853 "Starting sched scan: interval %d (no timeout)",
854 wpa_s->sched_scan_interval);
856 wpa_dbg(wpa_s, MSG_DEBUG,
857 "Starting sched scan: interval %d timeout %d",
858 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
861 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms,
862 wpa_s->sched_scan_interval);
864 os_free(params.filter_ssids);
866 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
867 if (prev_state != wpa_s->wpa_state)
868 wpa_supplicant_set_state(wpa_s, prev_state);
872 /* If we have more SSIDs to scan, add a timeout so we scan them too */
873 if (ssid || !wpa_s->first_sched_scan) {
874 wpa_s->sched_scan_timed_out = 0;
875 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
876 wpa_supplicant_sched_scan_timeout,
878 wpa_s->first_sched_scan = 0;
879 wpa_s->sched_scan_timeout /= 2;
880 wpa_s->sched_scan_interval *= 2;
888 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
889 * @wpa_s: Pointer to wpa_supplicant data
891 * This function is used to cancel a scan request scheduled with
892 * wpa_supplicant_req_scan().
894 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
896 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
897 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
902 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
903 * @wpa_s: Pointer to wpa_supplicant data
905 * This function is used to stop a periodic scheduled scan.
907 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
909 if (!wpa_s->sched_scanning)
912 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
913 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
914 wpa_supplicant_stop_sched_scan(wpa_s);
918 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
921 if (wpa_s->scanning != scanning) {
922 wpa_s->scanning = scanning;
923 wpas_notify_scanning(wpa_s);
928 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
934 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
935 for (i = 0; ie && i < ie[1]; i++) {
936 if ((ie[i + 2] & 0x7f) > rate)
937 rate = ie[i + 2] & 0x7f;
940 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
941 for (i = 0; ie && i < ie[1]; i++) {
942 if ((ie[i + 2] & 0x7f) > rate)
943 rate = ie[i + 2] & 0x7f;
950 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
954 pos = (const u8 *) (res + 1);
955 end = pos + res->ie_len;
957 while (pos + 1 < end) {
958 if (pos + 2 + pos[1] > end)
969 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
974 pos = (const u8 *) (res + 1);
975 end = pos + res->ie_len;
977 while (pos + 1 < end) {
978 if (pos + 2 + pos[1] > end)
980 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
981 vendor_type == WPA_GET_BE32(&pos[2]))
990 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
996 buf = wpabuf_alloc(res->ie_len);
1000 pos = (const u8 *) (res + 1);
1001 end = pos + res->ie_len;
1003 while (pos + 1 < end) {
1004 if (pos + 2 + pos[1] > end)
1006 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1007 vendor_type == WPA_GET_BE32(&pos[2]))
1008 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1012 if (wpabuf_len(buf) == 0) {
1021 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1022 const struct wpa_scan_res *res, u32 vendor_type)
1025 const u8 *end, *pos;
1027 if (res->beacon_ie_len == 0)
1029 buf = wpabuf_alloc(res->beacon_ie_len);
1033 pos = (const u8 *) (res + 1);
1035 end = pos + res->beacon_ie_len;
1037 while (pos + 1 < end) {
1038 if (pos + 2 + pos[1] > end)
1040 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1041 vendor_type == WPA_GET_BE32(&pos[2]))
1042 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1046 if (wpabuf_len(buf) == 0) {
1056 * Channels with a great SNR can operate at full rate. What is a great SNR?
1057 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1058 * rule of thumb is that any SNR above 20 is good." This one
1059 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1060 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1061 * conservative value.
1063 #define GREAT_SNR 30
1065 /* Compare function for sorting scan results. Return >0 if @b is considered
1067 static int wpa_scan_result_compar(const void *a, const void *b)
1069 #define IS_5GHZ(n) (n > 4000)
1070 #define MIN(a,b) a < b ? a : b
1071 struct wpa_scan_res **_wa = (void *) a;
1072 struct wpa_scan_res **_wb = (void *) b;
1073 struct wpa_scan_res *wa = *_wa;
1074 struct wpa_scan_res *wb = *_wb;
1075 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1078 /* WPA/WPA2 support preferred */
1079 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1080 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1081 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1082 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1084 if (wpa_b && !wpa_a)
1086 if (!wpa_b && wpa_a)
1089 /* privacy support preferred */
1090 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1091 (wb->caps & IEEE80211_CAP_PRIVACY))
1093 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1094 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1097 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1098 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1099 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1100 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1102 /* Not suitable information to calculate SNR, so use level */
1107 /* best/max rate preferred if SNR close enough */
1108 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1109 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1110 maxrate_a = wpa_scan_get_max_rate(wa);
1111 maxrate_b = wpa_scan_get_max_rate(wb);
1112 if (maxrate_a != maxrate_b)
1113 return maxrate_b - maxrate_a;
1114 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1115 return IS_5GHZ(wa->freq) ? -1 : 1;
1118 /* use freq for channel preference */
1120 /* all things being equal, use SNR; if SNRs are
1121 * identical, use quality values since some drivers may only report
1122 * that value and leave the signal level zero */
1124 return wb->qual - wa->qual;
1125 return snr_b - snr_a;
1132 /* Compare function for sorting scan results when searching a WPS AP for
1133 * provisioning. Return >0 if @b is considered better. */
1134 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1136 struct wpa_scan_res **_wa = (void *) a;
1137 struct wpa_scan_res **_wb = (void *) b;
1138 struct wpa_scan_res *wa = *_wa;
1139 struct wpa_scan_res *wb = *_wb;
1140 int uses_wps_a, uses_wps_b;
1141 struct wpabuf *wps_a, *wps_b;
1144 /* Optimization - check WPS IE existence before allocated memory and
1145 * doing full reassembly. */
1146 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1147 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1148 if (uses_wps_a && !uses_wps_b)
1150 if (!uses_wps_a && uses_wps_b)
1153 if (uses_wps_a && uses_wps_b) {
1154 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1155 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1156 res = wps_ap_priority_compar(wps_a, wps_b);
1164 * Do not use current AP security policy as a sorting criteria during
1165 * WPS provisioning step since the AP may get reconfigured at the
1166 * completion of provisioning.
1169 /* all things being equal, use signal level; if signal levels are
1170 * identical, use quality values since some drivers may only report
1171 * that value and leave the signal level zero */
1172 if (wb->level == wa->level)
1173 return wb->qual - wa->qual;
1174 return wb->level - wa->level;
1176 #endif /* CONFIG_WPS */
1179 static void dump_scan_res(struct wpa_scan_results *scan_res)
1181 #ifndef CONFIG_NO_STDOUT_DEBUG
1184 if (scan_res->res == NULL || scan_res->num == 0)
1187 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1189 for (i = 0; i < scan_res->num; i++) {
1190 struct wpa_scan_res *r = scan_res->res[i];
1191 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1192 == WPA_SCAN_LEVEL_DBM) {
1193 int snr = r->level - r->noise;
1194 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1195 "noise=%d level=%d snr=%d%s flags=0x%x",
1196 MAC2STR(r->bssid), r->freq, r->qual,
1197 r->noise, r->level, snr,
1198 snr >= GREAT_SNR ? "*" : "", r->flags);
1200 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1201 "noise=%d level=%d flags=0x%x",
1202 MAC2STR(r->bssid), r->freq, r->qual,
1203 r->noise, r->level, r->flags);
1206 #endif /* CONFIG_NO_STDOUT_DEBUG */
1211 * wpa_supplicant_get_scan_results - Get scan results
1212 * @wpa_s: Pointer to wpa_supplicant data
1213 * @info: Information about what was scanned or %NULL if not available
1214 * @new_scan: Whether a new scan was performed
1215 * Returns: Scan results, %NULL on failure
1217 * This function request the current scan results from the driver and updates
1218 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1219 * results with wpa_scan_results_free().
1221 struct wpa_scan_results *
1222 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1223 struct scan_info *info, int new_scan)
1225 struct wpa_scan_results *scan_res;
1227 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1229 scan_res = wpa_drv_get_scan_results2(wpa_s);
1230 if (scan_res == NULL) {
1231 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1236 if (wpas_wps_in_progress(wpa_s)) {
1237 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1238 "provisioning rules");
1239 compar = wpa_scan_result_wps_compar;
1241 #endif /* CONFIG_WPS */
1243 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1245 dump_scan_res(scan_res);
1247 wpa_bss_update_start(wpa_s);
1248 for (i = 0; i < scan_res->num; i++)
1249 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1250 wpa_bss_update_end(wpa_s, info, new_scan);
1256 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1258 struct wpa_scan_results *scan_res;
1259 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1260 if (scan_res == NULL)
1262 wpa_scan_results_free(scan_res);