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