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-2019 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 <net/arp.h> 18 #include <net/cfg80211.h> 19 #include <net/cfg80211-wext.h> 20 #include <net/iw_handler.h> 21 #include "core.h" 22 #include "nl80211.h" 23 #include "wext-compat.h" 24 #include "rdev-ops.h" 25 26 /** 27 * DOC: BSS tree/list structure 28 * 29 * At the top level, the BSS list is kept in both a list in each 30 * registered device (@bss_list) as well as an RB-tree for faster 31 * lookup. In the RB-tree, entries can be looked up using their 32 * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID 33 * for other BSSes. 34 * 35 * Due to the possibility of hidden SSIDs, there's a second level 36 * structure, the "hidden_list" and "hidden_beacon_bss" pointer. 37 * The hidden_list connects all BSSes belonging to a single AP 38 * that has a hidden SSID, and connects beacon and probe response 39 * entries. For a probe response entry for a hidden SSID, the 40 * hidden_beacon_bss pointer points to the BSS struct holding the 41 * beacon's information. 42 * 43 * Reference counting is done for all these references except for 44 * the hidden_list, so that a beacon BSS struct that is otherwise 45 * not referenced has one reference for being on the bss_list and 46 * one for each probe response entry that points to it using the 47 * hidden_beacon_bss pointer. When a BSS struct that has such a 48 * pointer is get/put, the refcount update is also propagated to 49 * the referenced struct, this ensure that it cannot get removed 50 * while somebody is using the probe response version. 51 * 52 * Note that the hidden_beacon_bss pointer never changes, due to 53 * the reference counting. Therefore, no locking is needed for 54 * it. 55 * 56 * Also note that the hidden_beacon_bss pointer is only relevant 57 * if the driver uses something other than the IEs, e.g. private 58 * data stored stored in the BSS struct, since the beacon IEs are 59 * also linked into the probe response struct. 60 */ 61 62 /* 63 * Limit the number of BSS entries stored in mac80211. Each one is 64 * a bit over 4k at most, so this limits to roughly 4-5M of memory. 65 * If somebody wants to really attack this though, they'd likely 66 * use small beacons, and only one type of frame, limiting each of 67 * the entries to a much smaller size (in order to generate more 68 * entries in total, so overhead is bigger.) 69 */ 70 static int bss_entries_limit = 1000; 71 module_param(bss_entries_limit, int, 0644); 72 MODULE_PARM_DESC(bss_entries_limit, 73 "limit to number of scan BSS entries (per wiphy, default 1000)"); 74 75 #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ) 76 77 static void bss_free(struct cfg80211_internal_bss *bss) 78 { 79 struct cfg80211_bss_ies *ies; 80 81 if (WARN_ON(atomic_read(&bss->hold))) 82 return; 83 84 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies); 85 if (ies && !bss->pub.hidden_beacon_bss) 86 kfree_rcu(ies, rcu_head); 87 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies); 88 if (ies) 89 kfree_rcu(ies, rcu_head); 90 91 /* 92 * This happens when the module is removed, it doesn't 93 * really matter any more save for completeness 94 */ 95 if (!list_empty(&bss->hidden_list)) 96 list_del(&bss->hidden_list); 97 98 kfree(bss); 99 } 100 101 static inline void bss_ref_get(struct cfg80211_registered_device *rdev, 102 struct cfg80211_internal_bss *bss) 103 { 104 lockdep_assert_held(&rdev->bss_lock); 105 106 bss->refcount++; 107 if (bss->pub.hidden_beacon_bss) { 108 bss = container_of(bss->pub.hidden_beacon_bss, 109 struct cfg80211_internal_bss, 110 pub); 111 bss->refcount++; 112 } 113 if (bss->pub.transmitted_bss) { 114 bss = container_of(bss->pub.transmitted_bss, 115 struct cfg80211_internal_bss, 116 pub); 117 bss->refcount++; 118 } 119 } 120 121 static inline void bss_ref_put(struct cfg80211_registered_device *rdev, 122 struct cfg80211_internal_bss *bss) 123 { 124 lockdep_assert_held(&rdev->bss_lock); 125 126 if (bss->pub.hidden_beacon_bss) { 127 struct cfg80211_internal_bss *hbss; 128 hbss = container_of(bss->pub.hidden_beacon_bss, 129 struct cfg80211_internal_bss, 130 pub); 131 hbss->refcount--; 132 if (hbss->refcount == 0) 133 bss_free(hbss); 134 } 135 136 if (bss->pub.transmitted_bss) { 137 struct cfg80211_internal_bss *tbss; 138 139 tbss = container_of(bss->pub.transmitted_bss, 140 struct cfg80211_internal_bss, 141 pub); 142 tbss->refcount--; 143 if (tbss->refcount == 0) 144 bss_free(tbss); 145 } 146 147 bss->refcount--; 148 if (bss->refcount == 0) 149 bss_free(bss); 150 } 151 152 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev, 153 struct cfg80211_internal_bss *bss) 154 { 155 lockdep_assert_held(&rdev->bss_lock); 156 157 if (!list_empty(&bss->hidden_list)) { 158 /* 159 * don't remove the beacon entry if it has 160 * probe responses associated with it 161 */ 162 if (!bss->pub.hidden_beacon_bss) 163 return false; 164 /* 165 * if it's a probe response entry break its 166 * link to the other entries in the group 167 */ 168 list_del_init(&bss->hidden_list); 169 } 170 171 list_del_init(&bss->list); 172 list_del_init(&bss->pub.nontrans_list); 173 rb_erase(&bss->rbn, &rdev->bss_tree); 174 rdev->bss_entries--; 175 WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list), 176 "rdev bss entries[%d]/list[empty:%d] corruption\n", 177 rdev->bss_entries, list_empty(&rdev->bss_list)); 178 bss_ref_put(rdev, bss); 179 return true; 180 } 181 182 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen, 183 const u8 *subelement, size_t subie_len, 184 u8 *new_ie, gfp_t gfp) 185 { 186 u8 *pos, *tmp; 187 const u8 *tmp_old, *tmp_new; 188 u8 *sub_copy; 189 190 /* copy subelement as we need to change its content to 191 * mark an ie after it is processed. 192 */ 193 sub_copy = kmalloc(subie_len, gfp); 194 if (!sub_copy) 195 return 0; 196 memcpy(sub_copy, subelement, subie_len); 197 198 pos = &new_ie[0]; 199 200 /* set new ssid */ 201 tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len); 202 if (tmp_new) { 203 memcpy(pos, tmp_new, tmp_new[1] + 2); 204 pos += (tmp_new[1] + 2); 205 } 206 207 /* go through IEs in ie (skip SSID) and subelement, 208 * merge them into new_ie 209 */ 210 tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen); 211 tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie; 212 213 while (tmp_old + tmp_old[1] + 2 - ie <= ielen) { 214 if (tmp_old[0] == 0) { 215 tmp_old++; 216 continue; 217 } 218 219 if (tmp_old[0] == WLAN_EID_EXTENSION) 220 tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy, 221 subie_len); 222 else 223 tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy, 224 subie_len); 225 226 if (!tmp) { 227 /* ie in old ie but not in subelement */ 228 if (tmp_old[0] != WLAN_EID_MULTIPLE_BSSID) { 229 memcpy(pos, tmp_old, tmp_old[1] + 2); 230 pos += tmp_old[1] + 2; 231 } 232 } else { 233 /* ie in transmitting ie also in subelement, 234 * copy from subelement and flag the ie in subelement 235 * as copied (by setting eid field to WLAN_EID_SSID, 236 * which is skipped anyway). 237 * For vendor ie, compare OUI + type + subType to 238 * determine if they are the same ie. 239 */ 240 if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) { 241 if (!memcmp(tmp_old + 2, tmp + 2, 5)) { 242 /* same vendor ie, copy from 243 * subelement 244 */ 245 memcpy(pos, tmp, tmp[1] + 2); 246 pos += tmp[1] + 2; 247 tmp[0] = WLAN_EID_SSID; 248 } else { 249 memcpy(pos, tmp_old, tmp_old[1] + 2); 250 pos += tmp_old[1] + 2; 251 } 252 } else { 253 /* copy ie from subelement into new ie */ 254 memcpy(pos, tmp, tmp[1] + 2); 255 pos += tmp[1] + 2; 256 tmp[0] = WLAN_EID_SSID; 257 } 258 } 259 260 if (tmp_old + tmp_old[1] + 2 - ie == ielen) 261 break; 262 263 tmp_old += tmp_old[1] + 2; 264 } 265 266 /* go through subelement again to check if there is any ie not 267 * copied to new ie, skip ssid, capability, bssid-index ie 268 */ 269 tmp_new = sub_copy; 270 while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) { 271 if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP || 272 tmp_new[0] == WLAN_EID_SSID || 273 tmp_new[0] == WLAN_EID_MULTI_BSSID_IDX)) { 274 memcpy(pos, tmp_new, tmp_new[1] + 2); 275 pos += tmp_new[1] + 2; 276 } 277 if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len) 278 break; 279 tmp_new += tmp_new[1] + 2; 280 } 281 282 kfree(sub_copy); 283 return pos - new_ie; 284 } 285 286 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid, 287 const u8 *ssid, size_t ssid_len) 288 { 289 const struct cfg80211_bss_ies *ies; 290 const u8 *ssidie; 291 292 if (bssid && !ether_addr_equal(a->bssid, bssid)) 293 return false; 294 295 if (!ssid) 296 return true; 297 298 ies = rcu_access_pointer(a->ies); 299 if (!ies) 300 return false; 301 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len); 302 if (!ssidie) 303 return false; 304 if (ssidie[1] != ssid_len) 305 return false; 306 return memcmp(ssidie + 2, ssid, ssid_len) == 0; 307 } 308 309 static int 310 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss, 311 struct cfg80211_bss *nontrans_bss) 312 { 313 const u8 *ssid; 314 size_t ssid_len; 315 struct cfg80211_bss *bss = NULL; 316 317 rcu_read_lock(); 318 ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID); 319 if (!ssid) { 320 rcu_read_unlock(); 321 return -EINVAL; 322 } 323 ssid_len = ssid[1]; 324 ssid = ssid + 2; 325 rcu_read_unlock(); 326 327 /* check if nontrans_bss is in the list */ 328 list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) { 329 if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len)) 330 return 0; 331 } 332 333 /* add to the list */ 334 list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list); 335 return 0; 336 } 337 338 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev, 339 unsigned long expire_time) 340 { 341 struct cfg80211_internal_bss *bss, *tmp; 342 bool expired = false; 343 344 lockdep_assert_held(&rdev->bss_lock); 345 346 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) { 347 if (atomic_read(&bss->hold)) 348 continue; 349 if (!time_after(expire_time, bss->ts)) 350 continue; 351 352 if (__cfg80211_unlink_bss(rdev, bss)) 353 expired = true; 354 } 355 356 if (expired) 357 rdev->bss_generation++; 358 } 359 360 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev) 361 { 362 struct cfg80211_internal_bss *bss, *oldest = NULL; 363 bool ret; 364 365 lockdep_assert_held(&rdev->bss_lock); 366 367 list_for_each_entry(bss, &rdev->bss_list, list) { 368 if (atomic_read(&bss->hold)) 369 continue; 370 371 if (!list_empty(&bss->hidden_list) && 372 !bss->pub.hidden_beacon_bss) 373 continue; 374 375 if (oldest && time_before(oldest->ts, bss->ts)) 376 continue; 377 oldest = bss; 378 } 379 380 if (WARN_ON(!oldest)) 381 return false; 382 383 /* 384 * The callers make sure to increase rdev->bss_generation if anything 385 * gets removed (and a new entry added), so there's no need to also do 386 * it here. 387 */ 388 389 ret = __cfg80211_unlink_bss(rdev, oldest); 390 WARN_ON(!ret); 391 return ret; 392 } 393 394 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, 395 bool send_message) 396 { 397 struct cfg80211_scan_request *request; 398 struct wireless_dev *wdev; 399 struct sk_buff *msg; 400 #ifdef CONFIG_CFG80211_WEXT 401 union iwreq_data wrqu; 402 #endif 403 404 ASSERT_RTNL(); 405 406 if (rdev->scan_msg) { 407 nl80211_send_scan_msg(rdev, rdev->scan_msg); 408 rdev->scan_msg = NULL; 409 return; 410 } 411 412 request = rdev->scan_req; 413 if (!request) 414 return; 415 416 wdev = request->wdev; 417 418 /* 419 * This must be before sending the other events! 420 * Otherwise, wpa_supplicant gets completely confused with 421 * wext events. 422 */ 423 if (wdev->netdev) 424 cfg80211_sme_scan_done(wdev->netdev); 425 426 if (!request->info.aborted && 427 request->flags & NL80211_SCAN_FLAG_FLUSH) { 428 /* flush entries from previous scans */ 429 spin_lock_bh(&rdev->bss_lock); 430 __cfg80211_bss_expire(rdev, request->scan_start); 431 spin_unlock_bh(&rdev->bss_lock); 432 } 433 434 msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted); 435 436 #ifdef CONFIG_CFG80211_WEXT 437 if (wdev->netdev && !request->info.aborted) { 438 memset(&wrqu, 0, sizeof(wrqu)); 439 440 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL); 441 } 442 #endif 443 444 if (wdev->netdev) 445 dev_put(wdev->netdev); 446 447 rdev->scan_req = NULL; 448 kfree(request); 449 450 if (!send_message) 451 rdev->scan_msg = msg; 452 else 453 nl80211_send_scan_msg(rdev, msg); 454 } 455 456 void __cfg80211_scan_done(struct work_struct *wk) 457 { 458 struct cfg80211_registered_device *rdev; 459 460 rdev = container_of(wk, struct cfg80211_registered_device, 461 scan_done_wk); 462 463 rtnl_lock(); 464 ___cfg80211_scan_done(rdev, true); 465 rtnl_unlock(); 466 } 467 468 void cfg80211_scan_done(struct cfg80211_scan_request *request, 469 struct cfg80211_scan_info *info) 470 { 471 trace_cfg80211_scan_done(request, info); 472 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req); 473 474 request->info = *info; 475 request->notified = true; 476 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk); 477 } 478 EXPORT_SYMBOL(cfg80211_scan_done); 479 480 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev, 481 struct cfg80211_sched_scan_request *req) 482 { 483 ASSERT_RTNL(); 484 485 list_add_rcu(&req->list, &rdev->sched_scan_req_list); 486 } 487 488 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev, 489 struct cfg80211_sched_scan_request *req) 490 { 491 ASSERT_RTNL(); 492 493 list_del_rcu(&req->list); 494 kfree_rcu(req, rcu_head); 495 } 496 497 static struct cfg80211_sched_scan_request * 498 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid) 499 { 500 struct cfg80211_sched_scan_request *pos; 501 502 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); 503 504 list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) { 505 if (pos->reqid == reqid) 506 return pos; 507 } 508 return NULL; 509 } 510 511 /* 512 * Determines if a scheduled scan request can be handled. When a legacy 513 * scheduled scan is running no other scheduled scan is allowed regardless 514 * whether the request is for legacy or multi-support scan. When a multi-support 515 * scheduled scan is running a request for legacy scan is not allowed. In this 516 * case a request for multi-support scan can be handled if resources are 517 * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached. 518 */ 519 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev, 520 bool want_multi) 521 { 522 struct cfg80211_sched_scan_request *pos; 523 int i = 0; 524 525 list_for_each_entry(pos, &rdev->sched_scan_req_list, list) { 526 /* request id zero means legacy in progress */ 527 if (!i && !pos->reqid) 528 return -EINPROGRESS; 529 i++; 530 } 531 532 if (i) { 533 /* no legacy allowed when multi request(s) are active */ 534 if (!want_multi) 535 return -EINPROGRESS; 536 537 /* resource limit reached */ 538 if (i == rdev->wiphy.max_sched_scan_reqs) 539 return -ENOSPC; 540 } 541 return 0; 542 } 543 544 void cfg80211_sched_scan_results_wk(struct work_struct *work) 545 { 546 struct cfg80211_registered_device *rdev; 547 struct cfg80211_sched_scan_request *req, *tmp; 548 549 rdev = container_of(work, struct cfg80211_registered_device, 550 sched_scan_res_wk); 551 552 rtnl_lock(); 553 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) { 554 if (req->report_results) { 555 req->report_results = false; 556 if (req->flags & NL80211_SCAN_FLAG_FLUSH) { 557 /* flush entries from previous scans */ 558 spin_lock_bh(&rdev->bss_lock); 559 __cfg80211_bss_expire(rdev, req->scan_start); 560 spin_unlock_bh(&rdev->bss_lock); 561 req->scan_start = jiffies; 562 } 563 nl80211_send_sched_scan(req, 564 NL80211_CMD_SCHED_SCAN_RESULTS); 565 } 566 } 567 rtnl_unlock(); 568 } 569 570 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid) 571 { 572 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 573 struct cfg80211_sched_scan_request *request; 574 575 trace_cfg80211_sched_scan_results(wiphy, reqid); 576 /* ignore if we're not scanning */ 577 578 rcu_read_lock(); 579 request = cfg80211_find_sched_scan_req(rdev, reqid); 580 if (request) { 581 request->report_results = true; 582 queue_work(cfg80211_wq, &rdev->sched_scan_res_wk); 583 } 584 rcu_read_unlock(); 585 } 586 EXPORT_SYMBOL(cfg80211_sched_scan_results); 587 588 void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid) 589 { 590 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 591 592 ASSERT_RTNL(); 593 594 trace_cfg80211_sched_scan_stopped(wiphy, reqid); 595 596 __cfg80211_stop_sched_scan(rdev, reqid, true); 597 } 598 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl); 599 600 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid) 601 { 602 rtnl_lock(); 603 cfg80211_sched_scan_stopped_rtnl(wiphy, reqid); 604 rtnl_unlock(); 605 } 606 EXPORT_SYMBOL(cfg80211_sched_scan_stopped); 607 608 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev, 609 struct cfg80211_sched_scan_request *req, 610 bool driver_initiated) 611 { 612 ASSERT_RTNL(); 613 614 if (!driver_initiated) { 615 int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid); 616 if (err) 617 return err; 618 } 619 620 nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED); 621 622 cfg80211_del_sched_scan_req(rdev, req); 623 624 return 0; 625 } 626 627 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev, 628 u64 reqid, bool driver_initiated) 629 { 630 struct cfg80211_sched_scan_request *sched_scan_req; 631 632 ASSERT_RTNL(); 633 634 sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid); 635 if (!sched_scan_req) 636 return -ENOENT; 637 638 return cfg80211_stop_sched_scan_req(rdev, sched_scan_req, 639 driver_initiated); 640 } 641 642 void cfg80211_bss_age(struct cfg80211_registered_device *rdev, 643 unsigned long age_secs) 644 { 645 struct cfg80211_internal_bss *bss; 646 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC); 647 648 spin_lock_bh(&rdev->bss_lock); 649 list_for_each_entry(bss, &rdev->bss_list, list) 650 bss->ts -= age_jiffies; 651 spin_unlock_bh(&rdev->bss_lock); 652 } 653 654 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev) 655 { 656 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE); 657 } 658 659 const struct element * 660 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len, 661 const u8 *match, unsigned int match_len, 662 unsigned int match_offset) 663 { 664 const struct element *elem; 665 666 for_each_element_id(elem, eid, ies, len) { 667 if (elem->datalen >= match_offset + match_len && 668 !memcmp(elem->data + match_offset, match, match_len)) 669 return elem; 670 } 671 672 return NULL; 673 } 674 EXPORT_SYMBOL(cfg80211_find_elem_match); 675 676 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type, 677 const u8 *ies, 678 unsigned int len) 679 { 680 const struct element *elem; 681 u8 match[] = { oui >> 16, oui >> 8, oui, oui_type }; 682 int match_len = (oui_type < 0) ? 3 : sizeof(match); 683 684 if (WARN_ON(oui_type > 0xff)) 685 return NULL; 686 687 elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len, 688 match, match_len, 0); 689 690 if (!elem || elem->datalen < 4) 691 return NULL; 692 693 return elem; 694 } 695 EXPORT_SYMBOL(cfg80211_find_vendor_elem); 696 697 /** 698 * enum bss_compare_mode - BSS compare mode 699 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find) 700 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode 701 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode 702 */ 703 enum bss_compare_mode { 704 BSS_CMP_REGULAR, 705 BSS_CMP_HIDE_ZLEN, 706 BSS_CMP_HIDE_NUL, 707 }; 708 709 static int cmp_bss(struct cfg80211_bss *a, 710 struct cfg80211_bss *b, 711 enum bss_compare_mode mode) 712 { 713 const struct cfg80211_bss_ies *a_ies, *b_ies; 714 const u8 *ie1 = NULL; 715 const u8 *ie2 = NULL; 716 int i, r; 717 718 if (a->channel != b->channel) 719 return b->channel->center_freq - a->channel->center_freq; 720 721 a_ies = rcu_access_pointer(a->ies); 722 if (!a_ies) 723 return -1; 724 b_ies = rcu_access_pointer(b->ies); 725 if (!b_ies) 726 return 1; 727 728 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability)) 729 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID, 730 a_ies->data, a_ies->len); 731 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability)) 732 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID, 733 b_ies->data, b_ies->len); 734 if (ie1 && ie2) { 735 int mesh_id_cmp; 736 737 if (ie1[1] == ie2[1]) 738 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]); 739 else 740 mesh_id_cmp = ie2[1] - ie1[1]; 741 742 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, 743 a_ies->data, a_ies->len); 744 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, 745 b_ies->data, b_ies->len); 746 if (ie1 && ie2) { 747 if (mesh_id_cmp) 748 return mesh_id_cmp; 749 if (ie1[1] != ie2[1]) 750 return ie2[1] - ie1[1]; 751 return memcmp(ie1 + 2, ie2 + 2, ie1[1]); 752 } 753 } 754 755 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid)); 756 if (r) 757 return r; 758 759 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len); 760 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len); 761 762 if (!ie1 && !ie2) 763 return 0; 764 765 /* 766 * Note that with "hide_ssid", the function returns a match if 767 * the already-present BSS ("b") is a hidden SSID beacon for 768 * the new BSS ("a"). 769 */ 770 771 /* sort missing IE before (left of) present IE */ 772 if (!ie1) 773 return -1; 774 if (!ie2) 775 return 1; 776 777 switch (mode) { 778 case BSS_CMP_HIDE_ZLEN: 779 /* 780 * In ZLEN mode we assume the BSS entry we're 781 * looking for has a zero-length SSID. So if 782 * the one we're looking at right now has that, 783 * return 0. Otherwise, return the difference 784 * in length, but since we're looking for the 785 * 0-length it's really equivalent to returning 786 * the length of the one we're looking at. 787 * 788 * No content comparison is needed as we assume 789 * the content length is zero. 790 */ 791 return ie2[1]; 792 case BSS_CMP_REGULAR: 793 default: 794 /* sort by length first, then by contents */ 795 if (ie1[1] != ie2[1]) 796 return ie2[1] - ie1[1]; 797 return memcmp(ie1 + 2, ie2 + 2, ie1[1]); 798 case BSS_CMP_HIDE_NUL: 799 if (ie1[1] != ie2[1]) 800 return ie2[1] - ie1[1]; 801 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */ 802 for (i = 0; i < ie2[1]; i++) 803 if (ie2[i + 2]) 804 return -1; 805 return 0; 806 } 807 } 808 809 static bool cfg80211_bss_type_match(u16 capability, 810 enum nl80211_band band, 811 enum ieee80211_bss_type bss_type) 812 { 813 bool ret = true; 814 u16 mask, val; 815 816 if (bss_type == IEEE80211_BSS_TYPE_ANY) 817 return ret; 818 819 if (band == NL80211_BAND_60GHZ) { 820 mask = WLAN_CAPABILITY_DMG_TYPE_MASK; 821 switch (bss_type) { 822 case IEEE80211_BSS_TYPE_ESS: 823 val = WLAN_CAPABILITY_DMG_TYPE_AP; 824 break; 825 case IEEE80211_BSS_TYPE_PBSS: 826 val = WLAN_CAPABILITY_DMG_TYPE_PBSS; 827 break; 828 case IEEE80211_BSS_TYPE_IBSS: 829 val = WLAN_CAPABILITY_DMG_TYPE_IBSS; 830 break; 831 default: 832 return false; 833 } 834 } else { 835 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS; 836 switch (bss_type) { 837 case IEEE80211_BSS_TYPE_ESS: 838 val = WLAN_CAPABILITY_ESS; 839 break; 840 case IEEE80211_BSS_TYPE_IBSS: 841 val = WLAN_CAPABILITY_IBSS; 842 break; 843 case IEEE80211_BSS_TYPE_MBSS: 844 val = 0; 845 break; 846 default: 847 return false; 848 } 849 } 850 851 ret = ((capability & mask) == val); 852 return ret; 853 } 854 855 /* Returned bss is reference counted and must be cleaned up appropriately. */ 856 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy, 857 struct ieee80211_channel *channel, 858 const u8 *bssid, 859 const u8 *ssid, size_t ssid_len, 860 enum ieee80211_bss_type bss_type, 861 enum ieee80211_privacy privacy) 862 { 863 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 864 struct cfg80211_internal_bss *bss, *res = NULL; 865 unsigned long now = jiffies; 866 int bss_privacy; 867 868 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type, 869 privacy); 870 871 spin_lock_bh(&rdev->bss_lock); 872 873 list_for_each_entry(bss, &rdev->bss_list, list) { 874 if (!cfg80211_bss_type_match(bss->pub.capability, 875 bss->pub.channel->band, bss_type)) 876 continue; 877 878 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY); 879 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) || 880 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy)) 881 continue; 882 if (channel && bss->pub.channel != channel) 883 continue; 884 if (!is_valid_ether_addr(bss->pub.bssid)) 885 continue; 886 /* Don't get expired BSS structs */ 887 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) && 888 !atomic_read(&bss->hold)) 889 continue; 890 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) { 891 res = bss; 892 bss_ref_get(rdev, res); 893 break; 894 } 895 } 896 897 spin_unlock_bh(&rdev->bss_lock); 898 if (!res) 899 return NULL; 900 trace_cfg80211_return_bss(&res->pub); 901 return &res->pub; 902 } 903 EXPORT_SYMBOL(cfg80211_get_bss); 904 905 static void rb_insert_bss(struct cfg80211_registered_device *rdev, 906 struct cfg80211_internal_bss *bss) 907 { 908 struct rb_node **p = &rdev->bss_tree.rb_node; 909 struct rb_node *parent = NULL; 910 struct cfg80211_internal_bss *tbss; 911 int cmp; 912 913 while (*p) { 914 parent = *p; 915 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn); 916 917 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR); 918 919 if (WARN_ON(!cmp)) { 920 /* will sort of leak this BSS */ 921 return; 922 } 923 924 if (cmp < 0) 925 p = &(*p)->rb_left; 926 else 927 p = &(*p)->rb_right; 928 } 929 930 rb_link_node(&bss->rbn, parent, p); 931 rb_insert_color(&bss->rbn, &rdev->bss_tree); 932 } 933 934 static struct cfg80211_internal_bss * 935 rb_find_bss(struct cfg80211_registered_device *rdev, 936 struct cfg80211_internal_bss *res, 937 enum bss_compare_mode mode) 938 { 939 struct rb_node *n = rdev->bss_tree.rb_node; 940 struct cfg80211_internal_bss *bss; 941 int r; 942 943 while (n) { 944 bss = rb_entry(n, struct cfg80211_internal_bss, rbn); 945 r = cmp_bss(&res->pub, &bss->pub, mode); 946 947 if (r == 0) 948 return bss; 949 else if (r < 0) 950 n = n->rb_left; 951 else 952 n = n->rb_right; 953 } 954 955 return NULL; 956 } 957 958 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev, 959 struct cfg80211_internal_bss *new) 960 { 961 const struct cfg80211_bss_ies *ies; 962 struct cfg80211_internal_bss *bss; 963 const u8 *ie; 964 int i, ssidlen; 965 u8 fold = 0; 966 u32 n_entries = 0; 967 968 ies = rcu_access_pointer(new->pub.beacon_ies); 969 if (WARN_ON(!ies)) 970 return false; 971 972 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len); 973 if (!ie) { 974 /* nothing to do */ 975 return true; 976 } 977 978 ssidlen = ie[1]; 979 for (i = 0; i < ssidlen; i++) 980 fold |= ie[2 + i]; 981 982 if (fold) { 983 /* not a hidden SSID */ 984 return true; 985 } 986 987 /* This is the bad part ... */ 988 989 list_for_each_entry(bss, &rdev->bss_list, list) { 990 /* 991 * we're iterating all the entries anyway, so take the 992 * opportunity to validate the list length accounting 993 */ 994 n_entries++; 995 996 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid)) 997 continue; 998 if (bss->pub.channel != new->pub.channel) 999 continue; 1000 if (bss->pub.scan_width != new->pub.scan_width) 1001 continue; 1002 if (rcu_access_pointer(bss->pub.beacon_ies)) 1003 continue; 1004 ies = rcu_access_pointer(bss->pub.ies); 1005 if (!ies) 1006 continue; 1007 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len); 1008 if (!ie) 1009 continue; 1010 if (ssidlen && ie[1] != ssidlen) 1011 continue; 1012 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss)) 1013 continue; 1014 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list))) 1015 list_del(&bss->hidden_list); 1016 /* combine them */ 1017 list_add(&bss->hidden_list, &new->hidden_list); 1018 bss->pub.hidden_beacon_bss = &new->pub; 1019 new->refcount += bss->refcount; 1020 rcu_assign_pointer(bss->pub.beacon_ies, 1021 new->pub.beacon_ies); 1022 } 1023 1024 WARN_ONCE(n_entries != rdev->bss_entries, 1025 "rdev bss entries[%d]/list[len:%d] corruption\n", 1026 rdev->bss_entries, n_entries); 1027 1028 return true; 1029 } 1030 1031 struct cfg80211_non_tx_bss { 1032 struct cfg80211_bss *tx_bss; 1033 u8 max_bssid_indicator; 1034 u8 bssid_index; 1035 }; 1036 1037 /* Returned bss is reference counted and must be cleaned up appropriately. */ 1038 static struct cfg80211_internal_bss * 1039 cfg80211_bss_update(struct cfg80211_registered_device *rdev, 1040 struct cfg80211_internal_bss *tmp, 1041 bool signal_valid) 1042 { 1043 struct cfg80211_internal_bss *found = NULL; 1044 1045 if (WARN_ON(!tmp->pub.channel)) 1046 return NULL; 1047 1048 tmp->ts = jiffies; 1049 1050 spin_lock_bh(&rdev->bss_lock); 1051 1052 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) { 1053 spin_unlock_bh(&rdev->bss_lock); 1054 return NULL; 1055 } 1056 1057 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR); 1058 1059 if (found) { 1060 /* Update IEs */ 1061 if (rcu_access_pointer(tmp->pub.proberesp_ies)) { 1062 const struct cfg80211_bss_ies *old; 1063 1064 old = rcu_access_pointer(found->pub.proberesp_ies); 1065 1066 rcu_assign_pointer(found->pub.proberesp_ies, 1067 tmp->pub.proberesp_ies); 1068 /* Override possible earlier Beacon frame IEs */ 1069 rcu_assign_pointer(found->pub.ies, 1070 tmp->pub.proberesp_ies); 1071 if (old) 1072 kfree_rcu((struct cfg80211_bss_ies *)old, 1073 rcu_head); 1074 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) { 1075 const struct cfg80211_bss_ies *old; 1076 struct cfg80211_internal_bss *bss; 1077 1078 if (found->pub.hidden_beacon_bss && 1079 !list_empty(&found->hidden_list)) { 1080 const struct cfg80211_bss_ies *f; 1081 1082 /* 1083 * The found BSS struct is one of the probe 1084 * response members of a group, but we're 1085 * receiving a beacon (beacon_ies in the tmp 1086 * bss is used). This can only mean that the 1087 * AP changed its beacon from not having an 1088 * SSID to showing it, which is confusing so 1089 * drop this information. 1090 */ 1091 1092 f = rcu_access_pointer(tmp->pub.beacon_ies); 1093 kfree_rcu((struct cfg80211_bss_ies *)f, 1094 rcu_head); 1095 goto drop; 1096 } 1097 1098 old = rcu_access_pointer(found->pub.beacon_ies); 1099 1100 rcu_assign_pointer(found->pub.beacon_ies, 1101 tmp->pub.beacon_ies); 1102 1103 /* Override IEs if they were from a beacon before */ 1104 if (old == rcu_access_pointer(found->pub.ies)) 1105 rcu_assign_pointer(found->pub.ies, 1106 tmp->pub.beacon_ies); 1107 1108 /* Assign beacon IEs to all sub entries */ 1109 list_for_each_entry(bss, &found->hidden_list, 1110 hidden_list) { 1111 const struct cfg80211_bss_ies *ies; 1112 1113 ies = rcu_access_pointer(bss->pub.beacon_ies); 1114 WARN_ON(ies != old); 1115 1116 rcu_assign_pointer(bss->pub.beacon_ies, 1117 tmp->pub.beacon_ies); 1118 } 1119 1120 if (old) 1121 kfree_rcu((struct cfg80211_bss_ies *)old, 1122 rcu_head); 1123 } 1124 1125 found->pub.beacon_interval = tmp->pub.beacon_interval; 1126 /* 1127 * don't update the signal if beacon was heard on 1128 * adjacent channel. 1129 */ 1130 if (signal_valid) 1131 found->pub.signal = tmp->pub.signal; 1132 found->pub.capability = tmp->pub.capability; 1133 found->ts = tmp->ts; 1134 found->ts_boottime = tmp->ts_boottime; 1135 found->parent_tsf = tmp->parent_tsf; 1136 found->pub.chains = tmp->pub.chains; 1137 memcpy(found->pub.chain_signal, tmp->pub.chain_signal, 1138 IEEE80211_MAX_CHAINS); 1139 ether_addr_copy(found->parent_bssid, tmp->parent_bssid); 1140 found->pub.max_bssid_indicator = tmp->pub.max_bssid_indicator; 1141 found->pub.bssid_index = tmp->pub.bssid_index; 1142 } else { 1143 struct cfg80211_internal_bss *new; 1144 struct cfg80211_internal_bss *hidden; 1145 struct cfg80211_bss_ies *ies; 1146 1147 /* 1148 * create a copy -- the "res" variable that is passed in 1149 * is allocated on the stack since it's not needed in the 1150 * more common case of an update 1151 */ 1152 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size, 1153 GFP_ATOMIC); 1154 if (!new) { 1155 ies = (void *)rcu_dereference(tmp->pub.beacon_ies); 1156 if (ies) 1157 kfree_rcu(ies, rcu_head); 1158 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies); 1159 if (ies) 1160 kfree_rcu(ies, rcu_head); 1161 goto drop; 1162 } 1163 memcpy(new, tmp, sizeof(*new)); 1164 new->refcount = 1; 1165 INIT_LIST_HEAD(&new->hidden_list); 1166 INIT_LIST_HEAD(&new->pub.nontrans_list); 1167 1168 if (rcu_access_pointer(tmp->pub.proberesp_ies)) { 1169 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN); 1170 if (!hidden) 1171 hidden = rb_find_bss(rdev, tmp, 1172 BSS_CMP_HIDE_NUL); 1173 if (hidden) { 1174 new->pub.hidden_beacon_bss = &hidden->pub; 1175 list_add(&new->hidden_list, 1176 &hidden->hidden_list); 1177 hidden->refcount++; 1178 rcu_assign_pointer(new->pub.beacon_ies, 1179 hidden->pub.beacon_ies); 1180 } 1181 } else { 1182 /* 1183 * Ok so we found a beacon, and don't have an entry. If 1184 * it's a beacon with hidden SSID, we might be in for an 1185 * expensive search for any probe responses that should 1186 * be grouped with this beacon for updates ... 1187 */ 1188 if (!cfg80211_combine_bsses(rdev, new)) { 1189 kfree(new); 1190 goto drop; 1191 } 1192 } 1193 1194 if (rdev->bss_entries >= bss_entries_limit && 1195 !cfg80211_bss_expire_oldest(rdev)) { 1196 kfree(new); 1197 goto drop; 1198 } 1199 1200 /* This must be before the call to bss_ref_get */ 1201 if (tmp->pub.transmitted_bss) { 1202 struct cfg80211_internal_bss *pbss = 1203 container_of(tmp->pub.transmitted_bss, 1204 struct cfg80211_internal_bss, 1205 pub); 1206 1207 new->pub.transmitted_bss = tmp->pub.transmitted_bss; 1208 bss_ref_get(rdev, pbss); 1209 } 1210 1211 list_add_tail(&new->list, &rdev->bss_list); 1212 rdev->bss_entries++; 1213 rb_insert_bss(rdev, new); 1214 found = new; 1215 } 1216 1217 rdev->bss_generation++; 1218 bss_ref_get(rdev, found); 1219 spin_unlock_bh(&rdev->bss_lock); 1220 1221 return found; 1222 drop: 1223 spin_unlock_bh(&rdev->bss_lock); 1224 return NULL; 1225 } 1226 1227 /* 1228 * Update RX channel information based on the available frame payload 1229 * information. This is mainly for the 2.4 GHz band where frames can be received 1230 * from neighboring channels and the Beacon frames use the DSSS Parameter Set 1231 * element to indicate the current (transmitting) channel, but this might also 1232 * be needed on other bands if RX frequency does not match with the actual 1233 * operating channel of a BSS. 1234 */ 1235 static struct ieee80211_channel * 1236 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen, 1237 struct ieee80211_channel *channel, 1238 enum nl80211_bss_scan_width scan_width) 1239 { 1240 const u8 *tmp; 1241 u32 freq; 1242 int channel_number = -1; 1243 struct ieee80211_channel *alt_channel; 1244 1245 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen); 1246 if (tmp && tmp[1] == 1) { 1247 channel_number = tmp[2]; 1248 } else { 1249 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen); 1250 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) { 1251 struct ieee80211_ht_operation *htop = (void *)(tmp + 2); 1252 1253 channel_number = htop->primary_chan; 1254 } 1255 } 1256 1257 if (channel_number < 0) { 1258 /* No channel information in frame payload */ 1259 return channel; 1260 } 1261 1262 freq = ieee80211_channel_to_frequency(channel_number, channel->band); 1263 alt_channel = ieee80211_get_channel(wiphy, freq); 1264 if (!alt_channel) { 1265 if (channel->band == NL80211_BAND_2GHZ) { 1266 /* 1267 * Better not allow unexpected channels when that could 1268 * be going beyond the 1-11 range (e.g., discovering 1269 * BSS on channel 12 when radio is configured for 1270 * channel 11. 1271 */ 1272 return NULL; 1273 } 1274 1275 /* No match for the payload channel number - ignore it */ 1276 return channel; 1277 } 1278 1279 if (scan_width == NL80211_BSS_CHAN_WIDTH_10 || 1280 scan_width == NL80211_BSS_CHAN_WIDTH_5) { 1281 /* 1282 * Ignore channel number in 5 and 10 MHz channels where there 1283 * may not be an n:1 or 1:n mapping between frequencies and 1284 * channel numbers. 1285 */ 1286 return channel; 1287 } 1288 1289 /* 1290 * Use the channel determined through the payload channel number 1291 * instead of the RX channel reported by the driver. 1292 */ 1293 if (alt_channel->flags & IEEE80211_CHAN_DISABLED) 1294 return NULL; 1295 return alt_channel; 1296 } 1297 1298 /* Returned bss is reference counted and must be cleaned up appropriately. */ 1299 static struct cfg80211_bss * 1300 cfg80211_inform_single_bss_data(struct wiphy *wiphy, 1301 struct cfg80211_inform_bss *data, 1302 enum cfg80211_bss_frame_type ftype, 1303 const u8 *bssid, u64 tsf, u16 capability, 1304 u16 beacon_interval, const u8 *ie, size_t ielen, 1305 struct cfg80211_non_tx_bss *non_tx_data, 1306 gfp_t gfp) 1307 { 1308 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1309 struct cfg80211_bss_ies *ies; 1310 struct ieee80211_channel *channel; 1311 struct cfg80211_internal_bss tmp = {}, *res; 1312 int bss_type; 1313 bool signal_valid; 1314 1315 if (WARN_ON(!wiphy)) 1316 return NULL; 1317 1318 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC && 1319 (data->signal < 0 || data->signal > 100))) 1320 return NULL; 1321 1322 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan, 1323 data->scan_width); 1324 if (!channel) 1325 return NULL; 1326 1327 memcpy(tmp.pub.bssid, bssid, ETH_ALEN); 1328 tmp.pub.channel = channel; 1329 tmp.pub.scan_width = data->scan_width; 1330 tmp.pub.signal = data->signal; 1331 tmp.pub.beacon_interval = beacon_interval; 1332 tmp.pub.capability = capability; 1333 tmp.ts_boottime = data->boottime_ns; 1334 if (non_tx_data) { 1335 tmp.pub.transmitted_bss = non_tx_data->tx_bss; 1336 tmp.pub.bssid_index = non_tx_data->bssid_index; 1337 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator; 1338 } 1339 1340 /* 1341 * If we do not know here whether the IEs are from a Beacon or Probe 1342 * Response frame, we need to pick one of the options and only use it 1343 * with the driver that does not provide the full Beacon/Probe Response 1344 * frame. Use Beacon frame pointer to avoid indicating that this should 1345 * override the IEs pointer should we have received an earlier 1346 * indication of Probe Response data. 1347 */ 1348 ies = kzalloc(sizeof(*ies) + ielen, gfp); 1349 if (!ies) 1350 return NULL; 1351 ies->len = ielen; 1352 ies->tsf = tsf; 1353 ies->from_beacon = false; 1354 memcpy(ies->data, ie, ielen); 1355 1356 switch (ftype) { 1357 case CFG80211_BSS_FTYPE_BEACON: 1358 ies->from_beacon = true; 1359 /* fall through */ 1360 case CFG80211_BSS_FTYPE_UNKNOWN: 1361 rcu_assign_pointer(tmp.pub.beacon_ies, ies); 1362 break; 1363 case CFG80211_BSS_FTYPE_PRESP: 1364 rcu_assign_pointer(tmp.pub.proberesp_ies, ies); 1365 break; 1366 } 1367 rcu_assign_pointer(tmp.pub.ies, ies); 1368 1369 signal_valid = abs(data->chan->center_freq - channel->center_freq) <= 1370 wiphy->max_adj_channel_rssi_comp; 1371 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid); 1372 if (!res) 1373 return NULL; 1374 1375 if (channel->band == NL80211_BAND_60GHZ) { 1376 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK; 1377 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP || 1378 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS) 1379 regulatory_hint_found_beacon(wiphy, channel, gfp); 1380 } else { 1381 if (res->pub.capability & WLAN_CAPABILITY_ESS) 1382 regulatory_hint_found_beacon(wiphy, channel, gfp); 1383 } 1384 1385 if (non_tx_data && non_tx_data->tx_bss) { 1386 /* this is a nontransmitting bss, we need to add it to 1387 * transmitting bss' list if it is not there 1388 */ 1389 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss, 1390 &res->pub)) { 1391 if (__cfg80211_unlink_bss(rdev, res)) 1392 rdev->bss_generation++; 1393 } 1394 } 1395 1396 trace_cfg80211_return_bss(&res->pub); 1397 /* cfg80211_bss_update gives us a referenced result */ 1398 return &res->pub; 1399 } 1400 1401 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy, 1402 struct cfg80211_inform_bss *data, 1403 enum cfg80211_bss_frame_type ftype, 1404 const u8 *bssid, u64 tsf, 1405 u16 beacon_interval, const u8 *ie, 1406 size_t ielen, 1407 struct cfg80211_non_tx_bss *non_tx_data, 1408 gfp_t gfp) 1409 { 1410 const u8 *mbssid_index_ie; 1411 const struct element *elem, *sub; 1412 size_t new_ie_len; 1413 u8 new_bssid[ETH_ALEN]; 1414 u8 *new_ie; 1415 u16 capability; 1416 struct cfg80211_bss *bss; 1417 1418 if (!non_tx_data) 1419 return; 1420 if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen)) 1421 return; 1422 if (!wiphy->support_mbssid) 1423 return; 1424 if (wiphy->support_only_he_mbssid && 1425 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen)) 1426 return; 1427 1428 new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp); 1429 if (!new_ie) 1430 return; 1431 1432 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) { 1433 if (elem->datalen < 4) 1434 continue; 1435 for_each_element(sub, elem->data + 1, elem->datalen - 1) { 1436 if (sub->id != 0 || sub->datalen < 4) { 1437 /* not a valid BSS profile */ 1438 continue; 1439 } 1440 1441 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP || 1442 sub->data[1] != 2) { 1443 /* The first element within the Nontransmitted 1444 * BSSID Profile is not the Nontransmitted 1445 * BSSID Capability element. 1446 */ 1447 continue; 1448 } 1449 1450 /* found a Nontransmitted BSSID Profile */ 1451 mbssid_index_ie = cfg80211_find_ie 1452 (WLAN_EID_MULTI_BSSID_IDX, 1453 sub->data, sub->datalen); 1454 if (!mbssid_index_ie || mbssid_index_ie[1] < 1 || 1455 mbssid_index_ie[2] == 0) { 1456 /* No valid Multiple BSSID-Index element */ 1457 continue; 1458 } 1459 1460 non_tx_data->bssid_index = mbssid_index_ie[2]; 1461 non_tx_data->max_bssid_indicator = elem->data[0]; 1462 1463 cfg80211_gen_new_bssid(bssid, 1464 non_tx_data->max_bssid_indicator, 1465 non_tx_data->bssid_index, 1466 new_bssid); 1467 memset(new_ie, 0, IEEE80211_MAX_DATA_LEN); 1468 new_ie_len = cfg80211_gen_new_ie(ie, ielen, sub->data, 1469 sub->datalen, new_ie, 1470 gfp); 1471 if (!new_ie_len) 1472 continue; 1473 1474 capability = get_unaligned_le16(sub->data + 2); 1475 bss = cfg80211_inform_single_bss_data(wiphy, data, 1476 ftype, 1477 new_bssid, tsf, 1478 capability, 1479 beacon_interval, 1480 new_ie, 1481 new_ie_len, 1482 non_tx_data, 1483 gfp); 1484 if (!bss) 1485 break; 1486 cfg80211_put_bss(wiphy, bss); 1487 } 1488 } 1489 1490 kfree(new_ie); 1491 } 1492 1493 struct cfg80211_bss * 1494 cfg80211_inform_bss_data(struct wiphy *wiphy, 1495 struct cfg80211_inform_bss *data, 1496 enum cfg80211_bss_frame_type ftype, 1497 const u8 *bssid, u64 tsf, u16 capability, 1498 u16 beacon_interval, const u8 *ie, size_t ielen, 1499 gfp_t gfp) 1500 { 1501 struct cfg80211_bss *res; 1502 struct cfg80211_non_tx_bss non_tx_data; 1503 1504 res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf, 1505 capability, beacon_interval, ie, 1506 ielen, NULL, gfp); 1507 non_tx_data.tx_bss = res; 1508 cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf, 1509 beacon_interval, ie, ielen, &non_tx_data, 1510 gfp); 1511 return res; 1512 } 1513 EXPORT_SYMBOL(cfg80211_inform_bss_data); 1514 1515 static void 1516 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy, 1517 struct cfg80211_inform_bss *data, 1518 struct ieee80211_mgmt *mgmt, size_t len, 1519 struct cfg80211_non_tx_bss *non_tx_data, 1520 gfp_t gfp) 1521 { 1522 enum cfg80211_bss_frame_type ftype; 1523 const u8 *ie = mgmt->u.probe_resp.variable; 1524 size_t ielen = len - offsetof(struct ieee80211_mgmt, 1525 u.probe_resp.variable); 1526 1527 ftype = ieee80211_is_beacon(mgmt->frame_control) ? 1528 CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP; 1529 1530 cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid, 1531 le64_to_cpu(mgmt->u.probe_resp.timestamp), 1532 le16_to_cpu(mgmt->u.probe_resp.beacon_int), 1533 ie, ielen, non_tx_data, gfp); 1534 } 1535 1536 static void 1537 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy, 1538 struct cfg80211_bss *nontrans_bss, 1539 struct ieee80211_mgmt *mgmt, size_t len, 1540 gfp_t gfp) 1541 { 1542 u8 *ie, *new_ie, *pos; 1543 const u8 *nontrans_ssid, *trans_ssid, *mbssid; 1544 size_t ielen = len - offsetof(struct ieee80211_mgmt, 1545 u.probe_resp.variable); 1546 size_t new_ie_len; 1547 struct cfg80211_bss_ies *new_ies; 1548 const struct cfg80211_bss_ies *old; 1549 u8 cpy_len; 1550 1551 ie = mgmt->u.probe_resp.variable; 1552 1553 new_ie_len = ielen; 1554 trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen); 1555 if (!trans_ssid) 1556 return; 1557 new_ie_len -= trans_ssid[1]; 1558 mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen); 1559 if (!mbssid) 1560 return; 1561 new_ie_len -= mbssid[1]; 1562 rcu_read_lock(); 1563 nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID); 1564 if (!nontrans_ssid) { 1565 rcu_read_unlock(); 1566 return; 1567 } 1568 new_ie_len += nontrans_ssid[1]; 1569 rcu_read_unlock(); 1570 1571 /* generate new ie for nontrans BSS 1572 * 1. replace SSID with nontrans BSS' SSID 1573 * 2. skip MBSSID IE 1574 */ 1575 new_ie = kzalloc(new_ie_len, gfp); 1576 if (!new_ie) 1577 return; 1578 new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, gfp); 1579 if (!new_ies) 1580 goto out_free; 1581 1582 pos = new_ie; 1583 1584 /* copy the nontransmitted SSID */ 1585 cpy_len = nontrans_ssid[1] + 2; 1586 memcpy(pos, nontrans_ssid, cpy_len); 1587 pos += cpy_len; 1588 /* copy the IEs between SSID and MBSSID */ 1589 cpy_len = trans_ssid[1] + 2; 1590 memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len))); 1591 pos += (mbssid - (trans_ssid + cpy_len)); 1592 /* copy the IEs after MBSSID */ 1593 cpy_len = mbssid[1] + 2; 1594 memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len))); 1595 1596 /* update ie */ 1597 new_ies->len = new_ie_len; 1598 new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp); 1599 new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control); 1600 memcpy(new_ies->data, new_ie, new_ie_len); 1601 if (ieee80211_is_probe_resp(mgmt->frame_control)) { 1602 old = rcu_access_pointer(nontrans_bss->proberesp_ies); 1603 rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies); 1604 rcu_assign_pointer(nontrans_bss->ies, new_ies); 1605 if (old) 1606 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head); 1607 } else { 1608 old = rcu_access_pointer(nontrans_bss->beacon_ies); 1609 rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies); 1610 rcu_assign_pointer(nontrans_bss->ies, new_ies); 1611 if (old) 1612 kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head); 1613 } 1614 1615 out_free: 1616 kfree(new_ie); 1617 } 1618 1619 /* cfg80211_inform_bss_width_frame helper */ 1620 static struct cfg80211_bss * 1621 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy, 1622 struct cfg80211_inform_bss *data, 1623 struct ieee80211_mgmt *mgmt, size_t len, 1624 struct cfg80211_non_tx_bss *non_tx_data, 1625 gfp_t gfp) 1626 { 1627 struct cfg80211_internal_bss tmp = {}, *res; 1628 struct cfg80211_bss_ies *ies; 1629 struct ieee80211_channel *channel; 1630 bool signal_valid; 1631 size_t ielen = len - offsetof(struct ieee80211_mgmt, 1632 u.probe_resp.variable); 1633 int bss_type; 1634 1635 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) != 1636 offsetof(struct ieee80211_mgmt, u.beacon.variable)); 1637 1638 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len); 1639 1640 if (WARN_ON(!mgmt)) 1641 return NULL; 1642 1643 if (WARN_ON(!wiphy)) 1644 return NULL; 1645 1646 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC && 1647 (data->signal < 0 || data->signal > 100))) 1648 return NULL; 1649 1650 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable))) 1651 return NULL; 1652 1653 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable, 1654 ielen, data->chan, data->scan_width); 1655 if (!channel) 1656 return NULL; 1657 1658 ies = kzalloc(sizeof(*ies) + ielen, gfp); 1659 if (!ies) 1660 return NULL; 1661 ies->len = ielen; 1662 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp); 1663 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control); 1664 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen); 1665 1666 if (ieee80211_is_probe_resp(mgmt->frame_control)) 1667 rcu_assign_pointer(tmp.pub.proberesp_ies, ies); 1668 else 1669 rcu_assign_pointer(tmp.pub.beacon_ies, ies); 1670 rcu_assign_pointer(tmp.pub.ies, ies); 1671 1672 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN); 1673 tmp.pub.channel = channel; 1674 tmp.pub.scan_width = data->scan_width; 1675 tmp.pub.signal = data->signal; 1676 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int); 1677 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info); 1678 tmp.ts_boottime = data->boottime_ns; 1679 tmp.parent_tsf = data->parent_tsf; 1680 tmp.pub.chains = data->chains; 1681 memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS); 1682 ether_addr_copy(tmp.parent_bssid, data->parent_bssid); 1683 if (non_tx_data) { 1684 tmp.pub.transmitted_bss = non_tx_data->tx_bss; 1685 tmp.pub.bssid_index = non_tx_data->bssid_index; 1686 tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator; 1687 } 1688 1689 signal_valid = abs(data->chan->center_freq - channel->center_freq) <= 1690 wiphy->max_adj_channel_rssi_comp; 1691 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid); 1692 if (!res) 1693 return NULL; 1694 1695 if (channel->band == NL80211_BAND_60GHZ) { 1696 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK; 1697 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP || 1698 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS) 1699 regulatory_hint_found_beacon(wiphy, channel, gfp); 1700 } else { 1701 if (res->pub.capability & WLAN_CAPABILITY_ESS) 1702 regulatory_hint_found_beacon(wiphy, channel, gfp); 1703 } 1704 1705 trace_cfg80211_return_bss(&res->pub); 1706 /* cfg80211_bss_update gives us a referenced result */ 1707 return &res->pub; 1708 } 1709 1710 struct cfg80211_bss * 1711 cfg80211_inform_bss_frame_data(struct wiphy *wiphy, 1712 struct cfg80211_inform_bss *data, 1713 struct ieee80211_mgmt *mgmt, size_t len, 1714 gfp_t gfp) 1715 { 1716 struct cfg80211_bss *res, *tmp_bss; 1717 const u8 *ie = mgmt->u.probe_resp.variable; 1718 const struct cfg80211_bss_ies *ies1, *ies2; 1719 size_t ielen = len - offsetof(struct ieee80211_mgmt, 1720 u.probe_resp.variable); 1721 struct cfg80211_non_tx_bss non_tx_data; 1722 1723 res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt, 1724 len, NULL, gfp); 1725 if (!res || !wiphy->support_mbssid || 1726 !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen)) 1727 return res; 1728 if (wiphy->support_only_he_mbssid && 1729 !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen)) 1730 return res; 1731 1732 non_tx_data.tx_bss = res; 1733 /* process each non-transmitting bss */ 1734 cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len, 1735 &non_tx_data, gfp); 1736 1737 /* check if the res has other nontransmitting bss which is not 1738 * in MBSSID IE 1739 */ 1740 ies1 = rcu_access_pointer(res->ies); 1741 1742 /* go through nontrans_list, if the timestamp of the BSS is 1743 * earlier than the timestamp of the transmitting BSS then 1744 * update it 1745 */ 1746 list_for_each_entry(tmp_bss, &res->nontrans_list, 1747 nontrans_list) { 1748 ies2 = rcu_access_pointer(tmp_bss->ies); 1749 if (ies2->tsf < ies1->tsf) 1750 cfg80211_update_notlisted_nontrans(wiphy, tmp_bss, 1751 mgmt, len, gfp); 1752 } 1753 1754 return res; 1755 } 1756 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data); 1757 1758 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) 1759 { 1760 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1761 struct cfg80211_internal_bss *bss; 1762 1763 if (!pub) 1764 return; 1765 1766 bss = container_of(pub, struct cfg80211_internal_bss, pub); 1767 1768 spin_lock_bh(&rdev->bss_lock); 1769 bss_ref_get(rdev, bss); 1770 spin_unlock_bh(&rdev->bss_lock); 1771 } 1772 EXPORT_SYMBOL(cfg80211_ref_bss); 1773 1774 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) 1775 { 1776 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1777 struct cfg80211_internal_bss *bss; 1778 1779 if (!pub) 1780 return; 1781 1782 bss = container_of(pub, struct cfg80211_internal_bss, pub); 1783 1784 spin_lock_bh(&rdev->bss_lock); 1785 bss_ref_put(rdev, bss); 1786 spin_unlock_bh(&rdev->bss_lock); 1787 } 1788 EXPORT_SYMBOL(cfg80211_put_bss); 1789 1790 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) 1791 { 1792 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1793 struct cfg80211_internal_bss *bss, *tmp1; 1794 struct cfg80211_bss *nontrans_bss, *tmp; 1795 1796 if (WARN_ON(!pub)) 1797 return; 1798 1799 bss = container_of(pub, struct cfg80211_internal_bss, pub); 1800 1801 spin_lock_bh(&rdev->bss_lock); 1802 if (list_empty(&bss->list)) 1803 goto out; 1804 1805 list_for_each_entry_safe(nontrans_bss, tmp, 1806 &pub->nontrans_list, 1807 nontrans_list) { 1808 tmp1 = container_of(nontrans_bss, 1809 struct cfg80211_internal_bss, pub); 1810 if (__cfg80211_unlink_bss(rdev, tmp1)) 1811 rdev->bss_generation++; 1812 } 1813 1814 if (__cfg80211_unlink_bss(rdev, bss)) 1815 rdev->bss_generation++; 1816 out: 1817 spin_unlock_bh(&rdev->bss_lock); 1818 } 1819 EXPORT_SYMBOL(cfg80211_unlink_bss); 1820 1821 #ifdef CONFIG_CFG80211_WEXT 1822 static struct cfg80211_registered_device * 1823 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex) 1824 { 1825 struct cfg80211_registered_device *rdev; 1826 struct net_device *dev; 1827 1828 ASSERT_RTNL(); 1829 1830 dev = dev_get_by_index(net, ifindex); 1831 if (!dev) 1832 return ERR_PTR(-ENODEV); 1833 if (dev->ieee80211_ptr) 1834 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy); 1835 else 1836 rdev = ERR_PTR(-ENODEV); 1837 dev_put(dev); 1838 return rdev; 1839 } 1840 1841 int cfg80211_wext_siwscan(struct net_device *dev, 1842 struct iw_request_info *info, 1843 union iwreq_data *wrqu, char *extra) 1844 { 1845 struct cfg80211_registered_device *rdev; 1846 struct wiphy *wiphy; 1847 struct iw_scan_req *wreq = NULL; 1848 struct cfg80211_scan_request *creq = NULL; 1849 int i, err, n_channels = 0; 1850 enum nl80211_band band; 1851 1852 if (!netif_running(dev)) 1853 return -ENETDOWN; 1854 1855 if (wrqu->data.length == sizeof(struct iw_scan_req)) 1856 wreq = (struct iw_scan_req *)extra; 1857 1858 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); 1859 1860 if (IS_ERR(rdev)) 1861 return PTR_ERR(rdev); 1862 1863 if (rdev->scan_req || rdev->scan_msg) { 1864 err = -EBUSY; 1865 goto out; 1866 } 1867 1868 wiphy = &rdev->wiphy; 1869 1870 /* Determine number of channels, needed to allocate creq */ 1871 if (wreq && wreq->num_channels) 1872 n_channels = wreq->num_channels; 1873 else 1874 n_channels = ieee80211_get_num_supported_channels(wiphy); 1875 1876 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) + 1877 n_channels * sizeof(void *), 1878 GFP_ATOMIC); 1879 if (!creq) { 1880 err = -ENOMEM; 1881 goto out; 1882 } 1883 1884 creq->wiphy = wiphy; 1885 creq->wdev = dev->ieee80211_ptr; 1886 /* SSIDs come after channels */ 1887 creq->ssids = (void *)&creq->channels[n_channels]; 1888 creq->n_channels = n_channels; 1889 creq->n_ssids = 1; 1890 creq->scan_start = jiffies; 1891 1892 /* translate "Scan on frequencies" request */ 1893 i = 0; 1894 for (band = 0; band < NUM_NL80211_BANDS; band++) { 1895 int j; 1896 1897 if (!wiphy->bands[band]) 1898 continue; 1899 1900 for (j = 0; j < wiphy->bands[band]->n_channels; j++) { 1901 /* ignore disabled channels */ 1902 if (wiphy->bands[band]->channels[j].flags & 1903 IEEE80211_CHAN_DISABLED) 1904 continue; 1905 1906 /* If we have a wireless request structure and the 1907 * wireless request specifies frequencies, then search 1908 * for the matching hardware channel. 1909 */ 1910 if (wreq && wreq->num_channels) { 1911 int k; 1912 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq; 1913 for (k = 0; k < wreq->num_channels; k++) { 1914 struct iw_freq *freq = 1915 &wreq->channel_list[k]; 1916 int wext_freq = 1917 cfg80211_wext_freq(freq); 1918 1919 if (wext_freq == wiphy_freq) 1920 goto wext_freq_found; 1921 } 1922 goto wext_freq_not_found; 1923 } 1924 1925 wext_freq_found: 1926 creq->channels[i] = &wiphy->bands[band]->channels[j]; 1927 i++; 1928 wext_freq_not_found: ; 1929 } 1930 } 1931 /* No channels found? */ 1932 if (!i) { 1933 err = -EINVAL; 1934 goto out; 1935 } 1936 1937 /* Set real number of channels specified in creq->channels[] */ 1938 creq->n_channels = i; 1939 1940 /* translate "Scan for SSID" request */ 1941 if (wreq) { 1942 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) { 1943 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) { 1944 err = -EINVAL; 1945 goto out; 1946 } 1947 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len); 1948 creq->ssids[0].ssid_len = wreq->essid_len; 1949 } 1950 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) 1951 creq->n_ssids = 0; 1952 } 1953 1954 for (i = 0; i < NUM_NL80211_BANDS; i++) 1955 if (wiphy->bands[i]) 1956 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1; 1957 1958 eth_broadcast_addr(creq->bssid); 1959 1960 rdev->scan_req = creq; 1961 err = rdev_scan(rdev, creq); 1962 if (err) { 1963 rdev->scan_req = NULL; 1964 /* creq will be freed below */ 1965 } else { 1966 nl80211_send_scan_start(rdev, dev->ieee80211_ptr); 1967 /* creq now owned by driver */ 1968 creq = NULL; 1969 dev_hold(dev); 1970 } 1971 out: 1972 kfree(creq); 1973 return err; 1974 } 1975 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan); 1976 1977 static char *ieee80211_scan_add_ies(struct iw_request_info *info, 1978 const struct cfg80211_bss_ies *ies, 1979 char *current_ev, char *end_buf) 1980 { 1981 const u8 *pos, *end, *next; 1982 struct iw_event iwe; 1983 1984 if (!ies) 1985 return current_ev; 1986 1987 /* 1988 * If needed, fragment the IEs buffer (at IE boundaries) into short 1989 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages. 1990 */ 1991 pos = ies->data; 1992 end = pos + ies->len; 1993 1994 while (end - pos > IW_GENERIC_IE_MAX) { 1995 next = pos + 2 + pos[1]; 1996 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX) 1997 next = next + 2 + next[1]; 1998 1999 memset(&iwe, 0, sizeof(iwe)); 2000 iwe.cmd = IWEVGENIE; 2001 iwe.u.data.length = next - pos; 2002 current_ev = iwe_stream_add_point_check(info, current_ev, 2003 end_buf, &iwe, 2004 (void *)pos); 2005 if (IS_ERR(current_ev)) 2006 return current_ev; 2007 pos = next; 2008 } 2009 2010 if (end > pos) { 2011 memset(&iwe, 0, sizeof(iwe)); 2012 iwe.cmd = IWEVGENIE; 2013 iwe.u.data.length = end - pos; 2014 current_ev = iwe_stream_add_point_check(info, current_ev, 2015 end_buf, &iwe, 2016 (void *)pos); 2017 if (IS_ERR(current_ev)) 2018 return current_ev; 2019 } 2020 2021 return current_ev; 2022 } 2023 2024 static char * 2025 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info, 2026 struct cfg80211_internal_bss *bss, char *current_ev, 2027 char *end_buf) 2028 { 2029 const struct cfg80211_bss_ies *ies; 2030 struct iw_event iwe; 2031 const u8 *ie; 2032 u8 buf[50]; 2033 u8 *cfg, *p, *tmp; 2034 int rem, i, sig; 2035 bool ismesh = false; 2036 2037 memset(&iwe, 0, sizeof(iwe)); 2038 iwe.cmd = SIOCGIWAP; 2039 iwe.u.ap_addr.sa_family = ARPHRD_ETHER; 2040 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN); 2041 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe, 2042 IW_EV_ADDR_LEN); 2043 if (IS_ERR(current_ev)) 2044 return current_ev; 2045 2046 memset(&iwe, 0, sizeof(iwe)); 2047 iwe.cmd = SIOCGIWFREQ; 2048 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq); 2049 iwe.u.freq.e = 0; 2050 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe, 2051 IW_EV_FREQ_LEN); 2052 if (IS_ERR(current_ev)) 2053 return current_ev; 2054 2055 memset(&iwe, 0, sizeof(iwe)); 2056 iwe.cmd = SIOCGIWFREQ; 2057 iwe.u.freq.m = bss->pub.channel->center_freq; 2058 iwe.u.freq.e = 6; 2059 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe, 2060 IW_EV_FREQ_LEN); 2061 if (IS_ERR(current_ev)) 2062 return current_ev; 2063 2064 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) { 2065 memset(&iwe, 0, sizeof(iwe)); 2066 iwe.cmd = IWEVQUAL; 2067 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED | 2068 IW_QUAL_NOISE_INVALID | 2069 IW_QUAL_QUAL_UPDATED; 2070 switch (wiphy->signal_type) { 2071 case CFG80211_SIGNAL_TYPE_MBM: 2072 sig = bss->pub.signal / 100; 2073 iwe.u.qual.level = sig; 2074 iwe.u.qual.updated |= IW_QUAL_DBM; 2075 if (sig < -110) /* rather bad */ 2076 sig = -110; 2077 else if (sig > -40) /* perfect */ 2078 sig = -40; 2079 /* will give a range of 0 .. 70 */ 2080 iwe.u.qual.qual = sig + 110; 2081 break; 2082 case CFG80211_SIGNAL_TYPE_UNSPEC: 2083 iwe.u.qual.level = bss->pub.signal; 2084 /* will give range 0 .. 100 */ 2085 iwe.u.qual.qual = bss->pub.signal; 2086 break; 2087 default: 2088 /* not reached */ 2089 break; 2090 } 2091 current_ev = iwe_stream_add_event_check(info, current_ev, 2092 end_buf, &iwe, 2093 IW_EV_QUAL_LEN); 2094 if (IS_ERR(current_ev)) 2095 return current_ev; 2096 } 2097 2098 memset(&iwe, 0, sizeof(iwe)); 2099 iwe.cmd = SIOCGIWENCODE; 2100 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY) 2101 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; 2102 else 2103 iwe.u.data.flags = IW_ENCODE_DISABLED; 2104 iwe.u.data.length = 0; 2105 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf, 2106 &iwe, ""); 2107 if (IS_ERR(current_ev)) 2108 return current_ev; 2109 2110 rcu_read_lock(); 2111 ies = rcu_dereference(bss->pub.ies); 2112 rem = ies->len; 2113 ie = ies->data; 2114 2115 while (rem >= 2) { 2116 /* invalid data */ 2117 if (ie[1] > rem - 2) 2118 break; 2119 2120 switch (ie[0]) { 2121 case WLAN_EID_SSID: 2122 memset(&iwe, 0, sizeof(iwe)); 2123 iwe.cmd = SIOCGIWESSID; 2124 iwe.u.data.length = ie[1]; 2125 iwe.u.data.flags = 1; 2126 current_ev = iwe_stream_add_point_check(info, 2127 current_ev, 2128 end_buf, &iwe, 2129 (u8 *)ie + 2); 2130 if (IS_ERR(current_ev)) 2131 goto unlock; 2132 break; 2133 case WLAN_EID_MESH_ID: 2134 memset(&iwe, 0, sizeof(iwe)); 2135 iwe.cmd = SIOCGIWESSID; 2136 iwe.u.data.length = ie[1]; 2137 iwe.u.data.flags = 1; 2138 current_ev = iwe_stream_add_point_check(info, 2139 current_ev, 2140 end_buf, &iwe, 2141 (u8 *)ie + 2); 2142 if (IS_ERR(current_ev)) 2143 goto unlock; 2144 break; 2145 case WLAN_EID_MESH_CONFIG: 2146 ismesh = true; 2147 if (ie[1] != sizeof(struct ieee80211_meshconf_ie)) 2148 break; 2149 cfg = (u8 *)ie + 2; 2150 memset(&iwe, 0, sizeof(iwe)); 2151 iwe.cmd = IWEVCUSTOM; 2152 sprintf(buf, "Mesh Network Path Selection Protocol ID: " 2153 "0x%02X", cfg[0]); 2154 iwe.u.data.length = strlen(buf); 2155 current_ev = iwe_stream_add_point_check(info, 2156 current_ev, 2157 end_buf, 2158 &iwe, buf); 2159 if (IS_ERR(current_ev)) 2160 goto unlock; 2161 sprintf(buf, "Path Selection Metric ID: 0x%02X", 2162 cfg[1]); 2163 iwe.u.data.length = strlen(buf); 2164 current_ev = iwe_stream_add_point_check(info, 2165 current_ev, 2166 end_buf, 2167 &iwe, buf); 2168 if (IS_ERR(current_ev)) 2169 goto unlock; 2170 sprintf(buf, "Congestion Control Mode ID: 0x%02X", 2171 cfg[2]); 2172 iwe.u.data.length = strlen(buf); 2173 current_ev = iwe_stream_add_point_check(info, 2174 current_ev, 2175 end_buf, 2176 &iwe, buf); 2177 if (IS_ERR(current_ev)) 2178 goto unlock; 2179 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]); 2180 iwe.u.data.length = strlen(buf); 2181 current_ev = iwe_stream_add_point_check(info, 2182 current_ev, 2183 end_buf, 2184 &iwe, buf); 2185 if (IS_ERR(current_ev)) 2186 goto unlock; 2187 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]); 2188 iwe.u.data.length = strlen(buf); 2189 current_ev = iwe_stream_add_point_check(info, 2190 current_ev, 2191 end_buf, 2192 &iwe, buf); 2193 if (IS_ERR(current_ev)) 2194 goto unlock; 2195 sprintf(buf, "Formation Info: 0x%02X", cfg[5]); 2196 iwe.u.data.length = strlen(buf); 2197 current_ev = iwe_stream_add_point_check(info, 2198 current_ev, 2199 end_buf, 2200 &iwe, buf); 2201 if (IS_ERR(current_ev)) 2202 goto unlock; 2203 sprintf(buf, "Capabilities: 0x%02X", cfg[6]); 2204 iwe.u.data.length = strlen(buf); 2205 current_ev = iwe_stream_add_point_check(info, 2206 current_ev, 2207 end_buf, 2208 &iwe, buf); 2209 if (IS_ERR(current_ev)) 2210 goto unlock; 2211 break; 2212 case WLAN_EID_SUPP_RATES: 2213 case WLAN_EID_EXT_SUPP_RATES: 2214 /* display all supported rates in readable format */ 2215 p = current_ev + iwe_stream_lcp_len(info); 2216 2217 memset(&iwe, 0, sizeof(iwe)); 2218 iwe.cmd = SIOCGIWRATE; 2219 /* Those two flags are ignored... */ 2220 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0; 2221 2222 for (i = 0; i < ie[1]; i++) { 2223 iwe.u.bitrate.value = 2224 ((ie[i + 2] & 0x7f) * 500000); 2225 tmp = p; 2226 p = iwe_stream_add_value(info, current_ev, p, 2227 end_buf, &iwe, 2228 IW_EV_PARAM_LEN); 2229 if (p == tmp) { 2230 current_ev = ERR_PTR(-E2BIG); 2231 goto unlock; 2232 } 2233 } 2234 current_ev = p; 2235 break; 2236 } 2237 rem -= ie[1] + 2; 2238 ie += ie[1] + 2; 2239 } 2240 2241 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) || 2242 ismesh) { 2243 memset(&iwe, 0, sizeof(iwe)); 2244 iwe.cmd = SIOCGIWMODE; 2245 if (ismesh) 2246 iwe.u.mode = IW_MODE_MESH; 2247 else if (bss->pub.capability & WLAN_CAPABILITY_ESS) 2248 iwe.u.mode = IW_MODE_MASTER; 2249 else 2250 iwe.u.mode = IW_MODE_ADHOC; 2251 current_ev = iwe_stream_add_event_check(info, current_ev, 2252 end_buf, &iwe, 2253 IW_EV_UINT_LEN); 2254 if (IS_ERR(current_ev)) 2255 goto unlock; 2256 } 2257 2258 memset(&iwe, 0, sizeof(iwe)); 2259 iwe.cmd = IWEVCUSTOM; 2260 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf)); 2261 iwe.u.data.length = strlen(buf); 2262 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf, 2263 &iwe, buf); 2264 if (IS_ERR(current_ev)) 2265 goto unlock; 2266 memset(&iwe, 0, sizeof(iwe)); 2267 iwe.cmd = IWEVCUSTOM; 2268 sprintf(buf, " Last beacon: %ums ago", 2269 elapsed_jiffies_msecs(bss->ts)); 2270 iwe.u.data.length = strlen(buf); 2271 current_ev = iwe_stream_add_point_check(info, current_ev, 2272 end_buf, &iwe, buf); 2273 if (IS_ERR(current_ev)) 2274 goto unlock; 2275 2276 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf); 2277 2278 unlock: 2279 rcu_read_unlock(); 2280 return current_ev; 2281 } 2282 2283 2284 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev, 2285 struct iw_request_info *info, 2286 char *buf, size_t len) 2287 { 2288 char *current_ev = buf; 2289 char *end_buf = buf + len; 2290 struct cfg80211_internal_bss *bss; 2291 int err = 0; 2292 2293 spin_lock_bh(&rdev->bss_lock); 2294 cfg80211_bss_expire(rdev); 2295 2296 list_for_each_entry(bss, &rdev->bss_list, list) { 2297 if (buf + len - current_ev <= IW_EV_ADDR_LEN) { 2298 err = -E2BIG; 2299 break; 2300 } 2301 current_ev = ieee80211_bss(&rdev->wiphy, info, bss, 2302 current_ev, end_buf); 2303 if (IS_ERR(current_ev)) { 2304 err = PTR_ERR(current_ev); 2305 break; 2306 } 2307 } 2308 spin_unlock_bh(&rdev->bss_lock); 2309 2310 if (err) 2311 return err; 2312 return current_ev - buf; 2313 } 2314 2315 2316 int cfg80211_wext_giwscan(struct net_device *dev, 2317 struct iw_request_info *info, 2318 struct iw_point *data, char *extra) 2319 { 2320 struct cfg80211_registered_device *rdev; 2321 int res; 2322 2323 if (!netif_running(dev)) 2324 return -ENETDOWN; 2325 2326 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); 2327 2328 if (IS_ERR(rdev)) 2329 return PTR_ERR(rdev); 2330 2331 if (rdev->scan_req || rdev->scan_msg) 2332 return -EAGAIN; 2333 2334 res = ieee80211_scan_results(rdev, info, extra, data->length); 2335 data->length = 0; 2336 if (res >= 0) { 2337 data->length = res; 2338 res = 0; 2339 } 2340 2341 return res; 2342 } 2343 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan); 2344 #endif 2345