]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/wireless/mlme.c
Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[mirror_ubuntu-artful-kernel.git] / net / wireless / mlme.c
CommitLineData
6039f6d2
JM
1/*
2 * cfg80211 MLME SAP interface
3 *
4 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
c6fb08aa 9#include <linux/etherdevice.h>
6039f6d2
JM
10#include <linux/netdevice.h>
11#include <linux/nl80211.h>
5a0e3ad6 12#include <linux/slab.h>
a9a11622 13#include <linux/wireless.h>
6039f6d2 14#include <net/cfg80211.h>
a9a11622 15#include <net/iw_handler.h>
6039f6d2
JM
16#include "core.h"
17#include "nl80211.h"
18
cb0b4beb 19void cfg80211_send_rx_auth(struct net_device *dev, const u8 *buf, size_t len)
6039f6d2 20{
19957bb3
JB
21 struct wireless_dev *wdev = dev->ieee80211_ptr;
22 struct wiphy *wiphy = wdev->wiphy;
6039f6d2 23 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
19957bb3 24
667503dd 25 wdev_lock(wdev);
cb0b4beb 26
95de817b
JB
27 nl80211_send_rx_auth(rdev, dev, buf, len, GFP_KERNEL);
28 cfg80211_sme_rx_auth(dev, buf, len);
667503dd
JB
29
30 wdev_unlock(wdev);
6039f6d2
JM
31}
32EXPORT_SYMBOL(cfg80211_send_rx_auth);
33
95de817b
JB
34void cfg80211_send_rx_assoc(struct net_device *dev, struct cfg80211_bss *bss,
35 const u8 *buf, size_t len)
6039f6d2 36{
6829c878
JB
37 u16 status_code;
38 struct wireless_dev *wdev = dev->ieee80211_ptr;
39 struct wiphy *wiphy = wdev->wiphy;
6039f6d2 40 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6829c878
JB
41 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
42 u8 *ie = mgmt->u.assoc_resp.variable;
95de817b 43 int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
6829c878 44
667503dd 45 wdev_lock(wdev);
cb0b4beb 46
6829c878
JB
47 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
48
f401a6f7
JB
49 /*
50 * This is a bit of a hack, we don't notify userspace of
51 * a (re-)association reply if we tried to send a reassoc
52 * and got a reject -- we only try again with an assoc
53 * frame instead of reassoc.
54 */
55 if (status_code != WLAN_STATUS_SUCCESS && wdev->conn &&
95de817b
JB
56 cfg80211_sme_failed_reassoc(wdev)) {
57 cfg80211_put_bss(bss);
f401a6f7 58 goto out;
95de817b 59 }
f401a6f7 60
cb0b4beb 61 nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL);
6829c878 62
95de817b 63 if (status_code != WLAN_STATUS_SUCCESS && wdev->conn) {
7d930bc3 64 cfg80211_sme_failed_assoc(wdev);
7d930bc3
JB
65 /*
66 * do not call connect_result() now because the
67 * sme will schedule work that does it later.
68 */
95de817b 69 cfg80211_put_bss(bss);
7d930bc3 70 goto out;
df7fc0f9
JB
71 }
72
ea416a79
JB
73 if (!wdev->conn && wdev->sme_state == CFG80211_SME_IDLE) {
74 /*
75 * This is for the userspace SME, the CONNECTING
76 * state will be changed to CONNECTED by
77 * __cfg80211_connect_result() below.
78 */
79 wdev->sme_state = CFG80211_SME_CONNECTING;
80 }
81
95de817b 82 /* this consumes the bss reference */
df7fc0f9
JB
83 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
84 status_code,
95de817b 85 status_code == WLAN_STATUS_SUCCESS, bss);
f401a6f7 86 out:
667503dd 87 wdev_unlock(wdev);
6039f6d2
JM
88}
89EXPORT_SYMBOL(cfg80211_send_rx_assoc);
90
ce470613 91void __cfg80211_send_deauth(struct net_device *dev,
667503dd 92 const u8 *buf, size_t len)
6039f6d2 93{
6829c878
JB
94 struct wireless_dev *wdev = dev->ieee80211_ptr;
95 struct wiphy *wiphy = wdev->wiphy;
6039f6d2 96 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6829c878 97 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
19957bb3 98 const u8 *bssid = mgmt->bssid;
95de817b 99 bool was_current = false;
6829c878 100
667503dd 101 ASSERT_WDEV_LOCK(wdev);
cb0b4beb 102
19957bb3 103 if (wdev->current_bss &&
ac422d3c 104 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
19957bb3
JB
105 cfg80211_unhold_bss(wdev->current_bss);
106 cfg80211_put_bss(&wdev->current_bss->pub);
107 wdev->current_bss = NULL;
3f3b6a8d 108 was_current = true;
19957bb3 109 }
19957bb3 110
5fba4af3
JB
111 nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);
112
3f3b6a8d 113 if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
6829c878
JB
114 u16 reason_code;
115 bool from_ap;
116
117 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
118
ac422d3c 119 from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
667503dd 120 __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
6829c878 121 } else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
667503dd
JB
122 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
123 WLAN_STATUS_UNSPECIFIED_FAILURE,
df7fc0f9 124 false, NULL);
667503dd
JB
125 }
126}
ce470613 127EXPORT_SYMBOL(__cfg80211_send_deauth);
667503dd 128
ce470613 129void cfg80211_send_deauth(struct net_device *dev, const u8 *buf, size_t len)
667503dd
JB
130{
131 struct wireless_dev *wdev = dev->ieee80211_ptr;
132
ce470613
HS
133 wdev_lock(wdev);
134 __cfg80211_send_deauth(dev, buf, len);
135 wdev_unlock(wdev);
6039f6d2 136}
53b46b84 137EXPORT_SYMBOL(cfg80211_send_deauth);
6039f6d2 138
ce470613 139void __cfg80211_send_disassoc(struct net_device *dev,
667503dd 140 const u8 *buf, size_t len)
6039f6d2 141{
6829c878
JB
142 struct wireless_dev *wdev = dev->ieee80211_ptr;
143 struct wiphy *wiphy = wdev->wiphy;
6039f6d2 144 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6829c878 145 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
19957bb3 146 const u8 *bssid = mgmt->bssid;
19957bb3
JB
147 u16 reason_code;
148 bool from_ap;
6829c878 149
596a07c1 150 ASSERT_WDEV_LOCK(wdev);
cb0b4beb
JB
151
152 nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
a3b8b056 153
596a07c1
JB
154 if (wdev->sme_state != CFG80211_SME_CONNECTED)
155 return;
6829c878 156
19957bb3 157 if (wdev->current_bss &&
ac422d3c 158 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
95de817b
JB
159 cfg80211_sme_disassoc(dev, wdev->current_bss);
160 cfg80211_unhold_bss(wdev->current_bss);
161 cfg80211_put_bss(&wdev->current_bss->pub);
162 wdev->current_bss = NULL;
19957bb3
JB
163 } else
164 WARN_ON(1);
6829c878 165
6829c878 166
19957bb3
JB
167 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
168
ac422d3c 169 from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
667503dd 170 __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
667503dd 171}
ce470613 172EXPORT_SYMBOL(__cfg80211_send_disassoc);
667503dd 173
ce470613 174void cfg80211_send_disassoc(struct net_device *dev, const u8 *buf, size_t len)
667503dd
JB
175{
176 struct wireless_dev *wdev = dev->ieee80211_ptr;
177
ce470613
HS
178 wdev_lock(wdev);
179 __cfg80211_send_disassoc(dev, buf, len);
180 wdev_unlock(wdev);
1965c853 181}
6829c878 182EXPORT_SYMBOL(cfg80211_send_disassoc);
1965c853 183
cf4e594e
JM
184void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
185 size_t len)
186{
187 struct wireless_dev *wdev = dev->ieee80211_ptr;
188 struct wiphy *wiphy = wdev->wiphy;
189 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
190
191 nl80211_send_unprot_deauth(rdev, dev, buf, len, GFP_ATOMIC);
192}
193EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
194
195void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
196 size_t len)
197{
198 struct wireless_dev *wdev = dev->ieee80211_ptr;
199 struct wiphy *wiphy = wdev->wiphy;
200 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
201
202 nl80211_send_unprot_disassoc(rdev, dev, buf, len, GFP_ATOMIC);
203}
204EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
205
a58ce43f
JB
206void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
207{
208 struct wireless_dev *wdev = dev->ieee80211_ptr;
209 struct wiphy *wiphy = wdev->wiphy;
210 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
211
212 wdev_lock(wdev);
213
214 nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
215 if (wdev->sme_state == CFG80211_SME_CONNECTING)
216 __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
217 WLAN_STATUS_UNSPECIFIED_FAILURE,
218 false, NULL);
219
667503dd 220 wdev_unlock(wdev);
1965c853
JM
221}
222EXPORT_SYMBOL(cfg80211_send_auth_timeout);
223
cb0b4beb 224void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
1965c853 225{
6829c878
JB
226 struct wireless_dev *wdev = dev->ieee80211_ptr;
227 struct wiphy *wiphy = wdev->wiphy;
1965c853 228 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
19957bb3 229
667503dd 230 wdev_lock(wdev);
cb0b4beb
JB
231
232 nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
6829c878 233 if (wdev->sme_state == CFG80211_SME_CONNECTING)
667503dd
JB
234 __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
235 WLAN_STATUS_UNSPECIFIED_FAILURE,
df7fc0f9 236 false, NULL);
19957bb3 237
667503dd 238 wdev_unlock(wdev);
1965c853
JM
239}
240EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
241
a3b8b056
JM
242void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
243 enum nl80211_key_type key_type, int key_id,
e6d6e342 244 const u8 *tsc, gfp_t gfp)
a3b8b056
JM
245{
246 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
247 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3d23e349 248#ifdef CONFIG_CFG80211_WEXT
f58d4ed9 249 union iwreq_data wrqu;
e6d6e342 250 char *buf = kmalloc(128, gfp);
f58d4ed9
JB
251
252 if (buf) {
253 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
254 "keyid=%d %scast addr=%pM)", key_id,
255 key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
256 addr);
257 memset(&wrqu, 0, sizeof(wrqu));
258 wrqu.data.length = strlen(buf);
259 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
260 kfree(buf);
261 }
262#endif
263
e6d6e342 264 nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
a3b8b056
JM
265}
266EXPORT_SYMBOL(cfg80211_michael_mic_failure);
19957bb3
JB
267
268/* some MLME handling for userspace SME */
667503dd
JB
269int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
270 struct net_device *dev,
271 struct ieee80211_channel *chan,
272 enum nl80211_auth_type auth_type,
273 const u8 *bssid,
274 const u8 *ssid, int ssid_len,
fffd0934 275 const u8 *ie, int ie_len,
95de817b 276 const u8 *key, int key_len, int key_idx)
19957bb3
JB
277{
278 struct wireless_dev *wdev = dev->ieee80211_ptr;
279 struct cfg80211_auth_request req;
95de817b 280 int err;
19957bb3 281
667503dd
JB
282 ASSERT_WDEV_LOCK(wdev);
283
fffd0934
JB
284 if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
285 if (!key || !key_len || key_idx < 0 || key_idx > 4)
286 return -EINVAL;
287
0a9b5e17 288 if (wdev->current_bss &&
ac422d3c 289 ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
0a9b5e17
JB
290 return -EALREADY;
291
19957bb3
JB
292 memset(&req, 0, sizeof(req));
293
294 req.ie = ie;
295 req.ie_len = ie_len;
296 req.auth_type = auth_type;
297 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
298 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
fffd0934
JB
299 req.key = key;
300 req.key_len = key_len;
301 req.key_idx = key_idx;
19957bb3
JB
302 if (!req.bss)
303 return -ENOENT;
304
e4e32459
MK
305 err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
306 CHAN_MODE_SHARED);
307 if (err)
308 goto out;
309
19957bb3 310 err = rdev->ops->auth(&rdev->wiphy, dev, &req);
19957bb3 311
e4e32459 312out:
95de817b 313 cfg80211_put_bss(req.bss);
19957bb3
JB
314 return err;
315}
316
667503dd
JB
317int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
318 struct net_device *dev, struct ieee80211_channel *chan,
319 enum nl80211_auth_type auth_type, const u8 *bssid,
320 const u8 *ssid, int ssid_len,
fffd0934 321 const u8 *ie, int ie_len,
95de817b 322 const u8 *key, int key_len, int key_idx)
667503dd
JB
323{
324 int err;
325
e4e32459 326 mutex_lock(&rdev->devlist_mtx);
667503dd
JB
327 wdev_lock(dev->ieee80211_ptr);
328 err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
fffd0934 329 ssid, ssid_len, ie, ie_len,
95de817b 330 key, key_len, key_idx);
667503dd 331 wdev_unlock(dev->ieee80211_ptr);
e4e32459 332 mutex_unlock(&rdev->devlist_mtx);
667503dd
JB
333
334 return err;
335}
336
7e7c8926
BG
337/* Do a logical ht_capa &= ht_capa_mask. */
338void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
339 const struct ieee80211_ht_cap *ht_capa_mask)
340{
341 int i;
342 u8 *p1, *p2;
343 if (!ht_capa_mask) {
344 memset(ht_capa, 0, sizeof(*ht_capa));
345 return;
346 }
347
348 p1 = (u8*)(ht_capa);
349 p2 = (u8*)(ht_capa_mask);
350 for (i = 0; i<sizeof(*ht_capa); i++)
351 p1[i] &= p2[i];
352}
353
667503dd
JB
354int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
355 struct net_device *dev,
356 struct ieee80211_channel *chan,
357 const u8 *bssid, const u8 *prev_bssid,
358 const u8 *ssid, int ssid_len,
359 const u8 *ie, int ie_len, bool use_mfp,
7e7c8926
BG
360 struct cfg80211_crypto_settings *crypt,
361 u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
362 struct ieee80211_ht_cap *ht_capa_mask)
19957bb3
JB
363{
364 struct wireless_dev *wdev = dev->ieee80211_ptr;
365 struct cfg80211_assoc_request req;
95de817b 366 int err;
24b6b15f 367 bool was_connected = false;
19957bb3 368
667503dd
JB
369 ASSERT_WDEV_LOCK(wdev);
370
19957bb3
JB
371 memset(&req, 0, sizeof(req));
372
24b6b15f 373 if (wdev->current_bss && prev_bssid &&
ac422d3c 374 ether_addr_equal(wdev->current_bss->pub.bssid, prev_bssid)) {
24b6b15f
JM
375 /*
376 * Trying to reassociate: Allow this to proceed and let the old
377 * association to be dropped when the new one is completed.
378 */
379 if (wdev->sme_state == CFG80211_SME_CONNECTED) {
380 was_connected = true;
381 wdev->sme_state = CFG80211_SME_CONNECTING;
382 }
383 } else if (wdev->current_bss)
19957bb3
JB
384 return -EALREADY;
385
386 req.ie = ie;
387 req.ie_len = ie_len;
388 memcpy(&req.crypto, crypt, sizeof(req.crypto));
389 req.use_mfp = use_mfp;
3e5d7649 390 req.prev_bssid = prev_bssid;
7e7c8926
BG
391 req.flags = assoc_flags;
392 if (ht_capa)
393 memcpy(&req.ht_capa, ht_capa, sizeof(req.ht_capa));
394 if (ht_capa_mask)
395 memcpy(&req.ht_capa_mask, ht_capa_mask,
396 sizeof(req.ht_capa_mask));
397 cfg80211_oper_and_ht_capa(&req.ht_capa_mask,
398 rdev->wiphy.ht_capa_mod_mask);
399
19957bb3
JB
400 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
401 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
24b6b15f
JM
402 if (!req.bss) {
403 if (was_connected)
404 wdev->sme_state = CFG80211_SME_CONNECTED;
19957bb3 405 return -ENOENT;
24b6b15f 406 }
19957bb3 407
e4e32459
MK
408 err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
409 CHAN_MODE_SHARED);
410 if (err)
411 goto out;
412
95de817b 413 err = rdev->ops->assoc(&rdev->wiphy, dev, &req);
19957bb3 414
e4e32459 415out:
95de817b
JB
416 if (err) {
417 if (was_connected)
418 wdev->sme_state = CFG80211_SME_CONNECTED;
419 cfg80211_put_bss(req.bss);
19957bb3
JB
420 }
421
19957bb3
JB
422 return err;
423}
424
667503dd
JB
425int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
426 struct net_device *dev,
427 struct ieee80211_channel *chan,
428 const u8 *bssid, const u8 *prev_bssid,
429 const u8 *ssid, int ssid_len,
430 const u8 *ie, int ie_len, bool use_mfp,
7e7c8926
BG
431 struct cfg80211_crypto_settings *crypt,
432 u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
433 struct ieee80211_ht_cap *ht_capa_mask)
667503dd
JB
434{
435 struct wireless_dev *wdev = dev->ieee80211_ptr;
436 int err;
437
e4e32459 438 mutex_lock(&rdev->devlist_mtx);
667503dd
JB
439 wdev_lock(wdev);
440 err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
7e7c8926
BG
441 ssid, ssid_len, ie, ie_len, use_mfp, crypt,
442 assoc_flags, ht_capa, ht_capa_mask);
667503dd 443 wdev_unlock(wdev);
e4e32459 444 mutex_unlock(&rdev->devlist_mtx);
667503dd
JB
445
446 return err;
447}
448
449int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
450 struct net_device *dev, const u8 *bssid,
d5cdfacb
JM
451 const u8 *ie, int ie_len, u16 reason,
452 bool local_state_change)
19957bb3
JB
453{
454 struct wireless_dev *wdev = dev->ieee80211_ptr;
95de817b
JB
455 struct cfg80211_deauth_request req = {
456 .bssid = bssid,
457 .reason_code = reason,
458 .ie = ie,
459 .ie_len = ie_len,
460 };
19957bb3 461
667503dd
JB
462 ASSERT_WDEV_LOCK(wdev);
463
95de817b
JB
464 if (local_state_change) {
465 if (wdev->current_bss &&
ac422d3c 466 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
95de817b
JB
467 cfg80211_unhold_bss(wdev->current_bss);
468 cfg80211_put_bss(&wdev->current_bss->pub);
469 wdev->current_bss = NULL;
19957bb3 470 }
19957bb3 471
95de817b
JB
472 return 0;
473 }
19957bb3 474
63c9c5e7 475 return rdev->ops->deauth(&rdev->wiphy, dev, &req);
19957bb3
JB
476}
477
667503dd
JB
478int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
479 struct net_device *dev, const u8 *bssid,
d5cdfacb
JM
480 const u8 *ie, int ie_len, u16 reason,
481 bool local_state_change)
667503dd
JB
482{
483 struct wireless_dev *wdev = dev->ieee80211_ptr;
484 int err;
485
486 wdev_lock(wdev);
d5cdfacb
JM
487 err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
488 local_state_change);
667503dd
JB
489 wdev_unlock(wdev);
490
491 return err;
492}
493
494static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
495 struct net_device *dev, const u8 *bssid,
d5cdfacb
JM
496 const u8 *ie, int ie_len, u16 reason,
497 bool local_state_change)
19957bb3
JB
498{
499 struct wireless_dev *wdev = dev->ieee80211_ptr;
500 struct cfg80211_disassoc_request req;
501
667503dd
JB
502 ASSERT_WDEV_LOCK(wdev);
503
f9d6b402
JB
504 if (wdev->sme_state != CFG80211_SME_CONNECTED)
505 return -ENOTCONN;
506
507 if (WARN_ON(!wdev->current_bss))
508 return -ENOTCONN;
509
19957bb3
JB
510 memset(&req, 0, sizeof(req));
511 req.reason_code = reason;
d5cdfacb 512 req.local_state_change = local_state_change;
19957bb3
JB
513 req.ie = ie;
514 req.ie_len = ie_len;
ac422d3c 515 if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
19957bb3
JB
516 req.bss = &wdev->current_bss->pub;
517 else
518 return -ENOTCONN;
519
63c9c5e7 520 return rdev->ops->disassoc(&rdev->wiphy, dev, &req);
667503dd
JB
521}
522
523int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
524 struct net_device *dev, const u8 *bssid,
d5cdfacb
JM
525 const u8 *ie, int ie_len, u16 reason,
526 bool local_state_change)
667503dd
JB
527{
528 struct wireless_dev *wdev = dev->ieee80211_ptr;
529 int err;
530
531 wdev_lock(wdev);
d5cdfacb
JM
532 err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
533 local_state_change);
667503dd
JB
534 wdev_unlock(wdev);
535
536 return err;
19957bb3
JB
537}
538
539void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
540 struct net_device *dev)
541{
542 struct wireless_dev *wdev = dev->ieee80211_ptr;
543 struct cfg80211_deauth_request req;
95de817b 544 u8 bssid[ETH_ALEN];
19957bb3 545
667503dd
JB
546 ASSERT_WDEV_LOCK(wdev);
547
19957bb3
JB
548 if (!rdev->ops->deauth)
549 return;
550
551 memset(&req, 0, sizeof(req));
552 req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
553 req.ie = NULL;
554 req.ie_len = 0;
555
95de817b
JB
556 if (!wdev->current_bss)
557 return;
19957bb3 558
95de817b
JB
559 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
560 req.bssid = bssid;
63c9c5e7 561 rdev->ops->deauth(&rdev->wiphy, dev, &req);
95de817b
JB
562
563 if (wdev->current_bss) {
564 cfg80211_unhold_bss(wdev->current_bss);
565 cfg80211_put_bss(&wdev->current_bss->pub);
566 wdev->current_bss = NULL;
19957bb3
JB
567 }
568}
9588bbd5 569
71bbc994 570void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9588bbd5
JM
571 struct ieee80211_channel *chan,
572 enum nl80211_channel_type channel_type,
573 unsigned int duration, gfp_t gfp)
574{
71bbc994 575 struct wiphy *wiphy = wdev->wiphy;
9588bbd5
JM
576 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
577
71bbc994 578 nl80211_send_remain_on_channel(rdev, wdev, cookie, chan, channel_type,
9588bbd5
JM
579 duration, gfp);
580}
581EXPORT_SYMBOL(cfg80211_ready_on_channel);
582
71bbc994 583void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9588bbd5
JM
584 struct ieee80211_channel *chan,
585 enum nl80211_channel_type channel_type,
586 gfp_t gfp)
587{
71bbc994 588 struct wiphy *wiphy = wdev->wiphy;
9588bbd5
JM
589 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
590
71bbc994 591 nl80211_send_remain_on_channel_cancel(rdev, wdev, cookie, chan,
9588bbd5
JM
592 channel_type, gfp);
593}
594EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
98b62183
JB
595
596void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
597 struct station_info *sinfo, gfp_t gfp)
598{
599 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
600 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
601
602 nl80211_send_sta_event(rdev, dev, mac_addr, sinfo, gfp);
603}
604EXPORT_SYMBOL(cfg80211_new_sta);
026331c4 605
ec15e68b
JM
606void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
607{
608 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
609 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
610
611 nl80211_send_sta_del_event(rdev, dev, mac_addr, gfp);
612}
613EXPORT_SYMBOL(cfg80211_del_sta);
614
ed44a951
PP
615void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
616 enum nl80211_connect_failed_reason reason,
617 gfp_t gfp)
618{
619 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
620 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
621
622 nl80211_send_conn_failed_event(rdev, dev, mac_addr, reason, gfp);
623}
624EXPORT_SYMBOL(cfg80211_conn_failed);
625
2e161f78 626struct cfg80211_mgmt_registration {
026331c4
JM
627 struct list_head list;
628
15e47304 629 u32 nlportid;
026331c4
JM
630
631 int match_len;
632
2e161f78
JB
633 __le16 frame_type;
634
026331c4
JM
635 u8 match[];
636};
637
15e47304 638int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
2e161f78
JB
639 u16 frame_type, const u8 *match_data,
640 int match_len)
026331c4 641{
271733cf
JB
642 struct wiphy *wiphy = wdev->wiphy;
643 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
2e161f78 644 struct cfg80211_mgmt_registration *reg, *nreg;
026331c4 645 int err = 0;
2e161f78
JB
646 u16 mgmt_type;
647
648 if (!wdev->wiphy->mgmt_stypes)
649 return -EOPNOTSUPP;
650
651 if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
652 return -EINVAL;
653
654 if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
655 return -EINVAL;
656
657 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
658 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
659 return -EINVAL;
026331c4
JM
660
661 nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
662 if (!nreg)
663 return -ENOMEM;
664
2e161f78 665 spin_lock_bh(&wdev->mgmt_registrations_lock);
026331c4 666
2e161f78 667 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
026331c4
JM
668 int mlen = min(match_len, reg->match_len);
669
2e161f78
JB
670 if (frame_type != le16_to_cpu(reg->frame_type))
671 continue;
672
026331c4
JM
673 if (memcmp(reg->match, match_data, mlen) == 0) {
674 err = -EALREADY;
675 break;
676 }
677 }
678
679 if (err) {
680 kfree(nreg);
681 goto out;
682 }
683
684 memcpy(nreg->match, match_data, match_len);
685 nreg->match_len = match_len;
15e47304 686 nreg->nlportid = snd_portid;
2e161f78
JB
687 nreg->frame_type = cpu_to_le16(frame_type);
688 list_add(&nreg->list, &wdev->mgmt_registrations);
026331c4 689
271733cf 690 if (rdev->ops->mgmt_frame_register)
71bbc994 691 rdev->ops->mgmt_frame_register(wiphy, wdev, frame_type, true);
271733cf 692
026331c4 693 out:
2e161f78 694 spin_unlock_bh(&wdev->mgmt_registrations_lock);
271733cf 695
026331c4
JM
696 return err;
697}
698
15e47304 699void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
026331c4 700{
271733cf
JB
701 struct wiphy *wiphy = wdev->wiphy;
702 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
2e161f78 703 struct cfg80211_mgmt_registration *reg, *tmp;
026331c4 704
2e161f78 705 spin_lock_bh(&wdev->mgmt_registrations_lock);
026331c4 706
2e161f78 707 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
15e47304 708 if (reg->nlportid != nlportid)
271733cf
JB
709 continue;
710
711 if (rdev->ops->mgmt_frame_register) {
712 u16 frame_type = le16_to_cpu(reg->frame_type);
713
71bbc994 714 rdev->ops->mgmt_frame_register(wiphy, wdev,
271733cf 715 frame_type, false);
026331c4 716 }
271733cf
JB
717
718 list_del(&reg->list);
719 kfree(reg);
026331c4
JM
720 }
721
2e161f78 722 spin_unlock_bh(&wdev->mgmt_registrations_lock);
28946da7 723
15e47304
EB
724 if (nlportid == wdev->ap_unexpected_nlportid)
725 wdev->ap_unexpected_nlportid = 0;
026331c4
JM
726}
727
2e161f78 728void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
026331c4 729{
2e161f78 730 struct cfg80211_mgmt_registration *reg, *tmp;
026331c4 731
2e161f78 732 spin_lock_bh(&wdev->mgmt_registrations_lock);
026331c4 733
2e161f78 734 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
026331c4
JM
735 list_del(&reg->list);
736 kfree(reg);
737 }
738
2e161f78 739 spin_unlock_bh(&wdev->mgmt_registrations_lock);
026331c4
JM
740}
741
2e161f78 742int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
71bbc994 743 struct wireless_dev *wdev,
f7ca38df 744 struct ieee80211_channel *chan, bool offchan,
2e161f78 745 enum nl80211_channel_type channel_type,
f7ca38df 746 bool channel_type_valid, unsigned int wait,
e9f935e3 747 const u8 *buf, size_t len, bool no_cck,
e247bd90 748 bool dont_wait_for_ack, u64 *cookie)
026331c4 749{
026331c4 750 const struct ieee80211_mgmt *mgmt;
2e161f78
JB
751 u16 stype;
752
753 if (!wdev->wiphy->mgmt_stypes)
754 return -EOPNOTSUPP;
026331c4 755
2e161f78 756 if (!rdev->ops->mgmt_tx)
026331c4 757 return -EOPNOTSUPP;
2e161f78 758
026331c4
JM
759 if (len < 24 + 1)
760 return -EINVAL;
761
762 mgmt = (const struct ieee80211_mgmt *) buf;
2e161f78
JB
763
764 if (!ieee80211_is_mgmt(mgmt->frame_control))
026331c4 765 return -EINVAL;
2e161f78
JB
766
767 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
768 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
769 return -EINVAL;
770
771 if (ieee80211_is_action(mgmt->frame_control) &&
772 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
663fcafd
JB
773 int err = 0;
774
fe100acd
JB
775 wdev_lock(wdev);
776
663fcafd
JB
777 switch (wdev->iftype) {
778 case NL80211_IFTYPE_ADHOC:
779 case NL80211_IFTYPE_STATION:
780 case NL80211_IFTYPE_P2P_CLIENT:
781 if (!wdev->current_bss) {
782 err = -ENOTCONN;
783 break;
784 }
785
ac422d3c
JP
786 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
787 mgmt->bssid)) {
663fcafd
JB
788 err = -ENOTCONN;
789 break;
790 }
791
792 /*
793 * check for IBSS DA must be done by driver as
794 * cfg80211 doesn't track the stations
795 */
796 if (wdev->iftype == NL80211_IFTYPE_ADHOC)
797 break;
fe100acd 798
663fcafd 799 /* for station, check that DA is the AP */
ac422d3c
JP
800 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
801 mgmt->da)) {
663fcafd
JB
802 err = -ENOTCONN;
803 break;
804 }
805 break;
806 case NL80211_IFTYPE_AP:
807 case NL80211_IFTYPE_P2P_GO:
808 case NL80211_IFTYPE_AP_VLAN:
98104fde 809 if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
663fcafd
JB
810 err = -EINVAL;
811 break;
0778a6a3 812 case NL80211_IFTYPE_MESH_POINT:
ac422d3c 813 if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
0778a6a3
JC
814 err = -EINVAL;
815 break;
816 }
817 /*
818 * check for mesh DA must be done by driver as
819 * cfg80211 doesn't track the stations
820 */
821 break;
98104fde
JB
822 case NL80211_IFTYPE_P2P_DEVICE:
823 /*
824 * fall through, P2P device only supports
825 * public action frames
826 */
663fcafd
JB
827 default:
828 err = -EOPNOTSUPP;
829 break;
830 }
fe100acd 831 wdev_unlock(wdev);
663fcafd
JB
832
833 if (err)
834 return err;
026331c4
JM
835 }
836
98104fde 837 if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
026331c4
JM
838 return -EINVAL;
839
840 /* Transmit the Action frame as requested by user space */
71bbc994 841 return rdev->ops->mgmt_tx(&rdev->wiphy, wdev, chan, offchan,
f7ca38df 842 channel_type, channel_type_valid,
e247bd90
JB
843 wait, buf, len, no_cck, dont_wait_for_ack,
844 cookie);
026331c4
JM
845}
846
71bbc994 847bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
804483e9 848 const u8 *buf, size_t len, gfp_t gfp)
026331c4 849{
026331c4
JM
850 struct wiphy *wiphy = wdev->wiphy;
851 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
2e161f78
JB
852 struct cfg80211_mgmt_registration *reg;
853 const struct ieee80211_txrx_stypes *stypes =
854 &wiphy->mgmt_stypes[wdev->iftype];
855 struct ieee80211_mgmt *mgmt = (void *)buf;
856 const u8 *data;
857 int data_len;
026331c4 858 bool result = false;
2e161f78
JB
859 __le16 ftype = mgmt->frame_control &
860 cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
861 u16 stype;
026331c4 862
2e161f78 863 stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
026331c4 864
2e161f78
JB
865 if (!(stypes->rx & BIT(stype)))
866 return false;
026331c4 867
2e161f78
JB
868 data = buf + ieee80211_hdrlen(mgmt->frame_control);
869 data_len = len - ieee80211_hdrlen(mgmt->frame_control);
870
871 spin_lock_bh(&wdev->mgmt_registrations_lock);
872
873 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
874 if (reg->frame_type != ftype)
875 continue;
026331c4 876
2e161f78 877 if (reg->match_len > data_len)
026331c4
JM
878 continue;
879
2e161f78 880 if (memcmp(reg->match, data, reg->match_len))
026331c4
JM
881 continue;
882
883 /* found match! */
884
885 /* Indicate the received Action frame to user space */
15e47304 886 if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
804483e9 887 freq, sig_mbm,
2e161f78 888 buf, len, gfp))
026331c4
JM
889 continue;
890
891 result = true;
892 break;
893 }
894
2e161f78 895 spin_unlock_bh(&wdev->mgmt_registrations_lock);
026331c4
JM
896
897 return result;
898}
2e161f78 899EXPORT_SYMBOL(cfg80211_rx_mgmt);
026331c4 900
71bbc994 901void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
2e161f78 902 const u8 *buf, size_t len, bool ack, gfp_t gfp)
026331c4 903{
026331c4
JM
904 struct wiphy *wiphy = wdev->wiphy;
905 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
906
907 /* Indicate TX status of the Action frame to user space */
71bbc994 908 nl80211_send_mgmt_tx_status(rdev, wdev, cookie, buf, len, ack, gfp);
026331c4 909}
2e161f78 910EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
d6dc1a38
JO
911
912void cfg80211_cqm_rssi_notify(struct net_device *dev,
913 enum nl80211_cqm_rssi_threshold_event rssi_event,
914 gfp_t gfp)
915{
916 struct wireless_dev *wdev = dev->ieee80211_ptr;
917 struct wiphy *wiphy = wdev->wiphy;
918 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
919
920 /* Indicate roaming trigger event to user space */
921 nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
922}
923EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
c063dbf5
JB
924
925void cfg80211_cqm_pktloss_notify(struct net_device *dev,
926 const u8 *peer, u32 num_packets, gfp_t gfp)
927{
928 struct wireless_dev *wdev = dev->ieee80211_ptr;
929 struct wiphy *wiphy = wdev->wiphy;
930 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
931
932 /* Indicate roaming trigger event to user space */
933 nl80211_send_cqm_pktloss_notify(rdev, dev, peer, num_packets, gfp);
934}
935EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
e5497d76 936
84f10708
TP
937void cfg80211_cqm_txe_notify(struct net_device *dev,
938 const u8 *peer, u32 num_packets,
939 u32 rate, u32 intvl, gfp_t gfp)
940{
941 struct wireless_dev *wdev = dev->ieee80211_ptr;
942 struct wiphy *wiphy = wdev->wiphy;
943 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
944
945 nl80211_send_cqm_txe_notify(rdev, dev, peer, num_packets,
946 rate, intvl, gfp);
947}
948EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
949
e5497d76
JB
950void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
951 const u8 *replay_ctr, gfp_t gfp)
952{
953 struct wireless_dev *wdev = dev->ieee80211_ptr;
954 struct wiphy *wiphy = wdev->wiphy;
955 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
956
957 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
958}
959EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
c9df56b4
JM
960
961void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
962 const u8 *bssid, bool preauth, gfp_t gfp)
963{
964 struct wireless_dev *wdev = dev->ieee80211_ptr;
965 struct wiphy *wiphy = wdev->wiphy;
966 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
967
968 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
969}
970EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
28946da7 971
5314526b
TP
972void cfg80211_ch_switch_notify(struct net_device *dev, int freq,
973 enum nl80211_channel_type type)
974{
975 struct wireless_dev *wdev = dev->ieee80211_ptr;
976 struct wiphy *wiphy = wdev->wiphy;
977 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
978 struct ieee80211_channel *chan;
979
980 wdev_lock(wdev);
981
982 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
983 wdev->iftype != NL80211_IFTYPE_P2P_GO))
984 goto out;
985
986 chan = rdev_freq_to_chan(rdev, freq, type);
987 if (WARN_ON(!chan))
988 goto out;
989
f4489ebe 990 wdev->channel = chan;
5314526b
TP
991 nl80211_ch_switch_notify(rdev, dev, freq, type, GFP_KERNEL);
992out:
993 wdev_unlock(wdev);
994 return;
995}
996EXPORT_SYMBOL(cfg80211_ch_switch_notify);
997
28946da7
JB
998bool cfg80211_rx_spurious_frame(struct net_device *dev,
999 const u8 *addr, gfp_t gfp)
1000{
1001 struct wireless_dev *wdev = dev->ieee80211_ptr;
1002
1003 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
1004 wdev->iftype != NL80211_IFTYPE_P2P_GO))
1005 return false;
1006
1007 return nl80211_unexpected_frame(dev, addr, gfp);
1008}
1009EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
b92ab5d8
JB
1010
1011bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
1012 const u8 *addr, gfp_t gfp)
1013{
1014 struct wireless_dev *wdev = dev->ieee80211_ptr;
1015
1016 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
1017 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
1018 wdev->iftype != NL80211_IFTYPE_AP_VLAN))
1019 return false;
1020
1021 return nl80211_unexpected_4addr_frame(dev, addr, gfp);
1022}
1023EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);