blob: e67a74488bbe0c554be16b7b8ddbe9e5d8abf13d [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
Amol Grover3ee93062020-02-19 14:41:04 +0530559 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
560 lockdep_rtnl_is_held()) {
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100561 if (pos->reqid == reqid)
562 return pos;
563 }
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100564 return NULL;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100565}
566
567/*
568 * Determines if a scheduled scan request can be handled. When a legacy
569 * scheduled scan is running no other scheduled scan is allowed regardless
570 * whether the request is for legacy or multi-support scan. When a multi-support
571 * scheduled scan is running a request for legacy scan is not allowed. In this
572 * case a request for multi-support scan can be handled if resources are
573 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
574 */
575int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
576 bool want_multi)
577{
578 struct cfg80211_sched_scan_request *pos;
579 int i = 0;
580
581 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
582 /* request id zero means legacy in progress */
583 if (!i && !pos->reqid)
584 return -EINPROGRESS;
585 i++;
586 }
587
588 if (i) {
589 /* no legacy allowed when multi request(s) are active */
590 if (!want_multi)
591 return -EINPROGRESS;
592
593 /* resource limit reached */
594 if (i == rdev->wiphy.max_sched_scan_reqs)
595 return -ENOSPC;
596 }
597 return 0;
598}
599
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100600void cfg80211_sched_scan_results_wk(struct work_struct *work)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300601{
602 struct cfg80211_registered_device *rdev;
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100603 struct cfg80211_sched_scan_request *req, *tmp;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300604
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100605 rdev = container_of(work, struct cfg80211_registered_device,
606 sched_scan_res_wk);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300607
Johannes Berg5fe231e2013-05-08 21:45:15 +0200608 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100609 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
610 if (req->report_results) {
611 req->report_results = false;
612 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
613 /* flush entries from previous scans */
614 spin_lock_bh(&rdev->bss_lock);
615 __cfg80211_bss_expire(rdev, req->scan_start);
616 spin_unlock_bh(&rdev->bss_lock);
617 req->scan_start = jiffies;
618 }
619 nl80211_send_sched_scan(req,
620 NL80211_CMD_SCHED_SCAN_RESULTS);
Sam Leffler15d60302012-10-11 21:03:34 -0700621 }
Sam Leffler15d60302012-10-11 21:03:34 -0700622 }
Johannes Berg5fe231e2013-05-08 21:45:15 +0200623 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300624}
625
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100626void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300627{
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100628 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
629 struct cfg80211_sched_scan_request *request;
630
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100631 trace_cfg80211_sched_scan_results(wiphy, reqid);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300632 /* ignore if we're not scanning */
Jukka Rissanen31a60ed2014-12-15 13:25:38 +0200633
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100634 rcu_read_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100635 request = cfg80211_find_sched_scan_req(rdev, reqid);
636 if (request) {
637 request->report_results = true;
638 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
639 }
Arend Van Spriel1b57b622017-05-23 09:58:07 +0100640 rcu_read_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300641}
642EXPORT_SYMBOL(cfg80211_sched_scan_results);
643
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100644void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300645{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800646 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300647
Eliad Peller792e6aa2014-04-30 16:14:23 +0300648 ASSERT_RTNL();
649
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100650 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
Beni Lev4ee3e062012-08-27 12:49:39 +0300651
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100652 __cfg80211_stop_sched_scan(rdev, reqid, true);
Eliad Peller792e6aa2014-04-30 16:14:23 +0300653}
654EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
655
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100656void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
Eliad Peller792e6aa2014-04-30 16:14:23 +0300657{
658 rtnl_lock();
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100659 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200660 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300661}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300662EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
663
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100664int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
665 struct cfg80211_sched_scan_request *req,
666 bool driver_initiated)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300667{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200668 ASSERT_RTNL();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300669
Luciano Coelho85a99942011-05-12 16:28:29 +0300670 if (!driver_initiated) {
Arend Van Spriel3a3ecf12017-04-21 13:05:02 +0100671 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
Luciano Coelho85a99942011-05-12 16:28:29 +0300672 if (err)
673 return err;
674 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300675
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100676 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300677
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100678 cfg80211_del_sched_scan_req(rdev, req);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300679
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200680 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300681}
682
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100683int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
684 u64 reqid, bool driver_initiated)
685{
686 struct cfg80211_sched_scan_request *sched_scan_req;
687
688 ASSERT_RTNL();
689
690 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100691 if (!sched_scan_req)
692 return -ENOENT;
Arend Van Sprielca986ad2017-04-21 13:05:00 +0100693
694 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
695 driver_initiated);
696}
697
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800698void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500699 unsigned long age_secs)
700{
701 struct cfg80211_internal_bss *bss;
702 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
703
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800704 spin_lock_bh(&rdev->bss_lock);
705 list_for_each_entry(bss, &rdev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500706 bss->ts -= age_jiffies;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800707 spin_unlock_bh(&rdev->bss_lock);
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500708}
709
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800710void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
Johannes Berg2a519312009-02-10 21:25:55 +0100711{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800712 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100713}
714
Emmanuel Grumbach2f1805e2020-06-25 14:15:24 +0300715void cfg80211_bss_flush(struct wiphy *wiphy)
716{
717 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
718
719 spin_lock_bh(&rdev->bss_lock);
720 __cfg80211_bss_expire(rdev, jiffies);
721 spin_unlock_bh(&rdev->bss_lock);
722}
723EXPORT_SYMBOL(cfg80211_bss_flush);
724
Johannes Berg49a68e02019-02-07 23:26:38 +0100725const struct element *
726cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
727 const u8 *match, unsigned int match_len,
728 unsigned int match_offset)
Johannes Berg2a519312009-02-10 21:25:55 +0100729{
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100730 const struct element *elem;
731
Johannes Berg0f3b07f2019-02-07 21:44:41 +0100732 for_each_element_id(elem, eid, ies, len) {
Johannes Berg49a68e02019-02-07 23:26:38 +0100733 if (elem->datalen >= match_offset + match_len &&
734 !memcmp(elem->data + match_offset, match, match_len))
735 return elem;
Johannes Berg2a519312009-02-10 21:25:55 +0100736 }
Luca Coelhofbd05e42016-09-15 18:15:09 +0300737
738 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100739}
Johannes Berg49a68e02019-02-07 23:26:38 +0100740EXPORT_SYMBOL(cfg80211_find_elem_match);
Johannes Berg2a519312009-02-10 21:25:55 +0100741
Johannes Berg49a68e02019-02-07 23:26:38 +0100742const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
743 const u8 *ies,
744 unsigned int len)
Eliad Peller0c28ec52011-09-15 11:53:01 +0300745{
Johannes Berg49a68e02019-02-07 23:26:38 +0100746 const struct element *elem;
Luca Coelhofbd05e42016-09-15 18:15:09 +0300747 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
748 int match_len = (oui_type < 0) ? 3 : sizeof(match);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300749
Emmanuel Grumbach9e9ea432016-05-03 16:08:07 +0300750 if (WARN_ON(oui_type > 0xff))
751 return NULL;
752
Johannes Berg49a68e02019-02-07 23:26:38 +0100753 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
754 match, match_len, 0);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300755
Johannes Berg49a68e02019-02-07 23:26:38 +0100756 if (!elem || elem->datalen < 4)
Luca Coelhofbd05e42016-09-15 18:15:09 +0300757 return NULL;
Luciano Coelho67194292013-02-12 20:11:38 +0200758
Johannes Berg49a68e02019-02-07 23:26:38 +0100759 return elem;
Eliad Peller0c28ec52011-09-15 11:53:01 +0300760}
Johannes Berg49a68e02019-02-07 23:26:38 +0100761EXPORT_SYMBOL(cfg80211_find_vendor_elem);
Eliad Peller0c28ec52011-09-15 11:53:01 +0300762
Johannes Berg4593c4c2013-02-01 19:20:03 +0100763/**
764 * enum bss_compare_mode - BSS compare mode
765 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
766 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
767 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
768 */
769enum bss_compare_mode {
770 BSS_CMP_REGULAR,
771 BSS_CMP_HIDE_ZLEN,
772 BSS_CMP_HIDE_NUL,
773};
774
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100775static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100776 struct cfg80211_bss *b,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100777 enum bss_compare_mode mode)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100778{
Johannes Berg9caf0362012-11-29 01:25:20 +0100779 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg3af63412013-01-30 00:40:20 +0100780 const u8 *ie1 = NULL;
781 const u8 *ie2 = NULL;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100782 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100783
Johannes Berg3af63412013-01-30 00:40:20 +0100784 if (a->channel != b->channel)
785 return b->channel->center_freq - a->channel->center_freq;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100786
Johannes Berg9caf0362012-11-29 01:25:20 +0100787 a_ies = rcu_access_pointer(a->ies);
788 if (!a_ies)
789 return -1;
790 b_ies = rcu_access_pointer(b->ies);
791 if (!b_ies)
792 return 1;
793
Johannes Berg3af63412013-01-30 00:40:20 +0100794 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
795 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
796 a_ies->data, a_ies->len);
797 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
798 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
799 b_ies->data, b_ies->len);
800 if (ie1 && ie2) {
801 int mesh_id_cmp;
802
803 if (ie1[1] == ie2[1])
804 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
805 else
806 mesh_id_cmp = ie2[1] - ie1[1];
807
808 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
809 a_ies->data, a_ies->len);
810 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
811 b_ies->data, b_ies->len);
812 if (ie1 && ie2) {
813 if (mesh_id_cmp)
814 return mesh_id_cmp;
815 if (ie1[1] != ie2[1])
816 return ie2[1] - ie1[1];
817 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
818 }
819 }
820
Johannes Berg3af63412013-01-30 00:40:20 +0100821 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
822 if (r)
823 return r;
824
Johannes Berg9caf0362012-11-29 01:25:20 +0100825 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
826 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100827
Johannes Berg5622f5b2013-01-30 00:26:45 +0100828 if (!ie1 && !ie2)
829 return 0;
830
Johannes Bergf94f8b12012-11-28 22:42:34 +0100831 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100832 * Note that with "hide_ssid", the function returns a match if
833 * the already-present BSS ("b") is a hidden SSID beacon for
834 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100835 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100836
837 /* sort missing IE before (left of) present IE */
838 if (!ie1)
839 return -1;
840 if (!ie2)
841 return 1;
842
Johannes Berg4593c4c2013-02-01 19:20:03 +0100843 switch (mode) {
844 case BSS_CMP_HIDE_ZLEN:
845 /*
846 * In ZLEN mode we assume the BSS entry we're
847 * looking for has a zero-length SSID. So if
848 * the one we're looking at right now has that,
849 * return 0. Otherwise, return the difference
850 * in length, but since we're looking for the
851 * 0-length it's really equivalent to returning
852 * the length of the one we're looking at.
853 *
854 * No content comparison is needed as we assume
855 * the content length is zero.
856 */
857 return ie2[1];
858 case BSS_CMP_REGULAR:
859 default:
860 /* sort by length first, then by contents */
861 if (ie1[1] != ie2[1])
862 return ie2[1] - ie1[1];
Johannes Berg5622f5b2013-01-30 00:26:45 +0100863 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100864 case BSS_CMP_HIDE_NUL:
865 if (ie1[1] != ie2[1])
866 return ie2[1] - ie1[1];
867 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
868 for (i = 0; i < ie2[1]; i++)
869 if (ie2[i + 2])
870 return -1;
871 return 0;
872 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100873}
874
Dedy Lansky6eb18132015-02-08 15:52:03 +0200875static bool cfg80211_bss_type_match(u16 capability,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200876 enum nl80211_band band,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200877 enum ieee80211_bss_type bss_type)
878{
879 bool ret = true;
880 u16 mask, val;
881
882 if (bss_type == IEEE80211_BSS_TYPE_ANY)
883 return ret;
884
Johannes Berg57fbcce2016-04-12 15:56:15 +0200885 if (band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200886 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
887 switch (bss_type) {
888 case IEEE80211_BSS_TYPE_ESS:
889 val = WLAN_CAPABILITY_DMG_TYPE_AP;
890 break;
891 case IEEE80211_BSS_TYPE_PBSS:
892 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
893 break;
894 case IEEE80211_BSS_TYPE_IBSS:
895 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
896 break;
897 default:
898 return false;
899 }
900 } else {
901 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
902 switch (bss_type) {
903 case IEEE80211_BSS_TYPE_ESS:
904 val = WLAN_CAPABILITY_ESS;
905 break;
906 case IEEE80211_BSS_TYPE_IBSS:
907 val = WLAN_CAPABILITY_IBSS;
908 break;
909 case IEEE80211_BSS_TYPE_MBSS:
910 val = 0;
911 break;
912 default:
913 return false;
914 }
915 }
916
917 ret = ((capability & mask) == val);
918 return ret;
919}
920
Ben Greear0e3a39b2013-06-19 14:06:27 -0700921/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100922struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
923 struct ieee80211_channel *channel,
924 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100925 const u8 *ssid, size_t ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200926 enum ieee80211_bss_type bss_type,
927 enum ieee80211_privacy privacy)
Johannes Berg2a519312009-02-10 21:25:55 +0100928{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800929 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +0100930 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200931 unsigned long now = jiffies;
Dedy Lansky6eb18132015-02-08 15:52:03 +0200932 int bss_privacy;
Johannes Berg2a519312009-02-10 21:25:55 +0100933
Dedy Lansky6eb18132015-02-08 15:52:03 +0200934 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
935 privacy);
Beni Lev4ee3e062012-08-27 12:49:39 +0300936
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800937 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100938
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800939 list_for_each_entry(bss, &rdev->bss_list, list) {
Dedy Lansky6eb18132015-02-08 15:52:03 +0200940 if (!cfg80211_bss_type_match(bss->pub.capability,
941 bss->pub.channel->band, bss_type))
942 continue;
943
944 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
945 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
946 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
Johannes Berg79420f02009-02-10 21:25:59 +0100947 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100948 if (channel && bss->pub.channel != channel)
949 continue;
Johannes Bergc14a7402014-04-09 22:36:50 +0200950 if (!is_valid_ether_addr(bss->pub.bssid))
951 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200952 /* Don't get expired BSS structs */
953 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
954 !atomic_read(&bss->hold))
955 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100956 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
957 res = bss;
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800958 bss_ref_get(rdev, res);
Johannes Berg2a519312009-02-10 21:25:55 +0100959 break;
960 }
961 }
962
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800963 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +0100964 if (!res)
965 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300966 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100967 return &res->pub;
968}
969EXPORT_SYMBOL(cfg80211_get_bss);
970
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800971static void rb_insert_bss(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +0100972 struct cfg80211_internal_bss *bss)
973{
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800974 struct rb_node **p = &rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +0100975 struct rb_node *parent = NULL;
976 struct cfg80211_internal_bss *tbss;
977 int cmp;
978
979 while (*p) {
980 parent = *p;
981 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
982
Johannes Berg4593c4c2013-02-01 19:20:03 +0100983 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100984
985 if (WARN_ON(!cmp)) {
986 /* will sort of leak this BSS */
987 return;
988 }
989
990 if (cmp < 0)
991 p = &(*p)->rb_left;
992 else
993 p = &(*p)->rb_right;
994 }
995
996 rb_link_node(&bss->rbn, parent, p);
Zhao, Gang1b8ec872014-04-21 12:53:02 +0800997 rb_insert_color(&bss->rbn, &rdev->bss_tree);
Johannes Berg2a519312009-02-10 21:25:55 +0100998}
999
1000static struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001001rb_find_bss(struct cfg80211_registered_device *rdev,
Johannes Berg5622f5b2013-01-30 00:26:45 +01001002 struct cfg80211_internal_bss *res,
Johannes Berg4593c4c2013-02-01 19:20:03 +01001003 enum bss_compare_mode mode)
Johannes Berg2a519312009-02-10 21:25:55 +01001004{
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001005 struct rb_node *n = rdev->bss_tree.rb_node;
Johannes Berg2a519312009-02-10 21:25:55 +01001006 struct cfg80211_internal_bss *bss;
1007 int r;
1008
1009 while (n) {
1010 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg4593c4c2013-02-01 19:20:03 +01001011 r = cmp_bss(&res->pub, &bss->pub, mode);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001012
1013 if (r == 0)
1014 return bss;
1015 else if (r < 0)
1016 n = n->rb_left;
1017 else
1018 n = n->rb_right;
1019 }
1020
1021 return NULL;
1022}
1023
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001024static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
Johannes Berg776b3582013-02-01 02:06:18 +01001025 struct cfg80211_internal_bss *new)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001026{
Johannes Berg9caf0362012-11-29 01:25:20 +01001027 const struct cfg80211_bss_ies *ies;
Johannes Berg776b3582013-02-01 02:06:18 +01001028 struct cfg80211_internal_bss *bss;
1029 const u8 *ie;
1030 int i, ssidlen;
1031 u8 fold = 0;
Johannes Berg9853a552016-11-15 12:05:11 +01001032 u32 n_entries = 0;
Johannes Berg9caf0362012-11-29 01:25:20 +01001033
Johannes Berg776b3582013-02-01 02:06:18 +01001034 ies = rcu_access_pointer(new->pub.beacon_ies);
Johannes Berg9caf0362012-11-29 01:25:20 +01001035 if (WARN_ON(!ies))
Johannes Berg776b3582013-02-01 02:06:18 +01001036 return false;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001037
Johannes Berg776b3582013-02-01 02:06:18 +01001038 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1039 if (!ie) {
1040 /* nothing to do */
1041 return true;
1042 }
1043
1044 ssidlen = ie[1];
1045 for (i = 0; i < ssidlen; i++)
1046 fold |= ie[2 + i];
1047
1048 if (fold) {
1049 /* not a hidden SSID */
1050 return true;
1051 }
1052
1053 /* This is the bad part ... */
1054
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001055 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg9853a552016-11-15 12:05:11 +01001056 /*
1057 * we're iterating all the entries anyway, so take the
1058 * opportunity to validate the list length accounting
1059 */
1060 n_entries++;
1061
Johannes Berg776b3582013-02-01 02:06:18 +01001062 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1063 continue;
1064 if (bss->pub.channel != new->pub.channel)
1065 continue;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02001066 if (bss->pub.scan_width != new->pub.scan_width)
1067 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001068 if (rcu_access_pointer(bss->pub.beacon_ies))
1069 continue;
1070 ies = rcu_access_pointer(bss->pub.ies);
1071 if (!ies)
1072 continue;
1073 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1074 if (!ie)
1075 continue;
1076 if (ssidlen && ie[1] != ssidlen)
1077 continue;
Johannes Berg776b3582013-02-01 02:06:18 +01001078 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1079 continue;
1080 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1081 list_del(&bss->hidden_list);
1082 /* combine them */
1083 list_add(&bss->hidden_list, &new->hidden_list);
1084 bss->pub.hidden_beacon_bss = &new->pub;
1085 new->refcount += bss->refcount;
1086 rcu_assign_pointer(bss->pub.beacon_ies,
1087 new->pub.beacon_ies);
1088 }
1089
Johannes Berg9853a552016-11-15 12:05:11 +01001090 WARN_ONCE(n_entries != rdev->bss_entries,
1091 "rdev bss entries[%d]/list[len:%d] corruption\n",
1092 rdev->bss_entries, n_entries);
1093
Johannes Berg776b3582013-02-01 02:06:18 +01001094 return true;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001095}
1096
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001097struct cfg80211_non_tx_bss {
1098 struct cfg80211_bss *tx_bss;
1099 u8 max_bssid_indicator;
1100 u8 bssid_index;
1101};
1102
Sergey Matyukevich3ab82272019-07-26 16:39:32 +00001103static bool
1104cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1105 struct cfg80211_internal_bss *known,
1106 struct cfg80211_internal_bss *new,
1107 bool signal_valid)
1108{
1109 lockdep_assert_held(&rdev->bss_lock);
1110
1111 /* Update IEs */
1112 if (rcu_access_pointer(new->pub.proberesp_ies)) {
1113 const struct cfg80211_bss_ies *old;
1114
1115 old = rcu_access_pointer(known->pub.proberesp_ies);
1116
1117 rcu_assign_pointer(known->pub.proberesp_ies,
1118 new->pub.proberesp_ies);
1119 /* Override possible earlier Beacon frame IEs */
1120 rcu_assign_pointer(known->pub.ies,
1121 new->pub.proberesp_ies);
1122 if (old)
1123 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1124 } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1125 const struct cfg80211_bss_ies *old;
1126 struct cfg80211_internal_bss *bss;
1127
1128 if (known->pub.hidden_beacon_bss &&
1129 !list_empty(&known->hidden_list)) {
1130 const struct cfg80211_bss_ies *f;
1131
1132 /* The known BSS struct is one of the probe
1133 * response members of a group, but we're
1134 * receiving a beacon (beacon_ies in the new
1135 * bss is used). This can only mean that the
1136 * AP changed its beacon from not having an
1137 * SSID to showing it, which is confusing so
1138 * drop this information.
1139 */
1140
1141 f = rcu_access_pointer(new->pub.beacon_ies);
1142 kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1143 return false;
1144 }
1145
1146 old = rcu_access_pointer(known->pub.beacon_ies);
1147
1148 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1149
1150 /* Override IEs if they were from a beacon before */
1151 if (old == rcu_access_pointer(known->pub.ies))
1152 rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1153
1154 /* Assign beacon IEs to all sub entries */
1155 list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1156 const struct cfg80211_bss_ies *ies;
1157
1158 ies = rcu_access_pointer(bss->pub.beacon_ies);
1159 WARN_ON(ies != old);
1160
1161 rcu_assign_pointer(bss->pub.beacon_ies,
1162 new->pub.beacon_ies);
1163 }
1164
1165 if (old)
1166 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1167 }
1168
1169 known->pub.beacon_interval = new->pub.beacon_interval;
1170
1171 /* don't update the signal if beacon was heard on
1172 * adjacent channel.
1173 */
1174 if (signal_valid)
1175 known->pub.signal = new->pub.signal;
1176 known->pub.capability = new->pub.capability;
1177 known->ts = new->ts;
1178 known->ts_boottime = new->ts_boottime;
1179 known->parent_tsf = new->parent_tsf;
1180 known->pub.chains = new->pub.chains;
1181 memcpy(known->pub.chain_signal, new->pub.chain_signal,
1182 IEEE80211_MAX_CHAINS);
1183 ether_addr_copy(known->parent_bssid, new->parent_bssid);
1184 known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1185 known->pub.bssid_index = new->pub.bssid_index;
1186
1187 return true;
1188}
1189
Ben Greear0e3a39b2013-06-19 14:06:27 -07001190/* Returned bss is reference counted and must be cleaned up appropriately. */
Chaitanya Tataa3ce17d2019-05-01 18:25:24 +05301191struct cfg80211_internal_bss *
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001192cfg80211_bss_update(struct cfg80211_registered_device *rdev,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001193 struct cfg80211_internal_bss *tmp,
Chaitanya Tataa3ce17d2019-05-01 18:25:24 +05301194 bool signal_valid, unsigned long ts)
Johannes Berg2a519312009-02-10 21:25:55 +01001195{
1196 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001197
Johannes Berg9caf0362012-11-29 01:25:20 +01001198 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +01001199 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001200
Chaitanya Tataa3ce17d2019-05-01 18:25:24 +05301201 tmp->ts = ts;
Johannes Berg2a519312009-02-10 21:25:55 +01001202
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001203 spin_lock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001204
Johannes Berg9caf0362012-11-29 01:25:20 +01001205 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001206 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg9caf0362012-11-29 01:25:20 +01001207 return NULL;
1208 }
1209
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001210 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +01001211
Johannes Bergcd1658f2009-04-16 15:00:58 +02001212 if (found) {
Sergey Matyukevich3ab82272019-07-26 16:39:32 +00001213 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1214 goto drop;
Johannes Berg2a519312009-02-10 21:25:55 +01001215 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +01001216 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001217 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +01001218 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +01001219
Johannes Berg9caf0362012-11-29 01:25:20 +01001220 /*
1221 * create a copy -- the "res" variable that is passed in
1222 * is allocated on the stack since it's not needed in the
1223 * more common case of an update
1224 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001225 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
Johannes Berg9caf0362012-11-29 01:25:20 +01001226 GFP_ATOMIC);
1227 if (!new) {
1228 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1229 if (ies)
1230 kfree_rcu(ies, rcu_head);
1231 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1232 if (ies)
1233 kfree_rcu(ies, rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +01001234 goto drop;
Johannes Berg9caf0362012-11-29 01:25:20 +01001235 }
1236 memcpy(new, tmp, sizeof(*new));
Johannes Berg776b3582013-02-01 02:06:18 +01001237 new->refcount = 1;
1238 INIT_LIST_HEAD(&new->hidden_list);
Sara Sharon7011ba52019-01-21 12:25:59 +02001239 INIT_LIST_HEAD(&new->pub.nontrans_list);
Johannes Berg776b3582013-02-01 02:06:18 +01001240
1241 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001242 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
Johannes Berg776b3582013-02-01 02:06:18 +01001243 if (!hidden)
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001244 hidden = rb_find_bss(rdev, tmp,
Johannes Berg776b3582013-02-01 02:06:18 +01001245 BSS_CMP_HIDE_NUL);
1246 if (hidden) {
1247 new->pub.hidden_beacon_bss = &hidden->pub;
1248 list_add(&new->hidden_list,
1249 &hidden->hidden_list);
1250 hidden->refcount++;
1251 rcu_assign_pointer(new->pub.beacon_ies,
1252 hidden->pub.beacon_ies);
1253 }
1254 } else {
1255 /*
1256 * Ok so we found a beacon, and don't have an entry. If
1257 * it's a beacon with hidden SSID, we might be in for an
1258 * expensive search for any probe responses that should
1259 * be grouped with this beacon for updates ...
1260 */
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001261 if (!cfg80211_combine_bsses(rdev, new)) {
Johannes Berg776b3582013-02-01 02:06:18 +01001262 kfree(new);
1263 goto drop;
1264 }
1265 }
1266
Johannes Berg9853a552016-11-15 12:05:11 +01001267 if (rdev->bss_entries >= bss_entries_limit &&
1268 !cfg80211_bss_expire_oldest(rdev)) {
1269 kfree(new);
1270 goto drop;
1271 }
1272
Sara Sharona3584f56d2019-01-21 12:22:21 +02001273 /* This must be before the call to bss_ref_get */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001274 if (tmp->pub.transmitted_bss) {
Sara Sharona3584f56d2019-01-21 12:22:21 +02001275 struct cfg80211_internal_bss *pbss =
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001276 container_of(tmp->pub.transmitted_bss,
Sara Sharona3584f56d2019-01-21 12:22:21 +02001277 struct cfg80211_internal_bss,
1278 pub);
1279
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001280 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
Sara Sharona3584f56d2019-01-21 12:22:21 +02001281 bss_ref_get(rdev, pbss);
1282 }
1283
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001284 list_add_tail(&new->list, &rdev->bss_list);
Johannes Berg9853a552016-11-15 12:05:11 +01001285 rdev->bss_entries++;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001286 rb_insert_bss(rdev, new);
Johannes Berg9caf0362012-11-29 01:25:20 +01001287 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +01001288 }
1289
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001290 rdev->bss_generation++;
1291 bss_ref_get(rdev, found);
1292 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001293
Johannes Berg2a519312009-02-10 21:25:55 +01001294 return found;
Johannes Berg776b3582013-02-01 02:06:18 +01001295 drop:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001296 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg776b3582013-02-01 02:06:18 +01001297 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001298}
1299
Jouni Malinen119f94a2018-09-05 18:52:22 +03001300/*
1301 * Update RX channel information based on the available frame payload
1302 * information. This is mainly for the 2.4 GHz band where frames can be received
1303 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1304 * element to indicate the current (transmitting) channel, but this might also
1305 * be needed on other bands if RX frequency does not match with the actual
1306 * operating channel of a BSS.
1307 */
Johannes Berg0172bb72012-11-23 14:23:30 +01001308static struct ieee80211_channel *
1309cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001310 struct ieee80211_channel *channel,
1311 enum nl80211_bss_scan_width scan_width)
Johannes Berg0172bb72012-11-23 14:23:30 +01001312{
1313 const u8 *tmp;
1314 u32 freq;
1315 int channel_number = -1;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001316 struct ieee80211_channel *alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001317
1318 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1319 if (tmp && tmp[1] == 1) {
1320 channel_number = tmp[2];
1321 } else {
1322 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1323 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1324 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1325
1326 channel_number = htop->primary_chan;
1327 }
1328 }
1329
Jouni Malinen119f94a2018-09-05 18:52:22 +03001330 if (channel_number < 0) {
1331 /* No channel information in frame payload */
Johannes Berg0172bb72012-11-23 14:23:30 +01001332 return channel;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001333 }
Johannes Berg0172bb72012-11-23 14:23:30 +01001334
Thomas Pedersen934f4c72020-04-01 18:18:03 -07001335 freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1336 alt_channel = ieee80211_get_channel_khz(wiphy, freq);
Jouni Malinen119f94a2018-09-05 18:52:22 +03001337 if (!alt_channel) {
1338 if (channel->band == NL80211_BAND_2GHZ) {
1339 /*
1340 * Better not allow unexpected channels when that could
1341 * be going beyond the 1-11 range (e.g., discovering
1342 * BSS on channel 12 when radio is configured for
1343 * channel 11.
1344 */
1345 return NULL;
1346 }
1347
1348 /* No match for the payload channel number - ignore it */
1349 return channel;
1350 }
1351
1352 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1353 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1354 /*
1355 * Ignore channel number in 5 and 10 MHz channels where there
1356 * may not be an n:1 or 1:n mapping between frequencies and
1357 * channel numbers.
1358 */
1359 return channel;
1360 }
1361
1362 /*
1363 * Use the channel determined through the payload channel number
1364 * instead of the RX channel reported by the driver.
1365 */
1366 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg0172bb72012-11-23 14:23:30 +01001367 return NULL;
Jouni Malinen119f94a2018-09-05 18:52:22 +03001368 return alt_channel;
Johannes Berg0172bb72012-11-23 14:23:30 +01001369}
1370
Ben Greear0e3a39b2013-06-19 14:06:27 -07001371/* Returned bss is reference counted and must be cleaned up appropriately. */
Peng Xu0b8fb822019-01-21 12:14:57 +02001372static struct cfg80211_bss *
1373cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1374 struct cfg80211_inform_bss *data,
1375 enum cfg80211_bss_frame_type ftype,
1376 const u8 *bssid, u64 tsf, u16 capability,
1377 u16 beacon_interval, const u8 *ie, size_t ielen,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001378 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001379 gfp_t gfp)
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001380{
Peng Xu0b8fb822019-01-21 12:14:57 +02001381 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg9caf0362012-11-29 01:25:20 +01001382 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001383 struct ieee80211_channel *channel;
Sara Sharon7011ba52019-01-21 12:25:59 +02001384 struct cfg80211_internal_bss tmp = {}, *res;
Dedy Lansky6eb18132015-02-08 15:52:03 +02001385 int bss_type;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001386 bool signal_valid;
Johannes Berg60d7dfe2019-07-03 15:38:23 +02001387 unsigned long ts;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001388
1389 if (WARN_ON(!wiphy))
1390 return NULL;
1391
Sujith22fe88d2010-05-13 10:34:08 +05301392 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001393 (data->signal < 0 || data->signal > 100)))
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001394 return NULL;
1395
Jouni Malinen119f94a2018-09-05 18:52:22 +03001396 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1397 data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001398 if (!channel)
1399 return NULL;
1400
Johannes Berg9caf0362012-11-29 01:25:20 +01001401 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1402 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001403 tmp.pub.scan_width = data->scan_width;
1404 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001405 tmp.pub.beacon_interval = beacon_interval;
1406 tmp.pub.capability = capability;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001407 tmp.ts_boottime = data->boottime_ns;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001408 if (non_tx_data) {
1409 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
Johannes Berg60d7dfe2019-07-03 15:38:23 +02001410 ts = bss_from_pub(non_tx_data->tx_bss)->ts;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001411 tmp.pub.bssid_index = non_tx_data->bssid_index;
1412 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
Johannes Berg60d7dfe2019-07-03 15:38:23 +02001413 } else {
1414 ts = jiffies;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001415 }
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001416
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001417 /*
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001418 * If we do not know here whether the IEs are from a Beacon or Probe
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001419 * Response frame, we need to pick one of the options and only use it
1420 * with the driver that does not provide the full Beacon/Probe Response
1421 * frame. Use Beacon frame pointer to avoid indicating that this should
Johannes Berg50521aa2013-01-30 21:33:19 +01001422 * override the IEs pointer should we have received an earlier
Johannes Berg9caf0362012-11-29 01:25:20 +01001423 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +02001424 */
Johannes Berg0e227082014-08-12 20:34:30 +02001425 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001426 if (!ies)
1427 return NULL;
1428 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001429 ies->tsf = tsf;
Johannes Berg0e227082014-08-12 20:34:30 +02001430 ies->from_beacon = false;
Johannes Berg9caf0362012-11-29 01:25:20 +01001431 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001432
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001433 switch (ftype) {
1434 case CFG80211_BSS_FTYPE_BEACON:
1435 ies->from_beacon = true;
Luca Coelho925b5972018-12-15 11:03:21 +02001436 /* fall through */
Johannes Berg5bc8c1f2014-08-12 21:01:28 +02001437 case CFG80211_BSS_FTYPE_UNKNOWN:
1438 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1439 break;
1440 case CFG80211_BSS_FTYPE_PRESP:
1441 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1442 break;
1443 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001444 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001445
Emmanuel Grumbach7bb106e2020-02-14 23:23:38 +01001446 signal_valid = data->chan == channel;
Johannes Berg60d7dfe2019-07-03 15:38:23 +02001447 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001448 if (!res)
1449 return NULL;
1450
Johannes Berg57fbcce2016-04-12 15:56:15 +02001451 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001452 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1453 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1454 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1455 regulatory_hint_found_beacon(wiphy, channel, gfp);
1456 } else {
1457 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1458 regulatory_hint_found_beacon(wiphy, channel, gfp);
1459 }
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001460
Johannes Bergb0d1d7f2019-07-03 15:38:22 +02001461 if (non_tx_data) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001462 /* this is a nontransmitting bss, we need to add it to
1463 * transmitting bss' list if it is not there
1464 */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001465 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1466 &res->pub)) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001467 if (__cfg80211_unlink_bss(rdev, res))
1468 rdev->bss_generation++;
1469 }
1470 }
1471
Beni Lev4ee3e062012-08-27 12:49:39 +03001472 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001473 /* cfg80211_bss_update gives us a referenced result */
1474 return &res->pub;
1475}
Peng Xu0b8fb822019-01-21 12:14:57 +02001476
Sara Sharonfe806e42019-03-15 17:39:05 +02001477static const struct element
1478*cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
1479 const struct element *mbssid_elem,
1480 const struct element *sub_elem)
1481{
1482 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
1483 const struct element *next_mbssid;
1484 const struct element *next_sub;
1485
1486 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
1487 mbssid_end,
1488 ielen - (mbssid_end - ie));
1489
1490 /*
1491 * If is is not the last subelement in current MBSSID IE or there isn't
1492 * a next MBSSID IE - profile is complete.
1493 */
1494 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
1495 !next_mbssid)
1496 return NULL;
1497
1498 /* For any length error, just return NULL */
1499
1500 if (next_mbssid->datalen < 4)
1501 return NULL;
1502
1503 next_sub = (void *)&next_mbssid->data[1];
1504
1505 if (next_mbssid->data + next_mbssid->datalen <
1506 next_sub->data + next_sub->datalen)
1507 return NULL;
1508
1509 if (next_sub->id != 0 || next_sub->datalen < 2)
1510 return NULL;
1511
1512 /*
1513 * Check if the first element in the next sub element is a start
1514 * of a new profile
1515 */
1516 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
1517 NULL : next_mbssid;
1518}
1519
1520size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
1521 const struct element *mbssid_elem,
1522 const struct element *sub_elem,
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001523 u8 *merged_ie, size_t max_copy_len)
Sara Sharonfe806e42019-03-15 17:39:05 +02001524{
1525 size_t copied_len = sub_elem->datalen;
1526 const struct element *next_mbssid;
1527
1528 if (sub_elem->datalen > max_copy_len)
1529 return 0;
1530
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001531 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
Sara Sharonfe806e42019-03-15 17:39:05 +02001532
1533 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
1534 mbssid_elem,
1535 sub_elem))) {
1536 const struct element *next_sub = (void *)&next_mbssid->data[1];
1537
1538 if (copied_len + next_sub->datalen > max_copy_len)
1539 break;
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001540 memcpy(merged_ie + copied_len, next_sub->data,
Sara Sharonfe806e42019-03-15 17:39:05 +02001541 next_sub->datalen);
1542 copied_len += next_sub->datalen;
1543 }
1544
1545 return copied_len;
1546}
1547EXPORT_SYMBOL(cfg80211_merge_profile);
1548
Peng Xu0b8fb822019-01-21 12:14:57 +02001549static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1550 struct cfg80211_inform_bss *data,
1551 enum cfg80211_bss_frame_type ftype,
1552 const u8 *bssid, u64 tsf,
1553 u16 beacon_interval, const u8 *ie,
1554 size_t ielen,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001555 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001556 gfp_t gfp)
1557{
Johannes Berg1c8745f2019-02-07 22:36:33 +01001558 const u8 *mbssid_index_ie;
1559 const struct element *elem, *sub;
1560 size_t new_ie_len;
Peng Xu0b8fb822019-01-21 12:14:57 +02001561 u8 new_bssid[ETH_ALEN];
Sara Sharonfe806e42019-03-15 17:39:05 +02001562 u8 *new_ie, *profile;
1563 u64 seen_indices = 0;
Peng Xu0b8fb822019-01-21 12:14:57 +02001564 u16 capability;
1565 struct cfg80211_bss *bss;
1566
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001567 if (!non_tx_data)
Peng Xu0b8fb822019-01-21 12:14:57 +02001568 return;
1569 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1570 return;
Sara Sharon213ed572019-01-16 23:02:03 +02001571 if (!wiphy->support_mbssid)
1572 return;
1573 if (wiphy->support_only_he_mbssid &&
1574 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1575 return;
Peng Xu0b8fb822019-01-21 12:14:57 +02001576
Peng Xu0b8fb822019-01-21 12:14:57 +02001577 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1578 if (!new_ie)
1579 return;
1580
Sara Sharonfe806e42019-03-15 17:39:05 +02001581 profile = kmalloc(ielen, gfp);
1582 if (!profile)
1583 goto out;
1584
Johannes Berg1c8745f2019-02-07 22:36:33 +01001585 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1586 if (elem->datalen < 4)
1587 continue;
1588 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
Sara Sharonfe806e42019-03-15 17:39:05 +02001589 u8 profile_len;
1590
Johannes Berg1c8745f2019-02-07 22:36:33 +01001591 if (sub->id != 0 || sub->datalen < 4) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001592 /* not a valid BSS profile */
1593 continue;
1594 }
1595
Johannes Berg1c8745f2019-02-07 22:36:33 +01001596 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1597 sub->data[1] != 2) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001598 /* The first element within the Nontransmitted
1599 * BSSID Profile is not the Nontransmitted
1600 * BSSID Capability element.
1601 */
1602 continue;
1603 }
1604
Sara Sharonfe806e42019-03-15 17:39:05 +02001605 memset(profile, 0, ielen);
1606 profile_len = cfg80211_merge_profile(ie, ielen,
1607 elem,
1608 sub,
Dan Carpenter5809a5d2019-04-11 11:59:50 +03001609 profile,
Sara Sharonfe806e42019-03-15 17:39:05 +02001610 ielen);
1611
Peng Xu0b8fb822019-01-21 12:14:57 +02001612 /* found a Nontransmitted BSSID Profile */
1613 mbssid_index_ie = cfg80211_find_ie
1614 (WLAN_EID_MULTI_BSSID_IDX,
Sara Sharonfe806e42019-03-15 17:39:05 +02001615 profile, profile_len);
Peng Xu0b8fb822019-01-21 12:14:57 +02001616 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
Sara Sharonfe806e42019-03-15 17:39:05 +02001617 mbssid_index_ie[2] == 0 ||
1618 mbssid_index_ie[2] > 46) {
Peng Xu0b8fb822019-01-21 12:14:57 +02001619 /* No valid Multiple BSSID-Index element */
1620 continue;
1621 }
1622
Luca Coelhoebb3ca32019-05-29 15:25:29 +03001623 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
Sara Sharonfe806e42019-03-15 17:39:05 +02001624 /* We don't support legacy split of a profile */
1625 net_dbg_ratelimited("Partial info for BSSID index %d\n",
1626 mbssid_index_ie[2]);
1627
Luca Coelhoebb3ca32019-05-29 15:25:29 +03001628 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
Sara Sharonfe806e42019-03-15 17:39:05 +02001629
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001630 non_tx_data->bssid_index = mbssid_index_ie[2];
1631 non_tx_data->max_bssid_indicator = elem->data[0];
1632
1633 cfg80211_gen_new_bssid(bssid,
1634 non_tx_data->max_bssid_indicator,
1635 non_tx_data->bssid_index,
Peng Xu0b8fb822019-01-21 12:14:57 +02001636 new_bssid);
1637 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
Sara Sharonfe806e42019-03-15 17:39:05 +02001638 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
1639 profile,
1640 profile_len, new_ie,
Peng Xu0b8fb822019-01-21 12:14:57 +02001641 gfp);
1642 if (!new_ie_len)
1643 continue;
1644
Sara Sharonfe806e42019-03-15 17:39:05 +02001645 capability = get_unaligned_le16(profile + 2);
Peng Xu0b8fb822019-01-21 12:14:57 +02001646 bss = cfg80211_inform_single_bss_data(wiphy, data,
1647 ftype,
1648 new_bssid, tsf,
1649 capability,
1650 beacon_interval,
1651 new_ie,
1652 new_ie_len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001653 non_tx_data,
1654 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001655 if (!bss)
1656 break;
1657 cfg80211_put_bss(wiphy, bss);
1658 }
Peng Xu0b8fb822019-01-21 12:14:57 +02001659 }
1660
Sara Sharonfe806e42019-03-15 17:39:05 +02001661out:
Peng Xu0b8fb822019-01-21 12:14:57 +02001662 kfree(new_ie);
Sara Sharonfe806e42019-03-15 17:39:05 +02001663 kfree(profile);
Peng Xu0b8fb822019-01-21 12:14:57 +02001664}
1665
1666struct cfg80211_bss *
1667cfg80211_inform_bss_data(struct wiphy *wiphy,
1668 struct cfg80211_inform_bss *data,
1669 enum cfg80211_bss_frame_type ftype,
1670 const u8 *bssid, u64 tsf, u16 capability,
1671 u16 beacon_interval, const u8 *ie, size_t ielen,
1672 gfp_t gfp)
1673{
1674 struct cfg80211_bss *res;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001675 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001676
1677 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1678 capability, beacon_interval, ie,
1679 ielen, NULL, gfp);
Johannes Bergb0d1d7f2019-07-03 15:38:22 +02001680 if (!res)
1681 return NULL;
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001682 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001683 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001684 beacon_interval, ie, ielen, &non_tx_data,
1685 gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001686 return res;
1687}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001688EXPORT_SYMBOL(cfg80211_inform_bss_data);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +02001689
Peng Xu0b8fb822019-01-21 12:14:57 +02001690static void
1691cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1692 struct cfg80211_inform_bss *data,
1693 struct ieee80211_mgmt *mgmt, size_t len,
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001694 struct cfg80211_non_tx_bss *non_tx_data,
Peng Xu0b8fb822019-01-21 12:14:57 +02001695 gfp_t gfp)
1696{
1697 enum cfg80211_bss_frame_type ftype;
1698 const u8 *ie = mgmt->u.probe_resp.variable;
1699 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1700 u.probe_resp.variable);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001701
Peng Xu0b8fb822019-01-21 12:14:57 +02001702 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1703 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1704
1705 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1706 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1707 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001708 ie, ielen, non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001709}
1710
1711static void
1712cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
Sara Sharon7011ba52019-01-21 12:25:59 +02001713 struct cfg80211_bss *nontrans_bss,
Sara Sharon461c4c22019-10-04 15:37:06 +03001714 struct ieee80211_mgmt *mgmt, size_t len)
Peng Xu0b8fb822019-01-21 12:14:57 +02001715{
1716 u8 *ie, *new_ie, *pos;
1717 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1718 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1719 u.probe_resp.variable);
1720 size_t new_ie_len;
1721 struct cfg80211_bss_ies *new_ies;
1722 const struct cfg80211_bss_ies *old;
1723 u8 cpy_len;
1724
Sara Sharon461c4c22019-10-04 15:37:06 +03001725 lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
1726
Peng Xu0b8fb822019-01-21 12:14:57 +02001727 ie = mgmt->u.probe_resp.variable;
1728
1729 new_ie_len = ielen;
1730 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1731 if (!trans_ssid)
1732 return;
1733 new_ie_len -= trans_ssid[1];
1734 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
Johannes Berg242b0932019-09-20 21:54:18 +02001735 /*
1736 * It's not valid to have the MBSSID element before SSID
1737 * ignore if that happens - the code below assumes it is
1738 * after (while copying things inbetween).
1739 */
1740 if (!mbssid || mbssid < trans_ssid)
Peng Xu0b8fb822019-01-21 12:14:57 +02001741 return;
1742 new_ie_len -= mbssid[1];
Sara Sharon461c4c22019-10-04 15:37:06 +03001743
Sara Sharon7011ba52019-01-21 12:25:59 +02001744 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
Sara Sharon461c4c22019-10-04 15:37:06 +03001745 if (!nontrans_ssid)
Peng Xu0b8fb822019-01-21 12:14:57 +02001746 return;
Sara Sharon461c4c22019-10-04 15:37:06 +03001747
Peng Xu0b8fb822019-01-21 12:14:57 +02001748 new_ie_len += nontrans_ssid[1];
Peng Xu0b8fb822019-01-21 12:14:57 +02001749
1750 /* generate new ie for nontrans BSS
1751 * 1. replace SSID with nontrans BSS' SSID
1752 * 2. skip MBSSID IE
1753 */
Sara Sharon461c4c22019-10-04 15:37:06 +03001754 new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
Peng Xu0b8fb822019-01-21 12:14:57 +02001755 if (!new_ie)
1756 return;
Sara Sharon461c4c22019-10-04 15:37:06 +03001757
1758 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
Sara Sharonbede8d22019-01-30 08:48:21 +02001759 if (!new_ies)
1760 goto out_free;
Peng Xu0b8fb822019-01-21 12:14:57 +02001761
1762 pos = new_ie;
1763
1764 /* copy the nontransmitted SSID */
1765 cpy_len = nontrans_ssid[1] + 2;
1766 memcpy(pos, nontrans_ssid, cpy_len);
1767 pos += cpy_len;
1768 /* copy the IEs between SSID and MBSSID */
1769 cpy_len = trans_ssid[1] + 2;
1770 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1771 pos += (mbssid - (trans_ssid + cpy_len));
1772 /* copy the IEs after MBSSID */
1773 cpy_len = mbssid[1] + 2;
1774 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1775
1776 /* update ie */
1777 new_ies->len = new_ie_len;
1778 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1779 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1780 memcpy(new_ies->data, new_ie, new_ie_len);
1781 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001782 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1783 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1784 rcu_assign_pointer(nontrans_bss->ies, new_ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001785 if (old)
1786 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1787 } else {
Sara Sharon7011ba52019-01-21 12:25:59 +02001788 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1789 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1790 rcu_assign_pointer(nontrans_bss->ies, new_ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001791 if (old)
1792 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1793 }
Sara Sharonbede8d22019-01-30 08:48:21 +02001794
1795out_free:
1796 kfree(new_ie);
Peng Xu0b8fb822019-01-21 12:14:57 +02001797}
1798
1799/* cfg80211_inform_bss_width_frame helper */
1800static struct cfg80211_bss *
1801cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1802 struct cfg80211_inform_bss *data,
1803 struct ieee80211_mgmt *mgmt, size_t len,
Peng Xu0b8fb822019-01-21 12:14:57 +02001804 gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +01001805{
Johannes Berg9caf0362012-11-29 01:25:20 +01001806 struct cfg80211_internal_bss tmp = {}, *res;
1807 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001808 struct ieee80211_channel *channel;
Emmanuel Grumbach67af9812014-05-18 10:15:24 +03001809 bool signal_valid;
Johannes Berg2a519312009-02-10 21:25:55 +01001810 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1811 u.probe_resp.variable);
Dedy Lansky6eb18132015-02-08 15:52:03 +02001812 int bss_type;
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001813
Johannes Berg0172bb72012-11-23 14:23:30 +01001814 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1815 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1816
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001817 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
Beni Lev4ee3e062012-08-27 12:49:39 +03001818
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001819 if (WARN_ON(!mgmt))
1820 return NULL;
1821
1822 if (WARN_ON(!wiphy))
1823 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001824
Sujith22fe88d2010-05-13 10:34:08 +05301825 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001826 (data->signal < 0 || data->signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +01001827 return NULL;
1828
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +01001829 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +01001830 return NULL;
1831
Johannes Berg0172bb72012-11-23 14:23:30 +01001832 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
Jouni Malinen119f94a2018-09-05 18:52:22 +03001833 ielen, data->chan, data->scan_width);
Johannes Berg0172bb72012-11-23 14:23:30 +01001834 if (!channel)
1835 return NULL;
1836
Johannes Berg0e227082014-08-12 20:34:30 +02001837 ies = kzalloc(sizeof(*ies) + ielen, gfp);
Johannes Berg9caf0362012-11-29 01:25:20 +01001838 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001839 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +01001840 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001841 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
Johannes Berg0e227082014-08-12 20:34:30 +02001842 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
Johannes Berg9caf0362012-11-29 01:25:20 +01001843 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +01001844
Johannes Berg9caf0362012-11-29 01:25:20 +01001845 if (ieee80211_is_probe_resp(mgmt->frame_control))
1846 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1847 else
1848 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1849 rcu_assign_pointer(tmp.pub.ies, ies);
Arend Van Spriel505a2e82016-12-16 11:21:54 +00001850
Johannes Berg9caf0362012-11-29 01:25:20 +01001851 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1852 tmp.pub.channel = channel;
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001853 tmp.pub.scan_width = data->scan_width;
1854 tmp.pub.signal = data->signal;
Johannes Berg9caf0362012-11-29 01:25:20 +01001855 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1856 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001857 tmp.ts_boottime = data->boottime_ns;
Avraham Stern1d762502016-07-05 17:10:13 +03001858 tmp.parent_tsf = data->parent_tsf;
Sunil Dutt983dafa2017-12-13 19:51:36 +02001859 tmp.pub.chains = data->chains;
1860 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
Avraham Stern1d762502016-07-05 17:10:13 +03001861 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
Johannes Berg2a519312009-02-10 21:25:55 +01001862
Emmanuel Grumbach7bb106e2020-02-14 23:23:38 +01001863 signal_valid = data->chan == channel;
Chaitanya Tataa3ce17d2019-05-01 18:25:24 +05301864 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
1865 jiffies);
Johannes Berg2a519312009-02-10 21:25:55 +01001866 if (!res)
1867 return NULL;
1868
Johannes Berg57fbcce2016-04-12 15:56:15 +02001869 if (channel->band == NL80211_BAND_60GHZ) {
Dedy Lansky6eb18132015-02-08 15:52:03 +02001870 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1871 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1872 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1873 regulatory_hint_found_beacon(wiphy, channel, gfp);
1874 } else {
1875 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1876 regulatory_hint_found_beacon(wiphy, channel, gfp);
1877 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001878
Beni Lev4ee3e062012-08-27 12:49:39 +03001879 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +01001880 /* cfg80211_bss_update gives us a referenced result */
1881 return &res->pub;
1882}
Peng Xu0b8fb822019-01-21 12:14:57 +02001883
1884struct cfg80211_bss *
1885cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1886 struct cfg80211_inform_bss *data,
1887 struct ieee80211_mgmt *mgmt, size_t len,
1888 gfp_t gfp)
1889{
Sara Sharon7011ba52019-01-21 12:25:59 +02001890 struct cfg80211_bss *res, *tmp_bss;
Peng Xu0b8fb822019-01-21 12:14:57 +02001891 const u8 *ie = mgmt->u.probe_resp.variable;
1892 const struct cfg80211_bss_ies *ies1, *ies2;
1893 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1894 u.probe_resp.variable);
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001895 struct cfg80211_non_tx_bss non_tx_data;
Peng Xu0b8fb822019-01-21 12:14:57 +02001896
1897 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
Johannes Berg84f17722019-07-03 15:38:21 +02001898 len, gfp);
Sara Sharon213ed572019-01-16 23:02:03 +02001899 if (!res || !wiphy->support_mbssid ||
1900 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1901 return res;
1902 if (wiphy->support_only_he_mbssid &&
1903 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
Peng Xu0b8fb822019-01-21 12:14:57 +02001904 return res;
1905
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001906 non_tx_data.tx_bss = res;
Peng Xu0b8fb822019-01-21 12:14:57 +02001907 /* process each non-transmitting bss */
Sara Sharon0cd01ef2019-01-22 09:50:50 +02001908 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1909 &non_tx_data, gfp);
Peng Xu0b8fb822019-01-21 12:14:57 +02001910
Sara Sharon461c4c22019-10-04 15:37:06 +03001911 spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
1912
Peng Xu0b8fb822019-01-21 12:14:57 +02001913 /* check if the res has other nontransmitting bss which is not
1914 * in MBSSID IE
1915 */
1916 ies1 = rcu_access_pointer(res->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001917
1918 /* go through nontrans_list, if the timestamp of the BSS is
1919 * earlier than the timestamp of the transmitting BSS then
1920 * update it
1921 */
Sara Sharon7011ba52019-01-21 12:25:59 +02001922 list_for_each_entry(tmp_bss, &res->nontrans_list,
Peng Xu0b8fb822019-01-21 12:14:57 +02001923 nontrans_list) {
Sara Sharon7011ba52019-01-21 12:25:59 +02001924 ies2 = rcu_access_pointer(tmp_bss->ies);
Peng Xu0b8fb822019-01-21 12:14:57 +02001925 if (ies2->tsf < ies1->tsf)
1926 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
Sara Sharon461c4c22019-10-04 15:37:06 +03001927 mgmt, len);
Peng Xu0b8fb822019-01-21 12:14:57 +02001928 }
Sara Sharon461c4c22019-10-04 15:37:06 +03001929 spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
Peng Xu0b8fb822019-01-21 12:14:57 +02001930
1931 return res;
1932}
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02001933EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
Johannes Berg2a519312009-02-10 21:25:55 +01001934
Johannes Berg5b112d32013-02-01 01:49:58 +01001935void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001936{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001937 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001938 struct cfg80211_internal_bss *bss;
1939
1940 if (!pub)
1941 return;
1942
1943 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001944
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001945 spin_lock_bh(&rdev->bss_lock);
1946 bss_ref_get(rdev, bss);
1947 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001948}
1949EXPORT_SYMBOL(cfg80211_ref_bss);
1950
Johannes Berg5b112d32013-02-01 01:49:58 +01001951void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg2a519312009-02-10 21:25:55 +01001952{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001953 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001954 struct cfg80211_internal_bss *bss;
1955
1956 if (!pub)
1957 return;
1958
1959 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001960
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001961 spin_lock_bh(&rdev->bss_lock);
1962 bss_ref_put(rdev, bss);
1963 spin_unlock_bh(&rdev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001964}
1965EXPORT_SYMBOL(cfg80211_put_bss);
1966
Johannes Bergd491af12009-02-10 21:25:58 +01001967void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1968{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001969 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Sara Sharon7011ba52019-01-21 12:25:59 +02001970 struct cfg80211_internal_bss *bss, *tmp1;
1971 struct cfg80211_bss *nontrans_bss, *tmp;
Johannes Bergd491af12009-02-10 21:25:58 +01001972
1973 if (WARN_ON(!pub))
1974 return;
1975
1976 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1977
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001978 spin_lock_bh(&rdev->bss_lock);
Sara Sharon7011ba52019-01-21 12:25:59 +02001979 if (list_empty(&bss->list))
1980 goto out;
Peng Xu0b8fb822019-01-21 12:14:57 +02001981
Sara Sharon7011ba52019-01-21 12:25:59 +02001982 list_for_each_entry_safe(nontrans_bss, tmp,
1983 &pub->nontrans_list,
1984 nontrans_list) {
1985 tmp1 = container_of(nontrans_bss,
1986 struct cfg80211_internal_bss, pub);
1987 if (__cfg80211_unlink_bss(rdev, tmp1))
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001988 rdev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +02001989 }
Sara Sharon7011ba52019-01-21 12:25:59 +02001990
1991 if (__cfg80211_unlink_bss(rdev, bss))
1992 rdev->bss_generation++;
1993out:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001994 spin_unlock_bh(&rdev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +01001995}
1996EXPORT_SYMBOL(cfg80211_unlink_bss);
1997
Ilan Peer4770c8f2019-05-29 15:25:32 +03001998void cfg80211_bss_iter(struct wiphy *wiphy,
1999 struct cfg80211_chan_def *chandef,
2000 void (*iter)(struct wiphy *wiphy,
2001 struct cfg80211_bss *bss,
2002 void *data),
2003 void *iter_data)
2004{
2005 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2006 struct cfg80211_internal_bss *bss;
2007
2008 spin_lock_bh(&rdev->bss_lock);
2009
2010 list_for_each_entry(bss, &rdev->bss_list, list) {
2011 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel))
2012 iter(wiphy, &bss->pub, iter_data);
2013 }
2014
2015 spin_unlock_bh(&rdev->bss_lock);
2016}
2017EXPORT_SYMBOL(cfg80211_bss_iter);
2018
Sergey Matyukevich0afd4252019-07-26 16:39:34 +00002019void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2020 struct ieee80211_channel *chan)
2021{
2022 struct wiphy *wiphy = wdev->wiphy;
2023 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2024 struct cfg80211_internal_bss *cbss = wdev->current_bss;
2025 struct cfg80211_internal_bss *new = NULL;
2026 struct cfg80211_internal_bss *bss;
2027 struct cfg80211_bss *nontrans_bss;
2028 struct cfg80211_bss *tmp;
2029
2030 spin_lock_bh(&rdev->bss_lock);
2031
Ilan Peer05dcb8b2020-03-26 15:09:43 +02002032 /*
2033 * Some APs use CSA also for bandwidth changes, i.e., without actually
2034 * changing the control channel, so no need to update in such a case.
2035 */
2036 if (cbss->pub.channel == chan)
Sergey Matyukevich0afd4252019-07-26 16:39:34 +00002037 goto done;
2038
2039 /* use transmitting bss */
2040 if (cbss->pub.transmitted_bss)
2041 cbss = container_of(cbss->pub.transmitted_bss,
2042 struct cfg80211_internal_bss,
2043 pub);
2044
2045 cbss->pub.channel = chan;
2046
2047 list_for_each_entry(bss, &rdev->bss_list, list) {
2048 if (!cfg80211_bss_type_match(bss->pub.capability,
2049 bss->pub.channel->band,
2050 wdev->conn_bss_type))
2051 continue;
2052
2053 if (bss == cbss)
2054 continue;
2055
2056 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2057 new = bss;
2058 break;
2059 }
2060 }
2061
2062 if (new) {
2063 /* to save time, update IEs for transmitting bss only */
2064 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2065 new->pub.proberesp_ies = NULL;
2066 new->pub.beacon_ies = NULL;
2067 }
2068
2069 list_for_each_entry_safe(nontrans_bss, tmp,
2070 &new->pub.nontrans_list,
2071 nontrans_list) {
2072 bss = container_of(nontrans_bss,
2073 struct cfg80211_internal_bss, pub);
2074 if (__cfg80211_unlink_bss(rdev, bss))
2075 rdev->bss_generation++;
2076 }
2077
2078 WARN_ON(atomic_read(&new->hold));
2079 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2080 rdev->bss_generation++;
2081 }
2082
2083 rb_erase(&cbss->rbn, &rdev->bss_tree);
2084 rb_insert_bss(rdev, cbss);
2085 rdev->bss_generation++;
2086
2087 list_for_each_entry_safe(nontrans_bss, tmp,
2088 &cbss->pub.nontrans_list,
2089 nontrans_list) {
2090 bss = container_of(nontrans_bss,
2091 struct cfg80211_internal_bss, pub);
2092 bss->pub.channel = chan;
2093 rb_erase(&bss->rbn, &rdev->bss_tree);
2094 rb_insert_bss(rdev, bss);
2095 rdev->bss_generation++;
2096 }
2097
2098done:
2099 spin_unlock_bh(&rdev->bss_lock);
2100}
2101
Johannes Berg3d23e342009-09-29 23:27:28 +02002102#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9f419f32013-05-08 21:34:22 +02002103static struct cfg80211_registered_device *
2104cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2105{
Johannes Berg5fe231e2013-05-08 21:45:15 +02002106 struct cfg80211_registered_device *rdev;
Johannes Berg9f419f32013-05-08 21:34:22 +02002107 struct net_device *dev;
2108
Johannes Berg5fe231e2013-05-08 21:45:15 +02002109 ASSERT_RTNL();
2110
Johannes Berg9f419f32013-05-08 21:34:22 +02002111 dev = dev_get_by_index(net, ifindex);
2112 if (!dev)
Johannes Berg5fe231e2013-05-08 21:45:15 +02002113 return ERR_PTR(-ENODEV);
2114 if (dev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +08002115 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002116 else
Johannes Berg9f419f32013-05-08 21:34:22 +02002117 rdev = ERR_PTR(-ENODEV);
2118 dev_put(dev);
Johannes Berg9f419f32013-05-08 21:34:22 +02002119 return rdev;
2120}
2121
Johannes Berg2a519312009-02-10 21:25:55 +01002122int cfg80211_wext_siwscan(struct net_device *dev,
2123 struct iw_request_info *info,
2124 union iwreq_data *wrqu, char *extra)
2125{
2126 struct cfg80211_registered_device *rdev;
2127 struct wiphy *wiphy;
2128 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01002129 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01002130 int i, err, n_channels = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002131 enum nl80211_band band;
Johannes Berg2a519312009-02-10 21:25:55 +01002132
2133 if (!netif_running(dev))
2134 return -ENETDOWN;
2135
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002136 if (wrqu->data.length == sizeof(struct iw_scan_req))
2137 wreq = (struct iw_scan_req *)extra;
2138
Johannes Berg463d0182009-07-14 00:33:35 +02002139 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01002140
2141 if (IS_ERR(rdev))
2142 return PTR_ERR(rdev);
2143
Johannes Bergf9d15d12014-01-22 11:14:19 +02002144 if (rdev->scan_req || rdev->scan_msg) {
Johannes Berg2a519312009-02-10 21:25:55 +01002145 err = -EBUSY;
2146 goto out;
2147 }
2148
2149 wiphy = &rdev->wiphy;
2150
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002151 /* Determine number of channels, needed to allocate creq */
2152 if (wreq && wreq->num_channels)
2153 n_channels = wreq->num_channels;
Ilan Peerbdfbec22014-01-09 11:37:23 +02002154 else
2155 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01002156
2157 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2158 n_channels * sizeof(void *),
2159 GFP_ATOMIC);
2160 if (!creq) {
2161 err = -ENOMEM;
2162 goto out;
2163 }
2164
2165 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +02002166 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +02002167 /* SSIDs come after channels */
2168 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01002169 creq->n_channels = n_channels;
2170 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -07002171 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01002172
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002173 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +01002174 i = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002175 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg2a519312009-02-10 21:25:55 +01002176 int j;
Johannes Berg584991d2009-11-02 13:32:03 +01002177
Johannes Berg2a519312009-02-10 21:25:55 +01002178 if (!wiphy->bands[band])
2179 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01002180
Johannes Berg2a519312009-02-10 21:25:55 +01002181 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01002182 /* ignore disabled channels */
2183 if (wiphy->bands[band]->channels[j].flags &
2184 IEEE80211_CHAN_DISABLED)
2185 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002186
2187 /* If we have a wireless request structure and the
2188 * wireless request specifies frequencies, then search
2189 * for the matching hardware channel.
2190 */
2191 if (wreq && wreq->num_channels) {
2192 int k;
2193 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2194 for (k = 0; k < wreq->num_channels; k++) {
Zhao, Gang96998e32014-04-09 09:28:06 +08002195 struct iw_freq *freq =
2196 &wreq->channel_list[k];
2197 int wext_freq =
2198 cfg80211_wext_freq(freq);
2199
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002200 if (wext_freq == wiphy_freq)
2201 goto wext_freq_found;
2202 }
2203 goto wext_freq_not_found;
2204 }
2205
2206 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01002207 creq->channels[i] = &wiphy->bands[band]->channels[j];
2208 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002209 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01002210 }
2211 }
Holger Schurig8862dc52009-09-11 10:13:55 +02002212 /* No channels found? */
2213 if (!i) {
2214 err = -EINVAL;
2215 goto out;
2216 }
Johannes Berg2a519312009-02-10 21:25:55 +01002217
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002218 /* Set real number of channels specified in creq->channels[] */
2219 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01002220
Holger Schurigb2e3abd2009-09-09 13:09:54 +02002221 /* translate "Scan for SSID" request */
2222 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01002223 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01002224 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2225 err = -EINVAL;
2226 goto out;
2227 }
Johannes Berg2a519312009-02-10 21:25:55 +01002228 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2229 creq->ssids[0].ssid_len = wreq->essid_len;
2230 }
2231 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2232 creq->n_ssids = 0;
2233 }
2234
Johannes Berg57fbcce2016-04-12 15:56:15 +02002235 for (i = 0; i < NUM_NL80211_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02002236 if (wiphy->bands[i])
2237 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02002238
Jouni Malinen818965d2016-02-26 22:12:47 +02002239 eth_broadcast_addr(creq->bssid);
2240
Johannes Berg2a519312009-02-10 21:25:55 +01002241 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03002242 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01002243 if (err) {
2244 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01002245 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02002246 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02002247 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01002248 /* creq now owned by driver */
2249 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02002250 dev_hold(dev);
2251 }
Johannes Berg2a519312009-02-10 21:25:55 +01002252 out:
Johannes Berg65486c82009-12-23 15:33:35 +01002253 kfree(creq);
Johannes Berg2a519312009-02-10 21:25:55 +01002254 return err;
2255}
Johannes Berg2afe38d2015-01-06 14:00:53 +01002256EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01002257
James Minor76a70e92015-02-24 12:58:20 -06002258static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2259 const struct cfg80211_bss_ies *ies,
2260 char *current_ev, char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01002261{
Johannes Berg9caf0362012-11-29 01:25:20 +01002262 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01002263 struct iw_event iwe;
2264
Johannes Berg9caf0362012-11-29 01:25:20 +01002265 if (!ies)
James Minor76a70e92015-02-24 12:58:20 -06002266 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002267
2268 /*
2269 * If needed, fragment the IEs buffer (at IE boundaries) into short
2270 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2271 */
Johannes Berg9caf0362012-11-29 01:25:20 +01002272 pos = ies->data;
2273 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01002274
2275 while (end - pos > IW_GENERIC_IE_MAX) {
2276 next = pos + 2 + pos[1];
2277 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2278 next = next + 2 + next[1];
2279
2280 memset(&iwe, 0, sizeof(iwe));
2281 iwe.cmd = IWEVGENIE;
2282 iwe.u.data.length = next - pos;
James Minor76a70e92015-02-24 12:58:20 -06002283 current_ev = iwe_stream_add_point_check(info, current_ev,
2284 end_buf, &iwe,
2285 (void *)pos);
2286 if (IS_ERR(current_ev))
2287 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002288 pos = next;
2289 }
2290
2291 if (end > pos) {
2292 memset(&iwe, 0, sizeof(iwe));
2293 iwe.cmd = IWEVGENIE;
2294 iwe.u.data.length = end - pos;
James Minor76a70e92015-02-24 12:58:20 -06002295 current_ev = iwe_stream_add_point_check(info, current_ev,
2296 end_buf, &iwe,
2297 (void *)pos);
2298 if (IS_ERR(current_ev))
2299 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002300 }
James Minor76a70e92015-02-24 12:58:20 -06002301
2302 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002303}
2304
Johannes Berg2a519312009-02-10 21:25:55 +01002305static char *
Johannes Berg77965c972009-02-18 18:45:06 +01002306ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2307 struct cfg80211_internal_bss *bss, char *current_ev,
2308 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01002309{
Johannes Berg9caf0362012-11-29 01:25:20 +01002310 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01002311 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01002312 const u8 *ie;
James Minor76a70e92015-02-24 12:58:20 -06002313 u8 buf[50];
2314 u8 *cfg, *p, *tmp;
Johannes Berg9caf0362012-11-29 01:25:20 +01002315 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002316 bool ismesh = false;
2317
2318 memset(&iwe, 0, sizeof(iwe));
2319 iwe.cmd = SIOCGIWAP;
2320 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2321 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
James Minor76a70e92015-02-24 12:58:20 -06002322 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2323 IW_EV_ADDR_LEN);
2324 if (IS_ERR(current_ev))
2325 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002326
2327 memset(&iwe, 0, sizeof(iwe));
2328 iwe.cmd = SIOCGIWFREQ;
2329 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2330 iwe.u.freq.e = 0;
James Minor76a70e92015-02-24 12:58:20 -06002331 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2332 IW_EV_FREQ_LEN);
2333 if (IS_ERR(current_ev))
2334 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002335
2336 memset(&iwe, 0, sizeof(iwe));
2337 iwe.cmd = SIOCGIWFREQ;
2338 iwe.u.freq.m = bss->pub.channel->center_freq;
2339 iwe.u.freq.e = 6;
James Minor76a70e92015-02-24 12:58:20 -06002340 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2341 IW_EV_FREQ_LEN);
2342 if (IS_ERR(current_ev))
2343 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002344
Johannes Berg77965c972009-02-18 18:45:06 +01002345 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01002346 memset(&iwe, 0, sizeof(iwe));
2347 iwe.cmd = IWEVQUAL;
2348 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2349 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01002350 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c972009-02-18 18:45:06 +01002351 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002352 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01002353 sig = bss->pub.signal / 100;
2354 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01002355 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01002356 if (sig < -110) /* rather bad */
2357 sig = -110;
2358 else if (sig > -40) /* perfect */
2359 sig = -40;
2360 /* will give a range of 0 .. 70 */
2361 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01002362 break;
2363 case CFG80211_SIGNAL_TYPE_UNSPEC:
2364 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01002365 /* will give range 0 .. 100 */
2366 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01002367 break;
2368 default:
2369 /* not reached */
2370 break;
2371 }
James Minor76a70e92015-02-24 12:58:20 -06002372 current_ev = iwe_stream_add_event_check(info, current_ev,
2373 end_buf, &iwe,
2374 IW_EV_QUAL_LEN);
2375 if (IS_ERR(current_ev))
2376 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002377 }
2378
2379 memset(&iwe, 0, sizeof(iwe));
2380 iwe.cmd = SIOCGIWENCODE;
2381 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2382 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2383 else
2384 iwe.u.data.flags = IW_ENCODE_DISABLED;
2385 iwe.u.data.length = 0;
James Minor76a70e92015-02-24 12:58:20 -06002386 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2387 &iwe, "");
2388 if (IS_ERR(current_ev))
2389 return current_ev;
Johannes Berg2a519312009-02-10 21:25:55 +01002390
Johannes Berg9caf0362012-11-29 01:25:20 +01002391 rcu_read_lock();
2392 ies = rcu_dereference(bss->pub.ies);
Johannes Berg83c7aa12013-02-05 16:51:29 +01002393 rem = ies->len;
2394 ie = ies->data;
Johannes Berg9caf0362012-11-29 01:25:20 +01002395
Johannes Berg83c7aa12013-02-05 16:51:29 +01002396 while (rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01002397 /* invalid data */
2398 if (ie[1] > rem - 2)
2399 break;
2400
2401 switch (ie[0]) {
2402 case WLAN_EID_SSID:
2403 memset(&iwe, 0, sizeof(iwe));
2404 iwe.cmd = SIOCGIWESSID;
2405 iwe.u.data.length = ie[1];
2406 iwe.u.data.flags = 1;
James Minor76a70e92015-02-24 12:58:20 -06002407 current_ev = iwe_stream_add_point_check(info,
2408 current_ev,
2409 end_buf, &iwe,
2410 (u8 *)ie + 2);
2411 if (IS_ERR(current_ev))
2412 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002413 break;
2414 case WLAN_EID_MESH_ID:
2415 memset(&iwe, 0, sizeof(iwe));
2416 iwe.cmd = SIOCGIWESSID;
2417 iwe.u.data.length = ie[1];
2418 iwe.u.data.flags = 1;
James Minor76a70e92015-02-24 12:58:20 -06002419 current_ev = iwe_stream_add_point_check(info,
2420 current_ev,
2421 end_buf, &iwe,
2422 (u8 *)ie + 2);
2423 if (IS_ERR(current_ev))
2424 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002425 break;
2426 case WLAN_EID_MESH_CONFIG:
2427 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00002428 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01002429 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01002430 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01002431 memset(&iwe, 0, sizeof(iwe));
2432 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002433 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2434 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01002435 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002436 current_ev = iwe_stream_add_point_check(info,
2437 current_ev,
2438 end_buf,
2439 &iwe, buf);
2440 if (IS_ERR(current_ev))
2441 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002442 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2443 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01002444 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002445 current_ev = iwe_stream_add_point_check(info,
2446 current_ev,
2447 end_buf,
2448 &iwe, buf);
2449 if (IS_ERR(current_ev))
2450 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002451 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2452 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01002453 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002454 current_ev = iwe_stream_add_point_check(info,
2455 current_ev,
2456 end_buf,
2457 &iwe, buf);
2458 if (IS_ERR(current_ev))
2459 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002460 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01002461 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002462 current_ev = iwe_stream_add_point_check(info,
2463 current_ev,
2464 end_buf,
2465 &iwe, buf);
2466 if (IS_ERR(current_ev))
2467 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002468 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2469 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002470 current_ev = iwe_stream_add_point_check(info,
2471 current_ev,
2472 end_buf,
2473 &iwe, buf);
2474 if (IS_ERR(current_ev))
2475 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002476 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2477 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002478 current_ev = iwe_stream_add_point_check(info,
2479 current_ev,
2480 end_buf,
2481 &iwe, buf);
2482 if (IS_ERR(current_ev))
2483 goto unlock;
Rui Paulo76aa5e72009-11-18 18:22:59 +00002484 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01002485 iwe.u.data.length = strlen(buf);
James Minor76a70e92015-02-24 12:58:20 -06002486 current_ev = iwe_stream_add_point_check(info,
2487 current_ev,
2488 end_buf,
2489 &iwe, buf);
2490 if (IS_ERR(current_ev))
2491 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002492 break;
2493 case WLAN_EID_SUPP_RATES:
2494 case WLAN_EID_EXT_SUPP_RATES:
2495 /* display all supported rates in readable format */
2496 p = current_ev + iwe_stream_lcp_len(info);
2497
2498 memset(&iwe, 0, sizeof(iwe));
2499 iwe.cmd = SIOCGIWRATE;
2500 /* Those two flags are ignored... */
2501 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2502
2503 for (i = 0; i < ie[1]; i++) {
2504 iwe.u.bitrate.value =
2505 ((ie[i + 2] & 0x7f) * 500000);
James Minor76a70e92015-02-24 12:58:20 -06002506 tmp = p;
Johannes Berg2a519312009-02-10 21:25:55 +01002507 p = iwe_stream_add_value(info, current_ev, p,
James Minor76a70e92015-02-24 12:58:20 -06002508 end_buf, &iwe,
2509 IW_EV_PARAM_LEN);
2510 if (p == tmp) {
2511 current_ev = ERR_PTR(-E2BIG);
2512 goto unlock;
2513 }
Johannes Berg2a519312009-02-10 21:25:55 +01002514 }
2515 current_ev = p;
2516 break;
2517 }
2518 rem -= ie[1] + 2;
2519 ie += ie[1] + 2;
2520 }
2521
Joe Perchesf64f9e72009-11-29 16:55:45 -08002522 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2523 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01002524 memset(&iwe, 0, sizeof(iwe));
2525 iwe.cmd = SIOCGIWMODE;
2526 if (ismesh)
2527 iwe.u.mode = IW_MODE_MESH;
2528 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2529 iwe.u.mode = IW_MODE_MASTER;
2530 else
2531 iwe.u.mode = IW_MODE_ADHOC;
James Minor76a70e92015-02-24 12:58:20 -06002532 current_ev = iwe_stream_add_event_check(info, current_ev,
2533 end_buf, &iwe,
2534 IW_EV_UINT_LEN);
2535 if (IS_ERR(current_ev))
2536 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002537 }
2538
James Minor76a70e92015-02-24 12:58:20 -06002539 memset(&iwe, 0, sizeof(iwe));
2540 iwe.cmd = IWEVCUSTOM;
2541 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2542 iwe.u.data.length = strlen(buf);
2543 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2544 &iwe, buf);
2545 if (IS_ERR(current_ev))
2546 goto unlock;
2547 memset(&iwe, 0, sizeof(iwe));
2548 iwe.cmd = IWEVCUSTOM;
2549 sprintf(buf, " Last beacon: %ums ago",
2550 elapsed_jiffies_msecs(bss->ts));
2551 iwe.u.data.length = strlen(buf);
2552 current_ev = iwe_stream_add_point_check(info, current_ev,
2553 end_buf, &iwe, buf);
2554 if (IS_ERR(current_ev))
2555 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01002556
James Minor76a70e92015-02-24 12:58:20 -06002557 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2558
2559 unlock:
Johannes Berg9caf0362012-11-29 01:25:20 +01002560 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01002561 return current_ev;
2562}
2563
2564
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002565static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
Johannes Berg2a519312009-02-10 21:25:55 +01002566 struct iw_request_info *info,
2567 char *buf, size_t len)
2568{
2569 char *current_ev = buf;
2570 char *end_buf = buf + len;
2571 struct cfg80211_internal_bss *bss;
James Minor76a70e92015-02-24 12:58:20 -06002572 int err = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01002573
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002574 spin_lock_bh(&rdev->bss_lock);
2575 cfg80211_bss_expire(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01002576
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002577 list_for_each_entry(bss, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01002578 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
James Minor76a70e92015-02-24 12:58:20 -06002579 err = -E2BIG;
2580 break;
Johannes Berg2a519312009-02-10 21:25:55 +01002581 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002582 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
Johannes Berg77965c972009-02-18 18:45:06 +01002583 current_ev, end_buf);
James Minor76a70e92015-02-24 12:58:20 -06002584 if (IS_ERR(current_ev)) {
2585 err = PTR_ERR(current_ev);
2586 break;
2587 }
Johannes Berg2a519312009-02-10 21:25:55 +01002588 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002589 spin_unlock_bh(&rdev->bss_lock);
James Minor76a70e92015-02-24 12:58:20 -06002590
2591 if (err)
2592 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01002593 return current_ev - buf;
2594}
2595
2596
2597int cfg80211_wext_giwscan(struct net_device *dev,
2598 struct iw_request_info *info,
2599 struct iw_point *data, char *extra)
2600{
2601 struct cfg80211_registered_device *rdev;
2602 int res;
2603
2604 if (!netif_running(dev))
2605 return -ENETDOWN;
2606
Johannes Berg463d0182009-07-14 00:33:35 +02002607 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01002608
2609 if (IS_ERR(rdev))
2610 return PTR_ERR(rdev);
2611
Johannes Bergf9d15d12014-01-22 11:14:19 +02002612 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg5fe231e2013-05-08 21:45:15 +02002613 return -EAGAIN;
Johannes Berg2a519312009-02-10 21:25:55 +01002614
2615 res = ieee80211_scan_results(rdev, info, extra, data->length);
2616 data->length = 0;
2617 if (res >= 0) {
2618 data->length = res;
2619 res = 0;
2620 }
2621
Johannes Berg2a519312009-02-10 21:25:55 +01002622 return res;
2623}
Johannes Berg2afe38d2015-01-06 14:00:53 +01002624EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01002625#endif