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