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 /* RCU might be appropriate here since we usually
33 * only read the list, and that can happen quite
34 * often because we need to do it for each command */
35 LIST_HEAD(cfg80211_drv_list);
36 DEFINE_MUTEX(cfg80211_drv_mutex);
37 static int wiphy_counter;
40 static struct dentry *ieee80211_debugfs_dir;
42 /* requires cfg80211_drv_mutex to be held! */
43 static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
45 struct cfg80211_registered_device *result = NULL, *drv;
47 list_for_each_entry(drv, &cfg80211_drv_list, list) {
48 if (drv->idx == wiphy) {
57 /* requires cfg80211_drv_mutex to be held! */
58 static struct cfg80211_registered_device *
59 __cfg80211_drv_from_info(struct genl_info *info)
62 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
63 struct net_device *dev;
66 if (info->attrs[NL80211_ATTR_WIPHY]) {
67 bywiphy = cfg80211_drv_by_wiphy(
68 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
72 if (info->attrs[NL80211_ATTR_IFINDEX]) {
73 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
74 dev = dev_get_by_index(&init_net, ifindex);
76 if (dev->ieee80211_ptr)
78 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
84 if (bywiphy && byifidx) {
85 if (bywiphy != byifidx)
86 return ERR_PTR(-EINVAL);
88 return bywiphy; /* == byifidx */
99 struct cfg80211_registered_device *
100 cfg80211_get_dev_from_info(struct genl_info *info)
102 struct cfg80211_registered_device *drv;
104 mutex_lock(&cfg80211_drv_mutex);
105 drv = __cfg80211_drv_from_info(info);
107 /* if it is not an error we grab the lock on
108 * it to assure it won't be going away while
109 * we operate on it */
111 mutex_lock(&drv->mtx);
113 mutex_unlock(&cfg80211_drv_mutex);
118 struct cfg80211_registered_device *
119 cfg80211_get_dev_from_ifindex(int ifindex)
121 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
122 struct net_device *dev;
124 mutex_lock(&cfg80211_drv_mutex);
125 dev = dev_get_by_index(&init_net, ifindex);
128 if (dev->ieee80211_ptr) {
129 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
130 mutex_lock(&drv->mtx);
132 drv = ERR_PTR(-ENODEV);
135 mutex_unlock(&cfg80211_drv_mutex);
139 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
142 mutex_unlock(&drv->mtx);
145 int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
148 struct cfg80211_registered_device *drv;
149 int idx, taken = -1, result, digits;
151 mutex_lock(&cfg80211_drv_mutex);
153 /* prohibit calling the thing phy%d when %d is not its number */
154 sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
155 if (taken == strlen(newname) && idx != rdev->idx) {
156 /* count number of places needed to print idx */
161 * deny the name if it is phy<idx> where <idx> is printed
162 * without leading zeroes. taken == strlen(newname) here
165 if (taken == strlen(PHY_NAME) + digits)
170 /* Ignore nop renames */
172 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
175 /* Ensure another device does not already have this name. */
176 list_for_each_entry(drv, &cfg80211_drv_list, list) {
178 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
182 /* this will only check for collisions in sysfs
183 * which is not even always compiled in.
185 result = device_rename(&rdev->wiphy.dev, newname);
189 if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
190 rdev->wiphy.debugfsdir,
191 rdev->wiphy.debugfsdir->d_parent,
193 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
198 mutex_unlock(&cfg80211_drv_mutex);
200 nl80211_notify_dev_rename(rdev);
205 /* exported functions */
207 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
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 /* now increase counter for the next device unless
228 * it has wrapped previously */
229 if (wiphy_counter >= 0)
232 mutex_unlock(&cfg80211_drv_mutex);
234 if (unlikely(drv->idx < 0)) {
240 /* give it a proper name */
241 snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
242 PHY_NAME "%d", drv->idx);
244 mutex_init(&drv->mtx);
245 mutex_init(&drv->devlist_mtx);
246 INIT_LIST_HEAD(&drv->netdev_list);
248 device_initialize(&drv->wiphy.dev);
249 drv->wiphy.dev.class = &ieee80211_class;
250 drv->wiphy.dev.platform_data = drv;
254 EXPORT_SYMBOL(wiphy_new);
256 int wiphy_register(struct wiphy *wiphy)
258 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
260 enum ieee80211_band band;
261 struct ieee80211_supported_band *sband;
262 bool have_band = false;
264 u16 ifmodes = wiphy->interface_modes;
266 /* sanity check ifmodes */
268 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
269 if (WARN_ON(ifmodes != wiphy->interface_modes))
270 wiphy->interface_modes = ifmodes;
272 /* sanity check supported bands/channels */
273 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
274 sband = wiphy->bands[band];
280 if (!sband->n_channels || !sband->n_bitrates) {
285 for (i = 0; i < sband->n_channels; i++) {
286 sband->channels[i].orig_flags =
287 sband->channels[i].flags;
288 sband->channels[i].orig_mag =
289 sband->channels[i].max_antenna_gain;
290 sband->channels[i].orig_mpwr =
291 sband->channels[i].max_power;
292 sband->channels[i].band = band;
303 /* check and set up bitrates */
304 ieee80211_set_bitrate_flags(wiphy);
306 /* set up regulatory info */
307 mutex_lock(&cfg80211_reg_mutex);
308 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
309 mutex_unlock(&cfg80211_reg_mutex);
311 mutex_lock(&cfg80211_drv_mutex);
313 res = device_add(&drv->wiphy.dev);
317 list_add(&drv->list, &cfg80211_drv_list);
320 drv->wiphy.debugfsdir =
321 debugfs_create_dir(wiphy_name(&drv->wiphy),
322 ieee80211_debugfs_dir);
326 mutex_unlock(&cfg80211_drv_mutex);
329 EXPORT_SYMBOL(wiphy_register);
331 void wiphy_unregister(struct wiphy *wiphy)
333 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
335 /* protect the device list */
336 mutex_lock(&cfg80211_drv_mutex);
338 BUG_ON(!list_empty(&drv->netdev_list));
341 * Try to grab drv->mtx. If a command is still in progress,
342 * hopefully the driver will refuse it since it's tearing
343 * down the device already. We wait for this command to complete
344 * before unlinking the item from the list.
345 * Note: as codified by the BUG_ON above we cannot get here if
346 * a virtual interface is still associated. Hence, we can only
347 * get to lock contention here if userspace issues a command
348 * that identified the hardware by wiphy index.
350 mutex_lock(&drv->mtx);
351 /* unlock again before freeing */
352 mutex_unlock(&drv->mtx);
354 list_del(&drv->list);
355 device_del(&drv->wiphy.dev);
356 debugfs_remove(drv->wiphy.debugfsdir);
358 mutex_unlock(&cfg80211_drv_mutex);
360 EXPORT_SYMBOL(wiphy_unregister);
362 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
364 mutex_destroy(&drv->mtx);
365 mutex_destroy(&drv->devlist_mtx);
369 void wiphy_free(struct wiphy *wiphy)
371 put_device(&wiphy->dev);
373 EXPORT_SYMBOL(wiphy_free);
375 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
379 struct net_device *dev = ndev;
380 struct cfg80211_registered_device *rdev;
382 if (!dev->ieee80211_ptr)
385 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
388 case NETDEV_REGISTER:
389 mutex_lock(&rdev->devlist_mtx);
390 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
391 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
393 printk(KERN_ERR "wireless: failed to add phy80211 "
394 "symlink to netdev!\n");
396 dev->ieee80211_ptr->netdev = dev;
397 mutex_unlock(&rdev->devlist_mtx);
399 case NETDEV_UNREGISTER:
400 mutex_lock(&rdev->devlist_mtx);
401 if (!list_empty(&dev->ieee80211_ptr->list)) {
402 sysfs_remove_link(&dev->dev.kobj, "phy80211");
403 list_del_init(&dev->ieee80211_ptr->list);
405 mutex_unlock(&rdev->devlist_mtx);
412 static struct notifier_block cfg80211_netdev_notifier = {
413 .notifier_call = cfg80211_netdev_notifier_call,
416 static int cfg80211_init(void)
420 err = wiphy_sysfs_init();
424 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
426 goto out_fail_notifier;
428 err = nl80211_init();
430 goto out_fail_nl80211;
432 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
434 err = regulatory_init();
441 debugfs_remove(ieee80211_debugfs_dir);
443 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
450 subsys_initcall(cfg80211_init);
452 static void cfg80211_exit(void)
454 debugfs_remove(ieee80211_debugfs_dir);
456 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
460 module_exit(cfg80211_exit);