cfg80211: Add new wireless regulatory infrastructure
[safe/jmp/linux-2.6] / 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 <linux/list.h>
17 #include <net/genetlink.h>
18 #include <net/cfg80211.h>
19 #include <net/wireless.h>
20 #include "nl80211.h"
21 #include "core.h"
22 #include "sysfs.h"
23 #include "reg.h"
24
25 /* name for sysfs, %d is appended */
26 #define PHY_NAME "phy"
27
28 MODULE_AUTHOR("Johannes Berg");
29 MODULE_LICENSE("GPL");
30 MODULE_DESCRIPTION("wireless configuration support");
31
32 struct list_head regulatory_requests;
33
34 /* Central wireless core regulatory domains, we only need two,
35  * the current one and a world regulatory domain in case we have no
36  * information to give us an alpha2 */
37 struct ieee80211_regdomain *cfg80211_regdomain;
38
39 /* We keep a static world regulatory domain in case of the absence of CRDA */
40 const struct ieee80211_regdomain world_regdom = {
41         .n_reg_rules = 1,
42         .alpha2 =  "00",
43         .reg_rules = {
44                 REG_RULE(2402, 2472, 40, 6, 20,
45                         NL80211_RRF_PASSIVE_SCAN |
46                         NL80211_RRF_NO_IBSS),
47         }
48 };
49
50 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
51 /* All this fucking static junk will be removed soon, so
52  * don't fucking count on it !@#$ */
53
54 static char *ieee80211_regdom = "US";
55 module_param(ieee80211_regdom, charp, 0444);
56 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
57
58 /* We assume 40 MHz bandwidth for the old regulatory work.
59  * We make emphasis we are using the exact same frequencies
60  * as before */
61
62 const struct ieee80211_regdomain us_regdom = {
63         .n_reg_rules = 6,
64         .alpha2 =  "US",
65         .reg_rules = {
66                 /* IEEE 802.11b/g, channels 1..11 */
67                 REG_RULE(2412-20, 2462+20, 40, 6, 27, 0),
68                 /* IEEE 802.11a, channel 36 */
69                 REG_RULE(5180-20, 5180+20, 40, 6, 23, 0),
70                 /* IEEE 802.11a, channel 40 */
71                 REG_RULE(5200-20, 5200+20, 40, 6, 23, 0),
72                 /* IEEE 802.11a, channel 44 */
73                 REG_RULE(5220-20, 5220+20, 40, 6, 23, 0),
74                 /* IEEE 802.11a, channels 48..64 */
75                 REG_RULE(5240-20, 5320+20, 40, 6, 23, 0),
76                 /* IEEE 802.11a, channels 149..165, outdoor */
77                 REG_RULE(5745-20, 5825+20, 40, 6, 30, 0),
78         }
79 };
80
81 const struct ieee80211_regdomain jp_regdom = {
82         .n_reg_rules = 3,
83         .alpha2 =  "JP",
84         .reg_rules = {
85                 /* IEEE 802.11b/g, channels 1..14 */
86                 REG_RULE(2412-20, 2484+20, 40, 6, 20, 0),
87                 /* IEEE 802.11a, channels 34..48 */
88                 REG_RULE(5170-20, 5240+20, 40, 6, 20,
89                         NL80211_RRF_PASSIVE_SCAN),
90                 /* IEEE 802.11a, channels 52..64 */
91                 REG_RULE(5260-20, 5320+20, 40, 6, 20,
92                         NL80211_RRF_NO_IBSS |
93                         NL80211_RRF_DFS),
94         }
95 };
96
97 const struct ieee80211_regdomain eu_regdom = {
98         .n_reg_rules = 6,
99         /* This alpha2 is bogus, we leave it here just for stupid
100          * backward compatibility */
101         .alpha2 =  "EU",
102         .reg_rules = {
103                 /* IEEE 802.11b/g, channels 1..13 */
104                 REG_RULE(2412-20, 2472+20, 40, 6, 20, 0),
105                 /* IEEE 802.11a, channel 36 */
106                 REG_RULE(5180-20, 5180+20, 40, 6, 23,
107                         NL80211_RRF_PASSIVE_SCAN),
108                 /* IEEE 802.11a, channel 40 */
109                 REG_RULE(5200-20, 5200+20, 40, 6, 23,
110                         NL80211_RRF_PASSIVE_SCAN),
111                 /* IEEE 802.11a, channel 44 */
112                 REG_RULE(5220-20, 5220+20, 40, 6, 23,
113                         NL80211_RRF_PASSIVE_SCAN),
114                 /* IEEE 802.11a, channels 48..64 */
115                 REG_RULE(5240-20, 5320+20, 40, 6, 20,
116                         NL80211_RRF_NO_IBSS |
117                         NL80211_RRF_DFS),
118                 /* IEEE 802.11a, channels 100..140 */
119                 REG_RULE(5500-20, 5700+20, 40, 6, 30,
120                         NL80211_RRF_NO_IBSS |
121                         NL80211_RRF_DFS),
122         }
123 };
124
125 #endif
126
127 struct ieee80211_regdomain *cfg80211_world_regdom =
128         (struct ieee80211_regdomain *) &world_regdom;
129
130 LIST_HEAD(regulatory_requests);
131 DEFINE_MUTEX(cfg80211_reg_mutex);
132
133 /* RCU might be appropriate here since we usually
134  * only read the list, and that can happen quite
135  * often because we need to do it for each command */
136 LIST_HEAD(cfg80211_drv_list);
137 DEFINE_MUTEX(cfg80211_drv_mutex);
138 static int wiphy_counter;
139
140 /* for debugfs */
141 static struct dentry *ieee80211_debugfs_dir;
142
143 /* requires cfg80211_drv_mutex to be held! */
144 static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
145 {
146         struct cfg80211_registered_device *result = NULL, *drv;
147
148         list_for_each_entry(drv, &cfg80211_drv_list, list) {
149                 if (drv->idx == wiphy) {
150                         result = drv;
151                         break;
152                 }
153         }
154
155         return result;
156 }
157
158 /* requires cfg80211_drv_mutex to be held! */
159 static struct cfg80211_registered_device *
160 __cfg80211_drv_from_info(struct genl_info *info)
161 {
162         int ifindex;
163         struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
164         struct net_device *dev;
165         int err = -EINVAL;
166
167         if (info->attrs[NL80211_ATTR_WIPHY]) {
168                 bywiphy = cfg80211_drv_by_wiphy(
169                                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
170                 err = -ENODEV;
171         }
172
173         if (info->attrs[NL80211_ATTR_IFINDEX]) {
174                 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
175                 dev = dev_get_by_index(&init_net, ifindex);
176                 if (dev) {
177                         if (dev->ieee80211_ptr)
178                                 byifidx =
179                                         wiphy_to_dev(dev->ieee80211_ptr->wiphy);
180                         dev_put(dev);
181                 }
182                 err = -ENODEV;
183         }
184
185         if (bywiphy && byifidx) {
186                 if (bywiphy != byifidx)
187                         return ERR_PTR(-EINVAL);
188                 else
189                         return bywiphy; /* == byifidx */
190         }
191         if (bywiphy)
192                 return bywiphy;
193
194         if (byifidx)
195                 return byifidx;
196
197         return ERR_PTR(err);
198 }
199
200 struct cfg80211_registered_device *
201 cfg80211_get_dev_from_info(struct genl_info *info)
202 {
203         struct cfg80211_registered_device *drv;
204
205         mutex_lock(&cfg80211_drv_mutex);
206         drv = __cfg80211_drv_from_info(info);
207
208         /* if it is not an error we grab the lock on
209          * it to assure it won't be going away while
210          * we operate on it */
211         if (!IS_ERR(drv))
212                 mutex_lock(&drv->mtx);
213
214         mutex_unlock(&cfg80211_drv_mutex);
215
216         return drv;
217 }
218
219 struct cfg80211_registered_device *
220 cfg80211_get_dev_from_ifindex(int ifindex)
221 {
222         struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
223         struct net_device *dev;
224
225         mutex_lock(&cfg80211_drv_mutex);
226         dev = dev_get_by_index(&init_net, ifindex);
227         if (!dev)
228                 goto out;
229         if (dev->ieee80211_ptr) {
230                 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
231                 mutex_lock(&drv->mtx);
232         } else
233                 drv = ERR_PTR(-ENODEV);
234         dev_put(dev);
235  out:
236         mutex_unlock(&cfg80211_drv_mutex);
237         return drv;
238 }
239
240 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
241 {
242         BUG_ON(IS_ERR(drv));
243         mutex_unlock(&drv->mtx);
244 }
245
246 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
247                         char *newname)
248 {
249         struct cfg80211_registered_device *drv;
250         int idx, taken = -1, result, digits;
251
252         mutex_lock(&cfg80211_drv_mutex);
253
254         /* prohibit calling the thing phy%d when %d is not its number */
255         sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
256         if (taken == strlen(newname) && idx != rdev->idx) {
257                 /* count number of places needed to print idx */
258                 digits = 1;
259                 while (idx /= 10)
260                         digits++;
261                 /*
262                  * deny the name if it is phy<idx> where <idx> is printed
263                  * without leading zeroes. taken == strlen(newname) here
264                  */
265                 result = -EINVAL;
266                 if (taken == strlen(PHY_NAME) + digits)
267                         goto out_unlock;
268         }
269
270
271         /* Ignore nop renames */
272         result = 0;
273         if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
274                 goto out_unlock;
275
276         /* Ensure another device does not already have this name. */
277         list_for_each_entry(drv, &cfg80211_drv_list, list) {
278                 result = -EINVAL;
279                 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
280                         goto out_unlock;
281         }
282
283         /* this will only check for collisions in sysfs
284          * which is not even always compiled in.
285          */
286         result = device_rename(&rdev->wiphy.dev, newname);
287         if (result)
288                 goto out_unlock;
289
290         if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
291                             rdev->wiphy.debugfsdir,
292                             rdev->wiphy.debugfsdir->d_parent,
293                             newname))
294                 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
295                        newname);
296
297         result = 0;
298 out_unlock:
299         mutex_unlock(&cfg80211_drv_mutex);
300         if (result == 0)
301                 nl80211_notify_dev_rename(rdev);
302
303         return result;
304 }
305
306 /* exported functions */
307
308 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
309 {
310         struct cfg80211_registered_device *drv;
311         int alloc_size;
312
313         WARN_ON(!ops->add_key && ops->del_key);
314         WARN_ON(ops->add_key && !ops->del_key);
315
316         alloc_size = sizeof(*drv) + sizeof_priv;
317
318         drv = kzalloc(alloc_size, GFP_KERNEL);
319         if (!drv)
320                 return NULL;
321
322         drv->ops = ops;
323
324         mutex_lock(&cfg80211_drv_mutex);
325
326         drv->idx = wiphy_counter;
327
328         /* now increase counter for the next device unless
329          * it has wrapped previously */
330         if (wiphy_counter >= 0)
331                 wiphy_counter++;
332
333         mutex_unlock(&cfg80211_drv_mutex);
334
335         if (unlikely(drv->idx < 0)) {
336                 /* ugh, wrapped! */
337                 kfree(drv);
338                 return NULL;
339         }
340
341         /* give it a proper name */
342         snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
343                  PHY_NAME "%d", drv->idx);
344
345         mutex_init(&drv->mtx);
346         mutex_init(&drv->devlist_mtx);
347         INIT_LIST_HEAD(&drv->netdev_list);
348
349         device_initialize(&drv->wiphy.dev);
350         drv->wiphy.dev.class = &ieee80211_class;
351         drv->wiphy.dev.platform_data = drv;
352
353         return &drv->wiphy;
354 }
355 EXPORT_SYMBOL(wiphy_new);
356
357 int wiphy_register(struct wiphy *wiphy)
358 {
359         struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
360         int res;
361         enum ieee80211_band band;
362         struct ieee80211_supported_band *sband;
363         bool have_band = false;
364         int i;
365         u16 ifmodes = wiphy->interface_modes;
366
367         /* sanity check ifmodes */
368         WARN_ON(!ifmodes);
369         ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
370         if (WARN_ON(ifmodes != wiphy->interface_modes))
371                 wiphy->interface_modes = ifmodes;
372
373         /* sanity check supported bands/channels */
374         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
375                 sband = wiphy->bands[band];
376                 if (!sband)
377                         continue;
378
379                 sband->band = band;
380
381                 if (!sband->n_channels || !sband->n_bitrates) {
382                         WARN_ON(1);
383                         return -EINVAL;
384                 }
385
386                 for (i = 0; i < sband->n_channels; i++) {
387                         sband->channels[i].orig_flags =
388                                 sband->channels[i].flags;
389                         sband->channels[i].orig_mag =
390                                 sband->channels[i].max_antenna_gain;
391                         sband->channels[i].orig_mpwr =
392                                 sband->channels[i].max_power;
393                         sband->channels[i].band = band;
394                 }
395
396                 have_band = true;
397         }
398
399         if (!have_band) {
400                 WARN_ON(1);
401                 return -EINVAL;
402         }
403
404         /* check and set up bitrates */
405         ieee80211_set_bitrate_flags(wiphy);
406
407         /* set up regulatory info */
408         mutex_lock(&cfg80211_reg_mutex);
409         wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
410         mutex_unlock(&cfg80211_reg_mutex);
411
412         mutex_lock(&cfg80211_drv_mutex);
413
414         res = device_add(&drv->wiphy.dev);
415         if (res)
416                 goto out_unlock;
417
418         list_add(&drv->list, &cfg80211_drv_list);
419
420         /* add to debugfs */
421         drv->wiphy.debugfsdir =
422                 debugfs_create_dir(wiphy_name(&drv->wiphy),
423                                    ieee80211_debugfs_dir);
424
425         res = 0;
426 out_unlock:
427         mutex_unlock(&cfg80211_drv_mutex);
428         return res;
429 }
430 EXPORT_SYMBOL(wiphy_register);
431
432 void wiphy_unregister(struct wiphy *wiphy)
433 {
434         struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
435
436         /* protect the device list */
437         mutex_lock(&cfg80211_drv_mutex);
438
439         BUG_ON(!list_empty(&drv->netdev_list));
440
441         /*
442          * Try to grab drv->mtx. If a command is still in progress,
443          * hopefully the driver will refuse it since it's tearing
444          * down the device already. We wait for this command to complete
445          * before unlinking the item from the list.
446          * Note: as codified by the BUG_ON above we cannot get here if
447          * a virtual interface is still associated. Hence, we can only
448          * get to lock contention here if userspace issues a command
449          * that identified the hardware by wiphy index.
450          */
451         mutex_lock(&drv->mtx);
452         /* unlock again before freeing */
453         mutex_unlock(&drv->mtx);
454
455         list_del(&drv->list);
456         device_del(&drv->wiphy.dev);
457         debugfs_remove(drv->wiphy.debugfsdir);
458
459         mutex_unlock(&cfg80211_drv_mutex);
460 }
461 EXPORT_SYMBOL(wiphy_unregister);
462
463 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
464 {
465         mutex_destroy(&drv->mtx);
466         mutex_destroy(&drv->devlist_mtx);
467         kfree(drv);
468 }
469
470 void wiphy_free(struct wiphy *wiphy)
471 {
472         put_device(&wiphy->dev);
473 }
474 EXPORT_SYMBOL(wiphy_free);
475
476 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
477                                          unsigned long state,
478                                          void *ndev)
479 {
480         struct net_device *dev = ndev;
481         struct cfg80211_registered_device *rdev;
482
483         if (!dev->ieee80211_ptr)
484                 return 0;
485
486         rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
487
488         switch (state) {
489         case NETDEV_REGISTER:
490                 mutex_lock(&rdev->devlist_mtx);
491                 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
492                 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
493                                       "phy80211")) {
494                         printk(KERN_ERR "wireless: failed to add phy80211 "
495                                 "symlink to netdev!\n");
496                 }
497                 dev->ieee80211_ptr->netdev = dev;
498                 mutex_unlock(&rdev->devlist_mtx);
499                 break;
500         case NETDEV_UNREGISTER:
501                 mutex_lock(&rdev->devlist_mtx);
502                 if (!list_empty(&dev->ieee80211_ptr->list)) {
503                         sysfs_remove_link(&dev->dev.kobj, "phy80211");
504                         list_del_init(&dev->ieee80211_ptr->list);
505                 }
506                 mutex_unlock(&rdev->devlist_mtx);
507                 break;
508         }
509
510         return 0;
511 }
512
513 static struct notifier_block cfg80211_netdev_notifier = {
514         .notifier_call = cfg80211_netdev_notifier_call,
515 };
516
517 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
518 const struct ieee80211_regdomain *static_regdom(char *alpha2)
519 {
520         if (alpha2[0] == 'U' && alpha2[1] == 'S')
521                 return &us_regdom;
522         if (alpha2[0] == 'J' && alpha2[1] == 'P')
523                 return &jp_regdom;
524         if (alpha2[0] == 'E' && alpha2[1] == 'U')
525                 return &eu_regdom;
526         /* Default, as per the old rules */
527         return &us_regdom;
528 }
529 #endif
530
531 static int cfg80211_init(void)
532 {
533         int err;
534
535 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
536         cfg80211_regdomain =
537                 (struct ieee80211_regdomain *) static_regdom(ieee80211_regdom);
538         /* Used during reset_regdomains_static() */
539         cfg80211_world_regdom = cfg80211_regdomain;
540 #else
541         cfg80211_regdomain =
542                 (struct ieee80211_regdomain *) cfg80211_world_regdom;
543 #endif
544
545         err = wiphy_sysfs_init();
546         if (err)
547                 goto out_fail_sysfs;
548
549         err = register_netdevice_notifier(&cfg80211_netdev_notifier);
550         if (err)
551                 goto out_fail_notifier;
552
553         err = nl80211_init();
554         if (err)
555                 goto out_fail_nl80211;
556
557         ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
558
559         err = regulatory_init();
560         if (err)
561                 goto out_fail_reg;
562
563 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
564         printk(KERN_INFO "cfg80211: Using old static regulatory domain:\n");
565         print_regdomain_info(cfg80211_regdomain);
566         /* The old code still requests for a new regdomain and if
567          * you have CRDA you get it updated, otherwise you get
568          * stuck with the static values. We ignore "EU" code as
569          * that is not a valid ISO / IEC 3166 alpha2 */
570         if (ieee80211_regdom[0] != 'E' &&
571                         ieee80211_regdom[1] != 'U')
572                 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
573                         ieee80211_regdom, NULL);
574 #else
575         err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
576         if (err)
577                 printk(KERN_ERR "cfg80211: calling CRDA failed - "
578                         "unable to update world regulatory domain, "
579                         "using static definition\n");
580 #endif
581
582         return 0;
583
584 out_fail_reg:
585         debugfs_remove(ieee80211_debugfs_dir);
586 out_fail_nl80211:
587         unregister_netdevice_notifier(&cfg80211_netdev_notifier);
588 out_fail_notifier:
589         wiphy_sysfs_exit();
590 out_fail_sysfs:
591         return err;
592 }
593
594 subsys_initcall(cfg80211_init);
595
596 static void cfg80211_exit(void)
597 {
598         debugfs_remove(ieee80211_debugfs_dir);
599         nl80211_exit();
600         unregister_netdevice_notifier(&cfg80211_netdev_notifier);
601         wiphy_sysfs_exit();
602         regulatory_exit();
603 }
604 module_exit(cfg80211_exit);