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 *cfg80211_drv_by_wiphy(int wiphy)
42 struct cfg80211_registered_device *result = NULL, *drv;
44 list_for_each_entry(drv, &cfg80211_drv_list, list) {
45 if (drv->idx == wiphy) {
54 /* requires cfg80211_drv_mutex to be held! */
55 static struct cfg80211_registered_device *
56 __cfg80211_drv_from_info(struct genl_info *info)
59 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
60 struct net_device *dev;
63 if (info->attrs[NL80211_ATTR_WIPHY]) {
64 bywiphy = cfg80211_drv_by_wiphy(
65 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
69 if (info->attrs[NL80211_ATTR_IFINDEX]) {
70 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
71 dev = dev_get_by_index(&init_net, ifindex);
73 if (dev->ieee80211_ptr)
75 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
81 if (bywiphy && byifidx) {
82 if (bywiphy != byifidx)
83 return ERR_PTR(-EINVAL);
85 return bywiphy; /* == byifidx */
96 struct cfg80211_registered_device *
97 cfg80211_get_dev_from_info(struct genl_info *info)
99 struct cfg80211_registered_device *drv;
101 mutex_lock(&cfg80211_drv_mutex);
102 drv = __cfg80211_drv_from_info(info);
104 /* if it is not an error we grab the lock on
105 * it to assure it won't be going away while
106 * we operate on it */
108 mutex_lock(&drv->mtx);
110 mutex_unlock(&cfg80211_drv_mutex);
115 struct cfg80211_registered_device *
116 cfg80211_get_dev_from_ifindex(int ifindex)
118 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
119 struct net_device *dev;
121 mutex_lock(&cfg80211_drv_mutex);
122 dev = dev_get_by_index(&init_net, ifindex);
125 if (dev->ieee80211_ptr) {
126 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
127 mutex_lock(&drv->mtx);
129 drv = ERR_PTR(-ENODEV);
132 mutex_unlock(&cfg80211_drv_mutex);
136 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
139 mutex_unlock(&drv->mtx);
142 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
145 struct cfg80211_registered_device *drv;
146 int idx, taken = -1, result, digits;
148 mutex_lock(&cfg80211_drv_mutex);
150 /* prohibit calling the thing phy%d when %d is not its number */
151 sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
152 if (taken == strlen(newname) && idx != rdev->idx) {
153 /* count number of places needed to print idx */
158 * deny the name if it is phy<idx> where <idx> is printed
159 * without leading zeroes. taken == strlen(newname) here
162 if (taken == strlen(PHY_NAME) + digits)
167 /* Ignore nop renames */
169 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
172 /* Ensure another device does not already have this name. */
173 list_for_each_entry(drv, &cfg80211_drv_list, list) {
175 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
179 /* this will only check for collisions in sysfs
180 * which is not even always compiled in.
182 result = device_rename(&rdev->wiphy.dev, newname);
186 if (rdev->wiphy.debugfsdir &&
187 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
188 rdev->wiphy.debugfsdir,
189 rdev->wiphy.debugfsdir->d_parent,
191 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
196 mutex_unlock(&cfg80211_drv_mutex);
198 nl80211_notify_dev_rename(rdev);
203 /* exported functions */
205 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
207 static int wiphy_counter;
209 struct cfg80211_registered_device *drv;
212 WARN_ON(!ops->add_key && ops->del_key);
213 WARN_ON(ops->add_key && !ops->del_key);
215 alloc_size = sizeof(*drv) + sizeof_priv;
217 drv = kzalloc(alloc_size, GFP_KERNEL);
223 mutex_lock(&cfg80211_drv_mutex);
225 drv->idx = wiphy_counter++;
227 if (unlikely(drv->idx < 0)) {
229 mutex_unlock(&cfg80211_drv_mutex);
235 mutex_unlock(&cfg80211_drv_mutex);
237 /* give it a proper name */
238 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->idx);
240 mutex_init(&drv->mtx);
241 mutex_init(&drv->devlist_mtx);
242 INIT_LIST_HEAD(&drv->netdev_list);
243 spin_lock_init(&drv->bss_lock);
244 INIT_LIST_HEAD(&drv->bss_list);
246 device_initialize(&drv->wiphy.dev);
247 drv->wiphy.dev.class = &ieee80211_class;
248 drv->wiphy.dev.platform_data = drv;
252 EXPORT_SYMBOL(wiphy_new);
254 int wiphy_register(struct wiphy *wiphy)
256 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
258 enum ieee80211_band band;
259 struct ieee80211_supported_band *sband;
260 bool have_band = false;
262 u16 ifmodes = wiphy->interface_modes;
264 if (WARN_ON(wiphy->max_scan_ssids < 1))
267 /* sanity check ifmodes */
269 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
270 if (WARN_ON(ifmodes != wiphy->interface_modes))
271 wiphy->interface_modes = ifmodes;
273 /* sanity check supported bands/channels */
274 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
275 sband = wiphy->bands[band];
281 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
285 * Since we use a u32 for rate bitmaps in
286 * ieee80211_get_response_rate, we cannot
287 * have more than 32 legacy rates.
289 if (WARN_ON(sband->n_bitrates > 32))
292 for (i = 0; i < sband->n_channels; i++) {
293 sband->channels[i].orig_flags =
294 sband->channels[i].flags;
295 sband->channels[i].orig_mag =
296 sband->channels[i].max_antenna_gain;
297 sband->channels[i].orig_mpwr =
298 sband->channels[i].max_power;
299 sband->channels[i].band = band;
310 /* check and set up bitrates */
311 ieee80211_set_bitrate_flags(wiphy);
313 mutex_lock(&cfg80211_drv_mutex);
315 /* set up regulatory info */
316 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
318 res = device_add(&drv->wiphy.dev);
322 list_add(&drv->list, &cfg80211_drv_list);
325 drv->wiphy.debugfsdir =
326 debugfs_create_dir(wiphy_name(&drv->wiphy),
327 ieee80211_debugfs_dir);
328 if (IS_ERR(drv->wiphy.debugfsdir))
329 drv->wiphy.debugfsdir = NULL;
333 mutex_unlock(&cfg80211_drv_mutex);
336 EXPORT_SYMBOL(wiphy_register);
338 void wiphy_unregister(struct wiphy *wiphy)
340 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
342 /* protect the device list */
343 mutex_lock(&cfg80211_drv_mutex);
345 BUG_ON(!list_empty(&drv->netdev_list));
348 * Try to grab drv->mtx. If a command is still in progress,
349 * hopefully the driver will refuse it since it's tearing
350 * down the device already. We wait for this command to complete
351 * before unlinking the item from the list.
352 * Note: as codified by the BUG_ON above we cannot get here if
353 * a virtual interface is still associated. Hence, we can only
354 * get to lock contention here if userspace issues a command
355 * that identified the hardware by wiphy index.
357 mutex_lock(&drv->mtx);
358 /* unlock again before freeing */
359 mutex_unlock(&drv->mtx);
361 /* If this device got a regulatory hint tell core its
362 * free to listen now to a new shiny device regulatory hint */
363 reg_device_remove(wiphy);
365 list_del(&drv->list);
366 device_del(&drv->wiphy.dev);
367 debugfs_remove(drv->wiphy.debugfsdir);
369 mutex_unlock(&cfg80211_drv_mutex);
371 EXPORT_SYMBOL(wiphy_unregister);
373 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
375 struct cfg80211_internal_bss *scan, *tmp;
376 mutex_destroy(&drv->mtx);
377 mutex_destroy(&drv->devlist_mtx);
378 list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
379 cfg80211_put_bss(&scan->pub);
383 void wiphy_free(struct wiphy *wiphy)
385 put_device(&wiphy->dev);
387 EXPORT_SYMBOL(wiphy_free);
389 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
393 struct net_device *dev = ndev;
394 struct cfg80211_registered_device *rdev;
396 if (!dev->ieee80211_ptr)
399 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
401 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
404 case NETDEV_REGISTER:
405 mutex_lock(&rdev->devlist_mtx);
406 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
407 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
409 printk(KERN_ERR "wireless: failed to add phy80211 "
410 "symlink to netdev!\n");
412 dev->ieee80211_ptr->netdev = dev;
413 mutex_unlock(&rdev->devlist_mtx);
415 case NETDEV_UNREGISTER:
416 mutex_lock(&rdev->devlist_mtx);
417 if (!list_empty(&dev->ieee80211_ptr->list)) {
418 sysfs_remove_link(&dev->dev.kobj, "phy80211");
419 list_del_init(&dev->ieee80211_ptr->list);
421 mutex_unlock(&rdev->devlist_mtx);
428 static struct notifier_block cfg80211_netdev_notifier = {
429 .notifier_call = cfg80211_netdev_notifier_call,
432 static int cfg80211_init(void)
436 err = wiphy_sysfs_init();
440 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
442 goto out_fail_notifier;
444 err = nl80211_init();
446 goto out_fail_nl80211;
448 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
450 err = regulatory_init();
457 debugfs_remove(ieee80211_debugfs_dir);
459 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
466 subsys_initcall(cfg80211_init);
468 static void cfg80211_exit(void)
470 debugfs_remove(ieee80211_debugfs_dir);
472 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
476 module_exit(cfg80211_exit);