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