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