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