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