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