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