• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This is the linux wireless configuration interface.
3  *
4  * Copyright 2006-2010		Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2013-2014  Intel Mobile Communications GmbH
6  * Copyright 2015	Intel Deutschland GmbH
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/list.h>
15 #include <linux/slab.h>
16 #include <linux/nl80211.h>
17 #include <linux/debugfs.h>
18 #include <linux/notifier.h>
19 #include <linux/device.h>
20 #include <linux/etherdevice.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/sched.h>
23 #include <net/genetlink.h>
24 #include <net/cfg80211.h>
25 #include "nl80211.h"
26 #include "core.h"
27 #include "sysfs.h"
28 #include "debugfs.h"
29 #include "wext-compat.h"
30 #include "rdev-ops.h"
31 
32 /* name for sysfs, %d is appended */
33 #define PHY_NAME "phy"
34 
35 MODULE_AUTHOR("Johannes Berg");
36 MODULE_LICENSE("GPL");
37 MODULE_DESCRIPTION("wireless configuration support");
38 MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
39 
40 /* RCU-protected (and RTNL for writers) */
41 LIST_HEAD(cfg80211_rdev_list);
42 int cfg80211_rdev_list_generation;
43 
44 /* for debugfs */
45 static struct dentry *ieee80211_debugfs_dir;
46 
47 /* for the cleanup, scan and event works */
48 struct workqueue_struct *cfg80211_wq;
49 
50 static bool cfg80211_disable_40mhz_24ghz;
51 module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
52 MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
53 		 "Disable 40MHz support in the 2.4GHz band");
54 
cfg80211_rdev_by_wiphy_idx(int wiphy_idx)55 struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
56 {
57 	struct cfg80211_registered_device *result = NULL, *rdev;
58 
59 	ASSERT_RTNL();
60 
61 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
62 		if (rdev->wiphy_idx == wiphy_idx) {
63 			result = rdev;
64 			break;
65 		}
66 	}
67 
68 	return result;
69 }
70 
get_wiphy_idx(struct wiphy * wiphy)71 int get_wiphy_idx(struct wiphy *wiphy)
72 {
73 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
74 
75 	return rdev->wiphy_idx;
76 }
77 
wiphy_idx_to_wiphy(int wiphy_idx)78 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
79 {
80 	struct cfg80211_registered_device *rdev;
81 
82 	ASSERT_RTNL();
83 
84 	rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
85 	if (!rdev)
86 		return NULL;
87 	return &rdev->wiphy;
88 }
89 
cfg80211_dev_check_name(struct cfg80211_registered_device * rdev,const char * newname)90 static int cfg80211_dev_check_name(struct cfg80211_registered_device *rdev,
91 				   const char *newname)
92 {
93 	struct cfg80211_registered_device *rdev2;
94 	int wiphy_idx, taken = -1, digits;
95 
96 	ASSERT_RTNL();
97 
98 	/* prohibit calling the thing phy%d when %d is not its number */
99 	sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
100 	if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
101 		/* count number of places needed to print wiphy_idx */
102 		digits = 1;
103 		while (wiphy_idx /= 10)
104 			digits++;
105 		/*
106 		 * deny the name if it is phy<idx> where <idx> is printed
107 		 * without leading zeroes. taken == strlen(newname) here
108 		 */
109 		if (taken == strlen(PHY_NAME) + digits)
110 			return -EINVAL;
111 	}
112 
113 	/* Ensure another device does not already have this name. */
114 	list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
115 		if (strcmp(newname, wiphy_name(&rdev2->wiphy)) == 0)
116 			return -EINVAL;
117 
118 	return 0;
119 }
120 
cfg80211_dev_rename(struct cfg80211_registered_device * rdev,char * newname)121 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
122 			char *newname)
123 {
124 	int result;
125 
126 	ASSERT_RTNL();
127 
128 	/* Ignore nop renames */
129 	if (strcmp(newname, wiphy_name(&rdev->wiphy)) == 0)
130 		return 0;
131 
132 	result = cfg80211_dev_check_name(rdev, newname);
133 	if (result < 0)
134 		return result;
135 
136 	result = device_rename(&rdev->wiphy.dev, newname);
137 	if (result)
138 		return result;
139 
140 	if (rdev->wiphy.debugfsdir &&
141 	    !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
142 			    rdev->wiphy.debugfsdir,
143 			    rdev->wiphy.debugfsdir->d_parent,
144 			    newname))
145 		pr_err("failed to rename debugfs dir to %s!\n", newname);
146 
147 	nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
148 
149 	return 0;
150 }
151 
cfg80211_switch_netns(struct cfg80211_registered_device * rdev,struct net * net)152 int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
153 			  struct net *net)
154 {
155 	struct wireless_dev *wdev;
156 	int err = 0;
157 
158 	if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
159 		return -EOPNOTSUPP;
160 
161 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
162 		if (!wdev->netdev)
163 			continue;
164 		wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
165 		err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
166 		if (err)
167 			break;
168 		wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
169 	}
170 
171 	if (err) {
172 		/* failed -- clean up to old netns */
173 		net = wiphy_net(&rdev->wiphy);
174 
175 		list_for_each_entry_continue_reverse(wdev,
176 						     &rdev->wiphy.wdev_list,
177 						     list) {
178 			if (!wdev->netdev)
179 				continue;
180 			wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
181 			err = dev_change_net_namespace(wdev->netdev, net,
182 							"wlan%d");
183 			WARN_ON(err);
184 			wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
185 		}
186 
187 		return err;
188 	}
189 
190 	wiphy_net_set(&rdev->wiphy, net);
191 
192 	err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
193 	WARN_ON(err);
194 
195 	return 0;
196 }
197 
cfg80211_rfkill_poll(struct rfkill * rfkill,void * data)198 static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
199 {
200 	struct cfg80211_registered_device *rdev = data;
201 
202 	rdev_rfkill_poll(rdev);
203 }
204 
cfg80211_stop_p2p_device(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)205 void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
206 			      struct wireless_dev *wdev)
207 {
208 	ASSERT_RTNL();
209 
210 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
211 		return;
212 
213 	if (!wdev->p2p_started)
214 		return;
215 
216 	rdev_stop_p2p_device(rdev, wdev);
217 	wdev->p2p_started = false;
218 
219 	rdev->opencount--;
220 
221 	if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
222 		if (WARN_ON(!rdev->scan_req->notified))
223 			rdev->scan_req->info.aborted = true;
224 		___cfg80211_scan_done(rdev, false);
225 	}
226 }
227 
cfg80211_stop_nan(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)228 void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
229 		       struct wireless_dev *wdev)
230 {
231 	ASSERT_RTNL();
232 
233 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_NAN))
234 		return;
235 
236 	if (!wdev->nan_started)
237 		return;
238 
239 	rdev_stop_nan(rdev, wdev);
240 	wdev->nan_started = false;
241 
242 	rdev->opencount--;
243 }
244 
cfg80211_shutdown_all_interfaces(struct wiphy * wiphy)245 void cfg80211_shutdown_all_interfaces(struct wiphy *wiphy)
246 {
247 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
248 	struct wireless_dev *wdev;
249 
250 	ASSERT_RTNL();
251 
252 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
253 		if (wdev->netdev) {
254 			dev_close(wdev->netdev);
255 			continue;
256 		}
257 		/* otherwise, check iftype */
258 		switch (wdev->iftype) {
259 		case NL80211_IFTYPE_P2P_DEVICE:
260 			cfg80211_stop_p2p_device(rdev, wdev);
261 			break;
262 		case NL80211_IFTYPE_NAN:
263 			cfg80211_stop_nan(rdev, wdev);
264 			break;
265 		default:
266 			break;
267 		}
268 	}
269 }
270 EXPORT_SYMBOL_GPL(cfg80211_shutdown_all_interfaces);
271 
cfg80211_rfkill_set_block(void * data,bool blocked)272 static int cfg80211_rfkill_set_block(void *data, bool blocked)
273 {
274 	struct cfg80211_registered_device *rdev = data;
275 
276 	if (!blocked)
277 		return 0;
278 
279 	rtnl_lock();
280 	cfg80211_shutdown_all_interfaces(&rdev->wiphy);
281 	rtnl_unlock();
282 
283 	return 0;
284 }
285 
cfg80211_rfkill_sync_work(struct work_struct * work)286 static void cfg80211_rfkill_sync_work(struct work_struct *work)
287 {
288 	struct cfg80211_registered_device *rdev;
289 
290 	rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
291 	cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
292 }
293 
cfg80211_event_work(struct work_struct * work)294 static void cfg80211_event_work(struct work_struct *work)
295 {
296 	struct cfg80211_registered_device *rdev;
297 
298 	rdev = container_of(work, struct cfg80211_registered_device,
299 			    event_work);
300 
301 	rtnl_lock();
302 	cfg80211_process_rdev_events(rdev);
303 	rtnl_unlock();
304 }
305 
cfg80211_destroy_ifaces(struct cfg80211_registered_device * rdev)306 void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev)
307 {
308 	struct cfg80211_iface_destroy *item;
309 
310 	ASSERT_RTNL();
311 
312 	spin_lock_irq(&rdev->destroy_list_lock);
313 	while ((item = list_first_entry_or_null(&rdev->destroy_list,
314 						struct cfg80211_iface_destroy,
315 						list))) {
316 		struct wireless_dev *wdev, *tmp;
317 		u32 nlportid = item->nlportid;
318 
319 		list_del(&item->list);
320 		kfree(item);
321 		spin_unlock_irq(&rdev->destroy_list_lock);
322 
323 		list_for_each_entry_safe(wdev, tmp,
324 					 &rdev->wiphy.wdev_list, list) {
325 			if (nlportid == wdev->owner_nlportid)
326 				rdev_del_virtual_intf(rdev, wdev);
327 		}
328 
329 		spin_lock_irq(&rdev->destroy_list_lock);
330 	}
331 	spin_unlock_irq(&rdev->destroy_list_lock);
332 }
333 
cfg80211_destroy_iface_wk(struct work_struct * work)334 static void cfg80211_destroy_iface_wk(struct work_struct *work)
335 {
336 	struct cfg80211_registered_device *rdev;
337 
338 	rdev = container_of(work, struct cfg80211_registered_device,
339 			    destroy_work);
340 
341 	rtnl_lock();
342 	cfg80211_destroy_ifaces(rdev);
343 	rtnl_unlock();
344 }
345 
cfg80211_sched_scan_stop_wk(struct work_struct * work)346 static void cfg80211_sched_scan_stop_wk(struct work_struct *work)
347 {
348 	struct cfg80211_registered_device *rdev;
349 
350 	rdev = container_of(work, struct cfg80211_registered_device,
351 			   sched_scan_stop_wk);
352 
353 	rtnl_lock();
354 
355 	__cfg80211_stop_sched_scan(rdev, false);
356 
357 	rtnl_unlock();
358 }
359 
360 /* exported functions */
361 
wiphy_new_nm(const struct cfg80211_ops * ops,int sizeof_priv,const char * requested_name)362 struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,
363 			   const char *requested_name)
364 {
365 	static atomic_t wiphy_counter = ATOMIC_INIT(0);
366 
367 	struct cfg80211_registered_device *rdev;
368 	int alloc_size;
369 
370 	WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
371 	WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
372 	WARN_ON(ops->connect && !ops->disconnect);
373 	WARN_ON(ops->join_ibss && !ops->leave_ibss);
374 	WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
375 	WARN_ON(ops->add_station && !ops->del_station);
376 	WARN_ON(ops->add_mpath && !ops->del_mpath);
377 	WARN_ON(ops->join_mesh && !ops->leave_mesh);
378 	WARN_ON(ops->start_p2p_device && !ops->stop_p2p_device);
379 	WARN_ON(ops->start_ap && !ops->stop_ap);
380 	WARN_ON(ops->join_ocb && !ops->leave_ocb);
381 	WARN_ON(ops->suspend && !ops->resume);
382 	WARN_ON(ops->sched_scan_start && !ops->sched_scan_stop);
383 	WARN_ON(ops->remain_on_channel && !ops->cancel_remain_on_channel);
384 	WARN_ON(ops->tdls_channel_switch && !ops->tdls_cancel_channel_switch);
385 	WARN_ON(ops->add_tx_ts && !ops->del_tx_ts);
386 
387 	alloc_size = sizeof(*rdev) + sizeof_priv;
388 
389 	rdev = kzalloc(alloc_size, GFP_KERNEL);
390 	if (!rdev)
391 		return NULL;
392 
393 	rdev->ops = ops;
394 
395 	rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
396 
397 	if (unlikely(rdev->wiphy_idx < 0)) {
398 		/* ugh, wrapped! */
399 		atomic_dec(&wiphy_counter);
400 		kfree(rdev);
401 		return NULL;
402 	}
403 
404 	/* atomic_inc_return makes it start at 1, make it start at 0 */
405 	rdev->wiphy_idx--;
406 
407 	/* give it a proper name */
408 	if (requested_name && requested_name[0]) {
409 		int rv;
410 
411 		rtnl_lock();
412 		rv = cfg80211_dev_check_name(rdev, requested_name);
413 
414 		if (rv < 0) {
415 			rtnl_unlock();
416 			goto use_default_name;
417 		}
418 
419 		rv = dev_set_name(&rdev->wiphy.dev, "%s", requested_name);
420 		rtnl_unlock();
421 		if (rv)
422 			goto use_default_name;
423 	} else {
424 		int rv;
425 
426 use_default_name:
427 		/* NOTE:  This is *probably* safe w/out holding rtnl because of
428 		 * the restrictions on phy names.  Probably this call could
429 		 * fail if some other part of the kernel (re)named a device
430 		 * phyX.  But, might should add some locking and check return
431 		 * value, and use a different name if this one exists?
432 		 */
433 		rv = dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
434 		if (rv < 0) {
435 			kfree(rdev);
436 			return NULL;
437 		}
438 	}
439 
440 	INIT_LIST_HEAD(&rdev->wiphy.wdev_list);
441 	INIT_LIST_HEAD(&rdev->beacon_registrations);
442 	spin_lock_init(&rdev->beacon_registrations_lock);
443 	spin_lock_init(&rdev->bss_lock);
444 	INIT_LIST_HEAD(&rdev->bss_list);
445 	INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
446 	INIT_WORK(&rdev->sched_scan_results_wk, __cfg80211_sched_scan_results);
447 	INIT_LIST_HEAD(&rdev->mlme_unreg);
448 	spin_lock_init(&rdev->mlme_unreg_lock);
449 	INIT_WORK(&rdev->mlme_unreg_wk, cfg80211_mlme_unreg_wk);
450 	INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
451 			  cfg80211_dfs_channels_update_work);
452 #ifdef CONFIG_CFG80211_WEXT
453 	rdev->wiphy.wext = &cfg80211_wext_handler;
454 #endif
455 
456 	device_initialize(&rdev->wiphy.dev);
457 	rdev->wiphy.dev.class = &ieee80211_class;
458 	rdev->wiphy.dev.platform_data = rdev;
459 	device_enable_async_suspend(&rdev->wiphy.dev);
460 
461 	INIT_LIST_HEAD(&rdev->destroy_list);
462 	spin_lock_init(&rdev->destroy_list_lock);
463 	INIT_WORK(&rdev->destroy_work, cfg80211_destroy_iface_wk);
464 	INIT_WORK(&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);
465 
466 #ifdef CONFIG_CFG80211_DEFAULT_PS
467 	rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
468 #endif
469 
470 	wiphy_net_set(&rdev->wiphy, &init_net);
471 
472 	rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
473 	rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
474 				   &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
475 				   &rdev->rfkill_ops, rdev);
476 
477 	if (!rdev->rfkill) {
478 		kfree(rdev);
479 		return NULL;
480 	}
481 
482 	INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
483 	INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
484 	INIT_WORK(&rdev->event_work, cfg80211_event_work);
485 
486 	init_waitqueue_head(&rdev->dev_wait);
487 
488 	/*
489 	 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
490 	 * Fragmentation and RTS threshold are disabled by default with the
491 	 * special -1 value.
492 	 */
493 	rdev->wiphy.retry_short = 7;
494 	rdev->wiphy.retry_long = 4;
495 	rdev->wiphy.frag_threshold = (u32) -1;
496 	rdev->wiphy.rts_threshold = (u32) -1;
497 	rdev->wiphy.coverage_class = 0;
498 
499 	rdev->wiphy.max_num_csa_counters = 1;
500 
501 	rdev->wiphy.max_sched_scan_plans = 1;
502 	rdev->wiphy.max_sched_scan_plan_interval = U32_MAX;
503 
504 	return &rdev->wiphy;
505 }
506 EXPORT_SYMBOL(wiphy_new_nm);
507 
wiphy_verify_combinations(struct wiphy * wiphy)508 static int wiphy_verify_combinations(struct wiphy *wiphy)
509 {
510 	const struct ieee80211_iface_combination *c;
511 	int i, j;
512 
513 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
514 		u32 cnt = 0;
515 		u16 all_iftypes = 0;
516 
517 		c = &wiphy->iface_combinations[i];
518 
519 		/*
520 		 * Combinations with just one interface aren't real,
521 		 * however we make an exception for DFS.
522 		 */
523 		if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
524 			return -EINVAL;
525 
526 		/* Need at least one channel */
527 		if (WARN_ON(!c->num_different_channels))
528 			return -EINVAL;
529 
530 		/*
531 		 * Put a sane limit on maximum number of different
532 		 * channels to simplify channel accounting code.
533 		 */
534 		if (WARN_ON(c->num_different_channels >
535 				CFG80211_MAX_NUM_DIFFERENT_CHANNELS))
536 			return -EINVAL;
537 
538 		/* DFS only works on one channel. */
539 		if (WARN_ON(c->radar_detect_widths &&
540 			    (c->num_different_channels > 1)))
541 			return -EINVAL;
542 
543 		if (WARN_ON(!c->n_limits))
544 			return -EINVAL;
545 
546 		for (j = 0; j < c->n_limits; j++) {
547 			u16 types = c->limits[j].types;
548 
549 			/* interface types shouldn't overlap */
550 			if (WARN_ON(types & all_iftypes))
551 				return -EINVAL;
552 			all_iftypes |= types;
553 
554 			if (WARN_ON(!c->limits[j].max))
555 				return -EINVAL;
556 
557 			/* Shouldn't list software iftypes in combinations! */
558 			if (WARN_ON(wiphy->software_iftypes & types))
559 				return -EINVAL;
560 
561 			/* Only a single P2P_DEVICE can be allowed */
562 			if (WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
563 				    c->limits[j].max > 1))
564 				return -EINVAL;
565 
566 			/* Only a single NAN can be allowed */
567 			if (WARN_ON(types & BIT(NL80211_IFTYPE_NAN) &&
568 				    c->limits[j].max > 1))
569 				return -EINVAL;
570 
571 			cnt += c->limits[j].max;
572 			/*
573 			 * Don't advertise an unsupported type
574 			 * in a combination.
575 			 */
576 			if (WARN_ON((wiphy->interface_modes & types) != types))
577 				return -EINVAL;
578 		}
579 
580 		/* You can't even choose that many! */
581 		if (WARN_ON(cnt < c->max_interfaces))
582 			return -EINVAL;
583 	}
584 
585 	return 0;
586 }
587 
wiphy_register(struct wiphy * wiphy)588 int wiphy_register(struct wiphy *wiphy)
589 {
590 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
591 	int res;
592 	enum nl80211_band band;
593 	struct ieee80211_supported_band *sband;
594 	bool have_band = false;
595 	int i;
596 	u16 ifmodes = wiphy->interface_modes;
597 
598 #ifdef CONFIG_PM
599 	if (WARN_ON(wiphy->wowlan &&
600 		    (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
601 		    !(wiphy->wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
602 		return -EINVAL;
603 	if (WARN_ON(wiphy->wowlan &&
604 		    !wiphy->wowlan->flags && !wiphy->wowlan->n_patterns &&
605 		    !wiphy->wowlan->tcp))
606 		return -EINVAL;
607 #endif
608 	if (WARN_ON((wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
609 		    (!rdev->ops->tdls_channel_switch ||
610 		     !rdev->ops->tdls_cancel_channel_switch)))
611 		return -EINVAL;
612 
613 	if (WARN_ON((wiphy->interface_modes & BIT(NL80211_IFTYPE_NAN)) &&
614 		    (!rdev->ops->start_nan || !rdev->ops->stop_nan ||
615 		     !rdev->ops->add_nan_func || !rdev->ops->del_nan_func)))
616 		return -EINVAL;
617 
618 	/*
619 	 * if a wiphy has unsupported modes for regulatory channel enforcement,
620 	 * opt-out of enforcement checking
621 	 */
622 	if (wiphy->interface_modes & ~(BIT(NL80211_IFTYPE_STATION) |
623 				       BIT(NL80211_IFTYPE_P2P_CLIENT) |
624 				       BIT(NL80211_IFTYPE_AP) |
625 				       BIT(NL80211_IFTYPE_P2P_GO) |
626 				       BIT(NL80211_IFTYPE_ADHOC) |
627 				       BIT(NL80211_IFTYPE_P2P_DEVICE) |
628 				       BIT(NL80211_IFTYPE_NAN) |
629 				       BIT(NL80211_IFTYPE_AP_VLAN) |
630 				       BIT(NL80211_IFTYPE_MONITOR)))
631 		wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF;
632 
633 	if (WARN_ON((wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) &&
634 		    (wiphy->regulatory_flags &
635 					(REGULATORY_CUSTOM_REG |
636 					 REGULATORY_STRICT_REG |
637 					 REGULATORY_COUNTRY_IE_FOLLOW_POWER |
638 					 REGULATORY_COUNTRY_IE_IGNORE))))
639 		return -EINVAL;
640 
641 	if (WARN_ON(wiphy->coalesce &&
642 		    (!wiphy->coalesce->n_rules ||
643 		     !wiphy->coalesce->n_patterns) &&
644 		    (!wiphy->coalesce->pattern_min_len ||
645 		     wiphy->coalesce->pattern_min_len >
646 			wiphy->coalesce->pattern_max_len)))
647 		return -EINVAL;
648 
649 	if (WARN_ON(wiphy->ap_sme_capa &&
650 		    !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
651 		return -EINVAL;
652 
653 	if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
654 		return -EINVAL;
655 
656 	if (WARN_ON(wiphy->addresses &&
657 		    !is_zero_ether_addr(wiphy->perm_addr) &&
658 		    memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
659 			   ETH_ALEN)))
660 		return -EINVAL;
661 
662 	if (WARN_ON(wiphy->max_acl_mac_addrs &&
663 		    (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
664 		     !rdev->ops->set_mac_acl)))
665 		return -EINVAL;
666 
667 	/* assure only valid behaviours are flagged by driver
668 	 * hence subtract 2 as bit 0 is invalid.
669 	 */
670 	if (WARN_ON(wiphy->bss_select_support &&
671 		    (wiphy->bss_select_support & ~(BIT(__NL80211_BSS_SELECT_ATTR_AFTER_LAST) - 2))))
672 		return -EINVAL;
673 
674 	if (wiphy->addresses)
675 		memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
676 
677 	/* sanity check ifmodes */
678 	WARN_ON(!ifmodes);
679 	ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
680 	if (WARN_ON(ifmodes != wiphy->interface_modes))
681 		wiphy->interface_modes = ifmodes;
682 
683 	res = wiphy_verify_combinations(wiphy);
684 	if (res)
685 		return res;
686 
687 	/* sanity check supported bands/channels */
688 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
689 		sband = wiphy->bands[band];
690 		if (!sband)
691 			continue;
692 
693 		sband->band = band;
694 		if (WARN_ON(!sband->n_channels))
695 			return -EINVAL;
696 		/*
697 		 * on 60GHz band, there are no legacy rates, so
698 		 * n_bitrates is 0
699 		 */
700 		if (WARN_ON(band != NL80211_BAND_60GHZ &&
701 			    !sband->n_bitrates))
702 			return -EINVAL;
703 
704 		/*
705 		 * Since cfg80211_disable_40mhz_24ghz is global, we can
706 		 * modify the sband's ht data even if the driver uses a
707 		 * global structure for that.
708 		 */
709 		if (cfg80211_disable_40mhz_24ghz &&
710 		    band == NL80211_BAND_2GHZ &&
711 		    sband->ht_cap.ht_supported) {
712 			sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
713 			sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
714 		}
715 
716 		/*
717 		 * Since we use a u32 for rate bitmaps in
718 		 * ieee80211_get_response_rate, we cannot
719 		 * have more than 32 legacy rates.
720 		 */
721 		if (WARN_ON(sband->n_bitrates > 32))
722 			return -EINVAL;
723 
724 		for (i = 0; i < sband->n_channels; i++) {
725 			sband->channels[i].orig_flags =
726 				sband->channels[i].flags;
727 			sband->channels[i].orig_mag = INT_MAX;
728 			sband->channels[i].orig_mpwr =
729 				sband->channels[i].max_power;
730 			sband->channels[i].band = band;
731 		}
732 
733 		have_band = true;
734 	}
735 
736 	if (!have_band) {
737 		WARN_ON(1);
738 		return -EINVAL;
739 	}
740 
741 #ifdef CONFIG_PM
742 	if (WARN_ON(rdev->wiphy.wowlan && rdev->wiphy.wowlan->n_patterns &&
743 		    (!rdev->wiphy.wowlan->pattern_min_len ||
744 		     rdev->wiphy.wowlan->pattern_min_len >
745 				rdev->wiphy.wowlan->pattern_max_len)))
746 		return -EINVAL;
747 #endif
748 
749 	/* check and set up bitrates */
750 	ieee80211_set_bitrate_flags(wiphy);
751 
752 	rdev->wiphy.features |= NL80211_FEATURE_SCAN_FLUSH;
753 
754 	rtnl_lock();
755 	res = device_add(&rdev->wiphy.dev);
756 	if (res) {
757 		rtnl_unlock();
758 		return res;
759 	}
760 
761 	/* set up regulatory info */
762 	wiphy_regulatory_register(wiphy);
763 
764 	list_add_rcu(&rdev->list, &cfg80211_rdev_list);
765 	cfg80211_rdev_list_generation++;
766 
767 	/* add to debugfs */
768 	rdev->wiphy.debugfsdir =
769 		debugfs_create_dir(wiphy_name(&rdev->wiphy),
770 				   ieee80211_debugfs_dir);
771 	if (IS_ERR(rdev->wiphy.debugfsdir))
772 		rdev->wiphy.debugfsdir = NULL;
773 
774 	cfg80211_debugfs_rdev_add(rdev);
775 	nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
776 
777 	if (wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
778 		struct regulatory_request request;
779 
780 		request.wiphy_idx = get_wiphy_idx(wiphy);
781 		request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
782 		request.alpha2[0] = '9';
783 		request.alpha2[1] = '9';
784 
785 		nl80211_send_reg_change_event(&request);
786 	}
787 
788 	/* Check that nobody globally advertises any capabilities they do not
789 	 * advertise on all possible interface types.
790 	 */
791 	if (wiphy->extended_capabilities_len &&
792 	    wiphy->num_iftype_ext_capab &&
793 	    wiphy->iftype_ext_capab) {
794 		u8 supported_on_all, j;
795 		const struct wiphy_iftype_ext_capab *capab;
796 
797 		capab = wiphy->iftype_ext_capab;
798 		for (j = 0; j < wiphy->extended_capabilities_len; j++) {
799 			if (capab[0].extended_capabilities_len > j)
800 				supported_on_all =
801 					capab[0].extended_capabilities[j];
802 			else
803 				supported_on_all = 0x00;
804 			for (i = 1; i < wiphy->num_iftype_ext_capab; i++) {
805 				if (j >= capab[i].extended_capabilities_len) {
806 					supported_on_all = 0x00;
807 					break;
808 				}
809 				supported_on_all &=
810 					capab[i].extended_capabilities[j];
811 			}
812 			if (WARN_ON(wiphy->extended_capabilities[j] &
813 				    ~supported_on_all))
814 				break;
815 		}
816 	}
817 
818 	rdev->wiphy.registered = true;
819 	rtnl_unlock();
820 
821 	res = rfkill_register(rdev->rfkill);
822 	if (res) {
823 		rfkill_destroy(rdev->rfkill);
824 		rdev->rfkill = NULL;
825 		wiphy_unregister(&rdev->wiphy);
826 		return res;
827 	}
828 
829 	return 0;
830 }
831 EXPORT_SYMBOL(wiphy_register);
832 
wiphy_rfkill_start_polling(struct wiphy * wiphy)833 void wiphy_rfkill_start_polling(struct wiphy *wiphy)
834 {
835 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
836 
837 	if (!rdev->ops->rfkill_poll)
838 		return;
839 	rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
840 	rfkill_resume_polling(rdev->rfkill);
841 }
842 EXPORT_SYMBOL(wiphy_rfkill_start_polling);
843 
wiphy_rfkill_stop_polling(struct wiphy * wiphy)844 void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
845 {
846 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
847 
848 	rfkill_pause_polling(rdev->rfkill);
849 }
850 EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
851 
wiphy_unregister(struct wiphy * wiphy)852 void wiphy_unregister(struct wiphy *wiphy)
853 {
854 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
855 
856 	wait_event(rdev->dev_wait, ({
857 		int __count;
858 		rtnl_lock();
859 		__count = rdev->opencount;
860 		rtnl_unlock();
861 		__count == 0; }));
862 
863 	if (rdev->rfkill)
864 		rfkill_unregister(rdev->rfkill);
865 
866 	rtnl_lock();
867 	nl80211_notify_wiphy(rdev, NL80211_CMD_DEL_WIPHY);
868 	rdev->wiphy.registered = false;
869 
870 	WARN_ON(!list_empty(&rdev->wiphy.wdev_list));
871 
872 	/*
873 	 * First remove the hardware from everywhere, this makes
874 	 * it impossible to find from userspace.
875 	 */
876 	debugfs_remove_recursive(rdev->wiphy.debugfsdir);
877 	list_del_rcu(&rdev->list);
878 	synchronize_rcu();
879 
880 	/*
881 	 * If this device got a regulatory hint tell core its
882 	 * free to listen now to a new shiny device regulatory hint
883 	 */
884 	wiphy_regulatory_deregister(wiphy);
885 
886 	cfg80211_rdev_list_generation++;
887 	device_del(&rdev->wiphy.dev);
888 
889 	rtnl_unlock();
890 
891 	flush_work(&rdev->scan_done_wk);
892 	cancel_work_sync(&rdev->conn_work);
893 	flush_work(&rdev->event_work);
894 	cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
895 	flush_work(&rdev->destroy_work);
896 	flush_work(&rdev->sched_scan_stop_wk);
897 	flush_work(&rdev->mlme_unreg_wk);
898 
899 #ifdef CONFIG_PM
900 	if (rdev->wiphy.wowlan_config && rdev->ops->set_wakeup)
901 		rdev_set_wakeup(rdev, false);
902 #endif
903 	cfg80211_rdev_free_wowlan(rdev);
904 	cfg80211_rdev_free_coalesce(rdev);
905 }
906 EXPORT_SYMBOL(wiphy_unregister);
907 
cfg80211_dev_free(struct cfg80211_registered_device * rdev)908 void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
909 {
910 	struct cfg80211_internal_bss *scan, *tmp;
911 	struct cfg80211_beacon_registration *reg, *treg;
912 	rfkill_destroy(rdev->rfkill);
913 	list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
914 		list_del(&reg->list);
915 		kfree(reg);
916 	}
917 	list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
918 		cfg80211_put_bss(&rdev->wiphy, &scan->pub);
919 	kfree(rdev);
920 }
921 
wiphy_free(struct wiphy * wiphy)922 void wiphy_free(struct wiphy *wiphy)
923 {
924 	put_device(&wiphy->dev);
925 }
926 EXPORT_SYMBOL(wiphy_free);
927 
wiphy_rfkill_set_hw_state(struct wiphy * wiphy,bool blocked)928 void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
929 {
930 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
931 
932 	if (rfkill_set_hw_state(rdev->rfkill, blocked))
933 		schedule_work(&rdev->rfkill_sync);
934 }
935 EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
936 
cfg80211_unregister_wdev(struct wireless_dev * wdev)937 void cfg80211_unregister_wdev(struct wireless_dev *wdev)
938 {
939 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
940 
941 	ASSERT_RTNL();
942 
943 	if (WARN_ON(wdev->netdev))
944 		return;
945 
946 	nl80211_notify_iface(rdev, wdev, NL80211_CMD_DEL_INTERFACE);
947 
948 	list_del_rcu(&wdev->list);
949 	rdev->devlist_generation++;
950 
951 	switch (wdev->iftype) {
952 	case NL80211_IFTYPE_P2P_DEVICE:
953 		cfg80211_mlme_purge_registrations(wdev);
954 		cfg80211_stop_p2p_device(rdev, wdev);
955 		break;
956 	case NL80211_IFTYPE_NAN:
957 		cfg80211_stop_nan(rdev, wdev);
958 		break;
959 	default:
960 		WARN_ON_ONCE(1);
961 		break;
962 	}
963 }
964 EXPORT_SYMBOL(cfg80211_unregister_wdev);
965 
966 static const struct device_type wiphy_type = {
967 	.name	= "wlan",
968 };
969 
cfg80211_update_iface_num(struct cfg80211_registered_device * rdev,enum nl80211_iftype iftype,int num)970 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
971 			       enum nl80211_iftype iftype, int num)
972 {
973 	ASSERT_RTNL();
974 
975 	rdev->num_running_ifaces += num;
976 	if (iftype == NL80211_IFTYPE_MONITOR)
977 		rdev->num_running_monitor_ifaces += num;
978 }
979 
__cfg80211_leave(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)980 void __cfg80211_leave(struct cfg80211_registered_device *rdev,
981 		      struct wireless_dev *wdev)
982 {
983 	struct net_device *dev = wdev->netdev;
984 	struct cfg80211_sched_scan_request *sched_scan_req;
985 
986 	ASSERT_RTNL();
987 	ASSERT_WDEV_LOCK(wdev);
988 
989 	switch (wdev->iftype) {
990 	case NL80211_IFTYPE_ADHOC:
991 		__cfg80211_leave_ibss(rdev, dev, true);
992 		break;
993 	case NL80211_IFTYPE_P2P_CLIENT:
994 	case NL80211_IFTYPE_STATION:
995 		sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
996 		if (sched_scan_req && dev == sched_scan_req->dev)
997 			__cfg80211_stop_sched_scan(rdev, false);
998 
999 #ifdef CONFIG_CFG80211_WEXT
1000 		kfree(wdev->wext.ie);
1001 		wdev->wext.ie = NULL;
1002 		wdev->wext.ie_len = 0;
1003 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1004 #endif
1005 		cfg80211_disconnect(rdev, dev,
1006 				    WLAN_REASON_DEAUTH_LEAVING, true);
1007 		break;
1008 	case NL80211_IFTYPE_MESH_POINT:
1009 		__cfg80211_leave_mesh(rdev, dev);
1010 		break;
1011 	case NL80211_IFTYPE_AP:
1012 	case NL80211_IFTYPE_P2P_GO:
1013 		__cfg80211_stop_ap(rdev, dev, true);
1014 		break;
1015 	case NL80211_IFTYPE_OCB:
1016 		__cfg80211_leave_ocb(rdev, dev);
1017 		break;
1018 	case NL80211_IFTYPE_WDS:
1019 		/* must be handled by mac80211/driver, has no APIs */
1020 		break;
1021 	case NL80211_IFTYPE_P2P_DEVICE:
1022 	case NL80211_IFTYPE_NAN:
1023 		/* cannot happen, has no netdev */
1024 		break;
1025 	case NL80211_IFTYPE_AP_VLAN:
1026 	case NL80211_IFTYPE_MONITOR:
1027 		/* nothing to do */
1028 		break;
1029 	case NL80211_IFTYPE_UNSPECIFIED:
1030 	case NUM_NL80211_IFTYPES:
1031 		/* invalid */
1032 		break;
1033 	}
1034 }
1035 
cfg80211_leave(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)1036 void cfg80211_leave(struct cfg80211_registered_device *rdev,
1037 		    struct wireless_dev *wdev)
1038 {
1039 	wdev_lock(wdev);
1040 	__cfg80211_leave(rdev, wdev);
1041 	wdev_unlock(wdev);
1042 }
1043 
cfg80211_stop_iface(struct wiphy * wiphy,struct wireless_dev * wdev,gfp_t gfp)1044 void cfg80211_stop_iface(struct wiphy *wiphy, struct wireless_dev *wdev,
1045 			 gfp_t gfp)
1046 {
1047 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1048 	struct cfg80211_event *ev;
1049 	unsigned long flags;
1050 
1051 	trace_cfg80211_stop_iface(wiphy, wdev);
1052 
1053 	ev = kzalloc(sizeof(*ev), gfp);
1054 	if (!ev)
1055 		return;
1056 
1057 	ev->type = EVENT_STOPPED;
1058 
1059 	spin_lock_irqsave(&wdev->event_lock, flags);
1060 	list_add_tail(&ev->list, &wdev->event_list);
1061 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1062 	queue_work(cfg80211_wq, &rdev->event_work);
1063 }
1064 EXPORT_SYMBOL(cfg80211_stop_iface);
1065 
cfg80211_netdev_notifier_call(struct notifier_block * nb,unsigned long state,void * ptr)1066 static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
1067 					 unsigned long state, void *ptr)
1068 {
1069 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1070 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1071 	struct cfg80211_registered_device *rdev;
1072 	struct cfg80211_sched_scan_request *sched_scan_req;
1073 
1074 	if (!wdev)
1075 		return NOTIFY_DONE;
1076 
1077 	rdev = wiphy_to_rdev(wdev->wiphy);
1078 
1079 	WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
1080 
1081 	switch (state) {
1082 	case NETDEV_POST_INIT:
1083 		SET_NETDEV_DEVTYPE(dev, &wiphy_type);
1084 		break;
1085 	case NETDEV_REGISTER:
1086 		/*
1087 		 * NB: cannot take rdev->mtx here because this may be
1088 		 * called within code protected by it when interfaces
1089 		 * are added with nl80211.
1090 		 */
1091 		mutex_init(&wdev->mtx);
1092 		INIT_LIST_HEAD(&wdev->event_list);
1093 		spin_lock_init(&wdev->event_lock);
1094 		INIT_LIST_HEAD(&wdev->mgmt_registrations);
1095 		spin_lock_init(&wdev->mgmt_registrations_lock);
1096 
1097 		wdev->identifier = ++rdev->wdev_id;
1098 		list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
1099 		rdev->devlist_generation++;
1100 		/* can only change netns with wiphy */
1101 		dev->features |= NETIF_F_NETNS_LOCAL;
1102 
1103 		if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
1104 				      "phy80211")) {
1105 			pr_err("failed to add phy80211 symlink to netdev!\n");
1106 		}
1107 		wdev->netdev = dev;
1108 #ifdef CONFIG_CFG80211_WEXT
1109 		wdev->wext.default_key = -1;
1110 		wdev->wext.default_mgmt_key = -1;
1111 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1112 #endif
1113 
1114 		if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
1115 			wdev->ps = true;
1116 		else
1117 			wdev->ps = false;
1118 		/* allow mac80211 to determine the timeout */
1119 		wdev->ps_timeout = -1;
1120 
1121 		if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1122 		     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
1123 		     wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
1124 			dev->priv_flags |= IFF_DONT_BRIDGE;
1125 
1126 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
1127 		break;
1128 	case NETDEV_GOING_DOWN:
1129 		cfg80211_leave(rdev, wdev);
1130 		break;
1131 	case NETDEV_DOWN:
1132 		cfg80211_update_iface_num(rdev, wdev->iftype, -1);
1133 		if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
1134 			if (WARN_ON(!rdev->scan_req->notified))
1135 				rdev->scan_req->info.aborted = true;
1136 			___cfg80211_scan_done(rdev, false);
1137 		}
1138 
1139 		sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
1140 		if (WARN_ON(sched_scan_req &&
1141 			    sched_scan_req->dev == wdev->netdev)) {
1142 			__cfg80211_stop_sched_scan(rdev, false);
1143 		}
1144 
1145 		rdev->opencount--;
1146 		wake_up(&rdev->dev_wait);
1147 		break;
1148 	case NETDEV_UP:
1149 		cfg80211_update_iface_num(rdev, wdev->iftype, 1);
1150 		wdev_lock(wdev);
1151 		switch (wdev->iftype) {
1152 #ifdef CONFIG_CFG80211_WEXT
1153 		case NL80211_IFTYPE_ADHOC:
1154 			cfg80211_ibss_wext_join(rdev, wdev);
1155 			break;
1156 		case NL80211_IFTYPE_STATION:
1157 			cfg80211_mgd_wext_connect(rdev, wdev);
1158 			break;
1159 #endif
1160 #ifdef CONFIG_MAC80211_MESH
1161 		case NL80211_IFTYPE_MESH_POINT:
1162 			{
1163 				/* backward compat code... */
1164 				struct mesh_setup setup;
1165 				memcpy(&setup, &default_mesh_setup,
1166 						sizeof(setup));
1167 				 /* back compat only needed for mesh_id */
1168 				setup.mesh_id = wdev->ssid;
1169 				setup.mesh_id_len = wdev->mesh_id_up_len;
1170 				if (wdev->mesh_id_up_len)
1171 					__cfg80211_join_mesh(rdev, dev,
1172 							&setup,
1173 							&default_mesh_config);
1174 				break;
1175 			}
1176 #endif
1177 		default:
1178 			break;
1179 		}
1180 		wdev_unlock(wdev);
1181 		rdev->opencount++;
1182 
1183 		/*
1184 		 * Configure power management to the driver here so that its
1185 		 * correctly set also after interface type changes etc.
1186 		 */
1187 		if ((wdev->iftype == NL80211_IFTYPE_STATION ||
1188 		     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
1189 		    rdev->ops->set_power_mgmt)
1190 			if (rdev_set_power_mgmt(rdev, dev, wdev->ps,
1191 						wdev->ps_timeout)) {
1192 				/* assume this means it's off */
1193 				wdev->ps = false;
1194 			}
1195 		break;
1196 	case NETDEV_UNREGISTER:
1197 		/*
1198 		 * It is possible to get NETDEV_UNREGISTER
1199 		 * multiple times. To detect that, check
1200 		 * that the interface is still on the list
1201 		 * of registered interfaces, and only then
1202 		 * remove and clean it up.
1203 		 */
1204 		if (!list_empty(&wdev->list)) {
1205 			nl80211_notify_iface(rdev, wdev,
1206 					     NL80211_CMD_DEL_INTERFACE);
1207 			sysfs_remove_link(&dev->dev.kobj, "phy80211");
1208 			list_del_rcu(&wdev->list);
1209 			rdev->devlist_generation++;
1210 			cfg80211_mlme_purge_registrations(wdev);
1211 #ifdef CONFIG_CFG80211_WEXT
1212 			kzfree(wdev->wext.keys);
1213 #endif
1214 		}
1215 		/*
1216 		 * synchronise (so that we won't find this netdev
1217 		 * from other code any more) and then clear the list
1218 		 * head so that the above code can safely check for
1219 		 * !list_empty() to avoid double-cleanup.
1220 		 */
1221 		synchronize_rcu();
1222 		INIT_LIST_HEAD(&wdev->list);
1223 		/*
1224 		 * Ensure that all events have been processed and
1225 		 * freed.
1226 		 */
1227 		cfg80211_process_wdev_events(wdev);
1228 
1229 		if (WARN_ON(wdev->current_bss)) {
1230 			cfg80211_unhold_bss(wdev->current_bss);
1231 			cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1232 			wdev->current_bss = NULL;
1233 		}
1234 		break;
1235 	case NETDEV_PRE_UP:
1236 		if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
1237 			return notifier_from_errno(-EOPNOTSUPP);
1238 		if (rfkill_blocked(rdev->rfkill))
1239 			return notifier_from_errno(-ERFKILL);
1240 		break;
1241 	default:
1242 		return NOTIFY_DONE;
1243 	}
1244 
1245 	wireless_nlevent_flush();
1246 
1247 	return NOTIFY_OK;
1248 }
1249 
1250 static struct notifier_block cfg80211_netdev_notifier = {
1251 	.notifier_call = cfg80211_netdev_notifier_call,
1252 };
1253 
cfg80211_pernet_exit(struct net * net)1254 static void __net_exit cfg80211_pernet_exit(struct net *net)
1255 {
1256 	struct cfg80211_registered_device *rdev;
1257 
1258 	rtnl_lock();
1259 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1260 		if (net_eq(wiphy_net(&rdev->wiphy), net))
1261 			WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1262 	}
1263 	rtnl_unlock();
1264 }
1265 
1266 static struct pernet_operations cfg80211_pernet_ops = {
1267 	.exit = cfg80211_pernet_exit,
1268 };
1269 
cfg80211_init(void)1270 static int __init cfg80211_init(void)
1271 {
1272 	int err;
1273 
1274 	err = register_pernet_device(&cfg80211_pernet_ops);
1275 	if (err)
1276 		goto out_fail_pernet;
1277 
1278 	err = wiphy_sysfs_init();
1279 	if (err)
1280 		goto out_fail_sysfs;
1281 
1282 	err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1283 	if (err)
1284 		goto out_fail_notifier;
1285 
1286 	err = nl80211_init();
1287 	if (err)
1288 		goto out_fail_nl80211;
1289 
1290 	ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1291 
1292 	err = regulatory_init();
1293 	if (err)
1294 		goto out_fail_reg;
1295 
1296 	cfg80211_wq = alloc_ordered_workqueue("cfg80211", WQ_MEM_RECLAIM);
1297 	if (!cfg80211_wq) {
1298 		err = -ENOMEM;
1299 		goto out_fail_wq;
1300 	}
1301 
1302 	return 0;
1303 
1304 out_fail_wq:
1305 	regulatory_exit();
1306 out_fail_reg:
1307 	debugfs_remove(ieee80211_debugfs_dir);
1308 	nl80211_exit();
1309 out_fail_nl80211:
1310 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1311 out_fail_notifier:
1312 	wiphy_sysfs_exit();
1313 out_fail_sysfs:
1314 	unregister_pernet_device(&cfg80211_pernet_ops);
1315 out_fail_pernet:
1316 	return err;
1317 }
1318 subsys_initcall(cfg80211_init);
1319 
cfg80211_exit(void)1320 static void __exit cfg80211_exit(void)
1321 {
1322 	debugfs_remove(ieee80211_debugfs_dir);
1323 	nl80211_exit();
1324 	unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1325 	wiphy_sysfs_exit();
1326 	regulatory_exit();
1327 	unregister_pernet_device(&cfg80211_pernet_ops);
1328 	destroy_workqueue(cfg80211_wq);
1329 }
1330 module_exit(cfg80211_exit);
1331