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 res = NULL; 2457 } 2458 } 2459 2460 if (!res) 2461 goto drop; 2462 } 2463 spin_unlock_bh(&rdev->bss_lock); 2464 2465 trace_cfg80211_return_bss(&res->pub); 2466 /* __cfg80211_bss_update gives us a referenced result */ 2467 return &res->pub; 2468 2469 drop: 2470 spin_unlock_bh(&rdev->bss_lock); 2471 return NULL; 2472 } 2473 2474 static bool cfg80211_iter_profile_continuation(const u8 *ie, size_t ielen, 2475 const struct element **mbssid, 2476 const struct element **sub_elem) 2477 { 2478 const u8 *mbssid_end = (*mbssid)->data + (*mbssid)->datalen; 2479 const struct element *next_mbssid; 2480 const struct element *next_sub; 2481 2482 next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, 2483 mbssid_end, 2484 ielen - (mbssid_end - ie)); 2485 2486 /* 2487 * If it is not the last subelement in current MBSSID IE or there isn't 2488 * a next MBSSID IE - profile is complete. 2489 */ 2490 if (((*sub_elem)->data + (*sub_elem)->datalen < mbssid_end - 1) || 2491 !next_mbssid) 2492 return false; 2493 2494 /* For any length error, just return false to stop iteration */ 2495 2496 if (next_mbssid->datalen < 4) 2497 return false; 2498 2499 next_sub = (void *)&next_mbssid->data[1]; 2500 2501 if (next_mbssid->data + next_mbssid->datalen < 2502 next_sub->data + next_sub->datalen) 2503 return false; 2504 2505 if (next_sub->id != 0 || next_sub->datalen < 2) 2506 return false; 2507 2508 /* 2509 * Check if the first element in the next sub element is a start 2510 * of a new profile 2511 */ 2512 if (next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP) 2513 return false; 2514 2515 *mbssid = next_mbssid; 2516 *sub_elem = next_sub; 2517 return true; 2518 } 2519 2520 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen, 2521 const struct element *mbssid_elem, 2522 const struct element *sub_elem, 2523 u8 *merged_ie, size_t max_copy_len) 2524 { 2525 size_t copied_len = sub_elem->datalen; 2526 2527 if (sub_elem->datalen > max_copy_len) 2528 return 0; 2529 2530 memcpy(merged_ie, sub_elem->data, sub_elem->datalen); 2531 2532 while (cfg80211_iter_profile_continuation(ie, ielen, 2533 &mbssid_elem, 2534 &sub_elem)) { 2535 if (copied_len + sub_elem->datalen > max_copy_len) 2536 break; 2537 memcpy(merged_ie + copied_len, sub_elem->data, 2538 sub_elem->datalen); 2539 copied_len += sub_elem->datalen; 2540 } 2541 2542 return copied_len; 2543 } 2544 EXPORT_SYMBOL(cfg80211_merge_profile); 2545 2546 static void 2547 cfg80211_parse_mbssid_data(struct wiphy *wiphy, 2548 struct cfg80211_inform_single_bss_data *tx_data, 2549 struct cfg80211_bss *source_bss, 2550 gfp_t gfp) 2551 { 2552 struct cfg80211_inform_single_bss_data data = { 2553 .drv_data = tx_data->drv_data, 2554 .ftype = tx_data->ftype, 2555 .tsf = tx_data->tsf, 2556 .beacon_interval = tx_data->beacon_interval, 2557 .source_bss = source_bss, 2558 .bss_source = BSS_SOURCE_MBSSID, 2559 .use_for = tx_data->use_for, 2560 .cannot_use_reasons = tx_data->cannot_use_reasons, 2561 }; 2562 const u8 *mbssid_index_ie; 2563 const struct element *elem, *sub; 2564 u8 *new_ie, *profile; 2565 u64 seen_indices = 0; 2566 struct cfg80211_bss *bss; 2567 2568 if (!source_bss) 2569 return; 2570 if (!cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID, 2571 tx_data->ie, tx_data->ielen)) 2572 return; 2573 if (!wiphy->support_mbssid) 2574 return; 2575 if (wiphy->support_only_he_mbssid && 2576 !cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, 2577 tx_data->ie, tx_data->ielen)) 2578 return; 2579 2580 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp); 2581 if (!new_ie) 2582 return; 2583 2584 profile = kmalloc(tx_data->ielen, gfp); 2585 if (!profile) 2586 goto out; 2587 2588 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, 2589 tx_data->ie, tx_data->ielen) { 2590 if (elem->datalen < 4) 2591 continue; 2592 if (elem->data[0] < 1 || (int)elem->data[0] > 8) 2593 continue; 2594 for_each_element(sub, elem->data + 1, elem->datalen - 1) { 2595 u8 profile_len; 2596 2597 if (sub->id != 0 || sub->datalen < 4) { 2598 /* not a valid BSS profile */ 2599 continue; 2600 } 2601 2602 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP || 2603 sub->data[1] != 2) { 2604 /* The first element within the Nontransmitted 2605 * BSSID Profile is not the Nontransmitted 2606 * BSSID Capability element. 2607 */ 2608 continue; 2609 } 2610 2611 memset(profile, 0, tx_data->ielen); 2612 profile_len = cfg80211_merge_profile(tx_data->ie, 2613 tx_data->ielen, 2614 elem, 2615 sub, 2616 profile, 2617 tx_data->ielen); 2618 2619 /* found a Nontransmitted BSSID Profile */ 2620 mbssid_index_ie = cfg80211_find_ie 2621 (WLAN_EID_MULTI_BSSID_IDX, 2622 profile, profile_len); 2623 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 || 2624 mbssid_index_ie[2] == 0 || 2625 mbssid_index_ie[2] > 46 || 2626 mbssid_index_ie[2] >= (1 << elem->data[0])) { 2627 /* No valid Multiple BSSID-Index element */ 2628 continue; 2629 } 2630 2631 if (seen_indices & BIT_ULL(mbssid_index_ie[2])) 2632 /* We don't support legacy split of a profile */ 2633 net_dbg_ratelimited("Partial info for BSSID index %d\n", 2634 mbssid_index_ie[2]); 2635 2636 seen_indices |= BIT_ULL(mbssid_index_ie[2]); 2637 2638 data.bssid_index = mbssid_index_ie[2]; 2639 data.max_bssid_indicator = elem->data[0]; 2640 2641 cfg80211_gen_new_bssid(tx_data->bssid, 2642 data.max_bssid_indicator, 2643 data.bssid_index, 2644 data.bssid); 2645 2646 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN); 2647 data.ie = new_ie; 2648 data.ielen = cfg80211_gen_new_ie(tx_data->ie, 2649 tx_data->ielen, 2650 profile, 2651 profile_len, 2652 new_ie, 2653 IEEE80211_MAX_DATA_LEN); 2654 if (!data.ielen) 2655 continue; 2656 2657 data.capability = get_unaligned_le16(profile + 2); 2658 bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp); 2659 if (!bss) 2660 break; 2661 cfg80211_put_bss(wiphy, bss); 2662 } 2663 } 2664 2665 out: 2666 kfree(new_ie); 2667 kfree(profile); 2668 } 2669 2670 ssize_t cfg80211_defragment_element(const struct element *elem, const u8 *ies, 2671 size_t ieslen, u8 *data, size_t data_len, 2672 u8 frag_id) 2673 { 2674 const struct element *next; 2675 ssize_t copied; 2676 u8 elem_datalen; 2677 2678 if (!elem || (const u8 *)elem < ies || 2679 (const u8 *)elem + sizeof(*elem) > ies + ieslen || 2680 (const u8 *)elem + sizeof(*elem) + elem->datalen > ies + ieslen) 2681 return -EINVAL; 2682 2683 /* elem might be invalid after the memmove */ 2684 next = (void *)(elem->data + elem->datalen); 2685 elem_datalen = elem->datalen; 2686 2687 if (elem->id == WLAN_EID_EXTENSION) { 2688 copied = elem->datalen - 1; 2689 2690 if (data) { 2691 if (copied > data_len) 2692 return -ENOSPC; 2693 2694 memmove(data, elem->data + 1, copied); 2695 } 2696 } else { 2697 copied = elem->datalen; 2698 2699 if (data) { 2700 if (copied > data_len) 2701 return -ENOSPC; 2702 2703 memmove(data, elem->data, copied); 2704 } 2705 } 2706 2707 /* Fragmented elements must have 255 bytes */ 2708 if (elem_datalen < 255) 2709 return copied; 2710 2711 for (elem = next; 2712 elem->data < ies + ieslen && 2713 elem->data + elem->datalen <= ies + ieslen; 2714 elem = next) { 2715 /* elem might be invalid after the memmove */ 2716 next = (void *)(elem->data + elem->datalen); 2717 2718 if (elem->id != frag_id) 2719 break; 2720 2721 elem_datalen = elem->datalen; 2722 2723 if (data) { 2724 if (copied + elem_datalen > data_len) 2725 return -ENOSPC; 2726 2727 memmove(data + copied, elem->data, elem_datalen); 2728 } 2729 2730 copied += elem_datalen; 2731 2732 /* Only the last fragment may be short */ 2733 if (elem_datalen != 255) 2734 break; 2735 } 2736 2737 return copied; 2738 } 2739 EXPORT_SYMBOL(cfg80211_defragment_element); 2740 2741 struct cfg80211_mle { 2742 struct ieee80211_multi_link_elem *mle; 2743 struct ieee80211_mle_per_sta_profile 2744 *sta_prof[IEEE80211_MLD_MAX_NUM_LINKS]; 2745 ssize_t sta_prof_len[IEEE80211_MLD_MAX_NUM_LINKS]; 2746 2747 u8 data[]; 2748 }; 2749 2750 static struct cfg80211_mle * 2751 cfg80211_defrag_mle(const struct element *mle, const u8 *ie, size_t ielen, 2752 gfp_t gfp) 2753 { 2754 const struct element *elem; 2755 struct cfg80211_mle *res; 2756 size_t buf_len; 2757 ssize_t mle_len; 2758 u8 common_size, idx; 2759 2760 if (!mle || !ieee80211_mle_size_ok(mle->data + 1, mle->datalen - 1)) 2761 return NULL; 2762 2763 /* Required length for first defragmentation */ 2764 buf_len = mle->datalen - 1; 2765 for_each_element(elem, mle->data + mle->datalen, 2766 ie + ielen - mle->data - mle->datalen) { 2767 if (elem->id != WLAN_EID_FRAGMENT) 2768 break; 2769 2770 buf_len += elem->datalen; 2771 } 2772 2773 res = kzalloc_flex(*res, data, buf_len, gfp); 2774 if (!res) 2775 return NULL; 2776 2777 mle_len = cfg80211_defragment_element(mle, ie, ielen, 2778 res->data, buf_len, 2779 WLAN_EID_FRAGMENT); 2780 if (mle_len < 0) 2781 goto error; 2782 2783 res->mle = (void *)res->data; 2784 2785 /* Find the sub-element area in the buffer */ 2786 common_size = ieee80211_mle_common_size((u8 *)res->mle); 2787 ie = res->data + common_size; 2788 ielen = mle_len - common_size; 2789 2790 idx = 0; 2791 for_each_element_id(elem, (u8)IEEE80211_MLE_SUBELEM_PER_STA_PROFILE, 2792 ie, ielen) { 2793 res->sta_prof[idx] = (void *)elem->data; 2794 res->sta_prof_len[idx] = elem->datalen; 2795 2796 idx++; 2797 if (idx >= IEEE80211_MLD_MAX_NUM_LINKS) 2798 break; 2799 } 2800 if (!for_each_element_completed(elem, ie, ielen)) 2801 goto error; 2802 2803 /* Defragment sta_info in-place */ 2804 for (idx = 0; idx < IEEE80211_MLD_MAX_NUM_LINKS && res->sta_prof[idx]; 2805 idx++) { 2806 if (res->sta_prof_len[idx] < 255) 2807 continue; 2808 2809 elem = (void *)res->sta_prof[idx] - 2; 2810 2811 if (idx + 1 < ARRAY_SIZE(res->sta_prof) && 2812 res->sta_prof[idx + 1]) 2813 buf_len = (u8 *)res->sta_prof[idx + 1] - 2814 (u8 *)res->sta_prof[idx]; 2815 else 2816 buf_len = ielen + ie - (u8 *)elem; 2817 2818 res->sta_prof_len[idx] = 2819 cfg80211_defragment_element(elem, 2820 (u8 *)elem, buf_len, 2821 (u8 *)res->sta_prof[idx], 2822 buf_len, 2823 IEEE80211_MLE_SUBELEM_FRAGMENT); 2824 if (res->sta_prof_len[idx] < 0) 2825 goto error; 2826 } 2827 2828 return res; 2829 2830 error: 2831 kfree(res); 2832 return NULL; 2833 } 2834 2835 struct tbtt_info_iter_data { 2836 const struct ieee80211_neighbor_ap_info *ap_info; 2837 u8 param_ch_count; 2838 u32 use_for; 2839 u8 mld_id, link_id; 2840 bool non_tx; 2841 }; 2842 2843 static enum cfg80211_rnr_iter_ret 2844 cfg802121_mld_ap_rnr_iter(void *_data, u8 type, 2845 const struct ieee80211_neighbor_ap_info *info, 2846 const u8 *tbtt_info, u8 tbtt_info_len) 2847 { 2848 const struct ieee80211_rnr_mld_params *mld_params; 2849 struct tbtt_info_iter_data *data = _data; 2850 u8 link_id; 2851 bool non_tx = false; 2852 2853 if (type == IEEE80211_TBTT_INFO_TYPE_TBTT && 2854 tbtt_info_len >= offsetofend(struct ieee80211_tbtt_info_ge_11, 2855 mld_params)) { 2856 const struct ieee80211_tbtt_info_ge_11 *tbtt_info_ge_11 = 2857 (void *)tbtt_info; 2858 2859 non_tx = (tbtt_info_ge_11->bss_params & 2860 (IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID | 2861 IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID)) == 2862 IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID; 2863 mld_params = &tbtt_info_ge_11->mld_params; 2864 } else if (type == IEEE80211_TBTT_INFO_TYPE_MLD && 2865 tbtt_info_len >= sizeof(struct ieee80211_rnr_mld_params)) 2866 mld_params = (void *)tbtt_info; 2867 else 2868 return RNR_ITER_CONTINUE; 2869 2870 link_id = le16_get_bits(mld_params->params, 2871 IEEE80211_RNR_MLD_PARAMS_LINK_ID); 2872 2873 if (data->mld_id != mld_params->mld_id) 2874 return RNR_ITER_CONTINUE; 2875 2876 if (data->link_id != link_id) 2877 return RNR_ITER_CONTINUE; 2878 2879 data->ap_info = info; 2880 data->param_ch_count = 2881 le16_get_bits(mld_params->params, 2882 IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT); 2883 data->non_tx = non_tx; 2884 2885 if (type == IEEE80211_TBTT_INFO_TYPE_TBTT) 2886 data->use_for = NL80211_BSS_USE_FOR_ALL; 2887 else 2888 data->use_for = NL80211_BSS_USE_FOR_MLD_LINK; 2889 return RNR_ITER_BREAK; 2890 } 2891 2892 static u8 2893 cfg80211_rnr_info_for_mld_ap(const u8 *ie, size_t ielen, u8 mld_id, u8 link_id, 2894 const struct ieee80211_neighbor_ap_info **ap_info, 2895 u8 *param_ch_count, bool *non_tx) 2896 { 2897 struct tbtt_info_iter_data data = { 2898 .mld_id = mld_id, 2899 .link_id = link_id, 2900 }; 2901 2902 cfg80211_iter_rnr(ie, ielen, cfg802121_mld_ap_rnr_iter, &data); 2903 2904 *ap_info = data.ap_info; 2905 *param_ch_count = data.param_ch_count; 2906 *non_tx = data.non_tx; 2907 2908 return data.use_for; 2909 } 2910 2911 static struct element * 2912 cfg80211_gen_reporter_rnr(struct cfg80211_bss *source_bss, bool is_mbssid, 2913 bool same_mld, u8 link_id, u8 bss_change_count, 2914 gfp_t gfp) 2915 { 2916 const struct cfg80211_bss_ies *ies; 2917 struct ieee80211_neighbor_ap_info ap_info; 2918 struct ieee80211_tbtt_info_ge_11 tbtt_info; 2919 u32 short_ssid; 2920 const struct element *elem; 2921 struct element *res; 2922 2923 /* 2924 * We only generate the RNR to permit ML lookups. For that we do not 2925 * need an entry for the corresponding transmitting BSS, lets just skip 2926 * it even though it would be easy to add. 2927 */ 2928 if (!same_mld) 2929 return NULL; 2930 2931 /* We could use tx_data->ies if we change cfg80211_calc_short_ssid */ 2932 rcu_read_lock(); 2933 ies = rcu_dereference(source_bss->ies); 2934 2935 ap_info.tbtt_info_len = offsetofend(typeof(tbtt_info), mld_params); 2936 ap_info.tbtt_info_hdr = 2937 u8_encode_bits(IEEE80211_TBTT_INFO_TYPE_TBTT, 2938 IEEE80211_AP_INFO_TBTT_HDR_TYPE) | 2939 u8_encode_bits(0, IEEE80211_AP_INFO_TBTT_HDR_COUNT); 2940 2941 ap_info.channel = ieee80211_frequency_to_channel(source_bss->channel->center_freq); 2942 2943 /* operating class */ 2944 elem = cfg80211_find_elem(WLAN_EID_SUPPORTED_REGULATORY_CLASSES, 2945 ies->data, ies->len); 2946 if (elem && elem->datalen >= 1) { 2947 ap_info.op_class = elem->data[0]; 2948 } else { 2949 struct cfg80211_chan_def chandef; 2950 2951 /* The AP is not providing us with anything to work with. So 2952 * make up a somewhat reasonable operating class, but don't 2953 * bother with it too much as no one will ever use the 2954 * information. 2955 */ 2956 cfg80211_chandef_create(&chandef, source_bss->channel, 2957 NL80211_CHAN_NO_HT); 2958 2959 if (!ieee80211_chandef_to_operating_class(&chandef, 2960 &ap_info.op_class)) 2961 goto out_unlock; 2962 } 2963 2964 /* Just set TBTT offset and PSD 20 to invalid/unknown */ 2965 tbtt_info.tbtt_offset = 255; 2966 tbtt_info.psd_20 = IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED; 2967 2968 memcpy(tbtt_info.bssid, source_bss->bssid, ETH_ALEN); 2969 if (cfg80211_calc_short_ssid(ies, &elem, &short_ssid)) 2970 goto out_unlock; 2971 2972 rcu_read_unlock(); 2973 2974 tbtt_info.short_ssid = cpu_to_le32(short_ssid); 2975 2976 tbtt_info.bss_params = IEEE80211_RNR_TBTT_PARAMS_SAME_SSID; 2977 2978 if (is_mbssid) { 2979 tbtt_info.bss_params |= IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID; 2980 tbtt_info.bss_params |= IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID; 2981 } 2982 2983 tbtt_info.mld_params.mld_id = 0; 2984 tbtt_info.mld_params.params = 2985 le16_encode_bits(link_id, IEEE80211_RNR_MLD_PARAMS_LINK_ID) | 2986 le16_encode_bits(bss_change_count, 2987 IEEE80211_RNR_MLD_PARAMS_BSS_CHANGE_COUNT); 2988 2989 res = kzalloc_flex(*res, data, sizeof(ap_info) + ap_info.tbtt_info_len, 2990 gfp); 2991 if (!res) 2992 return NULL; 2993 2994 /* Copy the data */ 2995 res->id = WLAN_EID_REDUCED_NEIGHBOR_REPORT; 2996 res->datalen = sizeof(ap_info) + ap_info.tbtt_info_len; 2997 memcpy(res->data, &ap_info, sizeof(ap_info)); 2998 memcpy(res->data + sizeof(ap_info), &tbtt_info, ap_info.tbtt_info_len); 2999 3000 return res; 3001 3002 out_unlock: 3003 rcu_read_unlock(); 3004 return NULL; 3005 } 3006 3007 static void 3008 cfg80211_parse_ml_elem_sta_data(struct wiphy *wiphy, 3009 struct cfg80211_inform_single_bss_data *tx_data, 3010 struct cfg80211_bss *source_bss, 3011 const struct element *elem, 3012 gfp_t gfp) 3013 { 3014 struct cfg80211_inform_single_bss_data data = { 3015 .drv_data = tx_data->drv_data, 3016 .ftype = tx_data->ftype, 3017 .source_bss = source_bss, 3018 .bss_source = BSS_SOURCE_STA_PROFILE, 3019 }; 3020 struct element *reporter_rnr = NULL; 3021 struct ieee80211_multi_link_elem *ml_elem; 3022 struct cfg80211_mle *mle; 3023 const struct element *ssid_elem; 3024 const u8 *ssid = NULL; 3025 size_t ssid_len = 0; 3026 u16 control; 3027 u8 ml_common_len; 3028 u8 *new_ie = NULL; 3029 struct cfg80211_bss *bss; 3030 u8 mld_id, reporter_link_id, bss_change_count; 3031 u16 seen_links = 0; 3032 u8 i; 3033 3034 if (!ieee80211_mle_type_ok(elem->data + 1, 3035 IEEE80211_ML_CONTROL_TYPE_BASIC, 3036 elem->datalen - 1)) 3037 return; 3038 3039 ml_elem = (void *)(elem->data + 1); 3040 control = le16_to_cpu(ml_elem->control); 3041 ml_common_len = ml_elem->variable[0]; 3042 3043 /* Must be present when transmitted by an AP (in a probe response) */ 3044 if (!(control & IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT) || 3045 !(control & IEEE80211_MLC_BASIC_PRES_LINK_ID) || 3046 !(control & IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP)) 3047 return; 3048 3049 reporter_link_id = ieee80211_mle_get_link_id(elem->data + 1); 3050 bss_change_count = ieee80211_mle_get_bss_param_ch_cnt(elem->data + 1); 3051 3052 /* 3053 * The MLD ID of the reporting AP is always zero. It is set if the AP 3054 * is part of an MBSSID set and will be non-zero for ML Elements 3055 * relating to a nontransmitted BSS (matching the Multi-BSSID Index, 3056 * Draft P802.11be_D3.2, 35.3.4.2) 3057 */ 3058 mld_id = ieee80211_mle_get_mld_id(elem->data + 1); 3059 3060 /* Fully defrag the ML element for sta information/profile iteration */ 3061 mle = cfg80211_defrag_mle(elem, tx_data->ie, tx_data->ielen, gfp); 3062 if (!mle) 3063 return; 3064 3065 /* No point in doing anything if there is no per-STA profile */ 3066 if (!mle->sta_prof[0]) 3067 goto out; 3068 3069 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp); 3070 if (!new_ie) 3071 goto out; 3072 3073 reporter_rnr = cfg80211_gen_reporter_rnr(source_bss, 3074 u16_get_bits(control, 3075 IEEE80211_MLC_BASIC_PRES_MLD_ID), 3076 mld_id == 0, reporter_link_id, 3077 bss_change_count, 3078 gfp); 3079 3080 ssid_elem = cfg80211_find_elem(WLAN_EID_SSID, tx_data->ie, 3081 tx_data->ielen); 3082 if (ssid_elem) { 3083 ssid = ssid_elem->data; 3084 ssid_len = ssid_elem->datalen; 3085 } 3086 3087 for (i = 0; i < ARRAY_SIZE(mle->sta_prof) && mle->sta_prof[i]; i++) { 3088 const struct ieee80211_neighbor_ap_info *ap_info; 3089 enum nl80211_band band; 3090 u32 freq; 3091 const u8 *profile; 3092 ssize_t profile_len; 3093 u8 param_ch_count; 3094 u8 link_id, use_for; 3095 bool non_tx; 3096 3097 if (!ieee80211_mle_basic_sta_prof_size_ok((u8 *)mle->sta_prof[i], 3098 mle->sta_prof_len[i])) 3099 continue; 3100 3101 control = le16_to_cpu(mle->sta_prof[i]->control); 3102 3103 if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE)) 3104 continue; 3105 3106 link_id = u16_get_bits(control, 3107 IEEE80211_MLE_STA_CONTROL_LINK_ID); 3108 if (seen_links & BIT(link_id)) 3109 break; 3110 seen_links |= BIT(link_id); 3111 3112 if (!(control & IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT) || 3113 !(control & IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT) || 3114 !(control & IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT)) 3115 continue; 3116 3117 memcpy(data.bssid, mle->sta_prof[i]->variable, ETH_ALEN); 3118 data.beacon_interval = 3119 get_unaligned_le16(mle->sta_prof[i]->variable + 6); 3120 data.tsf = tx_data->tsf + 3121 get_unaligned_le64(mle->sta_prof[i]->variable + 8); 3122 3123 /* sta_info_len counts itself */ 3124 profile = mle->sta_prof[i]->variable + 3125 mle->sta_prof[i]->sta_info_len - 1; 3126 profile_len = (u8 *)mle->sta_prof[i] + mle->sta_prof_len[i] - 3127 profile; 3128 3129 if (profile_len < 2) 3130 continue; 3131 3132 data.capability = get_unaligned_le16(profile); 3133 profile += 2; 3134 profile_len -= 2; 3135 3136 /* Find in RNR to look up channel information */ 3137 use_for = cfg80211_rnr_info_for_mld_ap(tx_data->ie, 3138 tx_data->ielen, 3139 mld_id, link_id, 3140 &ap_info, 3141 ¶m_ch_count, 3142 &non_tx); 3143 if (!use_for) 3144 continue; 3145 3146 /* 3147 * As of 802.11be_D5.0, the specification does not give us any 3148 * way of discovering both the MaxBSSID and the Multiple-BSSID 3149 * Index. It does seem like the Multiple-BSSID Index element 3150 * may be provided, but section 9.4.2.45 explicitly forbids 3151 * including a Multiple-BSSID Element (in this case without any 3152 * subelements). 3153 * Without both pieces of information we cannot calculate the 3154 * reference BSSID, so simply ignore the BSS. 3155 */ 3156 if (non_tx) 3157 continue; 3158 3159 /* We could sanity check the BSSID is included */ 3160 3161 if (!ieee80211_operating_class_to_band(ap_info->op_class, 3162 &band)) 3163 continue; 3164 3165 freq = ieee80211_channel_to_freq_khz(ap_info->channel, band); 3166 data.channel = ieee80211_get_channel_khz(wiphy, freq); 3167 3168 /* Skip if RNR element specifies an unsupported channel */ 3169 if (!data.channel) 3170 continue; 3171 3172 /* Skip if BSS entry generated from MBSSID or DIRECT source 3173 * frame data available already. 3174 */ 3175 bss = cfg80211_get_bss(wiphy, data.channel, data.bssid, ssid, 3176 ssid_len, IEEE80211_BSS_TYPE_ANY, 3177 IEEE80211_PRIVACY_ANY); 3178 if (bss) { 3179 struct cfg80211_internal_bss *ibss = bss_from_pub(bss); 3180 3181 if (data.capability == bss->capability && 3182 ibss->bss_source != BSS_SOURCE_STA_PROFILE) { 3183 cfg80211_put_bss(wiphy, bss); 3184 continue; 3185 } 3186 cfg80211_put_bss(wiphy, bss); 3187 } 3188 3189 if (use_for == NL80211_BSS_USE_FOR_MLD_LINK && 3190 !(wiphy->flags & WIPHY_FLAG_SUPPORTS_NSTR_NONPRIMARY)) { 3191 use_for = 0; 3192 data.cannot_use_reasons = 3193 NL80211_BSS_CANNOT_USE_NSTR_NONPRIMARY; 3194 } 3195 data.use_for = use_for; 3196 3197 /* Generate new elements */ 3198 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN); 3199 data.ie = new_ie; 3200 data.ielen = cfg80211_gen_new_ie(tx_data->ie, tx_data->ielen, 3201 profile, profile_len, 3202 new_ie, 3203 IEEE80211_MAX_DATA_LEN); 3204 if (!data.ielen) 3205 continue; 3206 3207 /* The generated elements do not contain: 3208 * - Basic ML element 3209 * - A TBTT entry in the RNR for the transmitting AP 3210 * 3211 * This information is needed both internally and in userspace 3212 * as such, we should append it here. 3213 */ 3214 if (data.ielen + 3 + sizeof(*ml_elem) + ml_common_len > 3215 IEEE80211_MAX_DATA_LEN) 3216 continue; 3217 3218 /* Copy the Basic Multi-Link element including the common 3219 * information, and then fix up the link ID and BSS param 3220 * change count. 3221 * Note that the ML element length has been verified and we 3222 * also checked that it contains the link ID. 3223 */ 3224 new_ie[data.ielen++] = WLAN_EID_EXTENSION; 3225 new_ie[data.ielen++] = 1 + sizeof(*ml_elem) + ml_common_len; 3226 new_ie[data.ielen++] = WLAN_EID_EXT_EHT_MULTI_LINK; 3227 memcpy(new_ie + data.ielen, ml_elem, 3228 sizeof(*ml_elem) + ml_common_len); 3229 3230 new_ie[data.ielen + sizeof(*ml_elem) + 1 + ETH_ALEN] = link_id; 3231 new_ie[data.ielen + sizeof(*ml_elem) + 1 + ETH_ALEN + 1] = 3232 param_ch_count; 3233 3234 data.ielen += sizeof(*ml_elem) + ml_common_len; 3235 3236 if (reporter_rnr && (use_for & NL80211_BSS_USE_FOR_NORMAL)) { 3237 if (data.ielen + sizeof(struct element) + 3238 reporter_rnr->datalen > IEEE80211_MAX_DATA_LEN) 3239 continue; 3240 3241 memcpy(new_ie + data.ielen, reporter_rnr, 3242 sizeof(struct element) + reporter_rnr->datalen); 3243 data.ielen += sizeof(struct element) + 3244 reporter_rnr->datalen; 3245 } 3246 3247 bss = cfg80211_inform_single_bss_data(wiphy, &data, gfp); 3248 if (!bss) 3249 break; 3250 cfg80211_put_bss(wiphy, bss); 3251 } 3252 3253 out: 3254 kfree(reporter_rnr); 3255 kfree(new_ie); 3256 kfree(mle); 3257 } 3258 3259 static void cfg80211_parse_ml_sta_data(struct wiphy *wiphy, 3260 struct cfg80211_inform_single_bss_data *tx_data, 3261 struct cfg80211_bss *source_bss, 3262 gfp_t gfp) 3263 { 3264 const struct element *elem; 3265 3266 if (!source_bss) 3267 return; 3268 3269 if (tx_data->ftype != CFG80211_BSS_FTYPE_PRESP) 3270 return; 3271 3272 for_each_element_extid(elem, WLAN_EID_EXT_EHT_MULTI_LINK, 3273 tx_data->ie, tx_data->ielen) 3274 cfg80211_parse_ml_elem_sta_data(wiphy, tx_data, source_bss, 3275 elem, gfp); 3276 } 3277 3278 struct cfg80211_bss * 3279 cfg80211_inform_bss_data(struct wiphy *wiphy, 3280 struct cfg80211_inform_bss *data, 3281 enum cfg80211_bss_frame_type ftype, 3282 const u8 *bssid, u64 tsf, u16 capability, 3283 u16 beacon_interval, const u8 *ie, size_t ielen, 3284 gfp_t gfp) 3285 { 3286 struct cfg80211_inform_single_bss_data inform_data = { 3287 .drv_data = data, 3288 .ftype = ftype, 3289 .tsf = tsf, 3290 .capability = capability, 3291 .beacon_interval = beacon_interval, 3292 .ie = ie, 3293 .ielen = ielen, 3294 .use_for = data->restrict_use ? 3295 data->use_for : 3296 NL80211_BSS_USE_FOR_ALL, 3297 .cannot_use_reasons = data->cannot_use_reasons, 3298 }; 3299 struct cfg80211_bss *res; 3300 3301 memcpy(inform_data.bssid, bssid, ETH_ALEN); 3302 3303 res = cfg80211_inform_single_bss_data(wiphy, &inform_data, gfp); 3304 if (!res) 3305 return NULL; 3306 3307 /* don't do any further MBSSID/ML handling for S1G */ 3308 if (ftype == CFG80211_BSS_FTYPE_S1G_BEACON) 3309 return res; 3310 3311 cfg80211_parse_mbssid_data(wiphy, &inform_data, res, gfp); 3312 3313 cfg80211_parse_ml_sta_data(wiphy, &inform_data, res, gfp); 3314 3315 return res; 3316 } 3317 EXPORT_SYMBOL(cfg80211_inform_bss_data); 3318 3319 struct cfg80211_bss * 3320 cfg80211_inform_bss_frame_data(struct wiphy *wiphy, 3321 struct cfg80211_inform_bss *data, 3322 struct ieee80211_mgmt *mgmt, size_t len, 3323 gfp_t gfp) 3324 { 3325 size_t min_hdr_len; 3326 struct ieee80211_ext *ext = NULL; 3327 enum cfg80211_bss_frame_type ftype; 3328 u16 beacon_interval; 3329 const u8 *bssid; 3330 u16 capability; 3331 const u8 *ie; 3332 size_t ielen; 3333 u64 tsf; 3334 size_t s1g_optional_len; 3335 3336 if (WARN_ON(!mgmt)) 3337 return NULL; 3338 3339 if (WARN_ON(!wiphy)) 3340 return NULL; 3341 3342 /* NAN beacons are not a BSS, don't add to the BSS table */ 3343 if (ieee80211_is_nan_beacon(mgmt, len)) 3344 return NULL; 3345 3346 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) != 3347 offsetof(struct ieee80211_mgmt, u.beacon.variable)); 3348 3349 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len); 3350 3351 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) { 3352 ext = (void *) mgmt; 3353 s1g_optional_len = 3354 ieee80211_s1g_optional_len(ext->frame_control); 3355 min_hdr_len = 3356 offsetof(struct ieee80211_ext, u.s1g_beacon.variable) + 3357 s1g_optional_len; 3358 } else { 3359 /* same for beacons */ 3360 min_hdr_len = offsetof(struct ieee80211_mgmt, 3361 u.probe_resp.variable); 3362 } 3363 3364 if (WARN_ON(len < min_hdr_len)) 3365 return NULL; 3366 3367 ielen = len - min_hdr_len; 3368 ie = mgmt->u.probe_resp.variable; 3369 if (ext) { 3370 const struct ieee80211_s1g_bcn_compat_ie *compat; 3371 const struct element *elem; 3372 3373 ie = ext->u.s1g_beacon.variable + s1g_optional_len; 3374 elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT, ie, ielen); 3375 if (!elem) 3376 return NULL; 3377 if (elem->datalen < sizeof(*compat)) 3378 return NULL; 3379 compat = (void *)elem->data; 3380 bssid = ext->u.s1g_beacon.sa; 3381 capability = le16_to_cpu(compat->compat_info); 3382 beacon_interval = le16_to_cpu(compat->beacon_int); 3383 tsf = le32_to_cpu(ext->u.s1g_beacon.timestamp); 3384 tsf |= (u64)le32_to_cpu(compat->tsf_completion) << 32; 3385 } else { 3386 bssid = mgmt->bssid; 3387 beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int); 3388 capability = le16_to_cpu(mgmt->u.probe_resp.capab_info); 3389 tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp); 3390 } 3391 3392 if (ieee80211_is_probe_resp(mgmt->frame_control)) 3393 ftype = CFG80211_BSS_FTYPE_PRESP; 3394 else if (ext) 3395 ftype = CFG80211_BSS_FTYPE_S1G_BEACON; 3396 else 3397 ftype = CFG80211_BSS_FTYPE_BEACON; 3398 3399 return cfg80211_inform_bss_data(wiphy, data, ftype, 3400 bssid, tsf, capability, 3401 beacon_interval, ie, ielen, 3402 gfp); 3403 } 3404 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data); 3405 3406 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) 3407 { 3408 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 3409 3410 if (!pub) 3411 return; 3412 3413 spin_lock_bh(&rdev->bss_lock); 3414 bss_ref_get(rdev, bss_from_pub(pub)); 3415 spin_unlock_bh(&rdev->bss_lock); 3416 } 3417 EXPORT_SYMBOL(cfg80211_ref_bss); 3418 3419 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) 3420 { 3421 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 3422 3423 if (!pub) 3424 return; 3425 3426 spin_lock_bh(&rdev->bss_lock); 3427 bss_ref_put(rdev, bss_from_pub(pub)); 3428 spin_unlock_bh(&rdev->bss_lock); 3429 } 3430 EXPORT_SYMBOL(cfg80211_put_bss); 3431 3432 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) 3433 { 3434 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 3435 struct cfg80211_internal_bss *bss, *tmp1; 3436 struct cfg80211_bss *nontrans_bss, *tmp; 3437 3438 if (WARN_ON(!pub)) 3439 return; 3440 3441 bss = bss_from_pub(pub); 3442 3443 spin_lock_bh(&rdev->bss_lock); 3444 if (list_empty(&bss->list)) 3445 goto out; 3446 3447 list_for_each_entry_safe(nontrans_bss, tmp, 3448 &pub->nontrans_list, 3449 nontrans_list) { 3450 tmp1 = bss_from_pub(nontrans_bss); 3451 if (__cfg80211_unlink_bss(rdev, tmp1)) 3452 rdev->bss_generation++; 3453 } 3454 3455 if (__cfg80211_unlink_bss(rdev, bss)) 3456 rdev->bss_generation++; 3457 out: 3458 spin_unlock_bh(&rdev->bss_lock); 3459 } 3460 EXPORT_SYMBOL(cfg80211_unlink_bss); 3461 3462 void cfg80211_bss_iter(struct wiphy *wiphy, 3463 struct cfg80211_chan_def *chandef, 3464 void (*iter)(struct wiphy *wiphy, 3465 struct cfg80211_bss *bss, 3466 void *data), 3467 void *iter_data) 3468 { 3469 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 3470 struct cfg80211_internal_bss *bss; 3471 3472 spin_lock_bh(&rdev->bss_lock); 3473 3474 list_for_each_entry(bss, &rdev->bss_list, list) { 3475 if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel, 3476 false)) 3477 iter(wiphy, &bss->pub, iter_data); 3478 } 3479 3480 spin_unlock_bh(&rdev->bss_lock); 3481 } 3482 EXPORT_SYMBOL(cfg80211_bss_iter); 3483 3484 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev, 3485 unsigned int link_id, 3486 struct ieee80211_channel *chan) 3487 { 3488 struct wiphy *wiphy = wdev->wiphy; 3489 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 3490 struct cfg80211_internal_bss *cbss = wdev->links[link_id].client.current_bss; 3491 struct cfg80211_internal_bss *new = NULL; 3492 struct cfg80211_internal_bss *bss; 3493 struct cfg80211_bss *nontrans_bss; 3494 struct cfg80211_bss *tmp; 3495 3496 spin_lock_bh(&rdev->bss_lock); 3497 3498 /* 3499 * Some APs use CSA also for bandwidth changes, i.e., without actually 3500 * changing the control channel, so no need to update in such a case. 3501 */ 3502 if (cbss->pub.channel == chan) 3503 goto done; 3504 3505 /* use transmitting bss */ 3506 if (cbss->pub.transmitted_bss) 3507 cbss = bss_from_pub(cbss->pub.transmitted_bss); 3508 3509 cbss->pub.channel = chan; 3510 3511 list_for_each_entry(bss, &rdev->bss_list, list) { 3512 if (bss == cbss) 3513 continue; 3514 3515 if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) { 3516 new = bss; 3517 break; 3518 } 3519 } 3520 3521 if (new) { 3522 /* to save time, update IEs for transmitting bss only */ 3523 cfg80211_update_known_bss(rdev, cbss, new, false); 3524 new->pub.proberesp_ies = NULL; 3525 new->pub.beacon_ies = NULL; 3526 3527 list_for_each_entry_safe(nontrans_bss, tmp, 3528 &new->pub.nontrans_list, 3529 nontrans_list) { 3530 bss = bss_from_pub(nontrans_bss); 3531 if (__cfg80211_unlink_bss(rdev, bss)) 3532 rdev->bss_generation++; 3533 } 3534 3535 WARN_ON(atomic_read(&new->hold)); 3536 if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new))) 3537 rdev->bss_generation++; 3538 } 3539 cfg80211_rehash_bss(rdev, cbss); 3540 3541 list_for_each_entry_safe(nontrans_bss, tmp, 3542 &cbss->pub.nontrans_list, 3543 nontrans_list) { 3544 bss = bss_from_pub(nontrans_bss); 3545 bss->pub.channel = chan; 3546 cfg80211_rehash_bss(rdev, bss); 3547 } 3548 3549 done: 3550 spin_unlock_bh(&rdev->bss_lock); 3551 } 3552 3553 #ifdef CONFIG_CFG80211_WEXT 3554 static struct cfg80211_registered_device * 3555 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex) 3556 { 3557 struct cfg80211_registered_device *rdev; 3558 struct net_device *dev; 3559 3560 ASSERT_RTNL(); 3561 3562 dev = dev_get_by_index(net, ifindex); 3563 if (!dev) 3564 return ERR_PTR(-ENODEV); 3565 if (dev->ieee80211_ptr) 3566 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy); 3567 else 3568 rdev = ERR_PTR(-ENODEV); 3569 dev_put(dev); 3570 return rdev; 3571 } 3572 3573 int cfg80211_wext_siwscan(struct net_device *dev, 3574 struct iw_request_info *info, 3575 union iwreq_data *wrqu, char *extra) 3576 { 3577 struct cfg80211_registered_device *rdev; 3578 struct wiphy *wiphy; 3579 struct iw_scan_req *wreq = NULL; 3580 struct cfg80211_scan_request_int *creq; 3581 int i, err, n_channels = 0; 3582 enum nl80211_band band; 3583 3584 if (!netif_running(dev)) 3585 return -ENETDOWN; 3586 3587 if (wrqu->data.length == sizeof(struct iw_scan_req)) 3588 wreq = (struct iw_scan_req *)extra; 3589 3590 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); 3591 3592 if (IS_ERR(rdev)) 3593 return PTR_ERR(rdev); 3594 3595 if (rdev->scan_req || rdev->scan_msg) 3596 return -EBUSY; 3597 3598 wiphy = &rdev->wiphy; 3599 3600 /* Determine number of channels, needed to allocate creq */ 3601 if (wreq && wreq->num_channels) { 3602 /* Passed from userspace so should be checked */ 3603 if (unlikely(wreq->num_channels > IW_MAX_FREQUENCIES)) 3604 return -EINVAL; 3605 n_channels = wreq->num_channels; 3606 } else { 3607 n_channels = ieee80211_get_num_supported_channels(wiphy); 3608 } 3609 3610 creq = kzalloc(struct_size(creq, req.channels, n_channels) + 3611 sizeof(struct cfg80211_ssid), 3612 GFP_ATOMIC); 3613 if (!creq) 3614 return -ENOMEM; 3615 3616 creq->req.wiphy = wiphy; 3617 creq->req.wdev = dev->ieee80211_ptr; 3618 /* SSIDs come after channels */ 3619 creq->req.ssids = (void *)creq + 3620 struct_size(creq, req.channels, n_channels); 3621 creq->req.n_channels = n_channels; 3622 creq->req.n_ssids = 1; 3623 creq->req.scan_start = jiffies; 3624 3625 /* translate "Scan on frequencies" request */ 3626 i = 0; 3627 for (band = 0; band < NUM_NL80211_BANDS; band++) { 3628 int j; 3629 3630 if (!wiphy->bands[band]) 3631 continue; 3632 3633 for (j = 0; j < wiphy->bands[band]->n_channels; j++) { 3634 struct ieee80211_channel *chan; 3635 3636 /* ignore disabled channels */ 3637 chan = &wiphy->bands[band]->channels[j]; 3638 if (chan->flags & IEEE80211_CHAN_DISABLED || 3639 !cfg80211_wdev_channel_allowed(creq->req.wdev, chan)) 3640 continue; 3641 3642 /* If we have a wireless request structure and the 3643 * wireless request specifies frequencies, then search 3644 * for the matching hardware channel. 3645 */ 3646 if (wreq && wreq->num_channels) { 3647 int k; 3648 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq; 3649 for (k = 0; k < wreq->num_channels; k++) { 3650 struct iw_freq *freq = 3651 &wreq->channel_list[k]; 3652 int wext_freq = 3653 cfg80211_wext_freq(freq); 3654 3655 if (wext_freq == wiphy_freq) 3656 goto wext_freq_found; 3657 } 3658 goto wext_freq_not_found; 3659 } 3660 3661 wext_freq_found: 3662 creq->req.channels[i] = 3663 &wiphy->bands[band]->channels[j]; 3664 i++; 3665 wext_freq_not_found: ; 3666 } 3667 } 3668 /* No channels found? */ 3669 if (!i) { 3670 err = -EINVAL; 3671 goto out; 3672 } 3673 3674 /* Set real number of channels specified in creq->req.channels[] */ 3675 creq->req.n_channels = i; 3676 3677 /* translate "Scan for SSID" request */ 3678 if (wreq) { 3679 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) { 3680 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) { 3681 err = -EINVAL; 3682 goto out; 3683 } 3684 memcpy(creq->req.ssids[0].ssid, wreq->essid, 3685 wreq->essid_len); 3686 creq->req.ssids[0].ssid_len = wreq->essid_len; 3687 } 3688 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) { 3689 creq->req.ssids = NULL; 3690 creq->req.n_ssids = 0; 3691 } 3692 } 3693 3694 for (i = 0; i < NUM_NL80211_BANDS; i++) 3695 if (wiphy->bands[i]) 3696 creq->req.rates[i] = 3697 (1 << wiphy->bands[i]->n_bitrates) - 1; 3698 3699 eth_broadcast_addr(creq->req.bssid); 3700 3701 scoped_guard(wiphy, &rdev->wiphy) { 3702 rdev->scan_req = creq; 3703 err = rdev_scan(rdev, creq); 3704 if (err) { 3705 rdev->scan_req = NULL; 3706 /* creq will be freed below */ 3707 } else { 3708 nl80211_send_scan_start(rdev, dev->ieee80211_ptr); 3709 /* creq now owned by driver */ 3710 creq = NULL; 3711 dev_hold(dev); 3712 } 3713 } 3714 3715 out: 3716 kfree(creq); 3717 return err; 3718 } 3719 3720 static char *ieee80211_scan_add_ies(struct iw_request_info *info, 3721 const struct cfg80211_bss_ies *ies, 3722 char *current_ev, char *end_buf) 3723 { 3724 const u8 *pos, *end, *next; 3725 struct iw_event iwe; 3726 3727 if (!ies) 3728 return current_ev; 3729 3730 /* 3731 * If needed, fragment the IEs buffer (at IE boundaries) into short 3732 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages. 3733 */ 3734 pos = ies->data; 3735 end = pos + ies->len; 3736 3737 while (end - pos > IW_GENERIC_IE_MAX) { 3738 next = pos + 2 + pos[1]; 3739 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX) 3740 next = next + 2 + next[1]; 3741 3742 memset(&iwe, 0, sizeof(iwe)); 3743 iwe.cmd = IWEVGENIE; 3744 iwe.u.data.length = next - pos; 3745 current_ev = iwe_stream_add_point_check(info, current_ev, 3746 end_buf, &iwe, 3747 (void *)pos); 3748 if (IS_ERR(current_ev)) 3749 return current_ev; 3750 pos = next; 3751 } 3752 3753 if (end > pos) { 3754 memset(&iwe, 0, sizeof(iwe)); 3755 iwe.cmd = IWEVGENIE; 3756 iwe.u.data.length = end - pos; 3757 current_ev = iwe_stream_add_point_check(info, current_ev, 3758 end_buf, &iwe, 3759 (void *)pos); 3760 if (IS_ERR(current_ev)) 3761 return current_ev; 3762 } 3763 3764 return current_ev; 3765 } 3766 3767 static char * 3768 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info, 3769 struct cfg80211_internal_bss *bss, char *current_ev, 3770 char *end_buf) 3771 { 3772 const struct cfg80211_bss_ies *ies; 3773 struct iw_event iwe; 3774 const u8 *ie; 3775 u8 buf[50]; 3776 u8 *cfg, *p, *tmp; 3777 int rem, i, sig; 3778 bool ismesh = false; 3779 3780 memset(&iwe, 0, sizeof(iwe)); 3781 iwe.cmd = SIOCGIWAP; 3782 iwe.u.ap_addr.sa_family = ARPHRD_ETHER; 3783 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN); 3784 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe, 3785 IW_EV_ADDR_LEN); 3786 if (IS_ERR(current_ev)) 3787 return current_ev; 3788 3789 memset(&iwe, 0, sizeof(iwe)); 3790 iwe.cmd = SIOCGIWFREQ; 3791 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq); 3792 iwe.u.freq.e = 0; 3793 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe, 3794 IW_EV_FREQ_LEN); 3795 if (IS_ERR(current_ev)) 3796 return current_ev; 3797 3798 memset(&iwe, 0, sizeof(iwe)); 3799 iwe.cmd = SIOCGIWFREQ; 3800 iwe.u.freq.m = bss->pub.channel->center_freq; 3801 iwe.u.freq.e = 6; 3802 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe, 3803 IW_EV_FREQ_LEN); 3804 if (IS_ERR(current_ev)) 3805 return current_ev; 3806 3807 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) { 3808 memset(&iwe, 0, sizeof(iwe)); 3809 iwe.cmd = IWEVQUAL; 3810 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED | 3811 IW_QUAL_NOISE_INVALID | 3812 IW_QUAL_QUAL_UPDATED; 3813 switch (wiphy->signal_type) { 3814 case CFG80211_SIGNAL_TYPE_MBM: 3815 sig = bss->pub.signal / 100; 3816 iwe.u.qual.level = sig; 3817 iwe.u.qual.updated |= IW_QUAL_DBM; 3818 if (sig < -110) /* rather bad */ 3819 sig = -110; 3820 else if (sig > -40) /* perfect */ 3821 sig = -40; 3822 /* will give a range of 0 .. 70 */ 3823 iwe.u.qual.qual = sig + 110; 3824 break; 3825 case CFG80211_SIGNAL_TYPE_UNSPEC: 3826 iwe.u.qual.level = bss->pub.signal; 3827 /* will give range 0 .. 100 */ 3828 iwe.u.qual.qual = bss->pub.signal; 3829 break; 3830 default: 3831 /* not reached */ 3832 break; 3833 } 3834 current_ev = iwe_stream_add_event_check(info, current_ev, 3835 end_buf, &iwe, 3836 IW_EV_QUAL_LEN); 3837 if (IS_ERR(current_ev)) 3838 return current_ev; 3839 } 3840 3841 memset(&iwe, 0, sizeof(iwe)); 3842 iwe.cmd = SIOCGIWENCODE; 3843 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY) 3844 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; 3845 else 3846 iwe.u.data.flags = IW_ENCODE_DISABLED; 3847 iwe.u.data.length = 0; 3848 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf, 3849 &iwe, ""); 3850 if (IS_ERR(current_ev)) 3851 return current_ev; 3852 3853 rcu_read_lock(); 3854 ies = rcu_dereference(bss->pub.ies); 3855 rem = ies->len; 3856 ie = ies->data; 3857 3858 while (rem >= 2) { 3859 /* invalid data */ 3860 if (ie[1] > rem - 2) 3861 break; 3862 3863 switch (ie[0]) { 3864 case WLAN_EID_SSID: 3865 memset(&iwe, 0, sizeof(iwe)); 3866 iwe.cmd = SIOCGIWESSID; 3867 iwe.u.data.length = ie[1]; 3868 iwe.u.data.flags = 1; 3869 current_ev = iwe_stream_add_point_check(info, 3870 current_ev, 3871 end_buf, &iwe, 3872 (u8 *)ie + 2); 3873 if (IS_ERR(current_ev)) 3874 goto unlock; 3875 break; 3876 case WLAN_EID_MESH_ID: 3877 memset(&iwe, 0, sizeof(iwe)); 3878 iwe.cmd = SIOCGIWESSID; 3879 iwe.u.data.length = ie[1]; 3880 iwe.u.data.flags = 1; 3881 current_ev = iwe_stream_add_point_check(info, 3882 current_ev, 3883 end_buf, &iwe, 3884 (u8 *)ie + 2); 3885 if (IS_ERR(current_ev)) 3886 goto unlock; 3887 break; 3888 case WLAN_EID_MESH_CONFIG: 3889 ismesh = true; 3890 if (ie[1] != sizeof(struct ieee80211_meshconf_ie)) 3891 break; 3892 cfg = (u8 *)ie + 2; 3893 memset(&iwe, 0, sizeof(iwe)); 3894 iwe.cmd = IWEVCUSTOM; 3895 iwe.u.data.length = sprintf(buf, 3896 "Mesh Network Path Selection Protocol ID: 0x%02X", 3897 cfg[0]); 3898 current_ev = iwe_stream_add_point_check(info, 3899 current_ev, 3900 end_buf, 3901 &iwe, buf); 3902 if (IS_ERR(current_ev)) 3903 goto unlock; 3904 iwe.u.data.length = sprintf(buf, 3905 "Path Selection Metric ID: 0x%02X", 3906 cfg[1]); 3907 current_ev = iwe_stream_add_point_check(info, 3908 current_ev, 3909 end_buf, 3910 &iwe, buf); 3911 if (IS_ERR(current_ev)) 3912 goto unlock; 3913 iwe.u.data.length = sprintf(buf, 3914 "Congestion Control Mode ID: 0x%02X", 3915 cfg[2]); 3916 current_ev = iwe_stream_add_point_check(info, 3917 current_ev, 3918 end_buf, 3919 &iwe, buf); 3920 if (IS_ERR(current_ev)) 3921 goto unlock; 3922 iwe.u.data.length = sprintf(buf, 3923 "Synchronization ID: 0x%02X", 3924 cfg[3]); 3925 current_ev = iwe_stream_add_point_check(info, 3926 current_ev, 3927 end_buf, 3928 &iwe, buf); 3929 if (IS_ERR(current_ev)) 3930 goto unlock; 3931 iwe.u.data.length = sprintf(buf, 3932 "Authentication ID: 0x%02X", 3933 cfg[4]); 3934 current_ev = iwe_stream_add_point_check(info, 3935 current_ev, 3936 end_buf, 3937 &iwe, buf); 3938 if (IS_ERR(current_ev)) 3939 goto unlock; 3940 iwe.u.data.length = sprintf(buf, 3941 "Formation Info: 0x%02X", 3942 cfg[5]); 3943 current_ev = iwe_stream_add_point_check(info, 3944 current_ev, 3945 end_buf, 3946 &iwe, buf); 3947 if (IS_ERR(current_ev)) 3948 goto unlock; 3949 iwe.u.data.length = sprintf(buf, 3950 "Capabilities: 0x%02X", 3951 cfg[6]); 3952 current_ev = iwe_stream_add_point_check(info, 3953 current_ev, 3954 end_buf, 3955 &iwe, buf); 3956 if (IS_ERR(current_ev)) 3957 goto unlock; 3958 break; 3959 case WLAN_EID_SUPP_RATES: 3960 case WLAN_EID_EXT_SUPP_RATES: 3961 /* display all supported rates in readable format */ 3962 p = current_ev + iwe_stream_lcp_len(info); 3963 3964 memset(&iwe, 0, sizeof(iwe)); 3965 iwe.cmd = SIOCGIWRATE; 3966 /* Those two flags are ignored... */ 3967 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0; 3968 3969 for (i = 0; i < ie[1]; i++) { 3970 iwe.u.bitrate.value = 3971 ((ie[i + 2] & 0x7f) * 500000); 3972 tmp = p; 3973 p = iwe_stream_add_value(info, current_ev, p, 3974 end_buf, &iwe, 3975 IW_EV_PARAM_LEN); 3976 if (p == tmp) { 3977 current_ev = ERR_PTR(-E2BIG); 3978 goto unlock; 3979 } 3980 } 3981 current_ev = p; 3982 break; 3983 } 3984 rem -= ie[1] + 2; 3985 ie += ie[1] + 2; 3986 } 3987 3988 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) || 3989 ismesh) { 3990 memset(&iwe, 0, sizeof(iwe)); 3991 iwe.cmd = SIOCGIWMODE; 3992 if (ismesh) 3993 iwe.u.mode = IW_MODE_MESH; 3994 else if (bss->pub.capability & WLAN_CAPABILITY_ESS) 3995 iwe.u.mode = IW_MODE_MASTER; 3996 else 3997 iwe.u.mode = IW_MODE_ADHOC; 3998 current_ev = iwe_stream_add_event_check(info, current_ev, 3999 end_buf, &iwe, 4000 IW_EV_UINT_LEN); 4001 if (IS_ERR(current_ev)) 4002 goto unlock; 4003 } 4004 4005 memset(&iwe, 0, sizeof(iwe)); 4006 iwe.cmd = IWEVCUSTOM; 4007 iwe.u.data.length = sprintf(buf, "tsf=%016llx", 4008 (unsigned long long)(ies->tsf)); 4009 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf, 4010 &iwe, buf); 4011 if (IS_ERR(current_ev)) 4012 goto unlock; 4013 memset(&iwe, 0, sizeof(iwe)); 4014 iwe.cmd = IWEVCUSTOM; 4015 iwe.u.data.length = sprintf(buf, " Last beacon: %ums ago", 4016 elapsed_jiffies_msecs(bss->ts)); 4017 current_ev = iwe_stream_add_point_check(info, current_ev, 4018 end_buf, &iwe, buf); 4019 if (IS_ERR(current_ev)) 4020 goto unlock; 4021 4022 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf); 4023 4024 unlock: 4025 rcu_read_unlock(); 4026 return current_ev; 4027 } 4028 4029 4030 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev, 4031 struct iw_request_info *info, 4032 char *buf, size_t len) 4033 { 4034 char *current_ev = buf; 4035 char *end_buf = buf + len; 4036 struct cfg80211_internal_bss *bss; 4037 int err = 0; 4038 4039 spin_lock_bh(&rdev->bss_lock); 4040 cfg80211_bss_expire(rdev); 4041 4042 list_for_each_entry(bss, &rdev->bss_list, list) { 4043 if (buf + len - current_ev <= IW_EV_ADDR_LEN) { 4044 err = -E2BIG; 4045 break; 4046 } 4047 current_ev = ieee80211_bss(&rdev->wiphy, info, bss, 4048 current_ev, end_buf); 4049 if (IS_ERR(current_ev)) { 4050 err = PTR_ERR(current_ev); 4051 break; 4052 } 4053 } 4054 spin_unlock_bh(&rdev->bss_lock); 4055 4056 if (err) 4057 return err; 4058 return current_ev - buf; 4059 } 4060 4061 4062 int cfg80211_wext_giwscan(struct net_device *dev, 4063 struct iw_request_info *info, 4064 union iwreq_data *wrqu, char *extra) 4065 { 4066 struct iw_point *data = &wrqu->data; 4067 struct cfg80211_registered_device *rdev; 4068 int res; 4069 4070 if (!netif_running(dev)) 4071 return -ENETDOWN; 4072 4073 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); 4074 4075 if (IS_ERR(rdev)) 4076 return PTR_ERR(rdev); 4077 4078 if (rdev->scan_req || rdev->scan_msg) 4079 return -EAGAIN; 4080 4081 res = ieee80211_scan_results(rdev, info, extra, data->length); 4082 data->length = 0; 4083 if (res >= 0) { 4084 data->length = res; 4085 res = 0; 4086 } 4087 4088 return res; 4089 } 4090 #endif 4091