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