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