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