blob: 9416b8f55f50edf1b6939bbe837a95019550f71e [file] [log] [blame]
Johannes Berg704232c2007-04-23 12:20:05 -07001/*
2 * This is the linux wireless configuration interface.
3 *
Johannes Berg5f2aa252010-01-17 15:49:02 +01004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg704232c2007-04-23 12:20:05 -07005 */
6
Joe Perchese9c02682010-11-16 19:56:49 -08007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
Johannes Berg704232c2007-04-23 12:20:05 -07009#include <linux/if.h>
10#include <linux/module.h>
11#include <linux/err.h>
Johannes Berg704232c2007-04-23 12:20:05 -070012#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Johannes Berg704232c2007-04-23 12:20:05 -070014#include <linux/nl80211.h>
15#include <linux/debugfs.h>
16#include <linux/notifier.h>
17#include <linux/device.h>
Zhu Yi16a832e2009-08-19 16:08:22 +080018#include <linux/etherdevice.h>
Johannes Berg1f87f7d2009-06-02 13:01:41 +020019#include <linux/rtnetlink.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040020#include <linux/sched.h>
Johannes Berg704232c2007-04-23 12:20:05 -070021#include <net/genetlink.h>
22#include <net/cfg80211.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "nl80211.h"
Johannes Berg704232c2007-04-23 12:20:05 -070024#include "core.h"
25#include "sysfs.h"
Luis R. Rodriguez1ac61302009-05-02 00:37:21 -040026#include "debugfs.h"
Johannes Berga9a11622009-07-27 12:01:53 +020027#include "wext-compat.h"
John W. Linville4890e3b2009-09-30 14:50:17 -040028#include "ethtool.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030029#include "rdev-ops.h"
Johannes Berg704232c2007-04-23 12:20:05 -070030
31/* name for sysfs, %d is appended */
32#define PHY_NAME "phy"
33
34MODULE_AUTHOR("Johannes Berg");
35MODULE_LICENSE("GPL");
36MODULE_DESCRIPTION("wireless configuration support");
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070037MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME);
Johannes Berg704232c2007-04-23 12:20:05 -070038
Johannes Berg5f2aa252010-01-17 15:49:02 +010039/* RCU-protected (and cfg80211_mutex for writers) */
Johannes Berg79c97e92009-07-07 03:56:12 +020040LIST_HEAD(cfg80211_rdev_list);
Johannes Bergf5ea9122009-08-07 16:17:38 +020041int cfg80211_rdev_list_generation;
Luis R. Rodrigueza1794392009-02-21 00:04:21 -050042
Luis R. Rodrigueza1794392009-02-21 00:04:21 -050043DEFINE_MUTEX(cfg80211_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -070044
45/* for debugfs */
46static struct dentry *ieee80211_debugfs_dir;
47
Alban Browaeyse60d7442009-11-25 15:13:00 +010048/* for the cleanup, scan and event works */
49struct workqueue_struct *cfg80211_wq;
50
Amitkumar Karwar40db6c72011-04-21 14:10:27 -070051static bool cfg80211_disable_40mhz_24ghz;
52module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
53MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
54 "Disable 40MHz support in the 2.4GHz band");
55
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050056/* requires cfg80211_mutex to be held! */
Johannes Berg79c97e92009-07-07 03:56:12 +020057struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
Johannes Berg55682962007-09-20 13:09:35 -040058{
Johannes Berg79c97e92009-07-07 03:56:12 +020059 struct cfg80211_registered_device *result = NULL, *rdev;
Johannes Berg55682962007-09-20 13:09:35 -040060
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -050061 assert_cfg80211_lock();
62
Johannes Berg79c97e92009-07-07 03:56:12 +020063 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
64 if (rdev->wiphy_idx == wiphy_idx) {
65 result = rdev;
Johannes Berg55682962007-09-20 13:09:35 -040066 break;
67 }
68 }
69
70 return result;
71}
72
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050073int get_wiphy_idx(struct wiphy *wiphy)
74{
Johannes Bergf4173762012-12-03 18:23:37 +010075 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
76
Johannes Berg79c97e92009-07-07 03:56:12 +020077 return rdev->wiphy_idx;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050078}
79
Johannes Berg79c97e92009-07-07 03:56:12 +020080/* requires cfg80211_rdev_mutex to be held! */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050081struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
82{
Johannes Berg79c97e92009-07-07 03:56:12 +020083 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050084
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050085 assert_cfg80211_lock();
86
Johannes Berg79c97e92009-07-07 03:56:12 +020087 rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
88 if (!rdev)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050089 return NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +020090 return &rdev->wiphy;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -050091}
92
Johannes Berg4bbf4d52009-03-24 09:35:46 +010093/* requires cfg80211_mutex to be held */
Johannes Berg55682962007-09-20 13:09:35 -040094int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
95 char *newname)
96{
Johannes Berg79c97e92009-07-07 03:56:12 +020097 struct cfg80211_registered_device *rdev2;
Johannes Berg76232252010-10-11 14:46:52 -040098 int wiphy_idx, taken = -1, result, digits;
Johannes Berg55682962007-09-20 13:09:35 -040099
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100100 assert_cfg80211_lock();
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700101
Johannes Berg76232252010-10-11 14:46:52 -0400102 /* prohibit calling the thing phy%d when %d is not its number */
103 sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
104 if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
105 /* count number of places needed to print wiphy_idx */
106 digits = 1;
107 while (wiphy_idx /= 10)
108 digits++;
109 /*
110 * deny the name if it is phy<idx> where <idx> is printed
111 * without leading zeroes. taken == strlen(newname) here
112 */
113 if (taken == strlen(PHY_NAME) + digits)
114 return -EINVAL;
115 }
116
117
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700118 /* Ignore nop renames */
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700119 if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100120 return 0;
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700121
122 /* Ensure another device does not already have this name. */
Johannes Berg79c97e92009-07-07 03:56:12 +0200123 list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
124 if (strcmp(newname, dev_name(&rdev2->wiphy.dev)) == 0)
Johannes Berg76232252010-10-11 14:46:52 -0400125 return -EINVAL;
Eric W. Biederman2940bb62008-05-08 14:30:18 -0700126
Johannes Berg55682962007-09-20 13:09:35 -0400127 result = device_rename(&rdev->wiphy.dev, newname);
128 if (result)
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100129 return result;
Johannes Berg55682962007-09-20 13:09:35 -0400130
Johannes Berg33c03602008-10-08 10:23:48 +0200131 if (rdev->wiphy.debugfsdir &&
132 !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
Johannes Berg55682962007-09-20 13:09:35 -0400133 rdev->wiphy.debugfsdir,
134 rdev->wiphy.debugfsdir->d_parent,
135 newname))
Joe Perchese9c02682010-11-16 19:56:49 -0800136 pr_err("failed to rename debugfs dir to %s!\n", newname);
Johannes Berg55682962007-09-20 13:09:35 -0400137
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100138 nl80211_notify_dev_rename(rdev);
Johannes Berg55682962007-09-20 13:09:35 -0400139
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100140 return 0;
Johannes Berg55682962007-09-20 13:09:35 -0400141}
142
Johannes Berg463d0182009-07-14 00:33:35 +0200143int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
144 struct net *net)
145{
146 struct wireless_dev *wdev;
147 int err = 0;
148
Johannes Berg5be83de2009-11-19 00:56:28 +0100149 if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
Johannes Berg463d0182009-07-14 00:33:35 +0200150 return -EOPNOTSUPP;
151
Johannes Berg89a54e42012-06-15 14:33:17 +0200152 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergba22fb52012-06-15 18:00:00 +0200153 if (!wdev->netdev)
154 continue;
Johannes Berg463d0182009-07-14 00:33:35 +0200155 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
156 err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
157 if (err)
158 break;
159 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
160 }
161
162 if (err) {
163 /* failed -- clean up to old netns */
164 net = wiphy_net(&rdev->wiphy);
165
Johannes Berg89a54e42012-06-15 14:33:17 +0200166 list_for_each_entry_continue_reverse(wdev, &rdev->wdev_list,
Johannes Berg463d0182009-07-14 00:33:35 +0200167 list) {
Johannes Bergba22fb52012-06-15 18:00:00 +0200168 if (!wdev->netdev)
169 continue;
Johannes Berg463d0182009-07-14 00:33:35 +0200170 wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
171 err = dev_change_net_namespace(wdev->netdev, net,
172 "wlan%d");
173 WARN_ON(err);
174 wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
175 }
Johannes Berg04600792010-08-05 17:45:15 +0200176
177 return err;
Johannes Berg463d0182009-07-14 00:33:35 +0200178 }
179
180 wiphy_net_set(&rdev->wiphy, net);
181
Johannes Berg04600792010-08-05 17:45:15 +0200182 err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
183 WARN_ON(err);
184
185 return 0;
Johannes Berg463d0182009-07-14 00:33:35 +0200186}
187
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200188static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
189{
Johannes Berg79c97e92009-07-07 03:56:12 +0200190 struct cfg80211_registered_device *rdev = data;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200191
Hila Gonene35e4d22012-06-27 17:19:42 +0300192 rdev_rfkill_poll(rdev);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200193}
194
Johannes Bergf9f47522013-03-19 15:04:07 +0100195void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
196 struct wireless_dev *wdev)
197{
198 lockdep_assert_held(&rdev->devlist_mtx);
199 lockdep_assert_held(&rdev->sched_scan_mtx);
200
201 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_P2P_DEVICE))
202 return;
203
204 if (!wdev->p2p_started)
205 return;
206
207 rdev_stop_p2p_device(rdev, wdev);
208 wdev->p2p_started = false;
209
210 rdev->opencount--;
211
212 if (rdev->scan_req && rdev->scan_req->wdev == wdev) {
213 bool busy = work_busy(&rdev->scan_done_wk);
214
215 /*
216 * If the work isn't pending or running (in which case it would
217 * be waiting for the lock we hold) the driver didn't properly
218 * cancel the scan when the interface was removed. In this case
219 * warn and leak the scan request object to not crash later.
220 */
221 WARN_ON(!busy);
222
223 rdev->scan_req->aborted = true;
224 ___cfg80211_scan_done(rdev, !busy);
225 }
226}
227
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200228static int cfg80211_rfkill_set_block(void *data, bool blocked)
229{
Johannes Berg79c97e92009-07-07 03:56:12 +0200230 struct cfg80211_registered_device *rdev = data;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200231 struct wireless_dev *wdev;
232
233 if (!blocked)
234 return 0;
235
236 rtnl_lock();
Johannes Bergf9f47522013-03-19 15:04:07 +0100237
238 /* read-only iteration need not hold the devlist_mtx */
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200239
Johannes Berg98104fde2012-06-16 00:19:54 +0200240 list_for_each_entry(wdev, &rdev->wdev_list, list) {
241 if (wdev->netdev) {
Johannes Bergba22fb52012-06-15 18:00:00 +0200242 dev_close(wdev->netdev);
Johannes Berg98104fde2012-06-16 00:19:54 +0200243 continue;
244 }
245 /* otherwise, check iftype */
246 switch (wdev->iftype) {
247 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Bergf9f47522013-03-19 15:04:07 +0100248 /* but this requires it */
249 mutex_lock(&rdev->devlist_mtx);
250 mutex_lock(&rdev->sched_scan_mtx);
251 cfg80211_stop_p2p_device(rdev, wdev);
252 mutex_unlock(&rdev->sched_scan_mtx);
253 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +0200254 break;
255 default:
256 break;
257 }
258 }
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200259
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200260 rtnl_unlock();
261
262 return 0;
263}
264
265static void cfg80211_rfkill_sync_work(struct work_struct *work)
266{
Johannes Berg79c97e92009-07-07 03:56:12 +0200267 struct cfg80211_registered_device *rdev;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200268
Johannes Berg79c97e92009-07-07 03:56:12 +0200269 rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
270 cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200271}
272
Johannes Berg667503dd2009-07-07 03:56:11 +0200273static void cfg80211_event_work(struct work_struct *work)
274{
275 struct cfg80211_registered_device *rdev;
Johannes Berg667503dd2009-07-07 03:56:11 +0200276
277 rdev = container_of(work, struct cfg80211_registered_device,
278 event_work);
279
280 rtnl_lock();
281 cfg80211_lock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200282
Johannes Berg3d54d252009-08-21 14:51:05 +0200283 cfg80211_process_rdev_events(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200284 cfg80211_unlock_rdev(rdev);
285 rtnl_unlock();
286}
287
Johannes Berg704232c2007-04-23 12:20:05 -0700288/* exported functions */
289
David Kilroy3dcf6702009-05-16 23:13:46 +0100290struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
Johannes Berg704232c2007-04-23 12:20:05 -0700291{
Johannes Berg73810b72013-05-08 21:49:02 +0200292 static atomic_t wiphy_counter = ATOMIC_INIT(0);
Johannes Berg76232252010-10-11 14:46:52 -0400293
294 struct cfg80211_registered_device *rdev;
Johannes Berg704232c2007-04-23 12:20:05 -0700295 int alloc_size;
296
Johannes Berg0b20633d2009-07-07 03:56:13 +0200297 WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
298 WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
299 WARN_ON(ops->connect && !ops->disconnect);
300 WARN_ON(ops->join_ibss && !ops->leave_ibss);
301 WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
302 WARN_ON(ops->add_station && !ops->del_station);
303 WARN_ON(ops->add_mpath && !ops->del_mpath);
Johannes Berg29cbe682010-12-03 09:20:44 +0100304 WARN_ON(ops->join_mesh && !ops->leave_mesh);
Johannes Berg41ade002007-12-19 02:03:29 +0100305
Johannes Berg79c97e92009-07-07 03:56:12 +0200306 alloc_size = sizeof(*rdev) + sizeof_priv;
Johannes Berg704232c2007-04-23 12:20:05 -0700307
Johannes Berg79c97e92009-07-07 03:56:12 +0200308 rdev = kzalloc(alloc_size, GFP_KERNEL);
309 if (!rdev)
Johannes Berg704232c2007-04-23 12:20:05 -0700310 return NULL;
311
Johannes Berg79c97e92009-07-07 03:56:12 +0200312 rdev->ops = ops;
Johannes Berg704232c2007-04-23 12:20:05 -0700313
Johannes Berg73810b72013-05-08 21:49:02 +0200314 rdev->wiphy_idx = atomic_inc_return(&wiphy_counter);
Johannes Berga4d73ee2007-04-26 20:50:35 -0700315
Johannes Bergf4173762012-12-03 18:23:37 +0100316 if (unlikely(rdev->wiphy_idx < 0)) {
Johannes Berg76232252010-10-11 14:46:52 -0400317 /* ugh, wrapped! */
Johannes Berg73810b72013-05-08 21:49:02 +0200318 atomic_dec(&wiphy_counter);
Johannes Berg79c97e92009-07-07 03:56:12 +0200319 kfree(rdev);
Johannes Berg704232c2007-04-23 12:20:05 -0700320 return NULL;
321 }
Johannes Berg704232c2007-04-23 12:20:05 -0700322
Johannes Berg76232252010-10-11 14:46:52 -0400323 /* give it a proper name */
324 dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
325
Johannes Berg79c97e92009-07-07 03:56:12 +0200326 mutex_init(&rdev->mtx);
327 mutex_init(&rdev->devlist_mtx);
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300328 mutex_init(&rdev->sched_scan_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +0200329 INIT_LIST_HEAD(&rdev->wdev_list);
Ben Greear37c73b52012-10-26 14:49:25 -0700330 INIT_LIST_HEAD(&rdev->beacon_registrations);
331 spin_lock_init(&rdev->beacon_registrations_lock);
Johannes Berg79c97e92009-07-07 03:56:12 +0200332 spin_lock_init(&rdev->bss_lock);
333 INIT_LIST_HEAD(&rdev->bss_list);
334 INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300335 INIT_WORK(&rdev->sched_scan_results_wk, __cfg80211_sched_scan_results);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100336 INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk,
337 cfg80211_dfs_channels_update_work);
Johannes Berg3d23e342009-09-29 23:27:28 +0200338#ifdef CONFIG_CFG80211_WEXT
339 rdev->wiphy.wext = &cfg80211_wext_handler;
340#endif
341
Johannes Berg79c97e92009-07-07 03:56:12 +0200342 device_initialize(&rdev->wiphy.dev);
343 rdev->wiphy.dev.class = &ieee80211_class;
344 rdev->wiphy.dev.platform_data = rdev;
Johannes Berg704232c2007-04-23 12:20:05 -0700345
Johannes Berg5be83de2009-11-19 00:56:28 +0100346#ifdef CONFIG_CFG80211_DEFAULT_PS
347 rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
348#endif
Johannes Berg16cb9d42009-08-12 23:33:20 +0200349
Johannes Berg463d0182009-07-14 00:33:35 +0200350 wiphy_net_set(&rdev->wiphy, &init_net);
351
Johannes Berg79c97e92009-07-07 03:56:12 +0200352 rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
353 rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
354 &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
355 &rdev->rfkill_ops, rdev);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200356
Johannes Berg79c97e92009-07-07 03:56:12 +0200357 if (!rdev->rfkill) {
358 kfree(rdev);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200359 return NULL;
360 }
361
Johannes Berg79c97e92009-07-07 03:56:12 +0200362 INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
363 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
364 INIT_WORK(&rdev->event_work, cfg80211_event_work);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200365
Johannes Bergad002392009-08-18 19:51:57 +0200366 init_waitqueue_head(&rdev->dev_wait);
367
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200368 /*
369 * Initialize wiphy parameters to IEEE 802.11 MIB default values.
370 * Fragmentation and RTS threshold are disabled by default with the
371 * special -1 value.
372 */
Johannes Berg79c97e92009-07-07 03:56:12 +0200373 rdev->wiphy.retry_short = 7;
374 rdev->wiphy.retry_long = 4;
375 rdev->wiphy.frag_threshold = (u32) -1;
376 rdev->wiphy.rts_threshold = (u32) -1;
Lukáš Turek81077e82009-12-21 22:50:47 +0100377 rdev->wiphy.coverage_class = 0;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200378
Johannes Bergd0ae7082013-02-27 15:08:28 +0100379 rdev->wiphy.features = NL80211_FEATURE_SCAN_FLUSH;
Sam Leffler15d60302012-10-11 21:03:34 -0700380
Johannes Berg79c97e92009-07-07 03:56:12 +0200381 return &rdev->wiphy;
Johannes Berg704232c2007-04-23 12:20:05 -0700382}
383EXPORT_SYMBOL(wiphy_new);
384
Johannes Berg7527a782011-05-13 10:58:57 +0200385static int wiphy_verify_combinations(struct wiphy *wiphy)
386{
387 const struct ieee80211_iface_combination *c;
388 int i, j;
389
Johannes Berg7527a782011-05-13 10:58:57 +0200390 for (i = 0; i < wiphy->n_iface_combinations; i++) {
391 u32 cnt = 0;
392 u16 all_iftypes = 0;
393
394 c = &wiphy->iface_combinations[i];
395
Simon Wunderlich11c4a072013-01-08 14:04:07 +0100396 /*
397 * Combinations with just one interface aren't real,
398 * however we make an exception for DFS.
399 */
400 if (WARN_ON((c->max_interfaces < 2) && !c->radar_detect_widths))
Johannes Berg7527a782011-05-13 10:58:57 +0200401 return -EINVAL;
402
403 /* Need at least one channel */
404 if (WARN_ON(!c->num_different_channels))
405 return -EINVAL;
406
Michal Kaziord4e50c52012-06-29 12:47:07 +0200407 /*
408 * Put a sane limit on maximum number of different
409 * channels to simplify channel accounting code.
410 */
411 if (WARN_ON(c->num_different_channels >
412 CFG80211_MAX_NUM_DIFFERENT_CHANNELS))
413 return -EINVAL;
414
Simon Wunderlich11c4a072013-01-08 14:04:07 +0100415 /* DFS only works on one channel. */
416 if (WARN_ON(c->radar_detect_widths &&
417 (c->num_different_channels > 1)))
418 return -EINVAL;
419
Johannes Berg7527a782011-05-13 10:58:57 +0200420 if (WARN_ON(!c->n_limits))
421 return -EINVAL;
422
423 for (j = 0; j < c->n_limits; j++) {
424 u16 types = c->limits[j].types;
425
426 /*
427 * interface types shouldn't overlap, this is
428 * used in cfg80211_can_change_interface()
429 */
430 if (WARN_ON(types & all_iftypes))
431 return -EINVAL;
432 all_iftypes |= types;
433
434 if (WARN_ON(!c->limits[j].max))
435 return -EINVAL;
436
437 /* Shouldn't list software iftypes in combinations! */
438 if (WARN_ON(wiphy->software_iftypes & types))
439 return -EINVAL;
440
Johannes Berg98104fde2012-06-16 00:19:54 +0200441 /* Only a single P2P_DEVICE can be allowed */
442 if (WARN_ON(types & BIT(NL80211_IFTYPE_P2P_DEVICE) &&
443 c->limits[j].max > 1))
444 return -EINVAL;
445
Johannes Berg7527a782011-05-13 10:58:57 +0200446 cnt += c->limits[j].max;
447 /*
448 * Don't advertise an unsupported type
449 * in a combination.
450 */
451 if (WARN_ON((wiphy->interface_modes & types) != types))
452 return -EINVAL;
453 }
454
455 /* You can't even choose that many! */
456 if (WARN_ON(cnt < c->max_interfaces))
457 return -EINVAL;
458 }
459
460 return 0;
461}
462
Johannes Berg704232c2007-04-23 12:20:05 -0700463int wiphy_register(struct wiphy *wiphy)
464{
Johannes Berg79c97e92009-07-07 03:56:12 +0200465 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg704232c2007-04-23 12:20:05 -0700466 int res;
Johannes Berg8318d782008-01-24 19:38:38 +0100467 enum ieee80211_band band;
468 struct ieee80211_supported_band *sband;
469 bool have_band = false;
470 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700471 u16 ifmodes = wiphy->interface_modes;
472
Johannes Bergdfb89c52012-06-27 09:23:48 +0200473#ifdef CONFIG_PM
Johannes Berg77dbbb12011-07-13 10:48:55 +0200474 if (WARN_ON((wiphy->wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
475 !(wiphy->wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
476 return -EINVAL;
Johannes Bergdfb89c52012-06-27 09:23:48 +0200477#endif
Johannes Berg77dbbb12011-07-13 10:48:55 +0200478
Johannes Berg562a7482011-11-07 12:39:33 +0100479 if (WARN_ON(wiphy->ap_sme_capa &&
480 !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
481 return -EINVAL;
482
Johannes Bergef15aac2010-01-20 12:02:33 +0100483 if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
484 return -EINVAL;
485
486 if (WARN_ON(wiphy->addresses &&
487 !is_zero_ether_addr(wiphy->perm_addr) &&
488 memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
489 ETH_ALEN)))
490 return -EINVAL;
491
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530492 if (WARN_ON(wiphy->max_acl_mac_addrs &&
493 (!(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME) ||
494 !rdev->ops->set_mac_acl)))
495 return -EINVAL;
496
Johannes Bergef15aac2010-01-20 12:02:33 +0100497 if (wiphy->addresses)
498 memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
499
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700500 /* sanity check ifmodes */
501 WARN_ON(!ifmodes);
Johannes Berg2e161f72010-08-12 15:38:38 +0200502 ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700503 if (WARN_ON(ifmodes != wiphy->interface_modes))
504 wiphy->interface_modes = ifmodes;
Johannes Berg8318d782008-01-24 19:38:38 +0100505
Johannes Berg7527a782011-05-13 10:58:57 +0200506 res = wiphy_verify_combinations(wiphy);
507 if (res)
508 return res;
509
Johannes Berg8318d782008-01-24 19:38:38 +0100510 /* sanity check supported bands/channels */
511 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
512 sband = wiphy->bands[band];
513 if (!sband)
514 continue;
515
516 sband->band = band;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300517 if (WARN_ON(!sband->n_channels))
518 return -EINVAL;
519 /*
520 * on 60gHz band, there are no legacy rates, so
521 * n_bitrates is 0
522 */
523 if (WARN_ON(band != IEEE80211_BAND_60GHZ &&
524 !sband->n_bitrates))
Johannes Berg8318d782008-01-24 19:38:38 +0100525 return -EINVAL;
Johannes Berg881d9482009-01-21 15:13:48 +0100526
527 /*
Amitkumar Karwar40db6c72011-04-21 14:10:27 -0700528 * Since cfg80211_disable_40mhz_24ghz is global, we can
529 * modify the sband's ht data even if the driver uses a
530 * global structure for that.
531 */
532 if (cfg80211_disable_40mhz_24ghz &&
533 band == IEEE80211_BAND_2GHZ &&
534 sband->ht_cap.ht_supported) {
535 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
536 sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
537 }
538
539 /*
Johannes Berg881d9482009-01-21 15:13:48 +0100540 * Since we use a u32 for rate bitmaps in
541 * ieee80211_get_response_rate, we cannot
542 * have more than 32 legacy rates.
543 */
544 if (WARN_ON(sband->n_bitrates > 32))
545 return -EINVAL;
Johannes Berg8318d782008-01-24 19:38:38 +0100546
547 for (i = 0; i < sband->n_channels; i++) {
548 sband->channels[i].orig_flags =
549 sband->channels[i].flags;
Felix Fietkauc4a9faf2012-10-17 13:56:19 +0200550 sband->channels[i].orig_mag = INT_MAX;
Johannes Berg8318d782008-01-24 19:38:38 +0100551 sband->channels[i].orig_mpwr =
552 sband->channels[i].max_power;
553 sband->channels[i].band = band;
554 }
555
556 have_band = true;
557 }
558
559 if (!have_band) {
560 WARN_ON(1);
561 return -EINVAL;
562 }
563
Johannes Bergdfb89c52012-06-27 09:23:48 +0200564#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +0200565 if (rdev->wiphy.wowlan.n_patterns) {
566 if (WARN_ON(!rdev->wiphy.wowlan.pattern_min_len ||
567 rdev->wiphy.wowlan.pattern_min_len >
568 rdev->wiphy.wowlan.pattern_max_len))
569 return -EINVAL;
570 }
Johannes Bergdfb89c52012-06-27 09:23:48 +0200571#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +0200572
Johannes Berg8318d782008-01-24 19:38:38 +0100573 /* check and set up bitrates */
574 ieee80211_set_bitrate_flags(wiphy);
575
Maxime Bizon5a652052010-07-21 17:21:38 +0200576 mutex_lock(&cfg80211_mutex);
577
Johannes Berg79c97e92009-07-07 03:56:12 +0200578 res = device_add(&rdev->wiphy.dev);
John W. Linvillec3d34d52010-08-30 17:36:40 -0400579 if (res) {
580 mutex_unlock(&cfg80211_mutex);
581 return res;
582 }
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200583
Johannes Berg2f0accc2009-06-10 16:50:29 +0200584 /* set up regulatory info */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700585 wiphy_regulatory_register(wiphy);
Johannes Berg2f0accc2009-06-10 16:50:29 +0200586
Johannes Berg5f2aa252010-01-17 15:49:02 +0100587 list_add_rcu(&rdev->list, &cfg80211_rdev_list);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200588 cfg80211_rdev_list_generation++;
Johannes Berg704232c2007-04-23 12:20:05 -0700589
590 /* add to debugfs */
Johannes Berg79c97e92009-07-07 03:56:12 +0200591 rdev->wiphy.debugfsdir =
592 debugfs_create_dir(wiphy_name(&rdev->wiphy),
Johannes Berg704232c2007-04-23 12:20:05 -0700593 ieee80211_debugfs_dir);
Johannes Berg79c97e92009-07-07 03:56:12 +0200594 if (IS_ERR(rdev->wiphy.debugfsdir))
595 rdev->wiphy.debugfsdir = NULL;
Johannes Berg704232c2007-04-23 12:20:05 -0700596
Johannes Berg5be83de2009-11-19 00:56:28 +0100597 if (wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -0400598 struct regulatory_request request;
599
600 request.wiphy_idx = get_wiphy_idx(wiphy);
601 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
602 request.alpha2[0] = '9';
603 request.alpha2[1] = '9';
604
605 nl80211_send_reg_change_event(&request);
606 }
607
Johannes Berg79c97e92009-07-07 03:56:12 +0200608 cfg80211_debugfs_rdev_add(rdev);
Maxime Bizon5a652052010-07-21 17:21:38 +0200609 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez1ac61302009-05-02 00:37:21 -0400610
John W. Linvillec3d34d52010-08-30 17:36:40 -0400611 /*
612 * due to a locking dependency this has to be outside of the
613 * cfg80211_mutex lock
614 */
615 res = rfkill_register(rdev->rfkill);
Johannes Berg03cd7e42013-05-10 19:23:40 +0200616 if (res) {
617 device_del(&rdev->wiphy.dev);
618
619 mutex_lock(&cfg80211_mutex);
620 debugfs_remove_recursive(rdev->wiphy.debugfsdir);
621 list_del_rcu(&rdev->list);
622 wiphy_regulatory_deregister(wiphy);
623 mutex_unlock(&cfg80211_mutex);
624 return res;
625 }
John W. Linvillec3d34d52010-08-30 17:36:40 -0400626
Stanislaw Gruszkaecb44332011-08-12 14:00:59 +0200627 rtnl_lock();
628 rdev->wiphy.registered = true;
629 rtnl_unlock();
Johannes Berg2f0accc2009-06-10 16:50:29 +0200630 return 0;
Johannes Berg704232c2007-04-23 12:20:05 -0700631}
632EXPORT_SYMBOL(wiphy_register);
633
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200634void wiphy_rfkill_start_polling(struct wiphy *wiphy)
635{
Johannes Berg79c97e92009-07-07 03:56:12 +0200636 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200637
Johannes Berg79c97e92009-07-07 03:56:12 +0200638 if (!rdev->ops->rfkill_poll)
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200639 return;
Johannes Berg79c97e92009-07-07 03:56:12 +0200640 rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
641 rfkill_resume_polling(rdev->rfkill);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200642}
643EXPORT_SYMBOL(wiphy_rfkill_start_polling);
644
645void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
646{
Johannes Berg79c97e92009-07-07 03:56:12 +0200647 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200648
Johannes Berg79c97e92009-07-07 03:56:12 +0200649 rfkill_pause_polling(rdev->rfkill);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200650}
651EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
652
Johannes Berg704232c2007-04-23 12:20:05 -0700653void wiphy_unregister(struct wiphy *wiphy)
654{
Johannes Berg79c97e92009-07-07 03:56:12 +0200655 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg704232c2007-04-23 12:20:05 -0700656
Stanislaw Gruszkaecb44332011-08-12 14:00:59 +0200657 rtnl_lock();
658 rdev->wiphy.registered = false;
659 rtnl_unlock();
660
Johannes Berg79c97e92009-07-07 03:56:12 +0200661 rfkill_unregister(rdev->rfkill);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200662
Johannes Bergf16bfc12007-04-26 20:51:12 -0700663 /* protect the device list */
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500664 mutex_lock(&cfg80211_mutex);
Johannes Berg704232c2007-04-23 12:20:05 -0700665
Johannes Bergad002392009-08-18 19:51:57 +0200666 wait_event(rdev->dev_wait, ({
667 int __count;
668 mutex_lock(&rdev->devlist_mtx);
669 __count = rdev->opencount;
670 mutex_unlock(&rdev->devlist_mtx);
Cristian Chilipireac4f60842012-05-08 12:38:53 +0300671 __count == 0; }));
Johannes Bergad002392009-08-18 19:51:57 +0200672
673 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +0200674 BUG_ON(!list_empty(&rdev->wdev_list));
Johannes Bergad002392009-08-18 19:51:57 +0200675 mutex_unlock(&rdev->devlist_mtx);
676
677 /*
678 * First remove the hardware from everywhere, this makes
679 * it impossible to find from userspace.
680 */
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100681 debugfs_remove_recursive(rdev->wiphy.debugfsdir);
Johannes Berg5f2aa252010-01-17 15:49:02 +0100682 list_del_rcu(&rdev->list);
683 synchronize_rcu();
Johannes Bergf16bfc12007-04-26 20:51:12 -0700684
685 /*
Johannes Berg79c97e92009-07-07 03:56:12 +0200686 * Try to grab rdev->mtx. If a command is still in progress,
Johannes Bergf16bfc12007-04-26 20:51:12 -0700687 * hopefully the driver will refuse it since it's tearing
688 * down the device already. We wait for this command to complete
689 * before unlinking the item from the list.
690 * Note: as codified by the BUG_ON above we cannot get here if
Johannes Bergad002392009-08-18 19:51:57 +0200691 * a virtual interface is still present. Hence, we can only get
692 * to lock contention here if userspace issues a command that
693 * identified the hardware by wiphy index.
Johannes Bergf16bfc12007-04-26 20:51:12 -0700694 */
Johannes Berg0ff6ce72009-08-17 12:25:37 +0200695 cfg80211_lock_rdev(rdev);
Johannes Bergad002392009-08-18 19:51:57 +0200696 /* nothing */
Johannes Berg0ff6ce72009-08-17 12:25:37 +0200697 cfg80211_unlock_rdev(rdev);
Johannes Berg704232c2007-04-23 12:20:05 -0700698
Luis R. Rodriguezbfead082012-07-12 11:49:19 -0700699 /*
700 * If this device got a regulatory hint tell core its
701 * free to listen now to a new shiny device regulatory hint
702 */
703 wiphy_regulatory_deregister(wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800704
Johannes Bergf5ea9122009-08-07 16:17:38 +0200705 cfg80211_rdev_list_generation++;
Johannes Berg79c97e92009-07-07 03:56:12 +0200706 device_del(&rdev->wiphy.dev);
Johannes Berg704232c2007-04-23 12:20:05 -0700707
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500708 mutex_unlock(&cfg80211_mutex);
Johannes Berg66825882009-07-13 13:24:44 +0200709
Johannes Berg36e6fea2009-08-12 22:21:21 +0200710 flush_work(&rdev->scan_done_wk);
Johannes Berg66825882009-07-13 13:24:44 +0200711 cancel_work_sync(&rdev->conn_work);
Johannes Berg66825882009-07-13 13:24:44 +0200712 flush_work(&rdev->event_work);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100713 cancel_delayed_work_sync(&rdev->dfs_update_channels_wk);
Johannes Berg6d525632012-04-04 15:05:25 +0200714
715 if (rdev->wowlan && rdev->ops->set_wakeup)
Hila Gonene35e4d22012-06-27 17:19:42 +0300716 rdev_set_wakeup(rdev, false);
Johannes Berg6d525632012-04-04 15:05:25 +0200717 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg704232c2007-04-23 12:20:05 -0700718}
719EXPORT_SYMBOL(wiphy_unregister);
720
Johannes Berg79c97e92009-07-07 03:56:12 +0200721void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
Johannes Berg704232c2007-04-23 12:20:05 -0700722{
Johannes Berg2a519312009-02-10 21:25:55 +0100723 struct cfg80211_internal_bss *scan, *tmp;
Ben Greear37c73b52012-10-26 14:49:25 -0700724 struct cfg80211_beacon_registration *reg, *treg;
Johannes Berg79c97e92009-07-07 03:56:12 +0200725 rfkill_destroy(rdev->rfkill);
726 mutex_destroy(&rdev->mtx);
727 mutex_destroy(&rdev->devlist_mtx);
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300728 mutex_destroy(&rdev->sched_scan_mtx);
Ben Greear37c73b52012-10-26 14:49:25 -0700729 list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) {
730 list_del(&reg->list);
731 kfree(reg);
732 }
Johannes Berg79c97e92009-07-07 03:56:12 +0200733 list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
Johannes Berg5b112d32013-02-01 01:49:58 +0100734 cfg80211_put_bss(&rdev->wiphy, &scan->pub);
Johannes Berg79c97e92009-07-07 03:56:12 +0200735 kfree(rdev);
Johannes Berg704232c2007-04-23 12:20:05 -0700736}
737
738void wiphy_free(struct wiphy *wiphy)
739{
740 put_device(&wiphy->dev);
741}
742EXPORT_SYMBOL(wiphy_free);
743
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200744void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
745{
Johannes Berg79c97e92009-07-07 03:56:12 +0200746 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200747
Johannes Berg79c97e92009-07-07 03:56:12 +0200748 if (rfkill_set_hw_state(rdev->rfkill, blocked))
749 schedule_work(&rdev->rfkill_sync);
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200750}
751EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
752
Johannes Bergad002392009-08-18 19:51:57 +0200753static void wdev_cleanup_work(struct work_struct *work)
754{
755 struct wireless_dev *wdev;
756 struct cfg80211_registered_device *rdev;
757
758 wdev = container_of(work, struct wireless_dev, cleanup_work);
759 rdev = wiphy_to_dev(wdev->wiphy);
760
Johannes Bergf9f47522013-03-19 15:04:07 +0100761 mutex_lock(&rdev->sched_scan_mtx);
Johannes Bergad002392009-08-18 19:51:57 +0200762
Johannes Bergfd014282012-06-18 19:17:03 +0200763 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
Johannes Bergad002392009-08-18 19:51:57 +0200764 rdev->scan_req->aborted = true;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200765 ___cfg80211_scan_done(rdev, true);
Johannes Bergad002392009-08-18 19:51:57 +0200766 }
767
Luciano Coelho807f8a82011-05-11 17:09:35 +0300768 if (WARN_ON(rdev->sched_scan_req &&
769 rdev->sched_scan_req->dev == wdev->netdev)) {
770 __cfg80211_stop_sched_scan(rdev, false);
771 }
772
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300773 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Bergad002392009-08-18 19:51:57 +0200774
775 mutex_lock(&rdev->devlist_mtx);
776 rdev->opencount--;
777 mutex_unlock(&rdev->devlist_mtx);
778 wake_up(&rdev->dev_wait);
779
780 dev_put(wdev->netdev);
781}
782
Johannes Berg98104fde2012-06-16 00:19:54 +0200783void cfg80211_unregister_wdev(struct wireless_dev *wdev)
784{
785 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
786
787 ASSERT_RTNL();
788
789 if (WARN_ON(wdev->netdev))
790 return;
791
792 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +0100793 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +0200794 list_del_rcu(&wdev->list);
795 rdev->devlist_generation++;
796
797 switch (wdev->iftype) {
798 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Bergf9f47522013-03-19 15:04:07 +0100799 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +0200800 break;
801 default:
802 WARN_ON_ONCE(1);
803 break;
804 }
Johannes Bergf9f47522013-03-19 15:04:07 +0100805 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +0200806 mutex_unlock(&rdev->devlist_mtx);
807}
808EXPORT_SYMBOL(cfg80211_unregister_wdev);
809
Marcel Holtmann053a93d2009-10-02 05:15:28 +0000810static struct device_type wiphy_type = {
811 .name = "wlan",
812};
813
Michal Kaziordbbae262012-06-29 12:47:01 +0200814void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
815 enum nl80211_iftype iftype, int num)
816{
Johannes Bergc5a7e582012-07-04 13:28:18 +0200817 ASSERT_RTNL();
Michal Kaziordbbae262012-06-29 12:47:01 +0200818
819 rdev->num_running_ifaces += num;
820 if (iftype == NL80211_IFTYPE_MONITOR)
821 rdev->num_running_monitor_ifaces += num;
Michal Kaziordbbae262012-06-29 12:47:01 +0200822}
823
Stanislaw Gruszka81256962013-02-28 10:55:25 +0100824void cfg80211_leave(struct cfg80211_registered_device *rdev,
825 struct wireless_dev *wdev)
826{
827 struct net_device *dev = wdev->netdev;
828
829 switch (wdev->iftype) {
830 case NL80211_IFTYPE_ADHOC:
831 cfg80211_leave_ibss(rdev, dev, true);
832 break;
833 case NL80211_IFTYPE_P2P_CLIENT:
834 case NL80211_IFTYPE_STATION:
835 mutex_lock(&rdev->sched_scan_mtx);
836 __cfg80211_stop_sched_scan(rdev, false);
837 mutex_unlock(&rdev->sched_scan_mtx);
838
839 wdev_lock(wdev);
840#ifdef CONFIG_CFG80211_WEXT
841 kfree(wdev->wext.ie);
842 wdev->wext.ie = NULL;
843 wdev->wext.ie_len = 0;
844 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
845#endif
846 __cfg80211_disconnect(rdev, dev,
847 WLAN_REASON_DEAUTH_LEAVING, true);
Stanislaw Gruszka81256962013-02-28 10:55:25 +0100848 wdev_unlock(wdev);
849 break;
850 case NL80211_IFTYPE_MESH_POINT:
851 cfg80211_leave_mesh(rdev, dev);
852 break;
853 case NL80211_IFTYPE_AP:
854 cfg80211_stop_ap(rdev, dev);
855 break;
856 default:
857 break;
858 }
859
860 wdev->beacon_interval = 0;
861}
862
Cristian Chilipireac4f60842012-05-08 12:38:53 +0300863static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
Johannes Berg704232c2007-04-23 12:20:05 -0700864 unsigned long state,
865 void *ndev)
866{
867 struct net_device *dev = ndev;
Johannes Berg2a783c12009-07-01 21:26:45 +0200868 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg704232c2007-04-23 12:20:05 -0700869 struct cfg80211_registered_device *rdev;
Johannes Berg7527a782011-05-13 10:58:57 +0200870 int ret;
Johannes Berg704232c2007-04-23 12:20:05 -0700871
Johannes Berg2a783c12009-07-01 21:26:45 +0200872 if (!wdev)
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200873 return NOTIFY_DONE;
Johannes Berg704232c2007-04-23 12:20:05 -0700874
Johannes Berg2a783c12009-07-01 21:26:45 +0200875 rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg704232c2007-04-23 12:20:05 -0700876
Johannes Berg2a783c12009-07-01 21:26:45 +0200877 WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg60719ff2008-09-16 14:55:09 +0200878
Johannes Berg704232c2007-04-23 12:20:05 -0700879 switch (state) {
Marcel Holtmann053a93d2009-10-02 05:15:28 +0000880 case NETDEV_POST_INIT:
881 SET_NETDEV_DEVTYPE(dev, &wiphy_type);
882 break;
Johannes Berg704232c2007-04-23 12:20:05 -0700883 case NETDEV_REGISTER:
Johannes Berg0ff6ce72009-08-17 12:25:37 +0200884 /*
885 * NB: cannot take rdev->mtx here because this may be
886 * called within code protected by it when interfaces
887 * are added with nl80211.
888 */
Johannes Berg667503dd2009-07-07 03:56:11 +0200889 mutex_init(&wdev->mtx);
Johannes Bergad002392009-08-18 19:51:57 +0200890 INIT_WORK(&wdev->cleanup_work, wdev_cleanup_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200891 INIT_LIST_HEAD(&wdev->event_list);
892 spin_lock_init(&wdev->event_lock);
Johannes Berg2e161f72010-08-12 15:38:38 +0200893 INIT_LIST_HEAD(&wdev->mgmt_registrations);
894 spin_lock_init(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200895
Johannes Berg704232c2007-04-23 12:20:05 -0700896 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +0200897 wdev->identifier = ++rdev->wdev_id;
898 list_add_rcu(&wdev->list, &rdev->wdev_list);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200899 rdev->devlist_generation++;
Johannes Berg463d0182009-07-14 00:33:35 +0200900 /* can only change netns with wiphy */
901 dev->features |= NETIF_F_NETNS_LOCAL;
902
Johannes Berg704232c2007-04-23 12:20:05 -0700903 if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
904 "phy80211")) {
Joe Perchese9c02682010-11-16 19:56:49 -0800905 pr_err("failed to add phy80211 symlink to netdev!\n");
Johannes Berg704232c2007-04-23 12:20:05 -0700906 }
Johannes Berg2a783c12009-07-01 21:26:45 +0200907 wdev->netdev = dev;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200908 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergbc92afd2009-07-01 21:26:57 +0200909 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg3d23e342009-09-29 23:27:28 +0200910#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a783c12009-07-01 21:26:45 +0200911 wdev->wext.default_key = -1;
912 wdev->wext.default_mgmt_key = -1;
Johannes Bergf2129352009-07-01 21:26:56 +0200913 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
Kalle Valoffb9eb32010-02-17 17:58:10 +0200914#endif
915
Johannes Berg5be83de2009-11-19 00:56:28 +0100916 if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
Kalle Valoffb9eb32010-02-17 17:58:10 +0200917 wdev->ps = true;
Johannes Berg5be83de2009-11-19 00:56:28 +0100918 else
Kalle Valoffb9eb32010-02-17 17:58:10 +0200919 wdev->ps = false;
Juuso Oikarinen9043f3b2010-04-27 12:47:41 +0300920 /* allow mac80211 to determine the timeout */
921 wdev->ps_timeout = -1;
Kalle Valoffb9eb32010-02-17 17:58:10 +0200922
Stanislaw Gruszkad07d7502013-01-10 23:19:10 +0000923 netdev_set_default_ethtool_ops(dev, &cfg80211_ethtool_ops);
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100924
925 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200926 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100927 wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
928 dev->priv_flags |= IFF_DONT_BRIDGE;
Johannes Berg704232c2007-04-23 12:20:05 -0700929 break;
Johannes Berg04a773a2009-04-19 21:24:32 +0200930 case NETDEV_GOING_DOWN:
Stanislaw Gruszka81256962013-02-28 10:55:25 +0100931 cfg80211_leave(rdev, wdev);
Johannes Berg01a0ac42009-08-20 21:36:16 +0200932 break;
933 case NETDEV_DOWN:
Michal Kaziordbbae262012-06-29 12:47:01 +0200934 cfg80211_update_iface_num(rdev, wdev->iftype, -1);
Johannes Bergc5a7e582012-07-04 13:28:18 +0200935 dev_hold(dev);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100936 queue_work(cfg80211_wq, &wdev->cleanup_work);
Johannes Berg04a773a2009-04-19 21:24:32 +0200937 break;
938 case NETDEV_UP:
Johannes Bergad002392009-08-18 19:51:57 +0200939 /*
940 * If we have a really quick DOWN/UP succession we may
941 * have this work still pending ... cancel it and see
942 * if it was pending, in which case we need to account
943 * for some of the work it would have done.
944 */
945 if (cancel_work_sync(&wdev->cleanup_work)) {
946 mutex_lock(&rdev->devlist_mtx);
947 rdev->opencount--;
948 mutex_unlock(&rdev->devlist_mtx);
949 dev_put(dev);
950 }
Johannes Berg4290cb42012-07-12 22:19:48 +0200951 cfg80211_update_iface_num(rdev, wdev->iftype, 1);
Johannes Berg667503dd2009-07-07 03:56:11 +0200952 cfg80211_lock_rdev(rdev);
Johannes Bergaee83ea2009-08-09 11:51:29 +0200953 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +0100954 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200955 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200956 switch (wdev->iftype) {
Johannes Berg29cbe682010-12-03 09:20:44 +0100957#ifdef CONFIG_CFG80211_WEXT
Johannes Bergf2129352009-07-01 21:26:56 +0200958 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200959 cfg80211_ibss_wext_join(rdev, wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200960 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200961 case NL80211_IFTYPE_STATION:
Johannes Bergfffd0932009-07-08 14:22:54 +0200962 cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200963 break;
Johannes Berg29cbe682010-12-03 09:20:44 +0100964#endif
Javier Cardonac80d5452010-12-16 17:37:49 -0800965#ifdef CONFIG_MAC80211_MESH
Johannes Berg29cbe682010-12-03 09:20:44 +0100966 case NL80211_IFTYPE_MESH_POINT:
Javier Cardonac80d5452010-12-16 17:37:49 -0800967 {
968 /* backward compat code... */
969 struct mesh_setup setup;
970 memcpy(&setup, &default_mesh_setup,
971 sizeof(setup));
972 /* back compat only needed for mesh_id */
973 setup.mesh_id = wdev->ssid;
974 setup.mesh_id_len = wdev->mesh_id_up_len;
975 if (wdev->mesh_id_up_len)
976 __cfg80211_join_mesh(rdev, dev,
977 &setup,
978 &default_mesh_config);
979 break;
980 }
981#endif
Johannes Bergf2129352009-07-01 21:26:56 +0200982 default:
983 break;
984 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200985 wdev_unlock(wdev);
Johannes Bergf9f47522013-03-19 15:04:07 +0100986 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Bergad002392009-08-18 19:51:57 +0200987 rdev->opencount++;
Johannes Bergaee83ea2009-08-09 11:51:29 +0200988 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200989 cfg80211_unlock_rdev(rdev);
Juuso Oikarinenbf6a0572011-01-31 15:52:58 +0200990
991 /*
992 * Configure power management to the driver here so that its
993 * correctly set also after interface type changes etc.
994 */
Eliad Peller5966f2d2011-07-19 12:57:13 +0300995 if ((wdev->iftype == NL80211_IFTYPE_STATION ||
996 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
Juuso Oikarinenbf6a0572011-01-31 15:52:58 +0200997 rdev->ops->set_power_mgmt)
Hila Gonene35e4d22012-06-27 17:19:42 +0300998 if (rdev_set_power_mgmt(rdev, dev, wdev->ps,
999 wdev->ps_timeout)) {
Juuso Oikarinenbf6a0572011-01-31 15:52:58 +02001000 /* assume this means it's off */
1001 wdev->ps = false;
1002 }
Johannes Berg2a783c12009-07-01 21:26:45 +02001003 break;
Johannes Berg704232c2007-04-23 12:20:05 -07001004 case NETDEV_UNREGISTER:
Johannes Berg0ff6ce72009-08-17 12:25:37 +02001005 /*
1006 * NB: cannot take rdev->mtx here because this may be
1007 * called within code protected by it when interfaces
1008 * are removed with nl80211.
1009 */
Johannes Berg704232c2007-04-23 12:20:05 -07001010 mutex_lock(&rdev->devlist_mtx);
Johannes Berge40cbdac2009-07-30 14:04:01 +02001011 /*
1012 * It is possible to get NETDEV_UNREGISTER
1013 * multiple times. To detect that, check
1014 * that the interface is still on the list
1015 * of registered interfaces, and only then
1016 * remove and clean it up.
1017 */
Johannes Berg2a783c12009-07-01 21:26:45 +02001018 if (!list_empty(&wdev->list)) {
Johannes Berg704232c2007-04-23 12:20:05 -07001019 sysfs_remove_link(&dev->dev.kobj, "phy80211");
Johannes Berg5f2aa252010-01-17 15:49:02 +01001020 list_del_rcu(&wdev->list);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001021 rdev->devlist_generation++;
Johannes Berg2e161f72010-08-12 15:38:38 +02001022 cfg80211_mlme_purge_registrations(wdev);
Johannes Berg3d23e342009-09-29 23:27:28 +02001023#ifdef CONFIG_CFG80211_WEXT
Johannes Berge40cbdac2009-07-30 14:04:01 +02001024 kfree(wdev->wext.keys);
1025#endif
Johannes Berg704232c2007-04-23 12:20:05 -07001026 }
1027 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg5f2aa252010-01-17 15:49:02 +01001028 /*
1029 * synchronise (so that we won't find this netdev
1030 * from other code any more) and then clear the list
1031 * head so that the above code can safely check for
1032 * !list_empty() to avoid double-cleanup.
1033 */
1034 synchronize_rcu();
1035 INIT_LIST_HEAD(&wdev->list);
Daniel Drake1f6fc432012-08-02 18:41:48 +01001036 /*
1037 * Ensure that all events have been processed and
1038 * freed.
1039 */
1040 cfg80211_process_wdev_events(wdev);
Johannes Berg704232c2007-04-23 12:20:05 -07001041 break;
Johannes Berg1f87f7d2009-06-02 13:01:41 +02001042 case NETDEV_PRE_UP:
Johannes Berg0b20633d2009-07-07 03:56:13 +02001043 if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
1044 return notifier_from_errno(-EOPNOTSUPP);
Johannes Berg1f87f7d2009-06-02 13:01:41 +02001045 if (rfkill_blocked(rdev->rfkill))
1046 return notifier_from_errno(-ERFKILL);
Michal Kaziore4e32452012-06-29 12:47:08 +02001047 mutex_lock(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +02001048 ret = cfg80211_can_add_interface(rdev, wdev->iftype);
Michal Kaziore4e32452012-06-29 12:47:08 +02001049 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +02001050 if (ret)
1051 return notifier_from_errno(ret);
Johannes Berg1f87f7d2009-06-02 13:01:41 +02001052 break;
Johannes Berg704232c2007-04-23 12:20:05 -07001053 }
1054
Johannes Berg1f87f7d2009-06-02 13:01:41 +02001055 return NOTIFY_DONE;
Johannes Berg704232c2007-04-23 12:20:05 -07001056}
1057
1058static struct notifier_block cfg80211_netdev_notifier = {
1059 .notifier_call = cfg80211_netdev_notifier_call,
1060};
1061
Johannes Berg463d0182009-07-14 00:33:35 +02001062static void __net_exit cfg80211_pernet_exit(struct net *net)
1063{
1064 struct cfg80211_registered_device *rdev;
1065
1066 rtnl_lock();
1067 mutex_lock(&cfg80211_mutex);
1068 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1069 if (net_eq(wiphy_net(&rdev->wiphy), net))
1070 WARN_ON(cfg80211_switch_netns(rdev, &init_net));
1071 }
1072 mutex_unlock(&cfg80211_mutex);
1073 rtnl_unlock();
1074}
1075
1076static struct pernet_operations cfg80211_pernet_ops = {
1077 .exit = cfg80211_pernet_exit,
1078};
1079
1080static int __init cfg80211_init(void)
Johannes Berg704232c2007-04-23 12:20:05 -07001081{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001082 int err;
1083
Johannes Berg463d0182009-07-14 00:33:35 +02001084 err = register_pernet_device(&cfg80211_pernet_ops);
1085 if (err)
1086 goto out_fail_pernet;
1087
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001088 err = wiphy_sysfs_init();
Johannes Berg704232c2007-04-23 12:20:05 -07001089 if (err)
1090 goto out_fail_sysfs;
1091
1092 err = register_netdevice_notifier(&cfg80211_netdev_notifier);
1093 if (err)
1094 goto out_fail_notifier;
1095
Johannes Berg55682962007-09-20 13:09:35 -04001096 err = nl80211_init();
1097 if (err)
1098 goto out_fail_nl80211;
1099
Johannes Berg704232c2007-04-23 12:20:05 -07001100 ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
1101
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001102 err = regulatory_init();
1103 if (err)
1104 goto out_fail_reg;
1105
Alban Browaeyse60d7442009-11-25 15:13:00 +01001106 cfg80211_wq = create_singlethread_workqueue("cfg80211");
Wei Yongjunf00f1882013-03-20 20:22:54 +08001107 if (!cfg80211_wq) {
1108 err = -ENOMEM;
Alban Browaeyse60d7442009-11-25 15:13:00 +01001109 goto out_fail_wq;
Wei Yongjunf00f1882013-03-20 20:22:54 +08001110 }
Alban Browaeyse60d7442009-11-25 15:13:00 +01001111
Johannes Berg704232c2007-04-23 12:20:05 -07001112 return 0;
1113
Alban Browaeyse60d7442009-11-25 15:13:00 +01001114out_fail_wq:
1115 regulatory_exit();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001116out_fail_reg:
1117 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -04001118out_fail_nl80211:
1119 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
Johannes Berg704232c2007-04-23 12:20:05 -07001120out_fail_notifier:
1121 wiphy_sysfs_exit();
1122out_fail_sysfs:
Johannes Berg463d0182009-07-14 00:33:35 +02001123 unregister_pernet_device(&cfg80211_pernet_ops);
1124out_fail_pernet:
Johannes Berg704232c2007-04-23 12:20:05 -07001125 return err;
1126}
Johannes Berg3a462462007-09-10 13:44:45 +02001127subsys_initcall(cfg80211_init);
Johannes Berg704232c2007-04-23 12:20:05 -07001128
Uwe Kleine-Königf884e382010-06-18 09:38:54 +02001129static void __exit cfg80211_exit(void)
Johannes Berg704232c2007-04-23 12:20:05 -07001130{
1131 debugfs_remove(ieee80211_debugfs_dir);
Johannes Berg55682962007-09-20 13:09:35 -04001132 nl80211_exit();
Johannes Berg704232c2007-04-23 12:20:05 -07001133 unregister_netdevice_notifier(&cfg80211_netdev_notifier);
1134 wiphy_sysfs_exit();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001135 regulatory_exit();
Johannes Berg463d0182009-07-14 00:33:35 +02001136 unregister_pernet_device(&cfg80211_pernet_ops);
Alban Browaeyse60d7442009-11-25 15:13:00 +01001137 destroy_workqueue(cfg80211_wq);
Johannes Berg704232c2007-04-23 12:20:05 -07001138}
1139module_exit(cfg80211_exit);