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