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