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