blob: 35d457b2751e2724d42e4451e11cf5e435ad6df5 [file] [log] [blame]
Johannes Berg704232c2007-04-23 12:20:05 -07001/*
2 * This is the linux wireless configuration interface.
3 *
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07004 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg704232c2007-04-23 12:20:05 -07005 */
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 Berg55682962007-09-20 13:09:35 -040019#include "nl80211.h"
Johannes Berg704232c2007-04-23 12:20:05 -070020#include "core.h"
21#include "sysfs.h"
22
23/* name for sysfs, %d is appended */
24#define PHY_NAME "phy"
25
26MODULE_AUTHOR("Johannes Berg");
27MODULE_LICENSE("GPL");
28MODULE_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 */
33LIST_HEAD(cfg80211_drv_list);
34DEFINE_MUTEX(cfg80211_drv_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -070035
36/* for debugfs */
37static struct dentry *ieee80211_debugfs_dir;
38
Johannes Berg55682962007-09-20 13:09:35 -040039/* requires cfg80211_drv_mutex to be held! */
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050040static struct cfg80211_registered_device *
41cfg80211_drv_by_wiphy_idx(int wiphy_idx)
Johannes Berg55682962007-09-20 13:09:35 -040042{
43 struct cfg80211_registered_device *result = NULL, *drv;
44
Luis R. Rodriguez85fd1292009-02-21 00:04:20 -050045 if (!wiphy_idx_valid(wiphy_idx))
46 return NULL;
47
Johannes Berg55682962007-09-20 13:09:35 -040048 list_for_each_entry(drv, &cfg80211_drv_list, list) {
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050049 if (drv->wiphy_idx == wiphy_idx) {
Johannes Berg55682962007-09-20 13:09:35 -040050 result = drv;
51 break;
52 }
53 }
54
55 return result;
56}
57
58/* requires cfg80211_drv_mutex to be held! */
59static struct cfg80211_registered_device *
60__cfg80211_drv_from_info(struct genl_info *info)
61{
62 int ifindex;
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050063 struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
Johannes Berg55682962007-09-20 13:09:35 -040064 struct net_device *dev;
65 int err = -EINVAL;
66
67 if (info->attrs[NL80211_ATTR_WIPHY]) {
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050068 bywiphyidx = cfg80211_drv_by_wiphy_idx(
Johannes Berg55682962007-09-20 13:09:35 -040069 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
70 err = -ENODEV;
71 }
72
73 if (info->attrs[NL80211_ATTR_IFINDEX]) {
74 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
75 dev = dev_get_by_index(&init_net, ifindex);
76 if (dev) {
77 if (dev->ieee80211_ptr)
78 byifidx =
79 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
80 dev_put(dev);
81 }
82 err = -ENODEV;
83 }
84
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050085 if (bywiphyidx && byifidx) {
86 if (bywiphyidx != byifidx)
Johannes Berg55682962007-09-20 13:09:35 -040087 return ERR_PTR(-EINVAL);
88 else
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050089 return bywiphyidx; /* == byifidx */
Johannes Berg55682962007-09-20 13:09:35 -040090 }
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050091 if (bywiphyidx)
92 return bywiphyidx;
Johannes Berg55682962007-09-20 13:09:35 -040093
94 if (byifidx)
95 return byifidx;
96
97 return ERR_PTR(err);
98}
99
100struct cfg80211_registered_device *
101cfg80211_get_dev_from_info(struct genl_info *info)
102{
103 struct cfg80211_registered_device *drv;
104
105 mutex_lock(&cfg80211_drv_mutex);
106 drv = __cfg80211_drv_from_info(info);
107
108 /* if it is not an error we grab the lock on
109 * it to assure it won't be going away while
110 * we operate on it */
111 if (!IS_ERR(drv))
112 mutex_lock(&drv->mtx);
113
114 mutex_unlock(&cfg80211_drv_mutex);
115
116 return drv;
117}
118
119struct cfg80211_registered_device *
120cfg80211_get_dev_from_ifindex(int ifindex)
121{
122 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
123 struct net_device *dev;
124
125 mutex_lock(&cfg80211_drv_mutex);
126 dev = dev_get_by_index(&init_net, ifindex);
127 if (!dev)
128 goto out;
129 if (dev->ieee80211_ptr) {
130 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
131 mutex_lock(&drv->mtx);
132 } else
133 drv = ERR_PTR(-ENODEV);
134 dev_put(dev);
135 out:
136 mutex_unlock(&cfg80211_drv_mutex);
137 return drv;
138}
139
140void cfg80211_put_dev(struct cfg80211_registered_device *drv)
141{
142 BUG_ON(IS_ERR(drv));
143 mutex_unlock(&drv->mtx);
144}
145
146int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
147 char *newname)
148{
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700149 struct cfg80211_registered_device *drv;
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500150 int wiphy_idx, taken = -1, result, digits;
Johannes Berg55682962007-09-20 13:09:35 -0400151
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700152 mutex_lock(&cfg80211_drv_mutex);
153
Johannes Berg55682962007-09-20 13:09:35 -0400154 /* prohibit calling the thing phy%d when %d is not its number */
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500155 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
156 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
157 /* count number of places needed to print wiphy_idx */
Johannes Berg55682962007-09-20 13:09:35 -0400158 digits = 1;
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500159 while (wiphy_idx /= 10)
Johannes Berg55682962007-09-20 13:09:35 -0400160 digits++;
161 /*
162 * deny the name if it is phy<idx> where <idx> is printed
163 * without leading zeroes. taken == strlen(newname) here
164 */
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700165 result = -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -0400166 if (taken == strlen(PHY_NAME) + digits)
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700167 goto out_unlock;
Johannes Berg55682962007-09-20 13:09:35 -0400168 }
169
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700170
171 /* Ignore nop renames */
172 result = 0;
173 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
174 goto out_unlock;
175
176 /* Ensure another device does not already have this name. */
177 list_for_each_entry(drv, &cfg80211_drv_list, list) {
178 result = -EINVAL;
179 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
180 goto out_unlock;
181 }
182
183 /* this will only check for collisions in sysfs
184 * which is not even always compiled in.
185 */
Johannes Berg55682962007-09-20 13:09:35 -0400186 result = device_rename(&rdev->wiphy.dev, newname);
187 if (result)
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700188 goto out_unlock;
Johannes Berg55682962007-09-20 13:09:35 -0400189
Johannes Berg33c03602008-10-08 10:23:48 +0200190 if (rdev->wiphy.debugfsdir &&
191 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
Johannes Berg55682962007-09-20 13:09:35 -0400192 rdev->wiphy.debugfsdir,
193 rdev->wiphy.debugfsdir->d_parent,
194 newname))
195 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
196 newname);
197
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700198 result = 0;
199out_unlock:
200 mutex_unlock(&cfg80211_drv_mutex);
201 if (result == 0)
202 nl80211_notify_dev_rename(rdev);
Johannes Berg55682962007-09-20 13:09:35 -0400203
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700204 return result;
Johannes Berg55682962007-09-20 13:09:35 -0400205}
206
Johannes Berg704232c2007-04-23 12:20:05 -0700207/* exported functions */
208
209struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
210{
Denis ChengRq638af072008-09-23 02:35:37 +0800211 static int wiphy_counter;
212
Johannes Berg704232c2007-04-23 12:20:05 -0700213 struct cfg80211_registered_device *drv;
214 int alloc_size;
215
Johannes Berg41ade002007-12-19 02:03:29 +0100216 WARN_ON(!ops->add_key && ops->del_key);
217 WARN_ON(ops->add_key && !ops->del_key);
218
Johannes Berg704232c2007-04-23 12:20:05 -0700219 alloc_size = sizeof(*drv) + sizeof_priv;
220
221 drv = kzalloc(alloc_size, GFP_KERNEL);
222 if (!drv)
223 return NULL;
224
225 drv->ops = ops;
226
227 mutex_lock(&cfg80211_drv_mutex);
228
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500229 drv->wiphy_idx = wiphy_counter++;
Johannes Berga4d73ee2007-04-26 20:50:35 -0700230
Luis R. Rodriguez85fd1292009-02-21 00:04:20 -0500231 if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
Denis ChengRq638af072008-09-23 02:35:37 +0800232 wiphy_counter--;
233 mutex_unlock(&cfg80211_drv_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700234 /* ugh, wrapped! */
235 kfree(drv);
236 return NULL;
237 }
Johannes Berg704232c2007-04-23 12:20:05 -0700238
Denis ChengRq638af072008-09-23 02:35:37 +0800239 mutex_unlock(&cfg80211_drv_mutex);
240
Johannes Berg704232c2007-04-23 12:20:05 -0700241 /* give it a proper name */
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500242 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
Johannes Berg704232c2007-04-23 12:20:05 -0700243
Johannes Berg704232c2007-04-23 12:20:05 -0700244 mutex_init(&drv->mtx);
245 mutex_init(&drv->devlist_mtx);
246 INIT_LIST_HEAD(&drv->netdev_list);
Johannes Berg2a519312009-02-10 21:25:55 +0100247 spin_lock_init(&drv->bss_lock);
248 INIT_LIST_HEAD(&drv->bss_list);
Johannes Berg704232c2007-04-23 12:20:05 -0700249
250 device_initialize(&drv->wiphy.dev);
251 drv->wiphy.dev.class = &ieee80211_class;
252 drv->wiphy.dev.platform_data = drv;
253
254 return &drv->wiphy;
255}
256EXPORT_SYMBOL(wiphy_new);
257
258int wiphy_register(struct wiphy *wiphy)
259{
260 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
261 int res;
Johannes Berg8318d782008-01-24 19:38:38 +0100262 enum ieee80211_band band;
263 struct ieee80211_supported_band *sband;
264 bool have_band = false;
265 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700266 u16 ifmodes = wiphy->interface_modes;
267
Johannes Berg2a519312009-02-10 21:25:55 +0100268 if (WARN_ON(wiphy->max_scan_ssids < 1))
269 return -EINVAL;
270
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700271 /* sanity check ifmodes */
272 WARN_ON(!ifmodes);
273 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
274 if (WARN_ON(ifmodes != wiphy->interface_modes))
275 wiphy->interface_modes = ifmodes;
Johannes Berg8318d782008-01-24 19:38:38 +0100276
277 /* sanity check supported bands/channels */
278 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
279 sband = wiphy->bands[band];
280 if (!sband)
281 continue;
282
283 sband->band = band;
284
Johannes Berg881d9482009-01-21 15:13:48 +0100285 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
Johannes Berg8318d782008-01-24 19:38:38 +0100286 return -EINVAL;
Johannes Berg881d9482009-01-21 15:13:48 +0100287
288 /*
289 * Since we use a u32 for rate bitmaps in
290 * ieee80211_get_response_rate, we cannot
291 * have more than 32 legacy rates.
292 */
293 if (WARN_ON(sband->n_bitrates > 32))
294 return -EINVAL;
Johannes Berg8318d782008-01-24 19:38:38 +0100295
296 for (i = 0; i < sband->n_channels; i++) {
297 sband->channels[i].orig_flags =
298 sband->channels[i].flags;
299 sband->channels[i].orig_mag =
300 sband->channels[i].max_antenna_gain;
301 sband->channels[i].orig_mpwr =
302 sband->channels[i].max_power;
303 sband->channels[i].band = band;
304 }
305
306 have_band = true;
307 }
308
309 if (!have_band) {
310 WARN_ON(1);
311 return -EINVAL;
312 }
313
314 /* check and set up bitrates */
315 ieee80211_set_bitrate_flags(wiphy);
316
Johannes Berg704232c2007-04-23 12:20:05 -0700317 mutex_lock(&cfg80211_drv_mutex);
318
Johannes Bergf3b407f2008-10-21 09:57:41 +0200319 /* set up regulatory info */
320 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
321
Johannes Berg704232c2007-04-23 12:20:05 -0700322 res = device_add(&drv->wiphy.dev);
323 if (res)
324 goto out_unlock;
325
326 list_add(&drv->list, &cfg80211_drv_list);
327
328 /* add to debugfs */
329 drv->wiphy.debugfsdir =
330 debugfs_create_dir(wiphy_name(&drv->wiphy),
331 ieee80211_debugfs_dir);
Johannes Berg33c03602008-10-08 10:23:48 +0200332 if (IS_ERR(drv->wiphy.debugfsdir))
333 drv->wiphy.debugfsdir = NULL;
Johannes Berg704232c2007-04-23 12:20:05 -0700334
335 res = 0;
336out_unlock:
337 mutex_unlock(&cfg80211_drv_mutex);
338 return res;
339}
340EXPORT_SYMBOL(wiphy_register);
341
342void wiphy_unregister(struct wiphy *wiphy)
343{
344 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
345
Johannes Bergf16bfc12007-04-26 20:51:12 -0700346 /* protect the device list */
Johannes Berg704232c2007-04-23 12:20:05 -0700347 mutex_lock(&cfg80211_drv_mutex);
348
Johannes Bergf16bfc12007-04-26 20:51:12 -0700349 BUG_ON(!list_empty(&drv->netdev_list));
350
351 /*
352 * Try to grab drv->mtx. If a command is still in progress,
353 * hopefully the driver will refuse it since it's tearing
354 * down the device already. We wait for this command to complete
355 * before unlinking the item from the list.
356 * Note: as codified by the BUG_ON above we cannot get here if
357 * a virtual interface is still associated. Hence, we can only
358 * get to lock contention here if userspace issues a command
359 * that identified the hardware by wiphy index.
360 */
Johannes Berg704232c2007-04-23 12:20:05 -0700361 mutex_lock(&drv->mtx);
Johannes Bergf16bfc12007-04-26 20:51:12 -0700362 /* unlock again before freeing */
Johannes Berg704232c2007-04-23 12:20:05 -0700363 mutex_unlock(&drv->mtx);
364
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800365 /* If this device got a regulatory hint tell core its
366 * free to listen now to a new shiny device regulatory hint */
367 reg_device_remove(wiphy);
368
Johannes Bergf16bfc12007-04-26 20:51:12 -0700369 list_del(&drv->list);
Johannes Berg704232c2007-04-23 12:20:05 -0700370 device_del(&drv->wiphy.dev);
371 debugfs_remove(drv->wiphy.debugfsdir);
372
373 mutex_unlock(&cfg80211_drv_mutex);
374}
375EXPORT_SYMBOL(wiphy_unregister);
376
377void cfg80211_dev_free(struct cfg80211_registered_device *drv)
378{
Johannes Berg2a519312009-02-10 21:25:55 +0100379 struct cfg80211_internal_bss *scan, *tmp;
Johannes Berg704232c2007-04-23 12:20:05 -0700380 mutex_destroy(&drv->mtx);
381 mutex_destroy(&drv->devlist_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +0100382 list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
Johannes Berg78c1c7e2009-02-10 21:25:57 +0100383 cfg80211_put_bss(&scan->pub);
Johannes Berg704232c2007-04-23 12:20:05 -0700384 kfree(drv);
385}
386
387void wiphy_free(struct wiphy *wiphy)
388{
389 put_device(&wiphy->dev);
390}
391EXPORT_SYMBOL(wiphy_free);
392
393static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
394 unsigned long state,
395 void *ndev)
396{
397 struct net_device *dev = ndev;
398 struct cfg80211_registered_device *rdev;
399
400 if (!dev->ieee80211_ptr)
401 return 0;
402
403 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
404
Johannes Berg60719ff2008-09-16 14:55:09 +0200405 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
406
Johannes Berg704232c2007-04-23 12:20:05 -0700407 switch (state) {
408 case NETDEV_REGISTER:
409 mutex_lock(&rdev->devlist_mtx);
410 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
411 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
412 "phy80211")) {
413 printk(KERN_ERR "wireless: failed to add phy80211 "
414 "symlink to netdev!\n");
415 }
416 dev->ieee80211_ptr->netdev = dev;
417 mutex_unlock(&rdev->devlist_mtx);
418 break;
419 case NETDEV_UNREGISTER:
420 mutex_lock(&rdev->devlist_mtx);
421 if (!list_empty(&dev->ieee80211_ptr->list)) {
422 sysfs_remove_link(&dev->dev.kobj, "phy80211");
423 list_del_init(&dev->ieee80211_ptr->list);
424 }
425 mutex_unlock(&rdev->devlist_mtx);
426 break;
427 }
428
429 return 0;
430}
431
432static struct notifier_block cfg80211_netdev_notifier = {
433 .notifier_call = cfg80211_netdev_notifier_call,
434};
435
436static int cfg80211_init(void)
437{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700438 int err;
439
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700440 err = wiphy_sysfs_init();
Johannes Berg704232c2007-04-23 12:20:05 -0700441 if (err)
442 goto out_fail_sysfs;
443
444 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
445 if (err)
446 goto out_fail_notifier;
447
Johannes Berg55682962007-09-20 13:09:35 -0400448 err = nl80211_init();
449 if (err)
450 goto out_fail_nl80211;
451
Johannes Berg704232c2007-04-23 12:20:05 -0700452 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
453
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700454 err = regulatory_init();
455 if (err)
456 goto out_fail_reg;
457
Johannes Berg704232c2007-04-23 12:20:05 -0700458 return 0;
459
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700460out_fail_reg:
461 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400462out_fail_nl80211:
463 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
Johannes Berg704232c2007-04-23 12:20:05 -0700464out_fail_notifier:
465 wiphy_sysfs_exit();
466out_fail_sysfs:
467 return err;
468}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700469
Johannes Berg3a462462007-09-10 13:44:45 +0200470subsys_initcall(cfg80211_init);
Johannes Berg704232c2007-04-23 12:20:05 -0700471
472static void cfg80211_exit(void)
473{
474 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400475 nl80211_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700476 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
477 wiphy_sysfs_exit();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700478 regulatory_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700479}
480module_exit(cfg80211_exit);