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