xref: /openbmc/linux/net/wireless/scan.c (revision 97d5f2e9)
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-2023 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 "core.h"
24 #include "nl80211.h"
25 #include "wext-compat.h"
26 #include "rdev-ops.h"
27 
28 /**
29  * DOC: BSS tree/list structure
30  *
31  * At the top level, the BSS list is kept in both a list in each
32  * registered device (@bss_list) as well as an RB-tree for faster
33  * lookup. In the RB-tree, entries can be looked up using their
34  * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
35  * for other BSSes.
36  *
37  * Due to the possibility of hidden SSIDs, there's a second level
38  * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
39  * The hidden_list connects all BSSes belonging to a single AP
40  * that has a hidden SSID, and connects beacon and probe response
41  * entries. For a probe response entry for a hidden SSID, the
42  * hidden_beacon_bss pointer points to the BSS struct holding the
43  * beacon's information.
44  *
45  * Reference counting is done for all these references except for
46  * the hidden_list, so that a beacon BSS struct that is otherwise
47  * not referenced has one reference for being on the bss_list and
48  * one for each probe response entry that points to it using the
49  * hidden_beacon_bss pointer. When a BSS struct that has such a
50  * pointer is get/put, the refcount update is also propagated to
51  * the referenced struct, this ensure that it cannot get removed
52  * while somebody is using the probe response version.
53  *
54  * Note that the hidden_beacon_bss pointer never changes, due to
55  * the reference counting. Therefore, no locking is needed for
56  * it.
57  *
58  * Also note that the hidden_beacon_bss pointer is only relevant
59  * if the driver uses something other than the IEs, e.g. private
60  * data stored in the BSS struct, since the beacon IEs are
61  * also linked into the probe response struct.
62  */
63 
64 /*
65  * Limit the number of BSS entries stored in mac80211. Each one is
66  * a bit over 4k at most, so this limits to roughly 4-5M of memory.
67  * If somebody wants to really attack this though, they'd likely
68  * use small beacons, and only one type of frame, limiting each of
69  * the entries to a much smaller size (in order to generate more
70  * entries in total, so overhead is bigger.)
71  */
72 static int bss_entries_limit = 1000;
73 module_param(bss_entries_limit, int, 0644);
74 MODULE_PARM_DESC(bss_entries_limit,
75                  "limit to number of scan BSS entries (per wiphy, default 1000)");
76 
77 #define IEEE80211_SCAN_RESULT_EXPIRE	(30 * HZ)
78 
79 /**
80  * struct cfg80211_colocated_ap - colocated AP information
81  *
82  * @list: linked list to all colocated aPS
83  * @bssid: BSSID of the reported AP
84  * @ssid: SSID of the reported AP
85  * @ssid_len: length of the ssid
86  * @center_freq: frequency the reported AP is on
87  * @unsolicited_probe: the reported AP is part of an ESS, where all the APs
88  *	that operate in the same channel as the reported AP and that might be
89  *	detected by a STA receiving this frame, are transmitting unsolicited
90  *	Probe Response frames every 20 TUs
91  * @oct_recommended: OCT is recommended to exchange MMPDUs with the reported AP
92  * @same_ssid: the reported AP has the same SSID as the reporting AP
93  * @multi_bss: the reported AP is part of a multiple BSSID set
94  * @transmitted_bssid: the reported AP is the transmitting BSSID
95  * @colocated_ess: all the APs that share the same ESS as the reported AP are
96  *	colocated and can be discovered via legacy bands.
97  * @short_ssid_valid: short_ssid is valid and can be used
98  * @short_ssid: the short SSID for this SSID
99  */
100 struct cfg80211_colocated_ap {
101 	struct list_head list;
102 	u8 bssid[ETH_ALEN];
103 	u8 ssid[IEEE80211_MAX_SSID_LEN];
104 	size_t ssid_len;
105 	u32 short_ssid;
106 	u32 center_freq;
107 	u8 unsolicited_probe:1,
108 	   oct_recommended:1,
109 	   same_ssid:1,
110 	   multi_bss:1,
111 	   transmitted_bssid:1,
112 	   colocated_ess:1,
113 	   short_ssid_valid:1;
114 };
115 
116 static void bss_free(struct cfg80211_internal_bss *bss)
117 {
118 	struct cfg80211_bss_ies *ies;
119 
120 	if (WARN_ON(atomic_read(&bss->hold)))
121 		return;
122 
123 	ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
124 	if (ies && !bss->pub.hidden_beacon_bss)
125 		kfree_rcu(ies, rcu_head);
126 	ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
127 	if (ies)
128 		kfree_rcu(ies, rcu_head);
129 
130 	/*
131 	 * This happens when the module is removed, it doesn't
132 	 * really matter any more save for completeness
133 	 */
134 	if (!list_empty(&bss->hidden_list))
135 		list_del(&bss->hidden_list);
136 
137 	kfree(bss);
138 }
139 
140 static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
141 			       struct cfg80211_internal_bss *bss)
142 {
143 	lockdep_assert_held(&rdev->bss_lock);
144 
145 	bss->refcount++;
146 
147 	if (bss->pub.hidden_beacon_bss)
148 		bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
149 
150 	if (bss->pub.transmitted_bss)
151 		bss_from_pub(bss->pub.transmitted_bss)->refcount++;
152 }
153 
154 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
155 			       struct cfg80211_internal_bss *bss)
156 {
157 	lockdep_assert_held(&rdev->bss_lock);
158 
159 	if (bss->pub.hidden_beacon_bss) {
160 		struct cfg80211_internal_bss *hbss;
161 
162 		hbss = bss_from_pub(bss->pub.hidden_beacon_bss);
163 		hbss->refcount--;
164 		if (hbss->refcount == 0)
165 			bss_free(hbss);
166 	}
167 
168 	if (bss->pub.transmitted_bss) {
169 		struct cfg80211_internal_bss *tbss;
170 
171 		tbss = bss_from_pub(bss->pub.transmitted_bss);
172 		tbss->refcount--;
173 		if (tbss->refcount == 0)
174 			bss_free(tbss);
175 	}
176 
177 	bss->refcount--;
178 	if (bss->refcount == 0)
179 		bss_free(bss);
180 }
181 
182 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
183 				  struct cfg80211_internal_bss *bss)
184 {
185 	lockdep_assert_held(&rdev->bss_lock);
186 
187 	if (!list_empty(&bss->hidden_list)) {
188 		/*
189 		 * don't remove the beacon entry if it has
190 		 * probe responses associated with it
191 		 */
192 		if (!bss->pub.hidden_beacon_bss)
193 			return false;
194 		/*
195 		 * if it's a probe response entry break its
196 		 * link to the other entries in the group
197 		 */
198 		list_del_init(&bss->hidden_list);
199 	}
200 
201 	list_del_init(&bss->list);
202 	list_del_init(&bss->pub.nontrans_list);
203 	rb_erase(&bss->rbn, &rdev->bss_tree);
204 	rdev->bss_entries--;
205 	WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
206 		  "rdev bss entries[%d]/list[empty:%d] corruption\n",
207 		  rdev->bss_entries, list_empty(&rdev->bss_list));
208 	bss_ref_put(rdev, bss);
209 	return true;
210 }
211 
212 bool cfg80211_is_element_inherited(const struct element *elem,
213 				   const struct element *non_inherit_elem)
214 {
215 	u8 id_len, ext_id_len, i, loop_len, id;
216 	const u8 *list;
217 
218 	if (elem->id == WLAN_EID_MULTIPLE_BSSID)
219 		return false;
220 
221 	if (!non_inherit_elem || non_inherit_elem->datalen < 2)
222 		return true;
223 
224 	/*
225 	 * non inheritance element format is:
226 	 * ext ID (56) | IDs list len | list | extension IDs list len | list
227 	 * Both lists are optional. Both lengths are mandatory.
228 	 * This means valid length is:
229 	 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
230 	 */
231 	id_len = non_inherit_elem->data[1];
232 	if (non_inherit_elem->datalen < 3 + id_len)
233 		return true;
234 
235 	ext_id_len = non_inherit_elem->data[2 + id_len];
236 	if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
237 		return true;
238 
239 	if (elem->id == WLAN_EID_EXTENSION) {
240 		if (!ext_id_len)
241 			return true;
242 		loop_len = ext_id_len;
243 		list = &non_inherit_elem->data[3 + id_len];
244 		id = elem->data[0];
245 	} else {
246 		if (!id_len)
247 			return true;
248 		loop_len = id_len;
249 		list = &non_inherit_elem->data[2];
250 		id = elem->id;
251 	}
252 
253 	for (i = 0; i < loop_len; i++) {
254 		if (list[i] == id)
255 			return false;
256 	}
257 
258 	return true;
259 }
260 EXPORT_SYMBOL(cfg80211_is_element_inherited);
261 
262 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
263 				  const u8 *subelement, size_t subie_len,
264 				  u8 *new_ie, gfp_t gfp)
265 {
266 	u8 *pos, *tmp;
267 	const u8 *tmp_old, *tmp_new;
268 	const struct element *non_inherit_elem;
269 	u8 *sub_copy;
270 
271 	/* copy subelement as we need to change its content to
272 	 * mark an ie after it is processed.
273 	 */
274 	sub_copy = kmemdup(subelement, subie_len, gfp);
275 	if (!sub_copy)
276 		return 0;
277 
278 	pos = &new_ie[0];
279 
280 	/* set new ssid */
281 	tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
282 	if (tmp_new) {
283 		memcpy(pos, tmp_new, tmp_new[1] + 2);
284 		pos += (tmp_new[1] + 2);
285 	}
286 
287 	/* get non inheritance list if exists */
288 	non_inherit_elem =
289 		cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
290 				       sub_copy, subie_len);
291 
292 	/* go through IEs in ie (skip SSID) and subelement,
293 	 * merge them into new_ie
294 	 */
295 	tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
296 	tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
297 
298 	while (tmp_old + 2 - ie <= ielen &&
299 	       tmp_old + tmp_old[1] + 2 - ie <= ielen) {
300 		if (tmp_old[0] == 0) {
301 			tmp_old++;
302 			continue;
303 		}
304 
305 		if (tmp_old[0] == WLAN_EID_EXTENSION)
306 			tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
307 							 subie_len);
308 		else
309 			tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
310 						     subie_len);
311 
312 		if (!tmp) {
313 			const struct element *old_elem = (void *)tmp_old;
314 
315 			/* ie in old ie but not in subelement */
316 			if (cfg80211_is_element_inherited(old_elem,
317 							  non_inherit_elem)) {
318 				memcpy(pos, tmp_old, tmp_old[1] + 2);
319 				pos += tmp_old[1] + 2;
320 			}
321 		} else {
322 			/* ie in transmitting ie also in subelement,
323 			 * copy from subelement and flag the ie in subelement
324 			 * as copied (by setting eid field to WLAN_EID_SSID,
325 			 * which is skipped anyway).
326 			 * For vendor ie, compare OUI + type + subType to
327 			 * determine if they are the same ie.
328 			 */
329 			if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
330 				if (tmp_old[1] >= 5 && tmp[1] >= 5 &&
331 				    !memcmp(tmp_old + 2, tmp + 2, 5)) {
332 					/* same vendor ie, copy from
333 					 * subelement
334 					 */
335 					memcpy(pos, tmp, tmp[1] + 2);
336 					pos += tmp[1] + 2;
337 					tmp[0] = WLAN_EID_SSID;
338 				} else {
339 					memcpy(pos, tmp_old, tmp_old[1] + 2);
340 					pos += tmp_old[1] + 2;
341 				}
342 			} else {
343 				/* copy ie from subelement into new ie */
344 				memcpy(pos, tmp, tmp[1] + 2);
345 				pos += tmp[1] + 2;
346 				tmp[0] = WLAN_EID_SSID;
347 			}
348 		}
349 
350 		if (tmp_old + tmp_old[1] + 2 - ie == ielen)
351 			break;
352 
353 		tmp_old += tmp_old[1] + 2;
354 	}
355 
356 	/* go through subelement again to check if there is any ie not
357 	 * copied to new ie, skip ssid, capability, bssid-index ie
358 	 */
359 	tmp_new = sub_copy;
360 	while (tmp_new + 2 - sub_copy <= subie_len &&
361 	       tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
362 		if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
363 		      tmp_new[0] == WLAN_EID_SSID)) {
364 			memcpy(pos, tmp_new, tmp_new[1] + 2);
365 			pos += tmp_new[1] + 2;
366 		}
367 		if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
368 			break;
369 		tmp_new += tmp_new[1] + 2;
370 	}
371 
372 	kfree(sub_copy);
373 	return pos - new_ie;
374 }
375 
376 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
377 		   const u8 *ssid, size_t ssid_len)
378 {
379 	const struct cfg80211_bss_ies *ies;
380 	const struct element *ssid_elem;
381 
382 	if (bssid && !ether_addr_equal(a->bssid, bssid))
383 		return false;
384 
385 	if (!ssid)
386 		return true;
387 
388 	ies = rcu_access_pointer(a->ies);
389 	if (!ies)
390 		return false;
391 	ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
392 	if (!ssid_elem)
393 		return false;
394 	if (ssid_elem->datalen != ssid_len)
395 		return false;
396 	return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
397 }
398 
399 static int
400 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
401 			   struct cfg80211_bss *nontrans_bss)
402 {
403 	const struct element *ssid_elem;
404 	struct cfg80211_bss *bss = NULL;
405 
406 	rcu_read_lock();
407 	ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
408 	if (!ssid_elem) {
409 		rcu_read_unlock();
410 		return -EINVAL;
411 	}
412 
413 	/* check if nontrans_bss is in the list */
414 	list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
415 		if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
416 			   ssid_elem->datalen)) {
417 			rcu_read_unlock();
418 			return 0;
419 		}
420 	}
421 
422 	rcu_read_unlock();
423 
424 	/*
425 	 * This is a bit weird - it's not on the list, but already on another
426 	 * one! The only way that could happen is if there's some BSSID/SSID
427 	 * shared by multiple APs in their multi-BSSID profiles, potentially
428 	 * with hidden SSID mixed in ... ignore it.
429 	 */
430 	if (!list_empty(&nontrans_bss->nontrans_list))
431 		return -EINVAL;
432 
433 	/* add to the list */
434 	list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
435 	return 0;
436 }
437 
438 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
439 				  unsigned long expire_time)
440 {
441 	struct cfg80211_internal_bss *bss, *tmp;
442 	bool expired = false;
443 
444 	lockdep_assert_held(&rdev->bss_lock);
445 
446 	list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
447 		if (atomic_read(&bss->hold))
448 			continue;
449 		if (!time_after(expire_time, bss->ts))
450 			continue;
451 
452 		if (__cfg80211_unlink_bss(rdev, bss))
453 			expired = true;
454 	}
455 
456 	if (expired)
457 		rdev->bss_generation++;
458 }
459 
460 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
461 {
462 	struct cfg80211_internal_bss *bss, *oldest = NULL;
463 	bool ret;
464 
465 	lockdep_assert_held(&rdev->bss_lock);
466 
467 	list_for_each_entry(bss, &rdev->bss_list, list) {
468 		if (atomic_read(&bss->hold))
469 			continue;
470 
471 		if (!list_empty(&bss->hidden_list) &&
472 		    !bss->pub.hidden_beacon_bss)
473 			continue;
474 
475 		if (oldest && time_before(oldest->ts, bss->ts))
476 			continue;
477 		oldest = bss;
478 	}
479 
480 	if (WARN_ON(!oldest))
481 		return false;
482 
483 	/*
484 	 * The callers make sure to increase rdev->bss_generation if anything
485 	 * gets removed (and a new entry added), so there's no need to also do
486 	 * it here.
487 	 */
488 
489 	ret = __cfg80211_unlink_bss(rdev, oldest);
490 	WARN_ON(!ret);
491 	return ret;
492 }
493 
494 static u8 cfg80211_parse_bss_param(u8 data,
495 				   struct cfg80211_colocated_ap *coloc_ap)
496 {
497 	coloc_ap->oct_recommended =
498 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
499 	coloc_ap->same_ssid =
500 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
501 	coloc_ap->multi_bss =
502 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
503 	coloc_ap->transmitted_bssid =
504 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
505 	coloc_ap->unsolicited_probe =
506 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
507 	coloc_ap->colocated_ess =
508 		u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
509 
510 	return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
511 }
512 
513 static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
514 				    const struct element **elem, u32 *s_ssid)
515 {
516 
517 	*elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
518 	if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
519 		return -EINVAL;
520 
521 	*s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
522 	return 0;
523 }
524 
525 static void cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
526 {
527 	struct cfg80211_colocated_ap *ap, *tmp_ap;
528 
529 	list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
530 		list_del(&ap->list);
531 		kfree(ap);
532 	}
533 }
534 
535 static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
536 				  const u8 *pos, u8 length,
537 				  const struct element *ssid_elem,
538 				  int s_ssid_tmp)
539 {
540 	/* skip the TBTT offset */
541 	pos++;
542 
543 	/* ignore entries with invalid BSSID */
544 	if (!is_valid_ether_addr(pos))
545 		return -EINVAL;
546 
547 	memcpy(entry->bssid, pos, ETH_ALEN);
548 	pos += ETH_ALEN;
549 
550 	if (length >= IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM) {
551 		memcpy(&entry->short_ssid, pos,
552 		       sizeof(entry->short_ssid));
553 		entry->short_ssid_valid = true;
554 		pos += 4;
555 	}
556 
557 	/* skip non colocated APs */
558 	if (!cfg80211_parse_bss_param(*pos, entry))
559 		return -EINVAL;
560 	pos++;
561 
562 	if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM) {
563 		/*
564 		 * no information about the short ssid. Consider the entry valid
565 		 * for now. It would later be dropped in case there are explicit
566 		 * SSIDs that need to be matched
567 		 */
568 		if (!entry->same_ssid)
569 			return 0;
570 	}
571 
572 	if (entry->same_ssid) {
573 		entry->short_ssid = s_ssid_tmp;
574 		entry->short_ssid_valid = true;
575 
576 		/*
577 		 * This is safe because we validate datalen in
578 		 * cfg80211_parse_colocated_ap(), before calling this
579 		 * function.
580 		 */
581 		memcpy(&entry->ssid, &ssid_elem->data,
582 		       ssid_elem->datalen);
583 		entry->ssid_len = ssid_elem->datalen;
584 	}
585 	return 0;
586 }
587 
588 static int cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
589 				       struct list_head *list)
590 {
591 	struct ieee80211_neighbor_ap_info *ap_info;
592 	const struct element *elem, *ssid_elem;
593 	const u8 *pos, *end;
594 	u32 s_ssid_tmp;
595 	int n_coloc = 0, ret;
596 	LIST_HEAD(ap_list);
597 
598 	elem = cfg80211_find_elem(WLAN_EID_REDUCED_NEIGHBOR_REPORT, ies->data,
599 				  ies->len);
600 	if (!elem)
601 		return 0;
602 
603 	pos = elem->data;
604 	end = pos + elem->datalen;
605 
606 	ret = cfg80211_calc_short_ssid(ies, &ssid_elem, &s_ssid_tmp);
607 	if (ret)
608 		return ret;
609 
610 	/* RNR IE may contain more than one NEIGHBOR_AP_INFO */
611 	while (pos + sizeof(*ap_info) <= end) {
612 		enum nl80211_band band;
613 		int freq;
614 		u8 length, i, count;
615 
616 		ap_info = (void *)pos;
617 		count = u8_get_bits(ap_info->tbtt_info_hdr,
618 				    IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
619 		length = ap_info->tbtt_info_len;
620 
621 		pos += sizeof(*ap_info);
622 
623 		if (!ieee80211_operating_class_to_band(ap_info->op_class,
624 						       &band))
625 			break;
626 
627 		freq = ieee80211_channel_to_frequency(ap_info->channel, band);
628 
629 		if (end - pos < count * length)
630 			break;
631 
632 		/*
633 		 * TBTT info must include bss param + BSSID +
634 		 * (short SSID or same_ssid bit to be set).
635 		 * ignore other options, and move to the
636 		 * next AP info
637 		 */
638 		if (band != NL80211_BAND_6GHZ ||
639 		    (length != IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM &&
640 		     length < IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM)) {
641 			pos += count * length;
642 			continue;
643 		}
644 
645 		for (i = 0; i < count; i++) {
646 			struct cfg80211_colocated_ap *entry;
647 
648 			entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN,
649 					GFP_ATOMIC);
650 
651 			if (!entry)
652 				break;
653 
654 			entry->center_freq = freq;
655 
656 			if (!cfg80211_parse_ap_info(entry, pos, length,
657 						    ssid_elem, s_ssid_tmp)) {
658 				n_coloc++;
659 				list_add_tail(&entry->list, &ap_list);
660 			} else {
661 				kfree(entry);
662 			}
663 
664 			pos += length;
665 		}
666 	}
667 
668 	if (pos != end) {
669 		cfg80211_free_coloc_ap_list(&ap_list);
670 		return 0;
671 	}
672 
673 	list_splice_tail(&ap_list, list);
674 	return n_coloc;
675 }
676 
677 static  void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
678 					struct ieee80211_channel *chan,
679 					bool add_to_6ghz)
680 {
681 	int i;
682 	u32 n_channels = request->n_channels;
683 	struct cfg80211_scan_6ghz_params *params =
684 		&request->scan_6ghz_params[request->n_6ghz_params];
685 
686 	for (i = 0; i < n_channels; i++) {
687 		if (request->channels[i] == chan) {
688 			if (add_to_6ghz)
689 				params->channel_idx = i;
690 			return;
691 		}
692 	}
693 
694 	request->channels[n_channels] = chan;
695 	if (add_to_6ghz)
696 		request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
697 			n_channels;
698 
699 	request->n_channels++;
700 }
701 
702 static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
703 				     struct cfg80211_scan_request *request)
704 {
705 	int i;
706 	u32 s_ssid;
707 
708 	for (i = 0; i < request->n_ssids; i++) {
709 		/* wildcard ssid in the scan request */
710 		if (!request->ssids[i].ssid_len) {
711 			if (ap->multi_bss && !ap->transmitted_bssid)
712 				continue;
713 
714 			return true;
715 		}
716 
717 		if (ap->ssid_len &&
718 		    ap->ssid_len == request->ssids[i].ssid_len) {
719 			if (!memcmp(request->ssids[i].ssid, ap->ssid,
720 				    ap->ssid_len))
721 				return true;
722 		} else if (ap->short_ssid_valid) {
723 			s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
724 					   request->ssids[i].ssid_len);
725 
726 			if (ap->short_ssid == s_ssid)
727 				return true;
728 		}
729 	}
730 
731 	return false;
732 }
733 
734 static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
735 {
736 	u8 i;
737 	struct cfg80211_colocated_ap *ap;
738 	int n_channels, count = 0, err;
739 	struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
740 	LIST_HEAD(coloc_ap_list);
741 	bool need_scan_psc = true;
742 	const struct ieee80211_sband_iftype_data *iftd;
743 
744 	rdev_req->scan_6ghz = true;
745 
746 	if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
747 		return -EOPNOTSUPP;
748 
749 	iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
750 					       rdev_req->wdev->iftype);
751 	if (!iftd || !iftd->he_cap.has_he)
752 		return -EOPNOTSUPP;
753 
754 	n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
755 
756 	if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
757 		struct cfg80211_internal_bss *intbss;
758 
759 		spin_lock_bh(&rdev->bss_lock);
760 		list_for_each_entry(intbss, &rdev->bss_list, list) {
761 			struct cfg80211_bss *res = &intbss->pub;
762 			const struct cfg80211_bss_ies *ies;
763 
764 			ies = rcu_access_pointer(res->ies);
765 			count += cfg80211_parse_colocated_ap(ies,
766 							     &coloc_ap_list);
767 		}
768 		spin_unlock_bh(&rdev->bss_lock);
769 	}
770 
771 	request = kzalloc(struct_size(request, channels, n_channels) +
772 			  sizeof(*request->scan_6ghz_params) * count +
773 			  sizeof(*request->ssids) * rdev_req->n_ssids,
774 			  GFP_KERNEL);
775 	if (!request) {
776 		cfg80211_free_coloc_ap_list(&coloc_ap_list);
777 		return -ENOMEM;
778 	}
779 
780 	*request = *rdev_req;
781 	request->n_channels = 0;
782 	request->scan_6ghz_params =
783 		(void *)&request->channels[n_channels];
784 
785 	/*
786 	 * PSC channels should not be scanned in case of direct scan with 1 SSID
787 	 * and at least one of the reported co-located APs with same SSID
788 	 * indicating that all APs in the same ESS are co-located
789 	 */
790 	if (count && request->n_ssids == 1 && request->ssids[0].ssid_len) {
791 		list_for_each_entry(ap, &coloc_ap_list, list) {
792 			if (ap->colocated_ess &&
793 			    cfg80211_find_ssid_match(ap, request)) {
794 				need_scan_psc = false;
795 				break;
796 			}
797 		}
798 	}
799 
800 	/*
801 	 * add to the scan request the channels that need to be scanned
802 	 * regardless of the collocated APs (PSC channels or all channels
803 	 * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
804 	 */
805 	for (i = 0; i < rdev_req->n_channels; i++) {
806 		if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
807 		    ((need_scan_psc &&
808 		      cfg80211_channel_is_psc(rdev_req->channels[i])) ||
809 		     !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
810 			cfg80211_scan_req_add_chan(request,
811 						   rdev_req->channels[i],
812 						   false);
813 		}
814 	}
815 
816 	if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
817 		goto skip;
818 
819 	list_for_each_entry(ap, &coloc_ap_list, list) {
820 		bool found = false;
821 		struct cfg80211_scan_6ghz_params *scan_6ghz_params =
822 			&request->scan_6ghz_params[request->n_6ghz_params];
823 		struct ieee80211_channel *chan =
824 			ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
825 
826 		if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
827 			continue;
828 
829 		for (i = 0; i < rdev_req->n_channels; i++) {
830 			if (rdev_req->channels[i] == chan)
831 				found = true;
832 		}
833 
834 		if (!found)
835 			continue;
836 
837 		if (request->n_ssids > 0 &&
838 		    !cfg80211_find_ssid_match(ap, request))
839 			continue;
840 
841 		if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
842 			continue;
843 
844 		cfg80211_scan_req_add_chan(request, chan, true);
845 		memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
846 		scan_6ghz_params->short_ssid = ap->short_ssid;
847 		scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
848 		scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
849 
850 		/*
851 		 * If a PSC channel is added to the scan and 'need_scan_psc' is
852 		 * set to false, then all the APs that the scan logic is
853 		 * interested with on the channel are collocated and thus there
854 		 * is no need to perform the initial PSC channel listen.
855 		 */
856 		if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
857 			scan_6ghz_params->psc_no_listen = true;
858 
859 		request->n_6ghz_params++;
860 	}
861 
862 skip:
863 	cfg80211_free_coloc_ap_list(&coloc_ap_list);
864 
865 	if (request->n_channels) {
866 		struct cfg80211_scan_request *old = rdev->int_scan_req;
867 		rdev->int_scan_req = request;
868 
869 		/*
870 		 * Add the ssids from the parent scan request to the new scan
871 		 * request, so the driver would be able to use them in its
872 		 * probe requests to discover hidden APs on PSC channels.
873 		 */
874 		request->ssids = (void *)&request->channels[request->n_channels];
875 		request->n_ssids = rdev_req->n_ssids;
876 		memcpy(request->ssids, rdev_req->ssids, sizeof(*request->ssids) *
877 		       request->n_ssids);
878 
879 		/*
880 		 * If this scan follows a previous scan, save the scan start
881 		 * info from the first part of the scan
882 		 */
883 		if (old)
884 			rdev->int_scan_req->info = old->info;
885 
886 		err = rdev_scan(rdev, request);
887 		if (err) {
888 			rdev->int_scan_req = old;
889 			kfree(request);
890 		} else {
891 			kfree(old);
892 		}
893 
894 		return err;
895 	}
896 
897 	kfree(request);
898 	return -EINVAL;
899 }
900 
901 int cfg80211_scan(struct cfg80211_registered_device *rdev)
902 {
903 	struct cfg80211_scan_request *request;
904 	struct cfg80211_scan_request *rdev_req = rdev->scan_req;
905 	u32 n_channels = 0, idx, i;
906 
907 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
908 		return rdev_scan(rdev, rdev_req);
909 
910 	for (i = 0; i < rdev_req->n_channels; i++) {
911 		if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
912 			n_channels++;
913 	}
914 
915 	if (!n_channels)
916 		return cfg80211_scan_6ghz(rdev);
917 
918 	request = kzalloc(struct_size(request, channels, n_channels),
919 			  GFP_KERNEL);
920 	if (!request)
921 		return -ENOMEM;
922 
923 	*request = *rdev_req;
924 	request->n_channels = n_channels;
925 
926 	for (i = idx = 0; i < rdev_req->n_channels; i++) {
927 		if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
928 			request->channels[idx++] = rdev_req->channels[i];
929 	}
930 
931 	rdev_req->scan_6ghz = false;
932 	rdev->int_scan_req = request;
933 	return rdev_scan(rdev, request);
934 }
935 
936 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
937 			   bool send_message)
938 {
939 	struct cfg80211_scan_request *request, *rdev_req;
940 	struct wireless_dev *wdev;
941 	struct sk_buff *msg;
942 #ifdef CONFIG_CFG80211_WEXT
943 	union iwreq_data wrqu;
944 #endif
945 
946 	lockdep_assert_held(&rdev->wiphy.mtx);
947 
948 	if (rdev->scan_msg) {
949 		nl80211_send_scan_msg(rdev, rdev->scan_msg);
950 		rdev->scan_msg = NULL;
951 		return;
952 	}
953 
954 	rdev_req = rdev->scan_req;
955 	if (!rdev_req)
956 		return;
957 
958 	wdev = rdev_req->wdev;
959 	request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
960 
961 	if (wdev_running(wdev) &&
962 	    (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
963 	    !rdev_req->scan_6ghz && !request->info.aborted &&
964 	    !cfg80211_scan_6ghz(rdev))
965 		return;
966 
967 	/*
968 	 * This must be before sending the other events!
969 	 * Otherwise, wpa_supplicant gets completely confused with
970 	 * wext events.
971 	 */
972 	if (wdev->netdev)
973 		cfg80211_sme_scan_done(wdev->netdev);
974 
975 	if (!request->info.aborted &&
976 	    request->flags & NL80211_SCAN_FLAG_FLUSH) {
977 		/* flush entries from previous scans */
978 		spin_lock_bh(&rdev->bss_lock);
979 		__cfg80211_bss_expire(rdev, request->scan_start);
980 		spin_unlock_bh(&rdev->bss_lock);
981 	}
982 
983 	msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
984 
985 #ifdef CONFIG_CFG80211_WEXT
986 	if (wdev->netdev && !request->info.aborted) {
987 		memset(&wrqu, 0, sizeof(wrqu));
988 
989 		wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
990 	}
991 #endif
992 
993 	dev_put(wdev->netdev);
994 
995 	kfree(rdev->int_scan_req);
996 	rdev->int_scan_req = NULL;
997 
998 	kfree(rdev->scan_req);
999 	rdev->scan_req = NULL;
1000 
1001 	if (!send_message)
1002 		rdev->scan_msg = msg;
1003 	else
1004 		nl80211_send_scan_msg(rdev, msg);
1005 }
1006 
1007 void __cfg80211_scan_done(struct work_struct *wk)
1008 {
1009 	struct cfg80211_registered_device *rdev;
1010 
1011 	rdev = container_of(wk, struct cfg80211_registered_device,
1012 			    scan_done_wk);
1013 
1014 	wiphy_lock(&rdev->wiphy);
1015 	___cfg80211_scan_done(rdev, true);
1016 	wiphy_unlock(&rdev->wiphy);
1017 }
1018 
1019 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1020 			struct cfg80211_scan_info *info)
1021 {
1022 	struct cfg80211_scan_info old_info = request->info;
1023 
1024 	trace_cfg80211_scan_done(request, info);
1025 	WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
1026 		request != wiphy_to_rdev(request->wiphy)->int_scan_req);
1027 
1028 	request->info = *info;
1029 
1030 	/*
1031 	 * In case the scan is split, the scan_start_tsf and tsf_bssid should
1032 	 * be of the first part. In such a case old_info.scan_start_tsf should
1033 	 * be non zero.
1034 	 */
1035 	if (request->scan_6ghz && old_info.scan_start_tsf) {
1036 		request->info.scan_start_tsf = old_info.scan_start_tsf;
1037 		memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
1038 		       sizeof(request->info.tsf_bssid));
1039 	}
1040 
1041 	request->notified = true;
1042 	queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
1043 }
1044 EXPORT_SYMBOL(cfg80211_scan_done);
1045 
1046 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1047 				 struct cfg80211_sched_scan_request *req)
1048 {
1049 	lockdep_assert_held(&rdev->wiphy.mtx);
1050 
1051 	list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1052 }
1053 
1054 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1055 					struct cfg80211_sched_scan_request *req)
1056 {
1057 	lockdep_assert_held(&rdev->wiphy.mtx);
1058 
1059 	list_del_rcu(&req->list);
1060 	kfree_rcu(req, rcu_head);
1061 }
1062 
1063 static struct cfg80211_sched_scan_request *
1064 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1065 {
1066 	struct cfg80211_sched_scan_request *pos;
1067 
1068 	list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1069 				lockdep_is_held(&rdev->wiphy.mtx)) {
1070 		if (pos->reqid == reqid)
1071 			return pos;
1072 	}
1073 	return NULL;
1074 }
1075 
1076 /*
1077  * Determines if a scheduled scan request can be handled. When a legacy
1078  * scheduled scan is running no other scheduled scan is allowed regardless
1079  * whether the request is for legacy or multi-support scan. When a multi-support
1080  * scheduled scan is running a request for legacy scan is not allowed. In this
1081  * case a request for multi-support scan can be handled if resources are
1082  * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1083  */
1084 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1085 				     bool want_multi)
1086 {
1087 	struct cfg80211_sched_scan_request *pos;
1088 	int i = 0;
1089 
1090 	list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1091 		/* request id zero means legacy in progress */
1092 		if (!i && !pos->reqid)
1093 			return -EINPROGRESS;
1094 		i++;
1095 	}
1096 
1097 	if (i) {
1098 		/* no legacy allowed when multi request(s) are active */
1099 		if (!want_multi)
1100 			return -EINPROGRESS;
1101 
1102 		/* resource limit reached */
1103 		if (i == rdev->wiphy.max_sched_scan_reqs)
1104 			return -ENOSPC;
1105 	}
1106 	return 0;
1107 }
1108 
1109 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1110 {
1111 	struct cfg80211_registered_device *rdev;
1112 	struct cfg80211_sched_scan_request *req, *tmp;
1113 
1114 	rdev = container_of(work, struct cfg80211_registered_device,
1115 			   sched_scan_res_wk);
1116 
1117 	wiphy_lock(&rdev->wiphy);
1118 	list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1119 		if (req->report_results) {
1120 			req->report_results = false;
1121 			if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1122 				/* flush entries from previous scans */
1123 				spin_lock_bh(&rdev->bss_lock);
1124 				__cfg80211_bss_expire(rdev, req->scan_start);
1125 				spin_unlock_bh(&rdev->bss_lock);
1126 				req->scan_start = jiffies;
1127 			}
1128 			nl80211_send_sched_scan(req,
1129 						NL80211_CMD_SCHED_SCAN_RESULTS);
1130 		}
1131 	}
1132 	wiphy_unlock(&rdev->wiphy);
1133 }
1134 
1135 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1136 {
1137 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1138 	struct cfg80211_sched_scan_request *request;
1139 
1140 	trace_cfg80211_sched_scan_results(wiphy, reqid);
1141 	/* ignore if we're not scanning */
1142 
1143 	rcu_read_lock();
1144 	request = cfg80211_find_sched_scan_req(rdev, reqid);
1145 	if (request) {
1146 		request->report_results = true;
1147 		queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1148 	}
1149 	rcu_read_unlock();
1150 }
1151 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1152 
1153 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1154 {
1155 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1156 
1157 	lockdep_assert_held(&wiphy->mtx);
1158 
1159 	trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1160 
1161 	__cfg80211_stop_sched_scan(rdev, reqid, true);
1162 }
1163 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1164 
1165 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1166 {
1167 	wiphy_lock(wiphy);
1168 	cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1169 	wiphy_unlock(wiphy);
1170 }
1171 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1172 
1173 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1174 				 struct cfg80211_sched_scan_request *req,
1175 				 bool driver_initiated)
1176 {
1177 	lockdep_assert_held(&rdev->wiphy.mtx);
1178 
1179 	if (!driver_initiated) {
1180 		int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1181 		if (err)
1182 			return err;
1183 	}
1184 
1185 	nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1186 
1187 	cfg80211_del_sched_scan_req(rdev, req);
1188 
1189 	return 0;
1190 }
1191 
1192 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1193 			       u64 reqid, bool driver_initiated)
1194 {
1195 	struct cfg80211_sched_scan_request *sched_scan_req;
1196 
1197 	lockdep_assert_held(&rdev->wiphy.mtx);
1198 
1199 	sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1200 	if (!sched_scan_req)
1201 		return -ENOENT;
1202 
1203 	return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1204 					    driver_initiated);
1205 }
1206 
1207 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1208                       unsigned long age_secs)
1209 {
1210 	struct cfg80211_internal_bss *bss;
1211 	unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
1212 
1213 	spin_lock_bh(&rdev->bss_lock);
1214 	list_for_each_entry(bss, &rdev->bss_list, list)
1215 		bss->ts -= age_jiffies;
1216 	spin_unlock_bh(&rdev->bss_lock);
1217 }
1218 
1219 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1220 {
1221 	__cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1222 }
1223 
1224 void cfg80211_bss_flush(struct wiphy *wiphy)
1225 {
1226 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1227 
1228 	spin_lock_bh(&rdev->bss_lock);
1229 	__cfg80211_bss_expire(rdev, jiffies);
1230 	spin_unlock_bh(&rdev->bss_lock);
1231 }
1232 EXPORT_SYMBOL(cfg80211_bss_flush);
1233 
1234 const struct element *
1235 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1236 			 const u8 *match, unsigned int match_len,
1237 			 unsigned int match_offset)
1238 {
1239 	const struct element *elem;
1240 
1241 	for_each_element_id(elem, eid, ies, len) {
1242 		if (elem->datalen >= match_offset + match_len &&
1243 		    !memcmp(elem->data + match_offset, match, match_len))
1244 			return elem;
1245 	}
1246 
1247 	return NULL;
1248 }
1249 EXPORT_SYMBOL(cfg80211_find_elem_match);
1250 
1251 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1252 						const u8 *ies,
1253 						unsigned int len)
1254 {
1255 	const struct element *elem;
1256 	u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1257 	int match_len = (oui_type < 0) ? 3 : sizeof(match);
1258 
1259 	if (WARN_ON(oui_type > 0xff))
1260 		return NULL;
1261 
1262 	elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1263 					match, match_len, 0);
1264 
1265 	if (!elem || elem->datalen < 4)
1266 		return NULL;
1267 
1268 	return elem;
1269 }
1270 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1271 
1272 /**
1273  * enum bss_compare_mode - BSS compare mode
1274  * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1275  * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1276  * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1277  */
1278 enum bss_compare_mode {
1279 	BSS_CMP_REGULAR,
1280 	BSS_CMP_HIDE_ZLEN,
1281 	BSS_CMP_HIDE_NUL,
1282 };
1283 
1284 static int cmp_bss(struct cfg80211_bss *a,
1285 		   struct cfg80211_bss *b,
1286 		   enum bss_compare_mode mode)
1287 {
1288 	const struct cfg80211_bss_ies *a_ies, *b_ies;
1289 	const u8 *ie1 = NULL;
1290 	const u8 *ie2 = NULL;
1291 	int i, r;
1292 
1293 	if (a->channel != b->channel)
1294 		return (b->channel->center_freq * 1000 + b->channel->freq_offset) -
1295 		       (a->channel->center_freq * 1000 + a->channel->freq_offset);
1296 
1297 	a_ies = rcu_access_pointer(a->ies);
1298 	if (!a_ies)
1299 		return -1;
1300 	b_ies = rcu_access_pointer(b->ies);
1301 	if (!b_ies)
1302 		return 1;
1303 
1304 	if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1305 		ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1306 				       a_ies->data, a_ies->len);
1307 	if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1308 		ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1309 				       b_ies->data, b_ies->len);
1310 	if (ie1 && ie2) {
1311 		int mesh_id_cmp;
1312 
1313 		if (ie1[1] == ie2[1])
1314 			mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1315 		else
1316 			mesh_id_cmp = ie2[1] - ie1[1];
1317 
1318 		ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1319 				       a_ies->data, a_ies->len);
1320 		ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1321 				       b_ies->data, b_ies->len);
1322 		if (ie1 && ie2) {
1323 			if (mesh_id_cmp)
1324 				return mesh_id_cmp;
1325 			if (ie1[1] != ie2[1])
1326 				return ie2[1] - ie1[1];
1327 			return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1328 		}
1329 	}
1330 
1331 	r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1332 	if (r)
1333 		return r;
1334 
1335 	ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1336 	ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1337 
1338 	if (!ie1 && !ie2)
1339 		return 0;
1340 
1341 	/*
1342 	 * Note that with "hide_ssid", the function returns a match if
1343 	 * the already-present BSS ("b") is a hidden SSID beacon for
1344 	 * the new BSS ("a").
1345 	 */
1346 
1347 	/* sort missing IE before (left of) present IE */
1348 	if (!ie1)
1349 		return -1;
1350 	if (!ie2)
1351 		return 1;
1352 
1353 	switch (mode) {
1354 	case BSS_CMP_HIDE_ZLEN:
1355 		/*
1356 		 * In ZLEN mode we assume the BSS entry we're
1357 		 * looking for has a zero-length SSID. So if
1358 		 * the one we're looking at right now has that,
1359 		 * return 0. Otherwise, return the difference
1360 		 * in length, but since we're looking for the
1361 		 * 0-length it's really equivalent to returning
1362 		 * the length of the one we're looking at.
1363 		 *
1364 		 * No content comparison is needed as we assume
1365 		 * the content length is zero.
1366 		 */
1367 		return ie2[1];
1368 	case BSS_CMP_REGULAR:
1369 	default:
1370 		/* sort by length first, then by contents */
1371 		if (ie1[1] != ie2[1])
1372 			return ie2[1] - ie1[1];
1373 		return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1374 	case BSS_CMP_HIDE_NUL:
1375 		if (ie1[1] != ie2[1])
1376 			return ie2[1] - ie1[1];
1377 		/* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1378 		for (i = 0; i < ie2[1]; i++)
1379 			if (ie2[i + 2])
1380 				return -1;
1381 		return 0;
1382 	}
1383 }
1384 
1385 static bool cfg80211_bss_type_match(u16 capability,
1386 				    enum nl80211_band band,
1387 				    enum ieee80211_bss_type bss_type)
1388 {
1389 	bool ret = true;
1390 	u16 mask, val;
1391 
1392 	if (bss_type == IEEE80211_BSS_TYPE_ANY)
1393 		return ret;
1394 
1395 	if (band == NL80211_BAND_60GHZ) {
1396 		mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1397 		switch (bss_type) {
1398 		case IEEE80211_BSS_TYPE_ESS:
1399 			val = WLAN_CAPABILITY_DMG_TYPE_AP;
1400 			break;
1401 		case IEEE80211_BSS_TYPE_PBSS:
1402 			val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1403 			break;
1404 		case IEEE80211_BSS_TYPE_IBSS:
1405 			val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1406 			break;
1407 		default:
1408 			return false;
1409 		}
1410 	} else {
1411 		mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1412 		switch (bss_type) {
1413 		case IEEE80211_BSS_TYPE_ESS:
1414 			val = WLAN_CAPABILITY_ESS;
1415 			break;
1416 		case IEEE80211_BSS_TYPE_IBSS:
1417 			val = WLAN_CAPABILITY_IBSS;
1418 			break;
1419 		case IEEE80211_BSS_TYPE_MBSS:
1420 			val = 0;
1421 			break;
1422 		default:
1423 			return false;
1424 		}
1425 	}
1426 
1427 	ret = ((capability & mask) == val);
1428 	return ret;
1429 }
1430 
1431 /* Returned bss is reference counted and must be cleaned up appropriately. */
1432 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
1433 				      struct ieee80211_channel *channel,
1434 				      const u8 *bssid,
1435 				      const u8 *ssid, size_t ssid_len,
1436 				      enum ieee80211_bss_type bss_type,
1437 				      enum ieee80211_privacy privacy)
1438 {
1439 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1440 	struct cfg80211_internal_bss *bss, *res = NULL;
1441 	unsigned long now = jiffies;
1442 	int bss_privacy;
1443 
1444 	trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1445 			       privacy);
1446 
1447 	spin_lock_bh(&rdev->bss_lock);
1448 
1449 	list_for_each_entry(bss, &rdev->bss_list, list) {
1450 		if (!cfg80211_bss_type_match(bss->pub.capability,
1451 					     bss->pub.channel->band, bss_type))
1452 			continue;
1453 
1454 		bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1455 		if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1456 		    (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1457 			continue;
1458 		if (channel && bss->pub.channel != channel)
1459 			continue;
1460 		if (!is_valid_ether_addr(bss->pub.bssid))
1461 			continue;
1462 		/* Don't get expired BSS structs */
1463 		if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1464 		    !atomic_read(&bss->hold))
1465 			continue;
1466 		if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
1467 			res = bss;
1468 			bss_ref_get(rdev, res);
1469 			break;
1470 		}
1471 	}
1472 
1473 	spin_unlock_bh(&rdev->bss_lock);
1474 	if (!res)
1475 		return NULL;
1476 	trace_cfg80211_return_bss(&res->pub);
1477 	return &res->pub;
1478 }
1479 EXPORT_SYMBOL(cfg80211_get_bss);
1480 
1481 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1482 			  struct cfg80211_internal_bss *bss)
1483 {
1484 	struct rb_node **p = &rdev->bss_tree.rb_node;
1485 	struct rb_node *parent = NULL;
1486 	struct cfg80211_internal_bss *tbss;
1487 	int cmp;
1488 
1489 	while (*p) {
1490 		parent = *p;
1491 		tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1492 
1493 		cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1494 
1495 		if (WARN_ON(!cmp)) {
1496 			/* will sort of leak this BSS */
1497 			return;
1498 		}
1499 
1500 		if (cmp < 0)
1501 			p = &(*p)->rb_left;
1502 		else
1503 			p = &(*p)->rb_right;
1504 	}
1505 
1506 	rb_link_node(&bss->rbn, parent, p);
1507 	rb_insert_color(&bss->rbn, &rdev->bss_tree);
1508 }
1509 
1510 static struct cfg80211_internal_bss *
1511 rb_find_bss(struct cfg80211_registered_device *rdev,
1512 	    struct cfg80211_internal_bss *res,
1513 	    enum bss_compare_mode mode)
1514 {
1515 	struct rb_node *n = rdev->bss_tree.rb_node;
1516 	struct cfg80211_internal_bss *bss;
1517 	int r;
1518 
1519 	while (n) {
1520 		bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1521 		r = cmp_bss(&res->pub, &bss->pub, mode);
1522 
1523 		if (r == 0)
1524 			return bss;
1525 		else if (r < 0)
1526 			n = n->rb_left;
1527 		else
1528 			n = n->rb_right;
1529 	}
1530 
1531 	return NULL;
1532 }
1533 
1534 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1535 				   struct cfg80211_internal_bss *new)
1536 {
1537 	const struct cfg80211_bss_ies *ies;
1538 	struct cfg80211_internal_bss *bss;
1539 	const u8 *ie;
1540 	int i, ssidlen;
1541 	u8 fold = 0;
1542 	u32 n_entries = 0;
1543 
1544 	ies = rcu_access_pointer(new->pub.beacon_ies);
1545 	if (WARN_ON(!ies))
1546 		return false;
1547 
1548 	ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1549 	if (!ie) {
1550 		/* nothing to do */
1551 		return true;
1552 	}
1553 
1554 	ssidlen = ie[1];
1555 	for (i = 0; i < ssidlen; i++)
1556 		fold |= ie[2 + i];
1557 
1558 	if (fold) {
1559 		/* not a hidden SSID */
1560 		return true;
1561 	}
1562 
1563 	/* This is the bad part ... */
1564 
1565 	list_for_each_entry(bss, &rdev->bss_list, list) {
1566 		/*
1567 		 * we're iterating all the entries anyway, so take the
1568 		 * opportunity to validate the list length accounting
1569 		 */
1570 		n_entries++;
1571 
1572 		if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1573 			continue;
1574 		if (bss->pub.channel != new->pub.channel)
1575 			continue;
1576 		if (bss->pub.scan_width != new->pub.scan_width)
1577 			continue;
1578 		if (rcu_access_pointer(bss->pub.beacon_ies))
1579 			continue;
1580 		ies = rcu_access_pointer(bss->pub.ies);
1581 		if (!ies)
1582 			continue;
1583 		ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1584 		if (!ie)
1585 			continue;
1586 		if (ssidlen && ie[1] != ssidlen)
1587 			continue;
1588 		if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1589 			continue;
1590 		if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1591 			list_del(&bss->hidden_list);
1592 		/* combine them */
1593 		list_add(&bss->hidden_list, &new->hidden_list);
1594 		bss->pub.hidden_beacon_bss = &new->pub;
1595 		new->refcount += bss->refcount;
1596 		rcu_assign_pointer(bss->pub.beacon_ies,
1597 				   new->pub.beacon_ies);
1598 	}
1599 
1600 	WARN_ONCE(n_entries != rdev->bss_entries,
1601 		  "rdev bss entries[%d]/list[len:%d] corruption\n",
1602 		  rdev->bss_entries, n_entries);
1603 
1604 	return true;
1605 }
1606 
1607 struct cfg80211_non_tx_bss {
1608 	struct cfg80211_bss *tx_bss;
1609 	u8 max_bssid_indicator;
1610 	u8 bssid_index;
1611 };
1612 
1613 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1614 					 const struct cfg80211_bss_ies *new_ies,
1615 					 const struct cfg80211_bss_ies *old_ies)
1616 {
1617 	struct cfg80211_internal_bss *bss;
1618 
1619 	/* Assign beacon IEs to all sub entries */
1620 	list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1621 		const struct cfg80211_bss_ies *ies;
1622 
1623 		ies = rcu_access_pointer(bss->pub.beacon_ies);
1624 		WARN_ON(ies != old_ies);
1625 
1626 		rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1627 	}
1628 }
1629 
1630 static bool
1631 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1632 			  struct cfg80211_internal_bss *known,
1633 			  struct cfg80211_internal_bss *new,
1634 			  bool signal_valid)
1635 {
1636 	lockdep_assert_held(&rdev->bss_lock);
1637 
1638 	/* Update IEs */
1639 	if (rcu_access_pointer(new->pub.proberesp_ies)) {
1640 		const struct cfg80211_bss_ies *old;
1641 
1642 		old = rcu_access_pointer(known->pub.proberesp_ies);
1643 
1644 		rcu_assign_pointer(known->pub.proberesp_ies,
1645 				   new->pub.proberesp_ies);
1646 		/* Override possible earlier Beacon frame IEs */
1647 		rcu_assign_pointer(known->pub.ies,
1648 				   new->pub.proberesp_ies);
1649 		if (old)
1650 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1651 	} else if (rcu_access_pointer(new->pub.beacon_ies)) {
1652 		const struct cfg80211_bss_ies *old;
1653 
1654 		if (known->pub.hidden_beacon_bss &&
1655 		    !list_empty(&known->hidden_list)) {
1656 			const struct cfg80211_bss_ies *f;
1657 
1658 			/* The known BSS struct is one of the probe
1659 			 * response members of a group, but we're
1660 			 * receiving a beacon (beacon_ies in the new
1661 			 * bss is used). This can only mean that the
1662 			 * AP changed its beacon from not having an
1663 			 * SSID to showing it, which is confusing so
1664 			 * drop this information.
1665 			 */
1666 
1667 			f = rcu_access_pointer(new->pub.beacon_ies);
1668 			kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1669 			return false;
1670 		}
1671 
1672 		old = rcu_access_pointer(known->pub.beacon_ies);
1673 
1674 		rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1675 
1676 		/* Override IEs if they were from a beacon before */
1677 		if (old == rcu_access_pointer(known->pub.ies))
1678 			rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1679 
1680 		cfg80211_update_hidden_bsses(known,
1681 					     rcu_access_pointer(new->pub.beacon_ies),
1682 					     old);
1683 
1684 		if (old)
1685 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1686 	}
1687 
1688 	known->pub.beacon_interval = new->pub.beacon_interval;
1689 
1690 	/* don't update the signal if beacon was heard on
1691 	 * adjacent channel.
1692 	 */
1693 	if (signal_valid)
1694 		known->pub.signal = new->pub.signal;
1695 	known->pub.capability = new->pub.capability;
1696 	known->ts = new->ts;
1697 	known->ts_boottime = new->ts_boottime;
1698 	known->parent_tsf = new->parent_tsf;
1699 	known->pub.chains = new->pub.chains;
1700 	memcpy(known->pub.chain_signal, new->pub.chain_signal,
1701 	       IEEE80211_MAX_CHAINS);
1702 	ether_addr_copy(known->parent_bssid, new->parent_bssid);
1703 	known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1704 	known->pub.bssid_index = new->pub.bssid_index;
1705 
1706 	return true;
1707 }
1708 
1709 /* Returned bss is reference counted and must be cleaned up appropriately. */
1710 struct cfg80211_internal_bss *
1711 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1712 		    struct cfg80211_internal_bss *tmp,
1713 		    bool signal_valid, unsigned long ts)
1714 {
1715 	struct cfg80211_internal_bss *found = NULL;
1716 
1717 	if (WARN_ON(!tmp->pub.channel))
1718 		return NULL;
1719 
1720 	tmp->ts = ts;
1721 
1722 	spin_lock_bh(&rdev->bss_lock);
1723 
1724 	if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1725 		spin_unlock_bh(&rdev->bss_lock);
1726 		return NULL;
1727 	}
1728 
1729 	found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1730 
1731 	if (found) {
1732 		if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1733 			goto drop;
1734 	} else {
1735 		struct cfg80211_internal_bss *new;
1736 		struct cfg80211_internal_bss *hidden;
1737 		struct cfg80211_bss_ies *ies;
1738 
1739 		/*
1740 		 * create a copy -- the "res" variable that is passed in
1741 		 * is allocated on the stack since it's not needed in the
1742 		 * more common case of an update
1743 		 */
1744 		new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1745 			      GFP_ATOMIC);
1746 		if (!new) {
1747 			ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1748 			if (ies)
1749 				kfree_rcu(ies, rcu_head);
1750 			ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1751 			if (ies)
1752 				kfree_rcu(ies, rcu_head);
1753 			goto drop;
1754 		}
1755 		memcpy(new, tmp, sizeof(*new));
1756 		new->refcount = 1;
1757 		INIT_LIST_HEAD(&new->hidden_list);
1758 		INIT_LIST_HEAD(&new->pub.nontrans_list);
1759 		/* we'll set this later if it was non-NULL */
1760 		new->pub.transmitted_bss = NULL;
1761 
1762 		if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1763 			hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1764 			if (!hidden)
1765 				hidden = rb_find_bss(rdev, tmp,
1766 						     BSS_CMP_HIDE_NUL);
1767 			if (hidden) {
1768 				new->pub.hidden_beacon_bss = &hidden->pub;
1769 				list_add(&new->hidden_list,
1770 					 &hidden->hidden_list);
1771 				hidden->refcount++;
1772 				rcu_assign_pointer(new->pub.beacon_ies,
1773 						   hidden->pub.beacon_ies);
1774 			}
1775 		} else {
1776 			/*
1777 			 * Ok so we found a beacon, and don't have an entry. If
1778 			 * it's a beacon with hidden SSID, we might be in for an
1779 			 * expensive search for any probe responses that should
1780 			 * be grouped with this beacon for updates ...
1781 			 */
1782 			if (!cfg80211_combine_bsses(rdev, new)) {
1783 				bss_ref_put(rdev, new);
1784 				goto drop;
1785 			}
1786 		}
1787 
1788 		if (rdev->bss_entries >= bss_entries_limit &&
1789 		    !cfg80211_bss_expire_oldest(rdev)) {
1790 			bss_ref_put(rdev, new);
1791 			goto drop;
1792 		}
1793 
1794 		/* This must be before the call to bss_ref_get */
1795 		if (tmp->pub.transmitted_bss) {
1796 			new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1797 			bss_ref_get(rdev, bss_from_pub(tmp->pub.transmitted_bss));
1798 		}
1799 
1800 		list_add_tail(&new->list, &rdev->bss_list);
1801 		rdev->bss_entries++;
1802 		rb_insert_bss(rdev, new);
1803 		found = new;
1804 	}
1805 
1806 	rdev->bss_generation++;
1807 	bss_ref_get(rdev, found);
1808 	spin_unlock_bh(&rdev->bss_lock);
1809 
1810 	return found;
1811  drop:
1812 	spin_unlock_bh(&rdev->bss_lock);
1813 	return NULL;
1814 }
1815 
1816 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
1817 				    enum nl80211_band band)
1818 {
1819 	const struct element *tmp;
1820 
1821 	if (band == NL80211_BAND_6GHZ) {
1822 		struct ieee80211_he_operation *he_oper;
1823 
1824 		tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
1825 					     ielen);
1826 		if (tmp && tmp->datalen >= sizeof(*he_oper) &&
1827 		    tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
1828 			const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
1829 
1830 			he_oper = (void *)&tmp->data[1];
1831 
1832 			he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
1833 			if (!he_6ghz_oper)
1834 				return -1;
1835 
1836 			return he_6ghz_oper->primary;
1837 		}
1838 	} else if (band == NL80211_BAND_S1GHZ) {
1839 		tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
1840 		if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
1841 			struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
1842 
1843 			return s1gop->oper_ch;
1844 		}
1845 	} else {
1846 		tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
1847 		if (tmp && tmp->datalen == 1)
1848 			return tmp->data[0];
1849 
1850 		tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
1851 		if (tmp &&
1852 		    tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
1853 			struct ieee80211_ht_operation *htop = (void *)tmp->data;
1854 
1855 			return htop->primary_chan;
1856 		}
1857 	}
1858 
1859 	return -1;
1860 }
1861 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
1862 
1863 /*
1864  * Update RX channel information based on the available frame payload
1865  * information. This is mainly for the 2.4 GHz band where frames can be received
1866  * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1867  * element to indicate the current (transmitting) channel, but this might also
1868  * be needed on other bands if RX frequency does not match with the actual
1869  * operating channel of a BSS, or if the AP reports a different primary channel.
1870  */
1871 static struct ieee80211_channel *
1872 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1873 			 struct ieee80211_channel *channel,
1874 			 enum nl80211_bss_scan_width scan_width)
1875 {
1876 	u32 freq;
1877 	int channel_number;
1878 	struct ieee80211_channel *alt_channel;
1879 
1880 	channel_number = cfg80211_get_ies_channel_number(ie, ielen,
1881 							 channel->band);
1882 
1883 	if (channel_number < 0) {
1884 		/* No channel information in frame payload */
1885 		return channel;
1886 	}
1887 
1888 	freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1889 
1890 	/*
1891 	 * Frame info (beacon/prob res) is the same as received channel,
1892 	 * no need for further processing.
1893 	 */
1894 	if (freq == ieee80211_channel_to_khz(channel))
1895 		return channel;
1896 
1897 	alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1898 	if (!alt_channel) {
1899 		if (channel->band == NL80211_BAND_2GHZ ||
1900 		    channel->band == NL80211_BAND_6GHZ) {
1901 			/*
1902 			 * Better not allow unexpected channels when that could
1903 			 * be going beyond the 1-11 range (e.g., discovering
1904 			 * BSS on channel 12 when radio is configured for
1905 			 * channel 11) or beyond the 6 GHz channel range.
1906 			 */
1907 			return NULL;
1908 		}
1909 
1910 		/* No match for the payload channel number - ignore it */
1911 		return channel;
1912 	}
1913 
1914 	if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1915 	    scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1916 		/*
1917 		 * Ignore channel number in 5 and 10 MHz channels where there
1918 		 * may not be an n:1 or 1:n mapping between frequencies and
1919 		 * channel numbers.
1920 		 */
1921 		return channel;
1922 	}
1923 
1924 	/*
1925 	 * Use the channel determined through the payload channel number
1926 	 * instead of the RX channel reported by the driver.
1927 	 */
1928 	if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1929 		return NULL;
1930 	return alt_channel;
1931 }
1932 
1933 /* Returned bss is reference counted and must be cleaned up appropriately. */
1934 static struct cfg80211_bss *
1935 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1936 				struct cfg80211_inform_bss *data,
1937 				enum cfg80211_bss_frame_type ftype,
1938 				const u8 *bssid, u64 tsf, u16 capability,
1939 				u16 beacon_interval, const u8 *ie, size_t ielen,
1940 				struct cfg80211_non_tx_bss *non_tx_data,
1941 				gfp_t gfp)
1942 {
1943 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1944 	struct cfg80211_bss_ies *ies;
1945 	struct ieee80211_channel *channel;
1946 	struct cfg80211_internal_bss tmp = {}, *res;
1947 	int bss_type;
1948 	bool signal_valid;
1949 	unsigned long ts;
1950 
1951 	if (WARN_ON(!wiphy))
1952 		return NULL;
1953 
1954 	if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1955 		    (data->signal < 0 || data->signal > 100)))
1956 		return NULL;
1957 
1958 	channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1959 					   data->scan_width);
1960 	if (!channel)
1961 		return NULL;
1962 
1963 	memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1964 	tmp.pub.channel = channel;
1965 	tmp.pub.scan_width = data->scan_width;
1966 	tmp.pub.signal = data->signal;
1967 	tmp.pub.beacon_interval = beacon_interval;
1968 	tmp.pub.capability = capability;
1969 	tmp.ts_boottime = data->boottime_ns;
1970 	tmp.parent_tsf = data->parent_tsf;
1971 	ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1972 
1973 	if (non_tx_data) {
1974 		tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1975 		ts = bss_from_pub(non_tx_data->tx_bss)->ts;
1976 		tmp.pub.bssid_index = non_tx_data->bssid_index;
1977 		tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1978 	} else {
1979 		ts = jiffies;
1980 	}
1981 
1982 	/*
1983 	 * If we do not know here whether the IEs are from a Beacon or Probe
1984 	 * Response frame, we need to pick one of the options and only use it
1985 	 * with the driver that does not provide the full Beacon/Probe Response
1986 	 * frame. Use Beacon frame pointer to avoid indicating that this should
1987 	 * override the IEs pointer should we have received an earlier
1988 	 * indication of Probe Response data.
1989 	 */
1990 	ies = kzalloc(sizeof(*ies) + ielen, gfp);
1991 	if (!ies)
1992 		return NULL;
1993 	ies->len = ielen;
1994 	ies->tsf = tsf;
1995 	ies->from_beacon = false;
1996 	memcpy(ies->data, ie, ielen);
1997 
1998 	switch (ftype) {
1999 	case CFG80211_BSS_FTYPE_BEACON:
2000 		ies->from_beacon = true;
2001 		fallthrough;
2002 	case CFG80211_BSS_FTYPE_UNKNOWN:
2003 		rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2004 		break;
2005 	case CFG80211_BSS_FTYPE_PRESP:
2006 		rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2007 		break;
2008 	}
2009 	rcu_assign_pointer(tmp.pub.ies, ies);
2010 
2011 	signal_valid = data->chan == channel;
2012 	res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
2013 	if (!res)
2014 		return NULL;
2015 
2016 	if (channel->band == NL80211_BAND_60GHZ) {
2017 		bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2018 		if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2019 		    bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2020 			regulatory_hint_found_beacon(wiphy, channel, gfp);
2021 	} else {
2022 		if (res->pub.capability & WLAN_CAPABILITY_ESS)
2023 			regulatory_hint_found_beacon(wiphy, channel, gfp);
2024 	}
2025 
2026 	if (non_tx_data) {
2027 		/* this is a nontransmitting bss, we need to add it to
2028 		 * transmitting bss' list if it is not there
2029 		 */
2030 		spin_lock_bh(&rdev->bss_lock);
2031 		if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
2032 					       &res->pub)) {
2033 			if (__cfg80211_unlink_bss(rdev, res)) {
2034 				rdev->bss_generation++;
2035 				res = NULL;
2036 			}
2037 		}
2038 		spin_unlock_bh(&rdev->bss_lock);
2039 
2040 		if (!res)
2041 			return NULL;
2042 	}
2043 
2044 	trace_cfg80211_return_bss(&res->pub);
2045 	/* cfg80211_bss_update gives us a referenced result */
2046 	return &res->pub;
2047 }
2048 
2049 static const struct element
2050 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
2051 				   const struct element *mbssid_elem,
2052 				   const struct element *sub_elem)
2053 {
2054 	const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
2055 	const struct element *next_mbssid;
2056 	const struct element *next_sub;
2057 
2058 	next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2059 					 mbssid_end,
2060 					 ielen - (mbssid_end - ie));
2061 
2062 	/*
2063 	 * If it is not the last subelement in current MBSSID IE or there isn't
2064 	 * a next MBSSID IE - profile is complete.
2065 	*/
2066 	if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
2067 	    !next_mbssid)
2068 		return NULL;
2069 
2070 	/* For any length error, just return NULL */
2071 
2072 	if (next_mbssid->datalen < 4)
2073 		return NULL;
2074 
2075 	next_sub = (void *)&next_mbssid->data[1];
2076 
2077 	if (next_mbssid->data + next_mbssid->datalen <
2078 	    next_sub->data + next_sub->datalen)
2079 		return NULL;
2080 
2081 	if (next_sub->id != 0 || next_sub->datalen < 2)
2082 		return NULL;
2083 
2084 	/*
2085 	 * Check if the first element in the next sub element is a start
2086 	 * of a new profile
2087 	 */
2088 	return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
2089 	       NULL : next_mbssid;
2090 }
2091 
2092 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2093 			      const struct element *mbssid_elem,
2094 			      const struct element *sub_elem,
2095 			      u8 *merged_ie, size_t max_copy_len)
2096 {
2097 	size_t copied_len = sub_elem->datalen;
2098 	const struct element *next_mbssid;
2099 
2100 	if (sub_elem->datalen > max_copy_len)
2101 		return 0;
2102 
2103 	memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2104 
2105 	while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
2106 								mbssid_elem,
2107 								sub_elem))) {
2108 		const struct element *next_sub = (void *)&next_mbssid->data[1];
2109 
2110 		if (copied_len + next_sub->datalen > max_copy_len)
2111 			break;
2112 		memcpy(merged_ie + copied_len, next_sub->data,
2113 		       next_sub->datalen);
2114 		copied_len += next_sub->datalen;
2115 	}
2116 
2117 	return copied_len;
2118 }
2119 EXPORT_SYMBOL(cfg80211_merge_profile);
2120 
2121 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2122 				       struct cfg80211_inform_bss *data,
2123 				       enum cfg80211_bss_frame_type ftype,
2124 				       const u8 *bssid, u64 tsf,
2125 				       u16 beacon_interval, const u8 *ie,
2126 				       size_t ielen,
2127 				       struct cfg80211_non_tx_bss *non_tx_data,
2128 				       gfp_t gfp)
2129 {
2130 	const u8 *mbssid_index_ie;
2131 	const struct element *elem, *sub;
2132 	size_t new_ie_len;
2133 	u8 new_bssid[ETH_ALEN];
2134 	u8 *new_ie, *profile;
2135 	u64 seen_indices = 0;
2136 	u16 capability;
2137 	struct cfg80211_bss *bss;
2138 
2139 	if (!non_tx_data)
2140 		return;
2141 	if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2142 		return;
2143 	if (!wiphy->support_mbssid)
2144 		return;
2145 	if (wiphy->support_only_he_mbssid &&
2146 	    !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2147 		return;
2148 
2149 	new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2150 	if (!new_ie)
2151 		return;
2152 
2153 	profile = kmalloc(ielen, gfp);
2154 	if (!profile)
2155 		goto out;
2156 
2157 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
2158 		if (elem->datalen < 4)
2159 			continue;
2160 		if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2161 			continue;
2162 		for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2163 			u8 profile_len;
2164 
2165 			if (sub->id != 0 || sub->datalen < 4) {
2166 				/* not a valid BSS profile */
2167 				continue;
2168 			}
2169 
2170 			if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2171 			    sub->data[1] != 2) {
2172 				/* The first element within the Nontransmitted
2173 				 * BSSID Profile is not the Nontransmitted
2174 				 * BSSID Capability element.
2175 				 */
2176 				continue;
2177 			}
2178 
2179 			memset(profile, 0, ielen);
2180 			profile_len = cfg80211_merge_profile(ie, ielen,
2181 							     elem,
2182 							     sub,
2183 							     profile,
2184 							     ielen);
2185 
2186 			/* found a Nontransmitted BSSID Profile */
2187 			mbssid_index_ie = cfg80211_find_ie
2188 				(WLAN_EID_MULTI_BSSID_IDX,
2189 				 profile, profile_len);
2190 			if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2191 			    mbssid_index_ie[2] == 0 ||
2192 			    mbssid_index_ie[2] > 46) {
2193 				/* No valid Multiple BSSID-Index element */
2194 				continue;
2195 			}
2196 
2197 			if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2198 				/* We don't support legacy split of a profile */
2199 				net_dbg_ratelimited("Partial info for BSSID index %d\n",
2200 						    mbssid_index_ie[2]);
2201 
2202 			seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2203 
2204 			non_tx_data->bssid_index = mbssid_index_ie[2];
2205 			non_tx_data->max_bssid_indicator = elem->data[0];
2206 
2207 			cfg80211_gen_new_bssid(bssid,
2208 					       non_tx_data->max_bssid_indicator,
2209 					       non_tx_data->bssid_index,
2210 					       new_bssid);
2211 			memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2212 			new_ie_len = cfg80211_gen_new_ie(ie, ielen,
2213 							 profile,
2214 							 profile_len, new_ie,
2215 							 gfp);
2216 			if (!new_ie_len)
2217 				continue;
2218 
2219 			capability = get_unaligned_le16(profile + 2);
2220 			bss = cfg80211_inform_single_bss_data(wiphy, data,
2221 							      ftype,
2222 							      new_bssid, tsf,
2223 							      capability,
2224 							      beacon_interval,
2225 							      new_ie,
2226 							      new_ie_len,
2227 							      non_tx_data,
2228 							      gfp);
2229 			if (!bss)
2230 				break;
2231 			cfg80211_put_bss(wiphy, bss);
2232 		}
2233 	}
2234 
2235 out:
2236 	kfree(new_ie);
2237 	kfree(profile);
2238 }
2239 
2240 struct cfg80211_bss *
2241 cfg80211_inform_bss_data(struct wiphy *wiphy,
2242 			 struct cfg80211_inform_bss *data,
2243 			 enum cfg80211_bss_frame_type ftype,
2244 			 const u8 *bssid, u64 tsf, u16 capability,
2245 			 u16 beacon_interval, const u8 *ie, size_t ielen,
2246 			 gfp_t gfp)
2247 {
2248 	struct cfg80211_bss *res;
2249 	struct cfg80211_non_tx_bss non_tx_data;
2250 
2251 	res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
2252 					      capability, beacon_interval, ie,
2253 					      ielen, NULL, gfp);
2254 	if (!res)
2255 		return NULL;
2256 	non_tx_data.tx_bss = res;
2257 	cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
2258 				   beacon_interval, ie, ielen, &non_tx_data,
2259 				   gfp);
2260 	return res;
2261 }
2262 EXPORT_SYMBOL(cfg80211_inform_bss_data);
2263 
2264 static void
2265 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
2266 				 struct cfg80211_inform_bss *data,
2267 				 struct ieee80211_mgmt *mgmt, size_t len,
2268 				 struct cfg80211_non_tx_bss *non_tx_data,
2269 				 gfp_t gfp)
2270 {
2271 	enum cfg80211_bss_frame_type ftype;
2272 	const u8 *ie = mgmt->u.probe_resp.variable;
2273 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
2274 				      u.probe_resp.variable);
2275 
2276 	ftype = ieee80211_is_beacon(mgmt->frame_control) ?
2277 		CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
2278 
2279 	cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
2280 				   le64_to_cpu(mgmt->u.probe_resp.timestamp),
2281 				   le16_to_cpu(mgmt->u.probe_resp.beacon_int),
2282 				   ie, ielen, non_tx_data, gfp);
2283 }
2284 
2285 static void
2286 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
2287 				   struct cfg80211_bss *nontrans_bss,
2288 				   struct ieee80211_mgmt *mgmt, size_t len)
2289 {
2290 	u8 *ie, *new_ie, *pos;
2291 	const struct element *nontrans_ssid;
2292 	const u8 *trans_ssid, *mbssid;
2293 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
2294 				      u.probe_resp.variable);
2295 	size_t new_ie_len;
2296 	struct cfg80211_bss_ies *new_ies;
2297 	const struct cfg80211_bss_ies *old;
2298 	size_t cpy_len;
2299 
2300 	lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
2301 
2302 	ie = mgmt->u.probe_resp.variable;
2303 
2304 	new_ie_len = ielen;
2305 	trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
2306 	if (!trans_ssid)
2307 		return;
2308 	new_ie_len -= trans_ssid[1];
2309 	mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
2310 	/*
2311 	 * It's not valid to have the MBSSID element before SSID
2312 	 * ignore if that happens - the code below assumes it is
2313 	 * after (while copying things inbetween).
2314 	 */
2315 	if (!mbssid || mbssid < trans_ssid)
2316 		return;
2317 	new_ie_len -= mbssid[1];
2318 
2319 	nontrans_ssid = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
2320 	if (!nontrans_ssid)
2321 		return;
2322 
2323 	new_ie_len += nontrans_ssid->datalen;
2324 
2325 	/* generate new ie for nontrans BSS
2326 	 * 1. replace SSID with nontrans BSS' SSID
2327 	 * 2. skip MBSSID IE
2328 	 */
2329 	new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
2330 	if (!new_ie)
2331 		return;
2332 
2333 	new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
2334 	if (!new_ies)
2335 		goto out_free;
2336 
2337 	pos = new_ie;
2338 
2339 	/* copy the nontransmitted SSID */
2340 	cpy_len = nontrans_ssid->datalen + 2;
2341 	memcpy(pos, nontrans_ssid, cpy_len);
2342 	pos += cpy_len;
2343 	/* copy the IEs between SSID and MBSSID */
2344 	cpy_len = trans_ssid[1] + 2;
2345 	memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
2346 	pos += (mbssid - (trans_ssid + cpy_len));
2347 	/* copy the IEs after MBSSID */
2348 	cpy_len = mbssid[1] + 2;
2349 	memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
2350 
2351 	/* update ie */
2352 	new_ies->len = new_ie_len;
2353 	new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2354 	new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
2355 	memcpy(new_ies->data, new_ie, new_ie_len);
2356 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
2357 		old = rcu_access_pointer(nontrans_bss->proberesp_ies);
2358 		rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
2359 		rcu_assign_pointer(nontrans_bss->ies, new_ies);
2360 		if (old)
2361 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
2362 	} else {
2363 		old = rcu_access_pointer(nontrans_bss->beacon_ies);
2364 		rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
2365 		cfg80211_update_hidden_bsses(bss_from_pub(nontrans_bss),
2366 					     new_ies, old);
2367 		rcu_assign_pointer(nontrans_bss->ies, new_ies);
2368 		if (old)
2369 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
2370 	}
2371 
2372 out_free:
2373 	kfree(new_ie);
2374 }
2375 
2376 /* cfg80211_inform_bss_width_frame helper */
2377 static struct cfg80211_bss *
2378 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
2379 				      struct cfg80211_inform_bss *data,
2380 				      struct ieee80211_mgmt *mgmt, size_t len,
2381 				      gfp_t gfp)
2382 {
2383 	struct cfg80211_internal_bss tmp = {}, *res;
2384 	struct cfg80211_bss_ies *ies;
2385 	struct ieee80211_channel *channel;
2386 	bool signal_valid;
2387 	struct ieee80211_ext *ext = NULL;
2388 	u8 *bssid, *variable;
2389 	u16 capability, beacon_int;
2390 	size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
2391 					     u.probe_resp.variable);
2392 	int bss_type;
2393 
2394 	BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
2395 			offsetof(struct ieee80211_mgmt, u.beacon.variable));
2396 
2397 	trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
2398 
2399 	if (WARN_ON(!mgmt))
2400 		return NULL;
2401 
2402 	if (WARN_ON(!wiphy))
2403 		return NULL;
2404 
2405 	if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2406 		    (data->signal < 0 || data->signal > 100)))
2407 		return NULL;
2408 
2409 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2410 		ext = (void *) mgmt;
2411 		min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
2412 		if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2413 			min_hdr_len = offsetof(struct ieee80211_ext,
2414 					       u.s1g_short_beacon.variable);
2415 	}
2416 
2417 	if (WARN_ON(len < min_hdr_len))
2418 		return NULL;
2419 
2420 	ielen = len - min_hdr_len;
2421 	variable = mgmt->u.probe_resp.variable;
2422 	if (ext) {
2423 		if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2424 			variable = ext->u.s1g_short_beacon.variable;
2425 		else
2426 			variable = ext->u.s1g_beacon.variable;
2427 	}
2428 
2429 	channel = cfg80211_get_bss_channel(wiphy, variable,
2430 					   ielen, data->chan, data->scan_width);
2431 	if (!channel)
2432 		return NULL;
2433 
2434 	if (ext) {
2435 		const struct ieee80211_s1g_bcn_compat_ie *compat;
2436 		const struct element *elem;
2437 
2438 		elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
2439 					  variable, ielen);
2440 		if (!elem)
2441 			return NULL;
2442 		if (elem->datalen < sizeof(*compat))
2443 			return NULL;
2444 		compat = (void *)elem->data;
2445 		bssid = ext->u.s1g_beacon.sa;
2446 		capability = le16_to_cpu(compat->compat_info);
2447 		beacon_int = le16_to_cpu(compat->beacon_int);
2448 	} else {
2449 		bssid = mgmt->bssid;
2450 		beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2451 		capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
2452 	}
2453 
2454 	ies = kzalloc(sizeof(*ies) + ielen, gfp);
2455 	if (!ies)
2456 		return NULL;
2457 	ies->len = ielen;
2458 	ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2459 	ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
2460 			   ieee80211_is_s1g_beacon(mgmt->frame_control);
2461 	memcpy(ies->data, variable, ielen);
2462 
2463 	if (ieee80211_is_probe_resp(mgmt->frame_control))
2464 		rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2465 	else
2466 		rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2467 	rcu_assign_pointer(tmp.pub.ies, ies);
2468 
2469 	memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2470 	tmp.pub.beacon_interval = beacon_int;
2471 	tmp.pub.capability = capability;
2472 	tmp.pub.channel = channel;
2473 	tmp.pub.scan_width = data->scan_width;
2474 	tmp.pub.signal = data->signal;
2475 	tmp.ts_boottime = data->boottime_ns;
2476 	tmp.parent_tsf = data->parent_tsf;
2477 	tmp.pub.chains = data->chains;
2478 	memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
2479 	ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2480 
2481 	signal_valid = data->chan == channel;
2482 	res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
2483 				  jiffies);
2484 	if (!res)
2485 		return NULL;
2486 
2487 	if (channel->band == NL80211_BAND_60GHZ) {
2488 		bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2489 		if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2490 		    bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2491 			regulatory_hint_found_beacon(wiphy, channel, gfp);
2492 	} else {
2493 		if (res->pub.capability & WLAN_CAPABILITY_ESS)
2494 			regulatory_hint_found_beacon(wiphy, channel, gfp);
2495 	}
2496 
2497 	trace_cfg80211_return_bss(&res->pub);
2498 	/* cfg80211_bss_update gives us a referenced result */
2499 	return &res->pub;
2500 }
2501 
2502 struct cfg80211_bss *
2503 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2504 			       struct cfg80211_inform_bss *data,
2505 			       struct ieee80211_mgmt *mgmt, size_t len,
2506 			       gfp_t gfp)
2507 {
2508 	struct cfg80211_bss *res, *tmp_bss;
2509 	const u8 *ie = mgmt->u.probe_resp.variable;
2510 	const struct cfg80211_bss_ies *ies1, *ies2;
2511 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
2512 				      u.probe_resp.variable);
2513 	struct cfg80211_non_tx_bss non_tx_data = {};
2514 
2515 	res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
2516 						    len, gfp);
2517 
2518 	/* don't do any further MBSSID handling for S1G */
2519 	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
2520 		return res;
2521 
2522 	if (!res || !wiphy->support_mbssid ||
2523 	    !cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2524 		return res;
2525 	if (wiphy->support_only_he_mbssid &&
2526 	    !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2527 		return res;
2528 
2529 	non_tx_data.tx_bss = res;
2530 	/* process each non-transmitting bss */
2531 	cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
2532 					 &non_tx_data, gfp);
2533 
2534 	spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2535 
2536 	/* check if the res has other nontransmitting bss which is not
2537 	 * in MBSSID IE
2538 	 */
2539 	ies1 = rcu_access_pointer(res->ies);
2540 
2541 	/* go through nontrans_list, if the timestamp of the BSS is
2542 	 * earlier than the timestamp of the transmitting BSS then
2543 	 * update it
2544 	 */
2545 	list_for_each_entry(tmp_bss, &res->nontrans_list,
2546 			    nontrans_list) {
2547 		ies2 = rcu_access_pointer(tmp_bss->ies);
2548 		if (ies2->tsf < ies1->tsf)
2549 			cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
2550 							   mgmt, len);
2551 	}
2552 	spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2553 
2554 	return res;
2555 }
2556 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2557 
2558 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2559 {
2560 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2561 
2562 	if (!pub)
2563 		return;
2564 
2565 	spin_lock_bh(&rdev->bss_lock);
2566 	bss_ref_get(rdev, bss_from_pub(pub));
2567 	spin_unlock_bh(&rdev->bss_lock);
2568 }
2569 EXPORT_SYMBOL(cfg80211_ref_bss);
2570 
2571 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2572 {
2573 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2574 
2575 	if (!pub)
2576 		return;
2577 
2578 	spin_lock_bh(&rdev->bss_lock);
2579 	bss_ref_put(rdev, bss_from_pub(pub));
2580 	spin_unlock_bh(&rdev->bss_lock);
2581 }
2582 EXPORT_SYMBOL(cfg80211_put_bss);
2583 
2584 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2585 {
2586 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2587 	struct cfg80211_internal_bss *bss, *tmp1;
2588 	struct cfg80211_bss *nontrans_bss, *tmp;
2589 
2590 	if (WARN_ON(!pub))
2591 		return;
2592 
2593 	bss = bss_from_pub(pub);
2594 
2595 	spin_lock_bh(&rdev->bss_lock);
2596 	if (list_empty(&bss->list))
2597 		goto out;
2598 
2599 	list_for_each_entry_safe(nontrans_bss, tmp,
2600 				 &pub->nontrans_list,
2601 				 nontrans_list) {
2602 		tmp1 = bss_from_pub(nontrans_bss);
2603 		if (__cfg80211_unlink_bss(rdev, tmp1))
2604 			rdev->bss_generation++;
2605 	}
2606 
2607 	if (__cfg80211_unlink_bss(rdev, bss))
2608 		rdev->bss_generation++;
2609 out:
2610 	spin_unlock_bh(&rdev->bss_lock);
2611 }
2612 EXPORT_SYMBOL(cfg80211_unlink_bss);
2613 
2614 void cfg80211_bss_iter(struct wiphy *wiphy,
2615 		       struct cfg80211_chan_def *chandef,
2616 		       void (*iter)(struct wiphy *wiphy,
2617 				    struct cfg80211_bss *bss,
2618 				    void *data),
2619 		       void *iter_data)
2620 {
2621 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2622 	struct cfg80211_internal_bss *bss;
2623 
2624 	spin_lock_bh(&rdev->bss_lock);
2625 
2626 	list_for_each_entry(bss, &rdev->bss_list, list) {
2627 		if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
2628 						     false))
2629 			iter(wiphy, &bss->pub, iter_data);
2630 	}
2631 
2632 	spin_unlock_bh(&rdev->bss_lock);
2633 }
2634 EXPORT_SYMBOL(cfg80211_bss_iter);
2635 
2636 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2637 				     unsigned int link_id,
2638 				     struct ieee80211_channel *chan)
2639 {
2640 	struct wiphy *wiphy = wdev->wiphy;
2641 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2642 	struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
2643 	struct cfg80211_internal_bss *new = NULL;
2644 	struct cfg80211_internal_bss *bss;
2645 	struct cfg80211_bss *nontrans_bss;
2646 	struct cfg80211_bss *tmp;
2647 
2648 	spin_lock_bh(&rdev->bss_lock);
2649 
2650 	/*
2651 	 * Some APs use CSA also for bandwidth changes, i.e., without actually
2652 	 * changing the control channel, so no need to update in such a case.
2653 	 */
2654 	if (cbss->pub.channel == chan)
2655 		goto done;
2656 
2657 	/* use transmitting bss */
2658 	if (cbss->pub.transmitted_bss)
2659 		cbss = bss_from_pub(cbss->pub.transmitted_bss);
2660 
2661 	cbss->pub.channel = chan;
2662 
2663 	list_for_each_entry(bss, &rdev->bss_list, list) {
2664 		if (!cfg80211_bss_type_match(bss->pub.capability,
2665 					     bss->pub.channel->band,
2666 					     wdev->conn_bss_type))
2667 			continue;
2668 
2669 		if (bss == cbss)
2670 			continue;
2671 
2672 		if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2673 			new = bss;
2674 			break;
2675 		}
2676 	}
2677 
2678 	if (new) {
2679 		/* to save time, update IEs for transmitting bss only */
2680 		if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2681 			new->pub.proberesp_ies = NULL;
2682 			new->pub.beacon_ies = NULL;
2683 		}
2684 
2685 		list_for_each_entry_safe(nontrans_bss, tmp,
2686 					 &new->pub.nontrans_list,
2687 					 nontrans_list) {
2688 			bss = bss_from_pub(nontrans_bss);
2689 			if (__cfg80211_unlink_bss(rdev, bss))
2690 				rdev->bss_generation++;
2691 		}
2692 
2693 		WARN_ON(atomic_read(&new->hold));
2694 		if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2695 			rdev->bss_generation++;
2696 	}
2697 
2698 	rb_erase(&cbss->rbn, &rdev->bss_tree);
2699 	rb_insert_bss(rdev, cbss);
2700 	rdev->bss_generation++;
2701 
2702 	list_for_each_entry_safe(nontrans_bss, tmp,
2703 				 &cbss->pub.nontrans_list,
2704 				 nontrans_list) {
2705 		bss = bss_from_pub(nontrans_bss);
2706 		bss->pub.channel = chan;
2707 		rb_erase(&bss->rbn, &rdev->bss_tree);
2708 		rb_insert_bss(rdev, bss);
2709 		rdev->bss_generation++;
2710 	}
2711 
2712 done:
2713 	spin_unlock_bh(&rdev->bss_lock);
2714 }
2715 
2716 #ifdef CONFIG_CFG80211_WEXT
2717 static struct cfg80211_registered_device *
2718 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2719 {
2720 	struct cfg80211_registered_device *rdev;
2721 	struct net_device *dev;
2722 
2723 	ASSERT_RTNL();
2724 
2725 	dev = dev_get_by_index(net, ifindex);
2726 	if (!dev)
2727 		return ERR_PTR(-ENODEV);
2728 	if (dev->ieee80211_ptr)
2729 		rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2730 	else
2731 		rdev = ERR_PTR(-ENODEV);
2732 	dev_put(dev);
2733 	return rdev;
2734 }
2735 
2736 int cfg80211_wext_siwscan(struct net_device *dev,
2737 			  struct iw_request_info *info,
2738 			  union iwreq_data *wrqu, char *extra)
2739 {
2740 	struct cfg80211_registered_device *rdev;
2741 	struct wiphy *wiphy;
2742 	struct iw_scan_req *wreq = NULL;
2743 	struct cfg80211_scan_request *creq;
2744 	int i, err, n_channels = 0;
2745 	enum nl80211_band band;
2746 
2747 	if (!netif_running(dev))
2748 		return -ENETDOWN;
2749 
2750 	if (wrqu->data.length == sizeof(struct iw_scan_req))
2751 		wreq = (struct iw_scan_req *)extra;
2752 
2753 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2754 
2755 	if (IS_ERR(rdev))
2756 		return PTR_ERR(rdev);
2757 
2758 	if (rdev->scan_req || rdev->scan_msg)
2759 		return -EBUSY;
2760 
2761 	wiphy = &rdev->wiphy;
2762 
2763 	/* Determine number of channels, needed to allocate creq */
2764 	if (wreq && wreq->num_channels)
2765 		n_channels = wreq->num_channels;
2766 	else
2767 		n_channels = ieee80211_get_num_supported_channels(wiphy);
2768 
2769 	creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2770 		       n_channels * sizeof(void *),
2771 		       GFP_ATOMIC);
2772 	if (!creq)
2773 		return -ENOMEM;
2774 
2775 	creq->wiphy = wiphy;
2776 	creq->wdev = dev->ieee80211_ptr;
2777 	/* SSIDs come after channels */
2778 	creq->ssids = (void *)&creq->channels[n_channels];
2779 	creq->n_channels = n_channels;
2780 	creq->n_ssids = 1;
2781 	creq->scan_start = jiffies;
2782 
2783 	/* translate "Scan on frequencies" request */
2784 	i = 0;
2785 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2786 		int j;
2787 
2788 		if (!wiphy->bands[band])
2789 			continue;
2790 
2791 		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2792 			/* ignore disabled channels */
2793 			if (wiphy->bands[band]->channels[j].flags &
2794 						IEEE80211_CHAN_DISABLED)
2795 				continue;
2796 
2797 			/* If we have a wireless request structure and the
2798 			 * wireless request specifies frequencies, then search
2799 			 * for the matching hardware channel.
2800 			 */
2801 			if (wreq && wreq->num_channels) {
2802 				int k;
2803 				int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2804 				for (k = 0; k < wreq->num_channels; k++) {
2805 					struct iw_freq *freq =
2806 						&wreq->channel_list[k];
2807 					int wext_freq =
2808 						cfg80211_wext_freq(freq);
2809 
2810 					if (wext_freq == wiphy_freq)
2811 						goto wext_freq_found;
2812 				}
2813 				goto wext_freq_not_found;
2814 			}
2815 
2816 		wext_freq_found:
2817 			creq->channels[i] = &wiphy->bands[band]->channels[j];
2818 			i++;
2819 		wext_freq_not_found: ;
2820 		}
2821 	}
2822 	/* No channels found? */
2823 	if (!i) {
2824 		err = -EINVAL;
2825 		goto out;
2826 	}
2827 
2828 	/* Set real number of channels specified in creq->channels[] */
2829 	creq->n_channels = i;
2830 
2831 	/* translate "Scan for SSID" request */
2832 	if (wreq) {
2833 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2834 			if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2835 				err = -EINVAL;
2836 				goto out;
2837 			}
2838 			memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2839 			creq->ssids[0].ssid_len = wreq->essid_len;
2840 		}
2841 		if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2842 			creq->n_ssids = 0;
2843 	}
2844 
2845 	for (i = 0; i < NUM_NL80211_BANDS; i++)
2846 		if (wiphy->bands[i])
2847 			creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2848 
2849 	eth_broadcast_addr(creq->bssid);
2850 
2851 	wiphy_lock(&rdev->wiphy);
2852 
2853 	rdev->scan_req = creq;
2854 	err = rdev_scan(rdev, creq);
2855 	if (err) {
2856 		rdev->scan_req = NULL;
2857 		/* creq will be freed below */
2858 	} else {
2859 		nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2860 		/* creq now owned by driver */
2861 		creq = NULL;
2862 		dev_hold(dev);
2863 	}
2864 	wiphy_unlock(&rdev->wiphy);
2865  out:
2866 	kfree(creq);
2867 	return err;
2868 }
2869 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2870 
2871 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2872 				    const struct cfg80211_bss_ies *ies,
2873 				    char *current_ev, char *end_buf)
2874 {
2875 	const u8 *pos, *end, *next;
2876 	struct iw_event iwe;
2877 
2878 	if (!ies)
2879 		return current_ev;
2880 
2881 	/*
2882 	 * If needed, fragment the IEs buffer (at IE boundaries) into short
2883 	 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2884 	 */
2885 	pos = ies->data;
2886 	end = pos + ies->len;
2887 
2888 	while (end - pos > IW_GENERIC_IE_MAX) {
2889 		next = pos + 2 + pos[1];
2890 		while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2891 			next = next + 2 + next[1];
2892 
2893 		memset(&iwe, 0, sizeof(iwe));
2894 		iwe.cmd = IWEVGENIE;
2895 		iwe.u.data.length = next - pos;
2896 		current_ev = iwe_stream_add_point_check(info, current_ev,
2897 							end_buf, &iwe,
2898 							(void *)pos);
2899 		if (IS_ERR(current_ev))
2900 			return current_ev;
2901 		pos = next;
2902 	}
2903 
2904 	if (end > pos) {
2905 		memset(&iwe, 0, sizeof(iwe));
2906 		iwe.cmd = IWEVGENIE;
2907 		iwe.u.data.length = end - pos;
2908 		current_ev = iwe_stream_add_point_check(info, current_ev,
2909 							end_buf, &iwe,
2910 							(void *)pos);
2911 		if (IS_ERR(current_ev))
2912 			return current_ev;
2913 	}
2914 
2915 	return current_ev;
2916 }
2917 
2918 static char *
2919 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2920 	      struct cfg80211_internal_bss *bss, char *current_ev,
2921 	      char *end_buf)
2922 {
2923 	const struct cfg80211_bss_ies *ies;
2924 	struct iw_event iwe;
2925 	const u8 *ie;
2926 	u8 buf[50];
2927 	u8 *cfg, *p, *tmp;
2928 	int rem, i, sig;
2929 	bool ismesh = false;
2930 
2931 	memset(&iwe, 0, sizeof(iwe));
2932 	iwe.cmd = SIOCGIWAP;
2933 	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2934 	memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2935 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2936 						IW_EV_ADDR_LEN);
2937 	if (IS_ERR(current_ev))
2938 		return current_ev;
2939 
2940 	memset(&iwe, 0, sizeof(iwe));
2941 	iwe.cmd = SIOCGIWFREQ;
2942 	iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2943 	iwe.u.freq.e = 0;
2944 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2945 						IW_EV_FREQ_LEN);
2946 	if (IS_ERR(current_ev))
2947 		return current_ev;
2948 
2949 	memset(&iwe, 0, sizeof(iwe));
2950 	iwe.cmd = SIOCGIWFREQ;
2951 	iwe.u.freq.m = bss->pub.channel->center_freq;
2952 	iwe.u.freq.e = 6;
2953 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2954 						IW_EV_FREQ_LEN);
2955 	if (IS_ERR(current_ev))
2956 		return current_ev;
2957 
2958 	if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2959 		memset(&iwe, 0, sizeof(iwe));
2960 		iwe.cmd = IWEVQUAL;
2961 		iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2962 				     IW_QUAL_NOISE_INVALID |
2963 				     IW_QUAL_QUAL_UPDATED;
2964 		switch (wiphy->signal_type) {
2965 		case CFG80211_SIGNAL_TYPE_MBM:
2966 			sig = bss->pub.signal / 100;
2967 			iwe.u.qual.level = sig;
2968 			iwe.u.qual.updated |= IW_QUAL_DBM;
2969 			if (sig < -110)		/* rather bad */
2970 				sig = -110;
2971 			else if (sig > -40)	/* perfect */
2972 				sig = -40;
2973 			/* will give a range of 0 .. 70 */
2974 			iwe.u.qual.qual = sig + 110;
2975 			break;
2976 		case CFG80211_SIGNAL_TYPE_UNSPEC:
2977 			iwe.u.qual.level = bss->pub.signal;
2978 			/* will give range 0 .. 100 */
2979 			iwe.u.qual.qual = bss->pub.signal;
2980 			break;
2981 		default:
2982 			/* not reached */
2983 			break;
2984 		}
2985 		current_ev = iwe_stream_add_event_check(info, current_ev,
2986 							end_buf, &iwe,
2987 							IW_EV_QUAL_LEN);
2988 		if (IS_ERR(current_ev))
2989 			return current_ev;
2990 	}
2991 
2992 	memset(&iwe, 0, sizeof(iwe));
2993 	iwe.cmd = SIOCGIWENCODE;
2994 	if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2995 		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2996 	else
2997 		iwe.u.data.flags = IW_ENCODE_DISABLED;
2998 	iwe.u.data.length = 0;
2999 	current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3000 						&iwe, "");
3001 	if (IS_ERR(current_ev))
3002 		return current_ev;
3003 
3004 	rcu_read_lock();
3005 	ies = rcu_dereference(bss->pub.ies);
3006 	rem = ies->len;
3007 	ie = ies->data;
3008 
3009 	while (rem >= 2) {
3010 		/* invalid data */
3011 		if (ie[1] > rem - 2)
3012 			break;
3013 
3014 		switch (ie[0]) {
3015 		case WLAN_EID_SSID:
3016 			memset(&iwe, 0, sizeof(iwe));
3017 			iwe.cmd = SIOCGIWESSID;
3018 			iwe.u.data.length = ie[1];
3019 			iwe.u.data.flags = 1;
3020 			current_ev = iwe_stream_add_point_check(info,
3021 								current_ev,
3022 								end_buf, &iwe,
3023 								(u8 *)ie + 2);
3024 			if (IS_ERR(current_ev))
3025 				goto unlock;
3026 			break;
3027 		case WLAN_EID_MESH_ID:
3028 			memset(&iwe, 0, sizeof(iwe));
3029 			iwe.cmd = SIOCGIWESSID;
3030 			iwe.u.data.length = ie[1];
3031 			iwe.u.data.flags = 1;
3032 			current_ev = iwe_stream_add_point_check(info,
3033 								current_ev,
3034 								end_buf, &iwe,
3035 								(u8 *)ie + 2);
3036 			if (IS_ERR(current_ev))
3037 				goto unlock;
3038 			break;
3039 		case WLAN_EID_MESH_CONFIG:
3040 			ismesh = true;
3041 			if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
3042 				break;
3043 			cfg = (u8 *)ie + 2;
3044 			memset(&iwe, 0, sizeof(iwe));
3045 			iwe.cmd = IWEVCUSTOM;
3046 			sprintf(buf, "Mesh Network Path Selection Protocol ID: "
3047 				"0x%02X", cfg[0]);
3048 			iwe.u.data.length = strlen(buf);
3049 			current_ev = iwe_stream_add_point_check(info,
3050 								current_ev,
3051 								end_buf,
3052 								&iwe, buf);
3053 			if (IS_ERR(current_ev))
3054 				goto unlock;
3055 			sprintf(buf, "Path Selection Metric ID: 0x%02X",
3056 				cfg[1]);
3057 			iwe.u.data.length = strlen(buf);
3058 			current_ev = iwe_stream_add_point_check(info,
3059 								current_ev,
3060 								end_buf,
3061 								&iwe, buf);
3062 			if (IS_ERR(current_ev))
3063 				goto unlock;
3064 			sprintf(buf, "Congestion Control Mode ID: 0x%02X",
3065 				cfg[2]);
3066 			iwe.u.data.length = strlen(buf);
3067 			current_ev = iwe_stream_add_point_check(info,
3068 								current_ev,
3069 								end_buf,
3070 								&iwe, buf);
3071 			if (IS_ERR(current_ev))
3072 				goto unlock;
3073 			sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
3074 			iwe.u.data.length = strlen(buf);
3075 			current_ev = iwe_stream_add_point_check(info,
3076 								current_ev,
3077 								end_buf,
3078 								&iwe, buf);
3079 			if (IS_ERR(current_ev))
3080 				goto unlock;
3081 			sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
3082 			iwe.u.data.length = strlen(buf);
3083 			current_ev = iwe_stream_add_point_check(info,
3084 								current_ev,
3085 								end_buf,
3086 								&iwe, buf);
3087 			if (IS_ERR(current_ev))
3088 				goto unlock;
3089 			sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
3090 			iwe.u.data.length = strlen(buf);
3091 			current_ev = iwe_stream_add_point_check(info,
3092 								current_ev,
3093 								end_buf,
3094 								&iwe, buf);
3095 			if (IS_ERR(current_ev))
3096 				goto unlock;
3097 			sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
3098 			iwe.u.data.length = strlen(buf);
3099 			current_ev = iwe_stream_add_point_check(info,
3100 								current_ev,
3101 								end_buf,
3102 								&iwe, buf);
3103 			if (IS_ERR(current_ev))
3104 				goto unlock;
3105 			break;
3106 		case WLAN_EID_SUPP_RATES:
3107 		case WLAN_EID_EXT_SUPP_RATES:
3108 			/* display all supported rates in readable format */
3109 			p = current_ev + iwe_stream_lcp_len(info);
3110 
3111 			memset(&iwe, 0, sizeof(iwe));
3112 			iwe.cmd = SIOCGIWRATE;
3113 			/* Those two flags are ignored... */
3114 			iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3115 
3116 			for (i = 0; i < ie[1]; i++) {
3117 				iwe.u.bitrate.value =
3118 					((ie[i + 2] & 0x7f) * 500000);
3119 				tmp = p;
3120 				p = iwe_stream_add_value(info, current_ev, p,
3121 							 end_buf, &iwe,
3122 							 IW_EV_PARAM_LEN);
3123 				if (p == tmp) {
3124 					current_ev = ERR_PTR(-E2BIG);
3125 					goto unlock;
3126 				}
3127 			}
3128 			current_ev = p;
3129 			break;
3130 		}
3131 		rem -= ie[1] + 2;
3132 		ie += ie[1] + 2;
3133 	}
3134 
3135 	if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3136 	    ismesh) {
3137 		memset(&iwe, 0, sizeof(iwe));
3138 		iwe.cmd = SIOCGIWMODE;
3139 		if (ismesh)
3140 			iwe.u.mode = IW_MODE_MESH;
3141 		else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3142 			iwe.u.mode = IW_MODE_MASTER;
3143 		else
3144 			iwe.u.mode = IW_MODE_ADHOC;
3145 		current_ev = iwe_stream_add_event_check(info, current_ev,
3146 							end_buf, &iwe,
3147 							IW_EV_UINT_LEN);
3148 		if (IS_ERR(current_ev))
3149 			goto unlock;
3150 	}
3151 
3152 	memset(&iwe, 0, sizeof(iwe));
3153 	iwe.cmd = IWEVCUSTOM;
3154 	sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
3155 	iwe.u.data.length = strlen(buf);
3156 	current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3157 						&iwe, buf);
3158 	if (IS_ERR(current_ev))
3159 		goto unlock;
3160 	memset(&iwe, 0, sizeof(iwe));
3161 	iwe.cmd = IWEVCUSTOM;
3162 	sprintf(buf, " Last beacon: %ums ago",
3163 		elapsed_jiffies_msecs(bss->ts));
3164 	iwe.u.data.length = strlen(buf);
3165 	current_ev = iwe_stream_add_point_check(info, current_ev,
3166 						end_buf, &iwe, buf);
3167 	if (IS_ERR(current_ev))
3168 		goto unlock;
3169 
3170 	current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3171 
3172  unlock:
3173 	rcu_read_unlock();
3174 	return current_ev;
3175 }
3176 
3177 
3178 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3179 				  struct iw_request_info *info,
3180 				  char *buf, size_t len)
3181 {
3182 	char *current_ev = buf;
3183 	char *end_buf = buf + len;
3184 	struct cfg80211_internal_bss *bss;
3185 	int err = 0;
3186 
3187 	spin_lock_bh(&rdev->bss_lock);
3188 	cfg80211_bss_expire(rdev);
3189 
3190 	list_for_each_entry(bss, &rdev->bss_list, list) {
3191 		if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3192 			err = -E2BIG;
3193 			break;
3194 		}
3195 		current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
3196 					   current_ev, end_buf);
3197 		if (IS_ERR(current_ev)) {
3198 			err = PTR_ERR(current_ev);
3199 			break;
3200 		}
3201 	}
3202 	spin_unlock_bh(&rdev->bss_lock);
3203 
3204 	if (err)
3205 		return err;
3206 	return current_ev - buf;
3207 }
3208 
3209 
3210 int cfg80211_wext_giwscan(struct net_device *dev,
3211 			  struct iw_request_info *info,
3212 			  union iwreq_data *wrqu, char *extra)
3213 {
3214 	struct iw_point *data = &wrqu->data;
3215 	struct cfg80211_registered_device *rdev;
3216 	int res;
3217 
3218 	if (!netif_running(dev))
3219 		return -ENETDOWN;
3220 
3221 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3222 
3223 	if (IS_ERR(rdev))
3224 		return PTR_ERR(rdev);
3225 
3226 	if (rdev->scan_req || rdev->scan_msg)
3227 		return -EAGAIN;
3228 
3229 	res = ieee80211_scan_results(rdev, info, extra, data->length);
3230 	data->length = 0;
3231 	if (res >= 0) {
3232 		data->length = res;
3233 		res = 0;
3234 	}
3235 
3236 	return res;
3237 }
3238 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
3239 #endif
3240