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-2022 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_gen_new_ie(const u8 *ie, size_t ielen,
266 const u8 *subelement, size_t subie_len,
267 u8 *new_ie, gfp_t gfp)
270 const u8 *tmp_old, *tmp_new;
271 const struct element *non_inherit_elem;
274 /* copy subelement as we need to change its content to
275 * mark an ie after it is processed.
277 sub_copy = kmemdup(subelement, subie_len, gfp);
284 tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
286 memcpy(pos, tmp_new, tmp_new[1] + 2);
287 pos += (tmp_new[1] + 2);
290 /* get non inheritance list if exists */
292 cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
293 sub_copy, subie_len);
295 /* go through IEs in ie (skip SSID) and subelement,
296 * merge them into new_ie
298 tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
299 tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
301 while (tmp_old + 2 - ie <= ielen &&
302 tmp_old + tmp_old[1] + 2 - ie <= ielen) {
303 if (tmp_old[0] == 0) {
308 if (tmp_old[0] == WLAN_EID_EXTENSION)
309 tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
312 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
316 const struct element *old_elem = (void *)tmp_old;
318 /* ie in old ie but not in subelement */
319 if (cfg80211_is_element_inherited(old_elem,
321 memcpy(pos, tmp_old, tmp_old[1] + 2);
322 pos += tmp_old[1] + 2;
325 /* ie in transmitting ie also in subelement,
326 * copy from subelement and flag the ie in subelement
327 * as copied (by setting eid field to WLAN_EID_SSID,
328 * which is skipped anyway).
329 * For vendor ie, compare OUI + type + subType to
330 * determine if they are the same ie.
332 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
333 if (tmp_old[1] >= 5 && tmp[1] >= 5 &&
334 !memcmp(tmp_old + 2, tmp + 2, 5)) {
335 /* same vendor ie, copy from
338 memcpy(pos, tmp, tmp[1] + 2);
340 tmp[0] = WLAN_EID_SSID;
342 memcpy(pos, tmp_old, tmp_old[1] + 2);
343 pos += tmp_old[1] + 2;
346 /* copy ie from subelement into new ie */
347 memcpy(pos, tmp, tmp[1] + 2);
349 tmp[0] = WLAN_EID_SSID;
353 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
356 tmp_old += tmp_old[1] + 2;
359 /* go through subelement again to check if there is any ie not
360 * copied to new ie, skip ssid, capability, bssid-index ie
363 while (tmp_new + 2 - sub_copy <= subie_len &&
364 tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
365 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
366 tmp_new[0] == WLAN_EID_SSID)) {
367 memcpy(pos, tmp_new, tmp_new[1] + 2);
368 pos += tmp_new[1] + 2;
370 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
372 tmp_new += tmp_new[1] + 2;
379 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
380 const u8 *ssid, size_t ssid_len)
382 const struct cfg80211_bss_ies *ies;
383 const struct element *ssid_elem;
385 if (bssid && !ether_addr_equal(a->bssid, bssid))
391 ies = rcu_access_pointer(a->ies);
394 ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
397 if (ssid_elem->datalen != ssid_len)
399 return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
403 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
404 struct cfg80211_bss *nontrans_bss)
406 const struct element *ssid_elem;
407 struct cfg80211_bss *bss = NULL;
410 ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
416 /* check if nontrans_bss is in the list */
417 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
418 if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
419 ssid_elem->datalen)) {
428 * This is a bit weird - it's not on the list, but already on another
429 * one! The only way that could happen is if there's some BSSID/SSID
430 * shared by multiple APs in their multi-BSSID profiles, potentially
431 * with hidden SSID mixed in ... ignore it.
433 if (!list_empty(&nontrans_bss->nontrans_list))
436 /* add to the list */
437 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
441 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
442 unsigned long expire_time)
444 struct cfg80211_internal_bss *bss, *tmp;
445 bool expired = false;
447 lockdep_assert_held(&rdev->bss_lock);
449 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
450 if (atomic_read(&bss->hold))
452 if (!time_after(expire_time, bss->ts))
455 if (__cfg80211_unlink_bss(rdev, bss))
460 rdev->bss_generation++;
463 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
465 struct cfg80211_internal_bss *bss, *oldest = NULL;
468 lockdep_assert_held(&rdev->bss_lock);
470 list_for_each_entry(bss, &rdev->bss_list, list) {
471 if (atomic_read(&bss->hold))
474 if (!list_empty(&bss->hidden_list) &&
475 !bss->pub.hidden_beacon_bss)
478 if (oldest && time_before(oldest->ts, bss->ts))
483 if (WARN_ON(!oldest))
487 * The callers make sure to increase rdev->bss_generation if anything
488 * gets removed (and a new entry added), so there's no need to also do
492 ret = __cfg80211_unlink_bss(rdev, oldest);
497 static u8 cfg80211_parse_bss_param(u8 data,
498 struct cfg80211_colocated_ap *coloc_ap)
500 coloc_ap->oct_recommended =
501 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
502 coloc_ap->same_ssid =
503 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
504 coloc_ap->multi_bss =
505 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
506 coloc_ap->transmitted_bssid =
507 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
508 coloc_ap->unsolicited_probe =
509 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
510 coloc_ap->colocated_ess =
511 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
513 return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
516 static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
517 const struct element **elem, u32 *s_ssid)
520 *elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
521 if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
524 *s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
528 static void cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
530 struct cfg80211_colocated_ap *ap, *tmp_ap;
532 list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
538 static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
539 const u8 *pos, u8 length,
540 const struct element *ssid_elem,
543 /* skip the TBTT offset */
546 memcpy(entry->bssid, pos, ETH_ALEN);
549 if (length >= IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM) {
550 memcpy(&entry->short_ssid, pos,
551 sizeof(entry->short_ssid));
552 entry->short_ssid_valid = true;
556 /* skip non colocated APs */
557 if (!cfg80211_parse_bss_param(*pos, entry))
561 if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM) {
563 * no information about the short ssid. Consider the entry valid
564 * for now. It would later be dropped in case there are explicit
565 * SSIDs that need to be matched
567 if (!entry->same_ssid)
571 if (entry->same_ssid) {
572 entry->short_ssid = s_ssid_tmp;
573 entry->short_ssid_valid = true;
576 * This is safe because we validate datalen in
577 * cfg80211_parse_colocated_ap(), before calling this
580 memcpy(&entry->ssid, &ssid_elem->data,
582 entry->ssid_len = ssid_elem->datalen;
587 static int cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
588 struct list_head *list)
590 struct ieee80211_neighbor_ap_info *ap_info;
591 const struct element *elem, *ssid_elem;
594 int n_coloc = 0, ret;
597 elem = cfg80211_find_elem(WLAN_EID_REDUCED_NEIGHBOR_REPORT, ies->data,
603 end = pos + elem->datalen;
605 ret = cfg80211_calc_short_ssid(ies, &ssid_elem, &s_ssid_tmp);
609 /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
610 while (pos + sizeof(*ap_info) <= end) {
611 enum nl80211_band band;
615 ap_info = (void *)pos;
616 count = u8_get_bits(ap_info->tbtt_info_hdr,
617 IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
618 length = ap_info->tbtt_info_len;
620 pos += sizeof(*ap_info);
622 if (!ieee80211_operating_class_to_band(ap_info->op_class,
626 freq = ieee80211_channel_to_frequency(ap_info->channel, band);
628 if (end - pos < count * length)
632 * TBTT info must include bss param + BSSID +
633 * (short SSID or same_ssid bit to be set).
634 * ignore other options, and move to the
637 if (band != NL80211_BAND_6GHZ ||
638 (length != IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM &&
639 length < IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM)) {
640 pos += count * length;
644 for (i = 0; i < count; i++) {
645 struct cfg80211_colocated_ap *entry;
647 entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN,
653 entry->center_freq = freq;
655 if (!cfg80211_parse_ap_info(entry, pos, length,
656 ssid_elem, s_ssid_tmp)) {
658 list_add_tail(&entry->list, &ap_list);
668 cfg80211_free_coloc_ap_list(&ap_list);
672 list_splice_tail(&ap_list, list);
676 static void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
677 struct ieee80211_channel *chan,
681 u32 n_channels = request->n_channels;
682 struct cfg80211_scan_6ghz_params *params =
683 &request->scan_6ghz_params[request->n_6ghz_params];
685 for (i = 0; i < n_channels; i++) {
686 if (request->channels[i] == chan) {
688 params->channel_idx = i;
693 request->channels[n_channels] = chan;
695 request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
698 request->n_channels++;
701 static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
702 struct cfg80211_scan_request *request)
707 for (i = 0; i < request->n_ssids; i++) {
708 /* wildcard ssid in the scan request */
709 if (!request->ssids[i].ssid_len) {
710 if (ap->multi_bss && !ap->transmitted_bssid)
717 ap->ssid_len == request->ssids[i].ssid_len) {
718 if (!memcmp(request->ssids[i].ssid, ap->ssid,
721 } else if (ap->short_ssid_valid) {
722 s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
723 request->ssids[i].ssid_len);
725 if (ap->short_ssid == s_ssid)
733 static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
736 struct cfg80211_colocated_ap *ap;
737 int n_channels, count = 0, err;
738 struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
739 LIST_HEAD(coloc_ap_list);
740 bool need_scan_psc = true;
741 const struct ieee80211_sband_iftype_data *iftd;
743 rdev_req->scan_6ghz = true;
745 if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
748 iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
749 rdev_req->wdev->iftype);
750 if (!iftd || !iftd->he_cap.has_he)
753 n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
755 if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
756 struct cfg80211_internal_bss *intbss;
758 spin_lock_bh(&rdev->bss_lock);
759 list_for_each_entry(intbss, &rdev->bss_list, list) {
760 struct cfg80211_bss *res = &intbss->pub;
761 const struct cfg80211_bss_ies *ies;
763 ies = rcu_access_pointer(res->ies);
764 count += cfg80211_parse_colocated_ap(ies,
767 spin_unlock_bh(&rdev->bss_lock);
770 request = kzalloc(struct_size(request, channels, n_channels) +
771 sizeof(*request->scan_6ghz_params) * count +
772 sizeof(*request->ssids) * rdev_req->n_ssids,
775 cfg80211_free_coloc_ap_list(&coloc_ap_list);
779 *request = *rdev_req;
780 request->n_channels = 0;
781 request->scan_6ghz_params =
782 (void *)&request->channels[n_channels];
785 * PSC channels should not be scanned in case of direct scan with 1 SSID
786 * and at least one of the reported co-located APs with same SSID
787 * indicating that all APs in the same ESS are co-located
789 if (count && request->n_ssids == 1 && request->ssids[0].ssid_len) {
790 list_for_each_entry(ap, &coloc_ap_list, list) {
791 if (ap->colocated_ess &&
792 cfg80211_find_ssid_match(ap, request)) {
793 need_scan_psc = false;
800 * add to the scan request the channels that need to be scanned
801 * regardless of the collocated APs (PSC channels or all channels
802 * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
804 for (i = 0; i < rdev_req->n_channels; i++) {
805 if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
807 cfg80211_channel_is_psc(rdev_req->channels[i])) ||
808 !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
809 cfg80211_scan_req_add_chan(request,
810 rdev_req->channels[i],
815 if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
818 list_for_each_entry(ap, &coloc_ap_list, list) {
820 struct cfg80211_scan_6ghz_params *scan_6ghz_params =
821 &request->scan_6ghz_params[request->n_6ghz_params];
822 struct ieee80211_channel *chan =
823 ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
825 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
828 for (i = 0; i < rdev_req->n_channels; i++) {
829 if (rdev_req->channels[i] == chan)
836 if (request->n_ssids > 0 &&
837 !cfg80211_find_ssid_match(ap, request))
840 if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
843 cfg80211_scan_req_add_chan(request, chan, true);
844 memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
845 scan_6ghz_params->short_ssid = ap->short_ssid;
846 scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
847 scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
850 * If a PSC channel is added to the scan and 'need_scan_psc' is
851 * set to false, then all the APs that the scan logic is
852 * interested with on the channel are collocated and thus there
853 * is no need to perform the initial PSC channel listen.
855 if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
856 scan_6ghz_params->psc_no_listen = true;
858 request->n_6ghz_params++;
862 cfg80211_free_coloc_ap_list(&coloc_ap_list);
864 if (request->n_channels) {
865 struct cfg80211_scan_request *old = rdev->int_scan_req;
866 rdev->int_scan_req = request;
869 * Add the ssids from the parent scan request to the new scan
870 * request, so the driver would be able to use them in its
871 * probe requests to discover hidden APs on PSC channels.
873 request->ssids = (void *)&request->channels[request->n_channels];
874 request->n_ssids = rdev_req->n_ssids;
875 memcpy(request->ssids, rdev_req->ssids, sizeof(*request->ssids) *
879 * If this scan follows a previous scan, save the scan start
880 * info from the first part of the scan
883 rdev->int_scan_req->info = old->info;
885 err = rdev_scan(rdev, request);
887 rdev->int_scan_req = old;
900 int cfg80211_scan(struct cfg80211_registered_device *rdev)
902 struct cfg80211_scan_request *request;
903 struct cfg80211_scan_request *rdev_req = rdev->scan_req;
904 u32 n_channels = 0, idx, i;
906 if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
907 return rdev_scan(rdev, rdev_req);
909 for (i = 0; i < rdev_req->n_channels; i++) {
910 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
915 return cfg80211_scan_6ghz(rdev);
917 request = kzalloc(struct_size(request, channels, n_channels),
922 *request = *rdev_req;
923 request->n_channels = n_channels;
925 for (i = idx = 0; i < rdev_req->n_channels; i++) {
926 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
927 request->channels[idx++] = rdev_req->channels[i];
930 rdev_req->scan_6ghz = false;
931 rdev->int_scan_req = request;
932 return rdev_scan(rdev, request);
935 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
938 struct cfg80211_scan_request *request, *rdev_req;
939 struct wireless_dev *wdev;
941 #ifdef CONFIG_CFG80211_WEXT
942 union iwreq_data wrqu;
945 lockdep_assert_held(&rdev->wiphy.mtx);
947 if (rdev->scan_msg) {
948 nl80211_send_scan_msg(rdev, rdev->scan_msg);
949 rdev->scan_msg = NULL;
953 rdev_req = rdev->scan_req;
957 wdev = rdev_req->wdev;
958 request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
960 if (wdev_running(wdev) &&
961 (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
962 !rdev_req->scan_6ghz && !request->info.aborted &&
963 !cfg80211_scan_6ghz(rdev))
967 * This must be before sending the other events!
968 * Otherwise, wpa_supplicant gets completely confused with
972 cfg80211_sme_scan_done(wdev->netdev);
974 if (!request->info.aborted &&
975 request->flags & NL80211_SCAN_FLAG_FLUSH) {
976 /* flush entries from previous scans */
977 spin_lock_bh(&rdev->bss_lock);
978 __cfg80211_bss_expire(rdev, request->scan_start);
979 spin_unlock_bh(&rdev->bss_lock);
982 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
984 #ifdef CONFIG_CFG80211_WEXT
985 if (wdev->netdev && !request->info.aborted) {
986 memset(&wrqu, 0, sizeof(wrqu));
988 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
992 dev_put(wdev->netdev);
994 kfree(rdev->int_scan_req);
995 rdev->int_scan_req = NULL;
997 kfree(rdev->scan_req);
998 rdev->scan_req = NULL;
1001 rdev->scan_msg = msg;
1003 nl80211_send_scan_msg(rdev, msg);
1006 void __cfg80211_scan_done(struct work_struct *wk)
1008 struct cfg80211_registered_device *rdev;
1010 rdev = container_of(wk, struct cfg80211_registered_device,
1013 wiphy_lock(&rdev->wiphy);
1014 ___cfg80211_scan_done(rdev, true);
1015 wiphy_unlock(&rdev->wiphy);
1018 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1019 struct cfg80211_scan_info *info)
1021 struct cfg80211_scan_info old_info = request->info;
1023 trace_cfg80211_scan_done(request, info);
1024 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
1025 request != wiphy_to_rdev(request->wiphy)->int_scan_req);
1027 request->info = *info;
1030 * In case the scan is split, the scan_start_tsf and tsf_bssid should
1031 * be of the first part. In such a case old_info.scan_start_tsf should
1034 if (request->scan_6ghz && old_info.scan_start_tsf) {
1035 request->info.scan_start_tsf = old_info.scan_start_tsf;
1036 memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
1037 sizeof(request->info.tsf_bssid));
1040 request->notified = true;
1041 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
1043 EXPORT_SYMBOL(cfg80211_scan_done);
1045 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1046 struct cfg80211_sched_scan_request *req)
1048 lockdep_assert_held(&rdev->wiphy.mtx);
1050 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1053 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1054 struct cfg80211_sched_scan_request *req)
1056 lockdep_assert_held(&rdev->wiphy.mtx);
1058 list_del_rcu(&req->list);
1059 kfree_rcu(req, rcu_head);
1062 static struct cfg80211_sched_scan_request *
1063 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1065 struct cfg80211_sched_scan_request *pos;
1067 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1068 lockdep_is_held(&rdev->wiphy.mtx)) {
1069 if (pos->reqid == reqid)
1076 * Determines if a scheduled scan request can be handled. When a legacy
1077 * scheduled scan is running no other scheduled scan is allowed regardless
1078 * whether the request is for legacy or multi-support scan. When a multi-support
1079 * scheduled scan is running a request for legacy scan is not allowed. In this
1080 * case a request for multi-support scan can be handled if resources are
1081 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1083 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1086 struct cfg80211_sched_scan_request *pos;
1089 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1090 /* request id zero means legacy in progress */
1091 if (!i && !pos->reqid)
1092 return -EINPROGRESS;
1097 /* no legacy allowed when multi request(s) are active */
1099 return -EINPROGRESS;
1101 /* resource limit reached */
1102 if (i == rdev->wiphy.max_sched_scan_reqs)
1108 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1110 struct cfg80211_registered_device *rdev;
1111 struct cfg80211_sched_scan_request *req, *tmp;
1113 rdev = container_of(work, struct cfg80211_registered_device,
1116 wiphy_lock(&rdev->wiphy);
1117 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1118 if (req->report_results) {
1119 req->report_results = false;
1120 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1121 /* flush entries from previous scans */
1122 spin_lock_bh(&rdev->bss_lock);
1123 __cfg80211_bss_expire(rdev, req->scan_start);
1124 spin_unlock_bh(&rdev->bss_lock);
1125 req->scan_start = jiffies;
1127 nl80211_send_sched_scan(req,
1128 NL80211_CMD_SCHED_SCAN_RESULTS);
1131 wiphy_unlock(&rdev->wiphy);
1134 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1136 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1137 struct cfg80211_sched_scan_request *request;
1139 trace_cfg80211_sched_scan_results(wiphy, reqid);
1140 /* ignore if we're not scanning */
1143 request = cfg80211_find_sched_scan_req(rdev, reqid);
1145 request->report_results = true;
1146 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1150 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1152 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1154 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1156 lockdep_assert_held(&wiphy->mtx);
1158 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1160 __cfg80211_stop_sched_scan(rdev, reqid, true);
1162 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1164 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1167 cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1168 wiphy_unlock(wiphy);
1170 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1172 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1173 struct cfg80211_sched_scan_request *req,
1174 bool driver_initiated)
1176 lockdep_assert_held(&rdev->wiphy.mtx);
1178 if (!driver_initiated) {
1179 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1184 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1186 cfg80211_del_sched_scan_req(rdev, req);
1191 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1192 u64 reqid, bool driver_initiated)
1194 struct cfg80211_sched_scan_request *sched_scan_req;
1196 lockdep_assert_held(&rdev->wiphy.mtx);
1198 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1199 if (!sched_scan_req)
1202 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1206 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1207 unsigned long age_secs)
1209 struct cfg80211_internal_bss *bss;
1210 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
1212 spin_lock_bh(&rdev->bss_lock);
1213 list_for_each_entry(bss, &rdev->bss_list, list)
1214 bss->ts -= age_jiffies;
1215 spin_unlock_bh(&rdev->bss_lock);
1218 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1220 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1223 void cfg80211_bss_flush(struct wiphy *wiphy)
1225 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1227 spin_lock_bh(&rdev->bss_lock);
1228 __cfg80211_bss_expire(rdev, jiffies);
1229 spin_unlock_bh(&rdev->bss_lock);
1231 EXPORT_SYMBOL(cfg80211_bss_flush);
1233 const struct element *
1234 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1235 const u8 *match, unsigned int match_len,
1236 unsigned int match_offset)
1238 const struct element *elem;
1240 for_each_element_id(elem, eid, ies, len) {
1241 if (elem->datalen >= match_offset + match_len &&
1242 !memcmp(elem->data + match_offset, match, match_len))
1248 EXPORT_SYMBOL(cfg80211_find_elem_match);
1250 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1254 const struct element *elem;
1255 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1256 int match_len = (oui_type < 0) ? 3 : sizeof(match);
1258 if (WARN_ON(oui_type > 0xff))
1261 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1262 match, match_len, 0);
1264 if (!elem || elem->datalen < 4)
1269 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1272 * enum bss_compare_mode - BSS compare mode
1273 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1274 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1275 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1277 enum bss_compare_mode {
1283 static int cmp_bss(struct cfg80211_bss *a,
1284 struct cfg80211_bss *b,
1285 enum bss_compare_mode mode)
1287 const struct cfg80211_bss_ies *a_ies, *b_ies;
1288 const u8 *ie1 = NULL;
1289 const u8 *ie2 = NULL;
1292 if (a->channel != b->channel)
1293 return b->channel->center_freq - a->channel->center_freq;
1295 a_ies = rcu_access_pointer(a->ies);
1298 b_ies = rcu_access_pointer(b->ies);
1302 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1303 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1304 a_ies->data, a_ies->len);
1305 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1306 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1307 b_ies->data, b_ies->len);
1311 if (ie1[1] == ie2[1])
1312 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1314 mesh_id_cmp = ie2[1] - ie1[1];
1316 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1317 a_ies->data, a_ies->len);
1318 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1319 b_ies->data, b_ies->len);
1323 if (ie1[1] != ie2[1])
1324 return ie2[1] - ie1[1];
1325 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1329 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1333 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1334 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1340 * Note that with "hide_ssid", the function returns a match if
1341 * the already-present BSS ("b") is a hidden SSID beacon for
1342 * the new BSS ("a").
1345 /* sort missing IE before (left of) present IE */
1352 case BSS_CMP_HIDE_ZLEN:
1354 * In ZLEN mode we assume the BSS entry we're
1355 * looking for has a zero-length SSID. So if
1356 * the one we're looking at right now has that,
1357 * return 0. Otherwise, return the difference
1358 * in length, but since we're looking for the
1359 * 0-length it's really equivalent to returning
1360 * the length of the one we're looking at.
1362 * No content comparison is needed as we assume
1363 * the content length is zero.
1366 case BSS_CMP_REGULAR:
1368 /* sort by length first, then by contents */
1369 if (ie1[1] != ie2[1])
1370 return ie2[1] - ie1[1];
1371 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1372 case BSS_CMP_HIDE_NUL:
1373 if (ie1[1] != ie2[1])
1374 return ie2[1] - ie1[1];
1375 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1376 for (i = 0; i < ie2[1]; i++)
1383 static bool cfg80211_bss_type_match(u16 capability,
1384 enum nl80211_band band,
1385 enum ieee80211_bss_type bss_type)
1390 if (bss_type == IEEE80211_BSS_TYPE_ANY)
1393 if (band == NL80211_BAND_60GHZ) {
1394 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1396 case IEEE80211_BSS_TYPE_ESS:
1397 val = WLAN_CAPABILITY_DMG_TYPE_AP;
1399 case IEEE80211_BSS_TYPE_PBSS:
1400 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1402 case IEEE80211_BSS_TYPE_IBSS:
1403 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1409 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1411 case IEEE80211_BSS_TYPE_ESS:
1412 val = WLAN_CAPABILITY_ESS;
1414 case IEEE80211_BSS_TYPE_IBSS:
1415 val = WLAN_CAPABILITY_IBSS;
1417 case IEEE80211_BSS_TYPE_MBSS:
1425 ret = ((capability & mask) == val);
1429 /* Returned bss is reference counted and must be cleaned up appropriately. */
1430 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
1431 struct ieee80211_channel *channel,
1433 const u8 *ssid, size_t ssid_len,
1434 enum ieee80211_bss_type bss_type,
1435 enum ieee80211_privacy privacy)
1437 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1438 struct cfg80211_internal_bss *bss, *res = NULL;
1439 unsigned long now = jiffies;
1442 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1445 spin_lock_bh(&rdev->bss_lock);
1447 list_for_each_entry(bss, &rdev->bss_list, list) {
1448 if (!cfg80211_bss_type_match(bss->pub.capability,
1449 bss->pub.channel->band, bss_type))
1452 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1453 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1454 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1456 if (channel && bss->pub.channel != channel)
1458 if (!is_valid_ether_addr(bss->pub.bssid))
1460 /* Don't get expired BSS structs */
1461 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1462 !atomic_read(&bss->hold))
1464 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
1466 bss_ref_get(rdev, res);
1471 spin_unlock_bh(&rdev->bss_lock);
1474 trace_cfg80211_return_bss(&res->pub);
1477 EXPORT_SYMBOL(cfg80211_get_bss);
1479 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1480 struct cfg80211_internal_bss *bss)
1482 struct rb_node **p = &rdev->bss_tree.rb_node;
1483 struct rb_node *parent = NULL;
1484 struct cfg80211_internal_bss *tbss;
1489 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1491 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1493 if (WARN_ON(!cmp)) {
1494 /* will sort of leak this BSS */
1501 p = &(*p)->rb_right;
1504 rb_link_node(&bss->rbn, parent, p);
1505 rb_insert_color(&bss->rbn, &rdev->bss_tree);
1508 static struct cfg80211_internal_bss *
1509 rb_find_bss(struct cfg80211_registered_device *rdev,
1510 struct cfg80211_internal_bss *res,
1511 enum bss_compare_mode mode)
1513 struct rb_node *n = rdev->bss_tree.rb_node;
1514 struct cfg80211_internal_bss *bss;
1518 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1519 r = cmp_bss(&res->pub, &bss->pub, mode);
1532 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1533 struct cfg80211_internal_bss *new)
1535 const struct cfg80211_bss_ies *ies;
1536 struct cfg80211_internal_bss *bss;
1542 ies = rcu_access_pointer(new->pub.beacon_ies);
1546 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1553 for (i = 0; i < ssidlen; i++)
1557 /* not a hidden SSID */
1561 /* This is the bad part ... */
1563 list_for_each_entry(bss, &rdev->bss_list, list) {
1565 * we're iterating all the entries anyway, so take the
1566 * opportunity to validate the list length accounting
1570 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1572 if (bss->pub.channel != new->pub.channel)
1574 if (bss->pub.scan_width != new->pub.scan_width)
1576 if (rcu_access_pointer(bss->pub.beacon_ies))
1578 ies = rcu_access_pointer(bss->pub.ies);
1581 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1584 if (ssidlen && ie[1] != ssidlen)
1586 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1588 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1589 list_del(&bss->hidden_list);
1591 list_add(&bss->hidden_list, &new->hidden_list);
1592 bss->pub.hidden_beacon_bss = &new->pub;
1593 new->refcount += bss->refcount;
1594 rcu_assign_pointer(bss->pub.beacon_ies,
1595 new->pub.beacon_ies);
1598 WARN_ONCE(n_entries != rdev->bss_entries,
1599 "rdev bss entries[%d]/list[len:%d] corruption\n",
1600 rdev->bss_entries, n_entries);
1605 struct cfg80211_non_tx_bss {
1606 struct cfg80211_bss *tx_bss;
1607 u8 max_bssid_indicator;
1611 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1612 const struct cfg80211_bss_ies *new_ies,
1613 const struct cfg80211_bss_ies *old_ies)
1615 struct cfg80211_internal_bss *bss;
1617 /* Assign beacon IEs to all sub entries */
1618 list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1619 const struct cfg80211_bss_ies *ies;
1621 ies = rcu_access_pointer(bss->pub.beacon_ies);
1622 WARN_ON(ies != old_ies);
1624 rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1629 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1630 struct cfg80211_internal_bss *known,
1631 struct cfg80211_internal_bss *new,
1634 lockdep_assert_held(&rdev->bss_lock);
1637 if (rcu_access_pointer(new->pub.proberesp_ies)) {
1638 const struct cfg80211_bss_ies *old;
1640 old = rcu_access_pointer(known->pub.proberesp_ies);
1642 rcu_assign_pointer(known->pub.proberesp_ies,
1643 new->pub.proberesp_ies);
1644 /* Override possible earlier Beacon frame IEs */
1645 rcu_assign_pointer(known->pub.ies,
1646 new->pub.proberesp_ies);
1648 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1649 } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1650 const struct cfg80211_bss_ies *old;
1652 if (known->pub.hidden_beacon_bss &&
1653 !list_empty(&known->hidden_list)) {
1654 const struct cfg80211_bss_ies *f;
1656 /* The known BSS struct is one of the probe
1657 * response members of a group, but we're
1658 * receiving a beacon (beacon_ies in the new
1659 * bss is used). This can only mean that the
1660 * AP changed its beacon from not having an
1661 * SSID to showing it, which is confusing so
1662 * drop this information.
1665 f = rcu_access_pointer(new->pub.beacon_ies);
1666 kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1670 old = rcu_access_pointer(known->pub.beacon_ies);
1672 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1674 /* Override IEs if they were from a beacon before */
1675 if (old == rcu_access_pointer(known->pub.ies))
1676 rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1678 cfg80211_update_hidden_bsses(known,
1679 rcu_access_pointer(new->pub.beacon_ies),
1683 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1686 known->pub.beacon_interval = new->pub.beacon_interval;
1688 /* don't update the signal if beacon was heard on
1692 known->pub.signal = new->pub.signal;
1693 known->pub.capability = new->pub.capability;
1694 known->ts = new->ts;
1695 known->ts_boottime = new->ts_boottime;
1696 known->parent_tsf = new->parent_tsf;
1697 known->pub.chains = new->pub.chains;
1698 memcpy(known->pub.chain_signal, new->pub.chain_signal,
1699 IEEE80211_MAX_CHAINS);
1700 ether_addr_copy(known->parent_bssid, new->parent_bssid);
1701 known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1702 known->pub.bssid_index = new->pub.bssid_index;
1707 /* Returned bss is reference counted and must be cleaned up appropriately. */
1708 struct cfg80211_internal_bss *
1709 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1710 struct cfg80211_internal_bss *tmp,
1711 bool signal_valid, unsigned long ts)
1713 struct cfg80211_internal_bss *found = NULL;
1715 if (WARN_ON(!tmp->pub.channel))
1720 spin_lock_bh(&rdev->bss_lock);
1722 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1723 spin_unlock_bh(&rdev->bss_lock);
1727 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1730 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1733 struct cfg80211_internal_bss *new;
1734 struct cfg80211_internal_bss *hidden;
1735 struct cfg80211_bss_ies *ies;
1738 * create a copy -- the "res" variable that is passed in
1739 * is allocated on the stack since it's not needed in the
1740 * more common case of an update
1742 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1745 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1747 kfree_rcu(ies, rcu_head);
1748 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1750 kfree_rcu(ies, rcu_head);
1753 memcpy(new, tmp, sizeof(*new));
1755 INIT_LIST_HEAD(&new->hidden_list);
1756 INIT_LIST_HEAD(&new->pub.nontrans_list);
1757 /* we'll set this later if it was non-NULL */
1758 new->pub.transmitted_bss = NULL;
1760 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1761 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1763 hidden = rb_find_bss(rdev, tmp,
1766 new->pub.hidden_beacon_bss = &hidden->pub;
1767 list_add(&new->hidden_list,
1768 &hidden->hidden_list);
1770 rcu_assign_pointer(new->pub.beacon_ies,
1771 hidden->pub.beacon_ies);
1775 * Ok so we found a beacon, and don't have an entry. If
1776 * it's a beacon with hidden SSID, we might be in for an
1777 * expensive search for any probe responses that should
1778 * be grouped with this beacon for updates ...
1780 if (!cfg80211_combine_bsses(rdev, new)) {
1781 bss_ref_put(rdev, new);
1786 if (rdev->bss_entries >= bss_entries_limit &&
1787 !cfg80211_bss_expire_oldest(rdev)) {
1788 bss_ref_put(rdev, new);
1792 /* This must be before the call to bss_ref_get */
1793 if (tmp->pub.transmitted_bss) {
1794 struct cfg80211_internal_bss *pbss =
1795 container_of(tmp->pub.transmitted_bss,
1796 struct cfg80211_internal_bss,
1799 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1800 bss_ref_get(rdev, pbss);
1803 list_add_tail(&new->list, &rdev->bss_list);
1804 rdev->bss_entries++;
1805 rb_insert_bss(rdev, new);
1809 rdev->bss_generation++;
1810 bss_ref_get(rdev, found);
1811 spin_unlock_bh(&rdev->bss_lock);
1815 spin_unlock_bh(&rdev->bss_lock);
1819 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
1820 enum nl80211_band band,
1821 enum cfg80211_bss_frame_type ftype)
1823 const struct element *tmp;
1825 if (band == NL80211_BAND_6GHZ) {
1826 struct ieee80211_he_operation *he_oper;
1828 tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
1830 if (tmp && tmp->datalen >= sizeof(*he_oper) &&
1831 tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
1832 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
1834 he_oper = (void *)&tmp->data[1];
1836 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
1840 if (ftype != CFG80211_BSS_FTYPE_BEACON ||
1841 he_6ghz_oper->control & IEEE80211_HE_6GHZ_OPER_CTRL_DUP_BEACON)
1842 return he_6ghz_oper->primary;
1844 } else if (band == NL80211_BAND_S1GHZ) {
1845 tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
1846 if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
1847 struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
1849 return s1gop->oper_ch;
1852 tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
1853 if (tmp && tmp->datalen == 1)
1854 return tmp->data[0];
1856 tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
1858 tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
1859 struct ieee80211_ht_operation *htop = (void *)tmp->data;
1861 return htop->primary_chan;
1867 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
1870 * Update RX channel information based on the available frame payload
1871 * information. This is mainly for the 2.4 GHz band where frames can be received
1872 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1873 * element to indicate the current (transmitting) channel, but this might also
1874 * be needed on other bands if RX frequency does not match with the actual
1875 * operating channel of a BSS, or if the AP reports a different primary channel.
1877 static struct ieee80211_channel *
1878 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1879 struct ieee80211_channel *channel,
1880 enum nl80211_bss_scan_width scan_width,
1881 enum cfg80211_bss_frame_type ftype)
1885 struct ieee80211_channel *alt_channel;
1887 channel_number = cfg80211_get_ies_channel_number(ie, ielen,
1888 channel->band, ftype);
1890 if (channel_number < 0) {
1891 /* No channel information in frame payload */
1895 freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1898 * In 6GHz, duplicated beacon indication is relevant for
1901 if (channel->band == NL80211_BAND_6GHZ &&
1902 (freq == channel->center_freq ||
1903 abs(freq - channel->center_freq) > 80))
1906 alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1908 if (channel->band == NL80211_BAND_2GHZ) {
1910 * Better not allow unexpected channels when that could
1911 * be going beyond the 1-11 range (e.g., discovering
1912 * BSS on channel 12 when radio is configured for
1918 /* No match for the payload channel number - ignore it */
1922 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1923 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1925 * Ignore channel number in 5 and 10 MHz channels where there
1926 * may not be an n:1 or 1:n mapping between frequencies and
1933 * Use the channel determined through the payload channel number
1934 * instead of the RX channel reported by the driver.
1936 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1941 /* Returned bss is reference counted and must be cleaned up appropriately. */
1942 static struct cfg80211_bss *
1943 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1944 struct cfg80211_inform_bss *data,
1945 enum cfg80211_bss_frame_type ftype,
1946 const u8 *bssid, u64 tsf, u16 capability,
1947 u16 beacon_interval, const u8 *ie, size_t ielen,
1948 struct cfg80211_non_tx_bss *non_tx_data,
1951 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1952 struct cfg80211_bss_ies *ies;
1953 struct ieee80211_channel *channel;
1954 struct cfg80211_internal_bss tmp = {}, *res;
1959 if (WARN_ON(!wiphy))
1962 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1963 (data->signal < 0 || data->signal > 100)))
1966 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1967 data->scan_width, ftype);
1971 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1972 tmp.pub.channel = channel;
1973 tmp.pub.scan_width = data->scan_width;
1974 tmp.pub.signal = data->signal;
1975 tmp.pub.beacon_interval = beacon_interval;
1976 tmp.pub.capability = capability;
1977 tmp.ts_boottime = data->boottime_ns;
1978 tmp.parent_tsf = data->parent_tsf;
1979 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1982 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1983 ts = bss_from_pub(non_tx_data->tx_bss)->ts;
1984 tmp.pub.bssid_index = non_tx_data->bssid_index;
1985 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1991 * If we do not know here whether the IEs are from a Beacon or Probe
1992 * Response frame, we need to pick one of the options and only use it
1993 * with the driver that does not provide the full Beacon/Probe Response
1994 * frame. Use Beacon frame pointer to avoid indicating that this should
1995 * override the IEs pointer should we have received an earlier
1996 * indication of Probe Response data.
1998 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2003 ies->from_beacon = false;
2004 memcpy(ies->data, ie, ielen);
2007 case CFG80211_BSS_FTYPE_BEACON:
2008 ies->from_beacon = true;
2010 case CFG80211_BSS_FTYPE_UNKNOWN:
2011 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2013 case CFG80211_BSS_FTYPE_PRESP:
2014 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2017 rcu_assign_pointer(tmp.pub.ies, ies);
2019 signal_valid = data->chan == channel;
2020 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
2024 if (channel->band == NL80211_BAND_60GHZ) {
2025 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2026 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2027 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2028 regulatory_hint_found_beacon(wiphy, channel, gfp);
2030 if (res->pub.capability & WLAN_CAPABILITY_ESS)
2031 regulatory_hint_found_beacon(wiphy, channel, gfp);
2035 /* this is a nontransmitting bss, we need to add it to
2036 * transmitting bss' list if it is not there
2038 spin_lock_bh(&rdev->bss_lock);
2039 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
2041 if (__cfg80211_unlink_bss(rdev, res)) {
2042 rdev->bss_generation++;
2046 spin_unlock_bh(&rdev->bss_lock);
2052 trace_cfg80211_return_bss(&res->pub);
2053 /* cfg80211_bss_update gives us a referenced result */
2057 static const struct element
2058 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
2059 const struct element *mbssid_elem,
2060 const struct element *sub_elem)
2062 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
2063 const struct element *next_mbssid;
2064 const struct element *next_sub;
2066 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2068 ielen - (mbssid_end - ie));
2071 * If it is not the last subelement in current MBSSID IE or there isn't
2072 * a next MBSSID IE - profile is complete.
2074 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
2078 /* For any length error, just return NULL */
2080 if (next_mbssid->datalen < 4)
2083 next_sub = (void *)&next_mbssid->data[1];
2085 if (next_mbssid->data + next_mbssid->datalen <
2086 next_sub->data + next_sub->datalen)
2089 if (next_sub->id != 0 || next_sub->datalen < 2)
2093 * Check if the first element in the next sub element is a start
2096 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
2100 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2101 const struct element *mbssid_elem,
2102 const struct element *sub_elem,
2103 u8 *merged_ie, size_t max_copy_len)
2105 size_t copied_len = sub_elem->datalen;
2106 const struct element *next_mbssid;
2108 if (sub_elem->datalen > max_copy_len)
2111 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2113 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
2116 const struct element *next_sub = (void *)&next_mbssid->data[1];
2118 if (copied_len + next_sub->datalen > max_copy_len)
2120 memcpy(merged_ie + copied_len, next_sub->data,
2122 copied_len += next_sub->datalen;
2127 EXPORT_SYMBOL(cfg80211_merge_profile);
2129 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2130 struct cfg80211_inform_bss *data,
2131 enum cfg80211_bss_frame_type ftype,
2132 const u8 *bssid, u64 tsf,
2133 u16 beacon_interval, const u8 *ie,
2135 struct cfg80211_non_tx_bss *non_tx_data,
2138 const u8 *mbssid_index_ie;
2139 const struct element *elem, *sub;
2141 u8 new_bssid[ETH_ALEN];
2142 u8 *new_ie, *profile;
2143 u64 seen_indices = 0;
2145 struct cfg80211_bss *bss;
2149 if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2151 if (!wiphy->support_mbssid)
2153 if (wiphy->support_only_he_mbssid &&
2154 !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2157 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2161 profile = kmalloc(ielen, gfp);
2165 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
2166 if (elem->datalen < 4)
2168 if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2170 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2173 if (sub->id != 0 || sub->datalen < 4) {
2174 /* not a valid BSS profile */
2178 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2179 sub->data[1] != 2) {
2180 /* The first element within the Nontransmitted
2181 * BSSID Profile is not the Nontransmitted
2182 * BSSID Capability element.
2187 memset(profile, 0, ielen);
2188 profile_len = cfg80211_merge_profile(ie, ielen,
2194 /* found a Nontransmitted BSSID Profile */
2195 mbssid_index_ie = cfg80211_find_ie
2196 (WLAN_EID_MULTI_BSSID_IDX,
2197 profile, profile_len);
2198 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2199 mbssid_index_ie[2] == 0 ||
2200 mbssid_index_ie[2] > 46) {
2201 /* No valid Multiple BSSID-Index element */
2205 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2206 /* We don't support legacy split of a profile */
2207 net_dbg_ratelimited("Partial info for BSSID index %d\n",
2208 mbssid_index_ie[2]);
2210 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2212 non_tx_data->bssid_index = mbssid_index_ie[2];
2213 non_tx_data->max_bssid_indicator = elem->data[0];
2215 cfg80211_gen_new_bssid(bssid,
2216 non_tx_data->max_bssid_indicator,
2217 non_tx_data->bssid_index,
2219 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2220 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
2222 profile_len, new_ie,
2227 capability = get_unaligned_le16(profile + 2);
2228 bss = cfg80211_inform_single_bss_data(wiphy, data,
2239 cfg80211_put_bss(wiphy, bss);
2248 struct cfg80211_bss *
2249 cfg80211_inform_bss_data(struct wiphy *wiphy,
2250 struct cfg80211_inform_bss *data,
2251 enum cfg80211_bss_frame_type ftype,
2252 const u8 *bssid, u64 tsf, u16 capability,
2253 u16 beacon_interval, const u8 *ie, size_t ielen,
2256 struct cfg80211_bss *res;
2257 struct cfg80211_non_tx_bss non_tx_data;
2259 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
2260 capability, beacon_interval, ie,
2264 non_tx_data.tx_bss = res;
2265 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
2266 beacon_interval, ie, ielen, &non_tx_data,
2270 EXPORT_SYMBOL(cfg80211_inform_bss_data);
2273 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
2274 struct cfg80211_inform_bss *data,
2275 struct ieee80211_mgmt *mgmt, size_t len,
2276 struct cfg80211_non_tx_bss *non_tx_data,
2279 enum cfg80211_bss_frame_type ftype;
2280 const u8 *ie = mgmt->u.probe_resp.variable;
2281 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2282 u.probe_resp.variable);
2284 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
2285 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
2287 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
2288 le64_to_cpu(mgmt->u.probe_resp.timestamp),
2289 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
2290 ie, ielen, non_tx_data, gfp);
2294 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
2295 struct cfg80211_bss *nontrans_bss,
2296 struct ieee80211_mgmt *mgmt, size_t len)
2298 u8 *ie, *new_ie, *pos;
2299 const struct element *nontrans_ssid;
2300 const u8 *trans_ssid, *mbssid;
2301 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2302 u.probe_resp.variable);
2304 struct cfg80211_bss_ies *new_ies;
2305 const struct cfg80211_bss_ies *old;
2308 lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
2310 ie = mgmt->u.probe_resp.variable;
2313 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
2316 new_ie_len -= trans_ssid[1];
2317 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
2319 * It's not valid to have the MBSSID element before SSID
2320 * ignore if that happens - the code below assumes it is
2321 * after (while copying things inbetween).
2323 if (!mbssid || mbssid < trans_ssid)
2325 new_ie_len -= mbssid[1];
2327 nontrans_ssid = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
2331 new_ie_len += nontrans_ssid->datalen;
2333 /* generate new ie for nontrans BSS
2334 * 1. replace SSID with nontrans BSS' SSID
2337 new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
2341 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
2347 /* copy the nontransmitted SSID */
2348 cpy_len = nontrans_ssid->datalen + 2;
2349 memcpy(pos, nontrans_ssid, cpy_len);
2351 /* copy the IEs between SSID and MBSSID */
2352 cpy_len = trans_ssid[1] + 2;
2353 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
2354 pos += (mbssid - (trans_ssid + cpy_len));
2355 /* copy the IEs after MBSSID */
2356 cpy_len = mbssid[1] + 2;
2357 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
2360 new_ies->len = new_ie_len;
2361 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2362 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
2363 memcpy(new_ies->data, new_ie, new_ie_len);
2364 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
2365 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
2366 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
2367 rcu_assign_pointer(nontrans_bss->ies, new_ies);
2369 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
2371 old = rcu_access_pointer(nontrans_bss->beacon_ies);
2372 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
2373 cfg80211_update_hidden_bsses(bss_from_pub(nontrans_bss),
2375 rcu_assign_pointer(nontrans_bss->ies, new_ies);
2377 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
2384 /* cfg80211_inform_bss_width_frame helper */
2385 static struct cfg80211_bss *
2386 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
2387 struct cfg80211_inform_bss *data,
2388 struct ieee80211_mgmt *mgmt, size_t len,
2391 struct cfg80211_internal_bss tmp = {}, *res;
2392 struct cfg80211_bss_ies *ies;
2393 struct ieee80211_channel *channel;
2395 struct ieee80211_ext *ext = NULL;
2396 u8 *bssid, *variable;
2397 u16 capability, beacon_int;
2398 size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
2399 u.probe_resp.variable);
2401 enum cfg80211_bss_frame_type ftype;
2403 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
2404 offsetof(struct ieee80211_mgmt, u.beacon.variable));
2406 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
2411 if (WARN_ON(!wiphy))
2414 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2415 (data->signal < 0 || data->signal > 100)))
2418 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2419 ext = (void *) mgmt;
2420 min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
2421 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2422 min_hdr_len = offsetof(struct ieee80211_ext,
2423 u.s1g_short_beacon.variable);
2426 if (WARN_ON(len < min_hdr_len))
2429 ielen = len - min_hdr_len;
2430 variable = mgmt->u.probe_resp.variable;
2432 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2433 variable = ext->u.s1g_short_beacon.variable;
2435 variable = ext->u.s1g_beacon.variable;
2438 if (ieee80211_is_beacon(mgmt->frame_control))
2439 ftype = CFG80211_BSS_FTYPE_BEACON;
2440 else if (ieee80211_is_probe_resp(mgmt->frame_control))
2441 ftype = CFG80211_BSS_FTYPE_PRESP;
2443 ftype = CFG80211_BSS_FTYPE_UNKNOWN;
2445 channel = cfg80211_get_bss_channel(wiphy, variable,
2446 ielen, data->chan, data->scan_width,
2452 const struct ieee80211_s1g_bcn_compat_ie *compat;
2453 const struct element *elem;
2455 elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
2459 if (elem->datalen < sizeof(*compat))
2461 compat = (void *)elem->data;
2462 bssid = ext->u.s1g_beacon.sa;
2463 capability = le16_to_cpu(compat->compat_info);
2464 beacon_int = le16_to_cpu(compat->beacon_int);
2466 bssid = mgmt->bssid;
2467 beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2468 capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
2471 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2475 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2476 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
2477 ieee80211_is_s1g_beacon(mgmt->frame_control);
2478 memcpy(ies->data, variable, ielen);
2480 if (ieee80211_is_probe_resp(mgmt->frame_control))
2481 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2483 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2484 rcu_assign_pointer(tmp.pub.ies, ies);
2486 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2487 tmp.pub.beacon_interval = beacon_int;
2488 tmp.pub.capability = capability;
2489 tmp.pub.channel = channel;
2490 tmp.pub.scan_width = data->scan_width;
2491 tmp.pub.signal = data->signal;
2492 tmp.ts_boottime = data->boottime_ns;
2493 tmp.parent_tsf = data->parent_tsf;
2494 tmp.pub.chains = data->chains;
2495 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
2496 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2498 signal_valid = data->chan == channel;
2499 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
2504 if (channel->band == NL80211_BAND_60GHZ) {
2505 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2506 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2507 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2508 regulatory_hint_found_beacon(wiphy, channel, gfp);
2510 if (res->pub.capability & WLAN_CAPABILITY_ESS)
2511 regulatory_hint_found_beacon(wiphy, channel, gfp);
2514 trace_cfg80211_return_bss(&res->pub);
2515 /* cfg80211_bss_update gives us a referenced result */
2519 struct cfg80211_bss *
2520 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2521 struct cfg80211_inform_bss *data,
2522 struct ieee80211_mgmt *mgmt, size_t len,
2525 struct cfg80211_bss *res, *tmp_bss;
2526 const u8 *ie = mgmt->u.probe_resp.variable;
2527 const struct cfg80211_bss_ies *ies1, *ies2;
2528 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2529 u.probe_resp.variable);
2530 struct cfg80211_non_tx_bss non_tx_data = {};
2532 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
2535 /* don't do any further MBSSID handling for S1G */
2536 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
2539 if (!res || !wiphy->support_mbssid ||
2540 !cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2542 if (wiphy->support_only_he_mbssid &&
2543 !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2546 non_tx_data.tx_bss = res;
2547 /* process each non-transmitting bss */
2548 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
2551 spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2553 /* check if the res has other nontransmitting bss which is not
2556 ies1 = rcu_access_pointer(res->ies);
2558 /* go through nontrans_list, if the timestamp of the BSS is
2559 * earlier than the timestamp of the transmitting BSS then
2562 list_for_each_entry(tmp_bss, &res->nontrans_list,
2564 ies2 = rcu_access_pointer(tmp_bss->ies);
2565 if (ies2->tsf < ies1->tsf)
2566 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
2569 spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2573 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2575 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2577 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2578 struct cfg80211_internal_bss *bss;
2583 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2585 spin_lock_bh(&rdev->bss_lock);
2586 bss_ref_get(rdev, bss);
2587 spin_unlock_bh(&rdev->bss_lock);
2589 EXPORT_SYMBOL(cfg80211_ref_bss);
2591 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2593 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2594 struct cfg80211_internal_bss *bss;
2599 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2601 spin_lock_bh(&rdev->bss_lock);
2602 bss_ref_put(rdev, bss);
2603 spin_unlock_bh(&rdev->bss_lock);
2605 EXPORT_SYMBOL(cfg80211_put_bss);
2607 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2609 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2610 struct cfg80211_internal_bss *bss, *tmp1;
2611 struct cfg80211_bss *nontrans_bss, *tmp;
2616 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2618 spin_lock_bh(&rdev->bss_lock);
2619 if (list_empty(&bss->list))
2622 list_for_each_entry_safe(nontrans_bss, tmp,
2623 &pub->nontrans_list,
2625 tmp1 = container_of(nontrans_bss,
2626 struct cfg80211_internal_bss, pub);
2627 if (__cfg80211_unlink_bss(rdev, tmp1))
2628 rdev->bss_generation++;
2631 if (__cfg80211_unlink_bss(rdev, bss))
2632 rdev->bss_generation++;
2634 spin_unlock_bh(&rdev->bss_lock);
2636 EXPORT_SYMBOL(cfg80211_unlink_bss);
2638 void cfg80211_bss_iter(struct wiphy *wiphy,
2639 struct cfg80211_chan_def *chandef,
2640 void (*iter)(struct wiphy *wiphy,
2641 struct cfg80211_bss *bss,
2645 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2646 struct cfg80211_internal_bss *bss;
2648 spin_lock_bh(&rdev->bss_lock);
2650 list_for_each_entry(bss, &rdev->bss_list, list) {
2651 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
2653 iter(wiphy, &bss->pub, iter_data);
2656 spin_unlock_bh(&rdev->bss_lock);
2658 EXPORT_SYMBOL(cfg80211_bss_iter);
2660 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2661 unsigned int link_id,
2662 struct ieee80211_channel *chan)
2664 struct wiphy *wiphy = wdev->wiphy;
2665 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2666 struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
2667 struct cfg80211_internal_bss *new = NULL;
2668 struct cfg80211_internal_bss *bss;
2669 struct cfg80211_bss *nontrans_bss;
2670 struct cfg80211_bss *tmp;
2672 spin_lock_bh(&rdev->bss_lock);
2675 * Some APs use CSA also for bandwidth changes, i.e., without actually
2676 * changing the control channel, so no need to update in such a case.
2678 if (cbss->pub.channel == chan)
2681 /* use transmitting bss */
2682 if (cbss->pub.transmitted_bss)
2683 cbss = container_of(cbss->pub.transmitted_bss,
2684 struct cfg80211_internal_bss,
2687 cbss->pub.channel = chan;
2689 list_for_each_entry(bss, &rdev->bss_list, list) {
2690 if (!cfg80211_bss_type_match(bss->pub.capability,
2691 bss->pub.channel->band,
2692 wdev->conn_bss_type))
2698 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2705 /* to save time, update IEs for transmitting bss only */
2706 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2707 new->pub.proberesp_ies = NULL;
2708 new->pub.beacon_ies = NULL;
2711 list_for_each_entry_safe(nontrans_bss, tmp,
2712 &new->pub.nontrans_list,
2714 bss = container_of(nontrans_bss,
2715 struct cfg80211_internal_bss, pub);
2716 if (__cfg80211_unlink_bss(rdev, bss))
2717 rdev->bss_generation++;
2720 WARN_ON(atomic_read(&new->hold));
2721 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2722 rdev->bss_generation++;
2725 rb_erase(&cbss->rbn, &rdev->bss_tree);
2726 rb_insert_bss(rdev, cbss);
2727 rdev->bss_generation++;
2729 list_for_each_entry_safe(nontrans_bss, tmp,
2730 &cbss->pub.nontrans_list,
2732 bss = container_of(nontrans_bss,
2733 struct cfg80211_internal_bss, pub);
2734 bss->pub.channel = chan;
2735 rb_erase(&bss->rbn, &rdev->bss_tree);
2736 rb_insert_bss(rdev, bss);
2737 rdev->bss_generation++;
2741 spin_unlock_bh(&rdev->bss_lock);
2744 #ifdef CONFIG_CFG80211_WEXT
2745 static struct cfg80211_registered_device *
2746 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2748 struct cfg80211_registered_device *rdev;
2749 struct net_device *dev;
2753 dev = dev_get_by_index(net, ifindex);
2755 return ERR_PTR(-ENODEV);
2756 if (dev->ieee80211_ptr)
2757 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2759 rdev = ERR_PTR(-ENODEV);
2764 int cfg80211_wext_siwscan(struct net_device *dev,
2765 struct iw_request_info *info,
2766 union iwreq_data *wrqu, char *extra)
2768 struct cfg80211_registered_device *rdev;
2769 struct wiphy *wiphy;
2770 struct iw_scan_req *wreq = NULL;
2771 struct cfg80211_scan_request *creq;
2772 int i, err, n_channels = 0;
2773 enum nl80211_band band;
2775 if (!netif_running(dev))
2778 if (wrqu->data.length == sizeof(struct iw_scan_req))
2779 wreq = (struct iw_scan_req *)extra;
2781 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2784 return PTR_ERR(rdev);
2786 if (rdev->scan_req || rdev->scan_msg)
2789 wiphy = &rdev->wiphy;
2791 /* Determine number of channels, needed to allocate creq */
2792 if (wreq && wreq->num_channels)
2793 n_channels = wreq->num_channels;
2795 n_channels = ieee80211_get_num_supported_channels(wiphy);
2797 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2798 n_channels * sizeof(void *),
2803 creq->wiphy = wiphy;
2804 creq->wdev = dev->ieee80211_ptr;
2805 /* SSIDs come after channels */
2806 creq->ssids = (void *)&creq->channels[n_channels];
2807 creq->n_channels = n_channels;
2809 creq->scan_start = jiffies;
2811 /* translate "Scan on frequencies" request */
2813 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2816 if (!wiphy->bands[band])
2819 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2820 /* ignore disabled channels */
2821 if (wiphy->bands[band]->channels[j].flags &
2822 IEEE80211_CHAN_DISABLED)
2825 /* If we have a wireless request structure and the
2826 * wireless request specifies frequencies, then search
2827 * for the matching hardware channel.
2829 if (wreq && wreq->num_channels) {
2831 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2832 for (k = 0; k < wreq->num_channels; k++) {
2833 struct iw_freq *freq =
2834 &wreq->channel_list[k];
2836 cfg80211_wext_freq(freq);
2838 if (wext_freq == wiphy_freq)
2839 goto wext_freq_found;
2841 goto wext_freq_not_found;
2845 creq->channels[i] = &wiphy->bands[band]->channels[j];
2847 wext_freq_not_found: ;
2850 /* No channels found? */
2856 /* Set real number of channels specified in creq->channels[] */
2857 creq->n_channels = i;
2859 /* translate "Scan for SSID" request */
2861 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2862 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2866 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2867 creq->ssids[0].ssid_len = wreq->essid_len;
2869 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2873 for (i = 0; i < NUM_NL80211_BANDS; i++)
2874 if (wiphy->bands[i])
2875 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2877 eth_broadcast_addr(creq->bssid);
2879 wiphy_lock(&rdev->wiphy);
2881 rdev->scan_req = creq;
2882 err = rdev_scan(rdev, creq);
2884 rdev->scan_req = NULL;
2885 /* creq will be freed below */
2887 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2888 /* creq now owned by driver */
2892 wiphy_unlock(&rdev->wiphy);
2897 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2899 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2900 const struct cfg80211_bss_ies *ies,
2901 char *current_ev, char *end_buf)
2903 const u8 *pos, *end, *next;
2904 struct iw_event iwe;
2910 * If needed, fragment the IEs buffer (at IE boundaries) into short
2911 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2914 end = pos + ies->len;
2916 while (end - pos > IW_GENERIC_IE_MAX) {
2917 next = pos + 2 + pos[1];
2918 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2919 next = next + 2 + next[1];
2921 memset(&iwe, 0, sizeof(iwe));
2922 iwe.cmd = IWEVGENIE;
2923 iwe.u.data.length = next - pos;
2924 current_ev = iwe_stream_add_point_check(info, current_ev,
2927 if (IS_ERR(current_ev))
2933 memset(&iwe, 0, sizeof(iwe));
2934 iwe.cmd = IWEVGENIE;
2935 iwe.u.data.length = end - pos;
2936 current_ev = iwe_stream_add_point_check(info, current_ev,
2939 if (IS_ERR(current_ev))
2947 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2948 struct cfg80211_internal_bss *bss, char *current_ev,
2951 const struct cfg80211_bss_ies *ies;
2952 struct iw_event iwe;
2957 bool ismesh = false;
2959 memset(&iwe, 0, sizeof(iwe));
2960 iwe.cmd = SIOCGIWAP;
2961 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2962 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2963 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2965 if (IS_ERR(current_ev))
2968 memset(&iwe, 0, sizeof(iwe));
2969 iwe.cmd = SIOCGIWFREQ;
2970 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2972 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2974 if (IS_ERR(current_ev))
2977 memset(&iwe, 0, sizeof(iwe));
2978 iwe.cmd = SIOCGIWFREQ;
2979 iwe.u.freq.m = bss->pub.channel->center_freq;
2981 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2983 if (IS_ERR(current_ev))
2986 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2987 memset(&iwe, 0, sizeof(iwe));
2989 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2990 IW_QUAL_NOISE_INVALID |
2991 IW_QUAL_QUAL_UPDATED;
2992 switch (wiphy->signal_type) {
2993 case CFG80211_SIGNAL_TYPE_MBM:
2994 sig = bss->pub.signal / 100;
2995 iwe.u.qual.level = sig;
2996 iwe.u.qual.updated |= IW_QUAL_DBM;
2997 if (sig < -110) /* rather bad */
2999 else if (sig > -40) /* perfect */
3001 /* will give a range of 0 .. 70 */
3002 iwe.u.qual.qual = sig + 110;
3004 case CFG80211_SIGNAL_TYPE_UNSPEC:
3005 iwe.u.qual.level = bss->pub.signal;
3006 /* will give range 0 .. 100 */
3007 iwe.u.qual.qual = bss->pub.signal;
3013 current_ev = iwe_stream_add_event_check(info, current_ev,
3016 if (IS_ERR(current_ev))
3020 memset(&iwe, 0, sizeof(iwe));
3021 iwe.cmd = SIOCGIWENCODE;
3022 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
3023 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3025 iwe.u.data.flags = IW_ENCODE_DISABLED;
3026 iwe.u.data.length = 0;
3027 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3029 if (IS_ERR(current_ev))
3033 ies = rcu_dereference(bss->pub.ies);
3039 if (ie[1] > rem - 2)
3044 memset(&iwe, 0, sizeof(iwe));
3045 iwe.cmd = SIOCGIWESSID;
3046 iwe.u.data.length = ie[1];
3047 iwe.u.data.flags = 1;
3048 current_ev = iwe_stream_add_point_check(info,
3052 if (IS_ERR(current_ev))
3055 case WLAN_EID_MESH_ID:
3056 memset(&iwe, 0, sizeof(iwe));
3057 iwe.cmd = SIOCGIWESSID;
3058 iwe.u.data.length = ie[1];
3059 iwe.u.data.flags = 1;
3060 current_ev = iwe_stream_add_point_check(info,
3064 if (IS_ERR(current_ev))
3067 case WLAN_EID_MESH_CONFIG:
3069 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
3072 memset(&iwe, 0, sizeof(iwe));
3073 iwe.cmd = IWEVCUSTOM;
3074 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
3076 iwe.u.data.length = strlen(buf);
3077 current_ev = iwe_stream_add_point_check(info,
3081 if (IS_ERR(current_ev))
3083 sprintf(buf, "Path Selection Metric ID: 0x%02X",
3085 iwe.u.data.length = strlen(buf);
3086 current_ev = iwe_stream_add_point_check(info,
3090 if (IS_ERR(current_ev))
3092 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
3094 iwe.u.data.length = strlen(buf);
3095 current_ev = iwe_stream_add_point_check(info,
3099 if (IS_ERR(current_ev))
3101 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
3102 iwe.u.data.length = strlen(buf);
3103 current_ev = iwe_stream_add_point_check(info,
3107 if (IS_ERR(current_ev))
3109 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
3110 iwe.u.data.length = strlen(buf);
3111 current_ev = iwe_stream_add_point_check(info,
3115 if (IS_ERR(current_ev))
3117 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
3118 iwe.u.data.length = strlen(buf);
3119 current_ev = iwe_stream_add_point_check(info,
3123 if (IS_ERR(current_ev))
3125 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
3126 iwe.u.data.length = strlen(buf);
3127 current_ev = iwe_stream_add_point_check(info,
3131 if (IS_ERR(current_ev))
3134 case WLAN_EID_SUPP_RATES:
3135 case WLAN_EID_EXT_SUPP_RATES:
3136 /* display all supported rates in readable format */
3137 p = current_ev + iwe_stream_lcp_len(info);
3139 memset(&iwe, 0, sizeof(iwe));
3140 iwe.cmd = SIOCGIWRATE;
3141 /* Those two flags are ignored... */
3142 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3144 for (i = 0; i < ie[1]; i++) {
3145 iwe.u.bitrate.value =
3146 ((ie[i + 2] & 0x7f) * 500000);
3148 p = iwe_stream_add_value(info, current_ev, p,
3152 current_ev = ERR_PTR(-E2BIG);
3163 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3165 memset(&iwe, 0, sizeof(iwe));
3166 iwe.cmd = SIOCGIWMODE;
3168 iwe.u.mode = IW_MODE_MESH;
3169 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3170 iwe.u.mode = IW_MODE_MASTER;
3172 iwe.u.mode = IW_MODE_ADHOC;
3173 current_ev = iwe_stream_add_event_check(info, current_ev,
3176 if (IS_ERR(current_ev))
3180 memset(&iwe, 0, sizeof(iwe));
3181 iwe.cmd = IWEVCUSTOM;
3182 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
3183 iwe.u.data.length = strlen(buf);
3184 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3186 if (IS_ERR(current_ev))
3188 memset(&iwe, 0, sizeof(iwe));
3189 iwe.cmd = IWEVCUSTOM;
3190 sprintf(buf, " Last beacon: %ums ago",
3191 elapsed_jiffies_msecs(bss->ts));
3192 iwe.u.data.length = strlen(buf);
3193 current_ev = iwe_stream_add_point_check(info, current_ev,
3194 end_buf, &iwe, buf);
3195 if (IS_ERR(current_ev))
3198 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3206 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3207 struct iw_request_info *info,
3208 char *buf, size_t len)
3210 char *current_ev = buf;
3211 char *end_buf = buf + len;
3212 struct cfg80211_internal_bss *bss;
3215 spin_lock_bh(&rdev->bss_lock);
3216 cfg80211_bss_expire(rdev);
3218 list_for_each_entry(bss, &rdev->bss_list, list) {
3219 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3223 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
3224 current_ev, end_buf);
3225 if (IS_ERR(current_ev)) {
3226 err = PTR_ERR(current_ev);
3230 spin_unlock_bh(&rdev->bss_lock);
3234 return current_ev - buf;
3238 int cfg80211_wext_giwscan(struct net_device *dev,
3239 struct iw_request_info *info,
3240 struct iw_point *data, char *extra)
3242 struct cfg80211_registered_device *rdev;
3245 if (!netif_running(dev))
3248 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3251 return PTR_ERR(rdev);
3253 if (rdev->scan_req || rdev->scan_msg)
3256 res = ieee80211_scan_results(rdev, info, extra, data->length);
3265 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);