2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2012, 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"
14 #include "common/wpa_ctrl.h"
16 #include "wpa_supplicant_i.h"
18 #include "wps_supplicant.h"
19 #include "p2p_supplicant.h"
21 #include "hs20_supplicant.h"
24 #include "gas_query.h"
28 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
33 ssid = wpa_supplicant_get_ssid(wpa_s);
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
39 if (wpa_s->current_ssid != NULL)
40 wpas_notify_network_changed(wpa_s);
42 wpa_supplicant_initiate_eapol(wpa_s);
43 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
44 "network - generating associated event");
45 os_memset(&data, 0, sizeof(data));
46 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
51 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
52 enum wps_request_type *req_type)
54 struct wpa_ssid *ssid;
57 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
58 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
62 *req_type = wpas_wps_get_req_type(ssid);
63 if (!ssid->eap.phase1)
66 if (os_strstr(ssid->eap.phase1, "pbc=1"))
71 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
72 !wpa_s->conf->p2p_disabled) {
73 wpa_s->wps->dev.p2p = 1;
76 *req_type = WPS_REQ_ENROLLEE_INFO;
79 #endif /* CONFIG_P2P */
83 #endif /* CONFIG_WPS */
87 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
88 * @wpa_s: Pointer to wpa_supplicant data
89 * Returns: 0 if no networks are enabled, >0 if networks are enabled
91 * This function is used to figure out whether any networks (or Interworking
92 * with enabled credentials and auto_interworking) are present in the current
95 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
97 struct wpa_ssid *ssid = wpa_s->conf->ssid;
98 int count = 0, disabled = 0;
100 if (!wpas_network_disabled(wpa_s, ssid))
106 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107 wpa_s->conf->auto_interworking)
109 if (count == 0 && disabled > 0) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111 "networks)", disabled);
117 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
121 if (!wpas_network_disabled(wpa_s, ssid))
126 /* ap_scan=2 mode - try to associate with each SSID. */
128 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
129 "end of scan list - go back to beginning");
130 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
131 wpa_supplicant_req_scan(wpa_s, 0, 0);
135 /* Continue from the next SSID on the next attempt. */
136 wpa_s->prev_scan_ssid = ssid;
138 /* Start from the beginning of the SSID list. */
139 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
141 wpa_supplicant_associate(wpa_s, NULL, ssid);
145 static int int_array_len(const int *a)
148 for (i = 0; a && a[i]; i++)
154 static void int_array_concat(int **res, const int *a)
159 reslen = int_array_len(*res);
160 alen = int_array_len(a);
162 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
168 for (i = 0; i <= alen; i++)
169 n[reslen + i] = a[i];
174 static int freq_cmp(const void *a, const void *b)
187 static void int_array_sort_unique(int *a)
195 alen = int_array_len(a);
196 qsort(a, alen, sizeof(int), freq_cmp);
200 while (a[i] && a[j]) {
214 * wpa_supplicant_trigger_scan - Request driver to start a scan
215 * @wpa_s: Pointer to wpa_supplicant data
216 * @params: Scan parameters
217 * Returns: 0 on success, -1 on failure
219 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
220 struct wpa_driver_scan_params *params)
224 wpa_supplicant_notify_scanning(wpa_s, 1);
226 ret = wpa_drv_scan(wpa_s, params);
228 wpa_supplicant_notify_scanning(wpa_s, 0);
229 wpas_notify_scan_done(wpa_s, 0);
231 os_get_time(&wpa_s->scan_trigger_time);
233 wpa_s->normal_scans++;
241 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
243 struct wpa_supplicant *wpa_s = eloop_ctx;
245 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
247 if (wpa_supplicant_req_sched_scan(wpa_s))
248 wpa_supplicant_req_scan(wpa_s, 0, 0);
253 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
255 struct wpa_supplicant *wpa_s = eloop_ctx;
257 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
259 wpa_s->sched_scan_timed_out = 1;
260 wpa_supplicant_cancel_sched_scan(wpa_s);
264 int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
265 struct wpa_driver_scan_params *params,
270 wpa_supplicant_notify_scanning(wpa_s, 1);
271 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
273 wpa_supplicant_notify_scanning(wpa_s, 0);
275 wpa_s->sched_scanning = 1;
281 int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
285 ret = wpa_drv_stop_sched_scan(wpa_s);
287 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
288 /* TODO: what to do if stopping fails? */
296 static struct wpa_driver_scan_filter *
297 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
299 struct wpa_driver_scan_filter *ssids;
300 struct wpa_ssid *ssid;
304 if (!conf->filter_ssids)
307 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
308 if (ssid->ssid && ssid->ssid_len)
313 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
317 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
318 if (!ssid->ssid || !ssid->ssid_len)
320 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
321 ssids[*num_ssids].ssid_len = ssid->ssid_len;
329 static void wpa_supplicant_optimize_freqs(
330 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
333 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
335 /* Optimize provisioning state scan based on GO information */
336 if (wpa_s->p2p_in_provisioning < 5 &&
337 wpa_s->go_params->freq > 0) {
338 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
339 "preferred frequency %d MHz",
340 wpa_s->go_params->freq);
341 params->freqs = os_zalloc(2 * sizeof(int));
343 params->freqs[0] = wpa_s->go_params->freq;
344 } else if (wpa_s->p2p_in_provisioning < 8 &&
345 wpa_s->go_params->freq_list[0]) {
346 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
348 int_array_concat(¶ms->freqs,
349 wpa_s->go_params->freq_list);
351 int_array_sort_unique(params->freqs);
353 wpa_s->p2p_in_provisioning++;
355 #endif /* CONFIG_P2P */
358 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
360 * Optimize post-provisioning scan based on channel used
361 * during provisioning.
363 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
364 "that was used during provisioning", wpa_s->wps_freq);
365 params->freqs = os_zalloc(2 * sizeof(int));
367 params->freqs[0] = wpa_s->wps_freq;
371 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
373 /* Optimize provisioning scan based on already known channel */
374 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
376 params->freqs = os_zalloc(2 * sizeof(int));
378 params->freqs[0] = wpa_s->wps_freq;
379 wpa_s->known_wps_freq = 0; /* only do this once */
381 #endif /* CONFIG_WPS */
385 #ifdef CONFIG_INTERWORKING
386 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
389 if (wpa_s->conf->interworking == 0)
392 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
393 wpabuf_put_u8(buf, 4);
394 wpabuf_put_u8(buf, 0x00);
395 wpabuf_put_u8(buf, 0x00);
396 wpabuf_put_u8(buf, 0x00);
397 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
399 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
400 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
402 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
404 if (!is_zero_ether_addr(wpa_s->conf->hessid))
405 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
407 #endif /* CONFIG_INTERWORKING */
410 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
412 struct wpabuf *extra_ie = NULL;
415 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
416 #endif /* CONFIG_WPS */
418 #ifdef CONFIG_INTERWORKING
419 if (wpa_s->conf->interworking &&
420 wpabuf_resize(&extra_ie, 100) == 0)
421 wpas_add_interworking_elements(wpa_s, extra_ie);
422 #endif /* CONFIG_INTERWORKING */
425 wps = wpas_wps_in_use(wpa_s, &req_type);
428 struct wpabuf *wps_ie;
429 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
432 wpa_s->wps->uuid, req_type,
435 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
436 wpabuf_put_buf(extra_ie, wps_ie);
443 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
444 if (wpabuf_resize(&extra_ie, ielen) == 0)
445 wpas_p2p_scan_ie(wpa_s, extra_ie);
447 #endif /* CONFIG_P2P */
449 #endif /* CONFIG_WPS */
452 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
453 wpas_hs20_add_indication(extra_ie);
454 #endif /* CONFIG_HS20 */
463 * Check whether there are any enabled networks or credentials that could be
464 * used for a non-P2P connection.
466 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
468 struct wpa_ssid *ssid;
470 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
471 if (wpas_network_disabled(wpa_s, ssid))
473 if (!ssid->p2p_group)
477 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
478 wpa_s->conf->auto_interworking)
484 #endif /* CONFIG_P2P */
487 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
489 enum hostapd_hw_mode mode)
493 for (i = 0; i < num_modes; i++) {
494 if (modes[i].mode == mode)
502 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
503 enum hostapd_hw_mode band,
504 struct wpa_driver_scan_params *params)
506 /* Include only supported channels for the specified band */
507 struct hostapd_hw_modes *mode;
510 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
512 /* No channels supported in this band - use empty list */
513 params->freqs = os_zalloc(sizeof(int));
517 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
518 if (params->freqs == NULL)
520 for (count = 0, i = 0; i < mode->num_channels; i++) {
521 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
523 params->freqs[count++] = mode->channels[i].freq;
528 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
529 struct wpa_driver_scan_params *params)
531 if (wpa_s->hw.modes == NULL)
532 return; /* unknown what channels the driver supports */
534 return; /* already using a limited channel set */
535 if (wpa_s->setband == WPA_SETBAND_5G)
536 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
538 else if (wpa_s->setband == WPA_SETBAND_2G)
539 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
544 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
546 struct wpa_supplicant *wpa_s = eloop_ctx;
547 struct wpa_ssid *ssid;
548 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
550 struct wpabuf *extra_ie = NULL;
551 struct wpa_driver_scan_params params;
552 struct wpa_driver_scan_params *scan_params;
554 enum wpa_states prev_state;
556 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
557 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
558 wpas_p2p_continue_after_scan(wpa_s);
562 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
563 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
564 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
565 wpas_p2p_continue_after_scan(wpa_s);
569 if (wpa_s->scanning) {
571 * If we are already in scanning state, we shall reschedule the
572 * the incoming scan request.
574 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
575 wpa_supplicant_req_scan(wpa_s, 1, 0);
579 if (!wpa_supplicant_enabled_networks(wpa_s) &&
580 wpa_s->scan_req == NORMAL_SCAN_REQ) {
581 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
582 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
583 wpas_p2p_continue_after_scan(wpa_s);
587 if (wpa_s->conf->ap_scan != 0 &&
588 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
589 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
590 "overriding ap_scan configuration");
591 wpa_s->conf->ap_scan = 0;
592 wpas_notify_ap_scan_changed(wpa_s);
595 if (wpa_s->conf->ap_scan == 0) {
596 wpa_supplicant_gen_assoc_event(wpa_s);
601 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
602 if (wpa_s->sta_scan_pending &&
603 wpas_p2p_in_progress(wpa_s) == 2 &&
604 wpa_s->global->p2p_cb_on_scan_complete) {
605 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
606 "mode scan during P2P search");
608 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
609 "while P2P operation is in progress");
610 wpa_s->sta_scan_pending = 1;
611 wpa_supplicant_req_scan(wpa_s, 5, 0);
615 #endif /* CONFIG_P2P */
618 if (gas_query_in_progress(wpa_s->gas)) {
619 wpa_dbg(wpa_s, MSG_DEBUG, "Delay scan while GAS query is in progress");
620 wpa_supplicant_req_scan(wpa_s, 1, 0);
623 #endif /* CONFIG_GAS */
625 if (wpa_s->conf->ap_scan == 2)
628 max_ssids = wpa_s->max_scan_ssids;
629 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
630 max_ssids = WPAS_MAX_SCAN_SSIDS;
633 scan_req = wpa_s->scan_req;
634 wpa_s->scan_req = NORMAL_SCAN_REQ;
636 os_memset(¶ms, 0, sizeof(params));
638 prev_state = wpa_s->wpa_state;
639 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
640 wpa_s->wpa_state == WPA_INACTIVE)
641 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
644 * If autoscan has set its own scanning parameters
646 if (wpa_s->autoscan_params != NULL) {
647 scan_params = wpa_s->autoscan_params;
651 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
652 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
653 if (ssid == wpa_s->connect_without_scan)
656 wpa_s->connect_without_scan = NULL;
658 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
659 "without scan step");
660 wpa_supplicant_associate(wpa_s, NULL, ssid);
666 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
668 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
669 wpa_s->p2p_in_provisioning,
670 wpa_s->show_group_started);
671 params.ssids[0].ssid = wpa_s->go_params->ssid;
672 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
673 params.num_ssids = 1;
676 #endif /* CONFIG_P2P */
678 /* Find the starting point from which to continue scanning */
679 ssid = wpa_s->conf->ssid;
680 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
682 if (ssid == wpa_s->prev_scan_ssid) {
690 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
691 wpa_s->connect_without_scan = NULL;
692 wpa_s->prev_scan_wildcard = 0;
693 wpa_supplicant_assoc_try(wpa_s, ssid);
695 } else if (wpa_s->conf->ap_scan == 2) {
697 * User-initiated scan request in ap_scan == 2; scan with
702 struct wpa_ssid *start = ssid, *tssid;
704 if (ssid == NULL && max_ssids > 1)
705 ssid = wpa_s->conf->ssid;
707 if (!wpas_network_disabled(wpa_s, ssid) &&
709 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
710 ssid->ssid, ssid->ssid_len);
711 params.ssids[params.num_ssids].ssid =
713 params.ssids[params.num_ssids].ssid_len =
716 if (params.num_ssids + 1 >= max_ssids)
722 if (ssid == NULL && max_ssids > 1 &&
723 start != wpa_s->conf->ssid)
724 ssid = wpa_s->conf->ssid;
727 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
728 if (wpas_network_disabled(wpa_s, tssid))
730 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
731 int_array_concat(¶ms.freqs,
734 os_free(params.freqs);
739 int_array_sort_unique(params.freqs);
742 if (ssid && max_ssids == 1) {
744 * If the driver is limited to 1 SSID at a time interleave
745 * wildcard SSID scans with specific SSID scans to avoid
746 * waiting a long time for a wildcard scan.
748 if (!wpa_s->prev_scan_wildcard) {
749 params.ssids[0].ssid = NULL;
750 params.ssids[0].ssid_len = 0;
751 wpa_s->prev_scan_wildcard = 1;
752 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
753 "wildcard SSID (Interleave with specific)");
755 wpa_s->prev_scan_ssid = ssid;
756 wpa_s->prev_scan_wildcard = 0;
757 wpa_dbg(wpa_s, MSG_DEBUG,
758 "Starting AP scan for specific SSID: %s",
759 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
764 wpa_s->prev_scan_ssid = ssid;
765 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
769 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
771 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
776 #endif /* CONFIG_P2P */
778 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
779 extra_ie = wpa_supplicant_extra_ies(wpa_s);
781 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
782 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
783 "generated frequency list");
784 params.freqs = wpa_s->next_scan_freqs;
786 os_free(wpa_s->next_scan_freqs);
787 wpa_s->next_scan_freqs = NULL;
788 wpa_setband_scan_freqs(wpa_s, ¶ms);
790 /* See if user specified frequencies. If so, scan only those. */
791 if (wpa_s->conf->freq_list && !params.freqs) {
792 wpa_dbg(wpa_s, MSG_DEBUG,
793 "Optimize scan based on conf->freq_list");
794 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
797 /* Use current associated channel? */
798 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
799 unsigned int num = wpa_s->num_multichan_concurrent;
801 params.freqs = os_calloc(num + 1, sizeof(int));
803 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
805 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
806 "current operating channels since "
807 "scan_cur_freq is enabled");
809 os_free(params.freqs);
815 params.filter_ssids = wpa_supplicant_build_filter_ssids(
816 wpa_s->conf, ¶ms.num_filter_ssids);
818 params.extra_ies = wpabuf_head(extra_ie);
819 params.extra_ies_len = wpabuf_len(extra_ie);
823 if (wpa_s->p2p_in_provisioning ||
824 (wpa_s->show_group_started && wpa_s->go_params)) {
826 * The interface may not yet be in P2P mode, so we have to
827 * explicitly request P2P probe to disable CCK rates.
829 params.p2p_probe = 1;
831 #endif /* CONFIG_P2P */
833 scan_params = ¶ms;
838 * If the driver does not support multi-channel concurrency and a
839 * virtual interface that shares the same radio with the wpa_s interface
840 * is operating there may not be need to scan other channels apart from
841 * the current operating channel on the other virtual interface. Filter
842 * out other channels in case we are trying to find a connection for a
843 * station interface when we are not configured to prefer station
844 * connection and a concurrent operation is already in process.
846 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
847 !scan_params->freqs && !params.freqs &&
848 wpas_is_p2p_prioritized(wpa_s) &&
849 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
850 non_p2p_network_enabled(wpa_s)) {
851 unsigned int num = wpa_s->num_multichan_concurrent;
853 params.freqs = os_calloc(num + 1, sizeof(int));
855 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
856 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
857 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
859 os_free(params.freqs);
864 #endif /* CONFIG_P2P */
866 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
868 wpabuf_free(extra_ie);
869 os_free(params.freqs);
870 os_free(params.filter_ssids);
873 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
874 if (prev_state != wpa_s->wpa_state)
875 wpa_supplicant_set_state(wpa_s, prev_state);
876 /* Restore scan_req since we will try to scan again */
877 wpa_s->scan_req = scan_req;
878 wpa_supplicant_req_scan(wpa_s, 1, 0);
880 wpa_s->scan_for_connection = 0;
885 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
887 struct os_time remaining, new_int;
890 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
895 if (cancelled && os_time_before(&remaining, &new_int)) {
896 new_int.sec = remaining.sec;
897 new_int.usec = remaining.usec;
901 eloop_register_timeout(new_int.sec, new_int.usec,
902 wpa_supplicant_scan, wpa_s, NULL);
904 wpa_s->scan_interval = sec;
909 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
910 * @wpa_s: Pointer to wpa_supplicant data
911 * @sec: Number of seconds after which to scan
912 * @usec: Number of microseconds after which to scan
914 * This function is used to schedule a scan for neighboring access points after
915 * the specified time.
917 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
919 if (eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL))
921 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d sec %d usec",
926 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
928 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
929 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
934 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
935 * @wpa_s: Pointer to wpa_supplicant data
936 * @sec: Number of seconds after which to scan
937 * @usec: Number of microseconds after which to scan
938 * Returns: 0 on success or -1 otherwise
940 * This function is used to schedule periodic scans for neighboring
941 * access points after the specified time.
943 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
946 if (!wpa_s->sched_scan_supported)
949 eloop_register_timeout(sec, usec,
950 wpa_supplicant_delayed_sched_scan_timeout,
958 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
959 * @wpa_s: Pointer to wpa_supplicant data
960 * Returns: 0 is sched_scan was started or -1 otherwise
962 * This function is used to schedule periodic scans for neighboring
963 * access points repeating the scan continuously.
965 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
967 struct wpa_driver_scan_params params;
968 struct wpa_driver_scan_params *scan_params;
969 enum wpa_states prev_state;
970 struct wpa_ssid *ssid = NULL;
971 struct wpabuf *extra_ie = NULL;
973 unsigned int max_sched_scan_ssids;
977 if (!wpa_s->sched_scan_supported)
980 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
981 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
983 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
984 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
987 if (wpa_s->sched_scanning) {
988 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
993 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
994 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
995 /* Use wildcard SSID to find this network */
997 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1002 if (!wpas_network_disabled(wpa_s, ssid) &&
1003 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1005 * Normal scan is more reliable and faster for WPS
1006 * operations and since these are for short periods of
1007 * time, the benefit of trying to use sched_scan would
1010 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1011 "sched_scan for WPS");
1014 #endif /* CONFIG_WPS */
1019 if (wpa_s->normal_scans < 3 &&
1020 (need_ssids <= wpa_s->max_scan_ssids ||
1021 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1023 * When normal scan can speed up operations, use that for the
1024 * first operations before starting the sched_scan to allow
1025 * user space sleep more. We do this only if the normal scan
1026 * has functionality that is suitable for this or if the
1027 * sched_scan does not have better support for multiple SSIDs.
1029 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1030 "sched_scan for initial scans (normal_scans=%d)",
1031 wpa_s->normal_scans);
1035 os_memset(¶ms, 0, sizeof(params));
1037 /* If we can't allocate space for the filters, we just don't filter */
1038 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1039 sizeof(struct wpa_driver_scan_filter));
1041 prev_state = wpa_s->wpa_state;
1042 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1043 wpa_s->wpa_state == WPA_INACTIVE)
1044 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1046 if (wpa_s->autoscan_params != NULL) {
1047 scan_params = wpa_s->autoscan_params;
1051 /* Find the starting point from which to continue scanning */
1052 ssid = wpa_s->conf->ssid;
1053 if (wpa_s->prev_sched_ssid) {
1055 if (ssid == wpa_s->prev_sched_ssid) {
1063 if (!ssid || !wpa_s->prev_sched_ssid) {
1064 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1065 if (wpa_s->conf->sched_scan_interval)
1066 wpa_s->sched_scan_interval =
1067 wpa_s->conf->sched_scan_interval;
1068 if (wpa_s->sched_scan_interval == 0)
1069 wpa_s->sched_scan_interval = 10;
1070 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1071 wpa_s->first_sched_scan = 1;
1072 ssid = wpa_s->conf->ssid;
1073 wpa_s->prev_sched_ssid = ssid;
1077 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1082 if (wpas_network_disabled(wpa_s, ssid))
1085 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1086 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1087 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1088 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1089 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1090 ssid->ssid, ssid->ssid_len);
1091 params.filter_ssids[params.num_filter_ssids].ssid_len =
1093 params.num_filter_ssids++;
1094 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1096 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1097 "filter for sched_scan - drop filter");
1098 os_free(params.filter_ssids);
1099 params.filter_ssids = NULL;
1100 params.num_filter_ssids = 0;
1103 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1104 if (params.num_ssids == max_sched_scan_ssids)
1105 break; /* only room for broadcast SSID */
1106 wpa_dbg(wpa_s, MSG_DEBUG,
1107 "add to active scan ssid: %s",
1108 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1109 params.ssids[params.num_ssids].ssid =
1111 params.ssids[params.num_ssids].ssid_len =
1114 if (params.num_ssids >= max_sched_scan_ssids) {
1115 wpa_s->prev_sched_ssid = ssid;
1119 (wpas_network_disabled(wpa_s, ssid) ||
1126 wpa_s->prev_sched_ssid = ssid;
1130 if (params.num_filter_ssids == 0) {
1131 os_free(params.filter_ssids);
1132 params.filter_ssids = NULL;
1135 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1137 params.extra_ies = wpabuf_head(extra_ie);
1138 params.extra_ies_len = wpabuf_len(extra_ie);
1141 scan_params = ¶ms;
1144 if (ssid || !wpa_s->first_sched_scan) {
1145 wpa_dbg(wpa_s, MSG_DEBUG,
1146 "Starting sched scan: interval %d timeout %d",
1147 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1149 wpa_dbg(wpa_s, MSG_DEBUG,
1150 "Starting sched scan: interval %d (no timeout)",
1151 wpa_s->sched_scan_interval);
1154 wpa_setband_scan_freqs(wpa_s, scan_params);
1156 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1157 wpa_s->sched_scan_interval);
1158 wpabuf_free(extra_ie);
1159 os_free(params.filter_ssids);
1161 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1162 if (prev_state != wpa_s->wpa_state)
1163 wpa_supplicant_set_state(wpa_s, prev_state);
1167 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1168 if (ssid || !wpa_s->first_sched_scan) {
1169 wpa_s->sched_scan_timed_out = 0;
1170 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1171 wpa_supplicant_sched_scan_timeout,
1173 wpa_s->first_sched_scan = 0;
1174 wpa_s->sched_scan_timeout /= 2;
1175 wpa_s->sched_scan_interval *= 2;
1176 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1177 wpa_s->sched_scan_interval = 10;
1178 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1182 /* If there is no more ssids, start next time from the beginning */
1184 wpa_s->prev_sched_ssid = NULL;
1191 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1192 * @wpa_s: Pointer to wpa_supplicant data
1194 * This function is used to cancel a scan request scheduled with
1195 * wpa_supplicant_req_scan().
1197 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1199 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1200 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1201 wpas_p2p_continue_after_scan(wpa_s);
1206 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1207 * @wpa_s: Pointer to wpa_supplicant data
1209 * This function is used to stop a delayed scheduled scan.
1211 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1213 if (!wpa_s->sched_scan_supported)
1216 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1217 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1223 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1224 * @wpa_s: Pointer to wpa_supplicant data
1226 * This function is used to stop a periodic scheduled scan.
1228 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1230 if (!wpa_s->sched_scanning)
1233 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1234 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1235 wpa_supplicant_stop_sched_scan(wpa_s);
1240 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1241 * @wpa_s: Pointer to wpa_supplicant data
1242 * @scanning: Whether scanning is currently in progress
1244 * This function is to generate scanning notifycations. It is called whenever
1245 * there may have been a change in scanning (scan started, completed, stopped).
1246 * wpas_notify_scanning() is called whenever the scanning state changed from the
1247 * previously notified state.
1249 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1252 if (wpa_s->scanning != scanning) {
1253 wpa_s->scanning = scanning;
1254 wpas_notify_scanning(wpa_s);
1259 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1265 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1266 for (i = 0; ie && i < ie[1]; i++) {
1267 if ((ie[i + 2] & 0x7f) > rate)
1268 rate = ie[i + 2] & 0x7f;
1271 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1272 for (i = 0; ie && i < ie[1]; i++) {
1273 if ((ie[i + 2] & 0x7f) > rate)
1274 rate = ie[i + 2] & 0x7f;
1282 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1283 * @res: Scan result entry
1284 * @ie: Information element identitifier (WLAN_EID_*)
1285 * Returns: Pointer to the information element (id field) or %NULL if not found
1287 * This function returns the first matching information element in the scan
1290 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1292 const u8 *end, *pos;
1294 pos = (const u8 *) (res + 1);
1295 end = pos + res->ie_len;
1297 while (pos + 1 < end) {
1298 if (pos + 2 + pos[1] > end)
1310 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1311 * @res: Scan result entry
1312 * @vendor_type: Vendor type (four octets starting the IE payload)
1313 * Returns: Pointer to the information element (id field) or %NULL if not found
1315 * This function returns the first matching information element in the scan
1318 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1321 const u8 *end, *pos;
1323 pos = (const u8 *) (res + 1);
1324 end = pos + res->ie_len;
1326 while (pos + 1 < end) {
1327 if (pos + 2 + pos[1] > end)
1329 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1330 vendor_type == WPA_GET_BE32(&pos[2]))
1340 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1341 * @res: Scan result entry
1342 * @vendor_type: Vendor type (four octets starting the IE payload)
1343 * Returns: Pointer to the information element (id field) or %NULL if not found
1345 * This function returns the first matching information element in the scan
1348 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1349 * from Beacon frames instead of either Beacon or Probe Response frames.
1351 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1354 const u8 *end, *pos;
1356 if (res->beacon_ie_len == 0)
1359 pos = (const u8 *) (res + 1);
1361 end = pos + res->beacon_ie_len;
1363 while (pos + 1 < end) {
1364 if (pos + 2 + pos[1] > end)
1366 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1367 vendor_type == WPA_GET_BE32(&pos[2]))
1377 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1378 * @res: Scan result entry
1379 * @vendor_type: Vendor type (four octets starting the IE payload)
1380 * Returns: Pointer to the information element payload or %NULL if not found
1382 * This function returns concatenated payload of possibly fragmented vendor
1383 * specific information elements in the scan result. The caller is responsible
1384 * for freeing the returned buffer.
1386 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1390 const u8 *end, *pos;
1392 buf = wpabuf_alloc(res->ie_len);
1396 pos = (const u8 *) (res + 1);
1397 end = pos + res->ie_len;
1399 while (pos + 1 < end) {
1400 if (pos + 2 + pos[1] > end)
1402 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1403 vendor_type == WPA_GET_BE32(&pos[2]))
1404 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1408 if (wpabuf_len(buf) == 0) {
1418 * Channels with a great SNR can operate at full rate. What is a great SNR?
1419 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1420 * rule of thumb is that any SNR above 20 is good." This one
1421 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1422 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1423 * conservative value.
1425 #define GREAT_SNR 30
1427 /* Compare function for sorting scan results. Return >0 if @b is considered
1429 static int wpa_scan_result_compar(const void *a, const void *b)
1431 #define IS_5GHZ(n) (n > 4000)
1432 #define MIN(a,b) a < b ? a : b
1433 struct wpa_scan_res **_wa = (void *) a;
1434 struct wpa_scan_res **_wb = (void *) b;
1435 struct wpa_scan_res *wa = *_wa;
1436 struct wpa_scan_res *wb = *_wb;
1437 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1440 /* WPA/WPA2 support preferred */
1441 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1442 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1443 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1444 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1446 if (wpa_b && !wpa_a)
1448 if (!wpa_b && wpa_a)
1451 /* privacy support preferred */
1452 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1453 (wb->caps & IEEE80211_CAP_PRIVACY))
1455 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1456 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1459 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1460 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1461 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1462 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1464 /* Not suitable information to calculate SNR, so use level */
1469 /* best/max rate preferred if SNR close enough */
1470 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1471 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1472 maxrate_a = wpa_scan_get_max_rate(wa);
1473 maxrate_b = wpa_scan_get_max_rate(wb);
1474 if (maxrate_a != maxrate_b)
1475 return maxrate_b - maxrate_a;
1476 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1477 return IS_5GHZ(wa->freq) ? -1 : 1;
1480 /* use freq for channel preference */
1482 /* all things being equal, use SNR; if SNRs are
1483 * identical, use quality values since some drivers may only report
1484 * that value and leave the signal level zero */
1486 return wb->qual - wa->qual;
1487 return snr_b - snr_a;
1494 /* Compare function for sorting scan results when searching a WPS AP for
1495 * provisioning. Return >0 if @b is considered better. */
1496 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1498 struct wpa_scan_res **_wa = (void *) a;
1499 struct wpa_scan_res **_wb = (void *) b;
1500 struct wpa_scan_res *wa = *_wa;
1501 struct wpa_scan_res *wb = *_wb;
1502 int uses_wps_a, uses_wps_b;
1503 struct wpabuf *wps_a, *wps_b;
1506 /* Optimization - check WPS IE existence before allocated memory and
1507 * doing full reassembly. */
1508 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1509 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1510 if (uses_wps_a && !uses_wps_b)
1512 if (!uses_wps_a && uses_wps_b)
1515 if (uses_wps_a && uses_wps_b) {
1516 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1517 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1518 res = wps_ap_priority_compar(wps_a, wps_b);
1526 * Do not use current AP security policy as a sorting criteria during
1527 * WPS provisioning step since the AP may get reconfigured at the
1528 * completion of provisioning.
1531 /* all things being equal, use signal level; if signal levels are
1532 * identical, use quality values since some drivers may only report
1533 * that value and leave the signal level zero */
1534 if (wb->level == wa->level)
1535 return wb->qual - wa->qual;
1536 return wb->level - wa->level;
1538 #endif /* CONFIG_WPS */
1541 static void dump_scan_res(struct wpa_scan_results *scan_res)
1543 #ifndef CONFIG_NO_STDOUT_DEBUG
1546 if (scan_res->res == NULL || scan_res->num == 0)
1549 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1551 for (i = 0; i < scan_res->num; i++) {
1552 struct wpa_scan_res *r = scan_res->res[i];
1554 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1555 == WPA_SCAN_LEVEL_DBM) {
1556 int snr = r->level - r->noise;
1557 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1558 "noise=%d level=%d snr=%d%s flags=0x%x "
1560 MAC2STR(r->bssid), r->freq, r->qual,
1561 r->noise, r->level, snr,
1562 snr >= GREAT_SNR ? "*" : "", r->flags,
1565 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1566 "noise=%d level=%d flags=0x%x age=%u",
1567 MAC2STR(r->bssid), r->freq, r->qual,
1568 r->noise, r->level, r->flags, r->age);
1570 pos = (u8 *) (r + 1);
1572 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1574 if (r->beacon_ie_len)
1575 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1576 pos, r->beacon_ie_len);
1578 #endif /* CONFIG_NO_STDOUT_DEBUG */
1583 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1584 * @wpa_s: Pointer to wpa_supplicant data
1585 * @bssid: BSSID to check
1586 * Returns: 0 if the BSSID is filtered or 1 if not
1588 * This function is used to filter out specific BSSIDs from scan reslts mainly
1589 * for testing purposes (SET bssid_filter ctrl_iface command).
1591 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1596 if (wpa_s->bssid_filter == NULL)
1599 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1600 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1609 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1610 struct wpa_scan_results *res)
1614 if (wpa_s->bssid_filter == NULL)
1617 for (i = 0, j = 0; i < res->num; i++) {
1618 if (wpa_supplicant_filter_bssid_match(wpa_s,
1619 res->res[i]->bssid)) {
1620 res->res[j++] = res->res[i];
1622 os_free(res->res[i]);
1627 if (res->num != j) {
1628 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1629 (int) (res->num - j));
1636 * wpa_supplicant_get_scan_results - Get scan results
1637 * @wpa_s: Pointer to wpa_supplicant data
1638 * @info: Information about what was scanned or %NULL if not available
1639 * @new_scan: Whether a new scan was performed
1640 * Returns: Scan results, %NULL on failure
1642 * This function request the current scan results from the driver and updates
1643 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1644 * results with wpa_scan_results_free().
1646 struct wpa_scan_results *
1647 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1648 struct scan_info *info, int new_scan)
1650 struct wpa_scan_results *scan_res;
1652 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1654 scan_res = wpa_drv_get_scan_results2(wpa_s);
1655 if (scan_res == NULL) {
1656 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1659 if (scan_res->fetch_time.sec == 0) {
1661 * Make sure we have a valid timestamp if the driver wrapper
1662 * does not set this.
1664 os_get_time(&scan_res->fetch_time);
1666 filter_scan_res(wpa_s, scan_res);
1669 if (wpas_wps_in_progress(wpa_s)) {
1670 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1671 "provisioning rules");
1672 compar = wpa_scan_result_wps_compar;
1674 #endif /* CONFIG_WPS */
1676 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1678 dump_scan_res(scan_res);
1680 wpa_bss_update_start(wpa_s);
1681 for (i = 0; i < scan_res->num; i++)
1682 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1683 &scan_res->fetch_time);
1684 wpa_bss_update_end(wpa_s, info, new_scan);
1691 * wpa_supplicant_update_scan_results - Update scan results from the driver
1692 * @wpa_s: Pointer to wpa_supplicant data
1693 * Returns: 0 on success, -1 on failure
1695 * This function updates the BSS table within wpa_supplicant based on the
1696 * currently available scan results from the driver without requesting a new
1697 * scan. This is used in cases where the driver indicates an association
1698 * (including roaming within ESS) and wpa_supplicant does not yet have the
1699 * needed information to complete the connection (e.g., to perform validation
1700 * steps in 4-way handshake).
1702 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1704 struct wpa_scan_results *scan_res;
1705 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1706 if (scan_res == NULL)
1708 wpa_scan_results_free(scan_res);
1715 * scan_only_handler - Reports scan results
1717 void scan_only_handler(struct wpa_supplicant *wpa_s,
1718 struct wpa_scan_results *scan_res)
1720 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1721 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1722 wpas_notify_scan_results(wpa_s);
1723 wpas_notify_scan_done(wpa_s, 1);
1727 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1729 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);