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