xref: /freebsd/contrib/wpa/wpa_supplicant/scan.c (revision 19261079)
1 /*
2  * WPA Supplicant - Scanning
3  * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "config.h"
16 #include "wpa_supplicant_i.h"
17 #include "driver_i.h"
18 #include "wps_supplicant.h"
19 #include "p2p_supplicant.h"
20 #include "p2p/p2p.h"
21 #include "hs20_supplicant.h"
22 #include "notify.h"
23 #include "bss.h"
24 #include "scan.h"
25 #include "mesh.h"
26 
27 
28 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29 {
30 	struct wpa_ssid *ssid;
31 	union wpa_event_data data;
32 
33 	ssid = wpa_supplicant_get_ssid(wpa_s);
34 	if (ssid == NULL)
35 		return;
36 
37 	if (wpa_s->current_ssid == NULL) {
38 		wpa_s->current_ssid = ssid;
39 		wpas_notify_network_changed(wpa_s);
40 	}
41 	wpa_supplicant_initiate_eapol(wpa_s);
42 	wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 		"network - generating associated event");
44 	os_memset(&data, 0, sizeof(data));
45 	wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46 }
47 
48 
49 #ifdef CONFIG_WPS
50 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
51 			   enum wps_request_type *req_type)
52 {
53 	struct wpa_ssid *ssid;
54 	int wps = 0;
55 
56 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
57 		if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 			continue;
59 
60 		wps = 1;
61 		*req_type = wpas_wps_get_req_type(ssid);
62 		if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
63 			return 2;
64 	}
65 
66 #ifdef CONFIG_P2P
67 	if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68 	    !wpa_s->conf->p2p_disabled) {
69 		wpa_s->wps->dev.p2p = 1;
70 		if (!wps) {
71 			wps = 1;
72 			*req_type = WPS_REQ_ENROLLEE_INFO;
73 		}
74 	}
75 #endif /* CONFIG_P2P */
76 
77 	return wps;
78 }
79 #endif /* CONFIG_WPS */
80 
81 
82 static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
83 					  const u8 *mac_addr)
84 {
85 	u8 *tmp;
86 
87 	if (params->mac_addr) {
88 		params->mac_addr_mask = NULL;
89 		os_free(params->mac_addr);
90 		params->mac_addr = NULL;
91 	}
92 
93 	params->mac_addr_rand = 1;
94 
95 	if (!mac_addr)
96 		return 0;
97 
98 	tmp = os_malloc(2 * ETH_ALEN);
99 	if (!tmp)
100 		return -1;
101 
102 	os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
103 	params->mac_addr = tmp;
104 	params->mac_addr_mask = tmp + ETH_ALEN;
105 	return 0;
106 }
107 
108 
109 /**
110  * wpa_supplicant_enabled_networks - Check whether there are enabled networks
111  * @wpa_s: Pointer to wpa_supplicant data
112  * Returns: 0 if no networks are enabled, >0 if networks are enabled
113  *
114  * This function is used to figure out whether any networks (or Interworking
115  * with enabled credentials and auto_interworking) are present in the current
116  * configuration.
117  */
118 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
119 {
120 	struct wpa_ssid *ssid = wpa_s->conf->ssid;
121 	int count = 0, disabled = 0;
122 
123 	if (wpa_s->p2p_mgmt)
124 		return 0; /* no normal network profiles on p2p_mgmt interface */
125 
126 	while (ssid) {
127 		if (!wpas_network_disabled(wpa_s, ssid))
128 			count++;
129 		else
130 			disabled++;
131 		ssid = ssid->next;
132 	}
133 	if (wpa_s->conf->cred && wpa_s->conf->interworking &&
134 	    wpa_s->conf->auto_interworking)
135 		count++;
136 	if (count == 0 && disabled > 0) {
137 		wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
138 			"networks)", disabled);
139 	}
140 	return count;
141 }
142 
143 
144 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
145 				     struct wpa_ssid *ssid)
146 {
147 	int min_temp_disabled = 0;
148 
149 	while (ssid) {
150 		if (!wpas_network_disabled(wpa_s, ssid)) {
151 			int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
152 
153 			if (temp_disabled <= 0)
154 				break;
155 
156 			if (!min_temp_disabled ||
157 			    temp_disabled < min_temp_disabled)
158 				min_temp_disabled = temp_disabled;
159 		}
160 		ssid = ssid->next;
161 	}
162 
163 	/* ap_scan=2 mode - try to associate with each SSID. */
164 	if (ssid == NULL) {
165 		wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
166 			"end of scan list - go back to beginning");
167 		wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
168 		wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
169 		return;
170 	}
171 	if (ssid->next) {
172 		/* Continue from the next SSID on the next attempt. */
173 		wpa_s->prev_scan_ssid = ssid;
174 	} else {
175 		/* Start from the beginning of the SSID list. */
176 		wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
177 	}
178 	wpa_supplicant_associate(wpa_s, NULL, ssid);
179 }
180 
181 
182 static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
183 {
184 	struct wpa_supplicant *wpa_s = work->wpa_s;
185 	struct wpa_driver_scan_params *params = work->ctx;
186 	int ret;
187 
188 	if (deinit) {
189 		if (!work->started) {
190 			wpa_scan_free_params(params);
191 			return;
192 		}
193 		wpa_supplicant_notify_scanning(wpa_s, 0);
194 		wpas_notify_scan_done(wpa_s, 0);
195 		wpa_s->scan_work = NULL;
196 		return;
197 	}
198 
199 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
200 	    wpa_s->wpa_state <= WPA_SCANNING)
201 		wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
202 
203 	if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
204 		wpa_msg(wpa_s, MSG_INFO,
205 			"Failed to assign random MAC address for a scan");
206 		wpa_scan_free_params(params);
207 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
208 		radio_work_done(work);
209 		return;
210 	}
211 
212 	wpa_supplicant_notify_scanning(wpa_s, 1);
213 
214 	if (wpa_s->clear_driver_scan_cache) {
215 		wpa_printf(MSG_DEBUG,
216 			   "Request driver to clear scan cache due to local BSS flush");
217 		params->only_new_results = 1;
218 	}
219 	ret = wpa_drv_scan(wpa_s, params);
220 	/*
221 	 * Store the obtained vendor scan cookie (if any) in wpa_s context.
222 	 * The current design is to allow only one scan request on each
223 	 * interface, hence having this scan cookie stored in wpa_s context is
224 	 * fine for now.
225 	 *
226 	 * Revisit this logic if concurrent scan operations per interface
227 	 * is supported.
228 	 */
229 	if (ret == 0)
230 		wpa_s->curr_scan_cookie = params->scan_cookie;
231 	wpa_scan_free_params(params);
232 	work->ctx = NULL;
233 	if (ret) {
234 		int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
235 			!wpa_s->beacon_rep_data.token;
236 
237 		if (wpa_s->disconnected)
238 			retry = 0;
239 
240 		/* do not retry if operation is not supported */
241 		if (ret == -EOPNOTSUPP)
242 			retry = 0;
243 
244 		wpa_supplicant_notify_scanning(wpa_s, 0);
245 		wpas_notify_scan_done(wpa_s, 0);
246 		if (wpa_s->wpa_state == WPA_SCANNING)
247 			wpa_supplicant_set_state(wpa_s,
248 						 wpa_s->scan_prev_wpa_state);
249 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
250 			ret, retry ? " retry=1" : "");
251 		radio_work_done(work);
252 
253 		if (retry) {
254 			/* Restore scan_req since we will try to scan again */
255 			wpa_s->scan_req = wpa_s->last_scan_req;
256 			wpa_supplicant_req_scan(wpa_s, 1, 0);
257 		} else if (wpa_s->scan_res_handler) {
258 			/* Clear the scan_res_handler */
259 			wpa_s->scan_res_handler = NULL;
260 		}
261 
262 		if (wpa_s->beacon_rep_data.token)
263 			wpas_rrm_refuse_request(wpa_s);
264 
265 		return;
266 	}
267 
268 	os_get_reltime(&wpa_s->scan_trigger_time);
269 	wpa_s->scan_runs++;
270 	wpa_s->normal_scans++;
271 	wpa_s->own_scan_requested = 1;
272 	wpa_s->clear_driver_scan_cache = 0;
273 	wpa_s->scan_work = work;
274 }
275 
276 
277 /**
278  * wpa_supplicant_trigger_scan - Request driver to start a scan
279  * @wpa_s: Pointer to wpa_supplicant data
280  * @params: Scan parameters
281  * Returns: 0 on success, -1 on failure
282  */
283 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
284 				struct wpa_driver_scan_params *params)
285 {
286 	struct wpa_driver_scan_params *ctx;
287 
288 	if (wpa_s->scan_work) {
289 		wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
290 		return -1;
291 	}
292 
293 	ctx = wpa_scan_clone_params(params);
294 	if (!ctx ||
295 	    radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
296 	{
297 		wpa_scan_free_params(ctx);
298 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
299 		return -1;
300 	}
301 
302 	return 0;
303 }
304 
305 
306 static void
307 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
308 {
309 	struct wpa_supplicant *wpa_s = eloop_ctx;
310 
311 	wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
312 
313 	if (wpa_supplicant_req_sched_scan(wpa_s))
314 		wpa_supplicant_req_scan(wpa_s, 0, 0);
315 }
316 
317 
318 static void
319 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
320 {
321 	struct wpa_supplicant *wpa_s = eloop_ctx;
322 
323 	wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
324 
325 	wpa_s->sched_scan_timed_out = 1;
326 	wpa_supplicant_cancel_sched_scan(wpa_s);
327 }
328 
329 
330 static int
331 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
332 				struct wpa_driver_scan_params *params)
333 {
334 	int ret;
335 
336 	wpa_supplicant_notify_scanning(wpa_s, 1);
337 	ret = wpa_drv_sched_scan(wpa_s, params);
338 	if (ret)
339 		wpa_supplicant_notify_scanning(wpa_s, 0);
340 	else
341 		wpa_s->sched_scanning = 1;
342 
343 	return ret;
344 }
345 
346 
347 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
348 {
349 	int ret;
350 
351 	ret = wpa_drv_stop_sched_scan(wpa_s);
352 	if (ret) {
353 		wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
354 		/* TODO: what to do if stopping fails? */
355 		return -1;
356 	}
357 
358 	return ret;
359 }
360 
361 
362 static struct wpa_driver_scan_filter *
363 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
364 {
365 	struct wpa_driver_scan_filter *ssids;
366 	struct wpa_ssid *ssid;
367 	size_t count;
368 
369 	*num_ssids = 0;
370 	if (!conf->filter_ssids)
371 		return NULL;
372 
373 	for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
374 		if (ssid->ssid && ssid->ssid_len)
375 			count++;
376 	}
377 	if (count == 0)
378 		return NULL;
379 	ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
380 	if (ssids == NULL)
381 		return NULL;
382 
383 	for (ssid = conf->ssid; ssid; ssid = ssid->next) {
384 		if (!ssid->ssid || !ssid->ssid_len)
385 			continue;
386 		os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
387 		ssids[*num_ssids].ssid_len = ssid->ssid_len;
388 		(*num_ssids)++;
389 	}
390 
391 	return ssids;
392 }
393 
394 
395 static void wpa_supplicant_optimize_freqs(
396 	struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
397 {
398 #ifdef CONFIG_P2P
399 	if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
400 	    wpa_s->go_params) {
401 		/* Optimize provisioning state scan based on GO information */
402 		if (wpa_s->p2p_in_provisioning < 5 &&
403 		    wpa_s->go_params->freq > 0) {
404 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
405 				"preferred frequency %d MHz",
406 				wpa_s->go_params->freq);
407 			params->freqs = os_calloc(2, sizeof(int));
408 			if (params->freqs)
409 				params->freqs[0] = wpa_s->go_params->freq;
410 		} else if (wpa_s->p2p_in_provisioning < 8 &&
411 			   wpa_s->go_params->freq_list[0]) {
412 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
413 				"channels");
414 			int_array_concat(&params->freqs,
415 					 wpa_s->go_params->freq_list);
416 			if (params->freqs)
417 				int_array_sort_unique(params->freqs);
418 		}
419 		wpa_s->p2p_in_provisioning++;
420 	}
421 
422 	if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
423 		/*
424 		 * Optimize scan based on GO information during persistent
425 		 * group reinvocation
426 		 */
427 		if (wpa_s->p2p_in_invitation < 5 &&
428 		    wpa_s->p2p_invite_go_freq > 0) {
429 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
430 				wpa_s->p2p_invite_go_freq);
431 			params->freqs = os_calloc(2, sizeof(int));
432 			if (params->freqs)
433 				params->freqs[0] = wpa_s->p2p_invite_go_freq;
434 		}
435 		wpa_s->p2p_in_invitation++;
436 		if (wpa_s->p2p_in_invitation > 20) {
437 			/*
438 			 * This should not really happen since the variable is
439 			 * cleared on group removal, but if it does happen, make
440 			 * sure we do not get stuck in special invitation scan
441 			 * mode.
442 			 */
443 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
444 			wpa_s->p2p_in_invitation = 0;
445 		}
446 	}
447 #endif /* CONFIG_P2P */
448 
449 #ifdef CONFIG_WPS
450 	if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
451 		/*
452 		 * Optimize post-provisioning scan based on channel used
453 		 * during provisioning.
454 		 */
455 		wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
456 			"that was used during provisioning", wpa_s->wps_freq);
457 		params->freqs = os_calloc(2, sizeof(int));
458 		if (params->freqs)
459 			params->freqs[0] = wpa_s->wps_freq;
460 		wpa_s->after_wps--;
461 	} else if (wpa_s->after_wps)
462 		wpa_s->after_wps--;
463 
464 	if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
465 	{
466 		/* Optimize provisioning scan based on already known channel */
467 		wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
468 			wpa_s->wps_freq);
469 		params->freqs = os_calloc(2, sizeof(int));
470 		if (params->freqs)
471 			params->freqs[0] = wpa_s->wps_freq;
472 		wpa_s->known_wps_freq = 0; /* only do this once */
473 	}
474 #endif /* CONFIG_WPS */
475 }
476 
477 
478 #ifdef CONFIG_INTERWORKING
479 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
480 					   struct wpabuf *buf)
481 {
482 	wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
483 	wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
484 		      1 + ETH_ALEN);
485 	wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
486 	/* No Venue Info */
487 	if (!is_zero_ether_addr(wpa_s->conf->hessid))
488 		wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
489 }
490 #endif /* CONFIG_INTERWORKING */
491 
492 
493 #ifdef CONFIG_MBO
494 static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
495 						struct wpabuf **ie)
496 {
497 	if (wpabuf_resize(ie, 5)) {
498 		wpa_printf(MSG_DEBUG,
499 			   "Failed to allocate space for FILS Request Parameters element");
500 		return;
501 	}
502 
503 	/* FILS Request Parameters element */
504 	wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
505 	wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
506 	wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
507 	/* Parameter control bitmap */
508 	wpabuf_put_u8(*ie, 0);
509 	/* Max Channel Time field - contains the value of MaxChannelTime
510 	 * parameter of the MLME-SCAN.request primitive represented in units of
511 	 * TUs, as an unsigned integer. A Max Channel Time field value of 255
512 	 * is used to indicate any duration of more than 254 TUs, or an
513 	 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
514 	 */
515 	wpabuf_put_u8(*ie, 255);
516 }
517 #endif /* CONFIG_MBO */
518 
519 
520 void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
521 {
522 	struct wpabuf *default_ies = NULL;
523 	u8 ext_capab[18];
524 	int ext_capab_len, frame_id;
525 	enum wpa_driver_if_type type = WPA_IF_STATION;
526 
527 #ifdef CONFIG_P2P
528 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
529 		type = WPA_IF_P2P_CLIENT;
530 #endif /* CONFIG_P2P */
531 
532 	wpa_drv_get_ext_capa(wpa_s, type);
533 
534 	ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
535 					     sizeof(ext_capab));
536 	if (ext_capab_len > 0 &&
537 	    wpabuf_resize(&default_ies, ext_capab_len) == 0)
538 		wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
539 
540 #ifdef CONFIG_MBO
541 	if (wpa_s->enable_oce & OCE_STA)
542 		wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
543 	/* Send MBO and OCE capabilities */
544 	if (wpabuf_resize(&default_ies, 12) == 0)
545 		wpas_mbo_scan_ie(wpa_s, default_ies);
546 #endif /* CONFIG_MBO */
547 
548 	if (type == WPA_IF_P2P_CLIENT)
549 		frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
550 	else
551 		frame_id = VENDOR_ELEM_PROBE_REQ;
552 
553 	if (wpa_s->vendor_elem[frame_id]) {
554 		size_t len;
555 
556 		len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
557 		if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
558 			wpabuf_put_buf(default_ies,
559 				       wpa_s->vendor_elem[frame_id]);
560 	}
561 
562 	if (default_ies)
563 		wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
564 					     wpabuf_len(default_ies));
565 	wpabuf_free(default_ies);
566 }
567 
568 
569 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
570 {
571 	struct wpabuf *extra_ie = NULL;
572 	u8 ext_capab[18];
573 	int ext_capab_len;
574 #ifdef CONFIG_WPS
575 	int wps = 0;
576 	enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
577 #endif /* CONFIG_WPS */
578 
579 #ifdef CONFIG_P2P
580 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
581 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
582 	else
583 #endif /* CONFIG_P2P */
584 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
585 
586 	ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
587 					     sizeof(ext_capab));
588 	if (ext_capab_len > 0 &&
589 	    wpabuf_resize(&extra_ie, ext_capab_len) == 0)
590 		wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
591 
592 #ifdef CONFIG_INTERWORKING
593 	if (wpa_s->conf->interworking &&
594 	    wpabuf_resize(&extra_ie, 100) == 0)
595 		wpas_add_interworking_elements(wpa_s, extra_ie);
596 #endif /* CONFIG_INTERWORKING */
597 
598 #ifdef CONFIG_MBO
599 	if (wpa_s->enable_oce & OCE_STA)
600 		wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
601 #endif /* CONFIG_MBO */
602 
603 #ifdef CONFIG_WPS
604 	wps = wpas_wps_in_use(wpa_s, &req_type);
605 
606 	if (wps) {
607 		struct wpabuf *wps_ie;
608 		wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
609 						DEV_PW_DEFAULT,
610 						&wpa_s->wps->dev,
611 						wpa_s->wps->uuid, req_type,
612 						0, NULL);
613 		if (wps_ie) {
614 			if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
615 				wpabuf_put_buf(extra_ie, wps_ie);
616 			wpabuf_free(wps_ie);
617 		}
618 	}
619 
620 #ifdef CONFIG_P2P
621 	if (wps) {
622 		size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
623 		if (wpabuf_resize(&extra_ie, ielen) == 0)
624 			wpas_p2p_scan_ie(wpa_s, extra_ie);
625 	}
626 #endif /* CONFIG_P2P */
627 
628 	wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
629 
630 #endif /* CONFIG_WPS */
631 
632 #ifdef CONFIG_HS20
633 	if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
634 		wpas_hs20_add_indication(extra_ie, -1, 0);
635 #endif /* CONFIG_HS20 */
636 
637 #ifdef CONFIG_FST
638 	if (wpa_s->fst_ies &&
639 	    wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
640 		wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
641 #endif /* CONFIG_FST */
642 
643 #ifdef CONFIG_MBO
644 	/* Send MBO and OCE capabilities */
645 	if (wpabuf_resize(&extra_ie, 12) == 0)
646 		wpas_mbo_scan_ie(wpa_s, extra_ie);
647 #endif /* CONFIG_MBO */
648 
649 	if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
650 		struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
651 
652 		if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
653 			wpabuf_put_buf(extra_ie, buf);
654 	}
655 
656 	return extra_ie;
657 }
658 
659 
660 #ifdef CONFIG_P2P
661 
662 /*
663  * Check whether there are any enabled networks or credentials that could be
664  * used for a non-P2P connection.
665  */
666 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
667 {
668 	struct wpa_ssid *ssid;
669 
670 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
671 		if (wpas_network_disabled(wpa_s, ssid))
672 			continue;
673 		if (!ssid->p2p_group)
674 			return 1;
675 	}
676 
677 	if (wpa_s->conf->cred && wpa_s->conf->interworking &&
678 	    wpa_s->conf->auto_interworking)
679 		return 1;
680 
681 	return 0;
682 }
683 
684 #endif /* CONFIG_P2P */
685 
686 
687 int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
688 			    enum hostapd_hw_mode band,
689 			    struct wpa_driver_scan_params *params, bool is_6ghz)
690 {
691 	/* Include only supported channels for the specified band */
692 	struct hostapd_hw_modes *mode;
693 	int num_chans = 0;
694 	int *freqs, i;
695 
696 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
697 	if (!mode)
698 		return -1;
699 
700 	if (params->freqs) {
701 		while (params->freqs[num_chans])
702 			num_chans++;
703 	}
704 
705 	freqs = os_realloc(params->freqs,
706 			   (num_chans + mode->num_channels + 1) * sizeof(int));
707 	if (!freqs)
708 		return -1;
709 
710 	params->freqs = freqs;
711 	for (i = 0; i < mode->num_channels; i++) {
712 		if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
713 			continue;
714 		params->freqs[num_chans++] = mode->channels[i].freq;
715 	}
716 	params->freqs[num_chans] = 0;
717 
718 	return 0;
719 }
720 
721 
722 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
723 				   struct wpa_driver_scan_params *params)
724 {
725 	if (wpa_s->hw.modes == NULL)
726 		return; /* unknown what channels the driver supports */
727 	if (params->freqs)
728 		return; /* already using a limited channel set */
729 
730 	if (wpa_s->setband_mask & WPA_SETBAND_5G)
731 		wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
732 					0);
733 	if (wpa_s->setband_mask & WPA_SETBAND_2G)
734 		wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
735 					0);
736 	if (wpa_s->setband_mask & WPA_SETBAND_6G)
737 		wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
738 					1);
739 }
740 
741 
742 static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
743 			      struct wpa_driver_scan_params *params,
744 			      size_t max_ssids, const u8 *ssid, size_t ssid_len)
745 {
746 	unsigned int j;
747 
748 	for (j = 0; j < params->num_ssids; j++) {
749 		if (params->ssids[j].ssid_len == ssid_len &&
750 		    params->ssids[j].ssid &&
751 		    os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
752 			return; /* already in the list */
753 	}
754 
755 	if (params->num_ssids + 1 > max_ssids) {
756 		wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
757 		return;
758 	}
759 
760 	wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
761 		   wpa_ssid_txt(ssid, ssid_len));
762 
763 	params->ssids[params->num_ssids].ssid = ssid;
764 	params->ssids[params->num_ssids].ssid_len = ssid_len;
765 	params->num_ssids++;
766 }
767 
768 
769 static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
770 				  struct wpa_driver_scan_params *params,
771 				  struct wpa_ssid *ssid, size_t max_ssids)
772 {
773 #ifdef CONFIG_OWE
774 	struct wpa_bss *bss;
775 
776 	if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
777 		return;
778 
779 	wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
780 		   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
781 
782 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
783 		const u8 *owe, *pos, *end;
784 		const u8 *owe_ssid;
785 		size_t owe_ssid_len;
786 
787 		if (bss->ssid_len != ssid->ssid_len ||
788 		    os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
789 			continue;
790 
791 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
792 		if (!owe || owe[1] < 4)
793 			continue;
794 
795 		pos = owe + 6;
796 		end = owe + 2 + owe[1];
797 
798 		/* Must include BSSID and ssid_len */
799 		if (end - pos < ETH_ALEN + 1)
800 			return;
801 
802 		/* Skip BSSID */
803 		pos += ETH_ALEN;
804 		owe_ssid_len = *pos++;
805 		owe_ssid = pos;
806 
807 		if ((size_t) (end - pos) < owe_ssid_len ||
808 		    owe_ssid_len > SSID_MAX_LEN)
809 			return;
810 
811 		wpa_printf(MSG_DEBUG,
812 			   "OWE: scan_ssids: transition mode OWE ssid=%s",
813 			   wpa_ssid_txt(owe_ssid, owe_ssid_len));
814 
815 		wpa_add_scan_ssid(wpa_s, params, max_ssids,
816 				  owe_ssid, owe_ssid_len);
817 		return;
818 	}
819 #endif /* CONFIG_OWE */
820 }
821 
822 
823 static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
824 			       struct wpa_driver_scan_params *params,
825 			       size_t max_ssids)
826 {
827 	unsigned int i;
828 	struct wpa_ssid *ssid;
829 
830 	/*
831 	 * For devices with max_ssids greater than 1, leave the last slot empty
832 	 * for adding the wildcard scan entry.
833 	 */
834 	max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
835 
836 	for (i = 0; i < wpa_s->scan_id_count; i++) {
837 		ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
838 		if (!ssid)
839 			continue;
840 		if (ssid->scan_ssid)
841 			wpa_add_scan_ssid(wpa_s, params, max_ssids,
842 					  ssid->ssid, ssid->ssid_len);
843 		/*
844 		 * Also add the SSID of the OWE BSS, to allow discovery of
845 		 * transition mode APs more quickly.
846 		 */
847 		wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
848 	}
849 
850 	wpa_s->scan_id_count = 0;
851 }
852 
853 
854 static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
855 				       struct wpa_driver_scan_params *params,
856 				       size_t max_ssids)
857 {
858 	unsigned int i;
859 
860 	if (wpa_s->ssids_from_scan_req == NULL ||
861 	    wpa_s->num_ssids_from_scan_req == 0)
862 		return 0;
863 
864 	if (wpa_s->num_ssids_from_scan_req > max_ssids) {
865 		wpa_s->num_ssids_from_scan_req = max_ssids;
866 		wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
867 			   (unsigned int) max_ssids);
868 	}
869 
870 	for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
871 		params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
872 		params->ssids[i].ssid_len =
873 			wpa_s->ssids_from_scan_req[i].ssid_len;
874 		wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
875 				  params->ssids[i].ssid,
876 				  params->ssids[i].ssid_len);
877 	}
878 
879 	params->num_ssids = wpa_s->num_ssids_from_scan_req;
880 	wpa_s->num_ssids_from_scan_req = 0;
881 	return 1;
882 }
883 
884 
885 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
886 {
887 	struct wpa_supplicant *wpa_s = eloop_ctx;
888 	struct wpa_ssid *ssid;
889 	int ret, p2p_in_prog;
890 	struct wpabuf *extra_ie = NULL;
891 	struct wpa_driver_scan_params params;
892 	struct wpa_driver_scan_params *scan_params;
893 	size_t max_ssids;
894 	int connect_without_scan = 0;
895 
896 	wpa_s->ignore_post_flush_scan_res = 0;
897 
898 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
899 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
900 		return;
901 	}
902 
903 	if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
904 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
905 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
906 		return;
907 	}
908 
909 	if (wpa_s->scanning) {
910 		/*
911 		 * If we are already in scanning state, we shall reschedule the
912 		 * the incoming scan request.
913 		 */
914 		wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
915 		wpa_supplicant_req_scan(wpa_s, 1, 0);
916 		return;
917 	}
918 
919 	if (!wpa_supplicant_enabled_networks(wpa_s) &&
920 	    wpa_s->scan_req == NORMAL_SCAN_REQ) {
921 		wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
922 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
923 		return;
924 	}
925 
926 	if (wpa_s->conf->ap_scan != 0 &&
927 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
928 		wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
929 			"overriding ap_scan configuration");
930 		wpa_s->conf->ap_scan = 0;
931 		wpas_notify_ap_scan_changed(wpa_s);
932 	}
933 
934 	if (wpa_s->conf->ap_scan == 0) {
935 		wpa_supplicant_gen_assoc_event(wpa_s);
936 		return;
937 	}
938 
939 	ssid = NULL;
940 	if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
941 	    wpa_s->connect_without_scan) {
942 		connect_without_scan = 1;
943 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
944 			if (ssid == wpa_s->connect_without_scan)
945 				break;
946 		}
947 	}
948 
949 	p2p_in_prog = wpas_p2p_in_progress(wpa_s);
950 	if (p2p_in_prog && p2p_in_prog != 2 &&
951 	    (!ssid ||
952 	     (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
953 		wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
954 		wpa_supplicant_req_scan(wpa_s, 5, 0);
955 		return;
956 	}
957 
958 	/*
959 	 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
960 	 * used for changing modes inside wpa_supplicant (roaming,
961 	 * auto-reconnect, etc). Discarding the scan might hurt these processes.
962 	 * The normal use case for PNO is to suspend the host immediately after
963 	 * starting PNO, so the periodic 100 ms attempts to run the scan do not
964 	 * normally happen in practice multiple times, i.e., this is simply
965 	 * restarting scanning once the host is woken up and PNO stopped.
966 	 */
967 	if (wpa_s->pno || wpa_s->pno_sched_pending) {
968 		wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
969 		wpa_supplicant_req_scan(wpa_s, 0, 100000);
970 		return;
971 	}
972 
973 	if (wpa_s->conf->ap_scan == 2)
974 		max_ssids = 1;
975 	else {
976 		max_ssids = wpa_s->max_scan_ssids;
977 		if (max_ssids > WPAS_MAX_SCAN_SSIDS)
978 			max_ssids = WPAS_MAX_SCAN_SSIDS;
979 	}
980 
981 	wpa_s->last_scan_req = wpa_s->scan_req;
982 	wpa_s->scan_req = NORMAL_SCAN_REQ;
983 
984 	if (connect_without_scan) {
985 		wpa_s->connect_without_scan = NULL;
986 		if (ssid) {
987 			wpa_printf(MSG_DEBUG, "Start a pre-selected network "
988 				   "without scan step");
989 			wpa_supplicant_associate(wpa_s, NULL, ssid);
990 			return;
991 		}
992 	}
993 
994 	os_memset(&params, 0, sizeof(params));
995 
996 	wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
997 	if (wpa_s->wpa_state == WPA_DISCONNECTED ||
998 	    wpa_s->wpa_state == WPA_INACTIVE)
999 		wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1000 
1001 	/*
1002 	 * If autoscan has set its own scanning parameters
1003 	 */
1004 	if (wpa_s->autoscan_params != NULL) {
1005 		scan_params = wpa_s->autoscan_params;
1006 		goto scan;
1007 	}
1008 
1009 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1010 	    wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1011 		wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1012 		goto ssid_list_set;
1013 	}
1014 
1015 #ifdef CONFIG_P2P
1016 	if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
1017 	    wpa_s->go_params && !wpa_s->conf->passive_scan) {
1018 		wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1019 			   wpa_s->p2p_in_provisioning,
1020 			   wpa_s->show_group_started);
1021 		params.ssids[0].ssid = wpa_s->go_params->ssid;
1022 		params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1023 		params.num_ssids = 1;
1024 		goto ssid_list_set;
1025 	}
1026 
1027 	if (wpa_s->p2p_in_invitation) {
1028 		if (wpa_s->current_ssid) {
1029 			wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1030 			params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1031 			params.ssids[0].ssid_len =
1032 				wpa_s->current_ssid->ssid_len;
1033 			params.num_ssids = 1;
1034 		} else {
1035 			wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1036 		}
1037 		goto ssid_list_set;
1038 	}
1039 #endif /* CONFIG_P2P */
1040 
1041 	/* Find the starting point from which to continue scanning */
1042 	ssid = wpa_s->conf->ssid;
1043 	if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1044 		while (ssid) {
1045 			if (ssid == wpa_s->prev_scan_ssid) {
1046 				ssid = ssid->next;
1047 				break;
1048 			}
1049 			ssid = ssid->next;
1050 		}
1051 	}
1052 
1053 	if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
1054 #ifdef CONFIG_AP
1055 	    !wpa_s->ap_iface &&
1056 #endif /* CONFIG_AP */
1057 	    wpa_s->conf->ap_scan == 2) {
1058 		wpa_s->connect_without_scan = NULL;
1059 		wpa_s->prev_scan_wildcard = 0;
1060 		wpa_supplicant_assoc_try(wpa_s, ssid);
1061 		return;
1062 	} else if (wpa_s->conf->ap_scan == 2) {
1063 		/*
1064 		 * User-initiated scan request in ap_scan == 2; scan with
1065 		 * wildcard SSID.
1066 		 */
1067 		ssid = NULL;
1068 	} else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1069 		/*
1070 		 * Perform single-channel single-SSID scan for
1071 		 * reassociate-to-same-BSS operation.
1072 		 */
1073 		/* Setup SSID */
1074 		ssid = wpa_s->current_ssid;
1075 		wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1076 				  ssid->ssid, ssid->ssid_len);
1077 		params.ssids[0].ssid = ssid->ssid;
1078 		params.ssids[0].ssid_len = ssid->ssid_len;
1079 		params.num_ssids = 1;
1080 
1081 		/*
1082 		 * Allocate memory for frequency array, allocate one extra
1083 		 * slot for the zero-terminator.
1084 		 */
1085 		params.freqs = os_malloc(sizeof(int) * 2);
1086 		if (params.freqs) {
1087 			params.freqs[0] = wpa_s->assoc_freq;
1088 			params.freqs[1] = 0;
1089 		}
1090 
1091 		/*
1092 		 * Reset the reattach flag so that we fall back to full scan if
1093 		 * this scan fails.
1094 		 */
1095 		wpa_s->reattach = 0;
1096 	} else {
1097 		struct wpa_ssid *start = ssid, *tssid;
1098 		int freqs_set = 0;
1099 		if (ssid == NULL && max_ssids > 1)
1100 			ssid = wpa_s->conf->ssid;
1101 		while (ssid) {
1102 			if (!wpas_network_disabled(wpa_s, ssid) &&
1103 			    ssid->scan_ssid) {
1104 				wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1105 						  ssid->ssid, ssid->ssid_len);
1106 				params.ssids[params.num_ssids].ssid =
1107 					ssid->ssid;
1108 				params.ssids[params.num_ssids].ssid_len =
1109 					ssid->ssid_len;
1110 				params.num_ssids++;
1111 				if (params.num_ssids + 1 >= max_ssids)
1112 					break;
1113 			}
1114 
1115 			if (!wpas_network_disabled(wpa_s, ssid)) {
1116 				/*
1117 				 * Also add the SSID of the OWE BSS, to allow
1118 				 * discovery of transition mode APs more
1119 				 * quickly.
1120 				 */
1121 				wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1122 						      max_ssids);
1123 			}
1124 
1125 			ssid = ssid->next;
1126 			if (ssid == start)
1127 				break;
1128 			if (ssid == NULL && max_ssids > 1 &&
1129 			    start != wpa_s->conf->ssid)
1130 				ssid = wpa_s->conf->ssid;
1131 		}
1132 
1133 		if (wpa_s->scan_id_count &&
1134 		    wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1135 			wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1136 
1137 		for (tssid = wpa_s->conf->ssid;
1138 		     wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1139 		     tssid = tssid->next) {
1140 			if (wpas_network_disabled(wpa_s, tssid))
1141 				continue;
1142 			if (((params.freqs || !freqs_set) &&
1143 			     tssid->scan_freq) &&
1144 			    int_array_len(params.freqs) < 100) {
1145 				int_array_concat(&params.freqs,
1146 						 tssid->scan_freq);
1147 			} else {
1148 				os_free(params.freqs);
1149 				params.freqs = NULL;
1150 			}
1151 			freqs_set = 1;
1152 		}
1153 		int_array_sort_unique(params.freqs);
1154 	}
1155 
1156 	if (ssid && max_ssids == 1) {
1157 		/*
1158 		 * If the driver is limited to 1 SSID at a time interleave
1159 		 * wildcard SSID scans with specific SSID scans to avoid
1160 		 * waiting a long time for a wildcard scan.
1161 		 */
1162 		if (!wpa_s->prev_scan_wildcard) {
1163 			params.ssids[0].ssid = NULL;
1164 			params.ssids[0].ssid_len = 0;
1165 			wpa_s->prev_scan_wildcard = 1;
1166 			wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1167 				"wildcard SSID (Interleave with specific)");
1168 		} else {
1169 			wpa_s->prev_scan_ssid = ssid;
1170 			wpa_s->prev_scan_wildcard = 0;
1171 			wpa_dbg(wpa_s, MSG_DEBUG,
1172 				"Starting AP scan for specific SSID: %s",
1173 				wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1174 		}
1175 	} else if (ssid) {
1176 		/* max_ssids > 1 */
1177 
1178 		wpa_s->prev_scan_ssid = ssid;
1179 		wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1180 			"the scan request");
1181 		params.num_ssids++;
1182 	} else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1183 		   wpa_s->manual_scan_passive && params.num_ssids == 0) {
1184 		wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
1185 	} else if (wpa_s->conf->passive_scan) {
1186 		wpa_dbg(wpa_s, MSG_DEBUG,
1187 			"Use passive scan based on configuration");
1188 	} else {
1189 		wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1190 		params.num_ssids++;
1191 		wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1192 			"SSID");
1193 	}
1194 
1195 ssid_list_set:
1196 	wpa_supplicant_optimize_freqs(wpa_s, &params);
1197 	extra_ie = wpa_supplicant_extra_ies(wpa_s);
1198 
1199 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1200 	    wpa_s->manual_scan_only_new) {
1201 		wpa_printf(MSG_DEBUG,
1202 			   "Request driver to clear scan cache due to manual only_new=1 scan");
1203 		params.only_new_results = 1;
1204 	}
1205 
1206 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1207 	    wpa_s->manual_scan_freqs) {
1208 		wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1209 		params.freqs = wpa_s->manual_scan_freqs;
1210 		wpa_s->manual_scan_freqs = NULL;
1211 	}
1212 
1213 	if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1214 		wpa_dbg(wpa_s, MSG_DEBUG,
1215 			"Limit select_network scan to specified channels");
1216 		params.freqs = wpa_s->select_network_scan_freqs;
1217 		wpa_s->select_network_scan_freqs = NULL;
1218 	}
1219 
1220 	if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1221 		wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1222 			"generated frequency list");
1223 		params.freqs = wpa_s->next_scan_freqs;
1224 	} else
1225 		os_free(wpa_s->next_scan_freqs);
1226 	wpa_s->next_scan_freqs = NULL;
1227 	wpa_setband_scan_freqs(wpa_s, &params);
1228 
1229 	/* See if user specified frequencies. If so, scan only those. */
1230 	if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1231 	    wpa_s->conf->initial_freq_list && !params.freqs) {
1232 		wpa_dbg(wpa_s, MSG_DEBUG,
1233 			"Optimize scan based on conf->initial_freq_list");
1234 		int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1235 	} else if (wpa_s->conf->freq_list && !params.freqs) {
1236 		wpa_dbg(wpa_s, MSG_DEBUG,
1237 			"Optimize scan based on conf->freq_list");
1238 		int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1239 	}
1240 
1241 	/* Use current associated channel? */
1242 	if (wpa_s->conf->scan_cur_freq && !params.freqs) {
1243 		unsigned int num = wpa_s->num_multichan_concurrent;
1244 
1245 		params.freqs = os_calloc(num + 1, sizeof(int));
1246 		if (params.freqs) {
1247 			num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1248 			if (num > 0) {
1249 				wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1250 					"current operating channels since "
1251 					"scan_cur_freq is enabled");
1252 			} else {
1253 				os_free(params.freqs);
1254 				params.freqs = NULL;
1255 			}
1256 		}
1257 	}
1258 
1259 #ifdef CONFIG_MBO
1260 	if (wpa_s->enable_oce & OCE_STA)
1261 		params.oce_scan = 1;
1262 #endif /* CONFIG_MBO */
1263 
1264 	params.filter_ssids = wpa_supplicant_build_filter_ssids(
1265 		wpa_s->conf, &params.num_filter_ssids);
1266 	if (extra_ie) {
1267 		params.extra_ies = wpabuf_head(extra_ie);
1268 		params.extra_ies_len = wpabuf_len(extra_ie);
1269 	}
1270 
1271 #ifdef CONFIG_P2P
1272 	if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
1273 	    (wpa_s->show_group_started && wpa_s->go_params)) {
1274 		/*
1275 		 * The interface may not yet be in P2P mode, so we have to
1276 		 * explicitly request P2P probe to disable CCK rates.
1277 		 */
1278 		params.p2p_probe = 1;
1279 	}
1280 #endif /* CONFIG_P2P */
1281 
1282 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
1283 	    wpa_s->wpa_state <= WPA_SCANNING)
1284 		wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
1285 
1286 	if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1287 		struct wpa_bss *bss;
1288 
1289 		params.bssid = wpa_s->next_scan_bssid;
1290 		bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1291 		if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1292 		    bss && bss->ssid_len && params.num_ssids == 1 &&
1293 		    params.ssids[0].ssid_len == 0) {
1294 			params.ssids[0].ssid = bss->ssid;
1295 			params.ssids[0].ssid_len = bss->ssid_len;
1296 			wpa_dbg(wpa_s, MSG_DEBUG,
1297 				"Scan a previously specified BSSID " MACSTR
1298 				" and SSID %s",
1299 				MAC2STR(params.bssid),
1300 				wpa_ssid_txt(bss->ssid, bss->ssid_len));
1301 		} else {
1302 			wpa_dbg(wpa_s, MSG_DEBUG,
1303 				"Scan a previously specified BSSID " MACSTR,
1304 				MAC2STR(params.bssid));
1305 		}
1306 	}
1307 
1308 	scan_params = &params;
1309 
1310 scan:
1311 #ifdef CONFIG_P2P
1312 	/*
1313 	 * If the driver does not support multi-channel concurrency and a
1314 	 * virtual interface that shares the same radio with the wpa_s interface
1315 	 * is operating there may not be need to scan other channels apart from
1316 	 * the current operating channel on the other virtual interface. Filter
1317 	 * out other channels in case we are trying to find a connection for a
1318 	 * station interface when we are not configured to prefer station
1319 	 * connection and a concurrent operation is already in process.
1320 	 */
1321 	if (wpa_s->scan_for_connection &&
1322 	    wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1323 	    !scan_params->freqs && !params.freqs &&
1324 	    wpas_is_p2p_prioritized(wpa_s) &&
1325 	    wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1326 	    non_p2p_network_enabled(wpa_s)) {
1327 		unsigned int num = wpa_s->num_multichan_concurrent;
1328 
1329 		params.freqs = os_calloc(num + 1, sizeof(int));
1330 		if (params.freqs) {
1331 			num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1332 			if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1333 				wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1334 			} else {
1335 				os_free(params.freqs);
1336 				params.freqs = NULL;
1337 			}
1338 		}
1339 	}
1340 #endif /* CONFIG_P2P */
1341 
1342 	ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
1343 
1344 	if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1345 	    !wpa_s->manual_scan_freqs) {
1346 		/* Restore manual_scan_freqs for the next attempt */
1347 		wpa_s->manual_scan_freqs = params.freqs;
1348 		params.freqs = NULL;
1349 	}
1350 
1351 	wpabuf_free(extra_ie);
1352 	os_free(params.freqs);
1353 	os_free(params.filter_ssids);
1354 	os_free(params.mac_addr);
1355 
1356 	if (ret) {
1357 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1358 		if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1359 			wpa_supplicant_set_state(wpa_s,
1360 						 wpa_s->scan_prev_wpa_state);
1361 		/* Restore scan_req since we will try to scan again */
1362 		wpa_s->scan_req = wpa_s->last_scan_req;
1363 		wpa_supplicant_req_scan(wpa_s, 1, 0);
1364 	} else {
1365 		wpa_s->scan_for_connection = 0;
1366 #ifdef CONFIG_INTERWORKING
1367 		wpa_s->interworking_fast_assoc_tried = 0;
1368 #endif /* CONFIG_INTERWORKING */
1369 		wpa_s->next_scan_bssid_wildcard_ssid = 0;
1370 		if (params.bssid)
1371 			os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1372 	}
1373 }
1374 
1375 
1376 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1377 {
1378 	struct os_reltime remaining, new_int;
1379 	int cancelled;
1380 
1381 	cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1382 					     &remaining);
1383 
1384 	new_int.sec = sec;
1385 	new_int.usec = 0;
1386 	if (cancelled && os_reltime_before(&remaining, &new_int)) {
1387 		new_int.sec = remaining.sec;
1388 		new_int.usec = remaining.usec;
1389 	}
1390 
1391 	if (cancelled) {
1392 		eloop_register_timeout(new_int.sec, new_int.usec,
1393 				       wpa_supplicant_scan, wpa_s, NULL);
1394 	}
1395 	wpa_s->scan_interval = sec;
1396 }
1397 
1398 
1399 /**
1400  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1401  * @wpa_s: Pointer to wpa_supplicant data
1402  * @sec: Number of seconds after which to scan
1403  * @usec: Number of microseconds after which to scan
1404  *
1405  * This function is used to schedule a scan for neighboring access points after
1406  * the specified time.
1407  */
1408 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1409 {
1410 	int res;
1411 
1412 	if (wpa_s->p2p_mgmt) {
1413 		wpa_dbg(wpa_s, MSG_DEBUG,
1414 			"Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1415 			sec, usec);
1416 		return;
1417 	}
1418 
1419 	res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1420 				    NULL);
1421 	if (res == 1) {
1422 		wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1423 			sec, usec);
1424 	} else if (res == 0) {
1425 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1426 			sec, usec);
1427 	} else {
1428 		wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1429 			sec, usec);
1430 		eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1431 	}
1432 }
1433 
1434 
1435 /**
1436  * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1437  * @wpa_s: Pointer to wpa_supplicant data
1438  * @sec: Number of seconds after which to scan
1439  * @usec: Number of microseconds after which to scan
1440  * Returns: 0 on success or -1 otherwise
1441  *
1442  * This function is used to schedule periodic scans for neighboring
1443  * access points after the specified time.
1444  */
1445 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1446 				      int sec, int usec)
1447 {
1448 	if (!wpa_s->sched_scan_supported)
1449 		return -1;
1450 
1451 	eloop_register_timeout(sec, usec,
1452 			       wpa_supplicant_delayed_sched_scan_timeout,
1453 			       wpa_s, NULL);
1454 
1455 	return 0;
1456 }
1457 
1458 
1459 static void
1460 wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1461 				  struct wpa_driver_scan_params *params)
1462 {
1463 	if (wpa_s->wpa_state != WPA_COMPLETED ||
1464 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1465 	    wpa_s->srp.relative_rssi_set == 0)
1466 		return;
1467 
1468 	params->relative_rssi_set = 1;
1469 	params->relative_rssi = wpa_s->srp.relative_rssi;
1470 
1471 	if (wpa_s->srp.relative_adjust_rssi == 0)
1472 		return;
1473 
1474 	params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1475 	params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1476 }
1477 
1478 
1479 /**
1480  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1481  * @wpa_s: Pointer to wpa_supplicant data
1482  * Returns: 0 is sched_scan was started or -1 otherwise
1483  *
1484  * This function is used to schedule periodic scans for neighboring
1485  * access points repeating the scan continuously.
1486  */
1487 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1488 {
1489 	struct wpa_driver_scan_params params;
1490 	struct wpa_driver_scan_params *scan_params;
1491 	enum wpa_states prev_state;
1492 	struct wpa_ssid *ssid = NULL;
1493 	struct wpabuf *extra_ie = NULL;
1494 	int ret;
1495 	unsigned int max_sched_scan_ssids;
1496 	int wildcard = 0;
1497 	int need_ssids;
1498 	struct sched_scan_plan scan_plan;
1499 
1500 	if (!wpa_s->sched_scan_supported)
1501 		return -1;
1502 
1503 	if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1504 		max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1505 	else
1506 		max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1507 	if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1508 		return -1;
1509 
1510 	wpa_s->sched_scan_stop_req = 0;
1511 
1512 	if (wpa_s->sched_scanning) {
1513 		wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1514 		return 0;
1515 	}
1516 
1517 	need_ssids = 0;
1518 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1519 		if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1520 			/* Use wildcard SSID to find this network */
1521 			wildcard = 1;
1522 		} else if (!wpas_network_disabled(wpa_s, ssid) &&
1523 			   ssid->ssid_len)
1524 			need_ssids++;
1525 
1526 #ifdef CONFIG_WPS
1527 		if (!wpas_network_disabled(wpa_s, ssid) &&
1528 		    ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1529 			/*
1530 			 * Normal scan is more reliable and faster for WPS
1531 			 * operations and since these are for short periods of
1532 			 * time, the benefit of trying to use sched_scan would
1533 			 * be limited.
1534 			 */
1535 			wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1536 				"sched_scan for WPS");
1537 			return -1;
1538 		}
1539 #endif /* CONFIG_WPS */
1540 	}
1541 	if (wildcard)
1542 		need_ssids++;
1543 
1544 	if (wpa_s->normal_scans < 3 &&
1545 	    (need_ssids <= wpa_s->max_scan_ssids ||
1546 	     wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1547 		/*
1548 		 * When normal scan can speed up operations, use that for the
1549 		 * first operations before starting the sched_scan to allow
1550 		 * user space sleep more. We do this only if the normal scan
1551 		 * has functionality that is suitable for this or if the
1552 		 * sched_scan does not have better support for multiple SSIDs.
1553 		 */
1554 		wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1555 			"sched_scan for initial scans (normal_scans=%d)",
1556 			wpa_s->normal_scans);
1557 		return -1;
1558 	}
1559 
1560 	os_memset(&params, 0, sizeof(params));
1561 
1562 	/* If we can't allocate space for the filters, we just don't filter */
1563 	params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1564 					sizeof(struct wpa_driver_scan_filter));
1565 
1566 	prev_state = wpa_s->wpa_state;
1567 	if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1568 	    wpa_s->wpa_state == WPA_INACTIVE)
1569 		wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1570 
1571 	if (wpa_s->autoscan_params != NULL) {
1572 		scan_params = wpa_s->autoscan_params;
1573 		goto scan;
1574 	}
1575 
1576 	/* Find the starting point from which to continue scanning */
1577 	ssid = wpa_s->conf->ssid;
1578 	if (wpa_s->prev_sched_ssid) {
1579 		while (ssid) {
1580 			if (ssid == wpa_s->prev_sched_ssid) {
1581 				ssid = ssid->next;
1582 				break;
1583 			}
1584 			ssid = ssid->next;
1585 		}
1586 	}
1587 
1588 	if (!ssid || !wpa_s->prev_sched_ssid) {
1589 		wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1590 		wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1591 		wpa_s->first_sched_scan = 1;
1592 		ssid = wpa_s->conf->ssid;
1593 		wpa_s->prev_sched_ssid = ssid;
1594 	}
1595 
1596 	if (wildcard) {
1597 		wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1598 		params.num_ssids++;
1599 	}
1600 
1601 	while (ssid) {
1602 		if (wpas_network_disabled(wpa_s, ssid))
1603 			goto next;
1604 
1605 		if (params.num_filter_ssids < wpa_s->max_match_sets &&
1606 		    params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1607 			wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1608 				wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1609 			os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1610 				  ssid->ssid, ssid->ssid_len);
1611 			params.filter_ssids[params.num_filter_ssids].ssid_len =
1612 				ssid->ssid_len;
1613 			params.num_filter_ssids++;
1614 		} else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1615 		{
1616 			wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1617 				"filter for sched_scan - drop filter");
1618 			os_free(params.filter_ssids);
1619 			params.filter_ssids = NULL;
1620 			params.num_filter_ssids = 0;
1621 		}
1622 
1623 		if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1624 			if (params.num_ssids == max_sched_scan_ssids)
1625 				break; /* only room for broadcast SSID */
1626 			wpa_dbg(wpa_s, MSG_DEBUG,
1627 				"add to active scan ssid: %s",
1628 				wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1629 			params.ssids[params.num_ssids].ssid =
1630 				ssid->ssid;
1631 			params.ssids[params.num_ssids].ssid_len =
1632 				ssid->ssid_len;
1633 			params.num_ssids++;
1634 			if (params.num_ssids >= max_sched_scan_ssids) {
1635 				wpa_s->prev_sched_ssid = ssid;
1636 				do {
1637 					ssid = ssid->next;
1638 				} while (ssid &&
1639 					 (wpas_network_disabled(wpa_s, ssid) ||
1640 					  !ssid->scan_ssid));
1641 				break;
1642 			}
1643 		}
1644 
1645 	next:
1646 		wpa_s->prev_sched_ssid = ssid;
1647 		ssid = ssid->next;
1648 	}
1649 
1650 	if (params.num_filter_ssids == 0) {
1651 		os_free(params.filter_ssids);
1652 		params.filter_ssids = NULL;
1653 	}
1654 
1655 	extra_ie = wpa_supplicant_extra_ies(wpa_s);
1656 	if (extra_ie) {
1657 		params.extra_ies = wpabuf_head(extra_ie);
1658 		params.extra_ies_len = wpabuf_len(extra_ie);
1659 	}
1660 
1661 	if (wpa_s->conf->filter_rssi)
1662 		params.filter_rssi = wpa_s->conf->filter_rssi;
1663 
1664 	/* See if user specified frequencies. If so, scan only those. */
1665 	if (wpa_s->conf->freq_list && !params.freqs) {
1666 		wpa_dbg(wpa_s, MSG_DEBUG,
1667 			"Optimize scan based on conf->freq_list");
1668 		int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1669 	}
1670 
1671 #ifdef CONFIG_MBO
1672 	if (wpa_s->enable_oce & OCE_STA)
1673 		params.oce_scan = 1;
1674 #endif /* CONFIG_MBO */
1675 
1676 	scan_params = &params;
1677 
1678 scan:
1679 	wpa_s->sched_scan_timed_out = 0;
1680 
1681 	/*
1682 	 * We cannot support multiple scan plans if the scan request includes
1683 	 * too many SSID's, so in this case use only the last scan plan and make
1684 	 * it run infinitely. It will be stopped by the timeout.
1685 	 */
1686 	if (wpa_s->sched_scan_plans_num == 1 ||
1687 	    (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1688 		params.sched_scan_plans = wpa_s->sched_scan_plans;
1689 		params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1690 	} else if (wpa_s->sched_scan_plans_num > 1) {
1691 		wpa_dbg(wpa_s, MSG_DEBUG,
1692 			"Too many SSIDs. Default to using single scheduled_scan plan");
1693 		params.sched_scan_plans =
1694 			&wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1695 						 1];
1696 		params.sched_scan_plans_num = 1;
1697 	} else {
1698 		if (wpa_s->conf->sched_scan_interval)
1699 			scan_plan.interval = wpa_s->conf->sched_scan_interval;
1700 		else
1701 			scan_plan.interval = 10;
1702 
1703 		if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1704 			wpa_printf(MSG_WARNING,
1705 				   "Scan interval too long(%u), use the maximum allowed(%u)",
1706 				   scan_plan.interval,
1707 				   wpa_s->max_sched_scan_plan_interval);
1708 			scan_plan.interval =
1709 				wpa_s->max_sched_scan_plan_interval;
1710 		}
1711 
1712 		scan_plan.iterations = 0;
1713 		params.sched_scan_plans = &scan_plan;
1714 		params.sched_scan_plans_num = 1;
1715 	}
1716 
1717 	params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1718 
1719 	if (ssid || !wpa_s->first_sched_scan) {
1720 		wpa_dbg(wpa_s, MSG_DEBUG,
1721 			"Starting sched scan after %u seconds: interval %u timeout %d",
1722 			params.sched_scan_start_delay,
1723 			params.sched_scan_plans[0].interval,
1724 			wpa_s->sched_scan_timeout);
1725 	} else {
1726 		wpa_dbg(wpa_s, MSG_DEBUG,
1727 			"Starting sched scan after %u seconds (no timeout)",
1728 			params.sched_scan_start_delay);
1729 	}
1730 
1731 	wpa_setband_scan_freqs(wpa_s, scan_params);
1732 
1733 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
1734 	    wpa_s->wpa_state <= WPA_SCANNING)
1735 		wpa_setup_mac_addr_rand_params(&params,
1736 					       wpa_s->mac_addr_sched_scan);
1737 
1738 	wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1739 
1740 	ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1741 	wpabuf_free(extra_ie);
1742 	os_free(params.filter_ssids);
1743 	os_free(params.mac_addr);
1744 	if (ret) {
1745 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1746 		if (prev_state != wpa_s->wpa_state)
1747 			wpa_supplicant_set_state(wpa_s, prev_state);
1748 		return ret;
1749 	}
1750 
1751 	/* If we have more SSIDs to scan, add a timeout so we scan them too */
1752 	if (ssid || !wpa_s->first_sched_scan) {
1753 		wpa_s->sched_scan_timed_out = 0;
1754 		eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1755 				       wpa_supplicant_sched_scan_timeout,
1756 				       wpa_s, NULL);
1757 		wpa_s->first_sched_scan = 0;
1758 		wpa_s->sched_scan_timeout /= 2;
1759 		params.sched_scan_plans[0].interval *= 2;
1760 		if ((unsigned int) wpa_s->sched_scan_timeout <
1761 		    params.sched_scan_plans[0].interval ||
1762 		    params.sched_scan_plans[0].interval >
1763 		    wpa_s->max_sched_scan_plan_interval) {
1764 			params.sched_scan_plans[0].interval = 10;
1765 			wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1766 		}
1767 	}
1768 
1769 	/* If there is no more ssids, start next time from the beginning */
1770 	if (!ssid)
1771 		wpa_s->prev_sched_ssid = NULL;
1772 
1773 	return 0;
1774 }
1775 
1776 
1777 /**
1778  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1779  * @wpa_s: Pointer to wpa_supplicant data
1780  *
1781  * This function is used to cancel a scan request scheduled with
1782  * wpa_supplicant_req_scan().
1783  */
1784 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1785 {
1786 	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1787 	eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1788 }
1789 
1790 
1791 /**
1792  * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1793  * @wpa_s: Pointer to wpa_supplicant data
1794  *
1795  * This function is used to stop a delayed scheduled scan.
1796  */
1797 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1798 {
1799 	if (!wpa_s->sched_scan_supported)
1800 		return;
1801 
1802 	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1803 	eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1804 			     wpa_s, NULL);
1805 }
1806 
1807 
1808 /**
1809  * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1810  * @wpa_s: Pointer to wpa_supplicant data
1811  *
1812  * This function is used to stop a periodic scheduled scan.
1813  */
1814 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1815 {
1816 	if (!wpa_s->sched_scanning)
1817 		return;
1818 
1819 	if (wpa_s->sched_scanning)
1820 		wpa_s->sched_scan_stop_req = 1;
1821 
1822 	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1823 	eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1824 	wpa_supplicant_stop_sched_scan(wpa_s);
1825 }
1826 
1827 
1828 /**
1829  * wpa_supplicant_notify_scanning - Indicate possible scan state change
1830  * @wpa_s: Pointer to wpa_supplicant data
1831  * @scanning: Whether scanning is currently in progress
1832  *
1833  * This function is to generate scanning notifycations. It is called whenever
1834  * there may have been a change in scanning (scan started, completed, stopped).
1835  * wpas_notify_scanning() is called whenever the scanning state changed from the
1836  * previously notified state.
1837  */
1838 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1839 				    int scanning)
1840 {
1841 	if (wpa_s->scanning != scanning) {
1842 		wpa_s->scanning = scanning;
1843 		wpas_notify_scanning(wpa_s);
1844 	}
1845 }
1846 
1847 
1848 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1849 {
1850 	int rate = 0;
1851 	const u8 *ie;
1852 	int i;
1853 
1854 	ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1855 	for (i = 0; ie && i < ie[1]; i++) {
1856 		if ((ie[i + 2] & 0x7f) > rate)
1857 			rate = ie[i + 2] & 0x7f;
1858 	}
1859 
1860 	ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1861 	for (i = 0; ie && i < ie[1]; i++) {
1862 		if ((ie[i + 2] & 0x7f) > rate)
1863 			rate = ie[i + 2] & 0x7f;
1864 	}
1865 
1866 	return rate;
1867 }
1868 
1869 
1870 /**
1871  * wpa_scan_get_ie - Fetch a specified information element from a scan result
1872  * @res: Scan result entry
1873  * @ie: Information element identitifier (WLAN_EID_*)
1874  * Returns: Pointer to the information element (id field) or %NULL if not found
1875  *
1876  * This function returns the first matching information element in the scan
1877  * result.
1878  */
1879 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1880 {
1881 	size_t ie_len = res->ie_len;
1882 
1883 	/* Use the Beacon frame IEs if res->ie_len is not available */
1884 	if (!ie_len)
1885 		ie_len = res->beacon_ie_len;
1886 
1887 	return get_ie((const u8 *) (res + 1), ie_len, ie);
1888 }
1889 
1890 
1891 /**
1892  * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1893  * @res: Scan result entry
1894  * @vendor_type: Vendor type (four octets starting the IE payload)
1895  * Returns: Pointer to the information element (id field) or %NULL if not found
1896  *
1897  * This function returns the first matching information element in the scan
1898  * result.
1899  */
1900 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1901 				  u32 vendor_type)
1902 {
1903 	const u8 *ies;
1904 	const struct element *elem;
1905 
1906 	ies = (const u8 *) (res + 1);
1907 
1908 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
1909 		if (elem->datalen >= 4 &&
1910 		    vendor_type == WPA_GET_BE32(elem->data))
1911 			return &elem->id;
1912 	}
1913 
1914 	return NULL;
1915 }
1916 
1917 
1918 /**
1919  * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1920  * @res: Scan result entry
1921  * @vendor_type: Vendor type (four octets starting the IE payload)
1922  * Returns: Pointer to the information element (id field) or %NULL if not found
1923  *
1924  * This function returns the first matching information element in the scan
1925  * result.
1926  *
1927  * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1928  * from Beacon frames instead of either Beacon or Probe Response frames.
1929  */
1930 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1931 					 u32 vendor_type)
1932 {
1933 	const u8 *ies;
1934 	const struct element *elem;
1935 
1936 	if (res->beacon_ie_len == 0)
1937 		return NULL;
1938 
1939 	ies = (const u8 *) (res + 1);
1940 	ies += res->ie_len;
1941 
1942 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1943 			    res->beacon_ie_len) {
1944 		if (elem->datalen >= 4 &&
1945 		    vendor_type == WPA_GET_BE32(elem->data))
1946 			return &elem->id;
1947 	}
1948 
1949 	return NULL;
1950 }
1951 
1952 
1953 /**
1954  * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1955  * @res: Scan result entry
1956  * @vendor_type: Vendor type (four octets starting the IE payload)
1957  * Returns: Pointer to the information element payload or %NULL if not found
1958  *
1959  * This function returns concatenated payload of possibly fragmented vendor
1960  * specific information elements in the scan result. The caller is responsible
1961  * for freeing the returned buffer.
1962  */
1963 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1964 					     u32 vendor_type)
1965 {
1966 	struct wpabuf *buf;
1967 	const u8 *end, *pos;
1968 
1969 	buf = wpabuf_alloc(res->ie_len);
1970 	if (buf == NULL)
1971 		return NULL;
1972 
1973 	pos = (const u8 *) (res + 1);
1974 	end = pos + res->ie_len;
1975 
1976 	while (end - pos > 1) {
1977 		u8 ie, len;
1978 
1979 		ie = pos[0];
1980 		len = pos[1];
1981 		if (len > end - pos - 2)
1982 			break;
1983 		pos += 2;
1984 		if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1985 		    vendor_type == WPA_GET_BE32(pos))
1986 			wpabuf_put_data(buf, pos + 4, len - 4);
1987 		pos += len;
1988 	}
1989 
1990 	if (wpabuf_len(buf) == 0) {
1991 		wpabuf_free(buf);
1992 		buf = NULL;
1993 	}
1994 
1995 	return buf;
1996 }
1997 
1998 
1999 /* Compare function for sorting scan results. Return >0 if @b is considered
2000  * better. */
2001 static int wpa_scan_result_compar(const void *a, const void *b)
2002 {
2003 #define MIN(a,b) a < b ? a : b
2004 	struct wpa_scan_res **_wa = (void *) a;
2005 	struct wpa_scan_res **_wb = (void *) b;
2006 	struct wpa_scan_res *wa = *_wa;
2007 	struct wpa_scan_res *wb = *_wb;
2008 	int wpa_a, wpa_b;
2009 	int snr_a, snr_b, snr_a_full, snr_b_full;
2010 
2011 	/* WPA/WPA2 support preferred */
2012 	wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2013 		wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2014 	wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2015 		wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2016 
2017 	if (wpa_b && !wpa_a)
2018 		return 1;
2019 	if (!wpa_b && wpa_a)
2020 		return -1;
2021 
2022 	/* privacy support preferred */
2023 	if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2024 	    (wb->caps & IEEE80211_CAP_PRIVACY))
2025 		return 1;
2026 	if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2027 	    (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2028 		return -1;
2029 
2030 	if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
2031 		snr_a_full = wa->snr;
2032 		snr_a = MIN(wa->snr, GREAT_SNR);
2033 		snr_b_full = wb->snr;
2034 		snr_b = MIN(wb->snr, GREAT_SNR);
2035 	} else {
2036 		/* Level is not in dBm, so we can't calculate
2037 		 * SNR. Just use raw level (units unknown). */
2038 		snr_a = snr_a_full = wa->level;
2039 		snr_b = snr_b_full = wb->level;
2040 	}
2041 
2042 	/* If SNR is close, decide by max rate or frequency band. For cases
2043 	 * involving the 6 GHz band, use the throughput estimate irrespective
2044 	 * of the SNR difference since the LPI/VLP rules may result in
2045 	 * significant differences in SNR for cases where the estimated
2046 	 * throughput can be considerably higher with the lower SNR. */
2047 	if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2048 			       is_6ghz_freq(wa->freq) ||
2049 			       is_6ghz_freq(wb->freq))) {
2050 		if (wa->est_throughput != wb->est_throughput)
2051 			return (int) wb->est_throughput -
2052 				(int) wa->est_throughput;
2053 	}
2054 	if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2055 	    (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
2056 		if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2057 			return is_6ghz_freq(wa->freq) ? -1 : 1;
2058 		if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2059 			return IS_5GHZ(wa->freq) ? -1 : 1;
2060 	}
2061 
2062 	/* all things being equal, use SNR; if SNRs are
2063 	 * identical, use quality values since some drivers may only report
2064 	 * that value and leave the signal level zero */
2065 	if (snr_b_full == snr_a_full)
2066 		return wb->qual - wa->qual;
2067 	return snr_b_full - snr_a_full;
2068 #undef MIN
2069 }
2070 
2071 
2072 #ifdef CONFIG_WPS
2073 /* Compare function for sorting scan results when searching a WPS AP for
2074  * provisioning. Return >0 if @b is considered better. */
2075 static int wpa_scan_result_wps_compar(const void *a, const void *b)
2076 {
2077 	struct wpa_scan_res **_wa = (void *) a;
2078 	struct wpa_scan_res **_wb = (void *) b;
2079 	struct wpa_scan_res *wa = *_wa;
2080 	struct wpa_scan_res *wb = *_wb;
2081 	int uses_wps_a, uses_wps_b;
2082 	struct wpabuf *wps_a, *wps_b;
2083 	int res;
2084 
2085 	/* Optimization - check WPS IE existence before allocated memory and
2086 	 * doing full reassembly. */
2087 	uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2088 	uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2089 	if (uses_wps_a && !uses_wps_b)
2090 		return -1;
2091 	if (!uses_wps_a && uses_wps_b)
2092 		return 1;
2093 
2094 	if (uses_wps_a && uses_wps_b) {
2095 		wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2096 		wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2097 		res = wps_ap_priority_compar(wps_a, wps_b);
2098 		wpabuf_free(wps_a);
2099 		wpabuf_free(wps_b);
2100 		if (res)
2101 			return res;
2102 	}
2103 
2104 	/*
2105 	 * Do not use current AP security policy as a sorting criteria during
2106 	 * WPS provisioning step since the AP may get reconfigured at the
2107 	 * completion of provisioning.
2108 	 */
2109 
2110 	/* all things being equal, use signal level; if signal levels are
2111 	 * identical, use quality values since some drivers may only report
2112 	 * that value and leave the signal level zero */
2113 	if (wb->level == wa->level)
2114 		return wb->qual - wa->qual;
2115 	return wb->level - wa->level;
2116 }
2117 #endif /* CONFIG_WPS */
2118 
2119 
2120 static void dump_scan_res(struct wpa_scan_results *scan_res)
2121 {
2122 #ifndef CONFIG_NO_STDOUT_DEBUG
2123 	size_t i;
2124 
2125 	if (scan_res->res == NULL || scan_res->num == 0)
2126 		return;
2127 
2128 	wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2129 
2130 	for (i = 0; i < scan_res->num; i++) {
2131 		struct wpa_scan_res *r = scan_res->res[i];
2132 		u8 *pos;
2133 		if (r->flags & WPA_SCAN_LEVEL_DBM) {
2134 			int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2135 
2136 			wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
2137 				   "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2138 				   MAC2STR(r->bssid), r->freq, r->qual,
2139 				   r->noise, noise_valid ? "" : "~", r->level,
2140 				   r->snr, r->snr >= GREAT_SNR ? "*" : "",
2141 				   r->flags,
2142 				   r->age, r->est_throughput);
2143 		} else {
2144 			wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
2145 				   "noise=%d level=%d flags=0x%x age=%u est=%u",
2146 				   MAC2STR(r->bssid), r->freq, r->qual,
2147 				   r->noise, r->level, r->flags, r->age,
2148 				   r->est_throughput);
2149 		}
2150 		pos = (u8 *) (r + 1);
2151 		if (r->ie_len)
2152 			wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2153 		pos += r->ie_len;
2154 		if (r->beacon_ie_len)
2155 			wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2156 				    pos, r->beacon_ie_len);
2157 	}
2158 #endif /* CONFIG_NO_STDOUT_DEBUG */
2159 }
2160 
2161 
2162 /**
2163  * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2164  * @wpa_s: Pointer to wpa_supplicant data
2165  * @bssid: BSSID to check
2166  * Returns: 0 if the BSSID is filtered or 1 if not
2167  *
2168  * This function is used to filter out specific BSSIDs from scan reslts mainly
2169  * for testing purposes (SET bssid_filter ctrl_iface command).
2170  */
2171 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2172 				      const u8 *bssid)
2173 {
2174 	size_t i;
2175 
2176 	if (wpa_s->bssid_filter == NULL)
2177 		return 1;
2178 
2179 	for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2180 		if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2181 			      ETH_ALEN) == 0)
2182 			return 1;
2183 	}
2184 
2185 	return 0;
2186 }
2187 
2188 
2189 void filter_scan_res(struct wpa_supplicant *wpa_s,
2190 		     struct wpa_scan_results *res)
2191 {
2192 	size_t i, j;
2193 
2194 	if (wpa_s->bssid_filter == NULL)
2195 		return;
2196 
2197 	for (i = 0, j = 0; i < res->num; i++) {
2198 		if (wpa_supplicant_filter_bssid_match(wpa_s,
2199 						      res->res[i]->bssid)) {
2200 			res->res[j++] = res->res[i];
2201 		} else {
2202 			os_free(res->res[i]);
2203 			res->res[i] = NULL;
2204 		}
2205 	}
2206 
2207 	if (res->num != j) {
2208 		wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2209 			   (int) (res->num - j));
2210 		res->num = j;
2211 	}
2212 }
2213 
2214 
2215 void scan_snr(struct wpa_scan_res *res)
2216 {
2217 	if (res->flags & WPA_SCAN_NOISE_INVALID) {
2218 		res->noise = is_6ghz_freq(res->freq) ?
2219 			DEFAULT_NOISE_FLOOR_6GHZ :
2220 			(IS_5GHZ(res->freq) ?
2221 			 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
2222 	}
2223 
2224 	if (res->flags & WPA_SCAN_LEVEL_DBM) {
2225 		res->snr = res->level - res->noise;
2226 	} else {
2227 		/* Level is not in dBm, so we can't calculate
2228 		 * SNR. Just use raw level (units unknown). */
2229 		res->snr = res->level;
2230 	}
2231 }
2232 
2233 
2234 /* Minimum SNR required to achieve a certain bitrate. */
2235 struct minsnr_bitrate_entry {
2236 	int minsnr;
2237 	unsigned int bitrate; /* in Mbps */
2238 };
2239 
2240 /* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2241 static const int vht_mcs = 8;
2242 
2243 static const struct minsnr_bitrate_entry vht20_table[] = {
2244 	{ 0, 0 },
2245 	{ 2, 6500 },   /* HT20 MCS0 */
2246 	{ 5, 13000 },  /* HT20 MCS1 */
2247 	{ 9, 19500 },  /* HT20 MCS2 */
2248 	{ 11, 26000 }, /* HT20 MCS3 */
2249 	{ 15, 39000 }, /* HT20 MCS4 */
2250 	{ 18, 52000 }, /* HT20 MCS5 */
2251 	{ 20, 58500 }, /* HT20 MCS6 */
2252 	{ 25, 65000 }, /* HT20 MCS7 */
2253 	{ 29, 78000 }, /* VHT20 MCS8 */
2254 	{ -1, 78000 }  /* SNR > 29 */
2255 };
2256 
2257 static const struct minsnr_bitrate_entry vht40_table[] = {
2258 	{ 0, 0 },
2259 	{ 5, 13500 },   /* HT40 MCS0 */
2260 	{ 8, 27000 },   /* HT40 MCS1 */
2261 	{ 12, 40500 },  /* HT40 MCS2 */
2262 	{ 14, 54000 },  /* HT40 MCS3 */
2263 	{ 18, 81000 },  /* HT40 MCS4 */
2264 	{ 21, 108000 }, /* HT40 MCS5 */
2265 	{ 23, 121500 }, /* HT40 MCS6 */
2266 	{ 28, 135000 }, /* HT40 MCS7 */
2267 	{ 32, 162000 }, /* VHT40 MCS8 */
2268 	{ 34, 180000 }, /* VHT40 MCS9 */
2269 	{ -1, 180000 }  /* SNR > 34 */
2270 };
2271 
2272 static const struct minsnr_bitrate_entry vht80_table[] = {
2273 	{ 0, 0 },
2274 	{ 8, 29300 },   /* VHT80 MCS0 */
2275 	{ 11, 58500 },  /* VHT80 MCS1 */
2276 	{ 15, 87800 },  /* VHT80 MCS2 */
2277 	{ 17, 117000 }, /* VHT80 MCS3 */
2278 	{ 21, 175500 }, /* VHT80 MCS4 */
2279 	{ 24, 234000 }, /* VHT80 MCS5 */
2280 	{ 26, 263300 }, /* VHT80 MCS6 */
2281 	{ 31, 292500 }, /* VHT80 MCS7 */
2282 	{ 35, 351000 }, /* VHT80 MCS8 */
2283 	{ 37, 390000 }, /* VHT80 MCS9 */
2284 	{ -1, 390000 }  /* SNR > 37 */
2285 };
2286 
2287 
2288 static const struct minsnr_bitrate_entry vht160_table[] = {
2289 	{ 0, 0 },
2290 	{ 11, 58500 },  /* VHT160 MCS0 */
2291 	{ 14, 117000 }, /* VHT160 MCS1 */
2292 	{ 18, 175500 }, /* VHT160 MCS2 */
2293 	{ 20, 234000 }, /* VHT160 MCS3 */
2294 	{ 24, 351000 }, /* VHT160 MCS4 */
2295 	{ 27, 468000 }, /* VHT160 MCS5 */
2296 	{ 29, 526500 }, /* VHT160 MCS6 */
2297 	{ 34, 585000 }, /* VHT160 MCS7 */
2298 	{ 38, 702000 }, /* VHT160 MCS8 */
2299 	{ 40, 780000 }, /* VHT160 MCS9 */
2300 	{ -1, 780000 }  /* SNR > 37 */
2301 };
2302 
2303 
2304 static const struct minsnr_bitrate_entry he20_table[] = {
2305 	{ 0, 0 },
2306 	{ 2, 8600 },    /* HE20 MCS0 */
2307 	{ 5, 17200 },   /* HE20 MCS1 */
2308 	{ 9, 25800 },   /* HE20 MCS2 */
2309 	{ 11, 34400 },  /* HE20 MCS3 */
2310 	{ 15, 51600 },  /* HE20 MCS4 */
2311 	{ 18, 68800 },  /* HE20 MCS5 */
2312 	{ 20, 77400 },  /* HE20 MCS6 */
2313 	{ 25, 86000 },  /* HE20 MCS7 */
2314 	{ 29, 103200 }, /* HE20 MCS8 */
2315 	{ 31, 114700 }, /* HE20 MCS9 */
2316 	{ 34, 129000 }, /* HE20 MCS10 */
2317 	{ 36, 143400 }, /* HE20 MCS11 */
2318 	{ -1, 143400 }  /* SNR > 29 */
2319 };
2320 
2321 static const struct minsnr_bitrate_entry he40_table[] = {
2322 	{ 0, 0 },
2323 	{ 5, 17200 },   /* HE40 MCS0 */
2324 	{ 8, 34400 },   /* HE40 MCS1 */
2325 	{ 12, 51600 },  /* HE40 MCS2 */
2326 	{ 14, 68800 },  /* HE40 MCS3 */
2327 	{ 18, 103200 }, /* HE40 MCS4 */
2328 	{ 21, 137600 }, /* HE40 MCS5 */
2329 	{ 23, 154900 }, /* HE40 MCS6 */
2330 	{ 28, 172100 }, /* HE40 MCS7 */
2331 	{ 32, 206500 }, /* HE40 MCS8 */
2332 	{ 34, 229400 }, /* HE40 MCS9 */
2333 	{ 37, 258100 }, /* HE40 MCS10 */
2334 	{ 39, 286800 }, /* HE40 MCS11 */
2335 	{ -1, 286800 }  /* SNR > 34 */
2336 };
2337 
2338 static const struct minsnr_bitrate_entry he80_table[] = {
2339 	{ 0, 0 },
2340 	{ 8, 36000 },   /* HE80 MCS0 */
2341 	{ 11, 72100 },  /* HE80 MCS1 */
2342 	{ 15, 108100 }, /* HE80 MCS2 */
2343 	{ 17, 144100 }, /* HE80 MCS3 */
2344 	{ 21, 216200 }, /* HE80 MCS4 */
2345 	{ 24, 288200 }, /* HE80 MCS5 */
2346 	{ 26, 324300 }, /* HE80 MCS6 */
2347 	{ 31, 360300 }, /* HE80 MCS7 */
2348 	{ 35, 432400 }, /* HE80 MCS8 */
2349 	{ 37, 480400 }, /* HE80 MCS9 */
2350 	{ 40, 540400 }, /* HE80 MCS10 */
2351 	{ 42, 600500 }, /* HE80 MCS11 */
2352 	{ -1, 600500 }  /* SNR > 37 */
2353 };
2354 
2355 
2356 static const struct minsnr_bitrate_entry he160_table[] = {
2357 	{ 0, 0 },
2358 	{ 11, 72100 },   /* HE160 MCS0 */
2359 	{ 14, 144100 },  /* HE160 MCS1 */
2360 	{ 18, 216200 },  /* HE160 MCS2 */
2361 	{ 20, 288200 },  /* HE160 MCS3 */
2362 	{ 24, 432400 },  /* HE160 MCS4 */
2363 	{ 27, 576500 },  /* HE160 MCS5 */
2364 	{ 29, 648500 },  /* HE160 MCS6 */
2365 	{ 34, 720600 },  /* HE160 MCS7 */
2366 	{ 38, 864700 },  /* HE160 MCS8 */
2367 	{ 40, 960800 },  /* HE160 MCS9 */
2368 	{ 43, 1080900 }, /* HE160 MCS10 */
2369 	{ 45, 1201000 }, /* HE160 MCS11 */
2370 	{ -1, 1201000 }  /* SNR > 37 */
2371 };
2372 
2373 
2374 static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2375 				     int rate0, int rate1)
2376 {
2377 	return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
2378 }
2379 
2380 
2381 static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2382 			     int snr, bool vht)
2383 {
2384 	const struct minsnr_bitrate_entry *prev, *entry = table;
2385 
2386 	while ((entry->minsnr != -1) &&
2387 	       (snr >= entry->minsnr) &&
2388 	       (vht || entry - table <= vht_mcs))
2389 		entry++;
2390 	if (entry == table)
2391 		return entry->bitrate;
2392 	prev = entry - 1;
2393 	if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2394 		return prev->bitrate;
2395 	return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2396 				entry->bitrate);
2397 }
2398 
2399 
2400 static unsigned int max_ht20_rate(int snr, bool vht)
2401 {
2402 	return max_rate(vht20_table, snr, vht);
2403 }
2404 
2405 
2406 static unsigned int max_ht40_rate(int snr, bool vht)
2407 {
2408 	return max_rate(vht40_table, snr, vht);
2409 }
2410 
2411 
2412 static unsigned int max_vht80_rate(int snr)
2413 {
2414 	return max_rate(vht80_table, snr, 1);
2415 }
2416 
2417 
2418 static unsigned int max_vht160_rate(int snr)
2419 {
2420 	return max_rate(vht160_table, snr, 1);
2421 }
2422 
2423 
2424 static unsigned int max_he_rate(const struct minsnr_bitrate_entry table[],
2425 				int snr)
2426 {
2427 	const struct minsnr_bitrate_entry *prev, *entry = table;
2428 
2429 	while (entry->minsnr != -1 && snr >= entry->minsnr)
2430 		entry++;
2431 	if (entry == table)
2432 		return 0;
2433 	prev = entry - 1;
2434 	if (entry->minsnr == -1)
2435 		return prev->bitrate;
2436 	return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2437 				prev->bitrate, entry->bitrate);
2438 }
2439 
2440 
2441 unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2442 			      const u8 *ies, size_t ies_len, int rate,
2443 			      int snr, int freq)
2444 {
2445 	struct hostapd_hw_modes *hw_mode;
2446 	unsigned int est, tmp;
2447 	const u8 *ie;
2448 
2449 	/* Limit based on estimated SNR */
2450 	if (rate > 1 * 2 && snr < 1)
2451 		rate = 1 * 2;
2452 	else if (rate > 2 * 2 && snr < 4)
2453 		rate = 2 * 2;
2454 	else if (rate > 6 * 2 && snr < 5)
2455 		rate = 6 * 2;
2456 	else if (rate > 9 * 2 && snr < 6)
2457 		rate = 9 * 2;
2458 	else if (rate > 12 * 2 && snr < 7)
2459 		rate = 12 * 2;
2460 	else if (rate > 12 * 2 && snr < 8)
2461 		rate = 14 * 2;
2462 	else if (rate > 12 * 2 && snr < 9)
2463 		rate = 16 * 2;
2464 	else if (rate > 18 * 2 && snr < 10)
2465 		rate = 18 * 2;
2466 	else if (rate > 24 * 2 && snr < 11)
2467 		rate = 24 * 2;
2468 	else if (rate > 24 * 2 && snr < 12)
2469 		rate = 27 * 2;
2470 	else if (rate > 24 * 2 && snr < 13)
2471 		rate = 30 * 2;
2472 	else if (rate > 24 * 2 && snr < 14)
2473 		rate = 33 * 2;
2474 	else if (rate > 36 * 2 && snr < 15)
2475 		rate = 36 * 2;
2476 	else if (rate > 36 * 2 && snr < 16)
2477 		rate = 39 * 2;
2478 	else if (rate > 36 * 2 && snr < 17)
2479 		rate = 42 * 2;
2480 	else if (rate > 36 * 2 && snr < 18)
2481 		rate = 45 * 2;
2482 	else if (rate > 48 * 2 && snr < 19)
2483 		rate = 48 * 2;
2484 	else if (rate > 48 * 2 && snr < 20)
2485 		rate = 51 * 2;
2486 	else if (rate > 54 * 2 && snr < 21)
2487 		rate = 54 * 2;
2488 	est = rate * 500;
2489 
2490 	hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2491 				     freq);
2492 
2493 	if (hw_mode && hw_mode->ht_capab) {
2494 		ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
2495 		if (ie) {
2496 			tmp = max_ht20_rate(snr, false);
2497 			if (tmp > est)
2498 				est = tmp;
2499 		}
2500 	}
2501 
2502 	if (hw_mode &&
2503 	    (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
2504 		ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
2505 		if (ie && ie[1] >= 2 &&
2506 		    (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2507 			tmp = max_ht40_rate(snr, false);
2508 			if (tmp > est)
2509 				est = tmp;
2510 		}
2511 	}
2512 
2513 	if (hw_mode && hw_mode->vht_capab) {
2514 		/* Use +1 to assume VHT is always faster than HT */
2515 		ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
2516 		if (ie) {
2517 			bool vht80 = false, vht160 = false;
2518 
2519 			tmp = max_ht20_rate(snr, true) + 1;
2520 			if (tmp > est)
2521 				est = tmp;
2522 
2523 			ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
2524 			if (ie && ie[1] >= 2 &&
2525 			    (ie[3] &
2526 			     HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2527 				tmp = max_ht40_rate(snr, true) + 1;
2528 				if (tmp > est)
2529 					est = tmp;
2530 			}
2531 
2532 			/* Determine VHT BSS bandwidth based on IEEE Std
2533 			 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
2534 			ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
2535 			if (ie && ie[1] >= 3) {
2536 				u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2537 				u8 seg0 = ie[3];
2538 				u8 seg1 = ie[4];
2539 
2540 				if (cw)
2541 					vht80 = true;
2542 				if (cw == 2 ||
2543 				    (cw == 3 &&
2544 				     (seg1 > 0 && abs(seg1 - seg0) == 16)))
2545 					vht160 = true;
2546 				if (cw == 1 &&
2547 				    ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2548 				     (seg1 > 0 && abs(seg1 - seg0) == 16)))
2549 					vht160 = true;
2550 			}
2551 
2552 			if (vht80) {
2553 				tmp = max_vht80_rate(snr) + 1;
2554 				if (tmp > est)
2555 					est = tmp;
2556 			}
2557 
2558 			if (vht160 &&
2559 			    (hw_mode->vht_capab &
2560 			     (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2561 			      VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
2562 				tmp = max_vht160_rate(snr) + 1;
2563 				if (tmp > est)
2564 					est = tmp;
2565 			}
2566 		}
2567 	}
2568 
2569 	if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
2570 		/* Use +2 to assume HE is always faster than HT/VHT */
2571 		struct ieee80211_he_capabilities *he;
2572 		struct he_capabilities *own_he;
2573 		u8 cw;
2574 
2575 		ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
2576 		if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
2577 			return est;
2578 		he = (struct ieee80211_he_capabilities *) &ie[3];
2579 		own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
2580 
2581 		tmp = max_he_rate(he20_table, snr) + 2;
2582 		if (tmp > est)
2583 			est = tmp;
2584 
2585 		cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2586 			own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
2587 		if (cw &
2588 		    (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
2589 		     HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2590 			tmp = max_he_rate(he40_table, snr) + 2;
2591 			if (tmp > est)
2592 				est = tmp;
2593 		}
2594 
2595 		if (!IS_2P4GHZ(freq) &&
2596 		    (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2597 			tmp = max_he_rate(he80_table, snr) + 2;
2598 			if (tmp > est)
2599 				est = tmp;
2600 		}
2601 
2602 		if (!IS_2P4GHZ(freq) &&
2603 		    (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2604 			   HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
2605 			tmp = max_he_rate(he160_table, snr) + 2;
2606 			if (tmp > est)
2607 				est = tmp;
2608 		}
2609 	}
2610 
2611 	return est;
2612 }
2613 
2614 
2615 void scan_est_throughput(struct wpa_supplicant *wpa_s,
2616 			 struct wpa_scan_res *res)
2617 {
2618 	int rate; /* max legacy rate in 500 kb/s units */
2619 	int snr = res->snr;
2620 	const u8 *ies = (const void *) (res + 1);
2621 	size_t ie_len = res->ie_len;
2622 
2623 	if (res->est_throughput)
2624 		return;
2625 
2626 	/* Get maximum legacy rate */
2627 	rate = wpa_scan_get_max_rate(res);
2628 
2629 	if (!ie_len)
2630 		ie_len = res->beacon_ie_len;
2631 	res->est_throughput =
2632 		wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, res->freq);
2633 
2634 	/* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2635 }
2636 
2637 
2638 /**
2639  * wpa_supplicant_get_scan_results - Get scan results
2640  * @wpa_s: Pointer to wpa_supplicant data
2641  * @info: Information about what was scanned or %NULL if not available
2642  * @new_scan: Whether a new scan was performed
2643  * Returns: Scan results, %NULL on failure
2644  *
2645  * This function request the current scan results from the driver and updates
2646  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2647  * results with wpa_scan_results_free().
2648  */
2649 struct wpa_scan_results *
2650 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2651 				struct scan_info *info, int new_scan)
2652 {
2653 	struct wpa_scan_results *scan_res;
2654 	size_t i;
2655 	int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2656 
2657 	scan_res = wpa_drv_get_scan_results2(wpa_s);
2658 	if (scan_res == NULL) {
2659 		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2660 		return NULL;
2661 	}
2662 	if (scan_res->fetch_time.sec == 0) {
2663 		/*
2664 		 * Make sure we have a valid timestamp if the driver wrapper
2665 		 * does not set this.
2666 		 */
2667 		os_get_reltime(&scan_res->fetch_time);
2668 	}
2669 	filter_scan_res(wpa_s, scan_res);
2670 
2671 	for (i = 0; i < scan_res->num; i++) {
2672 		struct wpa_scan_res *scan_res_item = scan_res->res[i];
2673 
2674 		scan_snr(scan_res_item);
2675 		scan_est_throughput(wpa_s, scan_res_item);
2676 	}
2677 
2678 #ifdef CONFIG_WPS
2679 	if (wpas_wps_searching(wpa_s)) {
2680 		wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2681 			"provisioning rules");
2682 		compar = wpa_scan_result_wps_compar;
2683 	}
2684 #endif /* CONFIG_WPS */
2685 
2686 	if (scan_res->res) {
2687 		qsort(scan_res->res, scan_res->num,
2688 		      sizeof(struct wpa_scan_res *), compar);
2689 	}
2690 	dump_scan_res(scan_res);
2691 
2692 	if (wpa_s->ignore_post_flush_scan_res) {
2693 		/* FLUSH command aborted an ongoing scan and these are the
2694 		 * results from the aborted scan. Do not process the results to
2695 		 * maintain flushed state. */
2696 		wpa_dbg(wpa_s, MSG_DEBUG,
2697 			"Do not update BSS table based on pending post-FLUSH scan results");
2698 		wpa_s->ignore_post_flush_scan_res = 0;
2699 		return scan_res;
2700 	}
2701 
2702 	wpa_bss_update_start(wpa_s);
2703 	for (i = 0; i < scan_res->num; i++)
2704 		wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2705 					&scan_res->fetch_time);
2706 	wpa_bss_update_end(wpa_s, info, new_scan);
2707 
2708 	return scan_res;
2709 }
2710 
2711 
2712 /**
2713  * wpa_supplicant_update_scan_results - Update scan results from the driver
2714  * @wpa_s: Pointer to wpa_supplicant data
2715  * Returns: 0 on success, -1 on failure
2716  *
2717  * This function updates the BSS table within wpa_supplicant based on the
2718  * currently available scan results from the driver without requesting a new
2719  * scan. This is used in cases where the driver indicates an association
2720  * (including roaming within ESS) and wpa_supplicant does not yet have the
2721  * needed information to complete the connection (e.g., to perform validation
2722  * steps in 4-way handshake).
2723  */
2724 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2725 {
2726 	struct wpa_scan_results *scan_res;
2727 	scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2728 	if (scan_res == NULL)
2729 		return -1;
2730 	wpa_scan_results_free(scan_res);
2731 
2732 	return 0;
2733 }
2734 
2735 
2736 /**
2737  * scan_only_handler - Reports scan results
2738  */
2739 void scan_only_handler(struct wpa_supplicant *wpa_s,
2740 		       struct wpa_scan_results *scan_res)
2741 {
2742 	wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
2743 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2744 	    wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2745 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2746 			     wpa_s->manual_scan_id);
2747 		wpa_s->manual_scan_use_id = 0;
2748 	} else {
2749 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2750 	}
2751 	wpas_notify_scan_results(wpa_s);
2752 	wpas_notify_scan_done(wpa_s, 1);
2753 	if (wpa_s->scan_work) {
2754 		struct wpa_radio_work *work = wpa_s->scan_work;
2755 		wpa_s->scan_work = NULL;
2756 		radio_work_done(work);
2757 	}
2758 
2759 	if (wpa_s->wpa_state == WPA_SCANNING)
2760 		wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
2761 }
2762 
2763 
2764 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2765 {
2766 	return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2767 }
2768 
2769 
2770 struct wpa_driver_scan_params *
2771 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2772 {
2773 	struct wpa_driver_scan_params *params;
2774 	size_t i;
2775 	u8 *n;
2776 
2777 	params = os_zalloc(sizeof(*params));
2778 	if (params == NULL)
2779 		return NULL;
2780 
2781 	for (i = 0; i < src->num_ssids; i++) {
2782 		if (src->ssids[i].ssid) {
2783 			n = os_memdup(src->ssids[i].ssid,
2784 				      src->ssids[i].ssid_len);
2785 			if (n == NULL)
2786 				goto failed;
2787 			params->ssids[i].ssid = n;
2788 			params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2789 		}
2790 	}
2791 	params->num_ssids = src->num_ssids;
2792 
2793 	if (src->extra_ies) {
2794 		n = os_memdup(src->extra_ies, src->extra_ies_len);
2795 		if (n == NULL)
2796 			goto failed;
2797 		params->extra_ies = n;
2798 		params->extra_ies_len = src->extra_ies_len;
2799 	}
2800 
2801 	if (src->freqs) {
2802 		int len = int_array_len(src->freqs);
2803 		params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
2804 		if (params->freqs == NULL)
2805 			goto failed;
2806 	}
2807 
2808 	if (src->filter_ssids) {
2809 		params->filter_ssids = os_memdup(src->filter_ssids,
2810 						 sizeof(*params->filter_ssids) *
2811 						 src->num_filter_ssids);
2812 		if (params->filter_ssids == NULL)
2813 			goto failed;
2814 		params->num_filter_ssids = src->num_filter_ssids;
2815 	}
2816 
2817 	params->filter_rssi = src->filter_rssi;
2818 	params->p2p_probe = src->p2p_probe;
2819 	params->only_new_results = src->only_new_results;
2820 	params->low_priority = src->low_priority;
2821 	params->duration = src->duration;
2822 	params->duration_mandatory = src->duration_mandatory;
2823 	params->oce_scan = src->oce_scan;
2824 
2825 	if (src->sched_scan_plans_num > 0) {
2826 		params->sched_scan_plans =
2827 			os_memdup(src->sched_scan_plans,
2828 				  sizeof(*src->sched_scan_plans) *
2829 				  src->sched_scan_plans_num);
2830 		if (!params->sched_scan_plans)
2831 			goto failed;
2832 
2833 		params->sched_scan_plans_num = src->sched_scan_plans_num;
2834 	}
2835 
2836 	if (src->mac_addr_rand &&
2837 	    wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2838 		goto failed;
2839 
2840 	if (src->bssid) {
2841 		u8 *bssid;
2842 
2843 		bssid = os_memdup(src->bssid, ETH_ALEN);
2844 		if (!bssid)
2845 			goto failed;
2846 		params->bssid = bssid;
2847 	}
2848 
2849 	params->relative_rssi_set = src->relative_rssi_set;
2850 	params->relative_rssi = src->relative_rssi;
2851 	params->relative_adjust_band = src->relative_adjust_band;
2852 	params->relative_adjust_rssi = src->relative_adjust_rssi;
2853 	params->p2p_include_6ghz = src->p2p_include_6ghz;
2854 	return params;
2855 
2856 failed:
2857 	wpa_scan_free_params(params);
2858 	return NULL;
2859 }
2860 
2861 
2862 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2863 {
2864 	size_t i;
2865 
2866 	if (params == NULL)
2867 		return;
2868 
2869 	for (i = 0; i < params->num_ssids; i++)
2870 		os_free((u8 *) params->ssids[i].ssid);
2871 	os_free((u8 *) params->extra_ies);
2872 	os_free(params->freqs);
2873 	os_free(params->filter_ssids);
2874 	os_free(params->sched_scan_plans);
2875 
2876 	/*
2877 	 * Note: params->mac_addr_mask points to same memory allocation and
2878 	 * must not be freed separately.
2879 	 */
2880 	os_free((u8 *) params->mac_addr);
2881 
2882 	os_free((u8 *) params->bssid);
2883 
2884 	os_free(params);
2885 }
2886 
2887 
2888 int wpas_start_pno(struct wpa_supplicant *wpa_s)
2889 {
2890 	int ret;
2891 	size_t prio, i, num_ssid, num_match_ssid;
2892 	struct wpa_ssid *ssid;
2893 	struct wpa_driver_scan_params params;
2894 	struct sched_scan_plan scan_plan;
2895 	unsigned int max_sched_scan_ssids;
2896 
2897 	if (!wpa_s->sched_scan_supported)
2898 		return -1;
2899 
2900 	if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2901 		max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2902 	else
2903 		max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2904 	if (max_sched_scan_ssids < 1)
2905 		return -1;
2906 
2907 	if (wpa_s->pno || wpa_s->pno_sched_pending)
2908 		return 0;
2909 
2910 	if ((wpa_s->wpa_state > WPA_SCANNING) &&
2911 	    (wpa_s->wpa_state < WPA_COMPLETED)) {
2912 		wpa_printf(MSG_ERROR, "PNO: In assoc process");
2913 		return -EAGAIN;
2914 	}
2915 
2916 	if (wpa_s->wpa_state == WPA_SCANNING) {
2917 		wpa_supplicant_cancel_scan(wpa_s);
2918 		if (wpa_s->sched_scanning) {
2919 			wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2920 				   "ongoing sched scan");
2921 			wpa_supplicant_cancel_sched_scan(wpa_s);
2922 			wpa_s->pno_sched_pending = 1;
2923 			return 0;
2924 		}
2925 	}
2926 
2927 	if (wpa_s->sched_scan_stop_req) {
2928 		wpa_printf(MSG_DEBUG,
2929 			   "Schedule PNO after previous sched scan has stopped");
2930 		wpa_s->pno_sched_pending = 1;
2931 		return 0;
2932 	}
2933 
2934 	os_memset(&params, 0, sizeof(params));
2935 
2936 	num_ssid = num_match_ssid = 0;
2937 	ssid = wpa_s->conf->ssid;
2938 	while (ssid) {
2939 		if (!wpas_network_disabled(wpa_s, ssid)) {
2940 			num_match_ssid++;
2941 			if (ssid->scan_ssid)
2942 				num_ssid++;
2943 		}
2944 		ssid = ssid->next;
2945 	}
2946 
2947 	if (num_match_ssid == 0) {
2948 		wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2949 		return -1;
2950 	}
2951 
2952 	if (num_match_ssid > num_ssid) {
2953 		params.num_ssids++; /* wildcard */
2954 		num_ssid++;
2955 	}
2956 
2957 	if (num_ssid > max_sched_scan_ssids) {
2958 		wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2959 			   "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2960 		num_ssid = max_sched_scan_ssids;
2961 	}
2962 
2963 	if (num_match_ssid > wpa_s->max_match_sets) {
2964 		num_match_ssid = wpa_s->max_match_sets;
2965 		wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
2966 	}
2967 	params.filter_ssids = os_calloc(num_match_ssid,
2968 					sizeof(struct wpa_driver_scan_filter));
2969 	if (params.filter_ssids == NULL)
2970 		return -1;
2971 
2972 	i = 0;
2973 	prio = 0;
2974 	ssid = wpa_s->conf->pssid[prio];
2975 	while (ssid) {
2976 		if (!wpas_network_disabled(wpa_s, ssid)) {
2977 			if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2978 				params.ssids[params.num_ssids].ssid =
2979 					ssid->ssid;
2980 				params.ssids[params.num_ssids].ssid_len =
2981 					 ssid->ssid_len;
2982 				params.num_ssids++;
2983 			}
2984 			os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2985 				  ssid->ssid_len);
2986 			params.filter_ssids[i].ssid_len = ssid->ssid_len;
2987 			params.num_filter_ssids++;
2988 			i++;
2989 			if (i == num_match_ssid)
2990 				break;
2991 		}
2992 		if (ssid->pnext)
2993 			ssid = ssid->pnext;
2994 		else if (prio + 1 == wpa_s->conf->num_prio)
2995 			break;
2996 		else
2997 			ssid = wpa_s->conf->pssid[++prio];
2998 	}
2999 
3000 	if (wpa_s->conf->filter_rssi)
3001 		params.filter_rssi = wpa_s->conf->filter_rssi;
3002 
3003 	if (wpa_s->sched_scan_plans_num) {
3004 		params.sched_scan_plans = wpa_s->sched_scan_plans;
3005 		params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3006 	} else {
3007 		/* Set one scan plan that will run infinitely */
3008 		if (wpa_s->conf->sched_scan_interval)
3009 			scan_plan.interval = wpa_s->conf->sched_scan_interval;
3010 		else
3011 			scan_plan.interval = 10;
3012 
3013 		scan_plan.iterations = 0;
3014 		params.sched_scan_plans = &scan_plan;
3015 		params.sched_scan_plans_num = 1;
3016 	}
3017 
3018 	params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3019 
3020 	if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3021 		wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3022 		params.freqs = wpa_s->manual_sched_scan_freqs;
3023 	}
3024 
3025 	if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
3026 	    wpa_s->wpa_state <= WPA_SCANNING)
3027 		wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
3028 
3029 	wpa_scan_set_relative_rssi_params(wpa_s, &params);
3030 
3031 	ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
3032 	os_free(params.filter_ssids);
3033 	os_free(params.mac_addr);
3034 	if (ret == 0)
3035 		wpa_s->pno = 1;
3036 	else
3037 		wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3038 	return ret;
3039 }
3040 
3041 
3042 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3043 {
3044 	int ret = 0;
3045 
3046 	if (!wpa_s->pno)
3047 		return 0;
3048 
3049 	ret = wpa_supplicant_stop_sched_scan(wpa_s);
3050 	wpa_s->sched_scan_stop_req = 1;
3051 
3052 	wpa_s->pno = 0;
3053 	wpa_s->pno_sched_pending = 0;
3054 
3055 	if (wpa_s->wpa_state == WPA_SCANNING)
3056 		wpa_supplicant_req_scan(wpa_s, 0, 0);
3057 
3058 	return ret;
3059 }
3060 
3061 
3062 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3063 				    unsigned int type)
3064 {
3065 	type &= MAC_ADDR_RAND_ALL;
3066 	wpa_s->mac_addr_rand_enable &= ~type;
3067 
3068 	if (type & MAC_ADDR_RAND_SCAN) {
3069 		os_free(wpa_s->mac_addr_scan);
3070 		wpa_s->mac_addr_scan = NULL;
3071 	}
3072 
3073 	if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3074 		os_free(wpa_s->mac_addr_sched_scan);
3075 		wpa_s->mac_addr_sched_scan = NULL;
3076 	}
3077 
3078 	if (type & MAC_ADDR_RAND_PNO) {
3079 		os_free(wpa_s->mac_addr_pno);
3080 		wpa_s->mac_addr_pno = NULL;
3081 	}
3082 }
3083 
3084 
3085 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3086 				unsigned int type, const u8 *addr,
3087 				const u8 *mask)
3088 {
3089 	u8 *tmp = NULL;
3090 
3091 	if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3092 		wpa_printf(MSG_INFO,
3093 			   "scan: MAC randomization type %u != supported=%u",
3094 			   type, wpa_s->mac_addr_rand_supported);
3095 		return -1;
3096 	}
3097 
3098 	wpas_mac_addr_rand_scan_clear(wpa_s, type);
3099 
3100 	if (addr) {
3101 		tmp = os_malloc(2 * ETH_ALEN);
3102 		if (!tmp)
3103 			return -1;
3104 		os_memcpy(tmp, addr, ETH_ALEN);
3105 		os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3106 	}
3107 
3108 	if (type == MAC_ADDR_RAND_SCAN) {
3109 		wpa_s->mac_addr_scan = tmp;
3110 	} else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3111 		wpa_s->mac_addr_sched_scan = tmp;
3112 	} else if (type == MAC_ADDR_RAND_PNO) {
3113 		wpa_s->mac_addr_pno = tmp;
3114 	} else {
3115 		wpa_printf(MSG_INFO,
3116 			   "scan: Invalid MAC randomization type=0x%x",
3117 			   type);
3118 		os_free(tmp);
3119 		return -1;
3120 	}
3121 
3122 	wpa_s->mac_addr_rand_enable |= type;
3123 	return 0;
3124 }
3125 
3126 
3127 int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3128 				     unsigned int type, u8 *mask)
3129 {
3130 	const u8 *to_copy;
3131 
3132 	if ((wpa_s->mac_addr_rand_enable & type) != type)
3133 		return -1;
3134 
3135 	if (type == MAC_ADDR_RAND_SCAN) {
3136 		to_copy = wpa_s->mac_addr_scan;
3137 	} else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3138 		to_copy = wpa_s->mac_addr_sched_scan;
3139 	} else if (type == MAC_ADDR_RAND_PNO) {
3140 		to_copy = wpa_s->mac_addr_pno;
3141 	} else {
3142 		wpa_printf(MSG_DEBUG,
3143 			   "scan: Invalid MAC randomization type=0x%x",
3144 			   type);
3145 		return -1;
3146 	}
3147 
3148 	os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3149 	return 0;
3150 }
3151 
3152 
3153 int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3154 {
3155 	struct wpa_radio_work *work;
3156 	struct wpa_radio *radio = wpa_s->radio;
3157 
3158 	dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3159 		if (work->wpa_s != wpa_s || !work->started ||
3160 		    (os_strcmp(work->type, "scan") != 0 &&
3161 		     os_strcmp(work->type, "p2p-scan") != 0))
3162 			continue;
3163 		wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
3164 		return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
3165 	}
3166 
3167 	wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3168 	return -1;
3169 }
3170 
3171 
3172 int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3173 {
3174 	struct sched_scan_plan *scan_plans = NULL;
3175 	const char *token, *context = NULL;
3176 	unsigned int num = 0;
3177 
3178 	if (!cmd)
3179 		return -1;
3180 
3181 	if (!cmd[0]) {
3182 		wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3183 		os_free(wpa_s->sched_scan_plans);
3184 		wpa_s->sched_scan_plans = NULL;
3185 		wpa_s->sched_scan_plans_num = 0;
3186 		return 0;
3187 	}
3188 
3189 	while ((token = cstr_token(cmd, " ", &context))) {
3190 		int ret;
3191 		struct sched_scan_plan *scan_plan, *n;
3192 
3193 		n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3194 		if (!n)
3195 			goto fail;
3196 
3197 		scan_plans = n;
3198 		scan_plan = &scan_plans[num];
3199 		num++;
3200 
3201 		ret = sscanf(token, "%u:%u", &scan_plan->interval,
3202 			     &scan_plan->iterations);
3203 		if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3204 			wpa_printf(MSG_ERROR,
3205 				   "Invalid sched scan plan input: %s", token);
3206 			goto fail;
3207 		}
3208 
3209 		if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3210 			wpa_printf(MSG_WARNING,
3211 				   "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3212 				   num, scan_plan->interval,
3213 				   wpa_s->max_sched_scan_plan_interval);
3214 			scan_plan->interval =
3215 				wpa_s->max_sched_scan_plan_interval;
3216 		}
3217 
3218 		if (ret == 1) {
3219 			scan_plan->iterations = 0;
3220 			break;
3221 		}
3222 
3223 		if (!scan_plan->iterations) {
3224 			wpa_printf(MSG_ERROR,
3225 				   "scan plan %u: Number of iterations cannot be zero",
3226 				   num);
3227 			goto fail;
3228 		}
3229 
3230 		if (scan_plan->iterations >
3231 		    wpa_s->max_sched_scan_plan_iterations) {
3232 			wpa_printf(MSG_WARNING,
3233 				   "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3234 				   num, scan_plan->iterations,
3235 				   wpa_s->max_sched_scan_plan_iterations);
3236 			scan_plan->iterations =
3237 				wpa_s->max_sched_scan_plan_iterations;
3238 		}
3239 
3240 		wpa_printf(MSG_DEBUG,
3241 			   "scan plan %u: interval=%u iterations=%u",
3242 			   num, scan_plan->interval, scan_plan->iterations);
3243 	}
3244 
3245 	if (!scan_plans) {
3246 		wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3247 		goto fail;
3248 	}
3249 
3250 	if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3251 		wpa_printf(MSG_ERROR,
3252 			   "All scan plans but the last must specify a number of iterations");
3253 		goto fail;
3254 	}
3255 
3256 	wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3257 		   num, scan_plans[num - 1].interval);
3258 
3259 	if (num > wpa_s->max_sched_scan_plans) {
3260 		wpa_printf(MSG_WARNING,
3261 			   "Too many scheduled scan plans (only %u supported)",
3262 			   wpa_s->max_sched_scan_plans);
3263 		wpa_printf(MSG_WARNING,
3264 			   "Use only the first %u scan plans, and the last one (in infinite loop)",
3265 			   wpa_s->max_sched_scan_plans - 1);
3266 		os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3267 			  &scan_plans[num - 1], sizeof(*scan_plans));
3268 		num = wpa_s->max_sched_scan_plans;
3269 	}
3270 
3271 	os_free(wpa_s->sched_scan_plans);
3272 	wpa_s->sched_scan_plans = scan_plans;
3273 	wpa_s->sched_scan_plans_num = num;
3274 
3275 	return 0;
3276 
3277 fail:
3278 	os_free(scan_plans);
3279 	wpa_printf(MSG_ERROR, "invalid scan plans list");
3280 	return -1;
3281 }
3282 
3283 
3284 /**
3285  * wpas_scan_reset_sched_scan - Reset sched_scan state
3286  * @wpa_s: Pointer to wpa_supplicant data
3287  *
3288  * This function is used to cancel a running scheduled scan and to reset an
3289  * internal scan state to continue with a regular scan on the following
3290  * wpa_supplicant_req_scan() calls.
3291  */
3292 void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3293 {
3294 	wpa_s->normal_scans = 0;
3295 	if (wpa_s->sched_scanning) {
3296 		wpa_s->sched_scan_timed_out = 0;
3297 		wpa_s->prev_sched_ssid = NULL;
3298 		wpa_supplicant_cancel_sched_scan(wpa_s);
3299 	}
3300 }
3301 
3302 
3303 void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3304 {
3305 	/* simulate timeout to restart the sched scan */
3306 	wpa_s->sched_scan_timed_out = 1;
3307 	wpa_s->prev_sched_ssid = NULL;
3308 	wpa_supplicant_cancel_sched_scan(wpa_s);
3309 }
3310