blob: 5cadbeb76a1430ca30d0b1faff578f83807057e5 [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>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070016#include <linux/list.h>
Johannes Berg704232c2007-04-23 12:20:05 -070017#include <net/genetlink.h>
18#include <net/cfg80211.h>
19#include <net/wireless.h>
Johannes Berg55682962007-09-20 13:09:35 -040020#include "nl80211.h"
Johannes Berg704232c2007-04-23 12:20:05 -070021#include "core.h"
22#include "sysfs.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070023#include "reg.h"
Johannes Berg704232c2007-04-23 12:20:05 -070024
25/* name for sysfs, %d is appended */
26#define PHY_NAME "phy"
27
28MODULE_AUTHOR("Johannes Berg");
29MODULE_LICENSE("GPL");
30MODULE_DESCRIPTION("wireless configuration support");
31
32/* RCU might be appropriate here since we usually
33 * only read the list, and that can happen quite
34 * often because we need to do it for each command */
35LIST_HEAD(cfg80211_drv_list);
36DEFINE_MUTEX(cfg80211_drv_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -070037
38/* for debugfs */
39static struct dentry *ieee80211_debugfs_dir;
40
Johannes Berg55682962007-09-20 13:09:35 -040041/* requires cfg80211_drv_mutex to be held! */
42static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
43{
44 struct cfg80211_registered_device *result = NULL, *drv;
45
46 list_for_each_entry(drv, &cfg80211_drv_list, list) {
47 if (drv->idx == wiphy) {
48 result = drv;
49 break;
50 }
51 }
52
53 return result;
54}
55
56/* requires cfg80211_drv_mutex to be held! */
57static struct cfg80211_registered_device *
58__cfg80211_drv_from_info(struct genl_info *info)
59{
60 int ifindex;
61 struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
62 struct net_device *dev;
63 int err = -EINVAL;
64
65 if (info->attrs[NL80211_ATTR_WIPHY]) {
66 bywiphy = cfg80211_drv_by_wiphy(
67 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
68 err = -ENODEV;
69 }
70
71 if (info->attrs[NL80211_ATTR_IFINDEX]) {
72 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
73 dev = dev_get_by_index(&init_net, ifindex);
74 if (dev) {
75 if (dev->ieee80211_ptr)
76 byifidx =
77 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
78 dev_put(dev);
79 }
80 err = -ENODEV;
81 }
82
83 if (bywiphy && byifidx) {
84 if (bywiphy != byifidx)
85 return ERR_PTR(-EINVAL);
86 else
87 return bywiphy; /* == byifidx */
88 }
89 if (bywiphy)
90 return bywiphy;
91
92 if (byifidx)
93 return byifidx;
94
95 return ERR_PTR(err);
96}
97
98struct cfg80211_registered_device *
99cfg80211_get_dev_from_info(struct genl_info *info)
100{
101 struct cfg80211_registered_device *drv;
102
103 mutex_lock(&cfg80211_drv_mutex);
104 drv = __cfg80211_drv_from_info(info);
105
106 /* if it is not an error we grab the lock on
107 * it to assure it won't be going away while
108 * we operate on it */
109 if (!IS_ERR(drv))
110 mutex_lock(&drv->mtx);
111
112 mutex_unlock(&cfg80211_drv_mutex);
113
114 return drv;
115}
116
117struct cfg80211_registered_device *
118cfg80211_get_dev_from_ifindex(int ifindex)
119{
120 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
121 struct net_device *dev;
122
123 mutex_lock(&cfg80211_drv_mutex);
124 dev = dev_get_by_index(&init_net, ifindex);
125 if (!dev)
126 goto out;
127 if (dev->ieee80211_ptr) {
128 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
129 mutex_lock(&drv->mtx);
130 } else
131 drv = ERR_PTR(-ENODEV);
132 dev_put(dev);
133 out:
134 mutex_unlock(&cfg80211_drv_mutex);
135 return drv;
136}
137
138void cfg80211_put_dev(struct cfg80211_registered_device *drv)
139{
140 BUG_ON(IS_ERR(drv));
141 mutex_unlock(&drv->mtx);
142}
143
144int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
145 char *newname)
146{
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700147 struct cfg80211_registered_device *drv;
Johannes Berg55682962007-09-20 13:09:35 -0400148 int idx, taken = -1, result, digits;
149
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700150 mutex_lock(&cfg80211_drv_mutex);
151
Johannes Berg55682962007-09-20 13:09:35 -0400152 /* prohibit calling the thing phy%d when %d is not its number */
153 sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
154 if (taken == strlen(newname) && idx != rdev->idx) {
155 /* count number of places needed to print idx */
156 digits = 1;
157 while (idx /= 10)
158 digits++;
159 /*
160 * deny the name if it is phy<idx> where <idx> is printed
161 * without leading zeroes. taken == strlen(newname) here
162 */
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700163 result = -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -0400164 if (taken == strlen(PHY_NAME) + digits)
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700165 goto out_unlock;
Johannes Berg55682962007-09-20 13:09:35 -0400166 }
167
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700168
169 /* Ignore nop renames */
170 result = 0;
171 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
172 goto out_unlock;
173
174 /* Ensure another device does not already have this name. */
175 list_for_each_entry(drv, &cfg80211_drv_list, list) {
176 result = -EINVAL;
177 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
178 goto out_unlock;
179 }
180
181 /* this will only check for collisions in sysfs
182 * which is not even always compiled in.
183 */
Johannes Berg55682962007-09-20 13:09:35 -0400184 result = device_rename(&rdev->wiphy.dev, newname);
185 if (result)
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700186 goto out_unlock;
Johannes Berg55682962007-09-20 13:09:35 -0400187
188 if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
189 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 */
239 snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
240 PHY_NAME "%d", drv->idx);
241
Johannes Berg704232c2007-04-23 12:20:05 -0700242 mutex_init(&drv->mtx);
243 mutex_init(&drv->devlist_mtx);
244 INIT_LIST_HEAD(&drv->netdev_list);
245
246 device_initialize(&drv->wiphy.dev);
247 drv->wiphy.dev.class = &ieee80211_class;
248 drv->wiphy.dev.platform_data = drv;
249
250 return &drv->wiphy;
251}
252EXPORT_SYMBOL(wiphy_new);
253
254int wiphy_register(struct wiphy *wiphy)
255{
256 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
257 int res;
Johannes Berg8318d782008-01-24 19:38:38 +0100258 enum ieee80211_band band;
259 struct ieee80211_supported_band *sband;
260 bool have_band = false;
261 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700262 u16 ifmodes = wiphy->interface_modes;
263
264 /* sanity check ifmodes */
265 WARN_ON(!ifmodes);
266 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
267 if (WARN_ON(ifmodes != wiphy->interface_modes))
268 wiphy->interface_modes = ifmodes;
Johannes Berg8318d782008-01-24 19:38:38 +0100269
270 /* sanity check supported bands/channels */
271 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
272 sband = wiphy->bands[band];
273 if (!sband)
274 continue;
275
276 sband->band = band;
277
278 if (!sband->n_channels || !sband->n_bitrates) {
279 WARN_ON(1);
280 return -EINVAL;
281 }
282
283 for (i = 0; i < sband->n_channels; i++) {
284 sband->channels[i].orig_flags =
285 sband->channels[i].flags;
286 sband->channels[i].orig_mag =
287 sband->channels[i].max_antenna_gain;
288 sband->channels[i].orig_mpwr =
289 sband->channels[i].max_power;
290 sband->channels[i].band = band;
291 }
292
293 have_band = true;
294 }
295
296 if (!have_band) {
297 WARN_ON(1);
298 return -EINVAL;
299 }
300
301 /* check and set up bitrates */
302 ieee80211_set_bitrate_flags(wiphy);
303
304 /* set up regulatory info */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700305 mutex_lock(&cfg80211_reg_mutex);
306 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
307 mutex_unlock(&cfg80211_reg_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700308
309 mutex_lock(&cfg80211_drv_mutex);
310
Johannes Berg704232c2007-04-23 12:20:05 -0700311 res = device_add(&drv->wiphy.dev);
312 if (res)
313 goto out_unlock;
314
315 list_add(&drv->list, &cfg80211_drv_list);
316
317 /* add to debugfs */
318 drv->wiphy.debugfsdir =
319 debugfs_create_dir(wiphy_name(&drv->wiphy),
320 ieee80211_debugfs_dir);
321
322 res = 0;
323out_unlock:
324 mutex_unlock(&cfg80211_drv_mutex);
325 return res;
326}
327EXPORT_SYMBOL(wiphy_register);
328
329void wiphy_unregister(struct wiphy *wiphy)
330{
331 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
332
Johannes Bergf16bfc12007-04-26 20:51:12 -0700333 /* protect the device list */
Johannes Berg704232c2007-04-23 12:20:05 -0700334 mutex_lock(&cfg80211_drv_mutex);
335
Johannes Bergf16bfc12007-04-26 20:51:12 -0700336 BUG_ON(!list_empty(&drv->netdev_list));
337
338 /*
339 * Try to grab drv->mtx. If a command is still in progress,
340 * hopefully the driver will refuse it since it's tearing
341 * down the device already. We wait for this command to complete
342 * before unlinking the item from the list.
343 * Note: as codified by the BUG_ON above we cannot get here if
344 * a virtual interface is still associated. Hence, we can only
345 * get to lock contention here if userspace issues a command
346 * that identified the hardware by wiphy index.
347 */
Johannes Berg704232c2007-04-23 12:20:05 -0700348 mutex_lock(&drv->mtx);
Johannes Bergf16bfc12007-04-26 20:51:12 -0700349 /* unlock again before freeing */
Johannes Berg704232c2007-04-23 12:20:05 -0700350 mutex_unlock(&drv->mtx);
351
Johannes Bergf16bfc12007-04-26 20:51:12 -0700352 list_del(&drv->list);
Johannes Berg704232c2007-04-23 12:20:05 -0700353 device_del(&drv->wiphy.dev);
354 debugfs_remove(drv->wiphy.debugfsdir);
355
356 mutex_unlock(&cfg80211_drv_mutex);
357}
358EXPORT_SYMBOL(wiphy_unregister);
359
360void cfg80211_dev_free(struct cfg80211_registered_device *drv)
361{
362 mutex_destroy(&drv->mtx);
363 mutex_destroy(&drv->devlist_mtx);
364 kfree(drv);
365}
366
367void wiphy_free(struct wiphy *wiphy)
368{
369 put_device(&wiphy->dev);
370}
371EXPORT_SYMBOL(wiphy_free);
372
373static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
374 unsigned long state,
375 void *ndev)
376{
377 struct net_device *dev = ndev;
378 struct cfg80211_registered_device *rdev;
379
380 if (!dev->ieee80211_ptr)
381 return 0;
382
383 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
384
Johannes Berg60719ff2008-09-16 14:55:09 +0200385 WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
386
Johannes Berg704232c2007-04-23 12:20:05 -0700387 switch (state) {
388 case NETDEV_REGISTER:
389 mutex_lock(&rdev->devlist_mtx);
390 list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
391 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
392 "phy80211")) {
393 printk(KERN_ERR "wireless: failed to add phy80211 "
394 "symlink to netdev!\n");
395 }
396 dev->ieee80211_ptr->netdev = dev;
397 mutex_unlock(&rdev->devlist_mtx);
398 break;
399 case NETDEV_UNREGISTER:
400 mutex_lock(&rdev->devlist_mtx);
401 if (!list_empty(&dev->ieee80211_ptr->list)) {
402 sysfs_remove_link(&dev->dev.kobj, "phy80211");
403 list_del_init(&dev->ieee80211_ptr->list);
404 }
405 mutex_unlock(&rdev->devlist_mtx);
406 break;
407 }
408
409 return 0;
410}
411
412static struct notifier_block cfg80211_netdev_notifier = {
413 .notifier_call = cfg80211_netdev_notifier_call,
414};
415
416static int cfg80211_init(void)
417{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700418 int err;
419
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700420 err = wiphy_sysfs_init();
Johannes Berg704232c2007-04-23 12:20:05 -0700421 if (err)
422 goto out_fail_sysfs;
423
424 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
425 if (err)
426 goto out_fail_notifier;
427
Johannes Berg55682962007-09-20 13:09:35 -0400428 err = nl80211_init();
429 if (err)
430 goto out_fail_nl80211;
431
Johannes Berg704232c2007-04-23 12:20:05 -0700432 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
433
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700434 err = regulatory_init();
435 if (err)
436 goto out_fail_reg;
437
Johannes Berg704232c2007-04-23 12:20:05 -0700438 return 0;
439
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700440out_fail_reg:
441 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400442out_fail_nl80211:
443 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
Johannes Berg704232c2007-04-23 12:20:05 -0700444out_fail_notifier:
445 wiphy_sysfs_exit();
446out_fail_sysfs:
447 return err;
448}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700449
Johannes Berg3a462462007-09-10 13:44:45 +0200450subsys_initcall(cfg80211_init);
Johannes Berg704232c2007-04-23 12:20:05 -0700451
452static void cfg80211_exit(void)
453{
454 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400455 nl80211_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700456 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
457 wiphy_sysfs_exit();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700458 regulatory_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700459}
460module_exit(cfg80211_exit);