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