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