2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2014, 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"
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;
101 return 0; /* no normal network profiles on p2p_mgmt interface */
104 if (!wpas_network_disabled(wpa_s
, ssid
))
110 if (wpa_s
->conf
->cred
&& wpa_s
->conf
->interworking
&&
111 wpa_s
->conf
->auto_interworking
)
113 if (count
== 0 && disabled
> 0) {
114 wpa_dbg(wpa_s
, MSG_DEBUG
, "No enabled networks (%d disabled "
115 "networks)", disabled
);
121 static void wpa_supplicant_assoc_try(struct wpa_supplicant
*wpa_s
,
122 struct wpa_ssid
*ssid
)
125 if (!wpas_network_disabled(wpa_s
, ssid
))
130 /* ap_scan=2 mode - try to associate with each SSID. */
132 wpa_dbg(wpa_s
, MSG_DEBUG
, "wpa_supplicant_assoc_try: Reached "
133 "end of scan list - go back to beginning");
134 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
135 wpa_supplicant_req_scan(wpa_s
, 0, 0);
139 /* Continue from the next SSID on the next attempt. */
140 wpa_s
->prev_scan_ssid
= ssid
;
142 /* Start from the beginning of the SSID list. */
143 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
145 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
149 static void wpas_trigger_scan_cb(struct wpa_radio_work
*work
, int deinit
)
151 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
152 struct wpa_driver_scan_params
*params
= work
->ctx
;
156 if (!work
->started
) {
157 wpa_scan_free_params(params
);
160 wpa_supplicant_notify_scanning(wpa_s
, 0);
161 wpas_notify_scan_done(wpa_s
, 0);
162 wpa_s
->scan_work
= NULL
;
166 if (wpas_update_random_addr_disassoc(wpa_s
) < 0) {
167 wpa_msg(wpa_s
, MSG_INFO
,
168 "Failed to assign random MAC address for a scan");
169 radio_work_done(work
);
173 wpa_supplicant_notify_scanning(wpa_s
, 1);
175 if (wpa_s
->clear_driver_scan_cache
) {
176 wpa_printf(MSG_DEBUG
,
177 "Request driver to clear scan cache due to local BSS flush");
178 params
->only_new_results
= 1;
180 ret
= wpa_drv_scan(wpa_s
, params
);
181 wpa_scan_free_params(params
);
184 int retry
= wpa_s
->last_scan_req
!= MANUAL_SCAN_REQ
;
186 if (wpa_s
->disconnected
)
189 wpa_supplicant_notify_scanning(wpa_s
, 0);
190 wpas_notify_scan_done(wpa_s
, 0);
191 if (wpa_s
->wpa_state
== WPA_SCANNING
)
192 wpa_supplicant_set_state(wpa_s
,
193 wpa_s
->scan_prev_wpa_state
);
194 wpa_msg(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_FAILED
"ret=%d%s",
195 ret
, retry
? " retry=1" : "");
196 radio_work_done(work
);
199 /* Restore scan_req since we will try to scan again */
200 wpa_s
->scan_req
= wpa_s
->last_scan_req
;
201 wpa_supplicant_req_scan(wpa_s
, 1, 0);
206 os_get_reltime(&wpa_s
->scan_trigger_time
);
208 wpa_s
->normal_scans
++;
209 wpa_s
->own_scan_requested
= 1;
210 wpa_s
->clear_driver_scan_cache
= 0;
211 wpa_s
->scan_work
= work
;
216 * wpa_supplicant_trigger_scan - Request driver to start a scan
217 * @wpa_s: Pointer to wpa_supplicant data
218 * @params: Scan parameters
219 * Returns: 0 on success, -1 on failure
221 int wpa_supplicant_trigger_scan(struct wpa_supplicant
*wpa_s
,
222 struct wpa_driver_scan_params
*params
)
224 struct wpa_driver_scan_params
*ctx
;
226 if (wpa_s
->scan_work
) {
227 wpa_dbg(wpa_s
, MSG_INFO
, "Reject scan trigger since one is already pending");
231 ctx
= wpa_scan_clone_params(params
);
235 if (radio_add_work(wpa_s
, 0, "scan", 0, wpas_trigger_scan_cb
, ctx
) < 0)
237 wpa_scan_free_params(ctx
);
246 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
248 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
250 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting delayed sched scan");
252 if (wpa_supplicant_req_sched_scan(wpa_s
))
253 wpa_supplicant_req_scan(wpa_s
, 0, 0);
258 wpa_supplicant_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
260 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
262 wpa_dbg(wpa_s
, MSG_DEBUG
, "Sched scan timeout - stopping it");
264 wpa_s
->sched_scan_timed_out
= 1;
265 wpa_supplicant_cancel_sched_scan(wpa_s
);
269 int wpa_supplicant_start_sched_scan(struct wpa_supplicant
*wpa_s
,
270 struct wpa_driver_scan_params
*params
)
274 wpa_supplicant_notify_scanning(wpa_s
, 1);
275 ret
= wpa_drv_sched_scan(wpa_s
, params
);
277 wpa_supplicant_notify_scanning(wpa_s
, 0);
279 wpa_s
->sched_scanning
= 1;
285 int wpa_supplicant_stop_sched_scan(struct wpa_supplicant
*wpa_s
)
289 ret
= wpa_drv_stop_sched_scan(wpa_s
);
291 wpa_dbg(wpa_s
, MSG_DEBUG
, "stopping sched_scan failed!");
292 /* TODO: what to do if stopping fails? */
300 static struct wpa_driver_scan_filter
*
301 wpa_supplicant_build_filter_ssids(struct wpa_config
*conf
, size_t *num_ssids
)
303 struct wpa_driver_scan_filter
*ssids
;
304 struct wpa_ssid
*ssid
;
308 if (!conf
->filter_ssids
)
311 for (count
= 0, ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
312 if (ssid
->ssid
&& ssid
->ssid_len
)
317 ssids
= os_calloc(count
, sizeof(struct wpa_driver_scan_filter
));
321 for (ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
322 if (!ssid
->ssid
|| !ssid
->ssid_len
)
324 os_memcpy(ssids
[*num_ssids
].ssid
, ssid
->ssid
, ssid
->ssid_len
);
325 ssids
[*num_ssids
].ssid_len
= ssid
->ssid_len
;
333 static void wpa_supplicant_optimize_freqs(
334 struct wpa_supplicant
*wpa_s
, struct wpa_driver_scan_params
*params
)
337 if (params
->freqs
== NULL
&& wpa_s
->p2p_in_provisioning
&&
339 /* Optimize provisioning state scan based on GO information */
340 if (wpa_s
->p2p_in_provisioning
< 5 &&
341 wpa_s
->go_params
->freq
> 0) {
342 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only GO "
343 "preferred frequency %d MHz",
344 wpa_s
->go_params
->freq
);
345 params
->freqs
= os_calloc(2, sizeof(int));
347 params
->freqs
[0] = wpa_s
->go_params
->freq
;
348 } else if (wpa_s
->p2p_in_provisioning
< 8 &&
349 wpa_s
->go_params
->freq_list
[0]) {
350 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only common "
352 int_array_concat(¶ms
->freqs
,
353 wpa_s
->go_params
->freq_list
);
355 int_array_sort_unique(params
->freqs
);
357 wpa_s
->p2p_in_provisioning
++;
360 if (params
->freqs
== NULL
&& wpa_s
->p2p_in_invitation
) {
362 * Optimize scan based on GO information during persistent
365 if (wpa_s
->p2p_in_invitation
< 5 &&
366 wpa_s
->p2p_invite_go_freq
> 0) {
367 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only GO preferred frequency %d MHz during invitation",
368 wpa_s
->p2p_invite_go_freq
);
369 params
->freqs
= os_calloc(2, sizeof(int));
371 params
->freqs
[0] = wpa_s
->p2p_invite_go_freq
;
373 wpa_s
->p2p_in_invitation
++;
374 if (wpa_s
->p2p_in_invitation
> 20) {
376 * This should not really happen since the variable is
377 * cleared on group removal, but if it does happen, make
378 * sure we do not get stuck in special invitation scan
381 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Clear p2p_in_invitation");
382 wpa_s
->p2p_in_invitation
= 0;
385 #endif /* CONFIG_P2P */
388 if (params
->freqs
== NULL
&& wpa_s
->after_wps
&& wpa_s
->wps_freq
) {
390 * Optimize post-provisioning scan based on channel used
391 * during provisioning.
393 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Scan only frequency %u MHz "
394 "that was used during provisioning", wpa_s
->wps_freq
);
395 params
->freqs
= os_calloc(2, sizeof(int));
397 params
->freqs
[0] = wpa_s
->wps_freq
;
399 } else if (wpa_s
->after_wps
)
402 if (params
->freqs
== NULL
&& wpa_s
->known_wps_freq
&& wpa_s
->wps_freq
)
404 /* Optimize provisioning scan based on already known channel */
405 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Scan only frequency %u MHz",
407 params
->freqs
= os_calloc(2, sizeof(int));
409 params
->freqs
[0] = wpa_s
->wps_freq
;
410 wpa_s
->known_wps_freq
= 0; /* only do this once */
412 #endif /* CONFIG_WPS */
416 #ifdef CONFIG_INTERWORKING
417 static void wpas_add_interworking_elements(struct wpa_supplicant
*wpa_s
,
420 wpabuf_put_u8(buf
, WLAN_EID_INTERWORKING
);
421 wpabuf_put_u8(buf
, is_zero_ether_addr(wpa_s
->conf
->hessid
) ? 1 :
423 wpabuf_put_u8(buf
, wpa_s
->conf
->access_network_type
);
425 if (!is_zero_ether_addr(wpa_s
->conf
->hessid
))
426 wpabuf_put_data(buf
, wpa_s
->conf
->hessid
, ETH_ALEN
);
428 #endif /* CONFIG_INTERWORKING */
431 static struct wpabuf
* wpa_supplicant_extra_ies(struct wpa_supplicant
*wpa_s
)
433 struct wpabuf
*extra_ie
= NULL
;
438 enum wps_request_type req_type
= WPS_REQ_ENROLLEE_INFO
;
439 #endif /* CONFIG_WPS */
441 ext_capab_len
= wpas_build_ext_capab(wpa_s
, ext_capab
,
443 if (ext_capab_len
> 0 &&
444 wpabuf_resize(&extra_ie
, ext_capab_len
) == 0)
445 wpabuf_put_data(extra_ie
, ext_capab
, ext_capab_len
);
447 #ifdef CONFIG_INTERWORKING
448 if (wpa_s
->conf
->interworking
&&
449 wpabuf_resize(&extra_ie
, 100) == 0)
450 wpas_add_interworking_elements(wpa_s
, extra_ie
);
451 #endif /* CONFIG_INTERWORKING */
454 wps
= wpas_wps_in_use(wpa_s
, &req_type
);
457 struct wpabuf
*wps_ie
;
458 wps_ie
= wps_build_probe_req_ie(wps
== 2 ? DEV_PW_PUSHBUTTON
:
461 wpa_s
->wps
->uuid
, req_type
,
464 if (wpabuf_resize(&extra_ie
, wpabuf_len(wps_ie
)) == 0)
465 wpabuf_put_buf(extra_ie
, wps_ie
);
472 size_t ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
473 if (wpabuf_resize(&extra_ie
, ielen
) == 0)
474 wpas_p2p_scan_ie(wpa_s
, extra_ie
);
476 #endif /* CONFIG_P2P */
478 wpa_supplicant_mesh_add_scan_ie(wpa_s
, &extra_ie
);
480 #endif /* CONFIG_WPS */
483 if (wpa_s
->conf
->hs20
&& wpabuf_resize(&extra_ie
, 7) == 0)
484 wpas_hs20_add_indication(extra_ie
, -1);
485 #endif /* CONFIG_HS20 */
488 if (wpa_s
->fst_ies
&&
489 wpabuf_resize(&extra_ie
, wpabuf_len(wpa_s
->fst_ies
)) == 0)
490 wpabuf_put_buf(extra_ie
, wpa_s
->fst_ies
);
491 #endif /* CONFIG_FST */
500 * Check whether there are any enabled networks or credentials that could be
501 * used for a non-P2P connection.
503 static int non_p2p_network_enabled(struct wpa_supplicant
*wpa_s
)
505 struct wpa_ssid
*ssid
;
507 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
508 if (wpas_network_disabled(wpa_s
, ssid
))
510 if (!ssid
->p2p_group
)
514 if (wpa_s
->conf
->cred
&& wpa_s
->conf
->interworking
&&
515 wpa_s
->conf
->auto_interworking
)
521 #endif /* CONFIG_P2P */
524 static void wpa_setband_scan_freqs_list(struct wpa_supplicant
*wpa_s
,
525 enum hostapd_hw_mode band
,
526 struct wpa_driver_scan_params
*params
)
528 /* Include only supported channels for the specified band */
529 struct hostapd_hw_modes
*mode
;
532 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, band
);
534 /* No channels supported in this band - use empty list */
535 params
->freqs
= os_zalloc(sizeof(int));
539 params
->freqs
= os_calloc(mode
->num_channels
+ 1, sizeof(int));
540 if (params
->freqs
== NULL
)
542 for (count
= 0, i
= 0; i
< mode
->num_channels
; i
++) {
543 if (mode
->channels
[i
].flag
& HOSTAPD_CHAN_DISABLED
)
545 params
->freqs
[count
++] = mode
->channels
[i
].freq
;
550 static void wpa_setband_scan_freqs(struct wpa_supplicant
*wpa_s
,
551 struct wpa_driver_scan_params
*params
)
553 if (wpa_s
->hw
.modes
== NULL
)
554 return; /* unknown what channels the driver supports */
556 return; /* already using a limited channel set */
557 if (wpa_s
->setband
== WPA_SETBAND_5G
)
558 wpa_setband_scan_freqs_list(wpa_s
, HOSTAPD_MODE_IEEE80211A
,
560 else if (wpa_s
->setband
== WPA_SETBAND_2G
)
561 wpa_setband_scan_freqs_list(wpa_s
, HOSTAPD_MODE_IEEE80211G
,
566 static void wpa_set_scan_ssids(struct wpa_supplicant
*wpa_s
,
567 struct wpa_driver_scan_params
*params
,
571 struct wpa_ssid
*ssid
;
573 for (i
= 0; i
< wpa_s
->scan_id_count
; i
++) {
576 ssid
= wpa_config_get_network(wpa_s
->conf
, wpa_s
->scan_id
[i
]);
577 if (!ssid
|| !ssid
->scan_ssid
)
580 for (j
= 0; j
< params
->num_ssids
; j
++) {
581 if (params
->ssids
[j
].ssid_len
== ssid
->ssid_len
&&
582 params
->ssids
[j
].ssid
&&
583 os_memcmp(params
->ssids
[j
].ssid
, ssid
->ssid
,
584 ssid
->ssid_len
) == 0)
587 if (j
< params
->num_ssids
)
588 continue; /* already in the list */
590 if (params
->num_ssids
+ 1 > max_ssids
) {
591 wpa_printf(MSG_DEBUG
,
592 "Over max scan SSIDs for manual request");
596 wpa_printf(MSG_DEBUG
, "Scan SSID (manual request): %s",
597 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
598 params
->ssids
[params
->num_ssids
].ssid
= ssid
->ssid
;
599 params
->ssids
[params
->num_ssids
].ssid_len
= ssid
->ssid_len
;
603 wpa_s
->scan_id_count
= 0;
607 static int wpa_set_ssids_from_scan_req(struct wpa_supplicant
*wpa_s
,
608 struct wpa_driver_scan_params
*params
,
613 if (wpa_s
->ssids_from_scan_req
== NULL
||
614 wpa_s
->num_ssids_from_scan_req
== 0)
617 if (wpa_s
->num_ssids_from_scan_req
> max_ssids
) {
618 wpa_s
->num_ssids_from_scan_req
= max_ssids
;
619 wpa_printf(MSG_DEBUG
, "Over max scan SSIDs from scan req: %u",
620 (unsigned int) max_ssids
);
623 for (i
= 0; i
< wpa_s
->num_ssids_from_scan_req
; i
++) {
624 params
->ssids
[i
].ssid
= wpa_s
->ssids_from_scan_req
[i
].ssid
;
625 params
->ssids
[i
].ssid_len
=
626 wpa_s
->ssids_from_scan_req
[i
].ssid_len
;
627 wpa_hexdump_ascii(MSG_DEBUG
, "specific SSID",
628 params
->ssids
[i
].ssid
,
629 params
->ssids
[i
].ssid_len
);
632 params
->num_ssids
= wpa_s
->num_ssids_from_scan_req
;
633 wpa_s
->num_ssids_from_scan_req
= 0;
638 static void wpa_supplicant_scan(void *eloop_ctx
, void *timeout_ctx
)
640 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
641 struct wpa_ssid
*ssid
;
642 int ret
, p2p_in_prog
;
643 struct wpabuf
*extra_ie
= NULL
;
644 struct wpa_driver_scan_params params
;
645 struct wpa_driver_scan_params
*scan_params
;
647 int connect_without_scan
= 0;
649 if (wpa_s
->pno
|| wpa_s
->pno_sched_pending
) {
650 wpa_dbg(wpa_s
, MSG_DEBUG
, "Skip scan - PNO is in progress");
654 if (wpa_s
->wpa_state
== WPA_INTERFACE_DISABLED
) {
655 wpa_dbg(wpa_s
, MSG_DEBUG
, "Skip scan - interface disabled");
659 if (wpa_s
->disconnected
&& wpa_s
->scan_req
== NORMAL_SCAN_REQ
) {
660 wpa_dbg(wpa_s
, MSG_DEBUG
, "Disconnected - do not scan");
661 wpa_supplicant_set_state(wpa_s
, WPA_DISCONNECTED
);
665 if (wpa_s
->scanning
) {
667 * If we are already in scanning state, we shall reschedule the
668 * the incoming scan request.
670 wpa_dbg(wpa_s
, MSG_DEBUG
, "Already scanning - Reschedule the incoming scan req");
671 wpa_supplicant_req_scan(wpa_s
, 1, 0);
675 if (!wpa_supplicant_enabled_networks(wpa_s
) &&
676 wpa_s
->scan_req
== NORMAL_SCAN_REQ
) {
677 wpa_dbg(wpa_s
, MSG_DEBUG
, "No enabled networks - do not scan");
678 wpa_supplicant_set_state(wpa_s
, WPA_INACTIVE
);
682 if (wpa_s
->conf
->ap_scan
!= 0 &&
683 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_WIRED
)) {
684 wpa_dbg(wpa_s
, MSG_DEBUG
, "Using wired authentication - "
685 "overriding ap_scan configuration");
686 wpa_s
->conf
->ap_scan
= 0;
687 wpas_notify_ap_scan_changed(wpa_s
);
690 if (wpa_s
->conf
->ap_scan
== 0) {
691 wpa_supplicant_gen_assoc_event(wpa_s
);
696 if (wpa_s
->scan_req
!= MANUAL_SCAN_REQ
&&
697 wpa_s
->connect_without_scan
) {
698 connect_without_scan
= 1;
699 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
700 if (ssid
== wpa_s
->connect_without_scan
)
705 p2p_in_prog
= wpas_p2p_in_progress(wpa_s
);
706 if (p2p_in_prog
&& p2p_in_prog
!= 2 &&
708 (ssid
->mode
!= WPAS_MODE_AP
&& ssid
->mode
!= WPAS_MODE_P2P_GO
))) {
709 wpa_dbg(wpa_s
, MSG_DEBUG
, "Delay station mode scan while P2P operation is in progress");
710 wpa_supplicant_req_scan(wpa_s
, 5, 0);
714 if (wpa_s
->conf
->ap_scan
== 2)
717 max_ssids
= wpa_s
->max_scan_ssids
;
718 if (max_ssids
> WPAS_MAX_SCAN_SSIDS
)
719 max_ssids
= WPAS_MAX_SCAN_SSIDS
;
722 wpa_s
->last_scan_req
= wpa_s
->scan_req
;
723 wpa_s
->scan_req
= NORMAL_SCAN_REQ
;
725 if (connect_without_scan
) {
726 wpa_s
->connect_without_scan
= NULL
;
728 wpa_printf(MSG_DEBUG
, "Start a pre-selected network "
729 "without scan step");
730 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
735 os_memset(¶ms
, 0, sizeof(params
));
737 wpa_s
->scan_prev_wpa_state
= wpa_s
->wpa_state
;
738 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
739 wpa_s
->wpa_state
== WPA_INACTIVE
)
740 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
743 * If autoscan has set its own scanning parameters
745 if (wpa_s
->autoscan_params
!= NULL
) {
746 scan_params
= wpa_s
->autoscan_params
;
750 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
751 wpa_set_ssids_from_scan_req(wpa_s
, ¶ms
, max_ssids
)) {
752 wpa_printf(MSG_DEBUG
, "Use specific SSIDs from SCAN command");
757 if ((wpa_s
->p2p_in_provisioning
|| wpa_s
->show_group_started
) &&
758 wpa_s
->go_params
&& !wpa_s
->conf
->passive_scan
) {
759 wpa_printf(MSG_DEBUG
, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
760 wpa_s
->p2p_in_provisioning
,
761 wpa_s
->show_group_started
);
762 params
.ssids
[0].ssid
= wpa_s
->go_params
->ssid
;
763 params
.ssids
[0].ssid_len
= wpa_s
->go_params
->ssid_len
;
764 params
.num_ssids
= 1;
768 if (wpa_s
->p2p_in_invitation
) {
769 if (wpa_s
->current_ssid
) {
770 wpa_printf(MSG_DEBUG
, "P2P: Use specific SSID for scan during invitation");
771 params
.ssids
[0].ssid
= wpa_s
->current_ssid
->ssid
;
772 params
.ssids
[0].ssid_len
=
773 wpa_s
->current_ssid
->ssid_len
;
774 params
.num_ssids
= 1;
776 wpa_printf(MSG_DEBUG
, "P2P: No specific SSID known for scan during invitation");
780 #endif /* CONFIG_P2P */
782 /* Find the starting point from which to continue scanning */
783 ssid
= wpa_s
->conf
->ssid
;
784 if (wpa_s
->prev_scan_ssid
!= WILDCARD_SSID_SCAN
) {
786 if (ssid
== wpa_s
->prev_scan_ssid
) {
794 if (wpa_s
->last_scan_req
!= MANUAL_SCAN_REQ
&&
797 #endif /* CONFIG_AP */
798 wpa_s
->conf
->ap_scan
== 2) {
799 wpa_s
->connect_without_scan
= NULL
;
800 wpa_s
->prev_scan_wildcard
= 0;
801 wpa_supplicant_assoc_try(wpa_s
, ssid
);
803 } else if (wpa_s
->conf
->ap_scan
== 2) {
805 * User-initiated scan request in ap_scan == 2; scan with
809 } else if (wpa_s
->reattach
&& wpa_s
->current_ssid
!= NULL
) {
811 * Perform single-channel single-SSID scan for
812 * reassociate-to-same-BSS operation.
815 ssid
= wpa_s
->current_ssid
;
816 wpa_hexdump_ascii(MSG_DEBUG
, "Scan SSID",
817 ssid
->ssid
, ssid
->ssid_len
);
818 params
.ssids
[0].ssid
= ssid
->ssid
;
819 params
.ssids
[0].ssid_len
= ssid
->ssid_len
;
820 params
.num_ssids
= 1;
823 * Allocate memory for frequency array, allocate one extra
824 * slot for the zero-terminator.
826 params
.freqs
= os_malloc(sizeof(int) * 2);
827 if (params
.freqs
== NULL
) {
828 wpa_dbg(wpa_s
, MSG_ERROR
, "Memory allocation failed");
831 params
.freqs
[0] = wpa_s
->assoc_freq
;
835 * Reset the reattach flag so that we fall back to full scan if
840 struct wpa_ssid
*start
= ssid
, *tssid
;
842 if (ssid
== NULL
&& max_ssids
> 1)
843 ssid
= wpa_s
->conf
->ssid
;
845 if (!wpas_network_disabled(wpa_s
, ssid
) &&
847 wpa_hexdump_ascii(MSG_DEBUG
, "Scan SSID",
848 ssid
->ssid
, ssid
->ssid_len
);
849 params
.ssids
[params
.num_ssids
].ssid
=
851 params
.ssids
[params
.num_ssids
].ssid_len
=
854 if (params
.num_ssids
+ 1 >= max_ssids
)
860 if (ssid
== NULL
&& max_ssids
> 1 &&
861 start
!= wpa_s
->conf
->ssid
)
862 ssid
= wpa_s
->conf
->ssid
;
865 if (wpa_s
->scan_id_count
&&
866 wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
)
867 wpa_set_scan_ssids(wpa_s
, ¶ms
, max_ssids
);
869 for (tssid
= wpa_s
->conf
->ssid
;
870 wpa_s
->last_scan_req
!= MANUAL_SCAN_REQ
&& tssid
;
871 tssid
= tssid
->next
) {
872 if (wpas_network_disabled(wpa_s
, tssid
))
874 if ((params
.freqs
|| !freqs_set
) && tssid
->scan_freq
) {
875 int_array_concat(¶ms
.freqs
,
878 os_free(params
.freqs
);
883 int_array_sort_unique(params
.freqs
);
886 if (ssid
&& max_ssids
== 1) {
888 * If the driver is limited to 1 SSID at a time interleave
889 * wildcard SSID scans with specific SSID scans to avoid
890 * waiting a long time for a wildcard scan.
892 if (!wpa_s
->prev_scan_wildcard
) {
893 params
.ssids
[0].ssid
= NULL
;
894 params
.ssids
[0].ssid_len
= 0;
895 wpa_s
->prev_scan_wildcard
= 1;
896 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for "
897 "wildcard SSID (Interleave with specific)");
899 wpa_s
->prev_scan_ssid
= ssid
;
900 wpa_s
->prev_scan_wildcard
= 0;
901 wpa_dbg(wpa_s
, MSG_DEBUG
,
902 "Starting AP scan for specific SSID: %s",
903 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
908 wpa_s
->prev_scan_ssid
= ssid
;
909 wpa_dbg(wpa_s
, MSG_DEBUG
, "Include wildcard SSID in "
912 } else if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
913 wpa_s
->manual_scan_passive
&& params
.num_ssids
== 0) {
914 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use passive scan based on manual request");
915 } else if (wpa_s
->conf
->passive_scan
) {
916 wpa_dbg(wpa_s
, MSG_DEBUG
,
917 "Use passive scan based on configuration");
919 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
921 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for wildcard "
926 wpa_supplicant_optimize_freqs(wpa_s
, ¶ms
);
927 extra_ie
= wpa_supplicant_extra_ies(wpa_s
);
929 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
930 wpa_s
->manual_scan_only_new
) {
931 wpa_printf(MSG_DEBUG
,
932 "Request driver to clear scan cache due to manual only_new=1 scan");
933 params
.only_new_results
= 1;
936 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&& params
.freqs
== NULL
&&
937 wpa_s
->manual_scan_freqs
) {
938 wpa_dbg(wpa_s
, MSG_DEBUG
, "Limit manual scan to specified channels");
939 params
.freqs
= wpa_s
->manual_scan_freqs
;
940 wpa_s
->manual_scan_freqs
= NULL
;
943 if (params
.freqs
== NULL
&& wpa_s
->next_scan_freqs
) {
944 wpa_dbg(wpa_s
, MSG_DEBUG
, "Optimize scan based on previously "
945 "generated frequency list");
946 params
.freqs
= wpa_s
->next_scan_freqs
;
948 os_free(wpa_s
->next_scan_freqs
);
949 wpa_s
->next_scan_freqs
= NULL
;
950 wpa_setband_scan_freqs(wpa_s
, ¶ms
);
952 /* See if user specified frequencies. If so, scan only those. */
953 if (wpa_s
->conf
->freq_list
&& !params
.freqs
) {
954 wpa_dbg(wpa_s
, MSG_DEBUG
,
955 "Optimize scan based on conf->freq_list");
956 int_array_concat(¶ms
.freqs
, wpa_s
->conf
->freq_list
);
959 /* Use current associated channel? */
960 if (wpa_s
->conf
->scan_cur_freq
&& !params
.freqs
) {
961 unsigned int num
= wpa_s
->num_multichan_concurrent
;
963 params
.freqs
= os_calloc(num
+ 1, sizeof(int));
965 num
= get_shared_radio_freqs(wpa_s
, params
.freqs
, num
);
967 wpa_dbg(wpa_s
, MSG_DEBUG
, "Scan only the "
968 "current operating channels since "
969 "scan_cur_freq is enabled");
971 os_free(params
.freqs
);
977 params
.filter_ssids
= wpa_supplicant_build_filter_ssids(
978 wpa_s
->conf
, ¶ms
.num_filter_ssids
);
980 params
.extra_ies
= wpabuf_head(extra_ie
);
981 params
.extra_ies_len
= wpabuf_len(extra_ie
);
985 if (wpa_s
->p2p_in_provisioning
|| wpa_s
->p2p_in_invitation
||
986 (wpa_s
->show_group_started
&& wpa_s
->go_params
)) {
988 * The interface may not yet be in P2P mode, so we have to
989 * explicitly request P2P probe to disable CCK rates.
991 params
.p2p_probe
= 1;
993 #endif /* CONFIG_P2P */
995 if (wpa_s
->mac_addr_rand_enable
& MAC_ADDR_RAND_SCAN
) {
996 params
.mac_addr_rand
= 1;
997 if (wpa_s
->mac_addr_scan
) {
998 params
.mac_addr
= wpa_s
->mac_addr_scan
;
999 params
.mac_addr_mask
= wpa_s
->mac_addr_scan
+ ETH_ALEN
;
1003 scan_params
= ¶ms
;
1008 * If the driver does not support multi-channel concurrency and a
1009 * virtual interface that shares the same radio with the wpa_s interface
1010 * is operating there may not be need to scan other channels apart from
1011 * the current operating channel on the other virtual interface. Filter
1012 * out other channels in case we are trying to find a connection for a
1013 * station interface when we are not configured to prefer station
1014 * connection and a concurrent operation is already in process.
1016 if (wpa_s
->scan_for_connection
&&
1017 wpa_s
->last_scan_req
== NORMAL_SCAN_REQ
&&
1018 !scan_params
->freqs
&& !params
.freqs
&&
1019 wpas_is_p2p_prioritized(wpa_s
) &&
1020 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
&&
1021 non_p2p_network_enabled(wpa_s
)) {
1022 unsigned int num
= wpa_s
->num_multichan_concurrent
;
1024 params
.freqs
= os_calloc(num
+ 1, sizeof(int));
1026 num
= get_shared_radio_freqs(wpa_s
, params
.freqs
, num
);
1027 if (num
> 0 && num
== wpa_s
->num_multichan_concurrent
) {
1028 wpa_dbg(wpa_s
, MSG_DEBUG
, "Scan only the current operating channels since all channels are already used");
1030 os_free(params
.freqs
);
1031 params
.freqs
= NULL
;
1035 #endif /* CONFIG_P2P */
1037 ret
= wpa_supplicant_trigger_scan(wpa_s
, scan_params
);
1039 if (ret
&& wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&& params
.freqs
&&
1040 !wpa_s
->manual_scan_freqs
) {
1041 /* Restore manual_scan_freqs for the next attempt */
1042 wpa_s
->manual_scan_freqs
= params
.freqs
;
1043 params
.freqs
= NULL
;
1046 wpabuf_free(extra_ie
);
1047 os_free(params
.freqs
);
1048 os_free(params
.filter_ssids
);
1051 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate AP scan");
1052 if (wpa_s
->scan_prev_wpa_state
!= wpa_s
->wpa_state
)
1053 wpa_supplicant_set_state(wpa_s
,
1054 wpa_s
->scan_prev_wpa_state
);
1055 /* Restore scan_req since we will try to scan again */
1056 wpa_s
->scan_req
= wpa_s
->last_scan_req
;
1057 wpa_supplicant_req_scan(wpa_s
, 1, 0);
1059 wpa_s
->scan_for_connection
= 0;
1060 #ifdef CONFIG_INTERWORKING
1061 wpa_s
->interworking_fast_assoc_tried
= 0;
1062 #endif /* CONFIG_INTERWORKING */
1067 void wpa_supplicant_update_scan_int(struct wpa_supplicant
*wpa_s
, int sec
)
1069 struct os_reltime remaining
, new_int
;
1072 cancelled
= eloop_cancel_timeout_one(wpa_supplicant_scan
, wpa_s
, NULL
,
1077 if (cancelled
&& os_reltime_before(&remaining
, &new_int
)) {
1078 new_int
.sec
= remaining
.sec
;
1079 new_int
.usec
= remaining
.usec
;
1083 eloop_register_timeout(new_int
.sec
, new_int
.usec
,
1084 wpa_supplicant_scan
, wpa_s
, NULL
);
1086 wpa_s
->scan_interval
= sec
;
1091 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1092 * @wpa_s: Pointer to wpa_supplicant data
1093 * @sec: Number of seconds after which to scan
1094 * @usec: Number of microseconds after which to scan
1096 * This function is used to schedule a scan for neighboring access points after
1097 * the specified time.
1099 void wpa_supplicant_req_scan(struct wpa_supplicant
*wpa_s
, int sec
, int usec
)
1103 if (wpa_s
->p2p_mgmt
) {
1104 wpa_dbg(wpa_s
, MSG_DEBUG
,
1105 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1110 res
= eloop_deplete_timeout(sec
, usec
, wpa_supplicant_scan
, wpa_s
,
1113 wpa_dbg(wpa_s
, MSG_DEBUG
, "Rescheduling scan request: %d.%06d sec",
1115 } else if (res
== 0) {
1116 wpa_dbg(wpa_s
, MSG_DEBUG
, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1119 wpa_dbg(wpa_s
, MSG_DEBUG
, "Setting scan request: %d.%06d sec",
1121 eloop_register_timeout(sec
, usec
, wpa_supplicant_scan
, wpa_s
, NULL
);
1127 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1128 * @wpa_s: Pointer to wpa_supplicant data
1129 * @sec: Number of seconds after which to scan
1130 * @usec: Number of microseconds after which to scan
1131 * Returns: 0 on success or -1 otherwise
1133 * This function is used to schedule periodic scans for neighboring
1134 * access points after the specified time.
1136 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant
*wpa_s
,
1139 if (!wpa_s
->sched_scan_supported
)
1142 eloop_register_timeout(sec
, usec
,
1143 wpa_supplicant_delayed_sched_scan_timeout
,
1151 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1152 * @wpa_s: Pointer to wpa_supplicant data
1153 * Returns: 0 is sched_scan was started or -1 otherwise
1155 * This function is used to schedule periodic scans for neighboring
1156 * access points repeating the scan continuously.
1158 int wpa_supplicant_req_sched_scan(struct wpa_supplicant
*wpa_s
)
1160 struct wpa_driver_scan_params params
;
1161 struct wpa_driver_scan_params
*scan_params
;
1162 enum wpa_states prev_state
;
1163 struct wpa_ssid
*ssid
= NULL
;
1164 struct wpabuf
*extra_ie
= NULL
;
1166 unsigned int max_sched_scan_ssids
;
1169 struct sched_scan_plan scan_plan
;
1171 if (!wpa_s
->sched_scan_supported
)
1174 if (wpa_s
->max_sched_scan_ssids
> WPAS_MAX_SCAN_SSIDS
)
1175 max_sched_scan_ssids
= WPAS_MAX_SCAN_SSIDS
;
1177 max_sched_scan_ssids
= wpa_s
->max_sched_scan_ssids
;
1178 if (max_sched_scan_ssids
< 1 || wpa_s
->conf
->disable_scan_offload
)
1181 if (wpa_s
->sched_scanning
) {
1182 wpa_dbg(wpa_s
, MSG_DEBUG
, "Already sched scanning");
1187 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1188 if (!wpas_network_disabled(wpa_s
, ssid
) && !ssid
->scan_ssid
) {
1189 /* Use wildcard SSID to find this network */
1191 } else if (!wpas_network_disabled(wpa_s
, ssid
) &&
1196 if (!wpas_network_disabled(wpa_s
, ssid
) &&
1197 ssid
->key_mgmt
== WPA_KEY_MGMT_WPS
) {
1199 * Normal scan is more reliable and faster for WPS
1200 * operations and since these are for short periods of
1201 * time, the benefit of trying to use sched_scan would
1204 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
1205 "sched_scan for WPS");
1208 #endif /* CONFIG_WPS */
1213 if (wpa_s
->normal_scans
< 3 &&
1214 (need_ssids
<= wpa_s
->max_scan_ssids
||
1215 wpa_s
->max_scan_ssids
>= (int) max_sched_scan_ssids
)) {
1217 * When normal scan can speed up operations, use that for the
1218 * first operations before starting the sched_scan to allow
1219 * user space sleep more. We do this only if the normal scan
1220 * has functionality that is suitable for this or if the
1221 * sched_scan does not have better support for multiple SSIDs.
1223 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
1224 "sched_scan for initial scans (normal_scans=%d)",
1225 wpa_s
->normal_scans
);
1229 os_memset(¶ms
, 0, sizeof(params
));
1231 /* If we can't allocate space for the filters, we just don't filter */
1232 params
.filter_ssids
= os_calloc(wpa_s
->max_match_sets
,
1233 sizeof(struct wpa_driver_scan_filter
));
1235 prev_state
= wpa_s
->wpa_state
;
1236 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
1237 wpa_s
->wpa_state
== WPA_INACTIVE
)
1238 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
1240 if (wpa_s
->autoscan_params
!= NULL
) {
1241 scan_params
= wpa_s
->autoscan_params
;
1245 /* Find the starting point from which to continue scanning */
1246 ssid
= wpa_s
->conf
->ssid
;
1247 if (wpa_s
->prev_sched_ssid
) {
1249 if (ssid
== wpa_s
->prev_sched_ssid
) {
1257 if (!ssid
|| !wpa_s
->prev_sched_ssid
) {
1258 wpa_dbg(wpa_s
, MSG_DEBUG
, "Beginning of SSID list");
1259 wpa_s
->sched_scan_timeout
= max_sched_scan_ssids
* 2;
1260 wpa_s
->first_sched_scan
= 1;
1261 ssid
= wpa_s
->conf
->ssid
;
1262 wpa_s
->prev_sched_ssid
= ssid
;
1266 wpa_dbg(wpa_s
, MSG_DEBUG
, "Add wildcard SSID to sched_scan");
1271 if (wpas_network_disabled(wpa_s
, ssid
))
1274 if (params
.num_filter_ssids
< wpa_s
->max_match_sets
&&
1275 params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
) {
1276 wpa_dbg(wpa_s
, MSG_DEBUG
, "add to filter ssid: %s",
1277 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
1278 os_memcpy(params
.filter_ssids
[params
.num_filter_ssids
].ssid
,
1279 ssid
->ssid
, ssid
->ssid_len
);
1280 params
.filter_ssids
[params
.num_filter_ssids
].ssid_len
=
1282 params
.num_filter_ssids
++;
1283 } else if (params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
)
1285 wpa_dbg(wpa_s
, MSG_DEBUG
, "Not enough room for SSID "
1286 "filter for sched_scan - drop filter");
1287 os_free(params
.filter_ssids
);
1288 params
.filter_ssids
= NULL
;
1289 params
.num_filter_ssids
= 0;
1292 if (ssid
->scan_ssid
&& ssid
->ssid
&& ssid
->ssid_len
) {
1293 if (params
.num_ssids
== max_sched_scan_ssids
)
1294 break; /* only room for broadcast SSID */
1295 wpa_dbg(wpa_s
, MSG_DEBUG
,
1296 "add to active scan ssid: %s",
1297 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
1298 params
.ssids
[params
.num_ssids
].ssid
=
1300 params
.ssids
[params
.num_ssids
].ssid_len
=
1303 if (params
.num_ssids
>= max_sched_scan_ssids
) {
1304 wpa_s
->prev_sched_ssid
= ssid
;
1308 (wpas_network_disabled(wpa_s
, ssid
) ||
1315 wpa_s
->prev_sched_ssid
= ssid
;
1319 if (params
.num_filter_ssids
== 0) {
1320 os_free(params
.filter_ssids
);
1321 params
.filter_ssids
= NULL
;
1324 extra_ie
= wpa_supplicant_extra_ies(wpa_s
);
1326 params
.extra_ies
= wpabuf_head(extra_ie
);
1327 params
.extra_ies_len
= wpabuf_len(extra_ie
);
1330 if (wpa_s
->conf
->filter_rssi
)
1331 params
.filter_rssi
= wpa_s
->conf
->filter_rssi
;
1333 /* See if user specified frequencies. If so, scan only those. */
1334 if (wpa_s
->conf
->freq_list
&& !params
.freqs
) {
1335 wpa_dbg(wpa_s
, MSG_DEBUG
,
1336 "Optimize scan based on conf->freq_list");
1337 int_array_concat(¶ms
.freqs
, wpa_s
->conf
->freq_list
);
1340 scan_params
= ¶ms
;
1343 wpa_s
->sched_scan_timed_out
= 0;
1346 * We cannot support multiple scan plans if the scan request includes
1347 * too many SSID's, so in this case use only the last scan plan and make
1348 * it run infinitely. It will be stopped by the timeout.
1350 if (wpa_s
->sched_scan_plans_num
== 1 ||
1351 (wpa_s
->sched_scan_plans_num
&& !ssid
&& wpa_s
->first_sched_scan
)) {
1352 params
.sched_scan_plans
= wpa_s
->sched_scan_plans
;
1353 params
.sched_scan_plans_num
= wpa_s
->sched_scan_plans_num
;
1354 } else if (wpa_s
->sched_scan_plans_num
> 1) {
1355 wpa_dbg(wpa_s
, MSG_DEBUG
,
1356 "Too many SSIDs. Default to using single scheduled_scan plan");
1357 params
.sched_scan_plans
=
1358 &wpa_s
->sched_scan_plans
[wpa_s
->sched_scan_plans_num
-
1360 params
.sched_scan_plans_num
= 1;
1362 if (wpa_s
->conf
->sched_scan_interval
)
1363 scan_plan
.interval
= wpa_s
->conf
->sched_scan_interval
;
1365 scan_plan
.interval
= 10;
1367 if (scan_plan
.interval
> wpa_s
->max_sched_scan_plan_interval
) {
1368 wpa_printf(MSG_WARNING
,
1369 "Scan interval too long(%u), use the maximum allowed(%u)",
1371 wpa_s
->max_sched_scan_plan_interval
);
1372 scan_plan
.interval
=
1373 wpa_s
->max_sched_scan_plan_interval
;
1376 scan_plan
.iterations
= 0;
1377 params
.sched_scan_plans
= &scan_plan
;
1378 params
.sched_scan_plans_num
= 1;
1381 if (ssid
|| !wpa_s
->first_sched_scan
) {
1382 wpa_dbg(wpa_s
, MSG_DEBUG
,
1383 "Starting sched scan: interval %u timeout %d",
1384 params
.sched_scan_plans
[0].interval
,
1385 wpa_s
->sched_scan_timeout
);
1387 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting sched scan (no timeout)");
1390 wpa_setband_scan_freqs(wpa_s
, scan_params
);
1392 if (wpa_s
->mac_addr_rand_enable
& MAC_ADDR_RAND_SCHED_SCAN
) {
1393 params
.mac_addr_rand
= 1;
1394 if (wpa_s
->mac_addr_sched_scan
) {
1395 params
.mac_addr
= wpa_s
->mac_addr_sched_scan
;
1396 params
.mac_addr_mask
= wpa_s
->mac_addr_sched_scan
+
1401 ret
= wpa_supplicant_start_sched_scan(wpa_s
, scan_params
);
1402 wpabuf_free(extra_ie
);
1403 os_free(params
.filter_ssids
);
1405 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate sched scan");
1406 if (prev_state
!= wpa_s
->wpa_state
)
1407 wpa_supplicant_set_state(wpa_s
, prev_state
);
1411 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1412 if (ssid
|| !wpa_s
->first_sched_scan
) {
1413 wpa_s
->sched_scan_timed_out
= 0;
1414 eloop_register_timeout(wpa_s
->sched_scan_timeout
, 0,
1415 wpa_supplicant_sched_scan_timeout
,
1417 wpa_s
->first_sched_scan
= 0;
1418 wpa_s
->sched_scan_timeout
/= 2;
1419 params
.sched_scan_plans
[0].interval
*= 2;
1420 if ((unsigned int) wpa_s
->sched_scan_timeout
<
1421 params
.sched_scan_plans
[0].interval
||
1422 params
.sched_scan_plans
[0].interval
>
1423 wpa_s
->max_sched_scan_plan_interval
) {
1424 params
.sched_scan_plans
[0].interval
= 10;
1425 wpa_s
->sched_scan_timeout
= max_sched_scan_ssids
* 2;
1429 /* If there is no more ssids, start next time from the beginning */
1431 wpa_s
->prev_sched_ssid
= NULL
;
1438 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1439 * @wpa_s: Pointer to wpa_supplicant data
1441 * This function is used to cancel a scan request scheduled with
1442 * wpa_supplicant_req_scan().
1444 void wpa_supplicant_cancel_scan(struct wpa_supplicant
*wpa_s
)
1446 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling scan request");
1447 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
1452 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1453 * @wpa_s: Pointer to wpa_supplicant data
1455 * This function is used to stop a delayed scheduled scan.
1457 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant
*wpa_s
)
1459 if (!wpa_s
->sched_scan_supported
)
1462 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling delayed sched scan");
1463 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout
,
1469 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1470 * @wpa_s: Pointer to wpa_supplicant data
1472 * This function is used to stop a periodic scheduled scan.
1474 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant
*wpa_s
)
1476 if (!wpa_s
->sched_scanning
)
1479 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling sched scan");
1480 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout
, wpa_s
, NULL
);
1481 wpa_supplicant_stop_sched_scan(wpa_s
);
1486 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1487 * @wpa_s: Pointer to wpa_supplicant data
1488 * @scanning: Whether scanning is currently in progress
1490 * This function is to generate scanning notifycations. It is called whenever
1491 * there may have been a change in scanning (scan started, completed, stopped).
1492 * wpas_notify_scanning() is called whenever the scanning state changed from the
1493 * previously notified state.
1495 void wpa_supplicant_notify_scanning(struct wpa_supplicant
*wpa_s
,
1498 if (wpa_s
->scanning
!= scanning
) {
1499 wpa_s
->scanning
= scanning
;
1500 wpas_notify_scanning(wpa_s
);
1505 static int wpa_scan_get_max_rate(const struct wpa_scan_res
*res
)
1511 ie
= wpa_scan_get_ie(res
, WLAN_EID_SUPP_RATES
);
1512 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1513 if ((ie
[i
+ 2] & 0x7f) > rate
)
1514 rate
= ie
[i
+ 2] & 0x7f;
1517 ie
= wpa_scan_get_ie(res
, WLAN_EID_EXT_SUPP_RATES
);
1518 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1519 if ((ie
[i
+ 2] & 0x7f) > rate
)
1520 rate
= ie
[i
+ 2] & 0x7f;
1528 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1529 * @res: Scan result entry
1530 * @ie: Information element identitifier (WLAN_EID_*)
1531 * Returns: Pointer to the information element (id field) or %NULL if not found
1533 * This function returns the first matching information element in the scan
1536 const u8
* wpa_scan_get_ie(const struct wpa_scan_res
*res
, u8 ie
)
1538 return get_ie((const u8
*) (res
+ 1), res
->ie_len
, ie
);
1543 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1544 * @res: Scan result entry
1545 * @vendor_type: Vendor type (four octets starting the IE payload)
1546 * Returns: Pointer to the information element (id field) or %NULL if not found
1548 * This function returns the first matching information element in the scan
1551 const u8
* wpa_scan_get_vendor_ie(const struct wpa_scan_res
*res
,
1554 const u8
*end
, *pos
;
1556 pos
= (const u8
*) (res
+ 1);
1557 end
= pos
+ res
->ie_len
;
1559 while (end
- pos
> 1) {
1560 if (2 + pos
[1] > end
- pos
)
1562 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1563 vendor_type
== WPA_GET_BE32(&pos
[2]))
1573 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1574 * @res: Scan result entry
1575 * @vendor_type: Vendor type (four octets starting the IE payload)
1576 * Returns: Pointer to the information element (id field) or %NULL if not found
1578 * This function returns the first matching information element in the scan
1581 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1582 * from Beacon frames instead of either Beacon or Probe Response frames.
1584 const u8
* wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res
*res
,
1587 const u8
*end
, *pos
;
1589 if (res
->beacon_ie_len
== 0)
1592 pos
= (const u8
*) (res
+ 1);
1594 end
= pos
+ res
->beacon_ie_len
;
1596 while (end
- pos
> 1) {
1597 if (2 + pos
[1] > end
- pos
)
1599 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1600 vendor_type
== WPA_GET_BE32(&pos
[2]))
1610 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1611 * @res: Scan result entry
1612 * @vendor_type: Vendor type (four octets starting the IE payload)
1613 * Returns: Pointer to the information element payload or %NULL if not found
1615 * This function returns concatenated payload of possibly fragmented vendor
1616 * specific information elements in the scan result. The caller is responsible
1617 * for freeing the returned buffer.
1619 struct wpabuf
* wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res
*res
,
1623 const u8
*end
, *pos
;
1625 buf
= wpabuf_alloc(res
->ie_len
);
1629 pos
= (const u8
*) (res
+ 1);
1630 end
= pos
+ res
->ie_len
;
1632 while (end
- pos
> 1) {
1633 if (2 + pos
[1] > end
- pos
)
1635 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1636 vendor_type
== WPA_GET_BE32(&pos
[2]))
1637 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1641 if (wpabuf_len(buf
) == 0) {
1651 * Channels with a great SNR can operate at full rate. What is a great SNR?
1652 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1653 * rule of thumb is that any SNR above 20 is good." This one
1654 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1655 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1656 * conservative value.
1658 #define GREAT_SNR 30
1660 #define IS_5GHZ(n) (n > 4000)
1662 /* Compare function for sorting scan results. Return >0 if @b is considered
1664 static int wpa_scan_result_compar(const void *a
, const void *b
)
1666 #define MIN(a,b) a < b ? a : b
1667 struct wpa_scan_res
**_wa
= (void *) a
;
1668 struct wpa_scan_res
**_wb
= (void *) b
;
1669 struct wpa_scan_res
*wa
= *_wa
;
1670 struct wpa_scan_res
*wb
= *_wb
;
1672 int snr_a
, snr_b
, snr_a_full
, snr_b_full
;
1674 /* WPA/WPA2 support preferred */
1675 wpa_a
= wpa_scan_get_vendor_ie(wa
, WPA_IE_VENDOR_TYPE
) != NULL
||
1676 wpa_scan_get_ie(wa
, WLAN_EID_RSN
) != NULL
;
1677 wpa_b
= wpa_scan_get_vendor_ie(wb
, WPA_IE_VENDOR_TYPE
) != NULL
||
1678 wpa_scan_get_ie(wb
, WLAN_EID_RSN
) != NULL
;
1680 if (wpa_b
&& !wpa_a
)
1682 if (!wpa_b
&& wpa_a
)
1685 /* privacy support preferred */
1686 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) == 0 &&
1687 (wb
->caps
& IEEE80211_CAP_PRIVACY
))
1689 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) &&
1690 (wb
->caps
& IEEE80211_CAP_PRIVACY
) == 0)
1693 if (wa
->flags
& wb
->flags
& WPA_SCAN_LEVEL_DBM
) {
1694 snr_a_full
= wa
->snr
;
1695 snr_a
= MIN(wa
->snr
, GREAT_SNR
);
1696 snr_b_full
= wb
->snr
;
1697 snr_b
= MIN(wb
->snr
, GREAT_SNR
);
1699 /* Level is not in dBm, so we can't calculate
1700 * SNR. Just use raw level (units unknown). */
1701 snr_a
= snr_a_full
= wa
->level
;
1702 snr_b
= snr_b_full
= wb
->level
;
1705 /* if SNR is close, decide by max rate or frequency band */
1706 if ((snr_a
&& snr_b
&& abs(snr_b
- snr_a
) < 5) ||
1707 (wa
->qual
&& wb
->qual
&& abs(wb
->qual
- wa
->qual
) < 10)) {
1708 if (wa
->est_throughput
!= wb
->est_throughput
)
1709 return wb
->est_throughput
- wa
->est_throughput
;
1710 if (IS_5GHZ(wa
->freq
) ^ IS_5GHZ(wb
->freq
))
1711 return IS_5GHZ(wa
->freq
) ? -1 : 1;
1714 /* all things being equal, use SNR; if SNRs are
1715 * identical, use quality values since some drivers may only report
1716 * that value and leave the signal level zero */
1717 if (snr_b_full
== snr_a_full
)
1718 return wb
->qual
- wa
->qual
;
1719 return snr_b_full
- snr_a_full
;
1725 /* Compare function for sorting scan results when searching a WPS AP for
1726 * provisioning. Return >0 if @b is considered better. */
1727 static int wpa_scan_result_wps_compar(const void *a
, const void *b
)
1729 struct wpa_scan_res
**_wa
= (void *) a
;
1730 struct wpa_scan_res
**_wb
= (void *) b
;
1731 struct wpa_scan_res
*wa
= *_wa
;
1732 struct wpa_scan_res
*wb
= *_wb
;
1733 int uses_wps_a
, uses_wps_b
;
1734 struct wpabuf
*wps_a
, *wps_b
;
1737 /* Optimization - check WPS IE existence before allocated memory and
1738 * doing full reassembly. */
1739 uses_wps_a
= wpa_scan_get_vendor_ie(wa
, WPS_IE_VENDOR_TYPE
) != NULL
;
1740 uses_wps_b
= wpa_scan_get_vendor_ie(wb
, WPS_IE_VENDOR_TYPE
) != NULL
;
1741 if (uses_wps_a
&& !uses_wps_b
)
1743 if (!uses_wps_a
&& uses_wps_b
)
1746 if (uses_wps_a
&& uses_wps_b
) {
1747 wps_a
= wpa_scan_get_vendor_ie_multi(wa
, WPS_IE_VENDOR_TYPE
);
1748 wps_b
= wpa_scan_get_vendor_ie_multi(wb
, WPS_IE_VENDOR_TYPE
);
1749 res
= wps_ap_priority_compar(wps_a
, wps_b
);
1757 * Do not use current AP security policy as a sorting criteria during
1758 * WPS provisioning step since the AP may get reconfigured at the
1759 * completion of provisioning.
1762 /* all things being equal, use signal level; if signal levels are
1763 * identical, use quality values since some drivers may only report
1764 * that value and leave the signal level zero */
1765 if (wb
->level
== wa
->level
)
1766 return wb
->qual
- wa
->qual
;
1767 return wb
->level
- wa
->level
;
1769 #endif /* CONFIG_WPS */
1772 static void dump_scan_res(struct wpa_scan_results
*scan_res
)
1774 #ifndef CONFIG_NO_STDOUT_DEBUG
1777 if (scan_res
->res
== NULL
|| scan_res
->num
== 0)
1780 wpa_printf(MSG_EXCESSIVE
, "Sorted scan results");
1782 for (i
= 0; i
< scan_res
->num
; i
++) {
1783 struct wpa_scan_res
*r
= scan_res
->res
[i
];
1785 if (r
->flags
& WPA_SCAN_LEVEL_DBM
) {
1786 int noise_valid
= !(r
->flags
& WPA_SCAN_NOISE_INVALID
);
1788 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1789 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
1790 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1791 r
->noise
, noise_valid
? "" : "~", r
->level
,
1792 r
->snr
, r
->snr
>= GREAT_SNR
? "*" : "",
1794 r
->age
, r
->est_throughput
);
1796 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1797 "noise=%d level=%d flags=0x%x age=%u est=%u",
1798 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1799 r
->noise
, r
->level
, r
->flags
, r
->age
,
1802 pos
= (u8
*) (r
+ 1);
1804 wpa_hexdump(MSG_EXCESSIVE
, "IEs", pos
, r
->ie_len
);
1806 if (r
->beacon_ie_len
)
1807 wpa_hexdump(MSG_EXCESSIVE
, "Beacon IEs",
1808 pos
, r
->beacon_ie_len
);
1810 #endif /* CONFIG_NO_STDOUT_DEBUG */
1815 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1816 * @wpa_s: Pointer to wpa_supplicant data
1817 * @bssid: BSSID to check
1818 * Returns: 0 if the BSSID is filtered or 1 if not
1820 * This function is used to filter out specific BSSIDs from scan reslts mainly
1821 * for testing purposes (SET bssid_filter ctrl_iface command).
1823 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant
*wpa_s
,
1828 if (wpa_s
->bssid_filter
== NULL
)
1831 for (i
= 0; i
< wpa_s
->bssid_filter_count
; i
++) {
1832 if (os_memcmp(wpa_s
->bssid_filter
+ i
* ETH_ALEN
, bssid
,
1841 static void filter_scan_res(struct wpa_supplicant
*wpa_s
,
1842 struct wpa_scan_results
*res
)
1846 if (wpa_s
->bssid_filter
== NULL
)
1849 for (i
= 0, j
= 0; i
< res
->num
; i
++) {
1850 if (wpa_supplicant_filter_bssid_match(wpa_s
,
1851 res
->res
[i
]->bssid
)) {
1852 res
->res
[j
++] = res
->res
[i
];
1854 os_free(res
->res
[i
]);
1859 if (res
->num
!= j
) {
1860 wpa_printf(MSG_DEBUG
, "Filtered out %d scan results",
1861 (int) (res
->num
- j
));
1868 * Noise floor values to use when we have signal strength
1869 * measurements, but no noise floor measurments. These values were
1870 * measured in an office environment with many APs.
1872 #define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1873 #define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1875 static void scan_snr(struct wpa_scan_res
*res
)
1877 if (res
->flags
& WPA_SCAN_NOISE_INVALID
) {
1878 res
->noise
= IS_5GHZ(res
->freq
) ?
1879 DEFAULT_NOISE_FLOOR_5GHZ
:
1880 DEFAULT_NOISE_FLOOR_2GHZ
;
1883 if (res
->flags
& WPA_SCAN_LEVEL_DBM
) {
1884 res
->snr
= res
->level
- res
->noise
;
1886 /* Level is not in dBm, so we can't calculate
1887 * SNR. Just use raw level (units unknown). */
1888 res
->snr
= res
->level
;
1893 static unsigned int max_ht20_rate(int snr
)
1896 return 6500; /* HT20 MCS0 */
1898 return 13000; /* HT20 MCS1 */
1900 return 19500; /* HT20 MCS2 */
1902 return 26000; /* HT20 MCS3 */
1904 return 39000; /* HT20 MCS4 */
1906 return 52000; /* HT20 MCS5 */
1908 return 58500; /* HT20 MCS6 */
1909 return 65000; /* HT20 MCS7 */
1913 static unsigned int max_ht40_rate(int snr
)
1916 return 13500; /* HT40 MCS0 */
1918 return 27000; /* HT40 MCS1 */
1920 return 40500; /* HT40 MCS2 */
1922 return 54000; /* HT40 MCS3 */
1924 return 81000; /* HT40 MCS4 */
1926 return 108000; /* HT40 MCS5 */
1928 return 121500; /* HT40 MCS6 */
1929 return 135000; /* HT40 MCS7 */
1933 static unsigned int max_vht80_rate(int snr
)
1938 return 29300; /* VHT80 MCS0 */
1940 return 58500; /* VHT80 MCS1 */
1942 return 87800; /* VHT80 MCS2 */
1944 return 117000; /* VHT80 MCS3 */
1946 return 175500; /* VHT80 MCS4 */
1948 return 234000; /* VHT80 MCS5 */
1950 return 263300; /* VHT80 MCS6 */
1952 return 292500; /* VHT80 MCS7 */
1954 return 351000; /* VHT80 MCS8 */
1955 return 390000; /* VHT80 MCS9 */
1959 static void scan_est_throughput(struct wpa_supplicant
*wpa_s
,
1960 struct wpa_scan_res
*res
)
1962 enum local_hw_capab capab
= wpa_s
->hw_capab
;
1963 int rate
; /* max legacy rate in 500 kb/s units */
1965 unsigned int est
, tmp
;
1968 if (res
->est_throughput
)
1971 /* Get maximum legacy rate */
1972 rate
= wpa_scan_get_max_rate(res
);
1974 /* Limit based on estimated SNR */
1975 if (rate
> 1 * 2 && snr
< 1)
1977 else if (rate
> 2 * 2 && snr
< 4)
1979 else if (rate
> 6 * 2 && snr
< 5)
1981 else if (rate
> 9 * 2 && snr
< 6)
1983 else if (rate
> 12 * 2 && snr
< 7)
1985 else if (rate
> 18 * 2 && snr
< 10)
1987 else if (rate
> 24 * 2 && snr
< 11)
1989 else if (rate
> 36 * 2 && snr
< 15)
1991 else if (rate
> 48 * 2 && snr
< 19)
1993 else if (rate
> 54 * 2 && snr
< 21)
1997 if (capab
== CAPAB_HT
|| capab
== CAPAB_HT40
|| capab
== CAPAB_VHT
) {
1998 ie
= wpa_scan_get_ie(res
, WLAN_EID_HT_CAP
);
2000 tmp
= max_ht20_rate(snr
);
2006 if (capab
== CAPAB_HT40
|| capab
== CAPAB_VHT
) {
2007 ie
= wpa_scan_get_ie(res
, WLAN_EID_HT_OPERATION
);
2008 if (ie
&& ie
[1] >= 2 &&
2009 (ie
[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK
)) {
2010 tmp
= max_ht40_rate(snr
);
2016 if (capab
== CAPAB_VHT
) {
2017 /* Use +1 to assume VHT is always faster than HT */
2018 ie
= wpa_scan_get_ie(res
, WLAN_EID_VHT_CAP
);
2020 tmp
= max_ht20_rate(snr
) + 1;
2024 ie
= wpa_scan_get_ie(res
, WLAN_EID_HT_OPERATION
);
2025 if (ie
&& ie
[1] >= 2 &&
2027 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK
)) {
2028 tmp
= max_ht40_rate(snr
) + 1;
2033 ie
= wpa_scan_get_ie(res
, WLAN_EID_VHT_OPERATION
);
2034 if (ie
&& ie
[1] >= 1 &&
2035 (ie
[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK
)) {
2036 tmp
= max_vht80_rate(snr
) + 1;
2043 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2045 res
->est_throughput
= est
;
2050 * wpa_supplicant_get_scan_results - Get scan results
2051 * @wpa_s: Pointer to wpa_supplicant data
2052 * @info: Information about what was scanned or %NULL if not available
2053 * @new_scan: Whether a new scan was performed
2054 * Returns: Scan results, %NULL on failure
2056 * This function request the current scan results from the driver and updates
2057 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2058 * results with wpa_scan_results_free().
2060 struct wpa_scan_results
*
2061 wpa_supplicant_get_scan_results(struct wpa_supplicant
*wpa_s
,
2062 struct scan_info
*info
, int new_scan
)
2064 struct wpa_scan_results
*scan_res
;
2066 int (*compar
)(const void *, const void *) = wpa_scan_result_compar
;
2068 scan_res
= wpa_drv_get_scan_results2(wpa_s
);
2069 if (scan_res
== NULL
) {
2070 wpa_dbg(wpa_s
, MSG_DEBUG
, "Failed to get scan results");
2073 if (scan_res
->fetch_time
.sec
== 0) {
2075 * Make sure we have a valid timestamp if the driver wrapper
2076 * does not set this.
2078 os_get_reltime(&scan_res
->fetch_time
);
2080 filter_scan_res(wpa_s
, scan_res
);
2082 for (i
= 0; i
< scan_res
->num
; i
++) {
2083 struct wpa_scan_res
*scan_res_item
= scan_res
->res
[i
];
2085 scan_snr(scan_res_item
);
2086 scan_est_throughput(wpa_s
, scan_res_item
);
2090 if (wpas_wps_searching(wpa_s
)) {
2091 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Order scan results with WPS "
2092 "provisioning rules");
2093 compar
= wpa_scan_result_wps_compar
;
2095 #endif /* CONFIG_WPS */
2097 qsort(scan_res
->res
, scan_res
->num
, sizeof(struct wpa_scan_res
*),
2099 dump_scan_res(scan_res
);
2101 wpa_bss_update_start(wpa_s
);
2102 for (i
= 0; i
< scan_res
->num
; i
++)
2103 wpa_bss_update_scan_res(wpa_s
, scan_res
->res
[i
],
2104 &scan_res
->fetch_time
);
2105 wpa_bss_update_end(wpa_s
, info
, new_scan
);
2112 * wpa_supplicant_update_scan_results - Update scan results from the driver
2113 * @wpa_s: Pointer to wpa_supplicant data
2114 * Returns: 0 on success, -1 on failure
2116 * This function updates the BSS table within wpa_supplicant based on the
2117 * currently available scan results from the driver without requesting a new
2118 * scan. This is used in cases where the driver indicates an association
2119 * (including roaming within ESS) and wpa_supplicant does not yet have the
2120 * needed information to complete the connection (e.g., to perform validation
2121 * steps in 4-way handshake).
2123 int wpa_supplicant_update_scan_results(struct wpa_supplicant
*wpa_s
)
2125 struct wpa_scan_results
*scan_res
;
2126 scan_res
= wpa_supplicant_get_scan_results(wpa_s
, NULL
, 0);
2127 if (scan_res
== NULL
)
2129 wpa_scan_results_free(scan_res
);
2136 * scan_only_handler - Reports scan results
2138 void scan_only_handler(struct wpa_supplicant
*wpa_s
,
2139 struct wpa_scan_results
*scan_res
)
2141 wpa_dbg(wpa_s
, MSG_DEBUG
, "Scan-only results received");
2142 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
2143 wpa_s
->manual_scan_use_id
&& wpa_s
->own_scan_running
) {
2144 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_RESULTS
"id=%u",
2145 wpa_s
->manual_scan_id
);
2146 wpa_s
->manual_scan_use_id
= 0;
2148 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_RESULTS
);
2150 wpas_notify_scan_results(wpa_s
);
2151 wpas_notify_scan_done(wpa_s
, 1);
2152 if (wpa_s
->scan_work
) {
2153 struct wpa_radio_work
*work
= wpa_s
->scan_work
;
2154 wpa_s
->scan_work
= NULL
;
2155 radio_work_done(work
);
2158 if (wpa_s
->wpa_state
== WPA_SCANNING
)
2159 wpa_supplicant_set_state(wpa_s
, wpa_s
->scan_prev_wpa_state
);
2163 int wpas_scan_scheduled(struct wpa_supplicant
*wpa_s
)
2165 return eloop_is_timeout_registered(wpa_supplicant_scan
, wpa_s
, NULL
);
2169 struct wpa_driver_scan_params
*
2170 wpa_scan_clone_params(const struct wpa_driver_scan_params
*src
)
2172 struct wpa_driver_scan_params
*params
;
2176 params
= os_zalloc(sizeof(*params
));
2180 for (i
= 0; i
< src
->num_ssids
; i
++) {
2181 if (src
->ssids
[i
].ssid
) {
2182 n
= os_malloc(src
->ssids
[i
].ssid_len
);
2185 os_memcpy(n
, src
->ssids
[i
].ssid
,
2186 src
->ssids
[i
].ssid_len
);
2187 params
->ssids
[i
].ssid
= n
;
2188 params
->ssids
[i
].ssid_len
= src
->ssids
[i
].ssid_len
;
2191 params
->num_ssids
= src
->num_ssids
;
2193 if (src
->extra_ies
) {
2194 n
= os_malloc(src
->extra_ies_len
);
2197 os_memcpy(n
, src
->extra_ies
, src
->extra_ies_len
);
2198 params
->extra_ies
= n
;
2199 params
->extra_ies_len
= src
->extra_ies_len
;
2203 int len
= int_array_len(src
->freqs
);
2204 params
->freqs
= os_malloc((len
+ 1) * sizeof(int));
2205 if (params
->freqs
== NULL
)
2207 os_memcpy(params
->freqs
, src
->freqs
, (len
+ 1) * sizeof(int));
2210 if (src
->filter_ssids
) {
2211 params
->filter_ssids
= os_malloc(sizeof(*params
->filter_ssids
) *
2212 src
->num_filter_ssids
);
2213 if (params
->filter_ssids
== NULL
)
2215 os_memcpy(params
->filter_ssids
, src
->filter_ssids
,
2216 sizeof(*params
->filter_ssids
) *
2217 src
->num_filter_ssids
);
2218 params
->num_filter_ssids
= src
->num_filter_ssids
;
2221 params
->filter_rssi
= src
->filter_rssi
;
2222 params
->p2p_probe
= src
->p2p_probe
;
2223 params
->only_new_results
= src
->only_new_results
;
2224 params
->low_priority
= src
->low_priority
;
2226 if (src
->sched_scan_plans_num
> 0) {
2227 params
->sched_scan_plans
=
2228 os_malloc(sizeof(*src
->sched_scan_plans
) *
2229 src
->sched_scan_plans_num
);
2230 if (!params
->sched_scan_plans
)
2233 os_memcpy(params
->sched_scan_plans
, src
->sched_scan_plans
,
2234 sizeof(*src
->sched_scan_plans
) *
2235 src
->sched_scan_plans_num
);
2236 params
->sched_scan_plans_num
= src
->sched_scan_plans_num
;
2239 if (src
->mac_addr_rand
) {
2240 params
->mac_addr_rand
= src
->mac_addr_rand
;
2242 if (src
->mac_addr
&& src
->mac_addr_mask
) {
2245 mac_addr
= os_malloc(2 * ETH_ALEN
);
2249 os_memcpy(mac_addr
, src
->mac_addr
, ETH_ALEN
);
2250 os_memcpy(mac_addr
+ ETH_ALEN
, src
->mac_addr_mask
,
2252 params
->mac_addr
= mac_addr
;
2253 params
->mac_addr_mask
= mac_addr
+ ETH_ALEN
;
2259 wpa_scan_free_params(params
);
2264 void wpa_scan_free_params(struct wpa_driver_scan_params
*params
)
2271 for (i
= 0; i
< params
->num_ssids
; i
++)
2272 os_free((u8
*) params
->ssids
[i
].ssid
);
2273 os_free((u8
*) params
->extra_ies
);
2274 os_free(params
->freqs
);
2275 os_free(params
->filter_ssids
);
2276 os_free(params
->sched_scan_plans
);
2279 * Note: params->mac_addr_mask points to same memory allocation and
2280 * must not be freed separately.
2282 os_free((u8
*) params
->mac_addr
);
2288 int wpas_start_pno(struct wpa_supplicant
*wpa_s
)
2291 size_t i
, num_ssid
, num_match_ssid
;
2292 struct wpa_ssid
*ssid
;
2293 struct wpa_driver_scan_params params
;
2294 struct sched_scan_plan scan_plan
;
2296 if (!wpa_s
->sched_scan_supported
)
2299 if (wpa_s
->pno
|| wpa_s
->pno_sched_pending
)
2302 if ((wpa_s
->wpa_state
> WPA_SCANNING
) &&
2303 (wpa_s
->wpa_state
<= WPA_COMPLETED
)) {
2304 wpa_printf(MSG_ERROR
, "PNO: In assoc process");
2308 if (wpa_s
->wpa_state
== WPA_SCANNING
) {
2309 wpa_supplicant_cancel_scan(wpa_s
);
2310 if (wpa_s
->sched_scanning
) {
2311 wpa_printf(MSG_DEBUG
, "Schedule PNO on completion of "
2312 "ongoing sched scan");
2313 wpa_supplicant_cancel_sched_scan(wpa_s
);
2314 wpa_s
->pno_sched_pending
= 1;
2319 os_memset(¶ms
, 0, sizeof(params
));
2321 num_ssid
= num_match_ssid
= 0;
2322 ssid
= wpa_s
->conf
->ssid
;
2324 if (!wpas_network_disabled(wpa_s
, ssid
)) {
2326 if (ssid
->scan_ssid
)
2332 if (num_match_ssid
== 0) {
2333 wpa_printf(MSG_DEBUG
, "PNO: No configured SSIDs");
2337 if (num_match_ssid
> num_ssid
) {
2338 params
.num_ssids
++; /* wildcard */
2342 if (num_ssid
> WPAS_MAX_SCAN_SSIDS
) {
2343 wpa_printf(MSG_DEBUG
, "PNO: Use only the first %u SSIDs from "
2344 "%u", WPAS_MAX_SCAN_SSIDS
, (unsigned int) num_ssid
);
2345 num_ssid
= WPAS_MAX_SCAN_SSIDS
;
2348 if (num_match_ssid
> wpa_s
->max_match_sets
) {
2349 num_match_ssid
= wpa_s
->max_match_sets
;
2350 wpa_dbg(wpa_s
, MSG_DEBUG
, "PNO: Too many SSIDs to match");
2352 params
.filter_ssids
= os_calloc(num_match_ssid
,
2353 sizeof(struct wpa_driver_scan_filter
));
2354 if (params
.filter_ssids
== NULL
)
2359 ssid
= wpa_s
->conf
->pssid
[prio
];
2361 if (!wpas_network_disabled(wpa_s
, ssid
)) {
2362 if (ssid
->scan_ssid
&& params
.num_ssids
< num_ssid
) {
2363 params
.ssids
[params
.num_ssids
].ssid
=
2365 params
.ssids
[params
.num_ssids
].ssid_len
=
2369 os_memcpy(params
.filter_ssids
[i
].ssid
, ssid
->ssid
,
2371 params
.filter_ssids
[i
].ssid_len
= ssid
->ssid_len
;
2372 params
.num_filter_ssids
++;
2374 if (i
== num_match_ssid
)
2379 else if (prio
+ 1 == wpa_s
->conf
->num_prio
)
2382 ssid
= wpa_s
->conf
->pssid
[++prio
];
2385 if (wpa_s
->conf
->filter_rssi
)
2386 params
.filter_rssi
= wpa_s
->conf
->filter_rssi
;
2388 if (wpa_s
->sched_scan_plans_num
) {
2389 params
.sched_scan_plans
= wpa_s
->sched_scan_plans
;
2390 params
.sched_scan_plans_num
= wpa_s
->sched_scan_plans_num
;
2392 /* Set one scan plan that will run infinitely */
2393 if (wpa_s
->conf
->sched_scan_interval
)
2394 scan_plan
.interval
= wpa_s
->conf
->sched_scan_interval
;
2396 scan_plan
.interval
= 10;
2398 scan_plan
.iterations
= 0;
2399 params
.sched_scan_plans
= &scan_plan
;
2400 params
.sched_scan_plans_num
= 1;
2403 if (params
.freqs
== NULL
&& wpa_s
->manual_sched_scan_freqs
) {
2404 wpa_dbg(wpa_s
, MSG_DEBUG
, "Limit sched scan to specified channels");
2405 params
.freqs
= wpa_s
->manual_sched_scan_freqs
;
2408 if (wpa_s
->mac_addr_rand_enable
& MAC_ADDR_RAND_PNO
) {
2409 params
.mac_addr_rand
= 1;
2410 if (wpa_s
->mac_addr_pno
) {
2411 params
.mac_addr
= wpa_s
->mac_addr_pno
;
2412 params
.mac_addr_mask
= wpa_s
->mac_addr_pno
+ ETH_ALEN
;
2416 ret
= wpa_supplicant_start_sched_scan(wpa_s
, ¶ms
);
2417 os_free(params
.filter_ssids
);
2421 wpa_msg(wpa_s
, MSG_ERROR
, "Failed to schedule PNO");
2426 int wpas_stop_pno(struct wpa_supplicant
*wpa_s
)
2433 ret
= wpa_supplicant_stop_sched_scan(wpa_s
);
2436 wpa_s
->pno_sched_pending
= 0;
2438 if (wpa_s
->wpa_state
== WPA_SCANNING
)
2439 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2445 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant
*wpa_s
,
2448 type
&= MAC_ADDR_RAND_ALL
;
2449 wpa_s
->mac_addr_rand_enable
&= ~type
;
2451 if (type
& MAC_ADDR_RAND_SCAN
) {
2452 os_free(wpa_s
->mac_addr_scan
);
2453 wpa_s
->mac_addr_scan
= NULL
;
2456 if (type
& MAC_ADDR_RAND_SCHED_SCAN
) {
2457 os_free(wpa_s
->mac_addr_sched_scan
);
2458 wpa_s
->mac_addr_sched_scan
= NULL
;
2461 if (type
& MAC_ADDR_RAND_PNO
) {
2462 os_free(wpa_s
->mac_addr_pno
);
2463 wpa_s
->mac_addr_pno
= NULL
;
2468 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant
*wpa_s
,
2469 unsigned int type
, const u8
*addr
,
2474 wpas_mac_addr_rand_scan_clear(wpa_s
, type
);
2477 tmp
= os_malloc(2 * ETH_ALEN
);
2480 os_memcpy(tmp
, addr
, ETH_ALEN
);
2481 os_memcpy(tmp
+ ETH_ALEN
, mask
, ETH_ALEN
);
2484 if (type
== MAC_ADDR_RAND_SCAN
) {
2485 wpa_s
->mac_addr_scan
= tmp
;
2486 } else if (type
== MAC_ADDR_RAND_SCHED_SCAN
) {
2487 wpa_s
->mac_addr_sched_scan
= tmp
;
2488 } else if (type
== MAC_ADDR_RAND_PNO
) {
2489 wpa_s
->mac_addr_pno
= tmp
;
2491 wpa_printf(MSG_INFO
,
2492 "scan: Invalid MAC randomization type=0x%x",
2498 wpa_s
->mac_addr_rand_enable
|= type
;
2503 int wpas_abort_ongoing_scan(struct wpa_supplicant
*wpa_s
)
2505 if (wpa_s
->scan_work
&& wpa_s
->own_scan_running
) {
2506 wpa_dbg(wpa_s
, MSG_DEBUG
, "Abort an ongoing scan");
2507 return wpa_drv_abort_scan(wpa_s
);
2514 int wpas_sched_scan_plans_set(struct wpa_supplicant
*wpa_s
, const char *cmd
)
2516 struct sched_scan_plan
*scan_plans
= NULL
;
2517 const char *token
, *context
= NULL
;
2518 unsigned int num
= 0;
2524 wpa_printf(MSG_DEBUG
, "Clear sched scan plans");
2525 os_free(wpa_s
->sched_scan_plans
);
2526 wpa_s
->sched_scan_plans
= NULL
;
2527 wpa_s
->sched_scan_plans_num
= 0;
2531 while ((token
= cstr_token(cmd
, " ", &context
))) {
2533 struct sched_scan_plan
*scan_plan
, *n
;
2535 n
= os_realloc_array(scan_plans
, num
+ 1, sizeof(*scan_plans
));
2540 scan_plan
= &scan_plans
[num
];
2543 ret
= sscanf(token
, "%u:%u", &scan_plan
->interval
,
2544 &scan_plan
->iterations
);
2545 if (ret
<= 0 || ret
> 2 || !scan_plan
->interval
) {
2546 wpa_printf(MSG_ERROR
,
2547 "Invalid sched scan plan input: %s", token
);
2551 if (!scan_plan
->interval
) {
2552 wpa_printf(MSG_ERROR
,
2553 "scan plan %u: Interval cannot be zero",
2558 if (scan_plan
->interval
> wpa_s
->max_sched_scan_plan_interval
) {
2559 wpa_printf(MSG_WARNING
,
2560 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2561 num
, scan_plan
->interval
,
2562 wpa_s
->max_sched_scan_plan_interval
);
2563 scan_plan
->interval
=
2564 wpa_s
->max_sched_scan_plan_interval
;
2568 scan_plan
->iterations
= 0;
2572 if (!scan_plan
->iterations
) {
2573 wpa_printf(MSG_ERROR
,
2574 "scan plan %u: Number of iterations cannot be zero",
2579 if (scan_plan
->iterations
>
2580 wpa_s
->max_sched_scan_plan_iterations
) {
2581 wpa_printf(MSG_WARNING
,
2582 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2583 num
, scan_plan
->iterations
,
2584 wpa_s
->max_sched_scan_plan_iterations
);
2585 scan_plan
->iterations
=
2586 wpa_s
->max_sched_scan_plan_iterations
;
2589 wpa_printf(MSG_DEBUG
,
2590 "scan plan %u: interval=%u iterations=%u",
2591 num
, scan_plan
->interval
, scan_plan
->iterations
);
2595 wpa_printf(MSG_ERROR
, "Invalid scan plans entry");
2599 if (cstr_token(cmd
, " ", &context
) || scan_plans
[num
- 1].iterations
) {
2600 wpa_printf(MSG_ERROR
,
2601 "All scan plans but the last must specify a number of iterations");
2605 wpa_printf(MSG_DEBUG
, "scan plan %u (last plan): interval=%u",
2606 num
, scan_plans
[num
- 1].interval
);
2608 if (num
> wpa_s
->max_sched_scan_plans
) {
2609 wpa_printf(MSG_WARNING
,
2610 "Too many scheduled scan plans (only %u supported)",
2611 wpa_s
->max_sched_scan_plans
);
2612 wpa_printf(MSG_WARNING
,
2613 "Use only the first %u scan plans, and the last one (in infinite loop)",
2614 wpa_s
->max_sched_scan_plans
- 1);
2615 os_memcpy(&scan_plans
[wpa_s
->max_sched_scan_plans
- 1],
2616 &scan_plans
[num
- 1], sizeof(*scan_plans
));
2617 num
= wpa_s
->max_sched_scan_plans
;
2620 os_free(wpa_s
->sched_scan_plans
);
2621 wpa_s
->sched_scan_plans
= scan_plans
;
2622 wpa_s
->sched_scan_plans_num
= num
;
2627 os_free(scan_plans
);
2628 wpa_printf(MSG_ERROR
, "invalid scan plans list");