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