xref: /freebsd/contrib/wpa/wpa_supplicant/scan.c (revision 4b72b91a)
139beb93cSSam Leffler /*
239beb93cSSam Leffler  * WPA Supplicant - Scanning
34bc52338SCy Schubert  * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
739beb93cSSam Leffler  */
839beb93cSSam Leffler 
9e28a4053SRui Paulo #include "utils/includes.h"
1039beb93cSSam Leffler 
11e28a4053SRui Paulo #include "utils/common.h"
12e28a4053SRui Paulo #include "utils/eloop.h"
13e28a4053SRui Paulo #include "common/ieee802_11_defs.h"
145b9c547cSRui Paulo #include "common/wpa_ctrl.h"
1539beb93cSSam Leffler #include "config.h"
1639beb93cSSam Leffler #include "wpa_supplicant_i.h"
17e28a4053SRui Paulo #include "driver_i.h"
1839beb93cSSam Leffler #include "wps_supplicant.h"
19f05cddf9SRui Paulo #include "p2p_supplicant.h"
20f05cddf9SRui Paulo #include "p2p/p2p.h"
21f05cddf9SRui Paulo #include "hs20_supplicant.h"
22e28a4053SRui Paulo #include "notify.h"
23e28a4053SRui Paulo #include "bss.h"
24e28a4053SRui Paulo #include "scan.h"
255b9c547cSRui Paulo #include "mesh.h"
2639beb93cSSam Leffler 
2739beb93cSSam Leffler 
wpa_supplicant_gen_assoc_event(struct wpa_supplicant * wpa_s)2839beb93cSSam Leffler static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
2939beb93cSSam Leffler {
3039beb93cSSam Leffler 	struct wpa_ssid *ssid;
3139beb93cSSam Leffler 	union wpa_event_data data;
3239beb93cSSam Leffler 
3339beb93cSSam Leffler 	ssid = wpa_supplicant_get_ssid(wpa_s);
3439beb93cSSam Leffler 	if (ssid == NULL)
3539beb93cSSam Leffler 		return;
3639beb93cSSam Leffler 
37e28a4053SRui Paulo 	if (wpa_s->current_ssid == NULL) {
3839beb93cSSam Leffler 		wpa_s->current_ssid = ssid;
39e28a4053SRui Paulo 		wpas_notify_network_changed(wpa_s);
40e28a4053SRui Paulo 	}
4139beb93cSSam Leffler 	wpa_supplicant_initiate_eapol(wpa_s);
42f05cddf9SRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43f05cddf9SRui Paulo 		"network - generating associated event");
4439beb93cSSam Leffler 	os_memset(&data, 0, sizeof(data));
4539beb93cSSam Leffler 	wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
4639beb93cSSam Leffler }
4739beb93cSSam Leffler 
4839beb93cSSam Leffler 
4939beb93cSSam Leffler #ifdef CONFIG_WPS
wpas_wps_in_use(struct wpa_supplicant * wpa_s,enum wps_request_type * req_type)50f05cddf9SRui Paulo static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
5139beb93cSSam Leffler 			   enum wps_request_type *req_type)
5239beb93cSSam Leffler {
5339beb93cSSam Leffler 	struct wpa_ssid *ssid;
5439beb93cSSam Leffler 	int wps = 0;
5539beb93cSSam Leffler 
56f05cddf9SRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
5739beb93cSSam Leffler 		if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
5839beb93cSSam Leffler 			continue;
5939beb93cSSam Leffler 
6039beb93cSSam Leffler 		wps = 1;
6139beb93cSSam Leffler 		*req_type = wpas_wps_get_req_type(ssid);
62780fb4a2SCy Schubert 		if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
6339beb93cSSam Leffler 			return 2;
6439beb93cSSam Leffler 	}
6539beb93cSSam Leffler 
66f05cddf9SRui Paulo #ifdef CONFIG_P2P
67f05cddf9SRui Paulo 	if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68f05cddf9SRui Paulo 	    !wpa_s->conf->p2p_disabled) {
69f05cddf9SRui Paulo 		wpa_s->wps->dev.p2p = 1;
70f05cddf9SRui Paulo 		if (!wps) {
71f05cddf9SRui Paulo 			wps = 1;
72f05cddf9SRui Paulo 			*req_type = WPS_REQ_ENROLLEE_INFO;
73f05cddf9SRui Paulo 		}
74f05cddf9SRui Paulo 	}
75f05cddf9SRui Paulo #endif /* CONFIG_P2P */
76f05cddf9SRui Paulo 
7739beb93cSSam Leffler 	return wps;
7839beb93cSSam Leffler }
7939beb93cSSam Leffler #endif /* CONFIG_WPS */
8039beb93cSSam Leffler 
813157ba21SRui Paulo 
wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params * params,const u8 * mac_addr)82c1d255d3SCy Schubert static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
83c1d255d3SCy Schubert 					  const u8 *mac_addr)
84c1d255d3SCy Schubert {
85c1d255d3SCy Schubert 	u8 *tmp;
86c1d255d3SCy Schubert 
87c1d255d3SCy Schubert 	if (params->mac_addr) {
88c1d255d3SCy Schubert 		params->mac_addr_mask = NULL;
89c1d255d3SCy Schubert 		os_free(params->mac_addr);
90c1d255d3SCy Schubert 		params->mac_addr = NULL;
91c1d255d3SCy Schubert 	}
92c1d255d3SCy Schubert 
93c1d255d3SCy Schubert 	params->mac_addr_rand = 1;
94c1d255d3SCy Schubert 
95c1d255d3SCy Schubert 	if (!mac_addr)
96c1d255d3SCy Schubert 		return 0;
97c1d255d3SCy Schubert 
98c1d255d3SCy Schubert 	tmp = os_malloc(2 * ETH_ALEN);
99c1d255d3SCy Schubert 	if (!tmp)
100c1d255d3SCy Schubert 		return -1;
101c1d255d3SCy Schubert 
102c1d255d3SCy Schubert 	os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
103c1d255d3SCy Schubert 	params->mac_addr = tmp;
104c1d255d3SCy Schubert 	params->mac_addr_mask = tmp + ETH_ALEN;
105c1d255d3SCy Schubert 	return 0;
106c1d255d3SCy Schubert }
107c1d255d3SCy Schubert 
108c1d255d3SCy Schubert 
109f05cddf9SRui Paulo /**
110f05cddf9SRui Paulo  * wpa_supplicant_enabled_networks - Check whether there are enabled networks
111f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
112f05cddf9SRui Paulo  * Returns: 0 if no networks are enabled, >0 if networks are enabled
113f05cddf9SRui Paulo  *
114f05cddf9SRui Paulo  * This function is used to figure out whether any networks (or Interworking
115f05cddf9SRui Paulo  * with enabled credentials and auto_interworking) are present in the current
116f05cddf9SRui Paulo  * configuration.
117f05cddf9SRui Paulo  */
wpa_supplicant_enabled_networks(struct wpa_supplicant * wpa_s)118f05cddf9SRui Paulo int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
1193157ba21SRui Paulo {
120f05cddf9SRui Paulo 	struct wpa_ssid *ssid = wpa_s->conf->ssid;
121f05cddf9SRui Paulo 	int count = 0, disabled = 0;
1225b9c547cSRui Paulo 
1235b9c547cSRui Paulo 	if (wpa_s->p2p_mgmt)
1245b9c547cSRui Paulo 		return 0; /* no normal network profiles on p2p_mgmt interface */
1255b9c547cSRui Paulo 
1263157ba21SRui Paulo 	while (ssid) {
127f05cddf9SRui Paulo 		if (!wpas_network_disabled(wpa_s, ssid))
128f05cddf9SRui Paulo 			count++;
129f05cddf9SRui Paulo 		else
130f05cddf9SRui Paulo 			disabled++;
1313157ba21SRui Paulo 		ssid = ssid->next;
1323157ba21SRui Paulo 	}
133f05cddf9SRui Paulo 	if (wpa_s->conf->cred && wpa_s->conf->interworking &&
134f05cddf9SRui Paulo 	    wpa_s->conf->auto_interworking)
135f05cddf9SRui Paulo 		count++;
136f05cddf9SRui Paulo 	if (count == 0 && disabled > 0) {
137f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
138f05cddf9SRui Paulo 			"networks)", disabled);
139f05cddf9SRui Paulo 	}
140f05cddf9SRui Paulo 	return count;
1413157ba21SRui Paulo }
1423157ba21SRui Paulo 
1433157ba21SRui Paulo 
wpa_supplicant_assoc_try(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)144e28a4053SRui Paulo static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
145e28a4053SRui Paulo 				     struct wpa_ssid *ssid)
146e28a4053SRui Paulo {
14785732ac8SCy Schubert 	int min_temp_disabled = 0;
14885732ac8SCy Schubert 
149e28a4053SRui Paulo 	while (ssid) {
15085732ac8SCy Schubert 		if (!wpas_network_disabled(wpa_s, ssid)) {
15185732ac8SCy Schubert 			int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
15285732ac8SCy Schubert 
15385732ac8SCy Schubert 			if (temp_disabled <= 0)
154e28a4053SRui Paulo 				break;
15585732ac8SCy Schubert 
15685732ac8SCy Schubert 			if (!min_temp_disabled ||
15785732ac8SCy Schubert 			    temp_disabled < min_temp_disabled)
15885732ac8SCy Schubert 				min_temp_disabled = temp_disabled;
15985732ac8SCy Schubert 		}
160e28a4053SRui Paulo 		ssid = ssid->next;
161e28a4053SRui Paulo 	}
162e28a4053SRui Paulo 
163e28a4053SRui Paulo 	/* ap_scan=2 mode - try to associate with each SSID. */
164e28a4053SRui Paulo 	if (ssid == NULL) {
165f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
166e28a4053SRui Paulo 			"end of scan list - go back to beginning");
167e28a4053SRui Paulo 		wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
16885732ac8SCy Schubert 		wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
169e28a4053SRui Paulo 		return;
170e28a4053SRui Paulo 	}
171e28a4053SRui Paulo 	if (ssid->next) {
172e28a4053SRui Paulo 		/* Continue from the next SSID on the next attempt. */
173e28a4053SRui Paulo 		wpa_s->prev_scan_ssid = ssid;
174e28a4053SRui Paulo 	} else {
175e28a4053SRui Paulo 		/* Start from the beginning of the SSID list. */
176e28a4053SRui Paulo 		wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
177e28a4053SRui Paulo 	}
178e28a4053SRui Paulo 	wpa_supplicant_associate(wpa_s, NULL, ssid);
179e28a4053SRui Paulo }
180e28a4053SRui Paulo 
181e28a4053SRui Paulo 
wpas_trigger_scan_cb(struct wpa_radio_work * work,int deinit)1825b9c547cSRui Paulo static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
183e28a4053SRui Paulo {
1845b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s = work->wpa_s;
1855b9c547cSRui Paulo 	struct wpa_driver_scan_params *params = work->ctx;
1865b9c547cSRui Paulo 	int ret;
187e28a4053SRui Paulo 
1885b9c547cSRui Paulo 	if (deinit) {
1895b9c547cSRui Paulo 		if (!work->started) {
1905b9c547cSRui Paulo 			wpa_scan_free_params(params);
191e28a4053SRui Paulo 			return;
192e28a4053SRui Paulo 		}
1935b9c547cSRui Paulo 		wpa_supplicant_notify_scanning(wpa_s, 0);
1945b9c547cSRui Paulo 		wpas_notify_scan_done(wpa_s, 0);
1955b9c547cSRui Paulo 		wpa_s->scan_work = NULL;
196e28a4053SRui Paulo 		return;
197e28a4053SRui Paulo 	}
1985b9c547cSRui Paulo 
199c1d255d3SCy Schubert 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
200c1d255d3SCy Schubert 	    wpa_s->wpa_state <= WPA_SCANNING)
201c1d255d3SCy Schubert 		wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
202c1d255d3SCy Schubert 
2035b9c547cSRui Paulo 	if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
2045b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO,
2055b9c547cSRui Paulo 			"Failed to assign random MAC address for a scan");
206780fb4a2SCy Schubert 		wpa_scan_free_params(params);
207780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
2085b9c547cSRui Paulo 		radio_work_done(work);
2095b9c547cSRui Paulo 		return;
210e28a4053SRui Paulo 	}
2115b9c547cSRui Paulo 
2125b9c547cSRui Paulo 	wpa_supplicant_notify_scanning(wpa_s, 1);
2135b9c547cSRui Paulo 
2145b9c547cSRui Paulo 	if (wpa_s->clear_driver_scan_cache) {
2155b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
2165b9c547cSRui Paulo 			   "Request driver to clear scan cache due to local BSS flush");
2175b9c547cSRui Paulo 		params->only_new_results = 1;
2185b9c547cSRui Paulo 	}
2195b9c547cSRui Paulo 	ret = wpa_drv_scan(wpa_s, params);
22085732ac8SCy Schubert 	/*
22185732ac8SCy Schubert 	 * Store the obtained vendor scan cookie (if any) in wpa_s context.
22285732ac8SCy Schubert 	 * The current design is to allow only one scan request on each
22385732ac8SCy Schubert 	 * interface, hence having this scan cookie stored in wpa_s context is
22485732ac8SCy Schubert 	 * fine for now.
22585732ac8SCy Schubert 	 *
22685732ac8SCy Schubert 	 * Revisit this logic if concurrent scan operations per interface
22785732ac8SCy Schubert 	 * is supported.
22885732ac8SCy Schubert 	 */
22985732ac8SCy Schubert 	if (ret == 0)
23085732ac8SCy Schubert 		wpa_s->curr_scan_cookie = params->scan_cookie;
2315b9c547cSRui Paulo 	wpa_scan_free_params(params);
2325b9c547cSRui Paulo 	work->ctx = NULL;
2335b9c547cSRui Paulo 	if (ret) {
23485732ac8SCy Schubert 		int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
23585732ac8SCy Schubert 			!wpa_s->beacon_rep_data.token;
2365b9c547cSRui Paulo 
2375b9c547cSRui Paulo 		if (wpa_s->disconnected)
2385b9c547cSRui Paulo 			retry = 0;
2395b9c547cSRui Paulo 
240c1d255d3SCy Schubert 		/* do not retry if operation is not supported */
241c1d255d3SCy Schubert 		if (ret == -EOPNOTSUPP)
242c1d255d3SCy Schubert 			retry = 0;
243c1d255d3SCy Schubert 
2445b9c547cSRui Paulo 		wpa_supplicant_notify_scanning(wpa_s, 0);
2455b9c547cSRui Paulo 		wpas_notify_scan_done(wpa_s, 0);
2465b9c547cSRui Paulo 		if (wpa_s->wpa_state == WPA_SCANNING)
2475b9c547cSRui Paulo 			wpa_supplicant_set_state(wpa_s,
2485b9c547cSRui Paulo 						 wpa_s->scan_prev_wpa_state);
2495b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
2505b9c547cSRui Paulo 			ret, retry ? " retry=1" : "");
2515b9c547cSRui Paulo 		radio_work_done(work);
2525b9c547cSRui Paulo 
2535b9c547cSRui Paulo 		if (retry) {
2545b9c547cSRui Paulo 			/* Restore scan_req since we will try to scan again */
2555b9c547cSRui Paulo 			wpa_s->scan_req = wpa_s->last_scan_req;
2565b9c547cSRui Paulo 			wpa_supplicant_req_scan(wpa_s, 1, 0);
25785732ac8SCy Schubert 		} else if (wpa_s->scan_res_handler) {
25885732ac8SCy Schubert 			/* Clear the scan_res_handler */
25985732ac8SCy Schubert 			wpa_s->scan_res_handler = NULL;
2605b9c547cSRui Paulo 		}
26185732ac8SCy Schubert 
26285732ac8SCy Schubert 		if (wpa_s->beacon_rep_data.token)
26385732ac8SCy Schubert 			wpas_rrm_refuse_request(wpa_s);
26485732ac8SCy Schubert 
2655b9c547cSRui Paulo 		return;
2665b9c547cSRui Paulo 	}
2675b9c547cSRui Paulo 
2685b9c547cSRui Paulo 	os_get_reltime(&wpa_s->scan_trigger_time);
2695b9c547cSRui Paulo 	wpa_s->scan_runs++;
2705b9c547cSRui Paulo 	wpa_s->normal_scans++;
2715b9c547cSRui Paulo 	wpa_s->own_scan_requested = 1;
2725b9c547cSRui Paulo 	wpa_s->clear_driver_scan_cache = 0;
2735b9c547cSRui Paulo 	wpa_s->scan_work = work;
274e28a4053SRui Paulo }
275e28a4053SRui Paulo 
276e28a4053SRui Paulo 
277f05cddf9SRui Paulo /**
278f05cddf9SRui Paulo  * wpa_supplicant_trigger_scan - Request driver to start a scan
279f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
280f05cddf9SRui Paulo  * @params: Scan parameters
281f05cddf9SRui Paulo  * Returns: 0 on success, -1 on failure
282f05cddf9SRui Paulo  */
wpa_supplicant_trigger_scan(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)283e28a4053SRui Paulo int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
284e28a4053SRui Paulo 				struct wpa_driver_scan_params *params)
285e28a4053SRui Paulo {
2865b9c547cSRui Paulo 	struct wpa_driver_scan_params *ctx;
287e28a4053SRui Paulo 
2885b9c547cSRui Paulo 	if (wpa_s->scan_work) {
2895b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
2905b9c547cSRui Paulo 		return -1;
291f05cddf9SRui Paulo 	}
292f05cddf9SRui Paulo 
2935b9c547cSRui Paulo 	ctx = wpa_scan_clone_params(params);
294780fb4a2SCy Schubert 	if (!ctx ||
295780fb4a2SCy Schubert 	    radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
2965b9c547cSRui Paulo 	{
2975b9c547cSRui Paulo 		wpa_scan_free_params(ctx);
298780fb4a2SCy Schubert 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
2995b9c547cSRui Paulo 		return -1;
3005b9c547cSRui Paulo 	}
3015b9c547cSRui Paulo 
3025b9c547cSRui Paulo 	return 0;
303f05cddf9SRui Paulo }
304f05cddf9SRui Paulo 
305f05cddf9SRui Paulo 
306f05cddf9SRui Paulo static void
wpa_supplicant_delayed_sched_scan_timeout(void * eloop_ctx,void * timeout_ctx)307f05cddf9SRui Paulo wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
308f05cddf9SRui Paulo {
309f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
310f05cddf9SRui Paulo 
311f05cddf9SRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
312f05cddf9SRui Paulo 
313f05cddf9SRui Paulo 	if (wpa_supplicant_req_sched_scan(wpa_s))
314f05cddf9SRui Paulo 		wpa_supplicant_req_scan(wpa_s, 0, 0);
315f05cddf9SRui Paulo }
316f05cddf9SRui Paulo 
317f05cddf9SRui Paulo 
318f05cddf9SRui Paulo static void
wpa_supplicant_sched_scan_timeout(void * eloop_ctx,void * timeout_ctx)319f05cddf9SRui Paulo wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
320f05cddf9SRui Paulo {
321f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
322f05cddf9SRui Paulo 
323f05cddf9SRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
324f05cddf9SRui Paulo 
325f05cddf9SRui Paulo 	wpa_s->sched_scan_timed_out = 1;
326f05cddf9SRui Paulo 	wpa_supplicant_cancel_sched_scan(wpa_s);
327f05cddf9SRui Paulo }
328f05cddf9SRui Paulo 
329f05cddf9SRui Paulo 
330780fb4a2SCy Schubert static int
wpa_supplicant_start_sched_scan(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)331780fb4a2SCy Schubert wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
332780fb4a2SCy Schubert 				struct wpa_driver_scan_params *params)
333f05cddf9SRui Paulo {
334f05cddf9SRui Paulo 	int ret;
335f05cddf9SRui Paulo 
336f05cddf9SRui Paulo 	wpa_supplicant_notify_scanning(wpa_s, 1);
337780fb4a2SCy Schubert 	ret = wpa_drv_sched_scan(wpa_s, params);
338f05cddf9SRui Paulo 	if (ret)
339f05cddf9SRui Paulo 		wpa_supplicant_notify_scanning(wpa_s, 0);
340f05cddf9SRui Paulo 	else
341f05cddf9SRui Paulo 		wpa_s->sched_scanning = 1;
342f05cddf9SRui Paulo 
343f05cddf9SRui Paulo 	return ret;
344f05cddf9SRui Paulo }
345f05cddf9SRui Paulo 
346f05cddf9SRui Paulo 
wpa_supplicant_stop_sched_scan(struct wpa_supplicant * wpa_s)347780fb4a2SCy Schubert static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
348f05cddf9SRui Paulo {
349f05cddf9SRui Paulo 	int ret;
350f05cddf9SRui Paulo 
351f05cddf9SRui Paulo 	ret = wpa_drv_stop_sched_scan(wpa_s);
352f05cddf9SRui Paulo 	if (ret) {
353f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
354f05cddf9SRui Paulo 		/* TODO: what to do if stopping fails? */
355f05cddf9SRui Paulo 		return -1;
356f05cddf9SRui Paulo 	}
357e28a4053SRui Paulo 
358e28a4053SRui Paulo 	return ret;
359e28a4053SRui Paulo }
360e28a4053SRui Paulo 
361e28a4053SRui Paulo 
362e28a4053SRui Paulo static struct wpa_driver_scan_filter *
wpa_supplicant_build_filter_ssids(struct wpa_config * conf,size_t * num_ssids)363e28a4053SRui Paulo wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
364e28a4053SRui Paulo {
365e28a4053SRui Paulo 	struct wpa_driver_scan_filter *ssids;
366e28a4053SRui Paulo 	struct wpa_ssid *ssid;
367e28a4053SRui Paulo 	size_t count;
368e28a4053SRui Paulo 
369e28a4053SRui Paulo 	*num_ssids = 0;
370e28a4053SRui Paulo 	if (!conf->filter_ssids)
371e28a4053SRui Paulo 		return NULL;
372e28a4053SRui Paulo 
373e28a4053SRui Paulo 	for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
374e28a4053SRui Paulo 		if (ssid->ssid && ssid->ssid_len)
375e28a4053SRui Paulo 			count++;
376e28a4053SRui Paulo 	}
377e28a4053SRui Paulo 	if (count == 0)
378e28a4053SRui Paulo 		return NULL;
3795b9c547cSRui Paulo 	ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
380e28a4053SRui Paulo 	if (ssids == NULL)
381e28a4053SRui Paulo 		return NULL;
382e28a4053SRui Paulo 
383e28a4053SRui Paulo 	for (ssid = conf->ssid; ssid; ssid = ssid->next) {
384e28a4053SRui Paulo 		if (!ssid->ssid || !ssid->ssid_len)
385e28a4053SRui Paulo 			continue;
386e28a4053SRui Paulo 		os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
387e28a4053SRui Paulo 		ssids[*num_ssids].ssid_len = ssid->ssid_len;
388e28a4053SRui Paulo 		(*num_ssids)++;
389e28a4053SRui Paulo 	}
390e28a4053SRui Paulo 
391e28a4053SRui Paulo 	return ssids;
392e28a4053SRui Paulo }
393e28a4053SRui Paulo 
394e28a4053SRui Paulo 
3954b72b91aSCy Schubert #ifdef CONFIG_P2P
is_6ghz_supported(struct wpa_supplicant * wpa_s)3964b72b91aSCy Schubert static bool is_6ghz_supported(struct wpa_supplicant *wpa_s)
3974b72b91aSCy Schubert {
3984b72b91aSCy Schubert 	struct hostapd_channel_data *chnl;
3994b72b91aSCy Schubert 	int i, j;
4004b72b91aSCy Schubert 
4014b72b91aSCy Schubert 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
4024b72b91aSCy Schubert 		if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A) {
4034b72b91aSCy Schubert 			chnl = wpa_s->hw.modes[i].channels;
4044b72b91aSCy Schubert 			for (j = 0; j < wpa_s->hw.modes[i].num_channels; j++) {
4054b72b91aSCy Schubert 				if (chnl[j].flag & HOSTAPD_CHAN_DISABLED)
4064b72b91aSCy Schubert 					continue;
4074b72b91aSCy Schubert 				if (is_6ghz_freq(chnl[j].freq))
4084b72b91aSCy Schubert 					return true;
4094b72b91aSCy Schubert 			}
4104b72b91aSCy Schubert 		}
4114b72b91aSCy Schubert 	}
4124b72b91aSCy Schubert 
4134b72b91aSCy Schubert 	return false;
4144b72b91aSCy Schubert }
4154b72b91aSCy Schubert #endif /* CONFIG_P2P */
4164b72b91aSCy Schubert 
4174b72b91aSCy Schubert 
wpa_supplicant_optimize_freqs(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)418f05cddf9SRui Paulo static void wpa_supplicant_optimize_freqs(
419f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
42039beb93cSSam Leffler {
421f05cddf9SRui Paulo #ifdef CONFIG_P2P
422f05cddf9SRui Paulo 	if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
423f05cddf9SRui Paulo 	    wpa_s->go_params) {
424f05cddf9SRui Paulo 		/* Optimize provisioning state scan based on GO information */
425f05cddf9SRui Paulo 		if (wpa_s->p2p_in_provisioning < 5 &&
426f05cddf9SRui Paulo 		    wpa_s->go_params->freq > 0) {
427f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
428f05cddf9SRui Paulo 				"preferred frequency %d MHz",
429f05cddf9SRui Paulo 				wpa_s->go_params->freq);
4305b9c547cSRui Paulo 			params->freqs = os_calloc(2, sizeof(int));
431f05cddf9SRui Paulo 			if (params->freqs)
432f05cddf9SRui Paulo 				params->freqs[0] = wpa_s->go_params->freq;
433f05cddf9SRui Paulo 		} else if (wpa_s->p2p_in_provisioning < 8 &&
434f05cddf9SRui Paulo 			   wpa_s->go_params->freq_list[0]) {
435f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
436f05cddf9SRui Paulo 				"channels");
437f05cddf9SRui Paulo 			int_array_concat(&params->freqs,
438f05cddf9SRui Paulo 					 wpa_s->go_params->freq_list);
439f05cddf9SRui Paulo 			if (params->freqs)
440f05cddf9SRui Paulo 				int_array_sort_unique(params->freqs);
441f05cddf9SRui Paulo 		}
442f05cddf9SRui Paulo 		wpa_s->p2p_in_provisioning++;
443f05cddf9SRui Paulo 	}
4445b9c547cSRui Paulo 
4455b9c547cSRui Paulo 	if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
4465b9c547cSRui Paulo 		/*
4475b9c547cSRui Paulo 		 * Optimize scan based on GO information during persistent
4485b9c547cSRui Paulo 		 * group reinvocation
4495b9c547cSRui Paulo 		 */
4505b9c547cSRui Paulo 		if (wpa_s->p2p_in_invitation < 5 &&
4515b9c547cSRui Paulo 		    wpa_s->p2p_invite_go_freq > 0) {
4525b9c547cSRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
4535b9c547cSRui Paulo 				wpa_s->p2p_invite_go_freq);
4545b9c547cSRui Paulo 			params->freqs = os_calloc(2, sizeof(int));
4555b9c547cSRui Paulo 			if (params->freqs)
4565b9c547cSRui Paulo 				params->freqs[0] = wpa_s->p2p_invite_go_freq;
4575b9c547cSRui Paulo 		}
4585b9c547cSRui Paulo 		wpa_s->p2p_in_invitation++;
4595b9c547cSRui Paulo 		if (wpa_s->p2p_in_invitation > 20) {
4605b9c547cSRui Paulo 			/*
4615b9c547cSRui Paulo 			 * This should not really happen since the variable is
4625b9c547cSRui Paulo 			 * cleared on group removal, but if it does happen, make
4635b9c547cSRui Paulo 			 * sure we do not get stuck in special invitation scan
4645b9c547cSRui Paulo 			 * mode.
4655b9c547cSRui Paulo 			 */
4665b9c547cSRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
4675b9c547cSRui Paulo 			wpa_s->p2p_in_invitation = 0;
4685b9c547cSRui Paulo 		}
4695b9c547cSRui Paulo 	}
470f05cddf9SRui Paulo #endif /* CONFIG_P2P */
471f05cddf9SRui Paulo 
472f05cddf9SRui Paulo #ifdef CONFIG_WPS
473f05cddf9SRui Paulo 	if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
474f05cddf9SRui Paulo 		/*
475f05cddf9SRui Paulo 		 * Optimize post-provisioning scan based on channel used
476f05cddf9SRui Paulo 		 * during provisioning.
477f05cddf9SRui Paulo 		 */
478f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
479f05cddf9SRui Paulo 			"that was used during provisioning", wpa_s->wps_freq);
4805b9c547cSRui Paulo 		params->freqs = os_calloc(2, sizeof(int));
481f05cddf9SRui Paulo 		if (params->freqs)
482f05cddf9SRui Paulo 			params->freqs[0] = wpa_s->wps_freq;
483f05cddf9SRui Paulo 		wpa_s->after_wps--;
4845b9c547cSRui Paulo 	} else if (wpa_s->after_wps)
4855b9c547cSRui Paulo 		wpa_s->after_wps--;
486f05cddf9SRui Paulo 
487f05cddf9SRui Paulo 	if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
488f05cddf9SRui Paulo 	{
489f05cddf9SRui Paulo 		/* Optimize provisioning scan based on already known channel */
490f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
491f05cddf9SRui Paulo 			wpa_s->wps_freq);
4925b9c547cSRui Paulo 		params->freqs = os_calloc(2, sizeof(int));
493f05cddf9SRui Paulo 		if (params->freqs)
494f05cddf9SRui Paulo 			params->freqs[0] = wpa_s->wps_freq;
495f05cddf9SRui Paulo 		wpa_s->known_wps_freq = 0; /* only do this once */
496f05cddf9SRui Paulo 	}
497f05cddf9SRui Paulo #endif /* CONFIG_WPS */
498f05cddf9SRui Paulo }
499f05cddf9SRui Paulo 
500f05cddf9SRui Paulo 
501f05cddf9SRui Paulo #ifdef CONFIG_INTERWORKING
wpas_add_interworking_elements(struct wpa_supplicant * wpa_s,struct wpabuf * buf)502f05cddf9SRui Paulo static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
503f05cddf9SRui Paulo 					   struct wpabuf *buf)
504f05cddf9SRui Paulo {
505f05cddf9SRui Paulo 	wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
506f05cddf9SRui Paulo 	wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
507f05cddf9SRui Paulo 		      1 + ETH_ALEN);
508f05cddf9SRui Paulo 	wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
509f05cddf9SRui Paulo 	/* No Venue Info */
510f05cddf9SRui Paulo 	if (!is_zero_ether_addr(wpa_s->conf->hessid))
511f05cddf9SRui Paulo 		wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
512f05cddf9SRui Paulo }
513f05cddf9SRui Paulo #endif /* CONFIG_INTERWORKING */
514f05cddf9SRui Paulo 
515f05cddf9SRui Paulo 
51685732ac8SCy Schubert #ifdef CONFIG_MBO
wpas_fils_req_param_add_max_channel(struct wpa_supplicant * wpa_s,struct wpabuf ** ie)51785732ac8SCy Schubert static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
51885732ac8SCy Schubert 						struct wpabuf **ie)
51985732ac8SCy Schubert {
52085732ac8SCy Schubert 	if (wpabuf_resize(ie, 5)) {
52185732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
52285732ac8SCy Schubert 			   "Failed to allocate space for FILS Request Parameters element");
52385732ac8SCy Schubert 		return;
52485732ac8SCy Schubert 	}
52585732ac8SCy Schubert 
52685732ac8SCy Schubert 	/* FILS Request Parameters element */
52785732ac8SCy Schubert 	wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
52885732ac8SCy Schubert 	wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
52985732ac8SCy Schubert 	wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
53085732ac8SCy Schubert 	/* Parameter control bitmap */
53185732ac8SCy Schubert 	wpabuf_put_u8(*ie, 0);
53285732ac8SCy Schubert 	/* Max Channel Time field - contains the value of MaxChannelTime
53385732ac8SCy Schubert 	 * parameter of the MLME-SCAN.request primitive represented in units of
53485732ac8SCy Schubert 	 * TUs, as an unsigned integer. A Max Channel Time field value of 255
53585732ac8SCy Schubert 	 * is used to indicate any duration of more than 254 TUs, or an
53685732ac8SCy Schubert 	 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
53785732ac8SCy Schubert 	 */
53885732ac8SCy Schubert 	wpabuf_put_u8(*ie, 255);
53985732ac8SCy Schubert }
54085732ac8SCy Schubert #endif /* CONFIG_MBO */
54185732ac8SCy Schubert 
54285732ac8SCy Schubert 
wpa_supplicant_set_default_scan_ies(struct wpa_supplicant * wpa_s)543780fb4a2SCy Schubert void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
544780fb4a2SCy Schubert {
545780fb4a2SCy Schubert 	struct wpabuf *default_ies = NULL;
546780fb4a2SCy Schubert 	u8 ext_capab[18];
547c1d255d3SCy Schubert 	int ext_capab_len, frame_id;
548780fb4a2SCy Schubert 	enum wpa_driver_if_type type = WPA_IF_STATION;
549780fb4a2SCy Schubert 
550780fb4a2SCy Schubert #ifdef CONFIG_P2P
551780fb4a2SCy Schubert 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
552780fb4a2SCy Schubert 		type = WPA_IF_P2P_CLIENT;
553780fb4a2SCy Schubert #endif /* CONFIG_P2P */
554780fb4a2SCy Schubert 
555780fb4a2SCy Schubert 	wpa_drv_get_ext_capa(wpa_s, type);
556780fb4a2SCy Schubert 
557780fb4a2SCy Schubert 	ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
558780fb4a2SCy Schubert 					     sizeof(ext_capab));
559780fb4a2SCy Schubert 	if (ext_capab_len > 0 &&
560780fb4a2SCy Schubert 	    wpabuf_resize(&default_ies, ext_capab_len) == 0)
561780fb4a2SCy Schubert 		wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
562780fb4a2SCy Schubert 
563780fb4a2SCy Schubert #ifdef CONFIG_MBO
56485732ac8SCy Schubert 	if (wpa_s->enable_oce & OCE_STA)
56585732ac8SCy Schubert 		wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
56685732ac8SCy Schubert 	/* Send MBO and OCE capabilities */
56785732ac8SCy Schubert 	if (wpabuf_resize(&default_ies, 12) == 0)
568780fb4a2SCy Schubert 		wpas_mbo_scan_ie(wpa_s, default_ies);
569780fb4a2SCy Schubert #endif /* CONFIG_MBO */
570780fb4a2SCy Schubert 
571c1d255d3SCy Schubert 	if (type == WPA_IF_P2P_CLIENT)
572c1d255d3SCy Schubert 		frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
573c1d255d3SCy Schubert 	else
574c1d255d3SCy Schubert 		frame_id = VENDOR_ELEM_PROBE_REQ;
575c1d255d3SCy Schubert 
576c1d255d3SCy Schubert 	if (wpa_s->vendor_elem[frame_id]) {
577c1d255d3SCy Schubert 		size_t len;
578c1d255d3SCy Schubert 
579c1d255d3SCy Schubert 		len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
580c1d255d3SCy Schubert 		if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
581c1d255d3SCy Schubert 			wpabuf_put_buf(default_ies,
582c1d255d3SCy Schubert 				       wpa_s->vendor_elem[frame_id]);
583c1d255d3SCy Schubert 	}
584c1d255d3SCy Schubert 
585780fb4a2SCy Schubert 	if (default_ies)
586780fb4a2SCy Schubert 		wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
587780fb4a2SCy Schubert 					     wpabuf_len(default_ies));
588780fb4a2SCy Schubert 	wpabuf_free(default_ies);
589780fb4a2SCy Schubert }
590780fb4a2SCy Schubert 
591780fb4a2SCy Schubert 
wpa_supplicant_extra_ies(struct wpa_supplicant * wpa_s)592f05cddf9SRui Paulo static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
593f05cddf9SRui Paulo {
594f05cddf9SRui Paulo 	struct wpabuf *extra_ie = NULL;
595325151a3SRui Paulo 	u8 ext_capab[18];
596325151a3SRui Paulo 	int ext_capab_len;
59739beb93cSSam Leffler #ifdef CONFIG_WPS
598e28a4053SRui Paulo 	int wps = 0;
59939beb93cSSam Leffler 	enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
60039beb93cSSam Leffler #endif /* CONFIG_WPS */
601f05cddf9SRui Paulo 
602780fb4a2SCy Schubert #ifdef CONFIG_P2P
603780fb4a2SCy Schubert 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
604780fb4a2SCy Schubert 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
605780fb4a2SCy Schubert 	else
606780fb4a2SCy Schubert #endif /* CONFIG_P2P */
607780fb4a2SCy Schubert 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
608780fb4a2SCy Schubert 
609325151a3SRui Paulo 	ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
610325151a3SRui Paulo 					     sizeof(ext_capab));
611325151a3SRui Paulo 	if (ext_capab_len > 0 &&
612325151a3SRui Paulo 	    wpabuf_resize(&extra_ie, ext_capab_len) == 0)
613325151a3SRui Paulo 		wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
614325151a3SRui Paulo 
615f05cddf9SRui Paulo #ifdef CONFIG_INTERWORKING
616f05cddf9SRui Paulo 	if (wpa_s->conf->interworking &&
617f05cddf9SRui Paulo 	    wpabuf_resize(&extra_ie, 100) == 0)
618f05cddf9SRui Paulo 		wpas_add_interworking_elements(wpa_s, extra_ie);
619f05cddf9SRui Paulo #endif /* CONFIG_INTERWORKING */
620f05cddf9SRui Paulo 
62185732ac8SCy Schubert #ifdef CONFIG_MBO
62285732ac8SCy Schubert 	if (wpa_s->enable_oce & OCE_STA)
62385732ac8SCy Schubert 		wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
62485732ac8SCy Schubert #endif /* CONFIG_MBO */
62585732ac8SCy Schubert 
626f05cddf9SRui Paulo #ifdef CONFIG_WPS
627f05cddf9SRui Paulo 	wps = wpas_wps_in_use(wpa_s, &req_type);
628f05cddf9SRui Paulo 
629f05cddf9SRui Paulo 	if (wps) {
630f05cddf9SRui Paulo 		struct wpabuf *wps_ie;
631f05cddf9SRui Paulo 		wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
632f05cddf9SRui Paulo 						DEV_PW_DEFAULT,
633f05cddf9SRui Paulo 						&wpa_s->wps->dev,
634f05cddf9SRui Paulo 						wpa_s->wps->uuid, req_type,
635f05cddf9SRui Paulo 						0, NULL);
636f05cddf9SRui Paulo 		if (wps_ie) {
637f05cddf9SRui Paulo 			if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
638f05cddf9SRui Paulo 				wpabuf_put_buf(extra_ie, wps_ie);
639f05cddf9SRui Paulo 			wpabuf_free(wps_ie);
640f05cddf9SRui Paulo 		}
641f05cddf9SRui Paulo 	}
642f05cddf9SRui Paulo 
643f05cddf9SRui Paulo #ifdef CONFIG_P2P
644f05cddf9SRui Paulo 	if (wps) {
645f05cddf9SRui Paulo 		size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
646f05cddf9SRui Paulo 		if (wpabuf_resize(&extra_ie, ielen) == 0)
647f05cddf9SRui Paulo 			wpas_p2p_scan_ie(wpa_s, extra_ie);
648f05cddf9SRui Paulo 	}
649f05cddf9SRui Paulo #endif /* CONFIG_P2P */
650f05cddf9SRui Paulo 
6515b9c547cSRui Paulo 	wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
6525b9c547cSRui Paulo 
653f05cddf9SRui Paulo #endif /* CONFIG_WPS */
654f05cddf9SRui Paulo 
6555b9c547cSRui Paulo #ifdef CONFIG_HS20
6564bc52338SCy Schubert 	if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
6574bc52338SCy Schubert 		wpas_hs20_add_indication(extra_ie, -1, 0);
6585b9c547cSRui Paulo #endif /* CONFIG_HS20 */
6595b9c547cSRui Paulo 
660325151a3SRui Paulo #ifdef CONFIG_FST
661325151a3SRui Paulo 	if (wpa_s->fst_ies &&
662325151a3SRui Paulo 	    wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
663325151a3SRui Paulo 		wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
664325151a3SRui Paulo #endif /* CONFIG_FST */
665325151a3SRui Paulo 
666780fb4a2SCy Schubert #ifdef CONFIG_MBO
66785732ac8SCy Schubert 	/* Send MBO and OCE capabilities */
66885732ac8SCy Schubert 	if (wpabuf_resize(&extra_ie, 12) == 0)
669780fb4a2SCy Schubert 		wpas_mbo_scan_ie(wpa_s, extra_ie);
670780fb4a2SCy Schubert #endif /* CONFIG_MBO */
671780fb4a2SCy Schubert 
672780fb4a2SCy Schubert 	if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
673780fb4a2SCy Schubert 		struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
674780fb4a2SCy Schubert 
675780fb4a2SCy Schubert 		if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
676780fb4a2SCy Schubert 			wpabuf_put_buf(extra_ie, buf);
677780fb4a2SCy Schubert 	}
678780fb4a2SCy Schubert 
679f05cddf9SRui Paulo 	return extra_ie;
680f05cddf9SRui Paulo }
681f05cddf9SRui Paulo 
682f05cddf9SRui Paulo 
683f05cddf9SRui Paulo #ifdef CONFIG_P2P
684f05cddf9SRui Paulo 
685f05cddf9SRui Paulo /*
686f05cddf9SRui Paulo  * Check whether there are any enabled networks or credentials that could be
687f05cddf9SRui Paulo  * used for a non-P2P connection.
688f05cddf9SRui Paulo  */
non_p2p_network_enabled(struct wpa_supplicant * wpa_s)689f05cddf9SRui Paulo static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
690f05cddf9SRui Paulo {
691f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
692f05cddf9SRui Paulo 
693f05cddf9SRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
694f05cddf9SRui Paulo 		if (wpas_network_disabled(wpa_s, ssid))
695f05cddf9SRui Paulo 			continue;
696f05cddf9SRui Paulo 		if (!ssid->p2p_group)
697f05cddf9SRui Paulo 			return 1;
698f05cddf9SRui Paulo 	}
699f05cddf9SRui Paulo 
700f05cddf9SRui Paulo 	if (wpa_s->conf->cred && wpa_s->conf->interworking &&
701f05cddf9SRui Paulo 	    wpa_s->conf->auto_interworking)
702f05cddf9SRui Paulo 		return 1;
703f05cddf9SRui Paulo 
704f05cddf9SRui Paulo 	return 0;
705f05cddf9SRui Paulo }
706f05cddf9SRui Paulo 
707f05cddf9SRui Paulo #endif /* CONFIG_P2P */
708f05cddf9SRui Paulo 
709f05cddf9SRui Paulo 
wpa_add_scan_freqs_list(struct wpa_supplicant * wpa_s,enum hostapd_hw_mode band,struct wpa_driver_scan_params * params,bool is_6ghz)710c1d255d3SCy Schubert int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
7115b9c547cSRui Paulo 			    enum hostapd_hw_mode band,
712c1d255d3SCy Schubert 			    struct wpa_driver_scan_params *params, bool is_6ghz)
7135b9c547cSRui Paulo {
7145b9c547cSRui Paulo 	/* Include only supported channels for the specified band */
7155b9c547cSRui Paulo 	struct hostapd_hw_modes *mode;
716c1d255d3SCy Schubert 	int num_chans = 0;
717c1d255d3SCy Schubert 	int *freqs, i;
7185b9c547cSRui Paulo 
719c1d255d3SCy Schubert 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
720c1d255d3SCy Schubert 	if (!mode)
721c1d255d3SCy Schubert 		return -1;
722c1d255d3SCy Schubert 
723c1d255d3SCy Schubert 	if (params->freqs) {
724c1d255d3SCy Schubert 		while (params->freqs[num_chans])
725c1d255d3SCy Schubert 			num_chans++;
7265b9c547cSRui Paulo 	}
7275b9c547cSRui Paulo 
728c1d255d3SCy Schubert 	freqs = os_realloc(params->freqs,
729c1d255d3SCy Schubert 			   (num_chans + mode->num_channels + 1) * sizeof(int));
730c1d255d3SCy Schubert 	if (!freqs)
731c1d255d3SCy Schubert 		return -1;
732c1d255d3SCy Schubert 
733c1d255d3SCy Schubert 	params->freqs = freqs;
734c1d255d3SCy Schubert 	for (i = 0; i < mode->num_channels; i++) {
7355b9c547cSRui Paulo 		if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
7365b9c547cSRui Paulo 			continue;
737c1d255d3SCy Schubert 		params->freqs[num_chans++] = mode->channels[i].freq;
7385b9c547cSRui Paulo 	}
739c1d255d3SCy Schubert 	params->freqs[num_chans] = 0;
740c1d255d3SCy Schubert 
741c1d255d3SCy Schubert 	return 0;
7425b9c547cSRui Paulo }
7435b9c547cSRui Paulo 
7445b9c547cSRui Paulo 
wpa_setband_scan_freqs(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)7455b9c547cSRui Paulo static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
7465b9c547cSRui Paulo 				   struct wpa_driver_scan_params *params)
7475b9c547cSRui Paulo {
7485b9c547cSRui Paulo 	if (wpa_s->hw.modes == NULL)
7495b9c547cSRui Paulo 		return; /* unknown what channels the driver supports */
7505b9c547cSRui Paulo 	if (params->freqs)
7515b9c547cSRui Paulo 		return; /* already using a limited channel set */
752c1d255d3SCy Schubert 
753c1d255d3SCy Schubert 	if (wpa_s->setband_mask & WPA_SETBAND_5G)
754c1d255d3SCy Schubert 		wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
7554b72b91aSCy Schubert 					false);
756c1d255d3SCy Schubert 	if (wpa_s->setband_mask & WPA_SETBAND_2G)
757c1d255d3SCy Schubert 		wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
7584b72b91aSCy Schubert 					false);
759c1d255d3SCy Schubert 	if (wpa_s->setband_mask & WPA_SETBAND_6G)
760c1d255d3SCy Schubert 		wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
7614b72b91aSCy Schubert 					true);
7625b9c547cSRui Paulo }
7635b9c547cSRui Paulo 
7645b9c547cSRui Paulo 
wpa_add_scan_ssid(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,size_t max_ssids,const u8 * ssid,size_t ssid_len)76585732ac8SCy Schubert static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
76685732ac8SCy Schubert 			      struct wpa_driver_scan_params *params,
76785732ac8SCy Schubert 			      size_t max_ssids, const u8 *ssid, size_t ssid_len)
76885732ac8SCy Schubert {
76985732ac8SCy Schubert 	unsigned int j;
77085732ac8SCy Schubert 
77185732ac8SCy Schubert 	for (j = 0; j < params->num_ssids; j++) {
77285732ac8SCy Schubert 		if (params->ssids[j].ssid_len == ssid_len &&
77385732ac8SCy Schubert 		    params->ssids[j].ssid &&
77485732ac8SCy Schubert 		    os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
77585732ac8SCy Schubert 			return; /* already in the list */
77685732ac8SCy Schubert 	}
77785732ac8SCy Schubert 
77885732ac8SCy Schubert 	if (params->num_ssids + 1 > max_ssids) {
77985732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
78085732ac8SCy Schubert 		return;
78185732ac8SCy Schubert 	}
78285732ac8SCy Schubert 
78385732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
78485732ac8SCy Schubert 		   wpa_ssid_txt(ssid, ssid_len));
78585732ac8SCy Schubert 
78685732ac8SCy Schubert 	params->ssids[params->num_ssids].ssid = ssid;
78785732ac8SCy Schubert 	params->ssids[params->num_ssids].ssid_len = ssid_len;
78885732ac8SCy Schubert 	params->num_ssids++;
78985732ac8SCy Schubert }
79085732ac8SCy Schubert 
79185732ac8SCy Schubert 
wpa_add_owe_scan_ssid(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,struct wpa_ssid * ssid,size_t max_ssids)79285732ac8SCy Schubert static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
79385732ac8SCy Schubert 				  struct wpa_driver_scan_params *params,
79485732ac8SCy Schubert 				  struct wpa_ssid *ssid, size_t max_ssids)
79585732ac8SCy Schubert {
79685732ac8SCy Schubert #ifdef CONFIG_OWE
79785732ac8SCy Schubert 	struct wpa_bss *bss;
79885732ac8SCy Schubert 
79985732ac8SCy Schubert 	if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
80085732ac8SCy Schubert 		return;
80185732ac8SCy Schubert 
80285732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
80385732ac8SCy Schubert 		   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
80485732ac8SCy Schubert 
80585732ac8SCy Schubert 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
80685732ac8SCy Schubert 		const u8 *owe, *pos, *end;
80785732ac8SCy Schubert 		const u8 *owe_ssid;
80885732ac8SCy Schubert 		size_t owe_ssid_len;
80985732ac8SCy Schubert 
81085732ac8SCy Schubert 		if (bss->ssid_len != ssid->ssid_len ||
81185732ac8SCy Schubert 		    os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
81285732ac8SCy Schubert 			continue;
81385732ac8SCy Schubert 
81485732ac8SCy Schubert 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
81585732ac8SCy Schubert 		if (!owe || owe[1] < 4)
81685732ac8SCy Schubert 			continue;
81785732ac8SCy Schubert 
81885732ac8SCy Schubert 		pos = owe + 6;
81985732ac8SCy Schubert 		end = owe + 2 + owe[1];
82085732ac8SCy Schubert 
82185732ac8SCy Schubert 		/* Must include BSSID and ssid_len */
82285732ac8SCy Schubert 		if (end - pos < ETH_ALEN + 1)
82385732ac8SCy Schubert 			return;
82485732ac8SCy Schubert 
82585732ac8SCy Schubert 		/* Skip BSSID */
82685732ac8SCy Schubert 		pos += ETH_ALEN;
82785732ac8SCy Schubert 		owe_ssid_len = *pos++;
82885732ac8SCy Schubert 		owe_ssid = pos;
82985732ac8SCy Schubert 
83085732ac8SCy Schubert 		if ((size_t) (end - pos) < owe_ssid_len ||
83185732ac8SCy Schubert 		    owe_ssid_len > SSID_MAX_LEN)
83285732ac8SCy Schubert 			return;
83385732ac8SCy Schubert 
83485732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
83585732ac8SCy Schubert 			   "OWE: scan_ssids: transition mode OWE ssid=%s",
83685732ac8SCy Schubert 			   wpa_ssid_txt(owe_ssid, owe_ssid_len));
83785732ac8SCy Schubert 
83885732ac8SCy Schubert 		wpa_add_scan_ssid(wpa_s, params, max_ssids,
83985732ac8SCy Schubert 				  owe_ssid, owe_ssid_len);
84085732ac8SCy Schubert 		return;
84185732ac8SCy Schubert 	}
84285732ac8SCy Schubert #endif /* CONFIG_OWE */
84385732ac8SCy Schubert }
84485732ac8SCy Schubert 
84585732ac8SCy Schubert 
wpa_set_scan_ssids(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,size_t max_ssids)8465b9c547cSRui Paulo static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
8475b9c547cSRui Paulo 			       struct wpa_driver_scan_params *params,
8485b9c547cSRui Paulo 			       size_t max_ssids)
8495b9c547cSRui Paulo {
8505b9c547cSRui Paulo 	unsigned int i;
8515b9c547cSRui Paulo 	struct wpa_ssid *ssid;
8525b9c547cSRui Paulo 
853780fb4a2SCy Schubert 	/*
854780fb4a2SCy Schubert 	 * For devices with max_ssids greater than 1, leave the last slot empty
855780fb4a2SCy Schubert 	 * for adding the wildcard scan entry.
856780fb4a2SCy Schubert 	 */
857780fb4a2SCy Schubert 	max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
858780fb4a2SCy Schubert 
8595b9c547cSRui Paulo 	for (i = 0; i < wpa_s->scan_id_count; i++) {
8605b9c547cSRui Paulo 		ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
86185732ac8SCy Schubert 		if (!ssid)
8625b9c547cSRui Paulo 			continue;
86385732ac8SCy Schubert 		if (ssid->scan_ssid)
86485732ac8SCy Schubert 			wpa_add_scan_ssid(wpa_s, params, max_ssids,
86585732ac8SCy Schubert 					  ssid->ssid, ssid->ssid_len);
86685732ac8SCy Schubert 		/*
86785732ac8SCy Schubert 		 * Also add the SSID of the OWE BSS, to allow discovery of
86885732ac8SCy Schubert 		 * transition mode APs more quickly.
86985732ac8SCy Schubert 		 */
87085732ac8SCy Schubert 		wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
8715b9c547cSRui Paulo 	}
8725b9c547cSRui Paulo 
8735b9c547cSRui Paulo 	wpa_s->scan_id_count = 0;
8745b9c547cSRui Paulo }
8755b9c547cSRui Paulo 
8765b9c547cSRui Paulo 
wpa_set_ssids_from_scan_req(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,size_t max_ssids)877325151a3SRui Paulo static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
878325151a3SRui Paulo 				       struct wpa_driver_scan_params *params,
879325151a3SRui Paulo 				       size_t max_ssids)
880325151a3SRui Paulo {
881325151a3SRui Paulo 	unsigned int i;
882325151a3SRui Paulo 
883325151a3SRui Paulo 	if (wpa_s->ssids_from_scan_req == NULL ||
884325151a3SRui Paulo 	    wpa_s->num_ssids_from_scan_req == 0)
885325151a3SRui Paulo 		return 0;
886325151a3SRui Paulo 
887325151a3SRui Paulo 	if (wpa_s->num_ssids_from_scan_req > max_ssids) {
888325151a3SRui Paulo 		wpa_s->num_ssids_from_scan_req = max_ssids;
889325151a3SRui Paulo 		wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
890325151a3SRui Paulo 			   (unsigned int) max_ssids);
891325151a3SRui Paulo 	}
892325151a3SRui Paulo 
893325151a3SRui Paulo 	for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
894325151a3SRui Paulo 		params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
895325151a3SRui Paulo 		params->ssids[i].ssid_len =
896325151a3SRui Paulo 			wpa_s->ssids_from_scan_req[i].ssid_len;
897325151a3SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
898325151a3SRui Paulo 				  params->ssids[i].ssid,
899325151a3SRui Paulo 				  params->ssids[i].ssid_len);
900325151a3SRui Paulo 	}
901325151a3SRui Paulo 
902325151a3SRui Paulo 	params->num_ssids = wpa_s->num_ssids_from_scan_req;
903325151a3SRui Paulo 	wpa_s->num_ssids_from_scan_req = 0;
904325151a3SRui Paulo 	return 1;
905325151a3SRui Paulo }
906325151a3SRui Paulo 
907325151a3SRui Paulo 
wpa_supplicant_scan(void * eloop_ctx,void * timeout_ctx)908f05cddf9SRui Paulo static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
909f05cddf9SRui Paulo {
910f05cddf9SRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
911f05cddf9SRui Paulo 	struct wpa_ssid *ssid;
9125b9c547cSRui Paulo 	int ret, p2p_in_prog;
913f05cddf9SRui Paulo 	struct wpabuf *extra_ie = NULL;
914e28a4053SRui Paulo 	struct wpa_driver_scan_params params;
915f05cddf9SRui Paulo 	struct wpa_driver_scan_params *scan_params;
916e28a4053SRui Paulo 	size_t max_ssids;
9175b9c547cSRui Paulo 	int connect_without_scan = 0;
9185b9c547cSRui Paulo 
91985732ac8SCy Schubert 	wpa_s->ignore_post_flush_scan_res = 0;
92039beb93cSSam Leffler 
921f05cddf9SRui Paulo 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
922f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
923f05cddf9SRui Paulo 		return;
924f05cddf9SRui Paulo 	}
925f05cddf9SRui Paulo 
926f05cddf9SRui Paulo 	if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
927f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
9283157ba21SRui Paulo 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
92939beb93cSSam Leffler 		return;
9303157ba21SRui Paulo 	}
93139beb93cSSam Leffler 
9325b9c547cSRui Paulo 	if (wpa_s->scanning) {
9335b9c547cSRui Paulo 		/*
9345b9c547cSRui Paulo 		 * If we are already in scanning state, we shall reschedule the
9355b9c547cSRui Paulo 		 * the incoming scan request.
9365b9c547cSRui Paulo 		 */
9375b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
9385b9c547cSRui Paulo 		wpa_supplicant_req_scan(wpa_s, 1, 0);
9395b9c547cSRui Paulo 		return;
9405b9c547cSRui Paulo 	}
9415b9c547cSRui Paulo 
942f05cddf9SRui Paulo 	if (!wpa_supplicant_enabled_networks(wpa_s) &&
943f05cddf9SRui Paulo 	    wpa_s->scan_req == NORMAL_SCAN_REQ) {
944f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
94539beb93cSSam Leffler 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
94639beb93cSSam Leffler 		return;
94739beb93cSSam Leffler 	}
94839beb93cSSam Leffler 
94939beb93cSSam Leffler 	if (wpa_s->conf->ap_scan != 0 &&
950e28a4053SRui Paulo 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
951f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
95239beb93cSSam Leffler 			"overriding ap_scan configuration");
95339beb93cSSam Leffler 		wpa_s->conf->ap_scan = 0;
954e28a4053SRui Paulo 		wpas_notify_ap_scan_changed(wpa_s);
95539beb93cSSam Leffler 	}
95639beb93cSSam Leffler 
95739beb93cSSam Leffler 	if (wpa_s->conf->ap_scan == 0) {
95839beb93cSSam Leffler 		wpa_supplicant_gen_assoc_event(wpa_s);
95939beb93cSSam Leffler 		return;
96039beb93cSSam Leffler 	}
96139beb93cSSam Leffler 
9625b9c547cSRui Paulo 	ssid = NULL;
9635b9c547cSRui Paulo 	if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
9645b9c547cSRui Paulo 	    wpa_s->connect_without_scan) {
9655b9c547cSRui Paulo 		connect_without_scan = 1;
9665b9c547cSRui Paulo 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
9675b9c547cSRui Paulo 			if (ssid == wpa_s->connect_without_scan)
9685b9c547cSRui Paulo 				break;
9695b9c547cSRui Paulo 		}
9705b9c547cSRui Paulo 	}
9715b9c547cSRui Paulo 
9725b9c547cSRui Paulo 	p2p_in_prog = wpas_p2p_in_progress(wpa_s);
9735b9c547cSRui Paulo 	if (p2p_in_prog && p2p_in_prog != 2 &&
9745b9c547cSRui Paulo 	    (!ssid ||
9755b9c547cSRui Paulo 	     (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
9765b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
977f05cddf9SRui Paulo 		wpa_supplicant_req_scan(wpa_s, 5, 0);
978f05cddf9SRui Paulo 		return;
979f05cddf9SRui Paulo 	}
980f05cddf9SRui Paulo 
98185732ac8SCy Schubert 	/*
98285732ac8SCy Schubert 	 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
98385732ac8SCy Schubert 	 * used for changing modes inside wpa_supplicant (roaming,
98485732ac8SCy Schubert 	 * auto-reconnect, etc). Discarding the scan might hurt these processes.
98585732ac8SCy Schubert 	 * The normal use case for PNO is to suspend the host immediately after
98685732ac8SCy Schubert 	 * starting PNO, so the periodic 100 ms attempts to run the scan do not
98785732ac8SCy Schubert 	 * normally happen in practice multiple times, i.e., this is simply
98885732ac8SCy Schubert 	 * restarting scanning once the host is woken up and PNO stopped.
98985732ac8SCy Schubert 	 */
99085732ac8SCy Schubert 	if (wpa_s->pno || wpa_s->pno_sched_pending) {
99185732ac8SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
99285732ac8SCy Schubert 		wpa_supplicant_req_scan(wpa_s, 0, 100000);
99385732ac8SCy Schubert 		return;
99485732ac8SCy Schubert 	}
99585732ac8SCy Schubert 
996f05cddf9SRui Paulo 	if (wpa_s->conf->ap_scan == 2)
997e28a4053SRui Paulo 		max_ssids = 1;
998e28a4053SRui Paulo 	else {
999e28a4053SRui Paulo 		max_ssids = wpa_s->max_scan_ssids;
1000e28a4053SRui Paulo 		if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1001e28a4053SRui Paulo 			max_ssids = WPAS_MAX_SCAN_SSIDS;
1002e28a4053SRui Paulo 	}
1003e28a4053SRui Paulo 
10045b9c547cSRui Paulo 	wpa_s->last_scan_req = wpa_s->scan_req;
1005f05cddf9SRui Paulo 	wpa_s->scan_req = NORMAL_SCAN_REQ;
1006e28a4053SRui Paulo 
10075b9c547cSRui Paulo 	if (connect_without_scan) {
10085b9c547cSRui Paulo 		wpa_s->connect_without_scan = NULL;
10095b9c547cSRui Paulo 		if (ssid) {
10105b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Start a pre-selected network "
10115b9c547cSRui Paulo 				   "without scan step");
10125b9c547cSRui Paulo 			wpa_supplicant_associate(wpa_s, NULL, ssid);
10135b9c547cSRui Paulo 			return;
10145b9c547cSRui Paulo 		}
10155b9c547cSRui Paulo 	}
10165b9c547cSRui Paulo 
1017e28a4053SRui Paulo 	os_memset(&params, 0, sizeof(params));
1018e28a4053SRui Paulo 
10195b9c547cSRui Paulo 	wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
102039beb93cSSam Leffler 	if (wpa_s->wpa_state == WPA_DISCONNECTED ||
102139beb93cSSam Leffler 	    wpa_s->wpa_state == WPA_INACTIVE)
102239beb93cSSam Leffler 		wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
102339beb93cSSam Leffler 
1024f05cddf9SRui Paulo 	/*
1025f05cddf9SRui Paulo 	 * If autoscan has set its own scanning parameters
1026f05cddf9SRui Paulo 	 */
1027f05cddf9SRui Paulo 	if (wpa_s->autoscan_params != NULL) {
1028f05cddf9SRui Paulo 		scan_params = wpa_s->autoscan_params;
1029f05cddf9SRui Paulo 		goto scan;
1030f05cddf9SRui Paulo 	}
1031f05cddf9SRui Paulo 
1032325151a3SRui Paulo 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1033325151a3SRui Paulo 	    wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1034325151a3SRui Paulo 		wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1035325151a3SRui Paulo 		goto ssid_list_set;
1036325151a3SRui Paulo 	}
1037325151a3SRui Paulo 
1038f05cddf9SRui Paulo #ifdef CONFIG_P2P
1039f05cddf9SRui Paulo 	if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
10405b9c547cSRui Paulo 	    wpa_s->go_params && !wpa_s->conf->passive_scan) {
10415b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
10425b9c547cSRui Paulo 			   wpa_s->p2p_in_provisioning,
10435b9c547cSRui Paulo 			   wpa_s->show_group_started);
1044f05cddf9SRui Paulo 		params.ssids[0].ssid = wpa_s->go_params->ssid;
1045f05cddf9SRui Paulo 		params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1046f05cddf9SRui Paulo 		params.num_ssids = 1;
1047f05cddf9SRui Paulo 		goto ssid_list_set;
1048f05cddf9SRui Paulo 	}
10495b9c547cSRui Paulo 
10505b9c547cSRui Paulo 	if (wpa_s->p2p_in_invitation) {
10515b9c547cSRui Paulo 		if (wpa_s->current_ssid) {
10525b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
10535b9c547cSRui Paulo 			params.ssids[0].ssid = wpa_s->current_ssid->ssid;
10545b9c547cSRui Paulo 			params.ssids[0].ssid_len =
10555b9c547cSRui Paulo 				wpa_s->current_ssid->ssid_len;
10565b9c547cSRui Paulo 			params.num_ssids = 1;
10575b9c547cSRui Paulo 		} else {
10585b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
10595b9c547cSRui Paulo 		}
10605b9c547cSRui Paulo 		goto ssid_list_set;
10615b9c547cSRui Paulo 	}
1062f05cddf9SRui Paulo #endif /* CONFIG_P2P */
1063f05cddf9SRui Paulo 
1064e28a4053SRui Paulo 	/* Find the starting point from which to continue scanning */
106539beb93cSSam Leffler 	ssid = wpa_s->conf->ssid;
1066e28a4053SRui Paulo 	if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
106739beb93cSSam Leffler 		while (ssid) {
106839beb93cSSam Leffler 			if (ssid == wpa_s->prev_scan_ssid) {
106939beb93cSSam Leffler 				ssid = ssid->next;
107039beb93cSSam Leffler 				break;
107139beb93cSSam Leffler 			}
107239beb93cSSam Leffler 			ssid = ssid->next;
107339beb93cSSam Leffler 		}
107439beb93cSSam Leffler 	}
107539beb93cSSam Leffler 
10765b9c547cSRui Paulo 	if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
1077325151a3SRui Paulo #ifdef CONFIG_AP
1078325151a3SRui Paulo 	    !wpa_s->ap_iface &&
1079325151a3SRui Paulo #endif /* CONFIG_AP */
10805b9c547cSRui Paulo 	    wpa_s->conf->ap_scan == 2) {
1081f05cddf9SRui Paulo 		wpa_s->connect_without_scan = NULL;
1082f05cddf9SRui Paulo 		wpa_s->prev_scan_wildcard = 0;
1083e28a4053SRui Paulo 		wpa_supplicant_assoc_try(wpa_s, ssid);
1084e28a4053SRui Paulo 		return;
1085e28a4053SRui Paulo 	} else if (wpa_s->conf->ap_scan == 2) {
108639beb93cSSam Leffler 		/*
1087e28a4053SRui Paulo 		 * User-initiated scan request in ap_scan == 2; scan with
1088e28a4053SRui Paulo 		 * wildcard SSID.
108939beb93cSSam Leffler 		 */
1090e28a4053SRui Paulo 		ssid = NULL;
10915b9c547cSRui Paulo 	} else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
10925b9c547cSRui Paulo 		/*
10935b9c547cSRui Paulo 		 * Perform single-channel single-SSID scan for
10945b9c547cSRui Paulo 		 * reassociate-to-same-BSS operation.
10955b9c547cSRui Paulo 		 */
10965b9c547cSRui Paulo 		/* Setup SSID */
10975b9c547cSRui Paulo 		ssid = wpa_s->current_ssid;
10985b9c547cSRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
10995b9c547cSRui Paulo 				  ssid->ssid, ssid->ssid_len);
11005b9c547cSRui Paulo 		params.ssids[0].ssid = ssid->ssid;
11015b9c547cSRui Paulo 		params.ssids[0].ssid_len = ssid->ssid_len;
11025b9c547cSRui Paulo 		params.num_ssids = 1;
11035b9c547cSRui Paulo 
11045b9c547cSRui Paulo 		/*
11055b9c547cSRui Paulo 		 * Allocate memory for frequency array, allocate one extra
11065b9c547cSRui Paulo 		 * slot for the zero-terminator.
11075b9c547cSRui Paulo 		 */
11085b9c547cSRui Paulo 		params.freqs = os_malloc(sizeof(int) * 2);
1109780fb4a2SCy Schubert 		if (params.freqs) {
11105b9c547cSRui Paulo 			params.freqs[0] = wpa_s->assoc_freq;
11115b9c547cSRui Paulo 			params.freqs[1] = 0;
1112780fb4a2SCy Schubert 		}
11135b9c547cSRui Paulo 
11145b9c547cSRui Paulo 		/*
11155b9c547cSRui Paulo 		 * Reset the reattach flag so that we fall back to full scan if
11165b9c547cSRui Paulo 		 * this scan fails.
11175b9c547cSRui Paulo 		 */
11185b9c547cSRui Paulo 		wpa_s->reattach = 0;
111939beb93cSSam Leffler 	} else {
1120e28a4053SRui Paulo 		struct wpa_ssid *start = ssid, *tssid;
1121e28a4053SRui Paulo 		int freqs_set = 0;
1122e28a4053SRui Paulo 		if (ssid == NULL && max_ssids > 1)
1123e28a4053SRui Paulo 			ssid = wpa_s->conf->ssid;
1124e28a4053SRui Paulo 		while (ssid) {
1125f05cddf9SRui Paulo 			if (!wpas_network_disabled(wpa_s, ssid) &&
1126f05cddf9SRui Paulo 			    ssid->scan_ssid) {
112739beb93cSSam Leffler 				wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
112839beb93cSSam Leffler 						  ssid->ssid, ssid->ssid_len);
1129e28a4053SRui Paulo 				params.ssids[params.num_ssids].ssid =
1130e28a4053SRui Paulo 					ssid->ssid;
1131e28a4053SRui Paulo 				params.ssids[params.num_ssids].ssid_len =
1132e28a4053SRui Paulo 					ssid->ssid_len;
1133e28a4053SRui Paulo 				params.num_ssids++;
1134e28a4053SRui Paulo 				if (params.num_ssids + 1 >= max_ssids)
1135e28a4053SRui Paulo 					break;
1136e28a4053SRui Paulo 			}
113785732ac8SCy Schubert 
113885732ac8SCy Schubert 			if (!wpas_network_disabled(wpa_s, ssid)) {
113985732ac8SCy Schubert 				/*
114085732ac8SCy Schubert 				 * Also add the SSID of the OWE BSS, to allow
114185732ac8SCy Schubert 				 * discovery of transition mode APs more
114285732ac8SCy Schubert 				 * quickly.
114385732ac8SCy Schubert 				 */
114485732ac8SCy Schubert 				wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
114585732ac8SCy Schubert 						      max_ssids);
114685732ac8SCy Schubert 			}
114785732ac8SCy Schubert 
1148e28a4053SRui Paulo 			ssid = ssid->next;
1149e28a4053SRui Paulo 			if (ssid == start)
1150e28a4053SRui Paulo 				break;
1151e28a4053SRui Paulo 			if (ssid == NULL && max_ssids > 1 &&
1152e28a4053SRui Paulo 			    start != wpa_s->conf->ssid)
1153e28a4053SRui Paulo 				ssid = wpa_s->conf->ssid;
1154e28a4053SRui Paulo 		}
1155e28a4053SRui Paulo 
11565b9c547cSRui Paulo 		if (wpa_s->scan_id_count &&
11575b9c547cSRui Paulo 		    wpa_s->last_scan_req == MANUAL_SCAN_REQ)
11585b9c547cSRui Paulo 			wpa_set_scan_ssids(wpa_s, &params, max_ssids);
11595b9c547cSRui Paulo 
11605b9c547cSRui Paulo 		for (tssid = wpa_s->conf->ssid;
11615b9c547cSRui Paulo 		     wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
11625b9c547cSRui Paulo 		     tssid = tssid->next) {
1163f05cddf9SRui Paulo 			if (wpas_network_disabled(wpa_s, tssid))
1164e28a4053SRui Paulo 				continue;
1165c1d255d3SCy Schubert 			if (((params.freqs || !freqs_set) &&
1166c1d255d3SCy Schubert 			     tssid->scan_freq) &&
1167c1d255d3SCy Schubert 			    int_array_len(params.freqs) < 100) {
1168e28a4053SRui Paulo 				int_array_concat(&params.freqs,
1169e28a4053SRui Paulo 						 tssid->scan_freq);
1170e28a4053SRui Paulo 			} else {
1171e28a4053SRui Paulo 				os_free(params.freqs);
1172e28a4053SRui Paulo 				params.freqs = NULL;
1173e28a4053SRui Paulo 			}
1174e28a4053SRui Paulo 			freqs_set = 1;
1175e28a4053SRui Paulo 		}
1176e28a4053SRui Paulo 		int_array_sort_unique(params.freqs);
1177e28a4053SRui Paulo 	}
1178e28a4053SRui Paulo 
1179f05cddf9SRui Paulo 	if (ssid && max_ssids == 1) {
1180f05cddf9SRui Paulo 		/*
1181f05cddf9SRui Paulo 		 * If the driver is limited to 1 SSID at a time interleave
1182f05cddf9SRui Paulo 		 * wildcard SSID scans with specific SSID scans to avoid
1183f05cddf9SRui Paulo 		 * waiting a long time for a wildcard scan.
1184f05cddf9SRui Paulo 		 */
1185f05cddf9SRui Paulo 		if (!wpa_s->prev_scan_wildcard) {
1186f05cddf9SRui Paulo 			params.ssids[0].ssid = NULL;
1187f05cddf9SRui Paulo 			params.ssids[0].ssid_len = 0;
1188f05cddf9SRui Paulo 			wpa_s->prev_scan_wildcard = 1;
1189f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1190f05cddf9SRui Paulo 				"wildcard SSID (Interleave with specific)");
1191f05cddf9SRui Paulo 		} else {
119239beb93cSSam Leffler 			wpa_s->prev_scan_ssid = ssid;
1193f05cddf9SRui Paulo 			wpa_s->prev_scan_wildcard = 0;
1194f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG,
1195f05cddf9SRui Paulo 				"Starting AP scan for specific SSID: %s",
1196f05cddf9SRui Paulo 				wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1197e28a4053SRui Paulo 		}
1198f05cddf9SRui Paulo 	} else if (ssid) {
1199f05cddf9SRui Paulo 		/* max_ssids > 1 */
1200f05cddf9SRui Paulo 
1201f05cddf9SRui Paulo 		wpa_s->prev_scan_ssid = ssid;
1202f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1203f05cddf9SRui Paulo 			"the scan request");
1204f05cddf9SRui Paulo 		params.num_ssids++;
12055b9c547cSRui Paulo 	} else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
12065b9c547cSRui Paulo 		   wpa_s->manual_scan_passive && params.num_ssids == 0) {
12075b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
12085b9c547cSRui Paulo 	} else if (wpa_s->conf->passive_scan) {
12095b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG,
12105b9c547cSRui Paulo 			"Use passive scan based on configuration");
1211e28a4053SRui Paulo 	} else {
1212e28a4053SRui Paulo 		wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1213e28a4053SRui Paulo 		params.num_ssids++;
1214f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1215f05cddf9SRui Paulo 			"SSID");
121639beb93cSSam Leffler 	}
121739beb93cSSam Leffler 
1218325151a3SRui Paulo ssid_list_set:
1219f05cddf9SRui Paulo 	wpa_supplicant_optimize_freqs(wpa_s, &params);
1220f05cddf9SRui Paulo 	extra_ie = wpa_supplicant_extra_ies(wpa_s);
1221e28a4053SRui Paulo 
12225b9c547cSRui Paulo 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
12235b9c547cSRui Paulo 	    wpa_s->manual_scan_only_new) {
12245b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
12255b9c547cSRui Paulo 			   "Request driver to clear scan cache due to manual only_new=1 scan");
12265b9c547cSRui Paulo 		params.only_new_results = 1;
12275b9c547cSRui Paulo 	}
12285b9c547cSRui Paulo 
12295b9c547cSRui Paulo 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
12305b9c547cSRui Paulo 	    wpa_s->manual_scan_freqs) {
12315b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
12325b9c547cSRui Paulo 		params.freqs = wpa_s->manual_scan_freqs;
12335b9c547cSRui Paulo 		wpa_s->manual_scan_freqs = NULL;
12345b9c547cSRui Paulo 	}
1235f05cddf9SRui Paulo 
123685732ac8SCy Schubert 	if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
123785732ac8SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
123885732ac8SCy Schubert 			"Limit select_network scan to specified channels");
123985732ac8SCy Schubert 		params.freqs = wpa_s->select_network_scan_freqs;
124085732ac8SCy Schubert 		wpa_s->select_network_scan_freqs = NULL;
124185732ac8SCy Schubert 	}
124285732ac8SCy Schubert 
1243f05cddf9SRui Paulo 	if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1244f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1245f05cddf9SRui Paulo 			"generated frequency list");
1246f05cddf9SRui Paulo 		params.freqs = wpa_s->next_scan_freqs;
1247f05cddf9SRui Paulo 	} else
1248f05cddf9SRui Paulo 		os_free(wpa_s->next_scan_freqs);
1249f05cddf9SRui Paulo 	wpa_s->next_scan_freqs = NULL;
12505b9c547cSRui Paulo 	wpa_setband_scan_freqs(wpa_s, &params);
12515b9c547cSRui Paulo 
12525b9c547cSRui Paulo 	/* See if user specified frequencies. If so, scan only those. */
1253c1d255d3SCy Schubert 	if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1254c1d255d3SCy Schubert 	    wpa_s->conf->initial_freq_list && !params.freqs) {
1255c1d255d3SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
1256c1d255d3SCy Schubert 			"Optimize scan based on conf->initial_freq_list");
1257c1d255d3SCy Schubert 		int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1258c1d255d3SCy Schubert 	} else if (wpa_s->conf->freq_list && !params.freqs) {
12595b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG,
12605b9c547cSRui Paulo 			"Optimize scan based on conf->freq_list");
12615b9c547cSRui Paulo 		int_array_concat(&params.freqs, wpa_s->conf->freq_list);
12625b9c547cSRui Paulo 	}
12635b9c547cSRui Paulo 
12645b9c547cSRui Paulo 	/* Use current associated channel? */
12655b9c547cSRui Paulo 	if (wpa_s->conf->scan_cur_freq && !params.freqs) {
12665b9c547cSRui Paulo 		unsigned int num = wpa_s->num_multichan_concurrent;
12675b9c547cSRui Paulo 
12685b9c547cSRui Paulo 		params.freqs = os_calloc(num + 1, sizeof(int));
12695b9c547cSRui Paulo 		if (params.freqs) {
12705b9c547cSRui Paulo 			num = get_shared_radio_freqs(wpa_s, params.freqs, num);
12715b9c547cSRui Paulo 			if (num > 0) {
12725b9c547cSRui Paulo 				wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
12735b9c547cSRui Paulo 					"current operating channels since "
12745b9c547cSRui Paulo 					"scan_cur_freq is enabled");
12755b9c547cSRui Paulo 			} else {
12765b9c547cSRui Paulo 				os_free(params.freqs);
12775b9c547cSRui Paulo 				params.freqs = NULL;
12785b9c547cSRui Paulo 			}
12795b9c547cSRui Paulo 		}
12805b9c547cSRui Paulo 	}
128139beb93cSSam Leffler 
128285732ac8SCy Schubert #ifdef CONFIG_MBO
128385732ac8SCy Schubert 	if (wpa_s->enable_oce & OCE_STA)
128485732ac8SCy Schubert 		params.oce_scan = 1;
128585732ac8SCy Schubert #endif /* CONFIG_MBO */
128685732ac8SCy Schubert 
1287e28a4053SRui Paulo 	params.filter_ssids = wpa_supplicant_build_filter_ssids(
1288e28a4053SRui Paulo 		wpa_s->conf, &params.num_filter_ssids);
1289f05cddf9SRui Paulo 	if (extra_ie) {
1290f05cddf9SRui Paulo 		params.extra_ies = wpabuf_head(extra_ie);
1291f05cddf9SRui Paulo 		params.extra_ies_len = wpabuf_len(extra_ie);
1292f05cddf9SRui Paulo 	}
12933157ba21SRui Paulo 
1294f05cddf9SRui Paulo #ifdef CONFIG_P2P
12955b9c547cSRui Paulo 	if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
1296f05cddf9SRui Paulo 	    (wpa_s->show_group_started && wpa_s->go_params)) {
1297f05cddf9SRui Paulo 		/*
1298f05cddf9SRui Paulo 		 * The interface may not yet be in P2P mode, so we have to
1299f05cddf9SRui Paulo 		 * explicitly request P2P probe to disable CCK rates.
1300f05cddf9SRui Paulo 		 */
1301f05cddf9SRui Paulo 		params.p2p_probe = 1;
1302f05cddf9SRui Paulo 	}
1303f05cddf9SRui Paulo #endif /* CONFIG_P2P */
130439beb93cSSam Leffler 
130585732ac8SCy Schubert 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
1306c1d255d3SCy Schubert 	    wpa_s->wpa_state <= WPA_SCANNING)
1307c1d255d3SCy Schubert 		wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
13085b9c547cSRui Paulo 
1309780fb4a2SCy Schubert 	if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1310780fb4a2SCy Schubert 		struct wpa_bss *bss;
1311780fb4a2SCy Schubert 
1312780fb4a2SCy Schubert 		params.bssid = wpa_s->next_scan_bssid;
1313780fb4a2SCy Schubert 		bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1314c1d255d3SCy Schubert 		if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1315c1d255d3SCy Schubert 		    bss && bss->ssid_len && params.num_ssids == 1 &&
1316780fb4a2SCy Schubert 		    params.ssids[0].ssid_len == 0) {
1317780fb4a2SCy Schubert 			params.ssids[0].ssid = bss->ssid;
1318780fb4a2SCy Schubert 			params.ssids[0].ssid_len = bss->ssid_len;
1319780fb4a2SCy Schubert 			wpa_dbg(wpa_s, MSG_DEBUG,
1320780fb4a2SCy Schubert 				"Scan a previously specified BSSID " MACSTR
1321780fb4a2SCy Schubert 				" and SSID %s",
1322780fb4a2SCy Schubert 				MAC2STR(params.bssid),
1323780fb4a2SCy Schubert 				wpa_ssid_txt(bss->ssid, bss->ssid_len));
1324780fb4a2SCy Schubert 		} else {
1325780fb4a2SCy Schubert 			wpa_dbg(wpa_s, MSG_DEBUG,
1326780fb4a2SCy Schubert 				"Scan a previously specified BSSID " MACSTR,
1327780fb4a2SCy Schubert 				MAC2STR(params.bssid));
1328780fb4a2SCy Schubert 		}
1329780fb4a2SCy Schubert 	}
1330780fb4a2SCy Schubert 
1331f05cddf9SRui Paulo 	scan_params = &params;
1332f05cddf9SRui Paulo 
1333f05cddf9SRui Paulo scan:
1334f05cddf9SRui Paulo #ifdef CONFIG_P2P
1335f05cddf9SRui Paulo 	/*
1336f05cddf9SRui Paulo 	 * If the driver does not support multi-channel concurrency and a
1337f05cddf9SRui Paulo 	 * virtual interface that shares the same radio with the wpa_s interface
1338f05cddf9SRui Paulo 	 * is operating there may not be need to scan other channels apart from
1339f05cddf9SRui Paulo 	 * the current operating channel on the other virtual interface. Filter
1340f05cddf9SRui Paulo 	 * out other channels in case we are trying to find a connection for a
1341f05cddf9SRui Paulo 	 * station interface when we are not configured to prefer station
1342f05cddf9SRui Paulo 	 * connection and a concurrent operation is already in process.
1343f05cddf9SRui Paulo 	 */
13445b9c547cSRui Paulo 	if (wpa_s->scan_for_connection &&
13455b9c547cSRui Paulo 	    wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1346f05cddf9SRui Paulo 	    !scan_params->freqs && !params.freqs &&
1347f05cddf9SRui Paulo 	    wpas_is_p2p_prioritized(wpa_s) &&
1348f05cddf9SRui Paulo 	    wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1349f05cddf9SRui Paulo 	    non_p2p_network_enabled(wpa_s)) {
13505b9c547cSRui Paulo 		unsigned int num = wpa_s->num_multichan_concurrent;
13515b9c547cSRui Paulo 
13525b9c547cSRui Paulo 		params.freqs = os_calloc(num + 1, sizeof(int));
13535b9c547cSRui Paulo 		if (params.freqs) {
13545b9c547cSRui Paulo 			num = get_shared_radio_freqs(wpa_s, params.freqs, num);
13555b9c547cSRui Paulo 			if (num > 0 && num == wpa_s->num_multichan_concurrent) {
13565b9c547cSRui Paulo 				wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
13575b9c547cSRui Paulo 			} else {
13585b9c547cSRui Paulo 				os_free(params.freqs);
13595b9c547cSRui Paulo 				params.freqs = NULL;
13605b9c547cSRui Paulo 			}
1361f05cddf9SRui Paulo 		}
1362f05cddf9SRui Paulo 	}
13634b72b91aSCy Schubert 
13644b72b91aSCy Schubert 	if (!params.freqs &&
13654b72b91aSCy Schubert 	    (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning) &&
13664b72b91aSCy Schubert 	    !is_p2p_allow_6ghz(wpa_s->global->p2p) &&
13674b72b91aSCy Schubert 	    is_6ghz_supported(wpa_s)) {
13684b72b91aSCy Schubert 		int i;
13694b72b91aSCy Schubert 
13704b72b91aSCy Schubert 		/* Exclude 5 GHz channels from the full scan for P2P connection
13714b72b91aSCy Schubert 		 * since the 6 GHz band is disabled for P2P uses. */
13724b72b91aSCy Schubert 		wpa_printf(MSG_DEBUG,
13734b72b91aSCy Schubert 			   "P2P: 6 GHz disabled - update the scan frequency list");
13744b72b91aSCy Schubert 		for (i = 0; i < wpa_s->hw.num_modes; i++) {
13754b72b91aSCy Schubert 			if (wpa_s->hw.modes[i].num_channels == 0)
13764b72b91aSCy Schubert 				continue;
13774b72b91aSCy Schubert 			if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211G)
13784b72b91aSCy Schubert 				wpa_add_scan_freqs_list(
13794b72b91aSCy Schubert 					wpa_s, HOSTAPD_MODE_IEEE80211G,
13804b72b91aSCy Schubert 					&params, false);
13814b72b91aSCy Schubert 			if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A)
13824b72b91aSCy Schubert 				wpa_add_scan_freqs_list(
13834b72b91aSCy Schubert 					wpa_s, HOSTAPD_MODE_IEEE80211A,
13844b72b91aSCy Schubert 					&params, false);
13854b72b91aSCy Schubert 			if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211AD)
13864b72b91aSCy Schubert 				wpa_add_scan_freqs_list(
13874b72b91aSCy Schubert 					wpa_s, HOSTAPD_MODE_IEEE80211AD,
13884b72b91aSCy Schubert 					&params, false);
13894b72b91aSCy Schubert 		}
13904b72b91aSCy Schubert 	}
1391f05cddf9SRui Paulo #endif /* CONFIG_P2P */
1392f05cddf9SRui Paulo 
1393f05cddf9SRui Paulo 	ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
1394f05cddf9SRui Paulo 
13955b9c547cSRui Paulo 	if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
13965b9c547cSRui Paulo 	    !wpa_s->manual_scan_freqs) {
13975b9c547cSRui Paulo 		/* Restore manual_scan_freqs for the next attempt */
13985b9c547cSRui Paulo 		wpa_s->manual_scan_freqs = params.freqs;
13995b9c547cSRui Paulo 		params.freqs = NULL;
14005b9c547cSRui Paulo 	}
14015b9c547cSRui Paulo 
1402f05cddf9SRui Paulo 	wpabuf_free(extra_ie);
1403e28a4053SRui Paulo 	os_free(params.freqs);
1404e28a4053SRui Paulo 	os_free(params.filter_ssids);
1405c1d255d3SCy Schubert 	os_free(params.mac_addr);
140639beb93cSSam Leffler 
140739beb93cSSam Leffler 	if (ret) {
1408f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
14095b9c547cSRui Paulo 		if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
14105b9c547cSRui Paulo 			wpa_supplicant_set_state(wpa_s,
14115b9c547cSRui Paulo 						 wpa_s->scan_prev_wpa_state);
1412f05cddf9SRui Paulo 		/* Restore scan_req since we will try to scan again */
14135b9c547cSRui Paulo 		wpa_s->scan_req = wpa_s->last_scan_req;
1414e28a4053SRui Paulo 		wpa_supplicant_req_scan(wpa_s, 1, 0);
1415f05cddf9SRui Paulo 	} else {
1416f05cddf9SRui Paulo 		wpa_s->scan_for_connection = 0;
14175b9c547cSRui Paulo #ifdef CONFIG_INTERWORKING
14185b9c547cSRui Paulo 		wpa_s->interworking_fast_assoc_tried = 0;
14195b9c547cSRui Paulo #endif /* CONFIG_INTERWORKING */
1420c1d255d3SCy Schubert 		wpa_s->next_scan_bssid_wildcard_ssid = 0;
1421780fb4a2SCy Schubert 		if (params.bssid)
1422780fb4a2SCy Schubert 			os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1423e28a4053SRui Paulo 	}
142439beb93cSSam Leffler }
142539beb93cSSam Leffler 
142639beb93cSSam Leffler 
wpa_supplicant_update_scan_int(struct wpa_supplicant * wpa_s,int sec)14275b9c547cSRui Paulo void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
14285b9c547cSRui Paulo {
14295b9c547cSRui Paulo 	struct os_reltime remaining, new_int;
14305b9c547cSRui Paulo 	int cancelled;
14315b9c547cSRui Paulo 
14325b9c547cSRui Paulo 	cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
14335b9c547cSRui Paulo 					     &remaining);
14345b9c547cSRui Paulo 
14355b9c547cSRui Paulo 	new_int.sec = sec;
14365b9c547cSRui Paulo 	new_int.usec = 0;
14375b9c547cSRui Paulo 	if (cancelled && os_reltime_before(&remaining, &new_int)) {
14385b9c547cSRui Paulo 		new_int.sec = remaining.sec;
14395b9c547cSRui Paulo 		new_int.usec = remaining.usec;
14405b9c547cSRui Paulo 	}
14415b9c547cSRui Paulo 
14425b9c547cSRui Paulo 	if (cancelled) {
14435b9c547cSRui Paulo 		eloop_register_timeout(new_int.sec, new_int.usec,
14445b9c547cSRui Paulo 				       wpa_supplicant_scan, wpa_s, NULL);
14455b9c547cSRui Paulo 	}
14465b9c547cSRui Paulo 	wpa_s->scan_interval = sec;
14475b9c547cSRui Paulo }
14485b9c547cSRui Paulo 
14495b9c547cSRui Paulo 
145039beb93cSSam Leffler /**
145139beb93cSSam Leffler  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
145239beb93cSSam Leffler  * @wpa_s: Pointer to wpa_supplicant data
145339beb93cSSam Leffler  * @sec: Number of seconds after which to scan
145439beb93cSSam Leffler  * @usec: Number of microseconds after which to scan
145539beb93cSSam Leffler  *
145639beb93cSSam Leffler  * This function is used to schedule a scan for neighboring access points after
145739beb93cSSam Leffler  * the specified time.
145839beb93cSSam Leffler  */
wpa_supplicant_req_scan(struct wpa_supplicant * wpa_s,int sec,int usec)145939beb93cSSam Leffler void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
146039beb93cSSam Leffler {
14615b9c547cSRui Paulo 	int res;
146239beb93cSSam Leffler 
14635b9c547cSRui Paulo 	if (wpa_s->p2p_mgmt) {
14645b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG,
14655b9c547cSRui Paulo 			"Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
14665b9c547cSRui Paulo 			sec, usec);
146739beb93cSSam Leffler 		return;
146839beb93cSSam Leffler 	}
146939beb93cSSam Leffler 
14705b9c547cSRui Paulo 	res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
14715b9c547cSRui Paulo 				    NULL);
14725b9c547cSRui Paulo 	if (res == 1) {
14735b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
147439beb93cSSam Leffler 			sec, usec);
14755b9c547cSRui Paulo 	} else if (res == 0) {
14765b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
14775b9c547cSRui Paulo 			sec, usec);
14785b9c547cSRui Paulo 	} else {
14795b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
14805b9c547cSRui Paulo 			sec, usec);
148139beb93cSSam Leffler 		eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
148239beb93cSSam Leffler 	}
14835b9c547cSRui Paulo }
148439beb93cSSam Leffler 
148539beb93cSSam Leffler 
148639beb93cSSam Leffler /**
1487f05cddf9SRui Paulo  * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1488f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
1489f05cddf9SRui Paulo  * @sec: Number of seconds after which to scan
1490f05cddf9SRui Paulo  * @usec: Number of microseconds after which to scan
1491f05cddf9SRui Paulo  * Returns: 0 on success or -1 otherwise
1492f05cddf9SRui Paulo  *
1493f05cddf9SRui Paulo  * This function is used to schedule periodic scans for neighboring
1494f05cddf9SRui Paulo  * access points after the specified time.
1495f05cddf9SRui Paulo  */
wpa_supplicant_delayed_sched_scan(struct wpa_supplicant * wpa_s,int sec,int usec)1496f05cddf9SRui Paulo int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1497f05cddf9SRui Paulo 				      int sec, int usec)
1498f05cddf9SRui Paulo {
1499f05cddf9SRui Paulo 	if (!wpa_s->sched_scan_supported)
1500f05cddf9SRui Paulo 		return -1;
1501f05cddf9SRui Paulo 
1502f05cddf9SRui Paulo 	eloop_register_timeout(sec, usec,
1503f05cddf9SRui Paulo 			       wpa_supplicant_delayed_sched_scan_timeout,
1504f05cddf9SRui Paulo 			       wpa_s, NULL);
1505f05cddf9SRui Paulo 
1506f05cddf9SRui Paulo 	return 0;
1507f05cddf9SRui Paulo }
1508f05cddf9SRui Paulo 
1509f05cddf9SRui Paulo 
151085732ac8SCy Schubert static void
wpa_scan_set_relative_rssi_params(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)151185732ac8SCy Schubert wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
151285732ac8SCy Schubert 				  struct wpa_driver_scan_params *params)
151385732ac8SCy Schubert {
151485732ac8SCy Schubert 	if (wpa_s->wpa_state != WPA_COMPLETED ||
151585732ac8SCy Schubert 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
151685732ac8SCy Schubert 	    wpa_s->srp.relative_rssi_set == 0)
151785732ac8SCy Schubert 		return;
151885732ac8SCy Schubert 
151985732ac8SCy Schubert 	params->relative_rssi_set = 1;
152085732ac8SCy Schubert 	params->relative_rssi = wpa_s->srp.relative_rssi;
152185732ac8SCy Schubert 
152285732ac8SCy Schubert 	if (wpa_s->srp.relative_adjust_rssi == 0)
152385732ac8SCy Schubert 		return;
152485732ac8SCy Schubert 
152585732ac8SCy Schubert 	params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
152685732ac8SCy Schubert 	params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
152785732ac8SCy Schubert }
152885732ac8SCy Schubert 
152985732ac8SCy Schubert 
1530f05cddf9SRui Paulo /**
1531f05cddf9SRui Paulo  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1532f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
1533f05cddf9SRui Paulo  * Returns: 0 is sched_scan was started or -1 otherwise
1534f05cddf9SRui Paulo  *
1535f05cddf9SRui Paulo  * This function is used to schedule periodic scans for neighboring
1536f05cddf9SRui Paulo  * access points repeating the scan continuously.
1537f05cddf9SRui Paulo  */
wpa_supplicant_req_sched_scan(struct wpa_supplicant * wpa_s)1538f05cddf9SRui Paulo int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1539f05cddf9SRui Paulo {
1540f05cddf9SRui Paulo 	struct wpa_driver_scan_params params;
1541f05cddf9SRui Paulo 	struct wpa_driver_scan_params *scan_params;
1542f05cddf9SRui Paulo 	enum wpa_states prev_state;
1543f05cddf9SRui Paulo 	struct wpa_ssid *ssid = NULL;
1544f05cddf9SRui Paulo 	struct wpabuf *extra_ie = NULL;
1545f05cddf9SRui Paulo 	int ret;
1546f05cddf9SRui Paulo 	unsigned int max_sched_scan_ssids;
1547f05cddf9SRui Paulo 	int wildcard = 0;
1548f05cddf9SRui Paulo 	int need_ssids;
1549780fb4a2SCy Schubert 	struct sched_scan_plan scan_plan;
1550f05cddf9SRui Paulo 
1551f05cddf9SRui Paulo 	if (!wpa_s->sched_scan_supported)
1552f05cddf9SRui Paulo 		return -1;
1553f05cddf9SRui Paulo 
1554f05cddf9SRui Paulo 	if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1555f05cddf9SRui Paulo 		max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1556f05cddf9SRui Paulo 	else
1557f05cddf9SRui Paulo 		max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1558f05cddf9SRui Paulo 	if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1559f05cddf9SRui Paulo 		return -1;
1560f05cddf9SRui Paulo 
1561780fb4a2SCy Schubert 	wpa_s->sched_scan_stop_req = 0;
1562780fb4a2SCy Schubert 
1563f05cddf9SRui Paulo 	if (wpa_s->sched_scanning) {
1564f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1565f05cddf9SRui Paulo 		return 0;
1566f05cddf9SRui Paulo 	}
1567f05cddf9SRui Paulo 
1568f05cddf9SRui Paulo 	need_ssids = 0;
1569f05cddf9SRui Paulo 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1570f05cddf9SRui Paulo 		if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1571f05cddf9SRui Paulo 			/* Use wildcard SSID to find this network */
1572f05cddf9SRui Paulo 			wildcard = 1;
1573f05cddf9SRui Paulo 		} else if (!wpas_network_disabled(wpa_s, ssid) &&
1574f05cddf9SRui Paulo 			   ssid->ssid_len)
1575f05cddf9SRui Paulo 			need_ssids++;
1576f05cddf9SRui Paulo 
1577f05cddf9SRui Paulo #ifdef CONFIG_WPS
1578f05cddf9SRui Paulo 		if (!wpas_network_disabled(wpa_s, ssid) &&
1579f05cddf9SRui Paulo 		    ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1580f05cddf9SRui Paulo 			/*
1581f05cddf9SRui Paulo 			 * Normal scan is more reliable and faster for WPS
1582f05cddf9SRui Paulo 			 * operations and since these are for short periods of
1583f05cddf9SRui Paulo 			 * time, the benefit of trying to use sched_scan would
1584f05cddf9SRui Paulo 			 * be limited.
1585f05cddf9SRui Paulo 			 */
1586f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1587f05cddf9SRui Paulo 				"sched_scan for WPS");
1588f05cddf9SRui Paulo 			return -1;
1589f05cddf9SRui Paulo 		}
1590f05cddf9SRui Paulo #endif /* CONFIG_WPS */
1591f05cddf9SRui Paulo 	}
1592f05cddf9SRui Paulo 	if (wildcard)
1593f05cddf9SRui Paulo 		need_ssids++;
1594f05cddf9SRui Paulo 
1595f05cddf9SRui Paulo 	if (wpa_s->normal_scans < 3 &&
1596f05cddf9SRui Paulo 	    (need_ssids <= wpa_s->max_scan_ssids ||
1597f05cddf9SRui Paulo 	     wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1598f05cddf9SRui Paulo 		/*
1599f05cddf9SRui Paulo 		 * When normal scan can speed up operations, use that for the
1600f05cddf9SRui Paulo 		 * first operations before starting the sched_scan to allow
1601f05cddf9SRui Paulo 		 * user space sleep more. We do this only if the normal scan
1602f05cddf9SRui Paulo 		 * has functionality that is suitable for this or if the
1603f05cddf9SRui Paulo 		 * sched_scan does not have better support for multiple SSIDs.
1604f05cddf9SRui Paulo 		 */
1605f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1606f05cddf9SRui Paulo 			"sched_scan for initial scans (normal_scans=%d)",
1607f05cddf9SRui Paulo 			wpa_s->normal_scans);
1608f05cddf9SRui Paulo 		return -1;
1609f05cddf9SRui Paulo 	}
1610f05cddf9SRui Paulo 
1611f05cddf9SRui Paulo 	os_memset(&params, 0, sizeof(params));
1612f05cddf9SRui Paulo 
1613f05cddf9SRui Paulo 	/* If we can't allocate space for the filters, we just don't filter */
16145b9c547cSRui Paulo 	params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1615f05cddf9SRui Paulo 					sizeof(struct wpa_driver_scan_filter));
1616f05cddf9SRui Paulo 
1617f05cddf9SRui Paulo 	prev_state = wpa_s->wpa_state;
1618f05cddf9SRui Paulo 	if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1619f05cddf9SRui Paulo 	    wpa_s->wpa_state == WPA_INACTIVE)
1620f05cddf9SRui Paulo 		wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1621f05cddf9SRui Paulo 
1622f05cddf9SRui Paulo 	if (wpa_s->autoscan_params != NULL) {
1623f05cddf9SRui Paulo 		scan_params = wpa_s->autoscan_params;
1624f05cddf9SRui Paulo 		goto scan;
1625f05cddf9SRui Paulo 	}
1626f05cddf9SRui Paulo 
1627f05cddf9SRui Paulo 	/* Find the starting point from which to continue scanning */
1628f05cddf9SRui Paulo 	ssid = wpa_s->conf->ssid;
1629f05cddf9SRui Paulo 	if (wpa_s->prev_sched_ssid) {
1630f05cddf9SRui Paulo 		while (ssid) {
1631f05cddf9SRui Paulo 			if (ssid == wpa_s->prev_sched_ssid) {
1632f05cddf9SRui Paulo 				ssid = ssid->next;
1633f05cddf9SRui Paulo 				break;
1634f05cddf9SRui Paulo 			}
1635f05cddf9SRui Paulo 			ssid = ssid->next;
1636f05cddf9SRui Paulo 		}
1637f05cddf9SRui Paulo 	}
1638f05cddf9SRui Paulo 
1639f05cddf9SRui Paulo 	if (!ssid || !wpa_s->prev_sched_ssid) {
1640f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1641f05cddf9SRui Paulo 		wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1642f05cddf9SRui Paulo 		wpa_s->first_sched_scan = 1;
1643f05cddf9SRui Paulo 		ssid = wpa_s->conf->ssid;
1644f05cddf9SRui Paulo 		wpa_s->prev_sched_ssid = ssid;
1645f05cddf9SRui Paulo 	}
1646f05cddf9SRui Paulo 
1647f05cddf9SRui Paulo 	if (wildcard) {
1648f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1649f05cddf9SRui Paulo 		params.num_ssids++;
1650f05cddf9SRui Paulo 	}
1651f05cddf9SRui Paulo 
1652f05cddf9SRui Paulo 	while (ssid) {
1653f05cddf9SRui Paulo 		if (wpas_network_disabled(wpa_s, ssid))
1654f05cddf9SRui Paulo 			goto next;
1655f05cddf9SRui Paulo 
1656f05cddf9SRui Paulo 		if (params.num_filter_ssids < wpa_s->max_match_sets &&
1657f05cddf9SRui Paulo 		    params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1658f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1659f05cddf9SRui Paulo 				wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1660f05cddf9SRui Paulo 			os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1661f05cddf9SRui Paulo 				  ssid->ssid, ssid->ssid_len);
1662f05cddf9SRui Paulo 			params.filter_ssids[params.num_filter_ssids].ssid_len =
1663f05cddf9SRui Paulo 				ssid->ssid_len;
1664f05cddf9SRui Paulo 			params.num_filter_ssids++;
1665f05cddf9SRui Paulo 		} else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1666f05cddf9SRui Paulo 		{
1667f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1668f05cddf9SRui Paulo 				"filter for sched_scan - drop filter");
1669f05cddf9SRui Paulo 			os_free(params.filter_ssids);
1670f05cddf9SRui Paulo 			params.filter_ssids = NULL;
1671f05cddf9SRui Paulo 			params.num_filter_ssids = 0;
1672f05cddf9SRui Paulo 		}
1673f05cddf9SRui Paulo 
1674f05cddf9SRui Paulo 		if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1675f05cddf9SRui Paulo 			if (params.num_ssids == max_sched_scan_ssids)
1676f05cddf9SRui Paulo 				break; /* only room for broadcast SSID */
1677f05cddf9SRui Paulo 			wpa_dbg(wpa_s, MSG_DEBUG,
1678f05cddf9SRui Paulo 				"add to active scan ssid: %s",
1679f05cddf9SRui Paulo 				wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1680f05cddf9SRui Paulo 			params.ssids[params.num_ssids].ssid =
1681f05cddf9SRui Paulo 				ssid->ssid;
1682f05cddf9SRui Paulo 			params.ssids[params.num_ssids].ssid_len =
1683f05cddf9SRui Paulo 				ssid->ssid_len;
1684f05cddf9SRui Paulo 			params.num_ssids++;
1685f05cddf9SRui Paulo 			if (params.num_ssids >= max_sched_scan_ssids) {
1686f05cddf9SRui Paulo 				wpa_s->prev_sched_ssid = ssid;
1687f05cddf9SRui Paulo 				do {
1688f05cddf9SRui Paulo 					ssid = ssid->next;
1689f05cddf9SRui Paulo 				} while (ssid &&
1690f05cddf9SRui Paulo 					 (wpas_network_disabled(wpa_s, ssid) ||
1691f05cddf9SRui Paulo 					  !ssid->scan_ssid));
1692f05cddf9SRui Paulo 				break;
1693f05cddf9SRui Paulo 			}
1694f05cddf9SRui Paulo 		}
1695f05cddf9SRui Paulo 
1696f05cddf9SRui Paulo 	next:
1697f05cddf9SRui Paulo 		wpa_s->prev_sched_ssid = ssid;
1698f05cddf9SRui Paulo 		ssid = ssid->next;
1699f05cddf9SRui Paulo 	}
1700f05cddf9SRui Paulo 
1701f05cddf9SRui Paulo 	if (params.num_filter_ssids == 0) {
1702f05cddf9SRui Paulo 		os_free(params.filter_ssids);
1703f05cddf9SRui Paulo 		params.filter_ssids = NULL;
1704f05cddf9SRui Paulo 	}
1705f05cddf9SRui Paulo 
1706f05cddf9SRui Paulo 	extra_ie = wpa_supplicant_extra_ies(wpa_s);
1707f05cddf9SRui Paulo 	if (extra_ie) {
1708f05cddf9SRui Paulo 		params.extra_ies = wpabuf_head(extra_ie);
1709f05cddf9SRui Paulo 		params.extra_ies_len = wpabuf_len(extra_ie);
1710f05cddf9SRui Paulo 	}
1711f05cddf9SRui Paulo 
17125b9c547cSRui Paulo 	if (wpa_s->conf->filter_rssi)
17135b9c547cSRui Paulo 		params.filter_rssi = wpa_s->conf->filter_rssi;
17145b9c547cSRui Paulo 
17155b9c547cSRui Paulo 	/* See if user specified frequencies. If so, scan only those. */
17165b9c547cSRui Paulo 	if (wpa_s->conf->freq_list && !params.freqs) {
17175b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG,
17185b9c547cSRui Paulo 			"Optimize scan based on conf->freq_list");
17195b9c547cSRui Paulo 		int_array_concat(&params.freqs, wpa_s->conf->freq_list);
17205b9c547cSRui Paulo 	}
17215b9c547cSRui Paulo 
172285732ac8SCy Schubert #ifdef CONFIG_MBO
172385732ac8SCy Schubert 	if (wpa_s->enable_oce & OCE_STA)
172485732ac8SCy Schubert 		params.oce_scan = 1;
172585732ac8SCy Schubert #endif /* CONFIG_MBO */
172685732ac8SCy Schubert 
1727f05cddf9SRui Paulo 	scan_params = &params;
1728f05cddf9SRui Paulo 
1729f05cddf9SRui Paulo scan:
1730780fb4a2SCy Schubert 	wpa_s->sched_scan_timed_out = 0;
1731780fb4a2SCy Schubert 
1732780fb4a2SCy Schubert 	/*
1733780fb4a2SCy Schubert 	 * We cannot support multiple scan plans if the scan request includes
1734780fb4a2SCy Schubert 	 * too many SSID's, so in this case use only the last scan plan and make
1735780fb4a2SCy Schubert 	 * it run infinitely. It will be stopped by the timeout.
1736780fb4a2SCy Schubert 	 */
1737780fb4a2SCy Schubert 	if (wpa_s->sched_scan_plans_num == 1 ||
1738780fb4a2SCy Schubert 	    (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1739780fb4a2SCy Schubert 		params.sched_scan_plans = wpa_s->sched_scan_plans;
1740780fb4a2SCy Schubert 		params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1741780fb4a2SCy Schubert 	} else if (wpa_s->sched_scan_plans_num > 1) {
1742780fb4a2SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
1743780fb4a2SCy Schubert 			"Too many SSIDs. Default to using single scheduled_scan plan");
1744780fb4a2SCy Schubert 		params.sched_scan_plans =
1745780fb4a2SCy Schubert 			&wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1746780fb4a2SCy Schubert 						 1];
1747780fb4a2SCy Schubert 		params.sched_scan_plans_num = 1;
1748780fb4a2SCy Schubert 	} else {
1749780fb4a2SCy Schubert 		if (wpa_s->conf->sched_scan_interval)
1750780fb4a2SCy Schubert 			scan_plan.interval = wpa_s->conf->sched_scan_interval;
1751780fb4a2SCy Schubert 		else
1752780fb4a2SCy Schubert 			scan_plan.interval = 10;
1753780fb4a2SCy Schubert 
1754780fb4a2SCy Schubert 		if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1755780fb4a2SCy Schubert 			wpa_printf(MSG_WARNING,
1756780fb4a2SCy Schubert 				   "Scan interval too long(%u), use the maximum allowed(%u)",
1757780fb4a2SCy Schubert 				   scan_plan.interval,
1758780fb4a2SCy Schubert 				   wpa_s->max_sched_scan_plan_interval);
1759780fb4a2SCy Schubert 			scan_plan.interval =
1760780fb4a2SCy Schubert 				wpa_s->max_sched_scan_plan_interval;
1761780fb4a2SCy Schubert 		}
1762780fb4a2SCy Schubert 
1763780fb4a2SCy Schubert 		scan_plan.iterations = 0;
1764780fb4a2SCy Schubert 		params.sched_scan_plans = &scan_plan;
1765780fb4a2SCy Schubert 		params.sched_scan_plans_num = 1;
1766780fb4a2SCy Schubert 	}
1767780fb4a2SCy Schubert 
176885732ac8SCy Schubert 	params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
176985732ac8SCy Schubert 
1770f05cddf9SRui Paulo 	if (ssid || !wpa_s->first_sched_scan) {
1771f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG,
177285732ac8SCy Schubert 			"Starting sched scan after %u seconds: interval %u timeout %d",
177385732ac8SCy Schubert 			params.sched_scan_start_delay,
1774780fb4a2SCy Schubert 			params.sched_scan_plans[0].interval,
1775780fb4a2SCy Schubert 			wpa_s->sched_scan_timeout);
1776f05cddf9SRui Paulo 	} else {
177785732ac8SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
177885732ac8SCy Schubert 			"Starting sched scan after %u seconds (no timeout)",
177985732ac8SCy Schubert 			params.sched_scan_start_delay);
1780f05cddf9SRui Paulo 	}
1781f05cddf9SRui Paulo 
17825b9c547cSRui Paulo 	wpa_setband_scan_freqs(wpa_s, scan_params);
17835b9c547cSRui Paulo 
178485732ac8SCy Schubert 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
1785c1d255d3SCy Schubert 	    wpa_s->wpa_state <= WPA_SCANNING)
1786c1d255d3SCy Schubert 		wpa_setup_mac_addr_rand_params(&params,
1787c1d255d3SCy Schubert 					       wpa_s->mac_addr_sched_scan);
17885b9c547cSRui Paulo 
178985732ac8SCy Schubert 	wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
179085732ac8SCy Schubert 
1791780fb4a2SCy Schubert 	ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1792f05cddf9SRui Paulo 	wpabuf_free(extra_ie);
1793f05cddf9SRui Paulo 	os_free(params.filter_ssids);
1794c1d255d3SCy Schubert 	os_free(params.mac_addr);
1795f05cddf9SRui Paulo 	if (ret) {
1796f05cddf9SRui Paulo 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1797f05cddf9SRui Paulo 		if (prev_state != wpa_s->wpa_state)
1798f05cddf9SRui Paulo 			wpa_supplicant_set_state(wpa_s, prev_state);
1799f05cddf9SRui Paulo 		return ret;
1800f05cddf9SRui Paulo 	}
1801f05cddf9SRui Paulo 
1802f05cddf9SRui Paulo 	/* If we have more SSIDs to scan, add a timeout so we scan them too */
1803f05cddf9SRui Paulo 	if (ssid || !wpa_s->first_sched_scan) {
1804f05cddf9SRui Paulo 		wpa_s->sched_scan_timed_out = 0;
1805f05cddf9SRui Paulo 		eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1806f05cddf9SRui Paulo 				       wpa_supplicant_sched_scan_timeout,
1807f05cddf9SRui Paulo 				       wpa_s, NULL);
1808f05cddf9SRui Paulo 		wpa_s->first_sched_scan = 0;
1809f05cddf9SRui Paulo 		wpa_s->sched_scan_timeout /= 2;
1810780fb4a2SCy Schubert 		params.sched_scan_plans[0].interval *= 2;
1811780fb4a2SCy Schubert 		if ((unsigned int) wpa_s->sched_scan_timeout <
1812780fb4a2SCy Schubert 		    params.sched_scan_plans[0].interval ||
1813780fb4a2SCy Schubert 		    params.sched_scan_plans[0].interval >
1814780fb4a2SCy Schubert 		    wpa_s->max_sched_scan_plan_interval) {
1815780fb4a2SCy Schubert 			params.sched_scan_plans[0].interval = 10;
18165b9c547cSRui Paulo 			wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1817f05cddf9SRui Paulo 		}
18185b9c547cSRui Paulo 	}
18195b9c547cSRui Paulo 
18205b9c547cSRui Paulo 	/* If there is no more ssids, start next time from the beginning */
18215b9c547cSRui Paulo 	if (!ssid)
18225b9c547cSRui Paulo 		wpa_s->prev_sched_ssid = NULL;
1823f05cddf9SRui Paulo 
1824f05cddf9SRui Paulo 	return 0;
1825f05cddf9SRui Paulo }
1826f05cddf9SRui Paulo 
1827f05cddf9SRui Paulo 
1828f05cddf9SRui Paulo /**
182939beb93cSSam Leffler  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
183039beb93cSSam Leffler  * @wpa_s: Pointer to wpa_supplicant data
183139beb93cSSam Leffler  *
183239beb93cSSam Leffler  * This function is used to cancel a scan request scheduled with
183339beb93cSSam Leffler  * wpa_supplicant_req_scan().
183439beb93cSSam Leffler  */
wpa_supplicant_cancel_scan(struct wpa_supplicant * wpa_s)183539beb93cSSam Leffler void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
183639beb93cSSam Leffler {
1837f05cddf9SRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
183839beb93cSSam Leffler 	eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
183939beb93cSSam Leffler }
18403157ba21SRui Paulo 
18413157ba21SRui Paulo 
1842f05cddf9SRui Paulo /**
18435b9c547cSRui Paulo  * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
18445b9c547cSRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
18455b9c547cSRui Paulo  *
18465b9c547cSRui Paulo  * This function is used to stop a delayed scheduled scan.
18475b9c547cSRui Paulo  */
wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant * wpa_s)18485b9c547cSRui Paulo void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
18495b9c547cSRui Paulo {
18505b9c547cSRui Paulo 	if (!wpa_s->sched_scan_supported)
18515b9c547cSRui Paulo 		return;
18525b9c547cSRui Paulo 
18535b9c547cSRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
18545b9c547cSRui Paulo 	eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
18555b9c547cSRui Paulo 			     wpa_s, NULL);
18565b9c547cSRui Paulo }
18575b9c547cSRui Paulo 
18585b9c547cSRui Paulo 
18595b9c547cSRui Paulo /**
1860f05cddf9SRui Paulo  * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1861f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
1862f05cddf9SRui Paulo  *
1863f05cddf9SRui Paulo  * This function is used to stop a periodic scheduled scan.
1864f05cddf9SRui Paulo  */
wpa_supplicant_cancel_sched_scan(struct wpa_supplicant * wpa_s)1865f05cddf9SRui Paulo void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1866f05cddf9SRui Paulo {
1867f05cddf9SRui Paulo 	if (!wpa_s->sched_scanning)
1868f05cddf9SRui Paulo 		return;
1869f05cddf9SRui Paulo 
1870780fb4a2SCy Schubert 	if (wpa_s->sched_scanning)
1871780fb4a2SCy Schubert 		wpa_s->sched_scan_stop_req = 1;
1872780fb4a2SCy Schubert 
1873f05cddf9SRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1874f05cddf9SRui Paulo 	eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1875f05cddf9SRui Paulo 	wpa_supplicant_stop_sched_scan(wpa_s);
1876f05cddf9SRui Paulo }
1877f05cddf9SRui Paulo 
1878f05cddf9SRui Paulo 
1879f05cddf9SRui Paulo /**
1880f05cddf9SRui Paulo  * wpa_supplicant_notify_scanning - Indicate possible scan state change
1881f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
1882f05cddf9SRui Paulo  * @scanning: Whether scanning is currently in progress
1883f05cddf9SRui Paulo  *
1884f05cddf9SRui Paulo  * This function is to generate scanning notifycations. It is called whenever
1885f05cddf9SRui Paulo  * there may have been a change in scanning (scan started, completed, stopped).
1886f05cddf9SRui Paulo  * wpas_notify_scanning() is called whenever the scanning state changed from the
1887f05cddf9SRui Paulo  * previously notified state.
1888f05cddf9SRui Paulo  */
wpa_supplicant_notify_scanning(struct wpa_supplicant * wpa_s,int scanning)18893157ba21SRui Paulo void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
18903157ba21SRui Paulo 				    int scanning)
18913157ba21SRui Paulo {
18923157ba21SRui Paulo 	if (wpa_s->scanning != scanning) {
18933157ba21SRui Paulo 		wpa_s->scanning = scanning;
1894e28a4053SRui Paulo 		wpas_notify_scanning(wpa_s);
18953157ba21SRui Paulo 	}
18963157ba21SRui Paulo }
18973157ba21SRui Paulo 
1898e28a4053SRui Paulo 
wpa_scan_get_max_rate(const struct wpa_scan_res * res)1899e28a4053SRui Paulo static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1900e28a4053SRui Paulo {
1901e28a4053SRui Paulo 	int rate = 0;
1902e28a4053SRui Paulo 	const u8 *ie;
1903e28a4053SRui Paulo 	int i;
1904e28a4053SRui Paulo 
1905e28a4053SRui Paulo 	ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1906e28a4053SRui Paulo 	for (i = 0; ie && i < ie[1]; i++) {
1907e28a4053SRui Paulo 		if ((ie[i + 2] & 0x7f) > rate)
1908e28a4053SRui Paulo 			rate = ie[i + 2] & 0x7f;
1909e28a4053SRui Paulo 	}
1910e28a4053SRui Paulo 
1911e28a4053SRui Paulo 	ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1912e28a4053SRui Paulo 	for (i = 0; ie && i < ie[1]; i++) {
1913e28a4053SRui Paulo 		if ((ie[i + 2] & 0x7f) > rate)
1914e28a4053SRui Paulo 			rate = ie[i + 2] & 0x7f;
1915e28a4053SRui Paulo 	}
1916e28a4053SRui Paulo 
1917e28a4053SRui Paulo 	return rate;
1918e28a4053SRui Paulo }
1919e28a4053SRui Paulo 
1920e28a4053SRui Paulo 
1921f05cddf9SRui Paulo /**
1922f05cddf9SRui Paulo  * wpa_scan_get_ie - Fetch a specified information element from a scan result
1923f05cddf9SRui Paulo  * @res: Scan result entry
1924f05cddf9SRui Paulo  * @ie: Information element identitifier (WLAN_EID_*)
1925f05cddf9SRui Paulo  * Returns: Pointer to the information element (id field) or %NULL if not found
1926f05cddf9SRui Paulo  *
1927f05cddf9SRui Paulo  * This function returns the first matching information element in the scan
1928f05cddf9SRui Paulo  * result.
1929f05cddf9SRui Paulo  */
wpa_scan_get_ie(const struct wpa_scan_res * res,u8 ie)1930e28a4053SRui Paulo const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1931e28a4053SRui Paulo {
193285732ac8SCy Schubert 	size_t ie_len = res->ie_len;
193385732ac8SCy Schubert 
193485732ac8SCy Schubert 	/* Use the Beacon frame IEs if res->ie_len is not available */
193585732ac8SCy Schubert 	if (!ie_len)
193685732ac8SCy Schubert 		ie_len = res->beacon_ie_len;
193785732ac8SCy Schubert 
193885732ac8SCy Schubert 	return get_ie((const u8 *) (res + 1), ie_len, ie);
1939e28a4053SRui Paulo }
1940e28a4053SRui Paulo 
1941e28a4053SRui Paulo 
1942f05cddf9SRui Paulo /**
1943f05cddf9SRui Paulo  * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1944f05cddf9SRui Paulo  * @res: Scan result entry
1945f05cddf9SRui Paulo  * @vendor_type: Vendor type (four octets starting the IE payload)
1946f05cddf9SRui Paulo  * Returns: Pointer to the information element (id field) or %NULL if not found
1947f05cddf9SRui Paulo  *
1948f05cddf9SRui Paulo  * This function returns the first matching information element in the scan
1949f05cddf9SRui Paulo  * result.
1950f05cddf9SRui Paulo  */
wpa_scan_get_vendor_ie(const struct wpa_scan_res * res,u32 vendor_type)1951e28a4053SRui Paulo const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1952e28a4053SRui Paulo 				  u32 vendor_type)
1953e28a4053SRui Paulo {
1954c1d255d3SCy Schubert 	const u8 *ies;
1955c1d255d3SCy Schubert 	const struct element *elem;
1956e28a4053SRui Paulo 
1957c1d255d3SCy Schubert 	ies = (const u8 *) (res + 1);
1958e28a4053SRui Paulo 
1959c1d255d3SCy Schubert 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
1960c1d255d3SCy Schubert 		if (elem->datalen >= 4 &&
1961c1d255d3SCy Schubert 		    vendor_type == WPA_GET_BE32(elem->data))
1962c1d255d3SCy Schubert 			return &elem->id;
1963e28a4053SRui Paulo 	}
1964e28a4053SRui Paulo 
1965e28a4053SRui Paulo 	return NULL;
1966e28a4053SRui Paulo }
1967e28a4053SRui Paulo 
1968e28a4053SRui Paulo 
1969f05cddf9SRui Paulo /**
19705b9c547cSRui Paulo  * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
19715b9c547cSRui Paulo  * @res: Scan result entry
19725b9c547cSRui Paulo  * @vendor_type: Vendor type (four octets starting the IE payload)
19735b9c547cSRui Paulo  * Returns: Pointer to the information element (id field) or %NULL if not found
19745b9c547cSRui Paulo  *
19755b9c547cSRui Paulo  * This function returns the first matching information element in the scan
19765b9c547cSRui Paulo  * result.
19775b9c547cSRui Paulo  *
19785b9c547cSRui Paulo  * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
19795b9c547cSRui Paulo  * from Beacon frames instead of either Beacon or Probe Response frames.
19805b9c547cSRui Paulo  */
wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res * res,u32 vendor_type)19815b9c547cSRui Paulo const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
19825b9c547cSRui Paulo 					 u32 vendor_type)
19835b9c547cSRui Paulo {
1984c1d255d3SCy Schubert 	const u8 *ies;
1985c1d255d3SCy Schubert 	const struct element *elem;
19865b9c547cSRui Paulo 
19875b9c547cSRui Paulo 	if (res->beacon_ie_len == 0)
19885b9c547cSRui Paulo 		return NULL;
19895b9c547cSRui Paulo 
1990c1d255d3SCy Schubert 	ies = (const u8 *) (res + 1);
1991c1d255d3SCy Schubert 	ies += res->ie_len;
19925b9c547cSRui Paulo 
1993c1d255d3SCy Schubert 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1994c1d255d3SCy Schubert 			    res->beacon_ie_len) {
1995c1d255d3SCy Schubert 		if (elem->datalen >= 4 &&
1996c1d255d3SCy Schubert 		    vendor_type == WPA_GET_BE32(elem->data))
1997c1d255d3SCy Schubert 			return &elem->id;
19985b9c547cSRui Paulo 	}
19995b9c547cSRui Paulo 
20005b9c547cSRui Paulo 	return NULL;
20015b9c547cSRui Paulo }
20025b9c547cSRui Paulo 
20035b9c547cSRui Paulo 
20045b9c547cSRui Paulo /**
2005f05cddf9SRui Paulo  * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2006f05cddf9SRui Paulo  * @res: Scan result entry
2007f05cddf9SRui Paulo  * @vendor_type: Vendor type (four octets starting the IE payload)
2008f05cddf9SRui Paulo  * Returns: Pointer to the information element payload or %NULL if not found
2009f05cddf9SRui Paulo  *
2010f05cddf9SRui Paulo  * This function returns concatenated payload of possibly fragmented vendor
2011f05cddf9SRui Paulo  * specific information elements in the scan result. The caller is responsible
2012f05cddf9SRui Paulo  * for freeing the returned buffer.
2013f05cddf9SRui Paulo  */
wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res * res,u32 vendor_type)2014e28a4053SRui Paulo struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2015e28a4053SRui Paulo 					     u32 vendor_type)
2016e28a4053SRui Paulo {
2017e28a4053SRui Paulo 	struct wpabuf *buf;
2018e28a4053SRui Paulo 	const u8 *end, *pos;
2019e28a4053SRui Paulo 
2020e28a4053SRui Paulo 	buf = wpabuf_alloc(res->ie_len);
2021e28a4053SRui Paulo 	if (buf == NULL)
2022e28a4053SRui Paulo 		return NULL;
2023e28a4053SRui Paulo 
2024e28a4053SRui Paulo 	pos = (const u8 *) (res + 1);
2025e28a4053SRui Paulo 	end = pos + res->ie_len;
2026e28a4053SRui Paulo 
2027780fb4a2SCy Schubert 	while (end - pos > 1) {
2028c1d255d3SCy Schubert 		u8 ie, len;
2029c1d255d3SCy Schubert 
2030c1d255d3SCy Schubert 		ie = pos[0];
2031c1d255d3SCy Schubert 		len = pos[1];
2032c1d255d3SCy Schubert 		if (len > end - pos - 2)
2033e28a4053SRui Paulo 			break;
2034c1d255d3SCy Schubert 		pos += 2;
2035c1d255d3SCy Schubert 		if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2036c1d255d3SCy Schubert 		    vendor_type == WPA_GET_BE32(pos))
2037c1d255d3SCy Schubert 			wpabuf_put_data(buf, pos + 4, len - 4);
2038c1d255d3SCy Schubert 		pos += len;
2039e28a4053SRui Paulo 	}
2040e28a4053SRui Paulo 
2041e28a4053SRui Paulo 	if (wpabuf_len(buf) == 0) {
2042e28a4053SRui Paulo 		wpabuf_free(buf);
2043e28a4053SRui Paulo 		buf = NULL;
2044e28a4053SRui Paulo 	}
2045e28a4053SRui Paulo 
2046e28a4053SRui Paulo 	return buf;
2047e28a4053SRui Paulo }
2048e28a4053SRui Paulo 
2049e28a4053SRui Paulo 
2050e28a4053SRui Paulo /* Compare function for sorting scan results. Return >0 if @b is considered
2051e28a4053SRui Paulo  * better. */
wpa_scan_result_compar(const void * a,const void * b)2052e28a4053SRui Paulo static int wpa_scan_result_compar(const void *a, const void *b)
2053e28a4053SRui Paulo {
2054f05cddf9SRui Paulo #define MIN(a,b) a < b ? a : b
2055e28a4053SRui Paulo 	struct wpa_scan_res **_wa = (void *) a;
2056e28a4053SRui Paulo 	struct wpa_scan_res **_wb = (void *) b;
2057e28a4053SRui Paulo 	struct wpa_scan_res *wa = *_wa;
2058e28a4053SRui Paulo 	struct wpa_scan_res *wb = *_wb;
20595b9c547cSRui Paulo 	int wpa_a, wpa_b;
20605b9c547cSRui Paulo 	int snr_a, snr_b, snr_a_full, snr_b_full;
2061e28a4053SRui Paulo 
2062e28a4053SRui Paulo 	/* WPA/WPA2 support preferred */
2063e28a4053SRui Paulo 	wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2064e28a4053SRui Paulo 		wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2065e28a4053SRui Paulo 	wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2066e28a4053SRui Paulo 		wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2067e28a4053SRui Paulo 
2068e28a4053SRui Paulo 	if (wpa_b && !wpa_a)
2069e28a4053SRui Paulo 		return 1;
2070e28a4053SRui Paulo 	if (!wpa_b && wpa_a)
2071e28a4053SRui Paulo 		return -1;
2072e28a4053SRui Paulo 
2073e28a4053SRui Paulo 	/* privacy support preferred */
2074e28a4053SRui Paulo 	if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2075e28a4053SRui Paulo 	    (wb->caps & IEEE80211_CAP_PRIVACY))
2076e28a4053SRui Paulo 		return 1;
2077e28a4053SRui Paulo 	if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2078e28a4053SRui Paulo 	    (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2079e28a4053SRui Paulo 		return -1;
2080e28a4053SRui Paulo 
20815b9c547cSRui Paulo 	if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
20825b9c547cSRui Paulo 		snr_a_full = wa->snr;
20835b9c547cSRui Paulo 		snr_a = MIN(wa->snr, GREAT_SNR);
20845b9c547cSRui Paulo 		snr_b_full = wb->snr;
2085325151a3SRui Paulo 		snr_b = MIN(wb->snr, GREAT_SNR);
2086f05cddf9SRui Paulo 	} else {
20875b9c547cSRui Paulo 		/* Level is not in dBm, so we can't calculate
20885b9c547cSRui Paulo 		 * SNR. Just use raw level (units unknown). */
20895b9c547cSRui Paulo 		snr_a = snr_a_full = wa->level;
20905b9c547cSRui Paulo 		snr_b = snr_b_full = wb->level;
2091f05cddf9SRui Paulo 	}
2092f05cddf9SRui Paulo 
2093c1d255d3SCy Schubert 	/* If SNR is close, decide by max rate or frequency band. For cases
2094c1d255d3SCy Schubert 	 * involving the 6 GHz band, use the throughput estimate irrespective
2095c1d255d3SCy Schubert 	 * of the SNR difference since the LPI/VLP rules may result in
2096c1d255d3SCy Schubert 	 * significant differences in SNR for cases where the estimated
2097c1d255d3SCy Schubert 	 * throughput can be considerably higher with the lower SNR. */
2098c1d255d3SCy Schubert 	if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2099c1d255d3SCy Schubert 			       is_6ghz_freq(wa->freq) ||
2100c1d255d3SCy Schubert 			       is_6ghz_freq(wb->freq))) {
21015b9c547cSRui Paulo 		if (wa->est_throughput != wb->est_throughput)
21024bc52338SCy Schubert 			return (int) wb->est_throughput -
21034bc52338SCy Schubert 				(int) wa->est_throughput;
210485732ac8SCy Schubert 	}
210585732ac8SCy Schubert 	if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
210685732ac8SCy Schubert 	    (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
2107c1d255d3SCy Schubert 		if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2108c1d255d3SCy Schubert 			return is_6ghz_freq(wa->freq) ? -1 : 1;
2109f05cddf9SRui Paulo 		if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2110f05cddf9SRui Paulo 			return IS_5GHZ(wa->freq) ? -1 : 1;
2111e28a4053SRui Paulo 	}
2112e28a4053SRui Paulo 
2113f05cddf9SRui Paulo 	/* all things being equal, use SNR; if SNRs are
2114f05cddf9SRui Paulo 	 * identical, use quality values since some drivers may only report
2115f05cddf9SRui Paulo 	 * that value and leave the signal level zero */
21165b9c547cSRui Paulo 	if (snr_b_full == snr_a_full)
2117f05cddf9SRui Paulo 		return wb->qual - wa->qual;
21185b9c547cSRui Paulo 	return snr_b_full - snr_a_full;
2119f05cddf9SRui Paulo #undef MIN
2120f05cddf9SRui Paulo }
2121f05cddf9SRui Paulo 
2122f05cddf9SRui Paulo 
2123f05cddf9SRui Paulo #ifdef CONFIG_WPS
2124f05cddf9SRui Paulo /* Compare function for sorting scan results when searching a WPS AP for
2125f05cddf9SRui Paulo  * provisioning. Return >0 if @b is considered better. */
wpa_scan_result_wps_compar(const void * a,const void * b)2126f05cddf9SRui Paulo static int wpa_scan_result_wps_compar(const void *a, const void *b)
2127f05cddf9SRui Paulo {
2128f05cddf9SRui Paulo 	struct wpa_scan_res **_wa = (void *) a;
2129f05cddf9SRui Paulo 	struct wpa_scan_res **_wb = (void *) b;
2130f05cddf9SRui Paulo 	struct wpa_scan_res *wa = *_wa;
2131f05cddf9SRui Paulo 	struct wpa_scan_res *wb = *_wb;
2132f05cddf9SRui Paulo 	int uses_wps_a, uses_wps_b;
2133f05cddf9SRui Paulo 	struct wpabuf *wps_a, *wps_b;
2134f05cddf9SRui Paulo 	int res;
2135f05cddf9SRui Paulo 
2136f05cddf9SRui Paulo 	/* Optimization - check WPS IE existence before allocated memory and
2137f05cddf9SRui Paulo 	 * doing full reassembly. */
2138f05cddf9SRui Paulo 	uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2139f05cddf9SRui Paulo 	uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2140f05cddf9SRui Paulo 	if (uses_wps_a && !uses_wps_b)
2141f05cddf9SRui Paulo 		return -1;
2142f05cddf9SRui Paulo 	if (!uses_wps_a && uses_wps_b)
2143f05cddf9SRui Paulo 		return 1;
2144f05cddf9SRui Paulo 
2145f05cddf9SRui Paulo 	if (uses_wps_a && uses_wps_b) {
2146f05cddf9SRui Paulo 		wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2147f05cddf9SRui Paulo 		wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2148f05cddf9SRui Paulo 		res = wps_ap_priority_compar(wps_a, wps_b);
2149f05cddf9SRui Paulo 		wpabuf_free(wps_a);
2150f05cddf9SRui Paulo 		wpabuf_free(wps_b);
2151f05cddf9SRui Paulo 		if (res)
2152f05cddf9SRui Paulo 			return res;
2153f05cddf9SRui Paulo 	}
2154f05cddf9SRui Paulo 
2155f05cddf9SRui Paulo 	/*
2156f05cddf9SRui Paulo 	 * Do not use current AP security policy as a sorting criteria during
2157f05cddf9SRui Paulo 	 * WPS provisioning step since the AP may get reconfigured at the
2158f05cddf9SRui Paulo 	 * completion of provisioning.
2159f05cddf9SRui Paulo 	 */
2160f05cddf9SRui Paulo 
2161e28a4053SRui Paulo 	/* all things being equal, use signal level; if signal levels are
2162e28a4053SRui Paulo 	 * identical, use quality values since some drivers may only report
2163e28a4053SRui Paulo 	 * that value and leave the signal level zero */
2164e28a4053SRui Paulo 	if (wb->level == wa->level)
2165e28a4053SRui Paulo 		return wb->qual - wa->qual;
2166e28a4053SRui Paulo 	return wb->level - wa->level;
2167e28a4053SRui Paulo }
2168f05cddf9SRui Paulo #endif /* CONFIG_WPS */
2169f05cddf9SRui Paulo 
2170f05cddf9SRui Paulo 
dump_scan_res(struct wpa_scan_results * scan_res)2171f05cddf9SRui Paulo static void dump_scan_res(struct wpa_scan_results *scan_res)
2172f05cddf9SRui Paulo {
2173f05cddf9SRui Paulo #ifndef CONFIG_NO_STDOUT_DEBUG
2174f05cddf9SRui Paulo 	size_t i;
2175f05cddf9SRui Paulo 
2176f05cddf9SRui Paulo 	if (scan_res->res == NULL || scan_res->num == 0)
2177f05cddf9SRui Paulo 		return;
2178f05cddf9SRui Paulo 
2179f05cddf9SRui Paulo 	wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2180f05cddf9SRui Paulo 
2181f05cddf9SRui Paulo 	for (i = 0; i < scan_res->num; i++) {
2182f05cddf9SRui Paulo 		struct wpa_scan_res *r = scan_res->res[i];
2183f05cddf9SRui Paulo 		u8 *pos;
21845b9c547cSRui Paulo 		if (r->flags & WPA_SCAN_LEVEL_DBM) {
21855b9c547cSRui Paulo 			int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
21865b9c547cSRui Paulo 
2187f05cddf9SRui Paulo 			wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
21885b9c547cSRui Paulo 				   "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2189f05cddf9SRui Paulo 				   MAC2STR(r->bssid), r->freq, r->qual,
21905b9c547cSRui Paulo 				   r->noise, noise_valid ? "" : "~", r->level,
21915b9c547cSRui Paulo 				   r->snr, r->snr >= GREAT_SNR ? "*" : "",
21925b9c547cSRui Paulo 				   r->flags,
21935b9c547cSRui Paulo 				   r->age, r->est_throughput);
2194f05cddf9SRui Paulo 		} else {
2195f05cddf9SRui Paulo 			wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
21965b9c547cSRui Paulo 				   "noise=%d level=%d flags=0x%x age=%u est=%u",
2197f05cddf9SRui Paulo 				   MAC2STR(r->bssid), r->freq, r->qual,
21985b9c547cSRui Paulo 				   r->noise, r->level, r->flags, r->age,
21995b9c547cSRui Paulo 				   r->est_throughput);
2200f05cddf9SRui Paulo 		}
2201f05cddf9SRui Paulo 		pos = (u8 *) (r + 1);
2202f05cddf9SRui Paulo 		if (r->ie_len)
2203f05cddf9SRui Paulo 			wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2204f05cddf9SRui Paulo 		pos += r->ie_len;
2205f05cddf9SRui Paulo 		if (r->beacon_ie_len)
2206f05cddf9SRui Paulo 			wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2207f05cddf9SRui Paulo 				    pos, r->beacon_ie_len);
2208f05cddf9SRui Paulo 	}
2209f05cddf9SRui Paulo #endif /* CONFIG_NO_STDOUT_DEBUG */
2210f05cddf9SRui Paulo }
2211f05cddf9SRui Paulo 
2212f05cddf9SRui Paulo 
2213f05cddf9SRui Paulo /**
2214f05cddf9SRui Paulo  * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2215f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
2216f05cddf9SRui Paulo  * @bssid: BSSID to check
2217f05cddf9SRui Paulo  * Returns: 0 if the BSSID is filtered or 1 if not
2218f05cddf9SRui Paulo  *
2219f05cddf9SRui Paulo  * This function is used to filter out specific BSSIDs from scan reslts mainly
2220f05cddf9SRui Paulo  * for testing purposes (SET bssid_filter ctrl_iface command).
2221f05cddf9SRui Paulo  */
wpa_supplicant_filter_bssid_match(struct wpa_supplicant * wpa_s,const u8 * bssid)2222f05cddf9SRui Paulo int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2223f05cddf9SRui Paulo 				      const u8 *bssid)
2224f05cddf9SRui Paulo {
2225f05cddf9SRui Paulo 	size_t i;
2226f05cddf9SRui Paulo 
2227f05cddf9SRui Paulo 	if (wpa_s->bssid_filter == NULL)
2228f05cddf9SRui Paulo 		return 1;
2229f05cddf9SRui Paulo 
2230f05cddf9SRui Paulo 	for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2231f05cddf9SRui Paulo 		if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2232f05cddf9SRui Paulo 			      ETH_ALEN) == 0)
2233f05cddf9SRui Paulo 			return 1;
2234f05cddf9SRui Paulo 	}
2235f05cddf9SRui Paulo 
2236f05cddf9SRui Paulo 	return 0;
2237f05cddf9SRui Paulo }
2238f05cddf9SRui Paulo 
2239f05cddf9SRui Paulo 
filter_scan_res(struct wpa_supplicant * wpa_s,struct wpa_scan_results * res)2240780fb4a2SCy Schubert void filter_scan_res(struct wpa_supplicant *wpa_s,
2241f05cddf9SRui Paulo 		     struct wpa_scan_results *res)
2242f05cddf9SRui Paulo {
2243f05cddf9SRui Paulo 	size_t i, j;
2244f05cddf9SRui Paulo 
2245f05cddf9SRui Paulo 	if (wpa_s->bssid_filter == NULL)
2246f05cddf9SRui Paulo 		return;
2247f05cddf9SRui Paulo 
2248f05cddf9SRui Paulo 	for (i = 0, j = 0; i < res->num; i++) {
2249f05cddf9SRui Paulo 		if (wpa_supplicant_filter_bssid_match(wpa_s,
2250f05cddf9SRui Paulo 						      res->res[i]->bssid)) {
2251f05cddf9SRui Paulo 			res->res[j++] = res->res[i];
2252f05cddf9SRui Paulo 		} else {
2253f05cddf9SRui Paulo 			os_free(res->res[i]);
2254f05cddf9SRui Paulo 			res->res[i] = NULL;
2255f05cddf9SRui Paulo 		}
2256f05cddf9SRui Paulo 	}
2257f05cddf9SRui Paulo 
2258f05cddf9SRui Paulo 	if (res->num != j) {
2259f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2260f05cddf9SRui Paulo 			   (int) (res->num - j));
2261f05cddf9SRui Paulo 		res->num = j;
2262f05cddf9SRui Paulo 	}
2263f05cddf9SRui Paulo }
2264e28a4053SRui Paulo 
2265e28a4053SRui Paulo 
scan_snr(struct wpa_scan_res * res)2266780fb4a2SCy Schubert void scan_snr(struct wpa_scan_res *res)
22675b9c547cSRui Paulo {
22685b9c547cSRui Paulo 	if (res->flags & WPA_SCAN_NOISE_INVALID) {
2269c1d255d3SCy Schubert 		res->noise = is_6ghz_freq(res->freq) ?
2270c1d255d3SCy Schubert 			DEFAULT_NOISE_FLOOR_6GHZ :
2271c1d255d3SCy Schubert 			(IS_5GHZ(res->freq) ?
2272c1d255d3SCy Schubert 			 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
22735b9c547cSRui Paulo 	}
22745b9c547cSRui Paulo 
22755b9c547cSRui Paulo 	if (res->flags & WPA_SCAN_LEVEL_DBM) {
22765b9c547cSRui Paulo 		res->snr = res->level - res->noise;
22775b9c547cSRui Paulo 	} else {
22785b9c547cSRui Paulo 		/* Level is not in dBm, so we can't calculate
22795b9c547cSRui Paulo 		 * SNR. Just use raw level (units unknown). */
22805b9c547cSRui Paulo 		res->snr = res->level;
22815b9c547cSRui Paulo 	}
22825b9c547cSRui Paulo }
22835b9c547cSRui Paulo 
22845b9c547cSRui Paulo 
2285c1d255d3SCy Schubert /* Minimum SNR required to achieve a certain bitrate. */
2286c1d255d3SCy Schubert struct minsnr_bitrate_entry {
2287c1d255d3SCy Schubert 	int minsnr;
2288c1d255d3SCy Schubert 	unsigned int bitrate; /* in Mbps */
2289c1d255d3SCy Schubert };
2290c1d255d3SCy Schubert 
2291c1d255d3SCy Schubert /* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2292c1d255d3SCy Schubert static const int vht_mcs = 8;
2293c1d255d3SCy Schubert 
2294c1d255d3SCy Schubert static const struct minsnr_bitrate_entry vht20_table[] = {
2295c1d255d3SCy Schubert 	{ 0, 0 },
2296c1d255d3SCy Schubert 	{ 2, 6500 },   /* HT20 MCS0 */
2297c1d255d3SCy Schubert 	{ 5, 13000 },  /* HT20 MCS1 */
2298c1d255d3SCy Schubert 	{ 9, 19500 },  /* HT20 MCS2 */
2299c1d255d3SCy Schubert 	{ 11, 26000 }, /* HT20 MCS3 */
2300c1d255d3SCy Schubert 	{ 15, 39000 }, /* HT20 MCS4 */
2301c1d255d3SCy Schubert 	{ 18, 52000 }, /* HT20 MCS5 */
2302c1d255d3SCy Schubert 	{ 20, 58500 }, /* HT20 MCS6 */
2303c1d255d3SCy Schubert 	{ 25, 65000 }, /* HT20 MCS7 */
2304c1d255d3SCy Schubert 	{ 29, 78000 }, /* VHT20 MCS8 */
2305c1d255d3SCy Schubert 	{ -1, 78000 }  /* SNR > 29 */
2306c1d255d3SCy Schubert };
2307c1d255d3SCy Schubert 
2308c1d255d3SCy Schubert static const struct minsnr_bitrate_entry vht40_table[] = {
2309c1d255d3SCy Schubert 	{ 0, 0 },
2310c1d255d3SCy Schubert 	{ 5, 13500 },   /* HT40 MCS0 */
2311c1d255d3SCy Schubert 	{ 8, 27000 },   /* HT40 MCS1 */
2312c1d255d3SCy Schubert 	{ 12, 40500 },  /* HT40 MCS2 */
2313c1d255d3SCy Schubert 	{ 14, 54000 },  /* HT40 MCS3 */
2314c1d255d3SCy Schubert 	{ 18, 81000 },  /* HT40 MCS4 */
2315c1d255d3SCy Schubert 	{ 21, 108000 }, /* HT40 MCS5 */
2316c1d255d3SCy Schubert 	{ 23, 121500 }, /* HT40 MCS6 */
2317c1d255d3SCy Schubert 	{ 28, 135000 }, /* HT40 MCS7 */
2318c1d255d3SCy Schubert 	{ 32, 162000 }, /* VHT40 MCS8 */
2319c1d255d3SCy Schubert 	{ 34, 180000 }, /* VHT40 MCS9 */
2320c1d255d3SCy Schubert 	{ -1, 180000 }  /* SNR > 34 */
2321c1d255d3SCy Schubert };
2322c1d255d3SCy Schubert 
2323c1d255d3SCy Schubert static const struct minsnr_bitrate_entry vht80_table[] = {
2324c1d255d3SCy Schubert 	{ 0, 0 },
2325c1d255d3SCy Schubert 	{ 8, 29300 },   /* VHT80 MCS0 */
2326c1d255d3SCy Schubert 	{ 11, 58500 },  /* VHT80 MCS1 */
2327c1d255d3SCy Schubert 	{ 15, 87800 },  /* VHT80 MCS2 */
2328c1d255d3SCy Schubert 	{ 17, 117000 }, /* VHT80 MCS3 */
2329c1d255d3SCy Schubert 	{ 21, 175500 }, /* VHT80 MCS4 */
2330c1d255d3SCy Schubert 	{ 24, 234000 }, /* VHT80 MCS5 */
2331c1d255d3SCy Schubert 	{ 26, 263300 }, /* VHT80 MCS6 */
2332c1d255d3SCy Schubert 	{ 31, 292500 }, /* VHT80 MCS7 */
2333c1d255d3SCy Schubert 	{ 35, 351000 }, /* VHT80 MCS8 */
2334c1d255d3SCy Schubert 	{ 37, 390000 }, /* VHT80 MCS9 */
2335c1d255d3SCy Schubert 	{ -1, 390000 }  /* SNR > 37 */
2336c1d255d3SCy Schubert };
2337c1d255d3SCy Schubert 
2338c1d255d3SCy Schubert 
2339c1d255d3SCy Schubert static const struct minsnr_bitrate_entry vht160_table[] = {
2340c1d255d3SCy Schubert 	{ 0, 0 },
2341c1d255d3SCy Schubert 	{ 11, 58500 },  /* VHT160 MCS0 */
2342c1d255d3SCy Schubert 	{ 14, 117000 }, /* VHT160 MCS1 */
2343c1d255d3SCy Schubert 	{ 18, 175500 }, /* VHT160 MCS2 */
2344c1d255d3SCy Schubert 	{ 20, 234000 }, /* VHT160 MCS3 */
2345c1d255d3SCy Schubert 	{ 24, 351000 }, /* VHT160 MCS4 */
2346c1d255d3SCy Schubert 	{ 27, 468000 }, /* VHT160 MCS5 */
2347c1d255d3SCy Schubert 	{ 29, 526500 }, /* VHT160 MCS6 */
2348c1d255d3SCy Schubert 	{ 34, 585000 }, /* VHT160 MCS7 */
2349c1d255d3SCy Schubert 	{ 38, 702000 }, /* VHT160 MCS8 */
2350c1d255d3SCy Schubert 	{ 40, 780000 }, /* VHT160 MCS9 */
2351c1d255d3SCy Schubert 	{ -1, 780000 }  /* SNR > 37 */
2352c1d255d3SCy Schubert };
2353c1d255d3SCy Schubert 
2354c1d255d3SCy Schubert 
2355c1d255d3SCy Schubert static const struct minsnr_bitrate_entry he20_table[] = {
2356c1d255d3SCy Schubert 	{ 0, 0 },
2357c1d255d3SCy Schubert 	{ 2, 8600 },    /* HE20 MCS0 */
2358c1d255d3SCy Schubert 	{ 5, 17200 },   /* HE20 MCS1 */
2359c1d255d3SCy Schubert 	{ 9, 25800 },   /* HE20 MCS2 */
2360c1d255d3SCy Schubert 	{ 11, 34400 },  /* HE20 MCS3 */
2361c1d255d3SCy Schubert 	{ 15, 51600 },  /* HE20 MCS4 */
2362c1d255d3SCy Schubert 	{ 18, 68800 },  /* HE20 MCS5 */
2363c1d255d3SCy Schubert 	{ 20, 77400 },  /* HE20 MCS6 */
2364c1d255d3SCy Schubert 	{ 25, 86000 },  /* HE20 MCS7 */
2365c1d255d3SCy Schubert 	{ 29, 103200 }, /* HE20 MCS8 */
2366c1d255d3SCy Schubert 	{ 31, 114700 }, /* HE20 MCS9 */
2367c1d255d3SCy Schubert 	{ 34, 129000 }, /* HE20 MCS10 */
2368c1d255d3SCy Schubert 	{ 36, 143400 }, /* HE20 MCS11 */
2369c1d255d3SCy Schubert 	{ -1, 143400 }  /* SNR > 29 */
2370c1d255d3SCy Schubert };
2371c1d255d3SCy Schubert 
2372c1d255d3SCy Schubert static const struct minsnr_bitrate_entry he40_table[] = {
2373c1d255d3SCy Schubert 	{ 0, 0 },
2374c1d255d3SCy Schubert 	{ 5, 17200 },   /* HE40 MCS0 */
2375c1d255d3SCy Schubert 	{ 8, 34400 },   /* HE40 MCS1 */
2376c1d255d3SCy Schubert 	{ 12, 51600 },  /* HE40 MCS2 */
2377c1d255d3SCy Schubert 	{ 14, 68800 },  /* HE40 MCS3 */
2378c1d255d3SCy Schubert 	{ 18, 103200 }, /* HE40 MCS4 */
2379c1d255d3SCy Schubert 	{ 21, 137600 }, /* HE40 MCS5 */
2380c1d255d3SCy Schubert 	{ 23, 154900 }, /* HE40 MCS6 */
2381c1d255d3SCy Schubert 	{ 28, 172100 }, /* HE40 MCS7 */
2382c1d255d3SCy Schubert 	{ 32, 206500 }, /* HE40 MCS8 */
2383c1d255d3SCy Schubert 	{ 34, 229400 }, /* HE40 MCS9 */
2384c1d255d3SCy Schubert 	{ 37, 258100 }, /* HE40 MCS10 */
2385c1d255d3SCy Schubert 	{ 39, 286800 }, /* HE40 MCS11 */
2386c1d255d3SCy Schubert 	{ -1, 286800 }  /* SNR > 34 */
2387c1d255d3SCy Schubert };
2388c1d255d3SCy Schubert 
2389c1d255d3SCy Schubert static const struct minsnr_bitrate_entry he80_table[] = {
2390c1d255d3SCy Schubert 	{ 0, 0 },
2391c1d255d3SCy Schubert 	{ 8, 36000 },   /* HE80 MCS0 */
2392c1d255d3SCy Schubert 	{ 11, 72100 },  /* HE80 MCS1 */
2393c1d255d3SCy Schubert 	{ 15, 108100 }, /* HE80 MCS2 */
2394c1d255d3SCy Schubert 	{ 17, 144100 }, /* HE80 MCS3 */
2395c1d255d3SCy Schubert 	{ 21, 216200 }, /* HE80 MCS4 */
2396c1d255d3SCy Schubert 	{ 24, 288200 }, /* HE80 MCS5 */
2397c1d255d3SCy Schubert 	{ 26, 324300 }, /* HE80 MCS6 */
2398c1d255d3SCy Schubert 	{ 31, 360300 }, /* HE80 MCS7 */
2399c1d255d3SCy Schubert 	{ 35, 432400 }, /* HE80 MCS8 */
2400c1d255d3SCy Schubert 	{ 37, 480400 }, /* HE80 MCS9 */
2401c1d255d3SCy Schubert 	{ 40, 540400 }, /* HE80 MCS10 */
2402c1d255d3SCy Schubert 	{ 42, 600500 }, /* HE80 MCS11 */
2403c1d255d3SCy Schubert 	{ -1, 600500 }  /* SNR > 37 */
2404c1d255d3SCy Schubert };
2405c1d255d3SCy Schubert 
2406c1d255d3SCy Schubert 
2407c1d255d3SCy Schubert static const struct minsnr_bitrate_entry he160_table[] = {
2408c1d255d3SCy Schubert 	{ 0, 0 },
2409c1d255d3SCy Schubert 	{ 11, 72100 },   /* HE160 MCS0 */
2410c1d255d3SCy Schubert 	{ 14, 144100 },  /* HE160 MCS1 */
2411c1d255d3SCy Schubert 	{ 18, 216200 },  /* HE160 MCS2 */
2412c1d255d3SCy Schubert 	{ 20, 288200 },  /* HE160 MCS3 */
2413c1d255d3SCy Schubert 	{ 24, 432400 },  /* HE160 MCS4 */
2414c1d255d3SCy Schubert 	{ 27, 576500 },  /* HE160 MCS5 */
2415c1d255d3SCy Schubert 	{ 29, 648500 },  /* HE160 MCS6 */
2416c1d255d3SCy Schubert 	{ 34, 720600 },  /* HE160 MCS7 */
2417c1d255d3SCy Schubert 	{ 38, 864700 },  /* HE160 MCS8 */
2418c1d255d3SCy Schubert 	{ 40, 960800 },  /* HE160 MCS9 */
2419c1d255d3SCy Schubert 	{ 43, 1080900 }, /* HE160 MCS10 */
2420c1d255d3SCy Schubert 	{ 45, 1201000 }, /* HE160 MCS11 */
2421c1d255d3SCy Schubert 	{ -1, 1201000 }  /* SNR > 37 */
2422c1d255d3SCy Schubert };
2423c1d255d3SCy Schubert 
2424c1d255d3SCy Schubert 
interpolate_rate(int snr,int snr0,int snr1,int rate0,int rate1)2425c1d255d3SCy Schubert static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2426c1d255d3SCy Schubert 				     int rate0, int rate1)
24275b9c547cSRui Paulo {
2428c1d255d3SCy Schubert 	return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
24295b9c547cSRui Paulo }
24305b9c547cSRui Paulo 
24315b9c547cSRui Paulo 
max_rate(const struct minsnr_bitrate_entry table[],int snr,bool vht)2432c1d255d3SCy Schubert static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2433c1d255d3SCy Schubert 			     int snr, bool vht)
24345b9c547cSRui Paulo {
2435c1d255d3SCy Schubert 	const struct minsnr_bitrate_entry *prev, *entry = table;
2436c1d255d3SCy Schubert 
2437c1d255d3SCy Schubert 	while ((entry->minsnr != -1) &&
2438c1d255d3SCy Schubert 	       (snr >= entry->minsnr) &&
2439c1d255d3SCy Schubert 	       (vht || entry - table <= vht_mcs))
2440c1d255d3SCy Schubert 		entry++;
2441c1d255d3SCy Schubert 	if (entry == table)
2442c1d255d3SCy Schubert 		return entry->bitrate;
2443c1d255d3SCy Schubert 	prev = entry - 1;
2444c1d255d3SCy Schubert 	if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2445c1d255d3SCy Schubert 		return prev->bitrate;
2446c1d255d3SCy Schubert 	return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2447c1d255d3SCy Schubert 				entry->bitrate);
2448c1d255d3SCy Schubert }
2449c1d255d3SCy Schubert 
2450c1d255d3SCy Schubert 
max_ht20_rate(int snr,bool vht)2451c1d255d3SCy Schubert static unsigned int max_ht20_rate(int snr, bool vht)
2452c1d255d3SCy Schubert {
2453c1d255d3SCy Schubert 	return max_rate(vht20_table, snr, vht);
2454c1d255d3SCy Schubert }
2455c1d255d3SCy Schubert 
2456c1d255d3SCy Schubert 
max_ht40_rate(int snr,bool vht)2457c1d255d3SCy Schubert static unsigned int max_ht40_rate(int snr, bool vht)
2458c1d255d3SCy Schubert {
2459c1d255d3SCy Schubert 	return max_rate(vht40_table, snr, vht);
24605b9c547cSRui Paulo }
24615b9c547cSRui Paulo 
24625b9c547cSRui Paulo 
max_vht80_rate(int snr)24635b9c547cSRui Paulo static unsigned int max_vht80_rate(int snr)
24645b9c547cSRui Paulo {
2465c1d255d3SCy Schubert 	return max_rate(vht80_table, snr, 1);
24665b9c547cSRui Paulo }
24675b9c547cSRui Paulo 
24685b9c547cSRui Paulo 
max_vht160_rate(int snr)2469c1d255d3SCy Schubert static unsigned int max_vht160_rate(int snr)
24705b9c547cSRui Paulo {
2471c1d255d3SCy Schubert 	return max_rate(vht160_table, snr, 1);
2472c1d255d3SCy Schubert }
2473c1d255d3SCy Schubert 
2474c1d255d3SCy Schubert 
max_he_rate(const struct minsnr_bitrate_entry table[],int snr)2475c1d255d3SCy Schubert static unsigned int max_he_rate(const struct minsnr_bitrate_entry table[],
2476c1d255d3SCy Schubert 				int snr)
2477c1d255d3SCy Schubert {
2478c1d255d3SCy Schubert 	const struct minsnr_bitrate_entry *prev, *entry = table;
2479c1d255d3SCy Schubert 
2480c1d255d3SCy Schubert 	while (entry->minsnr != -1 && snr >= entry->minsnr)
2481c1d255d3SCy Schubert 		entry++;
2482c1d255d3SCy Schubert 	if (entry == table)
2483c1d255d3SCy Schubert 		return 0;
2484c1d255d3SCy Schubert 	prev = entry - 1;
2485c1d255d3SCy Schubert 	if (entry->minsnr == -1)
2486c1d255d3SCy Schubert 		return prev->bitrate;
2487c1d255d3SCy Schubert 	return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2488c1d255d3SCy Schubert 				prev->bitrate, entry->bitrate);
2489c1d255d3SCy Schubert }
2490c1d255d3SCy Schubert 
2491c1d255d3SCy Schubert 
wpas_get_est_tpt(const struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len,int rate,int snr,int freq)2492c1d255d3SCy Schubert unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2493c1d255d3SCy Schubert 			      const u8 *ies, size_t ies_len, int rate,
2494c1d255d3SCy Schubert 			      int snr, int freq)
2495c1d255d3SCy Schubert {
2496c1d255d3SCy Schubert 	struct hostapd_hw_modes *hw_mode;
24975b9c547cSRui Paulo 	unsigned int est, tmp;
2498c1d255d3SCy Schubert 	const u8 *ie;
24995b9c547cSRui Paulo 
25005b9c547cSRui Paulo 	/* Limit based on estimated SNR */
25015b9c547cSRui Paulo 	if (rate > 1 * 2 && snr < 1)
25025b9c547cSRui Paulo 		rate = 1 * 2;
25035b9c547cSRui Paulo 	else if (rate > 2 * 2 && snr < 4)
25045b9c547cSRui Paulo 		rate = 2 * 2;
25055b9c547cSRui Paulo 	else if (rate > 6 * 2 && snr < 5)
25065b9c547cSRui Paulo 		rate = 6 * 2;
25075b9c547cSRui Paulo 	else if (rate > 9 * 2 && snr < 6)
25085b9c547cSRui Paulo 		rate = 9 * 2;
25095b9c547cSRui Paulo 	else if (rate > 12 * 2 && snr < 7)
25105b9c547cSRui Paulo 		rate = 12 * 2;
2511c1d255d3SCy Schubert 	else if (rate > 12 * 2 && snr < 8)
2512c1d255d3SCy Schubert 		rate = 14 * 2;
2513c1d255d3SCy Schubert 	else if (rate > 12 * 2 && snr < 9)
2514c1d255d3SCy Schubert 		rate = 16 * 2;
25155b9c547cSRui Paulo 	else if (rate > 18 * 2 && snr < 10)
25165b9c547cSRui Paulo 		rate = 18 * 2;
25175b9c547cSRui Paulo 	else if (rate > 24 * 2 && snr < 11)
25185b9c547cSRui Paulo 		rate = 24 * 2;
2519c1d255d3SCy Schubert 	else if (rate > 24 * 2 && snr < 12)
2520c1d255d3SCy Schubert 		rate = 27 * 2;
2521c1d255d3SCy Schubert 	else if (rate > 24 * 2 && snr < 13)
2522c1d255d3SCy Schubert 		rate = 30 * 2;
2523c1d255d3SCy Schubert 	else if (rate > 24 * 2 && snr < 14)
2524c1d255d3SCy Schubert 		rate = 33 * 2;
25255b9c547cSRui Paulo 	else if (rate > 36 * 2 && snr < 15)
25265b9c547cSRui Paulo 		rate = 36 * 2;
2527c1d255d3SCy Schubert 	else if (rate > 36 * 2 && snr < 16)
2528c1d255d3SCy Schubert 		rate = 39 * 2;
2529c1d255d3SCy Schubert 	else if (rate > 36 * 2 && snr < 17)
2530c1d255d3SCy Schubert 		rate = 42 * 2;
2531c1d255d3SCy Schubert 	else if (rate > 36 * 2 && snr < 18)
2532c1d255d3SCy Schubert 		rate = 45 * 2;
25335b9c547cSRui Paulo 	else if (rate > 48 * 2 && snr < 19)
25345b9c547cSRui Paulo 		rate = 48 * 2;
2535c1d255d3SCy Schubert 	else if (rate > 48 * 2 && snr < 20)
2536c1d255d3SCy Schubert 		rate = 51 * 2;
25375b9c547cSRui Paulo 	else if (rate > 54 * 2 && snr < 21)
25385b9c547cSRui Paulo 		rate = 54 * 2;
25395b9c547cSRui Paulo 	est = rate * 500;
25405b9c547cSRui Paulo 
2541c1d255d3SCy Schubert 	hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2542c1d255d3SCy Schubert 				     freq);
2543c1d255d3SCy Schubert 
2544c1d255d3SCy Schubert 	if (hw_mode && hw_mode->ht_capab) {
2545c1d255d3SCy Schubert 		ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
25465b9c547cSRui Paulo 		if (ie) {
2547c1d255d3SCy Schubert 			tmp = max_ht20_rate(snr, false);
25485b9c547cSRui Paulo 			if (tmp > est)
25495b9c547cSRui Paulo 				est = tmp;
25505b9c547cSRui Paulo 		}
25515b9c547cSRui Paulo 	}
25525b9c547cSRui Paulo 
2553c1d255d3SCy Schubert 	if (hw_mode &&
2554c1d255d3SCy Schubert 	    (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
2555c1d255d3SCy Schubert 		ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
25565b9c547cSRui Paulo 		if (ie && ie[1] >= 2 &&
25575b9c547cSRui Paulo 		    (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2558c1d255d3SCy Schubert 			tmp = max_ht40_rate(snr, false);
25595b9c547cSRui Paulo 			if (tmp > est)
25605b9c547cSRui Paulo 				est = tmp;
25615b9c547cSRui Paulo 		}
25625b9c547cSRui Paulo 	}
25635b9c547cSRui Paulo 
2564c1d255d3SCy Schubert 	if (hw_mode && hw_mode->vht_capab) {
25655b9c547cSRui Paulo 		/* Use +1 to assume VHT is always faster than HT */
2566c1d255d3SCy Schubert 		ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
25675b9c547cSRui Paulo 		if (ie) {
2568c1d255d3SCy Schubert 			bool vht80 = false, vht160 = false;
2569c1d255d3SCy Schubert 
2570c1d255d3SCy Schubert 			tmp = max_ht20_rate(snr, true) + 1;
25715b9c547cSRui Paulo 			if (tmp > est)
25725b9c547cSRui Paulo 				est = tmp;
25735b9c547cSRui Paulo 
2574c1d255d3SCy Schubert 			ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
25755b9c547cSRui Paulo 			if (ie && ie[1] >= 2 &&
25765b9c547cSRui Paulo 			    (ie[3] &
25775b9c547cSRui Paulo 			     HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2578c1d255d3SCy Schubert 				tmp = max_ht40_rate(snr, true) + 1;
25795b9c547cSRui Paulo 				if (tmp > est)
25805b9c547cSRui Paulo 					est = tmp;
25815b9c547cSRui Paulo 			}
25825b9c547cSRui Paulo 
2583c1d255d3SCy Schubert 			/* Determine VHT BSS bandwidth based on IEEE Std
2584c1d255d3SCy Schubert 			 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
2585c1d255d3SCy Schubert 			ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
2586c1d255d3SCy Schubert 			if (ie && ie[1] >= 3) {
2587c1d255d3SCy Schubert 				u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2588c1d255d3SCy Schubert 				u8 seg0 = ie[3];
2589c1d255d3SCy Schubert 				u8 seg1 = ie[4];
2590c1d255d3SCy Schubert 
2591c1d255d3SCy Schubert 				if (cw)
2592c1d255d3SCy Schubert 					vht80 = true;
2593c1d255d3SCy Schubert 				if (cw == 2 ||
2594c1d255d3SCy Schubert 				    (cw == 3 &&
2595c1d255d3SCy Schubert 				     (seg1 > 0 && abs(seg1 - seg0) == 16)))
2596c1d255d3SCy Schubert 					vht160 = true;
2597c1d255d3SCy Schubert 				if (cw == 1 &&
2598c1d255d3SCy Schubert 				    ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2599c1d255d3SCy Schubert 				     (seg1 > 0 && abs(seg1 - seg0) == 16)))
2600c1d255d3SCy Schubert 					vht160 = true;
2601c1d255d3SCy Schubert 			}
2602c1d255d3SCy Schubert 
2603c1d255d3SCy Schubert 			if (vht80) {
26045b9c547cSRui Paulo 				tmp = max_vht80_rate(snr) + 1;
26055b9c547cSRui Paulo 				if (tmp > est)
26065b9c547cSRui Paulo 					est = tmp;
26075b9c547cSRui Paulo 			}
2608c1d255d3SCy Schubert 
2609c1d255d3SCy Schubert 			if (vht160 &&
2610c1d255d3SCy Schubert 			    (hw_mode->vht_capab &
2611c1d255d3SCy Schubert 			     (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2612c1d255d3SCy Schubert 			      VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
2613c1d255d3SCy Schubert 				tmp = max_vht160_rate(snr) + 1;
2614c1d255d3SCy Schubert 				if (tmp > est)
2615c1d255d3SCy Schubert 					est = tmp;
26165b9c547cSRui Paulo 			}
26175b9c547cSRui Paulo 		}
2618c1d255d3SCy Schubert 	}
2619c1d255d3SCy Schubert 
2620c1d255d3SCy Schubert 	if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
2621c1d255d3SCy Schubert 		/* Use +2 to assume HE is always faster than HT/VHT */
2622c1d255d3SCy Schubert 		struct ieee80211_he_capabilities *he;
2623c1d255d3SCy Schubert 		struct he_capabilities *own_he;
2624c1d255d3SCy Schubert 		u8 cw;
2625c1d255d3SCy Schubert 
2626c1d255d3SCy Schubert 		ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
2627c1d255d3SCy Schubert 		if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
2628c1d255d3SCy Schubert 			return est;
2629c1d255d3SCy Schubert 		he = (struct ieee80211_he_capabilities *) &ie[3];
2630c1d255d3SCy Schubert 		own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
2631c1d255d3SCy Schubert 
2632c1d255d3SCy Schubert 		tmp = max_he_rate(he20_table, snr) + 2;
2633c1d255d3SCy Schubert 		if (tmp > est)
2634c1d255d3SCy Schubert 			est = tmp;
2635c1d255d3SCy Schubert 
2636c1d255d3SCy Schubert 		cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2637c1d255d3SCy Schubert 			own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
2638c1d255d3SCy Schubert 		if (cw &
2639c1d255d3SCy Schubert 		    (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
2640c1d255d3SCy Schubert 		     HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2641c1d255d3SCy Schubert 			tmp = max_he_rate(he40_table, snr) + 2;
2642c1d255d3SCy Schubert 			if (tmp > est)
2643c1d255d3SCy Schubert 				est = tmp;
2644c1d255d3SCy Schubert 		}
2645c1d255d3SCy Schubert 
2646c1d255d3SCy Schubert 		if (!IS_2P4GHZ(freq) &&
2647c1d255d3SCy Schubert 		    (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2648c1d255d3SCy Schubert 			tmp = max_he_rate(he80_table, snr) + 2;
2649c1d255d3SCy Schubert 			if (tmp > est)
2650c1d255d3SCy Schubert 				est = tmp;
2651c1d255d3SCy Schubert 		}
2652c1d255d3SCy Schubert 
2653c1d255d3SCy Schubert 		if (!IS_2P4GHZ(freq) &&
2654c1d255d3SCy Schubert 		    (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2655c1d255d3SCy Schubert 			   HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
2656c1d255d3SCy Schubert 			tmp = max_he_rate(he160_table, snr) + 2;
2657c1d255d3SCy Schubert 			if (tmp > est)
2658c1d255d3SCy Schubert 				est = tmp;
2659c1d255d3SCy Schubert 		}
2660c1d255d3SCy Schubert 	}
2661c1d255d3SCy Schubert 
2662c1d255d3SCy Schubert 	return est;
2663c1d255d3SCy Schubert }
2664c1d255d3SCy Schubert 
2665c1d255d3SCy Schubert 
scan_est_throughput(struct wpa_supplicant * wpa_s,struct wpa_scan_res * res)2666c1d255d3SCy Schubert void scan_est_throughput(struct wpa_supplicant *wpa_s,
2667c1d255d3SCy Schubert 			 struct wpa_scan_res *res)
2668c1d255d3SCy Schubert {
2669c1d255d3SCy Schubert 	int rate; /* max legacy rate in 500 kb/s units */
2670c1d255d3SCy Schubert 	int snr = res->snr;
2671c1d255d3SCy Schubert 	const u8 *ies = (const void *) (res + 1);
2672c1d255d3SCy Schubert 	size_t ie_len = res->ie_len;
2673c1d255d3SCy Schubert 
2674c1d255d3SCy Schubert 	if (res->est_throughput)
2675c1d255d3SCy Schubert 		return;
2676c1d255d3SCy Schubert 
2677c1d255d3SCy Schubert 	/* Get maximum legacy rate */
2678c1d255d3SCy Schubert 	rate = wpa_scan_get_max_rate(res);
2679c1d255d3SCy Schubert 
2680c1d255d3SCy Schubert 	if (!ie_len)
2681c1d255d3SCy Schubert 		ie_len = res->beacon_ie_len;
2682c1d255d3SCy Schubert 	res->est_throughput =
2683c1d255d3SCy Schubert 		wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, res->freq);
26845b9c547cSRui Paulo 
26855b9c547cSRui Paulo 	/* TODO: channel utilization and AP load (e.g., from AP Beacon) */
26865b9c547cSRui Paulo }
26875b9c547cSRui Paulo 
26885b9c547cSRui Paulo 
2689e28a4053SRui Paulo /**
2690e28a4053SRui Paulo  * wpa_supplicant_get_scan_results - Get scan results
2691e28a4053SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
2692e28a4053SRui Paulo  * @info: Information about what was scanned or %NULL if not available
2693e28a4053SRui Paulo  * @new_scan: Whether a new scan was performed
2694e28a4053SRui Paulo  * Returns: Scan results, %NULL on failure
2695e28a4053SRui Paulo  *
2696e28a4053SRui Paulo  * This function request the current scan results from the driver and updates
2697e28a4053SRui Paulo  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2698e28a4053SRui Paulo  * results with wpa_scan_results_free().
2699e28a4053SRui Paulo  */
2700e28a4053SRui Paulo struct wpa_scan_results *
wpa_supplicant_get_scan_results(struct wpa_supplicant * wpa_s,struct scan_info * info,int new_scan)2701e28a4053SRui Paulo wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2702e28a4053SRui Paulo 				struct scan_info *info, int new_scan)
2703e28a4053SRui Paulo {
2704e28a4053SRui Paulo 	struct wpa_scan_results *scan_res;
2705e28a4053SRui Paulo 	size_t i;
2706f05cddf9SRui Paulo 	int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2707e28a4053SRui Paulo 
2708e28a4053SRui Paulo 	scan_res = wpa_drv_get_scan_results2(wpa_s);
2709e28a4053SRui Paulo 	if (scan_res == NULL) {
2710f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2711e28a4053SRui Paulo 		return NULL;
2712e28a4053SRui Paulo 	}
27135b9c547cSRui Paulo 	if (scan_res->fetch_time.sec == 0) {
27145b9c547cSRui Paulo 		/*
27155b9c547cSRui Paulo 		 * Make sure we have a valid timestamp if the driver wrapper
27165b9c547cSRui Paulo 		 * does not set this.
27175b9c547cSRui Paulo 		 */
27185b9c547cSRui Paulo 		os_get_reltime(&scan_res->fetch_time);
27195b9c547cSRui Paulo 	}
2720f05cddf9SRui Paulo 	filter_scan_res(wpa_s, scan_res);
2721f05cddf9SRui Paulo 
27225b9c547cSRui Paulo 	for (i = 0; i < scan_res->num; i++) {
27235b9c547cSRui Paulo 		struct wpa_scan_res *scan_res_item = scan_res->res[i];
27245b9c547cSRui Paulo 
27255b9c547cSRui Paulo 		scan_snr(scan_res_item);
27265b9c547cSRui Paulo 		scan_est_throughput(wpa_s, scan_res_item);
27275b9c547cSRui Paulo 	}
27285b9c547cSRui Paulo 
2729f05cddf9SRui Paulo #ifdef CONFIG_WPS
27305b9c547cSRui Paulo 	if (wpas_wps_searching(wpa_s)) {
2731f05cddf9SRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2732f05cddf9SRui Paulo 			"provisioning rules");
2733f05cddf9SRui Paulo 		compar = wpa_scan_result_wps_compar;
2734f05cddf9SRui Paulo 	}
2735f05cddf9SRui Paulo #endif /* CONFIG_WPS */
2736e28a4053SRui Paulo 
273785732ac8SCy Schubert 	if (scan_res->res) {
273885732ac8SCy Schubert 		qsort(scan_res->res, scan_res->num,
273985732ac8SCy Schubert 		      sizeof(struct wpa_scan_res *), compar);
274085732ac8SCy Schubert 	}
2741f05cddf9SRui Paulo 	dump_scan_res(scan_res);
2742e28a4053SRui Paulo 
274385732ac8SCy Schubert 	if (wpa_s->ignore_post_flush_scan_res) {
274485732ac8SCy Schubert 		/* FLUSH command aborted an ongoing scan and these are the
274585732ac8SCy Schubert 		 * results from the aborted scan. Do not process the results to
274685732ac8SCy Schubert 		 * maintain flushed state. */
274785732ac8SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG,
274885732ac8SCy Schubert 			"Do not update BSS table based on pending post-FLUSH scan results");
274985732ac8SCy Schubert 		wpa_s->ignore_post_flush_scan_res = 0;
275085732ac8SCy Schubert 		return scan_res;
275185732ac8SCy Schubert 	}
275285732ac8SCy Schubert 
2753e28a4053SRui Paulo 	wpa_bss_update_start(wpa_s);
2754e28a4053SRui Paulo 	for (i = 0; i < scan_res->num; i++)
27555b9c547cSRui Paulo 		wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
27565b9c547cSRui Paulo 					&scan_res->fetch_time);
2757e28a4053SRui Paulo 	wpa_bss_update_end(wpa_s, info, new_scan);
2758e28a4053SRui Paulo 
2759e28a4053SRui Paulo 	return scan_res;
2760e28a4053SRui Paulo }
2761e28a4053SRui Paulo 
2762e28a4053SRui Paulo 
2763f05cddf9SRui Paulo /**
2764f05cddf9SRui Paulo  * wpa_supplicant_update_scan_results - Update scan results from the driver
2765f05cddf9SRui Paulo  * @wpa_s: Pointer to wpa_supplicant data
2766f05cddf9SRui Paulo  * Returns: 0 on success, -1 on failure
2767f05cddf9SRui Paulo  *
2768f05cddf9SRui Paulo  * This function updates the BSS table within wpa_supplicant based on the
2769f05cddf9SRui Paulo  * currently available scan results from the driver without requesting a new
2770f05cddf9SRui Paulo  * scan. This is used in cases where the driver indicates an association
2771f05cddf9SRui Paulo  * (including roaming within ESS) and wpa_supplicant does not yet have the
2772f05cddf9SRui Paulo  * needed information to complete the connection (e.g., to perform validation
2773f05cddf9SRui Paulo  * steps in 4-way handshake).
2774f05cddf9SRui Paulo  */
wpa_supplicant_update_scan_results(struct wpa_supplicant * wpa_s)2775e28a4053SRui Paulo int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2776e28a4053SRui Paulo {
2777e28a4053SRui Paulo 	struct wpa_scan_results *scan_res;
2778e28a4053SRui Paulo 	scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2779e28a4053SRui Paulo 	if (scan_res == NULL)
2780e28a4053SRui Paulo 		return -1;
2781e28a4053SRui Paulo 	wpa_scan_results_free(scan_res);
2782e28a4053SRui Paulo 
2783e28a4053SRui Paulo 	return 0;
2784e28a4053SRui Paulo }
27855b9c547cSRui Paulo 
27865b9c547cSRui Paulo 
27875b9c547cSRui Paulo /**
27885b9c547cSRui Paulo  * scan_only_handler - Reports scan results
27895b9c547cSRui Paulo  */
scan_only_handler(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)27905b9c547cSRui Paulo void scan_only_handler(struct wpa_supplicant *wpa_s,
27915b9c547cSRui Paulo 		       struct wpa_scan_results *scan_res)
27925b9c547cSRui Paulo {
27935b9c547cSRui Paulo 	wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
27945b9c547cSRui Paulo 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
27955b9c547cSRui Paulo 	    wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
27965b9c547cSRui Paulo 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
27975b9c547cSRui Paulo 			     wpa_s->manual_scan_id);
27985b9c547cSRui Paulo 		wpa_s->manual_scan_use_id = 0;
27995b9c547cSRui Paulo 	} else {
28005b9c547cSRui Paulo 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
28015b9c547cSRui Paulo 	}
28025b9c547cSRui Paulo 	wpas_notify_scan_results(wpa_s);
28035b9c547cSRui Paulo 	wpas_notify_scan_done(wpa_s, 1);
28045b9c547cSRui Paulo 	if (wpa_s->scan_work) {
28055b9c547cSRui Paulo 		struct wpa_radio_work *work = wpa_s->scan_work;
28065b9c547cSRui Paulo 		wpa_s->scan_work = NULL;
28075b9c547cSRui Paulo 		radio_work_done(work);
28085b9c547cSRui Paulo 	}
2809780fb4a2SCy Schubert 
2810780fb4a2SCy Schubert 	if (wpa_s->wpa_state == WPA_SCANNING)
2811780fb4a2SCy Schubert 		wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
28125b9c547cSRui Paulo }
28135b9c547cSRui Paulo 
28145b9c547cSRui Paulo 
wpas_scan_scheduled(struct wpa_supplicant * wpa_s)28155b9c547cSRui Paulo int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
28165b9c547cSRui Paulo {
28175b9c547cSRui Paulo 	return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
28185b9c547cSRui Paulo }
28195b9c547cSRui Paulo 
28205b9c547cSRui Paulo 
28215b9c547cSRui Paulo struct wpa_driver_scan_params *
wpa_scan_clone_params(const struct wpa_driver_scan_params * src)28225b9c547cSRui Paulo wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
28235b9c547cSRui Paulo {
28245b9c547cSRui Paulo 	struct wpa_driver_scan_params *params;
28255b9c547cSRui Paulo 	size_t i;
28265b9c547cSRui Paulo 	u8 *n;
28275b9c547cSRui Paulo 
28285b9c547cSRui Paulo 	params = os_zalloc(sizeof(*params));
28295b9c547cSRui Paulo 	if (params == NULL)
28305b9c547cSRui Paulo 		return NULL;
28315b9c547cSRui Paulo 
28325b9c547cSRui Paulo 	for (i = 0; i < src->num_ssids; i++) {
28335b9c547cSRui Paulo 		if (src->ssids[i].ssid) {
283485732ac8SCy Schubert 			n = os_memdup(src->ssids[i].ssid,
283585732ac8SCy Schubert 				      src->ssids[i].ssid_len);
28365b9c547cSRui Paulo 			if (n == NULL)
28375b9c547cSRui Paulo 				goto failed;
28385b9c547cSRui Paulo 			params->ssids[i].ssid = n;
28395b9c547cSRui Paulo 			params->ssids[i].ssid_len = src->ssids[i].ssid_len;
28405b9c547cSRui Paulo 		}
28415b9c547cSRui Paulo 	}
28425b9c547cSRui Paulo 	params->num_ssids = src->num_ssids;
28435b9c547cSRui Paulo 
28445b9c547cSRui Paulo 	if (src->extra_ies) {
284585732ac8SCy Schubert 		n = os_memdup(src->extra_ies, src->extra_ies_len);
28465b9c547cSRui Paulo 		if (n == NULL)
28475b9c547cSRui Paulo 			goto failed;
28485b9c547cSRui Paulo 		params->extra_ies = n;
28495b9c547cSRui Paulo 		params->extra_ies_len = src->extra_ies_len;
28505b9c547cSRui Paulo 	}
28515b9c547cSRui Paulo 
28525b9c547cSRui Paulo 	if (src->freqs) {
28535b9c547cSRui Paulo 		int len = int_array_len(src->freqs);
285485732ac8SCy Schubert 		params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
28555b9c547cSRui Paulo 		if (params->freqs == NULL)
28565b9c547cSRui Paulo 			goto failed;
28575b9c547cSRui Paulo 	}
28585b9c547cSRui Paulo 
28595b9c547cSRui Paulo 	if (src->filter_ssids) {
286085732ac8SCy Schubert 		params->filter_ssids = os_memdup(src->filter_ssids,
286185732ac8SCy Schubert 						 sizeof(*params->filter_ssids) *
28625b9c547cSRui Paulo 						 src->num_filter_ssids);
28635b9c547cSRui Paulo 		if (params->filter_ssids == NULL)
28645b9c547cSRui Paulo 			goto failed;
28655b9c547cSRui Paulo 		params->num_filter_ssids = src->num_filter_ssids;
28665b9c547cSRui Paulo 	}
28675b9c547cSRui Paulo 
28685b9c547cSRui Paulo 	params->filter_rssi = src->filter_rssi;
28695b9c547cSRui Paulo 	params->p2p_probe = src->p2p_probe;
28705b9c547cSRui Paulo 	params->only_new_results = src->only_new_results;
28715b9c547cSRui Paulo 	params->low_priority = src->low_priority;
287285732ac8SCy Schubert 	params->duration = src->duration;
287385732ac8SCy Schubert 	params->duration_mandatory = src->duration_mandatory;
287485732ac8SCy Schubert 	params->oce_scan = src->oce_scan;
28755b9c547cSRui Paulo 
2876780fb4a2SCy Schubert 	if (src->sched_scan_plans_num > 0) {
2877780fb4a2SCy Schubert 		params->sched_scan_plans =
287885732ac8SCy Schubert 			os_memdup(src->sched_scan_plans,
287985732ac8SCy Schubert 				  sizeof(*src->sched_scan_plans) *
2880780fb4a2SCy Schubert 				  src->sched_scan_plans_num);
2881780fb4a2SCy Schubert 		if (!params->sched_scan_plans)
2882780fb4a2SCy Schubert 			goto failed;
2883780fb4a2SCy Schubert 
2884780fb4a2SCy Schubert 		params->sched_scan_plans_num = src->sched_scan_plans_num;
2885780fb4a2SCy Schubert 	}
2886780fb4a2SCy Schubert 
2887c1d255d3SCy Schubert 	if (src->mac_addr_rand &&
2888c1d255d3SCy Schubert 	    wpa_setup_mac_addr_rand_params(params, src->mac_addr))
28895b9c547cSRui Paulo 		goto failed;
28905b9c547cSRui Paulo 
2891780fb4a2SCy Schubert 	if (src->bssid) {
2892780fb4a2SCy Schubert 		u8 *bssid;
2893780fb4a2SCy Schubert 
289485732ac8SCy Schubert 		bssid = os_memdup(src->bssid, ETH_ALEN);
2895780fb4a2SCy Schubert 		if (!bssid)
2896780fb4a2SCy Schubert 			goto failed;
2897780fb4a2SCy Schubert 		params->bssid = bssid;
2898780fb4a2SCy Schubert 	}
2899780fb4a2SCy Schubert 
290085732ac8SCy Schubert 	params->relative_rssi_set = src->relative_rssi_set;
290185732ac8SCy Schubert 	params->relative_rssi = src->relative_rssi;
290285732ac8SCy Schubert 	params->relative_adjust_band = src->relative_adjust_band;
290385732ac8SCy Schubert 	params->relative_adjust_rssi = src->relative_adjust_rssi;
2904c1d255d3SCy Schubert 	params->p2p_include_6ghz = src->p2p_include_6ghz;
29055b9c547cSRui Paulo 	return params;
29065b9c547cSRui Paulo 
29075b9c547cSRui Paulo failed:
29085b9c547cSRui Paulo 	wpa_scan_free_params(params);
29095b9c547cSRui Paulo 	return NULL;
29105b9c547cSRui Paulo }
29115b9c547cSRui Paulo 
29125b9c547cSRui Paulo 
wpa_scan_free_params(struct wpa_driver_scan_params * params)29135b9c547cSRui Paulo void wpa_scan_free_params(struct wpa_driver_scan_params *params)
29145b9c547cSRui Paulo {
29155b9c547cSRui Paulo 	size_t i;
29165b9c547cSRui Paulo 
29175b9c547cSRui Paulo 	if (params == NULL)
29185b9c547cSRui Paulo 		return;
29195b9c547cSRui Paulo 
29205b9c547cSRui Paulo 	for (i = 0; i < params->num_ssids; i++)
29215b9c547cSRui Paulo 		os_free((u8 *) params->ssids[i].ssid);
29225b9c547cSRui Paulo 	os_free((u8 *) params->extra_ies);
29235b9c547cSRui Paulo 	os_free(params->freqs);
29245b9c547cSRui Paulo 	os_free(params->filter_ssids);
2925780fb4a2SCy Schubert 	os_free(params->sched_scan_plans);
29265b9c547cSRui Paulo 
29275b9c547cSRui Paulo 	/*
29285b9c547cSRui Paulo 	 * Note: params->mac_addr_mask points to same memory allocation and
29295b9c547cSRui Paulo 	 * must not be freed separately.
29305b9c547cSRui Paulo 	 */
29315b9c547cSRui Paulo 	os_free((u8 *) params->mac_addr);
29325b9c547cSRui Paulo 
2933780fb4a2SCy Schubert 	os_free((u8 *) params->bssid);
2934780fb4a2SCy Schubert 
29355b9c547cSRui Paulo 	os_free(params);
29365b9c547cSRui Paulo }
29375b9c547cSRui Paulo 
29385b9c547cSRui Paulo 
wpas_start_pno(struct wpa_supplicant * wpa_s)29395b9c547cSRui Paulo int wpas_start_pno(struct wpa_supplicant *wpa_s)
29405b9c547cSRui Paulo {
2941c1d255d3SCy Schubert 	int ret;
2942c1d255d3SCy Schubert 	size_t prio, i, num_ssid, num_match_ssid;
29435b9c547cSRui Paulo 	struct wpa_ssid *ssid;
29445b9c547cSRui Paulo 	struct wpa_driver_scan_params params;
2945780fb4a2SCy Schubert 	struct sched_scan_plan scan_plan;
2946780fb4a2SCy Schubert 	unsigned int max_sched_scan_ssids;
29475b9c547cSRui Paulo 
29485b9c547cSRui Paulo 	if (!wpa_s->sched_scan_supported)
29495b9c547cSRui Paulo 		return -1;
29505b9c547cSRui Paulo 
2951780fb4a2SCy Schubert 	if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2952780fb4a2SCy Schubert 		max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2953780fb4a2SCy Schubert 	else
2954780fb4a2SCy Schubert 		max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2955780fb4a2SCy Schubert 	if (max_sched_scan_ssids < 1)
2956780fb4a2SCy Schubert 		return -1;
2957780fb4a2SCy Schubert 
29585b9c547cSRui Paulo 	if (wpa_s->pno || wpa_s->pno_sched_pending)
29595b9c547cSRui Paulo 		return 0;
29605b9c547cSRui Paulo 
29615b9c547cSRui Paulo 	if ((wpa_s->wpa_state > WPA_SCANNING) &&
296285732ac8SCy Schubert 	    (wpa_s->wpa_state < WPA_COMPLETED)) {
29635b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "PNO: In assoc process");
29645b9c547cSRui Paulo 		return -EAGAIN;
29655b9c547cSRui Paulo 	}
29665b9c547cSRui Paulo 
29675b9c547cSRui Paulo 	if (wpa_s->wpa_state == WPA_SCANNING) {
29685b9c547cSRui Paulo 		wpa_supplicant_cancel_scan(wpa_s);
29695b9c547cSRui Paulo 		if (wpa_s->sched_scanning) {
29705b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
29715b9c547cSRui Paulo 				   "ongoing sched scan");
29725b9c547cSRui Paulo 			wpa_supplicant_cancel_sched_scan(wpa_s);
29735b9c547cSRui Paulo 			wpa_s->pno_sched_pending = 1;
29745b9c547cSRui Paulo 			return 0;
29755b9c547cSRui Paulo 		}
29765b9c547cSRui Paulo 	}
29775b9c547cSRui Paulo 
2978780fb4a2SCy Schubert 	if (wpa_s->sched_scan_stop_req) {
2979780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG,
2980780fb4a2SCy Schubert 			   "Schedule PNO after previous sched scan has stopped");
2981780fb4a2SCy Schubert 		wpa_s->pno_sched_pending = 1;
2982780fb4a2SCy Schubert 		return 0;
2983780fb4a2SCy Schubert 	}
2984780fb4a2SCy Schubert 
29855b9c547cSRui Paulo 	os_memset(&params, 0, sizeof(params));
29865b9c547cSRui Paulo 
29875b9c547cSRui Paulo 	num_ssid = num_match_ssid = 0;
29885b9c547cSRui Paulo 	ssid = wpa_s->conf->ssid;
29895b9c547cSRui Paulo 	while (ssid) {
29905b9c547cSRui Paulo 		if (!wpas_network_disabled(wpa_s, ssid)) {
29915b9c547cSRui Paulo 			num_match_ssid++;
29925b9c547cSRui Paulo 			if (ssid->scan_ssid)
29935b9c547cSRui Paulo 				num_ssid++;
29945b9c547cSRui Paulo 		}
29955b9c547cSRui Paulo 		ssid = ssid->next;
29965b9c547cSRui Paulo 	}
29975b9c547cSRui Paulo 
29985b9c547cSRui Paulo 	if (num_match_ssid == 0) {
29995b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
30005b9c547cSRui Paulo 		return -1;
30015b9c547cSRui Paulo 	}
30025b9c547cSRui Paulo 
30035b9c547cSRui Paulo 	if (num_match_ssid > num_ssid) {
30045b9c547cSRui Paulo 		params.num_ssids++; /* wildcard */
30055b9c547cSRui Paulo 		num_ssid++;
30065b9c547cSRui Paulo 	}
30075b9c547cSRui Paulo 
3008780fb4a2SCy Schubert 	if (num_ssid > max_sched_scan_ssids) {
30095b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
3010780fb4a2SCy Schubert 			   "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3011780fb4a2SCy Schubert 		num_ssid = max_sched_scan_ssids;
30125b9c547cSRui Paulo 	}
30135b9c547cSRui Paulo 
30145b9c547cSRui Paulo 	if (num_match_ssid > wpa_s->max_match_sets) {
30155b9c547cSRui Paulo 		num_match_ssid = wpa_s->max_match_sets;
30165b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
30175b9c547cSRui Paulo 	}
30185b9c547cSRui Paulo 	params.filter_ssids = os_calloc(num_match_ssid,
30195b9c547cSRui Paulo 					sizeof(struct wpa_driver_scan_filter));
30205b9c547cSRui Paulo 	if (params.filter_ssids == NULL)
30215b9c547cSRui Paulo 		return -1;
30225b9c547cSRui Paulo 
30235b9c547cSRui Paulo 	i = 0;
30245b9c547cSRui Paulo 	prio = 0;
30255b9c547cSRui Paulo 	ssid = wpa_s->conf->pssid[prio];
30265b9c547cSRui Paulo 	while (ssid) {
30275b9c547cSRui Paulo 		if (!wpas_network_disabled(wpa_s, ssid)) {
30285b9c547cSRui Paulo 			if (ssid->scan_ssid && params.num_ssids < num_ssid) {
30295b9c547cSRui Paulo 				params.ssids[params.num_ssids].ssid =
30305b9c547cSRui Paulo 					ssid->ssid;
30315b9c547cSRui Paulo 				params.ssids[params.num_ssids].ssid_len =
30325b9c547cSRui Paulo 					 ssid->ssid_len;
30335b9c547cSRui Paulo 				params.num_ssids++;
30345b9c547cSRui Paulo 			}
30355b9c547cSRui Paulo 			os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
30365b9c547cSRui Paulo 				  ssid->ssid_len);
30375b9c547cSRui Paulo 			params.filter_ssids[i].ssid_len = ssid->ssid_len;
30385b9c547cSRui Paulo 			params.num_filter_ssids++;
30395b9c547cSRui Paulo 			i++;
30405b9c547cSRui Paulo 			if (i == num_match_ssid)
30415b9c547cSRui Paulo 				break;
30425b9c547cSRui Paulo 		}
30435b9c547cSRui Paulo 		if (ssid->pnext)
30445b9c547cSRui Paulo 			ssid = ssid->pnext;
30455b9c547cSRui Paulo 		else if (prio + 1 == wpa_s->conf->num_prio)
30465b9c547cSRui Paulo 			break;
30475b9c547cSRui Paulo 		else
30485b9c547cSRui Paulo 			ssid = wpa_s->conf->pssid[++prio];
30495b9c547cSRui Paulo 	}
30505b9c547cSRui Paulo 
30515b9c547cSRui Paulo 	if (wpa_s->conf->filter_rssi)
30525b9c547cSRui Paulo 		params.filter_rssi = wpa_s->conf->filter_rssi;
30535b9c547cSRui Paulo 
3054780fb4a2SCy Schubert 	if (wpa_s->sched_scan_plans_num) {
3055780fb4a2SCy Schubert 		params.sched_scan_plans = wpa_s->sched_scan_plans;
3056780fb4a2SCy Schubert 		params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3057780fb4a2SCy Schubert 	} else {
3058780fb4a2SCy Schubert 		/* Set one scan plan that will run infinitely */
3059780fb4a2SCy Schubert 		if (wpa_s->conf->sched_scan_interval)
3060780fb4a2SCy Schubert 			scan_plan.interval = wpa_s->conf->sched_scan_interval;
3061780fb4a2SCy Schubert 		else
3062780fb4a2SCy Schubert 			scan_plan.interval = 10;
3063780fb4a2SCy Schubert 
3064780fb4a2SCy Schubert 		scan_plan.iterations = 0;
3065780fb4a2SCy Schubert 		params.sched_scan_plans = &scan_plan;
3066780fb4a2SCy Schubert 		params.sched_scan_plans_num = 1;
3067780fb4a2SCy Schubert 	}
30685b9c547cSRui Paulo 
306985732ac8SCy Schubert 	params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
307085732ac8SCy Schubert 
30715b9c547cSRui Paulo 	if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
30725b9c547cSRui Paulo 		wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
30735b9c547cSRui Paulo 		params.freqs = wpa_s->manual_sched_scan_freqs;
30745b9c547cSRui Paulo 	}
30755b9c547cSRui Paulo 
307685732ac8SCy Schubert 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
3077c1d255d3SCy Schubert 	    wpa_s->wpa_state <= WPA_SCANNING)
3078c1d255d3SCy Schubert 		wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
30795b9c547cSRui Paulo 
308085732ac8SCy Schubert 	wpa_scan_set_relative_rssi_params(wpa_s, &params);
308185732ac8SCy Schubert 
3082780fb4a2SCy Schubert 	ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
30835b9c547cSRui Paulo 	os_free(params.filter_ssids);
3084c1d255d3SCy Schubert 	os_free(params.mac_addr);
30855b9c547cSRui Paulo 	if (ret == 0)
30865b9c547cSRui Paulo 		wpa_s->pno = 1;
30875b9c547cSRui Paulo 	else
30885b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
30895b9c547cSRui Paulo 	return ret;
30905b9c547cSRui Paulo }
30915b9c547cSRui Paulo 
30925b9c547cSRui Paulo 
wpas_stop_pno(struct wpa_supplicant * wpa_s)30935b9c547cSRui Paulo int wpas_stop_pno(struct wpa_supplicant *wpa_s)
30945b9c547cSRui Paulo {
30955b9c547cSRui Paulo 	int ret = 0;
30965b9c547cSRui Paulo 
30975b9c547cSRui Paulo 	if (!wpa_s->pno)
30985b9c547cSRui Paulo 		return 0;
30995b9c547cSRui Paulo 
31005b9c547cSRui Paulo 	ret = wpa_supplicant_stop_sched_scan(wpa_s);
3101780fb4a2SCy Schubert 	wpa_s->sched_scan_stop_req = 1;
31025b9c547cSRui Paulo 
31035b9c547cSRui Paulo 	wpa_s->pno = 0;
31045b9c547cSRui Paulo 	wpa_s->pno_sched_pending = 0;
31055b9c547cSRui Paulo 
31065b9c547cSRui Paulo 	if (wpa_s->wpa_state == WPA_SCANNING)
31075b9c547cSRui Paulo 		wpa_supplicant_req_scan(wpa_s, 0, 0);
31085b9c547cSRui Paulo 
31095b9c547cSRui Paulo 	return ret;
31105b9c547cSRui Paulo }
31115b9c547cSRui Paulo 
31125b9c547cSRui Paulo 
wpas_mac_addr_rand_scan_clear(struct wpa_supplicant * wpa_s,unsigned int type)31135b9c547cSRui Paulo void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
31145b9c547cSRui Paulo 				    unsigned int type)
31155b9c547cSRui Paulo {
31165b9c547cSRui Paulo 	type &= MAC_ADDR_RAND_ALL;
31175b9c547cSRui Paulo 	wpa_s->mac_addr_rand_enable &= ~type;
31185b9c547cSRui Paulo 
31195b9c547cSRui Paulo 	if (type & MAC_ADDR_RAND_SCAN) {
31205b9c547cSRui Paulo 		os_free(wpa_s->mac_addr_scan);
31215b9c547cSRui Paulo 		wpa_s->mac_addr_scan = NULL;
31225b9c547cSRui Paulo 	}
31235b9c547cSRui Paulo 
31245b9c547cSRui Paulo 	if (type & MAC_ADDR_RAND_SCHED_SCAN) {
31255b9c547cSRui Paulo 		os_free(wpa_s->mac_addr_sched_scan);
31265b9c547cSRui Paulo 		wpa_s->mac_addr_sched_scan = NULL;
31275b9c547cSRui Paulo 	}
31285b9c547cSRui Paulo 
31295b9c547cSRui Paulo 	if (type & MAC_ADDR_RAND_PNO) {
31305b9c547cSRui Paulo 		os_free(wpa_s->mac_addr_pno);
31315b9c547cSRui Paulo 		wpa_s->mac_addr_pno = NULL;
31325b9c547cSRui Paulo 	}
31335b9c547cSRui Paulo }
31345b9c547cSRui Paulo 
31355b9c547cSRui Paulo 
wpas_mac_addr_rand_scan_set(struct wpa_supplicant * wpa_s,unsigned int type,const u8 * addr,const u8 * mask)31365b9c547cSRui Paulo int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
31375b9c547cSRui Paulo 				unsigned int type, const u8 *addr,
31385b9c547cSRui Paulo 				const u8 *mask)
31395b9c547cSRui Paulo {
31405b9c547cSRui Paulo 	u8 *tmp = NULL;
31415b9c547cSRui Paulo 
31424bc52338SCy Schubert 	if ((wpa_s->mac_addr_rand_supported & type) != type ) {
31434bc52338SCy Schubert 		wpa_printf(MSG_INFO,
31444bc52338SCy Schubert 			   "scan: MAC randomization type %u != supported=%u",
31454bc52338SCy Schubert 			   type, wpa_s->mac_addr_rand_supported);
31464bc52338SCy Schubert 		return -1;
31474bc52338SCy Schubert 	}
31484bc52338SCy Schubert 
31495b9c547cSRui Paulo 	wpas_mac_addr_rand_scan_clear(wpa_s, type);
31505b9c547cSRui Paulo 
31515b9c547cSRui Paulo 	if (addr) {
31525b9c547cSRui Paulo 		tmp = os_malloc(2 * ETH_ALEN);
31535b9c547cSRui Paulo 		if (!tmp)
31545b9c547cSRui Paulo 			return -1;
31555b9c547cSRui Paulo 		os_memcpy(tmp, addr, ETH_ALEN);
31565b9c547cSRui Paulo 		os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
31575b9c547cSRui Paulo 	}
31585b9c547cSRui Paulo 
31595b9c547cSRui Paulo 	if (type == MAC_ADDR_RAND_SCAN) {
31605b9c547cSRui Paulo 		wpa_s->mac_addr_scan = tmp;
31615b9c547cSRui Paulo 	} else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
31625b9c547cSRui Paulo 		wpa_s->mac_addr_sched_scan = tmp;
31635b9c547cSRui Paulo 	} else if (type == MAC_ADDR_RAND_PNO) {
31645b9c547cSRui Paulo 		wpa_s->mac_addr_pno = tmp;
31655b9c547cSRui Paulo 	} else {
31665b9c547cSRui Paulo 		wpa_printf(MSG_INFO,
31675b9c547cSRui Paulo 			   "scan: Invalid MAC randomization type=0x%x",
31685b9c547cSRui Paulo 			   type);
31695b9c547cSRui Paulo 		os_free(tmp);
31705b9c547cSRui Paulo 		return -1;
31715b9c547cSRui Paulo 	}
31725b9c547cSRui Paulo 
31735b9c547cSRui Paulo 	wpa_s->mac_addr_rand_enable |= type;
31745b9c547cSRui Paulo 	return 0;
31755b9c547cSRui Paulo }
3176780fb4a2SCy Schubert 
3177780fb4a2SCy Schubert 
wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant * wpa_s,unsigned int type,u8 * mask)3178c1d255d3SCy Schubert int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3179c1d255d3SCy Schubert 				     unsigned int type, u8 *mask)
3180c1d255d3SCy Schubert {
3181c1d255d3SCy Schubert 	const u8 *to_copy;
3182c1d255d3SCy Schubert 
3183c1d255d3SCy Schubert 	if ((wpa_s->mac_addr_rand_enable & type) != type)
3184c1d255d3SCy Schubert 		return -1;
3185c1d255d3SCy Schubert 
3186c1d255d3SCy Schubert 	if (type == MAC_ADDR_RAND_SCAN) {
3187c1d255d3SCy Schubert 		to_copy = wpa_s->mac_addr_scan;
3188c1d255d3SCy Schubert 	} else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3189c1d255d3SCy Schubert 		to_copy = wpa_s->mac_addr_sched_scan;
3190c1d255d3SCy Schubert 	} else if (type == MAC_ADDR_RAND_PNO) {
3191c1d255d3SCy Schubert 		to_copy = wpa_s->mac_addr_pno;
3192c1d255d3SCy Schubert 	} else {
3193c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG,
3194c1d255d3SCy Schubert 			   "scan: Invalid MAC randomization type=0x%x",
3195c1d255d3SCy Schubert 			   type);
3196c1d255d3SCy Schubert 		return -1;
3197c1d255d3SCy Schubert 	}
3198c1d255d3SCy Schubert 
3199c1d255d3SCy Schubert 	os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3200c1d255d3SCy Schubert 	return 0;
3201c1d255d3SCy Schubert }
3202c1d255d3SCy Schubert 
3203c1d255d3SCy Schubert 
wpas_abort_ongoing_scan(struct wpa_supplicant * wpa_s)3204780fb4a2SCy Schubert int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3205780fb4a2SCy Schubert {
320685732ac8SCy Schubert 	struct wpa_radio_work *work;
320785732ac8SCy Schubert 	struct wpa_radio *radio = wpa_s->radio;
3208780fb4a2SCy Schubert 
320985732ac8SCy Schubert 	dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
321085732ac8SCy Schubert 		if (work->wpa_s != wpa_s || !work->started ||
321185732ac8SCy Schubert 		    (os_strcmp(work->type, "scan") != 0 &&
321285732ac8SCy Schubert 		     os_strcmp(work->type, "p2p-scan") != 0))
321385732ac8SCy Schubert 			continue;
3214780fb4a2SCy Schubert 		wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
321585732ac8SCy Schubert 		return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
3216780fb4a2SCy Schubert 	}
3217780fb4a2SCy Schubert 
321885732ac8SCy Schubert 	wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
321985732ac8SCy Schubert 	return -1;
3220780fb4a2SCy Schubert }
3221780fb4a2SCy Schubert 
3222780fb4a2SCy Schubert 
wpas_sched_scan_plans_set(struct wpa_supplicant * wpa_s,const char * cmd)3223780fb4a2SCy Schubert int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3224780fb4a2SCy Schubert {
3225780fb4a2SCy Schubert 	struct sched_scan_plan *scan_plans = NULL;
3226780fb4a2SCy Schubert 	const char *token, *context = NULL;
3227780fb4a2SCy Schubert 	unsigned int num = 0;
3228780fb4a2SCy Schubert 
3229780fb4a2SCy Schubert 	if (!cmd)
3230780fb4a2SCy Schubert 		return -1;
3231780fb4a2SCy Schubert 
3232780fb4a2SCy Schubert 	if (!cmd[0]) {
3233780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3234780fb4a2SCy Schubert 		os_free(wpa_s->sched_scan_plans);
3235780fb4a2SCy Schubert 		wpa_s->sched_scan_plans = NULL;
3236780fb4a2SCy Schubert 		wpa_s->sched_scan_plans_num = 0;
3237780fb4a2SCy Schubert 		return 0;
3238780fb4a2SCy Schubert 	}
3239780fb4a2SCy Schubert 
3240780fb4a2SCy Schubert 	while ((token = cstr_token(cmd, " ", &context))) {
3241780fb4a2SCy Schubert 		int ret;
3242780fb4a2SCy Schubert 		struct sched_scan_plan *scan_plan, *n;
3243780fb4a2SCy Schubert 
3244780fb4a2SCy Schubert 		n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3245780fb4a2SCy Schubert 		if (!n)
3246780fb4a2SCy Schubert 			goto fail;
3247780fb4a2SCy Schubert 
3248780fb4a2SCy Schubert 		scan_plans = n;
3249780fb4a2SCy Schubert 		scan_plan = &scan_plans[num];
3250780fb4a2SCy Schubert 		num++;
3251780fb4a2SCy Schubert 
3252780fb4a2SCy Schubert 		ret = sscanf(token, "%u:%u", &scan_plan->interval,
3253780fb4a2SCy Schubert 			     &scan_plan->iterations);
3254780fb4a2SCy Schubert 		if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3255780fb4a2SCy Schubert 			wpa_printf(MSG_ERROR,
3256780fb4a2SCy Schubert 				   "Invalid sched scan plan input: %s", token);
3257780fb4a2SCy Schubert 			goto fail;
3258780fb4a2SCy Schubert 		}
3259780fb4a2SCy Schubert 
3260780fb4a2SCy Schubert 		if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3261780fb4a2SCy Schubert 			wpa_printf(MSG_WARNING,
3262780fb4a2SCy Schubert 				   "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3263780fb4a2SCy Schubert 				   num, scan_plan->interval,
3264780fb4a2SCy Schubert 				   wpa_s->max_sched_scan_plan_interval);
3265780fb4a2SCy Schubert 			scan_plan->interval =
3266780fb4a2SCy Schubert 				wpa_s->max_sched_scan_plan_interval;
3267780fb4a2SCy Schubert 		}
3268780fb4a2SCy Schubert 
3269780fb4a2SCy Schubert 		if (ret == 1) {
3270780fb4a2SCy Schubert 			scan_plan->iterations = 0;
3271780fb4a2SCy Schubert 			break;
3272780fb4a2SCy Schubert 		}
3273780fb4a2SCy Schubert 
3274780fb4a2SCy Schubert 		if (!scan_plan->iterations) {
3275780fb4a2SCy Schubert 			wpa_printf(MSG_ERROR,
3276780fb4a2SCy Schubert 				   "scan plan %u: Number of iterations cannot be zero",
3277780fb4a2SCy Schubert 				   num);
3278780fb4a2SCy Schubert 			goto fail;
3279780fb4a2SCy Schubert 		}
3280780fb4a2SCy Schubert 
3281780fb4a2SCy Schubert 		if (scan_plan->iterations >
3282780fb4a2SCy Schubert 		    wpa_s->max_sched_scan_plan_iterations) {
3283780fb4a2SCy Schubert 			wpa_printf(MSG_WARNING,
3284780fb4a2SCy Schubert 				   "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3285780fb4a2SCy Schubert 				   num, scan_plan->iterations,
3286780fb4a2SCy Schubert 				   wpa_s->max_sched_scan_plan_iterations);
3287780fb4a2SCy Schubert 			scan_plan->iterations =
3288780fb4a2SCy Schubert 				wpa_s->max_sched_scan_plan_iterations;
3289780fb4a2SCy Schubert 		}
3290780fb4a2SCy Schubert 
3291780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG,
3292780fb4a2SCy Schubert 			   "scan plan %u: interval=%u iterations=%u",
3293780fb4a2SCy Schubert 			   num, scan_plan->interval, scan_plan->iterations);
3294780fb4a2SCy Schubert 	}
3295780fb4a2SCy Schubert 
3296780fb4a2SCy Schubert 	if (!scan_plans) {
3297780fb4a2SCy Schubert 		wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3298780fb4a2SCy Schubert 		goto fail;
3299780fb4a2SCy Schubert 	}
3300780fb4a2SCy Schubert 
3301780fb4a2SCy Schubert 	if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3302780fb4a2SCy Schubert 		wpa_printf(MSG_ERROR,
3303780fb4a2SCy Schubert 			   "All scan plans but the last must specify a number of iterations");
3304780fb4a2SCy Schubert 		goto fail;
3305780fb4a2SCy Schubert 	}
3306780fb4a2SCy Schubert 
3307780fb4a2SCy Schubert 	wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3308780fb4a2SCy Schubert 		   num, scan_plans[num - 1].interval);
3309780fb4a2SCy Schubert 
3310780fb4a2SCy Schubert 	if (num > wpa_s->max_sched_scan_plans) {
3311780fb4a2SCy Schubert 		wpa_printf(MSG_WARNING,
3312780fb4a2SCy Schubert 			   "Too many scheduled scan plans (only %u supported)",
3313780fb4a2SCy Schubert 			   wpa_s->max_sched_scan_plans);
3314780fb4a2SCy Schubert 		wpa_printf(MSG_WARNING,
3315780fb4a2SCy Schubert 			   "Use only the first %u scan plans, and the last one (in infinite loop)",
3316780fb4a2SCy Schubert 			   wpa_s->max_sched_scan_plans - 1);
3317780fb4a2SCy Schubert 		os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3318780fb4a2SCy Schubert 			  &scan_plans[num - 1], sizeof(*scan_plans));
3319780fb4a2SCy Schubert 		num = wpa_s->max_sched_scan_plans;
3320780fb4a2SCy Schubert 	}
3321780fb4a2SCy Schubert 
3322780fb4a2SCy Schubert 	os_free(wpa_s->sched_scan_plans);
3323780fb4a2SCy Schubert 	wpa_s->sched_scan_plans = scan_plans;
3324780fb4a2SCy Schubert 	wpa_s->sched_scan_plans_num = num;
3325780fb4a2SCy Schubert 
3326780fb4a2SCy Schubert 	return 0;
3327780fb4a2SCy Schubert 
3328780fb4a2SCy Schubert fail:
3329780fb4a2SCy Schubert 	os_free(scan_plans);
3330780fb4a2SCy Schubert 	wpa_printf(MSG_ERROR, "invalid scan plans list");
3331780fb4a2SCy Schubert 	return -1;
3332780fb4a2SCy Schubert }
3333780fb4a2SCy Schubert 
3334780fb4a2SCy Schubert 
3335780fb4a2SCy Schubert /**
3336780fb4a2SCy Schubert  * wpas_scan_reset_sched_scan - Reset sched_scan state
3337780fb4a2SCy Schubert  * @wpa_s: Pointer to wpa_supplicant data
3338780fb4a2SCy Schubert  *
3339780fb4a2SCy Schubert  * This function is used to cancel a running scheduled scan and to reset an
3340780fb4a2SCy Schubert  * internal scan state to continue with a regular scan on the following
3341780fb4a2SCy Schubert  * wpa_supplicant_req_scan() calls.
3342780fb4a2SCy Schubert  */
wpas_scan_reset_sched_scan(struct wpa_supplicant * wpa_s)3343780fb4a2SCy Schubert void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3344780fb4a2SCy Schubert {
3345780fb4a2SCy Schubert 	wpa_s->normal_scans = 0;
3346780fb4a2SCy Schubert 	if (wpa_s->sched_scanning) {
3347780fb4a2SCy Schubert 		wpa_s->sched_scan_timed_out = 0;
3348780fb4a2SCy Schubert 		wpa_s->prev_sched_ssid = NULL;
3349780fb4a2SCy Schubert 		wpa_supplicant_cancel_sched_scan(wpa_s);
3350780fb4a2SCy Schubert 	}
3351780fb4a2SCy Schubert }
3352780fb4a2SCy Schubert 
3353780fb4a2SCy Schubert 
wpas_scan_restart_sched_scan(struct wpa_supplicant * wpa_s)3354780fb4a2SCy Schubert void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3355780fb4a2SCy Schubert {
3356780fb4a2SCy Schubert 	/* simulate timeout to restart the sched scan */
3357780fb4a2SCy Schubert 	wpa_s->sched_scan_timed_out = 1;
3358780fb4a2SCy Schubert 	wpa_s->prev_sched_ssid = NULL;
3359780fb4a2SCy Schubert 	wpa_supplicant_cancel_sched_scan(wpa_s);
3360780fb4a2SCy Schubert }
3361