headers: remove sched.h from interrupt.h
[deliverable/linux.git] / net / wireless / core.c
CommitLineData
704232c2
JB
1/*
2 * This is the linux wireless configuration interface.
3 *
08645126 4 * Copyright 2006-2009 Johannes Berg <johannes@sipsolutions.net>
704232c2
JB
5 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
704232c2
JB
10#include <linux/list.h>
11#include <linux/nl80211.h>
12#include <linux/debugfs.h>
13#include <linux/notifier.h>
14#include <linux/device.h>
16a832e7 15#include <linux/etherdevice.h>
1f87f7d3 16#include <linux/rtnetlink.h>
d43c36dc 17#include <linux/sched.h>
704232c2
JB
18#include <net/genetlink.h>
19#include <net/cfg80211.h>
55682965 20#include "nl80211.h"
704232c2
JB
21#include "core.h"
22#include "sysfs.h"
1ac61302 23#include "debugfs.h"
a9a11622 24#include "wext-compat.h"
704232c2
JB
25
26/* name for sysfs, %d is appended */
27#define PHY_NAME "phy"
28
29MODULE_AUTHOR("Johannes Berg");
30MODULE_LICENSE("GPL");
31MODULE_DESCRIPTION("wireless configuration support");
32
33/* RCU might be appropriate here since we usually
34 * only read the list, and that can happen quite
35 * often because we need to do it for each command */
79c97e97 36LIST_HEAD(cfg80211_rdev_list);
f5ea9120 37int cfg80211_rdev_list_generation;
a1794390
LR
38
39/*
abc7381b 40 * This is used to protect the cfg80211_rdev_list
a1794390
LR
41 */
42DEFINE_MUTEX(cfg80211_mutex);
704232c2
JB
43
44/* for debugfs */
45static struct dentry *ieee80211_debugfs_dir;
46
806a9e39 47/* requires cfg80211_mutex to be held! */
79c97e97 48struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
55682965 49{
79c97e97 50 struct cfg80211_registered_device *result = NULL, *rdev;
55682965 51
85fd129a
LR
52 if (!wiphy_idx_valid(wiphy_idx))
53 return NULL;
54
761cf7ec
LR
55 assert_cfg80211_lock();
56
79c97e97
JB
57 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
58 if (rdev->wiphy_idx == wiphy_idx) {
59 result = rdev;
55682965
JB
60 break;
61 }
62 }
63
64 return result;
65}
66
806a9e39
LR
67int get_wiphy_idx(struct wiphy *wiphy)
68{
79c97e97 69 struct cfg80211_registered_device *rdev;
806a9e39
LR
70 if (!wiphy)
71 return WIPHY_IDX_STALE;
79c97e97
JB
72 rdev = wiphy_to_dev(wiphy);
73 return rdev->wiphy_idx;
806a9e39
LR
74}
75
79c97e97 76/* requires cfg80211_rdev_mutex to be held! */
806a9e39
LR
77struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
78{
79c97e97 79 struct cfg80211_registered_device *rdev;
806a9e39
LR
80
81 if (!wiphy_idx_valid(wiphy_idx))
82 return NULL;
83
84 assert_cfg80211_lock();
85
79c97e97
JB
86 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
87 if (!rdev)
806a9e39 88 return NULL;
79c97e97 89 return &rdev->wiphy;
806a9e39
LR
90}
91
a1794390 92/* requires cfg80211_mutex to be held! */
4bbf4d56 93struct cfg80211_registered_device *
79c97e97 94__cfg80211_rdev_from_info(struct genl_info *info)
55682965
JB
95{
96 int ifindex;
b5850a7a 97 struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
55682965
JB
98 struct net_device *dev;
99 int err = -EINVAL;
100
761cf7ec
LR
101 assert_cfg80211_lock();
102
55682965 103 if (info->attrs[NL80211_ATTR_WIPHY]) {
79c97e97 104 bywiphyidx = cfg80211_rdev_by_wiphy_idx(
55682965
JB
105 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
106 err = -ENODEV;
107 }
108
109 if (info->attrs[NL80211_ATTR_IFINDEX]) {
110 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
463d0183 111 dev = dev_get_by_index(genl_info_net(info), ifindex);
55682965
JB
112 if (dev) {
113 if (dev->ieee80211_ptr)
114 byifidx =
115 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
116 dev_put(dev);
117 }
118 err = -ENODEV;
119 }
120
b5850a7a
LR
121 if (bywiphyidx && byifidx) {
122 if (bywiphyidx != byifidx)
55682965
JB
123 return ERR_PTR(-EINVAL);
124 else
b5850a7a 125 return bywiphyidx; /* == byifidx */
55682965 126 }
b5850a7a
LR
127 if (bywiphyidx)
128 return bywiphyidx;
55682965
JB
129
130 if (byifidx)
131 return byifidx;
132
133 return ERR_PTR(err);
134}
135
136struct cfg80211_registered_device *
137cfg80211_get_dev_from_info(struct genl_info *info)
138{
79c97e97 139 struct cfg80211_registered_device *rdev;
55682965 140
a1794390 141 mutex_lock(&cfg80211_mutex);
79c97e97 142 rdev = __cfg80211_rdev_from_info(info);
55682965
JB
143
144 /* if it is not an error we grab the lock on
145 * it to assure it won't be going away while
146 * we operate on it */
79c97e97
JB
147 if (!IS_ERR(rdev))
148 mutex_lock(&rdev->mtx);
55682965 149
a1794390 150 mutex_unlock(&cfg80211_mutex);
55682965 151
79c97e97 152 return rdev;
55682965
JB
153}
154
155struct cfg80211_registered_device *
463d0183 156cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
55682965 157{
79c97e97 158 struct cfg80211_registered_device *rdev = ERR_PTR(-ENODEV);
55682965
JB
159 struct net_device *dev;
160
a1794390 161 mutex_lock(&cfg80211_mutex);
463d0183 162 dev = dev_get_by_index(net, ifindex);
55682965
JB
163 if (!dev)
164 goto out;
165 if (dev->ieee80211_ptr) {
79c97e97
JB
166 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
167 mutex_lock(&rdev->mtx);
55682965 168 } else
79c97e97 169 rdev = ERR_PTR(-ENODEV);
55682965
JB
170 dev_put(dev);
171 out:
a1794390 172 mutex_unlock(&cfg80211_mutex);
79c97e97 173 return rdev;
55682965
JB
174}
175
4bbf4d56 176/* requires cfg80211_mutex to be held */
55682965
JB
177int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
178 char *newname)
179{
79c97e97 180 struct cfg80211_registered_device *rdev2;
b5850a7a 181 int wiphy_idx, taken = -1, result, digits;
55682965 182
4bbf4d56 183 assert_cfg80211_lock();
2940bb69 184
55682965 185 /* prohibit calling the thing phy%d when %d is not its number */
b5850a7a
LR
186 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
187 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
188 /* count number of places needed to print wiphy_idx */
55682965 189 digits = 1;
b5850a7a 190 while (wiphy_idx /= 10)
55682965
JB
191 digits++;
192 /*
193 * deny the name if it is phy<idx> where <idx> is printed
194 * without leading zeroes. taken == strlen(newname) here
195 */
196 if (taken == strlen(PHY_NAME) + digits)
4bbf4d56 197 return -EINVAL;
2940bb69
EB
198 }
199
200
201 /* Ignore nop renames */
2940bb69 202 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
4bbf4d56 203 return 0;
2940bb69
EB
204
205 /* Ensure another device does not already have this name. */
79c97e97
JB
206 list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
207 if (strcmp(newname, dev_name(&rdev2->wiphy.dev)) == 0)
4bbf4d56 208 return -EINVAL;
55682965 209
55682965
JB
210 result = device_rename(&rdev->wiphy.dev, newname);
211 if (result)
4bbf4d56 212 return result;
55682965 213
33c0360b
JB
214 if (rdev->wiphy.debugfsdir &&
215 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
55682965
JB
216 rdev->wiphy.debugfsdir,
217 rdev->wiphy.debugfsdir->d_parent,
218 newname))
219 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
220 newname);
221
4bbf4d56 222 nl80211_notify_dev_rename(rdev);
55682965 223
4bbf4d56 224 return 0;
55682965
JB
225}
226
463d0183
JB
227int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
228 struct net *net)
229{
230 struct wireless_dev *wdev;
231 int err = 0;
232
233 if (!rdev->wiphy.netnsok)
234 return -EOPNOTSUPP;
235
236 list_for_each_entry(wdev, &rdev->netdev_list, list) {
237 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
238 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
239 if (err)
240 break;
241 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
242 }
243
244 if (err) {
245 /* failed -- clean up to old netns */
246 net = wiphy_net(&rdev->wiphy);
247
248 list_for_each_entry_continue_reverse(wdev, &rdev->netdev_list,
249 list) {
250 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
251 err = dev_change_net_namespace(wdev->netdev, net,
252 "wlan%d");
253 WARN_ON(err);
254 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
255 }
256 }
257
258 wiphy_net_set(&rdev->wiphy, net);
259
260 return err;
261}
262
1f87f7d3
JB
263static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
264{
79c97e97 265 struct cfg80211_registered_device *rdev = data;
1f87f7d3 266
79c97e97 267 rdev->ops->rfkill_poll(&rdev->wiphy);
1f87f7d3
JB
268}
269
270static int cfg80211_rfkill_set_block(void *data, bool blocked)
271{
79c97e97 272 struct cfg80211_registered_device *rdev = data;
1f87f7d3
JB
273 struct wireless_dev *wdev;
274
275 if (!blocked)
276 return 0;
277
278 rtnl_lock();
79c97e97 279 mutex_lock(&rdev->devlist_mtx);
1f87f7d3 280
79c97e97 281 list_for_each_entry(wdev, &rdev->netdev_list, list)
1f87f7d3
JB
282 dev_close(wdev->netdev);
283
79c97e97 284 mutex_unlock(&rdev->devlist_mtx);
1f87f7d3
JB
285 rtnl_unlock();
286
287 return 0;
288}
289
290static void cfg80211_rfkill_sync_work(struct work_struct *work)
291{
79c97e97 292 struct cfg80211_registered_device *rdev;
1f87f7d3 293
79c97e97
JB
294 rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
295 cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
1f87f7d3
JB
296}
297
667503dd
JB
298static void cfg80211_event_work(struct work_struct *work)
299{
300 struct cfg80211_registered_device *rdev;
667503dd
JB
301
302 rdev = container_of(work, struct cfg80211_registered_device,
303 event_work);
304
305 rtnl_lock();
306 cfg80211_lock_rdev(rdev);
667503dd 307
3d54d255 308 cfg80211_process_rdev_events(rdev);
667503dd
JB
309 cfg80211_unlock_rdev(rdev);
310 rtnl_unlock();
311}
312
704232c2
JB
313/* exported functions */
314
3dcf670b 315struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
704232c2 316{
638af073
DC
317 static int wiphy_counter;
318
79c97e97 319 struct cfg80211_registered_device *rdev;
704232c2
JB
320 int alloc_size;
321
0b20633d
JB
322 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
323 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
324 WARN_ON(ops->connect && !ops->disconnect);
325 WARN_ON(ops->join_ibss && !ops->leave_ibss);
326 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
327 WARN_ON(ops->add_station && !ops->del_station);
328 WARN_ON(ops->add_mpath && !ops->del_mpath);
41ade00f 329
79c97e97 330 alloc_size = sizeof(*rdev) + sizeof_priv;
704232c2 331
79c97e97
JB
332 rdev = kzalloc(alloc_size, GFP_KERNEL);
333 if (!rdev)
704232c2
JB
334 return NULL;
335
79c97e97 336 rdev->ops = ops;
704232c2 337
a1794390 338 mutex_lock(&cfg80211_mutex);
704232c2 339
79c97e97 340 rdev->wiphy_idx = wiphy_counter++;
a4d73ee1 341
79c97e97 342 if (unlikely(!wiphy_idx_valid(rdev->wiphy_idx))) {
638af073 343 wiphy_counter--;
a1794390 344 mutex_unlock(&cfg80211_mutex);
704232c2 345 /* ugh, wrapped! */
79c97e97 346 kfree(rdev);
704232c2
JB
347 return NULL;
348 }
704232c2 349
a1794390 350 mutex_unlock(&cfg80211_mutex);
638af073 351
704232c2 352 /* give it a proper name */
79c97e97
JB
353 dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
354
355 mutex_init(&rdev->mtx);
356 mutex_init(&rdev->devlist_mtx);
357 INIT_LIST_HEAD(&rdev->netdev_list);
358 spin_lock_init(&rdev->bss_lock);
359 INIT_LIST_HEAD(&rdev->bss_list);
360 INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
361
362 device_initialize(&rdev->wiphy.dev);
363 rdev->wiphy.dev.class = &ieee80211_class;
364 rdev->wiphy.dev.platform_data = rdev;
365
16cb9d42
JB
366 rdev->wiphy.ps_default = CONFIG_CFG80211_DEFAULT_PS_VALUE;
367
463d0183
JB
368 wiphy_net_set(&rdev->wiphy, &init_net);
369
79c97e97
JB
370 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
371 rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
372 &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
373 &rdev->rfkill_ops, rdev);
374
375 if (!rdev->rfkill) {
376 kfree(rdev);
1f87f7d3
JB
377 return NULL;
378 }
379
79c97e97
JB
380 INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
381 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
382 INIT_WORK(&rdev->event_work, cfg80211_event_work);
1f87f7d3 383
ad002395
JB
384 init_waitqueue_head(&rdev->dev_wait);
385
b9a5f8ca
JM
386 /*
387 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
388 * Fragmentation and RTS threshold are disabled by default with the
389 * special -1 value.
390 */
79c97e97
JB
391 rdev->wiphy.retry_short = 7;
392 rdev->wiphy.retry_long = 4;
393 rdev->wiphy.frag_threshold = (u32) -1;
394 rdev->wiphy.rts_threshold = (u32) -1;
b9a5f8ca 395
79c97e97 396 return &rdev->wiphy;
704232c2
JB
397}
398EXPORT_SYMBOL(wiphy_new);
399
400int wiphy_register(struct wiphy *wiphy)
401{
79c97e97 402 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
704232c2 403 int res;
8318d78a
JB
404 enum ieee80211_band band;
405 struct ieee80211_supported_band *sband;
406 bool have_band = false;
407 int i;
f59ac048
LR
408 u16 ifmodes = wiphy->interface_modes;
409
410 /* sanity check ifmodes */
411 WARN_ON(!ifmodes);
412 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
413 if (WARN_ON(ifmodes != wiphy->interface_modes))
414 wiphy->interface_modes = ifmodes;
8318d78a
JB
415
416 /* sanity check supported bands/channels */
417 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
418 sband = wiphy->bands[band];
419 if (!sband)
420 continue;
421
422 sband->band = band;
423
881d948c
JB
424 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
425 return -EINVAL;
426
427 /*
428 * Since we use a u32 for rate bitmaps in
429 * ieee80211_get_response_rate, we cannot
430 * have more than 32 legacy rates.
431 */
432 if (WARN_ON(sband->n_bitrates > 32))
8318d78a 433 return -EINVAL;
8318d78a
JB
434
435 for (i = 0; i < sband->n_channels; i++) {
436 sband->channels[i].orig_flags =
437 sband->channels[i].flags;
438 sband->channels[i].orig_mag =
439 sband->channels[i].max_antenna_gain;
440 sband->channels[i].orig_mpwr =
441 sband->channels[i].max_power;
442 sband->channels[i].band = band;
443 }
444
445 have_band = true;
446 }
447
448 if (!have_band) {
449 WARN_ON(1);
450 return -EINVAL;
451 }
452
453 /* check and set up bitrates */
454 ieee80211_set_bitrate_flags(wiphy);
455
79c97e97 456 res = device_add(&rdev->wiphy.dev);
704232c2 457 if (res)
2f0accc1 458 return res;
704232c2 459
79c97e97 460 res = rfkill_register(rdev->rfkill);
1f87f7d3
JB
461 if (res)
462 goto out_rm_dev;
463
2f0accc1
JB
464 mutex_lock(&cfg80211_mutex);
465
466 /* set up regulatory info */
467 wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
468
79c97e97 469 list_add(&rdev->list, &cfg80211_rdev_list);
f5ea9120 470 cfg80211_rdev_list_generation++;
704232c2 471
2f0accc1
JB
472 mutex_unlock(&cfg80211_mutex);
473
704232c2 474 /* add to debugfs */
79c97e97
JB
475 rdev->wiphy.debugfsdir =
476 debugfs_create_dir(wiphy_name(&rdev->wiphy),
704232c2 477 ieee80211_debugfs_dir);
79c97e97
JB
478 if (IS_ERR(rdev->wiphy.debugfsdir))
479 rdev->wiphy.debugfsdir = NULL;
704232c2 480
73d54c9e
LR
481 if (wiphy->custom_regulatory) {
482 struct regulatory_request request;
483
484 request.wiphy_idx = get_wiphy_idx(wiphy);
485 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
486 request.alpha2[0] = '9';
487 request.alpha2[1] = '9';
488
489 nl80211_send_reg_change_event(&request);
490 }
491
79c97e97 492 cfg80211_debugfs_rdev_add(rdev);
1ac61302 493
2f0accc1 494 return 0;
1f87f7d3
JB
495
496 out_rm_dev:
79c97e97 497 device_del(&rdev->wiphy.dev);
704232c2
JB
498 return res;
499}
500EXPORT_SYMBOL(wiphy_register);
501
1f87f7d3
JB
502void wiphy_rfkill_start_polling(struct wiphy *wiphy)
503{
79c97e97 504 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
1f87f7d3 505
79c97e97 506 if (!rdev->ops->rfkill_poll)
1f87f7d3 507 return;
79c97e97
JB
508 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
509 rfkill_resume_polling(rdev->rfkill);
1f87f7d3
JB
510}
511EXPORT_SYMBOL(wiphy_rfkill_start_polling);
512
513void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
514{
79c97e97 515 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
1f87f7d3 516
79c97e97 517 rfkill_pause_polling(rdev->rfkill);
1f87f7d3
JB
518}
519EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
520
704232c2
JB
521void wiphy_unregister(struct wiphy *wiphy)
522{
79c97e97 523 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
704232c2 524
79c97e97 525 rfkill_unregister(rdev->rfkill);
1f87f7d3 526
f16bfc1c 527 /* protect the device list */
a1794390 528 mutex_lock(&cfg80211_mutex);
704232c2 529
ad002395
JB
530 wait_event(rdev->dev_wait, ({
531 int __count;
532 mutex_lock(&rdev->devlist_mtx);
533 __count = rdev->opencount;
534 mutex_unlock(&rdev->devlist_mtx);
535 __count == 0;}));
536
537 mutex_lock(&rdev->devlist_mtx);
79c97e97 538 BUG_ON(!list_empty(&rdev->netdev_list));
ad002395
JB
539 mutex_unlock(&rdev->devlist_mtx);
540
541 /*
542 * First remove the hardware from everywhere, this makes
543 * it impossible to find from userspace.
544 */
545 cfg80211_debugfs_rdev_del(rdev);
546 list_del(&rdev->list);
f16bfc1c
JB
547
548 /*
79c97e97 549 * Try to grab rdev->mtx. If a command is still in progress,
f16bfc1c
JB
550 * hopefully the driver will refuse it since it's tearing
551 * down the device already. We wait for this command to complete
552 * before unlinking the item from the list.
553 * Note: as codified by the BUG_ON above we cannot get here if
ad002395
JB
554 * a virtual interface is still present. Hence, we can only get
555 * to lock contention here if userspace issues a command that
556 * identified the hardware by wiphy index.
f16bfc1c 557 */
0ff6ce7b 558 cfg80211_lock_rdev(rdev);
ad002395 559 /* nothing */
0ff6ce7b 560 cfg80211_unlock_rdev(rdev);
704232c2 561
3f2355cb
LR
562 /* If this device got a regulatory hint tell core its
563 * free to listen now to a new shiny device regulatory hint */
564 reg_device_remove(wiphy);
565
f5ea9120 566 cfg80211_rdev_list_generation++;
79c97e97
JB
567 device_del(&rdev->wiphy.dev);
568 debugfs_remove(rdev->wiphy.debugfsdir);
704232c2 569
a1794390 570 mutex_unlock(&cfg80211_mutex);
6682588a 571
36e6fea8 572 flush_work(&rdev->scan_done_wk);
6682588a 573 cancel_work_sync(&rdev->conn_work);
6682588a 574 flush_work(&rdev->event_work);
704232c2
JB
575}
576EXPORT_SYMBOL(wiphy_unregister);
577
79c97e97 578void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
704232c2 579{
2a519311 580 struct cfg80211_internal_bss *scan, *tmp;
79c97e97
JB
581 rfkill_destroy(rdev->rfkill);
582 mutex_destroy(&rdev->mtx);
583 mutex_destroy(&rdev->devlist_mtx);
584 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
78c1c7e1 585 cfg80211_put_bss(&scan->pub);
79c97e97 586 kfree(rdev);
704232c2
JB
587}
588
589void wiphy_free(struct wiphy *wiphy)
590{
591 put_device(&wiphy->dev);
592}
593EXPORT_SYMBOL(wiphy_free);
594
1f87f7d3
JB
595void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
596{
79c97e97 597 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
1f87f7d3 598
79c97e97
JB
599 if (rfkill_set_hw_state(rdev->rfkill, blocked))
600 schedule_work(&rdev->rfkill_sync);
1f87f7d3
JB
601}
602EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
603
ad002395
JB
604static void wdev_cleanup_work(struct work_struct *work)
605{
606 struct wireless_dev *wdev;
607 struct cfg80211_registered_device *rdev;
608
609 wdev = container_of(work, struct wireless_dev, cleanup_work);
610 rdev = wiphy_to_dev(wdev->wiphy);
611
612 cfg80211_lock_rdev(rdev);
613
614 if (WARN_ON(rdev->scan_req && rdev->scan_req->dev == wdev->netdev)) {
615 rdev->scan_req->aborted = true;
01a0ac41 616 ___cfg80211_scan_done(rdev, true);
ad002395
JB
617 }
618
619 cfg80211_unlock_rdev(rdev);
620
621 mutex_lock(&rdev->devlist_mtx);
622 rdev->opencount--;
623 mutex_unlock(&rdev->devlist_mtx);
624 wake_up(&rdev->dev_wait);
625
626 dev_put(wdev->netdev);
627}
628
704232c2
JB
629static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
630 unsigned long state,
631 void *ndev)
632{
633 struct net_device *dev = ndev;
2a783c13 634 struct wireless_dev *wdev = dev->ieee80211_ptr;
704232c2
JB
635 struct cfg80211_registered_device *rdev;
636
2a783c13 637 if (!wdev)
1f87f7d3 638 return NOTIFY_DONE;
704232c2 639
2a783c13 640 rdev = wiphy_to_dev(wdev->wiphy);
704232c2 641
2a783c13 642 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
60719ffd 643
704232c2
JB
644 switch (state) {
645 case NETDEV_REGISTER:
0ff6ce7b
JB
646 /*
647 * NB: cannot take rdev->mtx here because this may be
648 * called within code protected by it when interfaces
649 * are added with nl80211.
650 */
667503dd 651 mutex_init(&wdev->mtx);
ad002395 652 INIT_WORK(&wdev->cleanup_work, wdev_cleanup_work);
667503dd
JB
653 INIT_LIST_HEAD(&wdev->event_list);
654 spin_lock_init(&wdev->event_lock);
704232c2 655 mutex_lock(&rdev->devlist_mtx);
2a783c13 656 list_add(&wdev->list, &rdev->netdev_list);
f5ea9120 657 rdev->devlist_generation++;
463d0183
JB
658 /* can only change netns with wiphy */
659 dev->features |= NETIF_F_NETNS_LOCAL;
660
704232c2
JB
661 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
662 "phy80211")) {
663 printk(KERN_ERR "wireless: failed to add phy80211 "
664 "symlink to netdev!\n");
665 }
2a783c13 666 wdev->netdev = dev;
b23aa676 667 wdev->sme_state = CFG80211_SME_IDLE;
bc92afd9 668 mutex_unlock(&rdev->devlist_mtx);
08645126 669#ifdef CONFIG_WIRELESS_EXT
a9a11622
JB
670 if (!dev->wireless_handlers)
671 dev->wireless_handlers = &cfg80211_wext_handler;
2a783c13
JB
672 wdev->wext.default_key = -1;
673 wdev->wext.default_mgmt_key = -1;
f2129354 674 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
16cb9d42 675 wdev->wext.ps = wdev->wiphy->ps_default;
75e6c3b7 676 wdev->wext.ps_timeout = 100;
bc92afd9
JB
677 if (rdev->ops->set_power_mgmt)
678 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev,
679 wdev->wext.ps,
680 wdev->wext.ps_timeout)) {
681 /* assume this means it's off */
682 wdev->wext.ps = false;
683 }
08645126 684#endif
704232c2 685 break;
04a773ad 686 case NETDEV_GOING_DOWN:
b23aa676
SO
687 switch (wdev->iftype) {
688 case NL80211_IFTYPE_ADHOC:
689 cfg80211_leave_ibss(rdev, dev, true);
690 break;
691 case NL80211_IFTYPE_STATION:
667503dd 692 wdev_lock(wdev);
f2129354
JB
693#ifdef CONFIG_WIRELESS_EXT
694 kfree(wdev->wext.ie);
695 wdev->wext.ie = NULL;
696 wdev->wext.ie_len = 0;
0eb14647 697 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
f2129354 698#endif
667503dd
JB
699 __cfg80211_disconnect(rdev, dev,
700 WLAN_REASON_DEAUTH_LEAVING, true);
19957bb3 701 cfg80211_mlme_down(rdev, dev);
667503dd 702 wdev_unlock(wdev);
b23aa676
SO
703 break;
704 default:
705 break;
706 }
01a0ac41
JB
707 break;
708 case NETDEV_DOWN:
ad002395
JB
709 dev_hold(dev);
710 schedule_work(&wdev->cleanup_work);
04a773ad
JB
711 break;
712 case NETDEV_UP:
ad002395
JB
713 /*
714 * If we have a really quick DOWN/UP succession we may
715 * have this work still pending ... cancel it and see
716 * if it was pending, in which case we need to account
717 * for some of the work it would have done.
718 */
719 if (cancel_work_sync(&wdev->cleanup_work)) {
720 mutex_lock(&rdev->devlist_mtx);
721 rdev->opencount--;
722 mutex_unlock(&rdev->devlist_mtx);
723 dev_put(dev);
724 }
04a773ad 725#ifdef CONFIG_WIRELESS_EXT
667503dd 726 cfg80211_lock_rdev(rdev);
aee83eaf 727 mutex_lock(&rdev->devlist_mtx);
667503dd 728 wdev_lock(wdev);
f2129354
JB
729 switch (wdev->iftype) {
730 case NL80211_IFTYPE_ADHOC:
fffd0934 731 cfg80211_ibss_wext_join(rdev, wdev);
04a773ad 732 break;
f2129354 733 case NL80211_IFTYPE_STATION:
fffd0934 734 cfg80211_mgd_wext_connect(rdev, wdev);
f2129354
JB
735 break;
736 default:
04a773ad 737 break;
f2129354 738 }
667503dd 739 wdev_unlock(wdev);
ad002395 740 rdev->opencount++;
aee83eaf 741 mutex_unlock(&rdev->devlist_mtx);
667503dd 742 cfg80211_unlock_rdev(rdev);
04a773ad 743#endif
2a783c13 744 break;
704232c2 745 case NETDEV_UNREGISTER:
0ff6ce7b
JB
746 /*
747 * NB: cannot take rdev->mtx here because this may be
748 * called within code protected by it when interfaces
749 * are removed with nl80211.
750 */
704232c2 751 mutex_lock(&rdev->devlist_mtx);
e40cbdac
JB
752 /*
753 * It is possible to get NETDEV_UNREGISTER
754 * multiple times. To detect that, check
755 * that the interface is still on the list
756 * of registered interfaces, and only then
757 * remove and clean it up.
758 */
2a783c13 759 if (!list_empty(&wdev->list)) {
704232c2 760 sysfs_remove_link(&dev->dev.kobj, "phy80211");
2a783c13 761 list_del_init(&wdev->list);
f5ea9120 762 rdev->devlist_generation++;
fffd0934 763#ifdef CONFIG_WIRELESS_EXT
e40cbdac 764 kfree(wdev->wext.keys);
fffd0934 765#endif
e40cbdac
JB
766 }
767 mutex_unlock(&rdev->devlist_mtx);
704232c2 768 break;
1f87f7d3 769 case NETDEV_PRE_UP:
0b20633d
JB
770 if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
771 return notifier_from_errno(-EOPNOTSUPP);
1f87f7d3
JB
772 if (rfkill_blocked(rdev->rfkill))
773 return notifier_from_errno(-ERFKILL);
774 break;
704232c2
JB
775 }
776
1f87f7d3 777 return NOTIFY_DONE;
704232c2
JB
778}
779
780static struct notifier_block cfg80211_netdev_notifier = {
781 .notifier_call = cfg80211_netdev_notifier_call,
782};
783
463d0183
JB
784static void __net_exit cfg80211_pernet_exit(struct net *net)
785{
786 struct cfg80211_registered_device *rdev;
787
788 rtnl_lock();
789 mutex_lock(&cfg80211_mutex);
790 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
791 if (net_eq(wiphy_net(&rdev->wiphy), net))
792 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
793 }
794 mutex_unlock(&cfg80211_mutex);
795 rtnl_unlock();
796}
797
798static struct pernet_operations cfg80211_pernet_ops = {
799 .exit = cfg80211_pernet_exit,
800};
801
802static int __init cfg80211_init(void)
704232c2 803{
b2e1b302
LR
804 int err;
805
463d0183
JB
806 err = register_pernet_device(&cfg80211_pernet_ops);
807 if (err)
808 goto out_fail_pernet;
809
b2e1b302 810 err = wiphy_sysfs_init();
704232c2
JB
811 if (err)
812 goto out_fail_sysfs;
813
814 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
815 if (err)
816 goto out_fail_notifier;
817
55682965
JB
818 err = nl80211_init();
819 if (err)
820 goto out_fail_nl80211;
821
704232c2
JB
822 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
823
b2e1b302
LR
824 err = regulatory_init();
825 if (err)
826 goto out_fail_reg;
827
704232c2
JB
828 return 0;
829
b2e1b302
LR
830out_fail_reg:
831 debugfs_remove(ieee80211_debugfs_dir);
55682965
JB
832out_fail_nl80211:
833 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
704232c2
JB
834out_fail_notifier:
835 wiphy_sysfs_exit();
836out_fail_sysfs:
463d0183
JB
837 unregister_pernet_device(&cfg80211_pernet_ops);
838out_fail_pernet:
704232c2
JB
839 return err;
840}
3a462465 841subsys_initcall(cfg80211_init);
704232c2
JB
842
843static void cfg80211_exit(void)
844{
845 debugfs_remove(ieee80211_debugfs_dir);
55682965 846 nl80211_exit();
704232c2
JB
847 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
848 wiphy_sysfs_exit();
b2e1b302 849 regulatory_exit();
463d0183 850 unregister_pernet_device(&cfg80211_pernet_ops);
704232c2
JB
851}
852module_exit(cfg80211_exit);
This page took 0.350089 seconds and 5 git commands to generate.