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-2025 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
37MODULE_AUTHOR("Johannes Berg");
38MODULE_LICENSE("GPL");
39MODULE_DESCRIPTION("wireless configuration support");
40MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
41
42/* RCU-protected (and RTNL for writers) */
43LIST_HEAD(cfg80211_rdev_list);
44int cfg80211_rdev_list_generation;
45
46/* for debugfs */
47static struct dentry *ieee80211_debugfs_dir;
48
49/* for the cleanup, scan and event works */
50struct workqueue_struct *cfg80211_wq;
51
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
57struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
58{
59 struct cfg80211_registered_device *result = NULL, *rdev;
60
61 ASSERT_RTNL();
62
63 for_each_rdev(rdev) {
64 if (rdev->wiphy_idx == wiphy_idx) {
65 result = rdev;
66 break;
67 }
68 }
69
70 return result;
71}
72
73int get_wiphy_idx(struct wiphy *wiphy)
74{
75 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
76
77 return rdev->wiphy_idx;
78}
79
80struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
81{
82 struct cfg80211_registered_device *rdev;
83
84 ASSERT_RTNL();
85
86 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
87 if (!rdev)
88 return NULL;
89 return &rdev->wiphy;
90}
91
92static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
93 const char *newname)
94{
95 struct cfg80211_registered_device *rdev2;
96 int wiphy_idx, taken = -1, digits;
97
98 ASSERT_RTNL();
99
100 if (strlen(newname) > NL80211_WIPHY_NAME_MAXLEN)
101 return -EINVAL;
102
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
118 /* Ensure another device does not already have this name. */
119 for_each_rdev(rdev2)
120 if (strcmp(newname, wiphy_name(wiphy: &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();
132 lockdep_assert_wiphy(&rdev->wiphy);
133
134 /* Ignore nop renames */
135 if (strcmp(newname, wiphy_name(wiphy: &rdev->wiphy)) == 0)
136 return 0;
137
138 result = cfg80211_dev_check_name(rdev, newname);
139 if (result < 0)
140 return result;
141
142 result = device_rename(dev: &rdev->wiphy.dev, new_name: newname);
143 if (result)
144 return result;
145
146 debugfs_change_name(dentry: rdev->wiphy.debugfsdir, fmt: "%s", newname);
147
148 nl80211_notify_wiphy(rdev, cmd: NL80211_CMD_NEW_WIPHY);
149
150 return 0;
151}
152
153int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
154 struct net *net)
155{
156 struct wireless_dev *wdev;
157 int err = 0;
158
159 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
160 return -EOPNOTSUPP;
161
162 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
163 if (!wdev->netdev)
164 continue;
165 wdev->netdev->netns_immutable = false;
166 err = dev_change_net_namespace(dev: wdev->netdev, net, pat: "wlan%d");
167 if (err)
168 break;
169 wdev->netdev->netns_immutable = true;
170 }
171
172 if (err) {
173 /* failed -- clean up to old netns */
174 net = wiphy_net(wiphy: &rdev->wiphy);
175
176 list_for_each_entry_continue_reverse(wdev,
177 &rdev->wiphy.wdev_list,
178 list) {
179 if (!wdev->netdev)
180 continue;
181 wdev->netdev->netns_immutable = false;
182 err = dev_change_net_namespace(dev: wdev->netdev, net,
183 pat: "wlan%d");
184 WARN_ON(err);
185 wdev->netdev->netns_immutable = true;
186 }
187
188 return err;
189 }
190
191 guard(wiphy)(T: &rdev->wiphy);
192
193 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
194 if (!wdev->netdev)
195 continue;
196 nl80211_notify_iface(rdev, wdev, cmd: NL80211_CMD_DEL_INTERFACE);
197 }
198
199 nl80211_notify_wiphy(rdev, cmd: NL80211_CMD_DEL_WIPHY);
200
201 wiphy_net_set(wiphy: &rdev->wiphy, net);
202
203 err = device_rename(dev: &rdev->wiphy.dev, new_name: dev_name(dev: &rdev->wiphy.dev));
204 WARN_ON(err);
205
206 nl80211_notify_wiphy(rdev, cmd: NL80211_CMD_NEW_WIPHY);
207
208 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
209 if (!wdev->netdev)
210 continue;
211 nl80211_notify_iface(rdev, wdev, cmd: NL80211_CMD_NEW_INTERFACE);
212 }
213
214 return 0;
215}
216
217static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
218{
219 struct cfg80211_registered_device *rdev = data;
220
221 guard(wiphy)(T: &rdev->wiphy);
222
223 rdev_rfkill_poll(rdev);
224}
225
226void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
227 struct wireless_dev *wdev)
228{
229 lockdep_assert_held(&rdev->wiphy.mtx);
230
231 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
232 return;
233
234 if (!wdev_running(wdev))
235 return;
236
237 rdev_stop_p2p_device(rdev, wdev);
238 wdev->is_running = false;
239
240 rdev->opencount--;
241
242 if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
243 if (WARN_ON(!rdev->scan_req->notified &&
244 (!rdev->int_scan_req ||
245 !rdev->int_scan_req->notified)))
246 rdev->scan_req->info.aborted = true;
247 ___cfg80211_scan_done(rdev, send_message: false);
248 }
249}
250
251void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
252 struct wireless_dev *wdev)
253{
254 lockdep_assert_held(&rdev->wiphy.mtx);
255
256 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
257 return;
258
259 if (!wdev_running(wdev))
260 return;
261
262 rdev_stop_nan(rdev, wdev);
263 wdev->is_running = false;
264
265 rdev->opencount--;
266}
267
268void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
269{
270 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
271 struct wireless_dev *wdev;
272
273 ASSERT_RTNL();
274
275 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
276 if (wdev->netdev) {
277 dev_close(dev: wdev->netdev);
278 continue;
279 }
280
281 /* otherwise, check iftype */
282
283 guard(wiphy)(T: wiphy);
284
285 switch (wdev->iftype) {
286 case NL80211_IFTYPE_P2P_DEVICE:
287 cfg80211_stop_p2p_device(rdev, wdev);
288 break;
289 case NL80211_IFTYPE_NAN:
290 cfg80211_stop_nan(rdev, wdev);
291 break;
292 default:
293 break;
294 }
295 }
296}
297EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
298
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);
308 rtnl_unlock();
309
310 return 0;
311}
312
313static void cfg80211_rfkill_block_work(struct work_struct *work)
314{
315 struct cfg80211_registered_device *rdev;
316
317 rdev = container_of(work, struct cfg80211_registered_device,
318 rfkill_block);
319 cfg80211_rfkill_set_block(data: rdev, blocked: true);
320}
321
322static void cfg80211_event_work(struct work_struct *work)
323{
324 struct cfg80211_registered_device *rdev;
325
326 rdev = container_of(work, struct cfg80211_registered_device,
327 event_work);
328
329 guard(wiphy)(T: &rdev->wiphy);
330
331 cfg80211_process_rdev_events(rdev);
332}
333
334void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
335{
336 struct wireless_dev *wdev, *tmp;
337
338 ASSERT_RTNL();
339
340 list_for_each_entry_safe(wdev, tmp, &rdev->wiphy.wdev_list, list) {
341 if (wdev->nl_owner_dead) {
342 if (wdev->netdev)
343 dev_close(dev: wdev->netdev);
344
345 guard(wiphy)(T: &rdev->wiphy);
346
347 cfg80211_leave(rdev, wdev);
348 cfg80211_remove_virtual_intf(rdev, wdev);
349 }
350 }
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
365static void cfg80211_sched_scan_stop_wk(struct wiphy *wiphy,
366 struct wiphy_work *work)
367{
368 struct cfg80211_registered_device *rdev;
369 struct cfg80211_sched_scan_request *req, *tmp;
370
371 rdev = container_of(work, struct cfg80211_registered_device,
372 sched_scan_stop_wk);
373
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, driver_initiated: false);
377 }
378}
379
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(wiphy: &rdev->wiphy, chandef: &rdev->radar_chandef,
390 dfs_state: NL80211_DFS_UNAVAILABLE,
391 event: 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(wiphy: &rdev->wiphy, chandef: &rdev->cac_done_chandef,
406 dfs_state: NL80211_DFS_AVAILABLE,
407 event: NL80211_RADAR_CAC_FINISHED);
408
409 rtnl_unlock();
410}
411
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
419 trace_wiphy_work_worker_start(wiphy: &rdev->wiphy);
420
421 guard(wiphy)(T: &rdev->wiphy);
422 if (rdev->suspended)
423 return;
424
425 spin_lock_irq(lock: &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(entry: &wk->entry);
430 if (!list_empty(head: &rdev->wiphy_work_list))
431 queue_work(wq: system_unbound_wq, work);
432 spin_unlock_irq(lock: &rdev->wiphy_work_lock);
433
434 trace_wiphy_work_run(wiphy: &rdev->wiphy, work: wk);
435 wk->func(&rdev->wiphy, wk);
436 } else {
437 spin_unlock_irq(lock: &rdev->wiphy_work_lock);
438 }
439}
440
441/* exported functions */
442
443struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
444 const char *requested_name)
445{
446 static atomic_t wiphy_counter = ATOMIC_INIT(0);
447
448 struct cfg80211_registered_device *rdev;
449 int alloc_size;
450
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);
458 WARN_ON(ops->join_mesh && !ops->leave_mesh);
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);
467
468 alloc_size = sizeof(*rdev) + sizeof_priv;
469
470 rdev = kzalloc(alloc_size, GFP_KERNEL);
471 if (!rdev)
472 return NULL;
473
474 rdev->ops = ops;
475
476 rdev->wiphy_idx = atomic_inc_return(v: &wiphy_counter);
477
478 if (unlikely(rdev->wiphy_idx < 0)) {
479 /* ugh, wrapped! */
480 atomic_dec(v: &wiphy_counter);
481 kfree(objp: rdev);
482 return NULL;
483 }
484
485 /* atomic_inc_return makes it start at 1, make it start at 0 */
486 rdev->wiphy_idx--;
487
488 /* give it a proper name */
489 if (requested_name && requested_name[0]) {
490 int rv;
491
492 rtnl_lock();
493 rv = cfg80211_dev_check_name(rdev, newname: requested_name);
494
495 if (rv < 0) {
496 rtnl_unlock();
497 goto use_default_name;
498 }
499
500 rv = dev_set_name(dev: &rdev->wiphy.dev, name: "%s", requested_name);
501 rtnl_unlock();
502 if (rv)
503 goto use_default_name;
504 } else {
505 int rv;
506
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 */
514 rv = dev_set_name(dev: &rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
515 if (rv < 0) {
516 kfree(objp: rdev);
517 return NULL;
518 }
519 }
520
521 mutex_init(&rdev->wiphy.mtx);
522 INIT_LIST_HEAD(list: &rdev->wiphy.wdev_list);
523 INIT_LIST_HEAD(list: &rdev->beacon_registrations);
524 spin_lock_init(&rdev->beacon_registrations_lock);
525 spin_lock_init(&rdev->bss_lock);
526 INIT_LIST_HEAD(list: &rdev->bss_list);
527 INIT_LIST_HEAD(list: &rdev->sched_scan_req_list);
528 wiphy_work_init(work: &rdev->scan_done_wk, func: __cfg80211_scan_done);
529 INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
530 cfg80211_dfs_channels_update_work);
531#ifdef CONFIG_CFG80211_WEXT
532 rdev->wiphy.wext = &cfg80211_wext_handler;
533#endif
534
535 device_initialize(dev: &rdev->wiphy.dev);
536 rdev->wiphy.dev.class = &ieee80211_class;
537 rdev->wiphy.dev.platform_data = rdev;
538 device_enable_async_suspend(dev: &rdev->wiphy.dev);
539
540 INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
541 wiphy_work_init(work: &rdev->sched_scan_stop_wk, func: cfg80211_sched_scan_stop_wk);
542 INIT_WORK(&rdev->sched_scan_res_wk, cfg80211_sched_scan_results_wk);
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);
546 INIT_WORK(&rdev->mgmt_registrations_update_wk,
547 cfg80211_mgmt_registrations_update_wk);
548 spin_lock_init(&rdev->mgmt_registrations_lock);
549 INIT_WORK(&rdev->wiphy_work, cfg80211_wiphy_work);
550 INIT_LIST_HEAD(list: &rdev->wiphy_work_list);
551 spin_lock_init(&rdev->wiphy_work_lock);
552
553#ifdef CONFIG_CFG80211_DEFAULT_PS
554 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
555#endif
556
557 wiphy_net_set(wiphy: &rdev->wiphy, net: &init_net);
558
559 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
560 rdev->wiphy.rfkill = rfkill_alloc(name: dev_name(dev: &rdev->wiphy.dev),
561 parent: &rdev->wiphy.dev, type: RFKILL_TYPE_WLAN,
562 ops: &rdev->rfkill_ops, ops_data: rdev);
563
564 if (!rdev->wiphy.rfkill) {
565 wiphy_free(wiphy: &rdev->wiphy);
566 return NULL;
567 }
568
569 INIT_WORK(&rdev->rfkill_block, cfg80211_rfkill_block_work);
570 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
571 INIT_WORK(&rdev->event_work, cfg80211_event_work);
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);
576
577 init_waitqueue_head(&rdev->dev_wait);
578
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 */
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;
588 rdev->wiphy.coverage_class = 0;
589
590 rdev->wiphy.max_num_csa_counters = 1;
591
592 rdev->wiphy.max_sched_scan_plans = 1;
593 rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
594
595 return &rdev->wiphy;
596}
597EXPORT_SYMBOL(wiphy_new_nm);
598
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)
604{
605 const struct ieee80211_iface_combination *c;
606 int i, j;
607
608 for (i = 0; i < n_iface_comb; i++) {
609 u32 cnt = 0;
610 u16 all_iftypes = 0;
611
612 c = &iface_comb[i];
613
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))
619 return -EINVAL;
620
621 /* Need at least one channel */
622 if (WARN_ON(!c->num_different_channels))
623 return -EINVAL;
624
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))
632 return -EINVAL;
633
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
640 /* interface types shouldn't overlap */
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
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) &&
658 c->limits[j].max > 1))
659 return -EINVAL;
660
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) &&
667 c->limits[j].max > 1))
668 return -EINVAL;
669
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
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
694 if (WARN_ON(all_iftypes & BIT(NL80211_IFTYPE_WDS)))
695 return -EINVAL;
696
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
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 iface_comb: radio->iface_combinations,
716 n_iface_comb: radio->n_iface_combinations,
717 combined_radio: false);
718 if (ret)
719 return ret;
720 }
721
722 combined_radio = true;
723 }
724
725 ret = wiphy_verify_iface_combinations(wiphy,
726 iface_comb: wiphy->iface_combinations,
727 n_iface_comb: wiphy->n_iface_combinations,
728 combined_radio);
729
730 return ret;
731}
732
733int wiphy_register(struct wiphy *wiphy)
734{
735 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
736 int res;
737 enum nl80211_band band;
738 struct ieee80211_supported_band *sband;
739 bool have_band = false;
740 int i;
741 u16 ifmodes = wiphy->interface_modes;
742
743#ifdef CONFIG_PM
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))
751 return -EINVAL;
752#endif
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;
757
758 if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
759 (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
760 !rdev->ops->add_nan_func || !rdev->ops->del_nan_func ||
761 !(wiphy->nan_supported_bands & BIT(NL80211_BAND_2GHZ)))))
762 return -EINVAL;
763
764 if (WARN_ON(wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS)))
765 return -EINVAL;
766
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) |
781 BIT(NL80211_PREAMBLE_HE) |
782 BIT(NL80211_PREAMBLE_DMG))))
783 return -EINVAL;
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;
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) |
796 BIT(NL80211_CHAN_WIDTH_320) |
797 BIT(NL80211_CHAN_WIDTH_5) |
798 BIT(NL80211_CHAN_WIDTH_10))))
799 return -EINVAL;
800 }
801
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
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
818 if (WARN_ON(wiphy->ap_sme_capa &&
819 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
820 return -EINVAL;
821
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
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
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
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
848 if (WARN_ON(!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
849 rdev->ops->update_connect_params))
850 return -EINVAL;
851
852 if (wiphy->addresses)
853 memcpy(to: wiphy->perm_addr, from: wiphy->addresses[0].addr, ETH_ALEN);
854
855 /* sanity check ifmodes */
856 WARN_ON(!ifmodes);
857 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
858 if (WARN_ON(ifmodes != wiphy->interface_modes))
859 wiphy->interface_modes = ifmodes;
860
861 res = wiphy_verify_combinations(wiphy);
862 if (res)
863 return res;
864
865 /* sanity check supported bands/channels */
866 for (band = 0; band < NUM_NL80211_BANDS; band++) {
867 const struct ieee80211_sband_iftype_data *iftd;
868 u16 types = 0;
869 bool have_he = false;
870
871 sband = wiphy->bands[band];
872 if (!sband)
873 continue;
874
875 sband->band = band;
876 if (WARN_ON(!sband->n_channels))
877 return -EINVAL;
878 /*
879 * on 60GHz or sub-1Ghz band, there are no legacy rates, so
880 * n_bitrates is 0
881 */
882 if (WARN_ON((band != NL80211_BAND_60GHZ &&
883 band != NL80211_BAND_S1GHZ) &&
884 !sband->n_bitrates))
885 return -EINVAL;
886
887 if (WARN_ON(band == NL80211_BAND_6GHZ &&
888 (sband->ht_cap.ht_supported ||
889 sband->vht_cap.vht_supported)))
890 return -EINVAL;
891
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 &&
898 band == NL80211_BAND_2GHZ &&
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
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))
910 return -EINVAL;
911
912 for (i = 0; i < sband->n_channels; i++) {
913 sband->channels[i].orig_flags =
914 sband->channels[i].flags;
915 sband->channels[i].orig_mag = INT_MAX;
916 sband->channels[i].orig_mpwr =
917 sband->channels[i].max_power;
918 sband->channels[i].band = band;
919
920 if (WARN_ON(sband->channels[i].freq_offset >= 1000))
921 return -EINVAL;
922 }
923
924 for_each_sband_iftype_data(sband, i, iftd) {
925 bool has_ap, has_non_ap;
926 u32 ap_bits = BIT(NL80211_IFTYPE_AP) |
927 BIT(NL80211_IFTYPE_P2P_GO);
928
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;
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;
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;
958 }
959
960 if (WARN_ON(!have_he && band == NL80211_BAND_6GHZ))
961 return -EINVAL;
962
963 have_band = true;
964 }
965
966 if (!have_band) {
967 WARN_ON(1);
968 return -EINVAL;
969 }
970
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
984#ifdef CONFIG_PM
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;
990#endif
991
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
998 /* Allocate radio configuration space for multi-radio wiphy */
999 if (wiphy->n_radio > 0) {
1000 int idx;
1001
1002 wiphy->radio_cfg = kcalloc(wiphy->n_radio,
1003 sizeof(*wiphy->radio_cfg),
1004 GFP_KERNEL);
1005 if (!wiphy->radio_cfg)
1006 return -ENOMEM;
1007 /*
1008 * Initialize wiphy radio parameters to IEEE 802.11
1009 * MIB default values. RTS threshold is disabled by
1010 * default with the special -1 value.
1011 */
1012 for (idx = 0; idx < wiphy->n_radio; idx++)
1013 wiphy->radio_cfg[idx].rts_threshold = (u32)-1;
1014 }
1015
1016 /* check and set up bitrates */
1017 ieee80211_set_bitrate_flags(wiphy);
1018
1019 rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
1020
1021 if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_CTWINDOW)
1022 rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_CTWIN;
1023 else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)
1024 rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_CTWINDOW;
1025 if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_OPPPS)
1026 rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_OPPPS;
1027 else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)
1028 rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_OPPPS;
1029
1030 rtnl_lock();
1031 wiphy_lock(wiphy: &rdev->wiphy);
1032 res = device_add(dev: &rdev->wiphy.dev);
1033 if (res) {
1034 wiphy_unlock(wiphy: &rdev->wiphy);
1035 rtnl_unlock();
1036 return res;
1037 }
1038
1039 list_add_rcu(new: &rdev->list, head: &cfg80211_rdev_list);
1040 cfg80211_rdev_list_generation++;
1041
1042 /* add to debugfs */
1043 rdev->wiphy.debugfsdir = debugfs_create_dir(name: wiphy_name(wiphy: &rdev->wiphy),
1044 parent: ieee80211_debugfs_dir);
1045
1046 cfg80211_debugfs_rdev_add(rdev);
1047 nl80211_notify_wiphy(rdev, cmd: NL80211_CMD_NEW_WIPHY);
1048 wiphy_unlock(wiphy: &rdev->wiphy);
1049
1050 /* set up regulatory info */
1051 wiphy_regulatory_register(wiphy);
1052
1053 if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
1054 struct regulatory_request request;
1055
1056 request.wiphy_idx = get_wiphy_idx(wiphy);
1057 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
1058 request.alpha2[0] = '9';
1059 request.alpha2[1] = '9';
1060
1061 nl80211_send_reg_change_event(request: &request);
1062 }
1063
1064 /* Check that nobody globally advertises any capabilities they do not
1065 * advertise on all possible interface types.
1066 */
1067 if (wiphy->extended_capabilities_len &&
1068 wiphy->num_iftype_ext_capab &&
1069 wiphy->iftype_ext_capab) {
1070 u8 supported_on_all, j;
1071 const struct wiphy_iftype_ext_capab *capab;
1072
1073 capab = wiphy->iftype_ext_capab;
1074 for (j = 0; j < wiphy->extended_capabilities_len; j++) {
1075 if (capab[0].extended_capabilities_len > j)
1076 supported_on_all =
1077 capab[0].extended_capabilities[j];
1078 else
1079 supported_on_all = 0x00;
1080 for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
1081 if (j >= capab[i].extended_capabilities_len) {
1082 supported_on_all = 0x00;
1083 break;
1084 }
1085 supported_on_all &=
1086 capab[i].extended_capabilities[j];
1087 }
1088 if (WARN_ON(wiphy->extended_capabilities[j] &
1089 ~supported_on_all))
1090 break;
1091 }
1092 }
1093
1094 rdev->wiphy.registered = true;
1095 rtnl_unlock();
1096
1097 res = rfkill_register(rfkill: rdev->wiphy.rfkill);
1098 if (res) {
1099 rfkill_destroy(rfkill: rdev->wiphy.rfkill);
1100 rdev->wiphy.rfkill = NULL;
1101 wiphy_unregister(wiphy: &rdev->wiphy);
1102 return res;
1103 }
1104
1105 return 0;
1106}
1107EXPORT_SYMBOL(wiphy_register);
1108
1109void wiphy_rfkill_start_polling(struct wiphy *wiphy)
1110{
1111 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1112
1113 if (!rdev->ops->rfkill_poll)
1114 return;
1115 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
1116 rfkill_resume_polling(rfkill: wiphy->rfkill);
1117}
1118EXPORT_SYMBOL(wiphy_rfkill_start_polling);
1119
1120void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev,
1121 struct wiphy_work *end)
1122{
1123 unsigned int runaway_limit = 100;
1124 unsigned long flags;
1125
1126 lockdep_assert_held(&rdev->wiphy.mtx);
1127
1128 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1129 while (!list_empty(head: &rdev->wiphy_work_list)) {
1130 struct wiphy_work *wk;
1131
1132 wk = list_first_entry(&rdev->wiphy_work_list,
1133 struct wiphy_work, entry);
1134 list_del_init(entry: &wk->entry);
1135 spin_unlock_irqrestore(lock: &rdev->wiphy_work_lock, flags);
1136
1137 trace_wiphy_work_run(wiphy: &rdev->wiphy, work: wk);
1138 wk->func(&rdev->wiphy, wk);
1139
1140 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1141
1142 if (wk == end)
1143 break;
1144
1145 if (WARN_ON(--runaway_limit == 0))
1146 INIT_LIST_HEAD(list: &rdev->wiphy_work_list);
1147 }
1148 spin_unlock_irqrestore(lock: &rdev->wiphy_work_lock, flags);
1149}
1150
1151void wiphy_unregister(struct wiphy *wiphy)
1152{
1153 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1154
1155 wait_event(rdev->dev_wait, ({
1156 int __count;
1157 wiphy_lock(&rdev->wiphy);
1158 __count = rdev->opencount;
1159 wiphy_unlock(&rdev->wiphy);
1160 __count == 0; }));
1161
1162 if (rdev->wiphy.rfkill)
1163 rfkill_unregister(rfkill: rdev->wiphy.rfkill);
1164
1165 rtnl_lock();
1166 wiphy_lock(wiphy: &rdev->wiphy);
1167 nl80211_notify_wiphy(rdev, cmd: NL80211_CMD_DEL_WIPHY);
1168 rdev->wiphy.registered = false;
1169
1170 WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
1171
1172 /*
1173 * First remove the hardware from everywhere, this makes
1174 * it impossible to find from userspace.
1175 */
1176 debugfs_remove_recursive(dentry: rdev->wiphy.debugfsdir);
1177 list_del_rcu(entry: &rdev->list);
1178 synchronize_rcu();
1179
1180 /*
1181 * If this device got a regulatory hint tell core its
1182 * free to listen now to a new shiny device regulatory hint
1183 */
1184 wiphy_regulatory_deregister(wiphy);
1185
1186 cfg80211_rdev_list_generation++;
1187 device_del(dev: &rdev->wiphy.dev);
1188
1189#ifdef CONFIG_PM
1190 if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
1191 rdev_set_wakeup(rdev, enabled: false);
1192#endif
1193
1194 /* surely nothing is reachable now, clean up work */
1195 cfg80211_process_wiphy_works(rdev, NULL);
1196 wiphy_unlock(wiphy: &rdev->wiphy);
1197 rtnl_unlock();
1198
1199 /* this has nothing to do now but make sure it's gone */
1200 cancel_work_sync(work: &rdev->wiphy_work);
1201
1202 cancel_work_sync(work: &rdev->conn_work);
1203 flush_work(work: &rdev->event_work);
1204 cancel_delayed_work_sync(dwork: &rdev->dfs_update_channels_wk);
1205 cancel_delayed_work_sync(dwork: &rdev->background_cac_done_wk);
1206 flush_work(work: &rdev->destroy_work);
1207 flush_work(work: &rdev->propagate_radar_detect_wk);
1208 flush_work(work: &rdev->propagate_cac_done_wk);
1209 flush_work(work: &rdev->mgmt_registrations_update_wk);
1210 flush_work(work: &rdev->background_cac_abort_wk);
1211
1212 cfg80211_rdev_free_wowlan(rdev);
1213 cfg80211_free_coalesce(coalesce: rdev->coalesce);
1214 rdev->coalesce = NULL;
1215}
1216EXPORT_SYMBOL(wiphy_unregister);
1217
1218void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
1219{
1220 struct cfg80211_internal_bss *scan, *tmp;
1221 struct cfg80211_beacon_registration *reg, *treg;
1222 unsigned long flags;
1223
1224 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1225 WARN_ON(!list_empty(&rdev->wiphy_work_list));
1226 spin_unlock_irqrestore(lock: &rdev->wiphy_work_lock, flags);
1227 cancel_work_sync(work: &rdev->wiphy_work);
1228
1229 rfkill_destroy(rfkill: rdev->wiphy.rfkill);
1230 list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
1231 list_del(entry: &reg->list);
1232 kfree(objp: reg);
1233 }
1234 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
1235 cfg80211_put_bss(wiphy: &rdev->wiphy, bss: &scan->pub);
1236 mutex_destroy(lock: &rdev->wiphy.mtx);
1237
1238 /*
1239 * The 'regd' can only be non-NULL if we never finished
1240 * initializing the wiphy and thus never went through the
1241 * unregister path - e.g. in failure scenarios. Thus, it
1242 * cannot have been visible to anyone if non-NULL, so we
1243 * can just free it here.
1244 */
1245 kfree(rcu_dereference_raw(rdev->wiphy.regd));
1246
1247 kfree(objp: rdev);
1248}
1249
1250void wiphy_free(struct wiphy *wiphy)
1251{
1252 kfree(objp: wiphy->radio_cfg);
1253 put_device(dev: &wiphy->dev);
1254}
1255EXPORT_SYMBOL(wiphy_free);
1256
1257void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
1258 enum rfkill_hard_block_reasons reason)
1259{
1260 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1261
1262 if (rfkill_set_hw_state_reason(rfkill: wiphy->rfkill, blocked, reason))
1263 schedule_work(work: &rdev->rfkill_block);
1264}
1265EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
1266
1267static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
1268 bool unregister_netdev)
1269{
1270 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy: wdev->wiphy);
1271 struct cfg80211_cqm_config *cqm_config;
1272 unsigned int link_id;
1273
1274 ASSERT_RTNL();
1275 lockdep_assert_held(&rdev->wiphy.mtx);
1276
1277 nl80211_notify_iface(rdev, wdev, cmd: NL80211_CMD_DEL_INTERFACE);
1278
1279 wdev->registered = false;
1280
1281 if (wdev->netdev) {
1282 sysfs_remove_link(kobj: &wdev->netdev->dev.kobj, name: "phy80211");
1283 if (unregister_netdev)
1284 unregister_netdevice(dev: wdev->netdev);
1285 }
1286
1287 list_del_rcu(entry: &wdev->list);
1288 synchronize_net();
1289 rdev->devlist_generation++;
1290
1291 cfg80211_mlme_purge_registrations(wdev);
1292
1293 switch (wdev->iftype) {
1294 case NL80211_IFTYPE_P2P_DEVICE:
1295 cfg80211_stop_p2p_device(rdev, wdev);
1296 break;
1297 case NL80211_IFTYPE_NAN:
1298 cfg80211_stop_nan(rdev, wdev);
1299 break;
1300 default:
1301 break;
1302 }
1303
1304#ifdef CONFIG_CFG80211_WEXT
1305 kfree_sensitive(wdev->wext.keys);
1306 wdev->wext.keys = NULL;
1307#endif
1308 wiphy_work_cancel(wiphy: wdev->wiphy, work: &wdev->cqm_rssi_work);
1309 /* deleted from the list, so can't be found from nl80211 any more */
1310 cqm_config = rcu_access_pointer(wdev->cqm_config);
1311 kfree_rcu(cqm_config, rcu_head);
1312 RCU_INIT_POINTER(wdev->cqm_config, NULL);
1313
1314 /*
1315 * Ensure that all events have been processed and
1316 * freed.
1317 */
1318 cfg80211_process_wdev_events(wdev);
1319
1320 if (wdev->iftype == NL80211_IFTYPE_STATION ||
1321 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1322 for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
1323 struct cfg80211_internal_bss *curbss;
1324
1325 curbss = wdev->links[link_id].client.current_bss;
1326
1327 if (WARN_ON(curbss)) {
1328 cfg80211_unhold_bss(bss: curbss);
1329 cfg80211_put_bss(wiphy: wdev->wiphy, bss: &curbss->pub);
1330 wdev->links[link_id].client.current_bss = NULL;
1331 }
1332 }
1333 }
1334
1335 wdev->connected = false;
1336}
1337
1338void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1339{
1340 _cfg80211_unregister_wdev(wdev, unregister_netdev: true);
1341}
1342EXPORT_SYMBOL(cfg80211_unregister_wdev);
1343
1344static const struct device_type wiphy_type = {
1345 .name = "wlan",
1346};
1347
1348void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1349 enum nl80211_iftype iftype, int num)
1350{
1351 lockdep_assert_held(&rdev->wiphy.mtx);
1352
1353 rdev->num_running_ifaces += num;
1354 if (iftype == NL80211_IFTYPE_MONITOR)
1355 rdev->num_running_monitor_ifaces += num;
1356}
1357
1358void cfg80211_leave(struct cfg80211_registered_device *rdev,
1359 struct wireless_dev *wdev)
1360{
1361 struct net_device *dev = wdev->netdev;
1362 struct cfg80211_sched_scan_request *pos, *tmp;
1363
1364 lockdep_assert_held(&rdev->wiphy.mtx);
1365
1366 cfg80211_pmsr_wdev_down(wdev);
1367
1368 cfg80211_stop_background_radar_detection(wdev);
1369
1370 switch (wdev->iftype) {
1371 case NL80211_IFTYPE_ADHOC:
1372 cfg80211_leave_ibss(rdev, dev, nowext: true);
1373 break;
1374 case NL80211_IFTYPE_P2P_CLIENT:
1375 case NL80211_IFTYPE_STATION:
1376 list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1377 list) {
1378 if (dev == pos->dev)
1379 cfg80211_stop_sched_scan_req(rdev, req: pos, driver_initiated: false);
1380 }
1381
1382#ifdef CONFIG_CFG80211_WEXT
1383 kfree(wdev->wext.ie);
1384 wdev->wext.ie = NULL;
1385 wdev->wext.ie_len = 0;
1386 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1387#endif
1388 cfg80211_disconnect(rdev, dev,
1389 reason: WLAN_REASON_DEAUTH_LEAVING, wextev: true);
1390 break;
1391 case NL80211_IFTYPE_MESH_POINT:
1392 cfg80211_leave_mesh(rdev, dev);
1393 break;
1394 case NL80211_IFTYPE_AP:
1395 case NL80211_IFTYPE_P2P_GO:
1396 cfg80211_stop_ap(rdev, dev, link: -1, notify: true);
1397 break;
1398 case NL80211_IFTYPE_OCB:
1399 cfg80211_leave_ocb(rdev, dev);
1400 break;
1401 case NL80211_IFTYPE_P2P_DEVICE:
1402 case NL80211_IFTYPE_NAN:
1403 /* cannot happen, has no netdev */
1404 break;
1405 case NL80211_IFTYPE_AP_VLAN:
1406 case NL80211_IFTYPE_MONITOR:
1407 /* nothing to do */
1408 break;
1409 case NL80211_IFTYPE_UNSPECIFIED:
1410 case NL80211_IFTYPE_WDS:
1411 case NUM_NL80211_IFTYPES:
1412 /* invalid */
1413 break;
1414 }
1415}
1416
1417void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1418 gfp_t gfp)
1419{
1420 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1421 struct cfg80211_event *ev;
1422 unsigned long flags;
1423
1424 trace_cfg80211_stop_iface(wiphy, wdev);
1425
1426 ev = kzalloc(sizeof(*ev), gfp);
1427 if (!ev)
1428 return;
1429
1430 ev->type = EVENT_STOPPED;
1431
1432 spin_lock_irqsave(&wdev->event_lock, flags);
1433 list_add_tail(new: &ev->list, head: &wdev->event_list);
1434 spin_unlock_irqrestore(lock: &wdev->event_lock, flags);
1435 queue_work(wq: cfg80211_wq, work: &rdev->event_work);
1436}
1437EXPORT_SYMBOL(cfg80211_stop_iface);
1438
1439void cfg80211_init_wdev(struct wireless_dev *wdev)
1440{
1441 INIT_LIST_HEAD(list: &wdev->event_list);
1442 spin_lock_init(&wdev->event_lock);
1443 INIT_LIST_HEAD(list: &wdev->mgmt_registrations);
1444 INIT_LIST_HEAD(list: &wdev->pmsr_list);
1445 spin_lock_init(&wdev->pmsr_lock);
1446 INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
1447
1448#ifdef CONFIG_CFG80211_WEXT
1449 wdev->wext.default_key = -1;
1450 wdev->wext.default_mgmt_key = -1;
1451 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1452#endif
1453
1454 wiphy_work_init(work: &wdev->cqm_rssi_work, func: cfg80211_cqm_rssi_notify_work);
1455
1456 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1457 wdev->ps = true;
1458 else
1459 wdev->ps = false;
1460 /* allow mac80211 to determine the timeout */
1461 wdev->ps_timeout = -1;
1462
1463 wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
1464
1465 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1466 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1467 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1468 wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
1469
1470 INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1471}
1472
1473void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
1474 struct wireless_dev *wdev)
1475{
1476 ASSERT_RTNL();
1477 lockdep_assert_held(&rdev->wiphy.mtx);
1478
1479 /*
1480 * We get here also when the interface changes network namespaces,
1481 * as it's registered into the new one, but we don't want it to
1482 * change ID in that case. Checking if the ID is already assigned
1483 * works, because 0 isn't considered a valid ID and the memory is
1484 * 0-initialized.
1485 */
1486 if (!wdev->identifier)
1487 wdev->identifier = ++rdev->wdev_id;
1488 list_add_rcu(new: &wdev->list, head: &rdev->wiphy.wdev_list);
1489 rdev->devlist_generation++;
1490 wdev->registered = true;
1491
1492 if (wdev->netdev &&
1493 sysfs_create_link(kobj: &wdev->netdev->dev.kobj, target: &rdev->wiphy.dev.kobj,
1494 name: "phy80211"))
1495 pr_err("failed to add phy80211 symlink to netdev!\n");
1496
1497 nl80211_notify_iface(rdev, wdev, cmd: NL80211_CMD_NEW_INTERFACE);
1498}
1499
1500int cfg80211_register_netdevice(struct net_device *dev)
1501{
1502 struct wireless_dev *wdev = dev->ieee80211_ptr;
1503 struct cfg80211_registered_device *rdev;
1504 int ret;
1505
1506 ASSERT_RTNL();
1507
1508 if (WARN_ON(!wdev))
1509 return -EINVAL;
1510
1511 rdev = wiphy_to_rdev(wiphy: wdev->wiphy);
1512
1513 lockdep_assert_held(&rdev->wiphy.mtx);
1514
1515 /* we'll take care of this */
1516 wdev->registered = true;
1517 wdev->registering = true;
1518 ret = register_netdevice(dev);
1519 if (ret)
1520 goto out;
1521
1522 cfg80211_register_wdev(rdev, wdev);
1523 ret = 0;
1524out:
1525 wdev->registering = false;
1526 if (ret)
1527 wdev->registered = false;
1528 return ret;
1529}
1530EXPORT_SYMBOL(cfg80211_register_netdevice);
1531
1532static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1533 unsigned long state, void *ptr)
1534{
1535 struct net_device *dev = netdev_notifier_info_to_dev(info: ptr);
1536 struct wireless_dev *wdev = dev->ieee80211_ptr;
1537 struct cfg80211_registered_device *rdev;
1538 struct cfg80211_sched_scan_request *pos, *tmp;
1539
1540 if (!wdev)
1541 return NOTIFY_DONE;
1542
1543 rdev = wiphy_to_rdev(wiphy: wdev->wiphy);
1544
1545 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1546
1547 switch (state) {
1548 case NETDEV_POST_INIT:
1549 SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1550 wdev->netdev = dev;
1551 /* can only change netns with wiphy */
1552 dev->netns_immutable = true;
1553
1554 cfg80211_init_wdev(wdev);
1555 break;
1556 case NETDEV_REGISTER:
1557 if (!wdev->registered) {
1558 guard(wiphy)(T: &rdev->wiphy);
1559
1560 cfg80211_register_wdev(rdev, wdev);
1561 }
1562 break;
1563 case NETDEV_UNREGISTER:
1564 /*
1565 * It is possible to get NETDEV_UNREGISTER multiple times,
1566 * so check wdev->registered.
1567 */
1568 if (wdev->registered && !wdev->registering) {
1569 guard(wiphy)(T: &rdev->wiphy);
1570
1571 _cfg80211_unregister_wdev(wdev, unregister_netdev: false);
1572 }
1573 break;
1574 case NETDEV_GOING_DOWN:
1575 scoped_guard(wiphy, &rdev->wiphy) {
1576 cfg80211_leave(rdev, wdev);
1577 cfg80211_remove_links(wdev);
1578 }
1579 /* since we just did cfg80211_leave() nothing to do there */
1580 cancel_work_sync(work: &wdev->disconnect_wk);
1581 cancel_work_sync(work: &wdev->pmsr_free_wk);
1582 break;
1583 case NETDEV_DOWN:
1584 wiphy_lock(wiphy: &rdev->wiphy);
1585 cfg80211_update_iface_num(rdev, iftype: wdev->iftype, num: -1);
1586 if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
1587 if (WARN_ON(!rdev->scan_req->notified &&
1588 (!rdev->int_scan_req ||
1589 !rdev->int_scan_req->notified)))
1590 rdev->scan_req->info.aborted = true;
1591 ___cfg80211_scan_done(rdev, send_message: false);
1592 }
1593
1594 list_for_each_entry_safe(pos, tmp,
1595 &rdev->sched_scan_req_list, list) {
1596 if (WARN_ON(pos->dev == wdev->netdev))
1597 cfg80211_stop_sched_scan_req(rdev, req: pos, driver_initiated: false);
1598 }
1599
1600 rdev->opencount--;
1601 wiphy_unlock(wiphy: &rdev->wiphy);
1602 wake_up(&rdev->dev_wait);
1603 break;
1604 case NETDEV_UP:
1605 wiphy_lock(wiphy: &rdev->wiphy);
1606 cfg80211_update_iface_num(rdev, iftype: wdev->iftype, num: 1);
1607 switch (wdev->iftype) {
1608#ifdef CONFIG_CFG80211_WEXT
1609 case NL80211_IFTYPE_ADHOC:
1610 cfg80211_ibss_wext_join(rdev, wdev);
1611 break;
1612 case NL80211_IFTYPE_STATION:
1613 cfg80211_mgd_wext_connect(rdev, wdev);
1614 break;
1615#endif
1616#ifdef CONFIG_MAC80211_MESH
1617 case NL80211_IFTYPE_MESH_POINT:
1618 {
1619 /* backward compat code... */
1620 struct mesh_setup setup;
1621 memcpy(&setup, &default_mesh_setup,
1622 sizeof(setup));
1623 /* back compat only needed for mesh_id */
1624 setup.mesh_id = wdev->u.mesh.id;
1625 setup.mesh_id_len = wdev->u.mesh.id_up_len;
1626 if (wdev->u.mesh.id_up_len)
1627 __cfg80211_join_mesh(rdev, dev,
1628 &setup,
1629 &default_mesh_config);
1630 break;
1631 }
1632#endif
1633 default:
1634 break;
1635 }
1636 rdev->opencount++;
1637
1638 /*
1639 * Configure power management to the driver here so that its
1640 * correctly set also after interface type changes etc.
1641 */
1642 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1643 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1644 rdev->ops->set_power_mgmt &&
1645 rdev_set_power_mgmt(rdev, dev, enabled: wdev->ps,
1646 timeout: wdev->ps_timeout)) {
1647 /* assume this means it's off */
1648 wdev->ps = false;
1649 }
1650 wiphy_unlock(wiphy: &rdev->wiphy);
1651 break;
1652 case NETDEV_PRE_UP:
1653 if (!cfg80211_iftype_allowed(wiphy: wdev->wiphy, iftype: wdev->iftype,
1654 is_4addr: wdev->use_4addr, check_swif: 0))
1655 return notifier_from_errno(err: -EOPNOTSUPP);
1656
1657 if (rfkill_blocked(rfkill: rdev->wiphy.rfkill))
1658 return notifier_from_errno(err: -ERFKILL);
1659 break;
1660 default:
1661 return NOTIFY_DONE;
1662 }
1663
1664 wireless_nlevent_flush();
1665
1666 return NOTIFY_OK;
1667}
1668
1669static struct notifier_block cfg80211_netdev_notifier = {
1670 .notifier_call = cfg80211_netdev_notifier_call,
1671};
1672
1673static void __net_exit cfg80211_pernet_exit(struct net *net)
1674{
1675 struct cfg80211_registered_device *rdev;
1676
1677 rtnl_lock();
1678 for_each_rdev(rdev) {
1679 if (net_eq(net1: wiphy_net(wiphy: &rdev->wiphy), net2: net))
1680 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1681 }
1682 rtnl_unlock();
1683}
1684
1685static struct pernet_operations cfg80211_pernet_ops = {
1686 .exit = cfg80211_pernet_exit,
1687};
1688
1689void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
1690{
1691 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1692 unsigned long flags;
1693
1694 trace_wiphy_work_queue(wiphy, work);
1695
1696 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1697 if (list_empty(head: &work->entry))
1698 list_add_tail(new: &work->entry, head: &rdev->wiphy_work_list);
1699 spin_unlock_irqrestore(lock: &rdev->wiphy_work_lock, flags);
1700
1701 queue_work(wq: system_unbound_wq, work: &rdev->wiphy_work);
1702}
1703EXPORT_SYMBOL_GPL(wiphy_work_queue);
1704
1705void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
1706{
1707 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1708 unsigned long flags;
1709
1710 lockdep_assert_held(&wiphy->mtx);
1711
1712 trace_wiphy_work_cancel(wiphy, work);
1713
1714 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1715 if (!list_empty(head: &work->entry))
1716 list_del_init(entry: &work->entry);
1717 spin_unlock_irqrestore(lock: &rdev->wiphy_work_lock, flags);
1718}
1719EXPORT_SYMBOL_GPL(wiphy_work_cancel);
1720
1721void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
1722{
1723 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1724 unsigned long flags;
1725 bool run;
1726
1727 trace_wiphy_work_flush(wiphy, work);
1728
1729 spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1730 run = !work || !list_empty(head: &work->entry);
1731 spin_unlock_irqrestore(lock: &rdev->wiphy_work_lock, flags);
1732
1733 if (run)
1734 cfg80211_process_wiphy_works(rdev, end: work);
1735}
1736EXPORT_SYMBOL_GPL(wiphy_work_flush);
1737
1738void wiphy_delayed_work_timer(struct timer_list *t)
1739{
1740 struct wiphy_delayed_work *dwork = timer_container_of(dwork, t, timer);
1741
1742 wiphy_work_queue(dwork->wiphy, &dwork->work);
1743}
1744EXPORT_SYMBOL(wiphy_delayed_work_timer);
1745
1746void wiphy_delayed_work_queue(struct wiphy *wiphy,
1747 struct wiphy_delayed_work *dwork,
1748 unsigned long delay)
1749{
1750 trace_wiphy_delayed_work_queue(wiphy, work: &dwork->work, delay);
1751
1752 if (!delay) {
1753 timer_delete(timer: &dwork->timer);
1754 wiphy_work_queue(wiphy, &dwork->work);
1755 return;
1756 }
1757
1758 dwork->wiphy = wiphy;
1759 mod_timer(timer: &dwork->timer, expires: jiffies + delay);
1760}
1761EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
1762
1763void wiphy_delayed_work_cancel(struct wiphy *wiphy,
1764 struct wiphy_delayed_work *dwork)
1765{
1766 lockdep_assert_held(&wiphy->mtx);
1767
1768 timer_delete_sync(timer: &dwork->timer);
1769 wiphy_work_cancel(wiphy, &dwork->work);
1770}
1771EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
1772
1773void wiphy_delayed_work_flush(struct wiphy *wiphy,
1774 struct wiphy_delayed_work *dwork)
1775{
1776 lockdep_assert_held(&wiphy->mtx);
1777
1778 timer_delete_sync(timer: &dwork->timer);
1779 wiphy_work_flush(wiphy, &dwork->work);
1780}
1781EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
1782
1783bool wiphy_delayed_work_pending(struct wiphy *wiphy,
1784 struct wiphy_delayed_work *dwork)
1785{
1786 return timer_pending(timer: &dwork->timer);
1787}
1788EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
1789
1790static int __init cfg80211_init(void)
1791{
1792 int err;
1793
1794 err = register_pernet_device(&cfg80211_pernet_ops);
1795 if (err)
1796 goto out_fail_pernet;
1797
1798 err = wiphy_sysfs_init();
1799 if (err)
1800 goto out_fail_sysfs;
1801
1802 err = register_netdevice_notifier(nb: &cfg80211_netdev_notifier);
1803 if (err)
1804 goto out_fail_notifier;
1805
1806 err = nl80211_init();
1807 if (err)
1808 goto out_fail_nl80211;
1809
1810 ieee80211_debugfs_dir = debugfs_create_dir(name: "ieee80211", NULL);
1811
1812 err = regulatory_init();
1813 if (err)
1814 goto out_fail_reg;
1815
1816 cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1817 if (!cfg80211_wq) {
1818 err = -ENOMEM;
1819 goto out_fail_wq;
1820 }
1821
1822 return 0;
1823
1824out_fail_wq:
1825 regulatory_exit();
1826out_fail_reg:
1827 debugfs_remove(dentry: ieee80211_debugfs_dir);
1828 nl80211_exit();
1829out_fail_nl80211:
1830 unregister_netdevice_notifier(nb: &cfg80211_netdev_notifier);
1831out_fail_notifier:
1832 wiphy_sysfs_exit();
1833out_fail_sysfs:
1834 unregister_pernet_device(&cfg80211_pernet_ops);
1835out_fail_pernet:
1836 return err;
1837}
1838fs_initcall(cfg80211_init);
1839
1840static void __exit cfg80211_exit(void)
1841{
1842 debugfs_remove(dentry: ieee80211_debugfs_dir);
1843 nl80211_exit();
1844 unregister_netdevice_notifier(nb: &cfg80211_netdev_notifier);
1845 wiphy_sysfs_exit();
1846 regulatory_exit();
1847 unregister_pernet_device(&cfg80211_pernet_ops);
1848 destroy_workqueue(wq: cfg80211_wq);
1849}
1850module_exit(cfg80211_exit);
1851