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;
162 hbss = bss_from_pub(bss->pub.hidden_beacon_bss);
164 if (hbss->refcount == 0)
168 if (bss->pub.transmitted_bss) {
169 struct cfg80211_internal_bss *tbss;
171 tbss = bss_from_pub(bss->pub.transmitted_bss);
173 if (tbss->refcount == 0)
178 if (bss->refcount == 0)
182 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
183 struct cfg80211_internal_bss *bss)
185 lockdep_assert_held(&rdev->bss_lock);
187 if (!list_empty(&bss->hidden_list)) {
189 * don't remove the beacon entry if it has
190 * probe responses associated with it
192 if (!bss->pub.hidden_beacon_bss)
195 * if it's a probe response entry break its
196 * link to the other entries in the group
198 list_del_init(&bss->hidden_list);
201 list_del_init(&bss->list);
202 list_del_init(&bss->pub.nontrans_list);
203 rb_erase(&bss->rbn, &rdev->bss_tree);
205 WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
206 "rdev bss entries[%d]/list[empty:%d] corruption\n",
207 rdev->bss_entries, list_empty(&rdev->bss_list));
208 bss_ref_put(rdev, bss);
212 bool cfg80211_is_element_inherited(const struct element *elem,
213 const struct element *non_inherit_elem)
215 u8 id_len, ext_id_len, i, loop_len, id;
218 if (elem->id == WLAN_EID_MULTIPLE_BSSID)
221 if (elem->id == WLAN_EID_EXTENSION && elem->datalen > 1 &&
222 elem->data[0] == WLAN_EID_EXT_EHT_MULTI_LINK)
225 if (!non_inherit_elem || non_inherit_elem->datalen < 2)
229 * non inheritance element format is:
230 * ext ID (56) | IDs list len | list | extension IDs list len | list
231 * Both lists are optional. Both lengths are mandatory.
232 * This means valid length is:
233 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
235 id_len = non_inherit_elem->data[1];
236 if (non_inherit_elem->datalen < 3 + id_len)
239 ext_id_len = non_inherit_elem->data[2 + id_len];
240 if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
243 if (elem->id == WLAN_EID_EXTENSION) {
246 loop_len = ext_id_len;
247 list = &non_inherit_elem->data[3 + id_len];
253 list = &non_inherit_elem->data[2];
257 for (i = 0; i < loop_len; i++) {
264 EXPORT_SYMBOL(cfg80211_is_element_inherited);
266 static size_t cfg80211_copy_elem_with_frags(const struct element *elem,
267 const u8 *ie, size_t ie_len,
268 u8 **pos, u8 *buf, size_t buf_len)
270 if (WARN_ON((u8 *)elem < ie || elem->data > ie + ie_len ||
271 elem->data + elem->datalen > ie + ie_len))
274 if (elem->datalen + 2 > buf + buf_len - *pos)
277 memcpy(*pos, elem, elem->datalen + 2);
278 *pos += elem->datalen + 2;
280 /* Finish if it is not fragmented */
281 if (elem->datalen != 255)
284 ie_len = ie + ie_len - elem->data - elem->datalen;
285 ie = (const u8 *)elem->data + elem->datalen;
287 for_each_element(elem, ie, ie_len) {
288 if (elem->id != WLAN_EID_FRAGMENT)
291 if (elem->datalen + 2 > buf + buf_len - *pos)
294 memcpy(*pos, elem, elem->datalen + 2);
295 *pos += elem->datalen + 2;
297 if (elem->datalen != 255)
304 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
305 const u8 *subie, size_t subie_len,
306 u8 *new_ie, size_t new_ie_len)
308 const struct element *non_inherit_elem, *parent, *sub;
311 unsigned int match_len;
313 non_inherit_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
316 /* We copy the elements one by one from the parent to the generated
318 * If they are not inherited (included in subie or in the non
319 * inheritance element), then we copy all occurrences the first time
320 * we see this element type.
322 for_each_element(parent, ie, ielen) {
323 if (parent->id == WLAN_EID_FRAGMENT)
326 if (parent->id == WLAN_EID_EXTENSION) {
327 if (parent->datalen < 1)
330 id = WLAN_EID_EXTENSION;
331 ext_id = parent->data[0];
338 /* Find first occurrence in subie */
339 sub = cfg80211_find_elem_match(id, subie, subie_len,
340 &ext_id, match_len, 0);
342 /* Copy from parent if not in subie and inherited */
344 cfg80211_is_element_inherited(parent, non_inherit_elem)) {
345 if (!cfg80211_copy_elem_with_frags(parent,
354 /* Already copied if an earlier element had the same type */
355 if (cfg80211_find_elem_match(id, ie, (u8 *)parent - ie,
356 &ext_id, match_len, 0))
359 /* Not inheriting, copy all similar elements from subie */
361 if (!cfg80211_copy_elem_with_frags(sub,
367 sub = cfg80211_find_elem_match(id,
368 sub->data + sub->datalen,
372 &ext_id, match_len, 0);
376 /* The above misses elements that are included in subie but not in the
377 * parent, so do a pass over subie and append those.
378 * Skip the non-tx BSSID caps and non-inheritance element.
380 for_each_element(sub, subie, subie_len) {
381 if (sub->id == WLAN_EID_NON_TX_BSSID_CAP)
384 if (sub->id == WLAN_EID_FRAGMENT)
387 if (sub->id == WLAN_EID_EXTENSION) {
388 if (sub->datalen < 1)
391 id = WLAN_EID_EXTENSION;
392 ext_id = sub->data[0];
395 if (ext_id == WLAN_EID_EXT_NON_INHERITANCE)
402 /* Processed if one was included in the parent */
403 if (cfg80211_find_elem_match(id, ie, ielen,
404 &ext_id, match_len, 0))
407 if (!cfg80211_copy_elem_with_frags(sub, subie, subie_len,
408 &pos, new_ie, new_ie_len))
415 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
416 const u8 *ssid, size_t ssid_len)
418 const struct cfg80211_bss_ies *ies;
419 const struct element *ssid_elem;
421 if (bssid && !ether_addr_equal(a->bssid, bssid))
427 ies = rcu_access_pointer(a->ies);
430 ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
433 if (ssid_elem->datalen != ssid_len)
435 return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
439 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
440 struct cfg80211_bss *nontrans_bss)
442 const struct element *ssid_elem;
443 struct cfg80211_bss *bss = NULL;
446 ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
452 /* check if nontrans_bss is in the list */
453 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
454 if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
455 ssid_elem->datalen)) {
464 * This is a bit weird - it's not on the list, but already on another
465 * one! The only way that could happen is if there's some BSSID/SSID
466 * shared by multiple APs in their multi-BSSID profiles, potentially
467 * with hidden SSID mixed in ... ignore it.
469 if (!list_empty(&nontrans_bss->nontrans_list))
472 /* add to the list */
473 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
477 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
478 unsigned long expire_time)
480 struct cfg80211_internal_bss *bss, *tmp;
481 bool expired = false;
483 lockdep_assert_held(&rdev->bss_lock);
485 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
486 if (atomic_read(&bss->hold))
488 if (!time_after(expire_time, bss->ts))
491 if (__cfg80211_unlink_bss(rdev, bss))
496 rdev->bss_generation++;
499 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
501 struct cfg80211_internal_bss *bss, *oldest = NULL;
504 lockdep_assert_held(&rdev->bss_lock);
506 list_for_each_entry(bss, &rdev->bss_list, list) {
507 if (atomic_read(&bss->hold))
510 if (!list_empty(&bss->hidden_list) &&
511 !bss->pub.hidden_beacon_bss)
514 if (oldest && time_before(oldest->ts, bss->ts))
519 if (WARN_ON(!oldest))
523 * The callers make sure to increase rdev->bss_generation if anything
524 * gets removed (and a new entry added), so there's no need to also do
528 ret = __cfg80211_unlink_bss(rdev, oldest);
533 static u8 cfg80211_parse_bss_param(u8 data,
534 struct cfg80211_colocated_ap *coloc_ap)
536 coloc_ap->oct_recommended =
537 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
538 coloc_ap->same_ssid =
539 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
540 coloc_ap->multi_bss =
541 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
542 coloc_ap->transmitted_bssid =
543 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
544 coloc_ap->unsolicited_probe =
545 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
546 coloc_ap->colocated_ess =
547 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
549 return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
552 static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
553 const struct element **elem, u32 *s_ssid)
556 *elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
557 if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
560 *s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
564 static void cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
566 struct cfg80211_colocated_ap *ap, *tmp_ap;
568 list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
574 static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
575 const u8 *pos, u8 length,
576 const struct element *ssid_elem,
579 /* skip the TBTT offset */
582 /* ignore entries with invalid BSSID */
583 if (!is_valid_ether_addr(pos))
586 memcpy(entry->bssid, pos, ETH_ALEN);
589 if (length >= IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM) {
590 memcpy(&entry->short_ssid, pos,
591 sizeof(entry->short_ssid));
592 entry->short_ssid_valid = true;
596 /* skip non colocated APs */
597 if (!cfg80211_parse_bss_param(*pos, entry))
601 if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM) {
603 * no information about the short ssid. Consider the entry valid
604 * for now. It would later be dropped in case there are explicit
605 * SSIDs that need to be matched
607 if (!entry->same_ssid)
611 if (entry->same_ssid) {
612 entry->short_ssid = s_ssid_tmp;
613 entry->short_ssid_valid = true;
616 * This is safe because we validate datalen in
617 * cfg80211_parse_colocated_ap(), before calling this
620 memcpy(&entry->ssid, &ssid_elem->data,
622 entry->ssid_len = ssid_elem->datalen;
627 static int cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
628 struct list_head *list)
630 struct ieee80211_neighbor_ap_info *ap_info;
631 const struct element *elem, *ssid_elem;
634 int n_coloc = 0, ret;
637 elem = cfg80211_find_elem(WLAN_EID_REDUCED_NEIGHBOR_REPORT, ies->data,
643 end = pos + elem->datalen;
645 ret = cfg80211_calc_short_ssid(ies, &ssid_elem, &s_ssid_tmp);
649 /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
650 while (pos + sizeof(*ap_info) <= end) {
651 enum nl80211_band band;
655 ap_info = (void *)pos;
656 count = u8_get_bits(ap_info->tbtt_info_hdr,
657 IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
658 length = ap_info->tbtt_info_len;
660 pos += sizeof(*ap_info);
662 if (!ieee80211_operating_class_to_band(ap_info->op_class,
666 freq = ieee80211_channel_to_frequency(ap_info->channel, band);
668 if (end - pos < count * length)
671 if (u8_get_bits(ap_info->tbtt_info_hdr,
672 IEEE80211_AP_INFO_TBTT_HDR_TYPE) !=
673 IEEE80211_TBTT_INFO_TYPE_TBTT) {
674 pos += count * length;
679 * TBTT info must include bss param + BSSID +
680 * (short SSID or same_ssid bit to be set).
681 * ignore other options, and move to the
684 if (band != NL80211_BAND_6GHZ ||
685 (length != IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM &&
686 length < IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM)) {
687 pos += count * length;
691 for (i = 0; i < count; i++) {
692 struct cfg80211_colocated_ap *entry;
694 entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN,
700 entry->center_freq = freq;
702 if (!cfg80211_parse_ap_info(entry, pos, length,
703 ssid_elem, s_ssid_tmp)) {
705 list_add_tail(&entry->list, &ap_list);
715 cfg80211_free_coloc_ap_list(&ap_list);
719 list_splice_tail(&ap_list, list);
723 static void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
724 struct ieee80211_channel *chan,
728 u32 n_channels = request->n_channels;
729 struct cfg80211_scan_6ghz_params *params =
730 &request->scan_6ghz_params[request->n_6ghz_params];
732 for (i = 0; i < n_channels; i++) {
733 if (request->channels[i] == chan) {
735 params->channel_idx = i;
740 request->channels[n_channels] = chan;
742 request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
745 request->n_channels++;
748 static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
749 struct cfg80211_scan_request *request)
754 for (i = 0; i < request->n_ssids; i++) {
755 /* wildcard ssid in the scan request */
756 if (!request->ssids[i].ssid_len) {
757 if (ap->multi_bss && !ap->transmitted_bssid)
764 ap->ssid_len == request->ssids[i].ssid_len) {
765 if (!memcmp(request->ssids[i].ssid, ap->ssid,
768 } else if (ap->short_ssid_valid) {
769 s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
770 request->ssids[i].ssid_len);
772 if (ap->short_ssid == s_ssid)
780 static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
783 struct cfg80211_colocated_ap *ap;
784 int n_channels, count = 0, err;
785 struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
786 LIST_HEAD(coloc_ap_list);
787 bool need_scan_psc = true;
788 const struct ieee80211_sband_iftype_data *iftd;
790 rdev_req->scan_6ghz = true;
792 if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
795 iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
796 rdev_req->wdev->iftype);
797 if (!iftd || !iftd->he_cap.has_he)
800 n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
802 if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
803 struct cfg80211_internal_bss *intbss;
805 spin_lock_bh(&rdev->bss_lock);
806 list_for_each_entry(intbss, &rdev->bss_list, list) {
807 struct cfg80211_bss *res = &intbss->pub;
808 const struct cfg80211_bss_ies *ies;
810 ies = rcu_access_pointer(res->ies);
811 count += cfg80211_parse_colocated_ap(ies,
814 spin_unlock_bh(&rdev->bss_lock);
817 request = kzalloc(struct_size(request, channels, n_channels) +
818 sizeof(*request->scan_6ghz_params) * count +
819 sizeof(*request->ssids) * rdev_req->n_ssids,
822 cfg80211_free_coloc_ap_list(&coloc_ap_list);
826 *request = *rdev_req;
827 request->n_channels = 0;
828 request->scan_6ghz_params =
829 (void *)&request->channels[n_channels];
832 * PSC channels should not be scanned in case of direct scan with 1 SSID
833 * and at least one of the reported co-located APs with same SSID
834 * indicating that all APs in the same ESS are co-located
836 if (count && request->n_ssids == 1 && request->ssids[0].ssid_len) {
837 list_for_each_entry(ap, &coloc_ap_list, list) {
838 if (ap->colocated_ess &&
839 cfg80211_find_ssid_match(ap, request)) {
840 need_scan_psc = false;
847 * add to the scan request the channels that need to be scanned
848 * regardless of the collocated APs (PSC channels or all channels
849 * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
851 for (i = 0; i < rdev_req->n_channels; i++) {
852 if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
854 cfg80211_channel_is_psc(rdev_req->channels[i])) ||
855 !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
856 cfg80211_scan_req_add_chan(request,
857 rdev_req->channels[i],
862 if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
865 list_for_each_entry(ap, &coloc_ap_list, list) {
867 struct cfg80211_scan_6ghz_params *scan_6ghz_params =
868 &request->scan_6ghz_params[request->n_6ghz_params];
869 struct ieee80211_channel *chan =
870 ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
872 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
875 for (i = 0; i < rdev_req->n_channels; i++) {
876 if (rdev_req->channels[i] == chan)
883 if (request->n_ssids > 0 &&
884 !cfg80211_find_ssid_match(ap, request))
887 if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
890 cfg80211_scan_req_add_chan(request, chan, true);
891 memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
892 scan_6ghz_params->short_ssid = ap->short_ssid;
893 scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
894 scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
897 * If a PSC channel is added to the scan and 'need_scan_psc' is
898 * set to false, then all the APs that the scan logic is
899 * interested with on the channel are collocated and thus there
900 * is no need to perform the initial PSC channel listen.
902 if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
903 scan_6ghz_params->psc_no_listen = true;
905 request->n_6ghz_params++;
909 cfg80211_free_coloc_ap_list(&coloc_ap_list);
911 if (request->n_channels) {
912 struct cfg80211_scan_request *old = rdev->int_scan_req;
913 rdev->int_scan_req = request;
916 * Add the ssids from the parent scan request to the new scan
917 * request, so the driver would be able to use them in its
918 * probe requests to discover hidden APs on PSC channels.
920 request->ssids = (void *)&request->channels[request->n_channels];
921 request->n_ssids = rdev_req->n_ssids;
922 memcpy(request->ssids, rdev_req->ssids, sizeof(*request->ssids) *
926 * If this scan follows a previous scan, save the scan start
927 * info from the first part of the scan
930 rdev->int_scan_req->info = old->info;
932 err = rdev_scan(rdev, request);
934 rdev->int_scan_req = old;
947 int cfg80211_scan(struct cfg80211_registered_device *rdev)
949 struct cfg80211_scan_request *request;
950 struct cfg80211_scan_request *rdev_req = rdev->scan_req;
951 u32 n_channels = 0, idx, i;
953 if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
954 return rdev_scan(rdev, rdev_req);
956 for (i = 0; i < rdev_req->n_channels; i++) {
957 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
962 return cfg80211_scan_6ghz(rdev);
964 request = kzalloc(struct_size(request, channels, n_channels),
969 *request = *rdev_req;
970 request->n_channels = n_channels;
972 for (i = idx = 0; i < rdev_req->n_channels; i++) {
973 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
974 request->channels[idx++] = rdev_req->channels[i];
977 rdev_req->scan_6ghz = false;
978 rdev->int_scan_req = request;
979 return rdev_scan(rdev, request);
982 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
985 struct cfg80211_scan_request *request, *rdev_req;
986 struct wireless_dev *wdev;
988 #ifdef CONFIG_CFG80211_WEXT
989 union iwreq_data wrqu;
992 lockdep_assert_held(&rdev->wiphy.mtx);
994 if (rdev->scan_msg) {
995 nl80211_send_scan_msg(rdev, rdev->scan_msg);
996 rdev->scan_msg = NULL;
1000 rdev_req = rdev->scan_req;
1004 wdev = rdev_req->wdev;
1005 request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
1007 if (wdev_running(wdev) &&
1008 (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
1009 !rdev_req->scan_6ghz && !request->info.aborted &&
1010 !cfg80211_scan_6ghz(rdev))
1014 * This must be before sending the other events!
1015 * Otherwise, wpa_supplicant gets completely confused with
1019 cfg80211_sme_scan_done(wdev->netdev);
1021 if (!request->info.aborted &&
1022 request->flags & NL80211_SCAN_FLAG_FLUSH) {
1023 /* flush entries from previous scans */
1024 spin_lock_bh(&rdev->bss_lock);
1025 __cfg80211_bss_expire(rdev, request->scan_start);
1026 spin_unlock_bh(&rdev->bss_lock);
1029 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
1031 #ifdef CONFIG_CFG80211_WEXT
1032 if (wdev->netdev && !request->info.aborted) {
1033 memset(&wrqu, 0, sizeof(wrqu));
1035 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
1039 dev_put(wdev->netdev);
1041 kfree(rdev->int_scan_req);
1042 rdev->int_scan_req = NULL;
1044 kfree(rdev->scan_req);
1045 rdev->scan_req = NULL;
1048 rdev->scan_msg = msg;
1050 nl80211_send_scan_msg(rdev, msg);
1053 void __cfg80211_scan_done(struct wiphy *wiphy, struct wiphy_work *wk)
1055 ___cfg80211_scan_done(wiphy_to_rdev(wiphy), true);
1058 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1059 struct cfg80211_scan_info *info)
1061 struct cfg80211_scan_info old_info = request->info;
1063 trace_cfg80211_scan_done(request, info);
1064 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
1065 request != wiphy_to_rdev(request->wiphy)->int_scan_req);
1067 request->info = *info;
1070 * In case the scan is split, the scan_start_tsf and tsf_bssid should
1071 * be of the first part. In such a case old_info.scan_start_tsf should
1074 if (request->scan_6ghz && old_info.scan_start_tsf) {
1075 request->info.scan_start_tsf = old_info.scan_start_tsf;
1076 memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
1077 sizeof(request->info.tsf_bssid));
1080 request->notified = true;
1081 wiphy_work_queue(request->wiphy,
1082 &wiphy_to_rdev(request->wiphy)->scan_done_wk);
1084 EXPORT_SYMBOL(cfg80211_scan_done);
1086 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1087 struct cfg80211_sched_scan_request *req)
1089 lockdep_assert_held(&rdev->wiphy.mtx);
1091 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1094 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1095 struct cfg80211_sched_scan_request *req)
1097 lockdep_assert_held(&rdev->wiphy.mtx);
1099 list_del_rcu(&req->list);
1100 kfree_rcu(req, rcu_head);
1103 static struct cfg80211_sched_scan_request *
1104 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1106 struct cfg80211_sched_scan_request *pos;
1108 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1109 lockdep_is_held(&rdev->wiphy.mtx)) {
1110 if (pos->reqid == reqid)
1117 * Determines if a scheduled scan request can be handled. When a legacy
1118 * scheduled scan is running no other scheduled scan is allowed regardless
1119 * whether the request is for legacy or multi-support scan. When a multi-support
1120 * scheduled scan is running a request for legacy scan is not allowed. In this
1121 * case a request for multi-support scan can be handled if resources are
1122 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1124 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1127 struct cfg80211_sched_scan_request *pos;
1130 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1131 /* request id zero means legacy in progress */
1132 if (!i && !pos->reqid)
1133 return -EINPROGRESS;
1138 /* no legacy allowed when multi request(s) are active */
1140 return -EINPROGRESS;
1142 /* resource limit reached */
1143 if (i == rdev->wiphy.max_sched_scan_reqs)
1149 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1151 struct cfg80211_registered_device *rdev;
1152 struct cfg80211_sched_scan_request *req, *tmp;
1154 rdev = container_of(work, struct cfg80211_registered_device,
1157 wiphy_lock(&rdev->wiphy);
1158 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1159 if (req->report_results) {
1160 req->report_results = false;
1161 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1162 /* flush entries from previous scans */
1163 spin_lock_bh(&rdev->bss_lock);
1164 __cfg80211_bss_expire(rdev, req->scan_start);
1165 spin_unlock_bh(&rdev->bss_lock);
1166 req->scan_start = jiffies;
1168 nl80211_send_sched_scan(req,
1169 NL80211_CMD_SCHED_SCAN_RESULTS);
1172 wiphy_unlock(&rdev->wiphy);
1175 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1177 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1178 struct cfg80211_sched_scan_request *request;
1180 trace_cfg80211_sched_scan_results(wiphy, reqid);
1181 /* ignore if we're not scanning */
1184 request = cfg80211_find_sched_scan_req(rdev, reqid);
1186 request->report_results = true;
1187 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1191 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1193 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1195 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1197 lockdep_assert_held(&wiphy->mtx);
1199 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1201 __cfg80211_stop_sched_scan(rdev, reqid, true);
1203 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1205 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1208 cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1209 wiphy_unlock(wiphy);
1211 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1213 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1214 struct cfg80211_sched_scan_request *req,
1215 bool driver_initiated)
1217 lockdep_assert_held(&rdev->wiphy.mtx);
1219 if (!driver_initiated) {
1220 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1225 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1227 cfg80211_del_sched_scan_req(rdev, req);
1232 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1233 u64 reqid, bool driver_initiated)
1235 struct cfg80211_sched_scan_request *sched_scan_req;
1237 lockdep_assert_held(&rdev->wiphy.mtx);
1239 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1240 if (!sched_scan_req)
1243 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1247 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1248 unsigned long age_secs)
1250 struct cfg80211_internal_bss *bss;
1251 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
1253 spin_lock_bh(&rdev->bss_lock);
1254 list_for_each_entry(bss, &rdev->bss_list, list)
1255 bss->ts -= age_jiffies;
1256 spin_unlock_bh(&rdev->bss_lock);
1259 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1261 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1264 void cfg80211_bss_flush(struct wiphy *wiphy)
1266 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1268 spin_lock_bh(&rdev->bss_lock);
1269 __cfg80211_bss_expire(rdev, jiffies);
1270 spin_unlock_bh(&rdev->bss_lock);
1272 EXPORT_SYMBOL(cfg80211_bss_flush);
1274 const struct element *
1275 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1276 const u8 *match, unsigned int match_len,
1277 unsigned int match_offset)
1279 const struct element *elem;
1281 for_each_element_id(elem, eid, ies, len) {
1282 if (elem->datalen >= match_offset + match_len &&
1283 !memcmp(elem->data + match_offset, match, match_len))
1289 EXPORT_SYMBOL(cfg80211_find_elem_match);
1291 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1295 const struct element *elem;
1296 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1297 int match_len = (oui_type < 0) ? 3 : sizeof(match);
1299 if (WARN_ON(oui_type > 0xff))
1302 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1303 match, match_len, 0);
1305 if (!elem || elem->datalen < 4)
1310 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1313 * enum bss_compare_mode - BSS compare mode
1314 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1315 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1316 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1318 enum bss_compare_mode {
1324 static int cmp_bss(struct cfg80211_bss *a,
1325 struct cfg80211_bss *b,
1326 enum bss_compare_mode mode)
1328 const struct cfg80211_bss_ies *a_ies, *b_ies;
1329 const u8 *ie1 = NULL;
1330 const u8 *ie2 = NULL;
1333 if (a->channel != b->channel)
1334 return (b->channel->center_freq * 1000 + b->channel->freq_offset) -
1335 (a->channel->center_freq * 1000 + a->channel->freq_offset);
1337 a_ies = rcu_access_pointer(a->ies);
1340 b_ies = rcu_access_pointer(b->ies);
1344 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1345 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1346 a_ies->data, a_ies->len);
1347 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1348 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1349 b_ies->data, b_ies->len);
1353 if (ie1[1] == ie2[1])
1354 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1356 mesh_id_cmp = ie2[1] - ie1[1];
1358 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1359 a_ies->data, a_ies->len);
1360 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1361 b_ies->data, b_ies->len);
1365 if (ie1[1] != ie2[1])
1366 return ie2[1] - ie1[1];
1367 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1371 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1375 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1376 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1382 * Note that with "hide_ssid", the function returns a match if
1383 * the already-present BSS ("b") is a hidden SSID beacon for
1384 * the new BSS ("a").
1387 /* sort missing IE before (left of) present IE */
1394 case BSS_CMP_HIDE_ZLEN:
1396 * In ZLEN mode we assume the BSS entry we're
1397 * looking for has a zero-length SSID. So if
1398 * the one we're looking at right now has that,
1399 * return 0. Otherwise, return the difference
1400 * in length, but since we're looking for the
1401 * 0-length it's really equivalent to returning
1402 * the length of the one we're looking at.
1404 * No content comparison is needed as we assume
1405 * the content length is zero.
1408 case BSS_CMP_REGULAR:
1410 /* sort by length first, then by contents */
1411 if (ie1[1] != ie2[1])
1412 return ie2[1] - ie1[1];
1413 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1414 case BSS_CMP_HIDE_NUL:
1415 if (ie1[1] != ie2[1])
1416 return ie2[1] - ie1[1];
1417 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1418 for (i = 0; i < ie2[1]; i++)
1425 static bool cfg80211_bss_type_match(u16 capability,
1426 enum nl80211_band band,
1427 enum ieee80211_bss_type bss_type)
1432 if (bss_type == IEEE80211_BSS_TYPE_ANY)
1435 if (band == NL80211_BAND_60GHZ) {
1436 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1438 case IEEE80211_BSS_TYPE_ESS:
1439 val = WLAN_CAPABILITY_DMG_TYPE_AP;
1441 case IEEE80211_BSS_TYPE_PBSS:
1442 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1444 case IEEE80211_BSS_TYPE_IBSS:
1445 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1451 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1453 case IEEE80211_BSS_TYPE_ESS:
1454 val = WLAN_CAPABILITY_ESS;
1456 case IEEE80211_BSS_TYPE_IBSS:
1457 val = WLAN_CAPABILITY_IBSS;
1459 case IEEE80211_BSS_TYPE_MBSS:
1467 ret = ((capability & mask) == val);
1471 /* Returned bss is reference counted and must be cleaned up appropriately. */
1472 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
1473 struct ieee80211_channel *channel,
1475 const u8 *ssid, size_t ssid_len,
1476 enum ieee80211_bss_type bss_type,
1477 enum ieee80211_privacy privacy)
1479 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1480 struct cfg80211_internal_bss *bss, *res = NULL;
1481 unsigned long now = jiffies;
1484 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1487 spin_lock_bh(&rdev->bss_lock);
1489 list_for_each_entry(bss, &rdev->bss_list, list) {
1490 if (!cfg80211_bss_type_match(bss->pub.capability,
1491 bss->pub.channel->band, bss_type))
1494 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1495 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1496 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1498 if (channel && bss->pub.channel != channel)
1500 if (!is_valid_ether_addr(bss->pub.bssid))
1502 /* Don't get expired BSS structs */
1503 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1504 !atomic_read(&bss->hold))
1506 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
1508 bss_ref_get(rdev, res);
1513 spin_unlock_bh(&rdev->bss_lock);
1516 trace_cfg80211_return_bss(&res->pub);
1519 EXPORT_SYMBOL(cfg80211_get_bss);
1521 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1522 struct cfg80211_internal_bss *bss)
1524 struct rb_node **p = &rdev->bss_tree.rb_node;
1525 struct rb_node *parent = NULL;
1526 struct cfg80211_internal_bss *tbss;
1531 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1533 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1535 if (WARN_ON(!cmp)) {
1536 /* will sort of leak this BSS */
1543 p = &(*p)->rb_right;
1546 rb_link_node(&bss->rbn, parent, p);
1547 rb_insert_color(&bss->rbn, &rdev->bss_tree);
1550 static struct cfg80211_internal_bss *
1551 rb_find_bss(struct cfg80211_registered_device *rdev,
1552 struct cfg80211_internal_bss *res,
1553 enum bss_compare_mode mode)
1555 struct rb_node *n = rdev->bss_tree.rb_node;
1556 struct cfg80211_internal_bss *bss;
1560 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1561 r = cmp_bss(&res->pub, &bss->pub, mode);
1574 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1575 struct cfg80211_internal_bss *new)
1577 const struct cfg80211_bss_ies *ies;
1578 struct cfg80211_internal_bss *bss;
1584 ies = rcu_access_pointer(new->pub.beacon_ies);
1588 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1595 for (i = 0; i < ssidlen; i++)
1599 /* not a hidden SSID */
1603 /* This is the bad part ... */
1605 list_for_each_entry(bss, &rdev->bss_list, list) {
1607 * we're iterating all the entries anyway, so take the
1608 * opportunity to validate the list length accounting
1612 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1614 if (bss->pub.channel != new->pub.channel)
1616 if (bss->pub.scan_width != new->pub.scan_width)
1618 if (rcu_access_pointer(bss->pub.beacon_ies))
1620 ies = rcu_access_pointer(bss->pub.ies);
1623 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1626 if (ssidlen && ie[1] != ssidlen)
1628 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1630 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1631 list_del(&bss->hidden_list);
1633 list_add(&bss->hidden_list, &new->hidden_list);
1634 bss->pub.hidden_beacon_bss = &new->pub;
1635 new->refcount += bss->refcount;
1636 rcu_assign_pointer(bss->pub.beacon_ies,
1637 new->pub.beacon_ies);
1640 WARN_ONCE(n_entries != rdev->bss_entries,
1641 "rdev bss entries[%d]/list[len:%d] corruption\n",
1642 rdev->bss_entries, n_entries);
1647 struct cfg80211_non_tx_bss {
1648 struct cfg80211_bss *tx_bss;
1649 u8 max_bssid_indicator;
1653 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1654 const struct cfg80211_bss_ies *new_ies,
1655 const struct cfg80211_bss_ies *old_ies)
1657 struct cfg80211_internal_bss *bss;
1659 /* Assign beacon IEs to all sub entries */
1660 list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1661 const struct cfg80211_bss_ies *ies;
1663 ies = rcu_access_pointer(bss->pub.beacon_ies);
1664 WARN_ON(ies != old_ies);
1666 rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1671 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1672 struct cfg80211_internal_bss *known,
1673 struct cfg80211_internal_bss *new,
1676 lockdep_assert_held(&rdev->bss_lock);
1679 if (rcu_access_pointer(new->pub.proberesp_ies)) {
1680 const struct cfg80211_bss_ies *old;
1682 old = rcu_access_pointer(known->pub.proberesp_ies);
1684 rcu_assign_pointer(known->pub.proberesp_ies,
1685 new->pub.proberesp_ies);
1686 /* Override possible earlier Beacon frame IEs */
1687 rcu_assign_pointer(known->pub.ies,
1688 new->pub.proberesp_ies);
1690 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1691 } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1692 const struct cfg80211_bss_ies *old;
1694 if (known->pub.hidden_beacon_bss &&
1695 !list_empty(&known->hidden_list)) {
1696 const struct cfg80211_bss_ies *f;
1698 /* The known BSS struct is one of the probe
1699 * response members of a group, but we're
1700 * receiving a beacon (beacon_ies in the new
1701 * bss is used). This can only mean that the
1702 * AP changed its beacon from not having an
1703 * SSID to showing it, which is confusing so
1704 * drop this information.
1707 f = rcu_access_pointer(new->pub.beacon_ies);
1708 kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1712 old = rcu_access_pointer(known->pub.beacon_ies);
1714 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1716 /* Override IEs if they were from a beacon before */
1717 if (old == rcu_access_pointer(known->pub.ies))
1718 rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1720 cfg80211_update_hidden_bsses(known,
1721 rcu_access_pointer(new->pub.beacon_ies),
1725 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1728 known->pub.beacon_interval = new->pub.beacon_interval;
1730 /* don't update the signal if beacon was heard on
1734 known->pub.signal = new->pub.signal;
1735 known->pub.capability = new->pub.capability;
1736 known->ts = new->ts;
1737 known->ts_boottime = new->ts_boottime;
1738 known->parent_tsf = new->parent_tsf;
1739 known->pub.chains = new->pub.chains;
1740 memcpy(known->pub.chain_signal, new->pub.chain_signal,
1741 IEEE80211_MAX_CHAINS);
1742 ether_addr_copy(known->parent_bssid, new->parent_bssid);
1743 known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1744 known->pub.bssid_index = new->pub.bssid_index;
1749 /* Returned bss is reference counted and must be cleaned up appropriately. */
1750 static struct cfg80211_internal_bss *
1751 __cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1752 struct cfg80211_internal_bss *tmp,
1753 bool signal_valid, unsigned long ts)
1755 struct cfg80211_internal_bss *found = NULL;
1757 if (WARN_ON(!tmp->pub.channel))
1762 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
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 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1834 bss_ref_get(rdev, bss_from_pub(tmp->pub.transmitted_bss));
1837 list_add_tail(&new->list, &rdev->bss_list);
1838 rdev->bss_entries++;
1839 rb_insert_bss(rdev, new);
1843 rdev->bss_generation++;
1844 bss_ref_get(rdev, found);
1849 struct cfg80211_internal_bss *
1850 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1851 struct cfg80211_internal_bss *tmp,
1852 bool signal_valid, unsigned long ts)
1854 struct cfg80211_internal_bss *res;
1856 spin_lock_bh(&rdev->bss_lock);
1857 res = __cfg80211_bss_update(rdev, tmp, signal_valid, ts);
1858 spin_unlock_bh(&rdev->bss_lock);
1863 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
1864 enum nl80211_band band)
1866 const struct element *tmp;
1868 if (band == NL80211_BAND_6GHZ) {
1869 struct ieee80211_he_operation *he_oper;
1871 tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
1873 if (tmp && tmp->datalen >= sizeof(*he_oper) &&
1874 tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
1875 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
1877 he_oper = (void *)&tmp->data[1];
1879 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
1883 return he_6ghz_oper->primary;
1885 } else if (band == NL80211_BAND_S1GHZ) {
1886 tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
1887 if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
1888 struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
1890 return s1gop->oper_ch;
1893 tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
1894 if (tmp && tmp->datalen == 1)
1895 return tmp->data[0];
1897 tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
1899 tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
1900 struct ieee80211_ht_operation *htop = (void *)tmp->data;
1902 return htop->primary_chan;
1908 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
1911 * Update RX channel information based on the available frame payload
1912 * information. This is mainly for the 2.4 GHz band where frames can be received
1913 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1914 * element to indicate the current (transmitting) channel, but this might also
1915 * be needed on other bands if RX frequency does not match with the actual
1916 * operating channel of a BSS, or if the AP reports a different primary channel.
1918 static struct ieee80211_channel *
1919 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1920 struct ieee80211_channel *channel,
1921 enum nl80211_bss_scan_width scan_width)
1925 struct ieee80211_channel *alt_channel;
1927 channel_number = cfg80211_get_ies_channel_number(ie, ielen,
1930 if (channel_number < 0) {
1931 /* No channel information in frame payload */
1935 freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1938 * Frame info (beacon/prob res) is the same as received channel,
1939 * no need for further processing.
1941 if (freq == ieee80211_channel_to_khz(channel))
1944 alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1946 if (channel->band == NL80211_BAND_2GHZ ||
1947 channel->band == NL80211_BAND_6GHZ) {
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
1952 * channel 11) or beyond the 6 GHz channel range.
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,
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;
2028 if (channel->band == NL80211_BAND_60GHZ) {
2029 bss_type = capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2030 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2031 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2032 regulatory_hint_found_beacon(wiphy, channel,
2035 if (capability & WLAN_CAPABILITY_ESS)
2036 regulatory_hint_found_beacon(wiphy, channel,
2042 * If we do not know here whether the IEs are from a Beacon or Probe
2043 * Response frame, we need to pick one of the options and only use it
2044 * with the driver that does not provide the full Beacon/Probe Response
2045 * frame. Use Beacon frame pointer to avoid indicating that this should
2046 * override the IEs pointer should we have received an earlier
2047 * indication of Probe Response data.
2049 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2054 ies->from_beacon = false;
2055 memcpy(ies->data, ie, ielen);
2058 case CFG80211_BSS_FTYPE_BEACON:
2059 ies->from_beacon = true;
2061 case CFG80211_BSS_FTYPE_UNKNOWN:
2062 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2064 case CFG80211_BSS_FTYPE_PRESP:
2065 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2068 rcu_assign_pointer(tmp.pub.ies, ies);
2070 signal_valid = data->chan == channel;
2071 spin_lock_bh(&rdev->bss_lock);
2072 res = __cfg80211_bss_update(rdev, &tmp, signal_valid, ts);
2076 rdev_inform_bss(rdev, &res->pub, ies, data->drv_data);
2079 /* this is a nontransmitting bss, we need to add it to
2080 * transmitting bss' list if it is not there
2082 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
2084 if (__cfg80211_unlink_bss(rdev, res)) {
2085 rdev->bss_generation++;
2093 spin_unlock_bh(&rdev->bss_lock);
2095 trace_cfg80211_return_bss(&res->pub);
2096 /* __cfg80211_bss_update gives us a referenced result */
2100 spin_unlock_bh(&rdev->bss_lock);
2104 static const struct element
2105 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
2106 const struct element *mbssid_elem,
2107 const struct element *sub_elem)
2109 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
2110 const struct element *next_mbssid;
2111 const struct element *next_sub;
2113 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2115 ielen - (mbssid_end - ie));
2118 * If it is not the last subelement in current MBSSID IE or there isn't
2119 * a next MBSSID IE - profile is complete.
2121 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
2125 /* For any length error, just return NULL */
2127 if (next_mbssid->datalen < 4)
2130 next_sub = (void *)&next_mbssid->data[1];
2132 if (next_mbssid->data + next_mbssid->datalen <
2133 next_sub->data + next_sub->datalen)
2136 if (next_sub->id != 0 || next_sub->datalen < 2)
2140 * Check if the first element in the next sub element is a start
2143 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
2147 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2148 const struct element *mbssid_elem,
2149 const struct element *sub_elem,
2150 u8 *merged_ie, size_t max_copy_len)
2152 size_t copied_len = sub_elem->datalen;
2153 const struct element *next_mbssid;
2155 if (sub_elem->datalen > max_copy_len)
2158 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2160 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
2163 const struct element *next_sub = (void *)&next_mbssid->data[1];
2165 if (copied_len + next_sub->datalen > max_copy_len)
2167 memcpy(merged_ie + copied_len, next_sub->data,
2169 copied_len += next_sub->datalen;
2174 EXPORT_SYMBOL(cfg80211_merge_profile);
2176 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2177 struct cfg80211_inform_bss *data,
2178 enum cfg80211_bss_frame_type ftype,
2179 const u8 *bssid, u64 tsf,
2180 u16 beacon_interval, const u8 *ie,
2182 struct cfg80211_non_tx_bss *non_tx_data,
2185 const u8 *mbssid_index_ie;
2186 const struct element *elem, *sub;
2188 u8 new_bssid[ETH_ALEN];
2189 u8 *new_ie, *profile;
2190 u64 seen_indices = 0;
2192 struct cfg80211_bss *bss;
2196 if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2198 if (!wiphy->support_mbssid)
2200 if (wiphy->support_only_he_mbssid &&
2201 !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2204 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2208 profile = kmalloc(ielen, gfp);
2212 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
2213 if (elem->datalen < 4)
2215 if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2217 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2220 if (sub->id != 0 || sub->datalen < 4) {
2221 /* not a valid BSS profile */
2225 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2226 sub->data[1] != 2) {
2227 /* The first element within the Nontransmitted
2228 * BSSID Profile is not the Nontransmitted
2229 * BSSID Capability element.
2234 memset(profile, 0, ielen);
2235 profile_len = cfg80211_merge_profile(ie, ielen,
2241 /* found a Nontransmitted BSSID Profile */
2242 mbssid_index_ie = cfg80211_find_ie
2243 (WLAN_EID_MULTI_BSSID_IDX,
2244 profile, profile_len);
2245 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2246 mbssid_index_ie[2] == 0 ||
2247 mbssid_index_ie[2] > 46) {
2248 /* No valid Multiple BSSID-Index element */
2252 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2253 /* We don't support legacy split of a profile */
2254 net_dbg_ratelimited("Partial info for BSSID index %d\n",
2255 mbssid_index_ie[2]);
2257 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2259 non_tx_data->bssid_index = mbssid_index_ie[2];
2260 non_tx_data->max_bssid_indicator = elem->data[0];
2262 cfg80211_gen_new_bssid(bssid,
2263 non_tx_data->max_bssid_indicator,
2264 non_tx_data->bssid_index,
2266 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2267 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
2269 profile_len, new_ie,
2270 IEEE80211_MAX_DATA_LEN);
2274 capability = get_unaligned_le16(profile + 2);
2275 bss = cfg80211_inform_single_bss_data(wiphy, data,
2286 cfg80211_put_bss(wiphy, bss);
2295 ssize_t cfg80211_defragment_element(const struct element *elem, const u8 *ies,
2296 size_t ieslen, u8 *data, size_t data_len,
2299 const struct element *next;
2306 /* elem might be invalid after the memmove */
2307 next = (void *)(elem->data + elem->datalen);
2309 elem_datalen = elem->datalen;
2310 if (elem->id == WLAN_EID_EXTENSION) {
2311 copied = elem->datalen - 1;
2312 if (copied > data_len)
2315 memmove(data, elem->data + 1, copied);
2317 copied = elem->datalen;
2318 if (copied > data_len)
2321 memmove(data, elem->data, copied);
2324 /* Fragmented elements must have 255 bytes */
2325 if (elem_datalen < 255)
2329 elem->data < ies + ieslen &&
2330 elem->data + elem->datalen < ies + ieslen;
2332 /* elem might be invalid after the memmove */
2333 next = (void *)(elem->data + elem->datalen);
2335 if (elem->id != frag_id)
2338 elem_datalen = elem->datalen;
2340 if (copied + elem_datalen > data_len)
2343 memmove(data + copied, elem->data, elem_datalen);
2344 copied += elem_datalen;
2346 /* Only the last fragment may be short */
2347 if (elem_datalen != 255)
2353 EXPORT_SYMBOL(cfg80211_defragment_element);
2355 struct cfg80211_bss *
2356 cfg80211_inform_bss_data(struct wiphy *wiphy,
2357 struct cfg80211_inform_bss *data,
2358 enum cfg80211_bss_frame_type ftype,
2359 const u8 *bssid, u64 tsf, u16 capability,
2360 u16 beacon_interval, const u8 *ie, size_t ielen,
2363 struct cfg80211_bss *res;
2364 struct cfg80211_non_tx_bss non_tx_data;
2366 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
2367 capability, beacon_interval, ie,
2371 non_tx_data.tx_bss = res;
2372 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
2373 beacon_interval, ie, ielen, &non_tx_data,
2377 EXPORT_SYMBOL(cfg80211_inform_bss_data);
2379 /* cfg80211_inform_bss_width_frame helper */
2380 static struct cfg80211_bss *
2381 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
2382 struct cfg80211_inform_bss *data,
2383 struct ieee80211_mgmt *mgmt, size_t len,
2386 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2387 struct cfg80211_internal_bss tmp = {}, *res;
2388 struct cfg80211_bss_ies *ies;
2389 struct ieee80211_channel *channel;
2391 struct ieee80211_ext *ext = NULL;
2392 u8 *bssid, *variable;
2393 u16 capability, beacon_int;
2394 size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
2395 u.probe_resp.variable);
2398 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
2399 offsetof(struct ieee80211_mgmt, u.beacon.variable));
2401 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
2406 if (WARN_ON(!wiphy))
2409 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2410 (data->signal < 0 || data->signal > 100)))
2413 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2414 ext = (void *) mgmt;
2415 min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
2416 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2417 min_hdr_len = offsetof(struct ieee80211_ext,
2418 u.s1g_short_beacon.variable);
2421 if (WARN_ON(len < min_hdr_len))
2424 ielen = len - min_hdr_len;
2425 variable = mgmt->u.probe_resp.variable;
2427 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2428 variable = ext->u.s1g_short_beacon.variable;
2430 variable = ext->u.s1g_beacon.variable;
2433 channel = cfg80211_get_bss_channel(wiphy, variable,
2434 ielen, data->chan, data->scan_width);
2439 const struct ieee80211_s1g_bcn_compat_ie *compat;
2440 const struct element *elem;
2442 elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
2446 if (elem->datalen < sizeof(*compat))
2448 compat = (void *)elem->data;
2449 bssid = ext->u.s1g_beacon.sa;
2450 capability = le16_to_cpu(compat->compat_info);
2451 beacon_int = le16_to_cpu(compat->beacon_int);
2453 bssid = mgmt->bssid;
2454 beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2455 capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
2458 if (channel->band == NL80211_BAND_60GHZ) {
2459 bss_type = capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2460 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2461 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2462 regulatory_hint_found_beacon(wiphy, channel, gfp);
2464 if (capability & WLAN_CAPABILITY_ESS)
2465 regulatory_hint_found_beacon(wiphy, channel, gfp);
2468 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2472 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2473 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
2474 ieee80211_is_s1g_beacon(mgmt->frame_control);
2475 memcpy(ies->data, variable, ielen);
2477 if (ieee80211_is_probe_resp(mgmt->frame_control))
2478 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2480 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2481 rcu_assign_pointer(tmp.pub.ies, ies);
2483 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2484 tmp.pub.beacon_interval = beacon_int;
2485 tmp.pub.capability = capability;
2486 tmp.pub.channel = channel;
2487 tmp.pub.scan_width = data->scan_width;
2488 tmp.pub.signal = data->signal;
2489 tmp.ts_boottime = data->boottime_ns;
2490 tmp.parent_tsf = data->parent_tsf;
2491 tmp.pub.chains = data->chains;
2492 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
2493 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2495 signal_valid = data->chan == channel;
2496 spin_lock_bh(&rdev->bss_lock);
2497 res = __cfg80211_bss_update(rdev, &tmp, signal_valid, jiffies);
2501 rdev_inform_bss(rdev, &res->pub, ies, data->drv_data);
2503 spin_unlock_bh(&rdev->bss_lock);
2505 trace_cfg80211_return_bss(&res->pub);
2506 /* __cfg80211_bss_update gives us a referenced result */
2510 spin_unlock_bh(&rdev->bss_lock);
2514 struct cfg80211_bss *
2515 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2516 struct cfg80211_inform_bss *data,
2517 struct ieee80211_mgmt *mgmt, size_t len,
2520 struct cfg80211_bss *res;
2521 const u8 *ie = mgmt->u.probe_resp.variable;
2522 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2523 u.probe_resp.variable);
2524 enum cfg80211_bss_frame_type ftype;
2525 struct cfg80211_non_tx_bss non_tx_data = {};
2527 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
2532 /* don't do any further MBSSID handling for S1G */
2533 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
2536 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
2537 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
2538 non_tx_data.tx_bss = res;
2540 /* process each non-transmitting bss */
2541 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
2542 le64_to_cpu(mgmt->u.probe_resp.timestamp),
2543 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
2544 ie, ielen, &non_tx_data, gfp);
2548 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2550 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2552 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2557 spin_lock_bh(&rdev->bss_lock);
2558 bss_ref_get(rdev, bss_from_pub(pub));
2559 spin_unlock_bh(&rdev->bss_lock);
2561 EXPORT_SYMBOL(cfg80211_ref_bss);
2563 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2565 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2570 spin_lock_bh(&rdev->bss_lock);
2571 bss_ref_put(rdev, bss_from_pub(pub));
2572 spin_unlock_bh(&rdev->bss_lock);
2574 EXPORT_SYMBOL(cfg80211_put_bss);
2576 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2578 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2579 struct cfg80211_internal_bss *bss, *tmp1;
2580 struct cfg80211_bss *nontrans_bss, *tmp;
2585 bss = bss_from_pub(pub);
2587 spin_lock_bh(&rdev->bss_lock);
2588 if (list_empty(&bss->list))
2591 list_for_each_entry_safe(nontrans_bss, tmp,
2592 &pub->nontrans_list,
2594 tmp1 = bss_from_pub(nontrans_bss);
2595 if (__cfg80211_unlink_bss(rdev, tmp1))
2596 rdev->bss_generation++;
2599 if (__cfg80211_unlink_bss(rdev, bss))
2600 rdev->bss_generation++;
2602 spin_unlock_bh(&rdev->bss_lock);
2604 EXPORT_SYMBOL(cfg80211_unlink_bss);
2606 void cfg80211_bss_iter(struct wiphy *wiphy,
2607 struct cfg80211_chan_def *chandef,
2608 void (*iter)(struct wiphy *wiphy,
2609 struct cfg80211_bss *bss,
2613 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2614 struct cfg80211_internal_bss *bss;
2616 spin_lock_bh(&rdev->bss_lock);
2618 list_for_each_entry(bss, &rdev->bss_list, list) {
2619 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
2621 iter(wiphy, &bss->pub, iter_data);
2624 spin_unlock_bh(&rdev->bss_lock);
2626 EXPORT_SYMBOL(cfg80211_bss_iter);
2628 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2629 unsigned int link_id,
2630 struct ieee80211_channel *chan)
2632 struct wiphy *wiphy = wdev->wiphy;
2633 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2634 struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
2635 struct cfg80211_internal_bss *new = NULL;
2636 struct cfg80211_internal_bss *bss;
2637 struct cfg80211_bss *nontrans_bss;
2638 struct cfg80211_bss *tmp;
2640 spin_lock_bh(&rdev->bss_lock);
2643 * Some APs use CSA also for bandwidth changes, i.e., without actually
2644 * changing the control channel, so no need to update in such a case.
2646 if (cbss->pub.channel == chan)
2649 /* use transmitting bss */
2650 if (cbss->pub.transmitted_bss)
2651 cbss = bss_from_pub(cbss->pub.transmitted_bss);
2653 cbss->pub.channel = chan;
2655 list_for_each_entry(bss, &rdev->bss_list, list) {
2656 if (!cfg80211_bss_type_match(bss->pub.capability,
2657 bss->pub.channel->band,
2658 wdev->conn_bss_type))
2664 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2671 /* to save time, update IEs for transmitting bss only */
2672 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2673 new->pub.proberesp_ies = NULL;
2674 new->pub.beacon_ies = NULL;
2677 list_for_each_entry_safe(nontrans_bss, tmp,
2678 &new->pub.nontrans_list,
2680 bss = bss_from_pub(nontrans_bss);
2681 if (__cfg80211_unlink_bss(rdev, bss))
2682 rdev->bss_generation++;
2685 WARN_ON(atomic_read(&new->hold));
2686 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2687 rdev->bss_generation++;
2690 rb_erase(&cbss->rbn, &rdev->bss_tree);
2691 rb_insert_bss(rdev, cbss);
2692 rdev->bss_generation++;
2694 list_for_each_entry_safe(nontrans_bss, tmp,
2695 &cbss->pub.nontrans_list,
2697 bss = bss_from_pub(nontrans_bss);
2698 bss->pub.channel = chan;
2699 rb_erase(&bss->rbn, &rdev->bss_tree);
2700 rb_insert_bss(rdev, bss);
2701 rdev->bss_generation++;
2705 spin_unlock_bh(&rdev->bss_lock);
2708 #ifdef CONFIG_CFG80211_WEXT
2709 static struct cfg80211_registered_device *
2710 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2712 struct cfg80211_registered_device *rdev;
2713 struct net_device *dev;
2717 dev = dev_get_by_index(net, ifindex);
2719 return ERR_PTR(-ENODEV);
2720 if (dev->ieee80211_ptr)
2721 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2723 rdev = ERR_PTR(-ENODEV);
2728 int cfg80211_wext_siwscan(struct net_device *dev,
2729 struct iw_request_info *info,
2730 union iwreq_data *wrqu, char *extra)
2732 struct cfg80211_registered_device *rdev;
2733 struct wiphy *wiphy;
2734 struct iw_scan_req *wreq = NULL;
2735 struct cfg80211_scan_request *creq;
2736 int i, err, n_channels = 0;
2737 enum nl80211_band band;
2739 if (!netif_running(dev))
2742 if (wrqu->data.length == sizeof(struct iw_scan_req))
2743 wreq = (struct iw_scan_req *)extra;
2745 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2748 return PTR_ERR(rdev);
2750 if (rdev->scan_req || rdev->scan_msg)
2753 wiphy = &rdev->wiphy;
2755 /* Determine number of channels, needed to allocate creq */
2756 if (wreq && wreq->num_channels)
2757 n_channels = wreq->num_channels;
2759 n_channels = ieee80211_get_num_supported_channels(wiphy);
2761 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2762 n_channels * sizeof(void *),
2767 creq->wiphy = wiphy;
2768 creq->wdev = dev->ieee80211_ptr;
2769 /* SSIDs come after channels */
2770 creq->ssids = (void *)&creq->channels[n_channels];
2771 creq->n_channels = n_channels;
2773 creq->scan_start = jiffies;
2775 /* translate "Scan on frequencies" request */
2777 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2780 if (!wiphy->bands[band])
2783 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2784 /* ignore disabled channels */
2785 if (wiphy->bands[band]->channels[j].flags &
2786 IEEE80211_CHAN_DISABLED)
2789 /* If we have a wireless request structure and the
2790 * wireless request specifies frequencies, then search
2791 * for the matching hardware channel.
2793 if (wreq && wreq->num_channels) {
2795 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2796 for (k = 0; k < wreq->num_channels; k++) {
2797 struct iw_freq *freq =
2798 &wreq->channel_list[k];
2800 cfg80211_wext_freq(freq);
2802 if (wext_freq == wiphy_freq)
2803 goto wext_freq_found;
2805 goto wext_freq_not_found;
2809 creq->channels[i] = &wiphy->bands[band]->channels[j];
2811 wext_freq_not_found: ;
2814 /* No channels found? */
2820 /* Set real number of channels specified in creq->channels[] */
2821 creq->n_channels = i;
2823 /* translate "Scan for SSID" request */
2825 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2826 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2830 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2831 creq->ssids[0].ssid_len = wreq->essid_len;
2833 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2837 for (i = 0; i < NUM_NL80211_BANDS; i++)
2838 if (wiphy->bands[i])
2839 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2841 eth_broadcast_addr(creq->bssid);
2843 wiphy_lock(&rdev->wiphy);
2845 rdev->scan_req = creq;
2846 err = rdev_scan(rdev, creq);
2848 rdev->scan_req = NULL;
2849 /* creq will be freed below */
2851 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2852 /* creq now owned by driver */
2856 wiphy_unlock(&rdev->wiphy);
2861 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2863 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2864 const struct cfg80211_bss_ies *ies,
2865 char *current_ev, char *end_buf)
2867 const u8 *pos, *end, *next;
2868 struct iw_event iwe;
2874 * If needed, fragment the IEs buffer (at IE boundaries) into short
2875 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2878 end = pos + ies->len;
2880 while (end - pos > IW_GENERIC_IE_MAX) {
2881 next = pos + 2 + pos[1];
2882 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2883 next = next + 2 + next[1];
2885 memset(&iwe, 0, sizeof(iwe));
2886 iwe.cmd = IWEVGENIE;
2887 iwe.u.data.length = next - pos;
2888 current_ev = iwe_stream_add_point_check(info, current_ev,
2891 if (IS_ERR(current_ev))
2897 memset(&iwe, 0, sizeof(iwe));
2898 iwe.cmd = IWEVGENIE;
2899 iwe.u.data.length = end - pos;
2900 current_ev = iwe_stream_add_point_check(info, current_ev,
2903 if (IS_ERR(current_ev))
2911 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2912 struct cfg80211_internal_bss *bss, char *current_ev,
2915 const struct cfg80211_bss_ies *ies;
2916 struct iw_event iwe;
2921 bool ismesh = false;
2923 memset(&iwe, 0, sizeof(iwe));
2924 iwe.cmd = SIOCGIWAP;
2925 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2926 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2927 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2929 if (IS_ERR(current_ev))
2932 memset(&iwe, 0, sizeof(iwe));
2933 iwe.cmd = SIOCGIWFREQ;
2934 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2936 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2938 if (IS_ERR(current_ev))
2941 memset(&iwe, 0, sizeof(iwe));
2942 iwe.cmd = SIOCGIWFREQ;
2943 iwe.u.freq.m = bss->pub.channel->center_freq;
2945 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2947 if (IS_ERR(current_ev))
2950 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2951 memset(&iwe, 0, sizeof(iwe));
2953 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2954 IW_QUAL_NOISE_INVALID |
2955 IW_QUAL_QUAL_UPDATED;
2956 switch (wiphy->signal_type) {
2957 case CFG80211_SIGNAL_TYPE_MBM:
2958 sig = bss->pub.signal / 100;
2959 iwe.u.qual.level = sig;
2960 iwe.u.qual.updated |= IW_QUAL_DBM;
2961 if (sig < -110) /* rather bad */
2963 else if (sig > -40) /* perfect */
2965 /* will give a range of 0 .. 70 */
2966 iwe.u.qual.qual = sig + 110;
2968 case CFG80211_SIGNAL_TYPE_UNSPEC:
2969 iwe.u.qual.level = bss->pub.signal;
2970 /* will give range 0 .. 100 */
2971 iwe.u.qual.qual = bss->pub.signal;
2977 current_ev = iwe_stream_add_event_check(info, current_ev,
2980 if (IS_ERR(current_ev))
2984 memset(&iwe, 0, sizeof(iwe));
2985 iwe.cmd = SIOCGIWENCODE;
2986 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2987 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2989 iwe.u.data.flags = IW_ENCODE_DISABLED;
2990 iwe.u.data.length = 0;
2991 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2993 if (IS_ERR(current_ev))
2997 ies = rcu_dereference(bss->pub.ies);
3003 if (ie[1] > rem - 2)
3008 memset(&iwe, 0, sizeof(iwe));
3009 iwe.cmd = SIOCGIWESSID;
3010 iwe.u.data.length = ie[1];
3011 iwe.u.data.flags = 1;
3012 current_ev = iwe_stream_add_point_check(info,
3016 if (IS_ERR(current_ev))
3019 case WLAN_EID_MESH_ID:
3020 memset(&iwe, 0, sizeof(iwe));
3021 iwe.cmd = SIOCGIWESSID;
3022 iwe.u.data.length = ie[1];
3023 iwe.u.data.flags = 1;
3024 current_ev = iwe_stream_add_point_check(info,
3028 if (IS_ERR(current_ev))
3031 case WLAN_EID_MESH_CONFIG:
3033 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
3036 memset(&iwe, 0, sizeof(iwe));
3037 iwe.cmd = IWEVCUSTOM;
3038 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
3040 iwe.u.data.length = strlen(buf);
3041 current_ev = iwe_stream_add_point_check(info,
3045 if (IS_ERR(current_ev))
3047 sprintf(buf, "Path Selection Metric ID: 0x%02X",
3049 iwe.u.data.length = strlen(buf);
3050 current_ev = iwe_stream_add_point_check(info,
3054 if (IS_ERR(current_ev))
3056 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
3058 iwe.u.data.length = strlen(buf);
3059 current_ev = iwe_stream_add_point_check(info,
3063 if (IS_ERR(current_ev))
3065 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
3066 iwe.u.data.length = strlen(buf);
3067 current_ev = iwe_stream_add_point_check(info,
3071 if (IS_ERR(current_ev))
3073 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
3074 iwe.u.data.length = strlen(buf);
3075 current_ev = iwe_stream_add_point_check(info,
3079 if (IS_ERR(current_ev))
3081 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
3082 iwe.u.data.length = strlen(buf);
3083 current_ev = iwe_stream_add_point_check(info,
3087 if (IS_ERR(current_ev))
3089 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
3090 iwe.u.data.length = strlen(buf);
3091 current_ev = iwe_stream_add_point_check(info,
3095 if (IS_ERR(current_ev))
3098 case WLAN_EID_SUPP_RATES:
3099 case WLAN_EID_EXT_SUPP_RATES:
3100 /* display all supported rates in readable format */
3101 p = current_ev + iwe_stream_lcp_len(info);
3103 memset(&iwe, 0, sizeof(iwe));
3104 iwe.cmd = SIOCGIWRATE;
3105 /* Those two flags are ignored... */
3106 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3108 for (i = 0; i < ie[1]; i++) {
3109 iwe.u.bitrate.value =
3110 ((ie[i + 2] & 0x7f) * 500000);
3112 p = iwe_stream_add_value(info, current_ev, p,
3116 current_ev = ERR_PTR(-E2BIG);
3127 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3129 memset(&iwe, 0, sizeof(iwe));
3130 iwe.cmd = SIOCGIWMODE;
3132 iwe.u.mode = IW_MODE_MESH;
3133 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3134 iwe.u.mode = IW_MODE_MASTER;
3136 iwe.u.mode = IW_MODE_ADHOC;
3137 current_ev = iwe_stream_add_event_check(info, current_ev,
3140 if (IS_ERR(current_ev))
3144 memset(&iwe, 0, sizeof(iwe));
3145 iwe.cmd = IWEVCUSTOM;
3146 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
3147 iwe.u.data.length = strlen(buf);
3148 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3150 if (IS_ERR(current_ev))
3152 memset(&iwe, 0, sizeof(iwe));
3153 iwe.cmd = IWEVCUSTOM;
3154 sprintf(buf, " Last beacon: %ums ago",
3155 elapsed_jiffies_msecs(bss->ts));
3156 iwe.u.data.length = strlen(buf);
3157 current_ev = iwe_stream_add_point_check(info, current_ev,
3158 end_buf, &iwe, buf);
3159 if (IS_ERR(current_ev))
3162 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3170 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3171 struct iw_request_info *info,
3172 char *buf, size_t len)
3174 char *current_ev = buf;
3175 char *end_buf = buf + len;
3176 struct cfg80211_internal_bss *bss;
3179 spin_lock_bh(&rdev->bss_lock);
3180 cfg80211_bss_expire(rdev);
3182 list_for_each_entry(bss, &rdev->bss_list, list) {
3183 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3187 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
3188 current_ev, end_buf);
3189 if (IS_ERR(current_ev)) {
3190 err = PTR_ERR(current_ev);
3194 spin_unlock_bh(&rdev->bss_lock);
3198 return current_ev - buf;
3202 int cfg80211_wext_giwscan(struct net_device *dev,
3203 struct iw_request_info *info,
3204 union iwreq_data *wrqu, char *extra)
3206 struct iw_point *data = &wrqu->data;
3207 struct cfg80211_registered_device *rdev;
3210 if (!netif_running(dev))
3213 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3216 return PTR_ERR(rdev);
3218 if (rdev->scan_req || rdev->scan_msg)
3221 res = ieee80211_scan_results(rdev, info, extra, data->length);
3230 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);