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