1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * cfg80211 MLME SAP interface
4 *
5 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6 * Copyright (c) 2015 Intel Deutschland GmbH
7 * Copyright (C) 2019-2020 Intel Corporation
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/nl80211.h>
15 #include <linux/slab.h>
16 #include <linux/wireless.h>
17 #include <net/cfg80211.h>
18 #include <net/iw_handler.h>
19 #include "core.h"
20 #include "nl80211.h"
21 #include "rdev-ops.h"
22
23
cfg80211_rx_assoc_resp(struct net_device * dev,struct cfg80211_bss * bss,const u8 * buf,size_t len,int uapsd_queues,const u8 * req_ies,size_t req_ies_len)24 void cfg80211_rx_assoc_resp(struct net_device *dev, struct cfg80211_bss *bss,
25 const u8 *buf, size_t len, int uapsd_queues,
26 const u8 *req_ies, size_t req_ies_len)
27 {
28 struct wireless_dev *wdev = dev->ieee80211_ptr;
29 struct wiphy *wiphy = wdev->wiphy;
30 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
31 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
32 struct cfg80211_connect_resp_params cr;
33 const u8 *resp_ie = mgmt->u.assoc_resp.variable;
34 size_t resp_ie_len = len - offsetof(struct ieee80211_mgmt,
35 u.assoc_resp.variable);
36
37 if (bss->channel->band == NL80211_BAND_S1GHZ) {
38 resp_ie = (u8 *)&mgmt->u.s1g_assoc_resp.variable;
39 resp_ie_len = len - offsetof(struct ieee80211_mgmt,
40 u.s1g_assoc_resp.variable);
41 }
42
43 memset(&cr, 0, sizeof(cr));
44 cr.status = (int)le16_to_cpu(mgmt->u.assoc_resp.status_code);
45 cr.bssid = mgmt->bssid;
46 cr.bss = bss;
47 cr.req_ie = req_ies;
48 cr.req_ie_len = req_ies_len;
49 cr.resp_ie = resp_ie;
50 cr.resp_ie_len = resp_ie_len;
51 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
52
53 trace_cfg80211_send_rx_assoc(dev, bss);
54
55 /*
56 * This is a bit of a hack, we don't notify userspace of
57 * a (re-)association reply if we tried to send a reassoc
58 * and got a reject -- we only try again with an assoc
59 * frame instead of reassoc.
60 */
61 if (cfg80211_sme_rx_assoc_resp(wdev, cr.status)) {
62 cfg80211_unhold_bss(bss_from_pub(bss));
63 cfg80211_put_bss(wiphy, bss);
64 return;
65 }
66
67 nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL, uapsd_queues,
68 req_ies, req_ies_len);
69 /* update current_bss etc., consumes the bss reference */
70 __cfg80211_connect_result(dev, &cr, cr.status == WLAN_STATUS_SUCCESS);
71 }
72 EXPORT_SYMBOL(cfg80211_rx_assoc_resp);
73
cfg80211_process_auth(struct wireless_dev * wdev,const u8 * buf,size_t len)74 static void cfg80211_process_auth(struct wireless_dev *wdev,
75 const u8 *buf, size_t len)
76 {
77 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
78
79 nl80211_send_rx_auth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
80 cfg80211_sme_rx_auth(wdev, buf, len);
81 }
82
cfg80211_process_deauth(struct wireless_dev * wdev,const u8 * buf,size_t len,bool reconnect)83 static void cfg80211_process_deauth(struct wireless_dev *wdev,
84 const u8 *buf, size_t len,
85 bool reconnect)
86 {
87 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
88 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
89 const u8 *bssid = mgmt->bssid;
90 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
91 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
92
93 nl80211_send_deauth(rdev, wdev->netdev, buf, len, reconnect, GFP_KERNEL);
94
95 if (!wdev->connected || !ether_addr_equal(wdev->u.client.connected_addr, bssid))
96 return;
97
98 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
99 cfg80211_sme_deauth(wdev);
100 }
101
cfg80211_process_disassoc(struct wireless_dev * wdev,const u8 * buf,size_t len,bool reconnect)102 static void cfg80211_process_disassoc(struct wireless_dev *wdev,
103 const u8 *buf, size_t len,
104 bool reconnect)
105 {
106 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
107 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
108 const u8 *bssid = mgmt->bssid;
109 u16 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
110 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
111
112 nl80211_send_disassoc(rdev, wdev->netdev, buf, len, reconnect,
113 GFP_KERNEL);
114
115 if (WARN_ON(!wdev->connected ||
116 !ether_addr_equal(wdev->u.client.connected_addr, bssid)))
117 return;
118
119 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
120 cfg80211_sme_disassoc(wdev);
121 }
122
cfg80211_rx_mlme_mgmt(struct net_device * dev,const u8 * buf,size_t len)123 void cfg80211_rx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
124 {
125 struct wireless_dev *wdev = dev->ieee80211_ptr;
126 struct ieee80211_mgmt *mgmt = (void *)buf;
127
128 ASSERT_WDEV_LOCK(wdev);
129
130 trace_cfg80211_rx_mlme_mgmt(dev, buf, len);
131
132 if (WARN_ON(len < 2))
133 return;
134
135 if (ieee80211_is_auth(mgmt->frame_control))
136 cfg80211_process_auth(wdev, buf, len);
137 else if (ieee80211_is_deauth(mgmt->frame_control))
138 cfg80211_process_deauth(wdev, buf, len, false);
139 else if (ieee80211_is_disassoc(mgmt->frame_control))
140 cfg80211_process_disassoc(wdev, buf, len, false);
141 }
142 EXPORT_SYMBOL(cfg80211_rx_mlme_mgmt);
143
cfg80211_auth_timeout(struct net_device * dev,const u8 * addr)144 void cfg80211_auth_timeout(struct net_device *dev, const u8 *addr)
145 {
146 struct wireless_dev *wdev = dev->ieee80211_ptr;
147 struct wiphy *wiphy = wdev->wiphy;
148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
149
150 trace_cfg80211_send_auth_timeout(dev, addr);
151
152 nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
153 cfg80211_sme_auth_timeout(wdev);
154 }
155 EXPORT_SYMBOL(cfg80211_auth_timeout);
156
cfg80211_assoc_timeout(struct net_device * dev,struct cfg80211_bss * bss)157 void cfg80211_assoc_timeout(struct net_device *dev, struct cfg80211_bss *bss)
158 {
159 struct wireless_dev *wdev = dev->ieee80211_ptr;
160 struct wiphy *wiphy = wdev->wiphy;
161 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
162
163 trace_cfg80211_send_assoc_timeout(dev, bss->bssid);
164
165 nl80211_send_assoc_timeout(rdev, dev, bss->bssid, GFP_KERNEL);
166 cfg80211_sme_assoc_timeout(wdev);
167
168 cfg80211_unhold_bss(bss_from_pub(bss));
169 cfg80211_put_bss(wiphy, bss);
170 }
171 EXPORT_SYMBOL(cfg80211_assoc_timeout);
172
cfg80211_abandon_assoc(struct net_device * dev,struct cfg80211_bss * bss)173 void cfg80211_abandon_assoc(struct net_device *dev, struct cfg80211_bss *bss)
174 {
175 struct wireless_dev *wdev = dev->ieee80211_ptr;
176 struct wiphy *wiphy = wdev->wiphy;
177
178 cfg80211_sme_abandon_assoc(wdev);
179
180 cfg80211_unhold_bss(bss_from_pub(bss));
181 cfg80211_put_bss(wiphy, bss);
182 }
183 EXPORT_SYMBOL(cfg80211_abandon_assoc);
184
cfg80211_tx_mlme_mgmt(struct net_device * dev,const u8 * buf,size_t len,bool reconnect)185 void cfg80211_tx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len,
186 bool reconnect)
187 {
188 struct wireless_dev *wdev = dev->ieee80211_ptr;
189 struct ieee80211_mgmt *mgmt = (void *)buf;
190
191 ASSERT_WDEV_LOCK(wdev);
192
193 trace_cfg80211_tx_mlme_mgmt(dev, buf, len, reconnect);
194
195 if (WARN_ON(len < 2))
196 return;
197
198 if (ieee80211_is_deauth(mgmt->frame_control))
199 cfg80211_process_deauth(wdev, buf, len, reconnect);
200 else
201 cfg80211_process_disassoc(wdev, buf, len, reconnect);
202 }
203 EXPORT_SYMBOL(cfg80211_tx_mlme_mgmt);
204
cfg80211_michael_mic_failure(struct net_device * dev,const u8 * addr,enum nl80211_key_type key_type,int key_id,const u8 * tsc,gfp_t gfp)205 void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
206 enum nl80211_key_type key_type, int key_id,
207 const u8 *tsc, gfp_t gfp)
208 {
209 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
210 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
211 #ifdef CONFIG_CFG80211_WEXT
212 union iwreq_data wrqu;
213 char *buf = kmalloc(128, gfp);
214
215 if (buf) {
216 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
217 "keyid=%d %scast addr=%pM)", key_id,
218 key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
219 addr);
220 memset(&wrqu, 0, sizeof(wrqu));
221 wrqu.data.length = strlen(buf);
222 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
223 kfree(buf);
224 }
225 #endif
226
227 trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
228 nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
229 }
230 EXPORT_SYMBOL(cfg80211_michael_mic_failure);
231
232 /* some MLME handling for userspace SME */
cfg80211_mlme_auth(struct cfg80211_registered_device * rdev,struct net_device * dev,struct ieee80211_channel * chan,enum nl80211_auth_type auth_type,const u8 * bssid,const u8 * ssid,int ssid_len,const u8 * ie,int ie_len,const u8 * key,int key_len,int key_idx,const u8 * auth_data,int auth_data_len)233 int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
234 struct net_device *dev,
235 struct ieee80211_channel *chan,
236 enum nl80211_auth_type auth_type,
237 const u8 *bssid,
238 const u8 *ssid, int ssid_len,
239 const u8 *ie, int ie_len,
240 const u8 *key, int key_len, int key_idx,
241 const u8 *auth_data, int auth_data_len)
242 {
243 struct wireless_dev *wdev = dev->ieee80211_ptr;
244 struct cfg80211_auth_request req = {
245 .ie = ie,
246 .ie_len = ie_len,
247 .auth_data = auth_data,
248 .auth_data_len = auth_data_len,
249 .auth_type = auth_type,
250 .key = key,
251 .key_len = key_len,
252 .key_idx = key_idx,
253 };
254 int err;
255
256 ASSERT_WDEV_LOCK(wdev);
257
258 if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
259 if (!key || !key_len || key_idx < 0 || key_idx > 3)
260 return -EINVAL;
261
262 if (wdev->connected &&
263 ether_addr_equal(bssid, wdev->u.client.connected_addr))
264 return -EALREADY;
265
266 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
267 IEEE80211_BSS_TYPE_ESS,
268 IEEE80211_PRIVACY_ANY);
269 if (!req.bss)
270 return -ENOENT;
271
272 err = rdev_auth(rdev, dev, &req);
273
274 cfg80211_put_bss(&rdev->wiphy, req.bss);
275 return err;
276 }
277
278 /* Do a logical ht_capa &= ht_capa_mask. */
cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap * ht_capa,const struct ieee80211_ht_cap * ht_capa_mask)279 void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
280 const struct ieee80211_ht_cap *ht_capa_mask)
281 {
282 int i;
283 u8 *p1, *p2;
284 if (!ht_capa_mask) {
285 memset(ht_capa, 0, sizeof(*ht_capa));
286 return;
287 }
288
289 p1 = (u8*)(ht_capa);
290 p2 = (u8*)(ht_capa_mask);
291 for (i = 0; i < sizeof(*ht_capa); i++)
292 p1[i] &= p2[i];
293 }
294
295 /* Do a logical vht_capa &= vht_capa_mask. */
cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap * vht_capa,const struct ieee80211_vht_cap * vht_capa_mask)296 void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
297 const struct ieee80211_vht_cap *vht_capa_mask)
298 {
299 int i;
300 u8 *p1, *p2;
301 if (!vht_capa_mask) {
302 memset(vht_capa, 0, sizeof(*vht_capa));
303 return;
304 }
305
306 p1 = (u8*)(vht_capa);
307 p2 = (u8*)(vht_capa_mask);
308 for (i = 0; i < sizeof(*vht_capa); i++)
309 p1[i] &= p2[i];
310 }
311
cfg80211_mlme_assoc(struct cfg80211_registered_device * rdev,struct net_device * dev,struct ieee80211_channel * chan,const u8 * bssid,const u8 * ssid,int ssid_len,struct cfg80211_assoc_request * req)312 int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
313 struct net_device *dev,
314 struct ieee80211_channel *chan,
315 const u8 *bssid,
316 const u8 *ssid, int ssid_len,
317 struct cfg80211_assoc_request *req)
318 {
319 struct wireless_dev *wdev = dev->ieee80211_ptr;
320 int err;
321
322 ASSERT_WDEV_LOCK(wdev);
323
324 if (wdev->connected &&
325 (!req->prev_bssid ||
326 !ether_addr_equal(wdev->u.client.connected_addr, req->prev_bssid)))
327 return -EALREADY;
328
329 cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
330 rdev->wiphy.ht_capa_mod_mask);
331 cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
332 rdev->wiphy.vht_capa_mod_mask);
333
334 req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
335 IEEE80211_BSS_TYPE_ESS,
336 IEEE80211_PRIVACY_ANY);
337 if (!req->bss)
338 return -ENOENT;
339
340 err = rdev_assoc(rdev, dev, req);
341 if (!err)
342 cfg80211_hold_bss(bss_from_pub(req->bss));
343 else
344 cfg80211_put_bss(&rdev->wiphy, req->bss);
345
346 return err;
347 }
348
cfg80211_mlme_deauth(struct cfg80211_registered_device * rdev,struct net_device * dev,const u8 * bssid,const u8 * ie,int ie_len,u16 reason,bool local_state_change)349 int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
350 struct net_device *dev, const u8 *bssid,
351 const u8 *ie, int ie_len, u16 reason,
352 bool local_state_change)
353 {
354 struct wireless_dev *wdev = dev->ieee80211_ptr;
355 struct cfg80211_deauth_request req = {
356 .bssid = bssid,
357 .reason_code = reason,
358 .ie = ie,
359 .ie_len = ie_len,
360 .local_state_change = local_state_change,
361 };
362
363 ASSERT_WDEV_LOCK(wdev);
364
365 if (local_state_change &&
366 (!wdev->connected ||
367 !ether_addr_equal(wdev->u.client.connected_addr, bssid)))
368 return 0;
369
370 if (ether_addr_equal(wdev->disconnect_bssid, bssid) ||
371 (wdev->connected &&
372 ether_addr_equal(wdev->u.client.connected_addr, bssid)))
373 wdev->conn_owner_nlportid = 0;
374
375 return rdev_deauth(rdev, dev, &req);
376 }
377
cfg80211_mlme_disassoc(struct cfg80211_registered_device * rdev,struct net_device * dev,const u8 * bssid,const u8 * ie,int ie_len,u16 reason,bool local_state_change)378 int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
379 struct net_device *dev, const u8 *bssid,
380 const u8 *ie, int ie_len, u16 reason,
381 bool local_state_change)
382 {
383 struct wireless_dev *wdev = dev->ieee80211_ptr;
384 struct cfg80211_disassoc_request req = {
385 .reason_code = reason,
386 .local_state_change = local_state_change,
387 .ie = ie,
388 .ie_len = ie_len,
389 };
390 int err;
391
392 ASSERT_WDEV_LOCK(wdev);
393
394 if (!wdev->connected)
395 return -ENOTCONN;
396
397 if (ether_addr_equal(wdev->links[0].client.current_bss->pub.bssid,
398 bssid))
399 req.bss = &wdev->links[0].client.current_bss->pub;
400 else
401 return -ENOTCONN;
402
403 err = rdev_disassoc(rdev, dev, &req);
404 if (err)
405 return err;
406
407 /* driver should have reported the disassoc */
408 WARN_ON(wdev->connected);
409 return 0;
410 }
411
cfg80211_mlme_down(struct cfg80211_registered_device * rdev,struct net_device * dev)412 void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
413 struct net_device *dev)
414 {
415 struct wireless_dev *wdev = dev->ieee80211_ptr;
416 u8 bssid[ETH_ALEN];
417
418 ASSERT_WDEV_LOCK(wdev);
419
420 if (!rdev->ops->deauth)
421 return;
422
423 if (!wdev->connected)
424 return;
425
426 memcpy(bssid, wdev->u.client.connected_addr, ETH_ALEN);
427 cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
428 WLAN_REASON_DEAUTH_LEAVING, false);
429 }
430
431 struct cfg80211_mgmt_registration {
432 struct list_head list;
433 struct wireless_dev *wdev;
434
435 u32 nlportid;
436
437 int match_len;
438
439 __le16 frame_type;
440
441 bool multicast_rx;
442
443 u8 match[];
444 };
445
cfg80211_mgmt_registrations_update(struct wireless_dev * wdev)446 static void cfg80211_mgmt_registrations_update(struct wireless_dev *wdev)
447 {
448 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
449 struct wireless_dev *tmp;
450 struct cfg80211_mgmt_registration *reg;
451 struct mgmt_frame_regs upd = {};
452
453 lockdep_assert_held(&rdev->wiphy.mtx);
454
455 spin_lock_bh(&rdev->mgmt_registrations_lock);
456 if (!wdev->mgmt_registrations_need_update) {
457 spin_unlock_bh(&rdev->mgmt_registrations_lock);
458 return;
459 }
460
461 rcu_read_lock();
462 list_for_each_entry_rcu(tmp, &rdev->wiphy.wdev_list, list) {
463 list_for_each_entry(reg, &tmp->mgmt_registrations, list) {
464 u32 mask = BIT(le16_to_cpu(reg->frame_type) >> 4);
465 u32 mcast_mask = 0;
466
467 if (reg->multicast_rx)
468 mcast_mask = mask;
469
470 upd.global_stypes |= mask;
471 upd.global_mcast_stypes |= mcast_mask;
472
473 if (tmp == wdev) {
474 upd.interface_stypes |= mask;
475 upd.interface_mcast_stypes |= mcast_mask;
476 }
477 }
478 }
479 rcu_read_unlock();
480
481 wdev->mgmt_registrations_need_update = 0;
482 spin_unlock_bh(&rdev->mgmt_registrations_lock);
483
484 rdev_update_mgmt_frame_registrations(rdev, wdev, &upd);
485 }
486
cfg80211_mgmt_registrations_update_wk(struct work_struct * wk)487 void cfg80211_mgmt_registrations_update_wk(struct work_struct *wk)
488 {
489 struct cfg80211_registered_device *rdev;
490 struct wireless_dev *wdev;
491
492 rdev = container_of(wk, struct cfg80211_registered_device,
493 mgmt_registrations_update_wk);
494
495 wiphy_lock(&rdev->wiphy);
496 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
497 cfg80211_mgmt_registrations_update(wdev);
498 wiphy_unlock(&rdev->wiphy);
499 }
500
cfg80211_mlme_register_mgmt(struct wireless_dev * wdev,u32 snd_portid,u16 frame_type,const u8 * match_data,int match_len,bool multicast_rx,struct netlink_ext_ack * extack)501 int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
502 u16 frame_type, const u8 *match_data,
503 int match_len, bool multicast_rx,
504 struct netlink_ext_ack *extack)
505 {
506 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
507 struct cfg80211_mgmt_registration *reg, *nreg;
508 int err = 0;
509 u16 mgmt_type;
510 bool update_multicast = false;
511
512 if (!wdev->wiphy->mgmt_stypes)
513 return -EOPNOTSUPP;
514
515 if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT) {
516 NL_SET_ERR_MSG(extack, "frame type not management");
517 return -EINVAL;
518 }
519
520 if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) {
521 NL_SET_ERR_MSG(extack, "Invalid frame type");
522 return -EINVAL;
523 }
524
525 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
526 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type))) {
527 NL_SET_ERR_MSG(extack,
528 "Registration to specific type not supported");
529 return -EINVAL;
530 }
531
532 /*
533 * To support Pre Association Security Negotiation (PASN), registration
534 * for authentication frames should be supported. However, as some
535 * versions of the user space daemons wrongly register to all types of
536 * authentication frames (which might result in unexpected behavior)
537 * allow such registration if the request is for a specific
538 * authentication algorithm number.
539 */
540 if (wdev->iftype == NL80211_IFTYPE_STATION &&
541 (frame_type & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_AUTH &&
542 !(match_data && match_len >= 2)) {
543 NL_SET_ERR_MSG(extack,
544 "Authentication algorithm number required");
545 return -EINVAL;
546 }
547
548 nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
549 if (!nreg)
550 return -ENOMEM;
551
552 spin_lock_bh(&rdev->mgmt_registrations_lock);
553
554 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
555 int mlen = min(match_len, reg->match_len);
556
557 if (frame_type != le16_to_cpu(reg->frame_type))
558 continue;
559
560 if (memcmp(reg->match, match_data, mlen) == 0) {
561 if (reg->multicast_rx != multicast_rx) {
562 update_multicast = true;
563 reg->multicast_rx = multicast_rx;
564 break;
565 }
566 NL_SET_ERR_MSG(extack, "Match already configured");
567 err = -EALREADY;
568 break;
569 }
570 }
571
572 if (err)
573 goto out;
574
575 if (update_multicast) {
576 kfree(nreg);
577 } else {
578 memcpy(nreg->match, match_data, match_len);
579 nreg->match_len = match_len;
580 nreg->nlportid = snd_portid;
581 nreg->frame_type = cpu_to_le16(frame_type);
582 nreg->wdev = wdev;
583 nreg->multicast_rx = multicast_rx;
584 list_add(&nreg->list, &wdev->mgmt_registrations);
585 }
586 wdev->mgmt_registrations_need_update = 1;
587 spin_unlock_bh(&rdev->mgmt_registrations_lock);
588
589 cfg80211_mgmt_registrations_update(wdev);
590
591 return 0;
592
593 out:
594 kfree(nreg);
595 spin_unlock_bh(&rdev->mgmt_registrations_lock);
596
597 return err;
598 }
599
cfg80211_mlme_unregister_socket(struct wireless_dev * wdev,u32 nlportid)600 void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
601 {
602 struct wiphy *wiphy = wdev->wiphy;
603 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
604 struct cfg80211_mgmt_registration *reg, *tmp;
605
606 spin_lock_bh(&rdev->mgmt_registrations_lock);
607
608 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
609 if (reg->nlportid != nlportid)
610 continue;
611
612 list_del(®->list);
613 kfree(reg);
614
615 wdev->mgmt_registrations_need_update = 1;
616 schedule_work(&rdev->mgmt_registrations_update_wk);
617 }
618
619 spin_unlock_bh(&rdev->mgmt_registrations_lock);
620
621 if (nlportid && rdev->crit_proto_nlportid == nlportid) {
622 rdev->crit_proto_nlportid = 0;
623 rdev_crit_proto_stop(rdev, wdev);
624 }
625
626 if (nlportid == wdev->ap_unexpected_nlportid)
627 wdev->ap_unexpected_nlportid = 0;
628 }
629
cfg80211_mlme_purge_registrations(struct wireless_dev * wdev)630 void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
631 {
632 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
633 struct cfg80211_mgmt_registration *reg, *tmp;
634
635 spin_lock_bh(&rdev->mgmt_registrations_lock);
636 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
637 list_del(®->list);
638 kfree(reg);
639 }
640 wdev->mgmt_registrations_need_update = 1;
641 spin_unlock_bh(&rdev->mgmt_registrations_lock);
642
643 cfg80211_mgmt_registrations_update(wdev);
644 }
645
cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)646 int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
647 struct wireless_dev *wdev,
648 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
649 {
650 const struct ieee80211_mgmt *mgmt;
651 u16 stype;
652
653 if (!wdev->wiphy->mgmt_stypes)
654 return -EOPNOTSUPP;
655
656 if (!rdev->ops->mgmt_tx)
657 return -EOPNOTSUPP;
658
659 if (params->len < 24 + 1)
660 return -EINVAL;
661
662 mgmt = (const struct ieee80211_mgmt *)params->buf;
663
664 if (!ieee80211_is_mgmt(mgmt->frame_control))
665 return -EINVAL;
666
667 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
668 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
669 return -EINVAL;
670
671 if (ieee80211_is_action(mgmt->frame_control) &&
672 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
673 int err = 0;
674
675 wdev_lock(wdev);
676
677 switch (wdev->iftype) {
678 case NL80211_IFTYPE_ADHOC:
679 /*
680 * check for IBSS DA must be done by driver as
681 * cfg80211 doesn't track the stations
682 */
683 if (!wdev->u.ibss.current_bss ||
684 !ether_addr_equal(wdev->u.ibss.current_bss->pub.bssid,
685 mgmt->bssid)) {
686 err = -ENOTCONN;
687 break;
688 }
689 break;
690 case NL80211_IFTYPE_STATION:
691 case NL80211_IFTYPE_P2P_CLIENT:
692 if (!wdev->connected) {
693 err = -ENOTCONN;
694 break;
695 }
696
697 /* FIXME: MLD may address this differently */
698
699 if (!ether_addr_equal(wdev->u.client.connected_addr,
700 mgmt->bssid)) {
701 err = -ENOTCONN;
702 break;
703 }
704
705 /* for station, check that DA is the AP */
706 if (!ether_addr_equal(wdev->u.client.connected_addr,
707 mgmt->da)) {
708 err = -ENOTCONN;
709 break;
710 }
711 break;
712 case NL80211_IFTYPE_AP:
713 case NL80211_IFTYPE_P2P_GO:
714 case NL80211_IFTYPE_AP_VLAN:
715 if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
716 err = -EINVAL;
717 break;
718 case NL80211_IFTYPE_MESH_POINT:
719 if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
720 err = -EINVAL;
721 break;
722 }
723 /*
724 * check for mesh DA must be done by driver as
725 * cfg80211 doesn't track the stations
726 */
727 break;
728 case NL80211_IFTYPE_P2P_DEVICE:
729 /*
730 * fall through, P2P device only supports
731 * public action frames
732 */
733 case NL80211_IFTYPE_NAN:
734 default:
735 err = -EOPNOTSUPP;
736 break;
737 }
738 wdev_unlock(wdev);
739
740 if (err)
741 return err;
742 }
743
744 if (!ether_addr_equal(mgmt->sa, wdev_address(wdev))) {
745 /* Allow random TA to be used with Public Action frames if the
746 * driver has indicated support for this. Otherwise, only allow
747 * the local address to be used.
748 */
749 if (!ieee80211_is_action(mgmt->frame_control) ||
750 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
751 return -EINVAL;
752 if (!wdev->connected &&
753 !wiphy_ext_feature_isset(
754 &rdev->wiphy,
755 NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA))
756 return -EINVAL;
757 if (wdev->connected &&
758 !wiphy_ext_feature_isset(
759 &rdev->wiphy,
760 NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA_CONNECTED))
761 return -EINVAL;
762 }
763
764 /* Transmit the Action frame as requested by user space */
765 return rdev_mgmt_tx(rdev, wdev, params, cookie);
766 }
767
cfg80211_rx_mgmt_khz(struct wireless_dev * wdev,int freq,int sig_dbm,const u8 * buf,size_t len,u32 flags)768 bool cfg80211_rx_mgmt_khz(struct wireless_dev *wdev, int freq, int sig_dbm,
769 const u8 *buf, size_t len, u32 flags)
770 {
771 struct wiphy *wiphy = wdev->wiphy;
772 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
773 struct cfg80211_mgmt_registration *reg;
774 const struct ieee80211_txrx_stypes *stypes =
775 &wiphy->mgmt_stypes[wdev->iftype];
776 struct ieee80211_mgmt *mgmt = (void *)buf;
777 const u8 *data;
778 int data_len;
779 bool result = false;
780 __le16 ftype = mgmt->frame_control &
781 cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
782 u16 stype;
783
784 trace_cfg80211_rx_mgmt(wdev, freq, sig_dbm);
785 stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
786
787 if (!(stypes->rx & BIT(stype))) {
788 trace_cfg80211_return_bool(false);
789 return false;
790 }
791
792 data = buf + ieee80211_hdrlen(mgmt->frame_control);
793 data_len = len - ieee80211_hdrlen(mgmt->frame_control);
794
795 spin_lock_bh(&rdev->mgmt_registrations_lock);
796
797 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
798 if (reg->frame_type != ftype)
799 continue;
800
801 if (reg->match_len > data_len)
802 continue;
803
804 if (memcmp(reg->match, data, reg->match_len))
805 continue;
806
807 /* found match! */
808
809 /* Indicate the received Action frame to user space */
810 if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
811 freq, sig_dbm,
812 buf, len, flags, GFP_ATOMIC))
813 continue;
814
815 result = true;
816 break;
817 }
818
819 spin_unlock_bh(&rdev->mgmt_registrations_lock);
820
821 trace_cfg80211_return_bool(result);
822 return result;
823 }
824 EXPORT_SYMBOL(cfg80211_rx_mgmt_khz);
825
cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device * rdev)826 void cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device *rdev)
827 {
828 cancel_delayed_work(&rdev->dfs_update_channels_wk);
829 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk, 0);
830 }
831
cfg80211_dfs_channels_update_work(struct work_struct * work)832 void cfg80211_dfs_channels_update_work(struct work_struct *work)
833 {
834 struct delayed_work *delayed_work = to_delayed_work(work);
835 struct cfg80211_registered_device *rdev;
836 struct cfg80211_chan_def chandef;
837 struct ieee80211_supported_band *sband;
838 struct ieee80211_channel *c;
839 struct wiphy *wiphy;
840 bool check_again = false;
841 unsigned long timeout, next_time = 0;
842 unsigned long time_dfs_update;
843 enum nl80211_radar_event radar_event;
844 int bandid, i;
845
846 rdev = container_of(delayed_work, struct cfg80211_registered_device,
847 dfs_update_channels_wk);
848 wiphy = &rdev->wiphy;
849
850 rtnl_lock();
851 for (bandid = 0; bandid < NUM_NL80211_BANDS; bandid++) {
852 sband = wiphy->bands[bandid];
853 if (!sband)
854 continue;
855
856 for (i = 0; i < sband->n_channels; i++) {
857 c = &sband->channels[i];
858
859 if (!(c->flags & IEEE80211_CHAN_RADAR))
860 continue;
861
862 if (c->dfs_state != NL80211_DFS_UNAVAILABLE &&
863 c->dfs_state != NL80211_DFS_AVAILABLE)
864 continue;
865
866 if (c->dfs_state == NL80211_DFS_UNAVAILABLE) {
867 time_dfs_update = IEEE80211_DFS_MIN_NOP_TIME_MS;
868 radar_event = NL80211_RADAR_NOP_FINISHED;
869 } else {
870 if (regulatory_pre_cac_allowed(wiphy) ||
871 cfg80211_any_wiphy_oper_chan(wiphy, c))
872 continue;
873
874 time_dfs_update = REG_PRE_CAC_EXPIRY_GRACE_MS;
875 radar_event = NL80211_RADAR_PRE_CAC_EXPIRED;
876 }
877
878 timeout = c->dfs_state_entered +
879 msecs_to_jiffies(time_dfs_update);
880
881 if (time_after_eq(jiffies, timeout)) {
882 c->dfs_state = NL80211_DFS_USABLE;
883 c->dfs_state_entered = jiffies;
884
885 cfg80211_chandef_create(&chandef, c,
886 NL80211_CHAN_NO_HT);
887
888 nl80211_radar_notify(rdev, &chandef,
889 radar_event, NULL,
890 GFP_ATOMIC);
891
892 regulatory_propagate_dfs_state(wiphy, &chandef,
893 c->dfs_state,
894 radar_event);
895 continue;
896 }
897
898 if (!check_again)
899 next_time = timeout - jiffies;
900 else
901 next_time = min(next_time, timeout - jiffies);
902 check_again = true;
903 }
904 }
905 rtnl_unlock();
906
907 /* reschedule if there are other channels waiting to be cleared again */
908 if (check_again)
909 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
910 next_time);
911 }
912
913
__cfg80211_radar_event(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,bool offchan,gfp_t gfp)914 void __cfg80211_radar_event(struct wiphy *wiphy,
915 struct cfg80211_chan_def *chandef,
916 bool offchan, gfp_t gfp)
917 {
918 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
919
920 trace_cfg80211_radar_event(wiphy, chandef, offchan);
921
922 /* only set the chandef supplied channel to unavailable, in
923 * case the radar is detected on only one of multiple channels
924 * spanned by the chandef.
925 */
926 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
927
928 if (offchan)
929 queue_work(cfg80211_wq, &rdev->background_cac_abort_wk);
930
931 cfg80211_sched_dfs_chan_update(rdev);
932
933 nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
934
935 memcpy(&rdev->radar_chandef, chandef, sizeof(struct cfg80211_chan_def));
936 queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
937 }
938 EXPORT_SYMBOL(__cfg80211_radar_event);
939
cfg80211_cac_event(struct net_device * netdev,const struct cfg80211_chan_def * chandef,enum nl80211_radar_event event,gfp_t gfp)940 void cfg80211_cac_event(struct net_device *netdev,
941 const struct cfg80211_chan_def *chandef,
942 enum nl80211_radar_event event, gfp_t gfp)
943 {
944 struct wireless_dev *wdev = netdev->ieee80211_ptr;
945 struct wiphy *wiphy = wdev->wiphy;
946 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
947 unsigned long timeout;
948
949 /* not yet supported */
950 if (wdev->valid_links)
951 return;
952
953 trace_cfg80211_cac_event(netdev, event);
954
955 if (WARN_ON(!wdev->cac_started && event != NL80211_RADAR_CAC_STARTED))
956 return;
957
958 switch (event) {
959 case NL80211_RADAR_CAC_FINISHED:
960 timeout = wdev->cac_start_time +
961 msecs_to_jiffies(wdev->cac_time_ms);
962 WARN_ON(!time_after_eq(jiffies, timeout));
963 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
964 memcpy(&rdev->cac_done_chandef, chandef,
965 sizeof(struct cfg80211_chan_def));
966 queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk);
967 cfg80211_sched_dfs_chan_update(rdev);
968 fallthrough;
969 case NL80211_RADAR_CAC_ABORTED:
970 wdev->cac_started = false;
971 break;
972 case NL80211_RADAR_CAC_STARTED:
973 wdev->cac_started = true;
974 break;
975 default:
976 WARN_ON(1);
977 return;
978 }
979
980 nl80211_radar_notify(rdev, chandef, event, netdev, gfp);
981 }
982 EXPORT_SYMBOL(cfg80211_cac_event);
983
984 static void
__cfg80211_background_cac_event(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,const struct cfg80211_chan_def * chandef,enum nl80211_radar_event event)985 __cfg80211_background_cac_event(struct cfg80211_registered_device *rdev,
986 struct wireless_dev *wdev,
987 const struct cfg80211_chan_def *chandef,
988 enum nl80211_radar_event event)
989 {
990 struct wiphy *wiphy = &rdev->wiphy;
991 struct net_device *netdev;
992
993 lockdep_assert_wiphy(&rdev->wiphy);
994
995 if (!cfg80211_chandef_valid(chandef))
996 return;
997
998 if (!rdev->background_radar_wdev)
999 return;
1000
1001 switch (event) {
1002 case NL80211_RADAR_CAC_FINISHED:
1003 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
1004 memcpy(&rdev->cac_done_chandef, chandef, sizeof(*chandef));
1005 queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk);
1006 cfg80211_sched_dfs_chan_update(rdev);
1007 wdev = rdev->background_radar_wdev;
1008 break;
1009 case NL80211_RADAR_CAC_ABORTED:
1010 if (!cancel_delayed_work(&rdev->background_cac_done_wk))
1011 return;
1012 wdev = rdev->background_radar_wdev;
1013 break;
1014 case NL80211_RADAR_CAC_STARTED:
1015 break;
1016 default:
1017 return;
1018 }
1019
1020 netdev = wdev ? wdev->netdev : NULL;
1021 nl80211_radar_notify(rdev, chandef, event, netdev, GFP_KERNEL);
1022 }
1023
1024 static void
cfg80211_background_cac_event(struct cfg80211_registered_device * rdev,const struct cfg80211_chan_def * chandef,enum nl80211_radar_event event)1025 cfg80211_background_cac_event(struct cfg80211_registered_device *rdev,
1026 const struct cfg80211_chan_def *chandef,
1027 enum nl80211_radar_event event)
1028 {
1029 wiphy_lock(&rdev->wiphy);
1030 __cfg80211_background_cac_event(rdev, rdev->background_radar_wdev,
1031 chandef, event);
1032 wiphy_unlock(&rdev->wiphy);
1033 }
1034
cfg80211_background_cac_done_wk(struct work_struct * work)1035 void cfg80211_background_cac_done_wk(struct work_struct *work)
1036 {
1037 struct delayed_work *delayed_work = to_delayed_work(work);
1038 struct cfg80211_registered_device *rdev;
1039
1040 rdev = container_of(delayed_work, struct cfg80211_registered_device,
1041 background_cac_done_wk);
1042 cfg80211_background_cac_event(rdev, &rdev->background_radar_chandef,
1043 NL80211_RADAR_CAC_FINISHED);
1044 }
1045
cfg80211_background_cac_abort_wk(struct work_struct * work)1046 void cfg80211_background_cac_abort_wk(struct work_struct *work)
1047 {
1048 struct cfg80211_registered_device *rdev;
1049
1050 rdev = container_of(work, struct cfg80211_registered_device,
1051 background_cac_abort_wk);
1052 cfg80211_background_cac_event(rdev, &rdev->background_radar_chandef,
1053 NL80211_RADAR_CAC_ABORTED);
1054 }
1055
cfg80211_background_cac_abort(struct wiphy * wiphy)1056 void cfg80211_background_cac_abort(struct wiphy *wiphy)
1057 {
1058 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1059
1060 queue_work(cfg80211_wq, &rdev->background_cac_abort_wk);
1061 }
1062 EXPORT_SYMBOL(cfg80211_background_cac_abort);
1063
1064 int
cfg80211_start_background_radar_detection(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,struct cfg80211_chan_def * chandef)1065 cfg80211_start_background_radar_detection(struct cfg80211_registered_device *rdev,
1066 struct wireless_dev *wdev,
1067 struct cfg80211_chan_def *chandef)
1068 {
1069 unsigned int cac_time_ms;
1070 int err;
1071
1072 lockdep_assert_wiphy(&rdev->wiphy);
1073
1074 if (!wiphy_ext_feature_isset(&rdev->wiphy,
1075 NL80211_EXT_FEATURE_RADAR_BACKGROUND))
1076 return -EOPNOTSUPP;
1077
1078 /* Offchannel chain already locked by another wdev */
1079 if (rdev->background_radar_wdev && rdev->background_radar_wdev != wdev)
1080 return -EBUSY;
1081
1082 /* CAC already in progress on the offchannel chain */
1083 if (rdev->background_radar_wdev == wdev &&
1084 delayed_work_pending(&rdev->background_cac_done_wk))
1085 return -EBUSY;
1086
1087 err = rdev_set_radar_background(rdev, chandef);
1088 if (err)
1089 return err;
1090
1091 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, chandef);
1092 if (!cac_time_ms)
1093 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1094
1095 rdev->background_radar_chandef = *chandef;
1096 rdev->background_radar_wdev = wdev; /* Get offchain ownership */
1097
1098 __cfg80211_background_cac_event(rdev, wdev, chandef,
1099 NL80211_RADAR_CAC_STARTED);
1100 queue_delayed_work(cfg80211_wq, &rdev->background_cac_done_wk,
1101 msecs_to_jiffies(cac_time_ms));
1102
1103 return 0;
1104 }
1105
cfg80211_stop_background_radar_detection(struct wireless_dev * wdev)1106 void cfg80211_stop_background_radar_detection(struct wireless_dev *wdev)
1107 {
1108 struct wiphy *wiphy = wdev->wiphy;
1109 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1110
1111 lockdep_assert_wiphy(wiphy);
1112
1113 if (wdev != rdev->background_radar_wdev)
1114 return;
1115
1116 rdev_set_radar_background(rdev, NULL);
1117 rdev->background_radar_wdev = NULL; /* Release offchain ownership */
1118
1119 __cfg80211_background_cac_event(rdev, wdev,
1120 &rdev->background_radar_chandef,
1121 NL80211_RADAR_CAC_ABORTED);
1122 }
1123