2 * This is the linux wireless configuration interface.
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * Copyright 2015-2017 Intel Deutschland GmbH
7 * Copyright (C) 2018-2021 Intel Corporation
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/err.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
17 #include <linux/nl80211.h>
18 #include <linux/debugfs.h>
19 #include <linux/notifier.h>
20 #include <linux/device.h>
21 #include <linux/etherdevice.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/sched.h>
24 #include <net/genetlink.h>
25 #include <net/cfg80211.h>
30 #include "wext-compat.h"
33 /* name for sysfs, %d is appended */
34 #define PHY_NAME "phy"
36 MODULE_AUTHOR("Johannes Berg");
37 MODULE_LICENSE("GPL");
38 MODULE_DESCRIPTION("wireless configuration support");
39 MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
41 /* RCU-protected (and RTNL for writers) */
42 LIST_HEAD(cfg80211_rdev_list);
43 int cfg80211_rdev_list_generation;
46 static struct dentry *ieee80211_debugfs_dir;
48 /* for the cleanup, scan and event works */
49 struct workqueue_struct *cfg80211_wq;
51 static bool cfg80211_disable_40mhz_24ghz;
52 module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
53 MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
54 "Disable 40MHz support in the 2.4GHz band");
56 struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
58 struct cfg80211_registered_device *result = NULL, *rdev;
62 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
63 if (rdev->wiphy_idx == wiphy_idx) {
72 int get_wiphy_idx(struct wiphy *wiphy)
74 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
76 return rdev->wiphy_idx;
79 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
81 struct cfg80211_registered_device *rdev;
85 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
91 static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
94 struct cfg80211_registered_device *rdev2;
95 int wiphy_idx, taken = -1, digits;
99 if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN)
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 */
107 while (wiphy_idx /= 10)
110 * deny the name if it is phy<idx> where <idx> is printed
111 * without leading zeroes. taken == strlen(newname) here
113 if (taken == strlen(PHY_NAME) + digits)
117 /* Ensure another device does not already have this name. */
118 list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
119 if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
125 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
132 /* Ignore nop renames */
133 if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
136 result = cfg80211_dev_check_name(rdev, newname);
140 result = device_rename(&rdev->wiphy.dev, newname);
144 if (rdev->wiphy.debugfsdir &&
145 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
146 rdev->wiphy.debugfsdir,
147 rdev->wiphy.debugfsdir->d_parent,
149 pr_err("failed to rename debugfs dir to %s!\n", newname);
151 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
156 int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
159 struct wireless_dev *wdev;
162 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
165 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
168 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
169 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
172 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
176 /* failed -- clean up to old netns */
177 net = wiphy_net(&rdev->wiphy);
179 list_for_each_entry_continue_reverse(wdev,
180 &rdev->wiphy.wdev_list,
184 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
185 err = dev_change_net_namespace(wdev->netdev, net,
188 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
194 wiphy_net_set(&rdev->wiphy, net);
196 err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
202 static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
204 struct cfg80211_registered_device *rdev = data;
206 rdev_rfkill_poll(rdev);
209 void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
210 struct wireless_dev *wdev)
214 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
217 if (!wdev_running(wdev))
220 rdev_stop_p2p_device(rdev, wdev);
221 wdev->is_running = false;
225 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
226 if (WARN_ON(!rdev->scan_req->notified))
227 rdev->scan_req->info.aborted = true;
228 ___cfg80211_scan_done(rdev, false);
232 void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
233 struct wireless_dev *wdev)
237 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
240 if (!wdev_running(wdev))
243 rdev_stop_nan(rdev, wdev);
244 wdev->is_running = false;
249 void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
251 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
252 struct wireless_dev *wdev;
256 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
258 dev_close(wdev->netdev);
261 /* otherwise, check iftype */
262 switch (wdev->iftype) {
263 case NL80211_IFTYPE_P2P_DEVICE:
264 cfg80211_stop_p2p_device(rdev, wdev);
266 case NL80211_IFTYPE_NAN:
267 cfg80211_stop_nan(rdev, wdev);
274 EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
276 static int cfg80211_rfkill_set_block(void *data, bool blocked)
278 struct cfg80211_registered_device *rdev = data;
284 cfg80211_shutdown_all_interfaces(&rdev->wiphy);
290 static void cfg80211_rfkill_sync_work(struct work_struct *work)
292 struct cfg80211_registered_device *rdev;
294 rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
295 cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
298 static void cfg80211_event_work(struct work_struct *work)
300 struct cfg80211_registered_device *rdev;
302 rdev = container_of(work, struct cfg80211_registered_device,
306 cfg80211_process_rdev_events(rdev);
310 void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
312 struct wireless_dev *wdev, *tmp;
316 list_for_each_entry_safe(wdev, tmp, &rdev->wiphy.wdev_list, list) {
317 if (wdev->nl_owner_dead)
318 rdev_del_virtual_intf(rdev, wdev);
322 static void cfg80211_destroy_iface_wk(struct work_struct *work)
324 struct cfg80211_registered_device *rdev;
326 rdev = container_of(work, struct cfg80211_registered_device,
330 cfg80211_destroy_ifaces(rdev);
334 static void cfg80211_sched_scan_stop_wk(struct work_struct *work)
336 struct cfg80211_registered_device *rdev;
337 struct cfg80211_sched_scan_request *req, *tmp;
339 rdev = container_of(work, struct cfg80211_registered_device,
343 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
344 if (req->nl_owner_dead)
345 cfg80211_stop_sched_scan_req(rdev, req, false);
350 static void cfg80211_propagate_radar_detect_wk(struct work_struct *work)
352 struct cfg80211_registered_device *rdev;
354 rdev = container_of(work, struct cfg80211_registered_device,
355 propagate_radar_detect_wk);
359 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->radar_chandef,
360 NL80211_DFS_UNAVAILABLE,
361 NL80211_RADAR_DETECTED);
366 static void cfg80211_propagate_cac_done_wk(struct work_struct *work)
368 struct cfg80211_registered_device *rdev;
370 rdev = container_of(work, struct cfg80211_registered_device,
371 propagate_cac_done_wk);
375 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->cac_done_chandef,
376 NL80211_DFS_AVAILABLE,
377 NL80211_RADAR_CAC_FINISHED);
382 /* exported functions */
384 struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
385 const char *requested_name)
387 static atomic_t wiphy_counter = ATOMIC_INIT(0);
389 struct cfg80211_registered_device *rdev;
392 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
393 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
394 WARN_ON(ops->connect && !ops->disconnect);
395 WARN_ON(ops->join_ibss && !ops->leave_ibss);
396 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
397 WARN_ON(ops->add_station && !ops->del_station);
398 WARN_ON(ops->add_mpath && !ops->del_mpath);
399 WARN_ON(ops->join_mesh && !ops->leave_mesh);
400 WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device);
401 WARN_ON(ops->start_ap && !ops->stop_ap);
402 WARN_ON(ops->join_ocb && !ops->leave_ocb);
403 WARN_ON(ops->suspend && !ops->resume);
404 WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop);
405 WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel);
406 WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch);
407 WARN_ON(ops->add_tx_ts && !ops->del_tx_ts);
409 alloc_size = sizeof(*rdev) + sizeof_priv;
411 rdev = kzalloc(alloc_size, GFP_KERNEL);
417 rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
419 if (unlikely(rdev->wiphy_idx < 0)) {
421 atomic_dec(&wiphy_counter);
426 /* atomic_inc_return makes it start at 1, make it start at 0 */
429 /* give it a proper name */
430 if (requested_name && requested_name[0]) {
434 rv = cfg80211_dev_check_name(rdev, requested_name);
438 goto use_default_name;
441 rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name);
444 goto use_default_name;
449 /* NOTE: This is *probably* safe w/out holding rtnl because of
450 * the restrictions on phy names. Probably this call could
451 * fail if some other part of the kernel (re)named a device
452 * phyX. But, might should add some locking and check return
453 * value, and use a different name if this one exists?
455 rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
462 INIT_LIST_HEAD(&rdev->wiphy.wdev_list);
463 INIT_LIST_HEAD(&rdev->beacon_registrations);
464 spin_lock_init(&rdev->beacon_registrations_lock);
465 spin_lock_init(&rdev->bss_lock);
466 INIT_LIST_HEAD(&rdev->bss_list);
467 INIT_LIST_HEAD(&rdev->sched_scan_req_list);
468 INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
469 INIT_LIST_HEAD(&rdev->mlme_unreg);
470 spin_lock_init(&rdev->mlme_unreg_lock);
471 INIT_WORK(&rdev->mlme_unreg_wk, cfg80211_mlme_unreg_wk);
472 INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
473 cfg80211_dfs_channels_update_work);
474 #ifdef CONFIG_CFG80211_WEXT
475 rdev->wiphy.wext = &cfg80211_wext_handler;
478 device_initialize(&rdev->wiphy.dev);
479 rdev->wiphy.dev.class = &ieee80211_class;
480 rdev->wiphy.dev.platform_data = rdev;
481 device_enable_async_suspend(&rdev->wiphy.dev);
483 INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
484 INIT_WORK(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);
485 INIT_WORK(&rdev->sched_scan_res_wk, cfg80211_sched_scan_results_wk);
486 INIT_WORK(&rdev->propagate_radar_detect_wk,
487 cfg80211_propagate_radar_detect_wk);
488 INIT_WORK(&rdev->propagate_cac_done_wk, cfg80211_propagate_cac_done_wk);
490 #ifdef CONFIG_CFG80211_DEFAULT_PS
491 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
494 wiphy_net_set(&rdev->wiphy, &init_net);
496 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
497 rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
498 &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
499 &rdev->rfkill_ops, rdev);
502 wiphy_free(&rdev->wiphy);
506 INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
507 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
508 INIT_WORK(&rdev->event_work, cfg80211_event_work);
510 init_waitqueue_head(&rdev->dev_wait);
513 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
514 * Fragmentation and RTS threshold are disabled by default with the
517 rdev->wiphy.retry_short = 7;
518 rdev->wiphy.retry_long = 4;
519 rdev->wiphy.frag_threshold = (u32) -1;
520 rdev->wiphy.rts_threshold = (u32) -1;
521 rdev->wiphy.coverage_class = 0;
523 rdev->wiphy.max_num_csa_counters = 1;
525 rdev->wiphy.max_sched_scan_plans = 1;
526 rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
530 EXPORT_SYMBOL(wiphy_new_nm);
532 static int wiphy_verify_combinations(struct wiphy *wiphy)
534 const struct ieee80211_iface_combination *c;
537 for (i = 0; i < wiphy->n_iface_combinations; i++) {
541 c = &wiphy->iface_combinations[i];
544 * Combinations with just one interface aren't real,
545 * however we make an exception for DFS.
547 if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
550 /* Need at least one channel */
551 if (WARN_ON(!c->num_different_channels))
555 * Put a sane limit on maximum number of different
556 * channels to simplify channel accounting code.
558 if (WARN_ON(c->num_different_channels >
559 CFG80211_MAX_NUM_DIFFERENT_CHANNELS))
562 /* DFS only works on one channel. */
563 if (WARN_ON(c->radar_detect_widths &&
564 (c->num_different_channels > 1)))
567 if (WARN_ON(!c->n_limits))
570 for (j = 0; j < c->n_limits; j++) {
571 u16 types = c->limits[j].types;
573 /* interface types shouldn't overlap */
574 if (WARN_ON(types & all_iftypes))
576 all_iftypes |= types;
578 if (WARN_ON(!c->limits[j].max))
581 /* Shouldn't list software iftypes in combinations! */
582 if (WARN_ON(wiphy->software_iftypes & types))
585 /* Only a single P2P_DEVICE can be allowed */
586 if (WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
587 c->limits[j].max > 1))
590 /* Only a single NAN can be allowed */
591 if (WARN_ON(types & BIT(NL80211_IFTYPE_NAN) &&
592 c->limits[j].max > 1))
596 * This isn't well-defined right now. If you have an
597 * IBSS interface, then its beacon interval may change
598 * by joining other networks, and nothing prevents it
600 * So technically we probably shouldn't even allow AP
601 * and IBSS in the same interface, but it seems that
602 * some drivers support that, possibly only with fixed
603 * beacon intervals for IBSS.
605 if (WARN_ON(types & BIT(NL80211_IFTYPE_ADHOC) &&
606 c->beacon_int_min_gcd)) {
610 cnt += c->limits[j].max;
612 * Don't advertise an unsupported type
615 if (WARN_ON((wiphy->interface_modes & types) != types))
619 #ifndef CONFIG_WIRELESS_WDS
620 if (WARN_ON(all_iftypes & BIT(NL80211_IFTYPE_WDS)))
624 /* You can't even choose that many! */
625 if (WARN_ON(cnt < c->max_interfaces))
632 int wiphy_register(struct wiphy *wiphy)
634 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
636 enum nl80211_band band;
637 struct ieee80211_supported_band *sband;
638 bool have_band = false;
640 u16 ifmodes = wiphy->interface_modes;
643 if (WARN_ON(wiphy->wowlan &&
644 (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
645 !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
647 if (WARN_ON(wiphy->wowlan &&
648 !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns &&
649 !wiphy->wowlan->tcp))
652 if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
653 (!rdev->ops->tdls_channel_switch ||
654 !rdev->ops->tdls_cancel_channel_switch)))
657 if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
658 (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
659 !rdev->ops->add_nan_func || !rdev->ops->del_nan_func ||
660 !(wiphy->nan_supported_bands & BIT(NL80211_BAND_2GHZ)))))
663 #ifndef CONFIG_WIRELESS_WDS
664 if (WARN_ON(wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS)))
669 * if a wiphy has unsupported modes for regulatory channel enforcement,
670 * opt-out of enforcement checking
672 if (wiphy->interface_modes & ~(BIT(NL80211_IFTYPE_STATION) |
673 BIT(NL80211_IFTYPE_P2P_CLIENT) |
674 BIT(NL80211_IFTYPE_AP) |
675 BIT(NL80211_IFTYPE_P2P_GO) |
676 BIT(NL80211_IFTYPE_ADHOC) |
677 BIT(NL80211_IFTYPE_P2P_DEVICE) |
678 BIT(NL80211_IFTYPE_NAN) |
679 BIT(NL80211_IFTYPE_AP_VLAN) |
680 BIT(NL80211_IFTYPE_MONITOR)))
681 wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF;
683 if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
684 (wiphy->regulatory_flags &
685 (REGULATORY_CUSTOM_REG |
686 REGULATORY_STRICT_REG |
687 REGULATORY_COUNTRY_IE_FOLLOW_POWER |
688 REGULATORY_COUNTRY_IE_IGNORE))))
691 if (WARN_ON(wiphy->coalesce &&
692 (!wiphy->coalesce->n_rules ||
693 !wiphy->coalesce->n_patterns) &&
694 (!wiphy->coalesce->pattern_min_len ||
695 wiphy->coalesce->pattern_min_len >
696 wiphy->coalesce->pattern_max_len)))
699 if (WARN_ON(wiphy->ap_sme_capa &&
700 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
703 if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
706 if (WARN_ON(wiphy->addresses &&
707 !is_zero_ether_addr(wiphy->perm_addr) &&
708 memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
712 if (WARN_ON(wiphy->max_acl_mac_addrs &&
713 (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
714 !rdev->ops->set_mac_acl)))
717 /* assure only valid behaviours are flagged by driver
718 * hence subtract 2 as bit 0 is invalid.
720 if (WARN_ON(wiphy->bss_select_support &&
721 (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
724 if (WARN_ON(wiphy_ext_feature_isset(&rdev->wiphy,
725 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) &&
726 (!rdev->ops->set_pmk || !rdev->ops->del_pmk)))
729 if (WARN_ON(!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
730 rdev->ops->update_connect_params))
733 if (wiphy->addresses)
734 memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
736 /* sanity check ifmodes */
738 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
739 if (WARN_ON(ifmodes != wiphy->interface_modes))
740 wiphy->interface_modes = ifmodes;
742 res = wiphy_verify_combinations(wiphy);
746 /* sanity check supported bands/channels */
747 for (band = 0; band < NUM_NL80211_BANDS; band++) {
750 sband = wiphy->bands[band];
755 if (WARN_ON(!sband->n_channels))
758 * on 60GHz band, there are no legacy rates, so
761 if (WARN_ON(band != NL80211_BAND_60GHZ &&
766 * Since cfg80211_disable_40mhz_24ghz is global, we can
767 * modify the sband's ht data even if the driver uses a
768 * global structure for that.
770 if (cfg80211_disable_40mhz_24ghz &&
771 band == NL80211_BAND_2GHZ &&
772 sband->ht_cap.ht_supported) {
773 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
774 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
778 * Since we use a u32 for rate bitmaps in
779 * ieee80211_get_response_rate, we cannot
780 * have more than 32 legacy rates.
782 if (WARN_ON(sband->n_bitrates > 32))
785 for (i = 0; i < sband->n_channels; i++) {
786 sband->channels[i].orig_flags =
787 sband->channels[i].flags;
788 sband->channels[i].orig_mag = INT_MAX;
789 sband->channels[i].orig_mpwr =
790 sband->channels[i].max_power;
791 sband->channels[i].band = band;
794 for (i = 0; i < sband->n_iftype_data; i++) {
795 const struct ieee80211_sband_iftype_data *iftd;
797 iftd = &sband->iftype_data[i];
799 if (WARN_ON(!iftd->types_mask))
801 if (WARN_ON(types & iftd->types_mask))
804 /* at least one piece of information must be present */
805 if (WARN_ON(!iftd->he_cap.has_he))
808 types |= iftd->types_mask;
820 if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
821 (!rdev->wiphy.wowlan->pattern_min_len ||
822 rdev->wiphy.wowlan->pattern_min_len >
823 rdev->wiphy.wowlan->pattern_max_len)))
827 /* check and set up bitrates */
828 ieee80211_set_bitrate_flags(wiphy);
830 rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
833 res = device_add(&rdev->wiphy.dev);
839 list_add_rcu(&rdev->list, &cfg80211_rdev_list);
840 cfg80211_rdev_list_generation++;
843 rdev->wiphy.debugfsdir =
844 debugfs_create_dir(wiphy_name(&rdev->wiphy),
845 ieee80211_debugfs_dir);
846 if (IS_ERR(rdev->wiphy.debugfsdir))
847 rdev->wiphy.debugfsdir = NULL;
849 cfg80211_debugfs_rdev_add(rdev);
850 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
852 /* set up regulatory info */
853 wiphy_regulatory_register(wiphy);
855 if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
856 struct regulatory_request request;
858 request.wiphy_idx = get_wiphy_idx(wiphy);
859 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
860 request.alpha2[0] = '9';
861 request.alpha2[1] = '9';
863 nl80211_send_reg_change_event(&request);
866 /* Check that nobody globally advertises any capabilities they do not
867 * advertise on all possible interface types.
869 if (wiphy->extended_capabilities_len &&
870 wiphy->num_iftype_ext_capab &&
871 wiphy->iftype_ext_capab) {
872 u8 supported_on_all, j;
873 const struct wiphy_iftype_ext_capab *capab;
875 capab = wiphy->iftype_ext_capab;
876 for (j = 0; j < wiphy->extended_capabilities_len; j++) {
877 if (capab[0].extended_capabilities_len > j)
879 capab[0].extended_capabilities[j];
881 supported_on_all = 0x00;
882 for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
883 if (j >= capab[i].extended_capabilities_len) {
884 supported_on_all = 0x00;
888 capab[i].extended_capabilities[j];
890 if (WARN_ON(wiphy->extended_capabilities[j] &
896 rdev->wiphy.registered = true;
899 res = rfkill_register(rdev->rfkill);
901 rfkill_destroy(rdev->rfkill);
903 wiphy_unregister(&rdev->wiphy);
909 EXPORT_SYMBOL(wiphy_register);
911 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
913 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
915 if (!rdev->ops->rfkill_poll)
917 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
918 rfkill_resume_polling(rdev->rfkill);
920 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
922 void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
924 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
926 rfkill_pause_polling(rdev->rfkill);
928 EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
930 void wiphy_unregister(struct wiphy *wiphy)
932 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
934 wait_event(rdev->dev_wait, ({
937 __count = rdev->opencount;
942 rfkill_unregister(rdev->rfkill);
945 nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
946 rdev->wiphy.registered = false;
948 WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
951 * First remove the hardware from everywhere, this makes
952 * it impossible to find from userspace.
954 debugfs_remove_recursive(rdev->wiphy.debugfsdir);
955 list_del_rcu(&rdev->list);
959 * If this device got a regulatory hint tell core its
960 * free to listen now to a new shiny device regulatory hint
962 wiphy_regulatory_deregister(wiphy);
964 cfg80211_rdev_list_generation++;
965 device_del(&rdev->wiphy.dev);
969 flush_work(&rdev->scan_done_wk);
970 cancel_work_sync(&rdev->conn_work);
971 flush_work(&rdev->event_work);
972 cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
973 flush_work(&rdev->destroy_work);
974 flush_work(&rdev->sched_scan_stop_wk);
975 flush_work(&rdev->mlme_unreg_wk);
976 flush_work(&rdev->propagate_radar_detect_wk);
977 flush_work(&rdev->propagate_cac_done_wk);
980 if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
981 rdev_set_wakeup(rdev, false);
983 cfg80211_rdev_free_wowlan(rdev);
984 cfg80211_rdev_free_coalesce(rdev);
986 EXPORT_SYMBOL(wiphy_unregister);
988 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
990 struct cfg80211_internal_bss *scan, *tmp;
991 struct cfg80211_beacon_registration *reg, *treg;
992 rfkill_destroy(rdev->rfkill);
993 list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
994 list_del(®->list);
997 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
998 cfg80211_put_bss(&rdev->wiphy, &scan->pub);
1002 void wiphy_free(struct wiphy *wiphy)
1004 put_device(&wiphy->dev);
1006 EXPORT_SYMBOL(wiphy_free);
1008 void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
1010 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1012 if (rfkill_set_hw_state(rdev->rfkill, blocked))
1013 schedule_work(&rdev->rfkill_sync);
1015 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
1017 void cfg80211_cqm_config_free(struct wireless_dev *wdev)
1019 kfree(wdev->cqm_config);
1020 wdev->cqm_config = NULL;
1023 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1025 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1029 if (WARN_ON(wdev->netdev))
1032 nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1034 list_del_rcu(&wdev->list);
1036 rdev->devlist_generation++;
1038 switch (wdev->iftype) {
1039 case NL80211_IFTYPE_P2P_DEVICE:
1040 cfg80211_mlme_purge_registrations(wdev);
1041 cfg80211_stop_p2p_device(rdev, wdev);
1043 case NL80211_IFTYPE_NAN:
1044 cfg80211_stop_nan(rdev, wdev);
1051 cfg80211_cqm_config_free(wdev);
1053 EXPORT_SYMBOL(cfg80211_unregister_wdev);
1055 static const struct device_type wiphy_type = {
1059 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1060 enum nl80211_iftype iftype, int num)
1064 rdev->num_running_ifaces += num;
1065 if (iftype == NL80211_IFTYPE_MONITOR)
1066 rdev->num_running_monitor_ifaces += num;
1069 void __cfg80211_leave(struct cfg80211_registered_device *rdev,
1070 struct wireless_dev *wdev)
1072 struct net_device *dev = wdev->netdev;
1073 struct cfg80211_sched_scan_request *pos, *tmp;
1076 ASSERT_WDEV_LOCK(wdev);
1078 switch (wdev->iftype) {
1079 case NL80211_IFTYPE_ADHOC:
1080 __cfg80211_leave_ibss(rdev, dev, true);
1082 case NL80211_IFTYPE_P2P_CLIENT:
1083 case NL80211_IFTYPE_STATION:
1084 list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1086 if (dev == pos->dev)
1087 cfg80211_stop_sched_scan_req(rdev, pos, false);
1090 #ifdef CONFIG_CFG80211_WEXT
1091 kfree(wdev->wext.ie);
1092 wdev->wext.ie = NULL;
1093 wdev->wext.ie_len = 0;
1094 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1096 cfg80211_disconnect(rdev, dev,
1097 WLAN_REASON_DEAUTH_LEAVING, true);
1099 case NL80211_IFTYPE_MESH_POINT:
1100 __cfg80211_leave_mesh(rdev, dev);
1102 case NL80211_IFTYPE_AP:
1103 case NL80211_IFTYPE_P2P_GO:
1104 __cfg80211_stop_ap(rdev, dev, true);
1106 case NL80211_IFTYPE_OCB:
1107 __cfg80211_leave_ocb(rdev, dev);
1109 case NL80211_IFTYPE_WDS:
1110 /* must be handled by mac80211/driver, has no APIs */
1112 case NL80211_IFTYPE_P2P_DEVICE:
1113 case NL80211_IFTYPE_NAN:
1114 /* cannot happen, has no netdev */
1116 case NL80211_IFTYPE_AP_VLAN:
1117 case NL80211_IFTYPE_MONITOR:
1120 case NL80211_IFTYPE_UNSPECIFIED:
1121 case NUM_NL80211_IFTYPES:
1127 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1128 struct wireless_dev *wdev)
1131 __cfg80211_leave(rdev, wdev);
1135 void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1138 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1139 struct cfg80211_event *ev;
1140 unsigned long flags;
1142 trace_cfg80211_stop_iface(wiphy, wdev);
1144 ev = kzalloc(sizeof(*ev), gfp);
1148 ev->type = EVENT_STOPPED;
1150 spin_lock_irqsave(&wdev->event_lock, flags);
1151 list_add_tail(&ev->list, &wdev->event_list);
1152 spin_unlock_irqrestore(&wdev->event_lock, flags);
1153 queue_work(cfg80211_wq, &rdev->event_work);
1155 EXPORT_SYMBOL(cfg80211_stop_iface);
1157 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1158 unsigned long state, void *ptr)
1160 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1161 struct wireless_dev *wdev = dev->ieee80211_ptr;
1162 struct cfg80211_registered_device *rdev;
1163 struct cfg80211_sched_scan_request *pos, *tmp;
1168 rdev = wiphy_to_rdev(wdev->wiphy);
1170 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1173 case NETDEV_POST_INIT:
1174 SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1176 case NETDEV_REGISTER:
1178 * NB: cannot take rdev->mtx here because this may be
1179 * called within code protected by it when interfaces
1180 * are added with nl80211.
1182 mutex_init(&wdev->mtx);
1183 INIT_LIST_HEAD(&wdev->event_list);
1184 spin_lock_init(&wdev->event_lock);
1185 INIT_LIST_HEAD(&wdev->mgmt_registrations);
1186 spin_lock_init(&wdev->mgmt_registrations_lock);
1189 * We get here also when the interface changes network namespaces,
1190 * as it's registered into the new one, but we don't want it to
1191 * change ID in that case. Checking if the ID is already assigned
1192 * works, because 0 isn't considered a valid ID and the memory is
1195 if (!wdev->identifier)
1196 wdev->identifier = ++rdev->wdev_id;
1197 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1198 rdev->devlist_generation++;
1199 /* can only change netns with wiphy */
1200 dev->features |= NETIF_F_NETNS_LOCAL;
1202 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
1204 pr_err("failed to add phy80211 symlink to netdev!\n");
1207 #ifdef CONFIG_CFG80211_WEXT
1208 wdev->wext.default_key = -1;
1209 wdev->wext.default_mgmt_key = -1;
1210 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1213 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1217 /* allow mac80211 to determine the timeout */
1218 wdev->ps_timeout = -1;
1220 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1221 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1222 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1223 dev->priv_flags |= IFF_DONT_BRIDGE;
1225 INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1227 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1229 case NETDEV_GOING_DOWN:
1230 cfg80211_leave(rdev, wdev);
1233 cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1234 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
1235 if (WARN_ON(!rdev->scan_req->notified))
1236 rdev->scan_req->info.aborted = true;
1237 ___cfg80211_scan_done(rdev, false);
1240 list_for_each_entry_safe(pos, tmp,
1241 &rdev->sched_scan_req_list, list) {
1242 if (WARN_ON(pos && pos->dev == wdev->netdev))
1243 cfg80211_stop_sched_scan_req(rdev, pos, false);
1247 wake_up(&rdev->dev_wait);
1250 cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1252 switch (wdev->iftype) {
1253 #ifdef CONFIG_CFG80211_WEXT
1254 case NL80211_IFTYPE_ADHOC:
1255 cfg80211_ibss_wext_join(rdev, wdev);
1257 case NL80211_IFTYPE_STATION:
1258 cfg80211_mgd_wext_connect(rdev, wdev);
1261 #ifdef CONFIG_MAC80211_MESH
1262 case NL80211_IFTYPE_MESH_POINT:
1264 /* backward compat code... */
1265 struct mesh_setup setup;
1266 memcpy(&setup, &default_mesh_setup,
1268 /* back compat only needed for mesh_id */
1269 setup.mesh_id = wdev->ssid;
1270 setup.mesh_id_len = wdev->mesh_id_up_len;
1271 if (wdev->mesh_id_up_len)
1272 __cfg80211_join_mesh(rdev, dev,
1274 &default_mesh_config);
1285 * Configure power management to the driver here so that its
1286 * correctly set also after interface type changes etc.
1288 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1289 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1290 rdev->ops->set_power_mgmt &&
1291 rdev_set_power_mgmt(rdev, dev, wdev->ps,
1292 wdev->ps_timeout)) {
1293 /* assume this means it's off */
1297 case NETDEV_UNREGISTER:
1299 * It is possible to get NETDEV_UNREGISTER
1300 * multiple times. To detect that, check
1301 * that the interface is still on the list
1302 * of registered interfaces, and only then
1303 * remove and clean it up.
1305 if (!list_empty(&wdev->list)) {
1306 nl80211_notify_iface(rdev, wdev,
1307 NL80211_CMD_DEL_INTERFACE);
1308 sysfs_remove_link(&dev->dev.kobj, "phy80211");
1309 list_del_rcu(&wdev->list);
1310 rdev->devlist_generation++;
1311 cfg80211_mlme_purge_registrations(wdev);
1312 #ifdef CONFIG_CFG80211_WEXT
1313 kzfree(wdev->wext.keys);
1315 flush_work(&wdev->disconnect_wk);
1316 cfg80211_cqm_config_free(wdev);
1319 * synchronise (so that we won't find this netdev
1320 * from other code any more) and then clear the list
1321 * head so that the above code can safely check for
1322 * !list_empty() to avoid double-cleanup.
1325 INIT_LIST_HEAD(&wdev->list);
1327 * Ensure that all events have been processed and
1330 cfg80211_process_wdev_events(wdev);
1332 if (WARN_ON(wdev->current_bss)) {
1333 cfg80211_unhold_bss(wdev->current_bss);
1334 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1335 wdev->current_bss = NULL;
1339 if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
1340 wdev->use_4addr, 0))
1341 return notifier_from_errno(-EOPNOTSUPP);
1343 if (rfkill_blocked(rdev->rfkill))
1344 return notifier_from_errno(-ERFKILL);
1350 wireless_nlevent_flush();
1355 static struct notifier_block cfg80211_netdev_notifier = {
1356 .notifier_call = cfg80211_netdev_notifier_call,
1359 static void __net_exit cfg80211_pernet_exit(struct net *net)
1361 struct cfg80211_registered_device *rdev;
1364 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1365 if (net_eq(wiphy_net(&rdev->wiphy), net))
1366 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1371 static struct pernet_operations cfg80211_pernet_ops = {
1372 .exit = cfg80211_pernet_exit,
1375 static int __init cfg80211_init(void)
1379 err = register_pernet_device(&cfg80211_pernet_ops);
1381 goto out_fail_pernet;
1383 err = wiphy_sysfs_init();
1385 goto out_fail_sysfs;
1387 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1389 goto out_fail_notifier;
1391 err = nl80211_init();
1393 goto out_fail_nl80211;
1395 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1397 err = regulatory_init();
1401 cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1412 debugfs_remove(ieee80211_debugfs_dir);
1415 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1419 unregister_pernet_device(&cfg80211_pernet_ops);
1423 fs_initcall(cfg80211_init);
1425 static void __exit cfg80211_exit(void)
1427 debugfs_remove(ieee80211_debugfs_dir);
1429 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1432 unregister_pernet_device(&cfg80211_pernet_ops);
1433 destroy_workqueue(cfg80211_wq);
1435 module_exit(cfg80211_exit);