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