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