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