]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/wireless/core.c
nl80211: Add set/get for frag/rts threshold and retry limits
[mirror_ubuntu-artful-kernel.git] / net / wireless / core.c
CommitLineData
704232c2
JB
1/*
2 * This is the linux wireless configuration interface.
3 *
f59ac048 4 * Copyright 2006-2008 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>
15#include <net/genetlink.h>
16#include <net/cfg80211.h>
55682965 17#include "nl80211.h"
704232c2
JB
18#include "core.h"
19#include "sysfs.h"
20
21/* name for sysfs, %d is appended */
22#define PHY_NAME "phy"
23
24MODULE_AUTHOR("Johannes Berg");
25MODULE_LICENSE("GPL");
26MODULE_DESCRIPTION("wireless configuration support");
27
28/* RCU might be appropriate here since we usually
29 * only read the list, and that can happen quite
30 * often because we need to do it for each command */
31LIST_HEAD(cfg80211_drv_list);
a1794390
LR
32
33/*
e38f8a7a
LR
34 * This is used to protect the cfg80211_drv_list, cfg80211_regdomain,
35 * country_ie_regdomain, the reg_beacon_list and the the last regulatory
36 * request receipt (last_request).
a1794390
LR
37 */
38DEFINE_MUTEX(cfg80211_mutex);
704232c2
JB
39
40/* for debugfs */
41static struct dentry *ieee80211_debugfs_dir;
42
806a9e39
LR
43/* requires cfg80211_mutex to be held! */
44struct cfg80211_registered_device *cfg80211_drv_by_wiphy_idx(int wiphy_idx)
55682965
JB
45{
46 struct cfg80211_registered_device *result = NULL, *drv;
47
85fd129a
LR
48 if (!wiphy_idx_valid(wiphy_idx))
49 return NULL;
50
761cf7ec
LR
51 assert_cfg80211_lock();
52
55682965 53 list_for_each_entry(drv, &cfg80211_drv_list, list) {
b5850a7a 54 if (drv->wiphy_idx == wiphy_idx) {
55682965
JB
55 result = drv;
56 break;
57 }
58 }
59
60 return result;
61}
62
806a9e39
LR
63int get_wiphy_idx(struct wiphy *wiphy)
64{
65 struct cfg80211_registered_device *drv;
66 if (!wiphy)
67 return WIPHY_IDX_STALE;
68 drv = wiphy_to_dev(wiphy);
69 return drv->wiphy_idx;
70}
71
72/* requires cfg80211_drv_mutex to be held! */
73struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
74{
75 struct cfg80211_registered_device *drv;
76
77 if (!wiphy_idx_valid(wiphy_idx))
78 return NULL;
79
80 assert_cfg80211_lock();
81
82 drv = cfg80211_drv_by_wiphy_idx(wiphy_idx);
83 if (!drv)
84 return NULL;
85 return &drv->wiphy;
86}
87
a1794390 88/* requires cfg80211_mutex to be held! */
4bbf4d56 89struct cfg80211_registered_device *
55682965
JB
90__cfg80211_drv_from_info(struct genl_info *info)
91{
92 int ifindex;
b5850a7a 93 struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
55682965
JB
94 struct net_device *dev;
95 int err = -EINVAL;
96
761cf7ec
LR
97 assert_cfg80211_lock();
98
55682965 99 if (info->attrs[NL80211_ATTR_WIPHY]) {
b5850a7a 100 bywiphyidx = cfg80211_drv_by_wiphy_idx(
55682965
JB
101 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
102 err = -ENODEV;
103 }
104
105 if (info->attrs[NL80211_ATTR_IFINDEX]) {
106 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
107 dev = dev_get_by_index(&init_net, ifindex);
108 if (dev) {
109 if (dev->ieee80211_ptr)
110 byifidx =
111 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
112 dev_put(dev);
113 }
114 err = -ENODEV;
115 }
116
b5850a7a
LR
117 if (bywiphyidx && byifidx) {
118 if (bywiphyidx != byifidx)
55682965
JB
119 return ERR_PTR(-EINVAL);
120 else
b5850a7a 121 return bywiphyidx; /* == byifidx */
55682965 122 }
b5850a7a
LR
123 if (bywiphyidx)
124 return bywiphyidx;
55682965
JB
125
126 if (byifidx)
127 return byifidx;
128
129 return ERR_PTR(err);
130}
131
132struct cfg80211_registered_device *
133cfg80211_get_dev_from_info(struct genl_info *info)
134{
135 struct cfg80211_registered_device *drv;
136
a1794390 137 mutex_lock(&cfg80211_mutex);
55682965
JB
138 drv = __cfg80211_drv_from_info(info);
139
140 /* if it is not an error we grab the lock on
141 * it to assure it won't be going away while
142 * we operate on it */
143 if (!IS_ERR(drv))
144 mutex_lock(&drv->mtx);
145
a1794390 146 mutex_unlock(&cfg80211_mutex);
55682965
JB
147
148 return drv;
149}
150
151struct cfg80211_registered_device *
152cfg80211_get_dev_from_ifindex(int ifindex)
153{
154 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
155 struct net_device *dev;
156
a1794390 157 mutex_lock(&cfg80211_mutex);
55682965
JB
158 dev = dev_get_by_index(&init_net, ifindex);
159 if (!dev)
160 goto out;
161 if (dev->ieee80211_ptr) {
162 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
163 mutex_lock(&drv->mtx);
164 } else
165 drv = ERR_PTR(-ENODEV);
166 dev_put(dev);
167 out:
a1794390 168 mutex_unlock(&cfg80211_mutex);
55682965
JB
169 return drv;
170}
171
172void cfg80211_put_dev(struct cfg80211_registered_device *drv)
173{
174 BUG_ON(IS_ERR(drv));
175 mutex_unlock(&drv->mtx);
176}
177
4bbf4d56 178/* requires cfg80211_mutex to be held */
55682965
JB
179int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
180 char *newname)
181{
2940bb69 182 struct cfg80211_registered_device *drv;
b5850a7a 183 int wiphy_idx, taken = -1, result, digits;
55682965 184
4bbf4d56 185 assert_cfg80211_lock();
2940bb69 186
55682965 187 /* prohibit calling the thing phy%d when %d is not its number */
b5850a7a
LR
188 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
189 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
190 /* count number of places needed to print wiphy_idx */
55682965 191 digits = 1;
b5850a7a 192 while (wiphy_idx /= 10)
55682965
JB
193 digits++;
194 /*
195 * deny the name if it is phy<idx> where <idx> is printed
196 * without leading zeroes. taken == strlen(newname) here
197 */
198 if (taken == strlen(PHY_NAME) + digits)
4bbf4d56 199 return -EINVAL;
2940bb69
EB
200 }
201
202
203 /* Ignore nop renames */
2940bb69 204 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
4bbf4d56 205 return 0;
2940bb69
EB
206
207 /* Ensure another device does not already have this name. */
4bbf4d56 208 list_for_each_entry(drv, &cfg80211_drv_list, list)
2940bb69 209 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
4bbf4d56 210 return -EINVAL;
55682965 211
55682965
JB
212 result = device_rename(&rdev->wiphy.dev, newname);
213 if (result)
4bbf4d56 214 return result;
55682965 215
33c0360b
JB
216 if (rdev->wiphy.debugfsdir &&
217 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
55682965
JB
218 rdev->wiphy.debugfsdir,
219 rdev->wiphy.debugfsdir->d_parent,
220 newname))
221 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
222 newname);
223
4bbf4d56 224 nl80211_notify_dev_rename(rdev);
55682965 225
4bbf4d56 226 return 0;
55682965
JB
227}
228
704232c2
JB
229/* exported functions */
230
231struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
232{
638af073
DC
233 static int wiphy_counter;
234
704232c2
JB
235 struct cfg80211_registered_device *drv;
236 int alloc_size;
237
41ade00f
JB
238 WARN_ON(!ops->add_key && ops->del_key);
239 WARN_ON(ops->add_key && !ops->del_key);
240
704232c2
JB
241 alloc_size = sizeof(*drv) + sizeof_priv;
242
243 drv = kzalloc(alloc_size, GFP_KERNEL);
244 if (!drv)
245 return NULL;
246
247 drv->ops = ops;
248
a1794390 249 mutex_lock(&cfg80211_mutex);
704232c2 250
b5850a7a 251 drv->wiphy_idx = wiphy_counter++;
a4d73ee1 252
85fd129a 253 if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
638af073 254 wiphy_counter--;
a1794390 255 mutex_unlock(&cfg80211_mutex);
704232c2
JB
256 /* ugh, wrapped! */
257 kfree(drv);
258 return NULL;
259 }
704232c2 260
a1794390 261 mutex_unlock(&cfg80211_mutex);
638af073 262
704232c2 263 /* give it a proper name */
b5850a7a 264 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
704232c2 265
704232c2
JB
266 mutex_init(&drv->mtx);
267 mutex_init(&drv->devlist_mtx);
268 INIT_LIST_HEAD(&drv->netdev_list);
2a519311
JB
269 spin_lock_init(&drv->bss_lock);
270 INIT_LIST_HEAD(&drv->bss_list);
704232c2
JB
271
272 device_initialize(&drv->wiphy.dev);
273 drv->wiphy.dev.class = &ieee80211_class;
274 drv->wiphy.dev.platform_data = drv;
275
b9a5f8ca
JM
276 /*
277 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
278 * Fragmentation and RTS threshold are disabled by default with the
279 * special -1 value.
280 */
281 drv->wiphy.retry_short = 7;
282 drv->wiphy.retry_long = 4;
283 drv->wiphy.frag_threshold = (u32) -1;
284 drv->wiphy.rts_threshold = (u32) -1;
285
704232c2
JB
286 return &drv->wiphy;
287}
288EXPORT_SYMBOL(wiphy_new);
289
290int wiphy_register(struct wiphy *wiphy)
291{
292 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
293 int res;
8318d78a
JB
294 enum ieee80211_band band;
295 struct ieee80211_supported_band *sband;
296 bool have_band = false;
297 int i;
f59ac048
LR
298 u16 ifmodes = wiphy->interface_modes;
299
2a519311
JB
300 if (WARN_ON(wiphy->max_scan_ssids < 1))
301 return -EINVAL;
302
f59ac048
LR
303 /* sanity check ifmodes */
304 WARN_ON(!ifmodes);
305 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
306 if (WARN_ON(ifmodes != wiphy->interface_modes))
307 wiphy->interface_modes = ifmodes;
8318d78a
JB
308
309 /* sanity check supported bands/channels */
310 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
311 sband = wiphy->bands[band];
312 if (!sband)
313 continue;
314
315 sband->band = band;
316
881d948c
JB
317 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
318 return -EINVAL;
319
320 /*
321 * Since we use a u32 for rate bitmaps in
322 * ieee80211_get_response_rate, we cannot
323 * have more than 32 legacy rates.
324 */
325 if (WARN_ON(sband->n_bitrates > 32))
8318d78a 326 return -EINVAL;
8318d78a
JB
327
328 for (i = 0; i < sband->n_channels; i++) {
329 sband->channels[i].orig_flags =
330 sband->channels[i].flags;
331 sband->channels[i].orig_mag =
332 sband->channels[i].max_antenna_gain;
333 sband->channels[i].orig_mpwr =
334 sband->channels[i].max_power;
335 sband->channels[i].band = band;
336 }
337
338 have_band = true;
339 }
340
341 if (!have_band) {
342 WARN_ON(1);
343 return -EINVAL;
344 }
345
346 /* check and set up bitrates */
347 ieee80211_set_bitrate_flags(wiphy);
348
a1794390 349 mutex_lock(&cfg80211_mutex);
f3b407fb 350
8318d78a 351 /* set up regulatory info */
7db90f4a 352 wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
704232c2 353
704232c2
JB
354 res = device_add(&drv->wiphy.dev);
355 if (res)
356 goto out_unlock;
357
358 list_add(&drv->list, &cfg80211_drv_list);
359
360 /* add to debugfs */
361 drv->wiphy.debugfsdir =
362 debugfs_create_dir(wiphy_name(&drv->wiphy),
363 ieee80211_debugfs_dir);
33c0360b
JB
364 if (IS_ERR(drv->wiphy.debugfsdir))
365 drv->wiphy.debugfsdir = NULL;
704232c2 366
73d54c9e
LR
367 if (wiphy->custom_regulatory) {
368 struct regulatory_request request;
369
370 request.wiphy_idx = get_wiphy_idx(wiphy);
371 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
372 request.alpha2[0] = '9';
373 request.alpha2[1] = '9';
374
375 nl80211_send_reg_change_event(&request);
376 }
377
704232c2
JB
378 res = 0;
379out_unlock:
a1794390 380 mutex_unlock(&cfg80211_mutex);
704232c2
JB
381 return res;
382}
383EXPORT_SYMBOL(wiphy_register);
384
385void wiphy_unregister(struct wiphy *wiphy)
386{
387 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
388
f16bfc1c 389 /* protect the device list */
a1794390 390 mutex_lock(&cfg80211_mutex);
704232c2 391
f16bfc1c
JB
392 BUG_ON(!list_empty(&drv->netdev_list));
393
394 /*
395 * Try to grab drv->mtx. If a command is still in progress,
396 * hopefully the driver will refuse it since it's tearing
397 * down the device already. We wait for this command to complete
398 * before unlinking the item from the list.
399 * Note: as codified by the BUG_ON above we cannot get here if
400 * a virtual interface is still associated. Hence, we can only
401 * get to lock contention here if userspace issues a command
402 * that identified the hardware by wiphy index.
403 */
704232c2 404 mutex_lock(&drv->mtx);
f16bfc1c 405 /* unlock again before freeing */
704232c2
JB
406 mutex_unlock(&drv->mtx);
407
3f2355cb
LR
408 /* If this device got a regulatory hint tell core its
409 * free to listen now to a new shiny device regulatory hint */
410 reg_device_remove(wiphy);
411
f16bfc1c 412 list_del(&drv->list);
704232c2
JB
413 device_del(&drv->wiphy.dev);
414 debugfs_remove(drv->wiphy.debugfsdir);
415
a1794390 416 mutex_unlock(&cfg80211_mutex);
704232c2
JB
417}
418EXPORT_SYMBOL(wiphy_unregister);
419
420void cfg80211_dev_free(struct cfg80211_registered_device *drv)
421{
2a519311 422 struct cfg80211_internal_bss *scan, *tmp;
704232c2
JB
423 mutex_destroy(&drv->mtx);
424 mutex_destroy(&drv->devlist_mtx);
2a519311 425 list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
78c1c7e1 426 cfg80211_put_bss(&scan->pub);
704232c2
JB
427 kfree(drv);
428}
429
430void wiphy_free(struct wiphy *wiphy)
431{
432 put_device(&wiphy->dev);
433}
434EXPORT_SYMBOL(wiphy_free);
435
436static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
437 unsigned long state,
438 void *ndev)
439{
440 struct net_device *dev = ndev;
441 struct cfg80211_registered_device *rdev;
442
443 if (!dev->ieee80211_ptr)
444 return 0;
445
446 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
447
60719ffd
JB
448 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
449
704232c2
JB
450 switch (state) {
451 case NETDEV_REGISTER:
452 mutex_lock(&rdev->devlist_mtx);
453 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
454 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
455 "phy80211")) {
456 printk(KERN_ERR "wireless: failed to add phy80211 "
457 "symlink to netdev!\n");
458 }
459 dev->ieee80211_ptr->netdev = dev;
460 mutex_unlock(&rdev->devlist_mtx);
461 break;
04a773ad
JB
462 case NETDEV_GOING_DOWN:
463 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
464 break;
465 if (!dev->ieee80211_ptr->ssid_len)
466 break;
467 cfg80211_leave_ibss(rdev, dev);
468 break;
469 case NETDEV_UP:
470#ifdef CONFIG_WIRELESS_EXT
471 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
472 break;
473 if (!dev->ieee80211_ptr->wext.ssid_len)
474 break;
475 cfg80211_join_ibss(rdev, dev, &dev->ieee80211_ptr->wext);
476 break;
477#endif
704232c2
JB
478 case NETDEV_UNREGISTER:
479 mutex_lock(&rdev->devlist_mtx);
480 if (!list_empty(&dev->ieee80211_ptr->list)) {
481 sysfs_remove_link(&dev->dev.kobj, "phy80211");
482 list_del_init(&dev->ieee80211_ptr->list);
483 }
484 mutex_unlock(&rdev->devlist_mtx);
485 break;
486 }
487
488 return 0;
489}
490
491static struct notifier_block cfg80211_netdev_notifier = {
492 .notifier_call = cfg80211_netdev_notifier_call,
493};
494
495static int cfg80211_init(void)
496{
b2e1b302
LR
497 int err;
498
b2e1b302 499 err = wiphy_sysfs_init();
704232c2
JB
500 if (err)
501 goto out_fail_sysfs;
502
503 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
504 if (err)
505 goto out_fail_notifier;
506
55682965
JB
507 err = nl80211_init();
508 if (err)
509 goto out_fail_nl80211;
510
704232c2
JB
511 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
512
b2e1b302
LR
513 err = regulatory_init();
514 if (err)
515 goto out_fail_reg;
516
704232c2
JB
517 return 0;
518
b2e1b302
LR
519out_fail_reg:
520 debugfs_remove(ieee80211_debugfs_dir);
55682965
JB
521out_fail_nl80211:
522 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
704232c2
JB
523out_fail_notifier:
524 wiphy_sysfs_exit();
525out_fail_sysfs:
526 return err;
527}
b2e1b302 528
3a462465 529subsys_initcall(cfg80211_init);
704232c2
JB
530
531static void cfg80211_exit(void)
532{
533 debugfs_remove(ieee80211_debugfs_dir);
55682965 534 nl80211_exit();
704232c2
JB
535 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
536 wiphy_sysfs_exit();
b2e1b302 537 regulatory_exit();
704232c2
JB
538}
539module_exit(cfg80211_exit);