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 <net/genetlink.h>
17 #include <net/cfg80211.h>
18 #include <net/wireless.h>
23 /* name for sysfs, %d is appended */
24 #define PHY_NAME "phy"
26 MODULE_AUTHOR("Johannes Berg");
27 MODULE_LICENSE("GPL");
28 MODULE_DESCRIPTION("wireless configuration support");
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);
37 static struct dentry *ieee80211_debugfs_dir;
39 /* requires cfg80211_drv_mutex to be held! */
40 static struct cfg80211_registered_device *
41 cfg80211_drv_by_wiphy_idx(int wiphy_idx)
43 struct cfg80211_registered_device *result = NULL, *drv;
45 if (!wiphy_idx_valid(wiphy_idx))
48 list_for_each_entry(drv, &cfg80211_drv_list, list) {
49 if (drv->wiphy_idx == wiphy_idx) {
58 /* requires cfg80211_drv_mutex to be held! */
59 static struct cfg80211_registered_device *
60 __cfg80211_drv_from_info(struct genl_info *info)
63 struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
64 struct net_device *dev;
67 if (info->attrs[NL80211_ATTR_WIPHY]) {
68 bywiphyidx = cfg80211_drv_by_wiphy_idx(
69 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
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);
77 if (dev->ieee80211_ptr)
79 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
85 if (bywiphyidx && byifidx) {
86 if (bywiphyidx != byifidx)
87 return ERR_PTR(-EINVAL);
89 return bywiphyidx; /* == byifidx */
100 struct cfg80211_registered_device *
101 cfg80211_get_dev_from_info(struct genl_info *info)
103 struct cfg80211_registered_device *drv;
105 mutex_lock(&cfg80211_drv_mutex);
106 drv = __cfg80211_drv_from_info(info);
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 */
112 mutex_lock(&drv->mtx);
114 mutex_unlock(&cfg80211_drv_mutex);
119 struct cfg80211_registered_device *
120 cfg80211_get_dev_from_ifindex(int ifindex)
122 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
123 struct net_device *dev;
125 mutex_lock(&cfg80211_drv_mutex);
126 dev = dev_get_by_index(&init_net, ifindex);
129 if (dev->ieee80211_ptr) {
130 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
131 mutex_lock(&drv->mtx);
133 drv = ERR_PTR(-ENODEV);
136 mutex_unlock(&cfg80211_drv_mutex);
140 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
143 mutex_unlock(&drv->mtx);
146 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
149 struct cfg80211_registered_device *drv;
150 int wiphy_idx, taken = -1, result, digits;
152 mutex_lock(&cfg80211_drv_mutex);
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 */
159 while (wiphy_idx /= 10)
162 * deny the name if it is phy<idx> where <idx> is printed
163 * without leading zeroes. taken == strlen(newname) here
166 if (taken == strlen(PHY_NAME) + digits)
171 /* Ignore nop renames */
173 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
176 /* Ensure another device does not already have this name. */
177 list_for_each_entry(drv, &cfg80211_drv_list, list) {
179 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
183 /* this will only check for collisions in sysfs
184 * which is not even always compiled in.
186 result = device_rename(&rdev->wiphy.dev, newname);
190 if (rdev->wiphy.debugfsdir &&
191 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
192 rdev->wiphy.debugfsdir,
193 rdev->wiphy.debugfsdir->d_parent,
195 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
200 mutex_unlock(&cfg80211_drv_mutex);
202 nl80211_notify_dev_rename(rdev);
207 /* exported functions */
209 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
211 static int wiphy_counter;
213 struct cfg80211_registered_device *drv;
216 WARN_ON(!ops->add_key && ops->del_key);
217 WARN_ON(ops->add_key && !ops->del_key);
219 alloc_size = sizeof(*drv) + sizeof_priv;
221 drv = kzalloc(alloc_size, GFP_KERNEL);
227 mutex_lock(&cfg80211_drv_mutex);
229 drv->wiphy_idx = wiphy_counter++;
231 if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
233 mutex_unlock(&cfg80211_drv_mutex);
239 mutex_unlock(&cfg80211_drv_mutex);
241 /* give it a proper name */
242 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
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);
250 device_initialize(&drv->wiphy.dev);
251 drv->wiphy.dev.class = &ieee80211_class;
252 drv->wiphy.dev.platform_data = drv;
256 EXPORT_SYMBOL(wiphy_new);
258 int wiphy_register(struct wiphy *wiphy)
260 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
262 enum ieee80211_band band;
263 struct ieee80211_supported_band *sband;
264 bool have_band = false;
266 u16 ifmodes = wiphy->interface_modes;
268 if (WARN_ON(wiphy->max_scan_ssids < 1))
271 /* sanity check ifmodes */
273 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
274 if (WARN_ON(ifmodes != wiphy->interface_modes))
275 wiphy->interface_modes = ifmodes;
277 /* sanity check supported bands/channels */
278 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
279 sband = wiphy->bands[band];
285 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
289 * Since we use a u32 for rate bitmaps in
290 * ieee80211_get_response_rate, we cannot
291 * have more than 32 legacy rates.
293 if (WARN_ON(sband->n_bitrates > 32))
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;
314 /* check and set up bitrates */
315 ieee80211_set_bitrate_flags(wiphy);
317 mutex_lock(&cfg80211_drv_mutex);
319 /* set up regulatory info */
320 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
322 res = device_add(&drv->wiphy.dev);
326 list_add(&drv->list, &cfg80211_drv_list);
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;
337 mutex_unlock(&cfg80211_drv_mutex);
340 EXPORT_SYMBOL(wiphy_register);
342 void wiphy_unregister(struct wiphy *wiphy)
344 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
346 /* protect the device list */
347 mutex_lock(&cfg80211_drv_mutex);
349 BUG_ON(!list_empty(&drv->netdev_list));
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.
361 mutex_lock(&drv->mtx);
362 /* unlock again before freeing */
363 mutex_unlock(&drv->mtx);
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);
369 list_del(&drv->list);
370 device_del(&drv->wiphy.dev);
371 debugfs_remove(drv->wiphy.debugfsdir);
373 mutex_unlock(&cfg80211_drv_mutex);
375 EXPORT_SYMBOL(wiphy_unregister);
377 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
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);
387 void wiphy_free(struct wiphy *wiphy)
389 put_device(&wiphy->dev);
391 EXPORT_SYMBOL(wiphy_free);
393 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
397 struct net_device *dev = ndev;
398 struct cfg80211_registered_device *rdev;
400 if (!dev->ieee80211_ptr)
403 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
405 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
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,
413 printk(KERN_ERR "wireless: failed to add phy80211 "
414 "symlink to netdev!\n");
416 dev->ieee80211_ptr->netdev = dev;
417 mutex_unlock(&rdev->devlist_mtx);
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);
425 mutex_unlock(&rdev->devlist_mtx);
432 static struct notifier_block cfg80211_netdev_notifier = {
433 .notifier_call = cfg80211_netdev_notifier_call,
436 static int cfg80211_init(void)
440 err = wiphy_sysfs_init();
444 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
446 goto out_fail_notifier;
448 err = nl80211_init();
450 goto out_fail_nl80211;
452 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
454 err = regulatory_init();
461 debugfs_remove(ieee80211_debugfs_dir);
463 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
470 subsys_initcall(cfg80211_init);
472 static void cfg80211_exit(void)
474 debugfs_remove(ieee80211_debugfs_dir);
476 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
480 module_exit(cfg80211_exit);