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