xref: /linux/net/wireless/scan.c (revision 5c458073553f0ef74f5c8db1bd459c87c722a299)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cfg80211 scan result handling
4  *
5  * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2016	Intel Deutschland GmbH
8  * Copyright (C) 2018-2026 Intel Corporation
9  */
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/wireless.h>
15 #include <linux/nl80211.h>
16 #include <linux/etherdevice.h>
17 #include <linux/crc32.h>
18 #include <linux/bitfield.h>
19 #include <net/arp.h>
20 #include <net/cfg80211.h>
21 #include <net/cfg80211-wext.h>
22 #include <net/iw_handler.h>
23 #include <kunit/visibility.h>
24 #include "core.h"
25 #include "nl80211.h"
26 #include "wext-compat.h"
27 #include "rdev-ops.h"
28 
29 /**
30  * DOC: BSS tree/list structure
31  *
32  * At the top level, the BSS list is kept in both a list in each
33  * registered device (@bss_list) as well as an RB-tree for faster
34  * lookup. In the RB-tree, entries can be looked up using their
35  * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
36  * for other BSSes.
37  *
38  * Due to the possibility of hidden SSIDs, there's a second level
39  * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
40  * The hidden_list connects all BSSes belonging to a single AP
41  * that has a hidden SSID, and connects beacon and probe response
42  * entries. For a probe response entry for a hidden SSID, the
43  * hidden_beacon_bss pointer points to the BSS struct holding the
44  * beacon's information.
45  *
46  * Reference counting is done for all these references except for
47  * the hidden_list, so that a beacon BSS struct that is otherwise
48  * not referenced has one reference for being on the bss_list and
49  * one for each probe response entry that points to it using the
50  * hidden_beacon_bss pointer. When a BSS struct that has such a
51  * pointer is get/put, the refcount update is also propagated to
52  * the referenced struct, this ensure that it cannot get removed
53  * while somebody is using the probe response version.
54  *
55  * Note that the hidden_beacon_bss pointer never changes, due to
56  * the reference counting. Therefore, no locking is needed for
57  * it.
58  *
59  * Also note that the hidden_beacon_bss pointer is only relevant
60  * if the driver uses something other than the IEs, e.g. private
61  * data stored in the BSS struct, since the beacon IEs are
62  * also linked into the probe response struct.
63  */
64 
65 /*
66  * Limit the number of BSS entries stored in mac80211. Each one is
67  * a bit over 4k at most, so this limits to roughly 4-5M of memory.
68  * If somebody wants to really attack this though, they'd likely
69  * use small beacons, and only one type of frame, limiting each of
70  * the entries to a much smaller size (in order to generate more
71  * entries in total, so overhead is bigger.)
72  */
73 static int bss_entries_limit = 1000;
74 module_param(bss_entries_limit, int, 0644);
75 MODULE_PARM_DESC(bss_entries_limit,
76                  "limit to number of scan BSS entries (per wiphy, default 1000)");
77 
78 #define IEEE80211_SCAN_RESULT_EXPIRE	(30 * HZ)
79 
80 static void bss_free(struct cfg80211_internal_bss *bss)
81 {
82 	struct cfg80211_bss_ies *ies;
83 
84 	if (WARN_ON(atomic_read(&bss->hold)))
85 		return;
86 
87 	ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
88 	if (ies && !bss->pub.hidden_beacon_bss)
89 		kfree_rcu(ies, rcu_head);
90 	ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
91 	if (ies)
92 		kfree_rcu(ies, rcu_head);
93 
94 	/*
95 	 * This happens when the module is removed, it doesn't
96 	 * really matter any more save for completeness
97 	 */
98 	if (!list_empty(&bss->hidden_list))
99 		list_del(&bss->hidden_list);
100 
101 	kfree(bss);
102 }
103 
104 static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
105 			       struct cfg80211_internal_bss *bss)
106 {
107 	lockdep_assert_held(&rdev->bss_lock);
108 
109 	bss->refcount++;
110 
111 	if (bss->pub.hidden_beacon_bss)
112 		bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
113 
114 	if (bss->pub.transmitted_bss)
115 		bss_from_pub(bss->pub.transmitted_bss)->refcount++;
116 }
117 
118 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
119 			       struct cfg80211_internal_bss *bss)
120 {
121 	lockdep_assert_held(&rdev->bss_lock);
122 
123 	if (bss->pub.hidden_beacon_bss) {
124 		struct cfg80211_internal_bss *hbss;
125 
126 		hbss = bss_from_pub(bss->pub.hidden_beacon_bss);
127 		hbss->refcount--;
128 		if (hbss->refcount == 0)
129 			bss_free(hbss);
130 	}
131 
132 	if (bss->pub.transmitted_bss) {
133 		struct cfg80211_internal_bss *tbss;
134 
135 		tbss = bss_from_pub(bss->pub.transmitted_bss);
136 		tbss->refcount--;
137 		if (tbss->refcount == 0)
138 			bss_free(tbss);
139 	}
140 
141 	bss->refcount--;
142 	if (bss->refcount == 0)
143 		bss_free(bss);
144 }
145 
146 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
147 				  struct cfg80211_internal_bss *bss)
148 {
149 	lockdep_assert_held(&rdev->bss_lock);
150 
151 	if (!list_empty(&bss->hidden_list)) {
152 		/*
153 		 * don't remove the beacon entry if it has
154 		 * probe responses associated with it
155 		 */
156 		if (!bss->pub.hidden_beacon_bss)
157 			return false;
158 		/*
159 		 * if it's a probe response entry break its
160 		 * link to the other entries in the group
161 		 */
162 		list_del_init(&bss->hidden_list);
163 	}
164 
165 	list_del_init(&bss->list);
166 	list_del_init(&bss->pub.nontrans_list);
167 	rb_erase(&bss->rbn, &rdev->bss_tree);
168 	rdev->bss_entries--;
169 	WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
170 		  "rdev bss entries[%d]/list[empty:%d] corruption\n",
171 		  rdev->bss_entries, list_empty(&rdev->bss_list));
172 	bss_ref_put(rdev, bss);
173 	return true;
174 }
175 
176 bool cfg80211_is_element_inherited(const struct element *elem,
177 				   const struct element *non_inherit_elem)
178 {
179 	u8 id_len, ext_id_len, i, loop_len, id;
180 	const u8 *list;
181 
182 	if (elem->id == WLAN_EID_MULTIPLE_BSSID)
183 		return false;
184 
185 	if (elem->id == WLAN_EID_EXTENSION && elem->datalen > 1 &&
186 	    elem->data[0] == WLAN_EID_EXT_EHT_MULTI_LINK)
187 		return false;
188 
189 	if (!non_inherit_elem || non_inherit_elem->datalen < 2)
190 		return true;
191 
192 	/*
193 	 * non inheritance element format is:
194 	 * ext ID (56) | IDs list len | list | extension IDs list len | list
195 	 * Both lists are optional. Both lengths are mandatory.
196 	 * This means valid length is:
197 	 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
198 	 */
199 	id_len = non_inherit_elem->data[1];
200 	if (non_inherit_elem->datalen < 3 + id_len)
201 		return true;
202 
203 	ext_id_len = non_inherit_elem->data[2 + id_len];
204 	if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
205 		return true;
206 
207 	if (elem->id == WLAN_EID_EXTENSION) {
208 		if (!ext_id_len || !elem->datalen)
209 			return true;
210 		loop_len = ext_id_len;
211 		list = &non_inherit_elem->data[3 + id_len];
212 		id = elem->data[0];
213 	} else {
214 		if (!id_len)
215 			return true;
216 		loop_len = id_len;
217 		list = &non_inherit_elem->data[2];
218 		id = elem->id;
219 	}
220 
221 	for (i = 0; i < loop_len; i++) {
222 		if (list[i] == id)
223 			return false;
224 	}
225 
226 	return true;
227 }
228 EXPORT_SYMBOL(cfg80211_is_element_inherited);
229 
230 static size_t cfg80211_copy_elem_with_frags(const struct element *elem,
231 					    const u8 *ie, size_t ie_len,
232 					    u8 **pos, u8 *buf, size_t buf_len)
233 {
234 	if (WARN_ON((u8 *)elem < ie || elem->data > ie + ie_len ||
235 		    elem->data + elem->datalen > ie + ie_len))
236 		return 0;
237 
238 	if (elem->datalen + 2 > buf + buf_len - *pos)
239 		return 0;
240 
241 	memcpy(*pos, elem, elem->datalen + 2);
242 	*pos += elem->datalen + 2;
243 
244 	/* Finish if it is not fragmented  */
245 	if (elem->datalen != 255)
246 		return *pos - buf;
247 
248 	ie_len = ie + ie_len - elem->data - elem->datalen;
249 	ie = (const u8 *)elem->data + elem->datalen;
250 
251 	for_each_element(elem, ie, ie_len) {
252 		if (elem->id != WLAN_EID_FRAGMENT)
253 			break;
254 
255 		if (elem->datalen + 2 > buf + buf_len - *pos)
256 			return 0;
257 
258 		memcpy(*pos, elem, elem->datalen + 2);
259 		*pos += elem->datalen + 2;
260 
261 		if (elem->datalen != 255)
262 			break;
263 	}
264 
265 	return *pos - buf;
266 }
267 
268 VISIBLE_IF_CFG80211_KUNIT size_t
269 cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
270 		    const u8 *subie, size_t subie_len,
271 		    u8 *new_ie, size_t new_ie_len)
272 {
273 	const struct element *non_inherit_elem, *parent, *sub;
274 	u8 *pos = new_ie;
275 	const u8 *mbssid_index_ie;
276 	u8 id, ext_id, bssid_index = 255;
277 	unsigned int match_len;
278 
279 	non_inherit_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
280 						  subie, subie_len);
281 
282 	mbssid_index_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, subie,
283 					   subie_len);
284 	if (mbssid_index_ie && mbssid_index_ie[1] > 0 &&
285 	    mbssid_index_ie[2] > 0 && mbssid_index_ie[2] <= 46)
286 		bssid_index = mbssid_index_ie[2];
287 
288 	/* We copy the elements one by one from the parent to the generated
289 	 * elements.
290 	 * If they are not inherited (included in subie or in the non
291 	 * inheritance element), then we copy all occurrences the first time
292 	 * we see this element type.
293 	 */
294 	for_each_element(parent, ie, ielen) {
295 		if (parent->id == WLAN_EID_FRAGMENT)
296 			continue;
297 
298 		if (parent->id == WLAN_EID_EXTENSION) {
299 			if (parent->datalen < 1)
300 				continue;
301 
302 			id = WLAN_EID_EXTENSION;
303 			ext_id = parent->data[0];
304 			match_len = 1;
305 		} else {
306 			id = parent->id;
307 			match_len = 0;
308 		}
309 
310 		/* Find first occurrence in subie */
311 		sub = cfg80211_find_elem_match(id, subie, subie_len,
312 					       &ext_id, match_len, 0);
313 
314 		/* Copy from parent if not in subie and inherited */
315 		if (!sub &&
316 		    cfg80211_is_element_inherited(parent, non_inherit_elem)) {
317 			if (!cfg80211_copy_elem_with_frags(parent,
318 							   ie, ielen,
319 							   &pos, new_ie,
320 							   new_ie_len))
321 				return 0;
322 
323 			continue;
324 		}
325 
326 		/* For ML probe response, match the MLE in the frame body with
327 		 * MLD id being 'bssid_index'
328 		 */
329 		if (parent->id == WLAN_EID_EXTENSION &&
330 		    parent->data[0] == WLAN_EID_EXT_EHT_MULTI_LINK &&
331 		    ieee80211_mle_type_ok(parent->data + 1,
332 					  IEEE80211_ML_CONTROL_TYPE_BASIC,
333 					  parent->datalen - 1) &&
334 		    bssid_index == ieee80211_mle_get_mld_id(parent->data + 1)) {
335 			if (!cfg80211_copy_elem_with_frags(parent,
336 							   ie, ielen,
337 							   &pos, new_ie,
338 							   new_ie_len))
339 				return 0;
340 
341 			/* Continue here to prevent processing the MLE in
342 			 * sub-element, which AP MLD should not carry
343 			 */
344 			continue;
345 		}
346 
347 		/* Already copied if an earlier element had the same type */
348 		if (cfg80211_find_elem_match(id, ie, (u8 *)parent - ie,
349 					     &ext_id, match_len, 0))
350 			continue;
351 
352 		/* Not inheriting, copy all similar elements from subie */
353 		while (sub) {
354 			if (!cfg80211_copy_elem_with_frags(sub,
355 							   subie, subie_len,
356 							   &pos, new_ie,
357 							   new_ie_len))
358 				return 0;
359 
360 			sub = cfg80211_find_elem_match(id,
361 						       sub->data + sub->datalen,
362 						       subie_len + subie -
363 						       (sub->data +
364 							sub->datalen),
365 						       &ext_id, match_len, 0);
366 		}
367 	}
368 
369 	/* The above misses elements that are included in subie but not in the
370 	 * parent, so do a pass over subie and append those.
371 	 * Skip the non-tx BSSID caps and non-inheritance element.
372 	 */
373 	for_each_element(sub, subie, subie_len) {
374 		if (sub->id == WLAN_EID_NON_TX_BSSID_CAP)
375 			continue;
376 
377 		if (sub->id == WLAN_EID_FRAGMENT)
378 			continue;
379 
380 		if (sub->id == WLAN_EID_EXTENSION) {
381 			if (sub->datalen < 1)
382 				continue;
383 
384 			id = WLAN_EID_EXTENSION;
385 			ext_id = sub->data[0];
386 			match_len = 1;
387 
388 			if (ext_id == WLAN_EID_EXT_NON_INHERITANCE)
389 				continue;
390 		} else {
391 			id = sub->id;
392 			match_len = 0;
393 		}
394 
395 		/* Processed if one was included in the parent */
396 		if (cfg80211_find_elem_match(id, ie, ielen,
397 					     &ext_id, match_len, 0))
398 			continue;
399 
400 		if (!cfg80211_copy_elem_with_frags(sub, subie, subie_len,
401 						   &pos, new_ie, new_ie_len))
402 			return 0;
403 	}
404 
405 	return pos - new_ie;
406 }
407 EXPORT_SYMBOL_IF_CFG80211_KUNIT(cfg80211_gen_new_ie);
408 
409 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
410 		   const u8 *ssid, size_t ssid_len)
411 {
412 	const struct cfg80211_bss_ies *ies;
413 	const struct element *ssid_elem;
414 
415 	if (bssid && !ether_addr_equal(a->bssid, bssid))
416 		return false;
417 
418 	if (!ssid)
419 		return true;
420 
421 	ies = rcu_access_pointer(a->ies);
422 	if (!ies)
423 		return false;
424 	ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
425 	if (!ssid_elem)
426 		return false;
427 	if (ssid_elem->datalen != ssid_len)
428 		return false;
429 	return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
430 }
431 
432 static int
433 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
434 			   struct cfg80211_bss *nontrans_bss)
435 {
436 	const struct element *ssid_elem;
437 	struct cfg80211_bss *bss = NULL;
438 
439 	rcu_read_lock();
440 	ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
441 	if (!ssid_elem) {
442 		rcu_read_unlock();
443 		return -EINVAL;
444 	}
445 
446 	/* check if nontrans_bss is in the list */
447 	list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
448 		if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
449 			   ssid_elem->datalen)) {
450 			rcu_read_unlock();
451 			return 0;
452 		}
453 	}
454 
455 	rcu_read_unlock();
456 
457 	/*
458 	 * This is a bit weird - it's not on the list, but already on another
459 	 * one! The only way that could happen is if there's some BSSID/SSID
460 	 * shared by multiple APs in their multi-BSSID profiles, potentially
461 	 * with hidden SSID mixed in ... ignore it.
462 	 */
463 	if (!list_empty(&nontrans_bss->nontrans_list))
464 		return -EINVAL;
465 
466 	/* add to the list */
467 	list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
468 	return 0;
469 }
470 
471 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
472 				  unsigned long expire_time)
473 {
474 	struct cfg80211_internal_bss *bss, *tmp;
475 	bool expired = false;
476 
477 	lockdep_assert_held(&rdev->bss_lock);
478 
479 	list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
480 		if (atomic_read(&bss->hold))
481 			continue;
482 		if (!time_after(expire_time, bss->ts))
483 			continue;
484 
485 		if (__cfg80211_unlink_bss(rdev, bss))
486 			expired = true;
487 	}
488 
489 	if (expired)
490 		rdev->bss_generation++;
491 }
492 
493 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
494 {
495 	struct cfg80211_internal_bss *bss, *oldest = NULL;
496 	bool ret;
497 
498 	lockdep_assert_held(&rdev->bss_lock);
499 
500 	list_for_each_entry(bss, &rdev->bss_list, list) {
501 		if (atomic_read(&bss->hold))
502 			continue;
503 
504 		if (!list_empty(&bss->hidden_list) &&
505 		    !bss->pub.hidden_beacon_bss)
506 			continue;
507 
508 		if (oldest && time_before(oldest->ts, bss->ts))
509 			continue;
510 		oldest = bss;
511 	}
512 
513 	if (WARN_ON(!oldest))
514 		return false;
515 
516 	/*
517 	 * The callers make sure to increase rdev->bss_generation if anything
518 	 * gets removed (and a new entry added), so there's no need to also do
519 	 * it here.
520 	 */
521 
522 	ret = __cfg80211_unlink_bss(rdev, oldest);
523 	WARN_ON(!ret);
524 	return ret;
525 }
526 
527 static u8 cfg80211_parse_bss_param(u8 data,
528 				   struct cfg80211_colocated_ap *coloc_ap)
529 {
530 	coloc_ap->oct_recommended =
531 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
532 	coloc_ap->same_ssid =
533 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
534 	coloc_ap->multi_bss =
535 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
536 	coloc_ap->transmitted_bssid =
537 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
538 	coloc_ap->unsolicited_probe =
539 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
540 	coloc_ap->colocated_ess =
541 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
542 
543 	return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
544 }
545 
546 static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
547 				    const struct element **elem, u32 *s_ssid)
548 {
549 
550 	*elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
551 	if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
552 		return -EINVAL;
553 
554 	*s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
555 	return 0;
556 }
557 
558 VISIBLE_IF_CFG80211_KUNIT void
559 cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
560 {
561 	struct cfg80211_colocated_ap *ap, *tmp_ap;
562 
563 	list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
564 		list_del(&ap->list);
565 		kfree(ap);
566 	}
567 }
568 EXPORT_SYMBOL_IF_CFG80211_KUNIT(cfg80211_free_coloc_ap_list);
569 
570 static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
571 				  const u8 *pos, u8 length,
572 				  const struct element *ssid_elem,
573 				  u32 s_ssid_tmp)
574 {
575 	u8 bss_params;
576 
577 	entry->psd_20 = IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
578 
579 	/* The length is already verified by the caller to contain bss_params */
580 	if (length > sizeof(struct ieee80211_tbtt_info_7_8_9)) {
581 		struct ieee80211_tbtt_info_ge_11 *tbtt_info = (void *)pos;
582 
583 		memcpy(entry->bssid, tbtt_info->bssid, ETH_ALEN);
584 		entry->short_ssid = le32_to_cpu(tbtt_info->short_ssid);
585 		entry->short_ssid_valid = true;
586 
587 		bss_params = tbtt_info->bss_params;
588 
589 		/* Ignore disabled links */
590 		if (length >= offsetofend(typeof(*tbtt_info), mld_params)) {
591 			if (le16_get_bits(tbtt_info->mld_params.params,
592 					  IEEE80211_RNR_MLD_PARAMS_DISABLED_LINK))
593 				return -EINVAL;
594 		}
595 
596 		if (length >= offsetofend(struct ieee80211_tbtt_info_ge_11,
597 					  psd_20))
598 			entry->psd_20 = tbtt_info->psd_20;
599 	} else {
600 		struct ieee80211_tbtt_info_7_8_9 *tbtt_info = (void *)pos;
601 
602 		memcpy(entry->bssid, tbtt_info->bssid, ETH_ALEN);
603 
604 		bss_params = tbtt_info->bss_params;
605 
606 		if (length == offsetofend(struct ieee80211_tbtt_info_7_8_9,
607 					  psd_20))
608 			entry->psd_20 = tbtt_info->psd_20;
609 	}
610 
611 	/* ignore entries with invalid BSSID */
612 	if (!is_valid_ether_addr(entry->bssid))
613 		return -EINVAL;
614 
615 	/* skip non colocated APs */
616 	if (!cfg80211_parse_bss_param(bss_params, entry))
617 		return -EINVAL;
618 
619 	/* no information about the short ssid. Consider the entry valid
620 	 * for now. It would later be dropped in case there are explicit
621 	 * SSIDs that need to be matched
622 	 */
623 	if (!entry->same_ssid && !entry->short_ssid_valid)
624 		return 0;
625 
626 	if (entry->same_ssid) {
627 		entry->short_ssid = s_ssid_tmp;
628 		entry->short_ssid_valid = true;
629 
630 		/*
631 		 * This is safe because we validate datalen in
632 		 * cfg80211_parse_colocated_ap(), before calling this
633 		 * function.
634 		 */
635 		memcpy(&entry->ssid, &ssid_elem->data, ssid_elem->datalen);
636 		entry->ssid_len = ssid_elem->datalen;
637 	}
638 
639 	return 0;
640 }
641 
642 bool cfg80211_iter_rnr(const u8 *elems, size_t elems_len,
643 		       enum cfg80211_rnr_iter_ret
644 		       (*iter)(void *data, u8 type,
645 			       const struct ieee80211_neighbor_ap_info *info,
646 			       const u8 *tbtt_info, u8 tbtt_info_len),
647 		       void *iter_data)
648 {
649 	const struct element *rnr;
650 	const u8 *pos, *end;
651 
652 	for_each_element_id(rnr, WLAN_EID_REDUCED_NEIGHBOR_REPORT,
653 			    elems, elems_len) {
654 		const struct ieee80211_neighbor_ap_info *info;
655 
656 		pos = rnr->data;
657 		end = rnr->data + rnr->datalen;
658 
659 		/* RNR IE may contain more than one NEIGHBOR_AP_INFO */
660 		while (sizeof(*info) <= end - pos) {
661 			u8 length, i, count;
662 			u8 type;
663 
664 			info = (void *)pos;
665 			count = u8_get_bits(info->tbtt_info_hdr,
666 					    IEEE80211_AP_INFO_TBTT_HDR_COUNT) +
667 				1;
668 			length = info->tbtt_info_len;
669 
670 			pos += sizeof(*info);
671 
672 			if (count * length > end - pos)
673 				return false;
674 
675 			type = u8_get_bits(info->tbtt_info_hdr,
676 					   IEEE80211_AP_INFO_TBTT_HDR_TYPE);
677 
678 			for (i = 0; i < count; i++) {
679 				switch (iter(iter_data, type, info,
680 					     pos, length)) {
681 				case RNR_ITER_CONTINUE:
682 					break;
683 				case RNR_ITER_BREAK:
684 					return true;
685 				case RNR_ITER_ERROR:
686 					return false;
687 				}
688 
689 				pos += length;
690 			}
691 		}
692 
693 		if (pos != end)
694 			return false;
695 	}
696 
697 	return true;
698 }
699 EXPORT_SYMBOL_GPL(cfg80211_iter_rnr);
700 
701 struct colocated_ap_data {
702 	const struct element *ssid_elem;
703 	struct list_head ap_list;
704 	u32 s_ssid_tmp;
705 	int n_coloc;
706 };
707 
708 static enum cfg80211_rnr_iter_ret
709 cfg80211_parse_colocated_ap_iter(void *_data, u8 type,
710 				 const struct ieee80211_neighbor_ap_info *info,
711 				 const u8 *tbtt_info, u8 tbtt_info_len)
712 {
713 	struct colocated_ap_data *data = _data;
714 	struct cfg80211_colocated_ap *entry;
715 	enum nl80211_band band;
716 
717 	if (type != IEEE80211_TBTT_INFO_TYPE_TBTT)
718 		return RNR_ITER_CONTINUE;
719 
720 	if (!ieee80211_operating_class_to_band(info->op_class, &band))
721 		return RNR_ITER_CONTINUE;
722 
723 	/* TBTT info must include bss param + BSSID + (short SSID or
724 	 * same_ssid bit to be set). Ignore other options, and move to
725 	 * the next AP info
726 	 */
727 	if (band != NL80211_BAND_6GHZ ||
728 	    !(tbtt_info_len == offsetofend(struct ieee80211_tbtt_info_7_8_9,
729 					   bss_params) ||
730 	      tbtt_info_len == sizeof(struct ieee80211_tbtt_info_7_8_9) ||
731 	      tbtt_info_len >= offsetofend(struct ieee80211_tbtt_info_ge_11,
732 					   bss_params)))
733 		return RNR_ITER_CONTINUE;
734 
735 	entry = kzalloc_obj(*entry, GFP_ATOMIC);
736 	if (!entry)
737 		return RNR_ITER_ERROR;
738 
739 	entry->center_freq =
740 		ieee80211_channel_to_frequency(info->channel, band);
741 
742 	if (!cfg80211_parse_ap_info(entry, tbtt_info, tbtt_info_len,
743 				    data->ssid_elem, data->s_ssid_tmp)) {
744 		struct cfg80211_colocated_ap *tmp;
745 
746 		/* Don't add duplicate BSSIDs on the same channel. */
747 		list_for_each_entry(tmp, &data->ap_list, list) {
748 			if (ether_addr_equal(tmp->bssid, entry->bssid) &&
749 			    tmp->center_freq == entry->center_freq) {
750 				kfree(entry);
751 				return RNR_ITER_CONTINUE;
752 			}
753 		}
754 
755 		data->n_coloc++;
756 		list_add_tail(&entry->list, &data->ap_list);
757 	} else {
758 		kfree(entry);
759 	}
760 
761 	return RNR_ITER_CONTINUE;
762 }
763 
764 VISIBLE_IF_CFG80211_KUNIT int
765 cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
766 			    struct list_head *list)
767 {
768 	struct colocated_ap_data data = {};
769 	int ret;
770 
771 	INIT_LIST_HEAD(&data.ap_list);
772 
773 	ret = cfg80211_calc_short_ssid(ies, &data.ssid_elem, &data.s_ssid_tmp);
774 	if (ret)
775 		return 0;
776 
777 	if (!cfg80211_iter_rnr(ies->data, ies->len,
778 			       cfg80211_parse_colocated_ap_iter, &data)) {
779 		cfg80211_free_coloc_ap_list(&data.ap_list);
780 		return 0;
781 	}
782 
783 	list_splice_tail(&data.ap_list, list);
784 	return data.n_coloc;
785 }
786 EXPORT_SYMBOL_IF_CFG80211_KUNIT(cfg80211_parse_colocated_ap);
787 
788 static void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
789 				       struct ieee80211_channel *chan,
790 				       bool add_to_6ghz)
791 {
792 	int i;
793 	u32 n_channels = request->n_channels;
794 	struct cfg80211_scan_6ghz_params *params =
795 		&request->scan_6ghz_params[request->n_6ghz_params];
796 
797 	for (i = 0; i < n_channels; i++) {
798 		if (request->channels[i] == chan) {
799 			if (add_to_6ghz)
800 				params->channel_idx = i;
801 			return;
802 		}
803 	}
804 
805 	request->n_channels++;
806 	request->channels[n_channels] = chan;
807 	if (add_to_6ghz)
808 		request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
809 			n_channels;
810 }
811 
812 static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
813 				     struct cfg80211_scan_request *request)
814 {
815 	int i;
816 	u32 s_ssid;
817 
818 	for (i = 0; i < request->n_ssids; i++) {
819 		/* wildcard ssid in the scan request */
820 		if (!request->ssids[i].ssid_len) {
821 			if (ap->multi_bss && !ap->transmitted_bssid)
822 				continue;
823 
824 			return true;
825 		}
826 
827 		if (ap->ssid_len &&
828 		    ap->ssid_len == request->ssids[i].ssid_len) {
829 			if (!memcmp(request->ssids[i].ssid, ap->ssid,
830 				    ap->ssid_len))
831 				return true;
832 		} else if (ap->short_ssid_valid) {
833 			s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
834 					   request->ssids[i].ssid_len);
835 
836 			if (ap->short_ssid == s_ssid)
837 				return true;
838 		}
839 	}
840 
841 	return false;
842 }
843 
844 static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev,
845 			      bool first_part)
846 {
847 	u8 i;
848 	struct cfg80211_colocated_ap *ap;
849 	int n_channels, count = 0, err;
850 	struct cfg80211_scan_request_int *request, *rdev_req = rdev->scan_req;
851 	LIST_HEAD(coloc_ap_list);
852 	bool need_scan_psc = true;
853 	const struct ieee80211_sband_iftype_data *iftd;
854 	size_t size, offs_ssids, offs_6ghz_params, offs_ies;
855 
856 	rdev_req->req.scan_6ghz = true;
857 	rdev_req->req.first_part = first_part;
858 
859 	if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
860 		return -EOPNOTSUPP;
861 
862 	iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
863 					       rdev_req->req.wdev->iftype);
864 	if (!iftd || !iftd->he_cap.has_he)
865 		return -EOPNOTSUPP;
866 
867 	n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
868 
869 	if (rdev_req->req.flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
870 		struct cfg80211_internal_bss *intbss;
871 
872 		spin_lock_bh(&rdev->bss_lock);
873 		list_for_each_entry(intbss, &rdev->bss_list, list) {
874 			struct cfg80211_bss *res = &intbss->pub;
875 			const struct cfg80211_bss_ies *ies;
876 			const struct element *ssid_elem;
877 			struct cfg80211_colocated_ap *entry;
878 			u32 s_ssid_tmp;
879 			int ret;
880 
881 			ies = rcu_access_pointer(res->ies);
882 			count += cfg80211_parse_colocated_ap(ies,
883 							     &coloc_ap_list);
884 
885 			/* In case the scan request specified a specific BSSID
886 			 * and the BSS is found and operating on 6GHz band then
887 			 * add this AP to the collocated APs list.
888 			 * This is relevant for ML probe requests when the lower
889 			 * band APs have not been discovered.
890 			 */
891 			if (is_broadcast_ether_addr(rdev_req->req.bssid) ||
892 			    !ether_addr_equal(rdev_req->req.bssid, res->bssid) ||
893 			    res->channel->band != NL80211_BAND_6GHZ)
894 				continue;
895 
896 			ret = cfg80211_calc_short_ssid(ies, &ssid_elem,
897 						       &s_ssid_tmp);
898 			if (ret)
899 				continue;
900 
901 			entry = kzalloc_obj(*entry, GFP_ATOMIC);
902 			if (!entry)
903 				continue;
904 
905 			memcpy(entry->bssid, res->bssid, ETH_ALEN);
906 			entry->short_ssid = s_ssid_tmp;
907 			memcpy(entry->ssid, ssid_elem->data,
908 			       ssid_elem->datalen);
909 			entry->ssid_len = ssid_elem->datalen;
910 			entry->short_ssid_valid = true;
911 			entry->center_freq = res->channel->center_freq;
912 
913 			list_add_tail(&entry->list, &coloc_ap_list);
914 			count++;
915 		}
916 		spin_unlock_bh(&rdev->bss_lock);
917 	}
918 
919 	size = struct_size(request, req.channels, n_channels);
920 	offs_ssids = size;
921 	size += sizeof(*request->req.ssids) * rdev_req->req.n_ssids;
922 	offs_6ghz_params = size;
923 	size += sizeof(*request->req.scan_6ghz_params) * count;
924 	offs_ies = size;
925 	size += rdev_req->req.ie_len;
926 
927 	request = kzalloc(size, GFP_KERNEL);
928 	if (!request) {
929 		cfg80211_free_coloc_ap_list(&coloc_ap_list);
930 		return -ENOMEM;
931 	}
932 
933 	*request = *rdev_req;
934 	request->req.n_channels = 0;
935 	request->req.n_6ghz_params = 0;
936 	if (rdev_req->req.n_ssids) {
937 		/*
938 		 * Add the ssids from the parent scan request to the new
939 		 * scan request, so the driver would be able to use them
940 		 * in its probe requests to discover hidden APs on PSC
941 		 * channels.
942 		 */
943 		request->req.ssids = (void *)request + offs_ssids;
944 		memcpy(request->req.ssids, rdev_req->req.ssids,
945 		       sizeof(*request->req.ssids) * request->req.n_ssids);
946 	}
947 	request->req.scan_6ghz_params = (void *)request + offs_6ghz_params;
948 
949 	if (rdev_req->req.ie_len) {
950 		void *ie = (void *)request + offs_ies;
951 
952 		memcpy(ie, rdev_req->req.ie, rdev_req->req.ie_len);
953 		request->req.ie = ie;
954 	}
955 
956 	/*
957 	 * PSC channels should not be scanned in case of direct scan with 1 SSID
958 	 * and at least one of the reported co-located APs with same SSID
959 	 * indicating that all APs in the same ESS are co-located
960 	 */
961 	if (count &&
962 	    request->req.n_ssids == 1 &&
963 	    request->req.ssids[0].ssid_len) {
964 		list_for_each_entry(ap, &coloc_ap_list, list) {
965 			if (ap->colocated_ess &&
966 			    cfg80211_find_ssid_match(ap, &request->req)) {
967 				need_scan_psc = false;
968 				break;
969 			}
970 		}
971 	}
972 
973 	/*
974 	 * add to the scan request the channels that need to be scanned
975 	 * regardless of the collocated APs (PSC channels or all channels
976 	 * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
977 	 */
978 	for (i = 0; i < rdev_req->req.n_channels; i++) {
979 		if (rdev_req->req.channels[i]->band == NL80211_BAND_6GHZ &&
980 		    ((need_scan_psc &&
981 		      cfg80211_channel_is_psc(rdev_req->req.channels[i])) ||
982 		     !(rdev_req->req.flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
983 			cfg80211_scan_req_add_chan(&request->req,
984 						   rdev_req->req.channels[i],
985 						   false);
986 		}
987 	}
988 
989 	if (!(rdev_req->req.flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
990 		goto skip;
991 
992 	list_for_each_entry(ap, &coloc_ap_list, list) {
993 		bool found = false;
994 		struct cfg80211_scan_6ghz_params *scan_6ghz_params =
995 			&request->req.scan_6ghz_params[request->req.n_6ghz_params];
996 		struct ieee80211_channel *chan =
997 			ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
998 
999 		if (!chan || chan->flags & IEEE80211_CHAN_DISABLED ||
1000 		    !cfg80211_wdev_channel_allowed(rdev_req->req.wdev, chan))
1001 			continue;
1002 
1003 		for (i = 0; i < rdev_req->req.n_channels; i++) {
1004 			if (rdev_req->req.channels[i] == chan)
1005 				found = true;
1006 		}
1007 
1008 		if (!found)
1009 			continue;
1010 
1011 		if (request->req.n_ssids > 0 &&
1012 		    !cfg80211_find_ssid_match(ap, &request->req))
1013 			continue;
1014 
1015 		if (!is_broadcast_ether_addr(request->req.bssid) &&
1016 		    !ether_addr_equal(request->req.bssid, ap->bssid))
1017 			continue;
1018 
1019 		if (!request->req.n_ssids && ap->multi_bss &&
1020 		    !ap->transmitted_bssid)
1021 			continue;
1022 
1023 		cfg80211_scan_req_add_chan(&request->req, chan, true);
1024 		memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
1025 		scan_6ghz_params->short_ssid = ap->short_ssid;
1026 		scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
1027 		scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
1028 		scan_6ghz_params->psd_20 = ap->psd_20;
1029 
1030 		/*
1031 		 * If a PSC channel is added to the scan and 'need_scan_psc' is
1032 		 * set to false, then all the APs that the scan logic is
1033 		 * interested with on the channel are collocated and thus there
1034 		 * is no need to perform the initial PSC channel listen.
1035 		 */
1036 		if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
1037 			scan_6ghz_params->psc_no_listen = true;
1038 
1039 		request->req.n_6ghz_params++;
1040 	}
1041 
1042 skip:
1043 	cfg80211_free_coloc_ap_list(&coloc_ap_list);
1044 
1045 	if (request->req.n_channels) {
1046 		struct cfg80211_scan_request_int *old = rdev->int_scan_req;
1047 
1048 		rdev->int_scan_req = request;
1049 
1050 		/*
1051 		 * If this scan follows a previous scan, save the scan start
1052 		 * info from the first part of the scan
1053 		 */
1054 		if (!first_part && !WARN_ON(!old))
1055 			rdev->int_scan_req->info = old->info;
1056 
1057 		err = rdev_scan(rdev, request);
1058 		if (err) {
1059 			rdev->int_scan_req = old;
1060 			kfree(request);
1061 		} else {
1062 			kfree(old);
1063 		}
1064 
1065 		return err;
1066 	}
1067 
1068 	kfree(request);
1069 	return -EINVAL;
1070 }
1071 
1072 int cfg80211_scan(struct cfg80211_registered_device *rdev)
1073 {
1074 	struct cfg80211_scan_request_int *request;
1075 	struct cfg80211_scan_request_int *rdev_req = rdev->scan_req;
1076 	u32 n_channels = 0, idx, i;
1077 	int err;
1078 
1079 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ)) {
1080 		rdev_req->req.first_part = true;
1081 		return rdev_scan(rdev, rdev_req);
1082 	}
1083 
1084 	for (i = 0; i < rdev_req->req.n_channels; i++) {
1085 		if (rdev_req->req.channels[i]->band != NL80211_BAND_6GHZ)
1086 			n_channels++;
1087 	}
1088 
1089 	if (!n_channels)
1090 		return cfg80211_scan_6ghz(rdev, true);
1091 
1092 	request = kzalloc_flex(*request, req.channels, n_channels);
1093 	if (!request)
1094 		return -ENOMEM;
1095 
1096 	*request = *rdev_req;
1097 	request->req.n_channels = n_channels;
1098 
1099 	for (i = idx = 0; i < rdev_req->req.n_channels; i++) {
1100 		if (rdev_req->req.channels[i]->band != NL80211_BAND_6GHZ)
1101 			request->req.channels[idx++] =
1102 				rdev_req->req.channels[i];
1103 	}
1104 
1105 	rdev_req->req.scan_6ghz = false;
1106 	rdev_req->req.first_part = true;
1107 	err = rdev_scan(rdev, request);
1108 	if (err) {
1109 		kfree(request);
1110 		return err;
1111 	}
1112 
1113 	rdev->int_scan_req = request;
1114 	return 0;
1115 }
1116 
1117 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
1118 			   bool send_message)
1119 {
1120 	struct cfg80211_scan_request_int *request, *rdev_req;
1121 	struct wireless_dev *wdev;
1122 	struct sk_buff *msg;
1123 #ifdef CONFIG_CFG80211_WEXT
1124 	union iwreq_data wrqu;
1125 #endif
1126 
1127 	lockdep_assert_held(&rdev->wiphy.mtx);
1128 
1129 	if (rdev->scan_msg) {
1130 		nl80211_send_scan_msg(rdev, rdev->scan_msg);
1131 		rdev->scan_msg = NULL;
1132 		return;
1133 	}
1134 
1135 	rdev_req = rdev->scan_req;
1136 	if (!rdev_req)
1137 		return;
1138 
1139 	wdev = rdev_req->req.wdev;
1140 	request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
1141 
1142 	if (wdev_running(wdev) &&
1143 	    (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
1144 	    !rdev_req->req.scan_6ghz && !request->info.aborted &&
1145 	    !cfg80211_scan_6ghz(rdev, false))
1146 		return;
1147 
1148 	/*
1149 	 * This must be before sending the other events!
1150 	 * Otherwise, wpa_supplicant gets completely confused with
1151 	 * wext events.
1152 	 */
1153 	if (wdev->netdev)
1154 		cfg80211_sme_scan_done(wdev->netdev);
1155 
1156 	if (!request->info.aborted &&
1157 	    request->req.flags & NL80211_SCAN_FLAG_FLUSH) {
1158 		/* flush entries from previous scans */
1159 		spin_lock_bh(&rdev->bss_lock);
1160 		__cfg80211_bss_expire(rdev, request->req.scan_start);
1161 		spin_unlock_bh(&rdev->bss_lock);
1162 	}
1163 
1164 	msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
1165 
1166 #ifdef CONFIG_CFG80211_WEXT
1167 	if (wdev->netdev && !request->info.aborted) {
1168 		memset(&wrqu, 0, sizeof(wrqu));
1169 
1170 		wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
1171 	}
1172 #endif
1173 
1174 	dev_put(wdev->netdev);
1175 
1176 	kfree(rdev->int_scan_req);
1177 	rdev->int_scan_req = NULL;
1178 
1179 	kfree(rdev->scan_req);
1180 	rdev->scan_req = NULL;
1181 
1182 	if (!send_message)
1183 		rdev->scan_msg = msg;
1184 	else
1185 		nl80211_send_scan_msg(rdev, msg);
1186 }
1187 
1188 void __cfg80211_scan_done(struct wiphy *wiphy, struct wiphy_work *wk)
1189 {
1190 	___cfg80211_scan_done(wiphy_to_rdev(wiphy), true);
1191 }
1192 
1193 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1194 			struct cfg80211_scan_info *info)
1195 {
1196 	struct cfg80211_scan_request_int *intreq =
1197 		container_of(request, struct cfg80211_scan_request_int, req);
1198 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(request->wiphy);
1199 	struct cfg80211_scan_info old_info = intreq->info;
1200 
1201 	trace_cfg80211_scan_done(intreq, info);
1202 	WARN_ON(intreq != rdev->scan_req &&
1203 		intreq != rdev->int_scan_req);
1204 
1205 	intreq->info = *info;
1206 
1207 	/*
1208 	 * In case the scan is split, the scan_start_tsf and tsf_bssid should
1209 	 * be of the first part. In such a case old_info.scan_start_tsf should
1210 	 * be non zero.
1211 	 */
1212 	if (request->scan_6ghz && old_info.scan_start_tsf) {
1213 		intreq->info.scan_start_tsf = old_info.scan_start_tsf;
1214 		memcpy(intreq->info.tsf_bssid, old_info.tsf_bssid,
1215 		       sizeof(intreq->info.tsf_bssid));
1216 	}
1217 
1218 	intreq->notified = true;
1219 	wiphy_work_queue(request->wiphy, &rdev->scan_done_wk);
1220 }
1221 EXPORT_SYMBOL(cfg80211_scan_done);
1222 
1223 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1224 				 struct cfg80211_sched_scan_request *req)
1225 {
1226 	lockdep_assert_held(&rdev->wiphy.mtx);
1227 
1228 	list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1229 }
1230 
1231 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1232 					struct cfg80211_sched_scan_request *req)
1233 {
1234 	lockdep_assert_held(&rdev->wiphy.mtx);
1235 
1236 	list_del_rcu(&req->list);
1237 	kfree_rcu(req, rcu_head);
1238 }
1239 
1240 static struct cfg80211_sched_scan_request *
1241 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1242 {
1243 	struct cfg80211_sched_scan_request *pos;
1244 
1245 	list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1246 				lockdep_is_held(&rdev->wiphy.mtx)) {
1247 		if (pos->reqid == reqid)
1248 			return pos;
1249 	}
1250 	return NULL;
1251 }
1252 
1253 /*
1254  * Determines if a scheduled scan request can be handled. When a legacy
1255  * scheduled scan is running no other scheduled scan is allowed regardless
1256  * whether the request is for legacy or multi-support scan. When a multi-support
1257  * scheduled scan is running a request for legacy scan is not allowed. In this
1258  * case a request for multi-support scan can be handled if resources are
1259  * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1260  */
1261 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1262 				     bool want_multi)
1263 {
1264 	struct cfg80211_sched_scan_request *pos;
1265 	int i = 0;
1266 
1267 	list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1268 		/* request id zero means legacy in progress */
1269 		if (!i && !pos->reqid)
1270 			return -EINPROGRESS;
1271 		i++;
1272 	}
1273 
1274 	if (i) {
1275 		/* no legacy allowed when multi request(s) are active */
1276 		if (!want_multi)
1277 			return -EINPROGRESS;
1278 
1279 		/* resource limit reached */
1280 		if (i == rdev->wiphy.max_sched_scan_reqs)
1281 			return -ENOSPC;
1282 	}
1283 	return 0;
1284 }
1285 
1286 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1287 {
1288 	struct cfg80211_registered_device *rdev;
1289 	struct cfg80211_sched_scan_request *req, *tmp;
1290 
1291 	rdev = container_of(work, struct cfg80211_registered_device,
1292 			   sched_scan_res_wk);
1293 
1294 	guard(wiphy)(&rdev->wiphy);
1295 
1296 	list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1297 		if (req->report_results) {
1298 			req->report_results = false;
1299 			if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1300 				/* flush entries from previous scans */
1301 				spin_lock_bh(&rdev->bss_lock);
1302 				__cfg80211_bss_expire(rdev, req->scan_start);
1303 				spin_unlock_bh(&rdev->bss_lock);
1304 				req->scan_start = jiffies;
1305 			}
1306 			nl80211_send_sched_scan(req,
1307 						NL80211_CMD_SCHED_SCAN_RESULTS);
1308 		}
1309 	}
1310 }
1311 
1312 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1313 {
1314 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1315 	struct cfg80211_sched_scan_request *request;
1316 
1317 	trace_cfg80211_sched_scan_results(wiphy, reqid);
1318 	/* ignore if we're not scanning */
1319 
1320 	rcu_read_lock();
1321 	request = cfg80211_find_sched_scan_req(rdev, reqid);
1322 	if (request) {
1323 		request->report_results = true;
1324 		queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1325 	}
1326 	rcu_read_unlock();
1327 }
1328 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1329 
1330 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1331 {
1332 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1333 
1334 	lockdep_assert_held(&wiphy->mtx);
1335 
1336 	trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1337 
1338 	__cfg80211_stop_sched_scan(rdev, reqid, true);
1339 }
1340 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1341 
1342 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1343 {
1344 	guard(wiphy)(wiphy);
1345 
1346 	cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1347 }
1348 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1349 
1350 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1351 				 struct cfg80211_sched_scan_request *req,
1352 				 bool driver_initiated)
1353 {
1354 	lockdep_assert_held(&rdev->wiphy.mtx);
1355 
1356 	if (!driver_initiated) {
1357 		int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1358 		if (err)
1359 			return err;
1360 	}
1361 
1362 	nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1363 
1364 	cfg80211_del_sched_scan_req(rdev, req);
1365 
1366 	return 0;
1367 }
1368 
1369 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1370 			       u64 reqid, bool driver_initiated)
1371 {
1372 	struct cfg80211_sched_scan_request *sched_scan_req;
1373 
1374 	lockdep_assert_held(&rdev->wiphy.mtx);
1375 
1376 	sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1377 	if (!sched_scan_req)
1378 		return -ENOENT;
1379 
1380 	return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1381 					    driver_initiated);
1382 }
1383 
1384 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1385                       unsigned long age_secs)
1386 {
1387 	struct cfg80211_internal_bss *bss;
1388 	unsigned long age_jiffies = secs_to_jiffies(age_secs);
1389 
1390 	spin_lock_bh(&rdev->bss_lock);
1391 	list_for_each_entry(bss, &rdev->bss_list, list)
1392 		bss->ts -= age_jiffies;
1393 	spin_unlock_bh(&rdev->bss_lock);
1394 }
1395 
1396 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1397 {
1398 	__cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1399 }
1400 
1401 void cfg80211_bss_flush(struct wiphy *wiphy)
1402 {
1403 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1404 
1405 	spin_lock_bh(&rdev->bss_lock);
1406 	__cfg80211_bss_expire(rdev, jiffies);
1407 	spin_unlock_bh(&rdev->bss_lock);
1408 }
1409 EXPORT_SYMBOL(cfg80211_bss_flush);
1410 
1411 const struct element *
1412 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1413 			 const u8 *match, unsigned int match_len,
1414 			 unsigned int match_offset)
1415 {
1416 	const struct element *elem;
1417 
1418 	for_each_element_id(elem, eid, ies, len) {
1419 		if (elem->datalen >= match_offset + match_len &&
1420 		    !memcmp(elem->data + match_offset, match, match_len))
1421 			return elem;
1422 	}
1423 
1424 	return NULL;
1425 }
1426 EXPORT_SYMBOL(cfg80211_find_elem_match);
1427 
1428 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1429 						const u8 *ies,
1430 						unsigned int len)
1431 {
1432 	const struct element *elem;
1433 	u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1434 	int match_len = (oui_type < 0) ? 3 : sizeof(match);
1435 
1436 	if (WARN_ON(oui_type > 0xff))
1437 		return NULL;
1438 
1439 	elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1440 					match, match_len, 0);
1441 
1442 	if (!elem || elem->datalen < 4)
1443 		return NULL;
1444 
1445 	return elem;
1446 }
1447 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1448 
1449 /**
1450  * enum bss_compare_mode - BSS compare mode
1451  * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1452  * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1453  * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1454  */
1455 enum bss_compare_mode {
1456 	BSS_CMP_REGULAR,
1457 	BSS_CMP_HIDE_ZLEN,
1458 	BSS_CMP_HIDE_NUL,
1459 };
1460 
1461 static int cmp_bss(struct cfg80211_bss *a,
1462 		   struct cfg80211_bss *b,
1463 		   enum bss_compare_mode mode)
1464 {
1465 	const struct cfg80211_bss_ies *a_ies, *b_ies;
1466 	const u8 *ie1 = NULL;
1467 	const u8 *ie2 = NULL;
1468 	int i, r;
1469 
1470 	if (a->channel != b->channel)
1471 		return (b->channel->center_freq * 1000 + b->channel->freq_offset) -
1472 		       (a->channel->center_freq * 1000 + a->channel->freq_offset);
1473 
1474 	a_ies = rcu_access_pointer(a->ies);
1475 	if (!a_ies)
1476 		return -1;
1477 	b_ies = rcu_access_pointer(b->ies);
1478 	if (!b_ies)
1479 		return 1;
1480 
1481 	if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1482 		ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1483 				       a_ies->data, a_ies->len);
1484 	if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1485 		ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1486 				       b_ies->data, b_ies->len);
1487 	if (ie1 && ie2) {
1488 		int mesh_id_cmp;
1489 
1490 		if (ie1[1] == ie2[1])
1491 			mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1492 		else
1493 			mesh_id_cmp = ie2[1] - ie1[1];
1494 
1495 		ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1496 				       a_ies->data, a_ies->len);
1497 		ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1498 				       b_ies->data, b_ies->len);
1499 		if (ie1 && ie2) {
1500 			if (mesh_id_cmp)
1501 				return mesh_id_cmp;
1502 			if (ie1[1] != ie2[1])
1503 				return ie2[1] - ie1[1];
1504 			return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1505 		}
1506 	}
1507 
1508 	r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1509 	if (r)
1510 		return r;
1511 
1512 	ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1513 	ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1514 
1515 	if (!ie1 && !ie2)
1516 		return 0;
1517 
1518 	/*
1519 	 * Note that with "hide_ssid", the function returns a match if
1520 	 * the already-present BSS ("b") is a hidden SSID beacon for
1521 	 * the new BSS ("a").
1522 	 */
1523 
1524 	/* sort missing IE before (left of) present IE */
1525 	if (!ie1)
1526 		return -1;
1527 	if (!ie2)
1528 		return 1;
1529 
1530 	switch (mode) {
1531 	case BSS_CMP_HIDE_ZLEN:
1532 		/*
1533 		 * In ZLEN mode we assume the BSS entry we're
1534 		 * looking for has a zero-length SSID. So if
1535 		 * the one we're looking at right now has that,
1536 		 * return 0. Otherwise, return the difference
1537 		 * in length, but since we're looking for the
1538 		 * 0-length it's really equivalent to returning
1539 		 * the length of the one we're looking at.
1540 		 *
1541 		 * No content comparison is needed as we assume
1542 		 * the content length is zero.
1543 		 */
1544 		return ie2[1];
1545 	case BSS_CMP_REGULAR:
1546 	default:
1547 		/* sort by length first, then by contents */
1548 		if (ie1[1] != ie2[1])
1549 			return ie2[1] - ie1[1];
1550 		return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1551 	case BSS_CMP_HIDE_NUL:
1552 		if (ie1[1] != ie2[1])
1553 			return ie2[1] - ie1[1];
1554 		/* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1555 		for (i = 0; i < ie2[1]; i++)
1556 			if (ie2[i + 2])
1557 				return -1;
1558 		return 0;
1559 	}
1560 }
1561 
1562 static bool cfg80211_bss_type_match(u16 capability,
1563 				    enum nl80211_band band,
1564 				    enum ieee80211_bss_type bss_type)
1565 {
1566 	bool ret = true;
1567 	u16 mask, val;
1568 
1569 	if (bss_type == IEEE80211_BSS_TYPE_ANY)
1570 		return ret;
1571 
1572 	if (band == NL80211_BAND_60GHZ) {
1573 		mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1574 		switch (bss_type) {
1575 		case IEEE80211_BSS_TYPE_ESS:
1576 			val = WLAN_CAPABILITY_DMG_TYPE_AP;
1577 			break;
1578 		case IEEE80211_BSS_TYPE_PBSS:
1579 			val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1580 			break;
1581 		case IEEE80211_BSS_TYPE_IBSS:
1582 			val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1583 			break;
1584 		default:
1585 			return false;
1586 		}
1587 	} else {
1588 		mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1589 		switch (bss_type) {
1590 		case IEEE80211_BSS_TYPE_ESS:
1591 			val = WLAN_CAPABILITY_ESS;
1592 			break;
1593 		case IEEE80211_BSS_TYPE_IBSS:
1594 			val = WLAN_CAPABILITY_IBSS;
1595 			break;
1596 		case IEEE80211_BSS_TYPE_MBSS:
1597 			val = 0;
1598 			break;
1599 		default:
1600 			return false;
1601 		}
1602 	}
1603 
1604 	ret = ((capability & mask) == val);
1605 	return ret;
1606 }
1607 
1608 /* Returned bss is reference counted and must be cleaned up appropriately. */
1609 struct cfg80211_bss *__cfg80211_get_bss(struct wiphy *wiphy,
1610 					struct ieee80211_channel *channel,
1611 					const u8 *bssid,
1612 					const u8 *ssid, size_t ssid_len,
1613 					enum ieee80211_bss_type bss_type,
1614 					enum ieee80211_privacy privacy,
1615 					u32 use_for,
1616 					struct netlink_ext_ack *extack)
1617 {
1618 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1619 	struct cfg80211_internal_bss *bss, *res = NULL;
1620 	bool expired = false, unusable = false;
1621 	unsigned long now = jiffies;
1622 	int bss_privacy;
1623 
1624 	trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1625 			       privacy);
1626 
1627 	spin_lock_bh(&rdev->bss_lock);
1628 
1629 	list_for_each_entry(bss, &rdev->bss_list, list) {
1630 		if (!cfg80211_bss_type_match(bss->pub.capability,
1631 					     bss->pub.channel->band, bss_type))
1632 			continue;
1633 
1634 		bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1635 		if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1636 		    (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1637 			continue;
1638 		if (channel && bss->pub.channel != channel)
1639 			continue;
1640 		if (!is_valid_ether_addr(bss->pub.bssid))
1641 			continue;
1642 		if (!is_bss(&bss->pub, bssid, ssid, ssid_len))
1643 			continue;
1644 
1645 		/*
1646 		 * The identity checks above must all come first so that
1647 		 * the expired/unusable classification below only ever
1648 		 * applies to entries that actually match the request.
1649 		 */
1650 
1651 		/* Don't get expired BSS structs */
1652 		if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1653 		    !atomic_read(&bss->hold)) {
1654 			expired = true;
1655 			continue;
1656 		}
1657 
1658 		if ((bss->pub.use_for & use_for) != use_for) {
1659 			unusable = true;
1660 			continue;
1661 		}
1662 
1663 		res = bss;
1664 		bss_ref_get(rdev, res);
1665 		break;
1666 	}
1667 
1668 	spin_unlock_bh(&rdev->bss_lock);
1669 	if (!res) {
1670 		if (expired && unusable)
1671 			NL_SET_ERR_MSG(extack,
1672 				       "BSS entries are expired or cannot be used for the requested operation");
1673 		else if (unusable)
1674 			NL_SET_ERR_MSG(extack,
1675 				       "BSS cannot be used for the requested operation");
1676 		else if (expired)
1677 			NL_SET_ERR_MSG(extack,
1678 				       "BSS entry in scan results is expired");
1679 		else
1680 			NL_SET_ERR_MSG(extack,
1681 				       "BSS not found in scan results");
1682 		return NULL;
1683 	}
1684 	trace_cfg80211_return_bss(&res->pub);
1685 	return &res->pub;
1686 }
1687 EXPORT_SYMBOL(__cfg80211_get_bss);
1688 
1689 static bool rb_insert_bss(struct cfg80211_registered_device *rdev,
1690 			  struct cfg80211_internal_bss *bss)
1691 {
1692 	struct rb_node **p = &rdev->bss_tree.rb_node;
1693 	struct rb_node *parent = NULL;
1694 	struct cfg80211_internal_bss *tbss;
1695 	int cmp;
1696 
1697 	while (*p) {
1698 		parent = *p;
1699 		tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1700 
1701 		cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1702 
1703 		if (WARN_ON(!cmp)) {
1704 			/* will sort of leak this BSS */
1705 			return false;
1706 		}
1707 
1708 		if (cmp < 0)
1709 			p = &(*p)->rb_left;
1710 		else
1711 			p = &(*p)->rb_right;
1712 	}
1713 
1714 	rb_link_node(&bss->rbn, parent, p);
1715 	rb_insert_color(&bss->rbn, &rdev->bss_tree);
1716 	return true;
1717 }
1718 
1719 static struct cfg80211_internal_bss *
1720 rb_find_bss(struct cfg80211_registered_device *rdev,
1721 	    struct cfg80211_internal_bss *res,
1722 	    enum bss_compare_mode mode)
1723 {
1724 	struct rb_node *n = rdev->bss_tree.rb_node;
1725 	struct cfg80211_internal_bss *bss;
1726 	int r;
1727 
1728 	while (n) {
1729 		bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1730 		r = cmp_bss(&res->pub, &bss->pub, mode);
1731 
1732 		if (r == 0)
1733 			return bss;
1734 		else if (r < 0)
1735 			n = n->rb_left;
1736 		else
1737 			n = n->rb_right;
1738 	}
1739 
1740 	return NULL;
1741 }
1742 
1743 static void cfg80211_insert_bss(struct cfg80211_registered_device *rdev,
1744 				struct cfg80211_internal_bss *bss)
1745 {
1746 	lockdep_assert_held(&rdev->bss_lock);
1747 
1748 	if (!rb_insert_bss(rdev, bss))
1749 		return;
1750 	list_add_tail(&bss->list, &rdev->bss_list);
1751 	rdev->bss_entries++;
1752 }
1753 
1754 static void cfg80211_rehash_bss(struct cfg80211_registered_device *rdev,
1755                                 struct cfg80211_internal_bss *bss)
1756 {
1757 	lockdep_assert_held(&rdev->bss_lock);
1758 
1759 	rb_erase(&bss->rbn, &rdev->bss_tree);
1760 	if (!rb_insert_bss(rdev, bss)) {
1761 		list_del(&bss->list);
1762 		if (!list_empty(&bss->hidden_list))
1763 			list_del_init(&bss->hidden_list);
1764 		if (!list_empty(&bss->pub.nontrans_list))
1765 			list_del_init(&bss->pub.nontrans_list);
1766 		rdev->bss_entries--;
1767 	}
1768 	rdev->bss_generation++;
1769 }
1770 
1771 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1772 				   struct cfg80211_internal_bss *new)
1773 {
1774 	const struct cfg80211_bss_ies *ies;
1775 	struct cfg80211_internal_bss *bss;
1776 	const u8 *ie;
1777 	int i, ssidlen;
1778 	u8 fold = 0;
1779 	u32 n_entries = 0;
1780 
1781 	ies = rcu_access_pointer(new->pub.beacon_ies);
1782 	if (WARN_ON(!ies))
1783 		return false;
1784 
1785 	ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1786 	if (!ie) {
1787 		/* nothing to do */
1788 		return true;
1789 	}
1790 
1791 	ssidlen = ie[1];
1792 	for (i = 0; i < ssidlen; i++)
1793 		fold |= ie[2 + i];
1794 
1795 	if (fold) {
1796 		/* not a hidden SSID */
1797 		return true;
1798 	}
1799 
1800 	/* This is the bad part ... */
1801 
1802 	list_for_each_entry(bss, &rdev->bss_list, list) {
1803 		/*
1804 		 * we're iterating all the entries anyway, so take the
1805 		 * opportunity to validate the list length accounting
1806 		 */
1807 		n_entries++;
1808 
1809 		if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1810 			continue;
1811 		if (bss->pub.channel != new->pub.channel)
1812 			continue;
1813 		if (rcu_access_pointer(bss->pub.beacon_ies))
1814 			continue;
1815 		ies = rcu_access_pointer(bss->pub.ies);
1816 		if (!ies)
1817 			continue;
1818 		ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1819 		if (!ie)
1820 			continue;
1821 		if (ssidlen && ie[1] != ssidlen)
1822 			continue;
1823 		if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1824 			continue;
1825 		if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1826 			list_del(&bss->hidden_list);
1827 		/* combine them */
1828 		list_add(&bss->hidden_list, &new->hidden_list);
1829 		bss->pub.hidden_beacon_bss = &new->pub;
1830 		new->refcount += bss->refcount;
1831 		rcu_assign_pointer(bss->pub.beacon_ies,
1832 				   new->pub.beacon_ies);
1833 	}
1834 
1835 	WARN_ONCE(n_entries != rdev->bss_entries,
1836 		  "rdev bss entries[%d]/list[len:%d] corruption\n",
1837 		  rdev->bss_entries, n_entries);
1838 
1839 	return true;
1840 }
1841 
1842 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1843 					 const struct cfg80211_bss_ies *new_ies,
1844 					 const struct cfg80211_bss_ies *old_ies)
1845 {
1846 	struct cfg80211_internal_bss *bss;
1847 
1848 	/* Assign beacon IEs to all sub entries */
1849 	list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1850 		const struct cfg80211_bss_ies *ies;
1851 
1852 		ies = rcu_access_pointer(bss->pub.beacon_ies);
1853 		WARN_ON(ies != old_ies);
1854 
1855 		rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1856 
1857 		bss->ts = known->ts;
1858 		bss->pub.ts_boottime = known->pub.ts_boottime;
1859 	}
1860 }
1861 
1862 static void cfg80211_check_stuck_ecsa(struct cfg80211_registered_device *rdev,
1863 				      struct cfg80211_internal_bss *known,
1864 				      const struct cfg80211_bss_ies *old)
1865 {
1866 	const struct ieee80211_ext_chansw_ie *ecsa;
1867 	const struct element *elem_new, *elem_old;
1868 	const struct cfg80211_bss_ies *new, *bcn;
1869 
1870 	if (known->pub.proberesp_ecsa_stuck)
1871 		return;
1872 
1873 	new = rcu_dereference_protected(known->pub.proberesp_ies,
1874 					lockdep_is_held(&rdev->bss_lock));
1875 	if (WARN_ON(!new))
1876 		return;
1877 
1878 	if (new->tsf - old->tsf < USEC_PER_SEC)
1879 		return;
1880 
1881 	elem_old = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
1882 				      old->data, old->len);
1883 	if (!elem_old)
1884 		return;
1885 
1886 	elem_new = cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
1887 				      new->data, new->len);
1888 	if (!elem_new)
1889 		return;
1890 
1891 	bcn = rcu_dereference_protected(known->pub.beacon_ies,
1892 					lockdep_is_held(&rdev->bss_lock));
1893 	if (bcn &&
1894 	    cfg80211_find_elem(WLAN_EID_EXT_CHANSWITCH_ANN,
1895 			       bcn->data, bcn->len))
1896 		return;
1897 
1898 	if (elem_new->datalen != elem_old->datalen)
1899 		return;
1900 	if (elem_new->datalen < sizeof(struct ieee80211_ext_chansw_ie))
1901 		return;
1902 	if (memcmp(elem_new->data, elem_old->data, elem_new->datalen))
1903 		return;
1904 
1905 	ecsa = (void *)elem_new->data;
1906 
1907 	if (!ecsa->mode)
1908 		return;
1909 
1910 	if (ecsa->new_ch_num !=
1911 	    ieee80211_frequency_to_channel(known->pub.channel->center_freq))
1912 		return;
1913 
1914 	known->pub.proberesp_ecsa_stuck = 1;
1915 }
1916 
1917 static bool
1918 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1919 			  struct cfg80211_internal_bss *known,
1920 			  struct cfg80211_internal_bss *new,
1921 			  bool signal_valid)
1922 {
1923 	lockdep_assert_held(&rdev->bss_lock);
1924 
1925 	/* Update time stamps */
1926 	known->ts = new->ts;
1927 	known->pub.ts_boottime = new->pub.ts_boottime;
1928 
1929 	/* Update IEs */
1930 	if (rcu_access_pointer(new->pub.proberesp_ies)) {
1931 		const struct cfg80211_bss_ies *old;
1932 
1933 		old = rcu_access_pointer(known->pub.proberesp_ies);
1934 
1935 		rcu_assign_pointer(known->pub.proberesp_ies,
1936 				   new->pub.proberesp_ies);
1937 		/* Override possible earlier Beacon frame IEs */
1938 		rcu_assign_pointer(known->pub.ies,
1939 				   new->pub.proberesp_ies);
1940 		if (old) {
1941 			cfg80211_check_stuck_ecsa(rdev, known, old);
1942 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1943 		}
1944 	}
1945 
1946 	if (rcu_access_pointer(new->pub.beacon_ies)) {
1947 		const struct cfg80211_bss_ies *old;
1948 
1949 		if (known->pub.hidden_beacon_bss &&
1950 		    !list_empty(&known->hidden_list)) {
1951 			const struct cfg80211_bss_ies *f;
1952 
1953 			/* The known BSS struct is one of the probe
1954 			 * response members of a group, but we're
1955 			 * receiving a beacon (beacon_ies in the new
1956 			 * bss is used). This can only mean that the
1957 			 * AP changed its beacon from not having an
1958 			 * SSID to showing it, which is confusing so
1959 			 * drop this information.
1960 			 */
1961 
1962 			f = rcu_access_pointer(new->pub.beacon_ies);
1963 			if (!new->pub.hidden_beacon_bss)
1964 				kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1965 			return false;
1966 		}
1967 
1968 		old = rcu_access_pointer(known->pub.beacon_ies);
1969 
1970 		rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1971 
1972 		/* Override IEs if they were from a beacon before */
1973 		if (old == rcu_access_pointer(known->pub.ies))
1974 			rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1975 
1976 		cfg80211_update_hidden_bsses(known,
1977 					     rcu_access_pointer(new->pub.beacon_ies),
1978 					     old);
1979 
1980 		if (old)
1981 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1982 	}
1983 
1984 	known->pub.beacon_interval = new->pub.beacon_interval;
1985 
1986 	/* don't update the signal if beacon was heard on
1987 	 * adjacent channel.
1988 	 */
1989 	if (signal_valid)
1990 		known->pub.signal = new->pub.signal;
1991 	known->pub.capability = new->pub.capability;
1992 	known->parent_tsf = new->parent_tsf;
1993 	known->pub.chains = new->pub.chains;
1994 	memcpy(known->pub.chain_signal, new->pub.chain_signal,
1995 	       IEEE80211_MAX_CHAINS);
1996 	ether_addr_copy(known->parent_bssid, new->parent_bssid);
1997 	known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1998 	known->pub.bssid_index = new->pub.bssid_index;
1999 	known->pub.use_for = new->pub.use_for;
2000 	known->pub.cannot_use_reasons = new->pub.cannot_use_reasons;
2001 	known->bss_source = new->bss_source;
2002 
2003 	return true;
2004 }
2005 
2006 /* Returned bss is reference counted and must be cleaned up appropriately. */
2007 static struct cfg80211_internal_bss *
2008 __cfg80211_bss_update(struct cfg80211_registered_device *rdev,
2009 		      struct cfg80211_internal_bss *tmp,
2010 		      bool signal_valid, unsigned long ts)
2011 {
2012 	struct cfg80211_internal_bss *found = NULL;
2013 	struct cfg80211_bss_ies *ies;
2014 
2015 	if (WARN_ON(!tmp->pub.channel))
2016 		goto free_ies;
2017 
2018 	tmp->ts = ts;
2019 
2020 	if (WARN_ON(!rcu_access_pointer(tmp->pub.ies)))
2021 		goto free_ies;
2022 
2023 	found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
2024 
2025 	if (found) {
2026 		if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
2027 			return NULL;
2028 	} else {
2029 		struct cfg80211_internal_bss *new;
2030 		struct cfg80211_internal_bss *hidden;
2031 
2032 		/*
2033 		 * create a copy -- the "res" variable that is passed in
2034 		 * is allocated on the stack since it's not needed in the
2035 		 * more common case of an update
2036 		 */
2037 		new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
2038 			      GFP_ATOMIC);
2039 		if (!new)
2040 			goto free_ies;
2041 		memcpy(new, tmp, sizeof(*new));
2042 		new->refcount = 1;
2043 		INIT_LIST_HEAD(&new->hidden_list);
2044 		INIT_LIST_HEAD(&new->pub.nontrans_list);
2045 		/* we'll set this later if it was non-NULL */
2046 		new->pub.transmitted_bss = NULL;
2047 
2048 		if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
2049 			hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
2050 			if (!hidden)
2051 				hidden = rb_find_bss(rdev, tmp,
2052 						     BSS_CMP_HIDE_NUL);
2053 			if (hidden) {
2054 				new->pub.hidden_beacon_bss = &hidden->pub;
2055 				list_add(&new->hidden_list,
2056 					 &hidden->hidden_list);
2057 				hidden->refcount++;
2058 
2059 				ies = (void *)rcu_access_pointer(new->pub.beacon_ies);
2060 				rcu_assign_pointer(new->pub.beacon_ies,
2061 						   hidden->pub.beacon_ies);
2062 				if (ies)
2063 					kfree_rcu(ies, rcu_head);
2064 			}
2065 		} else {
2066 			/*
2067 			 * Ok so we found a beacon, and don't have an entry. If
2068 			 * it's a beacon with hidden SSID, we might be in for an
2069 			 * expensive search for any probe responses that should
2070 			 * be grouped with this beacon for updates ...
2071 			 */
2072 			if (!cfg80211_combine_bsses(rdev, new)) {
2073 				bss_ref_put(rdev, new);
2074 				return NULL;
2075 			}
2076 		}
2077 
2078 		if (rdev->bss_entries >= bss_entries_limit &&
2079 		    !cfg80211_bss_expire_oldest(rdev)) {
2080 			bss_ref_put(rdev, new);
2081 			return NULL;
2082 		}
2083 
2084 		/* This must be before the call to bss_ref_get */
2085 		if (tmp->pub.transmitted_bss) {
2086 			new->pub.transmitted_bss = tmp->pub.transmitted_bss;
2087 			bss_ref_get(rdev, bss_from_pub(tmp->pub.transmitted_bss));
2088 		}
2089 
2090 		cfg80211_insert_bss(rdev, new);
2091 		found = new;
2092 	}
2093 
2094 	rdev->bss_generation++;
2095 	bss_ref_get(rdev, found);
2096 
2097 	return found;
2098 
2099 free_ies:
2100 	ies = (void *)rcu_access_pointer(tmp->pub.beacon_ies);
2101 	if (ies)
2102 		kfree_rcu(ies, rcu_head);
2103 	ies = (void *)rcu_access_pointer(tmp->pub.proberesp_ies);
2104 	if (ies)
2105 		kfree_rcu(ies, rcu_head);
2106 
2107 	return NULL;
2108 }
2109 
2110 struct cfg80211_internal_bss *
2111 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
2112 		    struct cfg80211_internal_bss *tmp,
2113 		    bool signal_valid, unsigned long ts)
2114 {
2115 	struct cfg80211_internal_bss *res;
2116 
2117 	spin_lock_bh(&rdev->bss_lock);
2118 	res = __cfg80211_bss_update(rdev, tmp, signal_valid, ts);
2119 	spin_unlock_bh(&rdev->bss_lock);
2120 
2121 	return res;
2122 }
2123 
2124 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
2125 				    enum nl80211_band band)
2126 {
2127 	const struct element *tmp;
2128 
2129 	if (band == NL80211_BAND_6GHZ) {
2130 		struct ieee80211_he_operation *he_oper;
2131 
2132 		tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
2133 					     ielen);
2134 		if (tmp && tmp->datalen >= sizeof(*he_oper) &&
2135 		    tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
2136 			const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
2137 
2138 			he_oper = (void *)&tmp->data[1];
2139 
2140 			he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
2141 			if (!he_6ghz_oper)
2142 				return -1;
2143 
2144 			return he_6ghz_oper->primary;
2145 		}
2146 	} else if (band == NL80211_BAND_S1GHZ) {
2147 		tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
2148 		if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
2149 			struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
2150 
2151 			return s1gop->oper_ch;
2152 		}
2153 	} else {
2154 		tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
2155 		if (tmp && tmp->datalen == 1)
2156 			return tmp->data[0];
2157 
2158 		tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
2159 		if (tmp &&
2160 		    tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
2161 			struct ieee80211_ht_operation *htop = (void *)tmp->data;
2162 
2163 			return htop->primary_chan;
2164 		}
2165 	}
2166 
2167 	return -1;
2168 }
2169 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
2170 
2171 /*
2172  * Update RX channel information based on the available frame payload
2173  * information. This is mainly for the 2.4 GHz band where frames can be received
2174  * from neighboring channels and the Beacon frames use the DSSS Parameter Set
2175  * element to indicate the current (transmitting) channel, but this might also
2176  * be needed on other bands if RX frequency does not match with the actual
2177  * operating channel of a BSS, or if the AP reports a different primary channel.
2178  */
2179 static struct ieee80211_channel *
2180 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
2181 			 struct ieee80211_channel *channel)
2182 {
2183 	u32 freq;
2184 	int channel_number;
2185 	struct ieee80211_channel *alt_channel;
2186 
2187 	channel_number = cfg80211_get_ies_channel_number(ie, ielen,
2188 							 channel->band);
2189 
2190 	if (channel_number < 0) {
2191 		/* No channel information in frame payload */
2192 		return channel;
2193 	}
2194 
2195 	freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
2196 
2197 	/*
2198 	 * Frame info (beacon/prob res) is the same as received channel,
2199 	 * no need for further processing.
2200 	 */
2201 	if (freq == ieee80211_channel_to_khz(channel))
2202 		return channel;
2203 
2204 	alt_channel = ieee80211_get_channel_khz(wiphy, freq);
2205 	if (!alt_channel) {
2206 		if (channel->band == NL80211_BAND_2GHZ ||
2207 		    channel->band == NL80211_BAND_6GHZ) {
2208 			/*
2209 			 * Better not allow unexpected channels when that could
2210 			 * be going beyond the 1-11 range (e.g., discovering
2211 			 * BSS on channel 12 when radio is configured for
2212 			 * channel 11) or beyond the 6 GHz channel range.
2213 			 */
2214 			return NULL;
2215 		}
2216 
2217 		/* No match for the payload channel number - ignore it */
2218 		return channel;
2219 	}
2220 
2221 	/*
2222 	 * Use the channel determined through the payload channel number
2223 	 * instead of the RX channel reported by the driver.
2224 	 */
2225 	if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
2226 		return NULL;
2227 	return alt_channel;
2228 }
2229 
2230 struct cfg80211_inform_single_bss_data {
2231 	struct cfg80211_inform_bss *drv_data;
2232 	enum cfg80211_bss_frame_type ftype;
2233 	struct ieee80211_channel *channel;
2234 	u8 bssid[ETH_ALEN];
2235 	u64 tsf;
2236 	u16 capability;
2237 	u16 beacon_interval;
2238 	const u8 *ie;
2239 	size_t ielen;
2240 
2241 	enum bss_source_type bss_source;
2242 	/* Set if reporting bss_source != BSS_SOURCE_DIRECT */
2243 	struct cfg80211_bss *source_bss;
2244 	u8 max_bssid_indicator;
2245 	u8 bssid_index;
2246 
2247 	u8 use_for;
2248 	u64 cannot_use_reasons;
2249 };
2250 
2251 enum ieee80211_ap_reg_power
2252 cfg80211_get_6ghz_power_type(const u8 *elems, size_t elems_len,
2253 			     u32 client_flags)
2254 {
2255 	const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
2256 	struct ieee80211_he_operation *he_oper;
2257 	const struct element *tmp;
2258 
2259 	tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION,
2260 				     elems, elems_len);
2261 	if (!tmp || tmp->datalen < sizeof(*he_oper) + 1 ||
2262 	    tmp->datalen < ieee80211_he_oper_size(tmp->data + 1))
2263 		return IEEE80211_REG_UNSET_AP;
2264 
2265 	he_oper = (void *)&tmp->data[1];
2266 	he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
2267 
2268 	if (!he_6ghz_oper)
2269 		return IEEE80211_REG_UNSET_AP;
2270 
2271 	return cfg80211_6ghz_power_type(he_6ghz_oper->control, client_flags);
2272 }
2273 
2274 static bool cfg80211_6ghz_power_type_valid(const u8 *elems, size_t elems_len,
2275 					   const u32 flags)
2276 {
2277 	switch (cfg80211_get_6ghz_power_type(elems, elems_len, flags)) {
2278 	case IEEE80211_REG_LPI_AP:
2279 		return true;
2280 	case IEEE80211_REG_SP_AP:
2281 		return !(flags & IEEE80211_CHAN_NO_6GHZ_AFC_CLIENT);
2282 	case IEEE80211_REG_VLP_AP:
2283 		return !(flags & IEEE80211_CHAN_NO_6GHZ_VLP_CLIENT);
2284 	default:
2285 		return false;
2286 	}
2287 }
2288 
2289 /* Returned bss is reference counted and must be cleaned up appropriately. */
2290 static struct cfg80211_bss *
2291 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
2292 				struct cfg80211_inform_single_bss_data *data,
2293 				gfp_t gfp)
2294 {
2295 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2296 	struct cfg80211_inform_bss *drv_data = data->drv_data;
2297 	struct cfg80211_bss_ies *ies;
2298 	struct ieee80211_channel *channel;
2299 	struct cfg80211_internal_bss tmp = {}, *res;
2300 	int bss_type;
2301 	bool signal_valid;
2302 	unsigned long ts;
2303 
2304 	if (WARN_ON(!wiphy))
2305 		return NULL;
2306 
2307 	if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2308 		    (drv_data->signal < 0 || drv_data->signal > 100)))
2309 		return NULL;
2310 
2311 	if (WARN_ON(data->bss_source != BSS_SOURCE_DIRECT && !data->source_bss))
2312 		return NULL;
2313 
2314 	channel = data->channel;
2315 	if (!channel)
2316 		channel = cfg80211_get_bss_channel(wiphy, data->ie, data->ielen,
2317 						   drv_data->chan);
2318 	if (!channel)
2319 		return NULL;
2320 
2321 	if (channel->band == NL80211_BAND_6GHZ &&
2322 	    !cfg80211_6ghz_power_type_valid(data->ie, data->ielen,
2323 					    channel->flags)) {
2324 		data->use_for = 0;
2325 		data->cannot_use_reasons =
2326 			NL80211_BSS_CANNOT_USE_6GHZ_PWR_MISMATCH;
2327 	}
2328 
2329 	memcpy(tmp.pub.bssid, data->bssid, ETH_ALEN);
2330 	tmp.pub.channel = channel;
2331 	if (data->bss_source != BSS_SOURCE_STA_PROFILE)
2332 		tmp.pub.signal = drv_data->signal;
2333 	else
2334 		tmp.pub.signal = 0;
2335 	tmp.pub.beacon_interval = data->beacon_interval;
2336 	tmp.pub.capability = data->capability;
2337 	tmp.pub.ts_boottime = drv_data->boottime_ns;
2338 	tmp.parent_tsf = drv_data->parent_tsf;
2339 	ether_addr_copy(tmp.parent_bssid, drv_data->parent_bssid);
2340 	tmp.pub.chains = drv_data->chains;
2341 	memcpy(tmp.pub.chain_signal, drv_data->chain_signal,
2342 	       IEEE80211_MAX_CHAINS);
2343 	tmp.pub.use_for = data->use_for;
2344 	tmp.pub.cannot_use_reasons = data->cannot_use_reasons;
2345 	tmp.bss_source = data->bss_source;
2346 
2347 	switch (data->bss_source) {
2348 	case BSS_SOURCE_MBSSID:
2349 		tmp.pub.transmitted_bss = data->source_bss;
2350 		fallthrough;
2351 	case BSS_SOURCE_STA_PROFILE:
2352 		ts = bss_from_pub(data->source_bss)->ts;
2353 		tmp.pub.bssid_index = data->bssid_index;
2354 		tmp.pub.max_bssid_indicator = data->max_bssid_indicator;
2355 		break;
2356 	case BSS_SOURCE_DIRECT:
2357 		ts = jiffies;
2358 
2359 		if (channel->band == NL80211_BAND_60GHZ) {
2360 			bss_type = data->capability &
2361 				   WLAN_CAPABILITY_DMG_TYPE_MASK;
2362 			if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2363 			    bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2364 				regulatory_hint_found_beacon(wiphy, channel,
2365 							     gfp);
2366 		} else {
2367 			if (data->capability & WLAN_CAPABILITY_ESS)
2368 				regulatory_hint_found_beacon(wiphy, channel,
2369 							     gfp);
2370 		}
2371 		break;
2372 	}
2373 
2374 	/*
2375 	 * If we do not know here whether the IEs are from a Beacon or Probe
2376 	 * Response frame, we need to pick one of the options and only use it
2377 	 * with the driver that does not provide the full Beacon/Probe Response
2378 	 * frame. Use Beacon frame pointer to avoid indicating that this should
2379 	 * override the IEs pointer should we have received an earlier
2380 	 * indication of Probe Response data.
2381 	 */
2382 	ies = kzalloc(sizeof(*ies) + data->ielen, gfp);
2383 	if (!ies)
2384 		return NULL;
2385 	ies->len = data->ielen;
2386 	ies->tsf = data->tsf;
2387 	ies->from_beacon = false;
2388 	memcpy(ies->data, data->ie, data->ielen);
2389 
2390 	switch (data->ftype) {
2391 	case CFG80211_BSS_FTYPE_BEACON:
2392 	case CFG80211_BSS_FTYPE_S1G_BEACON:
2393 		ies->from_beacon = true;
2394 		fallthrough;
2395 	case CFG80211_BSS_FTYPE_UNKNOWN:
2396 		rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2397 		break;
2398 	case CFG80211_BSS_FTYPE_PRESP:
2399 		rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2400 		break;
2401 	}
2402 	rcu_assign_pointer(tmp.pub.ies, ies);
2403 
2404 	signal_valid = drv_data->chan == channel;
2405 	spin_lock_bh(&rdev->bss_lock);
2406 	res = __cfg80211_bss_update(rdev, &tmp, signal_valid, ts);
2407 	if (!res)
2408 		goto drop;
2409 
2410 	rdev_inform_bss(rdev, &res->pub, ies, drv_data->drv_data);
2411 
2412 	if (data->bss_source == BSS_SOURCE_MBSSID) {
2413 		/* this is a nontransmitting bss, we need to add it to
2414 		 * transmitting bss' list if it is not there
2415 		 */
2416 		if (cfg80211_add_nontrans_list(data->source_bss, &res->pub)) {
2417 			if (__cfg80211_unlink_bss(rdev, res)) {
2418 				rdev->bss_generation++;
2419 				res = NULL;
2420 			}
2421 		}
2422 
2423 		if (!res)
2424 			goto drop;
2425 	}
2426 	spin_unlock_bh(&rdev->bss_lock);
2427 
2428 	trace_cfg80211_return_bss(&res->pub);
2429 	/* __cfg80211_bss_update gives us a referenced result */
2430 	return &res->pub;
2431 
2432 drop:
2433 	spin_unlock_bh(&rdev->bss_lock);
2434 	return NULL;
2435 }
2436 
2437 static bool cfg80211_iter_profile_continuation(const u8 *ie, size_t ielen,
2438 					       const struct element **mbssid,
2439 					       const struct element **sub_elem)
2440 {
2441 	const u8 *mbssid_end = (*mbssid)->data + (*mbssid)->datalen;
2442 	const struct element *next_mbssid;
2443 	const struct element *next_sub;
2444 
2445 	next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2446 					 mbssid_end,
2447 					 ielen - (mbssid_end - ie));
2448 
2449 	/*
2450 	 * If it is not the last subelement in current MBSSID IE or there isn't
2451 	 * a next MBSSID IE - profile is complete.
2452 	*/
2453 	if (((*sub_elem)->data + (*sub_elem)->datalen < mbssid_end - 1) ||
2454 	    !next_mbssid)
2455 		return false;
2456 
2457 	/* For any length error, just return false to stop iteration */
2458 
2459 	if (next_mbssid->datalen < 4)
2460 		return false;
2461 
2462 	next_sub = (void *)&next_mbssid->data[1];
2463 
2464 	if (next_mbssid->data + next_mbssid->datalen <
2465 	    next_sub->data + next_sub->datalen)
2466 		return false;
2467 
2468 	if (next_sub->id != 0 || next_sub->datalen < 2)
2469 		return false;
2470 
2471 	/*
2472 	 * Check if the first element in the next sub element is a start
2473 	 * of a new profile
2474 	 */
2475 	if (next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP)
2476 		return false;
2477 
2478 	*mbssid = next_mbssid;
2479 	*sub_elem = next_sub;
2480 	return true;
2481 }
2482 
2483 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2484 			      const struct element *mbssid_elem,
2485 			      const struct element *sub_elem,
2486 			      u8 *merged_ie, size_t max_copy_len)
2487 {
2488 	size_t copied_len = sub_elem->datalen;
2489 
2490 	if (sub_elem->datalen > max_copy_len)
2491 		return 0;
2492 
2493 	memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2494 
2495 	while (cfg80211_iter_profile_continuation(ie, ielen,
2496 						  &mbssid_elem,
2497 						  &sub_elem)) {
2498 		if (copied_len + sub_elem->datalen > max_copy_len)
2499 			break;
2500 		memcpy(merged_ie + copied_len, sub_elem->data,
2501 		       sub_elem->datalen);
2502 		copied_len += sub_elem->datalen;
2503 	}
2504 
2505 	return copied_len;
2506 }
2507 EXPORT_SYMBOL(cfg80211_merge_profile);
2508 
2509 static void
2510 cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2511 			   struct cfg80211_inform_single_bss_data *tx_data,
2512 			   struct cfg80211_bss *source_bss,
2513 			   gfp_t gfp)
2514 {
2515 	struct cfg80211_inform_single_bss_data data = {
2516 		.drv_data = tx_data->drv_data,
2517 		.ftype = tx_data->ftype,
2518 		.tsf = tx_data->tsf,
2519 		.beacon_interval = tx_data->beacon_interval,
2520 		.source_bss = source_bss,
2521 		.bss_source = BSS_SOURCE_MBSSID,
2522 		.use_for = tx_data->use_for,
2523 		.cannot_use_reasons = tx_data->cannot_use_reasons,
2524 	};
2525 	const u8 *mbssid_index_ie;
2526 	const struct element *elem, *sub;
2527 	u8 *new_ie, *profile;
2528 	u64 seen_indices = 0;
2529 	struct cfg80211_bss *bss;
2530 
2531 	if (!source_bss)
2532 		return;
2533 	if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2534 				tx_data->ie, tx_data->ielen))
2535 		return;
2536 	if (!wiphy->support_mbssid)
2537 		return;
2538 	if (wiphy->support_only_he_mbssid &&
2539 	    !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
2540 				    tx_data->ie, tx_data->ielen))
2541 		return;
2542 
2543 	new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2544 	if (!new_ie)
2545 		return;
2546 
2547 	profile = kmalloc(tx_data->ielen, gfp);
2548 	if (!profile)
2549 		goto out;
2550 
2551 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID,
2552 			    tx_data->ie, tx_data->ielen) {
2553 		if (elem->datalen < 4)
2554 			continue;
2555 		if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2556 			continue;
2557 		for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2558 			u8 profile_len;
2559 
2560 			if (sub->id != 0 || sub->datalen < 4) {
2561 				/* not a valid BSS profile */
2562 				continue;
2563 			}
2564 
2565 			if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2566 			    sub->data[1] != 2) {
2567 				/* The first element within the Nontransmitted
2568 				 * BSSID Profile is not the Nontransmitted
2569 				 * BSSID Capability element.
2570 				 */
2571 				continue;
2572 			}
2573 
2574 			memset(profile, 0, tx_data->ielen);
2575 			profile_len = cfg80211_merge_profile(tx_data->ie,
2576 							     tx_data->ielen,
2577 							     elem,
2578 							     sub,
2579 							     profile,
2580 							     tx_data->ielen);
2581 
2582 			/* found a Nontransmitted BSSID Profile */
2583 			mbssid_index_ie = cfg80211_find_ie
2584 				(WLAN_EID_MULTI_BSSID_IDX,
2585 				 profile, profile_len);
2586 			if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2587 			    mbssid_index_ie[2] == 0 ||
2588 			    mbssid_index_ie[2] > 46 ||
2589 			    mbssid_index_ie[2] >= (1 << elem->data[0])) {
2590 				/* No valid Multiple BSSID-Index element */
2591 				continue;
2592 			}
2593 
2594 			if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2595 				/* We don't support legacy split of a profile */
2596 				net_dbg_ratelimited("Partial info for BSSID index %d\n",
2597 						    mbssid_index_ie[2]);
2598 
2599 			seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2600 
2601 			data.bssid_index = mbssid_index_ie[2];
2602 			data.max_bssid_indicator = elem->data[0];
2603 
2604 			cfg80211_gen_new_bssid(tx_data->bssid,
2605 					       data.max_bssid_indicator,
2606 					       data.bssid_index,
2607 					       data.bssid);
2608 
2609 			memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2610 			data.ie = new_ie;
2611 			data.ielen = cfg80211_gen_new_ie(tx_data->ie,
2612 							 tx_data->ielen,
2613 							 profile,
2614 							 profile_len,
2615 							 new_ie,
2616 							 IEEE80211_MAX_DATA_LEN);
2617 			if (!data.ielen)
2618 				continue;
2619 
2620 			data.capability = get_unaligned_le16(profile + 2);
2621 			bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp);
2622 			if (!bss)
2623 				break;
2624 			cfg80211_put_bss(wiphy, bss);
2625 		}
2626 	}
2627 
2628 out:
2629 	kfree(new_ie);
2630 	kfree(profile);
2631 }
2632 
2633 ssize_t cfg80211_defragment_element(const struct element *elem, const u8 *ies,
2634 				    size_t ieslen, u8 *data, size_t data_len,
2635 				    u8 frag_id)
2636 {
2637 	const struct element *next;
2638 	ssize_t copied;
2639 	u8 elem_datalen;
2640 
2641 	if (!elem || (const u8 *)elem < ies ||
2642 	    (const u8 *)elem + sizeof(*elem) > ies + ieslen ||
2643 	    (const u8 *)elem + sizeof(*elem) + elem->datalen > ies + ieslen)
2644 		return -EINVAL;
2645 
2646 	/* elem might be invalid after the memmove */
2647 	next = (void *)(elem->data + elem->datalen);
2648 	elem_datalen = elem->datalen;
2649 
2650 	if (elem->id == WLAN_EID_EXTENSION) {
2651 		copied = elem->datalen - 1;
2652 
2653 		if (data) {
2654 			if (copied > data_len)
2655 				return -ENOSPC;
2656 
2657 			memmove(data, elem->data + 1, copied);
2658 		}
2659 	} else {
2660 		copied = elem->datalen;
2661 
2662 		if (data) {
2663 			if (copied > data_len)
2664 				return -ENOSPC;
2665 
2666 			memmove(data, elem->data, copied);
2667 		}
2668 	}
2669 
2670 	/* Fragmented elements must have 255 bytes */
2671 	if (elem_datalen < 255)
2672 		return copied;
2673 
2674 	for (elem = next;
2675 	     elem->data < ies + ieslen &&
2676 		elem->data + elem->datalen <= ies + ieslen;
2677 	     elem = next) {
2678 		/* elem might be invalid after the memmove */
2679 		next = (void *)(elem->data + elem->datalen);
2680 
2681 		if (elem->id != frag_id)
2682 			break;
2683 
2684 		elem_datalen = elem->datalen;
2685 
2686 		if (data) {
2687 			if (copied + elem_datalen > data_len)
2688 				return -ENOSPC;
2689 
2690 			memmove(data + copied, elem->data, elem_datalen);
2691 		}
2692 
2693 		copied += elem_datalen;
2694 
2695 		/* Only the last fragment may be short */
2696 		if (elem_datalen != 255)
2697 			break;
2698 	}
2699 
2700 	return copied;
2701 }
2702 EXPORT_SYMBOL(cfg80211_defragment_element);
2703 
2704 struct cfg80211_mle {
2705 	struct ieee80211_multi_link_elem *mle;
2706 	struct ieee80211_mle_per_sta_profile
2707 		*sta_prof[IEEE80211_MLD_MAX_NUM_LINKS];
2708 	ssize_t sta_prof_len[IEEE80211_MLD_MAX_NUM_LINKS];
2709 
2710 	u8 data[];
2711 };
2712 
2713 static struct cfg80211_mle *
2714 cfg80211_defrag_mle(const struct element *mle, const u8 *ie, size_t ielen,
2715 		    gfp_t gfp)
2716 {
2717 	const struct element *elem;
2718 	struct cfg80211_mle *res;
2719 	size_t buf_len;
2720 	ssize_t mle_len;
2721 	u8 common_size, idx;
2722 
2723 	if (!mle || !ieee80211_mle_size_ok(mle->data + 1, mle->datalen - 1))
2724 		return NULL;
2725 
2726 	/* Required length for first defragmentation */
2727 	buf_len = mle->datalen - 1;
2728 	for_each_element(elem, mle->data + mle->datalen,
2729 			 ie + ielen - mle->data - mle->datalen) {
2730 		if (elem->id != WLAN_EID_FRAGMENT)
2731 			break;
2732 
2733 		buf_len += elem->datalen;
2734 	}
2735 
2736 	res = kzalloc_flex(*res, data, buf_len, gfp);
2737 	if (!res)
2738 		return NULL;
2739 
2740 	mle_len = cfg80211_defragment_element(mle, ie, ielen,
2741 					      res->data, buf_len,
2742 					      WLAN_EID_FRAGMENT);
2743 	if (mle_len < 0)
2744 		goto error;
2745 
2746 	res->mle = (void *)res->data;
2747 
2748 	/* Find the sub-element area in the buffer */
2749 	common_size = ieee80211_mle_common_size((u8 *)res->mle);
2750 	ie = res->data + common_size;
2751 	ielen = mle_len - common_size;
2752 
2753 	idx = 0;
2754 	for_each_element_id(elem, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE,
2755 			    ie, ielen) {
2756 		res->sta_prof[idx] = (void *)elem->data;
2757 		res->sta_prof_len[idx] = elem->datalen;
2758 
2759 		idx++;
2760 		if (idx >= IEEE80211_MLD_MAX_NUM_LINKS)
2761 			break;
2762 	}
2763 	if (!for_each_element_completed(elem, ie, ielen))
2764 		goto error;
2765 
2766 	/* Defragment sta_info in-place */
2767 	for (idx = 0; idx < IEEE80211_MLD_MAX_NUM_LINKS && res->sta_prof[idx];
2768 	     idx++) {
2769 		if (res->sta_prof_len[idx] < 255)
2770 			continue;
2771 
2772 		elem = (void *)res->sta_prof[idx] - 2;
2773 
2774 		if (idx + 1 < ARRAY_SIZE(res->sta_prof) &&
2775 		    res->sta_prof[idx + 1])
2776 			buf_len = (u8 *)res->sta_prof[idx + 1] -
2777 				  (u8 *)res->sta_prof[idx];
2778 		else
2779 			buf_len = ielen + ie - (u8 *)elem;
2780 
2781 		res->sta_prof_len[idx] =
2782 			cfg80211_defragment_element(elem,
2783 						    (u8 *)elem, buf_len,
2784 						    (u8 *)res->sta_prof[idx],
2785 						    buf_len,
2786 						    IEEE80211_MLE_SUBELEM_FRAGMENT);
2787 		if (res->sta_prof_len[idx] < 0)
2788 			goto error;
2789 	}
2790 
2791 	return res;
2792 
2793 error:
2794 	kfree(res);
2795 	return NULL;
2796 }
2797 
2798 struct tbtt_info_iter_data {
2799 	const struct ieee80211_neighbor_ap_info *ap_info;
2800 	u8 param_ch_count;
2801 	u32 use_for;
2802 	u8 mld_id, link_id;
2803 	bool non_tx;
2804 };
2805 
2806 static enum cfg80211_rnr_iter_ret
2807 cfg802121_mld_ap_rnr_iter(void *_data, u8 type,
2808 			  const struct ieee80211_neighbor_ap_info *info,
2809 			  const u8 *tbtt_info, u8 tbtt_info_len)
2810 {
2811 	const struct ieee80211_rnr_mld_params *mld_params;
2812 	struct tbtt_info_iter_data *data = _data;
2813 	u8 link_id;
2814 	bool non_tx = false;
2815 
2816 	if (type == IEEE80211_TBTT_INFO_TYPE_TBTT &&
2817 	    tbtt_info_len >= offsetofend(struct ieee80211_tbtt_info_ge_11,
2818 					 mld_params)) {
2819 		const struct ieee80211_tbtt_info_ge_11 *tbtt_info_ge_11 =
2820 			(void *)tbtt_info;
2821 
2822 		non_tx = (tbtt_info_ge_11->bss_params &
2823 			  (IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID |
2824 			   IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID)) ==
2825 			 IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID;
2826 		mld_params = &tbtt_info_ge_11->mld_params;
2827 	} else if (type == IEEE80211_TBTT_INFO_TYPE_MLD &&
2828 		 tbtt_info_len >= sizeof(struct ieee80211_rnr_mld_params))
2829 		mld_params = (void *)tbtt_info;
2830 	else
2831 		return RNR_ITER_CONTINUE;
2832 
2833 	link_id = le16_get_bits(mld_params->params,
2834 				IEEE80211_RNR_MLD_PARAMS_LINK_ID);
2835 
2836 	if (data->mld_id != mld_params->mld_id)
2837 		return RNR_ITER_CONTINUE;
2838 
2839 	if (data->link_id != link_id)
2840 		return RNR_ITER_CONTINUE;
2841 
2842 	data->ap_info = info;
2843 	data->param_ch_count =
2844 		le16_get_bits(mld_params->params,
2845 			      IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT);
2846 	data->non_tx = non_tx;
2847 
2848 	if (type == IEEE80211_TBTT_INFO_TYPE_TBTT)
2849 		data->use_for = NL80211_BSS_USE_FOR_ALL;
2850 	else
2851 		data->use_for = NL80211_BSS_USE_FOR_MLD_LINK;
2852 	return RNR_ITER_BREAK;
2853 }
2854 
2855 static u8
2856 cfg80211_rnr_info_for_mld_ap(const u8 *ie, size_t ielen, u8 mld_id, u8 link_id,
2857 			     const struct ieee80211_neighbor_ap_info **ap_info,
2858 			     u8 *param_ch_count, bool *non_tx)
2859 {
2860 	struct tbtt_info_iter_data data = {
2861 		.mld_id = mld_id,
2862 		.link_id = link_id,
2863 	};
2864 
2865 	cfg80211_iter_rnr(ie, ielen, cfg802121_mld_ap_rnr_iter, &data);
2866 
2867 	*ap_info = data.ap_info;
2868 	*param_ch_count = data.param_ch_count;
2869 	*non_tx = data.non_tx;
2870 
2871 	return data.use_for;
2872 }
2873 
2874 static struct element *
2875 cfg80211_gen_reporter_rnr(struct cfg80211_bss *source_bss, bool is_mbssid,
2876 			  bool same_mld, u8 link_id, u8 bss_change_count,
2877 			  gfp_t gfp)
2878 {
2879 	const struct cfg80211_bss_ies *ies;
2880 	struct ieee80211_neighbor_ap_info ap_info;
2881 	struct ieee80211_tbtt_info_ge_11 tbtt_info;
2882 	u32 short_ssid;
2883 	const struct element *elem;
2884 	struct element *res;
2885 
2886 	/*
2887 	 * We only generate the RNR to permit ML lookups. For that we do not
2888 	 * need an entry for the corresponding transmitting BSS, lets just skip
2889 	 * it even though it would be easy to add.
2890 	 */
2891 	if (!same_mld)
2892 		return NULL;
2893 
2894 	/* We could use tx_data->ies if we change cfg80211_calc_short_ssid */
2895 	rcu_read_lock();
2896 	ies = rcu_dereference(source_bss->ies);
2897 
2898 	ap_info.tbtt_info_len = offsetofend(typeof(tbtt_info), mld_params);
2899 	ap_info.tbtt_info_hdr =
2900 			u8_encode_bits(IEEE80211_TBTT_INFO_TYPE_TBTT,
2901 				       IEEE80211_AP_INFO_TBTT_HDR_TYPE) |
2902 			u8_encode_bits(0, IEEE80211_AP_INFO_TBTT_HDR_COUNT);
2903 
2904 	ap_info.channel = ieee80211_frequency_to_channel(source_bss->channel->center_freq);
2905 
2906 	/* operating class */
2907 	elem = cfg80211_find_elem(WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
2908 				  ies->data, ies->len);
2909 	if (elem && elem->datalen >= 1) {
2910 		ap_info.op_class = elem->data[0];
2911 	} else {
2912 		struct cfg80211_chan_def chandef;
2913 
2914 		/* The AP is not providing us with anything to work with. So
2915 		 * make up a somewhat reasonable operating class, but don't
2916 		 * bother with it too much as no one will ever use the
2917 		 * information.
2918 		 */
2919 		cfg80211_chandef_create(&chandef, source_bss->channel,
2920 					NL80211_CHAN_NO_HT);
2921 
2922 		if (!ieee80211_chandef_to_operating_class(&chandef,
2923 							  &ap_info.op_class))
2924 			goto out_unlock;
2925 	}
2926 
2927 	/* Just set TBTT offset and PSD 20 to invalid/unknown */
2928 	tbtt_info.tbtt_offset = 255;
2929 	tbtt_info.psd_20 = IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
2930 
2931 	memcpy(tbtt_info.bssid, source_bss->bssid, ETH_ALEN);
2932 	if (cfg80211_calc_short_ssid(ies, &elem, &short_ssid))
2933 		goto out_unlock;
2934 
2935 	rcu_read_unlock();
2936 
2937 	tbtt_info.short_ssid = cpu_to_le32(short_ssid);
2938 
2939 	tbtt_info.bss_params = IEEE80211_RNR_TBTT_PARAMS_SAME_SSID;
2940 
2941 	if (is_mbssid) {
2942 		tbtt_info.bss_params |= IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID;
2943 		tbtt_info.bss_params |= IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID;
2944 	}
2945 
2946 	tbtt_info.mld_params.mld_id = 0;
2947 	tbtt_info.mld_params.params =
2948 		le16_encode_bits(link_id, IEEE80211_RNR_MLD_PARAMS_LINK_ID) |
2949 		le16_encode_bits(bss_change_count,
2950 				 IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT);
2951 
2952 	res = kzalloc_flex(*res, data, sizeof(ap_info) + ap_info.tbtt_info_len,
2953 			   gfp);
2954 	if (!res)
2955 		return NULL;
2956 
2957 	/* Copy the data */
2958 	res->id = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
2959 	res->datalen = sizeof(ap_info) + ap_info.tbtt_info_len;
2960 	memcpy(res->data, &ap_info, sizeof(ap_info));
2961 	memcpy(res->data + sizeof(ap_info), &tbtt_info, ap_info.tbtt_info_len);
2962 
2963 	return res;
2964 
2965 out_unlock:
2966 	rcu_read_unlock();
2967 	return NULL;
2968 }
2969 
2970 static void
2971 cfg80211_parse_ml_elem_sta_data(struct wiphy *wiphy,
2972 				struct cfg80211_inform_single_bss_data *tx_data,
2973 				struct cfg80211_bss *source_bss,
2974 				const struct element *elem,
2975 				gfp_t gfp)
2976 {
2977 	struct cfg80211_inform_single_bss_data data = {
2978 		.drv_data = tx_data->drv_data,
2979 		.ftype = tx_data->ftype,
2980 		.source_bss = source_bss,
2981 		.bss_source = BSS_SOURCE_STA_PROFILE,
2982 	};
2983 	struct element *reporter_rnr = NULL;
2984 	struct ieee80211_multi_link_elem *ml_elem;
2985 	struct cfg80211_mle *mle;
2986 	const struct element *ssid_elem;
2987 	const u8 *ssid = NULL;
2988 	size_t ssid_len = 0;
2989 	u16 control;
2990 	u8 ml_common_len;
2991 	u8 *new_ie = NULL;
2992 	struct cfg80211_bss *bss;
2993 	u8 mld_id, reporter_link_id, bss_change_count;
2994 	u16 seen_links = 0;
2995 	u8 i;
2996 
2997 	if (!ieee80211_mle_type_ok(elem->data + 1,
2998 				   IEEE80211_ML_CONTROL_TYPE_BASIC,
2999 				   elem->datalen - 1))
3000 		return;
3001 
3002 	ml_elem = (void *)(elem->data + 1);
3003 	control = le16_to_cpu(ml_elem->control);
3004 	ml_common_len = ml_elem->variable[0];
3005 
3006 	/* Must be present when transmitted by an AP (in a probe response) */
3007 	if (!(control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT) ||
3008 	    !(control & IEEE80211_MLC_BASIC_PRES_LINK_ID) ||
3009 	    !(control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP))
3010 		return;
3011 
3012 	reporter_link_id = ieee80211_mle_get_link_id(elem->data + 1);
3013 	bss_change_count = ieee80211_mle_get_bss_param_ch_cnt(elem->data + 1);
3014 
3015 	/*
3016 	 * The MLD ID of the reporting AP is always zero. It is set if the AP
3017 	 * is part of an MBSSID set and will be non-zero for ML Elements
3018 	 * relating to a nontransmitted BSS (matching the Multi-BSSID Index,
3019 	 * Draft P802.11be_D3.2, 35.3.4.2)
3020 	 */
3021 	mld_id = ieee80211_mle_get_mld_id(elem->data + 1);
3022 
3023 	/* Fully defrag the ML element for sta information/profile iteration */
3024 	mle = cfg80211_defrag_mle(elem, tx_data->ie, tx_data->ielen, gfp);
3025 	if (!mle)
3026 		return;
3027 
3028 	/* No point in doing anything if there is no per-STA profile */
3029 	if (!mle->sta_prof[0])
3030 		goto out;
3031 
3032 	new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
3033 	if (!new_ie)
3034 		goto out;
3035 
3036 	reporter_rnr = cfg80211_gen_reporter_rnr(source_bss,
3037 						 u16_get_bits(control,
3038 							      IEEE80211_MLC_BASIC_PRES_MLD_ID),
3039 						 mld_id == 0, reporter_link_id,
3040 						 bss_change_count,
3041 						 gfp);
3042 
3043 	ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, tx_data->ie,
3044 				       tx_data->ielen);
3045 	if (ssid_elem) {
3046 		ssid = ssid_elem->data;
3047 		ssid_len = ssid_elem->datalen;
3048 	}
3049 
3050 	for (i = 0; i < ARRAY_SIZE(mle->sta_prof) && mle->sta_prof[i]; i++) {
3051 		const struct ieee80211_neighbor_ap_info *ap_info;
3052 		enum nl80211_band band;
3053 		u32 freq;
3054 		const u8 *profile;
3055 		ssize_t profile_len;
3056 		u8 param_ch_count;
3057 		u8 link_id, use_for;
3058 		bool non_tx;
3059 
3060 		if (!ieee80211_mle_basic_sta_prof_size_ok((u8 *)mle->sta_prof[i],
3061 							  mle->sta_prof_len[i]))
3062 			continue;
3063 
3064 		control = le16_to_cpu(mle->sta_prof[i]->control);
3065 
3066 		if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
3067 			continue;
3068 
3069 		link_id = u16_get_bits(control,
3070 				       IEEE80211_MLE_STA_CONTROL_LINK_ID);
3071 		if (seen_links & BIT(link_id))
3072 			break;
3073 		seen_links |= BIT(link_id);
3074 
3075 		if (!(control & IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT) ||
3076 		    !(control & IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT) ||
3077 		    !(control & IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT))
3078 			continue;
3079 
3080 		memcpy(data.bssid, mle->sta_prof[i]->variable, ETH_ALEN);
3081 		data.beacon_interval =
3082 			get_unaligned_le16(mle->sta_prof[i]->variable + 6);
3083 		data.tsf = tx_data->tsf +
3084 			   get_unaligned_le64(mle->sta_prof[i]->variable + 8);
3085 
3086 		/* sta_info_len counts itself */
3087 		profile = mle->sta_prof[i]->variable +
3088 			  mle->sta_prof[i]->sta_info_len - 1;
3089 		profile_len = (u8 *)mle->sta_prof[i] + mle->sta_prof_len[i] -
3090 			      profile;
3091 
3092 		if (profile_len < 2)
3093 			continue;
3094 
3095 		data.capability = get_unaligned_le16(profile);
3096 		profile += 2;
3097 		profile_len -= 2;
3098 
3099 		/* Find in RNR to look up channel information */
3100 		use_for = cfg80211_rnr_info_for_mld_ap(tx_data->ie,
3101 						       tx_data->ielen,
3102 						       mld_id, link_id,
3103 						       &ap_info,
3104 						       &param_ch_count,
3105 						       &non_tx);
3106 		if (!use_for)
3107 			continue;
3108 
3109 		/*
3110 		 * As of 802.11be_D5.0, the specification does not give us any
3111 		 * way of discovering both the MaxBSSID and the Multiple-BSSID
3112 		 * Index. It does seem like the Multiple-BSSID Index element
3113 		 * may be provided, but section 9.4.2.45 explicitly forbids
3114 		 * including a Multiple-BSSID Element (in this case without any
3115 		 * subelements).
3116 		 * Without both pieces of information we cannot calculate the
3117 		 * reference BSSID, so simply ignore the BSS.
3118 		 */
3119 		if (non_tx)
3120 			continue;
3121 
3122 		/* We could sanity check the BSSID is included */
3123 
3124 		if (!ieee80211_operating_class_to_band(ap_info->op_class,
3125 						       &band))
3126 			continue;
3127 
3128 		freq = ieee80211_channel_to_freq_khz(ap_info->channel, band);
3129 		data.channel = ieee80211_get_channel_khz(wiphy, freq);
3130 
3131 		/* Skip if RNR element specifies an unsupported channel */
3132 		if (!data.channel)
3133 			continue;
3134 
3135 		/* Skip if BSS entry generated from MBSSID or DIRECT source
3136 		 * frame data available already.
3137 		 */
3138 		bss = cfg80211_get_bss(wiphy, data.channel, data.bssid, ssid,
3139 				       ssid_len, IEEE80211_BSS_TYPE_ANY,
3140 				       IEEE80211_PRIVACY_ANY);
3141 		if (bss) {
3142 			struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
3143 
3144 			if (data.capability == bss->capability &&
3145 			    ibss->bss_source != BSS_SOURCE_STA_PROFILE) {
3146 				cfg80211_put_bss(wiphy, bss);
3147 				continue;
3148 			}
3149 			cfg80211_put_bss(wiphy, bss);
3150 		}
3151 
3152 		if (use_for == NL80211_BSS_USE_FOR_MLD_LINK &&
3153 		    !(wiphy->flags & WIPHY_FLAG_SUPPORTS_NSTR_NONPRIMARY)) {
3154 			use_for = 0;
3155 			data.cannot_use_reasons =
3156 				NL80211_BSS_CANNOT_USE_NSTR_NONPRIMARY;
3157 		}
3158 		data.use_for = use_for;
3159 
3160 		/* Generate new elements */
3161 		memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
3162 		data.ie = new_ie;
3163 		data.ielen = cfg80211_gen_new_ie(tx_data->ie, tx_data->ielen,
3164 						 profile, profile_len,
3165 						 new_ie,
3166 						 IEEE80211_MAX_DATA_LEN);
3167 		if (!data.ielen)
3168 			continue;
3169 
3170 		/* The generated elements do not contain:
3171 		 *  - Basic ML element
3172 		 *  - A TBTT entry in the RNR for the transmitting AP
3173 		 *
3174 		 * This information is needed both internally and in userspace
3175 		 * as such, we should append it here.
3176 		 */
3177 		if (data.ielen + 3 + sizeof(*ml_elem) + ml_common_len >
3178 		    IEEE80211_MAX_DATA_LEN)
3179 			continue;
3180 
3181 		/* Copy the Basic Multi-Link element including the common
3182 		 * information, and then fix up the link ID and BSS param
3183 		 * change count.
3184 		 * Note that the ML element length has been verified and we
3185 		 * also checked that it contains the link ID.
3186 		 */
3187 		new_ie[data.ielen++] = WLAN_EID_EXTENSION;
3188 		new_ie[data.ielen++] = 1 + sizeof(*ml_elem) + ml_common_len;
3189 		new_ie[data.ielen++] = WLAN_EID_EXT_EHT_MULTI_LINK;
3190 		memcpy(new_ie + data.ielen, ml_elem,
3191 		       sizeof(*ml_elem) + ml_common_len);
3192 
3193 		new_ie[data.ielen + sizeof(*ml_elem) + 1 + ETH_ALEN] = link_id;
3194 		new_ie[data.ielen + sizeof(*ml_elem) + 1 + ETH_ALEN + 1] =
3195 			param_ch_count;
3196 
3197 		data.ielen += sizeof(*ml_elem) + ml_common_len;
3198 
3199 		if (reporter_rnr && (use_for & NL80211_BSS_USE_FOR_NORMAL)) {
3200 			if (data.ielen + sizeof(struct element) +
3201 			    reporter_rnr->datalen > IEEE80211_MAX_DATA_LEN)
3202 				continue;
3203 
3204 			memcpy(new_ie + data.ielen, reporter_rnr,
3205 			       sizeof(struct element) + reporter_rnr->datalen);
3206 			data.ielen += sizeof(struct element) +
3207 				      reporter_rnr->datalen;
3208 		}
3209 
3210 		bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp);
3211 		if (!bss)
3212 			break;
3213 		cfg80211_put_bss(wiphy, bss);
3214 	}
3215 
3216 out:
3217 	kfree(reporter_rnr);
3218 	kfree(new_ie);
3219 	kfree(mle);
3220 }
3221 
3222 static void cfg80211_parse_ml_sta_data(struct wiphy *wiphy,
3223 				       struct cfg80211_inform_single_bss_data *tx_data,
3224 				       struct cfg80211_bss *source_bss,
3225 				       gfp_t gfp)
3226 {
3227 	const struct element *elem;
3228 
3229 	if (!source_bss)
3230 		return;
3231 
3232 	if (tx_data->ftype != CFG80211_BSS_FTYPE_PRESP)
3233 		return;
3234 
3235 	for_each_element_extid(elem, WLAN_EID_EXT_EHT_MULTI_LINK,
3236 			       tx_data->ie, tx_data->ielen)
3237 		cfg80211_parse_ml_elem_sta_data(wiphy, tx_data, source_bss,
3238 						elem, gfp);
3239 }
3240 
3241 struct cfg80211_bss *
3242 cfg80211_inform_bss_data(struct wiphy *wiphy,
3243 			 struct cfg80211_inform_bss *data,
3244 			 enum cfg80211_bss_frame_type ftype,
3245 			 const u8 *bssid, u64 tsf, u16 capability,
3246 			 u16 beacon_interval, const u8 *ie, size_t ielen,
3247 			 gfp_t gfp)
3248 {
3249 	struct cfg80211_inform_single_bss_data inform_data = {
3250 		.drv_data = data,
3251 		.ftype = ftype,
3252 		.tsf = tsf,
3253 		.capability = capability,
3254 		.beacon_interval = beacon_interval,
3255 		.ie = ie,
3256 		.ielen = ielen,
3257 		.use_for = data->restrict_use ?
3258 				data->use_for :
3259 				NL80211_BSS_USE_FOR_ALL,
3260 		.cannot_use_reasons = data->cannot_use_reasons,
3261 	};
3262 	struct cfg80211_bss *res;
3263 
3264 	memcpy(inform_data.bssid, bssid, ETH_ALEN);
3265 
3266 	res = cfg80211_inform_single_bss_data(wiphy, &inform_data, gfp);
3267 	if (!res)
3268 		return NULL;
3269 
3270 	/* don't do any further MBSSID/ML handling for S1G */
3271 	if (ftype == CFG80211_BSS_FTYPE_S1G_BEACON)
3272 		return res;
3273 
3274 	cfg80211_parse_mbssid_data(wiphy, &inform_data, res, gfp);
3275 
3276 	cfg80211_parse_ml_sta_data(wiphy, &inform_data, res, gfp);
3277 
3278 	return res;
3279 }
3280 EXPORT_SYMBOL(cfg80211_inform_bss_data);
3281 
3282 struct cfg80211_bss *
3283 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
3284 			       struct cfg80211_inform_bss *data,
3285 			       struct ieee80211_mgmt *mgmt, size_t len,
3286 			       gfp_t gfp)
3287 {
3288 	size_t min_hdr_len;
3289 	struct ieee80211_ext *ext = NULL;
3290 	enum cfg80211_bss_frame_type ftype;
3291 	u16 beacon_interval;
3292 	const u8 *bssid;
3293 	u16 capability;
3294 	const u8 *ie;
3295 	size_t ielen;
3296 	u64 tsf;
3297 	size_t s1g_optional_len;
3298 
3299 	if (WARN_ON(!mgmt))
3300 		return NULL;
3301 
3302 	if (WARN_ON(!wiphy))
3303 		return NULL;
3304 
3305 	BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
3306 		     offsetof(struct ieee80211_mgmt, u.beacon.variable));
3307 
3308 	trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
3309 
3310 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
3311 		ext = (void *) mgmt;
3312 		s1g_optional_len =
3313 			ieee80211_s1g_optional_len(ext->frame_control);
3314 		min_hdr_len =
3315 			offsetof(struct ieee80211_ext, u.s1g_beacon.variable) +
3316 			s1g_optional_len;
3317 	} else {
3318 		/* same for beacons */
3319 		min_hdr_len = offsetof(struct ieee80211_mgmt,
3320 				       u.probe_resp.variable);
3321 	}
3322 
3323 	if (WARN_ON(len < min_hdr_len))
3324 		return NULL;
3325 
3326 	ielen = len - min_hdr_len;
3327 	ie = mgmt->u.probe_resp.variable;
3328 	if (ext) {
3329 		const struct ieee80211_s1g_bcn_compat_ie *compat;
3330 		const struct element *elem;
3331 
3332 		ie = ext->u.s1g_beacon.variable + s1g_optional_len;
3333 		elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT, ie, ielen);
3334 		if (!elem)
3335 			return NULL;
3336 		if (elem->datalen < sizeof(*compat))
3337 			return NULL;
3338 		compat = (void *)elem->data;
3339 		bssid = ext->u.s1g_beacon.sa;
3340 		capability = le16_to_cpu(compat->compat_info);
3341 		beacon_interval = le16_to_cpu(compat->beacon_int);
3342 		tsf = le32_to_cpu(ext->u.s1g_beacon.timestamp);
3343 		tsf |= (u64)le32_to_cpu(compat->tsf_completion) << 32;
3344 	} else {
3345 		bssid = mgmt->bssid;
3346 		beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
3347 		capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
3348 		tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
3349 	}
3350 
3351 	if (ieee80211_is_probe_resp(mgmt->frame_control))
3352 		ftype = CFG80211_BSS_FTYPE_PRESP;
3353 	else if (ext)
3354 		ftype = CFG80211_BSS_FTYPE_S1G_BEACON;
3355 	else
3356 		ftype = CFG80211_BSS_FTYPE_BEACON;
3357 
3358 	return cfg80211_inform_bss_data(wiphy, data, ftype,
3359 					bssid, tsf, capability,
3360 					beacon_interval, ie, ielen,
3361 					gfp);
3362 }
3363 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
3364 
3365 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
3366 {
3367 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3368 
3369 	if (!pub)
3370 		return;
3371 
3372 	spin_lock_bh(&rdev->bss_lock);
3373 	bss_ref_get(rdev, bss_from_pub(pub));
3374 	spin_unlock_bh(&rdev->bss_lock);
3375 }
3376 EXPORT_SYMBOL(cfg80211_ref_bss);
3377 
3378 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
3379 {
3380 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3381 
3382 	if (!pub)
3383 		return;
3384 
3385 	spin_lock_bh(&rdev->bss_lock);
3386 	bss_ref_put(rdev, bss_from_pub(pub));
3387 	spin_unlock_bh(&rdev->bss_lock);
3388 }
3389 EXPORT_SYMBOL(cfg80211_put_bss);
3390 
3391 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
3392 {
3393 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3394 	struct cfg80211_internal_bss *bss, *tmp1;
3395 	struct cfg80211_bss *nontrans_bss, *tmp;
3396 
3397 	if (WARN_ON(!pub))
3398 		return;
3399 
3400 	bss = bss_from_pub(pub);
3401 
3402 	spin_lock_bh(&rdev->bss_lock);
3403 	if (list_empty(&bss->list))
3404 		goto out;
3405 
3406 	list_for_each_entry_safe(nontrans_bss, tmp,
3407 				 &pub->nontrans_list,
3408 				 nontrans_list) {
3409 		tmp1 = bss_from_pub(nontrans_bss);
3410 		if (__cfg80211_unlink_bss(rdev, tmp1))
3411 			rdev->bss_generation++;
3412 	}
3413 
3414 	if (__cfg80211_unlink_bss(rdev, bss))
3415 		rdev->bss_generation++;
3416 out:
3417 	spin_unlock_bh(&rdev->bss_lock);
3418 }
3419 EXPORT_SYMBOL(cfg80211_unlink_bss);
3420 
3421 void cfg80211_bss_iter(struct wiphy *wiphy,
3422 		       struct cfg80211_chan_def *chandef,
3423 		       void (*iter)(struct wiphy *wiphy,
3424 				    struct cfg80211_bss *bss,
3425 				    void *data),
3426 		       void *iter_data)
3427 {
3428 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3429 	struct cfg80211_internal_bss *bss;
3430 
3431 	spin_lock_bh(&rdev->bss_lock);
3432 
3433 	list_for_each_entry(bss, &rdev->bss_list, list) {
3434 		if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
3435 						     false))
3436 			iter(wiphy, &bss->pub, iter_data);
3437 	}
3438 
3439 	spin_unlock_bh(&rdev->bss_lock);
3440 }
3441 EXPORT_SYMBOL(cfg80211_bss_iter);
3442 
3443 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
3444 				     unsigned int link_id,
3445 				     struct ieee80211_channel *chan)
3446 {
3447 	struct wiphy *wiphy = wdev->wiphy;
3448 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3449 	struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
3450 	struct cfg80211_internal_bss *new = NULL;
3451 	struct cfg80211_internal_bss *bss;
3452 	struct cfg80211_bss *nontrans_bss;
3453 	struct cfg80211_bss *tmp;
3454 
3455 	spin_lock_bh(&rdev->bss_lock);
3456 
3457 	/*
3458 	 * Some APs use CSA also for bandwidth changes, i.e., without actually
3459 	 * changing the control channel, so no need to update in such a case.
3460 	 */
3461 	if (cbss->pub.channel == chan)
3462 		goto done;
3463 
3464 	/* use transmitting bss */
3465 	if (cbss->pub.transmitted_bss)
3466 		cbss = bss_from_pub(cbss->pub.transmitted_bss);
3467 
3468 	cbss->pub.channel = chan;
3469 
3470 	list_for_each_entry(bss, &rdev->bss_list, list) {
3471 		if (!cfg80211_bss_type_match(bss->pub.capability,
3472 					     bss->pub.channel->band,
3473 					     wdev->conn_bss_type))
3474 			continue;
3475 
3476 		if (bss == cbss)
3477 			continue;
3478 
3479 		if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
3480 			new = bss;
3481 			break;
3482 		}
3483 	}
3484 
3485 	if (new) {
3486 		/* to save time, update IEs for transmitting bss only */
3487 		cfg80211_update_known_bss(rdev, cbss, new, false);
3488 		new->pub.proberesp_ies = NULL;
3489 		new->pub.beacon_ies = NULL;
3490 
3491 		list_for_each_entry_safe(nontrans_bss, tmp,
3492 					 &new->pub.nontrans_list,
3493 					 nontrans_list) {
3494 			bss = bss_from_pub(nontrans_bss);
3495 			if (__cfg80211_unlink_bss(rdev, bss))
3496 				rdev->bss_generation++;
3497 		}
3498 
3499 		WARN_ON(atomic_read(&new->hold));
3500 		if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
3501 			rdev->bss_generation++;
3502 	}
3503 	cfg80211_rehash_bss(rdev, cbss);
3504 
3505 	list_for_each_entry_safe(nontrans_bss, tmp,
3506 				 &cbss->pub.nontrans_list,
3507 				 nontrans_list) {
3508 		bss = bss_from_pub(nontrans_bss);
3509 		bss->pub.channel = chan;
3510 		cfg80211_rehash_bss(rdev, bss);
3511 	}
3512 
3513 done:
3514 	spin_unlock_bh(&rdev->bss_lock);
3515 }
3516 
3517 #ifdef CONFIG_CFG80211_WEXT
3518 static struct cfg80211_registered_device *
3519 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
3520 {
3521 	struct cfg80211_registered_device *rdev;
3522 	struct net_device *dev;
3523 
3524 	ASSERT_RTNL();
3525 
3526 	dev = dev_get_by_index(net, ifindex);
3527 	if (!dev)
3528 		return ERR_PTR(-ENODEV);
3529 	if (dev->ieee80211_ptr)
3530 		rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
3531 	else
3532 		rdev = ERR_PTR(-ENODEV);
3533 	dev_put(dev);
3534 	return rdev;
3535 }
3536 
3537 int cfg80211_wext_siwscan(struct net_device *dev,
3538 			  struct iw_request_info *info,
3539 			  union iwreq_data *wrqu, char *extra)
3540 {
3541 	struct cfg80211_registered_device *rdev;
3542 	struct wiphy *wiphy;
3543 	struct iw_scan_req *wreq = NULL;
3544 	struct cfg80211_scan_request_int *creq;
3545 	int i, err, n_channels = 0;
3546 	enum nl80211_band band;
3547 
3548 	if (!netif_running(dev))
3549 		return -ENETDOWN;
3550 
3551 	if (wrqu->data.length == sizeof(struct iw_scan_req))
3552 		wreq = (struct iw_scan_req *)extra;
3553 
3554 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3555 
3556 	if (IS_ERR(rdev))
3557 		return PTR_ERR(rdev);
3558 
3559 	if (rdev->scan_req || rdev->scan_msg)
3560 		return -EBUSY;
3561 
3562 	wiphy = &rdev->wiphy;
3563 
3564 	/* Determine number of channels, needed to allocate creq */
3565 	if (wreq && wreq->num_channels) {
3566 		/* Passed from userspace so should be checked */
3567 		if (unlikely(wreq->num_channels > IW_MAX_FREQUENCIES))
3568 			return -EINVAL;
3569 		n_channels = wreq->num_channels;
3570 	} else {
3571 		n_channels = ieee80211_get_num_supported_channels(wiphy);
3572 	}
3573 
3574 	creq = kzalloc(struct_size(creq, req.channels, n_channels) +
3575 		       sizeof(struct cfg80211_ssid),
3576 		       GFP_ATOMIC);
3577 	if (!creq)
3578 		return -ENOMEM;
3579 
3580 	creq->req.wiphy = wiphy;
3581 	creq->req.wdev = dev->ieee80211_ptr;
3582 	/* SSIDs come after channels */
3583 	creq->req.ssids = (void *)creq +
3584 			  struct_size(creq, req.channels, n_channels);
3585 	creq->req.n_channels = n_channels;
3586 	creq->req.n_ssids = 1;
3587 	creq->req.scan_start = jiffies;
3588 
3589 	/* translate "Scan on frequencies" request */
3590 	i = 0;
3591 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3592 		int j;
3593 
3594 		if (!wiphy->bands[band])
3595 			continue;
3596 
3597 		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3598 			struct ieee80211_channel *chan;
3599 
3600 			/* ignore disabled channels */
3601 			chan = &wiphy->bands[band]->channels[j];
3602 			if (chan->flags & IEEE80211_CHAN_DISABLED ||
3603 			    !cfg80211_wdev_channel_allowed(creq->req.wdev, chan))
3604 				continue;
3605 
3606 			/* If we have a wireless request structure and the
3607 			 * wireless request specifies frequencies, then search
3608 			 * for the matching hardware channel.
3609 			 */
3610 			if (wreq && wreq->num_channels) {
3611 				int k;
3612 				int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
3613 				for (k = 0; k < wreq->num_channels; k++) {
3614 					struct iw_freq *freq =
3615 						&wreq->channel_list[k];
3616 					int wext_freq =
3617 						cfg80211_wext_freq(freq);
3618 
3619 					if (wext_freq == wiphy_freq)
3620 						goto wext_freq_found;
3621 				}
3622 				goto wext_freq_not_found;
3623 			}
3624 
3625 		wext_freq_found:
3626 			creq->req.channels[i] =
3627 				&wiphy->bands[band]->channels[j];
3628 			i++;
3629 		wext_freq_not_found: ;
3630 		}
3631 	}
3632 	/* No channels found? */
3633 	if (!i) {
3634 		err = -EINVAL;
3635 		goto out;
3636 	}
3637 
3638 	/* Set real number of channels specified in creq->req.channels[] */
3639 	creq->req.n_channels = i;
3640 
3641 	/* translate "Scan for SSID" request */
3642 	if (wreq) {
3643 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
3644 			if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
3645 				err = -EINVAL;
3646 				goto out;
3647 			}
3648 			memcpy(creq->req.ssids[0].ssid, wreq->essid,
3649 			       wreq->essid_len);
3650 			creq->req.ssids[0].ssid_len = wreq->essid_len;
3651 		}
3652 		if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) {
3653 			creq->req.ssids = NULL;
3654 			creq->req.n_ssids = 0;
3655 		}
3656 	}
3657 
3658 	for (i = 0; i < NUM_NL80211_BANDS; i++)
3659 		if (wiphy->bands[i])
3660 			creq->req.rates[i] =
3661 				(1 << wiphy->bands[i]->n_bitrates) - 1;
3662 
3663 	eth_broadcast_addr(creq->req.bssid);
3664 
3665 	scoped_guard(wiphy, &rdev->wiphy) {
3666 		rdev->scan_req = creq;
3667 		err = rdev_scan(rdev, creq);
3668 		if (err) {
3669 			rdev->scan_req = NULL;
3670 			/* creq will be freed below */
3671 		} else {
3672 			nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
3673 			/* creq now owned by driver */
3674 			creq = NULL;
3675 			dev_hold(dev);
3676 		}
3677 	}
3678 
3679  out:
3680 	kfree(creq);
3681 	return err;
3682 }
3683 
3684 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
3685 				    const struct cfg80211_bss_ies *ies,
3686 				    char *current_ev, char *end_buf)
3687 {
3688 	const u8 *pos, *end, *next;
3689 	struct iw_event iwe;
3690 
3691 	if (!ies)
3692 		return current_ev;
3693 
3694 	/*
3695 	 * If needed, fragment the IEs buffer (at IE boundaries) into short
3696 	 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
3697 	 */
3698 	pos = ies->data;
3699 	end = pos + ies->len;
3700 
3701 	while (end - pos > IW_GENERIC_IE_MAX) {
3702 		next = pos + 2 + pos[1];
3703 		while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
3704 			next = next + 2 + next[1];
3705 
3706 		memset(&iwe, 0, sizeof(iwe));
3707 		iwe.cmd = IWEVGENIE;
3708 		iwe.u.data.length = next - pos;
3709 		current_ev = iwe_stream_add_point_check(info, current_ev,
3710 							end_buf, &iwe,
3711 							(void *)pos);
3712 		if (IS_ERR(current_ev))
3713 			return current_ev;
3714 		pos = next;
3715 	}
3716 
3717 	if (end > pos) {
3718 		memset(&iwe, 0, sizeof(iwe));
3719 		iwe.cmd = IWEVGENIE;
3720 		iwe.u.data.length = end - pos;
3721 		current_ev = iwe_stream_add_point_check(info, current_ev,
3722 							end_buf, &iwe,
3723 							(void *)pos);
3724 		if (IS_ERR(current_ev))
3725 			return current_ev;
3726 	}
3727 
3728 	return current_ev;
3729 }
3730 
3731 static char *
3732 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
3733 	      struct cfg80211_internal_bss *bss, char *current_ev,
3734 	      char *end_buf)
3735 {
3736 	const struct cfg80211_bss_ies *ies;
3737 	struct iw_event iwe;
3738 	const u8 *ie;
3739 	u8 buf[50];
3740 	u8 *cfg, *p, *tmp;
3741 	int rem, i, sig;
3742 	bool ismesh = false;
3743 
3744 	memset(&iwe, 0, sizeof(iwe));
3745 	iwe.cmd = SIOCGIWAP;
3746 	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3747 	memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
3748 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
3749 						IW_EV_ADDR_LEN);
3750 	if (IS_ERR(current_ev))
3751 		return current_ev;
3752 
3753 	memset(&iwe, 0, sizeof(iwe));
3754 	iwe.cmd = SIOCGIWFREQ;
3755 	iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
3756 	iwe.u.freq.e = 0;
3757 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
3758 						IW_EV_FREQ_LEN);
3759 	if (IS_ERR(current_ev))
3760 		return current_ev;
3761 
3762 	memset(&iwe, 0, sizeof(iwe));
3763 	iwe.cmd = SIOCGIWFREQ;
3764 	iwe.u.freq.m = bss->pub.channel->center_freq;
3765 	iwe.u.freq.e = 6;
3766 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
3767 						IW_EV_FREQ_LEN);
3768 	if (IS_ERR(current_ev))
3769 		return current_ev;
3770 
3771 	if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
3772 		memset(&iwe, 0, sizeof(iwe));
3773 		iwe.cmd = IWEVQUAL;
3774 		iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
3775 				     IW_QUAL_NOISE_INVALID |
3776 				     IW_QUAL_QUAL_UPDATED;
3777 		switch (wiphy->signal_type) {
3778 		case CFG80211_SIGNAL_TYPE_MBM:
3779 			sig = bss->pub.signal / 100;
3780 			iwe.u.qual.level = sig;
3781 			iwe.u.qual.updated |= IW_QUAL_DBM;
3782 			if (sig < -110)		/* rather bad */
3783 				sig = -110;
3784 			else if (sig > -40)	/* perfect */
3785 				sig = -40;
3786 			/* will give a range of 0 .. 70 */
3787 			iwe.u.qual.qual = sig + 110;
3788 			break;
3789 		case CFG80211_SIGNAL_TYPE_UNSPEC:
3790 			iwe.u.qual.level = bss->pub.signal;
3791 			/* will give range 0 .. 100 */
3792 			iwe.u.qual.qual = bss->pub.signal;
3793 			break;
3794 		default:
3795 			/* not reached */
3796 			break;
3797 		}
3798 		current_ev = iwe_stream_add_event_check(info, current_ev,
3799 							end_buf, &iwe,
3800 							IW_EV_QUAL_LEN);
3801 		if (IS_ERR(current_ev))
3802 			return current_ev;
3803 	}
3804 
3805 	memset(&iwe, 0, sizeof(iwe));
3806 	iwe.cmd = SIOCGIWENCODE;
3807 	if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
3808 		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3809 	else
3810 		iwe.u.data.flags = IW_ENCODE_DISABLED;
3811 	iwe.u.data.length = 0;
3812 	current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3813 						&iwe, "");
3814 	if (IS_ERR(current_ev))
3815 		return current_ev;
3816 
3817 	rcu_read_lock();
3818 	ies = rcu_dereference(bss->pub.ies);
3819 	rem = ies->len;
3820 	ie = ies->data;
3821 
3822 	while (rem >= 2) {
3823 		/* invalid data */
3824 		if (ie[1] > rem - 2)
3825 			break;
3826 
3827 		switch (ie[0]) {
3828 		case WLAN_EID_SSID:
3829 			memset(&iwe, 0, sizeof(iwe));
3830 			iwe.cmd = SIOCGIWESSID;
3831 			iwe.u.data.length = ie[1];
3832 			iwe.u.data.flags = 1;
3833 			current_ev = iwe_stream_add_point_check(info,
3834 								current_ev,
3835 								end_buf, &iwe,
3836 								(u8 *)ie + 2);
3837 			if (IS_ERR(current_ev))
3838 				goto unlock;
3839 			break;
3840 		case WLAN_EID_MESH_ID:
3841 			memset(&iwe, 0, sizeof(iwe));
3842 			iwe.cmd = SIOCGIWESSID;
3843 			iwe.u.data.length = ie[1];
3844 			iwe.u.data.flags = 1;
3845 			current_ev = iwe_stream_add_point_check(info,
3846 								current_ev,
3847 								end_buf, &iwe,
3848 								(u8 *)ie + 2);
3849 			if (IS_ERR(current_ev))
3850 				goto unlock;
3851 			break;
3852 		case WLAN_EID_MESH_CONFIG:
3853 			ismesh = true;
3854 			if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
3855 				break;
3856 			cfg = (u8 *)ie + 2;
3857 			memset(&iwe, 0, sizeof(iwe));
3858 			iwe.cmd = IWEVCUSTOM;
3859 			iwe.u.data.length = sprintf(buf,
3860 						    "Mesh Network Path Selection Protocol ID: 0x%02X",
3861 						    cfg[0]);
3862 			current_ev = iwe_stream_add_point_check(info,
3863 								current_ev,
3864 								end_buf,
3865 								&iwe, buf);
3866 			if (IS_ERR(current_ev))
3867 				goto unlock;
3868 			iwe.u.data.length = sprintf(buf,
3869 						    "Path Selection Metric ID: 0x%02X",
3870 						    cfg[1]);
3871 			current_ev = iwe_stream_add_point_check(info,
3872 								current_ev,
3873 								end_buf,
3874 								&iwe, buf);
3875 			if (IS_ERR(current_ev))
3876 				goto unlock;
3877 			iwe.u.data.length = sprintf(buf,
3878 						    "Congestion Control Mode ID: 0x%02X",
3879 						    cfg[2]);
3880 			current_ev = iwe_stream_add_point_check(info,
3881 								current_ev,
3882 								end_buf,
3883 								&iwe, buf);
3884 			if (IS_ERR(current_ev))
3885 				goto unlock;
3886 			iwe.u.data.length = sprintf(buf,
3887 						    "Synchronization ID: 0x%02X",
3888 						    cfg[3]);
3889 			current_ev = iwe_stream_add_point_check(info,
3890 								current_ev,
3891 								end_buf,
3892 								&iwe, buf);
3893 			if (IS_ERR(current_ev))
3894 				goto unlock;
3895 			iwe.u.data.length = sprintf(buf,
3896 						    "Authentication ID: 0x%02X",
3897 						    cfg[4]);
3898 			current_ev = iwe_stream_add_point_check(info,
3899 								current_ev,
3900 								end_buf,
3901 								&iwe, buf);
3902 			if (IS_ERR(current_ev))
3903 				goto unlock;
3904 			iwe.u.data.length = sprintf(buf,
3905 						    "Formation Info: 0x%02X",
3906 						    cfg[5]);
3907 			current_ev = iwe_stream_add_point_check(info,
3908 								current_ev,
3909 								end_buf,
3910 								&iwe, buf);
3911 			if (IS_ERR(current_ev))
3912 				goto unlock;
3913 			iwe.u.data.length = sprintf(buf,
3914 						    "Capabilities: 0x%02X",
3915 						    cfg[6]);
3916 			current_ev = iwe_stream_add_point_check(info,
3917 								current_ev,
3918 								end_buf,
3919 								&iwe, buf);
3920 			if (IS_ERR(current_ev))
3921 				goto unlock;
3922 			break;
3923 		case WLAN_EID_SUPP_RATES:
3924 		case WLAN_EID_EXT_SUPP_RATES:
3925 			/* display all supported rates in readable format */
3926 			p = current_ev + iwe_stream_lcp_len(info);
3927 
3928 			memset(&iwe, 0, sizeof(iwe));
3929 			iwe.cmd = SIOCGIWRATE;
3930 			/* Those two flags are ignored... */
3931 			iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3932 
3933 			for (i = 0; i < ie[1]; i++) {
3934 				iwe.u.bitrate.value =
3935 					((ie[i + 2] & 0x7f) * 500000);
3936 				tmp = p;
3937 				p = iwe_stream_add_value(info, current_ev, p,
3938 							 end_buf, &iwe,
3939 							 IW_EV_PARAM_LEN);
3940 				if (p == tmp) {
3941 					current_ev = ERR_PTR(-E2BIG);
3942 					goto unlock;
3943 				}
3944 			}
3945 			current_ev = p;
3946 			break;
3947 		}
3948 		rem -= ie[1] + 2;
3949 		ie += ie[1] + 2;
3950 	}
3951 
3952 	if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3953 	    ismesh) {
3954 		memset(&iwe, 0, sizeof(iwe));
3955 		iwe.cmd = SIOCGIWMODE;
3956 		if (ismesh)
3957 			iwe.u.mode = IW_MODE_MESH;
3958 		else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3959 			iwe.u.mode = IW_MODE_MASTER;
3960 		else
3961 			iwe.u.mode = IW_MODE_ADHOC;
3962 		current_ev = iwe_stream_add_event_check(info, current_ev,
3963 							end_buf, &iwe,
3964 							IW_EV_UINT_LEN);
3965 		if (IS_ERR(current_ev))
3966 			goto unlock;
3967 	}
3968 
3969 	memset(&iwe, 0, sizeof(iwe));
3970 	iwe.cmd = IWEVCUSTOM;
3971 	iwe.u.data.length = sprintf(buf, "tsf=%016llx",
3972 				    (unsigned long long)(ies->tsf));
3973 	current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3974 						&iwe, buf);
3975 	if (IS_ERR(current_ev))
3976 		goto unlock;
3977 	memset(&iwe, 0, sizeof(iwe));
3978 	iwe.cmd = IWEVCUSTOM;
3979 	iwe.u.data.length = sprintf(buf, " Last beacon: %ums ago",
3980 				    elapsed_jiffies_msecs(bss->ts));
3981 	current_ev = iwe_stream_add_point_check(info, current_ev,
3982 						end_buf, &iwe, buf);
3983 	if (IS_ERR(current_ev))
3984 		goto unlock;
3985 
3986 	current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3987 
3988  unlock:
3989 	rcu_read_unlock();
3990 	return current_ev;
3991 }
3992 
3993 
3994 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3995 				  struct iw_request_info *info,
3996 				  char *buf, size_t len)
3997 {
3998 	char *current_ev = buf;
3999 	char *end_buf = buf + len;
4000 	struct cfg80211_internal_bss *bss;
4001 	int err = 0;
4002 
4003 	spin_lock_bh(&rdev->bss_lock);
4004 	cfg80211_bss_expire(rdev);
4005 
4006 	list_for_each_entry(bss, &rdev->bss_list, list) {
4007 		if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4008 			err = -E2BIG;
4009 			break;
4010 		}
4011 		current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
4012 					   current_ev, end_buf);
4013 		if (IS_ERR(current_ev)) {
4014 			err = PTR_ERR(current_ev);
4015 			break;
4016 		}
4017 	}
4018 	spin_unlock_bh(&rdev->bss_lock);
4019 
4020 	if (err)
4021 		return err;
4022 	return current_ev - buf;
4023 }
4024 
4025 
4026 int cfg80211_wext_giwscan(struct net_device *dev,
4027 			  struct iw_request_info *info,
4028 			  union iwreq_data *wrqu, char *extra)
4029 {
4030 	struct iw_point *data = &wrqu->data;
4031 	struct cfg80211_registered_device *rdev;
4032 	int res;
4033 
4034 	if (!netif_running(dev))
4035 		return -ENETDOWN;
4036 
4037 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
4038 
4039 	if (IS_ERR(rdev))
4040 		return PTR_ERR(rdev);
4041 
4042 	if (rdev->scan_req || rdev->scan_msg)
4043 		return -EAGAIN;
4044 
4045 	res = ieee80211_scan_results(rdev, info, extra, data->length);
4046 	data->length = 0;
4047 	if (res >= 0) {
4048 		data->length = res;
4049 		res = 0;
4050 	}
4051 
4052 	return res;
4053 }
4054 #endif
4055