]> Git Repo - linux.git/blob - net/wireless/scan.c
Merge wireless into wireless-next
[linux.git] / net / wireless / scan.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cfg80211 scan result handling
4  *
5  * Copyright 2008 Johannes Berg <[email protected]>
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 0;
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 (!is_broadcast_ether_addr(request->bssid) &&
912                     !ether_addr_equal(request->bssid, ap->bssid))
913                         continue;
914
915                 if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
916                         continue;
917
918                 cfg80211_scan_req_add_chan(request, chan, true);
919                 memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
920                 scan_6ghz_params->short_ssid = ap->short_ssid;
921                 scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
922                 scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
923                 scan_6ghz_params->psd_20 = ap->psd_20;
924
925                 /*
926                  * If a PSC channel is added to the scan and 'need_scan_psc' is
927                  * set to false, then all the APs that the scan logic is
928                  * interested with on the channel are collocated and thus there
929                  * is no need to perform the initial PSC channel listen.
930                  */
931                 if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
932                         scan_6ghz_params->psc_no_listen = true;
933
934                 request->n_6ghz_params++;
935         }
936
937 skip:
938         cfg80211_free_coloc_ap_list(&coloc_ap_list);
939
940         if (request->n_channels) {
941                 struct cfg80211_scan_request *old = rdev->int_scan_req;
942                 rdev->int_scan_req = request;
943
944                 /*
945                  * Add the ssids from the parent scan request to the new scan
946                  * request, so the driver would be able to use them in its
947                  * probe requests to discover hidden APs on PSC channels.
948                  */
949                 request->ssids = (void *)&request->channels[request->n_channels];
950                 request->n_ssids = rdev_req->n_ssids;
951                 memcpy(request->ssids, rdev_req->ssids, sizeof(*request->ssids) *
952                        request->n_ssids);
953
954                 /*
955                  * If this scan follows a previous scan, save the scan start
956                  * info from the first part of the scan
957                  */
958                 if (old)
959                         rdev->int_scan_req->info = old->info;
960
961                 err = rdev_scan(rdev, request);
962                 if (err) {
963                         rdev->int_scan_req = old;
964                         kfree(request);
965                 } else {
966                         kfree(old);
967                 }
968
969                 return err;
970         }
971
972         kfree(request);
973         return -EINVAL;
974 }
975
976 int cfg80211_scan(struct cfg80211_registered_device *rdev)
977 {
978         struct cfg80211_scan_request *request;
979         struct cfg80211_scan_request *rdev_req = rdev->scan_req;
980         u32 n_channels = 0, idx, i;
981
982         if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
983                 return rdev_scan(rdev, rdev_req);
984
985         for (i = 0; i < rdev_req->n_channels; i++) {
986                 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
987                         n_channels++;
988         }
989
990         if (!n_channels)
991                 return cfg80211_scan_6ghz(rdev);
992
993         request = kzalloc(struct_size(request, channels, n_channels),
994                           GFP_KERNEL);
995         if (!request)
996                 return -ENOMEM;
997
998         *request = *rdev_req;
999         request->n_channels = n_channels;
1000
1001         for (i = idx = 0; i < rdev_req->n_channels; i++) {
1002                 if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
1003                         request->channels[idx++] = rdev_req->channels[i];
1004         }
1005
1006         rdev_req->scan_6ghz = false;
1007         rdev->int_scan_req = request;
1008         return rdev_scan(rdev, request);
1009 }
1010
1011 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
1012                            bool send_message)
1013 {
1014         struct cfg80211_scan_request *request, *rdev_req;
1015         struct wireless_dev *wdev;
1016         struct sk_buff *msg;
1017 #ifdef CONFIG_CFG80211_WEXT
1018         union iwreq_data wrqu;
1019 #endif
1020
1021         lockdep_assert_held(&rdev->wiphy.mtx);
1022
1023         if (rdev->scan_msg) {
1024                 nl80211_send_scan_msg(rdev, rdev->scan_msg);
1025                 rdev->scan_msg = NULL;
1026                 return;
1027         }
1028
1029         rdev_req = rdev->scan_req;
1030         if (!rdev_req)
1031                 return;
1032
1033         wdev = rdev_req->wdev;
1034         request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
1035
1036         if (wdev_running(wdev) &&
1037             (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
1038             !rdev_req->scan_6ghz && !request->info.aborted &&
1039             !cfg80211_scan_6ghz(rdev))
1040                 return;
1041
1042         /*
1043          * This must be before sending the other events!
1044          * Otherwise, wpa_supplicant gets completely confused with
1045          * wext events.
1046          */
1047         if (wdev->netdev)
1048                 cfg80211_sme_scan_done(wdev->netdev);
1049
1050         if (!request->info.aborted &&
1051             request->flags & NL80211_SCAN_FLAG_FLUSH) {
1052                 /* flush entries from previous scans */
1053                 spin_lock_bh(&rdev->bss_lock);
1054                 __cfg80211_bss_expire(rdev, request->scan_start);
1055                 spin_unlock_bh(&rdev->bss_lock);
1056         }
1057
1058         msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
1059
1060 #ifdef CONFIG_CFG80211_WEXT
1061         if (wdev->netdev && !request->info.aborted) {
1062                 memset(&wrqu, 0, sizeof(wrqu));
1063
1064                 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
1065         }
1066 #endif
1067
1068         dev_put(wdev->netdev);
1069
1070         kfree(rdev->int_scan_req);
1071         rdev->int_scan_req = NULL;
1072
1073         kfree(rdev->scan_req);
1074         rdev->scan_req = NULL;
1075
1076         if (!send_message)
1077                 rdev->scan_msg = msg;
1078         else
1079                 nl80211_send_scan_msg(rdev, msg);
1080 }
1081
1082 void __cfg80211_scan_done(struct wiphy *wiphy, struct wiphy_work *wk)
1083 {
1084         ___cfg80211_scan_done(wiphy_to_rdev(wiphy), true);
1085 }
1086
1087 void cfg80211_scan_done(struct cfg80211_scan_request *request,
1088                         struct cfg80211_scan_info *info)
1089 {
1090         struct cfg80211_scan_info old_info = request->info;
1091
1092         trace_cfg80211_scan_done(request, info);
1093         WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
1094                 request != wiphy_to_rdev(request->wiphy)->int_scan_req);
1095
1096         request->info = *info;
1097
1098         /*
1099          * In case the scan is split, the scan_start_tsf and tsf_bssid should
1100          * be of the first part. In such a case old_info.scan_start_tsf should
1101          * be non zero.
1102          */
1103         if (request->scan_6ghz && old_info.scan_start_tsf) {
1104                 request->info.scan_start_tsf = old_info.scan_start_tsf;
1105                 memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
1106                        sizeof(request->info.tsf_bssid));
1107         }
1108
1109         request->notified = true;
1110         wiphy_work_queue(request->wiphy,
1111                          &wiphy_to_rdev(request->wiphy)->scan_done_wk);
1112 }
1113 EXPORT_SYMBOL(cfg80211_scan_done);
1114
1115 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
1116                                  struct cfg80211_sched_scan_request *req)
1117 {
1118         lockdep_assert_held(&rdev->wiphy.mtx);
1119
1120         list_add_rcu(&req->list, &rdev->sched_scan_req_list);
1121 }
1122
1123 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
1124                                         struct cfg80211_sched_scan_request *req)
1125 {
1126         lockdep_assert_held(&rdev->wiphy.mtx);
1127
1128         list_del_rcu(&req->list);
1129         kfree_rcu(req, rcu_head);
1130 }
1131
1132 static struct cfg80211_sched_scan_request *
1133 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
1134 {
1135         struct cfg80211_sched_scan_request *pos;
1136
1137         list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
1138                                 lockdep_is_held(&rdev->wiphy.mtx)) {
1139                 if (pos->reqid == reqid)
1140                         return pos;
1141         }
1142         return NULL;
1143 }
1144
1145 /*
1146  * Determines if a scheduled scan request can be handled. When a legacy
1147  * scheduled scan is running no other scheduled scan is allowed regardless
1148  * whether the request is for legacy or multi-support scan. When a multi-support
1149  * scheduled scan is running a request for legacy scan is not allowed. In this
1150  * case a request for multi-support scan can be handled if resources are
1151  * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
1152  */
1153 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
1154                                      bool want_multi)
1155 {
1156         struct cfg80211_sched_scan_request *pos;
1157         int i = 0;
1158
1159         list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
1160                 /* request id zero means legacy in progress */
1161                 if (!i && !pos->reqid)
1162                         return -EINPROGRESS;
1163                 i++;
1164         }
1165
1166         if (i) {
1167                 /* no legacy allowed when multi request(s) are active */
1168                 if (!want_multi)
1169                         return -EINPROGRESS;
1170
1171                 /* resource limit reached */
1172                 if (i == rdev->wiphy.max_sched_scan_reqs)
1173                         return -ENOSPC;
1174         }
1175         return 0;
1176 }
1177
1178 void cfg80211_sched_scan_results_wk(struct work_struct *work)
1179 {
1180         struct cfg80211_registered_device *rdev;
1181         struct cfg80211_sched_scan_request *req, *tmp;
1182
1183         rdev = container_of(work, struct cfg80211_registered_device,
1184                            sched_scan_res_wk);
1185
1186         wiphy_lock(&rdev->wiphy);
1187         list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
1188                 if (req->report_results) {
1189                         req->report_results = false;
1190                         if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
1191                                 /* flush entries from previous scans */
1192                                 spin_lock_bh(&rdev->bss_lock);
1193                                 __cfg80211_bss_expire(rdev, req->scan_start);
1194                                 spin_unlock_bh(&rdev->bss_lock);
1195                                 req->scan_start = jiffies;
1196                         }
1197                         nl80211_send_sched_scan(req,
1198                                                 NL80211_CMD_SCHED_SCAN_RESULTS);
1199                 }
1200         }
1201         wiphy_unlock(&rdev->wiphy);
1202 }
1203
1204 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
1205 {
1206         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1207         struct cfg80211_sched_scan_request *request;
1208
1209         trace_cfg80211_sched_scan_results(wiphy, reqid);
1210         /* ignore if we're not scanning */
1211
1212         rcu_read_lock();
1213         request = cfg80211_find_sched_scan_req(rdev, reqid);
1214         if (request) {
1215                 request->report_results = true;
1216                 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
1217         }
1218         rcu_read_unlock();
1219 }
1220 EXPORT_SYMBOL(cfg80211_sched_scan_results);
1221
1222 void cfg80211_sched_scan_stopped_locked(struct wiphy *wiphy, u64 reqid)
1223 {
1224         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1225
1226         lockdep_assert_held(&wiphy->mtx);
1227
1228         trace_cfg80211_sched_scan_stopped(wiphy, reqid);
1229
1230         __cfg80211_stop_sched_scan(rdev, reqid, true);
1231 }
1232 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_locked);
1233
1234 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
1235 {
1236         wiphy_lock(wiphy);
1237         cfg80211_sched_scan_stopped_locked(wiphy, reqid);
1238         wiphy_unlock(wiphy);
1239 }
1240 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
1241
1242 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
1243                                  struct cfg80211_sched_scan_request *req,
1244                                  bool driver_initiated)
1245 {
1246         lockdep_assert_held(&rdev->wiphy.mtx);
1247
1248         if (!driver_initiated) {
1249                 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
1250                 if (err)
1251                         return err;
1252         }
1253
1254         nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
1255
1256         cfg80211_del_sched_scan_req(rdev, req);
1257
1258         return 0;
1259 }
1260
1261 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
1262                                u64 reqid, bool driver_initiated)
1263 {
1264         struct cfg80211_sched_scan_request *sched_scan_req;
1265
1266         lockdep_assert_held(&rdev->wiphy.mtx);
1267
1268         sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
1269         if (!sched_scan_req)
1270                 return -ENOENT;
1271
1272         return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
1273                                             driver_initiated);
1274 }
1275
1276 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
1277                       unsigned long age_secs)
1278 {
1279         struct cfg80211_internal_bss *bss;
1280         unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
1281
1282         spin_lock_bh(&rdev->bss_lock);
1283         list_for_each_entry(bss, &rdev->bss_list, list)
1284                 bss->ts -= age_jiffies;
1285         spin_unlock_bh(&rdev->bss_lock);
1286 }
1287
1288 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
1289 {
1290         __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
1291 }
1292
1293 void cfg80211_bss_flush(struct wiphy *wiphy)
1294 {
1295         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1296
1297         spin_lock_bh(&rdev->bss_lock);
1298         __cfg80211_bss_expire(rdev, jiffies);
1299         spin_unlock_bh(&rdev->bss_lock);
1300 }
1301 EXPORT_SYMBOL(cfg80211_bss_flush);
1302
1303 const struct element *
1304 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
1305                          const u8 *match, unsigned int match_len,
1306                          unsigned int match_offset)
1307 {
1308         const struct element *elem;
1309
1310         for_each_element_id(elem, eid, ies, len) {
1311                 if (elem->datalen >= match_offset + match_len &&
1312                     !memcmp(elem->data + match_offset, match, match_len))
1313                         return elem;
1314         }
1315
1316         return NULL;
1317 }
1318 EXPORT_SYMBOL(cfg80211_find_elem_match);
1319
1320 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
1321                                                 const u8 *ies,
1322                                                 unsigned int len)
1323 {
1324         const struct element *elem;
1325         u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
1326         int match_len = (oui_type < 0) ? 3 : sizeof(match);
1327
1328         if (WARN_ON(oui_type > 0xff))
1329                 return NULL;
1330
1331         elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
1332                                         match, match_len, 0);
1333
1334         if (!elem || elem->datalen < 4)
1335                 return NULL;
1336
1337         return elem;
1338 }
1339 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
1340
1341 /**
1342  * enum bss_compare_mode - BSS compare mode
1343  * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
1344  * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
1345  * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
1346  */
1347 enum bss_compare_mode {
1348         BSS_CMP_REGULAR,
1349         BSS_CMP_HIDE_ZLEN,
1350         BSS_CMP_HIDE_NUL,
1351 };
1352
1353 static int cmp_bss(struct cfg80211_bss *a,
1354                    struct cfg80211_bss *b,
1355                    enum bss_compare_mode mode)
1356 {
1357         const struct cfg80211_bss_ies *a_ies, *b_ies;
1358         const u8 *ie1 = NULL;
1359         const u8 *ie2 = NULL;
1360         int i, r;
1361
1362         if (a->channel != b->channel)
1363                 return (b->channel->center_freq * 1000 + b->channel->freq_offset) -
1364                        (a->channel->center_freq * 1000 + a->channel->freq_offset);
1365
1366         a_ies = rcu_access_pointer(a->ies);
1367         if (!a_ies)
1368                 return -1;
1369         b_ies = rcu_access_pointer(b->ies);
1370         if (!b_ies)
1371                 return 1;
1372
1373         if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
1374                 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1375                                        a_ies->data, a_ies->len);
1376         if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
1377                 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
1378                                        b_ies->data, b_ies->len);
1379         if (ie1 && ie2) {
1380                 int mesh_id_cmp;
1381
1382                 if (ie1[1] == ie2[1])
1383                         mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1384                 else
1385                         mesh_id_cmp = ie2[1] - ie1[1];
1386
1387                 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1388                                        a_ies->data, a_ies->len);
1389                 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
1390                                        b_ies->data, b_ies->len);
1391                 if (ie1 && ie2) {
1392                         if (mesh_id_cmp)
1393                                 return mesh_id_cmp;
1394                         if (ie1[1] != ie2[1])
1395                                 return ie2[1] - ie1[1];
1396                         return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1397                 }
1398         }
1399
1400         r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
1401         if (r)
1402                 return r;
1403
1404         ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
1405         ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
1406
1407         if (!ie1 && !ie2)
1408                 return 0;
1409
1410         /*
1411          * Note that with "hide_ssid", the function returns a match if
1412          * the already-present BSS ("b") is a hidden SSID beacon for
1413          * the new BSS ("a").
1414          */
1415
1416         /* sort missing IE before (left of) present IE */
1417         if (!ie1)
1418                 return -1;
1419         if (!ie2)
1420                 return 1;
1421
1422         switch (mode) {
1423         case BSS_CMP_HIDE_ZLEN:
1424                 /*
1425                  * In ZLEN mode we assume the BSS entry we're
1426                  * looking for has a zero-length SSID. So if
1427                  * the one we're looking at right now has that,
1428                  * return 0. Otherwise, return the difference
1429                  * in length, but since we're looking for the
1430                  * 0-length it's really equivalent to returning
1431                  * the length of the one we're looking at.
1432                  *
1433                  * No content comparison is needed as we assume
1434                  * the content length is zero.
1435                  */
1436                 return ie2[1];
1437         case BSS_CMP_REGULAR:
1438         default:
1439                 /* sort by length first, then by contents */
1440                 if (ie1[1] != ie2[1])
1441                         return ie2[1] - ie1[1];
1442                 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
1443         case BSS_CMP_HIDE_NUL:
1444                 if (ie1[1] != ie2[1])
1445                         return ie2[1] - ie1[1];
1446                 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
1447                 for (i = 0; i < ie2[1]; i++)
1448                         if (ie2[i + 2])
1449                                 return -1;
1450                 return 0;
1451         }
1452 }
1453
1454 static bool cfg80211_bss_type_match(u16 capability,
1455                                     enum nl80211_band band,
1456                                     enum ieee80211_bss_type bss_type)
1457 {
1458         bool ret = true;
1459         u16 mask, val;
1460
1461         if (bss_type == IEEE80211_BSS_TYPE_ANY)
1462                 return ret;
1463
1464         if (band == NL80211_BAND_60GHZ) {
1465                 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
1466                 switch (bss_type) {
1467                 case IEEE80211_BSS_TYPE_ESS:
1468                         val = WLAN_CAPABILITY_DMG_TYPE_AP;
1469                         break;
1470                 case IEEE80211_BSS_TYPE_PBSS:
1471                         val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
1472                         break;
1473                 case IEEE80211_BSS_TYPE_IBSS:
1474                         val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
1475                         break;
1476                 default:
1477                         return false;
1478                 }
1479         } else {
1480                 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
1481                 switch (bss_type) {
1482                 case IEEE80211_BSS_TYPE_ESS:
1483                         val = WLAN_CAPABILITY_ESS;
1484                         break;
1485                 case IEEE80211_BSS_TYPE_IBSS:
1486                         val = WLAN_CAPABILITY_IBSS;
1487                         break;
1488                 case IEEE80211_BSS_TYPE_MBSS:
1489                         val = 0;
1490                         break;
1491                 default:
1492                         return false;
1493                 }
1494         }
1495
1496         ret = ((capability & mask) == val);
1497         return ret;
1498 }
1499
1500 /* Returned bss is reference counted and must be cleaned up appropriately. */
1501 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
1502                                       struct ieee80211_channel *channel,
1503                                       const u8 *bssid,
1504                                       const u8 *ssid, size_t ssid_len,
1505                                       enum ieee80211_bss_type bss_type,
1506                                       enum ieee80211_privacy privacy)
1507 {
1508         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1509         struct cfg80211_internal_bss *bss, *res = NULL;
1510         unsigned long now = jiffies;
1511         int bss_privacy;
1512
1513         trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
1514                                privacy);
1515
1516         spin_lock_bh(&rdev->bss_lock);
1517
1518         list_for_each_entry(bss, &rdev->bss_list, list) {
1519                 if (!cfg80211_bss_type_match(bss->pub.capability,
1520                                              bss->pub.channel->band, bss_type))
1521                         continue;
1522
1523                 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
1524                 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
1525                     (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
1526                         continue;
1527                 if (channel && bss->pub.channel != channel)
1528                         continue;
1529                 if (!is_valid_ether_addr(bss->pub.bssid))
1530                         continue;
1531                 /* Don't get expired BSS structs */
1532                 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
1533                     !atomic_read(&bss->hold))
1534                         continue;
1535                 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
1536                         res = bss;
1537                         bss_ref_get(rdev, res);
1538                         break;
1539                 }
1540         }
1541
1542         spin_unlock_bh(&rdev->bss_lock);
1543         if (!res)
1544                 return NULL;
1545         trace_cfg80211_return_bss(&res->pub);
1546         return &res->pub;
1547 }
1548 EXPORT_SYMBOL(cfg80211_get_bss);
1549
1550 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1551                           struct cfg80211_internal_bss *bss)
1552 {
1553         struct rb_node **p = &rdev->bss_tree.rb_node;
1554         struct rb_node *parent = NULL;
1555         struct cfg80211_internal_bss *tbss;
1556         int cmp;
1557
1558         while (*p) {
1559                 parent = *p;
1560                 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1561
1562                 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1563
1564                 if (WARN_ON(!cmp)) {
1565                         /* will sort of leak this BSS */
1566                         return;
1567                 }
1568
1569                 if (cmp < 0)
1570                         p = &(*p)->rb_left;
1571                 else
1572                         p = &(*p)->rb_right;
1573         }
1574
1575         rb_link_node(&bss->rbn, parent, p);
1576         rb_insert_color(&bss->rbn, &rdev->bss_tree);
1577 }
1578
1579 static struct cfg80211_internal_bss *
1580 rb_find_bss(struct cfg80211_registered_device *rdev,
1581             struct cfg80211_internal_bss *res,
1582             enum bss_compare_mode mode)
1583 {
1584         struct rb_node *n = rdev->bss_tree.rb_node;
1585         struct cfg80211_internal_bss *bss;
1586         int r;
1587
1588         while (n) {
1589                 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1590                 r = cmp_bss(&res->pub, &bss->pub, mode);
1591
1592                 if (r == 0)
1593                         return bss;
1594                 else if (r < 0)
1595                         n = n->rb_left;
1596                 else
1597                         n = n->rb_right;
1598         }
1599
1600         return NULL;
1601 }
1602
1603 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1604                                    struct cfg80211_internal_bss *new)
1605 {
1606         const struct cfg80211_bss_ies *ies;
1607         struct cfg80211_internal_bss *bss;
1608         const u8 *ie;
1609         int i, ssidlen;
1610         u8 fold = 0;
1611         u32 n_entries = 0;
1612
1613         ies = rcu_access_pointer(new->pub.beacon_ies);
1614         if (WARN_ON(!ies))
1615                 return false;
1616
1617         ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1618         if (!ie) {
1619                 /* nothing to do */
1620                 return true;
1621         }
1622
1623         ssidlen = ie[1];
1624         for (i = 0; i < ssidlen; i++)
1625                 fold |= ie[2 + i];
1626
1627         if (fold) {
1628                 /* not a hidden SSID */
1629                 return true;
1630         }
1631
1632         /* This is the bad part ... */
1633
1634         list_for_each_entry(bss, &rdev->bss_list, list) {
1635                 /*
1636                  * we're iterating all the entries anyway, so take the
1637                  * opportunity to validate the list length accounting
1638                  */
1639                 n_entries++;
1640
1641                 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1642                         continue;
1643                 if (bss->pub.channel != new->pub.channel)
1644                         continue;
1645                 if (rcu_access_pointer(bss->pub.beacon_ies))
1646                         continue;
1647                 ies = rcu_access_pointer(bss->pub.ies);
1648                 if (!ies)
1649                         continue;
1650                 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1651                 if (!ie)
1652                         continue;
1653                 if (ssidlen && ie[1] != ssidlen)
1654                         continue;
1655                 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1656                         continue;
1657                 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1658                         list_del(&bss->hidden_list);
1659                 /* combine them */
1660                 list_add(&bss->hidden_list, &new->hidden_list);
1661                 bss->pub.hidden_beacon_bss = &new->pub;
1662                 new->refcount += bss->refcount;
1663                 rcu_assign_pointer(bss->pub.beacon_ies,
1664                                    new->pub.beacon_ies);
1665         }
1666
1667         WARN_ONCE(n_entries != rdev->bss_entries,
1668                   "rdev bss entries[%d]/list[len:%d] corruption\n",
1669                   rdev->bss_entries, n_entries);
1670
1671         return true;
1672 }
1673
1674 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1675                                          const struct cfg80211_bss_ies *new_ies,
1676                                          const struct cfg80211_bss_ies *old_ies)
1677 {
1678         struct cfg80211_internal_bss *bss;
1679
1680         /* Assign beacon IEs to all sub entries */
1681         list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1682                 const struct cfg80211_bss_ies *ies;
1683
1684                 ies = rcu_access_pointer(bss->pub.beacon_ies);
1685                 WARN_ON(ies != old_ies);
1686
1687                 rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1688         }
1689 }
1690
1691 static bool
1692 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1693                           struct cfg80211_internal_bss *known,
1694                           struct cfg80211_internal_bss *new,
1695                           bool signal_valid)
1696 {
1697         lockdep_assert_held(&rdev->bss_lock);
1698
1699         /* Update IEs */
1700         if (rcu_access_pointer(new->pub.proberesp_ies)) {
1701                 const struct cfg80211_bss_ies *old;
1702
1703                 old = rcu_access_pointer(known->pub.proberesp_ies);
1704
1705                 rcu_assign_pointer(known->pub.proberesp_ies,
1706                                    new->pub.proberesp_ies);
1707                 /* Override possible earlier Beacon frame IEs */
1708                 rcu_assign_pointer(known->pub.ies,
1709                                    new->pub.proberesp_ies);
1710                 if (old)
1711                         kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1712         } else if (rcu_access_pointer(new->pub.beacon_ies)) {
1713                 const struct cfg80211_bss_ies *old;
1714
1715                 if (known->pub.hidden_beacon_bss &&
1716                     !list_empty(&known->hidden_list)) {
1717                         const struct cfg80211_bss_ies *f;
1718
1719                         /* The known BSS struct is one of the probe
1720                          * response members of a group, but we're
1721                          * receiving a beacon (beacon_ies in the new
1722                          * bss is used). This can only mean that the
1723                          * AP changed its beacon from not having an
1724                          * SSID to showing it, which is confusing so
1725                          * drop this information.
1726                          */
1727
1728                         f = rcu_access_pointer(new->pub.beacon_ies);
1729                         kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1730                         return false;
1731                 }
1732
1733                 old = rcu_access_pointer(known->pub.beacon_ies);
1734
1735                 rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1736
1737                 /* Override IEs if they were from a beacon before */
1738                 if (old == rcu_access_pointer(known->pub.ies))
1739                         rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1740
1741                 cfg80211_update_hidden_bsses(known,
1742                                              rcu_access_pointer(new->pub.beacon_ies),
1743                                              old);
1744
1745                 if (old)
1746                         kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1747         }
1748
1749         known->pub.beacon_interval = new->pub.beacon_interval;
1750
1751         /* don't update the signal if beacon was heard on
1752          * adjacent channel.
1753          */
1754         if (signal_valid)
1755                 known->pub.signal = new->pub.signal;
1756         known->pub.capability = new->pub.capability;
1757         known->ts = new->ts;
1758         known->ts_boottime = new->ts_boottime;
1759         known->parent_tsf = new->parent_tsf;
1760         known->pub.chains = new->pub.chains;
1761         memcpy(known->pub.chain_signal, new->pub.chain_signal,
1762                IEEE80211_MAX_CHAINS);
1763         ether_addr_copy(known->parent_bssid, new->parent_bssid);
1764         known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1765         known->pub.bssid_index = new->pub.bssid_index;
1766
1767         return true;
1768 }
1769
1770 /* Returned bss is reference counted and must be cleaned up appropriately. */
1771 static struct cfg80211_internal_bss *
1772 __cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1773                       struct cfg80211_internal_bss *tmp,
1774                       bool signal_valid, unsigned long ts)
1775 {
1776         struct cfg80211_internal_bss *found = NULL;
1777
1778         if (WARN_ON(!tmp->pub.channel))
1779                 return NULL;
1780
1781         tmp->ts = ts;
1782
1783         if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1784                 return NULL;
1785         }
1786
1787         found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1788
1789         if (found) {
1790                 if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1791                         return NULL;
1792         } else {
1793                 struct cfg80211_internal_bss *new;
1794                 struct cfg80211_internal_bss *hidden;
1795                 struct cfg80211_bss_ies *ies;
1796
1797                 /*
1798                  * create a copy -- the "res" variable that is passed in
1799                  * is allocated on the stack since it's not needed in the
1800                  * more common case of an update
1801                  */
1802                 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1803                               GFP_ATOMIC);
1804                 if (!new) {
1805                         ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1806                         if (ies)
1807                                 kfree_rcu(ies, rcu_head);
1808                         ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1809                         if (ies)
1810                                 kfree_rcu(ies, rcu_head);
1811                         return NULL;
1812                 }
1813                 memcpy(new, tmp, sizeof(*new));
1814                 new->refcount = 1;
1815                 INIT_LIST_HEAD(&new->hidden_list);
1816                 INIT_LIST_HEAD(&new->pub.nontrans_list);
1817                 /* we'll set this later if it was non-NULL */
1818                 new->pub.transmitted_bss = NULL;
1819
1820                 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1821                         hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1822                         if (!hidden)
1823                                 hidden = rb_find_bss(rdev, tmp,
1824                                                      BSS_CMP_HIDE_NUL);
1825                         if (hidden) {
1826                                 new->pub.hidden_beacon_bss = &hidden->pub;
1827                                 list_add(&new->hidden_list,
1828                                          &hidden->hidden_list);
1829                                 hidden->refcount++;
1830                                 rcu_assign_pointer(new->pub.beacon_ies,
1831                                                    hidden->pub.beacon_ies);
1832                         }
1833                 } else {
1834                         /*
1835                          * Ok so we found a beacon, and don't have an entry. If
1836                          * it's a beacon with hidden SSID, we might be in for an
1837                          * expensive search for any probe responses that should
1838                          * be grouped with this beacon for updates ...
1839                          */
1840                         if (!cfg80211_combine_bsses(rdev, new)) {
1841                                 bss_ref_put(rdev, new);
1842                                 return NULL;
1843                         }
1844                 }
1845
1846                 if (rdev->bss_entries >= bss_entries_limit &&
1847                     !cfg80211_bss_expire_oldest(rdev)) {
1848                         bss_ref_put(rdev, new);
1849                         return NULL;
1850                 }
1851
1852                 /* This must be before the call to bss_ref_get */
1853                 if (tmp->pub.transmitted_bss) {
1854                         new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1855                         bss_ref_get(rdev, bss_from_pub(tmp->pub.transmitted_bss));
1856                 }
1857
1858                 list_add_tail(&new->list, &rdev->bss_list);
1859                 rdev->bss_entries++;
1860                 rb_insert_bss(rdev, new);
1861                 found = new;
1862         }
1863
1864         rdev->bss_generation++;
1865         bss_ref_get(rdev, found);
1866
1867         return found;
1868 }
1869
1870 struct cfg80211_internal_bss *
1871 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1872                     struct cfg80211_internal_bss *tmp,
1873                     bool signal_valid, unsigned long ts)
1874 {
1875         struct cfg80211_internal_bss *res;
1876
1877         spin_lock_bh(&rdev->bss_lock);
1878         res = __cfg80211_bss_update(rdev, tmp, signal_valid, ts);
1879         spin_unlock_bh(&rdev->bss_lock);
1880
1881         return res;
1882 }
1883
1884 int cfg80211_get_ies_channel_number(const u8 *ie, size_t ielen,
1885                                     enum nl80211_band band)
1886 {
1887         const struct element *tmp;
1888
1889         if (band == NL80211_BAND_6GHZ) {
1890                 struct ieee80211_he_operation *he_oper;
1891
1892                 tmp = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ie,
1893                                              ielen);
1894                 if (tmp && tmp->datalen >= sizeof(*he_oper) &&
1895                     tmp->datalen >= ieee80211_he_oper_size(&tmp->data[1])) {
1896                         const struct ieee80211_he_6ghz_oper *he_6ghz_oper;
1897
1898                         he_oper = (void *)&tmp->data[1];
1899
1900                         he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper);
1901                         if (!he_6ghz_oper)
1902                                 return -1;
1903
1904                         return he_6ghz_oper->primary;
1905                 }
1906         } else if (band == NL80211_BAND_S1GHZ) {
1907                 tmp = cfg80211_find_elem(WLAN_EID_S1G_OPERATION, ie, ielen);
1908                 if (tmp && tmp->datalen >= sizeof(struct ieee80211_s1g_oper_ie)) {
1909                         struct ieee80211_s1g_oper_ie *s1gop = (void *)tmp->data;
1910
1911                         return s1gop->oper_ch;
1912                 }
1913         } else {
1914                 tmp = cfg80211_find_elem(WLAN_EID_DS_PARAMS, ie, ielen);
1915                 if (tmp && tmp->datalen == 1)
1916                         return tmp->data[0];
1917
1918                 tmp = cfg80211_find_elem(WLAN_EID_HT_OPERATION, ie, ielen);
1919                 if (tmp &&
1920                     tmp->datalen >= sizeof(struct ieee80211_ht_operation)) {
1921                         struct ieee80211_ht_operation *htop = (void *)tmp->data;
1922
1923                         return htop->primary_chan;
1924                 }
1925         }
1926
1927         return -1;
1928 }
1929 EXPORT_SYMBOL(cfg80211_get_ies_channel_number);
1930
1931 /*
1932  * Update RX channel information based on the available frame payload
1933  * information. This is mainly for the 2.4 GHz band where frames can be received
1934  * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1935  * element to indicate the current (transmitting) channel, but this might also
1936  * be needed on other bands if RX frequency does not match with the actual
1937  * operating channel of a BSS, or if the AP reports a different primary channel.
1938  */
1939 static struct ieee80211_channel *
1940 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1941                          struct ieee80211_channel *channel)
1942 {
1943         u32 freq;
1944         int channel_number;
1945         struct ieee80211_channel *alt_channel;
1946
1947         channel_number = cfg80211_get_ies_channel_number(ie, ielen,
1948                                                          channel->band);
1949
1950         if (channel_number < 0) {
1951                 /* No channel information in frame payload */
1952                 return channel;
1953         }
1954
1955         freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1956
1957         /*
1958          * Frame info (beacon/prob res) is the same as received channel,
1959          * no need for further processing.
1960          */
1961         if (freq == ieee80211_channel_to_khz(channel))
1962                 return channel;
1963
1964         alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1965         if (!alt_channel) {
1966                 if (channel->band == NL80211_BAND_2GHZ ||
1967                     channel->band == NL80211_BAND_6GHZ) {
1968                         /*
1969                          * Better not allow unexpected channels when that could
1970                          * be going beyond the 1-11 range (e.g., discovering
1971                          * BSS on channel 12 when radio is configured for
1972                          * channel 11) or beyond the 6 GHz channel range.
1973                          */
1974                         return NULL;
1975                 }
1976
1977                 /* No match for the payload channel number - ignore it */
1978                 return channel;
1979         }
1980
1981         /*
1982          * Use the channel determined through the payload channel number
1983          * instead of the RX channel reported by the driver.
1984          */
1985         if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1986                 return NULL;
1987         return alt_channel;
1988 }
1989
1990 struct cfg80211_inform_single_bss_data {
1991         struct cfg80211_inform_bss *drv_data;
1992         enum cfg80211_bss_frame_type ftype;
1993         struct ieee80211_channel *channel;
1994         u8 bssid[ETH_ALEN];
1995         u64 tsf;
1996         u16 capability;
1997         u16 beacon_interval;
1998         const u8 *ie;
1999         size_t ielen;
2000
2001         enum {
2002                 BSS_SOURCE_DIRECT = 0,
2003                 BSS_SOURCE_MBSSID,
2004                 BSS_SOURCE_STA_PROFILE,
2005         } bss_source;
2006         /* Set if reporting bss_source != BSS_SOURCE_DIRECT */
2007         struct cfg80211_bss *source_bss;
2008         u8 max_bssid_indicator;
2009         u8 bssid_index;
2010 };
2011
2012 /* Returned bss is reference counted and must be cleaned up appropriately. */
2013 static struct cfg80211_bss *
2014 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
2015                                 struct cfg80211_inform_single_bss_data *data,
2016                                 gfp_t gfp)
2017 {
2018         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2019         struct cfg80211_inform_bss *drv_data = data->drv_data;
2020         struct cfg80211_bss_ies *ies;
2021         struct ieee80211_channel *channel;
2022         struct cfg80211_internal_bss tmp = {}, *res;
2023         int bss_type;
2024         bool signal_valid;
2025         unsigned long ts;
2026
2027         if (WARN_ON(!wiphy))
2028                 return NULL;
2029
2030         if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2031                     (drv_data->signal < 0 || drv_data->signal > 100)))
2032                 return NULL;
2033
2034         if (WARN_ON(data->bss_source != BSS_SOURCE_DIRECT && !data->source_bss))
2035                 return NULL;
2036
2037         channel = data->channel;
2038         if (!channel)
2039                 channel = cfg80211_get_bss_channel(wiphy, data->ie, data->ielen,
2040                                                    drv_data->chan);
2041         if (!channel)
2042                 return NULL;
2043
2044         memcpy(tmp.pub.bssid, data->bssid, ETH_ALEN);
2045         tmp.pub.channel = channel;
2046         if (data->bss_source != BSS_SOURCE_STA_PROFILE)
2047                 tmp.pub.signal = drv_data->signal;
2048         else
2049                 tmp.pub.signal = 0;
2050         tmp.pub.beacon_interval = data->beacon_interval;
2051         tmp.pub.capability = data->capability;
2052         tmp.ts_boottime = drv_data->boottime_ns;
2053         tmp.parent_tsf = drv_data->parent_tsf;
2054         ether_addr_copy(tmp.parent_bssid, drv_data->parent_bssid);
2055
2056         if (data->bss_source != BSS_SOURCE_DIRECT) {
2057                 tmp.pub.transmitted_bss = data->source_bss;
2058                 ts = bss_from_pub(data->source_bss)->ts;
2059                 tmp.pub.bssid_index = data->bssid_index;
2060                 tmp.pub.max_bssid_indicator = data->max_bssid_indicator;
2061         } else {
2062                 ts = jiffies;
2063
2064                 if (channel->band == NL80211_BAND_60GHZ) {
2065                         bss_type = data->capability &
2066                                    WLAN_CAPABILITY_DMG_TYPE_MASK;
2067                         if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2068                             bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2069                                 regulatory_hint_found_beacon(wiphy, channel,
2070                                                              gfp);
2071                 } else {
2072                         if (data->capability & WLAN_CAPABILITY_ESS)
2073                                 regulatory_hint_found_beacon(wiphy, channel,
2074                                                              gfp);
2075                 }
2076         }
2077
2078         /*
2079          * If we do not know here whether the IEs are from a Beacon or Probe
2080          * Response frame, we need to pick one of the options and only use it
2081          * with the driver that does not provide the full Beacon/Probe Response
2082          * frame. Use Beacon frame pointer to avoid indicating that this should
2083          * override the IEs pointer should we have received an earlier
2084          * indication of Probe Response data.
2085          */
2086         ies = kzalloc(sizeof(*ies) + data->ielen, gfp);
2087         if (!ies)
2088                 return NULL;
2089         ies->len = data->ielen;
2090         ies->tsf = data->tsf;
2091         ies->from_beacon = false;
2092         memcpy(ies->data, data->ie, data->ielen);
2093
2094         switch (data->ftype) {
2095         case CFG80211_BSS_FTYPE_BEACON:
2096                 ies->from_beacon = true;
2097                 fallthrough;
2098         case CFG80211_BSS_FTYPE_UNKNOWN:
2099                 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2100                 break;
2101         case CFG80211_BSS_FTYPE_PRESP:
2102                 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2103                 break;
2104         }
2105         rcu_assign_pointer(tmp.pub.ies, ies);
2106
2107         signal_valid = drv_data->chan == channel;
2108         spin_lock_bh(&rdev->bss_lock);
2109         res = __cfg80211_bss_update(rdev, &tmp, signal_valid, ts);
2110         if (!res)
2111                 goto drop;
2112
2113         rdev_inform_bss(rdev, &res->pub, ies, data->drv_data);
2114
2115         if (data->bss_source == BSS_SOURCE_MBSSID) {
2116                 /* this is a nontransmitting bss, we need to add it to
2117                  * transmitting bss' list if it is not there
2118                  */
2119                 if (cfg80211_add_nontrans_list(data->source_bss, &res->pub)) {
2120                         if (__cfg80211_unlink_bss(rdev, res)) {
2121                                 rdev->bss_generation++;
2122                                 res = NULL;
2123                         }
2124                 }
2125
2126                 if (!res)
2127                         goto drop;
2128         }
2129         spin_unlock_bh(&rdev->bss_lock);
2130
2131         trace_cfg80211_return_bss(&res->pub);
2132         /* __cfg80211_bss_update gives us a referenced result */
2133         return &res->pub;
2134
2135 drop:
2136         spin_unlock_bh(&rdev->bss_lock);
2137         return NULL;
2138 }
2139
2140 static const struct element
2141 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
2142                                    const struct element *mbssid_elem,
2143                                    const struct element *sub_elem)
2144 {
2145         const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
2146         const struct element *next_mbssid;
2147         const struct element *next_sub;
2148
2149         next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2150                                          mbssid_end,
2151                                          ielen - (mbssid_end - ie));
2152
2153         /*
2154          * If it is not the last subelement in current MBSSID IE or there isn't
2155          * a next MBSSID IE - profile is complete.
2156         */
2157         if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
2158             !next_mbssid)
2159                 return NULL;
2160
2161         /* For any length error, just return NULL */
2162
2163         if (next_mbssid->datalen < 4)
2164                 return NULL;
2165
2166         next_sub = (void *)&next_mbssid->data[1];
2167
2168         if (next_mbssid->data + next_mbssid->datalen <
2169             next_sub->data + next_sub->datalen)
2170                 return NULL;
2171
2172         if (next_sub->id != 0 || next_sub->datalen < 2)
2173                 return NULL;
2174
2175         /*
2176          * Check if the first element in the next sub element is a start
2177          * of a new profile
2178          */
2179         return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
2180                NULL : next_mbssid;
2181 }
2182
2183 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
2184                               const struct element *mbssid_elem,
2185                               const struct element *sub_elem,
2186                               u8 *merged_ie, size_t max_copy_len)
2187 {
2188         size_t copied_len = sub_elem->datalen;
2189         const struct element *next_mbssid;
2190
2191         if (sub_elem->datalen > max_copy_len)
2192                 return 0;
2193
2194         memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
2195
2196         while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
2197                                                                 mbssid_elem,
2198                                                                 sub_elem))) {
2199                 const struct element *next_sub = (void *)&next_mbssid->data[1];
2200
2201                 if (copied_len + next_sub->datalen > max_copy_len)
2202                         break;
2203                 memcpy(merged_ie + copied_len, next_sub->data,
2204                        next_sub->datalen);
2205                 copied_len += next_sub->datalen;
2206         }
2207
2208         return copied_len;
2209 }
2210 EXPORT_SYMBOL(cfg80211_merge_profile);
2211
2212 static void
2213 cfg80211_parse_mbssid_data(struct wiphy *wiphy,
2214                            struct cfg80211_inform_single_bss_data *tx_data,
2215                            struct cfg80211_bss *source_bss,
2216                            gfp_t gfp)
2217 {
2218         struct cfg80211_inform_single_bss_data data = {
2219                 .drv_data = tx_data->drv_data,
2220                 .ftype = tx_data->ftype,
2221                 .tsf = tx_data->tsf,
2222                 .beacon_interval = tx_data->beacon_interval,
2223                 .source_bss = source_bss,
2224                 .bss_source = BSS_SOURCE_MBSSID,
2225         };
2226         const u8 *mbssid_index_ie;
2227         const struct element *elem, *sub;
2228         u8 *new_ie, *profile;
2229         u64 seen_indices = 0;
2230         struct cfg80211_bss *bss;
2231
2232         if (!source_bss)
2233                 return;
2234         if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
2235                                 tx_data->ie, tx_data->ielen))
2236                 return;
2237         if (!wiphy->support_mbssid)
2238                 return;
2239         if (wiphy->support_only_he_mbssid &&
2240             !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
2241                                     tx_data->ie, tx_data->ielen))
2242                 return;
2243
2244         new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2245         if (!new_ie)
2246                 return;
2247
2248         profile = kmalloc(tx_data->ielen, gfp);
2249         if (!profile)
2250                 goto out;
2251
2252         for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID,
2253                             tx_data->ie, tx_data->ielen) {
2254                 if (elem->datalen < 4)
2255                         continue;
2256                 if (elem->data[0] < 1 || (int)elem->data[0] > 8)
2257                         continue;
2258                 for_each_element(sub, elem->data + 1, elem->datalen - 1) {
2259                         u8 profile_len;
2260
2261                         if (sub->id != 0 || sub->datalen < 4) {
2262                                 /* not a valid BSS profile */
2263                                 continue;
2264                         }
2265
2266                         if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
2267                             sub->data[1] != 2) {
2268                                 /* The first element within the Nontransmitted
2269                                  * BSSID Profile is not the Nontransmitted
2270                                  * BSSID Capability element.
2271                                  */
2272                                 continue;
2273                         }
2274
2275                         memset(profile, 0, tx_data->ielen);
2276                         profile_len = cfg80211_merge_profile(tx_data->ie,
2277                                                              tx_data->ielen,
2278                                                              elem,
2279                                                              sub,
2280                                                              profile,
2281                                                              tx_data->ielen);
2282
2283                         /* found a Nontransmitted BSSID Profile */
2284                         mbssid_index_ie = cfg80211_find_ie
2285                                 (WLAN_EID_MULTI_BSSID_IDX,
2286                                  profile, profile_len);
2287                         if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
2288                             mbssid_index_ie[2] == 0 ||
2289                             mbssid_index_ie[2] > 46) {
2290                                 /* No valid Multiple BSSID-Index element */
2291                                 continue;
2292                         }
2293
2294                         if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
2295                                 /* We don't support legacy split of a profile */
2296                                 net_dbg_ratelimited("Partial info for BSSID index %d\n",
2297                                                     mbssid_index_ie[2]);
2298
2299                         seen_indices |= BIT_ULL(mbssid_index_ie[2]);
2300
2301                         data.bssid_index = mbssid_index_ie[2];
2302                         data.max_bssid_indicator = elem->data[0];
2303
2304                         cfg80211_gen_new_bssid(tx_data->bssid,
2305                                                data.max_bssid_indicator,
2306                                                data.bssid_index,
2307                                                data.bssid);
2308
2309                         memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2310                         data.ie = new_ie;
2311                         data.ielen = cfg80211_gen_new_ie(tx_data->ie,
2312                                                          tx_data->ielen,
2313                                                          profile,
2314                                                          profile_len,
2315                                                          new_ie,
2316                                                          IEEE80211_MAX_DATA_LEN);
2317                         if (!data.ielen)
2318                                 continue;
2319
2320                         data.capability = get_unaligned_le16(profile + 2);
2321                         bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp);
2322                         if (!bss)
2323                                 break;
2324                         cfg80211_put_bss(wiphy, bss);
2325                 }
2326         }
2327
2328 out:
2329         kfree(new_ie);
2330         kfree(profile);
2331 }
2332
2333 ssize_t cfg80211_defragment_element(const struct element *elem, const u8 *ies,
2334                                     size_t ieslen, u8 *data, size_t data_len,
2335                                     u8 frag_id)
2336 {
2337         const struct element *next;
2338         ssize_t copied;
2339         u8 elem_datalen;
2340
2341         if (!elem)
2342                 return -EINVAL;
2343
2344         /* elem might be invalid after the memmove */
2345         next = (void *)(elem->data + elem->datalen);
2346         elem_datalen = elem->datalen;
2347
2348         if (elem->id == WLAN_EID_EXTENSION) {
2349                 copied = elem->datalen - 1;
2350                 if (copied > data_len)
2351                         return -ENOSPC;
2352
2353                 memmove(data, elem->data + 1, copied);
2354         } else {
2355                 copied = elem->datalen;
2356                 if (copied > data_len)
2357                         return -ENOSPC;
2358
2359                 memmove(data, elem->data, copied);
2360         }
2361
2362         /* Fragmented elements must have 255 bytes */
2363         if (elem_datalen < 255)
2364                 return copied;
2365
2366         for (elem = next;
2367              elem->data < ies + ieslen &&
2368                 elem->data + elem->datalen <= ies + ieslen;
2369              elem = next) {
2370                 /* elem might be invalid after the memmove */
2371                 next = (void *)(elem->data + elem->datalen);
2372
2373                 if (elem->id != frag_id)
2374                         break;
2375
2376                 elem_datalen = elem->datalen;
2377
2378                 if (copied + elem_datalen > data_len)
2379                         return -ENOSPC;
2380
2381                 memmove(data + copied, elem->data, elem_datalen);
2382                 copied += elem_datalen;
2383
2384                 /* Only the last fragment may be short */
2385                 if (elem_datalen != 255)
2386                         break;
2387         }
2388
2389         return copied;
2390 }
2391 EXPORT_SYMBOL(cfg80211_defragment_element);
2392
2393 struct cfg80211_mle {
2394         struct ieee80211_multi_link_elem *mle;
2395         struct ieee80211_mle_per_sta_profile
2396                 *sta_prof[IEEE80211_MLD_MAX_NUM_LINKS];
2397         ssize_t sta_prof_len[IEEE80211_MLD_MAX_NUM_LINKS];
2398
2399         u8 data[];
2400 };
2401
2402 static struct cfg80211_mle *
2403 cfg80211_defrag_mle(const struct element *mle, const u8 *ie, size_t ielen,
2404                     gfp_t gfp)
2405 {
2406         const struct element *elem;
2407         struct cfg80211_mle *res;
2408         size_t buf_len;
2409         ssize_t mle_len;
2410         u8 common_size, idx;
2411
2412         if (!mle || !ieee80211_mle_size_ok(mle->data + 1, mle->datalen - 1))
2413                 return NULL;
2414
2415         /* Required length for first defragmentation */
2416         buf_len = mle->datalen - 1;
2417         for_each_element(elem, mle->data + mle->datalen,
2418                          ielen - sizeof(*mle) + mle->datalen) {
2419                 if (elem->id != WLAN_EID_FRAGMENT)
2420                         break;
2421
2422                 buf_len += elem->datalen;
2423         }
2424
2425         res = kzalloc(struct_size(res, data, buf_len), gfp);
2426         if (!res)
2427                 return NULL;
2428
2429         mle_len = cfg80211_defragment_element(mle, ie, ielen,
2430                                               res->data, buf_len,
2431                                               WLAN_EID_FRAGMENT);
2432         if (mle_len < 0)
2433                 goto error;
2434
2435         res->mle = (void *)res->data;
2436
2437         /* Find the sub-element area in the buffer */
2438         common_size = ieee80211_mle_common_size((u8 *)res->mle);
2439         ie = res->data + common_size;
2440         ielen = mle_len - common_size;
2441
2442         idx = 0;
2443         for_each_element_id(elem, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE,
2444                             ie, ielen) {
2445                 res->sta_prof[idx] = (void *)elem->data;
2446                 res->sta_prof_len[idx] = elem->datalen;
2447
2448                 idx++;
2449                 if (idx >= IEEE80211_MLD_MAX_NUM_LINKS)
2450                         break;
2451         }
2452         if (!for_each_element_completed(elem, ie, ielen))
2453                 goto error;
2454
2455         /* Defragment sta_info in-place */
2456         for (idx = 0; idx < IEEE80211_MLD_MAX_NUM_LINKS && res->sta_prof[idx];
2457              idx++) {
2458                 if (res->sta_prof_len[idx] < 255)
2459                         continue;
2460
2461                 elem = (void *)res->sta_prof[idx] - 2;
2462
2463                 if (idx + 1 < ARRAY_SIZE(res->sta_prof) &&
2464                     res->sta_prof[idx + 1])
2465                         buf_len = (u8 *)res->sta_prof[idx + 1] -
2466                                   (u8 *)res->sta_prof[idx];
2467                 else
2468                         buf_len = ielen + ie - (u8 *)elem;
2469
2470                 res->sta_prof_len[idx] =
2471                         cfg80211_defragment_element(elem,
2472                                                     (u8 *)elem, buf_len,
2473                                                     (u8 *)res->sta_prof[idx],
2474                                                     buf_len,
2475                                                     IEEE80211_MLE_SUBELEM_FRAGMENT);
2476                 if (res->sta_prof_len[idx] < 0)
2477                         goto error;
2478         }
2479
2480         return res;
2481
2482 error:
2483         kfree(res);
2484         return NULL;
2485 }
2486
2487 static bool
2488 cfg80211_tbtt_info_for_mld_ap(const u8 *ie, size_t ielen, u8 mld_id, u8 link_id,
2489                               const struct ieee80211_neighbor_ap_info **ap_info,
2490                               const u8 **tbtt_info)
2491 {
2492         const struct ieee80211_neighbor_ap_info *info;
2493         const struct element *rnr;
2494         const u8 *pos, *end;
2495
2496         for_each_element_id(rnr, WLAN_EID_REDUCED_NEIGHBOR_REPORT, ie, ielen) {
2497                 pos = rnr->data;
2498                 end = rnr->data + rnr->datalen;
2499
2500                 /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
2501                 while (sizeof(*info) <= end - pos) {
2502                         const struct ieee80211_rnr_mld_params *mld_params;
2503                         u16 params;
2504                         u8 length, i, count, mld_params_offset;
2505                         u8 type, lid;
2506
2507                         info = (void *)pos;
2508                         count = u8_get_bits(info->tbtt_info_hdr,
2509                                             IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
2510                         length = info->tbtt_info_len;
2511
2512                         pos += sizeof(*info);
2513
2514                         if (count * length > end - pos)
2515                                 return false;
2516
2517                         type = u8_get_bits(info->tbtt_info_hdr,
2518                                            IEEE80211_AP_INFO_TBTT_HDR_TYPE);
2519
2520                         /* Only accept full TBTT information. NSTR mobile APs
2521                          * use the shortened version, but we ignore them here.
2522                          */
2523                         if (type == IEEE80211_TBTT_INFO_TYPE_TBTT &&
2524                             length >=
2525                             offsetofend(struct ieee80211_tbtt_info_ge_11,
2526                                         mld_params)) {
2527                                 mld_params_offset =
2528                                         offsetof(struct ieee80211_tbtt_info_ge_11, mld_params);
2529                         } else {
2530                                 pos += count * length;
2531                                 continue;
2532                         }
2533
2534                         for (i = 0; i < count; i++) {
2535                                 mld_params = (void *)pos + mld_params_offset;
2536                                 params = le16_to_cpu(mld_params->params);
2537
2538                                 lid = u16_get_bits(params,
2539                                                    IEEE80211_RNR_MLD_PARAMS_LINK_ID);
2540
2541                                 if (mld_id == mld_params->mld_id &&
2542                                     link_id == lid) {
2543                                         *ap_info = info;
2544                                         *tbtt_info = pos;
2545
2546                                         return true;
2547                                 }
2548
2549                                 pos += length;
2550                         }
2551                 }
2552         }
2553
2554         return false;
2555 }
2556
2557 static void cfg80211_parse_ml_sta_data(struct wiphy *wiphy,
2558                                        struct cfg80211_inform_single_bss_data *tx_data,
2559                                        struct cfg80211_bss *source_bss,
2560                                        gfp_t gfp)
2561 {
2562         struct cfg80211_inform_single_bss_data data = {
2563                 .drv_data = tx_data->drv_data,
2564                 .ftype = tx_data->ftype,
2565                 .source_bss = source_bss,
2566                 .bss_source = BSS_SOURCE_STA_PROFILE,
2567         };
2568         struct ieee80211_multi_link_elem *ml_elem;
2569         const struct element *elem;
2570         struct cfg80211_mle *mle;
2571         u16 control;
2572         u8 *new_ie;
2573         struct cfg80211_bss *bss;
2574         int mld_id;
2575         u16 seen_links = 0;
2576         const u8 *pos;
2577         u8 i;
2578
2579         if (!source_bss)
2580                 return;
2581
2582         if (tx_data->ftype != CFG80211_BSS_FTYPE_PRESP)
2583                 return;
2584
2585         elem = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_MULTI_LINK,
2586                                       tx_data->ie, tx_data->ielen);
2587         if (!elem || !ieee80211_mle_size_ok(elem->data + 1, elem->datalen - 1))
2588                 return;
2589
2590         ml_elem = (void *)elem->data + 1;
2591         control = le16_to_cpu(ml_elem->control);
2592         if (u16_get_bits(control, IEEE80211_ML_CONTROL_TYPE) !=
2593             IEEE80211_ML_CONTROL_TYPE_BASIC)
2594                 return;
2595
2596         /* Must be present when transmitted by an AP (in a probe response) */
2597         if (!(control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT) ||
2598             !(control & IEEE80211_MLC_BASIC_PRES_LINK_ID) ||
2599             !(control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP))
2600                 return;
2601
2602         /* length + MLD MAC address + link ID info + BSS Params Change Count */
2603         pos = ml_elem->variable + 1 + 6 + 1 + 1;
2604
2605         if (u16_get_bits(control, IEEE80211_MLC_BASIC_PRES_MED_SYNC_DELAY))
2606                 pos += 2;
2607         if (u16_get_bits(control, IEEE80211_MLC_BASIC_PRES_EML_CAPA))
2608                 pos += 2;
2609
2610         /* MLD capabilities and operations */
2611         pos += 2;
2612
2613         /* Not included when the (nontransmitted) AP is responding itself,
2614          * but defined to zero then (Draft P802.11be_D3.0, 9.4.2.170.2)
2615          */
2616         if (u16_get_bits(control, IEEE80211_MLC_BASIC_PRES_MLD_ID)) {
2617                 mld_id = *pos;
2618                 pos += 1;
2619         } else {
2620                 mld_id = 0;
2621         }
2622
2623         /* Extended MLD capabilities and operations */
2624         pos += 2;
2625
2626         /* Fully defrag the ML element for sta information/profile iteration */
2627         mle = cfg80211_defrag_mle(elem, tx_data->ie, tx_data->ielen, gfp);
2628         if (!mle)
2629                 return;
2630
2631         new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
2632         if (!new_ie)
2633                 goto out;
2634
2635         for (i = 0; i < ARRAY_SIZE(mle->sta_prof) && mle->sta_prof[i]; i++) {
2636                 const struct ieee80211_neighbor_ap_info *ap_info;
2637                 enum nl80211_band band;
2638                 u32 freq;
2639                 const u8 *profile;
2640                 const u8 *tbtt_info;
2641                 ssize_t profile_len;
2642                 u8 link_id;
2643
2644                 if (!ieee80211_mle_basic_sta_prof_size_ok((u8 *)mle->sta_prof[i],
2645                                                           mle->sta_prof_len[i]))
2646                         continue;
2647
2648                 control = le16_to_cpu(mle->sta_prof[i]->control);
2649
2650                 if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
2651                         continue;
2652
2653                 link_id = u16_get_bits(control,
2654                                        IEEE80211_MLE_STA_CONTROL_LINK_ID);
2655                 if (seen_links & BIT(link_id))
2656                         break;
2657                 seen_links |= BIT(link_id);
2658
2659                 if (!(control & IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT) ||
2660                     !(control & IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT) ||
2661                     !(control & IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT))
2662                         continue;
2663
2664                 memcpy(data.bssid, mle->sta_prof[i]->variable, ETH_ALEN);
2665                 data.beacon_interval =
2666                         get_unaligned_le16(mle->sta_prof[i]->variable + 6);
2667                 data.tsf = tx_data->tsf +
2668                            get_unaligned_le64(mle->sta_prof[i]->variable + 8);
2669
2670                 /* sta_info_len counts itself */
2671                 profile = mle->sta_prof[i]->variable +
2672                           mle->sta_prof[i]->sta_info_len - 1;
2673                 profile_len = (u8 *)mle->sta_prof[i] + mle->sta_prof_len[i] -
2674                               profile;
2675
2676                 if (profile_len < 2)
2677                         continue;
2678
2679                 data.capability = get_unaligned_le16(profile);
2680                 profile += 2;
2681                 profile_len -= 2;
2682
2683                 /* Find in RNR to look up channel information */
2684                 if (!cfg80211_tbtt_info_for_mld_ap(tx_data->ie, tx_data->ielen,
2685                                                    mld_id, link_id,
2686                                                    &ap_info, &tbtt_info))
2687                         continue;
2688
2689                 /* We could sanity check the BSSID is included */
2690
2691                 if (!ieee80211_operating_class_to_band(ap_info->op_class,
2692                                                        &band))
2693                         continue;
2694
2695                 freq = ieee80211_channel_to_freq_khz(ap_info->channel, band);
2696                 data.channel = ieee80211_get_channel_khz(wiphy, freq);
2697
2698                 /* Generate new elements */
2699                 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
2700                 data.ie = new_ie;
2701                 data.ielen = cfg80211_gen_new_ie(tx_data->ie, tx_data->ielen,
2702                                                  profile, profile_len,
2703                                                  new_ie,
2704                                                  IEEE80211_MAX_DATA_LEN);
2705                 if (!data.ielen)
2706                         continue;
2707
2708                 bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp);
2709                 if (!bss)
2710                         break;
2711                 cfg80211_put_bss(wiphy, bss);
2712         }
2713
2714 out:
2715         kfree(new_ie);
2716         kfree(mle);
2717 }
2718
2719 struct cfg80211_bss *
2720 cfg80211_inform_bss_data(struct wiphy *wiphy,
2721                          struct cfg80211_inform_bss *data,
2722                          enum cfg80211_bss_frame_type ftype,
2723                          const u8 *bssid, u64 tsf, u16 capability,
2724                          u16 beacon_interval, const u8 *ie, size_t ielen,
2725                          gfp_t gfp)
2726 {
2727         struct cfg80211_inform_single_bss_data inform_data = {
2728                 .drv_data = data,
2729                 .ftype = ftype,
2730                 .tsf = tsf,
2731                 .capability = capability,
2732                 .beacon_interval = beacon_interval,
2733                 .ie = ie,
2734                 .ielen = ielen,
2735         };
2736         struct cfg80211_bss *res;
2737
2738         memcpy(inform_data.bssid, bssid, ETH_ALEN);
2739
2740         res = cfg80211_inform_single_bss_data(wiphy, &inform_data, gfp);
2741         if (!res)
2742                 return NULL;
2743
2744         cfg80211_parse_mbssid_data(wiphy, &inform_data, res, gfp);
2745
2746         cfg80211_parse_ml_sta_data(wiphy, &inform_data, res, gfp);
2747
2748         return res;
2749 }
2750 EXPORT_SYMBOL(cfg80211_inform_bss_data);
2751
2752 /* cfg80211_inform_bss_width_frame helper */
2753 static struct cfg80211_bss *
2754 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
2755                                       struct cfg80211_inform_bss *data,
2756                                       struct ieee80211_mgmt *mgmt, size_t len,
2757                                       gfp_t gfp)
2758 {
2759         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2760         struct cfg80211_internal_bss tmp = {}, *res;
2761         struct cfg80211_bss_ies *ies;
2762         struct ieee80211_channel *channel;
2763         bool signal_valid;
2764         struct ieee80211_ext *ext = NULL;
2765         u8 *bssid, *variable;
2766         u16 capability, beacon_int;
2767         size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
2768                                              u.probe_resp.variable);
2769         int bss_type;
2770
2771         BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
2772                         offsetof(struct ieee80211_mgmt, u.beacon.variable));
2773
2774         trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
2775
2776         if (WARN_ON(!mgmt))
2777                 return NULL;
2778
2779         if (WARN_ON(!wiphy))
2780                 return NULL;
2781
2782         if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
2783                     (data->signal < 0 || data->signal > 100)))
2784                 return NULL;
2785
2786         if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
2787                 ext = (void *) mgmt;
2788                 min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
2789                 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2790                         min_hdr_len = offsetof(struct ieee80211_ext,
2791                                                u.s1g_short_beacon.variable);
2792         }
2793
2794         if (WARN_ON(len < min_hdr_len))
2795                 return NULL;
2796
2797         ielen = len - min_hdr_len;
2798         variable = mgmt->u.probe_resp.variable;
2799         if (ext) {
2800                 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
2801                         variable = ext->u.s1g_short_beacon.variable;
2802                 else
2803                         variable = ext->u.s1g_beacon.variable;
2804         }
2805
2806         channel = cfg80211_get_bss_channel(wiphy, variable, ielen, data->chan);
2807         if (!channel)
2808                 return NULL;
2809
2810         if (ext) {
2811                 const struct ieee80211_s1g_bcn_compat_ie *compat;
2812                 const struct element *elem;
2813
2814                 elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
2815                                           variable, ielen);
2816                 if (!elem)
2817                         return NULL;
2818                 if (elem->datalen < sizeof(*compat))
2819                         return NULL;
2820                 compat = (void *)elem->data;
2821                 bssid = ext->u.s1g_beacon.sa;
2822                 capability = le16_to_cpu(compat->compat_info);
2823                 beacon_int = le16_to_cpu(compat->beacon_int);
2824         } else {
2825                 bssid = mgmt->bssid;
2826                 beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2827                 capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
2828         }
2829
2830         if (channel->band == NL80211_BAND_60GHZ) {
2831                 bss_type = capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
2832                 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
2833                     bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
2834                         regulatory_hint_found_beacon(wiphy, channel, gfp);
2835         } else {
2836                 if (capability & WLAN_CAPABILITY_ESS)
2837                         regulatory_hint_found_beacon(wiphy, channel, gfp);
2838         }
2839
2840         ies = kzalloc(sizeof(*ies) + ielen, gfp);
2841         if (!ies)
2842                 return NULL;
2843         ies->len = ielen;
2844         ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2845         ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
2846                            ieee80211_is_s1g_beacon(mgmt->frame_control);
2847         memcpy(ies->data, variable, ielen);
2848
2849         if (ieee80211_is_probe_resp(mgmt->frame_control))
2850                 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
2851         else
2852                 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
2853         rcu_assign_pointer(tmp.pub.ies, ies);
2854
2855         memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
2856         tmp.pub.beacon_interval = beacon_int;
2857         tmp.pub.capability = capability;
2858         tmp.pub.channel = channel;
2859         tmp.pub.signal = data->signal;
2860         tmp.ts_boottime = data->boottime_ns;
2861         tmp.parent_tsf = data->parent_tsf;
2862         tmp.pub.chains = data->chains;
2863         memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
2864         ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
2865
2866         signal_valid = data->chan == channel;
2867         spin_lock_bh(&rdev->bss_lock);
2868         res = __cfg80211_bss_update(rdev, &tmp, signal_valid, jiffies);
2869         if (!res)
2870                 goto drop;
2871
2872         rdev_inform_bss(rdev, &res->pub, ies, data->drv_data);
2873
2874         spin_unlock_bh(&rdev->bss_lock);
2875
2876         trace_cfg80211_return_bss(&res->pub);
2877         /* __cfg80211_bss_update gives us a referenced result */
2878         return &res->pub;
2879
2880 drop:
2881         spin_unlock_bh(&rdev->bss_lock);
2882         return NULL;
2883 }
2884
2885 struct cfg80211_bss *
2886 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2887                                struct cfg80211_inform_bss *data,
2888                                struct ieee80211_mgmt *mgmt, size_t len,
2889                                gfp_t gfp)
2890 {
2891         struct cfg80211_inform_single_bss_data inform_data = {
2892                 .drv_data = data,
2893                 .ie = mgmt->u.probe_resp.variable,
2894                 .ielen = len - offsetof(struct ieee80211_mgmt,
2895                                         u.probe_resp.variable),
2896         };
2897         struct cfg80211_bss *res;
2898
2899         res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
2900                                                     len, gfp);
2901         if (!res)
2902                 return NULL;
2903
2904         /* don't do any further MBSSID/ML handling for S1G */
2905         if (ieee80211_is_s1g_beacon(mgmt->frame_control))
2906                 return res;
2907
2908         inform_data.ftype = ieee80211_is_beacon(mgmt->frame_control) ?
2909                 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
2910         memcpy(inform_data.bssid, mgmt->bssid, ETH_ALEN);
2911         inform_data.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
2912         inform_data.beacon_interval =
2913                 le16_to_cpu(mgmt->u.probe_resp.beacon_int);
2914
2915         /* process each non-transmitting bss */
2916         cfg80211_parse_mbssid_data(wiphy, &inform_data, res, gfp);
2917
2918         cfg80211_parse_ml_sta_data(wiphy, &inform_data, res, gfp);
2919
2920         return res;
2921 }
2922 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
2923
2924 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2925 {
2926         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2927
2928         if (!pub)
2929                 return;
2930
2931         spin_lock_bh(&rdev->bss_lock);
2932         bss_ref_get(rdev, bss_from_pub(pub));
2933         spin_unlock_bh(&rdev->bss_lock);
2934 }
2935 EXPORT_SYMBOL(cfg80211_ref_bss);
2936
2937 void cfg80211_put_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_put(rdev, bss_from_pub(pub));
2946         spin_unlock_bh(&rdev->bss_lock);
2947 }
2948 EXPORT_SYMBOL(cfg80211_put_bss);
2949
2950 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2951 {
2952         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2953         struct cfg80211_internal_bss *bss, *tmp1;
2954         struct cfg80211_bss *nontrans_bss, *tmp;
2955
2956         if (WARN_ON(!pub))
2957                 return;
2958
2959         bss = bss_from_pub(pub);
2960
2961         spin_lock_bh(&rdev->bss_lock);
2962         if (list_empty(&bss->list))
2963                 goto out;
2964
2965         list_for_each_entry_safe(nontrans_bss, tmp,
2966                                  &pub->nontrans_list,
2967                                  nontrans_list) {
2968                 tmp1 = bss_from_pub(nontrans_bss);
2969                 if (__cfg80211_unlink_bss(rdev, tmp1))
2970                         rdev->bss_generation++;
2971         }
2972
2973         if (__cfg80211_unlink_bss(rdev, bss))
2974                 rdev->bss_generation++;
2975 out:
2976         spin_unlock_bh(&rdev->bss_lock);
2977 }
2978 EXPORT_SYMBOL(cfg80211_unlink_bss);
2979
2980 void cfg80211_bss_iter(struct wiphy *wiphy,
2981                        struct cfg80211_chan_def *chandef,
2982                        void (*iter)(struct wiphy *wiphy,
2983                                     struct cfg80211_bss *bss,
2984                                     void *data),
2985                        void *iter_data)
2986 {
2987         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2988         struct cfg80211_internal_bss *bss;
2989
2990         spin_lock_bh(&rdev->bss_lock);
2991
2992         list_for_each_entry(bss, &rdev->bss_list, list) {
2993                 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel,
2994                                                      false))
2995                         iter(wiphy, &bss->pub, iter_data);
2996         }
2997
2998         spin_unlock_bh(&rdev->bss_lock);
2999 }
3000 EXPORT_SYMBOL(cfg80211_bss_iter);
3001
3002 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
3003                                      unsigned int link_id,
3004                                      struct ieee80211_channel *chan)
3005 {
3006         struct wiphy *wiphy = wdev->wiphy;
3007         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3008         struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss;
3009         struct cfg80211_internal_bss *new = NULL;
3010         struct cfg80211_internal_bss *bss;
3011         struct cfg80211_bss *nontrans_bss;
3012         struct cfg80211_bss *tmp;
3013
3014         spin_lock_bh(&rdev->bss_lock);
3015
3016         /*
3017          * Some APs use CSA also for bandwidth changes, i.e., without actually
3018          * changing the control channel, so no need to update in such a case.
3019          */
3020         if (cbss->pub.channel == chan)
3021                 goto done;
3022
3023         /* use transmitting bss */
3024         if (cbss->pub.transmitted_bss)
3025                 cbss = bss_from_pub(cbss->pub.transmitted_bss);
3026
3027         cbss->pub.channel = chan;
3028
3029         list_for_each_entry(bss, &rdev->bss_list, list) {
3030                 if (!cfg80211_bss_type_match(bss->pub.capability,
3031                                              bss->pub.channel->band,
3032                                              wdev->conn_bss_type))
3033                         continue;
3034
3035                 if (bss == cbss)
3036                         continue;
3037
3038                 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
3039                         new = bss;
3040                         break;
3041                 }
3042         }
3043
3044         if (new) {
3045                 /* to save time, update IEs for transmitting bss only */
3046                 if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
3047                         new->pub.proberesp_ies = NULL;
3048                         new->pub.beacon_ies = NULL;
3049                 }
3050
3051                 list_for_each_entry_safe(nontrans_bss, tmp,
3052                                          &new->pub.nontrans_list,
3053                                          nontrans_list) {
3054                         bss = bss_from_pub(nontrans_bss);
3055                         if (__cfg80211_unlink_bss(rdev, bss))
3056                                 rdev->bss_generation++;
3057                 }
3058
3059                 WARN_ON(atomic_read(&new->hold));
3060                 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
3061                         rdev->bss_generation++;
3062         }
3063
3064         rb_erase(&cbss->rbn, &rdev->bss_tree);
3065         rb_insert_bss(rdev, cbss);
3066         rdev->bss_generation++;
3067
3068         list_for_each_entry_safe(nontrans_bss, tmp,
3069                                  &cbss->pub.nontrans_list,
3070                                  nontrans_list) {
3071                 bss = bss_from_pub(nontrans_bss);
3072                 bss->pub.channel = chan;
3073                 rb_erase(&bss->rbn, &rdev->bss_tree);
3074                 rb_insert_bss(rdev, bss);
3075                 rdev->bss_generation++;
3076         }
3077
3078 done:
3079         spin_unlock_bh(&rdev->bss_lock);
3080 }
3081
3082 #ifdef CONFIG_CFG80211_WEXT
3083 static struct cfg80211_registered_device *
3084 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
3085 {
3086         struct cfg80211_registered_device *rdev;
3087         struct net_device *dev;
3088
3089         ASSERT_RTNL();
3090
3091         dev = dev_get_by_index(net, ifindex);
3092         if (!dev)
3093                 return ERR_PTR(-ENODEV);
3094         if (dev->ieee80211_ptr)
3095                 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
3096         else
3097                 rdev = ERR_PTR(-ENODEV);
3098         dev_put(dev);
3099         return rdev;
3100 }
3101
3102 int cfg80211_wext_siwscan(struct net_device *dev,
3103                           struct iw_request_info *info,
3104                           union iwreq_data *wrqu, char *extra)
3105 {
3106         struct cfg80211_registered_device *rdev;
3107         struct wiphy *wiphy;
3108         struct iw_scan_req *wreq = NULL;
3109         struct cfg80211_scan_request *creq;
3110         int i, err, n_channels = 0;
3111         enum nl80211_band band;
3112
3113         if (!netif_running(dev))
3114                 return -ENETDOWN;
3115
3116         if (wrqu->data.length == sizeof(struct iw_scan_req))
3117                 wreq = (struct iw_scan_req *)extra;
3118
3119         rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3120
3121         if (IS_ERR(rdev))
3122                 return PTR_ERR(rdev);
3123
3124         if (rdev->scan_req || rdev->scan_msg)
3125                 return -EBUSY;
3126
3127         wiphy = &rdev->wiphy;
3128
3129         /* Determine number of channels, needed to allocate creq */
3130         if (wreq && wreq->num_channels)
3131                 n_channels = wreq->num_channels;
3132         else
3133                 n_channels = ieee80211_get_num_supported_channels(wiphy);
3134
3135         creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
3136                        n_channels * sizeof(void *),
3137                        GFP_ATOMIC);
3138         if (!creq)
3139                 return -ENOMEM;
3140
3141         creq->wiphy = wiphy;
3142         creq->wdev = dev->ieee80211_ptr;
3143         /* SSIDs come after channels */
3144         creq->ssids = (void *)&creq->channels[n_channels];
3145         creq->n_channels = n_channels;
3146         creq->n_ssids = 1;
3147         creq->scan_start = jiffies;
3148
3149         /* translate "Scan on frequencies" request */
3150         i = 0;
3151         for (band = 0; band < NUM_NL80211_BANDS; band++) {
3152                 int j;
3153
3154                 if (!wiphy->bands[band])
3155                         continue;
3156
3157                 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3158                         /* ignore disabled channels */
3159                         if (wiphy->bands[band]->channels[j].flags &
3160                                                 IEEE80211_CHAN_DISABLED)
3161                                 continue;
3162
3163                         /* If we have a wireless request structure and the
3164                          * wireless request specifies frequencies, then search
3165                          * for the matching hardware channel.
3166                          */
3167                         if (wreq && wreq->num_channels) {
3168                                 int k;
3169                                 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
3170                                 for (k = 0; k < wreq->num_channels; k++) {
3171                                         struct iw_freq *freq =
3172                                                 &wreq->channel_list[k];
3173                                         int wext_freq =
3174                                                 cfg80211_wext_freq(freq);
3175
3176                                         if (wext_freq == wiphy_freq)
3177                                                 goto wext_freq_found;
3178                                 }
3179                                 goto wext_freq_not_found;
3180                         }
3181
3182                 wext_freq_found:
3183                         creq->channels[i] = &wiphy->bands[band]->channels[j];
3184                         i++;
3185                 wext_freq_not_found: ;
3186                 }
3187         }
3188         /* No channels found? */
3189         if (!i) {
3190                 err = -EINVAL;
3191                 goto out;
3192         }
3193
3194         /* Set real number of channels specified in creq->channels[] */
3195         creq->n_channels = i;
3196
3197         /* translate "Scan for SSID" request */
3198         if (wreq) {
3199                 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
3200                         if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
3201                                 err = -EINVAL;
3202                                 goto out;
3203                         }
3204                         memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
3205                         creq->ssids[0].ssid_len = wreq->essid_len;
3206                 }
3207                 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
3208                         creq->n_ssids = 0;
3209         }
3210
3211         for (i = 0; i < NUM_NL80211_BANDS; i++)
3212                 if (wiphy->bands[i])
3213                         creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
3214
3215         eth_broadcast_addr(creq->bssid);
3216
3217         wiphy_lock(&rdev->wiphy);
3218
3219         rdev->scan_req = creq;
3220         err = rdev_scan(rdev, creq);
3221         if (err) {
3222                 rdev->scan_req = NULL;
3223                 /* creq will be freed below */
3224         } else {
3225                 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
3226                 /* creq now owned by driver */
3227                 creq = NULL;
3228                 dev_hold(dev);
3229         }
3230         wiphy_unlock(&rdev->wiphy);
3231  out:
3232         kfree(creq);
3233         return err;
3234 }
3235 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
3236
3237 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
3238                                     const struct cfg80211_bss_ies *ies,
3239                                     char *current_ev, char *end_buf)
3240 {
3241         const u8 *pos, *end, *next;
3242         struct iw_event iwe;
3243
3244         if (!ies)
3245                 return current_ev;
3246
3247         /*
3248          * If needed, fragment the IEs buffer (at IE boundaries) into short
3249          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
3250          */
3251         pos = ies->data;
3252         end = pos + ies->len;
3253
3254         while (end - pos > IW_GENERIC_IE_MAX) {
3255                 next = pos + 2 + pos[1];
3256                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
3257                         next = next + 2 + next[1];
3258
3259                 memset(&iwe, 0, sizeof(iwe));
3260                 iwe.cmd = IWEVGENIE;
3261                 iwe.u.data.length = next - pos;
3262                 current_ev = iwe_stream_add_point_check(info, current_ev,
3263                                                         end_buf, &iwe,
3264                                                         (void *)pos);
3265                 if (IS_ERR(current_ev))
3266                         return current_ev;
3267                 pos = next;
3268         }
3269
3270         if (end > pos) {
3271                 memset(&iwe, 0, sizeof(iwe));
3272                 iwe.cmd = IWEVGENIE;
3273                 iwe.u.data.length = end - pos;
3274                 current_ev = iwe_stream_add_point_check(info, current_ev,
3275                                                         end_buf, &iwe,
3276                                                         (void *)pos);
3277                 if (IS_ERR(current_ev))
3278                         return current_ev;
3279         }
3280
3281         return current_ev;
3282 }
3283
3284 static char *
3285 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
3286               struct cfg80211_internal_bss *bss, char *current_ev,
3287               char *end_buf)
3288 {
3289         const struct cfg80211_bss_ies *ies;
3290         struct iw_event iwe;
3291         const u8 *ie;
3292         u8 buf[50];
3293         u8 *cfg, *p, *tmp;
3294         int rem, i, sig;
3295         bool ismesh = false;
3296
3297         memset(&iwe, 0, sizeof(iwe));
3298         iwe.cmd = SIOCGIWAP;
3299         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3300         memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
3301         current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
3302                                                 IW_EV_ADDR_LEN);
3303         if (IS_ERR(current_ev))
3304                 return current_ev;
3305
3306         memset(&iwe, 0, sizeof(iwe));
3307         iwe.cmd = SIOCGIWFREQ;
3308         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
3309         iwe.u.freq.e = 0;
3310         current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
3311                                                 IW_EV_FREQ_LEN);
3312         if (IS_ERR(current_ev))
3313                 return current_ev;
3314
3315         memset(&iwe, 0, sizeof(iwe));
3316         iwe.cmd = SIOCGIWFREQ;
3317         iwe.u.freq.m = bss->pub.channel->center_freq;
3318         iwe.u.freq.e = 6;
3319         current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
3320                                                 IW_EV_FREQ_LEN);
3321         if (IS_ERR(current_ev))
3322                 return current_ev;
3323
3324         if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
3325                 memset(&iwe, 0, sizeof(iwe));
3326                 iwe.cmd = IWEVQUAL;
3327                 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
3328                                      IW_QUAL_NOISE_INVALID |
3329                                      IW_QUAL_QUAL_UPDATED;
3330                 switch (wiphy->signal_type) {
3331                 case CFG80211_SIGNAL_TYPE_MBM:
3332                         sig = bss->pub.signal / 100;
3333                         iwe.u.qual.level = sig;
3334                         iwe.u.qual.updated |= IW_QUAL_DBM;
3335                         if (sig < -110)         /* rather bad */
3336                                 sig = -110;
3337                         else if (sig > -40)     /* perfect */
3338                                 sig = -40;
3339                         /* will give a range of 0 .. 70 */
3340                         iwe.u.qual.qual = sig + 110;
3341                         break;
3342                 case CFG80211_SIGNAL_TYPE_UNSPEC:
3343                         iwe.u.qual.level = bss->pub.signal;
3344                         /* will give range 0 .. 100 */
3345                         iwe.u.qual.qual = bss->pub.signal;
3346                         break;
3347                 default:
3348                         /* not reached */
3349                         break;
3350                 }
3351                 current_ev = iwe_stream_add_event_check(info, current_ev,
3352                                                         end_buf, &iwe,
3353                                                         IW_EV_QUAL_LEN);
3354                 if (IS_ERR(current_ev))
3355                         return current_ev;
3356         }
3357
3358         memset(&iwe, 0, sizeof(iwe));
3359         iwe.cmd = SIOCGIWENCODE;
3360         if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
3361                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3362         else
3363                 iwe.u.data.flags = IW_ENCODE_DISABLED;
3364         iwe.u.data.length = 0;
3365         current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3366                                                 &iwe, "");
3367         if (IS_ERR(current_ev))
3368                 return current_ev;
3369
3370         rcu_read_lock();
3371         ies = rcu_dereference(bss->pub.ies);
3372         rem = ies->len;
3373         ie = ies->data;
3374
3375         while (rem >= 2) {
3376                 /* invalid data */
3377                 if (ie[1] > rem - 2)
3378                         break;
3379
3380                 switch (ie[0]) {
3381                 case WLAN_EID_SSID:
3382                         memset(&iwe, 0, sizeof(iwe));
3383                         iwe.cmd = SIOCGIWESSID;
3384                         iwe.u.data.length = ie[1];
3385                         iwe.u.data.flags = 1;
3386                         current_ev = iwe_stream_add_point_check(info,
3387                                                                 current_ev,
3388                                                                 end_buf, &iwe,
3389                                                                 (u8 *)ie + 2);
3390                         if (IS_ERR(current_ev))
3391                                 goto unlock;
3392                         break;
3393                 case WLAN_EID_MESH_ID:
3394                         memset(&iwe, 0, sizeof(iwe));
3395                         iwe.cmd = SIOCGIWESSID;
3396                         iwe.u.data.length = ie[1];
3397                         iwe.u.data.flags = 1;
3398                         current_ev = iwe_stream_add_point_check(info,
3399                                                                 current_ev,
3400                                                                 end_buf, &iwe,
3401                                                                 (u8 *)ie + 2);
3402                         if (IS_ERR(current_ev))
3403                                 goto unlock;
3404                         break;
3405                 case WLAN_EID_MESH_CONFIG:
3406                         ismesh = true;
3407                         if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
3408                                 break;
3409                         cfg = (u8 *)ie + 2;
3410                         memset(&iwe, 0, sizeof(iwe));
3411                         iwe.cmd = IWEVCUSTOM;
3412                         iwe.u.data.length = sprintf(buf,
3413                                                     "Mesh Network Path Selection Protocol ID: 0x%02X",
3414                                                     cfg[0]);
3415                         current_ev = iwe_stream_add_point_check(info,
3416                                                                 current_ev,
3417                                                                 end_buf,
3418                                                                 &iwe, buf);
3419                         if (IS_ERR(current_ev))
3420                                 goto unlock;
3421                         iwe.u.data.length = sprintf(buf,
3422                                                     "Path Selection Metric ID: 0x%02X",
3423                                                     cfg[1]);
3424                         current_ev = iwe_stream_add_point_check(info,
3425                                                                 current_ev,
3426                                                                 end_buf,
3427                                                                 &iwe, buf);
3428                         if (IS_ERR(current_ev))
3429                                 goto unlock;
3430                         iwe.u.data.length = sprintf(buf,
3431                                                     "Congestion Control Mode ID: 0x%02X",
3432                                                     cfg[2]);
3433                         current_ev = iwe_stream_add_point_check(info,
3434                                                                 current_ev,
3435                                                                 end_buf,
3436                                                                 &iwe, buf);
3437                         if (IS_ERR(current_ev))
3438                                 goto unlock;
3439                         iwe.u.data.length = sprintf(buf,
3440                                                     "Synchronization ID: 0x%02X",
3441                                                     cfg[3]);
3442                         current_ev = iwe_stream_add_point_check(info,
3443                                                                 current_ev,
3444                                                                 end_buf,
3445                                                                 &iwe, buf);
3446                         if (IS_ERR(current_ev))
3447                                 goto unlock;
3448                         iwe.u.data.length = sprintf(buf,
3449                                                     "Authentication ID: 0x%02X",
3450                                                     cfg[4]);
3451                         current_ev = iwe_stream_add_point_check(info,
3452                                                                 current_ev,
3453                                                                 end_buf,
3454                                                                 &iwe, buf);
3455                         if (IS_ERR(current_ev))
3456                                 goto unlock;
3457                         iwe.u.data.length = sprintf(buf,
3458                                                     "Formation Info: 0x%02X",
3459                                                     cfg[5]);
3460                         current_ev = iwe_stream_add_point_check(info,
3461                                                                 current_ev,
3462                                                                 end_buf,
3463                                                                 &iwe, buf);
3464                         if (IS_ERR(current_ev))
3465                                 goto unlock;
3466                         iwe.u.data.length = sprintf(buf,
3467                                                     "Capabilities: 0x%02X",
3468                                                     cfg[6]);
3469                         current_ev = iwe_stream_add_point_check(info,
3470                                                                 current_ev,
3471                                                                 end_buf,
3472                                                                 &iwe, buf);
3473                         if (IS_ERR(current_ev))
3474                                 goto unlock;
3475                         break;
3476                 case WLAN_EID_SUPP_RATES:
3477                 case WLAN_EID_EXT_SUPP_RATES:
3478                         /* display all supported rates in readable format */
3479                         p = current_ev + iwe_stream_lcp_len(info);
3480
3481                         memset(&iwe, 0, sizeof(iwe));
3482                         iwe.cmd = SIOCGIWRATE;
3483                         /* Those two flags are ignored... */
3484                         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3485
3486                         for (i = 0; i < ie[1]; i++) {
3487                                 iwe.u.bitrate.value =
3488                                         ((ie[i + 2] & 0x7f) * 500000);
3489                                 tmp = p;
3490                                 p = iwe_stream_add_value(info, current_ev, p,
3491                                                          end_buf, &iwe,
3492                                                          IW_EV_PARAM_LEN);
3493                                 if (p == tmp) {
3494                                         current_ev = ERR_PTR(-E2BIG);
3495                                         goto unlock;
3496                                 }
3497                         }
3498                         current_ev = p;
3499                         break;
3500                 }
3501                 rem -= ie[1] + 2;
3502                 ie += ie[1] + 2;
3503         }
3504
3505         if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
3506             ismesh) {
3507                 memset(&iwe, 0, sizeof(iwe));
3508                 iwe.cmd = SIOCGIWMODE;
3509                 if (ismesh)
3510                         iwe.u.mode = IW_MODE_MESH;
3511                 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
3512                         iwe.u.mode = IW_MODE_MASTER;
3513                 else
3514                         iwe.u.mode = IW_MODE_ADHOC;
3515                 current_ev = iwe_stream_add_event_check(info, current_ev,
3516                                                         end_buf, &iwe,
3517                                                         IW_EV_UINT_LEN);
3518                 if (IS_ERR(current_ev))
3519                         goto unlock;
3520         }
3521
3522         memset(&iwe, 0, sizeof(iwe));
3523         iwe.cmd = IWEVCUSTOM;
3524         iwe.u.data.length = sprintf(buf, "tsf=%016llx",
3525                                     (unsigned long long)(ies->tsf));
3526         current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
3527                                                 &iwe, buf);
3528         if (IS_ERR(current_ev))
3529                 goto unlock;
3530         memset(&iwe, 0, sizeof(iwe));
3531         iwe.cmd = IWEVCUSTOM;
3532         iwe.u.data.length = sprintf(buf, " Last beacon: %ums ago",
3533                                     elapsed_jiffies_msecs(bss->ts));
3534         current_ev = iwe_stream_add_point_check(info, current_ev,
3535                                                 end_buf, &iwe, buf);
3536         if (IS_ERR(current_ev))
3537                 goto unlock;
3538
3539         current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
3540
3541  unlock:
3542         rcu_read_unlock();
3543         return current_ev;
3544 }
3545
3546
3547 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
3548                                   struct iw_request_info *info,
3549                                   char *buf, size_t len)
3550 {
3551         char *current_ev = buf;
3552         char *end_buf = buf + len;
3553         struct cfg80211_internal_bss *bss;
3554         int err = 0;
3555
3556         spin_lock_bh(&rdev->bss_lock);
3557         cfg80211_bss_expire(rdev);
3558
3559         list_for_each_entry(bss, &rdev->bss_list, list) {
3560                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3561                         err = -E2BIG;
3562                         break;
3563                 }
3564                 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
3565                                            current_ev, end_buf);
3566                 if (IS_ERR(current_ev)) {
3567                         err = PTR_ERR(current_ev);
3568                         break;
3569                 }
3570         }
3571         spin_unlock_bh(&rdev->bss_lock);
3572
3573         if (err)
3574                 return err;
3575         return current_ev - buf;
3576 }
3577
3578
3579 int cfg80211_wext_giwscan(struct net_device *dev,
3580                           struct iw_request_info *info,
3581                           union iwreq_data *wrqu, char *extra)
3582 {
3583         struct iw_point *data = &wrqu->data;
3584         struct cfg80211_registered_device *rdev;
3585         int res;
3586
3587         if (!netif_running(dev))
3588                 return -ENETDOWN;
3589
3590         rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
3591
3592         if (IS_ERR(rdev))
3593                 return PTR_ERR(rdev);
3594
3595         if (rdev->scan_req || rdev->scan_msg)
3596                 return -EAGAIN;
3597
3598         res = ieee80211_scan_results(rdev, info, extra, data->length);
3599         data->length = 0;
3600         if (res >= 0) {
3601                 data->length = res;
3602                 res = 0;
3603         }
3604
3605         return res;
3606 }
3607 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
3608 #endif
This page took 0.263761 seconds and 4 git commands to generate.