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