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