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