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