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