1 // SPDX-License-Identifier: GPL-2.0
3 * cfg80211 scan result handling
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-2023 Intel Corporation
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>
20 #include <net/cfg80211.h>
21 #include <net/cfg80211-wext.h>
22 #include <net/iw_handler.h>
25 #include "wext-compat.h"
29 * DOC: BSS tree/list structure
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
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.
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.
54 * Note that the hidden_beacon_bss pointer never changes, due to
55 * the reference counting. Therefore, no locking is needed for
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.
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.)
72 static int bss_entries_limit = 1000;
73 module_param(bss_entries_limit, int, 0644);
74 MODULE_PARM_DESC(bss_entries_limit,
75 "limit to number of scan BSS entries (per wiphy, default 1000)");
77 #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
80 * struct cfg80211_colocated_ap - colocated AP information
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
100 struct cfg80211_colocated_ap {
101 struct list_head list;
103 u8 ssid[IEEE80211_MAX_SSID_LEN];
107 u8 unsolicited_probe:1,
116 static void bss_free(struct cfg80211_internal_bss *bss)
118 struct cfg80211_bss_ies *ies;
120 if (WARN_ON(atomic_read(&bss->hold)))
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);
128 kfree_rcu(ies, rcu_head);
131 * This happens when the module is removed, it doesn't
132 * really matter any more save for completeness
134 if (!list_empty(&bss->hidden_list))
135 list_del(&bss->hidden_list);
140 static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
141 struct cfg80211_internal_bss *bss)
143 lockdep_assert_held(&rdev->bss_lock);
147 if (bss->pub.hidden_beacon_bss)
148 bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
150 if (bss->pub.transmitted_bss)
151 bss_from_pub(bss->pub.transmitted_bss)->refcount++;
154 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
155 struct cfg80211_internal_bss *bss)
157 lockdep_assert_held(&rdev->bss_lock);
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,
165 if (hbss->refcount == 0)
169 if (bss->pub.transmitted_bss) {
170 struct cfg80211_internal_bss *tbss;
172 tbss = container_of(bss->pub.transmitted_bss,
173 struct cfg80211_internal_bss,
176 if (tbss->refcount == 0)
181 if (bss->refcount == 0)
185 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
186 struct cfg80211_internal_bss *bss)
188 lockdep_assert_held(&rdev->bss_lock);
190 if (!list_empty(&bss->hidden_list)) {
192 * don't remove the beacon entry if it has
193 * probe responses associated with it
195 if (!bss->pub.hidden_beacon_bss)
198 * if it's a probe response entry break its
199 * link to the other entries in the group
201 list_del_init(&bss->hidden_list);
204 list_del_init(&bss->list);
205 list_del_init(&bss->pub.nontrans_list);
206 rb_erase(&bss->rbn, &rdev->bss_tree);
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);
215 bool cfg80211_is_element_inherited(const struct element *elem,
216 const struct element *non_inherit_elem)
218 u8 id_len, ext_id_len, i, loop_len, id;
221 if (elem->id == WLAN_EID_MULTIPLE_BSSID)
224 if (!non_inherit_elem || non_inherit_elem->datalen < 2)
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
234 id_len = non_inherit_elem->data[1];
235 if (non_inherit_elem->datalen < 3 + id_len)
238 ext_id_len = non_inherit_elem->data[2 + id_len];
239 if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
242 if (elem->id == WLAN_EID_EXTENSION) {
245 loop_len = ext_id_len;
246 list = &non_inherit_elem->data[3 + id_len];
252 list = &non_inherit_elem->data[2];
256 for (i = 0; i < loop_len; i++) {
263 EXPORT_SYMBOL(cfg80211_is_element_inherited);
265 static size_t cfg80211_copy_elem_with_frags(const struct element *elem,
266 const u8 *ie, size_t ie_len,
267 u8 **pos, u8 *buf, size_t buf_len)
269 if (WARN_ON((u8 *)elem < ie || elem->data > ie + ie_len ||
270 elem->data + elem->datalen > ie + ie_len))
273 if (elem->datalen + 2 > buf + buf_len - *pos)
276 memcpy(*pos, elem, elem->datalen + 2);
277 *pos += elem->datalen + 2;
279 /* Finish if it is not fragmented */
280 if (elem->datalen != 255)
283 ie_len = ie + ie_len - elem->data - elem->datalen;
284 ie = (const u8 *)elem->data + elem->datalen;
286 for_each_element(elem, ie, ie_len) {
287 if (elem->id != WLAN_EID_FRAGMENT)
290 if (elem->datalen + 2 > buf + buf_len - *pos)
293 memcpy(*pos, elem, elem->datalen + 2);
294 *pos += elem->datalen + 2;
296 if (elem->datalen != 255)
303 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
304 const u8 *subie, size_t subie_len,
305 u8 *new_ie, size_t new_ie_len)
307 const struct element *non_inherit_elem, *parent, *sub;
310 unsigned int match_len;
312 non_inherit_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
315 /* We copy the elements one by one from the parent to the generated
317 * If they are not inherited (included in subie or in the non
318 * inheritance element), then we copy all occurrences the first time
319 * we see this element type.
321 for_each_element(parent, ie, ielen) {
322 if (parent->id == WLAN_EID_FRAGMENT)
325 if (parent->id == WLAN_EID_EXTENSION) {
326 if (parent->datalen < 1)
329 id = WLAN_EID_EXTENSION;
330 ext_id = parent->data[0];
337 /* Find first occurrence in subie */
338 sub = cfg80211_find_elem_match(id, subie, subie_len,
339 &ext_id, match_len, 0);
341 /* Copy from parent if not in subie and inherited */
343 cfg80211_is_element_inherited(parent, non_inherit_elem)) {
344 if (!cfg80211_copy_elem_with_frags(parent,
353 /* Already copied if an earlier element had the same type */
354 if (cfg80211_find_elem_match(id, ie, (u8 *)parent - ie,
355 &ext_id, match_len, 0))
358 /* Not inheriting, copy all similar elements from subie */
360 if (!cfg80211_copy_elem_with_frags(sub,
366 sub = cfg80211_find_elem_match(id,
367 sub->data + sub->datalen,
371 &ext_id, match_len, 0);
375 /* The above misses elements that are included in subie but not in the
376 * parent, so do a pass over subie and append those.
377 * Skip the non-tx BSSID caps and non-inheritance element.
379 for_each_element(sub, subie, subie_len) {
380 if (sub->id == WLAN_EID_NON_TX_BSSID_CAP)
383 if (sub->id == WLAN_EID_FRAGMENT)
386 if (sub->id == WLAN_EID_EXTENSION) {
387 if (sub->datalen < 1)
390 id = WLAN_EID_EXTENSION;
391 ext_id = sub->data[0];
394 if (ext_id == WLAN_EID_EXT_NON_INHERITANCE)
401 /* Processed if one was included in the parent */
402 if (cfg80211_find_elem_match(id, ie, ielen,
403 &ext_id, match_len, 0))
406 if (!cfg80211_copy_elem_with_frags(sub, subie, subie_len,
407 &pos, new_ie, new_ie_len))
414 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
415 const u8 *ssid, size_t ssid_len)
417 const struct cfg80211_bss_ies *ies;
418 const struct element *ssid_elem;
420 if (bssid && !ether_addr_equal(a->bssid, bssid))
426 ies = rcu_access_pointer(a->ies);
429 ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
432 if (ssid_elem->datalen != ssid_len)
434 return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
438 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
439 struct cfg80211_bss *nontrans_bss)
441 const struct element *ssid_elem;
442 struct cfg80211_bss *bss = NULL;
445 ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
451 /* check if nontrans_bss is in the list */
452 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
453 if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
454 ssid_elem->datalen)) {
463 * This is a bit weird - it's not on the list, but already on another
464 * one! The only way that could happen is if there's some BSSID/SSID
465 * shared by multiple APs in their multi-BSSID profiles, potentially
466 * with hidden SSID mixed in ... ignore it.
468 if (!list_empty(&nontrans_bss->nontrans_list))
471 /* add to the list */
472 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
476 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
477 unsigned long expire_time)
479 struct cfg80211_internal_bss *bss, *tmp;
480 bool expired = false;
482 lockdep_assert_held(&rdev->bss_lock);
484 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
485 if (atomic_read(&bss->hold))
487 if (!time_after(expire_time, bss->ts))
490 if (__cfg80211_unlink_bss(rdev, bss))
495 rdev->bss_generation++;
498 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
500 struct cfg80211_internal_bss *bss, *oldest = NULL;
503 lockdep_assert_held(&rdev->bss_lock);
505 list_for_each_entry(bss, &rdev->bss_list, list) {
506 if (atomic_read(&bss->hold))
509 if (!list_empty(&bss->hidden_list) &&
510 !bss->pub.hidden_beacon_bss)
513 if (oldest && time_before(oldest->ts, bss->ts))
518 if (WARN_ON(!oldest))
522 * The callers make sure to increase rdev->bss_generation if anything
523 * gets removed (and a new entry added), so there's no need to also do
527 ret = __cfg80211_unlink_bss(rdev, oldest);
532 static u8 cfg80211_parse_bss_param(u8 data,
533 struct cfg80211_colocated_ap *coloc_ap)
535 coloc_ap->oct_recommended =
536 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
537 coloc_ap->same_ssid =
538 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
539 coloc_ap->multi_bss =
540 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
541 coloc_ap->transmitted_bssid =
542 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
543 coloc_ap->unsolicited_probe =
544 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
545 coloc_ap->colocated_ess =
546 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
548 return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
551 static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
552 const struct element **elem, u32 *s_ssid)
555 *elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
556 if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
559 *s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
563 static void cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
565 struct cfg80211_colocated_ap *ap, *tmp_ap;
567 list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
573 static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
574 const u8 *pos, u8 length,
575 const struct element *ssid_elem,
578 /* skip the TBTT offset */
581 /* ignore entries with invalid BSSID */
582 if (!is_valid_ether_addr(pos))
585 memcpy(entry->bssid, pos, ETH_ALEN);
588 if (length >= IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM) {
589 memcpy(&entry->short_ssid, pos,
590 sizeof(entry->short_ssid));
591 entry->short_ssid_valid = true;
595 /* skip non colocated APs */
596 if (!cfg80211_parse_bss_param(*pos, entry))
600 if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM) {
602 * no information about the short ssid. Consider the entry valid
603 * for now. It would later be dropped in case there are explicit
604 * SSIDs that need to be matched
606 if (!entry->same_ssid)
610 if (entry->same_ssid) {
611 entry->short_ssid = s_ssid_tmp;
612 entry->short_ssid_valid = true;
615 * This is safe because we validate datalen in
616 * cfg80211_parse_colocated_ap(), before calling this
619 memcpy(&entry->ssid, &ssid_elem->data,
621 entry->ssid_len = ssid_elem->datalen;
626 static int cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
627 struct list_head *list)
629 struct ieee80211_neighbor_ap_info *ap_info;
630 const struct element *elem, *ssid_elem;
633 int n_coloc = 0, ret;
636 elem = cfg80211_find_elem(WLAN_EID_REDUCED_NEIGHBOR_REPORT, ies->data,
642 end = pos + elem->datalen;
644 ret = cfg80211_calc_short_ssid(ies, &ssid_elem, &s_ssid_tmp);
648 /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
649 while (pos + sizeof(*ap_info) <= end) {
650 enum nl80211_band band;
654 ap_info = (void *)pos;
655 count = u8_get_bits(ap_info->tbtt_info_hdr,
656 IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
657 length = ap_info->tbtt_info_len;
659 pos += sizeof(*ap_info);
661 if (!ieee80211_operating_class_to_band(ap_info->op_class,
665 freq = ieee80211_channel_to_frequency(ap_info->channel, band);
667 if (end - pos < count * length)
671 * TBTT info must include bss param + BSSID +
672 * (short SSID or same_ssid bit to be set).
673 * ignore other options, and move to the
676 if (band != NL80211_BAND_6GHZ ||
677 (length != IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM &&
678 length < IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM)) {
679 pos += count * length;
683 for (i = 0; i < count; i++) {
684 struct cfg80211_colocated_ap *entry;
686 entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN,
692 entry->center_freq = freq;
694 if (!cfg80211_parse_ap_info(entry, pos, length,
695 ssid_elem, s_ssid_tmp)) {
697 list_add_tail(&entry->list, &ap_list);
707 cfg80211_free_coloc_ap_list(&ap_list);
711 list_splice_tail(&ap_list, list);
715 static void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
716 struct ieee80211_channel *chan,
720 u32 n_channels = request->n_channels;
721 struct cfg80211_scan_6ghz_params *params =
722 &request->scan_6ghz_params[request->n_6ghz_params];
724 for (i = 0; i < n_channels; i++) {
725 if (request->channels[i] == chan) {
727 params->channel_idx = i;
732 request->channels[n_channels] = chan;
734 request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
737 request->n_channels++;
740 static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
741 struct cfg80211_scan_request *request)
746 for (i = 0; i < request->n_ssids; i++) {
747 /* wildcard ssid in the scan request */
748 if (!request->ssids[i].ssid_len) {
749 if (ap->multi_bss && !ap->transmitted_bssid)
756 ap->ssid_len == request->ssids[i].ssid_len) {
757 if (!memcmp(request->ssids[i].ssid, ap->ssid,
760 } else if (ap->short_ssid_valid) {
761 s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
762 request->ssids[i].ssid_len);
764 if (ap->short_ssid == s_ssid)
772 static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
775 struct cfg80211_colocated_ap *ap;
776 int n_channels, count = 0, err;
777 struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
778 LIST_HEAD(coloc_ap_list);
779 bool need_scan_psc = true;
780 const struct ieee80211_sband_iftype_data *iftd;
782 rdev_req->scan_6ghz = true;
784 if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
787 iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
788 rdev_req->wdev->iftype);
789 if (!iftd || !iftd->he_cap.has_he)
792 n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
794 if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
795 struct cfg80211_internal_bss *intbss;
797 spin_lock_bh(&rdev->bss_lock);
798 list_for_each_entry(intbss, &rdev->bss_list, list) {
799 struct cfg80211_bss *res = &intbss->pub;
800 const struct cfg80211_bss_ies *ies;
802 ies = rcu_access_pointer(res->ies);
803 count += cfg80211_parse_colocated_ap(ies,
806 spin_unlock_bh(&rdev->bss_lock);
809 request = kzalloc(struct_size(request, channels, n_channels) +
810 sizeof(*request->scan_6ghz_params) * count +
811 sizeof(*request->ssids) * rdev_req->n_ssids,
814 cfg80211_free_coloc_ap_list(&coloc_ap_list);
818 *request = *rdev_req;
819 request->n_channels = 0;
820 request->scan_6ghz_params =
821 (void *)&request->channels[n_channels];
824 * PSC channels should not be scanned in case of direct scan with 1 SSID
825 * and at least one of the reported co-located APs with same SSID
826 * indicating that all APs in the same ESS are co-located
828 if (count && request->n_ssids == 1 && request->ssids[0].ssid_len) {
829 list_for_each_entry(ap, &coloc_ap_list, list) {
830 if (ap->colocated_ess &&
831 cfg80211_find_ssid_match(ap, request)) {
832 need_scan_psc = false;
839 * add to the scan request the channels that need to be scanned
840 * regardless of the collocated APs (PSC channels or all channels
841 * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
843 for (i = 0; i < rdev_req->n_channels; i++) {
844 if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
846 cfg80211_channel_is_psc(rdev_req->channels[i])) ||
847 !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
848 cfg80211_scan_req_add_chan(request,
849 rdev_req->channels[i],
854 if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
857 list_for_each_entry(ap, &coloc_ap_list, list) {
859 struct cfg80211_scan_6ghz_params *scan_6ghz_params =
860 &request->scan_6ghz_params[request->n_6ghz_params];
861 struct ieee80211_channel *chan =
862 ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
864 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
867 for (i = 0; i < rdev_req->n_channels; i++) {
868 if (rdev_req->channels[i] == chan)
875 if (request->n_ssids > 0 &&
876 !cfg80211_find_ssid_match(ap, request))
879 if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
882 cfg80211_scan_req_add_chan(request, chan, true);
883 memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
884 scan_6ghz_params->short_ssid = ap->short_ssid;
885 scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
886 scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
889 * If a PSC channel is added to the scan and 'need_scan_psc' is
890 * set to false, then all the APs that the scan logic is
891 * interested with on the channel are collocated and thus there
892 * is no need to perform the initial PSC channel listen.
894 if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
895 scan_6ghz_params->psc_no_listen = true;
897 request->n_6ghz_params++;
901 cfg80211_free_coloc_ap_list(&coloc_ap_list);
903 if (request->n_channels) {
904 struct cfg80211_scan_request *old = rdev->int_scan_req;
905 rdev->int_scan_req = request;
908 * Add the ssids from the parent scan request to the new scan
909 * request, so the driver would be able to use them in its
910 * probe requests to discover hidden APs on PSC channels.
912 request->ssids = (void *)&request->channels[request->n_channels];
913 request->n_ssids = rdev_req->n_ssids;
914 memcpy(request->ssids, rdev_req->ssids, sizeof(*request->ssids) *
918 * If this scan follows a previous scan, save the scan start
919 * info from the first part of the scan
922 rdev->int_scan_req->info = old->info;
924 err = rdev_scan(rdev, request);
926 rdev->int_scan_req = old;
939 int cfg80211_scan(struct cfg80211_registered_device *rdev)
941 struct cfg80211_scan_request *request;
942 struct cfg80211_scan_request *rdev_req = rdev->scan_req;
943 u32 n_channels = 0, idx, i;
945 if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
946 return rdev_scan(rdev, rdev_req);
948 for (i = 0; i < rdev_req->n_channels; i++) {
949 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
954 return cfg80211_scan_6ghz(rdev);
956 request = kzalloc(struct_size(request, channels, n_channels),
961 *request = *rdev_req;
962 request->n_channels = n_channels;
964 for (i = idx = 0; i < rdev_req->n_channels; i++) {
965 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
966 request->channels[idx++] = rdev_req->channels[i];
969 rdev_req->scan_6ghz = false;
970 rdev->int_scan_req = request;
971 return rdev_scan(rdev, request);
974 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
977 struct cfg80211_scan_request *request, *rdev_req;
978 struct wireless_dev *wdev;
980 #ifdef CONFIG_CFG80211_WEXT
981 union iwreq_data wrqu;
984 lockdep_assert_held(&rdev->wiphy.mtx);
986 if (rdev->scan_msg) {
987 nl80211_send_scan_msg(rdev, rdev->scan_msg);
988 rdev->scan_msg = NULL;
992 rdev_req = rdev->scan_req;
996 wdev = rdev_req->wdev;
997 request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
999 if (wdev_running(wdev) &&
1000 (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
1001 !rdev_req->scan_6ghz && !request->info.aborted &&
1002 !cfg80211_scan_6ghz(rdev))
1006 * This must be before sending the other events!
1007 * Otherwise, wpa_supplicant gets completely confused with
1011 cfg80211_sme_scan_done(wdev->netdev);
1013 if (!request->info.aborted &&
1014 request->flags & NL80211_SCAN_FLAG_FLUSH) {
1015 /* flush entries from previous scans */
1016 spin_lock_bh(&rdev->bss_lock);
1017 __cfg80211_bss_expire(rdev, request->scan_start);
1018 spin_unlock_bh(&rdev->bss_lock);
1021 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
1023 #ifdef CONFIG_CFG80211_WEXT
1024 if (wdev->netdev && !request->info.aborted) {
1025 memset(&wrqu, 0, sizeof(wrqu));
1027 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
1031 dev_put(wdev->netdev);
1033 kfree(rdev->int_scan_req);
1034 rdev->int_scan_req = NULL;
1036 kfree(rdev->scan_req);
1037 rdev->scan_req = NULL;
1040 rdev->scan_msg = msg;
1042 nl80211_send_scan_msg(rdev, msg);
1045 void __cfg80211_scan_done(struct work_struct *wk)
1047 struct cfg80211_registered_device *rdev;
1049 rdev = container_of(wk, struct cfg80211_registered_device,
1052 wiphy_lock(&rdev->wiphy);
1053 ___cfg80211_scan_done(rdev, true);
1054 wiphy_unlock(&rdev->wiphy);
1057 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1058 struct cfg80211_scan_info *info)
1060 struct cfg80211_scan_info old_info = request->info;
1062 trace_cfg80211_scan_done(request, info);
1063 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
1064 request != wiphy_to_rdev(request->wiphy)->int_scan_req);
1066 request->info = *info;
1069 * In case the scan is split, the scan_start_tsf and tsf_bssid should
1070 * be of the first part. In such a case old_info.scan_start_tsf should
1073 if (request->scan_6ghz && old_info.scan_start_tsf) {
1074 request->info.scan_start_tsf = old_info.scan_start_tsf;
1075 memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
1076 sizeof(request->info.tsf_bssid));
1079 request->notified = true;
1080 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
1082 EXPORT_SYMBOL(cfg80211_scan_done);
1084 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1085 struct cfg80211_sched_scan_request *req)
1087 lockdep_assert_held(&rdev->wiphy.mtx);
1089 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1092 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1093 struct cfg80211_sched_scan_request *req)
1095 lockdep_assert_held(&rdev->wiphy.mtx);
1097 list_del_rcu(&req->list);
1098 kfree_rcu(req, rcu_head);
1101 static struct cfg80211_sched_scan_request *
1102 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1104 struct cfg80211_sched_scan_request *pos;
1106 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1107 lockdep_is_held(&rdev->wiphy.mtx)) {
1108 if (pos->reqid == reqid)
1115 * Determines if a scheduled scan request can be handled. When a legacy
1116 * scheduled scan is running no other scheduled scan is allowed regardless
1117 * whether the request is for legacy or multi-support scan. When a multi-support
1118 * scheduled scan is running a request for legacy scan is not allowed. In this
1119 * case a request for multi-support scan can be handled if resources are
1120 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1122 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1125 struct cfg80211_sched_scan_request *pos;
1128 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1129 /* request id zero means legacy in progress */
1130 if (!i && !pos->reqid)
1131 return -EINPROGRESS;
1136 /* no legacy allowed when multi request(s) are active */
1138 return -EINPROGRESS;
1140 /* resource limit reached */
1141 if (i == rdev->wiphy.max_sched_scan_reqs)
1147 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1149 struct cfg80211_registered_device *rdev;
1150 struct cfg80211_sched_scan_request *req, *tmp;
1152 rdev = container_of(work, struct cfg80211_registered_device,
1155 wiphy_lock(&rdev->wiphy);
1156 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1157 if (req->report_results) {
1158 req->report_results = false;
1159 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1160 /* flush entries from previous scans */
1161 spin_lock_bh(&rdev->bss_lock);
1162 __cfg80211_bss_expire(rdev, req->scan_start);
1163 spin_unlock_bh(&rdev->bss_lock);
1164 req->scan_start = jiffies;
1166 nl80211_send_sched_scan(req,
1167 NL80211_CMD_SCHED_SCAN_RESULTS);
1170 wiphy_unlock(&rdev->wiphy);
1173 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1175 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1176 struct cfg80211_sched_scan_request *request;
1178 trace_cfg80211_sched_scan_results(wiphy, reqid);
1179 /* ignore if we're not scanning */
1182 request = cfg80211_find_sched_scan_req(rdev, reqid);
1184 request->report_results = true;
1185 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1189 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1191 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1193 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1195 lockdep_assert_held(&wiphy->mtx);
1197 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1199 __cfg80211_stop_sched_scan(rdev, reqid, true);
1201 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1203 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1206 cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1207 wiphy_unlock(wiphy);
1209 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1211 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1212 struct cfg80211_sched_scan_request *req,
1213 bool driver_initiated)
1215 lockdep_assert_held(&rdev->wiphy.mtx);
1217 if (!driver_initiated) {
1218 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1223 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1225 cfg80211_del_sched_scan_req(rdev, req);
1230 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1231 u64 reqid, bool driver_initiated)
1233 struct cfg80211_sched_scan_request *sched_scan_req;
1235 lockdep_assert_held(&rdev->wiphy.mtx);
1237 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1238 if (!sched_scan_req)
1241 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1245 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1246 unsigned long age_secs)
1248 struct cfg80211_internal_bss *bss;
1249 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
1251 spin_lock_bh(&rdev->bss_lock);
1252 list_for_each_entry(bss, &rdev->bss_list, list)
1253 bss->ts -= age_jiffies;
1254 spin_unlock_bh(&rdev->bss_lock);
1257 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1259 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1262 void cfg80211_bss_flush(struct wiphy *wiphy)
1264 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1266 spin_lock_bh(&rdev->bss_lock);
1267 __cfg80211_bss_expire(rdev, jiffies);
1268 spin_unlock_bh(&rdev->bss_lock);
1270 EXPORT_SYMBOL(cfg80211_bss_flush);
1272 const struct element *
1273 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1274 const u8 *match, unsigned int match_len,
1275 unsigned int match_offset)
1277 const struct element *elem;
1279 for_each_element_id(elem, eid, ies, len) {
1280 if (elem->datalen >= match_offset + match_len &&
1281 !memcmp(elem->data + match_offset, match, match_len))
1287 EXPORT_SYMBOL(cfg80211_find_elem_match);
1289 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1293 const struct element *elem;
1294 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1295 int match_len = (oui_type < 0) ? 3 : sizeof(match);
1297 if (WARN_ON(oui_type > 0xff))
1300 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1301 match, match_len, 0);
1303 if (!elem || elem->datalen < 4)
1308 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1311 * enum bss_compare_mode - BSS compare mode
1312 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1313 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1314 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1316 enum bss_compare_mode {
1322 static int cmp_bss(struct cfg80211_bss *a,
1323 struct cfg80211_bss *b,
1324 enum bss_compare_mode mode)
1326 const struct cfg80211_bss_ies *a_ies, *b_ies;
1327 const u8 *ie1 = NULL;
1328 const u8 *ie2 = NULL;
1331 if (a->channel != b->channel)
1332 return b->channel->center_freq - a->channel->center_freq;
1334 a_ies = rcu_access_pointer(a->ies);
1337 b_ies = rcu_access_pointer(b->ies);
1341 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1342 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1343 a_ies->data, a_ies->len);
1344 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1345 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1346 b_ies->data, b_ies->len);
1350 if (ie1[1] == ie2[1])
1351 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1353 mesh_id_cmp = ie2[1] - ie1[1];
1355 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1356 a_ies->data, a_ies->len);
1357 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1358 b_ies->data, b_ies->len);
1362 if (ie1[1] != ie2[1])
1363 return ie2[1] - ie1[1];
1364 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1368 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1372 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1373 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1379 * Note that with "hide_ssid", the function returns a match if
1380 * the already-present BSS ("b") is a hidden SSID beacon for
1381 * the new BSS ("a").
1384 /* sort missing IE before (left of) present IE */
1391 case BSS_CMP_HIDE_ZLEN:
1393 * In ZLEN mode we assume the BSS entry we're
1394 * looking for has a zero-length SSID. So if
1395 * the one we're looking at right now has that,
1396 * return 0. Otherwise, return the difference
1397 * in length, but since we're looking for the
1398 * 0-length it's really equivalent to returning
1399 * the length of the one we're looking at.
1401 * No content comparison is needed as we assume
1402 * the content length is zero.
1405 case BSS_CMP_REGULAR:
1407 /* sort by length first, then by contents */
1408 if (ie1[1] != ie2[1])
1409 return ie2[1] - ie1[1];
1410 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1411 case BSS_CMP_HIDE_NUL:
1412 if (ie1[1] != ie2[1])
1413 return ie2[1] - ie1[1];
1414 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1415 for (i = 0; i < ie2[1]; i++)
1422 static bool cfg80211_bss_type_match(u16 capability,
1423 enum nl80211_band band,
1424 enum ieee80211_bss_type bss_type)
1429 if (bss_type == IEEE80211_BSS_TYPE_ANY)
1432 if (band == NL80211_BAND_60GHZ) {
1433 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1435 case IEEE80211_BSS_TYPE_ESS:
1436 val = WLAN_CAPABILITY_DMG_TYPE_AP;
1438 case IEEE80211_BSS_TYPE_PBSS:
1439 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1441 case IEEE80211_BSS_TYPE_IBSS:
1442 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1448 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1450 case IEEE80211_BSS_TYPE_ESS:
1451 val = WLAN_CAPABILITY_ESS;
1453 case IEEE80211_BSS_TYPE_IBSS:
1454 val = WLAN_CAPABILITY_IBSS;
1456 case IEEE80211_BSS_TYPE_MBSS:
1464 ret = ((capability & mask) == val);
1468 /* Returned bss is reference counted and must be cleaned up appropriately. */
1469 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
1470 struct ieee80211_channel *channel,
1472 const u8 *ssid, size_t ssid_len,
1473 enum ieee80211_bss_type bss_type,
1474 enum ieee80211_privacy privacy)
1476 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1477 struct cfg80211_internal_bss *bss, *res = NULL;
1478 unsigned long now = jiffies;
1481 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1484 spin_lock_bh(&rdev->bss_lock);
1486 list_for_each_entry(bss, &rdev->bss_list, list) {
1487 if (!cfg80211_bss_type_match(bss->pub.capability,
1488 bss->pub.channel->band, bss_type))
1491 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1492 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1493 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1495 if (channel && bss->pub.channel != channel)
1497 if (!is_valid_ether_addr(bss->pub.bssid))
1499 /* Don't get expired BSS structs */
1500 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1501 !atomic_read(&bss->hold))
1503 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
1505 bss_ref_get(rdev, res);
1510 spin_unlock_bh(&rdev->bss_lock);
1513 trace_cfg80211_return_bss(&res->pub);
1516 EXPORT_SYMBOL(cfg80211_get_bss);
1518 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1519 struct cfg80211_internal_bss *bss)
1521 struct rb_node **p = &rdev->bss_tree.rb_node;
1522 struct rb_node *parent = NULL;
1523 struct cfg80211_internal_bss *tbss;
1528 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1530 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1532 if (WARN_ON(!cmp)) {
1533 /* will sort of leak this BSS */
1540 p = &(*p)->rb_right;
1543 rb_link_node(&bss->rbn, parent, p);
1544 rb_insert_color(&bss->rbn, &rdev->bss_tree);
1547 static struct cfg80211_internal_bss *
1548 rb_find_bss(struct cfg80211_registered_device *rdev,
1549 struct cfg80211_internal_bss *res,
1550 enum bss_compare_mode mode)
1552 struct rb_node *n = rdev->bss_tree.rb_node;
1553 struct cfg80211_internal_bss *bss;
1557 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1558 r = cmp_bss(&res->pub, &bss->pub, mode);
1571 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1572 struct cfg80211_internal_bss *new)
1574 const struct cfg80211_bss_ies *ies;
1575 struct cfg80211_internal_bss *bss;
1581 ies = rcu_access_pointer(new->pub.beacon_ies);
1585 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1592 for (i = 0; i < ssidlen; i++)
1596 /* not a hidden SSID */
1600 /* This is the bad part ... */
1602 list_for_each_entry(bss, &rdev->bss_list, list) {
1604 * we're iterating all the entries anyway, so take the
1605 * opportunity to validate the list length accounting
1609 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1611 if (bss->pub.channel != new->pub.channel)
1613 if (bss->pub.scan_width != new->pub.scan_width)
1615 if (rcu_access_pointer(bss->pub.beacon_ies))
1617 ies = rcu_access_pointer(bss->pub.ies);
1620 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1623 if (ssidlen && ie[1] != ssidlen)
1625 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1627 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1628 list_del(&bss->hidden_list);
1630 list_add(&bss->hidden_list, &new->hidden_list);
1631 bss->pub.hidden_beacon_bss = &new->pub;
1632 new->refcount += bss->refcount;
1633 rcu_assign_pointer(bss->pub.beacon_ies,
1634 new->pub.beacon_ies);
1637 WARN_ONCE(n_entries != rdev->bss_entries,
1638 "rdev bss entries[%d]/list[len:%d] corruption\n",
1639 rdev->bss_entries, n_entries);
1644 struct cfg80211_non_tx_bss {
1645 struct cfg80211_bss *tx_bss;
1646 u8 max_bssid_indicator;
1650 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1651 const struct cfg80211_bss_ies *new_ies,
1652 const struct cfg80211_bss_ies *old_ies)
1654 struct cfg80211_internal_bss *bss;
1656 /* Assign beacon IEs to all sub entries */
1657 list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1658 const struct cfg80211_bss_ies *ies;
1660 ies = rcu_access_pointer(bss->pub.beacon_ies);
1661 WARN_ON(ies != old_ies);
1663 rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1668 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1669 struct cfg80211_internal_bss *known,
1670 struct cfg80211_internal_bss *new,
1673 lockdep_assert_held(&rdev->bss_lock);
1676 if (rcu_access_pointer(new->pub.proberesp_ies)) {
1677 const struct cfg80211_bss_ies *old;
1679 old = rcu_access_pointer(known->pub.proberesp_ies);
1681 rcu_assign_pointer(known->pub.proberesp_ies,
1682 new->pub.proberesp_ies);
1683 /* Override possible earlier Beacon frame IEs */
1684 rcu_assign_pointer(known->pub.ies,
1685 new->pub.proberesp_ies);
1687 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1688 } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1689 const struct cfg80211_bss_ies *old;
1691 if (known->pub.hidden_beacon_bss &&
1692 !list_empty(&known->hidden_list)) {
1693 const struct cfg80211_bss_ies *f;
1695 /* The known BSS struct is one of the probe
1696 * response members of a group, but we're
1697 * receiving a beacon (beacon_ies in the new
1698 * bss is used). This can only mean that the
1699 * AP changed its beacon from not having an
1700 * SSID to showing it, which is confusing so
1701 * drop this information.
1704 f = rcu_access_pointer(new->pub.beacon_ies);
1705 kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1709 old = rcu_access_pointer(known->pub.beacon_ies);
1711 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1713 /* Override IEs if they were from a beacon before */
1714 if (old == rcu_access_pointer(known->pub.ies))
1715 rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1717 cfg80211_update_hidden_bsses(known,
1718 rcu_access_pointer(new->pub.beacon_ies),
1722 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1725 known->pub.beacon_interval = new->pub.beacon_interval;
1727 /* don't update the signal if beacon was heard on
1731 known->pub.signal = new->pub.signal;
1732 known->pub.capability = new->pub.capability;
1733 known->ts = new->ts;
1734 known->ts_boottime = new->ts_boottime;
1735 known->parent_tsf = new->parent_tsf;
1736 known->pub.chains = new->pub.chains;
1737 memcpy(known->pub.chain_signal, new->pub.chain_signal,
1738 IEEE80211_MAX_CHAINS);
1739 ether_addr_copy(known->parent_bssid, new->parent_bssid);
1740 known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1741 known->pub.bssid_index = new->pub.bssid_index;
1746 /* Returned bss is reference counted and must be cleaned up appropriately. */
1747 struct cfg80211_internal_bss *
1748 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1749 struct cfg80211_internal_bss *tmp,
1750 bool signal_valid, unsigned long ts)
1752 struct cfg80211_internal_bss *found = NULL;
1754 if (WARN_ON(!tmp->pub.channel))
1759 spin_lock_bh(&rdev->bss_lock);
1761 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1762 spin_unlock_bh(&rdev->bss_lock);
1766 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1769 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1772 struct cfg80211_internal_bss *new;
1773 struct cfg80211_internal_bss *hidden;
1774 struct cfg80211_bss_ies *ies;
1777 * create a copy -- the "res" variable that is passed in
1778 * is allocated on the stack since it's not needed in the
1779 * more common case of an update
1781 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1784 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1786 kfree_rcu(ies, rcu_head);
1787 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1789 kfree_rcu(ies, rcu_head);
1792 memcpy(new, tmp, sizeof(*new));
1794 INIT_LIST_HEAD(&new->hidden_list);
1795 INIT_LIST_HEAD(&new->pub.nontrans_list);
1796 /* we'll set this later if it was non-NULL */
1797 new->pub.transmitted_bss = NULL;
1799 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1800 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1802 hidden = rb_find_bss(rdev, tmp,
1805 new->pub.hidden_beacon_bss = &hidden->pub;
1806 list_add(&new->hidden_list,
1807 &hidden->hidden_list);
1809 rcu_assign_pointer(new->pub.beacon_ies,
1810 hidden->pub.beacon_ies);
1814 * Ok so we found a beacon, and don't have an entry. If
1815 * it's a beacon with hidden SSID, we might be in for an
1816 * expensive search for any probe responses that should
1817 * be grouped with this beacon for updates ...
1819 if (!cfg80211_combine_bsses(rdev, new)) {
1820 bss_ref_put(rdev, new);
1825 if (rdev->bss_entries >= bss_entries_limit &&
1826 !cfg80211_bss_expire_oldest(rdev)) {
1827 bss_ref_put(rdev, new);
1831 /* This must be before the call to bss_ref_get */
1832 if (tmp->pub.transmitted_bss) {
1833 struct cfg80211_internal_bss *pbss =
1834 container_of(tmp->pub.transmitted_bss,
1835 struct cfg80211_internal_bss,
1838 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1839 bss_ref_get(rdev, pbss);
1842 list_add_tail(&new->list, &rdev->bss_list);
1843 rdev->bss_entries++;
1844 rb_insert_bss(rdev, new);
1848 rdev->bss_generation++;
1849 bss_ref_get(rdev, found);
1850 spin_unlock_bh(&rdev->bss_lock);
1854 spin_unlock_bh(&rdev->bss_lock);
1858 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
1859 enum nl80211_band band,
1860 enum cfg80211_bss_frame_type ftype)
1862 const struct element *tmp;
1864 if (band == NL80211_BAND_6GHZ) {
1865 struct ieee80211_he_operation *he_oper;
1867 tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
1869 if (tmp && tmp->datalen >= sizeof(*he_oper) &&
1870 tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
1871 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
1873 he_oper = (void *)&tmp->data[1];
1875 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
1879 if (ftype != CFG80211_BSS_FTYPE_BEACON ||
1880 he_6ghz_oper->control & IEEE80211_HE_6GHZ_OPER_CTRL_DUP_BEACON)
1881 return he_6ghz_oper->primary;
1883 } else if (band == NL80211_BAND_S1GHZ) {
1884 tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
1885 if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
1886 struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
1888 return s1gop->oper_ch;
1891 tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
1892 if (tmp && tmp->datalen == 1)
1893 return tmp->data[0];
1895 tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
1897 tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
1898 struct ieee80211_ht_operation *htop = (void *)tmp->data;
1900 return htop->primary_chan;
1906 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
1909 * Update RX channel information based on the available frame payload
1910 * information. This is mainly for the 2.4 GHz band where frames can be received
1911 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1912 * element to indicate the current (transmitting) channel, but this might also
1913 * be needed on other bands if RX frequency does not match with the actual
1914 * operating channel of a BSS, or if the AP reports a different primary channel.
1916 static struct ieee80211_channel *
1917 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1918 struct ieee80211_channel *channel,
1919 enum nl80211_bss_scan_width scan_width,
1920 enum cfg80211_bss_frame_type ftype)
1924 struct ieee80211_channel *alt_channel;
1926 channel_number = cfg80211_get_ies_channel_number(ie, ielen,
1927 channel->band, ftype);
1929 if (channel_number < 0) {
1930 /* No channel information in frame payload */
1934 freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1937 * In 6GHz, duplicated beacon indication is relevant for
1940 if (channel->band == NL80211_BAND_6GHZ &&
1941 (freq == channel->center_freq ||
1942 abs(freq - channel->center_freq) > 80))
1945 alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1947 if (channel->band == NL80211_BAND_2GHZ) {
1949 * Better not allow unexpected channels when that could
1950 * be going beyond the 1-11 range (e.g., discovering
1951 * BSS on channel 12 when radio is configured for
1957 /* No match for the payload channel number - ignore it */
1961 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1962 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1964 * Ignore channel number in 5 and 10 MHz channels where there
1965 * may not be an n:1 or 1:n mapping between frequencies and
1972 * Use the channel determined through the payload channel number
1973 * instead of the RX channel reported by the driver.
1975 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1980 /* Returned bss is reference counted and must be cleaned up appropriately. */
1981 static struct cfg80211_bss *
1982 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1983 struct cfg80211_inform_bss *data,
1984 enum cfg80211_bss_frame_type ftype,
1985 const u8 *bssid, u64 tsf, u16 capability,
1986 u16 beacon_interval, const u8 *ie, size_t ielen,
1987 struct cfg80211_non_tx_bss *non_tx_data,
1990 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1991 struct cfg80211_bss_ies *ies;
1992 struct ieee80211_channel *channel;
1993 struct cfg80211_internal_bss tmp = {}, *res;
1998 if (WARN_ON(!wiphy))
2001 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2002 (data->signal < 0 || data->signal > 100)))
2005 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
2006 data->scan_width, ftype);
2010 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2011 tmp.pub.channel = channel;
2012 tmp.pub.scan_width = data->scan_width;
2013 tmp.pub.signal = data->signal;
2014 tmp.pub.beacon_interval = beacon_interval;
2015 tmp.pub.capability = capability;
2016 tmp.ts_boottime = data->boottime_ns;
2017 tmp.parent_tsf = data->parent_tsf;
2018 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2021 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
2022 ts = bss_from_pub(non_tx_data->tx_bss)->ts;
2023 tmp.pub.bssid_index = non_tx_data->bssid_index;
2024 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
2030 * If we do not know here whether the IEs are from a Beacon or Probe
2031 * Response frame, we need to pick one of the options and only use it
2032 * with the driver that does not provide the full Beacon/Probe Response
2033 * frame. Use Beacon frame pointer to avoid indicating that this should
2034 * override the IEs pointer should we have received an earlier
2035 * indication of Probe Response data.
2037 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2042 ies->from_beacon = false;
2043 memcpy(ies->data, ie, ielen);
2046 case CFG80211_BSS_FTYPE_BEACON:
2047 ies->from_beacon = true;
2049 case CFG80211_BSS_FTYPE_UNKNOWN:
2050 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2052 case CFG80211_BSS_FTYPE_PRESP:
2053 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2056 rcu_assign_pointer(tmp.pub.ies, ies);
2058 signal_valid = data->chan == channel;
2059 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
2063 if (channel->band == NL80211_BAND_60GHZ) {
2064 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2065 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2066 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2067 regulatory_hint_found_beacon(wiphy, channel, gfp);
2069 if (res->pub.capability & WLAN_CAPABILITY_ESS)
2070 regulatory_hint_found_beacon(wiphy, channel, gfp);
2074 /* this is a nontransmitting bss, we need to add it to
2075 * transmitting bss' list if it is not there
2077 spin_lock_bh(&rdev->bss_lock);
2078 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
2080 if (__cfg80211_unlink_bss(rdev, res)) {
2081 rdev->bss_generation++;
2085 spin_unlock_bh(&rdev->bss_lock);
2091 trace_cfg80211_return_bss(&res->pub);
2092 /* cfg80211_bss_update gives us a referenced result */
2096 static const struct element
2097 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
2098 const struct element *mbssid_elem,
2099 const struct element *sub_elem)
2101 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
2102 const struct element *next_mbssid;
2103 const struct element *next_sub;
2105 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2107 ielen - (mbssid_end - ie));
2110 * If it is not the last subelement in current MBSSID IE or there isn't
2111 * a next MBSSID IE - profile is complete.
2113 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
2117 /* For any length error, just return NULL */
2119 if (next_mbssid->datalen < 4)
2122 next_sub = (void *)&next_mbssid->data[1];
2124 if (next_mbssid->data + next_mbssid->datalen <
2125 next_sub->data + next_sub->datalen)
2128 if (next_sub->id != 0 || next_sub->datalen < 2)
2132 * Check if the first element in the next sub element is a start
2135 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
2139 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2140 const struct element *mbssid_elem,
2141 const struct element *sub_elem,
2142 u8 *merged_ie, size_t max_copy_len)
2144 size_t copied_len = sub_elem->datalen;
2145 const struct element *next_mbssid;
2147 if (sub_elem->datalen > max_copy_len)
2150 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2152 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
2155 const struct element *next_sub = (void *)&next_mbssid->data[1];
2157 if (copied_len + next_sub->datalen > max_copy_len)
2159 memcpy(merged_ie + copied_len, next_sub->data,
2161 copied_len += next_sub->datalen;
2166 EXPORT_SYMBOL(cfg80211_merge_profile);
2168 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2169 struct cfg80211_inform_bss *data,
2170 enum cfg80211_bss_frame_type ftype,
2171 const u8 *bssid, u64 tsf,
2172 u16 beacon_interval, const u8 *ie,
2174 struct cfg80211_non_tx_bss *non_tx_data,
2177 const u8 *mbssid_index_ie;
2178 const struct element *elem, *sub;
2180 u8 new_bssid[ETH_ALEN];
2181 u8 *new_ie, *profile;
2182 u64 seen_indices = 0;
2184 struct cfg80211_bss *bss;
2188 if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2190 if (!wiphy->support_mbssid)
2192 if (wiphy->support_only_he_mbssid &&
2193 !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2196 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2200 profile = kmalloc(ielen, gfp);
2204 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
2205 if (elem->datalen < 4)
2207 if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2209 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2212 if (sub->id != 0 || sub->datalen < 4) {
2213 /* not a valid BSS profile */
2217 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2218 sub->data[1] != 2) {
2219 /* The first element within the Nontransmitted
2220 * BSSID Profile is not the Nontransmitted
2221 * BSSID Capability element.
2226 memset(profile, 0, ielen);
2227 profile_len = cfg80211_merge_profile(ie, ielen,
2233 /* found a Nontransmitted BSSID Profile */
2234 mbssid_index_ie = cfg80211_find_ie
2235 (WLAN_EID_MULTI_BSSID_IDX,
2236 profile, profile_len);
2237 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2238 mbssid_index_ie[2] == 0 ||
2239 mbssid_index_ie[2] > 46) {
2240 /* No valid Multiple BSSID-Index element */
2244 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2245 /* We don't support legacy split of a profile */
2246 net_dbg_ratelimited("Partial info for BSSID index %d\n",
2247 mbssid_index_ie[2]);
2249 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2251 non_tx_data->bssid_index = mbssid_index_ie[2];
2252 non_tx_data->max_bssid_indicator = elem->data[0];
2254 cfg80211_gen_new_bssid(bssid,
2255 non_tx_data->max_bssid_indicator,
2256 non_tx_data->bssid_index,
2258 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2259 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
2261 profile_len, new_ie,
2262 IEEE80211_MAX_DATA_LEN);
2266 capability = get_unaligned_le16(profile + 2);
2267 bss = cfg80211_inform_single_bss_data(wiphy, data,
2278 cfg80211_put_bss(wiphy, bss);
2287 struct cfg80211_bss *
2288 cfg80211_inform_bss_data(struct wiphy *wiphy,
2289 struct cfg80211_inform_bss *data,
2290 enum cfg80211_bss_frame_type ftype,
2291 const u8 *bssid, u64 tsf, u16 capability,
2292 u16 beacon_interval, const u8 *ie, size_t ielen,
2295 struct cfg80211_bss *res;
2296 struct cfg80211_non_tx_bss non_tx_data;
2298 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
2299 capability, beacon_interval, ie,
2303 non_tx_data.tx_bss = res;
2304 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
2305 beacon_interval, ie, ielen, &non_tx_data,
2309 EXPORT_SYMBOL(cfg80211_inform_bss_data);
2311 /* cfg80211_inform_bss_width_frame helper */
2312 static struct cfg80211_bss *
2313 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
2314 struct cfg80211_inform_bss *data,
2315 struct ieee80211_mgmt *mgmt, size_t len,
2318 struct cfg80211_internal_bss tmp = {}, *res;
2319 struct cfg80211_bss_ies *ies;
2320 struct ieee80211_channel *channel;
2322 struct ieee80211_ext *ext = NULL;
2323 u8 *bssid, *variable;
2324 u16 capability, beacon_int;
2325 size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
2326 u.probe_resp.variable);
2328 enum cfg80211_bss_frame_type ftype;
2330 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
2331 offsetof(struct ieee80211_mgmt, u.beacon.variable));
2333 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
2338 if (WARN_ON(!wiphy))
2341 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2342 (data->signal < 0 || data->signal > 100)))
2345 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2346 ext = (void *) mgmt;
2347 min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
2348 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2349 min_hdr_len = offsetof(struct ieee80211_ext,
2350 u.s1g_short_beacon.variable);
2353 if (WARN_ON(len < min_hdr_len))
2356 ielen = len - min_hdr_len;
2357 variable = mgmt->u.probe_resp.variable;
2359 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2360 variable = ext->u.s1g_short_beacon.variable;
2362 variable = ext->u.s1g_beacon.variable;
2365 if (ieee80211_is_beacon(mgmt->frame_control))
2366 ftype = CFG80211_BSS_FTYPE_BEACON;
2367 else if (ieee80211_is_probe_resp(mgmt->frame_control))
2368 ftype = CFG80211_BSS_FTYPE_PRESP;
2370 ftype = CFG80211_BSS_FTYPE_UNKNOWN;
2372 channel = cfg80211_get_bss_channel(wiphy, variable,
2373 ielen, data->chan, data->scan_width,
2379 const struct ieee80211_s1g_bcn_compat_ie *compat;
2380 const struct element *elem;
2382 elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
2386 if (elem->datalen < sizeof(*compat))
2388 compat = (void *)elem->data;
2389 bssid = ext->u.s1g_beacon.sa;
2390 capability = le16_to_cpu(compat->compat_info);
2391 beacon_int = le16_to_cpu(compat->beacon_int);
2393 bssid = mgmt->bssid;
2394 beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2395 capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
2398 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2402 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2403 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
2404 ieee80211_is_s1g_beacon(mgmt->frame_control);
2405 memcpy(ies->data, variable, ielen);
2407 if (ieee80211_is_probe_resp(mgmt->frame_control))
2408 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2410 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2411 rcu_assign_pointer(tmp.pub.ies, ies);
2413 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2414 tmp.pub.beacon_interval = beacon_int;
2415 tmp.pub.capability = capability;
2416 tmp.pub.channel = channel;
2417 tmp.pub.scan_width = data->scan_width;
2418 tmp.pub.signal = data->signal;
2419 tmp.ts_boottime = data->boottime_ns;
2420 tmp.parent_tsf = data->parent_tsf;
2421 tmp.pub.chains = data->chains;
2422 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
2423 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2425 signal_valid = data->chan == channel;
2426 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
2431 if (channel->band == NL80211_BAND_60GHZ) {
2432 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2433 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2434 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2435 regulatory_hint_found_beacon(wiphy, channel, gfp);
2437 if (res->pub.capability & WLAN_CAPABILITY_ESS)
2438 regulatory_hint_found_beacon(wiphy, channel, gfp);
2441 trace_cfg80211_return_bss(&res->pub);
2442 /* cfg80211_bss_update gives us a referenced result */
2446 struct cfg80211_bss *
2447 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2448 struct cfg80211_inform_bss *data,
2449 struct ieee80211_mgmt *mgmt, size_t len,
2452 struct cfg80211_bss *res;
2453 const u8 *ie = mgmt->u.probe_resp.variable;
2454 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2455 u.probe_resp.variable);
2456 enum cfg80211_bss_frame_type ftype;
2457 struct cfg80211_non_tx_bss non_tx_data = {};
2459 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
2464 /* don't do any further MBSSID handling for S1G */
2465 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
2468 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
2469 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
2470 non_tx_data.tx_bss = res;
2472 /* process each non-transmitting bss */
2473 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
2474 le64_to_cpu(mgmt->u.probe_resp.timestamp),
2475 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
2476 ie, ielen, &non_tx_data, gfp);
2480 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2482 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2484 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2485 struct cfg80211_internal_bss *bss;
2490 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2492 spin_lock_bh(&rdev->bss_lock);
2493 bss_ref_get(rdev, bss);
2494 spin_unlock_bh(&rdev->bss_lock);
2496 EXPORT_SYMBOL(cfg80211_ref_bss);
2498 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2500 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2501 struct cfg80211_internal_bss *bss;
2506 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2508 spin_lock_bh(&rdev->bss_lock);
2509 bss_ref_put(rdev, bss);
2510 spin_unlock_bh(&rdev->bss_lock);
2512 EXPORT_SYMBOL(cfg80211_put_bss);
2514 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2516 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2517 struct cfg80211_internal_bss *bss, *tmp1;
2518 struct cfg80211_bss *nontrans_bss, *tmp;
2523 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2525 spin_lock_bh(&rdev->bss_lock);
2526 if (list_empty(&bss->list))
2529 list_for_each_entry_safe(nontrans_bss, tmp,
2530 &pub->nontrans_list,
2532 tmp1 = container_of(nontrans_bss,
2533 struct cfg80211_internal_bss, pub);
2534 if (__cfg80211_unlink_bss(rdev, tmp1))
2535 rdev->bss_generation++;
2538 if (__cfg80211_unlink_bss(rdev, bss))
2539 rdev->bss_generation++;
2541 spin_unlock_bh(&rdev->bss_lock);
2543 EXPORT_SYMBOL(cfg80211_unlink_bss);
2545 void cfg80211_bss_iter(struct wiphy *wiphy,
2546 struct cfg80211_chan_def *chandef,
2547 void (*iter)(struct wiphy *wiphy,
2548 struct cfg80211_bss *bss,
2552 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2553 struct cfg80211_internal_bss *bss;
2555 spin_lock_bh(&rdev->bss_lock);
2557 list_for_each_entry(bss, &rdev->bss_list, list) {
2558 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
2560 iter(wiphy, &bss->pub, iter_data);
2563 spin_unlock_bh(&rdev->bss_lock);
2565 EXPORT_SYMBOL(cfg80211_bss_iter);
2567 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2568 unsigned int link_id,
2569 struct ieee80211_channel *chan)
2571 struct wiphy *wiphy = wdev->wiphy;
2572 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2573 struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
2574 struct cfg80211_internal_bss *new = NULL;
2575 struct cfg80211_internal_bss *bss;
2576 struct cfg80211_bss *nontrans_bss;
2577 struct cfg80211_bss *tmp;
2579 spin_lock_bh(&rdev->bss_lock);
2582 * Some APs use CSA also for bandwidth changes, i.e., without actually
2583 * changing the control channel, so no need to update in such a case.
2585 if (cbss->pub.channel == chan)
2588 /* use transmitting bss */
2589 if (cbss->pub.transmitted_bss)
2590 cbss = container_of(cbss->pub.transmitted_bss,
2591 struct cfg80211_internal_bss,
2594 cbss->pub.channel = chan;
2596 list_for_each_entry(bss, &rdev->bss_list, list) {
2597 if (!cfg80211_bss_type_match(bss->pub.capability,
2598 bss->pub.channel->band,
2599 wdev->conn_bss_type))
2605 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2612 /* to save time, update IEs for transmitting bss only */
2613 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2614 new->pub.proberesp_ies = NULL;
2615 new->pub.beacon_ies = NULL;
2618 list_for_each_entry_safe(nontrans_bss, tmp,
2619 &new->pub.nontrans_list,
2621 bss = container_of(nontrans_bss,
2622 struct cfg80211_internal_bss, pub);
2623 if (__cfg80211_unlink_bss(rdev, bss))
2624 rdev->bss_generation++;
2627 WARN_ON(atomic_read(&new->hold));
2628 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2629 rdev->bss_generation++;
2632 rb_erase(&cbss->rbn, &rdev->bss_tree);
2633 rb_insert_bss(rdev, cbss);
2634 rdev->bss_generation++;
2636 list_for_each_entry_safe(nontrans_bss, tmp,
2637 &cbss->pub.nontrans_list,
2639 bss = container_of(nontrans_bss,
2640 struct cfg80211_internal_bss, pub);
2641 bss->pub.channel = chan;
2642 rb_erase(&bss->rbn, &rdev->bss_tree);
2643 rb_insert_bss(rdev, bss);
2644 rdev->bss_generation++;
2648 spin_unlock_bh(&rdev->bss_lock);
2651 #ifdef CONFIG_CFG80211_WEXT
2652 static struct cfg80211_registered_device *
2653 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2655 struct cfg80211_registered_device *rdev;
2656 struct net_device *dev;
2660 dev = dev_get_by_index(net, ifindex);
2662 return ERR_PTR(-ENODEV);
2663 if (dev->ieee80211_ptr)
2664 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2666 rdev = ERR_PTR(-ENODEV);
2671 int cfg80211_wext_siwscan(struct net_device *dev,
2672 struct iw_request_info *info,
2673 union iwreq_data *wrqu, char *extra)
2675 struct cfg80211_registered_device *rdev;
2676 struct wiphy *wiphy;
2677 struct iw_scan_req *wreq = NULL;
2678 struct cfg80211_scan_request *creq;
2679 int i, err, n_channels = 0;
2680 enum nl80211_band band;
2682 if (!netif_running(dev))
2685 if (wrqu->data.length == sizeof(struct iw_scan_req))
2686 wreq = (struct iw_scan_req *)extra;
2688 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2691 return PTR_ERR(rdev);
2693 if (rdev->scan_req || rdev->scan_msg)
2696 wiphy = &rdev->wiphy;
2698 /* Determine number of channels, needed to allocate creq */
2699 if (wreq && wreq->num_channels)
2700 n_channels = wreq->num_channels;
2702 n_channels = ieee80211_get_num_supported_channels(wiphy);
2704 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2705 n_channels * sizeof(void *),
2710 creq->wiphy = wiphy;
2711 creq->wdev = dev->ieee80211_ptr;
2712 /* SSIDs come after channels */
2713 creq->ssids = (void *)&creq->channels[n_channels];
2714 creq->n_channels = n_channels;
2716 creq->scan_start = jiffies;
2718 /* translate "Scan on frequencies" request */
2720 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2723 if (!wiphy->bands[band])
2726 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2727 /* ignore disabled channels */
2728 if (wiphy->bands[band]->channels[j].flags &
2729 IEEE80211_CHAN_DISABLED)
2732 /* If we have a wireless request structure and the
2733 * wireless request specifies frequencies, then search
2734 * for the matching hardware channel.
2736 if (wreq && wreq->num_channels) {
2738 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2739 for (k = 0; k < wreq->num_channels; k++) {
2740 struct iw_freq *freq =
2741 &wreq->channel_list[k];
2743 cfg80211_wext_freq(freq);
2745 if (wext_freq == wiphy_freq)
2746 goto wext_freq_found;
2748 goto wext_freq_not_found;
2752 creq->channels[i] = &wiphy->bands[band]->channels[j];
2754 wext_freq_not_found: ;
2757 /* No channels found? */
2763 /* Set real number of channels specified in creq->channels[] */
2764 creq->n_channels = i;
2766 /* translate "Scan for SSID" request */
2768 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2769 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2773 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2774 creq->ssids[0].ssid_len = wreq->essid_len;
2776 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2780 for (i = 0; i < NUM_NL80211_BANDS; i++)
2781 if (wiphy->bands[i])
2782 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2784 eth_broadcast_addr(creq->bssid);
2786 wiphy_lock(&rdev->wiphy);
2788 rdev->scan_req = creq;
2789 err = rdev_scan(rdev, creq);
2791 rdev->scan_req = NULL;
2792 /* creq will be freed below */
2794 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2795 /* creq now owned by driver */
2799 wiphy_unlock(&rdev->wiphy);
2804 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2806 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2807 const struct cfg80211_bss_ies *ies,
2808 char *current_ev, char *end_buf)
2810 const u8 *pos, *end, *next;
2811 struct iw_event iwe;
2817 * If needed, fragment the IEs buffer (at IE boundaries) into short
2818 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2821 end = pos + ies->len;
2823 while (end - pos > IW_GENERIC_IE_MAX) {
2824 next = pos + 2 + pos[1];
2825 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2826 next = next + 2 + next[1];
2828 memset(&iwe, 0, sizeof(iwe));
2829 iwe.cmd = IWEVGENIE;
2830 iwe.u.data.length = next - pos;
2831 current_ev = iwe_stream_add_point_check(info, current_ev,
2834 if (IS_ERR(current_ev))
2840 memset(&iwe, 0, sizeof(iwe));
2841 iwe.cmd = IWEVGENIE;
2842 iwe.u.data.length = end - pos;
2843 current_ev = iwe_stream_add_point_check(info, current_ev,
2846 if (IS_ERR(current_ev))
2854 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2855 struct cfg80211_internal_bss *bss, char *current_ev,
2858 const struct cfg80211_bss_ies *ies;
2859 struct iw_event iwe;
2864 bool ismesh = false;
2866 memset(&iwe, 0, sizeof(iwe));
2867 iwe.cmd = SIOCGIWAP;
2868 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2869 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2870 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2872 if (IS_ERR(current_ev))
2875 memset(&iwe, 0, sizeof(iwe));
2876 iwe.cmd = SIOCGIWFREQ;
2877 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2879 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2881 if (IS_ERR(current_ev))
2884 memset(&iwe, 0, sizeof(iwe));
2885 iwe.cmd = SIOCGIWFREQ;
2886 iwe.u.freq.m = bss->pub.channel->center_freq;
2888 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2890 if (IS_ERR(current_ev))
2893 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2894 memset(&iwe, 0, sizeof(iwe));
2896 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2897 IW_QUAL_NOISE_INVALID |
2898 IW_QUAL_QUAL_UPDATED;
2899 switch (wiphy->signal_type) {
2900 case CFG80211_SIGNAL_TYPE_MBM:
2901 sig = bss->pub.signal / 100;
2902 iwe.u.qual.level = sig;
2903 iwe.u.qual.updated |= IW_QUAL_DBM;
2904 if (sig < -110) /* rather bad */
2906 else if (sig > -40) /* perfect */
2908 /* will give a range of 0 .. 70 */
2909 iwe.u.qual.qual = sig + 110;
2911 case CFG80211_SIGNAL_TYPE_UNSPEC:
2912 iwe.u.qual.level = bss->pub.signal;
2913 /* will give range 0 .. 100 */
2914 iwe.u.qual.qual = bss->pub.signal;
2920 current_ev = iwe_stream_add_event_check(info, current_ev,
2923 if (IS_ERR(current_ev))
2927 memset(&iwe, 0, sizeof(iwe));
2928 iwe.cmd = SIOCGIWENCODE;
2929 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2930 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2932 iwe.u.data.flags = IW_ENCODE_DISABLED;
2933 iwe.u.data.length = 0;
2934 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2936 if (IS_ERR(current_ev))
2940 ies = rcu_dereference(bss->pub.ies);
2946 if (ie[1] > rem - 2)
2951 memset(&iwe, 0, sizeof(iwe));
2952 iwe.cmd = SIOCGIWESSID;
2953 iwe.u.data.length = ie[1];
2954 iwe.u.data.flags = 1;
2955 current_ev = iwe_stream_add_point_check(info,
2959 if (IS_ERR(current_ev))
2962 case WLAN_EID_MESH_ID:
2963 memset(&iwe, 0, sizeof(iwe));
2964 iwe.cmd = SIOCGIWESSID;
2965 iwe.u.data.length = ie[1];
2966 iwe.u.data.flags = 1;
2967 current_ev = iwe_stream_add_point_check(info,
2971 if (IS_ERR(current_ev))
2974 case WLAN_EID_MESH_CONFIG:
2976 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2979 memset(&iwe, 0, sizeof(iwe));
2980 iwe.cmd = IWEVCUSTOM;
2981 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2983 iwe.u.data.length = strlen(buf);
2984 current_ev = iwe_stream_add_point_check(info,
2988 if (IS_ERR(current_ev))
2990 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2992 iwe.u.data.length = strlen(buf);
2993 current_ev = iwe_stream_add_point_check(info,
2997 if (IS_ERR(current_ev))
2999 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
3001 iwe.u.data.length = strlen(buf);
3002 current_ev = iwe_stream_add_point_check(info,
3006 if (IS_ERR(current_ev))
3008 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
3009 iwe.u.data.length = strlen(buf);
3010 current_ev = iwe_stream_add_point_check(info,
3014 if (IS_ERR(current_ev))
3016 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
3017 iwe.u.data.length = strlen(buf);
3018 current_ev = iwe_stream_add_point_check(info,
3022 if (IS_ERR(current_ev))
3024 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
3025 iwe.u.data.length = strlen(buf);
3026 current_ev = iwe_stream_add_point_check(info,
3030 if (IS_ERR(current_ev))
3032 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
3033 iwe.u.data.length = strlen(buf);
3034 current_ev = iwe_stream_add_point_check(info,
3038 if (IS_ERR(current_ev))
3041 case WLAN_EID_SUPP_RATES:
3042 case WLAN_EID_EXT_SUPP_RATES:
3043 /* display all supported rates in readable format */
3044 p = current_ev + iwe_stream_lcp_len(info);
3046 memset(&iwe, 0, sizeof(iwe));
3047 iwe.cmd = SIOCGIWRATE;
3048 /* Those two flags are ignored... */
3049 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3051 for (i = 0; i < ie[1]; i++) {
3052 iwe.u.bitrate.value =
3053 ((ie[i + 2] & 0x7f) * 500000);
3055 p = iwe_stream_add_value(info, current_ev, p,
3059 current_ev = ERR_PTR(-E2BIG);
3070 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3072 memset(&iwe, 0, sizeof(iwe));
3073 iwe.cmd = SIOCGIWMODE;
3075 iwe.u.mode = IW_MODE_MESH;
3076 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3077 iwe.u.mode = IW_MODE_MASTER;
3079 iwe.u.mode = IW_MODE_ADHOC;
3080 current_ev = iwe_stream_add_event_check(info, current_ev,
3083 if (IS_ERR(current_ev))
3087 memset(&iwe, 0, sizeof(iwe));
3088 iwe.cmd = IWEVCUSTOM;
3089 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
3090 iwe.u.data.length = strlen(buf);
3091 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3093 if (IS_ERR(current_ev))
3095 memset(&iwe, 0, sizeof(iwe));
3096 iwe.cmd = IWEVCUSTOM;
3097 sprintf(buf, " Last beacon: %ums ago",
3098 elapsed_jiffies_msecs(bss->ts));
3099 iwe.u.data.length = strlen(buf);
3100 current_ev = iwe_stream_add_point_check(info, current_ev,
3101 end_buf, &iwe, buf);
3102 if (IS_ERR(current_ev))
3105 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3113 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3114 struct iw_request_info *info,
3115 char *buf, size_t len)
3117 char *current_ev = buf;
3118 char *end_buf = buf + len;
3119 struct cfg80211_internal_bss *bss;
3122 spin_lock_bh(&rdev->bss_lock);
3123 cfg80211_bss_expire(rdev);
3125 list_for_each_entry(bss, &rdev->bss_list, list) {
3126 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3130 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
3131 current_ev, end_buf);
3132 if (IS_ERR(current_ev)) {
3133 err = PTR_ERR(current_ev);
3137 spin_unlock_bh(&rdev->bss_lock);
3141 return current_ev - buf;
3145 int cfg80211_wext_giwscan(struct net_device *dev,
3146 struct iw_request_info *info,
3147 struct iw_point *data, char *extra)
3149 struct cfg80211_registered_device *rdev;
3152 if (!netif_running(dev))
3155 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3158 return PTR_ERR(rdev);
3160 if (rdev->scan_req || rdev->scan_msg)
3163 res = ieee80211_scan_results(rdev, info, extra, data->length);
3172 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);