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 radio_work_done(work);
169 wpa_supplicant_notify_scanning(wpa_s, 1);
171 if (wpa_s->clear_driver_scan_cache) {
172 wpa_printf(MSG_DEBUG,
173 "Request driver to clear scan cache due to local BSS flush");
174 params->only_new_results = 1;
176 ret = wpa_drv_scan(wpa_s, params);
177 wpa_scan_free_params(params);
180 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
182 if (wpa_s->disconnected)
185 wpa_supplicant_notify_scanning(wpa_s, 0);
186 wpas_notify_scan_done(wpa_s, 0);
187 if (wpa_s->wpa_state == WPA_SCANNING)
188 wpa_supplicant_set_state(wpa_s,
189 wpa_s->scan_prev_wpa_state);
190 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
191 ret, retry ? " retry=1" : "");
192 radio_work_done(work);
195 /* Restore scan_req since we will try to scan again */
196 wpa_s->scan_req = wpa_s->last_scan_req;
197 wpa_supplicant_req_scan(wpa_s, 1, 0);
202 os_get_reltime(&wpa_s->scan_trigger_time);
204 wpa_s->normal_scans++;
205 wpa_s->own_scan_requested = 1;
206 wpa_s->clear_driver_scan_cache = 0;
207 wpa_s->scan_work = work;
212 * wpa_supplicant_trigger_scan - Request driver to start a scan
213 * @wpa_s: Pointer to wpa_supplicant data
214 * @params: Scan parameters
215 * Returns: 0 on success, -1 on failure
217 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
218 struct wpa_driver_scan_params *params)
220 struct wpa_driver_scan_params *ctx;
222 if (wpa_s->scan_work) {
223 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
227 ctx = wpa_scan_clone_params(params);
231 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
233 wpa_scan_free_params(ctx);
242 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
244 struct wpa_supplicant *wpa_s = eloop_ctx;
246 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
248 if (wpa_supplicant_req_sched_scan(wpa_s))
249 wpa_supplicant_req_scan(wpa_s, 0, 0);
254 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
256 struct wpa_supplicant *wpa_s = eloop_ctx;
258 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
260 wpa_s->sched_scan_timed_out = 1;
261 wpa_supplicant_cancel_sched_scan(wpa_s);
266 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
267 struct wpa_driver_scan_params *params)
271 wpa_supplicant_notify_scanning(wpa_s, 1);
272 ret = wpa_drv_sched_scan(wpa_s, params);
274 wpa_supplicant_notify_scanning(wpa_s, 0);
276 wpa_s->sched_scanning = 1;
282 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
286 ret = wpa_drv_stop_sched_scan(wpa_s);
288 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
289 /* TODO: what to do if stopping fails? */
297 static struct wpa_driver_scan_filter *
298 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
300 struct wpa_driver_scan_filter *ssids;
301 struct wpa_ssid *ssid;
305 if (!conf->filter_ssids)
308 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
309 if (ssid->ssid && ssid->ssid_len)
314 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
318 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
319 if (!ssid->ssid || !ssid->ssid_len)
321 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
322 ssids[*num_ssids].ssid_len = ssid->ssid_len;
330 static void wpa_supplicant_optimize_freqs(
331 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
334 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
336 /* Optimize provisioning state scan based on GO information */
337 if (wpa_s->p2p_in_provisioning < 5 &&
338 wpa_s->go_params->freq > 0) {
339 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
340 "preferred frequency %d MHz",
341 wpa_s->go_params->freq);
342 params->freqs = os_calloc(2, sizeof(int));
344 params->freqs[0] = wpa_s->go_params->freq;
345 } else if (wpa_s->p2p_in_provisioning < 8 &&
346 wpa_s->go_params->freq_list[0]) {
347 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
349 int_array_concat(¶ms->freqs,
350 wpa_s->go_params->freq_list);
352 int_array_sort_unique(params->freqs);
354 wpa_s->p2p_in_provisioning++;
357 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
359 * Optimize scan based on GO information during persistent
362 if (wpa_s->p2p_in_invitation < 5 &&
363 wpa_s->p2p_invite_go_freq > 0) {
364 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
365 wpa_s->p2p_invite_go_freq);
366 params->freqs = os_calloc(2, sizeof(int));
368 params->freqs[0] = wpa_s->p2p_invite_go_freq;
370 wpa_s->p2p_in_invitation++;
371 if (wpa_s->p2p_in_invitation > 20) {
373 * This should not really happen since the variable is
374 * cleared on group removal, but if it does happen, make
375 * sure we do not get stuck in special invitation scan
378 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
379 wpa_s->p2p_in_invitation = 0;
382 #endif /* CONFIG_P2P */
385 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
387 * Optimize post-provisioning scan based on channel used
388 * during provisioning.
390 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
391 "that was used during provisioning", wpa_s->wps_freq);
392 params->freqs = os_calloc(2, sizeof(int));
394 params->freqs[0] = wpa_s->wps_freq;
396 } else if (wpa_s->after_wps)
399 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
401 /* Optimize provisioning scan based on already known channel */
402 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
404 params->freqs = os_calloc(2, sizeof(int));
406 params->freqs[0] = wpa_s->wps_freq;
407 wpa_s->known_wps_freq = 0; /* only do this once */
409 #endif /* CONFIG_WPS */
413 #ifdef CONFIG_INTERWORKING
414 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
417 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
418 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
420 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
422 if (!is_zero_ether_addr(wpa_s->conf->hessid))
423 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
425 #endif /* CONFIG_INTERWORKING */
428 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
430 struct wpabuf *extra_ie = NULL;
435 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
436 #endif /* CONFIG_WPS */
438 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
440 if (ext_capab_len > 0 &&
441 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
442 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
444 #ifdef CONFIG_INTERWORKING
445 if (wpa_s->conf->interworking &&
446 wpabuf_resize(&extra_ie, 100) == 0)
447 wpas_add_interworking_elements(wpa_s, extra_ie);
448 #endif /* CONFIG_INTERWORKING */
451 wps = wpas_wps_in_use(wpa_s, &req_type);
454 struct wpabuf *wps_ie;
455 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
458 wpa_s->wps->uuid, req_type,
461 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
462 wpabuf_put_buf(extra_ie, wps_ie);
469 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
470 if (wpabuf_resize(&extra_ie, ielen) == 0)
471 wpas_p2p_scan_ie(wpa_s, extra_ie);
473 #endif /* CONFIG_P2P */
475 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
477 #endif /* CONFIG_WPS */
480 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
481 wpas_hs20_add_indication(extra_ie, -1);
482 #endif /* CONFIG_HS20 */
485 if (wpa_s->fst_ies &&
486 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
487 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
488 #endif /* CONFIG_FST */
491 /* Send cellular capabilities for potential MBO STAs */
492 if (wpabuf_resize(&extra_ie, 9) == 0)
493 wpas_mbo_scan_ie(wpa_s, extra_ie);
494 #endif /* CONFIG_MBO */
496 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
497 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
499 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
500 wpabuf_put_buf(extra_ie, buf);
510 * Check whether there are any enabled networks or credentials that could be
511 * used for a non-P2P connection.
513 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
515 struct wpa_ssid *ssid;
517 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
518 if (wpas_network_disabled(wpa_s, ssid))
520 if (!ssid->p2p_group)
524 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
525 wpa_s->conf->auto_interworking)
531 #endif /* CONFIG_P2P */
534 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
535 enum hostapd_hw_mode band,
536 struct wpa_driver_scan_params *params)
538 /* Include only supported channels for the specified band */
539 struct hostapd_hw_modes *mode;
542 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
544 /* No channels supported in this band - use empty list */
545 params->freqs = os_zalloc(sizeof(int));
549 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
550 if (params->freqs == NULL)
552 for (count = 0, i = 0; i < mode->num_channels; i++) {
553 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
555 params->freqs[count++] = mode->channels[i].freq;
560 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
561 struct wpa_driver_scan_params *params)
563 if (wpa_s->hw.modes == NULL)
564 return; /* unknown what channels the driver supports */
566 return; /* already using a limited channel set */
567 if (wpa_s->setband == WPA_SETBAND_5G)
568 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
570 else if (wpa_s->setband == WPA_SETBAND_2G)
571 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
576 static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
577 struct wpa_driver_scan_params *params,
581 struct wpa_ssid *ssid;
583 for (i = 0; i < wpa_s->scan_id_count; i++) {
586 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
587 if (!ssid || !ssid->scan_ssid)
590 for (j = 0; j < params->num_ssids; j++) {
591 if (params->ssids[j].ssid_len == ssid->ssid_len &&
592 params->ssids[j].ssid &&
593 os_memcmp(params->ssids[j].ssid, ssid->ssid,
594 ssid->ssid_len) == 0)
597 if (j < params->num_ssids)
598 continue; /* already in the list */
600 if (params->num_ssids + 1 > max_ssids) {
601 wpa_printf(MSG_DEBUG,
602 "Over max scan SSIDs for manual request");
606 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
607 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
608 params->ssids[params->num_ssids].ssid = ssid->ssid;
609 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
613 wpa_s->scan_id_count = 0;
617 static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
618 struct wpa_driver_scan_params *params,
623 if (wpa_s->ssids_from_scan_req == NULL ||
624 wpa_s->num_ssids_from_scan_req == 0)
627 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
628 wpa_s->num_ssids_from_scan_req = max_ssids;
629 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
630 (unsigned int) max_ssids);
633 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
634 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
635 params->ssids[i].ssid_len =
636 wpa_s->ssids_from_scan_req[i].ssid_len;
637 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
638 params->ssids[i].ssid,
639 params->ssids[i].ssid_len);
642 params->num_ssids = wpa_s->num_ssids_from_scan_req;
643 wpa_s->num_ssids_from_scan_req = 0;
648 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
650 struct wpa_supplicant *wpa_s = eloop_ctx;
651 struct wpa_ssid *ssid;
652 int ret, p2p_in_prog;
653 struct wpabuf *extra_ie = NULL;
654 struct wpa_driver_scan_params params;
655 struct wpa_driver_scan_params *scan_params;
657 int connect_without_scan = 0;
659 if (wpa_s->pno || wpa_s->pno_sched_pending) {
660 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
664 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
665 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
669 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
670 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
671 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
675 if (wpa_s->scanning) {
677 * If we are already in scanning state, we shall reschedule the
678 * the incoming scan request.
680 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
681 wpa_supplicant_req_scan(wpa_s, 1, 0);
685 if (!wpa_supplicant_enabled_networks(wpa_s) &&
686 wpa_s->scan_req == NORMAL_SCAN_REQ) {
687 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
688 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
692 if (wpa_s->conf->ap_scan != 0 &&
693 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
694 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
695 "overriding ap_scan configuration");
696 wpa_s->conf->ap_scan = 0;
697 wpas_notify_ap_scan_changed(wpa_s);
700 if (wpa_s->conf->ap_scan == 0) {
701 wpa_supplicant_gen_assoc_event(wpa_s);
706 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
707 wpa_s->connect_without_scan) {
708 connect_without_scan = 1;
709 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
710 if (ssid == wpa_s->connect_without_scan)
715 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
716 if (p2p_in_prog && p2p_in_prog != 2 &&
718 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
719 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
720 wpa_supplicant_req_scan(wpa_s, 5, 0);
724 if (wpa_s->conf->ap_scan == 2)
727 max_ssids = wpa_s->max_scan_ssids;
728 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
729 max_ssids = WPAS_MAX_SCAN_SSIDS;
732 wpa_s->last_scan_req = wpa_s->scan_req;
733 wpa_s->scan_req = NORMAL_SCAN_REQ;
735 if (connect_without_scan) {
736 wpa_s->connect_without_scan = NULL;
738 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
739 "without scan step");
740 wpa_supplicant_associate(wpa_s, NULL, ssid);
745 os_memset(¶ms, 0, sizeof(params));
747 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
748 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
749 wpa_s->wpa_state == WPA_INACTIVE)
750 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
753 * If autoscan has set its own scanning parameters
755 if (wpa_s->autoscan_params != NULL) {
756 scan_params = wpa_s->autoscan_params;
760 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
761 wpa_set_ssids_from_scan_req(wpa_s, ¶ms, max_ssids)) {
762 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
767 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
768 wpa_s->go_params && !wpa_s->conf->passive_scan) {
769 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
770 wpa_s->p2p_in_provisioning,
771 wpa_s->show_group_started);
772 params.ssids[0].ssid = wpa_s->go_params->ssid;
773 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
774 params.num_ssids = 1;
778 if (wpa_s->p2p_in_invitation) {
779 if (wpa_s->current_ssid) {
780 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
781 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
782 params.ssids[0].ssid_len =
783 wpa_s->current_ssid->ssid_len;
784 params.num_ssids = 1;
786 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
790 #endif /* CONFIG_P2P */
792 /* Find the starting point from which to continue scanning */
793 ssid = wpa_s->conf->ssid;
794 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
796 if (ssid == wpa_s->prev_scan_ssid) {
804 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
807 #endif /* CONFIG_AP */
808 wpa_s->conf->ap_scan == 2) {
809 wpa_s->connect_without_scan = NULL;
810 wpa_s->prev_scan_wildcard = 0;
811 wpa_supplicant_assoc_try(wpa_s, ssid);
813 } else if (wpa_s->conf->ap_scan == 2) {
815 * User-initiated scan request in ap_scan == 2; scan with
819 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
821 * Perform single-channel single-SSID scan for
822 * reassociate-to-same-BSS operation.
825 ssid = wpa_s->current_ssid;
826 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
827 ssid->ssid, ssid->ssid_len);
828 params.ssids[0].ssid = ssid->ssid;
829 params.ssids[0].ssid_len = ssid->ssid_len;
830 params.num_ssids = 1;
833 * Allocate memory for frequency array, allocate one extra
834 * slot for the zero-terminator.
836 params.freqs = os_malloc(sizeof(int) * 2);
837 if (params.freqs == NULL) {
838 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
841 params.freqs[0] = wpa_s->assoc_freq;
845 * Reset the reattach flag so that we fall back to full scan if
850 struct wpa_ssid *start = ssid, *tssid;
852 if (ssid == NULL && max_ssids > 1)
853 ssid = wpa_s->conf->ssid;
855 if (!wpas_network_disabled(wpa_s, ssid) &&
857 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
858 ssid->ssid, ssid->ssid_len);
859 params.ssids[params.num_ssids].ssid =
861 params.ssids[params.num_ssids].ssid_len =
864 if (params.num_ssids + 1 >= max_ssids)
870 if (ssid == NULL && max_ssids > 1 &&
871 start != wpa_s->conf->ssid)
872 ssid = wpa_s->conf->ssid;
875 if (wpa_s->scan_id_count &&
876 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
877 wpa_set_scan_ssids(wpa_s, ¶ms, max_ssids);
879 for (tssid = wpa_s->conf->ssid;
880 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
881 tssid = tssid->next) {
882 if (wpas_network_disabled(wpa_s, tssid))
884 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
885 int_array_concat(¶ms.freqs,
888 os_free(params.freqs);
893 int_array_sort_unique(params.freqs);
896 if (ssid && max_ssids == 1) {
898 * If the driver is limited to 1 SSID at a time interleave
899 * wildcard SSID scans with specific SSID scans to avoid
900 * waiting a long time for a wildcard scan.
902 if (!wpa_s->prev_scan_wildcard) {
903 params.ssids[0].ssid = NULL;
904 params.ssids[0].ssid_len = 0;
905 wpa_s->prev_scan_wildcard = 1;
906 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
907 "wildcard SSID (Interleave with specific)");
909 wpa_s->prev_scan_ssid = ssid;
910 wpa_s->prev_scan_wildcard = 0;
911 wpa_dbg(wpa_s, MSG_DEBUG,
912 "Starting AP scan for specific SSID: %s",
913 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
918 wpa_s->prev_scan_ssid = ssid;
919 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
922 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
923 wpa_s->manual_scan_passive && params.num_ssids == 0) {
924 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
925 } else if (wpa_s->conf->passive_scan) {
926 wpa_dbg(wpa_s, MSG_DEBUG,
927 "Use passive scan based on configuration");
929 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
931 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
936 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
937 extra_ie = wpa_supplicant_extra_ies(wpa_s);
939 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
940 wpa_s->manual_scan_only_new) {
941 wpa_printf(MSG_DEBUG,
942 "Request driver to clear scan cache due to manual only_new=1 scan");
943 params.only_new_results = 1;
946 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
947 wpa_s->manual_scan_freqs) {
948 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
949 params.freqs = wpa_s->manual_scan_freqs;
950 wpa_s->manual_scan_freqs = NULL;
953 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
954 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
955 "generated frequency list");
956 params.freqs = wpa_s->next_scan_freqs;
958 os_free(wpa_s->next_scan_freqs);
959 wpa_s->next_scan_freqs = NULL;
960 wpa_setband_scan_freqs(wpa_s, ¶ms);
962 /* See if user specified frequencies. If so, scan only those. */
963 if (wpa_s->conf->freq_list && !params.freqs) {
964 wpa_dbg(wpa_s, MSG_DEBUG,
965 "Optimize scan based on conf->freq_list");
966 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
969 /* Use current associated channel? */
970 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
971 unsigned int num = wpa_s->num_multichan_concurrent;
973 params.freqs = os_calloc(num + 1, sizeof(int));
975 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
977 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
978 "current operating channels since "
979 "scan_cur_freq is enabled");
981 os_free(params.freqs);
987 params.filter_ssids = wpa_supplicant_build_filter_ssids(
988 wpa_s->conf, ¶ms.num_filter_ssids);
990 params.extra_ies = wpabuf_head(extra_ie);
991 params.extra_ies_len = wpabuf_len(extra_ie);
995 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
996 (wpa_s->show_group_started && wpa_s->go_params)) {
998 * The interface may not yet be in P2P mode, so we have to
999 * explicitly request P2P probe to disable CCK rates.
1001 params.p2p_probe = 1;
1003 #endif /* CONFIG_P2P */
1005 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
1006 params.mac_addr_rand = 1;
1007 if (wpa_s->mac_addr_scan) {
1008 params.mac_addr = wpa_s->mac_addr_scan;
1009 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1013 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1014 struct wpa_bss *bss;
1016 params.bssid = wpa_s->next_scan_bssid;
1017 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1018 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1019 params.ssids[0].ssid_len == 0) {
1020 params.ssids[0].ssid = bss->ssid;
1021 params.ssids[0].ssid_len = bss->ssid_len;
1022 wpa_dbg(wpa_s, MSG_DEBUG,
1023 "Scan a previously specified BSSID " MACSTR
1025 MAC2STR(params.bssid),
1026 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1028 wpa_dbg(wpa_s, MSG_DEBUG,
1029 "Scan a previously specified BSSID " MACSTR,
1030 MAC2STR(params.bssid));
1034 scan_params = ¶ms;
1039 * If the driver does not support multi-channel concurrency and a
1040 * virtual interface that shares the same radio with the wpa_s interface
1041 * is operating there may not be need to scan other channels apart from
1042 * the current operating channel on the other virtual interface. Filter
1043 * out other channels in case we are trying to find a connection for a
1044 * station interface when we are not configured to prefer station
1045 * connection and a concurrent operation is already in process.
1047 if (wpa_s->scan_for_connection &&
1048 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1049 !scan_params->freqs && !params.freqs &&
1050 wpas_is_p2p_prioritized(wpa_s) &&
1051 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1052 non_p2p_network_enabled(wpa_s)) {
1053 unsigned int num = wpa_s->num_multichan_concurrent;
1055 params.freqs = os_calloc(num + 1, sizeof(int));
1057 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1058 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1059 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1061 os_free(params.freqs);
1062 params.freqs = NULL;
1066 #endif /* CONFIG_P2P */
1068 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
1070 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1071 !wpa_s->manual_scan_freqs) {
1072 /* Restore manual_scan_freqs for the next attempt */
1073 wpa_s->manual_scan_freqs = params.freqs;
1074 params.freqs = NULL;
1077 wpabuf_free(extra_ie);
1078 os_free(params.freqs);
1079 os_free(params.filter_ssids);
1082 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1083 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1084 wpa_supplicant_set_state(wpa_s,
1085 wpa_s->scan_prev_wpa_state);
1086 /* Restore scan_req since we will try to scan again */
1087 wpa_s->scan_req = wpa_s->last_scan_req;
1088 wpa_supplicant_req_scan(wpa_s, 1, 0);
1090 wpa_s->scan_for_connection = 0;
1091 #ifdef CONFIG_INTERWORKING
1092 wpa_s->interworking_fast_assoc_tried = 0;
1093 #endif /* CONFIG_INTERWORKING */
1095 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1100 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1102 struct os_reltime remaining, new_int;
1105 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1110 if (cancelled && os_reltime_before(&remaining, &new_int)) {
1111 new_int.sec = remaining.sec;
1112 new_int.usec = remaining.usec;
1116 eloop_register_timeout(new_int.sec, new_int.usec,
1117 wpa_supplicant_scan, wpa_s, NULL);
1119 wpa_s->scan_interval = sec;
1124 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1125 * @wpa_s: Pointer to wpa_supplicant data
1126 * @sec: Number of seconds after which to scan
1127 * @usec: Number of microseconds after which to scan
1129 * This function is used to schedule a scan for neighboring access points after
1130 * the specified time.
1132 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1136 if (wpa_s->p2p_mgmt) {
1137 wpa_dbg(wpa_s, MSG_DEBUG,
1138 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1143 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1146 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1148 } else if (res == 0) {
1149 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1152 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1154 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1160 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1161 * @wpa_s: Pointer to wpa_supplicant data
1162 * @sec: Number of seconds after which to scan
1163 * @usec: Number of microseconds after which to scan
1164 * Returns: 0 on success or -1 otherwise
1166 * This function is used to schedule periodic scans for neighboring
1167 * access points after the specified time.
1169 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1172 if (!wpa_s->sched_scan_supported)
1175 eloop_register_timeout(sec, usec,
1176 wpa_supplicant_delayed_sched_scan_timeout,
1184 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1185 * @wpa_s: Pointer to wpa_supplicant data
1186 * Returns: 0 is sched_scan was started or -1 otherwise
1188 * This function is used to schedule periodic scans for neighboring
1189 * access points repeating the scan continuously.
1191 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1193 struct wpa_driver_scan_params params;
1194 struct wpa_driver_scan_params *scan_params;
1195 enum wpa_states prev_state;
1196 struct wpa_ssid *ssid = NULL;
1197 struct wpabuf *extra_ie = NULL;
1199 unsigned int max_sched_scan_ssids;
1202 struct sched_scan_plan scan_plan;
1204 if (!wpa_s->sched_scan_supported)
1207 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1208 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1210 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1211 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1214 if (wpa_s->sched_scanning) {
1215 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1220 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1221 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1222 /* Use wildcard SSID to find this network */
1224 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1229 if (!wpas_network_disabled(wpa_s, ssid) &&
1230 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1232 * Normal scan is more reliable and faster for WPS
1233 * operations and since these are for short periods of
1234 * time, the benefit of trying to use sched_scan would
1237 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1238 "sched_scan for WPS");
1241 #endif /* CONFIG_WPS */
1246 if (wpa_s->normal_scans < 3 &&
1247 (need_ssids <= wpa_s->max_scan_ssids ||
1248 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1250 * When normal scan can speed up operations, use that for the
1251 * first operations before starting the sched_scan to allow
1252 * user space sleep more. We do this only if the normal scan
1253 * has functionality that is suitable for this or if the
1254 * sched_scan does not have better support for multiple SSIDs.
1256 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1257 "sched_scan for initial scans (normal_scans=%d)",
1258 wpa_s->normal_scans);
1262 os_memset(¶ms, 0, sizeof(params));
1264 /* If we can't allocate space for the filters, we just don't filter */
1265 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1266 sizeof(struct wpa_driver_scan_filter));
1268 prev_state = wpa_s->wpa_state;
1269 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1270 wpa_s->wpa_state == WPA_INACTIVE)
1271 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1273 if (wpa_s->autoscan_params != NULL) {
1274 scan_params = wpa_s->autoscan_params;
1278 /* Find the starting point from which to continue scanning */
1279 ssid = wpa_s->conf->ssid;
1280 if (wpa_s->prev_sched_ssid) {
1282 if (ssid == wpa_s->prev_sched_ssid) {
1290 if (!ssid || !wpa_s->prev_sched_ssid) {
1291 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1292 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1293 wpa_s->first_sched_scan = 1;
1294 ssid = wpa_s->conf->ssid;
1295 wpa_s->prev_sched_ssid = ssid;
1299 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1304 if (wpas_network_disabled(wpa_s, ssid))
1307 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1308 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1309 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1310 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1311 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1312 ssid->ssid, ssid->ssid_len);
1313 params.filter_ssids[params.num_filter_ssids].ssid_len =
1315 params.num_filter_ssids++;
1316 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1318 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1319 "filter for sched_scan - drop filter");
1320 os_free(params.filter_ssids);
1321 params.filter_ssids = NULL;
1322 params.num_filter_ssids = 0;
1325 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1326 if (params.num_ssids == max_sched_scan_ssids)
1327 break; /* only room for broadcast SSID */
1328 wpa_dbg(wpa_s, MSG_DEBUG,
1329 "add to active scan ssid: %s",
1330 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1331 params.ssids[params.num_ssids].ssid =
1333 params.ssids[params.num_ssids].ssid_len =
1336 if (params.num_ssids >= max_sched_scan_ssids) {
1337 wpa_s->prev_sched_ssid = ssid;
1341 (wpas_network_disabled(wpa_s, ssid) ||
1348 wpa_s->prev_sched_ssid = ssid;
1352 if (params.num_filter_ssids == 0) {
1353 os_free(params.filter_ssids);
1354 params.filter_ssids = NULL;
1357 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1359 params.extra_ies = wpabuf_head(extra_ie);
1360 params.extra_ies_len = wpabuf_len(extra_ie);
1363 if (wpa_s->conf->filter_rssi)
1364 params.filter_rssi = wpa_s->conf->filter_rssi;
1366 /* See if user specified frequencies. If so, scan only those. */
1367 if (wpa_s->conf->freq_list && !params.freqs) {
1368 wpa_dbg(wpa_s, MSG_DEBUG,
1369 "Optimize scan based on conf->freq_list");
1370 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
1373 scan_params = ¶ms;
1376 wpa_s->sched_scan_timed_out = 0;
1379 * We cannot support multiple scan plans if the scan request includes
1380 * too many SSID's, so in this case use only the last scan plan and make
1381 * it run infinitely. It will be stopped by the timeout.
1383 if (wpa_s->sched_scan_plans_num == 1 ||
1384 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1385 params.sched_scan_plans = wpa_s->sched_scan_plans;
1386 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1387 } else if (wpa_s->sched_scan_plans_num > 1) {
1388 wpa_dbg(wpa_s, MSG_DEBUG,
1389 "Too many SSIDs. Default to using single scheduled_scan plan");
1390 params.sched_scan_plans =
1391 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1393 params.sched_scan_plans_num = 1;
1395 if (wpa_s->conf->sched_scan_interval)
1396 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1398 scan_plan.interval = 10;
1400 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1401 wpa_printf(MSG_WARNING,
1402 "Scan interval too long(%u), use the maximum allowed(%u)",
1404 wpa_s->max_sched_scan_plan_interval);
1405 scan_plan.interval =
1406 wpa_s->max_sched_scan_plan_interval;
1409 scan_plan.iterations = 0;
1410 params.sched_scan_plans = &scan_plan;
1411 params.sched_scan_plans_num = 1;
1414 if (ssid || !wpa_s->first_sched_scan) {
1415 wpa_dbg(wpa_s, MSG_DEBUG,
1416 "Starting sched scan: interval %u timeout %d",
1417 params.sched_scan_plans[0].interval,
1418 wpa_s->sched_scan_timeout);
1420 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
1423 wpa_setband_scan_freqs(wpa_s, scan_params);
1425 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1426 params.mac_addr_rand = 1;
1427 if (wpa_s->mac_addr_sched_scan) {
1428 params.mac_addr = wpa_s->mac_addr_sched_scan;
1429 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1434 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1435 wpabuf_free(extra_ie);
1436 os_free(params.filter_ssids);
1438 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1439 if (prev_state != wpa_s->wpa_state)
1440 wpa_supplicant_set_state(wpa_s, prev_state);
1444 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1445 if (ssid || !wpa_s->first_sched_scan) {
1446 wpa_s->sched_scan_timed_out = 0;
1447 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1448 wpa_supplicant_sched_scan_timeout,
1450 wpa_s->first_sched_scan = 0;
1451 wpa_s->sched_scan_timeout /= 2;
1452 params.sched_scan_plans[0].interval *= 2;
1453 if ((unsigned int) wpa_s->sched_scan_timeout <
1454 params.sched_scan_plans[0].interval ||
1455 params.sched_scan_plans[0].interval >
1456 wpa_s->max_sched_scan_plan_interval) {
1457 params.sched_scan_plans[0].interval = 10;
1458 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1462 /* If there is no more ssids, start next time from the beginning */
1464 wpa_s->prev_sched_ssid = NULL;
1471 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1472 * @wpa_s: Pointer to wpa_supplicant data
1474 * This function is used to cancel a scan request scheduled with
1475 * wpa_supplicant_req_scan().
1477 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1479 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1480 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1485 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1486 * @wpa_s: Pointer to wpa_supplicant data
1488 * This function is used to stop a delayed scheduled scan.
1490 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1492 if (!wpa_s->sched_scan_supported)
1495 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1496 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1502 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1503 * @wpa_s: Pointer to wpa_supplicant data
1505 * This function is used to stop a periodic scheduled scan.
1507 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1509 if (!wpa_s->sched_scanning)
1512 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1513 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1514 wpa_supplicant_stop_sched_scan(wpa_s);
1519 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1520 * @wpa_s: Pointer to wpa_supplicant data
1521 * @scanning: Whether scanning is currently in progress
1523 * This function is to generate scanning notifycations. It is called whenever
1524 * there may have been a change in scanning (scan started, completed, stopped).
1525 * wpas_notify_scanning() is called whenever the scanning state changed from the
1526 * previously notified state.
1528 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1531 if (wpa_s->scanning != scanning) {
1532 wpa_s->scanning = scanning;
1533 wpas_notify_scanning(wpa_s);
1538 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1544 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1545 for (i = 0; ie && i < ie[1]; i++) {
1546 if ((ie[i + 2] & 0x7f) > rate)
1547 rate = ie[i + 2] & 0x7f;
1550 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1551 for (i = 0; ie && i < ie[1]; i++) {
1552 if ((ie[i + 2] & 0x7f) > rate)
1553 rate = ie[i + 2] & 0x7f;
1561 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1562 * @res: Scan result entry
1563 * @ie: Information element identitifier (WLAN_EID_*)
1564 * Returns: Pointer to the information element (id field) or %NULL if not found
1566 * This function returns the first matching information element in the scan
1569 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1571 return get_ie((const u8 *) (res + 1), res->ie_len, ie);
1576 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1577 * @res: Scan result entry
1578 * @vendor_type: Vendor type (four octets starting the IE payload)
1579 * Returns: Pointer to the information element (id field) or %NULL if not found
1581 * This function returns the first matching information element in the scan
1584 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1587 const u8 *end, *pos;
1589 pos = (const u8 *) (res + 1);
1590 end = pos + res->ie_len;
1592 while (end - pos > 1) {
1593 if (2 + pos[1] > end - pos)
1595 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1596 vendor_type == WPA_GET_BE32(&pos[2]))
1606 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1607 * @res: Scan result entry
1608 * @vendor_type: Vendor type (four octets starting the IE payload)
1609 * Returns: Pointer to the information element (id field) or %NULL if not found
1611 * This function returns the first matching information element in the scan
1614 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1615 * from Beacon frames instead of either Beacon or Probe Response frames.
1617 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1620 const u8 *end, *pos;
1622 if (res->beacon_ie_len == 0)
1625 pos = (const u8 *) (res + 1);
1627 end = pos + res->beacon_ie_len;
1629 while (end - pos > 1) {
1630 if (2 + pos[1] > end - pos)
1632 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1633 vendor_type == WPA_GET_BE32(&pos[2]))
1643 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1644 * @res: Scan result entry
1645 * @vendor_type: Vendor type (four octets starting the IE payload)
1646 * Returns: Pointer to the information element payload or %NULL if not found
1648 * This function returns concatenated payload of possibly fragmented vendor
1649 * specific information elements in the scan result. The caller is responsible
1650 * for freeing the returned buffer.
1652 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1656 const u8 *end, *pos;
1658 buf = wpabuf_alloc(res->ie_len);
1662 pos = (const u8 *) (res + 1);
1663 end = pos + res->ie_len;
1665 while (end - pos > 1) {
1666 if (2 + pos[1] > end - pos)
1668 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1669 vendor_type == WPA_GET_BE32(&pos[2]))
1670 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1674 if (wpabuf_len(buf) == 0) {
1684 * Channels with a great SNR can operate at full rate. What is a great SNR?
1685 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1686 * rule of thumb is that any SNR above 20 is good." This one
1687 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1688 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1689 * conservative value.
1691 #define GREAT_SNR 30
1693 #define IS_5GHZ(n) (n > 4000)
1695 /* Compare function for sorting scan results. Return >0 if @b is considered
1697 static int wpa_scan_result_compar(const void *a, const void *b)
1699 #define MIN(a,b) a < b ? a : b
1700 struct wpa_scan_res **_wa = (void *) a;
1701 struct wpa_scan_res **_wb = (void *) b;
1702 struct wpa_scan_res *wa = *_wa;
1703 struct wpa_scan_res *wb = *_wb;
1705 int snr_a, snr_b, snr_a_full, snr_b_full;
1707 /* WPA/WPA2 support preferred */
1708 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1709 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1710 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1711 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1713 if (wpa_b && !wpa_a)
1715 if (!wpa_b && wpa_a)
1718 /* privacy support preferred */
1719 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1720 (wb->caps & IEEE80211_CAP_PRIVACY))
1722 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1723 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1726 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
1727 snr_a_full = wa->snr;
1728 snr_a = MIN(wa->snr, GREAT_SNR);
1729 snr_b_full = wb->snr;
1730 snr_b = MIN(wb->snr, GREAT_SNR);
1732 /* Level is not in dBm, so we can't calculate
1733 * SNR. Just use raw level (units unknown). */
1734 snr_a = snr_a_full = wa->level;
1735 snr_b = snr_b_full = wb->level;
1738 /* if SNR is close, decide by max rate or frequency band */
1739 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1740 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1741 if (wa->est_throughput != wb->est_throughput)
1742 return wb->est_throughput - wa->est_throughput;
1743 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1744 return IS_5GHZ(wa->freq) ? -1 : 1;
1747 /* all things being equal, use SNR; if SNRs are
1748 * identical, use quality values since some drivers may only report
1749 * that value and leave the signal level zero */
1750 if (snr_b_full == snr_a_full)
1751 return wb->qual - wa->qual;
1752 return snr_b_full - snr_a_full;
1758 /* Compare function for sorting scan results when searching a WPS AP for
1759 * provisioning. Return >0 if @b is considered better. */
1760 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1762 struct wpa_scan_res **_wa = (void *) a;
1763 struct wpa_scan_res **_wb = (void *) b;
1764 struct wpa_scan_res *wa = *_wa;
1765 struct wpa_scan_res *wb = *_wb;
1766 int uses_wps_a, uses_wps_b;
1767 struct wpabuf *wps_a, *wps_b;
1770 /* Optimization - check WPS IE existence before allocated memory and
1771 * doing full reassembly. */
1772 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1773 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1774 if (uses_wps_a && !uses_wps_b)
1776 if (!uses_wps_a && uses_wps_b)
1779 if (uses_wps_a && uses_wps_b) {
1780 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1781 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1782 res = wps_ap_priority_compar(wps_a, wps_b);
1790 * Do not use current AP security policy as a sorting criteria during
1791 * WPS provisioning step since the AP may get reconfigured at the
1792 * completion of provisioning.
1795 /* all things being equal, use signal level; if signal levels are
1796 * identical, use quality values since some drivers may only report
1797 * that value and leave the signal level zero */
1798 if (wb->level == wa->level)
1799 return wb->qual - wa->qual;
1800 return wb->level - wa->level;
1802 #endif /* CONFIG_WPS */
1805 static void dump_scan_res(struct wpa_scan_results *scan_res)
1807 #ifndef CONFIG_NO_STDOUT_DEBUG
1810 if (scan_res->res == NULL || scan_res->num == 0)
1813 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1815 for (i = 0; i < scan_res->num; i++) {
1816 struct wpa_scan_res *r = scan_res->res[i];
1818 if (r->flags & WPA_SCAN_LEVEL_DBM) {
1819 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1821 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1822 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
1823 MAC2STR(r->bssid), r->freq, r->qual,
1824 r->noise, noise_valid ? "" : "~", r->level,
1825 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1827 r->age, r->est_throughput);
1829 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1830 "noise=%d level=%d flags=0x%x age=%u est=%u",
1831 MAC2STR(r->bssid), r->freq, r->qual,
1832 r->noise, r->level, r->flags, r->age,
1835 pos = (u8 *) (r + 1);
1837 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1839 if (r->beacon_ie_len)
1840 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1841 pos, r->beacon_ie_len);
1843 #endif /* CONFIG_NO_STDOUT_DEBUG */
1848 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1849 * @wpa_s: Pointer to wpa_supplicant data
1850 * @bssid: BSSID to check
1851 * Returns: 0 if the BSSID is filtered or 1 if not
1853 * This function is used to filter out specific BSSIDs from scan reslts mainly
1854 * for testing purposes (SET bssid_filter ctrl_iface command).
1856 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1861 if (wpa_s->bssid_filter == NULL)
1864 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1865 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1874 void filter_scan_res(struct wpa_supplicant *wpa_s,
1875 struct wpa_scan_results *res)
1879 if (wpa_s->bssid_filter == NULL)
1882 for (i = 0, j = 0; i < res->num; i++) {
1883 if (wpa_supplicant_filter_bssid_match(wpa_s,
1884 res->res[i]->bssid)) {
1885 res->res[j++] = res->res[i];
1887 os_free(res->res[i]);
1892 if (res->num != j) {
1893 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1894 (int) (res->num - j));
1901 * Noise floor values to use when we have signal strength
1902 * measurements, but no noise floor measurements. These values were
1903 * measured in an office environment with many APs.
1905 #define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1906 #define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1908 void scan_snr(struct wpa_scan_res *res)
1910 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1911 res->noise = IS_5GHZ(res->freq) ?
1912 DEFAULT_NOISE_FLOOR_5GHZ :
1913 DEFAULT_NOISE_FLOOR_2GHZ;
1916 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1917 res->snr = res->level - res->noise;
1919 /* Level is not in dBm, so we can't calculate
1920 * SNR. Just use raw level (units unknown). */
1921 res->snr = res->level;
1926 static unsigned int max_ht20_rate(int snr)
1929 return 6500; /* HT20 MCS0 */
1931 return 13000; /* HT20 MCS1 */
1933 return 19500; /* HT20 MCS2 */
1935 return 26000; /* HT20 MCS3 */
1937 return 39000; /* HT20 MCS4 */
1939 return 52000; /* HT20 MCS5 */
1941 return 58500; /* HT20 MCS6 */
1942 return 65000; /* HT20 MCS7 */
1946 static unsigned int max_ht40_rate(int snr)
1949 return 13500; /* HT40 MCS0 */
1951 return 27000; /* HT40 MCS1 */
1953 return 40500; /* HT40 MCS2 */
1955 return 54000; /* HT40 MCS3 */
1957 return 81000; /* HT40 MCS4 */
1959 return 108000; /* HT40 MCS5 */
1961 return 121500; /* HT40 MCS6 */
1962 return 135000; /* HT40 MCS7 */
1966 static unsigned int max_vht80_rate(int snr)
1971 return 29300; /* VHT80 MCS0 */
1973 return 58500; /* VHT80 MCS1 */
1975 return 87800; /* VHT80 MCS2 */
1977 return 117000; /* VHT80 MCS3 */
1979 return 175500; /* VHT80 MCS4 */
1981 return 234000; /* VHT80 MCS5 */
1983 return 263300; /* VHT80 MCS6 */
1985 return 292500; /* VHT80 MCS7 */
1987 return 351000; /* VHT80 MCS8 */
1988 return 390000; /* VHT80 MCS9 */
1992 void scan_est_throughput(struct wpa_supplicant *wpa_s,
1993 struct wpa_scan_res *res)
1995 enum local_hw_capab capab = wpa_s->hw_capab;
1996 int rate; /* max legacy rate in 500 kb/s units */
1998 unsigned int est, tmp;
2001 if (res->est_throughput)
2004 /* Get maximum legacy rate */
2005 rate = wpa_scan_get_max_rate(res);
2007 /* Limit based on estimated SNR */
2008 if (rate > 1 * 2 && snr < 1)
2010 else if (rate > 2 * 2 && snr < 4)
2012 else if (rate > 6 * 2 && snr < 5)
2014 else if (rate > 9 * 2 && snr < 6)
2016 else if (rate > 12 * 2 && snr < 7)
2018 else if (rate > 18 * 2 && snr < 10)
2020 else if (rate > 24 * 2 && snr < 11)
2022 else if (rate > 36 * 2 && snr < 15)
2024 else if (rate > 48 * 2 && snr < 19)
2026 else if (rate > 54 * 2 && snr < 21)
2030 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2031 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2033 tmp = max_ht20_rate(snr);
2039 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2040 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2041 if (ie && ie[1] >= 2 &&
2042 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2043 tmp = max_ht40_rate(snr);
2049 if (capab == CAPAB_VHT) {
2050 /* Use +1 to assume VHT is always faster than HT */
2051 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2053 tmp = max_ht20_rate(snr) + 1;
2057 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2058 if (ie && ie[1] >= 2 &&
2060 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2061 tmp = max_ht40_rate(snr) + 1;
2066 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2067 if (ie && ie[1] >= 1 &&
2068 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2069 tmp = max_vht80_rate(snr) + 1;
2076 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2078 res->est_throughput = est;
2083 * wpa_supplicant_get_scan_results - Get scan results
2084 * @wpa_s: Pointer to wpa_supplicant data
2085 * @info: Information about what was scanned or %NULL if not available
2086 * @new_scan: Whether a new scan was performed
2087 * Returns: Scan results, %NULL on failure
2089 * This function request the current scan results from the driver and updates
2090 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2091 * results with wpa_scan_results_free().
2093 struct wpa_scan_results *
2094 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2095 struct scan_info *info, int new_scan)
2097 struct wpa_scan_results *scan_res;
2099 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2101 scan_res = wpa_drv_get_scan_results2(wpa_s);
2102 if (scan_res == NULL) {
2103 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2106 if (scan_res->fetch_time.sec == 0) {
2108 * Make sure we have a valid timestamp if the driver wrapper
2109 * does not set this.
2111 os_get_reltime(&scan_res->fetch_time);
2113 filter_scan_res(wpa_s, scan_res);
2115 for (i = 0; i < scan_res->num; i++) {
2116 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2118 scan_snr(scan_res_item);
2119 scan_est_throughput(wpa_s, scan_res_item);
2123 if (wpas_wps_searching(wpa_s)) {
2124 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2125 "provisioning rules");
2126 compar = wpa_scan_result_wps_compar;
2128 #endif /* CONFIG_WPS */
2130 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
2132 dump_scan_res(scan_res);
2134 wpa_bss_update_start(wpa_s);
2135 for (i = 0; i < scan_res->num; i++)
2136 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2137 &scan_res->fetch_time);
2138 wpa_bss_update_end(wpa_s, info, new_scan);
2145 * wpa_supplicant_update_scan_results - Update scan results from the driver
2146 * @wpa_s: Pointer to wpa_supplicant data
2147 * Returns: 0 on success, -1 on failure
2149 * This function updates the BSS table within wpa_supplicant based on the
2150 * currently available scan results from the driver without requesting a new
2151 * scan. This is used in cases where the driver indicates an association
2152 * (including roaming within ESS) and wpa_supplicant does not yet have the
2153 * needed information to complete the connection (e.g., to perform validation
2154 * steps in 4-way handshake).
2156 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2158 struct wpa_scan_results *scan_res;
2159 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2160 if (scan_res == NULL)
2162 wpa_scan_results_free(scan_res);
2169 * scan_only_handler - Reports scan results
2171 void scan_only_handler(struct wpa_supplicant *wpa_s,
2172 struct wpa_scan_results *scan_res)
2174 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
2175 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2176 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2177 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2178 wpa_s->manual_scan_id);
2179 wpa_s->manual_scan_use_id = 0;
2181 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2183 wpas_notify_scan_results(wpa_s);
2184 wpas_notify_scan_done(wpa_s, 1);
2185 if (wpa_s->scan_work) {
2186 struct wpa_radio_work *work = wpa_s->scan_work;
2187 wpa_s->scan_work = NULL;
2188 radio_work_done(work);
2191 if (wpa_s->wpa_state == WPA_SCANNING)
2192 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
2196 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2198 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2202 struct wpa_driver_scan_params *
2203 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2205 struct wpa_driver_scan_params *params;
2209 params = os_zalloc(sizeof(*params));
2213 for (i = 0; i < src->num_ssids; i++) {
2214 if (src->ssids[i].ssid) {
2215 n = os_malloc(src->ssids[i].ssid_len);
2218 os_memcpy(n, src->ssids[i].ssid,
2219 src->ssids[i].ssid_len);
2220 params->ssids[i].ssid = n;
2221 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2224 params->num_ssids = src->num_ssids;
2226 if (src->extra_ies) {
2227 n = os_malloc(src->extra_ies_len);
2230 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2231 params->extra_ies = n;
2232 params->extra_ies_len = src->extra_ies_len;
2236 int len = int_array_len(src->freqs);
2237 params->freqs = os_malloc((len + 1) * sizeof(int));
2238 if (params->freqs == NULL)
2240 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2243 if (src->filter_ssids) {
2244 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
2245 src->num_filter_ssids);
2246 if (params->filter_ssids == NULL)
2248 os_memcpy(params->filter_ssids, src->filter_ssids,
2249 sizeof(*params->filter_ssids) *
2250 src->num_filter_ssids);
2251 params->num_filter_ssids = src->num_filter_ssids;
2254 params->filter_rssi = src->filter_rssi;
2255 params->p2p_probe = src->p2p_probe;
2256 params->only_new_results = src->only_new_results;
2257 params->low_priority = src->low_priority;
2259 if (src->sched_scan_plans_num > 0) {
2260 params->sched_scan_plans =
2261 os_malloc(sizeof(*src->sched_scan_plans) *
2262 src->sched_scan_plans_num);
2263 if (!params->sched_scan_plans)
2266 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2267 sizeof(*src->sched_scan_plans) *
2268 src->sched_scan_plans_num);
2269 params->sched_scan_plans_num = src->sched_scan_plans_num;
2272 if (src->mac_addr_rand) {
2273 params->mac_addr_rand = src->mac_addr_rand;
2275 if (src->mac_addr && src->mac_addr_mask) {
2278 mac_addr = os_malloc(2 * ETH_ALEN);
2282 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2283 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2285 params->mac_addr = mac_addr;
2286 params->mac_addr_mask = mac_addr + ETH_ALEN;
2293 bssid = os_malloc(ETH_ALEN);
2296 os_memcpy(bssid, src->bssid, ETH_ALEN);
2297 params->bssid = bssid;
2303 wpa_scan_free_params(params);
2308 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2315 for (i = 0; i < params->num_ssids; i++)
2316 os_free((u8 *) params->ssids[i].ssid);
2317 os_free((u8 *) params->extra_ies);
2318 os_free(params->freqs);
2319 os_free(params->filter_ssids);
2320 os_free(params->sched_scan_plans);
2323 * Note: params->mac_addr_mask points to same memory allocation and
2324 * must not be freed separately.
2326 os_free((u8 *) params->mac_addr);
2328 os_free((u8 *) params->bssid);
2334 int wpas_start_pno(struct wpa_supplicant *wpa_s)
2337 size_t i, num_ssid, num_match_ssid;
2338 struct wpa_ssid *ssid;
2339 struct wpa_driver_scan_params params;
2340 struct sched_scan_plan scan_plan;
2341 unsigned int max_sched_scan_ssids;
2343 if (!wpa_s->sched_scan_supported)
2346 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2347 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2349 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2350 if (max_sched_scan_ssids < 1)
2353 if (wpa_s->pno || wpa_s->pno_sched_pending)
2356 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2357 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2358 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2362 if (wpa_s->wpa_state == WPA_SCANNING) {
2363 wpa_supplicant_cancel_scan(wpa_s);
2364 if (wpa_s->sched_scanning) {
2365 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2366 "ongoing sched scan");
2367 wpa_supplicant_cancel_sched_scan(wpa_s);
2368 wpa_s->pno_sched_pending = 1;
2373 os_memset(¶ms, 0, sizeof(params));
2375 num_ssid = num_match_ssid = 0;
2376 ssid = wpa_s->conf->ssid;
2378 if (!wpas_network_disabled(wpa_s, ssid)) {
2380 if (ssid->scan_ssid)
2386 if (num_match_ssid == 0) {
2387 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2391 if (num_match_ssid > num_ssid) {
2392 params.num_ssids++; /* wildcard */
2396 if (num_ssid > max_sched_scan_ssids) {
2397 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2398 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2399 num_ssid = max_sched_scan_ssids;
2402 if (num_match_ssid > wpa_s->max_match_sets) {
2403 num_match_ssid = wpa_s->max_match_sets;
2404 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
2406 params.filter_ssids = os_calloc(num_match_ssid,
2407 sizeof(struct wpa_driver_scan_filter));
2408 if (params.filter_ssids == NULL)
2413 ssid = wpa_s->conf->pssid[prio];
2415 if (!wpas_network_disabled(wpa_s, ssid)) {
2416 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2417 params.ssids[params.num_ssids].ssid =
2419 params.ssids[params.num_ssids].ssid_len =
2423 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2425 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2426 params.num_filter_ssids++;
2428 if (i == num_match_ssid)
2433 else if (prio + 1 == wpa_s->conf->num_prio)
2436 ssid = wpa_s->conf->pssid[++prio];
2439 if (wpa_s->conf->filter_rssi)
2440 params.filter_rssi = wpa_s->conf->filter_rssi;
2442 if (wpa_s->sched_scan_plans_num) {
2443 params.sched_scan_plans = wpa_s->sched_scan_plans;
2444 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2446 /* Set one scan plan that will run infinitely */
2447 if (wpa_s->conf->sched_scan_interval)
2448 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2450 scan_plan.interval = 10;
2452 scan_plan.iterations = 0;
2453 params.sched_scan_plans = &scan_plan;
2454 params.sched_scan_plans_num = 1;
2457 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2458 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2459 params.freqs = wpa_s->manual_sched_scan_freqs;
2462 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2463 params.mac_addr_rand = 1;
2464 if (wpa_s->mac_addr_pno) {
2465 params.mac_addr = wpa_s->mac_addr_pno;
2466 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2470 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms);
2471 os_free(params.filter_ssids);
2475 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2480 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2487 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2490 wpa_s->pno_sched_pending = 0;
2492 if (wpa_s->wpa_state == WPA_SCANNING)
2493 wpa_supplicant_req_scan(wpa_s, 0, 0);
2499 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2502 type &= MAC_ADDR_RAND_ALL;
2503 wpa_s->mac_addr_rand_enable &= ~type;
2505 if (type & MAC_ADDR_RAND_SCAN) {
2506 os_free(wpa_s->mac_addr_scan);
2507 wpa_s->mac_addr_scan = NULL;
2510 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2511 os_free(wpa_s->mac_addr_sched_scan);
2512 wpa_s->mac_addr_sched_scan = NULL;
2515 if (type & MAC_ADDR_RAND_PNO) {
2516 os_free(wpa_s->mac_addr_pno);
2517 wpa_s->mac_addr_pno = NULL;
2522 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2523 unsigned int type, const u8 *addr,
2528 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2531 tmp = os_malloc(2 * ETH_ALEN);
2534 os_memcpy(tmp, addr, ETH_ALEN);
2535 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2538 if (type == MAC_ADDR_RAND_SCAN) {
2539 wpa_s->mac_addr_scan = tmp;
2540 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2541 wpa_s->mac_addr_sched_scan = tmp;
2542 } else if (type == MAC_ADDR_RAND_PNO) {
2543 wpa_s->mac_addr_pno = tmp;
2545 wpa_printf(MSG_INFO,
2546 "scan: Invalid MAC randomization type=0x%x",
2552 wpa_s->mac_addr_rand_enable |= type;
2557 int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2559 int scan_work = !!wpa_s->scan_work;
2562 scan_work |= !!wpa_s->p2p_scan_work;
2563 #endif /* CONFIG_P2P */
2565 if (scan_work && wpa_s->own_scan_running) {
2566 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2567 return wpa_drv_abort_scan(wpa_s);
2574 int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2576 struct sched_scan_plan *scan_plans = NULL;
2577 const char *token, *context = NULL;
2578 unsigned int num = 0;
2584 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2585 os_free(wpa_s->sched_scan_plans);
2586 wpa_s->sched_scan_plans = NULL;
2587 wpa_s->sched_scan_plans_num = 0;
2591 while ((token = cstr_token(cmd, " ", &context))) {
2593 struct sched_scan_plan *scan_plan, *n;
2595 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2600 scan_plan = &scan_plans[num];
2603 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2604 &scan_plan->iterations);
2605 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2606 wpa_printf(MSG_ERROR,
2607 "Invalid sched scan plan input: %s", token);
2611 if (!scan_plan->interval) {
2612 wpa_printf(MSG_ERROR,
2613 "scan plan %u: Interval cannot be zero",
2618 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2619 wpa_printf(MSG_WARNING,
2620 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2621 num, scan_plan->interval,
2622 wpa_s->max_sched_scan_plan_interval);
2623 scan_plan->interval =
2624 wpa_s->max_sched_scan_plan_interval;
2628 scan_plan->iterations = 0;
2632 if (!scan_plan->iterations) {
2633 wpa_printf(MSG_ERROR,
2634 "scan plan %u: Number of iterations cannot be zero",
2639 if (scan_plan->iterations >
2640 wpa_s->max_sched_scan_plan_iterations) {
2641 wpa_printf(MSG_WARNING,
2642 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2643 num, scan_plan->iterations,
2644 wpa_s->max_sched_scan_plan_iterations);
2645 scan_plan->iterations =
2646 wpa_s->max_sched_scan_plan_iterations;
2649 wpa_printf(MSG_DEBUG,
2650 "scan plan %u: interval=%u iterations=%u",
2651 num, scan_plan->interval, scan_plan->iterations);
2655 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2659 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2660 wpa_printf(MSG_ERROR,
2661 "All scan plans but the last must specify a number of iterations");
2665 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2666 num, scan_plans[num - 1].interval);
2668 if (num > wpa_s->max_sched_scan_plans) {
2669 wpa_printf(MSG_WARNING,
2670 "Too many scheduled scan plans (only %u supported)",
2671 wpa_s->max_sched_scan_plans);
2672 wpa_printf(MSG_WARNING,
2673 "Use only the first %u scan plans, and the last one (in infinite loop)",
2674 wpa_s->max_sched_scan_plans - 1);
2675 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2676 &scan_plans[num - 1], sizeof(*scan_plans));
2677 num = wpa_s->max_sched_scan_plans;
2680 os_free(wpa_s->sched_scan_plans);
2681 wpa_s->sched_scan_plans = scan_plans;
2682 wpa_s->sched_scan_plans_num = num;
2687 os_free(scan_plans);
2688 wpa_printf(MSG_ERROR, "invalid scan plans list");