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