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 bool cfg80211_is_element_inherited(const struct element *elem,
183 const struct element *non_inherit_elem)
185 u8 id_len, ext_id_len, i, loop_len, id;
188 if (elem->id == WLAN_EID_MULTIPLE_BSSID)
191 if (!non_inherit_elem || non_inherit_elem->datalen < 2)
195 * non inheritance element format is:
196 * ext ID (56) | IDs list len | list | extension IDs list len | list
197 * Both lists are optional. Both lengths are mandatory.
198 * This means valid length is:
199 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
201 id_len = non_inherit_elem->data[1];
202 if (non_inherit_elem->datalen < 3 + id_len)
205 ext_id_len = non_inherit_elem->data[2 + id_len];
206 if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
209 if (elem->id == WLAN_EID_EXTENSION) {
212 loop_len = ext_id_len;
213 list = &non_inherit_elem->data[3 + id_len];
219 list = &non_inherit_elem->data[2];
223 for (i = 0; i < loop_len; i++) {
230 EXPORT_SYMBOL(cfg80211_is_element_inherited);
232 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
233 const u8 *subelement, size_t subie_len,
234 u8 *new_ie, gfp_t gfp)
237 const u8 *tmp_old, *tmp_new;
238 const struct element *non_inherit_elem;
241 /* copy subelement as we need to change its content to
242 * mark an ie after it is processed.
244 sub_copy = kmemdup(subelement, subie_len, gfp);
251 tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
253 memcpy(pos, tmp_new, tmp_new[1] + 2);
254 pos += (tmp_new[1] + 2);
257 /* get non inheritance list if exists */
259 cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
260 sub_copy, subie_len);
262 /* go through IEs in ie (skip SSID) and subelement,
263 * merge them into new_ie
265 tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
266 tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
268 while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
269 if (tmp_old[0] == 0) {
274 if (tmp_old[0] == WLAN_EID_EXTENSION)
275 tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
278 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
282 const struct element *old_elem = (void *)tmp_old;
284 /* ie in old ie but not in subelement */
285 if (cfg80211_is_element_inherited(old_elem,
287 memcpy(pos, tmp_old, tmp_old[1] + 2);
288 pos += tmp_old[1] + 2;
291 /* ie in transmitting ie also in subelement,
292 * copy from subelement and flag the ie in subelement
293 * as copied (by setting eid field to WLAN_EID_SSID,
294 * which is skipped anyway).
295 * For vendor ie, compare OUI + type + subType to
296 * determine if they are the same ie.
298 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
299 if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
300 /* same vendor ie, copy from
303 memcpy(pos, tmp, tmp[1] + 2);
305 tmp[0] = WLAN_EID_SSID;
307 memcpy(pos, tmp_old, tmp_old[1] + 2);
308 pos += tmp_old[1] + 2;
311 /* copy ie from subelement into new ie */
312 memcpy(pos, tmp, tmp[1] + 2);
314 tmp[0] = WLAN_EID_SSID;
318 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
321 tmp_old += tmp_old[1] + 2;
324 /* go through subelement again to check if there is any ie not
325 * copied to new ie, skip ssid, capability, bssid-index ie
328 while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
329 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
330 tmp_new[0] == WLAN_EID_SSID)) {
331 memcpy(pos, tmp_new, tmp_new[1] + 2);
332 pos += tmp_new[1] + 2;
334 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
336 tmp_new += tmp_new[1] + 2;
343 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
344 const u8 *ssid, size_t ssid_len)
346 const struct cfg80211_bss_ies *ies;
349 if (bssid && !ether_addr_equal(a->bssid, bssid))
355 ies = rcu_access_pointer(a->ies);
358 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
361 if (ssidie[1] != ssid_len)
363 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
367 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
368 struct cfg80211_bss *nontrans_bss)
372 struct cfg80211_bss *bss = NULL;
375 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
384 /* check if nontrans_bss is in the list */
385 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
386 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
390 /* add to the list */
391 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
395 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
396 unsigned long expire_time)
398 struct cfg80211_internal_bss *bss, *tmp;
399 bool expired = false;
401 lockdep_assert_held(&rdev->bss_lock);
403 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
404 if (atomic_read(&bss->hold))
406 if (!time_after(expire_time, bss->ts))
409 if (__cfg80211_unlink_bss(rdev, bss))
414 rdev->bss_generation++;
417 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
419 struct cfg80211_internal_bss *bss, *oldest = NULL;
422 lockdep_assert_held(&rdev->bss_lock);
424 list_for_each_entry(bss, &rdev->bss_list, list) {
425 if (atomic_read(&bss->hold))
428 if (!list_empty(&bss->hidden_list) &&
429 !bss->pub.hidden_beacon_bss)
432 if (oldest && time_before(oldest->ts, bss->ts))
437 if (WARN_ON(!oldest))
441 * The callers make sure to increase rdev->bss_generation if anything
442 * gets removed (and a new entry added), so there's no need to also do
446 ret = __cfg80211_unlink_bss(rdev, oldest);
451 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
454 struct cfg80211_scan_request *request;
455 struct wireless_dev *wdev;
457 #ifdef CONFIG_CFG80211_WEXT
458 union iwreq_data wrqu;
463 if (rdev->scan_msg) {
464 nl80211_send_scan_msg(rdev, rdev->scan_msg);
465 rdev->scan_msg = NULL;
469 request = rdev->scan_req;
473 wdev = request->wdev;
476 * This must be before sending the other events!
477 * Otherwise, wpa_supplicant gets completely confused with
481 cfg80211_sme_scan_done(wdev->netdev);
483 if (!request->info.aborted &&
484 request->flags & NL80211_SCAN_FLAG_FLUSH) {
485 /* flush entries from previous scans */
486 spin_lock_bh(&rdev->bss_lock);
487 __cfg80211_bss_expire(rdev, request->scan_start);
488 spin_unlock_bh(&rdev->bss_lock);
491 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
493 #ifdef CONFIG_CFG80211_WEXT
494 if (wdev->netdev && !request->info.aborted) {
495 memset(&wrqu, 0, sizeof(wrqu));
497 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
502 dev_put(wdev->netdev);
504 rdev->scan_req = NULL;
508 rdev->scan_msg = msg;
510 nl80211_send_scan_msg(rdev, msg);
513 void __cfg80211_scan_done(struct work_struct *wk)
515 struct cfg80211_registered_device *rdev;
517 rdev = container_of(wk, struct cfg80211_registered_device,
521 ___cfg80211_scan_done(rdev, true);
525 void cfg80211_scan_done(struct cfg80211_scan_request *request,
526 struct cfg80211_scan_info *info)
528 trace_cfg80211_scan_done(request, info);
529 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
531 request->info = *info;
532 request->notified = true;
533 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
535 EXPORT_SYMBOL(cfg80211_scan_done);
537 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
538 struct cfg80211_sched_scan_request *req)
542 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
545 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
546 struct cfg80211_sched_scan_request *req)
550 list_del_rcu(&req->list);
551 kfree_rcu(req, rcu_head);
554 static struct cfg80211_sched_scan_request *
555 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
557 struct cfg80211_sched_scan_request *pos;
559 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
561 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
562 if (pos->reqid == reqid)
569 * Determines if a scheduled scan request can be handled. When a legacy
570 * scheduled scan is running no other scheduled scan is allowed regardless
571 * whether the request is for legacy or multi-support scan. When a multi-support
572 * scheduled scan is running a request for legacy scan is not allowed. In this
573 * case a request for multi-support scan can be handled if resources are
574 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
576 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
579 struct cfg80211_sched_scan_request *pos;
582 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
583 /* request id zero means legacy in progress */
584 if (!i && !pos->reqid)
590 /* no legacy allowed when multi request(s) are active */
594 /* resource limit reached */
595 if (i == rdev->wiphy.max_sched_scan_reqs)
601 void cfg80211_sched_scan_results_wk(struct work_struct *work)
603 struct cfg80211_registered_device *rdev;
604 struct cfg80211_sched_scan_request *req, *tmp;
606 rdev = container_of(work, struct cfg80211_registered_device,
610 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
611 if (req->report_results) {
612 req->report_results = false;
613 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
614 /* flush entries from previous scans */
615 spin_lock_bh(&rdev->bss_lock);
616 __cfg80211_bss_expire(rdev, req->scan_start);
617 spin_unlock_bh(&rdev->bss_lock);
618 req->scan_start = jiffies;
620 nl80211_send_sched_scan(req,
621 NL80211_CMD_SCHED_SCAN_RESULTS);
627 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
629 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
630 struct cfg80211_sched_scan_request *request;
632 trace_cfg80211_sched_scan_results(wiphy, reqid);
633 /* ignore if we're not scanning */
636 request = cfg80211_find_sched_scan_req(rdev, reqid);
638 request->report_results = true;
639 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
643 EXPORT_SYMBOL(cfg80211_sched_scan_results);
645 void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
647 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
651 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
653 __cfg80211_stop_sched_scan(rdev, reqid, true);
655 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
657 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
660 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
663 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
665 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
666 struct cfg80211_sched_scan_request *req,
667 bool driver_initiated)
671 if (!driver_initiated) {
672 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
677 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
679 cfg80211_del_sched_scan_req(rdev, req);
684 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
685 u64 reqid, bool driver_initiated)
687 struct cfg80211_sched_scan_request *sched_scan_req;
691 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
695 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
699 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
700 unsigned long age_secs)
702 struct cfg80211_internal_bss *bss;
703 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
705 spin_lock_bh(&rdev->bss_lock);
706 list_for_each_entry(bss, &rdev->bss_list, list)
707 bss->ts -= age_jiffies;
708 spin_unlock_bh(&rdev->bss_lock);
711 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
713 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
716 const struct element *
717 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
718 const u8 *match, unsigned int match_len,
719 unsigned int match_offset)
721 const struct element *elem;
723 for_each_element_id(elem, eid, ies, len) {
724 if (elem->datalen >= match_offset + match_len &&
725 !memcmp(elem->data + match_offset, match, match_len))
731 EXPORT_SYMBOL(cfg80211_find_elem_match);
733 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
737 const struct element *elem;
738 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
739 int match_len = (oui_type < 0) ? 3 : sizeof(match);
741 if (WARN_ON(oui_type > 0xff))
744 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
745 match, match_len, 0);
747 if (!elem || elem->datalen < 4)
752 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
755 * enum bss_compare_mode - BSS compare mode
756 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
757 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
758 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
760 enum bss_compare_mode {
766 static int cmp_bss(struct cfg80211_bss *a,
767 struct cfg80211_bss *b,
768 enum bss_compare_mode mode)
770 const struct cfg80211_bss_ies *a_ies, *b_ies;
771 const u8 *ie1 = NULL;
772 const u8 *ie2 = NULL;
775 if (a->channel != b->channel)
776 return b->channel->center_freq - a->channel->center_freq;
778 a_ies = rcu_access_pointer(a->ies);
781 b_ies = rcu_access_pointer(b->ies);
785 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
786 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
787 a_ies->data, a_ies->len);
788 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
789 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
790 b_ies->data, b_ies->len);
794 if (ie1[1] == ie2[1])
795 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
797 mesh_id_cmp = ie2[1] - ie1[1];
799 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
800 a_ies->data, a_ies->len);
801 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
802 b_ies->data, b_ies->len);
806 if (ie1[1] != ie2[1])
807 return ie2[1] - ie1[1];
808 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
812 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
816 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
817 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
823 * Note that with "hide_ssid", the function returns a match if
824 * the already-present BSS ("b") is a hidden SSID beacon for
828 /* sort missing IE before (left of) present IE */
835 case BSS_CMP_HIDE_ZLEN:
837 * In ZLEN mode we assume the BSS entry we're
838 * looking for has a zero-length SSID. So if
839 * the one we're looking at right now has that,
840 * return 0. Otherwise, return the difference
841 * in length, but since we're looking for the
842 * 0-length it's really equivalent to returning
843 * the length of the one we're looking at.
845 * No content comparison is needed as we assume
846 * the content length is zero.
849 case BSS_CMP_REGULAR:
851 /* sort by length first, then by contents */
852 if (ie1[1] != ie2[1])
853 return ie2[1] - ie1[1];
854 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
855 case BSS_CMP_HIDE_NUL:
856 if (ie1[1] != ie2[1])
857 return ie2[1] - ie1[1];
858 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
859 for (i = 0; i < ie2[1]; i++)
866 static bool cfg80211_bss_type_match(u16 capability,
867 enum nl80211_band band,
868 enum ieee80211_bss_type bss_type)
873 if (bss_type == IEEE80211_BSS_TYPE_ANY)
876 if (band == NL80211_BAND_60GHZ) {
877 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
879 case IEEE80211_BSS_TYPE_ESS:
880 val = WLAN_CAPABILITY_DMG_TYPE_AP;
882 case IEEE80211_BSS_TYPE_PBSS:
883 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
885 case IEEE80211_BSS_TYPE_IBSS:
886 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
892 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
894 case IEEE80211_BSS_TYPE_ESS:
895 val = WLAN_CAPABILITY_ESS;
897 case IEEE80211_BSS_TYPE_IBSS:
898 val = WLAN_CAPABILITY_IBSS;
900 case IEEE80211_BSS_TYPE_MBSS:
908 ret = ((capability & mask) == val);
912 /* Returned bss is reference counted and must be cleaned up appropriately. */
913 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
914 struct ieee80211_channel *channel,
916 const u8 *ssid, size_t ssid_len,
917 enum ieee80211_bss_type bss_type,
918 enum ieee80211_privacy privacy)
920 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
921 struct cfg80211_internal_bss *bss, *res = NULL;
922 unsigned long now = jiffies;
925 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
928 spin_lock_bh(&rdev->bss_lock);
930 list_for_each_entry(bss, &rdev->bss_list, list) {
931 if (!cfg80211_bss_type_match(bss->pub.capability,
932 bss->pub.channel->band, bss_type))
935 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
936 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
937 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
939 if (channel && bss->pub.channel != channel)
941 if (!is_valid_ether_addr(bss->pub.bssid))
943 /* Don't get expired BSS structs */
944 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
945 !atomic_read(&bss->hold))
947 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
949 bss_ref_get(rdev, res);
954 spin_unlock_bh(&rdev->bss_lock);
957 trace_cfg80211_return_bss(&res->pub);
960 EXPORT_SYMBOL(cfg80211_get_bss);
962 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
963 struct cfg80211_internal_bss *bss)
965 struct rb_node **p = &rdev->bss_tree.rb_node;
966 struct rb_node *parent = NULL;
967 struct cfg80211_internal_bss *tbss;
972 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
974 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
977 /* will sort of leak this BSS */
987 rb_link_node(&bss->rbn, parent, p);
988 rb_insert_color(&bss->rbn, &rdev->bss_tree);
991 static struct cfg80211_internal_bss *
992 rb_find_bss(struct cfg80211_registered_device *rdev,
993 struct cfg80211_internal_bss *res,
994 enum bss_compare_mode mode)
996 struct rb_node *n = rdev->bss_tree.rb_node;
997 struct cfg80211_internal_bss *bss;
1001 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1002 r = cmp_bss(&res->pub, &bss->pub, mode);
1015 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1016 struct cfg80211_internal_bss *new)
1018 const struct cfg80211_bss_ies *ies;
1019 struct cfg80211_internal_bss *bss;
1025 ies = rcu_access_pointer(new->pub.beacon_ies);
1029 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1036 for (i = 0; i < ssidlen; i++)
1040 /* not a hidden SSID */
1044 /* This is the bad part ... */
1046 list_for_each_entry(bss, &rdev->bss_list, list) {
1048 * we're iterating all the entries anyway, so take the
1049 * opportunity to validate the list length accounting
1053 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1055 if (bss->pub.channel != new->pub.channel)
1057 if (bss->pub.scan_width != new->pub.scan_width)
1059 if (rcu_access_pointer(bss->pub.beacon_ies))
1061 ies = rcu_access_pointer(bss->pub.ies);
1064 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1067 if (ssidlen && ie[1] != ssidlen)
1069 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1071 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1072 list_del(&bss->hidden_list);
1074 list_add(&bss->hidden_list, &new->hidden_list);
1075 bss->pub.hidden_beacon_bss = &new->pub;
1076 new->refcount += bss->refcount;
1077 rcu_assign_pointer(bss->pub.beacon_ies,
1078 new->pub.beacon_ies);
1081 WARN_ONCE(n_entries != rdev->bss_entries,
1082 "rdev bss entries[%d]/list[len:%d] corruption\n",
1083 rdev->bss_entries, n_entries);
1088 struct cfg80211_non_tx_bss {
1089 struct cfg80211_bss *tx_bss;
1090 u8 max_bssid_indicator;
1095 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1096 struct cfg80211_internal_bss *known,
1097 struct cfg80211_internal_bss *new,
1100 lockdep_assert_held(&rdev->bss_lock);
1103 if (rcu_access_pointer(new->pub.proberesp_ies)) {
1104 const struct cfg80211_bss_ies *old;
1106 old = rcu_access_pointer(known->pub.proberesp_ies);
1108 rcu_assign_pointer(known->pub.proberesp_ies,
1109 new->pub.proberesp_ies);
1110 /* Override possible earlier Beacon frame IEs */
1111 rcu_assign_pointer(known->pub.ies,
1112 new->pub.proberesp_ies);
1114 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1115 } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1116 const struct cfg80211_bss_ies *old;
1117 struct cfg80211_internal_bss *bss;
1119 if (known->pub.hidden_beacon_bss &&
1120 !list_empty(&known->hidden_list)) {
1121 const struct cfg80211_bss_ies *f;
1123 /* The known BSS struct is one of the probe
1124 * response members of a group, but we're
1125 * receiving a beacon (beacon_ies in the new
1126 * bss is used). This can only mean that the
1127 * AP changed its beacon from not having an
1128 * SSID to showing it, which is confusing so
1129 * drop this information.
1132 f = rcu_access_pointer(new->pub.beacon_ies);
1133 kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1137 old = rcu_access_pointer(known->pub.beacon_ies);
1139 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1141 /* Override IEs if they were from a beacon before */
1142 if (old == rcu_access_pointer(known->pub.ies))
1143 rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1145 /* Assign beacon IEs to all sub entries */
1146 list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1147 const struct cfg80211_bss_ies *ies;
1149 ies = rcu_access_pointer(bss->pub.beacon_ies);
1150 WARN_ON(ies != old);
1152 rcu_assign_pointer(bss->pub.beacon_ies,
1153 new->pub.beacon_ies);
1157 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1160 known->pub.beacon_interval = new->pub.beacon_interval;
1162 /* don't update the signal if beacon was heard on
1166 known->pub.signal = new->pub.signal;
1167 known->pub.capability = new->pub.capability;
1168 known->ts = new->ts;
1169 known->ts_boottime = new->ts_boottime;
1170 known->parent_tsf = new->parent_tsf;
1171 known->pub.chains = new->pub.chains;
1172 memcpy(known->pub.chain_signal, new->pub.chain_signal,
1173 IEEE80211_MAX_CHAINS);
1174 ether_addr_copy(known->parent_bssid, new->parent_bssid);
1175 known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1176 known->pub.bssid_index = new->pub.bssid_index;
1181 /* Returned bss is reference counted and must be cleaned up appropriately. */
1182 struct cfg80211_internal_bss *
1183 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1184 struct cfg80211_internal_bss *tmp,
1185 bool signal_valid, unsigned long ts)
1187 struct cfg80211_internal_bss *found = NULL;
1189 if (WARN_ON(!tmp->pub.channel))
1194 spin_lock_bh(&rdev->bss_lock);
1196 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1197 spin_unlock_bh(&rdev->bss_lock);
1201 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1204 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1207 struct cfg80211_internal_bss *new;
1208 struct cfg80211_internal_bss *hidden;
1209 struct cfg80211_bss_ies *ies;
1212 * create a copy -- the "res" variable that is passed in
1213 * is allocated on the stack since it's not needed in the
1214 * more common case of an update
1216 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1219 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1221 kfree_rcu(ies, rcu_head);
1222 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1224 kfree_rcu(ies, rcu_head);
1227 memcpy(new, tmp, sizeof(*new));
1229 INIT_LIST_HEAD(&new->hidden_list);
1230 INIT_LIST_HEAD(&new->pub.nontrans_list);
1232 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1233 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1235 hidden = rb_find_bss(rdev, tmp,
1238 new->pub.hidden_beacon_bss = &hidden->pub;
1239 list_add(&new->hidden_list,
1240 &hidden->hidden_list);
1242 rcu_assign_pointer(new->pub.beacon_ies,
1243 hidden->pub.beacon_ies);
1247 * Ok so we found a beacon, and don't have an entry. If
1248 * it's a beacon with hidden SSID, we might be in for an
1249 * expensive search for any probe responses that should
1250 * be grouped with this beacon for updates ...
1252 if (!cfg80211_combine_bsses(rdev, new)) {
1258 if (rdev->bss_entries >= bss_entries_limit &&
1259 !cfg80211_bss_expire_oldest(rdev)) {
1264 /* This must be before the call to bss_ref_get */
1265 if (tmp->pub.transmitted_bss) {
1266 struct cfg80211_internal_bss *pbss =
1267 container_of(tmp->pub.transmitted_bss,
1268 struct cfg80211_internal_bss,
1271 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1272 bss_ref_get(rdev, pbss);
1275 list_add_tail(&new->list, &rdev->bss_list);
1276 rdev->bss_entries++;
1277 rb_insert_bss(rdev, new);
1281 rdev->bss_generation++;
1282 bss_ref_get(rdev, found);
1283 spin_unlock_bh(&rdev->bss_lock);
1287 spin_unlock_bh(&rdev->bss_lock);
1292 * Update RX channel information based on the available frame payload
1293 * information. This is mainly for the 2.4 GHz band where frames can be received
1294 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1295 * element to indicate the current (transmitting) channel, but this might also
1296 * be needed on other bands if RX frequency does not match with the actual
1297 * operating channel of a BSS.
1299 static struct ieee80211_channel *
1300 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1301 struct ieee80211_channel *channel,
1302 enum nl80211_bss_scan_width scan_width)
1306 int channel_number = -1;
1307 struct ieee80211_channel *alt_channel;
1309 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1310 if (tmp && tmp[1] == 1) {
1311 channel_number = tmp[2];
1313 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1314 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1315 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1317 channel_number = htop->primary_chan;
1321 if (channel_number < 0) {
1322 /* No channel information in frame payload */
1326 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
1327 alt_channel = ieee80211_get_channel(wiphy, freq);
1329 if (channel->band == NL80211_BAND_2GHZ) {
1331 * Better not allow unexpected channels when that could
1332 * be going beyond the 1-11 range (e.g., discovering
1333 * BSS on channel 12 when radio is configured for
1339 /* No match for the payload channel number - ignore it */
1343 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1344 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1346 * Ignore channel number in 5 and 10 MHz channels where there
1347 * may not be an n:1 or 1:n mapping between frequencies and
1354 * Use the channel determined through the payload channel number
1355 * instead of the RX channel reported by the driver.
1357 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1362 /* Returned bss is reference counted and must be cleaned up appropriately. */
1363 static struct cfg80211_bss *
1364 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1365 struct cfg80211_inform_bss *data,
1366 enum cfg80211_bss_frame_type ftype,
1367 const u8 *bssid, u64 tsf, u16 capability,
1368 u16 beacon_interval, const u8 *ie, size_t ielen,
1369 struct cfg80211_non_tx_bss *non_tx_data,
1372 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1373 struct cfg80211_bss_ies *ies;
1374 struct ieee80211_channel *channel;
1375 struct cfg80211_internal_bss tmp = {}, *res;
1380 if (WARN_ON(!wiphy))
1383 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1384 (data->signal < 0 || data->signal > 100)))
1387 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1392 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1393 tmp.pub.channel = channel;
1394 tmp.pub.scan_width = data->scan_width;
1395 tmp.pub.signal = data->signal;
1396 tmp.pub.beacon_interval = beacon_interval;
1397 tmp.pub.capability = capability;
1398 tmp.ts_boottime = data->boottime_ns;
1400 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1401 ts = bss_from_pub(non_tx_data->tx_bss)->ts;
1402 tmp.pub.bssid_index = non_tx_data->bssid_index;
1403 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1409 * If we do not know here whether the IEs are from a Beacon or Probe
1410 * Response frame, we need to pick one of the options and only use it
1411 * with the driver that does not provide the full Beacon/Probe Response
1412 * frame. Use Beacon frame pointer to avoid indicating that this should
1413 * override the IEs pointer should we have received an earlier
1414 * indication of Probe Response data.
1416 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1421 ies->from_beacon = false;
1422 memcpy(ies->data, ie, ielen);
1425 case CFG80211_BSS_FTYPE_BEACON:
1426 ies->from_beacon = true;
1428 case CFG80211_BSS_FTYPE_UNKNOWN:
1429 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1431 case CFG80211_BSS_FTYPE_PRESP:
1432 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1435 rcu_assign_pointer(tmp.pub.ies, ies);
1437 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1438 wiphy->max_adj_channel_rssi_comp;
1439 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
1443 if (channel->band == NL80211_BAND_60GHZ) {
1444 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1445 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1446 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1447 regulatory_hint_found_beacon(wiphy, channel, gfp);
1449 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1450 regulatory_hint_found_beacon(wiphy, channel, gfp);
1454 /* this is a nontransmitting bss, we need to add it to
1455 * transmitting bss' list if it is not there
1457 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1459 if (__cfg80211_unlink_bss(rdev, res))
1460 rdev->bss_generation++;
1464 trace_cfg80211_return_bss(&res->pub);
1465 /* cfg80211_bss_update gives us a referenced result */
1469 static const struct element
1470 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
1471 const struct element *mbssid_elem,
1472 const struct element *sub_elem)
1474 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
1475 const struct element *next_mbssid;
1476 const struct element *next_sub;
1478 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
1480 ielen - (mbssid_end - ie));
1483 * If is is not the last subelement in current MBSSID IE or there isn't
1484 * a next MBSSID IE - profile is complete.
1486 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
1490 /* For any length error, just return NULL */
1492 if (next_mbssid->datalen < 4)
1495 next_sub = (void *)&next_mbssid->data[1];
1497 if (next_mbssid->data + next_mbssid->datalen <
1498 next_sub->data + next_sub->datalen)
1501 if (next_sub->id != 0 || next_sub->datalen < 2)
1505 * Check if the first element in the next sub element is a start
1508 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
1512 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
1513 const struct element *mbssid_elem,
1514 const struct element *sub_elem,
1515 u8 *merged_ie, size_t max_copy_len)
1517 size_t copied_len = sub_elem->datalen;
1518 const struct element *next_mbssid;
1520 if (sub_elem->datalen > max_copy_len)
1523 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
1525 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
1528 const struct element *next_sub = (void *)&next_mbssid->data[1];
1530 if (copied_len + next_sub->datalen > max_copy_len)
1532 memcpy(merged_ie + copied_len, next_sub->data,
1534 copied_len += next_sub->datalen;
1539 EXPORT_SYMBOL(cfg80211_merge_profile);
1541 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1542 struct cfg80211_inform_bss *data,
1543 enum cfg80211_bss_frame_type ftype,
1544 const u8 *bssid, u64 tsf,
1545 u16 beacon_interval, const u8 *ie,
1547 struct cfg80211_non_tx_bss *non_tx_data,
1550 const u8 *mbssid_index_ie;
1551 const struct element *elem, *sub;
1553 u8 new_bssid[ETH_ALEN];
1554 u8 *new_ie, *profile;
1555 u64 seen_indices = 0;
1557 struct cfg80211_bss *bss;
1561 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1563 if (!wiphy->support_mbssid)
1565 if (wiphy->support_only_he_mbssid &&
1566 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1569 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1573 profile = kmalloc(ielen, gfp);
1577 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1578 if (elem->datalen < 4)
1580 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1583 if (sub->id != 0 || sub->datalen < 4) {
1584 /* not a valid BSS profile */
1588 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1589 sub->data[1] != 2) {
1590 /* The first element within the Nontransmitted
1591 * BSSID Profile is not the Nontransmitted
1592 * BSSID Capability element.
1597 memset(profile, 0, ielen);
1598 profile_len = cfg80211_merge_profile(ie, ielen,
1604 /* found a Nontransmitted BSSID Profile */
1605 mbssid_index_ie = cfg80211_find_ie
1606 (WLAN_EID_MULTI_BSSID_IDX,
1607 profile, profile_len);
1608 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1609 mbssid_index_ie[2] == 0 ||
1610 mbssid_index_ie[2] > 46) {
1611 /* No valid Multiple BSSID-Index element */
1615 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
1616 /* We don't support legacy split of a profile */
1617 net_dbg_ratelimited("Partial info for BSSID index %d\n",
1618 mbssid_index_ie[2]);
1620 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
1622 non_tx_data->bssid_index = mbssid_index_ie[2];
1623 non_tx_data->max_bssid_indicator = elem->data[0];
1625 cfg80211_gen_new_bssid(bssid,
1626 non_tx_data->max_bssid_indicator,
1627 non_tx_data->bssid_index,
1629 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
1630 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
1632 profile_len, new_ie,
1637 capability = get_unaligned_le16(profile + 2);
1638 bss = cfg80211_inform_single_bss_data(wiphy, data,
1649 cfg80211_put_bss(wiphy, bss);
1658 struct cfg80211_bss *
1659 cfg80211_inform_bss_data(struct wiphy *wiphy,
1660 struct cfg80211_inform_bss *data,
1661 enum cfg80211_bss_frame_type ftype,
1662 const u8 *bssid, u64 tsf, u16 capability,
1663 u16 beacon_interval, const u8 *ie, size_t ielen,
1666 struct cfg80211_bss *res;
1667 struct cfg80211_non_tx_bss non_tx_data;
1669 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1670 capability, beacon_interval, ie,
1674 non_tx_data.tx_bss = res;
1675 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
1676 beacon_interval, ie, ielen, &non_tx_data,
1680 EXPORT_SYMBOL(cfg80211_inform_bss_data);
1683 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1684 struct cfg80211_inform_bss *data,
1685 struct ieee80211_mgmt *mgmt, size_t len,
1686 struct cfg80211_non_tx_bss *non_tx_data,
1689 enum cfg80211_bss_frame_type ftype;
1690 const u8 *ie = mgmt->u.probe_resp.variable;
1691 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1692 u.probe_resp.variable);
1694 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1695 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1697 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1698 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1699 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
1700 ie, ielen, non_tx_data, gfp);
1704 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
1705 struct cfg80211_bss *nontrans_bss,
1706 struct ieee80211_mgmt *mgmt, size_t len)
1708 u8 *ie, *new_ie, *pos;
1709 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1710 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1711 u.probe_resp.variable);
1713 struct cfg80211_bss_ies *new_ies;
1714 const struct cfg80211_bss_ies *old;
1717 lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
1719 ie = mgmt->u.probe_resp.variable;
1722 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1725 new_ie_len -= trans_ssid[1];
1726 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1728 * It's not valid to have the MBSSID element before SSID
1729 * ignore if that happens - the code below assumes it is
1730 * after (while copying things inbetween).
1732 if (!mbssid || mbssid < trans_ssid)
1734 new_ie_len -= mbssid[1];
1736 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
1740 new_ie_len += nontrans_ssid[1];
1742 /* generate new ie for nontrans BSS
1743 * 1. replace SSID with nontrans BSS' SSID
1746 new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
1750 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
1756 /* copy the nontransmitted SSID */
1757 cpy_len = nontrans_ssid[1] + 2;
1758 memcpy(pos, nontrans_ssid, cpy_len);
1760 /* copy the IEs between SSID and MBSSID */
1761 cpy_len = trans_ssid[1] + 2;
1762 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1763 pos += (mbssid - (trans_ssid + cpy_len));
1764 /* copy the IEs after MBSSID */
1765 cpy_len = mbssid[1] + 2;
1766 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1769 new_ies->len = new_ie_len;
1770 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1771 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1772 memcpy(new_ies->data, new_ie, new_ie_len);
1773 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1774 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1775 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1776 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1778 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1780 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1781 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1782 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1784 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1791 /* cfg80211_inform_bss_width_frame helper */
1792 static struct cfg80211_bss *
1793 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1794 struct cfg80211_inform_bss *data,
1795 struct ieee80211_mgmt *mgmt, size_t len,
1798 struct cfg80211_internal_bss tmp = {}, *res;
1799 struct cfg80211_bss_ies *ies;
1800 struct ieee80211_channel *channel;
1802 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1803 u.probe_resp.variable);
1806 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1807 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1809 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
1814 if (WARN_ON(!wiphy))
1817 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1818 (data->signal < 0 || data->signal > 100)))
1821 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
1824 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
1825 ielen, data->chan, data->scan_width);
1829 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1833 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1834 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1835 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
1837 if (ieee80211_is_probe_resp(mgmt->frame_control))
1838 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1840 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1841 rcu_assign_pointer(tmp.pub.ies, ies);
1843 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1844 tmp.pub.channel = channel;
1845 tmp.pub.scan_width = data->scan_width;
1846 tmp.pub.signal = data->signal;
1847 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1848 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
1849 tmp.ts_boottime = data->boottime_ns;
1850 tmp.parent_tsf = data->parent_tsf;
1851 tmp.pub.chains = data->chains;
1852 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1853 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1855 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1856 wiphy->max_adj_channel_rssi_comp;
1857 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
1862 if (channel->band == NL80211_BAND_60GHZ) {
1863 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1864 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1865 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1866 regulatory_hint_found_beacon(wiphy, channel, gfp);
1868 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1869 regulatory_hint_found_beacon(wiphy, channel, gfp);
1872 trace_cfg80211_return_bss(&res->pub);
1873 /* cfg80211_bss_update gives us a referenced result */
1877 struct cfg80211_bss *
1878 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1879 struct cfg80211_inform_bss *data,
1880 struct ieee80211_mgmt *mgmt, size_t len,
1883 struct cfg80211_bss *res, *tmp_bss;
1884 const u8 *ie = mgmt->u.probe_resp.variable;
1885 const struct cfg80211_bss_ies *ies1, *ies2;
1886 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1887 u.probe_resp.variable);
1888 struct cfg80211_non_tx_bss non_tx_data;
1890 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1892 if (!res || !wiphy->support_mbssid ||
1893 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1895 if (wiphy->support_only_he_mbssid &&
1896 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1899 non_tx_data.tx_bss = res;
1900 /* process each non-transmitting bss */
1901 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1904 spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
1906 /* check if the res has other nontransmitting bss which is not
1909 ies1 = rcu_access_pointer(res->ies);
1911 /* go through nontrans_list, if the timestamp of the BSS is
1912 * earlier than the timestamp of the transmitting BSS then
1915 list_for_each_entry(tmp_bss, &res->nontrans_list,
1917 ies2 = rcu_access_pointer(tmp_bss->ies);
1918 if (ies2->tsf < ies1->tsf)
1919 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1922 spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
1926 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
1928 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1930 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1931 struct cfg80211_internal_bss *bss;
1936 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1938 spin_lock_bh(&rdev->bss_lock);
1939 bss_ref_get(rdev, bss);
1940 spin_unlock_bh(&rdev->bss_lock);
1942 EXPORT_SYMBOL(cfg80211_ref_bss);
1944 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1946 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1947 struct cfg80211_internal_bss *bss;
1952 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1954 spin_lock_bh(&rdev->bss_lock);
1955 bss_ref_put(rdev, bss);
1956 spin_unlock_bh(&rdev->bss_lock);
1958 EXPORT_SYMBOL(cfg80211_put_bss);
1960 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1962 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1963 struct cfg80211_internal_bss *bss, *tmp1;
1964 struct cfg80211_bss *nontrans_bss, *tmp;
1969 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1971 spin_lock_bh(&rdev->bss_lock);
1972 if (list_empty(&bss->list))
1975 list_for_each_entry_safe(nontrans_bss, tmp,
1976 &pub->nontrans_list,
1978 tmp1 = container_of(nontrans_bss,
1979 struct cfg80211_internal_bss, pub);
1980 if (__cfg80211_unlink_bss(rdev, tmp1))
1981 rdev->bss_generation++;
1984 if (__cfg80211_unlink_bss(rdev, bss))
1985 rdev->bss_generation++;
1987 spin_unlock_bh(&rdev->bss_lock);
1989 EXPORT_SYMBOL(cfg80211_unlink_bss);
1991 void cfg80211_bss_iter(struct wiphy *wiphy,
1992 struct cfg80211_chan_def *chandef,
1993 void (*iter)(struct wiphy *wiphy,
1994 struct cfg80211_bss *bss,
1998 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1999 struct cfg80211_internal_bss *bss;
2001 spin_lock_bh(&rdev->bss_lock);
2003 list_for_each_entry(bss, &rdev->bss_list, list) {
2004 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel))
2005 iter(wiphy, &bss->pub, iter_data);
2008 spin_unlock_bh(&rdev->bss_lock);
2010 EXPORT_SYMBOL(cfg80211_bss_iter);
2012 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2013 struct ieee80211_channel *chan)
2015 struct wiphy *wiphy = wdev->wiphy;
2016 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2017 struct cfg80211_internal_bss *cbss = wdev->current_bss;
2018 struct cfg80211_internal_bss *new = NULL;
2019 struct cfg80211_internal_bss *bss;
2020 struct cfg80211_bss *nontrans_bss;
2021 struct cfg80211_bss *tmp;
2023 spin_lock_bh(&rdev->bss_lock);
2025 if (WARN_ON(cbss->pub.channel == chan))
2028 /* use transmitting bss */
2029 if (cbss->pub.transmitted_bss)
2030 cbss = container_of(cbss->pub.transmitted_bss,
2031 struct cfg80211_internal_bss,
2034 cbss->pub.channel = chan;
2036 list_for_each_entry(bss, &rdev->bss_list, list) {
2037 if (!cfg80211_bss_type_match(bss->pub.capability,
2038 bss->pub.channel->band,
2039 wdev->conn_bss_type))
2045 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2052 /* to save time, update IEs for transmitting bss only */
2053 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2054 new->pub.proberesp_ies = NULL;
2055 new->pub.beacon_ies = NULL;
2058 list_for_each_entry_safe(nontrans_bss, tmp,
2059 &new->pub.nontrans_list,
2061 bss = container_of(nontrans_bss,
2062 struct cfg80211_internal_bss, pub);
2063 if (__cfg80211_unlink_bss(rdev, bss))
2064 rdev->bss_generation++;
2067 WARN_ON(atomic_read(&new->hold));
2068 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2069 rdev->bss_generation++;
2072 rb_erase(&cbss->rbn, &rdev->bss_tree);
2073 rb_insert_bss(rdev, cbss);
2074 rdev->bss_generation++;
2076 list_for_each_entry_safe(nontrans_bss, tmp,
2077 &cbss->pub.nontrans_list,
2079 bss = container_of(nontrans_bss,
2080 struct cfg80211_internal_bss, pub);
2081 bss->pub.channel = chan;
2082 rb_erase(&bss->rbn, &rdev->bss_tree);
2083 rb_insert_bss(rdev, bss);
2084 rdev->bss_generation++;
2088 spin_unlock_bh(&rdev->bss_lock);
2091 #ifdef CONFIG_CFG80211_WEXT
2092 static struct cfg80211_registered_device *
2093 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2095 struct cfg80211_registered_device *rdev;
2096 struct net_device *dev;
2100 dev = dev_get_by_index(net, ifindex);
2102 return ERR_PTR(-ENODEV);
2103 if (dev->ieee80211_ptr)
2104 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2106 rdev = ERR_PTR(-ENODEV);
2111 int cfg80211_wext_siwscan(struct net_device *dev,
2112 struct iw_request_info *info,
2113 union iwreq_data *wrqu, char *extra)
2115 struct cfg80211_registered_device *rdev;
2116 struct wiphy *wiphy;
2117 struct iw_scan_req *wreq = NULL;
2118 struct cfg80211_scan_request *creq = NULL;
2119 int i, err, n_channels = 0;
2120 enum nl80211_band band;
2122 if (!netif_running(dev))
2125 if (wrqu->data.length == sizeof(struct iw_scan_req))
2126 wreq = (struct iw_scan_req *)extra;
2128 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2131 return PTR_ERR(rdev);
2133 if (rdev->scan_req || rdev->scan_msg) {
2138 wiphy = &rdev->wiphy;
2140 /* Determine number of channels, needed to allocate creq */
2141 if (wreq && wreq->num_channels)
2142 n_channels = wreq->num_channels;
2144 n_channels = ieee80211_get_num_supported_channels(wiphy);
2146 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2147 n_channels * sizeof(void *),
2154 creq->wiphy = wiphy;
2155 creq->wdev = dev->ieee80211_ptr;
2156 /* SSIDs come after channels */
2157 creq->ssids = (void *)&creq->channels[n_channels];
2158 creq->n_channels = n_channels;
2160 creq->scan_start = jiffies;
2162 /* translate "Scan on frequencies" request */
2164 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2167 if (!wiphy->bands[band])
2170 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2171 /* ignore disabled channels */
2172 if (wiphy->bands[band]->channels[j].flags &
2173 IEEE80211_CHAN_DISABLED)
2176 /* If we have a wireless request structure and the
2177 * wireless request specifies frequencies, then search
2178 * for the matching hardware channel.
2180 if (wreq && wreq->num_channels) {
2182 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2183 for (k = 0; k < wreq->num_channels; k++) {
2184 struct iw_freq *freq =
2185 &wreq->channel_list[k];
2187 cfg80211_wext_freq(freq);
2189 if (wext_freq == wiphy_freq)
2190 goto wext_freq_found;
2192 goto wext_freq_not_found;
2196 creq->channels[i] = &wiphy->bands[band]->channels[j];
2198 wext_freq_not_found: ;
2201 /* No channels found? */
2207 /* Set real number of channels specified in creq->channels[] */
2208 creq->n_channels = i;
2210 /* translate "Scan for SSID" request */
2212 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2213 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2217 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2218 creq->ssids[0].ssid_len = wreq->essid_len;
2220 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2224 for (i = 0; i < NUM_NL80211_BANDS; i++)
2225 if (wiphy->bands[i])
2226 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2228 eth_broadcast_addr(creq->bssid);
2230 rdev->scan_req = creq;
2231 err = rdev_scan(rdev, creq);
2233 rdev->scan_req = NULL;
2234 /* creq will be freed below */
2236 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2237 /* creq now owned by driver */
2245 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2247 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2248 const struct cfg80211_bss_ies *ies,
2249 char *current_ev, char *end_buf)
2251 const u8 *pos, *end, *next;
2252 struct iw_event iwe;
2258 * If needed, fragment the IEs buffer (at IE boundaries) into short
2259 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2262 end = pos + ies->len;
2264 while (end - pos > IW_GENERIC_IE_MAX) {
2265 next = pos + 2 + pos[1];
2266 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2267 next = next + 2 + next[1];
2269 memset(&iwe, 0, sizeof(iwe));
2270 iwe.cmd = IWEVGENIE;
2271 iwe.u.data.length = next - pos;
2272 current_ev = iwe_stream_add_point_check(info, current_ev,
2275 if (IS_ERR(current_ev))
2281 memset(&iwe, 0, sizeof(iwe));
2282 iwe.cmd = IWEVGENIE;
2283 iwe.u.data.length = end - pos;
2284 current_ev = iwe_stream_add_point_check(info, current_ev,
2287 if (IS_ERR(current_ev))
2295 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2296 struct cfg80211_internal_bss *bss, char *current_ev,
2299 const struct cfg80211_bss_ies *ies;
2300 struct iw_event iwe;
2305 bool ismesh = false;
2307 memset(&iwe, 0, sizeof(iwe));
2308 iwe.cmd = SIOCGIWAP;
2309 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2310 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2311 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2313 if (IS_ERR(current_ev))
2316 memset(&iwe, 0, sizeof(iwe));
2317 iwe.cmd = SIOCGIWFREQ;
2318 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2320 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2322 if (IS_ERR(current_ev))
2325 memset(&iwe, 0, sizeof(iwe));
2326 iwe.cmd = SIOCGIWFREQ;
2327 iwe.u.freq.m = bss->pub.channel->center_freq;
2329 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2331 if (IS_ERR(current_ev))
2334 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2335 memset(&iwe, 0, sizeof(iwe));
2337 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2338 IW_QUAL_NOISE_INVALID |
2339 IW_QUAL_QUAL_UPDATED;
2340 switch (wiphy->signal_type) {
2341 case CFG80211_SIGNAL_TYPE_MBM:
2342 sig = bss->pub.signal / 100;
2343 iwe.u.qual.level = sig;
2344 iwe.u.qual.updated |= IW_QUAL_DBM;
2345 if (sig < -110) /* rather bad */
2347 else if (sig > -40) /* perfect */
2349 /* will give a range of 0 .. 70 */
2350 iwe.u.qual.qual = sig + 110;
2352 case CFG80211_SIGNAL_TYPE_UNSPEC:
2353 iwe.u.qual.level = bss->pub.signal;
2354 /* will give range 0 .. 100 */
2355 iwe.u.qual.qual = bss->pub.signal;
2361 current_ev = iwe_stream_add_event_check(info, current_ev,
2364 if (IS_ERR(current_ev))
2368 memset(&iwe, 0, sizeof(iwe));
2369 iwe.cmd = SIOCGIWENCODE;
2370 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2371 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2373 iwe.u.data.flags = IW_ENCODE_DISABLED;
2374 iwe.u.data.length = 0;
2375 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2377 if (IS_ERR(current_ev))
2381 ies = rcu_dereference(bss->pub.ies);
2387 if (ie[1] > rem - 2)
2392 memset(&iwe, 0, sizeof(iwe));
2393 iwe.cmd = SIOCGIWESSID;
2394 iwe.u.data.length = ie[1];
2395 iwe.u.data.flags = 1;
2396 current_ev = iwe_stream_add_point_check(info,
2400 if (IS_ERR(current_ev))
2403 case WLAN_EID_MESH_ID:
2404 memset(&iwe, 0, sizeof(iwe));
2405 iwe.cmd = SIOCGIWESSID;
2406 iwe.u.data.length = ie[1];
2407 iwe.u.data.flags = 1;
2408 current_ev = iwe_stream_add_point_check(info,
2412 if (IS_ERR(current_ev))
2415 case WLAN_EID_MESH_CONFIG:
2417 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2420 memset(&iwe, 0, sizeof(iwe));
2421 iwe.cmd = IWEVCUSTOM;
2422 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2424 iwe.u.data.length = strlen(buf);
2425 current_ev = iwe_stream_add_point_check(info,
2429 if (IS_ERR(current_ev))
2431 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2433 iwe.u.data.length = strlen(buf);
2434 current_ev = iwe_stream_add_point_check(info,
2438 if (IS_ERR(current_ev))
2440 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2442 iwe.u.data.length = strlen(buf);
2443 current_ev = iwe_stream_add_point_check(info,
2447 if (IS_ERR(current_ev))
2449 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2450 iwe.u.data.length = strlen(buf);
2451 current_ev = iwe_stream_add_point_check(info,
2455 if (IS_ERR(current_ev))
2457 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2458 iwe.u.data.length = strlen(buf);
2459 current_ev = iwe_stream_add_point_check(info,
2463 if (IS_ERR(current_ev))
2465 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2466 iwe.u.data.length = strlen(buf);
2467 current_ev = iwe_stream_add_point_check(info,
2471 if (IS_ERR(current_ev))
2473 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2474 iwe.u.data.length = strlen(buf);
2475 current_ev = iwe_stream_add_point_check(info,
2479 if (IS_ERR(current_ev))
2482 case WLAN_EID_SUPP_RATES:
2483 case WLAN_EID_EXT_SUPP_RATES:
2484 /* display all supported rates in readable format */
2485 p = current_ev + iwe_stream_lcp_len(info);
2487 memset(&iwe, 0, sizeof(iwe));
2488 iwe.cmd = SIOCGIWRATE;
2489 /* Those two flags are ignored... */
2490 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2492 for (i = 0; i < ie[1]; i++) {
2493 iwe.u.bitrate.value =
2494 ((ie[i + 2] & 0x7f) * 500000);
2496 p = iwe_stream_add_value(info, current_ev, p,
2500 current_ev = ERR_PTR(-E2BIG);
2511 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2513 memset(&iwe, 0, sizeof(iwe));
2514 iwe.cmd = SIOCGIWMODE;
2516 iwe.u.mode = IW_MODE_MESH;
2517 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2518 iwe.u.mode = IW_MODE_MASTER;
2520 iwe.u.mode = IW_MODE_ADHOC;
2521 current_ev = iwe_stream_add_event_check(info, current_ev,
2524 if (IS_ERR(current_ev))
2528 memset(&iwe, 0, sizeof(iwe));
2529 iwe.cmd = IWEVCUSTOM;
2530 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2531 iwe.u.data.length = strlen(buf);
2532 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2534 if (IS_ERR(current_ev))
2536 memset(&iwe, 0, sizeof(iwe));
2537 iwe.cmd = IWEVCUSTOM;
2538 sprintf(buf, " Last beacon: %ums ago",
2539 elapsed_jiffies_msecs(bss->ts));
2540 iwe.u.data.length = strlen(buf);
2541 current_ev = iwe_stream_add_point_check(info, current_ev,
2542 end_buf, &iwe, buf);
2543 if (IS_ERR(current_ev))
2546 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2554 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2555 struct iw_request_info *info,
2556 char *buf, size_t len)
2558 char *current_ev = buf;
2559 char *end_buf = buf + len;
2560 struct cfg80211_internal_bss *bss;
2563 spin_lock_bh(&rdev->bss_lock);
2564 cfg80211_bss_expire(rdev);
2566 list_for_each_entry(bss, &rdev->bss_list, list) {
2567 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
2571 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
2572 current_ev, end_buf);
2573 if (IS_ERR(current_ev)) {
2574 err = PTR_ERR(current_ev);
2578 spin_unlock_bh(&rdev->bss_lock);
2582 return current_ev - buf;
2586 int cfg80211_wext_giwscan(struct net_device *dev,
2587 struct iw_request_info *info,
2588 struct iw_point *data, char *extra)
2590 struct cfg80211_registered_device *rdev;
2593 if (!netif_running(dev))
2596 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2599 return PTR_ERR(rdev);
2601 if (rdev->scan_req || rdev->scan_msg)
2604 res = ieee80211_scan_results(rdev, info, extra, data->length);
2613 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);