xref: /linux/net/mac80211/cfg.c (revision 77de28cd7cf172e782319a144bf64e693794d78b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * mac80211 configuration hooks for cfg80211
4  *
5  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2015  Intel Mobile Communications GmbH
7  * Copyright (C) 2015-2017 Intel Deutschland GmbH
8  * Copyright (C) 2018-2026 Intel Corporation
9  */
10 
11 #include <linux/ieee80211.h>
12 #include <linux/nl80211.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/slab.h>
15 #include <net/net_namespace.h>
16 #include <linux/rcupdate.h>
17 #include <linux/fips.h>
18 #include <linux/if_ether.h>
19 #include <net/cfg80211.h>
20 #include "ieee80211_i.h"
21 #include "driver-ops.h"
22 #include "rate.h"
23 #include "mesh.h"
24 #include "wme.h"
25 
26 static struct ieee80211_link_data *
27 ieee80211_link_or_deflink(struct ieee80211_sub_if_data *sdata, int link_id,
28 			  bool require_valid)
29 {
30 	struct ieee80211_link_data *link;
31 
32 	if (link_id < 0) {
33 		/*
34 		 * For keys, if sdata is not an MLD, we might not use
35 		 * the return value at all (if it's not a pairwise key),
36 		 * so in that case (require_valid==false) don't error.
37 		 */
38 		if (require_valid && ieee80211_vif_is_mld(&sdata->vif))
39 			return ERR_PTR(-EINVAL);
40 
41 		return &sdata->deflink;
42 	}
43 
44 	link = sdata_dereference(sdata->link[link_id], sdata);
45 	if (!link)
46 		return ERR_PTR(-ENOLINK);
47 	return link;
48 }
49 
50 static void ieee80211_set_mu_mimo_follow(struct ieee80211_sub_if_data *sdata,
51 					 struct vif_params *params)
52 {
53 	bool mu_mimo_groups = false;
54 	bool mu_mimo_follow = false;
55 
56 	if (params->vht_mumimo_groups) {
57 		u64 membership;
58 
59 		BUILD_BUG_ON(sizeof(membership) != WLAN_MEMBERSHIP_LEN);
60 
61 		memcpy(sdata->vif.bss_conf.mu_group.membership,
62 		       params->vht_mumimo_groups, WLAN_MEMBERSHIP_LEN);
63 		memcpy(sdata->vif.bss_conf.mu_group.position,
64 		       params->vht_mumimo_groups + WLAN_MEMBERSHIP_LEN,
65 		       WLAN_USER_POSITION_LEN);
66 
67 		/* don't care about endianness - just check for 0 */
68 		memcpy(&membership, params->vht_mumimo_groups,
69 		       WLAN_MEMBERSHIP_LEN);
70 		mu_mimo_groups = membership != 0;
71 
72 		/* Unset following if configured explicitly */
73 		eth_broadcast_addr(sdata->u.mntr.mu_follow_addr);
74 	}
75 
76 	if (params->vht_mumimo_follow_addr) {
77 		mu_mimo_follow =
78 			is_valid_ether_addr(params->vht_mumimo_follow_addr);
79 		ether_addr_copy(sdata->u.mntr.mu_follow_addr,
80 				params->vht_mumimo_follow_addr);
81 
82 		/* Unset current membership until a management frame is RXed */
83 		memset(sdata->vif.bss_conf.mu_group.membership, 0,
84 		       WLAN_MEMBERSHIP_LEN);
85 	}
86 
87 	sdata->vif.bss_conf.mu_mimo_owner = mu_mimo_groups || mu_mimo_follow;
88 
89 	/* Notify only after setting mu_mimo_owner */
90 	if (sdata->vif.bss_conf.mu_mimo_owner &&
91 	    sdata->flags & IEEE80211_SDATA_IN_DRIVER)
92 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
93 						  BSS_CHANGED_MU_GROUPS);
94 }
95 
96 static int ieee80211_set_mon_options(struct ieee80211_sub_if_data *sdata,
97 				     struct vif_params *params)
98 {
99 	struct ieee80211_local *local = sdata->local;
100 	struct ieee80211_sub_if_data *monitor_sdata = NULL;
101 
102 	/* check flags first */
103 	if (params->flags && ieee80211_sdata_running(sdata)) {
104 		u32 mask = MONITOR_FLAG_ACTIVE;
105 
106 		/*
107 		 * Prohibit MONITOR_FLAG_ACTIVE to be changed
108 		 * while the interface is up.
109 		 * Else we would need to add a lot of cruft
110 		 * to update everything:
111 		 *	monitor and all fif_* counters
112 		 *	reconfigure hardware
113 		 */
114 		if ((params->flags & mask) != (sdata->u.mntr.flags & mask))
115 			return -EBUSY;
116 	}
117 
118 	/* validate whether MU-MIMO can be configured */
119 	if (!ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF) &&
120 	    !ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR) &&
121 	    (params->vht_mumimo_groups || params->vht_mumimo_follow_addr))
122 		return -EOPNOTSUPP;
123 
124 	/* Also update dependent monitor_sdata if required */
125 	if (test_bit(SDATA_STATE_RUNNING, &sdata->state) &&
126 	    !ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
127 		monitor_sdata = wiphy_dereference(local->hw.wiphy,
128 						  local->monitor_sdata);
129 
130 	/* apply all changes now - no failures allowed */
131 
132 	if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF) ||
133 	    ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) {
134 		/* This is copied in when the VIF is activated */
135 		ieee80211_set_mu_mimo_follow(sdata, params);
136 
137 		if (monitor_sdata)
138 			ieee80211_set_mu_mimo_follow(monitor_sdata, params);
139 	}
140 
141 	if (params->flags) {
142 		if (ieee80211_sdata_running(sdata)) {
143 			ieee80211_adjust_monitor_flags(sdata, -1);
144 			sdata->u.mntr.flags = params->flags;
145 			ieee80211_adjust_monitor_flags(sdata, 1);
146 
147 			ieee80211_configure_filter(local);
148 		} else {
149 			/*
150 			 * Because the interface is down, ieee80211_do_stop
151 			 * and ieee80211_do_open take care of "everything"
152 			 * mentioned in the comment above.
153 			 */
154 			sdata->u.mntr.flags = params->flags;
155 		}
156 	}
157 
158 	return 0;
159 }
160 
161 static int ieee80211_set_ap_mbssid_options(struct ieee80211_sub_if_data *sdata,
162 					   struct cfg80211_mbssid_config *params,
163 					   struct ieee80211_bss_conf *link_conf)
164 {
165 	struct ieee80211_sub_if_data *tx_sdata;
166 	struct ieee80211_bss_conf *old;
167 
168 	link_conf->bssid_index = 0;
169 	link_conf->nontransmitted = false;
170 	link_conf->ema_ap = false;
171 	link_conf->bssid_indicator = 0;
172 
173 	if (sdata->vif.type != NL80211_IFTYPE_AP || !params->tx_wdev)
174 		return -EINVAL;
175 
176 	old = sdata_dereference(link_conf->tx_bss_conf, sdata);
177 	if (old)
178 		return -EALREADY;
179 
180 	tx_sdata = IEEE80211_WDEV_TO_SUB_IF(params->tx_wdev);
181 	if (!tx_sdata)
182 		return -EINVAL;
183 
184 	if (tx_sdata == sdata) {
185 		rcu_assign_pointer(link_conf->tx_bss_conf, link_conf);
186 	} else {
187 		struct ieee80211_bss_conf *tx_bss_conf;
188 
189 		tx_bss_conf = sdata_dereference(tx_sdata->vif.link_conf[params->tx_link_id],
190 						sdata);
191 		if (rcu_access_pointer(tx_bss_conf->tx_bss_conf) != tx_bss_conf)
192 			return -EINVAL;
193 
194 		rcu_assign_pointer(link_conf->tx_bss_conf, tx_bss_conf);
195 
196 		link_conf->nontransmitted = true;
197 		link_conf->bssid_index = params->index;
198 		link_conf->bssid_indicator = tx_bss_conf->bssid_indicator;
199 	}
200 	if (params->ema)
201 		link_conf->ema_ap = true;
202 
203 	return 0;
204 }
205 
206 static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy,
207 						const char *name,
208 						unsigned char name_assign_type,
209 						enum nl80211_iftype type,
210 						struct vif_params *params)
211 {
212 	struct ieee80211_local *local = wiphy_priv(wiphy);
213 	struct wireless_dev *wdev;
214 	struct ieee80211_sub_if_data *sdata;
215 	int err;
216 
217 	err = ieee80211_if_add(local, name, name_assign_type, &wdev, type, params);
218 	if (err)
219 		return ERR_PTR(err);
220 
221 	sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
222 
223 	if (type == NL80211_IFTYPE_MONITOR) {
224 		err = ieee80211_set_mon_options(sdata, params);
225 		if (err) {
226 			ieee80211_if_remove(sdata);
227 			return NULL;
228 		}
229 	}
230 
231 	/* Let the driver know that an interface is going to be added.
232 	 * Indicate so only for interface types that will be added to the
233 	 * driver.
234 	 */
235 	switch (type) {
236 	case NL80211_IFTYPE_AP_VLAN:
237 		break;
238 	case NL80211_IFTYPE_MONITOR:
239 		if (!ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF) ||
240 		    !(params->flags & MONITOR_FLAG_ACTIVE))
241 			break;
242 		fallthrough;
243 	default:
244 		drv_prep_add_interface(local,
245 				       ieee80211_vif_type_p2p(&sdata->vif));
246 		break;
247 	}
248 
249 	return wdev;
250 }
251 
252 static int ieee80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
253 {
254 	ieee80211_if_remove(IEEE80211_WDEV_TO_SUB_IF(wdev));
255 
256 	return 0;
257 }
258 
259 static int ieee80211_change_iface(struct wiphy *wiphy,
260 				  struct net_device *dev,
261 				  enum nl80211_iftype type,
262 				  struct vif_params *params)
263 {
264 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
265 	struct ieee80211_local *local = sdata->local;
266 	struct sta_info *sta;
267 	int ret;
268 
269 	lockdep_assert_wiphy(local->hw.wiphy);
270 
271 	ret = ieee80211_if_change_type(sdata, type);
272 	if (ret)
273 		return ret;
274 
275 	if (type == NL80211_IFTYPE_AP_VLAN && params->use_4addr == 0) {
276 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
277 		ieee80211_check_fast_rx_iface(sdata);
278 	} else if (type == NL80211_IFTYPE_STATION && params->use_4addr >= 0) {
279 		struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
280 
281 		if (params->use_4addr == ifmgd->use_4addr)
282 			return 0;
283 
284 		/* FIXME: no support for 4-addr MLO yet */
285 		if (ieee80211_vif_is_mld(&sdata->vif))
286 			return -EOPNOTSUPP;
287 
288 		sdata->u.mgd.use_4addr = params->use_4addr;
289 		if (!ifmgd->associated)
290 			return 0;
291 
292 		sta = sta_info_get(sdata, sdata->deflink.u.mgd.bssid);
293 		if (sta)
294 			drv_sta_set_4addr(local, sdata, &sta->sta,
295 					  params->use_4addr);
296 
297 		if (params->use_4addr)
298 			ieee80211_send_4addr_nullfunc(local, sdata);
299 	}
300 
301 	if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
302 		ret = ieee80211_set_mon_options(sdata, params);
303 		if (ret)
304 			return ret;
305 	}
306 
307 	return 0;
308 }
309 
310 static int ieee80211_start_p2p_device(struct wiphy *wiphy,
311 				      struct wireless_dev *wdev)
312 {
313 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
314 	int ret;
315 
316 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
317 
318 	ret = ieee80211_check_combinations(sdata, NULL, 0, 0, -1);
319 	if (ret < 0)
320 		return ret;
321 
322 	return ieee80211_do_open(wdev, true);
323 }
324 
325 static void ieee80211_stop_p2p_device(struct wiphy *wiphy,
326 				      struct wireless_dev *wdev)
327 {
328 	ieee80211_sdata_stop(IEEE80211_WDEV_TO_SUB_IF(wdev));
329 }
330 
331 static void ieee80211_nan_conf_free(struct cfg80211_nan_conf *conf)
332 {
333 	kfree(conf->extra_nan_attrs);
334 	kfree(conf->vendor_elems);
335 	memset(conf, 0, sizeof(*conf));
336 }
337 
338 static void ieee80211_stop_nan(struct wiphy *wiphy,
339 			       struct wireless_dev *wdev)
340 {
341 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
342 
343 	if (!sdata->u.nan.started)
344 		return;
345 
346 	drv_stop_nan(sdata->local, sdata);
347 	sdata->u.nan.started = false;
348 
349 	ieee80211_nan_conf_free(&sdata->u.nan.conf);
350 
351 	ieee80211_sdata_stop(sdata);
352 	ieee80211_recalc_idle(sdata->local);
353 }
354 
355 static int ieee80211_nan_conf_copy(struct cfg80211_nan_conf *dst,
356 				   struct cfg80211_nan_conf *src,
357 				   u32 changes)
358 {
359 	if (changes & CFG80211_NAN_CONF_CHANGED_PREF)
360 		dst->master_pref = src->master_pref;
361 
362 	if (changes & CFG80211_NAN_CONF_CHANGED_BANDS)
363 		dst->bands = src->bands;
364 
365 	if (changes & CFG80211_NAN_CONF_CHANGED_CONFIG) {
366 		dst->scan_period = src->scan_period;
367 		dst->scan_dwell_time = src->scan_dwell_time;
368 		dst->discovery_beacon_interval =
369 			src->discovery_beacon_interval;
370 		dst->enable_dw_notification = src->enable_dw_notification;
371 		memcpy(&dst->band_cfgs, &src->band_cfgs,
372 		       sizeof(dst->band_cfgs));
373 
374 		kfree(dst->extra_nan_attrs);
375 		dst->extra_nan_attrs = NULL;
376 		dst->extra_nan_attrs_len = 0;
377 
378 		kfree(dst->vendor_elems);
379 		dst->vendor_elems = NULL;
380 		dst->vendor_elems_len = 0;
381 
382 		if (is_zero_ether_addr(dst->cluster_id))
383 			ether_addr_copy(dst->cluster_id, src->cluster_id);
384 
385 		if (src->extra_nan_attrs && src->extra_nan_attrs_len) {
386 			dst->extra_nan_attrs = kmemdup(src->extra_nan_attrs,
387 						       src->extra_nan_attrs_len,
388 						       GFP_KERNEL);
389 			if (!dst->extra_nan_attrs)
390 				goto no_mem;
391 
392 			dst->extra_nan_attrs_len = src->extra_nan_attrs_len;
393 		}
394 
395 		if (src->vendor_elems && src->vendor_elems_len) {
396 			dst->vendor_elems = kmemdup(src->vendor_elems,
397 						    src->vendor_elems_len,
398 						    GFP_KERNEL);
399 			if (!dst->vendor_elems)
400 				goto no_mem;
401 
402 			dst->vendor_elems_len = src->vendor_elems_len;
403 		}
404 	}
405 
406 	return 0;
407 
408 no_mem:
409 	ieee80211_nan_conf_free(dst);
410 	return -ENOMEM;
411 }
412 
413 static int ieee80211_start_nan(struct wiphy *wiphy,
414 			       struct wireless_dev *wdev,
415 			       struct cfg80211_nan_conf *conf)
416 {
417 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
418 	int ret;
419 
420 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
421 
422 	if (sdata->u.nan.started)
423 		return -EALREADY;
424 
425 	ret = ieee80211_check_combinations(sdata, NULL, 0, 0, -1);
426 	if (ret < 0)
427 		return ret;
428 
429 	ret = ieee80211_do_open(wdev, true);
430 	if (ret)
431 		return ret;
432 
433 	ret = drv_start_nan(sdata->local, sdata, conf);
434 	if (ret) {
435 		ieee80211_sdata_stop(sdata);
436 		return ret;
437 	}
438 
439 	sdata->u.nan.started = true;
440 	ieee80211_recalc_idle(sdata->local);
441 
442 	ret = ieee80211_nan_conf_copy(&sdata->u.nan.conf, conf, 0xFFFFFFFF);
443 	if (ret) {
444 		ieee80211_stop_nan(wiphy, wdev);
445 		return ret;
446 	}
447 
448 	return 0;
449 }
450 
451 static int ieee80211_nan_change_conf(struct wiphy *wiphy,
452 				     struct wireless_dev *wdev,
453 				     struct cfg80211_nan_conf *conf,
454 				     u32 changes)
455 {
456 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
457 	struct cfg80211_nan_conf new_conf = {};
458 	int ret = 0;
459 
460 	if (sdata->vif.type != NL80211_IFTYPE_NAN)
461 		return -EOPNOTSUPP;
462 
463 	if (!ieee80211_sdata_running(sdata))
464 		return -ENETDOWN;
465 
466 	if (!changes)
467 		return 0;
468 
469 	/* First make a full copy of the previous configuration and then apply
470 	 * the changes. This might be a little wasteful, but it is simpler.
471 	 */
472 	ret = ieee80211_nan_conf_copy(&new_conf, &sdata->u.nan.conf,
473 				      0xFFFFFFFF);
474 	if (ret < 0)
475 		return ret;
476 
477 	ret = ieee80211_nan_conf_copy(&new_conf, conf, changes);
478 	if (ret < 0)
479 		return ret;
480 
481 	ret = drv_nan_change_conf(sdata->local, sdata, &new_conf, changes);
482 	if (ret) {
483 		ieee80211_nan_conf_free(&new_conf);
484 	} else {
485 		ieee80211_nan_conf_free(&sdata->u.nan.conf);
486 		sdata->u.nan.conf = new_conf;
487 	}
488 
489 	return ret;
490 }
491 
492 static int ieee80211_add_nan_func(struct wiphy *wiphy,
493 				  struct wireless_dev *wdev,
494 				  struct cfg80211_nan_func *nan_func)
495 {
496 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
497 	int ret;
498 
499 	if (sdata->vif.type != NL80211_IFTYPE_NAN)
500 		return -EOPNOTSUPP;
501 
502 	if (!ieee80211_sdata_running(sdata))
503 		return -ENETDOWN;
504 
505 	spin_lock_bh(&sdata->u.nan.func_lock);
506 
507 	ret = idr_alloc(&sdata->u.nan.function_inst_ids,
508 			nan_func, 1, sdata->local->hw.max_nan_de_entries + 1,
509 			GFP_ATOMIC);
510 	spin_unlock_bh(&sdata->u.nan.func_lock);
511 
512 	if (ret < 0)
513 		return ret;
514 
515 	nan_func->instance_id = ret;
516 
517 	WARN_ON(nan_func->instance_id == 0);
518 
519 	ret = drv_add_nan_func(sdata->local, sdata, nan_func);
520 	if (ret) {
521 		spin_lock_bh(&sdata->u.nan.func_lock);
522 		idr_remove(&sdata->u.nan.function_inst_ids,
523 			   nan_func->instance_id);
524 		spin_unlock_bh(&sdata->u.nan.func_lock);
525 	}
526 
527 	return ret;
528 }
529 
530 static struct cfg80211_nan_func *
531 ieee80211_find_nan_func_by_cookie(struct ieee80211_sub_if_data *sdata,
532 				  u64 cookie)
533 {
534 	struct cfg80211_nan_func *func;
535 	int id;
536 
537 	lockdep_assert_held(&sdata->u.nan.func_lock);
538 
539 	idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id) {
540 		if (func->cookie == cookie)
541 			return func;
542 	}
543 
544 	return NULL;
545 }
546 
547 static void ieee80211_del_nan_func(struct wiphy *wiphy,
548 				  struct wireless_dev *wdev, u64 cookie)
549 {
550 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
551 	struct cfg80211_nan_func *func;
552 	u8 instance_id = 0;
553 
554 	if (sdata->vif.type != NL80211_IFTYPE_NAN ||
555 	    !ieee80211_sdata_running(sdata))
556 		return;
557 
558 	spin_lock_bh(&sdata->u.nan.func_lock);
559 
560 	func = ieee80211_find_nan_func_by_cookie(sdata, cookie);
561 	if (func)
562 		instance_id = func->instance_id;
563 
564 	spin_unlock_bh(&sdata->u.nan.func_lock);
565 
566 	if (instance_id)
567 		drv_del_nan_func(sdata->local, sdata, instance_id);
568 }
569 
570 static int ieee80211_set_noack_map(struct wiphy *wiphy,
571 				  struct net_device *dev,
572 				  u16 noack_map)
573 {
574 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
575 
576 	sdata->noack_map = noack_map;
577 
578 	ieee80211_check_fast_xmit_iface(sdata);
579 
580 	return 0;
581 }
582 
583 static int ieee80211_set_tx(struct ieee80211_sub_if_data *sdata,
584 			    const u8 *mac_addr, u8 key_idx)
585 {
586 	struct ieee80211_local *local = sdata->local;
587 	struct ieee80211_key *key;
588 	struct sta_info *sta;
589 	int ret = -EINVAL;
590 
591 	if (!wiphy_ext_feature_isset(local->hw.wiphy,
592 				     NL80211_EXT_FEATURE_EXT_KEY_ID))
593 		return -EINVAL;
594 
595 	sta = sta_info_get_bss(sdata, mac_addr);
596 
597 	if (!sta)
598 		return -EINVAL;
599 
600 	if (sta->ptk_idx == key_idx)
601 		return 0;
602 
603 	key = wiphy_dereference(local->hw.wiphy, sta->ptk[key_idx]);
604 
605 	if (key && key->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)
606 		ret = ieee80211_set_tx_key(key);
607 
608 	return ret;
609 }
610 
611 static int ieee80211_add_key(struct wiphy *wiphy, struct wireless_dev *wdev,
612 			     int link_id, u8 key_idx, bool pairwise,
613 			     const u8 *mac_addr, struct key_params *params)
614 {
615 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
616 	struct ieee80211_link_data *link =
617 		ieee80211_link_or_deflink(sdata, link_id, false);
618 	struct ieee80211_local *local = sdata->local;
619 	struct sta_info *sta = NULL;
620 	struct ieee80211_key *key;
621 	int err;
622 
623 	lockdep_assert_wiphy(local->hw.wiphy);
624 
625 	if (!ieee80211_sdata_running(sdata))
626 		return -ENETDOWN;
627 
628 	if (IS_ERR(link))
629 		return PTR_ERR(link);
630 
631 	if (WARN_ON(pairwise && link_id >= 0))
632 		return -EINVAL;
633 
634 	if (pairwise && params->mode == NL80211_KEY_SET_TX)
635 		return ieee80211_set_tx(sdata, mac_addr, key_idx);
636 
637 	/* reject WEP and TKIP keys if WEP failed to initialize */
638 	switch (params->cipher) {
639 	case WLAN_CIPHER_SUITE_WEP40:
640 	case WLAN_CIPHER_SUITE_TKIP:
641 	case WLAN_CIPHER_SUITE_WEP104:
642 		if (link_id >= 0)
643 			return -EINVAL;
644 		if (WARN_ON_ONCE(fips_enabled))
645 			return -EINVAL;
646 		break;
647 	default:
648 		break;
649 	}
650 
651 	key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
652 				  params->key, params->seq_len, params->seq);
653 	if (IS_ERR(key))
654 		return PTR_ERR(key);
655 
656 	if (pairwise) {
657 		key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
658 		key->conf.link_id = -1;
659 	} else {
660 		key->conf.link_id = link->link_id;
661 	}
662 
663 	if (params->mode == NL80211_KEY_NO_TX)
664 		key->conf.flags |= IEEE80211_KEY_FLAG_NO_AUTO_TX;
665 
666 	if (mac_addr) {
667 		sta = sta_info_get_bss(sdata, mac_addr);
668 		/*
669 		 * The ASSOC test makes sure the driver is ready to
670 		 * receive the key. When wpa_supplicant has roamed
671 		 * using FT, it attempts to set the key before
672 		 * association has completed, this rejects that attempt
673 		 * so it will set the key again after association.
674 		 *
675 		 * With (re)association frame encryption enabled, cfg80211
676 		 * may deliver keys to mac80211 before the station has
677 		 * associated. In that case, accept the key if the station
678 		 * is an Enhanced Privacy Protection (EPP) peer.
679 		 * If (re)association frame encryption support is not present,
680 		 * cfg80211 will not allow key installation in non‑AP STA mode.
681 		 *
682 		 * TODO: accept the key if we have a station entry and
683 		 *	 add it to the device after the station associates.
684 		 */
685 		if (!sta || (!sta->sta.epp_peer &&
686 			     !test_sta_flag(sta, WLAN_STA_ASSOC))) {
687 			ieee80211_key_free_unused(key);
688 			return -ENOENT;
689 		}
690 	}
691 
692 	switch (sdata->vif.type) {
693 	case NL80211_IFTYPE_STATION:
694 		if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
695 			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
696 		break;
697 	case NL80211_IFTYPE_AP:
698 	case NL80211_IFTYPE_AP_VLAN:
699 		/* Keys without a station are used for TX only */
700 		if (sta && test_sta_flag(sta, WLAN_STA_MFP))
701 			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
702 		break;
703 	case NL80211_IFTYPE_ADHOC:
704 		/* no MFP (yet) */
705 		break;
706 	case NL80211_IFTYPE_MESH_POINT:
707 #ifdef CONFIG_MAC80211_MESH
708 		if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
709 			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
710 		break;
711 #endif
712 	case NL80211_IFTYPE_WDS:
713 	case NL80211_IFTYPE_MONITOR:
714 	case NL80211_IFTYPE_P2P_DEVICE:
715 	case NL80211_IFTYPE_NAN:
716 	case NL80211_IFTYPE_UNSPECIFIED:
717 	case NUM_NL80211_IFTYPES:
718 	case NL80211_IFTYPE_P2P_CLIENT:
719 	case NL80211_IFTYPE_P2P_GO:
720 	case NL80211_IFTYPE_OCB:
721 		/* shouldn't happen */
722 		WARN_ON_ONCE(1);
723 		break;
724 	}
725 
726 	err = ieee80211_key_link(key, link, sta);
727 	/* KRACK protection, shouldn't happen but just silently accept key */
728 	if (err == -EALREADY)
729 		err = 0;
730 
731 	return err;
732 }
733 
734 static struct ieee80211_key *
735 ieee80211_lookup_key(struct ieee80211_sub_if_data *sdata, int link_id,
736 		     u8 key_idx, bool pairwise, const u8 *mac_addr)
737 {
738 	struct ieee80211_local *local __maybe_unused = sdata->local;
739 	struct ieee80211_link_data *link = &sdata->deflink;
740 	struct ieee80211_key *key;
741 
742 	if (link_id >= 0) {
743 		link = sdata_dereference(sdata->link[link_id], sdata);
744 		if (!link)
745 			return NULL;
746 	}
747 
748 	if (mac_addr) {
749 		struct sta_info *sta;
750 		struct link_sta_info *link_sta;
751 
752 		sta = sta_info_get_bss(sdata, mac_addr);
753 		if (!sta)
754 			return NULL;
755 
756 		if (link_id >= 0) {
757 			link_sta = rcu_dereference_check(sta->link[link_id],
758 							 lockdep_is_held(&local->hw.wiphy->mtx));
759 			if (!link_sta)
760 				return NULL;
761 		} else {
762 			link_sta = &sta->deflink;
763 		}
764 
765 		if (pairwise && key_idx < NUM_DEFAULT_KEYS)
766 			return wiphy_dereference(local->hw.wiphy,
767 						 sta->ptk[key_idx]);
768 
769 		if (!pairwise &&
770 		    key_idx < NUM_DEFAULT_KEYS +
771 			      NUM_DEFAULT_MGMT_KEYS +
772 			      NUM_DEFAULT_BEACON_KEYS)
773 			return wiphy_dereference(local->hw.wiphy,
774 						 link_sta->gtk[key_idx]);
775 
776 		return NULL;
777 	}
778 
779 	if (pairwise && key_idx < NUM_DEFAULT_KEYS)
780 		return wiphy_dereference(local->hw.wiphy, sdata->keys[key_idx]);
781 
782 	key = wiphy_dereference(local->hw.wiphy, link->gtk[key_idx]);
783 	if (key)
784 		return key;
785 
786 	/* or maybe it was a WEP key */
787 	if (key_idx < NUM_DEFAULT_KEYS)
788 		return wiphy_dereference(local->hw.wiphy, sdata->keys[key_idx]);
789 
790 	return NULL;
791 }
792 
793 static int ieee80211_del_key(struct wiphy *wiphy, struct wireless_dev *wdev,
794 			     int link_id, u8 key_idx, bool pairwise,
795 			     const u8 *mac_addr)
796 {
797 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
798 	struct ieee80211_local *local = sdata->local;
799 	struct ieee80211_key *key;
800 
801 	lockdep_assert_wiphy(local->hw.wiphy);
802 
803 	key = ieee80211_lookup_key(sdata, link_id, key_idx, pairwise, mac_addr);
804 	if (!key)
805 		return -ENOENT;
806 
807 	ieee80211_key_free(key, sdata->vif.type == NL80211_IFTYPE_STATION);
808 
809 	return 0;
810 }
811 
812 static int ieee80211_get_key(struct wiphy *wiphy, struct wireless_dev *wdev,
813 			     int link_id, u8 key_idx, bool pairwise,
814 			     const u8 *mac_addr, void *cookie,
815 			     void (*callback)(void *cookie,
816 					      struct key_params *params))
817 {
818 	struct ieee80211_sub_if_data *sdata;
819 	u8 seq[6] = {0};
820 	struct key_params params;
821 	struct ieee80211_key *key;
822 	u64 pn64;
823 	u32 iv32;
824 	u16 iv16;
825 	int err = -ENOENT;
826 	struct ieee80211_key_seq kseq = {};
827 
828 	sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
829 
830 	rcu_read_lock();
831 
832 	key = ieee80211_lookup_key(sdata, link_id, key_idx, pairwise, mac_addr);
833 	if (!key)
834 		goto out;
835 
836 	memset(&params, 0, sizeof(params));
837 
838 	params.cipher = key->conf.cipher;
839 
840 	switch (key->conf.cipher) {
841 	case WLAN_CIPHER_SUITE_TKIP:
842 		pn64 = atomic64_read(&key->conf.tx_pn);
843 		iv32 = TKIP_PN_TO_IV32(pn64);
844 		iv16 = TKIP_PN_TO_IV16(pn64);
845 
846 		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
847 		    !(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
848 			drv_get_key_seq(sdata->local, key, &kseq);
849 			iv32 = kseq.tkip.iv32;
850 			iv16 = kseq.tkip.iv16;
851 		}
852 
853 		seq[0] = iv16 & 0xff;
854 		seq[1] = (iv16 >> 8) & 0xff;
855 		seq[2] = iv32 & 0xff;
856 		seq[3] = (iv32 >> 8) & 0xff;
857 		seq[4] = (iv32 >> 16) & 0xff;
858 		seq[5] = (iv32 >> 24) & 0xff;
859 		params.seq = seq;
860 		params.seq_len = 6;
861 		break;
862 	case WLAN_CIPHER_SUITE_CCMP:
863 	case WLAN_CIPHER_SUITE_CCMP_256:
864 	case WLAN_CIPHER_SUITE_AES_CMAC:
865 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
866 		BUILD_BUG_ON(offsetof(typeof(kseq), ccmp) !=
867 			     offsetof(typeof(kseq), aes_cmac));
868 		fallthrough;
869 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
870 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
871 		BUILD_BUG_ON(offsetof(typeof(kseq), ccmp) !=
872 			     offsetof(typeof(kseq), aes_gmac));
873 		fallthrough;
874 	case WLAN_CIPHER_SUITE_GCMP:
875 	case WLAN_CIPHER_SUITE_GCMP_256:
876 		BUILD_BUG_ON(offsetof(typeof(kseq), ccmp) !=
877 			     offsetof(typeof(kseq), gcmp));
878 
879 		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
880 		    !(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
881 			drv_get_key_seq(sdata->local, key, &kseq);
882 			memcpy(seq, kseq.ccmp.pn, 6);
883 		} else {
884 			pn64 = atomic64_read(&key->conf.tx_pn);
885 			seq[0] = pn64;
886 			seq[1] = pn64 >> 8;
887 			seq[2] = pn64 >> 16;
888 			seq[3] = pn64 >> 24;
889 			seq[4] = pn64 >> 32;
890 			seq[5] = pn64 >> 40;
891 		}
892 		params.seq = seq;
893 		params.seq_len = 6;
894 		break;
895 	default:
896 		if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
897 			break;
898 		if (WARN_ON(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
899 			break;
900 		drv_get_key_seq(sdata->local, key, &kseq);
901 		params.seq = kseq.hw.seq;
902 		params.seq_len = kseq.hw.seq_len;
903 		break;
904 	}
905 
906 	callback(cookie, &params);
907 	err = 0;
908 
909  out:
910 	rcu_read_unlock();
911 	return err;
912 }
913 
914 static int ieee80211_config_default_key(struct wiphy *wiphy,
915 					struct net_device *dev,
916 					int link_id, u8 key_idx, bool uni,
917 					bool multi)
918 {
919 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
920 	struct ieee80211_link_data *link =
921 		ieee80211_link_or_deflink(sdata, link_id, false);
922 
923 	if (IS_ERR(link))
924 		return PTR_ERR(link);
925 
926 	ieee80211_set_default_key(link, key_idx, uni, multi);
927 
928 	return 0;
929 }
930 
931 static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
932 					     struct wireless_dev *wdev,
933 					     int link_id, u8 key_idx)
934 {
935 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
936 	struct ieee80211_link_data *link =
937 		ieee80211_link_or_deflink(sdata, link_id, true);
938 
939 	if (IS_ERR(link))
940 		return PTR_ERR(link);
941 
942 	ieee80211_set_default_mgmt_key(link, key_idx);
943 
944 	return 0;
945 }
946 
947 static int ieee80211_config_default_beacon_key(struct wiphy *wiphy,
948 					       struct wireless_dev *wdev,
949 					       int link_id, u8 key_idx)
950 {
951 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
952 	struct ieee80211_link_data *link =
953 		ieee80211_link_or_deflink(sdata, link_id, true);
954 
955 	if (IS_ERR(link))
956 		return PTR_ERR(link);
957 
958 	ieee80211_set_default_beacon_key(link, key_idx);
959 
960 	return 0;
961 }
962 
963 void sta_set_rate_info_tx(struct sta_info *sta,
964 			  const struct ieee80211_tx_rate *rate,
965 			  struct rate_info *rinfo)
966 {
967 	rinfo->flags = 0;
968 	if (rate->flags & IEEE80211_TX_RC_MCS) {
969 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
970 		rinfo->mcs = rate->idx;
971 	} else if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
972 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
973 		rinfo->mcs = ieee80211_rate_get_vht_mcs(rate);
974 		rinfo->nss = ieee80211_rate_get_vht_nss(rate);
975 	} else {
976 		struct ieee80211_supported_band *sband;
977 
978 		sband = ieee80211_get_sband(sta->sdata);
979 		WARN_ON_ONCE(sband && !sband->bitrates);
980 		if (sband && sband->bitrates)
981 			rinfo->legacy = sband->bitrates[rate->idx].bitrate;
982 	}
983 	if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
984 		rinfo->bw = RATE_INFO_BW_40;
985 	else if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
986 		rinfo->bw = RATE_INFO_BW_80;
987 	else if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
988 		rinfo->bw = RATE_INFO_BW_160;
989 	else
990 		rinfo->bw = RATE_INFO_BW_20;
991 	if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
992 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
993 }
994 
995 static int ieee80211_dump_station(struct wiphy *wiphy, struct wireless_dev *wdev,
996 				  int idx, u8 *mac, struct station_info *sinfo)
997 {
998 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
999 	struct ieee80211_local *local = sdata->local;
1000 	struct sta_info *sta;
1001 	int ret = -ENOENT;
1002 
1003 	lockdep_assert_wiphy(local->hw.wiphy);
1004 
1005 	sta = sta_info_get_by_idx(sdata, idx);
1006 	if (sta) {
1007 		ret = 0;
1008 		memcpy(mac, sta->sta.addr, ETH_ALEN);
1009 		sta_set_sinfo(sta, sinfo, true);
1010 
1011 		/* Add accumulated removed link data to sinfo data for
1012 		 * consistency for MLO
1013 		 */
1014 		if (sinfo->valid_links)
1015 			sta_set_accumulated_removed_links_sinfo(sta, sinfo);
1016 
1017 	}
1018 
1019 	return ret;
1020 }
1021 
1022 static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
1023 				 int idx, struct survey_info *survey)
1024 {
1025 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1026 
1027 	return drv_get_survey(local, idx, survey);
1028 }
1029 
1030 static int ieee80211_get_station(struct wiphy *wiphy,
1031 				 struct wireless_dev *wdev,
1032 				 const u8 *mac, struct station_info *sinfo)
1033 {
1034 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
1035 	struct ieee80211_local *local = sdata->local;
1036 	struct sta_info *sta;
1037 	int ret = -ENOENT;
1038 
1039 	lockdep_assert_wiphy(local->hw.wiphy);
1040 
1041 	sta = sta_info_get_bss(sdata, mac);
1042 	if (sta) {
1043 		ret = 0;
1044 		sta_set_sinfo(sta, sinfo, true);
1045 
1046 		/* Add accumulated removed link data to sinfo data for
1047 		 * consistency for MLO
1048 		 */
1049 		if (sinfo->valid_links)
1050 			sta_set_accumulated_removed_links_sinfo(sta, sinfo);
1051 	}
1052 
1053 	return ret;
1054 }
1055 
1056 static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
1057 					 struct net_device *dev,
1058 					 struct cfg80211_chan_def *chandef)
1059 {
1060 	struct ieee80211_local *local = wiphy_priv(wiphy);
1061 	struct ieee80211_sub_if_data *sdata;
1062 	struct ieee80211_chan_req chanreq = { .oper = *chandef };
1063 	int ret;
1064 
1065 	lockdep_assert_wiphy(local->hw.wiphy);
1066 
1067 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1068 	if (!ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) {
1069 		if (cfg80211_chandef_identical(&local->monitor_chanreq.oper,
1070 					       &chanreq.oper))
1071 			return 0;
1072 
1073 		sdata = wiphy_dereference(wiphy, local->monitor_sdata);
1074 		if (!sdata)
1075 			goto done;
1076 	}
1077 
1078 	if (rcu_access_pointer(sdata->deflink.conf->chanctx_conf) &&
1079 	    cfg80211_chandef_identical(&sdata->vif.bss_conf.chanreq.oper,
1080 				       &chanreq.oper))
1081 		return 0;
1082 
1083 	ieee80211_link_release_channel(&sdata->deflink);
1084 	ret = ieee80211_link_use_channel(&sdata->deflink, &chanreq,
1085 					 IEEE80211_CHANCTX_SHARED);
1086 	if (ret)
1087 		return ret;
1088 done:
1089 	local->monitor_chanreq = chanreq;
1090 	return 0;
1091 }
1092 
1093 static int
1094 ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
1095 			 const u8 *resp, size_t resp_len,
1096 			 const struct ieee80211_csa_settings *csa,
1097 			 const struct ieee80211_color_change_settings *cca,
1098 			 struct ieee80211_link_data *link)
1099 {
1100 	struct probe_resp *new, *old;
1101 
1102 	if (!resp || !resp_len)
1103 		return 1;
1104 
1105 	old = sdata_dereference(link->u.ap.probe_resp, sdata);
1106 
1107 	new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
1108 	if (!new)
1109 		return -ENOMEM;
1110 
1111 	new->len = resp_len;
1112 	memcpy(new->data, resp, resp_len);
1113 
1114 	if (csa)
1115 		memcpy(new->cntdwn_counter_offsets, csa->counter_offsets_presp,
1116 		       csa->n_counter_offsets_presp *
1117 		       sizeof(new->cntdwn_counter_offsets[0]));
1118 	else if (cca)
1119 		new->cntdwn_counter_offsets[0] = cca->counter_offset_presp;
1120 
1121 	rcu_assign_pointer(link->u.ap.probe_resp, new);
1122 	if (old)
1123 		kfree_rcu(old, rcu_head);
1124 
1125 	return 0;
1126 }
1127 
1128 static int ieee80211_set_fils_discovery(struct ieee80211_sub_if_data *sdata,
1129 					struct cfg80211_fils_discovery *params,
1130 					struct ieee80211_link_data *link,
1131 					struct ieee80211_bss_conf *link_conf,
1132 					u64 *changed)
1133 {
1134 	struct fils_discovery_data *new, *old = NULL;
1135 	struct ieee80211_fils_discovery *fd;
1136 
1137 	if (!params->update)
1138 		return 0;
1139 
1140 	fd = &link_conf->fils_discovery;
1141 	fd->min_interval = params->min_interval;
1142 	fd->max_interval = params->max_interval;
1143 
1144 	old = sdata_dereference(link->u.ap.fils_discovery, sdata);
1145 	if (old)
1146 		kfree_rcu(old, rcu_head);
1147 
1148 	if (params->tmpl && params->tmpl_len) {
1149 		new = kzalloc(sizeof(*new) + params->tmpl_len, GFP_KERNEL);
1150 		if (!new)
1151 			return -ENOMEM;
1152 		new->len = params->tmpl_len;
1153 		memcpy(new->data, params->tmpl, params->tmpl_len);
1154 		rcu_assign_pointer(link->u.ap.fils_discovery, new);
1155 	} else {
1156 		RCU_INIT_POINTER(link->u.ap.fils_discovery, NULL);
1157 	}
1158 
1159 	*changed |= BSS_CHANGED_FILS_DISCOVERY;
1160 	return 0;
1161 }
1162 
1163 static int
1164 ieee80211_set_unsol_bcast_probe_resp(struct ieee80211_sub_if_data *sdata,
1165 				     struct cfg80211_unsol_bcast_probe_resp *params,
1166 				     struct ieee80211_link_data *link,
1167 				     struct ieee80211_bss_conf *link_conf,
1168 				     u64 *changed)
1169 {
1170 	struct unsol_bcast_probe_resp_data *new, *old = NULL;
1171 
1172 	if (!params->update)
1173 		return 0;
1174 
1175 	link_conf->unsol_bcast_probe_resp_interval = params->interval;
1176 
1177 	old = sdata_dereference(link->u.ap.unsol_bcast_probe_resp, sdata);
1178 	if (old)
1179 		kfree_rcu(old, rcu_head);
1180 
1181 	if (params->tmpl && params->tmpl_len) {
1182 		new = kzalloc(sizeof(*new) + params->tmpl_len, GFP_KERNEL);
1183 		if (!new)
1184 			return -ENOMEM;
1185 		new->len = params->tmpl_len;
1186 		memcpy(new->data, params->tmpl, params->tmpl_len);
1187 		rcu_assign_pointer(link->u.ap.unsol_bcast_probe_resp, new);
1188 	} else {
1189 		RCU_INIT_POINTER(link->u.ap.unsol_bcast_probe_resp, NULL);
1190 	}
1191 
1192 	*changed |= BSS_CHANGED_UNSOL_BCAST_PROBE_RESP;
1193 	return 0;
1194 }
1195 
1196 static int
1197 ieee80211_set_s1g_short_beacon(struct ieee80211_sub_if_data *sdata,
1198 			       struct ieee80211_link_data *link,
1199 			       struct cfg80211_s1g_short_beacon *params)
1200 {
1201 	struct s1g_short_beacon_data *new;
1202 	struct s1g_short_beacon_data *old =
1203 		sdata_dereference(link->u.ap.s1g_short_beacon, sdata);
1204 	size_t new_len =
1205 		sizeof(*new) + params->short_head_len + params->short_tail_len;
1206 
1207 	if (!params->update)
1208 		return 0;
1209 
1210 	if (!params->short_head)
1211 		return -EINVAL;
1212 
1213 	new = kzalloc(new_len, GFP_KERNEL);
1214 	if (!new)
1215 		return -ENOMEM;
1216 
1217 	/* Memory layout: | struct | head | tail | */
1218 	new->short_head = (u8 *)new + sizeof(*new);
1219 	new->short_head_len = params->short_head_len;
1220 	memcpy(new->short_head, params->short_head, params->short_head_len);
1221 
1222 	if (params->short_tail) {
1223 		new->short_tail = new->short_head + params->short_head_len;
1224 		new->short_tail_len = params->short_tail_len;
1225 		memcpy(new->short_tail, params->short_tail,
1226 		       params->short_tail_len);
1227 	}
1228 
1229 	rcu_assign_pointer(link->u.ap.s1g_short_beacon, new);
1230 
1231 	if (old)
1232 		kfree_rcu(old, rcu_head);
1233 
1234 	return 0;
1235 }
1236 
1237 static int ieee80211_set_ftm_responder_params(
1238 				struct ieee80211_sub_if_data *sdata,
1239 				const u8 *lci, size_t lci_len,
1240 				const u8 *civicloc, size_t civicloc_len,
1241 				struct ieee80211_bss_conf *link_conf)
1242 {
1243 	struct ieee80211_ftm_responder_params *new, *old;
1244 	u8 *pos;
1245 	int len;
1246 
1247 	if (!lci_len && !civicloc_len)
1248 		return 0;
1249 
1250 	old = link_conf->ftmr_params;
1251 	len = lci_len + civicloc_len;
1252 
1253 	new = kzalloc(sizeof(*new) + len, GFP_KERNEL);
1254 	if (!new)
1255 		return -ENOMEM;
1256 
1257 	pos = (u8 *)(new + 1);
1258 	if (lci_len) {
1259 		new->lci_len = lci_len;
1260 		new->lci = pos;
1261 		memcpy(pos, lci, lci_len);
1262 		pos += lci_len;
1263 	}
1264 
1265 	if (civicloc_len) {
1266 		new->civicloc_len = civicloc_len;
1267 		new->civicloc = pos;
1268 		memcpy(pos, civicloc, civicloc_len);
1269 		pos += civicloc_len;
1270 	}
1271 
1272 	link_conf->ftmr_params = new;
1273 	kfree(old);
1274 
1275 	return 0;
1276 }
1277 
1278 static int
1279 ieee80211_copy_mbssid_beacon(u8 *pos, struct cfg80211_mbssid_elems *dst,
1280 			     struct cfg80211_mbssid_elems *src)
1281 {
1282 	int i, offset = 0;
1283 
1284 	dst->cnt = src->cnt;
1285 	for (i = 0; i < src->cnt; i++) {
1286 		memcpy(pos + offset, src->elem[i].data, src->elem[i].len);
1287 		dst->elem[i].len = src->elem[i].len;
1288 		dst->elem[i].data = pos + offset;
1289 		offset += dst->elem[i].len;
1290 	}
1291 
1292 	return offset;
1293 }
1294 
1295 static int
1296 ieee80211_copy_rnr_beacon(u8 *pos, struct cfg80211_rnr_elems *dst,
1297 			  struct cfg80211_rnr_elems *src)
1298 {
1299 	int i, offset = 0;
1300 
1301 	dst->cnt = src->cnt;
1302 	for (i = 0; i < src->cnt; i++) {
1303 		memcpy(pos + offset, src->elem[i].data, src->elem[i].len);
1304 		dst->elem[i].len = src->elem[i].len;
1305 		dst->elem[i].data = pos + offset;
1306 		offset += dst->elem[i].len;
1307 	}
1308 
1309 	return offset;
1310 }
1311 
1312 static int
1313 ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
1314 			struct ieee80211_link_data *link,
1315 			struct cfg80211_beacon_data *params,
1316 			const struct ieee80211_csa_settings *csa,
1317 			const struct ieee80211_color_change_settings *cca,
1318 			u64 *changed)
1319 {
1320 	struct cfg80211_mbssid_elems *mbssid = NULL;
1321 	struct cfg80211_rnr_elems *rnr = NULL;
1322 	struct beacon_data *new, *old;
1323 	int new_head_len, new_tail_len;
1324 	int size, err;
1325 	u64 _changed = BSS_CHANGED_BEACON;
1326 	struct ieee80211_bss_conf *link_conf = link->conf;
1327 
1328 	old = sdata_dereference(link->u.ap.beacon, sdata);
1329 
1330 	/* Need to have a beacon head if we don't have one yet */
1331 	if (!params->head && !old)
1332 		return -EINVAL;
1333 
1334 	/* new or old head? */
1335 	if (params->head)
1336 		new_head_len = params->head_len;
1337 	else
1338 		new_head_len = old->head_len;
1339 
1340 	/* new or old tail? */
1341 	if (params->tail || !old)
1342 		/* params->tail_len will be zero for !params->tail */
1343 		new_tail_len = params->tail_len;
1344 	else
1345 		new_tail_len = old->tail_len;
1346 
1347 	size = sizeof(*new) + new_head_len + new_tail_len;
1348 
1349 	if (params->mbssid_ies) {
1350 		mbssid = params->mbssid_ies;
1351 		size += struct_size(new->mbssid_ies, elem, mbssid->cnt);
1352 		if (params->rnr_ies) {
1353 			rnr = params->rnr_ies;
1354 			size += struct_size(new->rnr_ies, elem, rnr->cnt);
1355 		}
1356 		size += ieee80211_get_mbssid_beacon_len(mbssid, rnr,
1357 							mbssid->cnt);
1358 	}
1359 
1360 	new = kzalloc(size, GFP_KERNEL);
1361 	if (!new)
1362 		return -ENOMEM;
1363 
1364 	/* start filling the new info now */
1365 
1366 	/*
1367 	 * pointers go into the block we allocated,
1368 	 * memory is | beacon_data | head | tail | mbssid_ies | rnr_ies
1369 	 */
1370 	new->head = ((u8 *) new) + sizeof(*new);
1371 	new->tail = new->head + new_head_len;
1372 	new->head_len = new_head_len;
1373 	new->tail_len = new_tail_len;
1374 	/* copy in optional mbssid_ies */
1375 	if (mbssid) {
1376 		u8 *pos = new->tail + new->tail_len;
1377 
1378 		new->mbssid_ies = (void *)pos;
1379 		pos += struct_size(new->mbssid_ies, elem, mbssid->cnt);
1380 		pos += ieee80211_copy_mbssid_beacon(pos, new->mbssid_ies,
1381 						    mbssid);
1382 		if (rnr) {
1383 			new->rnr_ies = (void *)pos;
1384 			pos += struct_size(new->rnr_ies, elem, rnr->cnt);
1385 			ieee80211_copy_rnr_beacon(pos, new->rnr_ies, rnr);
1386 		}
1387 		/* update bssid_indicator */
1388 		if (new->mbssid_ies->cnt && new->mbssid_ies->elem[0].len > 2)
1389 			link_conf->bssid_indicator =
1390 					*(new->mbssid_ies->elem[0].data + 2);
1391 		else
1392 			link_conf->bssid_indicator = 0;
1393 	}
1394 
1395 	if (csa) {
1396 		new->cntdwn_current_counter = csa->count;
1397 		memcpy(new->cntdwn_counter_offsets, csa->counter_offsets_beacon,
1398 		       csa->n_counter_offsets_beacon *
1399 		       sizeof(new->cntdwn_counter_offsets[0]));
1400 	} else if (cca) {
1401 		new->cntdwn_current_counter = cca->count;
1402 		new->cntdwn_counter_offsets[0] = cca->counter_offset_beacon;
1403 	}
1404 
1405 	/* copy in head */
1406 	if (params->head)
1407 		memcpy(new->head, params->head, new_head_len);
1408 	else
1409 		memcpy(new->head, old->head, new_head_len);
1410 
1411 	/* copy in optional tail */
1412 	if (params->tail)
1413 		memcpy(new->tail, params->tail, new_tail_len);
1414 	else
1415 		if (old)
1416 			memcpy(new->tail, old->tail, new_tail_len);
1417 
1418 	err = ieee80211_set_probe_resp(sdata, params->probe_resp,
1419 				       params->probe_resp_len, csa, cca, link);
1420 	if (err < 0) {
1421 		kfree(new);
1422 		return err;
1423 	}
1424 	if (err == 0)
1425 		_changed |= BSS_CHANGED_AP_PROBE_RESP;
1426 
1427 	if (params->ftm_responder != -1) {
1428 		link_conf->ftm_responder = params->ftm_responder;
1429 		err = ieee80211_set_ftm_responder_params(sdata,
1430 							 params->lci,
1431 							 params->lci_len,
1432 							 params->civicloc,
1433 							 params->civicloc_len,
1434 							 link_conf);
1435 
1436 		if (err < 0) {
1437 			kfree(new);
1438 			return err;
1439 		}
1440 
1441 		_changed |= BSS_CHANGED_FTM_RESPONDER;
1442 	}
1443 
1444 	rcu_assign_pointer(link->u.ap.beacon, new);
1445 	sdata->u.ap.active = true;
1446 
1447 	if (old)
1448 		kfree_rcu(old, rcu_head);
1449 
1450 	*changed |= _changed;
1451 	return 0;
1452 }
1453 
1454 static u8 ieee80211_num_beaconing_links(struct ieee80211_sub_if_data *sdata)
1455 {
1456 	struct ieee80211_link_data *link;
1457 	u8 link_id, num = 0;
1458 
1459 	if (sdata->vif.type != NL80211_IFTYPE_AP &&
1460 	    sdata->vif.type != NL80211_IFTYPE_P2P_GO)
1461 		return num;
1462 
1463 	/* non-MLO mode of operation also uses link_id 0 in sdata so it is
1464 	 * safe to directly proceed with the below loop
1465 	 */
1466 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
1467 		link = sdata_dereference(sdata->link[link_id], sdata);
1468 		if (!link)
1469 			continue;
1470 
1471 		if (sdata_dereference(link->u.ap.beacon, sdata))
1472 			num++;
1473 	}
1474 
1475 	return num;
1476 }
1477 
1478 static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
1479 			      struct cfg80211_ap_settings *params)
1480 {
1481 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1482 	struct ieee80211_local *local = sdata->local;
1483 	struct beacon_data *old;
1484 	struct ieee80211_sub_if_data *vlan;
1485 	u64 changed = BSS_CHANGED_BEACON_INT |
1486 		      BSS_CHANGED_BEACON_ENABLED |
1487 		      BSS_CHANGED_BEACON |
1488 		      BSS_CHANGED_P2P_PS |
1489 		      BSS_CHANGED_TXPOWER |
1490 		      BSS_CHANGED_TWT;
1491 	int i, err;
1492 	int prev_beacon_int;
1493 	unsigned int link_id = params->beacon.link_id;
1494 	struct ieee80211_link_data *link;
1495 	struct ieee80211_bss_conf *link_conf;
1496 	struct ieee80211_chan_req chanreq = { .oper = params->chandef };
1497 	u64 tsf;
1498 
1499 	lockdep_assert_wiphy(local->hw.wiphy);
1500 
1501 	link = sdata_dereference(sdata->link[link_id], sdata);
1502 	if (!link)
1503 		return -ENOLINK;
1504 
1505 	link_conf = link->conf;
1506 
1507 	old = sdata_dereference(link->u.ap.beacon, sdata);
1508 	if (old)
1509 		return -EALREADY;
1510 
1511 	link->smps_mode = IEEE80211_SMPS_OFF;
1512 
1513 	link->needed_rx_chains = sdata->local->rx_chains;
1514 
1515 	prev_beacon_int = link_conf->beacon_int;
1516 	link_conf->beacon_int = params->beacon_interval;
1517 
1518 	if (params->ht_cap)
1519 		link_conf->ht_ldpc =
1520 			params->ht_cap->cap_info &
1521 				cpu_to_le16(IEEE80211_HT_CAP_LDPC_CODING);
1522 
1523 	if (params->vht_cap) {
1524 		link_conf->vht_ldpc =
1525 			params->vht_cap->vht_cap_info &
1526 				cpu_to_le32(IEEE80211_VHT_CAP_RXLDPC);
1527 		link_conf->vht_su_beamformer =
1528 			params->vht_cap->vht_cap_info &
1529 				cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
1530 		link_conf->vht_su_beamformee =
1531 			params->vht_cap->vht_cap_info &
1532 				cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
1533 		link_conf->vht_mu_beamformer =
1534 			params->vht_cap->vht_cap_info &
1535 				cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
1536 		link_conf->vht_mu_beamformee =
1537 			params->vht_cap->vht_cap_info &
1538 				cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
1539 	}
1540 
1541 	if (params->he_cap && params->he_oper) {
1542 		link_conf->he_support = true;
1543 		link_conf->htc_trig_based_pkt_ext =
1544 			le32_get_bits(params->he_oper->he_oper_params,
1545 			      IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
1546 		link_conf->frame_time_rts_th =
1547 			le32_get_bits(params->he_oper->he_oper_params,
1548 			      IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
1549 		changed |= BSS_CHANGED_HE_OBSS_PD;
1550 
1551 		if (params->beacon.he_bss_color.enabled)
1552 			changed |= BSS_CHANGED_HE_BSS_COLOR;
1553 	}
1554 
1555 	if (params->he_cap) {
1556 		link_conf->he_ldpc =
1557 			params->he_cap->phy_cap_info[1] &
1558 				IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
1559 		link_conf->he_su_beamformer =
1560 			params->he_cap->phy_cap_info[3] &
1561 				IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER;
1562 		link_conf->he_su_beamformee =
1563 			params->he_cap->phy_cap_info[4] &
1564 				IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE;
1565 		link_conf->he_mu_beamformer =
1566 			params->he_cap->phy_cap_info[4] &
1567 				IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
1568 		link_conf->he_full_ul_mumimo =
1569 			params->he_cap->phy_cap_info[2] &
1570 				IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO;
1571 	}
1572 
1573 	if (params->eht_cap) {
1574 		if (!link_conf->he_support)
1575 			return -EOPNOTSUPP;
1576 
1577 		link_conf->eht_support = true;
1578 
1579 		link_conf->eht_su_beamformer =
1580 			params->eht_cap->fixed.phy_cap_info[0] &
1581 				IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER;
1582 		link_conf->eht_su_beamformee =
1583 			params->eht_cap->fixed.phy_cap_info[0] &
1584 				IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE;
1585 		link_conf->eht_mu_beamformer =
1586 			params->eht_cap->fixed.phy_cap_info[7] &
1587 				(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
1588 				 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
1589 				 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
1590 		link_conf->eht_80mhz_full_bw_ul_mumimo =
1591 			params->eht_cap->fixed.phy_cap_info[7] &
1592 				(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
1593 				 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
1594 				 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
1595 		link_conf->eht_disable_mcs15 =
1596 			u8_get_bits(params->eht_oper->params,
1597 				    IEEE80211_EHT_OPER_MCS15_DISABLE);
1598 	} else {
1599 		link_conf->eht_su_beamformer = false;
1600 		link_conf->eht_su_beamformee = false;
1601 		link_conf->eht_mu_beamformer = false;
1602 	}
1603 
1604 	if (params->uhr_oper) {
1605 		if (!link_conf->eht_support)
1606 			return -EOPNOTSUPP;
1607 
1608 		link_conf->uhr_support = true;
1609 	}
1610 
1611 	if (sdata->vif.type == NL80211_IFTYPE_AP &&
1612 	    params->mbssid_config.tx_wdev) {
1613 		err = ieee80211_set_ap_mbssid_options(sdata,
1614 						      &params->mbssid_config,
1615 						      link_conf);
1616 		if (err)
1617 			return err;
1618 	}
1619 
1620 	err = ieee80211_link_use_channel(link, &chanreq,
1621 					 IEEE80211_CHANCTX_SHARED);
1622 	if (!err)
1623 		ieee80211_link_copy_chanctx_to_vlans(link, false);
1624 	if (err) {
1625 		link_conf->beacon_int = prev_beacon_int;
1626 		return err;
1627 	}
1628 
1629 	/*
1630 	 * Apply control port protocol, this allows us to
1631 	 * not encrypt dynamic WEP control frames.
1632 	 */
1633 	sdata->control_port_protocol = params->crypto.control_port_ethertype;
1634 	sdata->control_port_no_encrypt = params->crypto.control_port_no_encrypt;
1635 	sdata->control_port_over_nl80211 =
1636 				params->crypto.control_port_over_nl80211;
1637 	sdata->control_port_no_preauth =
1638 				params->crypto.control_port_no_preauth;
1639 
1640 	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1641 		vlan->control_port_protocol =
1642 			params->crypto.control_port_ethertype;
1643 		vlan->control_port_no_encrypt =
1644 			params->crypto.control_port_no_encrypt;
1645 		vlan->control_port_over_nl80211 =
1646 			params->crypto.control_port_over_nl80211;
1647 		vlan->control_port_no_preauth =
1648 			params->crypto.control_port_no_preauth;
1649 	}
1650 
1651 	link_conf->dtim_period = params->dtim_period;
1652 	link_conf->enable_beacon = true;
1653 	link_conf->allow_p2p_go_ps = sdata->vif.p2p;
1654 	link_conf->twt_responder = params->twt_responder;
1655 	link_conf->he_obss_pd = params->he_obss_pd;
1656 	link_conf->he_bss_color = params->beacon.he_bss_color;
1657 	link_conf->s1g_long_beacon_period = params->s1g_long_beacon_period;
1658 	sdata->vif.cfg.s1g = params->chandef.chan->band == NL80211_BAND_S1GHZ;
1659 
1660 	sdata->vif.cfg.ssid_len = params->ssid_len;
1661 	if (params->ssid_len)
1662 		memcpy(sdata->vif.cfg.ssid, params->ssid,
1663 		       params->ssid_len);
1664 	link_conf->hidden_ssid =
1665 		(params->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE);
1666 
1667 	memset(&link_conf->p2p_noa_attr, 0,
1668 	       sizeof(link_conf->p2p_noa_attr));
1669 	link_conf->p2p_noa_attr.oppps_ctwindow =
1670 		params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
1671 	if (params->p2p_opp_ps)
1672 		link_conf->p2p_noa_attr.oppps_ctwindow |=
1673 					IEEE80211_P2P_OPPPS_ENABLE_BIT;
1674 
1675 	sdata->beacon_rate_set = false;
1676 	if (wiphy_ext_feature_isset(local->hw.wiphy,
1677 				    NL80211_EXT_FEATURE_BEACON_RATE_LEGACY)) {
1678 		for (i = 0; i < NUM_NL80211_BANDS; i++) {
1679 			sdata->beacon_rateidx_mask[i] =
1680 				params->beacon_rate.control[i].legacy;
1681 			if (sdata->beacon_rateidx_mask[i])
1682 				sdata->beacon_rate_set = true;
1683 		}
1684 	}
1685 
1686 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
1687 		link_conf->beacon_tx_rate = params->beacon_rate;
1688 
1689 	err = ieee80211_assign_beacon(sdata, link, &params->beacon, NULL, NULL,
1690 				      &changed);
1691 	if (err < 0)
1692 		goto error;
1693 
1694 	err = ieee80211_set_fils_discovery(sdata, &params->fils_discovery,
1695 					   link, link_conf, &changed);
1696 	if (err < 0)
1697 		goto error;
1698 
1699 	err = ieee80211_set_unsol_bcast_probe_resp(sdata,
1700 						   &params->unsol_bcast_probe_resp,
1701 						   link, link_conf, &changed);
1702 	if (err < 0)
1703 		goto error;
1704 
1705 	if (sdata->vif.cfg.s1g) {
1706 		err = ieee80211_set_s1g_short_beacon(sdata, link,
1707 						     &params->s1g_short_beacon);
1708 		if (err < 0)
1709 			goto error;
1710 	}
1711 
1712 	err = drv_start_ap(sdata->local, sdata, link_conf);
1713 	if (err) {
1714 		old = sdata_dereference(link->u.ap.beacon, sdata);
1715 
1716 		if (old)
1717 			kfree_rcu(old, rcu_head);
1718 		RCU_INIT_POINTER(link->u.ap.beacon, NULL);
1719 
1720 		if (ieee80211_num_beaconing_links(sdata) == 0)
1721 			sdata->u.ap.active = false;
1722 
1723 		goto error;
1724 	}
1725 
1726 	tsf = drv_get_tsf(local, sdata);
1727 	ieee80211_recalc_dtim(sdata, tsf);
1728 
1729 	if (link->u.ap.s1g_short_beacon)
1730 		ieee80211_recalc_sb_count(sdata, tsf);
1731 
1732 	ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_SSID);
1733 	ieee80211_link_info_change_notify(sdata, link, changed);
1734 
1735 	if (ieee80211_num_beaconing_links(sdata) <= 1)
1736 		netif_carrier_on(dev);
1737 
1738 	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1739 		netif_carrier_on(vlan->dev);
1740 
1741 	return 0;
1742 
1743 error:
1744 	ieee80211_link_release_channel(link);
1745 
1746 	return err;
1747 }
1748 
1749 static int ieee80211_change_beacon(struct wiphy *wiphy, struct net_device *dev,
1750 				   struct cfg80211_ap_update *params)
1751 
1752 {
1753 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1754 	struct ieee80211_link_data *link;
1755 	struct cfg80211_beacon_data *beacon = &params->beacon;
1756 	struct beacon_data *old;
1757 	int err;
1758 	struct ieee80211_bss_conf *link_conf;
1759 	u64 changed = 0;
1760 
1761 	lockdep_assert_wiphy(wiphy);
1762 
1763 	link = sdata_dereference(sdata->link[beacon->link_id], sdata);
1764 	if (!link)
1765 		return -ENOLINK;
1766 
1767 	link_conf = link->conf;
1768 
1769 	/* don't allow changing the beacon while a countdown is in place - offset
1770 	 * of channel switch counter may change
1771 	 */
1772 	if (link_conf->csa_active || link_conf->color_change_active)
1773 		return -EBUSY;
1774 
1775 	old = sdata_dereference(link->u.ap.beacon, sdata);
1776 	if (!old)
1777 		return -ENOENT;
1778 
1779 	err = ieee80211_assign_beacon(sdata, link, beacon, NULL, NULL,
1780 				      &changed);
1781 	if (err < 0)
1782 		return err;
1783 
1784 	err = ieee80211_set_fils_discovery(sdata, &params->fils_discovery,
1785 					   link, link_conf, &changed);
1786 	if (err < 0)
1787 		return err;
1788 
1789 	err = ieee80211_set_unsol_bcast_probe_resp(sdata,
1790 						   &params->unsol_bcast_probe_resp,
1791 						   link, link_conf, &changed);
1792 	if (err < 0)
1793 		return err;
1794 
1795 	if (link->u.ap.s1g_short_beacon) {
1796 		err = ieee80211_set_s1g_short_beacon(sdata, link,
1797 						     &params->s1g_short_beacon);
1798 		if (err < 0)
1799 			return err;
1800 	}
1801 
1802 	if (beacon->he_bss_color_valid &&
1803 	    beacon->he_bss_color.enabled != link_conf->he_bss_color.enabled) {
1804 		link_conf->he_bss_color.enabled = beacon->he_bss_color.enabled;
1805 		changed |= BSS_CHANGED_HE_BSS_COLOR;
1806 	}
1807 
1808 	ieee80211_link_info_change_notify(sdata, link, changed);
1809 	return 0;
1810 }
1811 
1812 static void ieee80211_free_next_beacon(struct ieee80211_link_data *link)
1813 {
1814 	if (!link->u.ap.next_beacon)
1815 		return;
1816 
1817 	kfree(link->u.ap.next_beacon->mbssid_ies);
1818 	kfree(link->u.ap.next_beacon->rnr_ies);
1819 	kfree(link->u.ap.next_beacon);
1820 	link->u.ap.next_beacon = NULL;
1821 }
1822 
1823 static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev,
1824 			     unsigned int link_id)
1825 {
1826 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1827 	struct ieee80211_sub_if_data *vlan;
1828 	struct ieee80211_local *local = sdata->local;
1829 	struct beacon_data *old_beacon;
1830 	struct probe_resp *old_probe_resp;
1831 	struct fils_discovery_data *old_fils_discovery;
1832 	struct unsol_bcast_probe_resp_data *old_unsol_bcast_probe_resp;
1833 	struct s1g_short_beacon_data *old_s1g_short_beacon;
1834 	struct cfg80211_chan_def chandef;
1835 	struct ieee80211_link_data *link =
1836 		sdata_dereference(sdata->link[link_id], sdata);
1837 	struct ieee80211_bss_conf *link_conf = link->conf;
1838 	LIST_HEAD(keys);
1839 
1840 	lockdep_assert_wiphy(local->hw.wiphy);
1841 
1842 	old_beacon = sdata_dereference(link->u.ap.beacon, sdata);
1843 	if (!old_beacon)
1844 		return -ENOENT;
1845 	old_probe_resp = sdata_dereference(link->u.ap.probe_resp,
1846 					   sdata);
1847 	old_fils_discovery = sdata_dereference(link->u.ap.fils_discovery,
1848 					       sdata);
1849 	old_unsol_bcast_probe_resp =
1850 		sdata_dereference(link->u.ap.unsol_bcast_probe_resp,
1851 				  sdata);
1852 	old_s1g_short_beacon =
1853 		sdata_dereference(link->u.ap.s1g_short_beacon, sdata);
1854 
1855 	/* abort any running channel switch or color change */
1856 	link_conf->csa_active = false;
1857 	link_conf->color_change_active = false;
1858 	ieee80211_vif_unblock_queues_csa(sdata);
1859 
1860 	ieee80211_free_next_beacon(link);
1861 
1862 	/* turn off carrier for this interface and dependent VLANs */
1863 	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1864 		netif_carrier_off(vlan->dev);
1865 
1866 	if (ieee80211_num_beaconing_links(sdata) <= 1) {
1867 		netif_carrier_off(dev);
1868 		sdata->u.ap.active = false;
1869 	}
1870 
1871 	/* remove beacon and probe response */
1872 	RCU_INIT_POINTER(link->u.ap.beacon, NULL);
1873 	RCU_INIT_POINTER(link->u.ap.probe_resp, NULL);
1874 	RCU_INIT_POINTER(link->u.ap.fils_discovery, NULL);
1875 	RCU_INIT_POINTER(link->u.ap.unsol_bcast_probe_resp, NULL);
1876 	RCU_INIT_POINTER(link->u.ap.s1g_short_beacon, NULL);
1877 	kfree_rcu(old_beacon, rcu_head);
1878 	if (old_probe_resp)
1879 		kfree_rcu(old_probe_resp, rcu_head);
1880 	if (old_fils_discovery)
1881 		kfree_rcu(old_fils_discovery, rcu_head);
1882 	if (old_unsol_bcast_probe_resp)
1883 		kfree_rcu(old_unsol_bcast_probe_resp, rcu_head);
1884 	if (old_s1g_short_beacon)
1885 		kfree_rcu(old_s1g_short_beacon, rcu_head);
1886 
1887 	kfree(link_conf->ftmr_params);
1888 	link_conf->ftmr_params = NULL;
1889 
1890 	link_conf->bssid_index = 0;
1891 	link_conf->nontransmitted = false;
1892 	link_conf->ema_ap = false;
1893 	link_conf->bssid_indicator = 0;
1894 	link_conf->fils_discovery.min_interval = 0;
1895 	link_conf->fils_discovery.max_interval = 0;
1896 	link_conf->unsol_bcast_probe_resp_interval = 0;
1897 
1898 	__sta_info_flush(sdata, true, link_id, NULL);
1899 
1900 	ieee80211_remove_link_keys(link, &keys);
1901 	if (!list_empty(&keys)) {
1902 		synchronize_net();
1903 		ieee80211_free_key_list(local, &keys);
1904 	}
1905 
1906 	ieee80211_stop_mbssid(sdata);
1907 	RCU_INIT_POINTER(link_conf->tx_bss_conf, NULL);
1908 
1909 	link_conf->enable_beacon = false;
1910 	sdata->beacon_rate_set = false;
1911 	sdata->vif.cfg.ssid_len = 0;
1912 	sdata->vif.cfg.s1g = false;
1913 	clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1914 	ieee80211_link_info_change_notify(sdata, link,
1915 					  BSS_CHANGED_BEACON_ENABLED);
1916 
1917 	if (sdata->wdev.links[link_id].cac_started) {
1918 		chandef = link_conf->chanreq.oper;
1919 		wiphy_hrtimer_work_cancel(wiphy, &link->dfs_cac_timer_work);
1920 		cfg80211_cac_event(sdata->dev, &chandef,
1921 				   NL80211_RADAR_CAC_ABORTED,
1922 				   GFP_KERNEL, link_id);
1923 	}
1924 
1925 	drv_stop_ap(sdata->local, sdata, link_conf);
1926 
1927 	/* free all potentially still buffered bcast frames */
1928 	local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps.bc_buf);
1929 	ieee80211_purge_tx_queue(&local->hw, &sdata->u.ap.ps.bc_buf);
1930 
1931 	ieee80211_link_copy_chanctx_to_vlans(link, true);
1932 	ieee80211_link_release_channel(link);
1933 
1934 	return 0;
1935 }
1936 
1937 static int sta_apply_auth_flags(struct ieee80211_local *local,
1938 				struct sta_info *sta,
1939 				u32 mask, u32 set)
1940 {
1941 	int ret;
1942 
1943 	if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1944 	    set & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1945 	    !test_sta_flag(sta, WLAN_STA_AUTH)) {
1946 		ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1947 		if (ret)
1948 			return ret;
1949 	}
1950 
1951 	if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1952 	    set & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1953 	    !test_sta_flag(sta, WLAN_STA_ASSOC)) {
1954 		/*
1955 		 * When peer becomes associated, init rate control as
1956 		 * well. Some drivers require rate control initialized
1957 		 * before drv_sta_state() is called.
1958 		 */
1959 		if (!test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1960 			rate_control_rate_init_all_links(sta);
1961 
1962 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1963 		if (ret)
1964 			return ret;
1965 	}
1966 
1967 	if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1968 		if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
1969 			ret = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
1970 		else if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1971 			ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1972 		else
1973 			ret = 0;
1974 		if (ret)
1975 			return ret;
1976 	}
1977 
1978 	if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1979 	    !(set & BIT(NL80211_STA_FLAG_ASSOCIATED)) &&
1980 	    test_sta_flag(sta, WLAN_STA_ASSOC)) {
1981 		ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1982 		if (ret)
1983 			return ret;
1984 	}
1985 
1986 	if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1987 	    !(set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) &&
1988 	    test_sta_flag(sta, WLAN_STA_AUTH)) {
1989 		ret = sta_info_move_state(sta, IEEE80211_STA_NONE);
1990 		if (ret)
1991 			return ret;
1992 	}
1993 
1994 	return 0;
1995 }
1996 
1997 static void sta_apply_mesh_params(struct ieee80211_local *local,
1998 				  struct sta_info *sta,
1999 				  struct station_parameters *params)
2000 {
2001 #ifdef CONFIG_MAC80211_MESH
2002 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2003 	u64 changed = 0;
2004 
2005 	if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE) {
2006 		switch (params->plink_state) {
2007 		case NL80211_PLINK_ESTAB:
2008 			if (sta->mesh->plink_state != NL80211_PLINK_ESTAB)
2009 				changed = mesh_plink_inc_estab_count(sdata);
2010 			sta->mesh->plink_state = params->plink_state;
2011 			sta->mesh->aid = params->peer_aid;
2012 
2013 			ieee80211_mps_sta_status_update(sta);
2014 			changed |= ieee80211_mps_set_sta_local_pm(sta,
2015 				      sdata->u.mesh.mshcfg.power_mode);
2016 
2017 			ewma_mesh_tx_rate_avg_init(&sta->mesh->tx_rate_avg);
2018 			/* init at low value */
2019 			ewma_mesh_tx_rate_avg_add(&sta->mesh->tx_rate_avg, 10);
2020 
2021 			break;
2022 		case NL80211_PLINK_LISTEN:
2023 		case NL80211_PLINK_BLOCKED:
2024 		case NL80211_PLINK_OPN_SNT:
2025 		case NL80211_PLINK_OPN_RCVD:
2026 		case NL80211_PLINK_CNF_RCVD:
2027 		case NL80211_PLINK_HOLDING:
2028 			if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
2029 				changed = mesh_plink_dec_estab_count(sdata);
2030 			sta->mesh->plink_state = params->plink_state;
2031 
2032 			ieee80211_mps_sta_status_update(sta);
2033 			changed |= ieee80211_mps_set_sta_local_pm(sta,
2034 					NL80211_MESH_POWER_UNKNOWN);
2035 			break;
2036 		default:
2037 			/*  nothing  */
2038 			break;
2039 		}
2040 	}
2041 
2042 	switch (params->plink_action) {
2043 	case NL80211_PLINK_ACTION_NO_ACTION:
2044 		/* nothing */
2045 		break;
2046 	case NL80211_PLINK_ACTION_OPEN:
2047 		changed |= mesh_plink_open(sta);
2048 		break;
2049 	case NL80211_PLINK_ACTION_BLOCK:
2050 		changed |= mesh_plink_block(sta);
2051 		break;
2052 	}
2053 
2054 	if (params->local_pm)
2055 		changed |= ieee80211_mps_set_sta_local_pm(sta,
2056 							  params->local_pm);
2057 
2058 	ieee80211_mbss_info_change_notify(sdata, changed);
2059 #endif
2060 }
2061 
2062 enum sta_link_apply_mode {
2063 	STA_LINK_MODE_NEW,
2064 	STA_LINK_MODE_STA_MODIFY,
2065 	STA_LINK_MODE_LINK_MODIFY,
2066 };
2067 
2068 static int sta_link_apply_parameters(struct ieee80211_local *local,
2069 				     struct sta_info *sta,
2070 				     enum sta_link_apply_mode mode,
2071 				     struct link_station_parameters *params)
2072 {
2073 	struct ieee80211_supported_band *sband;
2074 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2075 	u32 link_id = params->link_id < 0 ? 0 : params->link_id;
2076 	struct ieee80211_link_data *link =
2077 		sdata_dereference(sdata->link[link_id], sdata);
2078 	struct link_sta_info *link_sta =
2079 		rcu_dereference_protected(sta->link[link_id],
2080 					  lockdep_is_held(&local->hw.wiphy->mtx));
2081 	bool changes = params->link_mac ||
2082 		       params->txpwr_set ||
2083 		       params->supported_rates_len ||
2084 		       params->ht_capa ||
2085 		       params->vht_capa ||
2086 		       params->he_capa ||
2087 		       params->eht_capa ||
2088 		       params->uhr_capa ||
2089 		       params->s1g_capa ||
2090 		       params->opmode_notif_used;
2091 
2092 	switch (mode) {
2093 	case STA_LINK_MODE_NEW:
2094 		if (!params->link_mac)
2095 			return -EINVAL;
2096 		break;
2097 	case STA_LINK_MODE_LINK_MODIFY:
2098 		break;
2099 	case STA_LINK_MODE_STA_MODIFY:
2100 		if (params->link_id >= 0)
2101 			break;
2102 		if (!changes)
2103 			return 0;
2104 		break;
2105 	}
2106 
2107 	if (!link || !link_sta)
2108 		return -EINVAL;
2109 
2110 	sband = ieee80211_get_link_sband(link);
2111 	if (!sband)
2112 		return -EINVAL;
2113 
2114 	if (params->link_mac) {
2115 		if (mode == STA_LINK_MODE_NEW) {
2116 			memcpy(link_sta->addr, params->link_mac, ETH_ALEN);
2117 			memcpy(link_sta->pub->addr, params->link_mac, ETH_ALEN);
2118 		} else if (!ether_addr_equal(link_sta->addr,
2119 					     params->link_mac)) {
2120 			return -EINVAL;
2121 		}
2122 	}
2123 
2124 	if (params->txpwr_set) {
2125 		int ret;
2126 
2127 		link_sta->pub->txpwr.type = params->txpwr.type;
2128 		if (params->txpwr.type == NL80211_TX_POWER_LIMITED)
2129 			link_sta->pub->txpwr.power = params->txpwr.power;
2130 		ret = drv_sta_set_txpwr(local, sdata, sta);
2131 		if (ret)
2132 			return ret;
2133 	}
2134 
2135 	if (params->supported_rates &&
2136 	    params->supported_rates_len &&
2137 	    !ieee80211_parse_bitrates(sband, params->supported_rates,
2138 				      params->supported_rates_len,
2139 				      &link_sta->pub->supp_rates[sband->band]))
2140 		return -EINVAL;
2141 
2142 	if (params->ht_capa)
2143 		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
2144 						  params->ht_capa, link_sta);
2145 
2146 	/* VHT can override some HT caps such as the A-MSDU max length */
2147 	if (params->vht_capa)
2148 		ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2149 						    params->vht_capa, NULL,
2150 						    link_sta);
2151 
2152 	if (params->he_capa)
2153 		ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
2154 						  (void *)params->he_capa,
2155 						  params->he_capa_len,
2156 						  (void *)params->he_6ghz_capa,
2157 						  link_sta);
2158 
2159 	if (params->he_capa && params->eht_capa)
2160 		ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband,
2161 						    (u8 *)params->he_capa,
2162 						    params->he_capa_len,
2163 						    params->eht_capa,
2164 						    params->eht_capa_len,
2165 						    link_sta);
2166 
2167 	if (params->uhr_capa)
2168 		ieee80211_uhr_cap_ie_to_sta_uhr_cap(sdata, sband,
2169 						    params->uhr_capa,
2170 						    params->uhr_capa_len,
2171 						    link_sta);
2172 
2173 	if (params->s1g_capa)
2174 		ieee80211_s1g_cap_to_sta_s1g_cap(sdata, params->s1g_capa,
2175 						 link_sta);
2176 
2177 	ieee80211_sta_init_nss(link_sta);
2178 
2179 	if (params->opmode_notif_used) {
2180 		enum nl80211_chan_width width = link->conf->chanreq.oper.width;
2181 
2182 		switch (width) {
2183 		case NL80211_CHAN_WIDTH_20:
2184 		case NL80211_CHAN_WIDTH_40:
2185 		case NL80211_CHAN_WIDTH_80:
2186 		case NL80211_CHAN_WIDTH_160:
2187 		case NL80211_CHAN_WIDTH_80P80:
2188 		case NL80211_CHAN_WIDTH_320: /* not VHT, allowed for HE/EHT */
2189 			break;
2190 		default:
2191 			return -EINVAL;
2192 		}
2193 
2194 		/* returned value is only needed for rc update, but the
2195 		 * rc isn't initialized here yet, so ignore it
2196 		 */
2197 		__ieee80211_vht_handle_opmode(sdata, link_sta,
2198 					      params->opmode_notif,
2199 					      sband->band);
2200 	}
2201 
2202 	return 0;
2203 }
2204 
2205 static int sta_apply_parameters(struct ieee80211_local *local,
2206 				struct sta_info *sta,
2207 				struct station_parameters *params)
2208 {
2209 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2210 	u32 mask, set;
2211 	int ret = 0;
2212 
2213 	mask = params->sta_flags_mask;
2214 	set = params->sta_flags_set;
2215 
2216 	if (params->epp_peer)
2217 		sta->sta.epp_peer = true;
2218 
2219 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2220 		/*
2221 		 * In mesh mode, ASSOCIATED isn't part of the nl80211
2222 		 * API but must follow AUTHENTICATED for driver state.
2223 		 */
2224 		if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED))
2225 			mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2226 		if (set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
2227 			set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2228 	} else if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2229 		/*
2230 		 * TDLS -- everything follows authorized, but
2231 		 * only becoming authorized is possible, not
2232 		 * going back
2233 		 */
2234 		if (set & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
2235 			set |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2236 			       BIT(NL80211_STA_FLAG_ASSOCIATED);
2237 			mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2238 				BIT(NL80211_STA_FLAG_ASSOCIATED);
2239 		}
2240 	}
2241 
2242 	if (mask & BIT(NL80211_STA_FLAG_WME) &&
2243 	    local->hw.queues >= IEEE80211_NUM_ACS)
2244 		sta->sta.wme = set & BIT(NL80211_STA_FLAG_WME);
2245 
2246 	/* auth flags will be set later for TDLS,
2247 	 * and for unassociated stations that move to associated */
2248 	if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
2249 	    !((mask & BIT(NL80211_STA_FLAG_ASSOCIATED)) &&
2250 	      (set & BIT(NL80211_STA_FLAG_ASSOCIATED)))) {
2251 		ret = sta_apply_auth_flags(local, sta, mask, set);
2252 		if (ret)
2253 			return ret;
2254 	}
2255 
2256 	if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
2257 		if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
2258 			set_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
2259 		else
2260 			clear_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
2261 	}
2262 
2263 	if (mask & BIT(NL80211_STA_FLAG_MFP)) {
2264 		sta->sta.mfp = !!(set & BIT(NL80211_STA_FLAG_MFP));
2265 		if (set & BIT(NL80211_STA_FLAG_MFP))
2266 			set_sta_flag(sta, WLAN_STA_MFP);
2267 		else
2268 			clear_sta_flag(sta, WLAN_STA_MFP);
2269 	}
2270 
2271 	if (mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
2272 		if (set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2273 			set_sta_flag(sta, WLAN_STA_TDLS_PEER);
2274 		else
2275 			clear_sta_flag(sta, WLAN_STA_TDLS_PEER);
2276 	}
2277 
2278 	if (mask & BIT(NL80211_STA_FLAG_SPP_AMSDU))
2279 		sta->sta.spp_amsdu = set & BIT(NL80211_STA_FLAG_SPP_AMSDU);
2280 
2281 	/* mark TDLS channel switch support, if the AP allows it */
2282 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
2283 	    !sdata->deflink.u.mgd.tdls_chan_switch_prohibited &&
2284 	    params->ext_capab_len >= 4 &&
2285 	    params->ext_capab[3] & WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH)
2286 		set_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH);
2287 
2288 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
2289 	    !sdata->u.mgd.tdls_wider_bw_prohibited &&
2290 	    ieee80211_hw_check(&local->hw, TDLS_WIDER_BW) &&
2291 	    params->ext_capab_len >= 8 &&
2292 	    params->ext_capab[7] & WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED)
2293 		set_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW);
2294 
2295 	if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD) {
2296 		sta->sta.uapsd_queues = params->uapsd_queues;
2297 		sta->sta.max_sp = params->max_sp;
2298 	}
2299 
2300 	ieee80211_sta_set_max_amsdu_subframes(sta, params->ext_capab,
2301 					      params->ext_capab_len);
2302 
2303 	/*
2304 	 * cfg80211 validates this (1-2007) and allows setting the AID
2305 	 * only when creating a new station entry. For S1G APs, the current
2306 	 * implementation supports a maximum of 1600 AIDs.
2307 	 */
2308 	if (params->aid) {
2309 		if (sdata->vif.cfg.s1g &&
2310 		    params->aid > IEEE80211_MAX_SUPPORTED_S1G_AID)
2311 			return -EINVAL;
2312 
2313 		sta->sta.aid = params->aid;
2314 	}
2315 
2316 	/*
2317 	 * Some of the following updates would be racy if called on an
2318 	 * existing station, via ieee80211_change_station(). However,
2319 	 * all such changes are rejected by cfg80211 except for updates
2320 	 * changing the supported rates on an existing but not yet used
2321 	 * TDLS peer.
2322 	 */
2323 
2324 	if (params->listen_interval >= 0)
2325 		sta->listen_interval = params->listen_interval;
2326 
2327 	if (params->eml_cap_present)
2328 		sta->sta.eml_cap = params->eml_cap;
2329 
2330 	ret = sta_link_apply_parameters(local, sta, STA_LINK_MODE_STA_MODIFY,
2331 					&params->link_sta_params);
2332 	if (ret)
2333 		return ret;
2334 
2335 	if (params->support_p2p_ps >= 0)
2336 		sta->sta.support_p2p_ps = params->support_p2p_ps;
2337 
2338 	if (ieee80211_vif_is_mesh(&sdata->vif))
2339 		sta_apply_mesh_params(local, sta, params);
2340 
2341 	if (params->airtime_weight)
2342 		sta->airtime_weight = params->airtime_weight;
2343 
2344 	/* set the STA state after all sta info from usermode has been set */
2345 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) ||
2346 	    set & BIT(NL80211_STA_FLAG_ASSOCIATED)) {
2347 		ret = sta_apply_auth_flags(local, sta, mask, set);
2348 		if (ret)
2349 			return ret;
2350 	}
2351 
2352 	/* Mark the STA as MLO if MLD MAC address is available */
2353 	if (params->link_sta_params.mld_mac)
2354 		sta->sta.mlo = true;
2355 
2356 	return 0;
2357 }
2358 
2359 static int ieee80211_add_station(struct wiphy *wiphy, struct wireless_dev *wdev,
2360 				 const u8 *mac,
2361 				 struct station_parameters *params)
2362 {
2363 	struct ieee80211_local *local = wiphy_priv(wiphy);
2364 	struct sta_info *sta;
2365 	struct ieee80211_sub_if_data *sdata;
2366 	int err;
2367 
2368 	lockdep_assert_wiphy(local->hw.wiphy);
2369 
2370 	if (params->vlan) {
2371 		sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
2372 
2373 		if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2374 		    sdata->vif.type != NL80211_IFTYPE_AP)
2375 			return -EINVAL;
2376 	} else
2377 		sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2378 
2379 	if (ether_addr_equal(mac, sdata->vif.addr))
2380 		return -EINVAL;
2381 
2382 	if (!is_valid_ether_addr(mac))
2383 		return -EINVAL;
2384 
2385 	if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
2386 	    sdata->vif.type == NL80211_IFTYPE_STATION &&
2387 	    !sdata->u.mgd.associated)
2388 		return -EINVAL;
2389 
2390 	/*
2391 	 * If we have a link ID, it can be a non-MLO station on an AP MLD,
2392 	 * but we need to have a link_mac in that case as well, so use the
2393 	 * STA's MAC address in that case.
2394 	 */
2395 	if (params->link_sta_params.link_id >= 0)
2396 		sta = sta_info_alloc_with_link(sdata, mac,
2397 					       params->link_sta_params.link_id,
2398 					       params->link_sta_params.link_mac ?: mac,
2399 					       GFP_KERNEL);
2400 	else
2401 		sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
2402 
2403 	if (!sta)
2404 		return -ENOMEM;
2405 
2406 	if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2407 		sta->sta.tdls = true;
2408 
2409 	/* Though the mutex is not needed here (since the station is not
2410 	 * visible yet), sta_apply_parameters (and inner functions) require
2411 	 * the mutex due to other paths.
2412 	 */
2413 	err = sta_apply_parameters(local, sta, params);
2414 	if (err) {
2415 		sta_info_free(local, sta);
2416 		return err;
2417 	}
2418 
2419 	/*
2420 	 * for TDLS and for unassociated station, rate control should be
2421 	 * initialized only when rates are known and station is marked
2422 	 * authorized/associated
2423 	 */
2424 	if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
2425 	    test_sta_flag(sta, WLAN_STA_ASSOC))
2426 		rate_control_rate_init_all_links(sta);
2427 
2428 	return sta_info_insert(sta);
2429 }
2430 
2431 static int ieee80211_del_station(struct wiphy *wiphy, struct wireless_dev *wdev,
2432 				 struct station_del_parameters *params)
2433 {
2434 	struct ieee80211_sub_if_data *sdata;
2435 
2436 	sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2437 
2438 	if (params->mac)
2439 		return sta_info_destroy_addr_bss(sdata, params->mac);
2440 
2441 	sta_info_flush(sdata, params->link_id);
2442 	return 0;
2443 }
2444 
2445 static int ieee80211_change_station(struct wiphy *wiphy,
2446 				    struct wireless_dev *wdev, const u8 *mac,
2447 				    struct station_parameters *params)
2448 {
2449 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2450 	struct ieee80211_local *local = wiphy_priv(wiphy);
2451 	struct sta_info *sta;
2452 	struct ieee80211_sub_if_data *vlansdata;
2453 	enum cfg80211_station_type statype;
2454 	int err;
2455 
2456 	lockdep_assert_wiphy(local->hw.wiphy);
2457 
2458 	sta = sta_info_get_bss(sdata, mac);
2459 	if (!sta)
2460 		return -ENOENT;
2461 
2462 	switch (sdata->vif.type) {
2463 	case NL80211_IFTYPE_MESH_POINT:
2464 		if (sdata->u.mesh.user_mpm)
2465 			statype = CFG80211_STA_MESH_PEER_USER;
2466 		else
2467 			statype = CFG80211_STA_MESH_PEER_KERNEL;
2468 		break;
2469 	case NL80211_IFTYPE_ADHOC:
2470 		statype = CFG80211_STA_IBSS;
2471 		break;
2472 	case NL80211_IFTYPE_STATION:
2473 		if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2474 			statype = CFG80211_STA_AP_STA;
2475 			break;
2476 		}
2477 		if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2478 			statype = CFG80211_STA_TDLS_PEER_ACTIVE;
2479 		else
2480 			statype = CFG80211_STA_TDLS_PEER_SETUP;
2481 		break;
2482 	case NL80211_IFTYPE_AP:
2483 	case NL80211_IFTYPE_AP_VLAN:
2484 		if (test_sta_flag(sta, WLAN_STA_ASSOC))
2485 			statype = CFG80211_STA_AP_CLIENT;
2486 		else
2487 			statype = CFG80211_STA_AP_CLIENT_UNASSOC;
2488 		break;
2489 	default:
2490 		return -EOPNOTSUPP;
2491 	}
2492 
2493 	err = cfg80211_check_station_change(wiphy, params, statype);
2494 	if (err)
2495 		return err;
2496 
2497 	if (params->vlan && params->vlan != sta->sdata->dev) {
2498 		vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
2499 
2500 		if (params->vlan->ieee80211_ptr->use_4addr) {
2501 			if (vlansdata->u.vlan.sta)
2502 				return -EBUSY;
2503 
2504 			rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
2505 			__ieee80211_check_fast_rx_iface(vlansdata);
2506 			drv_sta_set_4addr(local, sta->sdata, &sta->sta, true);
2507 		}
2508 
2509 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2510 		    sta->sdata->u.vlan.sta)
2511 			RCU_INIT_POINTER(sta->sdata->u.vlan.sta, NULL);
2512 
2513 		if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2514 			ieee80211_vif_dec_num_mcast(sta->sdata);
2515 
2516 		sta->sdata = vlansdata;
2517 		ieee80211_check_fast_rx(sta);
2518 		ieee80211_check_fast_xmit(sta);
2519 
2520 		if (test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
2521 			ieee80211_vif_inc_num_mcast(sta->sdata);
2522 			cfg80211_send_layer2_update(sta->sdata->dev,
2523 						    sta->sta.addr);
2524 		}
2525 	}
2526 
2527 	err = sta_apply_parameters(local, sta, params);
2528 	if (err)
2529 		return err;
2530 
2531 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2532 	    params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
2533 		ieee80211_recalc_ps(local);
2534 		ieee80211_recalc_ps_vif(sdata);
2535 	}
2536 
2537 	return 0;
2538 }
2539 
2540 #ifdef CONFIG_MAC80211_MESH
2541 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
2542 			       const u8 *dst, const u8 *next_hop)
2543 {
2544 	struct ieee80211_sub_if_data *sdata;
2545 	struct mesh_path *mpath;
2546 	struct sta_info *sta;
2547 
2548 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2549 
2550 	rcu_read_lock();
2551 	sta = sta_info_get(sdata, next_hop);
2552 	if (!sta) {
2553 		rcu_read_unlock();
2554 		return -ENOENT;
2555 	}
2556 
2557 	mpath = mesh_path_add(sdata, dst);
2558 	if (IS_ERR(mpath)) {
2559 		rcu_read_unlock();
2560 		return PTR_ERR(mpath);
2561 	}
2562 
2563 	mesh_path_fix_nexthop(mpath, sta);
2564 
2565 	rcu_read_unlock();
2566 	return 0;
2567 }
2568 
2569 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
2570 			       const u8 *dst)
2571 {
2572 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2573 
2574 	if (dst)
2575 		return mesh_path_del(sdata, dst);
2576 
2577 	mesh_path_flush_by_iface(sdata);
2578 	return 0;
2579 }
2580 
2581 static int ieee80211_change_mpath(struct wiphy *wiphy, struct net_device *dev,
2582 				  const u8 *dst, const u8 *next_hop)
2583 {
2584 	struct ieee80211_sub_if_data *sdata;
2585 	struct mesh_path *mpath;
2586 	struct sta_info *sta;
2587 
2588 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2589 
2590 	rcu_read_lock();
2591 
2592 	sta = sta_info_get(sdata, next_hop);
2593 	if (!sta) {
2594 		rcu_read_unlock();
2595 		return -ENOENT;
2596 	}
2597 
2598 	mpath = mesh_path_lookup(sdata, dst);
2599 	if (!mpath) {
2600 		rcu_read_unlock();
2601 		return -ENOENT;
2602 	}
2603 
2604 	mesh_path_fix_nexthop(mpath, sta);
2605 
2606 	rcu_read_unlock();
2607 	return 0;
2608 }
2609 
2610 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
2611 			    struct mpath_info *pinfo)
2612 {
2613 	struct sta_info *next_hop_sta = rcu_dereference(mpath->next_hop);
2614 
2615 	if (next_hop_sta)
2616 		memcpy(next_hop, next_hop_sta->sta.addr, ETH_ALEN);
2617 	else
2618 		eth_zero_addr(next_hop);
2619 
2620 	memset(pinfo, 0, sizeof(*pinfo));
2621 
2622 	pinfo->generation = mpath->sdata->u.mesh.mesh_paths_generation;
2623 
2624 	pinfo->filled = MPATH_INFO_FRAME_QLEN |
2625 			MPATH_INFO_SN |
2626 			MPATH_INFO_METRIC |
2627 			MPATH_INFO_EXPTIME |
2628 			MPATH_INFO_DISCOVERY_TIMEOUT |
2629 			MPATH_INFO_DISCOVERY_RETRIES |
2630 			MPATH_INFO_FLAGS |
2631 			MPATH_INFO_HOP_COUNT |
2632 			MPATH_INFO_PATH_CHANGE;
2633 
2634 	pinfo->frame_qlen = mpath->frame_queue.qlen;
2635 	pinfo->sn = mpath->sn;
2636 	pinfo->metric = mpath->metric;
2637 	if (time_before(jiffies, mpath->exp_time))
2638 		pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
2639 	pinfo->discovery_timeout =
2640 			jiffies_to_msecs(mpath->discovery_timeout);
2641 	pinfo->discovery_retries = mpath->discovery_retries;
2642 	if (mpath->flags & MESH_PATH_ACTIVE)
2643 		pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
2644 	if (mpath->flags & MESH_PATH_RESOLVING)
2645 		pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
2646 	if (mpath->flags & MESH_PATH_SN_VALID)
2647 		pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
2648 	if (mpath->flags & MESH_PATH_FIXED)
2649 		pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
2650 	if (mpath->flags & MESH_PATH_RESOLVED)
2651 		pinfo->flags |= NL80211_MPATH_FLAG_RESOLVED;
2652 	pinfo->hop_count = mpath->hop_count;
2653 	pinfo->path_change_count = mpath->path_change_count;
2654 }
2655 
2656 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
2657 			       u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
2658 
2659 {
2660 	struct ieee80211_sub_if_data *sdata;
2661 	struct mesh_path *mpath;
2662 
2663 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2664 
2665 	rcu_read_lock();
2666 	mpath = mesh_path_lookup(sdata, dst);
2667 	if (!mpath) {
2668 		rcu_read_unlock();
2669 		return -ENOENT;
2670 	}
2671 	memcpy(dst, mpath->dst, ETH_ALEN);
2672 	mpath_set_pinfo(mpath, next_hop, pinfo);
2673 	rcu_read_unlock();
2674 	return 0;
2675 }
2676 
2677 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
2678 				int idx, u8 *dst, u8 *next_hop,
2679 				struct mpath_info *pinfo)
2680 {
2681 	struct ieee80211_sub_if_data *sdata;
2682 	struct mesh_path *mpath;
2683 
2684 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2685 
2686 	rcu_read_lock();
2687 	mpath = mesh_path_lookup_by_idx(sdata, idx);
2688 	if (!mpath) {
2689 		rcu_read_unlock();
2690 		return -ENOENT;
2691 	}
2692 	memcpy(dst, mpath->dst, ETH_ALEN);
2693 	mpath_set_pinfo(mpath, next_hop, pinfo);
2694 	rcu_read_unlock();
2695 	return 0;
2696 }
2697 
2698 static void mpp_set_pinfo(struct mesh_path *mpath, u8 *mpp,
2699 			  struct mpath_info *pinfo)
2700 {
2701 	memset(pinfo, 0, sizeof(*pinfo));
2702 	memcpy(mpp, mpath->mpp, ETH_ALEN);
2703 
2704 	pinfo->generation = mpath->sdata->u.mesh.mpp_paths_generation;
2705 }
2706 
2707 static int ieee80211_get_mpp(struct wiphy *wiphy, struct net_device *dev,
2708 			     u8 *dst, u8 *mpp, struct mpath_info *pinfo)
2709 
2710 {
2711 	struct ieee80211_sub_if_data *sdata;
2712 	struct mesh_path *mpath;
2713 
2714 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2715 
2716 	rcu_read_lock();
2717 	mpath = mpp_path_lookup(sdata, dst);
2718 	if (!mpath) {
2719 		rcu_read_unlock();
2720 		return -ENOENT;
2721 	}
2722 	memcpy(dst, mpath->dst, ETH_ALEN);
2723 	mpp_set_pinfo(mpath, mpp, pinfo);
2724 	rcu_read_unlock();
2725 	return 0;
2726 }
2727 
2728 static int ieee80211_dump_mpp(struct wiphy *wiphy, struct net_device *dev,
2729 			      int idx, u8 *dst, u8 *mpp,
2730 			      struct mpath_info *pinfo)
2731 {
2732 	struct ieee80211_sub_if_data *sdata;
2733 	struct mesh_path *mpath;
2734 
2735 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2736 
2737 	rcu_read_lock();
2738 	mpath = mpp_path_lookup_by_idx(sdata, idx);
2739 	if (!mpath) {
2740 		rcu_read_unlock();
2741 		return -ENOENT;
2742 	}
2743 	memcpy(dst, mpath->dst, ETH_ALEN);
2744 	mpp_set_pinfo(mpath, mpp, pinfo);
2745 	rcu_read_unlock();
2746 	return 0;
2747 }
2748 
2749 static int ieee80211_get_mesh_config(struct wiphy *wiphy,
2750 				struct net_device *dev,
2751 				struct mesh_config *conf)
2752 {
2753 	struct ieee80211_sub_if_data *sdata;
2754 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2755 
2756 	memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
2757 	return 0;
2758 }
2759 
2760 static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
2761 {
2762 	return (mask >> (parm-1)) & 0x1;
2763 }
2764 
2765 static int copy_mesh_setup(struct ieee80211_if_mesh *ifmsh,
2766 		const struct mesh_setup *setup)
2767 {
2768 	u8 *new_ie;
2769 	struct ieee80211_sub_if_data *sdata = container_of(ifmsh,
2770 					struct ieee80211_sub_if_data, u.mesh);
2771 	int i;
2772 
2773 	/* allocate information elements */
2774 	new_ie = NULL;
2775 
2776 	if (setup->ie_len) {
2777 		new_ie = kmemdup(setup->ie, setup->ie_len,
2778 				GFP_KERNEL);
2779 		if (!new_ie)
2780 			return -ENOMEM;
2781 	}
2782 	ifmsh->ie_len = setup->ie_len;
2783 	ifmsh->ie = new_ie;
2784 
2785 	/* now copy the rest of the setup parameters */
2786 	ifmsh->mesh_id_len = setup->mesh_id_len;
2787 	memcpy(ifmsh->mesh_id, setup->mesh_id, ifmsh->mesh_id_len);
2788 	ifmsh->mesh_sp_id = setup->sync_method;
2789 	ifmsh->mesh_pp_id = setup->path_sel_proto;
2790 	ifmsh->mesh_pm_id = setup->path_metric;
2791 	ifmsh->user_mpm = setup->user_mpm;
2792 	ifmsh->mesh_auth_id = setup->auth_id;
2793 	ifmsh->security = IEEE80211_MESH_SEC_NONE;
2794 	ifmsh->userspace_handles_dfs = setup->userspace_handles_dfs;
2795 	if (setup->is_authenticated)
2796 		ifmsh->security |= IEEE80211_MESH_SEC_AUTHED;
2797 	if (setup->is_secure)
2798 		ifmsh->security |= IEEE80211_MESH_SEC_SECURED;
2799 
2800 	/* mcast rate setting in Mesh Node */
2801 	memcpy(sdata->vif.bss_conf.mcast_rate, setup->mcast_rate,
2802 						sizeof(setup->mcast_rate));
2803 	sdata->vif.bss_conf.basic_rates = setup->basic_rates;
2804 
2805 	sdata->vif.bss_conf.beacon_int = setup->beacon_interval;
2806 	sdata->vif.bss_conf.dtim_period = setup->dtim_period;
2807 
2808 	sdata->beacon_rate_set = false;
2809 	if (wiphy_ext_feature_isset(sdata->local->hw.wiphy,
2810 				    NL80211_EXT_FEATURE_BEACON_RATE_LEGACY)) {
2811 		for (i = 0; i < NUM_NL80211_BANDS; i++) {
2812 			sdata->beacon_rateidx_mask[i] =
2813 				setup->beacon_rate.control[i].legacy;
2814 			if (sdata->beacon_rateidx_mask[i])
2815 				sdata->beacon_rate_set = true;
2816 		}
2817 	}
2818 
2819 	return 0;
2820 }
2821 
2822 static int ieee80211_update_mesh_config(struct wiphy *wiphy,
2823 					struct net_device *dev, u32 mask,
2824 					const struct mesh_config *nconf)
2825 {
2826 	struct mesh_config *conf;
2827 	struct ieee80211_sub_if_data *sdata;
2828 	struct ieee80211_if_mesh *ifmsh;
2829 
2830 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2831 	ifmsh = &sdata->u.mesh;
2832 
2833 	/* Set the config options which we are interested in setting */
2834 	conf = &(sdata->u.mesh.mshcfg);
2835 	if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
2836 		conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
2837 	if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
2838 		conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
2839 	if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
2840 		conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
2841 	if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
2842 		conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
2843 	if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
2844 		conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
2845 	if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
2846 		conf->dot11MeshTTL = nconf->dot11MeshTTL;
2847 	if (_chg_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask))
2848 		conf->element_ttl = nconf->element_ttl;
2849 	if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask)) {
2850 		if (ifmsh->user_mpm)
2851 			return -EBUSY;
2852 		conf->auto_open_plinks = nconf->auto_open_plinks;
2853 	}
2854 	if (_chg_mesh_attr(NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR, mask))
2855 		conf->dot11MeshNbrOffsetMaxNeighbor =
2856 			nconf->dot11MeshNbrOffsetMaxNeighbor;
2857 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
2858 		conf->dot11MeshHWMPmaxPREQretries =
2859 			nconf->dot11MeshHWMPmaxPREQretries;
2860 	if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
2861 		conf->path_refresh_time = nconf->path_refresh_time;
2862 	if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
2863 		conf->min_discovery_timeout = nconf->min_discovery_timeout;
2864 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
2865 		conf->dot11MeshHWMPactivePathTimeout =
2866 			nconf->dot11MeshHWMPactivePathTimeout;
2867 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
2868 		conf->dot11MeshHWMPpreqMinInterval =
2869 			nconf->dot11MeshHWMPpreqMinInterval;
2870 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL, mask))
2871 		conf->dot11MeshHWMPperrMinInterval =
2872 			nconf->dot11MeshHWMPperrMinInterval;
2873 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2874 			   mask))
2875 		conf->dot11MeshHWMPnetDiameterTraversalTime =
2876 			nconf->dot11MeshHWMPnetDiameterTraversalTime;
2877 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
2878 		conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
2879 		ieee80211_mesh_root_setup(ifmsh);
2880 	}
2881 	if (_chg_mesh_attr(NL80211_MESHCONF_GATE_ANNOUNCEMENTS, mask)) {
2882 		/* our current gate announcement implementation rides on root
2883 		 * announcements, so require this ifmsh to also be a root node
2884 		 * */
2885 		if (nconf->dot11MeshGateAnnouncementProtocol &&
2886 		    !(conf->dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)) {
2887 			conf->dot11MeshHWMPRootMode = IEEE80211_PROACTIVE_RANN;
2888 			ieee80211_mesh_root_setup(ifmsh);
2889 		}
2890 		conf->dot11MeshGateAnnouncementProtocol =
2891 			nconf->dot11MeshGateAnnouncementProtocol;
2892 	}
2893 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_RANN_INTERVAL, mask))
2894 		conf->dot11MeshHWMPRannInterval =
2895 			nconf->dot11MeshHWMPRannInterval;
2896 	if (_chg_mesh_attr(NL80211_MESHCONF_FORWARDING, mask))
2897 		conf->dot11MeshForwarding = nconf->dot11MeshForwarding;
2898 	if (_chg_mesh_attr(NL80211_MESHCONF_RSSI_THRESHOLD, mask)) {
2899 		/* our RSSI threshold implementation is supported only for
2900 		 * devices that report signal in dBm.
2901 		 */
2902 		if (!ieee80211_hw_check(&sdata->local->hw, SIGNAL_DBM))
2903 			return -EOPNOTSUPP;
2904 		conf->rssi_threshold = nconf->rssi_threshold;
2905 	}
2906 	if (_chg_mesh_attr(NL80211_MESHCONF_HT_OPMODE, mask)) {
2907 		conf->ht_opmode = nconf->ht_opmode;
2908 		sdata->vif.bss_conf.ht_operation_mode = nconf->ht_opmode;
2909 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
2910 						  BSS_CHANGED_HT);
2911 	}
2912 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT, mask))
2913 		conf->dot11MeshHWMPactivePathToRootTimeout =
2914 			nconf->dot11MeshHWMPactivePathToRootTimeout;
2915 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOT_INTERVAL, mask))
2916 		conf->dot11MeshHWMProotInterval =
2917 			nconf->dot11MeshHWMProotInterval;
2918 	if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL, mask))
2919 		conf->dot11MeshHWMPconfirmationInterval =
2920 			nconf->dot11MeshHWMPconfirmationInterval;
2921 	if (_chg_mesh_attr(NL80211_MESHCONF_POWER_MODE, mask)) {
2922 		conf->power_mode = nconf->power_mode;
2923 		ieee80211_mps_local_status_update(sdata);
2924 	}
2925 	if (_chg_mesh_attr(NL80211_MESHCONF_AWAKE_WINDOW, mask))
2926 		conf->dot11MeshAwakeWindowDuration =
2927 			nconf->dot11MeshAwakeWindowDuration;
2928 	if (_chg_mesh_attr(NL80211_MESHCONF_PLINK_TIMEOUT, mask))
2929 		conf->plink_timeout = nconf->plink_timeout;
2930 	if (_chg_mesh_attr(NL80211_MESHCONF_CONNECTED_TO_GATE, mask))
2931 		conf->dot11MeshConnectedToMeshGate =
2932 			nconf->dot11MeshConnectedToMeshGate;
2933 	if (_chg_mesh_attr(NL80211_MESHCONF_NOLEARN, mask))
2934 		conf->dot11MeshNolearn = nconf->dot11MeshNolearn;
2935 	if (_chg_mesh_attr(NL80211_MESHCONF_CONNECTED_TO_AS, mask))
2936 		conf->dot11MeshConnectedToAuthServer =
2937 			nconf->dot11MeshConnectedToAuthServer;
2938 	ieee80211_mbss_info_change_notify(sdata, BSS_CHANGED_BEACON);
2939 	return 0;
2940 }
2941 
2942 static int ieee80211_join_mesh(struct wiphy *wiphy, struct net_device *dev,
2943 			       const struct mesh_config *conf,
2944 			       const struct mesh_setup *setup)
2945 {
2946 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2947 	struct ieee80211_chan_req chanreq = { .oper = setup->chandef };
2948 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2949 	int err;
2950 
2951 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
2952 
2953 	memcpy(&ifmsh->mshcfg, conf, sizeof(struct mesh_config));
2954 	err = copy_mesh_setup(ifmsh, setup);
2955 	if (err)
2956 		return err;
2957 
2958 	sdata->control_port_over_nl80211 = setup->control_port_over_nl80211;
2959 
2960 	/* can mesh use other SMPS modes? */
2961 	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
2962 	sdata->deflink.needed_rx_chains = sdata->local->rx_chains;
2963 
2964 	err = ieee80211_link_use_channel(&sdata->deflink, &chanreq,
2965 					 IEEE80211_CHANCTX_SHARED);
2966 	if (err)
2967 		return err;
2968 
2969 	return ieee80211_start_mesh(sdata);
2970 }
2971 
2972 static int ieee80211_leave_mesh(struct wiphy *wiphy, struct net_device *dev)
2973 {
2974 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2975 
2976 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
2977 
2978 	ieee80211_stop_mesh(sdata);
2979 	ieee80211_link_release_channel(&sdata->deflink);
2980 	kfree(sdata->u.mesh.ie);
2981 
2982 	return 0;
2983 }
2984 #endif
2985 
2986 static int ieee80211_change_bss(struct wiphy *wiphy,
2987 				struct net_device *dev,
2988 				struct bss_parameters *params)
2989 {
2990 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2991 	struct ieee80211_link_data *link;
2992 	struct ieee80211_supported_band *sband;
2993 	u64 changed = 0;
2994 
2995 	link = ieee80211_link_or_deflink(sdata, params->link_id, true);
2996 	if (IS_ERR(link))
2997 		return PTR_ERR(link);
2998 
2999 	if (!sdata_dereference(link->u.ap.beacon, sdata))
3000 		return -ENOENT;
3001 
3002 	sband = ieee80211_get_link_sband(link);
3003 	if (!sband)
3004 		return -EINVAL;
3005 
3006 	if (params->basic_rates) {
3007 		if (!ieee80211_parse_bitrates(sband,
3008 					      params->basic_rates,
3009 					      params->basic_rates_len,
3010 					      &link->conf->basic_rates))
3011 			return -EINVAL;
3012 		changed |= BSS_CHANGED_BASIC_RATES;
3013 		ieee80211_check_rate_mask(link);
3014 	}
3015 
3016 	if (params->use_cts_prot >= 0) {
3017 		link->conf->use_cts_prot = params->use_cts_prot;
3018 		changed |= BSS_CHANGED_ERP_CTS_PROT;
3019 	}
3020 	if (params->use_short_preamble >= 0) {
3021 		link->conf->use_short_preamble = params->use_short_preamble;
3022 		changed |= BSS_CHANGED_ERP_PREAMBLE;
3023 	}
3024 
3025 	if (!link->conf->use_short_slot &&
3026 	    (sband->band == NL80211_BAND_5GHZ ||
3027 	     sband->band == NL80211_BAND_6GHZ)) {
3028 		link->conf->use_short_slot = true;
3029 		changed |= BSS_CHANGED_ERP_SLOT;
3030 	}
3031 
3032 	if (params->use_short_slot_time >= 0) {
3033 		link->conf->use_short_slot = params->use_short_slot_time;
3034 		changed |= BSS_CHANGED_ERP_SLOT;
3035 	}
3036 
3037 	if (params->ap_isolate >= 0) {
3038 		if (params->ap_isolate)
3039 			sdata->flags |= IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
3040 		else
3041 			sdata->flags &= ~IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
3042 		ieee80211_check_fast_rx_iface(sdata);
3043 	}
3044 
3045 	if (params->ht_opmode >= 0) {
3046 		link->conf->ht_operation_mode = (u16)params->ht_opmode;
3047 		changed |= BSS_CHANGED_HT;
3048 	}
3049 
3050 	if (params->p2p_ctwindow >= 0) {
3051 		link->conf->p2p_noa_attr.oppps_ctwindow &=
3052 					~IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
3053 		link->conf->p2p_noa_attr.oppps_ctwindow |=
3054 			params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
3055 		changed |= BSS_CHANGED_P2P_PS;
3056 	}
3057 
3058 	if (params->p2p_opp_ps > 0) {
3059 		link->conf->p2p_noa_attr.oppps_ctwindow |=
3060 					IEEE80211_P2P_OPPPS_ENABLE_BIT;
3061 		changed |= BSS_CHANGED_P2P_PS;
3062 	} else if (params->p2p_opp_ps == 0) {
3063 		link->conf->p2p_noa_attr.oppps_ctwindow &=
3064 					~IEEE80211_P2P_OPPPS_ENABLE_BIT;
3065 		changed |= BSS_CHANGED_P2P_PS;
3066 	}
3067 
3068 	ieee80211_link_info_change_notify(sdata, link, changed);
3069 
3070 	return 0;
3071 }
3072 
3073 static int ieee80211_set_txq_params(struct wiphy *wiphy,
3074 				    struct net_device *dev,
3075 				    struct ieee80211_txq_params *params)
3076 {
3077 	struct ieee80211_local *local = wiphy_priv(wiphy);
3078 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3079 	struct ieee80211_link_data *link =
3080 		ieee80211_link_or_deflink(sdata, params->link_id, true);
3081 	struct ieee80211_tx_queue_params p;
3082 
3083 	if (!local->ops->conf_tx)
3084 		return -EOPNOTSUPP;
3085 
3086 	if (local->hw.queues < IEEE80211_NUM_ACS)
3087 		return -EOPNOTSUPP;
3088 
3089 	if (IS_ERR(link))
3090 		return PTR_ERR(link);
3091 
3092 	memset(&p, 0, sizeof(p));
3093 	p.aifs = params->aifs;
3094 	p.cw_max = params->cwmax;
3095 	p.cw_min = params->cwmin;
3096 	p.txop = params->txop;
3097 
3098 	/*
3099 	 * Setting tx queue params disables u-apsd because it's only
3100 	 * called in master mode.
3101 	 */
3102 	p.uapsd = false;
3103 
3104 	ieee80211_regulatory_limit_wmm_params(sdata, &p, params->ac);
3105 
3106 	link->tx_conf[params->ac] = p;
3107 	if (drv_conf_tx(local, link, params->ac, &p)) {
3108 		wiphy_debug(local->hw.wiphy,
3109 			    "failed to set TX queue parameters for AC %d\n",
3110 			    params->ac);
3111 		return -EINVAL;
3112 	}
3113 
3114 	ieee80211_link_info_change_notify(sdata, link,
3115 					  BSS_CHANGED_QOS);
3116 
3117 	return 0;
3118 }
3119 
3120 #ifdef CONFIG_PM
3121 static int ieee80211_suspend(struct wiphy *wiphy,
3122 			     struct cfg80211_wowlan *wowlan)
3123 {
3124 	return __ieee80211_suspend(wiphy_priv(wiphy), wowlan);
3125 }
3126 
3127 static int ieee80211_resume(struct wiphy *wiphy)
3128 {
3129 	return __ieee80211_resume(wiphy_priv(wiphy));
3130 }
3131 #else
3132 #define ieee80211_suspend NULL
3133 #define ieee80211_resume NULL
3134 #endif
3135 
3136 static int ieee80211_scan(struct wiphy *wiphy,
3137 			  struct cfg80211_scan_request *req)
3138 {
3139 	struct ieee80211_sub_if_data *sdata;
3140 	struct ieee80211_link_data *link;
3141 	struct ieee80211_channel *chan;
3142 	int radio_idx;
3143 
3144 	sdata = IEEE80211_WDEV_TO_SUB_IF(req->wdev);
3145 
3146 	switch (ieee80211_vif_type_p2p(&sdata->vif)) {
3147 	case NL80211_IFTYPE_STATION:
3148 	case NL80211_IFTYPE_ADHOC:
3149 	case NL80211_IFTYPE_MESH_POINT:
3150 	case NL80211_IFTYPE_P2P_CLIENT:
3151 	case NL80211_IFTYPE_P2P_DEVICE:
3152 		break;
3153 	case NL80211_IFTYPE_P2P_GO:
3154 		if (sdata->local->ops->hw_scan)
3155 			break;
3156 		/*
3157 		 * FIXME: implement NoA while scanning in software,
3158 		 * for now fall through to allow scanning only when
3159 		 * beaconing hasn't been configured yet
3160 		 */
3161 		fallthrough;
3162 	case NL80211_IFTYPE_AP:
3163 		/*
3164 		 * If the scan has been forced (and the driver supports
3165 		 * forcing), don't care about being beaconing already.
3166 		 * This will create problems to the attached stations (e.g. all
3167 		 * the frames sent while scanning on other channel will be
3168 		 * lost)
3169 		 */
3170 		for_each_link_data(sdata, link) {
3171 			/* if the link is not beaconing, ignore it */
3172 			if (!sdata_dereference(link->u.ap.beacon, sdata))
3173 				continue;
3174 
3175 			chan = link->conf->chanreq.oper.chan;
3176 			radio_idx = cfg80211_get_radio_idx_by_chan(wiphy, chan);
3177 
3178 			if (ieee80211_is_radio_idx_in_scan_req(wiphy, req,
3179 							       radio_idx) &&
3180 			    (!(wiphy->features & NL80211_FEATURE_AP_SCAN) ||
3181 			     !(req->flags & NL80211_SCAN_FLAG_AP)))
3182 				return -EOPNOTSUPP;
3183 		}
3184 		break;
3185 	case NL80211_IFTYPE_NAN:
3186 	default:
3187 		return -EOPNOTSUPP;
3188 	}
3189 
3190 	return ieee80211_request_scan(sdata, req);
3191 }
3192 
3193 static void ieee80211_abort_scan(struct wiphy *wiphy, struct wireless_dev *wdev)
3194 {
3195 	ieee80211_scan_cancel(wiphy_priv(wiphy));
3196 }
3197 
3198 static int
3199 ieee80211_sched_scan_start(struct wiphy *wiphy,
3200 			   struct net_device *dev,
3201 			   struct cfg80211_sched_scan_request *req)
3202 {
3203 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3204 
3205 	if (!sdata->local->ops->sched_scan_start)
3206 		return -EOPNOTSUPP;
3207 
3208 	return ieee80211_request_sched_scan_start(sdata, req);
3209 }
3210 
3211 static int
3212 ieee80211_sched_scan_stop(struct wiphy *wiphy, struct net_device *dev,
3213 			  u64 reqid)
3214 {
3215 	struct ieee80211_local *local = wiphy_priv(wiphy);
3216 
3217 	if (!local->ops->sched_scan_stop)
3218 		return -EOPNOTSUPP;
3219 
3220 	return ieee80211_request_sched_scan_stop(local);
3221 }
3222 
3223 static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
3224 			  struct cfg80211_auth_request *req)
3225 {
3226 	return ieee80211_mgd_auth(IEEE80211_DEV_TO_SUB_IF(dev), req);
3227 }
3228 
3229 static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
3230 			   struct cfg80211_assoc_request *req)
3231 {
3232 	return ieee80211_mgd_assoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
3233 }
3234 
3235 static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
3236 			    struct cfg80211_deauth_request *req)
3237 {
3238 	return ieee80211_mgd_deauth(IEEE80211_DEV_TO_SUB_IF(dev), req);
3239 }
3240 
3241 static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
3242 			      struct cfg80211_disassoc_request *req)
3243 {
3244 	return ieee80211_mgd_disassoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
3245 }
3246 
3247 static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
3248 			       struct cfg80211_ibss_params *params)
3249 {
3250 	return ieee80211_ibss_join(IEEE80211_DEV_TO_SUB_IF(dev), params);
3251 }
3252 
3253 static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
3254 {
3255 	return ieee80211_ibss_leave(IEEE80211_DEV_TO_SUB_IF(dev));
3256 }
3257 
3258 static int ieee80211_join_ocb(struct wiphy *wiphy, struct net_device *dev,
3259 			      struct ocb_setup *setup)
3260 {
3261 	return ieee80211_ocb_join(IEEE80211_DEV_TO_SUB_IF(dev), setup);
3262 }
3263 
3264 static int ieee80211_leave_ocb(struct wiphy *wiphy, struct net_device *dev)
3265 {
3266 	return ieee80211_ocb_leave(IEEE80211_DEV_TO_SUB_IF(dev));
3267 }
3268 
3269 static int ieee80211_set_mcast_rate(struct wiphy *wiphy, struct net_device *dev,
3270 				    int rate[NUM_NL80211_BANDS])
3271 {
3272 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3273 
3274 	memcpy(sdata->vif.bss_conf.mcast_rate, rate,
3275 	       sizeof(int) * NUM_NL80211_BANDS);
3276 
3277 	if (ieee80211_sdata_running(sdata))
3278 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
3279 						  BSS_CHANGED_MCAST_RATE);
3280 
3281 	return 0;
3282 }
3283 
3284 static int ieee80211_set_wiphy_params(struct wiphy *wiphy, int radio_idx,
3285 				      u32 changed)
3286 {
3287 	struct ieee80211_local *local = wiphy_priv(wiphy);
3288 	int err;
3289 
3290 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
3291 		ieee80211_check_fast_xmit_all(local);
3292 
3293 		err = drv_set_frag_threshold(local, radio_idx,
3294 					     wiphy->frag_threshold);
3295 
3296 		if (err) {
3297 			ieee80211_check_fast_xmit_all(local);
3298 			return err;
3299 		}
3300 	}
3301 
3302 	if ((changed & WIPHY_PARAM_COVERAGE_CLASS) ||
3303 	    (changed & WIPHY_PARAM_DYN_ACK)) {
3304 		s16 coverage_class;
3305 
3306 		coverage_class = changed & WIPHY_PARAM_COVERAGE_CLASS ?
3307 					wiphy->coverage_class : -1;
3308 		err = drv_set_coverage_class(local, radio_idx,
3309 					     coverage_class);
3310 
3311 		if (err)
3312 			return err;
3313 	}
3314 
3315 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
3316 		u32 rts_threshold;
3317 
3318 		if ((radio_idx == -1) || (radio_idx >= wiphy->n_radio))
3319 			rts_threshold = wiphy->rts_threshold;
3320 		else
3321 			rts_threshold =
3322 				wiphy->radio_cfg[radio_idx].rts_threshold;
3323 
3324 		err = drv_set_rts_threshold(local, radio_idx, rts_threshold);
3325 
3326 		if (err)
3327 			return err;
3328 	}
3329 
3330 	if (changed & WIPHY_PARAM_RETRY_SHORT) {
3331 		if (wiphy->retry_short > IEEE80211_MAX_TX_RETRY)
3332 			return -EINVAL;
3333 		local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
3334 	}
3335 	if (changed & WIPHY_PARAM_RETRY_LONG) {
3336 		if (wiphy->retry_long > IEEE80211_MAX_TX_RETRY)
3337 			return -EINVAL;
3338 		local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
3339 	}
3340 	if (changed &
3341 	    (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
3342 		ieee80211_hw_config(local, radio_idx,
3343 				    IEEE80211_CONF_CHANGE_RETRY_LIMITS);
3344 
3345 	if (changed & (WIPHY_PARAM_TXQ_LIMIT |
3346 		       WIPHY_PARAM_TXQ_MEMORY_LIMIT |
3347 		       WIPHY_PARAM_TXQ_QUANTUM))
3348 		ieee80211_txq_set_params(local, radio_idx);
3349 
3350 	return 0;
3351 }
3352 
3353 static int ieee80211_set_tx_power(struct wiphy *wiphy,
3354 				  struct wireless_dev *wdev, int radio_idx,
3355 				  enum nl80211_tx_power_setting type, int mbm)
3356 {
3357 	struct ieee80211_local *local = wiphy_priv(wiphy);
3358 	struct ieee80211_sub_if_data *sdata;
3359 	enum nl80211_tx_power_setting txp_type = type;
3360 	bool update_txp_type = false;
3361 	bool has_monitor = false;
3362 	int user_power_level;
3363 	int old_power = local->user_power_level;
3364 
3365 	lockdep_assert_wiphy(local->hw.wiphy);
3366 
3367 	switch (type) {
3368 	case NL80211_TX_POWER_AUTOMATIC:
3369 		user_power_level = IEEE80211_UNSET_POWER_LEVEL;
3370 		txp_type = NL80211_TX_POWER_LIMITED;
3371 		break;
3372 	case NL80211_TX_POWER_LIMITED:
3373 	case NL80211_TX_POWER_FIXED:
3374 		if (mbm < 0 || (mbm % 100))
3375 			return -EOPNOTSUPP;
3376 		user_power_level = MBM_TO_DBM(mbm);
3377 		break;
3378 	default:
3379 		return -EINVAL;
3380 	}
3381 
3382 	if (wdev) {
3383 		sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3384 
3385 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
3386 		    !ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) {
3387 			if (!ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
3388 				return -EOPNOTSUPP;
3389 
3390 			sdata = wiphy_dereference(local->hw.wiphy,
3391 						  local->monitor_sdata);
3392 			if (!sdata)
3393 				return -EOPNOTSUPP;
3394 		}
3395 
3396 		for (int link_id = 0;
3397 		     link_id < ARRAY_SIZE(sdata->link);
3398 		     link_id++) {
3399 			struct ieee80211_link_data *link =
3400 				wiphy_dereference(wiphy, sdata->link[link_id]);
3401 
3402 			if (!link)
3403 				continue;
3404 
3405 			link->user_power_level = user_power_level;
3406 
3407 			if (txp_type != link->conf->txpower_type) {
3408 				update_txp_type = true;
3409 				link->conf->txpower_type = txp_type;
3410 			}
3411 
3412 			ieee80211_recalc_txpower(link, update_txp_type);
3413 		}
3414 		return 0;
3415 	}
3416 
3417 	local->user_power_level = user_power_level;
3418 
3419 	list_for_each_entry(sdata, &local->interfaces, list) {
3420 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
3421 		    !ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) {
3422 			has_monitor = true;
3423 			continue;
3424 		}
3425 
3426 		for (int link_id = 0;
3427 		     link_id < ARRAY_SIZE(sdata->link);
3428 		     link_id++) {
3429 			struct ieee80211_link_data *link =
3430 				wiphy_dereference(wiphy, sdata->link[link_id]);
3431 
3432 			if (!link)
3433 				continue;
3434 
3435 			link->user_power_level = local->user_power_level;
3436 			if (txp_type != link->conf->txpower_type)
3437 				update_txp_type = true;
3438 			link->conf->txpower_type = txp_type;
3439 		}
3440 	}
3441 	list_for_each_entry(sdata, &local->interfaces, list) {
3442 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
3443 		    !ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
3444 			continue;
3445 
3446 		for (int link_id = 0;
3447 		     link_id < ARRAY_SIZE(sdata->link);
3448 		     link_id++) {
3449 			struct ieee80211_link_data *link =
3450 				wiphy_dereference(wiphy, sdata->link[link_id]);
3451 
3452 			if (!link)
3453 				continue;
3454 
3455 			ieee80211_recalc_txpower(link, update_txp_type);
3456 		}
3457 	}
3458 
3459 	if (has_monitor) {
3460 		sdata = wiphy_dereference(local->hw.wiphy,
3461 					  local->monitor_sdata);
3462 		if (sdata && ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) {
3463 			sdata->deflink.user_power_level = local->user_power_level;
3464 			if (txp_type != sdata->vif.bss_conf.txpower_type)
3465 				update_txp_type = true;
3466 			sdata->vif.bss_conf.txpower_type = txp_type;
3467 
3468 			ieee80211_recalc_txpower(&sdata->deflink,
3469 						 update_txp_type);
3470 		}
3471 	}
3472 
3473 	if (local->emulate_chanctx &&
3474 	    (old_power != local->user_power_level))
3475 		ieee80211_hw_conf_chan(local);
3476 
3477 	return 0;
3478 }
3479 
3480 static int ieee80211_get_tx_power(struct wiphy *wiphy,
3481 				  struct wireless_dev *wdev,
3482 				  int radio_idx,
3483 				  unsigned int link_id,
3484 				  int *dbm)
3485 {
3486 	struct ieee80211_local *local = wiphy_priv(wiphy);
3487 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3488 	struct ieee80211_link_data *link_data;
3489 
3490 	if (local->ops->get_txpower &&
3491 	    (sdata->flags & IEEE80211_SDATA_IN_DRIVER))
3492 		return drv_get_txpower(local, sdata, link_id, dbm);
3493 
3494 	if (local->emulate_chanctx) {
3495 		*dbm = local->hw.conf.power_level;
3496 	} else {
3497 		link_data = wiphy_dereference(wiphy, sdata->link[link_id]);
3498 
3499 		if (link_data)
3500 			*dbm = link_data->conf->txpower;
3501 		else
3502 			return -ENOLINK;
3503 	}
3504 
3505 	/* INT_MIN indicates no power level was set yet */
3506 	if (*dbm == INT_MIN)
3507 		return -EINVAL;
3508 
3509 	return 0;
3510 }
3511 
3512 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
3513 {
3514 	struct ieee80211_local *local = wiphy_priv(wiphy);
3515 
3516 	drv_rfkill_poll(local);
3517 }
3518 
3519 #ifdef CONFIG_NL80211_TESTMODE
3520 static int ieee80211_testmode_cmd(struct wiphy *wiphy,
3521 				  struct wireless_dev *wdev,
3522 				  void *data, int len)
3523 {
3524 	struct ieee80211_local *local = wiphy_priv(wiphy);
3525 	struct ieee80211_vif *vif = NULL;
3526 
3527 	if (!local->ops->testmode_cmd)
3528 		return -EOPNOTSUPP;
3529 
3530 	if (wdev) {
3531 		struct ieee80211_sub_if_data *sdata;
3532 
3533 		sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3534 		if (sdata->flags & IEEE80211_SDATA_IN_DRIVER)
3535 			vif = &sdata->vif;
3536 	}
3537 
3538 	return local->ops->testmode_cmd(&local->hw, vif, data, len);
3539 }
3540 
3541 static int ieee80211_testmode_dump(struct wiphy *wiphy,
3542 				   struct sk_buff *skb,
3543 				   struct netlink_callback *cb,
3544 				   void *data, int len)
3545 {
3546 	struct ieee80211_local *local = wiphy_priv(wiphy);
3547 
3548 	if (!local->ops->testmode_dump)
3549 		return -EOPNOTSUPP;
3550 
3551 	return local->ops->testmode_dump(&local->hw, skb, cb, data, len);
3552 }
3553 #endif
3554 
3555 int __ieee80211_request_smps_mgd(struct ieee80211_sub_if_data *sdata,
3556 				 struct ieee80211_link_data *link,
3557 				 enum ieee80211_smps_mode smps_mode)
3558 {
3559 	const u8 *ap;
3560 	enum ieee80211_smps_mode old_req;
3561 	int err;
3562 	struct sta_info *sta;
3563 	bool tdls_peer_found = false;
3564 
3565 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3566 
3567 	if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
3568 		return -EINVAL;
3569 
3570 	if (!ieee80211_vif_link_active(&sdata->vif, link->link_id))
3571 		return 0;
3572 
3573 	old_req = link->u.mgd.req_smps;
3574 	link->u.mgd.req_smps = smps_mode;
3575 
3576 	/* The driver indicated that EML is enabled for the interface, which
3577 	 * implies that SMPS flows towards the AP should be stopped.
3578 	 */
3579 	if (sdata->vif.driver_flags & IEEE80211_VIF_EML_ACTIVE)
3580 		return 0;
3581 
3582 	if (old_req == smps_mode &&
3583 	    smps_mode != IEEE80211_SMPS_AUTOMATIC)
3584 		return 0;
3585 
3586 	/*
3587 	 * If not associated, or current association is not an HT
3588 	 * association, there's no need to do anything, just store
3589 	 * the new value until we associate.
3590 	 */
3591 	if (!sdata->u.mgd.associated ||
3592 	    link->conf->chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT)
3593 		return 0;
3594 
3595 	ap = sdata->vif.cfg.ap_addr;
3596 
3597 	rcu_read_lock();
3598 	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
3599 		if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
3600 		    !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3601 			continue;
3602 
3603 		tdls_peer_found = true;
3604 		break;
3605 	}
3606 	rcu_read_unlock();
3607 
3608 	if (smps_mode == IEEE80211_SMPS_AUTOMATIC) {
3609 		if (tdls_peer_found || !sdata->u.mgd.powersave)
3610 			smps_mode = IEEE80211_SMPS_OFF;
3611 		else
3612 			smps_mode = IEEE80211_SMPS_DYNAMIC;
3613 	}
3614 
3615 	/* send SM PS frame to AP */
3616 	err = ieee80211_send_smps_action(sdata, smps_mode,
3617 					 ap, ap,
3618 					 ieee80211_vif_is_mld(&sdata->vif) ?
3619 					 link->link_id : -1);
3620 	if (err)
3621 		link->u.mgd.req_smps = old_req;
3622 	else if (smps_mode != IEEE80211_SMPS_OFF && tdls_peer_found)
3623 		ieee80211_teardown_tdls_peers(link);
3624 
3625 	return err;
3626 }
3627 
3628 static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
3629 				    bool enabled, int timeout)
3630 {
3631 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3632 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3633 	unsigned int link_id;
3634 
3635 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
3636 		return -EOPNOTSUPP;
3637 
3638 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
3639 		return -EOPNOTSUPP;
3640 
3641 	if (enabled == sdata->u.mgd.powersave &&
3642 	    timeout == local->dynamic_ps_forced_timeout)
3643 		return 0;
3644 
3645 	sdata->u.mgd.powersave = enabled;
3646 	local->dynamic_ps_forced_timeout = timeout;
3647 
3648 	/* no change, but if automatic follow powersave */
3649 	for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
3650 		struct ieee80211_link_data *link;
3651 
3652 		link = sdata_dereference(sdata->link[link_id], sdata);
3653 
3654 		if (!link)
3655 			continue;
3656 		__ieee80211_request_smps_mgd(sdata, link,
3657 					     link->u.mgd.req_smps);
3658 	}
3659 
3660 	if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3661 		ieee80211_hw_config(local, -1, IEEE80211_CONF_CHANGE_PS);
3662 
3663 	ieee80211_recalc_ps(local);
3664 	ieee80211_recalc_ps_vif(sdata);
3665 	ieee80211_check_fast_rx_iface(sdata);
3666 
3667 	return 0;
3668 }
3669 
3670 static void ieee80211_set_cqm_rssi_link(struct ieee80211_sub_if_data *sdata,
3671 					struct ieee80211_link_data *link,
3672 					s32 rssi_thold, u32 rssi_hyst,
3673 					s32 rssi_low, s32 rssi_high)
3674 {
3675 	struct ieee80211_bss_conf *conf;
3676 
3677 	if (!link || !link->conf)
3678 		return;
3679 
3680 	conf = link->conf;
3681 
3682 	if (rssi_thold && rssi_hyst &&
3683 	    rssi_thold == conf->cqm_rssi_thold &&
3684 	    rssi_hyst == conf->cqm_rssi_hyst)
3685 		return;
3686 
3687 	conf->cqm_rssi_thold = rssi_thold;
3688 	conf->cqm_rssi_hyst = rssi_hyst;
3689 	conf->cqm_rssi_low = rssi_low;
3690 	conf->cqm_rssi_high = rssi_high;
3691 	link->u.mgd.last_cqm_event_signal = 0;
3692 
3693 	if (!ieee80211_vif_link_active(&sdata->vif, link->link_id))
3694 		return;
3695 
3696 	if (sdata->u.mgd.associated &&
3697 	    (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
3698 		ieee80211_link_info_change_notify(sdata, link, BSS_CHANGED_CQM);
3699 }
3700 
3701 static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
3702 					 struct net_device *dev,
3703 					 s32 rssi_thold, u32 rssi_hyst)
3704 {
3705 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3706 	struct ieee80211_vif *vif = &sdata->vif;
3707 	int link_id;
3708 
3709 	if (vif->driver_flags & IEEE80211_VIF_BEACON_FILTER &&
3710 	    !(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
3711 		return -EOPNOTSUPP;
3712 
3713 	/* For MLD, handle CQM change on all the active links */
3714 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
3715 		struct ieee80211_link_data *link =
3716 			sdata_dereference(sdata->link[link_id], sdata);
3717 
3718 		ieee80211_set_cqm_rssi_link(sdata, link, rssi_thold, rssi_hyst,
3719 					    0, 0);
3720 	}
3721 
3722 	return 0;
3723 }
3724 
3725 static int ieee80211_set_cqm_rssi_range_config(struct wiphy *wiphy,
3726 					       struct net_device *dev,
3727 					       s32 rssi_low, s32 rssi_high)
3728 {
3729 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3730 	struct ieee80211_vif *vif = &sdata->vif;
3731 	int link_id;
3732 
3733 	if (vif->driver_flags & IEEE80211_VIF_BEACON_FILTER)
3734 		return -EOPNOTSUPP;
3735 
3736 	/* For MLD, handle CQM change on all the active links */
3737 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
3738 		struct ieee80211_link_data *link =
3739 			sdata_dereference(sdata->link[link_id], sdata);
3740 
3741 		ieee80211_set_cqm_rssi_link(sdata, link, 0, 0,
3742 					    rssi_low, rssi_high);
3743 	}
3744 
3745 	return 0;
3746 }
3747 
3748 static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
3749 				      struct net_device *dev,
3750 				      unsigned int link_id,
3751 				      const u8 *addr,
3752 				      const struct cfg80211_bitrate_mask *mask)
3753 {
3754 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3755 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3756 	int i, ret;
3757 
3758 	if (!ieee80211_sdata_running(sdata))
3759 		return -ENETDOWN;
3760 
3761 	/*
3762 	 * If active validate the setting and reject it if it doesn't leave
3763 	 * at least one basic rate usable, since we really have to be able
3764 	 * to send something, and if we're an AP we have to be able to do
3765 	 * so at a basic rate so that all clients can receive it.
3766 	 */
3767 	if (rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) &&
3768 	    sdata->vif.bss_conf.chanreq.oper.chan) {
3769 		u32 basic_rates = sdata->vif.bss_conf.basic_rates;
3770 		enum nl80211_band band;
3771 
3772 		band = sdata->vif.bss_conf.chanreq.oper.chan->band;
3773 
3774 		if (!(mask->control[band].legacy & basic_rates))
3775 			return -EINVAL;
3776 	}
3777 
3778 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3779 		ret = drv_set_bitrate_mask(local, sdata, mask);
3780 		if (ret)
3781 			return ret;
3782 	}
3783 
3784 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
3785 		struct ieee80211_supported_band *sband = wiphy->bands[i];
3786 		int j;
3787 
3788 		sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
3789 		memcpy(sdata->rc_rateidx_mcs_mask[i], mask->control[i].ht_mcs,
3790 		       sizeof(mask->control[i].ht_mcs));
3791 		memcpy(sdata->rc_rateidx_vht_mcs_mask[i],
3792 		       mask->control[i].vht_mcs,
3793 		       sizeof(mask->control[i].vht_mcs));
3794 
3795 		sdata->rc_has_mcs_mask[i] = false;
3796 		sdata->rc_has_vht_mcs_mask[i] = false;
3797 		if (!sband)
3798 			continue;
3799 
3800 		for (j = 0; j < IEEE80211_HT_MCS_MASK_LEN; j++) {
3801 			if (sdata->rc_rateidx_mcs_mask[i][j] != 0xff) {
3802 				sdata->rc_has_mcs_mask[i] = true;
3803 				break;
3804 			}
3805 		}
3806 
3807 		for (j = 0; j < NL80211_VHT_NSS_MAX; j++) {
3808 			if (sdata->rc_rateidx_vht_mcs_mask[i][j] != 0xffff) {
3809 				sdata->rc_has_vht_mcs_mask[i] = true;
3810 				break;
3811 			}
3812 		}
3813 	}
3814 
3815 	return 0;
3816 }
3817 
3818 static bool ieee80211_is_scan_ongoing(struct wiphy *wiphy,
3819 				      struct ieee80211_local *local,
3820 				      struct cfg80211_chan_def *chandef)
3821 {
3822 	struct cfg80211_scan_request *scan_req;
3823 	int chan_radio_idx, req_radio_idx;
3824 	struct ieee80211_roc_work *roc;
3825 
3826 	if (list_empty(&local->roc_list) && !local->scanning)
3827 		return false;
3828 
3829 	req_radio_idx = cfg80211_get_radio_idx_by_chan(wiphy, chandef->chan);
3830 
3831 	if (local->scanning) {
3832 		scan_req = wiphy_dereference(wiphy, local->scan_req);
3833 		/*
3834 		 * Scan is going on but info is not there. Should not happen
3835 		 * but if it does, let's not take risk and assume we can't use
3836 		 * the hw hence return true
3837 		 */
3838 		if (WARN_ON_ONCE(!scan_req))
3839 			return true;
3840 
3841 		return ieee80211_is_radio_idx_in_scan_req(wiphy, scan_req,
3842 							  req_radio_idx);
3843 	}
3844 
3845 	list_for_each_entry(roc, &local->roc_list, list) {
3846 		chan_radio_idx = cfg80211_get_radio_idx_by_chan(wiphy,
3847 								roc->chan);
3848 		if (chan_radio_idx == req_radio_idx)
3849 			return true;
3850 	}
3851 
3852 	return false;
3853 }
3854 
3855 static int ieee80211_start_radar_detection(struct wiphy *wiphy,
3856 					   struct net_device *dev,
3857 					   struct cfg80211_chan_def *chandef,
3858 					   u32 cac_time_ms, int link_id)
3859 {
3860 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3861 	struct ieee80211_chan_req chanreq = { .oper = *chandef };
3862 	struct ieee80211_local *local = sdata->local;
3863 	struct ieee80211_link_data *link_data;
3864 	int err;
3865 
3866 	lockdep_assert_wiphy(local->hw.wiphy);
3867 
3868 	if (ieee80211_is_scan_ongoing(wiphy, local, chandef))
3869 		return -EBUSY;
3870 
3871 	link_data = sdata_dereference(sdata->link[link_id], sdata);
3872 	if (!link_data)
3873 		return -ENOLINK;
3874 
3875 	/* whatever, but channel contexts should not complain about that one */
3876 	link_data->smps_mode = IEEE80211_SMPS_OFF;
3877 	link_data->needed_rx_chains = local->rx_chains;
3878 
3879 	err = ieee80211_link_use_channel(link_data, &chanreq,
3880 					 IEEE80211_CHANCTX_SHARED);
3881 	if (err)
3882 		return err;
3883 
3884 	wiphy_hrtimer_work_queue(wiphy, &link_data->dfs_cac_timer_work,
3885 				 ms_to_ktime(cac_time_ms));
3886 
3887 	return 0;
3888 }
3889 
3890 static void ieee80211_end_cac(struct wiphy *wiphy,
3891 			      struct net_device *dev, unsigned int link_id)
3892 {
3893 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3894 	struct ieee80211_local *local = sdata->local;
3895 	struct ieee80211_link_data *link_data;
3896 
3897 	lockdep_assert_wiphy(local->hw.wiphy);
3898 
3899 	list_for_each_entry(sdata, &local->interfaces, list) {
3900 		link_data = sdata_dereference(sdata->link[link_id], sdata);
3901 		if (!link_data)
3902 			continue;
3903 
3904 		wiphy_hrtimer_work_cancel(wiphy,
3905 					  &link_data->dfs_cac_timer_work);
3906 
3907 		if (sdata->wdev.links[link_id].cac_started) {
3908 			ieee80211_link_release_channel(link_data);
3909 			sdata->wdev.links[link_id].cac_started = false;
3910 		}
3911 	}
3912 }
3913 
3914 static struct cfg80211_beacon_data *
3915 cfg80211_beacon_dup(struct cfg80211_beacon_data *beacon)
3916 {
3917 	struct cfg80211_beacon_data *new_beacon;
3918 	u8 *pos;
3919 	int len;
3920 
3921 	len = beacon->head_len + beacon->tail_len + beacon->beacon_ies_len +
3922 	      beacon->proberesp_ies_len + beacon->assocresp_ies_len +
3923 	      beacon->probe_resp_len + beacon->lci_len + beacon->civicloc_len;
3924 
3925 	if (beacon->mbssid_ies)
3926 		len += ieee80211_get_mbssid_beacon_len(beacon->mbssid_ies,
3927 						       beacon->rnr_ies,
3928 						       beacon->mbssid_ies->cnt);
3929 
3930 	new_beacon = kzalloc(sizeof(*new_beacon) + len, GFP_KERNEL);
3931 	if (!new_beacon)
3932 		return NULL;
3933 
3934 	if (beacon->mbssid_ies && beacon->mbssid_ies->cnt) {
3935 		new_beacon->mbssid_ies =
3936 			kzalloc_flex(*new_beacon->mbssid_ies, elem,
3937 				     beacon->mbssid_ies->cnt);
3938 		if (!new_beacon->mbssid_ies) {
3939 			kfree(new_beacon);
3940 			return NULL;
3941 		}
3942 
3943 		if (beacon->rnr_ies && beacon->rnr_ies->cnt) {
3944 			new_beacon->rnr_ies =
3945 				kzalloc_flex(*new_beacon->rnr_ies, elem,
3946 					     beacon->rnr_ies->cnt);
3947 			if (!new_beacon->rnr_ies) {
3948 				kfree(new_beacon->mbssid_ies);
3949 				kfree(new_beacon);
3950 				return NULL;
3951 			}
3952 		}
3953 	}
3954 
3955 	pos = (u8 *)(new_beacon + 1);
3956 	if (beacon->head_len) {
3957 		new_beacon->head_len = beacon->head_len;
3958 		new_beacon->head = pos;
3959 		memcpy(pos, beacon->head, beacon->head_len);
3960 		pos += beacon->head_len;
3961 	}
3962 	if (beacon->tail_len) {
3963 		new_beacon->tail_len = beacon->tail_len;
3964 		new_beacon->tail = pos;
3965 		memcpy(pos, beacon->tail, beacon->tail_len);
3966 		pos += beacon->tail_len;
3967 	}
3968 	if (beacon->beacon_ies_len) {
3969 		new_beacon->beacon_ies_len = beacon->beacon_ies_len;
3970 		new_beacon->beacon_ies = pos;
3971 		memcpy(pos, beacon->beacon_ies, beacon->beacon_ies_len);
3972 		pos += beacon->beacon_ies_len;
3973 	}
3974 	if (beacon->proberesp_ies_len) {
3975 		new_beacon->proberesp_ies_len = beacon->proberesp_ies_len;
3976 		new_beacon->proberesp_ies = pos;
3977 		memcpy(pos, beacon->proberesp_ies, beacon->proberesp_ies_len);
3978 		pos += beacon->proberesp_ies_len;
3979 	}
3980 	if (beacon->assocresp_ies_len) {
3981 		new_beacon->assocresp_ies_len = beacon->assocresp_ies_len;
3982 		new_beacon->assocresp_ies = pos;
3983 		memcpy(pos, beacon->assocresp_ies, beacon->assocresp_ies_len);
3984 		pos += beacon->assocresp_ies_len;
3985 	}
3986 	if (beacon->probe_resp_len) {
3987 		new_beacon->probe_resp_len = beacon->probe_resp_len;
3988 		new_beacon->probe_resp = pos;
3989 		memcpy(pos, beacon->probe_resp, beacon->probe_resp_len);
3990 		pos += beacon->probe_resp_len;
3991 	}
3992 	if (beacon->mbssid_ies && beacon->mbssid_ies->cnt) {
3993 		pos += ieee80211_copy_mbssid_beacon(pos,
3994 						    new_beacon->mbssid_ies,
3995 						    beacon->mbssid_ies);
3996 		if (beacon->rnr_ies && beacon->rnr_ies->cnt)
3997 			pos += ieee80211_copy_rnr_beacon(pos,
3998 							 new_beacon->rnr_ies,
3999 							 beacon->rnr_ies);
4000 	}
4001 
4002 	/* might copy -1, meaning no changes requested */
4003 	new_beacon->ftm_responder = beacon->ftm_responder;
4004 	if (beacon->lci) {
4005 		new_beacon->lci_len = beacon->lci_len;
4006 		new_beacon->lci = pos;
4007 		memcpy(pos, beacon->lci, beacon->lci_len);
4008 		pos += beacon->lci_len;
4009 	}
4010 	if (beacon->civicloc) {
4011 		new_beacon->civicloc_len = beacon->civicloc_len;
4012 		new_beacon->civicloc = pos;
4013 		memcpy(pos, beacon->civicloc, beacon->civicloc_len);
4014 		pos += beacon->civicloc_len;
4015 	}
4016 
4017 	return new_beacon;
4018 }
4019 
4020 void ieee80211_csa_finish(struct ieee80211_vif *vif, unsigned int link_id)
4021 {
4022 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4023 	struct ieee80211_local *local = sdata->local;
4024 	struct ieee80211_bss_conf *tx_bss_conf;
4025 	struct ieee80211_link_data *link_data;
4026 
4027 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
4028 		return;
4029 
4030 	rcu_read_lock();
4031 
4032 	link_data = rcu_dereference(sdata->link[link_id]);
4033 	if (WARN_ON(!link_data)) {
4034 		rcu_read_unlock();
4035 		return;
4036 	}
4037 
4038 	tx_bss_conf = rcu_dereference(link_data->conf->tx_bss_conf);
4039 	if (tx_bss_conf == link_data->conf) {
4040 		/* Trigger ieee80211_csa_finish() on the non-transmitting
4041 		 * interfaces when channel switch is received on
4042 		 * transmitting interface
4043 		 */
4044 		struct ieee80211_link_data *iter;
4045 
4046 		for_each_sdata_link_rcu(local, iter) {
4047 			if (iter->sdata == sdata ||
4048 			    rcu_access_pointer(iter->conf->tx_bss_conf) != tx_bss_conf)
4049 				continue;
4050 
4051 			wiphy_work_queue(iter->sdata->local->hw.wiphy,
4052 					 &iter->csa.finalize_work);
4053 		}
4054 	}
4055 
4056 	wiphy_work_queue(local->hw.wiphy, &link_data->csa.finalize_work);
4057 
4058 	rcu_read_unlock();
4059 }
4060 EXPORT_SYMBOL(ieee80211_csa_finish);
4061 
4062 void ieee80211_channel_switch_disconnect(struct ieee80211_vif *vif)
4063 {
4064 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4065 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4066 	struct ieee80211_local *local = sdata->local;
4067 
4068 	sdata_info(sdata, "channel switch failed, disconnecting\n");
4069 	wiphy_work_queue(local->hw.wiphy, &ifmgd->csa_connection_drop_work);
4070 }
4071 EXPORT_SYMBOL(ieee80211_channel_switch_disconnect);
4072 
4073 static int ieee80211_set_after_csa_beacon(struct ieee80211_link_data *link_data,
4074 					  u64 *changed)
4075 {
4076 	struct ieee80211_sub_if_data *sdata = link_data->sdata;
4077 	int err;
4078 
4079 	switch (sdata->vif.type) {
4080 	case NL80211_IFTYPE_AP:
4081 		if (!link_data->u.ap.next_beacon)
4082 			return -EINVAL;
4083 
4084 		err = ieee80211_assign_beacon(sdata, link_data,
4085 					      link_data->u.ap.next_beacon,
4086 					      NULL, NULL, changed);
4087 		ieee80211_free_next_beacon(link_data);
4088 
4089 		if (err < 0)
4090 			return err;
4091 		break;
4092 	case NL80211_IFTYPE_ADHOC:
4093 		err = ieee80211_ibss_finish_csa(sdata, changed);
4094 		if (err < 0)
4095 			return err;
4096 		break;
4097 #ifdef CONFIG_MAC80211_MESH
4098 	case NL80211_IFTYPE_MESH_POINT:
4099 		err = ieee80211_mesh_finish_csa(sdata, changed);
4100 		if (err < 0)
4101 			return err;
4102 		break;
4103 #endif
4104 	default:
4105 		WARN_ON(1);
4106 		return -EINVAL;
4107 	}
4108 
4109 	return 0;
4110 }
4111 
4112 static int __ieee80211_csa_finalize(struct ieee80211_link_data *link_data)
4113 {
4114 	struct ieee80211_sub_if_data *sdata = link_data->sdata;
4115 	struct ieee80211_local *local = sdata->local;
4116 	struct ieee80211_bss_conf *link_conf = link_data->conf;
4117 	u64 changed = 0;
4118 	int err;
4119 
4120 	lockdep_assert_wiphy(local->hw.wiphy);
4121 
4122 	/*
4123 	 * using reservation isn't immediate as it may be deferred until later
4124 	 * with multi-vif. once reservation is complete it will re-schedule the
4125 	 * work with no reserved_chanctx so verify chandef to check if it
4126 	 * completed successfully
4127 	 */
4128 
4129 	if (link_data->reserved_chanctx) {
4130 		/*
4131 		 * with multi-vif csa driver may call ieee80211_csa_finish()
4132 		 * many times while waiting for other interfaces to use their
4133 		 * reservations
4134 		 */
4135 		if (link_data->reserved_ready)
4136 			return 0;
4137 
4138 		return ieee80211_link_use_reserved_context(link_data);
4139 	}
4140 
4141 	if (!cfg80211_chandef_identical(&link_conf->chanreq.oper,
4142 					&link_data->csa.chanreq.oper))
4143 		return -EINVAL;
4144 
4145 	link_conf->csa_active = false;
4146 
4147 	err = ieee80211_set_after_csa_beacon(link_data, &changed);
4148 	if (err)
4149 		return err;
4150 
4151 	ieee80211_link_info_change_notify(sdata, link_data, changed);
4152 
4153 	ieee80211_vif_unblock_queues_csa(sdata);
4154 
4155 	err = drv_post_channel_switch(link_data);
4156 	if (err)
4157 		return err;
4158 
4159 	cfg80211_ch_switch_notify(sdata->dev, &link_data->csa.chanreq.oper,
4160 				  link_data->link_id);
4161 
4162 	return 0;
4163 }
4164 
4165 static void ieee80211_csa_finalize(struct ieee80211_link_data *link_data)
4166 {
4167 	struct ieee80211_sub_if_data *sdata = link_data->sdata;
4168 	int link_id = -1;
4169 
4170 	if (__ieee80211_csa_finalize(link_data)) {
4171 		sdata_info(sdata, "failed to finalize CSA on link %d, disconnecting\n",
4172 			   link_data->link_id);
4173 		if (sdata->vif.type == NL80211_IFTYPE_AP ||
4174 		    sdata->vif.type == NL80211_IFTYPE_P2P_GO)
4175 			/*
4176 			 * link_id is expected only for AP/P2P_GO type
4177 			 * currently
4178 			 */
4179 			link_id = link_data->link_id;
4180 
4181 		cfg80211_stop_link(sdata->local->hw.wiphy, &sdata->wdev,
4182 				   link_id, GFP_KERNEL);
4183 	}
4184 }
4185 
4186 void ieee80211_csa_finalize_work(struct wiphy *wiphy, struct wiphy_work *work)
4187 {
4188 	struct ieee80211_link_data *link =
4189 		container_of(work, struct ieee80211_link_data, csa.finalize_work);
4190 	struct ieee80211_sub_if_data *sdata = link->sdata;
4191 	struct ieee80211_local *local = sdata->local;
4192 
4193 	lockdep_assert_wiphy(local->hw.wiphy);
4194 
4195 	/* AP might have been stopped while waiting for the lock. */
4196 	if (!link->conf->csa_active)
4197 		return;
4198 
4199 	if (!ieee80211_sdata_running(sdata))
4200 		return;
4201 
4202 	ieee80211_csa_finalize(link);
4203 }
4204 
4205 static int ieee80211_set_csa_beacon(struct ieee80211_link_data *link_data,
4206 				    struct cfg80211_csa_settings *params,
4207 				    u64 *changed)
4208 {
4209 	struct ieee80211_sub_if_data *sdata = link_data->sdata;
4210 	struct ieee80211_csa_settings csa = {};
4211 	int err;
4212 
4213 	switch (sdata->vif.type) {
4214 	case NL80211_IFTYPE_AP:
4215 		link_data->u.ap.next_beacon =
4216 			cfg80211_beacon_dup(&params->beacon_after);
4217 		if (!link_data->u.ap.next_beacon)
4218 			return -ENOMEM;
4219 
4220 		/*
4221 		 * With a count of 0, we don't have to wait for any
4222 		 * TBTT before switching, so complete the CSA
4223 		 * immediately.  In theory, with a count == 1 we
4224 		 * should delay the switch until just before the next
4225 		 * TBTT, but that would complicate things so we switch
4226 		 * immediately too.  If we would delay the switch
4227 		 * until the next TBTT, we would have to set the probe
4228 		 * response here.
4229 		 *
4230 		 * TODO: A channel switch with count <= 1 without
4231 		 * sending a CSA action frame is kind of useless,
4232 		 * because the clients won't know we're changing
4233 		 * channels.  The action frame must be implemented
4234 		 * either here or in the userspace.
4235 		 */
4236 		if (params->count <= 1)
4237 			break;
4238 
4239 		if ((params->n_counter_offsets_beacon >
4240 		     IEEE80211_MAX_CNTDWN_COUNTERS_NUM) ||
4241 		    (params->n_counter_offsets_presp >
4242 		     IEEE80211_MAX_CNTDWN_COUNTERS_NUM)) {
4243 			ieee80211_free_next_beacon(link_data);
4244 			return -EINVAL;
4245 		}
4246 
4247 		csa.counter_offsets_beacon = params->counter_offsets_beacon;
4248 		csa.counter_offsets_presp = params->counter_offsets_presp;
4249 		csa.n_counter_offsets_beacon = params->n_counter_offsets_beacon;
4250 		csa.n_counter_offsets_presp = params->n_counter_offsets_presp;
4251 		csa.count = params->count;
4252 
4253 		err = ieee80211_assign_beacon(sdata, link_data,
4254 					      &params->beacon_csa, &csa,
4255 					      NULL, changed);
4256 		if (err < 0) {
4257 			ieee80211_free_next_beacon(link_data);
4258 			return err;
4259 		}
4260 
4261 		break;
4262 	case NL80211_IFTYPE_ADHOC:
4263 		if (!sdata->vif.cfg.ibss_joined)
4264 			return -EINVAL;
4265 
4266 		if (params->chandef.width != sdata->u.ibss.chandef.width)
4267 			return -EINVAL;
4268 
4269 		switch (params->chandef.width) {
4270 		case NL80211_CHAN_WIDTH_40:
4271 			if (cfg80211_get_chandef_type(&params->chandef) !=
4272 			    cfg80211_get_chandef_type(&sdata->u.ibss.chandef))
4273 				return -EINVAL;
4274 			break;
4275 		case NL80211_CHAN_WIDTH_5:
4276 		case NL80211_CHAN_WIDTH_10:
4277 		case NL80211_CHAN_WIDTH_20_NOHT:
4278 		case NL80211_CHAN_WIDTH_20:
4279 			break;
4280 		default:
4281 			return -EINVAL;
4282 		}
4283 
4284 		/* changes into another band are not supported */
4285 		if (sdata->u.ibss.chandef.chan->band !=
4286 		    params->chandef.chan->band)
4287 			return -EINVAL;
4288 
4289 		/* see comments in the NL80211_IFTYPE_AP block */
4290 		if (params->count > 1) {
4291 			err = ieee80211_ibss_csa_beacon(sdata, params, changed);
4292 			if (err < 0)
4293 				return err;
4294 		}
4295 
4296 		ieee80211_send_action_csa(sdata, params);
4297 
4298 		break;
4299 #ifdef CONFIG_MAC80211_MESH
4300 	case NL80211_IFTYPE_MESH_POINT: {
4301 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4302 
4303 		/* changes into another band are not supported */
4304 		if (sdata->vif.bss_conf.chanreq.oper.chan->band !=
4305 		    params->chandef.chan->band)
4306 			return -EINVAL;
4307 
4308 		if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_NONE) {
4309 			ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_INIT;
4310 			if (!ifmsh->pre_value)
4311 				ifmsh->pre_value = 1;
4312 			else
4313 				ifmsh->pre_value++;
4314 		}
4315 
4316 		/* see comments in the NL80211_IFTYPE_AP block */
4317 		if (params->count > 1) {
4318 			err = ieee80211_mesh_csa_beacon(sdata, params, changed);
4319 			if (err < 0) {
4320 				ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
4321 				return err;
4322 			}
4323 		}
4324 
4325 		if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT)
4326 			ieee80211_send_action_csa(sdata, params);
4327 
4328 		break;
4329 		}
4330 #endif
4331 	default:
4332 		return -EOPNOTSUPP;
4333 	}
4334 
4335 	return 0;
4336 }
4337 
4338 static void ieee80211_color_change_abort(struct ieee80211_link_data *link)
4339 {
4340 	link->conf->color_change_active = false;
4341 
4342 	ieee80211_free_next_beacon(link);
4343 
4344 	cfg80211_color_change_aborted_notify(link->sdata->dev, link->link_id);
4345 }
4346 
4347 static int
4348 __ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
4349 			   struct cfg80211_csa_settings *params)
4350 {
4351 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4352 	struct ieee80211_chan_req chanreq = { .oper = params->chandef };
4353 	struct ieee80211_local *local = sdata->local;
4354 	struct ieee80211_channel_switch ch_switch = {
4355 		.link_id = params->link_id,
4356 	};
4357 	struct ieee80211_chanctx_conf *conf;
4358 	struct ieee80211_chanctx *chanctx;
4359 	struct ieee80211_bss_conf *link_conf;
4360 	struct ieee80211_link_data *link_data;
4361 	u64 changed = 0;
4362 	u8 link_id = params->link_id;
4363 	int err;
4364 
4365 	lockdep_assert_wiphy(local->hw.wiphy);
4366 
4367 	if (ieee80211_is_scan_ongoing(wiphy, local, &params->chandef))
4368 		return -EBUSY;
4369 
4370 	if (sdata->wdev.links[link_id].cac_started)
4371 		return -EBUSY;
4372 
4373 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
4374 		return -EINVAL;
4375 
4376 	link_data = wiphy_dereference(wiphy, sdata->link[link_id]);
4377 	if (!link_data)
4378 		return -ENOLINK;
4379 
4380 	link_conf = link_data->conf;
4381 
4382 	if (chanreq.oper.punctured && !link_conf->eht_support)
4383 		return -EINVAL;
4384 
4385 	/* don't allow another channel switch if one is already active. */
4386 	if (link_conf->csa_active)
4387 		return -EBUSY;
4388 
4389 	conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
4390 	if (!conf) {
4391 		err = -EBUSY;
4392 		goto out;
4393 	}
4394 
4395 	if (params->chandef.chan->freq_offset) {
4396 		/* this may work, but is untested */
4397 		err = -EOPNOTSUPP;
4398 		goto out;
4399 	}
4400 
4401 	err = ieee80211_set_unsol_bcast_probe_resp(sdata,
4402 						   &params->unsol_bcast_probe_resp,
4403 						   link_data, link_conf, &changed);
4404 	if (err)
4405 		goto out;
4406 
4407 	chanctx = container_of(conf, struct ieee80211_chanctx, conf);
4408 
4409 	ch_switch.timestamp = 0;
4410 	ch_switch.device_timestamp = 0;
4411 	ch_switch.block_tx = params->block_tx;
4412 	ch_switch.chandef = chanreq.oper;
4413 	ch_switch.count = params->count;
4414 
4415 	err = drv_pre_channel_switch(sdata, &ch_switch);
4416 	if (err)
4417 		goto out;
4418 
4419 	err = ieee80211_link_reserve_chanctx(link_data, &chanreq,
4420 					     chanctx->mode,
4421 					     params->radar_required);
4422 	if (err)
4423 		goto out;
4424 
4425 	/* if reservation is invalid then this will fail */
4426 	err = ieee80211_check_combinations(sdata, NULL, 0, 0, -1);
4427 	if (err) {
4428 		ieee80211_link_unreserve_chanctx(link_data);
4429 		goto out;
4430 	}
4431 
4432 	/* if there is a color change in progress, abort it */
4433 	if (link_conf->color_change_active)
4434 		ieee80211_color_change_abort(link_data);
4435 
4436 	err = ieee80211_set_csa_beacon(link_data, params, &changed);
4437 	if (err) {
4438 		ieee80211_link_unreserve_chanctx(link_data);
4439 		goto out;
4440 	}
4441 
4442 	link_data->csa.chanreq = chanreq;
4443 	link_conf->csa_active = true;
4444 
4445 	if (params->block_tx)
4446 		ieee80211_vif_block_queues_csa(sdata);
4447 
4448 	cfg80211_ch_switch_started_notify(sdata->dev,
4449 					  &link_data->csa.chanreq.oper, link_id,
4450 					  params->count, params->block_tx);
4451 
4452 	if (changed) {
4453 		ieee80211_link_info_change_notify(sdata, link_data, changed);
4454 		drv_channel_switch_beacon(sdata, &link_data->csa.chanreq.oper);
4455 	} else {
4456 		/* if the beacon didn't change, we can finalize immediately */
4457 		ieee80211_csa_finalize(link_data);
4458 	}
4459 
4460 out:
4461 	return err;
4462 }
4463 
4464 int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
4465 			     struct cfg80211_csa_settings *params)
4466 {
4467 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4468 	struct ieee80211_local *local = sdata->local;
4469 
4470 	lockdep_assert_wiphy(local->hw.wiphy);
4471 
4472 	return __ieee80211_channel_switch(wiphy, dev, params);
4473 }
4474 
4475 u64 ieee80211_mgmt_tx_cookie(struct ieee80211_local *local)
4476 {
4477 	lockdep_assert_wiphy(local->hw.wiphy);
4478 
4479 	local->roc_cookie_counter++;
4480 
4481 	/* wow, you wrapped 64 bits ... more likely a bug */
4482 	if (WARN_ON(local->roc_cookie_counter == 0))
4483 		local->roc_cookie_counter++;
4484 
4485 	return local->roc_cookie_counter;
4486 }
4487 
4488 int ieee80211_attach_ack_skb(struct ieee80211_local *local, struct sk_buff *skb,
4489 			     u64 *cookie, gfp_t gfp)
4490 {
4491 	unsigned long spin_flags;
4492 	struct sk_buff *ack_skb;
4493 	int id;
4494 
4495 	ack_skb = skb_copy(skb, gfp);
4496 	if (!ack_skb)
4497 		return -ENOMEM;
4498 
4499 	spin_lock_irqsave(&local->ack_status_lock, spin_flags);
4500 	id = idr_alloc(&local->ack_status_frames, ack_skb,
4501 		       1, 0x2000, GFP_ATOMIC);
4502 	spin_unlock_irqrestore(&local->ack_status_lock, spin_flags);
4503 
4504 	if (id < 0) {
4505 		kfree_skb(ack_skb);
4506 		return -ENOMEM;
4507 	}
4508 
4509 	IEEE80211_SKB_CB(skb)->status_data_idr = 1;
4510 	IEEE80211_SKB_CB(skb)->status_data = id;
4511 
4512 	*cookie = ieee80211_mgmt_tx_cookie(local);
4513 	IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
4514 
4515 	return 0;
4516 }
4517 
4518 static void
4519 ieee80211_update_mgmt_frame_registrations(struct wiphy *wiphy,
4520 					  struct wireless_dev *wdev,
4521 					  struct mgmt_frame_regs *upd)
4522 {
4523 	struct ieee80211_local *local = wiphy_priv(wiphy);
4524 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
4525 	u32 preq_mask = BIT(IEEE80211_STYPE_PROBE_REQ >> 4);
4526 	u32 action_mask = BIT(IEEE80211_STYPE_ACTION >> 4);
4527 	bool global_change, intf_change;
4528 
4529 	global_change =
4530 		(local->probe_req_reg != !!(upd->global_stypes & preq_mask)) ||
4531 		(local->rx_mcast_action_reg !=
4532 		 !!(upd->global_mcast_stypes & action_mask));
4533 	local->probe_req_reg = upd->global_stypes & preq_mask;
4534 	local->rx_mcast_action_reg = upd->global_mcast_stypes & action_mask;
4535 
4536 	intf_change = (sdata->vif.probe_req_reg !=
4537 		       !!(upd->interface_stypes & preq_mask)) ||
4538 		(sdata->vif.rx_mcast_action_reg !=
4539 		 !!(upd->interface_mcast_stypes & action_mask));
4540 	sdata->vif.probe_req_reg = upd->interface_stypes & preq_mask;
4541 	sdata->vif.rx_mcast_action_reg =
4542 		upd->interface_mcast_stypes & action_mask;
4543 
4544 	if (!local->open_count)
4545 		return;
4546 
4547 	if (intf_change && ieee80211_sdata_running(sdata))
4548 		drv_config_iface_filter(local, sdata,
4549 					sdata->vif.probe_req_reg ?
4550 						FIF_PROBE_REQ : 0,
4551 					FIF_PROBE_REQ);
4552 
4553 	if (global_change)
4554 		ieee80211_configure_filter(local);
4555 }
4556 
4557 static int ieee80211_set_antenna(struct wiphy *wiphy, int radio_idx,
4558 				 u32 tx_ant, u32 rx_ant)
4559 {
4560 	struct ieee80211_local *local = wiphy_priv(wiphy);
4561 	int ret;
4562 
4563 	if (local->started)
4564 		return -EOPNOTSUPP;
4565 
4566 	ret = drv_set_antenna(local, tx_ant, rx_ant);
4567 	if (ret)
4568 		return ret;
4569 
4570 	local->rx_chains = hweight8(rx_ant);
4571 	return 0;
4572 }
4573 
4574 static int ieee80211_get_antenna(struct wiphy *wiphy, int radio_idx,
4575 				 u32 *tx_ant, u32 *rx_ant)
4576 {
4577 	struct ieee80211_local *local = wiphy_priv(wiphy);
4578 
4579 	return drv_get_antenna(local, radio_idx, tx_ant, rx_ant);
4580 }
4581 
4582 static int ieee80211_set_rekey_data(struct wiphy *wiphy,
4583 				    struct net_device *dev,
4584 				    struct cfg80211_gtk_rekey_data *data)
4585 {
4586 	struct ieee80211_local *local = wiphy_priv(wiphy);
4587 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4588 
4589 	if (!local->ops->set_rekey_data)
4590 		return -EOPNOTSUPP;
4591 
4592 	drv_set_rekey_data(local, sdata, data);
4593 
4594 	return 0;
4595 }
4596 
4597 static int ieee80211_probe_client(struct wiphy *wiphy, struct net_device *dev,
4598 				  const u8 *peer, u64 *cookie)
4599 {
4600 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4601 	struct ieee80211_local *local = sdata->local;
4602 	struct ieee80211_qos_hdr *nullfunc;
4603 	struct sk_buff *skb;
4604 	int size = sizeof(*nullfunc);
4605 	__le16 fc;
4606 	bool qos;
4607 	struct ieee80211_tx_info *info;
4608 	struct sta_info *sta;
4609 	struct ieee80211_chanctx_conf *chanctx_conf;
4610 	struct ieee80211_bss_conf *conf;
4611 	enum nl80211_band band;
4612 	u8 link_id;
4613 	int ret;
4614 
4615 	/* the lock is needed to assign the cookie later */
4616 	lockdep_assert_wiphy(local->hw.wiphy);
4617 
4618 	rcu_read_lock();
4619 	sta = sta_info_get_bss(sdata, peer);
4620 	if (!sta) {
4621 		ret = -ENOLINK;
4622 		goto unlock;
4623 	}
4624 
4625 	qos = sta->sta.wme;
4626 
4627 	if (ieee80211_vif_is_mld(&sdata->vif)) {
4628 		if (sta->sta.mlo) {
4629 			link_id = IEEE80211_LINK_UNSPECIFIED;
4630 		} else {
4631 			/*
4632 			 * For non-MLO clients connected to an AP MLD, band
4633 			 * information is not used; instead, sta->deflink is
4634 			 * used to send packets.
4635 			 */
4636 			link_id = sta->deflink.link_id;
4637 
4638 			conf = rcu_dereference(sdata->vif.link_conf[link_id]);
4639 
4640 			if (unlikely(!conf)) {
4641 				ret = -ENOLINK;
4642 				goto unlock;
4643 			}
4644 		}
4645 		/* MLD transmissions must not rely on the band */
4646 		band = 0;
4647 	} else {
4648 		chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
4649 		if (WARN_ON(!chanctx_conf)) {
4650 			ret = -EINVAL;
4651 			goto unlock;
4652 		}
4653 		band = chanctx_conf->def.chan->band;
4654 		link_id = 0;
4655 	}
4656 
4657 	if (qos) {
4658 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
4659 				 IEEE80211_STYPE_QOS_NULLFUNC |
4660 				 IEEE80211_FCTL_FROMDS);
4661 	} else {
4662 		size -= 2;
4663 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
4664 				 IEEE80211_STYPE_NULLFUNC |
4665 				 IEEE80211_FCTL_FROMDS);
4666 	}
4667 
4668 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
4669 	if (!skb) {
4670 		ret = -ENOMEM;
4671 		goto unlock;
4672 	}
4673 
4674 	skb->dev = dev;
4675 
4676 	skb_reserve(skb, local->hw.extra_tx_headroom);
4677 
4678 	nullfunc = skb_put(skb, size);
4679 	nullfunc->frame_control = fc;
4680 	nullfunc->duration_id = 0;
4681 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
4682 	if (ieee80211_vif_is_mld(&sdata->vif) && !sta->sta.mlo) {
4683 		memcpy(nullfunc->addr2, conf->addr, ETH_ALEN);
4684 		memcpy(nullfunc->addr3, conf->addr, ETH_ALEN);
4685 	} else {
4686 		memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
4687 		memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
4688 	}
4689 	nullfunc->seq_ctrl = 0;
4690 
4691 	info = IEEE80211_SKB_CB(skb);
4692 
4693 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
4694 		       IEEE80211_TX_INTFL_NL80211_FRAME_TX;
4695 	info->band = band;
4696 
4697 	info->control.flags |= u32_encode_bits(link_id,
4698 					       IEEE80211_TX_CTRL_MLO_LINK);
4699 	skb_set_queue_mapping(skb, IEEE80211_AC_VO);
4700 	skb->priority = 7;
4701 	if (qos)
4702 		nullfunc->qos_ctrl = cpu_to_le16(7);
4703 
4704 	ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_ATOMIC);
4705 	if (ret) {
4706 		kfree_skb(skb);
4707 		goto unlock;
4708 	}
4709 
4710 	local_bh_disable();
4711 	ieee80211_xmit(sdata, sta, skb);
4712 	local_bh_enable();
4713 
4714 	ret = 0;
4715 unlock:
4716 	rcu_read_unlock();
4717 
4718 	return ret;
4719 }
4720 
4721 static int ieee80211_cfg_get_channel(struct wiphy *wiphy,
4722 				     struct wireless_dev *wdev,
4723 				     unsigned int link_id,
4724 				     struct cfg80211_chan_def *chandef)
4725 {
4726 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
4727 	struct ieee80211_local *local = wiphy_priv(wiphy);
4728 	struct ieee80211_chanctx_conf *chanctx_conf;
4729 	struct ieee80211_link_data *link;
4730 	int ret = -ENODATA;
4731 
4732 	rcu_read_lock();
4733 	link = rcu_dereference(sdata->link[link_id]);
4734 	if (!link) {
4735 		ret = -ENOLINK;
4736 		goto out;
4737 	}
4738 
4739 	chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
4740 	if (chanctx_conf) {
4741 		*chandef = link->conf->chanreq.oper;
4742 		ret = 0;
4743 	} else if (local->open_count > 0 &&
4744 		   local->open_count == local->virt_monitors &&
4745 		   sdata->vif.type == NL80211_IFTYPE_MONITOR) {
4746 		*chandef = local->monitor_chanreq.oper;
4747 		ret = 0;
4748 	}
4749 out:
4750 	rcu_read_unlock();
4751 
4752 	return ret;
4753 }
4754 
4755 #ifdef CONFIG_PM
4756 static void ieee80211_set_wakeup(struct wiphy *wiphy, bool enabled)
4757 {
4758 	drv_set_wakeup(wiphy_priv(wiphy), enabled);
4759 }
4760 #endif
4761 
4762 static int ieee80211_set_qos_map(struct wiphy *wiphy,
4763 				 struct net_device *dev,
4764 				 struct cfg80211_qos_map *qos_map)
4765 {
4766 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4767 	struct mac80211_qos_map *new_qos_map, *old_qos_map;
4768 
4769 	if (qos_map) {
4770 		new_qos_map = kzalloc_obj(*new_qos_map);
4771 		if (!new_qos_map)
4772 			return -ENOMEM;
4773 		memcpy(&new_qos_map->qos_map, qos_map, sizeof(*qos_map));
4774 	} else {
4775 		/* A NULL qos_map was passed to disable QoS mapping */
4776 		new_qos_map = NULL;
4777 	}
4778 
4779 	old_qos_map = sdata_dereference(sdata->qos_map, sdata);
4780 	rcu_assign_pointer(sdata->qos_map, new_qos_map);
4781 	if (old_qos_map)
4782 		kfree_rcu(old_qos_map, rcu_head);
4783 
4784 	return 0;
4785 }
4786 
4787 static int ieee80211_set_ap_chanwidth(struct wiphy *wiphy,
4788 				      struct net_device *dev,
4789 				      unsigned int link_id,
4790 				      struct cfg80211_chan_def *chandef)
4791 {
4792 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4793 	struct ieee80211_link_data *link;
4794 	struct ieee80211_chan_req chanreq = { .oper = *chandef };
4795 	int ret;
4796 	u64 changed = 0;
4797 
4798 	link = sdata_dereference(sdata->link[link_id], sdata);
4799 
4800 	ret = ieee80211_link_change_chanreq(link, &chanreq, &changed);
4801 	if (ret == 0)
4802 		ieee80211_link_info_change_notify(sdata, link, changed);
4803 
4804 	return ret;
4805 }
4806 
4807 static int ieee80211_add_tx_ts(struct wiphy *wiphy, struct net_device *dev,
4808 			       u8 tsid, const u8 *peer, u8 up,
4809 			       u16 admitted_time)
4810 {
4811 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4812 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4813 	int ac = ieee802_1d_to_ac[up];
4814 
4815 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
4816 		return -EOPNOTSUPP;
4817 
4818 	if (!(sdata->wmm_acm & BIT(up)))
4819 		return -EINVAL;
4820 
4821 	if (ifmgd->tx_tspec[ac].admitted_time)
4822 		return -EBUSY;
4823 
4824 	if (admitted_time) {
4825 		ifmgd->tx_tspec[ac].admitted_time = 32 * admitted_time;
4826 		ifmgd->tx_tspec[ac].tsid = tsid;
4827 		ifmgd->tx_tspec[ac].up = up;
4828 	}
4829 
4830 	return 0;
4831 }
4832 
4833 static int ieee80211_del_tx_ts(struct wiphy *wiphy, struct net_device *dev,
4834 			       u8 tsid, const u8 *peer)
4835 {
4836 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4837 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4838 	struct ieee80211_local *local = wiphy_priv(wiphy);
4839 	int ac;
4840 
4841 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4842 		struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
4843 
4844 		/* skip unused entries */
4845 		if (!tx_tspec->admitted_time)
4846 			continue;
4847 
4848 		if (tx_tspec->tsid != tsid)
4849 			continue;
4850 
4851 		/* due to this new packets will be reassigned to non-ACM ACs */
4852 		tx_tspec->up = -1;
4853 
4854 		/* Make sure that all packets have been sent to avoid to
4855 		 * restore the QoS params on packets that are still on the
4856 		 * queues.
4857 		 */
4858 		synchronize_net();
4859 		ieee80211_flush_queues(local, sdata, false);
4860 
4861 		/* restore the normal QoS parameters
4862 		 * (unconditionally to avoid races)
4863 		 */
4864 		tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
4865 		tx_tspec->downgraded = false;
4866 		ieee80211_sta_handle_tspec_ac_params(sdata);
4867 
4868 		/* finally clear all the data */
4869 		memset(tx_tspec, 0, sizeof(*tx_tspec));
4870 
4871 		return 0;
4872 	}
4873 
4874 	return -ENOENT;
4875 }
4876 
4877 void ieee80211_nan_func_terminated(struct ieee80211_vif *vif,
4878 				   u8 inst_id,
4879 				   enum nl80211_nan_func_term_reason reason,
4880 				   gfp_t gfp)
4881 {
4882 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4883 	struct cfg80211_nan_func *func;
4884 	u64 cookie;
4885 
4886 	if (WARN_ON(vif->type != NL80211_IFTYPE_NAN))
4887 		return;
4888 
4889 	spin_lock_bh(&sdata->u.nan.func_lock);
4890 
4891 	func = idr_find(&sdata->u.nan.function_inst_ids, inst_id);
4892 	if (WARN_ON(!func)) {
4893 		spin_unlock_bh(&sdata->u.nan.func_lock);
4894 		return;
4895 	}
4896 
4897 	cookie = func->cookie;
4898 	idr_remove(&sdata->u.nan.function_inst_ids, inst_id);
4899 
4900 	spin_unlock_bh(&sdata->u.nan.func_lock);
4901 
4902 	cfg80211_free_nan_func(func);
4903 
4904 	cfg80211_nan_func_terminated(ieee80211_vif_to_wdev(vif), inst_id,
4905 				     reason, cookie, gfp);
4906 }
4907 EXPORT_SYMBOL(ieee80211_nan_func_terminated);
4908 
4909 void ieee80211_nan_func_match(struct ieee80211_vif *vif,
4910 			      struct cfg80211_nan_match_params *match,
4911 			      gfp_t gfp)
4912 {
4913 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4914 	struct cfg80211_nan_func *func;
4915 
4916 	if (WARN_ON(vif->type != NL80211_IFTYPE_NAN))
4917 		return;
4918 
4919 	spin_lock_bh(&sdata->u.nan.func_lock);
4920 
4921 	func = idr_find(&sdata->u.nan.function_inst_ids,  match->inst_id);
4922 	if (WARN_ON(!func)) {
4923 		spin_unlock_bh(&sdata->u.nan.func_lock);
4924 		return;
4925 	}
4926 	match->cookie = func->cookie;
4927 
4928 	spin_unlock_bh(&sdata->u.nan.func_lock);
4929 
4930 	cfg80211_nan_match(ieee80211_vif_to_wdev(vif), match, gfp);
4931 }
4932 EXPORT_SYMBOL(ieee80211_nan_func_match);
4933 
4934 static int ieee80211_set_multicast_to_unicast(struct wiphy *wiphy,
4935 					      struct net_device *dev,
4936 					      const bool enabled)
4937 {
4938 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4939 
4940 	sdata->u.ap.multicast_to_unicast = enabled;
4941 
4942 	return 0;
4943 }
4944 
4945 void ieee80211_fill_txq_stats(struct cfg80211_txq_stats *txqstats,
4946 			      struct txq_info *txqi)
4947 {
4948 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_BACKLOG_BYTES))) {
4949 		txqstats->filled |= BIT(NL80211_TXQ_STATS_BACKLOG_BYTES);
4950 		txqstats->backlog_bytes = txqi->tin.backlog_bytes;
4951 	}
4952 
4953 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_BACKLOG_PACKETS))) {
4954 		txqstats->filled |= BIT(NL80211_TXQ_STATS_BACKLOG_PACKETS);
4955 		txqstats->backlog_packets = txqi->tin.backlog_packets;
4956 	}
4957 
4958 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_FLOWS))) {
4959 		txqstats->filled |= BIT(NL80211_TXQ_STATS_FLOWS);
4960 		txqstats->flows = txqi->tin.flows;
4961 	}
4962 
4963 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_DROPS))) {
4964 		txqstats->filled |= BIT(NL80211_TXQ_STATS_DROPS);
4965 		txqstats->drops = txqi->cstats.drop_count;
4966 	}
4967 
4968 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_ECN_MARKS))) {
4969 		txqstats->filled |= BIT(NL80211_TXQ_STATS_ECN_MARKS);
4970 		txqstats->ecn_marks = txqi->cstats.ecn_mark;
4971 	}
4972 
4973 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_OVERLIMIT))) {
4974 		txqstats->filled |= BIT(NL80211_TXQ_STATS_OVERLIMIT);
4975 		txqstats->overlimit = txqi->tin.overlimit;
4976 	}
4977 
4978 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_COLLISIONS))) {
4979 		txqstats->filled |= BIT(NL80211_TXQ_STATS_COLLISIONS);
4980 		txqstats->collisions = txqi->tin.collisions;
4981 	}
4982 
4983 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_TX_BYTES))) {
4984 		txqstats->filled |= BIT(NL80211_TXQ_STATS_TX_BYTES);
4985 		txqstats->tx_bytes = txqi->tin.tx_bytes;
4986 	}
4987 
4988 	if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_TX_PACKETS))) {
4989 		txqstats->filled |= BIT(NL80211_TXQ_STATS_TX_PACKETS);
4990 		txqstats->tx_packets = txqi->tin.tx_packets;
4991 	}
4992 }
4993 
4994 static int ieee80211_get_txq_stats(struct wiphy *wiphy,
4995 				   struct wireless_dev *wdev,
4996 				   struct cfg80211_txq_stats *txqstats)
4997 {
4998 	struct ieee80211_local *local = wiphy_priv(wiphy);
4999 	struct ieee80211_sub_if_data *sdata;
5000 	int ret = 0;
5001 
5002 	spin_lock_bh(&local->fq.lock);
5003 
5004 	if (wdev) {
5005 		sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
5006 		if (!sdata->vif.txq) {
5007 			ret = 1;
5008 			goto out;
5009 		}
5010 		ieee80211_fill_txq_stats(txqstats, to_txq_info(sdata->vif.txq));
5011 	} else {
5012 		/* phy stats */
5013 		txqstats->filled |= BIT(NL80211_TXQ_STATS_BACKLOG_PACKETS) |
5014 				    BIT(NL80211_TXQ_STATS_BACKLOG_BYTES) |
5015 				    BIT(NL80211_TXQ_STATS_OVERLIMIT) |
5016 				    BIT(NL80211_TXQ_STATS_OVERMEMORY) |
5017 				    BIT(NL80211_TXQ_STATS_COLLISIONS) |
5018 				    BIT(NL80211_TXQ_STATS_MAX_FLOWS);
5019 		txqstats->backlog_packets = local->fq.backlog;
5020 		txqstats->backlog_bytes = local->fq.memory_usage;
5021 		txqstats->overlimit = local->fq.overlimit;
5022 		txqstats->overmemory = local->fq.overmemory;
5023 		txqstats->collisions = local->fq.collisions;
5024 		txqstats->max_flows = local->fq.flows_cnt;
5025 	}
5026 
5027 out:
5028 	spin_unlock_bh(&local->fq.lock);
5029 
5030 	return ret;
5031 }
5032 
5033 static int
5034 ieee80211_get_ftm_responder_stats(struct wiphy *wiphy,
5035 				  struct net_device *dev,
5036 				  struct cfg80211_ftm_responder_stats *ftm_stats)
5037 {
5038 	struct ieee80211_local *local = wiphy_priv(wiphy);
5039 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5040 
5041 	return drv_get_ftm_responder_stats(local, sdata, ftm_stats);
5042 }
5043 
5044 static int
5045 ieee80211_start_pmsr(struct wiphy *wiphy, struct wireless_dev *dev,
5046 		     struct cfg80211_pmsr_request *request)
5047 {
5048 	struct ieee80211_local *local = wiphy_priv(wiphy);
5049 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(dev);
5050 
5051 	return drv_start_pmsr(local, sdata, request);
5052 }
5053 
5054 static void
5055 ieee80211_abort_pmsr(struct wiphy *wiphy, struct wireless_dev *dev,
5056 		     struct cfg80211_pmsr_request *request)
5057 {
5058 	struct ieee80211_local *local = wiphy_priv(wiphy);
5059 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(dev);
5060 
5061 	return drv_abort_pmsr(local, sdata, request);
5062 }
5063 
5064 static int ieee80211_set_tid_config(struct wiphy *wiphy,
5065 				    struct net_device *dev,
5066 				    struct cfg80211_tid_config *tid_conf)
5067 {
5068 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5069 	struct sta_info *sta;
5070 
5071 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5072 
5073 	if (!sdata->local->ops->set_tid_config)
5074 		return -EOPNOTSUPP;
5075 
5076 	if (!tid_conf->peer)
5077 		return drv_set_tid_config(sdata->local, sdata, NULL, tid_conf);
5078 
5079 	sta = sta_info_get_bss(sdata, tid_conf->peer);
5080 	if (!sta)
5081 		return -ENOENT;
5082 
5083 	return drv_set_tid_config(sdata->local, sdata, &sta->sta, tid_conf);
5084 }
5085 
5086 static int ieee80211_reset_tid_config(struct wiphy *wiphy,
5087 				      struct net_device *dev,
5088 				      const u8 *peer, u8 tids)
5089 {
5090 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5091 	struct sta_info *sta;
5092 
5093 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5094 
5095 	if (!sdata->local->ops->reset_tid_config)
5096 		return -EOPNOTSUPP;
5097 
5098 	if (!peer)
5099 		return drv_reset_tid_config(sdata->local, sdata, NULL, tids);
5100 
5101 	sta = sta_info_get_bss(sdata, peer);
5102 	if (!sta)
5103 		return -ENOENT;
5104 
5105 	return drv_reset_tid_config(sdata->local, sdata, &sta->sta, tids);
5106 }
5107 
5108 static int ieee80211_set_sar_specs(struct wiphy *wiphy,
5109 				   struct cfg80211_sar_specs *sar)
5110 {
5111 	struct ieee80211_local *local = wiphy_priv(wiphy);
5112 
5113 	if (!local->ops->set_sar_specs)
5114 		return -EOPNOTSUPP;
5115 
5116 	return local->ops->set_sar_specs(&local->hw, sar);
5117 }
5118 
5119 static int
5120 ieee80211_set_after_color_change_beacon(struct ieee80211_link_data *link,
5121 					u64 *changed)
5122 {
5123 	struct ieee80211_sub_if_data *sdata = link->sdata;
5124 
5125 	switch (sdata->vif.type) {
5126 	case NL80211_IFTYPE_AP: {
5127 		int ret;
5128 
5129 		if (!link->u.ap.next_beacon)
5130 			return -EINVAL;
5131 
5132 		ret = ieee80211_assign_beacon(sdata, link,
5133 					      link->u.ap.next_beacon,
5134 					      NULL, NULL, changed);
5135 		ieee80211_free_next_beacon(link);
5136 
5137 		if (ret < 0)
5138 			return ret;
5139 
5140 		break;
5141 	}
5142 	default:
5143 		WARN_ON_ONCE(1);
5144 		return -EINVAL;
5145 	}
5146 
5147 	return 0;
5148 }
5149 
5150 static int
5151 ieee80211_set_color_change_beacon(struct ieee80211_link_data *link,
5152 				  struct cfg80211_color_change_settings *params,
5153 				  u64 *changed)
5154 {
5155 	struct ieee80211_sub_if_data *sdata = link->sdata;
5156 	struct ieee80211_color_change_settings color_change = {};
5157 	int err;
5158 
5159 	switch (sdata->vif.type) {
5160 	case NL80211_IFTYPE_AP:
5161 		link->u.ap.next_beacon =
5162 			cfg80211_beacon_dup(&params->beacon_next);
5163 		if (!link->u.ap.next_beacon)
5164 			return -ENOMEM;
5165 
5166 		if (params->count <= 1)
5167 			break;
5168 
5169 		color_change.counter_offset_beacon =
5170 			params->counter_offset_beacon;
5171 		color_change.counter_offset_presp =
5172 			params->counter_offset_presp;
5173 		color_change.count = params->count;
5174 
5175 		err = ieee80211_assign_beacon(sdata, link,
5176 					      &params->beacon_color_change,
5177 					      NULL, &color_change, changed);
5178 		if (err < 0) {
5179 			ieee80211_free_next_beacon(link);
5180 			return err;
5181 		}
5182 		break;
5183 	default:
5184 		return -EOPNOTSUPP;
5185 	}
5186 
5187 	return 0;
5188 }
5189 
5190 static void
5191 ieee80211_color_change_bss_config_notify(struct ieee80211_link_data *link,
5192 					 u8 color, int enable, u64 changed)
5193 {
5194 	struct ieee80211_sub_if_data *sdata = link->sdata;
5195 
5196 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5197 
5198 	link->conf->he_bss_color.color = color;
5199 	link->conf->he_bss_color.enabled = enable;
5200 	changed |= BSS_CHANGED_HE_BSS_COLOR;
5201 
5202 	ieee80211_link_info_change_notify(sdata, link, changed);
5203 
5204 	if (!link->conf->nontransmitted &&
5205 	    rcu_access_pointer(link->conf->tx_bss_conf)) {
5206 		struct ieee80211_link_data *tmp;
5207 
5208 		for_each_sdata_link(sdata->local, tmp) {
5209 			if (tmp->sdata == sdata ||
5210 			    rcu_access_pointer(tmp->conf->tx_bss_conf) != link->conf)
5211 				continue;
5212 
5213 			tmp->conf->he_bss_color.color = color;
5214 			tmp->conf->he_bss_color.enabled = enable;
5215 			ieee80211_link_info_change_notify(tmp->sdata, tmp,
5216 							  BSS_CHANGED_HE_BSS_COLOR);
5217 		}
5218 	}
5219 }
5220 
5221 static int ieee80211_color_change_finalize(struct ieee80211_link_data *link)
5222 {
5223 	struct ieee80211_sub_if_data *sdata = link->sdata;
5224 	struct ieee80211_local *local = sdata->local;
5225 	u64 changed = 0;
5226 	int err;
5227 
5228 	lockdep_assert_wiphy(local->hw.wiphy);
5229 
5230 	link->conf->color_change_active = false;
5231 
5232 	err = ieee80211_set_after_color_change_beacon(link, &changed);
5233 	if (err) {
5234 		cfg80211_color_change_aborted_notify(sdata->dev, link->link_id);
5235 		return err;
5236 	}
5237 
5238 	ieee80211_color_change_bss_config_notify(link,
5239 						 link->conf->color_change_color,
5240 						 1, changed);
5241 	cfg80211_color_change_notify(sdata->dev, link->link_id);
5242 
5243 	return 0;
5244 }
5245 
5246 void ieee80211_color_change_finalize_work(struct wiphy *wiphy,
5247 					  struct wiphy_work *work)
5248 {
5249 	struct ieee80211_link_data *link =
5250 		container_of(work, struct ieee80211_link_data,
5251 			     color_change_finalize_work);
5252 	struct ieee80211_sub_if_data *sdata = link->sdata;
5253 	struct ieee80211_bss_conf *link_conf = link->conf;
5254 	struct ieee80211_local *local = sdata->local;
5255 
5256 	lockdep_assert_wiphy(local->hw.wiphy);
5257 
5258 	/* AP might have been stopped while waiting for the lock. */
5259 	if (!link_conf->color_change_active)
5260 		return;
5261 
5262 	if (!ieee80211_sdata_running(sdata))
5263 		return;
5264 
5265 	ieee80211_color_change_finalize(link);
5266 }
5267 
5268 void ieee80211_color_collision_detection_work(struct wiphy *wiphy,
5269 					      struct wiphy_work *work)
5270 {
5271 	struct ieee80211_link_data *link =
5272 		container_of(work, struct ieee80211_link_data,
5273 			     color_collision_detect_work.work);
5274 	struct ieee80211_sub_if_data *sdata = link->sdata;
5275 
5276 	cfg80211_obss_color_collision_notify(sdata->dev, link->color_bitmap,
5277 					     link->link_id);
5278 }
5279 
5280 void ieee80211_color_change_finish(struct ieee80211_vif *vif, u8 link_id)
5281 {
5282 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5283 	struct ieee80211_link_data *link;
5284 
5285 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
5286 		return;
5287 
5288 	rcu_read_lock();
5289 
5290 	link = rcu_dereference(sdata->link[link_id]);
5291 	if (WARN_ON(!link)) {
5292 		rcu_read_unlock();
5293 		return;
5294 	}
5295 
5296 	wiphy_work_queue(sdata->local->hw.wiphy,
5297 			 &link->color_change_finalize_work);
5298 
5299 	rcu_read_unlock();
5300 }
5301 EXPORT_SYMBOL_GPL(ieee80211_color_change_finish);
5302 
5303 void
5304 ieee80211_obss_color_collision_notify(struct ieee80211_vif *vif,
5305 				      u64 color_bitmap, u8 link_id)
5306 {
5307 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5308 	struct ieee80211_link_data *link;
5309 
5310 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
5311 		return;
5312 
5313 	rcu_read_lock();
5314 
5315 	link = rcu_dereference(sdata->link[link_id]);
5316 	if (WARN_ON(!link)) {
5317 		rcu_read_unlock();
5318 		return;
5319 	}
5320 
5321 	if (link->conf->color_change_active || link->conf->csa_active) {
5322 		rcu_read_unlock();
5323 		return;
5324 	}
5325 
5326 	if (wiphy_delayed_work_pending(sdata->local->hw.wiphy,
5327 				       &link->color_collision_detect_work)) {
5328 		rcu_read_unlock();
5329 		return;
5330 	}
5331 
5332 	link->color_bitmap = color_bitmap;
5333 	/* queue the color collision detection event every 500 ms in order to
5334 	 * avoid sending too much netlink messages to userspace.
5335 	 */
5336 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
5337 				 &link->color_collision_detect_work,
5338 				 msecs_to_jiffies(500));
5339 
5340 	rcu_read_unlock();
5341 }
5342 EXPORT_SYMBOL_GPL(ieee80211_obss_color_collision_notify);
5343 
5344 static int
5345 ieee80211_color_change(struct wiphy *wiphy, struct net_device *dev,
5346 		       struct cfg80211_color_change_settings *params)
5347 {
5348 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5349 	struct ieee80211_local *local = sdata->local;
5350 	struct ieee80211_bss_conf *link_conf;
5351 	struct ieee80211_link_data *link;
5352 	u8 link_id = params->link_id;
5353 	u64 changed = 0;
5354 	int err;
5355 
5356 	lockdep_assert_wiphy(local->hw.wiphy);
5357 
5358 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
5359 		return -EINVAL;
5360 
5361 	link = wiphy_dereference(wiphy, sdata->link[link_id]);
5362 	if (!link)
5363 		return -ENOLINK;
5364 
5365 	link_conf = link->conf;
5366 
5367 	if (link_conf->nontransmitted)
5368 		return -EINVAL;
5369 
5370 	/* don't allow another color change if one is already active or if csa
5371 	 * is active
5372 	 */
5373 	if (link_conf->color_change_active || link_conf->csa_active) {
5374 		err = -EBUSY;
5375 		goto out;
5376 	}
5377 
5378 	err = ieee80211_set_unsol_bcast_probe_resp(sdata,
5379 						   &params->unsol_bcast_probe_resp,
5380 						   link, link_conf, &changed);
5381 	if (err)
5382 		goto out;
5383 
5384 	err = ieee80211_set_color_change_beacon(link, params, &changed);
5385 	if (err)
5386 		goto out;
5387 
5388 	link_conf->color_change_active = true;
5389 	link_conf->color_change_color = params->color;
5390 
5391 	cfg80211_color_change_started_notify(sdata->dev, params->count, link_id);
5392 
5393 	if (changed)
5394 		ieee80211_color_change_bss_config_notify(link, 0, 0, changed);
5395 	else
5396 		/* if the beacon didn't change, we can finalize immediately */
5397 		ieee80211_color_change_finalize(link);
5398 
5399 out:
5400 
5401 	return err;
5402 }
5403 
5404 static int
5405 ieee80211_set_radar_background(struct wiphy *wiphy,
5406 			       struct cfg80211_chan_def *chandef)
5407 {
5408 	struct ieee80211_local *local = wiphy_priv(wiphy);
5409 
5410 	if (!local->ops->set_radar_background)
5411 		return -EOPNOTSUPP;
5412 
5413 	return local->ops->set_radar_background(&local->hw, chandef);
5414 }
5415 
5416 static int ieee80211_add_intf_link(struct wiphy *wiphy,
5417 				   struct wireless_dev *wdev,
5418 				   unsigned int link_id)
5419 {
5420 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
5421 
5422 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5423 
5424 	if (wdev->use_4addr)
5425 		return -EOPNOTSUPP;
5426 
5427 	return ieee80211_vif_set_links(sdata, wdev->valid_links, 0);
5428 }
5429 
5430 static void ieee80211_del_intf_link(struct wiphy *wiphy,
5431 				    struct wireless_dev *wdev,
5432 				    unsigned int link_id)
5433 {
5434 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
5435 	u16 new_links = wdev->valid_links & ~BIT(link_id);
5436 
5437 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5438 
5439 	/* During the link teardown process, certain functions require the
5440 	 * link_id to remain in the valid_links bitmap. Therefore, instead
5441 	 * of removing the link_id from the bitmap, pass a masked value to
5442 	 * simulate as if link_id does not exist anymore.
5443 	 */
5444 	ieee80211_vif_set_links(sdata, new_links, 0);
5445 }
5446 
5447 static int
5448 ieee80211_add_link_station(struct wiphy *wiphy, struct net_device *dev,
5449 			   struct link_station_parameters *params)
5450 {
5451 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5452 	struct ieee80211_local *local = wiphy_priv(wiphy);
5453 	struct sta_info *sta;
5454 	int ret;
5455 
5456 	lockdep_assert_wiphy(local->hw.wiphy);
5457 
5458 	sta = sta_info_get_bss(sdata, params->mld_mac);
5459 	if (!sta)
5460 		return -ENOENT;
5461 
5462 	if (!sta->sta.valid_links)
5463 		return -EINVAL;
5464 
5465 	if (sta->sta.valid_links & BIT(params->link_id))
5466 		return -EALREADY;
5467 
5468 	ret = ieee80211_sta_allocate_link(sta, params->link_id);
5469 	if (ret)
5470 		return ret;
5471 
5472 	ret = sta_link_apply_parameters(local, sta, STA_LINK_MODE_NEW, params);
5473 	if (ret) {
5474 		ieee80211_sta_free_link(sta, params->link_id);
5475 		return ret;
5476 	}
5477 
5478 	if (test_sta_flag(sta, WLAN_STA_ASSOC)) {
5479 		struct link_sta_info *link_sta;
5480 
5481 		link_sta = sdata_dereference(sta->link[params->link_id], sdata);
5482 		rate_control_rate_init(link_sta);
5483 	}
5484 
5485 	/* ieee80211_sta_activate_link frees the link upon failure */
5486 	return ieee80211_sta_activate_link(sta, params->link_id);
5487 }
5488 
5489 static int
5490 ieee80211_mod_link_station(struct wiphy *wiphy, struct net_device *dev,
5491 			   struct link_station_parameters *params)
5492 {
5493 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5494 	struct ieee80211_local *local = wiphy_priv(wiphy);
5495 	struct sta_info *sta;
5496 
5497 	lockdep_assert_wiphy(local->hw.wiphy);
5498 
5499 	sta = sta_info_get_bss(sdata, params->mld_mac);
5500 	if (!sta)
5501 		return -ENOENT;
5502 
5503 	if (!(sta->sta.valid_links & BIT(params->link_id)))
5504 		return -EINVAL;
5505 
5506 	return sta_link_apply_parameters(local, sta, STA_LINK_MODE_LINK_MODIFY,
5507 					 params);
5508 }
5509 
5510 static int
5511 ieee80211_del_link_station(struct wiphy *wiphy, struct net_device *dev,
5512 			   struct link_station_del_parameters *params)
5513 {
5514 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5515 	struct sta_info *sta;
5516 
5517 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5518 
5519 	sta = sta_info_get_bss(sdata, params->mld_mac);
5520 	if (!sta)
5521 		return -ENOENT;
5522 
5523 	if (!(sta->sta.valid_links & BIT(params->link_id)))
5524 		return -EINVAL;
5525 
5526 	/* must not create a STA without links */
5527 	if (sta->sta.valid_links == BIT(params->link_id))
5528 		return -EINVAL;
5529 
5530 	ieee80211_sta_remove_link(sta, params->link_id);
5531 
5532 	return 0;
5533 }
5534 
5535 static int ieee80211_set_hw_timestamp(struct wiphy *wiphy,
5536 				      struct net_device *dev,
5537 				      struct cfg80211_set_hw_timestamp *hwts)
5538 {
5539 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5540 	struct ieee80211_local *local = sdata->local;
5541 
5542 	if (!local->ops->set_hw_timestamp)
5543 		return -EOPNOTSUPP;
5544 
5545 	if (!check_sdata_in_driver(sdata))
5546 		return -EIO;
5547 
5548 	return local->ops->set_hw_timestamp(&local->hw, &sdata->vif, hwts);
5549 }
5550 
5551 static int
5552 ieee80211_set_ttlm(struct wiphy *wiphy, struct net_device *dev,
5553 		   struct cfg80211_ttlm_params *params)
5554 {
5555 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5556 
5557 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5558 
5559 	return ieee80211_req_neg_ttlm(sdata, params);
5560 }
5561 
5562 static int
5563 ieee80211_assoc_ml_reconf(struct wiphy *wiphy, struct net_device *dev,
5564 			  struct cfg80211_ml_reconf_req *req)
5565 {
5566 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5567 
5568 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
5569 
5570 	return ieee80211_mgd_assoc_ml_reconf(sdata, req);
5571 }
5572 
5573 static int
5574 ieee80211_set_epcs(struct wiphy *wiphy, struct net_device *dev, bool enable)
5575 {
5576 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5577 
5578 	return ieee80211_mgd_set_epcs(sdata, enable);
5579 }
5580 
5581 const struct cfg80211_ops mac80211_config_ops = {
5582 	.add_virtual_intf = ieee80211_add_iface,
5583 	.del_virtual_intf = ieee80211_del_iface,
5584 	.change_virtual_intf = ieee80211_change_iface,
5585 	.start_p2p_device = ieee80211_start_p2p_device,
5586 	.stop_p2p_device = ieee80211_stop_p2p_device,
5587 	.add_key = ieee80211_add_key,
5588 	.del_key = ieee80211_del_key,
5589 	.get_key = ieee80211_get_key,
5590 	.set_default_key = ieee80211_config_default_key,
5591 	.set_default_mgmt_key = ieee80211_config_default_mgmt_key,
5592 	.set_default_beacon_key = ieee80211_config_default_beacon_key,
5593 	.start_ap = ieee80211_start_ap,
5594 	.change_beacon = ieee80211_change_beacon,
5595 	.stop_ap = ieee80211_stop_ap,
5596 	.add_station = ieee80211_add_station,
5597 	.del_station = ieee80211_del_station,
5598 	.change_station = ieee80211_change_station,
5599 	.get_station = ieee80211_get_station,
5600 	.dump_station = ieee80211_dump_station,
5601 	.dump_survey = ieee80211_dump_survey,
5602 #ifdef CONFIG_MAC80211_MESH
5603 	.add_mpath = ieee80211_add_mpath,
5604 	.del_mpath = ieee80211_del_mpath,
5605 	.change_mpath = ieee80211_change_mpath,
5606 	.get_mpath = ieee80211_get_mpath,
5607 	.dump_mpath = ieee80211_dump_mpath,
5608 	.get_mpp = ieee80211_get_mpp,
5609 	.dump_mpp = ieee80211_dump_mpp,
5610 	.update_mesh_config = ieee80211_update_mesh_config,
5611 	.get_mesh_config = ieee80211_get_mesh_config,
5612 	.join_mesh = ieee80211_join_mesh,
5613 	.leave_mesh = ieee80211_leave_mesh,
5614 #endif
5615 	.join_ocb = ieee80211_join_ocb,
5616 	.leave_ocb = ieee80211_leave_ocb,
5617 	.change_bss = ieee80211_change_bss,
5618 	.inform_bss = ieee80211_inform_bss,
5619 	.set_txq_params = ieee80211_set_txq_params,
5620 	.set_monitor_channel = ieee80211_set_monitor_channel,
5621 	.suspend = ieee80211_suspend,
5622 	.resume = ieee80211_resume,
5623 	.scan = ieee80211_scan,
5624 	.abort_scan = ieee80211_abort_scan,
5625 	.sched_scan_start = ieee80211_sched_scan_start,
5626 	.sched_scan_stop = ieee80211_sched_scan_stop,
5627 	.auth = ieee80211_auth,
5628 	.assoc = ieee80211_assoc,
5629 	.deauth = ieee80211_deauth,
5630 	.disassoc = ieee80211_disassoc,
5631 	.join_ibss = ieee80211_join_ibss,
5632 	.leave_ibss = ieee80211_leave_ibss,
5633 	.set_mcast_rate = ieee80211_set_mcast_rate,
5634 	.set_wiphy_params = ieee80211_set_wiphy_params,
5635 	.set_tx_power = ieee80211_set_tx_power,
5636 	.get_tx_power = ieee80211_get_tx_power,
5637 	.rfkill_poll = ieee80211_rfkill_poll,
5638 	CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
5639 	CFG80211_TESTMODE_DUMP(ieee80211_testmode_dump)
5640 	.set_power_mgmt = ieee80211_set_power_mgmt,
5641 	.set_bitrate_mask = ieee80211_set_bitrate_mask,
5642 	.remain_on_channel = ieee80211_remain_on_channel,
5643 	.cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
5644 	.mgmt_tx = ieee80211_mgmt_tx,
5645 	.mgmt_tx_cancel_wait = ieee80211_mgmt_tx_cancel_wait,
5646 	.set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
5647 	.set_cqm_rssi_range_config = ieee80211_set_cqm_rssi_range_config,
5648 	.update_mgmt_frame_registrations =
5649 		ieee80211_update_mgmt_frame_registrations,
5650 	.set_antenna = ieee80211_set_antenna,
5651 	.get_antenna = ieee80211_get_antenna,
5652 	.set_rekey_data = ieee80211_set_rekey_data,
5653 	.tdls_oper = ieee80211_tdls_oper,
5654 	.tdls_mgmt = ieee80211_tdls_mgmt,
5655 	.tdls_channel_switch = ieee80211_tdls_channel_switch,
5656 	.tdls_cancel_channel_switch = ieee80211_tdls_cancel_channel_switch,
5657 	.probe_client = ieee80211_probe_client,
5658 	.set_noack_map = ieee80211_set_noack_map,
5659 #ifdef CONFIG_PM
5660 	.set_wakeup = ieee80211_set_wakeup,
5661 #endif
5662 	.get_channel = ieee80211_cfg_get_channel,
5663 	.start_radar_detection = ieee80211_start_radar_detection,
5664 	.end_cac = ieee80211_end_cac,
5665 	.channel_switch = ieee80211_channel_switch,
5666 	.set_qos_map = ieee80211_set_qos_map,
5667 	.set_ap_chanwidth = ieee80211_set_ap_chanwidth,
5668 	.add_tx_ts = ieee80211_add_tx_ts,
5669 	.del_tx_ts = ieee80211_del_tx_ts,
5670 	.start_nan = ieee80211_start_nan,
5671 	.stop_nan = ieee80211_stop_nan,
5672 	.nan_change_conf = ieee80211_nan_change_conf,
5673 	.add_nan_func = ieee80211_add_nan_func,
5674 	.del_nan_func = ieee80211_del_nan_func,
5675 	.set_multicast_to_unicast = ieee80211_set_multicast_to_unicast,
5676 	.tx_control_port = ieee80211_tx_control_port,
5677 	.get_txq_stats = ieee80211_get_txq_stats,
5678 	.get_ftm_responder_stats = ieee80211_get_ftm_responder_stats,
5679 	.start_pmsr = ieee80211_start_pmsr,
5680 	.abort_pmsr = ieee80211_abort_pmsr,
5681 	.probe_mesh_link = ieee80211_probe_mesh_link,
5682 	.set_tid_config = ieee80211_set_tid_config,
5683 	.reset_tid_config = ieee80211_reset_tid_config,
5684 	.set_sar_specs = ieee80211_set_sar_specs,
5685 	.color_change = ieee80211_color_change,
5686 	.set_radar_background = ieee80211_set_radar_background,
5687 	.add_intf_link = ieee80211_add_intf_link,
5688 	.del_intf_link = ieee80211_del_intf_link,
5689 	.add_link_station = ieee80211_add_link_station,
5690 	.mod_link_station = ieee80211_mod_link_station,
5691 	.del_link_station = ieee80211_del_link_station,
5692 	.set_hw_timestamp = ieee80211_set_hw_timestamp,
5693 	.set_ttlm = ieee80211_set_ttlm,
5694 	.get_radio_mask = ieee80211_get_radio_mask,
5695 	.assoc_ml_reconf = ieee80211_assoc_ml_reconf,
5696 	.set_epcs = ieee80211_set_epcs,
5697 };
5698