xref: /linux/net/wireless/core.c (revision a382a34276cb94d1cdc620b622dd85c55589a166)
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->rfkill_block);
1216 	cancel_work_sync(&rdev->conn_work);
1217 	flush_work(&rdev->event_work);
1218 	cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
1219 	cancel_delayed_work_sync(&rdev->background_cac_done_wk);
1220 	flush_work(&rdev->destroy_work);
1221 	flush_work(&rdev->propagate_radar_detect_wk);
1222 	flush_work(&rdev->propagate_cac_done_wk);
1223 	flush_work(&rdev->mgmt_registrations_update_wk);
1224 	flush_work(&rdev->background_cac_abort_wk);
1225 
1226 	cfg80211_rdev_free_wowlan(rdev);
1227 	cfg80211_free_coalesce(rdev->coalesce);
1228 	rdev->coalesce = NULL;
1229 }
1230 EXPORT_SYMBOL(wiphy_unregister);
1231 
1232 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
1233 {
1234 	struct cfg80211_internal_bss *scan, *tmp;
1235 	struct cfg80211_beacon_registration *reg, *treg;
1236 	unsigned long flags;
1237 
1238 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1239 	WARN_ON(!list_empty(&rdev->wiphy_work_list));
1240 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1241 	cancel_work_sync(&rdev->wiphy_work);
1242 
1243 	rfkill_destroy(rdev->wiphy.rfkill);
1244 	list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
1245 		list_del(&reg->list);
1246 		kfree(reg);
1247 	}
1248 	list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
1249 		cfg80211_put_bss(&rdev->wiphy, &scan->pub);
1250 	mutex_destroy(&rdev->wiphy.mtx);
1251 
1252 	/*
1253 	 * The 'regd' can only be non-NULL if we never finished
1254 	 * initializing the wiphy and thus never went through the
1255 	 * unregister path - e.g. in failure scenarios. Thus, it
1256 	 * cannot have been visible to anyone if non-NULL, so we
1257 	 * can just free it here.
1258 	 */
1259 	kfree(rcu_dereference_raw(rdev->wiphy.regd));
1260 
1261 	kfree(rdev);
1262 }
1263 
1264 void wiphy_free(struct wiphy *wiphy)
1265 {
1266 	kfree(wiphy->radio_cfg);
1267 	put_device(&wiphy->dev);
1268 }
1269 EXPORT_SYMBOL(wiphy_free);
1270 
1271 void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
1272 				      enum rfkill_hard_block_reasons reason)
1273 {
1274 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1275 
1276 	if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason))
1277 		schedule_work(&rdev->rfkill_block);
1278 }
1279 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
1280 
1281 static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
1282 				      bool unregister_netdev)
1283 {
1284 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1285 	struct cfg80211_cqm_config *cqm_config;
1286 	unsigned int link_id;
1287 
1288 	ASSERT_RTNL();
1289 	lockdep_assert_held(&rdev->wiphy.mtx);
1290 
1291 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1292 
1293 	wdev->registered = false;
1294 
1295 	if (wdev->netdev) {
1296 		sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211");
1297 		if (unregister_netdev)
1298 			unregister_netdevice(wdev->netdev);
1299 	}
1300 
1301 	list_del_rcu(&wdev->list);
1302 	synchronize_net();
1303 	rdev->devlist_generation++;
1304 
1305 	cfg80211_mlme_purge_registrations(wdev);
1306 
1307 	switch (wdev->iftype) {
1308 	case NL80211_IFTYPE_P2P_DEVICE:
1309 		cfg80211_stop_p2p_device(rdev, wdev);
1310 		break;
1311 	case NL80211_IFTYPE_NAN:
1312 		cfg80211_stop_nan(rdev, wdev);
1313 		break;
1314 	default:
1315 		break;
1316 	}
1317 
1318 #ifdef CONFIG_CFG80211_WEXT
1319 	kfree_sensitive(wdev->wext.keys);
1320 	wdev->wext.keys = NULL;
1321 #endif
1322 	wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work);
1323 	/* deleted from the list, so can't be found from nl80211 any more */
1324 	cqm_config = rcu_access_pointer(wdev->cqm_config);
1325 	kfree_rcu(cqm_config, rcu_head);
1326 	RCU_INIT_POINTER(wdev->cqm_config, NULL);
1327 
1328 	/*
1329 	 * Ensure that all events have been processed and
1330 	 * freed.
1331 	 */
1332 	cfg80211_process_wdev_events(wdev);
1333 
1334 	if (wdev->iftype == NL80211_IFTYPE_STATION ||
1335 	    wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1336 		for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
1337 			struct cfg80211_internal_bss *curbss;
1338 
1339 			curbss = wdev->links[link_id].client.current_bss;
1340 
1341 			if (WARN_ON(curbss)) {
1342 				cfg80211_unhold_bss(curbss);
1343 				cfg80211_put_bss(wdev->wiphy, &curbss->pub);
1344 				wdev->links[link_id].client.current_bss = NULL;
1345 			}
1346 		}
1347 	}
1348 
1349 	wdev->connected = false;
1350 }
1351 
1352 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1353 {
1354 	_cfg80211_unregister_wdev(wdev, true);
1355 }
1356 EXPORT_SYMBOL(cfg80211_unregister_wdev);
1357 
1358 static const struct device_type wiphy_type = {
1359 	.name	= "wlan",
1360 };
1361 
1362 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1363 			       enum nl80211_iftype iftype, int num)
1364 {
1365 	lockdep_assert_held(&rdev->wiphy.mtx);
1366 
1367 	rdev->num_running_ifaces += num;
1368 	if (iftype == NL80211_IFTYPE_MONITOR)
1369 		rdev->num_running_monitor_ifaces += num;
1370 }
1371 
1372 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1373 		    struct wireless_dev *wdev,
1374 		    int link_id)
1375 {
1376 	struct net_device *dev = wdev->netdev;
1377 	struct cfg80211_sched_scan_request *pos, *tmp;
1378 
1379 	lockdep_assert_held(&rdev->wiphy.mtx);
1380 
1381 	cfg80211_pmsr_wdev_down(wdev);
1382 
1383 	cfg80211_stop_radar_detection(wdev);
1384 	cfg80211_stop_background_radar_detection(wdev);
1385 
1386 	switch (wdev->iftype) {
1387 	case NL80211_IFTYPE_ADHOC:
1388 		cfg80211_leave_ibss(rdev, dev, true);
1389 		break;
1390 	case NL80211_IFTYPE_P2P_CLIENT:
1391 	case NL80211_IFTYPE_STATION:
1392 		list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1393 					 list) {
1394 			if (dev == pos->dev)
1395 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1396 		}
1397 
1398 #ifdef CONFIG_CFG80211_WEXT
1399 		kfree(wdev->wext.ie);
1400 		wdev->wext.ie = NULL;
1401 		wdev->wext.ie_len = 0;
1402 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1403 #endif
1404 		cfg80211_disconnect(rdev, dev,
1405 				    WLAN_REASON_DEAUTH_LEAVING, true);
1406 		break;
1407 	case NL80211_IFTYPE_MESH_POINT:
1408 		cfg80211_leave_mesh(rdev, dev);
1409 		break;
1410 	case NL80211_IFTYPE_AP:
1411 	case NL80211_IFTYPE_P2P_GO:
1412 		cfg80211_stop_ap(rdev, dev, link_id, true);
1413 		break;
1414 	case NL80211_IFTYPE_OCB:
1415 		cfg80211_leave_ocb(rdev, dev);
1416 		break;
1417 	case NL80211_IFTYPE_P2P_DEVICE:
1418 		cfg80211_stop_p2p_device(rdev, wdev);
1419 		break;
1420 	case NL80211_IFTYPE_NAN:
1421 		cfg80211_stop_nan(rdev, wdev);
1422 		break;
1423 	case NL80211_IFTYPE_AP_VLAN:
1424 	case NL80211_IFTYPE_MONITOR:
1425 		/* nothing to do */
1426 		break;
1427 	case NL80211_IFTYPE_UNSPECIFIED:
1428 	case NL80211_IFTYPE_WDS:
1429 	case NUM_NL80211_IFTYPES:
1430 		/* invalid */
1431 		break;
1432 	}
1433 }
1434 
1435 void cfg80211_stop_link(struct wiphy *wiphy, struct wireless_dev *wdev,
1436 			int link_id, gfp_t gfp)
1437 {
1438 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1439 	struct cfg80211_event *ev;
1440 	unsigned long flags;
1441 
1442 	/* Only AP/GO interfaces may have a specific link_id */
1443 	if (WARN_ON_ONCE(link_id != -1 &&
1444 			 wdev->iftype != NL80211_IFTYPE_AP &&
1445 			 wdev->iftype != NL80211_IFTYPE_P2P_GO))
1446 		link_id = -1;
1447 
1448 	trace_cfg80211_stop_link(wiphy, wdev, link_id);
1449 
1450 	ev = kzalloc(sizeof(*ev), gfp);
1451 	if (!ev)
1452 		return;
1453 
1454 	ev->type = EVENT_STOPPED;
1455 	ev->link_id = link_id;
1456 
1457 	spin_lock_irqsave(&wdev->event_lock, flags);
1458 	list_add_tail(&ev->list, &wdev->event_list);
1459 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1460 	queue_work(cfg80211_wq, &rdev->event_work);
1461 }
1462 EXPORT_SYMBOL(cfg80211_stop_link);
1463 
1464 void cfg80211_init_wdev(struct wireless_dev *wdev)
1465 {
1466 	INIT_LIST_HEAD(&wdev->event_list);
1467 	spin_lock_init(&wdev->event_lock);
1468 	INIT_LIST_HEAD(&wdev->mgmt_registrations);
1469 	INIT_LIST_HEAD(&wdev->pmsr_list);
1470 	spin_lock_init(&wdev->pmsr_lock);
1471 	INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
1472 
1473 #ifdef CONFIG_CFG80211_WEXT
1474 	wdev->wext.default_key = -1;
1475 	wdev->wext.default_mgmt_key = -1;
1476 	wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1477 #endif
1478 
1479 	wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work);
1480 
1481 	if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1482 		wdev->ps = true;
1483 	else
1484 		wdev->ps = false;
1485 	/* allow mac80211 to determine the timeout */
1486 	wdev->ps_timeout = -1;
1487 
1488 	wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
1489 
1490 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1491 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1492 	     wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1493 		wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
1494 
1495 	INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1496 }
1497 
1498 void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
1499 			    struct wireless_dev *wdev)
1500 {
1501 	ASSERT_RTNL();
1502 	lockdep_assert_held(&rdev->wiphy.mtx);
1503 
1504 	/*
1505 	 * We get here also when the interface changes network namespaces,
1506 	 * as it's registered into the new one, but we don't want it to
1507 	 * change ID in that case. Checking if the ID is already assigned
1508 	 * works, because 0 isn't considered a valid ID and the memory is
1509 	 * 0-initialized.
1510 	 */
1511 	if (!wdev->identifier)
1512 		wdev->identifier = ++rdev->wdev_id;
1513 	list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1514 	rdev->devlist_generation++;
1515 	wdev->registered = true;
1516 
1517 	if (wdev->netdev &&
1518 	    sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj,
1519 			      "phy80211"))
1520 		pr_err("failed to add phy80211 symlink to netdev!\n");
1521 
1522 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1523 }
1524 
1525 int cfg80211_register_netdevice(struct net_device *dev)
1526 {
1527 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1528 	struct cfg80211_registered_device *rdev;
1529 	int ret;
1530 
1531 	ASSERT_RTNL();
1532 
1533 	if (WARN_ON(!wdev))
1534 		return -EINVAL;
1535 
1536 	rdev = wiphy_to_rdev(wdev->wiphy);
1537 
1538 	lockdep_assert_held(&rdev->wiphy.mtx);
1539 
1540 	/* we'll take care of this */
1541 	wdev->registered = true;
1542 	wdev->registering = true;
1543 	ret = register_netdevice(dev);
1544 	if (ret)
1545 		goto out;
1546 
1547 	cfg80211_register_wdev(rdev, wdev);
1548 	ret = 0;
1549 out:
1550 	wdev->registering = false;
1551 	if (ret)
1552 		wdev->registered = false;
1553 	return ret;
1554 }
1555 EXPORT_SYMBOL(cfg80211_register_netdevice);
1556 
1557 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1558 					 unsigned long state, void *ptr)
1559 {
1560 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1561 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1562 	struct cfg80211_registered_device *rdev;
1563 	struct cfg80211_sched_scan_request *pos, *tmp;
1564 
1565 	if (!wdev)
1566 		return NOTIFY_DONE;
1567 
1568 	rdev = wiphy_to_rdev(wdev->wiphy);
1569 
1570 	WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1571 
1572 	switch (state) {
1573 	case NETDEV_POST_INIT:
1574 		SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1575 		wdev->netdev = dev;
1576 		/* can only change netns with wiphy */
1577 		dev->netns_immutable = true;
1578 
1579 		cfg80211_init_wdev(wdev);
1580 		break;
1581 	case NETDEV_REGISTER:
1582 		if (!wdev->registered) {
1583 			guard(wiphy)(&rdev->wiphy);
1584 
1585 			cfg80211_register_wdev(rdev, wdev);
1586 		}
1587 		break;
1588 	case NETDEV_UNREGISTER:
1589 		/*
1590 		 * It is possible to get NETDEV_UNREGISTER multiple times,
1591 		 * so check wdev->registered.
1592 		 */
1593 		if (wdev->registered && !wdev->registering) {
1594 			guard(wiphy)(&rdev->wiphy);
1595 
1596 			_cfg80211_unregister_wdev(wdev, false);
1597 		}
1598 		break;
1599 	case NETDEV_GOING_DOWN:
1600 		scoped_guard(wiphy, &rdev->wiphy) {
1601 			cfg80211_leave(rdev, wdev, -1);
1602 			cfg80211_remove_links(wdev);
1603 		}
1604 		/* since we just did cfg80211_leave() nothing to do there */
1605 		cancel_work_sync(&wdev->disconnect_wk);
1606 		cancel_work_sync(&wdev->pmsr_free_wk);
1607 		break;
1608 	case NETDEV_DOWN:
1609 		wiphy_lock(&rdev->wiphy);
1610 		cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1611 		if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
1612 			if (WARN_ON(!rdev->scan_req->notified &&
1613 				    (!rdev->int_scan_req ||
1614 				     !rdev->int_scan_req->notified)))
1615 				rdev->scan_req->info.aborted = true;
1616 			___cfg80211_scan_done(rdev, false);
1617 		}
1618 
1619 		list_for_each_entry_safe(pos, tmp,
1620 					 &rdev->sched_scan_req_list, list) {
1621 			if (WARN_ON(pos->dev == wdev->netdev))
1622 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1623 		}
1624 
1625 		rdev->opencount--;
1626 		wiphy_unlock(&rdev->wiphy);
1627 		wake_up(&rdev->dev_wait);
1628 		break;
1629 	case NETDEV_UP:
1630 		wiphy_lock(&rdev->wiphy);
1631 		cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1632 		switch (wdev->iftype) {
1633 #ifdef CONFIG_CFG80211_WEXT
1634 		case NL80211_IFTYPE_ADHOC:
1635 			cfg80211_ibss_wext_join(rdev, wdev);
1636 			break;
1637 		case NL80211_IFTYPE_STATION:
1638 			cfg80211_mgd_wext_connect(rdev, wdev);
1639 			break;
1640 #endif
1641 #ifdef CONFIG_MAC80211_MESH
1642 		case NL80211_IFTYPE_MESH_POINT:
1643 			{
1644 				/* backward compat code... */
1645 				struct mesh_setup setup;
1646 				memcpy(&setup, &default_mesh_setup,
1647 						sizeof(setup));
1648 				 /* back compat only needed for mesh_id */
1649 				setup.mesh_id = wdev->u.mesh.id;
1650 				setup.mesh_id_len = wdev->u.mesh.id_up_len;
1651 				if (wdev->u.mesh.id_up_len)
1652 					__cfg80211_join_mesh(rdev, dev,
1653 							&setup,
1654 							&default_mesh_config);
1655 				break;
1656 			}
1657 #endif
1658 		default:
1659 			break;
1660 		}
1661 		rdev->opencount++;
1662 
1663 		/*
1664 		 * Configure power management to the driver here so that its
1665 		 * correctly set also after interface type changes etc.
1666 		 */
1667 		if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1668 		     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1669 		    rdev->ops->set_power_mgmt &&
1670 		    rdev_set_power_mgmt(rdev, dev, wdev->ps,
1671 					wdev->ps_timeout)) {
1672 			/* assume this means it's off */
1673 			wdev->ps = false;
1674 		}
1675 		wiphy_unlock(&rdev->wiphy);
1676 		break;
1677 	case NETDEV_PRE_UP:
1678 		if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
1679 					     wdev->use_4addr, 0))
1680 			return notifier_from_errno(-EOPNOTSUPP);
1681 
1682 		if (rfkill_blocked(rdev->wiphy.rfkill))
1683 			return notifier_from_errno(-ERFKILL);
1684 		break;
1685 	default:
1686 		return NOTIFY_DONE;
1687 	}
1688 
1689 	wireless_nlevent_flush();
1690 
1691 	return NOTIFY_OK;
1692 }
1693 
1694 static struct notifier_block cfg80211_netdev_notifier = {
1695 	.notifier_call = cfg80211_netdev_notifier_call,
1696 };
1697 
1698 static void __net_exit cfg80211_pernet_exit(struct net *net)
1699 {
1700 	struct cfg80211_registered_device *rdev;
1701 
1702 	rtnl_lock();
1703 	for_each_rdev(rdev) {
1704 		if (net_eq(wiphy_net(&rdev->wiphy), net))
1705 			WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1706 	}
1707 	rtnl_unlock();
1708 }
1709 
1710 static struct pernet_operations cfg80211_pernet_ops = {
1711 	.exit = cfg80211_pernet_exit,
1712 };
1713 
1714 void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
1715 {
1716 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1717 	unsigned long flags;
1718 
1719 	trace_wiphy_work_queue(wiphy, work);
1720 
1721 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1722 	if (list_empty(&work->entry))
1723 		list_add_tail(&work->entry, &rdev->wiphy_work_list);
1724 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1725 
1726 	queue_work(system_dfl_wq, &rdev->wiphy_work);
1727 }
1728 EXPORT_SYMBOL_GPL(wiphy_work_queue);
1729 
1730 void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
1731 {
1732 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1733 	unsigned long flags;
1734 
1735 	lockdep_assert_held(&wiphy->mtx);
1736 
1737 	trace_wiphy_work_cancel(wiphy, work);
1738 
1739 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1740 	if (!list_empty(&work->entry))
1741 		list_del_init(&work->entry);
1742 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1743 }
1744 EXPORT_SYMBOL_GPL(wiphy_work_cancel);
1745 
1746 void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
1747 {
1748 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1749 	unsigned long flags;
1750 	bool run;
1751 
1752 	trace_wiphy_work_flush(wiphy, work);
1753 
1754 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1755 	run = !work || !list_empty(&work->entry);
1756 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1757 
1758 	if (run)
1759 		cfg80211_process_wiphy_works(rdev, work);
1760 }
1761 EXPORT_SYMBOL_GPL(wiphy_work_flush);
1762 
1763 void wiphy_delayed_work_timer(struct timer_list *t)
1764 {
1765 	struct wiphy_delayed_work *dwork = timer_container_of(dwork, t, timer);
1766 
1767 	wiphy_work_queue(dwork->wiphy, &dwork->work);
1768 }
1769 EXPORT_SYMBOL(wiphy_delayed_work_timer);
1770 
1771 void wiphy_delayed_work_queue(struct wiphy *wiphy,
1772 			      struct wiphy_delayed_work *dwork,
1773 			      unsigned long delay)
1774 {
1775 	trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay);
1776 
1777 	if (!delay) {
1778 		timer_delete(&dwork->timer);
1779 		wiphy_work_queue(wiphy, &dwork->work);
1780 		return;
1781 	}
1782 
1783 	dwork->wiphy = wiphy;
1784 	mod_timer(&dwork->timer, jiffies + delay);
1785 }
1786 EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
1787 
1788 void wiphy_delayed_work_cancel(struct wiphy *wiphy,
1789 			       struct wiphy_delayed_work *dwork)
1790 {
1791 	lockdep_assert_held(&wiphy->mtx);
1792 
1793 	timer_delete_sync(&dwork->timer);
1794 	wiphy_work_cancel(wiphy, &dwork->work);
1795 }
1796 EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
1797 
1798 void wiphy_delayed_work_flush(struct wiphy *wiphy,
1799 			      struct wiphy_delayed_work *dwork)
1800 {
1801 	lockdep_assert_held(&wiphy->mtx);
1802 
1803 	timer_delete_sync(&dwork->timer);
1804 	wiphy_work_flush(wiphy, &dwork->work);
1805 }
1806 EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
1807 
1808 bool wiphy_delayed_work_pending(struct wiphy *wiphy,
1809 				struct wiphy_delayed_work *dwork)
1810 {
1811 	return timer_pending(&dwork->timer);
1812 }
1813 EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
1814 
1815 enum hrtimer_restart wiphy_hrtimer_work_timer(struct hrtimer *t)
1816 {
1817 	struct wiphy_hrtimer_work *hrwork =
1818 		container_of(t, struct wiphy_hrtimer_work, timer);
1819 
1820 	wiphy_work_queue(hrwork->wiphy, &hrwork->work);
1821 
1822 	return HRTIMER_NORESTART;
1823 }
1824 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_timer);
1825 
1826 void wiphy_hrtimer_work_queue(struct wiphy *wiphy,
1827 			      struct wiphy_hrtimer_work *hrwork,
1828 			      ktime_t delay)
1829 {
1830 	trace_wiphy_hrtimer_work_queue(wiphy, &hrwork->work, delay);
1831 
1832 	if (!delay) {
1833 		hrtimer_cancel(&hrwork->timer);
1834 		wiphy_work_queue(wiphy, &hrwork->work);
1835 		return;
1836 	}
1837 
1838 	hrwork->wiphy = wiphy;
1839 	hrtimer_start_range_ns(&hrwork->timer, delay,
1840 			       1000 * NSEC_PER_USEC, HRTIMER_MODE_REL);
1841 }
1842 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_queue);
1843 
1844 void wiphy_hrtimer_work_cancel(struct wiphy *wiphy,
1845 			       struct wiphy_hrtimer_work *hrwork)
1846 {
1847 	lockdep_assert_held(&wiphy->mtx);
1848 
1849 	hrtimer_cancel(&hrwork->timer);
1850 	wiphy_work_cancel(wiphy, &hrwork->work);
1851 }
1852 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_cancel);
1853 
1854 void wiphy_hrtimer_work_flush(struct wiphy *wiphy,
1855 			      struct wiphy_hrtimer_work *hrwork)
1856 {
1857 	lockdep_assert_held(&wiphy->mtx);
1858 
1859 	hrtimer_cancel(&hrwork->timer);
1860 	wiphy_work_flush(wiphy, &hrwork->work);
1861 }
1862 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_flush);
1863 
1864 bool wiphy_hrtimer_work_pending(struct wiphy *wiphy,
1865 				struct wiphy_hrtimer_work *hrwork)
1866 {
1867 	return hrtimer_is_queued(&hrwork->timer);
1868 }
1869 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_pending);
1870 
1871 static int __init cfg80211_init(void)
1872 {
1873 	int err;
1874 
1875 	err = register_pernet_device(&cfg80211_pernet_ops);
1876 	if (err)
1877 		goto out_fail_pernet;
1878 
1879 	err = wiphy_sysfs_init();
1880 	if (err)
1881 		goto out_fail_sysfs;
1882 
1883 	err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1884 	if (err)
1885 		goto out_fail_notifier;
1886 
1887 	err = nl80211_init();
1888 	if (err)
1889 		goto out_fail_nl80211;
1890 
1891 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1892 
1893 	err = regulatory_init();
1894 	if (err)
1895 		goto out_fail_reg;
1896 
1897 	cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1898 	if (!cfg80211_wq) {
1899 		err = -ENOMEM;
1900 		goto out_fail_wq;
1901 	}
1902 
1903 	return 0;
1904 
1905 out_fail_wq:
1906 	regulatory_exit();
1907 out_fail_reg:
1908 	debugfs_remove(ieee80211_debugfs_dir);
1909 	nl80211_exit();
1910 out_fail_nl80211:
1911 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1912 out_fail_notifier:
1913 	wiphy_sysfs_exit();
1914 out_fail_sysfs:
1915 	unregister_pernet_device(&cfg80211_pernet_ops);
1916 out_fail_pernet:
1917 	return err;
1918 }
1919 fs_initcall(cfg80211_init);
1920 
1921 static void __exit cfg80211_exit(void)
1922 {
1923 	debugfs_remove(ieee80211_debugfs_dir);
1924 	nl80211_exit();
1925 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1926 	wiphy_sysfs_exit();
1927 	regulatory_exit();
1928 	unregister_pernet_device(&cfg80211_pernet_ops);
1929 	destroy_workqueue(cfg80211_wq);
1930 }
1931 module_exit(cfg80211_exit);
1932