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