Commit | Line | Data |
---|---|---|
704232c2 JB |
1 | /* |
2 | * This is the linux wireless configuration interface. | |
3 | * | |
f59ac048 | 4 | * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net> |
704232c2 JB |
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> | |
55682965 | 19 | #include "nl80211.h" |
704232c2 JB |
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); | |
a1794390 LR |
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); | |
704232c2 JB |
40 | |
41 | /* for debugfs */ | |
42 | static struct dentry *ieee80211_debugfs_dir; | |
43 | ||
55682965 | 44 | /* requires cfg80211_drv_mutex to be held! */ |
b5850a7a LR |
45 | static struct cfg80211_registered_device * |
46 | cfg80211_drv_by_wiphy_idx(int wiphy_idx) | |
55682965 JB |
47 | { |
48 | struct cfg80211_registered_device *result = NULL, *drv; | |
49 | ||
85fd129a LR |
50 | if (!wiphy_idx_valid(wiphy_idx)) |
51 | return NULL; | |
52 | ||
55682965 | 53 | list_for_each_entry(drv, &cfg80211_drv_list, list) { |
b5850a7a | 54 | if (drv->wiphy_idx == wiphy_idx) { |
55682965 JB |
55 | result = drv; |
56 | break; | |
57 | } | |
58 | } | |
59 | ||
60 | return result; | |
61 | } | |
62 | ||
a1794390 | 63 | /* requires cfg80211_mutex to be held! */ |
55682965 JB |
64 | static struct cfg80211_registered_device * |
65 | __cfg80211_drv_from_info(struct genl_info *info) | |
66 | { | |
67 | int ifindex; | |
b5850a7a | 68 | struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL; |
55682965 JB |
69 | struct net_device *dev; |
70 | int err = -EINVAL; | |
71 | ||
72 | if (info->attrs[NL80211_ATTR_WIPHY]) { | |
b5850a7a | 73 | bywiphyidx = cfg80211_drv_by_wiphy_idx( |
55682965 JB |
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 | ||
b5850a7a LR |
90 | if (bywiphyidx && byifidx) { |
91 | if (bywiphyidx != byifidx) | |
55682965 JB |
92 | return ERR_PTR(-EINVAL); |
93 | else | |
b5850a7a | 94 | return bywiphyidx; /* == byifidx */ |
55682965 | 95 | } |
b5850a7a LR |
96 | if (bywiphyidx) |
97 | return bywiphyidx; | |
55682965 JB |
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 | ||
a1794390 | 110 | mutex_lock(&cfg80211_mutex); |
55682965 JB |
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 | ||
a1794390 | 119 | mutex_unlock(&cfg80211_mutex); |
55682965 JB |
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 | ||
a1794390 | 130 | mutex_lock(&cfg80211_mutex); |
55682965 JB |
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: | |
a1794390 | 141 | mutex_unlock(&cfg80211_mutex); |
55682965 JB |
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 | { | |
2940bb69 | 154 | struct cfg80211_registered_device *drv; |
b5850a7a | 155 | int wiphy_idx, taken = -1, result, digits; |
55682965 | 156 | |
a1794390 | 157 | mutex_lock(&cfg80211_mutex); |
2940bb69 | 158 | |
55682965 | 159 | /* prohibit calling the thing phy%d when %d is not its number */ |
b5850a7a LR |
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 */ | |
55682965 | 163 | digits = 1; |
b5850a7a | 164 | while (wiphy_idx /= 10) |
55682965 JB |
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 | */ | |
2940bb69 | 170 | result = -EINVAL; |
55682965 | 171 | if (taken == strlen(PHY_NAME) + digits) |
2940bb69 EB |
172 | goto out_unlock; |
173 | } | |
174 | ||
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; | |
55682965 JB |
186 | } |
187 | ||
2940bb69 EB |
188 | /* this will only check for collisions in sysfs |
189 | * which is not even always compiled in. | |
190 | */ | |
55682965 JB |
191 | result = device_rename(&rdev->wiphy.dev, newname); |
192 | if (result) | |
2940bb69 | 193 | goto out_unlock; |
55682965 | 194 | |
33c0360b JB |
195 | if (rdev->wiphy.debugfsdir && |
196 | !debugfs_rename(rdev->wiphy.debugfsdir->d_parent, | |
55682965 JB |
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 | ||
2940bb69 EB |
203 | result = 0; |
204 | out_unlock: | |
a1794390 | 205 | mutex_unlock(&cfg80211_mutex); |
2940bb69 EB |
206 | if (result == 0) |
207 | nl80211_notify_dev_rename(rdev); | |
55682965 | 208 | |
2940bb69 | 209 | return result; |
55682965 JB |
210 | } |
211 | ||
704232c2 JB |
212 | /* exported functions */ |
213 | ||
214 | struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv) | |
215 | { | |
638af073 DC |
216 | static int wiphy_counter; |
217 | ||
704232c2 JB |
218 | struct cfg80211_registered_device *drv; |
219 | int alloc_size; | |
220 | ||
41ade00f JB |
221 | WARN_ON(!ops->add_key && ops->del_key); |
222 | WARN_ON(ops->add_key && !ops->del_key); | |
223 | ||
704232c2 JB |
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 | ||
a1794390 | 232 | mutex_lock(&cfg80211_mutex); |
704232c2 | 233 | |
b5850a7a | 234 | drv->wiphy_idx = wiphy_counter++; |
a4d73ee1 | 235 | |
85fd129a | 236 | if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) { |
638af073 | 237 | wiphy_counter--; |
a1794390 | 238 | mutex_unlock(&cfg80211_mutex); |
704232c2 JB |
239 | /* ugh, wrapped! */ |
240 | kfree(drv); | |
241 | return NULL; | |
242 | } | |
704232c2 | 243 | |
a1794390 | 244 | mutex_unlock(&cfg80211_mutex); |
638af073 | 245 | |
704232c2 | 246 | /* give it a proper name */ |
b5850a7a | 247 | dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx); |
704232c2 | 248 | |
704232c2 JB |
249 | mutex_init(&drv->mtx); |
250 | mutex_init(&drv->devlist_mtx); | |
251 | INIT_LIST_HEAD(&drv->netdev_list); | |
2a519311 JB |
252 | spin_lock_init(&drv->bss_lock); |
253 | INIT_LIST_HEAD(&drv->bss_list); | |
704232c2 JB |
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; | |
8318d78a JB |
267 | enum ieee80211_band band; |
268 | struct ieee80211_supported_band *sband; | |
269 | bool have_band = false; | |
270 | int i; | |
f59ac048 LR |
271 | u16 ifmodes = wiphy->interface_modes; |
272 | ||
2a519311 JB |
273 | if (WARN_ON(wiphy->max_scan_ssids < 1)) |
274 | return -EINVAL; | |
275 | ||
f59ac048 LR |
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; | |
8318d78a JB |
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 | ||
881d948c JB |
290 | if (WARN_ON(!sband->n_channels || !sband->n_bitrates)) |
291 | return -EINVAL; | |
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)) | |
8318d78a | 299 | return -EINVAL; |
8318d78a JB |
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 | ||
a1794390 | 322 | mutex_lock(&cfg80211_mutex); |
f3b407fb | 323 | |
8318d78a | 324 | /* set up regulatory info */ |
b2e1b302 | 325 | wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE); |
704232c2 | 326 | |
704232c2 JB |
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); | |
33c0360b JB |
337 | if (IS_ERR(drv->wiphy.debugfsdir)) |
338 | drv->wiphy.debugfsdir = NULL; | |
704232c2 JB |
339 | |
340 | res = 0; | |
341 | out_unlock: | |
a1794390 | 342 | mutex_unlock(&cfg80211_mutex); |
704232c2 JB |
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 | ||
f16bfc1c | 351 | /* protect the device list */ |
a1794390 | 352 | mutex_lock(&cfg80211_mutex); |
704232c2 | 353 | |
f16bfc1c JB |
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 | */ | |
704232c2 | 366 | mutex_lock(&drv->mtx); |
f16bfc1c | 367 | /* unlock again before freeing */ |
704232c2 JB |
368 | mutex_unlock(&drv->mtx); |
369 | ||
3f2355cb LR |
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 | ||
f16bfc1c | 374 | list_del(&drv->list); |
704232c2 JB |
375 | device_del(&drv->wiphy.dev); |
376 | debugfs_remove(drv->wiphy.debugfsdir); | |
377 | ||
a1794390 | 378 | mutex_unlock(&cfg80211_mutex); |
704232c2 JB |
379 | } |
380 | EXPORT_SYMBOL(wiphy_unregister); | |
381 | ||
382 | void cfg80211_dev_free(struct cfg80211_registered_device *drv) | |
383 | { | |
2a519311 | 384 | struct cfg80211_internal_bss *scan, *tmp; |
704232c2 JB |
385 | mutex_destroy(&drv->mtx); |
386 | mutex_destroy(&drv->devlist_mtx); | |
2a519311 | 387 | list_for_each_entry_safe(scan, tmp, &drv->bss_list, list) |
78c1c7e1 | 388 | cfg80211_put_bss(&scan->pub); |
704232c2 JB |
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 | ||
60719ffd JB |
410 | WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED); |
411 | ||
704232c2 JB |
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 | { | |
b2e1b302 LR |
443 | int err; |
444 | ||
b2e1b302 | 445 | err = wiphy_sysfs_init(); |
704232c2 JB |
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 | ||
55682965 JB |
453 | err = nl80211_init(); |
454 | if (err) | |
455 | goto out_fail_nl80211; | |
456 | ||
704232c2 JB |
457 | ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL); |
458 | ||
b2e1b302 LR |
459 | err = regulatory_init(); |
460 | if (err) | |
461 | goto out_fail_reg; | |
462 | ||
704232c2 JB |
463 | return 0; |
464 | ||
b2e1b302 LR |
465 | out_fail_reg: |
466 | debugfs_remove(ieee80211_debugfs_dir); | |
55682965 JB |
467 | out_fail_nl80211: |
468 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); | |
704232c2 JB |
469 | out_fail_notifier: |
470 | wiphy_sysfs_exit(); | |
471 | out_fail_sysfs: | |
472 | return err; | |
473 | } | |
b2e1b302 | 474 | |
3a462465 | 475 | subsys_initcall(cfg80211_init); |
704232c2 JB |
476 | |
477 | static void cfg80211_exit(void) | |
478 | { | |
479 | debugfs_remove(ieee80211_debugfs_dir); | |
55682965 | 480 | nl80211_exit(); |
704232c2 JB |
481 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); |
482 | wiphy_sysfs_exit(); | |
b2e1b302 | 483 | regulatory_exit(); |
704232c2 JB |
484 | } |
485 | module_exit(cfg80211_exit); |