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