Commit | Line | Data |
---|---|---|
704232c2 JB |
1 | /* |
2 | * This is the linux wireless configuration interface. | |
3 | * | |
5f2aa25e | 4 | * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net> |
704232c2 JB |
5 | */ |
6 | ||
e9c0268f JP |
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
8 | ||
704232c2 JB |
9 | #include <linux/if.h> |
10 | #include <linux/module.h> | |
11 | #include <linux/err.h> | |
704232c2 | 12 | #include <linux/list.h> |
5a0e3ad6 | 13 | #include <linux/slab.h> |
704232c2 JB |
14 | #include <linux/nl80211.h> |
15 | #include <linux/debugfs.h> | |
16 | #include <linux/notifier.h> | |
17 | #include <linux/device.h> | |
16a832e7 | 18 | #include <linux/etherdevice.h> |
1f87f7d3 | 19 | #include <linux/rtnetlink.h> |
d43c36dc | 20 | #include <linux/sched.h> |
704232c2 JB |
21 | #include <net/genetlink.h> |
22 | #include <net/cfg80211.h> | |
55682965 | 23 | #include "nl80211.h" |
704232c2 JB |
24 | #include "core.h" |
25 | #include "sysfs.h" | |
1ac61302 | 26 | #include "debugfs.h" |
a9a11622 | 27 | #include "wext-compat.h" |
4890e3be | 28 | #include "ethtool.h" |
e35e4d28 | 29 | #include "rdev-ops.h" |
704232c2 JB |
30 | |
31 | /* name for sysfs, %d is appended */ | |
32 | #define PHY_NAME "phy" | |
33 | ||
34 | MODULE_AUTHOR("Johannes Berg"); | |
35 | MODULE_LICENSE("GPL"); | |
36 | MODULE_DESCRIPTION("wireless configuration support"); | |
fb4e1568 | 37 | MODULE_ALIAS_GENL_FAMILY(NL80211_GENL_NAME); |
704232c2 | 38 | |
5f2aa25e | 39 | /* RCU-protected (and cfg80211_mutex for writers) */ |
79c97e97 | 40 | LIST_HEAD(cfg80211_rdev_list); |
f5ea9120 | 41 | int cfg80211_rdev_list_generation; |
a1794390 | 42 | |
a1794390 | 43 | DEFINE_MUTEX(cfg80211_mutex); |
704232c2 JB |
44 | |
45 | /* for debugfs */ | |
46 | static struct dentry *ieee80211_debugfs_dir; | |
47 | ||
e60d7443 AB |
48 | /* for the cleanup, scan and event works */ |
49 | struct workqueue_struct *cfg80211_wq; | |
50 | ||
40db6c77 AK |
51 | static bool cfg80211_disable_40mhz_24ghz; |
52 | module_param(cfg80211_disable_40mhz_24ghz, bool, 0644); | |
53 | MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz, | |
54 | "Disable 40MHz support in the 2.4GHz band"); | |
55 | ||
806a9e39 | 56 | /* requires cfg80211_mutex to be held! */ |
79c97e97 | 57 | struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx) |
55682965 | 58 | { |
79c97e97 | 59 | struct cfg80211_registered_device *result = NULL, *rdev; |
55682965 | 60 | |
761cf7ec LR |
61 | assert_cfg80211_lock(); |
62 | ||
79c97e97 JB |
63 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
64 | if (rdev->wiphy_idx == wiphy_idx) { | |
65 | result = rdev; | |
55682965 JB |
66 | break; |
67 | } | |
68 | } | |
69 | ||
70 | return result; | |
71 | } | |
72 | ||
806a9e39 LR |
73 | int get_wiphy_idx(struct wiphy *wiphy) |
74 | { | |
f4173766 JB |
75 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
76 | ||
79c97e97 | 77 | return rdev->wiphy_idx; |
806a9e39 LR |
78 | } |
79 | ||
79c97e97 | 80 | /* requires cfg80211_rdev_mutex to be held! */ |
806a9e39 LR |
81 | struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx) |
82 | { | |
79c97e97 | 83 | struct cfg80211_registered_device *rdev; |
806a9e39 | 84 | |
806a9e39 LR |
85 | assert_cfg80211_lock(); |
86 | ||
79c97e97 JB |
87 | rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx); |
88 | if (!rdev) | |
806a9e39 | 89 | return NULL; |
79c97e97 | 90 | return &rdev->wiphy; |
806a9e39 LR |
91 | } |
92 | ||
4bbf4d56 | 93 | /* requires cfg80211_mutex to be held */ |
55682965 JB |
94 | int cfg80211_dev_rename(struct cfg80211_registered_device *rdev, |
95 | char *newname) | |
96 | { | |
79c97e97 | 97 | struct cfg80211_registered_device *rdev2; |
7623225f | 98 | int wiphy_idx, taken = -1, result, digits; |
55682965 | 99 | |
4bbf4d56 | 100 | assert_cfg80211_lock(); |
2940bb69 | 101 | |
7623225f JB |
102 | /* 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 | ||
2940bb69 | 118 | /* Ignore nop renames */ |
2940bb69 | 119 | if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0) |
4bbf4d56 | 120 | return 0; |
2940bb69 EB |
121 | |
122 | /* Ensure another device does not already have this name. */ | |
79c97e97 JB |
123 | list_for_each_entry(rdev2, &cfg80211_rdev_list, list) |
124 | if (strcmp(newname, dev_name(&rdev2->wiphy.dev)) == 0) | |
7623225f | 125 | return -EINVAL; |
55682965 | 126 | |
55682965 JB |
127 | result = device_rename(&rdev->wiphy.dev, newname); |
128 | if (result) | |
4bbf4d56 | 129 | return result; |
55682965 | 130 | |
33c0360b JB |
131 | if (rdev->wiphy.debugfsdir && |
132 | !debugfs_rename(rdev->wiphy.debugfsdir->d_parent, | |
55682965 JB |
133 | rdev->wiphy.debugfsdir, |
134 | rdev->wiphy.debugfsdir->d_parent, | |
135 | newname)) | |
e9c0268f | 136 | pr_err("failed to rename debugfs dir to %s!\n", newname); |
55682965 | 137 | |
4bbf4d56 | 138 | nl80211_notify_dev_rename(rdev); |
55682965 | 139 | |
4bbf4d56 | 140 | return 0; |
55682965 JB |
141 | } |
142 | ||
463d0183 JB |
143 | int cfg80211_switch_netns(struct cfg80211_registered_device *rdev, |
144 | struct net *net) | |
145 | { | |
146 | struct wireless_dev *wdev; | |
147 | int err = 0; | |
148 | ||
5be83de5 | 149 | if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK)) |
463d0183 JB |
150 | return -EOPNOTSUPP; |
151 | ||
89a54e48 | 152 | list_for_each_entry(wdev, &rdev->wdev_list, list) { |
ba22fb5b JB |
153 | if (!wdev->netdev) |
154 | continue; | |
463d0183 JB |
155 | 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 | ||
89a54e48 | 166 | list_for_each_entry_continue_reverse(wdev, &rdev->wdev_list, |
463d0183 | 167 | list) { |
ba22fb5b JB |
168 | if (!wdev->netdev) |
169 | continue; | |
463d0183 JB |
170 | 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 | } | |
04600794 JB |
176 | |
177 | return err; | |
463d0183 JB |
178 | } |
179 | ||
180 | wiphy_net_set(&rdev->wiphy, net); | |
181 | ||
04600794 JB |
182 | err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev)); |
183 | WARN_ON(err); | |
184 | ||
185 | return 0; | |
463d0183 JB |
186 | } |
187 | ||
1f87f7d3 JB |
188 | static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data) |
189 | { | |
79c97e97 | 190 | struct cfg80211_registered_device *rdev = data; |
1f87f7d3 | 191 | |
e35e4d28 | 192 | rdev_rfkill_poll(rdev); |
1f87f7d3 JB |
193 | } |
194 | ||
f9f47529 JB |
195 | void 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 | ||
1f87f7d3 JB |
228 | static int cfg80211_rfkill_set_block(void *data, bool blocked) |
229 | { | |
79c97e97 | 230 | struct cfg80211_registered_device *rdev = data; |
1f87f7d3 JB |
231 | struct wireless_dev *wdev; |
232 | ||
233 | if (!blocked) | |
234 | return 0; | |
235 | ||
236 | rtnl_lock(); | |
f9f47529 JB |
237 | |
238 | /* read-only iteration need not hold the devlist_mtx */ | |
1f87f7d3 | 239 | |
98104fde JB |
240 | list_for_each_entry(wdev, &rdev->wdev_list, list) { |
241 | if (wdev->netdev) { | |
ba22fb5b | 242 | dev_close(wdev->netdev); |
98104fde JB |
243 | continue; |
244 | } | |
245 | /* otherwise, check iftype */ | |
246 | switch (wdev->iftype) { | |
247 | case NL80211_IFTYPE_P2P_DEVICE: | |
f9f47529 JB |
248 | /* 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); | |
98104fde JB |
254 | break; |
255 | default: | |
256 | break; | |
257 | } | |
258 | } | |
1f87f7d3 | 259 | |
1f87f7d3 JB |
260 | rtnl_unlock(); |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static void cfg80211_rfkill_sync_work(struct work_struct *work) | |
266 | { | |
79c97e97 | 267 | struct cfg80211_registered_device *rdev; |
1f87f7d3 | 268 | |
79c97e97 JB |
269 | rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync); |
270 | cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill)); | |
1f87f7d3 JB |
271 | } |
272 | ||
667503dd JB |
273 | static void cfg80211_event_work(struct work_struct *work) |
274 | { | |
275 | struct cfg80211_registered_device *rdev; | |
667503dd JB |
276 | |
277 | rdev = container_of(work, struct cfg80211_registered_device, | |
278 | event_work); | |
279 | ||
280 | rtnl_lock(); | |
281 | cfg80211_lock_rdev(rdev); | |
667503dd | 282 | |
3d54d255 | 283 | cfg80211_process_rdev_events(rdev); |
667503dd JB |
284 | cfg80211_unlock_rdev(rdev); |
285 | rtnl_unlock(); | |
286 | } | |
287 | ||
704232c2 JB |
288 | /* exported functions */ |
289 | ||
3dcf670b | 290 | struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv) |
704232c2 | 291 | { |
73810b77 | 292 | static atomic_t wiphy_counter = ATOMIC_INIT(0); |
7623225f JB |
293 | |
294 | struct cfg80211_registered_device *rdev; | |
704232c2 JB |
295 | int alloc_size; |
296 | ||
0b20633d JB |
297 | 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); | |
29cbe68c | 304 | WARN_ON(ops->join_mesh && !ops->leave_mesh); |
41ade00f | 305 | |
79c97e97 | 306 | alloc_size = sizeof(*rdev) + sizeof_priv; |
704232c2 | 307 | |
79c97e97 JB |
308 | rdev = kzalloc(alloc_size, GFP_KERNEL); |
309 | if (!rdev) | |
704232c2 JB |
310 | return NULL; |
311 | ||
79c97e97 | 312 | rdev->ops = ops; |
704232c2 | 313 | |
73810b77 | 314 | rdev->wiphy_idx = atomic_inc_return(&wiphy_counter); |
a4d73ee1 | 315 | |
f4173766 | 316 | if (unlikely(rdev->wiphy_idx < 0)) { |
7623225f | 317 | /* ugh, wrapped! */ |
73810b77 | 318 | atomic_dec(&wiphy_counter); |
79c97e97 | 319 | kfree(rdev); |
704232c2 JB |
320 | return NULL; |
321 | } | |
704232c2 | 322 | |
7623225f JB |
323 | /* give it a proper name */ |
324 | dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx); | |
325 | ||
79c97e97 JB |
326 | mutex_init(&rdev->mtx); |
327 | mutex_init(&rdev->devlist_mtx); | |
c10841ca | 328 | mutex_init(&rdev->sched_scan_mtx); |
89a54e48 | 329 | INIT_LIST_HEAD(&rdev->wdev_list); |
37c73b5f BG |
330 | INIT_LIST_HEAD(&rdev->beacon_registrations); |
331 | spin_lock_init(&rdev->beacon_registrations_lock); | |
79c97e97 JB |
332 | spin_lock_init(&rdev->bss_lock); |
333 | INIT_LIST_HEAD(&rdev->bss_list); | |
334 | INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done); | |
807f8a8c | 335 | INIT_WORK(&rdev->sched_scan_results_wk, __cfg80211_sched_scan_results); |
04f39047 SW |
336 | INIT_DELAYED_WORK(&rdev->dfs_update_channels_wk, |
337 | cfg80211_dfs_channels_update_work); | |
3d23e349 JB |
338 | #ifdef CONFIG_CFG80211_WEXT |
339 | rdev->wiphy.wext = &cfg80211_wext_handler; | |
340 | #endif | |
341 | ||
79c97e97 JB |
342 | device_initialize(&rdev->wiphy.dev); |
343 | rdev->wiphy.dev.class = &ieee80211_class; | |
344 | rdev->wiphy.dev.platform_data = rdev; | |
345 | ||
5be83de5 JB |
346 | #ifdef CONFIG_CFG80211_DEFAULT_PS |
347 | rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT; | |
348 | #endif | |
16cb9d42 | 349 | |
463d0183 JB |
350 | wiphy_net_set(&rdev->wiphy, &init_net); |
351 | ||
79c97e97 JB |
352 | 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); | |
356 | ||
357 | if (!rdev->rfkill) { | |
358 | kfree(rdev); | |
1f87f7d3 JB |
359 | return NULL; |
360 | } | |
361 | ||
79c97e97 JB |
362 | 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); | |
1f87f7d3 | 365 | |
ad002395 JB |
366 | init_waitqueue_head(&rdev->dev_wait); |
367 | ||
b9a5f8ca JM |
368 | /* |
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 | */ | |
79c97e97 JB |
373 | 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; | |
81077e82 | 377 | rdev->wiphy.coverage_class = 0; |
b9a5f8ca | 378 | |
d0ae708d | 379 | rdev->wiphy.features = NL80211_FEATURE_SCAN_FLUSH; |
15d6030b | 380 | |
79c97e97 | 381 | return &rdev->wiphy; |
704232c2 JB |
382 | } |
383 | EXPORT_SYMBOL(wiphy_new); | |
384 | ||
7527a782 JB |
385 | static int wiphy_verify_combinations(struct wiphy *wiphy) |
386 | { | |
387 | const struct ieee80211_iface_combination *c; | |
388 | int i, j; | |
389 | ||
7527a782 JB |
390 | 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 | ||
11c4a075 SW |
396 | /* |
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)) | |
7527a782 JB |
401 | return -EINVAL; |
402 | ||
403 | /* Need at least one channel */ | |
404 | if (WARN_ON(!c->num_different_channels)) | |
405 | return -EINVAL; | |
406 | ||
d4e50c59 MK |
407 | /* |
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 | ||
11c4a075 SW |
415 | /* DFS only works on one channel. */ |
416 | if (WARN_ON(c->radar_detect_widths && | |
417 | (c->num_different_channels > 1))) | |
418 | return -EINVAL; | |
419 | ||
7527a782 JB |
420 | 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 | ||
98104fde JB |
441 | /* 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 | ||
7527a782 JB |
446 | 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 | ||
704232c2 JB |
463 | int wiphy_register(struct wiphy *wiphy) |
464 | { | |
79c97e97 | 465 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
704232c2 | 466 | int res; |
8318d78a JB |
467 | enum ieee80211_band band; |
468 | struct ieee80211_supported_band *sband; | |
469 | bool have_band = false; | |
470 | int i; | |
f59ac048 LR |
471 | u16 ifmodes = wiphy->interface_modes; |
472 | ||
dfb89c56 | 473 | #ifdef CONFIG_PM |
77dbbb13 JB |
474 | if (WARN_ON((wiphy->wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) && |
475 | !(wiphy->wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY))) | |
476 | return -EINVAL; | |
dfb89c56 | 477 | #endif |
77dbbb13 | 478 | |
562a7480 JB |
479 | if (WARN_ON(wiphy->ap_sme_capa && |
480 | !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME))) | |
481 | return -EINVAL; | |
482 | ||
ef15aac6 JB |
483 | 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 | ||
77765eaf VT |
492 | 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 | ||
ef15aac6 JB |
497 | if (wiphy->addresses) |
498 | memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN); | |
499 | ||
f59ac048 LR |
500 | /* sanity check ifmodes */ |
501 | WARN_ON(!ifmodes); | |
2e161f78 | 502 | ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1; |
f59ac048 LR |
503 | if (WARN_ON(ifmodes != wiphy->interface_modes)) |
504 | wiphy->interface_modes = ifmodes; | |
8318d78a | 505 | |
7527a782 JB |
506 | res = wiphy_verify_combinations(wiphy); |
507 | if (res) | |
508 | return res; | |
509 | ||
8318d78a JB |
510 | /* 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; | |
3a0c52a6 VK |
517 | 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)) | |
881d948c JB |
525 | return -EINVAL; |
526 | ||
40db6c77 AK |
527 | /* |
528 | * 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 | ||
881d948c JB |
539 | /* |
540 | * 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)) | |
8318d78a | 545 | return -EINVAL; |
8318d78a JB |
546 | |
547 | for (i = 0; i < sband->n_channels; i++) { | |
548 | sband->channels[i].orig_flags = | |
549 | sband->channels[i].flags; | |
c4a9fafc | 550 | sband->channels[i].orig_mag = INT_MAX; |
8318d78a JB |
551 | 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 | ||
dfb89c56 | 564 | #ifdef CONFIG_PM |
ff1b6e69 JB |
565 | 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 | } | |
dfb89c56 | 571 | #endif |
ff1b6e69 | 572 | |
8318d78a JB |
573 | /* check and set up bitrates */ |
574 | ieee80211_set_bitrate_flags(wiphy); | |
575 | ||
5a652052 MB |
576 | mutex_lock(&cfg80211_mutex); |
577 | ||
79c97e97 | 578 | res = device_add(&rdev->wiphy.dev); |
c3d34d5d JL |
579 | if (res) { |
580 | mutex_unlock(&cfg80211_mutex); | |
581 | return res; | |
582 | } | |
1f87f7d3 | 583 | |
2f0accc1 | 584 | /* set up regulatory info */ |
57b5ce07 | 585 | wiphy_regulatory_register(wiphy); |
2f0accc1 | 586 | |
5f2aa25e | 587 | list_add_rcu(&rdev->list, &cfg80211_rdev_list); |
f5ea9120 | 588 | cfg80211_rdev_list_generation++; |
704232c2 JB |
589 | |
590 | /* add to debugfs */ | |
79c97e97 JB |
591 | rdev->wiphy.debugfsdir = |
592 | debugfs_create_dir(wiphy_name(&rdev->wiphy), | |
704232c2 | 593 | ieee80211_debugfs_dir); |
79c97e97 JB |
594 | if (IS_ERR(rdev->wiphy.debugfsdir)) |
595 | rdev->wiphy.debugfsdir = NULL; | |
704232c2 | 596 | |
5be83de5 | 597 | if (wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) { |
73d54c9e LR |
598 | 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 | ||
79c97e97 | 608 | cfg80211_debugfs_rdev_add(rdev); |
5a652052 | 609 | mutex_unlock(&cfg80211_mutex); |
1ac61302 | 610 | |
c3d34d5d JL |
611 | /* |
612 | * due to a locking dependency this has to be outside of the | |
613 | * cfg80211_mutex lock | |
614 | */ | |
615 | res = rfkill_register(rdev->rfkill); | |
03cd7e4e JB |
616 | 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 | } | |
c3d34d5d | 626 | |
ecb44335 SG |
627 | rtnl_lock(); |
628 | rdev->wiphy.registered = true; | |
629 | rtnl_unlock(); | |
2f0accc1 | 630 | return 0; |
704232c2 JB |
631 | } |
632 | EXPORT_SYMBOL(wiphy_register); | |
633 | ||
1f87f7d3 JB |
634 | void wiphy_rfkill_start_polling(struct wiphy *wiphy) |
635 | { | |
79c97e97 | 636 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
1f87f7d3 | 637 | |
79c97e97 | 638 | if (!rdev->ops->rfkill_poll) |
1f87f7d3 | 639 | return; |
79c97e97 JB |
640 | rdev->rfkill_ops.poll = cfg80211_rfkill_poll; |
641 | rfkill_resume_polling(rdev->rfkill); | |
1f87f7d3 JB |
642 | } |
643 | EXPORT_SYMBOL(wiphy_rfkill_start_polling); | |
644 | ||
645 | void wiphy_rfkill_stop_polling(struct wiphy *wiphy) | |
646 | { | |
79c97e97 | 647 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
1f87f7d3 | 648 | |
79c97e97 | 649 | rfkill_pause_polling(rdev->rfkill); |
1f87f7d3 JB |
650 | } |
651 | EXPORT_SYMBOL(wiphy_rfkill_stop_polling); | |
652 | ||
704232c2 JB |
653 | void wiphy_unregister(struct wiphy *wiphy) |
654 | { | |
79c97e97 | 655 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
704232c2 | 656 | |
ecb44335 SG |
657 | rtnl_lock(); |
658 | rdev->wiphy.registered = false; | |
659 | rtnl_unlock(); | |
660 | ||
79c97e97 | 661 | rfkill_unregister(rdev->rfkill); |
1f87f7d3 | 662 | |
f16bfc1c | 663 | /* protect the device list */ |
a1794390 | 664 | mutex_lock(&cfg80211_mutex); |
704232c2 | 665 | |
ad002395 JB |
666 | wait_event(rdev->dev_wait, ({ |
667 | int __count; | |
668 | mutex_lock(&rdev->devlist_mtx); | |
669 | __count = rdev->opencount; | |
670 | mutex_unlock(&rdev->devlist_mtx); | |
c4f60846 | 671 | __count == 0; })); |
ad002395 JB |
672 | |
673 | mutex_lock(&rdev->devlist_mtx); | |
89a54e48 | 674 | BUG_ON(!list_empty(&rdev->wdev_list)); |
ad002395 JB |
675 | mutex_unlock(&rdev->devlist_mtx); |
676 | ||
677 | /* | |
678 | * First remove the hardware from everywhere, this makes | |
679 | * it impossible to find from userspace. | |
680 | */ | |
7bcfaf2f | 681 | debugfs_remove_recursive(rdev->wiphy.debugfsdir); |
5f2aa25e JB |
682 | list_del_rcu(&rdev->list); |
683 | synchronize_rcu(); | |
f16bfc1c JB |
684 | |
685 | /* | |
79c97e97 | 686 | * Try to grab rdev->mtx. If a command is still in progress, |
f16bfc1c JB |
687 | * 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 | |
ad002395 JB |
691 | * 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. | |
f16bfc1c | 694 | */ |
0ff6ce7b | 695 | cfg80211_lock_rdev(rdev); |
ad002395 | 696 | /* nothing */ |
0ff6ce7b | 697 | cfg80211_unlock_rdev(rdev); |
704232c2 | 698 | |
bfead080 LR |
699 | /* |
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); | |
3f2355cb | 704 | |
f5ea9120 | 705 | cfg80211_rdev_list_generation++; |
79c97e97 | 706 | device_del(&rdev->wiphy.dev); |
704232c2 | 707 | |
a1794390 | 708 | mutex_unlock(&cfg80211_mutex); |
6682588a | 709 | |
36e6fea8 | 710 | flush_work(&rdev->scan_done_wk); |
6682588a | 711 | cancel_work_sync(&rdev->conn_work); |
6682588a | 712 | flush_work(&rdev->event_work); |
04f39047 | 713 | cancel_delayed_work_sync(&rdev->dfs_update_channels_wk); |
6d52563f JB |
714 | |
715 | if (rdev->wowlan && rdev->ops->set_wakeup) | |
e35e4d28 | 716 | rdev_set_wakeup(rdev, false); |
6d52563f | 717 | cfg80211_rdev_free_wowlan(rdev); |
704232c2 JB |
718 | } |
719 | EXPORT_SYMBOL(wiphy_unregister); | |
720 | ||
79c97e97 | 721 | void cfg80211_dev_free(struct cfg80211_registered_device *rdev) |
704232c2 | 722 | { |
2a519311 | 723 | struct cfg80211_internal_bss *scan, *tmp; |
37c73b5f | 724 | struct cfg80211_beacon_registration *reg, *treg; |
79c97e97 JB |
725 | rfkill_destroy(rdev->rfkill); |
726 | mutex_destroy(&rdev->mtx); | |
727 | mutex_destroy(&rdev->devlist_mtx); | |
c10841ca | 728 | mutex_destroy(&rdev->sched_scan_mtx); |
37c73b5f BG |
729 | list_for_each_entry_safe(reg, treg, &rdev->beacon_registrations, list) { |
730 | list_del(®->list); | |
731 | kfree(reg); | |
732 | } | |
79c97e97 | 733 | list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list) |
5b112d3d | 734 | cfg80211_put_bss(&rdev->wiphy, &scan->pub); |
79c97e97 | 735 | kfree(rdev); |
704232c2 JB |
736 | } |
737 | ||
738 | void wiphy_free(struct wiphy *wiphy) | |
739 | { | |
740 | put_device(&wiphy->dev); | |
741 | } | |
742 | EXPORT_SYMBOL(wiphy_free); | |
743 | ||
1f87f7d3 JB |
744 | void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked) |
745 | { | |
79c97e97 | 746 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
1f87f7d3 | 747 | |
79c97e97 JB |
748 | if (rfkill_set_hw_state(rdev->rfkill, blocked)) |
749 | schedule_work(&rdev->rfkill_sync); | |
1f87f7d3 JB |
750 | } |
751 | EXPORT_SYMBOL(wiphy_rfkill_set_hw_state); | |
752 | ||
ad002395 JB |
753 | static 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 | ||
f9f47529 | 761 | mutex_lock(&rdev->sched_scan_mtx); |
ad002395 | 762 | |
fd014284 | 763 | if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) { |
ad002395 | 764 | rdev->scan_req->aborted = true; |
01a0ac41 | 765 | ___cfg80211_scan_done(rdev, true); |
ad002395 JB |
766 | } |
767 | ||
807f8a8c LC |
768 | if (WARN_ON(rdev->sched_scan_req && |
769 | rdev->sched_scan_req->dev == wdev->netdev)) { | |
770 | __cfg80211_stop_sched_scan(rdev, false); | |
771 | } | |
772 | ||
c10841ca | 773 | mutex_unlock(&rdev->sched_scan_mtx); |
ad002395 JB |
774 | |
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 | ||
98104fde JB |
783 | void 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); | |
f9f47529 | 793 | mutex_lock(&rdev->sched_scan_mtx); |
98104fde JB |
794 | list_del_rcu(&wdev->list); |
795 | rdev->devlist_generation++; | |
796 | ||
797 | switch (wdev->iftype) { | |
798 | case NL80211_IFTYPE_P2P_DEVICE: | |
f9f47529 | 799 | cfg80211_stop_p2p_device(rdev, wdev); |
98104fde JB |
800 | break; |
801 | default: | |
802 | WARN_ON_ONCE(1); | |
803 | break; | |
804 | } | |
f9f47529 | 805 | mutex_unlock(&rdev->sched_scan_mtx); |
98104fde JB |
806 | mutex_unlock(&rdev->devlist_mtx); |
807 | } | |
808 | EXPORT_SYMBOL(cfg80211_unregister_wdev); | |
809 | ||
053a93dd MH |
810 | static struct device_type wiphy_type = { |
811 | .name = "wlan", | |
812 | }; | |
813 | ||
dbbae26a MK |
814 | void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev, |
815 | enum nl80211_iftype iftype, int num) | |
816 | { | |
c5a7e582 | 817 | ASSERT_RTNL(); |
dbbae26a MK |
818 | |
819 | rdev->num_running_ifaces += num; | |
820 | if (iftype == NL80211_IFTYPE_MONITOR) | |
821 | rdev->num_running_monitor_ifaces += num; | |
dbbae26a MK |
822 | } |
823 | ||
81256969 SG |
824 | void 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); | |
81256969 SG |
848 | 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 | ||
c4f60846 | 863 | static int cfg80211_netdev_notifier_call(struct notifier_block *nb, |
704232c2 JB |
864 | unsigned long state, |
865 | void *ndev) | |
866 | { | |
867 | struct net_device *dev = ndev; | |
2a783c13 | 868 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
704232c2 | 869 | struct cfg80211_registered_device *rdev; |
7527a782 | 870 | int ret; |
704232c2 | 871 | |
2a783c13 | 872 | if (!wdev) |
1f87f7d3 | 873 | return NOTIFY_DONE; |
704232c2 | 874 | |
2a783c13 | 875 | rdev = wiphy_to_dev(wdev->wiphy); |
704232c2 | 876 | |
2a783c13 | 877 | WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED); |
60719ffd | 878 | |
704232c2 | 879 | switch (state) { |
053a93dd MH |
880 | case NETDEV_POST_INIT: |
881 | SET_NETDEV_DEVTYPE(dev, &wiphy_type); | |
882 | break; | |
704232c2 | 883 | case NETDEV_REGISTER: |
0ff6ce7b JB |
884 | /* |
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 | */ | |
667503dd | 889 | mutex_init(&wdev->mtx); |
ad002395 | 890 | INIT_WORK(&wdev->cleanup_work, wdev_cleanup_work); |
667503dd JB |
891 | INIT_LIST_HEAD(&wdev->event_list); |
892 | spin_lock_init(&wdev->event_lock); | |
2e161f78 JB |
893 | INIT_LIST_HEAD(&wdev->mgmt_registrations); |
894 | spin_lock_init(&wdev->mgmt_registrations_lock); | |
026331c4 | 895 | |
704232c2 | 896 | mutex_lock(&rdev->devlist_mtx); |
89a54e48 JB |
897 | wdev->identifier = ++rdev->wdev_id; |
898 | list_add_rcu(&wdev->list, &rdev->wdev_list); | |
f5ea9120 | 899 | rdev->devlist_generation++; |
463d0183 JB |
900 | /* can only change netns with wiphy */ |
901 | dev->features |= NETIF_F_NETNS_LOCAL; | |
902 | ||
704232c2 JB |
903 | if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj, |
904 | "phy80211")) { | |
e9c0268f | 905 | pr_err("failed to add phy80211 symlink to netdev!\n"); |
704232c2 | 906 | } |
2a783c13 | 907 | wdev->netdev = dev; |
b23aa676 | 908 | wdev->sme_state = CFG80211_SME_IDLE; |
bc92afd9 | 909 | mutex_unlock(&rdev->devlist_mtx); |
3d23e349 | 910 | #ifdef CONFIG_CFG80211_WEXT |
2a783c13 JB |
911 | wdev->wext.default_key = -1; |
912 | wdev->wext.default_mgmt_key = -1; | |
f2129354 | 913 | wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC; |
ffb9eb3d KV |
914 | #endif |
915 | ||
5be83de5 | 916 | if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT) |
ffb9eb3d | 917 | wdev->ps = true; |
5be83de5 | 918 | else |
ffb9eb3d | 919 | wdev->ps = false; |
9043f3b8 JO |
920 | /* allow mac80211 to determine the timeout */ |
921 | wdev->ps_timeout = -1; | |
ffb9eb3d | 922 | |
d07d7507 | 923 | netdev_set_default_ethtool_ops(dev, &cfg80211_ethtool_ops); |
ad4bb6f8 JB |
924 | |
925 | if ((wdev->iftype == NL80211_IFTYPE_STATION || | |
074ac8df | 926 | wdev->iftype == NL80211_IFTYPE_P2P_CLIENT || |
ad4bb6f8 JB |
927 | wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr) |
928 | dev->priv_flags |= IFF_DONT_BRIDGE; | |
704232c2 | 929 | break; |
04a773ad | 930 | case NETDEV_GOING_DOWN: |
81256969 | 931 | cfg80211_leave(rdev, wdev); |
01a0ac41 JB |
932 | break; |
933 | case NETDEV_DOWN: | |
dbbae26a | 934 | cfg80211_update_iface_num(rdev, wdev->iftype, -1); |
c5a7e582 | 935 | dev_hold(dev); |
e60d7443 | 936 | queue_work(cfg80211_wq, &wdev->cleanup_work); |
04a773ad JB |
937 | break; |
938 | case NETDEV_UP: | |
ad002395 JB |
939 | /* |
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 | } | |
4290cb4b | 951 | cfg80211_update_iface_num(rdev, wdev->iftype, 1); |
667503dd | 952 | cfg80211_lock_rdev(rdev); |
aee83eaf | 953 | mutex_lock(&rdev->devlist_mtx); |
f9f47529 | 954 | mutex_lock(&rdev->sched_scan_mtx); |
667503dd | 955 | wdev_lock(wdev); |
f2129354 | 956 | switch (wdev->iftype) { |
29cbe68c | 957 | #ifdef CONFIG_CFG80211_WEXT |
f2129354 | 958 | case NL80211_IFTYPE_ADHOC: |
fffd0934 | 959 | cfg80211_ibss_wext_join(rdev, wdev); |
04a773ad | 960 | break; |
f2129354 | 961 | case NL80211_IFTYPE_STATION: |
fffd0934 | 962 | cfg80211_mgd_wext_connect(rdev, wdev); |
f2129354 | 963 | break; |
29cbe68c | 964 | #endif |
c80d545d | 965 | #ifdef CONFIG_MAC80211_MESH |
29cbe68c | 966 | case NL80211_IFTYPE_MESH_POINT: |
c80d545d JC |
967 | { |
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 | |
f2129354 | 982 | default: |
04a773ad | 983 | break; |
f2129354 | 984 | } |
667503dd | 985 | wdev_unlock(wdev); |
f9f47529 | 986 | mutex_unlock(&rdev->sched_scan_mtx); |
ad002395 | 987 | rdev->opencount++; |
aee83eaf | 988 | mutex_unlock(&rdev->devlist_mtx); |
667503dd | 989 | cfg80211_unlock_rdev(rdev); |
bf6a0579 JO |
990 | |
991 | /* | |
992 | * Configure power management to the driver here so that its | |
993 | * correctly set also after interface type changes etc. | |
994 | */ | |
5966f2dd EP |
995 | if ((wdev->iftype == NL80211_IFTYPE_STATION || |
996 | wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) && | |
bf6a0579 | 997 | rdev->ops->set_power_mgmt) |
e35e4d28 HG |
998 | if (rdev_set_power_mgmt(rdev, dev, wdev->ps, |
999 | wdev->ps_timeout)) { | |
bf6a0579 JO |
1000 | /* assume this means it's off */ |
1001 | wdev->ps = false; | |
1002 | } | |
2a783c13 | 1003 | break; |
704232c2 | 1004 | case NETDEV_UNREGISTER: |
0ff6ce7b JB |
1005 | /* |
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 | */ | |
704232c2 | 1010 | mutex_lock(&rdev->devlist_mtx); |
e40cbdac JB |
1011 | /* |
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 | */ | |
2a783c13 | 1018 | if (!list_empty(&wdev->list)) { |
704232c2 | 1019 | sysfs_remove_link(&dev->dev.kobj, "phy80211"); |
5f2aa25e | 1020 | list_del_rcu(&wdev->list); |
f5ea9120 | 1021 | rdev->devlist_generation++; |
2e161f78 | 1022 | cfg80211_mlme_purge_registrations(wdev); |
3d23e349 | 1023 | #ifdef CONFIG_CFG80211_WEXT |
e40cbdac | 1024 | kfree(wdev->wext.keys); |
fffd0934 | 1025 | #endif |
e40cbdac JB |
1026 | } |
1027 | mutex_unlock(&rdev->devlist_mtx); | |
5f2aa25e JB |
1028 | /* |
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); | |
1f6fc43e DD |
1036 | /* |
1037 | * Ensure that all events have been processed and | |
1038 | * freed. | |
1039 | */ | |
1040 | cfg80211_process_wdev_events(wdev); | |
704232c2 | 1041 | break; |
1f87f7d3 | 1042 | case NETDEV_PRE_UP: |
0b20633d JB |
1043 | if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype))) |
1044 | return notifier_from_errno(-EOPNOTSUPP); | |
1f87f7d3 JB |
1045 | if (rfkill_blocked(rdev->rfkill)) |
1046 | return notifier_from_errno(-ERFKILL); | |
e4e32459 | 1047 | mutex_lock(&rdev->devlist_mtx); |
7527a782 | 1048 | ret = cfg80211_can_add_interface(rdev, wdev->iftype); |
e4e32459 | 1049 | mutex_unlock(&rdev->devlist_mtx); |
7527a782 JB |
1050 | if (ret) |
1051 | return notifier_from_errno(ret); | |
1f87f7d3 | 1052 | break; |
704232c2 JB |
1053 | } |
1054 | ||
1f87f7d3 | 1055 | return NOTIFY_DONE; |
704232c2 JB |
1056 | } |
1057 | ||
1058 | static struct notifier_block cfg80211_netdev_notifier = { | |
1059 | .notifier_call = cfg80211_netdev_notifier_call, | |
1060 | }; | |
1061 | ||
463d0183 JB |
1062 | static 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 | ||
1076 | static struct pernet_operations cfg80211_pernet_ops = { | |
1077 | .exit = cfg80211_pernet_exit, | |
1078 | }; | |
1079 | ||
1080 | static int __init cfg80211_init(void) | |
704232c2 | 1081 | { |
b2e1b302 LR |
1082 | int err; |
1083 | ||
463d0183 JB |
1084 | err = register_pernet_device(&cfg80211_pernet_ops); |
1085 | if (err) | |
1086 | goto out_fail_pernet; | |
1087 | ||
b2e1b302 | 1088 | err = wiphy_sysfs_init(); |
704232c2 JB |
1089 | 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 | ||
55682965 JB |
1096 | err = nl80211_init(); |
1097 | if (err) | |
1098 | goto out_fail_nl80211; | |
1099 | ||
704232c2 JB |
1100 | ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL); |
1101 | ||
b2e1b302 LR |
1102 | err = regulatory_init(); |
1103 | if (err) | |
1104 | goto out_fail_reg; | |
1105 | ||
e60d7443 | 1106 | cfg80211_wq = create_singlethread_workqueue("cfg80211"); |
f00f188f WY |
1107 | if (!cfg80211_wq) { |
1108 | err = -ENOMEM; | |
e60d7443 | 1109 | goto out_fail_wq; |
f00f188f | 1110 | } |
e60d7443 | 1111 | |
704232c2 JB |
1112 | return 0; |
1113 | ||
e60d7443 AB |
1114 | out_fail_wq: |
1115 | regulatory_exit(); | |
b2e1b302 LR |
1116 | out_fail_reg: |
1117 | debugfs_remove(ieee80211_debugfs_dir); | |
55682965 JB |
1118 | out_fail_nl80211: |
1119 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); | |
704232c2 JB |
1120 | out_fail_notifier: |
1121 | wiphy_sysfs_exit(); | |
1122 | out_fail_sysfs: | |
463d0183 JB |
1123 | unregister_pernet_device(&cfg80211_pernet_ops); |
1124 | out_fail_pernet: | |
704232c2 JB |
1125 | return err; |
1126 | } | |
3a462465 | 1127 | subsys_initcall(cfg80211_init); |
704232c2 | 1128 | |
f884e387 | 1129 | static void __exit cfg80211_exit(void) |
704232c2 JB |
1130 | { |
1131 | debugfs_remove(ieee80211_debugfs_dir); | |
55682965 | 1132 | nl80211_exit(); |
704232c2 JB |
1133 | unregister_netdevice_notifier(&cfg80211_netdev_notifier); |
1134 | wiphy_sysfs_exit(); | |
b2e1b302 | 1135 | regulatory_exit(); |
463d0183 | 1136 | unregister_pernet_device(&cfg80211_pernet_ops); |
e60d7443 | 1137 | destroy_workqueue(cfg80211_wq); |
704232c2 JB |
1138 | } |
1139 | module_exit(cfg80211_exit); |