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