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