Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / net / wireless / core.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
704232c2
JB
2/*
3 * This is the linux wireless configuration interface.
4 *
5f2aa25e 5 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
2740f0cf 6 * Copyright 2013-2014 Intel Mobile Communications GmbH
c4cbaf79 7 * Copyright 2015-2017 Intel Deutschland GmbH
db97bb04 8 * Copyright (C) 2018-2025 Intel Corporation
704232c2
JB
9 */
10
e9c0268f
JP
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
704232c2
JB
13#include <linux/if.h>
14#include <linux/module.h>
15#include <linux/err.h>
704232c2 16#include <linux/list.h>
5a0e3ad6 17#include <linux/slab.h>
704232c2
JB
18#include <linux/nl80211.h>
19#include <linux/debugfs.h>
20#include <linux/notifier.h>
21#include <linux/device.h>
16a832e7 22#include <linux/etherdevice.h>
1f87f7d3 23#include <linux/rtnetlink.h>
d43c36dc 24#include <linux/sched.h>
704232c2
JB
25#include <net/genetlink.h>
26#include <net/cfg80211.h>
55682965 27#include "nl80211.h"
704232c2
JB
28#include "core.h"
29#include "sysfs.h"
1ac61302 30#include "debugfs.h"
a9a11622 31#include "wext-compat.h"
e35e4d28 32#include "rdev-ops.h"
704232c2
JB
33
34/* name for sysfs, %d is appended */
35#define PHY_NAME "phy"
36
37MODULE_AUTHOR("Johannes Berg");
38MODULE_LICENSE("GPL");
39MODULE_DESCRIPTION("wireless configuration support");
fb4e1568 40MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
704232c2 41
5fe231e8 42/* RCU-protected (and RTNL for writers) */
79c97e97 43LIST_HEAD(cfg80211_rdev_list);
f5ea9120 44int cfg80211_rdev_list_generation;
a1794390 45
704232c2
JB
46/* for debugfs */
47static struct dentry *ieee80211_debugfs_dir;
48
e60d7443
AB
49/* for the cleanup, scan and event works */
50struct workqueue_struct *cfg80211_wq;
51
40db6c77
AK
52static bool cfg80211_disable_40mhz_24ghz;
53module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
54MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
55 "Disable 40MHz support in the 2.4GHz band");
56
79c97e97 57struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
55682965 58{
79c97e97 59 struct cfg80211_registered_device *result = NULL, *rdev;
55682965 60
5fe231e8 61 ASSERT_RTNL();
761cf7ec 62
7483a214 63 for_each_rdev(rdev) {
79c97e97
JB
64 if (rdev->wiphy_idx == wiphy_idx) {
65 result = rdev;
55682965
JB
66 break;
67 }
68 }
69
70 return result;
71}
72
806a9e39
LR
73int get_wiphy_idx(struct wiphy *wiphy)
74{
f26cbf40 75 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
f4173766 76
79c97e97 77 return rdev->wiphy_idx;
806a9e39
LR
78}
79
806a9e39
LR
80struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
81{
79c97e97 82 struct cfg80211_registered_device *rdev;
806a9e39 83
5fe231e8 84 ASSERT_RTNL();
806a9e39 85
79c97e97
JB
86 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
87 if (!rdev)
806a9e39 88 return NULL;
79c97e97 89 return &rdev->wiphy;
806a9e39
LR
90}
91
1998d90a
BG
92static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
93 const char *newname)
55682965 94{
79c97e97 95 struct cfg80211_registered_device *rdev2;
1998d90a 96 int wiphy_idx, taken = -1, digits;
55682965 97
5fe231e8 98 ASSERT_RTNL();
2940bb69 99
a7cfebcb
JB
100 if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN)
101 return -EINVAL;
102
7623225f
JB
103 /* prohibit calling the thing phy%d when %d is not its number */
104 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
105 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
106 /* count number of places needed to print wiphy_idx */
107 digits = 1;
108 while (wiphy_idx /= 10)
109 digits++;
110 /*
111 * deny the name if it is phy<idx> where <idx> is printed
112 * without leading zeroes. taken == strlen(newname) here
113 */
114 if (taken == strlen(PHY_NAME) + digits)
115 return -EINVAL;
116 }
117
1998d90a 118 /* Ensure another device does not already have this name. */
7483a214 119 for_each_rdev(rdev2)
1998d90a
BG
120 if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
121 return -EINVAL;
122
123 return 0;
124}
125
126int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
127 char *newname)
128{
129 int result;
130
131 ASSERT_RTNL();
4d45145b 132 lockdep_assert_wiphy(&rdev->wiphy);
7623225f 133
2940bb69 134 /* Ignore nop renames */
1998d90a 135 if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
4bbf4d56 136 return 0;
2940bb69 137
1998d90a
BG
138 result = cfg80211_dev_check_name(rdev, newname);
139 if (result < 0)
140 return result;
55682965 141
55682965
JB
142 result = device_rename(&rdev->wiphy.dev, newname);
143 if (result)
4bbf4d56 144 return result;
55682965 145
f7862dfe 146 debugfs_change_name(rdev->wiphy.debugfsdir, "%s", newname);
55682965 147
3bb20556 148 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
55682965 149
4bbf4d56 150 return 0;
55682965
JB
151}
152
463d0183
JB
153int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
154 struct net *net)
155{
156 struct wireless_dev *wdev;
157 int err = 0;
158
5be83de5 159 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
463d0183
JB
160 return -EOPNOTSUPP;
161
53873f13 162 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
ba22fb5b
JB
163 if (!wdev->netdev)
164 continue;
0c493da8 165 wdev->netdev->netns_immutable = false;
463d0183
JB
166 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
167 if (err)
168 break;
0c493da8 169 wdev->netdev->netns_immutable = true;
463d0183
JB
170 }
171
172 if (err) {
173 /* failed -- clean up to old netns */
174 net = wiphy_net(&rdev->wiphy);
175
53873f13
JB
176 list_for_each_entry_continue_reverse(wdev,
177 &rdev->wiphy.wdev_list,
463d0183 178 list) {
ba22fb5b
JB
179 if (!wdev->netdev)
180 continue;
0c493da8 181 wdev->netdev->netns_immutable = false;
463d0183
JB
182 err = dev_change_net_namespace(wdev->netdev, net,
183 "wlan%d");
184 WARN_ON(err);
0c493da8 185 wdev->netdev->netns_immutable = true;
463d0183 186 }
04600794
JB
187
188 return err;
463d0183
JB
189 }
190
f42d22d3
JB
191 guard(wiphy)(&rdev->wiphy);
192
c90b670b
MW
193 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
194 if (!wdev->netdev)
195 continue;
196 nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
197 }
4d45145b 198
c90b670b
MW
199 nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
200
463d0183
JB
201 wiphy_net_set(&rdev->wiphy, net);
202
04600794
JB
203 err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
204 WARN_ON(err);
205
c90b670b 206 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
4d45145b 207
c90b670b
MW
208 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
209 if (!wdev->netdev)
210 continue;
211 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
212 }
213
04600794 214 return 0;
463d0183
JB
215}
216
1f87f7d3
JB
217static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
218{
79c97e97 219 struct cfg80211_registered_device *rdev = data;
1f87f7d3 220
f42d22d3
JB
221 guard(wiphy)(&rdev->wiphy);
222
e35e4d28 223 rdev_rfkill_poll(rdev);
1f87f7d3
JB
224}
225
f9f47529
JB
226void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
227 struct wireless_dev *wdev)
228{
a05829a7 229 lockdep_assert_held(&rdev->wiphy.mtx);
f9f47529
JB
230
231 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
232 return;
233
73c7da3d 234 if (!wdev_running(wdev))
f9f47529
JB
235 return;
236
237 rdev_stop_p2p_device(rdev, wdev);
73c7da3d 238 wdev->is_running = false;
f9f47529
JB
239
240 rdev->opencount--;
241
a617302c 242 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
c8cb5b85
TM
243 if (WARN_ON(!rdev->scan_req->notified &&
244 (!rdev->int_scan_req ||
245 !rdev->int_scan_req->notified)))
1d76250b 246 rdev->scan_req->info.aborted = true;
f9d15d16 247 ___cfg80211_scan_done(rdev, false);
a617302c 248 }
f9f47529
JB
249}
250
cb3b7d87
AB
251void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
252 struct wireless_dev *wdev)
253{
a05829a7 254 lockdep_assert_held(&rdev->wiphy.mtx);
cb3b7d87
AB
255
256 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
257 return;
258
73c7da3d 259 if (!wdev_running(wdev))
cb3b7d87
AB
260 return;
261
262 rdev_stop_nan(rdev, wdev);
73c7da3d 263 wdev->is_running = false;
cb3b7d87
AB
264
265 rdev->opencount--;
266}
267
f6837ba8 268void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
1f87f7d3 269{
f6837ba8 270 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1f87f7d3
JB
271 struct wireless_dev *wdev;
272
f6837ba8 273 ASSERT_RTNL();
f9f47529 274
53873f13 275 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
98104fde 276 if (wdev->netdev) {
ba22fb5b 277 dev_close(wdev->netdev);
98104fde
JB
278 continue;
279 }
a05829a7 280
98104fde 281 /* otherwise, check iftype */
a05829a7 282
f42d22d3 283 guard(wiphy)(wiphy);
a05829a7 284
98104fde
JB
285 switch (wdev->iftype) {
286 case NL80211_IFTYPE_P2P_DEVICE:
f9f47529 287 cfg80211_stop_p2p_device(rdev, wdev);
98104fde 288 break;
cb3b7d87
AB
289 case NL80211_IFTYPE_NAN:
290 cfg80211_stop_nan(rdev, wdev);
291 break;
98104fde
JB
292 default:
293 break;
294 }
295 }
f6837ba8
JB
296}
297EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
1f87f7d3 298
f6837ba8
JB
299static int cfg80211_rfkill_set_block(void *data, bool blocked)
300{
301 struct cfg80211_registered_device *rdev = data;
302
303 if (!blocked)
304 return 0;
305
306 rtnl_lock();
307 cfg80211_shutdown_all_interfaces(&rdev->wiphy);
1f87f7d3
JB
308 rtnl_unlock();
309
310 return 0;
311}
312
3cfe91c4 313static void cfg80211_rfkill_block_work(struct work_struct *work)
1f87f7d3 314{
79c97e97 315 struct cfg80211_registered_device *rdev;
1f87f7d3 316
3cfe91c4
JB
317 rdev = container_of(work, struct cfg80211_registered_device,
318 rfkill_block);
319 cfg80211_rfkill_set_block(rdev, true);
1f87f7d3
JB
320}
321
667503dd
JB
322static void cfg80211_event_work(struct work_struct *work)
323{
324 struct cfg80211_registered_device *rdev;
667503dd
JB
325
326 rdev = container_of(work, struct cfg80211_registered_device,
327 event_work);
328
f42d22d3
JB
329 guard(wiphy)(&rdev->wiphy);
330
3d54d255 331 cfg80211_process_rdev_events(rdev);
667503dd
JB
332}
333
78f22b6a
JB
334void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
335{
ab81007a 336 struct wireless_dev *wdev, *tmp;
78f22b6a
JB
337
338 ASSERT_RTNL();
339
f0a6fd15 340 list_for_each_entry_safe(wdev, tmp, &rdev->wiphy.wdev_list, list) {
ea6b2098
JB
341 if (wdev->nl_owner_dead) {
342 if (wdev->netdev)
343 dev_close(wdev->netdev);
ea6b2098 344
f42d22d3
JB
345 guard(wiphy)(&rdev->wiphy);
346
ea6b2098 347 cfg80211_leave(rdev, wdev);
cdf0a0a8 348 cfg80211_remove_virtual_intf(rdev, wdev);
ea6b2098 349 }
78f22b6a 350 }
78f22b6a
JB
351}
352
353static void cfg80211_destroy_iface_wk(struct work_struct *work)
354{
355 struct cfg80211_registered_device *rdev;
356
357 rdev = container_of(work, struct cfg80211_registered_device,
358 destroy_work);
359
360 rtnl_lock();
361 cfg80211_destroy_ifaces(rdev);
362 rtnl_unlock();
363}
364
c88d7178
JB
365static void cfg80211_sched_scan_stop_wk(struct wiphy *wiphy,
366 struct wiphy_work *work)
93a1e86c
JR
367{
368 struct cfg80211_registered_device *rdev;
ca986ad9 369 struct cfg80211_sched_scan_request *req, *tmp;
93a1e86c
JR
370
371 rdev = container_of(work, struct cfg80211_registered_device,
372 sched_scan_stop_wk);
373
ca986ad9
AVS
374 list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
375 if (req->nl_owner_dead)
376 cfg80211_stop_sched_scan_req(rdev, req, false);
377 }
93a1e86c
JR
378}
379
89766727
VT
380static void cfg80211_propagate_radar_detect_wk(struct work_struct *work)
381{
382 struct cfg80211_registered_device *rdev;
383
384 rdev = container_of(work, struct cfg80211_registered_device,
385 propagate_radar_detect_wk);
386
387 rtnl_lock();
388
389 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->radar_chandef,
390 NL80211_DFS_UNAVAILABLE,
391 NL80211_RADAR_DETECTED);
392
393 rtnl_unlock();
394}
395
396static void cfg80211_propagate_cac_done_wk(struct work_struct *work)
397{
398 struct cfg80211_registered_device *rdev;
399
400 rdev = container_of(work, struct cfg80211_registered_device,
401 propagate_cac_done_wk);
402
403 rtnl_lock();
404
405 regulatory_propagate_dfs_state(&rdev->wiphy, &rdev->cac_done_chandef,
406 NL80211_DFS_AVAILABLE,
407 NL80211_RADAR_CAC_FINISHED);
408
409 rtnl_unlock();
410}
411
a3ee4dc8
JB
412static void cfg80211_wiphy_work(struct work_struct *work)
413{
414 struct cfg80211_registered_device *rdev;
415 struct wiphy_work *wk;
416
417 rdev = container_of(work, struct cfg80211_registered_device, wiphy_work);
418
eb745c7c
JB
419 trace_wiphy_work_worker_start(&rdev->wiphy);
420
f42d22d3 421 guard(wiphy)(&rdev->wiphy);
a3ee4dc8 422 if (rdev->suspended)
f42d22d3 423 return;
a3ee4dc8
JB
424
425 spin_lock_irq(&rdev->wiphy_work_lock);
426 wk = list_first_entry_or_null(&rdev->wiphy_work_list,
427 struct wiphy_work, entry);
428 if (wk) {
429 list_del_init(&wk->entry);
430 if (!list_empty(&rdev->wiphy_work_list))
e296c95e 431 queue_work(system_unbound_wq, work);
a3ee4dc8
JB
432 spin_unlock_irq(&rdev->wiphy_work_lock);
433
eb745c7c 434 trace_wiphy_work_run(&rdev->wiphy, wk);
a3ee4dc8
JB
435 wk->func(&rdev->wiphy, wk);
436 } else {
437 spin_unlock_irq(&rdev->wiphy_work_lock);
438 }
a3ee4dc8
JB
439}
440
704232c2
JB
441/* exported functions */
442
1998d90a
BG
443struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
444 const char *requested_name)
704232c2 445{
73810b77 446 static atomic_t wiphy_counter = ATOMIC_INIT(0);
7623225f
JB
447
448 struct cfg80211_registered_device *rdev;
704232c2
JB
449 int alloc_size;
450
0b20633d
JB
451 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
452 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
453 WARN_ON(ops->connect && !ops->disconnect);
454 WARN_ON(ops->join_ibss && !ops->leave_ibss);
455 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
456 WARN_ON(ops->add_station && !ops->del_station);
457 WARN_ON(ops->add_mpath && !ops->del_mpath);
29cbe68c 458 WARN_ON(ops->join_mesh && !ops->leave_mesh);
de3bb771
OO
459 WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device);
460 WARN_ON(ops->start_ap && !ops->stop_ap);
461 WARN_ON(ops->join_ocb && !ops->leave_ocb);
462 WARN_ON(ops->suspend && !ops->resume);
463 WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop);
464 WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel);
465 WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch);
466 WARN_ON(ops->add_tx_ts && !ops->del_tx_ts);
41ade00f 467
79c97e97 468 alloc_size = sizeof(*rdev) + sizeof_priv;
704232c2 469
79c97e97
JB
470 rdev = kzalloc(alloc_size, GFP_KERNEL);
471 if (!rdev)
704232c2
JB
472 return NULL;
473
79c97e97 474 rdev->ops = ops;
704232c2 475
73810b77 476 rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
a4d73ee1 477
f4173766 478 if (unlikely(rdev->wiphy_idx < 0)) {
7623225f 479 /* ugh, wrapped! */
73810b77 480 atomic_dec(&wiphy_counter);
79c97e97 481 kfree(rdev);
704232c2
JB
482 return NULL;
483 }
704232c2 484
9b881963
JB
485 /* atomic_inc_return makes it start at 1, make it start at 0 */
486 rdev->wiphy_idx--;
487
7623225f 488 /* give it a proper name */
1998d90a
BG
489 if (requested_name && requested_name[0]) {
490 int rv;
491
492 rtnl_lock();
493 rv = cfg80211_dev_check_name(rdev, requested_name);
494
495 if (rv < 0) {
496 rtnl_unlock();
497 goto use_default_name;
498 }
499
500 rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name);
501 rtnl_unlock();
502 if (rv)
503 goto use_default_name;
504 } else {
59b179b4
JB
505 int rv;
506
1998d90a
BG
507use_default_name:
508 /* NOTE: This is *probably* safe w/out holding rtnl because of
509 * the restrictions on phy names. Probably this call could
510 * fail if some other part of the kernel (re)named a device
511 * phyX. But, might should add some locking and check return
512 * value, and use a different name if this one exists?
513 */
59b179b4
JB
514 rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
515 if (rv < 0) {
516 kfree(rdev);
517 return NULL;
518 }
1998d90a 519 }
7623225f 520
a05829a7 521 mutex_init(&rdev->wiphy.mtx);
53873f13 522 INIT_LIST_HEAD(&rdev->wiphy.wdev_list);
37c73b5f
BG
523 INIT_LIST_HEAD(&rdev->beacon_registrations);
524 spin_lock_init(&rdev->beacon_registrations_lock);
79c97e97
JB
525 spin_lock_init(&rdev->bss_lock);
526 INIT_LIST_HEAD(&rdev->bss_list);
ca986ad9 527 INIT_LIST_HEAD(&rdev->sched_scan_req_list);
fe0af9fe 528 wiphy_work_init(&rdev->scan_done_wk, __cfg80211_scan_done);
04f39047
SW
529 INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
530 cfg80211_dfs_channels_update_work);
3d23e349
JB
531#ifdef CONFIG_CFG80211_WEXT
532 rdev->wiphy.wext = &cfg80211_wext_handler;
533#endif
534
79c97e97
JB
535 device_initialize(&rdev->wiphy.dev);
536 rdev->wiphy.dev.class = &ieee80211_class;
537 rdev->wiphy.dev.platform_data = rdev;
9f0e1354 538 device_enable_async_suspend(&rdev->wiphy.dev);
79c97e97 539
78f22b6a 540 INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
c88d7178 541 wiphy_work_init(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);
b34939b9 542 INIT_WORK(&rdev->sched_scan_res_wk, cfg80211_sched_scan_results_wk);
89766727
VT
543 INIT_WORK(&rdev->propagate_radar_detect_wk,
544 cfg80211_propagate_radar_detect_wk);
545 INIT_WORK(&rdev->propagate_cac_done_wk, cfg80211_propagate_cac_done_wk);
79ea1e12
JB
546 INIT_WORK(&rdev->mgmt_registrations_update_wk,
547 cfg80211_mgmt_registrations_update_wk);
09b1d5dc 548 spin_lock_init(&rdev->mgmt_registrations_lock);
fc88dee8
EAD
549 INIT_WORK(&rdev->wiphy_work, cfg80211_wiphy_work);
550 INIT_LIST_HEAD(&rdev->wiphy_work_list);
551 spin_lock_init(&rdev->wiphy_work_lock);
78f22b6a 552
5be83de5
JB
553#ifdef CONFIG_CFG80211_DEFAULT_PS
554 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
555#endif
16cb9d42 556
463d0183
JB
557 wiphy_net_set(&rdev->wiphy, &init_net);
558
79c97e97 559 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
358ae888
EG
560 rdev->wiphy.rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
561 &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
562 &rdev->rfkill_ops, rdev);
79c97e97 563
358ae888 564 if (!rdev->wiphy.rfkill) {
4f488fbc 565 wiphy_free(&rdev->wiphy);
1f87f7d3
JB
566 return NULL;
567 }
568
3cfe91c4 569 INIT_WORK(&rdev->rfkill_block, cfg80211_rfkill_block_work);
79c97e97
JB
570 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
571 INIT_WORK(&rdev->event_work, cfg80211_event_work);
a95bfb87
LB
572 INIT_WORK(&rdev->background_cac_abort_wk,
573 cfg80211_background_cac_abort_wk);
574 INIT_DELAYED_WORK(&rdev->background_cac_done_wk,
575 cfg80211_background_cac_done_wk);
1f87f7d3 576
ad002395
JB
577 init_waitqueue_head(&rdev->dev_wait);
578
b9a5f8ca
JM
579 /*
580 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
581 * Fragmentation and RTS threshold are disabled by default with the
582 * special -1 value.
583 */
79c97e97
JB
584 rdev->wiphy.retry_short = 7;
585 rdev->wiphy.retry_long = 4;
586 rdev->wiphy.frag_threshold = (u32) -1;
587 rdev->wiphy.rts_threshold = (u32) -1;
81077e82 588 rdev->wiphy.coverage_class = 0;
b9a5f8ca 589
9a774c78
AO
590 rdev->wiphy.max_num_csa_counters = 1;
591
3b06d277
AS
592 rdev->wiphy.max_sched_scan_plans = 1;
593 rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
594
79c97e97 595 return &rdev->wiphy;
704232c2 596}
1998d90a 597EXPORT_SYMBOL(wiphy_new_nm);
704232c2 598
bd9813d1
KP
599static
600int wiphy_verify_iface_combinations(struct wiphy *wiphy,
601 const struct ieee80211_iface_combination *iface_comb,
602 int n_iface_comb,
603 bool combined_radio)
7527a782
JB
604{
605 const struct ieee80211_iface_combination *c;
606 int i, j;
607
bd9813d1 608 for (i = 0; i < n_iface_comb; i++) {
7527a782
JB
609 u32 cnt = 0;
610 u16 all_iftypes = 0;
611
bd9813d1 612 c = &iface_comb[i];
7527a782 613
11c4a075
SW
614 /*
615 * Combinations with just one interface aren't real,
616 * however we make an exception for DFS.
617 */
618 if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
7527a782
JB
619 return -EINVAL;
620
621 /* Need at least one channel */
622 if (WARN_ON(!c->num_different_channels))
623 return -EINVAL;
624
bd9813d1
KP
625 /* DFS only works on one channel. Avoid this check
626 * for multi-radio global combination, since it hold
627 * the capabilities of all radio combinations.
628 */
629 if (!combined_radio &&
630 WARN_ON(c->radar_detect_widths &&
631 c->num_different_channels > 1))
11c4a075
SW
632 return -EINVAL;
633
7527a782
JB
634 if (WARN_ON(!c->n_limits))
635 return -EINVAL;
636
637 for (j = 0; j < c->n_limits; j++) {
638 u16 types = c->limits[j].types;
639
b6a55015 640 /* interface types shouldn't overlap */
7527a782
JB
641 if (WARN_ON(types & all_iftypes))
642 return -EINVAL;
643 all_iftypes |= types;
644
645 if (WARN_ON(!c->limits[j].max))
646 return -EINVAL;
647
648 /* Shouldn't list software iftypes in combinations! */
649 if (WARN_ON(wiphy->software_iftypes & types))
650 return -EINVAL;
651
bd9813d1
KP
652 /* Only a single P2P_DEVICE can be allowed, avoid this
653 * check for multi-radio global combination, since it
654 * hold the capabilities of all radio combinations.
655 */
656 if (!combined_radio &&
657 WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
98104fde
JB
658 c->limits[j].max > 1))
659 return -EINVAL;
660
bd9813d1
KP
661 /* Only a single NAN can be allowed, avoid this
662 * check for multi-radio global combination, since it
663 * hold the capabilities of all radio combinations.
664 */
665 if (!combined_radio &&
666 WARN_ON(types & BIT(NL80211_IFTYPE_NAN) &&
cb3b7d87
AB
667 c->limits[j].max > 1))
668 return -EINVAL;
669
56271da2
JB
670 /*
671 * This isn't well-defined right now. If you have an
672 * IBSS interface, then its beacon interval may change
673 * by joining other networks, and nothing prevents it
674 * from doing that.
675 * So technically we probably shouldn't even allow AP
676 * and IBSS in the same interface, but it seems that
677 * some drivers support that, possibly only with fixed
678 * beacon intervals for IBSS.
679 */
680 if (WARN_ON(types & BIT(NL80211_IFTYPE_ADHOC) &&
681 c->beacon_int_min_gcd)) {
682 return -EINVAL;
683 }
684
7527a782
JB
685 cnt += c->limits[j].max;
686 /*
687 * Don't advertise an unsupported type
688 * in a combination.
689 */
690 if (WARN_ON((wiphy->interface_modes & types) != types))
691 return -EINVAL;
692 }
693
8f205423
JB
694 if (WARN_ON(all_iftypes & BIT(NL80211_IFTYPE_WDS)))
695 return -EINVAL;
8f205423 696
7527a782
JB
697 /* You can't even choose that many! */
698 if (WARN_ON(cnt < c->max_interfaces))
699 return -EINVAL;
700 }
701
702 return 0;
703}
704
bd9813d1
KP
705static int wiphy_verify_combinations(struct wiphy *wiphy)
706{
707 int i, ret;
708 bool combined_radio = false;
709
710 if (wiphy->n_radio) {
711 for (i = 0; i < wiphy->n_radio; i++) {
712 const struct wiphy_radio *radio = &wiphy->radio[i];
713
714 ret = wiphy_verify_iface_combinations(wiphy,
715 radio->iface_combinations,
716 radio->n_iface_combinations,
717 false);
718 if (ret)
719 return ret;
720 }
721
722 combined_radio = true;
723 }
724
725 ret = wiphy_verify_iface_combinations(wiphy,
726 wiphy->iface_combinations,
727 wiphy->n_iface_combinations,
728 combined_radio);
729
730 return ret;
731}
732
704232c2
JB
733int wiphy_register(struct wiphy *wiphy)
734{
f26cbf40 735 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
704232c2 736 int res;
57fbcce3 737 enum nl80211_band band;
8318d78a
JB
738 struct ieee80211_supported_band *sband;
739 bool have_band = false;
740 int i;
f59ac048
LR
741 u16 ifmodes = wiphy->interface_modes;
742
dfb89c56 743#ifdef CONFIG_PM
964dc9e2
JB
744 if (WARN_ON(wiphy->wowlan &&
745 (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
746 !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
747 return -EINVAL;
748 if (WARN_ON(wiphy->wowlan &&
749 !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns &&
750 !wiphy->wowlan->tcp))
77dbbb13 751 return -EINVAL;
dfb89c56 752#endif
1057d35e
AN
753 if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
754 (!rdev->ops->tdls_channel_switch ||
755 !rdev->ops->tdls_cancel_channel_switch)))
756 return -EINVAL;
77dbbb13 757
cb3b7d87 758 if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
a442b761 759 (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
8585989d
LC
760 !rdev->ops->add_nan_func || !rdev->ops->del_nan_func ||
761 !(wiphy->nan_supported_bands & BIT(NL80211_BAND_2GHZ)))))
cb3b7d87
AB
762 return -EINVAL;
763
8f205423
JB
764 if (WARN_ON(wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS)))
765 return -EINVAL;
8f205423 766
9bb7e0f2
JB
767 if (WARN_ON(wiphy->pmsr_capa && !wiphy->pmsr_capa->ftm.supported))
768 return -EINVAL;
769
770 if (wiphy->pmsr_capa && wiphy->pmsr_capa->ftm.supported) {
771 if (WARN_ON(!wiphy->pmsr_capa->ftm.asap &&
772 !wiphy->pmsr_capa->ftm.non_asap))
773 return -EINVAL;
774 if (WARN_ON(!wiphy->pmsr_capa->ftm.preambles ||
775 !wiphy->pmsr_capa->ftm.bandwidths))
776 return -EINVAL;
777 if (WARN_ON(wiphy->pmsr_capa->ftm.preambles &
778 ~(BIT(NL80211_PREAMBLE_LEGACY) |
779 BIT(NL80211_PREAMBLE_HT) |
780 BIT(NL80211_PREAMBLE_VHT) |
efb5520d 781 BIT(NL80211_PREAMBLE_HE) |
9bb7e0f2
JB
782 BIT(NL80211_PREAMBLE_DMG))))
783 return -EINVAL;
efb5520d
AS
784 if (WARN_ON((wiphy->pmsr_capa->ftm.trigger_based ||
785 wiphy->pmsr_capa->ftm.non_trigger_based) &&
786 !(wiphy->pmsr_capa->ftm.preambles &
787 BIT(NL80211_PREAMBLE_HE))))
788 return -EINVAL;
9bb7e0f2
JB
789 if (WARN_ON(wiphy->pmsr_capa->ftm.bandwidths &
790 ~(BIT(NL80211_CHAN_WIDTH_20_NOHT) |
791 BIT(NL80211_CHAN_WIDTH_20) |
792 BIT(NL80211_CHAN_WIDTH_40) |
793 BIT(NL80211_CHAN_WIDTH_80) |
794 BIT(NL80211_CHAN_WIDTH_80P80) |
795 BIT(NL80211_CHAN_WIDTH_160) |
db97bb04 796 BIT(NL80211_CHAN_WIDTH_320) |
9bb7e0f2
JB
797 BIT(NL80211_CHAN_WIDTH_5) |
798 BIT(NL80211_CHAN_WIDTH_10))))
799 return -EINVAL;
800 }
801
b0d7aa59
JD
802 if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
803 (wiphy->regulatory_flags &
804 (REGULATORY_CUSTOM_REG |
805 REGULATORY_STRICT_REG |
806 REGULATORY_COUNTRY_IE_FOLLOW_POWER |
807 REGULATORY_COUNTRY_IE_IGNORE))))
808 return -EINVAL;
809
be29b99a
AK
810 if (WARN_ON(wiphy->coalesce &&
811 (!wiphy->coalesce->n_rules ||
812 !wiphy->coalesce->n_patterns) &&
813 (!wiphy->coalesce->pattern_min_len ||
814 wiphy->coalesce->pattern_min_len >
815 wiphy->coalesce->pattern_max_len)))
816 return -EINVAL;
817
562a7480
JB
818 if (WARN_ON(wiphy->ap_sme_capa &&
819 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
820 return -EINVAL;
821
ef15aac6
JB
822 if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
823 return -EINVAL;
824
825 if (WARN_ON(wiphy->addresses &&
826 !is_zero_ether_addr(wiphy->perm_addr) &&
827 memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
828 ETH_ALEN)))
829 return -EINVAL;
830
77765eaf
VT
831 if (WARN_ON(wiphy->max_acl_mac_addrs &&
832 (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
833 !rdev->ops->set_mac_acl)))
834 return -EINVAL;
835
38de03d2
AS
836 /* assure only valid behaviours are flagged by driver
837 * hence subtract 2 as bit 0 is invalid.
838 */
839 if (WARN_ON(wiphy->bss_select_support &&
840 (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
841 return -EINVAL;
842
3a00df57
AS
843 if (WARN_ON(wiphy_ext_feature_isset(&rdev->wiphy,
844 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) &&
845 (!rdev->ops->set_pmk || !rdev->ops->del_pmk)))
846 return -EINVAL;
847
7f9a3e15
VK
848 if (WARN_ON(!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
849 rdev->ops->update_connect_params))
850 return -EINVAL;
851
ef15aac6
JB
852 if (wiphy->addresses)
853 memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
854
f59ac048
LR
855 /* sanity check ifmodes */
856 WARN_ON(!ifmodes);
2e161f78 857 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
f59ac048
LR
858 if (WARN_ON(ifmodes != wiphy->interface_modes))
859 wiphy->interface_modes = ifmodes;
8318d78a 860
7527a782
JB
861 res = wiphy_verify_combinations(wiphy);
862 if (res)
863 return res;
864
8318d78a 865 /* sanity check supported bands/channels */
57fbcce3 866 for (band = 0; band < NUM_NL80211_BANDS; band++) {
e8c18412 867 const struct ieee80211_sband_iftype_data *iftd;
c4cbaf79 868 u16 types = 0;
f4381365 869 bool have_he = false;
c4cbaf79 870
8318d78a
JB
871 sband = wiphy->bands[band];
872 if (!sband)
873 continue;
874
875 sband->band = band;
3a0c52a6
VK
876 if (WARN_ON(!sband->n_channels))
877 return -EINVAL;
878 /*
df78a0c0 879 * on 60GHz or sub-1Ghz band, there are no legacy rates, so
3a0c52a6
VK
880 * n_bitrates is 0
881 */
df78a0c0
TP
882 if (WARN_ON((band != NL80211_BAND_60GHZ &&
883 band != NL80211_BAND_S1GHZ) &&
3a0c52a6 884 !sband->n_bitrates))
881d948c
JB
885 return -EINVAL;
886
461ce35d
JB
887 if (WARN_ON(band == NL80211_BAND_6GHZ &&
888 (sband->ht_cap.ht_supported ||
889 sband->vht_cap.vht_supported)))
890 return -EINVAL;
891
40db6c77
AK
892 /*
893 * Since cfg80211_disable_40mhz_24ghz is global, we can
894 * modify the sband's ht data even if the driver uses a
895 * global structure for that.
896 */
897 if (cfg80211_disable_40mhz_24ghz &&
57fbcce3 898 band == NL80211_BAND_2GHZ &&
40db6c77
AK
899 sband->ht_cap.ht_supported) {
900 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
901 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
902 }
903
881d948c
JB
904 /*
905 * Since we use a u32 for rate bitmaps in
906 * ieee80211_get_response_rate, we cannot
907 * have more than 32 legacy rates.
908 */
909 if (WARN_ON(sband->n_bitrates > 32))
8318d78a 910 return -EINVAL;
8318d78a
JB
911
912 for (i = 0; i < sband->n_channels; i++) {
913 sband->channels[i].orig_flags =
914 sband->channels[i].flags;
c4a9fafc 915 sband->channels[i].orig_mag = INT_MAX;
8318d78a
JB
916 sband->channels[i].orig_mpwr =
917 sband->channels[i].max_power;
918 sband->channels[i].band = band;
be689f68
JB
919
920 if (WARN_ON(sband->channels[i].freq_offset >= 1000))
921 return -EINVAL;
8318d78a
JB
922 }
923
e8c18412 924 for_each_sband_iftype_data(sband, i, iftd) {
ea5cba26
JB
925 bool has_ap, has_non_ap;
926 u32 ap_bits = BIT(NL80211_IFTYPE_AP) |
927 BIT(NL80211_IFTYPE_P2P_GO);
c4cbaf79 928
c4cbaf79
LC
929 if (WARN_ON(!iftd->types_mask))
930 return -EINVAL;
931 if (WARN_ON(types & iftd->types_mask))
932 return -EINVAL;
933
934 /* at least one piece of information must be present */
935 if (WARN_ON(!iftd->he_cap.has_he))
936 return -EINVAL;
937
938 types |= iftd->types_mask;
f4381365
JB
939
940 if (i == 0)
941 have_he = iftd->he_cap.has_he;
942 else
943 have_he = have_he &&
944 iftd->he_cap.has_he;
ea5cba26
JB
945
946 has_ap = iftd->types_mask & ap_bits;
947 has_non_ap = iftd->types_mask & ~ap_bits;
948
949 /*
950 * For EHT 20 MHz STA, the capabilities format differs
951 * but to simplify, don't check 20 MHz but rather check
952 * only if AP and non-AP were mentioned at the same time,
953 * reject if so.
954 */
955 if (WARN_ON(iftd->eht_cap.has_eht &&
956 has_ap && has_non_ap))
957 return -EINVAL;
c4cbaf79
LC
958 }
959
f4381365
JB
960 if (WARN_ON(!have_he && band == NL80211_BAND_6GHZ))
961 return -EINVAL;
962
8318d78a
JB
963 have_band = true;
964 }
965
966 if (!have_band) {
967 WARN_ON(1);
968 return -EINVAL;
969 }
970
901bb989
JB
971 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
972 /*
973 * Validate we have a policy (can be explicitly set to
974 * VENDOR_CMD_RAW_DATA which is non-NULL) and also that
975 * we have at least one of doit/dumpit.
976 */
977 if (WARN_ON(!rdev->wiphy.vendor_commands[i].policy))
978 return -EINVAL;
979 if (WARN_ON(!rdev->wiphy.vendor_commands[i].doit &&
980 !rdev->wiphy.vendor_commands[i].dumpit))
981 return -EINVAL;
982 }
983
dfb89c56 984#ifdef CONFIG_PM
964dc9e2
JB
985 if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
986 (!rdev->wiphy.wowlan->pattern_min_len ||
987 rdev->wiphy.wowlan->pattern_min_len >
988 rdev->wiphy.wowlan->pattern_max_len)))
989 return -EINVAL;
dfb89c56 990#endif
ff1b6e69 991
ecad3b0b
VJ
992 if (!wiphy->max_num_akm_suites)
993 wiphy->max_num_akm_suites = NL80211_MAX_NR_AKM_SUITES;
994 else if (wiphy->max_num_akm_suites < NL80211_MAX_NR_AKM_SUITES ||
995 wiphy->max_num_akm_suites > CFG80211_MAX_NUM_AKM_SUITES)
996 return -EINVAL;
997
8318d78a
JB
998 /* check and set up bitrates */
999 ieee80211_set_bitrate_flags(wiphy);
1000
00c3a6ed
JB
1001 rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
1002
aa5f66d5 1003 rtnl_lock();
4d45145b 1004 wiphy_lock(&rdev->wiphy);
79c97e97 1005 res = device_add(&rdev->wiphy.dev);
c3d34d5d 1006 if (res) {
4d45145b 1007 wiphy_unlock(&rdev->wiphy);
aa5f66d5 1008 rtnl_unlock();
c3d34d5d
JL
1009 return res;
1010 }
1f87f7d3 1011
5f2aa25e 1012 list_add_rcu(&rdev->list, &cfg80211_rdev_list);
f5ea9120 1013 cfg80211_rdev_list_generation++;
704232c2
JB
1014
1015 /* add to debugfs */
d82574a8
GKH
1016 rdev->wiphy.debugfsdir = debugfs_create_dir(wiphy_name(&rdev->wiphy),
1017 ieee80211_debugfs_dir);
704232c2 1018
a796dac9
TB
1019 cfg80211_debugfs_rdev_add(rdev);
1020 nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
4d45145b 1021 wiphy_unlock(&rdev->wiphy);
a796dac9 1022
1b7b3ac8
MK
1023 /* set up regulatory info */
1024 wiphy_regulatory_register(wiphy);
1025
a2f73b6c 1026 if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
73d54c9e
LR
1027 struct regulatory_request request;
1028
1029 request.wiphy_idx = get_wiphy_idx(wiphy);
1030 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
1031 request.alpha2[0] = '9';
1032 request.alpha2[1] = '9';
1033
1034 nl80211_send_reg_change_event(&request);
1035 }
1036
019ae3a9
KV
1037 /* Check that nobody globally advertises any capabilities they do not
1038 * advertise on all possible interface types.
1039 */
1040 if (wiphy->extended_capabilities_len &&
1041 wiphy->num_iftype_ext_capab &&
1042 wiphy->iftype_ext_capab) {
1043 u8 supported_on_all, j;
1044 const struct wiphy_iftype_ext_capab *capab;
1045
1046 capab = wiphy->iftype_ext_capab;
1047 for (j = 0; j < wiphy->extended_capabilities_len; j++) {
1048 if (capab[0].extended_capabilities_len > j)
1049 supported_on_all =
1050 capab[0].extended_capabilities[j];
1051 else
1052 supported_on_all = 0x00;
1053 for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
1054 if (j >= capab[i].extended_capabilities_len) {
1055 supported_on_all = 0x00;
1056 break;
1057 }
1058 supported_on_all &=
1059 capab[i].extended_capabilities[j];
1060 }
1061 if (WARN_ON(wiphy->extended_capabilities[j] &
1062 ~supported_on_all))
1063 break;
1064 }
1065 }
1066
ecb44335
SG
1067 rdev->wiphy.registered = true;
1068 rtnl_unlock();
aa5f66d5 1069
358ae888 1070 res = rfkill_register(rdev->wiphy.rfkill);
aa5f66d5 1071 if (res) {
358ae888
EG
1072 rfkill_destroy(rdev->wiphy.rfkill);
1073 rdev->wiphy.rfkill = NULL;
aa5f66d5
JB
1074 wiphy_unregister(&rdev->wiphy);
1075 return res;
1076 }
1077
2f0accc1 1078 return 0;
704232c2
JB
1079}
1080EXPORT_SYMBOL(wiphy_register);
1081
1f87f7d3
JB
1082void wiphy_rfkill_start_polling(struct wiphy *wiphy)
1083{
f26cbf40 1084 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1f87f7d3 1085
79c97e97 1086 if (!rdev->ops->rfkill_poll)
1f87f7d3 1087 return;
79c97e97 1088 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
358ae888 1089 rfkill_resume_polling(wiphy->rfkill);
1f87f7d3
JB
1090}
1091EXPORT_SYMBOL(wiphy_rfkill_start_polling);
1092
56cfb8ce
JB
1093void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev,
1094 struct wiphy_work *end)
a3ee4dc8
JB
1095{
1096 unsigned int runaway_limit = 100;
1097 unsigned long flags;
1098
1099 lockdep_assert_held(&rdev->wiphy.mtx);
1100
1101 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1102 while (!list_empty(&rdev->wiphy_work_list)) {
1103 struct wiphy_work *wk;
1104
1105 wk = list_first_entry(&rdev->wiphy_work_list,
1106 struct wiphy_work, entry);
1107 list_del_init(&wk->entry);
1108 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1109
eb745c7c 1110 trace_wiphy_work_run(&rdev->wiphy, wk);
a3ee4dc8
JB
1111 wk->func(&rdev->wiphy, wk);
1112
1113 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
56cfb8ce
JB
1114
1115 if (wk == end)
1116 break;
1117
a3ee4dc8
JB
1118 if (WARN_ON(--runaway_limit == 0))
1119 INIT_LIST_HEAD(&rdev->wiphy_work_list);
1120 }
1121 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1122}
1123
704232c2
JB
1124void wiphy_unregister(struct wiphy *wiphy)
1125{
f26cbf40 1126 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
704232c2 1127
ad002395
JB
1128 wait_event(rdev->dev_wait, ({
1129 int __count;
a05829a7 1130 wiphy_lock(&rdev->wiphy);
ad002395 1131 __count = rdev->opencount;
a05829a7 1132 wiphy_unlock(&rdev->wiphy);
c4f60846 1133 __count == 0; }));
ad002395 1134
358ae888
EG
1135 if (rdev->wiphy.rfkill)
1136 rfkill_unregister(rdev->wiphy.rfkill);
256c90de 1137
5fe231e8 1138 rtnl_lock();
a05829a7 1139 wiphy_lock(&rdev->wiphy);
3bb20556 1140 nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
5fe231e8
JB
1141 rdev->wiphy.registered = false;
1142
53873f13 1143 WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
ad002395
JB
1144
1145 /*
1146 * First remove the hardware from everywhere, this makes
1147 * it impossible to find from userspace.
1148 */
7bcfaf2f 1149 debugfs_remove_recursive(rdev->wiphy.debugfsdir);
5f2aa25e
JB
1150 list_del_rcu(&rdev->list);
1151 synchronize_rcu();
f16bfc1c 1152
bfead080
LR
1153 /*
1154 * If this device got a regulatory hint tell core its
1155 * free to listen now to a new shiny device regulatory hint
1156 */
1157 wiphy_regulatory_deregister(wiphy);
3f2355cb 1158
f5ea9120 1159 cfg80211_rdev_list_generation++;
79c97e97 1160 device_del(&rdev->wiphy.dev);
704232c2 1161
a993df0f
JB
1162#ifdef CONFIG_PM
1163 if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
1164 rdev_set_wakeup(rdev, false);
1165#endif
a3ee4dc8
JB
1166
1167 /* surely nothing is reachable now, clean up work */
56cfb8ce 1168 cfg80211_process_wiphy_works(rdev, NULL);
a05829a7 1169 wiphy_unlock(&rdev->wiphy);
5fe231e8 1170 rtnl_unlock();
6682588a 1171
a3ee4dc8
JB
1172 /* this has nothing to do now but make sure it's gone */
1173 cancel_work_sync(&rdev->wiphy_work);
1174
6682588a 1175 cancel_work_sync(&rdev->conn_work);
6682588a 1176 flush_work(&rdev->event_work);
04f39047 1177 cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
a95bfb87 1178 cancel_delayed_work_sync(&rdev->background_cac_done_wk);
78f22b6a 1179 flush_work(&rdev->destroy_work);
89766727
VT
1180 flush_work(&rdev->propagate_radar_detect_wk);
1181 flush_work(&rdev->propagate_cac_done_wk);
79ea1e12 1182 flush_work(&rdev->mgmt_registrations_update_wk);
a95bfb87 1183 flush_work(&rdev->background_cac_abort_wk);
6d52563f 1184
6d52563f 1185 cfg80211_rdev_free_wowlan(rdev);
8526f8c8
JB
1186 cfg80211_free_coalesce(rdev->coalesce);
1187 rdev->coalesce = NULL;
704232c2
JB
1188}
1189EXPORT_SYMBOL(wiphy_unregister);
1190
79c97e97 1191void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
704232c2 1192{
2a519311 1193 struct cfg80211_internal_bss *scan, *tmp;
37c73b5f 1194 struct cfg80211_beacon_registration *reg, *treg;
72d52047
MK
1195 unsigned long flags;
1196
1197 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1198 WARN_ON(!list_empty(&rdev->wiphy_work_list));
1199 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1200 cancel_work_sync(&rdev->wiphy_work);
1201
358ae888 1202 rfkill_destroy(rdev->wiphy.rfkill);
37c73b5f
BG
1203 list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
1204 list_del(&reg->list);
1205 kfree(reg);
1206 }
79c97e97 1207 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
5b112d3d 1208 cfg80211_put_bss(&rdev->wiphy, &scan->pub);
a05829a7 1209 mutex_destroy(&rdev->wiphy.mtx);
e53e9828
JB
1210
1211 /*
1212 * The 'regd' can only be non-NULL if we never finished
1213 * initializing the wiphy and thus never went through the
1214 * unregister path - e.g. in failure scenarios. Thus, it
1215 * cannot have been visible to anyone if non-NULL, so we
1216 * can just free it here.
1217 */
1218 kfree(rcu_dereference_raw(rdev->wiphy.regd));
1219
79c97e97 1220 kfree(rdev);
704232c2
JB
1221}
1222
1223void wiphy_free(struct wiphy *wiphy)
1224{
1225 put_device(&wiphy->dev);
1226}
1227EXPORT_SYMBOL(wiphy_free);
1228
6f779a66
EG
1229void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
1230 enum rfkill_hard_block_reasons reason)
1f87f7d3 1231{
f26cbf40 1232 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1f87f7d3 1233
358ae888 1234 if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason))
3cfe91c4 1235 schedule_work(&rdev->rfkill_block);
1f87f7d3 1236}
6f779a66 1237EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
1f87f7d3 1238
2fe8ef10
JB
1239static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
1240 bool unregister_netdev)
98104fde 1241{
f26cbf40 1242 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
37c20b2e 1243 struct cfg80211_cqm_config *cqm_config;
7b0a0e3c 1244 unsigned int link_id;
98104fde
JB
1245
1246 ASSERT_RTNL();
a05829a7 1247 lockdep_assert_held(&rdev->wiphy.mtx);
98104fde 1248
7f8ed01e
DK
1249 nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1250
2fe8ef10
JB
1251 wdev->registered = false;
1252
1253 if (wdev->netdev) {
1254 sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211");
1255 if (unregister_netdev)
1256 unregister_netdevice(wdev->netdev);
1257 }
1258
98104fde 1259 list_del_rcu(&wdev->list);
2fe8ef10 1260 synchronize_net();
98104fde
JB
1261 rdev->devlist_generation++;
1262
85dd3da4
JB
1263 cfg80211_mlme_purge_registrations(wdev);
1264
98104fde
JB
1265 switch (wdev->iftype) {
1266 case NL80211_IFTYPE_P2P_DEVICE:
f9f47529 1267 cfg80211_stop_p2p_device(rdev, wdev);
98104fde 1268 break;
cb3b7d87
AB
1269 case NL80211_IFTYPE_NAN:
1270 cfg80211_stop_nan(rdev, wdev);
1271 break;
98104fde 1272 default:
98104fde
JB
1273 break;
1274 }
4a4b8169 1275
85dd3da4 1276#ifdef CONFIG_CFG80211_WEXT
453431a5 1277 kfree_sensitive(wdev->wext.keys);
56cb31e1 1278 wdev->wext.keys = NULL;
85dd3da4 1279#endif
37c20b2e
JB
1280 wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work);
1281 /* deleted from the list, so can't be found from nl80211 any more */
1282 cqm_config = rcu_access_pointer(wdev->cqm_config);
1283 kfree_rcu(cqm_config, rcu_head);
d5fee261 1284 RCU_INIT_POINTER(wdev->cqm_config, NULL);
2fe8ef10
JB
1285
1286 /*
1287 * Ensure that all events have been processed and
1288 * freed.
1289 */
1290 cfg80211_process_wdev_events(wdev);
1291
7b0a0e3c
JB
1292 if (wdev->iftype == NL80211_IFTYPE_STATION ||
1293 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1294 for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
1295 struct cfg80211_internal_bss *curbss;
1296
1297 curbss = wdev->links[link_id].client.current_bss;
1298
1299 if (WARN_ON(curbss)) {
1300 cfg80211_unhold_bss(curbss);
1301 cfg80211_put_bss(wdev->wiphy, &curbss->pub);
1302 wdev->links[link_id].client.current_bss = NULL;
1303 }
1304 }
2fe8ef10 1305 }
7b0a0e3c
JB
1306
1307 wdev->connected = false;
98104fde 1308}
85dd3da4
JB
1309
1310void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1311{
2fe8ef10 1312 _cfg80211_unregister_wdev(wdev, true);
85dd3da4 1313}
98104fde
JB
1314EXPORT_SYMBOL(cfg80211_unregister_wdev);
1315
f1e3d556 1316static const struct device_type wiphy_type = {
053a93dd
MH
1317 .name = "wlan",
1318};
1319
dbbae26a
MK
1320void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1321 enum nl80211_iftype iftype, int num)
1322{
a05829a7 1323 lockdep_assert_held(&rdev->wiphy.mtx);
dbbae26a
MK
1324
1325 rdev->num_running_ifaces += num;
1326 if (iftype == NL80211_IFTYPE_MONITOR)
1327 rdev->num_running_monitor_ifaces += num;
dbbae26a
MK
1328}
1329
076fc877
JB
1330void cfg80211_leave(struct cfg80211_registered_device *rdev,
1331 struct wireless_dev *wdev)
81256969
SG
1332{
1333 struct net_device *dev = wdev->netdev;
ca986ad9 1334 struct cfg80211_sched_scan_request *pos, *tmp;
81256969 1335
a05829a7 1336 lockdep_assert_held(&rdev->wiphy.mtx);
24d584d7 1337
9bb7e0f2
JB
1338 cfg80211_pmsr_wdev_down(wdev);
1339
a95bfb87 1340 cfg80211_stop_background_radar_detection(wdev);
bc2dfc02 1341
81256969
SG
1342 switch (wdev->iftype) {
1343 case NL80211_IFTYPE_ADHOC:
076fc877 1344 cfg80211_leave_ibss(rdev, dev, true);
81256969
SG
1345 break;
1346 case NL80211_IFTYPE_P2P_CLIENT:
1347 case NL80211_IFTYPE_STATION:
ca986ad9
AVS
1348 list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1349 list) {
1350 if (dev == pos->dev)
1351 cfg80211_stop_sched_scan_req(rdev, pos, false);
1352 }
81256969 1353
81256969
SG
1354#ifdef CONFIG_CFG80211_WEXT
1355 kfree(wdev->wext.ie);
1356 wdev->wext.ie = NULL;
1357 wdev->wext.ie_len = 0;
1358 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1359#endif
83739b03
JB
1360 cfg80211_disconnect(rdev, dev,
1361 WLAN_REASON_DEAUTH_LEAVING, true);
81256969
SG
1362 break;
1363 case NL80211_IFTYPE_MESH_POINT:
076fc877 1364 cfg80211_leave_mesh(rdev, dev);
81256969
SG
1365 break;
1366 case NL80211_IFTYPE_AP:
74418ede 1367 case NL80211_IFTYPE_P2P_GO:
076fc877 1368 cfg80211_stop_ap(rdev, dev, -1, true);
81256969 1369 break;
6e0bd6c3 1370 case NL80211_IFTYPE_OCB:
076fc877 1371 cfg80211_leave_ocb(rdev, dev);
6e0bd6c3 1372 break;
de4fcbad 1373 case NL80211_IFTYPE_P2P_DEVICE:
cb3b7d87 1374 case NL80211_IFTYPE_NAN:
de4fcbad
JB
1375 /* cannot happen, has no netdev */
1376 break;
1377 case NL80211_IFTYPE_AP_VLAN:
1378 case NL80211_IFTYPE_MONITOR:
1379 /* nothing to do */
1380 break;
1381 case NL80211_IFTYPE_UNSPECIFIED:
e7e0517c 1382 case NL80211_IFTYPE_WDS:
de4fcbad
JB
1383 case NUM_NL80211_IFTYPES:
1384 /* invalid */
81256969
SG
1385 break;
1386 }
81256969
SG
1387}
1388
f04c2203
MK
1389void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1390 gfp_t gfp)
1391{
1392 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1393 struct cfg80211_event *ev;
1394 unsigned long flags;
1395
1396 trace_cfg80211_stop_iface(wiphy, wdev);
1397
1398 ev = kzalloc(sizeof(*ev), gfp);
1399 if (!ev)
1400 return;
1401
1402 ev->type = EVENT_STOPPED;
1403
1404 spin_lock_irqsave(&wdev->event_lock, flags);
1405 list_add_tail(&ev->list, &wdev->event_list);
1406 spin_unlock_irqrestore(&wdev->event_lock, flags);
1407 queue_work(cfg80211_wq, &rdev->event_work);
1408}
1409EXPORT_SYMBOL(cfg80211_stop_iface);
1410
9bdaf3b9 1411void cfg80211_init_wdev(struct wireless_dev *wdev)
e4d4216e 1412{
e4d4216e
JB
1413 INIT_LIST_HEAD(&wdev->event_list);
1414 spin_lock_init(&wdev->event_lock);
1415 INIT_LIST_HEAD(&wdev->mgmt_registrations);
9bb7e0f2
JB
1416 INIT_LIST_HEAD(&wdev->pmsr_list);
1417 spin_lock_init(&wdev->pmsr_lock);
1418 INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
e4d4216e 1419
9bdaf3b9
JB
1420#ifdef CONFIG_CFG80211_WEXT
1421 wdev->wext.default_key = -1;
1422 wdev->wext.default_mgmt_key = -1;
1423 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1424#endif
1425
37c20b2e
JB
1426 wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work);
1427
9bdaf3b9
JB
1428 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1429 wdev->ps = true;
1430 else
1431 wdev->ps = false;
1432 /* allow mac80211 to determine the timeout */
1433 wdev->ps_timeout = -1;
1434
3607798a
FF
1435 wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
1436
9bdaf3b9
JB
1437 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1438 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1439 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1440 wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
1441
1442 INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1443}
1444
1445void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
1446 struct wireless_dev *wdev)
1447{
a05829a7
JB
1448 ASSERT_RTNL();
1449 lockdep_assert_held(&rdev->wiphy.mtx);
1450
e4d4216e
JB
1451 /*
1452 * We get here also when the interface changes network namespaces,
1453 * as it's registered into the new one, but we don't want it to
1454 * change ID in that case. Checking if the ID is already assigned
1455 * works, because 0 isn't considered a valid ID and the memory is
1456 * 0-initialized.
1457 */
1458 if (!wdev->identifier)
1459 wdev->identifier = ++rdev->wdev_id;
1460 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1461 rdev->devlist_generation++;
2fe8ef10 1462 wdev->registered = true;
ec8f170b 1463
43076c1e
JB
1464 if (wdev->netdev &&
1465 sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj,
1466 "phy80211"))
1467 pr_err("failed to add phy80211 symlink to netdev!\n");
1468
ec8f170b 1469 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
e4d4216e
JB
1470}
1471
2fe8ef10
JB
1472int cfg80211_register_netdevice(struct net_device *dev)
1473{
1474 struct wireless_dev *wdev = dev->ieee80211_ptr;
1475 struct cfg80211_registered_device *rdev;
1476 int ret;
1477
1478 ASSERT_RTNL();
1479
1480 if (WARN_ON(!wdev))
1481 return -EINVAL;
1482
1483 rdev = wiphy_to_rdev(wdev->wiphy);
1484
1485 lockdep_assert_held(&rdev->wiphy.mtx);
1486
1487 /* we'll take care of this */
1488 wdev->registered = true;
40c575d1 1489 wdev->registering = true;
2fe8ef10
JB
1490 ret = register_netdevice(dev);
1491 if (ret)
1492 goto out;
1493
2fe8ef10
JB
1494 cfg80211_register_wdev(rdev, wdev);
1495 ret = 0;
1496out:
40c575d1 1497 wdev->registering = false;
2fe8ef10
JB
1498 if (ret)
1499 wdev->registered = false;
1500 return ret;
1501}
1502EXPORT_SYMBOL(cfg80211_register_netdevice);
1503
c4f60846 1504static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
351638e7 1505 unsigned long state, void *ptr)
704232c2 1506{
351638e7 1507 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2a783c13 1508 struct wireless_dev *wdev = dev->ieee80211_ptr;
704232c2 1509 struct cfg80211_registered_device *rdev;
ca986ad9 1510 struct cfg80211_sched_scan_request *pos, *tmp;
704232c2 1511
2a783c13 1512 if (!wdev)
1f87f7d3 1513 return NOTIFY_DONE;
704232c2 1514
f26cbf40 1515 rdev = wiphy_to_rdev(wdev->wiphy);
704232c2 1516
2a783c13 1517 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
60719ffd 1518
704232c2 1519 switch (state) {
053a93dd
MH
1520 case NETDEV_POST_INIT:
1521 SET_NETDEV_DEVTYPE(dev, &wiphy_type);
9bdaf3b9
JB
1522 wdev->netdev = dev;
1523 /* can only change netns with wiphy */
0c493da8 1524 dev->netns_immutable = true;
9bdaf3b9
JB
1525
1526 cfg80211_init_wdev(wdev);
053a93dd 1527 break;
704232c2 1528 case NETDEV_REGISTER:
a05829a7 1529 if (!wdev->registered) {
f42d22d3
JB
1530 guard(wiphy)(&rdev->wiphy);
1531
2fe8ef10 1532 cfg80211_register_wdev(rdev, wdev);
a05829a7 1533 }
2fe8ef10
JB
1534 break;
1535 case NETDEV_UNREGISTER:
0ff6ce7b 1536 /*
2fe8ef10
JB
1537 * It is possible to get NETDEV_UNREGISTER multiple times,
1538 * so check wdev->registered.
0ff6ce7b 1539 */
40c575d1 1540 if (wdev->registered && !wdev->registering) {
f42d22d3
JB
1541 guard(wiphy)(&rdev->wiphy);
1542
2fe8ef10 1543 _cfg80211_unregister_wdev(wdev, false);
a05829a7 1544 }
704232c2 1545 break;
04a773ad 1546 case NETDEV_GOING_DOWN:
f42d22d3
JB
1547 scoped_guard(wiphy, &rdev->wiphy) {
1548 cfg80211_leave(rdev, wdev);
1549 cfg80211_remove_links(wdev);
1550 }
e9da6df7
JB
1551 /* since we just did cfg80211_leave() nothing to do there */
1552 cancel_work_sync(&wdev->disconnect_wk);
0dcb84ed 1553 cancel_work_sync(&wdev->pmsr_free_wk);
01a0ac41
JB
1554 break;
1555 case NETDEV_DOWN:
a05829a7 1556 wiphy_lock(&rdev->wiphy);
dbbae26a 1557 cfg80211_update_iface_num(rdev, wdev->iftype, -1);
a617302c 1558 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
c8cb5b85
TM
1559 if (WARN_ON(!rdev->scan_req->notified &&
1560 (!rdev->int_scan_req ||
1561 !rdev->int_scan_req->notified)))
1d76250b 1562 rdev->scan_req->info.aborted = true;
f9d15d16 1563 ___cfg80211_scan_done(rdev, false);
a617302c 1564 }
5fe231e8 1565
ca986ad9
AVS
1566 list_for_each_entry_safe(pos, tmp,
1567 &rdev->sched_scan_req_list, list) {
d25d062f 1568 if (WARN_ON(pos->dev == wdev->netdev))
ca986ad9 1569 cfg80211_stop_sched_scan_req(rdev, pos, false);
5fe231e8
JB
1570 }
1571
1572 rdev->opencount--;
a05829a7 1573 wiphy_unlock(&rdev->wiphy);
5fe231e8 1574 wake_up(&rdev->dev_wait);
04a773ad
JB
1575 break;
1576 case NETDEV_UP:
a05829a7 1577 wiphy_lock(&rdev->wiphy);
4290cb4b 1578 cfg80211_update_iface_num(rdev, wdev->iftype, 1);
f2129354 1579 switch (wdev->iftype) {
29cbe68c 1580#ifdef CONFIG_CFG80211_WEXT
f2129354 1581 case NL80211_IFTYPE_ADHOC:
fffd0934 1582 cfg80211_ibss_wext_join(rdev, wdev);
04a773ad 1583 break;
f2129354 1584 case NL80211_IFTYPE_STATION:
fffd0934 1585 cfg80211_mgd_wext_connect(rdev, wdev);
f2129354 1586 break;
29cbe68c 1587#endif
c80d545d 1588#ifdef CONFIG_MAC80211_MESH
29cbe68c 1589 case NL80211_IFTYPE_MESH_POINT:
c80d545d
JC
1590 {
1591 /* backward compat code... */
1592 struct mesh_setup setup;
1593 memcpy(&setup, &default_mesh_setup,
1594 sizeof(setup));
1595 /* back compat only needed for mesh_id */
7b0a0e3c
JB
1596 setup.mesh_id = wdev->u.mesh.id;
1597 setup.mesh_id_len = wdev->u.mesh.id_up_len;
1598 if (wdev->u.mesh.id_up_len)
c80d545d
JC
1599 __cfg80211_join_mesh(rdev, dev,
1600 &setup,
1601 &default_mesh_config);
1602 break;
1603 }
1604#endif
f2129354 1605 default:
04a773ad 1606 break;
f2129354 1607 }
ad002395 1608 rdev->opencount++;
bf6a0579
JO
1609
1610 /*
1611 * Configure power management to the driver here so that its
1612 * correctly set also after interface type changes etc.
1613 */
5966f2dd
EP
1614 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1615 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
d4f29978
JB
1616 rdev->ops->set_power_mgmt &&
1617 rdev_set_power_mgmt(rdev, dev, wdev->ps,
1618 wdev->ps_timeout)) {
1619 /* assume this means it's off */
1620 wdev->ps = false;
1621 }
a05829a7 1622 wiphy_unlock(&rdev->wiphy);
2a783c13 1623 break;
1f87f7d3 1624 case NETDEV_PRE_UP:
e6f40511
MP
1625 if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
1626 wdev->use_4addr, 0))
0b20633d 1627 return notifier_from_errno(-EOPNOTSUPP);
33d915d9 1628
358ae888 1629 if (rfkill_blocked(rdev->wiphy.rfkill))
b6a55015 1630 return notifier_from_errno(-ERFKILL);
1f87f7d3 1631 break;
6784c7db
ZG
1632 default:
1633 return NOTIFY_DONE;
704232c2
JB
1634 }
1635
cb150b9d
JB
1636 wireless_nlevent_flush();
1637
6784c7db 1638 return NOTIFY_OK;
704232c2
JB
1639}
1640
1641static struct notifier_block cfg80211_netdev_notifier = {
1642 .notifier_call = cfg80211_netdev_notifier_call,
1643};
1644
463d0183
JB
1645static void __net_exit cfg80211_pernet_exit(struct net *net)
1646{
1647 struct cfg80211_registered_device *rdev;
1648
1649 rtnl_lock();
7483a214 1650 for_each_rdev(rdev) {
463d0183
JB
1651 if (net_eq(wiphy_net(&rdev->wiphy), net))
1652 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1653 }
463d0183
JB
1654 rtnl_unlock();
1655}
1656
1657static struct pernet_operations cfg80211_pernet_ops = {
1658 .exit = cfg80211_pernet_exit,
1659};
1660
a3ee4dc8
JB
1661void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
1662{
1663 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1664 unsigned long flags;
1665
eb745c7c
JB
1666 trace_wiphy_work_queue(wiphy, work);
1667
a3ee4dc8
JB
1668 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1669 if (list_empty(&work->entry))
1670 list_add_tail(&work->entry, &rdev->wiphy_work_list);
1671 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1672
91d20ab9 1673 queue_work(system_unbound_wq, &rdev->wiphy_work);
a3ee4dc8
JB
1674}
1675EXPORT_SYMBOL_GPL(wiphy_work_queue);
1676
1677void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
1678{
1679 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1680 unsigned long flags;
1681
1682 lockdep_assert_held(&wiphy->mtx);
1683
eb745c7c
JB
1684 trace_wiphy_work_cancel(wiphy, work);
1685
a3ee4dc8
JB
1686 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1687 if (!list_empty(&work->entry))
1688 list_del_init(&work->entry);
1689 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1690}
1691EXPORT_SYMBOL_GPL(wiphy_work_cancel);
1692
56cfb8ce
JB
1693void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
1694{
1695 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1696 unsigned long flags;
1697 bool run;
1698
eb745c7c
JB
1699 trace_wiphy_work_flush(wiphy, work);
1700
56cfb8ce
JB
1701 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1702 run = !work || !list_empty(&work->entry);
1703 spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1704
1705 if (run)
1706 cfg80211_process_wiphy_works(rdev, work);
1707}
1708EXPORT_SYMBOL_GPL(wiphy_work_flush);
1709
a3ee4dc8
JB
1710void wiphy_delayed_work_timer(struct timer_list *t)
1711{
41cb0855 1712 struct wiphy_delayed_work *dwork = timer_container_of(dwork, t, timer);
a3ee4dc8
JB
1713
1714 wiphy_work_queue(dwork->wiphy, &dwork->work);
1715}
1716EXPORT_SYMBOL(wiphy_delayed_work_timer);
1717
1718void wiphy_delayed_work_queue(struct wiphy *wiphy,
1719 struct wiphy_delayed_work *dwork,
1720 unsigned long delay)
1721{
eb745c7c
JB
1722 trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay);
1723
a3ee4dc8 1724 if (!delay) {
8fa7292f 1725 timer_delete(&dwork->timer);
a3ee4dc8
JB
1726 wiphy_work_queue(wiphy, &dwork->work);
1727 return;
1728 }
1729
1730 dwork->wiphy = wiphy;
1731 mod_timer(&dwork->timer, jiffies + delay);
1732}
1733EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
1734
1735void wiphy_delayed_work_cancel(struct wiphy *wiphy,
1736 struct wiphy_delayed_work *dwork)
1737{
1738 lockdep_assert_held(&wiphy->mtx);
1739
8fa7292f 1740 timer_delete_sync(&dwork->timer);
a3ee4dc8
JB
1741 wiphy_work_cancel(wiphy, &dwork->work);
1742}
1743EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
1744
56cfb8ce
JB
1745void wiphy_delayed_work_flush(struct wiphy *wiphy,
1746 struct wiphy_delayed_work *dwork)
1747{
1748 lockdep_assert_held(&wiphy->mtx);
1749
8fa7292f 1750 timer_delete_sync(&dwork->timer);
56cfb8ce
JB
1751 wiphy_work_flush(wiphy, &dwork->work);
1752}
1753EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
1754
68d0021f
RP
1755bool wiphy_delayed_work_pending(struct wiphy *wiphy,
1756 struct wiphy_delayed_work *dwork)
1757{
1758 return timer_pending(&dwork->timer);
1759}
1760EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
1761
463d0183 1762static int __init cfg80211_init(void)
704232c2 1763{
b2e1b302
LR
1764 int err;
1765
463d0183
JB
1766 err = register_pernet_device(&cfg80211_pernet_ops);
1767 if (err)
1768 goto out_fail_pernet;
1769
b2e1b302 1770 err = wiphy_sysfs_init();
704232c2
JB
1771 if (err)
1772 goto out_fail_sysfs;
1773
1774 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1775 if (err)
1776 goto out_fail_notifier;
1777
55682965
JB
1778 err = nl80211_init();
1779 if (err)
1780 goto out_fail_nl80211;
1781
704232c2
JB
1782 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1783
b2e1b302
LR
1784 err = regulatory_init();
1785 if (err)
1786 goto out_fail_reg;
1787
e4819013 1788 cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
f00f188f
WY
1789 if (!cfg80211_wq) {
1790 err = -ENOMEM;
e60d7443 1791 goto out_fail_wq;
f00f188f 1792 }
e60d7443 1793
704232c2
JB
1794 return 0;
1795
e60d7443
AB
1796out_fail_wq:
1797 regulatory_exit();
b2e1b302
LR
1798out_fail_reg:
1799 debugfs_remove(ieee80211_debugfs_dir);
81daf735 1800 nl80211_exit();
55682965
JB
1801out_fail_nl80211:
1802 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
704232c2
JB
1803out_fail_notifier:
1804 wiphy_sysfs_exit();
1805out_fail_sysfs:
463d0183
JB
1806 unregister_pernet_device(&cfg80211_pernet_ops);
1807out_fail_pernet:
704232c2
JB
1808 return err;
1809}
007f6c5e 1810fs_initcall(cfg80211_init);
704232c2 1811
f884e387 1812static void __exit cfg80211_exit(void)
704232c2
JB
1813{
1814 debugfs_remove(ieee80211_debugfs_dir);
55682965 1815 nl80211_exit();
704232c2
JB
1816 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1817 wiphy_sysfs_exit();
b2e1b302 1818 regulatory_exit();
463d0183 1819 unregister_pernet_device(&cfg80211_pernet_ops);
e60d7443 1820 destroy_workqueue(cfg80211_wq);
704232c2
JB
1821}
1822module_exit(cfg80211_exit);