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