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