2 * This is the linux wireless configuration interface.
4 * Copyright 2006-2009 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>
21 /* name for sysfs, %d is appended */
22 #define PHY_NAME "phy"
24 MODULE_AUTHOR("Johannes Berg");
25 MODULE_LICENSE("GPL");
26 MODULE_DESCRIPTION("wireless configuration support");
28 /* RCU might be appropriate here since we usually
29 * only read the list, and that can happen quite
30 * often because we need to do it for each command */
31 LIST_HEAD(cfg80211_drv_list);
34 * This is used to protect the cfg80211_drv_list, cfg80211_regdomain,
35 * country_ie_regdomain, the reg_beacon_list and the the last regulatory
36 * request receipt (last_request).
38 DEFINE_MUTEX(cfg80211_mutex);
41 static struct dentry *ieee80211_debugfs_dir;
43 /* requires cfg80211_mutex to be held! */
44 struct cfg80211_registered_device *cfg80211_drv_by_wiphy_idx(int wiphy_idx)
46 struct cfg80211_registered_device *result = NULL, *drv;
48 if (!wiphy_idx_valid(wiphy_idx))
51 assert_cfg80211_lock();
53 list_for_each_entry(drv, &cfg80211_drv_list, list) {
54 if (drv->wiphy_idx == wiphy_idx) {
63 int get_wiphy_idx(struct wiphy *wiphy)
65 struct cfg80211_registered_device *drv;
67 return WIPHY_IDX_STALE;
68 drv = wiphy_to_dev(wiphy);
69 return drv->wiphy_idx;
72 /* requires cfg80211_drv_mutex to be held! */
73 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
75 struct cfg80211_registered_device *drv;
77 if (!wiphy_idx_valid(wiphy_idx))
80 assert_cfg80211_lock();
82 drv = cfg80211_drv_by_wiphy_idx(wiphy_idx);
88 /* requires cfg80211_mutex to be held! */
89 struct cfg80211_registered_device *
90 __cfg80211_drv_from_info(struct genl_info *info)
93 struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
94 struct net_device *dev;
97 assert_cfg80211_lock();
99 if (info->attrs[NL80211_ATTR_WIPHY]) {
100 bywiphyidx = cfg80211_drv_by_wiphy_idx(
101 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
105 if (info->attrs[NL80211_ATTR_IFINDEX]) {
106 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
107 dev = dev_get_by_index(&init_net, ifindex);
109 if (dev->ieee80211_ptr)
111 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
117 if (bywiphyidx && byifidx) {
118 if (bywiphyidx != byifidx)
119 return ERR_PTR(-EINVAL);
121 return bywiphyidx; /* == byifidx */
132 struct cfg80211_registered_device *
133 cfg80211_get_dev_from_info(struct genl_info *info)
135 struct cfg80211_registered_device *drv;
137 mutex_lock(&cfg80211_mutex);
138 drv = __cfg80211_drv_from_info(info);
140 /* if it is not an error we grab the lock on
141 * it to assure it won't be going away while
142 * we operate on it */
144 mutex_lock(&drv->mtx);
146 mutex_unlock(&cfg80211_mutex);
151 struct cfg80211_registered_device *
152 cfg80211_get_dev_from_ifindex(int ifindex)
154 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
155 struct net_device *dev;
157 mutex_lock(&cfg80211_mutex);
158 dev = dev_get_by_index(&init_net, ifindex);
161 if (dev->ieee80211_ptr) {
162 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
163 mutex_lock(&drv->mtx);
165 drv = ERR_PTR(-ENODEV);
168 mutex_unlock(&cfg80211_mutex);
172 void cfg80211_put_dev(struct cfg80211_registered_device *drv)
175 mutex_unlock(&drv->mtx);
178 /* requires cfg80211_mutex to be held */
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 assert_cfg80211_lock();
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
198 if (taken == strlen(PHY_NAME) + digits)
203 /* Ignore nop renames */
204 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
207 /* Ensure another device does not already have this name. */
208 list_for_each_entry(drv, &cfg80211_drv_list, list)
209 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
212 result = device_rename(&rdev->wiphy.dev, newname);
216 if (rdev->wiphy.debugfsdir &&
217 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
218 rdev->wiphy.debugfsdir,
219 rdev->wiphy.debugfsdir->d_parent,
221 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
224 nl80211_notify_dev_rename(rdev);
229 /* exported functions */
231 struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
233 static int wiphy_counter;
235 struct cfg80211_registered_device *drv;
238 WARN_ON(!ops->add_key && ops->del_key);
239 WARN_ON(ops->add_key && !ops->del_key);
241 alloc_size = sizeof(*drv) + sizeof_priv;
243 drv = kzalloc(alloc_size, GFP_KERNEL);
249 mutex_lock(&cfg80211_mutex);
251 drv->wiphy_idx = wiphy_counter++;
253 if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
255 mutex_unlock(&cfg80211_mutex);
261 mutex_unlock(&cfg80211_mutex);
263 /* give it a proper name */
264 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
266 mutex_init(&drv->mtx);
267 mutex_init(&drv->devlist_mtx);
268 INIT_LIST_HEAD(&drv->netdev_list);
269 spin_lock_init(&drv->bss_lock);
270 INIT_LIST_HEAD(&drv->bss_list);
272 device_initialize(&drv->wiphy.dev);
273 drv->wiphy.dev.class = &ieee80211_class;
274 drv->wiphy.dev.platform_data = drv;
277 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
278 * Fragmentation and RTS threshold are disabled by default with the
281 drv->wiphy.retry_short = 7;
282 drv->wiphy.retry_long = 4;
283 drv->wiphy.frag_threshold = (u32) -1;
284 drv->wiphy.rts_threshold = (u32) -1;
288 EXPORT_SYMBOL(wiphy_new);
290 int wiphy_register(struct wiphy *wiphy)
292 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
294 enum ieee80211_band band;
295 struct ieee80211_supported_band *sband;
296 bool have_band = false;
298 u16 ifmodes = wiphy->interface_modes;
300 if (WARN_ON(wiphy->max_scan_ssids < 1))
303 /* sanity check ifmodes */
305 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
306 if (WARN_ON(ifmodes != wiphy->interface_modes))
307 wiphy->interface_modes = ifmodes;
309 /* sanity check supported bands/channels */
310 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
311 sband = wiphy->bands[band];
317 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
321 * Since we use a u32 for rate bitmaps in
322 * ieee80211_get_response_rate, we cannot
323 * have more than 32 legacy rates.
325 if (WARN_ON(sband->n_bitrates > 32))
328 for (i = 0; i < sband->n_channels; i++) {
329 sband->channels[i].orig_flags =
330 sband->channels[i].flags;
331 sband->channels[i].orig_mag =
332 sband->channels[i].max_antenna_gain;
333 sband->channels[i].orig_mpwr =
334 sband->channels[i].max_power;
335 sband->channels[i].band = band;
346 /* check and set up bitrates */
347 ieee80211_set_bitrate_flags(wiphy);
349 mutex_lock(&cfg80211_mutex);
351 /* set up regulatory info */
352 wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
354 res = device_add(&drv->wiphy.dev);
358 list_add(&drv->list, &cfg80211_drv_list);
361 drv->wiphy.debugfsdir =
362 debugfs_create_dir(wiphy_name(&drv->wiphy),
363 ieee80211_debugfs_dir);
364 if (IS_ERR(drv->wiphy.debugfsdir))
365 drv->wiphy.debugfsdir = NULL;
367 if (wiphy->custom_regulatory) {
368 struct regulatory_request request;
370 request.wiphy_idx = get_wiphy_idx(wiphy);
371 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
372 request.alpha2[0] = '9';
373 request.alpha2[1] = '9';
375 nl80211_send_reg_change_event(&request);
380 mutex_unlock(&cfg80211_mutex);
383 EXPORT_SYMBOL(wiphy_register);
385 void wiphy_unregister(struct wiphy *wiphy)
387 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
389 /* protect the device list */
390 mutex_lock(&cfg80211_mutex);
392 BUG_ON(!list_empty(&drv->netdev_list));
395 * Try to grab drv->mtx. If a command is still in progress,
396 * hopefully the driver will refuse it since it's tearing
397 * down the device already. We wait for this command to complete
398 * before unlinking the item from the list.
399 * Note: as codified by the BUG_ON above we cannot get here if
400 * a virtual interface is still associated. Hence, we can only
401 * get to lock contention here if userspace issues a command
402 * that identified the hardware by wiphy index.
404 mutex_lock(&drv->mtx);
405 /* unlock again before freeing */
406 mutex_unlock(&drv->mtx);
408 /* If this device got a regulatory hint tell core its
409 * free to listen now to a new shiny device regulatory hint */
410 reg_device_remove(wiphy);
412 list_del(&drv->list);
413 device_del(&drv->wiphy.dev);
414 debugfs_remove(drv->wiphy.debugfsdir);
416 mutex_unlock(&cfg80211_mutex);
418 EXPORT_SYMBOL(wiphy_unregister);
420 void cfg80211_dev_free(struct cfg80211_registered_device *drv)
422 struct cfg80211_internal_bss *scan, *tmp;
423 mutex_destroy(&drv->mtx);
424 mutex_destroy(&drv->devlist_mtx);
425 list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
426 cfg80211_put_bss(&scan->pub);
430 void wiphy_free(struct wiphy *wiphy)
432 put_device(&wiphy->dev);
434 EXPORT_SYMBOL(wiphy_free);
436 static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
440 struct net_device *dev = ndev;
441 struct cfg80211_registered_device *rdev;
443 if (!dev->ieee80211_ptr)
446 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
448 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
451 case NETDEV_REGISTER:
452 mutex_lock(&rdev->devlist_mtx);
453 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
454 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
456 printk(KERN_ERR "wireless: failed to add phy80211 "
457 "symlink to netdev!\n");
459 dev->ieee80211_ptr->netdev = dev;
460 #ifdef CONFIG_WIRELESS_EXT
461 dev->ieee80211_ptr->wext.default_key = -1;
462 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
464 mutex_unlock(&rdev->devlist_mtx);
466 case NETDEV_GOING_DOWN:
467 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
469 if (!dev->ieee80211_ptr->ssid_len)
471 cfg80211_leave_ibss(rdev, dev, true);
474 #ifdef CONFIG_WIRELESS_EXT
475 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
477 if (!dev->ieee80211_ptr->wext.ibss.ssid_len)
479 cfg80211_join_ibss(rdev, dev, &dev->ieee80211_ptr->wext.ibss);
482 case NETDEV_UNREGISTER:
483 mutex_lock(&rdev->devlist_mtx);
484 if (!list_empty(&dev->ieee80211_ptr->list)) {
485 sysfs_remove_link(&dev->dev.kobj, "phy80211");
486 list_del_init(&dev->ieee80211_ptr->list);
488 mutex_unlock(&rdev->devlist_mtx);
495 static struct notifier_block cfg80211_netdev_notifier = {
496 .notifier_call = cfg80211_netdev_notifier_call,
499 static int cfg80211_init(void)
503 err = wiphy_sysfs_init();
507 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
509 goto out_fail_notifier;
511 err = nl80211_init();
513 goto out_fail_nl80211;
515 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
517 err = regulatory_init();
524 debugfs_remove(ieee80211_debugfs_dir);
526 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
533 subsys_initcall(cfg80211_init);
535 static void cfg80211_exit(void)
537 debugfs_remove(ieee80211_debugfs_dir);
539 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
543 module_exit(cfg80211_exit);