wifi: cfg80211: use a struct for inform_single_bss data
[platform/kernel/linux-starfive.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-2023 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 <linux/crc32.h>
18 #include <linux/bitfield.h>
19 #include <net/arp.h>
20 #include <net/cfg80211.h>
21 #include <net/cfg80211-wext.h>
22 #include <net/iw_handler.h>
23 #include "core.h"
24 #include "nl80211.h"
25 #include "wext-compat.h"
26 #include "rdev-ops.h"
27
28 /**
29  * DOC: BSS tree/list structure
30  *
31  * At the top level, the BSS list is kept in both a list in each
32  * registered device (@bss_list) as well as an RB-tree for faster
33  * lookup. In the RB-tree, entries can be looked up using their
34  * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
35  * for other BSSes.
36  *
37  * Due to the possibility of hidden SSIDs, there's a second level
38  * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
39  * The hidden_list connects all BSSes belonging to a single AP
40  * that has a hidden SSID, and connects beacon and probe response
41  * entries. For a probe response entry for a hidden SSID, the
42  * hidden_beacon_bss pointer points to the BSS struct holding the
43  * beacon's information.
44  *
45  * Reference counting is done for all these references except for
46  * the hidden_list, so that a beacon BSS struct that is otherwise
47  * not referenced has one reference for being on the bss_list and
48  * one for each probe response entry that points to it using the
49  * hidden_beacon_bss pointer. When a BSS struct that has such a
50  * pointer is get/put, the refcount update is also propagated to
51  * the referenced struct, this ensure that it cannot get removed
52  * while somebody is using the probe response version.
53  *
54  * Note that the hidden_beacon_bss pointer never changes, due to
55  * the reference counting. Therefore, no locking is needed for
56  * it.
57  *
58  * Also note that the hidden_beacon_bss pointer is only relevant
59  * if the driver uses something other than the IEs, e.g. private
60  * data stored in the BSS struct, since the beacon IEs are
61  * also linked into the probe response struct.
62  */
63
64 /*
65  * Limit the number of BSS entries stored in mac80211. Each one is
66  * a bit over 4k at most, so this limits to roughly 4-5M of memory.
67  * If somebody wants to really attack this though, they'd likely
68  * use small beacons, and only one type of frame, limiting each of
69  * the entries to a much smaller size (in order to generate more
70  * entries in total, so overhead is bigger.)
71  */
72 static int bss_entries_limit = 1000;
73 module_param(bss_entries_limit, int, 0644);
74 MODULE_PARM_DESC(bss_entries_limit,
75                  "limit to number of scan BSS entries (per wiphy, default 1000)");
76
77 #define IEEE80211_SCAN_RESULT_EXPIRE    (30 * HZ)
78
79 /**
80  * struct cfg80211_colocated_ap - colocated AP information
81  *
82  * @list: linked list to all colocated aPS
83  * @bssid: BSSID of the reported AP
84  * @ssid: SSID of the reported AP
85  * @ssid_len: length of the ssid
86  * @center_freq: frequency the reported AP is on
87  * @unsolicited_probe: the reported AP is part of an ESS, where all the APs
88  *      that operate in the same channel as the reported AP and that might be
89  *      detected by a STA receiving this frame, are transmitting unsolicited
90  *      Probe Response frames every 20 TUs
91  * @oct_recommended: OCT is recommended to exchange MMPDUs with the reported AP
92  * @same_ssid: the reported AP has the same SSID as the reporting AP
93  * @multi_bss: the reported AP is part of a multiple BSSID set
94  * @transmitted_bssid: the reported AP is the transmitting BSSID
95  * @colocated_ess: all the APs that share the same ESS as the reported AP are
96  *      colocated and can be discovered via legacy bands.
97  * @short_ssid_valid: short_ssid is valid and can be used
98  * @short_ssid: the short SSID for this SSID
99  */
100 struct cfg80211_colocated_ap {
101         struct list_head list;
102         u8 bssid[ETH_ALEN];
103         u8 ssid[IEEE80211_MAX_SSID_LEN];
104         size_t ssid_len;
105         u32 short_ssid;
106         u32 center_freq;
107         u8 unsolicited_probe:1,
108            oct_recommended:1,
109            same_ssid:1,
110            multi_bss:1,
111            transmitted_bssid:1,
112            colocated_ess:1,
113            short_ssid_valid:1;
114 };
115
116 static void bss_free(struct cfg80211_internal_bss *bss)
117 {
118         struct cfg80211_bss_ies *ies;
119
120         if (WARN_ON(atomic_read(&bss->hold)))
121                 return;
122
123         ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
124         if (ies && !bss->pub.hidden_beacon_bss)
125                 kfree_rcu(ies, rcu_head);
126         ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
127         if (ies)
128                 kfree_rcu(ies, rcu_head);
129
130         /*
131          * This happens when the module is removed, it doesn't
132          * really matter any more save for completeness
133          */
134         if (!list_empty(&bss->hidden_list))
135                 list_del(&bss->hidden_list);
136
137         kfree(bss);
138 }
139
140 static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
141                                struct cfg80211_internal_bss *bss)
142 {
143         lockdep_assert_held(&rdev->bss_lock);
144
145         bss->refcount++;
146
147         if (bss->pub.hidden_beacon_bss)
148                 bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
149
150         if (bss->pub.transmitted_bss)
151                 bss_from_pub(bss->pub.transmitted_bss)->refcount++;
152 }
153
154 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
155                                struct cfg80211_internal_bss *bss)
156 {
157         lockdep_assert_held(&rdev->bss_lock);
158
159         if (bss->pub.hidden_beacon_bss) {
160                 struct cfg80211_internal_bss *hbss;
161
162                 hbss = bss_from_pub(bss->pub.hidden_beacon_bss);
163                 hbss->refcount--;
164                 if (hbss->refcount == 0)
165                         bss_free(hbss);
166         }
167
168         if (bss->pub.transmitted_bss) {
169                 struct cfg80211_internal_bss *tbss;
170
171                 tbss = bss_from_pub(bss->pub.transmitted_bss);
172                 tbss->refcount--;
173                 if (tbss->refcount == 0)
174                         bss_free(tbss);
175         }
176
177         bss->refcount--;
178         if (bss->refcount == 0)
179                 bss_free(bss);
180 }
181
182 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
183                                   struct cfg80211_internal_bss *bss)
184 {
185         lockdep_assert_held(&rdev->bss_lock);
186
187         if (!list_empty(&bss->hidden_list)) {
188                 /*
189                  * don't remove the beacon entry if it has
190                  * probe responses associated with it
191                  */
192                 if (!bss->pub.hidden_beacon_bss)
193                         return false;
194                 /*
195                  * if it's a probe response entry break its
196                  * link to the other entries in the group
197                  */
198                 list_del_init(&bss->hidden_list);
199         }
200
201         list_del_init(&bss->list);
202         list_del_init(&bss->pub.nontrans_list);
203         rb_erase(&bss->rbn, &rdev->bss_tree);
204         rdev->bss_entries--;
205         WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
206                   "rdev bss entries[%d]/list[empty:%d] corruption\n",
207                   rdev->bss_entries, list_empty(&rdev->bss_list));
208         bss_ref_put(rdev, bss);
209         return true;
210 }
211
212 bool cfg80211_is_element_inherited(const struct element *elem,
213                                    const struct element *non_inherit_elem)
214 {
215         u8 id_len, ext_id_len, i, loop_len, id;
216         const u8 *list;
217
218         if (elem->id == WLAN_EID_MULTIPLE_BSSID)
219                 return false;
220
221         if (elem->id == WLAN_EID_EXTENSION && elem->datalen > 1 &&
222             elem->data[0] == WLAN_EID_EXT_EHT_MULTI_LINK)
223                 return false;
224
225         if (!non_inherit_elem || non_inherit_elem->datalen < 2)
226                 return true;
227
228         /*
229          * non inheritance element format is:
230          * ext ID (56) | IDs list len | list | extension IDs list len | list
231          * Both lists are optional. Both lengths are mandatory.
232          * This means valid length is:
233          * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
234          */
235         id_len = non_inherit_elem->data[1];
236         if (non_inherit_elem->datalen < 3 + id_len)
237                 return true;
238
239         ext_id_len = non_inherit_elem->data[2 + id_len];
240         if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
241                 return true;
242
243         if (elem->id == WLAN_EID_EXTENSION) {
244                 if (!ext_id_len)
245                         return true;
246                 loop_len = ext_id_len;
247                 list = &non_inherit_elem->data[3 + id_len];
248                 id = elem->data[0];
249         } else {
250                 if (!id_len)
251                         return true;
252                 loop_len = id_len;
253                 list = &non_inherit_elem->data[2];
254                 id = elem->id;
255         }
256
257         for (i = 0; i < loop_len; i++) {
258                 if (list[i] == id)
259                         return false;
260         }
261
262         return true;
263 }
264 EXPORT_SYMBOL(cfg80211_is_element_inherited);
265
266 static size_t cfg80211_copy_elem_with_frags(const struct element *elem,
267                                             const u8 *ie, size_t ie_len,
268                                             u8 **pos, u8 *buf, size_t buf_len)
269 {
270         if (WARN_ON((u8 *)elem < ie || elem->data > ie + ie_len ||
271                     elem->data + elem->datalen > ie + ie_len))
272                 return 0;
273
274         if (elem->datalen + 2 > buf + buf_len - *pos)
275                 return 0;
276
277         memcpy(*pos, elem, elem->datalen + 2);
278         *pos += elem->datalen + 2;
279
280         /* Finish if it is not fragmented  */
281         if (elem->datalen != 255)
282                 return *pos - buf;
283
284         ie_len = ie + ie_len - elem->data - elem->datalen;
285         ie = (const u8 *)elem->data + elem->datalen;
286
287         for_each_element(elem, ie, ie_len) {
288                 if (elem->id != WLAN_EID_FRAGMENT)
289                         break;
290
291                 if (elem->datalen + 2 > buf + buf_len - *pos)
292                         return 0;
293
294                 memcpy(*pos, elem, elem->datalen + 2);
295                 *pos += elem->datalen + 2;
296
297                 if (elem->datalen != 255)
298                         break;
299         }
300
301         return *pos - buf;
302 }
303
304 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
305                                   const u8 *subie, size_t subie_len,
306                                   u8 *new_ie, size_t new_ie_len)
307 {
308         const struct element *non_inherit_elem, *parent, *sub;
309         u8 *pos = new_ie;
310         u8 id, ext_id;
311         unsigned int match_len;
312
313         non_inherit_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
314                                                   subie, subie_len);
315
316         /* We copy the elements one by one from the parent to the generated
317          * elements.
318          * If they are not inherited (included in subie or in the non
319          * inheritance element), then we copy all occurrences the first time
320          * we see this element type.
321          */
322         for_each_element(parent, ie, ielen) {
323                 if (parent->id == WLAN_EID_FRAGMENT)
324                         continue;
325
326                 if (parent->id == WLAN_EID_EXTENSION) {
327                         if (parent->datalen < 1)
328                                 continue;
329
330                         id = WLAN_EID_EXTENSION;
331                         ext_id = parent->data[0];
332                         match_len = 1;
333                 } else {
334                         id = parent->id;
335                         match_len = 0;
336                 }
337
338                 /* Find first occurrence in subie */
339                 sub = cfg80211_find_elem_match(id, subie, subie_len,
340                                                &ext_id, match_len, 0);
341
342                 /* Copy from parent if not in subie and inherited */
343                 if (!sub &&
344                     cfg80211_is_element_inherited(parent, non_inherit_elem)) {
345                         if (!cfg80211_copy_elem_with_frags(parent,
346                                                            ie, ielen,
347                                                            &pos, new_ie,
348                                                            new_ie_len))
349                                 return 0;
350
351                         continue;
352                 }
353
354                 /* Already copied if an earlier element had the same type */
355                 if (cfg80211_find_elem_match(id, ie, (u8 *)parent - ie,
356                                              &ext_id, match_len, 0))
357                         continue;
358
359                 /* Not inheriting, copy all similar elements from subie */
360                 while (sub) {
361                         if (!cfg80211_copy_elem_with_frags(sub,
362                                                            subie, subie_len,
363                                                            &pos, new_ie,
364                                                            new_ie_len))
365                                 return 0;
366
367                         sub = cfg80211_find_elem_match(id,
368                                                        sub->data + sub->datalen,
369                                                        subie_len + subie -
370                                                        (sub->data +
371                                                         sub->datalen),
372                                                        &ext_id, match_len, 0);
373                 }
374         }
375
376         /* The above misses elements that are included in subie but not in the
377          * parent, so do a pass over subie and append those.
378          * Skip the non-tx BSSID caps and non-inheritance element.
379          */
380         for_each_element(sub, subie, subie_len) {
381                 if (sub->id == WLAN_EID_NON_TX_BSSID_CAP)
382                         continue;
383
384                 if (sub->id == WLAN_EID_FRAGMENT)
385                         continue;
386
387                 if (sub->id == WLAN_EID_EXTENSION) {
388                         if (sub->datalen < 1)
389                                 continue;
390
391                         id = WLAN_EID_EXTENSION;
392                         ext_id = sub->data[0];
393                         match_len = 1;
394
395                         if (ext_id == WLAN_EID_EXT_NON_INHERITANCE)
396                                 continue;
397                 } else {
398                         id = sub->id;
399                         match_len = 0;
400                 }
401
402                 /* Processed if one was included in the parent */
403                 if (cfg80211_find_elem_match(id, ie, ielen,
404                                              &ext_id, match_len, 0))
405                         continue;
406
407                 if (!cfg80211_copy_elem_with_frags(sub, subie, subie_len,
408                                                    &pos, new_ie, new_ie_len))
409                         return 0;
410         }
411
412         return pos - new_ie;
413 }
414
415 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
416                    const u8 *ssid, size_t ssid_len)
417 {
418         const struct cfg80211_bss_ies *ies;
419         const struct element *ssid_elem;
420
421         if (bssid && !ether_addr_equal(a->bssid, bssid))
422                 return false;
423
424         if (!ssid)
425                 return true;
426
427         ies = rcu_access_pointer(a->ies);
428         if (!ies)
429                 return false;
430         ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
431         if (!ssid_elem)
432                 return false;
433         if (ssid_elem->datalen != ssid_len)
434                 return false;
435         return memcmp(ssid_elem->data, ssid, ssid_len) == 0;
436 }
437
438 static int
439 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
440                            struct cfg80211_bss *nontrans_bss)
441 {
442         const struct element *ssid_elem;
443         struct cfg80211_bss *bss = NULL;
444
445         rcu_read_lock();
446         ssid_elem = ieee80211_bss_get_elem(nontrans_bss, WLAN_EID_SSID);
447         if (!ssid_elem) {
448                 rcu_read_unlock();
449                 return -EINVAL;
450         }
451
452         /* check if nontrans_bss is in the list */
453         list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
454                 if (is_bss(bss, nontrans_bss->bssid, ssid_elem->data,
455                            ssid_elem->datalen)) {
456                         rcu_read_unlock();
457                         return 0;
458                 }
459         }
460
461         rcu_read_unlock();
462
463         /*
464          * This is a bit weird - it's not on the list, but already on another
465          * one! The only way that could happen is if there's some BSSID/SSID
466          * shared by multiple APs in their multi-BSSID profiles, potentially
467          * with hidden SSID mixed in ... ignore it.
468          */
469         if (!list_empty(&nontrans_bss->nontrans_list))
470                 return -EINVAL;
471
472         /* add to the list */
473         list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
474         return 0;
475 }
476
477 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
478                                   unsigned long expire_time)
479 {
480         struct cfg80211_internal_bss *bss, *tmp;
481         bool expired = false;
482
483         lockdep_assert_held(&rdev->bss_lock);
484
485         list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
486                 if (atomic_read(&bss->hold))
487                         continue;
488                 if (!time_after(expire_time, bss->ts))
489                         continue;
490
491                 if (__cfg80211_unlink_bss(rdev, bss))
492                         expired = true;
493         }
494
495         if (expired)
496                 rdev->bss_generation++;
497 }
498
499 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
500 {
501         struct cfg80211_internal_bss *bss, *oldest = NULL;
502         bool ret;
503
504         lockdep_assert_held(&rdev->bss_lock);
505
506         list_for_each_entry(bss, &rdev->bss_list, list) {
507                 if (atomic_read(&bss->hold))
508                         continue;
509
510                 if (!list_empty(&bss->hidden_list) &&
511                     !bss->pub.hidden_beacon_bss)
512                         continue;
513
514                 if (oldest && time_before(oldest->ts, bss->ts))
515                         continue;
516                 oldest = bss;
517         }
518
519         if (WARN_ON(!oldest))
520                 return false;
521
522         /*
523          * The callers make sure to increase rdev->bss_generation if anything
524          * gets removed (and a new entry added), so there's no need to also do
525          * it here.
526          */
527
528         ret = __cfg80211_unlink_bss(rdev, oldest);
529         WARN_ON(!ret);
530         return ret;
531 }
532
533 static u8 cfg80211_parse_bss_param(u8 data,
534                                    struct cfg80211_colocated_ap *coloc_ap)
535 {
536         coloc_ap->oct_recommended =
537                 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
538         coloc_ap->same_ssid =
539                 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
540         coloc_ap->multi_bss =
541                 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
542         coloc_ap->transmitted_bssid =
543                 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
544         coloc_ap->unsolicited_probe =
545                 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
546         coloc_ap->colocated_ess =
547                 u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
548
549         return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
550 }
551
552 static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
553                                     const struct element **elem, u32 *s_ssid)
554 {
555
556         *elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
557         if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
558                 return -EINVAL;
559
560         *s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
561         return 0;
562 }
563
564 static void cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
565 {
566         struct cfg80211_colocated_ap *ap, *tmp_ap;
567
568         list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
569                 list_del(&ap->list);
570                 kfree(ap);
571         }
572 }
573
574 static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
575                                   const u8 *pos, u8 length,
576                                   const struct element *ssid_elem,
577                                   int s_ssid_tmp)
578 {
579         /* skip the TBTT offset */
580         pos++;
581
582         /* ignore entries with invalid BSSID */
583         if (!is_valid_ether_addr(pos))
584                 return -EINVAL;
585
586         memcpy(entry->bssid, pos, ETH_ALEN);
587         pos += ETH_ALEN;
588
589         if (length >= IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM) {
590                 memcpy(&entry->short_ssid, pos,
591                        sizeof(entry->short_ssid));
592                 entry->short_ssid_valid = true;
593                 pos += 4;
594         }
595
596         /* skip non colocated APs */
597         if (!cfg80211_parse_bss_param(*pos, entry))
598                 return -EINVAL;
599         pos++;
600
601         if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM) {
602                 /*
603                  * no information about the short ssid. Consider the entry valid
604                  * for now. It would later be dropped in case there are explicit
605                  * SSIDs that need to be matched
606                  */
607                 if (!entry->same_ssid)
608                         return 0;
609         }
610
611         if (entry->same_ssid) {
612                 entry->short_ssid = s_ssid_tmp;
613                 entry->short_ssid_valid = true;
614
615                 /*
616                  * This is safe because we validate datalen in
617                  * cfg80211_parse_colocated_ap(), before calling this
618                  * function.
619                  */
620                 memcpy(&entry->ssid, &ssid_elem->data,
621                        ssid_elem->datalen);
622                 entry->ssid_len = ssid_elem->datalen;
623         }
624         return 0;
625 }
626
627 static int cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
628                                        struct list_head *list)
629 {
630         struct ieee80211_neighbor_ap_info *ap_info;
631         const struct element *elem, *ssid_elem;
632         const u8 *pos, *end;
633         u32 s_ssid_tmp;
634         int n_coloc = 0, ret;
635         LIST_HEAD(ap_list);
636
637         elem = cfg80211_find_elem(WLAN_EID_REDUCED_NEIGHBOR_REPORT, ies->data,
638                                   ies->len);
639         if (!elem)
640                 return 0;
641
642         pos = elem->data;
643         end = pos + elem->datalen;
644
645         ret = cfg80211_calc_short_ssid(ies, &ssid_elem, &s_ssid_tmp);
646         if (ret)
647                 return ret;
648
649         /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
650         while (pos + sizeof(*ap_info) <= end) {
651                 enum nl80211_band band;
652                 int freq;
653                 u8 length, i, count;
654
655                 ap_info = (void *)pos;
656                 count = u8_get_bits(ap_info->tbtt_info_hdr,
657                                     IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
658                 length = ap_info->tbtt_info_len;
659
660                 pos += sizeof(*ap_info);
661
662                 if (!ieee80211_operating_class_to_band(ap_info->op_class,
663                                                        &band))
664                         break;
665
666                 freq = ieee80211_channel_to_frequency(ap_info->channel, band);
667
668                 if (end - pos < count * length)
669                         break;
670
671                 if (u8_get_bits(ap_info->tbtt_info_hdr,
672                                 IEEE80211_AP_INFO_TBTT_HDR_TYPE) !=
673                     IEEE80211_TBTT_INFO_TYPE_TBTT) {
674                         pos += count * length;
675                         continue;
676                 }
677
678                 /*
679                  * TBTT info must include bss param + BSSID +
680                  * (short SSID or same_ssid bit to be set).
681                  * ignore other options, and move to the
682                  * next AP info
683                  */
684                 if (band != NL80211_BAND_6GHZ ||
685                     (length != IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM &&
686                      length < IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM)) {
687                         pos += count * length;
688                         continue;
689                 }
690
691                 for (i = 0; i < count; i++) {
692                         struct cfg80211_colocated_ap *entry;
693
694                         entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN,
695                                         GFP_ATOMIC);
696
697                         if (!entry)
698                                 break;
699
700                         entry->center_freq = freq;
701
702                         if (!cfg80211_parse_ap_info(entry, pos, length,
703                                                     ssid_elem, s_ssid_tmp)) {
704                                 n_coloc++;
705                                 list_add_tail(&entry->list, &ap_list);
706                         } else {
707                                 kfree(entry);
708                         }
709
710                         pos += length;
711                 }
712         }
713
714         if (pos != end) {
715                 cfg80211_free_coloc_ap_list(&ap_list);
716                 return 0;
717         }
718
719         list_splice_tail(&ap_list, list);
720         return n_coloc;
721 }
722
723 static  void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
724                                         struct ieee80211_channel *chan,
725                                         bool add_to_6ghz)
726 {
727         int i;
728         u32 n_channels = request->n_channels;
729         struct cfg80211_scan_6ghz_params *params =
730                 &request->scan_6ghz_params[request->n_6ghz_params];
731
732         for (i = 0; i < n_channels; i++) {
733                 if (request->channels[i] == chan) {
734                         if (add_to_6ghz)
735                                 params->channel_idx = i;
736                         return;
737                 }
738         }
739
740         request->channels[n_channels] = chan;
741         if (add_to_6ghz)
742                 request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
743                         n_channels;
744
745         request->n_channels++;
746 }
747
748 static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
749                                      struct cfg80211_scan_request *request)
750 {
751         int i;
752         u32 s_ssid;
753
754         for (i = 0; i < request->n_ssids; i++) {
755                 /* wildcard ssid in the scan request */
756                 if (!request->ssids[i].ssid_len) {
757                         if (ap->multi_bss && !ap->transmitted_bssid)
758                                 continue;
759
760                         return true;
761                 }
762
763                 if (ap->ssid_len &&
764                     ap->ssid_len == request->ssids[i].ssid_len) {
765                         if (!memcmp(request->ssids[i].ssid, ap->ssid,
766                                     ap->ssid_len))
767                                 return true;
768                 } else if (ap->short_ssid_valid) {
769                         s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
770                                            request->ssids[i].ssid_len);
771
772                         if (ap->short_ssid == s_ssid)
773                                 return true;
774                 }
775         }
776
777         return false;
778 }
779
780 static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
781 {
782         u8 i;
783         struct cfg80211_colocated_ap *ap;
784         int n_channels, count = 0, err;
785         struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
786         LIST_HEAD(coloc_ap_list);
787         bool need_scan_psc = true;
788         const struct ieee80211_sband_iftype_data *iftd;
789
790         rdev_req->scan_6ghz = true;
791
792         if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
793                 return -EOPNOTSUPP;
794
795         iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
796                                                rdev_req->wdev->iftype);
797         if (!iftd || !iftd->he_cap.has_he)
798                 return -EOPNOTSUPP;
799
800         n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
801
802         if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
803                 struct cfg80211_internal_bss *intbss;
804
805                 spin_lock_bh(&rdev->bss_lock);
806                 list_for_each_entry(intbss, &rdev->bss_list, list) {
807                         struct cfg80211_bss *res = &intbss->pub;
808                         const struct cfg80211_bss_ies *ies;
809
810                         ies = rcu_access_pointer(res->ies);
811                         count += cfg80211_parse_colocated_ap(ies,
812                                                              &coloc_ap_list);
813                 }
814                 spin_unlock_bh(&rdev->bss_lock);
815         }
816
817         request = kzalloc(struct_size(request, channels, n_channels) +
818                           sizeof(*request->scan_6ghz_params) * count +
819                           sizeof(*request->ssids) * rdev_req->n_ssids,
820                           GFP_KERNEL);
821         if (!request) {
822                 cfg80211_free_coloc_ap_list(&coloc_ap_list);
823                 return -ENOMEM;
824         }
825
826         *request = *rdev_req;
827         request->n_channels = 0;
828         request->scan_6ghz_params =
829                 (void *)&request->channels[n_channels];
830
831         /*
832          * PSC channels should not be scanned in case of direct scan with 1 SSID
833          * and at least one of the reported co-located APs with same SSID
834          * indicating that all APs in the same ESS are co-located
835          */
836         if (count && request->n_ssids == 1 && request->ssids[0].ssid_len) {
837                 list_for_each_entry(ap, &coloc_ap_list, list) {
838                         if (ap->colocated_ess &&
839                             cfg80211_find_ssid_match(ap, request)) {
840                                 need_scan_psc = false;
841                                 break;
842                         }
843                 }
844         }
845
846         /*
847          * add to the scan request the channels that need to be scanned
848          * regardless of the collocated APs (PSC channels or all channels
849          * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
850          */
851         for (i = 0; i < rdev_req->n_channels; i++) {
852                 if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
853                     ((need_scan_psc &&
854                       cfg80211_channel_is_psc(rdev_req->channels[i])) ||
855                      !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
856                         cfg80211_scan_req_add_chan(request,
857                                                    rdev_req->channels[i],
858                                                    false);
859                 }
860         }
861
862         if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
863                 goto skip;
864
865         list_for_each_entry(ap, &coloc_ap_list, list) {
866                 bool found = false;
867                 struct cfg80211_scan_6ghz_params *scan_6ghz_params =
868                         &request->scan_6ghz_params[request->n_6ghz_params];
869                 struct ieee80211_channel *chan =
870                         ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
871
872                 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
873                         continue;
874
875                 for (i = 0; i < rdev_req->n_channels; i++) {
876                         if (rdev_req->channels[i] == chan)
877                                 found = true;
878                 }
879
880                 if (!found)
881                         continue;
882
883                 if (request->n_ssids > 0 &&
884                     !cfg80211_find_ssid_match(ap, request))
885                         continue;
886
887                 if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
888                         continue;
889
890                 cfg80211_scan_req_add_chan(request, chan, true);
891                 memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
892                 scan_6ghz_params->short_ssid = ap->short_ssid;
893                 scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
894                 scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
895
896                 /*
897                  * If a PSC channel is added to the scan and 'need_scan_psc' is
898                  * set to false, then all the APs that the scan logic is
899                  * interested with on the channel are collocated and thus there
900                  * is no need to perform the initial PSC channel listen.
901                  */
902                 if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
903                         scan_6ghz_params->psc_no_listen = true;
904
905                 request->n_6ghz_params++;
906         }
907
908 skip:
909         cfg80211_free_coloc_ap_list(&coloc_ap_list);
910
911         if (request->n_channels) {
912                 struct cfg80211_scan_request *old = rdev->int_scan_req;
913                 rdev->int_scan_req = request;
914
915                 /*
916                  * Add the ssids from the parent scan request to the new scan
917                  * request, so the driver would be able to use them in its
918                  * probe requests to discover hidden APs on PSC channels.
919                  */
920                 request->ssids = (void *)&request->channels[request->n_channels];
921                 request->n_ssids = rdev_req->n_ssids;
922                 memcpy(request->ssids, rdev_req->ssids, sizeof(*request->ssids) *
923                        request->n_ssids);
924
925                 /*
926                  * If this scan follows a previous scan, save the scan start
927                  * info from the first part of the scan
928                  */
929                 if (old)
930                         rdev->int_scan_req->info = old->info;
931
932                 err = rdev_scan(rdev, request);
933                 if (err) {
934                         rdev->int_scan_req = old;
935                         kfree(request);
936                 } else {
937                         kfree(old);
938                 }
939
940                 return err;
941         }
942
943         kfree(request);
944         return -EINVAL;
945 }
946
947 int cfg80211_scan(struct cfg80211_registered_device *rdev)
948 {
949         struct cfg80211_scan_request *request;
950         struct cfg80211_scan_request *rdev_req = rdev->scan_req;
951         u32 n_channels = 0, idx, i;
952
953         if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
954                 return rdev_scan(rdev, rdev_req);
955
956         for (i = 0; i < rdev_req->n_channels; i++) {
957                 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
958                         n_channels++;
959         }
960
961         if (!n_channels)
962                 return cfg80211_scan_6ghz(rdev);
963
964         request = kzalloc(struct_size(request, channels, n_channels),
965                           GFP_KERNEL);
966         if (!request)
967                 return -ENOMEM;
968
969         *request = *rdev_req;
970         request->n_channels = n_channels;
971
972         for (i = idx = 0; i < rdev_req->n_channels; i++) {
973                 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
974                         request->channels[idx++] = rdev_req->channels[i];
975         }
976
977         rdev_req->scan_6ghz = false;
978         rdev->int_scan_req = request;
979         return rdev_scan(rdev, request);
980 }
981
982 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
983                            bool send_message)
984 {
985         struct cfg80211_scan_request *request, *rdev_req;
986         struct wireless_dev *wdev;
987         struct sk_buff *msg;
988 #ifdef CONFIG_CFG80211_WEXT
989         union iwreq_data wrqu;
990 #endif
991
992         lockdep_assert_held(&rdev->wiphy.mtx);
993
994         if (rdev->scan_msg) {
995                 nl80211_send_scan_msg(rdev, rdev->scan_msg);
996                 rdev->scan_msg = NULL;
997                 return;
998         }
999
1000         rdev_req = rdev->scan_req;
1001         if (!rdev_req)
1002                 return;
1003
1004         wdev = rdev_req->wdev;
1005         request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
1006
1007         if (wdev_running(wdev) &&
1008             (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
1009             !rdev_req->scan_6ghz && !request->info.aborted &&
1010             !cfg80211_scan_6ghz(rdev))
1011                 return;
1012
1013         /*
1014          * This must be before sending the other events!
1015          * Otherwise, wpa_supplicant gets completely confused with
1016          * wext events.
1017          */
1018         if (wdev->netdev)
1019                 cfg80211_sme_scan_done(wdev->netdev);
1020
1021         if (!request->info.aborted &&
1022             request->flags & NL80211_SCAN_FLAG_FLUSH) {
1023                 /* flush entries from previous scans */
1024                 spin_lock_bh(&rdev->bss_lock);
1025                 __cfg80211_bss_expire(rdev, request->scan_start);
1026                 spin_unlock_bh(&rdev->bss_lock);
1027         }
1028
1029         msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
1030
1031 #ifdef CONFIG_CFG80211_WEXT
1032         if (wdev->netdev && !request->info.aborted) {
1033                 memset(&wrqu, 0, sizeof(wrqu));
1034
1035                 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
1036         }
1037 #endif
1038
1039         dev_put(wdev->netdev);
1040
1041         kfree(rdev->int_scan_req);
1042         rdev->int_scan_req = NULL;
1043
1044         kfree(rdev->scan_req);
1045         rdev->scan_req = NULL;
1046
1047         if (!send_message)
1048                 rdev->scan_msg = msg;
1049         else
1050                 nl80211_send_scan_msg(rdev, msg);
1051 }
1052
1053 void __cfg80211_scan_done(struct wiphy *wiphy, struct wiphy_work *wk)
1054 {
1055         ___cfg80211_scan_done(wiphy_to_rdev(wiphy), true);
1056 }
1057
1058 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1059                         struct cfg80211_scan_info *info)
1060 {
1061         struct cfg80211_scan_info old_info = request->info;
1062
1063         trace_cfg80211_scan_done(request, info);
1064         WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
1065                 request != wiphy_to_rdev(request->wiphy)->int_scan_req);
1066
1067         request->info = *info;
1068
1069         /*
1070          * In case the scan is split, the scan_start_tsf and tsf_bssid should
1071          * be of the first part. In such a case old_info.scan_start_tsf should
1072          * be non zero.
1073          */
1074         if (request->scan_6ghz && old_info.scan_start_tsf) {
1075                 request->info.scan_start_tsf = old_info.scan_start_tsf;
1076                 memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
1077                        sizeof(request->info.tsf_bssid));
1078         }
1079
1080         request->notified = true;
1081         wiphy_work_queue(request->wiphy,
1082                          &wiphy_to_rdev(request->wiphy)->scan_done_wk);
1083 }
1084 EXPORT_SYMBOL(cfg80211_scan_done);
1085
1086 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1087                                  struct cfg80211_sched_scan_request *req)
1088 {
1089         lockdep_assert_held(&rdev->wiphy.mtx);
1090
1091         list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1092 }
1093
1094 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1095                                         struct cfg80211_sched_scan_request *req)
1096 {
1097         lockdep_assert_held(&rdev->wiphy.mtx);
1098
1099         list_del_rcu(&req->list);
1100         kfree_rcu(req, rcu_head);
1101 }
1102
1103 static struct cfg80211_sched_scan_request *
1104 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1105 {
1106         struct cfg80211_sched_scan_request *pos;
1107
1108         list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1109                                 lockdep_is_held(&rdev->wiphy.mtx)) {
1110                 if (pos->reqid == reqid)
1111                         return pos;
1112         }
1113         return NULL;
1114 }
1115
1116 /*
1117  * Determines if a scheduled scan request can be handled. When a legacy
1118  * scheduled scan is running no other scheduled scan is allowed regardless
1119  * whether the request is for legacy or multi-support scan. When a multi-support
1120  * scheduled scan is running a request for legacy scan is not allowed. In this
1121  * case a request for multi-support scan can be handled if resources are
1122  * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1123  */
1124 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1125                                      bool want_multi)
1126 {
1127         struct cfg80211_sched_scan_request *pos;
1128         int i = 0;
1129
1130         list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1131                 /* request id zero means legacy in progress */
1132                 if (!i && !pos->reqid)
1133                         return -EINPROGRESS;
1134                 i++;
1135         }
1136
1137         if (i) {
1138                 /* no legacy allowed when multi request(s) are active */
1139                 if (!want_multi)
1140                         return -EINPROGRESS;
1141
1142                 /* resource limit reached */
1143                 if (i == rdev->wiphy.max_sched_scan_reqs)
1144                         return -ENOSPC;
1145         }
1146         return 0;
1147 }
1148
1149 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1150 {
1151         struct cfg80211_registered_device *rdev;
1152         struct cfg80211_sched_scan_request *req, *tmp;
1153
1154         rdev = container_of(work, struct cfg80211_registered_device,
1155                            sched_scan_res_wk);
1156
1157         wiphy_lock(&rdev->wiphy);
1158         list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1159                 if (req->report_results) {
1160                         req->report_results = false;
1161                         if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1162                                 /* flush entries from previous scans */
1163                                 spin_lock_bh(&rdev->bss_lock);
1164                                 __cfg80211_bss_expire(rdev, req->scan_start);
1165                                 spin_unlock_bh(&rdev->bss_lock);
1166                                 req->scan_start = jiffies;
1167                         }
1168                         nl80211_send_sched_scan(req,
1169                                                 NL80211_CMD_SCHED_SCAN_RESULTS);
1170                 }
1171         }
1172         wiphy_unlock(&rdev->wiphy);
1173 }
1174
1175 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1176 {
1177         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1178         struct cfg80211_sched_scan_request *request;
1179
1180         trace_cfg80211_sched_scan_results(wiphy, reqid);
1181         /* ignore if we're not scanning */
1182
1183         rcu_read_lock();
1184         request = cfg80211_find_sched_scan_req(rdev, reqid);
1185         if (request) {
1186                 request->report_results = true;
1187                 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1188         }
1189         rcu_read_unlock();
1190 }
1191 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1192
1193 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1194 {
1195         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1196
1197         lockdep_assert_held(&wiphy->mtx);
1198
1199         trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1200
1201         __cfg80211_stop_sched_scan(rdev, reqid, true);
1202 }
1203 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1204
1205 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1206 {
1207         wiphy_lock(wiphy);
1208         cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1209         wiphy_unlock(wiphy);
1210 }
1211 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1212
1213 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1214                                  struct cfg80211_sched_scan_request *req,
1215                                  bool driver_initiated)
1216 {
1217         lockdep_assert_held(&rdev->wiphy.mtx);
1218
1219         if (!driver_initiated) {
1220                 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1221                 if (err)
1222                         return err;
1223         }
1224
1225         nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1226
1227         cfg80211_del_sched_scan_req(rdev, req);
1228
1229         return 0;
1230 }
1231
1232 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1233                                u64 reqid, bool driver_initiated)
1234 {
1235         struct cfg80211_sched_scan_request *sched_scan_req;
1236
1237         lockdep_assert_held(&rdev->wiphy.mtx);
1238
1239         sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1240         if (!sched_scan_req)
1241                 return -ENOENT;
1242
1243         return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1244                                             driver_initiated);
1245 }
1246
1247 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1248                       unsigned long age_secs)
1249 {
1250         struct cfg80211_internal_bss *bss;
1251         unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
1252
1253         spin_lock_bh(&rdev->bss_lock);
1254         list_for_each_entry(bss, &rdev->bss_list, list)
1255                 bss->ts -= age_jiffies;
1256         spin_unlock_bh(&rdev->bss_lock);
1257 }
1258
1259 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1260 {
1261         __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1262 }
1263
1264 void cfg80211_bss_flush(struct wiphy *wiphy)
1265 {
1266         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1267
1268         spin_lock_bh(&rdev->bss_lock);
1269         __cfg80211_bss_expire(rdev, jiffies);
1270         spin_unlock_bh(&rdev->bss_lock);
1271 }
1272 EXPORT_SYMBOL(cfg80211_bss_flush);
1273
1274 const struct element *
1275 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1276                          const u8 *match, unsigned int match_len,
1277                          unsigned int match_offset)
1278 {
1279         const struct element *elem;
1280
1281         for_each_element_id(elem, eid, ies, len) {
1282                 if (elem->datalen >= match_offset + match_len &&
1283                     !memcmp(elem->data + match_offset, match, match_len))
1284                         return elem;
1285         }
1286
1287         return NULL;
1288 }
1289 EXPORT_SYMBOL(cfg80211_find_elem_match);
1290
1291 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1292                                                 const u8 *ies,
1293                                                 unsigned int len)
1294 {
1295         const struct element *elem;
1296         u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1297         int match_len = (oui_type < 0) ? 3 : sizeof(match);
1298
1299         if (WARN_ON(oui_type > 0xff))
1300                 return NULL;
1301
1302         elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1303                                         match, match_len, 0);
1304
1305         if (!elem || elem->datalen < 4)
1306                 return NULL;
1307
1308         return elem;
1309 }
1310 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1311
1312 /**
1313  * enum bss_compare_mode - BSS compare mode
1314  * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1315  * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1316  * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1317  */
1318 enum bss_compare_mode {
1319         BSS_CMP_REGULAR,
1320         BSS_CMP_HIDE_ZLEN,
1321         BSS_CMP_HIDE_NUL,
1322 };
1323
1324 static int cmp_bss(struct cfg80211_bss *a,
1325                    struct cfg80211_bss *b,
1326                    enum bss_compare_mode mode)
1327 {
1328         const struct cfg80211_bss_ies *a_ies, *b_ies;
1329         const u8 *ie1 = NULL;
1330         const u8 *ie2 = NULL;
1331         int i, r;
1332
1333         if (a->channel != b->channel)
1334                 return (b->channel->center_freq * 1000 + b->channel->freq_offset) -
1335                        (a->channel->center_freq * 1000 + a->channel->freq_offset);
1336
1337         a_ies = rcu_access_pointer(a->ies);
1338         if (!a_ies)
1339                 return -1;
1340         b_ies = rcu_access_pointer(b->ies);
1341         if (!b_ies)
1342                 return 1;
1343
1344         if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1345                 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1346                                        a_ies->data, a_ies->len);
1347         if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1348                 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1349                                        b_ies->data, b_ies->len);
1350         if (ie1 && ie2) {
1351                 int mesh_id_cmp;
1352
1353                 if (ie1[1] == ie2[1])
1354                         mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1355                 else
1356                         mesh_id_cmp = ie2[1] - ie1[1];
1357
1358                 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1359                                        a_ies->data, a_ies->len);
1360                 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1361                                        b_ies->data, b_ies->len);
1362                 if (ie1 && ie2) {
1363                         if (mesh_id_cmp)
1364                                 return mesh_id_cmp;
1365                         if (ie1[1] != ie2[1])
1366                                 return ie2[1] - ie1[1];
1367                         return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1368                 }
1369         }
1370
1371         r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1372         if (r)
1373                 return r;
1374
1375         ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1376         ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1377
1378         if (!ie1 && !ie2)
1379                 return 0;
1380
1381         /*
1382          * Note that with "hide_ssid", the function returns a match if
1383          * the already-present BSS ("b") is a hidden SSID beacon for
1384          * the new BSS ("a").
1385          */
1386
1387         /* sort missing IE before (left of) present IE */
1388         if (!ie1)
1389                 return -1;
1390         if (!ie2)
1391                 return 1;
1392
1393         switch (mode) {
1394         case BSS_CMP_HIDE_ZLEN:
1395                 /*
1396                  * In ZLEN mode we assume the BSS entry we're
1397                  * looking for has a zero-length SSID. So if
1398                  * the one we're looking at right now has that,
1399                  * return 0. Otherwise, return the difference
1400                  * in length, but since we're looking for the
1401                  * 0-length it's really equivalent to returning
1402                  * the length of the one we're looking at.
1403                  *
1404                  * No content comparison is needed as we assume
1405                  * the content length is zero.
1406                  */
1407                 return ie2[1];
1408         case BSS_CMP_REGULAR:
1409         default:
1410                 /* sort by length first, then by contents */
1411                 if (ie1[1] != ie2[1])
1412                         return ie2[1] - ie1[1];
1413                 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1414         case BSS_CMP_HIDE_NUL:
1415                 if (ie1[1] != ie2[1])
1416                         return ie2[1] - ie1[1];
1417                 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1418                 for (i = 0; i < ie2[1]; i++)
1419                         if (ie2[i + 2])
1420                                 return -1;
1421                 return 0;
1422         }
1423 }
1424
1425 static bool cfg80211_bss_type_match(u16 capability,
1426                                     enum nl80211_band band,
1427                                     enum ieee80211_bss_type bss_type)
1428 {
1429         bool ret = true;
1430         u16 mask, val;
1431
1432         if (bss_type == IEEE80211_BSS_TYPE_ANY)
1433                 return ret;
1434
1435         if (band == NL80211_BAND_60GHZ) {
1436                 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1437                 switch (bss_type) {
1438                 case IEEE80211_BSS_TYPE_ESS:
1439                         val = WLAN_CAPABILITY_DMG_TYPE_AP;
1440                         break;
1441                 case IEEE80211_BSS_TYPE_PBSS:
1442                         val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1443                         break;
1444                 case IEEE80211_BSS_TYPE_IBSS:
1445                         val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1446                         break;
1447                 default:
1448                         return false;
1449                 }
1450         } else {
1451                 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1452                 switch (bss_type) {
1453                 case IEEE80211_BSS_TYPE_ESS:
1454                         val = WLAN_CAPABILITY_ESS;
1455                         break;
1456                 case IEEE80211_BSS_TYPE_IBSS:
1457                         val = WLAN_CAPABILITY_IBSS;
1458                         break;
1459                 case IEEE80211_BSS_TYPE_MBSS:
1460                         val = 0;
1461                         break;
1462                 default:
1463                         return false;
1464                 }
1465         }
1466
1467         ret = ((capability & mask) == val);
1468         return ret;
1469 }
1470
1471 /* Returned bss is reference counted and must be cleaned up appropriately. */
1472 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
1473                                       struct ieee80211_channel *channel,
1474                                       const u8 *bssid,
1475                                       const u8 *ssid, size_t ssid_len,
1476                                       enum ieee80211_bss_type bss_type,
1477                                       enum ieee80211_privacy privacy)
1478 {
1479         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1480         struct cfg80211_internal_bss *bss, *res = NULL;
1481         unsigned long now = jiffies;
1482         int bss_privacy;
1483
1484         trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1485                                privacy);
1486
1487         spin_lock_bh(&rdev->bss_lock);
1488
1489         list_for_each_entry(bss, &rdev->bss_list, list) {
1490                 if (!cfg80211_bss_type_match(bss->pub.capability,
1491                                              bss->pub.channel->band, bss_type))
1492                         continue;
1493
1494                 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1495                 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1496                     (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1497                         continue;
1498                 if (channel && bss->pub.channel != channel)
1499                         continue;
1500                 if (!is_valid_ether_addr(bss->pub.bssid))
1501                         continue;
1502                 /* Don't get expired BSS structs */
1503                 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1504                     !atomic_read(&bss->hold))
1505                         continue;
1506                 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
1507                         res = bss;
1508                         bss_ref_get(rdev, res);
1509                         break;
1510                 }
1511         }
1512
1513         spin_unlock_bh(&rdev->bss_lock);
1514         if (!res)
1515                 return NULL;
1516         trace_cfg80211_return_bss(&res->pub);
1517         return &res->pub;
1518 }
1519 EXPORT_SYMBOL(cfg80211_get_bss);
1520
1521 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1522                           struct cfg80211_internal_bss *bss)
1523 {
1524         struct rb_node **p = &rdev->bss_tree.rb_node;
1525         struct rb_node *parent = NULL;
1526         struct cfg80211_internal_bss *tbss;
1527         int cmp;
1528
1529         while (*p) {
1530                 parent = *p;
1531                 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1532
1533                 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1534
1535                 if (WARN_ON(!cmp)) {
1536                         /* will sort of leak this BSS */
1537                         return;
1538                 }
1539
1540                 if (cmp < 0)
1541                         p = &(*p)->rb_left;
1542                 else
1543                         p = &(*p)->rb_right;
1544         }
1545
1546         rb_link_node(&bss->rbn, parent, p);
1547         rb_insert_color(&bss->rbn, &rdev->bss_tree);
1548 }
1549
1550 static struct cfg80211_internal_bss *
1551 rb_find_bss(struct cfg80211_registered_device *rdev,
1552             struct cfg80211_internal_bss *res,
1553             enum bss_compare_mode mode)
1554 {
1555         struct rb_node *n = rdev->bss_tree.rb_node;
1556         struct cfg80211_internal_bss *bss;
1557         int r;
1558
1559         while (n) {
1560                 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1561                 r = cmp_bss(&res->pub, &bss->pub, mode);
1562
1563                 if (r == 0)
1564                         return bss;
1565                 else if (r < 0)
1566                         n = n->rb_left;
1567                 else
1568                         n = n->rb_right;
1569         }
1570
1571         return NULL;
1572 }
1573
1574 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1575                                    struct cfg80211_internal_bss *new)
1576 {
1577         const struct cfg80211_bss_ies *ies;
1578         struct cfg80211_internal_bss *bss;
1579         const u8 *ie;
1580         int i, ssidlen;
1581         u8 fold = 0;
1582         u32 n_entries = 0;
1583
1584         ies = rcu_access_pointer(new->pub.beacon_ies);
1585         if (WARN_ON(!ies))
1586                 return false;
1587
1588         ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1589         if (!ie) {
1590                 /* nothing to do */
1591                 return true;
1592         }
1593
1594         ssidlen = ie[1];
1595         for (i = 0; i < ssidlen; i++)
1596                 fold |= ie[2 + i];
1597
1598         if (fold) {
1599                 /* not a hidden SSID */
1600                 return true;
1601         }
1602
1603         /* This is the bad part ... */
1604
1605         list_for_each_entry(bss, &rdev->bss_list, list) {
1606                 /*
1607                  * we're iterating all the entries anyway, so take the
1608                  * opportunity to validate the list length accounting
1609                  */
1610                 n_entries++;
1611
1612                 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1613                         continue;
1614                 if (bss->pub.channel != new->pub.channel)
1615                         continue;
1616                 if (bss->pub.scan_width != new->pub.scan_width)
1617                         continue;
1618                 if (rcu_access_pointer(bss->pub.beacon_ies))
1619                         continue;
1620                 ies = rcu_access_pointer(bss->pub.ies);
1621                 if (!ies)
1622                         continue;
1623                 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1624                 if (!ie)
1625                         continue;
1626                 if (ssidlen && ie[1] != ssidlen)
1627                         continue;
1628                 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1629                         continue;
1630                 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1631                         list_del(&bss->hidden_list);
1632                 /* combine them */
1633                 list_add(&bss->hidden_list, &new->hidden_list);
1634                 bss->pub.hidden_beacon_bss = &new->pub;
1635                 new->refcount += bss->refcount;
1636                 rcu_assign_pointer(bss->pub.beacon_ies,
1637                                    new->pub.beacon_ies);
1638         }
1639
1640         WARN_ONCE(n_entries != rdev->bss_entries,
1641                   "rdev bss entries[%d]/list[len:%d] corruption\n",
1642                   rdev->bss_entries, n_entries);
1643
1644         return true;
1645 }
1646
1647 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1648                                          const struct cfg80211_bss_ies *new_ies,
1649                                          const struct cfg80211_bss_ies *old_ies)
1650 {
1651         struct cfg80211_internal_bss *bss;
1652
1653         /* Assign beacon IEs to all sub entries */
1654         list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1655                 const struct cfg80211_bss_ies *ies;
1656
1657                 ies = rcu_access_pointer(bss->pub.beacon_ies);
1658                 WARN_ON(ies != old_ies);
1659
1660                 rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1661         }
1662 }
1663
1664 static bool
1665 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1666                           struct cfg80211_internal_bss *known,
1667                           struct cfg80211_internal_bss *new,
1668                           bool signal_valid)
1669 {
1670         lockdep_assert_held(&rdev->bss_lock);
1671
1672         /* Update IEs */
1673         if (rcu_access_pointer(new->pub.proberesp_ies)) {
1674                 const struct cfg80211_bss_ies *old;
1675
1676                 old = rcu_access_pointer(known->pub.proberesp_ies);
1677
1678                 rcu_assign_pointer(known->pub.proberesp_ies,
1679                                    new->pub.proberesp_ies);
1680                 /* Override possible earlier Beacon frame IEs */
1681                 rcu_assign_pointer(known->pub.ies,
1682                                    new->pub.proberesp_ies);
1683                 if (old)
1684                         kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1685         } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1686                 const struct cfg80211_bss_ies *old;
1687
1688                 if (known->pub.hidden_beacon_bss &&
1689                     !list_empty(&known->hidden_list)) {
1690                         const struct cfg80211_bss_ies *f;
1691
1692                         /* The known BSS struct is one of the probe
1693                          * response members of a group, but we're
1694                          * receiving a beacon (beacon_ies in the new
1695                          * bss is used). This can only mean that the
1696                          * AP changed its beacon from not having an
1697                          * SSID to showing it, which is confusing so
1698                          * drop this information.
1699                          */
1700
1701                         f = rcu_access_pointer(new->pub.beacon_ies);
1702                         kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1703                         return false;
1704                 }
1705
1706                 old = rcu_access_pointer(known->pub.beacon_ies);
1707
1708                 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1709
1710                 /* Override IEs if they were from a beacon before */
1711                 if (old == rcu_access_pointer(known->pub.ies))
1712                         rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1713
1714                 cfg80211_update_hidden_bsses(known,
1715                                              rcu_access_pointer(new->pub.beacon_ies),
1716                                              old);
1717
1718                 if (old)
1719                         kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1720         }
1721
1722         known->pub.beacon_interval = new->pub.beacon_interval;
1723
1724         /* don't update the signal if beacon was heard on
1725          * adjacent channel.
1726          */
1727         if (signal_valid)
1728                 known->pub.signal = new->pub.signal;
1729         known->pub.capability = new->pub.capability;
1730         known->ts = new->ts;
1731         known->ts_boottime = new->ts_boottime;
1732         known->parent_tsf = new->parent_tsf;
1733         known->pub.chains = new->pub.chains;
1734         memcpy(known->pub.chain_signal, new->pub.chain_signal,
1735                IEEE80211_MAX_CHAINS);
1736         ether_addr_copy(known->parent_bssid, new->parent_bssid);
1737         known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1738         known->pub.bssid_index = new->pub.bssid_index;
1739
1740         return true;
1741 }
1742
1743 /* Returned bss is reference counted and must be cleaned up appropriately. */
1744 static struct cfg80211_internal_bss *
1745 __cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1746                       struct cfg80211_internal_bss *tmp,
1747                       bool signal_valid, unsigned long ts)
1748 {
1749         struct cfg80211_internal_bss *found = NULL;
1750
1751         if (WARN_ON(!tmp->pub.channel))
1752                 return NULL;
1753
1754         tmp->ts = ts;
1755
1756         if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1757                 return NULL;
1758         }
1759
1760         found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1761
1762         if (found) {
1763                 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1764                         return NULL;
1765         } else {
1766                 struct cfg80211_internal_bss *new;
1767                 struct cfg80211_internal_bss *hidden;
1768                 struct cfg80211_bss_ies *ies;
1769
1770                 /*
1771                  * create a copy -- the "res" variable that is passed in
1772                  * is allocated on the stack since it's not needed in the
1773                  * more common case of an update
1774                  */
1775                 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1776                               GFP_ATOMIC);
1777                 if (!new) {
1778                         ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1779                         if (ies)
1780                                 kfree_rcu(ies, rcu_head);
1781                         ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1782                         if (ies)
1783                                 kfree_rcu(ies, rcu_head);
1784                         return NULL;
1785                 }
1786                 memcpy(new, tmp, sizeof(*new));
1787                 new->refcount = 1;
1788                 INIT_LIST_HEAD(&new->hidden_list);
1789                 INIT_LIST_HEAD(&new->pub.nontrans_list);
1790                 /* we'll set this later if it was non-NULL */
1791                 new->pub.transmitted_bss = NULL;
1792
1793                 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1794                         hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1795                         if (!hidden)
1796                                 hidden = rb_find_bss(rdev, tmp,
1797                                                      BSS_CMP_HIDE_NUL);
1798                         if (hidden) {
1799                                 new->pub.hidden_beacon_bss = &hidden->pub;
1800                                 list_add(&new->hidden_list,
1801                                          &hidden->hidden_list);
1802                                 hidden->refcount++;
1803                                 rcu_assign_pointer(new->pub.beacon_ies,
1804                                                    hidden->pub.beacon_ies);
1805                         }
1806                 } else {
1807                         /*
1808                          * Ok so we found a beacon, and don't have an entry. If
1809                          * it's a beacon with hidden SSID, we might be in for an
1810                          * expensive search for any probe responses that should
1811                          * be grouped with this beacon for updates ...
1812                          */
1813                         if (!cfg80211_combine_bsses(rdev, new)) {
1814                                 bss_ref_put(rdev, new);
1815                                 return NULL;
1816                         }
1817                 }
1818
1819                 if (rdev->bss_entries >= bss_entries_limit &&
1820                     !cfg80211_bss_expire_oldest(rdev)) {
1821                         bss_ref_put(rdev, new);
1822                         return NULL;
1823                 }
1824
1825                 /* This must be before the call to bss_ref_get */
1826                 if (tmp->pub.transmitted_bss) {
1827                         new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1828                         bss_ref_get(rdev, bss_from_pub(tmp->pub.transmitted_bss));
1829                 }
1830
1831                 list_add_tail(&new->list, &rdev->bss_list);
1832                 rdev->bss_entries++;
1833                 rb_insert_bss(rdev, new);
1834                 found = new;
1835         }
1836
1837         rdev->bss_generation++;
1838         bss_ref_get(rdev, found);
1839
1840         return found;
1841 }
1842
1843 struct cfg80211_internal_bss *
1844 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1845                     struct cfg80211_internal_bss *tmp,
1846                     bool signal_valid, unsigned long ts)
1847 {
1848         struct cfg80211_internal_bss *res;
1849
1850         spin_lock_bh(&rdev->bss_lock);
1851         res = __cfg80211_bss_update(rdev, tmp, signal_valid, ts);
1852         spin_unlock_bh(&rdev->bss_lock);
1853
1854         return res;
1855 }
1856
1857 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
1858                                     enum nl80211_band band)
1859 {
1860         const struct element *tmp;
1861
1862         if (band == NL80211_BAND_6GHZ) {
1863                 struct ieee80211_he_operation *he_oper;
1864
1865                 tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
1866                                              ielen);
1867                 if (tmp && tmp->datalen >= sizeof(*he_oper) &&
1868                     tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
1869                         const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
1870
1871                         he_oper = (void *)&tmp->data[1];
1872
1873                         he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
1874                         if (!he_6ghz_oper)
1875                                 return -1;
1876
1877                         return he_6ghz_oper->primary;
1878                 }
1879         } else if (band == NL80211_BAND_S1GHZ) {
1880                 tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
1881                 if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
1882                         struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
1883
1884                         return s1gop->oper_ch;
1885                 }
1886         } else {
1887                 tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
1888                 if (tmp && tmp->datalen == 1)
1889                         return tmp->data[0];
1890
1891                 tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
1892                 if (tmp &&
1893                     tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
1894                         struct ieee80211_ht_operation *htop = (void *)tmp->data;
1895
1896                         return htop->primary_chan;
1897                 }
1898         }
1899
1900         return -1;
1901 }
1902 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
1903
1904 /*
1905  * Update RX channel information based on the available frame payload
1906  * information. This is mainly for the 2.4 GHz band where frames can be received
1907  * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1908  * element to indicate the current (transmitting) channel, but this might also
1909  * be needed on other bands if RX frequency does not match with the actual
1910  * operating channel of a BSS, or if the AP reports a different primary channel.
1911  */
1912 static struct ieee80211_channel *
1913 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1914                          struct ieee80211_channel *channel,
1915                          enum nl80211_bss_scan_width scan_width)
1916 {
1917         u32 freq;
1918         int channel_number;
1919         struct ieee80211_channel *alt_channel;
1920
1921         channel_number = cfg80211_get_ies_channel_number(ie, ielen,
1922                                                          channel->band);
1923
1924         if (channel_number < 0) {
1925                 /* No channel information in frame payload */
1926                 return channel;
1927         }
1928
1929         freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1930
1931         /*
1932          * Frame info (beacon/prob res) is the same as received channel,
1933          * no need for further processing.
1934          */
1935         if (freq == ieee80211_channel_to_khz(channel))
1936                 return channel;
1937
1938         alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1939         if (!alt_channel) {
1940                 if (channel->band == NL80211_BAND_2GHZ ||
1941                     channel->band == NL80211_BAND_6GHZ) {
1942                         /*
1943                          * Better not allow unexpected channels when that could
1944                          * be going beyond the 1-11 range (e.g., discovering
1945                          * BSS on channel 12 when radio is configured for
1946                          * channel 11) or beyond the 6 GHz channel range.
1947                          */
1948                         return NULL;
1949                 }
1950
1951                 /* No match for the payload channel number - ignore it */
1952                 return channel;
1953         }
1954
1955         if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1956             scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1957                 /*
1958                  * Ignore channel number in 5 and 10 MHz channels where there
1959                  * may not be an n:1 or 1:n mapping between frequencies and
1960                  * channel numbers.
1961                  */
1962                 return channel;
1963         }
1964
1965         /*
1966          * Use the channel determined through the payload channel number
1967          * instead of the RX channel reported by the driver.
1968          */
1969         if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1970                 return NULL;
1971         return alt_channel;
1972 }
1973
1974 struct cfg80211_inform_single_bss_data {
1975         struct cfg80211_inform_bss *drv_data;
1976         enum cfg80211_bss_frame_type ftype;
1977         u8 bssid[ETH_ALEN];
1978         u64 tsf;
1979         u16 capability;
1980         u16 beacon_interval;
1981         const u8 *ie;
1982         size_t ielen;
1983
1984         /* Set for nontransmitted BSSIDs */
1985         struct cfg80211_bss *source_bss;
1986         u8 max_bssid_indicator;
1987         u8 bssid_index;
1988 };
1989
1990 /* Returned bss is reference counted and must be cleaned up appropriately. */
1991 static struct cfg80211_bss *
1992 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1993                                 struct cfg80211_inform_single_bss_data *data,
1994                                 gfp_t gfp)
1995 {
1996         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1997         struct cfg80211_inform_bss *drv_data = data->drv_data;
1998         struct cfg80211_bss_ies *ies;
1999         struct ieee80211_channel *channel;
2000         struct cfg80211_internal_bss tmp = {}, *res;
2001         int bss_type;
2002         bool signal_valid;
2003         unsigned long ts;
2004
2005         if (WARN_ON(!wiphy))
2006                 return NULL;
2007
2008         if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2009                     (drv_data->signal < 0 || drv_data->signal > 100)))
2010                 return NULL;
2011
2012         channel = cfg80211_get_bss_channel(wiphy, data->ie, data->ielen,
2013                                            drv_data->chan, drv_data->scan_width);
2014         if (!channel)
2015                 return NULL;
2016
2017         memcpy(tmp.pub.bssid, data->bssid, ETH_ALEN);
2018         tmp.pub.channel = channel;
2019         tmp.pub.scan_width = drv_data->scan_width;
2020         tmp.pub.signal = drv_data->signal;
2021         tmp.pub.beacon_interval = data->beacon_interval;
2022         tmp.pub.capability = data->capability;
2023         tmp.ts_boottime = drv_data->boottime_ns;
2024         tmp.parent_tsf = drv_data->parent_tsf;
2025         ether_addr_copy(tmp.parent_bssid, drv_data->parent_bssid);
2026
2027         if (data->source_bss) {
2028                 tmp.pub.transmitted_bss = data->source_bss;
2029                 ts = bss_from_pub(data->source_bss)->ts;
2030                 tmp.pub.bssid_index = data->bssid_index;
2031                 tmp.pub.max_bssid_indicator = data->max_bssid_indicator;
2032         } else {
2033                 ts = jiffies;
2034
2035                 if (channel->band == NL80211_BAND_60GHZ) {
2036                         bss_type = data->capability &
2037                                    WLAN_CAPABILITY_DMG_TYPE_MASK;
2038                         if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2039                             bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2040                                 regulatory_hint_found_beacon(wiphy, channel,
2041                                                              gfp);
2042                 } else {
2043                         if (data->capability & WLAN_CAPABILITY_ESS)
2044                                 regulatory_hint_found_beacon(wiphy, channel,
2045                                                              gfp);
2046                 }
2047         }
2048
2049         /*
2050          * If we do not know here whether the IEs are from a Beacon or Probe
2051          * Response frame, we need to pick one of the options and only use it
2052          * with the driver that does not provide the full Beacon/Probe Response
2053          * frame. Use Beacon frame pointer to avoid indicating that this should
2054          * override the IEs pointer should we have received an earlier
2055          * indication of Probe Response data.
2056          */
2057         ies = kzalloc(sizeof(*ies) + data->ielen, gfp);
2058         if (!ies)
2059                 return NULL;
2060         ies->len = data->ielen;
2061         ies->tsf = data->tsf;
2062         ies->from_beacon = false;
2063         memcpy(ies->data, data->ie, data->ielen);
2064
2065         switch (data->ftype) {
2066         case CFG80211_BSS_FTYPE_BEACON:
2067                 ies->from_beacon = true;
2068                 fallthrough;
2069         case CFG80211_BSS_FTYPE_UNKNOWN:
2070                 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2071                 break;
2072         case CFG80211_BSS_FTYPE_PRESP:
2073                 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2074                 break;
2075         }
2076         rcu_assign_pointer(tmp.pub.ies, ies);
2077
2078         signal_valid = drv_data->chan == channel;
2079         spin_lock_bh(&rdev->bss_lock);
2080         res = __cfg80211_bss_update(rdev, &tmp, signal_valid, ts);
2081         if (!res)
2082                 goto drop;
2083
2084         rdev_inform_bss(rdev, &res->pub, ies, data->drv_data);
2085
2086         if (data->source_bss) {
2087                 /* this is a nontransmitting bss, we need to add it to
2088                  * transmitting bss' list if it is not there
2089                  */
2090                 if (cfg80211_add_nontrans_list(data->source_bss, &res->pub)) {
2091                         if (__cfg80211_unlink_bss(rdev, res)) {
2092                                 rdev->bss_generation++;
2093                                 res = NULL;
2094                         }
2095                 }
2096
2097                 if (!res)
2098                         goto drop;
2099         }
2100         spin_unlock_bh(&rdev->bss_lock);
2101
2102         trace_cfg80211_return_bss(&res->pub);
2103         /* __cfg80211_bss_update gives us a referenced result */
2104         return &res->pub;
2105
2106 drop:
2107         spin_unlock_bh(&rdev->bss_lock);
2108         return NULL;
2109 }
2110
2111 static const struct element
2112 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
2113                                    const struct element *mbssid_elem,
2114                                    const struct element *sub_elem)
2115 {
2116         const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
2117         const struct element *next_mbssid;
2118         const struct element *next_sub;
2119
2120         next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2121                                          mbssid_end,
2122                                          ielen - (mbssid_end - ie));
2123
2124         /*
2125          * If it is not the last subelement in current MBSSID IE or there isn't
2126          * a next MBSSID IE - profile is complete.
2127         */
2128         if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
2129             !next_mbssid)
2130                 return NULL;
2131
2132         /* For any length error, just return NULL */
2133
2134         if (next_mbssid->datalen < 4)
2135                 return NULL;
2136
2137         next_sub = (void *)&next_mbssid->data[1];
2138
2139         if (next_mbssid->data + next_mbssid->datalen <
2140             next_sub->data + next_sub->datalen)
2141                 return NULL;
2142
2143         if (next_sub->id != 0 || next_sub->datalen < 2)
2144                 return NULL;
2145
2146         /*
2147          * Check if the first element in the next sub element is a start
2148          * of a new profile
2149          */
2150         return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
2151                NULL : next_mbssid;
2152 }
2153
2154 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2155                               const struct element *mbssid_elem,
2156                               const struct element *sub_elem,
2157                               u8 *merged_ie, size_t max_copy_len)
2158 {
2159         size_t copied_len = sub_elem->datalen;
2160         const struct element *next_mbssid;
2161
2162         if (sub_elem->datalen > max_copy_len)
2163                 return 0;
2164
2165         memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2166
2167         while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
2168                                                                 mbssid_elem,
2169                                                                 sub_elem))) {
2170                 const struct element *next_sub = (void *)&next_mbssid->data[1];
2171
2172                 if (copied_len + next_sub->datalen > max_copy_len)
2173                         break;
2174                 memcpy(merged_ie + copied_len, next_sub->data,
2175                        next_sub->datalen);
2176                 copied_len += next_sub->datalen;
2177         }
2178
2179         return copied_len;
2180 }
2181 EXPORT_SYMBOL(cfg80211_merge_profile);
2182
2183 static void
2184 cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2185                            struct cfg80211_inform_single_bss_data *tx_data,
2186                            struct cfg80211_bss *source_bss,
2187                            gfp_t gfp)
2188 {
2189         struct cfg80211_inform_single_bss_data data = {
2190                 .drv_data = tx_data->drv_data,
2191                 .ftype = tx_data->ftype,
2192                 .tsf = tx_data->tsf,
2193                 .beacon_interval = tx_data->beacon_interval,
2194                 .source_bss = source_bss,
2195         };
2196         const u8 *mbssid_index_ie;
2197         const struct element *elem, *sub;
2198         u8 *new_ie, *profile;
2199         u64 seen_indices = 0;
2200         struct cfg80211_bss *bss;
2201
2202         if (!source_bss)
2203                 return;
2204         if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2205                                 tx_data->ie, tx_data->ielen))
2206                 return;
2207         if (!wiphy->support_mbssid)
2208                 return;
2209         if (wiphy->support_only_he_mbssid &&
2210             !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
2211                                     tx_data->ie, tx_data->ielen))
2212                 return;
2213
2214         new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2215         if (!new_ie)
2216                 return;
2217
2218         profile = kmalloc(tx_data->ielen, gfp);
2219         if (!profile)
2220                 goto out;
2221
2222         for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID,
2223                             tx_data->ie, tx_data->ielen) {
2224                 if (elem->datalen < 4)
2225                         continue;
2226                 if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2227                         continue;
2228                 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2229                         u8 profile_len;
2230
2231                         if (sub->id != 0 || sub->datalen < 4) {
2232                                 /* not a valid BSS profile */
2233                                 continue;
2234                         }
2235
2236                         if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2237                             sub->data[1] != 2) {
2238                                 /* The first element within the Nontransmitted
2239                                  * BSSID Profile is not the Nontransmitted
2240                                  * BSSID Capability element.
2241                                  */
2242                                 continue;
2243                         }
2244
2245                         memset(profile, 0, tx_data->ielen);
2246                         profile_len = cfg80211_merge_profile(tx_data->ie,
2247                                                              tx_data->ielen,
2248                                                              elem,
2249                                                              sub,
2250                                                              profile,
2251                                                              tx_data->ielen);
2252
2253                         /* found a Nontransmitted BSSID Profile */
2254                         mbssid_index_ie = cfg80211_find_ie
2255                                 (WLAN_EID_MULTI_BSSID_IDX,
2256                                  profile, profile_len);
2257                         if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2258                             mbssid_index_ie[2] == 0 ||
2259                             mbssid_index_ie[2] > 46) {
2260                                 /* No valid Multiple BSSID-Index element */
2261                                 continue;
2262                         }
2263
2264                         if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2265                                 /* We don't support legacy split of a profile */
2266                                 net_dbg_ratelimited("Partial info for BSSID index %d\n",
2267                                                     mbssid_index_ie[2]);
2268
2269                         seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2270
2271                         data.bssid_index = mbssid_index_ie[2];
2272                         data.max_bssid_indicator = elem->data[0];
2273
2274                         cfg80211_gen_new_bssid(tx_data->bssid,
2275                                                data.max_bssid_indicator,
2276                                                data.bssid_index,
2277                                                data.bssid);
2278
2279                         memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2280                         data.ie = new_ie;
2281                         data.ielen = cfg80211_gen_new_ie(tx_data->ie,
2282                                                          tx_data->ielen,
2283                                                          profile,
2284                                                          profile_len,
2285                                                          new_ie,
2286                                                          IEEE80211_MAX_DATA_LEN);
2287                         if (!data.ielen)
2288                                 continue;
2289
2290                         data.capability = get_unaligned_le16(profile + 2);
2291                         bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp);
2292                         if (!bss)
2293                                 break;
2294                         cfg80211_put_bss(wiphy, bss);
2295                 }
2296         }
2297
2298 out:
2299         kfree(new_ie);
2300         kfree(profile);
2301 }
2302
2303 ssize_t cfg80211_defragment_element(const struct element *elem, const u8 *ies,
2304                                     size_t ieslen, u8 *data, size_t data_len,
2305                                     u8 frag_id)
2306 {
2307         const struct element *next;
2308         ssize_t copied;
2309         u8 elem_datalen;
2310
2311         if (!elem)
2312                 return -EINVAL;
2313
2314         /* elem might be invalid after the memmove */
2315         next = (void *)(elem->data + elem->datalen);
2316
2317         elem_datalen = elem->datalen;
2318         if (elem->id == WLAN_EID_EXTENSION) {
2319                 copied = elem->datalen - 1;
2320                 if (copied > data_len)
2321                         return -ENOSPC;
2322
2323                 memmove(data, elem->data + 1, copied);
2324         } else {
2325                 copied = elem->datalen;
2326                 if (copied > data_len)
2327                         return -ENOSPC;
2328
2329                 memmove(data, elem->data, copied);
2330         }
2331
2332         /* Fragmented elements must have 255 bytes */
2333         if (elem_datalen < 255)
2334                 return copied;
2335
2336         for (elem = next;
2337              elem->data < ies + ieslen &&
2338                 elem->data + elem->datalen < ies + ieslen;
2339              elem = next) {
2340                 /* elem might be invalid after the memmove */
2341                 next = (void *)(elem->data + elem->datalen);
2342
2343                 if (elem->id != frag_id)
2344                         break;
2345
2346                 elem_datalen = elem->datalen;
2347
2348                 if (copied + elem_datalen > data_len)
2349                         return -ENOSPC;
2350
2351                 memmove(data + copied, elem->data, elem_datalen);
2352                 copied += elem_datalen;
2353
2354                 /* Only the last fragment may be short */
2355                 if (elem_datalen != 255)
2356                         break;
2357         }
2358
2359         return copied;
2360 }
2361 EXPORT_SYMBOL(cfg80211_defragment_element);
2362
2363 struct cfg80211_bss *
2364 cfg80211_inform_bss_data(struct wiphy *wiphy,
2365                          struct cfg80211_inform_bss *data,
2366                          enum cfg80211_bss_frame_type ftype,
2367                          const u8 *bssid, u64 tsf, u16 capability,
2368                          u16 beacon_interval, const u8 *ie, size_t ielen,
2369                          gfp_t gfp)
2370 {
2371         struct cfg80211_inform_single_bss_data inform_data = {
2372                 .drv_data = data,
2373                 .ftype = ftype,
2374                 .tsf = tsf,
2375                 .capability = capability,
2376                 .beacon_interval = beacon_interval,
2377                 .ie = ie,
2378                 .ielen = ielen,
2379         };
2380         struct cfg80211_bss *res;
2381
2382         memcpy(inform_data.bssid, bssid, ETH_ALEN);
2383
2384         res = cfg80211_inform_single_bss_data(wiphy, &inform_data, gfp);
2385         if (!res)
2386                 return NULL;
2387
2388         cfg80211_parse_mbssid_data(wiphy, &inform_data, res, gfp);
2389         return res;
2390 }
2391 EXPORT_SYMBOL(cfg80211_inform_bss_data);
2392
2393 /* cfg80211_inform_bss_width_frame helper */
2394 static struct cfg80211_bss *
2395 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
2396                                       struct cfg80211_inform_bss *data,
2397                                       struct ieee80211_mgmt *mgmt, size_t len,
2398                                       gfp_t gfp)
2399 {
2400         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2401         struct cfg80211_internal_bss tmp = {}, *res;
2402         struct cfg80211_bss_ies *ies;
2403         struct ieee80211_channel *channel;
2404         bool signal_valid;
2405         struct ieee80211_ext *ext = NULL;
2406         u8 *bssid, *variable;
2407         u16 capability, beacon_int;
2408         size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
2409                                              u.probe_resp.variable);
2410         int bss_type;
2411
2412         BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
2413                         offsetof(struct ieee80211_mgmt, u.beacon.variable));
2414
2415         trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
2416
2417         if (WARN_ON(!mgmt))
2418                 return NULL;
2419
2420         if (WARN_ON(!wiphy))
2421                 return NULL;
2422
2423         if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2424                     (data->signal < 0 || data->signal > 100)))
2425                 return NULL;
2426
2427         if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2428                 ext = (void *) mgmt;
2429                 min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
2430                 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2431                         min_hdr_len = offsetof(struct ieee80211_ext,
2432                                                u.s1g_short_beacon.variable);
2433         }
2434
2435         if (WARN_ON(len < min_hdr_len))
2436                 return NULL;
2437
2438         ielen = len - min_hdr_len;
2439         variable = mgmt->u.probe_resp.variable;
2440         if (ext) {
2441                 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2442                         variable = ext->u.s1g_short_beacon.variable;
2443                 else
2444                         variable = ext->u.s1g_beacon.variable;
2445         }
2446
2447         channel = cfg80211_get_bss_channel(wiphy, variable,
2448                                            ielen, data->chan, data->scan_width);
2449         if (!channel)
2450                 return NULL;
2451
2452         if (ext) {
2453                 const struct ieee80211_s1g_bcn_compat_ie *compat;
2454                 const struct element *elem;
2455
2456                 elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
2457                                           variable, ielen);
2458                 if (!elem)
2459                         return NULL;
2460                 if (elem->datalen < sizeof(*compat))
2461                         return NULL;
2462                 compat = (void *)elem->data;
2463                 bssid = ext->u.s1g_beacon.sa;
2464                 capability = le16_to_cpu(compat->compat_info);
2465                 beacon_int = le16_to_cpu(compat->beacon_int);
2466         } else {
2467                 bssid = mgmt->bssid;
2468                 beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2469                 capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
2470         }
2471
2472         if (channel->band == NL80211_BAND_60GHZ) {
2473                 bss_type = capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2474                 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2475                     bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2476                         regulatory_hint_found_beacon(wiphy, channel, gfp);
2477         } else {
2478                 if (capability & WLAN_CAPABILITY_ESS)
2479                         regulatory_hint_found_beacon(wiphy, channel, gfp);
2480         }
2481
2482         ies = kzalloc(sizeof(*ies) + ielen, gfp);
2483         if (!ies)
2484                 return NULL;
2485         ies->len = ielen;
2486         ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2487         ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
2488                            ieee80211_is_s1g_beacon(mgmt->frame_control);
2489         memcpy(ies->data, variable, ielen);
2490
2491         if (ieee80211_is_probe_resp(mgmt->frame_control))
2492                 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2493         else
2494                 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2495         rcu_assign_pointer(tmp.pub.ies, ies);
2496
2497         memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2498         tmp.pub.beacon_interval = beacon_int;
2499         tmp.pub.capability = capability;
2500         tmp.pub.channel = channel;
2501         tmp.pub.scan_width = data->scan_width;
2502         tmp.pub.signal = data->signal;
2503         tmp.ts_boottime = data->boottime_ns;
2504         tmp.parent_tsf = data->parent_tsf;
2505         tmp.pub.chains = data->chains;
2506         memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
2507         ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2508
2509         signal_valid = data->chan == channel;
2510         spin_lock_bh(&rdev->bss_lock);
2511         res = __cfg80211_bss_update(rdev, &tmp, signal_valid, jiffies);
2512         if (!res)
2513                 goto drop;
2514
2515         rdev_inform_bss(rdev, &res->pub, ies, data->drv_data);
2516
2517         spin_unlock_bh(&rdev->bss_lock);
2518
2519         trace_cfg80211_return_bss(&res->pub);
2520         /* __cfg80211_bss_update gives us a referenced result */
2521         return &res->pub;
2522
2523 drop:
2524         spin_unlock_bh(&rdev->bss_lock);
2525         return NULL;
2526 }
2527
2528 struct cfg80211_bss *
2529 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2530                                struct cfg80211_inform_bss *data,
2531                                struct ieee80211_mgmt *mgmt, size_t len,
2532                                gfp_t gfp)
2533 {
2534         struct cfg80211_inform_single_bss_data inform_data = {
2535                 .drv_data = data,
2536                 .ie = mgmt->u.probe_resp.variable,
2537                 .ielen = len - offsetof(struct ieee80211_mgmt,
2538                                         u.probe_resp.variable),
2539         };
2540         struct cfg80211_bss *res;
2541
2542         res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
2543                                                     len, gfp);
2544         if (!res)
2545                 return NULL;
2546
2547         /* don't do any further MBSSID handling for S1G */
2548         if (ieee80211_is_s1g_beacon(mgmt->frame_control))
2549                 return res;
2550
2551         inform_data.ftype = ieee80211_is_beacon(mgmt->frame_control) ?
2552                 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
2553         memcpy(inform_data.bssid, mgmt->bssid, ETH_ALEN);
2554         inform_data.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2555         inform_data.beacon_interval =
2556                 le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2557
2558         /* process each non-transmitting bss */
2559         cfg80211_parse_mbssid_data(wiphy, &inform_data, res, gfp);
2560
2561         return res;
2562 }
2563 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2564
2565 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2566 {
2567         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2568
2569         if (!pub)
2570                 return;
2571
2572         spin_lock_bh(&rdev->bss_lock);
2573         bss_ref_get(rdev, bss_from_pub(pub));
2574         spin_unlock_bh(&rdev->bss_lock);
2575 }
2576 EXPORT_SYMBOL(cfg80211_ref_bss);
2577
2578 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2579 {
2580         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2581
2582         if (!pub)
2583                 return;
2584
2585         spin_lock_bh(&rdev->bss_lock);
2586         bss_ref_put(rdev, bss_from_pub(pub));
2587         spin_unlock_bh(&rdev->bss_lock);
2588 }
2589 EXPORT_SYMBOL(cfg80211_put_bss);
2590
2591 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2592 {
2593         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2594         struct cfg80211_internal_bss *bss, *tmp1;
2595         struct cfg80211_bss *nontrans_bss, *tmp;
2596
2597         if (WARN_ON(!pub))
2598                 return;
2599
2600         bss = bss_from_pub(pub);
2601
2602         spin_lock_bh(&rdev->bss_lock);
2603         if (list_empty(&bss->list))
2604                 goto out;
2605
2606         list_for_each_entry_safe(nontrans_bss, tmp,
2607                                  &pub->nontrans_list,
2608                                  nontrans_list) {
2609                 tmp1 = bss_from_pub(nontrans_bss);
2610                 if (__cfg80211_unlink_bss(rdev, tmp1))
2611                         rdev->bss_generation++;
2612         }
2613
2614         if (__cfg80211_unlink_bss(rdev, bss))
2615                 rdev->bss_generation++;
2616 out:
2617         spin_unlock_bh(&rdev->bss_lock);
2618 }
2619 EXPORT_SYMBOL(cfg80211_unlink_bss);
2620
2621 void cfg80211_bss_iter(struct wiphy *wiphy,
2622                        struct cfg80211_chan_def *chandef,
2623                        void (*iter)(struct wiphy *wiphy,
2624                                     struct cfg80211_bss *bss,
2625                                     void *data),
2626                        void *iter_data)
2627 {
2628         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2629         struct cfg80211_internal_bss *bss;
2630
2631         spin_lock_bh(&rdev->bss_lock);
2632
2633         list_for_each_entry(bss, &rdev->bss_list, list) {
2634                 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
2635                                                      false))
2636                         iter(wiphy, &bss->pub, iter_data);
2637         }
2638
2639         spin_unlock_bh(&rdev->bss_lock);
2640 }
2641 EXPORT_SYMBOL(cfg80211_bss_iter);
2642
2643 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2644                                      unsigned int link_id,
2645                                      struct ieee80211_channel *chan)
2646 {
2647         struct wiphy *wiphy = wdev->wiphy;
2648         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2649         struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
2650         struct cfg80211_internal_bss *new = NULL;
2651         struct cfg80211_internal_bss *bss;
2652         struct cfg80211_bss *nontrans_bss;
2653         struct cfg80211_bss *tmp;
2654
2655         spin_lock_bh(&rdev->bss_lock);
2656
2657         /*
2658          * Some APs use CSA also for bandwidth changes, i.e., without actually
2659          * changing the control channel, so no need to update in such a case.
2660          */
2661         if (cbss->pub.channel == chan)
2662                 goto done;
2663
2664         /* use transmitting bss */
2665         if (cbss->pub.transmitted_bss)
2666                 cbss = bss_from_pub(cbss->pub.transmitted_bss);
2667
2668         cbss->pub.channel = chan;
2669
2670         list_for_each_entry(bss, &rdev->bss_list, list) {
2671                 if (!cfg80211_bss_type_match(bss->pub.capability,
2672                                              bss->pub.channel->band,
2673                                              wdev->conn_bss_type))
2674                         continue;
2675
2676                 if (bss == cbss)
2677                         continue;
2678
2679                 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2680                         new = bss;
2681                         break;
2682                 }
2683         }
2684
2685         if (new) {
2686                 /* to save time, update IEs for transmitting bss only */
2687                 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2688                         new->pub.proberesp_ies = NULL;
2689                         new->pub.beacon_ies = NULL;
2690                 }
2691
2692                 list_for_each_entry_safe(nontrans_bss, tmp,
2693                                          &new->pub.nontrans_list,
2694                                          nontrans_list) {
2695                         bss = bss_from_pub(nontrans_bss);
2696                         if (__cfg80211_unlink_bss(rdev, bss))
2697                                 rdev->bss_generation++;
2698                 }
2699
2700                 WARN_ON(atomic_read(&new->hold));
2701                 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2702                         rdev->bss_generation++;
2703         }
2704
2705         rb_erase(&cbss->rbn, &rdev->bss_tree);
2706         rb_insert_bss(rdev, cbss);
2707         rdev->bss_generation++;
2708
2709         list_for_each_entry_safe(nontrans_bss, tmp,
2710                                  &cbss->pub.nontrans_list,
2711                                  nontrans_list) {
2712                 bss = bss_from_pub(nontrans_bss);
2713                 bss->pub.channel = chan;
2714                 rb_erase(&bss->rbn, &rdev->bss_tree);
2715                 rb_insert_bss(rdev, bss);
2716                 rdev->bss_generation++;
2717         }
2718
2719 done:
2720         spin_unlock_bh(&rdev->bss_lock);
2721 }
2722
2723 #ifdef CONFIG_CFG80211_WEXT
2724 static struct cfg80211_registered_device *
2725 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2726 {
2727         struct cfg80211_registered_device *rdev;
2728         struct net_device *dev;
2729
2730         ASSERT_RTNL();
2731
2732         dev = dev_get_by_index(net, ifindex);
2733         if (!dev)
2734                 return ERR_PTR(-ENODEV);
2735         if (dev->ieee80211_ptr)
2736                 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2737         else
2738                 rdev = ERR_PTR(-ENODEV);
2739         dev_put(dev);
2740         return rdev;
2741 }
2742
2743 int cfg80211_wext_siwscan(struct net_device *dev,
2744                           struct iw_request_info *info,
2745                           union iwreq_data *wrqu, char *extra)
2746 {
2747         struct cfg80211_registered_device *rdev;
2748         struct wiphy *wiphy;
2749         struct iw_scan_req *wreq = NULL;
2750         struct cfg80211_scan_request *creq;
2751         int i, err, n_channels = 0;
2752         enum nl80211_band band;
2753
2754         if (!netif_running(dev))
2755                 return -ENETDOWN;
2756
2757         if (wrqu->data.length == sizeof(struct iw_scan_req))
2758                 wreq = (struct iw_scan_req *)extra;
2759
2760         rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2761
2762         if (IS_ERR(rdev))
2763                 return PTR_ERR(rdev);
2764
2765         if (rdev->scan_req || rdev->scan_msg)
2766                 return -EBUSY;
2767
2768         wiphy = &rdev->wiphy;
2769
2770         /* Determine number of channels, needed to allocate creq */
2771         if (wreq && wreq->num_channels)
2772                 n_channels = wreq->num_channels;
2773         else
2774                 n_channels = ieee80211_get_num_supported_channels(wiphy);
2775
2776         creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2777                        n_channels * sizeof(void *),
2778                        GFP_ATOMIC);
2779         if (!creq)
2780                 return -ENOMEM;
2781
2782         creq->wiphy = wiphy;
2783         creq->wdev = dev->ieee80211_ptr;
2784         /* SSIDs come after channels */
2785         creq->ssids = (void *)&creq->channels[n_channels];
2786         creq->n_channels = n_channels;
2787         creq->n_ssids = 1;
2788         creq->scan_start = jiffies;
2789
2790         /* translate "Scan on frequencies" request */
2791         i = 0;
2792         for (band = 0; band < NUM_NL80211_BANDS; band++) {
2793                 int j;
2794
2795                 if (!wiphy->bands[band])
2796                         continue;
2797
2798                 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2799                         /* ignore disabled channels */
2800                         if (wiphy->bands[band]->channels[j].flags &
2801                                                 IEEE80211_CHAN_DISABLED)
2802                                 continue;
2803
2804                         /* If we have a wireless request structure and the
2805                          * wireless request specifies frequencies, then search
2806                          * for the matching hardware channel.
2807                          */
2808                         if (wreq && wreq->num_channels) {
2809                                 int k;
2810                                 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2811                                 for (k = 0; k < wreq->num_channels; k++) {
2812                                         struct iw_freq *freq =
2813                                                 &wreq->channel_list[k];
2814                                         int wext_freq =
2815                                                 cfg80211_wext_freq(freq);
2816
2817                                         if (wext_freq == wiphy_freq)
2818                                                 goto wext_freq_found;
2819                                 }
2820                                 goto wext_freq_not_found;
2821                         }
2822
2823                 wext_freq_found:
2824                         creq->channels[i] = &wiphy->bands[band]->channels[j];
2825                         i++;
2826                 wext_freq_not_found: ;
2827                 }
2828         }
2829         /* No channels found? */
2830         if (!i) {
2831                 err = -EINVAL;
2832                 goto out;
2833         }
2834
2835         /* Set real number of channels specified in creq->channels[] */
2836         creq->n_channels = i;
2837
2838         /* translate "Scan for SSID" request */
2839         if (wreq) {
2840                 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2841                         if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2842                                 err = -EINVAL;
2843                                 goto out;
2844                         }
2845                         memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2846                         creq->ssids[0].ssid_len = wreq->essid_len;
2847                 }
2848                 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2849                         creq->n_ssids = 0;
2850         }
2851
2852         for (i = 0; i < NUM_NL80211_BANDS; i++)
2853                 if (wiphy->bands[i])
2854                         creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2855
2856         eth_broadcast_addr(creq->bssid);
2857
2858         wiphy_lock(&rdev->wiphy);
2859
2860         rdev->scan_req = creq;
2861         err = rdev_scan(rdev, creq);
2862         if (err) {
2863                 rdev->scan_req = NULL;
2864                 /* creq will be freed below */
2865         } else {
2866                 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2867                 /* creq now owned by driver */
2868                 creq = NULL;
2869                 dev_hold(dev);
2870         }
2871         wiphy_unlock(&rdev->wiphy);
2872  out:
2873         kfree(creq);
2874         return err;
2875 }
2876 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2877
2878 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2879                                     const struct cfg80211_bss_ies *ies,
2880                                     char *current_ev, char *end_buf)
2881 {
2882         const u8 *pos, *end, *next;
2883         struct iw_event iwe;
2884
2885         if (!ies)
2886                 return current_ev;
2887
2888         /*
2889          * If needed, fragment the IEs buffer (at IE boundaries) into short
2890          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2891          */
2892         pos = ies->data;
2893         end = pos + ies->len;
2894
2895         while (end - pos > IW_GENERIC_IE_MAX) {
2896                 next = pos + 2 + pos[1];
2897                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2898                         next = next + 2 + next[1];
2899
2900                 memset(&iwe, 0, sizeof(iwe));
2901                 iwe.cmd = IWEVGENIE;
2902                 iwe.u.data.length = next - pos;
2903                 current_ev = iwe_stream_add_point_check(info, current_ev,
2904                                                         end_buf, &iwe,
2905                                                         (void *)pos);
2906                 if (IS_ERR(current_ev))
2907                         return current_ev;
2908                 pos = next;
2909         }
2910
2911         if (end > pos) {
2912                 memset(&iwe, 0, sizeof(iwe));
2913                 iwe.cmd = IWEVGENIE;
2914                 iwe.u.data.length = end - pos;
2915                 current_ev = iwe_stream_add_point_check(info, current_ev,
2916                                                         end_buf, &iwe,
2917                                                         (void *)pos);
2918                 if (IS_ERR(current_ev))
2919                         return current_ev;
2920         }
2921
2922         return current_ev;
2923 }
2924
2925 static char *
2926 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2927               struct cfg80211_internal_bss *bss, char *current_ev,
2928               char *end_buf)
2929 {
2930         const struct cfg80211_bss_ies *ies;
2931         struct iw_event iwe;
2932         const u8 *ie;
2933         u8 buf[50];
2934         u8 *cfg, *p, *tmp;
2935         int rem, i, sig;
2936         bool ismesh = false;
2937
2938         memset(&iwe, 0, sizeof(iwe));
2939         iwe.cmd = SIOCGIWAP;
2940         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2941         memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2942         current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2943                                                 IW_EV_ADDR_LEN);
2944         if (IS_ERR(current_ev))
2945                 return current_ev;
2946
2947         memset(&iwe, 0, sizeof(iwe));
2948         iwe.cmd = SIOCGIWFREQ;
2949         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2950         iwe.u.freq.e = 0;
2951         current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2952                                                 IW_EV_FREQ_LEN);
2953         if (IS_ERR(current_ev))
2954                 return current_ev;
2955
2956         memset(&iwe, 0, sizeof(iwe));
2957         iwe.cmd = SIOCGIWFREQ;
2958         iwe.u.freq.m = bss->pub.channel->center_freq;
2959         iwe.u.freq.e = 6;
2960         current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2961                                                 IW_EV_FREQ_LEN);
2962         if (IS_ERR(current_ev))
2963                 return current_ev;
2964
2965         if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2966                 memset(&iwe, 0, sizeof(iwe));
2967                 iwe.cmd = IWEVQUAL;
2968                 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2969                                      IW_QUAL_NOISE_INVALID |
2970                                      IW_QUAL_QUAL_UPDATED;
2971                 switch (wiphy->signal_type) {
2972                 case CFG80211_SIGNAL_TYPE_MBM:
2973                         sig = bss->pub.signal / 100;
2974                         iwe.u.qual.level = sig;
2975                         iwe.u.qual.updated |= IW_QUAL_DBM;
2976                         if (sig < -110)         /* rather bad */
2977                                 sig = -110;
2978                         else if (sig > -40)     /* perfect */
2979                                 sig = -40;
2980                         /* will give a range of 0 .. 70 */
2981                         iwe.u.qual.qual = sig + 110;
2982                         break;
2983                 case CFG80211_SIGNAL_TYPE_UNSPEC:
2984                         iwe.u.qual.level = bss->pub.signal;
2985                         /* will give range 0 .. 100 */
2986                         iwe.u.qual.qual = bss->pub.signal;
2987                         break;
2988                 default:
2989                         /* not reached */
2990                         break;
2991                 }
2992                 current_ev = iwe_stream_add_event_check(info, current_ev,
2993                                                         end_buf, &iwe,
2994                                                         IW_EV_QUAL_LEN);
2995                 if (IS_ERR(current_ev))
2996                         return current_ev;
2997         }
2998
2999         memset(&iwe, 0, sizeof(iwe));
3000         iwe.cmd = SIOCGIWENCODE;
3001         if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
3002                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3003         else
3004                 iwe.u.data.flags = IW_ENCODE_DISABLED;
3005         iwe.u.data.length = 0;
3006         current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3007                                                 &iwe, "");
3008         if (IS_ERR(current_ev))
3009                 return current_ev;
3010
3011         rcu_read_lock();
3012         ies = rcu_dereference(bss->pub.ies);
3013         rem = ies->len;
3014         ie = ies->data;
3015
3016         while (rem >= 2) {
3017                 /* invalid data */
3018                 if (ie[1] > rem - 2)
3019                         break;
3020
3021                 switch (ie[0]) {
3022                 case WLAN_EID_SSID:
3023                         memset(&iwe, 0, sizeof(iwe));
3024                         iwe.cmd = SIOCGIWESSID;
3025                         iwe.u.data.length = ie[1];
3026                         iwe.u.data.flags = 1;
3027                         current_ev = iwe_stream_add_point_check(info,
3028                                                                 current_ev,
3029                                                                 end_buf, &iwe,
3030                                                                 (u8 *)ie + 2);
3031                         if (IS_ERR(current_ev))
3032                                 goto unlock;
3033                         break;
3034                 case WLAN_EID_MESH_ID:
3035                         memset(&iwe, 0, sizeof(iwe));
3036                         iwe.cmd = SIOCGIWESSID;
3037                         iwe.u.data.length = ie[1];
3038                         iwe.u.data.flags = 1;
3039                         current_ev = iwe_stream_add_point_check(info,
3040                                                                 current_ev,
3041                                                                 end_buf, &iwe,
3042                                                                 (u8 *)ie + 2);
3043                         if (IS_ERR(current_ev))
3044                                 goto unlock;
3045                         break;
3046                 case WLAN_EID_MESH_CONFIG:
3047                         ismesh = true;
3048                         if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
3049                                 break;
3050                         cfg = (u8 *)ie + 2;
3051                         memset(&iwe, 0, sizeof(iwe));
3052                         iwe.cmd = IWEVCUSTOM;
3053                         sprintf(buf, "Mesh Network Path Selection Protocol ID: "
3054                                 "0x%02X", cfg[0]);
3055                         iwe.u.data.length = strlen(buf);
3056                         current_ev = iwe_stream_add_point_check(info,
3057                                                                 current_ev,
3058                                                                 end_buf,
3059                                                                 &iwe, buf);
3060                         if (IS_ERR(current_ev))
3061                                 goto unlock;
3062                         sprintf(buf, "Path Selection Metric ID: 0x%02X",
3063                                 cfg[1]);
3064                         iwe.u.data.length = strlen(buf);
3065                         current_ev = iwe_stream_add_point_check(info,
3066                                                                 current_ev,
3067                                                                 end_buf,
3068                                                                 &iwe, buf);
3069                         if (IS_ERR(current_ev))
3070                                 goto unlock;
3071                         sprintf(buf, "Congestion Control Mode ID: 0x%02X",
3072                                 cfg[2]);
3073                         iwe.u.data.length = strlen(buf);
3074                         current_ev = iwe_stream_add_point_check(info,
3075                                                                 current_ev,
3076                                                                 end_buf,
3077                                                                 &iwe, buf);
3078                         if (IS_ERR(current_ev))
3079                                 goto unlock;
3080                         sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
3081                         iwe.u.data.length = strlen(buf);
3082                         current_ev = iwe_stream_add_point_check(info,
3083                                                                 current_ev,
3084                                                                 end_buf,
3085                                                                 &iwe, buf);
3086                         if (IS_ERR(current_ev))
3087                                 goto unlock;
3088                         sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
3089                         iwe.u.data.length = strlen(buf);
3090                         current_ev = iwe_stream_add_point_check(info,
3091                                                                 current_ev,
3092                                                                 end_buf,
3093                                                                 &iwe, buf);
3094                         if (IS_ERR(current_ev))
3095                                 goto unlock;
3096                         sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
3097                         iwe.u.data.length = strlen(buf);
3098                         current_ev = iwe_stream_add_point_check(info,
3099                                                                 current_ev,
3100                                                                 end_buf,
3101                                                                 &iwe, buf);
3102                         if (IS_ERR(current_ev))
3103                                 goto unlock;
3104                         sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
3105                         iwe.u.data.length = strlen(buf);
3106                         current_ev = iwe_stream_add_point_check(info,
3107                                                                 current_ev,
3108                                                                 end_buf,
3109                                                                 &iwe, buf);
3110                         if (IS_ERR(current_ev))
3111                                 goto unlock;
3112                         break;
3113                 case WLAN_EID_SUPP_RATES:
3114                 case WLAN_EID_EXT_SUPP_RATES:
3115                         /* display all supported rates in readable format */
3116                         p = current_ev + iwe_stream_lcp_len(info);
3117
3118                         memset(&iwe, 0, sizeof(iwe));
3119                         iwe.cmd = SIOCGIWRATE;
3120                         /* Those two flags are ignored... */
3121                         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3122
3123                         for (i = 0; i < ie[1]; i++) {
3124                                 iwe.u.bitrate.value =
3125                                         ((ie[i + 2] & 0x7f) * 500000);
3126                                 tmp = p;
3127                                 p = iwe_stream_add_value(info, current_ev, p,
3128                                                          end_buf, &iwe,
3129                                                          IW_EV_PARAM_LEN);
3130                                 if (p == tmp) {
3131                                         current_ev = ERR_PTR(-E2BIG);
3132                                         goto unlock;
3133                                 }
3134                         }
3135                         current_ev = p;
3136                         break;
3137                 }
3138                 rem -= ie[1] + 2;
3139                 ie += ie[1] + 2;
3140         }
3141
3142         if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3143             ismesh) {
3144                 memset(&iwe, 0, sizeof(iwe));
3145                 iwe.cmd = SIOCGIWMODE;
3146                 if (ismesh)
3147                         iwe.u.mode = IW_MODE_MESH;
3148                 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3149                         iwe.u.mode = IW_MODE_MASTER;
3150                 else
3151                         iwe.u.mode = IW_MODE_ADHOC;
3152                 current_ev = iwe_stream_add_event_check(info, current_ev,
3153                                                         end_buf, &iwe,
3154                                                         IW_EV_UINT_LEN);
3155                 if (IS_ERR(current_ev))
3156                         goto unlock;
3157         }
3158
3159         memset(&iwe, 0, sizeof(iwe));
3160         iwe.cmd = IWEVCUSTOM;
3161         sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
3162         iwe.u.data.length = strlen(buf);
3163         current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3164                                                 &iwe, buf);
3165         if (IS_ERR(current_ev))
3166                 goto unlock;
3167         memset(&iwe, 0, sizeof(iwe));
3168         iwe.cmd = IWEVCUSTOM;
3169         sprintf(buf, " Last beacon: %ums ago",
3170                 elapsed_jiffies_msecs(bss->ts));
3171         iwe.u.data.length = strlen(buf);
3172         current_ev = iwe_stream_add_point_check(info, current_ev,
3173                                                 end_buf, &iwe, buf);
3174         if (IS_ERR(current_ev))
3175                 goto unlock;
3176
3177         current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3178
3179  unlock:
3180         rcu_read_unlock();
3181         return current_ev;
3182 }
3183
3184
3185 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3186                                   struct iw_request_info *info,
3187                                   char *buf, size_t len)
3188 {
3189         char *current_ev = buf;
3190         char *end_buf = buf + len;
3191         struct cfg80211_internal_bss *bss;
3192         int err = 0;
3193
3194         spin_lock_bh(&rdev->bss_lock);
3195         cfg80211_bss_expire(rdev);
3196
3197         list_for_each_entry(bss, &rdev->bss_list, list) {
3198                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3199                         err = -E2BIG;
3200                         break;
3201                 }
3202                 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
3203                                            current_ev, end_buf);
3204                 if (IS_ERR(current_ev)) {
3205                         err = PTR_ERR(current_ev);
3206                         break;
3207                 }
3208         }
3209         spin_unlock_bh(&rdev->bss_lock);
3210
3211         if (err)
3212                 return err;
3213         return current_ev - buf;
3214 }
3215
3216
3217 int cfg80211_wext_giwscan(struct net_device *dev,
3218                           struct iw_request_info *info,
3219                           union iwreq_data *wrqu, char *extra)
3220 {
3221         struct iw_point *data = &wrqu->data;
3222         struct cfg80211_registered_device *rdev;
3223         int res;
3224
3225         if (!netif_running(dev))
3226                 return -ENETDOWN;
3227
3228         rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3229
3230         if (IS_ERR(rdev))
3231                 return PTR_ERR(rdev);
3232
3233         if (rdev->scan_req || rdev->scan_msg)
3234                 return -EAGAIN;
3235
3236         res = ieee80211_scan_results(rdev, info, extra, data->length);
3237         data->length = 0;
3238         if (res >= 0) {
3239                 data->length = res;
3240                 res = 0;
3241         }
3242
3243         return res;
3244 }
3245 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
3246 #endif