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