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