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