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