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