mac80211: Fix condition validating WMM IE
[linux-block.git] / net / wireless / core.c
CommitLineData
704232c2
JB
1/*
2 * This is the linux wireless configuration interface.
3 *
5f2aa25e 4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
2740f0cf 5 * Copyright 2013-2014 Intel Mobile Communications GmbH
53873f13 6 * Copyright 2015 Intel Deutschland GmbH
704232c2
JB
7 */
8
e9c0268f
JP
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
704232c2
JB
11#include <linux/if.h>
12#include <linux/module.h>
13#include <linux/err.h>
704232c2 14#include <linux/list.h>
5a0e3ad6 15#include <linux/slab.h>
704232c2
JB
16#include <linux/nl80211.h>
17#include <linux/debugfs.h>
18#include <linux/notifier.h>
19#include <linux/device.h>
16a832e7 20#include <linux/etherdevice.h>
1f87f7d3 21#include <linux/rtnetlink.h>
d43c36dc 22#include <linux/sched.h>
704232c2
JB
23#include <net/genetlink.h>
24#include <net/cfg80211.h>
55682965 25#include "nl80211.h"
704232c2
JB
26#include "core.h"
27#include "sysfs.h"
1ac61302 28#include "debugfs.h"
a9a11622 29#include "wext-compat.h"
e35e4d28 30#include "rdev-ops.h"
704232c2
JB
31
32/* name for sysfs, %d is appended */
33#define PHY_NAME "phy"
34
35MODULE_AUTHOR("Johannes Berg");
36MODULE_LICENSE("GPL");
37MODULE_DESCRIPTION("wireless configuration support");
fb4e1568 38MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
704232c2 39
5fe231e8 40/* RCU-protected (and RTNL for writers) */
79c97e97 41LIST_HEAD(cfg80211_rdev_list);
f5ea9120 42int cfg80211_rdev_list_generation;
a1794390 43
704232c2
JB
44/* for debugfs */
45static struct dentry *ieee80211_debugfs_dir;
46
e60d7443
AB
47/* for the cleanup, scan and event works */
48struct workqueue_struct *cfg80211_wq;
49
40db6c77
AK
50static bool cfg80211_disable_40mhz_24ghz;
51module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
52MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
53 "Disable 40MHz support in the 2.4GHz band");
54
79c97e97 55struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
55682965 56{
79c97e97 57 struct cfg80211_registered_device *result = NULL, *rdev;
55682965 58
5fe231e8 59 ASSERT_RTNL();
761cf7ec 60
79c97e97
JB
61 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
62 if (rdev->wiphy_idx == wiphy_idx) {
63 result = rdev;
55682965
JB
64 break;
65 }
66 }
67
68 return result;
69}
70
806a9e39
LR
71int get_wiphy_idx(struct wiphy *wiphy)
72{
f26cbf40 73 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
f4173766 74
79c97e97 75 return rdev->wiphy_idx;
806a9e39
LR
76}
77
806a9e39
LR
78struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
79{
79c97e97 80 struct cfg80211_registered_device *rdev;
806a9e39 81
5fe231e8 82 ASSERT_RTNL();
806a9e39 83
79c97e97
JB
84 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
85 if (!rdev)
806a9e39 86 return NULL;
79c97e97 87 return &rdev->wiphy;
806a9e39
LR
88}
89
1998d90a
BG
90static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
91 const char *newname)
55682965 92{
79c97e97 93 struct cfg80211_registered_device *rdev2;
1998d90a 94 int wiphy_idx, taken = -1, digits;
55682965 95
5fe231e8 96 ASSERT_RTNL();
2940bb69 97
7623225f
JB
98 /* prohibit calling the thing phy%d when %d is not its number */
99 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
100 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
101 /* count number of places needed to print wiphy_idx */
102 digits = 1;
103 while (wiphy_idx /= 10)
104 digits++;
105 /*
106 * deny the name if it is phy<idx> where <idx> is printed
107 * without leading zeroes. taken == strlen(newname) here
108 */
109 if (taken == strlen(PHY_NAME) + digits)
110 return -EINVAL;
111 }
112
1998d90a
BG
113 /* Ensure another device does not already have this name. */
114 list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
115 if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
116 return -EINVAL;
117
118 return 0;
119}
120
121int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
122 char *newname)
123{
124 int result;
125
126 ASSERT_RTNL();
7623225f 127
2940bb69 128 /* Ignore nop renames */
1998d90a 129 if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
4bbf4d56 130 return 0;
2940bb69 131
1998d90a
BG
132 result = cfg80211_dev_check_name(rdev, newname);
133 if (result < 0)
134 return result;
55682965 135
55682965
JB
136 result = device_rename(&rdev->wiphy.dev, newname);
137 if (result)
4bbf4d56 138 return result;
55682965 139
33c0360b
JB
140 if (rdev->wiphy.debugfsdir &&
141 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
55682965
JB
142 rdev->wiphy.debugfsdir,
143 rdev->wiphy.debugfsdir->d_parent,
144 newname))
e9c0268f 145 pr_err("failed to rename debugfs dir to %s!\n", newname);
55682965 146
3bb20556 147 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
55682965 148
4bbf4d56 149 return 0;
55682965
JB
150}
151
463d0183
JB
152int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
153 struct net *net)
154{
155 struct wireless_dev *wdev;
156 int err = 0;
157
5be83de5 158 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
463d0183
JB
159 return -EOPNOTSUPP;
160
53873f13 161 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
ba22fb5b
JB
162 if (!wdev->netdev)
163 continue;
463d0183
JB
164 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
165 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
166 if (err)
167 break;
168 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
169 }
170
171 if (err) {
172 /* failed -- clean up to old netns */
173 net = wiphy_net(&rdev->wiphy);
174
53873f13
JB
175 list_for_each_entry_continue_reverse(wdev,
176 &rdev->wiphy.wdev_list,
463d0183 177 list) {
ba22fb5b
JB
178 if (!wdev->netdev)
179 continue;
463d0183
JB
180 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
181 err = dev_change_net_namespace(wdev->netdev, net,
182 "wlan%d");
183 WARN_ON(err);
184 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
185 }
04600794
JB
186
187 return err;
463d0183
JB
188 }
189
190 wiphy_net_set(&rdev->wiphy, net);
191
04600794
JB
192 err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
193 WARN_ON(err);
194
195 return 0;
463d0183
JB
196}
197
1f87f7d3
JB
198static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
199{
79c97e97 200 struct cfg80211_registered_device *rdev = data;
1f87f7d3 201
e35e4d28 202 rdev_rfkill_poll(rdev);
1f87f7d3
JB
203}
204
f9f47529
JB
205void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
206 struct wireless_dev *wdev)
207{
5fe231e8 208 ASSERT_RTNL();
f9f47529
JB
209
210 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
211 return;
212
73c7da3d 213 if (!wdev_running(wdev))
f9f47529
JB
214 return;
215
216 rdev_stop_p2p_device(rdev, wdev);
73c7da3d 217 wdev->is_running = false;
f9f47529
JB
218
219 rdev->opencount--;
220
a617302c
JB
221 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
222 if (WARN_ON(!rdev->scan_req->notified))
1d76250b 223 rdev->scan_req->info.aborted = true;
f9d15d16 224 ___cfg80211_scan_done(rdev, false);
a617302c 225 }
f9f47529
JB
226}
227
cb3b7d87
AB
228void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
229 struct wireless_dev *wdev)
230{
231 ASSERT_RTNL();
232
233 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
234 return;
235
73c7da3d 236 if (!wdev_running(wdev))
cb3b7d87
AB
237 return;
238
239 rdev_stop_nan(rdev, wdev);
73c7da3d 240 wdev->is_running = false;
cb3b7d87
AB
241
242 rdev->opencount--;
243}
244
f6837ba8 245void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
1f87f7d3 246{
f6837ba8 247 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1f87f7d3
JB
248 struct wireless_dev *wdev;
249
f6837ba8 250 ASSERT_RTNL();
f9f47529 251
53873f13 252 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
98104fde 253 if (wdev->netdev) {
ba22fb5b 254 dev_close(wdev->netdev);
98104fde
JB
255 continue;
256 }
257 /* otherwise, check iftype */
258 switch (wdev->iftype) {
259 case NL80211_IFTYPE_P2P_DEVICE:
f9f47529 260 cfg80211_stop_p2p_device(rdev, wdev);
98104fde 261 break;
cb3b7d87
AB
262 case NL80211_IFTYPE_NAN:
263 cfg80211_stop_nan(rdev, wdev);
264 break;
98104fde
JB
265 default:
266 break;
267 }
268 }
f6837ba8
JB
269}
270EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
1f87f7d3 271
f6837ba8
JB
272static int cfg80211_rfkill_set_block(void *data, bool blocked)
273{
274 struct cfg80211_registered_device *rdev = data;
275
276 if (!blocked)
277 return 0;
278
279 rtnl_lock();
280 cfg80211_shutdown_all_interfaces(&rdev->wiphy);
1f87f7d3
JB
281 rtnl_unlock();
282
283 return 0;
284}
285
286static void cfg80211_rfkill_sync_work(struct work_struct *work)
287{
79c97e97 288 struct cfg80211_registered_device *rdev;
1f87f7d3 289
79c97e97
JB
290 rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
291 cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
1f87f7d3
JB
292}
293
667503dd
JB
294static void cfg80211_event_work(struct work_struct *work)
295{
296 struct cfg80211_registered_device *rdev;
667503dd
JB
297
298 rdev = container_of(work, struct cfg80211_registered_device,
299 event_work);
300
301 rtnl_lock();
3d54d255 302 cfg80211_process_rdev_events(rdev);
667503dd
JB
303 rtnl_unlock();
304}
305
78f22b6a
JB
306void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
307{
ab81007a 308 struct wireless_dev *wdev, *tmp;
78f22b6a
JB
309
310 ASSERT_RTNL();
311
ab81007a
JB
312 list_for_each_entry_safe(wdev, tmp, &rdev->wiphy.wdev_list, list) {
313 if (wdev->nl_owner_dead)
314 rdev_del_virtual_intf(rdev, wdev);
78f22b6a 315 }
78f22b6a
JB
316}
317
318static void cfg80211_destroy_iface_wk(struct work_struct *work)
319{
320 struct cfg80211_registered_device *rdev;
321
322 rdev = container_of(work, struct cfg80211_registered_device,
323 destroy_work);
324
325 rtnl_lock();
326 cfg80211_destroy_ifaces(rdev);
327 rtnl_unlock();
328}
329
93a1e86c
JR
330static void cfg80211_sched_scan_stop_wk(struct work_struct *work)
331{
332 struct cfg80211_registered_device *rdev;
ca986ad9 333 struct cfg80211_sched_scan_request *req, *tmp;
93a1e86c
JR
334
335 rdev = container_of(work, struct cfg80211_registered_device,
336 sched_scan_stop_wk);
337
338 rtnl_lock();
ca986ad9
AVS
339 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
340 if (req->nl_owner_dead)
341 cfg80211_stop_sched_scan_req(rdev, req, false);
342 }
93a1e86c
JR
343 rtnl_unlock();
344}
345
89766727
VT
346static void cfg80211_propagate_radar_detect_wk(struct work_struct *work)
347{
348 struct cfg80211_registered_device *rdev;
349
350 rdev = container_of(work, struct cfg80211_registered_device,
351 propagate_radar_detect_wk);
352
353 rtnl_lock();
354
355 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->radar_chandef,
356 NL80211_DFS_UNAVAILABLE,
357 NL80211_RADAR_DETECTED);
358
359 rtnl_unlock();
360}
361
362static void cfg80211_propagate_cac_done_wk(struct work_struct *work)
363{
364 struct cfg80211_registered_device *rdev;
365
366 rdev = container_of(work, struct cfg80211_registered_device,
367 propagate_cac_done_wk);
368
369 rtnl_lock();
370
371 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->cac_done_chandef,
372 NL80211_DFS_AVAILABLE,
373 NL80211_RADAR_CAC_FINISHED);
374
375 rtnl_unlock();
376}
377
704232c2
JB
378/* exported functions */
379
1998d90a
BG
380struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
381 const char *requested_name)
704232c2 382{
73810b77 383 static atomic_t wiphy_counter = ATOMIC_INIT(0);
7623225f
JB
384
385 struct cfg80211_registered_device *rdev;
704232c2
JB
386 int alloc_size;
387
0b20633d
JB
388 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
389 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
390 WARN_ON(ops->connect && !ops->disconnect);
391 WARN_ON(ops->join_ibss && !ops->leave_ibss);
392 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
393 WARN_ON(ops->add_station && !ops->del_station);
394 WARN_ON(ops->add_mpath && !ops->del_mpath);
29cbe68c 395 WARN_ON(ops->join_mesh && !ops->leave_mesh);
de3bb771
OO
396 WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device);
397 WARN_ON(ops->start_ap && !ops->stop_ap);
398 WARN_ON(ops->join_ocb && !ops->leave_ocb);
399 WARN_ON(ops->suspend && !ops->resume);
400 WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop);
401 WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel);
402 WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch);
403 WARN_ON(ops->add_tx_ts && !ops->del_tx_ts);
41ade00f 404
79c97e97 405 alloc_size = sizeof(*rdev) + sizeof_priv;
704232c2 406
79c97e97
JB
407 rdev = kzalloc(alloc_size, GFP_KERNEL);
408 if (!rdev)
704232c2
JB
409 return NULL;
410
79c97e97 411 rdev->ops = ops;
704232c2 412
73810b77 413 rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
a4d73ee1 414
f4173766 415 if (unlikely(rdev->wiphy_idx < 0)) {
7623225f 416 /* ugh, wrapped! */
73810b77 417 atomic_dec(&wiphy_counter);
79c97e97 418 kfree(rdev);
704232c2
JB
419 return NULL;
420 }
704232c2 421
9b881963
JB
422 /* atomic_inc_return makes it start at 1, make it start at 0 */
423 rdev->wiphy_idx--;
424
7623225f 425 /* give it a proper name */
1998d90a
BG
426 if (requested_name && requested_name[0]) {
427 int rv;
428
429 rtnl_lock();
430 rv = cfg80211_dev_check_name(rdev, requested_name);
431
432 if (rv < 0) {
433 rtnl_unlock();
434 goto use_default_name;
435 }
436
437 rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name);
438 rtnl_unlock();
439 if (rv)
440 goto use_default_name;
441 } else {
59b179b4
JB
442 int rv;
443
1998d90a
BG
444use_default_name:
445 /* NOTE: This is *probably* safe w/out holding rtnl because of
446 * the restrictions on phy names. Probably this call could
447 * fail if some other part of the kernel (re)named a device
448 * phyX. But, might should add some locking and check return
449 * value, and use a different name if this one exists?
450 */
59b179b4
JB
451 rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
452 if (rv < 0) {
453 kfree(rdev);
454 return NULL;
455 }
1998d90a 456 }
7623225f 457
53873f13 458 INIT_LIST_HEAD(&rdev->wiphy.wdev_list);
37c73b5f
BG
459 INIT_LIST_HEAD(&rdev->beacon_registrations);
460 spin_lock_init(&rdev->beacon_registrations_lock);
79c97e97
JB
461 spin_lock_init(&rdev->bss_lock);
462 INIT_LIST_HEAD(&rdev->bss_list);
ca986ad9 463 INIT_LIST_HEAD(&rdev->sched_scan_req_list);
79c97e97 464 INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
33d8783c
JB
465 INIT_LIST_HEAD(&rdev->mlme_unreg);
466 spin_lock_init(&rdev->mlme_unreg_lock);
467 INIT_WORK(&rdev->mlme_unreg_wk, cfg80211_mlme_unreg_wk);
04f39047
SW
468 INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
469 cfg80211_dfs_channels_update_work);
3d23e349
JB
470#ifdef CONFIG_CFG80211_WEXT
471 rdev->wiphy.wext = &cfg80211_wext_handler;
472#endif
473
79c97e97
JB
474 device_initialize(&rdev->wiphy.dev);
475 rdev->wiphy.dev.class = &ieee80211_class;
476 rdev->wiphy.dev.platform_data = rdev;
9f0e1354 477 device_enable_async_suspend(&rdev->wiphy.dev);
79c97e97 478
78f22b6a 479 INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
93a1e86c 480 INIT_WORK(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);
b34939b9 481 INIT_WORK(&rdev->sched_scan_res_wk, cfg80211_sched_scan_results_wk);
89766727
VT
482 INIT_WORK(&rdev->propagate_radar_detect_wk,
483 cfg80211_propagate_radar_detect_wk);
484 INIT_WORK(&rdev->propagate_cac_done_wk, cfg80211_propagate_cac_done_wk);
78f22b6a 485
5be83de5
JB
486#ifdef CONFIG_CFG80211_DEFAULT_PS
487 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
488#endif
16cb9d42 489
463d0183
JB
490 wiphy_net_set(&rdev->wiphy, &init_net);
491
79c97e97
JB
492 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
493 rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
494 &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
495 &rdev->rfkill_ops, rdev);
496
497 if (!rdev->rfkill) {
498 kfree(rdev);
1f87f7d3
JB
499 return NULL;
500 }
501
79c97e97
JB
502 INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
503 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
504 INIT_WORK(&rdev->event_work, cfg80211_event_work);
1f87f7d3 505
ad002395
JB
506 init_waitqueue_head(&rdev->dev_wait);
507
b9a5f8ca
JM
508 /*
509 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
510 * Fragmentation and RTS threshold are disabled by default with the
511 * special -1 value.
512 */
79c97e97
JB
513 rdev->wiphy.retry_short = 7;
514 rdev->wiphy.retry_long = 4;
515 rdev->wiphy.frag_threshold = (u32) -1;
516 rdev->wiphy.rts_threshold = (u32) -1;
81077e82 517 rdev->wiphy.coverage_class = 0;
b9a5f8ca 518
9a774c78
AO
519 rdev->wiphy.max_num_csa_counters = 1;
520
3b06d277
AS
521 rdev->wiphy.max_sched_scan_plans = 1;
522 rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
523
79c97e97 524 return &rdev->wiphy;
704232c2 525}
1998d90a 526EXPORT_SYMBOL(wiphy_new_nm);
704232c2 527
7527a782
JB
528static int wiphy_verify_combinations(struct wiphy *wiphy)
529{
530 const struct ieee80211_iface_combination *c;
531 int i, j;
532
7527a782
JB
533 for (i = 0; i < wiphy->n_iface_combinations; i++) {
534 u32 cnt = 0;
535 u16 all_iftypes = 0;
536
537 c = &wiphy->iface_combinations[i];
538
11c4a075
SW
539 /*
540 * Combinations with just one interface aren't real,
541 * however we make an exception for DFS.
542 */
543 if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
7527a782
JB
544 return -EINVAL;
545
546 /* Need at least one channel */
547 if (WARN_ON(!c->num_different_channels))
548 return -EINVAL;
549
d4e50c59
MK
550 /*
551 * Put a sane limit on maximum number of different
552 * channels to simplify channel accounting code.
553 */
554 if (WARN_ON(c->num_different_channels >
555 CFG80211_MAX_NUM_DIFFERENT_CHANNELS))
556 return -EINVAL;
557
11c4a075
SW
558 /* DFS only works on one channel. */
559 if (WARN_ON(c->radar_detect_widths &&
560 (c->num_different_channels > 1)))
561 return -EINVAL;
562
7527a782
JB
563 if (WARN_ON(!c->n_limits))
564 return -EINVAL;
565
566 for (j = 0; j < c->n_limits; j++) {
567 u16 types = c->limits[j].types;
568
b6a55015 569 /* interface types shouldn't overlap */
7527a782
JB
570 if (WARN_ON(types & all_iftypes))
571 return -EINVAL;
572 all_iftypes |= types;
573
574 if (WARN_ON(!c->limits[j].max))
575 return -EINVAL;
576
577 /* Shouldn't list software iftypes in combinations! */
578 if (WARN_ON(wiphy->software_iftypes & types))
579 return -EINVAL;
580
98104fde
JB
581 /* Only a single P2P_DEVICE can be allowed */
582 if (WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
583 c->limits[j].max > 1))
584 return -EINVAL;
585
cb3b7d87
AB
586 /* Only a single NAN can be allowed */
587 if (WARN_ON(types & BIT(NL80211_IFTYPE_NAN) &&
588 c->limits[j].max > 1))
589 return -EINVAL;
590
56271da2
JB
591 /*
592 * This isn't well-defined right now. If you have an
593 * IBSS interface, then its beacon interval may change
594 * by joining other networks, and nothing prevents it
595 * from doing that.
596 * So technically we probably shouldn't even allow AP
597 * and IBSS in the same interface, but it seems that
598 * some drivers support that, possibly only with fixed
599 * beacon intervals for IBSS.
600 */
601 if (WARN_ON(types & BIT(NL80211_IFTYPE_ADHOC) &&
602 c->beacon_int_min_gcd)) {
603 return -EINVAL;
604 }
605
7527a782
JB
606 cnt += c->limits[j].max;
607 /*
608 * Don't advertise an unsupported type
609 * in a combination.
610 */
611 if (WARN_ON((wiphy->interface_modes & types) != types))
612 return -EINVAL;
613 }
614
8f205423
JB
615#ifndef CONFIG_WIRELESS_WDS
616 if (WARN_ON(all_iftypes & BIT(NL80211_IFTYPE_WDS)))
617 return -EINVAL;
618#endif
619
7527a782
JB
620 /* You can't even choose that many! */
621 if (WARN_ON(cnt < c->max_interfaces))
622 return -EINVAL;
623 }
624
625 return 0;
626}
627
704232c2
JB
628int wiphy_register(struct wiphy *wiphy)
629{
f26cbf40 630 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
704232c2 631 int res;
57fbcce3 632 enum nl80211_band band;
8318d78a
JB
633 struct ieee80211_supported_band *sband;
634 bool have_band = false;
635 int i;
f59ac048
LR
636 u16 ifmodes = wiphy->interface_modes;
637
dfb89c56 638#ifdef CONFIG_PM
964dc9e2
JB
639 if (WARN_ON(wiphy->wowlan &&
640 (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
641 !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
642 return -EINVAL;
643 if (WARN_ON(wiphy->wowlan &&
644 !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns &&
645 !wiphy->wowlan->tcp))
77dbbb13 646 return -EINVAL;
dfb89c56 647#endif
1057d35e
AN
648 if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
649 (!rdev->ops->tdls_channel_switch ||
650 !rdev->ops->tdls_cancel_channel_switch)))
651 return -EINVAL;
77dbbb13 652
cb3b7d87 653 if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
a442b761 654 (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
8585989d
LC
655 !rdev->ops->add_nan_func || !rdev->ops->del_nan_func ||
656 !(wiphy->nan_supported_bands & BIT(NL80211_BAND_2GHZ)))))
cb3b7d87
AB
657 return -EINVAL;
658
8f205423
JB
659#ifndef CONFIG_WIRELESS_WDS
660 if (WARN_ON(wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS)))
661 return -EINVAL;
662#endif
663
ad932f04
AN
664 /*
665 * if a wiphy has unsupported modes for regulatory channel enforcement,
666 * opt-out of enforcement checking
667 */
668 if (wiphy->interface_modes & ~(BIT(NL80211_IFTYPE_STATION) |
669 BIT(NL80211_IFTYPE_P2P_CLIENT) |
670 BIT(NL80211_IFTYPE_AP) |
671 BIT(NL80211_IFTYPE_P2P_GO) |
672 BIT(NL80211_IFTYPE_ADHOC) |
673 BIT(NL80211_IFTYPE_P2P_DEVICE) |
cb3b7d87 674 BIT(NL80211_IFTYPE_NAN) |
ad932f04
AN
675 BIT(NL80211_IFTYPE_AP_VLAN) |
676 BIT(NL80211_IFTYPE_MONITOR)))
677 wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF;
678
b0d7aa59
JD
679 if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
680 (wiphy->regulatory_flags &
681 (REGULATORY_CUSTOM_REG |
682 REGULATORY_STRICT_REG |
683 REGULATORY_COUNTRY_IE_FOLLOW_POWER |
684 REGULATORY_COUNTRY_IE_IGNORE))))
685 return -EINVAL;
686
be29b99a
AK
687 if (WARN_ON(wiphy->coalesce &&
688 (!wiphy->coalesce->n_rules ||
689 !wiphy->coalesce->n_patterns) &&
690 (!wiphy->coalesce->pattern_min_len ||
691 wiphy->coalesce->pattern_min_len >
692 wiphy->coalesce->pattern_max_len)))
693 return -EINVAL;
694
562a7480
JB
695 if (WARN_ON(wiphy->ap_sme_capa &&
696 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
697 return -EINVAL;
698
ef15aac6
JB
699 if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
700 return -EINVAL;
701
702 if (WARN_ON(wiphy->addresses &&
703 !is_zero_ether_addr(wiphy->perm_addr) &&
704 memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
705 ETH_ALEN)))
706 return -EINVAL;
707
77765eaf
VT
708 if (WARN_ON(wiphy->max_acl_mac_addrs &&
709 (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
710 !rdev->ops->set_mac_acl)))
711 return -EINVAL;
712
38de03d2
AS
713 /* assure only valid behaviours are flagged by driver
714 * hence subtract 2 as bit 0 is invalid.
715 */
716 if (WARN_ON(wiphy->bss_select_support &&
717 (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
718 return -EINVAL;
719
3a00df57
AS
720 if (WARN_ON(wiphy_ext_feature_isset(&rdev->wiphy,
721 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) &&
722 (!rdev->ops->set_pmk || !rdev->ops->del_pmk)))
723 return -EINVAL;
724
ef15aac6
JB
725 if (wiphy->addresses)
726 memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
727
f59ac048
LR
728 /* sanity check ifmodes */
729 WARN_ON(!ifmodes);
2e161f78 730 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
f59ac048
LR
731 if (WARN_ON(ifmodes != wiphy->interface_modes))
732 wiphy->interface_modes = ifmodes;
8318d78a 733
7527a782
JB
734 res = wiphy_verify_combinations(wiphy);
735 if (res)
736 return res;
737
8318d78a 738 /* sanity check supported bands/channels */
57fbcce3 739 for (band = 0; band < NUM_NL80211_BANDS; band++) {
8318d78a
JB
740 sband = wiphy->bands[band];
741 if (!sband)
742 continue;
743
744 sband->band = band;
3a0c52a6
VK
745 if (WARN_ON(!sband->n_channels))
746 return -EINVAL;
747 /*
8047d261 748 * on 60GHz band, there are no legacy rates, so
3a0c52a6
VK
749 * n_bitrates is 0
750 */
57fbcce3 751 if (WARN_ON(band != NL80211_BAND_60GHZ &&
3a0c52a6 752 !sband->n_bitrates))
881d948c
JB
753 return -EINVAL;
754
40db6c77
AK
755 /*
756 * Since cfg80211_disable_40mhz_24ghz is global, we can
757 * modify the sband's ht data even if the driver uses a
758 * global structure for that.
759 */
760 if (cfg80211_disable_40mhz_24ghz &&
57fbcce3 761 band == NL80211_BAND_2GHZ &&
40db6c77
AK
762 sband->ht_cap.ht_supported) {
763 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
764 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
765 }
766
881d948c
JB
767 /*
768 * Since we use a u32 for rate bitmaps in
769 * ieee80211_get_response_rate, we cannot
770 * have more than 32 legacy rates.
771 */
772 if (WARN_ON(sband->n_bitrates > 32))
8318d78a 773 return -EINVAL;
8318d78a
JB
774
775 for (i = 0; i < sband->n_channels; i++) {
776 sband->channels[i].orig_flags =
777 sband->channels[i].flags;
c4a9fafc 778 sband->channels[i].orig_mag = INT_MAX;
8318d78a
JB
779 sband->channels[i].orig_mpwr =
780 sband->channels[i].max_power;
781 sband->channels[i].band = band;
782 }
783
784 have_band = true;
785 }
786
787 if (!have_band) {
788 WARN_ON(1);
789 return -EINVAL;
790 }
791
dfb89c56 792#ifdef CONFIG_PM
964dc9e2
JB
793 if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
794 (!rdev->wiphy.wowlan->pattern_min_len ||
795 rdev->wiphy.wowlan->pattern_min_len >
796 rdev->wiphy.wowlan->pattern_max_len)))
797 return -EINVAL;
dfb89c56 798#endif
ff1b6e69 799
8318d78a
JB
800 /* check and set up bitrates */
801 ieee80211_set_bitrate_flags(wiphy);
802
00c3a6ed
JB
803 rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
804
aa5f66d5 805 rtnl_lock();
79c97e97 806 res = device_add(&rdev->wiphy.dev);
c3d34d5d 807 if (res) {
aa5f66d5 808 rtnl_unlock();
c3d34d5d
JL
809 return res;
810 }
1f87f7d3 811
2f0accc1 812 /* set up regulatory info */
57b5ce07 813 wiphy_regulatory_register(wiphy);
2f0accc1 814
5f2aa25e 815 list_add_rcu(&rdev->list, &cfg80211_rdev_list);
f5ea9120 816 cfg80211_rdev_list_generation++;
704232c2
JB
817
818 /* add to debugfs */
79c97e97
JB
819 rdev->wiphy.debugfsdir =
820 debugfs_create_dir(wiphy_name(&rdev->wiphy),
704232c2 821 ieee80211_debugfs_dir);
79c97e97
JB
822 if (IS_ERR(rdev->wiphy.debugfsdir))
823 rdev->wiphy.debugfsdir = NULL;
704232c2 824
a796dac9
TB
825 cfg80211_debugfs_rdev_add(rdev);
826 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
827
a2f73b6c 828 if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
73d54c9e
LR
829 struct regulatory_request request;
830
831 request.wiphy_idx = get_wiphy_idx(wiphy);
832 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
833 request.alpha2[0] = '9';
834 request.alpha2[1] = '9';
835
836 nl80211_send_reg_change_event(&request);
837 }
838
019ae3a9
KV
839 /* Check that nobody globally advertises any capabilities they do not
840 * advertise on all possible interface types.
841 */
842 if (wiphy->extended_capabilities_len &&
843 wiphy->num_iftype_ext_capab &&
844 wiphy->iftype_ext_capab) {
845 u8 supported_on_all, j;
846 const struct wiphy_iftype_ext_capab *capab;
847
848 capab = wiphy->iftype_ext_capab;
849 for (j = 0; j < wiphy->extended_capabilities_len; j++) {
850 if (capab[0].extended_capabilities_len > j)
851 supported_on_all =
852 capab[0].extended_capabilities[j];
853 else
854 supported_on_all = 0x00;
855 for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
856 if (j >= capab[i].extended_capabilities_len) {
857 supported_on_all = 0x00;
858 break;
859 }
860 supported_on_all &=
861 capab[i].extended_capabilities[j];
862 }
863 if (WARN_ON(wiphy->extended_capabilities[j] &
864 ~supported_on_all))
865 break;
866 }
867 }
868
ecb44335
SG
869 rdev->wiphy.registered = true;
870 rtnl_unlock();
aa5f66d5
JB
871
872 res = rfkill_register(rdev->rfkill);
873 if (res) {
874 rfkill_destroy(rdev->rfkill);
875 rdev->rfkill = NULL;
876 wiphy_unregister(&rdev->wiphy);
877 return res;
878 }
879
2f0accc1 880 return 0;
704232c2
JB
881}
882EXPORT_SYMBOL(wiphy_register);
883
1f87f7d3
JB
884void wiphy_rfkill_start_polling(struct wiphy *wiphy)
885{
f26cbf40 886 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1f87f7d3 887
79c97e97 888 if (!rdev->ops->rfkill_poll)
1f87f7d3 889 return;
79c97e97
JB
890 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
891 rfkill_resume_polling(rdev->rfkill);
1f87f7d3
JB
892}
893EXPORT_SYMBOL(wiphy_rfkill_start_polling);
894
895void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
896{
f26cbf40 897 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1f87f7d3 898
79c97e97 899 rfkill_pause_polling(rdev->rfkill);
1f87f7d3
JB
900}
901EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
902
704232c2
JB
903void wiphy_unregister(struct wiphy *wiphy)
904{
f26cbf40 905 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
704232c2 906
ad002395
JB
907 wait_event(rdev->dev_wait, ({
908 int __count;
5fe231e8 909 rtnl_lock();
ad002395 910 __count = rdev->opencount;
5fe231e8 911 rtnl_unlock();
c4f60846 912 __count == 0; }));
ad002395 913
aa5f66d5
JB
914 if (rdev->rfkill)
915 rfkill_unregister(rdev->rfkill);
256c90de 916
5fe231e8 917 rtnl_lock();
3bb20556 918 nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
5fe231e8
JB
919 rdev->wiphy.registered = false;
920
53873f13 921 WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
ad002395
JB
922
923 /*
924 * First remove the hardware from everywhere, this makes
925 * it impossible to find from userspace.
926 */
7bcfaf2f 927 debugfs_remove_recursive(rdev->wiphy.debugfsdir);
5f2aa25e
JB
928 list_del_rcu(&rdev->list);
929 synchronize_rcu();
f16bfc1c 930
bfead080
LR
931 /*
932 * If this device got a regulatory hint tell core its
933 * free to listen now to a new shiny device regulatory hint
934 */
935 wiphy_regulatory_deregister(wiphy);
3f2355cb 936
f5ea9120 937 cfg80211_rdev_list_generation++;
79c97e97 938 device_del(&rdev->wiphy.dev);
704232c2 939
5fe231e8 940 rtnl_unlock();
6682588a 941
36e6fea8 942 flush_work(&rdev->scan_done_wk);
6682588a 943 cancel_work_sync(&rdev->conn_work);
6682588a 944 flush_work(&rdev->event_work);
04f39047 945 cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
78f22b6a 946 flush_work(&rdev->destroy_work);
93a1e86c 947 flush_work(&rdev->sched_scan_stop_wk);
33d8783c 948 flush_work(&rdev->mlme_unreg_wk);
89766727
VT
949 flush_work(&rdev->propagate_radar_detect_wk);
950 flush_work(&rdev->propagate_cac_done_wk);
6d52563f 951
6abb9cb9
JB
952#ifdef CONFIG_PM
953 if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
e35e4d28 954 rdev_set_wakeup(rdev, false);
6abb9cb9 955#endif
6d52563f 956 cfg80211_rdev_free_wowlan(rdev);
be29b99a 957 cfg80211_rdev_free_coalesce(rdev);
704232c2
JB
958}
959EXPORT_SYMBOL(wiphy_unregister);
960
79c97e97 961void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
704232c2 962{
2a519311 963 struct cfg80211_internal_bss *scan, *tmp;
37c73b5f 964 struct cfg80211_beacon_registration *reg, *treg;
79c97e97 965 rfkill_destroy(rdev->rfkill);
37c73b5f
BG
966 list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
967 list_del(&reg->list);
968 kfree(reg);
969 }
79c97e97 970 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
5b112d3d 971 cfg80211_put_bss(&rdev->wiphy, &scan->pub);
79c97e97 972 kfree(rdev);
704232c2
JB
973}
974
975void wiphy_free(struct wiphy *wiphy)
976{
977 put_device(&wiphy->dev);
978}
979EXPORT_SYMBOL(wiphy_free);
980
1f87f7d3
JB
981void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
982{
f26cbf40 983 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1f87f7d3 984
79c97e97
JB
985 if (rfkill_set_hw_state(rdev->rfkill, blocked))
986 schedule_work(&rdev->rfkill_sync);
1f87f7d3
JB
987}
988EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
989
4a4b8169
AZ
990void cfg80211_cqm_config_free(struct wireless_dev *wdev)
991{
992 kfree(wdev->cqm_config);
993 wdev->cqm_config = NULL;
994}
995
98104fde
JB
996void cfg80211_unregister_wdev(struct wireless_dev *wdev)
997{
f26cbf40 998 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
98104fde
JB
999
1000 ASSERT_RTNL();
1001
1002 if (WARN_ON(wdev->netdev))
1003 return;
1004
7f8ed01e
DK
1005 nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1006
98104fde
JB
1007 list_del_rcu(&wdev->list);
1008 rdev->devlist_generation++;
1009
1010 switch (wdev->iftype) {
1011 case NL80211_IFTYPE_P2P_DEVICE:
33d8783c 1012 cfg80211_mlme_purge_registrations(wdev);
f9f47529 1013 cfg80211_stop_p2p_device(rdev, wdev);
98104fde 1014 break;
cb3b7d87
AB
1015 case NL80211_IFTYPE_NAN:
1016 cfg80211_stop_nan(rdev, wdev);
1017 break;
98104fde
JB
1018 default:
1019 WARN_ON_ONCE(1);
1020 break;
1021 }
4a4b8169
AZ
1022
1023 cfg80211_cqm_config_free(wdev);
98104fde
JB
1024}
1025EXPORT_SYMBOL(cfg80211_unregister_wdev);
1026
f1e3d556 1027static const struct device_type wiphy_type = {
053a93dd
MH
1028 .name = "wlan",
1029};
1030
dbbae26a
MK
1031void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1032 enum nl80211_iftype iftype, int num)
1033{
c5a7e582 1034 ASSERT_RTNL();
dbbae26a
MK
1035
1036 rdev->num_running_ifaces += num;
1037 if (iftype == NL80211_IFTYPE_MONITOR)
1038 rdev->num_running_monitor_ifaces += num;
dbbae26a
MK
1039}
1040
f04c2203
MK
1041void __cfg80211_leave(struct cfg80211_registered_device *rdev,
1042 struct wireless_dev *wdev)
81256969
SG
1043{
1044 struct net_device *dev = wdev->netdev;
ca986ad9 1045 struct cfg80211_sched_scan_request *pos, *tmp;
81256969 1046
24d584d7 1047 ASSERT_RTNL();
f04c2203 1048 ASSERT_WDEV_LOCK(wdev);
24d584d7 1049
81256969
SG
1050 switch (wdev->iftype) {
1051 case NL80211_IFTYPE_ADHOC:
f04c2203 1052 __cfg80211_leave_ibss(rdev, dev, true);
81256969
SG
1053 break;
1054 case NL80211_IFTYPE_P2P_CLIENT:
1055 case NL80211_IFTYPE_STATION:
ca986ad9
AVS
1056 list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1057 list) {
1058 if (dev == pos->dev)
1059 cfg80211_stop_sched_scan_req(rdev, pos, false);
1060 }
81256969 1061
81256969
SG
1062#ifdef CONFIG_CFG80211_WEXT
1063 kfree(wdev->wext.ie);
1064 wdev->wext.ie = NULL;
1065 wdev->wext.ie_len = 0;
1066 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1067#endif
83739b03
JB
1068 cfg80211_disconnect(rdev, dev,
1069 WLAN_REASON_DEAUTH_LEAVING, true);
81256969
SG
1070 break;
1071 case NL80211_IFTYPE_MESH_POINT:
f04c2203 1072 __cfg80211_leave_mesh(rdev, dev);
81256969
SG
1073 break;
1074 case NL80211_IFTYPE_AP:
74418ede 1075 case NL80211_IFTYPE_P2P_GO:
f04c2203 1076 __cfg80211_stop_ap(rdev, dev, true);
81256969 1077 break;
6e0bd6c3
RL
1078 case NL80211_IFTYPE_OCB:
1079 __cfg80211_leave_ocb(rdev, dev);
1080 break;
de4fcbad
JB
1081 case NL80211_IFTYPE_WDS:
1082 /* must be handled by mac80211/driver, has no APIs */
1083 break;
1084 case NL80211_IFTYPE_P2P_DEVICE:
cb3b7d87 1085 case NL80211_IFTYPE_NAN:
de4fcbad
JB
1086 /* cannot happen, has no netdev */
1087 break;
1088 case NL80211_IFTYPE_AP_VLAN:
1089 case NL80211_IFTYPE_MONITOR:
1090 /* nothing to do */
1091 break;
1092 case NL80211_IFTYPE_UNSPECIFIED:
1093 case NUM_NL80211_IFTYPES:
1094 /* invalid */
81256969
SG
1095 break;
1096 }
81256969
SG
1097}
1098
f04c2203
MK
1099void cfg80211_leave(struct cfg80211_registered_device *rdev,
1100 struct wireless_dev *wdev)
1101{
1102 wdev_lock(wdev);
1103 __cfg80211_leave(rdev, wdev);
1104 wdev_unlock(wdev);
1105}
1106
1107void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1108 gfp_t gfp)
1109{
1110 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1111 struct cfg80211_event *ev;
1112 unsigned long flags;
1113
1114 trace_cfg80211_stop_iface(wiphy, wdev);
1115
1116 ev = kzalloc(sizeof(*ev), gfp);
1117 if (!ev)
1118 return;
1119
1120 ev->type = EVENT_STOPPED;
1121
1122 spin_lock_irqsave(&wdev->event_lock, flags);
1123 list_add_tail(&ev->list, &wdev->event_list);
1124 spin_unlock_irqrestore(&wdev->event_lock, flags);
1125 queue_work(cfg80211_wq, &rdev->event_work);
1126}
1127EXPORT_SYMBOL(cfg80211_stop_iface);
1128
c4f60846 1129static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
351638e7 1130 unsigned long state, void *ptr)
704232c2 1131{
351638e7 1132 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2a783c13 1133 struct wireless_dev *wdev = dev->ieee80211_ptr;
704232c2 1134 struct cfg80211_registered_device *rdev;
ca986ad9 1135 struct cfg80211_sched_scan_request *pos, *tmp;
704232c2 1136
2a783c13 1137 if (!wdev)
1f87f7d3 1138 return NOTIFY_DONE;
704232c2 1139
f26cbf40 1140 rdev = wiphy_to_rdev(wdev->wiphy);
704232c2 1141
2a783c13 1142 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
60719ffd 1143
704232c2 1144 switch (state) {
053a93dd
MH
1145 case NETDEV_POST_INIT:
1146 SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1147 break;
704232c2 1148 case NETDEV_REGISTER:
0ff6ce7b
JB
1149 /*
1150 * NB: cannot take rdev->mtx here because this may be
1151 * called within code protected by it when interfaces
1152 * are added with nl80211.
1153 */
667503dd
JB
1154 mutex_init(&wdev->mtx);
1155 INIT_LIST_HEAD(&wdev->event_list);
1156 spin_lock_init(&wdev->event_lock);
2e161f78
JB
1157 INIT_LIST_HEAD(&wdev->mgmt_registrations);
1158 spin_lock_init(&wdev->mgmt_registrations_lock);
026331c4 1159
b6ecfd46
JB
1160 /*
1161 * We get here also when the interface changes network namespaces,
1162 * as it's registered into the new one, but we don't want it to
1163 * change ID in that case. Checking if the ID is already assigned
1164 * works, because 0 isn't considered a valid ID and the memory is
1165 * 0-initialized.
1166 */
1167 if (!wdev->identifier)
1168 wdev->identifier = ++rdev->wdev_id;
53873f13 1169 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
f5ea9120 1170 rdev->devlist_generation++;
463d0183
JB
1171 /* can only change netns with wiphy */
1172 dev->features |= NETIF_F_NETNS_LOCAL;
1173
704232c2
JB
1174 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
1175 "phy80211")) {
e9c0268f 1176 pr_err("failed to add phy80211 symlink to netdev!\n");
704232c2 1177 }
2a783c13 1178 wdev->netdev = dev;
3d23e349 1179#ifdef CONFIG_CFG80211_WEXT
2a783c13
JB
1180 wdev->wext.default_key = -1;
1181 wdev->wext.default_mgmt_key = -1;
f2129354 1182 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
ffb9eb3d
KV
1183#endif
1184
5be83de5 1185 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
ffb9eb3d 1186 wdev->ps = true;
5be83de5 1187 else
ffb9eb3d 1188 wdev->ps = false;
9043f3b8
JO
1189 /* allow mac80211 to determine the timeout */
1190 wdev->ps_timeout = -1;
ffb9eb3d 1191
ad4bb6f8 1192 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
074ac8df 1193 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
ad4bb6f8
JB
1194 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1195 dev->priv_flags |= IFF_DONT_BRIDGE;
896ff063 1196
bd2522b1
AZ
1197 INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1198
896ff063 1199 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
704232c2 1200 break;
04a773ad 1201 case NETDEV_GOING_DOWN:
81256969 1202 cfg80211_leave(rdev, wdev);
01a0ac41
JB
1203 break;
1204 case NETDEV_DOWN:
dbbae26a 1205 cfg80211_update_iface_num(rdev, wdev->iftype, -1);
a617302c
JB
1206 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
1207 if (WARN_ON(!rdev->scan_req->notified))
1d76250b 1208 rdev->scan_req->info.aborted = true;
f9d15d16 1209 ___cfg80211_scan_done(rdev, false);
a617302c 1210 }
5fe231e8 1211
ca986ad9
AVS
1212 list_for_each_entry_safe(pos, tmp,
1213 &rdev->sched_scan_req_list, list) {
1214 if (WARN_ON(pos && pos->dev == wdev->netdev))
1215 cfg80211_stop_sched_scan_req(rdev, pos, false);
5fe231e8
JB
1216 }
1217
1218 rdev->opencount--;
1219 wake_up(&rdev->dev_wait);
04a773ad
JB
1220 break;
1221 case NETDEV_UP:
4290cb4b 1222 cfg80211_update_iface_num(rdev, wdev->iftype, 1);
667503dd 1223 wdev_lock(wdev);
f2129354 1224 switch (wdev->iftype) {
29cbe68c 1225#ifdef CONFIG_CFG80211_WEXT
f2129354 1226 case NL80211_IFTYPE_ADHOC:
fffd0934 1227 cfg80211_ibss_wext_join(rdev, wdev);
04a773ad 1228 break;
f2129354 1229 case NL80211_IFTYPE_STATION:
fffd0934 1230 cfg80211_mgd_wext_connect(rdev, wdev);
f2129354 1231 break;
29cbe68c 1232#endif
c80d545d 1233#ifdef CONFIG_MAC80211_MESH
29cbe68c 1234 case NL80211_IFTYPE_MESH_POINT:
c80d545d
JC
1235 {
1236 /* backward compat code... */
1237 struct mesh_setup setup;
1238 memcpy(&setup, &default_mesh_setup,
1239 sizeof(setup));
1240 /* back compat only needed for mesh_id */
1241 setup.mesh_id = wdev->ssid;
1242 setup.mesh_id_len = wdev->mesh_id_up_len;
1243 if (wdev->mesh_id_up_len)
1244 __cfg80211_join_mesh(rdev, dev,
1245 &setup,
1246 &default_mesh_config);
1247 break;
1248 }
1249#endif
f2129354 1250 default:
04a773ad 1251 break;
f2129354 1252 }
667503dd 1253 wdev_unlock(wdev);
ad002395 1254 rdev->opencount++;
bf6a0579
JO
1255
1256 /*
1257 * Configure power management to the driver here so that its
1258 * correctly set also after interface type changes etc.
1259 */
5966f2dd
EP
1260 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1261 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
d4f29978
JB
1262 rdev->ops->set_power_mgmt &&
1263 rdev_set_power_mgmt(rdev, dev, wdev->ps,
1264 wdev->ps_timeout)) {
1265 /* assume this means it's off */
1266 wdev->ps = false;
1267 }
2a783c13 1268 break;
704232c2 1269 case NETDEV_UNREGISTER:
e40cbdac
JB
1270 /*
1271 * It is possible to get NETDEV_UNREGISTER
1272 * multiple times. To detect that, check
1273 * that the interface is still on the list
1274 * of registered interfaces, and only then
1275 * remove and clean it up.
1276 */
2a783c13 1277 if (!list_empty(&wdev->list)) {
7f8ed01e
DK
1278 nl80211_notify_iface(rdev, wdev,
1279 NL80211_CMD_DEL_INTERFACE);
704232c2 1280 sysfs_remove_link(&dev->dev.kobj, "phy80211");
5f2aa25e 1281 list_del_rcu(&wdev->list);
f5ea9120 1282 rdev->devlist_generation++;
2e161f78 1283 cfg80211_mlme_purge_registrations(wdev);
3d23e349 1284#ifdef CONFIG_CFG80211_WEXT
538c9eb8 1285 kzfree(wdev->wext.keys);
fffd0934 1286#endif
bd2522b1 1287 flush_work(&wdev->disconnect_wk);
4a4b8169 1288 cfg80211_cqm_config_free(wdev);
e40cbdac 1289 }
5f2aa25e
JB
1290 /*
1291 * synchronise (so that we won't find this netdev
1292 * from other code any more) and then clear the list
1293 * head so that the above code can safely check for
1294 * !list_empty() to avoid double-cleanup.
1295 */
1296 synchronize_rcu();
1297 INIT_LIST_HEAD(&wdev->list);
1f6fc43e
DD
1298 /*
1299 * Ensure that all events have been processed and
1300 * freed.
1301 */
1302 cfg80211_process_wdev_events(wdev);
f9bef3df
BG
1303
1304 if (WARN_ON(wdev->current_bss)) {
1305 cfg80211_unhold_bss(wdev->current_bss);
1306 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1307 wdev->current_bss = NULL;
1308 }
704232c2 1309 break;
1f87f7d3 1310 case NETDEV_PRE_UP:
0b20633d
JB
1311 if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
1312 return notifier_from_errno(-EOPNOTSUPP);
b6a55015
LC
1313 if (rfkill_blocked(rdev->rfkill))
1314 return notifier_from_errno(-ERFKILL);
1f87f7d3 1315 break;
6784c7db
ZG
1316 default:
1317 return NOTIFY_DONE;
704232c2
JB
1318 }
1319
cb150b9d
JB
1320 wireless_nlevent_flush();
1321
6784c7db 1322 return NOTIFY_OK;
704232c2
JB
1323}
1324
1325static struct notifier_block cfg80211_netdev_notifier = {
1326 .notifier_call = cfg80211_netdev_notifier_call,
1327};
1328
463d0183
JB
1329static void __net_exit cfg80211_pernet_exit(struct net *net)
1330{
1331 struct cfg80211_registered_device *rdev;
1332
1333 rtnl_lock();
463d0183
JB
1334 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1335 if (net_eq(wiphy_net(&rdev->wiphy), net))
1336 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1337 }
463d0183
JB
1338 rtnl_unlock();
1339}
1340
1341static struct pernet_operations cfg80211_pernet_ops = {
1342 .exit = cfg80211_pernet_exit,
1343};
1344
1345static int __init cfg80211_init(void)
704232c2 1346{
b2e1b302
LR
1347 int err;
1348
463d0183
JB
1349 err = register_pernet_device(&cfg80211_pernet_ops);
1350 if (err)
1351 goto out_fail_pernet;
1352
b2e1b302 1353 err = wiphy_sysfs_init();
704232c2
JB
1354 if (err)
1355 goto out_fail_sysfs;
1356
1357 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1358 if (err)
1359 goto out_fail_notifier;
1360
55682965
JB
1361 err = nl80211_init();
1362 if (err)
1363 goto out_fail_nl80211;
1364
704232c2
JB
1365 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1366
b2e1b302
LR
1367 err = regulatory_init();
1368 if (err)
1369 goto out_fail_reg;
1370
e4819013 1371 cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
f00f188f
WY
1372 if (!cfg80211_wq) {
1373 err = -ENOMEM;
e60d7443 1374 goto out_fail_wq;
f00f188f 1375 }
e60d7443 1376
704232c2
JB
1377 return 0;
1378
e60d7443
AB
1379out_fail_wq:
1380 regulatory_exit();
b2e1b302
LR
1381out_fail_reg:
1382 debugfs_remove(ieee80211_debugfs_dir);
81daf735 1383 nl80211_exit();
55682965
JB
1384out_fail_nl80211:
1385 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
704232c2
JB
1386out_fail_notifier:
1387 wiphy_sysfs_exit();
1388out_fail_sysfs:
463d0183
JB
1389 unregister_pernet_device(&cfg80211_pernet_ops);
1390out_fail_pernet:
704232c2
JB
1391 return err;
1392}
007f6c5e 1393fs_initcall(cfg80211_init);
704232c2 1394
f884e387 1395static void __exit cfg80211_exit(void)
704232c2
JB
1396{
1397 debugfs_remove(ieee80211_debugfs_dir);
55682965 1398 nl80211_exit();
704232c2
JB
1399 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1400 wiphy_sysfs_exit();
b2e1b302 1401 regulatory_exit();
463d0183 1402 unregister_pernet_device(&cfg80211_pernet_ops);
e60d7443 1403 destroy_workqueue(cfg80211_wq);
704232c2
JB
1404}
1405module_exit(cfg80211_exit);