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