]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/wireless/scan.c
UBUNTU: Ubuntu-5.3.0-29.31
[mirror_ubuntu-eoan-kernel.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
58 * data stored stored in the BSS struct, since the beacon IEs are
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
1b57b621 559 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
ca986ad9 560
1b57b621 561 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
ca986ad9
AVS
562 if (pos->reqid == reqid)
563 return pos;
564 }
b34939b9 565 return NULL;
ca986ad9
AVS
566}
567
568/*
569 * Determines if a scheduled scan request can be handled. When a legacy
570 * scheduled scan is running no other scheduled scan is allowed regardless
571 * whether the request is for legacy or multi-support scan. When a multi-support
572 * scheduled scan is running a request for legacy scan is not allowed. In this
573 * case a request for multi-support scan can be handled if resources are
574 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
575 */
576int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
577 bool want_multi)
578{
579 struct cfg80211_sched_scan_request *pos;
580 int i = 0;
581
582 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
583 /* request id zero means legacy in progress */
584 if (!i && !pos->reqid)
585 return -EINPROGRESS;
586 i++;
587 }
588
589 if (i) {
590 /* no legacy allowed when multi request(s) are active */
591 if (!want_multi)
592 return -EINPROGRESS;
593
594 /* resource limit reached */
595 if (i == rdev->wiphy.max_sched_scan_reqs)
596 return -ENOSPC;
597 }
598 return 0;
599}
600
b34939b9 601void cfg80211_sched_scan_results_wk(struct work_struct *work)
807f8a8c
LC
602{
603 struct cfg80211_registered_device *rdev;
b34939b9 604 struct cfg80211_sched_scan_request *req, *tmp;
807f8a8c 605
b34939b9
AVS
606 rdev = container_of(work, struct cfg80211_registered_device,
607 sched_scan_res_wk);
807f8a8c 608
5fe231e8 609 rtnl_lock();
b34939b9
AVS
610 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
611 if (req->report_results) {
612 req->report_results = false;
613 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
614 /* flush entries from previous scans */
615 spin_lock_bh(&rdev->bss_lock);
616 __cfg80211_bss_expire(rdev, req->scan_start);
617 spin_unlock_bh(&rdev->bss_lock);
618 req->scan_start = jiffies;
619 }
620 nl80211_send_sched_scan(req,
621 NL80211_CMD_SCHED_SCAN_RESULTS);
15d6030b 622 }
15d6030b 623 }
5fe231e8 624 rtnl_unlock();
807f8a8c
LC
625}
626
b34939b9 627void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
807f8a8c 628{
ca986ad9
AVS
629 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
630 struct cfg80211_sched_scan_request *request;
631
b34939b9 632 trace_cfg80211_sched_scan_results(wiphy, reqid);
807f8a8c 633 /* ignore if we're not scanning */
31a60ed1 634
1b57b621 635 rcu_read_lock();
b34939b9
AVS
636 request = cfg80211_find_sched_scan_req(rdev, reqid);
637 if (request) {
638 request->report_results = true;
639 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
640 }
1b57b621 641 rcu_read_unlock();
807f8a8c
LC
642}
643EXPORT_SYMBOL(cfg80211_sched_scan_results);
644
b34939b9 645void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
807f8a8c 646{
f26cbf40 647 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
807f8a8c 648
792e6aa7
EP
649 ASSERT_RTNL();
650
b34939b9 651 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
4ee3e063 652
b34939b9 653 __cfg80211_stop_sched_scan(rdev, reqid, true);
792e6aa7
EP
654}
655EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
656
b34939b9 657void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
792e6aa7
EP
658{
659 rtnl_lock();
b34939b9 660 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
5fe231e8 661 rtnl_unlock();
807f8a8c 662}
807f8a8c
LC
663EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
664
ca986ad9
AVS
665int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
666 struct cfg80211_sched_scan_request *req,
667 bool driver_initiated)
807f8a8c 668{
5fe231e8 669 ASSERT_RTNL();
807f8a8c 670
85a9994a 671 if (!driver_initiated) {
3a3ecf1d 672 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
85a9994a
LC
673 if (err)
674 return err;
675 }
807f8a8c 676
ca986ad9 677 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
807f8a8c 678
ca986ad9 679 cfg80211_del_sched_scan_req(rdev, req);
807f8a8c 680
3b4670ff 681 return 0;
807f8a8c
LC
682}
683
ca986ad9
AVS
684int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
685 u64 reqid, bool driver_initiated)
686{
687 struct cfg80211_sched_scan_request *sched_scan_req;
688
689 ASSERT_RTNL();
690
691 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
b34939b9
AVS
692 if (!sched_scan_req)
693 return -ENOENT;
ca986ad9
AVS
694
695 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
696 driver_initiated);
697}
698
1b8ec87a 699void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
cb3a8eec
DW
700 unsigned long age_secs)
701{
702 struct cfg80211_internal_bss *bss;
703 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
704
1b8ec87a
ZG
705 spin_lock_bh(&rdev->bss_lock);
706 list_for_each_entry(bss, &rdev->bss_list, list)
cb3a8eec 707 bss->ts -= age_jiffies;
1b8ec87a 708 spin_unlock_bh(&rdev->bss_lock);
cb3a8eec
DW
709}
710
1b8ec87a 711void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
2a519311 712{
1b8ec87a 713 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
2a519311
JB
714}
715
49a68e0d
JB
716const struct element *
717cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
718 const u8 *match, unsigned int match_len,
719 unsigned int match_offset)
2a519311 720{
0f3b07f0
JB
721 const struct element *elem;
722
0f3b07f0 723 for_each_element_id(elem, eid, ies, len) {
49a68e0d
JB
724 if (elem->datalen >= match_offset + match_len &&
725 !memcmp(elem->data + match_offset, match, match_len))
726 return elem;
2a519311 727 }
fbd05e4a
LC
728
729 return NULL;
2a519311 730}
49a68e0d 731EXPORT_SYMBOL(cfg80211_find_elem_match);
2a519311 732
49a68e0d
JB
733const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
734 const u8 *ies,
735 unsigned int len)
0c28ec58 736{
49a68e0d 737 const struct element *elem;
fbd05e4a
LC
738 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
739 int match_len = (oui_type < 0) ? 3 : sizeof(match);
0c28ec58 740
9e9ea439
EG
741 if (WARN_ON(oui_type > 0xff))
742 return NULL;
743
49a68e0d
JB
744 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
745 match, match_len, 0);
6719429d 746
49a68e0d 747 if (!elem || elem->datalen < 4)
fbd05e4a 748 return NULL;
6719429d 749
49a68e0d 750 return elem;
0c28ec58 751}
49a68e0d 752EXPORT_SYMBOL(cfg80211_find_vendor_elem);
0c28ec58 753
4593c4cb
JB
754/**
755 * enum bss_compare_mode - BSS compare mode
756 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
757 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
758 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
759 */
760enum bss_compare_mode {
761 BSS_CMP_REGULAR,
762 BSS_CMP_HIDE_ZLEN,
763 BSS_CMP_HIDE_NUL,
764};
765
dd9dfb9f 766static int cmp_bss(struct cfg80211_bss *a,
5622f5bb 767 struct cfg80211_bss *b,
4593c4cb 768 enum bss_compare_mode mode)
dd9dfb9f 769{
9caf0364 770 const struct cfg80211_bss_ies *a_ies, *b_ies;
3af6341c
JB
771 const u8 *ie1 = NULL;
772 const u8 *ie2 = NULL;
5622f5bb 773 int i, r;
dd9dfb9f 774
3af6341c
JB
775 if (a->channel != b->channel)
776 return b->channel->center_freq - a->channel->center_freq;
dd9dfb9f 777
9caf0364
JB
778 a_ies = rcu_access_pointer(a->ies);
779 if (!a_ies)
780 return -1;
781 b_ies = rcu_access_pointer(b->ies);
782 if (!b_ies)
783 return 1;
784
3af6341c
JB
785 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
786 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
787 a_ies->data, a_ies->len);
788 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
789 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
790 b_ies->data, b_ies->len);
791 if (ie1 && ie2) {
792 int mesh_id_cmp;
793
794 if (ie1[1] == ie2[1])
795 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
796 else
797 mesh_id_cmp = ie2[1] - ie1[1];
798
799 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
800 a_ies->data, a_ies->len);
801 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
802 b_ies->data, b_ies->len);
803 if (ie1 && ie2) {
804 if (mesh_id_cmp)
805 return mesh_id_cmp;
806 if (ie1[1] != ie2[1])
807 return ie2[1] - ie1[1];
808 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
809 }
810 }
811
3af6341c
JB
812 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
813 if (r)
814 return r;
815
9caf0364
JB
816 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
817 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
dd9dfb9f 818
5622f5bb
JB
819 if (!ie1 && !ie2)
820 return 0;
821
f94f8b16 822 /*
5622f5bb
JB
823 * Note that with "hide_ssid", the function returns a match if
824 * the already-present BSS ("b") is a hidden SSID beacon for
825 * the new BSS ("a").
f94f8b16 826 */
dd9dfb9f
DT
827
828 /* sort missing IE before (left of) present IE */
829 if (!ie1)
830 return -1;
831 if (!ie2)
832 return 1;
833
4593c4cb
JB
834 switch (mode) {
835 case BSS_CMP_HIDE_ZLEN:
836 /*
837 * In ZLEN mode we assume the BSS entry we're
838 * looking for has a zero-length SSID. So if
839 * the one we're looking at right now has that,
840 * return 0. Otherwise, return the difference
841 * in length, but since we're looking for the
842 * 0-length it's really equivalent to returning
843 * the length of the one we're looking at.
844 *
845 * No content comparison is needed as we assume
846 * the content length is zero.
847 */
848 return ie2[1];
849 case BSS_CMP_REGULAR:
850 default:
851 /* sort by length first, then by contents */
852 if (ie1[1] != ie2[1])
853 return ie2[1] - ie1[1];
5622f5bb 854 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
4593c4cb
JB
855 case BSS_CMP_HIDE_NUL:
856 if (ie1[1] != ie2[1])
857 return ie2[1] - ie1[1];
858 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
859 for (i = 0; i < ie2[1]; i++)
860 if (ie2[i + 2])
861 return -1;
862 return 0;
863 }
dd9dfb9f
DT
864}
865
6eb18137 866static bool cfg80211_bss_type_match(u16 capability,
57fbcce3 867 enum nl80211_band band,
6eb18137
DL
868 enum ieee80211_bss_type bss_type)
869{
870 bool ret = true;
871 u16 mask, val;
872
873 if (bss_type == IEEE80211_BSS_TYPE_ANY)
874 return ret;
875
57fbcce3 876 if (band == NL80211_BAND_60GHZ) {
6eb18137
DL
877 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
878 switch (bss_type) {
879 case IEEE80211_BSS_TYPE_ESS:
880 val = WLAN_CAPABILITY_DMG_TYPE_AP;
881 break;
882 case IEEE80211_BSS_TYPE_PBSS:
883 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
884 break;
885 case IEEE80211_BSS_TYPE_IBSS:
886 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
887 break;
888 default:
889 return false;
890 }
891 } else {
892 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
893 switch (bss_type) {
894 case IEEE80211_BSS_TYPE_ESS:
895 val = WLAN_CAPABILITY_ESS;
896 break;
897 case IEEE80211_BSS_TYPE_IBSS:
898 val = WLAN_CAPABILITY_IBSS;
899 break;
900 case IEEE80211_BSS_TYPE_MBSS:
901 val = 0;
902 break;
903 default:
904 return false;
905 }
906 }
907
908 ret = ((capability & mask) == val);
909 return ret;
910}
911
0e3a39b5 912/* Returned bss is reference counted and must be cleaned up appropriately. */
2a519311
JB
913struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
914 struct ieee80211_channel *channel,
915 const u8 *bssid,
79420f09 916 const u8 *ssid, size_t ssid_len,
6eb18137
DL
917 enum ieee80211_bss_type bss_type,
918 enum ieee80211_privacy privacy)
2a519311 919{
f26cbf40 920 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2a519311 921 struct cfg80211_internal_bss *bss, *res = NULL;
ccb6c136 922 unsigned long now = jiffies;
6eb18137 923 int bss_privacy;
2a519311 924
6eb18137
DL
925 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
926 privacy);
4ee3e063 927
1b8ec87a 928 spin_lock_bh(&rdev->bss_lock);
2a519311 929
1b8ec87a 930 list_for_each_entry(bss, &rdev->bss_list, list) {
6eb18137
DL
931 if (!cfg80211_bss_type_match(bss->pub.capability,
932 bss->pub.channel->band, bss_type))
933 continue;
934
935 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
936 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
937 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
79420f09 938 continue;
2a519311
JB
939 if (channel && bss->pub.channel != channel)
940 continue;
c14a7400
JB
941 if (!is_valid_ether_addr(bss->pub.bssid))
942 continue;
ccb6c136
JB
943 /* Don't get expired BSS structs */
944 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
945 !atomic_read(&bss->hold))
946 continue;
2a519311
JB
947 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
948 res = bss;
1b8ec87a 949 bss_ref_get(rdev, res);
2a519311
JB
950 break;
951 }
952 }
953
1b8ec87a 954 spin_unlock_bh(&rdev->bss_lock);
2a519311
JB
955 if (!res)
956 return NULL;
4ee3e063 957 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
958 return &res->pub;
959}
960EXPORT_SYMBOL(cfg80211_get_bss);
961
1b8ec87a 962static void rb_insert_bss(struct cfg80211_registered_device *rdev,
2a519311
JB
963 struct cfg80211_internal_bss *bss)
964{
1b8ec87a 965 struct rb_node **p = &rdev->bss_tree.rb_node;
2a519311
JB
966 struct rb_node *parent = NULL;
967 struct cfg80211_internal_bss *tbss;
968 int cmp;
969
970 while (*p) {
971 parent = *p;
972 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
973
4593c4cb 974 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
2a519311
JB
975
976 if (WARN_ON(!cmp)) {
977 /* will sort of leak this BSS */
978 return;
979 }
980
981 if (cmp < 0)
982 p = &(*p)->rb_left;
983 else
984 p = &(*p)->rb_right;
985 }
986
987 rb_link_node(&bss->rbn, parent, p);
1b8ec87a 988 rb_insert_color(&bss->rbn, &rdev->bss_tree);
2a519311
JB
989}
990
991static struct cfg80211_internal_bss *
1b8ec87a 992rb_find_bss(struct cfg80211_registered_device *rdev,
5622f5bb 993 struct cfg80211_internal_bss *res,
4593c4cb 994 enum bss_compare_mode mode)
dd9dfb9f 995{
1b8ec87a 996 struct rb_node *n = rdev->bss_tree.rb_node;
dd9dfb9f
DT
997 struct cfg80211_internal_bss *bss;
998 int r;
999
1000 while (n) {
1001 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
4593c4cb 1002 r = cmp_bss(&res->pub, &bss->pub, mode);
dd9dfb9f
DT
1003
1004 if (r == 0)
1005 return bss;
1006 else if (r < 0)
1007 n = n->rb_left;
1008 else
1009 n = n->rb_right;
1010 }
1011
1012 return NULL;
1013}
1014
1b8ec87a 1015static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
776b3580 1016 struct cfg80211_internal_bss *new)
dd9dfb9f 1017{
9caf0364 1018 const struct cfg80211_bss_ies *ies;
776b3580
JB
1019 struct cfg80211_internal_bss *bss;
1020 const u8 *ie;
1021 int i, ssidlen;
1022 u8 fold = 0;
9853a55e 1023 u32 n_entries = 0;
9caf0364 1024
776b3580 1025 ies = rcu_access_pointer(new->pub.beacon_ies);
9caf0364 1026 if (WARN_ON(!ies))
776b3580 1027 return false;
dd9dfb9f 1028
776b3580
JB
1029 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1030 if (!ie) {
1031 /* nothing to do */
1032 return true;
1033 }
1034
1035 ssidlen = ie[1];
1036 for (i = 0; i < ssidlen; i++)
1037 fold |= ie[2 + i];
1038
1039 if (fold) {
1040 /* not a hidden SSID */
1041 return true;
1042 }
1043
1044 /* This is the bad part ... */
1045
1b8ec87a 1046 list_for_each_entry(bss, &rdev->bss_list, list) {
9853a55e
JB
1047 /*
1048 * we're iterating all the entries anyway, so take the
1049 * opportunity to validate the list length accounting
1050 */
1051 n_entries++;
1052
776b3580
JB
1053 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1054 continue;
1055 if (bss->pub.channel != new->pub.channel)
1056 continue;
dcd6eac1
SW
1057 if (bss->pub.scan_width != new->pub.scan_width)
1058 continue;
776b3580
JB
1059 if (rcu_access_pointer(bss->pub.beacon_ies))
1060 continue;
1061 ies = rcu_access_pointer(bss->pub.ies);
1062 if (!ies)
1063 continue;
1064 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1065 if (!ie)
1066 continue;
1067 if (ssidlen && ie[1] != ssidlen)
1068 continue;
776b3580
JB
1069 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1070 continue;
1071 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1072 list_del(&bss->hidden_list);
1073 /* combine them */
1074 list_add(&bss->hidden_list, &new->hidden_list);
1075 bss->pub.hidden_beacon_bss = &new->pub;
1076 new->refcount += bss->refcount;
1077 rcu_assign_pointer(bss->pub.beacon_ies,
1078 new->pub.beacon_ies);
1079 }
1080
9853a55e
JB
1081 WARN_ONCE(n_entries != rdev->bss_entries,
1082 "rdev bss entries[%d]/list[len:%d] corruption\n",
1083 rdev->bss_entries, n_entries);
1084
776b3580 1085 return true;
dd9dfb9f
DT
1086}
1087
0cd01efb
SS
1088struct cfg80211_non_tx_bss {
1089 struct cfg80211_bss *tx_bss;
1090 u8 max_bssid_indicator;
1091 u8 bssid_index;
1092};
1093
0e3a39b5 1094/* Returned bss is reference counted and must be cleaned up appropriately. */
a3ce17d1 1095struct cfg80211_internal_bss *
1b8ec87a 1096cfg80211_bss_update(struct cfg80211_registered_device *rdev,
3afc2167 1097 struct cfg80211_internal_bss *tmp,
a3ce17d1 1098 bool signal_valid, unsigned long ts)
2a519311
JB
1099{
1100 struct cfg80211_internal_bss *found = NULL;
2a519311 1101
9caf0364 1102 if (WARN_ON(!tmp->pub.channel))
2a519311 1103 return NULL;
2a519311 1104
a3ce17d1 1105 tmp->ts = ts;
2a519311 1106
1b8ec87a 1107 spin_lock_bh(&rdev->bss_lock);
2a519311 1108
9caf0364 1109 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1b8ec87a 1110 spin_unlock_bh(&rdev->bss_lock);
9caf0364
JB
1111 return NULL;
1112 }
1113
1b8ec87a 1114 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
2a519311 1115
cd1658f5 1116 if (found) {
34a6eddb 1117 /* Update IEs */
9caf0364
JB
1118 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1119 const struct cfg80211_bss_ies *old;
1120
1121 old = rcu_access_pointer(found->pub.proberesp_ies);
cd1658f5 1122
9caf0364
JB
1123 rcu_assign_pointer(found->pub.proberesp_ies,
1124 tmp->pub.proberesp_ies);
34a6eddb 1125 /* Override possible earlier Beacon frame IEs */
9caf0364
JB
1126 rcu_assign_pointer(found->pub.ies,
1127 tmp->pub.proberesp_ies);
1128 if (old)
1129 kfree_rcu((struct cfg80211_bss_ies *)old,
1130 rcu_head);
1131 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
9537f227 1132 const struct cfg80211_bss_ies *old;
776b3580
JB
1133 struct cfg80211_internal_bss *bss;
1134
1135 if (found->pub.hidden_beacon_bss &&
1136 !list_empty(&found->hidden_list)) {
1345ee6a
JB
1137 const struct cfg80211_bss_ies *f;
1138
776b3580
JB
1139 /*
1140 * The found BSS struct is one of the probe
1141 * response members of a group, but we're
1142 * receiving a beacon (beacon_ies in the tmp
1143 * bss is used). This can only mean that the
1144 * AP changed its beacon from not having an
1145 * SSID to showing it, which is confusing so
1146 * drop this information.
1147 */
1345ee6a
JB
1148
1149 f = rcu_access_pointer(tmp->pub.beacon_ies);
1150 kfree_rcu((struct cfg80211_bss_ies *)f,
1151 rcu_head);
776b3580
JB
1152 goto drop;
1153 }
915de2ff 1154
9caf0364 1155 old = rcu_access_pointer(found->pub.beacon_ies);
9caf0364
JB
1156
1157 rcu_assign_pointer(found->pub.beacon_ies,
1158 tmp->pub.beacon_ies);
01123e23
SN
1159
1160 /* Override IEs if they were from a beacon before */
9537f227 1161 if (old == rcu_access_pointer(found->pub.ies))
9caf0364
JB
1162 rcu_assign_pointer(found->pub.ies,
1163 tmp->pub.beacon_ies);
cd1658f5 1164
776b3580
JB
1165 /* Assign beacon IEs to all sub entries */
1166 list_for_each_entry(bss, &found->hidden_list,
1167 hidden_list) {
1168 const struct cfg80211_bss_ies *ies;
1169
1170 ies = rcu_access_pointer(bss->pub.beacon_ies);
1171 WARN_ON(ies != old);
1172
1173 rcu_assign_pointer(bss->pub.beacon_ies,
1174 tmp->pub.beacon_ies);
1175 }
1176
9caf0364
JB
1177 if (old)
1178 kfree_rcu((struct cfg80211_bss_ies *)old,
1179 rcu_head);
1180 }
1345ee6a
JB
1181
1182 found->pub.beacon_interval = tmp->pub.beacon_interval;
3afc2167
EG
1183 /*
1184 * don't update the signal if beacon was heard on
1185 * adjacent channel.
1186 */
1187 if (signal_valid)
1188 found->pub.signal = tmp->pub.signal;
1345ee6a
JB
1189 found->pub.capability = tmp->pub.capability;
1190 found->ts = tmp->ts;
6e19bc4b 1191 found->ts_boottime = tmp->ts_boottime;
1d76250b 1192 found->parent_tsf = tmp->parent_tsf;
983dafaa
SD
1193 found->pub.chains = tmp->pub.chains;
1194 memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
1195 IEEE80211_MAX_CHAINS);
1d76250b 1196 ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
0cd01efb
SS
1197 found->pub.max_bssid_indicator = tmp->pub.max_bssid_indicator;
1198 found->pub.bssid_index = tmp->pub.bssid_index;
2a519311 1199 } else {
9caf0364 1200 struct cfg80211_internal_bss *new;
dd9dfb9f 1201 struct cfg80211_internal_bss *hidden;
9caf0364 1202 struct cfg80211_bss_ies *ies;
dd9dfb9f 1203
9caf0364
JB
1204 /*
1205 * create a copy -- the "res" variable that is passed in
1206 * is allocated on the stack since it's not needed in the
1207 * more common case of an update
1208 */
1b8ec87a 1209 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
9caf0364
JB
1210 GFP_ATOMIC);
1211 if (!new) {
1212 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1213 if (ies)
1214 kfree_rcu(ies, rcu_head);
1215 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1216 if (ies)
1217 kfree_rcu(ies, rcu_head);
776b3580 1218 goto drop;
9caf0364
JB
1219 }
1220 memcpy(new, tmp, sizeof(*new));
776b3580
JB
1221 new->refcount = 1;
1222 INIT_LIST_HEAD(&new->hidden_list);
7011ba58 1223 INIT_LIST_HEAD(&new->pub.nontrans_list);
776b3580
JB
1224
1225 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1b8ec87a 1226 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
776b3580 1227 if (!hidden)
1b8ec87a 1228 hidden = rb_find_bss(rdev, tmp,
776b3580
JB
1229 BSS_CMP_HIDE_NUL);
1230 if (hidden) {
1231 new->pub.hidden_beacon_bss = &hidden->pub;
1232 list_add(&new->hidden_list,
1233 &hidden->hidden_list);
1234 hidden->refcount++;
1235 rcu_assign_pointer(new->pub.beacon_ies,
1236 hidden->pub.beacon_ies);
1237 }
1238 } else {
1239 /*
1240 * Ok so we found a beacon, and don't have an entry. If
1241 * it's a beacon with hidden SSID, we might be in for an
1242 * expensive search for any probe responses that should
1243 * be grouped with this beacon for updates ...
1244 */
1b8ec87a 1245 if (!cfg80211_combine_bsses(rdev, new)) {
776b3580
JB
1246 kfree(new);
1247 goto drop;
1248 }
1249 }
1250
9853a55e
JB
1251 if (rdev->bss_entries >= bss_entries_limit &&
1252 !cfg80211_bss_expire_oldest(rdev)) {
1253 kfree(new);
1254 goto drop;
1255 }
1256
a3584f56 1257 /* This must be before the call to bss_ref_get */
0cd01efb 1258 if (tmp->pub.transmitted_bss) {
a3584f56 1259 struct cfg80211_internal_bss *pbss =
0cd01efb 1260 container_of(tmp->pub.transmitted_bss,
a3584f56
SS
1261 struct cfg80211_internal_bss,
1262 pub);
1263
0cd01efb 1264 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
a3584f56
SS
1265 bss_ref_get(rdev, pbss);
1266 }
1267
1b8ec87a 1268 list_add_tail(&new->list, &rdev->bss_list);
9853a55e 1269 rdev->bss_entries++;
1b8ec87a 1270 rb_insert_bss(rdev, new);
9caf0364 1271 found = new;
2a519311
JB
1272 }
1273
1b8ec87a
ZG
1274 rdev->bss_generation++;
1275 bss_ref_get(rdev, found);
1276 spin_unlock_bh(&rdev->bss_lock);
2a519311 1277
2a519311 1278 return found;
776b3580 1279 drop:
1b8ec87a 1280 spin_unlock_bh(&rdev->bss_lock);
776b3580 1281 return NULL;
2a519311
JB
1282}
1283
119f94a6
JM
1284/*
1285 * Update RX channel information based on the available frame payload
1286 * information. This is mainly for the 2.4 GHz band where frames can be received
1287 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1288 * element to indicate the current (transmitting) channel, but this might also
1289 * be needed on other bands if RX frequency does not match with the actual
1290 * operating channel of a BSS.
1291 */
0172bb75
JB
1292static struct ieee80211_channel *
1293cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
119f94a6
JM
1294 struct ieee80211_channel *channel,
1295 enum nl80211_bss_scan_width scan_width)
0172bb75
JB
1296{
1297 const u8 *tmp;
1298 u32 freq;
1299 int channel_number = -1;
119f94a6 1300 struct ieee80211_channel *alt_channel;
0172bb75
JB
1301
1302 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1303 if (tmp && tmp[1] == 1) {
1304 channel_number = tmp[2];
1305 } else {
1306 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1307 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1308 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1309
1310 channel_number = htop->primary_chan;
1311 }
1312 }
1313
119f94a6
JM
1314 if (channel_number < 0) {
1315 /* No channel information in frame payload */
0172bb75 1316 return channel;
119f94a6 1317 }
0172bb75
JB
1318
1319 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
119f94a6
JM
1320 alt_channel = ieee80211_get_channel(wiphy, freq);
1321 if (!alt_channel) {
1322 if (channel->band == NL80211_BAND_2GHZ) {
1323 /*
1324 * Better not allow unexpected channels when that could
1325 * be going beyond the 1-11 range (e.g., discovering
1326 * BSS on channel 12 when radio is configured for
1327 * channel 11.
1328 */
1329 return NULL;
1330 }
1331
1332 /* No match for the payload channel number - ignore it */
1333 return channel;
1334 }
1335
1336 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1337 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1338 /*
1339 * Ignore channel number in 5 and 10 MHz channels where there
1340 * may not be an n:1 or 1:n mapping between frequencies and
1341 * channel numbers.
1342 */
1343 return channel;
1344 }
1345
1346 /*
1347 * Use the channel determined through the payload channel number
1348 * instead of the RX channel reported by the driver.
1349 */
1350 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
0172bb75 1351 return NULL;
119f94a6 1352 return alt_channel;
0172bb75
JB
1353}
1354
0e3a39b5 1355/* Returned bss is reference counted and must be cleaned up appropriately. */
0b8fb823
PX
1356static struct cfg80211_bss *
1357cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1358 struct cfg80211_inform_bss *data,
1359 enum cfg80211_bss_frame_type ftype,
1360 const u8 *bssid, u64 tsf, u16 capability,
1361 u16 beacon_interval, const u8 *ie, size_t ielen,
0cd01efb 1362 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823 1363 gfp_t gfp)
06aa7afa 1364{
0b8fb823 1365 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9caf0364 1366 struct cfg80211_bss_ies *ies;
3afc2167 1367 struct ieee80211_channel *channel;
7011ba58 1368 struct cfg80211_internal_bss tmp = {}, *res;
6eb18137 1369 int bss_type;
67af9811 1370 bool signal_valid;
06aa7afa
JK
1371
1372 if (WARN_ON(!wiphy))
1373 return NULL;
1374
22fe88d3 1375 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
6e19bc4b 1376 (data->signal < 0 || data->signal > 100)))
06aa7afa
JK
1377 return NULL;
1378
119f94a6
JM
1379 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1380 data->scan_width);
0172bb75
JB
1381 if (!channel)
1382 return NULL;
1383
9caf0364
JB
1384 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1385 tmp.pub.channel = channel;
6e19bc4b
DS
1386 tmp.pub.scan_width = data->scan_width;
1387 tmp.pub.signal = data->signal;
9caf0364
JB
1388 tmp.pub.beacon_interval = beacon_interval;
1389 tmp.pub.capability = capability;
6e19bc4b 1390 tmp.ts_boottime = data->boottime_ns;
0cd01efb
SS
1391 if (non_tx_data) {
1392 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1393 tmp.pub.bssid_index = non_tx_data->bssid_index;
1394 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1395 }
6e19bc4b 1396
34a6eddb 1397 /*
5bc8c1f2 1398 * If we do not know here whether the IEs are from a Beacon or Probe
34a6eddb
JM
1399 * Response frame, we need to pick one of the options and only use it
1400 * with the driver that does not provide the full Beacon/Probe Response
1401 * frame. Use Beacon frame pointer to avoid indicating that this should
50521aa8 1402 * override the IEs pointer should we have received an earlier
9caf0364 1403 * indication of Probe Response data.
34a6eddb 1404 */
0e227084 1405 ies = kzalloc(sizeof(*ies) + ielen, gfp);
9caf0364
JB
1406 if (!ies)
1407 return NULL;
1408 ies->len = ielen;
8cef2c9d 1409 ies->tsf = tsf;
0e227084 1410 ies->from_beacon = false;
9caf0364 1411 memcpy(ies->data, ie, ielen);
06aa7afa 1412
5bc8c1f2
JB
1413 switch (ftype) {
1414 case CFG80211_BSS_FTYPE_BEACON:
1415 ies->from_beacon = true;
925b5978 1416 /* fall through */
5bc8c1f2
JB
1417 case CFG80211_BSS_FTYPE_UNKNOWN:
1418 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1419 break;
1420 case CFG80211_BSS_FTYPE_PRESP:
1421 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1422 break;
1423 }
9caf0364 1424 rcu_assign_pointer(tmp.pub.ies, ies);
06aa7afa 1425
6e19bc4b 1426 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
67af9811 1427 wiphy->max_adj_channel_rssi_comp;
a3ce17d1
CT
1428 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
1429 jiffies);
06aa7afa
JK
1430 if (!res)
1431 return NULL;
1432
57fbcce3 1433 if (channel->band == NL80211_BAND_60GHZ) {
6eb18137
DL
1434 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1435 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1436 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1437 regulatory_hint_found_beacon(wiphy, channel, gfp);
1438 } else {
1439 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1440 regulatory_hint_found_beacon(wiphy, channel, gfp);
1441 }
06aa7afa 1442
0cd01efb 1443 if (non_tx_data && non_tx_data->tx_bss) {
0b8fb823
PX
1444 /* this is a nontransmitting bss, we need to add it to
1445 * transmitting bss' list if it is not there
1446 */
0cd01efb
SS
1447 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1448 &res->pub)) {
0b8fb823
PX
1449 if (__cfg80211_unlink_bss(rdev, res))
1450 rdev->bss_generation++;
1451 }
1452 }
1453
4ee3e063 1454 trace_cfg80211_return_bss(&res->pub);
06aa7afa
JK
1455 /* cfg80211_bss_update gives us a referenced result */
1456 return &res->pub;
1457}
06aa7afa 1458
fe806e49
SS
1459static const struct element
1460*cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
1461 const struct element *mbssid_elem,
1462 const struct element *sub_elem)
1463{
1464 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
1465 const struct element *next_mbssid;
1466 const struct element *next_sub;
1467
1468 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
1469 mbssid_end,
1470 ielen - (mbssid_end - ie));
1471
1472 /*
1473 * If is is not the last subelement in current MBSSID IE or there isn't
1474 * a next MBSSID IE - profile is complete.
1475 */
1476 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
1477 !next_mbssid)
1478 return NULL;
1479
1480 /* For any length error, just return NULL */
1481
1482 if (next_mbssid->datalen < 4)
1483 return NULL;
1484
1485 next_sub = (void *)&next_mbssid->data[1];
1486
1487 if (next_mbssid->data + next_mbssid->datalen <
1488 next_sub->data + next_sub->datalen)
1489 return NULL;
1490
1491 if (next_sub->id != 0 || next_sub->datalen < 2)
1492 return NULL;
1493
1494 /*
1495 * Check if the first element in the next sub element is a start
1496 * of a new profile
1497 */
1498 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
1499 NULL : next_mbssid;
1500}
1501
1502size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
1503 const struct element *mbssid_elem,
1504 const struct element *sub_elem,
5809a5d5 1505 u8 *merged_ie, size_t max_copy_len)
fe806e49
SS
1506{
1507 size_t copied_len = sub_elem->datalen;
1508 const struct element *next_mbssid;
1509
1510 if (sub_elem->datalen > max_copy_len)
1511 return 0;
1512
5809a5d5 1513 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
fe806e49
SS
1514
1515 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
1516 mbssid_elem,
1517 sub_elem))) {
1518 const struct element *next_sub = (void *)&next_mbssid->data[1];
1519
1520 if (copied_len + next_sub->datalen > max_copy_len)
1521 break;
5809a5d5 1522 memcpy(merged_ie + copied_len, next_sub->data,
fe806e49
SS
1523 next_sub->datalen);
1524 copied_len += next_sub->datalen;
1525 }
1526
1527 return copied_len;
1528}
1529EXPORT_SYMBOL(cfg80211_merge_profile);
1530
0b8fb823
PX
1531static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1532 struct cfg80211_inform_bss *data,
1533 enum cfg80211_bss_frame_type ftype,
1534 const u8 *bssid, u64 tsf,
1535 u16 beacon_interval, const u8 *ie,
1536 size_t ielen,
0cd01efb 1537 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823
PX
1538 gfp_t gfp)
1539{
1c8745f3
JB
1540 const u8 *mbssid_index_ie;
1541 const struct element *elem, *sub;
1542 size_t new_ie_len;
0b8fb823 1543 u8 new_bssid[ETH_ALEN];
fe806e49
SS
1544 u8 *new_ie, *profile;
1545 u64 seen_indices = 0;
0b8fb823
PX
1546 u16 capability;
1547 struct cfg80211_bss *bss;
1548
0cd01efb 1549 if (!non_tx_data)
0b8fb823
PX
1550 return;
1551 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1552 return;
213ed579
SS
1553 if (!wiphy->support_mbssid)
1554 return;
1555 if (wiphy->support_only_he_mbssid &&
1556 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1557 return;
0b8fb823 1558
0b8fb823
PX
1559 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1560 if (!new_ie)
1561 return;
1562
fe806e49
SS
1563 profile = kmalloc(ielen, gfp);
1564 if (!profile)
1565 goto out;
1566
1c8745f3
JB
1567 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1568 if (elem->datalen < 4)
1569 continue;
1570 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
fe806e49
SS
1571 u8 profile_len;
1572
1c8745f3 1573 if (sub->id != 0 || sub->datalen < 4) {
0b8fb823
PX
1574 /* not a valid BSS profile */
1575 continue;
1576 }
1577
1c8745f3
JB
1578 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1579 sub->data[1] != 2) {
0b8fb823
PX
1580 /* The first element within the Nontransmitted
1581 * BSSID Profile is not the Nontransmitted
1582 * BSSID Capability element.
1583 */
1584 continue;
1585 }
1586
fe806e49
SS
1587 memset(profile, 0, ielen);
1588 profile_len = cfg80211_merge_profile(ie, ielen,
1589 elem,
1590 sub,
5809a5d5 1591 profile,
fe806e49
SS
1592 ielen);
1593
0b8fb823
PX
1594 /* found a Nontransmitted BSSID Profile */
1595 mbssid_index_ie = cfg80211_find_ie
1596 (WLAN_EID_MULTI_BSSID_IDX,
fe806e49 1597 profile, profile_len);
0b8fb823 1598 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
fe806e49
SS
1599 mbssid_index_ie[2] == 0 ||
1600 mbssid_index_ie[2] > 46) {
0b8fb823
PX
1601 /* No valid Multiple BSSID-Index element */
1602 continue;
1603 }
1604
ebb3ca3b 1605 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
fe806e49
SS
1606 /* We don't support legacy split of a profile */
1607 net_dbg_ratelimited("Partial info for BSSID index %d\n",
1608 mbssid_index_ie[2]);
1609
ebb3ca3b 1610 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
fe806e49 1611
0cd01efb
SS
1612 non_tx_data->bssid_index = mbssid_index_ie[2];
1613 non_tx_data->max_bssid_indicator = elem->data[0];
1614
1615 cfg80211_gen_new_bssid(bssid,
1616 non_tx_data->max_bssid_indicator,
1617 non_tx_data->bssid_index,
0b8fb823
PX
1618 new_bssid);
1619 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
fe806e49
SS
1620 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
1621 profile,
1622 profile_len, new_ie,
0b8fb823
PX
1623 gfp);
1624 if (!new_ie_len)
1625 continue;
1626
fe806e49 1627 capability = get_unaligned_le16(profile + 2);
0b8fb823
PX
1628 bss = cfg80211_inform_single_bss_data(wiphy, data,
1629 ftype,
1630 new_bssid, tsf,
1631 capability,
1632 beacon_interval,
1633 new_ie,
1634 new_ie_len,
0cd01efb
SS
1635 non_tx_data,
1636 gfp);
0b8fb823
PX
1637 if (!bss)
1638 break;
1639 cfg80211_put_bss(wiphy, bss);
1640 }
0b8fb823
PX
1641 }
1642
fe806e49 1643out:
0b8fb823 1644 kfree(new_ie);
fe806e49 1645 kfree(profile);
0b8fb823
PX
1646}
1647
2a519311 1648struct cfg80211_bss *
0b8fb823
PX
1649cfg80211_inform_bss_data(struct wiphy *wiphy,
1650 struct cfg80211_inform_bss *data,
1651 enum cfg80211_bss_frame_type ftype,
1652 const u8 *bssid, u64 tsf, u16 capability,
1653 u16 beacon_interval, const u8 *ie, size_t ielen,
1654 gfp_t gfp)
1655{
1656 struct cfg80211_bss *res;
0cd01efb 1657 struct cfg80211_non_tx_bss non_tx_data;
0b8fb823
PX
1658
1659 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1660 capability, beacon_interval, ie,
1661 ielen, NULL, gfp);
0cd01efb 1662 non_tx_data.tx_bss = res;
0b8fb823 1663 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
0cd01efb
SS
1664 beacon_interval, ie, ielen, &non_tx_data,
1665 gfp);
0b8fb823
PX
1666 return res;
1667}
1668EXPORT_SYMBOL(cfg80211_inform_bss_data);
1669
1670static void
1671cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1672 struct cfg80211_inform_bss *data,
1673 struct ieee80211_mgmt *mgmt, size_t len,
0cd01efb 1674 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823
PX
1675 gfp_t gfp)
1676{
1677 enum cfg80211_bss_frame_type ftype;
1678 const u8 *ie = mgmt->u.probe_resp.variable;
1679 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1680 u.probe_resp.variable);
1681
1682 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1683 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1684
1685 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1686 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1687 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
0cd01efb 1688 ie, ielen, non_tx_data, gfp);
0b8fb823
PX
1689}
1690
1691static void
1692cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
7011ba58 1693 struct cfg80211_bss *nontrans_bss,
0b8fb823
PX
1694 struct ieee80211_mgmt *mgmt, size_t len,
1695 gfp_t gfp)
1696{
1697 u8 *ie, *new_ie, *pos;
1698 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1699 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1700 u.probe_resp.variable);
1701 size_t new_ie_len;
1702 struct cfg80211_bss_ies *new_ies;
1703 const struct cfg80211_bss_ies *old;
1704 u8 cpy_len;
1705
1706 ie = mgmt->u.probe_resp.variable;
1707
1708 new_ie_len = ielen;
1709 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1710 if (!trans_ssid)
1711 return;
1712 new_ie_len -= trans_ssid[1];
1713 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
89ce76e6
JB
1714 /*
1715 * It's not valid to have the MBSSID element before SSID
1716 * ignore if that happens - the code below assumes it is
1717 * after (while copying things inbetween).
1718 */
1719 if (!mbssid || mbssid < trans_ssid)
0b8fb823
PX
1720 return;
1721 new_ie_len -= mbssid[1];
1722 rcu_read_lock();
7011ba58 1723 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
0b8fb823
PX
1724 if (!nontrans_ssid) {
1725 rcu_read_unlock();
1726 return;
1727 }
1728 new_ie_len += nontrans_ssid[1];
1729 rcu_read_unlock();
1730
1731 /* generate new ie for nontrans BSS
1732 * 1. replace SSID with nontrans BSS' SSID
1733 * 2. skip MBSSID IE
1734 */
1735 new_ie = kzalloc(new_ie_len, gfp);
1736 if (!new_ie)
1737 return;
1738 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp);
bede8d29
SS
1739 if (!new_ies)
1740 goto out_free;
0b8fb823
PX
1741
1742 pos = new_ie;
1743
1744 /* copy the nontransmitted SSID */
1745 cpy_len = nontrans_ssid[1] + 2;
1746 memcpy(pos, nontrans_ssid, cpy_len);
1747 pos += cpy_len;
1748 /* copy the IEs between SSID and MBSSID */
1749 cpy_len = trans_ssid[1] + 2;
1750 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1751 pos += (mbssid - (trans_ssid + cpy_len));
1752 /* copy the IEs after MBSSID */
1753 cpy_len = mbssid[1] + 2;
1754 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1755
1756 /* update ie */
1757 new_ies->len = new_ie_len;
1758 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1759 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1760 memcpy(new_ies->data, new_ie, new_ie_len);
1761 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
7011ba58
SS
1762 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1763 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1764 rcu_assign_pointer(nontrans_bss->ies, new_ies);
0b8fb823
PX
1765 if (old)
1766 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1767 } else {
7011ba58
SS
1768 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1769 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1770 rcu_assign_pointer(nontrans_bss->ies, new_ies);
0b8fb823
PX
1771 if (old)
1772 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1773 }
bede8d29
SS
1774
1775out_free:
1776 kfree(new_ie);
0b8fb823 1777}
6e19bc4b 1778
0b8fb823
PX
1779/* cfg80211_inform_bss_width_frame helper */
1780static struct cfg80211_bss *
1781cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1782 struct cfg80211_inform_bss *data,
1783 struct ieee80211_mgmt *mgmt, size_t len,
0cd01efb 1784 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823 1785 gfp_t gfp)
2a519311 1786{
9caf0364
JB
1787 struct cfg80211_internal_bss tmp = {}, *res;
1788 struct cfg80211_bss_ies *ies;
3afc2167 1789 struct ieee80211_channel *channel;
67af9811 1790 bool signal_valid;
2a519311
JB
1791 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1792 u.probe_resp.variable);
6eb18137 1793 int bss_type;
bef9bacc 1794
0172bb75
JB
1795 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1796 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1797
6e19bc4b 1798 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
4ee3e063 1799
bef9bacc
MK
1800 if (WARN_ON(!mgmt))
1801 return NULL;
1802
1803 if (WARN_ON(!wiphy))
1804 return NULL;
2a519311 1805
22fe88d3 1806 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
6e19bc4b 1807 (data->signal < 0 || data->signal > 100)))
2a519311
JB
1808 return NULL;
1809
bef9bacc 1810 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
2a519311
JB
1811 return NULL;
1812
0172bb75 1813 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
119f94a6 1814 ielen, data->chan, data->scan_width);
0172bb75
JB
1815 if (!channel)
1816 return NULL;
1817
0e227084 1818 ies = kzalloc(sizeof(*ies) + ielen, gfp);
9caf0364 1819 if (!ies)
2a519311 1820 return NULL;
9caf0364 1821 ies->len = ielen;
8cef2c9d 1822 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
0e227084 1823 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
9caf0364 1824 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
2a519311 1825
9caf0364
JB
1826 if (ieee80211_is_probe_resp(mgmt->frame_control))
1827 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1828 else
1829 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1830 rcu_assign_pointer(tmp.pub.ies, ies);
505a2e88 1831
9caf0364
JB
1832 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1833 tmp.pub.channel = channel;
6e19bc4b
DS
1834 tmp.pub.scan_width = data->scan_width;
1835 tmp.pub.signal = data->signal;
9caf0364
JB
1836 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1837 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
6e19bc4b 1838 tmp.ts_boottime = data->boottime_ns;
1d76250b 1839 tmp.parent_tsf = data->parent_tsf;
983dafaa
SD
1840 tmp.pub.chains = data->chains;
1841 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1d76250b 1842 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
0cd01efb
SS
1843 if (non_tx_data) {
1844 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1845 tmp.pub.bssid_index = non_tx_data->bssid_index;
1846 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1847 }
9caf0364 1848
6e19bc4b 1849 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
67af9811 1850 wiphy->max_adj_channel_rssi_comp;
a3ce17d1
CT
1851 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
1852 jiffies);
2a519311
JB
1853 if (!res)
1854 return NULL;
1855
57fbcce3 1856 if (channel->band == NL80211_BAND_60GHZ) {
6eb18137
DL
1857 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1858 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1859 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1860 regulatory_hint_found_beacon(wiphy, channel, gfp);
1861 } else {
1862 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1863 regulatory_hint_found_beacon(wiphy, channel, gfp);
1864 }
e38f8a7a 1865
4ee3e063 1866 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
1867 /* cfg80211_bss_update gives us a referenced result */
1868 return &res->pub;
1869}
0b8fb823
PX
1870
1871struct cfg80211_bss *
1872cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1873 struct cfg80211_inform_bss *data,
1874 struct ieee80211_mgmt *mgmt, size_t len,
1875 gfp_t gfp)
1876{
7011ba58 1877 struct cfg80211_bss *res, *tmp_bss;
0b8fb823
PX
1878 const u8 *ie = mgmt->u.probe_resp.variable;
1879 const struct cfg80211_bss_ies *ies1, *ies2;
1880 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1881 u.probe_resp.variable);
0cd01efb 1882 struct cfg80211_non_tx_bss non_tx_data;
0b8fb823
PX
1883
1884 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1885 len, NULL, gfp);
213ed579
SS
1886 if (!res || !wiphy->support_mbssid ||
1887 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1888 return res;
1889 if (wiphy->support_only_he_mbssid &&
1890 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
0b8fb823
PX
1891 return res;
1892
0cd01efb 1893 non_tx_data.tx_bss = res;
0b8fb823 1894 /* process each non-transmitting bss */
0cd01efb
SS
1895 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1896 &non_tx_data, gfp);
0b8fb823
PX
1897
1898 /* check if the res has other nontransmitting bss which is not
1899 * in MBSSID IE
1900 */
1901 ies1 = rcu_access_pointer(res->ies);
0b8fb823
PX
1902
1903 /* go through nontrans_list, if the timestamp of the BSS is
1904 * earlier than the timestamp of the transmitting BSS then
1905 * update it
1906 */
7011ba58 1907 list_for_each_entry(tmp_bss, &res->nontrans_list,
0b8fb823 1908 nontrans_list) {
7011ba58 1909 ies2 = rcu_access_pointer(tmp_bss->ies);
0b8fb823
PX
1910 if (ies2->tsf < ies1->tsf)
1911 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1912 mgmt, len, gfp);
1913 }
1914
1915 return res;
1916}
6e19bc4b 1917EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2a519311 1918
5b112d3d 1919void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
4c0c0b75 1920{
f26cbf40 1921 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
4c0c0b75
JB
1922 struct cfg80211_internal_bss *bss;
1923
1924 if (!pub)
1925 return;
1926
1927 bss = container_of(pub, struct cfg80211_internal_bss, pub);
776b3580 1928
1b8ec87a
ZG
1929 spin_lock_bh(&rdev->bss_lock);
1930 bss_ref_get(rdev, bss);
1931 spin_unlock_bh(&rdev->bss_lock);
4c0c0b75
JB
1932}
1933EXPORT_SYMBOL(cfg80211_ref_bss);
1934
5b112d3d 1935void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2a519311 1936{
f26cbf40 1937 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2a519311
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_put(rdev, bss);
1947 spin_unlock_bh(&rdev->bss_lock);
2a519311
JB
1948}
1949EXPORT_SYMBOL(cfg80211_put_bss);
1950
d491af19
JB
1951void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1952{
f26cbf40 1953 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
7011ba58
SS
1954 struct cfg80211_internal_bss *bss, *tmp1;
1955 struct cfg80211_bss *nontrans_bss, *tmp;
d491af19
JB
1956
1957 if (WARN_ON(!pub))
1958 return;
1959
1960 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1961
1b8ec87a 1962 spin_lock_bh(&rdev->bss_lock);
7011ba58
SS
1963 if (list_empty(&bss->list))
1964 goto out;
1965
1966 list_for_each_entry_safe(nontrans_bss, tmp,
1967 &pub->nontrans_list,
1968 nontrans_list) {
1969 tmp1 = container_of(nontrans_bss,
1970 struct cfg80211_internal_bss, pub);
1971 if (__cfg80211_unlink_bss(rdev, tmp1))
1b8ec87a 1972 rdev->bss_generation++;
3207390a 1973 }
7011ba58
SS
1974
1975 if (__cfg80211_unlink_bss(rdev, bss))
1976 rdev->bss_generation++;
1977out:
1b8ec87a 1978 spin_unlock_bh(&rdev->bss_lock);
d491af19
JB
1979}
1980EXPORT_SYMBOL(cfg80211_unlink_bss);
1981
4770c8f9
IP
1982void cfg80211_bss_iter(struct wiphy *wiphy,
1983 struct cfg80211_chan_def *chandef,
1984 void (*iter)(struct wiphy *wiphy,
1985 struct cfg80211_bss *bss,
1986 void *data),
1987 void *iter_data)
1988{
1989 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1990 struct cfg80211_internal_bss *bss;
1991
1992 spin_lock_bh(&rdev->bss_lock);
1993
1994 list_for_each_entry(bss, &rdev->bss_list, list) {
1995 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel))
1996 iter(wiphy, &bss->pub, iter_data);
1997 }
1998
1999 spin_unlock_bh(&rdev->bss_lock);
2000}
2001EXPORT_SYMBOL(cfg80211_bss_iter);
2002
3d23e349 2003#ifdef CONFIG_CFG80211_WEXT
9f419f38
JB
2004static struct cfg80211_registered_device *
2005cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2006{
5fe231e8 2007 struct cfg80211_registered_device *rdev;
9f419f38
JB
2008 struct net_device *dev;
2009
5fe231e8
JB
2010 ASSERT_RTNL();
2011
9f419f38
JB
2012 dev = dev_get_by_index(net, ifindex);
2013 if (!dev)
5fe231e8
JB
2014 return ERR_PTR(-ENODEV);
2015 if (dev->ieee80211_ptr)
f26cbf40 2016 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
5fe231e8 2017 else
9f419f38
JB
2018 rdev = ERR_PTR(-ENODEV);
2019 dev_put(dev);
9f419f38
JB
2020 return rdev;
2021}
2022
2a519311
JB
2023int cfg80211_wext_siwscan(struct net_device *dev,
2024 struct iw_request_info *info,
2025 union iwreq_data *wrqu, char *extra)
2026{
2027 struct cfg80211_registered_device *rdev;
2028 struct wiphy *wiphy;
2029 struct iw_scan_req *wreq = NULL;
65486c8b 2030 struct cfg80211_scan_request *creq = NULL;
2a519311 2031 int i, err, n_channels = 0;
57fbcce3 2032 enum nl80211_band band;
2a519311
JB
2033
2034 if (!netif_running(dev))
2035 return -ENETDOWN;
2036
b2e3abdc
HS
2037 if (wrqu->data.length == sizeof(struct iw_scan_req))
2038 wreq = (struct iw_scan_req *)extra;
2039
463d0183 2040 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
2041
2042 if (IS_ERR(rdev))
2043 return PTR_ERR(rdev);
2044
f9d15d16 2045 if (rdev->scan_req || rdev->scan_msg) {
2a519311
JB
2046 err = -EBUSY;
2047 goto out;
2048 }
2049
2050 wiphy = &rdev->wiphy;
2051
b2e3abdc
HS
2052 /* Determine number of channels, needed to allocate creq */
2053 if (wreq && wreq->num_channels)
2054 n_channels = wreq->num_channels;
bdfbec2d
IP
2055 else
2056 n_channels = ieee80211_get_num_supported_channels(wiphy);
2a519311
JB
2057
2058 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2059 n_channels * sizeof(void *),
2060 GFP_ATOMIC);
2061 if (!creq) {
2062 err = -ENOMEM;
2063 goto out;
2064 }
2065
2066 creq->wiphy = wiphy;
fd014284 2067 creq->wdev = dev->ieee80211_ptr;
5ba63533
JB
2068 /* SSIDs come after channels */
2069 creq->ssids = (void *)&creq->channels[n_channels];
2a519311
JB
2070 creq->n_channels = n_channels;
2071 creq->n_ssids = 1;
15d6030b 2072 creq->scan_start = jiffies;
2a519311 2073
b2e3abdc 2074 /* translate "Scan on frequencies" request */
2a519311 2075 i = 0;
57fbcce3 2076 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2a519311 2077 int j;
584991dc 2078
2a519311
JB
2079 if (!wiphy->bands[band])
2080 continue;
584991dc 2081
2a519311 2082 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
2083 /* ignore disabled channels */
2084 if (wiphy->bands[band]->channels[j].flags &
2085 IEEE80211_CHAN_DISABLED)
2086 continue;
b2e3abdc
HS
2087
2088 /* If we have a wireless request structure and the
2089 * wireless request specifies frequencies, then search
2090 * for the matching hardware channel.
2091 */
2092 if (wreq && wreq->num_channels) {
2093 int k;
2094 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2095 for (k = 0; k < wreq->num_channels; k++) {
96998e3a
ZG
2096 struct iw_freq *freq =
2097 &wreq->channel_list[k];
2098 int wext_freq =
2099 cfg80211_wext_freq(freq);
2100
b2e3abdc
HS
2101 if (wext_freq == wiphy_freq)
2102 goto wext_freq_found;
2103 }
2104 goto wext_freq_not_found;
2105 }
2106
2107 wext_freq_found:
2a519311
JB
2108 creq->channels[i] = &wiphy->bands[band]->channels[j];
2109 i++;
b2e3abdc 2110 wext_freq_not_found: ;
2a519311
JB
2111 }
2112 }
8862dc5f
HS
2113 /* No channels found? */
2114 if (!i) {
2115 err = -EINVAL;
2116 goto out;
2117 }
2a519311 2118
b2e3abdc
HS
2119 /* Set real number of channels specified in creq->channels[] */
2120 creq->n_channels = i;
2a519311 2121
b2e3abdc
HS
2122 /* translate "Scan for SSID" request */
2123 if (wreq) {
2a519311 2124 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
65486c8b
JB
2125 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2126 err = -EINVAL;
2127 goto out;
2128 }
2a519311
JB
2129 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2130 creq->ssids[0].ssid_len = wreq->essid_len;
2131 }
2132 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2133 creq->n_ssids = 0;
2134 }
2135
57fbcce3 2136 for (i = 0; i < NUM_NL80211_BANDS; i++)
a401d2bb
JB
2137 if (wiphy->bands[i])
2138 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
34850ab2 2139
818965d3
JM
2140 eth_broadcast_addr(creq->bssid);
2141
2a519311 2142 rdev->scan_req = creq;
e35e4d28 2143 err = rdev_scan(rdev, creq);
2a519311
JB
2144 if (err) {
2145 rdev->scan_req = NULL;
65486c8b 2146 /* creq will be freed below */
463d0183 2147 } else {
fd014284 2148 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
65486c8b
JB
2149 /* creq now owned by driver */
2150 creq = NULL;
463d0183
JB
2151 dev_hold(dev);
2152 }
2a519311 2153 out:
65486c8b 2154 kfree(creq);
2a519311
JB
2155 return err;
2156}
2afe38d1 2157EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2a519311 2158
76a70e9c
JM
2159static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2160 const struct cfg80211_bss_ies *ies,
2161 char *current_ev, char *end_buf)
2a519311 2162{
9caf0364 2163 const u8 *pos, *end, *next;
2a519311
JB
2164 struct iw_event iwe;
2165
9caf0364 2166 if (!ies)
76a70e9c 2167 return current_ev;
2a519311
JB
2168
2169 /*
2170 * If needed, fragment the IEs buffer (at IE boundaries) into short
2171 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2172 */
9caf0364
JB
2173 pos = ies->data;
2174 end = pos + ies->len;
2a519311
JB
2175
2176 while (end - pos > IW_GENERIC_IE_MAX) {
2177 next = pos + 2 + pos[1];
2178 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2179 next = next + 2 + next[1];
2180
2181 memset(&iwe, 0, sizeof(iwe));
2182 iwe.cmd = IWEVGENIE;
2183 iwe.u.data.length = next - pos;
76a70e9c
JM
2184 current_ev = iwe_stream_add_point_check(info, current_ev,
2185 end_buf, &iwe,
2186 (void *)pos);
2187 if (IS_ERR(current_ev))
2188 return current_ev;
2a519311
JB
2189 pos = next;
2190 }
2191
2192 if (end > pos) {
2193 memset(&iwe, 0, sizeof(iwe));
2194 iwe.cmd = IWEVGENIE;
2195 iwe.u.data.length = end - pos;
76a70e9c
JM
2196 current_ev = iwe_stream_add_point_check(info, current_ev,
2197 end_buf, &iwe,
2198 (void *)pos);
2199 if (IS_ERR(current_ev))
2200 return current_ev;
2a519311 2201 }
76a70e9c
JM
2202
2203 return current_ev;
2a519311
JB
2204}
2205
2a519311 2206static char *
77965c97
JB
2207ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2208 struct cfg80211_internal_bss *bss, char *current_ev,
2209 char *end_buf)
2a519311 2210{
9caf0364 2211 const struct cfg80211_bss_ies *ies;
2a519311 2212 struct iw_event iwe;
9caf0364 2213 const u8 *ie;
76a70e9c
JM
2214 u8 buf[50];
2215 u8 *cfg, *p, *tmp;
9caf0364 2216 int rem, i, sig;
2a519311
JB
2217 bool ismesh = false;
2218
2219 memset(&iwe, 0, sizeof(iwe));
2220 iwe.cmd = SIOCGIWAP;
2221 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2222 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
76a70e9c
JM
2223 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2224 IW_EV_ADDR_LEN);
2225 if (IS_ERR(current_ev))
2226 return current_ev;
2a519311
JB
2227
2228 memset(&iwe, 0, sizeof(iwe));
2229 iwe.cmd = SIOCGIWFREQ;
2230 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2231 iwe.u.freq.e = 0;
76a70e9c
JM
2232 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2233 IW_EV_FREQ_LEN);
2234 if (IS_ERR(current_ev))
2235 return current_ev;
2a519311
JB
2236
2237 memset(&iwe, 0, sizeof(iwe));
2238 iwe.cmd = SIOCGIWFREQ;
2239 iwe.u.freq.m = bss->pub.channel->center_freq;
2240 iwe.u.freq.e = 6;
76a70e9c
JM
2241 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2242 IW_EV_FREQ_LEN);
2243 if (IS_ERR(current_ev))
2244 return current_ev;
2a519311 2245
77965c97 2246 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2a519311
JB
2247 memset(&iwe, 0, sizeof(iwe));
2248 iwe.cmd = IWEVQUAL;
2249 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2250 IW_QUAL_NOISE_INVALID |
a77b8552 2251 IW_QUAL_QUAL_UPDATED;
77965c97 2252 switch (wiphy->signal_type) {
2a519311 2253 case CFG80211_SIGNAL_TYPE_MBM:
a77b8552
JB
2254 sig = bss->pub.signal / 100;
2255 iwe.u.qual.level = sig;
2a519311 2256 iwe.u.qual.updated |= IW_QUAL_DBM;
a77b8552
JB
2257 if (sig < -110) /* rather bad */
2258 sig = -110;
2259 else if (sig > -40) /* perfect */
2260 sig = -40;
2261 /* will give a range of 0 .. 70 */
2262 iwe.u.qual.qual = sig + 110;
2a519311
JB
2263 break;
2264 case CFG80211_SIGNAL_TYPE_UNSPEC:
2265 iwe.u.qual.level = bss->pub.signal;
a77b8552
JB
2266 /* will give range 0 .. 100 */
2267 iwe.u.qual.qual = bss->pub.signal;
2a519311
JB
2268 break;
2269 default:
2270 /* not reached */
2271 break;
2272 }
76a70e9c
JM
2273 current_ev = iwe_stream_add_event_check(info, current_ev,
2274 end_buf, &iwe,
2275 IW_EV_QUAL_LEN);
2276 if (IS_ERR(current_ev))
2277 return current_ev;
2a519311
JB
2278 }
2279
2280 memset(&iwe, 0, sizeof(iwe));
2281 iwe.cmd = SIOCGIWENCODE;
2282 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2283 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2284 else
2285 iwe.u.data.flags = IW_ENCODE_DISABLED;
2286 iwe.u.data.length = 0;
76a70e9c
JM
2287 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2288 &iwe, "");
2289 if (IS_ERR(current_ev))
2290 return current_ev;
2a519311 2291
9caf0364
JB
2292 rcu_read_lock();
2293 ies = rcu_dereference(bss->pub.ies);
83c7aa1a
JB
2294 rem = ies->len;
2295 ie = ies->data;
9caf0364 2296
83c7aa1a 2297 while (rem >= 2) {
2a519311
JB
2298 /* invalid data */
2299 if (ie[1] > rem - 2)
2300 break;
2301
2302 switch (ie[0]) {
2303 case WLAN_EID_SSID:
2304 memset(&iwe, 0, sizeof(iwe));
2305 iwe.cmd = SIOCGIWESSID;
2306 iwe.u.data.length = ie[1];
2307 iwe.u.data.flags = 1;
76a70e9c
JM
2308 current_ev = iwe_stream_add_point_check(info,
2309 current_ev,
2310 end_buf, &iwe,
2311 (u8 *)ie + 2);
2312 if (IS_ERR(current_ev))
2313 goto unlock;
2a519311
JB
2314 break;
2315 case WLAN_EID_MESH_ID:
2316 memset(&iwe, 0, sizeof(iwe));
2317 iwe.cmd = SIOCGIWESSID;
2318 iwe.u.data.length = ie[1];
2319 iwe.u.data.flags = 1;
76a70e9c
JM
2320 current_ev = iwe_stream_add_point_check(info,
2321 current_ev,
2322 end_buf, &iwe,
2323 (u8 *)ie + 2);
2324 if (IS_ERR(current_ev))
2325 goto unlock;
2a519311
JB
2326 break;
2327 case WLAN_EID_MESH_CONFIG:
2328 ismesh = true;
136cfa28 2329 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2a519311 2330 break;
9caf0364 2331 cfg = (u8 *)ie + 2;
2a519311
JB
2332 memset(&iwe, 0, sizeof(iwe));
2333 iwe.cmd = IWEVCUSTOM;
76aa5e70
RP
2334 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2335 "0x%02X", cfg[0]);
2a519311 2336 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2337 current_ev = iwe_stream_add_point_check(info,
2338 current_ev,
2339 end_buf,
2340 &iwe, buf);
2341 if (IS_ERR(current_ev))
2342 goto unlock;
76aa5e70
RP
2343 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2344 cfg[1]);
2a519311 2345 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2346 current_ev = iwe_stream_add_point_check(info,
2347 current_ev,
2348 end_buf,
2349 &iwe, buf);
2350 if (IS_ERR(current_ev))
2351 goto unlock;
76aa5e70
RP
2352 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2353 cfg[2]);
2a519311 2354 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2355 current_ev = iwe_stream_add_point_check(info,
2356 current_ev,
2357 end_buf,
2358 &iwe, buf);
2359 if (IS_ERR(current_ev))
2360 goto unlock;
76aa5e70 2361 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2a519311 2362 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2363 current_ev = iwe_stream_add_point_check(info,
2364 current_ev,
2365 end_buf,
2366 &iwe, buf);
2367 if (IS_ERR(current_ev))
2368 goto unlock;
76aa5e70
RP
2369 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2370 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2371 current_ev = iwe_stream_add_point_check(info,
2372 current_ev,
2373 end_buf,
2374 &iwe, buf);
2375 if (IS_ERR(current_ev))
2376 goto unlock;
76aa5e70
RP
2377 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2378 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2379 current_ev = iwe_stream_add_point_check(info,
2380 current_ev,
2381 end_buf,
2382 &iwe, buf);
2383 if (IS_ERR(current_ev))
2384 goto unlock;
76aa5e70 2385 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2a519311 2386 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2387 current_ev = iwe_stream_add_point_check(info,
2388 current_ev,
2389 end_buf,
2390 &iwe, buf);
2391 if (IS_ERR(current_ev))
2392 goto unlock;
2a519311
JB
2393 break;
2394 case WLAN_EID_SUPP_RATES:
2395 case WLAN_EID_EXT_SUPP_RATES:
2396 /* display all supported rates in readable format */
2397 p = current_ev + iwe_stream_lcp_len(info);
2398
2399 memset(&iwe, 0, sizeof(iwe));
2400 iwe.cmd = SIOCGIWRATE;
2401 /* Those two flags are ignored... */
2402 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2403
2404 for (i = 0; i < ie[1]; i++) {
2405 iwe.u.bitrate.value =
2406 ((ie[i + 2] & 0x7f) * 500000);
76a70e9c 2407 tmp = p;
2a519311 2408 p = iwe_stream_add_value(info, current_ev, p,
76a70e9c
JM
2409 end_buf, &iwe,
2410 IW_EV_PARAM_LEN);
2411 if (p == tmp) {
2412 current_ev = ERR_PTR(-E2BIG);
2413 goto unlock;
2414 }
2a519311
JB
2415 }
2416 current_ev = p;
2417 break;
2418 }
2419 rem -= ie[1] + 2;
2420 ie += ie[1] + 2;
2421 }
2422
f64f9e71
JP
2423 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2424 ismesh) {
2a519311
JB
2425 memset(&iwe, 0, sizeof(iwe));
2426 iwe.cmd = SIOCGIWMODE;
2427 if (ismesh)
2428 iwe.u.mode = IW_MODE_MESH;
2429 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2430 iwe.u.mode = IW_MODE_MASTER;
2431 else
2432 iwe.u.mode = IW_MODE_ADHOC;
76a70e9c
JM
2433 current_ev = iwe_stream_add_event_check(info, current_ev,
2434 end_buf, &iwe,
2435 IW_EV_UINT_LEN);
2436 if (IS_ERR(current_ev))
2437 goto unlock;
2a519311
JB
2438 }
2439
76a70e9c
JM
2440 memset(&iwe, 0, sizeof(iwe));
2441 iwe.cmd = IWEVCUSTOM;
2442 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2443 iwe.u.data.length = strlen(buf);
2444 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2445 &iwe, buf);
2446 if (IS_ERR(current_ev))
2447 goto unlock;
2448 memset(&iwe, 0, sizeof(iwe));
2449 iwe.cmd = IWEVCUSTOM;
2450 sprintf(buf, " Last beacon: %ums ago",
2451 elapsed_jiffies_msecs(bss->ts));
2452 iwe.u.data.length = strlen(buf);
2453 current_ev = iwe_stream_add_point_check(info, current_ev,
2454 end_buf, &iwe, buf);
2455 if (IS_ERR(current_ev))
2456 goto unlock;
2457
2458 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2459
2460 unlock:
9caf0364 2461 rcu_read_unlock();
2a519311
JB
2462 return current_ev;
2463}
2464
2465
1b8ec87a 2466static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2a519311
JB
2467 struct iw_request_info *info,
2468 char *buf, size_t len)
2469{
2470 char *current_ev = buf;
2471 char *end_buf = buf + len;
2472 struct cfg80211_internal_bss *bss;
76a70e9c 2473 int err = 0;
2a519311 2474
1b8ec87a
ZG
2475 spin_lock_bh(&rdev->bss_lock);
2476 cfg80211_bss_expire(rdev);
2a519311 2477
1b8ec87a 2478 list_for_each_entry(bss, &rdev->bss_list, list) {
2a519311 2479 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
76a70e9c
JM
2480 err = -E2BIG;
2481 break;
2a519311 2482 }
1b8ec87a 2483 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
77965c97 2484 current_ev, end_buf);
76a70e9c
JM
2485 if (IS_ERR(current_ev)) {
2486 err = PTR_ERR(current_ev);
2487 break;
2488 }
2a519311 2489 }
1b8ec87a 2490 spin_unlock_bh(&rdev->bss_lock);
76a70e9c
JM
2491
2492 if (err)
2493 return err;
2a519311
JB
2494 return current_ev - buf;
2495}
2496
2497
2498int cfg80211_wext_giwscan(struct net_device *dev,
2499 struct iw_request_info *info,
2500 struct iw_point *data, char *extra)
2501{
2502 struct cfg80211_registered_device *rdev;
2503 int res;
2504
2505 if (!netif_running(dev))
2506 return -ENETDOWN;
2507
463d0183 2508 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
2509
2510 if (IS_ERR(rdev))
2511 return PTR_ERR(rdev);
2512
f9d15d16 2513 if (rdev->scan_req || rdev->scan_msg)
5fe231e8 2514 return -EAGAIN;
2a519311
JB
2515
2516 res = ieee80211_scan_results(rdev, info, extra, data->length);
2517 data->length = 0;
2518 if (res >= 0) {
2519 data->length = res;
2520 res = 0;
2521 }
2522
2a519311
JB
2523 return res;
2524}
2afe38d1 2525EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
2a519311 2526#endif