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