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