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