xref: /linux/net/wireless/core.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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->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 	/* Allocate radio configuration space for multi-radio wiphy */
999 	if (wiphy->n_radio > 0) {
1000 		int idx;
1001 
1002 		wiphy->radio_cfg = kcalloc(wiphy->n_radio,
1003 					   sizeof(*wiphy->radio_cfg),
1004 					   GFP_KERNEL);
1005 		if (!wiphy->radio_cfg)
1006 			return -ENOMEM;
1007 		/*
1008 		 * Initialize wiphy radio parameters to IEEE 802.11
1009 		 * MIB default values. RTS threshold is disabled by
1010 		 * default with the special -1 value.
1011 		 */
1012 		for (idx = 0; idx < wiphy->n_radio; idx++)
1013 			wiphy->radio_cfg[idx].rts_threshold = (u32)-1;
1014 	}
1015 
1016 	/* check and set up bitrates */
1017 	ieee80211_set_bitrate_flags(wiphy);
1018 
1019 	rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
1020 
1021 	rtnl_lock();
1022 	wiphy_lock(&rdev->wiphy);
1023 	res = device_add(&rdev->wiphy.dev);
1024 	if (res) {
1025 		wiphy_unlock(&rdev->wiphy);
1026 		rtnl_unlock();
1027 		return res;
1028 	}
1029 
1030 	list_add_rcu(&rdev->list, &cfg80211_rdev_list);
1031 	cfg80211_rdev_list_generation++;
1032 
1033 	/* add to debugfs */
1034 	rdev->wiphy.debugfsdir = debugfs_create_dir(wiphy_name(&rdev->wiphy),
1035 						    ieee80211_debugfs_dir);
1036 
1037 	cfg80211_debugfs_rdev_add(rdev);
1038 	nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
1039 	wiphy_unlock(&rdev->wiphy);
1040 
1041 	/* set up regulatory info */
1042 	wiphy_regulatory_register(wiphy);
1043 
1044 	if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
1045 		struct regulatory_request request;
1046 
1047 		request.wiphy_idx = get_wiphy_idx(wiphy);
1048 		request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
1049 		request.alpha2[0] = '9';
1050 		request.alpha2[1] = '9';
1051 
1052 		nl80211_send_reg_change_event(&request);
1053 	}
1054 
1055 	/* Check that nobody globally advertises any capabilities they do not
1056 	 * advertise on all possible interface types.
1057 	 */
1058 	if (wiphy->extended_capabilities_len &&
1059 	    wiphy->num_iftype_ext_capab &&
1060 	    wiphy->iftype_ext_capab) {
1061 		u8 supported_on_all, j;
1062 		const struct wiphy_iftype_ext_capab *capab;
1063 
1064 		capab = wiphy->iftype_ext_capab;
1065 		for (j = 0; j < wiphy->extended_capabilities_len; j++) {
1066 			if (capab[0].extended_capabilities_len > j)
1067 				supported_on_all =
1068 					capab[0].extended_capabilities[j];
1069 			else
1070 				supported_on_all = 0x00;
1071 			for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
1072 				if (j >= capab[i].extended_capabilities_len) {
1073 					supported_on_all = 0x00;
1074 					break;
1075 				}
1076 				supported_on_all &=
1077 					capab[i].extended_capabilities[j];
1078 			}
1079 			if (WARN_ON(wiphy->extended_capabilities[j] &
1080 				    ~supported_on_all))
1081 				break;
1082 		}
1083 	}
1084 
1085 	rdev->wiphy.registered = true;
1086 	rtnl_unlock();
1087 
1088 	res = rfkill_register(rdev->wiphy.rfkill);
1089 	if (res) {
1090 		rfkill_destroy(rdev->wiphy.rfkill);
1091 		rdev->wiphy.rfkill = NULL;
1092 		wiphy_unregister(&rdev->wiphy);
1093 		return res;
1094 	}
1095 
1096 	return 0;
1097 }
1098 EXPORT_SYMBOL(wiphy_register);
1099 
wiphy_rfkill_start_polling(struct wiphy * wiphy)1100 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
1101 {
1102 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1103 
1104 	if (!rdev->ops->rfkill_poll)
1105 		return;
1106 	rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
1107 	rfkill_resume_polling(wiphy->rfkill);
1108 }
1109 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
1110 
cfg80211_process_wiphy_works(struct cfg80211_registered_device * rdev,struct wiphy_work * end)1111 void cfg80211_process_wiphy_works(struct cfg80211_registered_device *rdev,
1112 				  struct wiphy_work *end)
1113 {
1114 	unsigned int runaway_limit = 100;
1115 	unsigned long flags;
1116 
1117 	lockdep_assert_held(&rdev->wiphy.mtx);
1118 
1119 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1120 	while (!list_empty(&rdev->wiphy_work_list)) {
1121 		struct wiphy_work *wk;
1122 
1123 		wk = list_first_entry(&rdev->wiphy_work_list,
1124 				      struct wiphy_work, entry);
1125 		list_del_init(&wk->entry);
1126 		spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1127 
1128 		trace_wiphy_work_run(&rdev->wiphy, wk);
1129 		wk->func(&rdev->wiphy, wk);
1130 
1131 		spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1132 
1133 		if (wk == end)
1134 			break;
1135 
1136 		if (WARN_ON(--runaway_limit == 0))
1137 			INIT_LIST_HEAD(&rdev->wiphy_work_list);
1138 	}
1139 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1140 }
1141 
wiphy_unregister(struct wiphy * wiphy)1142 void wiphy_unregister(struct wiphy *wiphy)
1143 {
1144 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1145 
1146 	wait_event(rdev->dev_wait, ({
1147 		int __count;
1148 		wiphy_lock(&rdev->wiphy);
1149 		__count = rdev->opencount;
1150 		wiphy_unlock(&rdev->wiphy);
1151 		__count == 0; }));
1152 
1153 	if (rdev->wiphy.rfkill)
1154 		rfkill_unregister(rdev->wiphy.rfkill);
1155 
1156 	rtnl_lock();
1157 	wiphy_lock(&rdev->wiphy);
1158 	nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
1159 	rdev->wiphy.registered = false;
1160 
1161 	WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
1162 
1163 	/*
1164 	 * First remove the hardware from everywhere, this makes
1165 	 * it impossible to find from userspace.
1166 	 */
1167 	debugfs_remove_recursive(rdev->wiphy.debugfsdir);
1168 	list_del_rcu(&rdev->list);
1169 	synchronize_rcu();
1170 
1171 	/*
1172 	 * If this device got a regulatory hint tell core its
1173 	 * free to listen now to a new shiny device regulatory hint
1174 	 */
1175 	wiphy_regulatory_deregister(wiphy);
1176 
1177 	cfg80211_rdev_list_generation++;
1178 	device_del(&rdev->wiphy.dev);
1179 
1180 #ifdef CONFIG_PM
1181 	if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
1182 		rdev_set_wakeup(rdev, false);
1183 #endif
1184 
1185 	/* surely nothing is reachable now, clean up work */
1186 	cfg80211_process_wiphy_works(rdev, NULL);
1187 	wiphy_unlock(&rdev->wiphy);
1188 	rtnl_unlock();
1189 
1190 	/* this has nothing to do now but make sure it's gone */
1191 	cancel_work_sync(&rdev->wiphy_work);
1192 
1193 	cancel_work_sync(&rdev->conn_work);
1194 	flush_work(&rdev->event_work);
1195 	cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
1196 	cancel_delayed_work_sync(&rdev->background_cac_done_wk);
1197 	flush_work(&rdev->destroy_work);
1198 	flush_work(&rdev->propagate_radar_detect_wk);
1199 	flush_work(&rdev->propagate_cac_done_wk);
1200 	flush_work(&rdev->mgmt_registrations_update_wk);
1201 	flush_work(&rdev->background_cac_abort_wk);
1202 
1203 	cfg80211_rdev_free_wowlan(rdev);
1204 	cfg80211_free_coalesce(rdev->coalesce);
1205 	rdev->coalesce = NULL;
1206 }
1207 EXPORT_SYMBOL(wiphy_unregister);
1208 
cfg80211_dev_free(struct cfg80211_registered_device * rdev)1209 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
1210 {
1211 	struct cfg80211_internal_bss *scan, *tmp;
1212 	struct cfg80211_beacon_registration *reg, *treg;
1213 	unsigned long flags;
1214 
1215 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1216 	WARN_ON(!list_empty(&rdev->wiphy_work_list));
1217 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1218 	cancel_work_sync(&rdev->wiphy_work);
1219 
1220 	rfkill_destroy(rdev->wiphy.rfkill);
1221 	list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
1222 		list_del(&reg->list);
1223 		kfree(reg);
1224 	}
1225 	list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
1226 		cfg80211_put_bss(&rdev->wiphy, &scan->pub);
1227 	mutex_destroy(&rdev->wiphy.mtx);
1228 
1229 	/*
1230 	 * The 'regd' can only be non-NULL if we never finished
1231 	 * initializing the wiphy and thus never went through the
1232 	 * unregister path - e.g. in failure scenarios. Thus, it
1233 	 * cannot have been visible to anyone if non-NULL, so we
1234 	 * can just free it here.
1235 	 */
1236 	kfree(rcu_dereference_raw(rdev->wiphy.regd));
1237 
1238 	kfree(rdev);
1239 }
1240 
wiphy_free(struct wiphy * wiphy)1241 void wiphy_free(struct wiphy *wiphy)
1242 {
1243 	kfree(wiphy->radio_cfg);
1244 	put_device(&wiphy->dev);
1245 }
1246 EXPORT_SYMBOL(wiphy_free);
1247 
wiphy_rfkill_set_hw_state_reason(struct wiphy * wiphy,bool blocked,enum rfkill_hard_block_reasons reason)1248 void wiphy_rfkill_set_hw_state_reason(struct wiphy *wiphy, bool blocked,
1249 				      enum rfkill_hard_block_reasons reason)
1250 {
1251 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1252 
1253 	if (rfkill_set_hw_state_reason(wiphy->rfkill, blocked, reason))
1254 		schedule_work(&rdev->rfkill_block);
1255 }
1256 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state_reason);
1257 
_cfg80211_unregister_wdev(struct wireless_dev * wdev,bool unregister_netdev)1258 static void _cfg80211_unregister_wdev(struct wireless_dev *wdev,
1259 				      bool unregister_netdev)
1260 {
1261 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1262 	struct cfg80211_cqm_config *cqm_config;
1263 	unsigned int link_id;
1264 
1265 	ASSERT_RTNL();
1266 	lockdep_assert_held(&rdev->wiphy.mtx);
1267 
1268 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
1269 
1270 	wdev->registered = false;
1271 
1272 	if (wdev->netdev) {
1273 		sysfs_remove_link(&wdev->netdev->dev.kobj, "phy80211");
1274 		if (unregister_netdev)
1275 			unregister_netdevice(wdev->netdev);
1276 	}
1277 
1278 	list_del_rcu(&wdev->list);
1279 	synchronize_net();
1280 	rdev->devlist_generation++;
1281 
1282 	cfg80211_mlme_purge_registrations(wdev);
1283 
1284 	switch (wdev->iftype) {
1285 	case NL80211_IFTYPE_P2P_DEVICE:
1286 		cfg80211_stop_p2p_device(rdev, wdev);
1287 		break;
1288 	case NL80211_IFTYPE_NAN:
1289 		cfg80211_stop_nan(rdev, wdev);
1290 		break;
1291 	default:
1292 		break;
1293 	}
1294 
1295 #ifdef CONFIG_CFG80211_WEXT
1296 	kfree_sensitive(wdev->wext.keys);
1297 	wdev->wext.keys = NULL;
1298 #endif
1299 	wiphy_work_cancel(wdev->wiphy, &wdev->cqm_rssi_work);
1300 	/* deleted from the list, so can't be found from nl80211 any more */
1301 	cqm_config = rcu_access_pointer(wdev->cqm_config);
1302 	kfree_rcu(cqm_config, rcu_head);
1303 	RCU_INIT_POINTER(wdev->cqm_config, NULL);
1304 
1305 	/*
1306 	 * Ensure that all events have been processed and
1307 	 * freed.
1308 	 */
1309 	cfg80211_process_wdev_events(wdev);
1310 
1311 	if (wdev->iftype == NL80211_IFTYPE_STATION ||
1312 	    wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1313 		for (link_id = 0; link_id < ARRAY_SIZE(wdev->links); link_id++) {
1314 			struct cfg80211_internal_bss *curbss;
1315 
1316 			curbss = wdev->links[link_id].client.current_bss;
1317 
1318 			if (WARN_ON(curbss)) {
1319 				cfg80211_unhold_bss(curbss);
1320 				cfg80211_put_bss(wdev->wiphy, &curbss->pub);
1321 				wdev->links[link_id].client.current_bss = NULL;
1322 			}
1323 		}
1324 	}
1325 
1326 	wdev->connected = false;
1327 }
1328 
cfg80211_unregister_wdev(struct wireless_dev * wdev)1329 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
1330 {
1331 	_cfg80211_unregister_wdev(wdev, true);
1332 }
1333 EXPORT_SYMBOL(cfg80211_unregister_wdev);
1334 
1335 static const struct device_type wiphy_type = {
1336 	.name	= "wlan",
1337 };
1338 
cfg80211_update_iface_num(struct cfg80211_registered_device * rdev,enum nl80211_iftype iftype,int num)1339 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
1340 			       enum nl80211_iftype iftype, int num)
1341 {
1342 	lockdep_assert_held(&rdev->wiphy.mtx);
1343 
1344 	rdev->num_running_ifaces += num;
1345 	if (iftype == NL80211_IFTYPE_MONITOR)
1346 		rdev->num_running_monitor_ifaces += num;
1347 }
1348 
cfg80211_leave(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)1349 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1350 		    struct wireless_dev *wdev)
1351 {
1352 	struct net_device *dev = wdev->netdev;
1353 	struct cfg80211_sched_scan_request *pos, *tmp;
1354 
1355 	lockdep_assert_held(&rdev->wiphy.mtx);
1356 
1357 	cfg80211_pmsr_wdev_down(wdev);
1358 
1359 	cfg80211_stop_background_radar_detection(wdev);
1360 
1361 	switch (wdev->iftype) {
1362 	case NL80211_IFTYPE_ADHOC:
1363 		cfg80211_leave_ibss(rdev, dev, true);
1364 		break;
1365 	case NL80211_IFTYPE_P2P_CLIENT:
1366 	case NL80211_IFTYPE_STATION:
1367 		list_for_each_entry_safe(pos, tmp, &rdev->sched_scan_req_list,
1368 					 list) {
1369 			if (dev == pos->dev)
1370 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1371 		}
1372 
1373 #ifdef CONFIG_CFG80211_WEXT
1374 		kfree(wdev->wext.ie);
1375 		wdev->wext.ie = NULL;
1376 		wdev->wext.ie_len = 0;
1377 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1378 #endif
1379 		cfg80211_disconnect(rdev, dev,
1380 				    WLAN_REASON_DEAUTH_LEAVING, true);
1381 		break;
1382 	case NL80211_IFTYPE_MESH_POINT:
1383 		cfg80211_leave_mesh(rdev, dev);
1384 		break;
1385 	case NL80211_IFTYPE_AP:
1386 	case NL80211_IFTYPE_P2P_GO:
1387 		cfg80211_stop_ap(rdev, dev, -1, true);
1388 		break;
1389 	case NL80211_IFTYPE_OCB:
1390 		cfg80211_leave_ocb(rdev, dev);
1391 		break;
1392 	case NL80211_IFTYPE_P2P_DEVICE:
1393 	case NL80211_IFTYPE_NAN:
1394 		/* cannot happen, has no netdev */
1395 		break;
1396 	case NL80211_IFTYPE_AP_VLAN:
1397 	case NL80211_IFTYPE_MONITOR:
1398 		/* nothing to do */
1399 		break;
1400 	case NL80211_IFTYPE_UNSPECIFIED:
1401 	case NL80211_IFTYPE_WDS:
1402 	case NUM_NL80211_IFTYPES:
1403 		/* invalid */
1404 		break;
1405 	}
1406 }
1407 
cfg80211_stop_iface(struct wiphy * wiphy,struct wireless_dev * wdev,gfp_t gfp)1408 void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1409 			 gfp_t gfp)
1410 {
1411 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1412 	struct cfg80211_event *ev;
1413 	unsigned long flags;
1414 
1415 	trace_cfg80211_stop_iface(wiphy, wdev);
1416 
1417 	ev = kzalloc(sizeof(*ev), gfp);
1418 	if (!ev)
1419 		return;
1420 
1421 	ev->type = EVENT_STOPPED;
1422 
1423 	spin_lock_irqsave(&wdev->event_lock, flags);
1424 	list_add_tail(&ev->list, &wdev->event_list);
1425 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1426 	queue_work(cfg80211_wq, &rdev->event_work);
1427 }
1428 EXPORT_SYMBOL(cfg80211_stop_iface);
1429 
cfg80211_init_wdev(struct wireless_dev * wdev)1430 void cfg80211_init_wdev(struct wireless_dev *wdev)
1431 {
1432 	INIT_LIST_HEAD(&wdev->event_list);
1433 	spin_lock_init(&wdev->event_lock);
1434 	INIT_LIST_HEAD(&wdev->mgmt_registrations);
1435 	INIT_LIST_HEAD(&wdev->pmsr_list);
1436 	spin_lock_init(&wdev->pmsr_lock);
1437 	INIT_WORK(&wdev->pmsr_free_wk, cfg80211_pmsr_free_wk);
1438 
1439 #ifdef CONFIG_CFG80211_WEXT
1440 	wdev->wext.default_key = -1;
1441 	wdev->wext.default_mgmt_key = -1;
1442 	wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1443 #endif
1444 
1445 	wiphy_work_init(&wdev->cqm_rssi_work, cfg80211_cqm_rssi_notify_work);
1446 
1447 	if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1448 		wdev->ps = true;
1449 	else
1450 		wdev->ps = false;
1451 	/* allow mac80211 to determine the timeout */
1452 	wdev->ps_timeout = -1;
1453 
1454 	wdev->radio_mask = BIT(wdev->wiphy->n_radio) - 1;
1455 
1456 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1457 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1458 	     wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1459 		wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;
1460 
1461 	INIT_WORK(&wdev->disconnect_wk, cfg80211_autodisconnect_wk);
1462 }
1463 
cfg80211_register_wdev(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)1464 void cfg80211_register_wdev(struct cfg80211_registered_device *rdev,
1465 			    struct wireless_dev *wdev)
1466 {
1467 	ASSERT_RTNL();
1468 	lockdep_assert_held(&rdev->wiphy.mtx);
1469 
1470 	/*
1471 	 * We get here also when the interface changes network namespaces,
1472 	 * as it's registered into the new one, but we don't want it to
1473 	 * change ID in that case. Checking if the ID is already assigned
1474 	 * works, because 0 isn't considered a valid ID and the memory is
1475 	 * 0-initialized.
1476 	 */
1477 	if (!wdev->identifier)
1478 		wdev->identifier = ++rdev->wdev_id;
1479 	list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1480 	rdev->devlist_generation++;
1481 	wdev->registered = true;
1482 
1483 	if (wdev->netdev &&
1484 	    sysfs_create_link(&wdev->netdev->dev.kobj, &rdev->wiphy.dev.kobj,
1485 			      "phy80211"))
1486 		pr_err("failed to add phy80211 symlink to netdev!\n");
1487 
1488 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1489 }
1490 
cfg80211_register_netdevice(struct net_device * dev)1491 int cfg80211_register_netdevice(struct net_device *dev)
1492 {
1493 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1494 	struct cfg80211_registered_device *rdev;
1495 	int ret;
1496 
1497 	ASSERT_RTNL();
1498 
1499 	if (WARN_ON(!wdev))
1500 		return -EINVAL;
1501 
1502 	rdev = wiphy_to_rdev(wdev->wiphy);
1503 
1504 	lockdep_assert_held(&rdev->wiphy.mtx);
1505 
1506 	/* we'll take care of this */
1507 	wdev->registered = true;
1508 	wdev->registering = true;
1509 	ret = register_netdevice(dev);
1510 	if (ret)
1511 		goto out;
1512 
1513 	cfg80211_register_wdev(rdev, wdev);
1514 	ret = 0;
1515 out:
1516 	wdev->registering = false;
1517 	if (ret)
1518 		wdev->registered = false;
1519 	return ret;
1520 }
1521 EXPORT_SYMBOL(cfg80211_register_netdevice);
1522 
cfg80211_netdev_notifier_call(struct notifier_block * nb,unsigned long state,void * ptr)1523 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1524 					 unsigned long state, void *ptr)
1525 {
1526 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1527 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1528 	struct cfg80211_registered_device *rdev;
1529 	struct cfg80211_sched_scan_request *pos, *tmp;
1530 
1531 	if (!wdev)
1532 		return NOTIFY_DONE;
1533 
1534 	rdev = wiphy_to_rdev(wdev->wiphy);
1535 
1536 	WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1537 
1538 	switch (state) {
1539 	case NETDEV_POST_INIT:
1540 		SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1541 		wdev->netdev = dev;
1542 		/* can only change netns with wiphy */
1543 		dev->netns_immutable = true;
1544 
1545 		cfg80211_init_wdev(wdev);
1546 		break;
1547 	case NETDEV_REGISTER:
1548 		if (!wdev->registered) {
1549 			guard(wiphy)(&rdev->wiphy);
1550 
1551 			cfg80211_register_wdev(rdev, wdev);
1552 		}
1553 		break;
1554 	case NETDEV_UNREGISTER:
1555 		/*
1556 		 * It is possible to get NETDEV_UNREGISTER multiple times,
1557 		 * so check wdev->registered.
1558 		 */
1559 		if (wdev->registered && !wdev->registering) {
1560 			guard(wiphy)(&rdev->wiphy);
1561 
1562 			_cfg80211_unregister_wdev(wdev, false);
1563 		}
1564 		break;
1565 	case NETDEV_GOING_DOWN:
1566 		scoped_guard(wiphy, &rdev->wiphy) {
1567 			cfg80211_leave(rdev, wdev);
1568 			cfg80211_remove_links(wdev);
1569 		}
1570 		/* since we just did cfg80211_leave() nothing to do there */
1571 		cancel_work_sync(&wdev->disconnect_wk);
1572 		cancel_work_sync(&wdev->pmsr_free_wk);
1573 		break;
1574 	case NETDEV_DOWN:
1575 		wiphy_lock(&rdev->wiphy);
1576 		cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1577 		if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) {
1578 			if (WARN_ON(!rdev->scan_req->notified &&
1579 				    (!rdev->int_scan_req ||
1580 				     !rdev->int_scan_req->notified)))
1581 				rdev->scan_req->info.aborted = true;
1582 			___cfg80211_scan_done(rdev, false);
1583 		}
1584 
1585 		list_for_each_entry_safe(pos, tmp,
1586 					 &rdev->sched_scan_req_list, list) {
1587 			if (WARN_ON(pos->dev == wdev->netdev))
1588 				cfg80211_stop_sched_scan_req(rdev, pos, false);
1589 		}
1590 
1591 		rdev->opencount--;
1592 		wiphy_unlock(&rdev->wiphy);
1593 		wake_up(&rdev->dev_wait);
1594 		break;
1595 	case NETDEV_UP:
1596 		wiphy_lock(&rdev->wiphy);
1597 		cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1598 		switch (wdev->iftype) {
1599 #ifdef CONFIG_CFG80211_WEXT
1600 		case NL80211_IFTYPE_ADHOC:
1601 			cfg80211_ibss_wext_join(rdev, wdev);
1602 			break;
1603 		case NL80211_IFTYPE_STATION:
1604 			cfg80211_mgd_wext_connect(rdev, wdev);
1605 			break;
1606 #endif
1607 #ifdef CONFIG_MAC80211_MESH
1608 		case NL80211_IFTYPE_MESH_POINT:
1609 			{
1610 				/* backward compat code... */
1611 				struct mesh_setup setup;
1612 				memcpy(&setup, &default_mesh_setup,
1613 						sizeof(setup));
1614 				 /* back compat only needed for mesh_id */
1615 				setup.mesh_id = wdev->u.mesh.id;
1616 				setup.mesh_id_len = wdev->u.mesh.id_up_len;
1617 				if (wdev->u.mesh.id_up_len)
1618 					__cfg80211_join_mesh(rdev, dev,
1619 							&setup,
1620 							&default_mesh_config);
1621 				break;
1622 			}
1623 #endif
1624 		default:
1625 			break;
1626 		}
1627 		rdev->opencount++;
1628 
1629 		/*
1630 		 * Configure power management to the driver here so that its
1631 		 * correctly set also after interface type changes etc.
1632 		 */
1633 		if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1634 		     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1635 		    rdev->ops->set_power_mgmt &&
1636 		    rdev_set_power_mgmt(rdev, dev, wdev->ps,
1637 					wdev->ps_timeout)) {
1638 			/* assume this means it's off */
1639 			wdev->ps = false;
1640 		}
1641 		wiphy_unlock(&rdev->wiphy);
1642 		break;
1643 	case NETDEV_PRE_UP:
1644 		if (!cfg80211_iftype_allowed(wdev->wiphy, wdev->iftype,
1645 					     wdev->use_4addr, 0))
1646 			return notifier_from_errno(-EOPNOTSUPP);
1647 
1648 		if (rfkill_blocked(rdev->wiphy.rfkill))
1649 			return notifier_from_errno(-ERFKILL);
1650 		break;
1651 	default:
1652 		return NOTIFY_DONE;
1653 	}
1654 
1655 	wireless_nlevent_flush();
1656 
1657 	return NOTIFY_OK;
1658 }
1659 
1660 static struct notifier_block cfg80211_netdev_notifier = {
1661 	.notifier_call = cfg80211_netdev_notifier_call,
1662 };
1663 
cfg80211_pernet_exit(struct net * net)1664 static void __net_exit cfg80211_pernet_exit(struct net *net)
1665 {
1666 	struct cfg80211_registered_device *rdev;
1667 
1668 	rtnl_lock();
1669 	for_each_rdev(rdev) {
1670 		if (net_eq(wiphy_net(&rdev->wiphy), net))
1671 			WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1672 	}
1673 	rtnl_unlock();
1674 }
1675 
1676 static struct pernet_operations cfg80211_pernet_ops = {
1677 	.exit = cfg80211_pernet_exit,
1678 };
1679 
wiphy_work_queue(struct wiphy * wiphy,struct wiphy_work * work)1680 void wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *work)
1681 {
1682 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1683 	unsigned long flags;
1684 
1685 	trace_wiphy_work_queue(wiphy, work);
1686 
1687 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1688 	if (list_empty(&work->entry))
1689 		list_add_tail(&work->entry, &rdev->wiphy_work_list);
1690 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1691 
1692 	queue_work(system_unbound_wq, &rdev->wiphy_work);
1693 }
1694 EXPORT_SYMBOL_GPL(wiphy_work_queue);
1695 
wiphy_work_cancel(struct wiphy * wiphy,struct wiphy_work * work)1696 void wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *work)
1697 {
1698 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1699 	unsigned long flags;
1700 
1701 	lockdep_assert_held(&wiphy->mtx);
1702 
1703 	trace_wiphy_work_cancel(wiphy, work);
1704 
1705 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1706 	if (!list_empty(&work->entry))
1707 		list_del_init(&work->entry);
1708 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1709 }
1710 EXPORT_SYMBOL_GPL(wiphy_work_cancel);
1711 
wiphy_work_flush(struct wiphy * wiphy,struct wiphy_work * work)1712 void wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *work)
1713 {
1714 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1715 	unsigned long flags;
1716 	bool run;
1717 
1718 	trace_wiphy_work_flush(wiphy, work);
1719 
1720 	spin_lock_irqsave(&rdev->wiphy_work_lock, flags);
1721 	run = !work || !list_empty(&work->entry);
1722 	spin_unlock_irqrestore(&rdev->wiphy_work_lock, flags);
1723 
1724 	if (run)
1725 		cfg80211_process_wiphy_works(rdev, work);
1726 }
1727 EXPORT_SYMBOL_GPL(wiphy_work_flush);
1728 
wiphy_delayed_work_timer(struct timer_list * t)1729 void wiphy_delayed_work_timer(struct timer_list *t)
1730 {
1731 	struct wiphy_delayed_work *dwork = timer_container_of(dwork, t, timer);
1732 
1733 	wiphy_work_queue(dwork->wiphy, &dwork->work);
1734 }
1735 EXPORT_SYMBOL(wiphy_delayed_work_timer);
1736 
wiphy_delayed_work_queue(struct wiphy * wiphy,struct wiphy_delayed_work * dwork,unsigned long delay)1737 void wiphy_delayed_work_queue(struct wiphy *wiphy,
1738 			      struct wiphy_delayed_work *dwork,
1739 			      unsigned long delay)
1740 {
1741 	trace_wiphy_delayed_work_queue(wiphy, &dwork->work, delay);
1742 
1743 	if (!delay) {
1744 		timer_delete(&dwork->timer);
1745 		wiphy_work_queue(wiphy, &dwork->work);
1746 		return;
1747 	}
1748 
1749 	dwork->wiphy = wiphy;
1750 	mod_timer(&dwork->timer, jiffies + delay);
1751 }
1752 EXPORT_SYMBOL_GPL(wiphy_delayed_work_queue);
1753 
wiphy_delayed_work_cancel(struct wiphy * wiphy,struct wiphy_delayed_work * dwork)1754 void wiphy_delayed_work_cancel(struct wiphy *wiphy,
1755 			       struct wiphy_delayed_work *dwork)
1756 {
1757 	lockdep_assert_held(&wiphy->mtx);
1758 
1759 	timer_delete_sync(&dwork->timer);
1760 	wiphy_work_cancel(wiphy, &dwork->work);
1761 }
1762 EXPORT_SYMBOL_GPL(wiphy_delayed_work_cancel);
1763 
wiphy_delayed_work_flush(struct wiphy * wiphy,struct wiphy_delayed_work * dwork)1764 void wiphy_delayed_work_flush(struct wiphy *wiphy,
1765 			      struct wiphy_delayed_work *dwork)
1766 {
1767 	lockdep_assert_held(&wiphy->mtx);
1768 
1769 	timer_delete_sync(&dwork->timer);
1770 	wiphy_work_flush(wiphy, &dwork->work);
1771 }
1772 EXPORT_SYMBOL_GPL(wiphy_delayed_work_flush);
1773 
wiphy_delayed_work_pending(struct wiphy * wiphy,struct wiphy_delayed_work * dwork)1774 bool wiphy_delayed_work_pending(struct wiphy *wiphy,
1775 				struct wiphy_delayed_work *dwork)
1776 {
1777 	return timer_pending(&dwork->timer);
1778 }
1779 EXPORT_SYMBOL_GPL(wiphy_delayed_work_pending);
1780 
cfg80211_init(void)1781 static int __init cfg80211_init(void)
1782 {
1783 	int err;
1784 
1785 	err = register_pernet_device(&cfg80211_pernet_ops);
1786 	if (err)
1787 		goto out_fail_pernet;
1788 
1789 	err = wiphy_sysfs_init();
1790 	if (err)
1791 		goto out_fail_sysfs;
1792 
1793 	err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1794 	if (err)
1795 		goto out_fail_notifier;
1796 
1797 	err = nl80211_init();
1798 	if (err)
1799 		goto out_fail_nl80211;
1800 
1801 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1802 
1803 	err = regulatory_init();
1804 	if (err)
1805 		goto out_fail_reg;
1806 
1807 	cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1808 	if (!cfg80211_wq) {
1809 		err = -ENOMEM;
1810 		goto out_fail_wq;
1811 	}
1812 
1813 	return 0;
1814 
1815 out_fail_wq:
1816 	regulatory_exit();
1817 out_fail_reg:
1818 	debugfs_remove(ieee80211_debugfs_dir);
1819 	nl80211_exit();
1820 out_fail_nl80211:
1821 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1822 out_fail_notifier:
1823 	wiphy_sysfs_exit();
1824 out_fail_sysfs:
1825 	unregister_pernet_device(&cfg80211_pernet_ops);
1826 out_fail_pernet:
1827 	return err;
1828 }
1829 fs_initcall(cfg80211_init);
1830 
cfg80211_exit(void)1831 static void __exit cfg80211_exit(void)
1832 {
1833 	debugfs_remove(ieee80211_debugfs_dir);
1834 	nl80211_exit();
1835 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1836 	wiphy_sysfs_exit();
1837 	regulatory_exit();
1838 	unregister_pernet_device(&cfg80211_pernet_ops);
1839 	destroy_workqueue(cfg80211_wq);
1840 }
1841 module_exit(cfg80211_exit);
1842