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/list.h>
11 #include <linux/nl80211.h>
12 #include <linux/debugfs.h>
13 #include <linux/notifier.h>
14 #include <linux/device.h>
15 #include <net/genetlink.h>
16 #include <net/cfg80211.h>
17 #include <net/wireless.h>
22 /* name for sysfs, %d is appended */
23 #define PHY_NAME "phy"
25 MODULE_AUTHOR("Johannes Berg");
26 MODULE_LICENSE("GPL");
27 MODULE_DESCRIPTION("wireless configuration support");
29 /* RCU might be appropriate here since we usually
30 * only read the list, and that can happen quite
31 * often because we need to do it for each command */
32 LIST_HEAD(cfg80211_drv_list);
35 * This is used to protect the cfg80211_drv_list, cfg80211_regdomain,
36 * country_ie_regdomain, the reg_beacon_list and the the last regulatory
37 * request receipt (last_request).
39 DEFINE_MUTEX(cfg80211_mutex);
42 static struct dentry *ieee80211_debugfs_dir;
44 /* requires cfg80211_mutex to be held! */
45 struct cfg80211_registered_device *cfg80211_drv_by_wiphy_idx(int wiphy_idx)
47 struct cfg80211_registered_device *result = NULL, *drv;
49 if (!wiphy_idx_valid(wiphy_idx))
52 assert_cfg80211_lock();
54 list_for_each_entry(drv, &cfg80211_drv_list, list) {
55 if (drv->wiphy_idx == wiphy_idx) {
64 int get_wiphy_idx(struct wiphy *wiphy)
66 struct cfg80211_registered_device *drv;
68 return WIPHY_IDX_STALE;
69 drv = wiphy_to_dev(wiphy);
70 return drv->wiphy_idx;
73 /* requires cfg80211_drv_mutex to be held! */
74 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
76 struct cfg80211_registered_device *drv;
78 if (!wiphy_idx_valid(wiphy_idx))
81 assert_cfg80211_lock();
83 drv = cfg80211_drv_by_wiphy_idx(wiphy_idx);
89 /* requires cfg80211_mutex to be held! */
90 static struct cfg80211_registered_device *
91 __cfg80211_drv_from_info(struct genl_info *info)
94 struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
95 struct net_device *dev;
98 assert_cfg80211_lock();
100 if (info->attrs[NL80211_ATTR_WIPHY]) {
101 bywiphyidx = cfg80211_drv_by_wiphy_idx(
102 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
106 if (info->attrs[NL80211_ATTR_IFINDEX]) {
107 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
108 dev = dev_get_by_index(&init_net, ifindex);
110 if (dev->ieee80211_ptr)
112 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
118 if (bywiphyidx && byifidx) {
119 if (bywiphyidx != byifidx)
120 return ERR_PTR(-EINVAL);
122 return bywiphyidx; /* == byifidx */
133 struct cfg80211_registered_device *
134 cfg80211_get_dev_from_info(struct genl_info *info)
136 struct cfg80211_registered_device *drv;
138 mutex_lock(&cfg80211_mutex);
139 drv = __cfg80211_drv_from_info(info);
141 /* if it is not an error we grab the lock on
142 * it to assure it won't be going away while
143 * we operate on it */
145 mutex_lock(&drv->mtx);
147 mutex_unlock(&cfg80211_mutex);
152 struct cfg80211_registered_device *
153 cfg80211_get_dev_from_ifindex(int ifindex)
155 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
156 struct net_device *dev;
158 mutex_lock(&cfg80211_mutex);
159 dev = dev_get_by_index(&init_net, ifindex);
162 if (dev->ieee80211_ptr) {
163 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
164 mutex_lock(&drv->mtx);
166 drv = ERR_PTR(-ENODEV);
169 mutex_unlock(&cfg80211_mutex);
173 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
176 mutex_unlock(&drv->mtx);
179 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
182 struct cfg80211_registered_device *drv;
183 int wiphy_idx, taken = -1, result, digits;
185 mutex_lock(&cfg80211_mutex);
187 /* prohibit calling the thing phy%d when %d is not its number */
188 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
189 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
190 /* count number of places needed to print wiphy_idx */
192 while (wiphy_idx /= 10)
195 * deny the name if it is phy<idx> where <idx> is printed
196 * without leading zeroes. taken == strlen(newname) here
199 if (taken == strlen(PHY_NAME) + digits)
204 /* Ignore nop renames */
206 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
209 /* Ensure another device does not already have this name. */
210 list_for_each_entry(drv, &cfg80211_drv_list, list) {
212 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
216 /* this will only check for collisions in sysfs
217 * which is not even always compiled in.
219 result = device_rename(&rdev->wiphy.dev, newname);
223 if (rdev->wiphy.debugfsdir &&
224 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
225 rdev->wiphy.debugfsdir,
226 rdev->wiphy.debugfsdir->d_parent,
228 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
233 mutex_unlock(&cfg80211_mutex);
235 nl80211_notify_dev_rename(rdev);
240 /* exported functions */
242 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
244 static int wiphy_counter;
246 struct cfg80211_registered_device *drv;
249 WARN_ON(!ops->add_key && ops->del_key);
250 WARN_ON(ops->add_key && !ops->del_key);
252 alloc_size = sizeof(*drv) + sizeof_priv;
254 drv = kzalloc(alloc_size, GFP_KERNEL);
260 mutex_lock(&cfg80211_mutex);
262 drv->wiphy_idx = wiphy_counter++;
264 if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
266 mutex_unlock(&cfg80211_mutex);
272 mutex_unlock(&cfg80211_mutex);
274 /* give it a proper name */
275 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
277 mutex_init(&drv->mtx);
278 mutex_init(&drv->devlist_mtx);
279 INIT_LIST_HEAD(&drv->netdev_list);
280 spin_lock_init(&drv->bss_lock);
281 INIT_LIST_HEAD(&drv->bss_list);
283 device_initialize(&drv->wiphy.dev);
284 drv->wiphy.dev.class = &ieee80211_class;
285 drv->wiphy.dev.platform_data = drv;
289 EXPORT_SYMBOL(wiphy_new);
291 int wiphy_register(struct wiphy *wiphy)
293 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
295 enum ieee80211_band band;
296 struct ieee80211_supported_band *sband;
297 bool have_band = false;
299 u16 ifmodes = wiphy->interface_modes;
301 if (WARN_ON(wiphy->max_scan_ssids < 1))
304 /* sanity check ifmodes */
306 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
307 if (WARN_ON(ifmodes != wiphy->interface_modes))
308 wiphy->interface_modes = ifmodes;
310 /* sanity check supported bands/channels */
311 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
312 sband = wiphy->bands[band];
318 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
322 * Since we use a u32 for rate bitmaps in
323 * ieee80211_get_response_rate, we cannot
324 * have more than 32 legacy rates.
326 if (WARN_ON(sband->n_bitrates > 32))
329 for (i = 0; i < sband->n_channels; i++) {
330 sband->channels[i].orig_flags =
331 sband->channels[i].flags;
332 sband->channels[i].orig_mag =
333 sband->channels[i].max_antenna_gain;
334 sband->channels[i].orig_mpwr =
335 sband->channels[i].max_power;
336 sband->channels[i].band = band;
347 /* check and set up bitrates */
348 ieee80211_set_bitrate_flags(wiphy);
350 mutex_lock(&cfg80211_mutex);
352 /* set up regulatory info */
353 wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
355 res = device_add(&drv->wiphy.dev);
359 list_add(&drv->list, &cfg80211_drv_list);
362 drv->wiphy.debugfsdir =
363 debugfs_create_dir(wiphy_name(&drv->wiphy),
364 ieee80211_debugfs_dir);
365 if (IS_ERR(drv->wiphy.debugfsdir))
366 drv->wiphy.debugfsdir = NULL;
368 if (wiphy->custom_regulatory) {
369 struct regulatory_request request;
371 request.wiphy_idx = get_wiphy_idx(wiphy);
372 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
373 request.alpha2[0] = '9';
374 request.alpha2[1] = '9';
376 nl80211_send_reg_change_event(&request);
381 mutex_unlock(&cfg80211_mutex);
384 EXPORT_SYMBOL(wiphy_register);
386 void wiphy_unregister(struct wiphy *wiphy)
388 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
390 /* protect the device list */
391 mutex_lock(&cfg80211_mutex);
393 BUG_ON(!list_empty(&drv->netdev_list));
396 * Try to grab drv->mtx. If a command is still in progress,
397 * hopefully the driver will refuse it since it's tearing
398 * down the device already. We wait for this command to complete
399 * before unlinking the item from the list.
400 * Note: as codified by the BUG_ON above we cannot get here if
401 * a virtual interface is still associated. Hence, we can only
402 * get to lock contention here if userspace issues a command
403 * that identified the hardware by wiphy index.
405 mutex_lock(&drv->mtx);
406 /* unlock again before freeing */
407 mutex_unlock(&drv->mtx);
409 /* If this device got a regulatory hint tell core its
410 * free to listen now to a new shiny device regulatory hint */
411 reg_device_remove(wiphy);
413 list_del(&drv->list);
414 device_del(&drv->wiphy.dev);
415 debugfs_remove(drv->wiphy.debugfsdir);
417 mutex_unlock(&cfg80211_mutex);
419 EXPORT_SYMBOL(wiphy_unregister);
421 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
423 struct cfg80211_internal_bss *scan, *tmp;
424 mutex_destroy(&drv->mtx);
425 mutex_destroy(&drv->devlist_mtx);
426 list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
427 cfg80211_put_bss(&scan->pub);
431 void wiphy_free(struct wiphy *wiphy)
433 put_device(&wiphy->dev);
435 EXPORT_SYMBOL(wiphy_free);
437 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
441 struct net_device *dev = ndev;
442 struct cfg80211_registered_device *rdev;
444 if (!dev->ieee80211_ptr)
447 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
449 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
452 case NETDEV_REGISTER:
453 mutex_lock(&rdev->devlist_mtx);
454 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
455 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
457 printk(KERN_ERR "wireless: failed to add phy80211 "
458 "symlink to netdev!\n");
460 dev->ieee80211_ptr->netdev = dev;
461 mutex_unlock(&rdev->devlist_mtx);
463 case NETDEV_UNREGISTER:
464 mutex_lock(&rdev->devlist_mtx);
465 if (!list_empty(&dev->ieee80211_ptr->list)) {
466 sysfs_remove_link(&dev->dev.kobj, "phy80211");
467 list_del_init(&dev->ieee80211_ptr->list);
469 mutex_unlock(&rdev->devlist_mtx);
476 static struct notifier_block cfg80211_netdev_notifier = {
477 .notifier_call = cfg80211_netdev_notifier_call,
480 static int cfg80211_init(void)
484 err = wiphy_sysfs_init();
488 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
490 goto out_fail_notifier;
492 err = nl80211_init();
494 goto out_fail_nl80211;
496 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
498 err = regulatory_init();
505 debugfs_remove(ieee80211_debugfs_dir);
507 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
514 subsys_initcall(cfg80211_init);
516 static void cfg80211_exit(void)
518 debugfs_remove(ieee80211_debugfs_dir);
520 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
524 module_exit(cfg80211_exit);