1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * This is the linux wireless configuration interface. 4 * 5 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net> 6 * Copyright 2013-2014 Intel Mobile Communications GmbH 7 * Copyright 2015-2017 Intel Deutschland GmbH 8 * Copyright (C) 2018-2024 Intel Corporation 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/if.h> 14 #include <linux/module.h> 15 #include <linux/err.h> 16 #include <linux/list.h> 17 #include <linux/slab.h> 18 #include <linux/nl80211.h> 19 #include <linux/debugfs.h> 20 #include <linux/notifier.h> 21 #include <linux/device.h> 22 #include <linux/etherdevice.h> 23 #include <linux/rtnetlink.h> 24 #include <linux/sched.h> 25 #include <net/genetlink.h> 26 #include <net/cfg80211.h> 27 #include "nl80211.h" 28 #include "core.h" 29 #include "sysfs.h" 30 #include "debugfs.h" 31 #include "wext-compat.h" 32 #include "rdev-ops.h" 33 34 /* name for sysfs, %d is appended */ 35 #define PHY_NAME "phy" 36 37 MODULE_AUTHOR("Johannes Berg"); 38 MODULE_LICENSE("GPL"); 39 MODULE_DESCRIPTION("wireless configuration support"); 40 MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME); 41 42 /* RCU-protected (and RTNL for writers) */ 43 LIST_HEAD(cfg80211_rdev_list); 44 int cfg80211_rdev_list_generation; 45 46 /* for debugfs */ 47 static struct dentry *ieee80211_debugfs_dir; 48 49 /* for the cleanup, scan and event works */ 50 struct workqueue_struct *cfg80211_wq; 51 52 static bool cfg80211_disable_40mhz_24ghz; 53 module_param(cfg80211_disable_40mhz_24ghz, bool, 0644); 54 MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz, 55 "Disable 40MHz support in the 2.4GHz band"); 56 57 struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx) 58 { 59 struct cfg80211_registered_device *result = NULL, *rdev; 60 61 ASSERT_RTNL(); 62 63 for_each_rdev(rdev) { 64 if (rdev->wiphy_idx == wiphy_idx) { 65 result = rdev; 66 break; 67 } 68 } 69 70 return result; 71 } 72 73 int get_wiphy_idx(struct wiphy *wiphy) 74 { 75 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 76 77 return rdev->wiphy_idx; 78 } 79 80 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx) 81 { 82 struct cfg80211_registered_device *rdev; 83 84 ASSERT_RTNL(); 85 86 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx); 87 if (!rdev) 88 return NULL; 89 return &rdev->wiphy; 90 } 91 92 static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev, 93 const char *newname) 94 { 95 struct cfg80211_registered_device *rdev2; 96 int wiphy_idx, taken = -1, digits; 97 98 ASSERT_RTNL(); 99 100 if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN) 101 return -EINVAL; 102 103 /* prohibit calling the thing phy%d when %d is not its number */ 104 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken); 105 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) { 106 /* count number of places needed to print wiphy_idx */ 107 digits = 1; 108 while (wiphy_idx /= 10) 109 digits++; 110 /* 111 * deny the name if it is phy<idx> where <idx> is printed 112 * without leading zeroes. taken == strlen(newname) here 113 */ 114 if (taken == strlen(PHY_NAME) + digits) 115 return -EINVAL; 116 } 117 118 /* Ensure another device does not already have this name. */ 119 for_each_rdev(rdev2) 120 if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0) 121 return -EINVAL; 122 123 return 0; 124 } 125 126 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev, 127 char *newname) 128 { 129 int result; 130 131 ASSERT_RTNL(); 132 lockdep_assert_wiphy(&rdev->wiphy); 133 134 /* Ignore nop renames */ 135 if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0) 136 return 0; 137 138 result = cfg80211_dev_check_name(rdev, newname); 139 if (result < 0) 140 return result; 141 142 result = device_rename(&rdev->wiphy.dev, newname); 143 if (result) 144 return result; 145 146 if (!IS_ERR_OR_NULL(rdev->wiphy.debugfsdir)) 147 debugfs_rename(rdev->wiphy.debugfsdir->d_parent, 148 rdev->wiphy.debugfsdir, 149 rdev->wiphy.debugfsdir->d_parent, newname); 150 151 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY); 152 153 return 0; 154 } 155 156 int cfg80211_switch_netns(struct cfg80211_registered_device *rdev, 157 struct net *net) 158 { 159 struct wireless_dev *wdev; 160 int err = 0; 161 162 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK)) 163 return -EOPNOTSUPP; 164 165 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 166 if (!wdev->netdev) 167 continue; 168 wdev->netdev->netns_local = false; 169 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d"); 170 if (err) 171 break; 172 wdev->netdev->netns_local = true; 173 } 174 175 if (err) { 176 /* failed -- clean up to old netns */ 177 net = wiphy_net(&rdev->wiphy); 178 179 list_for_each_entry_continue_reverse(wdev, 180 &rdev->wiphy.wdev_list, 181 list) { 182 if (!wdev->netdev) 183 continue; 184 wdev->netdev->netns_local = false; 185 err = dev_change_net_namespace(wdev->netdev, net, 186 "wlan%d"); 187 WARN_ON(err); 188 wdev->netdev->netns_local = true; 189 } 190 191 return err; 192 } 193 194 wiphy_lock(&rdev->wiphy); 195 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 196 if (!wdev->netdev) 197 continue; 198 nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE); 199 } 200 201 nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY); 202 203 wiphy_net_set(&rdev->wiphy, net); 204 205 err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev)); 206 WARN_ON(err); 207 208 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY); 209 210 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 211 if (!wdev->netdev) 212 continue; 213 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE); 214 } 215 wiphy_unlock(&rdev->wiphy); 216 217 return 0; 218 } 219 220 static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data) 221 { 222 struct cfg80211_registered_device *rdev = data; 223 224 wiphy_lock(&rdev->wiphy); 225 rdev_rfkill_poll(rdev); 226 wiphy_unlock(&rdev->wiphy); 227 } 228 229 void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev, 230 struct wireless_dev *wdev) 231 { 232 lockdep_assert_held(&rdev->wiphy.mtx); 233 234 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)) 235 return; 236 237 if (!wdev_running(wdev)) 238 return; 239 240 rdev_stop_p2p_device(rdev, wdev); 241 wdev->is_running = false; 242 243 rdev->opencount--; 244 245 if (rdev->scan_req && rdev->scan_req->wdev == wdev) { 246 if (WARN_ON(!rdev->scan_req->notified && 247 (!rdev->int_scan_req || 248 !rdev->int_scan_req->notified))) 249 rdev->scan_req->info.aborted = true; 250 ___cfg80211_scan_done(rdev, false); 251 } 252 } 253 254 void cfg80211_stop_nan(struct cfg80211_registered_device *rdev, 255 struct wireless_dev *wdev) 256 { 257 lockdep_assert_held(&rdev->wiphy.mtx); 258 259 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN)) 260 return; 261 262 if (!wdev_running(wdev)) 263 return; 264 265 rdev_stop_nan(rdev, wdev); 266 wdev->is_running = false; 267 268 rdev->opencount--; 269 } 270 271 void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy) 272 { 273 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 274 struct wireless_dev *wdev; 275 276 ASSERT_RTNL(); 277 278 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 279 if (wdev->netdev) { 280 dev_close(wdev->netdev); 281 continue; 282 } 283 284 /* otherwise, check iftype */ 285 286 wiphy_lock(wiphy); 287 288 switch (wdev->iftype) { 289 case NL80211_IFTYPE_P2P_DEVICE: 290 cfg80211_stop_p2p_device(rdev, wdev); 291 break; 292 case NL80211_IFTYPE_NAN: 293 cfg80211_stop_nan(rdev, wdev); 294 break; 295 default: 296 break; 297 } 298 299 wiphy_unlock(wiphy); 300 } 301 } 302 EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces); 303 304 static int cfg80211_rfkill_set_block(void *data, bool blocked) 305 { 306 struct cfg80211_registered_device *rdev = data; 307 308 if (!blocked) 309 return 0; 310 311 rtnl_lock(); 312 cfg80211_shutdown_all_interfaces(&rdev->wiphy); 313 rtnl_unlock(); 314 315 return 0; 316 } 317 318 static void cfg80211_rfkill_block_work(struct work_struct *work) 319 { 320 struct cfg80211_registered_device *rdev; 321 322 rdev = container_of(work, struct cfg80211_registered_device, 323 rfkill_block); 324 cfg80211_rfkill_set_block(rdev, true); 325 } 326 327 static void cfg80211_event_work(struct work_struct *work) 328 { 329 struct cfg80211_registered_device *rdev; 330 331 rdev = container_of(work, struct cfg80211_registered_device, 332 event_work); 333 334 wiphy_lock(&rdev->wiphy); 335 cfg80211_process_rdev_events(rdev); 336 wiphy_unlock(&rdev->wiphy); 337 } 338 339 void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev) 340 { 341 struct wireless_dev *wdev, *tmp; 342 343 ASSERT_RTNL(); 344 345 list_for_each_entry_safe(wdev, tmp, &rdev->wiphy.wdev_list, list) { 346 if (wdev->nl_owner_dead) { 347 if (wdev->netdev) 348 dev_close(wdev->netdev); 349 350 wiphy_lock(&rdev->wiphy); 351 cfg80211_leave(rdev, wdev); 352 cfg80211_remove_virtual_intf(rdev, wdev); 353 wiphy_unlock(&rdev->wiphy); 354 } 355 } 356 } 357 358 static void cfg80211_destroy_iface_wk(struct work_struct *work) 359 { 360 struct cfg80211_registered_device *rdev; 361 362 rdev = container_of(work, struct cfg80211_registered_device, 363 destroy_work); 364 365 rtnl_lock(); 366 cfg80211_destroy_ifaces(rdev); 367 rtnl_unlock(); 368 } 369 370 static void cfg80211_sched_scan_stop_wk(struct wiphy *wiphy, 371 struct wiphy_work *work) 372 { 373 struct cfg80211_registered_device *rdev; 374 struct cfg80211_sched_scan_request *req, *tmp; 375 376 rdev = container_of(work, struct cfg80211_registered_device, 377 sched_scan_stop_wk); 378 379 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) { 380 if (req->nl_owner_dead) 381 cfg80211_stop_sched_scan_req(rdev, req, false); 382 } 383 } 384 385 static void cfg80211_propagate_radar_detect_wk(struct work_struct *work) 386 { 387 struct cfg80211_registered_device *rdev; 388 389 rdev = container_of(work, struct cfg80211_registered_device, 390 propagate_radar_detect_wk); 391 392 rtnl_lock(); 393 394 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->radar_chandef, 395 NL80211_DFS_UNAVAILABLE, 396 NL80211_RADAR_DETECTED); 397 398 rtnl_unlock(); 399 } 400 401 static void cfg80211_propagate_cac_done_wk(struct work_struct *work) 402 { 403 struct cfg80211_registered_device *rdev; 404 405 rdev = container_of(work, struct cfg80211_registered_device, 406 propagate_cac_done_wk); 407 408 rtnl_lock(); 409 410 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->cac_done_chandef, 411 NL80211_DFS_AVAILABLE, 412 NL80211_RADAR_CAC_FINISHED); 413 414 rtnl_unlock(); 415 } 416 417 static void cfg80211_wiphy_work(struct work_struct *work) 418 { 419 struct cfg80211_registered_device *rdev; 420 struct wiphy_work *wk; 421 422 rdev = container_of(work, struct cfg80211_registered_device, wiphy_work); 423 424 trace_wiphy_work_worker_start(&rdev->wiphy); 425 426 wiphy_lock(&rdev->wiphy); 427 if (rdev->suspended) 428 goto out; 429 430 spin_lock_irq(&rdev->wiphy_work_lock); 431 wk = list_first_entry_or_null(&rdev->wiphy_work_list, 432 struct wiphy_work, entry); 433 if (wk) { 434 list_del_init(&wk->entry); 435 if (!list_empty(&rdev->wiphy_work_list)) 436 queue_work(system_unbound_wq, work); 437 spin_unlock_irq(&rdev->wiphy_work_lock); 438 439 trace_wiphy_work_run(&rdev->wiphy, wk); 440 wk->func(&rdev->wiphy, wk); 441 } else { 442 spin_unlock_irq(&rdev->wiphy_work_lock); 443 } 444 out: 445 wiphy_unlock(&rdev->wiphy); 446 } 447 448 /* exported functions */ 449 450 struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv, 451 const char *requested_name) 452 { 453 static atomic_t wiphy_counter = ATOMIC_INIT(0); 454 455 struct cfg80211_registered_device *rdev; 456 int alloc_size; 457 458 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key)); 459 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc)); 460 WARN_ON(ops->connect && !ops->disconnect); 461 WARN_ON(ops->join_ibss && !ops->leave_ibss); 462 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf); 463 WARN_ON(ops->add_station && !ops->del_station); 464 WARN_ON(ops->add_mpath && !ops->del_mpath); 465 WARN_ON(ops->join_mesh && !ops->leave_mesh); 466 WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device); 467 WARN_ON(ops->start_ap && !ops->stop_ap); 468 WARN_ON(ops->join_ocb && !ops->leave_ocb); 469 WARN_ON(ops->suspend && !ops->resume); 470 WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop); 471 WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel); 472 WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch); 473 WARN_ON(ops->add_tx_ts && !ops->del_tx_ts); 474 475 alloc_size = sizeof(*rdev) + sizeof_priv; 476 477 rdev = kzalloc(alloc_size, GFP_KERNEL); 478 if (!rdev) 479 return NULL; 480 481 rdev->ops = ops; 482 483 rdev->wiphy_idx = atomic_inc_return(&wiphy_counter); 484 485 if (unlikely(rdev->wiphy_idx < 0)) { 486 /* ugh, wrapped! */ 487 atomic_dec(&wiphy_counter); 488 kfree(rdev); 489 return NULL; 490 } 491 492 /* atomic_inc_return makes it start at 1, make it start at 0 */ 493 rdev->wiphy_idx--; 494 495 /* give it a proper name */ 496 if (requested_name && requested_name[0]) { 497 int rv; 498 499 rtnl_lock(); 500 rv = cfg80211_dev_check_name(rdev, requested_name); 501 502 if (rv < 0) { 503 rtnl_unlock(); 504 goto use_default_name; 505 } 506 507 rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name); 508 rtnl_unlock(); 509 if (rv) 510 goto use_default_name; 511 } else { 512 int rv; 513 514 use_default_name: 515 /* NOTE: This is *probably* safe w/out holding rtnl because of 516 * the restrictions on phy names. Probably this call could 517 * fail if some other part of the kernel (re)named a device 518 * phyX. But, might should add some locking and check return 519 * value, and use a different name if this one exists? 520 */ 521 rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx); 522 if (rv < 0) { 523 kfree(rdev); 524 return NULL; 525 } 526 } 527 528 mutex_init(&rdev->wiphy.mtx); 529 INIT_LIST_HEAD(&rdev->wiphy.wdev_list); 530 INIT_LIST_HEAD(&rdev->beacon_registrations); 531 spin_lock_init(&rdev->beacon_registrations_lock); 532 spin_lock_init(&rdev->bss_lock); 533 INIT_LIST_HEAD(&rdev->bss_list); 534 INIT_LIST_HEAD(&rdev->sched_scan_req_list); 535 wiphy_work_init(&rdev->scan_done_wk, __cfg80211_scan_done); 536 INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk, 537 cfg80211_dfs_channels_update_work); 538 #ifdef CONFIG_CFG80211_WEXT 539 rdev->wiphy.wext = &cfg80211_wext_handler; 540 #endif 541 542 device_initialize(&rdev->wiphy.dev); 543 rdev->wiphy.dev.class = &ieee80211_class; 544 rdev->wiphy.dev.platform_data = rdev; 545 device_enable_async_suspend(&rdev->wiphy.dev); 546 547 INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk); 548 wiphy_work_init(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk); 549 INIT_WORK(&rdev->sched_scan_res_wk, cfg80211_sched_scan_results_wk); 550 INIT_WORK(&rdev->propagate_radar_detect_wk, 551 cfg80211_propagate_radar_detect_wk); 552 INIT_WORK(&rdev->propagate_cac_done_wk, cfg80211_propagate_cac_done_wk); 553 INIT_WORK(&rdev->mgmt_registrations_update_wk, 554 cfg80211_mgmt_registrations_update_wk); 555 spin_lock_init(&rdev->mgmt_registrations_lock); 556 557 #ifdef CONFIG_CFG80211_DEFAULT_PS 558 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT; 559 #endif 560 561 wiphy_net_set(&rdev->wiphy, &init_net); 562 563 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block; 564 rdev->wiphy.rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev), 565 &rdev->wiphy.dev, RFKILL_TYPE_WLAN, 566 &rdev->rfkill_ops, rdev); 567 568 if (!rdev->wiphy.rfkill) { 569 wiphy_free(&rdev->wiphy); 570 return NULL; 571 } 572 573 INIT_WORK(&rdev->wiphy_work, cfg80211_wiphy_work); 574 INIT_LIST_HEAD(&rdev->wiphy_work_list); 575 spin_lock_init(&rdev->wiphy_work_lock); 576 INIT_WORK(&rdev->rfkill_block, cfg80211_rfkill_block_work); 577 INIT_WORK(&rdev->conn_work, cfg80211_conn_work); 578 INIT_WORK(&rdev->event_work, cfg80211_event_work); 579 INIT_WORK(&rdev->background_cac_abort_wk, 580 cfg80211_background_cac_abort_wk); 581 INIT_DELAYED_WORK(&rdev->background_cac_done_wk, 582 cfg80211_background_cac_done_wk); 583 584 init_waitqueue_head(&rdev->dev_wait); 585 586 /* 587 * Initialize wiphy parameters to IEEE 802.11 MIB default values. 588 * Fragmentation and RTS threshold are disabled by default with the 589 * special -1 value. 590 */ 591 rdev->wiphy.retry_short = 7; 592 rdev->wiphy.retry_long = 4; 593 rdev->wiphy.frag_threshold = (u32) -1; 594 rdev->wiphy.rts_threshold = (u32) -1; 595 rdev->wiphy.coverage_class = 0; 596 597 rdev->wiphy.max_num_csa_counters = 1; 598 599 rdev->wiphy.max_sched_scan_plans = 1; 600 rdev->wiphy.max_sched_scan_plan_interval = U32_MAX; 601 602 return &rdev->wiphy; 603 } 604 EXPORT_SYMBOL(wiphy_new_nm); 605 606 static int wiphy_verify_combinations(struct wiphy *wiphy) 607 { 608 const struct ieee80211_iface_combination *c; 609 int i, j; 610 611 for (i = 0; i < wiphy->n_iface_combinations; i++) { 612 u32 cnt = 0; 613 u16 all_iftypes = 0; 614 615 c = &wiphy->iface_combinations[i]; 616 617 /* 618 * Combinations with just one interface aren't real, 619 * however we make an exception for DFS. 620 */ 621 if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths)) 622 return -EINVAL; 623 624 /* Need at least one channel */ 625 if (WARN_ON(!c->num_different_channels)) 626 return -EINVAL; 627 628 /* DFS only works on one channel. */ 629 if (WARN_ON(c->radar_detect_widths && 630 (c->num_different_channels > 1))) 631 return -EINVAL; 632 633 if (WARN_ON(!c->n_limits)) 634 return -EINVAL; 635 636 for (j = 0; j < c->n_limits; j++) { 637 u16 types = c->limits[j].types; 638 639 /* interface types shouldn't overlap */ 640 if (WARN_ON(types & all_iftypes)) 641 return -EINVAL; 642 all_iftypes |= types; 643 644 if (WARN_ON(!c->limits[j].max)) 645 return -EINVAL; 646 647 /* Shouldn't list software iftypes in combinations! */ 648 if (WARN_ON(wiphy->software_iftypes & types)) 649 return -EINVAL; 650 651 /* Only a single P2P_DEVICE can be allowed */ 652 if (WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) && 653 c->limits[j].max > 1)) 654 return -EINVAL; 655 656 /* Only a single NAN can be allowed */ 657 if (WARN_ON(types & BIT(NL80211_IFTYPE_NAN) && 658 c->limits[j].max > 1)) 659 return -EINVAL; 660 661 /* 662 * This isn't well-defined right now. If you have an 663 * IBSS interface, then its beacon interval may change 664 * by joining other networks, and nothing prevents it 665 * from doing that. 666 * So technically we probably shouldn't even allow AP 667 * and IBSS in the same interface, but it seems that 668 * some drivers support that, possibly only with fixed 669 * beacon intervals for IBSS. 670 */ 671 if (WARN_ON(types & BIT(NL80211_IFTYPE_ADHOC) && 672 c->beacon_int_min_gcd)) { 673 return -EINVAL; 674 } 675 676 cnt += c->limits[j].max; 677 /* 678 * Don't advertise an unsupported type 679 * in a combination. 680 */ 681 if (WARN_ON((wiphy->interface_modes & types) != types)) 682 return -EINVAL; 683 } 684 685 if (WARN_ON(all_iftypes & BIT(NL80211_IFTYPE_WDS))) 686 return -EINVAL; 687 688 /* You can't even choose that many! */ 689 if (WARN_ON(cnt < c->max_interfaces)) 690 return -EINVAL; 691 } 692 693 return 0; 694 } 695 696 int wiphy_register(struct wiphy *wiphy) 697 { 698 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 699 int res; 700 enum nl80211_band band; 701 struct ieee80211_supported_band *sband; 702 bool have_band = false; 703 int i; 704 u16 ifmodes = wiphy->interface_modes; 705 706 #ifdef CONFIG_PM 707 if (WARN_ON(wiphy->wowlan && 708 (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) && 709 !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY))) 710 return -EINVAL; 711 if (WARN_ON(wiphy->wowlan && 712 !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns && 713 !wiphy->wowlan->tcp)) 714 return -EINVAL; 715 #endif 716 if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) && 717 (!rdev->ops->tdls_channel_switch || 718 !rdev->ops->tdls_cancel_channel_switch))) 719 return -EINVAL; 720 721 if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) && 722 (!rdev->ops->start_nan || !rdev->ops->stop_nan || 723 !rdev->ops->add_nan_func || !rdev->ops->del_nan_func || 724 !(wiphy->nan_supported_bands & BIT(NL80211_BAND_2GHZ))))) 725 return -EINVAL; 726 727 if (WARN_ON(wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS))) 728 return -EINVAL; 729 730 if (WARN_ON(wiphy->pmsr_capa && !wiphy->pmsr_capa->ftm.supported)) 731 return -EINVAL; 732 733 if (wiphy->pmsr_capa && wiphy->pmsr_capa->ftm.supported) { 734 if (WARN_ON(!wiphy->pmsr_capa->ftm.asap && 735 !wiphy->pmsr_capa->ftm.non_asap)) 736 return -EINVAL; 737 if (WARN_ON(!wiphy->pmsr_capa->ftm.preambles || 738 !wiphy->pmsr_capa->ftm.bandwidths)) 739 return -EINVAL; 740 if (WARN_ON(wiphy->pmsr_capa->ftm.preambles & 741 ~(BIT(NL80211_PREAMBLE_LEGACY) | 742 BIT(NL80211_PREAMBLE_HT) | 743 BIT(NL80211_PREAMBLE_VHT) | 744 BIT(NL80211_PREAMBLE_HE) | 745 BIT(NL80211_PREAMBLE_DMG)))) 746 return -EINVAL; 747 if (WARN_ON((wiphy->pmsr_capa->ftm.trigger_based || 748 wiphy->pmsr_capa->ftm.non_trigger_based) && 749 !(wiphy->pmsr_capa->ftm.preambles & 750 BIT(NL80211_PREAMBLE_HE)))) 751 return -EINVAL; 752 if (WARN_ON(wiphy->pmsr_capa->ftm.bandwidths & 753 ~(BIT(NL80211_CHAN_WIDTH_20_NOHT) | 754 BIT(NL80211_CHAN_WIDTH_20) | 755 BIT(NL80211_CHAN_WIDTH_40) | 756 BIT(NL80211_CHAN_WIDTH_80) | 757 BIT(NL80211_CHAN_WIDTH_80P80) | 758 BIT(NL80211_CHAN_WIDTH_160) | 759 BIT(NL80211_CHAN_WIDTH_5) | 760 BIT(NL80211_CHAN_WIDTH_10)))) 761 return -EINVAL; 762 } 763 764 if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) && 765 (wiphy->regulatory_flags & 766 (REGULATORY_CUSTOM_REG | 767 REGULATORY_STRICT_REG | 768 REGULATORY_COUNTRY_IE_FOLLOW_POWER | 769 REGULATORY_COUNTRY_IE_IGNORE)))) 770 return -EINVAL; 771 772 if (WARN_ON(wiphy->coalesce && 773 (!wiphy->coalesce->n_rules || 774 !wiphy->coalesce->n_patterns) && 775 (!wiphy->coalesce->pattern_min_len || 776 wiphy->coalesce->pattern_min_len > 777 wiphy->coalesce->pattern_max_len))) 778 return -EINVAL; 779 780 if (WARN_ON(wiphy->ap_sme_capa && 781 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME))) 782 return -EINVAL; 783 784 if (WARN_ON(wiphy->addresses && !wiphy->n_addresses)) 785 return -EINVAL; 786 787 if (WARN_ON(wiphy->addresses && 788 !is_zero_ether_addr(wiphy->perm_addr) && 789 memcmp(wiphy->perm_addr, wiphy->addresses[0].addr, 790 ETH_ALEN))) 791 return -EINVAL; 792 793 if (WARN_ON(wiphy->max_acl_mac_addrs && 794 (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) || 795 !rdev->ops->set_mac_acl))) 796 return -EINVAL; 797 798 /* assure only valid behaviours are flagged by driver 799 * hence subtract 2 as bit 0 is invalid. 800 */ 801 if (WARN_ON(wiphy->bss_select_support && 802 (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2)))) 803 return -EINVAL; 804 805 if (WARN_ON(wiphy_ext_feature_isset(&rdev->wiphy, 806 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) && 807 (!rdev->ops->set_pmk || !rdev->ops->del_pmk))) 808 return -EINVAL; 809 810 if (WARN_ON(!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) && 811 rdev->ops->update_connect_params)) 812 return -EINVAL; 813 814 if (wiphy->addresses) 815 memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN); 816 817 /* sanity check ifmodes */ 818 WARN_ON(!ifmodes); 819 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1; 820 if (WARN_ON(ifmodes != wiphy->interface_modes)) 821 wiphy->interface_modes = ifmodes; 822 823 res = wiphy_verify_combinations(wiphy); 824 if (res) 825 return res; 826 827 /* sanity check supported bands/channels */ 828 for (band = 0; band < NUM_NL80211_BANDS; band++) { 829 const struct ieee80211_sband_iftype_data *iftd; 830 u16 types = 0; 831 bool have_he = false; 832 833 sband = wiphy->bands[band]; 834 if (!sband) 835 continue; 836 837 sband->band = band; 838 if (WARN_ON(!sband->n_channels)) 839 return -EINVAL; 840 /* 841 * on 60GHz or sub-1Ghz band, there are no legacy rates, so 842 * n_bitrates is 0 843 */ 844 if (WARN_ON((band != NL80211_BAND_60GHZ && 845 band != NL80211_BAND_S1GHZ) && 846 !sband->n_bitrates)) 847 return -EINVAL; 848 849 if (WARN_ON(band == NL80211_BAND_6GHZ && 850 (sband->ht_cap.ht_supported || 851 sband->vht_cap.vht_supported))) 852 return -EINVAL; 853 854 /* 855 * Since cfg80211_disable_40mhz_24ghz is global, we can 856 * modify the sband's ht data even if the driver uses a 857 * global structure for that. 858 */ 859 if (cfg80211_disable_40mhz_24ghz && 860 band == NL80211_BAND_2GHZ && 861 sband->ht_cap.ht_supported) { 862 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40; 863 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40; 864 } 865 866 /* 867 * Since we use a u32 for rate bitmaps in 868 * ieee80211_get_response_rate, we cannot 869 * have more than 32 legacy rates. 870 */ 871 if (WARN_ON(sband->n_bitrates > 32)) 872 return -EINVAL; 873 874 for (i = 0; i < sband->n_channels; i++) { 875 sband->channels[i].orig_flags = 876 sband->channels[i].flags; 877 sband->channels[i].orig_mag = INT_MAX; 878 sband->channels[i].orig_mpwr = 879 sband->channels[i].max_power; 880 sband->channels[i].band = band; 881 882 if (WARN_ON(sband->channels[i].freq_offset >= 1000)) 883 return -EINVAL; 884 } 885 886 for_each_sband_iftype_data(sband, i, iftd) { 887 bool has_ap, has_non_ap; 888 u32 ap_bits = BIT(NL80211_IFTYPE_AP) | 889 BIT(NL80211_IFTYPE_P2P_GO); 890 891 if (WARN_ON(!iftd->types_mask)) 892 return -EINVAL; 893 if (WARN_ON(types & iftd->types_mask)) 894 return -EINVAL; 895 896 /* at least one piece of information must be present */ 897 if (WARN_ON(!iftd->he_cap.has_he)) 898 return -EINVAL; 899 900 types |= iftd->types_mask; 901 902 if (i == 0) 903 have_he = iftd->he_cap.has_he; 904 else 905 have_he = have_he && 906 iftd->he_cap.has_he; 907 908 has_ap = iftd->types_mask & ap_bits; 909 has_non_ap = iftd->types_mask & ~ap_bits; 910 911 /* 912 * For EHT 20 MHz STA, the capabilities format differs 913 * but to simplify, don't check 20 MHz but rather check 914 * only if AP and non-AP were mentioned at the same time, 915 * reject if so. 916 */ 917 if (WARN_ON(iftd->eht_cap.has_eht && 918 has_ap && has_non_ap)) 919 return -EINVAL; 920 } 921 922 if (WARN_ON(!have_he && band == NL80211_BAND_6GHZ)) 923 return -EINVAL; 924 925 have_band = true; 926 } 927 928 if (!have_band) { 929 WARN_ON(1); 930 return -EINVAL; 931 } 932 933 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) { 934 /* 935 * Validate we have a policy (can be explicitly set to 936 * VENDOR_CMD_RAW_DATA which is non-NULL) and also that 937 * we have at least one of doit/dumpit. 938 */ 939 if (WARN_ON(!rdev->wiphy.vendor_commands[i].policy)) 940 return -EINVAL; 941 if (WARN_ON(!rdev->wiphy.vendor_commands[i].doit && 942 !rdev->wiphy.vendor_commands[i].dumpit)) 943 return -EINVAL; 944 } 945 946 #ifdef CONFIG_PM 947 if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns && 948 (!rdev->wiphy.wowlan->pattern_min_len || 949 rdev->wiphy.wowlan->pattern_min_len > 950 rdev->wiphy.wowlan->pattern_max_len))) 951 return -EINVAL; 952 #endif 953 954 if (!wiphy->max_num_akm_suites) 955 wiphy->max_num_akm_suites = NL80211_MAX_NR_AKM_SUITES; 956 else if (wiphy->max_num_akm_suites < NL80211_MAX_NR_AKM_SUITES || 957 wiphy->max_num_akm_suites > CFG80211_MAX_NUM_AKM_SUITES) 958 return -EINVAL; 959 960 /* check and set up bitrates */ 961 ieee80211_set_bitrate_flags(wiphy); 962 963 rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH; 964 965 rtnl_lock(); 966 wiphy_lock(&rdev->wiphy); 967 res = device_add(&rdev->wiphy.dev); 968 if (res) { 969 wiphy_unlock(&rdev->wiphy); 970 rtnl_unlock(); 971 return res; 972 } 973 974 list_add_rcu(&rdev->list, &cfg80211_rdev_list); 975 cfg80211_rdev_list_generation++; 976 977 /* add to debugfs */ 978 rdev->wiphy.debugfsdir = debugfs_create_dir(wiphy_name(&rdev->wiphy), 979 ieee80211_debugfs_dir); 980 981 cfg80211_debugfs_rdev_add(rdev); 982 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY); 983 wiphy_unlock(&rdev->wiphy); 984 985 /* set up regulatory info */ 986 wiphy_regulatory_register(wiphy); 987 988 if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) { 989 struct regulatory_request request; 990 991 request.wiphy_idx = get_wiphy_idx(wiphy); 992 request.initiator = NL80211_REGDOM_SET_BY_DRIVER; 993 request.alpha2[0] = '9'; 994 request.alpha2[1] = '9'; 995 996 nl80211_send_reg_change_event(&request); 997 } 998 999 /* Check that nobody globally advertises any capabilities they do not 1000 * advertise on all possible interface types. 1001 */ 1002 if (wiphy->extended_capabilities_len && 1003 wiphy->num_iftype_ext_capab && 1004 wiphy->iftype_ext_capab) { 1005 u8 supported_on_all, j; 1006 const struct wiphy_iftype_ext_capab *capab; 1007 1008 capab = wiphy->iftype_ext_capab; 1009 for (j = 0; j < wiphy->extended_capabilities_len; j++) { 1010 if (capab[0].extended_capabilities_len > j) 1011 supported_on_all = 1012 capab[0].extended_capabilities[j]; 1013 else 1014 supported_on_all = 0x00; 1015 for (i = 1; i < wiphy->num_iftype_ext_capab; i++) { 1016 if (j >= capab[i].extended_capabilities_len) { 1017 supported_on_all = 0x00; 1018 break; 1019 } 1020 supported_on_all &= 1021 capab[i].extended_capabilities[j]; 1022 } 1023 if (WARN_ON(wiphy->extended_capabilities[j] & 1024 ~supported_on_all)) 1025 break; 1026 } 1027 } 1028 1029 rdev->wiphy.registered = true; 1030 rtnl_unlock(); 1031 1032 res = rfkill_register(rdev->wiphy.rfkill); 1033 if (res) { 1034 rfkill_destroy(rdev->wiphy.rfkill); 1035 rdev->wiphy.rfkill = NULL; 1036 wiphy_unregister(&rdev->wiphy); 1037 return res; 1038 } 1039 1040 return 0; 1041 } 1042 EXPORT_SYMBOL(wiphy_register); 1043 1044 void wiphy_rfkill_start_polling(struct wiphy *wiphy) 1045 { 1046 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1047 1048 if (!rdev->ops->rfkill_poll) 1049 return; 1050 rdev->rfkill_ops.poll = cfg80211_rfkill_poll; 1051 rfkill_resume_polling(wiphy->rfkill); 1052 } 1053 EXPORT_SYMBOL(wiphy_rfkill_start_polling); 1054 1055 void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev, 1056 struct wiphy_work *end) 1057 { 1058 unsigned int runaway_limit = 100; 1059 unsigned long flags; 1060 1061 lockdep_assert_held(&rdev->wiphy.mtx); 1062 1063 spin_lock_irqsave(&rdev->wiphy_work_lock, flags); 1064 while (!list_empty(&rdev->wiphy_work_list)) { 1065 struct wiphy_work *wk; 1066 1067 wk = list_first_entry(&rdev->wiphy_work_list, 1068 struct wiphy_work, entry); 1069 list_del_init(&wk->entry); 1070 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags); 1071 1072 trace_wiphy_work_run(&rdev->wiphy, wk); 1073 wk->func(&rdev->wiphy, wk); 1074 1075 spin_lock_irqsave(&rdev->wiphy_work_lock, flags); 1076 1077 if (wk == end) 1078 break; 1079 1080 if (WARN_ON(--runaway_limit == 0)) 1081 INIT_LIST_HEAD(&rdev->wiphy_work_list); 1082 } 1083 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags); 1084 } 1085 1086 void wiphy_unregister(struct wiphy *wiphy) 1087 { 1088 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1089 1090 wait_event(rdev->dev_wait, ({ 1091 int __count; 1092 wiphy_lock(&rdev->wiphy); 1093 __count = rdev->opencount; 1094 wiphy_unlock(&rdev->wiphy); 1095 __count == 0; })); 1096 1097 if (rdev->wiphy.rfkill) 1098 rfkill_unregister(rdev->wiphy.rfkill); 1099 1100 rtnl_lock(); 1101 wiphy_lock(&rdev->wiphy); 1102 nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY); 1103 rdev->wiphy.registered = false; 1104 1105 WARN_ON(!list_empty(&rdev->wiphy.wdev_list)); 1106 1107 /* 1108 * First remove the hardware from everywhere, this makes 1109 * it impossible to find from userspace. 1110 */ 1111 debugfs_remove_recursive(rdev->wiphy.debugfsdir); 1112 list_del_rcu(&rdev->list); 1113 synchronize_rcu(); 1114 1115 /* 1116 * If this device got a regulatory hint tell core its 1117 * free to listen now to a new shiny device regulatory hint 1118 */ 1119 wiphy_regulatory_deregister(wiphy); 1120 1121 cfg80211_rdev_list_generation++; 1122 device_del(&rdev->wiphy.dev); 1123 1124 #ifdef CONFIG_PM 1125 if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup) 1126 rdev_set_wakeup(rdev, false); 1127 #endif 1128 1129 /* surely nothing is reachable now, clean up work */ 1130 cfg80211_process_wiphy_works(rdev, NULL); 1131 wiphy_unlock(&rdev->wiphy); 1132 rtnl_unlock(); 1133 1134 /* this has nothing to do now but make sure it's gone */ 1135 cancel_work_sync(&rdev->wiphy_work); 1136 1137 cancel_work_sync(&rdev->conn_work); 1138 flush_work(&rdev->event_work); 1139 cancel_delayed_work_sync(&rdev->dfs_update_channels_wk); 1140 cancel_delayed_work_sync(&rdev->background_cac_done_wk); 1141 flush_work(&rdev->destroy_work); 1142 flush_work(&rdev->propagate_radar_detect_wk); 1143 flush_work(&rdev->propagate_cac_done_wk); 1144 flush_work(&rdev->mgmt_registrations_update_wk); 1145 flush_work(&rdev->background_cac_abort_wk); 1146 1147 cfg80211_rdev_free_wowlan(rdev); 1148 cfg80211_free_coalesce(rdev->coalesce); 1149 rdev->coalesce = NULL; 1150 } 1151 EXPORT_SYMBOL(wiphy_unregister); 1152 1153 void cfg80211_dev_free(struct cfg80211_registered_device *rdev) 1154 { 1155 struct cfg80211_internal_bss *scan, *tmp; 1156 struct cfg80211_beacon_registration *reg, *treg; 1157 rfkill_destroy(rdev->wiphy.rfkill); 1158 list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) { 1159 list_del(®->list); 1160 kfree(reg); 1161 } 1162 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list) 1163 cfg80211_put_bss(&rdev->wiphy, &scan->pub); 1164 mutex_destroy(&rdev->wiphy.mtx); 1165 1166 /* 1167 * The 'regd' can only be non-NULL if we never finished 1168 * initializing the wiphy and thus never went through the 1169 * unregister path - e.g. in failure scenarios. Thus, it 1170 * cannot have been visible to anyone if non-NULL, so we 1171 * can just free it here. 1172 */ 1173 kfree(rcu_dereference_raw(rdev->wiphy.regd)); 1174 1175 kfree(rdev); 1176 } 1177 1178 void wiphy_free(struct wiphy *wiphy) 1179 { 1180 put_device(&wiphy->dev); 1181 } 1182 EXPORT_SYMBOL(wiphy_free); 1183 1184 void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked, 1185 enum rfkill_hard_block_reasons reason) 1186 { 1187 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1188 1189 if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason)) 1190 schedule_work(&rdev->rfkill_block); 1191 } 1192 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason); 1193 1194 static void _cfg80211_unregister_wdev(struct wireless_dev *wdev, 1195 bool unregister_netdev) 1196 { 1197 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 1198 struct cfg80211_cqm_config *cqm_config; 1199 unsigned int link_id; 1200 1201 ASSERT_RTNL(); 1202 lockdep_assert_held(&rdev->wiphy.mtx); 1203 1204 nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE); 1205 1206 wdev->registered = false; 1207 1208 if (wdev->netdev) { 1209 sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211"); 1210 if (unregister_netdev) 1211 unregister_netdevice(wdev->netdev); 1212 } 1213 1214 list_del_rcu(&wdev->list); 1215 synchronize_net(); 1216 rdev->devlist_generation++; 1217 1218 cfg80211_mlme_purge_registrations(wdev); 1219 1220 switch (wdev->iftype) { 1221 case NL80211_IFTYPE_P2P_DEVICE: 1222 cfg80211_stop_p2p_device(rdev, wdev); 1223 break; 1224 case NL80211_IFTYPE_NAN: 1225 cfg80211_stop_nan(rdev, wdev); 1226 break; 1227 default: 1228 break; 1229 } 1230 1231 #ifdef CONFIG_CFG80211_WEXT 1232 kfree_sensitive(wdev->wext.keys); 1233 wdev->wext.keys = NULL; 1234 #endif 1235 wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work); 1236 /* deleted from the list, so can't be found from nl80211 any more */ 1237 cqm_config = rcu_access_pointer(wdev->cqm_config); 1238 kfree_rcu(cqm_config, rcu_head); 1239 1240 /* 1241 * Ensure that all events have been processed and 1242 * freed. 1243 */ 1244 cfg80211_process_wdev_events(wdev); 1245 1246 if (wdev->iftype == NL80211_IFTYPE_STATION || 1247 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) { 1248 for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) { 1249 struct cfg80211_internal_bss *curbss; 1250 1251 curbss = wdev->links[link_id].client.current_bss; 1252 1253 if (WARN_ON(curbss)) { 1254 cfg80211_unhold_bss(curbss); 1255 cfg80211_put_bss(wdev->wiphy, &curbss->pub); 1256 wdev->links[link_id].client.current_bss = NULL; 1257 } 1258 } 1259 } 1260 1261 wdev->connected = false; 1262 } 1263 1264 void cfg80211_unregister_wdev(struct wireless_dev *wdev) 1265 { 1266 _cfg80211_unregister_wdev(wdev, true); 1267 } 1268 EXPORT_SYMBOL(cfg80211_unregister_wdev); 1269 1270 static const struct device_type wiphy_type = { 1271 .name = "wlan", 1272 }; 1273 1274 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev, 1275 enum nl80211_iftype iftype, int num) 1276 { 1277 lockdep_assert_held(&rdev->wiphy.mtx); 1278 1279 rdev->num_running_ifaces += num; 1280 if (iftype == NL80211_IFTYPE_MONITOR) 1281 rdev->num_running_monitor_ifaces += num; 1282 } 1283 1284 void cfg80211_leave(struct cfg80211_registered_device *rdev, 1285 struct wireless_dev *wdev) 1286 { 1287 struct net_device *dev = wdev->netdev; 1288 struct cfg80211_sched_scan_request *pos, *tmp; 1289 1290 lockdep_assert_held(&rdev->wiphy.mtx); 1291 1292 cfg80211_pmsr_wdev_down(wdev); 1293 1294 cfg80211_stop_background_radar_detection(wdev); 1295 1296 switch (wdev->iftype) { 1297 case NL80211_IFTYPE_ADHOC: 1298 cfg80211_leave_ibss(rdev, dev, true); 1299 break; 1300 case NL80211_IFTYPE_P2P_CLIENT: 1301 case NL80211_IFTYPE_STATION: 1302 list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list, 1303 list) { 1304 if (dev == pos->dev) 1305 cfg80211_stop_sched_scan_req(rdev, pos, false); 1306 } 1307 1308 #ifdef CONFIG_CFG80211_WEXT 1309 kfree(wdev->wext.ie); 1310 wdev->wext.ie = NULL; 1311 wdev->wext.ie_len = 0; 1312 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC; 1313 #endif 1314 cfg80211_disconnect(rdev, dev, 1315 WLAN_REASON_DEAUTH_LEAVING, true); 1316 break; 1317 case NL80211_IFTYPE_MESH_POINT: 1318 cfg80211_leave_mesh(rdev, dev); 1319 break; 1320 case NL80211_IFTYPE_AP: 1321 case NL80211_IFTYPE_P2P_GO: 1322 cfg80211_stop_ap(rdev, dev, -1, true); 1323 break; 1324 case NL80211_IFTYPE_OCB: 1325 cfg80211_leave_ocb(rdev, dev); 1326 break; 1327 case NL80211_IFTYPE_P2P_DEVICE: 1328 case NL80211_IFTYPE_NAN: 1329 /* cannot happen, has no netdev */ 1330 break; 1331 case NL80211_IFTYPE_AP_VLAN: 1332 case NL80211_IFTYPE_MONITOR: 1333 /* nothing to do */ 1334 break; 1335 case NL80211_IFTYPE_UNSPECIFIED: 1336 case NL80211_IFTYPE_WDS: 1337 case NUM_NL80211_IFTYPES: 1338 /* invalid */ 1339 break; 1340 } 1341 } 1342 1343 void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev, 1344 gfp_t gfp) 1345 { 1346 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1347 struct cfg80211_event *ev; 1348 unsigned long flags; 1349 1350 trace_cfg80211_stop_iface(wiphy, wdev); 1351 1352 ev = kzalloc(sizeof(*ev), gfp); 1353 if (!ev) 1354 return; 1355 1356 ev->type = EVENT_STOPPED; 1357 1358 spin_lock_irqsave(&wdev->event_lock, flags); 1359 list_add_tail(&ev->list, &wdev->event_list); 1360 spin_unlock_irqrestore(&wdev->event_lock, flags); 1361 queue_work(cfg80211_wq, &rdev->event_work); 1362 } 1363 EXPORT_SYMBOL(cfg80211_stop_iface); 1364 1365 void cfg80211_init_wdev(struct wireless_dev *wdev) 1366 { 1367 INIT_LIST_HEAD(&wdev->event_list); 1368 spin_lock_init(&wdev->event_lock); 1369 INIT_LIST_HEAD(&wdev->mgmt_registrations); 1370 INIT_LIST_HEAD(&wdev->pmsr_list); 1371 spin_lock_init(&wdev->pmsr_lock); 1372 INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk); 1373 1374 #ifdef CONFIG_CFG80211_WEXT 1375 wdev->wext.default_key = -1; 1376 wdev->wext.default_mgmt_key = -1; 1377 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC; 1378 #endif 1379 1380 wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work); 1381 1382 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT) 1383 wdev->ps = true; 1384 else 1385 wdev->ps = false; 1386 /* allow mac80211 to determine the timeout */ 1387 wdev->ps_timeout = -1; 1388 1389 if ((wdev->iftype == NL80211_IFTYPE_STATION || 1390 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT || 1391 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr) 1392 wdev->netdev->priv_flags |= IFF_DONT_BRIDGE; 1393 1394 INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk); 1395 } 1396 1397 void cfg80211_register_wdev(struct cfg80211_registered_device *rdev, 1398 struct wireless_dev *wdev) 1399 { 1400 ASSERT_RTNL(); 1401 lockdep_assert_held(&rdev->wiphy.mtx); 1402 1403 /* 1404 * We get here also when the interface changes network namespaces, 1405 * as it's registered into the new one, but we don't want it to 1406 * change ID in that case. Checking if the ID is already assigned 1407 * works, because 0 isn't considered a valid ID and the memory is 1408 * 0-initialized. 1409 */ 1410 if (!wdev->identifier) 1411 wdev->identifier = ++rdev->wdev_id; 1412 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list); 1413 rdev->devlist_generation++; 1414 wdev->registered = true; 1415 1416 if (wdev->netdev && 1417 sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj, 1418 "phy80211")) 1419 pr_err("failed to add phy80211 symlink to netdev!\n"); 1420 1421 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE); 1422 } 1423 1424 int cfg80211_register_netdevice(struct net_device *dev) 1425 { 1426 struct wireless_dev *wdev = dev->ieee80211_ptr; 1427 struct cfg80211_registered_device *rdev; 1428 int ret; 1429 1430 ASSERT_RTNL(); 1431 1432 if (WARN_ON(!wdev)) 1433 return -EINVAL; 1434 1435 rdev = wiphy_to_rdev(wdev->wiphy); 1436 1437 lockdep_assert_held(&rdev->wiphy.mtx); 1438 1439 /* we'll take care of this */ 1440 wdev->registered = true; 1441 wdev->registering = true; 1442 ret = register_netdevice(dev); 1443 if (ret) 1444 goto out; 1445 1446 cfg80211_register_wdev(rdev, wdev); 1447 ret = 0; 1448 out: 1449 wdev->registering = false; 1450 if (ret) 1451 wdev->registered = false; 1452 return ret; 1453 } 1454 EXPORT_SYMBOL(cfg80211_register_netdevice); 1455 1456 static int cfg80211_netdev_notifier_call(struct notifier_block *nb, 1457 unsigned long state, void *ptr) 1458 { 1459 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1460 struct wireless_dev *wdev = dev->ieee80211_ptr; 1461 struct cfg80211_registered_device *rdev; 1462 struct cfg80211_sched_scan_request *pos, *tmp; 1463 1464 if (!wdev) 1465 return NOTIFY_DONE; 1466 1467 rdev = wiphy_to_rdev(wdev->wiphy); 1468 1469 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED); 1470 1471 switch (state) { 1472 case NETDEV_POST_INIT: 1473 SET_NETDEV_DEVTYPE(dev, &wiphy_type); 1474 wdev->netdev = dev; 1475 /* can only change netns with wiphy */ 1476 dev->netns_local = true; 1477 1478 cfg80211_init_wdev(wdev); 1479 break; 1480 case NETDEV_REGISTER: 1481 if (!wdev->registered) { 1482 wiphy_lock(&rdev->wiphy); 1483 cfg80211_register_wdev(rdev, wdev); 1484 wiphy_unlock(&rdev->wiphy); 1485 } 1486 break; 1487 case NETDEV_UNREGISTER: 1488 /* 1489 * It is possible to get NETDEV_UNREGISTER multiple times, 1490 * so check wdev->registered. 1491 */ 1492 if (wdev->registered && !wdev->registering) { 1493 wiphy_lock(&rdev->wiphy); 1494 _cfg80211_unregister_wdev(wdev, false); 1495 wiphy_unlock(&rdev->wiphy); 1496 } 1497 break; 1498 case NETDEV_GOING_DOWN: 1499 wiphy_lock(&rdev->wiphy); 1500 cfg80211_leave(rdev, wdev); 1501 cfg80211_remove_links(wdev); 1502 wiphy_unlock(&rdev->wiphy); 1503 /* since we just did cfg80211_leave() nothing to do there */ 1504 cancel_work_sync(&wdev->disconnect_wk); 1505 cancel_work_sync(&wdev->pmsr_free_wk); 1506 break; 1507 case NETDEV_DOWN: 1508 wiphy_lock(&rdev->wiphy); 1509 cfg80211_update_iface_num(rdev, wdev->iftype, -1); 1510 if (rdev->scan_req && rdev->scan_req->wdev == wdev) { 1511 if (WARN_ON(!rdev->scan_req->notified && 1512 (!rdev->int_scan_req || 1513 !rdev->int_scan_req->notified))) 1514 rdev->scan_req->info.aborted = true; 1515 ___cfg80211_scan_done(rdev, false); 1516 } 1517 1518 list_for_each_entry_safe(pos, tmp, 1519 &rdev->sched_scan_req_list, list) { 1520 if (WARN_ON(pos->dev == wdev->netdev)) 1521 cfg80211_stop_sched_scan_req(rdev, pos, false); 1522 } 1523 1524 rdev->opencount--; 1525 wiphy_unlock(&rdev->wiphy); 1526 wake_up(&rdev->dev_wait); 1527 break; 1528 case NETDEV_UP: 1529 wiphy_lock(&rdev->wiphy); 1530 cfg80211_update_iface_num(rdev, wdev->iftype, 1); 1531 switch (wdev->iftype) { 1532 #ifdef CONFIG_CFG80211_WEXT 1533 case NL80211_IFTYPE_ADHOC: 1534 cfg80211_ibss_wext_join(rdev, wdev); 1535 break; 1536 case NL80211_IFTYPE_STATION: 1537 cfg80211_mgd_wext_connect(rdev, wdev); 1538 break; 1539 #endif 1540 #ifdef CONFIG_MAC80211_MESH 1541 case NL80211_IFTYPE_MESH_POINT: 1542 { 1543 /* backward compat code... */ 1544 struct mesh_setup setup; 1545 memcpy(&setup, &default_mesh_setup, 1546 sizeof(setup)); 1547 /* back compat only needed for mesh_id */ 1548 setup.mesh_id = wdev->u.mesh.id; 1549 setup.mesh_id_len = wdev->u.mesh.id_up_len; 1550 if (wdev->u.mesh.id_up_len) 1551 __cfg80211_join_mesh(rdev, dev, 1552 &setup, 1553 &default_mesh_config); 1554 break; 1555 } 1556 #endif 1557 default: 1558 break; 1559 } 1560 rdev->opencount++; 1561 1562 /* 1563 * Configure power management to the driver here so that its 1564 * correctly set also after interface type changes etc. 1565 */ 1566 if ((wdev->iftype == NL80211_IFTYPE_STATION || 1567 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) && 1568 rdev->ops->set_power_mgmt && 1569 rdev_set_power_mgmt(rdev, dev, wdev->ps, 1570 wdev->ps_timeout)) { 1571 /* assume this means it's off */ 1572 wdev->ps = false; 1573 } 1574 wiphy_unlock(&rdev->wiphy); 1575 break; 1576 case NETDEV_PRE_UP: 1577 if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype, 1578 wdev->use_4addr, 0)) 1579 return notifier_from_errno(-EOPNOTSUPP); 1580 1581 if (rfkill_blocked(rdev->wiphy.rfkill)) 1582 return notifier_from_errno(-ERFKILL); 1583 break; 1584 default: 1585 return NOTIFY_DONE; 1586 } 1587 1588 wireless_nlevent_flush(); 1589 1590 return NOTIFY_OK; 1591 } 1592 1593 static struct notifier_block cfg80211_netdev_notifier = { 1594 .notifier_call = cfg80211_netdev_notifier_call, 1595 }; 1596 1597 static void __net_exit cfg80211_pernet_exit(struct net *net) 1598 { 1599 struct cfg80211_registered_device *rdev; 1600 1601 rtnl_lock(); 1602 for_each_rdev(rdev) { 1603 if (net_eq(wiphy_net(&rdev->wiphy), net)) 1604 WARN_ON(cfg80211_switch_netns(rdev, &init_net)); 1605 } 1606 rtnl_unlock(); 1607 } 1608 1609 static struct pernet_operations cfg80211_pernet_ops = { 1610 .exit = cfg80211_pernet_exit, 1611 }; 1612 1613 void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work) 1614 { 1615 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1616 unsigned long flags; 1617 1618 trace_wiphy_work_queue(wiphy, work); 1619 1620 spin_lock_irqsave(&rdev->wiphy_work_lock, flags); 1621 if (list_empty(&work->entry)) 1622 list_add_tail(&work->entry, &rdev->wiphy_work_list); 1623 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags); 1624 1625 queue_work(system_unbound_wq, &rdev->wiphy_work); 1626 } 1627 EXPORT_SYMBOL_GPL(wiphy_work_queue); 1628 1629 void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work) 1630 { 1631 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1632 unsigned long flags; 1633 1634 lockdep_assert_held(&wiphy->mtx); 1635 1636 trace_wiphy_work_cancel(wiphy, work); 1637 1638 spin_lock_irqsave(&rdev->wiphy_work_lock, flags); 1639 if (!list_empty(&work->entry)) 1640 list_del_init(&work->entry); 1641 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags); 1642 } 1643 EXPORT_SYMBOL_GPL(wiphy_work_cancel); 1644 1645 void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work) 1646 { 1647 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1648 unsigned long flags; 1649 bool run; 1650 1651 trace_wiphy_work_flush(wiphy, work); 1652 1653 spin_lock_irqsave(&rdev->wiphy_work_lock, flags); 1654 run = !work || !list_empty(&work->entry); 1655 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags); 1656 1657 if (run) 1658 cfg80211_process_wiphy_works(rdev, work); 1659 } 1660 EXPORT_SYMBOL_GPL(wiphy_work_flush); 1661 1662 void wiphy_delayed_work_timer(struct timer_list *t) 1663 { 1664 struct wiphy_delayed_work *dwork = from_timer(dwork, t, timer); 1665 1666 wiphy_work_queue(dwork->wiphy, &dwork->work); 1667 } 1668 EXPORT_SYMBOL(wiphy_delayed_work_timer); 1669 1670 void wiphy_delayed_work_queue(struct wiphy *wiphy, 1671 struct wiphy_delayed_work *dwork, 1672 unsigned long delay) 1673 { 1674 trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay); 1675 1676 if (!delay) { 1677 del_timer(&dwork->timer); 1678 wiphy_work_queue(wiphy, &dwork->work); 1679 return; 1680 } 1681 1682 dwork->wiphy = wiphy; 1683 mod_timer(&dwork->timer, jiffies + delay); 1684 } 1685 EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue); 1686 1687 void wiphy_delayed_work_cancel(struct wiphy *wiphy, 1688 struct wiphy_delayed_work *dwork) 1689 { 1690 lockdep_assert_held(&wiphy->mtx); 1691 1692 del_timer_sync(&dwork->timer); 1693 wiphy_work_cancel(wiphy, &dwork->work); 1694 } 1695 EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel); 1696 1697 void wiphy_delayed_work_flush(struct wiphy *wiphy, 1698 struct wiphy_delayed_work *dwork) 1699 { 1700 lockdep_assert_held(&wiphy->mtx); 1701 1702 del_timer_sync(&dwork->timer); 1703 wiphy_work_flush(wiphy, &dwork->work); 1704 } 1705 EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush); 1706 1707 static int __init cfg80211_init(void) 1708 { 1709 int err; 1710 1711 err = register_pernet_device(&cfg80211_pernet_ops); 1712 if (err) 1713 goto out_fail_pernet; 1714 1715 err = wiphy_sysfs_init(); 1716 if (err) 1717 goto out_fail_sysfs; 1718 1719 err = register_netdevice_notifier(&cfg80211_netdev_notifier); 1720 if (err) 1721 goto out_fail_notifier; 1722 1723 err = nl80211_init(); 1724 if (err) 1725 goto out_fail_nl80211; 1726 1727 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL); 1728 1729 err = regulatory_init(); 1730 if (err) 1731 goto out_fail_reg; 1732 1733 cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM); 1734 if (!cfg80211_wq) { 1735 err = -ENOMEM; 1736 goto out_fail_wq; 1737 } 1738 1739 return 0; 1740 1741 out_fail_wq: 1742 regulatory_exit(); 1743 out_fail_reg: 1744 debugfs_remove(ieee80211_debugfs_dir); 1745 nl80211_exit(); 1746 out_fail_nl80211: 1747 unregister_netdevice_notifier(&cfg80211_netdev_notifier); 1748 out_fail_notifier: 1749 wiphy_sysfs_exit(); 1750 out_fail_sysfs: 1751 unregister_pernet_device(&cfg80211_pernet_ops); 1752 out_fail_pernet: 1753 return err; 1754 } 1755 fs_initcall(cfg80211_init); 1756 1757 static void __exit cfg80211_exit(void) 1758 { 1759 debugfs_remove(ieee80211_debugfs_dir); 1760 nl80211_exit(); 1761 unregister_netdevice_notifier(&cfg80211_netdev_notifier); 1762 wiphy_sysfs_exit(); 1763 regulatory_exit(); 1764 unregister_pernet_device(&cfg80211_pernet_ops); 1765 destroy_workqueue(cfg80211_wq); 1766 } 1767 module_exit(cfg80211_exit); 1768