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