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