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