mac80211: do not call driver wake_tx_queue op during reconfig
[linux-2.6-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
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
0b8fb823
PX
182static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
183 const u8 *subelement, size_t subie_len,
184 u8 *new_ie, gfp_t gfp)
185{
186 u8 *pos, *tmp;
187 const u8 *tmp_old, *tmp_new;
188 u8 *sub_copy;
189
190 /* copy subelement as we need to change its content to
191 * mark an ie after it is processed.
192 */
193 sub_copy = kmalloc(subie_len, gfp);
194 if (!sub_copy)
195 return 0;
196 memcpy(sub_copy, subelement, subie_len);
197
198 pos = &new_ie[0];
199
200 /* set new ssid */
201 tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
202 if (tmp_new) {
203 memcpy(pos, tmp_new, tmp_new[1] + 2);
204 pos += (tmp_new[1] + 2);
205 }
206
207 /* go through IEs in ie (skip SSID) and subelement,
208 * merge them into new_ie
209 */
210 tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
211 tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
212
213 while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
214 if (tmp_old[0] == 0) {
215 tmp_old++;
216 continue;
217 }
218
c17fe043
SS
219 if (tmp_old[0] == WLAN_EID_EXTENSION)
220 tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
221 subie_len);
222 else
223 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
224 subie_len);
225
0b8fb823
PX
226 if (!tmp) {
227 /* ie in old ie but not in subelement */
228 if (tmp_old[0] != WLAN_EID_MULTIPLE_BSSID) {
229 memcpy(pos, tmp_old, tmp_old[1] + 2);
230 pos += tmp_old[1] + 2;
231 }
232 } else {
233 /* ie in transmitting ie also in subelement,
234 * copy from subelement and flag the ie in subelement
c17fe043
SS
235 * as copied (by setting eid field to WLAN_EID_SSID,
236 * which is skipped anyway).
237 * For vendor ie, compare OUI + type + subType to
0b8fb823
PX
238 * determine if they are the same ie.
239 */
240 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
241 if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
242 /* same vendor ie, copy from
243 * subelement
244 */
245 memcpy(pos, tmp, tmp[1] + 2);
246 pos += tmp[1] + 2;
c17fe043 247 tmp[0] = WLAN_EID_SSID;
0b8fb823
PX
248 } else {
249 memcpy(pos, tmp_old, tmp_old[1] + 2);
250 pos += tmp_old[1] + 2;
251 }
252 } else {
253 /* copy ie from subelement into new ie */
254 memcpy(pos, tmp, tmp[1] + 2);
255 pos += tmp[1] + 2;
c17fe043 256 tmp[0] = WLAN_EID_SSID;
0b8fb823
PX
257 }
258 }
259
260 if (tmp_old + tmp_old[1] + 2 - ie == ielen)
261 break;
262
263 tmp_old += tmp_old[1] + 2;
264 }
265
266 /* go through subelement again to check if there is any ie not
267 * copied to new ie, skip ssid, capability, bssid-index ie
268 */
269 tmp_new = sub_copy;
270 while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
271 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
272 tmp_new[0] == WLAN_EID_SSID ||
c17fe043 273 tmp_new[0] == WLAN_EID_MULTI_BSSID_IDX)) {
0b8fb823
PX
274 memcpy(pos, tmp_new, tmp_new[1] + 2);
275 pos += tmp_new[1] + 2;
276 }
277 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
278 break;
279 tmp_new += tmp_new[1] + 2;
280 }
281
282 kfree(sub_copy);
283 return pos - new_ie;
284}
285
286static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
287 const u8 *ssid, size_t ssid_len)
288{
289 const struct cfg80211_bss_ies *ies;
290 const u8 *ssidie;
291
292 if (bssid && !ether_addr_equal(a->bssid, bssid))
293 return false;
294
295 if (!ssid)
296 return true;
297
298 ies = rcu_access_pointer(a->ies);
299 if (!ies)
300 return false;
301 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
302 if (!ssidie)
303 return false;
304 if (ssidie[1] != ssid_len)
305 return false;
306 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
307}
308
309static int
7011ba58
SS
310cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
311 struct cfg80211_bss *nontrans_bss)
0b8fb823
PX
312{
313 const u8 *ssid;
314 size_t ssid_len;
7011ba58 315 struct cfg80211_bss *bss = NULL;
0b8fb823
PX
316
317 rcu_read_lock();
7011ba58 318 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
0b8fb823
PX
319 if (!ssid) {
320 rcu_read_unlock();
321 return -EINVAL;
322 }
323 ssid_len = ssid[1];
324 ssid = ssid + 2;
325 rcu_read_unlock();
326
327 /* check if nontrans_bss is in the list */
328 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
7011ba58 329 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))
0b8fb823
PX
330 return 0;
331 }
332
333 /* add to the list */
334 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
335 return 0;
336}
337
1b8ec87a 338static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
15d6030b
SL
339 unsigned long expire_time)
340{
341 struct cfg80211_internal_bss *bss, *tmp;
342 bool expired = false;
343
1b8ec87a 344 lockdep_assert_held(&rdev->bss_lock);
4b1af479 345
1b8ec87a 346 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
15d6030b
SL
347 if (atomic_read(&bss->hold))
348 continue;
349 if (!time_after(expire_time, bss->ts))
350 continue;
351
1b8ec87a 352 if (__cfg80211_unlink_bss(rdev, bss))
776b3580 353 expired = true;
15d6030b
SL
354 }
355
356 if (expired)
1b8ec87a 357 rdev->bss_generation++;
15d6030b
SL
358}
359
9853a55e
JB
360static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
361{
362 struct cfg80211_internal_bss *bss, *oldest = NULL;
363 bool ret;
364
365 lockdep_assert_held(&rdev->bss_lock);
366
367 list_for_each_entry(bss, &rdev->bss_list, list) {
368 if (atomic_read(&bss->hold))
369 continue;
370
371 if (!list_empty(&bss->hidden_list) &&
372 !bss->pub.hidden_beacon_bss)
373 continue;
374
375 if (oldest && time_before(oldest->ts, bss->ts))
376 continue;
377 oldest = bss;
378 }
379
380 if (WARN_ON(!oldest))
381 return false;
382
383 /*
384 * The callers make sure to increase rdev->bss_generation if anything
385 * gets removed (and a new entry added), so there's no need to also do
386 * it here.
387 */
388
389 ret = __cfg80211_unlink_bss(rdev, oldest);
390 WARN_ON(!ret);
391 return ret;
392}
393
f9d15d16
JB
394void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
395 bool send_message)
2a519311 396{
667503dd 397 struct cfg80211_scan_request *request;
fd014284 398 struct wireless_dev *wdev;
f9d15d16 399 struct sk_buff *msg;
3d23e349 400#ifdef CONFIG_CFG80211_WEXT
2a519311
JB
401 union iwreq_data wrqu;
402#endif
403
5fe231e8 404 ASSERT_RTNL();
01a0ac41 405
f9d15d16 406 if (rdev->scan_msg) {
505a2e88 407 nl80211_send_scan_msg(rdev, rdev->scan_msg);
f9d15d16
JB
408 rdev->scan_msg = NULL;
409 return;
410 }
667503dd 411
f9d15d16 412 request = rdev->scan_req;
01a0ac41
JB
413 if (!request)
414 return;
415
fd014284 416 wdev = request->wdev;
2a519311 417
6829c878
JB
418 /*
419 * This must be before sending the other events!
420 * Otherwise, wpa_supplicant gets completely confused with
421 * wext events.
422 */
fd014284
JB
423 if (wdev->netdev)
424 cfg80211_sme_scan_done(wdev->netdev);
6829c878 425
1d76250b 426 if (!request->info.aborted &&
f9d15d16
JB
427 request->flags & NL80211_SCAN_FLAG_FLUSH) {
428 /* flush entries from previous scans */
429 spin_lock_bh(&rdev->bss_lock);
430 __cfg80211_bss_expire(rdev, request->scan_start);
431 spin_unlock_bh(&rdev->bss_lock);
15d6030b 432 }
2a519311 433
1d76250b 434 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
f9d15d16 435
3d23e349 436#ifdef CONFIG_CFG80211_WEXT
1d76250b 437 if (wdev->netdev && !request->info.aborted) {
2a519311
JB
438 memset(&wrqu, 0, sizeof(wrqu));
439
fd014284 440 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
2a519311
JB
441 }
442#endif
443
fd014284
JB
444 if (wdev->netdev)
445 dev_put(wdev->netdev);
2a519311 446
36e6fea8 447 rdev->scan_req = NULL;
4a58e7c3 448 kfree(request);
f9d15d16
JB
449
450 if (!send_message)
451 rdev->scan_msg = msg;
452 else
505a2e88 453 nl80211_send_scan_msg(rdev, msg);
2a519311 454}
667503dd 455
36e6fea8
JB
456void __cfg80211_scan_done(struct work_struct *wk)
457{
458 struct cfg80211_registered_device *rdev;
459
460 rdev = container_of(wk, struct cfg80211_registered_device,
461 scan_done_wk);
462
5fe231e8 463 rtnl_lock();
f9d15d16 464 ___cfg80211_scan_done(rdev, true);
5fe231e8 465 rtnl_unlock();
36e6fea8
JB
466}
467
1d76250b
AS
468void cfg80211_scan_done(struct cfg80211_scan_request *request,
469 struct cfg80211_scan_info *info)
667503dd 470{
1d76250b 471 trace_cfg80211_scan_done(request, info);
f26cbf40 472 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
667503dd 473
1d76250b 474 request->info = *info;
5fe231e8 475 request->notified = true;
f26cbf40 476 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
667503dd 477}
2a519311
JB
478EXPORT_SYMBOL(cfg80211_scan_done);
479
ca986ad9
AVS
480void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
481 struct cfg80211_sched_scan_request *req)
482{
483 ASSERT_RTNL();
484
485 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
486}
487
488static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
489 struct cfg80211_sched_scan_request *req)
490{
491 ASSERT_RTNL();
492
493 list_del_rcu(&req->list);
494 kfree_rcu(req, rcu_head);
495}
496
497static struct cfg80211_sched_scan_request *
498cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
499{
500 struct cfg80211_sched_scan_request *pos;
501
1b57b621 502 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
ca986ad9 503
1b57b621 504 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
ca986ad9
AVS
505 if (pos->reqid == reqid)
506 return pos;
507 }
b34939b9 508 return NULL;
ca986ad9
AVS
509}
510
511/*
512 * Determines if a scheduled scan request can be handled. When a legacy
513 * scheduled scan is running no other scheduled scan is allowed regardless
514 * whether the request is for legacy or multi-support scan. When a multi-support
515 * scheduled scan is running a request for legacy scan is not allowed. In this
516 * case a request for multi-support scan can be handled if resources are
517 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
518 */
519int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
520 bool want_multi)
521{
522 struct cfg80211_sched_scan_request *pos;
523 int i = 0;
524
525 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
526 /* request id zero means legacy in progress */
527 if (!i && !pos->reqid)
528 return -EINPROGRESS;
529 i++;
530 }
531
532 if (i) {
533 /* no legacy allowed when multi request(s) are active */
534 if (!want_multi)
535 return -EINPROGRESS;
536
537 /* resource limit reached */
538 if (i == rdev->wiphy.max_sched_scan_reqs)
539 return -ENOSPC;
540 }
541 return 0;
542}
543
b34939b9 544void cfg80211_sched_scan_results_wk(struct work_struct *work)
807f8a8c
LC
545{
546 struct cfg80211_registered_device *rdev;
b34939b9 547 struct cfg80211_sched_scan_request *req, *tmp;
807f8a8c 548
b34939b9
AVS
549 rdev = container_of(work, struct cfg80211_registered_device,
550 sched_scan_res_wk);
807f8a8c 551
5fe231e8 552 rtnl_lock();
b34939b9
AVS
553 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
554 if (req->report_results) {
555 req->report_results = false;
556 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
557 /* flush entries from previous scans */
558 spin_lock_bh(&rdev->bss_lock);
559 __cfg80211_bss_expire(rdev, req->scan_start);
560 spin_unlock_bh(&rdev->bss_lock);
561 req->scan_start = jiffies;
562 }
563 nl80211_send_sched_scan(req,
564 NL80211_CMD_SCHED_SCAN_RESULTS);
15d6030b 565 }
15d6030b 566 }
5fe231e8 567 rtnl_unlock();
807f8a8c
LC
568}
569
b34939b9 570void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
807f8a8c 571{
ca986ad9
AVS
572 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
573 struct cfg80211_sched_scan_request *request;
574
b34939b9 575 trace_cfg80211_sched_scan_results(wiphy, reqid);
807f8a8c 576 /* ignore if we're not scanning */
31a60ed1 577
1b57b621 578 rcu_read_lock();
b34939b9
AVS
579 request = cfg80211_find_sched_scan_req(rdev, reqid);
580 if (request) {
581 request->report_results = true;
582 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
583 }
1b57b621 584 rcu_read_unlock();
807f8a8c
LC
585}
586EXPORT_SYMBOL(cfg80211_sched_scan_results);
587
b34939b9 588void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
807f8a8c 589{
f26cbf40 590 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
807f8a8c 591
792e6aa7
EP
592 ASSERT_RTNL();
593
b34939b9 594 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
4ee3e063 595
b34939b9 596 __cfg80211_stop_sched_scan(rdev, reqid, true);
792e6aa7
EP
597}
598EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
599
b34939b9 600void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
792e6aa7
EP
601{
602 rtnl_lock();
b34939b9 603 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
5fe231e8 604 rtnl_unlock();
807f8a8c 605}
807f8a8c
LC
606EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
607
ca986ad9
AVS
608int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
609 struct cfg80211_sched_scan_request *req,
610 bool driver_initiated)
807f8a8c 611{
5fe231e8 612 ASSERT_RTNL();
807f8a8c 613
85a9994a 614 if (!driver_initiated) {
3a3ecf1d 615 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
85a9994a
LC
616 if (err)
617 return err;
618 }
807f8a8c 619
ca986ad9 620 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
807f8a8c 621
ca986ad9 622 cfg80211_del_sched_scan_req(rdev, req);
807f8a8c 623
3b4670ff 624 return 0;
807f8a8c
LC
625}
626
ca986ad9
AVS
627int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
628 u64 reqid, bool driver_initiated)
629{
630 struct cfg80211_sched_scan_request *sched_scan_req;
631
632 ASSERT_RTNL();
633
634 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
b34939b9
AVS
635 if (!sched_scan_req)
636 return -ENOENT;
ca986ad9
AVS
637
638 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
639 driver_initiated);
640}
641
1b8ec87a 642void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
cb3a8eec
DW
643 unsigned long age_secs)
644{
645 struct cfg80211_internal_bss *bss;
646 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
647
1b8ec87a
ZG
648 spin_lock_bh(&rdev->bss_lock);
649 list_for_each_entry(bss, &rdev->bss_list, list)
cb3a8eec 650 bss->ts -= age_jiffies;
1b8ec87a 651 spin_unlock_bh(&rdev->bss_lock);
cb3a8eec
DW
652}
653
1b8ec87a 654void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
2a519311 655{
1b8ec87a 656 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
2a519311
JB
657}
658
49a68e0d
JB
659const struct element *
660cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
661 const u8 *match, unsigned int match_len,
662 unsigned int match_offset)
2a519311 663{
0f3b07f0
JB
664 const struct element *elem;
665
0f3b07f0 666 for_each_element_id(elem, eid, ies, len) {
49a68e0d
JB
667 if (elem->datalen >= match_offset + match_len &&
668 !memcmp(elem->data + match_offset, match, match_len))
669 return elem;
2a519311 670 }
fbd05e4a
LC
671
672 return NULL;
2a519311 673}
49a68e0d 674EXPORT_SYMBOL(cfg80211_find_elem_match);
2a519311 675
49a68e0d
JB
676const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
677 const u8 *ies,
678 unsigned int len)
0c28ec58 679{
49a68e0d 680 const struct element *elem;
fbd05e4a
LC
681 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
682 int match_len = (oui_type < 0) ? 3 : sizeof(match);
0c28ec58 683
9e9ea439
EG
684 if (WARN_ON(oui_type > 0xff))
685 return NULL;
686
49a68e0d
JB
687 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
688 match, match_len, 0);
6719429d 689
49a68e0d 690 if (!elem || elem->datalen < 4)
fbd05e4a 691 return NULL;
6719429d 692
49a68e0d 693 return elem;
0c28ec58 694}
49a68e0d 695EXPORT_SYMBOL(cfg80211_find_vendor_elem);
0c28ec58 696
4593c4cb
JB
697/**
698 * enum bss_compare_mode - BSS compare mode
699 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
700 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
701 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
702 */
703enum bss_compare_mode {
704 BSS_CMP_REGULAR,
705 BSS_CMP_HIDE_ZLEN,
706 BSS_CMP_HIDE_NUL,
707};
708
dd9dfb9f 709static int cmp_bss(struct cfg80211_bss *a,
5622f5bb 710 struct cfg80211_bss *b,
4593c4cb 711 enum bss_compare_mode mode)
dd9dfb9f 712{
9caf0364 713 const struct cfg80211_bss_ies *a_ies, *b_ies;
3af6341c
JB
714 const u8 *ie1 = NULL;
715 const u8 *ie2 = NULL;
5622f5bb 716 int i, r;
dd9dfb9f 717
3af6341c
JB
718 if (a->channel != b->channel)
719 return b->channel->center_freq - a->channel->center_freq;
dd9dfb9f 720
9caf0364
JB
721 a_ies = rcu_access_pointer(a->ies);
722 if (!a_ies)
723 return -1;
724 b_ies = rcu_access_pointer(b->ies);
725 if (!b_ies)
726 return 1;
727
3af6341c
JB
728 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
729 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
730 a_ies->data, a_ies->len);
731 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
732 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
733 b_ies->data, b_ies->len);
734 if (ie1 && ie2) {
735 int mesh_id_cmp;
736
737 if (ie1[1] == ie2[1])
738 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
739 else
740 mesh_id_cmp = ie2[1] - ie1[1];
741
742 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
743 a_ies->data, a_ies->len);
744 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
745 b_ies->data, b_ies->len);
746 if (ie1 && ie2) {
747 if (mesh_id_cmp)
748 return mesh_id_cmp;
749 if (ie1[1] != ie2[1])
750 return ie2[1] - ie1[1];
751 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
752 }
753 }
754
3af6341c
JB
755 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
756 if (r)
757 return r;
758
9caf0364
JB
759 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
760 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
dd9dfb9f 761
5622f5bb
JB
762 if (!ie1 && !ie2)
763 return 0;
764
f94f8b16 765 /*
5622f5bb
JB
766 * Note that with "hide_ssid", the function returns a match if
767 * the already-present BSS ("b") is a hidden SSID beacon for
768 * the new BSS ("a").
f94f8b16 769 */
dd9dfb9f
DT
770
771 /* sort missing IE before (left of) present IE */
772 if (!ie1)
773 return -1;
774 if (!ie2)
775 return 1;
776
4593c4cb
JB
777 switch (mode) {
778 case BSS_CMP_HIDE_ZLEN:
779 /*
780 * In ZLEN mode we assume the BSS entry we're
781 * looking for has a zero-length SSID. So if
782 * the one we're looking at right now has that,
783 * return 0. Otherwise, return the difference
784 * in length, but since we're looking for the
785 * 0-length it's really equivalent to returning
786 * the length of the one we're looking at.
787 *
788 * No content comparison is needed as we assume
789 * the content length is zero.
790 */
791 return ie2[1];
792 case BSS_CMP_REGULAR:
793 default:
794 /* sort by length first, then by contents */
795 if (ie1[1] != ie2[1])
796 return ie2[1] - ie1[1];
5622f5bb 797 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
4593c4cb
JB
798 case BSS_CMP_HIDE_NUL:
799 if (ie1[1] != ie2[1])
800 return ie2[1] - ie1[1];
801 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
802 for (i = 0; i < ie2[1]; i++)
803 if (ie2[i + 2])
804 return -1;
805 return 0;
806 }
dd9dfb9f
DT
807}
808
6eb18137 809static bool cfg80211_bss_type_match(u16 capability,
57fbcce3 810 enum nl80211_band band,
6eb18137
DL
811 enum ieee80211_bss_type bss_type)
812{
813 bool ret = true;
814 u16 mask, val;
815
816 if (bss_type == IEEE80211_BSS_TYPE_ANY)
817 return ret;
818
57fbcce3 819 if (band == NL80211_BAND_60GHZ) {
6eb18137
DL
820 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
821 switch (bss_type) {
822 case IEEE80211_BSS_TYPE_ESS:
823 val = WLAN_CAPABILITY_DMG_TYPE_AP;
824 break;
825 case IEEE80211_BSS_TYPE_PBSS:
826 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
827 break;
828 case IEEE80211_BSS_TYPE_IBSS:
829 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
830 break;
831 default:
832 return false;
833 }
834 } else {
835 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
836 switch (bss_type) {
837 case IEEE80211_BSS_TYPE_ESS:
838 val = WLAN_CAPABILITY_ESS;
839 break;
840 case IEEE80211_BSS_TYPE_IBSS:
841 val = WLAN_CAPABILITY_IBSS;
842 break;
843 case IEEE80211_BSS_TYPE_MBSS:
844 val = 0;
845 break;
846 default:
847 return false;
848 }
849 }
850
851 ret = ((capability & mask) == val);
852 return ret;
853}
854
0e3a39b5 855/* Returned bss is reference counted and must be cleaned up appropriately. */
2a519311
JB
856struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
857 struct ieee80211_channel *channel,
858 const u8 *bssid,
79420f09 859 const u8 *ssid, size_t ssid_len,
6eb18137
DL
860 enum ieee80211_bss_type bss_type,
861 enum ieee80211_privacy privacy)
2a519311 862{
f26cbf40 863 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2a519311 864 struct cfg80211_internal_bss *bss, *res = NULL;
ccb6c136 865 unsigned long now = jiffies;
6eb18137 866 int bss_privacy;
2a519311 867
6eb18137
DL
868 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
869 privacy);
4ee3e063 870
1b8ec87a 871 spin_lock_bh(&rdev->bss_lock);
2a519311 872
1b8ec87a 873 list_for_each_entry(bss, &rdev->bss_list, list) {
6eb18137
DL
874 if (!cfg80211_bss_type_match(bss->pub.capability,
875 bss->pub.channel->band, bss_type))
876 continue;
877
878 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
879 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
880 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
79420f09 881 continue;
2a519311
JB
882 if (channel && bss->pub.channel != channel)
883 continue;
c14a7400
JB
884 if (!is_valid_ether_addr(bss->pub.bssid))
885 continue;
ccb6c136
JB
886 /* Don't get expired BSS structs */
887 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
888 !atomic_read(&bss->hold))
889 continue;
2a519311
JB
890 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
891 res = bss;
1b8ec87a 892 bss_ref_get(rdev, res);
2a519311
JB
893 break;
894 }
895 }
896
1b8ec87a 897 spin_unlock_bh(&rdev->bss_lock);
2a519311
JB
898 if (!res)
899 return NULL;
4ee3e063 900 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
901 return &res->pub;
902}
903EXPORT_SYMBOL(cfg80211_get_bss);
904
1b8ec87a 905static void rb_insert_bss(struct cfg80211_registered_device *rdev,
2a519311
JB
906 struct cfg80211_internal_bss *bss)
907{
1b8ec87a 908 struct rb_node **p = &rdev->bss_tree.rb_node;
2a519311
JB
909 struct rb_node *parent = NULL;
910 struct cfg80211_internal_bss *tbss;
911 int cmp;
912
913 while (*p) {
914 parent = *p;
915 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
916
4593c4cb 917 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
2a519311
JB
918
919 if (WARN_ON(!cmp)) {
920 /* will sort of leak this BSS */
921 return;
922 }
923
924 if (cmp < 0)
925 p = &(*p)->rb_left;
926 else
927 p = &(*p)->rb_right;
928 }
929
930 rb_link_node(&bss->rbn, parent, p);
1b8ec87a 931 rb_insert_color(&bss->rbn, &rdev->bss_tree);
2a519311
JB
932}
933
934static struct cfg80211_internal_bss *
1b8ec87a 935rb_find_bss(struct cfg80211_registered_device *rdev,
5622f5bb 936 struct cfg80211_internal_bss *res,
4593c4cb 937 enum bss_compare_mode mode)
dd9dfb9f 938{
1b8ec87a 939 struct rb_node *n = rdev->bss_tree.rb_node;
dd9dfb9f
DT
940 struct cfg80211_internal_bss *bss;
941 int r;
942
943 while (n) {
944 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
4593c4cb 945 r = cmp_bss(&res->pub, &bss->pub, mode);
dd9dfb9f
DT
946
947 if (r == 0)
948 return bss;
949 else if (r < 0)
950 n = n->rb_left;
951 else
952 n = n->rb_right;
953 }
954
955 return NULL;
956}
957
1b8ec87a 958static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
776b3580 959 struct cfg80211_internal_bss *new)
dd9dfb9f 960{
9caf0364 961 const struct cfg80211_bss_ies *ies;
776b3580
JB
962 struct cfg80211_internal_bss *bss;
963 const u8 *ie;
964 int i, ssidlen;
965 u8 fold = 0;
9853a55e 966 u32 n_entries = 0;
9caf0364 967
776b3580 968 ies = rcu_access_pointer(new->pub.beacon_ies);
9caf0364 969 if (WARN_ON(!ies))
776b3580 970 return false;
dd9dfb9f 971
776b3580
JB
972 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
973 if (!ie) {
974 /* nothing to do */
975 return true;
976 }
977
978 ssidlen = ie[1];
979 for (i = 0; i < ssidlen; i++)
980 fold |= ie[2 + i];
981
982 if (fold) {
983 /* not a hidden SSID */
984 return true;
985 }
986
987 /* This is the bad part ... */
988
1b8ec87a 989 list_for_each_entry(bss, &rdev->bss_list, list) {
9853a55e
JB
990 /*
991 * we're iterating all the entries anyway, so take the
992 * opportunity to validate the list length accounting
993 */
994 n_entries++;
995
776b3580
JB
996 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
997 continue;
998 if (bss->pub.channel != new->pub.channel)
999 continue;
dcd6eac1
SW
1000 if (bss->pub.scan_width != new->pub.scan_width)
1001 continue;
776b3580
JB
1002 if (rcu_access_pointer(bss->pub.beacon_ies))
1003 continue;
1004 ies = rcu_access_pointer(bss->pub.ies);
1005 if (!ies)
1006 continue;
1007 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1008 if (!ie)
1009 continue;
1010 if (ssidlen && ie[1] != ssidlen)
1011 continue;
776b3580
JB
1012 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1013 continue;
1014 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1015 list_del(&bss->hidden_list);
1016 /* combine them */
1017 list_add(&bss->hidden_list, &new->hidden_list);
1018 bss->pub.hidden_beacon_bss = &new->pub;
1019 new->refcount += bss->refcount;
1020 rcu_assign_pointer(bss->pub.beacon_ies,
1021 new->pub.beacon_ies);
1022 }
1023
9853a55e
JB
1024 WARN_ONCE(n_entries != rdev->bss_entries,
1025 "rdev bss entries[%d]/list[len:%d] corruption\n",
1026 rdev->bss_entries, n_entries);
1027
776b3580 1028 return true;
dd9dfb9f
DT
1029}
1030
0cd01efb
SS
1031struct cfg80211_non_tx_bss {
1032 struct cfg80211_bss *tx_bss;
1033 u8 max_bssid_indicator;
1034 u8 bssid_index;
1035};
1036
0e3a39b5 1037/* Returned bss is reference counted and must be cleaned up appropriately. */
2a519311 1038static struct cfg80211_internal_bss *
1b8ec87a 1039cfg80211_bss_update(struct cfg80211_registered_device *rdev,
3afc2167
EG
1040 struct cfg80211_internal_bss *tmp,
1041 bool signal_valid)
2a519311
JB
1042{
1043 struct cfg80211_internal_bss *found = NULL;
2a519311 1044
9caf0364 1045 if (WARN_ON(!tmp->pub.channel))
2a519311 1046 return NULL;
2a519311 1047
9caf0364 1048 tmp->ts = jiffies;
2a519311 1049
1b8ec87a 1050 spin_lock_bh(&rdev->bss_lock);
2a519311 1051
9caf0364 1052 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1b8ec87a 1053 spin_unlock_bh(&rdev->bss_lock);
9caf0364
JB
1054 return NULL;
1055 }
1056
1b8ec87a 1057 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
2a519311 1058
cd1658f5 1059 if (found) {
34a6eddb 1060 /* Update IEs */
9caf0364
JB
1061 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1062 const struct cfg80211_bss_ies *old;
1063
1064 old = rcu_access_pointer(found->pub.proberesp_ies);
cd1658f5 1065
9caf0364
JB
1066 rcu_assign_pointer(found->pub.proberesp_ies,
1067 tmp->pub.proberesp_ies);
34a6eddb 1068 /* Override possible earlier Beacon frame IEs */
9caf0364
JB
1069 rcu_assign_pointer(found->pub.ies,
1070 tmp->pub.proberesp_ies);
1071 if (old)
1072 kfree_rcu((struct cfg80211_bss_ies *)old,
1073 rcu_head);
1074 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
9537f227 1075 const struct cfg80211_bss_ies *old;
776b3580
JB
1076 struct cfg80211_internal_bss *bss;
1077
1078 if (found->pub.hidden_beacon_bss &&
1079 !list_empty(&found->hidden_list)) {
1345ee6a
JB
1080 const struct cfg80211_bss_ies *f;
1081
776b3580
JB
1082 /*
1083 * The found BSS struct is one of the probe
1084 * response members of a group, but we're
1085 * receiving a beacon (beacon_ies in the tmp
1086 * bss is used). This can only mean that the
1087 * AP changed its beacon from not having an
1088 * SSID to showing it, which is confusing so
1089 * drop this information.
1090 */
1345ee6a
JB
1091
1092 f = rcu_access_pointer(tmp->pub.beacon_ies);
1093 kfree_rcu((struct cfg80211_bss_ies *)f,
1094 rcu_head);
776b3580
JB
1095 goto drop;
1096 }
915de2ff 1097
9caf0364 1098 old = rcu_access_pointer(found->pub.beacon_ies);
9caf0364
JB
1099
1100 rcu_assign_pointer(found->pub.beacon_ies,
1101 tmp->pub.beacon_ies);
01123e23
SN
1102
1103 /* Override IEs if they were from a beacon before */
9537f227 1104 if (old == rcu_access_pointer(found->pub.ies))
9caf0364
JB
1105 rcu_assign_pointer(found->pub.ies,
1106 tmp->pub.beacon_ies);
cd1658f5 1107
776b3580
JB
1108 /* Assign beacon IEs to all sub entries */
1109 list_for_each_entry(bss, &found->hidden_list,
1110 hidden_list) {
1111 const struct cfg80211_bss_ies *ies;
1112
1113 ies = rcu_access_pointer(bss->pub.beacon_ies);
1114 WARN_ON(ies != old);
1115
1116 rcu_assign_pointer(bss->pub.beacon_ies,
1117 tmp->pub.beacon_ies);
1118 }
1119
9caf0364
JB
1120 if (old)
1121 kfree_rcu((struct cfg80211_bss_ies *)old,
1122 rcu_head);
1123 }
1345ee6a
JB
1124
1125 found->pub.beacon_interval = tmp->pub.beacon_interval;
3afc2167
EG
1126 /*
1127 * don't update the signal if beacon was heard on
1128 * adjacent channel.
1129 */
1130 if (signal_valid)
1131 found->pub.signal = tmp->pub.signal;
1345ee6a
JB
1132 found->pub.capability = tmp->pub.capability;
1133 found->ts = tmp->ts;
6e19bc4b 1134 found->ts_boottime = tmp->ts_boottime;
1d76250b 1135 found->parent_tsf = tmp->parent_tsf;
983dafaa
SD
1136 found->pub.chains = tmp->pub.chains;
1137 memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
1138 IEEE80211_MAX_CHAINS);
1d76250b 1139 ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
0cd01efb
SS
1140 found->pub.max_bssid_indicator = tmp->pub.max_bssid_indicator;
1141 found->pub.bssid_index = tmp->pub.bssid_index;
2a519311 1142 } else {
9caf0364 1143 struct cfg80211_internal_bss *new;
dd9dfb9f 1144 struct cfg80211_internal_bss *hidden;
9caf0364 1145 struct cfg80211_bss_ies *ies;
dd9dfb9f 1146
9caf0364
JB
1147 /*
1148 * create a copy -- the "res" variable that is passed in
1149 * is allocated on the stack since it's not needed in the
1150 * more common case of an update
1151 */
1b8ec87a 1152 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
9caf0364
JB
1153 GFP_ATOMIC);
1154 if (!new) {
1155 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1156 if (ies)
1157 kfree_rcu(ies, rcu_head);
1158 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1159 if (ies)
1160 kfree_rcu(ies, rcu_head);
776b3580 1161 goto drop;
9caf0364
JB
1162 }
1163 memcpy(new, tmp, sizeof(*new));
776b3580
JB
1164 new->refcount = 1;
1165 INIT_LIST_HEAD(&new->hidden_list);
7011ba58 1166 INIT_LIST_HEAD(&new->pub.nontrans_list);
776b3580
JB
1167
1168 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1b8ec87a 1169 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
776b3580 1170 if (!hidden)
1b8ec87a 1171 hidden = rb_find_bss(rdev, tmp,
776b3580
JB
1172 BSS_CMP_HIDE_NUL);
1173 if (hidden) {
1174 new->pub.hidden_beacon_bss = &hidden->pub;
1175 list_add(&new->hidden_list,
1176 &hidden->hidden_list);
1177 hidden->refcount++;
1178 rcu_assign_pointer(new->pub.beacon_ies,
1179 hidden->pub.beacon_ies);
1180 }
1181 } else {
1182 /*
1183 * Ok so we found a beacon, and don't have an entry. If
1184 * it's a beacon with hidden SSID, we might be in for an
1185 * expensive search for any probe responses that should
1186 * be grouped with this beacon for updates ...
1187 */
1b8ec87a 1188 if (!cfg80211_combine_bsses(rdev, new)) {
776b3580
JB
1189 kfree(new);
1190 goto drop;
1191 }
1192 }
1193
9853a55e
JB
1194 if (rdev->bss_entries >= bss_entries_limit &&
1195 !cfg80211_bss_expire_oldest(rdev)) {
1196 kfree(new);
1197 goto drop;
1198 }
1199
a3584f56 1200 /* This must be before the call to bss_ref_get */
0cd01efb 1201 if (tmp->pub.transmitted_bss) {
a3584f56 1202 struct cfg80211_internal_bss *pbss =
0cd01efb 1203 container_of(tmp->pub.transmitted_bss,
a3584f56
SS
1204 struct cfg80211_internal_bss,
1205 pub);
1206
0cd01efb 1207 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
a3584f56
SS
1208 bss_ref_get(rdev, pbss);
1209 }
1210
1b8ec87a 1211 list_add_tail(&new->list, &rdev->bss_list);
9853a55e 1212 rdev->bss_entries++;
1b8ec87a 1213 rb_insert_bss(rdev, new);
9caf0364 1214 found = new;
2a519311
JB
1215 }
1216
1b8ec87a
ZG
1217 rdev->bss_generation++;
1218 bss_ref_get(rdev, found);
1219 spin_unlock_bh(&rdev->bss_lock);
2a519311 1220
2a519311 1221 return found;
776b3580 1222 drop:
1b8ec87a 1223 spin_unlock_bh(&rdev->bss_lock);
776b3580 1224 return NULL;
2a519311
JB
1225}
1226
119f94a6
JM
1227/*
1228 * Update RX channel information based on the available frame payload
1229 * information. This is mainly for the 2.4 GHz band where frames can be received
1230 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1231 * element to indicate the current (transmitting) channel, but this might also
1232 * be needed on other bands if RX frequency does not match with the actual
1233 * operating channel of a BSS.
1234 */
0172bb75
JB
1235static struct ieee80211_channel *
1236cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
119f94a6
JM
1237 struct ieee80211_channel *channel,
1238 enum nl80211_bss_scan_width scan_width)
0172bb75
JB
1239{
1240 const u8 *tmp;
1241 u32 freq;
1242 int channel_number = -1;
119f94a6 1243 struct ieee80211_channel *alt_channel;
0172bb75
JB
1244
1245 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1246 if (tmp && tmp[1] == 1) {
1247 channel_number = tmp[2];
1248 } else {
1249 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1250 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1251 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1252
1253 channel_number = htop->primary_chan;
1254 }
1255 }
1256
119f94a6
JM
1257 if (channel_number < 0) {
1258 /* No channel information in frame payload */
0172bb75 1259 return channel;
119f94a6 1260 }
0172bb75
JB
1261
1262 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
119f94a6
JM
1263 alt_channel = ieee80211_get_channel(wiphy, freq);
1264 if (!alt_channel) {
1265 if (channel->band == NL80211_BAND_2GHZ) {
1266 /*
1267 * Better not allow unexpected channels when that could
1268 * be going beyond the 1-11 range (e.g., discovering
1269 * BSS on channel 12 when radio is configured for
1270 * channel 11.
1271 */
1272 return NULL;
1273 }
1274
1275 /* No match for the payload channel number - ignore it */
1276 return channel;
1277 }
1278
1279 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1280 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1281 /*
1282 * Ignore channel number in 5 and 10 MHz channels where there
1283 * may not be an n:1 or 1:n mapping between frequencies and
1284 * channel numbers.
1285 */
1286 return channel;
1287 }
1288
1289 /*
1290 * Use the channel determined through the payload channel number
1291 * instead of the RX channel reported by the driver.
1292 */
1293 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
0172bb75 1294 return NULL;
119f94a6 1295 return alt_channel;
0172bb75
JB
1296}
1297
0e3a39b5 1298/* Returned bss is reference counted and must be cleaned up appropriately. */
0b8fb823
PX
1299static struct cfg80211_bss *
1300cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1301 struct cfg80211_inform_bss *data,
1302 enum cfg80211_bss_frame_type ftype,
1303 const u8 *bssid, u64 tsf, u16 capability,
1304 u16 beacon_interval, const u8 *ie, size_t ielen,
0cd01efb 1305 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823 1306 gfp_t gfp)
06aa7afa 1307{
0b8fb823 1308 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9caf0364 1309 struct cfg80211_bss_ies *ies;
3afc2167 1310 struct ieee80211_channel *channel;
7011ba58 1311 struct cfg80211_internal_bss tmp = {}, *res;
6eb18137 1312 int bss_type;
67af9811 1313 bool signal_valid;
06aa7afa
JK
1314
1315 if (WARN_ON(!wiphy))
1316 return NULL;
1317
22fe88d3 1318 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
6e19bc4b 1319 (data->signal < 0 || data->signal > 100)))
06aa7afa
JK
1320 return NULL;
1321
119f94a6
JM
1322 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1323 data->scan_width);
0172bb75
JB
1324 if (!channel)
1325 return NULL;
1326
9caf0364
JB
1327 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1328 tmp.pub.channel = channel;
6e19bc4b
DS
1329 tmp.pub.scan_width = data->scan_width;
1330 tmp.pub.signal = data->signal;
9caf0364
JB
1331 tmp.pub.beacon_interval = beacon_interval;
1332 tmp.pub.capability = capability;
6e19bc4b 1333 tmp.ts_boottime = data->boottime_ns;
0cd01efb
SS
1334 if (non_tx_data) {
1335 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1336 tmp.pub.bssid_index = non_tx_data->bssid_index;
1337 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1338 }
6e19bc4b 1339
34a6eddb 1340 /*
5bc8c1f2 1341 * If we do not know here whether the IEs are from a Beacon or Probe
34a6eddb
JM
1342 * Response frame, we need to pick one of the options and only use it
1343 * with the driver that does not provide the full Beacon/Probe Response
1344 * frame. Use Beacon frame pointer to avoid indicating that this should
50521aa8 1345 * override the IEs pointer should we have received an earlier
9caf0364 1346 * indication of Probe Response data.
34a6eddb 1347 */
0e227084 1348 ies = kzalloc(sizeof(*ies) + ielen, gfp);
9caf0364
JB
1349 if (!ies)
1350 return NULL;
1351 ies->len = ielen;
8cef2c9d 1352 ies->tsf = tsf;
0e227084 1353 ies->from_beacon = false;
9caf0364 1354 memcpy(ies->data, ie, ielen);
06aa7afa 1355
5bc8c1f2
JB
1356 switch (ftype) {
1357 case CFG80211_BSS_FTYPE_BEACON:
1358 ies->from_beacon = true;
925b5978 1359 /* fall through */
5bc8c1f2
JB
1360 case CFG80211_BSS_FTYPE_UNKNOWN:
1361 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1362 break;
1363 case CFG80211_BSS_FTYPE_PRESP:
1364 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1365 break;
1366 }
9caf0364 1367 rcu_assign_pointer(tmp.pub.ies, ies);
06aa7afa 1368
6e19bc4b 1369 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
67af9811 1370 wiphy->max_adj_channel_rssi_comp;
0cd01efb 1371 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
06aa7afa
JK
1372 if (!res)
1373 return NULL;
1374
57fbcce3 1375 if (channel->band == NL80211_BAND_60GHZ) {
6eb18137
DL
1376 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1377 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1378 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1379 regulatory_hint_found_beacon(wiphy, channel, gfp);
1380 } else {
1381 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1382 regulatory_hint_found_beacon(wiphy, channel, gfp);
1383 }
06aa7afa 1384
0cd01efb 1385 if (non_tx_data && non_tx_data->tx_bss) {
0b8fb823
PX
1386 /* this is a nontransmitting bss, we need to add it to
1387 * transmitting bss' list if it is not there
1388 */
0cd01efb
SS
1389 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1390 &res->pub)) {
0b8fb823
PX
1391 if (__cfg80211_unlink_bss(rdev, res))
1392 rdev->bss_generation++;
1393 }
1394 }
1395
4ee3e063 1396 trace_cfg80211_return_bss(&res->pub);
06aa7afa
JK
1397 /* cfg80211_bss_update gives us a referenced result */
1398 return &res->pub;
1399}
06aa7afa 1400
0b8fb823
PX
1401static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1402 struct cfg80211_inform_bss *data,
1403 enum cfg80211_bss_frame_type ftype,
1404 const u8 *bssid, u64 tsf,
1405 u16 beacon_interval, const u8 *ie,
1406 size_t ielen,
0cd01efb 1407 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823
PX
1408 gfp_t gfp)
1409{
1c8745f3
JB
1410 const u8 *mbssid_index_ie;
1411 const struct element *elem, *sub;
1412 size_t new_ie_len;
0b8fb823
PX
1413 u8 new_bssid[ETH_ALEN];
1414 u8 *new_ie;
1415 u16 capability;
1416 struct cfg80211_bss *bss;
1417
0cd01efb 1418 if (!non_tx_data)
0b8fb823
PX
1419 return;
1420 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1421 return;
213ed579
SS
1422 if (!wiphy->support_mbssid)
1423 return;
1424 if (wiphy->support_only_he_mbssid &&
1425 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1426 return;
0b8fb823 1427
0b8fb823
PX
1428 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1429 if (!new_ie)
1430 return;
1431
1c8745f3
JB
1432 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1433 if (elem->datalen < 4)
1434 continue;
1435 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1436 if (sub->id != 0 || sub->datalen < 4) {
0b8fb823
PX
1437 /* not a valid BSS profile */
1438 continue;
1439 }
1440
1c8745f3
JB
1441 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1442 sub->data[1] != 2) {
0b8fb823
PX
1443 /* The first element within the Nontransmitted
1444 * BSSID Profile is not the Nontransmitted
1445 * BSSID Capability element.
1446 */
1447 continue;
1448 }
1449
1450 /* found a Nontransmitted BSSID Profile */
1451 mbssid_index_ie = cfg80211_find_ie
1452 (WLAN_EID_MULTI_BSSID_IDX,
1c8745f3 1453 sub->data, sub->datalen);
0b8fb823
PX
1454 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1455 mbssid_index_ie[2] == 0) {
1456 /* No valid Multiple BSSID-Index element */
1457 continue;
1458 }
1459
0cd01efb
SS
1460 non_tx_data->bssid_index = mbssid_index_ie[2];
1461 non_tx_data->max_bssid_indicator = elem->data[0];
1462
1463 cfg80211_gen_new_bssid(bssid,
1464 non_tx_data->max_bssid_indicator,
1465 non_tx_data->bssid_index,
0b8fb823
PX
1466 new_bssid);
1467 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
1c8745f3
JB
1468 new_ie_len = cfg80211_gen_new_ie(ie, ielen, sub->data,
1469 sub->datalen, new_ie,
0b8fb823
PX
1470 gfp);
1471 if (!new_ie_len)
1472 continue;
1473
1c8745f3 1474 capability = get_unaligned_le16(sub->data + 2);
0b8fb823
PX
1475 bss = cfg80211_inform_single_bss_data(wiphy, data,
1476 ftype,
1477 new_bssid, tsf,
1478 capability,
1479 beacon_interval,
1480 new_ie,
1481 new_ie_len,
0cd01efb
SS
1482 non_tx_data,
1483 gfp);
0b8fb823
PX
1484 if (!bss)
1485 break;
1486 cfg80211_put_bss(wiphy, bss);
1487 }
0b8fb823
PX
1488 }
1489
1490 kfree(new_ie);
1491}
1492
2a519311 1493struct cfg80211_bss *
0b8fb823
PX
1494cfg80211_inform_bss_data(struct wiphy *wiphy,
1495 struct cfg80211_inform_bss *data,
1496 enum cfg80211_bss_frame_type ftype,
1497 const u8 *bssid, u64 tsf, u16 capability,
1498 u16 beacon_interval, const u8 *ie, size_t ielen,
1499 gfp_t gfp)
1500{
1501 struct cfg80211_bss *res;
0cd01efb 1502 struct cfg80211_non_tx_bss non_tx_data;
0b8fb823
PX
1503
1504 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1505 capability, beacon_interval, ie,
1506 ielen, NULL, gfp);
0cd01efb 1507 non_tx_data.tx_bss = res;
0b8fb823 1508 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
0cd01efb
SS
1509 beacon_interval, ie, ielen, &non_tx_data,
1510 gfp);
0b8fb823
PX
1511 return res;
1512}
1513EXPORT_SYMBOL(cfg80211_inform_bss_data);
1514
1515static void
1516cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1517 struct cfg80211_inform_bss *data,
1518 struct ieee80211_mgmt *mgmt, size_t len,
0cd01efb 1519 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823
PX
1520 gfp_t gfp)
1521{
1522 enum cfg80211_bss_frame_type ftype;
1523 const u8 *ie = mgmt->u.probe_resp.variable;
1524 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1525 u.probe_resp.variable);
1526
1527 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1528 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1529
1530 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1531 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1532 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
0cd01efb 1533 ie, ielen, non_tx_data, gfp);
0b8fb823
PX
1534}
1535
1536static void
1537cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
7011ba58 1538 struct cfg80211_bss *nontrans_bss,
0b8fb823
PX
1539 struct ieee80211_mgmt *mgmt, size_t len,
1540 gfp_t gfp)
1541{
1542 u8 *ie, *new_ie, *pos;
1543 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1544 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1545 u.probe_resp.variable);
1546 size_t new_ie_len;
1547 struct cfg80211_bss_ies *new_ies;
1548 const struct cfg80211_bss_ies *old;
1549 u8 cpy_len;
1550
1551 ie = mgmt->u.probe_resp.variable;
1552
1553 new_ie_len = ielen;
1554 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1555 if (!trans_ssid)
1556 return;
1557 new_ie_len -= trans_ssid[1];
1558 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1559 if (!mbssid)
1560 return;
1561 new_ie_len -= mbssid[1];
1562 rcu_read_lock();
7011ba58 1563 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
0b8fb823
PX
1564 if (!nontrans_ssid) {
1565 rcu_read_unlock();
1566 return;
1567 }
1568 new_ie_len += nontrans_ssid[1];
1569 rcu_read_unlock();
1570
1571 /* generate new ie for nontrans BSS
1572 * 1. replace SSID with nontrans BSS' SSID
1573 * 2. skip MBSSID IE
1574 */
1575 new_ie = kzalloc(new_ie_len, gfp);
1576 if (!new_ie)
1577 return;
1578 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp);
bede8d29
SS
1579 if (!new_ies)
1580 goto out_free;
0b8fb823
PX
1581
1582 pos = new_ie;
1583
1584 /* copy the nontransmitted SSID */
1585 cpy_len = nontrans_ssid[1] + 2;
1586 memcpy(pos, nontrans_ssid, cpy_len);
1587 pos += cpy_len;
1588 /* copy the IEs between SSID and MBSSID */
1589 cpy_len = trans_ssid[1] + 2;
1590 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1591 pos += (mbssid - (trans_ssid + cpy_len));
1592 /* copy the IEs after MBSSID */
1593 cpy_len = mbssid[1] + 2;
1594 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1595
1596 /* update ie */
1597 new_ies->len = new_ie_len;
1598 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1599 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1600 memcpy(new_ies->data, new_ie, new_ie_len);
1601 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
7011ba58
SS
1602 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1603 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1604 rcu_assign_pointer(nontrans_bss->ies, new_ies);
0b8fb823
PX
1605 if (old)
1606 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1607 } else {
7011ba58
SS
1608 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1609 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1610 rcu_assign_pointer(nontrans_bss->ies, new_ies);
0b8fb823
PX
1611 if (old)
1612 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1613 }
bede8d29
SS
1614
1615out_free:
1616 kfree(new_ie);
0b8fb823 1617}
6e19bc4b 1618
0b8fb823
PX
1619/* cfg80211_inform_bss_width_frame helper */
1620static struct cfg80211_bss *
1621cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1622 struct cfg80211_inform_bss *data,
1623 struct ieee80211_mgmt *mgmt, size_t len,
0cd01efb 1624 struct cfg80211_non_tx_bss *non_tx_data,
0b8fb823 1625 gfp_t gfp)
2a519311 1626{
9caf0364
JB
1627 struct cfg80211_internal_bss tmp = {}, *res;
1628 struct cfg80211_bss_ies *ies;
3afc2167 1629 struct ieee80211_channel *channel;
67af9811 1630 bool signal_valid;
2a519311
JB
1631 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1632 u.probe_resp.variable);
6eb18137 1633 int bss_type;
bef9bacc 1634
0172bb75
JB
1635 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1636 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1637
6e19bc4b 1638 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
4ee3e063 1639
bef9bacc
MK
1640 if (WARN_ON(!mgmt))
1641 return NULL;
1642
1643 if (WARN_ON(!wiphy))
1644 return NULL;
2a519311 1645
22fe88d3 1646 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
6e19bc4b 1647 (data->signal < 0 || data->signal > 100)))
2a519311
JB
1648 return NULL;
1649
bef9bacc 1650 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
2a519311
JB
1651 return NULL;
1652
0172bb75 1653 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
119f94a6 1654 ielen, data->chan, data->scan_width);
0172bb75
JB
1655 if (!channel)
1656 return NULL;
1657
0e227084 1658 ies = kzalloc(sizeof(*ies) + ielen, gfp);
9caf0364 1659 if (!ies)
2a519311 1660 return NULL;
9caf0364 1661 ies->len = ielen;
8cef2c9d 1662 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
0e227084 1663 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
9caf0364 1664 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
2a519311 1665
9caf0364
JB
1666 if (ieee80211_is_probe_resp(mgmt->frame_control))
1667 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1668 else
1669 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1670 rcu_assign_pointer(tmp.pub.ies, ies);
505a2e88 1671
9caf0364
JB
1672 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1673 tmp.pub.channel = channel;
6e19bc4b
DS
1674 tmp.pub.scan_width = data->scan_width;
1675 tmp.pub.signal = data->signal;
9caf0364
JB
1676 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1677 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
6e19bc4b 1678 tmp.ts_boottime = data->boottime_ns;
1d76250b 1679 tmp.parent_tsf = data->parent_tsf;
983dafaa
SD
1680 tmp.pub.chains = data->chains;
1681 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1d76250b 1682 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
0cd01efb
SS
1683 if (non_tx_data) {
1684 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1685 tmp.pub.bssid_index = non_tx_data->bssid_index;
1686 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1687 }
9caf0364 1688
6e19bc4b 1689 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
67af9811 1690 wiphy->max_adj_channel_rssi_comp;
0cd01efb 1691 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
2a519311
JB
1692 if (!res)
1693 return NULL;
1694
57fbcce3 1695 if (channel->band == NL80211_BAND_60GHZ) {
6eb18137
DL
1696 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1697 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1698 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1699 regulatory_hint_found_beacon(wiphy, channel, gfp);
1700 } else {
1701 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1702 regulatory_hint_found_beacon(wiphy, channel, gfp);
1703 }
e38f8a7a 1704
4ee3e063 1705 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
1706 /* cfg80211_bss_update gives us a referenced result */
1707 return &res->pub;
1708}
0b8fb823
PX
1709
1710struct cfg80211_bss *
1711cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1712 struct cfg80211_inform_bss *data,
1713 struct ieee80211_mgmt *mgmt, size_t len,
1714 gfp_t gfp)
1715{
7011ba58 1716 struct cfg80211_bss *res, *tmp_bss;
0b8fb823
PX
1717 const u8 *ie = mgmt->u.probe_resp.variable;
1718 const struct cfg80211_bss_ies *ies1, *ies2;
1719 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1720 u.probe_resp.variable);
0cd01efb 1721 struct cfg80211_non_tx_bss non_tx_data;
0b8fb823
PX
1722
1723 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1724 len, NULL, gfp);
213ed579
SS
1725 if (!res || !wiphy->support_mbssid ||
1726 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1727 return res;
1728 if (wiphy->support_only_he_mbssid &&
1729 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
0b8fb823
PX
1730 return res;
1731
0cd01efb 1732 non_tx_data.tx_bss = res;
0b8fb823 1733 /* process each non-transmitting bss */
0cd01efb
SS
1734 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1735 &non_tx_data, gfp);
0b8fb823
PX
1736
1737 /* check if the res has other nontransmitting bss which is not
1738 * in MBSSID IE
1739 */
1740 ies1 = rcu_access_pointer(res->ies);
0b8fb823
PX
1741
1742 /* go through nontrans_list, if the timestamp of the BSS is
1743 * earlier than the timestamp of the transmitting BSS then
1744 * update it
1745 */
7011ba58 1746 list_for_each_entry(tmp_bss, &res->nontrans_list,
0b8fb823 1747 nontrans_list) {
7011ba58 1748 ies2 = rcu_access_pointer(tmp_bss->ies);
0b8fb823
PX
1749 if (ies2->tsf < ies1->tsf)
1750 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1751 mgmt, len, gfp);
1752 }
1753
1754 return res;
1755}
6e19bc4b 1756EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2a519311 1757
5b112d3d 1758void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
4c0c0b75 1759{
f26cbf40 1760 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
4c0c0b75
JB
1761 struct cfg80211_internal_bss *bss;
1762
1763 if (!pub)
1764 return;
1765
1766 bss = container_of(pub, struct cfg80211_internal_bss, pub);
776b3580 1767
1b8ec87a
ZG
1768 spin_lock_bh(&rdev->bss_lock);
1769 bss_ref_get(rdev, bss);
1770 spin_unlock_bh(&rdev->bss_lock);
4c0c0b75
JB
1771}
1772EXPORT_SYMBOL(cfg80211_ref_bss);
1773
5b112d3d 1774void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2a519311 1775{
f26cbf40 1776 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2a519311
JB
1777 struct cfg80211_internal_bss *bss;
1778
1779 if (!pub)
1780 return;
1781
1782 bss = container_of(pub, struct cfg80211_internal_bss, pub);
776b3580 1783
1b8ec87a
ZG
1784 spin_lock_bh(&rdev->bss_lock);
1785 bss_ref_put(rdev, bss);
1786 spin_unlock_bh(&rdev->bss_lock);
2a519311
JB
1787}
1788EXPORT_SYMBOL(cfg80211_put_bss);
1789
d491af19
JB
1790void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1791{
f26cbf40 1792 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
7011ba58
SS
1793 struct cfg80211_internal_bss *bss, *tmp1;
1794 struct cfg80211_bss *nontrans_bss, *tmp;
d491af19
JB
1795
1796 if (WARN_ON(!pub))
1797 return;
1798
1799 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1800
1b8ec87a 1801 spin_lock_bh(&rdev->bss_lock);
7011ba58
SS
1802 if (list_empty(&bss->list))
1803 goto out;
1804
1805 list_for_each_entry_safe(nontrans_bss, tmp,
1806 &pub->nontrans_list,
1807 nontrans_list) {
1808 tmp1 = container_of(nontrans_bss,
1809 struct cfg80211_internal_bss, pub);
1810 if (__cfg80211_unlink_bss(rdev, tmp1))
1b8ec87a 1811 rdev->bss_generation++;
3207390a 1812 }
7011ba58
SS
1813
1814 if (__cfg80211_unlink_bss(rdev, bss))
1815 rdev->bss_generation++;
1816out:
1b8ec87a 1817 spin_unlock_bh(&rdev->bss_lock);
d491af19
JB
1818}
1819EXPORT_SYMBOL(cfg80211_unlink_bss);
1820
3d23e349 1821#ifdef CONFIG_CFG80211_WEXT
9f419f38
JB
1822static struct cfg80211_registered_device *
1823cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1824{
5fe231e8 1825 struct cfg80211_registered_device *rdev;
9f419f38
JB
1826 struct net_device *dev;
1827
5fe231e8
JB
1828 ASSERT_RTNL();
1829
9f419f38
JB
1830 dev = dev_get_by_index(net, ifindex);
1831 if (!dev)
5fe231e8
JB
1832 return ERR_PTR(-ENODEV);
1833 if (dev->ieee80211_ptr)
f26cbf40 1834 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
5fe231e8 1835 else
9f419f38
JB
1836 rdev = ERR_PTR(-ENODEV);
1837 dev_put(dev);
9f419f38
JB
1838 return rdev;
1839}
1840
2a519311
JB
1841int cfg80211_wext_siwscan(struct net_device *dev,
1842 struct iw_request_info *info,
1843 union iwreq_data *wrqu, char *extra)
1844{
1845 struct cfg80211_registered_device *rdev;
1846 struct wiphy *wiphy;
1847 struct iw_scan_req *wreq = NULL;
65486c8b 1848 struct cfg80211_scan_request *creq = NULL;
2a519311 1849 int i, err, n_channels = 0;
57fbcce3 1850 enum nl80211_band band;
2a519311
JB
1851
1852 if (!netif_running(dev))
1853 return -ENETDOWN;
1854
b2e3abdc
HS
1855 if (wrqu->data.length == sizeof(struct iw_scan_req))
1856 wreq = (struct iw_scan_req *)extra;
1857
463d0183 1858 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
1859
1860 if (IS_ERR(rdev))
1861 return PTR_ERR(rdev);
1862
f9d15d16 1863 if (rdev->scan_req || rdev->scan_msg) {
2a519311
JB
1864 err = -EBUSY;
1865 goto out;
1866 }
1867
1868 wiphy = &rdev->wiphy;
1869
b2e3abdc
HS
1870 /* Determine number of channels, needed to allocate creq */
1871 if (wreq && wreq->num_channels)
1872 n_channels = wreq->num_channels;
bdfbec2d
IP
1873 else
1874 n_channels = ieee80211_get_num_supported_channels(wiphy);
2a519311
JB
1875
1876 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1877 n_channels * sizeof(void *),
1878 GFP_ATOMIC);
1879 if (!creq) {
1880 err = -ENOMEM;
1881 goto out;
1882 }
1883
1884 creq->wiphy = wiphy;
fd014284 1885 creq->wdev = dev->ieee80211_ptr;
5ba63533
JB
1886 /* SSIDs come after channels */
1887 creq->ssids = (void *)&creq->channels[n_channels];
2a519311
JB
1888 creq->n_channels = n_channels;
1889 creq->n_ssids = 1;
15d6030b 1890 creq->scan_start = jiffies;
2a519311 1891
b2e3abdc 1892 /* translate "Scan on frequencies" request */
2a519311 1893 i = 0;
57fbcce3 1894 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2a519311 1895 int j;
584991dc 1896
2a519311
JB
1897 if (!wiphy->bands[band])
1898 continue;
584991dc 1899
2a519311 1900 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
1901 /* ignore disabled channels */
1902 if (wiphy->bands[band]->channels[j].flags &
1903 IEEE80211_CHAN_DISABLED)
1904 continue;
b2e3abdc
HS
1905
1906 /* If we have a wireless request structure and the
1907 * wireless request specifies frequencies, then search
1908 * for the matching hardware channel.
1909 */
1910 if (wreq && wreq->num_channels) {
1911 int k;
1912 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1913 for (k = 0; k < wreq->num_channels; k++) {
96998e3a
ZG
1914 struct iw_freq *freq =
1915 &wreq->channel_list[k];
1916 int wext_freq =
1917 cfg80211_wext_freq(freq);
1918
b2e3abdc
HS
1919 if (wext_freq == wiphy_freq)
1920 goto wext_freq_found;
1921 }
1922 goto wext_freq_not_found;
1923 }
1924
1925 wext_freq_found:
2a519311
JB
1926 creq->channels[i] = &wiphy->bands[band]->channels[j];
1927 i++;
b2e3abdc 1928 wext_freq_not_found: ;
2a519311
JB
1929 }
1930 }
8862dc5f
HS
1931 /* No channels found? */
1932 if (!i) {
1933 err = -EINVAL;
1934 goto out;
1935 }
2a519311 1936
b2e3abdc
HS
1937 /* Set real number of channels specified in creq->channels[] */
1938 creq->n_channels = i;
2a519311 1939
b2e3abdc
HS
1940 /* translate "Scan for SSID" request */
1941 if (wreq) {
2a519311 1942 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
65486c8b
JB
1943 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1944 err = -EINVAL;
1945 goto out;
1946 }
2a519311
JB
1947 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1948 creq->ssids[0].ssid_len = wreq->essid_len;
1949 }
1950 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1951 creq->n_ssids = 0;
1952 }
1953
57fbcce3 1954 for (i = 0; i < NUM_NL80211_BANDS; i++)
a401d2bb
JB
1955 if (wiphy->bands[i])
1956 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
34850ab2 1957
818965d3
JM
1958 eth_broadcast_addr(creq->bssid);
1959
2a519311 1960 rdev->scan_req = creq;
e35e4d28 1961 err = rdev_scan(rdev, creq);
2a519311
JB
1962 if (err) {
1963 rdev->scan_req = NULL;
65486c8b 1964 /* creq will be freed below */
463d0183 1965 } else {
fd014284 1966 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
65486c8b
JB
1967 /* creq now owned by driver */
1968 creq = NULL;
463d0183
JB
1969 dev_hold(dev);
1970 }
2a519311 1971 out:
65486c8b 1972 kfree(creq);
2a519311
JB
1973 return err;
1974}
2afe38d1 1975EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2a519311 1976
76a70e9c
JM
1977static char *ieee80211_scan_add_ies(struct iw_request_info *info,
1978 const struct cfg80211_bss_ies *ies,
1979 char *current_ev, char *end_buf)
2a519311 1980{
9caf0364 1981 const u8 *pos, *end, *next;
2a519311
JB
1982 struct iw_event iwe;
1983
9caf0364 1984 if (!ies)
76a70e9c 1985 return current_ev;
2a519311
JB
1986
1987 /*
1988 * If needed, fragment the IEs buffer (at IE boundaries) into short
1989 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1990 */
9caf0364
JB
1991 pos = ies->data;
1992 end = pos + ies->len;
2a519311
JB
1993
1994 while (end - pos > IW_GENERIC_IE_MAX) {
1995 next = pos + 2 + pos[1];
1996 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1997 next = next + 2 + next[1];
1998
1999 memset(&iwe, 0, sizeof(iwe));
2000 iwe.cmd = IWEVGENIE;
2001 iwe.u.data.length = next - pos;
76a70e9c
JM
2002 current_ev = iwe_stream_add_point_check(info, current_ev,
2003 end_buf, &iwe,
2004 (void *)pos);
2005 if (IS_ERR(current_ev))
2006 return current_ev;
2a519311
JB
2007 pos = next;
2008 }
2009
2010 if (end > pos) {
2011 memset(&iwe, 0, sizeof(iwe));
2012 iwe.cmd = IWEVGENIE;
2013 iwe.u.data.length = end - pos;
76a70e9c
JM
2014 current_ev = iwe_stream_add_point_check(info, current_ev,
2015 end_buf, &iwe,
2016 (void *)pos);
2017 if (IS_ERR(current_ev))
2018 return current_ev;
2a519311 2019 }
76a70e9c
JM
2020
2021 return current_ev;
2a519311
JB
2022}
2023
2a519311 2024static char *
77965c97
JB
2025ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2026 struct cfg80211_internal_bss *bss, char *current_ev,
2027 char *end_buf)
2a519311 2028{
9caf0364 2029 const struct cfg80211_bss_ies *ies;
2a519311 2030 struct iw_event iwe;
9caf0364 2031 const u8 *ie;
76a70e9c
JM
2032 u8 buf[50];
2033 u8 *cfg, *p, *tmp;
9caf0364 2034 int rem, i, sig;
2a519311
JB
2035 bool ismesh = false;
2036
2037 memset(&iwe, 0, sizeof(iwe));
2038 iwe.cmd = SIOCGIWAP;
2039 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2040 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
76a70e9c
JM
2041 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2042 IW_EV_ADDR_LEN);
2043 if (IS_ERR(current_ev))
2044 return current_ev;
2a519311
JB
2045
2046 memset(&iwe, 0, sizeof(iwe));
2047 iwe.cmd = SIOCGIWFREQ;
2048 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2049 iwe.u.freq.e = 0;
76a70e9c
JM
2050 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2051 IW_EV_FREQ_LEN);
2052 if (IS_ERR(current_ev))
2053 return current_ev;
2a519311
JB
2054
2055 memset(&iwe, 0, sizeof(iwe));
2056 iwe.cmd = SIOCGIWFREQ;
2057 iwe.u.freq.m = bss->pub.channel->center_freq;
2058 iwe.u.freq.e = 6;
76a70e9c
JM
2059 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2060 IW_EV_FREQ_LEN);
2061 if (IS_ERR(current_ev))
2062 return current_ev;
2a519311 2063
77965c97 2064 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2a519311
JB
2065 memset(&iwe, 0, sizeof(iwe));
2066 iwe.cmd = IWEVQUAL;
2067 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2068 IW_QUAL_NOISE_INVALID |
a77b8552 2069 IW_QUAL_QUAL_UPDATED;
77965c97 2070 switch (wiphy->signal_type) {
2a519311 2071 case CFG80211_SIGNAL_TYPE_MBM:
a77b8552
JB
2072 sig = bss->pub.signal / 100;
2073 iwe.u.qual.level = sig;
2a519311 2074 iwe.u.qual.updated |= IW_QUAL_DBM;
a77b8552
JB
2075 if (sig < -110) /* rather bad */
2076 sig = -110;
2077 else if (sig > -40) /* perfect */
2078 sig = -40;
2079 /* will give a range of 0 .. 70 */
2080 iwe.u.qual.qual = sig + 110;
2a519311
JB
2081 break;
2082 case CFG80211_SIGNAL_TYPE_UNSPEC:
2083 iwe.u.qual.level = bss->pub.signal;
a77b8552
JB
2084 /* will give range 0 .. 100 */
2085 iwe.u.qual.qual = bss->pub.signal;
2a519311
JB
2086 break;
2087 default:
2088 /* not reached */
2089 break;
2090 }
76a70e9c
JM
2091 current_ev = iwe_stream_add_event_check(info, current_ev,
2092 end_buf, &iwe,
2093 IW_EV_QUAL_LEN);
2094 if (IS_ERR(current_ev))
2095 return current_ev;
2a519311
JB
2096 }
2097
2098 memset(&iwe, 0, sizeof(iwe));
2099 iwe.cmd = SIOCGIWENCODE;
2100 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2101 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2102 else
2103 iwe.u.data.flags = IW_ENCODE_DISABLED;
2104 iwe.u.data.length = 0;
76a70e9c
JM
2105 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2106 &iwe, "");
2107 if (IS_ERR(current_ev))
2108 return current_ev;
2a519311 2109
9caf0364
JB
2110 rcu_read_lock();
2111 ies = rcu_dereference(bss->pub.ies);
83c7aa1a
JB
2112 rem = ies->len;
2113 ie = ies->data;
9caf0364 2114
83c7aa1a 2115 while (rem >= 2) {
2a519311
JB
2116 /* invalid data */
2117 if (ie[1] > rem - 2)
2118 break;
2119
2120 switch (ie[0]) {
2121 case WLAN_EID_SSID:
2122 memset(&iwe, 0, sizeof(iwe));
2123 iwe.cmd = SIOCGIWESSID;
2124 iwe.u.data.length = ie[1];
2125 iwe.u.data.flags = 1;
76a70e9c
JM
2126 current_ev = iwe_stream_add_point_check(info,
2127 current_ev,
2128 end_buf, &iwe,
2129 (u8 *)ie + 2);
2130 if (IS_ERR(current_ev))
2131 goto unlock;
2a519311
JB
2132 break;
2133 case WLAN_EID_MESH_ID:
2134 memset(&iwe, 0, sizeof(iwe));
2135 iwe.cmd = SIOCGIWESSID;
2136 iwe.u.data.length = ie[1];
2137 iwe.u.data.flags = 1;
76a70e9c
JM
2138 current_ev = iwe_stream_add_point_check(info,
2139 current_ev,
2140 end_buf, &iwe,
2141 (u8 *)ie + 2);
2142 if (IS_ERR(current_ev))
2143 goto unlock;
2a519311
JB
2144 break;
2145 case WLAN_EID_MESH_CONFIG:
2146 ismesh = true;
136cfa28 2147 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2a519311 2148 break;
9caf0364 2149 cfg = (u8 *)ie + 2;
2a519311
JB
2150 memset(&iwe, 0, sizeof(iwe));
2151 iwe.cmd = IWEVCUSTOM;
76aa5e70
RP
2152 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2153 "0x%02X", cfg[0]);
2a519311 2154 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2155 current_ev = iwe_stream_add_point_check(info,
2156 current_ev,
2157 end_buf,
2158 &iwe, buf);
2159 if (IS_ERR(current_ev))
2160 goto unlock;
76aa5e70
RP
2161 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2162 cfg[1]);
2a519311 2163 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2164 current_ev = iwe_stream_add_point_check(info,
2165 current_ev,
2166 end_buf,
2167 &iwe, buf);
2168 if (IS_ERR(current_ev))
2169 goto unlock;
76aa5e70
RP
2170 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2171 cfg[2]);
2a519311 2172 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2173 current_ev = iwe_stream_add_point_check(info,
2174 current_ev,
2175 end_buf,
2176 &iwe, buf);
2177 if (IS_ERR(current_ev))
2178 goto unlock;
76aa5e70 2179 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2a519311 2180 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2181 current_ev = iwe_stream_add_point_check(info,
2182 current_ev,
2183 end_buf,
2184 &iwe, buf);
2185 if (IS_ERR(current_ev))
2186 goto unlock;
76aa5e70
RP
2187 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2188 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2189 current_ev = iwe_stream_add_point_check(info,
2190 current_ev,
2191 end_buf,
2192 &iwe, buf);
2193 if (IS_ERR(current_ev))
2194 goto unlock;
76aa5e70
RP
2195 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2196 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2197 current_ev = iwe_stream_add_point_check(info,
2198 current_ev,
2199 end_buf,
2200 &iwe, buf);
2201 if (IS_ERR(current_ev))
2202 goto unlock;
76aa5e70 2203 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2a519311 2204 iwe.u.data.length = strlen(buf);
76a70e9c
JM
2205 current_ev = iwe_stream_add_point_check(info,
2206 current_ev,
2207 end_buf,
2208 &iwe, buf);
2209 if (IS_ERR(current_ev))
2210 goto unlock;
2a519311
JB
2211 break;
2212 case WLAN_EID_SUPP_RATES:
2213 case WLAN_EID_EXT_SUPP_RATES:
2214 /* display all supported rates in readable format */
2215 p = current_ev + iwe_stream_lcp_len(info);
2216
2217 memset(&iwe, 0, sizeof(iwe));
2218 iwe.cmd = SIOCGIWRATE;
2219 /* Those two flags are ignored... */
2220 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2221
2222 for (i = 0; i < ie[1]; i++) {
2223 iwe.u.bitrate.value =
2224 ((ie[i + 2] & 0x7f) * 500000);
76a70e9c 2225 tmp = p;
2a519311 2226 p = iwe_stream_add_value(info, current_ev, p,
76a70e9c
JM
2227 end_buf, &iwe,
2228 IW_EV_PARAM_LEN);
2229 if (p == tmp) {
2230 current_ev = ERR_PTR(-E2BIG);
2231 goto unlock;
2232 }
2a519311
JB
2233 }
2234 current_ev = p;
2235 break;
2236 }
2237 rem -= ie[1] + 2;
2238 ie += ie[1] + 2;
2239 }
2240
f64f9e71
JP
2241 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2242 ismesh) {
2a519311
JB
2243 memset(&iwe, 0, sizeof(iwe));
2244 iwe.cmd = SIOCGIWMODE;
2245 if (ismesh)
2246 iwe.u.mode = IW_MODE_MESH;
2247 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2248 iwe.u.mode = IW_MODE_MASTER;
2249 else
2250 iwe.u.mode = IW_MODE_ADHOC;
76a70e9c
JM
2251 current_ev = iwe_stream_add_event_check(info, current_ev,
2252 end_buf, &iwe,
2253 IW_EV_UINT_LEN);
2254 if (IS_ERR(current_ev))
2255 goto unlock;
2a519311
JB
2256 }
2257
76a70e9c
JM
2258 memset(&iwe, 0, sizeof(iwe));
2259 iwe.cmd = IWEVCUSTOM;
2260 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2261 iwe.u.data.length = strlen(buf);
2262 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2263 &iwe, buf);
2264 if (IS_ERR(current_ev))
2265 goto unlock;
2266 memset(&iwe, 0, sizeof(iwe));
2267 iwe.cmd = IWEVCUSTOM;
2268 sprintf(buf, " Last beacon: %ums ago",
2269 elapsed_jiffies_msecs(bss->ts));
2270 iwe.u.data.length = strlen(buf);
2271 current_ev = iwe_stream_add_point_check(info, current_ev,
2272 end_buf, &iwe, buf);
2273 if (IS_ERR(current_ev))
2274 goto unlock;
2275
2276 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2277
2278 unlock:
9caf0364 2279 rcu_read_unlock();
2a519311
JB
2280 return current_ev;
2281}
2282
2283
1b8ec87a 2284static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2a519311
JB
2285 struct iw_request_info *info,
2286 char *buf, size_t len)
2287{
2288 char *current_ev = buf;
2289 char *end_buf = buf + len;
2290 struct cfg80211_internal_bss *bss;
76a70e9c 2291 int err = 0;
2a519311 2292
1b8ec87a
ZG
2293 spin_lock_bh(&rdev->bss_lock);
2294 cfg80211_bss_expire(rdev);
2a519311 2295
1b8ec87a 2296 list_for_each_entry(bss, &rdev->bss_list, list) {
2a519311 2297 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
76a70e9c
JM
2298 err = -E2BIG;
2299 break;
2a519311 2300 }
1b8ec87a 2301 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
77965c97 2302 current_ev, end_buf);
76a70e9c
JM
2303 if (IS_ERR(current_ev)) {
2304 err = PTR_ERR(current_ev);
2305 break;
2306 }
2a519311 2307 }
1b8ec87a 2308 spin_unlock_bh(&rdev->bss_lock);
76a70e9c
JM
2309
2310 if (err)
2311 return err;
2a519311
JB
2312 return current_ev - buf;
2313}
2314
2315
2316int cfg80211_wext_giwscan(struct net_device *dev,
2317 struct iw_request_info *info,
2318 struct iw_point *data, char *extra)
2319{
2320 struct cfg80211_registered_device *rdev;
2321 int res;
2322
2323 if (!netif_running(dev))
2324 return -ENETDOWN;
2325
463d0183 2326 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
2327
2328 if (IS_ERR(rdev))
2329 return PTR_ERR(rdev);
2330
f9d15d16 2331 if (rdev->scan_req || rdev->scan_msg)
5fe231e8 2332 return -EAGAIN;
2a519311
JB
2333
2334 res = ieee80211_scan_results(rdev, info, extra, data->length);
2335 data->length = 0;
2336 if (res >= 0) {
2337 data->length = res;
2338 res = 0;
2339 }
2340
2a519311
JB
2341 return res;
2342}
2afe38d1 2343EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
2a519311 2344#endif