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