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