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