blob: 49f700a1460b0274260977ef0e7256d2d087724f [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 ||
Sara Sharon5bd9d102019-03-15 17:39:02 +0200272 tmp_new[0] == WLAN_EID_SSID)) {
Peng Xu0b8fb822019-01-21 12:14:57 +0200273 memcpy(pos, tmp_new, tmp_new[1] + 2);
274 pos += tmp_new[1] + 2;
275 }
276 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
277 break;
278 tmp_new += tmp_new[1] + 2;
279 }
280
281 kfree(sub_copy);
282 return pos - new_ie;
283}
284
285static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
286 const u8 *ssid, size_t ssid_len)
287{
288 const struct cfg80211_bss_ies *ies;
289 const u8 *ssidie;
290
291 if (bssid && !ether_addr_equal(a->bssid, bssid))
292 return false;
293
294 if (!ssid)
295 return true;
296
297 ies = rcu_access_pointer(a->ies);
298 if (!ies)
299 return false;
300 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
301 if (!ssidie)
302 return false;
303 if (ssidie[1] != ssid_len)
304 return false;
305 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
306}
307
308static int
Sara Sharon7011ba52019-01-21 12:25:59 +0200309cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
310 struct cfg80211_bss *nontrans_bss)
Peng Xu0b8fb822019-01-21 12:14:57 +0200311{
312 const u8 *ssid;
313 size_t ssid_len;
Sara Sharon7011ba52019-01-21 12:25:59 +0200314 struct cfg80211_bss *bss = NULL;
Peng Xu0b8fb822019-01-21 12:14:57 +0200315
316 rcu_read_lock();
Sara Sharon7011ba52019-01-21 12:25:59 +0200317 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
Peng Xu0b8fb822019-01-21 12:14:57 +0200318 if (!ssid) {
319 rcu_read_unlock();
320 return -EINVAL;
321 }
322 ssid_len = ssid[1];
323 ssid = ssid + 2;
324 rcu_read_unlock();
325
326 /* check if nontrans_bss is in the list */
327 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
Sara Sharon7011ba52019-01-21 12:25:59 +0200328 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
Peng Xu0b8fb822019-01-21 12:14:57 +0200329 return 0;
330 }
331
332 /* add to the list */
333 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
334 return 0;
335}
336
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800337static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
Sam Leffler15d60302012-10-11 21:03:34 -0700338 unsigned long expire_time)
339{
340 struct cfg80211_internal_bss *bss, *tmp;
341 bool expired = false;
342
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800343 lockdep_assert_held(&rdev->bss_lock);
Johannes Berg4b1af472013-02-01 01:05:43 +0100344
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800345 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
Sam Leffler15d60302012-10-11 21:03:34 -0700346 if (atomic_read(&bss->hold))
347 continue;
348 if (!time_after(expire_time, bss->ts))
349 continue;
350
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800351 if (__cfg80211_unlink_bss(rdev, bss))
Johannes Berg776b3582013-02-01 02:06:18 +0100352 expired = true;
Sam Leffler15d60302012-10-11 21:03:34 -0700353 }
354
355 if (expired)
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800356 rdev->bss_generation++;
Sam Leffler15d60302012-10-11 21:03:34 -0700357}
358
Johannes Berg9853a552016-11-15 12:05:11 +0100359static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
360{
361 struct cfg80211_internal_bss *bss, *oldest = NULL;
362 bool ret;
363
364 lockdep_assert_held(&rdev->bss_lock);
365
366 list_for_each_entry(bss, &rdev->bss_list, list) {
367 if (atomic_read(&bss->hold))
368 continue;
369
370 if (!list_empty(&bss->hidden_list) &&
371 !bss->pub.hidden_beacon_bss)
372 continue;
373
374 if (oldest && time_before(oldest->ts, bss->ts))
375 continue;
376 oldest = bss;
377 }
378
379 if (WARN_ON(!oldest))
380 return false;
381
382 /*
383 * The callers make sure to increase rdev->bss_generation if anything
384 * gets removed (and a new entry added), so there's no need to also do
385 * it here.
386 */
387
388 ret = __cfg80211_unlink_bss(rdev, oldest);
389 WARN_ON(!ret);
390 return ret;
391}
392
Johannes Bergf9d15d12014-01-22 11:14:19 +0200393void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
394 bool send_message)
Johannes Berg2a519312009-02-10 21:25:55 +0100395{
Johannes Berg667503d2009-07-07 03:56:11 +0200396 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +0200397 struct wireless_dev *wdev;
Johannes Bergf9d15d12014-01-22 11:14:19 +0200398 struct sk_buff *msg;
Johannes Berg3d23e342009-09-29 23:27:28 +0200399#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100400 union iwreq_data wrqu;
401#endif
402
Johannes Berg5fe231e2013-05-08 21:45:15 +0200403 ASSERT_RTNL();
Johannes Berg01a0ac42009-08-20 21:36:16 +0200404
Johannes Bergf9d15d12014-01-22 11:14:19 +0200405 if (rdev->scan_msg) {
Arend Van Spriel505a2e82016-12-16 11:21:54 +0000406 nl80211_send_scan_msg(rdev, rdev->scan_msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200407 rdev->scan_msg = NULL;
408 return;
409 }
Johannes Berg667503d2009-07-07 03:56:11 +0200410
Johannes Bergf9d15d12014-01-22 11:14:19 +0200411 request = rdev->scan_req;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200412 if (!request)
413 return;
414
Johannes Bergfd014282012-06-18 19:17:03 +0200415 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +0100416
Johannes Berg6829c872009-07-02 09:13:27 +0200417 /*
418 * This must be before sending the other events!
419 * Otherwise, wpa_supplicant gets completely confused with
420 * wext events.
421 */
Johannes Bergfd014282012-06-18 19:17:03 +0200422 if (wdev->netdev)
423 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200424
Avraham Stern1d762502016-07-05 17:10:13 +0300425 if (!request->info.aborted &&
Johannes Bergf9d15d12014-01-22 11:14:19 +0200426 request->flags & NL80211_SCAN_FLAG_FLUSH) {
427 /* flush entries from previous scans */
428 spin_lock_bh(&rdev->bss_lock);
429 __cfg80211_bss_expire(rdev, request->scan_start);
430 spin_unlock_bh(&rdev->bss_lock);
Sam Leffler15d60302012-10-11 21:03:34 -0700431 }
Johannes Berg2a519312009-02-10 21:25:55 +0100432
Avraham Stern1d762502016-07-05 17:10:13 +0300433 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200434
Johannes Berg3d23e342009-09-29 23:27:28 +0200435#ifdef CONFIG_CFG80211_WEXT
Avraham Stern1d762502016-07-05 17:10:13 +0300436 if (wdev->netdev && !request->info.aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100437 memset(&wrqu, 0, sizeof(wrqu));
438
Johannes Bergfd014282012-06-18 19:17:03 +0200439 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100440 }
441#endif
442
Johannes Bergfd014282012-06-18 19:17:03 +0200443 if (wdev->netdev)
444 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100445
Johannes Berg36e6fea2009-08-12 22:21:21 +0200446 rdev->scan_req = NULL;
Eliad Peller4a58e7c2013-12-05 18:30:17 +0200447 kfree(request);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200448
449 if (!send_message)
450 rdev->scan_msg = msg;
451 else
Arend Van Spriel505a2e82016-12-16 11:21:54 +0000452 nl80211_send_scan_msg(rdev, msg);
Johannes Berg2a519312009-02-10 21:25:55 +0100453}
Johannes Berg667503d2009-07-07 03:56:11 +0200454
Johannes Berg36e6fea2009-08-12 22:21:21 +0200455void __cfg80211_scan_done(struct work_struct *wk)
456{
457 struct cfg80211_registered_device *rdev;
458
459 rdev = container_of(wk, struct cfg80211_registered_device,
460 scan_done_wk);
461
Johannes Berg5fe231e2013-05-08 21:45:15 +0200462 rtnl_lock();
Johannes Bergf9d15d12014-01-22 11:14:19 +0200463 ___cfg80211_scan_done(rdev, true);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200464 rtnl_unlock();
Johannes Berg36e6fea2009-08-12 22:21:21 +0200465}
466
Avraham Stern1d762502016-07-05 17:10:13 +0300467void cfg80211_scan_done(struct cfg80211_scan_request *request,
468 struct cfg80211_scan_info *info)
Johannes Berg667503d2009-07-07 03:56:11 +0200469{
Avraham Stern1d762502016-07-05 17:10:13 +0300470 trace_cfg80211_scan_done(request, info);
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800471 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
Johannes Berg667503d2009-07-07 03:56:11 +0200472
Avraham Stern1d762502016-07-05 17:10:13 +0300473 request->info = *info;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200474 request->notified = true;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800475 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +0200476}
Johannes Berg2a519312009-02-10 21:25:55 +0100477EXPORT_SYMBOL(cfg80211_scan_done);
478
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100479void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
480 struct cfg80211_sched_scan_request *req)
481{
482 ASSERT_RTNL();
483
484 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
485}
486
487static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
488 struct cfg80211_sched_scan_request *req)
489{
490 ASSERT_RTNL();
491
492 list_del_rcu(&req->list);
493 kfree_rcu(req, rcu_head);
494}
495
496static struct cfg80211_sched_scan_request *
497cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
498{
499 struct cfg80211_sched_scan_request *pos;
500
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100501 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100502
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100503 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100504 if (pos->reqid == reqid)
505 return pos;
506 }
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100507 return NULL;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100508}
509
510/*
511 * Determines if a scheduled scan request can be handled. When a legacy
512 * scheduled scan is running no other scheduled scan is allowed regardless
513 * whether the request is for legacy or multi-support scan. When a multi-support
514 * scheduled scan is running a request for legacy scan is not allowed. In this
515 * case a request for multi-support scan can be handled if resources are
516 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
517 */
518int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
519 bool want_multi)
520{
521 struct cfg80211_sched_scan_request *pos;
522 int i = 0;
523
524 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
525 /* request id zero means legacy in progress */
526 if (!i && !pos->reqid)
527 return -EINPROGRESS;
528 i++;
529 }
530
531 if (i) {
532 /* no legacy allowed when multi request(s) are active */
533 if (!want_multi)
534 return -EINPROGRESS;
535
536 /* resource limit reached */
537 if (i == rdev->wiphy.max_sched_scan_reqs)
538 return -ENOSPC;
539 }
540 return 0;
541}
542
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100543void cfg80211_sched_scan_results_wk(struct work_struct *work)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300544{
545 struct cfg80211_registered_device *rdev;
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100546 struct cfg80211_sched_scan_request *req, *tmp;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300547
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100548 rdev = container_of(work, struct cfg80211_registered_device,
549 sched_scan_res_wk);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300550
Johannes Berg5fe231e2013-05-08 21:45:15 +0200551 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100552 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
553 if (req->report_results) {
554 req->report_results = false;
555 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
556 /* flush entries from previous scans */
557 spin_lock_bh(&rdev->bss_lock);
558 __cfg80211_bss_expire(rdev, req->scan_start);
559 spin_unlock_bh(&rdev->bss_lock);
560 req->scan_start = jiffies;
561 }
562 nl80211_send_sched_scan(req,
563 NL80211_CMD_SCHED_SCAN_RESULTS);
Sam Leffler15d60302012-10-11 21:03:34 -0700564 }
Sam Leffler15d60302012-10-11 21:03:34 -0700565 }
Johannes Berg5fe231e2013-05-08 21:45:15 +0200566 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300567}
568
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100569void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300570{
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100571 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
572 struct cfg80211_sched_scan_request *request;
573
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100574 trace_cfg80211_sched_scan_results(wiphy, reqid);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300575 /* ignore if we're not scanning */
Jukka Rissanen31a60ed2014-12-15 13:25:38 +0200576
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100577 rcu_read_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100578 request = cfg80211_find_sched_scan_req(rdev, reqid);
579 if (request) {
580 request->report_results = true;
581 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
582 }
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100583 rcu_read_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300584}
585EXPORT_SYMBOL(cfg80211_sched_scan_results);
586
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100587void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300588{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800589 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300590
Eliad Peller792e6aa2014-04-30 16:14:23 +0300591 ASSERT_RTNL();
592
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100593 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
Beni Lev4ee3e062012-08-27 12:49:39 +0300594
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100595 __cfg80211_stop_sched_scan(rdev, reqid, true);
Eliad Peller792e6aa2014-04-30 16:14:23 +0300596}
597EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
598
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100599void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
Eliad Peller792e6aa2014-04-30 16:14:23 +0300600{
601 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100602 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200603 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300604}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300605EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
606
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100607int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
608 struct cfg80211_sched_scan_request *req,
609 bool driver_initiated)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300610{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200611 ASSERT_RTNL();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300612
Luciano Coelho85a99942011-05-12 16:28:29 +0300613 if (!driver_initiated) {
Arend Van Spriel3a3ecf12017-04-21 13:05:02 +0100614 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
Luciano Coelho85a99942011-05-12 16:28:29 +0300615 if (err)
616 return err;
617 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300618
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100619 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300620
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100621 cfg80211_del_sched_scan_req(rdev, req);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300622
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200623 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300624}
625
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100626int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
627 u64 reqid, bool driver_initiated)
628{
629 struct cfg80211_sched_scan_request *sched_scan_req;
630
631 ASSERT_RTNL();
632
633 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100634 if (!sched_scan_req)
635 return -ENOENT;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100636
637 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
638 driver_initiated);
639}
640
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800641void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500642 unsigned long age_secs)
643{
644 struct cfg80211_internal_bss *bss;
645 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
646
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800647 spin_lock_bh(&rdev->bss_lock);
648 list_for_each_entry(bss, &rdev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500649 bss->ts -= age_jiffies;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800650 spin_unlock_bh(&rdev->bss_lock);
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500651}
652
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800653void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
Johannes Berg2a519312009-02-10 21:25:55 +0100654{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800655 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100656}
657
Johannes Berg49a68e02019-02-07 23:26:38 +0100658const struct element *
659cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
660 const u8 *match, unsigned int match_len,
661 unsigned int match_offset)
Johannes Berg2a519312009-02-10 21:25:55 +0100662{
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100663 const struct element *elem;
664
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100665 for_each_element_id(elem, eid, ies, len) {
Johannes Berg49a68e02019-02-07 23:26:38 +0100666 if (elem->datalen >= match_offset + match_len &&
667 !memcmp(elem->data + match_offset, match, match_len))
668 return elem;
Johannes Berg2a519312009-02-10 21:25:55 +0100669 }
Luca Coelhofbd05e42016-09-15 18:15:09 +0300670
671 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100672}
Johannes Berg49a68e02019-02-07 23:26:38 +0100673EXPORT_SYMBOL(cfg80211_find_elem_match);
Johannes Berg2a519312009-02-10 21:25:55 +0100674
Johannes Berg49a68e02019-02-07 23:26:38 +0100675const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
676 const u8 *ies,
677 unsigned int len)
Eliad Peller0c28ec52011-09-15 11:53:01 +0300678{
Johannes Berg49a68e02019-02-07 23:26:38 +0100679 const struct element *elem;
Luca Coelhofbd05e42016-09-15 18:15:09 +0300680 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
681 int match_len = (oui_type < 0) ? 3 : sizeof(match);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300682
Emmanuel Grumbach9e9ea432016-05-03 16:08:07 +0300683 if (WARN_ON(oui_type > 0xff))
684 return NULL;
685
Johannes Berg49a68e02019-02-07 23:26:38 +0100686 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
687 match, match_len, 0);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300688
Johannes Berg49a68e02019-02-07 23:26:38 +0100689 if (!elem || elem->datalen < 4)
Luca Coelhofbd05e42016-09-15 18:15:09 +0300690 return NULL;
Luciano Coelho67194292013-02-12 20:11:38 +0200691
Johannes Berg49a68e02019-02-07 23:26:38 +0100692 return elem;
Eliad Peller0c28ec52011-09-15 11:53:01 +0300693}
Johannes Berg49a68e02019-02-07 23:26:38 +0100694EXPORT_SYMBOL(cfg80211_find_vendor_elem);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300695
Johannes Berg4593c4c2013-02-01 19:20:03 +0100696/**
697 * enum bss_compare_mode - BSS compare mode
698 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
699 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
700 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
701 */
702enum bss_compare_mode {
703 BSS_CMP_REGULAR,
704 BSS_CMP_HIDE_ZLEN,
705 BSS_CMP_HIDE_NUL,
706};
707
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100708static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100709 struct cfg80211_bss *b,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100710 enum bss_compare_mode mode)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100711{
Johannes Berg9caf0362012-11-29 01:25:20 +0100712 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg3af63412013-01-30 00:40:20 +0100713 const u8 *ie1 = NULL;
714 const u8 *ie2 = NULL;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100715 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100716
Johannes Berg3af63412013-01-30 00:40:20 +0100717 if (a->channel != b->channel)
718 return b->channel->center_freq - a->channel->center_freq;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100719
Johannes Berg9caf0362012-11-29 01:25:20 +0100720 a_ies = rcu_access_pointer(a->ies);
721 if (!a_ies)
722 return -1;
723 b_ies = rcu_access_pointer(b->ies);
724 if (!b_ies)
725 return 1;
726
Johannes Berg3af63412013-01-30 00:40:20 +0100727 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
728 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
729 a_ies->data, a_ies->len);
730 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
731 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
732 b_ies->data, b_ies->len);
733 if (ie1 && ie2) {
734 int mesh_id_cmp;
735
736 if (ie1[1] == ie2[1])
737 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
738 else
739 mesh_id_cmp = ie2[1] - ie1[1];
740
741 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
742 a_ies->data, a_ies->len);
743 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
744 b_ies->data, b_ies->len);
745 if (ie1 && ie2) {
746 if (mesh_id_cmp)
747 return mesh_id_cmp;
748 if (ie1[1] != ie2[1])
749 return ie2[1] - ie1[1];
750 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
751 }
752 }
753
Johannes Berg3af63412013-01-30 00:40:20 +0100754 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
755 if (r)
756 return r;
757
Johannes Berg9caf0362012-11-29 01:25:20 +0100758 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
759 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100760
Johannes Berg5622f5b2013-01-30 00:26:45 +0100761 if (!ie1 && !ie2)
762 return 0;
763
Johannes Bergf94f8b12012-11-28 22:42:34 +0100764 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100765 * Note that with "hide_ssid", the function returns a match if
766 * the already-present BSS ("b") is a hidden SSID beacon for
767 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100768 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100769
770 /* sort missing IE before (left of) present IE */
771 if (!ie1)
772 return -1;
773 if (!ie2)
774 return 1;
775
Johannes Berg4593c4c2013-02-01 19:20:03 +0100776 switch (mode) {
777 case BSS_CMP_HIDE_ZLEN:
778 /*
779 * In ZLEN mode we assume the BSS entry we're
780 * looking for has a zero-length SSID. So if
781 * the one we're looking at right now has that,
782 * return 0. Otherwise, return the difference
783 * in length, but since we're looking for the
784 * 0-length it's really equivalent to returning
785 * the length of the one we're looking at.
786 *
787 * No content comparison is needed as we assume
788 * the content length is zero.
789 */
790 return ie2[1];
791 case BSS_CMP_REGULAR:
792 default:
793 /* sort by length first, then by contents */
794 if (ie1[1] != ie2[1])
795 return ie2[1] - ie1[1];
Johannes Berg5622f5b2013-01-30 00:26:45 +0100796 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100797 case BSS_CMP_HIDE_NUL:
798 if (ie1[1] != ie2[1])
799 return ie2[1] - ie1[1];
800 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
801 for (i = 0; i < ie2[1]; i++)
802 if (ie2[i + 2])
803 return -1;
804 return 0;
805 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100806}
807
Dedy Lansky6eb18132015-02-08 15:52:03 +0200808static bool cfg80211_bss_type_match(u16 capability,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200809 enum nl80211_band band,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200810 enum ieee80211_bss_type bss_type)
811{
812 bool ret = true;
813 u16 mask, val;
814
815 if (bss_type == IEEE80211_BSS_TYPE_ANY)
816 return ret;
817
Johannes Berg57fbcce2016-04-12 15:56:15 +0200818 if (band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200819 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
820 switch (bss_type) {
821 case IEEE80211_BSS_TYPE_ESS:
822 val = WLAN_CAPABILITY_DMG_TYPE_AP;
823 break;
824 case IEEE80211_BSS_TYPE_PBSS:
825 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
826 break;
827 case IEEE80211_BSS_TYPE_IBSS:
828 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
829 break;
830 default:
831 return false;
832 }
833 } else {
834 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
835 switch (bss_type) {
836 case IEEE80211_BSS_TYPE_ESS:
837 val = WLAN_CAPABILITY_ESS;
838 break;
839 case IEEE80211_BSS_TYPE_IBSS:
840 val = WLAN_CAPABILITY_IBSS;
841 break;
842 case IEEE80211_BSS_TYPE_MBSS:
843 val = 0;
844 break;
845 default:
846 return false;
847 }
848 }
849
850 ret = ((capability & mask) == val);
851 return ret;
852}
853
Ben Greear0e3a39b2013-06-19 14:06:27 -0700854/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100855struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
856 struct ieee80211_channel *channel,
857 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100858 const u8 *ssid, size_t ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200859 enum ieee80211_bss_type bss_type,
860 enum ieee80211_privacy privacy)
Johannes Berg2a519312009-02-10 21:25:55 +0100861{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800862 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +0100863 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200864 unsigned long now = jiffies;
Dedy Lansky6eb18132015-02-08 15:52:03 +0200865 int bss_privacy;
Johannes Berg2a519312009-02-10 21:25:55 +0100866
Dedy Lansky6eb18132015-02-08 15:52:03 +0200867 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
868 privacy);
Beni Lev4ee3e062012-08-27 12:49:39 +0300869
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800870 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100871
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800872 list_for_each_entry(bss, &rdev->bss_list, list) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200873 if (!cfg80211_bss_type_match(bss->pub.capability,
874 bss->pub.channel->band, bss_type))
875 continue;
876
877 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
878 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
879 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
Johannes Berg79420f02009-02-10 21:25:59 +0100880 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100881 if (channel && bss->pub.channel != channel)
882 continue;
Johannes Bergc14a7402014-04-09 22:36:50 +0200883 if (!is_valid_ether_addr(bss->pub.bssid))
884 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200885 /* Don't get expired BSS structs */
886 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
887 !atomic_read(&bss->hold))
888 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100889 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
890 res = bss;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800891 bss_ref_get(rdev, res);
Johannes Berg2a519312009-02-10 21:25:55 +0100892 break;
893 }
894 }
895
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800896 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100897 if (!res)
898 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300899 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100900 return &res->pub;
901}
902EXPORT_SYMBOL(cfg80211_get_bss);
903
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800904static void rb_insert_bss(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +0100905 struct cfg80211_internal_bss *bss)
906{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800907 struct rb_node **p = &rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +0100908 struct rb_node *parent = NULL;
909 struct cfg80211_internal_bss *tbss;
910 int cmp;
911
912 while (*p) {
913 parent = *p;
914 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
915
Johannes Berg4593c4c2013-02-01 19:20:03 +0100916 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100917
918 if (WARN_ON(!cmp)) {
919 /* will sort of leak this BSS */
920 return;
921 }
922
923 if (cmp < 0)
924 p = &(*p)->rb_left;
925 else
926 p = &(*p)->rb_right;
927 }
928
929 rb_link_node(&bss->rbn, parent, p);
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800930 rb_insert_color(&bss->rbn, &rdev->bss_tree);
Johannes Berg2a519312009-02-10 21:25:55 +0100931}
932
933static struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800934rb_find_bss(struct cfg80211_registered_device *rdev,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100935 struct cfg80211_internal_bss *res,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100936 enum bss_compare_mode mode)
Johannes Berg2a519312009-02-10 21:25:55 +0100937{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800938 struct rb_node *n = rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +0100939 struct cfg80211_internal_bss *bss;
940 int r;
941
942 while (n) {
943 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100944 r = cmp_bss(&res->pub, &bss->pub, mode);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100945
946 if (r == 0)
947 return bss;
948 else if (r < 0)
949 n = n->rb_left;
950 else
951 n = n->rb_right;
952 }
953
954 return NULL;
955}
956
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800957static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
Johannes Berg776b3582013-02-01 02:06:18 +0100958 struct cfg80211_internal_bss *new)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100959{
Johannes Berg9caf0362012-11-29 01:25:20 +0100960 const struct cfg80211_bss_ies *ies;
Johannes Berg776b3582013-02-01 02:06:18 +0100961 struct cfg80211_internal_bss *bss;
962 const u8 *ie;
963 int i, ssidlen;
964 u8 fold = 0;
Johannes Berg9853a552016-11-15 12:05:11 +0100965 u32 n_entries = 0;
Johannes Berg9caf0362012-11-29 01:25:20 +0100966
Johannes Berg776b3582013-02-01 02:06:18 +0100967 ies = rcu_access_pointer(new->pub.beacon_ies);
Johannes Berg9caf0362012-11-29 01:25:20 +0100968 if (WARN_ON(!ies))
Johannes Berg776b3582013-02-01 02:06:18 +0100969 return false;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100970
Johannes Berg776b3582013-02-01 02:06:18 +0100971 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
972 if (!ie) {
973 /* nothing to do */
974 return true;
975 }
976
977 ssidlen = ie[1];
978 for (i = 0; i < ssidlen; i++)
979 fold |= ie[2 + i];
980
981 if (fold) {
982 /* not a hidden SSID */
983 return true;
984 }
985
986 /* This is the bad part ... */
987
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800988 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg9853a552016-11-15 12:05:11 +0100989 /*
990 * we're iterating all the entries anyway, so take the
991 * opportunity to validate the list length accounting
992 */
993 n_entries++;
994
Johannes Berg776b3582013-02-01 02:06:18 +0100995 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
996 continue;
997 if (bss->pub.channel != new->pub.channel)
998 continue;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200999 if (bss->pub.scan_width != new->pub.scan_width)
1000 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001001 if (rcu_access_pointer(bss->pub.beacon_ies))
1002 continue;
1003 ies = rcu_access_pointer(bss->pub.ies);
1004 if (!ies)
1005 continue;
1006 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1007 if (!ie)
1008 continue;
1009 if (ssidlen && ie[1] != ssidlen)
1010 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001011 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1012 continue;
1013 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1014 list_del(&bss->hidden_list);
1015 /* combine them */
1016 list_add(&bss->hidden_list, &new->hidden_list);
1017 bss->pub.hidden_beacon_bss = &new->pub;
1018 new->refcount += bss->refcount;
1019 rcu_assign_pointer(bss->pub.beacon_ies,
1020 new->pub.beacon_ies);
1021 }
1022
Johannes Berg9853a552016-11-15 12:05:11 +01001023 WARN_ONCE(n_entries != rdev->bss_entries,
1024 "rdev bss entries[%d]/list[len:%d] corruption\n",
1025 rdev->bss_entries, n_entries);
1026
Johannes Berg776b3582013-02-01 02:06:18 +01001027 return true;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001028}
1029
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001030struct cfg80211_non_tx_bss {
1031 struct cfg80211_bss *tx_bss;
1032 u8 max_bssid_indicator;
1033 u8 bssid_index;
1034};
1035
Ben Greear0e3a39b2013-06-19 14:06:27 -07001036/* Returned bss is reference counted and must be cleaned up appropriately. */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001037static struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001038cfg80211_bss_update(struct cfg80211_registered_device *rdev,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001039 struct cfg80211_internal_bss *tmp,
1040 bool signal_valid)
Johannes Berg2a519312009-02-10 21:25:55 +01001041{
1042 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001043
Johannes Berg9caf0362012-11-29 01:25:20 +01001044 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +01001045 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001046
Johannes Berg9caf0362012-11-29 01:25:20 +01001047 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001048
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001049 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001050
Johannes Berg9caf0362012-11-29 01:25:20 +01001051 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001052 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg9caf0362012-11-29 01:25:20 +01001053 return NULL;
1054 }
1055
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001056 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +01001057
Johannes Bergcd1658f2009-04-16 15:00:58 +02001058 if (found) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001059 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +01001060 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1061 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +02001062
Johannes Berg9caf0362012-11-29 01:25:20 +01001063 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +02001064
Johannes Berg9caf0362012-11-29 01:25:20 +01001065 rcu_assign_pointer(found->pub.proberesp_ies,
1066 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001067 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +01001068 rcu_assign_pointer(found->pub.ies,
1069 tmp->pub.proberesp_ies);
1070 if (old)
1071 kfree_rcu((struct cfg80211_bss_ies *)old,
1072 rcu_head);
1073 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
Johannes Berg9537f222013-02-01 01:19:48 +01001074 const struct cfg80211_bss_ies *old;
Johannes Berg776b3582013-02-01 02:06:18 +01001075 struct cfg80211_internal_bss *bss;
1076
1077 if (found->pub.hidden_beacon_bss &&
1078 !list_empty(&found->hidden_list)) {
Johannes Berg1345ee62013-03-06 10:31:05 +01001079 const struct cfg80211_bss_ies *f;
1080
Johannes Berg776b3582013-02-01 02:06:18 +01001081 /*
1082 * The found BSS struct is one of the probe
1083 * response members of a group, but we're
1084 * receiving a beacon (beacon_ies in the tmp
1085 * bss is used). This can only mean that the
1086 * AP changed its beacon from not having an
1087 * SSID to showing it, which is confusing so
1088 * drop this information.
1089 */
Johannes Berg1345ee62013-03-06 10:31:05 +01001090
1091 f = rcu_access_pointer(tmp->pub.beacon_ies);
1092 kfree_rcu((struct cfg80211_bss_ies *)f,
1093 rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +01001094 goto drop;
1095 }
Johannes Berg915de2f2012-11-28 22:39:37 +01001096
Johannes Berg9caf0362012-11-29 01:25:20 +01001097 old = rcu_access_pointer(found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001098
Johannes Berg9caf0362012-11-29 01:25:20 +01001099 rcu_assign_pointer(found->pub.beacon_ies,
1100 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +01001101
1102 /* Override IEs if they were from a beacon before */
Johannes Berg9537f222013-02-01 01:19:48 +01001103 if (old == rcu_access_pointer(found->pub.ies))
Johannes Berg9caf0362012-11-29 01:25:20 +01001104 rcu_assign_pointer(found->pub.ies,
1105 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +02001106
Johannes Berg776b3582013-02-01 02:06:18 +01001107 /* Assign beacon IEs to all sub entries */
1108 list_for_each_entry(bss, &found->hidden_list,
1109 hidden_list) {
1110 const struct cfg80211_bss_ies *ies;
1111
1112 ies = rcu_access_pointer(bss->pub.beacon_ies);
1113 WARN_ON(ies != old);
1114
1115 rcu_assign_pointer(bss->pub.beacon_ies,
1116 tmp->pub.beacon_ies);
1117 }
1118
Johannes Berg9caf0362012-11-29 01:25:20 +01001119 if (old)
1120 kfree_rcu((struct cfg80211_bss_ies *)old,
1121 rcu_head);
1122 }
Johannes Berg1345ee62013-03-06 10:31:05 +01001123
1124 found->pub.beacon_interval = tmp->pub.beacon_interval;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001125 /*
1126 * don't update the signal if beacon was heard on
1127 * adjacent channel.
1128 */
1129 if (signal_valid)
1130 found->pub.signal = tmp->pub.signal;
Johannes Berg1345ee62013-03-06 10:31:05 +01001131 found->pub.capability = tmp->pub.capability;
1132 found->ts = tmp->ts;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001133 found->ts_boottime = tmp->ts_boottime;
Avraham Stern1d762502016-07-05 17:10:13 +03001134 found->parent_tsf = tmp->parent_tsf;
Sunil Dutt983dafa2017-12-13 19:51:36 +02001135 found->pub.chains = tmp->pub.chains;
1136 memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
1137 IEEE80211_MAX_CHAINS);
Avraham Stern1d762502016-07-05 17:10:13 +03001138 ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001139 found->pub.max_bssid_indicator = tmp->pub.max_bssid_indicator;
1140 found->pub.bssid_index = tmp->pub.bssid_index;
Johannes Berg2a519312009-02-10 21:25:55 +01001141 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +01001142 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001143 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +01001144 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001145
Johannes Berg9caf0362012-11-29 01:25:20 +01001146 /*
1147 * create a copy -- the "res" variable that is passed in
1148 * is allocated on the stack since it's not needed in the
1149 * more common case of an update
1150 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001151 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
Johannes Berg9caf0362012-11-29 01:25:20 +01001152 GFP_ATOMIC);
1153 if (!new) {
1154 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1155 if (ies)
1156 kfree_rcu(ies, rcu_head);
1157 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1158 if (ies)
1159 kfree_rcu(ies, rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +01001160 goto drop;
Johannes Berg9caf0362012-11-29 01:25:20 +01001161 }
1162 memcpy(new, tmp, sizeof(*new));
Johannes Berg776b3582013-02-01 02:06:18 +01001163 new->refcount = 1;
1164 INIT_LIST_HEAD(&new->hidden_list);
Sara Sharon7011ba52019-01-21 12:25:59 +02001165 INIT_LIST_HEAD(&new->pub.nontrans_list);
Johannes Berg776b3582013-02-01 02:06:18 +01001166
1167 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001168 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
Johannes Berg776b3582013-02-01 02:06:18 +01001169 if (!hidden)
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001170 hidden = rb_find_bss(rdev, tmp,
Johannes Berg776b3582013-02-01 02:06:18 +01001171 BSS_CMP_HIDE_NUL);
1172 if (hidden) {
1173 new->pub.hidden_beacon_bss = &hidden->pub;
1174 list_add(&new->hidden_list,
1175 &hidden->hidden_list);
1176 hidden->refcount++;
1177 rcu_assign_pointer(new->pub.beacon_ies,
1178 hidden->pub.beacon_ies);
1179 }
1180 } else {
1181 /*
1182 * Ok so we found a beacon, and don't have an entry. If
1183 * it's a beacon with hidden SSID, we might be in for an
1184 * expensive search for any probe responses that should
1185 * be grouped with this beacon for updates ...
1186 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001187 if (!cfg80211_combine_bsses(rdev, new)) {
Johannes Berg776b3582013-02-01 02:06:18 +01001188 kfree(new);
1189 goto drop;
1190 }
1191 }
1192
Johannes Berg9853a552016-11-15 12:05:11 +01001193 if (rdev->bss_entries >= bss_entries_limit &&
1194 !cfg80211_bss_expire_oldest(rdev)) {
1195 kfree(new);
1196 goto drop;
1197 }
1198
Sara Sharona3584f56d2019-01-21 12:22:21 +02001199 /* This must be before the call to bss_ref_get */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001200 if (tmp->pub.transmitted_bss) {
Sara Sharona3584f56d2019-01-21 12:22:21 +02001201 struct cfg80211_internal_bss *pbss =
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001202 container_of(tmp->pub.transmitted_bss,
Sara Sharona3584f56d2019-01-21 12:22:21 +02001203 struct cfg80211_internal_bss,
1204 pub);
1205
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001206 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
Sara Sharona3584f56d2019-01-21 12:22:21 +02001207 bss_ref_get(rdev, pbss);
1208 }
1209
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001210 list_add_tail(&new->list, &rdev->bss_list);
Johannes Berg9853a552016-11-15 12:05:11 +01001211 rdev->bss_entries++;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001212 rb_insert_bss(rdev, new);
Johannes Berg9caf0362012-11-29 01:25:20 +01001213 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +01001214 }
1215
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001216 rdev->bss_generation++;
1217 bss_ref_get(rdev, found);
1218 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001219
Johannes Berg2a519312009-02-10 21:25:55 +01001220 return found;
Johannes Berg776b3582013-02-01 02:06:18 +01001221 drop:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001222 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg776b3582013-02-01 02:06:18 +01001223 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001224}
1225
Jouni Malinen119f94a2018-09-05 18:52:22 +03001226/*
1227 * Update RX channel information based on the available frame payload
1228 * information. This is mainly for the 2.4 GHz band where frames can be received
1229 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1230 * element to indicate the current (transmitting) channel, but this might also
1231 * be needed on other bands if RX frequency does not match with the actual
1232 * operating channel of a BSS.
1233 */
Johannes Berg0172bb72012-11-23 14:23:30 +01001234static struct ieee80211_channel *
1235cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001236 struct ieee80211_channel *channel,
1237 enum nl80211_bss_scan_width scan_width)
Johannes Berg0172bb72012-11-23 14:23:30 +01001238{
1239 const u8 *tmp;
1240 u32 freq;
1241 int channel_number = -1;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001242 struct ieee80211_channel *alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001243
1244 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1245 if (tmp && tmp[1] == 1) {
1246 channel_number = tmp[2];
1247 } else {
1248 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1249 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1250 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1251
1252 channel_number = htop->primary_chan;
1253 }
1254 }
1255
Jouni Malinen119f94a2018-09-05 18:52:22 +03001256 if (channel_number < 0) {
1257 /* No channel information in frame payload */
Johannes Berg0172bb72012-11-23 14:23:30 +01001258 return channel;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001259 }
Johannes Berg0172bb72012-11-23 14:23:30 +01001260
1261 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
Jouni Malinen119f94a2018-09-05 18:52:22 +03001262 alt_channel = ieee80211_get_channel(wiphy, freq);
1263 if (!alt_channel) {
1264 if (channel->band == NL80211_BAND_2GHZ) {
1265 /*
1266 * Better not allow unexpected channels when that could
1267 * be going beyond the 1-11 range (e.g., discovering
1268 * BSS on channel 12 when radio is configured for
1269 * channel 11.
1270 */
1271 return NULL;
1272 }
1273
1274 /* No match for the payload channel number - ignore it */
1275 return channel;
1276 }
1277
1278 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1279 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1280 /*
1281 * Ignore channel number in 5 and 10 MHz channels where there
1282 * may not be an n:1 or 1:n mapping between frequencies and
1283 * channel numbers.
1284 */
1285 return channel;
1286 }
1287
1288 /*
1289 * Use the channel determined through the payload channel number
1290 * instead of the RX channel reported by the driver.
1291 */
1292 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg0172bb72012-11-23 14:23:30 +01001293 return NULL;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001294 return alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001295}
1296
Ben Greear0e3a39b2013-06-19 14:06:27 -07001297/* Returned bss is reference counted and must be cleaned up appropriately. */
Peng Xu0b8fb822019-01-21 12:14:57 +02001298static struct cfg80211_bss *
1299cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1300 struct cfg80211_inform_bss *data,
1301 enum cfg80211_bss_frame_type ftype,
1302 const u8 *bssid, u64 tsf, u16 capability,
1303 u16 beacon_interval, const u8 *ie, size_t ielen,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001304 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001305 gfp_t gfp)
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001306{
Peng Xu0b8fb822019-01-21 12:14:57 +02001307 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg9caf0362012-11-29 01:25:20 +01001308 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001309 struct ieee80211_channel *channel;
Sara Sharon7011ba52019-01-21 12:25:59 +02001310 struct cfg80211_internal_bss tmp = {}, *res;
Dedy Lansky6eb18132015-02-08 15:52:03 +02001311 int bss_type;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001312 bool signal_valid;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001313
1314 if (WARN_ON(!wiphy))
1315 return NULL;
1316
Sujith22fe88d2010-05-13 10:34:08 +05301317 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001318 (data->signal < 0 || data->signal > 100)))
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001319 return NULL;
1320
Jouni Malinen119f94a2018-09-05 18:52:22 +03001321 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1322 data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001323 if (!channel)
1324 return NULL;
1325
Johannes Berg9caf0362012-11-29 01:25:20 +01001326 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1327 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001328 tmp.pub.scan_width = data->scan_width;
1329 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001330 tmp.pub.beacon_interval = beacon_interval;
1331 tmp.pub.capability = capability;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001332 tmp.ts_boottime = data->boottime_ns;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001333 if (non_tx_data) {
1334 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1335 tmp.pub.bssid_index = non_tx_data->bssid_index;
1336 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1337 }
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001338
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001339 /*
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001340 * If we do not know here whether the IEs are from a Beacon or Probe
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001341 * Response frame, we need to pick one of the options and only use it
1342 * with the driver that does not provide the full Beacon/Probe Response
1343 * frame. Use Beacon frame pointer to avoid indicating that this should
Johannes Berg50521aa2013-01-30 21:33:19 +01001344 * override the IEs pointer should we have received an earlier
Johannes Berg9caf0362012-11-29 01:25:20 +01001345 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001346 */
Johannes Berg0e227082014-08-12 20:34:30 +02001347 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001348 if (!ies)
1349 return NULL;
1350 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001351 ies->tsf = tsf;
Johannes Berg0e227082014-08-12 20:34:30 +02001352 ies->from_beacon = false;
Johannes Berg9caf0362012-11-29 01:25:20 +01001353 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001354
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001355 switch (ftype) {
1356 case CFG80211_BSS_FTYPE_BEACON:
1357 ies->from_beacon = true;
Luca Coelho925b5972018-12-15 11:03:21 +02001358 /* fall through */
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001359 case CFG80211_BSS_FTYPE_UNKNOWN:
1360 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1361 break;
1362 case CFG80211_BSS_FTYPE_PRESP:
1363 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1364 break;
1365 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001366 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001367
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001368 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001369 wiphy->max_adj_channel_rssi_comp;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001370 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001371 if (!res)
1372 return NULL;
1373
Johannes Berg57fbcce2016-04-12 15:56:15 +02001374 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001375 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1376 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1377 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1378 regulatory_hint_found_beacon(wiphy, channel, gfp);
1379 } else {
1380 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1381 regulatory_hint_found_beacon(wiphy, channel, gfp);
1382 }
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001383
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001384 if (non_tx_data && non_tx_data->tx_bss) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001385 /* this is a nontransmitting bss, we need to add it to
1386 * transmitting bss' list if it is not there
1387 */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001388 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1389 &res->pub)) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001390 if (__cfg80211_unlink_bss(rdev, res))
1391 rdev->bss_generation++;
1392 }
1393 }
1394
Beni Lev4ee3e062012-08-27 12:49:39 +03001395 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001396 /* cfg80211_bss_update gives us a referenced result */
1397 return &res->pub;
1398}
Peng Xu0b8fb822019-01-21 12:14:57 +02001399
1400static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1401 struct cfg80211_inform_bss *data,
1402 enum cfg80211_bss_frame_type ftype,
1403 const u8 *bssid, u64 tsf,
1404 u16 beacon_interval, const u8 *ie,
1405 size_t ielen,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001406 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001407 gfp_t gfp)
1408{
Johannes Berg1c8745f2019-02-07 22:36:33 +01001409 const u8 *mbssid_index_ie;
1410 const struct element *elem, *sub;
1411 size_t new_ie_len;
Peng Xu0b8fb822019-01-21 12:14:57 +02001412 u8 new_bssid[ETH_ALEN];
1413 u8 *new_ie;
1414 u16 capability;
1415 struct cfg80211_bss *bss;
1416
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001417 if (!non_tx_data)
Peng Xu0b8fb822019-01-21 12:14:57 +02001418 return;
1419 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1420 return;
Sara Sharon213ed572019-01-16 23:02:03 +02001421 if (!wiphy->support_mbssid)
1422 return;
1423 if (wiphy->support_only_he_mbssid &&
1424 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1425 return;
Peng Xu0b8fb822019-01-21 12:14:57 +02001426
Peng Xu0b8fb822019-01-21 12:14:57 +02001427 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1428 if (!new_ie)
1429 return;
1430
Johannes Berg1c8745f2019-02-07 22:36:33 +01001431 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1432 if (elem->datalen < 4)
1433 continue;
1434 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1435 if (sub->id != 0 || sub->datalen < 4) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001436 /* not a valid BSS profile */
1437 continue;
1438 }
1439
Johannes Berg1c8745f2019-02-07 22:36:33 +01001440 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1441 sub->data[1] != 2) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001442 /* The first element within the Nontransmitted
1443 * BSSID Profile is not the Nontransmitted
1444 * BSSID Capability element.
1445 */
1446 continue;
1447 }
1448
1449 /* found a Nontransmitted BSSID Profile */
1450 mbssid_index_ie = cfg80211_find_ie
1451 (WLAN_EID_MULTI_BSSID_IDX,
Johannes Berg1c8745f2019-02-07 22:36:33 +01001452 sub->data, sub->datalen);
Peng Xu0b8fb822019-01-21 12:14:57 +02001453 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1454 mbssid_index_ie[2] == 0) {
1455 /* No valid Multiple BSSID-Index element */
1456 continue;
1457 }
1458
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001459 non_tx_data->bssid_index = mbssid_index_ie[2];
1460 non_tx_data->max_bssid_indicator = elem->data[0];
1461
1462 cfg80211_gen_new_bssid(bssid,
1463 non_tx_data->max_bssid_indicator,
1464 non_tx_data->bssid_index,
Peng Xu0b8fb822019-01-21 12:14:57 +02001465 new_bssid);
1466 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
Johannes Berg1c8745f2019-02-07 22:36:33 +01001467 new_ie_len = cfg80211_gen_new_ie(ie, ielen, sub->data,
1468 sub->datalen, new_ie,
Peng Xu0b8fb822019-01-21 12:14:57 +02001469 gfp);
1470 if (!new_ie_len)
1471 continue;
1472
Johannes Berg1c8745f2019-02-07 22:36:33 +01001473 capability = get_unaligned_le16(sub->data + 2);
Peng Xu0b8fb822019-01-21 12:14:57 +02001474 bss = cfg80211_inform_single_bss_data(wiphy, data,
1475 ftype,
1476 new_bssid, tsf,
1477 capability,
1478 beacon_interval,
1479 new_ie,
1480 new_ie_len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001481 non_tx_data,
1482 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001483 if (!bss)
1484 break;
1485 cfg80211_put_bss(wiphy, bss);
1486 }
Peng Xu0b8fb822019-01-21 12:14:57 +02001487 }
1488
1489 kfree(new_ie);
1490}
1491
1492struct cfg80211_bss *
1493cfg80211_inform_bss_data(struct wiphy *wiphy,
1494 struct cfg80211_inform_bss *data,
1495 enum cfg80211_bss_frame_type ftype,
1496 const u8 *bssid, u64 tsf, u16 capability,
1497 u16 beacon_interval, const u8 *ie, size_t ielen,
1498 gfp_t gfp)
1499{
1500 struct cfg80211_bss *res;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001501 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001502
1503 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1504 capability, beacon_interval, ie,
1505 ielen, NULL, gfp);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001506 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001507 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001508 beacon_interval, ie, ielen, &non_tx_data,
1509 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001510 return res;
1511}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001512EXPORT_SYMBOL(cfg80211_inform_bss_data);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001513
Peng Xu0b8fb822019-01-21 12:14:57 +02001514static void
1515cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1516 struct cfg80211_inform_bss *data,
1517 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001518 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001519 gfp_t gfp)
1520{
1521 enum cfg80211_bss_frame_type ftype;
1522 const u8 *ie = mgmt->u.probe_resp.variable;
1523 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1524 u.probe_resp.variable);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001525
Peng Xu0b8fb822019-01-21 12:14:57 +02001526 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1527 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1528
1529 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1530 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1531 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001532 ie, ielen, non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001533}
1534
1535static void
1536cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
Sara Sharon7011ba52019-01-21 12:25:59 +02001537 struct cfg80211_bss *nontrans_bss,
Peng Xu0b8fb822019-01-21 12:14:57 +02001538 struct ieee80211_mgmt *mgmt, size_t len,
1539 gfp_t gfp)
1540{
1541 u8 *ie, *new_ie, *pos;
1542 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1543 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1544 u.probe_resp.variable);
1545 size_t new_ie_len;
1546 struct cfg80211_bss_ies *new_ies;
1547 const struct cfg80211_bss_ies *old;
1548 u8 cpy_len;
1549
1550 ie = mgmt->u.probe_resp.variable;
1551
1552 new_ie_len = ielen;
1553 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1554 if (!trans_ssid)
1555 return;
1556 new_ie_len -= trans_ssid[1];
1557 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1558 if (!mbssid)
1559 return;
1560 new_ie_len -= mbssid[1];
1561 rcu_read_lock();
Sara Sharon7011ba52019-01-21 12:25:59 +02001562 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
Peng Xu0b8fb822019-01-21 12:14:57 +02001563 if (!nontrans_ssid) {
1564 rcu_read_unlock();
1565 return;
1566 }
1567 new_ie_len += nontrans_ssid[1];
1568 rcu_read_unlock();
1569
1570 /* generate new ie for nontrans BSS
1571 * 1. replace SSID with nontrans BSS' SSID
1572 * 2. skip MBSSID IE
1573 */
1574 new_ie = kzalloc(new_ie_len, gfp);
1575 if (!new_ie)
1576 return;
1577 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp);
Sara Sharonbede8d22019-01-30 08:48:21 +02001578 if (!new_ies)
1579 goto out_free;
Peng Xu0b8fb822019-01-21 12:14:57 +02001580
1581 pos = new_ie;
1582
1583 /* copy the nontransmitted SSID */
1584 cpy_len = nontrans_ssid[1] + 2;
1585 memcpy(pos, nontrans_ssid, cpy_len);
1586 pos += cpy_len;
1587 /* copy the IEs between SSID and MBSSID */
1588 cpy_len = trans_ssid[1] + 2;
1589 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1590 pos += (mbssid - (trans_ssid + cpy_len));
1591 /* copy the IEs after MBSSID */
1592 cpy_len = mbssid[1] + 2;
1593 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1594
1595 /* update ie */
1596 new_ies->len = new_ie_len;
1597 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1598 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1599 memcpy(new_ies->data, new_ie, new_ie_len);
1600 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001601 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1602 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1603 rcu_assign_pointer(nontrans_bss->ies, new_ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001604 if (old)
1605 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1606 } else {
Sara Sharon7011ba52019-01-21 12:25:59 +02001607 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1608 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1609 rcu_assign_pointer(nontrans_bss->ies, new_ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001610 if (old)
1611 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1612 }
Sara Sharonbede8d22019-01-30 08:48:21 +02001613
1614out_free:
1615 kfree(new_ie);
Peng Xu0b8fb822019-01-21 12:14:57 +02001616}
1617
1618/* cfg80211_inform_bss_width_frame helper */
1619static struct cfg80211_bss *
1620cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1621 struct cfg80211_inform_bss *data,
1622 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001623 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001624 gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +01001625{
Johannes Berg9caf0362012-11-29 01:25:20 +01001626 struct cfg80211_internal_bss tmp = {}, *res;
1627 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001628 struct ieee80211_channel *channel;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001629 bool signal_valid;
Johannes Berg2a519312009-02-10 21:25:55 +01001630 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1631 u.probe_resp.variable);
Dedy Lansky6eb18132015-02-08 15:52:03 +02001632 int bss_type;
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001633
Johannes Berg0172bb72012-11-23 14:23:30 +01001634 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1635 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1636
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001637 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
Beni Lev4ee3e062012-08-27 12:49:39 +03001638
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001639 if (WARN_ON(!mgmt))
1640 return NULL;
1641
1642 if (WARN_ON(!wiphy))
1643 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001644
Sujith22fe88d2010-05-13 10:34:08 +05301645 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001646 (data->signal < 0 || data->signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +01001647 return NULL;
1648
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001649 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +01001650 return NULL;
1651
Johannes Berg0172bb72012-11-23 14:23:30 +01001652 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001653 ielen, data->chan, data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001654 if (!channel)
1655 return NULL;
1656
Johannes Berg0e227082014-08-12 20:34:30 +02001657 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001658 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001659 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +01001660 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001661 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
Johannes Berg0e227082014-08-12 20:34:30 +02001662 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
Johannes Berg9caf0362012-11-29 01:25:20 +01001663 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +01001664
Johannes Berg9caf0362012-11-29 01:25:20 +01001665 if (ieee80211_is_probe_resp(mgmt->frame_control))
1666 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1667 else
1668 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1669 rcu_assign_pointer(tmp.pub.ies, ies);
Arend Van Spriel505a2e82016-12-16 11:21:54 +00001670
Johannes Berg9caf0362012-11-29 01:25:20 +01001671 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1672 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001673 tmp.pub.scan_width = data->scan_width;
1674 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001675 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1676 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001677 tmp.ts_boottime = data->boottime_ns;
Avraham Stern1d762502016-07-05 17:10:13 +03001678 tmp.parent_tsf = data->parent_tsf;
Sunil Dutt983dafa2017-12-13 19:51:36 +02001679 tmp.pub.chains = data->chains;
1680 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
Avraham Stern1d762502016-07-05 17:10:13 +03001681 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001682 if (non_tx_data) {
1683 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1684 tmp.pub.bssid_index = non_tx_data->bssid_index;
1685 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1686 }
Johannes Berg2a519312009-02-10 21:25:55 +01001687
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001688 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001689 wiphy->max_adj_channel_rssi_comp;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001690 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
Johannes Berg2a519312009-02-10 21:25:55 +01001691 if (!res)
1692 return NULL;
1693
Johannes Berg57fbcce2016-04-12 15:56:15 +02001694 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001695 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1696 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1697 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1698 regulatory_hint_found_beacon(wiphy, channel, gfp);
1699 } else {
1700 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1701 regulatory_hint_found_beacon(wiphy, channel, gfp);
1702 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001703
Beni Lev4ee3e062012-08-27 12:49:39 +03001704 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +01001705 /* cfg80211_bss_update gives us a referenced result */
1706 return &res->pub;
1707}
Peng Xu0b8fb822019-01-21 12:14:57 +02001708
1709struct cfg80211_bss *
1710cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1711 struct cfg80211_inform_bss *data,
1712 struct ieee80211_mgmt *mgmt, size_t len,
1713 gfp_t gfp)
1714{
Sara Sharon7011ba52019-01-21 12:25:59 +02001715 struct cfg80211_bss *res, *tmp_bss;
Peng Xu0b8fb822019-01-21 12:14:57 +02001716 const u8 *ie = mgmt->u.probe_resp.variable;
1717 const struct cfg80211_bss_ies *ies1, *ies2;
1718 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1719 u.probe_resp.variable);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001720 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001721
1722 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1723 len, NULL, gfp);
Sara Sharon213ed572019-01-16 23:02:03 +02001724 if (!res || !wiphy->support_mbssid ||
1725 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1726 return res;
1727 if (wiphy->support_only_he_mbssid &&
1728 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
Peng Xu0b8fb822019-01-21 12:14:57 +02001729 return res;
1730
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001731 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001732 /* process each non-transmitting bss */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001733 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1734 &non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001735
1736 /* check if the res has other nontransmitting bss which is not
1737 * in MBSSID IE
1738 */
1739 ies1 = rcu_access_pointer(res->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001740
1741 /* go through nontrans_list, if the timestamp of the BSS is
1742 * earlier than the timestamp of the transmitting BSS then
1743 * update it
1744 */
Sara Sharon7011ba52019-01-21 12:25:59 +02001745 list_for_each_entry(tmp_bss, &res->nontrans_list,
Peng Xu0b8fb822019-01-21 12:14:57 +02001746 nontrans_list) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001747 ies2 = rcu_access_pointer(tmp_bss->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001748 if (ies2->tsf < ies1->tsf)
1749 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1750 mgmt, len, gfp);
1751 }
1752
1753 return res;
1754}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001755EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
Johannes Berg2a519312009-02-10 21:25:55 +01001756
Johannes Berg5b112d32013-02-01 01:49:58 +01001757void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001758{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001759 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001760 struct cfg80211_internal_bss *bss;
1761
1762 if (!pub)
1763 return;
1764
1765 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001766
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001767 spin_lock_bh(&rdev->bss_lock);
1768 bss_ref_get(rdev, bss);
1769 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001770}
1771EXPORT_SYMBOL(cfg80211_ref_bss);
1772
Johannes Berg5b112d32013-02-01 01:49:58 +01001773void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg2a519312009-02-10 21:25:55 +01001774{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001775 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001776 struct cfg80211_internal_bss *bss;
1777
1778 if (!pub)
1779 return;
1780
1781 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001782
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001783 spin_lock_bh(&rdev->bss_lock);
1784 bss_ref_put(rdev, bss);
1785 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001786}
1787EXPORT_SYMBOL(cfg80211_put_bss);
1788
Johannes Bergd491af12009-02-10 21:25:58 +01001789void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1790{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001791 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Sara Sharon7011ba52019-01-21 12:25:59 +02001792 struct cfg80211_internal_bss *bss, *tmp1;
1793 struct cfg80211_bss *nontrans_bss, *tmp;
Johannes Bergd491af12009-02-10 21:25:58 +01001794
1795 if (WARN_ON(!pub))
1796 return;
1797
1798 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1799
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001800 spin_lock_bh(&rdev->bss_lock);
Sara Sharon7011ba52019-01-21 12:25:59 +02001801 if (list_empty(&bss->list))
1802 goto out;
Peng Xu0b8fb822019-01-21 12:14:57 +02001803
Sara Sharon7011ba52019-01-21 12:25:59 +02001804 list_for_each_entry_safe(nontrans_bss, tmp,
1805 &pub->nontrans_list,
1806 nontrans_list) {
1807 tmp1 = container_of(nontrans_bss,
1808 struct cfg80211_internal_bss, pub);
1809 if (__cfg80211_unlink_bss(rdev, tmp1))
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001810 rdev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +02001811 }
Sara Sharon7011ba52019-01-21 12:25:59 +02001812
1813 if (__cfg80211_unlink_bss(rdev, bss))
1814 rdev->bss_generation++;
1815out:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001816 spin_unlock_bh(&rdev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +01001817}
1818EXPORT_SYMBOL(cfg80211_unlink_bss);
1819
Johannes Berg3d23e342009-09-29 23:27:28 +02001820#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9f419f32013-05-08 21:34:22 +02001821static struct cfg80211_registered_device *
1822cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1823{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001824 struct cfg80211_registered_device *rdev;
Johannes Berg9f419f32013-05-08 21:34:22 +02001825 struct net_device *dev;
1826
Johannes Berg5fe231e2013-05-08 21:45:15 +02001827 ASSERT_RTNL();
1828
Johannes Berg9f419f32013-05-08 21:34:22 +02001829 dev = dev_get_by_index(net, ifindex);
1830 if (!dev)
Johannes Berg5fe231e2013-05-08 21:45:15 +02001831 return ERR_PTR(-ENODEV);
1832 if (dev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001833 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001834 else
Johannes Berg9f419f32013-05-08 21:34:22 +02001835 rdev = ERR_PTR(-ENODEV);
1836 dev_put(dev);
Johannes Berg9f419f32013-05-08 21:34:22 +02001837 return rdev;
1838}
1839
Johannes Berg2a519312009-02-10 21:25:55 +01001840int cfg80211_wext_siwscan(struct net_device *dev,
1841 struct iw_request_info *info,
1842 union iwreq_data *wrqu, char *extra)
1843{
1844 struct cfg80211_registered_device *rdev;
1845 struct wiphy *wiphy;
1846 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001847 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001848 int i, err, n_channels = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001849 enum nl80211_band band;
Johannes Berg2a519312009-02-10 21:25:55 +01001850
1851 if (!netif_running(dev))
1852 return -ENETDOWN;
1853
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001854 if (wrqu->data.length == sizeof(struct iw_scan_req))
1855 wreq = (struct iw_scan_req *)extra;
1856
Johannes Berg463d0182009-07-14 00:33:35 +02001857 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001858
1859 if (IS_ERR(rdev))
1860 return PTR_ERR(rdev);
1861
Johannes Bergf9d15d12014-01-22 11:14:19 +02001862 if (rdev->scan_req || rdev->scan_msg) {
Johannes Berg2a519312009-02-10 21:25:55 +01001863 err = -EBUSY;
1864 goto out;
1865 }
1866
1867 wiphy = &rdev->wiphy;
1868
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001869 /* Determine number of channels, needed to allocate creq */
1870 if (wreq && wreq->num_channels)
1871 n_channels = wreq->num_channels;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001872 else
1873 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001874
1875 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1876 n_channels * sizeof(void *),
1877 GFP_ATOMIC);
1878 if (!creq) {
1879 err = -ENOMEM;
1880 goto out;
1881 }
1882
1883 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +02001884 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +02001885 /* SSIDs come after channels */
1886 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01001887 creq->n_channels = n_channels;
1888 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -07001889 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001890
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001891 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +01001892 i = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001893 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg2a519312009-02-10 21:25:55 +01001894 int j;
Johannes Berg584991d2009-11-02 13:32:03 +01001895
Johannes Berg2a519312009-02-10 21:25:55 +01001896 if (!wiphy->bands[band])
1897 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01001898
Johannes Berg2a519312009-02-10 21:25:55 +01001899 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001900 /* ignore disabled channels */
1901 if (wiphy->bands[band]->channels[j].flags &
1902 IEEE80211_CHAN_DISABLED)
1903 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001904
1905 /* If we have a wireless request structure and the
1906 * wireless request specifies frequencies, then search
1907 * for the matching hardware channel.
1908 */
1909 if (wreq && wreq->num_channels) {
1910 int k;
1911 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1912 for (k = 0; k < wreq->num_channels; k++) {
Zhao, Gang96998e32014-04-09 09:28:06 +08001913 struct iw_freq *freq =
1914 &wreq->channel_list[k];
1915 int wext_freq =
1916 cfg80211_wext_freq(freq);
1917
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001918 if (wext_freq == wiphy_freq)
1919 goto wext_freq_found;
1920 }
1921 goto wext_freq_not_found;
1922 }
1923
1924 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001925 creq->channels[i] = &wiphy->bands[band]->channels[j];
1926 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001927 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001928 }
1929 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001930 /* No channels found? */
1931 if (!i) {
1932 err = -EINVAL;
1933 goto out;
1934 }
Johannes Berg2a519312009-02-10 21:25:55 +01001935
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001936 /* Set real number of channels specified in creq->channels[] */
1937 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001938
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001939 /* translate "Scan for SSID" request */
1940 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001941 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001942 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1943 err = -EINVAL;
1944 goto out;
1945 }
Johannes Berg2a519312009-02-10 21:25:55 +01001946 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1947 creq->ssids[0].ssid_len = wreq->essid_len;
1948 }
1949 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1950 creq->n_ssids = 0;
1951 }
1952
Johannes Berg57fbcce2016-04-12 15:56:15 +02001953 for (i = 0; i < NUM_NL80211_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001954 if (wiphy->bands[i])
1955 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001956
Jouni Malinen818965d2016-02-26 22:12:47 +02001957 eth_broadcast_addr(creq->bssid);
1958
Johannes Berg2a519312009-02-10 21:25:55 +01001959 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03001960 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001961 if (err) {
1962 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001963 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001964 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001965 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001966 /* creq now owned by driver */
1967 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001968 dev_hold(dev);
1969 }
Johannes Berg2a519312009-02-10 21:25:55 +01001970 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001971 kfree(creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001972 return err;
1973}
Johannes Berg2afe38d2015-01-06 14:00:53 +01001974EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001975
James Minor76a70e92015-02-24 12:58:20 -06001976static char *ieee80211_scan_add_ies(struct iw_request_info *info,
1977 const struct cfg80211_bss_ies *ies,
1978 char *current_ev, char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001979{
Johannes Berg9caf0362012-11-29 01:25:20 +01001980 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01001981 struct iw_event iwe;
1982
Johannes Berg9caf0362012-11-29 01:25:20 +01001983 if (!ies)
James Minor76a70e92015-02-24 12:58:20 -06001984 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01001985
1986 /*
1987 * If needed, fragment the IEs buffer (at IE boundaries) into short
1988 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1989 */
Johannes Berg9caf0362012-11-29 01:25:20 +01001990 pos = ies->data;
1991 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01001992
1993 while (end - pos > IW_GENERIC_IE_MAX) {
1994 next = pos + 2 + pos[1];
1995 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1996 next = next + 2 + next[1];
1997
1998 memset(&iwe, 0, sizeof(iwe));
1999 iwe.cmd = IWEVGENIE;
2000 iwe.u.data.length = next - pos;
James Minor76a70e92015-02-24 12:58:20 -06002001 current_ev = iwe_stream_add_point_check(info, current_ev,
2002 end_buf, &iwe,
2003 (void *)pos);
2004 if (IS_ERR(current_ev))
2005 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002006 pos = next;
2007 }
2008
2009 if (end > pos) {
2010 memset(&iwe, 0, sizeof(iwe));
2011 iwe.cmd = IWEVGENIE;
2012 iwe.u.data.length = end - pos;
James Minor76a70e92015-02-24 12:58:20 -06002013 current_ev = iwe_stream_add_point_check(info, current_ev,
2014 end_buf, &iwe,
2015 (void *)pos);
2016 if (IS_ERR(current_ev))
2017 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002018 }
James Minor76a70e92015-02-24 12:58:20 -06002019
2020 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002021}
2022
Johannes Berg2a519312009-02-10 21:25:55 +01002023static char *
Johannes Berg77965c972009-02-18 18:45:06 +01002024ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2025 struct cfg80211_internal_bss *bss, char *current_ev,
2026 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01002027{
Johannes Berg9caf0362012-11-29 01:25:20 +01002028 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01002029 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01002030 const u8 *ie;
James Minor76a70e92015-02-24 12:58:20 -06002031 u8 buf[50];
2032 u8 *cfg, *p, *tmp;
Johannes Berg9caf0362012-11-29 01:25:20 +01002033 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002034 bool ismesh = false;
2035
2036 memset(&iwe, 0, sizeof(iwe));
2037 iwe.cmd = SIOCGIWAP;
2038 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2039 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
James Minor76a70e92015-02-24 12:58:20 -06002040 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2041 IW_EV_ADDR_LEN);
2042 if (IS_ERR(current_ev))
2043 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002044
2045 memset(&iwe, 0, sizeof(iwe));
2046 iwe.cmd = SIOCGIWFREQ;
2047 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2048 iwe.u.freq.e = 0;
James Minor76a70e92015-02-24 12:58:20 -06002049 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2050 IW_EV_FREQ_LEN);
2051 if (IS_ERR(current_ev))
2052 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002053
2054 memset(&iwe, 0, sizeof(iwe));
2055 iwe.cmd = SIOCGIWFREQ;
2056 iwe.u.freq.m = bss->pub.channel->center_freq;
2057 iwe.u.freq.e = 6;
James Minor76a70e92015-02-24 12:58:20 -06002058 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2059 IW_EV_FREQ_LEN);
2060 if (IS_ERR(current_ev))
2061 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002062
Johannes Berg77965c972009-02-18 18:45:06 +01002063 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01002064 memset(&iwe, 0, sizeof(iwe));
2065 iwe.cmd = IWEVQUAL;
2066 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2067 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01002068 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c972009-02-18 18:45:06 +01002069 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002070 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01002071 sig = bss->pub.signal / 100;
2072 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002073 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01002074 if (sig < -110) /* rather bad */
2075 sig = -110;
2076 else if (sig > -40) /* perfect */
2077 sig = -40;
2078 /* will give a range of 0 .. 70 */
2079 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01002080 break;
2081 case CFG80211_SIGNAL_TYPE_UNSPEC:
2082 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01002083 /* will give range 0 .. 100 */
2084 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01002085 break;
2086 default:
2087 /* not reached */
2088 break;
2089 }
James Minor76a70e92015-02-24 12:58:20 -06002090 current_ev = iwe_stream_add_event_check(info, current_ev,
2091 end_buf, &iwe,
2092 IW_EV_QUAL_LEN);
2093 if (IS_ERR(current_ev))
2094 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002095 }
2096
2097 memset(&iwe, 0, sizeof(iwe));
2098 iwe.cmd = SIOCGIWENCODE;
2099 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2100 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2101 else
2102 iwe.u.data.flags = IW_ENCODE_DISABLED;
2103 iwe.u.data.length = 0;
James Minor76a70e92015-02-24 12:58:20 -06002104 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2105 &iwe, "");
2106 if (IS_ERR(current_ev))
2107 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002108
Johannes Berg9caf0362012-11-29 01:25:20 +01002109 rcu_read_lock();
2110 ies = rcu_dereference(bss->pub.ies);
Johannes Berg83c7aa12013-02-05 16:51:29 +01002111 rem = ies->len;
2112 ie = ies->data;
Johannes Berg9caf0362012-11-29 01:25:20 +01002113
Johannes Berg83c7aa12013-02-05 16:51:29 +01002114 while (rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01002115 /* invalid data */
2116 if (ie[1] > rem - 2)
2117 break;
2118
2119 switch (ie[0]) {
2120 case WLAN_EID_SSID:
2121 memset(&iwe, 0, sizeof(iwe));
2122 iwe.cmd = SIOCGIWESSID;
2123 iwe.u.data.length = ie[1];
2124 iwe.u.data.flags = 1;
James Minor76a70e92015-02-24 12:58:20 -06002125 current_ev = iwe_stream_add_point_check(info,
2126 current_ev,
2127 end_buf, &iwe,
2128 (u8 *)ie + 2);
2129 if (IS_ERR(current_ev))
2130 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002131 break;
2132 case WLAN_EID_MESH_ID:
2133 memset(&iwe, 0, sizeof(iwe));
2134 iwe.cmd = SIOCGIWESSID;
2135 iwe.u.data.length = ie[1];
2136 iwe.u.data.flags = 1;
James Minor76a70e92015-02-24 12:58:20 -06002137 current_ev = iwe_stream_add_point_check(info,
2138 current_ev,
2139 end_buf, &iwe,
2140 (u8 *)ie + 2);
2141 if (IS_ERR(current_ev))
2142 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002143 break;
2144 case WLAN_EID_MESH_CONFIG:
2145 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00002146 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01002147 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01002148 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01002149 memset(&iwe, 0, sizeof(iwe));
2150 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002151 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2152 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01002153 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002154 current_ev = iwe_stream_add_point_check(info,
2155 current_ev,
2156 end_buf,
2157 &iwe, buf);
2158 if (IS_ERR(current_ev))
2159 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002160 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2161 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01002162 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002163 current_ev = iwe_stream_add_point_check(info,
2164 current_ev,
2165 end_buf,
2166 &iwe, buf);
2167 if (IS_ERR(current_ev))
2168 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002169 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2170 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01002171 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002172 current_ev = iwe_stream_add_point_check(info,
2173 current_ev,
2174 end_buf,
2175 &iwe, buf);
2176 if (IS_ERR(current_ev))
2177 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002178 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01002179 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002180 current_ev = iwe_stream_add_point_check(info,
2181 current_ev,
2182 end_buf,
2183 &iwe, buf);
2184 if (IS_ERR(current_ev))
2185 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002186 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2187 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002188 current_ev = iwe_stream_add_point_check(info,
2189 current_ev,
2190 end_buf,
2191 &iwe, buf);
2192 if (IS_ERR(current_ev))
2193 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002194 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2195 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002196 current_ev = iwe_stream_add_point_check(info,
2197 current_ev,
2198 end_buf,
2199 &iwe, buf);
2200 if (IS_ERR(current_ev))
2201 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002202 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01002203 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002204 current_ev = iwe_stream_add_point_check(info,
2205 current_ev,
2206 end_buf,
2207 &iwe, buf);
2208 if (IS_ERR(current_ev))
2209 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002210 break;
2211 case WLAN_EID_SUPP_RATES:
2212 case WLAN_EID_EXT_SUPP_RATES:
2213 /* display all supported rates in readable format */
2214 p = current_ev + iwe_stream_lcp_len(info);
2215
2216 memset(&iwe, 0, sizeof(iwe));
2217 iwe.cmd = SIOCGIWRATE;
2218 /* Those two flags are ignored... */
2219 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2220
2221 for (i = 0; i < ie[1]; i++) {
2222 iwe.u.bitrate.value =
2223 ((ie[i + 2] & 0x7f) * 500000);
James Minor76a70e92015-02-24 12:58:20 -06002224 tmp = p;
Johannes Berg2a519312009-02-10 21:25:55 +01002225 p = iwe_stream_add_value(info, current_ev, p,
James Minor76a70e92015-02-24 12:58:20 -06002226 end_buf, &iwe,
2227 IW_EV_PARAM_LEN);
2228 if (p == tmp) {
2229 current_ev = ERR_PTR(-E2BIG);
2230 goto unlock;
2231 }
Johannes Berg2a519312009-02-10 21:25:55 +01002232 }
2233 current_ev = p;
2234 break;
2235 }
2236 rem -= ie[1] + 2;
2237 ie += ie[1] + 2;
2238 }
2239
Joe Perchesf64f9e72009-11-29 16:55:45 -08002240 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2241 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01002242 memset(&iwe, 0, sizeof(iwe));
2243 iwe.cmd = SIOCGIWMODE;
2244 if (ismesh)
2245 iwe.u.mode = IW_MODE_MESH;
2246 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2247 iwe.u.mode = IW_MODE_MASTER;
2248 else
2249 iwe.u.mode = IW_MODE_ADHOC;
James Minor76a70e92015-02-24 12:58:20 -06002250 current_ev = iwe_stream_add_event_check(info, current_ev,
2251 end_buf, &iwe,
2252 IW_EV_UINT_LEN);
2253 if (IS_ERR(current_ev))
2254 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002255 }
2256
James Minor76a70e92015-02-24 12:58:20 -06002257 memset(&iwe, 0, sizeof(iwe));
2258 iwe.cmd = IWEVCUSTOM;
2259 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2260 iwe.u.data.length = strlen(buf);
2261 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2262 &iwe, buf);
2263 if (IS_ERR(current_ev))
2264 goto unlock;
2265 memset(&iwe, 0, sizeof(iwe));
2266 iwe.cmd = IWEVCUSTOM;
2267 sprintf(buf, " Last beacon: %ums ago",
2268 elapsed_jiffies_msecs(bss->ts));
2269 iwe.u.data.length = strlen(buf);
2270 current_ev = iwe_stream_add_point_check(info, current_ev,
2271 end_buf, &iwe, buf);
2272 if (IS_ERR(current_ev))
2273 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002274
James Minor76a70e92015-02-24 12:58:20 -06002275 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2276
2277 unlock:
Johannes Berg9caf0362012-11-29 01:25:20 +01002278 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01002279 return current_ev;
2280}
2281
2282
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002283static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +01002284 struct iw_request_info *info,
2285 char *buf, size_t len)
2286{
2287 char *current_ev = buf;
2288 char *end_buf = buf + len;
2289 struct cfg80211_internal_bss *bss;
James Minor76a70e92015-02-24 12:58:20 -06002290 int err = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002291
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002292 spin_lock_bh(&rdev->bss_lock);
2293 cfg80211_bss_expire(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01002294
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002295 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01002296 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
James Minor76a70e92015-02-24 12:58:20 -06002297 err = -E2BIG;
2298 break;
Johannes Berg2a519312009-02-10 21:25:55 +01002299 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002300 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
Johannes Berg77965c972009-02-18 18:45:06 +01002301 current_ev, end_buf);
James Minor76a70e92015-02-24 12:58:20 -06002302 if (IS_ERR(current_ev)) {
2303 err = PTR_ERR(current_ev);
2304 break;
2305 }
Johannes Berg2a519312009-02-10 21:25:55 +01002306 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002307 spin_unlock_bh(&rdev->bss_lock);
James Minor76a70e92015-02-24 12:58:20 -06002308
2309 if (err)
2310 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01002311 return current_ev - buf;
2312}
2313
2314
2315int cfg80211_wext_giwscan(struct net_device *dev,
2316 struct iw_request_info *info,
2317 struct iw_point *data, char *extra)
2318{
2319 struct cfg80211_registered_device *rdev;
2320 int res;
2321
2322 if (!netif_running(dev))
2323 return -ENETDOWN;
2324
Johannes Berg463d0182009-07-14 00:33:35 +02002325 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01002326
2327 if (IS_ERR(rdev))
2328 return PTR_ERR(rdev);
2329
Johannes Bergf9d15d12014-01-22 11:14:19 +02002330 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg5fe231e2013-05-08 21:45:15 +02002331 return -EAGAIN;
Johannes Berg2a519312009-02-10 21:25:55 +01002332
2333 res = ieee80211_scan_results(rdev, info, extra, data->length);
2334 data->length = 0;
2335 if (res >= 0) {
2336 data->length = res;
2337 res = 0;
2338 }
2339
Johannes Berg2a519312009-02-10 21:25:55 +01002340 return res;
2341}
Johannes Berg2afe38d2015-01-06 14:00:53 +01002342EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01002343#endif