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