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