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