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