blob: c6813beded069d7b5a06b37d0088b360e6f16098 [file] [log] [blame]
Johannes Berg704232c2007-04-23 12:20:05 -07001/*
2 * This is the linux wireless configuration interface.
3 *
Johannes Berg08645122009-05-11 13:54:58 +02004 * Copyright 2006-2009 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>
Johannes Berg704232c2007-04-23 12:20:05 -070010#include <linux/list.h>
11#include <linux/nl80211.h>
12#include <linux/debugfs.h>
13#include <linux/notifier.h>
14#include <linux/device.h>
Johannes Berg1f87f7d2009-06-02 13:01:41 +020015#include <linux/rtnetlink.h>
Johannes Berg704232c2007-04-23 12:20:05 -070016#include <net/genetlink.h>
17#include <net/cfg80211.h>
Johannes Berg55682962007-09-20 13:09:35 -040018#include "nl80211.h"
Johannes Berg704232c2007-04-23 12:20:05 -070019#include "core.h"
20#include "sysfs.h"
Luis R. Rodriguez1ac61302009-05-02 00:37:21 -040021#include "debugfs.h"
Johannes Berg704232c2007-04-23 12:20:05 -070022
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);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -050034
35/*
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050036 * This is used to protect the cfg80211_drv_list, cfg80211_regdomain,
37 * country_ie_regdomain, the reg_beacon_list and the the last regulatory
38 * request receipt (last_request).
Luis R. Rodrigueza1794392009-02-21 00:04:21 -050039 */
40DEFINE_MUTEX(cfg80211_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -070041
42/* for debugfs */
43static struct dentry *ieee80211_debugfs_dir;
44
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050045/* requires cfg80211_mutex to be held! */
46struct cfg80211_registered_device *cfg80211_drv_by_wiphy_idx(int wiphy_idx)
Johannes Berg55682962007-09-20 13:09:35 -040047{
48 struct cfg80211_registered_device *result = NULL, *drv;
49
Luis R. Rodriguez85fd1292009-02-21 00:04:20 -050050 if (!wiphy_idx_valid(wiphy_idx))
51 return NULL;
52
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -050053 assert_cfg80211_lock();
54
Johannes Berg55682962007-09-20 13:09:35 -040055 list_for_each_entry(drv, &cfg80211_drv_list, list) {
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050056 if (drv->wiphy_idx == wiphy_idx) {
Johannes Berg55682962007-09-20 13:09:35 -040057 result = drv;
58 break;
59 }
60 }
61
62 return result;
63}
64
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050065int get_wiphy_idx(struct wiphy *wiphy)
66{
67 struct cfg80211_registered_device *drv;
68 if (!wiphy)
69 return WIPHY_IDX_STALE;
70 drv = wiphy_to_dev(wiphy);
71 return drv->wiphy_idx;
72}
73
74/* requires cfg80211_drv_mutex to be held! */
75struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
76{
77 struct cfg80211_registered_device *drv;
78
79 if (!wiphy_idx_valid(wiphy_idx))
80 return NULL;
81
82 assert_cfg80211_lock();
83
84 drv = cfg80211_drv_by_wiphy_idx(wiphy_idx);
85 if (!drv)
86 return NULL;
87 return &drv->wiphy;
88}
89
Luis R. Rodrigueza1794392009-02-21 00:04:21 -050090/* requires cfg80211_mutex to be held! */
Johannes Berg4bbf4d52009-03-24 09:35:46 +010091struct cfg80211_registered_device *
Johannes Berg55682962007-09-20 13:09:35 -040092__cfg80211_drv_from_info(struct genl_info *info)
93{
94 int ifindex;
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -050095 struct cfg80211_registered_device *bywiphyidx = NULL, *byifidx = NULL;
Johannes Berg55682962007-09-20 13:09:35 -040096 struct net_device *dev;
97 int err = -EINVAL;
98
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -050099 assert_cfg80211_lock();
100
Johannes Berg55682962007-09-20 13:09:35 -0400101 if (info->attrs[NL80211_ATTR_WIPHY]) {
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500102 bywiphyidx = cfg80211_drv_by_wiphy_idx(
Johannes Berg55682962007-09-20 13:09:35 -0400103 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
104 err = -ENODEV;
105 }
106
107 if (info->attrs[NL80211_ATTR_IFINDEX]) {
108 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
109 dev = dev_get_by_index(&init_net, ifindex);
110 if (dev) {
111 if (dev->ieee80211_ptr)
112 byifidx =
113 wiphy_to_dev(dev->ieee80211_ptr->wiphy);
114 dev_put(dev);
115 }
116 err = -ENODEV;
117 }
118
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500119 if (bywiphyidx && byifidx) {
120 if (bywiphyidx != byifidx)
Johannes Berg55682962007-09-20 13:09:35 -0400121 return ERR_PTR(-EINVAL);
122 else
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500123 return bywiphyidx; /* == byifidx */
Johannes Berg55682962007-09-20 13:09:35 -0400124 }
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500125 if (bywiphyidx)
126 return bywiphyidx;
Johannes Berg55682962007-09-20 13:09:35 -0400127
128 if (byifidx)
129 return byifidx;
130
131 return ERR_PTR(err);
132}
133
134struct cfg80211_registered_device *
135cfg80211_get_dev_from_info(struct genl_info *info)
136{
137 struct cfg80211_registered_device *drv;
138
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500139 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400140 drv = __cfg80211_drv_from_info(info);
141
142 /* if it is not an error we grab the lock on
143 * it to assure it won't be going away while
144 * we operate on it */
145 if (!IS_ERR(drv))
146 mutex_lock(&drv->mtx);
147
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500148 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400149
150 return drv;
151}
152
153struct cfg80211_registered_device *
154cfg80211_get_dev_from_ifindex(int ifindex)
155{
156 struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
157 struct net_device *dev;
158
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500159 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400160 dev = dev_get_by_index(&init_net, ifindex);
161 if (!dev)
162 goto out;
163 if (dev->ieee80211_ptr) {
164 drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
165 mutex_lock(&drv->mtx);
166 } else
167 drv = ERR_PTR(-ENODEV);
168 dev_put(dev);
169 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500170 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400171 return drv;
172}
173
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100174/* requires cfg80211_mutex to be held */
Johannes Berg55682962007-09-20 13:09:35 -0400175int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
176 char *newname)
177{
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700178 struct cfg80211_registered_device *drv;
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500179 int wiphy_idx, taken = -1, result, digits;
Johannes Berg55682962007-09-20 13:09:35 -0400180
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100181 assert_cfg80211_lock();
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700182
Johannes Berg55682962007-09-20 13:09:35 -0400183 /* prohibit calling the thing phy%d when %d is not its number */
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500184 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
185 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
186 /* count number of places needed to print wiphy_idx */
Johannes Berg55682962007-09-20 13:09:35 -0400187 digits = 1;
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500188 while (wiphy_idx /= 10)
Johannes Berg55682962007-09-20 13:09:35 -0400189 digits++;
190 /*
191 * deny the name if it is phy<idx> where <idx> is printed
192 * without leading zeroes. taken == strlen(newname) here
193 */
194 if (taken == strlen(PHY_NAME) + digits)
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100195 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -0400196 }
197
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700198
199 /* Ignore nop renames */
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700200 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100201 return 0;
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700202
203 /* Ensure another device does not already have this name. */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100204 list_for_each_entry(drv, &cfg80211_drv_list, list)
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700205 if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100206 return -EINVAL;
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700207
Johannes Berg55682962007-09-20 13:09:35 -0400208 result = device_rename(&rdev->wiphy.dev, newname);
209 if (result)
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100210 return result;
Johannes Berg55682962007-09-20 13:09:35 -0400211
Johannes Berg33c03602008-10-08 10:23:48 +0200212 if (rdev->wiphy.debugfsdir &&
213 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
Johannes Berg55682962007-09-20 13:09:35 -0400214 rdev->wiphy.debugfsdir,
215 rdev->wiphy.debugfsdir->d_parent,
216 newname))
217 printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
218 newname);
219
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100220 nl80211_notify_dev_rename(rdev);
Johannes Berg55682962007-09-20 13:09:35 -0400221
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100222 return 0;
Johannes Berg55682962007-09-20 13:09:35 -0400223}
224
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200225static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
226{
227 struct cfg80211_registered_device *drv = data;
228
229 drv->ops->rfkill_poll(&drv->wiphy);
230}
231
232static int cfg80211_rfkill_set_block(void *data, bool blocked)
233{
234 struct cfg80211_registered_device *drv = data;
235 struct wireless_dev *wdev;
236
237 if (!blocked)
238 return 0;
239
240 rtnl_lock();
241 mutex_lock(&drv->devlist_mtx);
242
243 list_for_each_entry(wdev, &drv->netdev_list, list)
244 dev_close(wdev->netdev);
245
246 mutex_unlock(&drv->devlist_mtx);
247 rtnl_unlock();
248
249 return 0;
250}
251
252static void cfg80211_rfkill_sync_work(struct work_struct *work)
253{
254 struct cfg80211_registered_device *drv;
255
256 drv = container_of(work, struct cfg80211_registered_device, rfkill_sync);
257 cfg80211_rfkill_set_block(drv, rfkill_blocked(drv->rfkill));
258}
259
Johannes Berg704232c2007-04-23 12:20:05 -0700260/* exported functions */
261
David Kilroy3dcf6702009-05-16 23:13:46 +0100262struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
Johannes Berg704232c2007-04-23 12:20:05 -0700263{
Denis ChengRq638af072008-09-23 02:35:37 +0800264 static int wiphy_counter;
265
Johannes Berg704232c2007-04-23 12:20:05 -0700266 struct cfg80211_registered_device *drv;
267 int alloc_size;
268
Johannes Berg41ade002007-12-19 02:03:29 +0100269 WARN_ON(!ops->add_key && ops->del_key);
270 WARN_ON(ops->add_key && !ops->del_key);
271
Johannes Berg704232c2007-04-23 12:20:05 -0700272 alloc_size = sizeof(*drv) + sizeof_priv;
273
274 drv = kzalloc(alloc_size, GFP_KERNEL);
275 if (!drv)
276 return NULL;
277
278 drv->ops = ops;
279
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500280 mutex_lock(&cfg80211_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700281
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500282 drv->wiphy_idx = wiphy_counter++;
Johannes Berga4d73ee2007-04-26 20:50:35 -0700283
Luis R. Rodriguez85fd1292009-02-21 00:04:20 -0500284 if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
Denis ChengRq638af072008-09-23 02:35:37 +0800285 wiphy_counter--;
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500286 mutex_unlock(&cfg80211_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700287 /* ugh, wrapped! */
288 kfree(drv);
289 return NULL;
290 }
Johannes Berg704232c2007-04-23 12:20:05 -0700291
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500292 mutex_unlock(&cfg80211_mutex);
Denis ChengRq638af072008-09-23 02:35:37 +0800293
Johannes Berg704232c2007-04-23 12:20:05 -0700294 /* give it a proper name */
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500295 dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
Johannes Berg704232c2007-04-23 12:20:05 -0700296
Johannes Berg704232c2007-04-23 12:20:05 -0700297 mutex_init(&drv->mtx);
298 mutex_init(&drv->devlist_mtx);
299 INIT_LIST_HEAD(&drv->netdev_list);
Johannes Berg2a519312009-02-10 21:25:55 +0100300 spin_lock_init(&drv->bss_lock);
301 INIT_LIST_HEAD(&drv->bss_list);
Johannes Berg704232c2007-04-23 12:20:05 -0700302
303 device_initialize(&drv->wiphy.dev);
304 drv->wiphy.dev.class = &ieee80211_class;
305 drv->wiphy.dev.platform_data = drv;
306
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200307 drv->rfkill_ops.set_block = cfg80211_rfkill_set_block;
308 drv->rfkill = rfkill_alloc(dev_name(&drv->wiphy.dev),
309 &drv->wiphy.dev, RFKILL_TYPE_WLAN,
310 &drv->rfkill_ops, drv);
311
312 if (!drv->rfkill) {
313 kfree(drv);
314 return NULL;
315 }
316
317 INIT_WORK(&drv->rfkill_sync, cfg80211_rfkill_sync_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200318 INIT_WORK(&drv->conn_work, cfg80211_conn_work);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200319
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200320 /*
321 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
322 * Fragmentation and RTS threshold are disabled by default with the
323 * special -1 value.
324 */
325 drv->wiphy.retry_short = 7;
326 drv->wiphy.retry_long = 4;
327 drv->wiphy.frag_threshold = (u32) -1;
328 drv->wiphy.rts_threshold = (u32) -1;
329
Johannes Berg704232c2007-04-23 12:20:05 -0700330 return &drv->wiphy;
331}
332EXPORT_SYMBOL(wiphy_new);
333
334int wiphy_register(struct wiphy *wiphy)
335{
336 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
337 int res;
Johannes Berg8318d782008-01-24 19:38:38 +0100338 enum ieee80211_band band;
339 struct ieee80211_supported_band *sband;
340 bool have_band = false;
341 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700342 u16 ifmodes = wiphy->interface_modes;
343
344 /* sanity check ifmodes */
345 WARN_ON(!ifmodes);
346 ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
347 if (WARN_ON(ifmodes != wiphy->interface_modes))
348 wiphy->interface_modes = ifmodes;
Johannes Berg8318d782008-01-24 19:38:38 +0100349
350 /* sanity check supported bands/channels */
351 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
352 sband = wiphy->bands[band];
353 if (!sband)
354 continue;
355
356 sband->band = band;
357
Johannes Berg881d9482009-01-21 15:13:48 +0100358 if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
Johannes Berg8318d782008-01-24 19:38:38 +0100359 return -EINVAL;
Johannes Berg881d9482009-01-21 15:13:48 +0100360
361 /*
362 * Since we use a u32 for rate bitmaps in
363 * ieee80211_get_response_rate, we cannot
364 * have more than 32 legacy rates.
365 */
366 if (WARN_ON(sband->n_bitrates > 32))
367 return -EINVAL;
Johannes Berg8318d782008-01-24 19:38:38 +0100368
369 for (i = 0; i < sband->n_channels; i++) {
370 sband->channels[i].orig_flags =
371 sband->channels[i].flags;
372 sband->channels[i].orig_mag =
373 sband->channels[i].max_antenna_gain;
374 sband->channels[i].orig_mpwr =
375 sband->channels[i].max_power;
376 sband->channels[i].band = band;
377 }
378
379 have_band = true;
380 }
381
382 if (!have_band) {
383 WARN_ON(1);
384 return -EINVAL;
385 }
386
387 /* check and set up bitrates */
388 ieee80211_set_bitrate_flags(wiphy);
389
Johannes Berg704232c2007-04-23 12:20:05 -0700390 res = device_add(&drv->wiphy.dev);
391 if (res)
Johannes Berg2f0accc2009-06-10 16:50:29 +0200392 return res;
Johannes Berg704232c2007-04-23 12:20:05 -0700393
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200394 res = rfkill_register(drv->rfkill);
395 if (res)
396 goto out_rm_dev;
397
Johannes Berg2f0accc2009-06-10 16:50:29 +0200398 mutex_lock(&cfg80211_mutex);
399
400 /* set up regulatory info */
401 wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
402
Johannes Berg704232c2007-04-23 12:20:05 -0700403 list_add(&drv->list, &cfg80211_drv_list);
404
Johannes Berg2f0accc2009-06-10 16:50:29 +0200405 mutex_unlock(&cfg80211_mutex);
406
Johannes Berg704232c2007-04-23 12:20:05 -0700407 /* add to debugfs */
408 drv->wiphy.debugfsdir =
409 debugfs_create_dir(wiphy_name(&drv->wiphy),
410 ieee80211_debugfs_dir);
Johannes Berg33c03602008-10-08 10:23:48 +0200411 if (IS_ERR(drv->wiphy.debugfsdir))
412 drv->wiphy.debugfsdir = NULL;
Johannes Berg704232c2007-04-23 12:20:05 -0700413
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -0400414 if (wiphy->custom_regulatory) {
415 struct regulatory_request request;
416
417 request.wiphy_idx = get_wiphy_idx(wiphy);
418 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
419 request.alpha2[0] = '9';
420 request.alpha2[1] = '9';
421
422 nl80211_send_reg_change_event(&request);
423 }
424
Luis R. Rodriguez1ac61302009-05-02 00:37:21 -0400425 cfg80211_debugfs_drv_add(drv);
426
Johannes Berg2f0accc2009-06-10 16:50:29 +0200427 return 0;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200428
429 out_rm_dev:
430 device_del(&drv->wiphy.dev);
Johannes Berg704232c2007-04-23 12:20:05 -0700431 return res;
432}
433EXPORT_SYMBOL(wiphy_register);
434
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200435void wiphy_rfkill_start_polling(struct wiphy *wiphy)
436{
437 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
438
439 if (!drv->ops->rfkill_poll)
440 return;
441 drv->rfkill_ops.poll = cfg80211_rfkill_poll;
442 rfkill_resume_polling(drv->rfkill);
443}
444EXPORT_SYMBOL(wiphy_rfkill_start_polling);
445
446void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
447{
448 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
449
450 rfkill_pause_polling(drv->rfkill);
451}
452EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
453
Johannes Berg704232c2007-04-23 12:20:05 -0700454void wiphy_unregister(struct wiphy *wiphy)
455{
456 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
457
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200458 rfkill_unregister(drv->rfkill);
459
Johannes Bergf16bfc12007-04-26 20:51:12 -0700460 /* protect the device list */
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500461 mutex_lock(&cfg80211_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700462
Johannes Bergf16bfc12007-04-26 20:51:12 -0700463 BUG_ON(!list_empty(&drv->netdev_list));
464
465 /*
466 * Try to grab drv->mtx. If a command is still in progress,
467 * hopefully the driver will refuse it since it's tearing
468 * down the device already. We wait for this command to complete
469 * before unlinking the item from the list.
470 * Note: as codified by the BUG_ON above we cannot get here if
471 * a virtual interface is still associated. Hence, we can only
472 * get to lock contention here if userspace issues a command
473 * that identified the hardware by wiphy index.
474 */
Johannes Berg704232c2007-04-23 12:20:05 -0700475 mutex_lock(&drv->mtx);
Johannes Bergf16bfc12007-04-26 20:51:12 -0700476 /* unlock again before freeing */
Johannes Berg704232c2007-04-23 12:20:05 -0700477 mutex_unlock(&drv->mtx);
478
Johannes Berg6829c872009-07-02 09:13:27 +0200479 cancel_work_sync(&drv->conn_work);
480
Luis R. Rodriguez1ac61302009-05-02 00:37:21 -0400481 cfg80211_debugfs_drv_del(drv);
482
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800483 /* If this device got a regulatory hint tell core its
484 * free to listen now to a new shiny device regulatory hint */
485 reg_device_remove(wiphy);
486
Johannes Bergf16bfc12007-04-26 20:51:12 -0700487 list_del(&drv->list);
Johannes Berg704232c2007-04-23 12:20:05 -0700488 device_del(&drv->wiphy.dev);
489 debugfs_remove(drv->wiphy.debugfsdir);
490
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500491 mutex_unlock(&cfg80211_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700492}
493EXPORT_SYMBOL(wiphy_unregister);
494
495void cfg80211_dev_free(struct cfg80211_registered_device *drv)
496{
Johannes Berg2a519312009-02-10 21:25:55 +0100497 struct cfg80211_internal_bss *scan, *tmp;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200498 rfkill_destroy(drv->rfkill);
Johannes Berg704232c2007-04-23 12:20:05 -0700499 mutex_destroy(&drv->mtx);
500 mutex_destroy(&drv->devlist_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +0100501 list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
Johannes Berg78c1c7e2009-02-10 21:25:57 +0100502 cfg80211_put_bss(&scan->pub);
Johannes Berg704232c2007-04-23 12:20:05 -0700503 kfree(drv);
504}
505
506void wiphy_free(struct wiphy *wiphy)
507{
508 put_device(&wiphy->dev);
509}
510EXPORT_SYMBOL(wiphy_free);
511
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200512void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
513{
514 struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
515
516 if (rfkill_set_hw_state(drv->rfkill, blocked))
517 schedule_work(&drv->rfkill_sync);
518}
519EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
520
Johannes Berg704232c2007-04-23 12:20:05 -0700521static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
522 unsigned long state,
523 void *ndev)
524{
525 struct net_device *dev = ndev;
Johannes Berg2a783c12009-07-01 21:26:45 +0200526 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg704232c2007-04-23 12:20:05 -0700527 struct cfg80211_registered_device *rdev;
528
Johannes Berg2a783c12009-07-01 21:26:45 +0200529 if (!wdev)
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200530 return NOTIFY_DONE;
Johannes Berg704232c2007-04-23 12:20:05 -0700531
Johannes Berg2a783c12009-07-01 21:26:45 +0200532 rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg704232c2007-04-23 12:20:05 -0700533
Johannes Berg2a783c12009-07-01 21:26:45 +0200534 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg60719ff2008-09-16 14:55:09 +0200535
Johannes Berg704232c2007-04-23 12:20:05 -0700536 switch (state) {
537 case NETDEV_REGISTER:
538 mutex_lock(&rdev->devlist_mtx);
Johannes Berg2a783c12009-07-01 21:26:45 +0200539 list_add(&wdev->list, &rdev->netdev_list);
Johannes Berg704232c2007-04-23 12:20:05 -0700540 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
541 "phy80211")) {
542 printk(KERN_ERR "wireless: failed to add phy80211 "
543 "symlink to netdev!\n");
544 }
Johannes Berg2a783c12009-07-01 21:26:45 +0200545 wdev->netdev = dev;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200546 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergbc92afd2009-07-01 21:26:57 +0200547 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg08645122009-05-11 13:54:58 +0200548#ifdef CONFIG_WIRELESS_EXT
Johannes Berg2a783c12009-07-01 21:26:45 +0200549 wdev->wext.default_key = -1;
550 wdev->wext.default_mgmt_key = -1;
Johannes Bergf2129352009-07-01 21:26:56 +0200551 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
Johannes Bergbc92afd2009-07-01 21:26:57 +0200552 wdev->wext.ps = CONFIG_CFG80211_DEFAULT_PS_VALUE;
553 wdev->wext.ps_timeout = 500;
554 if (rdev->ops->set_power_mgmt)
555 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev,
556 wdev->wext.ps,
557 wdev->wext.ps_timeout)) {
558 /* assume this means it's off */
559 wdev->wext.ps = false;
560 }
Johannes Berg08645122009-05-11 13:54:58 +0200561#endif
Johannes Berg704232c2007-04-23 12:20:05 -0700562 break;
Johannes Berg04a773a2009-04-19 21:24:32 +0200563 case NETDEV_GOING_DOWN:
Samuel Ortizb23aa672009-07-01 21:26:54 +0200564 switch (wdev->iftype) {
565 case NL80211_IFTYPE_ADHOC:
566 cfg80211_leave_ibss(rdev, dev, true);
567 break;
568 case NL80211_IFTYPE_STATION:
Johannes Bergf2129352009-07-01 21:26:56 +0200569#ifdef CONFIG_WIRELESS_EXT
570 kfree(wdev->wext.ie);
571 wdev->wext.ie = NULL;
572 wdev->wext.ie_len = 0;
Johannes Berg0eb14642009-07-02 15:49:03 +0200573 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
Johannes Bergf2129352009-07-01 21:26:56 +0200574#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +0200575 cfg80211_disconnect(rdev, dev,
Johannes Bergf2129352009-07-01 21:26:56 +0200576 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg19957bb2009-07-02 17:20:43 +0200577 cfg80211_mlme_down(rdev, dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200578 break;
579 default:
580 break;
581 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200582 break;
583 case NETDEV_UP:
584#ifdef CONFIG_WIRELESS_EXT
Johannes Bergf2129352009-07-01 21:26:56 +0200585 switch (wdev->iftype) {
586 case NL80211_IFTYPE_ADHOC:
587 if (wdev->wext.ibss.ssid_len)
588 cfg80211_join_ibss(rdev, dev,
589 &wdev->wext.ibss);
Johannes Berg04a773a2009-04-19 21:24:32 +0200590 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200591 case NL80211_IFTYPE_STATION:
592 if (wdev->wext.connect.ssid_len)
593 cfg80211_connect(rdev, dev,
594 &wdev->wext.connect);
Johannes Berg04a773a2009-04-19 21:24:32 +0200595 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200596 default:
597 break;
598 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200599#endif
Johannes Berg2a783c12009-07-01 21:26:45 +0200600 break;
Johannes Berg704232c2007-04-23 12:20:05 -0700601 case NETDEV_UNREGISTER:
602 mutex_lock(&rdev->devlist_mtx);
Johannes Berg2a783c12009-07-01 21:26:45 +0200603 if (!list_empty(&wdev->list)) {
Johannes Berg704232c2007-04-23 12:20:05 -0700604 sysfs_remove_link(&dev->dev.kobj, "phy80211");
Johannes Berg2a783c12009-07-01 21:26:45 +0200605 list_del_init(&wdev->list);
Johannes Berg704232c2007-04-23 12:20:05 -0700606 }
607 mutex_unlock(&rdev->devlist_mtx);
608 break;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200609 case NETDEV_PRE_UP:
610 if (rfkill_blocked(rdev->rfkill))
611 return notifier_from_errno(-ERFKILL);
612 break;
Johannes Berg704232c2007-04-23 12:20:05 -0700613 }
614
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200615 return NOTIFY_DONE;
Johannes Berg704232c2007-04-23 12:20:05 -0700616}
617
618static struct notifier_block cfg80211_netdev_notifier = {
619 .notifier_call = cfg80211_netdev_notifier_call,
620};
621
622static int cfg80211_init(void)
623{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700624 int err;
625
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700626 err = wiphy_sysfs_init();
Johannes Berg704232c2007-04-23 12:20:05 -0700627 if (err)
628 goto out_fail_sysfs;
629
630 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
631 if (err)
632 goto out_fail_notifier;
633
Johannes Berg55682962007-09-20 13:09:35 -0400634 err = nl80211_init();
635 if (err)
636 goto out_fail_nl80211;
637
Johannes Berg704232c2007-04-23 12:20:05 -0700638 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
639
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700640 err = regulatory_init();
641 if (err)
642 goto out_fail_reg;
643
Johannes Berg704232c2007-04-23 12:20:05 -0700644 return 0;
645
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700646out_fail_reg:
647 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400648out_fail_nl80211:
649 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
Johannes Berg704232c2007-04-23 12:20:05 -0700650out_fail_notifier:
651 wiphy_sysfs_exit();
652out_fail_sysfs:
653 return err;
654}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700655
Johannes Berg3a462462007-09-10 13:44:45 +0200656subsys_initcall(cfg80211_init);
Johannes Berg704232c2007-04-23 12:20:05 -0700657
658static void cfg80211_exit(void)
659{
660 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -0400661 nl80211_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700662 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
663 wiphy_sysfs_exit();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700664 regulatory_exit();
Johannes Berg704232c2007-04-23 12:20:05 -0700665}
666module_exit(cfg80211_exit);