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