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 if (tmp_old[0] == WLAN_EID_EXTENSION)
220 tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
223 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
227 /* ie in old ie but not in subelement */
228 if (tmp_old[0] != WLAN_EID_MULTIPLE_BSSID) {
229 memcpy(pos, tmp_old, tmp_old[1] + 2);
230 pos += tmp_old[1] + 2;
233 /* ie in transmitting ie also in subelement,
234 * copy from subelement and flag the ie in subelement
235 * as copied (by setting eid field to WLAN_EID_SSID,
236 * which is skipped anyway).
237 * For vendor ie, compare OUI + type + subType to
238 * determine if they are the same ie.
240 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
241 if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
242 /* same vendor ie, copy from
245 memcpy(pos, tmp, tmp[1] + 2);
247 tmp[0] = WLAN_EID_SSID;
249 memcpy(pos, tmp_old, tmp_old[1] + 2);
250 pos += tmp_old[1] + 2;
253 /* copy ie from subelement into new ie */
254 memcpy(pos, tmp, tmp[1] + 2);
256 tmp[0] = WLAN_EID_SSID;
260 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
263 tmp_old += tmp_old[1] + 2;
266 /* go through subelement again to check if there is any ie not
267 * copied to new ie, skip ssid, capability, bssid-index ie
270 while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
271 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
272 tmp_new[0] == WLAN_EID_SSID ||
273 tmp_new[0] == WLAN_EID_MULTI_BSSID_IDX)) {
274 memcpy(pos, tmp_new, tmp_new[1] + 2);
275 pos += tmp_new[1] + 2;
277 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
279 tmp_new += tmp_new[1] + 2;
286 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
287 const u8 *ssid, size_t ssid_len)
289 const struct cfg80211_bss_ies *ies;
292 if (bssid && !ether_addr_equal(a->bssid, bssid))
298 ies = rcu_access_pointer(a->ies);
301 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
304 if (ssidie[1] != ssid_len)
306 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
310 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
311 struct cfg80211_bss *nontrans_bss)
315 struct cfg80211_bss *bss = NULL;
318 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
327 /* check if nontrans_bss is in the list */
328 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
329 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
333 /* add to the list */
334 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
338 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
339 unsigned long expire_time)
341 struct cfg80211_internal_bss *bss, *tmp;
342 bool expired = false;
344 lockdep_assert_held(&rdev->bss_lock);
346 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
347 if (atomic_read(&bss->hold))
349 if (!time_after(expire_time, bss->ts))
352 if (__cfg80211_unlink_bss(rdev, bss))
357 rdev->bss_generation++;
360 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
362 struct cfg80211_internal_bss *bss, *oldest = NULL;
365 lockdep_assert_held(&rdev->bss_lock);
367 list_for_each_entry(bss, &rdev->bss_list, list) {
368 if (atomic_read(&bss->hold))
371 if (!list_empty(&bss->hidden_list) &&
372 !bss->pub.hidden_beacon_bss)
375 if (oldest && time_before(oldest->ts, bss->ts))
380 if (WARN_ON(!oldest))
384 * The callers make sure to increase rdev->bss_generation if anything
385 * gets removed (and a new entry added), so there's no need to also do
389 ret = __cfg80211_unlink_bss(rdev, oldest);
394 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
397 struct cfg80211_scan_request *request;
398 struct wireless_dev *wdev;
400 #ifdef CONFIG_CFG80211_WEXT
401 union iwreq_data wrqu;
406 if (rdev->scan_msg) {
407 nl80211_send_scan_msg(rdev, rdev->scan_msg);
408 rdev->scan_msg = NULL;
412 request = rdev->scan_req;
416 wdev = request->wdev;
419 * This must be before sending the other events!
420 * Otherwise, wpa_supplicant gets completely confused with
424 cfg80211_sme_scan_done(wdev->netdev);
426 if (!request->info.aborted &&
427 request->flags & NL80211_SCAN_FLAG_FLUSH) {
428 /* flush entries from previous scans */
429 spin_lock_bh(&rdev->bss_lock);
430 __cfg80211_bss_expire(rdev, request->scan_start);
431 spin_unlock_bh(&rdev->bss_lock);
434 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
436 #ifdef CONFIG_CFG80211_WEXT
437 if (wdev->netdev && !request->info.aborted) {
438 memset(&wrqu, 0, sizeof(wrqu));
440 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
445 dev_put(wdev->netdev);
447 rdev->scan_req = NULL;
451 rdev->scan_msg = msg;
453 nl80211_send_scan_msg(rdev, msg);
456 void __cfg80211_scan_done(struct work_struct *wk)
458 struct cfg80211_registered_device *rdev;
460 rdev = container_of(wk, struct cfg80211_registered_device,
464 ___cfg80211_scan_done(rdev, true);
468 void cfg80211_scan_done(struct cfg80211_scan_request *request,
469 struct cfg80211_scan_info *info)
471 trace_cfg80211_scan_done(request, info);
472 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
474 request->info = *info;
475 request->notified = true;
476 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
478 EXPORT_SYMBOL(cfg80211_scan_done);
480 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
481 struct cfg80211_sched_scan_request *req)
485 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
488 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
489 struct cfg80211_sched_scan_request *req)
493 list_del_rcu(&req->list);
494 kfree_rcu(req, rcu_head);
497 static struct cfg80211_sched_scan_request *
498 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
500 struct cfg80211_sched_scan_request *pos;
502 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
504 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
505 if (pos->reqid == reqid)
512 * Determines if a scheduled scan request can be handled. When a legacy
513 * scheduled scan is running no other scheduled scan is allowed regardless
514 * whether the request is for legacy or multi-support scan. When a multi-support
515 * scheduled scan is running a request for legacy scan is not allowed. In this
516 * case a request for multi-support scan can be handled if resources are
517 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
519 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
522 struct cfg80211_sched_scan_request *pos;
525 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
526 /* request id zero means legacy in progress */
527 if (!i && !pos->reqid)
533 /* no legacy allowed when multi request(s) are active */
537 /* resource limit reached */
538 if (i == rdev->wiphy.max_sched_scan_reqs)
544 void cfg80211_sched_scan_results_wk(struct work_struct *work)
546 struct cfg80211_registered_device *rdev;
547 struct cfg80211_sched_scan_request *req, *tmp;
549 rdev = container_of(work, struct cfg80211_registered_device,
553 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
554 if (req->report_results) {
555 req->report_results = false;
556 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
557 /* flush entries from previous scans */
558 spin_lock_bh(&rdev->bss_lock);
559 __cfg80211_bss_expire(rdev, req->scan_start);
560 spin_unlock_bh(&rdev->bss_lock);
561 req->scan_start = jiffies;
563 nl80211_send_sched_scan(req,
564 NL80211_CMD_SCHED_SCAN_RESULTS);
570 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
572 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
573 struct cfg80211_sched_scan_request *request;
575 trace_cfg80211_sched_scan_results(wiphy, reqid);
576 /* ignore if we're not scanning */
579 request = cfg80211_find_sched_scan_req(rdev, reqid);
581 request->report_results = true;
582 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
586 EXPORT_SYMBOL(cfg80211_sched_scan_results);
588 void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
590 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
594 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
596 __cfg80211_stop_sched_scan(rdev, reqid, true);
598 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
600 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
603 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
606 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
608 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
609 struct cfg80211_sched_scan_request *req,
610 bool driver_initiated)
614 if (!driver_initiated) {
615 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
620 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
622 cfg80211_del_sched_scan_req(rdev, req);
627 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
628 u64 reqid, bool driver_initiated)
630 struct cfg80211_sched_scan_request *sched_scan_req;
634 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
638 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
642 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
643 unsigned long age_secs)
645 struct cfg80211_internal_bss *bss;
646 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
648 spin_lock_bh(&rdev->bss_lock);
649 list_for_each_entry(bss, &rdev->bss_list, list)
650 bss->ts -= age_jiffies;
651 spin_unlock_bh(&rdev->bss_lock);
654 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
656 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
659 const struct element *
660 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
661 const u8 *match, unsigned int match_len,
662 unsigned int match_offset)
664 const struct element *elem;
666 for_each_element_id(elem, eid, ies, len) {
667 if (elem->datalen >= match_offset + match_len &&
668 !memcmp(elem->data + match_offset, match, match_len))
674 EXPORT_SYMBOL(cfg80211_find_elem_match);
676 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
680 const struct element *elem;
681 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
682 int match_len = (oui_type < 0) ? 3 : sizeof(match);
684 if (WARN_ON(oui_type > 0xff))
687 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
688 match, match_len, 0);
690 if (!elem || elem->datalen < 4)
695 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
698 * enum bss_compare_mode - BSS compare mode
699 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
700 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
701 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
703 enum bss_compare_mode {
709 static int cmp_bss(struct cfg80211_bss *a,
710 struct cfg80211_bss *b,
711 enum bss_compare_mode mode)
713 const struct cfg80211_bss_ies *a_ies, *b_ies;
714 const u8 *ie1 = NULL;
715 const u8 *ie2 = NULL;
718 if (a->channel != b->channel)
719 return b->channel->center_freq - a->channel->center_freq;
721 a_ies = rcu_access_pointer(a->ies);
724 b_ies = rcu_access_pointer(b->ies);
728 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
729 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
730 a_ies->data, a_ies->len);
731 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
732 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
733 b_ies->data, b_ies->len);
737 if (ie1[1] == ie2[1])
738 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
740 mesh_id_cmp = ie2[1] - ie1[1];
742 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
743 a_ies->data, a_ies->len);
744 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
745 b_ies->data, b_ies->len);
749 if (ie1[1] != ie2[1])
750 return ie2[1] - ie1[1];
751 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
755 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
759 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
760 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
766 * Note that with "hide_ssid", the function returns a match if
767 * the already-present BSS ("b") is a hidden SSID beacon for
771 /* sort missing IE before (left of) present IE */
778 case BSS_CMP_HIDE_ZLEN:
780 * In ZLEN mode we assume the BSS entry we're
781 * looking for has a zero-length SSID. So if
782 * the one we're looking at right now has that,
783 * return 0. Otherwise, return the difference
784 * in length, but since we're looking for the
785 * 0-length it's really equivalent to returning
786 * the length of the one we're looking at.
788 * No content comparison is needed as we assume
789 * the content length is zero.
792 case BSS_CMP_REGULAR:
794 /* sort by length first, then by contents */
795 if (ie1[1] != ie2[1])
796 return ie2[1] - ie1[1];
797 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
798 case BSS_CMP_HIDE_NUL:
799 if (ie1[1] != ie2[1])
800 return ie2[1] - ie1[1];
801 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
802 for (i = 0; i < ie2[1]; i++)
809 static bool cfg80211_bss_type_match(u16 capability,
810 enum nl80211_band band,
811 enum ieee80211_bss_type bss_type)
816 if (bss_type == IEEE80211_BSS_TYPE_ANY)
819 if (band == NL80211_BAND_60GHZ) {
820 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
822 case IEEE80211_BSS_TYPE_ESS:
823 val = WLAN_CAPABILITY_DMG_TYPE_AP;
825 case IEEE80211_BSS_TYPE_PBSS:
826 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
828 case IEEE80211_BSS_TYPE_IBSS:
829 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
835 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
837 case IEEE80211_BSS_TYPE_ESS:
838 val = WLAN_CAPABILITY_ESS;
840 case IEEE80211_BSS_TYPE_IBSS:
841 val = WLAN_CAPABILITY_IBSS;
843 case IEEE80211_BSS_TYPE_MBSS:
851 ret = ((capability & mask) == val);
855 /* Returned bss is reference counted and must be cleaned up appropriately. */
856 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
857 struct ieee80211_channel *channel,
859 const u8 *ssid, size_t ssid_len,
860 enum ieee80211_bss_type bss_type,
861 enum ieee80211_privacy privacy)
863 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
864 struct cfg80211_internal_bss *bss, *res = NULL;
865 unsigned long now = jiffies;
868 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
871 spin_lock_bh(&rdev->bss_lock);
873 list_for_each_entry(bss, &rdev->bss_list, list) {
874 if (!cfg80211_bss_type_match(bss->pub.capability,
875 bss->pub.channel->band, bss_type))
878 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
879 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
880 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
882 if (channel && bss->pub.channel != channel)
884 if (!is_valid_ether_addr(bss->pub.bssid))
886 /* Don't get expired BSS structs */
887 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
888 !atomic_read(&bss->hold))
890 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
892 bss_ref_get(rdev, res);
897 spin_unlock_bh(&rdev->bss_lock);
900 trace_cfg80211_return_bss(&res->pub);
903 EXPORT_SYMBOL(cfg80211_get_bss);
905 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
906 struct cfg80211_internal_bss *bss)
908 struct rb_node **p = &rdev->bss_tree.rb_node;
909 struct rb_node *parent = NULL;
910 struct cfg80211_internal_bss *tbss;
915 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
917 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
920 /* will sort of leak this BSS */
930 rb_link_node(&bss->rbn, parent, p);
931 rb_insert_color(&bss->rbn, &rdev->bss_tree);
934 static struct cfg80211_internal_bss *
935 rb_find_bss(struct cfg80211_registered_device *rdev,
936 struct cfg80211_internal_bss *res,
937 enum bss_compare_mode mode)
939 struct rb_node *n = rdev->bss_tree.rb_node;
940 struct cfg80211_internal_bss *bss;
944 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
945 r = cmp_bss(&res->pub, &bss->pub, mode);
958 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
959 struct cfg80211_internal_bss *new)
961 const struct cfg80211_bss_ies *ies;
962 struct cfg80211_internal_bss *bss;
968 ies = rcu_access_pointer(new->pub.beacon_ies);
972 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
979 for (i = 0; i < ssidlen; i++)
983 /* not a hidden SSID */
987 /* This is the bad part ... */
989 list_for_each_entry(bss, &rdev->bss_list, list) {
991 * we're iterating all the entries anyway, so take the
992 * opportunity to validate the list length accounting
996 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
998 if (bss->pub.channel != new->pub.channel)
1000 if (bss->pub.scan_width != new->pub.scan_width)
1002 if (rcu_access_pointer(bss->pub.beacon_ies))
1004 ies = rcu_access_pointer(bss->pub.ies);
1007 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1010 if (ssidlen && ie[1] != ssidlen)
1012 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1014 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1015 list_del(&bss->hidden_list);
1017 list_add(&bss->hidden_list, &new->hidden_list);
1018 bss->pub.hidden_beacon_bss = &new->pub;
1019 new->refcount += bss->refcount;
1020 rcu_assign_pointer(bss->pub.beacon_ies,
1021 new->pub.beacon_ies);
1024 WARN_ONCE(n_entries != rdev->bss_entries,
1025 "rdev bss entries[%d]/list[len:%d] corruption\n",
1026 rdev->bss_entries, n_entries);
1031 struct cfg80211_non_tx_bss {
1032 struct cfg80211_bss *tx_bss;
1033 u8 max_bssid_indicator;
1037 /* Returned bss is reference counted and must be cleaned up appropriately. */
1038 static struct cfg80211_internal_bss *
1039 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1040 struct cfg80211_internal_bss *tmp,
1043 struct cfg80211_internal_bss *found = NULL;
1045 if (WARN_ON(!tmp->pub.channel))
1050 spin_lock_bh(&rdev->bss_lock);
1052 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1053 spin_unlock_bh(&rdev->bss_lock);
1057 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1061 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1062 const struct cfg80211_bss_ies *old;
1064 old = rcu_access_pointer(found->pub.proberesp_ies);
1066 rcu_assign_pointer(found->pub.proberesp_ies,
1067 tmp->pub.proberesp_ies);
1068 /* Override possible earlier Beacon frame IEs */
1069 rcu_assign_pointer(found->pub.ies,
1070 tmp->pub.proberesp_ies);
1072 kfree_rcu((struct cfg80211_bss_ies *)old,
1074 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
1075 const struct cfg80211_bss_ies *old;
1076 struct cfg80211_internal_bss *bss;
1078 if (found->pub.hidden_beacon_bss &&
1079 !list_empty(&found->hidden_list)) {
1080 const struct cfg80211_bss_ies *f;
1083 * The found BSS struct is one of the probe
1084 * response members of a group, but we're
1085 * receiving a beacon (beacon_ies in the tmp
1086 * bss is used). This can only mean that the
1087 * AP changed its beacon from not having an
1088 * SSID to showing it, which is confusing so
1089 * drop this information.
1092 f = rcu_access_pointer(tmp->pub.beacon_ies);
1093 kfree_rcu((struct cfg80211_bss_ies *)f,
1098 old = rcu_access_pointer(found->pub.beacon_ies);
1100 rcu_assign_pointer(found->pub.beacon_ies,
1101 tmp->pub.beacon_ies);
1103 /* Override IEs if they were from a beacon before */
1104 if (old == rcu_access_pointer(found->pub.ies))
1105 rcu_assign_pointer(found->pub.ies,
1106 tmp->pub.beacon_ies);
1108 /* Assign beacon IEs to all sub entries */
1109 list_for_each_entry(bss, &found->hidden_list,
1111 const struct cfg80211_bss_ies *ies;
1113 ies = rcu_access_pointer(bss->pub.beacon_ies);
1114 WARN_ON(ies != old);
1116 rcu_assign_pointer(bss->pub.beacon_ies,
1117 tmp->pub.beacon_ies);
1121 kfree_rcu((struct cfg80211_bss_ies *)old,
1125 found->pub.beacon_interval = tmp->pub.beacon_interval;
1127 * don't update the signal if beacon was heard on
1131 found->pub.signal = tmp->pub.signal;
1132 found->pub.capability = tmp->pub.capability;
1133 found->ts = tmp->ts;
1134 found->ts_boottime = tmp->ts_boottime;
1135 found->parent_tsf = tmp->parent_tsf;
1136 found->pub.chains = tmp->pub.chains;
1137 memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
1138 IEEE80211_MAX_CHAINS);
1139 ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
1140 found->pub.max_bssid_indicator = tmp->pub.max_bssid_indicator;
1141 found->pub.bssid_index = tmp->pub.bssid_index;
1143 struct cfg80211_internal_bss *new;
1144 struct cfg80211_internal_bss *hidden;
1145 struct cfg80211_bss_ies *ies;
1148 * create a copy -- the "res" variable that is passed in
1149 * is allocated on the stack since it's not needed in the
1150 * more common case of an update
1152 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1155 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1157 kfree_rcu(ies, rcu_head);
1158 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1160 kfree_rcu(ies, rcu_head);
1163 memcpy(new, tmp, sizeof(*new));
1165 INIT_LIST_HEAD(&new->hidden_list);
1166 INIT_LIST_HEAD(&new->pub.nontrans_list);
1168 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1169 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1171 hidden = rb_find_bss(rdev, tmp,
1174 new->pub.hidden_beacon_bss = &hidden->pub;
1175 list_add(&new->hidden_list,
1176 &hidden->hidden_list);
1178 rcu_assign_pointer(new->pub.beacon_ies,
1179 hidden->pub.beacon_ies);
1183 * Ok so we found a beacon, and don't have an entry. If
1184 * it's a beacon with hidden SSID, we might be in for an
1185 * expensive search for any probe responses that should
1186 * be grouped with this beacon for updates ...
1188 if (!cfg80211_combine_bsses(rdev, new)) {
1194 if (rdev->bss_entries >= bss_entries_limit &&
1195 !cfg80211_bss_expire_oldest(rdev)) {
1200 /* This must be before the call to bss_ref_get */
1201 if (tmp->pub.transmitted_bss) {
1202 struct cfg80211_internal_bss *pbss =
1203 container_of(tmp->pub.transmitted_bss,
1204 struct cfg80211_internal_bss,
1207 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1208 bss_ref_get(rdev, pbss);
1211 list_add_tail(&new->list, &rdev->bss_list);
1212 rdev->bss_entries++;
1213 rb_insert_bss(rdev, new);
1217 rdev->bss_generation++;
1218 bss_ref_get(rdev, found);
1219 spin_unlock_bh(&rdev->bss_lock);
1223 spin_unlock_bh(&rdev->bss_lock);
1228 * Update RX channel information based on the available frame payload
1229 * information. This is mainly for the 2.4 GHz band where frames can be received
1230 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1231 * element to indicate the current (transmitting) channel, but this might also
1232 * be needed on other bands if RX frequency does not match with the actual
1233 * operating channel of a BSS.
1235 static struct ieee80211_channel *
1236 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1237 struct ieee80211_channel *channel,
1238 enum nl80211_bss_scan_width scan_width)
1242 int channel_number = -1;
1243 struct ieee80211_channel *alt_channel;
1245 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1246 if (tmp && tmp[1] == 1) {
1247 channel_number = tmp[2];
1249 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1250 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1251 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1253 channel_number = htop->primary_chan;
1257 if (channel_number < 0) {
1258 /* No channel information in frame payload */
1262 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
1263 alt_channel = ieee80211_get_channel(wiphy, freq);
1265 if (channel->band == NL80211_BAND_2GHZ) {
1267 * Better not allow unexpected channels when that could
1268 * be going beyond the 1-11 range (e.g., discovering
1269 * BSS on channel 12 when radio is configured for
1275 /* No match for the payload channel number - ignore it */
1279 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1280 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1282 * Ignore channel number in 5 and 10 MHz channels where there
1283 * may not be an n:1 or 1:n mapping between frequencies and
1290 * Use the channel determined through the payload channel number
1291 * instead of the RX channel reported by the driver.
1293 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1298 /* Returned bss is reference counted and must be cleaned up appropriately. */
1299 static struct cfg80211_bss *
1300 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1301 struct cfg80211_inform_bss *data,
1302 enum cfg80211_bss_frame_type ftype,
1303 const u8 *bssid, u64 tsf, u16 capability,
1304 u16 beacon_interval, const u8 *ie, size_t ielen,
1305 struct cfg80211_non_tx_bss *non_tx_data,
1308 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1309 struct cfg80211_bss_ies *ies;
1310 struct ieee80211_channel *channel;
1311 struct cfg80211_internal_bss tmp = {}, *res;
1315 if (WARN_ON(!wiphy))
1318 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1319 (data->signal < 0 || data->signal > 100)))
1322 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1327 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1328 tmp.pub.channel = channel;
1329 tmp.pub.scan_width = data->scan_width;
1330 tmp.pub.signal = data->signal;
1331 tmp.pub.beacon_interval = beacon_interval;
1332 tmp.pub.capability = capability;
1333 tmp.ts_boottime = data->boottime_ns;
1335 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1336 tmp.pub.bssid_index = non_tx_data->bssid_index;
1337 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1341 * If we do not know here whether the IEs are from a Beacon or Probe
1342 * Response frame, we need to pick one of the options and only use it
1343 * with the driver that does not provide the full Beacon/Probe Response
1344 * frame. Use Beacon frame pointer to avoid indicating that this should
1345 * override the IEs pointer should we have received an earlier
1346 * indication of Probe Response data.
1348 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1353 ies->from_beacon = false;
1354 memcpy(ies->data, ie, ielen);
1357 case CFG80211_BSS_FTYPE_BEACON:
1358 ies->from_beacon = true;
1360 case CFG80211_BSS_FTYPE_UNKNOWN:
1361 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1363 case CFG80211_BSS_FTYPE_PRESP:
1364 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1367 rcu_assign_pointer(tmp.pub.ies, ies);
1369 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1370 wiphy->max_adj_channel_rssi_comp;
1371 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
1375 if (channel->band == NL80211_BAND_60GHZ) {
1376 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1377 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1378 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1379 regulatory_hint_found_beacon(wiphy, channel, gfp);
1381 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1382 regulatory_hint_found_beacon(wiphy, channel, gfp);
1385 if (non_tx_data && non_tx_data->tx_bss) {
1386 /* this is a nontransmitting bss, we need to add it to
1387 * transmitting bss' list if it is not there
1389 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1391 if (__cfg80211_unlink_bss(rdev, res))
1392 rdev->bss_generation++;
1396 trace_cfg80211_return_bss(&res->pub);
1397 /* cfg80211_bss_update gives us a referenced result */
1401 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1402 struct cfg80211_inform_bss *data,
1403 enum cfg80211_bss_frame_type ftype,
1404 const u8 *bssid, u64 tsf,
1405 u16 beacon_interval, const u8 *ie,
1407 struct cfg80211_non_tx_bss *non_tx_data,
1410 const u8 *mbssid_index_ie;
1411 const struct element *elem, *sub;
1413 u8 new_bssid[ETH_ALEN];
1416 struct cfg80211_bss *bss;
1420 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1422 if (!wiphy->support_mbssid)
1424 if (wiphy->support_only_he_mbssid &&
1425 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1428 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1432 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1433 if (elem->datalen < 4)
1435 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1436 if (sub->id != 0 || sub->datalen < 4) {
1437 /* not a valid BSS profile */
1441 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1442 sub->data[1] != 2) {
1443 /* The first element within the Nontransmitted
1444 * BSSID Profile is not the Nontransmitted
1445 * BSSID Capability element.
1450 /* found a Nontransmitted BSSID Profile */
1451 mbssid_index_ie = cfg80211_find_ie
1452 (WLAN_EID_MULTI_BSSID_IDX,
1453 sub->data, sub->datalen);
1454 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1455 mbssid_index_ie[2] == 0) {
1456 /* No valid Multiple BSSID-Index element */
1460 non_tx_data->bssid_index = mbssid_index_ie[2];
1461 non_tx_data->max_bssid_indicator = elem->data[0];
1463 cfg80211_gen_new_bssid(bssid,
1464 non_tx_data->max_bssid_indicator,
1465 non_tx_data->bssid_index,
1467 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
1468 new_ie_len = cfg80211_gen_new_ie(ie, ielen, sub->data,
1469 sub->datalen, new_ie,
1474 capability = get_unaligned_le16(sub->data + 2);
1475 bss = cfg80211_inform_single_bss_data(wiphy, data,
1486 cfg80211_put_bss(wiphy, bss);
1493 struct cfg80211_bss *
1494 cfg80211_inform_bss_data(struct wiphy *wiphy,
1495 struct cfg80211_inform_bss *data,
1496 enum cfg80211_bss_frame_type ftype,
1497 const u8 *bssid, u64 tsf, u16 capability,
1498 u16 beacon_interval, const u8 *ie, size_t ielen,
1501 struct cfg80211_bss *res;
1502 struct cfg80211_non_tx_bss non_tx_data;
1504 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1505 capability, beacon_interval, ie,
1507 non_tx_data.tx_bss = res;
1508 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
1509 beacon_interval, ie, ielen, &non_tx_data,
1513 EXPORT_SYMBOL(cfg80211_inform_bss_data);
1516 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1517 struct cfg80211_inform_bss *data,
1518 struct ieee80211_mgmt *mgmt, size_t len,
1519 struct cfg80211_non_tx_bss *non_tx_data,
1522 enum cfg80211_bss_frame_type ftype;
1523 const u8 *ie = mgmt->u.probe_resp.variable;
1524 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1525 u.probe_resp.variable);
1527 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1528 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1530 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1531 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1532 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
1533 ie, ielen, non_tx_data, gfp);
1537 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
1538 struct cfg80211_bss *nontrans_bss,
1539 struct ieee80211_mgmt *mgmt, size_t len,
1542 u8 *ie, *new_ie, *pos;
1543 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1544 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1545 u.probe_resp.variable);
1547 struct cfg80211_bss_ies *new_ies;
1548 const struct cfg80211_bss_ies *old;
1551 ie = mgmt->u.probe_resp.variable;
1554 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1557 new_ie_len -= trans_ssid[1];
1558 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1561 new_ie_len -= mbssid[1];
1563 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
1564 if (!nontrans_ssid) {
1568 new_ie_len += nontrans_ssid[1];
1571 /* generate new ie for nontrans BSS
1572 * 1. replace SSID with nontrans BSS' SSID
1575 new_ie = kzalloc(new_ie_len, gfp);
1578 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp);
1584 /* copy the nontransmitted SSID */
1585 cpy_len = nontrans_ssid[1] + 2;
1586 memcpy(pos, nontrans_ssid, cpy_len);
1588 /* copy the IEs between SSID and MBSSID */
1589 cpy_len = trans_ssid[1] + 2;
1590 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1591 pos += (mbssid - (trans_ssid + cpy_len));
1592 /* copy the IEs after MBSSID */
1593 cpy_len = mbssid[1] + 2;
1594 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1597 new_ies->len = new_ie_len;
1598 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1599 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1600 memcpy(new_ies->data, new_ie, new_ie_len);
1601 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1602 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1603 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1604 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1606 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1608 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1609 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1610 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1612 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1619 /* cfg80211_inform_bss_width_frame helper */
1620 static struct cfg80211_bss *
1621 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1622 struct cfg80211_inform_bss *data,
1623 struct ieee80211_mgmt *mgmt, size_t len,
1624 struct cfg80211_non_tx_bss *non_tx_data,
1627 struct cfg80211_internal_bss tmp = {}, *res;
1628 struct cfg80211_bss_ies *ies;
1629 struct ieee80211_channel *channel;
1631 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1632 u.probe_resp.variable);
1635 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1636 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1638 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
1643 if (WARN_ON(!wiphy))
1646 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1647 (data->signal < 0 || data->signal > 100)))
1650 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
1653 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
1654 ielen, data->chan, data->scan_width);
1658 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1662 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1663 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1664 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
1666 if (ieee80211_is_probe_resp(mgmt->frame_control))
1667 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1669 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1670 rcu_assign_pointer(tmp.pub.ies, ies);
1672 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1673 tmp.pub.channel = channel;
1674 tmp.pub.scan_width = data->scan_width;
1675 tmp.pub.signal = data->signal;
1676 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1677 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
1678 tmp.ts_boottime = data->boottime_ns;
1679 tmp.parent_tsf = data->parent_tsf;
1680 tmp.pub.chains = data->chains;
1681 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1682 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1684 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1685 tmp.pub.bssid_index = non_tx_data->bssid_index;
1686 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1689 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1690 wiphy->max_adj_channel_rssi_comp;
1691 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
1695 if (channel->band == NL80211_BAND_60GHZ) {
1696 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1697 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1698 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1699 regulatory_hint_found_beacon(wiphy, channel, gfp);
1701 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1702 regulatory_hint_found_beacon(wiphy, channel, gfp);
1705 trace_cfg80211_return_bss(&res->pub);
1706 /* cfg80211_bss_update gives us a referenced result */
1710 struct cfg80211_bss *
1711 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1712 struct cfg80211_inform_bss *data,
1713 struct ieee80211_mgmt *mgmt, size_t len,
1716 struct cfg80211_bss *res, *tmp_bss;
1717 const u8 *ie = mgmt->u.probe_resp.variable;
1718 const struct cfg80211_bss_ies *ies1, *ies2;
1719 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1720 u.probe_resp.variable);
1721 struct cfg80211_non_tx_bss non_tx_data;
1723 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1725 if (!res || !wiphy->support_mbssid ||
1726 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1728 if (wiphy->support_only_he_mbssid &&
1729 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1732 non_tx_data.tx_bss = res;
1733 /* process each non-transmitting bss */
1734 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1737 /* check if the res has other nontransmitting bss which is not
1740 ies1 = rcu_access_pointer(res->ies);
1742 /* go through nontrans_list, if the timestamp of the BSS is
1743 * earlier than the timestamp of the transmitting BSS then
1746 list_for_each_entry(tmp_bss, &res->nontrans_list,
1748 ies2 = rcu_access_pointer(tmp_bss->ies);
1749 if (ies2->tsf < ies1->tsf)
1750 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1756 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
1758 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1760 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1761 struct cfg80211_internal_bss *bss;
1766 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1768 spin_lock_bh(&rdev->bss_lock);
1769 bss_ref_get(rdev, bss);
1770 spin_unlock_bh(&rdev->bss_lock);
1772 EXPORT_SYMBOL(cfg80211_ref_bss);
1774 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1776 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1777 struct cfg80211_internal_bss *bss;
1782 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1784 spin_lock_bh(&rdev->bss_lock);
1785 bss_ref_put(rdev, bss);
1786 spin_unlock_bh(&rdev->bss_lock);
1788 EXPORT_SYMBOL(cfg80211_put_bss);
1790 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1792 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1793 struct cfg80211_internal_bss *bss, *tmp1;
1794 struct cfg80211_bss *nontrans_bss, *tmp;
1799 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1801 spin_lock_bh(&rdev->bss_lock);
1802 if (list_empty(&bss->list))
1805 list_for_each_entry_safe(nontrans_bss, tmp,
1806 &pub->nontrans_list,
1808 tmp1 = container_of(nontrans_bss,
1809 struct cfg80211_internal_bss, pub);
1810 if (__cfg80211_unlink_bss(rdev, tmp1))
1811 rdev->bss_generation++;
1814 if (__cfg80211_unlink_bss(rdev, bss))
1815 rdev->bss_generation++;
1817 spin_unlock_bh(&rdev->bss_lock);
1819 EXPORT_SYMBOL(cfg80211_unlink_bss);
1821 #ifdef CONFIG_CFG80211_WEXT
1822 static struct cfg80211_registered_device *
1823 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1825 struct cfg80211_registered_device *rdev;
1826 struct net_device *dev;
1830 dev = dev_get_by_index(net, ifindex);
1832 return ERR_PTR(-ENODEV);
1833 if (dev->ieee80211_ptr)
1834 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
1836 rdev = ERR_PTR(-ENODEV);
1841 int cfg80211_wext_siwscan(struct net_device *dev,
1842 struct iw_request_info *info,
1843 union iwreq_data *wrqu, char *extra)
1845 struct cfg80211_registered_device *rdev;
1846 struct wiphy *wiphy;
1847 struct iw_scan_req *wreq = NULL;
1848 struct cfg80211_scan_request *creq = NULL;
1849 int i, err, n_channels = 0;
1850 enum nl80211_band band;
1852 if (!netif_running(dev))
1855 if (wrqu->data.length == sizeof(struct iw_scan_req))
1856 wreq = (struct iw_scan_req *)extra;
1858 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1861 return PTR_ERR(rdev);
1863 if (rdev->scan_req || rdev->scan_msg) {
1868 wiphy = &rdev->wiphy;
1870 /* Determine number of channels, needed to allocate creq */
1871 if (wreq && wreq->num_channels)
1872 n_channels = wreq->num_channels;
1874 n_channels = ieee80211_get_num_supported_channels(wiphy);
1876 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1877 n_channels * sizeof(void *),
1884 creq->wiphy = wiphy;
1885 creq->wdev = dev->ieee80211_ptr;
1886 /* SSIDs come after channels */
1887 creq->ssids = (void *)&creq->channels[n_channels];
1888 creq->n_channels = n_channels;
1890 creq->scan_start = jiffies;
1892 /* translate "Scan on frequencies" request */
1894 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1897 if (!wiphy->bands[band])
1900 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1901 /* ignore disabled channels */
1902 if (wiphy->bands[band]->channels[j].flags &
1903 IEEE80211_CHAN_DISABLED)
1906 /* If we have a wireless request structure and the
1907 * wireless request specifies frequencies, then search
1908 * for the matching hardware channel.
1910 if (wreq && wreq->num_channels) {
1912 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1913 for (k = 0; k < wreq->num_channels; k++) {
1914 struct iw_freq *freq =
1915 &wreq->channel_list[k];
1917 cfg80211_wext_freq(freq);
1919 if (wext_freq == wiphy_freq)
1920 goto wext_freq_found;
1922 goto wext_freq_not_found;
1926 creq->channels[i] = &wiphy->bands[band]->channels[j];
1928 wext_freq_not_found: ;
1931 /* No channels found? */
1937 /* Set real number of channels specified in creq->channels[] */
1938 creq->n_channels = i;
1940 /* translate "Scan for SSID" request */
1942 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1943 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1947 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1948 creq->ssids[0].ssid_len = wreq->essid_len;
1950 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1954 for (i = 0; i < NUM_NL80211_BANDS; i++)
1955 if (wiphy->bands[i])
1956 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
1958 eth_broadcast_addr(creq->bssid);
1960 rdev->scan_req = creq;
1961 err = rdev_scan(rdev, creq);
1963 rdev->scan_req = NULL;
1964 /* creq will be freed below */
1966 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
1967 /* creq now owned by driver */
1975 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
1977 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
1978 const struct cfg80211_bss_ies *ies,
1979 char *current_ev, char *end_buf)
1981 const u8 *pos, *end, *next;
1982 struct iw_event iwe;
1988 * If needed, fragment the IEs buffer (at IE boundaries) into short
1989 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1992 end = pos + ies->len;
1994 while (end - pos > IW_GENERIC_IE_MAX) {
1995 next = pos + 2 + pos[1];
1996 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1997 next = next + 2 + next[1];
1999 memset(&iwe, 0, sizeof(iwe));
2000 iwe.cmd = IWEVGENIE;
2001 iwe.u.data.length = next - pos;
2002 current_ev = iwe_stream_add_point_check(info, current_ev,
2005 if (IS_ERR(current_ev))
2011 memset(&iwe, 0, sizeof(iwe));
2012 iwe.cmd = IWEVGENIE;
2013 iwe.u.data.length = end - pos;
2014 current_ev = iwe_stream_add_point_check(info, current_ev,
2017 if (IS_ERR(current_ev))
2025 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2026 struct cfg80211_internal_bss *bss, char *current_ev,
2029 const struct cfg80211_bss_ies *ies;
2030 struct iw_event iwe;
2035 bool ismesh = false;
2037 memset(&iwe, 0, sizeof(iwe));
2038 iwe.cmd = SIOCGIWAP;
2039 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2040 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2041 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2043 if (IS_ERR(current_ev))
2046 memset(&iwe, 0, sizeof(iwe));
2047 iwe.cmd = SIOCGIWFREQ;
2048 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2050 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2052 if (IS_ERR(current_ev))
2055 memset(&iwe, 0, sizeof(iwe));
2056 iwe.cmd = SIOCGIWFREQ;
2057 iwe.u.freq.m = bss->pub.channel->center_freq;
2059 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2061 if (IS_ERR(current_ev))
2064 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2065 memset(&iwe, 0, sizeof(iwe));
2067 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2068 IW_QUAL_NOISE_INVALID |
2069 IW_QUAL_QUAL_UPDATED;
2070 switch (wiphy->signal_type) {
2071 case CFG80211_SIGNAL_TYPE_MBM:
2072 sig = bss->pub.signal / 100;
2073 iwe.u.qual.level = sig;
2074 iwe.u.qual.updated |= IW_QUAL_DBM;
2075 if (sig < -110) /* rather bad */
2077 else if (sig > -40) /* perfect */
2079 /* will give a range of 0 .. 70 */
2080 iwe.u.qual.qual = sig + 110;
2082 case CFG80211_SIGNAL_TYPE_UNSPEC:
2083 iwe.u.qual.level = bss->pub.signal;
2084 /* will give range 0 .. 100 */
2085 iwe.u.qual.qual = bss->pub.signal;
2091 current_ev = iwe_stream_add_event_check(info, current_ev,
2094 if (IS_ERR(current_ev))
2098 memset(&iwe, 0, sizeof(iwe));
2099 iwe.cmd = SIOCGIWENCODE;
2100 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2101 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2103 iwe.u.data.flags = IW_ENCODE_DISABLED;
2104 iwe.u.data.length = 0;
2105 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2107 if (IS_ERR(current_ev))
2111 ies = rcu_dereference(bss->pub.ies);
2117 if (ie[1] > rem - 2)
2122 memset(&iwe, 0, sizeof(iwe));
2123 iwe.cmd = SIOCGIWESSID;
2124 iwe.u.data.length = ie[1];
2125 iwe.u.data.flags = 1;
2126 current_ev = iwe_stream_add_point_check(info,
2130 if (IS_ERR(current_ev))
2133 case WLAN_EID_MESH_ID:
2134 memset(&iwe, 0, sizeof(iwe));
2135 iwe.cmd = SIOCGIWESSID;
2136 iwe.u.data.length = ie[1];
2137 iwe.u.data.flags = 1;
2138 current_ev = iwe_stream_add_point_check(info,
2142 if (IS_ERR(current_ev))
2145 case WLAN_EID_MESH_CONFIG:
2147 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2150 memset(&iwe, 0, sizeof(iwe));
2151 iwe.cmd = IWEVCUSTOM;
2152 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
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, "Path Selection Metric ID: 0x%02X",
2163 iwe.u.data.length = strlen(buf);
2164 current_ev = iwe_stream_add_point_check(info,
2168 if (IS_ERR(current_ev))
2170 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2172 iwe.u.data.length = strlen(buf);
2173 current_ev = iwe_stream_add_point_check(info,
2177 if (IS_ERR(current_ev))
2179 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2180 iwe.u.data.length = strlen(buf);
2181 current_ev = iwe_stream_add_point_check(info,
2185 if (IS_ERR(current_ev))
2187 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2188 iwe.u.data.length = strlen(buf);
2189 current_ev = iwe_stream_add_point_check(info,
2193 if (IS_ERR(current_ev))
2195 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2196 iwe.u.data.length = strlen(buf);
2197 current_ev = iwe_stream_add_point_check(info,
2201 if (IS_ERR(current_ev))
2203 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2204 iwe.u.data.length = strlen(buf);
2205 current_ev = iwe_stream_add_point_check(info,
2209 if (IS_ERR(current_ev))
2212 case WLAN_EID_SUPP_RATES:
2213 case WLAN_EID_EXT_SUPP_RATES:
2214 /* display all supported rates in readable format */
2215 p = current_ev + iwe_stream_lcp_len(info);
2217 memset(&iwe, 0, sizeof(iwe));
2218 iwe.cmd = SIOCGIWRATE;
2219 /* Those two flags are ignored... */
2220 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2222 for (i = 0; i < ie[1]; i++) {
2223 iwe.u.bitrate.value =
2224 ((ie[i + 2] & 0x7f) * 500000);
2226 p = iwe_stream_add_value(info, current_ev, p,
2230 current_ev = ERR_PTR(-E2BIG);
2241 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2243 memset(&iwe, 0, sizeof(iwe));
2244 iwe.cmd = SIOCGIWMODE;
2246 iwe.u.mode = IW_MODE_MESH;
2247 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2248 iwe.u.mode = IW_MODE_MASTER;
2250 iwe.u.mode = IW_MODE_ADHOC;
2251 current_ev = iwe_stream_add_event_check(info, current_ev,
2254 if (IS_ERR(current_ev))
2258 memset(&iwe, 0, sizeof(iwe));
2259 iwe.cmd = IWEVCUSTOM;
2260 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2261 iwe.u.data.length = strlen(buf);
2262 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2264 if (IS_ERR(current_ev))
2266 memset(&iwe, 0, sizeof(iwe));
2267 iwe.cmd = IWEVCUSTOM;
2268 sprintf(buf, " Last beacon: %ums ago",
2269 elapsed_jiffies_msecs(bss->ts));
2270 iwe.u.data.length = strlen(buf);
2271 current_ev = iwe_stream_add_point_check(info, current_ev,
2272 end_buf, &iwe, buf);
2273 if (IS_ERR(current_ev))
2276 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2284 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2285 struct iw_request_info *info,
2286 char *buf, size_t len)
2288 char *current_ev = buf;
2289 char *end_buf = buf + len;
2290 struct cfg80211_internal_bss *bss;
2293 spin_lock_bh(&rdev->bss_lock);
2294 cfg80211_bss_expire(rdev);
2296 list_for_each_entry(bss, &rdev->bss_list, list) {
2297 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
2301 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
2302 current_ev, end_buf);
2303 if (IS_ERR(current_ev)) {
2304 err = PTR_ERR(current_ev);
2308 spin_unlock_bh(&rdev->bss_lock);
2312 return current_ev - buf;
2316 int cfg80211_wext_giwscan(struct net_device *dev,
2317 struct iw_request_info *info,
2318 struct iw_point *data, char *extra)
2320 struct cfg80211_registered_device *rdev;
2323 if (!netif_running(dev))
2326 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2329 return PTR_ERR(rdev);
2331 if (rdev->scan_req || rdev->scan_msg)
2334 res = ieee80211_scan_results(rdev, info, extra, data->length);
2343 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);