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