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