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