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