cfg80211: limit wiphy names to 128 bytes
[linux/fpc-iii.git] / net / wireless / core.c
blob7fbf4dd07277e1820b6979389a00fd88bc8b8f7d
1 /*
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 Intel Deutschland GmbH
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/list.h>
15 #include <linux/slab.h>
16 #include <linux/nl80211.h>
17 #include <linux/debugfs.h>
18 #include <linux/notifier.h>
19 #include <linux/device.h>
20 #include <linux/etherdevice.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/sched.h>
23 #include <net/genetlink.h>
24 #include <net/cfg80211.h>
25 #include "nl80211.h"
26 #include "core.h"
27 #include "sysfs.h"
28 #include "debugfs.h"
29 #include "wext-compat.h"
30 #include "rdev-ops.h"
32 /* name for sysfs, %d is appended */
33 #define PHY_NAME "phy"
35 MODULE_AUTHOR("Johannes Berg");
36 MODULE_LICENSE("GPL");
37 MODULE_DESCRIPTION("wireless configuration support");
38 MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
40 /* RCU-protected (and RTNL for writers) */
41 LIST_HEAD(cfg80211_rdev_list);
42 int cfg80211_rdev_list_generation;
44 /* for debugfs */
45 static struct dentry *ieee80211_debugfs_dir;
47 /* for the cleanup, scan and event works */
48 struct workqueue_struct *cfg80211_wq;
50 static bool cfg80211_disable_40mhz_24ghz;
51 module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
52 MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
53 "Disable 40MHz support in the 2.4GHz band");
55 struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
57 struct cfg80211_registered_device *result = NULL, *rdev;
59 ASSERT_RTNL();
61 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
62 if (rdev->wiphy_idx == wiphy_idx) {
63 result = rdev;
64 break;
68 return result;
71 int get_wiphy_idx(struct wiphy *wiphy)
73 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
75 return rdev->wiphy_idx;
78 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
80 struct cfg80211_registered_device *rdev;
82 ASSERT_RTNL();
84 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
85 if (!rdev)
86 return NULL;
87 return &rdev->wiphy;
90 static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
91 const char *newname)
93 struct cfg80211_registered_device *rdev2;
94 int wiphy_idx, taken = -1, digits;
96 ASSERT_RTNL();
98 if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN)
99 return -EINVAL;
101 /* prohibit calling the thing phy%d when %d is not its number */
102 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
103 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
104 /* count number of places needed to print wiphy_idx */
105 digits = 1;
106 while (wiphy_idx /= 10)
107 digits++;
109 * deny the name if it is phy<idx> where <idx> is printed
110 * without leading zeroes. taken == strlen(newname) here
112 if (taken == strlen(PHY_NAME) + digits)
113 return -EINVAL;
116 /* Ensure another device does not already have this name. */
117 list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
118 if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
119 return -EINVAL;
121 return 0;
124 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
125 char *newname)
127 int result;
129 ASSERT_RTNL();
131 /* Ignore nop renames */
132 if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
133 return 0;
135 result = cfg80211_dev_check_name(rdev, newname);
136 if (result < 0)
137 return result;
139 result = device_rename(&rdev->wiphy.dev, newname);
140 if (result)
141 return result;
143 if (rdev->wiphy.debugfsdir &&
144 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
145 rdev->wiphy.debugfsdir,
146 rdev->wiphy.debugfsdir->d_parent,
147 newname))
148 pr_err("failed to rename debugfs dir to %s!\n", newname);
150 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
152 return 0;
155 int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
156 struct net *net)
158 struct wireless_dev *wdev;
159 int err = 0;
161 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
162 return -EOPNOTSUPP;
164 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
165 if (!wdev->netdev)
166 continue;
167 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
168 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
169 if (err)
170 break;
171 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
174 if (err) {
175 /* failed -- clean up to old netns */
176 net = wiphy_net(&rdev->wiphy);
178 list_for_each_entry_continue_reverse(wdev,
179 &rdev->wiphy.wdev_list,
180 list) {
181 if (!wdev->netdev)
182 continue;
183 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
184 err = dev_change_net_namespace(wdev->netdev, net,
185 "wlan%d");
186 WARN_ON(err);
187 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
190 return err;
193 wiphy_net_set(&rdev->wiphy, net);
195 err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
196 WARN_ON(err);
198 return 0;
201 static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
203 struct cfg80211_registered_device *rdev = data;
205 rdev_rfkill_poll(rdev);
208 void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
209 struct wireless_dev *wdev)
211 ASSERT_RTNL();
213 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
214 return;
216 if (!wdev->p2p_started)
217 return;
219 rdev_stop_p2p_device(rdev, wdev);
220 wdev->p2p_started = false;
222 rdev->opencount--;
224 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
225 if (WARN_ON(!rdev->scan_req->notified))
226 rdev->scan_req->info.aborted = true;
227 ___cfg80211_scan_done(rdev, false);
231 void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
232 struct wireless_dev *wdev)
234 ASSERT_RTNL();
236 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
237 return;
239 if (!wdev->nan_started)
240 return;
242 rdev_stop_nan(rdev, wdev);
243 wdev->nan_started = false;
245 rdev->opencount--;
248 void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
250 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
251 struct wireless_dev *wdev;
253 ASSERT_RTNL();
255 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
256 if (wdev->netdev) {
257 dev_close(wdev->netdev);
258 continue;
260 /* otherwise, check iftype */
261 switch (wdev->iftype) {
262 case NL80211_IFTYPE_P2P_DEVICE:
263 cfg80211_stop_p2p_device(rdev, wdev);
264 break;
265 case NL80211_IFTYPE_NAN:
266 cfg80211_stop_nan(rdev, wdev);
267 break;
268 default:
269 break;
273 EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
275 static int cfg80211_rfkill_set_block(void *data, bool blocked)
277 struct cfg80211_registered_device *rdev = data;
279 if (!blocked)
280 return 0;
282 rtnl_lock();
283 cfg80211_shutdown_all_interfaces(&rdev->wiphy);
284 rtnl_unlock();
286 return 0;
289 static void cfg80211_rfkill_sync_work(struct work_struct *work)
291 struct cfg80211_registered_device *rdev;
293 rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
294 cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
297 static void cfg80211_event_work(struct work_struct *work)
299 struct cfg80211_registered_device *rdev;
301 rdev = container_of(work, struct cfg80211_registered_device,
302 event_work);
304 rtnl_lock();
305 cfg80211_process_rdev_events(rdev);
306 rtnl_unlock();
309 void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
311 struct cfg80211_iface_destroy *item;
313 ASSERT_RTNL();
315 spin_lock_irq(&rdev->destroy_list_lock);
316 while ((item = list_first_entry_or_null(&rdev->destroy_list,
317 struct cfg80211_iface_destroy,
318 list))) {
319 struct wireless_dev *wdev, *tmp;
320 u32 nlportid = item->nlportid;
322 list_del(&item->list);
323 kfree(item);
324 spin_unlock_irq(&rdev->destroy_list_lock);
326 list_for_each_entry_safe(wdev, tmp,
327 &rdev->wiphy.wdev_list, list) {
328 if (nlportid == wdev->owner_nlportid)
329 rdev_del_virtual_intf(rdev, wdev);
332 spin_lock_irq(&rdev->destroy_list_lock);
334 spin_unlock_irq(&rdev->destroy_list_lock);
337 static void cfg80211_destroy_iface_wk(struct work_struct *work)
339 struct cfg80211_registered_device *rdev;
341 rdev = container_of(work, struct cfg80211_registered_device,
342 destroy_work);
344 rtnl_lock();
345 cfg80211_destroy_ifaces(rdev);
346 rtnl_unlock();
349 static void cfg80211_sched_scan_stop_wk(struct work_struct *work)
351 struct cfg80211_registered_device *rdev;
353 rdev = container_of(work, struct cfg80211_registered_device,
354 sched_scan_stop_wk);
356 rtnl_lock();
358 __cfg80211_stop_sched_scan(rdev, false);
360 rtnl_unlock();
363 /* exported functions */
365 struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
366 const char *requested_name)
368 static atomic_t wiphy_counter = ATOMIC_INIT(0);
370 struct cfg80211_registered_device *rdev;
371 int alloc_size;
373 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
374 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
375 WARN_ON(ops->connect && !ops->disconnect);
376 WARN_ON(ops->join_ibss && !ops->leave_ibss);
377 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
378 WARN_ON(ops->add_station && !ops->del_station);
379 WARN_ON(ops->add_mpath && !ops->del_mpath);
380 WARN_ON(ops->join_mesh && !ops->leave_mesh);
381 WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device);
382 WARN_ON(ops->start_ap && !ops->stop_ap);
383 WARN_ON(ops->join_ocb && !ops->leave_ocb);
384 WARN_ON(ops->suspend && !ops->resume);
385 WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop);
386 WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel);
387 WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch);
388 WARN_ON(ops->add_tx_ts && !ops->del_tx_ts);
390 alloc_size = sizeof(*rdev) + sizeof_priv;
392 rdev = kzalloc(alloc_size, GFP_KERNEL);
393 if (!rdev)
394 return NULL;
396 rdev->ops = ops;
398 rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
400 if (unlikely(rdev->wiphy_idx < 0)) {
401 /* ugh, wrapped! */
402 atomic_dec(&wiphy_counter);
403 kfree(rdev);
404 return NULL;
407 /* atomic_inc_return makes it start at 1, make it start at 0 */
408 rdev->wiphy_idx--;
410 /* give it a proper name */
411 if (requested_name && requested_name[0]) {
412 int rv;
414 rtnl_lock();
415 rv = cfg80211_dev_check_name(rdev, requested_name);
417 if (rv < 0) {
418 rtnl_unlock();
419 goto use_default_name;
422 rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name);
423 rtnl_unlock();
424 if (rv)
425 goto use_default_name;
426 } else {
427 int rv;
429 use_default_name:
430 /* NOTE: This is *probably* safe w/out holding rtnl because of
431 * the restrictions on phy names. Probably this call could
432 * fail if some other part of the kernel (re)named a device
433 * phyX. But, might should add some locking and check return
434 * value, and use a different name if this one exists?
436 rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
437 if (rv < 0) {
438 kfree(rdev);
439 return NULL;
443 INIT_LIST_HEAD(&rdev->wiphy.wdev_list);
444 INIT_LIST_HEAD(&rdev->beacon_registrations);
445 spin_lock_init(&rdev->beacon_registrations_lock);
446 spin_lock_init(&rdev->bss_lock);
447 INIT_LIST_HEAD(&rdev->bss_list);
448 INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
449 INIT_WORK(&rdev->sched_scan_results_wk, __cfg80211_sched_scan_results);
450 INIT_LIST_HEAD(&rdev->mlme_unreg);
451 spin_lock_init(&rdev->mlme_unreg_lock);
452 INIT_WORK(&rdev->mlme_unreg_wk, cfg80211_mlme_unreg_wk);
453 INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
454 cfg80211_dfs_channels_update_work);
455 #ifdef CONFIG_CFG80211_WEXT
456 rdev->wiphy.wext = &cfg80211_wext_handler;
457 #endif
459 device_initialize(&rdev->wiphy.dev);
460 rdev->wiphy.dev.class = &ieee80211_class;
461 rdev->wiphy.dev.platform_data = rdev;
462 device_enable_async_suspend(&rdev->wiphy.dev);
464 INIT_LIST_HEAD(&rdev->destroy_list);
465 spin_lock_init(&rdev->destroy_list_lock);
466 INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
467 INIT_WORK(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);
469 #ifdef CONFIG_CFG80211_DEFAULT_PS
470 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
471 #endif
473 wiphy_net_set(&rdev->wiphy, &init_net);
475 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
476 rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
477 &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
478 &rdev->rfkill_ops, rdev);
480 if (!rdev->rfkill) {
481 kfree(rdev);
482 return NULL;
485 INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
486 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
487 INIT_WORK(&rdev->event_work, cfg80211_event_work);
489 init_waitqueue_head(&rdev->dev_wait);
492 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
493 * Fragmentation and RTS threshold are disabled by default with the
494 * special -1 value.
496 rdev->wiphy.retry_short = 7;
497 rdev->wiphy.retry_long = 4;
498 rdev->wiphy.frag_threshold = (u32) -1;
499 rdev->wiphy.rts_threshold = (u32) -1;
500 rdev->wiphy.coverage_class = 0;
502 rdev->wiphy.max_num_csa_counters = 1;
504 rdev->wiphy.max_sched_scan_plans = 1;
505 rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
507 return &rdev->wiphy;
509 EXPORT_SYMBOL(wiphy_new_nm);
511 static int wiphy_verify_combinations(struct wiphy *wiphy)
513 const struct ieee80211_iface_combination *c;
514 int i, j;
516 for (i = 0; i < wiphy->n_iface_combinations; i++) {
517 u32 cnt = 0;
518 u16 all_iftypes = 0;
520 c = &wiphy->iface_combinations[i];
523 * Combinations with just one interface aren't real,
524 * however we make an exception for DFS.
526 if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
527 return -EINVAL;
529 /* Need at least one channel */
530 if (WARN_ON(!c->num_different_channels))
531 return -EINVAL;
534 * Put a sane limit on maximum number of different
535 * channels to simplify channel accounting code.
537 if (WARN_ON(c->num_different_channels >
538 CFG80211_MAX_NUM_DIFFERENT_CHANNELS))
539 return -EINVAL;
541 /* DFS only works on one channel. */
542 if (WARN_ON(c->radar_detect_widths &&
543 (c->num_different_channels > 1)))
544 return -EINVAL;
546 if (WARN_ON(!c->n_limits))
547 return -EINVAL;
549 for (j = 0; j < c->n_limits; j++) {
550 u16 types = c->limits[j].types;
552 /* interface types shouldn't overlap */
553 if (WARN_ON(types & all_iftypes))
554 return -EINVAL;
555 all_iftypes |= types;
557 if (WARN_ON(!c->limits[j].max))
558 return -EINVAL;
560 /* Shouldn't list software iftypes in combinations! */
561 if (WARN_ON(wiphy->software_iftypes & types))
562 return -EINVAL;
564 /* Only a single P2P_DEVICE can be allowed */
565 if (WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
566 c->limits[j].max > 1))
567 return -EINVAL;
569 /* Only a single NAN can be allowed */
570 if (WARN_ON(types & BIT(NL80211_IFTYPE_NAN) &&
571 c->limits[j].max > 1))
572 return -EINVAL;
574 cnt += c->limits[j].max;
576 * Don't advertise an unsupported type
577 * in a combination.
579 if (WARN_ON((wiphy->interface_modes & types) != types))
580 return -EINVAL;
583 /* You can't even choose that many! */
584 if (WARN_ON(cnt < c->max_interfaces))
585 return -EINVAL;
588 return 0;
591 int wiphy_register(struct wiphy *wiphy)
593 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
594 int res;
595 enum nl80211_band band;
596 struct ieee80211_supported_band *sband;
597 bool have_band = false;
598 int i;
599 u16 ifmodes = wiphy->interface_modes;
601 #ifdef CONFIG_PM
602 if (WARN_ON(wiphy->wowlan &&
603 (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
604 !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
605 return -EINVAL;
606 if (WARN_ON(wiphy->wowlan &&
607 !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns &&
608 !wiphy->wowlan->tcp))
609 return -EINVAL;
610 #endif
611 if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
612 (!rdev->ops->tdls_channel_switch ||
613 !rdev->ops->tdls_cancel_channel_switch)))
614 return -EINVAL;
616 if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
617 (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
618 !rdev->ops->add_nan_func || !rdev->ops->del_nan_func)))
619 return -EINVAL;
622 * if a wiphy has unsupported modes for regulatory channel enforcement,
623 * opt-out of enforcement checking
625 if (wiphy->interface_modes & ~(BIT(NL80211_IFTYPE_STATION) |
626 BIT(NL80211_IFTYPE_P2P_CLIENT) |
627 BIT(NL80211_IFTYPE_AP) |
628 BIT(NL80211_IFTYPE_P2P_GO) |
629 BIT(NL80211_IFTYPE_ADHOC) |
630 BIT(NL80211_IFTYPE_P2P_DEVICE) |
631 BIT(NL80211_IFTYPE_NAN) |
632 BIT(NL80211_IFTYPE_AP_VLAN) |
633 BIT(NL80211_IFTYPE_MONITOR)))
634 wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF;
636 if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
637 (wiphy->regulatory_flags &
638 (REGULATORY_CUSTOM_REG |
639 REGULATORY_STRICT_REG |
640 REGULATORY_COUNTRY_IE_FOLLOW_POWER |
641 REGULATORY_COUNTRY_IE_IGNORE))))
642 return -EINVAL;
644 if (WARN_ON(wiphy->coalesce &&
645 (!wiphy->coalesce->n_rules ||
646 !wiphy->coalesce->n_patterns) &&
647 (!wiphy->coalesce->pattern_min_len ||
648 wiphy->coalesce->pattern_min_len >
649 wiphy->coalesce->pattern_max_len)))
650 return -EINVAL;
652 if (WARN_ON(wiphy->ap_sme_capa &&
653 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
654 return -EINVAL;
656 if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
657 return -EINVAL;
659 if (WARN_ON(wiphy->addresses &&
660 !is_zero_ether_addr(wiphy->perm_addr) &&
661 memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
662 ETH_ALEN)))
663 return -EINVAL;
665 if (WARN_ON(wiphy->max_acl_mac_addrs &&
666 (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
667 !rdev->ops->set_mac_acl)))
668 return -EINVAL;
670 /* assure only valid behaviours are flagged by driver
671 * hence subtract 2 as bit 0 is invalid.
673 if (WARN_ON(wiphy->bss_select_support &&
674 (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
675 return -EINVAL;
677 if (wiphy->addresses)
678 memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
680 /* sanity check ifmodes */
681 WARN_ON(!ifmodes);
682 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
683 if (WARN_ON(ifmodes != wiphy->interface_modes))
684 wiphy->interface_modes = ifmodes;
686 res = wiphy_verify_combinations(wiphy);
687 if (res)
688 return res;
690 /* sanity check supported bands/channels */
691 for (band = 0; band < NUM_NL80211_BANDS; band++) {
692 sband = wiphy->bands[band];
693 if (!sband)
694 continue;
696 sband->band = band;
697 if (WARN_ON(!sband->n_channels))
698 return -EINVAL;
700 * on 60GHz band, there are no legacy rates, so
701 * n_bitrates is 0
703 if (WARN_ON(band != NL80211_BAND_60GHZ &&
704 !sband->n_bitrates))
705 return -EINVAL;
708 * Since cfg80211_disable_40mhz_24ghz is global, we can
709 * modify the sband's ht data even if the driver uses a
710 * global structure for that.
712 if (cfg80211_disable_40mhz_24ghz &&
713 band == NL80211_BAND_2GHZ &&
714 sband->ht_cap.ht_supported) {
715 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
716 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
720 * Since we use a u32 for rate bitmaps in
721 * ieee80211_get_response_rate, we cannot
722 * have more than 32 legacy rates.
724 if (WARN_ON(sband->n_bitrates > 32))
725 return -EINVAL;
727 for (i = 0; i < sband->n_channels; i++) {
728 sband->channels[i].orig_flags =
729 sband->channels[i].flags;
730 sband->channels[i].orig_mag = INT_MAX;
731 sband->channels[i].orig_mpwr =
732 sband->channels[i].max_power;
733 sband->channels[i].band = band;
736 have_band = true;
739 if (!have_band) {
740 WARN_ON(1);
741 return -EINVAL;
744 #ifdef CONFIG_PM
745 if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
746 (!rdev->wiphy.wowlan->pattern_min_len ||
747 rdev->wiphy.wowlan->pattern_min_len >
748 rdev->wiphy.wowlan->pattern_max_len)))
749 return -EINVAL;
750 #endif
752 /* check and set up bitrates */
753 ieee80211_set_bitrate_flags(wiphy);
755 rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
757 rtnl_lock();
758 res = device_add(&rdev->wiphy.dev);
759 if (res) {
760 rtnl_unlock();
761 return res;
764 /* set up regulatory info */
765 wiphy_regulatory_register(wiphy);
767 list_add_rcu(&rdev->list, &cfg80211_rdev_list);
768 cfg80211_rdev_list_generation++;
770 /* add to debugfs */
771 rdev->wiphy.debugfsdir =
772 debugfs_create_dir(wiphy_name(&rdev->wiphy),
773 ieee80211_debugfs_dir);
774 if (IS_ERR(rdev->wiphy.debugfsdir))
775 rdev->wiphy.debugfsdir = NULL;
777 cfg80211_debugfs_rdev_add(rdev);
778 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
780 if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
781 struct regulatory_request request;
783 request.wiphy_idx = get_wiphy_idx(wiphy);
784 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
785 request.alpha2[0] = '9';
786 request.alpha2[1] = '9';
788 nl80211_send_reg_change_event(&request);
791 /* Check that nobody globally advertises any capabilities they do not
792 * advertise on all possible interface types.
794 if (wiphy->extended_capabilities_len &&
795 wiphy->num_iftype_ext_capab &&
796 wiphy->iftype_ext_capab) {
797 u8 supported_on_all, j;
798 const struct wiphy_iftype_ext_capab *capab;
800 capab = wiphy->iftype_ext_capab;
801 for (j = 0; j < wiphy->extended_capabilities_len; j++) {
802 if (capab[0].extended_capabilities_len > j)
803 supported_on_all =
804 capab[0].extended_capabilities[j];
805 else
806 supported_on_all = 0x00;
807 for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
808 if (j >= capab[i].extended_capabilities_len) {
809 supported_on_all = 0x00;
810 break;
812 supported_on_all &=
813 capab[i].extended_capabilities[j];
815 if (WARN_ON(wiphy->extended_capabilities[j] &
816 ~supported_on_all))
817 break;
821 rdev->wiphy.registered = true;
822 rtnl_unlock();
824 res = rfkill_register(rdev->rfkill);
825 if (res) {
826 rfkill_destroy(rdev->rfkill);
827 rdev->rfkill = NULL;
828 wiphy_unregister(&rdev->wiphy);
829 return res;
832 return 0;
834 EXPORT_SYMBOL(wiphy_register);
836 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
838 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
840 if (!rdev->ops->rfkill_poll)
841 return;
842 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
843 rfkill_resume_polling(rdev->rfkill);
845 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
847 void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
849 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
851 rfkill_pause_polling(rdev->rfkill);
853 EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
855 void wiphy_unregister(struct wiphy *wiphy)
857 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
859 wait_event(rdev->dev_wait, ({
860 int __count;
861 rtnl_lock();
862 __count = rdev->opencount;
863 rtnl_unlock();
864 __count == 0; }));
866 if (rdev->rfkill)
867 rfkill_unregister(rdev->rfkill);
869 rtnl_lock();
870 nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
871 rdev->wiphy.registered = false;
873 WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
876 * First remove the hardware from everywhere, this makes
877 * it impossible to find from userspace.
879 debugfs_remove_recursive(rdev->wiphy.debugfsdir);
880 list_del_rcu(&rdev->list);
881 synchronize_rcu();
884 * If this device got a regulatory hint tell core its
885 * free to listen now to a new shiny device regulatory hint
887 wiphy_regulatory_deregister(wiphy);
889 cfg80211_rdev_list_generation++;
890 device_del(&rdev->wiphy.dev);
892 rtnl_unlock();
894 flush_work(&rdev->scan_done_wk);
895 cancel_work_sync(&rdev->conn_work);
896 flush_work(&rdev->event_work);
897 cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
898 flush_work(&rdev->destroy_work);
899 flush_work(&rdev->sched_scan_stop_wk);
900 flush_work(&rdev->mlme_unreg_wk);
902 #ifdef CONFIG_PM
903 if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
904 rdev_set_wakeup(rdev, false);
905 #endif
906 cfg80211_rdev_free_wowlan(rdev);
907 cfg80211_rdev_free_coalesce(rdev);
909 EXPORT_SYMBOL(wiphy_unregister);
911 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
913 struct cfg80211_internal_bss *scan, *tmp;
914 struct cfg80211_beacon_registration *reg, *treg;
915 rfkill_destroy(rdev->rfkill);
916 list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
917 list_del(&reg->list);
918 kfree(reg);
920 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
921 cfg80211_put_bss(&rdev->wiphy, &scan->pub);
922 kfree(rdev);
925 void wiphy_free(struct wiphy *wiphy)
927 put_device(&wiphy->dev);
929 EXPORT_SYMBOL(wiphy_free);
931 void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
933 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
935 if (rfkill_set_hw_state(rdev->rfkill, blocked))
936 schedule_work(&rdev->rfkill_sync);
938 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
940 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
942 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
944 ASSERT_RTNL();
946 if (WARN_ON(wdev->netdev))
947 return;
949 nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
951 list_del_rcu(&wdev->list);
952 rdev->devlist_generation++;
954 switch (wdev->iftype) {
955 case NL80211_IFTYPE_P2P_DEVICE:
956 cfg80211_mlme_purge_registrations(wdev);
957 cfg80211_stop_p2p_device(rdev, wdev);
958 break;
959 case NL80211_IFTYPE_NAN:
960 cfg80211_stop_nan(rdev, wdev);
961 break;
962 default:
963 WARN_ON_ONCE(1);
964 break;
967 EXPORT_SYMBOL(cfg80211_unregister_wdev);
969 static const struct device_type wiphy_type = {
970 .name = "wlan",
973 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
974 enum nl80211_iftype iftype, int num)
976 ASSERT_RTNL();
978 rdev->num_running_ifaces += num;
979 if (iftype == NL80211_IFTYPE_MONITOR)
980 rdev->num_running_monitor_ifaces += num;
983 void __cfg80211_leave(struct cfg80211_registered_device *rdev,
984 struct wireless_dev *wdev)
986 struct net_device *dev = wdev->netdev;
987 struct cfg80211_sched_scan_request *sched_scan_req;
989 ASSERT_RTNL();
990 ASSERT_WDEV_LOCK(wdev);
992 switch (wdev->iftype) {
993 case NL80211_IFTYPE_ADHOC:
994 __cfg80211_leave_ibss(rdev, dev, true);
995 break;
996 case NL80211_IFTYPE_P2P_CLIENT:
997 case NL80211_IFTYPE_STATION:
998 sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
999 if (sched_scan_req && dev == sched_scan_req->dev)
1000 __cfg80211_stop_sched_scan(rdev, false);
1002 #ifdef CONFIG_CFG80211_WEXT
1003 kfree(wdev->wext.ie);
1004 wdev->wext.ie = NULL;
1005 wdev->wext.ie_len = 0;
1006 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1007 #endif
1008 cfg80211_disconnect(rdev, dev,
1009 WLAN_REASON_DEAUTH_LEAVING, true);
1010 break;
1011 case NL80211_IFTYPE_MESH_POINT:
1012 __cfg80211_leave_mesh(rdev, dev);
1013 break;
1014 case NL80211_IFTYPE_AP:
1015 case NL80211_IFTYPE_P2P_GO:
1016 __cfg80211_stop_ap(rdev, dev, true);
1017 break;
1018 case NL80211_IFTYPE_OCB:
1019 __cfg80211_leave_ocb(rdev, dev);
1020 break;
1021 case NL80211_IFTYPE_WDS:
1022 /* must be handled by mac80211/driver, has no APIs */
1023 break;
1024 case NL80211_IFTYPE_P2P_DEVICE:
1025 case NL80211_IFTYPE_NAN:
1026 /* cannot happen, has no netdev */
1027 break;
1028 case NL80211_IFTYPE_AP_VLAN:
1029 case NL80211_IFTYPE_MONITOR:
1030 /* nothing to do */
1031 break;
1032 case NL80211_IFTYPE_UNSPECIFIED:
1033 case NUM_NL80211_IFTYPES:
1034 /* invalid */
1035 break;
1039 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1040 struct wireless_dev *wdev)
1042 wdev_lock(wdev);
1043 __cfg80211_leave(rdev, wdev);
1044 wdev_unlock(wdev);
1047 void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1048 gfp_t gfp)
1050 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1051 struct cfg80211_event *ev;
1052 unsigned long flags;
1054 trace_cfg80211_stop_iface(wiphy, wdev);
1056 ev = kzalloc(sizeof(*ev), gfp);
1057 if (!ev)
1058 return;
1060 ev->type = EVENT_STOPPED;
1062 spin_lock_irqsave(&wdev->event_lock, flags);
1063 list_add_tail(&ev->list, &wdev->event_list);
1064 spin_unlock_irqrestore(&wdev->event_lock, flags);
1065 queue_work(cfg80211_wq, &rdev->event_work);
1067 EXPORT_SYMBOL(cfg80211_stop_iface);
1069 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1070 unsigned long state, void *ptr)
1072 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1073 struct wireless_dev *wdev = dev->ieee80211_ptr;
1074 struct cfg80211_registered_device *rdev;
1075 struct cfg80211_sched_scan_request *sched_scan_req;
1077 if (!wdev)
1078 return NOTIFY_DONE;
1080 rdev = wiphy_to_rdev(wdev->wiphy);
1082 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1084 switch (state) {
1085 case NETDEV_POST_INIT:
1086 SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1087 break;
1088 case NETDEV_REGISTER:
1090 * NB: cannot take rdev->mtx here because this may be
1091 * called within code protected by it when interfaces
1092 * are added with nl80211.
1094 mutex_init(&wdev->mtx);
1095 INIT_LIST_HEAD(&wdev->event_list);
1096 spin_lock_init(&wdev->event_lock);
1097 INIT_LIST_HEAD(&wdev->mgmt_registrations);
1098 spin_lock_init(&wdev->mgmt_registrations_lock);
1100 wdev->identifier = ++rdev->wdev_id;
1101 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1102 rdev->devlist_generation++;
1103 /* can only change netns with wiphy */
1104 dev->features |= NETIF_F_NETNS_LOCAL;
1106 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
1107 "phy80211")) {
1108 pr_err("failed to add phy80211 symlink to netdev!\n");
1110 wdev->netdev = dev;
1111 #ifdef CONFIG_CFG80211_WEXT
1112 wdev->wext.default_key = -1;
1113 wdev->wext.default_mgmt_key = -1;
1114 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1115 #endif
1117 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1118 wdev->ps = true;
1119 else
1120 wdev->ps = false;
1121 /* allow mac80211 to determine the timeout */
1122 wdev->ps_timeout = -1;
1124 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1125 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1126 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1127 dev->priv_flags |= IFF_DONT_BRIDGE;
1129 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1130 break;
1131 case NETDEV_GOING_DOWN:
1132 cfg80211_leave(rdev, wdev);
1133 break;
1134 case NETDEV_DOWN:
1135 cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1136 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
1137 if (WARN_ON(!rdev->scan_req->notified))
1138 rdev->scan_req->info.aborted = true;
1139 ___cfg80211_scan_done(rdev, false);
1142 sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
1143 if (WARN_ON(sched_scan_req &&
1144 sched_scan_req->dev == wdev->netdev)) {
1145 __cfg80211_stop_sched_scan(rdev, false);
1148 rdev->opencount--;
1149 wake_up(&rdev->dev_wait);
1150 break;
1151 case NETDEV_UP:
1152 cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1153 wdev_lock(wdev);
1154 switch (wdev->iftype) {
1155 #ifdef CONFIG_CFG80211_WEXT
1156 case NL80211_IFTYPE_ADHOC:
1157 cfg80211_ibss_wext_join(rdev, wdev);
1158 break;
1159 case NL80211_IFTYPE_STATION:
1160 cfg80211_mgd_wext_connect(rdev, wdev);
1161 break;
1162 #endif
1163 #ifdef CONFIG_MAC80211_MESH
1164 case NL80211_IFTYPE_MESH_POINT:
1166 /* backward compat code... */
1167 struct mesh_setup setup;
1168 memcpy(&setup, &default_mesh_setup,
1169 sizeof(setup));
1170 /* back compat only needed for mesh_id */
1171 setup.mesh_id = wdev->ssid;
1172 setup.mesh_id_len = wdev->mesh_id_up_len;
1173 if (wdev->mesh_id_up_len)
1174 __cfg80211_join_mesh(rdev, dev,
1175 &setup,
1176 &default_mesh_config);
1177 break;
1179 #endif
1180 default:
1181 break;
1183 wdev_unlock(wdev);
1184 rdev->opencount++;
1187 * Configure power management to the driver here so that its
1188 * correctly set also after interface type changes etc.
1190 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1191 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1192 rdev->ops->set_power_mgmt)
1193 if (rdev_set_power_mgmt(rdev, dev, wdev->ps,
1194 wdev->ps_timeout)) {
1195 /* assume this means it's off */
1196 wdev->ps = false;
1198 break;
1199 case NETDEV_UNREGISTER:
1201 * It is possible to get NETDEV_UNREGISTER
1202 * multiple times. To detect that, check
1203 * that the interface is still on the list
1204 * of registered interfaces, and only then
1205 * remove and clean it up.
1207 if (!list_empty(&wdev->list)) {
1208 nl80211_notify_iface(rdev, wdev,
1209 NL80211_CMD_DEL_INTERFACE);
1210 sysfs_remove_link(&dev->dev.kobj, "phy80211");
1211 list_del_rcu(&wdev->list);
1212 rdev->devlist_generation++;
1213 cfg80211_mlme_purge_registrations(wdev);
1214 #ifdef CONFIG_CFG80211_WEXT
1215 kzfree(wdev->wext.keys);
1216 #endif
1219 * synchronise (so that we won't find this netdev
1220 * from other code any more) and then clear the list
1221 * head so that the above code can safely check for
1222 * !list_empty() to avoid double-cleanup.
1224 synchronize_rcu();
1225 INIT_LIST_HEAD(&wdev->list);
1227 * Ensure that all events have been processed and
1228 * freed.
1230 cfg80211_process_wdev_events(wdev);
1232 if (WARN_ON(wdev->current_bss)) {
1233 cfg80211_unhold_bss(wdev->current_bss);
1234 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1235 wdev->current_bss = NULL;
1237 break;
1238 case NETDEV_PRE_UP:
1239 if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
1240 return notifier_from_errno(-EOPNOTSUPP);
1241 if (rfkill_blocked(rdev->rfkill))
1242 return notifier_from_errno(-ERFKILL);
1243 break;
1244 default:
1245 return NOTIFY_DONE;
1248 wireless_nlevent_flush();
1250 return NOTIFY_OK;
1253 static struct notifier_block cfg80211_netdev_notifier = {
1254 .notifier_call = cfg80211_netdev_notifier_call,
1257 static void __net_exit cfg80211_pernet_exit(struct net *net)
1259 struct cfg80211_registered_device *rdev;
1261 rtnl_lock();
1262 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1263 if (net_eq(wiphy_net(&rdev->wiphy), net))
1264 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1266 rtnl_unlock();
1269 static struct pernet_operations cfg80211_pernet_ops = {
1270 .exit = cfg80211_pernet_exit,
1273 static int __init cfg80211_init(void)
1275 int err;
1277 err = register_pernet_device(&cfg80211_pernet_ops);
1278 if (err)
1279 goto out_fail_pernet;
1281 err = wiphy_sysfs_init();
1282 if (err)
1283 goto out_fail_sysfs;
1285 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1286 if (err)
1287 goto out_fail_notifier;
1289 err = nl80211_init();
1290 if (err)
1291 goto out_fail_nl80211;
1293 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1295 err = regulatory_init();
1296 if (err)
1297 goto out_fail_reg;
1299 cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1300 if (!cfg80211_wq) {
1301 err = -ENOMEM;
1302 goto out_fail_wq;
1305 return 0;
1307 out_fail_wq:
1308 regulatory_exit();
1309 out_fail_reg:
1310 debugfs_remove(ieee80211_debugfs_dir);
1311 nl80211_exit();
1312 out_fail_nl80211:
1313 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1314 out_fail_notifier:
1315 wiphy_sysfs_exit();
1316 out_fail_sysfs:
1317 unregister_pernet_device(&cfg80211_pernet_ops);
1318 out_fail_pernet:
1319 return err;
1321 subsys_initcall(cfg80211_init);
1323 static void __exit cfg80211_exit(void)
1325 debugfs_remove(ieee80211_debugfs_dir);
1326 nl80211_exit();
1327 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1328 wiphy_sysfs_exit();
1329 regulatory_exit();
1330 unregister_pernet_device(&cfg80211_pernet_ops);
1331 destroy_workqueue(cfg80211_wq);
1333 module_exit(cfg80211_exit);