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