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