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