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