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