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