net: wireless: Convert to use the preferred fallthrough macro
[linux-block.git] / net / wireless / scan.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
2a519311
JB
2/*
3 * cfg80211 scan result handling
4 *
5 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
2740f0cf 6 * Copyright 2013-2014 Intel Mobile Communications GmbH
1d76250b 7 * Copyright 2016 Intel Deutschland GmbH
0b8fb823 8 * Copyright (C) 2018-2019 Intel Corporation
2a519311
JB
9 */
10#include <linux/kernel.h>
5a0e3ad6 11#include <linux/slab.h>
2a519311
JB
12#include <linux/module.h>
13#include <linux/netdevice.h>
14#include <linux/wireless.h>
15#include <linux/nl80211.h>
16#include <linux/etherdevice.h>
17#include <net/arp.h>
18#include <net/cfg80211.h>
262eb9b2 19#include <net/cfg80211-wext.h>
2a519311
JB
20#include <net/iw_handler.h>
21#include "core.h"
22#include "nl80211.h"
a9a11622 23#include "wext-compat.h"
e35e4d28 24#include "rdev-ops.h"
2a519311 25
776b3580
JB
26/**
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
8cf5c86d 58 * data stored in the BSS struct, since the beacon IEs are
776b3580
JB
59 * also linked into the probe response struct.
60 */
61
9853a55e
JB
62/*
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
f9616e0f 75#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
2a519311 76
776b3580 77static void bss_free(struct cfg80211_internal_bss *bss)
e8e27c66 78{
9caf0364 79 struct cfg80211_bss_ies *ies;
b629ea3d
JB
80
81 if (WARN_ON(atomic_read(&bss->hold)))
82 return;
83
9caf0364 84 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
776b3580 85 if (ies && !bss->pub.hidden_beacon_bss)
9caf0364
JB
86 kfree_rcu(ies, rcu_head);
87 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
88 if (ies)
89 kfree_rcu(ies, rcu_head);
e8e27c66 90
776b3580
JB
91 /*
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
e8e27c66
AK
98 kfree(bss);
99}
100
1b8ec87a 101static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
776b3580 102 struct cfg80211_internal_bss *bss)
0532d4f1 103{
1b8ec87a 104 lockdep_assert_held(&rdev->bss_lock);
776b3580
JB
105
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 }
7011ba58
SS
113 if (bss->pub.transmitted_bss) {
114 bss = container_of(bss->pub.transmitted_bss,
a3584f56
SS
115 struct cfg80211_internal_bss,
116 pub);
117 bss->refcount++;
118 }
0532d4f1
JB
119}
120
1b8ec87a 121static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
776b3580 122 struct cfg80211_internal_bss *bss)
0532d4f1 123{
1b8ec87a 124 lockdep_assert_held(&rdev->bss_lock);
776b3580
JB
125
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 }
a3584f56 135
7011ba58 136 if (bss->pub.transmitted_bss) {
a3584f56
SS
137 struct cfg80211_internal_bss *tbss;
138
7011ba58 139 tbss = container_of(bss->pub.transmitted_bss,
a3584f56
SS
140 struct cfg80211_internal_bss,
141 pub);
142 tbss->refcount--;
143 if (tbss->refcount == 0)
144 bss_free(tbss);
145 }
146
776b3580
JB
147 bss->refcount--;
148 if (bss->refcount == 0)
149 bss_free(bss);
0532d4f1
JB
150}
151
1b8ec87a 152static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
e8e27c66
AK
153 struct cfg80211_internal_bss *bss)
154{
1b8ec87a 155 lockdep_assert_held(&rdev->bss_lock);
4b1af479 156
776b3580
JB
157 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
e8e27c66 171 list_del_init(&bss->list);
7011ba58 172 list_del_init(&bss->pub.nontrans_list);
1b8ec87a 173 rb_erase(&bss->rbn, &rdev->bss_tree);
9853a55e
JB
174 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));
1b8ec87a 178 bss_ref_put(rdev, bss);
776b3580 179 return true;
e8e27c66
AK
180}
181
f7dacfb1
SS
182bool 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
0b8fb823
PX
232static 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;
f7dacfb1 238 const struct element *non_inherit_elem;
0b8fb823
PX
239 u8 *sub_copy;
240
241 /* copy subelement as we need to change its content to
242 * mark an ie after it is processed.
243 */
90abf96a 244 sub_copy = kmemdup(subelement, subie_len, gfp);
0b8fb823
PX
245 if (!sub_copy)
246 return 0;
0b8fb823
PX
247
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
f7dacfb1
SS
257 /* 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
0b8fb823
PX
262 /* 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
c17fe043
SS
274 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
0b8fb823 281 if (!tmp) {
f7dacfb1
SS
282 const struct element *old_elem = (void *)tmp_old;
283
0b8fb823 284 /* ie in old ie but not in subelement */
f7dacfb1
SS
285 if (cfg80211_is_element_inherited(old_elem,
286 non_inherit_elem)) {
0b8fb823
PX
287 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
c17fe043
SS
293 * as copied (by setting eid field to WLAN_EID_SSID,
294 * which is skipped anyway).
295 * For vendor ie, compare OUI + type + subType to
0b8fb823
PX
296 * 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;
c17fe043 305 tmp[0] = WLAN_EID_SSID;
0b8fb823
PX
306 } 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;
c17fe043 314 tmp[0] = WLAN_EID_SSID;
0b8fb823
PX
315 }
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 ||
5bd9d108 330 tmp_new[0] == WLAN_EID_SSID)) {
0b8fb823
PX
331 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
7011ba58
SS
367cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
368 struct cfg80211_bss *nontrans_bss)
0b8fb823
PX
369{
370 const u8 *ssid;
371 size_t ssid_len;
7011ba58 372 struct cfg80211_bss *bss = NULL;
0b8fb823
PX
373
374 rcu_read_lock();
7011ba58 375 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
0b8fb823
PX
376 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) {
7011ba58 386 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
0b8fb823
PX
387 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
1b8ec87a 395static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
15d6030b
SL
396 unsigned long expire_time)
397{
398 struct cfg80211_internal_bss *bss, *tmp;
399 bool expired = false;
400
1b8ec87a 401 lockdep_assert_held(&rdev->bss_lock);
4b1af479 402
1b8ec87a 403 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
15d6030b
SL
404 if (atomic_read(&bss->hold))
405 continue;
406 if (!time_after(expire_time, bss->ts))
407 continue;
408
1b8ec87a 409 if (__cfg80211_unlink_bss(rdev, bss))
776b3580 410 expired = true;
15d6030b
SL
411 }
412
413 if (expired)
1b8ec87a 414 rdev->bss_generation++;
15d6030b
SL
415}
416
9853a55e
JB
417static 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
f9d15d16
JB
451void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
452 bool send_message)
2a519311 453{
667503dd 454 struct cfg80211_scan_request *request;
fd014284 455 struct wireless_dev *wdev;
f9d15d16 456 struct sk_buff *msg;
3d23e349 457#ifdef CONFIG_CFG80211_WEXT
2a519311
JB
458 union iwreq_data wrqu;
459#endif
460
5fe231e8 461 ASSERT_RTNL();
01a0ac41 462
f9d15d16 463 if (rdev->scan_msg) {
505a2e88 464 nl80211_send_scan_msg(rdev, rdev->scan_msg);
f9d15d16
JB
465 rdev->scan_msg = NULL;
466 return;
467 }
667503dd 468
f9d15d16 469 request = rdev->scan_req;
01a0ac41
JB
470 if (!request)
471 return;
472
fd014284 473 wdev = request->wdev;
2a519311 474
6829c878
JB
475 /*
476 * This must be before sending the other events!
477 * Otherwise, wpa_supplicant gets completely confused with
478 * wext events.
479 */
fd014284
JB
480 if (wdev->netdev)
481 cfg80211_sme_scan_done(wdev->netdev);
6829c878 482
1d76250b 483 if (!request->info.aborted &&
f9d15d16
JB
484 request->flags & NL80211_SCAN_FLAG_FLUSH) {
485 /* flush entries from previous scans */
486 spin_lock_bh(&rdev->bss_lock);
487 __cfg80211_bss_expire(rdev, request->scan_start);
488 spin_unlock_bh(&rdev->bss_lock);
15d6030b 489 }
2a519311 490
1d76250b 491 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
f9d15d16 492
3d23e349 493#ifdef CONFIG_CFG80211_WEXT
1d76250b 494 if (wdev->netdev && !request->info.aborted) {
2a519311
JB
495 memset(&wrqu, 0, sizeof(wrqu));
496
fd014284 497 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
2a519311
JB
498 }
499#endif
500
fd014284
JB
501 if (wdev->netdev)
502 dev_put(wdev->netdev);
2a519311 503
36e6fea8 504 rdev->scan_req = NULL;
4a58e7c3 505 kfree(request);
f9d15d16
JB
506
507 if (!send_message)
508 rdev->scan_msg = msg;
509 else
505a2e88 510 nl80211_send_scan_msg(rdev, msg);
2a519311 511}
667503dd 512
36e6fea8
JB
513void __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
5fe231e8 520 rtnl_lock();
f9d15d16 521 ___cfg80211_scan_done(rdev, true);
5fe231e8 522 rtnl_unlock();
36e6fea8
JB
523}
524
1d76250b
AS
525void cfg80211_scan_done(struct cfg80211_scan_request *request,
526 struct cfg80211_scan_info *info)
667503dd 527{
1d76250b 528 trace_cfg80211_scan_done(request, info);
f26cbf40 529 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
667503dd 530
1d76250b 531 request->info = *info;
5fe231e8 532 request->notified = true;
f26cbf40 533 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
667503dd 534}
2a519311
JB
535EXPORT_SYMBOL(cfg80211_scan_done);
536
ca986ad9
AVS
537void 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
3ee9306b
AG
559 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
560 lockdep_rtnl_is_held()) {
ca986ad9
AVS
561 if (pos->reqid == reqid)
562 return pos;
563 }
b34939b9 564 return NULL;
ca986ad9
AVS
565}
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
b34939b9 600void cfg80211_sched_scan_results_wk(struct work_struct *work)
807f8a8c
LC
601{
602 struct cfg80211_registered_device *rdev;
b34939b9 603 struct cfg80211_sched_scan_request *req, *tmp;
807f8a8c 604
b34939b9
AVS
605 rdev = container_of(work, struct cfg80211_registered_device,
606 sched_scan_res_wk);
807f8a8c 607
5fe231e8 608 rtnl_lock();
b34939b9
AVS
609 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);
15d6030b 621 }
15d6030b 622 }
5fe231e8 623 rtnl_unlock();
807f8a8c
LC
624}
625
b34939b9 626void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
807f8a8c 627{
ca986ad9
AVS
628 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
629 struct cfg80211_sched_scan_request *request;
630
b34939b9 631 trace_cfg80211_sched_scan_results(wiphy, reqid);
807f8a8c 632 /* ignore if we're not scanning */
31a60ed1 633
1b57b621 634 rcu_read_lock();
b34939b9
AVS
635 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 }
1b57b621 640 rcu_read_unlock();
807f8a8c
LC
641}
642EXPORT_SYMBOL(cfg80211_sched_scan_results);
643
b34939b9 644void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
807f8a8c 645{
f26cbf40 646 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
807f8a8c 647
792e6aa7
EP
648 ASSERT_RTNL();
649
b34939b9 650 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
4ee3e063 651
b34939b9 652 __cfg80211_stop_sched_scan(rdev, reqid, true);
792e6aa7
EP
653}
654EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
655
b34939b9 656void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
792e6aa7
EP
657{
658 rtnl_lock();
b34939b9 659 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
5fe231e8 660 rtnl_unlock();
807f8a8c 661}
807f8a8c
LC
662EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
663
ca986ad9
AVS
664int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
665 struct cfg80211_sched_scan_request *req,
666 bool driver_initiated)
807f8a8c 667{
5fe231e8 668 ASSERT_RTNL();
807f8a8c 669
85a9994a 670 if (!driver_initiated) {
3a3ecf1d 671 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
85a9994a
LC
672 if (err)
673 return err;
674 }
807f8a8c 675
ca986ad9 676 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
807f8a8c 677
ca986ad9 678 cfg80211_del_sched_scan_req(rdev, req);
807f8a8c 679
3b4670ff 680 return 0;
807f8a8c
LC
681}
682
ca986ad9
AVS
683int __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);
b34939b9
AVS
691 if (!sched_scan_req)
692 return -ENOENT;
ca986ad9
AVS
693
694 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
695 driver_initiated);
696}
697
1b8ec87a 698void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
cb3a8eec
DW
699 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
1b8ec87a
ZG
704 spin_lock_bh(&rdev->bss_lock);
705 list_for_each_entry(bss, &rdev->bss_list, list)
cb3a8eec 706 bss->ts -= age_jiffies;
1b8ec87a 707 spin_unlock_bh(&rdev->bss_lock);
cb3a8eec
DW
708}
709
1b8ec87a 710void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
2a519311 711{
1b8ec87a 712 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
2a519311
JB
713}
714
2f1805ea
EG
715void 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
49a68e0d
JB
725const 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)
2a519311 729{
0f3b07f0
JB
730 const struct element *elem;
731
0f3b07f0 732 for_each_element_id(elem, eid, ies, len) {
49a68e0d
JB
733 if (elem->datalen >= match_offset + match_len &&
734 !memcmp(elem->data + match_offset, match, match_len))
735 return elem;
2a519311 736 }
fbd05e4a
LC
737
738 return NULL;
2a519311 739}
49a68e0d 740EXPORT_SYMBOL(cfg80211_find_elem_match);
2a519311 741
49a68e0d
JB
742const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
743 const u8 *ies,
744 unsigned int len)
0c28ec58 745{
49a68e0d 746 const struct element *elem;
fbd05e4a
LC
747 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
748 int match_len = (oui_type < 0) ? 3 : sizeof(match);
0c28ec58 749
9e9ea439
EG
750 if (WARN_ON(oui_type > 0xff))
751 return NULL;
752
49a68e0d
JB
753 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
754 match, match_len, 0);
6719429d 755
49a68e0d 756 if (!elem || elem->datalen < 4)
fbd05e4a 757 return NULL;
6719429d 758
49a68e0d 759 return elem;
0c28ec58 760}
49a68e0d 761EXPORT_SYMBOL(cfg80211_find_vendor_elem);
0c28ec58 762
4593c4cb
JB
763/**
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
dd9dfb9f 775static int cmp_bss(struct cfg80211_bss *a,
5622f5bb 776 struct cfg80211_bss *b,
4593c4cb 777 enum bss_compare_mode mode)
dd9dfb9f 778{
9caf0364 779 const struct cfg80211_bss_ies *a_ies, *b_ies;
3af6341c
JB
780 const u8 *ie1 = NULL;
781 const u8 *ie2 = NULL;
5622f5bb 782 int i, r;
dd9dfb9f 783
3af6341c
JB
784 if (a->channel != b->channel)
785 return b->channel->center_freq - a->channel->center_freq;
dd9dfb9f 786
9caf0364
JB
787 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
3af6341c
JB
794 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
3af6341c
JB
821 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
822 if (r)
823 return r;
824
9caf0364
JB
825 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);
dd9dfb9f 827
5622f5bb
JB
828 if (!ie1 && !ie2)
829 return 0;
830
f94f8b16 831 /*
5622f5bb
JB
832 * 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").
f94f8b16 835 */
dd9dfb9f
DT
836
837 /* sort missing IE before (left of) present IE */
838 if (!ie1)
839 return -1;
840 if (!ie2)
841 return 1;
842
4593c4cb
JB
843 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];
5622f5bb 863 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
4593c4cb
JB
864 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 }
dd9dfb9f
DT
873}
874
6eb18137 875static bool cfg80211_bss_type_match(u16 capability,
57fbcce3 876 enum nl80211_band band,
6eb18137
DL
877 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
57fbcce3 885 if (band == NL80211_BAND_60GHZ) {
6eb18137
DL
886 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
0e3a39b5 921/* Returned bss is reference counted and must be cleaned up appropriately. */
2a519311
JB
922struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
923 struct ieee80211_channel *channel,
924 const u8 *bssid,
79420f09 925 const u8 *ssid, size_t ssid_len,
6eb18137
DL
926 enum ieee80211_bss_type bss_type,
927 enum ieee80211_privacy privacy)
2a519311 928{
f26cbf40 929 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2a519311 930 struct cfg80211_internal_bss *bss, *res = NULL;
ccb6c136 931 unsigned long now = jiffies;
6eb18137 932 int bss_privacy;
2a519311 933
6eb18137
DL
934 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
935 privacy);
4ee3e063 936
1b8ec87a 937 spin_lock_bh(&rdev->bss_lock);
2a519311 938
1b8ec87a 939 list_for_each_entry(bss, &rdev->bss_list, list) {
6eb18137
DL
940 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))
79420f09 947 continue;
2a519311
JB
948 if (channel && bss->pub.channel != channel)
949 continue;
c14a7400
JB
950 if (!is_valid_ether_addr(bss->pub.bssid))
951 continue;
ccb6c136
JB
952 /* Don't get expired BSS structs */
953 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
954 !atomic_read(&bss->hold))
955 continue;
2a519311
JB
956 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
957 res = bss;
1b8ec87a 958 bss_ref_get(rdev, res);
2a519311
JB
959 break;
960 }
961 }
962
1b8ec87a 963 spin_unlock_bh(&rdev->bss_lock);
2a519311
JB
964 if (!res)
965 return NULL;
4ee3e063 966 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
967 return &res->pub;
968}
969EXPORT_SYMBOL(cfg80211_get_bss);
970
1b8ec87a 971static void rb_insert_bss(struct cfg80211_registered_device *rdev,
2a519311
JB
972 struct cfg80211_internal_bss *bss)
973{
1b8ec87a 974 struct rb_node **p = &rdev->bss_tree.rb_node;
2a519311
JB
975 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
4593c4cb 983 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
2a519311
JB
984
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);
1b8ec87a 997 rb_insert_color(&bss->rbn, &rdev->bss_tree);
2a519311
JB
998}
999
1000static struct cfg80211_internal_bss *
1b8ec87a 1001rb_find_bss(struct cfg80211_registered_device *rdev,
5622f5bb 1002 struct cfg80211_internal_bss *res,
4593c4cb 1003 enum bss_compare_mode mode)
dd9dfb9f 1004{
1b8ec87a 1005 struct rb_node *n = rdev->bss_tree.rb_node;
dd9dfb9f
DT
1006 struct cfg80211_internal_bss *bss;
1007 int r;
1008
1009 while (n) {
1010 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
4593c4cb 1011 r = cmp_bss(&res->pub, &bss->pub, mode);
dd9dfb9f
DT
1012
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
1b8ec87a 1024static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
776b3580 1025 struct cfg80211_internal_bss *new)
dd9dfb9f 1026{
9caf0364 1027 const struct cfg80211_bss_ies *ies;
776b3580
JB
1028 struct cfg80211_internal_bss *bss;
1029 const u8 *ie;
1030 int i, ssidlen;
1031 u8 fold = 0;
9853a55e 1032 u32 n_entries = 0;
9caf0364 1033
776b3580 1034 ies = rcu_access_pointer(new->pub.beacon_ies);
9caf0364 1035 if (WARN_ON(!ies))
776b3580 1036 return false;
dd9dfb9f 1037
776b3580
JB
1038 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
1b8ec87a 1055 list_for_each_entry(bss, &rdev->bss_list, list) {
9853a55e
JB
1056 /*
1057 * we're iterating all the entries anyway, so take the
1058 * opportunity to validate the list length accounting
1059 */
1060 n_entries++;
1061
776b3580
JB
1062 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1063 continue;
1064 if (bss->pub.channel != new->pub.channel)
1065 continue;
dcd6eac1
SW
1066 if (bss->pub.scan_width != new->pub.scan_width)
1067 continue;
776b3580
JB
1068 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;
776b3580
JB
1078 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
9853a55e
JB
1090 WARN_ONCE(n_entries != rdev->bss_entries,
1091 "rdev bss entries[%d]/list[len:%d] corruption\n",
1092 rdev->bss_entries, n_entries);
1093
776b3580 1094 return true;
dd9dfb9f
DT
1095}
1096
0cd01efb
SS
1097struct cfg80211_non_tx_bss {
1098 struct cfg80211_bss *tx_bss;
1099 u8 max_bssid_indicator;
1100 u8 bssid_index;
1101};
1102
3ab8227d
SM
1103static 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
0e3a39b5 1190/* Returned bss is reference counted and must be cleaned up appropriately. */
a3ce17d1 1191struct cfg80211_internal_bss *
1b8ec87a 1192cfg80211_bss_update(struct cfg80211_registered_device *rdev,
3afc2167 1193 struct cfg80211_internal_bss *tmp,
a3ce17d1 1194 bool signal_valid, unsigned long ts)
2a519311
JB
1195{
1196 struct cfg80211_internal_bss *found = NULL;
2a519311 1197
9caf0364 1198 if (WARN_ON(!tmp->pub.channel))
2a519311 1199 return NULL;
2a519311 1200
a3ce17d1 1201 tmp->ts = ts;
2a519311 1202
1b8ec87a 1203 spin_lock_bh(&rdev->bss_lock);
2a519311 1204
9caf0364 1205 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1b8ec87a 1206 spin_unlock_bh(&rdev->bss_lock);
9caf0364
JB
1207 return NULL;
1208 }
1209
1b8ec87a 1210 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
2a519311 1211
cd1658f5 1212 if (found) {
3ab8227d
SM
1213 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1214 goto drop;
2a519311 1215 } else {
9caf0364 1216 struct cfg80211_internal_bss *new;
dd9dfb9f 1217 struct cfg80211_internal_bss *hidden;
9caf0364 1218 struct cfg80211_bss_ies *ies;
dd9dfb9f 1219
9caf0364
JB
1220 /*
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 */
1b8ec87a 1225 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
9caf0364
JB
1226 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);
776b3580 1234 goto drop;
9caf0364
JB
1235 }
1236 memcpy(new, tmp, sizeof(*new));
776b3580
JB
1237 new->refcount = 1;
1238 INIT_LIST_HEAD(&new->hidden_list);
7011ba58 1239 INIT_LIST_HEAD(&new->pub.nontrans_list);
776b3580
JB
1240
1241 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1b8ec87a 1242 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
776b3580 1243 if (!hidden)
1b8ec87a 1244 hidden = rb_find_bss(rdev, tmp,
776b3580
JB
1245 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 */
1b8ec87a 1261 if (!cfg80211_combine_bsses(rdev, new)) {
776b3580
JB
1262 kfree(new);
1263 goto drop;
1264 }
1265 }
1266
9853a55e
JB
1267 if (rdev->bss_entries >= bss_entries_limit &&
1268 !cfg80211_bss_expire_oldest(rdev)) {
1269 kfree(new);
1270 goto drop;
1271 }
1272
a3584f56 1273 /* This must be before the call to bss_ref_get */
0cd01efb 1274 if (tmp->pub.transmitted_bss) {
a3584f56 1275 struct cfg80211_internal_bss *pbss =
0cd01efb 1276 container_of(tmp->pub.transmitted_bss,
a3584f56
SS
1277 struct cfg80211_internal_bss,
1278 pub);
1279
0cd01efb 1280 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
a3584f56
SS
1281 bss_ref_get(rdev, pbss);
1282 }
1283
1b8ec87a 1284 list_add_tail(&new->list, &rdev->bss_list);
9853a55e 1285 rdev->bss_entries++;
1b8ec87a 1286 rb_insert_bss(rdev, new);
9caf0364 1287 found = new;
2a519311
JB
1288 }
1289
1b8ec87a
ZG
1290 rdev->bss_generation++;
1291 bss_ref_get(rdev, found);
1292 spin_unlock_bh(&rdev->bss_lock);
2a519311 1293
2a519311 1294 return found;
776b3580 1295 drop:
1b8ec87a 1296 spin_unlock_bh(&rdev->bss_lock);
776b3580 1297 return NULL;
2a519311
JB
1298}
1299
119f94a6
JM
1300/*
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 */
0172bb75
JB
1308static struct ieee80211_channel *
1309cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
119f94a6
JM
1310 struct ieee80211_channel *channel,
1311 enum nl80211_bss_scan_width scan_width)
0172bb75
JB
1312{
1313 const u8 *tmp;
1314 u32 freq;
1315 int channel_number = -1;
119f94a6 1316 struct ieee80211_channel *alt_channel;
0172bb75
JB
1317
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
119f94a6
JM
1330 if (channel_number < 0) {
1331 /* No channel information in frame payload */
0172bb75 1332 return channel;
119f94a6 1333 }
0172bb75 1334
934f4c7d
TP
1335 freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1336 alt_channel = ieee80211_get_channel_khz(wiphy, freq);
119f94a6
JM
1337 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)
0172bb75 1367 return NULL;
119f94a6 1368 return alt_channel;
0172bb75
JB
1369}
1370
0e3a39b5 1371/* Returned bss is reference counted and must be cleaned up appropriately. */
0b8fb823
PX
1372static 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,
0cd01efb 1378 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823 1379 gfp_t gfp)
06aa7afa 1380{
0b8fb823 1381 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9caf0364 1382 struct cfg80211_bss_ies *ies;
3afc2167 1383 struct ieee80211_channel *channel;
7011ba58 1384 struct cfg80211_internal_bss tmp = {}, *res;
6eb18137 1385 int bss_type;
67af9811 1386 bool signal_valid;
60d7dfea 1387 unsigned long ts;
06aa7afa
JK
1388
1389 if (WARN_ON(!wiphy))
1390 return NULL;
1391
22fe88d3 1392 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
6e19bc4b 1393 (data->signal < 0 || data->signal > 100)))
06aa7afa
JK
1394 return NULL;
1395
119f94a6
JM
1396 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1397 data->scan_width);
0172bb75
JB
1398 if (!channel)
1399 return NULL;
1400
9caf0364
JB
1401 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1402 tmp.pub.channel = channel;
6e19bc4b
DS
1403 tmp.pub.scan_width = data->scan_width;
1404 tmp.pub.signal = data->signal;
9caf0364
JB
1405 tmp.pub.beacon_interval = beacon_interval;
1406 tmp.pub.capability = capability;
6e19bc4b 1407 tmp.ts_boottime = data->boottime_ns;
0cd01efb
SS
1408 if (non_tx_data) {
1409 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
60d7dfea 1410 ts = bss_from_pub(non_tx_data->tx_bss)->ts;
0cd01efb
SS
1411 tmp.pub.bssid_index = non_tx_data->bssid_index;
1412 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
60d7dfea
JB
1413 } else {
1414 ts = jiffies;
0cd01efb 1415 }
6e19bc4b 1416
34a6eddb 1417 /*
5bc8c1f2 1418 * If we do not know here whether the IEs are from a Beacon or Probe
34a6eddb
JM
1419 * 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
50521aa8 1422 * override the IEs pointer should we have received an earlier
9caf0364 1423 * indication of Probe Response data.
34a6eddb 1424 */
0e227084 1425 ies = kzalloc(sizeof(*ies) + ielen, gfp);
9caf0364
JB
1426 if (!ies)
1427 return NULL;
1428 ies->len = ielen;
8cef2c9d 1429 ies->tsf = tsf;
0e227084 1430 ies->from_beacon = false;
9caf0364 1431 memcpy(ies->data, ie, ielen);
06aa7afa 1432
5bc8c1f2
JB
1433 switch (ftype) {
1434 case CFG80211_BSS_FTYPE_BEACON:
1435 ies->from_beacon = true;
7b506ff6 1436 fallthrough;
5bc8c1f2
JB
1437 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 }
9caf0364 1444 rcu_assign_pointer(tmp.pub.ies, ies);
06aa7afa 1445
7bb106eb 1446 signal_valid = data->chan == channel;
60d7dfea 1447 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
06aa7afa
JK
1448 if (!res)
1449 return NULL;
1450
57fbcce3 1451 if (channel->band == NL80211_BAND_60GHZ) {
6eb18137
DL
1452 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 }
06aa7afa 1460
b0d1d7ff 1461 if (non_tx_data) {
0b8fb823
PX
1462 /* this is a nontransmitting bss, we need to add it to
1463 * transmitting bss' list if it is not there
1464 */
0cd01efb
SS
1465 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1466 &res->pub)) {
0b8fb823
PX
1467 if (__cfg80211_unlink_bss(rdev, res))
1468 rdev->bss_generation++;
1469 }
1470 }
1471
4ee3e063 1472 trace_cfg80211_return_bss(&res->pub);
06aa7afa
JK
1473 /* cfg80211_bss_update gives us a referenced result */
1474 return &res->pub;
1475}
06aa7afa 1476
fe806e49
SS
1477static 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 /*
8cf5c86d 1491 * If it is not the last subelement in current MBSSID IE or there isn't
fe806e49
SS
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,
5809a5d5 1523 u8 *merged_ie, size_t max_copy_len)
fe806e49
SS
1524{
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
5809a5d5 1531 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
fe806e49
SS
1532
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;
5809a5d5 1540 memcpy(merged_ie + copied_len, next_sub->data,
fe806e49
SS
1541 next_sub->datalen);
1542 copied_len += next_sub->datalen;
1543 }
1544
1545 return copied_len;
1546}
1547EXPORT_SYMBOL(cfg80211_merge_profile);
1548
0b8fb823
PX
1549static 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,
0cd01efb 1555 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823
PX
1556 gfp_t gfp)
1557{
1c8745f3
JB
1558 const u8 *mbssid_index_ie;
1559 const struct element *elem, *sub;
1560 size_t new_ie_len;
0b8fb823 1561 u8 new_bssid[ETH_ALEN];
fe806e49
SS
1562 u8 *new_ie, *profile;
1563 u64 seen_indices = 0;
0b8fb823
PX
1564 u16 capability;
1565 struct cfg80211_bss *bss;
1566
0cd01efb 1567 if (!non_tx_data)
0b8fb823
PX
1568 return;
1569 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1570 return;
213ed579
SS
1571 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;
0b8fb823 1576
0b8fb823
PX
1577 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1578 if (!new_ie)
1579 return;
1580
fe806e49
SS
1581 profile = kmalloc(ielen, gfp);
1582 if (!profile)
1583 goto out;
1584
1c8745f3
JB
1585 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) {
fe806e49
SS
1589 u8 profile_len;
1590
1c8745f3 1591 if (sub->id != 0 || sub->datalen < 4) {
0b8fb823
PX
1592 /* not a valid BSS profile */
1593 continue;
1594 }
1595
1c8745f3
JB
1596 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1597 sub->data[1] != 2) {
0b8fb823
PX
1598 /* The first element within the Nontransmitted
1599 * BSSID Profile is not the Nontransmitted
1600 * BSSID Capability element.
1601 */
1602 continue;
1603 }
1604
fe806e49
SS
1605 memset(profile, 0, ielen);
1606 profile_len = cfg80211_merge_profile(ie, ielen,
1607 elem,
1608 sub,
5809a5d5 1609 profile,
fe806e49
SS
1610 ielen);
1611
0b8fb823
PX
1612 /* found a Nontransmitted BSSID Profile */
1613 mbssid_index_ie = cfg80211_find_ie
1614 (WLAN_EID_MULTI_BSSID_IDX,
fe806e49 1615 profile, profile_len);
0b8fb823 1616 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
fe806e49
SS
1617 mbssid_index_ie[2] == 0 ||
1618 mbssid_index_ie[2] > 46) {
0b8fb823
PX
1619 /* No valid Multiple BSSID-Index element */
1620 continue;
1621 }
1622
ebb3ca3b 1623 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
fe806e49
SS
1624 /* 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
ebb3ca3b 1628 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
fe806e49 1629
0cd01efb
SS
1630 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,
0b8fb823
PX
1636 new_bssid);
1637 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
fe806e49
SS
1638 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
1639 profile,
1640 profile_len, new_ie,
0b8fb823
PX
1641 gfp);
1642 if (!new_ie_len)
1643 continue;
1644
fe806e49 1645 capability = get_unaligned_le16(profile + 2);
0b8fb823
PX
1646 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,
0cd01efb
SS
1653 non_tx_data,
1654 gfp);
0b8fb823
PX
1655 if (!bss)
1656 break;
1657 cfg80211_put_bss(wiphy, bss);
1658 }
0b8fb823
PX
1659 }
1660
fe806e49 1661out:
0b8fb823 1662 kfree(new_ie);
fe806e49 1663 kfree(profile);
0b8fb823
PX
1664}
1665
2a519311 1666struct cfg80211_bss *
0b8fb823
PX
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;
0cd01efb 1675 struct cfg80211_non_tx_bss non_tx_data;
0b8fb823
PX
1676
1677 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1678 capability, beacon_interval, ie,
1679 ielen, NULL, gfp);
b0d1d7ff
JB
1680 if (!res)
1681 return NULL;
0cd01efb 1682 non_tx_data.tx_bss = res;
0b8fb823 1683 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
0cd01efb
SS
1684 beacon_interval, ie, ielen, &non_tx_data,
1685 gfp);
0b8fb823
PX
1686 return res;
1687}
1688EXPORT_SYMBOL(cfg80211_inform_bss_data);
1689
1690static void
1691cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1692 struct cfg80211_inform_bss *data,
1693 struct ieee80211_mgmt *mgmt, size_t len,
0cd01efb 1694 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823
PX
1695 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);
1701
1702 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),
0cd01efb 1708 ie, ielen, non_tx_data, gfp);
0b8fb823
PX
1709}
1710
1711static void
1712cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
7011ba58 1713 struct cfg80211_bss *nontrans_bss,
461c4c2b 1714 struct ieee80211_mgmt *mgmt, size_t len)
0b8fb823
PX
1715{
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
461c4c2b
SS
1725 lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
1726
0b8fb823
PX
1727 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);
242b0931
JB
1735 /*
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)
0b8fb823
PX
1741 return;
1742 new_ie_len -= mbssid[1];
461c4c2b 1743
7011ba58 1744 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
461c4c2b 1745 if (!nontrans_ssid)
0b8fb823 1746 return;
461c4c2b 1747
0b8fb823 1748 new_ie_len += nontrans_ssid[1];
0b8fb823
PX
1749
1750 /* generate new ie for nontrans BSS
1751 * 1. replace SSID with nontrans BSS' SSID
1752 * 2. skip MBSSID IE
1753 */
461c4c2b 1754 new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
0b8fb823
PX
1755 if (!new_ie)
1756 return;
461c4c2b
SS
1757
1758 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
bede8d29
SS
1759 if (!new_ies)
1760 goto out_free;
0b8fb823
PX
1761
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)) {
7011ba58
SS
1782 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);
0b8fb823
PX
1785 if (old)
1786 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1787 } else {
7011ba58
SS
1788 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);
0b8fb823
PX
1791 if (old)
1792 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1793 }
bede8d29
SS
1794
1795out_free:
1796 kfree(new_ie);
0b8fb823 1797}
6e19bc4b 1798
0b8fb823
PX
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,
0b8fb823 1804 gfp_t gfp)
2a519311 1805{
9caf0364
JB
1806 struct cfg80211_internal_bss tmp = {}, *res;
1807 struct cfg80211_bss_ies *ies;
3afc2167 1808 struct ieee80211_channel *channel;
67af9811 1809 bool signal_valid;
2a519311
JB
1810 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1811 u.probe_resp.variable);
6eb18137 1812 int bss_type;
bef9bacc 1813
0172bb75
JB
1814 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1815 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1816
6e19bc4b 1817 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
4ee3e063 1818
bef9bacc
MK
1819 if (WARN_ON(!mgmt))
1820 return NULL;
1821
1822 if (WARN_ON(!wiphy))
1823 return NULL;
2a519311 1824
22fe88d3 1825 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
6e19bc4b 1826 (data->signal < 0 || data->signal > 100)))
2a519311
JB
1827 return NULL;
1828
bef9bacc 1829 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
2a519311
JB
1830 return NULL;
1831
0172bb75 1832 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
119f94a6 1833 ielen, data->chan, data->scan_width);
0172bb75
JB
1834 if (!channel)
1835 return NULL;
1836
0e227084 1837 ies = kzalloc(sizeof(*ies) + ielen, gfp);
9caf0364 1838 if (!ies)
2a519311 1839 return NULL;
9caf0364 1840 ies->len = ielen;
8cef2c9d 1841 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
0e227084 1842 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
9caf0364 1843 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
2a519311 1844
9caf0364
JB
1845 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);
505a2e88 1850
9caf0364
JB
1851 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1852 tmp.pub.channel = channel;
6e19bc4b
DS
1853 tmp.pub.scan_width = data->scan_width;
1854 tmp.pub.signal = data->signal;
9caf0364
JB
1855 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);
6e19bc4b 1857 tmp.ts_boottime = data->boottime_ns;
1d76250b 1858 tmp.parent_tsf = data->parent_tsf;
983dafaa
SD
1859 tmp.pub.chains = data->chains;
1860 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1d76250b 1861 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
9caf0364 1862
7bb106eb 1863 signal_valid = data->chan == channel;
a3ce17d1
CT
1864 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
1865 jiffies);
2a519311
JB
1866 if (!res)
1867 return NULL;
1868
57fbcce3 1869 if (channel->band == NL80211_BAND_60GHZ) {
6eb18137
DL
1870 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 }
e38f8a7a 1878
4ee3e063 1879 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
1880 /* cfg80211_bss_update gives us a referenced result */
1881 return &res->pub;
1882}
0b8fb823
PX
1883
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{
7011ba58 1890 struct cfg80211_bss *res, *tmp_bss;
0b8fb823
PX
1891 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);
0cd01efb 1895 struct cfg80211_non_tx_bss non_tx_data;
0b8fb823
PX
1896
1897 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
84f1772b 1898 len, gfp);
213ed579
SS
1899 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))
0b8fb823
PX
1904 return res;
1905
0cd01efb 1906 non_tx_data.tx_bss = res;
0b8fb823 1907 /* process each non-transmitting bss */
0cd01efb
SS
1908 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1909 &non_tx_data, gfp);
0b8fb823 1910
461c4c2b
SS
1911 spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
1912
0b8fb823
PX
1913 /* check if the res has other nontransmitting bss which is not
1914 * in MBSSID IE
1915 */
1916 ies1 = rcu_access_pointer(res->ies);
0b8fb823
PX
1917
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 */
7011ba58 1922 list_for_each_entry(tmp_bss, &res->nontrans_list,
0b8fb823 1923 nontrans_list) {
7011ba58 1924 ies2 = rcu_access_pointer(tmp_bss->ies);
0b8fb823
PX
1925 if (ies2->tsf < ies1->tsf)
1926 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
461c4c2b 1927 mgmt, len);
0b8fb823 1928 }
461c4c2b 1929 spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
0b8fb823
PX
1930
1931 return res;
1932}
6e19bc4b 1933EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2a519311 1934
5b112d3d 1935void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
4c0c0b75 1936{
f26cbf40 1937 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
4c0c0b75
JB
1938 struct cfg80211_internal_bss *bss;
1939
1940 if (!pub)
1941 return;
1942
1943 bss = container_of(pub, struct cfg80211_internal_bss, pub);
776b3580 1944
1b8ec87a
ZG
1945 spin_lock_bh(&rdev->bss_lock);
1946 bss_ref_get(rdev, bss);
1947 spin_unlock_bh(&rdev->bss_lock);
4c0c0b75
JB
1948}
1949EXPORT_SYMBOL(cfg80211_ref_bss);
1950
5b112d3d 1951void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2a519311 1952{
f26cbf40 1953 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2a519311
JB
1954 struct cfg80211_internal_bss *bss;
1955
1956 if (!pub)
1957 return;
1958
1959 bss = container_of(pub, struct cfg80211_internal_bss, pub);
776b3580 1960
1b8ec87a
ZG
1961 spin_lock_bh(&rdev->bss_lock);
1962 bss_ref_put(rdev, bss);
1963 spin_unlock_bh(&rdev->bss_lock);
2a519311
JB
1964}
1965EXPORT_SYMBOL(cfg80211_put_bss);
1966
d491af19
JB
1967void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1968{
f26cbf40 1969 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
7011ba58
SS
1970 struct cfg80211_internal_bss *bss, *tmp1;
1971 struct cfg80211_bss *nontrans_bss, *tmp;
d491af19
JB
1972
1973 if (WARN_ON(!pub))
1974 return;
1975
1976 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1977
1b8ec87a 1978 spin_lock_bh(&rdev->bss_lock);
7011ba58
SS
1979 if (list_empty(&bss->list))
1980 goto out;
1981
1982 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))
1b8ec87a 1988 rdev->bss_generation++;
3207390a 1989 }
7011ba58
SS
1990
1991 if (__cfg80211_unlink_bss(rdev, bss))
1992 rdev->bss_generation++;
1993out:
1b8ec87a 1994 spin_unlock_bh(&rdev->bss_lock);
d491af19
JB
1995}
1996EXPORT_SYMBOL(cfg80211_unlink_bss);
1997
4770c8f9
IP
1998void 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
0afd425b
SM
2019void 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
05dcb8bb
IP
2032 /*
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)
0afd425b
SM
2037 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
3d23e349 2102#ifdef CONFIG_CFG80211_WEXT
9f419f38
JB
2103static struct cfg80211_registered_device *
2104cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2105{
5fe231e8 2106 struct cfg80211_registered_device *rdev;
9f419f38
JB
2107 struct net_device *dev;
2108
5fe231e8
JB
2109 ASSERT_RTNL();
2110
9f419f38
JB
2111 dev = dev_get_by_index(net, ifindex);
2112 if (!dev)
5fe231e8
JB
2113 return ERR_PTR(-ENODEV);
2114 if (dev->ieee80211_ptr)
f26cbf40 2115 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
5fe231e8 2116 else
9f419f38
JB
2117 rdev = ERR_PTR(-ENODEV);
2118 dev_put(dev);
9f419f38
JB
2119 return rdev;
2120}
2121
2a519311
JB
2122int 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;
65486c8b 2129 struct cfg80211_scan_request *creq = NULL;
2a519311 2130 int i, err, n_channels = 0;
57fbcce3 2131 enum nl80211_band band;
2a519311
JB
2132
2133 if (!netif_running(dev))
2134 return -ENETDOWN;
2135
b2e3abdc
HS
2136 if (wrqu->data.length == sizeof(struct iw_scan_req))
2137 wreq = (struct iw_scan_req *)extra;
2138
463d0183 2139 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
2140
2141 if (IS_ERR(rdev))
2142 return PTR_ERR(rdev);
2143
f9d15d16 2144 if (rdev->scan_req || rdev->scan_msg) {
2a519311
JB
2145 err = -EBUSY;
2146 goto out;
2147 }
2148
2149 wiphy = &rdev->wiphy;
2150
b2e3abdc
HS
2151 /* Determine number of channels, needed to allocate creq */
2152 if (wreq && wreq->num_channels)
2153 n_channels = wreq->num_channels;
bdfbec2d
IP
2154 else
2155 n_channels = ieee80211_get_num_supported_channels(wiphy);
2a519311
JB
2156
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;
fd014284 2166 creq->wdev = dev->ieee80211_ptr;
5ba63533
JB
2167 /* SSIDs come after channels */
2168 creq->ssids = (void *)&creq->channels[n_channels];
2a519311
JB
2169 creq->n_channels = n_channels;
2170 creq->n_ssids = 1;
15d6030b 2171 creq->scan_start = jiffies;
2a519311 2172
b2e3abdc 2173 /* translate "Scan on frequencies" request */
2a519311 2174 i = 0;
57fbcce3 2175 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2a519311 2176 int j;
584991dc 2177
2a519311
JB
2178 if (!wiphy->bands[band])
2179 continue;
584991dc 2180
2a519311 2181 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
2182 /* ignore disabled channels */
2183 if (wiphy->bands[band]->channels[j].flags &
2184 IEEE80211_CHAN_DISABLED)
2185 continue;
b2e3abdc
HS
2186
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++) {
96998e3a
ZG
2195 struct iw_freq *freq =
2196 &wreq->channel_list[k];
2197 int wext_freq =
2198 cfg80211_wext_freq(freq);
2199
b2e3abdc
HS
2200 if (wext_freq == wiphy_freq)
2201 goto wext_freq_found;
2202 }
2203 goto wext_freq_not_found;
2204 }
2205
2206 wext_freq_found:
2a519311
JB
2207 creq->channels[i] = &wiphy->bands[band]->channels[j];
2208 i++;
b2e3abdc 2209 wext_freq_not_found: ;
2a519311
JB
2210 }
2211 }
8862dc5f
HS
2212 /* No channels found? */
2213 if (!i) {
2214 err = -EINVAL;
2215 goto out;
2216 }
2a519311 2217
b2e3abdc
HS
2218 /* Set real number of channels specified in creq->channels[] */
2219 creq->n_channels = i;
2a519311 2220
b2e3abdc
HS
2221 /* translate "Scan for SSID" request */
2222 if (wreq) {
2a519311 2223 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
65486c8b
JB
2224 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2225 err = -EINVAL;
2226 goto out;
2227 }
2a519311
JB
2228 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
57fbcce3 2235 for (i = 0; i < NUM_NL80211_BANDS; i++)
a401d2bb
JB
2236 if (wiphy->bands[i])
2237 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
34850ab2 2238
818965d3
JM
2239 eth_broadcast_addr(creq->bssid);
2240
2a519311 2241 rdev->scan_req = creq;
e35e4d28 2242 err = rdev_scan(rdev, creq);
2a519311
JB
2243 if (err) {
2244 rdev->scan_req = NULL;
65486c8b 2245 /* creq will be freed below */
463d0183 2246 } else {
fd014284 2247 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
65486c8b
JB
2248 /* creq now owned by driver */
2249 creq = NULL;
463d0183
JB
2250 dev_hold(dev);
2251 }
2a519311 2252 out:
65486c8b 2253 kfree(creq);
2a519311
JB
2254 return err;
2255}
2afe38d1 2256EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2a519311 2257
76a70e9c
JM
2258static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2259 const struct cfg80211_bss_ies *ies,
2260 char *current_ev, char *end_buf)
2a519311 2261{
9caf0364 2262 const u8 *pos, *end, *next;
2a519311
JB
2263 struct iw_event iwe;
2264
9caf0364 2265 if (!ies)
76a70e9c 2266 return current_ev;
2a519311
JB
2267
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 */
9caf0364
JB
2272 pos = ies->data;
2273 end = pos + ies->len;
2a519311
JB
2274
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;
76a70e9c
JM
2283 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;
2a519311
JB
2288 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;
76a70e9c
JM
2295 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;
2a519311 2300 }
76a70e9c
JM
2301
2302 return current_ev;
2a519311
JB
2303}
2304
2a519311 2305static char *
77965c97
JB
2306ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2307 struct cfg80211_internal_bss *bss, char *current_ev,
2308 char *end_buf)
2a519311 2309{
9caf0364 2310 const struct cfg80211_bss_ies *ies;
2a519311 2311 struct iw_event iwe;
9caf0364 2312 const u8 *ie;
76a70e9c
JM
2313 u8 buf[50];
2314 u8 *cfg, *p, *tmp;
9caf0364 2315 int rem, i, sig;
2a519311
JB
2316 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);
76a70e9c
JM
2322 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;
2a519311
JB
2326
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;
76a70e9c
JM
2331 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;
2a519311
JB
2335
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;
76a70e9c
JM
2340 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;
2a519311 2344
77965c97 2345 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2a519311
JB
2346 memset(&iwe, 0, sizeof(iwe));
2347 iwe.cmd = IWEVQUAL;
2348 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2349 IW_QUAL_NOISE_INVALID |
a77b8552 2350 IW_QUAL_QUAL_UPDATED;
77965c97 2351 switch (wiphy->signal_type) {
2a519311 2352 case CFG80211_SIGNAL_TYPE_MBM:
a77b8552
JB
2353 sig = bss->pub.signal / 100;
2354 iwe.u.qual.level = sig;
2a519311 2355 iwe.u.qual.updated |= IW_QUAL_DBM;
a77b8552
JB
2356 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;
2a519311
JB
2362 break;
2363 case CFG80211_SIGNAL_TYPE_UNSPEC:
2364 iwe.u.qual.level = bss->pub.signal;
a77b8552
JB
2365 /* will give range 0 .. 100 */
2366 iwe.u.qual.qual = bss->pub.signal;
2a519311
JB
2367 break;
2368 default:
2369 /* not reached */
2370 break;
2371 }
76a70e9c
JM
2372 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;
2a519311
JB
2377 }
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;
76a70e9c
JM
2386 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2387 &iwe, "");
2388 if (IS_ERR(current_ev))
2389 return current_ev;
2a519311 2390
9caf0364
JB
2391 rcu_read_lock();
2392 ies = rcu_dereference(bss->pub.ies);
83c7aa1a
JB
2393 rem = ies->len;
2394 ie = ies->data;
9caf0364 2395
83c7aa1a 2396 while (rem >= 2) {
2a519311
JB
2397 /* 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;
76a70e9c
JM
2407 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;
2a519311
JB
2413 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;
76a70e9c
JM
2419 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;
2a519311
JB
2425 break;
2426 case WLAN_EID_MESH_CONFIG:
2427 ismesh = true;
136cfa28 2428 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2a519311 2429 break;
9caf0364 2430 cfg = (u8 *)ie + 2;
2a519311
JB
2431 memset(&iwe, 0, sizeof(iwe));
2432 iwe.cmd = IWEVCUSTOM;
76aa5e70
RP
2433 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2434 "0x%02X", cfg[0]);
2a519311 2435 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2436 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;
76aa5e70
RP
2442 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2443 cfg[1]);
2a519311 2444 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2445 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;
76aa5e70
RP
2451 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2452 cfg[2]);
2a519311 2453 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2454 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;
76aa5e70 2460 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2a519311 2461 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2462 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;
76aa5e70
RP
2468 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2469 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2470 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;
76aa5e70
RP
2476 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2477 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2478 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;
76aa5e70 2484 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2a519311 2485 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2486 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;
2a519311
JB
2492 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);
76a70e9c 2506 tmp = p;
2a519311 2507 p = iwe_stream_add_value(info, current_ev, p,
76a70e9c
JM
2508 end_buf, &iwe,
2509 IW_EV_PARAM_LEN);
2510 if (p == tmp) {
2511 current_ev = ERR_PTR(-E2BIG);
2512 goto unlock;
2513 }
2a519311
JB
2514 }
2515 current_ev = p;
2516 break;
2517 }
2518 rem -= ie[1] + 2;
2519 ie += ie[1] + 2;
2520 }
2521
f64f9e71
JP
2522 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2523 ismesh) {
2a519311
JB
2524 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;
76a70e9c
JM
2532 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;
2a519311
JB
2537 }
2538
76a70e9c
JM
2539 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;
2556
2557 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2558
2559 unlock:
9caf0364 2560 rcu_read_unlock();
2a519311
JB
2561 return current_ev;
2562}
2563
2564
1b8ec87a 2565static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2a519311
JB
2566 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;
76a70e9c 2572 int err = 0;
2a519311 2573
1b8ec87a
ZG
2574 spin_lock_bh(&rdev->bss_lock);
2575 cfg80211_bss_expire(rdev);
2a519311 2576
1b8ec87a 2577 list_for_each_entry(bss, &rdev->bss_list, list) {
2a519311 2578 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
76a70e9c
JM
2579 err = -E2BIG;
2580 break;
2a519311 2581 }
1b8ec87a 2582 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
77965c97 2583 current_ev, end_buf);
76a70e9c
JM
2584 if (IS_ERR(current_ev)) {
2585 err = PTR_ERR(current_ev);
2586 break;
2587 }
2a519311 2588 }
1b8ec87a 2589 spin_unlock_bh(&rdev->bss_lock);
76a70e9c
JM
2590
2591 if (err)
2592 return err;
2a519311
JB
2593 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
463d0183 2607 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
2608
2609 if (IS_ERR(rdev))
2610 return PTR_ERR(rdev);
2611
f9d15d16 2612 if (rdev->scan_req || rdev->scan_msg)
5fe231e8 2613 return -EAGAIN;
2a519311
JB
2614
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
2a519311
JB
2622 return res;
2623}
2afe38d1 2624EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
2a519311 2625#endif