xref: /linux/net/wireless/mlme.c (revision 5feaa7a07b85ebbef418ba4b80e4e0d23dc379f5)
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, 2022-2025 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 
24 void cfg80211_rx_assoc_resp(struct net_device *dev,
25 			    const struct cfg80211_rx_assoc_resp_data *data)
26 {
27 	struct wireless_dev *wdev = dev->ieee80211_ptr;
28 	struct wiphy *wiphy = wdev->wiphy;
29 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
30 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)data->buf;
31 	struct cfg80211_connect_resp_params cr = {
32 		.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED,
33 		.req_ie = data->req_ies,
34 		.req_ie_len = data->req_ies_len,
35 		.resp_ie = mgmt->u.assoc_resp.variable,
36 		.resp_ie_len = data->len -
37 			       offsetof(struct ieee80211_mgmt,
38 					u.assoc_resp.variable),
39 		.status = le16_to_cpu(mgmt->u.assoc_resp.status_code),
40 		.ap_mld_addr = data->ap_mld_addr,
41 	};
42 	unsigned int link_id;
43 
44 	for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) {
45 		cr.links[link_id].status = data->links[link_id].status;
46 		cr.links[link_id].bss = data->links[link_id].bss;
47 
48 		WARN_ON_ONCE(cr.links[link_id].status != WLAN_STATUS_SUCCESS &&
49 			     (!cr.ap_mld_addr || !cr.links[link_id].bss));
50 
51 		if (!cr.links[link_id].bss)
52 			continue;
53 		cr.links[link_id].bssid = data->links[link_id].bss->bssid;
54 		cr.links[link_id].addr = data->links[link_id].addr;
55 		/* need to have local link addresses for MLO connections */
56 		WARN_ON(cr.ap_mld_addr &&
57 			!is_valid_ether_addr(cr.links[link_id].addr));
58 
59 		BUG_ON(!cr.links[link_id].bss->channel);
60 
61 		if (cr.links[link_id].bss->channel->band == NL80211_BAND_S1GHZ) {
62 			WARN_ON(link_id);
63 			cr.resp_ie = (u8 *)&mgmt->u.s1g_assoc_resp.variable;
64 			cr.resp_ie_len = data->len -
65 					 offsetof(struct ieee80211_mgmt,
66 						  u.s1g_assoc_resp.variable);
67 		}
68 
69 		if (cr.ap_mld_addr)
70 			cr.valid_links |= BIT(link_id);
71 	}
72 
73 	trace_cfg80211_send_rx_assoc(dev, data);
74 
75 	/*
76 	 * This is a bit of a hack, we don't notify userspace of
77 	 * a (re-)association reply if we tried to send a reassoc
78 	 * and got a reject -- we only try again with an assoc
79 	 * frame instead of reassoc.
80 	 */
81 	if (cfg80211_sme_rx_assoc_resp(wdev, cr.status)) {
82 		for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) {
83 			struct cfg80211_bss *bss = data->links[link_id].bss;
84 
85 			if (!bss)
86 				continue;
87 
88 			cfg80211_unhold_bss(bss_from_pub(bss));
89 			cfg80211_put_bss(wiphy, bss);
90 		}
91 		return;
92 	}
93 
94 	nl80211_send_rx_assoc(rdev, dev, data);
95 	/* update current_bss etc., consumes the bss reference */
96 	__cfg80211_connect_result(dev, &cr, cr.status == WLAN_STATUS_SUCCESS);
97 }
98 EXPORT_SYMBOL(cfg80211_rx_assoc_resp);
99 
100 static void cfg80211_process_auth(struct wireless_dev *wdev,
101 				  const u8 *buf, size_t len)
102 {
103 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
104 
105 	nl80211_send_rx_auth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
106 	cfg80211_sme_rx_auth(wdev, buf, len);
107 }
108 
109 static void cfg80211_process_deauth(struct wireless_dev *wdev,
110 				    const u8 *buf, size_t len,
111 				    bool reconnect)
112 {
113 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
114 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
115 	const u8 *bssid = mgmt->bssid;
116 	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
117 	bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
118 
119 	nl80211_send_deauth(rdev, wdev->netdev, buf, len, reconnect, GFP_KERNEL);
120 
121 	if (!wdev->connected || !ether_addr_equal(wdev->u.client.connected_addr, bssid))
122 		return;
123 
124 	__cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
125 	cfg80211_sme_deauth(wdev);
126 }
127 
128 static void cfg80211_process_disassoc(struct wireless_dev *wdev,
129 				      const u8 *buf, size_t len,
130 				      bool reconnect)
131 {
132 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
133 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
134 	const u8 *bssid = mgmt->bssid;
135 	u16 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
136 	bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
137 
138 	nl80211_send_disassoc(rdev, wdev->netdev, buf, len, reconnect,
139 			      GFP_KERNEL);
140 
141 	if (WARN_ON(!wdev->connected ||
142 		    !ether_addr_equal(wdev->u.client.connected_addr, bssid)))
143 		return;
144 
145 	__cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
146 	cfg80211_sme_disassoc(wdev);
147 }
148 
149 void cfg80211_rx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
150 {
151 	struct wireless_dev *wdev = dev->ieee80211_ptr;
152 	struct ieee80211_mgmt *mgmt = (void *)buf;
153 
154 	lockdep_assert_wiphy(wdev->wiphy);
155 
156 	trace_cfg80211_rx_mlme_mgmt(dev, buf, len);
157 
158 	if (WARN_ON(len < 2))
159 		return;
160 
161 	if (ieee80211_is_auth(mgmt->frame_control))
162 		cfg80211_process_auth(wdev, buf, len);
163 	else if (ieee80211_is_deauth(mgmt->frame_control))
164 		cfg80211_process_deauth(wdev, buf, len, false);
165 	else if (ieee80211_is_disassoc(mgmt->frame_control))
166 		cfg80211_process_disassoc(wdev, buf, len, false);
167 }
168 EXPORT_SYMBOL(cfg80211_rx_mlme_mgmt);
169 
170 void cfg80211_auth_timeout(struct net_device *dev, const u8 *addr)
171 {
172 	struct wireless_dev *wdev = dev->ieee80211_ptr;
173 	struct wiphy *wiphy = wdev->wiphy;
174 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
175 
176 	trace_cfg80211_send_auth_timeout(dev, addr);
177 
178 	nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
179 	cfg80211_sme_auth_timeout(wdev);
180 }
181 EXPORT_SYMBOL(cfg80211_auth_timeout);
182 
183 void cfg80211_assoc_failure(struct net_device *dev,
184 			    struct cfg80211_assoc_failure *data)
185 {
186 	struct wireless_dev *wdev = dev->ieee80211_ptr;
187 	struct wiphy *wiphy = wdev->wiphy;
188 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
189 	const u8 *addr = data->ap_mld_addr ?: data->bss[0]->bssid;
190 	int i;
191 
192 	trace_cfg80211_send_assoc_failure(dev, data);
193 
194 	if (data->timeout) {
195 		nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
196 		cfg80211_sme_assoc_timeout(wdev);
197 	} else {
198 		cfg80211_sme_abandon_assoc(wdev);
199 	}
200 
201 	for (i = 0; i < ARRAY_SIZE(data->bss); i++) {
202 		struct cfg80211_bss *bss = data->bss[i];
203 
204 		if (!bss)
205 			continue;
206 
207 		cfg80211_unhold_bss(bss_from_pub(bss));
208 		cfg80211_put_bss(wiphy, bss);
209 	}
210 }
211 EXPORT_SYMBOL(cfg80211_assoc_failure);
212 
213 void cfg80211_tx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len,
214 			   bool reconnect)
215 {
216 	struct wireless_dev *wdev = dev->ieee80211_ptr;
217 	struct ieee80211_mgmt *mgmt = (void *)buf;
218 
219 	lockdep_assert_wiphy(wdev->wiphy);
220 
221 	trace_cfg80211_tx_mlme_mgmt(dev, buf, len, reconnect);
222 
223 	if (WARN_ON(len < 2))
224 		return;
225 
226 	if (ieee80211_is_deauth(mgmt->frame_control))
227 		cfg80211_process_deauth(wdev, buf, len, reconnect);
228 	else
229 		cfg80211_process_disassoc(wdev, buf, len, reconnect);
230 }
231 EXPORT_SYMBOL(cfg80211_tx_mlme_mgmt);
232 
233 void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
234 				  enum nl80211_key_type key_type, int key_id,
235 				  const u8 *tsc, gfp_t gfp)
236 {
237 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
238 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
239 #ifdef CONFIG_CFG80211_WEXT
240 	union iwreq_data wrqu;
241 	char *buf = kmalloc(128, gfp);
242 
243 	if (buf) {
244 		memset(&wrqu, 0, sizeof(wrqu));
245 		wrqu.data.length =
246 			sprintf(buf, "MLME-MICHAELMICFAILURE."
247 				"indication(keyid=%d %scast addr=%pM)",
248 				key_id, key_type == NL80211_KEYTYPE_GROUP
249 				? "broad" : "uni", addr);
250 		wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
251 		kfree(buf);
252 	}
253 #endif
254 
255 	trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
256 	nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
257 }
258 EXPORT_SYMBOL(cfg80211_michael_mic_failure);
259 
260 /* some MLME handling for userspace SME */
261 int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
262 		       struct net_device *dev,
263 		       struct cfg80211_auth_request *req)
264 {
265 	struct wireless_dev *wdev = dev->ieee80211_ptr;
266 
267 	lockdep_assert_wiphy(wdev->wiphy);
268 
269 	if (!req->bss)
270 		return -ENOENT;
271 
272 	if (req->link_id >= 0 &&
273 	    !(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
274 		return -EINVAL;
275 
276 	if (req->auth_type == NL80211_AUTHTYPE_SHARED_KEY) {
277 		if (!req->key || !req->key_len ||
278 		    req->key_idx < 0 || req->key_idx > 3)
279 			return -EINVAL;
280 	}
281 
282 	if (wdev->connected &&
283 	    ether_addr_equal(req->bss->bssid, wdev->u.client.connected_addr))
284 		return -EALREADY;
285 
286 	if (ether_addr_equal(req->bss->bssid, dev->dev_addr) ||
287 	    (req->link_id >= 0 &&
288 	     ether_addr_equal(req->ap_mld_addr, dev->dev_addr)))
289 		return -EINVAL;
290 
291 	return rdev_auth(rdev, dev, req);
292 }
293 
294 /*  Do a logical ht_capa &= ht_capa_mask.  */
295 void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
296 			       const struct ieee80211_ht_cap *ht_capa_mask)
297 {
298 	int i;
299 	u8 *p1, *p2;
300 	if (!ht_capa_mask) {
301 		memset(ht_capa, 0, sizeof(*ht_capa));
302 		return;
303 	}
304 
305 	p1 = (u8*)(ht_capa);
306 	p2 = (u8*)(ht_capa_mask);
307 	for (i = 0; i < sizeof(*ht_capa); i++)
308 		p1[i] &= p2[i];
309 }
310 
311 /*  Do a logical vht_capa &= vht_capa_mask.  */
312 void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
313 				const struct ieee80211_vht_cap *vht_capa_mask)
314 {
315 	int i;
316 	u8 *p1, *p2;
317 	if (!vht_capa_mask) {
318 		memset(vht_capa, 0, sizeof(*vht_capa));
319 		return;
320 	}
321 
322 	p1 = (u8*)(vht_capa);
323 	p2 = (u8*)(vht_capa_mask);
324 	for (i = 0; i < sizeof(*vht_capa); i++)
325 		p1[i] &= p2[i];
326 }
327 
328 static int
329 cfg80211_mlme_check_mlo_compat(const struct ieee80211_multi_link_elem *mle_a,
330 			       const struct ieee80211_multi_link_elem *mle_b,
331 			       struct netlink_ext_ack *extack)
332 {
333 	const struct ieee80211_mle_basic_common_info *common_a, *common_b;
334 
335 	common_a = (const void *)mle_a->variable;
336 	common_b = (const void *)mle_b->variable;
337 
338 	if (memcmp(common_a->mld_mac_addr, common_b->mld_mac_addr, ETH_ALEN)) {
339 		NL_SET_ERR_MSG(extack, "AP MLD address mismatch");
340 		return -EINVAL;
341 	}
342 
343 	if (ieee80211_mle_get_eml_cap((const u8 *)mle_a) !=
344 	    ieee80211_mle_get_eml_cap((const u8 *)mle_b)) {
345 		NL_SET_ERR_MSG(extack, "link EML capabilities mismatch");
346 		return -EINVAL;
347 	}
348 
349 	if (ieee80211_mle_get_mld_capa_op((const u8 *)mle_a) !=
350 	    ieee80211_mle_get_mld_capa_op((const u8 *)mle_b)) {
351 		NL_SET_ERR_MSG(extack, "link MLD capabilities/ops mismatch");
352 		return -EINVAL;
353 	}
354 
355 	/*
356 	 * Only verify the values in Extended MLD Capabilities that are
357 	 * not reserved when transmitted by an AP (and expected to remain the
358 	 * same over time).
359 	 * The Recommended Max Simultaneous Links subfield in particular is
360 	 * reserved when included in a unicast Probe Response frame and may
361 	 * also change when the AP adds/removes links. The BTM MLD
362 	 * Recommendation For Multiple APs Support subfield is reserved when
363 	 * transmitted by an AP. All other bits are currently reserved.
364 	 * See IEEE P802.11be/D7.0, Table 9-417o.
365 	 */
366 	if ((ieee80211_mle_get_ext_mld_capa_op((const u8 *)mle_a) &
367 	     (IEEE80211_EHT_ML_EXT_MLD_CAPA_OP_PARAM_UPDATE |
368 	      IEEE80211_EHT_ML_EXT_MLD_CAPA_NSTR_UPDATE |
369 	      IEEE80211_EHT_ML_EXT_MLD_CAPA_EMLSR_ENA_ON_ONE_LINK)) !=
370 	    (ieee80211_mle_get_ext_mld_capa_op((const u8 *)mle_b) &
371 	     (IEEE80211_EHT_ML_EXT_MLD_CAPA_OP_PARAM_UPDATE |
372 	      IEEE80211_EHT_ML_EXT_MLD_CAPA_NSTR_UPDATE |
373 	      IEEE80211_EHT_ML_EXT_MLD_CAPA_EMLSR_ENA_ON_ONE_LINK))) {
374 		NL_SET_ERR_MSG(extack,
375 			       "extended link MLD capabilities/ops mismatch");
376 		return -EINVAL;
377 	}
378 
379 	return 0;
380 }
381 
382 static int cfg80211_mlme_check_mlo(struct net_device *dev,
383 				   struct cfg80211_assoc_request *req,
384 				   struct netlink_ext_ack *extack)
385 {
386 	const struct ieee80211_multi_link_elem *mles[ARRAY_SIZE(req->links)] = {};
387 	int i;
388 
389 	if (req->link_id < 0)
390 		return 0;
391 
392 	if (!req->links[req->link_id].bss) {
393 		NL_SET_ERR_MSG(extack, "no BSS for assoc link");
394 		return -EINVAL;
395 	}
396 
397 	rcu_read_lock();
398 	for (i = 0; i < ARRAY_SIZE(req->links); i++) {
399 		const struct cfg80211_bss_ies *ies;
400 		const struct element *ml;
401 
402 		if (!req->links[i].bss)
403 			continue;
404 
405 		if (ether_addr_equal(req->links[i].bss->bssid, dev->dev_addr)) {
406 			NL_SET_ERR_MSG(extack, "BSSID must not be our address");
407 			req->links[i].error = -EINVAL;
408 			goto error;
409 		}
410 
411 		ies = rcu_dereference(req->links[i].bss->ies);
412 		ml = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_MULTI_LINK,
413 					    ies->data, ies->len);
414 		if (!ml) {
415 			NL_SET_ERR_MSG(extack, "MLO BSS w/o ML element");
416 			req->links[i].error = -EINVAL;
417 			goto error;
418 		}
419 
420 		if (!ieee80211_mle_type_ok(ml->data + 1,
421 					   IEEE80211_ML_CONTROL_TYPE_BASIC,
422 					   ml->datalen - 1)) {
423 			NL_SET_ERR_MSG(extack, "BSS with invalid ML element");
424 			req->links[i].error = -EINVAL;
425 			goto error;
426 		}
427 
428 		mles[i] = (const void *)(ml->data + 1);
429 
430 		if (ieee80211_mle_get_link_id((const u8 *)mles[i]) != i) {
431 			NL_SET_ERR_MSG(extack, "link ID mismatch");
432 			req->links[i].error = -EINVAL;
433 			goto error;
434 		}
435 	}
436 
437 	if (WARN_ON(!mles[req->link_id]))
438 		goto error;
439 
440 	for (i = 0; i < ARRAY_SIZE(req->links); i++) {
441 		if (i == req->link_id || !req->links[i].bss)
442 			continue;
443 
444 		if (WARN_ON(!mles[i]))
445 			goto error;
446 
447 		if (cfg80211_mlme_check_mlo_compat(mles[req->link_id], mles[i],
448 						   extack)) {
449 			req->links[i].error = -EINVAL;
450 			goto error;
451 		}
452 	}
453 
454 	rcu_read_unlock();
455 	return 0;
456 error:
457 	rcu_read_unlock();
458 	return -EINVAL;
459 }
460 
461 /* Note: caller must cfg80211_put_bss() regardless of result */
462 int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
463 			struct net_device *dev,
464 			struct cfg80211_assoc_request *req,
465 			struct netlink_ext_ack *extack)
466 {
467 	struct wireless_dev *wdev = dev->ieee80211_ptr;
468 	int err;
469 
470 	lockdep_assert_wiphy(wdev->wiphy);
471 
472 	err = cfg80211_mlme_check_mlo(dev, req, extack);
473 	if (err)
474 		return err;
475 
476 	if (wdev->connected &&
477 	    (!req->prev_bssid ||
478 	     !ether_addr_equal(wdev->u.client.connected_addr, req->prev_bssid)))
479 		return -EALREADY;
480 
481 	if ((req->bss && ether_addr_equal(req->bss->bssid, dev->dev_addr)) ||
482 	    (req->link_id >= 0 &&
483 	     ether_addr_equal(req->ap_mld_addr, dev->dev_addr)))
484 		return -EINVAL;
485 
486 	cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
487 				  rdev->wiphy.ht_capa_mod_mask);
488 	cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
489 				   rdev->wiphy.vht_capa_mod_mask);
490 
491 	err = rdev_assoc(rdev, dev, req);
492 	if (!err) {
493 		int link_id;
494 
495 		if (req->bss) {
496 			cfg80211_ref_bss(&rdev->wiphy, req->bss);
497 			cfg80211_hold_bss(bss_from_pub(req->bss));
498 		}
499 
500 		for (link_id = 0; link_id < ARRAY_SIZE(req->links); link_id++) {
501 			if (!req->links[link_id].bss)
502 				continue;
503 			cfg80211_ref_bss(&rdev->wiphy, req->links[link_id].bss);
504 			cfg80211_hold_bss(bss_from_pub(req->links[link_id].bss));
505 		}
506 	}
507 	return err;
508 }
509 
510 int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
511 			 struct net_device *dev, const u8 *bssid,
512 			 const u8 *ie, int ie_len, u16 reason,
513 			 bool local_state_change)
514 {
515 	struct wireless_dev *wdev = dev->ieee80211_ptr;
516 	struct cfg80211_deauth_request req = {
517 		.bssid = bssid,
518 		.reason_code = reason,
519 		.ie = ie,
520 		.ie_len = ie_len,
521 		.local_state_change = local_state_change,
522 	};
523 
524 	lockdep_assert_wiphy(wdev->wiphy);
525 
526 	if (local_state_change &&
527 	    (!wdev->connected ||
528 	     !ether_addr_equal(wdev->u.client.connected_addr, bssid)))
529 		return 0;
530 
531 	if (ether_addr_equal(wdev->disconnect_bssid, bssid) ||
532 	    (wdev->connected &&
533 	     ether_addr_equal(wdev->u.client.connected_addr, bssid)))
534 		wdev->conn_owner_nlportid = 0;
535 
536 	return rdev_deauth(rdev, dev, &req);
537 }
538 
539 int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
540 			   struct net_device *dev, const u8 *ap_addr,
541 			   const u8 *ie, int ie_len, u16 reason,
542 			   bool local_state_change)
543 {
544 	struct wireless_dev *wdev = dev->ieee80211_ptr;
545 	struct cfg80211_disassoc_request req = {
546 		.reason_code = reason,
547 		.local_state_change = local_state_change,
548 		.ie = ie,
549 		.ie_len = ie_len,
550 		.ap_addr = ap_addr,
551 	};
552 	int err;
553 
554 	lockdep_assert_wiphy(wdev->wiphy);
555 
556 	if (!wdev->connected)
557 		return -ENOTCONN;
558 
559 	if (memcmp(wdev->u.client.connected_addr, ap_addr, ETH_ALEN))
560 		return -ENOTCONN;
561 
562 	err = rdev_disassoc(rdev, dev, &req);
563 	if (err)
564 		return err;
565 
566 	/* driver should have reported the disassoc */
567 	WARN_ON(wdev->connected);
568 	return 0;
569 }
570 
571 void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
572 			struct net_device *dev)
573 {
574 	struct wireless_dev *wdev = dev->ieee80211_ptr;
575 	u8 bssid[ETH_ALEN];
576 
577 	lockdep_assert_wiphy(wdev->wiphy);
578 
579 	if (!rdev->ops->deauth)
580 		return;
581 
582 	if (!wdev->connected)
583 		return;
584 
585 	memcpy(bssid, wdev->u.client.connected_addr, ETH_ALEN);
586 	cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
587 			     WLAN_REASON_DEAUTH_LEAVING, false);
588 }
589 
590 struct cfg80211_mgmt_registration {
591 	struct list_head list;
592 	struct wireless_dev *wdev;
593 
594 	u32 nlportid;
595 
596 	int match_len;
597 
598 	__le16 frame_type;
599 
600 	bool multicast_rx;
601 
602 	u8 match[];
603 };
604 
605 static void cfg80211_mgmt_registrations_update(struct wireless_dev *wdev)
606 {
607 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
608 	struct wireless_dev *tmp;
609 	struct cfg80211_mgmt_registration *reg;
610 	struct mgmt_frame_regs upd = {};
611 
612 	lockdep_assert_held(&rdev->wiphy.mtx);
613 
614 	spin_lock_bh(&rdev->mgmt_registrations_lock);
615 	if (!wdev->mgmt_registrations_need_update) {
616 		spin_unlock_bh(&rdev->mgmt_registrations_lock);
617 		return;
618 	}
619 
620 	rcu_read_lock();
621 	list_for_each_entry_rcu(tmp, &rdev->wiphy.wdev_list, list) {
622 		list_for_each_entry(reg, &tmp->mgmt_registrations, list) {
623 			u32 mask = BIT(le16_to_cpu(reg->frame_type) >> 4);
624 			u32 mcast_mask = 0;
625 
626 			if (reg->multicast_rx)
627 				mcast_mask = mask;
628 
629 			upd.global_stypes |= mask;
630 			upd.global_mcast_stypes |= mcast_mask;
631 
632 			if (tmp == wdev) {
633 				upd.interface_stypes |= mask;
634 				upd.interface_mcast_stypes |= mcast_mask;
635 			}
636 		}
637 	}
638 	rcu_read_unlock();
639 
640 	wdev->mgmt_registrations_need_update = 0;
641 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
642 
643 	rdev_update_mgmt_frame_registrations(rdev, wdev, &upd);
644 }
645 
646 void cfg80211_mgmt_registrations_update_wk(struct work_struct *wk)
647 {
648 	struct cfg80211_registered_device *rdev;
649 	struct wireless_dev *wdev;
650 
651 	rdev = container_of(wk, struct cfg80211_registered_device,
652 			    mgmt_registrations_update_wk);
653 
654 	guard(wiphy)(&rdev->wiphy);
655 
656 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
657 		cfg80211_mgmt_registrations_update(wdev);
658 }
659 
660 int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
661 				u16 frame_type, const u8 *match_data,
662 				int match_len, bool multicast_rx,
663 				struct netlink_ext_ack *extack)
664 {
665 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
666 	struct cfg80211_mgmt_registration *reg, *nreg;
667 	int err = 0;
668 	u16 mgmt_type;
669 	bool update_multicast = false;
670 
671 	if (!wdev->wiphy->mgmt_stypes)
672 		return -EOPNOTSUPP;
673 
674 	if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT) {
675 		NL_SET_ERR_MSG(extack, "frame type not management");
676 		return -EINVAL;
677 	}
678 
679 	if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) {
680 		NL_SET_ERR_MSG(extack, "Invalid frame type");
681 		return -EINVAL;
682 	}
683 
684 	mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
685 	if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type))) {
686 		NL_SET_ERR_MSG(extack,
687 			       "Registration to specific type not supported");
688 		return -EINVAL;
689 	}
690 
691 	/*
692 	 * To support Pre Association Security Negotiation (PASN), registration
693 	 * for authentication frames should be supported. However, as some
694 	 * versions of the user space daemons wrongly register to all types of
695 	 * authentication frames (which might result in unexpected behavior)
696 	 * allow such registration if the request is for a specific
697 	 * authentication algorithm number.
698 	 */
699 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
700 	    (frame_type & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_AUTH &&
701 	    !(match_data && match_len >= 2)) {
702 		NL_SET_ERR_MSG(extack,
703 			       "Authentication algorithm number required");
704 		return -EINVAL;
705 	}
706 
707 	nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
708 	if (!nreg)
709 		return -ENOMEM;
710 
711 	spin_lock_bh(&rdev->mgmt_registrations_lock);
712 
713 	list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
714 		int mlen = min(match_len, reg->match_len);
715 
716 		if (frame_type != le16_to_cpu(reg->frame_type))
717 			continue;
718 
719 		if (memcmp(reg->match, match_data, mlen) == 0) {
720 			if (reg->multicast_rx != multicast_rx) {
721 				update_multicast = true;
722 				reg->multicast_rx = multicast_rx;
723 				break;
724 			}
725 			NL_SET_ERR_MSG(extack, "Match already configured");
726 			err = -EALREADY;
727 			break;
728 		}
729 	}
730 
731 	if (err)
732 		goto out;
733 
734 	if (update_multicast) {
735 		kfree(nreg);
736 	} else {
737 		memcpy(nreg->match, match_data, match_len);
738 		nreg->match_len = match_len;
739 		nreg->nlportid = snd_portid;
740 		nreg->frame_type = cpu_to_le16(frame_type);
741 		nreg->wdev = wdev;
742 		nreg->multicast_rx = multicast_rx;
743 		list_add(&nreg->list, &wdev->mgmt_registrations);
744 	}
745 	wdev->mgmt_registrations_need_update = 1;
746 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
747 
748 	cfg80211_mgmt_registrations_update(wdev);
749 
750 	return 0;
751 
752  out:
753 	kfree(nreg);
754 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
755 
756 	return err;
757 }
758 
759 void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
760 {
761 	struct wiphy *wiphy = wdev->wiphy;
762 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
763 	struct cfg80211_mgmt_registration *reg, *tmp;
764 
765 	spin_lock_bh(&rdev->mgmt_registrations_lock);
766 
767 	list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
768 		if (reg->nlportid != nlportid)
769 			continue;
770 
771 		list_del(&reg->list);
772 		kfree(reg);
773 
774 		wdev->mgmt_registrations_need_update = 1;
775 		schedule_work(&rdev->mgmt_registrations_update_wk);
776 	}
777 
778 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
779 
780 	if (nlportid && rdev->crit_proto_nlportid == nlportid) {
781 		rdev->crit_proto_nlportid = 0;
782 		rdev_crit_proto_stop(rdev, wdev);
783 	}
784 
785 	if (nlportid == wdev->ap_unexpected_nlportid)
786 		wdev->ap_unexpected_nlportid = 0;
787 }
788 
789 void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
790 {
791 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
792 	struct cfg80211_mgmt_registration *reg, *tmp;
793 
794 	spin_lock_bh(&rdev->mgmt_registrations_lock);
795 	list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
796 		list_del(&reg->list);
797 		kfree(reg);
798 	}
799 	wdev->mgmt_registrations_need_update = 1;
800 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
801 
802 	cfg80211_mgmt_registrations_update(wdev);
803 }
804 
805 static bool cfg80211_allowed_address(struct wireless_dev *wdev, const u8 *addr)
806 {
807 	int i;
808 
809 	for_each_valid_link(wdev, i) {
810 		if (ether_addr_equal(addr, wdev->links[i].addr))
811 			return true;
812 	}
813 
814 	return ether_addr_equal(addr, wdev_address(wdev));
815 }
816 
817 static bool cfg80211_allowed_random_address(struct wireless_dev *wdev,
818 					    const struct ieee80211_mgmt *mgmt)
819 {
820 	if (ieee80211_is_auth(mgmt->frame_control) ||
821 	    ieee80211_is_deauth(mgmt->frame_control)) {
822 		/* Allow random TA to be used with authentication and
823 		 * deauthentication frames if the driver has indicated support.
824 		 */
825 		if (wiphy_ext_feature_isset(
826 			    wdev->wiphy,
827 			    NL80211_EXT_FEATURE_AUTH_AND_DEAUTH_RANDOM_TA))
828 			return true;
829 	} else if (ieee80211_is_action(mgmt->frame_control) &&
830 		   mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
831 		/* Allow random TA to be used with Public Action frames if the
832 		 * driver has indicated support.
833 		 */
834 		if (!wdev->connected &&
835 		    wiphy_ext_feature_isset(
836 			    wdev->wiphy,
837 			    NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA))
838 			return true;
839 
840 		if (wdev->connected &&
841 		    wiphy_ext_feature_isset(
842 			    wdev->wiphy,
843 			    NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA_CONNECTED))
844 			return true;
845 	}
846 
847 	return false;
848 }
849 
850 int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
851 			  struct wireless_dev *wdev,
852 			  struct cfg80211_mgmt_tx_params *params, u64 *cookie)
853 {
854 	const struct ieee80211_mgmt *mgmt;
855 	u16 stype;
856 
857 	lockdep_assert_wiphy(&rdev->wiphy);
858 
859 	if (!wdev->wiphy->mgmt_stypes)
860 		return -EOPNOTSUPP;
861 
862 	if (!rdev->ops->mgmt_tx)
863 		return -EOPNOTSUPP;
864 
865 	if (params->len < 24 + 1)
866 		return -EINVAL;
867 
868 	mgmt = (const struct ieee80211_mgmt *)params->buf;
869 
870 	if (!ieee80211_is_mgmt(mgmt->frame_control))
871 		return -EINVAL;
872 
873 	stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
874 	if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
875 		return -EINVAL;
876 
877 	if (ieee80211_is_action(mgmt->frame_control) &&
878 	    mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
879 		int err = 0;
880 
881 		switch (wdev->iftype) {
882 		case NL80211_IFTYPE_ADHOC:
883 			/*
884 			 * check for IBSS DA must be done by driver as
885 			 * cfg80211 doesn't track the stations
886 			 */
887 			if (!wdev->u.ibss.current_bss ||
888 			    !ether_addr_equal(wdev->u.ibss.current_bss->pub.bssid,
889 					      mgmt->bssid)) {
890 				err = -ENOTCONN;
891 				break;
892 			}
893 			break;
894 		case NL80211_IFTYPE_STATION:
895 		case NL80211_IFTYPE_P2P_CLIENT:
896 			if (!wdev->connected) {
897 				err = -ENOTCONN;
898 				break;
899 			}
900 
901 			/* FIXME: MLD may address this differently */
902 
903 			if (!ether_addr_equal(wdev->u.client.connected_addr,
904 					      mgmt->bssid)) {
905 				err = -ENOTCONN;
906 				break;
907 			}
908 
909 			/* for station, check that DA is the AP */
910 			if (!ether_addr_equal(wdev->u.client.connected_addr,
911 					      mgmt->da)) {
912 				err = -ENOTCONN;
913 				break;
914 			}
915 			break;
916 		case NL80211_IFTYPE_AP:
917 		case NL80211_IFTYPE_P2P_GO:
918 		case NL80211_IFTYPE_AP_VLAN:
919 			if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)) &&
920 			    (params->link_id < 0 ||
921 			     !ether_addr_equal(mgmt->bssid,
922 					       wdev->links[params->link_id].addr)))
923 				err = -EINVAL;
924 			break;
925 		case NL80211_IFTYPE_MESH_POINT:
926 			if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
927 				err = -EINVAL;
928 				break;
929 			}
930 			/*
931 			 * check for mesh DA must be done by driver as
932 			 * cfg80211 doesn't track the stations
933 			 */
934 			break;
935 		case NL80211_IFTYPE_P2P_DEVICE:
936 			/*
937 			 * fall through, P2P device only supports
938 			 * public action frames
939 			 */
940 		case NL80211_IFTYPE_NAN:
941 		default:
942 			err = -EOPNOTSUPP;
943 			break;
944 		}
945 
946 		if (err)
947 			return err;
948 	}
949 
950 	if (!cfg80211_allowed_address(wdev, mgmt->sa) &&
951 	    !cfg80211_allowed_random_address(wdev, mgmt))
952 		return -EINVAL;
953 
954 	/* Transmit the management frame as requested by user space */
955 	return rdev_mgmt_tx(rdev, wdev, params, cookie);
956 }
957 
958 bool cfg80211_rx_mgmt_ext(struct wireless_dev *wdev,
959 			  struct cfg80211_rx_info *info)
960 {
961 	struct wiphy *wiphy = wdev->wiphy;
962 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
963 	struct cfg80211_mgmt_registration *reg;
964 	const struct ieee80211_txrx_stypes *stypes =
965 		&wiphy->mgmt_stypes[wdev->iftype];
966 	struct ieee80211_mgmt *mgmt = (void *)info->buf;
967 	const u8 *data;
968 	int data_len;
969 	bool result = false;
970 	__le16 ftype = mgmt->frame_control &
971 		cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
972 	u16 stype;
973 
974 	trace_cfg80211_rx_mgmt(wdev, info);
975 	stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
976 
977 	if (!(stypes->rx & BIT(stype))) {
978 		trace_cfg80211_return_bool(false);
979 		return false;
980 	}
981 
982 	data = info->buf + ieee80211_hdrlen(mgmt->frame_control);
983 	data_len = info->len - ieee80211_hdrlen(mgmt->frame_control);
984 
985 	spin_lock_bh(&rdev->mgmt_registrations_lock);
986 
987 	list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
988 		if (reg->frame_type != ftype)
989 			continue;
990 
991 		if (reg->match_len > data_len)
992 			continue;
993 
994 		if (memcmp(reg->match, data, reg->match_len))
995 			continue;
996 
997 		/* found match! */
998 
999 		/* Indicate the received Action frame to user space */
1000 		if (nl80211_send_mgmt(rdev, wdev, reg->nlportid, info,
1001 				      GFP_ATOMIC))
1002 			continue;
1003 
1004 		result = true;
1005 		break;
1006 	}
1007 
1008 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
1009 
1010 	trace_cfg80211_return_bool(result);
1011 	return result;
1012 }
1013 EXPORT_SYMBOL(cfg80211_rx_mgmt_ext);
1014 
1015 void cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device *rdev)
1016 {
1017 	cancel_delayed_work(&rdev->dfs_update_channels_wk);
1018 	queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk, 0);
1019 }
1020 
1021 void cfg80211_dfs_channels_update_work(struct work_struct *work)
1022 {
1023 	struct delayed_work *delayed_work = to_delayed_work(work);
1024 	struct cfg80211_registered_device *rdev;
1025 	struct cfg80211_chan_def chandef;
1026 	struct ieee80211_supported_band *sband;
1027 	struct ieee80211_channel *c;
1028 	struct wiphy *wiphy;
1029 	bool check_again = false;
1030 	unsigned long timeout, next_time = 0;
1031 	unsigned long time_dfs_update;
1032 	enum nl80211_radar_event radar_event;
1033 	int bandid, i;
1034 
1035 	rdev = container_of(delayed_work, struct cfg80211_registered_device,
1036 			    dfs_update_channels_wk);
1037 	wiphy = &rdev->wiphy;
1038 
1039 	rtnl_lock();
1040 	for (bandid = 0; bandid < NUM_NL80211_BANDS; bandid++) {
1041 		sband = wiphy->bands[bandid];
1042 		if (!sband)
1043 			continue;
1044 
1045 		for (i = 0; i < sband->n_channels; i++) {
1046 			c = &sband->channels[i];
1047 
1048 			if (!(c->flags & IEEE80211_CHAN_RADAR))
1049 				continue;
1050 
1051 			if (c->dfs_state != NL80211_DFS_UNAVAILABLE &&
1052 			    c->dfs_state != NL80211_DFS_AVAILABLE)
1053 				continue;
1054 
1055 			if (c->dfs_state == NL80211_DFS_UNAVAILABLE) {
1056 				time_dfs_update = IEEE80211_DFS_MIN_NOP_TIME_MS;
1057 				radar_event = NL80211_RADAR_NOP_FINISHED;
1058 			} else {
1059 				if (regulatory_pre_cac_allowed(wiphy) ||
1060 				    cfg80211_any_wiphy_oper_chan(wiphy, c))
1061 					continue;
1062 
1063 				time_dfs_update = REG_PRE_CAC_EXPIRY_GRACE_MS;
1064 				radar_event = NL80211_RADAR_PRE_CAC_EXPIRED;
1065 			}
1066 
1067 			timeout = c->dfs_state_entered +
1068 				  msecs_to_jiffies(time_dfs_update);
1069 
1070 			if (time_after_eq(jiffies, timeout)) {
1071 				c->dfs_state = NL80211_DFS_USABLE;
1072 				c->dfs_state_entered = jiffies;
1073 
1074 				cfg80211_chandef_create(&chandef, c,
1075 							NL80211_CHAN_NO_HT);
1076 
1077 				nl80211_radar_notify(rdev, &chandef,
1078 						     radar_event, NULL,
1079 						     GFP_ATOMIC);
1080 
1081 				regulatory_propagate_dfs_state(wiphy, &chandef,
1082 							       c->dfs_state,
1083 							       radar_event);
1084 				continue;
1085 			}
1086 
1087 			if (!check_again)
1088 				next_time = timeout - jiffies;
1089 			else
1090 				next_time = min(next_time, timeout - jiffies);
1091 			check_again = true;
1092 		}
1093 	}
1094 	rtnl_unlock();
1095 
1096 	/* reschedule if there are other channels waiting to be cleared again */
1097 	if (check_again)
1098 		queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
1099 				   next_time);
1100 }
1101 
1102 
1103 void __cfg80211_radar_event(struct wiphy *wiphy,
1104 			    struct cfg80211_chan_def *chandef,
1105 			    bool offchan, gfp_t gfp)
1106 {
1107 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1108 
1109 	trace_cfg80211_radar_event(wiphy, chandef, offchan);
1110 
1111 	/* only set the chandef supplied channel to unavailable, in
1112 	 * case the radar is detected on only one of multiple channels
1113 	 * spanned by the chandef.
1114 	 */
1115 	cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
1116 
1117 	if (offchan)
1118 		queue_work(cfg80211_wq, &rdev->background_cac_abort_wk);
1119 
1120 	cfg80211_sched_dfs_chan_update(rdev);
1121 
1122 	nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
1123 
1124 	memcpy(&rdev->radar_chandef, chandef, sizeof(struct cfg80211_chan_def));
1125 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
1126 }
1127 EXPORT_SYMBOL(__cfg80211_radar_event);
1128 
1129 void cfg80211_cac_event(struct net_device *netdev,
1130 			const struct cfg80211_chan_def *chandef,
1131 			enum nl80211_radar_event event, gfp_t gfp,
1132 			unsigned int link_id)
1133 {
1134 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
1135 	struct wiphy *wiphy = wdev->wiphy;
1136 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1137 	unsigned long timeout;
1138 
1139 	if (WARN_ON(wdev->valid_links &&
1140 		    !(wdev->valid_links & BIT(link_id))))
1141 		return;
1142 
1143 	trace_cfg80211_cac_event(netdev, event, link_id);
1144 
1145 	if (WARN_ON(!wdev->links[link_id].cac_started &&
1146 		    event != NL80211_RADAR_CAC_STARTED))
1147 		return;
1148 
1149 	switch (event) {
1150 	case NL80211_RADAR_CAC_FINISHED:
1151 		timeout = wdev->links[link_id].cac_start_time +
1152 			  msecs_to_jiffies(wdev->links[link_id].cac_time_ms);
1153 		WARN_ON(!time_after_eq(jiffies, timeout));
1154 		cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
1155 		memcpy(&rdev->cac_done_chandef, chandef,
1156 		       sizeof(struct cfg80211_chan_def));
1157 		queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk);
1158 		cfg80211_sched_dfs_chan_update(rdev);
1159 		fallthrough;
1160 	case NL80211_RADAR_CAC_ABORTED:
1161 		wdev->links[link_id].cac_started = false;
1162 		break;
1163 	case NL80211_RADAR_CAC_STARTED:
1164 		wdev->links[link_id].cac_started = true;
1165 		break;
1166 	default:
1167 		WARN_ON(1);
1168 		return;
1169 	}
1170 
1171 	nl80211_radar_notify(rdev, chandef, event, netdev, gfp);
1172 }
1173 EXPORT_SYMBOL(cfg80211_cac_event);
1174 
1175 static void
1176 __cfg80211_background_cac_event(struct cfg80211_registered_device *rdev,
1177 				struct wireless_dev *wdev,
1178 				const struct cfg80211_chan_def *chandef,
1179 				enum nl80211_radar_event event)
1180 {
1181 	struct wiphy *wiphy = &rdev->wiphy;
1182 	struct net_device *netdev;
1183 
1184 	lockdep_assert_wiphy(&rdev->wiphy);
1185 
1186 	if (!cfg80211_chandef_valid(chandef))
1187 		return;
1188 
1189 	if (!rdev->background_radar_wdev)
1190 		return;
1191 
1192 	switch (event) {
1193 	case NL80211_RADAR_CAC_FINISHED:
1194 		cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
1195 		memcpy(&rdev->cac_done_chandef, chandef, sizeof(*chandef));
1196 		queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk);
1197 		cfg80211_sched_dfs_chan_update(rdev);
1198 		wdev = rdev->background_radar_wdev;
1199 		break;
1200 	case NL80211_RADAR_CAC_ABORTED:
1201 		if (!cancel_delayed_work(&rdev->background_cac_done_wk))
1202 			return;
1203 		wdev = rdev->background_radar_wdev;
1204 		break;
1205 	case NL80211_RADAR_CAC_STARTED:
1206 		break;
1207 	default:
1208 		return;
1209 	}
1210 
1211 	netdev = wdev ? wdev->netdev : NULL;
1212 	nl80211_radar_notify(rdev, chandef, event, netdev, GFP_KERNEL);
1213 }
1214 
1215 static void
1216 cfg80211_background_cac_event(struct cfg80211_registered_device *rdev,
1217 			      const struct cfg80211_chan_def *chandef,
1218 			      enum nl80211_radar_event event)
1219 {
1220 	guard(wiphy)(&rdev->wiphy);
1221 
1222 	__cfg80211_background_cac_event(rdev, rdev->background_radar_wdev,
1223 					chandef, event);
1224 }
1225 
1226 void cfg80211_background_cac_done_wk(struct work_struct *work)
1227 {
1228 	struct delayed_work *delayed_work = to_delayed_work(work);
1229 	struct cfg80211_registered_device *rdev;
1230 
1231 	rdev = container_of(delayed_work, struct cfg80211_registered_device,
1232 			    background_cac_done_wk);
1233 	cfg80211_background_cac_event(rdev, &rdev->background_radar_chandef,
1234 				      NL80211_RADAR_CAC_FINISHED);
1235 }
1236 
1237 void cfg80211_background_cac_abort_wk(struct work_struct *work)
1238 {
1239 	struct cfg80211_registered_device *rdev;
1240 
1241 	rdev = container_of(work, struct cfg80211_registered_device,
1242 			    background_cac_abort_wk);
1243 	cfg80211_background_cac_event(rdev, &rdev->background_radar_chandef,
1244 				      NL80211_RADAR_CAC_ABORTED);
1245 }
1246 
1247 void cfg80211_background_cac_abort(struct wiphy *wiphy)
1248 {
1249 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1250 
1251 	queue_work(cfg80211_wq, &rdev->background_cac_abort_wk);
1252 }
1253 EXPORT_SYMBOL(cfg80211_background_cac_abort);
1254 
1255 int
1256 cfg80211_start_background_radar_detection(struct cfg80211_registered_device *rdev,
1257 					  struct wireless_dev *wdev,
1258 					  struct cfg80211_chan_def *chandef)
1259 {
1260 	unsigned int cac_time_ms;
1261 	int err;
1262 
1263 	lockdep_assert_wiphy(&rdev->wiphy);
1264 
1265 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
1266 				     NL80211_EXT_FEATURE_RADAR_BACKGROUND))
1267 		return -EOPNOTSUPP;
1268 
1269 	/* Offchannel chain already locked by another wdev */
1270 	if (rdev->background_radar_wdev && rdev->background_radar_wdev != wdev)
1271 		return -EBUSY;
1272 
1273 	/* CAC already in progress on the offchannel chain */
1274 	if (rdev->background_radar_wdev == wdev &&
1275 	    delayed_work_pending(&rdev->background_cac_done_wk))
1276 		return -EBUSY;
1277 
1278 	err = rdev_set_radar_background(rdev, chandef);
1279 	if (err)
1280 		return err;
1281 
1282 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, chandef);
1283 	if (!cac_time_ms)
1284 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1285 
1286 	rdev->background_radar_chandef = *chandef;
1287 	rdev->background_radar_wdev = wdev; /* Get offchain ownership */
1288 
1289 	__cfg80211_background_cac_event(rdev, wdev, chandef,
1290 					NL80211_RADAR_CAC_STARTED);
1291 	queue_delayed_work(cfg80211_wq, &rdev->background_cac_done_wk,
1292 			   msecs_to_jiffies(cac_time_ms));
1293 
1294 	return 0;
1295 }
1296 
1297 void cfg80211_stop_background_radar_detection(struct wireless_dev *wdev)
1298 {
1299 	struct wiphy *wiphy = wdev->wiphy;
1300 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1301 
1302 	lockdep_assert_wiphy(wiphy);
1303 
1304 	if (wdev != rdev->background_radar_wdev)
1305 		return;
1306 
1307 	rdev_set_radar_background(rdev, NULL);
1308 	rdev->background_radar_wdev = NULL; /* Release offchain ownership */
1309 
1310 	__cfg80211_background_cac_event(rdev, wdev,
1311 					&rdev->background_radar_chandef,
1312 					NL80211_RADAR_CAC_ABORTED);
1313 }
1314 
1315 int cfg80211_assoc_ml_reconf(struct cfg80211_registered_device *rdev,
1316 			     struct net_device *dev,
1317 			     struct cfg80211_ml_reconf_req *req)
1318 {
1319 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1320 	int err;
1321 
1322 	lockdep_assert_wiphy(wdev->wiphy);
1323 
1324 	err = rdev_assoc_ml_reconf(rdev, dev, req);
1325 	if (!err) {
1326 		int link_id;
1327 
1328 		for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS;
1329 		     link_id++) {
1330 			if (!req->add_links[link_id].bss)
1331 				continue;
1332 
1333 			cfg80211_ref_bss(&rdev->wiphy, req->add_links[link_id].bss);
1334 			cfg80211_hold_bss(bss_from_pub(req->add_links[link_id].bss));
1335 		}
1336 	}
1337 
1338 	return err;
1339 }
1340 
1341 void cfg80211_mlo_reconf_add_done(struct net_device *dev,
1342 				  struct cfg80211_mlo_reconf_done_data *data)
1343 {
1344 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1345 	struct wiphy *wiphy = wdev->wiphy;
1346 	int link_id;
1347 
1348 	lockdep_assert_wiphy(wiphy);
1349 
1350 	trace_cfg80211_mlo_reconf_add_done(dev, data->added_links,
1351 					   data->buf, data->len,
1352 					   data->driver_initiated);
1353 
1354 	if (WARN_ON(!wdev->valid_links))
1355 		return;
1356 
1357 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1358 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1359 		return;
1360 
1361 	/* validate that a BSS is given for each added link */
1362 	for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) {
1363 		struct cfg80211_bss *bss = data->links[link_id].bss;
1364 
1365 		if (!(data->added_links & BIT(link_id)))
1366 			continue;
1367 
1368 		if (WARN_ON(!bss))
1369 			return;
1370 	}
1371 
1372 	for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) {
1373 		struct cfg80211_bss *bss = data->links[link_id].bss;
1374 
1375 		if (!bss)
1376 			continue;
1377 
1378 		if (data->added_links & BIT(link_id)) {
1379 			wdev->links[link_id].client.current_bss =
1380 				bss_from_pub(bss);
1381 
1382 			if (data->driver_initiated)
1383 				cfg80211_hold_bss(bss_from_pub(bss));
1384 
1385 			memcpy(wdev->links[link_id].addr,
1386 			       data->links[link_id].addr,
1387 			       ETH_ALEN);
1388 		} else {
1389 			if (!data->driver_initiated)
1390 				cfg80211_unhold_bss(bss_from_pub(bss));
1391 
1392 			cfg80211_put_bss(wiphy, bss);
1393 		}
1394 	}
1395 
1396 	wdev->valid_links |= data->added_links;
1397 	nl80211_mlo_reconf_add_done(dev, data);
1398 }
1399 EXPORT_SYMBOL(cfg80211_mlo_reconf_add_done);
1400