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