blob: 287518c6caa40204525993d8b2477e269324f378 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Berg2a519312009-02-10 21:25:55 +01002/*
3 * cfg80211 scan result handling
4 *
5 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg2740f0c2014-09-03 15:24:58 +03006 * Copyright 2013-2014 Intel Mobile Communications GmbH
Avraham Stern1d762502016-07-05 17:10:13 +03007 * Copyright 2016 Intel Deutschland GmbH
Peng Xu0b8fb822019-01-21 12:14:57 +02008 * Copyright (C) 2018-2019 Intel Corporation
Johannes Berg2a519312009-02-10 21:25:55 +01009 */
10#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Johannes Berg2a519312009-02-10 21:25:55 +010012#include <linux/module.h>
13#include <linux/netdevice.h>
14#include <linux/wireless.h>
15#include <linux/nl80211.h>
16#include <linux/etherdevice.h>
17#include <net/arp.h>
18#include <net/cfg80211.h>
Johannes Berg262eb9b22011-07-13 10:39:09 +020019#include <net/cfg80211-wext.h>
Johannes Berg2a519312009-02-10 21:25:55 +010020#include <net/iw_handler.h>
21#include "core.h"
22#include "nl80211.h"
Johannes Berga9a11622009-07-27 12:01:53 +020023#include "wext-compat.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030024#include "rdev-ops.h"
Johannes Berg2a519312009-02-10 21:25:55 +010025
Johannes Berg776b3582013-02-01 02:06:18 +010026/**
27 * DOC: BSS tree/list structure
28 *
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
33 * for other BSSes.
34 *
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.
42 *
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.
51 *
52 * Note that the hidden_beacon_bss pointer never changes, due to
53 * the reference counting. Therefore, no locking is needed for
54 * it.
55 *
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.
60 */
61
Johannes Berg9853a552016-11-15 12:05:11 +010062/*
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.)
69 */
70static int bss_entries_limit = 1000;
71module_param(bss_entries_limit, int, 0644);
72MODULE_PARM_DESC(bss_entries_limit,
73 "limit to number of scan BSS entries (per wiphy, default 1000)");
74
Rajkumar Manoharanf9616e02012-04-13 16:38:40 +053075#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010076
Johannes Berg776b3582013-02-01 02:06:18 +010077static void bss_free(struct cfg80211_internal_bss *bss)
Amitkumar Karware8e27c62012-10-11 21:03:33 -070078{
Johannes Berg9caf0362012-11-29 01:25:20 +010079 struct cfg80211_bss_ies *ies;
Johannes Bergb629ea32012-11-28 22:14:56 +010080
81 if (WARN_ON(atomic_read(&bss->hold)))
82 return;
83
Johannes Berg9caf0362012-11-29 01:25:20 +010084 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
Johannes Berg776b3582013-02-01 02:06:18 +010085 if (ies && !bss->pub.hidden_beacon_bss)
Johannes Berg9caf0362012-11-29 01:25:20 +010086 kfree_rcu(ies, rcu_head);
87 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
88 if (ies)
89 kfree_rcu(ies, rcu_head);
Amitkumar Karware8e27c62012-10-11 21:03:33 -070090
Johannes Berg776b3582013-02-01 02:06:18 +010091 /*
92 * This happens when the module is removed, it doesn't
93 * really matter any more save for completeness
94 */
95 if (!list_empty(&bss->hidden_list))
96 list_del(&bss->hidden_list);
97
Amitkumar Karware8e27c62012-10-11 21:03:33 -070098 kfree(bss);
99}
100
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800101static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
Johannes Berg776b3582013-02-01 02:06:18 +0100102 struct cfg80211_internal_bss *bss)
Johannes Berg0532d4f2013-02-01 01:34:36 +0100103{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800104 lockdep_assert_held(&rdev->bss_lock);
Johannes Berg776b3582013-02-01 02:06:18 +0100105
106 bss->refcount++;
107 if (bss->pub.hidden_beacon_bss) {
108 bss = container_of(bss->pub.hidden_beacon_bss,
109 struct cfg80211_internal_bss,
110 pub);
111 bss->refcount++;
112 }
Sara Sharon7011ba52019-01-21 12:25:59 +0200113 if (bss->pub.transmitted_bss) {
114 bss = container_of(bss->pub.transmitted_bss,
Sara Sharona3584f56d2019-01-21 12:22:21 +0200115 struct cfg80211_internal_bss,
116 pub);
117 bss->refcount++;
118 }
Johannes Berg0532d4f2013-02-01 01:34:36 +0100119}
120
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800121static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
Johannes Berg776b3582013-02-01 02:06:18 +0100122 struct cfg80211_internal_bss *bss)
Johannes Berg0532d4f2013-02-01 01:34:36 +0100123{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800124 lockdep_assert_held(&rdev->bss_lock);
Johannes Berg776b3582013-02-01 02:06:18 +0100125
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,
130 pub);
131 hbss->refcount--;
132 if (hbss->refcount == 0)
133 bss_free(hbss);
134 }
Sara Sharona3584f56d2019-01-21 12:22:21 +0200135
Sara Sharon7011ba52019-01-21 12:25:59 +0200136 if (bss->pub.transmitted_bss) {
Sara Sharona3584f56d2019-01-21 12:22:21 +0200137 struct cfg80211_internal_bss *tbss;
138
Sara Sharon7011ba52019-01-21 12:25:59 +0200139 tbss = container_of(bss->pub.transmitted_bss,
Sara Sharona3584f56d2019-01-21 12:22:21 +0200140 struct cfg80211_internal_bss,
141 pub);
142 tbss->refcount--;
143 if (tbss->refcount == 0)
144 bss_free(tbss);
145 }
146
Johannes Berg776b3582013-02-01 02:06:18 +0100147 bss->refcount--;
148 if (bss->refcount == 0)
149 bss_free(bss);
Johannes Berg0532d4f2013-02-01 01:34:36 +0100150}
151
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800152static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
Amitkumar Karware8e27c62012-10-11 21:03:33 -0700153 struct cfg80211_internal_bss *bss)
154{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800155 lockdep_assert_held(&rdev->bss_lock);
Johannes Berg4b1af472013-02-01 01:05:43 +0100156
Johannes Berg776b3582013-02-01 02:06:18 +0100157 if (!list_empty(&bss->hidden_list)) {
158 /*
159 * don't remove the beacon entry if it has
160 * probe responses associated with it
161 */
162 if (!bss->pub.hidden_beacon_bss)
163 return false;
164 /*
165 * if it's a probe response entry break its
166 * link to the other entries in the group
167 */
168 list_del_init(&bss->hidden_list);
169 }
170
Amitkumar Karware8e27c62012-10-11 21:03:33 -0700171 list_del_init(&bss->list);
Sara Sharon7011ba52019-01-21 12:25:59 +0200172 list_del_init(&bss->pub.nontrans_list);
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800173 rb_erase(&bss->rbn, &rdev->bss_tree);
Johannes Berg9853a552016-11-15 12:05:11 +0100174 rdev->bss_entries--;
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));
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800178 bss_ref_put(rdev, bss);
Johannes Berg776b3582013-02-01 02:06:18 +0100179 return true;
Amitkumar Karware8e27c62012-10-11 21:03:33 -0700180}
181
Peng Xu0b8fb822019-01-21 12:14:57 +0200182static 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)
185{
186 u8 *pos, *tmp;
187 const u8 *tmp_old, *tmp_new;
188 u8 *sub_copy;
189
190 /* copy subelement as we need to change its content to
191 * mark an ie after it is processed.
192 */
193 sub_copy = kmalloc(subie_len, gfp);
194 if (!sub_copy)
195 return 0;
196 memcpy(sub_copy, subelement, subie_len);
197
198 pos = &new_ie[0];
199
200 /* set new ssid */
201 tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
202 if (tmp_new) {
203 memcpy(pos, tmp_new, tmp_new[1] + 2);
204 pos += (tmp_new[1] + 2);
205 }
206
207 /* go through IEs in ie (skip SSID) and subelement,
208 * merge them into new_ie
209 */
210 tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
211 tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
212
213 while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
214 if (tmp_old[0] == 0) {
215 tmp_old++;
216 continue;
217 }
218
Sara Sharonc17fe042019-01-29 14:00:58 +0200219 if (tmp_old[0] == WLAN_EID_EXTENSION)
220 tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
221 subie_len);
222 else
223 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
224 subie_len);
225
Peng Xu0b8fb822019-01-21 12:14:57 +0200226 if (!tmp) {
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;
231 }
232 } else {
233 /* ie in transmitting ie also in subelement,
234 * copy from subelement and flag the ie in subelement
Sara Sharonc17fe042019-01-29 14:00:58 +0200235 * as copied (by setting eid field to WLAN_EID_SSID,
236 * which is skipped anyway).
237 * For vendor ie, compare OUI + type + subType to
Peng Xu0b8fb822019-01-21 12:14:57 +0200238 * determine if they are the same ie.
239 */
240 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
241 if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
242 /* same vendor ie, copy from
243 * subelement
244 */
245 memcpy(pos, tmp, tmp[1] + 2);
246 pos += tmp[1] + 2;
Sara Sharonc17fe042019-01-29 14:00:58 +0200247 tmp[0] = WLAN_EID_SSID;
Peng Xu0b8fb822019-01-21 12:14:57 +0200248 } else {
249 memcpy(pos, tmp_old, tmp_old[1] + 2);
250 pos += tmp_old[1] + 2;
251 }
252 } else {
253 /* copy ie from subelement into new ie */
254 memcpy(pos, tmp, tmp[1] + 2);
255 pos += tmp[1] + 2;
Sara Sharonc17fe042019-01-29 14:00:58 +0200256 tmp[0] = WLAN_EID_SSID;
Peng Xu0b8fb822019-01-21 12:14:57 +0200257 }
258 }
259
260 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
261 break;
262
263 tmp_old += tmp_old[1] + 2;
264 }
265
266 /* go through subelement again to check if there is any ie not
267 * copied to new ie, skip ssid, capability, bssid-index ie
268 */
269 tmp_new = sub_copy;
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 ||
Sara Sharonc17fe042019-01-29 14:00:58 +0200273 tmp_new[0] == WLAN_EID_MULTI_BSSID_IDX)) {
Peng Xu0b8fb822019-01-21 12:14:57 +0200274 memcpy(pos, tmp_new, tmp_new[1] + 2);
275 pos += tmp_new[1] + 2;
276 }
277 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
278 break;
279 tmp_new += tmp_new[1] + 2;
280 }
281
282 kfree(sub_copy);
283 return pos - new_ie;
284}
285
286static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
287 const u8 *ssid, size_t ssid_len)
288{
289 const struct cfg80211_bss_ies *ies;
290 const u8 *ssidie;
291
292 if (bssid && !ether_addr_equal(a->bssid, bssid))
293 return false;
294
295 if (!ssid)
296 return true;
297
298 ies = rcu_access_pointer(a->ies);
299 if (!ies)
300 return false;
301 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
302 if (!ssidie)
303 return false;
304 if (ssidie[1] != ssid_len)
305 return false;
306 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
307}
308
309static int
Sara Sharon7011ba52019-01-21 12:25:59 +0200310cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
311 struct cfg80211_bss *nontrans_bss)
Peng Xu0b8fb822019-01-21 12:14:57 +0200312{
313 const u8 *ssid;
314 size_t ssid_len;
Sara Sharon7011ba52019-01-21 12:25:59 +0200315 struct cfg80211_bss *bss = NULL;
Peng Xu0b8fb822019-01-21 12:14:57 +0200316
317 rcu_read_lock();
Sara Sharon7011ba52019-01-21 12:25:59 +0200318 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
Peng Xu0b8fb822019-01-21 12:14:57 +0200319 if (!ssid) {
320 rcu_read_unlock();
321 return -EINVAL;
322 }
323 ssid_len = ssid[1];
324 ssid = ssid + 2;
325 rcu_read_unlock();
326
327 /* check if nontrans_bss is in the list */
328 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
Sara Sharon7011ba52019-01-21 12:25:59 +0200329 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
Peng Xu0b8fb822019-01-21 12:14:57 +0200330 return 0;
331 }
332
333 /* add to the list */
334 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
335 return 0;
336}
337
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800338static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
Sam Leffler15d60302012-10-11 21:03:34 -0700339 unsigned long expire_time)
340{
341 struct cfg80211_internal_bss *bss, *tmp;
342 bool expired = false;
343
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800344 lockdep_assert_held(&rdev->bss_lock);
Johannes Berg4b1af472013-02-01 01:05:43 +0100345
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800346 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
Sam Leffler15d60302012-10-11 21:03:34 -0700347 if (atomic_read(&bss->hold))
348 continue;
349 if (!time_after(expire_time, bss->ts))
350 continue;
351
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800352 if (__cfg80211_unlink_bss(rdev, bss))
Johannes Berg776b3582013-02-01 02:06:18 +0100353 expired = true;
Sam Leffler15d60302012-10-11 21:03:34 -0700354 }
355
356 if (expired)
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800357 rdev->bss_generation++;
Sam Leffler15d60302012-10-11 21:03:34 -0700358}
359
Johannes Berg9853a552016-11-15 12:05:11 +0100360static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
361{
362 struct cfg80211_internal_bss *bss, *oldest = NULL;
363 bool ret;
364
365 lockdep_assert_held(&rdev->bss_lock);
366
367 list_for_each_entry(bss, &rdev->bss_list, list) {
368 if (atomic_read(&bss->hold))
369 continue;
370
371 if (!list_empty(&bss->hidden_list) &&
372 !bss->pub.hidden_beacon_bss)
373 continue;
374
375 if (oldest && time_before(oldest->ts, bss->ts))
376 continue;
377 oldest = bss;
378 }
379
380 if (WARN_ON(!oldest))
381 return false;
382
383 /*
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
386 * it here.
387 */
388
389 ret = __cfg80211_unlink_bss(rdev, oldest);
390 WARN_ON(!ret);
391 return ret;
392}
393
Johannes Bergf9d15d12014-01-22 11:14:19 +0200394void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
395 bool send_message)
Johannes Berg2a519312009-02-10 21:25:55 +0100396{
Johannes Berg667503d2009-07-07 03:56:11 +0200397 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +0200398 struct wireless_dev *wdev;
Johannes Bergf9d15d12014-01-22 11:14:19 +0200399 struct sk_buff *msg;
Johannes Berg3d23e342009-09-29 23:27:28 +0200400#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100401 union iwreq_data wrqu;
402#endif
403
Johannes Berg5fe231e2013-05-08 21:45:15 +0200404 ASSERT_RTNL();
Johannes Berg01a0ac42009-08-20 21:36:16 +0200405
Johannes Bergf9d15d12014-01-22 11:14:19 +0200406 if (rdev->scan_msg) {
Arend Van Spriel505a2e82016-12-16 11:21:54 +0000407 nl80211_send_scan_msg(rdev, rdev->scan_msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200408 rdev->scan_msg = NULL;
409 return;
410 }
Johannes Berg667503d2009-07-07 03:56:11 +0200411
Johannes Bergf9d15d12014-01-22 11:14:19 +0200412 request = rdev->scan_req;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200413 if (!request)
414 return;
415
Johannes Bergfd014282012-06-18 19:17:03 +0200416 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +0100417
Johannes Berg6829c872009-07-02 09:13:27 +0200418 /*
419 * This must be before sending the other events!
420 * Otherwise, wpa_supplicant gets completely confused with
421 * wext events.
422 */
Johannes Bergfd014282012-06-18 19:17:03 +0200423 if (wdev->netdev)
424 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200425
Avraham Stern1d762502016-07-05 17:10:13 +0300426 if (!request->info.aborted &&
Johannes Bergf9d15d12014-01-22 11:14:19 +0200427 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);
Sam Leffler15d60302012-10-11 21:03:34 -0700432 }
Johannes Berg2a519312009-02-10 21:25:55 +0100433
Avraham Stern1d762502016-07-05 17:10:13 +0300434 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200435
Johannes Berg3d23e342009-09-29 23:27:28 +0200436#ifdef CONFIG_CFG80211_WEXT
Avraham Stern1d762502016-07-05 17:10:13 +0300437 if (wdev->netdev && !request->info.aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100438 memset(&wrqu, 0, sizeof(wrqu));
439
Johannes Bergfd014282012-06-18 19:17:03 +0200440 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100441 }
442#endif
443
Johannes Bergfd014282012-06-18 19:17:03 +0200444 if (wdev->netdev)
445 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100446
Johannes Berg36e6fea2009-08-12 22:21:21 +0200447 rdev->scan_req = NULL;
Eliad Peller4a58e7c2013-12-05 18:30:17 +0200448 kfree(request);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200449
450 if (!send_message)
451 rdev->scan_msg = msg;
452 else
Arend Van Spriel505a2e82016-12-16 11:21:54 +0000453 nl80211_send_scan_msg(rdev, msg);
Johannes Berg2a519312009-02-10 21:25:55 +0100454}
Johannes Berg667503d2009-07-07 03:56:11 +0200455
Johannes Berg36e6fea2009-08-12 22:21:21 +0200456void __cfg80211_scan_done(struct work_struct *wk)
457{
458 struct cfg80211_registered_device *rdev;
459
460 rdev = container_of(wk, struct cfg80211_registered_device,
461 scan_done_wk);
462
Johannes Berg5fe231e2013-05-08 21:45:15 +0200463 rtnl_lock();
Johannes Bergf9d15d12014-01-22 11:14:19 +0200464 ___cfg80211_scan_done(rdev, true);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200465 rtnl_unlock();
Johannes Berg36e6fea2009-08-12 22:21:21 +0200466}
467
Avraham Stern1d762502016-07-05 17:10:13 +0300468void cfg80211_scan_done(struct cfg80211_scan_request *request,
469 struct cfg80211_scan_info *info)
Johannes Berg667503d2009-07-07 03:56:11 +0200470{
Avraham Stern1d762502016-07-05 17:10:13 +0300471 trace_cfg80211_scan_done(request, info);
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800472 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
Johannes Berg667503d2009-07-07 03:56:11 +0200473
Avraham Stern1d762502016-07-05 17:10:13 +0300474 request->info = *info;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200475 request->notified = true;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800476 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +0200477}
Johannes Berg2a519312009-02-10 21:25:55 +0100478EXPORT_SYMBOL(cfg80211_scan_done);
479
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100480void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
481 struct cfg80211_sched_scan_request *req)
482{
483 ASSERT_RTNL();
484
485 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
486}
487
488static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
489 struct cfg80211_sched_scan_request *req)
490{
491 ASSERT_RTNL();
492
493 list_del_rcu(&req->list);
494 kfree_rcu(req, rcu_head);
495}
496
497static struct cfg80211_sched_scan_request *
498cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
499{
500 struct cfg80211_sched_scan_request *pos;
501
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100502 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100503
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100504 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100505 if (pos->reqid == reqid)
506 return pos;
507 }
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100508 return NULL;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100509}
510
511/*
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.
518 */
519int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
520 bool want_multi)
521{
522 struct cfg80211_sched_scan_request *pos;
523 int i = 0;
524
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)
528 return -EINPROGRESS;
529 i++;
530 }
531
532 if (i) {
533 /* no legacy allowed when multi request(s) are active */
534 if (!want_multi)
535 return -EINPROGRESS;
536
537 /* resource limit reached */
538 if (i == rdev->wiphy.max_sched_scan_reqs)
539 return -ENOSPC;
540 }
541 return 0;
542}
543
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100544void cfg80211_sched_scan_results_wk(struct work_struct *work)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300545{
546 struct cfg80211_registered_device *rdev;
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100547 struct cfg80211_sched_scan_request *req, *tmp;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300548
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100549 rdev = container_of(work, struct cfg80211_registered_device,
550 sched_scan_res_wk);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300551
Johannes Berg5fe231e2013-05-08 21:45:15 +0200552 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100553 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;
562 }
563 nl80211_send_sched_scan(req,
564 NL80211_CMD_SCHED_SCAN_RESULTS);
Sam Leffler15d60302012-10-11 21:03:34 -0700565 }
Sam Leffler15d60302012-10-11 21:03:34 -0700566 }
Johannes Berg5fe231e2013-05-08 21:45:15 +0200567 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300568}
569
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100570void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300571{
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100572 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
573 struct cfg80211_sched_scan_request *request;
574
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100575 trace_cfg80211_sched_scan_results(wiphy, reqid);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300576 /* ignore if we're not scanning */
Jukka Rissanen31a60ed2014-12-15 13:25:38 +0200577
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100578 rcu_read_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100579 request = cfg80211_find_sched_scan_req(rdev, reqid);
580 if (request) {
581 request->report_results = true;
582 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
583 }
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100584 rcu_read_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300585}
586EXPORT_SYMBOL(cfg80211_sched_scan_results);
587
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100588void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300589{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800590 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300591
Eliad Peller792e6aa2014-04-30 16:14:23 +0300592 ASSERT_RTNL();
593
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100594 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
Beni Lev4ee3e062012-08-27 12:49:39 +0300595
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100596 __cfg80211_stop_sched_scan(rdev, reqid, true);
Eliad Peller792e6aa2014-04-30 16:14:23 +0300597}
598EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
599
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100600void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
Eliad Peller792e6aa2014-04-30 16:14:23 +0300601{
602 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100603 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200604 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300605}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300606EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
607
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100608int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
609 struct cfg80211_sched_scan_request *req,
610 bool driver_initiated)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300611{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200612 ASSERT_RTNL();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300613
Luciano Coelho85a99942011-05-12 16:28:29 +0300614 if (!driver_initiated) {
Arend Van Spriel3a3ecf12017-04-21 13:05:02 +0100615 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
Luciano Coelho85a99942011-05-12 16:28:29 +0300616 if (err)
617 return err;
618 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300619
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100620 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300621
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100622 cfg80211_del_sched_scan_req(rdev, req);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300623
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200624 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300625}
626
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100627int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
628 u64 reqid, bool driver_initiated)
629{
630 struct cfg80211_sched_scan_request *sched_scan_req;
631
632 ASSERT_RTNL();
633
634 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100635 if (!sched_scan_req)
636 return -ENOENT;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100637
638 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
639 driver_initiated);
640}
641
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800642void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500643 unsigned long age_secs)
644{
645 struct cfg80211_internal_bss *bss;
646 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
647
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800648 spin_lock_bh(&rdev->bss_lock);
649 list_for_each_entry(bss, &rdev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500650 bss->ts -= age_jiffies;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800651 spin_unlock_bh(&rdev->bss_lock);
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500652}
653
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800654void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
Johannes Berg2a519312009-02-10 21:25:55 +0100655{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800656 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100657}
658
Johannes Berg49a68e02019-02-07 23:26:38 +0100659const struct element *
660cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
661 const u8 *match, unsigned int match_len,
662 unsigned int match_offset)
Johannes Berg2a519312009-02-10 21:25:55 +0100663{
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100664 const struct element *elem;
665
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100666 for_each_element_id(elem, eid, ies, len) {
Johannes Berg49a68e02019-02-07 23:26:38 +0100667 if (elem->datalen >= match_offset + match_len &&
668 !memcmp(elem->data + match_offset, match, match_len))
669 return elem;
Johannes Berg2a519312009-02-10 21:25:55 +0100670 }
Luca Coelhofbd05e42016-09-15 18:15:09 +0300671
672 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100673}
Johannes Berg49a68e02019-02-07 23:26:38 +0100674EXPORT_SYMBOL(cfg80211_find_elem_match);
Johannes Berg2a519312009-02-10 21:25:55 +0100675
Johannes Berg49a68e02019-02-07 23:26:38 +0100676const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
677 const u8 *ies,
678 unsigned int len)
Eliad Peller0c28ec52011-09-15 11:53:01 +0300679{
Johannes Berg49a68e02019-02-07 23:26:38 +0100680 const struct element *elem;
Luca Coelhofbd05e42016-09-15 18:15:09 +0300681 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
682 int match_len = (oui_type < 0) ? 3 : sizeof(match);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300683
Emmanuel Grumbach9e9ea432016-05-03 16:08:07 +0300684 if (WARN_ON(oui_type > 0xff))
685 return NULL;
686
Johannes Berg49a68e02019-02-07 23:26:38 +0100687 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
688 match, match_len, 0);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300689
Johannes Berg49a68e02019-02-07 23:26:38 +0100690 if (!elem || elem->datalen < 4)
Luca Coelhofbd05e42016-09-15 18:15:09 +0300691 return NULL;
Luciano Coelho67194292013-02-12 20:11:38 +0200692
Johannes Berg49a68e02019-02-07 23:26:38 +0100693 return elem;
Eliad Peller0c28ec52011-09-15 11:53:01 +0300694}
Johannes Berg49a68e02019-02-07 23:26:38 +0100695EXPORT_SYMBOL(cfg80211_find_vendor_elem);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300696
Johannes Berg4593c4c2013-02-01 19:20:03 +0100697/**
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
702 */
703enum bss_compare_mode {
704 BSS_CMP_REGULAR,
705 BSS_CMP_HIDE_ZLEN,
706 BSS_CMP_HIDE_NUL,
707};
708
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100709static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100710 struct cfg80211_bss *b,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100711 enum bss_compare_mode mode)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100712{
Johannes Berg9caf0362012-11-29 01:25:20 +0100713 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg3af63412013-01-30 00:40:20 +0100714 const u8 *ie1 = NULL;
715 const u8 *ie2 = NULL;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100716 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100717
Johannes Berg3af63412013-01-30 00:40:20 +0100718 if (a->channel != b->channel)
719 return b->channel->center_freq - a->channel->center_freq;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100720
Johannes Berg9caf0362012-11-29 01:25:20 +0100721 a_ies = rcu_access_pointer(a->ies);
722 if (!a_ies)
723 return -1;
724 b_ies = rcu_access_pointer(b->ies);
725 if (!b_ies)
726 return 1;
727
Johannes Berg3af63412013-01-30 00:40:20 +0100728 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);
734 if (ie1 && ie2) {
735 int mesh_id_cmp;
736
737 if (ie1[1] == ie2[1])
738 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
739 else
740 mesh_id_cmp = ie2[1] - ie1[1];
741
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);
746 if (ie1 && ie2) {
747 if (mesh_id_cmp)
748 return mesh_id_cmp;
749 if (ie1[1] != ie2[1])
750 return ie2[1] - ie1[1];
751 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
752 }
753 }
754
Johannes Berg3af63412013-01-30 00:40:20 +0100755 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
756 if (r)
757 return r;
758
Johannes Berg9caf0362012-11-29 01:25:20 +0100759 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);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100761
Johannes Berg5622f5b2013-01-30 00:26:45 +0100762 if (!ie1 && !ie2)
763 return 0;
764
Johannes Bergf94f8b12012-11-28 22:42:34 +0100765 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100766 * Note that with "hide_ssid", the function returns a match if
767 * the already-present BSS ("b") is a hidden SSID beacon for
768 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100769 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100770
771 /* sort missing IE before (left of) present IE */
772 if (!ie1)
773 return -1;
774 if (!ie2)
775 return 1;
776
Johannes Berg4593c4c2013-02-01 19:20:03 +0100777 switch (mode) {
778 case BSS_CMP_HIDE_ZLEN:
779 /*
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.
787 *
788 * No content comparison is needed as we assume
789 * the content length is zero.
790 */
791 return ie2[1];
792 case BSS_CMP_REGULAR:
793 default:
794 /* sort by length first, then by contents */
795 if (ie1[1] != ie2[1])
796 return ie2[1] - ie1[1];
Johannes Berg5622f5b2013-01-30 00:26:45 +0100797 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100798 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++)
803 if (ie2[i + 2])
804 return -1;
805 return 0;
806 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100807}
808
Dedy Lansky6eb18132015-02-08 15:52:03 +0200809static bool cfg80211_bss_type_match(u16 capability,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200810 enum nl80211_band band,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200811 enum ieee80211_bss_type bss_type)
812{
813 bool ret = true;
814 u16 mask, val;
815
816 if (bss_type == IEEE80211_BSS_TYPE_ANY)
817 return ret;
818
Johannes Berg57fbcce2016-04-12 15:56:15 +0200819 if (band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200820 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
821 switch (bss_type) {
822 case IEEE80211_BSS_TYPE_ESS:
823 val = WLAN_CAPABILITY_DMG_TYPE_AP;
824 break;
825 case IEEE80211_BSS_TYPE_PBSS:
826 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
827 break;
828 case IEEE80211_BSS_TYPE_IBSS:
829 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
830 break;
831 default:
832 return false;
833 }
834 } else {
835 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
836 switch (bss_type) {
837 case IEEE80211_BSS_TYPE_ESS:
838 val = WLAN_CAPABILITY_ESS;
839 break;
840 case IEEE80211_BSS_TYPE_IBSS:
841 val = WLAN_CAPABILITY_IBSS;
842 break;
843 case IEEE80211_BSS_TYPE_MBSS:
844 val = 0;
845 break;
846 default:
847 return false;
848 }
849 }
850
851 ret = ((capability & mask) == val);
852 return ret;
853}
854
Ben Greear0e3a39b2013-06-19 14:06:27 -0700855/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100856struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
857 struct ieee80211_channel *channel,
858 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100859 const u8 *ssid, size_t ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200860 enum ieee80211_bss_type bss_type,
861 enum ieee80211_privacy privacy)
Johannes Berg2a519312009-02-10 21:25:55 +0100862{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800863 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +0100864 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200865 unsigned long now = jiffies;
Dedy Lansky6eb18132015-02-08 15:52:03 +0200866 int bss_privacy;
Johannes Berg2a519312009-02-10 21:25:55 +0100867
Dedy Lansky6eb18132015-02-08 15:52:03 +0200868 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
869 privacy);
Beni Lev4ee3e062012-08-27 12:49:39 +0300870
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800871 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100872
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800873 list_for_each_entry(bss, &rdev->bss_list, list) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200874 if (!cfg80211_bss_type_match(bss->pub.capability,
875 bss->pub.channel->band, bss_type))
876 continue;
877
878 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
879 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
880 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
Johannes Berg79420f02009-02-10 21:25:59 +0100881 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100882 if (channel && bss->pub.channel != channel)
883 continue;
Johannes Bergc14a7402014-04-09 22:36:50 +0200884 if (!is_valid_ether_addr(bss->pub.bssid))
885 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200886 /* Don't get expired BSS structs */
887 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
888 !atomic_read(&bss->hold))
889 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100890 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
891 res = bss;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800892 bss_ref_get(rdev, res);
Johannes Berg2a519312009-02-10 21:25:55 +0100893 break;
894 }
895 }
896
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800897 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100898 if (!res)
899 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300900 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100901 return &res->pub;
902}
903EXPORT_SYMBOL(cfg80211_get_bss);
904
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800905static void rb_insert_bss(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +0100906 struct cfg80211_internal_bss *bss)
907{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800908 struct rb_node **p = &rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +0100909 struct rb_node *parent = NULL;
910 struct cfg80211_internal_bss *tbss;
911 int cmp;
912
913 while (*p) {
914 parent = *p;
915 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
916
Johannes Berg4593c4c2013-02-01 19:20:03 +0100917 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100918
919 if (WARN_ON(!cmp)) {
920 /* will sort of leak this BSS */
921 return;
922 }
923
924 if (cmp < 0)
925 p = &(*p)->rb_left;
926 else
927 p = &(*p)->rb_right;
928 }
929
930 rb_link_node(&bss->rbn, parent, p);
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800931 rb_insert_color(&bss->rbn, &rdev->bss_tree);
Johannes Berg2a519312009-02-10 21:25:55 +0100932}
933
934static struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800935rb_find_bss(struct cfg80211_registered_device *rdev,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100936 struct cfg80211_internal_bss *res,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100937 enum bss_compare_mode mode)
Johannes Berg2a519312009-02-10 21:25:55 +0100938{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800939 struct rb_node *n = rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +0100940 struct cfg80211_internal_bss *bss;
941 int r;
942
943 while (n) {
944 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100945 r = cmp_bss(&res->pub, &bss->pub, mode);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100946
947 if (r == 0)
948 return bss;
949 else if (r < 0)
950 n = n->rb_left;
951 else
952 n = n->rb_right;
953 }
954
955 return NULL;
956}
957
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800958static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
Johannes Berg776b3582013-02-01 02:06:18 +0100959 struct cfg80211_internal_bss *new)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100960{
Johannes Berg9caf0362012-11-29 01:25:20 +0100961 const struct cfg80211_bss_ies *ies;
Johannes Berg776b3582013-02-01 02:06:18 +0100962 struct cfg80211_internal_bss *bss;
963 const u8 *ie;
964 int i, ssidlen;
965 u8 fold = 0;
Johannes Berg9853a552016-11-15 12:05:11 +0100966 u32 n_entries = 0;
Johannes Berg9caf0362012-11-29 01:25:20 +0100967
Johannes Berg776b3582013-02-01 02:06:18 +0100968 ies = rcu_access_pointer(new->pub.beacon_ies);
Johannes Berg9caf0362012-11-29 01:25:20 +0100969 if (WARN_ON(!ies))
Johannes Berg776b3582013-02-01 02:06:18 +0100970 return false;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100971
Johannes Berg776b3582013-02-01 02:06:18 +0100972 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
973 if (!ie) {
974 /* nothing to do */
975 return true;
976 }
977
978 ssidlen = ie[1];
979 for (i = 0; i < ssidlen; i++)
980 fold |= ie[2 + i];
981
982 if (fold) {
983 /* not a hidden SSID */
984 return true;
985 }
986
987 /* This is the bad part ... */
988
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800989 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg9853a552016-11-15 12:05:11 +0100990 /*
991 * we're iterating all the entries anyway, so take the
992 * opportunity to validate the list length accounting
993 */
994 n_entries++;
995
Johannes Berg776b3582013-02-01 02:06:18 +0100996 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
997 continue;
998 if (bss->pub.channel != new->pub.channel)
999 continue;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02001000 if (bss->pub.scan_width != new->pub.scan_width)
1001 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001002 if (rcu_access_pointer(bss->pub.beacon_ies))
1003 continue;
1004 ies = rcu_access_pointer(bss->pub.ies);
1005 if (!ies)
1006 continue;
1007 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1008 if (!ie)
1009 continue;
1010 if (ssidlen && ie[1] != ssidlen)
1011 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001012 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1013 continue;
1014 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1015 list_del(&bss->hidden_list);
1016 /* combine them */
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);
1022 }
1023
Johannes Berg9853a552016-11-15 12:05:11 +01001024 WARN_ONCE(n_entries != rdev->bss_entries,
1025 "rdev bss entries[%d]/list[len:%d] corruption\n",
1026 rdev->bss_entries, n_entries);
1027
Johannes Berg776b3582013-02-01 02:06:18 +01001028 return true;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001029}
1030
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001031struct cfg80211_non_tx_bss {
1032 struct cfg80211_bss *tx_bss;
1033 u8 max_bssid_indicator;
1034 u8 bssid_index;
1035};
1036
Ben Greear0e3a39b2013-06-19 14:06:27 -07001037/* Returned bss is reference counted and must be cleaned up appropriately. */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001038static struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001039cfg80211_bss_update(struct cfg80211_registered_device *rdev,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001040 struct cfg80211_internal_bss *tmp,
1041 bool signal_valid)
Johannes Berg2a519312009-02-10 21:25:55 +01001042{
1043 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001044
Johannes Berg9caf0362012-11-29 01:25:20 +01001045 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +01001046 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001047
Johannes Berg9caf0362012-11-29 01:25:20 +01001048 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001049
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001050 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001051
Johannes Berg9caf0362012-11-29 01:25:20 +01001052 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001053 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg9caf0362012-11-29 01:25:20 +01001054 return NULL;
1055 }
1056
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001057 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +01001058
Johannes Bergcd1658f2009-04-16 15:00:58 +02001059 if (found) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001060 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +01001061 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1062 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +02001063
Johannes Berg9caf0362012-11-29 01:25:20 +01001064 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +02001065
Johannes Berg9caf0362012-11-29 01:25:20 +01001066 rcu_assign_pointer(found->pub.proberesp_ies,
1067 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001068 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +01001069 rcu_assign_pointer(found->pub.ies,
1070 tmp->pub.proberesp_ies);
1071 if (old)
1072 kfree_rcu((struct cfg80211_bss_ies *)old,
1073 rcu_head);
1074 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
Johannes Berg9537f222013-02-01 01:19:48 +01001075 const struct cfg80211_bss_ies *old;
Johannes Berg776b3582013-02-01 02:06:18 +01001076 struct cfg80211_internal_bss *bss;
1077
1078 if (found->pub.hidden_beacon_bss &&
1079 !list_empty(&found->hidden_list)) {
Johannes Berg1345ee62013-03-06 10:31:05 +01001080 const struct cfg80211_bss_ies *f;
1081
Johannes Berg776b3582013-02-01 02:06:18 +01001082 /*
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.
1090 */
Johannes Berg1345ee62013-03-06 10:31:05 +01001091
1092 f = rcu_access_pointer(tmp->pub.beacon_ies);
1093 kfree_rcu((struct cfg80211_bss_ies *)f,
1094 rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +01001095 goto drop;
1096 }
Johannes Berg915de2f2012-11-28 22:39:37 +01001097
Johannes Berg9caf0362012-11-29 01:25:20 +01001098 old = rcu_access_pointer(found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001099
Johannes Berg9caf0362012-11-29 01:25:20 +01001100 rcu_assign_pointer(found->pub.beacon_ies,
1101 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +01001102
1103 /* Override IEs if they were from a beacon before */
Johannes Berg9537f222013-02-01 01:19:48 +01001104 if (old == rcu_access_pointer(found->pub.ies))
Johannes Berg9caf0362012-11-29 01:25:20 +01001105 rcu_assign_pointer(found->pub.ies,
1106 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +02001107
Johannes Berg776b3582013-02-01 02:06:18 +01001108 /* Assign beacon IEs to all sub entries */
1109 list_for_each_entry(bss, &found->hidden_list,
1110 hidden_list) {
1111 const struct cfg80211_bss_ies *ies;
1112
1113 ies = rcu_access_pointer(bss->pub.beacon_ies);
1114 WARN_ON(ies != old);
1115
1116 rcu_assign_pointer(bss->pub.beacon_ies,
1117 tmp->pub.beacon_ies);
1118 }
1119
Johannes Berg9caf0362012-11-29 01:25:20 +01001120 if (old)
1121 kfree_rcu((struct cfg80211_bss_ies *)old,
1122 rcu_head);
1123 }
Johannes Berg1345ee62013-03-06 10:31:05 +01001124
1125 found->pub.beacon_interval = tmp->pub.beacon_interval;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001126 /*
1127 * don't update the signal if beacon was heard on
1128 * adjacent channel.
1129 */
1130 if (signal_valid)
1131 found->pub.signal = tmp->pub.signal;
Johannes Berg1345ee62013-03-06 10:31:05 +01001132 found->pub.capability = tmp->pub.capability;
1133 found->ts = tmp->ts;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001134 found->ts_boottime = tmp->ts_boottime;
Avraham Stern1d762502016-07-05 17:10:13 +03001135 found->parent_tsf = tmp->parent_tsf;
Sunil Dutt983dafa2017-12-13 19:51:36 +02001136 found->pub.chains = tmp->pub.chains;
1137 memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
1138 IEEE80211_MAX_CHAINS);
Avraham Stern1d762502016-07-05 17:10:13 +03001139 ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001140 found->pub.max_bssid_indicator = tmp->pub.max_bssid_indicator;
1141 found->pub.bssid_index = tmp->pub.bssid_index;
Johannes Berg2a519312009-02-10 21:25:55 +01001142 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +01001143 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001144 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +01001145 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001146
Johannes Berg9caf0362012-11-29 01:25:20 +01001147 /*
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
1151 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001152 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
Johannes Berg9caf0362012-11-29 01:25:20 +01001153 GFP_ATOMIC);
1154 if (!new) {
1155 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1156 if (ies)
1157 kfree_rcu(ies, rcu_head);
1158 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1159 if (ies)
1160 kfree_rcu(ies, rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +01001161 goto drop;
Johannes Berg9caf0362012-11-29 01:25:20 +01001162 }
1163 memcpy(new, tmp, sizeof(*new));
Johannes Berg776b3582013-02-01 02:06:18 +01001164 new->refcount = 1;
1165 INIT_LIST_HEAD(&new->hidden_list);
Sara Sharon7011ba52019-01-21 12:25:59 +02001166 INIT_LIST_HEAD(&new->pub.nontrans_list);
Johannes Berg776b3582013-02-01 02:06:18 +01001167
1168 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001169 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
Johannes Berg776b3582013-02-01 02:06:18 +01001170 if (!hidden)
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001171 hidden = rb_find_bss(rdev, tmp,
Johannes Berg776b3582013-02-01 02:06:18 +01001172 BSS_CMP_HIDE_NUL);
1173 if (hidden) {
1174 new->pub.hidden_beacon_bss = &hidden->pub;
1175 list_add(&new->hidden_list,
1176 &hidden->hidden_list);
1177 hidden->refcount++;
1178 rcu_assign_pointer(new->pub.beacon_ies,
1179 hidden->pub.beacon_ies);
1180 }
1181 } else {
1182 /*
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 ...
1187 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001188 if (!cfg80211_combine_bsses(rdev, new)) {
Johannes Berg776b3582013-02-01 02:06:18 +01001189 kfree(new);
1190 goto drop;
1191 }
1192 }
1193
Johannes Berg9853a552016-11-15 12:05:11 +01001194 if (rdev->bss_entries >= bss_entries_limit &&
1195 !cfg80211_bss_expire_oldest(rdev)) {
1196 kfree(new);
1197 goto drop;
1198 }
1199
Sara Sharona3584f56d2019-01-21 12:22:21 +02001200 /* This must be before the call to bss_ref_get */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001201 if (tmp->pub.transmitted_bss) {
Sara Sharona3584f56d2019-01-21 12:22:21 +02001202 struct cfg80211_internal_bss *pbss =
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001203 container_of(tmp->pub.transmitted_bss,
Sara Sharona3584f56d2019-01-21 12:22:21 +02001204 struct cfg80211_internal_bss,
1205 pub);
1206
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001207 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
Sara Sharona3584f56d2019-01-21 12:22:21 +02001208 bss_ref_get(rdev, pbss);
1209 }
1210
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001211 list_add_tail(&new->list, &rdev->bss_list);
Johannes Berg9853a552016-11-15 12:05:11 +01001212 rdev->bss_entries++;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001213 rb_insert_bss(rdev, new);
Johannes Berg9caf0362012-11-29 01:25:20 +01001214 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +01001215 }
1216
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001217 rdev->bss_generation++;
1218 bss_ref_get(rdev, found);
1219 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001220
Johannes Berg2a519312009-02-10 21:25:55 +01001221 return found;
Johannes Berg776b3582013-02-01 02:06:18 +01001222 drop:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001223 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg776b3582013-02-01 02:06:18 +01001224 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001225}
1226
Jouni Malinen119f94a2018-09-05 18:52:22 +03001227/*
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.
1234 */
Johannes Berg0172bb72012-11-23 14:23:30 +01001235static struct ieee80211_channel *
1236cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001237 struct ieee80211_channel *channel,
1238 enum nl80211_bss_scan_width scan_width)
Johannes Berg0172bb72012-11-23 14:23:30 +01001239{
1240 const u8 *tmp;
1241 u32 freq;
1242 int channel_number = -1;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001243 struct ieee80211_channel *alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001244
1245 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1246 if (tmp && tmp[1] == 1) {
1247 channel_number = tmp[2];
1248 } else {
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);
1252
1253 channel_number = htop->primary_chan;
1254 }
1255 }
1256
Jouni Malinen119f94a2018-09-05 18:52:22 +03001257 if (channel_number < 0) {
1258 /* No channel information in frame payload */
Johannes Berg0172bb72012-11-23 14:23:30 +01001259 return channel;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001260 }
Johannes Berg0172bb72012-11-23 14:23:30 +01001261
1262 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
Jouni Malinen119f94a2018-09-05 18:52:22 +03001263 alt_channel = ieee80211_get_channel(wiphy, freq);
1264 if (!alt_channel) {
1265 if (channel->band == NL80211_BAND_2GHZ) {
1266 /*
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
1270 * channel 11.
1271 */
1272 return NULL;
1273 }
1274
1275 /* No match for the payload channel number - ignore it */
1276 return channel;
1277 }
1278
1279 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1280 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1281 /*
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
1284 * channel numbers.
1285 */
1286 return channel;
1287 }
1288
1289 /*
1290 * Use the channel determined through the payload channel number
1291 * instead of the RX channel reported by the driver.
1292 */
1293 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg0172bb72012-11-23 14:23:30 +01001294 return NULL;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001295 return alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001296}
1297
Ben Greear0e3a39b2013-06-19 14:06:27 -07001298/* Returned bss is reference counted and must be cleaned up appropriately. */
Peng Xu0b8fb822019-01-21 12:14:57 +02001299static struct cfg80211_bss *
1300cfg80211_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,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001305 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001306 gfp_t gfp)
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001307{
Peng Xu0b8fb822019-01-21 12:14:57 +02001308 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg9caf0362012-11-29 01:25:20 +01001309 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001310 struct ieee80211_channel *channel;
Sara Sharon7011ba52019-01-21 12:25:59 +02001311 struct cfg80211_internal_bss tmp = {}, *res;
Dedy Lansky6eb18132015-02-08 15:52:03 +02001312 int bss_type;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001313 bool signal_valid;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001314
1315 if (WARN_ON(!wiphy))
1316 return NULL;
1317
Sujith22fe88d2010-05-13 10:34:08 +05301318 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001319 (data->signal < 0 || data->signal > 100)))
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001320 return NULL;
1321
Jouni Malinen119f94a2018-09-05 18:52:22 +03001322 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1323 data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001324 if (!channel)
1325 return NULL;
1326
Johannes Berg9caf0362012-11-29 01:25:20 +01001327 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1328 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001329 tmp.pub.scan_width = data->scan_width;
1330 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001331 tmp.pub.beacon_interval = beacon_interval;
1332 tmp.pub.capability = capability;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001333 tmp.ts_boottime = data->boottime_ns;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001334 if (non_tx_data) {
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;
1338 }
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001339
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001340 /*
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001341 * If we do not know here whether the IEs are from a Beacon or Probe
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001342 * 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
Johannes Berg50521aa2013-01-30 21:33:19 +01001345 * override the IEs pointer should we have received an earlier
Johannes Berg9caf0362012-11-29 01:25:20 +01001346 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001347 */
Johannes Berg0e227082014-08-12 20:34:30 +02001348 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001349 if (!ies)
1350 return NULL;
1351 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001352 ies->tsf = tsf;
Johannes Berg0e227082014-08-12 20:34:30 +02001353 ies->from_beacon = false;
Johannes Berg9caf0362012-11-29 01:25:20 +01001354 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001355
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001356 switch (ftype) {
1357 case CFG80211_BSS_FTYPE_BEACON:
1358 ies->from_beacon = true;
Luca Coelho925b5972018-12-15 11:03:21 +02001359 /* fall through */
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001360 case CFG80211_BSS_FTYPE_UNKNOWN:
1361 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1362 break;
1363 case CFG80211_BSS_FTYPE_PRESP:
1364 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1365 break;
1366 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001367 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001368
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001369 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001370 wiphy->max_adj_channel_rssi_comp;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001371 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001372 if (!res)
1373 return NULL;
1374
Johannes Berg57fbcce2016-04-12 15:56:15 +02001375 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001376 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);
1380 } else {
1381 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1382 regulatory_hint_found_beacon(wiphy, channel, gfp);
1383 }
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001384
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001385 if (non_tx_data && non_tx_data->tx_bss) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001386 /* this is a nontransmitting bss, we need to add it to
1387 * transmitting bss' list if it is not there
1388 */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001389 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1390 &res->pub)) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001391 if (__cfg80211_unlink_bss(rdev, res))
1392 rdev->bss_generation++;
1393 }
1394 }
1395
Beni Lev4ee3e062012-08-27 12:49:39 +03001396 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001397 /* cfg80211_bss_update gives us a referenced result */
1398 return &res->pub;
1399}
Peng Xu0b8fb822019-01-21 12:14:57 +02001400
1401static 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,
1406 size_t ielen,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001407 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001408 gfp_t gfp)
1409{
Johannes Berg1c8745f2019-02-07 22:36:33 +01001410 const u8 *mbssid_index_ie;
1411 const struct element *elem, *sub;
1412 size_t new_ie_len;
Peng Xu0b8fb822019-01-21 12:14:57 +02001413 u8 new_bssid[ETH_ALEN];
1414 u8 *new_ie;
1415 u16 capability;
1416 struct cfg80211_bss *bss;
1417
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001418 if (!non_tx_data)
Peng Xu0b8fb822019-01-21 12:14:57 +02001419 return;
1420 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1421 return;
Sara Sharon213ed572019-01-16 23:02:03 +02001422 if (!wiphy->support_mbssid)
1423 return;
1424 if (wiphy->support_only_he_mbssid &&
1425 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1426 return;
Peng Xu0b8fb822019-01-21 12:14:57 +02001427
Peng Xu0b8fb822019-01-21 12:14:57 +02001428 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1429 if (!new_ie)
1430 return;
1431
Johannes Berg1c8745f2019-02-07 22:36:33 +01001432 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1433 if (elem->datalen < 4)
1434 continue;
1435 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1436 if (sub->id != 0 || sub->datalen < 4) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001437 /* not a valid BSS profile */
1438 continue;
1439 }
1440
Johannes Berg1c8745f2019-02-07 22:36:33 +01001441 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1442 sub->data[1] != 2) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001443 /* The first element within the Nontransmitted
1444 * BSSID Profile is not the Nontransmitted
1445 * BSSID Capability element.
1446 */
1447 continue;
1448 }
1449
1450 /* found a Nontransmitted BSSID Profile */
1451 mbssid_index_ie = cfg80211_find_ie
1452 (WLAN_EID_MULTI_BSSID_IDX,
Johannes Berg1c8745f2019-02-07 22:36:33 +01001453 sub->data, sub->datalen);
Peng Xu0b8fb822019-01-21 12:14:57 +02001454 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1455 mbssid_index_ie[2] == 0) {
1456 /* No valid Multiple BSSID-Index element */
1457 continue;
1458 }
1459
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001460 non_tx_data->bssid_index = mbssid_index_ie[2];
1461 non_tx_data->max_bssid_indicator = elem->data[0];
1462
1463 cfg80211_gen_new_bssid(bssid,
1464 non_tx_data->max_bssid_indicator,
1465 non_tx_data->bssid_index,
Peng Xu0b8fb822019-01-21 12:14:57 +02001466 new_bssid);
1467 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
Johannes Berg1c8745f2019-02-07 22:36:33 +01001468 new_ie_len = cfg80211_gen_new_ie(ie, ielen, sub->data,
1469 sub->datalen, new_ie,
Peng Xu0b8fb822019-01-21 12:14:57 +02001470 gfp);
1471 if (!new_ie_len)
1472 continue;
1473
Johannes Berg1c8745f2019-02-07 22:36:33 +01001474 capability = get_unaligned_le16(sub->data + 2);
Peng Xu0b8fb822019-01-21 12:14:57 +02001475 bss = cfg80211_inform_single_bss_data(wiphy, data,
1476 ftype,
1477 new_bssid, tsf,
1478 capability,
1479 beacon_interval,
1480 new_ie,
1481 new_ie_len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001482 non_tx_data,
1483 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001484 if (!bss)
1485 break;
1486 cfg80211_put_bss(wiphy, bss);
1487 }
Peng Xu0b8fb822019-01-21 12:14:57 +02001488 }
1489
1490 kfree(new_ie);
1491}
1492
1493struct cfg80211_bss *
1494cfg80211_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,
1499 gfp_t gfp)
1500{
1501 struct cfg80211_bss *res;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001502 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001503
1504 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1505 capability, beacon_interval, ie,
1506 ielen, NULL, gfp);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001507 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001508 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001509 beacon_interval, ie, ielen, &non_tx_data,
1510 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001511 return res;
1512}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001513EXPORT_SYMBOL(cfg80211_inform_bss_data);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001514
Peng Xu0b8fb822019-01-21 12:14:57 +02001515static void
1516cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1517 struct cfg80211_inform_bss *data,
1518 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001519 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001520 gfp_t gfp)
1521{
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);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001526
Peng Xu0b8fb822019-01-21 12:14:57 +02001527 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1528 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1529
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),
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001533 ie, ielen, non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001534}
1535
1536static void
1537cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
Sara Sharon7011ba52019-01-21 12:25:59 +02001538 struct cfg80211_bss *nontrans_bss,
Peng Xu0b8fb822019-01-21 12:14:57 +02001539 struct ieee80211_mgmt *mgmt, size_t len,
1540 gfp_t gfp)
1541{
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);
1546 size_t new_ie_len;
1547 struct cfg80211_bss_ies *new_ies;
1548 const struct cfg80211_bss_ies *old;
1549 u8 cpy_len;
1550
1551 ie = mgmt->u.probe_resp.variable;
1552
1553 new_ie_len = ielen;
1554 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1555 if (!trans_ssid)
1556 return;
1557 new_ie_len -= trans_ssid[1];
1558 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1559 if (!mbssid)
1560 return;
1561 new_ie_len -= mbssid[1];
1562 rcu_read_lock();
Sara Sharon7011ba52019-01-21 12:25:59 +02001563 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
Peng Xu0b8fb822019-01-21 12:14:57 +02001564 if (!nontrans_ssid) {
1565 rcu_read_unlock();
1566 return;
1567 }
1568 new_ie_len += nontrans_ssid[1];
1569 rcu_read_unlock();
1570
1571 /* generate new ie for nontrans BSS
1572 * 1. replace SSID with nontrans BSS' SSID
1573 * 2. skip MBSSID IE
1574 */
1575 new_ie = kzalloc(new_ie_len, gfp);
1576 if (!new_ie)
1577 return;
1578 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp);
Sara Sharonbede8d22019-01-30 08:48:21 +02001579 if (!new_ies)
1580 goto out_free;
Peng Xu0b8fb822019-01-21 12:14:57 +02001581
1582 pos = new_ie;
1583
1584 /* copy the nontransmitted SSID */
1585 cpy_len = nontrans_ssid[1] + 2;
1586 memcpy(pos, nontrans_ssid, cpy_len);
1587 pos += 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)));
1595
1596 /* update ie */
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)) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001602 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);
Peng Xu0b8fb822019-01-21 12:14:57 +02001605 if (old)
1606 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1607 } else {
Sara Sharon7011ba52019-01-21 12:25:59 +02001608 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);
Peng Xu0b8fb822019-01-21 12:14:57 +02001611 if (old)
1612 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1613 }
Sara Sharonbede8d22019-01-30 08:48:21 +02001614
1615out_free:
1616 kfree(new_ie);
Peng Xu0b8fb822019-01-21 12:14:57 +02001617}
1618
1619/* cfg80211_inform_bss_width_frame helper */
1620static struct cfg80211_bss *
1621cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1622 struct cfg80211_inform_bss *data,
1623 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001624 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001625 gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +01001626{
Johannes Berg9caf0362012-11-29 01:25:20 +01001627 struct cfg80211_internal_bss tmp = {}, *res;
1628 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001629 struct ieee80211_channel *channel;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001630 bool signal_valid;
Johannes Berg2a519312009-02-10 21:25:55 +01001631 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1632 u.probe_resp.variable);
Dedy Lansky6eb18132015-02-08 15:52:03 +02001633 int bss_type;
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001634
Johannes Berg0172bb72012-11-23 14:23:30 +01001635 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1636 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1637
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001638 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
Beni Lev4ee3e062012-08-27 12:49:39 +03001639
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001640 if (WARN_ON(!mgmt))
1641 return NULL;
1642
1643 if (WARN_ON(!wiphy))
1644 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001645
Sujith22fe88d2010-05-13 10:34:08 +05301646 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001647 (data->signal < 0 || data->signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +01001648 return NULL;
1649
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001650 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +01001651 return NULL;
1652
Johannes Berg0172bb72012-11-23 14:23:30 +01001653 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001654 ielen, data->chan, data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001655 if (!channel)
1656 return NULL;
1657
Johannes Berg0e227082014-08-12 20:34:30 +02001658 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001659 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001660 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +01001661 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001662 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
Johannes Berg0e227082014-08-12 20:34:30 +02001663 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
Johannes Berg9caf0362012-11-29 01:25:20 +01001664 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +01001665
Johannes Berg9caf0362012-11-29 01:25:20 +01001666 if (ieee80211_is_probe_resp(mgmt->frame_control))
1667 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1668 else
1669 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1670 rcu_assign_pointer(tmp.pub.ies, ies);
Arend Van Spriel505a2e82016-12-16 11:21:54 +00001671
Johannes Berg9caf0362012-11-29 01:25:20 +01001672 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1673 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001674 tmp.pub.scan_width = data->scan_width;
1675 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001676 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);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001678 tmp.ts_boottime = data->boottime_ns;
Avraham Stern1d762502016-07-05 17:10:13 +03001679 tmp.parent_tsf = data->parent_tsf;
Sunil Dutt983dafa2017-12-13 19:51:36 +02001680 tmp.pub.chains = data->chains;
1681 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
Avraham Stern1d762502016-07-05 17:10:13 +03001682 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001683 if (non_tx_data) {
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;
1687 }
Johannes Berg2a519312009-02-10 21:25:55 +01001688
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001689 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001690 wiphy->max_adj_channel_rssi_comp;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001691 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
Johannes Berg2a519312009-02-10 21:25:55 +01001692 if (!res)
1693 return NULL;
1694
Johannes Berg57fbcce2016-04-12 15:56:15 +02001695 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001696 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);
1700 } else {
1701 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1702 regulatory_hint_found_beacon(wiphy, channel, gfp);
1703 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001704
Beni Lev4ee3e062012-08-27 12:49:39 +03001705 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +01001706 /* cfg80211_bss_update gives us a referenced result */
1707 return &res->pub;
1708}
Peng Xu0b8fb822019-01-21 12:14:57 +02001709
1710struct cfg80211_bss *
1711cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1712 struct cfg80211_inform_bss *data,
1713 struct ieee80211_mgmt *mgmt, size_t len,
1714 gfp_t gfp)
1715{
Sara Sharon7011ba52019-01-21 12:25:59 +02001716 struct cfg80211_bss *res, *tmp_bss;
Peng Xu0b8fb822019-01-21 12:14:57 +02001717 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);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001721 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001722
1723 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1724 len, NULL, gfp);
Sara Sharon213ed572019-01-16 23:02:03 +02001725 if (!res || !wiphy->support_mbssid ||
1726 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1727 return res;
1728 if (wiphy->support_only_he_mbssid &&
1729 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
Peng Xu0b8fb822019-01-21 12:14:57 +02001730 return res;
1731
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001732 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001733 /* process each non-transmitting bss */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001734 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1735 &non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001736
1737 /* check if the res has other nontransmitting bss which is not
1738 * in MBSSID IE
1739 */
1740 ies1 = rcu_access_pointer(res->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001741
1742 /* go through nontrans_list, if the timestamp of the BSS is
1743 * earlier than the timestamp of the transmitting BSS then
1744 * update it
1745 */
Sara Sharon7011ba52019-01-21 12:25:59 +02001746 list_for_each_entry(tmp_bss, &res->nontrans_list,
Peng Xu0b8fb822019-01-21 12:14:57 +02001747 nontrans_list) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001748 ies2 = rcu_access_pointer(tmp_bss->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001749 if (ies2->tsf < ies1->tsf)
1750 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1751 mgmt, len, gfp);
1752 }
1753
1754 return res;
1755}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001756EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
Johannes Berg2a519312009-02-10 21:25:55 +01001757
Johannes Berg5b112d32013-02-01 01:49:58 +01001758void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001759{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001760 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001761 struct cfg80211_internal_bss *bss;
1762
1763 if (!pub)
1764 return;
1765
1766 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001767
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001768 spin_lock_bh(&rdev->bss_lock);
1769 bss_ref_get(rdev, bss);
1770 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001771}
1772EXPORT_SYMBOL(cfg80211_ref_bss);
1773
Johannes Berg5b112d32013-02-01 01:49:58 +01001774void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg2a519312009-02-10 21:25:55 +01001775{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001776 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001777 struct cfg80211_internal_bss *bss;
1778
1779 if (!pub)
1780 return;
1781
1782 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001783
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001784 spin_lock_bh(&rdev->bss_lock);
1785 bss_ref_put(rdev, bss);
1786 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001787}
1788EXPORT_SYMBOL(cfg80211_put_bss);
1789
Johannes Bergd491af12009-02-10 21:25:58 +01001790void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1791{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001792 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Sara Sharon7011ba52019-01-21 12:25:59 +02001793 struct cfg80211_internal_bss *bss, *tmp1;
1794 struct cfg80211_bss *nontrans_bss, *tmp;
Johannes Bergd491af12009-02-10 21:25:58 +01001795
1796 if (WARN_ON(!pub))
1797 return;
1798
1799 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1800
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001801 spin_lock_bh(&rdev->bss_lock);
Sara Sharon7011ba52019-01-21 12:25:59 +02001802 if (list_empty(&bss->list))
1803 goto out;
Peng Xu0b8fb822019-01-21 12:14:57 +02001804
Sara Sharon7011ba52019-01-21 12:25:59 +02001805 list_for_each_entry_safe(nontrans_bss, tmp,
1806 &pub->nontrans_list,
1807 nontrans_list) {
1808 tmp1 = container_of(nontrans_bss,
1809 struct cfg80211_internal_bss, pub);
1810 if (__cfg80211_unlink_bss(rdev, tmp1))
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001811 rdev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +02001812 }
Sara Sharon7011ba52019-01-21 12:25:59 +02001813
1814 if (__cfg80211_unlink_bss(rdev, bss))
1815 rdev->bss_generation++;
1816out:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001817 spin_unlock_bh(&rdev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +01001818}
1819EXPORT_SYMBOL(cfg80211_unlink_bss);
1820
Johannes Berg3d23e342009-09-29 23:27:28 +02001821#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9f419f32013-05-08 21:34:22 +02001822static struct cfg80211_registered_device *
1823cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1824{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001825 struct cfg80211_registered_device *rdev;
Johannes Berg9f419f32013-05-08 21:34:22 +02001826 struct net_device *dev;
1827
Johannes Berg5fe231e2013-05-08 21:45:15 +02001828 ASSERT_RTNL();
1829
Johannes Berg9f419f32013-05-08 21:34:22 +02001830 dev = dev_get_by_index(net, ifindex);
1831 if (!dev)
Johannes Berg5fe231e2013-05-08 21:45:15 +02001832 return ERR_PTR(-ENODEV);
1833 if (dev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001834 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001835 else
Johannes Berg9f419f32013-05-08 21:34:22 +02001836 rdev = ERR_PTR(-ENODEV);
1837 dev_put(dev);
Johannes Berg9f419f32013-05-08 21:34:22 +02001838 return rdev;
1839}
1840
Johannes Berg2a519312009-02-10 21:25:55 +01001841int cfg80211_wext_siwscan(struct net_device *dev,
1842 struct iw_request_info *info,
1843 union iwreq_data *wrqu, char *extra)
1844{
1845 struct cfg80211_registered_device *rdev;
1846 struct wiphy *wiphy;
1847 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001848 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001849 int i, err, n_channels = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001850 enum nl80211_band band;
Johannes Berg2a519312009-02-10 21:25:55 +01001851
1852 if (!netif_running(dev))
1853 return -ENETDOWN;
1854
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001855 if (wrqu->data.length == sizeof(struct iw_scan_req))
1856 wreq = (struct iw_scan_req *)extra;
1857
Johannes Berg463d0182009-07-14 00:33:35 +02001858 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001859
1860 if (IS_ERR(rdev))
1861 return PTR_ERR(rdev);
1862
Johannes Bergf9d15d12014-01-22 11:14:19 +02001863 if (rdev->scan_req || rdev->scan_msg) {
Johannes Berg2a519312009-02-10 21:25:55 +01001864 err = -EBUSY;
1865 goto out;
1866 }
1867
1868 wiphy = &rdev->wiphy;
1869
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001870 /* Determine number of channels, needed to allocate creq */
1871 if (wreq && wreq->num_channels)
1872 n_channels = wreq->num_channels;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001873 else
1874 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001875
1876 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1877 n_channels * sizeof(void *),
1878 GFP_ATOMIC);
1879 if (!creq) {
1880 err = -ENOMEM;
1881 goto out;
1882 }
1883
1884 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +02001885 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +02001886 /* SSIDs come after channels */
1887 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01001888 creq->n_channels = n_channels;
1889 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -07001890 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001891
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001892 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +01001893 i = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001894 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg2a519312009-02-10 21:25:55 +01001895 int j;
Johannes Berg584991d2009-11-02 13:32:03 +01001896
Johannes Berg2a519312009-02-10 21:25:55 +01001897 if (!wiphy->bands[band])
1898 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01001899
Johannes Berg2a519312009-02-10 21:25:55 +01001900 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001901 /* ignore disabled channels */
1902 if (wiphy->bands[band]->channels[j].flags &
1903 IEEE80211_CHAN_DISABLED)
1904 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001905
1906 /* If we have a wireless request structure and the
1907 * wireless request specifies frequencies, then search
1908 * for the matching hardware channel.
1909 */
1910 if (wreq && wreq->num_channels) {
1911 int k;
1912 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1913 for (k = 0; k < wreq->num_channels; k++) {
Zhao, Gang96998e32014-04-09 09:28:06 +08001914 struct iw_freq *freq =
1915 &wreq->channel_list[k];
1916 int wext_freq =
1917 cfg80211_wext_freq(freq);
1918
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001919 if (wext_freq == wiphy_freq)
1920 goto wext_freq_found;
1921 }
1922 goto wext_freq_not_found;
1923 }
1924
1925 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001926 creq->channels[i] = &wiphy->bands[band]->channels[j];
1927 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001928 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001929 }
1930 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001931 /* No channels found? */
1932 if (!i) {
1933 err = -EINVAL;
1934 goto out;
1935 }
Johannes Berg2a519312009-02-10 21:25:55 +01001936
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001937 /* Set real number of channels specified in creq->channels[] */
1938 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001939
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001940 /* translate "Scan for SSID" request */
1941 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001942 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001943 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1944 err = -EINVAL;
1945 goto out;
1946 }
Johannes Berg2a519312009-02-10 21:25:55 +01001947 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1948 creq->ssids[0].ssid_len = wreq->essid_len;
1949 }
1950 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1951 creq->n_ssids = 0;
1952 }
1953
Johannes Berg57fbcce2016-04-12 15:56:15 +02001954 for (i = 0; i < NUM_NL80211_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001955 if (wiphy->bands[i])
1956 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001957
Jouni Malinen818965d2016-02-26 22:12:47 +02001958 eth_broadcast_addr(creq->bssid);
1959
Johannes Berg2a519312009-02-10 21:25:55 +01001960 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03001961 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001962 if (err) {
1963 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001964 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001965 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001966 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001967 /* creq now owned by driver */
1968 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001969 dev_hold(dev);
1970 }
Johannes Berg2a519312009-02-10 21:25:55 +01001971 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001972 kfree(creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001973 return err;
1974}
Johannes Berg2afe38d2015-01-06 14:00:53 +01001975EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001976
James Minor76a70e92015-02-24 12:58:20 -06001977static char *ieee80211_scan_add_ies(struct iw_request_info *info,
1978 const struct cfg80211_bss_ies *ies,
1979 char *current_ev, char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001980{
Johannes Berg9caf0362012-11-29 01:25:20 +01001981 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01001982 struct iw_event iwe;
1983
Johannes Berg9caf0362012-11-29 01:25:20 +01001984 if (!ies)
James Minor76a70e92015-02-24 12:58:20 -06001985 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01001986
1987 /*
1988 * If needed, fragment the IEs buffer (at IE boundaries) into short
1989 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1990 */
Johannes Berg9caf0362012-11-29 01:25:20 +01001991 pos = ies->data;
1992 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01001993
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];
1998
1999 memset(&iwe, 0, sizeof(iwe));
2000 iwe.cmd = IWEVGENIE;
2001 iwe.u.data.length = next - pos;
James Minor76a70e92015-02-24 12:58:20 -06002002 current_ev = iwe_stream_add_point_check(info, current_ev,
2003 end_buf, &iwe,
2004 (void *)pos);
2005 if (IS_ERR(current_ev))
2006 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002007 pos = next;
2008 }
2009
2010 if (end > pos) {
2011 memset(&iwe, 0, sizeof(iwe));
2012 iwe.cmd = IWEVGENIE;
2013 iwe.u.data.length = end - pos;
James Minor76a70e92015-02-24 12:58:20 -06002014 current_ev = iwe_stream_add_point_check(info, current_ev,
2015 end_buf, &iwe,
2016 (void *)pos);
2017 if (IS_ERR(current_ev))
2018 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002019 }
James Minor76a70e92015-02-24 12:58:20 -06002020
2021 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002022}
2023
Johannes Berg2a519312009-02-10 21:25:55 +01002024static char *
Johannes Berg77965c972009-02-18 18:45:06 +01002025ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2026 struct cfg80211_internal_bss *bss, char *current_ev,
2027 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01002028{
Johannes Berg9caf0362012-11-29 01:25:20 +01002029 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01002030 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01002031 const u8 *ie;
James Minor76a70e92015-02-24 12:58:20 -06002032 u8 buf[50];
2033 u8 *cfg, *p, *tmp;
Johannes Berg9caf0362012-11-29 01:25:20 +01002034 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002035 bool ismesh = false;
2036
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);
James Minor76a70e92015-02-24 12:58:20 -06002041 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2042 IW_EV_ADDR_LEN);
2043 if (IS_ERR(current_ev))
2044 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002045
2046 memset(&iwe, 0, sizeof(iwe));
2047 iwe.cmd = SIOCGIWFREQ;
2048 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2049 iwe.u.freq.e = 0;
James Minor76a70e92015-02-24 12:58:20 -06002050 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2051 IW_EV_FREQ_LEN);
2052 if (IS_ERR(current_ev))
2053 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002054
2055 memset(&iwe, 0, sizeof(iwe));
2056 iwe.cmd = SIOCGIWFREQ;
2057 iwe.u.freq.m = bss->pub.channel->center_freq;
2058 iwe.u.freq.e = 6;
James Minor76a70e92015-02-24 12:58:20 -06002059 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2060 IW_EV_FREQ_LEN);
2061 if (IS_ERR(current_ev))
2062 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002063
Johannes Berg77965c972009-02-18 18:45:06 +01002064 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01002065 memset(&iwe, 0, sizeof(iwe));
2066 iwe.cmd = IWEVQUAL;
2067 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2068 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01002069 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c972009-02-18 18:45:06 +01002070 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002071 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01002072 sig = bss->pub.signal / 100;
2073 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002074 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01002075 if (sig < -110) /* rather bad */
2076 sig = -110;
2077 else if (sig > -40) /* perfect */
2078 sig = -40;
2079 /* will give a range of 0 .. 70 */
2080 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01002081 break;
2082 case CFG80211_SIGNAL_TYPE_UNSPEC:
2083 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01002084 /* will give range 0 .. 100 */
2085 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01002086 break;
2087 default:
2088 /* not reached */
2089 break;
2090 }
James Minor76a70e92015-02-24 12:58:20 -06002091 current_ev = iwe_stream_add_event_check(info, current_ev,
2092 end_buf, &iwe,
2093 IW_EV_QUAL_LEN);
2094 if (IS_ERR(current_ev))
2095 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002096 }
2097
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;
2102 else
2103 iwe.u.data.flags = IW_ENCODE_DISABLED;
2104 iwe.u.data.length = 0;
James Minor76a70e92015-02-24 12:58:20 -06002105 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2106 &iwe, "");
2107 if (IS_ERR(current_ev))
2108 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002109
Johannes Berg9caf0362012-11-29 01:25:20 +01002110 rcu_read_lock();
2111 ies = rcu_dereference(bss->pub.ies);
Johannes Berg83c7aa12013-02-05 16:51:29 +01002112 rem = ies->len;
2113 ie = ies->data;
Johannes Berg9caf0362012-11-29 01:25:20 +01002114
Johannes Berg83c7aa12013-02-05 16:51:29 +01002115 while (rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01002116 /* invalid data */
2117 if (ie[1] > rem - 2)
2118 break;
2119
2120 switch (ie[0]) {
2121 case WLAN_EID_SSID:
2122 memset(&iwe, 0, sizeof(iwe));
2123 iwe.cmd = SIOCGIWESSID;
2124 iwe.u.data.length = ie[1];
2125 iwe.u.data.flags = 1;
James Minor76a70e92015-02-24 12:58:20 -06002126 current_ev = iwe_stream_add_point_check(info,
2127 current_ev,
2128 end_buf, &iwe,
2129 (u8 *)ie + 2);
2130 if (IS_ERR(current_ev))
2131 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002132 break;
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;
James Minor76a70e92015-02-24 12:58:20 -06002138 current_ev = iwe_stream_add_point_check(info,
2139 current_ev,
2140 end_buf, &iwe,
2141 (u8 *)ie + 2);
2142 if (IS_ERR(current_ev))
2143 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002144 break;
2145 case WLAN_EID_MESH_CONFIG:
2146 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00002147 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01002148 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01002149 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01002150 memset(&iwe, 0, sizeof(iwe));
2151 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002152 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2153 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01002154 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002155 current_ev = iwe_stream_add_point_check(info,
2156 current_ev,
2157 end_buf,
2158 &iwe, buf);
2159 if (IS_ERR(current_ev))
2160 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002161 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2162 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01002163 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002164 current_ev = iwe_stream_add_point_check(info,
2165 current_ev,
2166 end_buf,
2167 &iwe, buf);
2168 if (IS_ERR(current_ev))
2169 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002170 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2171 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01002172 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002173 current_ev = iwe_stream_add_point_check(info,
2174 current_ev,
2175 end_buf,
2176 &iwe, buf);
2177 if (IS_ERR(current_ev))
2178 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002179 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01002180 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002181 current_ev = iwe_stream_add_point_check(info,
2182 current_ev,
2183 end_buf,
2184 &iwe, buf);
2185 if (IS_ERR(current_ev))
2186 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002187 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2188 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002189 current_ev = iwe_stream_add_point_check(info,
2190 current_ev,
2191 end_buf,
2192 &iwe, buf);
2193 if (IS_ERR(current_ev))
2194 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002195 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2196 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002197 current_ev = iwe_stream_add_point_check(info,
2198 current_ev,
2199 end_buf,
2200 &iwe, buf);
2201 if (IS_ERR(current_ev))
2202 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002203 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01002204 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002205 current_ev = iwe_stream_add_point_check(info,
2206 current_ev,
2207 end_buf,
2208 &iwe, buf);
2209 if (IS_ERR(current_ev))
2210 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002211 break;
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);
2216
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;
2221
2222 for (i = 0; i < ie[1]; i++) {
2223 iwe.u.bitrate.value =
2224 ((ie[i + 2] & 0x7f) * 500000);
James Minor76a70e92015-02-24 12:58:20 -06002225 tmp = p;
Johannes Berg2a519312009-02-10 21:25:55 +01002226 p = iwe_stream_add_value(info, current_ev, p,
James Minor76a70e92015-02-24 12:58:20 -06002227 end_buf, &iwe,
2228 IW_EV_PARAM_LEN);
2229 if (p == tmp) {
2230 current_ev = ERR_PTR(-E2BIG);
2231 goto unlock;
2232 }
Johannes Berg2a519312009-02-10 21:25:55 +01002233 }
2234 current_ev = p;
2235 break;
2236 }
2237 rem -= ie[1] + 2;
2238 ie += ie[1] + 2;
2239 }
2240
Joe Perchesf64f9e72009-11-29 16:55:45 -08002241 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2242 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01002243 memset(&iwe, 0, sizeof(iwe));
2244 iwe.cmd = SIOCGIWMODE;
2245 if (ismesh)
2246 iwe.u.mode = IW_MODE_MESH;
2247 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2248 iwe.u.mode = IW_MODE_MASTER;
2249 else
2250 iwe.u.mode = IW_MODE_ADHOC;
James Minor76a70e92015-02-24 12:58:20 -06002251 current_ev = iwe_stream_add_event_check(info, current_ev,
2252 end_buf, &iwe,
2253 IW_EV_UINT_LEN);
2254 if (IS_ERR(current_ev))
2255 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002256 }
2257
James Minor76a70e92015-02-24 12:58:20 -06002258 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,
2263 &iwe, buf);
2264 if (IS_ERR(current_ev))
2265 goto unlock;
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))
2274 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002275
James Minor76a70e92015-02-24 12:58:20 -06002276 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2277
2278 unlock:
Johannes Berg9caf0362012-11-29 01:25:20 +01002279 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01002280 return current_ev;
2281}
2282
2283
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002284static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +01002285 struct iw_request_info *info,
2286 char *buf, size_t len)
2287{
2288 char *current_ev = buf;
2289 char *end_buf = buf + len;
2290 struct cfg80211_internal_bss *bss;
James Minor76a70e92015-02-24 12:58:20 -06002291 int err = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002292
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002293 spin_lock_bh(&rdev->bss_lock);
2294 cfg80211_bss_expire(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01002295
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002296 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01002297 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
James Minor76a70e92015-02-24 12:58:20 -06002298 err = -E2BIG;
2299 break;
Johannes Berg2a519312009-02-10 21:25:55 +01002300 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002301 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
Johannes Berg77965c972009-02-18 18:45:06 +01002302 current_ev, end_buf);
James Minor76a70e92015-02-24 12:58:20 -06002303 if (IS_ERR(current_ev)) {
2304 err = PTR_ERR(current_ev);
2305 break;
2306 }
Johannes Berg2a519312009-02-10 21:25:55 +01002307 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002308 spin_unlock_bh(&rdev->bss_lock);
James Minor76a70e92015-02-24 12:58:20 -06002309
2310 if (err)
2311 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01002312 return current_ev - buf;
2313}
2314
2315
2316int cfg80211_wext_giwscan(struct net_device *dev,
2317 struct iw_request_info *info,
2318 struct iw_point *data, char *extra)
2319{
2320 struct cfg80211_registered_device *rdev;
2321 int res;
2322
2323 if (!netif_running(dev))
2324 return -ENETDOWN;
2325
Johannes Berg463d0182009-07-14 00:33:35 +02002326 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01002327
2328 if (IS_ERR(rdev))
2329 return PTR_ERR(rdev);
2330
Johannes Bergf9d15d12014-01-22 11:14:19 +02002331 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg5fe231e2013-05-08 21:45:15 +02002332 return -EAGAIN;
Johannes Berg2a519312009-02-10 21:25:55 +01002333
2334 res = ieee80211_scan_results(rdev, info, extra, data->length);
2335 data->length = 0;
2336 if (res >= 0) {
2337 data->length = res;
2338 res = 0;
2339 }
2340
Johannes Berg2a519312009-02-10 21:25:55 +01002341 return res;
2342}
Johannes Berg2afe38d2015-01-06 14:00:53 +01002343EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01002344#endif