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