cfg80211: introduce cfg80211_get_chan_state
[deliverable/linux.git] / net / wireless / core.c
CommitLineData
704232c2
JB
1/*
2 * This is the linux wireless configuration interface.
3 *
5f2aa25e 4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
704232c2
JB
5 */
6
e9c0268f
JP
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
704232c2
JB
9#include <linux/if.h>
10#include <linux/module.h>
11#include <linux/err.h>
704232c2 12#include <linux/list.h>
5a0e3ad6 13#include <linux/slab.h>
704232c2
JB
14#include <linux/nl80211.h>
15#include <linux/debugfs.h>
16#include <linux/notifier.h>
17#include <linux/device.h>
16a832e7 18#include <linux/etherdevice.h>
1f87f7d3 19#include <linux/rtnetlink.h>
d43c36dc 20#include <linux/sched.h>
704232c2
JB
21#include <net/genetlink.h>
22#include <net/cfg80211.h>
55682965 23#include "nl80211.h"
704232c2
JB
24#include "core.h"
25#include "sysfs.h"
1ac61302 26#include "debugfs.h"
a9a11622 27#include "wext-compat.h"
4890e3be 28#include "ethtool.h"
704232c2
JB
29
30/* name for sysfs, %d is appended */
31#define PHY_NAME "phy"
32
33MODULE_AUTHOR("Johannes Berg");
34MODULE_LICENSE("GPL");
35MODULE_DESCRIPTION("wireless configuration support");
36
5f2aa25e 37/* RCU-protected (and cfg80211_mutex for writers) */
79c97e97 38LIST_HEAD(cfg80211_rdev_list);
f5ea9120 39int cfg80211_rdev_list_generation;
a1794390 40
a1794390 41DEFINE_MUTEX(cfg80211_mutex);
704232c2
JB
42
43/* for debugfs */
44static struct dentry *ieee80211_debugfs_dir;
45
e60d7443
AB
46/* for the cleanup, scan and event works */
47struct workqueue_struct *cfg80211_wq;
48
40db6c77
AK
49static bool cfg80211_disable_40mhz_24ghz;
50module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
51MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
52 "Disable 40MHz support in the 2.4GHz band");
53
806a9e39 54/* requires cfg80211_mutex to be held! */
79c97e97 55struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
55682965 56{
79c97e97 57 struct cfg80211_registered_device *result = NULL, *rdev;
55682965 58
85fd129a
LR
59 if (!wiphy_idx_valid(wiphy_idx))
60 return NULL;
61
761cf7ec
LR
62 assert_cfg80211_lock();
63
79c97e97
JB
64 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
65 if (rdev->wiphy_idx == wiphy_idx) {
66 result = rdev;
55682965
JB
67 break;
68 }
69 }
70
71 return result;
72}
73
806a9e39
LR
74int get_wiphy_idx(struct wiphy *wiphy)
75{
79c97e97 76 struct cfg80211_registered_device *rdev;
806a9e39
LR
77 if (!wiphy)
78 return WIPHY_IDX_STALE;
79c97e97
JB
79 rdev = wiphy_to_dev(wiphy);
80 return rdev->wiphy_idx;
806a9e39
LR
81}
82
79c97e97 83/* requires cfg80211_rdev_mutex to be held! */
806a9e39
LR
84struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
85{
79c97e97 86 struct cfg80211_registered_device *rdev;
806a9e39
LR
87
88 if (!wiphy_idx_valid(wiphy_idx))
89 return NULL;
90
91 assert_cfg80211_lock();
92
79c97e97
JB
93 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
94 if (!rdev)
806a9e39 95 return NULL;
79c97e97 96 return &rdev->wiphy;
806a9e39
LR
97}
98
55682965 99struct cfg80211_registered_device *
463d0183 100cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
55682965 101{
79c97e97 102 struct cfg80211_registered_device *rdev = ERR_PTR(-ENODEV);
55682965
JB
103 struct net_device *dev;
104
a1794390 105 mutex_lock(&cfg80211_mutex);
463d0183 106 dev = dev_get_by_index(net, ifindex);
55682965
JB
107 if (!dev)
108 goto out;
109 if (dev->ieee80211_ptr) {
79c97e97
JB
110 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
111 mutex_lock(&rdev->mtx);
55682965 112 } else
79c97e97 113 rdev = ERR_PTR(-ENODEV);
55682965
JB
114 dev_put(dev);
115 out:
a1794390 116 mutex_unlock(&cfg80211_mutex);
79c97e97 117 return rdev;
55682965
JB
118}
119
4bbf4d56 120/* requires cfg80211_mutex to be held */
55682965
JB
121int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
122 char *newname)
123{
79c97e97 124 struct cfg80211_registered_device *rdev2;
7623225f 125 int wiphy_idx, taken = -1, result, digits;
55682965 126
4bbf4d56 127 assert_cfg80211_lock();
2940bb69 128
7623225f
JB
129 /* prohibit calling the thing phy%d when %d is not its number */
130 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
131 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
132 /* count number of places needed to print wiphy_idx */
133 digits = 1;
134 while (wiphy_idx /= 10)
135 digits++;
136 /*
137 * deny the name if it is phy<idx> where <idx> is printed
138 * without leading zeroes. taken == strlen(newname) here
139 */
140 if (taken == strlen(PHY_NAME) + digits)
141 return -EINVAL;
142 }
143
144
2940bb69 145 /* Ignore nop renames */
2940bb69 146 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
4bbf4d56 147 return 0;
2940bb69
EB
148
149 /* Ensure another device does not already have this name. */
79c97e97
JB
150 list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
151 if (strcmp(newname, dev_name(&rdev2->wiphy.dev)) == 0)
7623225f 152 return -EINVAL;
55682965 153
55682965
JB
154 result = device_rename(&rdev->wiphy.dev, newname);
155 if (result)
4bbf4d56 156 return result;
55682965 157
33c0360b
JB
158 if (rdev->wiphy.debugfsdir &&
159 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
55682965
JB
160 rdev->wiphy.debugfsdir,
161 rdev->wiphy.debugfsdir->d_parent,
162 newname))
e9c0268f 163 pr_err("failed to rename debugfs dir to %s!\n", newname);
55682965 164
4bbf4d56 165 nl80211_notify_dev_rename(rdev);
55682965 166
4bbf4d56 167 return 0;
55682965
JB
168}
169
463d0183
JB
170int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
171 struct net *net)
172{
173 struct wireless_dev *wdev;
174 int err = 0;
175
5be83de5 176 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
463d0183
JB
177 return -EOPNOTSUPP;
178
179 list_for_each_entry(wdev, &rdev->netdev_list, list) {
180 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
181 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
182 if (err)
183 break;
184 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
185 }
186
187 if (err) {
188 /* failed -- clean up to old netns */
189 net = wiphy_net(&rdev->wiphy);
190
191 list_for_each_entry_continue_reverse(wdev, &rdev->netdev_list,
192 list) {
193 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
194 err = dev_change_net_namespace(wdev->netdev, net,
195 "wlan%d");
196 WARN_ON(err);
197 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
198 }
04600794
JB
199
200 return err;
463d0183
JB
201 }
202
203 wiphy_net_set(&rdev->wiphy, net);
204
04600794
JB
205 err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
206 WARN_ON(err);
207
208 return 0;
463d0183
JB
209}
210
1f87f7d3
JB
211static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
212{
79c97e97 213 struct cfg80211_registered_device *rdev = data;
1f87f7d3 214
79c97e97 215 rdev->ops->rfkill_poll(&rdev->wiphy);
1f87f7d3
JB
216}
217
218static int cfg80211_rfkill_set_block(void *data, bool blocked)
219{
79c97e97 220 struct cfg80211_registered_device *rdev = data;
1f87f7d3
JB
221 struct wireless_dev *wdev;
222
223 if (!blocked)
224 return 0;
225
226 rtnl_lock();
79c97e97 227 mutex_lock(&rdev->devlist_mtx);
1f87f7d3 228
79c97e97 229 list_for_each_entry(wdev, &rdev->netdev_list, list)
1f87f7d3
JB
230 dev_close(wdev->netdev);
231
79c97e97 232 mutex_unlock(&rdev->devlist_mtx);
1f87f7d3
JB
233 rtnl_unlock();
234
235 return 0;
236}
237
238static void cfg80211_rfkill_sync_work(struct work_struct *work)
239{
79c97e97 240 struct cfg80211_registered_device *rdev;
1f87f7d3 241
79c97e97
JB
242 rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
243 cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
1f87f7d3
JB
244}
245
667503dd
JB
246static void cfg80211_event_work(struct work_struct *work)
247{
248 struct cfg80211_registered_device *rdev;
667503dd
JB
249
250 rdev = container_of(work, struct cfg80211_registered_device,
251 event_work);
252
253 rtnl_lock();
254 cfg80211_lock_rdev(rdev);
667503dd 255
3d54d255 256 cfg80211_process_rdev_events(rdev);
667503dd
JB
257 cfg80211_unlock_rdev(rdev);
258 rtnl_unlock();
259}
260
704232c2
JB
261/* exported functions */
262
3dcf670b 263struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
704232c2 264{
638af073 265 static int wiphy_counter;
7623225f
JB
266
267 struct cfg80211_registered_device *rdev;
704232c2
JB
268 int alloc_size;
269
0b20633d
JB
270 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
271 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
272 WARN_ON(ops->connect && !ops->disconnect);
273 WARN_ON(ops->join_ibss && !ops->leave_ibss);
274 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
275 WARN_ON(ops->add_station && !ops->del_station);
276 WARN_ON(ops->add_mpath && !ops->del_mpath);
29cbe68c 277 WARN_ON(ops->join_mesh && !ops->leave_mesh);
41ade00f 278
79c97e97 279 alloc_size = sizeof(*rdev) + sizeof_priv;
704232c2 280
79c97e97
JB
281 rdev = kzalloc(alloc_size, GFP_KERNEL);
282 if (!rdev)
704232c2
JB
283 return NULL;
284
79c97e97 285 rdev->ops = ops;
704232c2 286
a1794390 287 mutex_lock(&cfg80211_mutex);
704232c2 288
79c97e97 289 rdev->wiphy_idx = wiphy_counter++;
a4d73ee1 290
79c97e97 291 if (unlikely(!wiphy_idx_valid(rdev->wiphy_idx))) {
638af073 292 wiphy_counter--;
a1794390 293 mutex_unlock(&cfg80211_mutex);
7623225f 294 /* ugh, wrapped! */
79c97e97 295 kfree(rdev);
704232c2
JB
296 return NULL;
297 }
704232c2 298
5a254ffe 299 mutex_unlock(&cfg80211_mutex);
79c97e97 300
7623225f
JB
301 /* give it a proper name */
302 dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
303
79c97e97
JB
304 mutex_init(&rdev->mtx);
305 mutex_init(&rdev->devlist_mtx);
c10841ca 306 mutex_init(&rdev->sched_scan_mtx);
79c97e97
JB
307 INIT_LIST_HEAD(&rdev->netdev_list);
308 spin_lock_init(&rdev->bss_lock);
309 INIT_LIST_HEAD(&rdev->bss_list);
310 INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
807f8a8c 311 INIT_WORK(&rdev->sched_scan_results_wk, __cfg80211_sched_scan_results);
3d23e349
JB
312#ifdef CONFIG_CFG80211_WEXT
313 rdev->wiphy.wext = &cfg80211_wext_handler;
314#endif
315
79c97e97
JB
316 device_initialize(&rdev->wiphy.dev);
317 rdev->wiphy.dev.class = &ieee80211_class;
318 rdev->wiphy.dev.platform_data = rdev;
319
5be83de5
JB
320#ifdef CONFIG_CFG80211_DEFAULT_PS
321 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
322#endif
16cb9d42 323
463d0183
JB
324 wiphy_net_set(&rdev->wiphy, &init_net);
325
79c97e97
JB
326 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
327 rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
328 &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
329 &rdev->rfkill_ops, rdev);
330
331 if (!rdev->rfkill) {
332 kfree(rdev);
1f87f7d3
JB
333 return NULL;
334 }
335
79c97e97
JB
336 INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
337 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
338 INIT_WORK(&rdev->event_work, cfg80211_event_work);
1f87f7d3 339
ad002395
JB
340 init_waitqueue_head(&rdev->dev_wait);
341
b9a5f8ca
JM
342 /*
343 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
344 * Fragmentation and RTS threshold are disabled by default with the
345 * special -1 value.
346 */
79c97e97
JB
347 rdev->wiphy.retry_short = 7;
348 rdev->wiphy.retry_long = 4;
349 rdev->wiphy.frag_threshold = (u32) -1;
350 rdev->wiphy.rts_threshold = (u32) -1;
81077e82 351 rdev->wiphy.coverage_class = 0;
b9a5f8ca 352
79c97e97 353 return &rdev->wiphy;
704232c2
JB
354}
355EXPORT_SYMBOL(wiphy_new);
356
7527a782
JB
357static int wiphy_verify_combinations(struct wiphy *wiphy)
358{
359 const struct ieee80211_iface_combination *c;
360 int i, j;
361
7527a782
JB
362 for (i = 0; i < wiphy->n_iface_combinations; i++) {
363 u32 cnt = 0;
364 u16 all_iftypes = 0;
365
366 c = &wiphy->iface_combinations[i];
367
368 /* Combinations with just one interface aren't real */
369 if (WARN_ON(c->max_interfaces < 2))
370 return -EINVAL;
371
372 /* Need at least one channel */
373 if (WARN_ON(!c->num_different_channels))
374 return -EINVAL;
375
376 if (WARN_ON(!c->n_limits))
377 return -EINVAL;
378
379 for (j = 0; j < c->n_limits; j++) {
380 u16 types = c->limits[j].types;
381
382 /*
383 * interface types shouldn't overlap, this is
384 * used in cfg80211_can_change_interface()
385 */
386 if (WARN_ON(types & all_iftypes))
387 return -EINVAL;
388 all_iftypes |= types;
389
390 if (WARN_ON(!c->limits[j].max))
391 return -EINVAL;
392
393 /* Shouldn't list software iftypes in combinations! */
394 if (WARN_ON(wiphy->software_iftypes & types))
395 return -EINVAL;
396
397 cnt += c->limits[j].max;
398 /*
399 * Don't advertise an unsupported type
400 * in a combination.
401 */
402 if (WARN_ON((wiphy->interface_modes & types) != types))
403 return -EINVAL;
404 }
405
406 /* You can't even choose that many! */
407 if (WARN_ON(cnt < c->max_interfaces))
408 return -EINVAL;
409 }
410
411 return 0;
412}
413
704232c2
JB
414int wiphy_register(struct wiphy *wiphy)
415{
79c97e97 416 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
704232c2 417 int res;
8318d78a
JB
418 enum ieee80211_band band;
419 struct ieee80211_supported_band *sband;
420 bool have_band = false;
421 int i;
f59ac048
LR
422 u16 ifmodes = wiphy->interface_modes;
423
dfb89c56 424#ifdef CONFIG_PM
77dbbb13
JB
425 if (WARN_ON((wiphy->wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
426 !(wiphy->wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
427 return -EINVAL;
dfb89c56 428#endif
77dbbb13 429
562a7480
JB
430 if (WARN_ON(wiphy->ap_sme_capa &&
431 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
432 return -EINVAL;
433
ef15aac6
JB
434 if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
435 return -EINVAL;
436
437 if (WARN_ON(wiphy->addresses &&
438 !is_zero_ether_addr(wiphy->perm_addr) &&
439 memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
440 ETH_ALEN)))
441 return -EINVAL;
442
443 if (wiphy->addresses)
444 memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
445
f59ac048
LR
446 /* sanity check ifmodes */
447 WARN_ON(!ifmodes);
2e161f78 448 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
f59ac048
LR
449 if (WARN_ON(ifmodes != wiphy->interface_modes))
450 wiphy->interface_modes = ifmodes;
8318d78a 451
7527a782
JB
452 res = wiphy_verify_combinations(wiphy);
453 if (res)
454 return res;
455
8318d78a
JB
456 /* sanity check supported bands/channels */
457 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
458 sband = wiphy->bands[band];
459 if (!sband)
460 continue;
461
462 sband->band = band;
463
881d948c
JB
464 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
465 return -EINVAL;
466
40db6c77
AK
467 /*
468 * Since cfg80211_disable_40mhz_24ghz is global, we can
469 * modify the sband's ht data even if the driver uses a
470 * global structure for that.
471 */
472 if (cfg80211_disable_40mhz_24ghz &&
473 band == IEEE80211_BAND_2GHZ &&
474 sband->ht_cap.ht_supported) {
475 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
476 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
477 }
478
881d948c
JB
479 /*
480 * Since we use a u32 for rate bitmaps in
481 * ieee80211_get_response_rate, we cannot
482 * have more than 32 legacy rates.
483 */
484 if (WARN_ON(sband->n_bitrates > 32))
8318d78a 485 return -EINVAL;
8318d78a
JB
486
487 for (i = 0; i < sband->n_channels; i++) {
488 sband->channels[i].orig_flags =
489 sband->channels[i].flags;
490 sband->channels[i].orig_mag =
491 sband->channels[i].max_antenna_gain;
492 sband->channels[i].orig_mpwr =
493 sband->channels[i].max_power;
494 sband->channels[i].band = band;
495 }
496
497 have_band = true;
498 }
499
500 if (!have_band) {
501 WARN_ON(1);
502 return -EINVAL;
503 }
504
dfb89c56 505#ifdef CONFIG_PM
ff1b6e69
JB
506 if (rdev->wiphy.wowlan.n_patterns) {
507 if (WARN_ON(!rdev->wiphy.wowlan.pattern_min_len ||
508 rdev->wiphy.wowlan.pattern_min_len >
509 rdev->wiphy.wowlan.pattern_max_len))
510 return -EINVAL;
511 }
dfb89c56 512#endif
ff1b6e69 513
8318d78a
JB
514 /* check and set up bitrates */
515 ieee80211_set_bitrate_flags(wiphy);
516
5a652052
MB
517 mutex_lock(&cfg80211_mutex);
518
79c97e97 519 res = device_add(&rdev->wiphy.dev);
c3d34d5d
JL
520 if (res) {
521 mutex_unlock(&cfg80211_mutex);
522 return res;
523 }
1f87f7d3 524
2f0accc1 525 /* set up regulatory info */
eac03e38 526 regulatory_update(wiphy, NL80211_REGDOM_SET_BY_CORE);
2f0accc1 527
5f2aa25e 528 list_add_rcu(&rdev->list, &cfg80211_rdev_list);
f5ea9120 529 cfg80211_rdev_list_generation++;
704232c2
JB
530
531 /* add to debugfs */
79c97e97
JB
532 rdev->wiphy.debugfsdir =
533 debugfs_create_dir(wiphy_name(&rdev->wiphy),
704232c2 534 ieee80211_debugfs_dir);
79c97e97
JB
535 if (IS_ERR(rdev->wiphy.debugfsdir))
536 rdev->wiphy.debugfsdir = NULL;
704232c2 537
5be83de5 538 if (wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
73d54c9e
LR
539 struct regulatory_request request;
540
541 request.wiphy_idx = get_wiphy_idx(wiphy);
542 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
543 request.alpha2[0] = '9';
544 request.alpha2[1] = '9';
545
546 nl80211_send_reg_change_event(&request);
547 }
548
79c97e97 549 cfg80211_debugfs_rdev_add(rdev);
5a652052 550 mutex_unlock(&cfg80211_mutex);
1ac61302 551
c3d34d5d
JL
552 /*
553 * due to a locking dependency this has to be outside of the
554 * cfg80211_mutex lock
555 */
556 res = rfkill_register(rdev->rfkill);
557 if (res)
558 goto out_rm_dev;
559
ecb44335
SG
560 rtnl_lock();
561 rdev->wiphy.registered = true;
562 rtnl_unlock();
2f0accc1 563 return 0;
1f87f7d3 564
5a652052 565out_rm_dev:
79c97e97 566 device_del(&rdev->wiphy.dev);
704232c2
JB
567 return res;
568}
569EXPORT_SYMBOL(wiphy_register);
570
1f87f7d3
JB
571void wiphy_rfkill_start_polling(struct wiphy *wiphy)
572{
79c97e97 573 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
1f87f7d3 574
79c97e97 575 if (!rdev->ops->rfkill_poll)
1f87f7d3 576 return;
79c97e97
JB
577 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
578 rfkill_resume_polling(rdev->rfkill);
1f87f7d3
JB
579}
580EXPORT_SYMBOL(wiphy_rfkill_start_polling);
581
582void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
583{
79c97e97 584 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
1f87f7d3 585
79c97e97 586 rfkill_pause_polling(rdev->rfkill);
1f87f7d3
JB
587}
588EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
589
704232c2
JB
590void wiphy_unregister(struct wiphy *wiphy)
591{
79c97e97 592 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
704232c2 593
ecb44335
SG
594 rtnl_lock();
595 rdev->wiphy.registered = false;
596 rtnl_unlock();
597
79c97e97 598 rfkill_unregister(rdev->rfkill);
1f87f7d3 599
f16bfc1c 600 /* protect the device list */
a1794390 601 mutex_lock(&cfg80211_mutex);
704232c2 602
ad002395
JB
603 wait_event(rdev->dev_wait, ({
604 int __count;
605 mutex_lock(&rdev->devlist_mtx);
606 __count = rdev->opencount;
607 mutex_unlock(&rdev->devlist_mtx);
c4f60846 608 __count == 0; }));
ad002395
JB
609
610 mutex_lock(&rdev->devlist_mtx);
79c97e97 611 BUG_ON(!list_empty(&rdev->netdev_list));
ad002395
JB
612 mutex_unlock(&rdev->devlist_mtx);
613
614 /*
615 * First remove the hardware from everywhere, this makes
616 * it impossible to find from userspace.
617 */
7bcfaf2f 618 debugfs_remove_recursive(rdev->wiphy.debugfsdir);
5f2aa25e
JB
619 list_del_rcu(&rdev->list);
620 synchronize_rcu();
f16bfc1c
JB
621
622 /*
79c97e97 623 * Try to grab rdev->mtx. If a command is still in progress,
f16bfc1c
JB
624 * hopefully the driver will refuse it since it's tearing
625 * down the device already. We wait for this command to complete
626 * before unlinking the item from the list.
627 * Note: as codified by the BUG_ON above we cannot get here if
ad002395
JB
628 * a virtual interface is still present. Hence, we can only get
629 * to lock contention here if userspace issues a command that
630 * identified the hardware by wiphy index.
f16bfc1c 631 */
0ff6ce7b 632 cfg80211_lock_rdev(rdev);
ad002395 633 /* nothing */
0ff6ce7b 634 cfg80211_unlock_rdev(rdev);
704232c2 635
3f2355cb
LR
636 /* If this device got a regulatory hint tell core its
637 * free to listen now to a new shiny device regulatory hint */
638 reg_device_remove(wiphy);
639
f5ea9120 640 cfg80211_rdev_list_generation++;
79c97e97 641 device_del(&rdev->wiphy.dev);
704232c2 642
a1794390 643 mutex_unlock(&cfg80211_mutex);
6682588a 644
36e6fea8 645 flush_work(&rdev->scan_done_wk);
6682588a 646 cancel_work_sync(&rdev->conn_work);
6682588a 647 flush_work(&rdev->event_work);
6d52563f
JB
648
649 if (rdev->wowlan && rdev->ops->set_wakeup)
650 rdev->ops->set_wakeup(&rdev->wiphy, false);
651 cfg80211_rdev_free_wowlan(rdev);
704232c2
JB
652}
653EXPORT_SYMBOL(wiphy_unregister);
654
79c97e97 655void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
704232c2 656{
2a519311 657 struct cfg80211_internal_bss *scan, *tmp;
79c97e97
JB
658 rfkill_destroy(rdev->rfkill);
659 mutex_destroy(&rdev->mtx);
660 mutex_destroy(&rdev->devlist_mtx);
c10841ca 661 mutex_destroy(&rdev->sched_scan_mtx);
79c97e97 662 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
78c1c7e1 663 cfg80211_put_bss(&scan->pub);
79c97e97 664 kfree(rdev);
704232c2
JB
665}
666
667void wiphy_free(struct wiphy *wiphy)
668{
669 put_device(&wiphy->dev);
670}
671EXPORT_SYMBOL(wiphy_free);
672
1f87f7d3
JB
673void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
674{
79c97e97 675 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
1f87f7d3 676
79c97e97
JB
677 if (rfkill_set_hw_state(rdev->rfkill, blocked))
678 schedule_work(&rdev->rfkill_sync);
1f87f7d3
JB
679}
680EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
681
ad002395
JB
682static void wdev_cleanup_work(struct work_struct *work)
683{
684 struct wireless_dev *wdev;
685 struct cfg80211_registered_device *rdev;
686
687 wdev = container_of(work, struct wireless_dev, cleanup_work);
688 rdev = wiphy_to_dev(wdev->wiphy);
689
690 cfg80211_lock_rdev(rdev);
691
692 if (WARN_ON(rdev->scan_req && rdev->scan_req->dev == wdev->netdev)) {
693 rdev->scan_req->aborted = true;
01a0ac41 694 ___cfg80211_scan_done(rdev, true);
ad002395
JB
695 }
696
c10841ca
LC
697 cfg80211_unlock_rdev(rdev);
698
699 mutex_lock(&rdev->sched_scan_mtx);
700
807f8a8c
LC
701 if (WARN_ON(rdev->sched_scan_req &&
702 rdev->sched_scan_req->dev == wdev->netdev)) {
703 __cfg80211_stop_sched_scan(rdev, false);
704 }
705
c10841ca 706 mutex_unlock(&rdev->sched_scan_mtx);
ad002395
JB
707
708 mutex_lock(&rdev->devlist_mtx);
709 rdev->opencount--;
710 mutex_unlock(&rdev->devlist_mtx);
711 wake_up(&rdev->dev_wait);
712
713 dev_put(wdev->netdev);
714}
715
053a93dd
MH
716static struct device_type wiphy_type = {
717 .name = "wlan",
718};
719
c4f60846 720static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
704232c2
JB
721 unsigned long state,
722 void *ndev)
723{
724 struct net_device *dev = ndev;
2a783c13 725 struct wireless_dev *wdev = dev->ieee80211_ptr;
704232c2 726 struct cfg80211_registered_device *rdev;
7527a782 727 int ret;
704232c2 728
2a783c13 729 if (!wdev)
1f87f7d3 730 return NOTIFY_DONE;
704232c2 731
2a783c13 732 rdev = wiphy_to_dev(wdev->wiphy);
704232c2 733
2a783c13 734 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
60719ffd 735
704232c2 736 switch (state) {
053a93dd
MH
737 case NETDEV_POST_INIT:
738 SET_NETDEV_DEVTYPE(dev, &wiphy_type);
739 break;
704232c2 740 case NETDEV_REGISTER:
0ff6ce7b
JB
741 /*
742 * NB: cannot take rdev->mtx here because this may be
743 * called within code protected by it when interfaces
744 * are added with nl80211.
745 */
667503dd 746 mutex_init(&wdev->mtx);
ad002395 747 INIT_WORK(&wdev->cleanup_work, wdev_cleanup_work);
667503dd
JB
748 INIT_LIST_HEAD(&wdev->event_list);
749 spin_lock_init(&wdev->event_lock);
2e161f78
JB
750 INIT_LIST_HEAD(&wdev->mgmt_registrations);
751 spin_lock_init(&wdev->mgmt_registrations_lock);
026331c4 752
704232c2 753 mutex_lock(&rdev->devlist_mtx);
5f2aa25e 754 list_add_rcu(&wdev->list, &rdev->netdev_list);
f5ea9120 755 rdev->devlist_generation++;
463d0183
JB
756 /* can only change netns with wiphy */
757 dev->features |= NETIF_F_NETNS_LOCAL;
758
704232c2
JB
759 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
760 "phy80211")) {
e9c0268f 761 pr_err("failed to add phy80211 symlink to netdev!\n");
704232c2 762 }
2a783c13 763 wdev->netdev = dev;
b23aa676 764 wdev->sme_state = CFG80211_SME_IDLE;
bc92afd9 765 mutex_unlock(&rdev->devlist_mtx);
3d23e349 766#ifdef CONFIG_CFG80211_WEXT
2a783c13
JB
767 wdev->wext.default_key = -1;
768 wdev->wext.default_mgmt_key = -1;
f2129354 769 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
ffb9eb3d
KV
770#endif
771
5be83de5 772 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
ffb9eb3d 773 wdev->ps = true;
5be83de5 774 else
ffb9eb3d 775 wdev->ps = false;
9043f3b8
JO
776 /* allow mac80211 to determine the timeout */
777 wdev->ps_timeout = -1;
ffb9eb3d 778
4890e3be
JL
779 if (!dev->ethtool_ops)
780 dev->ethtool_ops = &cfg80211_ethtool_ops;
ad4bb6f8
JB
781
782 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
074ac8df 783 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
ad4bb6f8
JB
784 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
785 dev->priv_flags |= IFF_DONT_BRIDGE;
704232c2 786 break;
04a773ad 787 case NETDEV_GOING_DOWN:
b23aa676
SO
788 switch (wdev->iftype) {
789 case NL80211_IFTYPE_ADHOC:
790 cfg80211_leave_ibss(rdev, dev, true);
791 break;
074ac8df 792 case NL80211_IFTYPE_P2P_CLIENT:
b23aa676 793 case NL80211_IFTYPE_STATION:
c10841ca 794 mutex_lock(&rdev->sched_scan_mtx);
807f8a8c 795 __cfg80211_stop_sched_scan(rdev, false);
c10841ca 796 mutex_unlock(&rdev->sched_scan_mtx);
807f8a8c 797
667503dd 798 wdev_lock(wdev);
3d23e349 799#ifdef CONFIG_CFG80211_WEXT
f2129354
JB
800 kfree(wdev->wext.ie);
801 wdev->wext.ie = NULL;
802 wdev->wext.ie_len = 0;
0eb14647 803 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
f2129354 804#endif
667503dd
JB
805 __cfg80211_disconnect(rdev, dev,
806 WLAN_REASON_DEAUTH_LEAVING, true);
19957bb3 807 cfg80211_mlme_down(rdev, dev);
667503dd 808 wdev_unlock(wdev);
b23aa676 809 break;
29cbe68c
JB
810 case NL80211_IFTYPE_MESH_POINT:
811 cfg80211_leave_mesh(rdev, dev);
812 break;
ac800140
MK
813 case NL80211_IFTYPE_AP:
814 cfg80211_stop_ap(rdev, dev);
815 break;
b23aa676
SO
816 default:
817 break;
818 }
56d1893d 819 wdev->beacon_interval = 0;
01a0ac41
JB
820 break;
821 case NETDEV_DOWN:
ad002395 822 dev_hold(dev);
e60d7443 823 queue_work(cfg80211_wq, &wdev->cleanup_work);
04a773ad
JB
824 break;
825 case NETDEV_UP:
ad002395
JB
826 /*
827 * If we have a really quick DOWN/UP succession we may
828 * have this work still pending ... cancel it and see
829 * if it was pending, in which case we need to account
830 * for some of the work it would have done.
831 */
832 if (cancel_work_sync(&wdev->cleanup_work)) {
833 mutex_lock(&rdev->devlist_mtx);
834 rdev->opencount--;
835 mutex_unlock(&rdev->devlist_mtx);
836 dev_put(dev);
837 }
667503dd 838 cfg80211_lock_rdev(rdev);
aee83eaf 839 mutex_lock(&rdev->devlist_mtx);
667503dd 840 wdev_lock(wdev);
f2129354 841 switch (wdev->iftype) {
29cbe68c 842#ifdef CONFIG_CFG80211_WEXT
f2129354 843 case NL80211_IFTYPE_ADHOC:
fffd0934 844 cfg80211_ibss_wext_join(rdev, wdev);
04a773ad 845 break;
f2129354 846 case NL80211_IFTYPE_STATION:
fffd0934 847 cfg80211_mgd_wext_connect(rdev, wdev);
f2129354 848 break;
29cbe68c 849#endif
c80d545d 850#ifdef CONFIG_MAC80211_MESH
29cbe68c 851 case NL80211_IFTYPE_MESH_POINT:
c80d545d
JC
852 {
853 /* backward compat code... */
854 struct mesh_setup setup;
855 memcpy(&setup, &default_mesh_setup,
856 sizeof(setup));
857 /* back compat only needed for mesh_id */
858 setup.mesh_id = wdev->ssid;
859 setup.mesh_id_len = wdev->mesh_id_up_len;
860 if (wdev->mesh_id_up_len)
861 __cfg80211_join_mesh(rdev, dev,
862 &setup,
863 &default_mesh_config);
864 break;
865 }
866#endif
f2129354 867 default:
04a773ad 868 break;
f2129354 869 }
667503dd 870 wdev_unlock(wdev);
ad002395 871 rdev->opencount++;
aee83eaf 872 mutex_unlock(&rdev->devlist_mtx);
667503dd 873 cfg80211_unlock_rdev(rdev);
bf6a0579
JO
874
875 /*
876 * Configure power management to the driver here so that its
877 * correctly set also after interface type changes etc.
878 */
5966f2dd
EP
879 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
880 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
bf6a0579
JO
881 rdev->ops->set_power_mgmt)
882 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev,
883 wdev->ps,
884 wdev->ps_timeout)) {
885 /* assume this means it's off */
886 wdev->ps = false;
887 }
2a783c13 888 break;
704232c2 889 case NETDEV_UNREGISTER:
0ff6ce7b
JB
890 /*
891 * NB: cannot take rdev->mtx here because this may be
892 * called within code protected by it when interfaces
893 * are removed with nl80211.
894 */
704232c2 895 mutex_lock(&rdev->devlist_mtx);
e40cbdac
JB
896 /*
897 * It is possible to get NETDEV_UNREGISTER
898 * multiple times. To detect that, check
899 * that the interface is still on the list
900 * of registered interfaces, and only then
901 * remove and clean it up.
902 */
2a783c13 903 if (!list_empty(&wdev->list)) {
704232c2 904 sysfs_remove_link(&dev->dev.kobj, "phy80211");
5f2aa25e 905 list_del_rcu(&wdev->list);
f5ea9120 906 rdev->devlist_generation++;
2e161f78 907 cfg80211_mlme_purge_registrations(wdev);
3d23e349 908#ifdef CONFIG_CFG80211_WEXT
e40cbdac 909 kfree(wdev->wext.keys);
fffd0934 910#endif
e40cbdac
JB
911 }
912 mutex_unlock(&rdev->devlist_mtx);
5f2aa25e
JB
913 /*
914 * synchronise (so that we won't find this netdev
915 * from other code any more) and then clear the list
916 * head so that the above code can safely check for
917 * !list_empty() to avoid double-cleanup.
918 */
919 synchronize_rcu();
920 INIT_LIST_HEAD(&wdev->list);
704232c2 921 break;
1f87f7d3 922 case NETDEV_PRE_UP:
0b20633d
JB
923 if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
924 return notifier_from_errno(-EOPNOTSUPP);
1f87f7d3
JB
925 if (rfkill_blocked(rdev->rfkill))
926 return notifier_from_errno(-ERFKILL);
7527a782
JB
927 ret = cfg80211_can_add_interface(rdev, wdev->iftype);
928 if (ret)
929 return notifier_from_errno(ret);
1f87f7d3 930 break;
704232c2
JB
931 }
932
1f87f7d3 933 return NOTIFY_DONE;
704232c2
JB
934}
935
936static struct notifier_block cfg80211_netdev_notifier = {
937 .notifier_call = cfg80211_netdev_notifier_call,
938};
939
463d0183
JB
940static void __net_exit cfg80211_pernet_exit(struct net *net)
941{
942 struct cfg80211_registered_device *rdev;
943
944 rtnl_lock();
945 mutex_lock(&cfg80211_mutex);
946 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
947 if (net_eq(wiphy_net(&rdev->wiphy), net))
948 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
949 }
950 mutex_unlock(&cfg80211_mutex);
951 rtnl_unlock();
952}
953
954static struct pernet_operations cfg80211_pernet_ops = {
955 .exit = cfg80211_pernet_exit,
956};
957
958static int __init cfg80211_init(void)
704232c2 959{
b2e1b302
LR
960 int err;
961
463d0183
JB
962 err = register_pernet_device(&cfg80211_pernet_ops);
963 if (err)
964 goto out_fail_pernet;
965
b2e1b302 966 err = wiphy_sysfs_init();
704232c2
JB
967 if (err)
968 goto out_fail_sysfs;
969
970 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
971 if (err)
972 goto out_fail_notifier;
973
55682965
JB
974 err = nl80211_init();
975 if (err)
976 goto out_fail_nl80211;
977
704232c2
JB
978 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
979
b2e1b302
LR
980 err = regulatory_init();
981 if (err)
982 goto out_fail_reg;
983
e60d7443
AB
984 cfg80211_wq = create_singlethread_workqueue("cfg80211");
985 if (!cfg80211_wq)
986 goto out_fail_wq;
987
704232c2
JB
988 return 0;
989
e60d7443
AB
990out_fail_wq:
991 regulatory_exit();
b2e1b302
LR
992out_fail_reg:
993 debugfs_remove(ieee80211_debugfs_dir);
55682965
JB
994out_fail_nl80211:
995 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
704232c2
JB
996out_fail_notifier:
997 wiphy_sysfs_exit();
998out_fail_sysfs:
463d0183
JB
999 unregister_pernet_device(&cfg80211_pernet_ops);
1000out_fail_pernet:
704232c2
JB
1001 return err;
1002}
3a462465 1003subsys_initcall(cfg80211_init);
704232c2 1004
f884e387 1005static void __exit cfg80211_exit(void)
704232c2
JB
1006{
1007 debugfs_remove(ieee80211_debugfs_dir);
55682965 1008 nl80211_exit();
704232c2
JB
1009 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1010 wiphy_sysfs_exit();
b2e1b302 1011 regulatory_exit();
463d0183 1012 unregister_pernet_device(&cfg80211_pernet_ops);
e60d7443 1013 destroy_workqueue(cfg80211_wq);
704232c2
JB
1014}
1015module_exit(cfg80211_exit);
This page took 0.58092 seconds and 5 git commands to generate.