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