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