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