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