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