blob: f7c6f871a219afb3354293af6cc476594fed162d [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * cfg80211 scan result handling
4 *
5 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright 2016 Intel Deutschland GmbH
8 * Copyright (C) 2018-2019 Intel Corporation
9 */
10#include <linux/kernel.h>
11#include <linux/slab.h>
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>
19#include <net/cfg80211-wext.h>
20#include <net/iw_handler.h>
21#include "core.h"
22#include "nl80211.h"
23#include "wext-compat.h"
24#include "rdev-ops.h"
25
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
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
75#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
76
77static void bss_free(struct cfg80211_internal_bss *bss)
78{
79 struct cfg80211_bss_ies *ies;
80
81 if (WARN_ON(atomic_read(&bss->hold)))
82 return;
83
84 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
85 if (ies && !bss->pub.hidden_beacon_bss)
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);
90
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
98 kfree(bss);
99}
100
101static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
102 struct cfg80211_internal_bss *bss)
103{
104 lockdep_assert_held(&rdev->bss_lock);
105
106 bss->refcount++;
107
108 if (bss->pub.hidden_beacon_bss)
109 bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
110
111 if (bss->pub.transmitted_bss)
112 bss_from_pub(bss->pub.transmitted_bss)->refcount++;
113}
114
115static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
116 struct cfg80211_internal_bss *bss)
117{
118 lockdep_assert_held(&rdev->bss_lock);
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
130 if (bss->pub.transmitted_bss) {
131 struct cfg80211_internal_bss *tbss;
132
133 tbss = container_of(bss->pub.transmitted_bss,
134 struct cfg80211_internal_bss,
135 pub);
136 tbss->refcount--;
137 if (tbss->refcount == 0)
138 bss_free(tbss);
139 }
140
141 bss->refcount--;
142 if (bss->refcount == 0)
143 bss_free(bss);
144}
145
146static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
147 struct cfg80211_internal_bss *bss)
148{
149 lockdep_assert_held(&rdev->bss_lock);
150
151 if (!list_empty(&bss->hidden_list)) {
152 /*
153 * don't remove the beacon entry if it has
154 * probe responses associated with it
155 */
156 if (!bss->pub.hidden_beacon_bss)
157 return false;
158 /*
159 * if it's a probe response entry break its
160 * link to the other entries in the group
161 */
162 list_del_init(&bss->hidden_list);
163 }
164
165 list_del_init(&bss->list);
166 list_del_init(&bss->pub.nontrans_list);
167 rb_erase(&bss->rbn, &rdev->bss_tree);
168 rdev->bss_entries--;
169 WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
170 "rdev bss entries[%d]/list[empty:%d] corruption\n",
171 rdev->bss_entries, list_empty(&rdev->bss_list));
172 bss_ref_put(rdev, bss);
173 return true;
174}
175
176bool cfg80211_is_element_inherited(const struct element *elem,
177 const struct element *non_inherit_elem)
178{
179 u8 id_len, ext_id_len, i, loop_len, id;
180 const u8 *list;
181
182 if (elem->id == WLAN_EID_MULTIPLE_BSSID)
183 return false;
184
185 if (!non_inherit_elem || non_inherit_elem->datalen < 2)
186 return true;
187
188 /*
189 * non inheritance element format is:
190 * ext ID (56) | IDs list len | list | extension IDs list len | list
191 * Both lists are optional. Both lengths are mandatory.
192 * This means valid length is:
193 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
194 */
195 id_len = non_inherit_elem->data[1];
196 if (non_inherit_elem->datalen < 3 + id_len)
197 return true;
198
199 ext_id_len = non_inherit_elem->data[2 + id_len];
200 if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
201 return true;
202
203 if (elem->id == WLAN_EID_EXTENSION) {
204 if (!ext_id_len)
205 return true;
206 loop_len = ext_id_len;
207 list = &non_inherit_elem->data[3 + id_len];
208 id = elem->data[0];
209 } else {
210 if (!id_len)
211 return true;
212 loop_len = id_len;
213 list = &non_inherit_elem->data[2];
214 id = elem->id;
215 }
216
217 for (i = 0; i < loop_len; i++) {
218 if (list[i] == id)
219 return false;
220 }
221
222 return true;
223}
224EXPORT_SYMBOL(cfg80211_is_element_inherited);
225
226static size_t cfg80211_copy_elem_with_frags(const struct element *elem,
227 const u8 *ie, size_t ie_len,
228 u8 **pos, u8 *buf, size_t buf_len)
229{
230 if (WARN_ON((u8 *)elem < ie || elem->data > ie + ie_len ||
231 elem->data + elem->datalen > ie + ie_len))
232 return 0;
233
234 if (elem->datalen + 2 > buf + buf_len - *pos)
235 return 0;
236
237 memcpy(*pos, elem, elem->datalen + 2);
238 *pos += elem->datalen + 2;
239
240 /* Finish if it is not fragmented */
241 if (elem->datalen != 255)
242 return *pos - buf;
243
244 ie_len = ie + ie_len - elem->data - elem->datalen;
245 ie = (const u8 *)elem->data + elem->datalen;
246
247 for_each_element(elem, ie, ie_len) {
248 if (elem->id != WLAN_EID_FRAGMENT)
249 break;
250
251 if (elem->datalen + 2 > buf + buf_len - *pos)
252 return 0;
253
254 memcpy(*pos, elem, elem->datalen + 2);
255 *pos += elem->datalen + 2;
256
257 if (elem->datalen != 255)
258 break;
259 }
260
261 return *pos - buf;
262}
263
264static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
265 const u8 *subie, size_t subie_len,
266 u8 *new_ie, size_t new_ie_len)
267{
268 const struct element *non_inherit_elem, *parent, *sub;
269 u8 *pos = new_ie;
270 u8 id, ext_id;
271 unsigned int match_len;
272
273 non_inherit_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
274 subie, subie_len);
275
276 /* We copy the elements one by one from the parent to the generated
277 * elements.
278 * If they are not inherited (included in subie or in the non
279 * inheritance element), then we copy all occurrences the first time
280 * we see this element type.
281 */
282 for_each_element(parent, ie, ielen) {
283 if (parent->id == WLAN_EID_FRAGMENT)
284 continue;
285
286 if (parent->id == WLAN_EID_EXTENSION) {
287 if (parent->datalen < 1)
288 continue;
289
290 id = WLAN_EID_EXTENSION;
291 ext_id = parent->data[0];
292 match_len = 1;
293 } else {
294 id = parent->id;
295 match_len = 0;
296 }
297
298 /* Find first occurrence in subie */
299 sub = cfg80211_find_elem_match(id, subie, subie_len,
300 &ext_id, match_len, 0);
301
302 /* Copy from parent if not in subie and inherited */
303 if (!sub &&
304 cfg80211_is_element_inherited(parent, non_inherit_elem)) {
305 if (!cfg80211_copy_elem_with_frags(parent,
306 ie, ielen,
307 &pos, new_ie,
308 new_ie_len))
309 return 0;
310
311 continue;
312 }
313
314 /* Already copied if an earlier element had the same type */
315 if (cfg80211_find_elem_match(id, ie, (u8 *)parent - ie,
316 &ext_id, match_len, 0))
317 continue;
318
319 /* Not inheriting, copy all similar elements from subie */
320 while (sub) {
321 if (!cfg80211_copy_elem_with_frags(sub,
322 subie, subie_len,
323 &pos, new_ie,
324 new_ie_len))
325 return 0;
326
327 sub = cfg80211_find_elem_match(id,
328 sub->data + sub->datalen,
329 subie_len + subie -
330 (sub->data +
331 sub->datalen),
332 &ext_id, match_len, 0);
333 }
334 }
335
336 /* The above misses elements that are included in subie but not in the
337 * parent, so do a pass over subie and append those.
338 * Skip the non-tx BSSID caps and non-inheritance element.
339 */
340 for_each_element(sub, subie, subie_len) {
341 if (sub->id == WLAN_EID_NON_TX_BSSID_CAP)
342 continue;
343
344 if (sub->id == WLAN_EID_FRAGMENT)
345 continue;
346
347 if (sub->id == WLAN_EID_EXTENSION) {
348 if (sub->datalen < 1)
349 continue;
350
351 id = WLAN_EID_EXTENSION;
352 ext_id = sub->data[0];
353 match_len = 1;
354
355 if (ext_id == WLAN_EID_EXT_NON_INHERITANCE)
356 continue;
357 } else {
358 id = sub->id;
359 match_len = 0;
360 }
361
362 /* Processed if one was included in the parent */
363 if (cfg80211_find_elem_match(id, ie, ielen,
364 &ext_id, match_len, 0))
365 continue;
366
367 if (!cfg80211_copy_elem_with_frags(sub, subie, subie_len,
368 &pos, new_ie, new_ie_len))
369 return 0;
370 }
371
372 return pos - new_ie;
373}
374
375static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
376 const u8 *ssid, size_t ssid_len)
377{
378 const struct cfg80211_bss_ies *ies;
379 const u8 *ssidie;
380
381 if (bssid && !ether_addr_equal(a->bssid, bssid))
382 return false;
383
384 if (!ssid)
385 return true;
386
387 ies = rcu_access_pointer(a->ies);
388 if (!ies)
389 return false;
390 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
391 if (!ssidie)
392 return false;
393 if (ssidie[1] != ssid_len)
394 return false;
395 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
396}
397
398static int
399cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
400 struct cfg80211_bss *nontrans_bss)
401{
402 const u8 *ssid;
403 size_t ssid_len;
404 struct cfg80211_bss *bss = NULL;
405
406 rcu_read_lock();
407 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
408 if (!ssid) {
409 rcu_read_unlock();
410 return -EINVAL;
411 }
412 ssid_len = ssid[1];
413 ssid = ssid + 2;
414
415 /* check if nontrans_bss is in the list */
416 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
417 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len)) {
418 rcu_read_unlock();
419 return 0;
420 }
421 }
422
423 rcu_read_unlock();
424
425 /*
426 * This is a bit weird - it's not on the list, but already on another
427 * one! The only way that could happen is if there's some BSSID/SSID
428 * shared by multiple APs in their multi-BSSID profiles, potentially
429 * with hidden SSID mixed in ... ignore it.
430 */
431 if (!list_empty(&nontrans_bss->nontrans_list))
432 return -EINVAL;
433
434 /* add to the list */
435 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
436 return 0;
437}
438
439static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
440 unsigned long expire_time)
441{
442 struct cfg80211_internal_bss *bss, *tmp;
443 bool expired = false;
444
445 lockdep_assert_held(&rdev->bss_lock);
446
447 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
448 if (atomic_read(&bss->hold))
449 continue;
450 if (!time_after(expire_time, bss->ts))
451 continue;
452
453 if (__cfg80211_unlink_bss(rdev, bss))
454 expired = true;
455 }
456
457 if (expired)
458 rdev->bss_generation++;
459}
460
461static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
462{
463 struct cfg80211_internal_bss *bss, *oldest = NULL;
464 bool ret;
465
466 lockdep_assert_held(&rdev->bss_lock);
467
468 list_for_each_entry(bss, &rdev->bss_list, list) {
469 if (atomic_read(&bss->hold))
470 continue;
471
472 if (!list_empty(&bss->hidden_list) &&
473 !bss->pub.hidden_beacon_bss)
474 continue;
475
476 if (oldest && time_before(oldest->ts, bss->ts))
477 continue;
478 oldest = bss;
479 }
480
481 if (WARN_ON(!oldest))
482 return false;
483
484 /*
485 * The callers make sure to increase rdev->bss_generation if anything
486 * gets removed (and a new entry added), so there's no need to also do
487 * it here.
488 */
489
490 ret = __cfg80211_unlink_bss(rdev, oldest);
491 WARN_ON(!ret);
492 return ret;
493}
494
495void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
496 bool send_message)
497{
498 struct cfg80211_scan_request *request;
499 struct wireless_dev *wdev;
500 struct sk_buff *msg;
501#ifdef CONFIG_CFG80211_WEXT
502 union iwreq_data wrqu;
503#endif
504
505 ASSERT_RTNL();
506
507 if (rdev->scan_msg) {
508 nl80211_send_scan_msg(rdev, rdev->scan_msg);
509 rdev->scan_msg = NULL;
510 return;
511 }
512
513 request = rdev->scan_req;
514 if (!request)
515 return;
516
517 wdev = request->wdev;
518
519 /*
520 * This must be before sending the other events!
521 * Otherwise, wpa_supplicant gets completely confused with
522 * wext events.
523 */
524 if (wdev->netdev)
525 cfg80211_sme_scan_done(wdev->netdev);
526
527 if (!request->info.aborted &&
528 request->flags & NL80211_SCAN_FLAG_FLUSH) {
529 /* flush entries from previous scans */
530 spin_lock_bh(&rdev->bss_lock);
531 __cfg80211_bss_expire(rdev, request->scan_start);
532 spin_unlock_bh(&rdev->bss_lock);
533 }
534
535 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
536
537#ifdef CONFIG_CFG80211_WEXT
538 if (wdev->netdev && !request->info.aborted) {
539 memset(&wrqu, 0, sizeof(wrqu));
540
541 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
542 }
543#endif
544
545 if (wdev->netdev)
546 dev_put(wdev->netdev);
547
548 rdev->scan_req = NULL;
549 kfree(request);
550
551 if (!send_message)
552 rdev->scan_msg = msg;
553 else
554 nl80211_send_scan_msg(rdev, msg);
555}
556
557void __cfg80211_scan_done(struct work_struct *wk)
558{
559 struct cfg80211_registered_device *rdev;
560
561 rdev = container_of(wk, struct cfg80211_registered_device,
562 scan_done_wk);
563
564 rtnl_lock();
565 ___cfg80211_scan_done(rdev, true);
566 rtnl_unlock();
567}
568
569void cfg80211_scan_done(struct cfg80211_scan_request *request,
570 struct cfg80211_scan_info *info)
571{
572 trace_cfg80211_scan_done(request, info);
573 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
574
575 request->info = *info;
576 request->notified = true;
577 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
578}
579EXPORT_SYMBOL(cfg80211_scan_done);
580
581void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
582 struct cfg80211_sched_scan_request *req)
583{
584 ASSERT_RTNL();
585
586 list_add_rcu(&req->list, &rdev->sched_scan_req_list);
587}
588
589static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
590 struct cfg80211_sched_scan_request *req)
591{
592 ASSERT_RTNL();
593
594 list_del_rcu(&req->list);
595 kfree_rcu(req, rcu_head);
596}
597
598static struct cfg80211_sched_scan_request *
599cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
600{
601 struct cfg80211_sched_scan_request *pos;
602
603 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
604
605 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
606 if (pos->reqid == reqid)
607 return pos;
608 }
609 return NULL;
610}
611
612/*
613 * Determines if a scheduled scan request can be handled. When a legacy
614 * scheduled scan is running no other scheduled scan is allowed regardless
615 * whether the request is for legacy or multi-support scan. When a multi-support
616 * scheduled scan is running a request for legacy scan is not allowed. In this
617 * case a request for multi-support scan can be handled if resources are
618 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
619 */
620int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
621 bool want_multi)
622{
623 struct cfg80211_sched_scan_request *pos;
624 int i = 0;
625
626 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
627 /* request id zero means legacy in progress */
628 if (!i && !pos->reqid)
629 return -EINPROGRESS;
630 i++;
631 }
632
633 if (i) {
634 /* no legacy allowed when multi request(s) are active */
635 if (!want_multi)
636 return -EINPROGRESS;
637
638 /* resource limit reached */
639 if (i == rdev->wiphy.max_sched_scan_reqs)
640 return -ENOSPC;
641 }
642 return 0;
643}
644
645void cfg80211_sched_scan_results_wk(struct work_struct *work)
646{
647 struct cfg80211_registered_device *rdev;
648 struct cfg80211_sched_scan_request *req, *tmp;
649
650 rdev = container_of(work, struct cfg80211_registered_device,
651 sched_scan_res_wk);
652
653 rtnl_lock();
654 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
655 if (req->report_results) {
656 req->report_results = false;
657 if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
658 /* flush entries from previous scans */
659 spin_lock_bh(&rdev->bss_lock);
660 __cfg80211_bss_expire(rdev, req->scan_start);
661 spin_unlock_bh(&rdev->bss_lock);
662 req->scan_start = jiffies;
663 }
664 nl80211_send_sched_scan(req,
665 NL80211_CMD_SCHED_SCAN_RESULTS);
666 }
667 }
668 rtnl_unlock();
669}
670
671void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
672{
673 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
674 struct cfg80211_sched_scan_request *request;
675
676 trace_cfg80211_sched_scan_results(wiphy, reqid);
677 /* ignore if we're not scanning */
678
679 rcu_read_lock();
680 request = cfg80211_find_sched_scan_req(rdev, reqid);
681 if (request) {
682 request->report_results = true;
683 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
684 }
685 rcu_read_unlock();
686}
687EXPORT_SYMBOL(cfg80211_sched_scan_results);
688
689void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
690{
691 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
692
693 ASSERT_RTNL();
694
695 trace_cfg80211_sched_scan_stopped(wiphy, reqid);
696
697 __cfg80211_stop_sched_scan(rdev, reqid, true);
698}
699EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
700
701void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
702{
703 rtnl_lock();
704 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
705 rtnl_unlock();
706}
707EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
708
709int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
710 struct cfg80211_sched_scan_request *req,
711 bool driver_initiated)
712{
713 ASSERT_RTNL();
714
715 if (!driver_initiated) {
716 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
717 if (err)
718 return err;
719 }
720
721 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
722
723 cfg80211_del_sched_scan_req(rdev, req);
724
725 return 0;
726}
727
728int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
729 u64 reqid, bool driver_initiated)
730{
731 struct cfg80211_sched_scan_request *sched_scan_req;
732
733 ASSERT_RTNL();
734
735 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
736 if (!sched_scan_req)
737 return -ENOENT;
738
739 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
740 driver_initiated);
741}
742
743void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
744 unsigned long age_secs)
745{
746 struct cfg80211_internal_bss *bss;
747 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
748
749 spin_lock_bh(&rdev->bss_lock);
750 list_for_each_entry(bss, &rdev->bss_list, list)
751 bss->ts -= age_jiffies;
752 spin_unlock_bh(&rdev->bss_lock);
753}
754
755void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
756{
757 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
758}
759
760const struct element *
761cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
762 const u8 *match, unsigned int match_len,
763 unsigned int match_offset)
764{
765 const struct element *elem;
766
767 for_each_element_id(elem, eid, ies, len) {
768 if (elem->datalen >= match_offset + match_len &&
769 !memcmp(elem->data + match_offset, match, match_len))
770 return elem;
771 }
772
773 return NULL;
774}
775EXPORT_SYMBOL(cfg80211_find_elem_match);
776
777const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
778 const u8 *ies,
779 unsigned int len)
780{
781 const struct element *elem;
782 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
783 int match_len = (oui_type < 0) ? 3 : sizeof(match);
784
785 if (WARN_ON(oui_type > 0xff))
786 return NULL;
787
788 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
789 match, match_len, 0);
790
791 if (!elem || elem->datalen < 4)
792 return NULL;
793
794 return elem;
795}
796EXPORT_SYMBOL(cfg80211_find_vendor_elem);
797
798/**
799 * enum bss_compare_mode - BSS compare mode
800 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
801 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
802 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
803 */
804enum bss_compare_mode {
805 BSS_CMP_REGULAR,
806 BSS_CMP_HIDE_ZLEN,
807 BSS_CMP_HIDE_NUL,
808};
809
810static int cmp_bss(struct cfg80211_bss *a,
811 struct cfg80211_bss *b,
812 enum bss_compare_mode mode)
813{
814 const struct cfg80211_bss_ies *a_ies, *b_ies;
815 const u8 *ie1 = NULL;
816 const u8 *ie2 = NULL;
817 int i, r;
818
819 if (a->channel != b->channel)
820 return b->channel->center_freq - a->channel->center_freq;
821
822 a_ies = rcu_access_pointer(a->ies);
823 if (!a_ies)
824 return -1;
825 b_ies = rcu_access_pointer(b->ies);
826 if (!b_ies)
827 return 1;
828
829 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
830 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
831 a_ies->data, a_ies->len);
832 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
833 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
834 b_ies->data, b_ies->len);
835 if (ie1 && ie2) {
836 int mesh_id_cmp;
837
838 if (ie1[1] == ie2[1])
839 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
840 else
841 mesh_id_cmp = ie2[1] - ie1[1];
842
843 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
844 a_ies->data, a_ies->len);
845 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
846 b_ies->data, b_ies->len);
847 if (ie1 && ie2) {
848 if (mesh_id_cmp)
849 return mesh_id_cmp;
850 if (ie1[1] != ie2[1])
851 return ie2[1] - ie1[1];
852 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
853 }
854 }
855
856 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
857 if (r)
858 return r;
859
860 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
861 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
862
863 if (!ie1 && !ie2)
864 return 0;
865
866 /*
867 * Note that with "hide_ssid", the function returns a match if
868 * the already-present BSS ("b") is a hidden SSID beacon for
869 * the new BSS ("a").
870 */
871
872 /* sort missing IE before (left of) present IE */
873 if (!ie1)
874 return -1;
875 if (!ie2)
876 return 1;
877
878 switch (mode) {
879 case BSS_CMP_HIDE_ZLEN:
880 /*
881 * In ZLEN mode we assume the BSS entry we're
882 * looking for has a zero-length SSID. So if
883 * the one we're looking at right now has that,
884 * return 0. Otherwise, return the difference
885 * in length, but since we're looking for the
886 * 0-length it's really equivalent to returning
887 * the length of the one we're looking at.
888 *
889 * No content comparison is needed as we assume
890 * the content length is zero.
891 */
892 return ie2[1];
893 case BSS_CMP_REGULAR:
894 default:
895 /* sort by length first, then by contents */
896 if (ie1[1] != ie2[1])
897 return ie2[1] - ie1[1];
898 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
899 case BSS_CMP_HIDE_NUL:
900 if (ie1[1] != ie2[1])
901 return ie2[1] - ie1[1];
902 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
903 for (i = 0; i < ie2[1]; i++)
904 if (ie2[i + 2])
905 return -1;
906 return 0;
907 }
908}
909
910static bool cfg80211_bss_type_match(u16 capability,
911 enum nl80211_band band,
912 enum ieee80211_bss_type bss_type)
913{
914 bool ret = true;
915 u16 mask, val;
916
917 if (bss_type == IEEE80211_BSS_TYPE_ANY)
918 return ret;
919
920 if (band == NL80211_BAND_60GHZ) {
921 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
922 switch (bss_type) {
923 case IEEE80211_BSS_TYPE_ESS:
924 val = WLAN_CAPABILITY_DMG_TYPE_AP;
925 break;
926 case IEEE80211_BSS_TYPE_PBSS:
927 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
928 break;
929 case IEEE80211_BSS_TYPE_IBSS:
930 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
931 break;
932 default:
933 return false;
934 }
935 } else {
936 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
937 switch (bss_type) {
938 case IEEE80211_BSS_TYPE_ESS:
939 val = WLAN_CAPABILITY_ESS;
940 break;
941 case IEEE80211_BSS_TYPE_IBSS:
942 val = WLAN_CAPABILITY_IBSS;
943 break;
944 case IEEE80211_BSS_TYPE_MBSS:
945 val = 0;
946 break;
947 default:
948 return false;
949 }
950 }
951
952 ret = ((capability & mask) == val);
953 return ret;
954}
955
956/* Returned bss is reference counted and must be cleaned up appropriately. */
957struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
958 struct ieee80211_channel *channel,
959 const u8 *bssid,
960 const u8 *ssid, size_t ssid_len,
961 enum ieee80211_bss_type bss_type,
962 enum ieee80211_privacy privacy)
963{
964 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
965 struct cfg80211_internal_bss *bss, *res = NULL;
966 unsigned long now = jiffies;
967 int bss_privacy;
968
969 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
970 privacy);
971
972 spin_lock_bh(&rdev->bss_lock);
973
974 list_for_each_entry(bss, &rdev->bss_list, list) {
975 if (!cfg80211_bss_type_match(bss->pub.capability,
976 bss->pub.channel->band, bss_type))
977 continue;
978
979 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
980 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
981 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
982 continue;
983 if (channel && bss->pub.channel != channel)
984 continue;
985 if (!is_valid_ether_addr(bss->pub.bssid))
986 continue;
987 /* Don't get expired BSS structs */
988 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
989 !atomic_read(&bss->hold))
990 continue;
991 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
992 res = bss;
993 bss_ref_get(rdev, res);
994 break;
995 }
996 }
997
998 spin_unlock_bh(&rdev->bss_lock);
999 if (!res)
1000 return NULL;
1001 trace_cfg80211_return_bss(&res->pub);
1002 return &res->pub;
1003}
1004EXPORT_SYMBOL(cfg80211_get_bss);
1005
1006static bool rb_insert_bss(struct cfg80211_registered_device *rdev,
1007 struct cfg80211_internal_bss *bss)
1008{
1009 struct rb_node **p = &rdev->bss_tree.rb_node;
1010 struct rb_node *parent = NULL;
1011 struct cfg80211_internal_bss *tbss;
1012 int cmp;
1013
1014 while (*p) {
1015 parent = *p;
1016 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1017
1018 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1019
1020 if (WARN_ON(!cmp)) {
1021 /* will sort of leak this BSS */
1022 return false;
1023 }
1024
1025 if (cmp < 0)
1026 p = &(*p)->rb_left;
1027 else
1028 p = &(*p)->rb_right;
1029 }
1030
1031 rb_link_node(&bss->rbn, parent, p);
1032 rb_insert_color(&bss->rbn, &rdev->bss_tree);
1033 return true;
1034}
1035
1036static struct cfg80211_internal_bss *
1037rb_find_bss(struct cfg80211_registered_device *rdev,
1038 struct cfg80211_internal_bss *res,
1039 enum bss_compare_mode mode)
1040{
1041 struct rb_node *n = rdev->bss_tree.rb_node;
1042 struct cfg80211_internal_bss *bss;
1043 int r;
1044
1045 while (n) {
1046 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1047 r = cmp_bss(&res->pub, &bss->pub, mode);
1048
1049 if (r == 0)
1050 return bss;
1051 else if (r < 0)
1052 n = n->rb_left;
1053 else
1054 n = n->rb_right;
1055 }
1056
1057 return NULL;
1058}
1059
1060static void cfg80211_insert_bss(struct cfg80211_registered_device *rdev,
1061 struct cfg80211_internal_bss *bss)
1062{
1063 lockdep_assert_held(&rdev->bss_lock);
1064
1065 if (!rb_insert_bss(rdev, bss))
1066 return;
1067 list_add_tail(&bss->list, &rdev->bss_list);
1068 rdev->bss_entries++;
1069}
1070
1071static void cfg80211_rehash_bss(struct cfg80211_registered_device *rdev,
1072 struct cfg80211_internal_bss *bss)
1073{
1074 lockdep_assert_held(&rdev->bss_lock);
1075
1076 rb_erase(&bss->rbn, &rdev->bss_tree);
1077 if (!rb_insert_bss(rdev, bss)) {
1078 list_del(&bss->list);
1079 if (!list_empty(&bss->hidden_list))
1080 list_del_init(&bss->hidden_list);
1081 if (!list_empty(&bss->pub.nontrans_list))
1082 list_del_init(&bss->pub.nontrans_list);
1083 rdev->bss_entries--;
1084 }
1085 rdev->bss_generation++;
1086}
1087
1088static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1089 struct cfg80211_internal_bss *new)
1090{
1091 const struct cfg80211_bss_ies *ies;
1092 struct cfg80211_internal_bss *bss;
1093 const u8 *ie;
1094 int i, ssidlen;
1095 u8 fold = 0;
1096 u32 n_entries = 0;
1097
1098 ies = rcu_access_pointer(new->pub.beacon_ies);
1099 if (WARN_ON(!ies))
1100 return false;
1101
1102 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1103 if (!ie) {
1104 /* nothing to do */
1105 return true;
1106 }
1107
1108 ssidlen = ie[1];
1109 for (i = 0; i < ssidlen; i++)
1110 fold |= ie[2 + i];
1111
1112 if (fold) {
1113 /* not a hidden SSID */
1114 return true;
1115 }
1116
1117 /* This is the bad part ... */
1118
1119 list_for_each_entry(bss, &rdev->bss_list, list) {
1120 /*
1121 * we're iterating all the entries anyway, so take the
1122 * opportunity to validate the list length accounting
1123 */
1124 n_entries++;
1125
1126 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1127 continue;
1128 if (bss->pub.channel != new->pub.channel)
1129 continue;
1130 if (bss->pub.scan_width != new->pub.scan_width)
1131 continue;
1132 if (rcu_access_pointer(bss->pub.beacon_ies))
1133 continue;
1134 ies = rcu_access_pointer(bss->pub.ies);
1135 if (!ies)
1136 continue;
1137 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1138 if (!ie)
1139 continue;
1140 if (ssidlen && ie[1] != ssidlen)
1141 continue;
1142 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1143 continue;
1144 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1145 list_del(&bss->hidden_list);
1146 /* combine them */
1147 list_add(&bss->hidden_list, &new->hidden_list);
1148 bss->pub.hidden_beacon_bss = &new->pub;
1149 new->refcount += bss->refcount;
1150 rcu_assign_pointer(bss->pub.beacon_ies,
1151 new->pub.beacon_ies);
1152 }
1153
1154 WARN_ONCE(n_entries != rdev->bss_entries,
1155 "rdev bss entries[%d]/list[len:%d] corruption\n",
1156 rdev->bss_entries, n_entries);
1157
1158 return true;
1159}
1160
1161struct cfg80211_non_tx_bss {
1162 struct cfg80211_bss *tx_bss;
1163 u8 max_bssid_indicator;
1164 u8 bssid_index;
1165};
1166
1167static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1168 const struct cfg80211_bss_ies *new_ies,
1169 const struct cfg80211_bss_ies *old_ies)
1170{
1171 struct cfg80211_internal_bss *bss;
1172
1173 /* Assign beacon IEs to all sub entries */
1174 list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1175 const struct cfg80211_bss_ies *ies;
1176
1177 ies = rcu_access_pointer(bss->pub.beacon_ies);
1178 WARN_ON(ies != old_ies);
1179
1180 rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1181 }
1182}
1183
1184static bool
1185cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1186 struct cfg80211_internal_bss *known,
1187 struct cfg80211_internal_bss *new,
1188 bool signal_valid)
1189{
1190 lockdep_assert_held(&rdev->bss_lock);
1191
1192 /* Update IEs */
1193 if (rcu_access_pointer(new->pub.proberesp_ies)) {
1194 const struct cfg80211_bss_ies *old;
1195
1196 old = rcu_access_pointer(known->pub.proberesp_ies);
1197
1198 rcu_assign_pointer(known->pub.proberesp_ies,
1199 new->pub.proberesp_ies);
1200 /* Override possible earlier Beacon frame IEs */
1201 rcu_assign_pointer(known->pub.ies,
1202 new->pub.proberesp_ies);
1203 if (old)
1204 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1205 } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1206 const struct cfg80211_bss_ies *old;
1207
1208 if (known->pub.hidden_beacon_bss &&
1209 !list_empty(&known->hidden_list)) {
1210 const struct cfg80211_bss_ies *f;
1211
1212 /* The known BSS struct is one of the probe
1213 * response members of a group, but we're
1214 * receiving a beacon (beacon_ies in the new
1215 * bss is used). This can only mean that the
1216 * AP changed its beacon from not having an
1217 * SSID to showing it, which is confusing so
1218 * drop this information.
1219 */
1220
1221 f = rcu_access_pointer(new->pub.beacon_ies);
1222 kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1223 return false;
1224 }
1225
1226 old = rcu_access_pointer(known->pub.beacon_ies);
1227
1228 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1229
1230 /* Override IEs if they were from a beacon before */
1231 if (old == rcu_access_pointer(known->pub.ies))
1232 rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1233
1234 cfg80211_update_hidden_bsses(known,
1235 rcu_access_pointer(new->pub.beacon_ies),
1236 old);
1237
1238 if (old)
1239 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1240 }
1241
1242 known->pub.beacon_interval = new->pub.beacon_interval;
1243
1244 /* don't update the signal if beacon was heard on
1245 * adjacent channel.
1246 */
1247 if (signal_valid)
1248 known->pub.signal = new->pub.signal;
1249 known->pub.capability = new->pub.capability;
1250 known->ts = new->ts;
1251 known->ts_boottime = new->ts_boottime;
1252 known->parent_tsf = new->parent_tsf;
1253 known->pub.chains = new->pub.chains;
1254 memcpy(known->pub.chain_signal, new->pub.chain_signal,
1255 IEEE80211_MAX_CHAINS);
1256 ether_addr_copy(known->parent_bssid, new->parent_bssid);
1257 known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1258 known->pub.bssid_index = new->pub.bssid_index;
1259
1260 return true;
1261}
1262
1263/* Returned bss is reference counted and must be cleaned up appropriately. */
1264struct cfg80211_internal_bss *
1265cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1266 struct cfg80211_internal_bss *tmp,
1267 bool signal_valid, unsigned long ts)
1268{
1269 struct cfg80211_internal_bss *found = NULL;
1270
1271 if (WARN_ON(!tmp->pub.channel))
1272 return NULL;
1273
1274 tmp->ts = ts;
1275
1276 spin_lock_bh(&rdev->bss_lock);
1277
1278 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1279 spin_unlock_bh(&rdev->bss_lock);
1280 return NULL;
1281 }
1282
1283 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1284
1285 if (found) {
1286 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1287 goto drop;
1288 } else {
1289 struct cfg80211_internal_bss *new;
1290 struct cfg80211_internal_bss *hidden;
1291 struct cfg80211_bss_ies *ies;
1292
1293 /*
1294 * create a copy -- the "res" variable that is passed in
1295 * is allocated on the stack since it's not needed in the
1296 * more common case of an update
1297 */
1298 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1299 GFP_ATOMIC);
1300 if (!new) {
1301 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1302 if (ies)
1303 kfree_rcu(ies, rcu_head);
1304 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1305 if (ies)
1306 kfree_rcu(ies, rcu_head);
1307 goto drop;
1308 }
1309 memcpy(new, tmp, sizeof(*new));
1310 new->refcount = 1;
1311 INIT_LIST_HEAD(&new->hidden_list);
1312 INIT_LIST_HEAD(&new->pub.nontrans_list);
1313 /* we'll set this later if it was non-NULL */
1314 new->pub.transmitted_bss = NULL;
1315
1316 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1317 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1318 if (!hidden)
1319 hidden = rb_find_bss(rdev, tmp,
1320 BSS_CMP_HIDE_NUL);
1321 if (hidden) {
1322 new->pub.hidden_beacon_bss = &hidden->pub;
1323 list_add(&new->hidden_list,
1324 &hidden->hidden_list);
1325 hidden->refcount++;
1326
1327 ies = (void *)rcu_access_pointer(new->pub.beacon_ies);
1328 rcu_assign_pointer(new->pub.beacon_ies,
1329 hidden->pub.beacon_ies);
1330 if (ies)
1331 kfree_rcu(ies, rcu_head);
1332 }
1333 } else {
1334 /*
1335 * Ok so we found a beacon, and don't have an entry. If
1336 * it's a beacon with hidden SSID, we might be in for an
1337 * expensive search for any probe responses that should
1338 * be grouped with this beacon for updates ...
1339 */
1340 if (!cfg80211_combine_bsses(rdev, new)) {
1341 bss_ref_put(rdev, new);
1342 goto drop;
1343 }
1344 }
1345
1346 if (rdev->bss_entries >= bss_entries_limit &&
1347 !cfg80211_bss_expire_oldest(rdev)) {
1348 bss_ref_put(rdev, new);
1349 goto drop;
1350 }
1351
1352 /* This must be before the call to bss_ref_get */
1353 if (tmp->pub.transmitted_bss) {
1354 struct cfg80211_internal_bss *pbss =
1355 container_of(tmp->pub.transmitted_bss,
1356 struct cfg80211_internal_bss,
1357 pub);
1358
1359 new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1360 bss_ref_get(rdev, pbss);
1361 }
1362
1363 cfg80211_insert_bss(rdev, new);
1364 found = new;
1365 }
1366
1367 rdev->bss_generation++;
1368 bss_ref_get(rdev, found);
1369 spin_unlock_bh(&rdev->bss_lock);
1370
1371 return found;
1372 drop:
1373 spin_unlock_bh(&rdev->bss_lock);
1374 return NULL;
1375}
1376
1377/*
1378 * Update RX channel information based on the available frame payload
1379 * information. This is mainly for the 2.4 GHz band where frames can be received
1380 * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1381 * element to indicate the current (transmitting) channel, but this might also
1382 * be needed on other bands if RX frequency does not match with the actual
1383 * operating channel of a BSS.
1384 */
1385static struct ieee80211_channel *
1386cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1387 struct ieee80211_channel *channel,
1388 enum nl80211_bss_scan_width scan_width)
1389{
1390 const u8 *tmp;
1391 u32 freq;
1392 int channel_number = -1;
1393 struct ieee80211_channel *alt_channel;
1394
1395 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1396 if (tmp && tmp[1] == 1) {
1397 channel_number = tmp[2];
1398 } else {
1399 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1400 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1401 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1402
1403 channel_number = htop->primary_chan;
1404 }
1405 }
1406
1407 if (channel_number < 0) {
1408 /* No channel information in frame payload */
1409 return channel;
1410 }
1411
1412 freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1413 alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1414 if (!alt_channel) {
1415 if (channel->band == NL80211_BAND_2GHZ) {
1416 /*
1417 * Better not allow unexpected channels when that could
1418 * be going beyond the 1-11 range (e.g., discovering
1419 * BSS on channel 12 when radio is configured for
1420 * channel 11.
1421 */
1422 return NULL;
1423 }
1424
1425 /* No match for the payload channel number - ignore it */
1426 return channel;
1427 }
1428
1429 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1430 scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1431 /*
1432 * Ignore channel number in 5 and 10 MHz channels where there
1433 * may not be an n:1 or 1:n mapping between frequencies and
1434 * channel numbers.
1435 */
1436 return channel;
1437 }
1438
1439 /*
1440 * Use the channel determined through the payload channel number
1441 * instead of the RX channel reported by the driver.
1442 */
1443 if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1444 return NULL;
1445 return alt_channel;
1446}
1447
1448/* Returned bss is reference counted and must be cleaned up appropriately. */
1449static struct cfg80211_bss *
1450cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1451 struct cfg80211_inform_bss *data,
1452 enum cfg80211_bss_frame_type ftype,
1453 const u8 *bssid, u64 tsf, u16 capability,
1454 u16 beacon_interval, const u8 *ie, size_t ielen,
1455 struct cfg80211_non_tx_bss *non_tx_data,
1456 gfp_t gfp)
1457{
1458 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1459 struct cfg80211_bss_ies *ies;
1460 struct ieee80211_channel *channel;
1461 struct cfg80211_internal_bss tmp = {}, *res;
1462 int bss_type;
1463 bool signal_valid;
1464 unsigned long ts;
1465
1466 if (WARN_ON(!wiphy))
1467 return NULL;
1468
1469 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1470 (data->signal < 0 || data->signal > 100)))
1471 return NULL;
1472
1473 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1474 data->scan_width);
1475 if (!channel)
1476 return NULL;
1477
1478 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1479 tmp.pub.channel = channel;
1480 tmp.pub.scan_width = data->scan_width;
1481 tmp.pub.signal = data->signal;
1482 tmp.pub.beacon_interval = beacon_interval;
1483 tmp.pub.capability = capability;
1484 tmp.ts_boottime = data->boottime_ns;
1485 if (non_tx_data) {
1486 tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1487 ts = bss_from_pub(non_tx_data->tx_bss)->ts;
1488 tmp.pub.bssid_index = non_tx_data->bssid_index;
1489 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1490 } else {
1491 ts = jiffies;
1492 }
1493
1494 /*
1495 * If we do not know here whether the IEs are from a Beacon or Probe
1496 * Response frame, we need to pick one of the options and only use it
1497 * with the driver that does not provide the full Beacon/Probe Response
1498 * frame. Use Beacon frame pointer to avoid indicating that this should
1499 * override the IEs pointer should we have received an earlier
1500 * indication of Probe Response data.
1501 */
1502 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1503 if (!ies)
1504 return NULL;
1505 ies->len = ielen;
1506 ies->tsf = tsf;
1507 ies->from_beacon = false;
1508 memcpy(ies->data, ie, ielen);
1509
1510 switch (ftype) {
1511 case CFG80211_BSS_FTYPE_BEACON:
1512 ies->from_beacon = true;
1513 /* fall through */
1514 case CFG80211_BSS_FTYPE_UNKNOWN:
1515 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1516 break;
1517 case CFG80211_BSS_FTYPE_PRESP:
1518 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1519 break;
1520 }
1521 rcu_assign_pointer(tmp.pub.ies, ies);
1522
1523 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1524 wiphy->max_adj_channel_rssi_comp;
1525 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
1526 if (!res)
1527 return NULL;
1528
1529 if (channel->band == NL80211_BAND_60GHZ) {
1530 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1531 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1532 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1533 regulatory_hint_found_beacon(wiphy, channel, gfp);
1534 } else {
1535 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1536 regulatory_hint_found_beacon(wiphy, channel, gfp);
1537 }
1538
1539 if (non_tx_data) {
1540 /* this is a nontransmitting bss, we need to add it to
1541 * transmitting bss' list if it is not there
1542 */
1543 spin_lock_bh(&rdev->bss_lock);
1544 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1545 &res->pub)) {
1546 if (__cfg80211_unlink_bss(rdev, res)) {
1547 rdev->bss_generation++;
1548 res = NULL;
1549 }
1550 }
1551 spin_unlock_bh(&rdev->bss_lock);
1552
1553 if (!res)
1554 return NULL;
1555 }
1556
1557 trace_cfg80211_return_bss(&res->pub);
1558 /* cfg80211_bss_update gives us a referenced result */
1559 return &res->pub;
1560}
1561
1562static const struct element
1563*cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
1564 const struct element *mbssid_elem,
1565 const struct element *sub_elem)
1566{
1567 const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
1568 const struct element *next_mbssid;
1569 const struct element *next_sub;
1570
1571 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
1572 mbssid_end,
1573 ielen - (mbssid_end - ie));
1574
1575 /*
1576 * If is is not the last subelement in current MBSSID IE or there isn't
1577 * a next MBSSID IE - profile is complete.
1578 */
1579 if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
1580 !next_mbssid)
1581 return NULL;
1582
1583 /* For any length error, just return NULL */
1584
1585 if (next_mbssid->datalen < 4)
1586 return NULL;
1587
1588 next_sub = (void *)&next_mbssid->data[1];
1589
1590 if (next_mbssid->data + next_mbssid->datalen <
1591 next_sub->data + next_sub->datalen)
1592 return NULL;
1593
1594 if (next_sub->id != 0 || next_sub->datalen < 2)
1595 return NULL;
1596
1597 /*
1598 * Check if the first element in the next sub element is a start
1599 * of a new profile
1600 */
1601 return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
1602 NULL : next_mbssid;
1603}
1604
1605size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
1606 const struct element *mbssid_elem,
1607 const struct element *sub_elem,
1608 u8 *merged_ie, size_t max_copy_len)
1609{
1610 size_t copied_len = sub_elem->datalen;
1611 const struct element *next_mbssid;
1612
1613 if (sub_elem->datalen > max_copy_len)
1614 return 0;
1615
1616 memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
1617
1618 while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
1619 mbssid_elem,
1620 sub_elem))) {
1621 const struct element *next_sub = (void *)&next_mbssid->data[1];
1622
1623 if (copied_len + next_sub->datalen > max_copy_len)
1624 break;
1625 memcpy(merged_ie + copied_len, next_sub->data,
1626 next_sub->datalen);
1627 copied_len += next_sub->datalen;
1628 }
1629
1630 return copied_len;
1631}
1632EXPORT_SYMBOL(cfg80211_merge_profile);
1633
1634static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1635 struct cfg80211_inform_bss *data,
1636 enum cfg80211_bss_frame_type ftype,
1637 const u8 *bssid, u64 tsf,
1638 u16 beacon_interval, const u8 *ie,
1639 size_t ielen,
1640 struct cfg80211_non_tx_bss *non_tx_data,
1641 gfp_t gfp)
1642{
1643 const u8 *mbssid_index_ie;
1644 const struct element *elem, *sub;
1645 size_t new_ie_len;
1646 u8 new_bssid[ETH_ALEN];
1647 u8 *new_ie, *profile;
1648 u64 seen_indices = 0;
1649 u16 capability;
1650 struct cfg80211_bss *bss;
1651
1652 if (!non_tx_data)
1653 return;
1654 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1655 return;
1656 if (!wiphy->support_mbssid)
1657 return;
1658 if (wiphy->support_only_he_mbssid &&
1659 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1660 return;
1661
1662 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1663 if (!new_ie)
1664 return;
1665
1666 profile = kmalloc(ielen, gfp);
1667 if (!profile)
1668 goto out;
1669
1670 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1671 if (elem->datalen < 4)
1672 continue;
1673 if (elem->data[0] < 1 || (int)elem->data[0] > 8)
1674 continue;
1675 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1676 u8 profile_len;
1677
1678 if (sub->id != 0 || sub->datalen < 4) {
1679 /* not a valid BSS profile */
1680 continue;
1681 }
1682
1683 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1684 sub->data[1] != 2) {
1685 /* The first element within the Nontransmitted
1686 * BSSID Profile is not the Nontransmitted
1687 * BSSID Capability element.
1688 */
1689 continue;
1690 }
1691
1692 memset(profile, 0, ielen);
1693 profile_len = cfg80211_merge_profile(ie, ielen,
1694 elem,
1695 sub,
1696 profile,
1697 ielen);
1698
1699 /* found a Nontransmitted BSSID Profile */
1700 mbssid_index_ie = cfg80211_find_ie
1701 (WLAN_EID_MULTI_BSSID_IDX,
1702 profile, profile_len);
1703 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1704 mbssid_index_ie[2] == 0 ||
1705 mbssid_index_ie[2] > 46) {
1706 /* No valid Multiple BSSID-Index element */
1707 continue;
1708 }
1709
1710 if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
1711 /* We don't support legacy split of a profile */
1712 net_dbg_ratelimited("Partial info for BSSID index %d\n",
1713 mbssid_index_ie[2]);
1714
1715 seen_indices |= BIT_ULL(mbssid_index_ie[2]);
1716
1717 non_tx_data->bssid_index = mbssid_index_ie[2];
1718 non_tx_data->max_bssid_indicator = elem->data[0];
1719
1720 cfg80211_gen_new_bssid(bssid,
1721 non_tx_data->max_bssid_indicator,
1722 non_tx_data->bssid_index,
1723 new_bssid);
1724 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
1725 new_ie_len = cfg80211_gen_new_ie(ie, ielen,
1726 profile,
1727 profile_len, new_ie,
1728 IEEE80211_MAX_DATA_LEN);
1729 if (!new_ie_len)
1730 continue;
1731
1732 capability = get_unaligned_le16(profile + 2);
1733 bss = cfg80211_inform_single_bss_data(wiphy, data,
1734 ftype,
1735 new_bssid, tsf,
1736 capability,
1737 beacon_interval,
1738 new_ie,
1739 new_ie_len,
1740 non_tx_data,
1741 gfp);
1742 if (!bss)
1743 break;
1744 cfg80211_put_bss(wiphy, bss);
1745 }
1746 }
1747
1748out:
1749 kfree(new_ie);
1750 kfree(profile);
1751}
1752
1753struct cfg80211_bss *
1754cfg80211_inform_bss_data(struct wiphy *wiphy,
1755 struct cfg80211_inform_bss *data,
1756 enum cfg80211_bss_frame_type ftype,
1757 const u8 *bssid, u64 tsf, u16 capability,
1758 u16 beacon_interval, const u8 *ie, size_t ielen,
1759 gfp_t gfp)
1760{
1761 struct cfg80211_bss *res;
1762 struct cfg80211_non_tx_bss non_tx_data;
1763
1764 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1765 capability, beacon_interval, ie,
1766 ielen, NULL, gfp);
1767 if (!res)
1768 return NULL;
1769 non_tx_data.tx_bss = res;
1770 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
1771 beacon_interval, ie, ielen, &non_tx_data,
1772 gfp);
1773 return res;
1774}
1775EXPORT_SYMBOL(cfg80211_inform_bss_data);
1776
1777static void
1778cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1779 struct cfg80211_inform_bss *data,
1780 struct ieee80211_mgmt *mgmt, size_t len,
1781 struct cfg80211_non_tx_bss *non_tx_data,
1782 gfp_t gfp)
1783{
1784 enum cfg80211_bss_frame_type ftype;
1785 const u8 *ie = mgmt->u.probe_resp.variable;
1786 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1787 u.probe_resp.variable);
1788
1789 ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1790 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1791
1792 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1793 le64_to_cpu(mgmt->u.probe_resp.timestamp),
1794 le16_to_cpu(mgmt->u.probe_resp.beacon_int),
1795 ie, ielen, non_tx_data, gfp);
1796}
1797
1798static void
1799cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
1800 struct cfg80211_bss *nontrans_bss,
1801 struct ieee80211_mgmt *mgmt, size_t len)
1802{
1803 u8 *ie, *new_ie, *pos;
1804 const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1805 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1806 u.probe_resp.variable);
1807 size_t new_ie_len;
1808 struct cfg80211_bss_ies *new_ies;
1809 const struct cfg80211_bss_ies *old;
1810 size_t cpy_len;
1811
1812 lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
1813
1814 ie = mgmt->u.probe_resp.variable;
1815
1816 new_ie_len = ielen;
1817 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1818 if (!trans_ssid)
1819 return;
1820 new_ie_len -= trans_ssid[1];
1821 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1822 /*
1823 * It's not valid to have the MBSSID element before SSID
1824 * ignore if that happens - the code below assumes it is
1825 * after (while copying things inbetween).
1826 */
1827 if (!mbssid || mbssid < trans_ssid)
1828 return;
1829 new_ie_len -= mbssid[1];
1830
1831 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
1832 if (!nontrans_ssid)
1833 return;
1834
1835 new_ie_len += nontrans_ssid[1];
1836
1837 /* generate new ie for nontrans BSS
1838 * 1. replace SSID with nontrans BSS' SSID
1839 * 2. skip MBSSID IE
1840 */
1841 new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
1842 if (!new_ie)
1843 return;
1844
1845 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
1846 if (!new_ies)
1847 goto out_free;
1848
1849 pos = new_ie;
1850
1851 /* copy the nontransmitted SSID */
1852 cpy_len = nontrans_ssid[1] + 2;
1853 memcpy(pos, nontrans_ssid, cpy_len);
1854 pos += cpy_len;
1855 /* copy the IEs between SSID and MBSSID */
1856 cpy_len = trans_ssid[1] + 2;
1857 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1858 pos += (mbssid - (trans_ssid + cpy_len));
1859 /* copy the IEs after MBSSID */
1860 cpy_len = mbssid[1] + 2;
1861 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1862
1863 /* update ie */
1864 new_ies->len = new_ie_len;
1865 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1866 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1867 memcpy(new_ies->data, new_ie, new_ie_len);
1868 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1869 old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1870 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1871 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1872 if (old)
1873 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1874 } else {
1875 old = rcu_access_pointer(nontrans_bss->beacon_ies);
1876 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1877 cfg80211_update_hidden_bsses(bss_from_pub(nontrans_bss),
1878 new_ies, old);
1879 rcu_assign_pointer(nontrans_bss->ies, new_ies);
1880 if (old)
1881 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1882 }
1883
1884out_free:
1885 kfree(new_ie);
1886}
1887
1888/* cfg80211_inform_bss_width_frame helper */
1889static struct cfg80211_bss *
1890cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1891 struct cfg80211_inform_bss *data,
1892 struct ieee80211_mgmt *mgmt, size_t len,
1893 gfp_t gfp)
1894{
1895 struct cfg80211_internal_bss tmp = {}, *res;
1896 struct cfg80211_bss_ies *ies;
1897 struct ieee80211_channel *channel;
1898 bool signal_valid;
1899 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1900 u.probe_resp.variable);
1901 int bss_type;
1902
1903 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1904 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1905
1906 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
1907
1908 if (WARN_ON(!mgmt))
1909 return NULL;
1910
1911 if (WARN_ON(!wiphy))
1912 return NULL;
1913
1914 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1915 (data->signal < 0 || data->signal > 100)))
1916 return NULL;
1917
1918 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
1919 return NULL;
1920
1921 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
1922 ielen, data->chan, data->scan_width);
1923 if (!channel)
1924 return NULL;
1925
1926 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1927 if (!ies)
1928 return NULL;
1929 ies->len = ielen;
1930 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1931 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1932 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
1933
1934 if (ieee80211_is_probe_resp(mgmt->frame_control))
1935 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1936 else
1937 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1938 rcu_assign_pointer(tmp.pub.ies, ies);
1939
1940 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1941 tmp.pub.channel = channel;
1942 tmp.pub.scan_width = data->scan_width;
1943 tmp.pub.signal = data->signal;
1944 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1945 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
1946 tmp.ts_boottime = data->boottime_ns;
1947 tmp.parent_tsf = data->parent_tsf;
1948 tmp.pub.chains = data->chains;
1949 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1950 ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1951
1952 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1953 wiphy->max_adj_channel_rssi_comp;
1954 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
1955 jiffies);
1956 if (!res)
1957 return NULL;
1958
1959 if (channel->band == NL80211_BAND_60GHZ) {
1960 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1961 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1962 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1963 regulatory_hint_found_beacon(wiphy, channel, gfp);
1964 } else {
1965 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1966 regulatory_hint_found_beacon(wiphy, channel, gfp);
1967 }
1968
1969 trace_cfg80211_return_bss(&res->pub);
1970 /* cfg80211_bss_update gives us a referenced result */
1971 return &res->pub;
1972}
1973
1974struct cfg80211_bss *
1975cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1976 struct cfg80211_inform_bss *data,
1977 struct ieee80211_mgmt *mgmt, size_t len,
1978 gfp_t gfp)
1979{
1980 struct cfg80211_bss *res, *tmp_bss;
1981 const u8 *ie = mgmt->u.probe_resp.variable;
1982 const struct cfg80211_bss_ies *ies1, *ies2;
1983 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1984 u.probe_resp.variable);
1985 struct cfg80211_non_tx_bss non_tx_data;
1986
1987 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1988 len, gfp);
1989 if (!res || !wiphy->support_mbssid ||
1990 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1991 return res;
1992 if (wiphy->support_only_he_mbssid &&
1993 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1994 return res;
1995
1996 non_tx_data.tx_bss = res;
1997 /* process each non-transmitting bss */
1998 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1999 &non_tx_data, gfp);
2000
2001 spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2002
2003 /* check if the res has other nontransmitting bss which is not
2004 * in MBSSID IE
2005 */
2006 ies1 = rcu_access_pointer(res->ies);
2007
2008 /* go through nontrans_list, if the timestamp of the BSS is
2009 * earlier than the timestamp of the transmitting BSS then
2010 * update it
2011 */
2012 list_for_each_entry(tmp_bss, &res->nontrans_list,
2013 nontrans_list) {
2014 ies2 = rcu_access_pointer(tmp_bss->ies);
2015 if (ies2->tsf < ies1->tsf)
2016 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
2017 mgmt, len);
2018 }
2019 spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
2020
2021 return res;
2022}
2023EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2024
2025void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2026{
2027 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2028 struct cfg80211_internal_bss *bss;
2029
2030 if (!pub)
2031 return;
2032
2033 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2034
2035 spin_lock_bh(&rdev->bss_lock);
2036 bss_ref_get(rdev, bss);
2037 spin_unlock_bh(&rdev->bss_lock);
2038}
2039EXPORT_SYMBOL(cfg80211_ref_bss);
2040
2041void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2042{
2043 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2044 struct cfg80211_internal_bss *bss;
2045
2046 if (!pub)
2047 return;
2048
2049 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2050
2051 spin_lock_bh(&rdev->bss_lock);
2052 bss_ref_put(rdev, bss);
2053 spin_unlock_bh(&rdev->bss_lock);
2054}
2055EXPORT_SYMBOL(cfg80211_put_bss);
2056
2057void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2058{
2059 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2060 struct cfg80211_internal_bss *bss, *tmp1;
2061 struct cfg80211_bss *nontrans_bss, *tmp;
2062
2063 if (WARN_ON(!pub))
2064 return;
2065
2066 bss = container_of(pub, struct cfg80211_internal_bss, pub);
2067
2068 spin_lock_bh(&rdev->bss_lock);
2069 if (list_empty(&bss->list))
2070 goto out;
2071
2072 list_for_each_entry_safe(nontrans_bss, tmp,
2073 &pub->nontrans_list,
2074 nontrans_list) {
2075 tmp1 = container_of(nontrans_bss,
2076 struct cfg80211_internal_bss, pub);
2077 if (__cfg80211_unlink_bss(rdev, tmp1))
2078 rdev->bss_generation++;
2079 }
2080
2081 if (__cfg80211_unlink_bss(rdev, bss))
2082 rdev->bss_generation++;
2083out:
2084 spin_unlock_bh(&rdev->bss_lock);
2085}
2086EXPORT_SYMBOL(cfg80211_unlink_bss);
2087
2088void cfg80211_bss_iter(struct wiphy *wiphy,
2089 struct cfg80211_chan_def *chandef,
2090 void (*iter)(struct wiphy *wiphy,
2091 struct cfg80211_bss *bss,
2092 void *data),
2093 void *iter_data)
2094{
2095 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2096 struct cfg80211_internal_bss *bss;
2097
2098 spin_lock_bh(&rdev->bss_lock);
2099
2100 list_for_each_entry(bss, &rdev->bss_list, list) {
2101 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel))
2102 iter(wiphy, &bss->pub, iter_data);
2103 }
2104
2105 spin_unlock_bh(&rdev->bss_lock);
2106}
2107EXPORT_SYMBOL(cfg80211_bss_iter);
2108
2109void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2110 struct ieee80211_channel *chan)
2111{
2112 struct wiphy *wiphy = wdev->wiphy;
2113 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2114 struct cfg80211_internal_bss *cbss = wdev->current_bss;
2115 struct cfg80211_internal_bss *new = NULL;
2116 struct cfg80211_internal_bss *bss;
2117 struct cfg80211_bss *nontrans_bss;
2118 struct cfg80211_bss *tmp;
2119
2120 spin_lock_bh(&rdev->bss_lock);
2121
2122 /*
2123 * Some APs use CSA also for bandwidth changes, i.e., without actually
2124 * changing the control channel, so no need to update in such a case.
2125 */
2126 if (cbss->pub.channel == chan)
2127 goto done;
2128
2129 /* use transmitting bss */
2130 if (cbss->pub.transmitted_bss)
2131 cbss = container_of(cbss->pub.transmitted_bss,
2132 struct cfg80211_internal_bss,
2133 pub);
2134
2135 cbss->pub.channel = chan;
2136
2137 list_for_each_entry(bss, &rdev->bss_list, list) {
2138 if (!cfg80211_bss_type_match(bss->pub.capability,
2139 bss->pub.channel->band,
2140 wdev->conn_bss_type))
2141 continue;
2142
2143 if (bss == cbss)
2144 continue;
2145
2146 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2147 new = bss;
2148 break;
2149 }
2150 }
2151
2152 if (new) {
2153 /* to save time, update IEs for transmitting bss only */
2154 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2155 new->pub.proberesp_ies = NULL;
2156 new->pub.beacon_ies = NULL;
2157 }
2158
2159 list_for_each_entry_safe(nontrans_bss, tmp,
2160 &new->pub.nontrans_list,
2161 nontrans_list) {
2162 bss = container_of(nontrans_bss,
2163 struct cfg80211_internal_bss, pub);
2164 if (__cfg80211_unlink_bss(rdev, bss))
2165 rdev->bss_generation++;
2166 }
2167
2168 WARN_ON(atomic_read(&new->hold));
2169 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2170 rdev->bss_generation++;
2171 }
2172 cfg80211_rehash_bss(rdev, cbss);
2173
2174 list_for_each_entry_safe(nontrans_bss, tmp,
2175 &cbss->pub.nontrans_list,
2176 nontrans_list) {
2177 bss = container_of(nontrans_bss,
2178 struct cfg80211_internal_bss, pub);
2179 bss->pub.channel = chan;
2180 cfg80211_rehash_bss(rdev, bss);
2181 }
2182
2183done:
2184 spin_unlock_bh(&rdev->bss_lock);
2185}
2186
2187#ifdef CONFIG_CFG80211_WEXT
2188static struct cfg80211_registered_device *
2189cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2190{
2191 struct cfg80211_registered_device *rdev;
2192 struct net_device *dev;
2193
2194 ASSERT_RTNL();
2195
2196 dev = dev_get_by_index(net, ifindex);
2197 if (!dev)
2198 return ERR_PTR(-ENODEV);
2199 if (dev->ieee80211_ptr)
2200 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2201 else
2202 rdev = ERR_PTR(-ENODEV);
2203 dev_put(dev);
2204 return rdev;
2205}
2206
2207int cfg80211_wext_siwscan(struct net_device *dev,
2208 struct iw_request_info *info,
2209 union iwreq_data *wrqu, char *extra)
2210{
2211 struct cfg80211_registered_device *rdev;
2212 struct wiphy *wiphy;
2213 struct iw_scan_req *wreq = NULL;
2214 struct cfg80211_scan_request *creq = NULL;
2215 int i, err, n_channels = 0;
2216 enum nl80211_band band;
2217
2218 if (!netif_running(dev))
2219 return -ENETDOWN;
2220
2221 if (wrqu->data.length == sizeof(struct iw_scan_req))
2222 wreq = (struct iw_scan_req *)extra;
2223
2224 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2225
2226 if (IS_ERR(rdev))
2227 return PTR_ERR(rdev);
2228
2229 if (rdev->scan_req || rdev->scan_msg) {
2230 err = -EBUSY;
2231 goto out;
2232 }
2233
2234 wiphy = &rdev->wiphy;
2235
2236 /* Determine number of channels, needed to allocate creq */
2237 if (wreq && wreq->num_channels) {
2238 /* Passed from userspace so should be checked */
2239 if (unlikely(wreq->num_channels > IW_MAX_FREQUENCIES))
2240 return -EINVAL;
2241 n_channels = wreq->num_channels;
2242 } else {
2243 n_channels = ieee80211_get_num_supported_channels(wiphy);
2244 }
2245
2246 creq = kzalloc(struct_size(creq, channels, n_channels) +
2247 sizeof(struct cfg80211_ssid),
2248 GFP_ATOMIC);
2249 if (!creq) {
2250 err = -ENOMEM;
2251 goto out;
2252 }
2253
2254 creq->wiphy = wiphy;
2255 creq->wdev = dev->ieee80211_ptr;
2256 /* SSIDs come after channels */
2257 creq->ssids = (void *)creq + struct_size(creq, channels, n_channels);
2258 creq->n_channels = n_channels;
2259 creq->n_ssids = 1;
2260 creq->scan_start = jiffies;
2261
2262 /* translate "Scan on frequencies" request */
2263 i = 0;
2264 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2265 int j;
2266
2267 if (!wiphy->bands[band])
2268 continue;
2269
2270 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2271 /* ignore disabled channels */
2272 if (wiphy->bands[band]->channels[j].flags &
2273 IEEE80211_CHAN_DISABLED)
2274 continue;
2275
2276 /* If we have a wireless request structure and the
2277 * wireless request specifies frequencies, then search
2278 * for the matching hardware channel.
2279 */
2280 if (wreq && wreq->num_channels) {
2281 int k;
2282 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2283 for (k = 0; k < wreq->num_channels; k++) {
2284 struct iw_freq *freq =
2285 &wreq->channel_list[k];
2286 int wext_freq =
2287 cfg80211_wext_freq(freq);
2288
2289 if (wext_freq == wiphy_freq)
2290 goto wext_freq_found;
2291 }
2292 goto wext_freq_not_found;
2293 }
2294
2295 wext_freq_found:
2296 creq->channels[i] = &wiphy->bands[band]->channels[j];
2297 i++;
2298 wext_freq_not_found: ;
2299 }
2300 }
2301 /* No channels found? */
2302 if (!i) {
2303 err = -EINVAL;
2304 goto out;
2305 }
2306
2307 /* Set real number of channels specified in creq->channels[] */
2308 creq->n_channels = i;
2309
2310 /* translate "Scan for SSID" request */
2311 if (wreq) {
2312 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2313 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2314 err = -EINVAL;
2315 goto out;
2316 }
2317 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2318 creq->ssids[0].ssid_len = wreq->essid_len;
2319 }
2320 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2321 creq->n_ssids = 0;
2322 }
2323
2324 for (i = 0; i < NUM_NL80211_BANDS; i++)
2325 if (wiphy->bands[i])
2326 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2327
2328 eth_broadcast_addr(creq->bssid);
2329
2330 rdev->scan_req = creq;
2331 err = rdev_scan(rdev, creq);
2332 if (err) {
2333 rdev->scan_req = NULL;
2334 /* creq will be freed below */
2335 } else {
2336 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2337 /* creq now owned by driver */
2338 creq = NULL;
2339 dev_hold(dev);
2340 }
2341 out:
2342 kfree(creq);
2343 return err;
2344}
2345EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2346
2347static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2348 const struct cfg80211_bss_ies *ies,
2349 char *current_ev, char *end_buf)
2350{
2351 const u8 *pos, *end, *next;
2352 struct iw_event iwe;
2353
2354 if (!ies)
2355 return current_ev;
2356
2357 /*
2358 * If needed, fragment the IEs buffer (at IE boundaries) into short
2359 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2360 */
2361 pos = ies->data;
2362 end = pos + ies->len;
2363
2364 while (end - pos > IW_GENERIC_IE_MAX) {
2365 next = pos + 2 + pos[1];
2366 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2367 next = next + 2 + next[1];
2368
2369 memset(&iwe, 0, sizeof(iwe));
2370 iwe.cmd = IWEVGENIE;
2371 iwe.u.data.length = next - pos;
2372 current_ev = iwe_stream_add_point_check(info, current_ev,
2373 end_buf, &iwe,
2374 (void *)pos);
2375 if (IS_ERR(current_ev))
2376 return current_ev;
2377 pos = next;
2378 }
2379
2380 if (end > pos) {
2381 memset(&iwe, 0, sizeof(iwe));
2382 iwe.cmd = IWEVGENIE;
2383 iwe.u.data.length = end - pos;
2384 current_ev = iwe_stream_add_point_check(info, current_ev,
2385 end_buf, &iwe,
2386 (void *)pos);
2387 if (IS_ERR(current_ev))
2388 return current_ev;
2389 }
2390
2391 return current_ev;
2392}
2393
2394static char *
2395ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2396 struct cfg80211_internal_bss *bss, char *current_ev,
2397 char *end_buf)
2398{
2399 const struct cfg80211_bss_ies *ies;
2400 struct iw_event iwe;
2401 const u8 *ie;
2402 u8 buf[50];
2403 u8 *cfg, *p, *tmp;
2404 int rem, i, sig;
2405 bool ismesh = false;
2406
2407 memset(&iwe, 0, sizeof(iwe));
2408 iwe.cmd = SIOCGIWAP;
2409 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2410 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2411 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2412 IW_EV_ADDR_LEN);
2413 if (IS_ERR(current_ev))
2414 return current_ev;
2415
2416 memset(&iwe, 0, sizeof(iwe));
2417 iwe.cmd = SIOCGIWFREQ;
2418 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2419 iwe.u.freq.e = 0;
2420 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2421 IW_EV_FREQ_LEN);
2422 if (IS_ERR(current_ev))
2423 return current_ev;
2424
2425 memset(&iwe, 0, sizeof(iwe));
2426 iwe.cmd = SIOCGIWFREQ;
2427 iwe.u.freq.m = bss->pub.channel->center_freq;
2428 iwe.u.freq.e = 6;
2429 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2430 IW_EV_FREQ_LEN);
2431 if (IS_ERR(current_ev))
2432 return current_ev;
2433
2434 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2435 memset(&iwe, 0, sizeof(iwe));
2436 iwe.cmd = IWEVQUAL;
2437 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2438 IW_QUAL_NOISE_INVALID |
2439 IW_QUAL_QUAL_UPDATED;
2440 switch (wiphy->signal_type) {
2441 case CFG80211_SIGNAL_TYPE_MBM:
2442 sig = bss->pub.signal / 100;
2443 iwe.u.qual.level = sig;
2444 iwe.u.qual.updated |= IW_QUAL_DBM;
2445 if (sig < -110) /* rather bad */
2446 sig = -110;
2447 else if (sig > -40) /* perfect */
2448 sig = -40;
2449 /* will give a range of 0 .. 70 */
2450 iwe.u.qual.qual = sig + 110;
2451 break;
2452 case CFG80211_SIGNAL_TYPE_UNSPEC:
2453 iwe.u.qual.level = bss->pub.signal;
2454 /* will give range 0 .. 100 */
2455 iwe.u.qual.qual = bss->pub.signal;
2456 break;
2457 default:
2458 /* not reached */
2459 break;
2460 }
2461 current_ev = iwe_stream_add_event_check(info, current_ev,
2462 end_buf, &iwe,
2463 IW_EV_QUAL_LEN);
2464 if (IS_ERR(current_ev))
2465 return current_ev;
2466 }
2467
2468 memset(&iwe, 0, sizeof(iwe));
2469 iwe.cmd = SIOCGIWENCODE;
2470 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2471 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2472 else
2473 iwe.u.data.flags = IW_ENCODE_DISABLED;
2474 iwe.u.data.length = 0;
2475 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2476 &iwe, "");
2477 if (IS_ERR(current_ev))
2478 return current_ev;
2479
2480 rcu_read_lock();
2481 ies = rcu_dereference(bss->pub.ies);
2482 rem = ies->len;
2483 ie = ies->data;
2484
2485 while (rem >= 2) {
2486 /* invalid data */
2487 if (ie[1] > rem - 2)
2488 break;
2489
2490 switch (ie[0]) {
2491 case WLAN_EID_SSID:
2492 memset(&iwe, 0, sizeof(iwe));
2493 iwe.cmd = SIOCGIWESSID;
2494 iwe.u.data.length = ie[1];
2495 iwe.u.data.flags = 1;
2496 current_ev = iwe_stream_add_point_check(info,
2497 current_ev,
2498 end_buf, &iwe,
2499 (u8 *)ie + 2);
2500 if (IS_ERR(current_ev))
2501 goto unlock;
2502 break;
2503 case WLAN_EID_MESH_ID:
2504 memset(&iwe, 0, sizeof(iwe));
2505 iwe.cmd = SIOCGIWESSID;
2506 iwe.u.data.length = ie[1];
2507 iwe.u.data.flags = 1;
2508 current_ev = iwe_stream_add_point_check(info,
2509 current_ev,
2510 end_buf, &iwe,
2511 (u8 *)ie + 2);
2512 if (IS_ERR(current_ev))
2513 goto unlock;
2514 break;
2515 case WLAN_EID_MESH_CONFIG:
2516 ismesh = true;
2517 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2518 break;
2519 cfg = (u8 *)ie + 2;
2520 memset(&iwe, 0, sizeof(iwe));
2521 iwe.cmd = IWEVCUSTOM;
2522 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2523 "0x%02X", cfg[0]);
2524 iwe.u.data.length = strlen(buf);
2525 current_ev = iwe_stream_add_point_check(info,
2526 current_ev,
2527 end_buf,
2528 &iwe, buf);
2529 if (IS_ERR(current_ev))
2530 goto unlock;
2531 sprintf(buf, "Path Selection Metric ID: 0x%02X",
2532 cfg[1]);
2533 iwe.u.data.length = strlen(buf);
2534 current_ev = iwe_stream_add_point_check(info,
2535 current_ev,
2536 end_buf,
2537 &iwe, buf);
2538 if (IS_ERR(current_ev))
2539 goto unlock;
2540 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2541 cfg[2]);
2542 iwe.u.data.length = strlen(buf);
2543 current_ev = iwe_stream_add_point_check(info,
2544 current_ev,
2545 end_buf,
2546 &iwe, buf);
2547 if (IS_ERR(current_ev))
2548 goto unlock;
2549 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2550 iwe.u.data.length = strlen(buf);
2551 current_ev = iwe_stream_add_point_check(info,
2552 current_ev,
2553 end_buf,
2554 &iwe, buf);
2555 if (IS_ERR(current_ev))
2556 goto unlock;
2557 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2558 iwe.u.data.length = strlen(buf);
2559 current_ev = iwe_stream_add_point_check(info,
2560 current_ev,
2561 end_buf,
2562 &iwe, buf);
2563 if (IS_ERR(current_ev))
2564 goto unlock;
2565 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2566 iwe.u.data.length = strlen(buf);
2567 current_ev = iwe_stream_add_point_check(info,
2568 current_ev,
2569 end_buf,
2570 &iwe, buf);
2571 if (IS_ERR(current_ev))
2572 goto unlock;
2573 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2574 iwe.u.data.length = strlen(buf);
2575 current_ev = iwe_stream_add_point_check(info,
2576 current_ev,
2577 end_buf,
2578 &iwe, buf);
2579 if (IS_ERR(current_ev))
2580 goto unlock;
2581 break;
2582 case WLAN_EID_SUPP_RATES:
2583 case WLAN_EID_EXT_SUPP_RATES:
2584 /* display all supported rates in readable format */
2585 p = current_ev + iwe_stream_lcp_len(info);
2586
2587 memset(&iwe, 0, sizeof(iwe));
2588 iwe.cmd = SIOCGIWRATE;
2589 /* Those two flags are ignored... */
2590 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2591
2592 for (i = 0; i < ie[1]; i++) {
2593 iwe.u.bitrate.value =
2594 ((ie[i + 2] & 0x7f) * 500000);
2595 tmp = p;
2596 p = iwe_stream_add_value(info, current_ev, p,
2597 end_buf, &iwe,
2598 IW_EV_PARAM_LEN);
2599 if (p == tmp) {
2600 current_ev = ERR_PTR(-E2BIG);
2601 goto unlock;
2602 }
2603 }
2604 current_ev = p;
2605 break;
2606 }
2607 rem -= ie[1] + 2;
2608 ie += ie[1] + 2;
2609 }
2610
2611 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2612 ismesh) {
2613 memset(&iwe, 0, sizeof(iwe));
2614 iwe.cmd = SIOCGIWMODE;
2615 if (ismesh)
2616 iwe.u.mode = IW_MODE_MESH;
2617 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2618 iwe.u.mode = IW_MODE_MASTER;
2619 else
2620 iwe.u.mode = IW_MODE_ADHOC;
2621 current_ev = iwe_stream_add_event_check(info, current_ev,
2622 end_buf, &iwe,
2623 IW_EV_UINT_LEN);
2624 if (IS_ERR(current_ev))
2625 goto unlock;
2626 }
2627
2628 memset(&iwe, 0, sizeof(iwe));
2629 iwe.cmd = IWEVCUSTOM;
2630 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2631 iwe.u.data.length = strlen(buf);
2632 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2633 &iwe, buf);
2634 if (IS_ERR(current_ev))
2635 goto unlock;
2636 memset(&iwe, 0, sizeof(iwe));
2637 iwe.cmd = IWEVCUSTOM;
2638 sprintf(buf, " Last beacon: %ums ago",
2639 elapsed_jiffies_msecs(bss->ts));
2640 iwe.u.data.length = strlen(buf);
2641 current_ev = iwe_stream_add_point_check(info, current_ev,
2642 end_buf, &iwe, buf);
2643 if (IS_ERR(current_ev))
2644 goto unlock;
2645
2646 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2647
2648 unlock:
2649 rcu_read_unlock();
2650 return current_ev;
2651}
2652
2653
2654static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2655 struct iw_request_info *info,
2656 char *buf, size_t len)
2657{
2658 char *current_ev = buf;
2659 char *end_buf = buf + len;
2660 struct cfg80211_internal_bss *bss;
2661 int err = 0;
2662
2663 spin_lock_bh(&rdev->bss_lock);
2664 cfg80211_bss_expire(rdev);
2665
2666 list_for_each_entry(bss, &rdev->bss_list, list) {
2667 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
2668 err = -E2BIG;
2669 break;
2670 }
2671 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
2672 current_ev, end_buf);
2673 if (IS_ERR(current_ev)) {
2674 err = PTR_ERR(current_ev);
2675 break;
2676 }
2677 }
2678 spin_unlock_bh(&rdev->bss_lock);
2679
2680 if (err)
2681 return err;
2682 return current_ev - buf;
2683}
2684
2685
2686int cfg80211_wext_giwscan(struct net_device *dev,
2687 struct iw_request_info *info,
2688 struct iw_point *data, char *extra)
2689{
2690 struct cfg80211_registered_device *rdev;
2691 int res;
2692
2693 if (!netif_running(dev))
2694 return -ENETDOWN;
2695
2696 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2697
2698 if (IS_ERR(rdev))
2699 return PTR_ERR(rdev);
2700
2701 if (rdev->scan_req || rdev->scan_msg)
2702 return -EAGAIN;
2703
2704 res = ieee80211_scan_results(rdev, info, extra, data->length);
2705 data->length = 0;
2706 if (res >= 0) {
2707 data->length = res;
2708 res = 0;
2709 }
2710
2711 return res;
2712}
2713EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
2714#endif