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