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