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