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