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);
146 * wpa_supplicant_trigger_scan - Request driver to start a scan
147 * @wpa_s: Pointer to wpa_supplicant data
148 * @params: Scan parameters
149 * Returns: 0 on success, -1 on failure
151 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
152 struct wpa_driver_scan_params *params)
156 wpa_supplicant_notify_scanning(wpa_s, 1);
158 ret = wpa_drv_scan(wpa_s, params);
160 wpa_supplicant_notify_scanning(wpa_s, 0);
161 wpas_notify_scan_done(wpa_s, 0);
163 os_get_reltime(&wpa_s->scan_trigger_time);
165 wpa_s->normal_scans++;
173 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
175 struct wpa_supplicant *wpa_s = eloop_ctx;
177 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
179 if (wpa_supplicant_req_sched_scan(wpa_s))
180 wpa_supplicant_req_scan(wpa_s, 0, 0);
185 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
187 struct wpa_supplicant *wpa_s = eloop_ctx;
189 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
191 wpa_s->sched_scan_timed_out = 1;
192 wpa_supplicant_cancel_sched_scan(wpa_s);
196 int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
197 struct wpa_driver_scan_params *params,
202 wpa_supplicant_notify_scanning(wpa_s, 1);
203 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
205 wpa_supplicant_notify_scanning(wpa_s, 0);
207 wpa_s->sched_scanning = 1;
213 int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
217 ret = wpa_drv_stop_sched_scan(wpa_s);
219 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
220 /* TODO: what to do if stopping fails? */
228 static struct wpa_driver_scan_filter *
229 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
231 struct wpa_driver_scan_filter *ssids;
232 struct wpa_ssid *ssid;
236 if (!conf->filter_ssids)
239 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
240 if (ssid->ssid && ssid->ssid_len)
245 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
249 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
250 if (!ssid->ssid || !ssid->ssid_len)
252 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
253 ssids[*num_ssids].ssid_len = ssid->ssid_len;
261 static void wpa_supplicant_optimize_freqs(
262 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
265 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
267 /* Optimize provisioning state scan based on GO information */
268 if (wpa_s->p2p_in_provisioning < 5 &&
269 wpa_s->go_params->freq > 0) {
270 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
271 "preferred frequency %d MHz",
272 wpa_s->go_params->freq);
273 params->freqs = os_zalloc(2 * sizeof(int));
275 params->freqs[0] = wpa_s->go_params->freq;
276 } else if (wpa_s->p2p_in_provisioning < 8 &&
277 wpa_s->go_params->freq_list[0]) {
278 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
280 int_array_concat(¶ms->freqs,
281 wpa_s->go_params->freq_list);
283 int_array_sort_unique(params->freqs);
285 wpa_s->p2p_in_provisioning++;
287 #endif /* CONFIG_P2P */
290 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
292 * Optimize post-provisioning scan based on channel used
293 * during provisioning.
295 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
296 "that was used during provisioning", wpa_s->wps_freq);
297 params->freqs = os_zalloc(2 * sizeof(int));
299 params->freqs[0] = wpa_s->wps_freq;
303 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
305 /* Optimize provisioning scan based on already known channel */
306 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
308 params->freqs = os_zalloc(2 * sizeof(int));
310 params->freqs[0] = wpa_s->wps_freq;
311 wpa_s->known_wps_freq = 0; /* only do this once */
313 #endif /* CONFIG_WPS */
317 #ifdef CONFIG_INTERWORKING
318 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
321 if (wpa_s->conf->interworking == 0)
324 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
325 wpabuf_put_u8(buf, 4);
326 wpabuf_put_u8(buf, 0x00);
327 wpabuf_put_u8(buf, 0x00);
328 wpabuf_put_u8(buf, 0x00);
329 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
331 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
332 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
334 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
336 if (!is_zero_ether_addr(wpa_s->conf->hessid))
337 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
339 #endif /* CONFIG_INTERWORKING */
342 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
344 struct wpabuf *extra_ie = NULL;
347 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
348 #endif /* CONFIG_WPS */
350 #ifdef CONFIG_INTERWORKING
351 if (wpa_s->conf->interworking &&
352 wpabuf_resize(&extra_ie, 100) == 0)
353 wpas_add_interworking_elements(wpa_s, extra_ie);
354 #endif /* CONFIG_INTERWORKING */
357 wps = wpas_wps_in_use(wpa_s, &req_type);
360 struct wpabuf *wps_ie;
361 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
364 wpa_s->wps->uuid, req_type,
367 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
368 wpabuf_put_buf(extra_ie, wps_ie);
375 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
376 if (wpabuf_resize(&extra_ie, ielen) == 0)
377 wpas_p2p_scan_ie(wpa_s, extra_ie);
379 #endif /* CONFIG_P2P */
381 #endif /* CONFIG_WPS */
384 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
385 wpas_hs20_add_indication(extra_ie);
386 #endif /* CONFIG_HS20 */
395 * Check whether there are any enabled networks or credentials that could be
396 * used for a non-P2P connection.
398 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
400 struct wpa_ssid *ssid;
402 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
403 if (wpas_network_disabled(wpa_s, ssid))
405 if (!ssid->p2p_group)
409 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
410 wpa_s->conf->auto_interworking)
416 #endif /* CONFIG_P2P */
419 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
421 enum hostapd_hw_mode mode)
425 for (i = 0; i < num_modes; i++) {
426 if (modes[i].mode == mode)
434 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
435 enum hostapd_hw_mode band,
436 struct wpa_driver_scan_params *params)
438 /* Include only supported channels for the specified band */
439 struct hostapd_hw_modes *mode;
442 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
444 /* No channels supported in this band - use empty list */
445 params->freqs = os_zalloc(sizeof(int));
449 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
450 if (params->freqs == NULL)
452 for (count = 0, i = 0; i < mode->num_channels; i++) {
453 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
455 params->freqs[count++] = mode->channels[i].freq;
460 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
461 struct wpa_driver_scan_params *params)
463 if (wpa_s->hw.modes == NULL)
464 return; /* unknown what channels the driver supports */
466 return; /* already using a limited channel set */
467 if (wpa_s->setband == WPA_SETBAND_5G)
468 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
470 else if (wpa_s->setband == WPA_SETBAND_2G)
471 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
476 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
478 struct wpa_supplicant *wpa_s = eloop_ctx;
479 struct wpa_ssid *ssid;
481 struct wpabuf *extra_ie = NULL;
482 struct wpa_driver_scan_params params;
483 struct wpa_driver_scan_params *scan_params;
485 enum wpa_states prev_state;
487 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
488 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
489 wpas_p2p_continue_after_scan(wpa_s);
493 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
494 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
495 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
496 wpas_p2p_continue_after_scan(wpa_s);
500 if (wpa_s->scanning) {
502 * If we are already in scanning state, we shall reschedule the
503 * the incoming scan request.
505 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
506 wpa_supplicant_req_scan(wpa_s, 1, 0);
510 if (!wpa_supplicant_enabled_networks(wpa_s) &&
511 wpa_s->scan_req == NORMAL_SCAN_REQ) {
512 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
513 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
514 wpas_p2p_continue_after_scan(wpa_s);
518 if (wpa_s->conf->ap_scan != 0 &&
519 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
520 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
521 "overriding ap_scan configuration");
522 wpa_s->conf->ap_scan = 0;
523 wpas_notify_ap_scan_changed(wpa_s);
526 if (wpa_s->conf->ap_scan == 0) {
527 wpa_supplicant_gen_assoc_event(wpa_s);
532 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
533 if (wpa_s->sta_scan_pending &&
534 wpas_p2p_in_progress(wpa_s) == 2 &&
535 wpa_s->global->p2p_cb_on_scan_complete) {
536 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
537 "mode scan during P2P search");
539 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
540 "while P2P operation is in progress");
541 wpa_s->sta_scan_pending = 1;
542 wpa_supplicant_req_scan(wpa_s, 5, 0);
546 #endif /* CONFIG_P2P */
549 if (gas_query_in_progress(wpa_s->gas)) {
550 wpa_dbg(wpa_s, MSG_DEBUG, "Delay scan while GAS query is in progress");
551 wpa_supplicant_req_scan(wpa_s, 1, 0);
554 #endif /* CONFIG_GAS */
556 if (wpa_s->conf->ap_scan == 2)
559 max_ssids = wpa_s->max_scan_ssids;
560 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
561 max_ssids = WPAS_MAX_SCAN_SSIDS;
564 wpa_s->last_scan_req = wpa_s->scan_req;
565 wpa_s->scan_req = NORMAL_SCAN_REQ;
567 os_memset(¶ms, 0, sizeof(params));
569 prev_state = wpa_s->wpa_state;
570 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
571 wpa_s->wpa_state == WPA_INACTIVE)
572 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
575 * If autoscan has set its own scanning parameters
577 if (wpa_s->autoscan_params != NULL) {
578 scan_params = wpa_s->autoscan_params;
582 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
583 wpa_s->connect_without_scan) {
584 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
585 if (ssid == wpa_s->connect_without_scan)
588 wpa_s->connect_without_scan = NULL;
590 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
591 "without scan step");
592 wpa_supplicant_associate(wpa_s, NULL, ssid);
598 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
600 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
601 wpa_s->p2p_in_provisioning,
602 wpa_s->show_group_started);
603 params.ssids[0].ssid = wpa_s->go_params->ssid;
604 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
605 params.num_ssids = 1;
608 #endif /* CONFIG_P2P */
610 /* Find the starting point from which to continue scanning */
611 ssid = wpa_s->conf->ssid;
612 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
614 if (ssid == wpa_s->prev_scan_ssid) {
622 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
623 wpa_s->conf->ap_scan == 2) {
624 wpa_s->connect_without_scan = NULL;
625 wpa_s->prev_scan_wildcard = 0;
626 wpa_supplicant_assoc_try(wpa_s, ssid);
628 } else if (wpa_s->conf->ap_scan == 2) {
630 * User-initiated scan request in ap_scan == 2; scan with
635 struct wpa_ssid *start = ssid, *tssid;
637 if (ssid == NULL && max_ssids > 1)
638 ssid = wpa_s->conf->ssid;
640 if (!wpas_network_disabled(wpa_s, ssid) &&
642 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
643 ssid->ssid, ssid->ssid_len);
644 params.ssids[params.num_ssids].ssid =
646 params.ssids[params.num_ssids].ssid_len =
649 if (params.num_ssids + 1 >= max_ssids)
655 if (ssid == NULL && max_ssids > 1 &&
656 start != wpa_s->conf->ssid)
657 ssid = wpa_s->conf->ssid;
660 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
661 if (wpas_network_disabled(wpa_s, tssid))
663 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
664 int_array_concat(¶ms.freqs,
667 os_free(params.freqs);
672 int_array_sort_unique(params.freqs);
675 if (ssid && max_ssids == 1) {
677 * If the driver is limited to 1 SSID at a time interleave
678 * wildcard SSID scans with specific SSID scans to avoid
679 * waiting a long time for a wildcard scan.
681 if (!wpa_s->prev_scan_wildcard) {
682 params.ssids[0].ssid = NULL;
683 params.ssids[0].ssid_len = 0;
684 wpa_s->prev_scan_wildcard = 1;
685 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
686 "wildcard SSID (Interleave with specific)");
688 wpa_s->prev_scan_ssid = ssid;
689 wpa_s->prev_scan_wildcard = 0;
690 wpa_dbg(wpa_s, MSG_DEBUG,
691 "Starting AP scan for specific SSID: %s",
692 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
697 wpa_s->prev_scan_ssid = ssid;
698 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
701 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
702 wpa_s->manual_scan_passive && params.num_ssids == 0) {
703 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
705 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
707 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
712 #endif /* CONFIG_P2P */
714 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
715 extra_ie = wpa_supplicant_extra_ies(wpa_s);
717 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
718 wpa_s->manual_scan_freqs) {
719 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
720 params.freqs = wpa_s->manual_scan_freqs;
721 wpa_s->manual_scan_freqs = NULL;
724 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
725 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
726 "generated frequency list");
727 params.freqs = wpa_s->next_scan_freqs;
729 os_free(wpa_s->next_scan_freqs);
730 wpa_s->next_scan_freqs = NULL;
731 wpa_setband_scan_freqs(wpa_s, ¶ms);
733 /* See if user specified frequencies. If so, scan only those. */
734 if (wpa_s->conf->freq_list && !params.freqs) {
735 wpa_dbg(wpa_s, MSG_DEBUG,
736 "Optimize scan based on conf->freq_list");
737 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
740 /* Use current associated channel? */
741 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
742 unsigned int num = wpa_s->num_multichan_concurrent;
744 params.freqs = os_calloc(num + 1, sizeof(int));
746 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
748 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
749 "current operating channels since "
750 "scan_cur_freq is enabled");
752 os_free(params.freqs);
758 params.filter_ssids = wpa_supplicant_build_filter_ssids(
759 wpa_s->conf, ¶ms.num_filter_ssids);
761 params.extra_ies = wpabuf_head(extra_ie);
762 params.extra_ies_len = wpabuf_len(extra_ie);
766 if (wpa_s->p2p_in_provisioning ||
767 (wpa_s->show_group_started && wpa_s->go_params)) {
769 * The interface may not yet be in P2P mode, so we have to
770 * explicitly request P2P probe to disable CCK rates.
772 params.p2p_probe = 1;
774 #endif /* CONFIG_P2P */
776 scan_params = ¶ms;
781 * If the driver does not support multi-channel concurrency and a
782 * virtual interface that shares the same radio with the wpa_s interface
783 * is operating there may not be need to scan other channels apart from
784 * the current operating channel on the other virtual interface. Filter
785 * out other channels in case we are trying to find a connection for a
786 * station interface when we are not configured to prefer station
787 * connection and a concurrent operation is already in process.
789 if (wpa_s->scan_for_connection &&
790 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
791 !scan_params->freqs && !params.freqs &&
792 wpas_is_p2p_prioritized(wpa_s) &&
793 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
794 non_p2p_network_enabled(wpa_s)) {
795 unsigned int num = wpa_s->num_multichan_concurrent;
797 params.freqs = os_calloc(num + 1, sizeof(int));
799 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
800 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
801 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
803 os_free(params.freqs);
808 #endif /* CONFIG_P2P */
810 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
812 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
813 !wpa_s->manual_scan_freqs) {
814 /* Restore manual_scan_freqs for the next attempt */
815 wpa_s->manual_scan_freqs = params.freqs;
819 wpabuf_free(extra_ie);
820 os_free(params.freqs);
821 os_free(params.filter_ssids);
824 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
825 if (prev_state != wpa_s->wpa_state)
826 wpa_supplicant_set_state(wpa_s, prev_state);
827 /* Restore scan_req since we will try to scan again */
828 wpa_s->scan_req = wpa_s->last_scan_req;
829 wpa_supplicant_req_scan(wpa_s, 1, 0);
831 wpa_s->scan_for_connection = 0;
836 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
838 struct os_reltime remaining, new_int;
841 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
846 if (cancelled && os_reltime_before(&remaining, &new_int)) {
847 new_int.sec = remaining.sec;
848 new_int.usec = remaining.usec;
852 eloop_register_timeout(new_int.sec, new_int.usec,
853 wpa_supplicant_scan, wpa_s, NULL);
855 wpa_s->scan_interval = sec;
860 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
861 * @wpa_s: Pointer to wpa_supplicant data
862 * @sec: Number of seconds after which to scan
863 * @usec: Number of microseconds after which to scan
865 * This function is used to schedule a scan for neighboring access points after
866 * the specified time.
868 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
870 if (eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL))
872 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d sec %d usec",
877 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
879 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
880 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
885 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
886 * @wpa_s: Pointer to wpa_supplicant data
887 * @sec: Number of seconds after which to scan
888 * @usec: Number of microseconds after which to scan
889 * Returns: 0 on success or -1 otherwise
891 * This function is used to schedule periodic scans for neighboring
892 * access points after the specified time.
894 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
897 if (!wpa_s->sched_scan_supported)
900 eloop_register_timeout(sec, usec,
901 wpa_supplicant_delayed_sched_scan_timeout,
909 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
910 * @wpa_s: Pointer to wpa_supplicant data
911 * Returns: 0 is sched_scan was started or -1 otherwise
913 * This function is used to schedule periodic scans for neighboring
914 * access points repeating the scan continuously.
916 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
918 struct wpa_driver_scan_params params;
919 struct wpa_driver_scan_params *scan_params;
920 enum wpa_states prev_state;
921 struct wpa_ssid *ssid = NULL;
922 struct wpabuf *extra_ie = NULL;
924 unsigned int max_sched_scan_ssids;
928 if (!wpa_s->sched_scan_supported)
931 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
932 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
934 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
935 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
938 if (wpa_s->sched_scanning) {
939 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
944 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
945 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
946 /* Use wildcard SSID to find this network */
948 } else if (!wpas_network_disabled(wpa_s, ssid) &&
953 if (!wpas_network_disabled(wpa_s, ssid) &&
954 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
956 * Normal scan is more reliable and faster for WPS
957 * operations and since these are for short periods of
958 * time, the benefit of trying to use sched_scan would
961 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
962 "sched_scan for WPS");
965 #endif /* CONFIG_WPS */
970 if (wpa_s->normal_scans < 3 &&
971 (need_ssids <= wpa_s->max_scan_ssids ||
972 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
974 * When normal scan can speed up operations, use that for the
975 * first operations before starting the sched_scan to allow
976 * user space sleep more. We do this only if the normal scan
977 * has functionality that is suitable for this or if the
978 * sched_scan does not have better support for multiple SSIDs.
980 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
981 "sched_scan for initial scans (normal_scans=%d)",
982 wpa_s->normal_scans);
986 os_memset(¶ms, 0, sizeof(params));
988 /* If we can't allocate space for the filters, we just don't filter */
989 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
990 sizeof(struct wpa_driver_scan_filter));
992 prev_state = wpa_s->wpa_state;
993 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
994 wpa_s->wpa_state == WPA_INACTIVE)
995 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
997 if (wpa_s->autoscan_params != NULL) {
998 scan_params = wpa_s->autoscan_params;
1002 /* Find the starting point from which to continue scanning */
1003 ssid = wpa_s->conf->ssid;
1004 if (wpa_s->prev_sched_ssid) {
1006 if (ssid == wpa_s->prev_sched_ssid) {
1014 if (!ssid || !wpa_s->prev_sched_ssid) {
1015 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1016 if (wpa_s->conf->sched_scan_interval)
1017 wpa_s->sched_scan_interval =
1018 wpa_s->conf->sched_scan_interval;
1019 if (wpa_s->sched_scan_interval == 0)
1020 wpa_s->sched_scan_interval = 10;
1021 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1022 wpa_s->first_sched_scan = 1;
1023 ssid = wpa_s->conf->ssid;
1024 wpa_s->prev_sched_ssid = ssid;
1028 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1033 if (wpas_network_disabled(wpa_s, ssid))
1036 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1037 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1038 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1039 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1040 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1041 ssid->ssid, ssid->ssid_len);
1042 params.filter_ssids[params.num_filter_ssids].ssid_len =
1044 params.num_filter_ssids++;
1045 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1047 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1048 "filter for sched_scan - drop filter");
1049 os_free(params.filter_ssids);
1050 params.filter_ssids = NULL;
1051 params.num_filter_ssids = 0;
1054 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1055 if (params.num_ssids == max_sched_scan_ssids)
1056 break; /* only room for broadcast SSID */
1057 wpa_dbg(wpa_s, MSG_DEBUG,
1058 "add to active scan ssid: %s",
1059 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1060 params.ssids[params.num_ssids].ssid =
1062 params.ssids[params.num_ssids].ssid_len =
1065 if (params.num_ssids >= max_sched_scan_ssids) {
1066 wpa_s->prev_sched_ssid = ssid;
1070 (wpas_network_disabled(wpa_s, ssid) ||
1077 wpa_s->prev_sched_ssid = ssid;
1081 if (params.num_filter_ssids == 0) {
1082 os_free(params.filter_ssids);
1083 params.filter_ssids = NULL;
1086 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1088 params.extra_ies = wpabuf_head(extra_ie);
1089 params.extra_ies_len = wpabuf_len(extra_ie);
1092 scan_params = ¶ms;
1095 if (ssid || !wpa_s->first_sched_scan) {
1096 wpa_dbg(wpa_s, MSG_DEBUG,
1097 "Starting sched scan: interval %d timeout %d",
1098 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1100 wpa_dbg(wpa_s, MSG_DEBUG,
1101 "Starting sched scan: interval %d (no timeout)",
1102 wpa_s->sched_scan_interval);
1105 wpa_setband_scan_freqs(wpa_s, scan_params);
1107 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1108 wpa_s->sched_scan_interval);
1109 wpabuf_free(extra_ie);
1110 os_free(params.filter_ssids);
1112 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1113 if (prev_state != wpa_s->wpa_state)
1114 wpa_supplicant_set_state(wpa_s, prev_state);
1118 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1119 if (ssid || !wpa_s->first_sched_scan) {
1120 wpa_s->sched_scan_timed_out = 0;
1121 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1122 wpa_supplicant_sched_scan_timeout,
1124 wpa_s->first_sched_scan = 0;
1125 wpa_s->sched_scan_timeout /= 2;
1126 wpa_s->sched_scan_interval *= 2;
1127 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1128 wpa_s->sched_scan_interval = 10;
1129 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1133 /* If there is no more ssids, start next time from the beginning */
1135 wpa_s->prev_sched_ssid = NULL;
1142 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1143 * @wpa_s: Pointer to wpa_supplicant data
1145 * This function is used to cancel a scan request scheduled with
1146 * wpa_supplicant_req_scan().
1148 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1150 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1151 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1152 wpas_p2p_continue_after_scan(wpa_s);
1157 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1158 * @wpa_s: Pointer to wpa_supplicant data
1160 * This function is used to stop a delayed scheduled scan.
1162 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1164 if (!wpa_s->sched_scan_supported)
1167 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1168 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1174 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1175 * @wpa_s: Pointer to wpa_supplicant data
1177 * This function is used to stop a periodic scheduled scan.
1179 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1181 if (!wpa_s->sched_scanning)
1184 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1185 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1186 wpa_supplicant_stop_sched_scan(wpa_s);
1191 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1192 * @wpa_s: Pointer to wpa_supplicant data
1193 * @scanning: Whether scanning is currently in progress
1195 * This function is to generate scanning notifycations. It is called whenever
1196 * there may have been a change in scanning (scan started, completed, stopped).
1197 * wpas_notify_scanning() is called whenever the scanning state changed from the
1198 * previously notified state.
1200 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1203 if (wpa_s->scanning != scanning) {
1204 wpa_s->scanning = scanning;
1205 wpas_notify_scanning(wpa_s);
1210 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1216 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1217 for (i = 0; ie && i < ie[1]; i++) {
1218 if ((ie[i + 2] & 0x7f) > rate)
1219 rate = ie[i + 2] & 0x7f;
1222 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1223 for (i = 0; ie && i < ie[1]; i++) {
1224 if ((ie[i + 2] & 0x7f) > rate)
1225 rate = ie[i + 2] & 0x7f;
1233 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1234 * @res: Scan result entry
1235 * @ie: Information element identitifier (WLAN_EID_*)
1236 * Returns: Pointer to the information element (id field) or %NULL if not found
1238 * This function returns the first matching information element in the scan
1241 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1243 const u8 *end, *pos;
1245 pos = (const u8 *) (res + 1);
1246 end = pos + res->ie_len;
1248 while (pos + 1 < end) {
1249 if (pos + 2 + pos[1] > end)
1261 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1262 * @res: Scan result entry
1263 * @vendor_type: Vendor type (four octets starting the IE payload)
1264 * Returns: Pointer to the information element (id field) or %NULL if not found
1266 * This function returns the first matching information element in the scan
1269 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1272 const u8 *end, *pos;
1274 pos = (const u8 *) (res + 1);
1275 end = pos + res->ie_len;
1277 while (pos + 1 < end) {
1278 if (pos + 2 + pos[1] > end)
1280 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1281 vendor_type == WPA_GET_BE32(&pos[2]))
1291 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1292 * @res: Scan result entry
1293 * @vendor_type: Vendor type (four octets starting the IE payload)
1294 * Returns: Pointer to the information element (id field) or %NULL if not found
1296 * This function returns the first matching information element in the scan
1299 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1300 * from Beacon frames instead of either Beacon or Probe Response frames.
1302 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1305 const u8 *end, *pos;
1307 if (res->beacon_ie_len == 0)
1310 pos = (const u8 *) (res + 1);
1312 end = pos + res->beacon_ie_len;
1314 while (pos + 1 < end) {
1315 if (pos + 2 + pos[1] > end)
1317 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1318 vendor_type == WPA_GET_BE32(&pos[2]))
1328 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1329 * @res: Scan result entry
1330 * @vendor_type: Vendor type (four octets starting the IE payload)
1331 * Returns: Pointer to the information element payload or %NULL if not found
1333 * This function returns concatenated payload of possibly fragmented vendor
1334 * specific information elements in the scan result. The caller is responsible
1335 * for freeing the returned buffer.
1337 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1341 const u8 *end, *pos;
1343 buf = wpabuf_alloc(res->ie_len);
1347 pos = (const u8 *) (res + 1);
1348 end = pos + res->ie_len;
1350 while (pos + 1 < end) {
1351 if (pos + 2 + pos[1] > end)
1353 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1354 vendor_type == WPA_GET_BE32(&pos[2]))
1355 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1359 if (wpabuf_len(buf) == 0) {
1369 * Channels with a great SNR can operate at full rate. What is a great SNR?
1370 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1371 * rule of thumb is that any SNR above 20 is good." This one
1372 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1373 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1374 * conservative value.
1376 #define GREAT_SNR 30
1378 /* Compare function for sorting scan results. Return >0 if @b is considered
1380 static int wpa_scan_result_compar(const void *a, const void *b)
1382 #define IS_5GHZ(n) (n > 4000)
1383 #define MIN(a,b) a < b ? a : b
1384 struct wpa_scan_res **_wa = (void *) a;
1385 struct wpa_scan_res **_wb = (void *) b;
1386 struct wpa_scan_res *wa = *_wa;
1387 struct wpa_scan_res *wb = *_wb;
1388 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1391 /* WPA/WPA2 support preferred */
1392 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1393 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1394 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1395 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1397 if (wpa_b && !wpa_a)
1399 if (!wpa_b && wpa_a)
1402 /* privacy support preferred */
1403 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1404 (wb->caps & IEEE80211_CAP_PRIVACY))
1406 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1407 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1410 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1411 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1412 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1413 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1415 /* Not suitable information to calculate SNR, so use level */
1420 /* best/max rate preferred if SNR close enough */
1421 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1422 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1423 maxrate_a = wpa_scan_get_max_rate(wa);
1424 maxrate_b = wpa_scan_get_max_rate(wb);
1425 if (maxrate_a != maxrate_b)
1426 return maxrate_b - maxrate_a;
1427 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1428 return IS_5GHZ(wa->freq) ? -1 : 1;
1431 /* use freq for channel preference */
1433 /* all things being equal, use SNR; if SNRs are
1434 * identical, use quality values since some drivers may only report
1435 * that value and leave the signal level zero */
1437 return wb->qual - wa->qual;
1438 return snr_b - snr_a;
1445 /* Compare function for sorting scan results when searching a WPS AP for
1446 * provisioning. Return >0 if @b is considered better. */
1447 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1449 struct wpa_scan_res **_wa = (void *) a;
1450 struct wpa_scan_res **_wb = (void *) b;
1451 struct wpa_scan_res *wa = *_wa;
1452 struct wpa_scan_res *wb = *_wb;
1453 int uses_wps_a, uses_wps_b;
1454 struct wpabuf *wps_a, *wps_b;
1457 /* Optimization - check WPS IE existence before allocated memory and
1458 * doing full reassembly. */
1459 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1460 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1461 if (uses_wps_a && !uses_wps_b)
1463 if (!uses_wps_a && uses_wps_b)
1466 if (uses_wps_a && uses_wps_b) {
1467 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1468 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1469 res = wps_ap_priority_compar(wps_a, wps_b);
1477 * Do not use current AP security policy as a sorting criteria during
1478 * WPS provisioning step since the AP may get reconfigured at the
1479 * completion of provisioning.
1482 /* all things being equal, use signal level; if signal levels are
1483 * identical, use quality values since some drivers may only report
1484 * that value and leave the signal level zero */
1485 if (wb->level == wa->level)
1486 return wb->qual - wa->qual;
1487 return wb->level - wa->level;
1489 #endif /* CONFIG_WPS */
1492 static void dump_scan_res(struct wpa_scan_results *scan_res)
1494 #ifndef CONFIG_NO_STDOUT_DEBUG
1497 if (scan_res->res == NULL || scan_res->num == 0)
1500 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1502 for (i = 0; i < scan_res->num; i++) {
1503 struct wpa_scan_res *r = scan_res->res[i];
1505 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1506 == WPA_SCAN_LEVEL_DBM) {
1507 int snr = r->level - r->noise;
1508 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1509 "noise=%d level=%d snr=%d%s flags=0x%x "
1511 MAC2STR(r->bssid), r->freq, r->qual,
1512 r->noise, r->level, snr,
1513 snr >= GREAT_SNR ? "*" : "", r->flags,
1516 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1517 "noise=%d level=%d flags=0x%x age=%u",
1518 MAC2STR(r->bssid), r->freq, r->qual,
1519 r->noise, r->level, r->flags, r->age);
1521 pos = (u8 *) (r + 1);
1523 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1525 if (r->beacon_ie_len)
1526 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1527 pos, r->beacon_ie_len);
1529 #endif /* CONFIG_NO_STDOUT_DEBUG */
1534 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1535 * @wpa_s: Pointer to wpa_supplicant data
1536 * @bssid: BSSID to check
1537 * Returns: 0 if the BSSID is filtered or 1 if not
1539 * This function is used to filter out specific BSSIDs from scan reslts mainly
1540 * for testing purposes (SET bssid_filter ctrl_iface command).
1542 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1547 if (wpa_s->bssid_filter == NULL)
1550 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1551 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1560 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1561 struct wpa_scan_results *res)
1565 if (wpa_s->bssid_filter == NULL)
1568 for (i = 0, j = 0; i < res->num; i++) {
1569 if (wpa_supplicant_filter_bssid_match(wpa_s,
1570 res->res[i]->bssid)) {
1571 res->res[j++] = res->res[i];
1573 os_free(res->res[i]);
1578 if (res->num != j) {
1579 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1580 (int) (res->num - j));
1587 * wpa_supplicant_get_scan_results - Get scan results
1588 * @wpa_s: Pointer to wpa_supplicant data
1589 * @info: Information about what was scanned or %NULL if not available
1590 * @new_scan: Whether a new scan was performed
1591 * Returns: Scan results, %NULL on failure
1593 * This function request the current scan results from the driver and updates
1594 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1595 * results with wpa_scan_results_free().
1597 struct wpa_scan_results *
1598 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1599 struct scan_info *info, int new_scan)
1601 struct wpa_scan_results *scan_res;
1603 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1605 scan_res = wpa_drv_get_scan_results2(wpa_s);
1606 if (scan_res == NULL) {
1607 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1610 if (scan_res->fetch_time.sec == 0) {
1612 * Make sure we have a valid timestamp if the driver wrapper
1613 * does not set this.
1615 os_get_reltime(&scan_res->fetch_time);
1617 filter_scan_res(wpa_s, scan_res);
1620 if (wpas_wps_in_progress(wpa_s)) {
1621 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1622 "provisioning rules");
1623 compar = wpa_scan_result_wps_compar;
1625 #endif /* CONFIG_WPS */
1627 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1629 dump_scan_res(scan_res);
1631 wpa_bss_update_start(wpa_s);
1632 for (i = 0; i < scan_res->num; i++)
1633 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1634 &scan_res->fetch_time);
1635 wpa_bss_update_end(wpa_s, info, new_scan);
1642 * wpa_supplicant_update_scan_results - Update scan results from the driver
1643 * @wpa_s: Pointer to wpa_supplicant data
1644 * Returns: 0 on success, -1 on failure
1646 * This function updates the BSS table within wpa_supplicant based on the
1647 * currently available scan results from the driver without requesting a new
1648 * scan. This is used in cases where the driver indicates an association
1649 * (including roaming within ESS) and wpa_supplicant does not yet have the
1650 * needed information to complete the connection (e.g., to perform validation
1651 * steps in 4-way handshake).
1653 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1655 struct wpa_scan_results *scan_res;
1656 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1657 if (scan_res == NULL)
1659 wpa_scan_results_free(scan_res);
1666 * scan_only_handler - Reports scan results
1668 void scan_only_handler(struct wpa_supplicant *wpa_s,
1669 struct wpa_scan_results *scan_res)
1671 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1672 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1673 wpas_notify_scan_results(wpa_s);
1674 wpas_notify_scan_done(wpa_s, 1);
1678 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1680 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);