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 (!memcmp(tmp_old + 2, tmp + 2, 5)) {
334 /* same vendor ie, copy from
337 memcpy(pos, tmp, tmp[1] + 2);
339 tmp[0] = WLAN_EID_SSID;
341 memcpy(pos, tmp_old, tmp_old[1] + 2);
342 pos += tmp_old[1] + 2;
345 /* copy ie from subelement into new ie */
346 memcpy(pos, tmp, tmp[1] + 2);
348 tmp[0] = WLAN_EID_SSID;
352 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
355 tmp_old += tmp_old[1] + 2;
358 /* go through subelement again to check if there is any ie not
359 * copied to new ie, skip ssid, capability, bssid-index ie
362 while (tmp_new + 2 - sub_copy <= subie_len &&
363 tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
364 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
365 tmp_new[0] == WLAN_EID_SSID)) {
366 memcpy(pos, tmp_new, tmp_new[1] + 2);
367 pos += tmp_new[1] + 2;
369 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
371 tmp_new += tmp_new[1] + 2;
378 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
379 const u8 *ssid, size_t ssid_len)
381 const struct cfg80211_bss_ies *ies;
382 const struct element *ssid_elem;
384 if (bssid && !ether_addr_equal(a->bssid, bssid))
390 ies = rcu_access_pointer(a->ies);
393 ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
396 if (ssid_elem->datalen != ssid_len)
398 return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
402 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
403 struct cfg80211_bss *nontrans_bss)
405 const struct element *ssid_elem;
406 struct cfg80211_bss *bss = NULL;
409 ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
415 /* check if nontrans_bss is in the list */
416 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
417 if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
418 ssid_elem->datalen)) {
427 * This is a bit weird - it's not on the list, but already on another
428 * one! The only way that could happen is if there's some BSSID/SSID
429 * shared by multiple APs in their multi-BSSID profiles, potentially
430 * with hidden SSID mixed in ... ignore it.
432 if (!list_empty(&nontrans_bss->nontrans_list))
435 /* add to the list */
436 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
440 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
441 unsigned long expire_time)
443 struct cfg80211_internal_bss *bss, *tmp;
444 bool expired = false;
446 lockdep_assert_held(&rdev->bss_lock);
448 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
449 if (atomic_read(&bss->hold))
451 if (!time_after(expire_time, bss->ts))
454 if (__cfg80211_unlink_bss(rdev, bss))
459 rdev->bss_generation++;
462 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
464 struct cfg80211_internal_bss *bss, *oldest = NULL;
467 lockdep_assert_held(&rdev->bss_lock);
469 list_for_each_entry(bss, &rdev->bss_list, list) {
470 if (atomic_read(&bss->hold))
473 if (!list_empty(&bss->hidden_list) &&
474 !bss->pub.hidden_beacon_bss)
477 if (oldest && time_before(oldest->ts, bss->ts))
482 if (WARN_ON(!oldest))
486 * The callers make sure to increase rdev->bss_generation if anything
487 * gets removed (and a new entry added), so there's no need to also do
491 ret = __cfg80211_unlink_bss(rdev, oldest);
496 static u8 cfg80211_parse_bss_param(u8 data,
497 struct cfg80211_colocated_ap *coloc_ap)
499 coloc_ap->oct_recommended =
500 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
501 coloc_ap->same_ssid =
502 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
503 coloc_ap->multi_bss =
504 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
505 coloc_ap->transmitted_bssid =
506 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
507 coloc_ap->unsolicited_probe =
508 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
509 coloc_ap->colocated_ess =
510 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
512 return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
515 static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
516 const struct element **elem, u32 *s_ssid)
519 *elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
520 if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
523 *s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
527 static void cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
529 struct cfg80211_colocated_ap *ap, *tmp_ap;
531 list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
537 static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
538 const u8 *pos, u8 length,
539 const struct element *ssid_elem,
542 /* skip the TBTT offset */
545 memcpy(entry->bssid, pos, ETH_ALEN);
548 if (length >= IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM) {
549 memcpy(&entry->short_ssid, pos,
550 sizeof(entry->short_ssid));
551 entry->short_ssid_valid = true;
555 /* skip non colocated APs */
556 if (!cfg80211_parse_bss_param(*pos, entry))
560 if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM) {
562 * no information about the short ssid. Consider the entry valid
563 * for now. It would later be dropped in case there are explicit
564 * SSIDs that need to be matched
566 if (!entry->same_ssid)
570 if (entry->same_ssid) {
571 entry->short_ssid = s_ssid_tmp;
572 entry->short_ssid_valid = true;
575 * This is safe because we validate datalen in
576 * cfg80211_parse_colocated_ap(), before calling this
579 memcpy(&entry->ssid, &ssid_elem->data,
581 entry->ssid_len = ssid_elem->datalen;
586 static int cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
587 struct list_head *list)
589 struct ieee80211_neighbor_ap_info *ap_info;
590 const struct element *elem, *ssid_elem;
593 int n_coloc = 0, ret;
596 elem = cfg80211_find_elem(WLAN_EID_REDUCED_NEIGHBOR_REPORT, ies->data,
602 end = pos + elem->datalen;
604 ret = cfg80211_calc_short_ssid(ies, &ssid_elem, &s_ssid_tmp);
608 /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
609 while (pos + sizeof(*ap_info) <= end) {
610 enum nl80211_band band;
614 ap_info = (void *)pos;
615 count = u8_get_bits(ap_info->tbtt_info_hdr,
616 IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
617 length = ap_info->tbtt_info_len;
619 pos += sizeof(*ap_info);
621 if (!ieee80211_operating_class_to_band(ap_info->op_class,
625 freq = ieee80211_channel_to_frequency(ap_info->channel, band);
627 if (end - pos < count * length)
631 * TBTT info must include bss param + BSSID +
632 * (short SSID or same_ssid bit to be set).
633 * ignore other options, and move to the
636 if (band != NL80211_BAND_6GHZ ||
637 (length != IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM &&
638 length < IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM)) {
639 pos += count * length;
643 for (i = 0; i < count; i++) {
644 struct cfg80211_colocated_ap *entry;
646 entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN,
652 entry->center_freq = freq;
654 if (!cfg80211_parse_ap_info(entry, pos, length,
655 ssid_elem, s_ssid_tmp)) {
657 list_add_tail(&entry->list, &ap_list);
667 cfg80211_free_coloc_ap_list(&ap_list);
671 list_splice_tail(&ap_list, list);
675 static void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
676 struct ieee80211_channel *chan,
680 u32 n_channels = request->n_channels;
681 struct cfg80211_scan_6ghz_params *params =
682 &request->scan_6ghz_params[request->n_6ghz_params];
684 for (i = 0; i < n_channels; i++) {
685 if (request->channels[i] == chan) {
687 params->channel_idx = i;
692 request->channels[n_channels] = chan;
694 request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
697 request->n_channels++;
700 static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
701 struct cfg80211_scan_request *request)
706 for (i = 0; i < request->n_ssids; i++) {
707 /* wildcard ssid in the scan request */
708 if (!request->ssids[i].ssid_len) {
709 if (ap->multi_bss && !ap->transmitted_bssid)
716 ap->ssid_len == request->ssids[i].ssid_len) {
717 if (!memcmp(request->ssids[i].ssid, ap->ssid,
720 } else if (ap->short_ssid_valid) {
721 s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
722 request->ssids[i].ssid_len);
724 if (ap->short_ssid == s_ssid)
732 static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
735 struct cfg80211_colocated_ap *ap;
736 int n_channels, count = 0, err;
737 struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
738 LIST_HEAD(coloc_ap_list);
739 bool need_scan_psc = true;
740 const struct ieee80211_sband_iftype_data *iftd;
742 rdev_req->scan_6ghz = true;
744 if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
747 iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
748 rdev_req->wdev->iftype);
749 if (!iftd || !iftd->he_cap.has_he)
752 n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
754 if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
755 struct cfg80211_internal_bss *intbss;
757 spin_lock_bh(&rdev->bss_lock);
758 list_for_each_entry(intbss, &rdev->bss_list, list) {
759 struct cfg80211_bss *res = &intbss->pub;
760 const struct cfg80211_bss_ies *ies;
762 ies = rcu_access_pointer(res->ies);
763 count += cfg80211_parse_colocated_ap(ies,
766 spin_unlock_bh(&rdev->bss_lock);
769 request = kzalloc(struct_size(request, channels, n_channels) +
770 sizeof(*request->scan_6ghz_params) * count +
771 sizeof(*request->ssids) * rdev_req->n_ssids,
774 cfg80211_free_coloc_ap_list(&coloc_ap_list);
778 *request = *rdev_req;
779 request->n_channels = 0;
780 request->scan_6ghz_params =
781 (void *)&request->channels[n_channels];
784 * PSC channels should not be scanned in case of direct scan with 1 SSID
785 * and at least one of the reported co-located APs with same SSID
786 * indicating that all APs in the same ESS are co-located
788 if (count && request->n_ssids == 1 && request->ssids[0].ssid_len) {
789 list_for_each_entry(ap, &coloc_ap_list, list) {
790 if (ap->colocated_ess &&
791 cfg80211_find_ssid_match(ap, request)) {
792 need_scan_psc = false;
799 * add to the scan request the channels that need to be scanned
800 * regardless of the collocated APs (PSC channels or all channels
801 * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
803 for (i = 0; i < rdev_req->n_channels; i++) {
804 if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
806 cfg80211_channel_is_psc(rdev_req->channels[i])) ||
807 !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
808 cfg80211_scan_req_add_chan(request,
809 rdev_req->channels[i],
814 if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
817 list_for_each_entry(ap, &coloc_ap_list, list) {
819 struct cfg80211_scan_6ghz_params *scan_6ghz_params =
820 &request->scan_6ghz_params[request->n_6ghz_params];
821 struct ieee80211_channel *chan =
822 ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
824 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
827 for (i = 0; i < rdev_req->n_channels; i++) {
828 if (rdev_req->channels[i] == chan)
835 if (request->n_ssids > 0 &&
836 !cfg80211_find_ssid_match(ap, request))
839 if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
842 cfg80211_scan_req_add_chan(request, chan, true);
843 memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
844 scan_6ghz_params->short_ssid = ap->short_ssid;
845 scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
846 scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
849 * If a PSC channel is added to the scan and 'need_scan_psc' is
850 * set to false, then all the APs that the scan logic is
851 * interested with on the channel are collocated and thus there
852 * is no need to perform the initial PSC channel listen.
854 if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
855 scan_6ghz_params->psc_no_listen = true;
857 request->n_6ghz_params++;
861 cfg80211_free_coloc_ap_list(&coloc_ap_list);
863 if (request->n_channels) {
864 struct cfg80211_scan_request *old = rdev->int_scan_req;
865 rdev->int_scan_req = request;
868 * Add the ssids from the parent scan request to the new scan
869 * request, so the driver would be able to use them in its
870 * probe requests to discover hidden APs on PSC channels.
872 request->ssids = (void *)&request->channels[request->n_channels];
873 request->n_ssids = rdev_req->n_ssids;
874 memcpy(request->ssids, rdev_req->ssids, sizeof(*request->ssids) *
878 * If this scan follows a previous scan, save the scan start
879 * info from the first part of the scan
882 rdev->int_scan_req->info = old->info;
884 err = rdev_scan(rdev, request);
886 rdev->int_scan_req = old;
899 int cfg80211_scan(struct cfg80211_registered_device *rdev)
901 struct cfg80211_scan_request *request;
902 struct cfg80211_scan_request *rdev_req = rdev->scan_req;
903 u32 n_channels = 0, idx, i;
905 if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
906 return rdev_scan(rdev, rdev_req);
908 for (i = 0; i < rdev_req->n_channels; i++) {
909 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
914 return cfg80211_scan_6ghz(rdev);
916 request = kzalloc(struct_size(request, channels, n_channels),
921 *request = *rdev_req;
922 request->n_channels = n_channels;
924 for (i = idx = 0; i < rdev_req->n_channels; i++) {
925 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
926 request->channels[idx++] = rdev_req->channels[i];
929 rdev_req->scan_6ghz = false;
930 rdev->int_scan_req = request;
931 return rdev_scan(rdev, request);
934 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
937 struct cfg80211_scan_request *request, *rdev_req;
938 struct wireless_dev *wdev;
940 #ifdef CONFIG_CFG80211_WEXT
941 union iwreq_data wrqu;
944 lockdep_assert_held(&rdev->wiphy.mtx);
946 if (rdev->scan_msg) {
947 nl80211_send_scan_msg(rdev, rdev->scan_msg);
948 rdev->scan_msg = NULL;
952 rdev_req = rdev->scan_req;
956 wdev = rdev_req->wdev;
957 request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
959 if (wdev_running(wdev) &&
960 (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
961 !rdev_req->scan_6ghz && !request->info.aborted &&
962 !cfg80211_scan_6ghz(rdev))
966 * This must be before sending the other events!
967 * Otherwise, wpa_supplicant gets completely confused with
971 cfg80211_sme_scan_done(wdev->netdev);
973 if (!request->info.aborted &&
974 request->flags & NL80211_SCAN_FLAG_FLUSH) {
975 /* flush entries from previous scans */
976 spin_lock_bh(&rdev->bss_lock);
977 __cfg80211_bss_expire(rdev, request->scan_start);
978 spin_unlock_bh(&rdev->bss_lock);
981 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
983 #ifdef CONFIG_CFG80211_WEXT
984 if (wdev->netdev && !request->info.aborted) {
985 memset(&wrqu, 0, sizeof(wrqu));
987 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
991 dev_put(wdev->netdev);
993 kfree(rdev->int_scan_req);
994 rdev->int_scan_req = NULL;
996 kfree(rdev->scan_req);
997 rdev->scan_req = NULL;
1000 rdev->scan_msg = msg;
1002 nl80211_send_scan_msg(rdev, msg);
1005 void __cfg80211_scan_done(struct work_struct *wk)
1007 struct cfg80211_registered_device *rdev;
1009 rdev = container_of(wk, struct cfg80211_registered_device,
1012 wiphy_lock(&rdev->wiphy);
1013 ___cfg80211_scan_done(rdev, true);
1014 wiphy_unlock(&rdev->wiphy);
1017 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1018 struct cfg80211_scan_info *info)
1020 struct cfg80211_scan_info old_info = request->info;
1022 trace_cfg80211_scan_done(request, info);
1023 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
1024 request != wiphy_to_rdev(request->wiphy)->int_scan_req);
1026 request->info = *info;
1029 * In case the scan is split, the scan_start_tsf and tsf_bssid should
1030 * be of the first part. In such a case old_info.scan_start_tsf should
1033 if (request->scan_6ghz && old_info.scan_start_tsf) {
1034 request->info.scan_start_tsf = old_info.scan_start_tsf;
1035 memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
1036 sizeof(request->info.tsf_bssid));
1039 request->notified = true;
1040 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
1042 EXPORT_SYMBOL(cfg80211_scan_done);
1044 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1045 struct cfg80211_sched_scan_request *req)
1047 lockdep_assert_held(&rdev->wiphy.mtx);
1049 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1052 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1053 struct cfg80211_sched_scan_request *req)
1055 lockdep_assert_held(&rdev->wiphy.mtx);
1057 list_del_rcu(&req->list);
1058 kfree_rcu(req, rcu_head);
1061 static struct cfg80211_sched_scan_request *
1062 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1064 struct cfg80211_sched_scan_request *pos;
1066 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1067 lockdep_is_held(&rdev->wiphy.mtx)) {
1068 if (pos->reqid == reqid)
1075 * Determines if a scheduled scan request can be handled. When a legacy
1076 * scheduled scan is running no other scheduled scan is allowed regardless
1077 * whether the request is for legacy or multi-support scan. When a multi-support
1078 * scheduled scan is running a request for legacy scan is not allowed. In this
1079 * case a request for multi-support scan can be handled if resources are
1080 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1082 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1085 struct cfg80211_sched_scan_request *pos;
1088 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1089 /* request id zero means legacy in progress */
1090 if (!i && !pos->reqid)
1091 return -EINPROGRESS;
1096 /* no legacy allowed when multi request(s) are active */
1098 return -EINPROGRESS;
1100 /* resource limit reached */
1101 if (i == rdev->wiphy.max_sched_scan_reqs)
1107 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1109 struct cfg80211_registered_device *rdev;
1110 struct cfg80211_sched_scan_request *req, *tmp;
1112 rdev = container_of(work, struct cfg80211_registered_device,
1115 wiphy_lock(&rdev->wiphy);
1116 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1117 if (req->report_results) {
1118 req->report_results = false;
1119 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1120 /* flush entries from previous scans */
1121 spin_lock_bh(&rdev->bss_lock);
1122 __cfg80211_bss_expire(rdev, req->scan_start);
1123 spin_unlock_bh(&rdev->bss_lock);
1124 req->scan_start = jiffies;
1126 nl80211_send_sched_scan(req,
1127 NL80211_CMD_SCHED_SCAN_RESULTS);
1130 wiphy_unlock(&rdev->wiphy);
1133 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1135 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1136 struct cfg80211_sched_scan_request *request;
1138 trace_cfg80211_sched_scan_results(wiphy, reqid);
1139 /* ignore if we're not scanning */
1142 request = cfg80211_find_sched_scan_req(rdev, reqid);
1144 request->report_results = true;
1145 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1149 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1151 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1153 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1155 lockdep_assert_held(&wiphy->mtx);
1157 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1159 __cfg80211_stop_sched_scan(rdev, reqid, true);
1161 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1163 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1166 cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1167 wiphy_unlock(wiphy);
1169 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1171 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1172 struct cfg80211_sched_scan_request *req,
1173 bool driver_initiated)
1175 lockdep_assert_held(&rdev->wiphy.mtx);
1177 if (!driver_initiated) {
1178 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1183 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1185 cfg80211_del_sched_scan_req(rdev, req);
1190 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1191 u64 reqid, bool driver_initiated)
1193 struct cfg80211_sched_scan_request *sched_scan_req;
1195 lockdep_assert_held(&rdev->wiphy.mtx);
1197 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1198 if (!sched_scan_req)
1201 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1205 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1206 unsigned long age_secs)
1208 struct cfg80211_internal_bss *bss;
1209 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
1211 spin_lock_bh(&rdev->bss_lock);
1212 list_for_each_entry(bss, &rdev->bss_list, list)
1213 bss->ts -= age_jiffies;
1214 spin_unlock_bh(&rdev->bss_lock);
1217 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1219 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1222 void cfg80211_bss_flush(struct wiphy *wiphy)
1224 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1226 spin_lock_bh(&rdev->bss_lock);
1227 __cfg80211_bss_expire(rdev, jiffies);
1228 spin_unlock_bh(&rdev->bss_lock);
1230 EXPORT_SYMBOL(cfg80211_bss_flush);
1232 const struct element *
1233 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1234 const u8 *match, unsigned int match_len,
1235 unsigned int match_offset)
1237 const struct element *elem;
1239 for_each_element_id(elem, eid, ies, len) {
1240 if (elem->datalen >= match_offset + match_len &&
1241 !memcmp(elem->data + match_offset, match, match_len))
1247 EXPORT_SYMBOL(cfg80211_find_elem_match);
1249 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1253 const struct element *elem;
1254 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1255 int match_len = (oui_type < 0) ? 3 : sizeof(match);
1257 if (WARN_ON(oui_type > 0xff))
1260 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1261 match, match_len, 0);
1263 if (!elem || elem->datalen < 4)
1268 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1271 * enum bss_compare_mode - BSS compare mode
1272 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1273 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1274 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1276 enum bss_compare_mode {
1282 static int cmp_bss(struct cfg80211_bss *a,
1283 struct cfg80211_bss *b,
1284 enum bss_compare_mode mode)
1286 const struct cfg80211_bss_ies *a_ies, *b_ies;
1287 const u8 *ie1 = NULL;
1288 const u8 *ie2 = NULL;
1291 if (a->channel != b->channel)
1292 return b->channel->center_freq - a->channel->center_freq;
1294 a_ies = rcu_access_pointer(a->ies);
1297 b_ies = rcu_access_pointer(b->ies);
1301 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1302 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1303 a_ies->data, a_ies->len);
1304 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1305 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1306 b_ies->data, b_ies->len);
1310 if (ie1[1] == ie2[1])
1311 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1313 mesh_id_cmp = ie2[1] - ie1[1];
1315 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1316 a_ies->data, a_ies->len);
1317 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1318 b_ies->data, b_ies->len);
1322 if (ie1[1] != ie2[1])
1323 return ie2[1] - ie1[1];
1324 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1328 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1332 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1333 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1339 * Note that with "hide_ssid", the function returns a match if
1340 * the already-present BSS ("b") is a hidden SSID beacon for
1341 * the new BSS ("a").
1344 /* sort missing IE before (left of) present IE */
1351 case BSS_CMP_HIDE_ZLEN:
1353 * In ZLEN mode we assume the BSS entry we're
1354 * looking for has a zero-length SSID. So if
1355 * the one we're looking at right now has that,
1356 * return 0. Otherwise, return the difference
1357 * in length, but since we're looking for the
1358 * 0-length it's really equivalent to returning
1359 * the length of the one we're looking at.
1361 * No content comparison is needed as we assume
1362 * the content length is zero.
1365 case BSS_CMP_REGULAR:
1367 /* sort by length first, then by contents */
1368 if (ie1[1] != ie2[1])
1369 return ie2[1] - ie1[1];
1370 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1371 case BSS_CMP_HIDE_NUL:
1372 if (ie1[1] != ie2[1])
1373 return ie2[1] - ie1[1];
1374 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1375 for (i = 0; i < ie2[1]; i++)
1382 static bool cfg80211_bss_type_match(u16 capability,
1383 enum nl80211_band band,
1384 enum ieee80211_bss_type bss_type)
1389 if (bss_type == IEEE80211_BSS_TYPE_ANY)
1392 if (band == NL80211_BAND_60GHZ) {
1393 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1395 case IEEE80211_BSS_TYPE_ESS:
1396 val = WLAN_CAPABILITY_DMG_TYPE_AP;
1398 case IEEE80211_BSS_TYPE_PBSS:
1399 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1401 case IEEE80211_BSS_TYPE_IBSS:
1402 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1408 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1410 case IEEE80211_BSS_TYPE_ESS:
1411 val = WLAN_CAPABILITY_ESS;
1413 case IEEE80211_BSS_TYPE_IBSS:
1414 val = WLAN_CAPABILITY_IBSS;
1416 case IEEE80211_BSS_TYPE_MBSS:
1424 ret = ((capability & mask) == val);
1428 /* Returned bss is reference counted and must be cleaned up appropriately. */
1429 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
1430 struct ieee80211_channel *channel,
1432 const u8 *ssid, size_t ssid_len,
1433 enum ieee80211_bss_type bss_type,
1434 enum ieee80211_privacy privacy)
1436 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1437 struct cfg80211_internal_bss *bss, *res = NULL;
1438 unsigned long now = jiffies;
1441 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1444 spin_lock_bh(&rdev->bss_lock);
1446 list_for_each_entry(bss, &rdev->bss_list, list) {
1447 if (!cfg80211_bss_type_match(bss->pub.capability,
1448 bss->pub.channel->band, bss_type))
1451 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1452 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1453 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1455 if (channel && bss->pub.channel != channel)
1457 if (!is_valid_ether_addr(bss->pub.bssid))
1459 /* Don't get expired BSS structs */
1460 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1461 !atomic_read(&bss->hold))
1463 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
1465 bss_ref_get(rdev, res);
1470 spin_unlock_bh(&rdev->bss_lock);
1473 trace_cfg80211_return_bss(&res->pub);
1476 EXPORT_SYMBOL(cfg80211_get_bss);
1478 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1479 struct cfg80211_internal_bss *bss)
1481 struct rb_node **p = &rdev->bss_tree.rb_node;
1482 struct rb_node *parent = NULL;
1483 struct cfg80211_internal_bss *tbss;
1488 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1490 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1492 if (WARN_ON(!cmp)) {
1493 /* will sort of leak this BSS */
1500 p = &(*p)->rb_right;
1503 rb_link_node(&bss->rbn, parent, p);
1504 rb_insert_color(&bss->rbn, &rdev->bss_tree);
1507 static struct cfg80211_internal_bss *
1508 rb_find_bss(struct cfg80211_registered_device *rdev,
1509 struct cfg80211_internal_bss *res,
1510 enum bss_compare_mode mode)
1512 struct rb_node *n = rdev->bss_tree.rb_node;
1513 struct cfg80211_internal_bss *bss;
1517 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1518 r = cmp_bss(&res->pub, &bss->pub, mode);
1531 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1532 struct cfg80211_internal_bss *new)
1534 const struct cfg80211_bss_ies *ies;
1535 struct cfg80211_internal_bss *bss;
1541 ies = rcu_access_pointer(new->pub.beacon_ies);
1545 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1552 for (i = 0; i < ssidlen; i++)
1556 /* not a hidden SSID */
1560 /* This is the bad part ... */
1562 list_for_each_entry(bss, &rdev->bss_list, list) {
1564 * we're iterating all the entries anyway, so take the
1565 * opportunity to validate the list length accounting
1569 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1571 if (bss->pub.channel != new->pub.channel)
1573 if (bss->pub.scan_width != new->pub.scan_width)
1575 if (rcu_access_pointer(bss->pub.beacon_ies))
1577 ies = rcu_access_pointer(bss->pub.ies);
1580 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1583 if (ssidlen && ie[1] != ssidlen)
1585 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1587 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1588 list_del(&bss->hidden_list);
1590 list_add(&bss->hidden_list, &new->hidden_list);
1591 bss->pub.hidden_beacon_bss = &new->pub;
1592 new->refcount += bss->refcount;
1593 rcu_assign_pointer(bss->pub.beacon_ies,
1594 new->pub.beacon_ies);
1597 WARN_ONCE(n_entries != rdev->bss_entries,
1598 "rdev bss entries[%d]/list[len:%d] corruption\n",
1599 rdev->bss_entries, n_entries);
1604 struct cfg80211_non_tx_bss {
1605 struct cfg80211_bss *tx_bss;
1606 u8 max_bssid_indicator;
1610 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1611 const struct cfg80211_bss_ies *new_ies,
1612 const struct cfg80211_bss_ies *old_ies)
1614 struct cfg80211_internal_bss *bss;
1616 /* Assign beacon IEs to all sub entries */
1617 list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1618 const struct cfg80211_bss_ies *ies;
1620 ies = rcu_access_pointer(bss->pub.beacon_ies);
1621 WARN_ON(ies != old_ies);
1623 rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1628 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1629 struct cfg80211_internal_bss *known,
1630 struct cfg80211_internal_bss *new,
1633 lockdep_assert_held(&rdev->bss_lock);
1636 if (rcu_access_pointer(new->pub.proberesp_ies)) {
1637 const struct cfg80211_bss_ies *old;
1639 old = rcu_access_pointer(known->pub.proberesp_ies);
1641 rcu_assign_pointer(known->pub.proberesp_ies,
1642 new->pub.proberesp_ies);
1643 /* Override possible earlier Beacon frame IEs */
1644 rcu_assign_pointer(known->pub.ies,
1645 new->pub.proberesp_ies);
1647 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1648 } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1649 const struct cfg80211_bss_ies *old;
1651 if (known->pub.hidden_beacon_bss &&
1652 !list_empty(&known->hidden_list)) {
1653 const struct cfg80211_bss_ies *f;
1655 /* The known BSS struct is one of the probe
1656 * response members of a group, but we're
1657 * receiving a beacon (beacon_ies in the new
1658 * bss is used). This can only mean that the
1659 * AP changed its beacon from not having an
1660 * SSID to showing it, which is confusing so
1661 * drop this information.
1664 f = rcu_access_pointer(new->pub.beacon_ies);
1665 kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1669 old = rcu_access_pointer(known->pub.beacon_ies);
1671 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1673 /* Override IEs if they were from a beacon before */
1674 if (old == rcu_access_pointer(known->pub.ies))
1675 rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1677 cfg80211_update_hidden_bsses(known,
1678 rcu_access_pointer(new->pub.beacon_ies),
1682 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1685 known->pub.beacon_interval = new->pub.beacon_interval;
1687 /* don't update the signal if beacon was heard on
1691 known->pub.signal = new->pub.signal;
1692 known->pub.capability = new->pub.capability;
1693 known->ts = new->ts;
1694 known->ts_boottime = new->ts_boottime;
1695 known->parent_tsf = new->parent_tsf;
1696 known->pub.chains = new->pub.chains;
1697 memcpy(known->pub.chain_signal, new->pub.chain_signal,
1698 IEEE80211_MAX_CHAINS);
1699 ether_addr_copy(known->parent_bssid, new->parent_bssid);
1700 known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1701 known->pub.bssid_index = new->pub.bssid_index;
1706 /* Returned bss is reference counted and must be cleaned up appropriately. */
1707 struct cfg80211_internal_bss *
1708 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1709 struct cfg80211_internal_bss *tmp,
1710 bool signal_valid, unsigned long ts)
1712 struct cfg80211_internal_bss *found = NULL;
1714 if (WARN_ON(!tmp->pub.channel))
1719 spin_lock_bh(&rdev->bss_lock);
1721 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1722 spin_unlock_bh(&rdev->bss_lock);
1726 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1729 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1732 struct cfg80211_internal_bss *new;
1733 struct cfg80211_internal_bss *hidden;
1734 struct cfg80211_bss_ies *ies;
1737 * create a copy -- the "res" variable that is passed in
1738 * is allocated on the stack since it's not needed in the
1739 * more common case of an update
1741 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1744 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1746 kfree_rcu(ies, rcu_head);
1747 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1749 kfree_rcu(ies, rcu_head);
1752 memcpy(new, tmp, sizeof(*new));
1754 INIT_LIST_HEAD(&new->hidden_list);
1755 INIT_LIST_HEAD(&new->pub.nontrans_list);
1756 /* we'll set this later if it was non-NULL */
1757 new->pub.transmitted_bss = NULL;
1759 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1760 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1762 hidden = rb_find_bss(rdev, tmp,
1765 new->pub.hidden_beacon_bss = &hidden->pub;
1766 list_add(&new->hidden_list,
1767 &hidden->hidden_list);
1769 rcu_assign_pointer(new->pub.beacon_ies,
1770 hidden->pub.beacon_ies);
1774 * Ok so we found a beacon, and don't have an entry. If
1775 * it's a beacon with hidden SSID, we might be in for an
1776 * expensive search for any probe responses that should
1777 * be grouped with this beacon for updates ...
1779 if (!cfg80211_combine_bsses(rdev, new)) {
1780 bss_ref_put(rdev, new);
1785 if (rdev->bss_entries >= bss_entries_limit &&
1786 !cfg80211_bss_expire_oldest(rdev)) {
1787 bss_ref_put(rdev, new);
1791 /* This must be before the call to bss_ref_get */
1792 if (tmp->pub.transmitted_bss) {
1793 struct cfg80211_internal_bss *pbss =
1794 container_of(tmp->pub.transmitted_bss,
1795 struct cfg80211_internal_bss,
1798 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1799 bss_ref_get(rdev, pbss);
1802 list_add_tail(&new->list, &rdev->bss_list);
1803 rdev->bss_entries++;
1804 rb_insert_bss(rdev, new);
1808 rdev->bss_generation++;
1809 bss_ref_get(rdev, found);
1810 spin_unlock_bh(&rdev->bss_lock);
1814 spin_unlock_bh(&rdev->bss_lock);
1818 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
1819 enum nl80211_band band,
1820 enum cfg80211_bss_frame_type ftype)
1822 const struct element *tmp;
1824 if (band == NL80211_BAND_6GHZ) {
1825 struct ieee80211_he_operation *he_oper;
1827 tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
1829 if (tmp && tmp->datalen >= sizeof(*he_oper) &&
1830 tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
1831 const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
1833 he_oper = (void *)&tmp->data[1];
1835 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
1839 if (ftype != CFG80211_BSS_FTYPE_BEACON ||
1840 he_6ghz_oper->control & IEEE80211_HE_6GHZ_OPER_CTRL_DUP_BEACON)
1841 return he_6ghz_oper->primary;
1843 } else if (band == NL80211_BAND_S1GHZ) {
1844 tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
1845 if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
1846 struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
1848 return s1gop->oper_ch;
1851 tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
1852 if (tmp && tmp->datalen == 1)
1853 return tmp->data[0];
1855 tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
1857 tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
1858 struct ieee80211_ht_operation *htop = (void *)tmp->data;
1860 return htop->primary_chan;
1866 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
1869 * Update RX channel information based on the available frame payload
1870 * information. This is mainly for the 2.4 GHz band where frames can be received
1871 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1872 * element to indicate the current (transmitting) channel, but this might also
1873 * be needed on other bands if RX frequency does not match with the actual
1874 * operating channel of a BSS, or if the AP reports a different primary channel.
1876 static struct ieee80211_channel *
1877 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1878 struct ieee80211_channel *channel,
1879 enum nl80211_bss_scan_width scan_width,
1880 enum cfg80211_bss_frame_type ftype)
1884 struct ieee80211_channel *alt_channel;
1886 channel_number = cfg80211_get_ies_channel_number(ie, ielen,
1887 channel->band, ftype);
1889 if (channel_number < 0) {
1890 /* No channel information in frame payload */
1894 freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1897 * In 6GHz, duplicated beacon indication is relevant for
1900 if (channel->band == NL80211_BAND_6GHZ &&
1901 (freq == channel->center_freq ||
1902 abs(freq - channel->center_freq) > 80))
1905 alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1907 if (channel->band == NL80211_BAND_2GHZ) {
1909 * Better not allow unexpected channels when that could
1910 * be going beyond the 1-11 range (e.g., discovering
1911 * BSS on channel 12 when radio is configured for
1917 /* No match for the payload channel number - ignore it */
1921 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1922 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1924 * Ignore channel number in 5 and 10 MHz channels where there
1925 * may not be an n:1 or 1:n mapping between frequencies and
1932 * Use the channel determined through the payload channel number
1933 * instead of the RX channel reported by the driver.
1935 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1940 /* Returned bss is reference counted and must be cleaned up appropriately. */
1941 static struct cfg80211_bss *
1942 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1943 struct cfg80211_inform_bss *data,
1944 enum cfg80211_bss_frame_type ftype,
1945 const u8 *bssid, u64 tsf, u16 capability,
1946 u16 beacon_interval, const u8 *ie, size_t ielen,
1947 struct cfg80211_non_tx_bss *non_tx_data,
1950 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1951 struct cfg80211_bss_ies *ies;
1952 struct ieee80211_channel *channel;
1953 struct cfg80211_internal_bss tmp = {}, *res;
1958 if (WARN_ON(!wiphy))
1961 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1962 (data->signal < 0 || data->signal > 100)))
1965 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1966 data->scan_width, ftype);
1970 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1971 tmp.pub.channel = channel;
1972 tmp.pub.scan_width = data->scan_width;
1973 tmp.pub.signal = data->signal;
1974 tmp.pub.beacon_interval = beacon_interval;
1975 tmp.pub.capability = capability;
1976 tmp.ts_boottime = data->boottime_ns;
1977 tmp.parent_tsf = data->parent_tsf;
1978 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1981 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1982 ts = bss_from_pub(non_tx_data->tx_bss)->ts;
1983 tmp.pub.bssid_index = non_tx_data->bssid_index;
1984 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1990 * If we do not know here whether the IEs are from a Beacon or Probe
1991 * Response frame, we need to pick one of the options and only use it
1992 * with the driver that does not provide the full Beacon/Probe Response
1993 * frame. Use Beacon frame pointer to avoid indicating that this should
1994 * override the IEs pointer should we have received an earlier
1995 * indication of Probe Response data.
1997 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2002 ies->from_beacon = false;
2003 memcpy(ies->data, ie, ielen);
2006 case CFG80211_BSS_FTYPE_BEACON:
2007 ies->from_beacon = true;
2009 case CFG80211_BSS_FTYPE_UNKNOWN:
2010 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2012 case CFG80211_BSS_FTYPE_PRESP:
2013 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2016 rcu_assign_pointer(tmp.pub.ies, ies);
2018 signal_valid = data->chan == channel;
2019 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
2023 if (channel->band == NL80211_BAND_60GHZ) {
2024 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2025 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2026 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2027 regulatory_hint_found_beacon(wiphy, channel, gfp);
2029 if (res->pub.capability & WLAN_CAPABILITY_ESS)
2030 regulatory_hint_found_beacon(wiphy, channel, gfp);
2034 /* this is a nontransmitting bss, we need to add it to
2035 * transmitting bss' list if it is not there
2037 spin_lock_bh(&rdev->bss_lock);
2038 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
2040 if (__cfg80211_unlink_bss(rdev, res)) {
2041 rdev->bss_generation++;
2045 spin_unlock_bh(&rdev->bss_lock);
2051 trace_cfg80211_return_bss(&res->pub);
2052 /* cfg80211_bss_update gives us a referenced result */
2056 static const struct element
2057 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
2058 const struct element *mbssid_elem,
2059 const struct element *sub_elem)
2061 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
2062 const struct element *next_mbssid;
2063 const struct element *next_sub;
2065 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2067 ielen - (mbssid_end - ie));
2070 * If it is not the last subelement in current MBSSID IE or there isn't
2071 * a next MBSSID IE - profile is complete.
2073 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
2077 /* For any length error, just return NULL */
2079 if (next_mbssid->datalen < 4)
2082 next_sub = (void *)&next_mbssid->data[1];
2084 if (next_mbssid->data + next_mbssid->datalen <
2085 next_sub->data + next_sub->datalen)
2088 if (next_sub->id != 0 || next_sub->datalen < 2)
2092 * Check if the first element in the next sub element is a start
2095 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
2099 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2100 const struct element *mbssid_elem,
2101 const struct element *sub_elem,
2102 u8 *merged_ie, size_t max_copy_len)
2104 size_t copied_len = sub_elem->datalen;
2105 const struct element *next_mbssid;
2107 if (sub_elem->datalen > max_copy_len)
2110 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2112 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
2115 const struct element *next_sub = (void *)&next_mbssid->data[1];
2117 if (copied_len + next_sub->datalen > max_copy_len)
2119 memcpy(merged_ie + copied_len, next_sub->data,
2121 copied_len += next_sub->datalen;
2126 EXPORT_SYMBOL(cfg80211_merge_profile);
2128 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2129 struct cfg80211_inform_bss *data,
2130 enum cfg80211_bss_frame_type ftype,
2131 const u8 *bssid, u64 tsf,
2132 u16 beacon_interval, const u8 *ie,
2134 struct cfg80211_non_tx_bss *non_tx_data,
2137 const u8 *mbssid_index_ie;
2138 const struct element *elem, *sub;
2140 u8 new_bssid[ETH_ALEN];
2141 u8 *new_ie, *profile;
2142 u64 seen_indices = 0;
2144 struct cfg80211_bss *bss;
2148 if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2150 if (!wiphy->support_mbssid)
2152 if (wiphy->support_only_he_mbssid &&
2153 !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2156 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2160 profile = kmalloc(ielen, gfp);
2164 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
2165 if (elem->datalen < 4)
2167 if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2169 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2172 if (sub->id != 0 || sub->datalen < 4) {
2173 /* not a valid BSS profile */
2177 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2178 sub->data[1] != 2) {
2179 /* The first element within the Nontransmitted
2180 * BSSID Profile is not the Nontransmitted
2181 * BSSID Capability element.
2186 memset(profile, 0, ielen);
2187 profile_len = cfg80211_merge_profile(ie, ielen,
2193 /* found a Nontransmitted BSSID Profile */
2194 mbssid_index_ie = cfg80211_find_ie
2195 (WLAN_EID_MULTI_BSSID_IDX,
2196 profile, profile_len);
2197 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2198 mbssid_index_ie[2] == 0 ||
2199 mbssid_index_ie[2] > 46) {
2200 /* No valid Multiple BSSID-Index element */
2204 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2205 /* We don't support legacy split of a profile */
2206 net_dbg_ratelimited("Partial info for BSSID index %d\n",
2207 mbssid_index_ie[2]);
2209 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2211 non_tx_data->bssid_index = mbssid_index_ie[2];
2212 non_tx_data->max_bssid_indicator = elem->data[0];
2214 cfg80211_gen_new_bssid(bssid,
2215 non_tx_data->max_bssid_indicator,
2216 non_tx_data->bssid_index,
2218 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2219 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
2221 profile_len, new_ie,
2226 capability = get_unaligned_le16(profile + 2);
2227 bss = cfg80211_inform_single_bss_data(wiphy, data,
2238 cfg80211_put_bss(wiphy, bss);
2247 struct cfg80211_bss *
2248 cfg80211_inform_bss_data(struct wiphy *wiphy,
2249 struct cfg80211_inform_bss *data,
2250 enum cfg80211_bss_frame_type ftype,
2251 const u8 *bssid, u64 tsf, u16 capability,
2252 u16 beacon_interval, const u8 *ie, size_t ielen,
2255 struct cfg80211_bss *res;
2256 struct cfg80211_non_tx_bss non_tx_data;
2258 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
2259 capability, beacon_interval, ie,
2263 non_tx_data.tx_bss = res;
2264 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
2265 beacon_interval, ie, ielen, &non_tx_data,
2269 EXPORT_SYMBOL(cfg80211_inform_bss_data);
2272 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
2273 struct cfg80211_inform_bss *data,
2274 struct ieee80211_mgmt *mgmt, size_t len,
2275 struct cfg80211_non_tx_bss *non_tx_data,
2278 enum cfg80211_bss_frame_type ftype;
2279 const u8 *ie = mgmt->u.probe_resp.variable;
2280 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2281 u.probe_resp.variable);
2283 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
2284 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
2286 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
2287 le64_to_cpu(mgmt->u.probe_resp.timestamp),
2288 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
2289 ie, ielen, non_tx_data, gfp);
2293 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
2294 struct cfg80211_bss *nontrans_bss,
2295 struct ieee80211_mgmt *mgmt, size_t len)
2297 u8 *ie, *new_ie, *pos;
2298 const struct element *nontrans_ssid;
2299 const u8 *trans_ssid, *mbssid;
2300 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2301 u.probe_resp.variable);
2303 struct cfg80211_bss_ies *new_ies;
2304 const struct cfg80211_bss_ies *old;
2307 lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
2309 ie = mgmt->u.probe_resp.variable;
2312 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
2315 new_ie_len -= trans_ssid[1];
2316 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
2318 * It's not valid to have the MBSSID element before SSID
2319 * ignore if that happens - the code below assumes it is
2320 * after (while copying things inbetween).
2322 if (!mbssid || mbssid < trans_ssid)
2324 new_ie_len -= mbssid[1];
2326 nontrans_ssid = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
2330 new_ie_len += nontrans_ssid->datalen;
2332 /* generate new ie for nontrans BSS
2333 * 1. replace SSID with nontrans BSS' SSID
2336 new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
2340 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
2346 /* copy the nontransmitted SSID */
2347 cpy_len = nontrans_ssid->datalen + 2;
2348 memcpy(pos, nontrans_ssid, cpy_len);
2350 /* copy the IEs between SSID and MBSSID */
2351 cpy_len = trans_ssid[1] + 2;
2352 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
2353 pos += (mbssid - (trans_ssid + cpy_len));
2354 /* copy the IEs after MBSSID */
2355 cpy_len = mbssid[1] + 2;
2356 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
2359 new_ies->len = new_ie_len;
2360 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2361 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
2362 memcpy(new_ies->data, new_ie, new_ie_len);
2363 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
2364 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
2365 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
2366 rcu_assign_pointer(nontrans_bss->ies, new_ies);
2368 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
2370 old = rcu_access_pointer(nontrans_bss->beacon_ies);
2371 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
2372 cfg80211_update_hidden_bsses(bss_from_pub(nontrans_bss),
2374 rcu_assign_pointer(nontrans_bss->ies, new_ies);
2376 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
2383 /* cfg80211_inform_bss_width_frame helper */
2384 static struct cfg80211_bss *
2385 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
2386 struct cfg80211_inform_bss *data,
2387 struct ieee80211_mgmt *mgmt, size_t len,
2390 struct cfg80211_internal_bss tmp = {}, *res;
2391 struct cfg80211_bss_ies *ies;
2392 struct ieee80211_channel *channel;
2394 struct ieee80211_ext *ext = NULL;
2395 u8 *bssid, *variable;
2396 u16 capability, beacon_int;
2397 size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
2398 u.probe_resp.variable);
2400 enum cfg80211_bss_frame_type ftype;
2402 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
2403 offsetof(struct ieee80211_mgmt, u.beacon.variable));
2405 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
2410 if (WARN_ON(!wiphy))
2413 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2414 (data->signal < 0 || data->signal > 100)))
2417 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2418 ext = (void *) mgmt;
2419 min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
2420 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2421 min_hdr_len = offsetof(struct ieee80211_ext,
2422 u.s1g_short_beacon.variable);
2425 if (WARN_ON(len < min_hdr_len))
2428 ielen = len - min_hdr_len;
2429 variable = mgmt->u.probe_resp.variable;
2431 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2432 variable = ext->u.s1g_short_beacon.variable;
2434 variable = ext->u.s1g_beacon.variable;
2437 if (ieee80211_is_beacon(mgmt->frame_control))
2438 ftype = CFG80211_BSS_FTYPE_BEACON;
2439 else if (ieee80211_is_probe_resp(mgmt->frame_control))
2440 ftype = CFG80211_BSS_FTYPE_PRESP;
2442 ftype = CFG80211_BSS_FTYPE_UNKNOWN;
2444 channel = cfg80211_get_bss_channel(wiphy, variable,
2445 ielen, data->chan, data->scan_width,
2451 const struct ieee80211_s1g_bcn_compat_ie *compat;
2452 const struct element *elem;
2454 elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
2458 if (elem->datalen < sizeof(*compat))
2460 compat = (void *)elem->data;
2461 bssid = ext->u.s1g_beacon.sa;
2462 capability = le16_to_cpu(compat->compat_info);
2463 beacon_int = le16_to_cpu(compat->beacon_int);
2465 bssid = mgmt->bssid;
2466 beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2467 capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
2470 ies = kzalloc(sizeof(*ies) + ielen, gfp);
2474 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2475 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
2476 ieee80211_is_s1g_beacon(mgmt->frame_control);
2477 memcpy(ies->data, variable, ielen);
2479 if (ieee80211_is_probe_resp(mgmt->frame_control))
2480 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2482 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2483 rcu_assign_pointer(tmp.pub.ies, ies);
2485 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2486 tmp.pub.beacon_interval = beacon_int;
2487 tmp.pub.capability = capability;
2488 tmp.pub.channel = channel;
2489 tmp.pub.scan_width = data->scan_width;
2490 tmp.pub.signal = data->signal;
2491 tmp.ts_boottime = data->boottime_ns;
2492 tmp.parent_tsf = data->parent_tsf;
2493 tmp.pub.chains = data->chains;
2494 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
2495 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2497 signal_valid = data->chan == channel;
2498 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
2503 if (channel->band == NL80211_BAND_60GHZ) {
2504 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2505 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2506 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2507 regulatory_hint_found_beacon(wiphy, channel, gfp);
2509 if (res->pub.capability & WLAN_CAPABILITY_ESS)
2510 regulatory_hint_found_beacon(wiphy, channel, gfp);
2513 trace_cfg80211_return_bss(&res->pub);
2514 /* cfg80211_bss_update gives us a referenced result */
2518 struct cfg80211_bss *
2519 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2520 struct cfg80211_inform_bss *data,
2521 struct ieee80211_mgmt *mgmt, size_t len,
2524 struct cfg80211_bss *res, *tmp_bss;
2525 const u8 *ie = mgmt->u.probe_resp.variable;
2526 const struct cfg80211_bss_ies *ies1, *ies2;
2527 size_t ielen = len - offsetof(struct ieee80211_mgmt,
2528 u.probe_resp.variable);
2529 struct cfg80211_non_tx_bss non_tx_data;
2531 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
2533 if (!res || !wiphy->support_mbssid ||
2534 !cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
2536 if (wiphy->support_only_he_mbssid &&
2537 !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
2540 non_tx_data.tx_bss = res;
2541 /* process each non-transmitting bss */
2542 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
2545 spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2547 /* check if the res has other nontransmitting bss which is not
2550 ies1 = rcu_access_pointer(res->ies);
2552 /* go through nontrans_list, if the timestamp of the BSS is
2553 * earlier than the timestamp of the transmitting BSS then
2556 list_for_each_entry(tmp_bss, &res->nontrans_list,
2558 ies2 = rcu_access_pointer(tmp_bss->ies);
2559 if (ies2->tsf < ies1->tsf)
2560 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
2563 spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2567 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2569 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2571 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2572 struct cfg80211_internal_bss *bss;
2577 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2579 spin_lock_bh(&rdev->bss_lock);
2580 bss_ref_get(rdev, bss);
2581 spin_unlock_bh(&rdev->bss_lock);
2583 EXPORT_SYMBOL(cfg80211_ref_bss);
2585 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2587 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2588 struct cfg80211_internal_bss *bss;
2593 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2595 spin_lock_bh(&rdev->bss_lock);
2596 bss_ref_put(rdev, bss);
2597 spin_unlock_bh(&rdev->bss_lock);
2599 EXPORT_SYMBOL(cfg80211_put_bss);
2601 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2603 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2604 struct cfg80211_internal_bss *bss, *tmp1;
2605 struct cfg80211_bss *nontrans_bss, *tmp;
2610 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2612 spin_lock_bh(&rdev->bss_lock);
2613 if (list_empty(&bss->list))
2616 list_for_each_entry_safe(nontrans_bss, tmp,
2617 &pub->nontrans_list,
2619 tmp1 = container_of(nontrans_bss,
2620 struct cfg80211_internal_bss, pub);
2621 if (__cfg80211_unlink_bss(rdev, tmp1))
2622 rdev->bss_generation++;
2625 if (__cfg80211_unlink_bss(rdev, bss))
2626 rdev->bss_generation++;
2628 spin_unlock_bh(&rdev->bss_lock);
2630 EXPORT_SYMBOL(cfg80211_unlink_bss);
2632 void cfg80211_bss_iter(struct wiphy *wiphy,
2633 struct cfg80211_chan_def *chandef,
2634 void (*iter)(struct wiphy *wiphy,
2635 struct cfg80211_bss *bss,
2639 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2640 struct cfg80211_internal_bss *bss;
2642 spin_lock_bh(&rdev->bss_lock);
2644 list_for_each_entry(bss, &rdev->bss_list, list) {
2645 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
2647 iter(wiphy, &bss->pub, iter_data);
2650 spin_unlock_bh(&rdev->bss_lock);
2652 EXPORT_SYMBOL(cfg80211_bss_iter);
2654 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2655 unsigned int link_id,
2656 struct ieee80211_channel *chan)
2658 struct wiphy *wiphy = wdev->wiphy;
2659 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2660 struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
2661 struct cfg80211_internal_bss *new = NULL;
2662 struct cfg80211_internal_bss *bss;
2663 struct cfg80211_bss *nontrans_bss;
2664 struct cfg80211_bss *tmp;
2666 spin_lock_bh(&rdev->bss_lock);
2669 * Some APs use CSA also for bandwidth changes, i.e., without actually
2670 * changing the control channel, so no need to update in such a case.
2672 if (cbss->pub.channel == chan)
2675 /* use transmitting bss */
2676 if (cbss->pub.transmitted_bss)
2677 cbss = container_of(cbss->pub.transmitted_bss,
2678 struct cfg80211_internal_bss,
2681 cbss->pub.channel = chan;
2683 list_for_each_entry(bss, &rdev->bss_list, list) {
2684 if (!cfg80211_bss_type_match(bss->pub.capability,
2685 bss->pub.channel->band,
2686 wdev->conn_bss_type))
2692 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2699 /* to save time, update IEs for transmitting bss only */
2700 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2701 new->pub.proberesp_ies = NULL;
2702 new->pub.beacon_ies = NULL;
2705 list_for_each_entry_safe(nontrans_bss, tmp,
2706 &new->pub.nontrans_list,
2708 bss = container_of(nontrans_bss,
2709 struct cfg80211_internal_bss, pub);
2710 if (__cfg80211_unlink_bss(rdev, bss))
2711 rdev->bss_generation++;
2714 WARN_ON(atomic_read(&new->hold));
2715 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2716 rdev->bss_generation++;
2719 rb_erase(&cbss->rbn, &rdev->bss_tree);
2720 rb_insert_bss(rdev, cbss);
2721 rdev->bss_generation++;
2723 list_for_each_entry_safe(nontrans_bss, tmp,
2724 &cbss->pub.nontrans_list,
2726 bss = container_of(nontrans_bss,
2727 struct cfg80211_internal_bss, pub);
2728 bss->pub.channel = chan;
2729 rb_erase(&bss->rbn, &rdev->bss_tree);
2730 rb_insert_bss(rdev, bss);
2731 rdev->bss_generation++;
2735 spin_unlock_bh(&rdev->bss_lock);
2738 #ifdef CONFIG_CFG80211_WEXT
2739 static struct cfg80211_registered_device *
2740 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2742 struct cfg80211_registered_device *rdev;
2743 struct net_device *dev;
2747 dev = dev_get_by_index(net, ifindex);
2749 return ERR_PTR(-ENODEV);
2750 if (dev->ieee80211_ptr)
2751 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2753 rdev = ERR_PTR(-ENODEV);
2758 int cfg80211_wext_siwscan(struct net_device *dev,
2759 struct iw_request_info *info,
2760 union iwreq_data *wrqu, char *extra)
2762 struct cfg80211_registered_device *rdev;
2763 struct wiphy *wiphy;
2764 struct iw_scan_req *wreq = NULL;
2765 struct cfg80211_scan_request *creq;
2766 int i, err, n_channels = 0;
2767 enum nl80211_band band;
2769 if (!netif_running(dev))
2772 if (wrqu->data.length == sizeof(struct iw_scan_req))
2773 wreq = (struct iw_scan_req *)extra;
2775 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2778 return PTR_ERR(rdev);
2780 if (rdev->scan_req || rdev->scan_msg)
2783 wiphy = &rdev->wiphy;
2785 /* Determine number of channels, needed to allocate creq */
2786 if (wreq && wreq->num_channels)
2787 n_channels = wreq->num_channels;
2789 n_channels = ieee80211_get_num_supported_channels(wiphy);
2791 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2792 n_channels * sizeof(void *),
2797 creq->wiphy = wiphy;
2798 creq->wdev = dev->ieee80211_ptr;
2799 /* SSIDs come after channels */
2800 creq->ssids = (void *)&creq->channels[n_channels];
2801 creq->n_channels = n_channels;
2803 creq->scan_start = jiffies;
2805 /* translate "Scan on frequencies" request */
2807 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2810 if (!wiphy->bands[band])
2813 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2814 /* ignore disabled channels */
2815 if (wiphy->bands[band]->channels[j].flags &
2816 IEEE80211_CHAN_DISABLED)
2819 /* If we have a wireless request structure and the
2820 * wireless request specifies frequencies, then search
2821 * for the matching hardware channel.
2823 if (wreq && wreq->num_channels) {
2825 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2826 for (k = 0; k < wreq->num_channels; k++) {
2827 struct iw_freq *freq =
2828 &wreq->channel_list[k];
2830 cfg80211_wext_freq(freq);
2832 if (wext_freq == wiphy_freq)
2833 goto wext_freq_found;
2835 goto wext_freq_not_found;
2839 creq->channels[i] = &wiphy->bands[band]->channels[j];
2841 wext_freq_not_found: ;
2844 /* No channels found? */
2850 /* Set real number of channels specified in creq->channels[] */
2851 creq->n_channels = i;
2853 /* translate "Scan for SSID" request */
2855 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2856 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2860 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2861 creq->ssids[0].ssid_len = wreq->essid_len;
2863 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2867 for (i = 0; i < NUM_NL80211_BANDS; i++)
2868 if (wiphy->bands[i])
2869 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2871 eth_broadcast_addr(creq->bssid);
2873 wiphy_lock(&rdev->wiphy);
2875 rdev->scan_req = creq;
2876 err = rdev_scan(rdev, creq);
2878 rdev->scan_req = NULL;
2879 /* creq will be freed below */
2881 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2882 /* creq now owned by driver */
2886 wiphy_unlock(&rdev->wiphy);
2891 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2893 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2894 const struct cfg80211_bss_ies *ies,
2895 char *current_ev, char *end_buf)
2897 const u8 *pos, *end, *next;
2898 struct iw_event iwe;
2904 * If needed, fragment the IEs buffer (at IE boundaries) into short
2905 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2908 end = pos + ies->len;
2910 while (end - pos > IW_GENERIC_IE_MAX) {
2911 next = pos + 2 + pos[1];
2912 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2913 next = next + 2 + next[1];
2915 memset(&iwe, 0, sizeof(iwe));
2916 iwe.cmd = IWEVGENIE;
2917 iwe.u.data.length = next - pos;
2918 current_ev = iwe_stream_add_point_check(info, current_ev,
2921 if (IS_ERR(current_ev))
2927 memset(&iwe, 0, sizeof(iwe));
2928 iwe.cmd = IWEVGENIE;
2929 iwe.u.data.length = end - pos;
2930 current_ev = iwe_stream_add_point_check(info, current_ev,
2933 if (IS_ERR(current_ev))
2941 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2942 struct cfg80211_internal_bss *bss, char *current_ev,
2945 const struct cfg80211_bss_ies *ies;
2946 struct iw_event iwe;
2951 bool ismesh = false;
2953 memset(&iwe, 0, sizeof(iwe));
2954 iwe.cmd = SIOCGIWAP;
2955 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2956 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2957 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2959 if (IS_ERR(current_ev))
2962 memset(&iwe, 0, sizeof(iwe));
2963 iwe.cmd = SIOCGIWFREQ;
2964 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2966 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2968 if (IS_ERR(current_ev))
2971 memset(&iwe, 0, sizeof(iwe));
2972 iwe.cmd = SIOCGIWFREQ;
2973 iwe.u.freq.m = bss->pub.channel->center_freq;
2975 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2977 if (IS_ERR(current_ev))
2980 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2981 memset(&iwe, 0, sizeof(iwe));
2983 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2984 IW_QUAL_NOISE_INVALID |
2985 IW_QUAL_QUAL_UPDATED;
2986 switch (wiphy->signal_type) {
2987 case CFG80211_SIGNAL_TYPE_MBM:
2988 sig = bss->pub.signal / 100;
2989 iwe.u.qual.level = sig;
2990 iwe.u.qual.updated |= IW_QUAL_DBM;
2991 if (sig < -110) /* rather bad */
2993 else if (sig > -40) /* perfect */
2995 /* will give a range of 0 .. 70 */
2996 iwe.u.qual.qual = sig + 110;
2998 case CFG80211_SIGNAL_TYPE_UNSPEC:
2999 iwe.u.qual.level = bss->pub.signal;
3000 /* will give range 0 .. 100 */
3001 iwe.u.qual.qual = bss->pub.signal;
3007 current_ev = iwe_stream_add_event_check(info, current_ev,
3010 if (IS_ERR(current_ev))
3014 memset(&iwe, 0, sizeof(iwe));
3015 iwe.cmd = SIOCGIWENCODE;
3016 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
3017 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3019 iwe.u.data.flags = IW_ENCODE_DISABLED;
3020 iwe.u.data.length = 0;
3021 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3023 if (IS_ERR(current_ev))
3027 ies = rcu_dereference(bss->pub.ies);
3033 if (ie[1] > rem - 2)
3038 memset(&iwe, 0, sizeof(iwe));
3039 iwe.cmd = SIOCGIWESSID;
3040 iwe.u.data.length = ie[1];
3041 iwe.u.data.flags = 1;
3042 current_ev = iwe_stream_add_point_check(info,
3046 if (IS_ERR(current_ev))
3049 case WLAN_EID_MESH_ID:
3050 memset(&iwe, 0, sizeof(iwe));
3051 iwe.cmd = SIOCGIWESSID;
3052 iwe.u.data.length = ie[1];
3053 iwe.u.data.flags = 1;
3054 current_ev = iwe_stream_add_point_check(info,
3058 if (IS_ERR(current_ev))
3061 case WLAN_EID_MESH_CONFIG:
3063 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
3066 memset(&iwe, 0, sizeof(iwe));
3067 iwe.cmd = IWEVCUSTOM;
3068 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
3070 iwe.u.data.length = strlen(buf);
3071 current_ev = iwe_stream_add_point_check(info,
3075 if (IS_ERR(current_ev))
3077 sprintf(buf, "Path Selection Metric ID: 0x%02X",
3079 iwe.u.data.length = strlen(buf);
3080 current_ev = iwe_stream_add_point_check(info,
3084 if (IS_ERR(current_ev))
3086 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
3088 iwe.u.data.length = strlen(buf);
3089 current_ev = iwe_stream_add_point_check(info,
3093 if (IS_ERR(current_ev))
3095 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
3096 iwe.u.data.length = strlen(buf);
3097 current_ev = iwe_stream_add_point_check(info,
3101 if (IS_ERR(current_ev))
3103 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
3104 iwe.u.data.length = strlen(buf);
3105 current_ev = iwe_stream_add_point_check(info,
3109 if (IS_ERR(current_ev))
3111 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
3112 iwe.u.data.length = strlen(buf);
3113 current_ev = iwe_stream_add_point_check(info,
3117 if (IS_ERR(current_ev))
3119 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
3120 iwe.u.data.length = strlen(buf);
3121 current_ev = iwe_stream_add_point_check(info,
3125 if (IS_ERR(current_ev))
3128 case WLAN_EID_SUPP_RATES:
3129 case WLAN_EID_EXT_SUPP_RATES:
3130 /* display all supported rates in readable format */
3131 p = current_ev + iwe_stream_lcp_len(info);
3133 memset(&iwe, 0, sizeof(iwe));
3134 iwe.cmd = SIOCGIWRATE;
3135 /* Those two flags are ignored... */
3136 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3138 for (i = 0; i < ie[1]; i++) {
3139 iwe.u.bitrate.value =
3140 ((ie[i + 2] & 0x7f) * 500000);
3142 p = iwe_stream_add_value(info, current_ev, p,
3146 current_ev = ERR_PTR(-E2BIG);
3157 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3159 memset(&iwe, 0, sizeof(iwe));
3160 iwe.cmd = SIOCGIWMODE;
3162 iwe.u.mode = IW_MODE_MESH;
3163 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3164 iwe.u.mode = IW_MODE_MASTER;
3166 iwe.u.mode = IW_MODE_ADHOC;
3167 current_ev = iwe_stream_add_event_check(info, current_ev,
3170 if (IS_ERR(current_ev))
3174 memset(&iwe, 0, sizeof(iwe));
3175 iwe.cmd = IWEVCUSTOM;
3176 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
3177 iwe.u.data.length = strlen(buf);
3178 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3180 if (IS_ERR(current_ev))
3182 memset(&iwe, 0, sizeof(iwe));
3183 iwe.cmd = IWEVCUSTOM;
3184 sprintf(buf, " Last beacon: %ums ago",
3185 elapsed_jiffies_msecs(bss->ts));
3186 iwe.u.data.length = strlen(buf);
3187 current_ev = iwe_stream_add_point_check(info, current_ev,
3188 end_buf, &iwe, buf);
3189 if (IS_ERR(current_ev))
3192 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3200 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3201 struct iw_request_info *info,
3202 char *buf, size_t len)
3204 char *current_ev = buf;
3205 char *end_buf = buf + len;
3206 struct cfg80211_internal_bss *bss;
3209 spin_lock_bh(&rdev->bss_lock);
3210 cfg80211_bss_expire(rdev);
3212 list_for_each_entry(bss, &rdev->bss_list, list) {
3213 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3217 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
3218 current_ev, end_buf);
3219 if (IS_ERR(current_ev)) {
3220 err = PTR_ERR(current_ev);
3224 spin_unlock_bh(&rdev->bss_lock);
3228 return current_ev - buf;
3232 int cfg80211_wext_giwscan(struct net_device *dev,
3233 struct iw_request_info *info,
3234 struct iw_point *data, char *extra)
3236 struct cfg80211_registered_device *rdev;
3239 if (!netif_running(dev))
3242 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3245 return PTR_ERR(rdev);
3247 if (rdev->scan_req || rdev->scan_msg)
3250 res = ieee80211_scan_results(rdev, info, extra, data->length);
3259 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);