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