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 wpas_notify_network_changed(wpa_s
);
41 wpa_supplicant_initiate_eapol(wpa_s
);
42 wpa_dbg(wpa_s
, MSG_DEBUG
, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data
, 0, sizeof(data
));
45 wpa_supplicant_event(wpa_s
, EVENT_ASSOC
, &data
);
50 static int wpas_wps_in_use(struct wpa_supplicant
*wpa_s
,
51 enum wps_request_type
*req_type
)
53 struct wpa_ssid
*ssid
;
56 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
57 if (!(ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))
61 *req_type
= wpas_wps_get_req_type(ssid
);
62 if (ssid
->eap
.phase1
&& os_strstr(ssid
->eap
.phase1
, "pbc=1"))
67 if (!wpa_s
->global
->p2p_disabled
&& wpa_s
->global
->p2p
&&
68 !wpa_s
->conf
->p2p_disabled
) {
69 wpa_s
->wps
->dev
.p2p
= 1;
72 *req_type
= WPS_REQ_ENROLLEE_INFO
;
75 #endif /* CONFIG_P2P */
79 #endif /* CONFIG_WPS */
83 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
84 * @wpa_s: Pointer to wpa_supplicant data
85 * Returns: 0 if no networks are enabled, >0 if networks are enabled
87 * This function is used to figure out whether any networks (or Interworking
88 * with enabled credentials and auto_interworking) are present in the current
91 int wpa_supplicant_enabled_networks(struct wpa_supplicant
*wpa_s
)
93 struct wpa_ssid
*ssid
= wpa_s
->conf
->ssid
;
94 int count
= 0, disabled
= 0;
97 return 0; /* no normal network profiles on p2p_mgmt interface */
100 if (!wpas_network_disabled(wpa_s
, ssid
))
106 if (wpa_s
->conf
->cred
&& wpa_s
->conf
->interworking
&&
107 wpa_s
->conf
->auto_interworking
)
109 if (count
== 0 && disabled
> 0) {
110 wpa_dbg(wpa_s
, MSG_DEBUG
, "No enabled networks (%d disabled "
111 "networks)", disabled
);
117 static void wpa_supplicant_assoc_try(struct wpa_supplicant
*wpa_s
,
118 struct wpa_ssid
*ssid
)
121 if (!wpas_network_disabled(wpa_s
, ssid
))
126 /* ap_scan=2 mode - try to associate with each SSID. */
128 wpa_dbg(wpa_s
, MSG_DEBUG
, "wpa_supplicant_assoc_try: Reached "
129 "end of scan list - go back to beginning");
130 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
131 wpa_supplicant_req_scan(wpa_s
, 0, 0);
135 /* Continue from the next SSID on the next attempt. */
136 wpa_s
->prev_scan_ssid
= ssid
;
138 /* Start from the beginning of the SSID list. */
139 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
141 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
145 static void wpas_trigger_scan_cb(struct wpa_radio_work
*work
, int deinit
)
147 struct wpa_supplicant
*wpa_s
= work
->wpa_s
;
148 struct wpa_driver_scan_params
*params
= work
->ctx
;
152 if (!work
->started
) {
153 wpa_scan_free_params(params
);
156 wpa_supplicant_notify_scanning(wpa_s
, 0);
157 wpas_notify_scan_done(wpa_s
, 0);
158 wpa_s
->scan_work
= NULL
;
162 if (wpas_update_random_addr_disassoc(wpa_s
) < 0) {
163 wpa_msg(wpa_s
, MSG_INFO
,
164 "Failed to assign random MAC address for a scan");
165 wpa_scan_free_params(params
);
166 wpa_msg(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_FAILED
"ret=-1");
167 radio_work_done(work
);
171 wpa_supplicant_notify_scanning(wpa_s
, 1);
173 if (wpa_s
->clear_driver_scan_cache
) {
174 wpa_printf(MSG_DEBUG
,
175 "Request driver to clear scan cache due to local BSS flush");
176 params
->only_new_results
= 1;
178 ret
= wpa_drv_scan(wpa_s
, params
);
180 * Store the obtained vendor scan cookie (if any) in wpa_s context.
181 * The current design is to allow only one scan request on each
182 * interface, hence having this scan cookie stored in wpa_s context is
185 * Revisit this logic if concurrent scan operations per interface
189 wpa_s
->curr_scan_cookie
= params
->scan_cookie
;
190 wpa_scan_free_params(params
);
193 int retry
= wpa_s
->last_scan_req
!= MANUAL_SCAN_REQ
;
195 if (wpa_s
->disconnected
)
198 wpa_supplicant_notify_scanning(wpa_s
, 0);
199 wpas_notify_scan_done(wpa_s
, 0);
200 if (wpa_s
->wpa_state
== WPA_SCANNING
)
201 wpa_supplicant_set_state(wpa_s
,
202 wpa_s
->scan_prev_wpa_state
);
203 wpa_msg(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_FAILED
"ret=%d%s",
204 ret
, retry
? " retry=1" : "");
205 radio_work_done(work
);
208 /* Restore scan_req since we will try to scan again */
209 wpa_s
->scan_req
= wpa_s
->last_scan_req
;
210 wpa_supplicant_req_scan(wpa_s
, 1, 0);
215 os_get_reltime(&wpa_s
->scan_trigger_time
);
217 wpa_s
->normal_scans
++;
218 wpa_s
->own_scan_requested
= 1;
219 wpa_s
->clear_driver_scan_cache
= 0;
220 wpa_s
->scan_work
= work
;
225 * wpa_supplicant_trigger_scan - Request driver to start a scan
226 * @wpa_s: Pointer to wpa_supplicant data
227 * @params: Scan parameters
228 * Returns: 0 on success, -1 on failure
230 int wpa_supplicant_trigger_scan(struct wpa_supplicant
*wpa_s
,
231 struct wpa_driver_scan_params
*params
)
233 struct wpa_driver_scan_params
*ctx
;
235 if (wpa_s
->scan_work
) {
236 wpa_dbg(wpa_s
, MSG_INFO
, "Reject scan trigger since one is already pending");
240 ctx
= wpa_scan_clone_params(params
);
242 radio_add_work(wpa_s
, 0, "scan", 0, wpas_trigger_scan_cb
, ctx
) < 0)
244 wpa_scan_free_params(ctx
);
245 wpa_msg(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_FAILED
"ret=-1");
254 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
256 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
258 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting delayed sched scan");
260 if (wpa_supplicant_req_sched_scan(wpa_s
))
261 wpa_supplicant_req_scan(wpa_s
, 0, 0);
266 wpa_supplicant_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
268 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
270 wpa_dbg(wpa_s
, MSG_DEBUG
, "Sched scan timeout - stopping it");
272 wpa_s
->sched_scan_timed_out
= 1;
273 wpa_supplicant_cancel_sched_scan(wpa_s
);
278 wpa_supplicant_start_sched_scan(struct wpa_supplicant
*wpa_s
,
279 struct wpa_driver_scan_params
*params
)
283 wpa_supplicant_notify_scanning(wpa_s
, 1);
284 ret
= wpa_drv_sched_scan(wpa_s
, params
);
286 wpa_supplicant_notify_scanning(wpa_s
, 0);
288 wpa_s
->sched_scanning
= 1;
294 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant
*wpa_s
)
298 ret
= wpa_drv_stop_sched_scan(wpa_s
);
300 wpa_dbg(wpa_s
, MSG_DEBUG
, "stopping sched_scan failed!");
301 /* TODO: what to do if stopping fails? */
309 static struct wpa_driver_scan_filter
*
310 wpa_supplicant_build_filter_ssids(struct wpa_config
*conf
, size_t *num_ssids
)
312 struct wpa_driver_scan_filter
*ssids
;
313 struct wpa_ssid
*ssid
;
317 if (!conf
->filter_ssids
)
320 for (count
= 0, ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
321 if (ssid
->ssid
&& ssid
->ssid_len
)
326 ssids
= os_calloc(count
, sizeof(struct wpa_driver_scan_filter
));
330 for (ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
331 if (!ssid
->ssid
|| !ssid
->ssid_len
)
333 os_memcpy(ssids
[*num_ssids
].ssid
, ssid
->ssid
, ssid
->ssid_len
);
334 ssids
[*num_ssids
].ssid_len
= ssid
->ssid_len
;
342 static void wpa_supplicant_optimize_freqs(
343 struct wpa_supplicant
*wpa_s
, struct wpa_driver_scan_params
*params
)
346 if (params
->freqs
== NULL
&& wpa_s
->p2p_in_provisioning
&&
348 /* Optimize provisioning state scan based on GO information */
349 if (wpa_s
->p2p_in_provisioning
< 5 &&
350 wpa_s
->go_params
->freq
> 0) {
351 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only GO "
352 "preferred frequency %d MHz",
353 wpa_s
->go_params
->freq
);
354 params
->freqs
= os_calloc(2, sizeof(int));
356 params
->freqs
[0] = wpa_s
->go_params
->freq
;
357 } else if (wpa_s
->p2p_in_provisioning
< 8 &&
358 wpa_s
->go_params
->freq_list
[0]) {
359 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only common "
361 int_array_concat(¶ms
->freqs
,
362 wpa_s
->go_params
->freq_list
);
364 int_array_sort_unique(params
->freqs
);
366 wpa_s
->p2p_in_provisioning
++;
369 if (params
->freqs
== NULL
&& wpa_s
->p2p_in_invitation
) {
371 * Optimize scan based on GO information during persistent
374 if (wpa_s
->p2p_in_invitation
< 5 &&
375 wpa_s
->p2p_invite_go_freq
> 0) {
376 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only GO preferred frequency %d MHz during invitation",
377 wpa_s
->p2p_invite_go_freq
);
378 params
->freqs
= os_calloc(2, sizeof(int));
380 params
->freqs
[0] = wpa_s
->p2p_invite_go_freq
;
382 wpa_s
->p2p_in_invitation
++;
383 if (wpa_s
->p2p_in_invitation
> 20) {
385 * This should not really happen since the variable is
386 * cleared on group removal, but if it does happen, make
387 * sure we do not get stuck in special invitation scan
390 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Clear p2p_in_invitation");
391 wpa_s
->p2p_in_invitation
= 0;
394 #endif /* CONFIG_P2P */
397 if (params
->freqs
== NULL
&& wpa_s
->after_wps
&& wpa_s
->wps_freq
) {
399 * Optimize post-provisioning scan based on channel used
400 * during provisioning.
402 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Scan only frequency %u MHz "
403 "that was used during provisioning", wpa_s
->wps_freq
);
404 params
->freqs
= os_calloc(2, sizeof(int));
406 params
->freqs
[0] = wpa_s
->wps_freq
;
408 } else if (wpa_s
->after_wps
)
411 if (params
->freqs
== NULL
&& wpa_s
->known_wps_freq
&& wpa_s
->wps_freq
)
413 /* Optimize provisioning scan based on already known channel */
414 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Scan only frequency %u MHz",
416 params
->freqs
= os_calloc(2, sizeof(int));
418 params
->freqs
[0] = wpa_s
->wps_freq
;
419 wpa_s
->known_wps_freq
= 0; /* only do this once */
421 #endif /* CONFIG_WPS */
425 #ifdef CONFIG_INTERWORKING
426 static void wpas_add_interworking_elements(struct wpa_supplicant
*wpa_s
,
429 wpabuf_put_u8(buf
, WLAN_EID_INTERWORKING
);
430 wpabuf_put_u8(buf
, is_zero_ether_addr(wpa_s
->conf
->hessid
) ? 1 :
432 wpabuf_put_u8(buf
, wpa_s
->conf
->access_network_type
);
434 if (!is_zero_ether_addr(wpa_s
->conf
->hessid
))
435 wpabuf_put_data(buf
, wpa_s
->conf
->hessid
, ETH_ALEN
);
437 #endif /* CONFIG_INTERWORKING */
440 void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant
*wpa_s
)
442 struct wpabuf
*default_ies
= NULL
;
445 enum wpa_driver_if_type type
= WPA_IF_STATION
;
448 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
)
449 type
= WPA_IF_P2P_CLIENT
;
450 #endif /* CONFIG_P2P */
452 wpa_drv_get_ext_capa(wpa_s
, type
);
454 ext_capab_len
= wpas_build_ext_capab(wpa_s
, ext_capab
,
456 if (ext_capab_len
> 0 &&
457 wpabuf_resize(&default_ies
, ext_capab_len
) == 0)
458 wpabuf_put_data(default_ies
, ext_capab
, ext_capab_len
);
461 /* Send cellular capabilities for potential MBO STAs */
462 if (wpabuf_resize(&default_ies
, 9) == 0)
463 wpas_mbo_scan_ie(wpa_s
, default_ies
);
464 #endif /* CONFIG_MBO */
467 wpa_drv_set_default_scan_ies(wpa_s
, wpabuf_head(default_ies
),
468 wpabuf_len(default_ies
));
469 wpabuf_free(default_ies
);
473 static struct wpabuf
* wpa_supplicant_extra_ies(struct wpa_supplicant
*wpa_s
)
475 struct wpabuf
*extra_ie
= NULL
;
480 enum wps_request_type req_type
= WPS_REQ_ENROLLEE_INFO
;
481 #endif /* CONFIG_WPS */
484 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
)
485 wpa_drv_get_ext_capa(wpa_s
, WPA_IF_P2P_CLIENT
);
487 #endif /* CONFIG_P2P */
488 wpa_drv_get_ext_capa(wpa_s
, WPA_IF_STATION
);
490 ext_capab_len
= wpas_build_ext_capab(wpa_s
, ext_capab
,
492 if (ext_capab_len
> 0 &&
493 wpabuf_resize(&extra_ie
, ext_capab_len
) == 0)
494 wpabuf_put_data(extra_ie
, ext_capab
, ext_capab_len
);
496 #ifdef CONFIG_INTERWORKING
497 if (wpa_s
->conf
->interworking
&&
498 wpabuf_resize(&extra_ie
, 100) == 0)
499 wpas_add_interworking_elements(wpa_s
, extra_ie
);
500 #endif /* CONFIG_INTERWORKING */
503 wps
= wpas_wps_in_use(wpa_s
, &req_type
);
506 struct wpabuf
*wps_ie
;
507 wps_ie
= wps_build_probe_req_ie(wps
== 2 ? DEV_PW_PUSHBUTTON
:
510 wpa_s
->wps
->uuid
, req_type
,
513 if (wpabuf_resize(&extra_ie
, wpabuf_len(wps_ie
)) == 0)
514 wpabuf_put_buf(extra_ie
, wps_ie
);
521 size_t ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
522 if (wpabuf_resize(&extra_ie
, ielen
) == 0)
523 wpas_p2p_scan_ie(wpa_s
, extra_ie
);
525 #endif /* CONFIG_P2P */
527 wpa_supplicant_mesh_add_scan_ie(wpa_s
, &extra_ie
);
529 #endif /* CONFIG_WPS */
532 if (wpa_s
->conf
->hs20
&& wpabuf_resize(&extra_ie
, 7) == 0)
533 wpas_hs20_add_indication(extra_ie
, -1);
534 #endif /* CONFIG_HS20 */
537 if (wpa_s
->fst_ies
&&
538 wpabuf_resize(&extra_ie
, wpabuf_len(wpa_s
->fst_ies
)) == 0)
539 wpabuf_put_buf(extra_ie
, wpa_s
->fst_ies
);
540 #endif /* CONFIG_FST */
543 /* Send cellular capabilities for potential MBO STAs */
544 if (wpabuf_resize(&extra_ie
, 9) == 0)
545 wpas_mbo_scan_ie(wpa_s
, extra_ie
);
546 #endif /* CONFIG_MBO */
548 if (wpa_s
->vendor_elem
[VENDOR_ELEM_PROBE_REQ
]) {
549 struct wpabuf
*buf
= wpa_s
->vendor_elem
[VENDOR_ELEM_PROBE_REQ
];
551 if (wpabuf_resize(&extra_ie
, wpabuf_len(buf
)) == 0)
552 wpabuf_put_buf(extra_ie
, buf
);
562 * Check whether there are any enabled networks or credentials that could be
563 * used for a non-P2P connection.
565 static int non_p2p_network_enabled(struct wpa_supplicant
*wpa_s
)
567 struct wpa_ssid
*ssid
;
569 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
570 if (wpas_network_disabled(wpa_s
, ssid
))
572 if (!ssid
->p2p_group
)
576 if (wpa_s
->conf
->cred
&& wpa_s
->conf
->interworking
&&
577 wpa_s
->conf
->auto_interworking
)
583 #endif /* CONFIG_P2P */
586 static void wpa_setband_scan_freqs_list(struct wpa_supplicant
*wpa_s
,
587 enum hostapd_hw_mode band
,
588 struct wpa_driver_scan_params
*params
)
590 /* Include only supported channels for the specified band */
591 struct hostapd_hw_modes
*mode
;
594 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, band
);
596 /* No channels supported in this band - use empty list */
597 params
->freqs
= os_zalloc(sizeof(int));
601 params
->freqs
= os_calloc(mode
->num_channels
+ 1, sizeof(int));
602 if (params
->freqs
== NULL
)
604 for (count
= 0, i
= 0; i
< mode
->num_channels
; i
++) {
605 if (mode
->channels
[i
].flag
& HOSTAPD_CHAN_DISABLED
)
607 params
->freqs
[count
++] = mode
->channels
[i
].freq
;
612 static void wpa_setband_scan_freqs(struct wpa_supplicant
*wpa_s
,
613 struct wpa_driver_scan_params
*params
)
615 if (wpa_s
->hw
.modes
== NULL
)
616 return; /* unknown what channels the driver supports */
618 return; /* already using a limited channel set */
619 if (wpa_s
->setband
== WPA_SETBAND_5G
)
620 wpa_setband_scan_freqs_list(wpa_s
, HOSTAPD_MODE_IEEE80211A
,
622 else if (wpa_s
->setband
== WPA_SETBAND_2G
)
623 wpa_setband_scan_freqs_list(wpa_s
, HOSTAPD_MODE_IEEE80211G
,
628 static void wpa_set_scan_ssids(struct wpa_supplicant
*wpa_s
,
629 struct wpa_driver_scan_params
*params
,
633 struct wpa_ssid
*ssid
;
636 * For devices with max_ssids greater than 1, leave the last slot empty
637 * for adding the wildcard scan entry.
639 max_ssids
= max_ssids
> 1 ? max_ssids
- 1 : max_ssids
;
641 for (i
= 0; i
< wpa_s
->scan_id_count
; i
++) {
644 ssid
= wpa_config_get_network(wpa_s
->conf
, wpa_s
->scan_id
[i
]);
645 if (!ssid
|| !ssid
->scan_ssid
)
648 for (j
= 0; j
< params
->num_ssids
; j
++) {
649 if (params
->ssids
[j
].ssid_len
== ssid
->ssid_len
&&
650 params
->ssids
[j
].ssid
&&
651 os_memcmp(params
->ssids
[j
].ssid
, ssid
->ssid
,
652 ssid
->ssid_len
) == 0)
655 if (j
< params
->num_ssids
)
656 continue; /* already in the list */
658 if (params
->num_ssids
+ 1 > max_ssids
) {
659 wpa_printf(MSG_DEBUG
,
660 "Over max scan SSIDs for manual request");
664 wpa_printf(MSG_DEBUG
, "Scan SSID (manual request): %s",
665 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
666 params
->ssids
[params
->num_ssids
].ssid
= ssid
->ssid
;
667 params
->ssids
[params
->num_ssids
].ssid_len
= ssid
->ssid_len
;
671 wpa_s
->scan_id_count
= 0;
675 static int wpa_set_ssids_from_scan_req(struct wpa_supplicant
*wpa_s
,
676 struct wpa_driver_scan_params
*params
,
681 if (wpa_s
->ssids_from_scan_req
== NULL
||
682 wpa_s
->num_ssids_from_scan_req
== 0)
685 if (wpa_s
->num_ssids_from_scan_req
> max_ssids
) {
686 wpa_s
->num_ssids_from_scan_req
= max_ssids
;
687 wpa_printf(MSG_DEBUG
, "Over max scan SSIDs from scan req: %u",
688 (unsigned int) max_ssids
);
691 for (i
= 0; i
< wpa_s
->num_ssids_from_scan_req
; i
++) {
692 params
->ssids
[i
].ssid
= wpa_s
->ssids_from_scan_req
[i
].ssid
;
693 params
->ssids
[i
].ssid_len
=
694 wpa_s
->ssids_from_scan_req
[i
].ssid_len
;
695 wpa_hexdump_ascii(MSG_DEBUG
, "specific SSID",
696 params
->ssids
[i
].ssid
,
697 params
->ssids
[i
].ssid_len
);
700 params
->num_ssids
= wpa_s
->num_ssids_from_scan_req
;
701 wpa_s
->num_ssids_from_scan_req
= 0;
706 static void wpa_supplicant_scan(void *eloop_ctx
, void *timeout_ctx
)
708 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
709 struct wpa_ssid
*ssid
;
710 int ret
, p2p_in_prog
;
711 struct wpabuf
*extra_ie
= NULL
;
712 struct wpa_driver_scan_params params
;
713 struct wpa_driver_scan_params
*scan_params
;
715 int connect_without_scan
= 0;
717 wpa_s
->ignore_post_flush_scan_res
= 0;
719 if (wpa_s
->wpa_state
== WPA_INTERFACE_DISABLED
) {
720 wpa_dbg(wpa_s
, MSG_DEBUG
, "Skip scan - interface disabled");
724 if (wpa_s
->disconnected
&& wpa_s
->scan_req
== NORMAL_SCAN_REQ
) {
725 wpa_dbg(wpa_s
, MSG_DEBUG
, "Disconnected - do not scan");
726 wpa_supplicant_set_state(wpa_s
, WPA_DISCONNECTED
);
730 if (wpa_s
->scanning
) {
732 * If we are already in scanning state, we shall reschedule the
733 * the incoming scan request.
735 wpa_dbg(wpa_s
, MSG_DEBUG
, "Already scanning - Reschedule the incoming scan req");
736 wpa_supplicant_req_scan(wpa_s
, 1, 0);
740 if (!wpa_supplicant_enabled_networks(wpa_s
) &&
741 wpa_s
->scan_req
== NORMAL_SCAN_REQ
) {
742 wpa_dbg(wpa_s
, MSG_DEBUG
, "No enabled networks - do not scan");
743 wpa_supplicant_set_state(wpa_s
, WPA_INACTIVE
);
747 if (wpa_s
->conf
->ap_scan
!= 0 &&
748 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_WIRED
)) {
749 wpa_dbg(wpa_s
, MSG_DEBUG
, "Using wired authentication - "
750 "overriding ap_scan configuration");
751 wpa_s
->conf
->ap_scan
= 0;
752 wpas_notify_ap_scan_changed(wpa_s
);
755 if (wpa_s
->conf
->ap_scan
== 0) {
756 wpa_supplicant_gen_assoc_event(wpa_s
);
761 if (wpa_s
->scan_req
!= MANUAL_SCAN_REQ
&&
762 wpa_s
->connect_without_scan
) {
763 connect_without_scan
= 1;
764 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
765 if (ssid
== wpa_s
->connect_without_scan
)
770 p2p_in_prog
= wpas_p2p_in_progress(wpa_s
);
771 if (p2p_in_prog
&& p2p_in_prog
!= 2 &&
773 (ssid
->mode
!= WPAS_MODE_AP
&& ssid
->mode
!= WPAS_MODE_P2P_GO
))) {
774 wpa_dbg(wpa_s
, MSG_DEBUG
, "Delay station mode scan while P2P operation is in progress");
775 wpa_supplicant_req_scan(wpa_s
, 5, 0);
780 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
781 * used for changing modes inside wpa_supplicant (roaming,
782 * auto-reconnect, etc). Discarding the scan might hurt these processes.
783 * The normal use case for PNO is to suspend the host immediately after
784 * starting PNO, so the periodic 100 ms attempts to run the scan do not
785 * normally happen in practice multiple times, i.e., this is simply
786 * restarting scanning once the host is woken up and PNO stopped.
788 if (wpa_s
->pno
|| wpa_s
->pno_sched_pending
) {
789 wpa_dbg(wpa_s
, MSG_DEBUG
, "Defer scan - PNO is in progress");
790 wpa_supplicant_req_scan(wpa_s
, 0, 100000);
794 if (wpa_s
->conf
->ap_scan
== 2)
797 max_ssids
= wpa_s
->max_scan_ssids
;
798 if (max_ssids
> WPAS_MAX_SCAN_SSIDS
)
799 max_ssids
= WPAS_MAX_SCAN_SSIDS
;
802 wpa_s
->last_scan_req
= wpa_s
->scan_req
;
803 wpa_s
->scan_req
= NORMAL_SCAN_REQ
;
805 if (connect_without_scan
) {
806 wpa_s
->connect_without_scan
= NULL
;
808 wpa_printf(MSG_DEBUG
, "Start a pre-selected network "
809 "without scan step");
810 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
815 os_memset(¶ms
, 0, sizeof(params
));
817 wpa_s
->scan_prev_wpa_state
= wpa_s
->wpa_state
;
818 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
819 wpa_s
->wpa_state
== WPA_INACTIVE
)
820 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
823 * If autoscan has set its own scanning parameters
825 if (wpa_s
->autoscan_params
!= NULL
) {
826 scan_params
= wpa_s
->autoscan_params
;
830 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
831 wpa_set_ssids_from_scan_req(wpa_s
, ¶ms
, max_ssids
)) {
832 wpa_printf(MSG_DEBUG
, "Use specific SSIDs from SCAN command");
837 if ((wpa_s
->p2p_in_provisioning
|| wpa_s
->show_group_started
) &&
838 wpa_s
->go_params
&& !wpa_s
->conf
->passive_scan
) {
839 wpa_printf(MSG_DEBUG
, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
840 wpa_s
->p2p_in_provisioning
,
841 wpa_s
->show_group_started
);
842 params
.ssids
[0].ssid
= wpa_s
->go_params
->ssid
;
843 params
.ssids
[0].ssid_len
= wpa_s
->go_params
->ssid_len
;
844 params
.num_ssids
= 1;
848 if (wpa_s
->p2p_in_invitation
) {
849 if (wpa_s
->current_ssid
) {
850 wpa_printf(MSG_DEBUG
, "P2P: Use specific SSID for scan during invitation");
851 params
.ssids
[0].ssid
= wpa_s
->current_ssid
->ssid
;
852 params
.ssids
[0].ssid_len
=
853 wpa_s
->current_ssid
->ssid_len
;
854 params
.num_ssids
= 1;
856 wpa_printf(MSG_DEBUG
, "P2P: No specific SSID known for scan during invitation");
860 #endif /* CONFIG_P2P */
862 /* Find the starting point from which to continue scanning */
863 ssid
= wpa_s
->conf
->ssid
;
864 if (wpa_s
->prev_scan_ssid
!= WILDCARD_SSID_SCAN
) {
866 if (ssid
== wpa_s
->prev_scan_ssid
) {
874 if (wpa_s
->last_scan_req
!= MANUAL_SCAN_REQ
&&
877 #endif /* CONFIG_AP */
878 wpa_s
->conf
->ap_scan
== 2) {
879 wpa_s
->connect_without_scan
= NULL
;
880 wpa_s
->prev_scan_wildcard
= 0;
881 wpa_supplicant_assoc_try(wpa_s
, ssid
);
883 } else if (wpa_s
->conf
->ap_scan
== 2) {
885 * User-initiated scan request in ap_scan == 2; scan with
889 } else if (wpa_s
->reattach
&& wpa_s
->current_ssid
!= NULL
) {
891 * Perform single-channel single-SSID scan for
892 * reassociate-to-same-BSS operation.
895 ssid
= wpa_s
->current_ssid
;
896 wpa_hexdump_ascii(MSG_DEBUG
, "Scan SSID",
897 ssid
->ssid
, ssid
->ssid_len
);
898 params
.ssids
[0].ssid
= ssid
->ssid
;
899 params
.ssids
[0].ssid_len
= ssid
->ssid_len
;
900 params
.num_ssids
= 1;
903 * Allocate memory for frequency array, allocate one extra
904 * slot for the zero-terminator.
906 params
.freqs
= os_malloc(sizeof(int) * 2);
908 params
.freqs
[0] = wpa_s
->assoc_freq
;
913 * Reset the reattach flag so that we fall back to full scan if
918 struct wpa_ssid
*start
= ssid
, *tssid
;
920 if (ssid
== NULL
&& max_ssids
> 1)
921 ssid
= wpa_s
->conf
->ssid
;
923 if (!wpas_network_disabled(wpa_s
, ssid
) &&
925 wpa_hexdump_ascii(MSG_DEBUG
, "Scan SSID",
926 ssid
->ssid
, ssid
->ssid_len
);
927 params
.ssids
[params
.num_ssids
].ssid
=
929 params
.ssids
[params
.num_ssids
].ssid_len
=
932 if (params
.num_ssids
+ 1 >= max_ssids
)
938 if (ssid
== NULL
&& max_ssids
> 1 &&
939 start
!= wpa_s
->conf
->ssid
)
940 ssid
= wpa_s
->conf
->ssid
;
943 if (wpa_s
->scan_id_count
&&
944 wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
)
945 wpa_set_scan_ssids(wpa_s
, ¶ms
, max_ssids
);
947 for (tssid
= wpa_s
->conf
->ssid
;
948 wpa_s
->last_scan_req
!= MANUAL_SCAN_REQ
&& tssid
;
949 tssid
= tssid
->next
) {
950 if (wpas_network_disabled(wpa_s
, tssid
))
952 if ((params
.freqs
|| !freqs_set
) && tssid
->scan_freq
) {
953 int_array_concat(¶ms
.freqs
,
956 os_free(params
.freqs
);
961 int_array_sort_unique(params
.freqs
);
964 if (ssid
&& max_ssids
== 1) {
966 * If the driver is limited to 1 SSID at a time interleave
967 * wildcard SSID scans with specific SSID scans to avoid
968 * waiting a long time for a wildcard scan.
970 if (!wpa_s
->prev_scan_wildcard
) {
971 params
.ssids
[0].ssid
= NULL
;
972 params
.ssids
[0].ssid_len
= 0;
973 wpa_s
->prev_scan_wildcard
= 1;
974 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for "
975 "wildcard SSID (Interleave with specific)");
977 wpa_s
->prev_scan_ssid
= ssid
;
978 wpa_s
->prev_scan_wildcard
= 0;
979 wpa_dbg(wpa_s
, MSG_DEBUG
,
980 "Starting AP scan for specific SSID: %s",
981 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
986 wpa_s
->prev_scan_ssid
= ssid
;
987 wpa_dbg(wpa_s
, MSG_DEBUG
, "Include wildcard SSID in "
990 } else if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
991 wpa_s
->manual_scan_passive
&& params
.num_ssids
== 0) {
992 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use passive scan based on manual request");
993 } else if (wpa_s
->conf
->passive_scan
) {
994 wpa_dbg(wpa_s
, MSG_DEBUG
,
995 "Use passive scan based on configuration");
997 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
999 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for wildcard "
1004 wpa_supplicant_optimize_freqs(wpa_s
, ¶ms
);
1005 extra_ie
= wpa_supplicant_extra_ies(wpa_s
);
1007 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
1008 wpa_s
->manual_scan_only_new
) {
1009 wpa_printf(MSG_DEBUG
,
1010 "Request driver to clear scan cache due to manual only_new=1 scan");
1011 params
.only_new_results
= 1;
1014 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&& params
.freqs
== NULL
&&
1015 wpa_s
->manual_scan_freqs
) {
1016 wpa_dbg(wpa_s
, MSG_DEBUG
, "Limit manual scan to specified channels");
1017 params
.freqs
= wpa_s
->manual_scan_freqs
;
1018 wpa_s
->manual_scan_freqs
= NULL
;
1021 if (params
.freqs
== NULL
&& wpa_s
->select_network_scan_freqs
) {
1022 wpa_dbg(wpa_s
, MSG_DEBUG
,
1023 "Limit select_network scan to specified channels");
1024 params
.freqs
= wpa_s
->select_network_scan_freqs
;
1025 wpa_s
->select_network_scan_freqs
= NULL
;
1028 if (params
.freqs
== NULL
&& wpa_s
->next_scan_freqs
) {
1029 wpa_dbg(wpa_s
, MSG_DEBUG
, "Optimize scan based on previously "
1030 "generated frequency list");
1031 params
.freqs
= wpa_s
->next_scan_freqs
;
1033 os_free(wpa_s
->next_scan_freqs
);
1034 wpa_s
->next_scan_freqs
= NULL
;
1035 wpa_setband_scan_freqs(wpa_s
, ¶ms
);
1037 /* See if user specified frequencies. If so, scan only those. */
1038 if (wpa_s
->conf
->freq_list
&& !params
.freqs
) {
1039 wpa_dbg(wpa_s
, MSG_DEBUG
,
1040 "Optimize scan based on conf->freq_list");
1041 int_array_concat(¶ms
.freqs
, wpa_s
->conf
->freq_list
);
1044 /* Use current associated channel? */
1045 if (wpa_s
->conf
->scan_cur_freq
&& !params
.freqs
) {
1046 unsigned int num
= wpa_s
->num_multichan_concurrent
;
1048 params
.freqs
= os_calloc(num
+ 1, sizeof(int));
1050 num
= get_shared_radio_freqs(wpa_s
, params
.freqs
, num
);
1052 wpa_dbg(wpa_s
, MSG_DEBUG
, "Scan only the "
1053 "current operating channels since "
1054 "scan_cur_freq is enabled");
1056 os_free(params
.freqs
);
1057 params
.freqs
= NULL
;
1062 params
.filter_ssids
= wpa_supplicant_build_filter_ssids(
1063 wpa_s
->conf
, ¶ms
.num_filter_ssids
);
1065 params
.extra_ies
= wpabuf_head(extra_ie
);
1066 params
.extra_ies_len
= wpabuf_len(extra_ie
);
1070 if (wpa_s
->p2p_in_provisioning
|| wpa_s
->p2p_in_invitation
||
1071 (wpa_s
->show_group_started
&& wpa_s
->go_params
)) {
1073 * The interface may not yet be in P2P mode, so we have to
1074 * explicitly request P2P probe to disable CCK rates.
1076 params
.p2p_probe
= 1;
1078 #endif /* CONFIG_P2P */
1080 if ((wpa_s
->mac_addr_rand_enable
& MAC_ADDR_RAND_SCAN
) &&
1081 wpa_s
->wpa_state
<= WPA_SCANNING
) {
1082 params
.mac_addr_rand
= 1;
1083 if (wpa_s
->mac_addr_scan
) {
1084 params
.mac_addr
= wpa_s
->mac_addr_scan
;
1085 params
.mac_addr_mask
= wpa_s
->mac_addr_scan
+ ETH_ALEN
;
1089 if (!is_zero_ether_addr(wpa_s
->next_scan_bssid
)) {
1090 struct wpa_bss
*bss
;
1092 params
.bssid
= wpa_s
->next_scan_bssid
;
1093 bss
= wpa_bss_get_bssid_latest(wpa_s
, params
.bssid
);
1094 if (bss
&& bss
->ssid_len
&& params
.num_ssids
== 1 &&
1095 params
.ssids
[0].ssid_len
== 0) {
1096 params
.ssids
[0].ssid
= bss
->ssid
;
1097 params
.ssids
[0].ssid_len
= bss
->ssid_len
;
1098 wpa_dbg(wpa_s
, MSG_DEBUG
,
1099 "Scan a previously specified BSSID " MACSTR
1101 MAC2STR(params
.bssid
),
1102 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
1104 wpa_dbg(wpa_s
, MSG_DEBUG
,
1105 "Scan a previously specified BSSID " MACSTR
,
1106 MAC2STR(params
.bssid
));
1110 scan_params
= ¶ms
;
1115 * If the driver does not support multi-channel concurrency and a
1116 * virtual interface that shares the same radio with the wpa_s interface
1117 * is operating there may not be need to scan other channels apart from
1118 * the current operating channel on the other virtual interface. Filter
1119 * out other channels in case we are trying to find a connection for a
1120 * station interface when we are not configured to prefer station
1121 * connection and a concurrent operation is already in process.
1123 if (wpa_s
->scan_for_connection
&&
1124 wpa_s
->last_scan_req
== NORMAL_SCAN_REQ
&&
1125 !scan_params
->freqs
&& !params
.freqs
&&
1126 wpas_is_p2p_prioritized(wpa_s
) &&
1127 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
&&
1128 non_p2p_network_enabled(wpa_s
)) {
1129 unsigned int num
= wpa_s
->num_multichan_concurrent
;
1131 params
.freqs
= os_calloc(num
+ 1, sizeof(int));
1133 num
= get_shared_radio_freqs(wpa_s
, params
.freqs
, num
);
1134 if (num
> 0 && num
== wpa_s
->num_multichan_concurrent
) {
1135 wpa_dbg(wpa_s
, MSG_DEBUG
, "Scan only the current operating channels since all channels are already used");
1137 os_free(params
.freqs
);
1138 params
.freqs
= NULL
;
1142 #endif /* CONFIG_P2P */
1144 ret
= wpa_supplicant_trigger_scan(wpa_s
, scan_params
);
1146 if (ret
&& wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&& params
.freqs
&&
1147 !wpa_s
->manual_scan_freqs
) {
1148 /* Restore manual_scan_freqs for the next attempt */
1149 wpa_s
->manual_scan_freqs
= params
.freqs
;
1150 params
.freqs
= NULL
;
1153 wpabuf_free(extra_ie
);
1154 os_free(params
.freqs
);
1155 os_free(params
.filter_ssids
);
1158 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate AP scan");
1159 if (wpa_s
->scan_prev_wpa_state
!= wpa_s
->wpa_state
)
1160 wpa_supplicant_set_state(wpa_s
,
1161 wpa_s
->scan_prev_wpa_state
);
1162 /* Restore scan_req since we will try to scan again */
1163 wpa_s
->scan_req
= wpa_s
->last_scan_req
;
1164 wpa_supplicant_req_scan(wpa_s
, 1, 0);
1166 wpa_s
->scan_for_connection
= 0;
1167 #ifdef CONFIG_INTERWORKING
1168 wpa_s
->interworking_fast_assoc_tried
= 0;
1169 #endif /* CONFIG_INTERWORKING */
1171 os_memset(wpa_s
->next_scan_bssid
, 0, ETH_ALEN
);
1176 void wpa_supplicant_update_scan_int(struct wpa_supplicant
*wpa_s
, int sec
)
1178 struct os_reltime remaining
, new_int
;
1181 cancelled
= eloop_cancel_timeout_one(wpa_supplicant_scan
, wpa_s
, NULL
,
1186 if (cancelled
&& os_reltime_before(&remaining
, &new_int
)) {
1187 new_int
.sec
= remaining
.sec
;
1188 new_int
.usec
= remaining
.usec
;
1192 eloop_register_timeout(new_int
.sec
, new_int
.usec
,
1193 wpa_supplicant_scan
, wpa_s
, NULL
);
1195 wpa_s
->scan_interval
= sec
;
1200 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1201 * @wpa_s: Pointer to wpa_supplicant data
1202 * @sec: Number of seconds after which to scan
1203 * @usec: Number of microseconds after which to scan
1205 * This function is used to schedule a scan for neighboring access points after
1206 * the specified time.
1208 void wpa_supplicant_req_scan(struct wpa_supplicant
*wpa_s
, int sec
, int usec
)
1212 if (wpa_s
->p2p_mgmt
) {
1213 wpa_dbg(wpa_s
, MSG_DEBUG
,
1214 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1219 res
= eloop_deplete_timeout(sec
, usec
, wpa_supplicant_scan
, wpa_s
,
1222 wpa_dbg(wpa_s
, MSG_DEBUG
, "Rescheduling scan request: %d.%06d sec",
1224 } else if (res
== 0) {
1225 wpa_dbg(wpa_s
, MSG_DEBUG
, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1228 wpa_dbg(wpa_s
, MSG_DEBUG
, "Setting scan request: %d.%06d sec",
1230 eloop_register_timeout(sec
, usec
, wpa_supplicant_scan
, wpa_s
, NULL
);
1236 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1237 * @wpa_s: Pointer to wpa_supplicant data
1238 * @sec: Number of seconds after which to scan
1239 * @usec: Number of microseconds after which to scan
1240 * Returns: 0 on success or -1 otherwise
1242 * This function is used to schedule periodic scans for neighboring
1243 * access points after the specified time.
1245 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant
*wpa_s
,
1248 if (!wpa_s
->sched_scan_supported
)
1251 eloop_register_timeout(sec
, usec
,
1252 wpa_supplicant_delayed_sched_scan_timeout
,
1260 wpa_scan_set_relative_rssi_params(struct wpa_supplicant
*wpa_s
,
1261 struct wpa_driver_scan_params
*params
)
1263 if (wpa_s
->wpa_state
!= WPA_COMPLETED
||
1264 !(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI
) ||
1265 wpa_s
->srp
.relative_rssi_set
== 0)
1268 params
->relative_rssi_set
= 1;
1269 params
->relative_rssi
= wpa_s
->srp
.relative_rssi
;
1271 if (wpa_s
->srp
.relative_adjust_rssi
== 0)
1274 params
->relative_adjust_band
= wpa_s
->srp
.relative_adjust_band
;
1275 params
->relative_adjust_rssi
= wpa_s
->srp
.relative_adjust_rssi
;
1280 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1281 * @wpa_s: Pointer to wpa_supplicant data
1282 * Returns: 0 is sched_scan was started or -1 otherwise
1284 * This function is used to schedule periodic scans for neighboring
1285 * access points repeating the scan continuously.
1287 int wpa_supplicant_req_sched_scan(struct wpa_supplicant
*wpa_s
)
1289 struct wpa_driver_scan_params params
;
1290 struct wpa_driver_scan_params
*scan_params
;
1291 enum wpa_states prev_state
;
1292 struct wpa_ssid
*ssid
= NULL
;
1293 struct wpabuf
*extra_ie
= NULL
;
1295 unsigned int max_sched_scan_ssids
;
1298 struct sched_scan_plan scan_plan
;
1300 if (!wpa_s
->sched_scan_supported
)
1303 if (wpa_s
->max_sched_scan_ssids
> WPAS_MAX_SCAN_SSIDS
)
1304 max_sched_scan_ssids
= WPAS_MAX_SCAN_SSIDS
;
1306 max_sched_scan_ssids
= wpa_s
->max_sched_scan_ssids
;
1307 if (max_sched_scan_ssids
< 1 || wpa_s
->conf
->disable_scan_offload
)
1310 wpa_s
->sched_scan_stop_req
= 0;
1312 if (wpa_s
->sched_scanning
) {
1313 wpa_dbg(wpa_s
, MSG_DEBUG
, "Already sched scanning");
1318 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
1319 if (!wpas_network_disabled(wpa_s
, ssid
) && !ssid
->scan_ssid
) {
1320 /* Use wildcard SSID to find this network */
1322 } else if (!wpas_network_disabled(wpa_s
, ssid
) &&
1327 if (!wpas_network_disabled(wpa_s
, ssid
) &&
1328 ssid
->key_mgmt
== WPA_KEY_MGMT_WPS
) {
1330 * Normal scan is more reliable and faster for WPS
1331 * operations and since these are for short periods of
1332 * time, the benefit of trying to use sched_scan would
1335 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
1336 "sched_scan for WPS");
1339 #endif /* CONFIG_WPS */
1344 if (wpa_s
->normal_scans
< 3 &&
1345 (need_ssids
<= wpa_s
->max_scan_ssids
||
1346 wpa_s
->max_scan_ssids
>= (int) max_sched_scan_ssids
)) {
1348 * When normal scan can speed up operations, use that for the
1349 * first operations before starting the sched_scan to allow
1350 * user space sleep more. We do this only if the normal scan
1351 * has functionality that is suitable for this or if the
1352 * sched_scan does not have better support for multiple SSIDs.
1354 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
1355 "sched_scan for initial scans (normal_scans=%d)",
1356 wpa_s
->normal_scans
);
1360 os_memset(¶ms
, 0, sizeof(params
));
1362 /* If we can't allocate space for the filters, we just don't filter */
1363 params
.filter_ssids
= os_calloc(wpa_s
->max_match_sets
,
1364 sizeof(struct wpa_driver_scan_filter
));
1366 prev_state
= wpa_s
->wpa_state
;
1367 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
1368 wpa_s
->wpa_state
== WPA_INACTIVE
)
1369 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
1371 if (wpa_s
->autoscan_params
!= NULL
) {
1372 scan_params
= wpa_s
->autoscan_params
;
1376 /* Find the starting point from which to continue scanning */
1377 ssid
= wpa_s
->conf
->ssid
;
1378 if (wpa_s
->prev_sched_ssid
) {
1380 if (ssid
== wpa_s
->prev_sched_ssid
) {
1388 if (!ssid
|| !wpa_s
->prev_sched_ssid
) {
1389 wpa_dbg(wpa_s
, MSG_DEBUG
, "Beginning of SSID list");
1390 wpa_s
->sched_scan_timeout
= max_sched_scan_ssids
* 2;
1391 wpa_s
->first_sched_scan
= 1;
1392 ssid
= wpa_s
->conf
->ssid
;
1393 wpa_s
->prev_sched_ssid
= ssid
;
1397 wpa_dbg(wpa_s
, MSG_DEBUG
, "Add wildcard SSID to sched_scan");
1402 if (wpas_network_disabled(wpa_s
, ssid
))
1405 if (params
.num_filter_ssids
< wpa_s
->max_match_sets
&&
1406 params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
) {
1407 wpa_dbg(wpa_s
, MSG_DEBUG
, "add to filter ssid: %s",
1408 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
1409 os_memcpy(params
.filter_ssids
[params
.num_filter_ssids
].ssid
,
1410 ssid
->ssid
, ssid
->ssid_len
);
1411 params
.filter_ssids
[params
.num_filter_ssids
].ssid_len
=
1413 params
.num_filter_ssids
++;
1414 } else if (params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
)
1416 wpa_dbg(wpa_s
, MSG_DEBUG
, "Not enough room for SSID "
1417 "filter for sched_scan - drop filter");
1418 os_free(params
.filter_ssids
);
1419 params
.filter_ssids
= NULL
;
1420 params
.num_filter_ssids
= 0;
1423 if (ssid
->scan_ssid
&& ssid
->ssid
&& ssid
->ssid_len
) {
1424 if (params
.num_ssids
== max_sched_scan_ssids
)
1425 break; /* only room for broadcast SSID */
1426 wpa_dbg(wpa_s
, MSG_DEBUG
,
1427 "add to active scan ssid: %s",
1428 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
1429 params
.ssids
[params
.num_ssids
].ssid
=
1431 params
.ssids
[params
.num_ssids
].ssid_len
=
1434 if (params
.num_ssids
>= max_sched_scan_ssids
) {
1435 wpa_s
->prev_sched_ssid
= ssid
;
1439 (wpas_network_disabled(wpa_s
, ssid
) ||
1446 wpa_s
->prev_sched_ssid
= ssid
;
1450 if (params
.num_filter_ssids
== 0) {
1451 os_free(params
.filter_ssids
);
1452 params
.filter_ssids
= NULL
;
1455 extra_ie
= wpa_supplicant_extra_ies(wpa_s
);
1457 params
.extra_ies
= wpabuf_head(extra_ie
);
1458 params
.extra_ies_len
= wpabuf_len(extra_ie
);
1461 if (wpa_s
->conf
->filter_rssi
)
1462 params
.filter_rssi
= wpa_s
->conf
->filter_rssi
;
1464 /* See if user specified frequencies. If so, scan only those. */
1465 if (wpa_s
->conf
->freq_list
&& !params
.freqs
) {
1466 wpa_dbg(wpa_s
, MSG_DEBUG
,
1467 "Optimize scan based on conf->freq_list");
1468 int_array_concat(¶ms
.freqs
, wpa_s
->conf
->freq_list
);
1471 scan_params
= ¶ms
;
1474 wpa_s
->sched_scan_timed_out
= 0;
1477 * We cannot support multiple scan plans if the scan request includes
1478 * too many SSID's, so in this case use only the last scan plan and make
1479 * it run infinitely. It will be stopped by the timeout.
1481 if (wpa_s
->sched_scan_plans_num
== 1 ||
1482 (wpa_s
->sched_scan_plans_num
&& !ssid
&& wpa_s
->first_sched_scan
)) {
1483 params
.sched_scan_plans
= wpa_s
->sched_scan_plans
;
1484 params
.sched_scan_plans_num
= wpa_s
->sched_scan_plans_num
;
1485 } else if (wpa_s
->sched_scan_plans_num
> 1) {
1486 wpa_dbg(wpa_s
, MSG_DEBUG
,
1487 "Too many SSIDs. Default to using single scheduled_scan plan");
1488 params
.sched_scan_plans
=
1489 &wpa_s
->sched_scan_plans
[wpa_s
->sched_scan_plans_num
-
1491 params
.sched_scan_plans_num
= 1;
1493 if (wpa_s
->conf
->sched_scan_interval
)
1494 scan_plan
.interval
= wpa_s
->conf
->sched_scan_interval
;
1496 scan_plan
.interval
= 10;
1498 if (scan_plan
.interval
> wpa_s
->max_sched_scan_plan_interval
) {
1499 wpa_printf(MSG_WARNING
,
1500 "Scan interval too long(%u), use the maximum allowed(%u)",
1502 wpa_s
->max_sched_scan_plan_interval
);
1503 scan_plan
.interval
=
1504 wpa_s
->max_sched_scan_plan_interval
;
1507 scan_plan
.iterations
= 0;
1508 params
.sched_scan_plans
= &scan_plan
;
1509 params
.sched_scan_plans_num
= 1;
1512 if (ssid
|| !wpa_s
->first_sched_scan
) {
1513 wpa_dbg(wpa_s
, MSG_DEBUG
,
1514 "Starting sched scan: interval %u timeout %d",
1515 params
.sched_scan_plans
[0].interval
,
1516 wpa_s
->sched_scan_timeout
);
1518 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting sched scan (no timeout)");
1521 wpa_setband_scan_freqs(wpa_s
, scan_params
);
1523 if ((wpa_s
->mac_addr_rand_enable
& MAC_ADDR_RAND_SCHED_SCAN
) &&
1524 wpa_s
->wpa_state
<= WPA_SCANNING
) {
1525 params
.mac_addr_rand
= 1;
1526 if (wpa_s
->mac_addr_sched_scan
) {
1527 params
.mac_addr
= wpa_s
->mac_addr_sched_scan
;
1528 params
.mac_addr_mask
= wpa_s
->mac_addr_sched_scan
+
1533 wpa_scan_set_relative_rssi_params(wpa_s
, scan_params
);
1535 ret
= wpa_supplicant_start_sched_scan(wpa_s
, scan_params
);
1536 wpabuf_free(extra_ie
);
1537 os_free(params
.filter_ssids
);
1539 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate sched scan");
1540 if (prev_state
!= wpa_s
->wpa_state
)
1541 wpa_supplicant_set_state(wpa_s
, prev_state
);
1545 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1546 if (ssid
|| !wpa_s
->first_sched_scan
) {
1547 wpa_s
->sched_scan_timed_out
= 0;
1548 eloop_register_timeout(wpa_s
->sched_scan_timeout
, 0,
1549 wpa_supplicant_sched_scan_timeout
,
1551 wpa_s
->first_sched_scan
= 0;
1552 wpa_s
->sched_scan_timeout
/= 2;
1553 params
.sched_scan_plans
[0].interval
*= 2;
1554 if ((unsigned int) wpa_s
->sched_scan_timeout
<
1555 params
.sched_scan_plans
[0].interval
||
1556 params
.sched_scan_plans
[0].interval
>
1557 wpa_s
->max_sched_scan_plan_interval
) {
1558 params
.sched_scan_plans
[0].interval
= 10;
1559 wpa_s
->sched_scan_timeout
= max_sched_scan_ssids
* 2;
1563 /* If there is no more ssids, start next time from the beginning */
1565 wpa_s
->prev_sched_ssid
= NULL
;
1572 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1573 * @wpa_s: Pointer to wpa_supplicant data
1575 * This function is used to cancel a scan request scheduled with
1576 * wpa_supplicant_req_scan().
1578 void wpa_supplicant_cancel_scan(struct wpa_supplicant
*wpa_s
)
1580 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling scan request");
1581 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
1586 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1587 * @wpa_s: Pointer to wpa_supplicant data
1589 * This function is used to stop a delayed scheduled scan.
1591 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant
*wpa_s
)
1593 if (!wpa_s
->sched_scan_supported
)
1596 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling delayed sched scan");
1597 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout
,
1603 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1604 * @wpa_s: Pointer to wpa_supplicant data
1606 * This function is used to stop a periodic scheduled scan.
1608 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant
*wpa_s
)
1610 if (!wpa_s
->sched_scanning
)
1613 if (wpa_s
->sched_scanning
)
1614 wpa_s
->sched_scan_stop_req
= 1;
1616 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling sched scan");
1617 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout
, wpa_s
, NULL
);
1618 wpa_supplicant_stop_sched_scan(wpa_s
);
1623 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1624 * @wpa_s: Pointer to wpa_supplicant data
1625 * @scanning: Whether scanning is currently in progress
1627 * This function is to generate scanning notifycations. It is called whenever
1628 * there may have been a change in scanning (scan started, completed, stopped).
1629 * wpas_notify_scanning() is called whenever the scanning state changed from the
1630 * previously notified state.
1632 void wpa_supplicant_notify_scanning(struct wpa_supplicant
*wpa_s
,
1635 if (wpa_s
->scanning
!= scanning
) {
1636 wpa_s
->scanning
= scanning
;
1637 wpas_notify_scanning(wpa_s
);
1642 static int wpa_scan_get_max_rate(const struct wpa_scan_res
*res
)
1648 ie
= wpa_scan_get_ie(res
, WLAN_EID_SUPP_RATES
);
1649 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1650 if ((ie
[i
+ 2] & 0x7f) > rate
)
1651 rate
= ie
[i
+ 2] & 0x7f;
1654 ie
= wpa_scan_get_ie(res
, WLAN_EID_EXT_SUPP_RATES
);
1655 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1656 if ((ie
[i
+ 2] & 0x7f) > rate
)
1657 rate
= ie
[i
+ 2] & 0x7f;
1665 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1666 * @res: Scan result entry
1667 * @ie: Information element identitifier (WLAN_EID_*)
1668 * Returns: Pointer to the information element (id field) or %NULL if not found
1670 * This function returns the first matching information element in the scan
1673 const u8
* wpa_scan_get_ie(const struct wpa_scan_res
*res
, u8 ie
)
1675 size_t ie_len
= res
->ie_len
;
1677 /* Use the Beacon frame IEs if res->ie_len is not available */
1679 ie_len
= res
->beacon_ie_len
;
1681 return get_ie((const u8
*) (res
+ 1), ie_len
, ie
);
1686 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1687 * @res: Scan result entry
1688 * @vendor_type: Vendor type (four octets starting the IE payload)
1689 * Returns: Pointer to the information element (id field) or %NULL if not found
1691 * This function returns the first matching information element in the scan
1694 const u8
* wpa_scan_get_vendor_ie(const struct wpa_scan_res
*res
,
1697 const u8
*end
, *pos
;
1699 pos
= (const u8
*) (res
+ 1);
1700 end
= pos
+ res
->ie_len
;
1702 while (end
- pos
> 1) {
1703 if (2 + pos
[1] > end
- pos
)
1705 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1706 vendor_type
== WPA_GET_BE32(&pos
[2]))
1716 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1717 * @res: Scan result entry
1718 * @vendor_type: Vendor type (four octets starting the IE payload)
1719 * Returns: Pointer to the information element (id field) or %NULL if not found
1721 * This function returns the first matching information element in the scan
1724 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1725 * from Beacon frames instead of either Beacon or Probe Response frames.
1727 const u8
* wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res
*res
,
1730 const u8
*end
, *pos
;
1732 if (res
->beacon_ie_len
== 0)
1735 pos
= (const u8
*) (res
+ 1);
1737 end
= pos
+ res
->beacon_ie_len
;
1739 while (end
- pos
> 1) {
1740 if (2 + pos
[1] > end
- pos
)
1742 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1743 vendor_type
== WPA_GET_BE32(&pos
[2]))
1753 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1754 * @res: Scan result entry
1755 * @vendor_type: Vendor type (four octets starting the IE payload)
1756 * Returns: Pointer to the information element payload or %NULL if not found
1758 * This function returns concatenated payload of possibly fragmented vendor
1759 * specific information elements in the scan result. The caller is responsible
1760 * for freeing the returned buffer.
1762 struct wpabuf
* wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res
*res
,
1766 const u8
*end
, *pos
;
1768 buf
= wpabuf_alloc(res
->ie_len
);
1772 pos
= (const u8
*) (res
+ 1);
1773 end
= pos
+ res
->ie_len
;
1775 while (end
- pos
> 1) {
1776 if (2 + pos
[1] > end
- pos
)
1778 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1779 vendor_type
== WPA_GET_BE32(&pos
[2]))
1780 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1784 if (wpabuf_len(buf
) == 0) {
1794 * Channels with a great SNR can operate at full rate. What is a great SNR?
1795 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1796 * rule of thumb is that any SNR above 20 is good." This one
1797 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1798 * recommends 25 as a minimum SNR for 54 Mbps data rate. The estimates used in
1799 * scan_est_throughput() allow even smaller SNR values for the maximum rates
1800 * (21 for 54 Mbps, 22 for VHT80 MCS9, 24 for HT40 and HT20 MCS7). Use 25 as a
1801 * somewhat conservative value here.
1803 #define GREAT_SNR 25
1805 #define IS_5GHZ(n) (n > 4000)
1807 /* Compare function for sorting scan results. Return >0 if @b is considered
1809 static int wpa_scan_result_compar(const void *a
, const void *b
)
1811 #define MIN(a,b) a < b ? a : b
1812 struct wpa_scan_res
**_wa
= (void *) a
;
1813 struct wpa_scan_res
**_wb
= (void *) b
;
1814 struct wpa_scan_res
*wa
= *_wa
;
1815 struct wpa_scan_res
*wb
= *_wb
;
1817 int snr_a
, snr_b
, snr_a_full
, snr_b_full
;
1819 /* WPA/WPA2 support preferred */
1820 wpa_a
= wpa_scan_get_vendor_ie(wa
, WPA_IE_VENDOR_TYPE
) != NULL
||
1821 wpa_scan_get_ie(wa
, WLAN_EID_RSN
) != NULL
;
1822 wpa_b
= wpa_scan_get_vendor_ie(wb
, WPA_IE_VENDOR_TYPE
) != NULL
||
1823 wpa_scan_get_ie(wb
, WLAN_EID_RSN
) != NULL
;
1825 if (wpa_b
&& !wpa_a
)
1827 if (!wpa_b
&& wpa_a
)
1830 /* privacy support preferred */
1831 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) == 0 &&
1832 (wb
->caps
& IEEE80211_CAP_PRIVACY
))
1834 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) &&
1835 (wb
->caps
& IEEE80211_CAP_PRIVACY
) == 0)
1838 if (wa
->flags
& wb
->flags
& WPA_SCAN_LEVEL_DBM
) {
1839 snr_a_full
= wa
->snr
;
1840 snr_a
= MIN(wa
->snr
, GREAT_SNR
);
1841 snr_b_full
= wb
->snr
;
1842 snr_b
= MIN(wb
->snr
, GREAT_SNR
);
1844 /* Level is not in dBm, so we can't calculate
1845 * SNR. Just use raw level (units unknown). */
1846 snr_a
= snr_a_full
= wa
->level
;
1847 snr_b
= snr_b_full
= wb
->level
;
1850 /* if SNR is close, decide by max rate or frequency band */
1851 if (snr_a
&& snr_b
&& abs(snr_b
- snr_a
) < 7) {
1852 if (wa
->est_throughput
!= wb
->est_throughput
)
1853 return wb
->est_throughput
- wa
->est_throughput
;
1855 if ((snr_a
&& snr_b
&& abs(snr_b
- snr_a
) < 5) ||
1856 (wa
->qual
&& wb
->qual
&& abs(wb
->qual
- wa
->qual
) < 10)) {
1857 if (IS_5GHZ(wa
->freq
) ^ IS_5GHZ(wb
->freq
))
1858 return IS_5GHZ(wa
->freq
) ? -1 : 1;
1861 /* all things being equal, use SNR; if SNRs are
1862 * identical, use quality values since some drivers may only report
1863 * that value and leave the signal level zero */
1864 if (snr_b_full
== snr_a_full
)
1865 return wb
->qual
- wa
->qual
;
1866 return snr_b_full
- snr_a_full
;
1872 /* Compare function for sorting scan results when searching a WPS AP for
1873 * provisioning. Return >0 if @b is considered better. */
1874 static int wpa_scan_result_wps_compar(const void *a
, const void *b
)
1876 struct wpa_scan_res
**_wa
= (void *) a
;
1877 struct wpa_scan_res
**_wb
= (void *) b
;
1878 struct wpa_scan_res
*wa
= *_wa
;
1879 struct wpa_scan_res
*wb
= *_wb
;
1880 int uses_wps_a
, uses_wps_b
;
1881 struct wpabuf
*wps_a
, *wps_b
;
1884 /* Optimization - check WPS IE existence before allocated memory and
1885 * doing full reassembly. */
1886 uses_wps_a
= wpa_scan_get_vendor_ie(wa
, WPS_IE_VENDOR_TYPE
) != NULL
;
1887 uses_wps_b
= wpa_scan_get_vendor_ie(wb
, WPS_IE_VENDOR_TYPE
) != NULL
;
1888 if (uses_wps_a
&& !uses_wps_b
)
1890 if (!uses_wps_a
&& uses_wps_b
)
1893 if (uses_wps_a
&& uses_wps_b
) {
1894 wps_a
= wpa_scan_get_vendor_ie_multi(wa
, WPS_IE_VENDOR_TYPE
);
1895 wps_b
= wpa_scan_get_vendor_ie_multi(wb
, WPS_IE_VENDOR_TYPE
);
1896 res
= wps_ap_priority_compar(wps_a
, wps_b
);
1904 * Do not use current AP security policy as a sorting criteria during
1905 * WPS provisioning step since the AP may get reconfigured at the
1906 * completion of provisioning.
1909 /* all things being equal, use signal level; if signal levels are
1910 * identical, use quality values since some drivers may only report
1911 * that value and leave the signal level zero */
1912 if (wb
->level
== wa
->level
)
1913 return wb
->qual
- wa
->qual
;
1914 return wb
->level
- wa
->level
;
1916 #endif /* CONFIG_WPS */
1919 static void dump_scan_res(struct wpa_scan_results
*scan_res
)
1921 #ifndef CONFIG_NO_STDOUT_DEBUG
1924 if (scan_res
->res
== NULL
|| scan_res
->num
== 0)
1927 wpa_printf(MSG_EXCESSIVE
, "Sorted scan results");
1929 for (i
= 0; i
< scan_res
->num
; i
++) {
1930 struct wpa_scan_res
*r
= scan_res
->res
[i
];
1932 if (r
->flags
& WPA_SCAN_LEVEL_DBM
) {
1933 int noise_valid
= !(r
->flags
& WPA_SCAN_NOISE_INVALID
);
1935 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1936 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
1937 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1938 r
->noise
, noise_valid
? "" : "~", r
->level
,
1939 r
->snr
, r
->snr
>= GREAT_SNR
? "*" : "",
1941 r
->age
, r
->est_throughput
);
1943 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1944 "noise=%d level=%d flags=0x%x age=%u est=%u",
1945 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1946 r
->noise
, r
->level
, r
->flags
, r
->age
,
1949 pos
= (u8
*) (r
+ 1);
1951 wpa_hexdump(MSG_EXCESSIVE
, "IEs", pos
, r
->ie_len
);
1953 if (r
->beacon_ie_len
)
1954 wpa_hexdump(MSG_EXCESSIVE
, "Beacon IEs",
1955 pos
, r
->beacon_ie_len
);
1957 #endif /* CONFIG_NO_STDOUT_DEBUG */
1962 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1963 * @wpa_s: Pointer to wpa_supplicant data
1964 * @bssid: BSSID to check
1965 * Returns: 0 if the BSSID is filtered or 1 if not
1967 * This function is used to filter out specific BSSIDs from scan reslts mainly
1968 * for testing purposes (SET bssid_filter ctrl_iface command).
1970 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant
*wpa_s
,
1975 if (wpa_s
->bssid_filter
== NULL
)
1978 for (i
= 0; i
< wpa_s
->bssid_filter_count
; i
++) {
1979 if (os_memcmp(wpa_s
->bssid_filter
+ i
* ETH_ALEN
, bssid
,
1988 void filter_scan_res(struct wpa_supplicant
*wpa_s
,
1989 struct wpa_scan_results
*res
)
1993 if (wpa_s
->bssid_filter
== NULL
)
1996 for (i
= 0, j
= 0; i
< res
->num
; i
++) {
1997 if (wpa_supplicant_filter_bssid_match(wpa_s
,
1998 res
->res
[i
]->bssid
)) {
1999 res
->res
[j
++] = res
->res
[i
];
2001 os_free(res
->res
[i
]);
2006 if (res
->num
!= j
) {
2007 wpa_printf(MSG_DEBUG
, "Filtered out %d scan results",
2008 (int) (res
->num
- j
));
2015 * Noise floor values to use when we have signal strength
2016 * measurements, but no noise floor measurements. These values were
2017 * measured in an office environment with many APs.
2019 #define DEFAULT_NOISE_FLOOR_2GHZ (-89)
2020 #define DEFAULT_NOISE_FLOOR_5GHZ (-92)
2022 void scan_snr(struct wpa_scan_res
*res
)
2024 if (res
->flags
& WPA_SCAN_NOISE_INVALID
) {
2025 res
->noise
= IS_5GHZ(res
->freq
) ?
2026 DEFAULT_NOISE_FLOOR_5GHZ
:
2027 DEFAULT_NOISE_FLOOR_2GHZ
;
2030 if (res
->flags
& WPA_SCAN_LEVEL_DBM
) {
2031 res
->snr
= res
->level
- res
->noise
;
2033 /* Level is not in dBm, so we can't calculate
2034 * SNR. Just use raw level (units unknown). */
2035 res
->snr
= res
->level
;
2040 static unsigned int max_ht20_rate(int snr
)
2043 return 6500; /* HT20 MCS0 */
2045 return 13000; /* HT20 MCS1 */
2047 return 19500; /* HT20 MCS2 */
2049 return 26000; /* HT20 MCS3 */
2051 return 39000; /* HT20 MCS4 */
2053 return 52000; /* HT20 MCS5 */
2055 return 58500; /* HT20 MCS6 */
2056 return 65000; /* HT20 MCS7 */
2060 static unsigned int max_ht40_rate(int snr
)
2063 return 13500; /* HT40 MCS0 */
2065 return 27000; /* HT40 MCS1 */
2067 return 40500; /* HT40 MCS2 */
2069 return 54000; /* HT40 MCS3 */
2071 return 81000; /* HT40 MCS4 */
2073 return 108000; /* HT40 MCS5 */
2075 return 121500; /* HT40 MCS6 */
2076 return 135000; /* HT40 MCS7 */
2080 static unsigned int max_vht80_rate(int snr
)
2085 return 29300; /* VHT80 MCS0 */
2087 return 58500; /* VHT80 MCS1 */
2089 return 87800; /* VHT80 MCS2 */
2091 return 117000; /* VHT80 MCS3 */
2093 return 175500; /* VHT80 MCS4 */
2095 return 234000; /* VHT80 MCS5 */
2097 return 263300; /* VHT80 MCS6 */
2099 return 292500; /* VHT80 MCS7 */
2101 return 351000; /* VHT80 MCS8 */
2102 return 390000; /* VHT80 MCS9 */
2106 void scan_est_throughput(struct wpa_supplicant
*wpa_s
,
2107 struct wpa_scan_res
*res
)
2109 enum local_hw_capab capab
= wpa_s
->hw_capab
;
2110 int rate
; /* max legacy rate in 500 kb/s units */
2112 unsigned int est
, tmp
;
2115 if (res
->est_throughput
)
2118 /* Get maximum legacy rate */
2119 rate
= wpa_scan_get_max_rate(res
);
2121 /* Limit based on estimated SNR */
2122 if (rate
> 1 * 2 && snr
< 1)
2124 else if (rate
> 2 * 2 && snr
< 4)
2126 else if (rate
> 6 * 2 && snr
< 5)
2128 else if (rate
> 9 * 2 && snr
< 6)
2130 else if (rate
> 12 * 2 && snr
< 7)
2132 else if (rate
> 18 * 2 && snr
< 10)
2134 else if (rate
> 24 * 2 && snr
< 11)
2136 else if (rate
> 36 * 2 && snr
< 15)
2138 else if (rate
> 48 * 2 && snr
< 19)
2140 else if (rate
> 54 * 2 && snr
< 21)
2144 if (capab
== CAPAB_HT
|| capab
== CAPAB_HT40
|| capab
== CAPAB_VHT
) {
2145 ie
= wpa_scan_get_ie(res
, WLAN_EID_HT_CAP
);
2147 tmp
= max_ht20_rate(snr
);
2153 if (capab
== CAPAB_HT40
|| capab
== CAPAB_VHT
) {
2154 ie
= wpa_scan_get_ie(res
, WLAN_EID_HT_OPERATION
);
2155 if (ie
&& ie
[1] >= 2 &&
2156 (ie
[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK
)) {
2157 tmp
= max_ht40_rate(snr
);
2163 if (capab
== CAPAB_VHT
) {
2164 /* Use +1 to assume VHT is always faster than HT */
2165 ie
= wpa_scan_get_ie(res
, WLAN_EID_VHT_CAP
);
2167 tmp
= max_ht20_rate(snr
) + 1;
2171 ie
= wpa_scan_get_ie(res
, WLAN_EID_HT_OPERATION
);
2172 if (ie
&& ie
[1] >= 2 &&
2174 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK
)) {
2175 tmp
= max_ht40_rate(snr
) + 1;
2180 ie
= wpa_scan_get_ie(res
, WLAN_EID_VHT_OPERATION
);
2181 if (ie
&& ie
[1] >= 1 &&
2182 (ie
[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK
)) {
2183 tmp
= max_vht80_rate(snr
) + 1;
2190 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2192 res
->est_throughput
= est
;
2197 * wpa_supplicant_get_scan_results - Get scan results
2198 * @wpa_s: Pointer to wpa_supplicant data
2199 * @info: Information about what was scanned or %NULL if not available
2200 * @new_scan: Whether a new scan was performed
2201 * Returns: Scan results, %NULL on failure
2203 * This function request the current scan results from the driver and updates
2204 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2205 * results with wpa_scan_results_free().
2207 struct wpa_scan_results
*
2208 wpa_supplicant_get_scan_results(struct wpa_supplicant
*wpa_s
,
2209 struct scan_info
*info
, int new_scan
)
2211 struct wpa_scan_results
*scan_res
;
2213 int (*compar
)(const void *, const void *) = wpa_scan_result_compar
;
2215 scan_res
= wpa_drv_get_scan_results2(wpa_s
);
2216 if (scan_res
== NULL
) {
2217 wpa_dbg(wpa_s
, MSG_DEBUG
, "Failed to get scan results");
2220 if (scan_res
->fetch_time
.sec
== 0) {
2222 * Make sure we have a valid timestamp if the driver wrapper
2223 * does not set this.
2225 os_get_reltime(&scan_res
->fetch_time
);
2227 filter_scan_res(wpa_s
, scan_res
);
2229 for (i
= 0; i
< scan_res
->num
; i
++) {
2230 struct wpa_scan_res
*scan_res_item
= scan_res
->res
[i
];
2232 scan_snr(scan_res_item
);
2233 scan_est_throughput(wpa_s
, scan_res_item
);
2237 if (wpas_wps_searching(wpa_s
)) {
2238 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Order scan results with WPS "
2239 "provisioning rules");
2240 compar
= wpa_scan_result_wps_compar
;
2242 #endif /* CONFIG_WPS */
2244 if (scan_res
->res
) {
2245 qsort(scan_res
->res
, scan_res
->num
,
2246 sizeof(struct wpa_scan_res
*), compar
);
2248 dump_scan_res(scan_res
);
2250 if (wpa_s
->ignore_post_flush_scan_res
) {
2251 /* FLUSH command aborted an ongoing scan and these are the
2252 * results from the aborted scan. Do not process the results to
2253 * maintain flushed state. */
2254 wpa_dbg(wpa_s
, MSG_DEBUG
,
2255 "Do not update BSS table based on pending post-FLUSH scan results");
2256 wpa_s
->ignore_post_flush_scan_res
= 0;
2260 wpa_bss_update_start(wpa_s
);
2261 for (i
= 0; i
< scan_res
->num
; i
++)
2262 wpa_bss_update_scan_res(wpa_s
, scan_res
->res
[i
],
2263 &scan_res
->fetch_time
);
2264 wpa_bss_update_end(wpa_s
, info
, new_scan
);
2271 * wpa_supplicant_update_scan_results - Update scan results from the driver
2272 * @wpa_s: Pointer to wpa_supplicant data
2273 * Returns: 0 on success, -1 on failure
2275 * This function updates the BSS table within wpa_supplicant based on the
2276 * currently available scan results from the driver without requesting a new
2277 * scan. This is used in cases where the driver indicates an association
2278 * (including roaming within ESS) and wpa_supplicant does not yet have the
2279 * needed information to complete the connection (e.g., to perform validation
2280 * steps in 4-way handshake).
2282 int wpa_supplicant_update_scan_results(struct wpa_supplicant
*wpa_s
)
2284 struct wpa_scan_results
*scan_res
;
2285 scan_res
= wpa_supplicant_get_scan_results(wpa_s
, NULL
, 0);
2286 if (scan_res
== NULL
)
2288 wpa_scan_results_free(scan_res
);
2295 * scan_only_handler - Reports scan results
2297 void scan_only_handler(struct wpa_supplicant
*wpa_s
,
2298 struct wpa_scan_results
*scan_res
)
2300 wpa_dbg(wpa_s
, MSG_DEBUG
, "Scan-only results received");
2301 if (wpa_s
->last_scan_req
== MANUAL_SCAN_REQ
&&
2302 wpa_s
->manual_scan_use_id
&& wpa_s
->own_scan_running
) {
2303 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_RESULTS
"id=%u",
2304 wpa_s
->manual_scan_id
);
2305 wpa_s
->manual_scan_use_id
= 0;
2307 wpa_msg_ctrl(wpa_s
, MSG_INFO
, WPA_EVENT_SCAN_RESULTS
);
2309 wpas_notify_scan_results(wpa_s
);
2310 wpas_notify_scan_done(wpa_s
, 1);
2311 if (wpa_s
->scan_work
) {
2312 struct wpa_radio_work
*work
= wpa_s
->scan_work
;
2313 wpa_s
->scan_work
= NULL
;
2314 radio_work_done(work
);
2317 if (wpa_s
->wpa_state
== WPA_SCANNING
)
2318 wpa_supplicant_set_state(wpa_s
, wpa_s
->scan_prev_wpa_state
);
2322 int wpas_scan_scheduled(struct wpa_supplicant
*wpa_s
)
2324 return eloop_is_timeout_registered(wpa_supplicant_scan
, wpa_s
, NULL
);
2328 struct wpa_driver_scan_params
*
2329 wpa_scan_clone_params(const struct wpa_driver_scan_params
*src
)
2331 struct wpa_driver_scan_params
*params
;
2335 params
= os_zalloc(sizeof(*params
));
2339 for (i
= 0; i
< src
->num_ssids
; i
++) {
2340 if (src
->ssids
[i
].ssid
) {
2341 n
= os_malloc(src
->ssids
[i
].ssid_len
);
2344 os_memcpy(n
, src
->ssids
[i
].ssid
,
2345 src
->ssids
[i
].ssid_len
);
2346 params
->ssids
[i
].ssid
= n
;
2347 params
->ssids
[i
].ssid_len
= src
->ssids
[i
].ssid_len
;
2350 params
->num_ssids
= src
->num_ssids
;
2352 if (src
->extra_ies
) {
2353 n
= os_malloc(src
->extra_ies_len
);
2356 os_memcpy(n
, src
->extra_ies
, src
->extra_ies_len
);
2357 params
->extra_ies
= n
;
2358 params
->extra_ies_len
= src
->extra_ies_len
;
2362 int len
= int_array_len(src
->freqs
);
2363 params
->freqs
= os_malloc((len
+ 1) * sizeof(int));
2364 if (params
->freqs
== NULL
)
2366 os_memcpy(params
->freqs
, src
->freqs
, (len
+ 1) * sizeof(int));
2369 if (src
->filter_ssids
) {
2370 params
->filter_ssids
= os_malloc(sizeof(*params
->filter_ssids
) *
2371 src
->num_filter_ssids
);
2372 if (params
->filter_ssids
== NULL
)
2374 os_memcpy(params
->filter_ssids
, src
->filter_ssids
,
2375 sizeof(*params
->filter_ssids
) *
2376 src
->num_filter_ssids
);
2377 params
->num_filter_ssids
= src
->num_filter_ssids
;
2380 params
->filter_rssi
= src
->filter_rssi
;
2381 params
->p2p_probe
= src
->p2p_probe
;
2382 params
->only_new_results
= src
->only_new_results
;
2383 params
->low_priority
= src
->low_priority
;
2384 params
->duration
= src
->duration
;
2385 params
->duration_mandatory
= src
->duration_mandatory
;
2387 if (src
->sched_scan_plans_num
> 0) {
2388 params
->sched_scan_plans
=
2389 os_malloc(sizeof(*src
->sched_scan_plans
) *
2390 src
->sched_scan_plans_num
);
2391 if (!params
->sched_scan_plans
)
2394 os_memcpy(params
->sched_scan_plans
, src
->sched_scan_plans
,
2395 sizeof(*src
->sched_scan_plans
) *
2396 src
->sched_scan_plans_num
);
2397 params
->sched_scan_plans_num
= src
->sched_scan_plans_num
;
2400 if (src
->mac_addr_rand
) {
2401 params
->mac_addr_rand
= src
->mac_addr_rand
;
2403 if (src
->mac_addr
&& src
->mac_addr_mask
) {
2406 mac_addr
= os_malloc(2 * ETH_ALEN
);
2410 os_memcpy(mac_addr
, src
->mac_addr
, ETH_ALEN
);
2411 os_memcpy(mac_addr
+ ETH_ALEN
, src
->mac_addr_mask
,
2413 params
->mac_addr
= mac_addr
;
2414 params
->mac_addr_mask
= mac_addr
+ ETH_ALEN
;
2421 bssid
= os_malloc(ETH_ALEN
);
2424 os_memcpy(bssid
, src
->bssid
, ETH_ALEN
);
2425 params
->bssid
= bssid
;
2428 params
->relative_rssi_set
= src
->relative_rssi_set
;
2429 params
->relative_rssi
= src
->relative_rssi
;
2430 params
->relative_adjust_band
= src
->relative_adjust_band
;
2431 params
->relative_adjust_rssi
= src
->relative_adjust_rssi
;
2435 wpa_scan_free_params(params
);
2440 void wpa_scan_free_params(struct wpa_driver_scan_params
*params
)
2447 for (i
= 0; i
< params
->num_ssids
; i
++)
2448 os_free((u8
*) params
->ssids
[i
].ssid
);
2449 os_free((u8
*) params
->extra_ies
);
2450 os_free(params
->freqs
);
2451 os_free(params
->filter_ssids
);
2452 os_free(params
->sched_scan_plans
);
2455 * Note: params->mac_addr_mask points to same memory allocation and
2456 * must not be freed separately.
2458 os_free((u8
*) params
->mac_addr
);
2460 os_free((u8
*) params
->bssid
);
2466 int wpas_start_pno(struct wpa_supplicant
*wpa_s
)
2469 size_t i
, num_ssid
, num_match_ssid
;
2470 struct wpa_ssid
*ssid
;
2471 struct wpa_driver_scan_params params
;
2472 struct sched_scan_plan scan_plan
;
2473 unsigned int max_sched_scan_ssids
;
2475 if (!wpa_s
->sched_scan_supported
)
2478 if (wpa_s
->max_sched_scan_ssids
> WPAS_MAX_SCAN_SSIDS
)
2479 max_sched_scan_ssids
= WPAS_MAX_SCAN_SSIDS
;
2481 max_sched_scan_ssids
= wpa_s
->max_sched_scan_ssids
;
2482 if (max_sched_scan_ssids
< 1)
2485 if (wpa_s
->pno
|| wpa_s
->pno_sched_pending
)
2488 if ((wpa_s
->wpa_state
> WPA_SCANNING
) &&
2489 (wpa_s
->wpa_state
< WPA_COMPLETED
)) {
2490 wpa_printf(MSG_ERROR
, "PNO: In assoc process");
2494 if (wpa_s
->wpa_state
== WPA_SCANNING
) {
2495 wpa_supplicant_cancel_scan(wpa_s
);
2496 if (wpa_s
->sched_scanning
) {
2497 wpa_printf(MSG_DEBUG
, "Schedule PNO on completion of "
2498 "ongoing sched scan");
2499 wpa_supplicant_cancel_sched_scan(wpa_s
);
2500 wpa_s
->pno_sched_pending
= 1;
2505 if (wpa_s
->sched_scan_stop_req
) {
2506 wpa_printf(MSG_DEBUG
,
2507 "Schedule PNO after previous sched scan has stopped");
2508 wpa_s
->pno_sched_pending
= 1;
2512 os_memset(¶ms
, 0, sizeof(params
));
2514 num_ssid
= num_match_ssid
= 0;
2515 ssid
= wpa_s
->conf
->ssid
;
2517 if (!wpas_network_disabled(wpa_s
, ssid
)) {
2519 if (ssid
->scan_ssid
)
2525 if (num_match_ssid
== 0) {
2526 wpa_printf(MSG_DEBUG
, "PNO: No configured SSIDs");
2530 if (num_match_ssid
> num_ssid
) {
2531 params
.num_ssids
++; /* wildcard */
2535 if (num_ssid
> max_sched_scan_ssids
) {
2536 wpa_printf(MSG_DEBUG
, "PNO: Use only the first %u SSIDs from "
2537 "%u", max_sched_scan_ssids
, (unsigned int) num_ssid
);
2538 num_ssid
= max_sched_scan_ssids
;
2541 if (num_match_ssid
> wpa_s
->max_match_sets
) {
2542 num_match_ssid
= wpa_s
->max_match_sets
;
2543 wpa_dbg(wpa_s
, MSG_DEBUG
, "PNO: Too many SSIDs to match");
2545 params
.filter_ssids
= os_calloc(num_match_ssid
,
2546 sizeof(struct wpa_driver_scan_filter
));
2547 if (params
.filter_ssids
== NULL
)
2552 ssid
= wpa_s
->conf
->pssid
[prio
];
2554 if (!wpas_network_disabled(wpa_s
, ssid
)) {
2555 if (ssid
->scan_ssid
&& params
.num_ssids
< num_ssid
) {
2556 params
.ssids
[params
.num_ssids
].ssid
=
2558 params
.ssids
[params
.num_ssids
].ssid_len
=
2562 os_memcpy(params
.filter_ssids
[i
].ssid
, ssid
->ssid
,
2564 params
.filter_ssids
[i
].ssid_len
= ssid
->ssid_len
;
2565 params
.num_filter_ssids
++;
2567 if (i
== num_match_ssid
)
2572 else if (prio
+ 1 == wpa_s
->conf
->num_prio
)
2575 ssid
= wpa_s
->conf
->pssid
[++prio
];
2578 if (wpa_s
->conf
->filter_rssi
)
2579 params
.filter_rssi
= wpa_s
->conf
->filter_rssi
;
2581 if (wpa_s
->sched_scan_plans_num
) {
2582 params
.sched_scan_plans
= wpa_s
->sched_scan_plans
;
2583 params
.sched_scan_plans_num
= wpa_s
->sched_scan_plans_num
;
2585 /* Set one scan plan that will run infinitely */
2586 if (wpa_s
->conf
->sched_scan_interval
)
2587 scan_plan
.interval
= wpa_s
->conf
->sched_scan_interval
;
2589 scan_plan
.interval
= 10;
2591 scan_plan
.iterations
= 0;
2592 params
.sched_scan_plans
= &scan_plan
;
2593 params
.sched_scan_plans_num
= 1;
2596 if (params
.freqs
== NULL
&& wpa_s
->manual_sched_scan_freqs
) {
2597 wpa_dbg(wpa_s
, MSG_DEBUG
, "Limit sched scan to specified channels");
2598 params
.freqs
= wpa_s
->manual_sched_scan_freqs
;
2601 if ((wpa_s
->mac_addr_rand_enable
& MAC_ADDR_RAND_PNO
) &&
2602 wpa_s
->wpa_state
<= WPA_SCANNING
) {
2603 params
.mac_addr_rand
= 1;
2604 if (wpa_s
->mac_addr_pno
) {
2605 params
.mac_addr
= wpa_s
->mac_addr_pno
;
2606 params
.mac_addr_mask
= wpa_s
->mac_addr_pno
+ ETH_ALEN
;
2610 wpa_scan_set_relative_rssi_params(wpa_s
, ¶ms
);
2612 ret
= wpa_supplicant_start_sched_scan(wpa_s
, ¶ms
);
2613 os_free(params
.filter_ssids
);
2617 wpa_msg(wpa_s
, MSG_ERROR
, "Failed to schedule PNO");
2622 int wpas_stop_pno(struct wpa_supplicant
*wpa_s
)
2629 ret
= wpa_supplicant_stop_sched_scan(wpa_s
);
2630 wpa_s
->sched_scan_stop_req
= 1;
2633 wpa_s
->pno_sched_pending
= 0;
2635 if (wpa_s
->wpa_state
== WPA_SCANNING
)
2636 wpa_supplicant_req_scan(wpa_s
, 0, 0);
2642 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant
*wpa_s
,
2645 type
&= MAC_ADDR_RAND_ALL
;
2646 wpa_s
->mac_addr_rand_enable
&= ~type
;
2648 if (type
& MAC_ADDR_RAND_SCAN
) {
2649 os_free(wpa_s
->mac_addr_scan
);
2650 wpa_s
->mac_addr_scan
= NULL
;
2653 if (type
& MAC_ADDR_RAND_SCHED_SCAN
) {
2654 os_free(wpa_s
->mac_addr_sched_scan
);
2655 wpa_s
->mac_addr_sched_scan
= NULL
;
2658 if (type
& MAC_ADDR_RAND_PNO
) {
2659 os_free(wpa_s
->mac_addr_pno
);
2660 wpa_s
->mac_addr_pno
= NULL
;
2665 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant
*wpa_s
,
2666 unsigned int type
, const u8
*addr
,
2671 wpas_mac_addr_rand_scan_clear(wpa_s
, type
);
2674 tmp
= os_malloc(2 * ETH_ALEN
);
2677 os_memcpy(tmp
, addr
, ETH_ALEN
);
2678 os_memcpy(tmp
+ ETH_ALEN
, mask
, ETH_ALEN
);
2681 if (type
== MAC_ADDR_RAND_SCAN
) {
2682 wpa_s
->mac_addr_scan
= tmp
;
2683 } else if (type
== MAC_ADDR_RAND_SCHED_SCAN
) {
2684 wpa_s
->mac_addr_sched_scan
= tmp
;
2685 } else if (type
== MAC_ADDR_RAND_PNO
) {
2686 wpa_s
->mac_addr_pno
= tmp
;
2688 wpa_printf(MSG_INFO
,
2689 "scan: Invalid MAC randomization type=0x%x",
2695 wpa_s
->mac_addr_rand_enable
|= type
;
2700 int wpas_abort_ongoing_scan(struct wpa_supplicant
*wpa_s
)
2702 struct wpa_radio_work
*work
;
2703 struct wpa_radio
*radio
= wpa_s
->radio
;
2705 dl_list_for_each(work
, &radio
->work
, struct wpa_radio_work
, list
) {
2706 if (work
->wpa_s
!= wpa_s
|| !work
->started
||
2707 (os_strcmp(work
->type
, "scan") != 0 &&
2708 os_strcmp(work
->type
, "p2p-scan") != 0))
2710 wpa_dbg(wpa_s
, MSG_DEBUG
, "Abort an ongoing scan");
2711 return wpa_drv_abort_scan(wpa_s
, wpa_s
->curr_scan_cookie
);
2714 wpa_dbg(wpa_s
, MSG_DEBUG
, "No ongoing scan/p2p-scan found to abort");
2719 int wpas_sched_scan_plans_set(struct wpa_supplicant
*wpa_s
, const char *cmd
)
2721 struct sched_scan_plan
*scan_plans
= NULL
;
2722 const char *token
, *context
= NULL
;
2723 unsigned int num
= 0;
2729 wpa_printf(MSG_DEBUG
, "Clear sched scan plans");
2730 os_free(wpa_s
->sched_scan_plans
);
2731 wpa_s
->sched_scan_plans
= NULL
;
2732 wpa_s
->sched_scan_plans_num
= 0;
2736 while ((token
= cstr_token(cmd
, " ", &context
))) {
2738 struct sched_scan_plan
*scan_plan
, *n
;
2740 n
= os_realloc_array(scan_plans
, num
+ 1, sizeof(*scan_plans
));
2745 scan_plan
= &scan_plans
[num
];
2748 ret
= sscanf(token
, "%u:%u", &scan_plan
->interval
,
2749 &scan_plan
->iterations
);
2750 if (ret
<= 0 || ret
> 2 || !scan_plan
->interval
) {
2751 wpa_printf(MSG_ERROR
,
2752 "Invalid sched scan plan input: %s", token
);
2756 if (scan_plan
->interval
> wpa_s
->max_sched_scan_plan_interval
) {
2757 wpa_printf(MSG_WARNING
,
2758 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2759 num
, scan_plan
->interval
,
2760 wpa_s
->max_sched_scan_plan_interval
);
2761 scan_plan
->interval
=
2762 wpa_s
->max_sched_scan_plan_interval
;
2766 scan_plan
->iterations
= 0;
2770 if (!scan_plan
->iterations
) {
2771 wpa_printf(MSG_ERROR
,
2772 "scan plan %u: Number of iterations cannot be zero",
2777 if (scan_plan
->iterations
>
2778 wpa_s
->max_sched_scan_plan_iterations
) {
2779 wpa_printf(MSG_WARNING
,
2780 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2781 num
, scan_plan
->iterations
,
2782 wpa_s
->max_sched_scan_plan_iterations
);
2783 scan_plan
->iterations
=
2784 wpa_s
->max_sched_scan_plan_iterations
;
2787 wpa_printf(MSG_DEBUG
,
2788 "scan plan %u: interval=%u iterations=%u",
2789 num
, scan_plan
->interval
, scan_plan
->iterations
);
2793 wpa_printf(MSG_ERROR
, "Invalid scan plans entry");
2797 if (cstr_token(cmd
, " ", &context
) || scan_plans
[num
- 1].iterations
) {
2798 wpa_printf(MSG_ERROR
,
2799 "All scan plans but the last must specify a number of iterations");
2803 wpa_printf(MSG_DEBUG
, "scan plan %u (last plan): interval=%u",
2804 num
, scan_plans
[num
- 1].interval
);
2806 if (num
> wpa_s
->max_sched_scan_plans
) {
2807 wpa_printf(MSG_WARNING
,
2808 "Too many scheduled scan plans (only %u supported)",
2809 wpa_s
->max_sched_scan_plans
);
2810 wpa_printf(MSG_WARNING
,
2811 "Use only the first %u scan plans, and the last one (in infinite loop)",
2812 wpa_s
->max_sched_scan_plans
- 1);
2813 os_memcpy(&scan_plans
[wpa_s
->max_sched_scan_plans
- 1],
2814 &scan_plans
[num
- 1], sizeof(*scan_plans
));
2815 num
= wpa_s
->max_sched_scan_plans
;
2818 os_free(wpa_s
->sched_scan_plans
);
2819 wpa_s
->sched_scan_plans
= scan_plans
;
2820 wpa_s
->sched_scan_plans_num
= num
;
2825 os_free(scan_plans
);
2826 wpa_printf(MSG_ERROR
, "invalid scan plans list");
2832 * wpas_scan_reset_sched_scan - Reset sched_scan state
2833 * @wpa_s: Pointer to wpa_supplicant data
2835 * This function is used to cancel a running scheduled scan and to reset an
2836 * internal scan state to continue with a regular scan on the following
2837 * wpa_supplicant_req_scan() calls.
2839 void wpas_scan_reset_sched_scan(struct wpa_supplicant
*wpa_s
)
2841 wpa_s
->normal_scans
= 0;
2842 if (wpa_s
->sched_scanning
) {
2843 wpa_s
->sched_scan_timed_out
= 0;
2844 wpa_s
->prev_sched_ssid
= NULL
;
2845 wpa_supplicant_cancel_sched_scan(wpa_s
);
2850 void wpas_scan_restart_sched_scan(struct wpa_supplicant
*wpa_s
)
2852 /* simulate timeout to restart the sched scan */
2853 wpa_s
->sched_scan_timed_out
= 1;
2854 wpa_s
->prev_sched_ssid
= NULL
;
2855 wpa_supplicant_cancel_sched_scan(wpa_s
);