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