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