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