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