xref: /linux/net/wireless/core.c (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
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->interface_modes & BIT(NL80211_IFTYPE_WDS)))
968 		return -EINVAL;
969 
970 	if (WARN_ON(wiphy->pmsr_capa && !wiphy->pmsr_capa->ftm.supported))
971 		return -EINVAL;
972 
973 	if (wiphy->pmsr_capa && wiphy->pmsr_capa->ftm.supported) {
974 		if (WARN_ON(!wiphy->pmsr_capa->ftm.asap &&
975 			    !wiphy->pmsr_capa->ftm.non_asap))
976 			return -EINVAL;
977 		if (WARN_ON(!wiphy->pmsr_capa->ftm.preambles ||
978 			    !wiphy->pmsr_capa->ftm.bandwidths))
979 			return -EINVAL;
980 		if (WARN_ON(wiphy->pmsr_capa->ftm.preambles &
981 				~(BIT(NL80211_PREAMBLE_LEGACY) |
982 				  BIT(NL80211_PREAMBLE_HT) |
983 				  BIT(NL80211_PREAMBLE_VHT) |
984 				  BIT(NL80211_PREAMBLE_HE) |
985 				  BIT(NL80211_PREAMBLE_DMG))))
986 			return -EINVAL;
987 		if (WARN_ON((wiphy->pmsr_capa->ftm.trigger_based ||
988 			     wiphy->pmsr_capa->ftm.non_trigger_based) &&
989 			    !(wiphy->pmsr_capa->ftm.preambles &
990 			      BIT(NL80211_PREAMBLE_HE))))
991 			return -EINVAL;
992 		if (WARN_ON(wiphy->pmsr_capa->ftm.bandwidths &
993 				~(BIT(NL80211_CHAN_WIDTH_20_NOHT) |
994 				  BIT(NL80211_CHAN_WIDTH_20) |
995 				  BIT(NL80211_CHAN_WIDTH_40) |
996 				  BIT(NL80211_CHAN_WIDTH_80) |
997 				  BIT(NL80211_CHAN_WIDTH_80P80) |
998 				  BIT(NL80211_CHAN_WIDTH_160) |
999 				  BIT(NL80211_CHAN_WIDTH_320))))
1000 			return -EINVAL;
1001 	}
1002 
1003 	if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
1004 		    (wiphy->regulatory_flags &
1005 					(REGULATORY_CUSTOM_REG |
1006 					 REGULATORY_STRICT_REG |
1007 					 REGULATORY_COUNTRY_IE_FOLLOW_POWER |
1008 					 REGULATORY_COUNTRY_IE_IGNORE))))
1009 		return -EINVAL;
1010 
1011 	if (WARN_ON(wiphy->coalesce &&
1012 		    (!wiphy->coalesce->n_rules ||
1013 		     !wiphy->coalesce->n_patterns) &&
1014 		    (!wiphy->coalesce->pattern_min_len ||
1015 		     wiphy->coalesce->pattern_min_len >
1016 			wiphy->coalesce->pattern_max_len)))
1017 		return -EINVAL;
1018 
1019 	if (WARN_ON(wiphy->ap_sme_capa &&
1020 		    !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
1021 		return -EINVAL;
1022 
1023 	if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
1024 		return -EINVAL;
1025 
1026 	if (WARN_ON(wiphy->addresses &&
1027 		    !is_zero_ether_addr(wiphy->perm_addr) &&
1028 		    memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
1029 			   ETH_ALEN)))
1030 		return -EINVAL;
1031 
1032 	if (WARN_ON(wiphy->max_acl_mac_addrs &&
1033 		    (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
1034 		     !rdev->ops->set_mac_acl)))
1035 		return -EINVAL;
1036 
1037 	/* assure only valid behaviours are flagged by driver
1038 	 * hence subtract 2 as bit 0 is invalid.
1039 	 */
1040 	if (WARN_ON(wiphy->bss_select_support &&
1041 		    (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
1042 		return -EINVAL;
1043 
1044 	if (WARN_ON(wiphy_ext_feature_isset(&rdev->wiphy,
1045 					    NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) &&
1046 		    (!rdev->ops->set_pmk || !rdev->ops->del_pmk)))
1047 		return -EINVAL;
1048 
1049 	if (WARN_ON(!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1050 		    rdev->ops->update_connect_params))
1051 		return -EINVAL;
1052 
1053 	if (wiphy->addresses)
1054 		memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
1055 
1056 	/* sanity check ifmodes */
1057 	WARN_ON(!ifmodes);
1058 	ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
1059 	if (WARN_ON(ifmodes != wiphy->interface_modes))
1060 		wiphy->interface_modes = ifmodes;
1061 
1062 	res = wiphy_verify_combinations(wiphy);
1063 	if (res)
1064 		return res;
1065 
1066 	if (!wiphy_cipher_suites_valid(wiphy))
1067 		return -EINVAL;
1068 
1069 	/* sanity check supported bands/channels */
1070 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1071 		const struct ieee80211_sband_iftype_data *iftd;
1072 		u16 types = 0;
1073 		bool have_he = false;
1074 
1075 		sband = wiphy->bands[band];
1076 		if (!sband)
1077 			continue;
1078 
1079 		sband->band = band;
1080 		if (WARN_ON(!sband->n_channels))
1081 			return -EINVAL;
1082 		/*
1083 		 * on 60GHz or sub-1Ghz band, there are no legacy rates, so
1084 		 * n_bitrates is 0
1085 		 */
1086 		if (WARN_ON((band != NL80211_BAND_60GHZ &&
1087 			     band != NL80211_BAND_S1GHZ) &&
1088 			    !sband->n_bitrates))
1089 			return -EINVAL;
1090 
1091 		if (WARN_ON(band == NL80211_BAND_6GHZ &&
1092 			    (sband->ht_cap.ht_supported ||
1093 			     sband->vht_cap.vht_supported)))
1094 			return -EINVAL;
1095 
1096 		/*
1097 		 * Since cfg80211_disable_40mhz_24ghz is global, we can
1098 		 * modify the sband's ht data even if the driver uses a
1099 		 * global structure for that.
1100 		 */
1101 		if (cfg80211_disable_40mhz_24ghz &&
1102 		    band == NL80211_BAND_2GHZ &&
1103 		    sband->ht_cap.ht_supported) {
1104 			sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1105 			sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
1106 		}
1107 
1108 		/*
1109 		 * Since we use a u32 for rate bitmaps in
1110 		 * ieee80211_get_response_rate, we cannot
1111 		 * have more than 32 legacy rates.
1112 		 */
1113 		if (WARN_ON(sband->n_bitrates > 32))
1114 			return -EINVAL;
1115 
1116 		for (i = 0; i < sband->n_channels; i++) {
1117 			sband->channels[i].orig_flags =
1118 				sband->channels[i].flags;
1119 			sband->channels[i].orig_mag = INT_MAX;
1120 			sband->channels[i].orig_mpwr =
1121 				sband->channels[i].max_power;
1122 			sband->channels[i].band = band;
1123 
1124 			if (WARN_ON(sband->channels[i].freq_offset >= 1000))
1125 				return -EINVAL;
1126 		}
1127 
1128 		for_each_sband_iftype_data(sband, i, iftd) {
1129 			bool has_ap, has_non_ap;
1130 			u32 ap_bits = BIT(NL80211_IFTYPE_AP) |
1131 				      BIT(NL80211_IFTYPE_P2P_GO);
1132 
1133 			if (WARN_ON(!iftd->types_mask))
1134 				return -EINVAL;
1135 			if (WARN_ON(types & iftd->types_mask))
1136 				return -EINVAL;
1137 
1138 			/* at least one piece of information must be present */
1139 			if (WARN_ON(!iftd->he_cap.has_he))
1140 				return -EINVAL;
1141 
1142 			types |= iftd->types_mask;
1143 
1144 			if (i == 0)
1145 				have_he = iftd->he_cap.has_he;
1146 			else
1147 				have_he = have_he &&
1148 					  iftd->he_cap.has_he;
1149 
1150 			has_ap = iftd->types_mask & ap_bits;
1151 			has_non_ap = iftd->types_mask & ~ap_bits;
1152 
1153 			/*
1154 			 * For EHT 20 MHz STA, the capabilities format differs
1155 			 * but to simplify, don't check 20 MHz but rather check
1156 			 * only if AP and non-AP were mentioned at the same time,
1157 			 * reject if so.
1158 			 */
1159 			if (WARN_ON(iftd->eht_cap.has_eht &&
1160 				    has_ap && has_non_ap))
1161 				return -EINVAL;
1162 		}
1163 
1164 		if (WARN_ON(!have_he && band == NL80211_BAND_6GHZ))
1165 			return -EINVAL;
1166 
1167 		have_band = true;
1168 	}
1169 
1170 	if (!have_band) {
1171 		WARN_ON(1);
1172 		return -EINVAL;
1173 	}
1174 
1175 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1176 		/*
1177 		 * Validate we have a policy (can be explicitly set to
1178 		 * VENDOR_CMD_RAW_DATA which is non-NULL) and also that
1179 		 * we have at least one of doit/dumpit.
1180 		 */
1181 		if (WARN_ON(!rdev->wiphy.vendor_commands[i].policy))
1182 			return -EINVAL;
1183 		if (WARN_ON(!rdev->wiphy.vendor_commands[i].doit &&
1184 			    !rdev->wiphy.vendor_commands[i].dumpit))
1185 			return -EINVAL;
1186 	}
1187 
1188 #ifdef CONFIG_PM
1189 	if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
1190 		    (!rdev->wiphy.wowlan->pattern_min_len ||
1191 		     rdev->wiphy.wowlan->pattern_min_len >
1192 				rdev->wiphy.wowlan->pattern_max_len)))
1193 		return -EINVAL;
1194 #endif
1195 
1196 	if (!wiphy->max_num_akm_suites)
1197 		wiphy->max_num_akm_suites = NL80211_MAX_NR_AKM_SUITES;
1198 	else if (wiphy->max_num_akm_suites < NL80211_MAX_NR_AKM_SUITES ||
1199 		 wiphy->max_num_akm_suites > CFG80211_MAX_NUM_AKM_SUITES)
1200 		return -EINVAL;
1201 
1202 	/* Allocate radio configuration space for multi-radio wiphy */
1203 	if (wiphy->n_radio > 0) {
1204 		int idx;
1205 
1206 		wiphy->radio_cfg = kzalloc_objs(*wiphy->radio_cfg,
1207 						wiphy->n_radio);
1208 		if (!wiphy->radio_cfg)
1209 			return -ENOMEM;
1210 		/*
1211 		 * Initialize wiphy radio parameters to IEEE 802.11
1212 		 * MIB default values. RTS threshold is disabled by
1213 		 * default with the special -1 value.
1214 		 */
1215 		for (idx = 0; idx < wiphy->n_radio; idx++)
1216 			wiphy->radio_cfg[idx].rts_threshold = (u32)-1;
1217 	}
1218 
1219 	/* check and set up bitrates */
1220 	ieee80211_set_bitrate_flags(wiphy);
1221 
1222 	rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
1223 
1224 	if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_CTWINDOW)
1225 		rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_CTWIN;
1226 	else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)
1227 		rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_CTWINDOW;
1228 	if (rdev->wiphy.bss_param_support & WIPHY_BSS_PARAM_P2P_OPPPS)
1229 		rdev->wiphy.features |= NL80211_FEATURE_P2P_GO_OPPPS;
1230 	else if (rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)
1231 		rdev->wiphy.bss_param_support |= WIPHY_BSS_PARAM_P2P_OPPPS;
1232 
1233 	rtnl_lock();
1234 	wiphy_lock(&rdev->wiphy);
1235 	res = device_add(&rdev->wiphy.dev);
1236 	if (res) {
1237 		wiphy_unlock(&rdev->wiphy);
1238 		rtnl_unlock();
1239 		return res;
1240 	}
1241 
1242 	list_add_rcu(&rdev->list, &cfg80211_rdev_list);
1243 	cfg80211_rdev_list_generation++;
1244 
1245 	/* add to debugfs */
1246 	rdev->wiphy.debugfsdir = debugfs_create_dir(wiphy_name(&rdev->wiphy),
1247 						    ieee80211_debugfs_dir);
1248 	if (wiphy->n_radio > 0) {
1249 		int idx;
1250 		char radio_name[RADIO_DEBUGFSDIR_MAX_LEN];
1251 
1252 		for (idx = 0; idx < wiphy->n_radio; idx++) {
1253 			scnprintf(radio_name, sizeof(radio_name), "radio%d",
1254 				  idx);
1255 			wiphy->radio_cfg[idx].radio_debugfsdir =
1256 				debugfs_create_dir(radio_name,
1257 						   rdev->wiphy.debugfsdir);
1258 		}
1259 	}
1260 
1261 	cfg80211_debugfs_rdev_add(rdev);
1262 	nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
1263 	wiphy_unlock(&rdev->wiphy);
1264 
1265 	/* set up regulatory info */
1266 	wiphy_regulatory_register(wiphy);
1267 
1268 	if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
1269 		struct regulatory_request request = {
1270 			.wiphy_idx = get_wiphy_idx(wiphy),
1271 			.initiator = NL80211_REGDOM_SET_BY_DRIVER,
1272 			.alpha2[0] = '9',
1273 			.alpha2[1] = '9',
1274 		};
1275 
1276 		nl80211_send_reg_change_event(&request);
1277 	}
1278 
1279 	/* Check that nobody globally advertises any capabilities they do not
1280 	 * advertise on all possible interface types.
1281 	 */
1282 	if (wiphy->extended_capabilities_len &&
1283 	    wiphy->num_iftype_ext_capab &&
1284 	    wiphy->iftype_ext_capab) {
1285 		u8 supported_on_all, j;
1286 		const struct wiphy_iftype_ext_capab *capab;
1287 
1288 		capab = wiphy->iftype_ext_capab;
1289 		for (j = 0; j < wiphy->extended_capabilities_len; j++) {
1290 			if (capab[0].extended_capabilities_len > j)
1291 				supported_on_all =
1292 					capab[0].extended_capabilities[j];
1293 			else
1294 				supported_on_all = 0x00;
1295 			for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
1296 				if (j >= capab[i].extended_capabilities_len) {
1297 					supported_on_all = 0x00;
1298 					break;
1299 				}
1300 				supported_on_all &=
1301 					capab[i].extended_capabilities[j];
1302 			}
1303 			if (WARN_ON(wiphy->extended_capabilities[j] &
1304 				    ~supported_on_all))
1305 				break;
1306 		}
1307 	}
1308 
1309 	rdev->wiphy.registered = true;
1310 	rtnl_unlock();
1311 
1312 	res = rfkill_register(rdev->wiphy.rfkill);
1313 	if (res) {
1314 		rfkill_destroy(rdev->wiphy.rfkill);
1315 		rdev->wiphy.rfkill = NULL;
1316 		wiphy_unregister(&rdev->wiphy);
1317 		return res;
1318 	}
1319 
1320 	return 0;
1321 }
1322 EXPORT_SYMBOL(wiphy_register);
1323 
1324 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
1325 {
1326 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1327 
1328 	if (!rdev->ops->rfkill_poll)
1329 		return;
1330 	rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
1331 	rfkill_resume_polling(wiphy->rfkill);
1332 }
1333 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
1334 
1335 void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev,
1336 				  struct wiphy_work *end)
1337 {
1338 	unsigned int runaway_limit = 100;
1339 	unsigned long flags;
1340 
1341 	lockdep_assert_held(&rdev->wiphy.mtx);
1342 
1343 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1344 	while (!list_empty(&rdev->wiphy_work_list)) {
1345 		struct wiphy_work *wk;
1346 
1347 		wk = list_first_entry(&rdev->wiphy_work_list,
1348 				      struct wiphy_work, entry);
1349 		list_del_init(&wk->entry);
1350 		spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1351 
1352 		trace_wiphy_work_run(&rdev->wiphy, wk);
1353 		wk->func(&rdev->wiphy, wk);
1354 
1355 		spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1356 
1357 		if (wk == end)
1358 			break;
1359 
1360 		if (WARN_ON(--runaway_limit == 0))
1361 			INIT_LIST_HEAD(&rdev->wiphy_work_list);
1362 	}
1363 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1364 }
1365 
1366 void wiphy_unregister(struct wiphy *wiphy)
1367 {
1368 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1369 
1370 	wait_event(rdev->dev_wait, ({
1371 		int __count;
1372 		wiphy_lock(&rdev->wiphy);
1373 		__count = rdev->opencount;
1374 		wiphy_unlock(&rdev->wiphy);
1375 		__count == 0; }));
1376 
1377 	if (rdev->wiphy.rfkill)
1378 		rfkill_unregister(rdev->wiphy.rfkill);
1379 
1380 	rtnl_lock();
1381 	wiphy_lock(&rdev->wiphy);
1382 	nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
1383 	rdev->wiphy.registered = false;
1384 
1385 	WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
1386 
1387 	/*
1388 	 * First remove the hardware from everywhere, this makes
1389 	 * it impossible to find from userspace.
1390 	 */
1391 	debugfs_remove_recursive(rdev->wiphy.debugfsdir);
1392 	list_del_rcu(&rdev->list);
1393 	synchronize_rcu();
1394 
1395 	/*
1396 	 * If this device got a regulatory hint tell core its
1397 	 * free to listen now to a new shiny device regulatory hint
1398 	 */
1399 	wiphy_regulatory_deregister(wiphy);
1400 
1401 	cfg80211_rdev_list_generation++;
1402 	device_del(&rdev->wiphy.dev);
1403 
1404 #ifdef CONFIG_PM
1405 	if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
1406 		rdev_set_wakeup(rdev, false);
1407 #endif
1408 
1409 	/* surely nothing is reachable now, clean up work */
1410 	cfg80211_process_wiphy_works(rdev, NULL);
1411 	wiphy_unlock(&rdev->wiphy);
1412 	rtnl_unlock();
1413 
1414 	/* this has nothing to do now but make sure it's gone */
1415 	cancel_work_sync(&rdev->wiphy_work);
1416 
1417 	cancel_work_sync(&rdev->sched_scan_res_wk);
1418 	cancel_work_sync(&rdev->rfkill_block);
1419 	cancel_work_sync(&rdev->conn_work);
1420 	flush_work(&rdev->event_work);
1421 	cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
1422 	cancel_delayed_work_sync(&rdev->background_cac_done_wk);
1423 	flush_work(&rdev->destroy_work);
1424 	flush_work(&rdev->reg_leave_nan_wk);
1425 	flush_work(&rdev->propagate_radar_detect_wk);
1426 	flush_work(&rdev->propagate_cac_done_wk);
1427 	flush_work(&rdev->mgmt_registrations_update_wk);
1428 	flush_work(&rdev->background_cac_abort_wk);
1429 
1430 	cfg80211_rdev_free_wowlan(rdev);
1431 	cfg80211_free_coalesce(rdev->coalesce);
1432 	rdev->coalesce = NULL;
1433 }
1434 EXPORT_SYMBOL(wiphy_unregister);
1435 
1436 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
1437 {
1438 	struct cfg80211_internal_bss *scan, *tmp;
1439 	struct cfg80211_beacon_registration *reg, *treg;
1440 	unsigned long flags;
1441 
1442 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1443 	WARN_ON(!list_empty(&rdev->wiphy_work_list));
1444 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1445 	cancel_work_sync(&rdev->wiphy_work);
1446 
1447 	rfkill_destroy(rdev->wiphy.rfkill);
1448 	list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
1449 		list_del(&reg->list);
1450 		kfree(reg);
1451 	}
1452 	list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
1453 		cfg80211_put_bss(&rdev->wiphy, &scan->pub);
1454 	mutex_destroy(&rdev->wiphy.mtx);
1455 
1456 	/*
1457 	 * The 'regd' can only be non-NULL if we never finished
1458 	 * initializing the wiphy and thus never went through the
1459 	 * unregister path - e.g. in failure scenarios. Thus, it
1460 	 * cannot have been visible to anyone if non-NULL, so we
1461 	 * can just free it here.
1462 	 */
1463 	kfree(rcu_dereference_raw(rdev->wiphy.regd));
1464 
1465 	kfree(rdev);
1466 }
1467 
1468 void wiphy_free(struct wiphy *wiphy)
1469 {
1470 	kfree(wiphy->radio_cfg);
1471 	put_device(&wiphy->dev);
1472 }
1473 EXPORT_SYMBOL(wiphy_free);
1474 
1475 void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
1476 				      enum rfkill_hard_block_reasons reason)
1477 {
1478 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1479 
1480 	if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason))
1481 		schedule_work(&rdev->rfkill_block);
1482 }
1483 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
1484 
1485 static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
1486 				      bool unregister_netdev)
1487 {
1488 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1489 	struct cfg80211_cqm_config *cqm_config;
1490 	unsigned int link_id;
1491 
1492 	ASSERT_RTNL();
1493 	lockdep_assert_held(&rdev->wiphy.mtx);
1494 
1495 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1496 
1497 	wdev->registered = false;
1498 
1499 	if (wdev->netdev) {
1500 		sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211");
1501 		if (unregister_netdev)
1502 			unregister_netdevice(wdev->netdev);
1503 	}
1504 
1505 	list_del_rcu(&wdev->list);
1506 	synchronize_net();
1507 	rdev->devlist_generation++;
1508 	wiphy_work_cancel(wdev->wiphy, &wdev->disconnect_wk);
1509 
1510 	cfg80211_mlme_purge_registrations(wdev);
1511 
1512 	switch (wdev->iftype) {
1513 	case NL80211_IFTYPE_P2P_DEVICE:
1514 		cfg80211_stop_p2p_device(rdev, wdev);
1515 		break;
1516 	case NL80211_IFTYPE_NAN:
1517 		cfg80211_stop_nan(rdev, wdev);
1518 		break;
1519 	case NL80211_IFTYPE_PD:
1520 		cfg80211_stop_pd(rdev, wdev);
1521 		break;
1522 	default:
1523 		break;
1524 	}
1525 
1526 #ifdef CONFIG_CFG80211_WEXT
1527 	kfree_sensitive(wdev->wext.keys);
1528 	wdev->wext.keys = NULL;
1529 #endif
1530 	wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work);
1531 	/* deleted from the list, so can't be found from nl80211 any more */
1532 	cqm_config = rcu_access_pointer(wdev->cqm_config);
1533 	kfree_rcu(cqm_config, rcu_head);
1534 	RCU_INIT_POINTER(wdev->cqm_config, NULL);
1535 
1536 	/*
1537 	 * Ensure that all events have been processed and
1538 	 * freed.
1539 	 */
1540 	cfg80211_process_wdev_events(wdev);
1541 
1542 	if (wdev->iftype == NL80211_IFTYPE_STATION ||
1543 	    wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1544 		for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
1545 			struct cfg80211_internal_bss *curbss;
1546 
1547 			curbss = wdev->links[link_id].client.current_bss;
1548 
1549 			if (WARN_ON(curbss)) {
1550 				cfg80211_unhold_bss(curbss);
1551 				cfg80211_put_bss(wdev->wiphy, &curbss->pub);
1552 				wdev->links[link_id].client.current_bss = NULL;
1553 			}
1554 		}
1555 	}
1556 
1557 	wdev->connected = false;
1558 }
1559 
1560 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1561 {
1562 	_cfg80211_unregister_wdev(wdev, true);
1563 }
1564 EXPORT_SYMBOL(cfg80211_unregister_wdev);
1565 
1566 static const struct device_type wiphy_type = {
1567 	.name	= "wlan",
1568 };
1569 
1570 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1571 			       enum nl80211_iftype iftype, int num)
1572 {
1573 	lockdep_assert_held(&rdev->wiphy.mtx);
1574 
1575 	rdev->num_running_ifaces += num;
1576 	if (iftype == NL80211_IFTYPE_MONITOR)
1577 		rdev->num_running_monitor_ifaces += num;
1578 }
1579 
1580 void cfg80211_leave_locked(struct cfg80211_registered_device *rdev,
1581 			   struct wireless_dev *wdev, int link_id)
1582 {
1583 	struct net_device *dev = wdev->netdev;
1584 	struct cfg80211_sched_scan_request *pos, *tmp;
1585 
1586 	lockdep_assert_held(&rdev->wiphy.mtx);
1587 
1588 	cfg80211_pmsr_wdev_down(wdev);
1589 
1590 	cfg80211_stop_radar_detection(wdev);
1591 	cfg80211_stop_background_radar_detection(wdev);
1592 
1593 	switch (wdev->iftype) {
1594 	case NL80211_IFTYPE_ADHOC:
1595 		cfg80211_leave_ibss(rdev, dev, true);
1596 		break;
1597 	case NL80211_IFTYPE_P2P_CLIENT:
1598 	case NL80211_IFTYPE_STATION:
1599 		list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1600 					 list) {
1601 			if (dev == pos->dev)
1602 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1603 		}
1604 
1605 #ifdef CONFIG_CFG80211_WEXT
1606 		kfree(wdev->wext.ie);
1607 		wdev->wext.ie = NULL;
1608 		wdev->wext.ie_len = 0;
1609 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1610 #endif
1611 		cfg80211_disconnect(rdev, dev,
1612 				    WLAN_REASON_DEAUTH_LEAVING, true);
1613 		break;
1614 	case NL80211_IFTYPE_MESH_POINT:
1615 		cfg80211_leave_mesh(rdev, dev);
1616 		break;
1617 	case NL80211_IFTYPE_AP:
1618 	case NL80211_IFTYPE_P2P_GO:
1619 		cfg80211_stop_ap(rdev, dev, link_id, true);
1620 		break;
1621 	case NL80211_IFTYPE_OCB:
1622 		cfg80211_leave_ocb(rdev, dev);
1623 		break;
1624 	case NL80211_IFTYPE_P2P_DEVICE:
1625 		cfg80211_stop_p2p_device(rdev, wdev);
1626 		break;
1627 	case NL80211_IFTYPE_NAN:
1628 		cfg80211_stop_nan(rdev, wdev);
1629 		break;
1630 	case NL80211_IFTYPE_PD:
1631 		cfg80211_stop_pd(rdev, wdev);
1632 		break;
1633 	case NL80211_IFTYPE_AP_VLAN:
1634 	case NL80211_IFTYPE_MONITOR:
1635 	case NL80211_IFTYPE_NAN_DATA:
1636 		/* nothing to do */
1637 		break;
1638 	case NL80211_IFTYPE_UNSPECIFIED:
1639 	case NL80211_IFTYPE_WDS:
1640 	case NUM_NL80211_IFTYPES:
1641 		/* invalid */
1642 		break;
1643 	}
1644 }
1645 
1646 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1647 		    struct wireless_dev *wdev, int link_id)
1648 {
1649 	ASSERT_RTNL();
1650 
1651 	/* NAN_DATA interfaces must be closed before stopping NAN */
1652 	cfg80211_close_dependents(rdev, wdev);
1653 
1654 	guard(wiphy)(&rdev->wiphy);
1655 
1656 	cfg80211_leave_locked(rdev, wdev, link_id);
1657 }
1658 
1659 void cfg80211_stop_link(struct wiphy *wiphy, struct wireless_dev *wdev,
1660 			int link_id, gfp_t gfp)
1661 {
1662 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1663 	struct cfg80211_event *ev;
1664 	unsigned long flags;
1665 
1666 	/* Only AP/GO interfaces may have a specific link_id */
1667 	if (WARN_ON_ONCE(link_id != -1 &&
1668 			 wdev->iftype != NL80211_IFTYPE_AP &&
1669 			 wdev->iftype != NL80211_IFTYPE_P2P_GO))
1670 		link_id = -1;
1671 
1672 	trace_cfg80211_stop_link(wiphy, wdev, link_id);
1673 
1674 	if (wdev->iftype == NL80211_IFTYPE_NAN)
1675 		return;
1676 
1677 	ev = kzalloc_obj(*ev, gfp);
1678 	if (!ev)
1679 		return;
1680 
1681 	ev->type = EVENT_STOPPED;
1682 	ev->link_id = link_id;
1683 
1684 	spin_lock_irqsave(&wdev->event_lock, flags);
1685 	list_add_tail(&ev->list, &wdev->event_list);
1686 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1687 	queue_work(cfg80211_wq, &rdev->event_work);
1688 }
1689 EXPORT_SYMBOL(cfg80211_stop_link);
1690 
1691 void cfg80211_init_wdev(struct wireless_dev *wdev)
1692 {
1693 	INIT_LIST_HEAD(&wdev->event_list);
1694 	spin_lock_init(&wdev->event_lock);
1695 	INIT_LIST_HEAD(&wdev->mgmt_registrations);
1696 	INIT_LIST_HEAD(&wdev->pmsr_list);
1697 	spin_lock_init(&wdev->pmsr_lock);
1698 	wiphy_work_init(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
1699 
1700 #ifdef CONFIG_CFG80211_WEXT
1701 	wdev->wext.default_key = -1;
1702 	wdev->wext.default_mgmt_key = -1;
1703 	wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1704 #endif
1705 
1706 	wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work);
1707 
1708 	if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1709 		wdev->ps = true;
1710 	else
1711 		wdev->ps = false;
1712 	/* allow mac80211 to determine the timeout */
1713 	wdev->ps_timeout = -1;
1714 
1715 	wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
1716 
1717 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1718 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1719 	     wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1720 		wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
1721 
1722 	wiphy_work_init(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1723 }
1724 
1725 void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
1726 			    struct wireless_dev *wdev)
1727 {
1728 	ASSERT_RTNL();
1729 	lockdep_assert_held(&rdev->wiphy.mtx);
1730 
1731 	/*
1732 	 * We get here also when the interface changes network namespaces,
1733 	 * as it's registered into the new one, but we don't want it to
1734 	 * change ID in that case. Checking if the ID is already assigned
1735 	 * works, because 0 isn't considered a valid ID and the memory is
1736 	 * 0-initialized.
1737 	 */
1738 	if (!wdev->identifier)
1739 		wdev->identifier = ++rdev->wdev_id;
1740 	list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1741 	rdev->devlist_generation++;
1742 	wdev->registered = true;
1743 
1744 	if (wdev->netdev &&
1745 	    sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj,
1746 			      "phy80211"))
1747 		pr_err("failed to add phy80211 symlink to netdev!\n");
1748 
1749 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1750 }
1751 
1752 int cfg80211_register_netdevice(struct net_device *dev)
1753 {
1754 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1755 	struct cfg80211_registered_device *rdev;
1756 	int ret;
1757 
1758 	ASSERT_RTNL();
1759 
1760 	if (WARN_ON(!wdev))
1761 		return -EINVAL;
1762 
1763 	rdev = wiphy_to_rdev(wdev->wiphy);
1764 
1765 	lockdep_assert_held(&rdev->wiphy.mtx);
1766 
1767 	/* we'll take care of this */
1768 	wdev->registered = true;
1769 	wdev->registering = true;
1770 	ret = register_netdevice(dev);
1771 	if (ret)
1772 		goto out;
1773 
1774 	cfg80211_register_wdev(rdev, wdev);
1775 	ret = 0;
1776 out:
1777 	wdev->registering = false;
1778 	if (ret)
1779 		wdev->registered = false;
1780 	return ret;
1781 }
1782 EXPORT_SYMBOL(cfg80211_register_netdevice);
1783 
1784 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1785 					 unsigned long state, void *ptr)
1786 {
1787 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1788 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1789 	struct cfg80211_registered_device *rdev;
1790 	struct cfg80211_sched_scan_request *pos, *tmp;
1791 
1792 	if (!wdev)
1793 		return NOTIFY_DONE;
1794 
1795 	rdev = wiphy_to_rdev(wdev->wiphy);
1796 
1797 	WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1798 
1799 	switch (state) {
1800 	case NETDEV_POST_INIT:
1801 		SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1802 		wdev->netdev = dev;
1803 		/* can only change netns with wiphy */
1804 		dev->netns_immutable = true;
1805 
1806 		cfg80211_init_wdev(wdev);
1807 		break;
1808 	case NETDEV_REGISTER:
1809 		if (!wdev->registered) {
1810 			guard(wiphy)(&rdev->wiphy);
1811 
1812 			cfg80211_register_wdev(rdev, wdev);
1813 		}
1814 		break;
1815 	case NETDEV_UNREGISTER:
1816 		/*
1817 		 * It is possible to get NETDEV_UNREGISTER multiple times,
1818 		 * so check wdev->registered.
1819 		 */
1820 		if (wdev->registered && !wdev->registering) {
1821 			guard(wiphy)(&rdev->wiphy);
1822 
1823 			_cfg80211_unregister_wdev(wdev, false);
1824 		}
1825 		break;
1826 	case NETDEV_GOING_DOWN:
1827 		cfg80211_leave(rdev, wdev, -1);
1828 		scoped_guard(wiphy, &rdev->wiphy) {
1829 			cfg80211_remove_links(wdev);
1830 			/* since we just did cfg80211_leave() nothing to do there */
1831 			wiphy_work_cancel(wdev->wiphy, &wdev->disconnect_wk);
1832 		}
1833 		break;
1834 	case NETDEV_DOWN:
1835 		wiphy_lock(&rdev->wiphy);
1836 		cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1837 		if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
1838 			if (!rdev->scan_req->notified &&
1839 			    (!rdev->int_scan_req ||
1840 			     !rdev->int_scan_req->notified))
1841 				rdev->scan_req->info.aborted = true;
1842 			___cfg80211_scan_done(rdev, false);
1843 		}
1844 
1845 		list_for_each_entry_safe(pos, tmp,
1846 					 &rdev->sched_scan_req_list, list) {
1847 			if (WARN_ON(pos->dev == wdev->netdev))
1848 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1849 		}
1850 
1851 		rdev->opencount--;
1852 		wiphy_unlock(&rdev->wiphy);
1853 		wake_up(&rdev->dev_wait);
1854 		break;
1855 	case NETDEV_UP:
1856 		wiphy_lock(&rdev->wiphy);
1857 		cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1858 		switch (wdev->iftype) {
1859 #ifdef CONFIG_CFG80211_WEXT
1860 		case NL80211_IFTYPE_ADHOC:
1861 			cfg80211_ibss_wext_join(rdev, wdev);
1862 			break;
1863 		case NL80211_IFTYPE_STATION:
1864 			cfg80211_mgd_wext_connect(rdev, wdev);
1865 			break;
1866 #endif
1867 #ifdef CONFIG_MAC80211_MESH
1868 		case NL80211_IFTYPE_MESH_POINT:
1869 			{
1870 				/* backward compat code... */
1871 				struct mesh_setup setup;
1872 				memcpy(&setup, &default_mesh_setup,
1873 						sizeof(setup));
1874 				 /* back compat only needed for mesh_id */
1875 				setup.mesh_id = wdev->u.mesh.id;
1876 				setup.mesh_id_len = wdev->u.mesh.id_up_len;
1877 				if (wdev->u.mesh.id_up_len)
1878 					__cfg80211_join_mesh(rdev, dev,
1879 							&setup,
1880 							&default_mesh_config);
1881 				break;
1882 			}
1883 #endif
1884 		default:
1885 			break;
1886 		}
1887 		rdev->opencount++;
1888 
1889 		/*
1890 		 * Configure power management to the driver here so that its
1891 		 * correctly set also after interface type changes etc.
1892 		 */
1893 		if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1894 		     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1895 		    rdev->ops->set_power_mgmt &&
1896 		    rdev_set_power_mgmt(rdev, dev, wdev->ps,
1897 					wdev->ps_timeout)) {
1898 			/* assume this means it's off */
1899 			wdev->ps = false;
1900 		}
1901 		wiphy_unlock(&rdev->wiphy);
1902 		break;
1903 	case NETDEV_PRE_UP:
1904 		if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
1905 					     wdev->use_4addr, 0))
1906 			return notifier_from_errno(-EOPNOTSUPP);
1907 
1908 		if (rfkill_blocked(rdev->wiphy.rfkill))
1909 			return notifier_from_errno(-ERFKILL);
1910 
1911 		/* NAN_DATA interfaces require a running NAN interface */
1912 		if (wdev->iftype == NL80211_IFTYPE_NAN_DATA) {
1913 			struct wireless_dev *iter;
1914 			bool nan_started = false;
1915 
1916 			list_for_each_entry(iter, &rdev->wiphy.wdev_list, list) {
1917 				if (iter->iftype == NL80211_IFTYPE_NAN &&
1918 				    wdev_running(iter)) {
1919 					nan_started = true;
1920 					break;
1921 				}
1922 			}
1923 
1924 			if (!nan_started)
1925 				return notifier_from_errno(-ENOLINK);
1926 		}
1927 		break;
1928 	default:
1929 		return NOTIFY_DONE;
1930 	}
1931 
1932 	wireless_nlevent_flush();
1933 
1934 	return NOTIFY_OK;
1935 }
1936 
1937 static struct notifier_block cfg80211_netdev_notifier = {
1938 	.notifier_call = cfg80211_netdev_notifier_call,
1939 };
1940 
1941 static void __net_exit cfg80211_pernet_exit(struct net *net)
1942 {
1943 	struct cfg80211_registered_device *rdev;
1944 
1945 	rtnl_lock();
1946 	for_each_rdev(rdev) {
1947 		if (net_eq(wiphy_net(&rdev->wiphy), net))
1948 			WARN_ON(__cfg80211_switch_netns(rdev, &init_net, true));
1949 	}
1950 	rtnl_unlock();
1951 }
1952 
1953 static struct pernet_operations cfg80211_pernet_ops = {
1954 	.exit = cfg80211_pernet_exit,
1955 };
1956 
1957 void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
1958 {
1959 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1960 	unsigned long flags;
1961 
1962 	trace_wiphy_work_queue(wiphy, work);
1963 
1964 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1965 	if (list_empty(&work->entry))
1966 		list_add_tail(&work->entry, &rdev->wiphy_work_list);
1967 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1968 
1969 	queue_work(system_dfl_wq, &rdev->wiphy_work);
1970 }
1971 EXPORT_SYMBOL_GPL(wiphy_work_queue);
1972 
1973 void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
1974 {
1975 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1976 	unsigned long flags;
1977 
1978 	lockdep_assert_held(&wiphy->mtx);
1979 
1980 	trace_wiphy_work_cancel(wiphy, work);
1981 
1982 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1983 	if (!list_empty(&work->entry))
1984 		list_del_init(&work->entry);
1985 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1986 }
1987 EXPORT_SYMBOL_GPL(wiphy_work_cancel);
1988 
1989 void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
1990 {
1991 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1992 	unsigned long flags;
1993 	bool run;
1994 
1995 	trace_wiphy_work_flush(wiphy, work);
1996 
1997 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1998 	run = !work || !list_empty(&work->entry);
1999 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
2000 
2001 	if (run)
2002 		cfg80211_process_wiphy_works(rdev, work);
2003 }
2004 EXPORT_SYMBOL_GPL(wiphy_work_flush);
2005 
2006 void wiphy_delayed_work_timer(struct timer_list *t)
2007 {
2008 	struct wiphy_delayed_work *dwork = timer_container_of(dwork, t, timer);
2009 
2010 	wiphy_work_queue(dwork->wiphy, &dwork->work);
2011 }
2012 EXPORT_SYMBOL(wiphy_delayed_work_timer);
2013 
2014 void wiphy_delayed_work_queue(struct wiphy *wiphy,
2015 			      struct wiphy_delayed_work *dwork,
2016 			      unsigned long delay)
2017 {
2018 	trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay);
2019 
2020 	if (!delay) {
2021 		timer_delete(&dwork->timer);
2022 		wiphy_work_queue(wiphy, &dwork->work);
2023 		return;
2024 	}
2025 
2026 	dwork->wiphy = wiphy;
2027 	mod_timer(&dwork->timer, jiffies + delay);
2028 }
2029 EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
2030 
2031 void wiphy_delayed_work_cancel(struct wiphy *wiphy,
2032 			       struct wiphy_delayed_work *dwork)
2033 {
2034 	lockdep_assert_held(&wiphy->mtx);
2035 
2036 	timer_delete_sync(&dwork->timer);
2037 	wiphy_work_cancel(wiphy, &dwork->work);
2038 }
2039 EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
2040 
2041 void wiphy_delayed_work_flush(struct wiphy *wiphy,
2042 			      struct wiphy_delayed_work *dwork)
2043 {
2044 	lockdep_assert_held(&wiphy->mtx);
2045 
2046 	timer_delete_sync(&dwork->timer);
2047 	wiphy_work_flush(wiphy, &dwork->work);
2048 }
2049 EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
2050 
2051 bool wiphy_delayed_work_pending(struct wiphy *wiphy,
2052 				struct wiphy_delayed_work *dwork)
2053 {
2054 	return timer_pending(&dwork->timer);
2055 }
2056 EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
2057 
2058 enum hrtimer_restart wiphy_hrtimer_work_timer(struct hrtimer *t)
2059 {
2060 	struct wiphy_hrtimer_work *hrwork =
2061 		container_of(t, struct wiphy_hrtimer_work, timer);
2062 
2063 	wiphy_work_queue(hrwork->wiphy, &hrwork->work);
2064 
2065 	return HRTIMER_NORESTART;
2066 }
2067 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_timer);
2068 
2069 void wiphy_hrtimer_work_queue(struct wiphy *wiphy,
2070 			      struct wiphy_hrtimer_work *hrwork,
2071 			      ktime_t delay)
2072 {
2073 	trace_wiphy_hrtimer_work_queue(wiphy, &hrwork->work, delay);
2074 
2075 	if (!delay) {
2076 		hrtimer_cancel(&hrwork->timer);
2077 		wiphy_work_queue(wiphy, &hrwork->work);
2078 		return;
2079 	}
2080 
2081 	hrwork->wiphy = wiphy;
2082 	hrtimer_start_range_ns(&hrwork->timer, delay,
2083 			       1000 * NSEC_PER_USEC, HRTIMER_MODE_REL);
2084 }
2085 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_queue);
2086 
2087 void wiphy_hrtimer_work_cancel(struct wiphy *wiphy,
2088 			       struct wiphy_hrtimer_work *hrwork)
2089 {
2090 	lockdep_assert_held(&wiphy->mtx);
2091 
2092 	hrtimer_cancel(&hrwork->timer);
2093 	wiphy_work_cancel(wiphy, &hrwork->work);
2094 }
2095 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_cancel);
2096 
2097 void wiphy_hrtimer_work_flush(struct wiphy *wiphy,
2098 			      struct wiphy_hrtimer_work *hrwork)
2099 {
2100 	lockdep_assert_held(&wiphy->mtx);
2101 
2102 	hrtimer_cancel(&hrwork->timer);
2103 	wiphy_work_flush(wiphy, &hrwork->work);
2104 }
2105 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_flush);
2106 
2107 bool wiphy_hrtimer_work_pending(struct wiphy *wiphy,
2108 				struct wiphy_hrtimer_work *hrwork)
2109 {
2110 	return hrtimer_is_queued(&hrwork->timer);
2111 }
2112 EXPORT_SYMBOL_GPL(wiphy_hrtimer_work_pending);
2113 
2114 static int __init cfg80211_init(void)
2115 {
2116 	int err;
2117 
2118 	err = register_pernet_device(&cfg80211_pernet_ops);
2119 	if (err)
2120 		goto out_fail_pernet;
2121 
2122 	err = wiphy_sysfs_init();
2123 	if (err)
2124 		goto out_fail_sysfs;
2125 
2126 	err = register_netdevice_notifier(&cfg80211_netdev_notifier);
2127 	if (err)
2128 		goto out_fail_notifier;
2129 
2130 	err = nl80211_init();
2131 	if (err)
2132 		goto out_fail_nl80211;
2133 
2134 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
2135 
2136 	err = regulatory_init();
2137 	if (err)
2138 		goto out_fail_reg;
2139 
2140 	cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
2141 	if (!cfg80211_wq) {
2142 		err = -ENOMEM;
2143 		goto out_fail_wq;
2144 	}
2145 
2146 	return 0;
2147 
2148 out_fail_wq:
2149 	regulatory_exit();
2150 out_fail_reg:
2151 	debugfs_remove(ieee80211_debugfs_dir);
2152 	nl80211_exit();
2153 out_fail_nl80211:
2154 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
2155 out_fail_notifier:
2156 	wiphy_sysfs_exit();
2157 out_fail_sysfs:
2158 	unregister_pernet_device(&cfg80211_pernet_ops);
2159 out_fail_pernet:
2160 	return err;
2161 }
2162 fs_initcall(cfg80211_init);
2163 
2164 static void __exit cfg80211_exit(void)
2165 {
2166 	debugfs_remove(ieee80211_debugfs_dir);
2167 	nl80211_exit();
2168 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
2169 	wiphy_sysfs_exit();
2170 	regulatory_exit();
2171 	unregister_pernet_device(&cfg80211_pernet_ops);
2172 	destroy_workqueue(cfg80211_wq);
2173 }
2174 module_exit(cfg80211_exit);
2175