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)
65 if (os_strstr(ssid->eap.phase1, "pbc=1"))
70 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
71 !wpa_s->conf->p2p_disabled) {
72 wpa_s->wps->dev.p2p = 1;
75 *req_type = WPS_REQ_ENROLLEE_INFO;
78 #endif /* CONFIG_P2P */
82 #endif /* CONFIG_WPS */
86 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
87 * @wpa_s: Pointer to wpa_supplicant data
88 * Returns: 0 if no networks are enabled, >0 if networks are enabled
90 * This function is used to figure out whether any networks (or Interworking
91 * with enabled credentials and auto_interworking) are present in the current
94 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
96 struct wpa_ssid *ssid = wpa_s->conf->ssid;
97 int count = 0, disabled = 0;
100 return 0; /* no normal network profiles on p2p_mgmt interface */
103 if (!wpas_network_disabled(wpa_s, ssid))
109 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
110 wpa_s->conf->auto_interworking)
112 if (count == 0 && disabled > 0) {
113 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
114 "networks)", disabled);
120 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
121 struct wpa_ssid *ssid)
124 if (!wpas_network_disabled(wpa_s, ssid))
129 /* ap_scan=2 mode - try to associate with each SSID. */
131 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
132 "end of scan list - go back to beginning");
133 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
134 wpa_supplicant_req_scan(wpa_s, 0, 0);
138 /* Continue from the next SSID on the next attempt. */
139 wpa_s->prev_scan_ssid = ssid;
141 /* Start from the beginning of the SSID list. */
142 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
144 wpa_supplicant_associate(wpa_s, NULL, ssid);
148 static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
150 struct wpa_supplicant *wpa_s = work->wpa_s;
151 struct wpa_driver_scan_params *params = work->ctx;
155 if (!work->started) {
156 wpa_scan_free_params(params);
159 wpa_supplicant_notify_scanning(wpa_s, 0);
160 wpas_notify_scan_done(wpa_s, 0);
161 wpa_s->scan_work = NULL;
165 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
166 wpa_msg(wpa_s, MSG_INFO,
167 "Failed to assign random MAC address for a scan");
168 radio_work_done(work);
172 wpa_supplicant_notify_scanning(wpa_s, 1);
174 if (wpa_s->clear_driver_scan_cache) {
175 wpa_printf(MSG_DEBUG,
176 "Request driver to clear scan cache due to local BSS flush");
177 params->only_new_results = 1;
179 ret = wpa_drv_scan(wpa_s, params);
180 wpa_scan_free_params(params);
183 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
185 if (wpa_s->disconnected)
188 wpa_supplicant_notify_scanning(wpa_s, 0);
189 wpas_notify_scan_done(wpa_s, 0);
190 if (wpa_s->wpa_state == WPA_SCANNING)
191 wpa_supplicant_set_state(wpa_s,
192 wpa_s->scan_prev_wpa_state);
193 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
194 ret, retry ? " retry=1" : "");
195 radio_work_done(work);
198 /* Restore scan_req since we will try to scan again */
199 wpa_s->scan_req = wpa_s->last_scan_req;
200 wpa_supplicant_req_scan(wpa_s, 1, 0);
205 os_get_reltime(&wpa_s->scan_trigger_time);
207 wpa_s->normal_scans++;
208 wpa_s->own_scan_requested = 1;
209 wpa_s->clear_driver_scan_cache = 0;
210 wpa_s->scan_work = work;
215 * wpa_supplicant_trigger_scan - Request driver to start a scan
216 * @wpa_s: Pointer to wpa_supplicant data
217 * @params: Scan parameters
218 * Returns: 0 on success, -1 on failure
220 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
221 struct wpa_driver_scan_params *params)
223 struct wpa_driver_scan_params *ctx;
225 if (wpa_s->scan_work) {
226 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
230 ctx = wpa_scan_clone_params(params);
234 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
236 wpa_scan_free_params(ctx);
245 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
247 struct wpa_supplicant *wpa_s = eloop_ctx;
249 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
251 if (wpa_supplicant_req_sched_scan(wpa_s))
252 wpa_supplicant_req_scan(wpa_s, 0, 0);
257 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
259 struct wpa_supplicant *wpa_s = eloop_ctx;
261 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
263 wpa_s->sched_scan_timed_out = 1;
264 wpa_supplicant_cancel_sched_scan(wpa_s);
269 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
270 struct wpa_driver_scan_params *params)
274 wpa_supplicant_notify_scanning(wpa_s, 1);
275 ret = wpa_drv_sched_scan(wpa_s, params);
277 wpa_supplicant_notify_scanning(wpa_s, 0);
279 wpa_s->sched_scanning = 1;
285 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
289 ret = wpa_drv_stop_sched_scan(wpa_s);
291 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
292 /* TODO: what to do if stopping fails? */
300 static struct wpa_driver_scan_filter *
301 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
303 struct wpa_driver_scan_filter *ssids;
304 struct wpa_ssid *ssid;
308 if (!conf->filter_ssids)
311 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
312 if (ssid->ssid && ssid->ssid_len)
317 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
321 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
322 if (!ssid->ssid || !ssid->ssid_len)
324 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
325 ssids[*num_ssids].ssid_len = ssid->ssid_len;
333 static void wpa_supplicant_optimize_freqs(
334 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
337 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
339 /* Optimize provisioning state scan based on GO information */
340 if (wpa_s->p2p_in_provisioning < 5 &&
341 wpa_s->go_params->freq > 0) {
342 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
343 "preferred frequency %d MHz",
344 wpa_s->go_params->freq);
345 params->freqs = os_calloc(2, sizeof(int));
347 params->freqs[0] = wpa_s->go_params->freq;
348 } else if (wpa_s->p2p_in_provisioning < 8 &&
349 wpa_s->go_params->freq_list[0]) {
350 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
352 int_array_concat(¶ms->freqs,
353 wpa_s->go_params->freq_list);
355 int_array_sort_unique(params->freqs);
357 wpa_s->p2p_in_provisioning++;
360 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
362 * Optimize scan based on GO information during persistent
365 if (wpa_s->p2p_in_invitation < 5 &&
366 wpa_s->p2p_invite_go_freq > 0) {
367 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
368 wpa_s->p2p_invite_go_freq);
369 params->freqs = os_calloc(2, sizeof(int));
371 params->freqs[0] = wpa_s->p2p_invite_go_freq;
373 wpa_s->p2p_in_invitation++;
374 if (wpa_s->p2p_in_invitation > 20) {
376 * This should not really happen since the variable is
377 * cleared on group removal, but if it does happen, make
378 * sure we do not get stuck in special invitation scan
381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
382 wpa_s->p2p_in_invitation = 0;
385 #endif /* CONFIG_P2P */
388 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
390 * Optimize post-provisioning scan based on channel used
391 * during provisioning.
393 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
394 "that was used during provisioning", wpa_s->wps_freq);
395 params->freqs = os_calloc(2, sizeof(int));
397 params->freqs[0] = wpa_s->wps_freq;
399 } else if (wpa_s->after_wps)
402 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
404 /* Optimize provisioning scan based on already known channel */
405 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
407 params->freqs = os_calloc(2, sizeof(int));
409 params->freqs[0] = wpa_s->wps_freq;
410 wpa_s->known_wps_freq = 0; /* only do this once */
412 #endif /* CONFIG_WPS */
416 #ifdef CONFIG_INTERWORKING
417 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
420 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
421 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
423 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
425 if (!is_zero_ether_addr(wpa_s->conf->hessid))
426 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
428 #endif /* CONFIG_INTERWORKING */
431 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
433 struct wpabuf *extra_ie = NULL;
438 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
439 #endif /* CONFIG_WPS */
441 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
443 if (ext_capab_len > 0 &&
444 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
445 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
447 #ifdef CONFIG_INTERWORKING
448 if (wpa_s->conf->interworking &&
449 wpabuf_resize(&extra_ie, 100) == 0)
450 wpas_add_interworking_elements(wpa_s, extra_ie);
451 #endif /* CONFIG_INTERWORKING */
454 wps = wpas_wps_in_use(wpa_s, &req_type);
457 struct wpabuf *wps_ie;
458 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
461 wpa_s->wps->uuid, req_type,
464 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
465 wpabuf_put_buf(extra_ie, wps_ie);
472 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
473 if (wpabuf_resize(&extra_ie, ielen) == 0)
474 wpas_p2p_scan_ie(wpa_s, extra_ie);
476 #endif /* CONFIG_P2P */
478 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
480 #endif /* CONFIG_WPS */
483 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
484 wpas_hs20_add_indication(extra_ie, -1);
485 #endif /* CONFIG_HS20 */
488 if (wpa_s->fst_ies &&
489 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
490 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
491 #endif /* CONFIG_FST */
494 /* Send cellular capabilities for potential MBO STAs */
495 if (wpabuf_resize(&extra_ie, 9) == 0)
496 wpas_mbo_scan_ie(wpa_s, extra_ie);
497 #endif /* CONFIG_MBO */
499 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
500 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
502 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
503 wpabuf_put_buf(extra_ie, buf);
513 * Check whether there are any enabled networks or credentials that could be
514 * used for a non-P2P connection.
516 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
518 struct wpa_ssid *ssid;
520 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
521 if (wpas_network_disabled(wpa_s, ssid))
523 if (!ssid->p2p_group)
527 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
528 wpa_s->conf->auto_interworking)
534 #endif /* CONFIG_P2P */
537 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
538 enum hostapd_hw_mode band,
539 struct wpa_driver_scan_params *params)
541 /* Include only supported channels for the specified band */
542 struct hostapd_hw_modes *mode;
545 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
547 /* No channels supported in this band - use empty list */
548 params->freqs = os_zalloc(sizeof(int));
552 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
553 if (params->freqs == NULL)
555 for (count = 0, i = 0; i < mode->num_channels; i++) {
556 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
558 params->freqs[count++] = mode->channels[i].freq;
563 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
564 struct wpa_driver_scan_params *params)
566 if (wpa_s->hw.modes == NULL)
567 return; /* unknown what channels the driver supports */
569 return; /* already using a limited channel set */
570 if (wpa_s->setband == WPA_SETBAND_5G)
571 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
573 else if (wpa_s->setband == WPA_SETBAND_2G)
574 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
579 static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
580 struct wpa_driver_scan_params *params,
584 struct wpa_ssid *ssid;
586 for (i = 0; i < wpa_s->scan_id_count; i++) {
589 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
590 if (!ssid || !ssid->scan_ssid)
593 for (j = 0; j < params->num_ssids; j++) {
594 if (params->ssids[j].ssid_len == ssid->ssid_len &&
595 params->ssids[j].ssid &&
596 os_memcmp(params->ssids[j].ssid, ssid->ssid,
597 ssid->ssid_len) == 0)
600 if (j < params->num_ssids)
601 continue; /* already in the list */
603 if (params->num_ssids + 1 > max_ssids) {
604 wpa_printf(MSG_DEBUG,
605 "Over max scan SSIDs for manual request");
609 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
610 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
611 params->ssids[params->num_ssids].ssid = ssid->ssid;
612 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
616 wpa_s->scan_id_count = 0;
620 static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
621 struct wpa_driver_scan_params *params,
626 if (wpa_s->ssids_from_scan_req == NULL ||
627 wpa_s->num_ssids_from_scan_req == 0)
630 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
631 wpa_s->num_ssids_from_scan_req = max_ssids;
632 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
633 (unsigned int) max_ssids);
636 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
637 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
638 params->ssids[i].ssid_len =
639 wpa_s->ssids_from_scan_req[i].ssid_len;
640 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
641 params->ssids[i].ssid,
642 params->ssids[i].ssid_len);
645 params->num_ssids = wpa_s->num_ssids_from_scan_req;
646 wpa_s->num_ssids_from_scan_req = 0;
651 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
653 struct wpa_supplicant *wpa_s = eloop_ctx;
654 struct wpa_ssid *ssid;
655 int ret, p2p_in_prog;
656 struct wpabuf *extra_ie = NULL;
657 struct wpa_driver_scan_params params;
658 struct wpa_driver_scan_params *scan_params;
660 int connect_without_scan = 0;
662 if (wpa_s->pno || wpa_s->pno_sched_pending) {
663 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
667 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
668 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
672 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
673 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
674 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
678 if (wpa_s->scanning) {
680 * If we are already in scanning state, we shall reschedule the
681 * the incoming scan request.
683 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
684 wpa_supplicant_req_scan(wpa_s, 1, 0);
688 if (!wpa_supplicant_enabled_networks(wpa_s) &&
689 wpa_s->scan_req == NORMAL_SCAN_REQ) {
690 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
691 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
695 if (wpa_s->conf->ap_scan != 0 &&
696 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
697 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
698 "overriding ap_scan configuration");
699 wpa_s->conf->ap_scan = 0;
700 wpas_notify_ap_scan_changed(wpa_s);
703 if (wpa_s->conf->ap_scan == 0) {
704 wpa_supplicant_gen_assoc_event(wpa_s);
709 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
710 wpa_s->connect_without_scan) {
711 connect_without_scan = 1;
712 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
713 if (ssid == wpa_s->connect_without_scan)
718 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
719 if (p2p_in_prog && p2p_in_prog != 2 &&
721 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
722 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
723 wpa_supplicant_req_scan(wpa_s, 5, 0);
727 if (wpa_s->conf->ap_scan == 2)
730 max_ssids = wpa_s->max_scan_ssids;
731 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
732 max_ssids = WPAS_MAX_SCAN_SSIDS;
735 wpa_s->last_scan_req = wpa_s->scan_req;
736 wpa_s->scan_req = NORMAL_SCAN_REQ;
738 if (connect_without_scan) {
739 wpa_s->connect_without_scan = NULL;
741 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
742 "without scan step");
743 wpa_supplicant_associate(wpa_s, NULL, ssid);
748 os_memset(¶ms, 0, sizeof(params));
750 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
751 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
752 wpa_s->wpa_state == WPA_INACTIVE)
753 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
756 * If autoscan has set its own scanning parameters
758 if (wpa_s->autoscan_params != NULL) {
759 scan_params = wpa_s->autoscan_params;
763 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
764 wpa_set_ssids_from_scan_req(wpa_s, ¶ms, max_ssids)) {
765 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
770 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
771 wpa_s->go_params && !wpa_s->conf->passive_scan) {
772 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
773 wpa_s->p2p_in_provisioning,
774 wpa_s->show_group_started);
775 params.ssids[0].ssid = wpa_s->go_params->ssid;
776 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
777 params.num_ssids = 1;
781 if (wpa_s->p2p_in_invitation) {
782 if (wpa_s->current_ssid) {
783 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
784 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
785 params.ssids[0].ssid_len =
786 wpa_s->current_ssid->ssid_len;
787 params.num_ssids = 1;
789 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
793 #endif /* CONFIG_P2P */
795 /* Find the starting point from which to continue scanning */
796 ssid = wpa_s->conf->ssid;
797 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
799 if (ssid == wpa_s->prev_scan_ssid) {
807 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
810 #endif /* CONFIG_AP */
811 wpa_s->conf->ap_scan == 2) {
812 wpa_s->connect_without_scan = NULL;
813 wpa_s->prev_scan_wildcard = 0;
814 wpa_supplicant_assoc_try(wpa_s, ssid);
816 } else if (wpa_s->conf->ap_scan == 2) {
818 * User-initiated scan request in ap_scan == 2; scan with
822 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
824 * Perform single-channel single-SSID scan for
825 * reassociate-to-same-BSS operation.
828 ssid = wpa_s->current_ssid;
829 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
830 ssid->ssid, ssid->ssid_len);
831 params.ssids[0].ssid = ssid->ssid;
832 params.ssids[0].ssid_len = ssid->ssid_len;
833 params.num_ssids = 1;
836 * Allocate memory for frequency array, allocate one extra
837 * slot for the zero-terminator.
839 params.freqs = os_malloc(sizeof(int) * 2);
840 if (params.freqs == NULL) {
841 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
844 params.freqs[0] = wpa_s->assoc_freq;
848 * Reset the reattach flag so that we fall back to full scan if
853 struct wpa_ssid *start = ssid, *tssid;
855 if (ssid == NULL && max_ssids > 1)
856 ssid = wpa_s->conf->ssid;
858 if (!wpas_network_disabled(wpa_s, ssid) &&
860 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
861 ssid->ssid, ssid->ssid_len);
862 params.ssids[params.num_ssids].ssid =
864 params.ssids[params.num_ssids].ssid_len =
867 if (params.num_ssids + 1 >= max_ssids)
873 if (ssid == NULL && max_ssids > 1 &&
874 start != wpa_s->conf->ssid)
875 ssid = wpa_s->conf->ssid;
878 if (wpa_s->scan_id_count &&
879 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
880 wpa_set_scan_ssids(wpa_s, ¶ms, max_ssids);
882 for (tssid = wpa_s->conf->ssid;
883 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
884 tssid = tssid->next) {
885 if (wpas_network_disabled(wpa_s, tssid))
887 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
888 int_array_concat(¶ms.freqs,
891 os_free(params.freqs);
896 int_array_sort_unique(params.freqs);
899 if (ssid && max_ssids == 1) {
901 * If the driver is limited to 1 SSID at a time interleave
902 * wildcard SSID scans with specific SSID scans to avoid
903 * waiting a long time for a wildcard scan.
905 if (!wpa_s->prev_scan_wildcard) {
906 params.ssids[0].ssid = NULL;
907 params.ssids[0].ssid_len = 0;
908 wpa_s->prev_scan_wildcard = 1;
909 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
910 "wildcard SSID (Interleave with specific)");
912 wpa_s->prev_scan_ssid = ssid;
913 wpa_s->prev_scan_wildcard = 0;
914 wpa_dbg(wpa_s, MSG_DEBUG,
915 "Starting AP scan for specific SSID: %s",
916 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
921 wpa_s->prev_scan_ssid = ssid;
922 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
925 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
926 wpa_s->manual_scan_passive && params.num_ssids == 0) {
927 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
928 } else if (wpa_s->conf->passive_scan) {
929 wpa_dbg(wpa_s, MSG_DEBUG,
930 "Use passive scan based on configuration");
932 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
934 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
939 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
940 extra_ie = wpa_supplicant_extra_ies(wpa_s);
942 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
943 wpa_s->manual_scan_only_new) {
944 wpa_printf(MSG_DEBUG,
945 "Request driver to clear scan cache due to manual only_new=1 scan");
946 params.only_new_results = 1;
949 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
950 wpa_s->manual_scan_freqs) {
951 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
952 params.freqs = wpa_s->manual_scan_freqs;
953 wpa_s->manual_scan_freqs = NULL;
956 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
957 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
958 "generated frequency list");
959 params.freqs = wpa_s->next_scan_freqs;
961 os_free(wpa_s->next_scan_freqs);
962 wpa_s->next_scan_freqs = NULL;
963 wpa_setband_scan_freqs(wpa_s, ¶ms);
965 /* See if user specified frequencies. If so, scan only those. */
966 if (wpa_s->conf->freq_list && !params.freqs) {
967 wpa_dbg(wpa_s, MSG_DEBUG,
968 "Optimize scan based on conf->freq_list");
969 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
972 /* Use current associated channel? */
973 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
974 unsigned int num = wpa_s->num_multichan_concurrent;
976 params.freqs = os_calloc(num + 1, sizeof(int));
978 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
980 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
981 "current operating channels since "
982 "scan_cur_freq is enabled");
984 os_free(params.freqs);
990 params.filter_ssids = wpa_supplicant_build_filter_ssids(
991 wpa_s->conf, ¶ms.num_filter_ssids);
993 params.extra_ies = wpabuf_head(extra_ie);
994 params.extra_ies_len = wpabuf_len(extra_ie);
998 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
999 (wpa_s->show_group_started && wpa_s->go_params)) {
1001 * The interface may not yet be in P2P mode, so we have to
1002 * explicitly request P2P probe to disable CCK rates.
1004 params.p2p_probe = 1;
1006 #endif /* CONFIG_P2P */
1008 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
1009 params.mac_addr_rand = 1;
1010 if (wpa_s->mac_addr_scan) {
1011 params.mac_addr = wpa_s->mac_addr_scan;
1012 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1016 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1017 struct wpa_bss *bss;
1019 params.bssid = wpa_s->next_scan_bssid;
1020 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1021 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1022 params.ssids[0].ssid_len == 0) {
1023 params.ssids[0].ssid = bss->ssid;
1024 params.ssids[0].ssid_len = bss->ssid_len;
1025 wpa_dbg(wpa_s, MSG_DEBUG,
1026 "Scan a previously specified BSSID " MACSTR
1028 MAC2STR(params.bssid),
1029 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1031 wpa_dbg(wpa_s, MSG_DEBUG,
1032 "Scan a previously specified BSSID " MACSTR,
1033 MAC2STR(params.bssid));
1037 scan_params = ¶ms;
1042 * If the driver does not support multi-channel concurrency and a
1043 * virtual interface that shares the same radio with the wpa_s interface
1044 * is operating there may not be need to scan other channels apart from
1045 * the current operating channel on the other virtual interface. Filter
1046 * out other channels in case we are trying to find a connection for a
1047 * station interface when we are not configured to prefer station
1048 * connection and a concurrent operation is already in process.
1050 if (wpa_s->scan_for_connection &&
1051 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1052 !scan_params->freqs && !params.freqs &&
1053 wpas_is_p2p_prioritized(wpa_s) &&
1054 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1055 non_p2p_network_enabled(wpa_s)) {
1056 unsigned int num = wpa_s->num_multichan_concurrent;
1058 params.freqs = os_calloc(num + 1, sizeof(int));
1060 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1061 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1062 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1064 os_free(params.freqs);
1065 params.freqs = NULL;
1069 #endif /* CONFIG_P2P */
1071 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
1073 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1074 !wpa_s->manual_scan_freqs) {
1075 /* Restore manual_scan_freqs for the next attempt */
1076 wpa_s->manual_scan_freqs = params.freqs;
1077 params.freqs = NULL;
1080 wpabuf_free(extra_ie);
1081 os_free(params.freqs);
1082 os_free(params.filter_ssids);
1085 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1086 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1087 wpa_supplicant_set_state(wpa_s,
1088 wpa_s->scan_prev_wpa_state);
1089 /* Restore scan_req since we will try to scan again */
1090 wpa_s->scan_req = wpa_s->last_scan_req;
1091 wpa_supplicant_req_scan(wpa_s, 1, 0);
1093 wpa_s->scan_for_connection = 0;
1094 #ifdef CONFIG_INTERWORKING
1095 wpa_s->interworking_fast_assoc_tried = 0;
1096 #endif /* CONFIG_INTERWORKING */
1098 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1103 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1105 struct os_reltime remaining, new_int;
1108 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1113 if (cancelled && os_reltime_before(&remaining, &new_int)) {
1114 new_int.sec = remaining.sec;
1115 new_int.usec = remaining.usec;
1119 eloop_register_timeout(new_int.sec, new_int.usec,
1120 wpa_supplicant_scan, wpa_s, NULL);
1122 wpa_s->scan_interval = sec;
1127 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1128 * @wpa_s: Pointer to wpa_supplicant data
1129 * @sec: Number of seconds after which to scan
1130 * @usec: Number of microseconds after which to scan
1132 * This function is used to schedule a scan for neighboring access points after
1133 * the specified time.
1135 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1139 if (wpa_s->p2p_mgmt) {
1140 wpa_dbg(wpa_s, MSG_DEBUG,
1141 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1146 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1149 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1151 } else if (res == 0) {
1152 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1155 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1157 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1163 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1164 * @wpa_s: Pointer to wpa_supplicant data
1165 * @sec: Number of seconds after which to scan
1166 * @usec: Number of microseconds after which to scan
1167 * Returns: 0 on success or -1 otherwise
1169 * This function is used to schedule periodic scans for neighboring
1170 * access points after the specified time.
1172 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1175 if (!wpa_s->sched_scan_supported)
1178 eloop_register_timeout(sec, usec,
1179 wpa_supplicant_delayed_sched_scan_timeout,
1187 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1188 * @wpa_s: Pointer to wpa_supplicant data
1189 * Returns: 0 is sched_scan was started or -1 otherwise
1191 * This function is used to schedule periodic scans for neighboring
1192 * access points repeating the scan continuously.
1194 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1196 struct wpa_driver_scan_params params;
1197 struct wpa_driver_scan_params *scan_params;
1198 enum wpa_states prev_state;
1199 struct wpa_ssid *ssid = NULL;
1200 struct wpabuf *extra_ie = NULL;
1202 unsigned int max_sched_scan_ssids;
1205 struct sched_scan_plan scan_plan;
1207 if (!wpa_s->sched_scan_supported)
1210 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1211 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1213 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1214 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1217 if (wpa_s->sched_scanning) {
1218 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1223 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1224 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1225 /* Use wildcard SSID to find this network */
1227 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1232 if (!wpas_network_disabled(wpa_s, ssid) &&
1233 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1235 * Normal scan is more reliable and faster for WPS
1236 * operations and since these are for short periods of
1237 * time, the benefit of trying to use sched_scan would
1240 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1241 "sched_scan for WPS");
1244 #endif /* CONFIG_WPS */
1249 if (wpa_s->normal_scans < 3 &&
1250 (need_ssids <= wpa_s->max_scan_ssids ||
1251 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1253 * When normal scan can speed up operations, use that for the
1254 * first operations before starting the sched_scan to allow
1255 * user space sleep more. We do this only if the normal scan
1256 * has functionality that is suitable for this or if the
1257 * sched_scan does not have better support for multiple SSIDs.
1259 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1260 "sched_scan for initial scans (normal_scans=%d)",
1261 wpa_s->normal_scans);
1265 os_memset(¶ms, 0, sizeof(params));
1267 /* If we can't allocate space for the filters, we just don't filter */
1268 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1269 sizeof(struct wpa_driver_scan_filter));
1271 prev_state = wpa_s->wpa_state;
1272 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1273 wpa_s->wpa_state == WPA_INACTIVE)
1274 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1276 if (wpa_s->autoscan_params != NULL) {
1277 scan_params = wpa_s->autoscan_params;
1281 /* Find the starting point from which to continue scanning */
1282 ssid = wpa_s->conf->ssid;
1283 if (wpa_s->prev_sched_ssid) {
1285 if (ssid == wpa_s->prev_sched_ssid) {
1293 if (!ssid || !wpa_s->prev_sched_ssid) {
1294 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1295 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1296 wpa_s->first_sched_scan = 1;
1297 ssid = wpa_s->conf->ssid;
1298 wpa_s->prev_sched_ssid = ssid;
1302 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1307 if (wpas_network_disabled(wpa_s, ssid))
1310 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1311 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1312 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1313 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1314 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1315 ssid->ssid, ssid->ssid_len);
1316 params.filter_ssids[params.num_filter_ssids].ssid_len =
1318 params.num_filter_ssids++;
1319 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1321 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1322 "filter for sched_scan - drop filter");
1323 os_free(params.filter_ssids);
1324 params.filter_ssids = NULL;
1325 params.num_filter_ssids = 0;
1328 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1329 if (params.num_ssids == max_sched_scan_ssids)
1330 break; /* only room for broadcast SSID */
1331 wpa_dbg(wpa_s, MSG_DEBUG,
1332 "add to active scan ssid: %s",
1333 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1334 params.ssids[params.num_ssids].ssid =
1336 params.ssids[params.num_ssids].ssid_len =
1339 if (params.num_ssids >= max_sched_scan_ssids) {
1340 wpa_s->prev_sched_ssid = ssid;
1344 (wpas_network_disabled(wpa_s, ssid) ||
1351 wpa_s->prev_sched_ssid = ssid;
1355 if (params.num_filter_ssids == 0) {
1356 os_free(params.filter_ssids);
1357 params.filter_ssids = NULL;
1360 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1362 params.extra_ies = wpabuf_head(extra_ie);
1363 params.extra_ies_len = wpabuf_len(extra_ie);
1366 if (wpa_s->conf->filter_rssi)
1367 params.filter_rssi = wpa_s->conf->filter_rssi;
1369 /* See if user specified frequencies. If so, scan only those. */
1370 if (wpa_s->conf->freq_list && !params.freqs) {
1371 wpa_dbg(wpa_s, MSG_DEBUG,
1372 "Optimize scan based on conf->freq_list");
1373 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
1376 scan_params = ¶ms;
1379 wpa_s->sched_scan_timed_out = 0;
1382 * We cannot support multiple scan plans if the scan request includes
1383 * too many SSID's, so in this case use only the last scan plan and make
1384 * it run infinitely. It will be stopped by the timeout.
1386 if (wpa_s->sched_scan_plans_num == 1 ||
1387 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1388 params.sched_scan_plans = wpa_s->sched_scan_plans;
1389 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1390 } else if (wpa_s->sched_scan_plans_num > 1) {
1391 wpa_dbg(wpa_s, MSG_DEBUG,
1392 "Too many SSIDs. Default to using single scheduled_scan plan");
1393 params.sched_scan_plans =
1394 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1396 params.sched_scan_plans_num = 1;
1398 if (wpa_s->conf->sched_scan_interval)
1399 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1401 scan_plan.interval = 10;
1403 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1404 wpa_printf(MSG_WARNING,
1405 "Scan interval too long(%u), use the maximum allowed(%u)",
1407 wpa_s->max_sched_scan_plan_interval);
1408 scan_plan.interval =
1409 wpa_s->max_sched_scan_plan_interval;
1412 scan_plan.iterations = 0;
1413 params.sched_scan_plans = &scan_plan;
1414 params.sched_scan_plans_num = 1;
1417 if (ssid || !wpa_s->first_sched_scan) {
1418 wpa_dbg(wpa_s, MSG_DEBUG,
1419 "Starting sched scan: interval %u timeout %d",
1420 params.sched_scan_plans[0].interval,
1421 wpa_s->sched_scan_timeout);
1423 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
1426 wpa_setband_scan_freqs(wpa_s, scan_params);
1428 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1429 params.mac_addr_rand = 1;
1430 if (wpa_s->mac_addr_sched_scan) {
1431 params.mac_addr = wpa_s->mac_addr_sched_scan;
1432 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1437 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1438 wpabuf_free(extra_ie);
1439 os_free(params.filter_ssids);
1441 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1442 if (prev_state != wpa_s->wpa_state)
1443 wpa_supplicant_set_state(wpa_s, prev_state);
1447 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1448 if (ssid || !wpa_s->first_sched_scan) {
1449 wpa_s->sched_scan_timed_out = 0;
1450 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1451 wpa_supplicant_sched_scan_timeout,
1453 wpa_s->first_sched_scan = 0;
1454 wpa_s->sched_scan_timeout /= 2;
1455 params.sched_scan_plans[0].interval *= 2;
1456 if ((unsigned int) wpa_s->sched_scan_timeout <
1457 params.sched_scan_plans[0].interval ||
1458 params.sched_scan_plans[0].interval >
1459 wpa_s->max_sched_scan_plan_interval) {
1460 params.sched_scan_plans[0].interval = 10;
1461 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1465 /* If there is no more ssids, start next time from the beginning */
1467 wpa_s->prev_sched_ssid = NULL;
1474 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1475 * @wpa_s: Pointer to wpa_supplicant data
1477 * This function is used to cancel a scan request scheduled with
1478 * wpa_supplicant_req_scan().
1480 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1482 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1483 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1488 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1489 * @wpa_s: Pointer to wpa_supplicant data
1491 * This function is used to stop a delayed scheduled scan.
1493 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1495 if (!wpa_s->sched_scan_supported)
1498 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1499 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1505 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1506 * @wpa_s: Pointer to wpa_supplicant data
1508 * This function is used to stop a periodic scheduled scan.
1510 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1512 if (!wpa_s->sched_scanning)
1515 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1516 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1517 wpa_supplicant_stop_sched_scan(wpa_s);
1522 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1523 * @wpa_s: Pointer to wpa_supplicant data
1524 * @scanning: Whether scanning is currently in progress
1526 * This function is to generate scanning notifycations. It is called whenever
1527 * there may have been a change in scanning (scan started, completed, stopped).
1528 * wpas_notify_scanning() is called whenever the scanning state changed from the
1529 * previously notified state.
1531 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1534 if (wpa_s->scanning != scanning) {
1535 wpa_s->scanning = scanning;
1536 wpas_notify_scanning(wpa_s);
1541 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1547 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1548 for (i = 0; ie && i < ie[1]; i++) {
1549 if ((ie[i + 2] & 0x7f) > rate)
1550 rate = ie[i + 2] & 0x7f;
1553 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1554 for (i = 0; ie && i < ie[1]; i++) {
1555 if ((ie[i + 2] & 0x7f) > rate)
1556 rate = ie[i + 2] & 0x7f;
1564 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1565 * @res: Scan result entry
1566 * @ie: Information element identitifier (WLAN_EID_*)
1567 * Returns: Pointer to the information element (id field) or %NULL if not found
1569 * This function returns the first matching information element in the scan
1572 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1574 return get_ie((const u8 *) (res + 1), res->ie_len, ie);
1579 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1580 * @res: Scan result entry
1581 * @vendor_type: Vendor type (four octets starting the IE payload)
1582 * Returns: Pointer to the information element (id field) or %NULL if not found
1584 * This function returns the first matching information element in the scan
1587 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1590 const u8 *end, *pos;
1592 pos = (const u8 *) (res + 1);
1593 end = pos + res->ie_len;
1595 while (end - pos > 1) {
1596 if (2 + pos[1] > end - pos)
1598 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1599 vendor_type == WPA_GET_BE32(&pos[2]))
1609 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1610 * @res: Scan result entry
1611 * @vendor_type: Vendor type (four octets starting the IE payload)
1612 * Returns: Pointer to the information element (id field) or %NULL if not found
1614 * This function returns the first matching information element in the scan
1617 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1618 * from Beacon frames instead of either Beacon or Probe Response frames.
1620 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1623 const u8 *end, *pos;
1625 if (res->beacon_ie_len == 0)
1628 pos = (const u8 *) (res + 1);
1630 end = pos + res->beacon_ie_len;
1632 while (end - pos > 1) {
1633 if (2 + pos[1] > end - pos)
1635 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1636 vendor_type == WPA_GET_BE32(&pos[2]))
1646 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1647 * @res: Scan result entry
1648 * @vendor_type: Vendor type (four octets starting the IE payload)
1649 * Returns: Pointer to the information element payload or %NULL if not found
1651 * This function returns concatenated payload of possibly fragmented vendor
1652 * specific information elements in the scan result. The caller is responsible
1653 * for freeing the returned buffer.
1655 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1659 const u8 *end, *pos;
1661 buf = wpabuf_alloc(res->ie_len);
1665 pos = (const u8 *) (res + 1);
1666 end = pos + res->ie_len;
1668 while (end - pos > 1) {
1669 if (2 + pos[1] > end - pos)
1671 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1672 vendor_type == WPA_GET_BE32(&pos[2]))
1673 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1677 if (wpabuf_len(buf) == 0) {
1687 * Channels with a great SNR can operate at full rate. What is a great SNR?
1688 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1689 * rule of thumb is that any SNR above 20 is good." This one
1690 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1691 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1692 * conservative value.
1694 #define GREAT_SNR 30
1696 #define IS_5GHZ(n) (n > 4000)
1698 /* Compare function for sorting scan results. Return >0 if @b is considered
1700 static int wpa_scan_result_compar(const void *a, const void *b)
1702 #define MIN(a,b) a < b ? a : b
1703 struct wpa_scan_res **_wa = (void *) a;
1704 struct wpa_scan_res **_wb = (void *) b;
1705 struct wpa_scan_res *wa = *_wa;
1706 struct wpa_scan_res *wb = *_wb;
1708 int snr_a, snr_b, snr_a_full, snr_b_full;
1710 /* WPA/WPA2 support preferred */
1711 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1712 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1713 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1714 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1716 if (wpa_b && !wpa_a)
1718 if (!wpa_b && wpa_a)
1721 /* privacy support preferred */
1722 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1723 (wb->caps & IEEE80211_CAP_PRIVACY))
1725 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1726 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1729 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
1730 snr_a_full = wa->snr;
1731 snr_a = MIN(wa->snr, GREAT_SNR);
1732 snr_b_full = wb->snr;
1733 snr_b = MIN(wb->snr, GREAT_SNR);
1735 /* Level is not in dBm, so we can't calculate
1736 * SNR. Just use raw level (units unknown). */
1737 snr_a = snr_a_full = wa->level;
1738 snr_b = snr_b_full = wb->level;
1741 /* if SNR is close, decide by max rate or frequency band */
1742 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1743 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1744 if (wa->est_throughput != wb->est_throughput)
1745 return wb->est_throughput - wa->est_throughput;
1746 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1747 return IS_5GHZ(wa->freq) ? -1 : 1;
1750 /* all things being equal, use SNR; if SNRs are
1751 * identical, use quality values since some drivers may only report
1752 * that value and leave the signal level zero */
1753 if (snr_b_full == snr_a_full)
1754 return wb->qual - wa->qual;
1755 return snr_b_full - snr_a_full;
1761 /* Compare function for sorting scan results when searching a WPS AP for
1762 * provisioning. Return >0 if @b is considered better. */
1763 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1765 struct wpa_scan_res **_wa = (void *) a;
1766 struct wpa_scan_res **_wb = (void *) b;
1767 struct wpa_scan_res *wa = *_wa;
1768 struct wpa_scan_res *wb = *_wb;
1769 int uses_wps_a, uses_wps_b;
1770 struct wpabuf *wps_a, *wps_b;
1773 /* Optimization - check WPS IE existence before allocated memory and
1774 * doing full reassembly. */
1775 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1776 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1777 if (uses_wps_a && !uses_wps_b)
1779 if (!uses_wps_a && uses_wps_b)
1782 if (uses_wps_a && uses_wps_b) {
1783 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1784 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1785 res = wps_ap_priority_compar(wps_a, wps_b);
1793 * Do not use current AP security policy as a sorting criteria during
1794 * WPS provisioning step since the AP may get reconfigured at the
1795 * completion of provisioning.
1798 /* all things being equal, use signal level; if signal levels are
1799 * identical, use quality values since some drivers may only report
1800 * that value and leave the signal level zero */
1801 if (wb->level == wa->level)
1802 return wb->qual - wa->qual;
1803 return wb->level - wa->level;
1805 #endif /* CONFIG_WPS */
1808 static void dump_scan_res(struct wpa_scan_results *scan_res)
1810 #ifndef CONFIG_NO_STDOUT_DEBUG
1813 if (scan_res->res == NULL || scan_res->num == 0)
1816 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1818 for (i = 0; i < scan_res->num; i++) {
1819 struct wpa_scan_res *r = scan_res->res[i];
1821 if (r->flags & WPA_SCAN_LEVEL_DBM) {
1822 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1824 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1825 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
1826 MAC2STR(r->bssid), r->freq, r->qual,
1827 r->noise, noise_valid ? "" : "~", r->level,
1828 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1830 r->age, r->est_throughput);
1832 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1833 "noise=%d level=%d flags=0x%x age=%u est=%u",
1834 MAC2STR(r->bssid), r->freq, r->qual,
1835 r->noise, r->level, r->flags, r->age,
1838 pos = (u8 *) (r + 1);
1840 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1842 if (r->beacon_ie_len)
1843 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1844 pos, r->beacon_ie_len);
1846 #endif /* CONFIG_NO_STDOUT_DEBUG */
1851 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1852 * @wpa_s: Pointer to wpa_supplicant data
1853 * @bssid: BSSID to check
1854 * Returns: 0 if the BSSID is filtered or 1 if not
1856 * This function is used to filter out specific BSSIDs from scan reslts mainly
1857 * for testing purposes (SET bssid_filter ctrl_iface command).
1859 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1864 if (wpa_s->bssid_filter == NULL)
1867 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1868 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1877 void filter_scan_res(struct wpa_supplicant *wpa_s,
1878 struct wpa_scan_results *res)
1882 if (wpa_s->bssid_filter == NULL)
1885 for (i = 0, j = 0; i < res->num; i++) {
1886 if (wpa_supplicant_filter_bssid_match(wpa_s,
1887 res->res[i]->bssid)) {
1888 res->res[j++] = res->res[i];
1890 os_free(res->res[i]);
1895 if (res->num != j) {
1896 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1897 (int) (res->num - j));
1904 * Noise floor values to use when we have signal strength
1905 * measurements, but no noise floor measurements. These values were
1906 * measured in an office environment with many APs.
1908 #define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1909 #define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1911 void scan_snr(struct wpa_scan_res *res)
1913 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1914 res->noise = IS_5GHZ(res->freq) ?
1915 DEFAULT_NOISE_FLOOR_5GHZ :
1916 DEFAULT_NOISE_FLOOR_2GHZ;
1919 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1920 res->snr = res->level - res->noise;
1922 /* Level is not in dBm, so we can't calculate
1923 * SNR. Just use raw level (units unknown). */
1924 res->snr = res->level;
1929 static unsigned int max_ht20_rate(int snr)
1932 return 6500; /* HT20 MCS0 */
1934 return 13000; /* HT20 MCS1 */
1936 return 19500; /* HT20 MCS2 */
1938 return 26000; /* HT20 MCS3 */
1940 return 39000; /* HT20 MCS4 */
1942 return 52000; /* HT20 MCS5 */
1944 return 58500; /* HT20 MCS6 */
1945 return 65000; /* HT20 MCS7 */
1949 static unsigned int max_ht40_rate(int snr)
1952 return 13500; /* HT40 MCS0 */
1954 return 27000; /* HT40 MCS1 */
1956 return 40500; /* HT40 MCS2 */
1958 return 54000; /* HT40 MCS3 */
1960 return 81000; /* HT40 MCS4 */
1962 return 108000; /* HT40 MCS5 */
1964 return 121500; /* HT40 MCS6 */
1965 return 135000; /* HT40 MCS7 */
1969 static unsigned int max_vht80_rate(int snr)
1974 return 29300; /* VHT80 MCS0 */
1976 return 58500; /* VHT80 MCS1 */
1978 return 87800; /* VHT80 MCS2 */
1980 return 117000; /* VHT80 MCS3 */
1982 return 175500; /* VHT80 MCS4 */
1984 return 234000; /* VHT80 MCS5 */
1986 return 263300; /* VHT80 MCS6 */
1988 return 292500; /* VHT80 MCS7 */
1990 return 351000; /* VHT80 MCS8 */
1991 return 390000; /* VHT80 MCS9 */
1995 void scan_est_throughput(struct wpa_supplicant *wpa_s,
1996 struct wpa_scan_res *res)
1998 enum local_hw_capab capab = wpa_s->hw_capab;
1999 int rate; /* max legacy rate in 500 kb/s units */
2001 unsigned int est, tmp;
2004 if (res->est_throughput)
2007 /* Get maximum legacy rate */
2008 rate = wpa_scan_get_max_rate(res);
2010 /* Limit based on estimated SNR */
2011 if (rate > 1 * 2 && snr < 1)
2013 else if (rate > 2 * 2 && snr < 4)
2015 else if (rate > 6 * 2 && snr < 5)
2017 else if (rate > 9 * 2 && snr < 6)
2019 else if (rate > 12 * 2 && snr < 7)
2021 else if (rate > 18 * 2 && snr < 10)
2023 else if (rate > 24 * 2 && snr < 11)
2025 else if (rate > 36 * 2 && snr < 15)
2027 else if (rate > 48 * 2 && snr < 19)
2029 else if (rate > 54 * 2 && snr < 21)
2033 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2034 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2036 tmp = max_ht20_rate(snr);
2042 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2043 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2044 if (ie && ie[1] >= 2 &&
2045 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2046 tmp = max_ht40_rate(snr);
2052 if (capab == CAPAB_VHT) {
2053 /* Use +1 to assume VHT is always faster than HT */
2054 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2056 tmp = max_ht20_rate(snr) + 1;
2060 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2061 if (ie && ie[1] >= 2 &&
2063 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2064 tmp = max_ht40_rate(snr) + 1;
2069 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2070 if (ie && ie[1] >= 1 &&
2071 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2072 tmp = max_vht80_rate(snr) + 1;
2079 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2081 res->est_throughput = est;
2086 * wpa_supplicant_get_scan_results - Get scan results
2087 * @wpa_s: Pointer to wpa_supplicant data
2088 * @info: Information about what was scanned or %NULL if not available
2089 * @new_scan: Whether a new scan was performed
2090 * Returns: Scan results, %NULL on failure
2092 * This function request the current scan results from the driver and updates
2093 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2094 * results with wpa_scan_results_free().
2096 struct wpa_scan_results *
2097 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2098 struct scan_info *info, int new_scan)
2100 struct wpa_scan_results *scan_res;
2102 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2104 scan_res = wpa_drv_get_scan_results2(wpa_s);
2105 if (scan_res == NULL) {
2106 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2109 if (scan_res->fetch_time.sec == 0) {
2111 * Make sure we have a valid timestamp if the driver wrapper
2112 * does not set this.
2114 os_get_reltime(&scan_res->fetch_time);
2116 filter_scan_res(wpa_s, scan_res);
2118 for (i = 0; i < scan_res->num; i++) {
2119 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2121 scan_snr(scan_res_item);
2122 scan_est_throughput(wpa_s, scan_res_item);
2126 if (wpas_wps_searching(wpa_s)) {
2127 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2128 "provisioning rules");
2129 compar = wpa_scan_result_wps_compar;
2131 #endif /* CONFIG_WPS */
2133 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
2135 dump_scan_res(scan_res);
2137 wpa_bss_update_start(wpa_s);
2138 for (i = 0; i < scan_res->num; i++)
2139 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2140 &scan_res->fetch_time);
2141 wpa_bss_update_end(wpa_s, info, new_scan);
2148 * wpa_supplicant_update_scan_results - Update scan results from the driver
2149 * @wpa_s: Pointer to wpa_supplicant data
2150 * Returns: 0 on success, -1 on failure
2152 * This function updates the BSS table within wpa_supplicant based on the
2153 * currently available scan results from the driver without requesting a new
2154 * scan. This is used in cases where the driver indicates an association
2155 * (including roaming within ESS) and wpa_supplicant does not yet have the
2156 * needed information to complete the connection (e.g., to perform validation
2157 * steps in 4-way handshake).
2159 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2161 struct wpa_scan_results *scan_res;
2162 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2163 if (scan_res == NULL)
2165 wpa_scan_results_free(scan_res);
2172 * scan_only_handler - Reports scan results
2174 void scan_only_handler(struct wpa_supplicant *wpa_s,
2175 struct wpa_scan_results *scan_res)
2177 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
2178 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2179 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2180 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2181 wpa_s->manual_scan_id);
2182 wpa_s->manual_scan_use_id = 0;
2184 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2186 wpas_notify_scan_results(wpa_s);
2187 wpas_notify_scan_done(wpa_s, 1);
2188 if (wpa_s->scan_work) {
2189 struct wpa_radio_work *work = wpa_s->scan_work;
2190 wpa_s->scan_work = NULL;
2191 radio_work_done(work);
2194 if (wpa_s->wpa_state == WPA_SCANNING)
2195 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
2199 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2201 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2205 struct wpa_driver_scan_params *
2206 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2208 struct wpa_driver_scan_params *params;
2212 params = os_zalloc(sizeof(*params));
2216 for (i = 0; i < src->num_ssids; i++) {
2217 if (src->ssids[i].ssid) {
2218 n = os_malloc(src->ssids[i].ssid_len);
2221 os_memcpy(n, src->ssids[i].ssid,
2222 src->ssids[i].ssid_len);
2223 params->ssids[i].ssid = n;
2224 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2227 params->num_ssids = src->num_ssids;
2229 if (src->extra_ies) {
2230 n = os_malloc(src->extra_ies_len);
2233 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2234 params->extra_ies = n;
2235 params->extra_ies_len = src->extra_ies_len;
2239 int len = int_array_len(src->freqs);
2240 params->freqs = os_malloc((len + 1) * sizeof(int));
2241 if (params->freqs == NULL)
2243 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2246 if (src->filter_ssids) {
2247 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
2248 src->num_filter_ssids);
2249 if (params->filter_ssids == NULL)
2251 os_memcpy(params->filter_ssids, src->filter_ssids,
2252 sizeof(*params->filter_ssids) *
2253 src->num_filter_ssids);
2254 params->num_filter_ssids = src->num_filter_ssids;
2257 params->filter_rssi = src->filter_rssi;
2258 params->p2p_probe = src->p2p_probe;
2259 params->only_new_results = src->only_new_results;
2260 params->low_priority = src->low_priority;
2262 if (src->sched_scan_plans_num > 0) {
2263 params->sched_scan_plans =
2264 os_malloc(sizeof(*src->sched_scan_plans) *
2265 src->sched_scan_plans_num);
2266 if (!params->sched_scan_plans)
2269 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2270 sizeof(*src->sched_scan_plans) *
2271 src->sched_scan_plans_num);
2272 params->sched_scan_plans_num = src->sched_scan_plans_num;
2275 if (src->mac_addr_rand) {
2276 params->mac_addr_rand = src->mac_addr_rand;
2278 if (src->mac_addr && src->mac_addr_mask) {
2281 mac_addr = os_malloc(2 * ETH_ALEN);
2285 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2286 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2288 params->mac_addr = mac_addr;
2289 params->mac_addr_mask = mac_addr + ETH_ALEN;
2296 bssid = os_malloc(ETH_ALEN);
2299 os_memcpy(bssid, src->bssid, ETH_ALEN);
2300 params->bssid = bssid;
2306 wpa_scan_free_params(params);
2311 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2318 for (i = 0; i < params->num_ssids; i++)
2319 os_free((u8 *) params->ssids[i].ssid);
2320 os_free((u8 *) params->extra_ies);
2321 os_free(params->freqs);
2322 os_free(params->filter_ssids);
2323 os_free(params->sched_scan_plans);
2326 * Note: params->mac_addr_mask points to same memory allocation and
2327 * must not be freed separately.
2329 os_free((u8 *) params->mac_addr);
2331 os_free((u8 *) params->bssid);
2337 int wpas_start_pno(struct wpa_supplicant *wpa_s)
2340 size_t i, num_ssid, num_match_ssid;
2341 struct wpa_ssid *ssid;
2342 struct wpa_driver_scan_params params;
2343 struct sched_scan_plan scan_plan;
2344 unsigned int max_sched_scan_ssids;
2346 if (!wpa_s->sched_scan_supported)
2349 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2350 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2352 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2353 if (max_sched_scan_ssids < 1)
2356 if (wpa_s->pno || wpa_s->pno_sched_pending)
2359 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2360 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2361 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2365 if (wpa_s->wpa_state == WPA_SCANNING) {
2366 wpa_supplicant_cancel_scan(wpa_s);
2367 if (wpa_s->sched_scanning) {
2368 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2369 "ongoing sched scan");
2370 wpa_supplicant_cancel_sched_scan(wpa_s);
2371 wpa_s->pno_sched_pending = 1;
2376 os_memset(¶ms, 0, sizeof(params));
2378 num_ssid = num_match_ssid = 0;
2379 ssid = wpa_s->conf->ssid;
2381 if (!wpas_network_disabled(wpa_s, ssid)) {
2383 if (ssid->scan_ssid)
2389 if (num_match_ssid == 0) {
2390 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2394 if (num_match_ssid > num_ssid) {
2395 params.num_ssids++; /* wildcard */
2399 if (num_ssid > max_sched_scan_ssids) {
2400 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2401 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2402 num_ssid = max_sched_scan_ssids;
2405 if (num_match_ssid > wpa_s->max_match_sets) {
2406 num_match_ssid = wpa_s->max_match_sets;
2407 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
2409 params.filter_ssids = os_calloc(num_match_ssid,
2410 sizeof(struct wpa_driver_scan_filter));
2411 if (params.filter_ssids == NULL)
2416 ssid = wpa_s->conf->pssid[prio];
2418 if (!wpas_network_disabled(wpa_s, ssid)) {
2419 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2420 params.ssids[params.num_ssids].ssid =
2422 params.ssids[params.num_ssids].ssid_len =
2426 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2428 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2429 params.num_filter_ssids++;
2431 if (i == num_match_ssid)
2436 else if (prio + 1 == wpa_s->conf->num_prio)
2439 ssid = wpa_s->conf->pssid[++prio];
2442 if (wpa_s->conf->filter_rssi)
2443 params.filter_rssi = wpa_s->conf->filter_rssi;
2445 if (wpa_s->sched_scan_plans_num) {
2446 params.sched_scan_plans = wpa_s->sched_scan_plans;
2447 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2449 /* Set one scan plan that will run infinitely */
2450 if (wpa_s->conf->sched_scan_interval)
2451 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2453 scan_plan.interval = 10;
2455 scan_plan.iterations = 0;
2456 params.sched_scan_plans = &scan_plan;
2457 params.sched_scan_plans_num = 1;
2460 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2461 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2462 params.freqs = wpa_s->manual_sched_scan_freqs;
2465 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2466 params.mac_addr_rand = 1;
2467 if (wpa_s->mac_addr_pno) {
2468 params.mac_addr = wpa_s->mac_addr_pno;
2469 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2473 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms);
2474 os_free(params.filter_ssids);
2478 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2483 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2490 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2493 wpa_s->pno_sched_pending = 0;
2495 if (wpa_s->wpa_state == WPA_SCANNING)
2496 wpa_supplicant_req_scan(wpa_s, 0, 0);
2502 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2505 type &= MAC_ADDR_RAND_ALL;
2506 wpa_s->mac_addr_rand_enable &= ~type;
2508 if (type & MAC_ADDR_RAND_SCAN) {
2509 os_free(wpa_s->mac_addr_scan);
2510 wpa_s->mac_addr_scan = NULL;
2513 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2514 os_free(wpa_s->mac_addr_sched_scan);
2515 wpa_s->mac_addr_sched_scan = NULL;
2518 if (type & MAC_ADDR_RAND_PNO) {
2519 os_free(wpa_s->mac_addr_pno);
2520 wpa_s->mac_addr_pno = NULL;
2525 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2526 unsigned int type, const u8 *addr,
2531 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2534 tmp = os_malloc(2 * ETH_ALEN);
2537 os_memcpy(tmp, addr, ETH_ALEN);
2538 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2541 if (type == MAC_ADDR_RAND_SCAN) {
2542 wpa_s->mac_addr_scan = tmp;
2543 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2544 wpa_s->mac_addr_sched_scan = tmp;
2545 } else if (type == MAC_ADDR_RAND_PNO) {
2546 wpa_s->mac_addr_pno = tmp;
2548 wpa_printf(MSG_INFO,
2549 "scan: Invalid MAC randomization type=0x%x",
2555 wpa_s->mac_addr_rand_enable |= type;
2560 int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2562 int scan_work = !!wpa_s->scan_work;
2565 scan_work |= !!wpa_s->p2p_scan_work;
2566 #endif /* CONFIG_P2P */
2568 if (scan_work && wpa_s->own_scan_running) {
2569 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2570 return wpa_drv_abort_scan(wpa_s);
2577 int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2579 struct sched_scan_plan *scan_plans = NULL;
2580 const char *token, *context = NULL;
2581 unsigned int num = 0;
2587 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2588 os_free(wpa_s->sched_scan_plans);
2589 wpa_s->sched_scan_plans = NULL;
2590 wpa_s->sched_scan_plans_num = 0;
2594 while ((token = cstr_token(cmd, " ", &context))) {
2596 struct sched_scan_plan *scan_plan, *n;
2598 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2603 scan_plan = &scan_plans[num];
2606 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2607 &scan_plan->iterations);
2608 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2609 wpa_printf(MSG_ERROR,
2610 "Invalid sched scan plan input: %s", token);
2614 if (!scan_plan->interval) {
2615 wpa_printf(MSG_ERROR,
2616 "scan plan %u: Interval cannot be zero",
2621 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2622 wpa_printf(MSG_WARNING,
2623 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2624 num, scan_plan->interval,
2625 wpa_s->max_sched_scan_plan_interval);
2626 scan_plan->interval =
2627 wpa_s->max_sched_scan_plan_interval;
2631 scan_plan->iterations = 0;
2635 if (!scan_plan->iterations) {
2636 wpa_printf(MSG_ERROR,
2637 "scan plan %u: Number of iterations cannot be zero",
2642 if (scan_plan->iterations >
2643 wpa_s->max_sched_scan_plan_iterations) {
2644 wpa_printf(MSG_WARNING,
2645 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2646 num, scan_plan->iterations,
2647 wpa_s->max_sched_scan_plan_iterations);
2648 scan_plan->iterations =
2649 wpa_s->max_sched_scan_plan_iterations;
2652 wpa_printf(MSG_DEBUG,
2653 "scan plan %u: interval=%u iterations=%u",
2654 num, scan_plan->interval, scan_plan->iterations);
2658 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2662 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2663 wpa_printf(MSG_ERROR,
2664 "All scan plans but the last must specify a number of iterations");
2668 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2669 num, scan_plans[num - 1].interval);
2671 if (num > wpa_s->max_sched_scan_plans) {
2672 wpa_printf(MSG_WARNING,
2673 "Too many scheduled scan plans (only %u supported)",
2674 wpa_s->max_sched_scan_plans);
2675 wpa_printf(MSG_WARNING,
2676 "Use only the first %u scan plans, and the last one (in infinite loop)",
2677 wpa_s->max_sched_scan_plans - 1);
2678 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2679 &scan_plans[num - 1], sizeof(*scan_plans));
2680 num = wpa_s->max_sched_scan_plans;
2683 os_free(wpa_s->sched_scan_plans);
2684 wpa_s->sched_scan_plans = scan_plans;
2685 wpa_s->sched_scan_plans_num = num;
2690 os_free(scan_plans);
2691 wpa_printf(MSG_ERROR, "invalid scan plans list");