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