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