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