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-2019 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>
18 #include <net/cfg80211.h>
19 #include <net/cfg80211-wext.h>
20 #include <net/iw_handler.h>
23 #include "wext-compat.h"
27 * DOC: BSS tree/list structure
29 * At the top level, the BSS list is kept in both a list in each
30 * registered device (@bss_list) as well as an RB-tree for faster
31 * lookup. In the RB-tree, entries can be looked up using their
32 * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
35 * Due to the possibility of hidden SSIDs, there's a second level
36 * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
37 * The hidden_list connects all BSSes belonging to a single AP
38 * that has a hidden SSID, and connects beacon and probe response
39 * entries. For a probe response entry for a hidden SSID, the
40 * hidden_beacon_bss pointer points to the BSS struct holding the
41 * beacon's information.
43 * Reference counting is done for all these references except for
44 * the hidden_list, so that a beacon BSS struct that is otherwise
45 * not referenced has one reference for being on the bss_list and
46 * one for each probe response entry that points to it using the
47 * hidden_beacon_bss pointer. When a BSS struct that has such a
48 * pointer is get/put, the refcount update is also propagated to
49 * the referenced struct, this ensure that it cannot get removed
50 * while somebody is using the probe response version.
52 * Note that the hidden_beacon_bss pointer never changes, due to
53 * the reference counting. Therefore, no locking is needed for
56 * Also note that the hidden_beacon_bss pointer is only relevant
57 * if the driver uses something other than the IEs, e.g. private
58 * data stored stored in the BSS struct, since the beacon IEs are
59 * also linked into the probe response struct.
63 * Limit the number of BSS entries stored in mac80211. Each one is
64 * a bit over 4k at most, so this limits to roughly 4-5M of memory.
65 * If somebody wants to really attack this though, they'd likely
66 * use small beacons, and only one type of frame, limiting each of
67 * the entries to a much smaller size (in order to generate more
68 * entries in total, so overhead is bigger.)
70 static int bss_entries_limit = 1000;
71 module_param(bss_entries_limit, int, 0644);
72 MODULE_PARM_DESC(bss_entries_limit,
73 "limit to number of scan BSS entries (per wiphy, default 1000)");
75 #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
77 static void bss_free(struct cfg80211_internal_bss *bss)
79 struct cfg80211_bss_ies *ies;
81 if (WARN_ON(atomic_read(&bss->hold)))
84 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
85 if (ies && !bss->pub.hidden_beacon_bss)
86 kfree_rcu(ies, rcu_head);
87 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
89 kfree_rcu(ies, rcu_head);
92 * This happens when the module is removed, it doesn't
93 * really matter any more save for completeness
95 if (!list_empty(&bss->hidden_list))
96 list_del(&bss->hidden_list);
101 static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
102 struct cfg80211_internal_bss *bss)
104 lockdep_assert_held(&rdev->bss_lock);
107 if (bss->pub.hidden_beacon_bss) {
108 bss = container_of(bss->pub.hidden_beacon_bss,
109 struct cfg80211_internal_bss,
113 if (bss->pub.transmitted_bss) {
114 bss = container_of(bss->pub.transmitted_bss,
115 struct cfg80211_internal_bss,
121 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
122 struct cfg80211_internal_bss *bss)
124 lockdep_assert_held(&rdev->bss_lock);
126 if (bss->pub.hidden_beacon_bss) {
127 struct cfg80211_internal_bss *hbss;
128 hbss = container_of(bss->pub.hidden_beacon_bss,
129 struct cfg80211_internal_bss,
132 if (hbss->refcount == 0)
136 if (bss->pub.transmitted_bss) {
137 struct cfg80211_internal_bss *tbss;
139 tbss = container_of(bss->pub.transmitted_bss,
140 struct cfg80211_internal_bss,
143 if (tbss->refcount == 0)
148 if (bss->refcount == 0)
152 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
153 struct cfg80211_internal_bss *bss)
155 lockdep_assert_held(&rdev->bss_lock);
157 if (!list_empty(&bss->hidden_list)) {
159 * don't remove the beacon entry if it has
160 * probe responses associated with it
162 if (!bss->pub.hidden_beacon_bss)
165 * if it's a probe response entry break its
166 * link to the other entries in the group
168 list_del_init(&bss->hidden_list);
171 list_del_init(&bss->list);
172 list_del_init(&bss->pub.nontrans_list);
173 rb_erase(&bss->rbn, &rdev->bss_tree);
175 WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
176 "rdev bss entries[%d]/list[empty:%d] corruption\n",
177 rdev->bss_entries, list_empty(&rdev->bss_list));
178 bss_ref_put(rdev, bss);
182 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
183 const u8 *subelement, size_t subie_len,
184 u8 *new_ie, gfp_t gfp)
187 const u8 *tmp_old, *tmp_new;
190 /* copy subelement as we need to change its content to
191 * mark an ie after it is processed.
193 sub_copy = kmalloc(subie_len, gfp);
196 memcpy(sub_copy, subelement, subie_len);
201 tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
203 memcpy(pos, tmp_new, tmp_new[1] + 2);
204 pos += (tmp_new[1] + 2);
207 /* go through IEs in ie (skip SSID) and subelement,
208 * merge them into new_ie
210 tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
211 tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
213 while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
214 if (tmp_old[0] == 0) {
219 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy, subie_len);
221 /* ie in old ie but not in subelement */
222 if (tmp_old[0] != WLAN_EID_MULTIPLE_BSSID) {
223 memcpy(pos, tmp_old, tmp_old[1] + 2);
224 pos += tmp_old[1] + 2;
227 /* ie in transmitting ie also in subelement,
228 * copy from subelement and flag the ie in subelement
229 * as copied (by setting eid field to 0xff). For
230 * vendor ie, compare OUI + type + subType to
231 * determine if they are the same ie.
233 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
234 if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
235 /* same vendor ie, copy from
238 memcpy(pos, tmp, tmp[1] + 2);
242 memcpy(pos, tmp_old, tmp_old[1] + 2);
243 pos += tmp_old[1] + 2;
246 /* copy ie from subelement into new ie */
247 memcpy(pos, tmp, tmp[1] + 2);
253 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
256 tmp_old += tmp_old[1] + 2;
259 /* go through subelement again to check if there is any ie not
260 * copied to new ie, skip ssid, capability, bssid-index ie
263 while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
264 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
265 tmp_new[0] == WLAN_EID_SSID ||
266 tmp_new[0] == WLAN_EID_MULTI_BSSID_IDX ||
267 tmp_new[0] == 0xff)) {
268 memcpy(pos, tmp_new, tmp_new[1] + 2);
269 pos += tmp_new[1] + 2;
271 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
273 tmp_new += tmp_new[1] + 2;
280 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
281 const u8 *ssid, size_t ssid_len)
283 const struct cfg80211_bss_ies *ies;
286 if (bssid && !ether_addr_equal(a->bssid, bssid))
292 ies = rcu_access_pointer(a->ies);
295 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
298 if (ssidie[1] != ssid_len)
300 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
304 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
305 struct cfg80211_bss *nontrans_bss)
309 struct cfg80211_bss *bss = NULL;
312 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
321 /* check if nontrans_bss is in the list */
322 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
323 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
327 /* add to the list */
328 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
332 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
333 unsigned long expire_time)
335 struct cfg80211_internal_bss *bss, *tmp;
336 bool expired = false;
338 lockdep_assert_held(&rdev->bss_lock);
340 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
341 if (atomic_read(&bss->hold))
343 if (!time_after(expire_time, bss->ts))
346 if (__cfg80211_unlink_bss(rdev, bss))
351 rdev->bss_generation++;
354 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
356 struct cfg80211_internal_bss *bss, *oldest = NULL;
359 lockdep_assert_held(&rdev->bss_lock);
361 list_for_each_entry(bss, &rdev->bss_list, list) {
362 if (atomic_read(&bss->hold))
365 if (!list_empty(&bss->hidden_list) &&
366 !bss->pub.hidden_beacon_bss)
369 if (oldest && time_before(oldest->ts, bss->ts))
374 if (WARN_ON(!oldest))
378 * The callers make sure to increase rdev->bss_generation if anything
379 * gets removed (and a new entry added), so there's no need to also do
383 ret = __cfg80211_unlink_bss(rdev, oldest);
388 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
391 struct cfg80211_scan_request *request;
392 struct wireless_dev *wdev;
394 #ifdef CONFIG_CFG80211_WEXT
395 union iwreq_data wrqu;
400 if (rdev->scan_msg) {
401 nl80211_send_scan_msg(rdev, rdev->scan_msg);
402 rdev->scan_msg = NULL;
406 request = rdev->scan_req;
410 wdev = request->wdev;
413 * This must be before sending the other events!
414 * Otherwise, wpa_supplicant gets completely confused with
418 cfg80211_sme_scan_done(wdev->netdev);
420 if (!request->info.aborted &&
421 request->flags & NL80211_SCAN_FLAG_FLUSH) {
422 /* flush entries from previous scans */
423 spin_lock_bh(&rdev->bss_lock);
424 __cfg80211_bss_expire(rdev, request->scan_start);
425 spin_unlock_bh(&rdev->bss_lock);
428 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
430 #ifdef CONFIG_CFG80211_WEXT
431 if (wdev->netdev && !request->info.aborted) {
432 memset(&wrqu, 0, sizeof(wrqu));
434 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
439 dev_put(wdev->netdev);
441 rdev->scan_req = NULL;
445 rdev->scan_msg = msg;
447 nl80211_send_scan_msg(rdev, msg);
450 void __cfg80211_scan_done(struct work_struct *wk)
452 struct cfg80211_registered_device *rdev;
454 rdev = container_of(wk, struct cfg80211_registered_device,
458 ___cfg80211_scan_done(rdev, true);
462 void cfg80211_scan_done(struct cfg80211_scan_request *request,
463 struct cfg80211_scan_info *info)
465 trace_cfg80211_scan_done(request, info);
466 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
468 request->info = *info;
469 request->notified = true;
470 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
472 EXPORT_SYMBOL(cfg80211_scan_done);
474 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
475 struct cfg80211_sched_scan_request *req)
479 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
482 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
483 struct cfg80211_sched_scan_request *req)
487 list_del_rcu(&req->list);
488 kfree_rcu(req, rcu_head);
491 static struct cfg80211_sched_scan_request *
492 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
494 struct cfg80211_sched_scan_request *pos;
496 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
498 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
499 if (pos->reqid == reqid)
506 * Determines if a scheduled scan request can be handled. When a legacy
507 * scheduled scan is running no other scheduled scan is allowed regardless
508 * whether the request is for legacy or multi-support scan. When a multi-support
509 * scheduled scan is running a request for legacy scan is not allowed. In this
510 * case a request for multi-support scan can be handled if resources are
511 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
513 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
516 struct cfg80211_sched_scan_request *pos;
519 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
520 /* request id zero means legacy in progress */
521 if (!i && !pos->reqid)
527 /* no legacy allowed when multi request(s) are active */
531 /* resource limit reached */
532 if (i == rdev->wiphy.max_sched_scan_reqs)
538 void cfg80211_sched_scan_results_wk(struct work_struct *work)
540 struct cfg80211_registered_device *rdev;
541 struct cfg80211_sched_scan_request *req, *tmp;
543 rdev = container_of(work, struct cfg80211_registered_device,
547 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
548 if (req->report_results) {
549 req->report_results = false;
550 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
551 /* flush entries from previous scans */
552 spin_lock_bh(&rdev->bss_lock);
553 __cfg80211_bss_expire(rdev, req->scan_start);
554 spin_unlock_bh(&rdev->bss_lock);
555 req->scan_start = jiffies;
557 nl80211_send_sched_scan(req,
558 NL80211_CMD_SCHED_SCAN_RESULTS);
564 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
566 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
567 struct cfg80211_sched_scan_request *request;
569 trace_cfg80211_sched_scan_results(wiphy, reqid);
570 /* ignore if we're not scanning */
573 request = cfg80211_find_sched_scan_req(rdev, reqid);
575 request->report_results = true;
576 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
580 EXPORT_SYMBOL(cfg80211_sched_scan_results);
582 void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
584 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
588 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
590 __cfg80211_stop_sched_scan(rdev, reqid, true);
592 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
594 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
597 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
600 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
602 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
603 struct cfg80211_sched_scan_request *req,
604 bool driver_initiated)
608 if (!driver_initiated) {
609 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
614 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
616 cfg80211_del_sched_scan_req(rdev, req);
621 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
622 u64 reqid, bool driver_initiated)
624 struct cfg80211_sched_scan_request *sched_scan_req;
628 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
632 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
636 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
637 unsigned long age_secs)
639 struct cfg80211_internal_bss *bss;
640 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
642 spin_lock_bh(&rdev->bss_lock);
643 list_for_each_entry(bss, &rdev->bss_list, list)
644 bss->ts -= age_jiffies;
645 spin_unlock_bh(&rdev->bss_lock);
648 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
650 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
653 const struct element *
654 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
655 const u8 *match, unsigned int match_len,
656 unsigned int match_offset)
658 const struct element *elem;
660 for_each_element_id(elem, eid, ies, len) {
661 if (elem->datalen >= match_offset + match_len &&
662 !memcmp(elem->data + match_offset, match, match_len))
668 EXPORT_SYMBOL(cfg80211_find_elem_match);
670 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
674 const struct element *elem;
675 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
676 int match_len = (oui_type < 0) ? 3 : sizeof(match);
678 if (WARN_ON(oui_type > 0xff))
681 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
682 match, match_len, 0);
684 if (!elem || elem->datalen < 4)
689 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
692 * enum bss_compare_mode - BSS compare mode
693 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
694 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
695 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
697 enum bss_compare_mode {
703 static int cmp_bss(struct cfg80211_bss *a,
704 struct cfg80211_bss *b,
705 enum bss_compare_mode mode)
707 const struct cfg80211_bss_ies *a_ies, *b_ies;
708 const u8 *ie1 = NULL;
709 const u8 *ie2 = NULL;
712 if (a->channel != b->channel)
713 return b->channel->center_freq - a->channel->center_freq;
715 a_ies = rcu_access_pointer(a->ies);
718 b_ies = rcu_access_pointer(b->ies);
722 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
723 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
724 a_ies->data, a_ies->len);
725 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
726 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
727 b_ies->data, b_ies->len);
731 if (ie1[1] == ie2[1])
732 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
734 mesh_id_cmp = ie2[1] - ie1[1];
736 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
737 a_ies->data, a_ies->len);
738 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
739 b_ies->data, b_ies->len);
743 if (ie1[1] != ie2[1])
744 return ie2[1] - ie1[1];
745 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
749 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
753 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
754 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
760 * Note that with "hide_ssid", the function returns a match if
761 * the already-present BSS ("b") is a hidden SSID beacon for
765 /* sort missing IE before (left of) present IE */
772 case BSS_CMP_HIDE_ZLEN:
774 * In ZLEN mode we assume the BSS entry we're
775 * looking for has a zero-length SSID. So if
776 * the one we're looking at right now has that,
777 * return 0. Otherwise, return the difference
778 * in length, but since we're looking for the
779 * 0-length it's really equivalent to returning
780 * the length of the one we're looking at.
782 * No content comparison is needed as we assume
783 * the content length is zero.
786 case BSS_CMP_REGULAR:
788 /* sort by length first, then by contents */
789 if (ie1[1] != ie2[1])
790 return ie2[1] - ie1[1];
791 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
792 case BSS_CMP_HIDE_NUL:
793 if (ie1[1] != ie2[1])
794 return ie2[1] - ie1[1];
795 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
796 for (i = 0; i < ie2[1]; i++)
803 static bool cfg80211_bss_type_match(u16 capability,
804 enum nl80211_band band,
805 enum ieee80211_bss_type bss_type)
810 if (bss_type == IEEE80211_BSS_TYPE_ANY)
813 if (band == NL80211_BAND_60GHZ) {
814 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
816 case IEEE80211_BSS_TYPE_ESS:
817 val = WLAN_CAPABILITY_DMG_TYPE_AP;
819 case IEEE80211_BSS_TYPE_PBSS:
820 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
822 case IEEE80211_BSS_TYPE_IBSS:
823 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
829 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
831 case IEEE80211_BSS_TYPE_ESS:
832 val = WLAN_CAPABILITY_ESS;
834 case IEEE80211_BSS_TYPE_IBSS:
835 val = WLAN_CAPABILITY_IBSS;
837 case IEEE80211_BSS_TYPE_MBSS:
845 ret = ((capability & mask) == val);
849 /* Returned bss is reference counted and must be cleaned up appropriately. */
850 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
851 struct ieee80211_channel *channel,
853 const u8 *ssid, size_t ssid_len,
854 enum ieee80211_bss_type bss_type,
855 enum ieee80211_privacy privacy)
857 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
858 struct cfg80211_internal_bss *bss, *res = NULL;
859 unsigned long now = jiffies;
862 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
865 spin_lock_bh(&rdev->bss_lock);
867 list_for_each_entry(bss, &rdev->bss_list, list) {
868 if (!cfg80211_bss_type_match(bss->pub.capability,
869 bss->pub.channel->band, bss_type))
872 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
873 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
874 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
876 if (channel && bss->pub.channel != channel)
878 if (!is_valid_ether_addr(bss->pub.bssid))
880 /* Don't get expired BSS structs */
881 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
882 !atomic_read(&bss->hold))
884 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
886 bss_ref_get(rdev, res);
891 spin_unlock_bh(&rdev->bss_lock);
894 trace_cfg80211_return_bss(&res->pub);
897 EXPORT_SYMBOL(cfg80211_get_bss);
899 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
900 struct cfg80211_internal_bss *bss)
902 struct rb_node **p = &rdev->bss_tree.rb_node;
903 struct rb_node *parent = NULL;
904 struct cfg80211_internal_bss *tbss;
909 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
911 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
914 /* will sort of leak this BSS */
924 rb_link_node(&bss->rbn, parent, p);
925 rb_insert_color(&bss->rbn, &rdev->bss_tree);
928 static struct cfg80211_internal_bss *
929 rb_find_bss(struct cfg80211_registered_device *rdev,
930 struct cfg80211_internal_bss *res,
931 enum bss_compare_mode mode)
933 struct rb_node *n = rdev->bss_tree.rb_node;
934 struct cfg80211_internal_bss *bss;
938 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
939 r = cmp_bss(&res->pub, &bss->pub, mode);
952 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
953 struct cfg80211_internal_bss *new)
955 const struct cfg80211_bss_ies *ies;
956 struct cfg80211_internal_bss *bss;
962 ies = rcu_access_pointer(new->pub.beacon_ies);
966 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
973 for (i = 0; i < ssidlen; i++)
977 /* not a hidden SSID */
981 /* This is the bad part ... */
983 list_for_each_entry(bss, &rdev->bss_list, list) {
985 * we're iterating all the entries anyway, so take the
986 * opportunity to validate the list length accounting
990 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
992 if (bss->pub.channel != new->pub.channel)
994 if (bss->pub.scan_width != new->pub.scan_width)
996 if (rcu_access_pointer(bss->pub.beacon_ies))
998 ies = rcu_access_pointer(bss->pub.ies);
1001 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1004 if (ssidlen && ie[1] != ssidlen)
1006 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1008 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1009 list_del(&bss->hidden_list);
1011 list_add(&bss->hidden_list, &new->hidden_list);
1012 bss->pub.hidden_beacon_bss = &new->pub;
1013 new->refcount += bss->refcount;
1014 rcu_assign_pointer(bss->pub.beacon_ies,
1015 new->pub.beacon_ies);
1018 WARN_ONCE(n_entries != rdev->bss_entries,
1019 "rdev bss entries[%d]/list[len:%d] corruption\n",
1020 rdev->bss_entries, n_entries);
1025 /* Returned bss is reference counted and must be cleaned up appropriately. */
1026 static struct cfg80211_internal_bss *
1027 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1028 struct cfg80211_internal_bss *tmp,
1029 struct cfg80211_bss *trans_bss,
1032 struct cfg80211_internal_bss *found = NULL;
1034 if (WARN_ON(!tmp->pub.channel))
1039 spin_lock_bh(&rdev->bss_lock);
1041 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1042 spin_unlock_bh(&rdev->bss_lock);
1046 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1050 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1051 const struct cfg80211_bss_ies *old;
1053 old = rcu_access_pointer(found->pub.proberesp_ies);
1055 rcu_assign_pointer(found->pub.proberesp_ies,
1056 tmp->pub.proberesp_ies);
1057 /* Override possible earlier Beacon frame IEs */
1058 rcu_assign_pointer(found->pub.ies,
1059 tmp->pub.proberesp_ies);
1061 kfree_rcu((struct cfg80211_bss_ies *)old,
1063 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
1064 const struct cfg80211_bss_ies *old;
1065 struct cfg80211_internal_bss *bss;
1067 if (found->pub.hidden_beacon_bss &&
1068 !list_empty(&found->hidden_list)) {
1069 const struct cfg80211_bss_ies *f;
1072 * The found BSS struct is one of the probe
1073 * response members of a group, but we're
1074 * receiving a beacon (beacon_ies in the tmp
1075 * bss is used). This can only mean that the
1076 * AP changed its beacon from not having an
1077 * SSID to showing it, which is confusing so
1078 * drop this information.
1081 f = rcu_access_pointer(tmp->pub.beacon_ies);
1082 kfree_rcu((struct cfg80211_bss_ies *)f,
1087 old = rcu_access_pointer(found->pub.beacon_ies);
1089 rcu_assign_pointer(found->pub.beacon_ies,
1090 tmp->pub.beacon_ies);
1092 /* Override IEs if they were from a beacon before */
1093 if (old == rcu_access_pointer(found->pub.ies))
1094 rcu_assign_pointer(found->pub.ies,
1095 tmp->pub.beacon_ies);
1097 /* Assign beacon IEs to all sub entries */
1098 list_for_each_entry(bss, &found->hidden_list,
1100 const struct cfg80211_bss_ies *ies;
1102 ies = rcu_access_pointer(bss->pub.beacon_ies);
1103 WARN_ON(ies != old);
1105 rcu_assign_pointer(bss->pub.beacon_ies,
1106 tmp->pub.beacon_ies);
1110 kfree_rcu((struct cfg80211_bss_ies *)old,
1114 found->pub.beacon_interval = tmp->pub.beacon_interval;
1116 * don't update the signal if beacon was heard on
1120 found->pub.signal = tmp->pub.signal;
1121 found->pub.capability = tmp->pub.capability;
1122 found->ts = tmp->ts;
1123 found->ts_boottime = tmp->ts_boottime;
1124 found->parent_tsf = tmp->parent_tsf;
1125 found->pub.chains = tmp->pub.chains;
1126 memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
1127 IEEE80211_MAX_CHAINS);
1128 ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
1130 struct cfg80211_internal_bss *new;
1131 struct cfg80211_internal_bss *hidden;
1132 struct cfg80211_bss_ies *ies;
1135 * create a copy -- the "res" variable that is passed in
1136 * is allocated on the stack since it's not needed in the
1137 * more common case of an update
1139 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1142 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1144 kfree_rcu(ies, rcu_head);
1145 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1147 kfree_rcu(ies, rcu_head);
1150 memcpy(new, tmp, sizeof(*new));
1152 INIT_LIST_HEAD(&new->hidden_list);
1153 INIT_LIST_HEAD(&new->pub.nontrans_list);
1155 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1156 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1158 hidden = rb_find_bss(rdev, tmp,
1161 new->pub.hidden_beacon_bss = &hidden->pub;
1162 list_add(&new->hidden_list,
1163 &hidden->hidden_list);
1165 rcu_assign_pointer(new->pub.beacon_ies,
1166 hidden->pub.beacon_ies);
1170 * Ok so we found a beacon, and don't have an entry. If
1171 * it's a beacon with hidden SSID, we might be in for an
1172 * expensive search for any probe responses that should
1173 * be grouped with this beacon for updates ...
1175 if (!cfg80211_combine_bsses(rdev, new)) {
1181 if (rdev->bss_entries >= bss_entries_limit &&
1182 !cfg80211_bss_expire_oldest(rdev)) {
1187 /* This must be before the call to bss_ref_get */
1189 struct cfg80211_internal_bss *pbss =
1190 container_of(trans_bss,
1191 struct cfg80211_internal_bss,
1194 new->pub.transmitted_bss = trans_bss;
1195 bss_ref_get(rdev, pbss);
1198 list_add_tail(&new->list, &rdev->bss_list);
1199 rdev->bss_entries++;
1200 rb_insert_bss(rdev, new);
1204 rdev->bss_generation++;
1205 bss_ref_get(rdev, found);
1206 spin_unlock_bh(&rdev->bss_lock);
1210 spin_unlock_bh(&rdev->bss_lock);
1215 * Update RX channel information based on the available frame payload
1216 * information. This is mainly for the 2.4 GHz band where frames can be received
1217 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1218 * element to indicate the current (transmitting) channel, but this might also
1219 * be needed on other bands if RX frequency does not match with the actual
1220 * operating channel of a BSS.
1222 static struct ieee80211_channel *
1223 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1224 struct ieee80211_channel *channel,
1225 enum nl80211_bss_scan_width scan_width)
1229 int channel_number = -1;
1230 struct ieee80211_channel *alt_channel;
1232 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1233 if (tmp && tmp[1] == 1) {
1234 channel_number = tmp[2];
1236 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1237 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1238 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1240 channel_number = htop->primary_chan;
1244 if (channel_number < 0) {
1245 /* No channel information in frame payload */
1249 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
1250 alt_channel = ieee80211_get_channel(wiphy, freq);
1252 if (channel->band == NL80211_BAND_2GHZ) {
1254 * Better not allow unexpected channels when that could
1255 * be going beyond the 1-11 range (e.g., discovering
1256 * BSS on channel 12 when radio is configured for
1262 /* No match for the payload channel number - ignore it */
1266 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1267 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1269 * Ignore channel number in 5 and 10 MHz channels where there
1270 * may not be an n:1 or 1:n mapping between frequencies and
1277 * Use the channel determined through the payload channel number
1278 * instead of the RX channel reported by the driver.
1280 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1285 /* Returned bss is reference counted and must be cleaned up appropriately. */
1286 static struct cfg80211_bss *
1287 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1288 struct cfg80211_inform_bss *data,
1289 enum cfg80211_bss_frame_type ftype,
1290 const u8 *bssid, u64 tsf, u16 capability,
1291 u16 beacon_interval, const u8 *ie, size_t ielen,
1292 struct cfg80211_bss *trans_bss,
1295 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1296 struct cfg80211_bss_ies *ies;
1297 struct ieee80211_channel *channel;
1298 struct cfg80211_internal_bss tmp = {}, *res;
1302 if (WARN_ON(!wiphy))
1305 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1306 (data->signal < 0 || data->signal > 100)))
1309 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1314 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1315 tmp.pub.channel = channel;
1316 tmp.pub.scan_width = data->scan_width;
1317 tmp.pub.signal = data->signal;
1318 tmp.pub.beacon_interval = beacon_interval;
1319 tmp.pub.capability = capability;
1320 tmp.ts_boottime = data->boottime_ns;
1323 * If we do not know here whether the IEs are from a Beacon or Probe
1324 * Response frame, we need to pick one of the options and only use it
1325 * with the driver that does not provide the full Beacon/Probe Response
1326 * frame. Use Beacon frame pointer to avoid indicating that this should
1327 * override the IEs pointer should we have received an earlier
1328 * indication of Probe Response data.
1330 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1335 ies->from_beacon = false;
1336 memcpy(ies->data, ie, ielen);
1339 case CFG80211_BSS_FTYPE_BEACON:
1340 ies->from_beacon = true;
1342 case CFG80211_BSS_FTYPE_UNKNOWN:
1343 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1345 case CFG80211_BSS_FTYPE_PRESP:
1346 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1349 rcu_assign_pointer(tmp.pub.ies, ies);
1351 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1352 wiphy->max_adj_channel_rssi_comp;
1353 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, trans_bss,
1358 if (channel->band == NL80211_BAND_60GHZ) {
1359 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1360 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1361 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1362 regulatory_hint_found_beacon(wiphy, channel, gfp);
1364 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1365 regulatory_hint_found_beacon(wiphy, channel, gfp);
1369 /* this is a nontransmitting bss, we need to add it to
1370 * transmitting bss' list if it is not there
1372 if (cfg80211_add_nontrans_list(trans_bss, &res->pub)) {
1373 if (__cfg80211_unlink_bss(rdev, res))
1374 rdev->bss_generation++;
1378 trace_cfg80211_return_bss(&res->pub);
1379 /* cfg80211_bss_update gives us a referenced result */
1383 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1384 struct cfg80211_inform_bss *data,
1385 enum cfg80211_bss_frame_type ftype,
1386 const u8 *bssid, u64 tsf,
1387 u16 beacon_interval, const u8 *ie,
1389 struct cfg80211_bss *trans_bss,
1392 const u8 *mbssid_index_ie;
1393 const struct element *elem, *sub;
1395 u8 new_bssid[ETH_ALEN];
1398 struct cfg80211_bss *bss;
1402 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1404 if (!wiphy->support_mbssid)
1406 if (wiphy->support_only_he_mbssid &&
1407 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1410 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1414 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1415 if (elem->datalen < 4)
1417 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1418 if (sub->id != 0 || sub->datalen < 4) {
1419 /* not a valid BSS profile */
1423 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1424 sub->data[1] != 2) {
1425 /* The first element within the Nontransmitted
1426 * BSSID Profile is not the Nontransmitted
1427 * BSSID Capability element.
1432 /* found a Nontransmitted BSSID Profile */
1433 mbssid_index_ie = cfg80211_find_ie
1434 (WLAN_EID_MULTI_BSSID_IDX,
1435 sub->data, sub->datalen);
1436 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1437 mbssid_index_ie[2] == 0) {
1438 /* No valid Multiple BSSID-Index element */
1442 cfg80211_gen_new_bssid(bssid, elem->data[0],
1445 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
1446 new_ie_len = cfg80211_gen_new_ie(ie, ielen, sub->data,
1447 sub->datalen, new_ie,
1452 capability = get_unaligned_le16(sub->data + 2);
1453 bss = cfg80211_inform_single_bss_data(wiphy, data,
1463 cfg80211_put_bss(wiphy, bss);
1470 struct cfg80211_bss *
1471 cfg80211_inform_bss_data(struct wiphy *wiphy,
1472 struct cfg80211_inform_bss *data,
1473 enum cfg80211_bss_frame_type ftype,
1474 const u8 *bssid, u64 tsf, u16 capability,
1475 u16 beacon_interval, const u8 *ie, size_t ielen,
1478 struct cfg80211_bss *res;
1480 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1481 capability, beacon_interval, ie,
1483 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
1484 beacon_interval, ie, ielen, res, gfp);
1487 EXPORT_SYMBOL(cfg80211_inform_bss_data);
1490 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1491 struct cfg80211_inform_bss *data,
1492 struct ieee80211_mgmt *mgmt, size_t len,
1493 struct cfg80211_bss *trans_bss,
1496 enum cfg80211_bss_frame_type ftype;
1497 const u8 *ie = mgmt->u.probe_resp.variable;
1498 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1499 u.probe_resp.variable);
1501 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1502 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1504 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1505 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1506 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
1507 ie, ielen, trans_bss, gfp);
1511 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
1512 struct cfg80211_bss *nontrans_bss,
1513 struct ieee80211_mgmt *mgmt, size_t len,
1516 u8 *ie, *new_ie, *pos;
1517 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1518 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1519 u.probe_resp.variable);
1521 struct cfg80211_bss_ies *new_ies;
1522 const struct cfg80211_bss_ies *old;
1525 ie = mgmt->u.probe_resp.variable;
1528 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1531 new_ie_len -= trans_ssid[1];
1532 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1535 new_ie_len -= mbssid[1];
1537 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
1538 if (!nontrans_ssid) {
1542 new_ie_len += nontrans_ssid[1];
1545 /* generate new ie for nontrans BSS
1546 * 1. replace SSID with nontrans BSS' SSID
1549 new_ie = kzalloc(new_ie_len, gfp);
1552 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp);
1560 /* copy the nontransmitted SSID */
1561 cpy_len = nontrans_ssid[1] + 2;
1562 memcpy(pos, nontrans_ssid, cpy_len);
1564 /* copy the IEs between SSID and MBSSID */
1565 cpy_len = trans_ssid[1] + 2;
1566 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1567 pos += (mbssid - (trans_ssid + cpy_len));
1568 /* copy the IEs after MBSSID */
1569 cpy_len = mbssid[1] + 2;
1570 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1573 new_ies->len = new_ie_len;
1574 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1575 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1576 memcpy(new_ies->data, new_ie, new_ie_len);
1577 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1578 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1579 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1580 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1582 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1584 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1585 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1586 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1588 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1592 /* cfg80211_inform_bss_width_frame helper */
1593 static struct cfg80211_bss *
1594 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1595 struct cfg80211_inform_bss *data,
1596 struct ieee80211_mgmt *mgmt, size_t len,
1597 struct cfg80211_bss *trans_bss,
1600 struct cfg80211_internal_bss tmp = {}, *res;
1601 struct cfg80211_bss_ies *ies;
1602 struct ieee80211_channel *channel;
1604 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1605 u.probe_resp.variable);
1608 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1609 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1611 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
1616 if (WARN_ON(!wiphy))
1619 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1620 (data->signal < 0 || data->signal > 100)))
1623 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
1626 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
1627 ielen, data->chan, data->scan_width);
1631 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1635 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1636 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1637 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
1639 if (ieee80211_is_probe_resp(mgmt->frame_control))
1640 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1642 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1643 rcu_assign_pointer(tmp.pub.ies, ies);
1645 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1646 tmp.pub.channel = channel;
1647 tmp.pub.scan_width = data->scan_width;
1648 tmp.pub.signal = data->signal;
1649 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1650 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
1651 tmp.ts_boottime = data->boottime_ns;
1652 tmp.parent_tsf = data->parent_tsf;
1653 tmp.pub.chains = data->chains;
1654 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1655 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1657 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1658 wiphy->max_adj_channel_rssi_comp;
1659 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, trans_bss,
1664 if (channel->band == NL80211_BAND_60GHZ) {
1665 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1666 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1667 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1668 regulatory_hint_found_beacon(wiphy, channel, gfp);
1670 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1671 regulatory_hint_found_beacon(wiphy, channel, gfp);
1674 trace_cfg80211_return_bss(&res->pub);
1675 /* cfg80211_bss_update gives us a referenced result */
1679 struct cfg80211_bss *
1680 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1681 struct cfg80211_inform_bss *data,
1682 struct ieee80211_mgmt *mgmt, size_t len,
1685 struct cfg80211_bss *res, *tmp_bss;
1686 const u8 *ie = mgmt->u.probe_resp.variable;
1687 const struct cfg80211_bss_ies *ies1, *ies2;
1688 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1689 u.probe_resp.variable);
1691 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1693 if (!res || !wiphy->support_mbssid ||
1694 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1696 if (wiphy->support_only_he_mbssid &&
1697 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1700 /* process each non-transmitting bss */
1701 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len, res, gfp);
1703 /* check if the res has other nontransmitting bss which is not
1706 ies1 = rcu_access_pointer(res->ies);
1708 /* go through nontrans_list, if the timestamp of the BSS is
1709 * earlier than the timestamp of the transmitting BSS then
1712 list_for_each_entry(tmp_bss, &res->nontrans_list,
1714 ies2 = rcu_access_pointer(tmp_bss->ies);
1715 if (ies2->tsf < ies1->tsf)
1716 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1722 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
1724 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1726 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1727 struct cfg80211_internal_bss *bss;
1732 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1734 spin_lock_bh(&rdev->bss_lock);
1735 bss_ref_get(rdev, bss);
1736 spin_unlock_bh(&rdev->bss_lock);
1738 EXPORT_SYMBOL(cfg80211_ref_bss);
1740 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1742 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1743 struct cfg80211_internal_bss *bss;
1748 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1750 spin_lock_bh(&rdev->bss_lock);
1751 bss_ref_put(rdev, bss);
1752 spin_unlock_bh(&rdev->bss_lock);
1754 EXPORT_SYMBOL(cfg80211_put_bss);
1756 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1758 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1759 struct cfg80211_internal_bss *bss, *tmp1;
1760 struct cfg80211_bss *nontrans_bss, *tmp;
1765 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1767 spin_lock_bh(&rdev->bss_lock);
1768 if (list_empty(&bss->list))
1771 list_for_each_entry_safe(nontrans_bss, tmp,
1772 &pub->nontrans_list,
1774 tmp1 = container_of(nontrans_bss,
1775 struct cfg80211_internal_bss, pub);
1776 if (__cfg80211_unlink_bss(rdev, tmp1))
1777 rdev->bss_generation++;
1780 if (__cfg80211_unlink_bss(rdev, bss))
1781 rdev->bss_generation++;
1783 spin_unlock_bh(&rdev->bss_lock);
1785 EXPORT_SYMBOL(cfg80211_unlink_bss);
1787 #ifdef CONFIG_CFG80211_WEXT
1788 static struct cfg80211_registered_device *
1789 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1791 struct cfg80211_registered_device *rdev;
1792 struct net_device *dev;
1796 dev = dev_get_by_index(net, ifindex);
1798 return ERR_PTR(-ENODEV);
1799 if (dev->ieee80211_ptr)
1800 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
1802 rdev = ERR_PTR(-ENODEV);
1807 int cfg80211_wext_siwscan(struct net_device *dev,
1808 struct iw_request_info *info,
1809 union iwreq_data *wrqu, char *extra)
1811 struct cfg80211_registered_device *rdev;
1812 struct wiphy *wiphy;
1813 struct iw_scan_req *wreq = NULL;
1814 struct cfg80211_scan_request *creq = NULL;
1815 int i, err, n_channels = 0;
1816 enum nl80211_band band;
1818 if (!netif_running(dev))
1821 if (wrqu->data.length == sizeof(struct iw_scan_req))
1822 wreq = (struct iw_scan_req *)extra;
1824 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1827 return PTR_ERR(rdev);
1829 if (rdev->scan_req || rdev->scan_msg) {
1834 wiphy = &rdev->wiphy;
1836 /* Determine number of channels, needed to allocate creq */
1837 if (wreq && wreq->num_channels)
1838 n_channels = wreq->num_channels;
1840 n_channels = ieee80211_get_num_supported_channels(wiphy);
1842 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1843 n_channels * sizeof(void *),
1850 creq->wiphy = wiphy;
1851 creq->wdev = dev->ieee80211_ptr;
1852 /* SSIDs come after channels */
1853 creq->ssids = (void *)&creq->channels[n_channels];
1854 creq->n_channels = n_channels;
1856 creq->scan_start = jiffies;
1858 /* translate "Scan on frequencies" request */
1860 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1863 if (!wiphy->bands[band])
1866 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1867 /* ignore disabled channels */
1868 if (wiphy->bands[band]->channels[j].flags &
1869 IEEE80211_CHAN_DISABLED)
1872 /* If we have a wireless request structure and the
1873 * wireless request specifies frequencies, then search
1874 * for the matching hardware channel.
1876 if (wreq && wreq->num_channels) {
1878 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1879 for (k = 0; k < wreq->num_channels; k++) {
1880 struct iw_freq *freq =
1881 &wreq->channel_list[k];
1883 cfg80211_wext_freq(freq);
1885 if (wext_freq == wiphy_freq)
1886 goto wext_freq_found;
1888 goto wext_freq_not_found;
1892 creq->channels[i] = &wiphy->bands[band]->channels[j];
1894 wext_freq_not_found: ;
1897 /* No channels found? */
1903 /* Set real number of channels specified in creq->channels[] */
1904 creq->n_channels = i;
1906 /* translate "Scan for SSID" request */
1908 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1909 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1913 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1914 creq->ssids[0].ssid_len = wreq->essid_len;
1916 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1920 for (i = 0; i < NUM_NL80211_BANDS; i++)
1921 if (wiphy->bands[i])
1922 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
1924 eth_broadcast_addr(creq->bssid);
1926 rdev->scan_req = creq;
1927 err = rdev_scan(rdev, creq);
1929 rdev->scan_req = NULL;
1930 /* creq will be freed below */
1932 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
1933 /* creq now owned by driver */
1941 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
1943 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
1944 const struct cfg80211_bss_ies *ies,
1945 char *current_ev, char *end_buf)
1947 const u8 *pos, *end, *next;
1948 struct iw_event iwe;
1954 * If needed, fragment the IEs buffer (at IE boundaries) into short
1955 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1958 end = pos + ies->len;
1960 while (end - pos > IW_GENERIC_IE_MAX) {
1961 next = pos + 2 + pos[1];
1962 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1963 next = next + 2 + next[1];
1965 memset(&iwe, 0, sizeof(iwe));
1966 iwe.cmd = IWEVGENIE;
1967 iwe.u.data.length = next - pos;
1968 current_ev = iwe_stream_add_point_check(info, current_ev,
1971 if (IS_ERR(current_ev))
1977 memset(&iwe, 0, sizeof(iwe));
1978 iwe.cmd = IWEVGENIE;
1979 iwe.u.data.length = end - pos;
1980 current_ev = iwe_stream_add_point_check(info, current_ev,
1983 if (IS_ERR(current_ev))
1991 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1992 struct cfg80211_internal_bss *bss, char *current_ev,
1995 const struct cfg80211_bss_ies *ies;
1996 struct iw_event iwe;
2001 bool ismesh = false;
2003 memset(&iwe, 0, sizeof(iwe));
2004 iwe.cmd = SIOCGIWAP;
2005 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2006 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2007 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2009 if (IS_ERR(current_ev))
2012 memset(&iwe, 0, sizeof(iwe));
2013 iwe.cmd = SIOCGIWFREQ;
2014 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2016 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2018 if (IS_ERR(current_ev))
2021 memset(&iwe, 0, sizeof(iwe));
2022 iwe.cmd = SIOCGIWFREQ;
2023 iwe.u.freq.m = bss->pub.channel->center_freq;
2025 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2027 if (IS_ERR(current_ev))
2030 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2031 memset(&iwe, 0, sizeof(iwe));
2033 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2034 IW_QUAL_NOISE_INVALID |
2035 IW_QUAL_QUAL_UPDATED;
2036 switch (wiphy->signal_type) {
2037 case CFG80211_SIGNAL_TYPE_MBM:
2038 sig = bss->pub.signal / 100;
2039 iwe.u.qual.level = sig;
2040 iwe.u.qual.updated |= IW_QUAL_DBM;
2041 if (sig < -110) /* rather bad */
2043 else if (sig > -40) /* perfect */
2045 /* will give a range of 0 .. 70 */
2046 iwe.u.qual.qual = sig + 110;
2048 case CFG80211_SIGNAL_TYPE_UNSPEC:
2049 iwe.u.qual.level = bss->pub.signal;
2050 /* will give range 0 .. 100 */
2051 iwe.u.qual.qual = bss->pub.signal;
2057 current_ev = iwe_stream_add_event_check(info, current_ev,
2060 if (IS_ERR(current_ev))
2064 memset(&iwe, 0, sizeof(iwe));
2065 iwe.cmd = SIOCGIWENCODE;
2066 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2067 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2069 iwe.u.data.flags = IW_ENCODE_DISABLED;
2070 iwe.u.data.length = 0;
2071 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2073 if (IS_ERR(current_ev))
2077 ies = rcu_dereference(bss->pub.ies);
2083 if (ie[1] > rem - 2)
2088 memset(&iwe, 0, sizeof(iwe));
2089 iwe.cmd = SIOCGIWESSID;
2090 iwe.u.data.length = ie[1];
2091 iwe.u.data.flags = 1;
2092 current_ev = iwe_stream_add_point_check(info,
2096 if (IS_ERR(current_ev))
2099 case WLAN_EID_MESH_ID:
2100 memset(&iwe, 0, sizeof(iwe));
2101 iwe.cmd = SIOCGIWESSID;
2102 iwe.u.data.length = ie[1];
2103 iwe.u.data.flags = 1;
2104 current_ev = iwe_stream_add_point_check(info,
2108 if (IS_ERR(current_ev))
2111 case WLAN_EID_MESH_CONFIG:
2113 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2116 memset(&iwe, 0, sizeof(iwe));
2117 iwe.cmd = IWEVCUSTOM;
2118 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2120 iwe.u.data.length = strlen(buf);
2121 current_ev = iwe_stream_add_point_check(info,
2125 if (IS_ERR(current_ev))
2127 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2129 iwe.u.data.length = strlen(buf);
2130 current_ev = iwe_stream_add_point_check(info,
2134 if (IS_ERR(current_ev))
2136 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2138 iwe.u.data.length = strlen(buf);
2139 current_ev = iwe_stream_add_point_check(info,
2143 if (IS_ERR(current_ev))
2145 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2146 iwe.u.data.length = strlen(buf);
2147 current_ev = iwe_stream_add_point_check(info,
2151 if (IS_ERR(current_ev))
2153 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2154 iwe.u.data.length = strlen(buf);
2155 current_ev = iwe_stream_add_point_check(info,
2159 if (IS_ERR(current_ev))
2161 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2162 iwe.u.data.length = strlen(buf);
2163 current_ev = iwe_stream_add_point_check(info,
2167 if (IS_ERR(current_ev))
2169 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2170 iwe.u.data.length = strlen(buf);
2171 current_ev = iwe_stream_add_point_check(info,
2175 if (IS_ERR(current_ev))
2178 case WLAN_EID_SUPP_RATES:
2179 case WLAN_EID_EXT_SUPP_RATES:
2180 /* display all supported rates in readable format */
2181 p = current_ev + iwe_stream_lcp_len(info);
2183 memset(&iwe, 0, sizeof(iwe));
2184 iwe.cmd = SIOCGIWRATE;
2185 /* Those two flags are ignored... */
2186 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2188 for (i = 0; i < ie[1]; i++) {
2189 iwe.u.bitrate.value =
2190 ((ie[i + 2] & 0x7f) * 500000);
2192 p = iwe_stream_add_value(info, current_ev, p,
2196 current_ev = ERR_PTR(-E2BIG);
2207 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2209 memset(&iwe, 0, sizeof(iwe));
2210 iwe.cmd = SIOCGIWMODE;
2212 iwe.u.mode = IW_MODE_MESH;
2213 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2214 iwe.u.mode = IW_MODE_MASTER;
2216 iwe.u.mode = IW_MODE_ADHOC;
2217 current_ev = iwe_stream_add_event_check(info, current_ev,
2220 if (IS_ERR(current_ev))
2224 memset(&iwe, 0, sizeof(iwe));
2225 iwe.cmd = IWEVCUSTOM;
2226 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2227 iwe.u.data.length = strlen(buf);
2228 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2230 if (IS_ERR(current_ev))
2232 memset(&iwe, 0, sizeof(iwe));
2233 iwe.cmd = IWEVCUSTOM;
2234 sprintf(buf, " Last beacon: %ums ago",
2235 elapsed_jiffies_msecs(bss->ts));
2236 iwe.u.data.length = strlen(buf);
2237 current_ev = iwe_stream_add_point_check(info, current_ev,
2238 end_buf, &iwe, buf);
2239 if (IS_ERR(current_ev))
2242 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2250 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2251 struct iw_request_info *info,
2252 char *buf, size_t len)
2254 char *current_ev = buf;
2255 char *end_buf = buf + len;
2256 struct cfg80211_internal_bss *bss;
2259 spin_lock_bh(&rdev->bss_lock);
2260 cfg80211_bss_expire(rdev);
2262 list_for_each_entry(bss, &rdev->bss_list, list) {
2263 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
2267 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
2268 current_ev, end_buf);
2269 if (IS_ERR(current_ev)) {
2270 err = PTR_ERR(current_ev);
2274 spin_unlock_bh(&rdev->bss_lock);
2278 return current_ev - buf;
2282 int cfg80211_wext_giwscan(struct net_device *dev,
2283 struct iw_request_info *info,
2284 struct iw_point *data, char *extra)
2286 struct cfg80211_registered_device *rdev;
2289 if (!netif_running(dev))
2292 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2295 return PTR_ERR(rdev);
2297 if (rdev->scan_req || rdev->scan_msg)
2300 res = ieee80211_scan_results(rdev, info, extra, data->length);
2309 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);