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