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