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