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