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