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