2 * This is the linux wireless configuration interface.
4 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.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>
25 /* name for sysfs, %d is appended */
26 #define PHY_NAME "phy"
28 MODULE_AUTHOR("Johannes Berg");
29 MODULE_LICENSE("GPL");
30 MODULE_DESCRIPTION("wireless configuration support");
32 struct list_head regulatory_requests;
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;
39 /* We keep a static world regulatory domain in case of the absence of CRDA */
40 const struct ieee80211_regdomain world_regdom = {
44 REG_RULE(2412-10, 2462+10, 40, 6, 20,
45 NL80211_RRF_PASSIVE_SCAN |
50 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
51 /* All this fucking static junk will be removed soon, so
52 * don't fucking count on it !@#$ */
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");
58 /* We assume 40 MHz bandwidth for the old regulatory work.
59 * We make emphasis we are using the exact same frequencies
62 const struct ieee80211_regdomain us_regdom = {
66 /* IEEE 802.11b/g, channels 1..11 */
67 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
68 /* IEEE 802.11a, channel 36 */
69 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
70 /* IEEE 802.11a, channel 40 */
71 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
72 /* IEEE 802.11a, channel 44 */
73 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
74 /* IEEE 802.11a, channels 48..64 */
75 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
76 /* IEEE 802.11a, channels 149..165, outdoor */
77 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
81 const struct ieee80211_regdomain jp_regdom = {
85 /* IEEE 802.11b/g, channels 1..14 */
86 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
87 /* IEEE 802.11a, channels 34..48 */
88 REG_RULE(5170-10, 5240+10, 40, 6, 20,
89 NL80211_RRF_PASSIVE_SCAN),
90 /* IEEE 802.11a, channels 52..64 */
91 REG_RULE(5260-10, 5320+10, 40, 6, 20,
97 const struct ieee80211_regdomain eu_regdom = {
99 /* This alpha2 is bogus, we leave it here just for stupid
100 * backward compatibility */
103 /* IEEE 802.11b/g, channels 1..13 */
104 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
105 /* IEEE 802.11a, channel 36 */
106 REG_RULE(5180-10, 5180+10, 40, 6, 23,
107 NL80211_RRF_PASSIVE_SCAN),
108 /* IEEE 802.11a, channel 40 */
109 REG_RULE(5200-10, 5200+10, 40, 6, 23,
110 NL80211_RRF_PASSIVE_SCAN),
111 /* IEEE 802.11a, channel 44 */
112 REG_RULE(5220-10, 5220+10, 40, 6, 23,
113 NL80211_RRF_PASSIVE_SCAN),
114 /* IEEE 802.11a, channels 48..64 */
115 REG_RULE(5240-10, 5320+10, 40, 6, 20,
116 NL80211_RRF_NO_IBSS |
118 /* IEEE 802.11a, channels 100..140 */
119 REG_RULE(5500-10, 5700+10, 40, 6, 30,
120 NL80211_RRF_NO_IBSS |
127 struct ieee80211_regdomain *cfg80211_world_regdom =
128 (struct ieee80211_regdomain *) &world_regdom;
130 LIST_HEAD(regulatory_requests);
131 DEFINE_MUTEX(cfg80211_reg_mutex);
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;
141 static struct dentry *ieee80211_debugfs_dir;
143 /* requires cfg80211_drv_mutex to be held! */
144 static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
146 struct cfg80211_registered_device *result = NULL, *drv;
148 list_for_each_entry(drv, &cfg80211_drv_list, list) {
149 if (drv->idx == wiphy) {
158 /* requires cfg80211_drv_mutex to be held! */
159 static struct cfg80211_registered_device *
160 __cfg80211_drv_from_info(struct genl_info *info)
163 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
164 struct net_device *dev;
167 if (info->attrs[NL80211_ATTR_WIPHY]) {
168 bywiphy = cfg80211_drv_by_wiphy(
169 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
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);
177 if (dev->ieee80211_ptr)
179 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
185 if (bywiphy && byifidx) {
186 if (bywiphy != byifidx)
187 return ERR_PTR(-EINVAL);
189 return bywiphy; /* == byifidx */
200 struct cfg80211_registered_device *
201 cfg80211_get_dev_from_info(struct genl_info *info)
203 struct cfg80211_registered_device *drv;
205 mutex_lock(&cfg80211_drv_mutex);
206 drv = __cfg80211_drv_from_info(info);
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 */
212 mutex_lock(&drv->mtx);
214 mutex_unlock(&cfg80211_drv_mutex);
219 struct cfg80211_registered_device *
220 cfg80211_get_dev_from_ifindex(int ifindex)
222 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
223 struct net_device *dev;
225 mutex_lock(&cfg80211_drv_mutex);
226 dev = dev_get_by_index(&init_net, ifindex);
229 if (dev->ieee80211_ptr) {
230 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
231 mutex_lock(&drv->mtx);
233 drv = ERR_PTR(-ENODEV);
236 mutex_unlock(&cfg80211_drv_mutex);
240 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
243 mutex_unlock(&drv->mtx);
246 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
249 struct cfg80211_registered_device *drv;
250 int idx, taken = -1, result, digits;
252 mutex_lock(&cfg80211_drv_mutex);
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 */
262 * deny the name if it is phy<idx> where <idx> is printed
263 * without leading zeroes. taken == strlen(newname) here
266 if (taken == strlen(PHY_NAME) + digits)
271 /* Ignore nop renames */
273 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
276 /* Ensure another device does not already have this name. */
277 list_for_each_entry(drv, &cfg80211_drv_list, list) {
279 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
283 /* this will only check for collisions in sysfs
284 * which is not even always compiled in.
286 result = device_rename(&rdev->wiphy.dev, newname);
290 if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
291 rdev->wiphy.debugfsdir,
292 rdev->wiphy.debugfsdir->d_parent,
294 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
299 mutex_unlock(&cfg80211_drv_mutex);
301 nl80211_notify_dev_rename(rdev);
306 /* exported functions */
308 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
310 struct cfg80211_registered_device *drv;
313 WARN_ON(!ops->add_key && ops->del_key);
314 WARN_ON(ops->add_key && !ops->del_key);
316 alloc_size = sizeof(*drv) + sizeof_priv;
318 drv = kzalloc(alloc_size, GFP_KERNEL);
324 mutex_lock(&cfg80211_drv_mutex);
326 drv->idx = wiphy_counter;
328 /* now increase counter for the next device unless
329 * it has wrapped previously */
330 if (wiphy_counter >= 0)
333 mutex_unlock(&cfg80211_drv_mutex);
335 if (unlikely(drv->idx < 0)) {
341 /* give it a proper name */
342 snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
343 PHY_NAME "%d", drv->idx);
345 mutex_init(&drv->mtx);
346 mutex_init(&drv->devlist_mtx);
347 INIT_LIST_HEAD(&drv->netdev_list);
349 device_initialize(&drv->wiphy.dev);
350 drv->wiphy.dev.class = &ieee80211_class;
351 drv->wiphy.dev.platform_data = drv;
355 EXPORT_SYMBOL(wiphy_new);
357 int wiphy_register(struct wiphy *wiphy)
359 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
361 enum ieee80211_band band;
362 struct ieee80211_supported_band *sband;
363 bool have_band = false;
365 u16 ifmodes = wiphy->interface_modes;
367 /* sanity check ifmodes */
369 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
370 if (WARN_ON(ifmodes != wiphy->interface_modes))
371 wiphy->interface_modes = ifmodes;
373 /* sanity check supported bands/channels */
374 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
375 sband = wiphy->bands[band];
381 if (!sband->n_channels || !sband->n_bitrates) {
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;
404 /* check and set up bitrates */
405 ieee80211_set_bitrate_flags(wiphy);
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);
412 mutex_lock(&cfg80211_drv_mutex);
414 res = device_add(&drv->wiphy.dev);
418 list_add(&drv->list, &cfg80211_drv_list);
421 drv->wiphy.debugfsdir =
422 debugfs_create_dir(wiphy_name(&drv->wiphy),
423 ieee80211_debugfs_dir);
427 mutex_unlock(&cfg80211_drv_mutex);
430 EXPORT_SYMBOL(wiphy_register);
432 void wiphy_unregister(struct wiphy *wiphy)
434 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
436 /* protect the device list */
437 mutex_lock(&cfg80211_drv_mutex);
439 BUG_ON(!list_empty(&drv->netdev_list));
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.
451 mutex_lock(&drv->mtx);
452 /* unlock again before freeing */
453 mutex_unlock(&drv->mtx);
455 list_del(&drv->list);
456 device_del(&drv->wiphy.dev);
457 debugfs_remove(drv->wiphy.debugfsdir);
459 mutex_unlock(&cfg80211_drv_mutex);
461 EXPORT_SYMBOL(wiphy_unregister);
463 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
465 mutex_destroy(&drv->mtx);
466 mutex_destroy(&drv->devlist_mtx);
470 void wiphy_free(struct wiphy *wiphy)
472 put_device(&wiphy->dev);
474 EXPORT_SYMBOL(wiphy_free);
476 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
480 struct net_device *dev = ndev;
481 struct cfg80211_registered_device *rdev;
483 if (!dev->ieee80211_ptr)
486 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
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,
494 printk(KERN_ERR "wireless: failed to add phy80211 "
495 "symlink to netdev!\n");
497 dev->ieee80211_ptr->netdev = dev;
498 mutex_unlock(&rdev->devlist_mtx);
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);
506 mutex_unlock(&rdev->devlist_mtx);
513 static struct notifier_block cfg80211_netdev_notifier = {
514 .notifier_call = cfg80211_netdev_notifier_call,
517 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
518 const struct ieee80211_regdomain *static_regdom(char *alpha2)
520 if (alpha2[0] == 'U' && alpha2[1] == 'S')
522 if (alpha2[0] == 'J' && alpha2[1] == 'P')
524 if (alpha2[0] == 'E' && alpha2[1] == 'U')
526 /* Default, as per the old rules */
531 static int cfg80211_init(void)
535 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
537 (struct ieee80211_regdomain *) static_regdom(ieee80211_regdom);
538 /* Used during reset_regdomains_static() */
539 cfg80211_world_regdom = cfg80211_regdomain;
542 (struct ieee80211_regdomain *) cfg80211_world_regdom;
545 err = wiphy_sysfs_init();
549 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
551 goto out_fail_notifier;
553 err = nl80211_init();
555 goto out_fail_nl80211;
557 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
559 err = regulatory_init();
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);
575 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
577 printk(KERN_ERR "cfg80211: calling CRDA failed - "
578 "unable to update world regulatory domain, "
579 "using static definition\n");
585 debugfs_remove(ieee80211_debugfs_dir);
587 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
594 subsys_initcall(cfg80211_init);
596 static void cfg80211_exit(void)
598 debugfs_remove(ieee80211_debugfs_dir);
600 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
604 module_exit(cfg80211_exit);