blob: c04f5451f89b114cb98c43b1b3ce6724858f2a4f [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
Sara Sharonf7dacfb2019-03-15 17:39:03 +0200182bool cfg80211_is_element_inherited(const struct element *elem,
183 const struct element *non_inherit_elem)
184{
185 u8 id_len, ext_id_len, i, loop_len, id;
186 const u8 *list;
187
188 if (elem->id == WLAN_EID_MULTIPLE_BSSID)
189 return false;
190
191 if (!non_inherit_elem || non_inherit_elem->datalen < 2)
192 return true;
193
194 /*
195 * non inheritance element format is:
196 * ext ID (56) | IDs list len | list | extension IDs list len | list
197 * Both lists are optional. Both lengths are mandatory.
198 * This means valid length is:
199 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
200 */
201 id_len = non_inherit_elem->data[1];
202 if (non_inherit_elem->datalen < 3 + id_len)
203 return true;
204
205 ext_id_len = non_inherit_elem->data[2 + id_len];
206 if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
207 return true;
208
209 if (elem->id == WLAN_EID_EXTENSION) {
210 if (!ext_id_len)
211 return true;
212 loop_len = ext_id_len;
213 list = &non_inherit_elem->data[3 + id_len];
214 id = elem->data[0];
215 } else {
216 if (!id_len)
217 return true;
218 loop_len = id_len;
219 list = &non_inherit_elem->data[2];
220 id = elem->id;
221 }
222
223 for (i = 0; i < loop_len; i++) {
224 if (list[i] == id)
225 return false;
226 }
227
228 return true;
229}
230EXPORT_SYMBOL(cfg80211_is_element_inherited);
231
Peng Xu0b8fb822019-01-21 12:14:57 +0200232static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
233 const u8 *subelement, size_t subie_len,
234 u8 *new_ie, gfp_t gfp)
235{
236 u8 *pos, *tmp;
237 const u8 *tmp_old, *tmp_new;
Sara Sharonf7dacfb2019-03-15 17:39:03 +0200238 const struct element *non_inherit_elem;
Peng Xu0b8fb822019-01-21 12:14:57 +0200239 u8 *sub_copy;
240
241 /* copy subelement as we need to change its content to
242 * mark an ie after it is processed.
243 */
YueHaibing90abf962019-02-25 12:38:49 +0000244 sub_copy = kmemdup(subelement, subie_len, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +0200245 if (!sub_copy)
246 return 0;
Peng Xu0b8fb822019-01-21 12:14:57 +0200247
248 pos = &new_ie[0];
249
250 /* set new ssid */
251 tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
252 if (tmp_new) {
253 memcpy(pos, tmp_new, tmp_new[1] + 2);
254 pos += (tmp_new[1] + 2);
255 }
256
Sara Sharonf7dacfb2019-03-15 17:39:03 +0200257 /* get non inheritance list if exists */
258 non_inherit_elem =
259 cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
260 sub_copy, subie_len);
261
Peng Xu0b8fb822019-01-21 12:14:57 +0200262 /* go through IEs in ie (skip SSID) and subelement,
263 * merge them into new_ie
264 */
265 tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
266 tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
267
268 while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
269 if (tmp_old[0] == 0) {
270 tmp_old++;
271 continue;
272 }
273
Sara Sharonc17fe042019-01-29 14:00:58 +0200274 if (tmp_old[0] == WLAN_EID_EXTENSION)
275 tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
276 subie_len);
277 else
278 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
279 subie_len);
280
Peng Xu0b8fb822019-01-21 12:14:57 +0200281 if (!tmp) {
Sara Sharonf7dacfb2019-03-15 17:39:03 +0200282 const struct element *old_elem = (void *)tmp_old;
283
Peng Xu0b8fb822019-01-21 12:14:57 +0200284 /* ie in old ie but not in subelement */
Sara Sharonf7dacfb2019-03-15 17:39:03 +0200285 if (cfg80211_is_element_inherited(old_elem,
286 non_inherit_elem)) {
Peng Xu0b8fb822019-01-21 12:14:57 +0200287 memcpy(pos, tmp_old, tmp_old[1] + 2);
288 pos += tmp_old[1] + 2;
289 }
290 } else {
291 /* ie in transmitting ie also in subelement,
292 * copy from subelement and flag the ie in subelement
Sara Sharonc17fe042019-01-29 14:00:58 +0200293 * as copied (by setting eid field to WLAN_EID_SSID,
294 * which is skipped anyway).
295 * For vendor ie, compare OUI + type + subType to
Peng Xu0b8fb822019-01-21 12:14:57 +0200296 * determine if they are the same ie.
297 */
298 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
299 if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
300 /* same vendor ie, copy from
301 * subelement
302 */
303 memcpy(pos, tmp, tmp[1] + 2);
304 pos += tmp[1] + 2;
Sara Sharonc17fe042019-01-29 14:00:58 +0200305 tmp[0] = WLAN_EID_SSID;
Peng Xu0b8fb822019-01-21 12:14:57 +0200306 } else {
307 memcpy(pos, tmp_old, tmp_old[1] + 2);
308 pos += tmp_old[1] + 2;
309 }
310 } else {
311 /* copy ie from subelement into new ie */
312 memcpy(pos, tmp, tmp[1] + 2);
313 pos += tmp[1] + 2;
Sara Sharonc17fe042019-01-29 14:00:58 +0200314 tmp[0] = WLAN_EID_SSID;
Peng Xu0b8fb822019-01-21 12:14:57 +0200315 }
316 }
317
318 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
319 break;
320
321 tmp_old += tmp_old[1] + 2;
322 }
323
324 /* go through subelement again to check if there is any ie not
325 * copied to new ie, skip ssid, capability, bssid-index ie
326 */
327 tmp_new = sub_copy;
328 while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
329 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
Sara Sharon5bd9d102019-03-15 17:39:02 +0200330 tmp_new[0] == WLAN_EID_SSID)) {
Peng Xu0b8fb822019-01-21 12:14:57 +0200331 memcpy(pos, tmp_new, tmp_new[1] + 2);
332 pos += tmp_new[1] + 2;
333 }
334 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
335 break;
336 tmp_new += tmp_new[1] + 2;
337 }
338
339 kfree(sub_copy);
340 return pos - new_ie;
341}
342
343static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
344 const u8 *ssid, size_t ssid_len)
345{
346 const struct cfg80211_bss_ies *ies;
347 const u8 *ssidie;
348
349 if (bssid && !ether_addr_equal(a->bssid, bssid))
350 return false;
351
352 if (!ssid)
353 return true;
354
355 ies = rcu_access_pointer(a->ies);
356 if (!ies)
357 return false;
358 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
359 if (!ssidie)
360 return false;
361 if (ssidie[1] != ssid_len)
362 return false;
363 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
364}
365
366static int
Sara Sharon7011ba52019-01-21 12:25:59 +0200367cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
368 struct cfg80211_bss *nontrans_bss)
Peng Xu0b8fb822019-01-21 12:14:57 +0200369{
370 const u8 *ssid;
371 size_t ssid_len;
Sara Sharon7011ba52019-01-21 12:25:59 +0200372 struct cfg80211_bss *bss = NULL;
Peng Xu0b8fb822019-01-21 12:14:57 +0200373
374 rcu_read_lock();
Sara Sharon7011ba52019-01-21 12:25:59 +0200375 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
Peng Xu0b8fb822019-01-21 12:14:57 +0200376 if (!ssid) {
377 rcu_read_unlock();
378 return -EINVAL;
379 }
380 ssid_len = ssid[1];
381 ssid = ssid + 2;
382 rcu_read_unlock();
383
384 /* check if nontrans_bss is in the list */
385 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
Sara Sharon7011ba52019-01-21 12:25:59 +0200386 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
Peng Xu0b8fb822019-01-21 12:14:57 +0200387 return 0;
388 }
389
390 /* add to the list */
391 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
392 return 0;
393}
394
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800395static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
Sam Leffler15d60302012-10-11 21:03:34 -0700396 unsigned long expire_time)
397{
398 struct cfg80211_internal_bss *bss, *tmp;
399 bool expired = false;
400
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800401 lockdep_assert_held(&rdev->bss_lock);
Johannes Berg4b1af472013-02-01 01:05:43 +0100402
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800403 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
Sam Leffler15d60302012-10-11 21:03:34 -0700404 if (atomic_read(&bss->hold))
405 continue;
406 if (!time_after(expire_time, bss->ts))
407 continue;
408
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800409 if (__cfg80211_unlink_bss(rdev, bss))
Johannes Berg776b3582013-02-01 02:06:18 +0100410 expired = true;
Sam Leffler15d60302012-10-11 21:03:34 -0700411 }
412
413 if (expired)
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800414 rdev->bss_generation++;
Sam Leffler15d60302012-10-11 21:03:34 -0700415}
416
Johannes Berg9853a552016-11-15 12:05:11 +0100417static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
418{
419 struct cfg80211_internal_bss *bss, *oldest = NULL;
420 bool ret;
421
422 lockdep_assert_held(&rdev->bss_lock);
423
424 list_for_each_entry(bss, &rdev->bss_list, list) {
425 if (atomic_read(&bss->hold))
426 continue;
427
428 if (!list_empty(&bss->hidden_list) &&
429 !bss->pub.hidden_beacon_bss)
430 continue;
431
432 if (oldest && time_before(oldest->ts, bss->ts))
433 continue;
434 oldest = bss;
435 }
436
437 if (WARN_ON(!oldest))
438 return false;
439
440 /*
441 * The callers make sure to increase rdev->bss_generation if anything
442 * gets removed (and a new entry added), so there's no need to also do
443 * it here.
444 */
445
446 ret = __cfg80211_unlink_bss(rdev, oldest);
447 WARN_ON(!ret);
448 return ret;
449}
450
Johannes Bergf9d15d12014-01-22 11:14:19 +0200451void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
452 bool send_message)
Johannes Berg2a519312009-02-10 21:25:55 +0100453{
Johannes Berg667503d2009-07-07 03:56:11 +0200454 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +0200455 struct wireless_dev *wdev;
Johannes Bergf9d15d12014-01-22 11:14:19 +0200456 struct sk_buff *msg;
Johannes Berg3d23e342009-09-29 23:27:28 +0200457#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100458 union iwreq_data wrqu;
459#endif
460
Johannes Berg5fe231e2013-05-08 21:45:15 +0200461 ASSERT_RTNL();
Johannes Berg01a0ac42009-08-20 21:36:16 +0200462
Johannes Bergf9d15d12014-01-22 11:14:19 +0200463 if (rdev->scan_msg) {
Arend Van Spriel505a2e82016-12-16 11:21:54 +0000464 nl80211_send_scan_msg(rdev, rdev->scan_msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200465 rdev->scan_msg = NULL;
466 return;
467 }
Johannes Berg667503d2009-07-07 03:56:11 +0200468
Johannes Bergf9d15d12014-01-22 11:14:19 +0200469 request = rdev->scan_req;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200470 if (!request)
471 return;
472
Johannes Bergfd014282012-06-18 19:17:03 +0200473 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +0100474
Johannes Berg6829c872009-07-02 09:13:27 +0200475 /*
476 * This must be before sending the other events!
477 * Otherwise, wpa_supplicant gets completely confused with
478 * wext events.
479 */
Johannes Bergfd014282012-06-18 19:17:03 +0200480 if (wdev->netdev)
481 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200482
Avraham Stern1d762502016-07-05 17:10:13 +0300483 if (!request->info.aborted &&
Johannes Bergf9d15d12014-01-22 11:14:19 +0200484 request->flags & NL80211_SCAN_FLAG_FLUSH) {
485 /* flush entries from previous scans */
486 spin_lock_bh(&rdev->bss_lock);
487 __cfg80211_bss_expire(rdev, request->scan_start);
488 spin_unlock_bh(&rdev->bss_lock);
Sam Leffler15d60302012-10-11 21:03:34 -0700489 }
Johannes Berg2a519312009-02-10 21:25:55 +0100490
Avraham Stern1d762502016-07-05 17:10:13 +0300491 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200492
Johannes Berg3d23e342009-09-29 23:27:28 +0200493#ifdef CONFIG_CFG80211_WEXT
Avraham Stern1d762502016-07-05 17:10:13 +0300494 if (wdev->netdev && !request->info.aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100495 memset(&wrqu, 0, sizeof(wrqu));
496
Johannes Bergfd014282012-06-18 19:17:03 +0200497 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100498 }
499#endif
500
Johannes Bergfd014282012-06-18 19:17:03 +0200501 if (wdev->netdev)
502 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100503
Johannes Berg36e6fea2009-08-12 22:21:21 +0200504 rdev->scan_req = NULL;
Eliad Peller4a58e7c2013-12-05 18:30:17 +0200505 kfree(request);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200506
507 if (!send_message)
508 rdev->scan_msg = msg;
509 else
Arend Van Spriel505a2e82016-12-16 11:21:54 +0000510 nl80211_send_scan_msg(rdev, msg);
Johannes Berg2a519312009-02-10 21:25:55 +0100511}
Johannes Berg667503d2009-07-07 03:56:11 +0200512
Johannes Berg36e6fea2009-08-12 22:21:21 +0200513void __cfg80211_scan_done(struct work_struct *wk)
514{
515 struct cfg80211_registered_device *rdev;
516
517 rdev = container_of(wk, struct cfg80211_registered_device,
518 scan_done_wk);
519
Johannes Berg5fe231e2013-05-08 21:45:15 +0200520 rtnl_lock();
Johannes Bergf9d15d12014-01-22 11:14:19 +0200521 ___cfg80211_scan_done(rdev, true);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200522 rtnl_unlock();
Johannes Berg36e6fea2009-08-12 22:21:21 +0200523}
524
Avraham Stern1d762502016-07-05 17:10:13 +0300525void cfg80211_scan_done(struct cfg80211_scan_request *request,
526 struct cfg80211_scan_info *info)
Johannes Berg667503d2009-07-07 03:56:11 +0200527{
Avraham Stern1d762502016-07-05 17:10:13 +0300528 trace_cfg80211_scan_done(request, info);
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800529 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
Johannes Berg667503d2009-07-07 03:56:11 +0200530
Avraham Stern1d762502016-07-05 17:10:13 +0300531 request->info = *info;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200532 request->notified = true;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800533 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +0200534}
Johannes Berg2a519312009-02-10 21:25:55 +0100535EXPORT_SYMBOL(cfg80211_scan_done);
536
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100537void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
538 struct cfg80211_sched_scan_request *req)
539{
540 ASSERT_RTNL();
541
542 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
543}
544
545static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
546 struct cfg80211_sched_scan_request *req)
547{
548 ASSERT_RTNL();
549
550 list_del_rcu(&req->list);
551 kfree_rcu(req, rcu_head);
552}
553
554static struct cfg80211_sched_scan_request *
555cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
556{
557 struct cfg80211_sched_scan_request *pos;
558
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100559 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100560
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100561 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100562 if (pos->reqid == reqid)
563 return pos;
564 }
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100565 return NULL;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100566}
567
568/*
569 * Determines if a scheduled scan request can be handled. When a legacy
570 * scheduled scan is running no other scheduled scan is allowed regardless
571 * whether the request is for legacy or multi-support scan. When a multi-support
572 * scheduled scan is running a request for legacy scan is not allowed. In this
573 * case a request for multi-support scan can be handled if resources are
574 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
575 */
576int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
577 bool want_multi)
578{
579 struct cfg80211_sched_scan_request *pos;
580 int i = 0;
581
582 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
583 /* request id zero means legacy in progress */
584 if (!i && !pos->reqid)
585 return -EINPROGRESS;
586 i++;
587 }
588
589 if (i) {
590 /* no legacy allowed when multi request(s) are active */
591 if (!want_multi)
592 return -EINPROGRESS;
593
594 /* resource limit reached */
595 if (i == rdev->wiphy.max_sched_scan_reqs)
596 return -ENOSPC;
597 }
598 return 0;
599}
600
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100601void cfg80211_sched_scan_results_wk(struct work_struct *work)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300602{
603 struct cfg80211_registered_device *rdev;
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100604 struct cfg80211_sched_scan_request *req, *tmp;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300605
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100606 rdev = container_of(work, struct cfg80211_registered_device,
607 sched_scan_res_wk);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300608
Johannes Berg5fe231e2013-05-08 21:45:15 +0200609 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100610 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
611 if (req->report_results) {
612 req->report_results = false;
613 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
614 /* flush entries from previous scans */
615 spin_lock_bh(&rdev->bss_lock);
616 __cfg80211_bss_expire(rdev, req->scan_start);
617 spin_unlock_bh(&rdev->bss_lock);
618 req->scan_start = jiffies;
619 }
620 nl80211_send_sched_scan(req,
621 NL80211_CMD_SCHED_SCAN_RESULTS);
Sam Leffler15d60302012-10-11 21:03:34 -0700622 }
Sam Leffler15d60302012-10-11 21:03:34 -0700623 }
Johannes Berg5fe231e2013-05-08 21:45:15 +0200624 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300625}
626
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100627void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300628{
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100629 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
630 struct cfg80211_sched_scan_request *request;
631
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100632 trace_cfg80211_sched_scan_results(wiphy, reqid);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300633 /* ignore if we're not scanning */
Jukka Rissanen31a60ed2014-12-15 13:25:38 +0200634
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100635 rcu_read_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100636 request = cfg80211_find_sched_scan_req(rdev, reqid);
637 if (request) {
638 request->report_results = true;
639 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
640 }
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100641 rcu_read_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300642}
643EXPORT_SYMBOL(cfg80211_sched_scan_results);
644
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100645void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300646{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800647 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300648
Eliad Peller792e6aa2014-04-30 16:14:23 +0300649 ASSERT_RTNL();
650
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100651 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
Beni Lev4ee3e062012-08-27 12:49:39 +0300652
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100653 __cfg80211_stop_sched_scan(rdev, reqid, true);
Eliad Peller792e6aa2014-04-30 16:14:23 +0300654}
655EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
656
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100657void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
Eliad Peller792e6aa2014-04-30 16:14:23 +0300658{
659 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100660 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200661 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300662}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300663EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
664
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100665int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
666 struct cfg80211_sched_scan_request *req,
667 bool driver_initiated)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300668{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200669 ASSERT_RTNL();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300670
Luciano Coelho85a99942011-05-12 16:28:29 +0300671 if (!driver_initiated) {
Arend Van Spriel3a3ecf12017-04-21 13:05:02 +0100672 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
Luciano Coelho85a99942011-05-12 16:28:29 +0300673 if (err)
674 return err;
675 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300676
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100677 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300678
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100679 cfg80211_del_sched_scan_req(rdev, req);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300680
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200681 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300682}
683
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100684int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
685 u64 reqid, bool driver_initiated)
686{
687 struct cfg80211_sched_scan_request *sched_scan_req;
688
689 ASSERT_RTNL();
690
691 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100692 if (!sched_scan_req)
693 return -ENOENT;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100694
695 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
696 driver_initiated);
697}
698
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800699void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500700 unsigned long age_secs)
701{
702 struct cfg80211_internal_bss *bss;
703 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
704
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800705 spin_lock_bh(&rdev->bss_lock);
706 list_for_each_entry(bss, &rdev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500707 bss->ts -= age_jiffies;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800708 spin_unlock_bh(&rdev->bss_lock);
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500709}
710
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800711void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
Johannes Berg2a519312009-02-10 21:25:55 +0100712{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800713 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100714}
715
Johannes Berg49a68e02019-02-07 23:26:38 +0100716const struct element *
717cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
718 const u8 *match, unsigned int match_len,
719 unsigned int match_offset)
Johannes Berg2a519312009-02-10 21:25:55 +0100720{
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100721 const struct element *elem;
722
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100723 for_each_element_id(elem, eid, ies, len) {
Johannes Berg49a68e02019-02-07 23:26:38 +0100724 if (elem->datalen >= match_offset + match_len &&
725 !memcmp(elem->data + match_offset, match, match_len))
726 return elem;
Johannes Berg2a519312009-02-10 21:25:55 +0100727 }
Luca Coelhofbd05e42016-09-15 18:15:09 +0300728
729 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100730}
Johannes Berg49a68e02019-02-07 23:26:38 +0100731EXPORT_SYMBOL(cfg80211_find_elem_match);
Johannes Berg2a519312009-02-10 21:25:55 +0100732
Johannes Berg49a68e02019-02-07 23:26:38 +0100733const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
734 const u8 *ies,
735 unsigned int len)
Eliad Peller0c28ec52011-09-15 11:53:01 +0300736{
Johannes Berg49a68e02019-02-07 23:26:38 +0100737 const struct element *elem;
Luca Coelhofbd05e42016-09-15 18:15:09 +0300738 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
739 int match_len = (oui_type < 0) ? 3 : sizeof(match);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300740
Emmanuel Grumbach9e9ea432016-05-03 16:08:07 +0300741 if (WARN_ON(oui_type > 0xff))
742 return NULL;
743
Johannes Berg49a68e02019-02-07 23:26:38 +0100744 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
745 match, match_len, 0);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300746
Johannes Berg49a68e02019-02-07 23:26:38 +0100747 if (!elem || elem->datalen < 4)
Luca Coelhofbd05e42016-09-15 18:15:09 +0300748 return NULL;
Luciano Coelho67194292013-02-12 20:11:38 +0200749
Johannes Berg49a68e02019-02-07 23:26:38 +0100750 return elem;
Eliad Peller0c28ec52011-09-15 11:53:01 +0300751}
Johannes Berg49a68e02019-02-07 23:26:38 +0100752EXPORT_SYMBOL(cfg80211_find_vendor_elem);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300753
Johannes Berg4593c4c2013-02-01 19:20:03 +0100754/**
755 * enum bss_compare_mode - BSS compare mode
756 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
757 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
758 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
759 */
760enum bss_compare_mode {
761 BSS_CMP_REGULAR,
762 BSS_CMP_HIDE_ZLEN,
763 BSS_CMP_HIDE_NUL,
764};
765
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100766static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100767 struct cfg80211_bss *b,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100768 enum bss_compare_mode mode)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100769{
Johannes Berg9caf0362012-11-29 01:25:20 +0100770 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg3af63412013-01-30 00:40:20 +0100771 const u8 *ie1 = NULL;
772 const u8 *ie2 = NULL;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100773 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100774
Johannes Berg3af63412013-01-30 00:40:20 +0100775 if (a->channel != b->channel)
776 return b->channel->center_freq - a->channel->center_freq;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100777
Johannes Berg9caf0362012-11-29 01:25:20 +0100778 a_ies = rcu_access_pointer(a->ies);
779 if (!a_ies)
780 return -1;
781 b_ies = rcu_access_pointer(b->ies);
782 if (!b_ies)
783 return 1;
784
Johannes Berg3af63412013-01-30 00:40:20 +0100785 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
786 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
787 a_ies->data, a_ies->len);
788 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
789 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
790 b_ies->data, b_ies->len);
791 if (ie1 && ie2) {
792 int mesh_id_cmp;
793
794 if (ie1[1] == ie2[1])
795 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
796 else
797 mesh_id_cmp = ie2[1] - ie1[1];
798
799 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
800 a_ies->data, a_ies->len);
801 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
802 b_ies->data, b_ies->len);
803 if (ie1 && ie2) {
804 if (mesh_id_cmp)
805 return mesh_id_cmp;
806 if (ie1[1] != ie2[1])
807 return ie2[1] - ie1[1];
808 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
809 }
810 }
811
Johannes Berg3af63412013-01-30 00:40:20 +0100812 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
813 if (r)
814 return r;
815
Johannes Berg9caf0362012-11-29 01:25:20 +0100816 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
817 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100818
Johannes Berg5622f5b2013-01-30 00:26:45 +0100819 if (!ie1 && !ie2)
820 return 0;
821
Johannes Bergf94f8b12012-11-28 22:42:34 +0100822 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100823 * Note that with "hide_ssid", the function returns a match if
824 * the already-present BSS ("b") is a hidden SSID beacon for
825 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100826 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100827
828 /* sort missing IE before (left of) present IE */
829 if (!ie1)
830 return -1;
831 if (!ie2)
832 return 1;
833
Johannes Berg4593c4c2013-02-01 19:20:03 +0100834 switch (mode) {
835 case BSS_CMP_HIDE_ZLEN:
836 /*
837 * In ZLEN mode we assume the BSS entry we're
838 * looking for has a zero-length SSID. So if
839 * the one we're looking at right now has that,
840 * return 0. Otherwise, return the difference
841 * in length, but since we're looking for the
842 * 0-length it's really equivalent to returning
843 * the length of the one we're looking at.
844 *
845 * No content comparison is needed as we assume
846 * the content length is zero.
847 */
848 return ie2[1];
849 case BSS_CMP_REGULAR:
850 default:
851 /* sort by length first, then by contents */
852 if (ie1[1] != ie2[1])
853 return ie2[1] - ie1[1];
Johannes Berg5622f5b2013-01-30 00:26:45 +0100854 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100855 case BSS_CMP_HIDE_NUL:
856 if (ie1[1] != ie2[1])
857 return ie2[1] - ie1[1];
858 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
859 for (i = 0; i < ie2[1]; i++)
860 if (ie2[i + 2])
861 return -1;
862 return 0;
863 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100864}
865
Dedy Lansky6eb18132015-02-08 15:52:03 +0200866static bool cfg80211_bss_type_match(u16 capability,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200867 enum nl80211_band band,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200868 enum ieee80211_bss_type bss_type)
869{
870 bool ret = true;
871 u16 mask, val;
872
873 if (bss_type == IEEE80211_BSS_TYPE_ANY)
874 return ret;
875
Johannes Berg57fbcce2016-04-12 15:56:15 +0200876 if (band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200877 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
878 switch (bss_type) {
879 case IEEE80211_BSS_TYPE_ESS:
880 val = WLAN_CAPABILITY_DMG_TYPE_AP;
881 break;
882 case IEEE80211_BSS_TYPE_PBSS:
883 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
884 break;
885 case IEEE80211_BSS_TYPE_IBSS:
886 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
887 break;
888 default:
889 return false;
890 }
891 } else {
892 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
893 switch (bss_type) {
894 case IEEE80211_BSS_TYPE_ESS:
895 val = WLAN_CAPABILITY_ESS;
896 break;
897 case IEEE80211_BSS_TYPE_IBSS:
898 val = WLAN_CAPABILITY_IBSS;
899 break;
900 case IEEE80211_BSS_TYPE_MBSS:
901 val = 0;
902 break;
903 default:
904 return false;
905 }
906 }
907
908 ret = ((capability & mask) == val);
909 return ret;
910}
911
Ben Greear0e3a39b2013-06-19 14:06:27 -0700912/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100913struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
914 struct ieee80211_channel *channel,
915 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100916 const u8 *ssid, size_t ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200917 enum ieee80211_bss_type bss_type,
918 enum ieee80211_privacy privacy)
Johannes Berg2a519312009-02-10 21:25:55 +0100919{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800920 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +0100921 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200922 unsigned long now = jiffies;
Dedy Lansky6eb18132015-02-08 15:52:03 +0200923 int bss_privacy;
Johannes Berg2a519312009-02-10 21:25:55 +0100924
Dedy Lansky6eb18132015-02-08 15:52:03 +0200925 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
926 privacy);
Beni Lev4ee3e062012-08-27 12:49:39 +0300927
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800928 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100929
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800930 list_for_each_entry(bss, &rdev->bss_list, list) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200931 if (!cfg80211_bss_type_match(bss->pub.capability,
932 bss->pub.channel->band, bss_type))
933 continue;
934
935 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
936 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
937 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
Johannes Berg79420f02009-02-10 21:25:59 +0100938 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100939 if (channel && bss->pub.channel != channel)
940 continue;
Johannes Bergc14a7402014-04-09 22:36:50 +0200941 if (!is_valid_ether_addr(bss->pub.bssid))
942 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200943 /* Don't get expired BSS structs */
944 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
945 !atomic_read(&bss->hold))
946 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100947 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
948 res = bss;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800949 bss_ref_get(rdev, res);
Johannes Berg2a519312009-02-10 21:25:55 +0100950 break;
951 }
952 }
953
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800954 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100955 if (!res)
956 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300957 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100958 return &res->pub;
959}
960EXPORT_SYMBOL(cfg80211_get_bss);
961
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800962static void rb_insert_bss(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +0100963 struct cfg80211_internal_bss *bss)
964{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800965 struct rb_node **p = &rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +0100966 struct rb_node *parent = NULL;
967 struct cfg80211_internal_bss *tbss;
968 int cmp;
969
970 while (*p) {
971 parent = *p;
972 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
973
Johannes Berg4593c4c2013-02-01 19:20:03 +0100974 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100975
976 if (WARN_ON(!cmp)) {
977 /* will sort of leak this BSS */
978 return;
979 }
980
981 if (cmp < 0)
982 p = &(*p)->rb_left;
983 else
984 p = &(*p)->rb_right;
985 }
986
987 rb_link_node(&bss->rbn, parent, p);
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800988 rb_insert_color(&bss->rbn, &rdev->bss_tree);
Johannes Berg2a519312009-02-10 21:25:55 +0100989}
990
991static struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800992rb_find_bss(struct cfg80211_registered_device *rdev,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100993 struct cfg80211_internal_bss *res,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100994 enum bss_compare_mode mode)
Johannes Berg2a519312009-02-10 21:25:55 +0100995{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800996 struct rb_node *n = rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +0100997 struct cfg80211_internal_bss *bss;
998 int r;
999
1000 while (n) {
1001 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg4593c4c2013-02-01 19:20:03 +01001002 r = cmp_bss(&res->pub, &bss->pub, mode);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001003
1004 if (r == 0)
1005 return bss;
1006 else if (r < 0)
1007 n = n->rb_left;
1008 else
1009 n = n->rb_right;
1010 }
1011
1012 return NULL;
1013}
1014
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001015static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
Johannes Berg776b3582013-02-01 02:06:18 +01001016 struct cfg80211_internal_bss *new)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001017{
Johannes Berg9caf0362012-11-29 01:25:20 +01001018 const struct cfg80211_bss_ies *ies;
Johannes Berg776b3582013-02-01 02:06:18 +01001019 struct cfg80211_internal_bss *bss;
1020 const u8 *ie;
1021 int i, ssidlen;
1022 u8 fold = 0;
Johannes Berg9853a552016-11-15 12:05:11 +01001023 u32 n_entries = 0;
Johannes Berg9caf0362012-11-29 01:25:20 +01001024
Johannes Berg776b3582013-02-01 02:06:18 +01001025 ies = rcu_access_pointer(new->pub.beacon_ies);
Johannes Berg9caf0362012-11-29 01:25:20 +01001026 if (WARN_ON(!ies))
Johannes Berg776b3582013-02-01 02:06:18 +01001027 return false;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001028
Johannes Berg776b3582013-02-01 02:06:18 +01001029 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1030 if (!ie) {
1031 /* nothing to do */
1032 return true;
1033 }
1034
1035 ssidlen = ie[1];
1036 for (i = 0; i < ssidlen; i++)
1037 fold |= ie[2 + i];
1038
1039 if (fold) {
1040 /* not a hidden SSID */
1041 return true;
1042 }
1043
1044 /* This is the bad part ... */
1045
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001046 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg9853a552016-11-15 12:05:11 +01001047 /*
1048 * we're iterating all the entries anyway, so take the
1049 * opportunity to validate the list length accounting
1050 */
1051 n_entries++;
1052
Johannes Berg776b3582013-02-01 02:06:18 +01001053 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1054 continue;
1055 if (bss->pub.channel != new->pub.channel)
1056 continue;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02001057 if (bss->pub.scan_width != new->pub.scan_width)
1058 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001059 if (rcu_access_pointer(bss->pub.beacon_ies))
1060 continue;
1061 ies = rcu_access_pointer(bss->pub.ies);
1062 if (!ies)
1063 continue;
1064 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1065 if (!ie)
1066 continue;
1067 if (ssidlen && ie[1] != ssidlen)
1068 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001069 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1070 continue;
1071 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1072 list_del(&bss->hidden_list);
1073 /* combine them */
1074 list_add(&bss->hidden_list, &new->hidden_list);
1075 bss->pub.hidden_beacon_bss = &new->pub;
1076 new->refcount += bss->refcount;
1077 rcu_assign_pointer(bss->pub.beacon_ies,
1078 new->pub.beacon_ies);
1079 }
1080
Johannes Berg9853a552016-11-15 12:05:11 +01001081 WARN_ONCE(n_entries != rdev->bss_entries,
1082 "rdev bss entries[%d]/list[len:%d] corruption\n",
1083 rdev->bss_entries, n_entries);
1084
Johannes Berg776b3582013-02-01 02:06:18 +01001085 return true;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001086}
1087
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001088struct cfg80211_non_tx_bss {
1089 struct cfg80211_bss *tx_bss;
1090 u8 max_bssid_indicator;
1091 u8 bssid_index;
1092};
1093
Ben Greear0e3a39b2013-06-19 14:06:27 -07001094/* Returned bss is reference counted and must be cleaned up appropriately. */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001095static struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001096cfg80211_bss_update(struct cfg80211_registered_device *rdev,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001097 struct cfg80211_internal_bss *tmp,
1098 bool signal_valid)
Johannes Berg2a519312009-02-10 21:25:55 +01001099{
1100 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001101
Johannes Berg9caf0362012-11-29 01:25:20 +01001102 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +01001103 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001104
Johannes Berg9caf0362012-11-29 01:25:20 +01001105 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001106
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001107 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001108
Johannes Berg9caf0362012-11-29 01:25:20 +01001109 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001110 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg9caf0362012-11-29 01:25:20 +01001111 return NULL;
1112 }
1113
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001114 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +01001115
Johannes Bergcd1658f2009-04-16 15:00:58 +02001116 if (found) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001117 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +01001118 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1119 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +02001120
Johannes Berg9caf0362012-11-29 01:25:20 +01001121 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +02001122
Johannes Berg9caf0362012-11-29 01:25:20 +01001123 rcu_assign_pointer(found->pub.proberesp_ies,
1124 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001125 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +01001126 rcu_assign_pointer(found->pub.ies,
1127 tmp->pub.proberesp_ies);
1128 if (old)
1129 kfree_rcu((struct cfg80211_bss_ies *)old,
1130 rcu_head);
1131 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
Johannes Berg9537f222013-02-01 01:19:48 +01001132 const struct cfg80211_bss_ies *old;
Johannes Berg776b3582013-02-01 02:06:18 +01001133 struct cfg80211_internal_bss *bss;
1134
1135 if (found->pub.hidden_beacon_bss &&
1136 !list_empty(&found->hidden_list)) {
Johannes Berg1345ee62013-03-06 10:31:05 +01001137 const struct cfg80211_bss_ies *f;
1138
Johannes Berg776b3582013-02-01 02:06:18 +01001139 /*
1140 * The found BSS struct is one of the probe
1141 * response members of a group, but we're
1142 * receiving a beacon (beacon_ies in the tmp
1143 * bss is used). This can only mean that the
1144 * AP changed its beacon from not having an
1145 * SSID to showing it, which is confusing so
1146 * drop this information.
1147 */
Johannes Berg1345ee62013-03-06 10:31:05 +01001148
1149 f = rcu_access_pointer(tmp->pub.beacon_ies);
1150 kfree_rcu((struct cfg80211_bss_ies *)f,
1151 rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +01001152 goto drop;
1153 }
Johannes Berg915de2f2012-11-28 22:39:37 +01001154
Johannes Berg9caf0362012-11-29 01:25:20 +01001155 old = rcu_access_pointer(found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001156
Johannes Berg9caf0362012-11-29 01:25:20 +01001157 rcu_assign_pointer(found->pub.beacon_ies,
1158 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +01001159
1160 /* Override IEs if they were from a beacon before */
Johannes Berg9537f222013-02-01 01:19:48 +01001161 if (old == rcu_access_pointer(found->pub.ies))
Johannes Berg9caf0362012-11-29 01:25:20 +01001162 rcu_assign_pointer(found->pub.ies,
1163 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +02001164
Johannes Berg776b3582013-02-01 02:06:18 +01001165 /* Assign beacon IEs to all sub entries */
1166 list_for_each_entry(bss, &found->hidden_list,
1167 hidden_list) {
1168 const struct cfg80211_bss_ies *ies;
1169
1170 ies = rcu_access_pointer(bss->pub.beacon_ies);
1171 WARN_ON(ies != old);
1172
1173 rcu_assign_pointer(bss->pub.beacon_ies,
1174 tmp->pub.beacon_ies);
1175 }
1176
Johannes Berg9caf0362012-11-29 01:25:20 +01001177 if (old)
1178 kfree_rcu((struct cfg80211_bss_ies *)old,
1179 rcu_head);
1180 }
Johannes Berg1345ee62013-03-06 10:31:05 +01001181
1182 found->pub.beacon_interval = tmp->pub.beacon_interval;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001183 /*
1184 * don't update the signal if beacon was heard on
1185 * adjacent channel.
1186 */
1187 if (signal_valid)
1188 found->pub.signal = tmp->pub.signal;
Johannes Berg1345ee62013-03-06 10:31:05 +01001189 found->pub.capability = tmp->pub.capability;
1190 found->ts = tmp->ts;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001191 found->ts_boottime = tmp->ts_boottime;
Avraham Stern1d762502016-07-05 17:10:13 +03001192 found->parent_tsf = tmp->parent_tsf;
Sunil Dutt983dafa2017-12-13 19:51:36 +02001193 found->pub.chains = tmp->pub.chains;
1194 memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
1195 IEEE80211_MAX_CHAINS);
Avraham Stern1d762502016-07-05 17:10:13 +03001196 ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001197 found->pub.max_bssid_indicator = tmp->pub.max_bssid_indicator;
1198 found->pub.bssid_index = tmp->pub.bssid_index;
Johannes Berg2a519312009-02-10 21:25:55 +01001199 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +01001200 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001201 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +01001202 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001203
Johannes Berg9caf0362012-11-29 01:25:20 +01001204 /*
1205 * create a copy -- the "res" variable that is passed in
1206 * is allocated on the stack since it's not needed in the
1207 * more common case of an update
1208 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001209 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
Johannes Berg9caf0362012-11-29 01:25:20 +01001210 GFP_ATOMIC);
1211 if (!new) {
1212 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1213 if (ies)
1214 kfree_rcu(ies, rcu_head);
1215 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1216 if (ies)
1217 kfree_rcu(ies, rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +01001218 goto drop;
Johannes Berg9caf0362012-11-29 01:25:20 +01001219 }
1220 memcpy(new, tmp, sizeof(*new));
Johannes Berg776b3582013-02-01 02:06:18 +01001221 new->refcount = 1;
1222 INIT_LIST_HEAD(&new->hidden_list);
Sara Sharon7011ba52019-01-21 12:25:59 +02001223 INIT_LIST_HEAD(&new->pub.nontrans_list);
Johannes Berg776b3582013-02-01 02:06:18 +01001224
1225 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001226 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
Johannes Berg776b3582013-02-01 02:06:18 +01001227 if (!hidden)
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001228 hidden = rb_find_bss(rdev, tmp,
Johannes Berg776b3582013-02-01 02:06:18 +01001229 BSS_CMP_HIDE_NUL);
1230 if (hidden) {
1231 new->pub.hidden_beacon_bss = &hidden->pub;
1232 list_add(&new->hidden_list,
1233 &hidden->hidden_list);
1234 hidden->refcount++;
1235 rcu_assign_pointer(new->pub.beacon_ies,
1236 hidden->pub.beacon_ies);
1237 }
1238 } else {
1239 /*
1240 * Ok so we found a beacon, and don't have an entry. If
1241 * it's a beacon with hidden SSID, we might be in for an
1242 * expensive search for any probe responses that should
1243 * be grouped with this beacon for updates ...
1244 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001245 if (!cfg80211_combine_bsses(rdev, new)) {
Johannes Berg776b3582013-02-01 02:06:18 +01001246 kfree(new);
1247 goto drop;
1248 }
1249 }
1250
Johannes Berg9853a552016-11-15 12:05:11 +01001251 if (rdev->bss_entries >= bss_entries_limit &&
1252 !cfg80211_bss_expire_oldest(rdev)) {
1253 kfree(new);
1254 goto drop;
1255 }
1256
Sara Sharona3584f56d2019-01-21 12:22:21 +02001257 /* This must be before the call to bss_ref_get */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001258 if (tmp->pub.transmitted_bss) {
Sara Sharona3584f56d2019-01-21 12:22:21 +02001259 struct cfg80211_internal_bss *pbss =
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001260 container_of(tmp->pub.transmitted_bss,
Sara Sharona3584f56d2019-01-21 12:22:21 +02001261 struct cfg80211_internal_bss,
1262 pub);
1263
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001264 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
Sara Sharona3584f56d2019-01-21 12:22:21 +02001265 bss_ref_get(rdev, pbss);
1266 }
1267
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001268 list_add_tail(&new->list, &rdev->bss_list);
Johannes Berg9853a552016-11-15 12:05:11 +01001269 rdev->bss_entries++;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001270 rb_insert_bss(rdev, new);
Johannes Berg9caf0362012-11-29 01:25:20 +01001271 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +01001272 }
1273
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001274 rdev->bss_generation++;
1275 bss_ref_get(rdev, found);
1276 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001277
Johannes Berg2a519312009-02-10 21:25:55 +01001278 return found;
Johannes Berg776b3582013-02-01 02:06:18 +01001279 drop:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001280 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg776b3582013-02-01 02:06:18 +01001281 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001282}
1283
Jouni Malinen119f94a2018-09-05 18:52:22 +03001284/*
1285 * Update RX channel information based on the available frame payload
1286 * information. This is mainly for the 2.4 GHz band where frames can be received
1287 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1288 * element to indicate the current (transmitting) channel, but this might also
1289 * be needed on other bands if RX frequency does not match with the actual
1290 * operating channel of a BSS.
1291 */
Johannes Berg0172bb72012-11-23 14:23:30 +01001292static struct ieee80211_channel *
1293cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001294 struct ieee80211_channel *channel,
1295 enum nl80211_bss_scan_width scan_width)
Johannes Berg0172bb72012-11-23 14:23:30 +01001296{
1297 const u8 *tmp;
1298 u32 freq;
1299 int channel_number = -1;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001300 struct ieee80211_channel *alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001301
1302 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1303 if (tmp && tmp[1] == 1) {
1304 channel_number = tmp[2];
1305 } else {
1306 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1307 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1308 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1309
1310 channel_number = htop->primary_chan;
1311 }
1312 }
1313
Jouni Malinen119f94a2018-09-05 18:52:22 +03001314 if (channel_number < 0) {
1315 /* No channel information in frame payload */
Johannes Berg0172bb72012-11-23 14:23:30 +01001316 return channel;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001317 }
Johannes Berg0172bb72012-11-23 14:23:30 +01001318
1319 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
Jouni Malinen119f94a2018-09-05 18:52:22 +03001320 alt_channel = ieee80211_get_channel(wiphy, freq);
1321 if (!alt_channel) {
1322 if (channel->band == NL80211_BAND_2GHZ) {
1323 /*
1324 * Better not allow unexpected channels when that could
1325 * be going beyond the 1-11 range (e.g., discovering
1326 * BSS on channel 12 when radio is configured for
1327 * channel 11.
1328 */
1329 return NULL;
1330 }
1331
1332 /* No match for the payload channel number - ignore it */
1333 return channel;
1334 }
1335
1336 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1337 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1338 /*
1339 * Ignore channel number in 5 and 10 MHz channels where there
1340 * may not be an n:1 or 1:n mapping between frequencies and
1341 * channel numbers.
1342 */
1343 return channel;
1344 }
1345
1346 /*
1347 * Use the channel determined through the payload channel number
1348 * instead of the RX channel reported by the driver.
1349 */
1350 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg0172bb72012-11-23 14:23:30 +01001351 return NULL;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001352 return alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001353}
1354
Ben Greear0e3a39b2013-06-19 14:06:27 -07001355/* Returned bss is reference counted and must be cleaned up appropriately. */
Peng Xu0b8fb822019-01-21 12:14:57 +02001356static struct cfg80211_bss *
1357cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1358 struct cfg80211_inform_bss *data,
1359 enum cfg80211_bss_frame_type ftype,
1360 const u8 *bssid, u64 tsf, u16 capability,
1361 u16 beacon_interval, const u8 *ie, size_t ielen,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001362 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001363 gfp_t gfp)
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001364{
Peng Xu0b8fb822019-01-21 12:14:57 +02001365 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg9caf0362012-11-29 01:25:20 +01001366 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001367 struct ieee80211_channel *channel;
Sara Sharon7011ba52019-01-21 12:25:59 +02001368 struct cfg80211_internal_bss tmp = {}, *res;
Dedy Lansky6eb18132015-02-08 15:52:03 +02001369 int bss_type;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001370 bool signal_valid;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001371
1372 if (WARN_ON(!wiphy))
1373 return NULL;
1374
Sujith22fe88d2010-05-13 10:34:08 +05301375 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001376 (data->signal < 0 || data->signal > 100)))
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001377 return NULL;
1378
Jouni Malinen119f94a2018-09-05 18:52:22 +03001379 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1380 data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001381 if (!channel)
1382 return NULL;
1383
Johannes Berg9caf0362012-11-29 01:25:20 +01001384 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1385 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001386 tmp.pub.scan_width = data->scan_width;
1387 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001388 tmp.pub.beacon_interval = beacon_interval;
1389 tmp.pub.capability = capability;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001390 tmp.ts_boottime = data->boottime_ns;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001391 if (non_tx_data) {
1392 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1393 tmp.pub.bssid_index = non_tx_data->bssid_index;
1394 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1395 }
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001396
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001397 /*
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001398 * If we do not know here whether the IEs are from a Beacon or Probe
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001399 * Response frame, we need to pick one of the options and only use it
1400 * with the driver that does not provide the full Beacon/Probe Response
1401 * frame. Use Beacon frame pointer to avoid indicating that this should
Johannes Berg50521aa2013-01-30 21:33:19 +01001402 * override the IEs pointer should we have received an earlier
Johannes Berg9caf0362012-11-29 01:25:20 +01001403 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001404 */
Johannes Berg0e227082014-08-12 20:34:30 +02001405 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001406 if (!ies)
1407 return NULL;
1408 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001409 ies->tsf = tsf;
Johannes Berg0e227082014-08-12 20:34:30 +02001410 ies->from_beacon = false;
Johannes Berg9caf0362012-11-29 01:25:20 +01001411 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001412
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001413 switch (ftype) {
1414 case CFG80211_BSS_FTYPE_BEACON:
1415 ies->from_beacon = true;
Luca Coelho925b5972018-12-15 11:03:21 +02001416 /* fall through */
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001417 case CFG80211_BSS_FTYPE_UNKNOWN:
1418 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1419 break;
1420 case CFG80211_BSS_FTYPE_PRESP:
1421 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1422 break;
1423 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001424 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001425
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001426 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001427 wiphy->max_adj_channel_rssi_comp;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001428 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001429 if (!res)
1430 return NULL;
1431
Johannes Berg57fbcce2016-04-12 15:56:15 +02001432 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001433 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1434 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1435 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1436 regulatory_hint_found_beacon(wiphy, channel, gfp);
1437 } else {
1438 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1439 regulatory_hint_found_beacon(wiphy, channel, gfp);
1440 }
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001441
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001442 if (non_tx_data && non_tx_data->tx_bss) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001443 /* this is a nontransmitting bss, we need to add it to
1444 * transmitting bss' list if it is not there
1445 */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001446 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1447 &res->pub)) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001448 if (__cfg80211_unlink_bss(rdev, res))
1449 rdev->bss_generation++;
1450 }
1451 }
1452
Beni Lev4ee3e062012-08-27 12:49:39 +03001453 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001454 /* cfg80211_bss_update gives us a referenced result */
1455 return &res->pub;
1456}
Peng Xu0b8fb822019-01-21 12:14:57 +02001457
Sara Sharonfe806e42019-03-15 17:39:05 +02001458static const struct element
1459*cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
1460 const struct element *mbssid_elem,
1461 const struct element *sub_elem)
1462{
1463 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
1464 const struct element *next_mbssid;
1465 const struct element *next_sub;
1466
1467 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
1468 mbssid_end,
1469 ielen - (mbssid_end - ie));
1470
1471 /*
1472 * If is is not the last subelement in current MBSSID IE or there isn't
1473 * a next MBSSID IE - profile is complete.
1474 */
1475 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
1476 !next_mbssid)
1477 return NULL;
1478
1479 /* For any length error, just return NULL */
1480
1481 if (next_mbssid->datalen < 4)
1482 return NULL;
1483
1484 next_sub = (void *)&next_mbssid->data[1];
1485
1486 if (next_mbssid->data + next_mbssid->datalen <
1487 next_sub->data + next_sub->datalen)
1488 return NULL;
1489
1490 if (next_sub->id != 0 || next_sub->datalen < 2)
1491 return NULL;
1492
1493 /*
1494 * Check if the first element in the next sub element is a start
1495 * of a new profile
1496 */
1497 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
1498 NULL : next_mbssid;
1499}
1500
1501size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
1502 const struct element *mbssid_elem,
1503 const struct element *sub_elem,
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001504 u8 *merged_ie, size_t max_copy_len)
Sara Sharonfe806e42019-03-15 17:39:05 +02001505{
1506 size_t copied_len = sub_elem->datalen;
1507 const struct element *next_mbssid;
1508
1509 if (sub_elem->datalen > max_copy_len)
1510 return 0;
1511
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001512 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
Sara Sharonfe806e42019-03-15 17:39:05 +02001513
1514 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
1515 mbssid_elem,
1516 sub_elem))) {
1517 const struct element *next_sub = (void *)&next_mbssid->data[1];
1518
1519 if (copied_len + next_sub->datalen > max_copy_len)
1520 break;
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001521 memcpy(merged_ie + copied_len, next_sub->data,
Sara Sharonfe806e42019-03-15 17:39:05 +02001522 next_sub->datalen);
1523 copied_len += next_sub->datalen;
1524 }
1525
1526 return copied_len;
1527}
1528EXPORT_SYMBOL(cfg80211_merge_profile);
1529
Peng Xu0b8fb822019-01-21 12:14:57 +02001530static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1531 struct cfg80211_inform_bss *data,
1532 enum cfg80211_bss_frame_type ftype,
1533 const u8 *bssid, u64 tsf,
1534 u16 beacon_interval, const u8 *ie,
1535 size_t ielen,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001536 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001537 gfp_t gfp)
1538{
Johannes Berg1c8745f2019-02-07 22:36:33 +01001539 const u8 *mbssid_index_ie;
1540 const struct element *elem, *sub;
1541 size_t new_ie_len;
Peng Xu0b8fb822019-01-21 12:14:57 +02001542 u8 new_bssid[ETH_ALEN];
Sara Sharonfe806e42019-03-15 17:39:05 +02001543 u8 *new_ie, *profile;
1544 u64 seen_indices = 0;
Peng Xu0b8fb822019-01-21 12:14:57 +02001545 u16 capability;
1546 struct cfg80211_bss *bss;
1547
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001548 if (!non_tx_data)
Peng Xu0b8fb822019-01-21 12:14:57 +02001549 return;
1550 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1551 return;
Sara Sharon213ed572019-01-16 23:02:03 +02001552 if (!wiphy->support_mbssid)
1553 return;
1554 if (wiphy->support_only_he_mbssid &&
1555 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1556 return;
Peng Xu0b8fb822019-01-21 12:14:57 +02001557
Peng Xu0b8fb822019-01-21 12:14:57 +02001558 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1559 if (!new_ie)
1560 return;
1561
Sara Sharonfe806e42019-03-15 17:39:05 +02001562 profile = kmalloc(ielen, gfp);
1563 if (!profile)
1564 goto out;
1565
Johannes Berg1c8745f2019-02-07 22:36:33 +01001566 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1567 if (elem->datalen < 4)
1568 continue;
1569 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
Sara Sharonfe806e42019-03-15 17:39:05 +02001570 u8 profile_len;
1571
Johannes Berg1c8745f2019-02-07 22:36:33 +01001572 if (sub->id != 0 || sub->datalen < 4) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001573 /* not a valid BSS profile */
1574 continue;
1575 }
1576
Johannes Berg1c8745f2019-02-07 22:36:33 +01001577 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1578 sub->data[1] != 2) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001579 /* The first element within the Nontransmitted
1580 * BSSID Profile is not the Nontransmitted
1581 * BSSID Capability element.
1582 */
1583 continue;
1584 }
1585
Sara Sharonfe806e42019-03-15 17:39:05 +02001586 memset(profile, 0, ielen);
1587 profile_len = cfg80211_merge_profile(ie, ielen,
1588 elem,
1589 sub,
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001590 profile,
Sara Sharonfe806e42019-03-15 17:39:05 +02001591 ielen);
1592
Peng Xu0b8fb822019-01-21 12:14:57 +02001593 /* found a Nontransmitted BSSID Profile */
1594 mbssid_index_ie = cfg80211_find_ie
1595 (WLAN_EID_MULTI_BSSID_IDX,
Sara Sharonfe806e42019-03-15 17:39:05 +02001596 profile, profile_len);
Peng Xu0b8fb822019-01-21 12:14:57 +02001597 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
Sara Sharonfe806e42019-03-15 17:39:05 +02001598 mbssid_index_ie[2] == 0 ||
1599 mbssid_index_ie[2] > 46) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001600 /* No valid Multiple BSSID-Index element */
1601 continue;
1602 }
1603
Sara Sharonfe806e42019-03-15 17:39:05 +02001604 if (seen_indices & BIT(mbssid_index_ie[2]))
1605 /* We don't support legacy split of a profile */
1606 net_dbg_ratelimited("Partial info for BSSID index %d\n",
1607 mbssid_index_ie[2]);
1608
1609 seen_indices |= BIT(mbssid_index_ie[2]);
1610
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001611 non_tx_data->bssid_index = mbssid_index_ie[2];
1612 non_tx_data->max_bssid_indicator = elem->data[0];
1613
1614 cfg80211_gen_new_bssid(bssid,
1615 non_tx_data->max_bssid_indicator,
1616 non_tx_data->bssid_index,
Peng Xu0b8fb822019-01-21 12:14:57 +02001617 new_bssid);
1618 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
Sara Sharonfe806e42019-03-15 17:39:05 +02001619 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
1620 profile,
1621 profile_len, new_ie,
Peng Xu0b8fb822019-01-21 12:14:57 +02001622 gfp);
1623 if (!new_ie_len)
1624 continue;
1625
Sara Sharonfe806e42019-03-15 17:39:05 +02001626 capability = get_unaligned_le16(profile + 2);
Peng Xu0b8fb822019-01-21 12:14:57 +02001627 bss = cfg80211_inform_single_bss_data(wiphy, data,
1628 ftype,
1629 new_bssid, tsf,
1630 capability,
1631 beacon_interval,
1632 new_ie,
1633 new_ie_len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001634 non_tx_data,
1635 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001636 if (!bss)
1637 break;
1638 cfg80211_put_bss(wiphy, bss);
1639 }
Peng Xu0b8fb822019-01-21 12:14:57 +02001640 }
1641
Sara Sharonfe806e42019-03-15 17:39:05 +02001642out:
Peng Xu0b8fb822019-01-21 12:14:57 +02001643 kfree(new_ie);
Sara Sharonfe806e42019-03-15 17:39:05 +02001644 kfree(profile);
Peng Xu0b8fb822019-01-21 12:14:57 +02001645}
1646
1647struct cfg80211_bss *
1648cfg80211_inform_bss_data(struct wiphy *wiphy,
1649 struct cfg80211_inform_bss *data,
1650 enum cfg80211_bss_frame_type ftype,
1651 const u8 *bssid, u64 tsf, u16 capability,
1652 u16 beacon_interval, const u8 *ie, size_t ielen,
1653 gfp_t gfp)
1654{
1655 struct cfg80211_bss *res;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001656 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001657
1658 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1659 capability, beacon_interval, ie,
1660 ielen, NULL, gfp);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001661 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001662 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001663 beacon_interval, ie, ielen, &non_tx_data,
1664 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001665 return res;
1666}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001667EXPORT_SYMBOL(cfg80211_inform_bss_data);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001668
Peng Xu0b8fb822019-01-21 12:14:57 +02001669static void
1670cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1671 struct cfg80211_inform_bss *data,
1672 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001673 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001674 gfp_t gfp)
1675{
1676 enum cfg80211_bss_frame_type ftype;
1677 const u8 *ie = mgmt->u.probe_resp.variable;
1678 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1679 u.probe_resp.variable);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001680
Peng Xu0b8fb822019-01-21 12:14:57 +02001681 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1682 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1683
1684 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1685 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1686 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001687 ie, ielen, non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001688}
1689
1690static void
1691cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
Sara Sharon7011ba52019-01-21 12:25:59 +02001692 struct cfg80211_bss *nontrans_bss,
Peng Xu0b8fb822019-01-21 12:14:57 +02001693 struct ieee80211_mgmt *mgmt, size_t len,
1694 gfp_t gfp)
1695{
1696 u8 *ie, *new_ie, *pos;
1697 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1698 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1699 u.probe_resp.variable);
1700 size_t new_ie_len;
1701 struct cfg80211_bss_ies *new_ies;
1702 const struct cfg80211_bss_ies *old;
1703 u8 cpy_len;
1704
1705 ie = mgmt->u.probe_resp.variable;
1706
1707 new_ie_len = ielen;
1708 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1709 if (!trans_ssid)
1710 return;
1711 new_ie_len -= trans_ssid[1];
1712 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1713 if (!mbssid)
1714 return;
1715 new_ie_len -= mbssid[1];
1716 rcu_read_lock();
Sara Sharon7011ba52019-01-21 12:25:59 +02001717 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
Peng Xu0b8fb822019-01-21 12:14:57 +02001718 if (!nontrans_ssid) {
1719 rcu_read_unlock();
1720 return;
1721 }
1722 new_ie_len += nontrans_ssid[1];
1723 rcu_read_unlock();
1724
1725 /* generate new ie for nontrans BSS
1726 * 1. replace SSID with nontrans BSS' SSID
1727 * 2. skip MBSSID IE
1728 */
1729 new_ie = kzalloc(new_ie_len, gfp);
1730 if (!new_ie)
1731 return;
1732 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp);
Sara Sharonbede8d22019-01-30 08:48:21 +02001733 if (!new_ies)
1734 goto out_free;
Peng Xu0b8fb822019-01-21 12:14:57 +02001735
1736 pos = new_ie;
1737
1738 /* copy the nontransmitted SSID */
1739 cpy_len = nontrans_ssid[1] + 2;
1740 memcpy(pos, nontrans_ssid, cpy_len);
1741 pos += cpy_len;
1742 /* copy the IEs between SSID and MBSSID */
1743 cpy_len = trans_ssid[1] + 2;
1744 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1745 pos += (mbssid - (trans_ssid + cpy_len));
1746 /* copy the IEs after MBSSID */
1747 cpy_len = mbssid[1] + 2;
1748 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1749
1750 /* update ie */
1751 new_ies->len = new_ie_len;
1752 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1753 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1754 memcpy(new_ies->data, new_ie, new_ie_len);
1755 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001756 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1757 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1758 rcu_assign_pointer(nontrans_bss->ies, new_ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001759 if (old)
1760 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1761 } else {
Sara Sharon7011ba52019-01-21 12:25:59 +02001762 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1763 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1764 rcu_assign_pointer(nontrans_bss->ies, new_ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001765 if (old)
1766 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1767 }
Sara Sharonbede8d22019-01-30 08:48:21 +02001768
1769out_free:
1770 kfree(new_ie);
Peng Xu0b8fb822019-01-21 12:14:57 +02001771}
1772
1773/* cfg80211_inform_bss_width_frame helper */
1774static struct cfg80211_bss *
1775cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1776 struct cfg80211_inform_bss *data,
1777 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001778 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001779 gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +01001780{
Johannes Berg9caf0362012-11-29 01:25:20 +01001781 struct cfg80211_internal_bss tmp = {}, *res;
1782 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001783 struct ieee80211_channel *channel;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001784 bool signal_valid;
Johannes Berg2a519312009-02-10 21:25:55 +01001785 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1786 u.probe_resp.variable);
Dedy Lansky6eb18132015-02-08 15:52:03 +02001787 int bss_type;
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001788
Johannes Berg0172bb72012-11-23 14:23:30 +01001789 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1790 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1791
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001792 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
Beni Lev4ee3e062012-08-27 12:49:39 +03001793
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001794 if (WARN_ON(!mgmt))
1795 return NULL;
1796
1797 if (WARN_ON(!wiphy))
1798 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001799
Sujith22fe88d2010-05-13 10:34:08 +05301800 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001801 (data->signal < 0 || data->signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +01001802 return NULL;
1803
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001804 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +01001805 return NULL;
1806
Johannes Berg0172bb72012-11-23 14:23:30 +01001807 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001808 ielen, data->chan, data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001809 if (!channel)
1810 return NULL;
1811
Johannes Berg0e227082014-08-12 20:34:30 +02001812 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001813 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001814 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +01001815 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001816 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
Johannes Berg0e227082014-08-12 20:34:30 +02001817 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
Johannes Berg9caf0362012-11-29 01:25:20 +01001818 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +01001819
Johannes Berg9caf0362012-11-29 01:25:20 +01001820 if (ieee80211_is_probe_resp(mgmt->frame_control))
1821 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1822 else
1823 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1824 rcu_assign_pointer(tmp.pub.ies, ies);
Arend Van Spriel505a2e82016-12-16 11:21:54 +00001825
Johannes Berg9caf0362012-11-29 01:25:20 +01001826 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1827 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001828 tmp.pub.scan_width = data->scan_width;
1829 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001830 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1831 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001832 tmp.ts_boottime = data->boottime_ns;
Avraham Stern1d762502016-07-05 17:10:13 +03001833 tmp.parent_tsf = data->parent_tsf;
Sunil Dutt983dafa2017-12-13 19:51:36 +02001834 tmp.pub.chains = data->chains;
1835 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
Avraham Stern1d762502016-07-05 17:10:13 +03001836 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001837 if (non_tx_data) {
1838 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1839 tmp.pub.bssid_index = non_tx_data->bssid_index;
1840 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1841 }
Johannes Berg2a519312009-02-10 21:25:55 +01001842
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001843 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001844 wiphy->max_adj_channel_rssi_comp;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001845 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
Johannes Berg2a519312009-02-10 21:25:55 +01001846 if (!res)
1847 return NULL;
1848
Johannes Berg57fbcce2016-04-12 15:56:15 +02001849 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001850 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1851 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1852 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1853 regulatory_hint_found_beacon(wiphy, channel, gfp);
1854 } else {
1855 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1856 regulatory_hint_found_beacon(wiphy, channel, gfp);
1857 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001858
Beni Lev4ee3e062012-08-27 12:49:39 +03001859 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +01001860 /* cfg80211_bss_update gives us a referenced result */
1861 return &res->pub;
1862}
Peng Xu0b8fb822019-01-21 12:14:57 +02001863
1864struct cfg80211_bss *
1865cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1866 struct cfg80211_inform_bss *data,
1867 struct ieee80211_mgmt *mgmt, size_t len,
1868 gfp_t gfp)
1869{
Sara Sharon7011ba52019-01-21 12:25:59 +02001870 struct cfg80211_bss *res, *tmp_bss;
Peng Xu0b8fb822019-01-21 12:14:57 +02001871 const u8 *ie = mgmt->u.probe_resp.variable;
1872 const struct cfg80211_bss_ies *ies1, *ies2;
1873 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1874 u.probe_resp.variable);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001875 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001876
1877 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1878 len, NULL, gfp);
Sara Sharon213ed572019-01-16 23:02:03 +02001879 if (!res || !wiphy->support_mbssid ||
1880 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1881 return res;
1882 if (wiphy->support_only_he_mbssid &&
1883 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
Peng Xu0b8fb822019-01-21 12:14:57 +02001884 return res;
1885
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001886 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001887 /* process each non-transmitting bss */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001888 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1889 &non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001890
1891 /* check if the res has other nontransmitting bss which is not
1892 * in MBSSID IE
1893 */
1894 ies1 = rcu_access_pointer(res->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001895
1896 /* go through nontrans_list, if the timestamp of the BSS is
1897 * earlier than the timestamp of the transmitting BSS then
1898 * update it
1899 */
Sara Sharon7011ba52019-01-21 12:25:59 +02001900 list_for_each_entry(tmp_bss, &res->nontrans_list,
Peng Xu0b8fb822019-01-21 12:14:57 +02001901 nontrans_list) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001902 ies2 = rcu_access_pointer(tmp_bss->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001903 if (ies2->tsf < ies1->tsf)
1904 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1905 mgmt, len, gfp);
1906 }
1907
1908 return res;
1909}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001910EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
Johannes Berg2a519312009-02-10 21:25:55 +01001911
Johannes Berg5b112d32013-02-01 01:49:58 +01001912void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001913{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001914 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001915 struct cfg80211_internal_bss *bss;
1916
1917 if (!pub)
1918 return;
1919
1920 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001921
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001922 spin_lock_bh(&rdev->bss_lock);
1923 bss_ref_get(rdev, bss);
1924 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001925}
1926EXPORT_SYMBOL(cfg80211_ref_bss);
1927
Johannes Berg5b112d32013-02-01 01:49:58 +01001928void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg2a519312009-02-10 21:25:55 +01001929{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001930 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001931 struct cfg80211_internal_bss *bss;
1932
1933 if (!pub)
1934 return;
1935
1936 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001937
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001938 spin_lock_bh(&rdev->bss_lock);
1939 bss_ref_put(rdev, bss);
1940 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001941}
1942EXPORT_SYMBOL(cfg80211_put_bss);
1943
Johannes Bergd491af12009-02-10 21:25:58 +01001944void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1945{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001946 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Sara Sharon7011ba52019-01-21 12:25:59 +02001947 struct cfg80211_internal_bss *bss, *tmp1;
1948 struct cfg80211_bss *nontrans_bss, *tmp;
Johannes Bergd491af12009-02-10 21:25:58 +01001949
1950 if (WARN_ON(!pub))
1951 return;
1952
1953 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1954
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001955 spin_lock_bh(&rdev->bss_lock);
Sara Sharon7011ba52019-01-21 12:25:59 +02001956 if (list_empty(&bss->list))
1957 goto out;
Peng Xu0b8fb822019-01-21 12:14:57 +02001958
Sara Sharon7011ba52019-01-21 12:25:59 +02001959 list_for_each_entry_safe(nontrans_bss, tmp,
1960 &pub->nontrans_list,
1961 nontrans_list) {
1962 tmp1 = container_of(nontrans_bss,
1963 struct cfg80211_internal_bss, pub);
1964 if (__cfg80211_unlink_bss(rdev, tmp1))
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001965 rdev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +02001966 }
Sara Sharon7011ba52019-01-21 12:25:59 +02001967
1968 if (__cfg80211_unlink_bss(rdev, bss))
1969 rdev->bss_generation++;
1970out:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001971 spin_unlock_bh(&rdev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +01001972}
1973EXPORT_SYMBOL(cfg80211_unlink_bss);
1974
Johannes Berg3d23e342009-09-29 23:27:28 +02001975#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9f419f32013-05-08 21:34:22 +02001976static struct cfg80211_registered_device *
1977cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1978{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001979 struct cfg80211_registered_device *rdev;
Johannes Berg9f419f32013-05-08 21:34:22 +02001980 struct net_device *dev;
1981
Johannes Berg5fe231e2013-05-08 21:45:15 +02001982 ASSERT_RTNL();
1983
Johannes Berg9f419f32013-05-08 21:34:22 +02001984 dev = dev_get_by_index(net, ifindex);
1985 if (!dev)
Johannes Berg5fe231e2013-05-08 21:45:15 +02001986 return ERR_PTR(-ENODEV);
1987 if (dev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001988 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001989 else
Johannes Berg9f419f32013-05-08 21:34:22 +02001990 rdev = ERR_PTR(-ENODEV);
1991 dev_put(dev);
Johannes Berg9f419f32013-05-08 21:34:22 +02001992 return rdev;
1993}
1994
Johannes Berg2a519312009-02-10 21:25:55 +01001995int cfg80211_wext_siwscan(struct net_device *dev,
1996 struct iw_request_info *info,
1997 union iwreq_data *wrqu, char *extra)
1998{
1999 struct cfg80211_registered_device *rdev;
2000 struct wiphy *wiphy;
2001 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c8b2009-12-23 15:33:35 +01002002 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01002003 int i, err, n_channels = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002004 enum nl80211_band band;
Johannes Berg2a519312009-02-10 21:25:55 +01002005
2006 if (!netif_running(dev))
2007 return -ENETDOWN;
2008
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002009 if (wrqu->data.length == sizeof(struct iw_scan_req))
2010 wreq = (struct iw_scan_req *)extra;
2011
Johannes Berg463d0182009-07-14 00:33:35 +02002012 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01002013
2014 if (IS_ERR(rdev))
2015 return PTR_ERR(rdev);
2016
Johannes Bergf9d15d12014-01-22 11:14:19 +02002017 if (rdev->scan_req || rdev->scan_msg) {
Johannes Berg2a519312009-02-10 21:25:55 +01002018 err = -EBUSY;
2019 goto out;
2020 }
2021
2022 wiphy = &rdev->wiphy;
2023
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002024 /* Determine number of channels, needed to allocate creq */
2025 if (wreq && wreq->num_channels)
2026 n_channels = wreq->num_channels;
Ilan Peerbdfbec22014-01-09 11:37:23 +02002027 else
2028 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01002029
2030 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2031 n_channels * sizeof(void *),
2032 GFP_ATOMIC);
2033 if (!creq) {
2034 err = -ENOMEM;
2035 goto out;
2036 }
2037
2038 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +02002039 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +02002040 /* SSIDs come after channels */
2041 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002042 creq->n_channels = n_channels;
2043 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -07002044 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01002045
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002046 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +01002047 i = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002048 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg2a519312009-02-10 21:25:55 +01002049 int j;
Johannes Berg584991d2009-11-02 13:32:03 +01002050
Johannes Berg2a519312009-02-10 21:25:55 +01002051 if (!wiphy->bands[band])
2052 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01002053
Johannes Berg2a519312009-02-10 21:25:55 +01002054 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01002055 /* ignore disabled channels */
2056 if (wiphy->bands[band]->channels[j].flags &
2057 IEEE80211_CHAN_DISABLED)
2058 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002059
2060 /* If we have a wireless request structure and the
2061 * wireless request specifies frequencies, then search
2062 * for the matching hardware channel.
2063 */
2064 if (wreq && wreq->num_channels) {
2065 int k;
2066 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2067 for (k = 0; k < wreq->num_channels; k++) {
Zhao, Gang96998e32014-04-09 09:28:06 +08002068 struct iw_freq *freq =
2069 &wreq->channel_list[k];
2070 int wext_freq =
2071 cfg80211_wext_freq(freq);
2072
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002073 if (wext_freq == wiphy_freq)
2074 goto wext_freq_found;
2075 }
2076 goto wext_freq_not_found;
2077 }
2078
2079 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01002080 creq->channels[i] = &wiphy->bands[band]->channels[j];
2081 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002082 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01002083 }
2084 }
Holger Schurig8862dc52009-09-11 10:13:55 +02002085 /* No channels found? */
2086 if (!i) {
2087 err = -EINVAL;
2088 goto out;
2089 }
Johannes Berg2a519312009-02-10 21:25:55 +01002090
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002091 /* Set real number of channels specified in creq->channels[] */
2092 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01002093
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002094 /* translate "Scan for SSID" request */
2095 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01002096 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c8b2009-12-23 15:33:35 +01002097 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2098 err = -EINVAL;
2099 goto out;
2100 }
Johannes Berg2a519312009-02-10 21:25:55 +01002101 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2102 creq->ssids[0].ssid_len = wreq->essid_len;
2103 }
2104 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2105 creq->n_ssids = 0;
2106 }
2107
Johannes Berg57fbcce2016-04-12 15:56:15 +02002108 for (i = 0; i < NUM_NL80211_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02002109 if (wiphy->bands[i])
2110 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02002111
Jouni Malinen818965d2016-02-26 22:12:47 +02002112 eth_broadcast_addr(creq->bssid);
2113
Johannes Berg2a519312009-02-10 21:25:55 +01002114 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03002115 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01002116 if (err) {
2117 rdev->scan_req = NULL;
Johannes Berg65486c8b2009-12-23 15:33:35 +01002118 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02002119 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02002120 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c8b2009-12-23 15:33:35 +01002121 /* creq now owned by driver */
2122 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02002123 dev_hold(dev);
2124 }
Johannes Berg2a519312009-02-10 21:25:55 +01002125 out:
Johannes Berg65486c8b2009-12-23 15:33:35 +01002126 kfree(creq);
Johannes Berg2a519312009-02-10 21:25:55 +01002127 return err;
2128}
Johannes Berg2afe38d2015-01-06 14:00:53 +01002129EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01002130
James Minor76a70e92015-02-24 12:58:20 -06002131static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2132 const struct cfg80211_bss_ies *ies,
2133 char *current_ev, char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01002134{
Johannes Berg9caf0362012-11-29 01:25:20 +01002135 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01002136 struct iw_event iwe;
2137
Johannes Berg9caf0362012-11-29 01:25:20 +01002138 if (!ies)
James Minor76a70e92015-02-24 12:58:20 -06002139 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002140
2141 /*
2142 * If needed, fragment the IEs buffer (at IE boundaries) into short
2143 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2144 */
Johannes Berg9caf0362012-11-29 01:25:20 +01002145 pos = ies->data;
2146 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01002147
2148 while (end - pos > IW_GENERIC_IE_MAX) {
2149 next = pos + 2 + pos[1];
2150 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2151 next = next + 2 + next[1];
2152
2153 memset(&iwe, 0, sizeof(iwe));
2154 iwe.cmd = IWEVGENIE;
2155 iwe.u.data.length = next - pos;
James Minor76a70e92015-02-24 12:58:20 -06002156 current_ev = iwe_stream_add_point_check(info, current_ev,
2157 end_buf, &iwe,
2158 (void *)pos);
2159 if (IS_ERR(current_ev))
2160 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002161 pos = next;
2162 }
2163
2164 if (end > pos) {
2165 memset(&iwe, 0, sizeof(iwe));
2166 iwe.cmd = IWEVGENIE;
2167 iwe.u.data.length = end - pos;
James Minor76a70e92015-02-24 12:58:20 -06002168 current_ev = iwe_stream_add_point_check(info, current_ev,
2169 end_buf, &iwe,
2170 (void *)pos);
2171 if (IS_ERR(current_ev))
2172 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002173 }
James Minor76a70e92015-02-24 12:58:20 -06002174
2175 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002176}
2177
Johannes Berg2a519312009-02-10 21:25:55 +01002178static char *
Johannes Berg77965c972009-02-18 18:45:06 +01002179ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2180 struct cfg80211_internal_bss *bss, char *current_ev,
2181 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01002182{
Johannes Berg9caf0362012-11-29 01:25:20 +01002183 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01002184 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01002185 const u8 *ie;
James Minor76a70e92015-02-24 12:58:20 -06002186 u8 buf[50];
2187 u8 *cfg, *p, *tmp;
Johannes Berg9caf0362012-11-29 01:25:20 +01002188 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002189 bool ismesh = false;
2190
2191 memset(&iwe, 0, sizeof(iwe));
2192 iwe.cmd = SIOCGIWAP;
2193 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2194 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
James Minor76a70e92015-02-24 12:58:20 -06002195 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2196 IW_EV_ADDR_LEN);
2197 if (IS_ERR(current_ev))
2198 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002199
2200 memset(&iwe, 0, sizeof(iwe));
2201 iwe.cmd = SIOCGIWFREQ;
2202 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2203 iwe.u.freq.e = 0;
James Minor76a70e92015-02-24 12:58:20 -06002204 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2205 IW_EV_FREQ_LEN);
2206 if (IS_ERR(current_ev))
2207 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002208
2209 memset(&iwe, 0, sizeof(iwe));
2210 iwe.cmd = SIOCGIWFREQ;
2211 iwe.u.freq.m = bss->pub.channel->center_freq;
2212 iwe.u.freq.e = 6;
James Minor76a70e92015-02-24 12:58:20 -06002213 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2214 IW_EV_FREQ_LEN);
2215 if (IS_ERR(current_ev))
2216 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002217
Johannes Berg77965c972009-02-18 18:45:06 +01002218 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01002219 memset(&iwe, 0, sizeof(iwe));
2220 iwe.cmd = IWEVQUAL;
2221 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2222 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01002223 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c972009-02-18 18:45:06 +01002224 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002225 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01002226 sig = bss->pub.signal / 100;
2227 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002228 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01002229 if (sig < -110) /* rather bad */
2230 sig = -110;
2231 else if (sig > -40) /* perfect */
2232 sig = -40;
2233 /* will give a range of 0 .. 70 */
2234 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01002235 break;
2236 case CFG80211_SIGNAL_TYPE_UNSPEC:
2237 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01002238 /* will give range 0 .. 100 */
2239 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01002240 break;
2241 default:
2242 /* not reached */
2243 break;
2244 }
James Minor76a70e92015-02-24 12:58:20 -06002245 current_ev = iwe_stream_add_event_check(info, current_ev,
2246 end_buf, &iwe,
2247 IW_EV_QUAL_LEN);
2248 if (IS_ERR(current_ev))
2249 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002250 }
2251
2252 memset(&iwe, 0, sizeof(iwe));
2253 iwe.cmd = SIOCGIWENCODE;
2254 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2255 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2256 else
2257 iwe.u.data.flags = IW_ENCODE_DISABLED;
2258 iwe.u.data.length = 0;
James Minor76a70e92015-02-24 12:58:20 -06002259 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2260 &iwe, "");
2261 if (IS_ERR(current_ev))
2262 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002263
Johannes Berg9caf0362012-11-29 01:25:20 +01002264 rcu_read_lock();
2265 ies = rcu_dereference(bss->pub.ies);
Johannes Berg83c7aa12013-02-05 16:51:29 +01002266 rem = ies->len;
2267 ie = ies->data;
Johannes Berg9caf0362012-11-29 01:25:20 +01002268
Johannes Berg83c7aa12013-02-05 16:51:29 +01002269 while (rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01002270 /* invalid data */
2271 if (ie[1] > rem - 2)
2272 break;
2273
2274 switch (ie[0]) {
2275 case WLAN_EID_SSID:
2276 memset(&iwe, 0, sizeof(iwe));
2277 iwe.cmd = SIOCGIWESSID;
2278 iwe.u.data.length = ie[1];
2279 iwe.u.data.flags = 1;
James Minor76a70e92015-02-24 12:58:20 -06002280 current_ev = iwe_stream_add_point_check(info,
2281 current_ev,
2282 end_buf, &iwe,
2283 (u8 *)ie + 2);
2284 if (IS_ERR(current_ev))
2285 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002286 break;
2287 case WLAN_EID_MESH_ID:
2288 memset(&iwe, 0, sizeof(iwe));
2289 iwe.cmd = SIOCGIWESSID;
2290 iwe.u.data.length = ie[1];
2291 iwe.u.data.flags = 1;
James Minor76a70e92015-02-24 12:58:20 -06002292 current_ev = iwe_stream_add_point_check(info,
2293 current_ev,
2294 end_buf, &iwe,
2295 (u8 *)ie + 2);
2296 if (IS_ERR(current_ev))
2297 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002298 break;
2299 case WLAN_EID_MESH_CONFIG:
2300 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00002301 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01002302 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01002303 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01002304 memset(&iwe, 0, sizeof(iwe));
2305 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002306 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2307 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01002308 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002309 current_ev = iwe_stream_add_point_check(info,
2310 current_ev,
2311 end_buf,
2312 &iwe, buf);
2313 if (IS_ERR(current_ev))
2314 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002315 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2316 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01002317 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002318 current_ev = iwe_stream_add_point_check(info,
2319 current_ev,
2320 end_buf,
2321 &iwe, buf);
2322 if (IS_ERR(current_ev))
2323 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002324 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2325 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01002326 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002327 current_ev = iwe_stream_add_point_check(info,
2328 current_ev,
2329 end_buf,
2330 &iwe, buf);
2331 if (IS_ERR(current_ev))
2332 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002333 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01002334 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002335 current_ev = iwe_stream_add_point_check(info,
2336 current_ev,
2337 end_buf,
2338 &iwe, buf);
2339 if (IS_ERR(current_ev))
2340 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002341 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2342 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002343 current_ev = iwe_stream_add_point_check(info,
2344 current_ev,
2345 end_buf,
2346 &iwe, buf);
2347 if (IS_ERR(current_ev))
2348 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002349 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2350 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002351 current_ev = iwe_stream_add_point_check(info,
2352 current_ev,
2353 end_buf,
2354 &iwe, buf);
2355 if (IS_ERR(current_ev))
2356 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002357 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01002358 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002359 current_ev = iwe_stream_add_point_check(info,
2360 current_ev,
2361 end_buf,
2362 &iwe, buf);
2363 if (IS_ERR(current_ev))
2364 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002365 break;
2366 case WLAN_EID_SUPP_RATES:
2367 case WLAN_EID_EXT_SUPP_RATES:
2368 /* display all supported rates in readable format */
2369 p = current_ev + iwe_stream_lcp_len(info);
2370
2371 memset(&iwe, 0, sizeof(iwe));
2372 iwe.cmd = SIOCGIWRATE;
2373 /* Those two flags are ignored... */
2374 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2375
2376 for (i = 0; i < ie[1]; i++) {
2377 iwe.u.bitrate.value =
2378 ((ie[i + 2] & 0x7f) * 500000);
James Minor76a70e92015-02-24 12:58:20 -06002379 tmp = p;
Johannes Berg2a519312009-02-10 21:25:55 +01002380 p = iwe_stream_add_value(info, current_ev, p,
James Minor76a70e92015-02-24 12:58:20 -06002381 end_buf, &iwe,
2382 IW_EV_PARAM_LEN);
2383 if (p == tmp) {
2384 current_ev = ERR_PTR(-E2BIG);
2385 goto unlock;
2386 }
Johannes Berg2a519312009-02-10 21:25:55 +01002387 }
2388 current_ev = p;
2389 break;
2390 }
2391 rem -= ie[1] + 2;
2392 ie += ie[1] + 2;
2393 }
2394
Joe Perchesf64f9e72009-11-29 16:55:45 -08002395 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2396 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01002397 memset(&iwe, 0, sizeof(iwe));
2398 iwe.cmd = SIOCGIWMODE;
2399 if (ismesh)
2400 iwe.u.mode = IW_MODE_MESH;
2401 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2402 iwe.u.mode = IW_MODE_MASTER;
2403 else
2404 iwe.u.mode = IW_MODE_ADHOC;
James Minor76a70e92015-02-24 12:58:20 -06002405 current_ev = iwe_stream_add_event_check(info, current_ev,
2406 end_buf, &iwe,
2407 IW_EV_UINT_LEN);
2408 if (IS_ERR(current_ev))
2409 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002410 }
2411
James Minor76a70e92015-02-24 12:58:20 -06002412 memset(&iwe, 0, sizeof(iwe));
2413 iwe.cmd = IWEVCUSTOM;
2414 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2415 iwe.u.data.length = strlen(buf);
2416 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2417 &iwe, buf);
2418 if (IS_ERR(current_ev))
2419 goto unlock;
2420 memset(&iwe, 0, sizeof(iwe));
2421 iwe.cmd = IWEVCUSTOM;
2422 sprintf(buf, " Last beacon: %ums ago",
2423 elapsed_jiffies_msecs(bss->ts));
2424 iwe.u.data.length = strlen(buf);
2425 current_ev = iwe_stream_add_point_check(info, current_ev,
2426 end_buf, &iwe, buf);
2427 if (IS_ERR(current_ev))
2428 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002429
James Minor76a70e92015-02-24 12:58:20 -06002430 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2431
2432 unlock:
Johannes Berg9caf0362012-11-29 01:25:20 +01002433 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01002434 return current_ev;
2435}
2436
2437
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002438static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +01002439 struct iw_request_info *info,
2440 char *buf, size_t len)
2441{
2442 char *current_ev = buf;
2443 char *end_buf = buf + len;
2444 struct cfg80211_internal_bss *bss;
James Minor76a70e92015-02-24 12:58:20 -06002445 int err = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002446
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002447 spin_lock_bh(&rdev->bss_lock);
2448 cfg80211_bss_expire(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01002449
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002450 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01002451 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
James Minor76a70e92015-02-24 12:58:20 -06002452 err = -E2BIG;
2453 break;
Johannes Berg2a519312009-02-10 21:25:55 +01002454 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002455 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
Johannes Berg77965c972009-02-18 18:45:06 +01002456 current_ev, end_buf);
James Minor76a70e92015-02-24 12:58:20 -06002457 if (IS_ERR(current_ev)) {
2458 err = PTR_ERR(current_ev);
2459 break;
2460 }
Johannes Berg2a519312009-02-10 21:25:55 +01002461 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002462 spin_unlock_bh(&rdev->bss_lock);
James Minor76a70e92015-02-24 12:58:20 -06002463
2464 if (err)
2465 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01002466 return current_ev - buf;
2467}
2468
2469
2470int cfg80211_wext_giwscan(struct net_device *dev,
2471 struct iw_request_info *info,
2472 struct iw_point *data, char *extra)
2473{
2474 struct cfg80211_registered_device *rdev;
2475 int res;
2476
2477 if (!netif_running(dev))
2478 return -ENETDOWN;
2479
Johannes Berg463d0182009-07-14 00:33:35 +02002480 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01002481
2482 if (IS_ERR(rdev))
2483 return PTR_ERR(rdev);
2484
Johannes Bergf9d15d12014-01-22 11:14:19 +02002485 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg5fe231e2013-05-08 21:45:15 +02002486 return -EAGAIN;
Johannes Berg2a519312009-02-10 21:25:55 +01002487
2488 res = ieee80211_scan_results(rdev, info, extra, data->length);
2489 data->length = 0;
2490 if (res >= 0) {
2491 data->length = res;
2492 res = 0;
2493 }
2494
Johannes Berg2a519312009-02-10 21:25:55 +01002495 return res;
2496}
Johannes Berg2afe38d2015-01-06 14:00:53 +01002497EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01002498#endif