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