Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This is the linux wireless configuration interface. |
| 3 | * |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 4 | * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net> |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/if.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/err.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> |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 19 | #include "nl80211.h" |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 20 | #include "core.h" |
| 21 | #include "sysfs.h" |
| 22 | |
| 23 | /* name for sysfs, %d is appended */ |
| 24 | #define PHY_NAME "phy" |
| 25 | |
| 26 | MODULE_AUTHOR("Johannes Berg"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | MODULE_DESCRIPTION("wireless configuration support"); |
| 29 | |
| 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); |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 34 | |
| 35 | /* |
| 36 | * This is used to protect the cfg80211_drv_list, cfg80211_regdomain, and |
| 37 | * the last reguluatory request receipt in regd.c |
| 38 | */ |
| 39 | DEFINE_MUTEX(cfg80211_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 40 | |
| 41 | /* for debugfs */ |
| 42 | static struct dentry *ieee80211_debugfs_dir; |
| 43 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 44 | /* requires cfg80211_drv_mutex to be held! */ |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 45 | static struct cfg80211_registered_device * |
| 46 | cfg80211_drv_by_wiphy_idx(int wiphy_idx) |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 47 | { |
| 48 | struct cfg80211_registered_device *result = NULL, *drv; |
| 49 | |
Luis R. Rodriguez | 85fd129 | 2009-02-21 00:04:20 -0500 | [diff] [blame] | 50 | if (!wiphy_idx_valid(wiphy_idx)) |
| 51 | return NULL; |
| 52 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 53 | list_for_each_entry(drv, &cfg80211_drv_list, list) { |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 54 | if (drv->wiphy_idx == wiphy_idx) { |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 55 | result = drv; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 63 | /* requires cfg80211_mutex to be held! */ |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 64 | static struct cfg80211_registered_device * |
| 65 | __cfg80211_drv_from_info(struct genl_info *info) |
| 66 | { |
| 67 | int ifindex; |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 68 | struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 69 | struct net_device *dev; |
| 70 | int err = -EINVAL; |
| 71 | |
| 72 | if (info->attrs[NL80211_ATTR_WIPHY]) { |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 73 | bywiphyidx = cfg80211_drv_by_wiphy_idx( |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 74 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY])); |
| 75 | err = -ENODEV; |
| 76 | } |
| 77 | |
| 78 | if (info->attrs[NL80211_ATTR_IFINDEX]) { |
| 79 | ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]); |
| 80 | dev = dev_get_by_index(&init_net, ifindex); |
| 81 | if (dev) { |
| 82 | if (dev->ieee80211_ptr) |
| 83 | byifidx = |
| 84 | wiphy_to_dev(dev->ieee80211_ptr->wiphy); |
| 85 | dev_put(dev); |
| 86 | } |
| 87 | err = -ENODEV; |
| 88 | } |
| 89 | |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 90 | if (bywiphyidx && byifidx) { |
| 91 | if (bywiphyidx != byifidx) |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 92 | return ERR_PTR(-EINVAL); |
| 93 | else |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 94 | return bywiphyidx; /* == byifidx */ |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 95 | } |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 96 | if (bywiphyidx) |
| 97 | return bywiphyidx; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 98 | |
| 99 | if (byifidx) |
| 100 | return byifidx; |
| 101 | |
| 102 | return ERR_PTR(err); |
| 103 | } |
| 104 | |
| 105 | struct cfg80211_registered_device * |
| 106 | cfg80211_get_dev_from_info(struct genl_info *info) |
| 107 | { |
| 108 | struct cfg80211_registered_device *drv; |
| 109 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 110 | mutex_lock(&cfg80211_mutex); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 111 | drv = __cfg80211_drv_from_info(info); |
| 112 | |
| 113 | /* if it is not an error we grab the lock on |
| 114 | * it to assure it won't be going away while |
| 115 | * we operate on it */ |
| 116 | if (!IS_ERR(drv)) |
| 117 | mutex_lock(&drv->mtx); |
| 118 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 119 | mutex_unlock(&cfg80211_mutex); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 120 | |
| 121 | return drv; |
| 122 | } |
| 123 | |
| 124 | struct cfg80211_registered_device * |
| 125 | cfg80211_get_dev_from_ifindex(int ifindex) |
| 126 | { |
| 127 | struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV); |
| 128 | struct net_device *dev; |
| 129 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 130 | mutex_lock(&cfg80211_mutex); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 131 | dev = dev_get_by_index(&init_net, ifindex); |
| 132 | if (!dev) |
| 133 | goto out; |
| 134 | if (dev->ieee80211_ptr) { |
| 135 | drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy); |
| 136 | mutex_lock(&drv->mtx); |
| 137 | } else |
| 138 | drv = ERR_PTR(-ENODEV); |
| 139 | dev_put(dev); |
| 140 | out: |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 141 | mutex_unlock(&cfg80211_mutex); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 142 | return drv; |
| 143 | } |
| 144 | |
| 145 | void cfg80211_put_dev(struct cfg80211_registered_device *drv) |
| 146 | { |
| 147 | BUG_ON(IS_ERR(drv)); |
| 148 | mutex_unlock(&drv->mtx); |
| 149 | } |
| 150 | |
| 151 | int cfg80211_dev_rename(struct cfg80211_registered_device *rdev, |
| 152 | char *newname) |
| 153 | { |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 154 | struct cfg80211_registered_device *drv; |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 155 | int wiphy_idx, taken = -1, result, digits; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 156 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 157 | mutex_lock(&cfg80211_mutex); |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 158 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 159 | /* prohibit calling the thing phy%d when %d is not its number */ |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 160 | sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken); |
| 161 | if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) { |
| 162 | /* count number of places needed to print wiphy_idx */ |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 163 | digits = 1; |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 164 | while (wiphy_idx /= 10) |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 165 | digits++; |
| 166 | /* |
| 167 | * deny the name if it is phy<idx> where <idx> is printed |
| 168 | * without leading zeroes. taken == strlen(newname) here |
| 169 | */ |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 170 | result = -EINVAL; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 171 | if (taken == strlen(PHY_NAME) + digits) |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 172 | goto out_unlock; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 175 | |
| 176 | /* Ignore nop renames */ |
| 177 | result = 0; |
| 178 | if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0) |
| 179 | goto out_unlock; |
| 180 | |
| 181 | /* Ensure another device does not already have this name. */ |
| 182 | list_for_each_entry(drv, &cfg80211_drv_list, list) { |
| 183 | result = -EINVAL; |
| 184 | if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0) |
| 185 | goto out_unlock; |
| 186 | } |
| 187 | |
| 188 | /* this will only check for collisions in sysfs |
| 189 | * which is not even always compiled in. |
| 190 | */ |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 191 | result = device_rename(&rdev->wiphy.dev, newname); |
| 192 | if (result) |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 193 | goto out_unlock; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 194 | |
Johannes Berg | 33c0360 | 2008-10-08 10:23:48 +0200 | [diff] [blame] | 195 | if (rdev->wiphy.debugfsdir && |
| 196 | !debugfs_rename(rdev->wiphy.debugfsdir->d_parent, |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 197 | rdev->wiphy.debugfsdir, |
| 198 | rdev->wiphy.debugfsdir->d_parent, |
| 199 | newname)) |
| 200 | printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n", |
| 201 | newname); |
| 202 | |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 203 | result = 0; |
| 204 | out_unlock: |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 205 | mutex_unlock(&cfg80211_mutex); |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 206 | if (result == 0) |
| 207 | nl80211_notify_dev_rename(rdev); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 208 | |
Eric W. Biederman | 2940bb6 | 2008-05-08 14:30:18 -0700 | [diff] [blame] | 209 | return result; |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 212 | /* exported functions */ |
| 213 | |
| 214 | struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv) |
| 215 | { |
Denis ChengRq | 638af07 | 2008-09-23 02:35:37 +0800 | [diff] [blame] | 216 | static int wiphy_counter; |
| 217 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 218 | struct cfg80211_registered_device *drv; |
| 219 | int alloc_size; |
| 220 | |
Johannes Berg | 41ade00 | 2007-12-19 02:03:29 +0100 | [diff] [blame] | 221 | WARN_ON(!ops->add_key && ops->del_key); |
| 222 | WARN_ON(ops->add_key && !ops->del_key); |
| 223 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 224 | alloc_size = sizeof(*drv) + sizeof_priv; |
| 225 | |
| 226 | drv = kzalloc(alloc_size, GFP_KERNEL); |
| 227 | if (!drv) |
| 228 | return NULL; |
| 229 | |
| 230 | drv->ops = ops; |
| 231 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 232 | mutex_lock(&cfg80211_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 233 | |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 234 | drv->wiphy_idx = wiphy_counter++; |
Johannes Berg | a4d73ee | 2007-04-26 20:50:35 -0700 | [diff] [blame] | 235 | |
Luis R. Rodriguez | 85fd129 | 2009-02-21 00:04:20 -0500 | [diff] [blame] | 236 | if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) { |
Denis ChengRq | 638af07 | 2008-09-23 02:35:37 +0800 | [diff] [blame] | 237 | wiphy_counter--; |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 238 | mutex_unlock(&cfg80211_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 239 | /* ugh, wrapped! */ |
| 240 | kfree(drv); |
| 241 | return NULL; |
| 242 | } |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 243 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 244 | mutex_unlock(&cfg80211_mutex); |
Denis ChengRq | 638af07 | 2008-09-23 02:35:37 +0800 | [diff] [blame] | 245 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 246 | /* give it a proper name */ |
Luis R. Rodriguez | b5850a7 | 2009-02-21 00:04:19 -0500 | [diff] [blame] | 247 | dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 248 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 249 | mutex_init(&drv->mtx); |
| 250 | mutex_init(&drv->devlist_mtx); |
| 251 | INIT_LIST_HEAD(&drv->netdev_list); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 252 | spin_lock_init(&drv->bss_lock); |
| 253 | INIT_LIST_HEAD(&drv->bss_list); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 254 | |
| 255 | device_initialize(&drv->wiphy.dev); |
| 256 | drv->wiphy.dev.class = &ieee80211_class; |
| 257 | drv->wiphy.dev.platform_data = drv; |
| 258 | |
| 259 | return &drv->wiphy; |
| 260 | } |
| 261 | EXPORT_SYMBOL(wiphy_new); |
| 262 | |
| 263 | int wiphy_register(struct wiphy *wiphy) |
| 264 | { |
| 265 | struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy); |
| 266 | int res; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 267 | enum ieee80211_band band; |
| 268 | struct ieee80211_supported_band *sband; |
| 269 | bool have_band = false; |
| 270 | int i; |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 271 | u16 ifmodes = wiphy->interface_modes; |
| 272 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 273 | if (WARN_ON(wiphy->max_scan_ssids < 1)) |
| 274 | return -EINVAL; |
| 275 | |
Luis R. Rodriguez | f59ac04 | 2008-08-29 16:26:43 -0700 | [diff] [blame] | 276 | /* sanity check ifmodes */ |
| 277 | WARN_ON(!ifmodes); |
| 278 | ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1; |
| 279 | if (WARN_ON(ifmodes != wiphy->interface_modes)) |
| 280 | wiphy->interface_modes = ifmodes; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 281 | |
| 282 | /* sanity check supported bands/channels */ |
| 283 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 284 | sband = wiphy->bands[band]; |
| 285 | if (!sband) |
| 286 | continue; |
| 287 | |
| 288 | sband->band = band; |
| 289 | |
Johannes Berg | 881d948 | 2009-01-21 15:13:48 +0100 | [diff] [blame] | 290 | if (WARN_ON(!sband->n_channels || !sband->n_bitrates)) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 291 | return -EINVAL; |
Johannes Berg | 881d948 | 2009-01-21 15:13:48 +0100 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * Since we use a u32 for rate bitmaps in |
| 295 | * ieee80211_get_response_rate, we cannot |
| 296 | * have more than 32 legacy rates. |
| 297 | */ |
| 298 | if (WARN_ON(sband->n_bitrates > 32)) |
| 299 | return -EINVAL; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 300 | |
| 301 | for (i = 0; i < sband->n_channels; i++) { |
| 302 | sband->channels[i].orig_flags = |
| 303 | sband->channels[i].flags; |
| 304 | sband->channels[i].orig_mag = |
| 305 | sband->channels[i].max_antenna_gain; |
| 306 | sband->channels[i].orig_mpwr = |
| 307 | sband->channels[i].max_power; |
| 308 | sband->channels[i].band = band; |
| 309 | } |
| 310 | |
| 311 | have_band = true; |
| 312 | } |
| 313 | |
| 314 | if (!have_band) { |
| 315 | WARN_ON(1); |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | |
| 319 | /* check and set up bitrates */ |
| 320 | ieee80211_set_bitrate_flags(wiphy); |
| 321 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 322 | mutex_lock(&cfg80211_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 323 | |
Johannes Berg | f3b407f | 2008-10-21 09:57:41 +0200 | [diff] [blame] | 324 | /* set up regulatory info */ |
| 325 | wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE); |
| 326 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 327 | res = device_add(&drv->wiphy.dev); |
| 328 | if (res) |
| 329 | goto out_unlock; |
| 330 | |
| 331 | list_add(&drv->list, &cfg80211_drv_list); |
| 332 | |
| 333 | /* add to debugfs */ |
| 334 | drv->wiphy.debugfsdir = |
| 335 | debugfs_create_dir(wiphy_name(&drv->wiphy), |
| 336 | ieee80211_debugfs_dir); |
Johannes Berg | 33c0360 | 2008-10-08 10:23:48 +0200 | [diff] [blame] | 337 | if (IS_ERR(drv->wiphy.debugfsdir)) |
| 338 | drv->wiphy.debugfsdir = NULL; |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 339 | |
| 340 | res = 0; |
| 341 | out_unlock: |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 342 | mutex_unlock(&cfg80211_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 343 | return res; |
| 344 | } |
| 345 | EXPORT_SYMBOL(wiphy_register); |
| 346 | |
| 347 | void wiphy_unregister(struct wiphy *wiphy) |
| 348 | { |
| 349 | struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy); |
| 350 | |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 351 | /* protect the device list */ |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 352 | mutex_lock(&cfg80211_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 353 | |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 354 | BUG_ON(!list_empty(&drv->netdev_list)); |
| 355 | |
| 356 | /* |
| 357 | * Try to grab drv->mtx. If a command is still in progress, |
| 358 | * hopefully the driver will refuse it since it's tearing |
| 359 | * down the device already. We wait for this command to complete |
| 360 | * before unlinking the item from the list. |
| 361 | * Note: as codified by the BUG_ON above we cannot get here if |
| 362 | * a virtual interface is still associated. Hence, we can only |
| 363 | * get to lock contention here if userspace issues a command |
| 364 | * that identified the hardware by wiphy index. |
| 365 | */ |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 366 | mutex_lock(&drv->mtx); |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 367 | /* unlock again before freeing */ |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 368 | mutex_unlock(&drv->mtx); |
| 369 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 370 | /* If this device got a regulatory hint tell core its |
| 371 | * free to listen now to a new shiny device regulatory hint */ |
| 372 | reg_device_remove(wiphy); |
| 373 | |
Johannes Berg | f16bfc1 | 2007-04-26 20:51:12 -0700 | [diff] [blame] | 374 | list_del(&drv->list); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 375 | device_del(&drv->wiphy.dev); |
| 376 | debugfs_remove(drv->wiphy.debugfsdir); |
| 377 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame^] | 378 | mutex_unlock(&cfg80211_mutex); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 379 | } |
| 380 | EXPORT_SYMBOL(wiphy_unregister); |
| 381 | |
| 382 | void cfg80211_dev_free(struct cfg80211_registered_device *drv) |
| 383 | { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 384 | struct cfg80211_internal_bss *scan, *tmp; |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 385 | mutex_destroy(&drv->mtx); |
| 386 | mutex_destroy(&drv->devlist_mtx); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 387 | list_for_each_entry_safe(scan, tmp, &drv->bss_list, list) |
Johannes Berg | 78c1c7e | 2009-02-10 21:25:57 +0100 | [diff] [blame] | 388 | cfg80211_put_bss(&scan->pub); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 389 | kfree(drv); |
| 390 | } |
| 391 | |
| 392 | void wiphy_free(struct wiphy *wiphy) |
| 393 | { |
| 394 | put_device(&wiphy->dev); |
| 395 | } |
| 396 | EXPORT_SYMBOL(wiphy_free); |
| 397 | |
| 398 | static int cfg80211_netdev_notifier_call(struct notifier_block * nb, |
| 399 | unsigned long state, |
| 400 | void *ndev) |
| 401 | { |
| 402 | struct net_device *dev = ndev; |
| 403 | struct cfg80211_registered_device *rdev; |
| 404 | |
| 405 | if (!dev->ieee80211_ptr) |
| 406 | return 0; |
| 407 | |
| 408 | rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy); |
| 409 | |
Johannes Berg | 60719ff | 2008-09-16 14:55:09 +0200 | [diff] [blame] | 410 | WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED); |
| 411 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 412 | switch (state) { |
| 413 | case NETDEV_REGISTER: |
| 414 | mutex_lock(&rdev->devlist_mtx); |
| 415 | list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list); |
| 416 | if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj, |
| 417 | "phy80211")) { |
| 418 | printk(KERN_ERR "wireless: failed to add phy80211 " |
| 419 | "symlink to netdev!\n"); |
| 420 | } |
| 421 | dev->ieee80211_ptr->netdev = dev; |
| 422 | mutex_unlock(&rdev->devlist_mtx); |
| 423 | break; |
| 424 | case NETDEV_UNREGISTER: |
| 425 | mutex_lock(&rdev->devlist_mtx); |
| 426 | if (!list_empty(&dev->ieee80211_ptr->list)) { |
| 427 | sysfs_remove_link(&dev->dev.kobj, "phy80211"); |
| 428 | list_del_init(&dev->ieee80211_ptr->list); |
| 429 | } |
| 430 | mutex_unlock(&rdev->devlist_mtx); |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static struct notifier_block cfg80211_netdev_notifier = { |
| 438 | .notifier_call = cfg80211_netdev_notifier_call, |
| 439 | }; |
| 440 | |
| 441 | static int cfg80211_init(void) |
| 442 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 443 | int err; |
| 444 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 445 | err = wiphy_sysfs_init(); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 446 | if (err) |
| 447 | goto out_fail_sysfs; |
| 448 | |
| 449 | err = register_netdevice_notifier(&cfg80211_netdev_notifier); |
| 450 | if (err) |
| 451 | goto out_fail_notifier; |
| 452 | |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 453 | err = nl80211_init(); |
| 454 | if (err) |
| 455 | goto out_fail_nl80211; |
| 456 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 457 | ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL); |
| 458 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 459 | err = regulatory_init(); |
| 460 | if (err) |
| 461 | goto out_fail_reg; |
| 462 | |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 463 | return 0; |
| 464 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 465 | out_fail_reg: |
| 466 | debugfs_remove(ieee80211_debugfs_dir); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 467 | out_fail_nl80211: |
| 468 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 469 | out_fail_notifier: |
| 470 | wiphy_sysfs_exit(); |
| 471 | out_fail_sysfs: |
| 472 | return err; |
| 473 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 474 | |
Johannes Berg | 3a46246 | 2007-09-10 13:44:45 +0200 | [diff] [blame] | 475 | subsys_initcall(cfg80211_init); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 476 | |
| 477 | static void cfg80211_exit(void) |
| 478 | { |
| 479 | debugfs_remove(ieee80211_debugfs_dir); |
Johannes Berg | 5568296 | 2007-09-20 13:09:35 -0400 | [diff] [blame] | 480 | nl80211_exit(); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 481 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); |
| 482 | wiphy_sysfs_exit(); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 483 | regulatory_exit(); |
Johannes Berg | 704232c | 2007-04-23 12:20:05 -0700 | [diff] [blame] | 484 | } |
| 485 | module_exit(cfg80211_exit); |