blob: 39e3d10fccdef5cf49761227e71868a88b997e79 [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"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070022#include "reg.h"
Johannes Berg704232c2007-04-23 12:20:05 -070023
24/* name for sysfs, %d is appended */
25#define PHY_NAME "phy"
26
27MODULE_AUTHOR("Johannes Berg");
28MODULE_LICENSE("GPL");
29MODULE_DESCRIPTION("wireless configuration support");
30
31/* RCU might be appropriate here since we usually
32 * only read the list, and that can happen quite
33 * often because we need to do it for each command */
34LIST_HEAD(cfg80211_drv_list);
35DEFINE_MUTEX(cfg80211_drv_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -070036
37/* for debugfs */
38static struct dentry *ieee80211_debugfs_dir;
39
Johannes Berg55682962007-09-20 13:09:35 -040040/* requires cfg80211_drv_mutex to be held! */
41static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
42{
43 struct cfg80211_registered_device *result = NULL, *drv;
44
45 list_for_each_entry(drv, &cfg80211_drv_list, list) {
46 if (drv->idx == wiphy) {
47 result = drv;
48 break;
49 }
50 }
51
52 return result;
53}
54
55/* requires cfg80211_drv_mutex to be held! */
56static struct cfg80211_registered_device *
57__cfg80211_drv_from_info(struct genl_info *info)
58{
59 int ifindex;
60 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
61 struct net_device *dev;
62 int err = -EINVAL;
63
64 if (info->attrs[NL80211_ATTR_WIPHY]) {
65 bywiphy = cfg80211_drv_by_wiphy(
66 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
67 err = -ENODEV;
68 }
69
70 if (info->attrs[NL80211_ATTR_IFINDEX]) {
71 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
72 dev = dev_get_by_index(&init_net, ifindex);
73 if (dev) {
74 if (dev->ieee80211_ptr)
75 byifidx =
76 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
77 dev_put(dev);
78 }
79 err = -ENODEV;
80 }
81
82 if (bywiphy && byifidx) {
83 if (bywiphy != byifidx)
84 return ERR_PTR(-EINVAL);
85 else
86 return bywiphy; /* == byifidx */
87 }
88 if (bywiphy)
89 return bywiphy;
90
91 if (byifidx)
92 return byifidx;
93
94 return ERR_PTR(err);
95}
96
97struct cfg80211_registered_device *
98cfg80211_get_dev_from_info(struct genl_info *info)
99{
100 struct cfg80211_registered_device *drv;
101
102 mutex_lock(&cfg80211_drv_mutex);
103 drv = __cfg80211_drv_from_info(info);
104
105 /* if it is not an error we grab the lock on
106 * it to assure it won't be going away while
107 * we operate on it */
108 if (!IS_ERR(drv))
109 mutex_lock(&drv->mtx);
110
111 mutex_unlock(&cfg80211_drv_mutex);
112
113 return drv;
114}
115
116struct cfg80211_registered_device *
117cfg80211_get_dev_from_ifindex(int ifindex)
118{
119 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
120 struct net_device *dev;
121
122 mutex_lock(&cfg80211_drv_mutex);
123 dev = dev_get_by_index(&init_net, ifindex);
124 if (!dev)
125 goto out;
126 if (dev->ieee80211_ptr) {
127 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
128 mutex_lock(&drv->mtx);
129 } else
130 drv = ERR_PTR(-ENODEV);
131 dev_put(dev);
132 out:
133 mutex_unlock(&cfg80211_drv_mutex);
134 return drv;
135}
136
137void cfg80211_put_dev(struct cfg80211_registered_device *drv)
138{
139 BUG_ON(IS_ERR(drv));
140 mutex_unlock(&drv->mtx);
141}
142
143int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
144 char *newname)
145{
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700146 struct cfg80211_registered_device *drv;
Johannes Berg55682962007-09-20 13:09:35 -0400147 int idx, taken = -1, result, digits;
148
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700149 mutex_lock(&cfg80211_drv_mutex);
150
Johannes Berg55682962007-09-20 13:09:35 -0400151 /* prohibit calling the thing phy%d when %d is not its number */
152 sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
153 if (taken == strlen(newname) && idx != rdev->idx) {
154 /* count number of places needed to print idx */
155 digits = 1;
156 while (idx /= 10)
157 digits++;
158 /*
159 * deny the name if it is phy<idx> where <idx> is printed
160 * without leading zeroes. taken == strlen(newname) here
161 */
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700162 result = -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -0400163 if (taken == strlen(PHY_NAME) + digits)
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700164 goto out_unlock;
Johannes Berg55682962007-09-20 13:09:35 -0400165 }
166
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700167
168 /* Ignore nop renames */
169 result = 0;
170 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
171 goto out_unlock;
172
173 /* Ensure another device does not already have this name. */
174 list_for_each_entry(drv, &cfg80211_drv_list, list) {
175 result = -EINVAL;
176 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
177 goto out_unlock;
178 }
179
180 /* this will only check for collisions in sysfs
181 * which is not even always compiled in.
182 */
Johannes Berg55682962007-09-20 13:09:35 -0400183 result = device_rename(&rdev->wiphy.dev, newname);
184 if (result)
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700185 goto out_unlock;
Johannes Berg55682962007-09-20 13:09:35 -0400186
Johannes Berg33c03602008-10-08 10:23:48 +0200187 if (rdev->wiphy.debugfsdir &&
188 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
Johannes Berg55682962007-09-20 13:09:35 -0400189 rdev->wiphy.debugfsdir,
190 rdev->wiphy.debugfsdir->d_parent,
191 newname))
192 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
193 newname);
194
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700195 result = 0;
196out_unlock:
197 mutex_unlock(&cfg80211_drv_mutex);
198 if (result == 0)
199 nl80211_notify_dev_rename(rdev);
Johannes Berg55682962007-09-20 13:09:35 -0400200
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700201 return result;
Johannes Berg55682962007-09-20 13:09:35 -0400202}
203
Johannes Berg704232c2007-04-23 12:20:05 -0700204/* exported functions */
205
206struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
207{
Denis ChengRq638af072008-09-23 02:35:37 +0800208 static int wiphy_counter;
209
Johannes Berg704232c2007-04-23 12:20:05 -0700210 struct cfg80211_registered_device *drv;
211 int alloc_size;
212
Johannes Berg41ade002007-12-19 02:03:29 +0100213 WARN_ON(!ops->add_key && ops->del_key);
214 WARN_ON(ops->add_key && !ops->del_key);
215
Johannes Berg704232c2007-04-23 12:20:05 -0700216 alloc_size = sizeof(*drv) + sizeof_priv;
217
218 drv = kzalloc(alloc_size, GFP_KERNEL);
219 if (!drv)
220 return NULL;
221
222 drv->ops = ops;
223
224 mutex_lock(&cfg80211_drv_mutex);
225
Denis ChengRq638af072008-09-23 02:35:37 +0800226 drv->idx = wiphy_counter++;
Johannes Berga4d73ee2007-04-26 20:50:35 -0700227
228 if (unlikely(drv->idx < 0)) {
Denis ChengRq638af072008-09-23 02:35:37 +0800229 wiphy_counter--;
230 mutex_unlock(&cfg80211_drv_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700231 /* ugh, wrapped! */
232 kfree(drv);
233 return NULL;
234 }
Johannes Berg704232c2007-04-23 12:20:05 -0700235
Denis ChengRq638af072008-09-23 02:35:37 +0800236 mutex_unlock(&cfg80211_drv_mutex);
237
Johannes Berg704232c2007-04-23 12:20:05 -0700238 /* give it a proper name */
Kay Sieversfb28ad32008-11-10 13:55:14 -0800239 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->idx);
Johannes Berg704232c2007-04-23 12:20:05 -0700240
Johannes Berg704232c2007-04-23 12:20:05 -0700241 mutex_init(&drv->mtx);
242 mutex_init(&drv->devlist_mtx);
243 INIT_LIST_HEAD(&drv->netdev_list);
244
245 device_initialize(&drv->wiphy.dev);
246 drv->wiphy.dev.class = &ieee80211_class;
247 drv->wiphy.dev.platform_data = drv;
248
249 return &drv->wiphy;
250}
251EXPORT_SYMBOL(wiphy_new);
252
253int wiphy_register(struct wiphy *wiphy)
254{
255 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
256 int res;
Johannes Berg8318d782008-01-24 19:38:38 +0100257 enum ieee80211_band band;
258 struct ieee80211_supported_band *sband;
259 bool have_band = false;
260 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700261 u16 ifmodes = wiphy->interface_modes;
262
263 /* sanity check ifmodes */
264 WARN_ON(!ifmodes);
265 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
266 if (WARN_ON(ifmodes != wiphy->interface_modes))
267 wiphy->interface_modes = ifmodes;
Johannes Berg8318d782008-01-24 19:38:38 +0100268
269 /* sanity check supported bands/channels */
270 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
271 sband = wiphy->bands[band];
272 if (!sband)
273 continue;
274
275 sband->band = band;
276
277 if (!sband->n_channels || !sband->n_bitrates) {
278 WARN_ON(1);
279 return -EINVAL;
280 }
281
282 for (i = 0; i < sband->n_channels; i++) {
283 sband->channels[i].orig_flags =
284 sband->channels[i].flags;
285 sband->channels[i].orig_mag =
286 sband->channels[i].max_antenna_gain;
287 sband->channels[i].orig_mpwr =
288 sband->channels[i].max_power;
289 sband->channels[i].band = band;
290 }
291
292 have_band = true;
293 }
294
295 if (!have_band) {
296 WARN_ON(1);
297 return -EINVAL;
298 }
299
300 /* check and set up bitrates */
301 ieee80211_set_bitrate_flags(wiphy);
302
Johannes Berg704232c2007-04-23 12:20:05 -0700303 mutex_lock(&cfg80211_drv_mutex);
304
Johannes Bergf3b407f2008-10-21 09:57:41 +0200305 /* set up regulatory info */
306 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
307
Johannes Berg704232c2007-04-23 12:20:05 -0700308 res = device_add(&drv->wiphy.dev);
309 if (res)
310 goto out_unlock;
311
312 list_add(&drv->list, &cfg80211_drv_list);
313
314 /* add to debugfs */
315 drv->wiphy.debugfsdir =
316 debugfs_create_dir(wiphy_name(&drv->wiphy),
317 ieee80211_debugfs_dir);
Johannes Berg33c03602008-10-08 10:23:48 +0200318 if (IS_ERR(drv->wiphy.debugfsdir))
319 drv->wiphy.debugfsdir = NULL;
Johannes Berg704232c2007-04-23 12:20:05 -0700320
321 res = 0;
322out_unlock:
323 mutex_unlock(&cfg80211_drv_mutex);
324 return res;
325}
326EXPORT_SYMBOL(wiphy_register);
327
328void wiphy_unregister(struct wiphy *wiphy)
329{
330 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
331
Johannes Bergf16bfc12007-04-26 20:51:12 -0700332 /* protect the device list */
Johannes Berg704232c2007-04-23 12:20:05 -0700333 mutex_lock(&cfg80211_drv_mutex);
334
Johannes Bergf16bfc12007-04-26 20:51:12 -0700335 BUG_ON(!list_empty(&drv->netdev_list));
336
337 /*
338 * Try to grab drv->mtx. If a command is still in progress,
339 * hopefully the driver will refuse it since it's tearing
340 * down the device already. We wait for this command to complete
341 * before unlinking the item from the list.
342 * Note: as codified by the BUG_ON above we cannot get here if
343 * a virtual interface is still associated. Hence, we can only
344 * get to lock contention here if userspace issues a command
345 * that identified the hardware by wiphy index.
346 */
Johannes Berg704232c2007-04-23 12:20:05 -0700347 mutex_lock(&drv->mtx);
Johannes Bergf16bfc12007-04-26 20:51:12 -0700348 /* unlock again before freeing */
Johannes Berg704232c2007-04-23 12:20:05 -0700349 mutex_unlock(&drv->mtx);
350
Johannes Bergf16bfc12007-04-26 20:51:12 -0700351 list_del(&drv->list);
Johannes Berg704232c2007-04-23 12:20:05 -0700352 device_del(&drv->wiphy.dev);
353 debugfs_remove(drv->wiphy.debugfsdir);
354
355 mutex_unlock(&cfg80211_drv_mutex);
356}
357EXPORT_SYMBOL(wiphy_unregister);
358
359void cfg80211_dev_free(struct cfg80211_registered_device *drv)
360{
361 mutex_destroy(&drv->mtx);
362 mutex_destroy(&drv->devlist_mtx);
363 kfree(drv);
364}
365
366void wiphy_free(struct wiphy *wiphy)
367{
368 put_device(&wiphy->dev);
369}
370EXPORT_SYMBOL(wiphy_free);
371
372static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
373 unsigned long state,
374 void *ndev)
375{
376 struct net_device *dev = ndev;
377 struct cfg80211_registered_device *rdev;
378
379 if (!dev->ieee80211_ptr)
380 return 0;
381
382 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
383
Johannes Berg60719ff2008-09-16 14:55:09 +0200384 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
385
Johannes Berg704232c2007-04-23 12:20:05 -0700386 switch (state) {
387 case NETDEV_REGISTER:
388 mutex_lock(&rdev->devlist_mtx);
389 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
390 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
391 "phy80211")) {
392 printk(KERN_ERR "wireless: failed to add phy80211 "
393 "symlink to netdev!\n");
394 }
395 dev->ieee80211_ptr->netdev = dev;
396 mutex_unlock(&rdev->devlist_mtx);
397 break;
398 case NETDEV_UNREGISTER:
399 mutex_lock(&rdev->devlist_mtx);
400 if (!list_empty(&dev->ieee80211_ptr->list)) {
401 sysfs_remove_link(&dev->dev.kobj, "phy80211");
402 list_del_init(&dev->ieee80211_ptr->list);
403 }
404 mutex_unlock(&rdev->devlist_mtx);
405 break;
406 }
407
408 return 0;
409}
410
411static struct notifier_block cfg80211_netdev_notifier = {
412 .notifier_call = cfg80211_netdev_notifier_call,
413};
414
415static int cfg80211_init(void)
416{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700417 int err;
418
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700419 err = wiphy_sysfs_init();
Johannes Berg704232c2007-04-23 12:20:05 -0700420 if (err)
421 goto out_fail_sysfs;
422
423 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
424 if (err)
425 goto out_fail_notifier;
426
Johannes Berg55682962007-09-20 13:09:35 -0400427 err = nl80211_init();
428 if (err)
429 goto out_fail_nl80211;
430
Johannes Berg704232c2007-04-23 12:20:05 -0700431 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
432
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700433 err = regulatory_init();
434 if (err)
435 goto out_fail_reg;
436
Johannes Berg704232c2007-04-23 12:20:05 -0700437 return 0;
438
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700439out_fail_reg:
440 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400441out_fail_nl80211:
442 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
Johannes Berg704232c2007-04-23 12:20:05 -0700443out_fail_notifier:
444 wiphy_sysfs_exit();
445out_fail_sysfs:
446 return err;
447}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700448
Johannes Berg3a462462007-09-10 13:44:45 +0200449subsys_initcall(cfg80211_init);
Johannes Berg704232c2007-04-23 12:20:05 -0700450
451static void cfg80211_exit(void)
452{
453 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400454 nl80211_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700455 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
456 wiphy_sysfs_exit();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700457 regulatory_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700458}
459module_exit(cfg80211_exit);