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