xref: /linux/net/wireless/core.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 = kzalloc_objs(*wiphy->radio_cfg,
1004 						wiphy->n_radio, GFP_KERNEL);
1005 		if (!wiphy->radio_cfg)
1006 			return -ENOMEM;
1007 		/*
1008 		 * Initialize wiphy radio parameters to IEEE 802.11
1009 		 * MIB default values. RTS threshold is disabled by
1010 		 * default with the special -1 value.
1011 		 */
1012 		for (idx = 0; idx < wiphy->n_radio; idx++)
1013 			wiphy->radio_cfg[idx].rts_threshold = (u32)-1;
1014 	}
1015 
1016 	/* check and set up bitrates */
1017 	ieee80211_set_bitrate_flags(wiphy);
1018 
1019 	rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
1020 
1021 	if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_CTWINDOW)
1022 		rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_CTWIN;
1023 	else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)
1024 		rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_CTWINDOW;
1025 	if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_OPPPS)
1026 		rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_OPPPS;
1027 	else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)
1028 		rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_OPPPS;
1029 
1030 	rtnl_lock();
1031 	wiphy_lock(&rdev->wiphy);
1032 	res = device_add(&rdev->wiphy.dev);
1033 	if (res) {
1034 		wiphy_unlock(&rdev->wiphy);
1035 		rtnl_unlock();
1036 		return res;
1037 	}
1038 
1039 	list_add_rcu(&rdev->list, &cfg80211_rdev_list);
1040 	cfg80211_rdev_list_generation++;
1041 
1042 	/* add to debugfs */
1043 	rdev->wiphy.debugfsdir = debugfs_create_dir(wiphy_name(&rdev->wiphy),
1044 						    ieee80211_debugfs_dir);
1045 	if (wiphy->n_radio > 0) {
1046 		int idx;
1047 		char radio_name[RADIO_DEBUGFSDIR_MAX_LEN];
1048 
1049 		for (idx = 0; idx < wiphy->n_radio; idx++) {
1050 			scnprintf(radio_name, sizeof(radio_name), "radio%d",
1051 				  idx);
1052 			wiphy->radio_cfg[idx].radio_debugfsdir =
1053 				debugfs_create_dir(radio_name,
1054 						   rdev->wiphy.debugfsdir);
1055 		}
1056 	}
1057 
1058 	cfg80211_debugfs_rdev_add(rdev);
1059 	nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
1060 	wiphy_unlock(&rdev->wiphy);
1061 
1062 	/* set up regulatory info */
1063 	wiphy_regulatory_register(wiphy);
1064 
1065 	if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
1066 		struct regulatory_request request = {
1067 			.wiphy_idx = get_wiphy_idx(wiphy),
1068 			.initiator = NL80211_REGDOM_SET_BY_DRIVER,
1069 			.alpha2[0] = '9',
1070 			.alpha2[1] = '9',
1071 		};
1072 
1073 		nl80211_send_reg_change_event(&request);
1074 	}
1075 
1076 	/* Check that nobody globally advertises any capabilities they do not
1077 	 * advertise on all possible interface types.
1078 	 */
1079 	if (wiphy->extended_capabilities_len &&
1080 	    wiphy->num_iftype_ext_capab &&
1081 	    wiphy->iftype_ext_capab) {
1082 		u8 supported_on_all, j;
1083 		const struct wiphy_iftype_ext_capab *capab;
1084 
1085 		capab = wiphy->iftype_ext_capab;
1086 		for (j = 0; j < wiphy->extended_capabilities_len; j++) {
1087 			if (capab[0].extended_capabilities_len > j)
1088 				supported_on_all =
1089 					capab[0].extended_capabilities[j];
1090 			else
1091 				supported_on_all = 0x00;
1092 			for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
1093 				if (j >= capab[i].extended_capabilities_len) {
1094 					supported_on_all = 0x00;
1095 					break;
1096 				}
1097 				supported_on_all &=
1098 					capab[i].extended_capabilities[j];
1099 			}
1100 			if (WARN_ON(wiphy->extended_capabilities[j] &
1101 				    ~supported_on_all))
1102 				break;
1103 		}
1104 	}
1105 
1106 	rdev->wiphy.registered = true;
1107 	rtnl_unlock();
1108 
1109 	res = rfkill_register(rdev->wiphy.rfkill);
1110 	if (res) {
1111 		rfkill_destroy(rdev->wiphy.rfkill);
1112 		rdev->wiphy.rfkill = NULL;
1113 		wiphy_unregister(&rdev->wiphy);
1114 		return res;
1115 	}
1116 
1117 	return 0;
1118 }
1119 EXPORT_SYMBOL(wiphy_register);
1120 
1121 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
1122 {
1123 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1124 
1125 	if (!rdev->ops->rfkill_poll)
1126 		return;
1127 	rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
1128 	rfkill_resume_polling(wiphy->rfkill);
1129 }
1130 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
1131 
1132 void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev,
1133 				  struct wiphy_work *end)
1134 {
1135 	unsigned int runaway_limit = 100;
1136 	unsigned long flags;
1137 
1138 	lockdep_assert_held(&rdev->wiphy.mtx);
1139 
1140 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1141 	while (!list_empty(&rdev->wiphy_work_list)) {
1142 		struct wiphy_work *wk;
1143 
1144 		wk = list_first_entry(&rdev->wiphy_work_list,
1145 				      struct wiphy_work, entry);
1146 		list_del_init(&wk->entry);
1147 		spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1148 
1149 		trace_wiphy_work_run(&rdev->wiphy, wk);
1150 		wk->func(&rdev->wiphy, wk);
1151 
1152 		spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1153 
1154 		if (wk == end)
1155 			break;
1156 
1157 		if (WARN_ON(--runaway_limit == 0))
1158 			INIT_LIST_HEAD(&rdev->wiphy_work_list);
1159 	}
1160 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1161 }
1162 
1163 void wiphy_unregister(struct wiphy *wiphy)
1164 {
1165 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1166 
1167 	wait_event(rdev->dev_wait, ({
1168 		int __count;
1169 		wiphy_lock(&rdev->wiphy);
1170 		__count = rdev->opencount;
1171 		wiphy_unlock(&rdev->wiphy);
1172 		__count == 0; }));
1173 
1174 	if (rdev->wiphy.rfkill)
1175 		rfkill_unregister(rdev->wiphy.rfkill);
1176 
1177 	rtnl_lock();
1178 	wiphy_lock(&rdev->wiphy);
1179 	nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
1180 	rdev->wiphy.registered = false;
1181 
1182 	WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
1183 
1184 	/*
1185 	 * First remove the hardware from everywhere, this makes
1186 	 * it impossible to find from userspace.
1187 	 */
1188 	debugfs_remove_recursive(rdev->wiphy.debugfsdir);
1189 	list_del_rcu(&rdev->list);
1190 	synchronize_rcu();
1191 
1192 	/*
1193 	 * If this device got a regulatory hint tell core its
1194 	 * free to listen now to a new shiny device regulatory hint
1195 	 */
1196 	wiphy_regulatory_deregister(wiphy);
1197 
1198 	cfg80211_rdev_list_generation++;
1199 	device_del(&rdev->wiphy.dev);
1200 
1201 #ifdef CONFIG_PM
1202 	if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
1203 		rdev_set_wakeup(rdev, false);
1204 #endif
1205 
1206 	/* surely nothing is reachable now, clean up work */
1207 	cfg80211_process_wiphy_works(rdev, NULL);
1208 	wiphy_unlock(&rdev->wiphy);
1209 	rtnl_unlock();
1210 
1211 	/* this has nothing to do now but make sure it's gone */
1212 	cancel_work_sync(&rdev->wiphy_work);
1213 
1214 	cancel_work_sync(&rdev->conn_work);
1215 	flush_work(&rdev->event_work);
1216 	cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
1217 	cancel_delayed_work_sync(&rdev->background_cac_done_wk);
1218 	flush_work(&rdev->destroy_work);
1219 	flush_work(&rdev->propagate_radar_detect_wk);
1220 	flush_work(&rdev->propagate_cac_done_wk);
1221 	flush_work(&rdev->mgmt_registrations_update_wk);
1222 	flush_work(&rdev->background_cac_abort_wk);
1223 
1224 	cfg80211_rdev_free_wowlan(rdev);
1225 	cfg80211_free_coalesce(rdev->coalesce);
1226 	rdev->coalesce = NULL;
1227 }
1228 EXPORT_SYMBOL(wiphy_unregister);
1229 
1230 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
1231 {
1232 	struct cfg80211_internal_bss *scan, *tmp;
1233 	struct cfg80211_beacon_registration *reg, *treg;
1234 	unsigned long flags;
1235 
1236 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1237 	WARN_ON(!list_empty(&rdev->wiphy_work_list));
1238 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1239 	cancel_work_sync(&rdev->wiphy_work);
1240 
1241 	rfkill_destroy(rdev->wiphy.rfkill);
1242 	list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
1243 		list_del(&reg->list);
1244 		kfree(reg);
1245 	}
1246 	list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
1247 		cfg80211_put_bss(&rdev->wiphy, &scan->pub);
1248 	mutex_destroy(&rdev->wiphy.mtx);
1249 
1250 	/*
1251 	 * The 'regd' can only be non-NULL if we never finished
1252 	 * initializing the wiphy and thus never went through the
1253 	 * unregister path - e.g. in failure scenarios. Thus, it
1254 	 * cannot have been visible to anyone if non-NULL, so we
1255 	 * can just free it here.
1256 	 */
1257 	kfree(rcu_dereference_raw(rdev->wiphy.regd));
1258 
1259 	kfree(rdev);
1260 }
1261 
1262 void wiphy_free(struct wiphy *wiphy)
1263 {
1264 	kfree(wiphy->radio_cfg);
1265 	put_device(&wiphy->dev);
1266 }
1267 EXPORT_SYMBOL(wiphy_free);
1268 
1269 void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
1270 				      enum rfkill_hard_block_reasons reason)
1271 {
1272 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1273 
1274 	if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason))
1275 		schedule_work(&rdev->rfkill_block);
1276 }
1277 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
1278 
1279 static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
1280 				      bool unregister_netdev)
1281 {
1282 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1283 	struct cfg80211_cqm_config *cqm_config;
1284 	unsigned int link_id;
1285 
1286 	ASSERT_RTNL();
1287 	lockdep_assert_held(&rdev->wiphy.mtx);
1288 
1289 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1290 
1291 	wdev->registered = false;
1292 
1293 	if (wdev->netdev) {
1294 		sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211");
1295 		if (unregister_netdev)
1296 			unregister_netdevice(wdev->netdev);
1297 	}
1298 
1299 	list_del_rcu(&wdev->list);
1300 	synchronize_net();
1301 	rdev->devlist_generation++;
1302 
1303 	cfg80211_mlme_purge_registrations(wdev);
1304 
1305 	switch (wdev->iftype) {
1306 	case NL80211_IFTYPE_P2P_DEVICE:
1307 		cfg80211_stop_p2p_device(rdev, wdev);
1308 		break;
1309 	case NL80211_IFTYPE_NAN:
1310 		cfg80211_stop_nan(rdev, wdev);
1311 		break;
1312 	default:
1313 		break;
1314 	}
1315 
1316 #ifdef CONFIG_CFG80211_WEXT
1317 	kfree_sensitive(wdev->wext.keys);
1318 	wdev->wext.keys = NULL;
1319 #endif
1320 	wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work);
1321 	/* deleted from the list, so can't be found from nl80211 any more */
1322 	cqm_config = rcu_access_pointer(wdev->cqm_config);
1323 	kfree_rcu(cqm_config, rcu_head);
1324 	RCU_INIT_POINTER(wdev->cqm_config, NULL);
1325 
1326 	/*
1327 	 * Ensure that all events have been processed and
1328 	 * freed.
1329 	 */
1330 	cfg80211_process_wdev_events(wdev);
1331 
1332 	if (wdev->iftype == NL80211_IFTYPE_STATION ||
1333 	    wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1334 		for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
1335 			struct cfg80211_internal_bss *curbss;
1336 
1337 			curbss = wdev->links[link_id].client.current_bss;
1338 
1339 			if (WARN_ON(curbss)) {
1340 				cfg80211_unhold_bss(curbss);
1341 				cfg80211_put_bss(wdev->wiphy, &curbss->pub);
1342 				wdev->links[link_id].client.current_bss = NULL;
1343 			}
1344 		}
1345 	}
1346 
1347 	wdev->connected = false;
1348 }
1349 
1350 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1351 {
1352 	_cfg80211_unregister_wdev(wdev, true);
1353 }
1354 EXPORT_SYMBOL(cfg80211_unregister_wdev);
1355 
1356 static const struct device_type wiphy_type = {
1357 	.name	= "wlan",
1358 };
1359 
1360 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1361 			       enum nl80211_iftype iftype, int num)
1362 {
1363 	lockdep_assert_held(&rdev->wiphy.mtx);
1364 
1365 	rdev->num_running_ifaces += num;
1366 	if (iftype == NL80211_IFTYPE_MONITOR)
1367 		rdev->num_running_monitor_ifaces += num;
1368 }
1369 
1370 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1371 		    struct wireless_dev *wdev,
1372 		    int link_id)
1373 {
1374 	struct net_device *dev = wdev->netdev;
1375 	struct cfg80211_sched_scan_request *pos, *tmp;
1376 
1377 	lockdep_assert_held(&rdev->wiphy.mtx);
1378 
1379 	cfg80211_pmsr_wdev_down(wdev);
1380 
1381 	cfg80211_stop_radar_detection(wdev);
1382 	cfg80211_stop_background_radar_detection(wdev);
1383 
1384 	switch (wdev->iftype) {
1385 	case NL80211_IFTYPE_ADHOC:
1386 		cfg80211_leave_ibss(rdev, dev, true);
1387 		break;
1388 	case NL80211_IFTYPE_P2P_CLIENT:
1389 	case NL80211_IFTYPE_STATION:
1390 		list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1391 					 list) {
1392 			if (dev == pos->dev)
1393 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1394 		}
1395 
1396 #ifdef CONFIG_CFG80211_WEXT
1397 		kfree(wdev->wext.ie);
1398 		wdev->wext.ie = NULL;
1399 		wdev->wext.ie_len = 0;
1400 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1401 #endif
1402 		cfg80211_disconnect(rdev, dev,
1403 				    WLAN_REASON_DEAUTH_LEAVING, true);
1404 		break;
1405 	case NL80211_IFTYPE_MESH_POINT:
1406 		cfg80211_leave_mesh(rdev, dev);
1407 		break;
1408 	case NL80211_IFTYPE_AP:
1409 	case NL80211_IFTYPE_P2P_GO:
1410 		cfg80211_stop_ap(rdev, dev, link_id, true);
1411 		break;
1412 	case NL80211_IFTYPE_OCB:
1413 		cfg80211_leave_ocb(rdev, dev);
1414 		break;
1415 	case NL80211_IFTYPE_P2P_DEVICE:
1416 		cfg80211_stop_p2p_device(rdev, wdev);
1417 		break;
1418 	case NL80211_IFTYPE_NAN:
1419 		cfg80211_stop_nan(rdev, wdev);
1420 		break;
1421 	case NL80211_IFTYPE_AP_VLAN:
1422 	case NL80211_IFTYPE_MONITOR:
1423 		/* nothing to do */
1424 		break;
1425 	case NL80211_IFTYPE_UNSPECIFIED:
1426 	case NL80211_IFTYPE_WDS:
1427 	case NUM_NL80211_IFTYPES:
1428 		/* invalid */
1429 		break;
1430 	}
1431 }
1432 
1433 void cfg80211_stop_link(struct wiphy *wiphy, struct wireless_dev *wdev,
1434 			int link_id, gfp_t gfp)
1435 {
1436 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1437 	struct cfg80211_event *ev;
1438 	unsigned long flags;
1439 
1440 	/* Only AP/GO interfaces may have a specific link_id */
1441 	if (WARN_ON_ONCE(link_id != -1 &&
1442 			 wdev->iftype != NL80211_IFTYPE_AP &&
1443 			 wdev->iftype != NL80211_IFTYPE_P2P_GO))
1444 		link_id = -1;
1445 
1446 	trace_cfg80211_stop_link(wiphy, wdev, link_id);
1447 
1448 	ev = kzalloc_obj(*ev, gfp);
1449 	if (!ev)
1450 		return;
1451 
1452 	ev->type = EVENT_STOPPED;
1453 	ev->link_id = link_id;
1454 
1455 	spin_lock_irqsave(&wdev->event_lock, flags);
1456 	list_add_tail(&ev->list, &wdev->event_list);
1457 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1458 	queue_work(cfg80211_wq, &rdev->event_work);
1459 }
1460 EXPORT_SYMBOL(cfg80211_stop_link);
1461 
1462 void cfg80211_init_wdev(struct wireless_dev *wdev)
1463 {
1464 	INIT_LIST_HEAD(&wdev->event_list);
1465 	spin_lock_init(&wdev->event_lock);
1466 	INIT_LIST_HEAD(&wdev->mgmt_registrations);
1467 	INIT_LIST_HEAD(&wdev->pmsr_list);
1468 	spin_lock_init(&wdev->pmsr_lock);
1469 	INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
1470 
1471 #ifdef CONFIG_CFG80211_WEXT
1472 	wdev->wext.default_key = -1;
1473 	wdev->wext.default_mgmt_key = -1;
1474 	wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1475 #endif
1476 
1477 	wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work);
1478 
1479 	if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1480 		wdev->ps = true;
1481 	else
1482 		wdev->ps = false;
1483 	/* allow mac80211 to determine the timeout */
1484 	wdev->ps_timeout = -1;
1485 
1486 	wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
1487 
1488 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1489 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1490 	     wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1491 		wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
1492 
1493 	INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1494 }
1495 
1496 void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
1497 			    struct wireless_dev *wdev)
1498 {
1499 	ASSERT_RTNL();
1500 	lockdep_assert_held(&rdev->wiphy.mtx);
1501 
1502 	/*
1503 	 * We get here also when the interface changes network namespaces,
1504 	 * as it's registered into the new one, but we don't want it to
1505 	 * change ID in that case. Checking if the ID is already assigned
1506 	 * works, because 0 isn't considered a valid ID and the memory is
1507 	 * 0-initialized.
1508 	 */
1509 	if (!wdev->identifier)
1510 		wdev->identifier = ++rdev->wdev_id;
1511 	list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1512 	rdev->devlist_generation++;
1513 	wdev->registered = true;
1514 
1515 	if (wdev->netdev &&
1516 	    sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj,
1517 			      "phy80211"))
1518 		pr_err("failed to add phy80211 symlink to netdev!\n");
1519 
1520 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1521 }
1522 
1523 int cfg80211_register_netdevice(struct net_device *dev)
1524 {
1525 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1526 	struct cfg80211_registered_device *rdev;
1527 	int ret;
1528 
1529 	ASSERT_RTNL();
1530 
1531 	if (WARN_ON(!wdev))
1532 		return -EINVAL;
1533 
1534 	rdev = wiphy_to_rdev(wdev->wiphy);
1535 
1536 	lockdep_assert_held(&rdev->wiphy.mtx);
1537 
1538 	/* we'll take care of this */
1539 	wdev->registered = true;
1540 	wdev->registering = true;
1541 	ret = register_netdevice(dev);
1542 	if (ret)
1543 		goto out;
1544 
1545 	cfg80211_register_wdev(rdev, wdev);
1546 	ret = 0;
1547 out:
1548 	wdev->registering = false;
1549 	if (ret)
1550 		wdev->registered = false;
1551 	return ret;
1552 }
1553 EXPORT_SYMBOL(cfg80211_register_netdevice);
1554 
1555 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1556 					 unsigned long state, void *ptr)
1557 {
1558 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1559 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1560 	struct cfg80211_registered_device *rdev;
1561 	struct cfg80211_sched_scan_request *pos, *tmp;
1562 
1563 	if (!wdev)
1564 		return NOTIFY_DONE;
1565 
1566 	rdev = wiphy_to_rdev(wdev->wiphy);
1567 
1568 	WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1569 
1570 	switch (state) {
1571 	case NETDEV_POST_INIT:
1572 		SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1573 		wdev->netdev = dev;
1574 		/* can only change netns with wiphy */
1575 		dev->netns_immutable = true;
1576 
1577 		cfg80211_init_wdev(wdev);
1578 		break;
1579 	case NETDEV_REGISTER:
1580 		if (!wdev->registered) {
1581 			guard(wiphy)(&rdev->wiphy);
1582 
1583 			cfg80211_register_wdev(rdev, wdev);
1584 		}
1585 		break;
1586 	case NETDEV_UNREGISTER:
1587 		/*
1588 		 * It is possible to get NETDEV_UNREGISTER multiple times,
1589 		 * so check wdev->registered.
1590 		 */
1591 		if (wdev->registered && !wdev->registering) {
1592 			guard(wiphy)(&rdev->wiphy);
1593 
1594 			_cfg80211_unregister_wdev(wdev, false);
1595 		}
1596 		break;
1597 	case NETDEV_GOING_DOWN:
1598 		scoped_guard(wiphy, &rdev->wiphy) {
1599 			cfg80211_leave(rdev, wdev, -1);
1600 			cfg80211_remove_links(wdev);
1601 		}
1602 		/* since we just did cfg80211_leave() nothing to do there */
1603 		cancel_work_sync(&wdev->disconnect_wk);
1604 		cancel_work_sync(&wdev->pmsr_free_wk);
1605 		break;
1606 	case NETDEV_DOWN:
1607 		wiphy_lock(&rdev->wiphy);
1608 		cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1609 		if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
1610 			if (WARN_ON(!rdev->scan_req->notified &&
1611 				    (!rdev->int_scan_req ||
1612 				     !rdev->int_scan_req->notified)))
1613 				rdev->scan_req->info.aborted = true;
1614 			___cfg80211_scan_done(rdev, false);
1615 		}
1616 
1617 		list_for_each_entry_safe(pos, tmp,
1618 					 &rdev->sched_scan_req_list, list) {
1619 			if (WARN_ON(pos->dev == wdev->netdev))
1620 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1621 		}
1622 
1623 		rdev->opencount--;
1624 		wiphy_unlock(&rdev->wiphy);
1625 		wake_up(&rdev->dev_wait);
1626 		break;
1627 	case NETDEV_UP:
1628 		wiphy_lock(&rdev->wiphy);
1629 		cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1630 		switch (wdev->iftype) {
1631 #ifdef CONFIG_CFG80211_WEXT
1632 		case NL80211_IFTYPE_ADHOC:
1633 			cfg80211_ibss_wext_join(rdev, wdev);
1634 			break;
1635 		case NL80211_IFTYPE_STATION:
1636 			cfg80211_mgd_wext_connect(rdev, wdev);
1637 			break;
1638 #endif
1639 #ifdef CONFIG_MAC80211_MESH
1640 		case NL80211_IFTYPE_MESH_POINT:
1641 			{
1642 				/* backward compat code... */
1643 				struct mesh_setup setup;
1644 				memcpy(&setup, &default_mesh_setup,
1645 						sizeof(setup));
1646 				 /* back compat only needed for mesh_id */
1647 				setup.mesh_id = wdev->u.mesh.id;
1648 				setup.mesh_id_len = wdev->u.mesh.id_up_len;
1649 				if (wdev->u.mesh.id_up_len)
1650 					__cfg80211_join_mesh(rdev, dev,
1651 							&setup,
1652 							&default_mesh_config);
1653 				break;
1654 			}
1655 #endif
1656 		default:
1657 			break;
1658 		}
1659 		rdev->opencount++;
1660 
1661 		/*
1662 		 * Configure power management to the driver here so that its
1663 		 * correctly set also after interface type changes etc.
1664 		 */
1665 		if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1666 		     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1667 		    rdev->ops->set_power_mgmt &&
1668 		    rdev_set_power_mgmt(rdev, dev, wdev->ps,
1669 					wdev->ps_timeout)) {
1670 			/* assume this means it's off */
1671 			wdev->ps = false;
1672 		}
1673 		wiphy_unlock(&rdev->wiphy);
1674 		break;
1675 	case NETDEV_PRE_UP:
1676 		if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
1677 					     wdev->use_4addr, 0))
1678 			return notifier_from_errno(-EOPNOTSUPP);
1679 
1680 		if (rfkill_blocked(rdev->wiphy.rfkill))
1681 			return notifier_from_errno(-ERFKILL);
1682 		break;
1683 	default:
1684 		return NOTIFY_DONE;
1685 	}
1686 
1687 	wireless_nlevent_flush();
1688 
1689 	return NOTIFY_OK;
1690 }
1691 
1692 static struct notifier_block cfg80211_netdev_notifier = {
1693 	.notifier_call = cfg80211_netdev_notifier_call,
1694 };
1695 
1696 static void __net_exit cfg80211_pernet_exit(struct net *net)
1697 {
1698 	struct cfg80211_registered_device *rdev;
1699 
1700 	rtnl_lock();
1701 	for_each_rdev(rdev) {
1702 		if (net_eq(wiphy_net(&rdev->wiphy), net))
1703 			WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1704 	}
1705 	rtnl_unlock();
1706 }
1707 
1708 static struct pernet_operations cfg80211_pernet_ops = {
1709 	.exit = cfg80211_pernet_exit,
1710 };
1711 
1712 void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
1713 {
1714 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1715 	unsigned long flags;
1716 
1717 	trace_wiphy_work_queue(wiphy, work);
1718 
1719 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1720 	if (list_empty(&work->entry))
1721 		list_add_tail(&work->entry, &rdev->wiphy_work_list);
1722 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1723 
1724 	queue_work(system_dfl_wq, &rdev->wiphy_work);
1725 }
1726 EXPORT_SYMBOL_GPL(wiphy_work_queue);
1727 
1728 void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
1729 {
1730 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1731 	unsigned long flags;
1732 
1733 	lockdep_assert_held(&wiphy->mtx);
1734 
1735 	trace_wiphy_work_cancel(wiphy, work);
1736 
1737 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1738 	if (!list_empty(&work->entry))
1739 		list_del_init(&work->entry);
1740 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1741 }
1742 EXPORT_SYMBOL_GPL(wiphy_work_cancel);
1743 
1744 void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
1745 {
1746 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1747 	unsigned long flags;
1748 	bool run;
1749 
1750 	trace_wiphy_work_flush(wiphy, work);
1751 
1752 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1753 	run = !work || !list_empty(&work->entry);
1754 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1755 
1756 	if (run)
1757 		cfg80211_process_wiphy_works(rdev, work);
1758 }
1759 EXPORT_SYMBOL_GPL(wiphy_work_flush);
1760 
1761 void wiphy_delayed_work_timer(struct timer_list *t)
1762 {
1763 	struct wiphy_delayed_work *dwork = timer_container_of(dwork, t, timer);
1764 
1765 	wiphy_work_queue(dwork->wiphy, &dwork->work);
1766 }
1767 EXPORT_SYMBOL(wiphy_delayed_work_timer);
1768 
1769 void wiphy_delayed_work_queue(struct wiphy *wiphy,
1770 			      struct wiphy_delayed_work *dwork,
1771 			      unsigned long delay)
1772 {
1773 	trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay);
1774 
1775 	if (!delay) {
1776 		timer_delete(&dwork->timer);
1777 		wiphy_work_queue(wiphy, &dwork->work);
1778 		return;
1779 	}
1780 
1781 	dwork->wiphy = wiphy;
1782 	mod_timer(&dwork->timer, jiffies + delay);
1783 }
1784 EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
1785 
1786 void wiphy_delayed_work_cancel(struct wiphy *wiphy,
1787 			       struct wiphy_delayed_work *dwork)
1788 {
1789 	lockdep_assert_held(&wiphy->mtx);
1790 
1791 	timer_delete_sync(&dwork->timer);
1792 	wiphy_work_cancel(wiphy, &dwork->work);
1793 }
1794 EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
1795 
1796 void wiphy_delayed_work_flush(struct wiphy *wiphy,
1797 			      struct wiphy_delayed_work *dwork)
1798 {
1799 	lockdep_assert_held(&wiphy->mtx);
1800 
1801 	timer_delete_sync(&dwork->timer);
1802 	wiphy_work_flush(wiphy, &dwork->work);
1803 }
1804 EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
1805 
1806 bool wiphy_delayed_work_pending(struct wiphy *wiphy,
1807 				struct wiphy_delayed_work *dwork)
1808 {
1809 	return timer_pending(&dwork->timer);
1810 }
1811 EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
1812 
1813 enum hrtimer_restart wiphy_hrtimer_work_timer(struct hrtimer *t)
1814 {
1815 	struct wiphy_hrtimer_work *hrwork =
1816 		container_of(t, struct wiphy_hrtimer_work, timer);
1817 
1818 	wiphy_work_queue(hrwork->wiphy, &hrwork->work);
1819 
1820 	return HRTIMER_NORESTART;
1821 }
1822 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_timer);
1823 
1824 void wiphy_hrtimer_work_queue(struct wiphy *wiphy,
1825 			      struct wiphy_hrtimer_work *hrwork,
1826 			      ktime_t delay)
1827 {
1828 	trace_wiphy_hrtimer_work_queue(wiphy, &hrwork->work, delay);
1829 
1830 	if (!delay) {
1831 		hrtimer_cancel(&hrwork->timer);
1832 		wiphy_work_queue(wiphy, &hrwork->work);
1833 		return;
1834 	}
1835 
1836 	hrwork->wiphy = wiphy;
1837 	hrtimer_start_range_ns(&hrwork->timer, delay,
1838 			       1000 * NSEC_PER_USEC, HRTIMER_MODE_REL);
1839 }
1840 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_queue);
1841 
1842 void wiphy_hrtimer_work_cancel(struct wiphy *wiphy,
1843 			       struct wiphy_hrtimer_work *hrwork)
1844 {
1845 	lockdep_assert_held(&wiphy->mtx);
1846 
1847 	hrtimer_cancel(&hrwork->timer);
1848 	wiphy_work_cancel(wiphy, &hrwork->work);
1849 }
1850 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_cancel);
1851 
1852 void wiphy_hrtimer_work_flush(struct wiphy *wiphy,
1853 			      struct wiphy_hrtimer_work *hrwork)
1854 {
1855 	lockdep_assert_held(&wiphy->mtx);
1856 
1857 	hrtimer_cancel(&hrwork->timer);
1858 	wiphy_work_flush(wiphy, &hrwork->work);
1859 }
1860 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_flush);
1861 
1862 bool wiphy_hrtimer_work_pending(struct wiphy *wiphy,
1863 				struct wiphy_hrtimer_work *hrwork)
1864 {
1865 	return hrtimer_is_queued(&hrwork->timer);
1866 }
1867 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_pending);
1868 
1869 static int __init cfg80211_init(void)
1870 {
1871 	int err;
1872 
1873 	err = register_pernet_device(&cfg80211_pernet_ops);
1874 	if (err)
1875 		goto out_fail_pernet;
1876 
1877 	err = wiphy_sysfs_init();
1878 	if (err)
1879 		goto out_fail_sysfs;
1880 
1881 	err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1882 	if (err)
1883 		goto out_fail_notifier;
1884 
1885 	err = nl80211_init();
1886 	if (err)
1887 		goto out_fail_nl80211;
1888 
1889 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1890 
1891 	err = regulatory_init();
1892 	if (err)
1893 		goto out_fail_reg;
1894 
1895 	cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1896 	if (!cfg80211_wq) {
1897 		err = -ENOMEM;
1898 		goto out_fail_wq;
1899 	}
1900 
1901 	return 0;
1902 
1903 out_fail_wq:
1904 	regulatory_exit();
1905 out_fail_reg:
1906 	debugfs_remove(ieee80211_debugfs_dir);
1907 	nl80211_exit();
1908 out_fail_nl80211:
1909 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1910 out_fail_notifier:
1911 	wiphy_sysfs_exit();
1912 out_fail_sysfs:
1913 	unregister_pernet_device(&cfg80211_pernet_ops);
1914 out_fail_pernet:
1915 	return err;
1916 }
1917 fs_initcall(cfg80211_init);
1918 
1919 static void __exit cfg80211_exit(void)
1920 {
1921 	debugfs_remove(ieee80211_debugfs_dir);
1922 	nl80211_exit();
1923 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1924 	wiphy_sysfs_exit();
1925 	regulatory_exit();
1926 	unregister_pernet_device(&cfg80211_pernet_ops);
1927 	destroy_workqueue(cfg80211_wq);
1928 }
1929 module_exit(cfg80211_exit);
1930