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