]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/wireless/mlme.c
Merge branch 'rc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[mirror_ubuntu-bionic-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>
33d8783c 5 * Copyright (c) 2015 Intel Deutschland GmbH
6039f6d2
JM
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
c6fb08aa 10#include <linux/etherdevice.h>
6039f6d2
JM
11#include <linux/netdevice.h>
12#include <linux/nl80211.h>
5a0e3ad6 13#include <linux/slab.h>
a9a11622 14#include <linux/wireless.h>
6039f6d2 15#include <net/cfg80211.h>
a9a11622 16#include <net/iw_handler.h>
6039f6d2
JM
17#include "core.h"
18#include "nl80211.h"
e35e4d28
HG
19#include "rdev-ops.h"
20
6039f6d2 21
6ff57cf8 22void cfg80211_rx_assoc_resp(struct net_device *dev, struct cfg80211_bss *bss,
b0b6aa2c 23 const u8 *buf, size_t len, int uapsd_queues)
6039f6d2 24{
6829c878
JB
25 struct wireless_dev *wdev = dev->ieee80211_ptr;
26 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 27 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
6829c878
JB
28 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
29 u8 *ie = mgmt->u.assoc_resp.variable;
95de817b 30 int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
ceca7b71 31 u16 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
6829c878 32
4ee3e063 33 trace_cfg80211_send_rx_assoc(dev, bss);
cb0b4beb 34
f401a6f7
JB
35 /*
36 * This is a bit of a hack, we don't notify userspace of
37 * a (re-)association reply if we tried to send a reassoc
38 * and got a reject -- we only try again with an assoc
39 * frame instead of reassoc.
40 */
ceca7b71 41 if (cfg80211_sme_rx_assoc_resp(wdev, status_code)) {
f1940c57 42 cfg80211_unhold_bss(bss_from_pub(bss));
5b112d3d 43 cfg80211_put_bss(wiphy, bss);
8d61ffa5 44 return;
95de817b 45 }
f401a6f7 46
b0b6aa2c 47 nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL, uapsd_queues);
ceca7b71 48 /* update current_bss etc., consumes the bss reference */
df7fc0f9
JB
49 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
50 status_code,
95de817b 51 status_code == WLAN_STATUS_SUCCESS, bss);
6039f6d2 52}
6ff57cf8 53EXPORT_SYMBOL(cfg80211_rx_assoc_resp);
6039f6d2 54
ceca7b71
JB
55static void cfg80211_process_auth(struct wireless_dev *wdev,
56 const u8 *buf, size_t len)
57{
f26cbf40 58 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
59
60 nl80211_send_rx_auth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
61 cfg80211_sme_rx_auth(wdev, buf, len);
62}
63
64static void cfg80211_process_deauth(struct wireless_dev *wdev,
6ff57cf8 65 const u8 *buf, size_t len)
6039f6d2 66{
f26cbf40 67 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
6829c878 68 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
19957bb3 69 const u8 *bssid = mgmt->bssid;
ceca7b71
JB
70 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
71 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
6829c878 72
ceca7b71 73 nl80211_send_deauth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
19957bb3 74
ceca7b71
JB
75 if (!wdev->current_bss ||
76 !ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
77 return;
6829c878 78
ceca7b71
JB
79 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
80 cfg80211_sme_deauth(wdev);
667503dd 81}
6039f6d2 82
ceca7b71 83static void cfg80211_process_disassoc(struct wireless_dev *wdev,
6ff57cf8 84 const u8 *buf, size_t len)
6039f6d2 85{
f26cbf40 86 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
6829c878 87 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
19957bb3 88 const u8 *bssid = mgmt->bssid;
ceca7b71
JB
89 u16 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
90 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
6829c878 91
ceca7b71 92 nl80211_send_disassoc(rdev, wdev->netdev, buf, len, GFP_KERNEL);
a3b8b056 93
ceca7b71
JB
94 if (WARN_ON(!wdev->current_bss ||
95 !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
596a07c1 96 return;
6829c878 97
ceca7b71
JB
98 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
99 cfg80211_sme_disassoc(wdev);
667503dd 100}
1965c853 101
6ff57cf8
JB
102void cfg80211_rx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
103{
104 struct wireless_dev *wdev = dev->ieee80211_ptr;
6ff57cf8
JB
105 struct ieee80211_mgmt *mgmt = (void *)buf;
106
107 ASSERT_WDEV_LOCK(wdev);
108
109 trace_cfg80211_rx_mlme_mgmt(dev, buf, len);
110
111 if (WARN_ON(len < 2))
112 return;
113
ceca7b71
JB
114 if (ieee80211_is_auth(mgmt->frame_control))
115 cfg80211_process_auth(wdev, buf, len);
116 else if (ieee80211_is_deauth(mgmt->frame_control))
117 cfg80211_process_deauth(wdev, buf, len);
118 else if (ieee80211_is_disassoc(mgmt->frame_control))
119 cfg80211_process_disassoc(wdev, buf, len);
6ff57cf8
JB
120}
121EXPORT_SYMBOL(cfg80211_rx_mlme_mgmt);
122
123void cfg80211_auth_timeout(struct net_device *dev, const u8 *addr)
a58ce43f
JB
124{
125 struct wireless_dev *wdev = dev->ieee80211_ptr;
126 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 127 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
a58ce43f 128
4ee3e063 129 trace_cfg80211_send_auth_timeout(dev, addr);
a58ce43f
JB
130
131 nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
ceca7b71 132 cfg80211_sme_auth_timeout(wdev);
1965c853 133}
6ff57cf8 134EXPORT_SYMBOL(cfg80211_auth_timeout);
1965c853 135
959867fa 136void cfg80211_assoc_timeout(struct net_device *dev, struct cfg80211_bss *bss)
1965c853 137{
6829c878
JB
138 struct wireless_dev *wdev = dev->ieee80211_ptr;
139 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 140 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19957bb3 141
959867fa 142 trace_cfg80211_send_assoc_timeout(dev, bss->bssid);
cb0b4beb 143
959867fa 144 nl80211_send_assoc_timeout(rdev, dev, bss->bssid, GFP_KERNEL);
ceca7b71 145 cfg80211_sme_assoc_timeout(wdev);
959867fa 146
f1940c57 147 cfg80211_unhold_bss(bss_from_pub(bss));
959867fa 148 cfg80211_put_bss(wiphy, bss);
1965c853 149}
6ff57cf8
JB
150EXPORT_SYMBOL(cfg80211_assoc_timeout);
151
152void cfg80211_tx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
153{
154 struct wireless_dev *wdev = dev->ieee80211_ptr;
155 struct ieee80211_mgmt *mgmt = (void *)buf;
156
157 ASSERT_WDEV_LOCK(wdev);
158
159 trace_cfg80211_tx_mlme_mgmt(dev, buf, len);
160
161 if (WARN_ON(len < 2))
162 return;
163
164 if (ieee80211_is_deauth(mgmt->frame_control))
ceca7b71 165 cfg80211_process_deauth(wdev, buf, len);
6ff57cf8 166 else
ceca7b71 167 cfg80211_process_disassoc(wdev, buf, len);
6ff57cf8
JB
168}
169EXPORT_SYMBOL(cfg80211_tx_mlme_mgmt);
1965c853 170
a3b8b056
JM
171void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
172 enum nl80211_key_type key_type, int key_id,
e6d6e342 173 const u8 *tsc, gfp_t gfp)
a3b8b056
JM
174{
175 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
f26cbf40 176 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3d23e349 177#ifdef CONFIG_CFG80211_WEXT
f58d4ed9 178 union iwreq_data wrqu;
e6d6e342 179 char *buf = kmalloc(128, gfp);
f58d4ed9
JB
180
181 if (buf) {
182 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
183 "keyid=%d %scast addr=%pM)", key_id,
184 key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
185 addr);
186 memset(&wrqu, 0, sizeof(wrqu));
187 wrqu.data.length = strlen(buf);
188 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
189 kfree(buf);
190 }
191#endif
192
4ee3e063 193 trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
e6d6e342 194 nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
a3b8b056
JM
195}
196EXPORT_SYMBOL(cfg80211_michael_mic_failure);
19957bb3
JB
197
198/* some MLME handling for userspace SME */
91bf9b26
JB
199int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
200 struct net_device *dev,
201 struct ieee80211_channel *chan,
202 enum nl80211_auth_type auth_type,
203 const u8 *bssid,
204 const u8 *ssid, int ssid_len,
205 const u8 *ie, int ie_len,
206 const u8 *key, int key_len, int key_idx,
207 const u8 *sae_data, int sae_data_len)
19957bb3
JB
208{
209 struct wireless_dev *wdev = dev->ieee80211_ptr;
7ade7036
JB
210 struct cfg80211_auth_request req = {
211 .ie = ie,
212 .ie_len = ie_len,
213 .sae_data = sae_data,
214 .sae_data_len = sae_data_len,
215 .auth_type = auth_type,
216 .key = key,
217 .key_len = key_len,
218 .key_idx = key_idx,
219 };
95de817b 220 int err;
19957bb3 221
667503dd
JB
222 ASSERT_WDEV_LOCK(wdev);
223
fffd0934 224 if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
b6b5555b 225 if (!key || !key_len || key_idx < 0 || key_idx > 3)
fffd0934
JB
226 return -EINVAL;
227
0a9b5e17 228 if (wdev->current_bss &&
ac422d3c 229 ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
0a9b5e17
JB
230 return -EALREADY;
231
19957bb3 232 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
6eb18137
DL
233 IEEE80211_BSS_TYPE_ESS,
234 IEEE80211_PRIVACY_ANY);
19957bb3
JB
235 if (!req.bss)
236 return -ENOENT;
237
e35e4d28 238 err = rdev_auth(rdev, dev, &req);
19957bb3 239
5b112d3d 240 cfg80211_put_bss(&rdev->wiphy, req.bss);
19957bb3
JB
241 return err;
242}
243
7e7c8926
BG
244/* Do a logical ht_capa &= ht_capa_mask. */
245void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
246 const struct ieee80211_ht_cap *ht_capa_mask)
247{
248 int i;
249 u8 *p1, *p2;
250 if (!ht_capa_mask) {
251 memset(ht_capa, 0, sizeof(*ht_capa));
252 return;
253 }
254
255 p1 = (u8*)(ht_capa);
256 p2 = (u8*)(ht_capa_mask);
257 for (i = 0; i<sizeof(*ht_capa); i++)
258 p1[i] &= p2[i];
259}
260
ee2aca34
JB
261/* Do a logical ht_capa &= ht_capa_mask. */
262void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
263 const struct ieee80211_vht_cap *vht_capa_mask)
264{
265 int i;
266 u8 *p1, *p2;
267 if (!vht_capa_mask) {
268 memset(vht_capa, 0, sizeof(*vht_capa));
269 return;
270 }
271
272 p1 = (u8*)(vht_capa);
273 p2 = (u8*)(vht_capa_mask);
274 for (i = 0; i < sizeof(*vht_capa); i++)
275 p1[i] &= p2[i];
276}
277
91bf9b26
JB
278int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
279 struct net_device *dev,
280 struct ieee80211_channel *chan,
281 const u8 *bssid,
282 const u8 *ssid, int ssid_len,
283 struct cfg80211_assoc_request *req)
19957bb3
JB
284{
285 struct wireless_dev *wdev = dev->ieee80211_ptr;
95de817b 286 int err;
19957bb3 287
667503dd
JB
288 ASSERT_WDEV_LOCK(wdev);
289
ceca7b71
JB
290 if (wdev->current_bss &&
291 (!req->prev_bssid || !ether_addr_equal(wdev->current_bss->pub.bssid,
292 req->prev_bssid)))
19957bb3
JB
293 return -EALREADY;
294
f62fab73 295 cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
7e7c8926 296 rdev->wiphy.ht_capa_mod_mask);
f62fab73 297 cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
ee2aca34 298 rdev->wiphy.vht_capa_mod_mask);
7e7c8926 299
f62fab73 300 req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
6eb18137
DL
301 IEEE80211_BSS_TYPE_ESS,
302 IEEE80211_PRIVACY_ANY);
ceca7b71 303 if (!req->bss)
19957bb3
JB
304 return -ENOENT;
305
f62fab73 306 err = rdev_assoc(rdev, dev, req);
f1940c57
JB
307 if (!err)
308 cfg80211_hold_bss(bss_from_pub(req->bss));
73de86a3 309 else
f62fab73 310 cfg80211_put_bss(&rdev->wiphy, req->bss);
19957bb3 311
19957bb3
JB
312 return err;
313}
314
91bf9b26
JB
315int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
316 struct net_device *dev, const u8 *bssid,
317 const u8 *ie, int ie_len, u16 reason,
318 bool local_state_change)
19957bb3
JB
319{
320 struct wireless_dev *wdev = dev->ieee80211_ptr;
95de817b
JB
321 struct cfg80211_deauth_request req = {
322 .bssid = bssid,
323 .reason_code = reason,
324 .ie = ie,
325 .ie_len = ie_len,
6863255b 326 .local_state_change = local_state_change,
95de817b 327 };
19957bb3 328
667503dd
JB
329 ASSERT_WDEV_LOCK(wdev);
330
ceca7b71
JB
331 if (local_state_change &&
332 (!wdev->current_bss ||
333 !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
95de817b 334 return 0;
19957bb3 335
e35e4d28 336 return rdev_deauth(rdev, dev, &req);
19957bb3
JB
337}
338
91bf9b26
JB
339int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
340 struct net_device *dev, const u8 *bssid,
341 const u8 *ie, int ie_len, u16 reason,
342 bool local_state_change)
19957bb3
JB
343{
344 struct wireless_dev *wdev = dev->ieee80211_ptr;
7ade7036
JB
345 struct cfg80211_disassoc_request req = {
346 .reason_code = reason,
347 .local_state_change = local_state_change,
348 .ie = ie,
349 .ie_len = ie_len,
350 };
ceca7b71 351 int err;
19957bb3 352
667503dd
JB
353 ASSERT_WDEV_LOCK(wdev);
354
ceca7b71 355 if (!wdev->current_bss)
f9d6b402
JB
356 return -ENOTCONN;
357
ac422d3c 358 if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
19957bb3
JB
359 req.bss = &wdev->current_bss->pub;
360 else
361 return -ENOTCONN;
362
ceca7b71
JB
363 err = rdev_disassoc(rdev, dev, &req);
364 if (err)
365 return err;
366
367 /* driver should have reported the disassoc */
368 WARN_ON(wdev->current_bss);
369 return 0;
667503dd
JB
370}
371
19957bb3
JB
372void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
373 struct net_device *dev)
374{
375 struct wireless_dev *wdev = dev->ieee80211_ptr;
95de817b 376 u8 bssid[ETH_ALEN];
19957bb3 377
667503dd
JB
378 ASSERT_WDEV_LOCK(wdev);
379
19957bb3
JB
380 if (!rdev->ops->deauth)
381 return;
382
95de817b
JB
383 if (!wdev->current_bss)
384 return;
19957bb3 385
95de817b 386 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
ceca7b71
JB
387 cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
388 WLAN_REASON_DEAUTH_LEAVING, false);
19957bb3 389}
9588bbd5 390
2e161f78 391struct cfg80211_mgmt_registration {
026331c4 392 struct list_head list;
33d8783c 393 struct wireless_dev *wdev;
026331c4 394
15e47304 395 u32 nlportid;
026331c4
JM
396
397 int match_len;
398
2e161f78
JB
399 __le16 frame_type;
400
026331c4
JM
401 u8 match[];
402};
403
33d8783c
JB
404static void
405cfg80211_process_mlme_unregistrations(struct cfg80211_registered_device *rdev)
406{
407 struct cfg80211_mgmt_registration *reg;
408
409 ASSERT_RTNL();
410
411 spin_lock_bh(&rdev->mlme_unreg_lock);
412 while ((reg = list_first_entry_or_null(&rdev->mlme_unreg,
413 struct cfg80211_mgmt_registration,
414 list))) {
415 list_del(&reg->list);
416 spin_unlock_bh(&rdev->mlme_unreg_lock);
417
418 if (rdev->ops->mgmt_frame_register) {
419 u16 frame_type = le16_to_cpu(reg->frame_type);
420
421 rdev_mgmt_frame_register(rdev, reg->wdev,
422 frame_type, false);
423 }
424
425 kfree(reg);
426
427 spin_lock_bh(&rdev->mlme_unreg_lock);
428 }
429 spin_unlock_bh(&rdev->mlme_unreg_lock);
430}
431
432void cfg80211_mlme_unreg_wk(struct work_struct *wk)
433{
434 struct cfg80211_registered_device *rdev;
435
436 rdev = container_of(wk, struct cfg80211_registered_device,
437 mlme_unreg_wk);
438
439 rtnl_lock();
440 cfg80211_process_mlme_unregistrations(rdev);
441 rtnl_unlock();
442}
443
15e47304 444int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
2e161f78
JB
445 u16 frame_type, const u8 *match_data,
446 int match_len)
026331c4 447{
271733cf 448 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2e161f78 450 struct cfg80211_mgmt_registration *reg, *nreg;
026331c4 451 int err = 0;
2e161f78
JB
452 u16 mgmt_type;
453
454 if (!wdev->wiphy->mgmt_stypes)
455 return -EOPNOTSUPP;
456
457 if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
458 return -EINVAL;
459
460 if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
461 return -EINVAL;
462
463 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
464 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
465 return -EINVAL;
026331c4
JM
466
467 nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
468 if (!nreg)
469 return -ENOMEM;
470
2e161f78 471 spin_lock_bh(&wdev->mgmt_registrations_lock);
026331c4 472
2e161f78 473 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
026331c4
JM
474 int mlen = min(match_len, reg->match_len);
475
2e161f78
JB
476 if (frame_type != le16_to_cpu(reg->frame_type))
477 continue;
478
026331c4
JM
479 if (memcmp(reg->match, match_data, mlen) == 0) {
480 err = -EALREADY;
481 break;
482 }
483 }
484
485 if (err) {
486 kfree(nreg);
487 goto out;
488 }
489
490 memcpy(nreg->match, match_data, match_len);
491 nreg->match_len = match_len;
15e47304 492 nreg->nlportid = snd_portid;
2e161f78 493 nreg->frame_type = cpu_to_le16(frame_type);
33d8783c 494 nreg->wdev = wdev;
2e161f78 495 list_add(&nreg->list, &wdev->mgmt_registrations);
33d8783c
JB
496 spin_unlock_bh(&wdev->mgmt_registrations_lock);
497
498 /* process all unregistrations to avoid driver confusion */
499 cfg80211_process_mlme_unregistrations(rdev);
026331c4 500
271733cf 501 if (rdev->ops->mgmt_frame_register)
e35e4d28 502 rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
271733cf 503
33d8783c
JB
504 return 0;
505
026331c4 506 out:
2e161f78 507 spin_unlock_bh(&wdev->mgmt_registrations_lock);
271733cf 508
026331c4
JM
509 return err;
510}
511
15e47304 512void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
026331c4 513{
271733cf 514 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 515 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2e161f78 516 struct cfg80211_mgmt_registration *reg, *tmp;
026331c4 517
2e161f78 518 spin_lock_bh(&wdev->mgmt_registrations_lock);
026331c4 519
2e161f78 520 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
15e47304 521 if (reg->nlportid != nlportid)
271733cf
JB
522 continue;
523
271733cf 524 list_del(&reg->list);
33d8783c
JB
525 spin_lock(&rdev->mlme_unreg_lock);
526 list_add_tail(&reg->list, &rdev->mlme_unreg);
527 spin_unlock(&rdev->mlme_unreg_lock);
528
529 schedule_work(&rdev->mlme_unreg_wk);
026331c4
JM
530 }
531
2e161f78 532 spin_unlock_bh(&wdev->mgmt_registrations_lock);
28946da7 533
5de17984
AS
534 if (nlportid && rdev->crit_proto_nlportid == nlportid) {
535 rdev->crit_proto_nlportid = 0;
536 rdev_crit_proto_stop(rdev, wdev);
537 }
538
15e47304
EB
539 if (nlportid == wdev->ap_unexpected_nlportid)
540 wdev->ap_unexpected_nlportid = 0;
026331c4
JM
541}
542
2e161f78 543void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
026331c4 544{
33d8783c 545 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
026331c4 546
2e161f78 547 spin_lock_bh(&wdev->mgmt_registrations_lock);
33d8783c
JB
548 spin_lock(&rdev->mlme_unreg_lock);
549 list_splice_tail_init(&wdev->mgmt_registrations, &rdev->mlme_unreg);
550 spin_unlock(&rdev->mlme_unreg_lock);
2e161f78 551 spin_unlock_bh(&wdev->mgmt_registrations_lock);
33d8783c
JB
552
553 cfg80211_process_mlme_unregistrations(rdev);
026331c4
JM
554}
555
2e161f78 556int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
71bbc994 557 struct wireless_dev *wdev,
b176e629 558 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
026331c4 559{
026331c4 560 const struct ieee80211_mgmt *mgmt;
2e161f78
JB
561 u16 stype;
562
563 if (!wdev->wiphy->mgmt_stypes)
564 return -EOPNOTSUPP;
026331c4 565
2e161f78 566 if (!rdev->ops->mgmt_tx)
026331c4 567 return -EOPNOTSUPP;
2e161f78 568
b176e629 569 if (params->len < 24 + 1)
026331c4
JM
570 return -EINVAL;
571
b176e629 572 mgmt = (const struct ieee80211_mgmt *)params->buf;
2e161f78
JB
573
574 if (!ieee80211_is_mgmt(mgmt->frame_control))
026331c4 575 return -EINVAL;
2e161f78
JB
576
577 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
578 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
579 return -EINVAL;
580
581 if (ieee80211_is_action(mgmt->frame_control) &&
582 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
663fcafd
JB
583 int err = 0;
584
fe100acd
JB
585 wdev_lock(wdev);
586
663fcafd
JB
587 switch (wdev->iftype) {
588 case NL80211_IFTYPE_ADHOC:
589 case NL80211_IFTYPE_STATION:
590 case NL80211_IFTYPE_P2P_CLIENT:
591 if (!wdev->current_bss) {
592 err = -ENOTCONN;
593 break;
594 }
595
ac422d3c
JP
596 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
597 mgmt->bssid)) {
663fcafd
JB
598 err = -ENOTCONN;
599 break;
600 }
601
602 /*
603 * check for IBSS DA must be done by driver as
604 * cfg80211 doesn't track the stations
605 */
606 if (wdev->iftype == NL80211_IFTYPE_ADHOC)
607 break;
fe100acd 608
663fcafd 609 /* for station, check that DA is the AP */
ac422d3c
JP
610 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
611 mgmt->da)) {
663fcafd
JB
612 err = -ENOTCONN;
613 break;
614 }
615 break;
616 case NL80211_IFTYPE_AP:
617 case NL80211_IFTYPE_P2P_GO:
618 case NL80211_IFTYPE_AP_VLAN:
98104fde 619 if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
663fcafd
JB
620 err = -EINVAL;
621 break;
0778a6a3 622 case NL80211_IFTYPE_MESH_POINT:
ac422d3c 623 if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
0778a6a3
JC
624 err = -EINVAL;
625 break;
626 }
627 /*
628 * check for mesh DA must be done by driver as
629 * cfg80211 doesn't track the stations
630 */
631 break;
98104fde
JB
632 case NL80211_IFTYPE_P2P_DEVICE:
633 /*
634 * fall through, P2P device only supports
635 * public action frames
636 */
cb3b7d87 637 case NL80211_IFTYPE_NAN:
663fcafd
JB
638 default:
639 err = -EOPNOTSUPP;
640 break;
641 }
fe100acd 642 wdev_unlock(wdev);
663fcafd
JB
643
644 if (err)
645 return err;
026331c4
JM
646 }
647
98104fde 648 if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
026331c4
JM
649 return -EINVAL;
650
651 /* Transmit the Action frame as requested by user space */
b176e629 652 return rdev_mgmt_tx(rdev, wdev, params, cookie);
026331c4
JM
653}
654
71bbc994 655bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
970fdfa8 656 const u8 *buf, size_t len, u32 flags)
026331c4 657{
026331c4 658 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 659 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2e161f78
JB
660 struct cfg80211_mgmt_registration *reg;
661 const struct ieee80211_txrx_stypes *stypes =
662 &wiphy->mgmt_stypes[wdev->iftype];
663 struct ieee80211_mgmt *mgmt = (void *)buf;
664 const u8 *data;
665 int data_len;
026331c4 666 bool result = false;
2e161f78
JB
667 __le16 ftype = mgmt->frame_control &
668 cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
669 u16 stype;
026331c4 670
4ee3e063 671 trace_cfg80211_rx_mgmt(wdev, freq, sig_mbm);
2e161f78 672 stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
026331c4 673
4ee3e063
BL
674 if (!(stypes->rx & BIT(stype))) {
675 trace_cfg80211_return_bool(false);
2e161f78 676 return false;
4ee3e063 677 }
026331c4 678
2e161f78
JB
679 data = buf + ieee80211_hdrlen(mgmt->frame_control);
680 data_len = len - ieee80211_hdrlen(mgmt->frame_control);
681
682 spin_lock_bh(&wdev->mgmt_registrations_lock);
683
684 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
685 if (reg->frame_type != ftype)
686 continue;
026331c4 687
2e161f78 688 if (reg->match_len > data_len)
026331c4
JM
689 continue;
690
2e161f78 691 if (memcmp(reg->match, data, reg->match_len))
026331c4
JM
692 continue;
693
694 /* found match! */
695
696 /* Indicate the received Action frame to user space */
15e47304 697 if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
804483e9 698 freq, sig_mbm,
970fdfa8 699 buf, len, flags, GFP_ATOMIC))
026331c4
JM
700 continue;
701
702 result = true;
703 break;
704 }
705
2e161f78 706 spin_unlock_bh(&wdev->mgmt_registrations_lock);
026331c4 707
4ee3e063 708 trace_cfg80211_return_bool(result);
026331c4
JM
709 return result;
710}
2e161f78 711EXPORT_SYMBOL(cfg80211_rx_mgmt);
026331c4 712
04f39047
SW
713void cfg80211_dfs_channels_update_work(struct work_struct *work)
714{
a85a7e28 715 struct delayed_work *delayed_work = to_delayed_work(work);
04f39047
SW
716 struct cfg80211_registered_device *rdev;
717 struct cfg80211_chan_def chandef;
718 struct ieee80211_supported_band *sband;
719 struct ieee80211_channel *c;
720 struct wiphy *wiphy;
721 bool check_again = false;
722 unsigned long timeout, next_time = 0;
723 int bandid, i;
724
04f39047
SW
725 rdev = container_of(delayed_work, struct cfg80211_registered_device,
726 dfs_update_channels_wk);
727 wiphy = &rdev->wiphy;
728
5fe231e8 729 rtnl_lock();
57fbcce3 730 for (bandid = 0; bandid < NUM_NL80211_BANDS; bandid++) {
04f39047
SW
731 sband = wiphy->bands[bandid];
732 if (!sband)
733 continue;
734
735 for (i = 0; i < sband->n_channels; i++) {
736 c = &sband->channels[i];
737
738 if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
739 continue;
740
c532a58b
MK
741 timeout = c->dfs_state_entered + msecs_to_jiffies(
742 IEEE80211_DFS_MIN_NOP_TIME_MS);
04f39047
SW
743
744 if (time_after_eq(jiffies, timeout)) {
745 c->dfs_state = NL80211_DFS_USABLE;
bbe09bbc
MK
746 c->dfs_state_entered = jiffies;
747
04f39047
SW
748 cfg80211_chandef_create(&chandef, c,
749 NL80211_CHAN_NO_HT);
750
751 nl80211_radar_notify(rdev, &chandef,
752 NL80211_RADAR_NOP_FINISHED,
753 NULL, GFP_ATOMIC);
754 continue;
755 }
756
757 if (!check_again)
758 next_time = timeout - jiffies;
759 else
760 next_time = min(next_time, timeout - jiffies);
761 check_again = true;
762 }
763 }
5fe231e8 764 rtnl_unlock();
04f39047
SW
765
766 /* reschedule if there are other channels waiting to be cleared again */
767 if (check_again)
768 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
769 next_time);
770}
771
772
773void cfg80211_radar_event(struct wiphy *wiphy,
774 struct cfg80211_chan_def *chandef,
775 gfp_t gfp)
776{
f26cbf40 777 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
04f39047
SW
778 unsigned long timeout;
779
780 trace_cfg80211_radar_event(wiphy, chandef);
781
782 /* only set the chandef supplied channel to unavailable, in
783 * case the radar is detected on only one of multiple channels
784 * spanned by the chandef.
785 */
786 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
787
788 timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_NOP_TIME_MS);
789 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
790 timeout);
791
792 nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
793}
794EXPORT_SYMBOL(cfg80211_radar_event);
795
796void cfg80211_cac_event(struct net_device *netdev,
d2859df5 797 const struct cfg80211_chan_def *chandef,
04f39047
SW
798 enum nl80211_radar_event event, gfp_t gfp)
799{
800 struct wireless_dev *wdev = netdev->ieee80211_ptr;
801 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 802 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
04f39047
SW
803 unsigned long timeout;
804
805 trace_cfg80211_cac_event(netdev, event);
806
807 if (WARN_ON(!wdev->cac_started))
808 return;
809
9e0e2961 810 if (WARN_ON(!wdev->chandef.chan))
04f39047
SW
811 return;
812
04f39047
SW
813 switch (event) {
814 case NL80211_RADAR_CAC_FINISHED:
815 timeout = wdev->cac_start_time +
31559f35 816 msecs_to_jiffies(wdev->cac_time_ms);
04f39047 817 WARN_ON(!time_after_eq(jiffies, timeout));
d2859df5 818 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
04f39047
SW
819 break;
820 case NL80211_RADAR_CAC_ABORTED:
821 break;
822 default:
823 WARN_ON(1);
824 return;
825 }
826 wdev->cac_started = false;
827
d2859df5 828 nl80211_radar_notify(rdev, chandef, event, netdev, gfp);
04f39047
SW
829}
830EXPORT_SYMBOL(cfg80211_cac_event);