xref: /linux/net/wireless/core.c (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
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-2025 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_immutable = false;
166 		err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
167 		if (err)
168 			break;
169 		wdev->netdev->netns_immutable = 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_immutable = false;
182 			err = dev_change_net_namespace(wdev->netdev, net,
183 							"wlan%d");
184 			WARN_ON(err);
185 			wdev->netdev->netns_immutable = 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 	INIT_WORK(&rdev->wiphy_work, cfg80211_wiphy_work);
550 	INIT_LIST_HEAD(&rdev->wiphy_work_list);
551 	spin_lock_init(&rdev->wiphy_work_lock);
552 
553 #ifdef CONFIG_CFG80211_DEFAULT_PS
554 	rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
555 #endif
556 
557 	wiphy_net_set(&rdev->wiphy, &init_net);
558 
559 	rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
560 	rdev->wiphy.rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
561 					  &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
562 					  &rdev->rfkill_ops, rdev);
563 
564 	if (!rdev->wiphy.rfkill) {
565 		wiphy_free(&rdev->wiphy);
566 		return NULL;
567 	}
568 
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_320) |
797 				  BIT(NL80211_CHAN_WIDTH_5) |
798 				  BIT(NL80211_CHAN_WIDTH_10))))
799 			return -EINVAL;
800 	}
801 
802 	if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
803 		    (wiphy->regulatory_flags &
804 					(REGULATORY_CUSTOM_REG |
805 					 REGULATORY_STRICT_REG |
806 					 REGULATORY_COUNTRY_IE_FOLLOW_POWER |
807 					 REGULATORY_COUNTRY_IE_IGNORE))))
808 		return -EINVAL;
809 
810 	if (WARN_ON(wiphy->coalesce &&
811 		    (!wiphy->coalesce->n_rules ||
812 		     !wiphy->coalesce->n_patterns) &&
813 		    (!wiphy->coalesce->pattern_min_len ||
814 		     wiphy->coalesce->pattern_min_len >
815 			wiphy->coalesce->pattern_max_len)))
816 		return -EINVAL;
817 
818 	if (WARN_ON(wiphy->ap_sme_capa &&
819 		    !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
820 		return -EINVAL;
821 
822 	if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
823 		return -EINVAL;
824 
825 	if (WARN_ON(wiphy->addresses &&
826 		    !is_zero_ether_addr(wiphy->perm_addr) &&
827 		    memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
828 			   ETH_ALEN)))
829 		return -EINVAL;
830 
831 	if (WARN_ON(wiphy->max_acl_mac_addrs &&
832 		    (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
833 		     !rdev->ops->set_mac_acl)))
834 		return -EINVAL;
835 
836 	/* assure only valid behaviours are flagged by driver
837 	 * hence subtract 2 as bit 0 is invalid.
838 	 */
839 	if (WARN_ON(wiphy->bss_select_support &&
840 		    (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
841 		return -EINVAL;
842 
843 	if (WARN_ON(wiphy_ext_feature_isset(&rdev->wiphy,
844 					    NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) &&
845 		    (!rdev->ops->set_pmk || !rdev->ops->del_pmk)))
846 		return -EINVAL;
847 
848 	if (WARN_ON(!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
849 		    rdev->ops->update_connect_params))
850 		return -EINVAL;
851 
852 	if (wiphy->addresses)
853 		memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
854 
855 	/* sanity check ifmodes */
856 	WARN_ON(!ifmodes);
857 	ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
858 	if (WARN_ON(ifmodes != wiphy->interface_modes))
859 		wiphy->interface_modes = ifmodes;
860 
861 	res = wiphy_verify_combinations(wiphy);
862 	if (res)
863 		return res;
864 
865 	/* sanity check supported bands/channels */
866 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
867 		const struct ieee80211_sband_iftype_data *iftd;
868 		u16 types = 0;
869 		bool have_he = false;
870 
871 		sband = wiphy->bands[band];
872 		if (!sband)
873 			continue;
874 
875 		sband->band = band;
876 		if (WARN_ON(!sband->n_channels))
877 			return -EINVAL;
878 		/*
879 		 * on 60GHz or sub-1Ghz band, there are no legacy rates, so
880 		 * n_bitrates is 0
881 		 */
882 		if (WARN_ON((band != NL80211_BAND_60GHZ &&
883 			     band != NL80211_BAND_S1GHZ) &&
884 			    !sband->n_bitrates))
885 			return -EINVAL;
886 
887 		if (WARN_ON(band == NL80211_BAND_6GHZ &&
888 			    (sband->ht_cap.ht_supported ||
889 			     sband->vht_cap.vht_supported)))
890 			return -EINVAL;
891 
892 		/*
893 		 * Since cfg80211_disable_40mhz_24ghz is global, we can
894 		 * modify the sband's ht data even if the driver uses a
895 		 * global structure for that.
896 		 */
897 		if (cfg80211_disable_40mhz_24ghz &&
898 		    band == NL80211_BAND_2GHZ &&
899 		    sband->ht_cap.ht_supported) {
900 			sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
901 			sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
902 		}
903 
904 		/*
905 		 * Since we use a u32 for rate bitmaps in
906 		 * ieee80211_get_response_rate, we cannot
907 		 * have more than 32 legacy rates.
908 		 */
909 		if (WARN_ON(sband->n_bitrates > 32))
910 			return -EINVAL;
911 
912 		for (i = 0; i < sband->n_channels; i++) {
913 			sband->channels[i].orig_flags =
914 				sband->channels[i].flags;
915 			sband->channels[i].orig_mag = INT_MAX;
916 			sband->channels[i].orig_mpwr =
917 				sband->channels[i].max_power;
918 			sband->channels[i].band = band;
919 
920 			if (WARN_ON(sband->channels[i].freq_offset >= 1000))
921 				return -EINVAL;
922 		}
923 
924 		for_each_sband_iftype_data(sband, i, iftd) {
925 			bool has_ap, has_non_ap;
926 			u32 ap_bits = BIT(NL80211_IFTYPE_AP) |
927 				      BIT(NL80211_IFTYPE_P2P_GO);
928 
929 			if (WARN_ON(!iftd->types_mask))
930 				return -EINVAL;
931 			if (WARN_ON(types & iftd->types_mask))
932 				return -EINVAL;
933 
934 			/* at least one piece of information must be present */
935 			if (WARN_ON(!iftd->he_cap.has_he))
936 				return -EINVAL;
937 
938 			types |= iftd->types_mask;
939 
940 			if (i == 0)
941 				have_he = iftd->he_cap.has_he;
942 			else
943 				have_he = have_he &&
944 					  iftd->he_cap.has_he;
945 
946 			has_ap = iftd->types_mask & ap_bits;
947 			has_non_ap = iftd->types_mask & ~ap_bits;
948 
949 			/*
950 			 * For EHT 20 MHz STA, the capabilities format differs
951 			 * but to simplify, don't check 20 MHz but rather check
952 			 * only if AP and non-AP were mentioned at the same time,
953 			 * reject if so.
954 			 */
955 			if (WARN_ON(iftd->eht_cap.has_eht &&
956 				    has_ap && has_non_ap))
957 				return -EINVAL;
958 		}
959 
960 		if (WARN_ON(!have_he && band == NL80211_BAND_6GHZ))
961 			return -EINVAL;
962 
963 		have_band = true;
964 	}
965 
966 	if (!have_band) {
967 		WARN_ON(1);
968 		return -EINVAL;
969 	}
970 
971 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
972 		/*
973 		 * Validate we have a policy (can be explicitly set to
974 		 * VENDOR_CMD_RAW_DATA which is non-NULL) and also that
975 		 * we have at least one of doit/dumpit.
976 		 */
977 		if (WARN_ON(!rdev->wiphy.vendor_commands[i].policy))
978 			return -EINVAL;
979 		if (WARN_ON(!rdev->wiphy.vendor_commands[i].doit &&
980 			    !rdev->wiphy.vendor_commands[i].dumpit))
981 			return -EINVAL;
982 	}
983 
984 #ifdef CONFIG_PM
985 	if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
986 		    (!rdev->wiphy.wowlan->pattern_min_len ||
987 		     rdev->wiphy.wowlan->pattern_min_len >
988 				rdev->wiphy.wowlan->pattern_max_len)))
989 		return -EINVAL;
990 #endif
991 
992 	if (!wiphy->max_num_akm_suites)
993 		wiphy->max_num_akm_suites = NL80211_MAX_NR_AKM_SUITES;
994 	else if (wiphy->max_num_akm_suites < NL80211_MAX_NR_AKM_SUITES ||
995 		 wiphy->max_num_akm_suites > CFG80211_MAX_NUM_AKM_SUITES)
996 		return -EINVAL;
997 
998 	/* check and set up bitrates */
999 	ieee80211_set_bitrate_flags(wiphy);
1000 
1001 	rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
1002 
1003 	rtnl_lock();
1004 	wiphy_lock(&rdev->wiphy);
1005 	res = device_add(&rdev->wiphy.dev);
1006 	if (res) {
1007 		wiphy_unlock(&rdev->wiphy);
1008 		rtnl_unlock();
1009 		return res;
1010 	}
1011 
1012 	list_add_rcu(&rdev->list, &cfg80211_rdev_list);
1013 	cfg80211_rdev_list_generation++;
1014 
1015 	/* add to debugfs */
1016 	rdev->wiphy.debugfsdir = debugfs_create_dir(wiphy_name(&rdev->wiphy),
1017 						    ieee80211_debugfs_dir);
1018 
1019 	cfg80211_debugfs_rdev_add(rdev);
1020 	nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
1021 	wiphy_unlock(&rdev->wiphy);
1022 
1023 	/* set up regulatory info */
1024 	wiphy_regulatory_register(wiphy);
1025 
1026 	if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
1027 		struct regulatory_request request;
1028 
1029 		request.wiphy_idx = get_wiphy_idx(wiphy);
1030 		request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
1031 		request.alpha2[0] = '9';
1032 		request.alpha2[1] = '9';
1033 
1034 		nl80211_send_reg_change_event(&request);
1035 	}
1036 
1037 	/* Check that nobody globally advertises any capabilities they do not
1038 	 * advertise on all possible interface types.
1039 	 */
1040 	if (wiphy->extended_capabilities_len &&
1041 	    wiphy->num_iftype_ext_capab &&
1042 	    wiphy->iftype_ext_capab) {
1043 		u8 supported_on_all, j;
1044 		const struct wiphy_iftype_ext_capab *capab;
1045 
1046 		capab = wiphy->iftype_ext_capab;
1047 		for (j = 0; j < wiphy->extended_capabilities_len; j++) {
1048 			if (capab[0].extended_capabilities_len > j)
1049 				supported_on_all =
1050 					capab[0].extended_capabilities[j];
1051 			else
1052 				supported_on_all = 0x00;
1053 			for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
1054 				if (j >= capab[i].extended_capabilities_len) {
1055 					supported_on_all = 0x00;
1056 					break;
1057 				}
1058 				supported_on_all &=
1059 					capab[i].extended_capabilities[j];
1060 			}
1061 			if (WARN_ON(wiphy->extended_capabilities[j] &
1062 				    ~supported_on_all))
1063 				break;
1064 		}
1065 	}
1066 
1067 	rdev->wiphy.registered = true;
1068 	rtnl_unlock();
1069 
1070 	res = rfkill_register(rdev->wiphy.rfkill);
1071 	if (res) {
1072 		rfkill_destroy(rdev->wiphy.rfkill);
1073 		rdev->wiphy.rfkill = NULL;
1074 		wiphy_unregister(&rdev->wiphy);
1075 		return res;
1076 	}
1077 
1078 	return 0;
1079 }
1080 EXPORT_SYMBOL(wiphy_register);
1081 
wiphy_rfkill_start_polling(struct wiphy * wiphy)1082 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
1083 {
1084 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1085 
1086 	if (!rdev->ops->rfkill_poll)
1087 		return;
1088 	rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
1089 	rfkill_resume_polling(wiphy->rfkill);
1090 }
1091 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
1092 
cfg80211_process_wiphy_works(struct cfg80211_registered_device * rdev,struct wiphy_work * end)1093 void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev,
1094 				  struct wiphy_work *end)
1095 {
1096 	unsigned int runaway_limit = 100;
1097 	unsigned long flags;
1098 
1099 	lockdep_assert_held(&rdev->wiphy.mtx);
1100 
1101 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1102 	while (!list_empty(&rdev->wiphy_work_list)) {
1103 		struct wiphy_work *wk;
1104 
1105 		wk = list_first_entry(&rdev->wiphy_work_list,
1106 				      struct wiphy_work, entry);
1107 		list_del_init(&wk->entry);
1108 		spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1109 
1110 		trace_wiphy_work_run(&rdev->wiphy, wk);
1111 		wk->func(&rdev->wiphy, wk);
1112 
1113 		spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1114 
1115 		if (wk == end)
1116 			break;
1117 
1118 		if (WARN_ON(--runaway_limit == 0))
1119 			INIT_LIST_HEAD(&rdev->wiphy_work_list);
1120 	}
1121 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1122 }
1123 
wiphy_unregister(struct wiphy * wiphy)1124 void wiphy_unregister(struct wiphy *wiphy)
1125 {
1126 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1127 
1128 	wait_event(rdev->dev_wait, ({
1129 		int __count;
1130 		wiphy_lock(&rdev->wiphy);
1131 		__count = rdev->opencount;
1132 		wiphy_unlock(&rdev->wiphy);
1133 		__count == 0; }));
1134 
1135 	if (rdev->wiphy.rfkill)
1136 		rfkill_unregister(rdev->wiphy.rfkill);
1137 
1138 	rtnl_lock();
1139 	wiphy_lock(&rdev->wiphy);
1140 	nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
1141 	rdev->wiphy.registered = false;
1142 
1143 	WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
1144 
1145 	/*
1146 	 * First remove the hardware from everywhere, this makes
1147 	 * it impossible to find from userspace.
1148 	 */
1149 	debugfs_remove_recursive(rdev->wiphy.debugfsdir);
1150 	list_del_rcu(&rdev->list);
1151 	synchronize_rcu();
1152 
1153 	/*
1154 	 * If this device got a regulatory hint tell core its
1155 	 * free to listen now to a new shiny device regulatory hint
1156 	 */
1157 	wiphy_regulatory_deregister(wiphy);
1158 
1159 	cfg80211_rdev_list_generation++;
1160 	device_del(&rdev->wiphy.dev);
1161 
1162 #ifdef CONFIG_PM
1163 	if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
1164 		rdev_set_wakeup(rdev, false);
1165 #endif
1166 
1167 	/* surely nothing is reachable now, clean up work */
1168 	cfg80211_process_wiphy_works(rdev, NULL);
1169 	wiphy_unlock(&rdev->wiphy);
1170 	rtnl_unlock();
1171 
1172 	/* this has nothing to do now but make sure it's gone */
1173 	cancel_work_sync(&rdev->wiphy_work);
1174 
1175 	cancel_work_sync(&rdev->conn_work);
1176 	flush_work(&rdev->event_work);
1177 	cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
1178 	cancel_delayed_work_sync(&rdev->background_cac_done_wk);
1179 	flush_work(&rdev->destroy_work);
1180 	flush_work(&rdev->propagate_radar_detect_wk);
1181 	flush_work(&rdev->propagate_cac_done_wk);
1182 	flush_work(&rdev->mgmt_registrations_update_wk);
1183 	flush_work(&rdev->background_cac_abort_wk);
1184 
1185 	cfg80211_rdev_free_wowlan(rdev);
1186 	cfg80211_free_coalesce(rdev->coalesce);
1187 	rdev->coalesce = NULL;
1188 }
1189 EXPORT_SYMBOL(wiphy_unregister);
1190 
cfg80211_dev_free(struct cfg80211_registered_device * rdev)1191 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
1192 {
1193 	struct cfg80211_internal_bss *scan, *tmp;
1194 	struct cfg80211_beacon_registration *reg, *treg;
1195 	unsigned long flags;
1196 
1197 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1198 	WARN_ON(!list_empty(&rdev->wiphy_work_list));
1199 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1200 	cancel_work_sync(&rdev->wiphy_work);
1201 
1202 	rfkill_destroy(rdev->wiphy.rfkill);
1203 	list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
1204 		list_del(&reg->list);
1205 		kfree(reg);
1206 	}
1207 	list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
1208 		cfg80211_put_bss(&rdev->wiphy, &scan->pub);
1209 	mutex_destroy(&rdev->wiphy.mtx);
1210 
1211 	/*
1212 	 * The 'regd' can only be non-NULL if we never finished
1213 	 * initializing the wiphy and thus never went through the
1214 	 * unregister path - e.g. in failure scenarios. Thus, it
1215 	 * cannot have been visible to anyone if non-NULL, so we
1216 	 * can just free it here.
1217 	 */
1218 	kfree(rcu_dereference_raw(rdev->wiphy.regd));
1219 
1220 	kfree(rdev);
1221 }
1222 
wiphy_free(struct wiphy * wiphy)1223 void wiphy_free(struct wiphy *wiphy)
1224 {
1225 	put_device(&wiphy->dev);
1226 }
1227 EXPORT_SYMBOL(wiphy_free);
1228 
wiphy_rfkill_set_hw_state_reason(struct wiphy * wiphy,bool blocked,enum rfkill_hard_block_reasons reason)1229 void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
1230 				      enum rfkill_hard_block_reasons reason)
1231 {
1232 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1233 
1234 	if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason))
1235 		schedule_work(&rdev->rfkill_block);
1236 }
1237 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
1238 
_cfg80211_unregister_wdev(struct wireless_dev * wdev,bool unregister_netdev)1239 static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
1240 				      bool unregister_netdev)
1241 {
1242 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1243 	struct cfg80211_cqm_config *cqm_config;
1244 	unsigned int link_id;
1245 
1246 	ASSERT_RTNL();
1247 	lockdep_assert_held(&rdev->wiphy.mtx);
1248 
1249 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1250 
1251 	wdev->registered = false;
1252 
1253 	if (wdev->netdev) {
1254 		sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211");
1255 		if (unregister_netdev)
1256 			unregister_netdevice(wdev->netdev);
1257 	}
1258 
1259 	list_del_rcu(&wdev->list);
1260 	synchronize_net();
1261 	rdev->devlist_generation++;
1262 
1263 	cfg80211_mlme_purge_registrations(wdev);
1264 
1265 	switch (wdev->iftype) {
1266 	case NL80211_IFTYPE_P2P_DEVICE:
1267 		cfg80211_stop_p2p_device(rdev, wdev);
1268 		break;
1269 	case NL80211_IFTYPE_NAN:
1270 		cfg80211_stop_nan(rdev, wdev);
1271 		break;
1272 	default:
1273 		break;
1274 	}
1275 
1276 #ifdef CONFIG_CFG80211_WEXT
1277 	kfree_sensitive(wdev->wext.keys);
1278 	wdev->wext.keys = NULL;
1279 #endif
1280 	wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work);
1281 	/* deleted from the list, so can't be found from nl80211 any more */
1282 	cqm_config = rcu_access_pointer(wdev->cqm_config);
1283 	kfree_rcu(cqm_config, rcu_head);
1284 	RCU_INIT_POINTER(wdev->cqm_config, NULL);
1285 
1286 	/*
1287 	 * Ensure that all events have been processed and
1288 	 * freed.
1289 	 */
1290 	cfg80211_process_wdev_events(wdev);
1291 
1292 	if (wdev->iftype == NL80211_IFTYPE_STATION ||
1293 	    wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1294 		for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
1295 			struct cfg80211_internal_bss *curbss;
1296 
1297 			curbss = wdev->links[link_id].client.current_bss;
1298 
1299 			if (WARN_ON(curbss)) {
1300 				cfg80211_unhold_bss(curbss);
1301 				cfg80211_put_bss(wdev->wiphy, &curbss->pub);
1302 				wdev->links[link_id].client.current_bss = NULL;
1303 			}
1304 		}
1305 	}
1306 
1307 	wdev->connected = false;
1308 }
1309 
cfg80211_unregister_wdev(struct wireless_dev * wdev)1310 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1311 {
1312 	_cfg80211_unregister_wdev(wdev, true);
1313 }
1314 EXPORT_SYMBOL(cfg80211_unregister_wdev);
1315 
1316 static const struct device_type wiphy_type = {
1317 	.name	= "wlan",
1318 };
1319 
cfg80211_update_iface_num(struct cfg80211_registered_device * rdev,enum nl80211_iftype iftype,int num)1320 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1321 			       enum nl80211_iftype iftype, int num)
1322 {
1323 	lockdep_assert_held(&rdev->wiphy.mtx);
1324 
1325 	rdev->num_running_ifaces += num;
1326 	if (iftype == NL80211_IFTYPE_MONITOR)
1327 		rdev->num_running_monitor_ifaces += num;
1328 }
1329 
cfg80211_leave(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)1330 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1331 		    struct wireless_dev *wdev)
1332 {
1333 	struct net_device *dev = wdev->netdev;
1334 	struct cfg80211_sched_scan_request *pos, *tmp;
1335 
1336 	lockdep_assert_held(&rdev->wiphy.mtx);
1337 
1338 	cfg80211_pmsr_wdev_down(wdev);
1339 
1340 	cfg80211_stop_background_radar_detection(wdev);
1341 
1342 	switch (wdev->iftype) {
1343 	case NL80211_IFTYPE_ADHOC:
1344 		cfg80211_leave_ibss(rdev, dev, true);
1345 		break;
1346 	case NL80211_IFTYPE_P2P_CLIENT:
1347 	case NL80211_IFTYPE_STATION:
1348 		list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1349 					 list) {
1350 			if (dev == pos->dev)
1351 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1352 		}
1353 
1354 #ifdef CONFIG_CFG80211_WEXT
1355 		kfree(wdev->wext.ie);
1356 		wdev->wext.ie = NULL;
1357 		wdev->wext.ie_len = 0;
1358 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1359 #endif
1360 		cfg80211_disconnect(rdev, dev,
1361 				    WLAN_REASON_DEAUTH_LEAVING, true);
1362 		break;
1363 	case NL80211_IFTYPE_MESH_POINT:
1364 		cfg80211_leave_mesh(rdev, dev);
1365 		break;
1366 	case NL80211_IFTYPE_AP:
1367 	case NL80211_IFTYPE_P2P_GO:
1368 		cfg80211_stop_ap(rdev, dev, -1, true);
1369 		break;
1370 	case NL80211_IFTYPE_OCB:
1371 		cfg80211_leave_ocb(rdev, dev);
1372 		break;
1373 	case NL80211_IFTYPE_P2P_DEVICE:
1374 	case NL80211_IFTYPE_NAN:
1375 		/* cannot happen, has no netdev */
1376 		break;
1377 	case NL80211_IFTYPE_AP_VLAN:
1378 	case NL80211_IFTYPE_MONITOR:
1379 		/* nothing to do */
1380 		break;
1381 	case NL80211_IFTYPE_UNSPECIFIED:
1382 	case NL80211_IFTYPE_WDS:
1383 	case NUM_NL80211_IFTYPES:
1384 		/* invalid */
1385 		break;
1386 	}
1387 }
1388 
cfg80211_stop_iface(struct wiphy * wiphy,struct wireless_dev * wdev,gfp_t gfp)1389 void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1390 			 gfp_t gfp)
1391 {
1392 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1393 	struct cfg80211_event *ev;
1394 	unsigned long flags;
1395 
1396 	trace_cfg80211_stop_iface(wiphy, wdev);
1397 
1398 	ev = kzalloc(sizeof(*ev), gfp);
1399 	if (!ev)
1400 		return;
1401 
1402 	ev->type = EVENT_STOPPED;
1403 
1404 	spin_lock_irqsave(&wdev->event_lock, flags);
1405 	list_add_tail(&ev->list, &wdev->event_list);
1406 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1407 	queue_work(cfg80211_wq, &rdev->event_work);
1408 }
1409 EXPORT_SYMBOL(cfg80211_stop_iface);
1410 
cfg80211_init_wdev(struct wireless_dev * wdev)1411 void cfg80211_init_wdev(struct wireless_dev *wdev)
1412 {
1413 	INIT_LIST_HEAD(&wdev->event_list);
1414 	spin_lock_init(&wdev->event_lock);
1415 	INIT_LIST_HEAD(&wdev->mgmt_registrations);
1416 	INIT_LIST_HEAD(&wdev->pmsr_list);
1417 	spin_lock_init(&wdev->pmsr_lock);
1418 	INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
1419 
1420 #ifdef CONFIG_CFG80211_WEXT
1421 	wdev->wext.default_key = -1;
1422 	wdev->wext.default_mgmt_key = -1;
1423 	wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1424 #endif
1425 
1426 	wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work);
1427 
1428 	if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1429 		wdev->ps = true;
1430 	else
1431 		wdev->ps = false;
1432 	/* allow mac80211 to determine the timeout */
1433 	wdev->ps_timeout = -1;
1434 
1435 	wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
1436 
1437 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1438 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1439 	     wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1440 		wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
1441 
1442 	INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1443 }
1444 
cfg80211_register_wdev(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)1445 void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
1446 			    struct wireless_dev *wdev)
1447 {
1448 	ASSERT_RTNL();
1449 	lockdep_assert_held(&rdev->wiphy.mtx);
1450 
1451 	/*
1452 	 * We get here also when the interface changes network namespaces,
1453 	 * as it's registered into the new one, but we don't want it to
1454 	 * change ID in that case. Checking if the ID is already assigned
1455 	 * works, because 0 isn't considered a valid ID and the memory is
1456 	 * 0-initialized.
1457 	 */
1458 	if (!wdev->identifier)
1459 		wdev->identifier = ++rdev->wdev_id;
1460 	list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1461 	rdev->devlist_generation++;
1462 	wdev->registered = true;
1463 
1464 	if (wdev->netdev &&
1465 	    sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj,
1466 			      "phy80211"))
1467 		pr_err("failed to add phy80211 symlink to netdev!\n");
1468 
1469 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1470 }
1471 
cfg80211_register_netdevice(struct net_device * dev)1472 int cfg80211_register_netdevice(struct net_device *dev)
1473 {
1474 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1475 	struct cfg80211_registered_device *rdev;
1476 	int ret;
1477 
1478 	ASSERT_RTNL();
1479 
1480 	if (WARN_ON(!wdev))
1481 		return -EINVAL;
1482 
1483 	rdev = wiphy_to_rdev(wdev->wiphy);
1484 
1485 	lockdep_assert_held(&rdev->wiphy.mtx);
1486 
1487 	/* we'll take care of this */
1488 	wdev->registered = true;
1489 	wdev->registering = true;
1490 	ret = register_netdevice(dev);
1491 	if (ret)
1492 		goto out;
1493 
1494 	cfg80211_register_wdev(rdev, wdev);
1495 	ret = 0;
1496 out:
1497 	wdev->registering = false;
1498 	if (ret)
1499 		wdev->registered = false;
1500 	return ret;
1501 }
1502 EXPORT_SYMBOL(cfg80211_register_netdevice);
1503 
cfg80211_netdev_notifier_call(struct notifier_block * nb,unsigned long state,void * ptr)1504 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1505 					 unsigned long state, void *ptr)
1506 {
1507 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1508 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1509 	struct cfg80211_registered_device *rdev;
1510 	struct cfg80211_sched_scan_request *pos, *tmp;
1511 
1512 	if (!wdev)
1513 		return NOTIFY_DONE;
1514 
1515 	rdev = wiphy_to_rdev(wdev->wiphy);
1516 
1517 	WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1518 
1519 	switch (state) {
1520 	case NETDEV_POST_INIT:
1521 		SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1522 		wdev->netdev = dev;
1523 		/* can only change netns with wiphy */
1524 		dev->netns_immutable = true;
1525 
1526 		cfg80211_init_wdev(wdev);
1527 		break;
1528 	case NETDEV_REGISTER:
1529 		if (!wdev->registered) {
1530 			guard(wiphy)(&rdev->wiphy);
1531 
1532 			cfg80211_register_wdev(rdev, wdev);
1533 		}
1534 		break;
1535 	case NETDEV_UNREGISTER:
1536 		/*
1537 		 * It is possible to get NETDEV_UNREGISTER multiple times,
1538 		 * so check wdev->registered.
1539 		 */
1540 		if (wdev->registered && !wdev->registering) {
1541 			guard(wiphy)(&rdev->wiphy);
1542 
1543 			_cfg80211_unregister_wdev(wdev, false);
1544 		}
1545 		break;
1546 	case NETDEV_GOING_DOWN:
1547 		scoped_guard(wiphy, &rdev->wiphy) {
1548 			cfg80211_leave(rdev, wdev);
1549 			cfg80211_remove_links(wdev);
1550 		}
1551 		/* since we just did cfg80211_leave() nothing to do there */
1552 		cancel_work_sync(&wdev->disconnect_wk);
1553 		cancel_work_sync(&wdev->pmsr_free_wk);
1554 		break;
1555 	case NETDEV_DOWN:
1556 		wiphy_lock(&rdev->wiphy);
1557 		cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1558 		if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
1559 			if (WARN_ON(!rdev->scan_req->notified &&
1560 				    (!rdev->int_scan_req ||
1561 				     !rdev->int_scan_req->notified)))
1562 				rdev->scan_req->info.aborted = true;
1563 			___cfg80211_scan_done(rdev, false);
1564 		}
1565 
1566 		list_for_each_entry_safe(pos, tmp,
1567 					 &rdev->sched_scan_req_list, list) {
1568 			if (WARN_ON(pos->dev == wdev->netdev))
1569 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1570 		}
1571 
1572 		rdev->opencount--;
1573 		wiphy_unlock(&rdev->wiphy);
1574 		wake_up(&rdev->dev_wait);
1575 		break;
1576 	case NETDEV_UP:
1577 		wiphy_lock(&rdev->wiphy);
1578 		cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1579 		switch (wdev->iftype) {
1580 #ifdef CONFIG_CFG80211_WEXT
1581 		case NL80211_IFTYPE_ADHOC:
1582 			cfg80211_ibss_wext_join(rdev, wdev);
1583 			break;
1584 		case NL80211_IFTYPE_STATION:
1585 			cfg80211_mgd_wext_connect(rdev, wdev);
1586 			break;
1587 #endif
1588 #ifdef CONFIG_MAC80211_MESH
1589 		case NL80211_IFTYPE_MESH_POINT:
1590 			{
1591 				/* backward compat code... */
1592 				struct mesh_setup setup;
1593 				memcpy(&setup, &default_mesh_setup,
1594 						sizeof(setup));
1595 				 /* back compat only needed for mesh_id */
1596 				setup.mesh_id = wdev->u.mesh.id;
1597 				setup.mesh_id_len = wdev->u.mesh.id_up_len;
1598 				if (wdev->u.mesh.id_up_len)
1599 					__cfg80211_join_mesh(rdev, dev,
1600 							&setup,
1601 							&default_mesh_config);
1602 				break;
1603 			}
1604 #endif
1605 		default:
1606 			break;
1607 		}
1608 		rdev->opencount++;
1609 
1610 		/*
1611 		 * Configure power management to the driver here so that its
1612 		 * correctly set also after interface type changes etc.
1613 		 */
1614 		if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1615 		     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1616 		    rdev->ops->set_power_mgmt &&
1617 		    rdev_set_power_mgmt(rdev, dev, wdev->ps,
1618 					wdev->ps_timeout)) {
1619 			/* assume this means it's off */
1620 			wdev->ps = false;
1621 		}
1622 		wiphy_unlock(&rdev->wiphy);
1623 		break;
1624 	case NETDEV_PRE_UP:
1625 		if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
1626 					     wdev->use_4addr, 0))
1627 			return notifier_from_errno(-EOPNOTSUPP);
1628 
1629 		if (rfkill_blocked(rdev->wiphy.rfkill))
1630 			return notifier_from_errno(-ERFKILL);
1631 		break;
1632 	default:
1633 		return NOTIFY_DONE;
1634 	}
1635 
1636 	wireless_nlevent_flush();
1637 
1638 	return NOTIFY_OK;
1639 }
1640 
1641 static struct notifier_block cfg80211_netdev_notifier = {
1642 	.notifier_call = cfg80211_netdev_notifier_call,
1643 };
1644 
cfg80211_pernet_exit(struct net * net)1645 static void __net_exit cfg80211_pernet_exit(struct net *net)
1646 {
1647 	struct cfg80211_registered_device *rdev;
1648 
1649 	rtnl_lock();
1650 	for_each_rdev(rdev) {
1651 		if (net_eq(wiphy_net(&rdev->wiphy), net))
1652 			WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1653 	}
1654 	rtnl_unlock();
1655 }
1656 
1657 static struct pernet_operations cfg80211_pernet_ops = {
1658 	.exit = cfg80211_pernet_exit,
1659 };
1660 
wiphy_work_queue(struct wiphy * wiphy,struct wiphy_work * work)1661 void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
1662 {
1663 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1664 	unsigned long flags;
1665 
1666 	trace_wiphy_work_queue(wiphy, work);
1667 
1668 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1669 	if (list_empty(&work->entry))
1670 		list_add_tail(&work->entry, &rdev->wiphy_work_list);
1671 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1672 
1673 	queue_work(system_unbound_wq, &rdev->wiphy_work);
1674 }
1675 EXPORT_SYMBOL_GPL(wiphy_work_queue);
1676 
wiphy_work_cancel(struct wiphy * wiphy,struct wiphy_work * work)1677 void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
1678 {
1679 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1680 	unsigned long flags;
1681 
1682 	lockdep_assert_held(&wiphy->mtx);
1683 
1684 	trace_wiphy_work_cancel(wiphy, work);
1685 
1686 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1687 	if (!list_empty(&work->entry))
1688 		list_del_init(&work->entry);
1689 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1690 }
1691 EXPORT_SYMBOL_GPL(wiphy_work_cancel);
1692 
wiphy_work_flush(struct wiphy * wiphy,struct wiphy_work * work)1693 void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
1694 {
1695 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1696 	unsigned long flags;
1697 	bool run;
1698 
1699 	trace_wiphy_work_flush(wiphy, work);
1700 
1701 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1702 	run = !work || !list_empty(&work->entry);
1703 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1704 
1705 	if (run)
1706 		cfg80211_process_wiphy_works(rdev, work);
1707 }
1708 EXPORT_SYMBOL_GPL(wiphy_work_flush);
1709 
wiphy_delayed_work_timer(struct timer_list * t)1710 void wiphy_delayed_work_timer(struct timer_list *t)
1711 {
1712 	struct wiphy_delayed_work *dwork = from_timer(dwork, t, timer);
1713 
1714 	wiphy_work_queue(dwork->wiphy, &dwork->work);
1715 }
1716 EXPORT_SYMBOL(wiphy_delayed_work_timer);
1717 
wiphy_delayed_work_queue(struct wiphy * wiphy,struct wiphy_delayed_work * dwork,unsigned long delay)1718 void wiphy_delayed_work_queue(struct wiphy *wiphy,
1719 			      struct wiphy_delayed_work *dwork,
1720 			      unsigned long delay)
1721 {
1722 	trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay);
1723 
1724 	if (!delay) {
1725 		del_timer(&dwork->timer);
1726 		wiphy_work_queue(wiphy, &dwork->work);
1727 		return;
1728 	}
1729 
1730 	dwork->wiphy = wiphy;
1731 	mod_timer(&dwork->timer, jiffies + delay);
1732 }
1733 EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
1734 
wiphy_delayed_work_cancel(struct wiphy * wiphy,struct wiphy_delayed_work * dwork)1735 void wiphy_delayed_work_cancel(struct wiphy *wiphy,
1736 			       struct wiphy_delayed_work *dwork)
1737 {
1738 	lockdep_assert_held(&wiphy->mtx);
1739 
1740 	del_timer_sync(&dwork->timer);
1741 	wiphy_work_cancel(wiphy, &dwork->work);
1742 }
1743 EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
1744 
wiphy_delayed_work_flush(struct wiphy * wiphy,struct wiphy_delayed_work * dwork)1745 void wiphy_delayed_work_flush(struct wiphy *wiphy,
1746 			      struct wiphy_delayed_work *dwork)
1747 {
1748 	lockdep_assert_held(&wiphy->mtx);
1749 
1750 	del_timer_sync(&dwork->timer);
1751 	wiphy_work_flush(wiphy, &dwork->work);
1752 }
1753 EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
1754 
wiphy_delayed_work_pending(struct wiphy * wiphy,struct wiphy_delayed_work * dwork)1755 bool wiphy_delayed_work_pending(struct wiphy *wiphy,
1756 				struct wiphy_delayed_work *dwork)
1757 {
1758 	return timer_pending(&dwork->timer);
1759 }
1760 EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
1761 
cfg80211_init(void)1762 static int __init cfg80211_init(void)
1763 {
1764 	int err;
1765 
1766 	err = register_pernet_device(&cfg80211_pernet_ops);
1767 	if (err)
1768 		goto out_fail_pernet;
1769 
1770 	err = wiphy_sysfs_init();
1771 	if (err)
1772 		goto out_fail_sysfs;
1773 
1774 	err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1775 	if (err)
1776 		goto out_fail_notifier;
1777 
1778 	err = nl80211_init();
1779 	if (err)
1780 		goto out_fail_nl80211;
1781 
1782 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1783 
1784 	err = regulatory_init();
1785 	if (err)
1786 		goto out_fail_reg;
1787 
1788 	cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1789 	if (!cfg80211_wq) {
1790 		err = -ENOMEM;
1791 		goto out_fail_wq;
1792 	}
1793 
1794 	return 0;
1795 
1796 out_fail_wq:
1797 	regulatory_exit();
1798 out_fail_reg:
1799 	debugfs_remove(ieee80211_debugfs_dir);
1800 	nl80211_exit();
1801 out_fail_nl80211:
1802 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1803 out_fail_notifier:
1804 	wiphy_sysfs_exit();
1805 out_fail_sysfs:
1806 	unregister_pernet_device(&cfg80211_pernet_ops);
1807 out_fail_pernet:
1808 	return err;
1809 }
1810 fs_initcall(cfg80211_init);
1811 
cfg80211_exit(void)1812 static void __exit cfg80211_exit(void)
1813 {
1814 	debugfs_remove(ieee80211_debugfs_dir);
1815 	nl80211_exit();
1816 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1817 	wiphy_sysfs_exit();
1818 	regulatory_exit();
1819 	unregister_pernet_device(&cfg80211_pernet_ops);
1820 	destroy_workqueue(cfg80211_wq);
1821 }
1822 module_exit(cfg80211_exit);
1823