Merge remote-tracking branch 'jc_docs/docs-next'
[linux] / net / mac80211 / cfg.c
1 /*
2  * mac80211 configuration hooks for cfg80211
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2013-2015  Intel Mobile Communications GmbH
6  * Copyright (C) 2015-2017 Intel Deutschland GmbH
7  * Copyright (C) 2018 Intel Corporation
8  *
9  * This file is GPLv2 as found in COPYING.
10  */
11
12 #include <linux/ieee80211.h>
13 #include <linux/nl80211.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/slab.h>
16 #include <net/net_namespace.h>
17 #include <linux/rcupdate.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 void ieee80211_set_mu_mimo_follow(struct ieee80211_sub_if_data *sdata,
27                                          struct vif_params *params)
28 {
29         bool mu_mimo_groups = false;
30         bool mu_mimo_follow = false;
31
32         if (params->vht_mumimo_groups) {
33                 u64 membership;
34
35                 BUILD_BUG_ON(sizeof(membership) != WLAN_MEMBERSHIP_LEN);
36
37                 memcpy(sdata->vif.bss_conf.mu_group.membership,
38                        params->vht_mumimo_groups, WLAN_MEMBERSHIP_LEN);
39                 memcpy(sdata->vif.bss_conf.mu_group.position,
40                        params->vht_mumimo_groups + WLAN_MEMBERSHIP_LEN,
41                        WLAN_USER_POSITION_LEN);
42                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_MU_GROUPS);
43                 /* don't care about endianness - just check for 0 */
44                 memcpy(&membership, params->vht_mumimo_groups,
45                        WLAN_MEMBERSHIP_LEN);
46                 mu_mimo_groups = membership != 0;
47         }
48
49         if (params->vht_mumimo_follow_addr) {
50                 mu_mimo_follow =
51                         is_valid_ether_addr(params->vht_mumimo_follow_addr);
52                 ether_addr_copy(sdata->u.mntr.mu_follow_addr,
53                                 params->vht_mumimo_follow_addr);
54         }
55
56         sdata->vif.mu_mimo_owner = mu_mimo_groups || mu_mimo_follow;
57 }
58
59 static int ieee80211_set_mon_options(struct ieee80211_sub_if_data *sdata,
60                                      struct vif_params *params)
61 {
62         struct ieee80211_local *local = sdata->local;
63         struct ieee80211_sub_if_data *monitor_sdata;
64
65         /* check flags first */
66         if (params->flags && ieee80211_sdata_running(sdata)) {
67                 u32 mask = MONITOR_FLAG_COOK_FRAMES | MONITOR_FLAG_ACTIVE;
68
69                 /*
70                  * Prohibit MONITOR_FLAG_COOK_FRAMES and
71                  * MONITOR_FLAG_ACTIVE to be changed while the
72                  * interface is up.
73                  * Else we would need to add a lot of cruft
74                  * to update everything:
75                  *      cooked_mntrs, monitor and all fif_* counters
76                  *      reconfigure hardware
77                  */
78                 if ((params->flags & mask) != (sdata->u.mntr.flags & mask))
79                         return -EBUSY;
80         }
81
82         /* also validate MU-MIMO change */
83         monitor_sdata = rtnl_dereference(local->monitor_sdata);
84
85         if (!monitor_sdata &&
86             (params->vht_mumimo_groups || params->vht_mumimo_follow_addr))
87                 return -EOPNOTSUPP;
88
89         /* apply all changes now - no failures allowed */
90
91         if (monitor_sdata)
92                 ieee80211_set_mu_mimo_follow(monitor_sdata, params);
93
94         if (params->flags) {
95                 if (ieee80211_sdata_running(sdata)) {
96                         ieee80211_adjust_monitor_flags(sdata, -1);
97                         sdata->u.mntr.flags = params->flags;
98                         ieee80211_adjust_monitor_flags(sdata, 1);
99
100                         ieee80211_configure_filter(local);
101                 } else {
102                         /*
103                          * Because the interface is down, ieee80211_do_stop
104                          * and ieee80211_do_open take care of "everything"
105                          * mentioned in the comment above.
106                          */
107                         sdata->u.mntr.flags = params->flags;
108                 }
109         }
110
111         return 0;
112 }
113
114 static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy,
115                                                 const char *name,
116                                                 unsigned char name_assign_type,
117                                                 enum nl80211_iftype type,
118                                                 struct vif_params *params)
119 {
120         struct ieee80211_local *local = wiphy_priv(wiphy);
121         struct wireless_dev *wdev;
122         struct ieee80211_sub_if_data *sdata;
123         int err;
124
125         err = ieee80211_if_add(local, name, name_assign_type, &wdev, type, params);
126         if (err)
127                 return ERR_PTR(err);
128
129         sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
130
131         if (type == NL80211_IFTYPE_MONITOR) {
132                 err = ieee80211_set_mon_options(sdata, params);
133                 if (err) {
134                         ieee80211_if_remove(sdata);
135                         return NULL;
136                 }
137         }
138
139         return wdev;
140 }
141
142 static int ieee80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
143 {
144         ieee80211_if_remove(IEEE80211_WDEV_TO_SUB_IF(wdev));
145
146         return 0;
147 }
148
149 static int ieee80211_change_iface(struct wiphy *wiphy,
150                                   struct net_device *dev,
151                                   enum nl80211_iftype type,
152                                   struct vif_params *params)
153 {
154         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
155         int ret;
156
157         ret = ieee80211_if_change_type(sdata, type);
158         if (ret)
159                 return ret;
160
161         if (type == NL80211_IFTYPE_AP_VLAN && params->use_4addr == 0) {
162                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
163                 ieee80211_check_fast_rx_iface(sdata);
164         } else if (type == NL80211_IFTYPE_STATION && params->use_4addr >= 0) {
165                 sdata->u.mgd.use_4addr = params->use_4addr;
166         }
167
168         if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
169                 ret = ieee80211_set_mon_options(sdata, params);
170                 if (ret)
171                         return ret;
172         }
173
174         return 0;
175 }
176
177 static int ieee80211_start_p2p_device(struct wiphy *wiphy,
178                                       struct wireless_dev *wdev)
179 {
180         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
181         int ret;
182
183         mutex_lock(&sdata->local->chanctx_mtx);
184         ret = ieee80211_check_combinations(sdata, NULL, 0, 0);
185         mutex_unlock(&sdata->local->chanctx_mtx);
186         if (ret < 0)
187                 return ret;
188
189         return ieee80211_do_open(wdev, true);
190 }
191
192 static void ieee80211_stop_p2p_device(struct wiphy *wiphy,
193                                       struct wireless_dev *wdev)
194 {
195         ieee80211_sdata_stop(IEEE80211_WDEV_TO_SUB_IF(wdev));
196 }
197
198 static int ieee80211_start_nan(struct wiphy *wiphy,
199                                struct wireless_dev *wdev,
200                                struct cfg80211_nan_conf *conf)
201 {
202         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
203         int ret;
204
205         mutex_lock(&sdata->local->chanctx_mtx);
206         ret = ieee80211_check_combinations(sdata, NULL, 0, 0);
207         mutex_unlock(&sdata->local->chanctx_mtx);
208         if (ret < 0)
209                 return ret;
210
211         ret = ieee80211_do_open(wdev, true);
212         if (ret)
213                 return ret;
214
215         ret = drv_start_nan(sdata->local, sdata, conf);
216         if (ret)
217                 ieee80211_sdata_stop(sdata);
218
219         sdata->u.nan.conf = *conf;
220
221         return ret;
222 }
223
224 static void ieee80211_stop_nan(struct wiphy *wiphy,
225                                struct wireless_dev *wdev)
226 {
227         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
228
229         drv_stop_nan(sdata->local, sdata);
230         ieee80211_sdata_stop(sdata);
231 }
232
233 static int ieee80211_nan_change_conf(struct wiphy *wiphy,
234                                      struct wireless_dev *wdev,
235                                      struct cfg80211_nan_conf *conf,
236                                      u32 changes)
237 {
238         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
239         struct cfg80211_nan_conf new_conf;
240         int ret = 0;
241
242         if (sdata->vif.type != NL80211_IFTYPE_NAN)
243                 return -EOPNOTSUPP;
244
245         if (!ieee80211_sdata_running(sdata))
246                 return -ENETDOWN;
247
248         new_conf = sdata->u.nan.conf;
249
250         if (changes & CFG80211_NAN_CONF_CHANGED_PREF)
251                 new_conf.master_pref = conf->master_pref;
252
253         if (changes & CFG80211_NAN_CONF_CHANGED_BANDS)
254                 new_conf.bands = conf->bands;
255
256         ret = drv_nan_change_conf(sdata->local, sdata, &new_conf, changes);
257         if (!ret)
258                 sdata->u.nan.conf = new_conf;
259
260         return ret;
261 }
262
263 static int ieee80211_add_nan_func(struct wiphy *wiphy,
264                                   struct wireless_dev *wdev,
265                                   struct cfg80211_nan_func *nan_func)
266 {
267         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
268         int ret;
269
270         if (sdata->vif.type != NL80211_IFTYPE_NAN)
271                 return -EOPNOTSUPP;
272
273         if (!ieee80211_sdata_running(sdata))
274                 return -ENETDOWN;
275
276         spin_lock_bh(&sdata->u.nan.func_lock);
277
278         ret = idr_alloc(&sdata->u.nan.function_inst_ids,
279                         nan_func, 1, sdata->local->hw.max_nan_de_entries + 1,
280                         GFP_ATOMIC);
281         spin_unlock_bh(&sdata->u.nan.func_lock);
282
283         if (ret < 0)
284                 return ret;
285
286         nan_func->instance_id = ret;
287
288         WARN_ON(nan_func->instance_id == 0);
289
290         ret = drv_add_nan_func(sdata->local, sdata, nan_func);
291         if (ret) {
292                 spin_lock_bh(&sdata->u.nan.func_lock);
293                 idr_remove(&sdata->u.nan.function_inst_ids,
294                            nan_func->instance_id);
295                 spin_unlock_bh(&sdata->u.nan.func_lock);
296         }
297
298         return ret;
299 }
300
301 static struct cfg80211_nan_func *
302 ieee80211_find_nan_func_by_cookie(struct ieee80211_sub_if_data *sdata,
303                                   u64 cookie)
304 {
305         struct cfg80211_nan_func *func;
306         int id;
307
308         lockdep_assert_held(&sdata->u.nan.func_lock);
309
310         idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id) {
311                 if (func->cookie == cookie)
312                         return func;
313         }
314
315         return NULL;
316 }
317
318 static void ieee80211_del_nan_func(struct wiphy *wiphy,
319                                   struct wireless_dev *wdev, u64 cookie)
320 {
321         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
322         struct cfg80211_nan_func *func;
323         u8 instance_id = 0;
324
325         if (sdata->vif.type != NL80211_IFTYPE_NAN ||
326             !ieee80211_sdata_running(sdata))
327                 return;
328
329         spin_lock_bh(&sdata->u.nan.func_lock);
330
331         func = ieee80211_find_nan_func_by_cookie(sdata, cookie);
332         if (func)
333                 instance_id = func->instance_id;
334
335         spin_unlock_bh(&sdata->u.nan.func_lock);
336
337         if (instance_id)
338                 drv_del_nan_func(sdata->local, sdata, instance_id);
339 }
340
341 static int ieee80211_set_noack_map(struct wiphy *wiphy,
342                                   struct net_device *dev,
343                                   u16 noack_map)
344 {
345         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
346
347         sdata->noack_map = noack_map;
348
349         ieee80211_check_fast_xmit_iface(sdata);
350
351         return 0;
352 }
353
354 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
355                              u8 key_idx, bool pairwise, const u8 *mac_addr,
356                              struct key_params *params)
357 {
358         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
359         struct ieee80211_local *local = sdata->local;
360         struct sta_info *sta = NULL;
361         const struct ieee80211_cipher_scheme *cs = NULL;
362         struct ieee80211_key *key;
363         int err;
364
365         if (!ieee80211_sdata_running(sdata))
366                 return -ENETDOWN;
367
368         /* reject WEP and TKIP keys if WEP failed to initialize */
369         switch (params->cipher) {
370         case WLAN_CIPHER_SUITE_WEP40:
371         case WLAN_CIPHER_SUITE_TKIP:
372         case WLAN_CIPHER_SUITE_WEP104:
373                 if (IS_ERR(local->wep_tx_tfm))
374                         return -EINVAL;
375                 break;
376         case WLAN_CIPHER_SUITE_CCMP:
377         case WLAN_CIPHER_SUITE_CCMP_256:
378         case WLAN_CIPHER_SUITE_AES_CMAC:
379         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
380         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
381         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
382         case WLAN_CIPHER_SUITE_GCMP:
383         case WLAN_CIPHER_SUITE_GCMP_256:
384                 break;
385         default:
386                 cs = ieee80211_cs_get(local, params->cipher, sdata->vif.type);
387                 break;
388         }
389
390         key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
391                                   params->key, params->seq_len, params->seq,
392                                   cs);
393         if (IS_ERR(key))
394                 return PTR_ERR(key);
395
396         if (pairwise)
397                 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
398
399         mutex_lock(&local->sta_mtx);
400
401         if (mac_addr) {
402                 sta = sta_info_get_bss(sdata, mac_addr);
403                 /*
404                  * The ASSOC test makes sure the driver is ready to
405                  * receive the key. When wpa_supplicant has roamed
406                  * using FT, it attempts to set the key before
407                  * association has completed, this rejects that attempt
408                  * so it will set the key again after association.
409                  *
410                  * TODO: accept the key if we have a station entry and
411                  *       add it to the device after the station.
412                  */
413                 if (!sta || !test_sta_flag(sta, WLAN_STA_ASSOC)) {
414                         ieee80211_key_free_unused(key);
415                         err = -ENOENT;
416                         goto out_unlock;
417                 }
418         }
419
420         switch (sdata->vif.type) {
421         case NL80211_IFTYPE_STATION:
422                 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
423                         key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
424                 break;
425         case NL80211_IFTYPE_AP:
426         case NL80211_IFTYPE_AP_VLAN:
427                 /* Keys without a station are used for TX only */
428                 if (sta && test_sta_flag(sta, WLAN_STA_MFP))
429                         key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
430                 break;
431         case NL80211_IFTYPE_ADHOC:
432                 /* no MFP (yet) */
433                 break;
434         case NL80211_IFTYPE_MESH_POINT:
435 #ifdef CONFIG_MAC80211_MESH
436                 if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
437                         key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
438                 break;
439 #endif
440         case NL80211_IFTYPE_WDS:
441         case NL80211_IFTYPE_MONITOR:
442         case NL80211_IFTYPE_P2P_DEVICE:
443         case NL80211_IFTYPE_NAN:
444         case NL80211_IFTYPE_UNSPECIFIED:
445         case NUM_NL80211_IFTYPES:
446         case NL80211_IFTYPE_P2P_CLIENT:
447         case NL80211_IFTYPE_P2P_GO:
448         case NL80211_IFTYPE_OCB:
449                 /* shouldn't happen */
450                 WARN_ON_ONCE(1);
451                 break;
452         }
453
454         if (sta)
455                 sta->cipher_scheme = cs;
456
457         err = ieee80211_key_link(key, sdata, sta);
458
459  out_unlock:
460         mutex_unlock(&local->sta_mtx);
461
462         return err;
463 }
464
465 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
466                              u8 key_idx, bool pairwise, const u8 *mac_addr)
467 {
468         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
469         struct ieee80211_local *local = sdata->local;
470         struct sta_info *sta;
471         struct ieee80211_key *key = NULL;
472         int ret;
473
474         mutex_lock(&local->sta_mtx);
475         mutex_lock(&local->key_mtx);
476
477         if (mac_addr) {
478                 ret = -ENOENT;
479
480                 sta = sta_info_get_bss(sdata, mac_addr);
481                 if (!sta)
482                         goto out_unlock;
483
484                 if (pairwise)
485                         key = key_mtx_dereference(local, sta->ptk[key_idx]);
486                 else
487                         key = key_mtx_dereference(local, sta->gtk[key_idx]);
488         } else
489                 key = key_mtx_dereference(local, sdata->keys[key_idx]);
490
491         if (!key) {
492                 ret = -ENOENT;
493                 goto out_unlock;
494         }
495
496         ieee80211_key_free(key, sdata->vif.type == NL80211_IFTYPE_STATION);
497
498         ret = 0;
499  out_unlock:
500         mutex_unlock(&local->key_mtx);
501         mutex_unlock(&local->sta_mtx);
502
503         return ret;
504 }
505
506 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
507                              u8 key_idx, bool pairwise, const u8 *mac_addr,
508                              void *cookie,
509                              void (*callback)(void *cookie,
510                                               struct key_params *params))
511 {
512         struct ieee80211_sub_if_data *sdata;
513         struct sta_info *sta = NULL;
514         u8 seq[6] = {0};
515         struct key_params params;
516         struct ieee80211_key *key = NULL;
517         u64 pn64;
518         u32 iv32;
519         u16 iv16;
520         int err = -ENOENT;
521         struct ieee80211_key_seq kseq = {};
522
523         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
524
525         rcu_read_lock();
526
527         if (mac_addr) {
528                 sta = sta_info_get_bss(sdata, mac_addr);
529                 if (!sta)
530                         goto out;
531
532                 if (pairwise && key_idx < NUM_DEFAULT_KEYS)
533                         key = rcu_dereference(sta->ptk[key_idx]);
534                 else if (!pairwise &&
535                          key_idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
536                         key = rcu_dereference(sta->gtk[key_idx]);
537         } else
538                 key = rcu_dereference(sdata->keys[key_idx]);
539
540         if (!key)
541                 goto out;
542
543         memset(&params, 0, sizeof(params));
544
545         params.cipher = key->conf.cipher;
546
547         switch (key->conf.cipher) {
548         case WLAN_CIPHER_SUITE_TKIP:
549                 pn64 = atomic64_read(&key->conf.tx_pn);
550                 iv32 = TKIP_PN_TO_IV32(pn64);
551                 iv16 = TKIP_PN_TO_IV16(pn64);
552
553                 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
554                     !(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
555                         drv_get_key_seq(sdata->local, key, &kseq);
556                         iv32 = kseq.tkip.iv32;
557                         iv16 = kseq.tkip.iv16;
558                 }
559
560                 seq[0] = iv16 & 0xff;
561                 seq[1] = (iv16 >> 8) & 0xff;
562                 seq[2] = iv32 & 0xff;
563                 seq[3] = (iv32 >> 8) & 0xff;
564                 seq[4] = (iv32 >> 16) & 0xff;
565                 seq[5] = (iv32 >> 24) & 0xff;
566                 params.seq = seq;
567                 params.seq_len = 6;
568                 break;
569         case WLAN_CIPHER_SUITE_CCMP:
570         case WLAN_CIPHER_SUITE_CCMP_256:
571         case WLAN_CIPHER_SUITE_AES_CMAC:
572         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
573                 BUILD_BUG_ON(offsetof(typeof(kseq), ccmp) !=
574                              offsetof(typeof(kseq), aes_cmac));
575                 /* fall through */
576         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
577         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
578                 BUILD_BUG_ON(offsetof(typeof(kseq), ccmp) !=
579                              offsetof(typeof(kseq), aes_gmac));
580                 /* fall through */
581         case WLAN_CIPHER_SUITE_GCMP:
582         case WLAN_CIPHER_SUITE_GCMP_256:
583                 BUILD_BUG_ON(offsetof(typeof(kseq), ccmp) !=
584                              offsetof(typeof(kseq), gcmp));
585
586                 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
587                     !(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
588                         drv_get_key_seq(sdata->local, key, &kseq);
589                         memcpy(seq, kseq.ccmp.pn, 6);
590                 } else {
591                         pn64 = atomic64_read(&key->conf.tx_pn);
592                         seq[0] = pn64;
593                         seq[1] = pn64 >> 8;
594                         seq[2] = pn64 >> 16;
595                         seq[3] = pn64 >> 24;
596                         seq[4] = pn64 >> 32;
597                         seq[5] = pn64 >> 40;
598                 }
599                 params.seq = seq;
600                 params.seq_len = 6;
601                 break;
602         default:
603                 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
604                         break;
605                 if (WARN_ON(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
606                         break;
607                 drv_get_key_seq(sdata->local, key, &kseq);
608                 params.seq = kseq.hw.seq;
609                 params.seq_len = kseq.hw.seq_len;
610                 break;
611         }
612
613         params.key = key->conf.key;
614         params.key_len = key->conf.keylen;
615
616         callback(cookie, &params);
617         err = 0;
618
619  out:
620         rcu_read_unlock();
621         return err;
622 }
623
624 static int ieee80211_config_default_key(struct wiphy *wiphy,
625                                         struct net_device *dev,
626                                         u8 key_idx, bool uni,
627                                         bool multi)
628 {
629         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
630
631         ieee80211_set_default_key(sdata, key_idx, uni, multi);
632
633         return 0;
634 }
635
636 static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
637                                              struct net_device *dev,
638                                              u8 key_idx)
639 {
640         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
641
642         ieee80211_set_default_mgmt_key(sdata, key_idx);
643
644         return 0;
645 }
646
647 void sta_set_rate_info_tx(struct sta_info *sta,
648                           const struct ieee80211_tx_rate *rate,
649                           struct rate_info *rinfo)
650 {
651         rinfo->flags = 0;
652         if (rate->flags & IEEE80211_TX_RC_MCS) {
653                 rinfo->flags |= RATE_INFO_FLAGS_MCS;
654                 rinfo->mcs = rate->idx;
655         } else if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
656                 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
657                 rinfo->mcs = ieee80211_rate_get_vht_mcs(rate);
658                 rinfo->nss = ieee80211_rate_get_vht_nss(rate);
659         } else {
660                 struct ieee80211_supported_band *sband;
661                 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
662                 u16 brate;
663
664                 sband = ieee80211_get_sband(sta->sdata);
665                 if (sband) {
666                         brate = sband->bitrates[rate->idx].bitrate;
667                         rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
668                 }
669         }
670         if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
671                 rinfo->bw = RATE_INFO_BW_40;
672         else if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
673                 rinfo->bw = RATE_INFO_BW_80;
674         else if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
675                 rinfo->bw = RATE_INFO_BW_160;
676         else
677                 rinfo->bw = RATE_INFO_BW_20;
678         if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
679                 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
680 }
681
682 static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
683                                   int idx, u8 *mac, struct station_info *sinfo)
684 {
685         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
686         struct ieee80211_local *local = sdata->local;
687         struct sta_info *sta;
688         int ret = -ENOENT;
689
690         mutex_lock(&local->sta_mtx);
691
692         sta = sta_info_get_by_idx(sdata, idx);
693         if (sta) {
694                 ret = 0;
695                 memcpy(mac, sta->sta.addr, ETH_ALEN);
696                 sta_set_sinfo(sta, sinfo, true);
697         }
698
699         mutex_unlock(&local->sta_mtx);
700
701         return ret;
702 }
703
704 static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
705                                  int idx, struct survey_info *survey)
706 {
707         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
708
709         return drv_get_survey(local, idx, survey);
710 }
711
712 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
713                                  const u8 *mac, struct station_info *sinfo)
714 {
715         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
716         struct ieee80211_local *local = sdata->local;
717         struct sta_info *sta;
718         int ret = -ENOENT;
719
720         mutex_lock(&local->sta_mtx);
721
722         sta = sta_info_get_bss(sdata, mac);
723         if (sta) {
724                 ret = 0;
725                 sta_set_sinfo(sta, sinfo, true);
726         }
727
728         mutex_unlock(&local->sta_mtx);
729
730         return ret;
731 }
732
733 static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
734                                          struct cfg80211_chan_def *chandef)
735 {
736         struct ieee80211_local *local = wiphy_priv(wiphy);
737         struct ieee80211_sub_if_data *sdata;
738         int ret = 0;
739
740         if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
741                 return 0;
742
743         mutex_lock(&local->mtx);
744         if (local->use_chanctx) {
745                 sdata = rtnl_dereference(local->monitor_sdata);
746                 if (sdata) {
747                         ieee80211_vif_release_channel(sdata);
748                         ret = ieee80211_vif_use_channel(sdata, chandef,
749                                         IEEE80211_CHANCTX_EXCLUSIVE);
750                 }
751         } else if (local->open_count == local->monitors) {
752                 local->_oper_chandef = *chandef;
753                 ieee80211_hw_config(local, 0);
754         }
755
756         if (ret == 0)
757                 local->monitor_chandef = *chandef;
758         mutex_unlock(&local->mtx);
759
760         return ret;
761 }
762
763 static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
764                                     const u8 *resp, size_t resp_len,
765                                     const struct ieee80211_csa_settings *csa)
766 {
767         struct probe_resp *new, *old;
768
769         if (!resp || !resp_len)
770                 return 1;
771
772         old = sdata_dereference(sdata->u.ap.probe_resp, sdata);
773
774         new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
775         if (!new)
776                 return -ENOMEM;
777
778         new->len = resp_len;
779         memcpy(new->data, resp, resp_len);
780
781         if (csa)
782                 memcpy(new->csa_counter_offsets, csa->counter_offsets_presp,
783                        csa->n_counter_offsets_presp *
784                        sizeof(new->csa_counter_offsets[0]));
785
786         rcu_assign_pointer(sdata->u.ap.probe_resp, new);
787         if (old)
788                 kfree_rcu(old, rcu_head);
789
790         return 0;
791 }
792
793 static int ieee80211_set_ftm_responder_params(
794                                 struct ieee80211_sub_if_data *sdata,
795                                 const u8 *lci, size_t lci_len,
796                                 const u8 *civicloc, size_t civicloc_len)
797 {
798         struct ieee80211_ftm_responder_params *new, *old;
799         struct ieee80211_bss_conf *bss_conf;
800         u8 *pos;
801         int len;
802
803         if (!lci_len && !civicloc_len)
804                 return 0;
805
806         bss_conf = &sdata->vif.bss_conf;
807         old = bss_conf->ftmr_params;
808         len = lci_len + civicloc_len;
809
810         new = kzalloc(sizeof(*new) + len, GFP_KERNEL);
811         if (!new)
812                 return -ENOMEM;
813
814         pos = (u8 *)(new + 1);
815         if (lci_len) {
816                 new->lci_len = lci_len;
817                 new->lci = pos;
818                 memcpy(pos, lci, lci_len);
819                 pos += lci_len;
820         }
821
822         if (civicloc_len) {
823                 new->civicloc_len = civicloc_len;
824                 new->civicloc = pos;
825                 memcpy(pos, civicloc, civicloc_len);
826                 pos += civicloc_len;
827         }
828
829         bss_conf->ftmr_params = new;
830         kfree(old);
831
832         return 0;
833 }
834
835 static int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
836                                    struct cfg80211_beacon_data *params,
837                                    const struct ieee80211_csa_settings *csa)
838 {
839         struct beacon_data *new, *old;
840         int new_head_len, new_tail_len;
841         int size, err;
842         u32 changed = BSS_CHANGED_BEACON;
843
844         old = sdata_dereference(sdata->u.ap.beacon, sdata);
845
846
847         /* Need to have a beacon head if we don't have one yet */
848         if (!params->head && !old)
849                 return -EINVAL;
850
851         /* new or old head? */
852         if (params->head)
853                 new_head_len = params->head_len;
854         else
855                 new_head_len = old->head_len;
856
857         /* new or old tail? */
858         if (params->tail || !old)
859                 /* params->tail_len will be zero for !params->tail */
860                 new_tail_len = params->tail_len;
861         else
862                 new_tail_len = old->tail_len;
863
864         size = sizeof(*new) + new_head_len + new_tail_len;
865
866         new = kzalloc(size, GFP_KERNEL);
867         if (!new)
868                 return -ENOMEM;
869
870         /* start filling the new info now */
871
872         /*
873          * pointers go into the block we allocated,
874          * memory is | beacon_data | head | tail |
875          */
876         new->head = ((u8 *) new) + sizeof(*new);
877         new->tail = new->head + new_head_len;
878         new->head_len = new_head_len;
879         new->tail_len = new_tail_len;
880
881         if (csa) {
882                 new->csa_current_counter = csa->count;
883                 memcpy(new->csa_counter_offsets, csa->counter_offsets_beacon,
884                        csa->n_counter_offsets_beacon *
885                        sizeof(new->csa_counter_offsets[0]));
886         }
887
888         /* copy in head */
889         if (params->head)
890                 memcpy(new->head, params->head, new_head_len);
891         else
892                 memcpy(new->head, old->head, new_head_len);
893
894         /* copy in optional tail */
895         if (params->tail)
896                 memcpy(new->tail, params->tail, new_tail_len);
897         else
898                 if (old)
899                         memcpy(new->tail, old->tail, new_tail_len);
900
901         err = ieee80211_set_probe_resp(sdata, params->probe_resp,
902                                        params->probe_resp_len, csa);
903         if (err < 0)
904                 return err;
905         if (err == 0)
906                 changed |= BSS_CHANGED_AP_PROBE_RESP;
907
908         if (params->ftm_responder != -1) {
909                 sdata->vif.bss_conf.ftm_responder = params->ftm_responder;
910                 err = ieee80211_set_ftm_responder_params(sdata,
911                                                          params->lci,
912                                                          params->lci_len,
913                                                          params->civicloc,
914                                                          params->civicloc_len);
915
916                 if (err < 0)
917                         return err;
918
919                 changed |= BSS_CHANGED_FTM_RESPONDER;
920         }
921
922         rcu_assign_pointer(sdata->u.ap.beacon, new);
923
924         if (old)
925                 kfree_rcu(old, rcu_head);
926
927         return changed;
928 }
929
930 static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
931                               struct cfg80211_ap_settings *params)
932 {
933         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
934         struct ieee80211_local *local = sdata->local;
935         struct beacon_data *old;
936         struct ieee80211_sub_if_data *vlan;
937         u32 changed = BSS_CHANGED_BEACON_INT |
938                       BSS_CHANGED_BEACON_ENABLED |
939                       BSS_CHANGED_BEACON |
940                       BSS_CHANGED_SSID |
941                       BSS_CHANGED_P2P_PS |
942                       BSS_CHANGED_TXPOWER;
943         int err;
944
945         old = sdata_dereference(sdata->u.ap.beacon, sdata);
946         if (old)
947                 return -EALREADY;
948
949         switch (params->smps_mode) {
950         case NL80211_SMPS_OFF:
951                 sdata->smps_mode = IEEE80211_SMPS_OFF;
952                 break;
953         case NL80211_SMPS_STATIC:
954                 sdata->smps_mode = IEEE80211_SMPS_STATIC;
955                 break;
956         case NL80211_SMPS_DYNAMIC:
957                 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
958                 break;
959         default:
960                 return -EINVAL;
961         }
962         sdata->u.ap.req_smps = sdata->smps_mode;
963
964         sdata->needed_rx_chains = sdata->local->rx_chains;
965
966         sdata->vif.bss_conf.beacon_int = params->beacon_interval;
967
968         if (params->he_cap)
969                 sdata->vif.bss_conf.he_support = true;
970
971         mutex_lock(&local->mtx);
972         err = ieee80211_vif_use_channel(sdata, &params->chandef,
973                                         IEEE80211_CHANCTX_SHARED);
974         if (!err)
975                 ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
976         mutex_unlock(&local->mtx);
977         if (err)
978                 return err;
979
980         /*
981          * Apply control port protocol, this allows us to
982          * not encrypt dynamic WEP control frames.
983          */
984         sdata->control_port_protocol = params->crypto.control_port_ethertype;
985         sdata->control_port_no_encrypt = params->crypto.control_port_no_encrypt;
986         sdata->control_port_over_nl80211 =
987                                 params->crypto.control_port_over_nl80211;
988         sdata->encrypt_headroom = ieee80211_cs_headroom(sdata->local,
989                                                         &params->crypto,
990                                                         sdata->vif.type);
991
992         list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
993                 vlan->control_port_protocol =
994                         params->crypto.control_port_ethertype;
995                 vlan->control_port_no_encrypt =
996                         params->crypto.control_port_no_encrypt;
997                 vlan->control_port_over_nl80211 =
998                         params->crypto.control_port_over_nl80211;
999                 vlan->encrypt_headroom =
1000                         ieee80211_cs_headroom(sdata->local,
1001                                               &params->crypto,
1002                                               vlan->vif.type);
1003         }
1004
1005         sdata->vif.bss_conf.dtim_period = params->dtim_period;
1006         sdata->vif.bss_conf.enable_beacon = true;
1007         sdata->vif.bss_conf.allow_p2p_go_ps = sdata->vif.p2p;
1008
1009         sdata->vif.bss_conf.ssid_len = params->ssid_len;
1010         if (params->ssid_len)
1011                 memcpy(sdata->vif.bss_conf.ssid, params->ssid,
1012                        params->ssid_len);
1013         sdata->vif.bss_conf.hidden_ssid =
1014                 (params->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE);
1015
1016         memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1017                sizeof(sdata->vif.bss_conf.p2p_noa_attr));
1018         sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow =
1019                 params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
1020         if (params->p2p_opp_ps)
1021                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
1022                                         IEEE80211_P2P_OPPPS_ENABLE_BIT;
1023
1024         err = ieee80211_assign_beacon(sdata, &params->beacon, NULL);
1025         if (err < 0) {
1026                 ieee80211_vif_release_channel(sdata);
1027                 return err;
1028         }
1029         changed |= err;
1030
1031         err = drv_start_ap(sdata->local, sdata);
1032         if (err) {
1033                 old = sdata_dereference(sdata->u.ap.beacon, sdata);
1034
1035                 if (old)
1036                         kfree_rcu(old, rcu_head);
1037                 RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1038                 ieee80211_vif_release_channel(sdata);
1039                 return err;
1040         }
1041
1042         ieee80211_recalc_dtim(local, sdata);
1043         ieee80211_bss_info_change_notify(sdata, changed);
1044
1045         netif_carrier_on(dev);
1046         list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1047                 netif_carrier_on(vlan->dev);
1048
1049         return 0;
1050 }
1051
1052 static int ieee80211_change_beacon(struct wiphy *wiphy, struct net_device *dev,
1053                                    struct cfg80211_beacon_data *params)
1054 {
1055         struct ieee80211_sub_if_data *sdata;
1056         struct beacon_data *old;
1057         int err;
1058
1059         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1060         sdata_assert_lock(sdata);
1061
1062         /* don't allow changing the beacon while CSA is in place - offset
1063          * of channel switch counter may change
1064          */
1065         if (sdata->vif.csa_active)
1066                 return -EBUSY;
1067
1068         old = sdata_dereference(sdata->u.ap.beacon, sdata);
1069         if (!old)
1070                 return -ENOENT;
1071
1072         err = ieee80211_assign_beacon(sdata, params, NULL);
1073         if (err < 0)
1074                 return err;
1075         ieee80211_bss_info_change_notify(sdata, err);
1076         return 0;
1077 }
1078
1079 static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1080 {
1081         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1082         struct ieee80211_sub_if_data *vlan;
1083         struct ieee80211_local *local = sdata->local;
1084         struct beacon_data *old_beacon;
1085         struct probe_resp *old_probe_resp;
1086         struct cfg80211_chan_def chandef;
1087
1088         sdata_assert_lock(sdata);
1089
1090         old_beacon = sdata_dereference(sdata->u.ap.beacon, sdata);
1091         if (!old_beacon)
1092                 return -ENOENT;
1093         old_probe_resp = sdata_dereference(sdata->u.ap.probe_resp, sdata);
1094
1095         /* abort any running channel switch */
1096         mutex_lock(&local->mtx);
1097         sdata->vif.csa_active = false;
1098         if (sdata->csa_block_tx) {
1099                 ieee80211_wake_vif_queues(local, sdata,
1100                                           IEEE80211_QUEUE_STOP_REASON_CSA);
1101                 sdata->csa_block_tx = false;
1102         }
1103
1104         mutex_unlock(&local->mtx);
1105
1106         kfree(sdata->u.ap.next_beacon);
1107         sdata->u.ap.next_beacon = NULL;
1108
1109         /* turn off carrier for this interface and dependent VLANs */
1110         list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1111                 netif_carrier_off(vlan->dev);
1112         netif_carrier_off(dev);
1113
1114         /* remove beacon and probe response */
1115         RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1116         RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
1117         kfree_rcu(old_beacon, rcu_head);
1118         if (old_probe_resp)
1119                 kfree_rcu(old_probe_resp, rcu_head);
1120         sdata->u.ap.driver_smps_mode = IEEE80211_SMPS_OFF;
1121
1122         kfree(sdata->vif.bss_conf.ftmr_params);
1123         sdata->vif.bss_conf.ftmr_params = NULL;
1124
1125         __sta_info_flush(sdata, true);
1126         ieee80211_free_keys(sdata, true);
1127
1128         sdata->vif.bss_conf.enable_beacon = false;
1129         sdata->vif.bss_conf.ssid_len = 0;
1130         clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1131         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
1132
1133         if (sdata->wdev.cac_started) {
1134                 chandef = sdata->vif.bss_conf.chandef;
1135                 cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
1136                 cfg80211_cac_event(sdata->dev, &chandef,
1137                                    NL80211_RADAR_CAC_ABORTED,
1138                                    GFP_KERNEL);
1139         }
1140
1141         drv_stop_ap(sdata->local, sdata);
1142
1143         /* free all potentially still buffered bcast frames */
1144         local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps.bc_buf);
1145         ieee80211_purge_tx_queue(&local->hw, &sdata->u.ap.ps.bc_buf);
1146
1147         mutex_lock(&local->mtx);
1148         ieee80211_vif_copy_chanctx_to_vlans(sdata, true);
1149         ieee80211_vif_release_channel(sdata);
1150         mutex_unlock(&local->mtx);
1151
1152         return 0;
1153 }
1154
1155 static int sta_apply_auth_flags(struct ieee80211_local *local,
1156                                 struct sta_info *sta,
1157                                 u32 mask, u32 set)
1158 {
1159         int ret;
1160
1161         if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1162             set & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1163             !test_sta_flag(sta, WLAN_STA_AUTH)) {
1164                 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1165                 if (ret)
1166                         return ret;
1167         }
1168
1169         if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1170             set & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1171             !test_sta_flag(sta, WLAN_STA_ASSOC)) {
1172                 /*
1173                  * When peer becomes associated, init rate control as
1174                  * well. Some drivers require rate control initialized
1175                  * before drv_sta_state() is called.
1176                  */
1177                 if (!test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1178                         rate_control_rate_init(sta);
1179
1180                 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1181                 if (ret)
1182                         return ret;
1183         }
1184
1185         if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1186                 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
1187                         ret = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
1188                 else if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1189                         ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1190                 else
1191                         ret = 0;
1192                 if (ret)
1193                         return ret;
1194         }
1195
1196         if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1197             !(set & BIT(NL80211_STA_FLAG_ASSOCIATED)) &&
1198             test_sta_flag(sta, WLAN_STA_ASSOC)) {
1199                 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1200                 if (ret)
1201                         return ret;
1202         }
1203
1204         if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1205             !(set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) &&
1206             test_sta_flag(sta, WLAN_STA_AUTH)) {
1207                 ret = sta_info_move_state(sta, IEEE80211_STA_NONE);
1208                 if (ret)
1209                         return ret;
1210         }
1211
1212         return 0;
1213 }
1214
1215 static void sta_apply_mesh_params(struct ieee80211_local *local,
1216                                   struct sta_info *sta,
1217                                   struct station_parameters *params)
1218 {
1219 #ifdef CONFIG_MAC80211_MESH
1220         struct ieee80211_sub_if_data *sdata = sta->sdata;
1221         u32 changed = 0;
1222
1223         if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE) {
1224                 switch (params->plink_state) {
1225                 case NL80211_PLINK_ESTAB:
1226                         if (sta->mesh->plink_state != NL80211_PLINK_ESTAB)
1227                                 changed = mesh_plink_inc_estab_count(sdata);
1228                         sta->mesh->plink_state = params->plink_state;
1229                         sta->mesh->aid = params->peer_aid;
1230
1231                         ieee80211_mps_sta_status_update(sta);
1232                         changed |= ieee80211_mps_set_sta_local_pm(sta,
1233                                       sdata->u.mesh.mshcfg.power_mode);
1234                         break;
1235                 case NL80211_PLINK_LISTEN:
1236                 case NL80211_PLINK_BLOCKED:
1237                 case NL80211_PLINK_OPN_SNT:
1238                 case NL80211_PLINK_OPN_RCVD:
1239                 case NL80211_PLINK_CNF_RCVD:
1240                 case NL80211_PLINK_HOLDING:
1241                         if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
1242                                 changed = mesh_plink_dec_estab_count(sdata);
1243                         sta->mesh->plink_state = params->plink_state;
1244
1245                         ieee80211_mps_sta_status_update(sta);
1246                         changed |= ieee80211_mps_set_sta_local_pm(sta,
1247                                         NL80211_MESH_POWER_UNKNOWN);
1248                         break;
1249                 default:
1250                         /*  nothing  */
1251                         break;
1252                 }
1253         }
1254
1255         switch (params->plink_action) {
1256         case NL80211_PLINK_ACTION_NO_ACTION:
1257                 /* nothing */
1258                 break;
1259         case NL80211_PLINK_ACTION_OPEN:
1260                 changed |= mesh_plink_open(sta);
1261                 break;
1262         case NL80211_PLINK_ACTION_BLOCK:
1263                 changed |= mesh_plink_block(sta);
1264                 break;
1265         }
1266
1267         if (params->local_pm)
1268                 changed |= ieee80211_mps_set_sta_local_pm(sta,
1269                                                           params->local_pm);
1270
1271         ieee80211_mbss_info_change_notify(sdata, changed);
1272 #endif
1273 }
1274
1275 static int sta_apply_parameters(struct ieee80211_local *local,
1276                                 struct sta_info *sta,
1277                                 struct station_parameters *params)
1278 {
1279         int ret = 0;
1280         struct ieee80211_supported_band *sband;
1281         struct ieee80211_sub_if_data *sdata = sta->sdata;
1282         u32 mask, set;
1283
1284         sband = ieee80211_get_sband(sdata);
1285         if (!sband)
1286                 return -EINVAL;
1287
1288         mask = params->sta_flags_mask;
1289         set = params->sta_flags_set;
1290
1291         if (ieee80211_vif_is_mesh(&sdata->vif)) {
1292                 /*
1293                  * In mesh mode, ASSOCIATED isn't part of the nl80211
1294                  * API but must follow AUTHENTICATED for driver state.
1295                  */
1296                 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1297                         mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1298                 if (set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1299                         set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1300         } else if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1301                 /*
1302                  * TDLS -- everything follows authorized, but
1303                  * only becoming authorized is possible, not
1304                  * going back
1305                  */
1306                 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1307                         set |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1308                                BIT(NL80211_STA_FLAG_ASSOCIATED);
1309                         mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1310                                 BIT(NL80211_STA_FLAG_ASSOCIATED);
1311                 }
1312         }
1313
1314         if (mask & BIT(NL80211_STA_FLAG_WME) &&
1315             local->hw.queues >= IEEE80211_NUM_ACS)
1316                 sta->sta.wme = set & BIT(NL80211_STA_FLAG_WME);
1317
1318         /* auth flags will be set later for TDLS,
1319          * and for unassociated stations that move to assocaited */
1320         if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1321             !((mask & BIT(NL80211_STA_FLAG_ASSOCIATED)) &&
1322               (set & BIT(NL80211_STA_FLAG_ASSOCIATED)))) {
1323                 ret = sta_apply_auth_flags(local, sta, mask, set);
1324                 if (ret)
1325                         return ret;
1326         }
1327
1328         if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
1329                 if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
1330                         set_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1331                 else
1332                         clear_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1333         }
1334
1335         if (mask & BIT(NL80211_STA_FLAG_MFP)) {
1336                 sta->sta.mfp = !!(set & BIT(NL80211_STA_FLAG_MFP));
1337                 if (set & BIT(NL80211_STA_FLAG_MFP))
1338                         set_sta_flag(sta, WLAN_STA_MFP);
1339                 else
1340                         clear_sta_flag(sta, WLAN_STA_MFP);
1341         }
1342
1343         if (mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
1344                 if (set & BIT(NL80211_STA_FLAG_TDLS_PEER))
1345                         set_sta_flag(sta, WLAN_STA_TDLS_PEER);
1346                 else
1347                         clear_sta_flag(sta, WLAN_STA_TDLS_PEER);
1348         }
1349
1350         /* mark TDLS channel switch support, if the AP allows it */
1351         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1352             !sdata->u.mgd.tdls_chan_switch_prohibited &&
1353             params->ext_capab_len >= 4 &&
1354             params->ext_capab[3] & WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH)
1355                 set_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH);
1356
1357         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1358             !sdata->u.mgd.tdls_wider_bw_prohibited &&
1359             ieee80211_hw_check(&local->hw, TDLS_WIDER_BW) &&
1360             params->ext_capab_len >= 8 &&
1361             params->ext_capab[7] & WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED)
1362                 set_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW);
1363
1364         if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD) {
1365                 sta->sta.uapsd_queues = params->uapsd_queues;
1366                 sta->sta.max_sp = params->max_sp;
1367         }
1368
1369         /* The sender might not have sent the last bit, consider it to be 0 */
1370         if (params->ext_capab_len >= 8) {
1371                 u8 val = (params->ext_capab[7] &
1372                           WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB) >> 7;
1373
1374                 /* we did get all the bits, take the MSB as well */
1375                 if (params->ext_capab_len >= 9) {
1376                         u8 val_msb = params->ext_capab[8] &
1377                                 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB;
1378                         val_msb <<= 1;
1379                         val |= val_msb;
1380                 }
1381
1382                 switch (val) {
1383                 case 1:
1384                         sta->sta.max_amsdu_subframes = 32;
1385                         break;
1386                 case 2:
1387                         sta->sta.max_amsdu_subframes = 16;
1388                         break;
1389                 case 3:
1390                         sta->sta.max_amsdu_subframes = 8;
1391                         break;
1392                 default:
1393                         sta->sta.max_amsdu_subframes = 0;
1394                 }
1395         }
1396
1397         /*
1398          * cfg80211 validates this (1-2007) and allows setting the AID
1399          * only when creating a new station entry
1400          */
1401         if (params->aid)
1402                 sta->sta.aid = params->aid;
1403
1404         /*
1405          * Some of the following updates would be racy if called on an
1406          * existing station, via ieee80211_change_station(). However,
1407          * all such changes are rejected by cfg80211 except for updates
1408          * changing the supported rates on an existing but not yet used
1409          * TDLS peer.
1410          */
1411
1412         if (params->listen_interval >= 0)
1413                 sta->listen_interval = params->listen_interval;
1414
1415         if (params->supported_rates) {
1416                 ieee80211_parse_bitrates(&sdata->vif.bss_conf.chandef,
1417                                          sband, params->supported_rates,
1418                                          params->supported_rates_len,
1419                                          &sta->sta.supp_rates[sband->band]);
1420         }
1421
1422         if (params->ht_capa)
1423                 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1424                                                   params->ht_capa, sta);
1425
1426         /* VHT can override some HT caps such as the A-MSDU max length */
1427         if (params->vht_capa)
1428                 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1429                                                     params->vht_capa, sta);
1430
1431         if (params->he_capa)
1432                 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
1433                                                   (void *)params->he_capa,
1434                                                   params->he_capa_len, sta);
1435
1436         if (params->opmode_notif_used) {
1437                 /* returned value is only needed for rc update, but the
1438                  * rc isn't initialized here yet, so ignore it
1439                  */
1440                 __ieee80211_vht_handle_opmode(sdata, sta, params->opmode_notif,
1441                                               sband->band);
1442         }
1443
1444         if (params->support_p2p_ps >= 0)
1445                 sta->sta.support_p2p_ps = params->support_p2p_ps;
1446
1447         if (ieee80211_vif_is_mesh(&sdata->vif))
1448                 sta_apply_mesh_params(local, sta, params);
1449
1450         /* set the STA state after all sta info from usermode has been set */
1451         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) ||
1452             set & BIT(NL80211_STA_FLAG_ASSOCIATED)) {
1453                 ret = sta_apply_auth_flags(local, sta, mask, set);
1454                 if (ret)
1455                         return ret;
1456         }
1457
1458         return 0;
1459 }
1460
1461 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
1462                                  const u8 *mac,
1463                                  struct station_parameters *params)
1464 {
1465         struct ieee80211_local *local = wiphy_priv(wiphy);
1466         struct sta_info *sta;
1467         struct ieee80211_sub_if_data *sdata;
1468         int err;
1469         int layer2_update;
1470
1471         if (params->vlan) {
1472                 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1473
1474                 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1475                     sdata->vif.type != NL80211_IFTYPE_AP)
1476                         return -EINVAL;
1477         } else
1478                 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1479
1480         if (ether_addr_equal(mac, sdata->vif.addr))
1481                 return -EINVAL;
1482
1483         if (is_multicast_ether_addr(mac))
1484                 return -EINVAL;
1485
1486         sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
1487         if (!sta)
1488                 return -ENOMEM;
1489
1490         if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
1491                 sta->sta.tdls = true;
1492
1493         if (sta->sta.tdls && sdata->vif.type == NL80211_IFTYPE_STATION &&
1494             !sdata->u.mgd.associated)
1495                 return -EINVAL;
1496
1497         err = sta_apply_parameters(local, sta, params);
1498         if (err) {
1499                 sta_info_free(local, sta);
1500                 return err;
1501         }
1502
1503         /*
1504          * for TDLS and for unassociated station, rate control should be
1505          * initialized only when rates are known and station is marked
1506          * authorized/associated
1507          */
1508         if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1509             test_sta_flag(sta, WLAN_STA_ASSOC))
1510                 rate_control_rate_init(sta);
1511
1512         layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1513                 sdata->vif.type == NL80211_IFTYPE_AP;
1514
1515         err = sta_info_insert_rcu(sta);
1516         if (err) {
1517                 rcu_read_unlock();
1518                 return err;
1519         }
1520
1521         if (layer2_update)
1522                 cfg80211_send_layer2_update(sta->sdata->dev, sta->sta.addr);
1523
1524         rcu_read_unlock();
1525
1526         return 0;
1527 }
1528
1529 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1530                                  struct station_del_parameters *params)
1531 {
1532         struct ieee80211_sub_if_data *sdata;
1533
1534         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1535
1536         if (params->mac)
1537                 return sta_info_destroy_addr_bss(sdata, params->mac);
1538
1539         sta_info_flush(sdata);
1540         return 0;
1541 }
1542
1543 static int ieee80211_change_station(struct wiphy *wiphy,
1544                                     struct net_device *dev, const u8 *mac,
1545                                     struct station_parameters *params)
1546 {
1547         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1548         struct ieee80211_local *local = wiphy_priv(wiphy);
1549         struct sta_info *sta;
1550         struct ieee80211_sub_if_data *vlansdata;
1551         enum cfg80211_station_type statype;
1552         int err;
1553
1554         mutex_lock(&local->sta_mtx);
1555
1556         sta = sta_info_get_bss(sdata, mac);
1557         if (!sta) {
1558                 err = -ENOENT;
1559                 goto out_err;
1560         }
1561
1562         switch (sdata->vif.type) {
1563         case NL80211_IFTYPE_MESH_POINT:
1564                 if (sdata->u.mesh.user_mpm)
1565                         statype = CFG80211_STA_MESH_PEER_USER;
1566                 else
1567                         statype = CFG80211_STA_MESH_PEER_KERNEL;
1568                 break;
1569         case NL80211_IFTYPE_ADHOC:
1570                 statype = CFG80211_STA_IBSS;
1571                 break;
1572         case NL80211_IFTYPE_STATION:
1573                 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1574                         statype = CFG80211_STA_AP_STA;
1575                         break;
1576                 }
1577                 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1578                         statype = CFG80211_STA_TDLS_PEER_ACTIVE;
1579                 else
1580                         statype = CFG80211_STA_TDLS_PEER_SETUP;
1581                 break;
1582         case NL80211_IFTYPE_AP:
1583         case NL80211_IFTYPE_AP_VLAN:
1584                 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1585                         statype = CFG80211_STA_AP_CLIENT;
1586                 else
1587                         statype = CFG80211_STA_AP_CLIENT_UNASSOC;
1588                 break;
1589         default:
1590                 err = -EOPNOTSUPP;
1591                 goto out_err;
1592         }
1593
1594         err = cfg80211_check_station_change(wiphy, params, statype);
1595         if (err)
1596                 goto out_err;
1597
1598         if (params->vlan && params->vlan != sta->sdata->dev) {
1599                 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1600
1601                 if (params->vlan->ieee80211_ptr->use_4addr) {
1602                         if (vlansdata->u.vlan.sta) {
1603                                 err = -EBUSY;
1604                                 goto out_err;
1605                         }
1606
1607                         rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
1608                         __ieee80211_check_fast_rx_iface(vlansdata);
1609                 }
1610
1611                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1612                     sta->sdata->u.vlan.sta)
1613                         RCU_INIT_POINTER(sta->sdata->u.vlan.sta, NULL);
1614
1615                 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1616                         ieee80211_vif_dec_num_mcast(sta->sdata);
1617
1618                 sta->sdata = vlansdata;
1619                 ieee80211_check_fast_xmit(sta);
1620
1621                 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1622                         ieee80211_vif_inc_num_mcast(sta->sdata);
1623
1624                 cfg80211_send_layer2_update(sta->sdata->dev, sta->sta.addr);
1625         }
1626
1627         err = sta_apply_parameters(local, sta, params);
1628         if (err)
1629                 goto out_err;
1630
1631         mutex_unlock(&local->sta_mtx);
1632
1633         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1634              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1635             sta->known_smps_mode != sta->sdata->bss->req_smps &&
1636             test_sta_flag(sta, WLAN_STA_AUTHORIZED) &&
1637             sta_info_tx_streams(sta) != 1) {
1638                 ht_dbg(sta->sdata,
1639                        "%pM just authorized and MIMO capable - update SMPS\n",
1640                        sta->sta.addr);
1641                 ieee80211_send_smps_action(sta->sdata,
1642                         sta->sdata->bss->req_smps,
1643                         sta->sta.addr,
1644                         sta->sdata->vif.bss_conf.bssid);
1645         }
1646
1647         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1648             params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1649                 ieee80211_recalc_ps(local);
1650                 ieee80211_recalc_ps_vif(sdata);
1651         }
1652
1653         return 0;
1654 out_err:
1655         mutex_unlock(&local->sta_mtx);
1656         return err;
1657 }
1658
1659 #ifdef CONFIG_MAC80211_MESH
1660 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
1661                                const u8 *dst, const u8 *next_hop)
1662 {
1663         struct ieee80211_sub_if_data *sdata;
1664         struct mesh_path *mpath;
1665         struct sta_info *sta;
1666
1667         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1668
1669         rcu_read_lock();
1670         sta = sta_info_get(sdata, next_hop);
1671         if (!sta) {
1672                 rcu_read_unlock();
1673                 return -ENOENT;
1674         }
1675
1676         mpath = mesh_path_add(sdata, dst);
1677         if (IS_ERR(mpath)) {
1678                 rcu_read_unlock();
1679                 return PTR_ERR(mpath);
1680         }
1681
1682         mesh_path_fix_nexthop(mpath, sta);
1683
1684         rcu_read_unlock();
1685         return 0;
1686 }
1687
1688 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
1689                                const u8 *dst)
1690 {
1691         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1692
1693         if (dst)
1694                 return mesh_path_del(sdata, dst);
1695
1696         mesh_path_flush_by_iface(sdata);
1697         return 0;
1698 }
1699
1700 static int ieee80211_change_mpath(struct wiphy *wiphy, struct net_device *dev,
1701                                   const u8 *dst, const u8 *next_hop)
1702 {
1703         struct ieee80211_sub_if_data *sdata;
1704         struct mesh_path *mpath;
1705         struct sta_info *sta;
1706
1707         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1708
1709         rcu_read_lock();
1710
1711         sta = sta_info_get(sdata, next_hop);
1712         if (!sta) {
1713                 rcu_read_unlock();
1714                 return -ENOENT;
1715         }
1716
1717         mpath = mesh_path_lookup(sdata, dst);
1718         if (!mpath) {
1719                 rcu_read_unlock();
1720                 return -ENOENT;
1721         }
1722
1723         mesh_path_fix_nexthop(mpath, sta);
1724
1725         rcu_read_unlock();
1726         return 0;
1727 }
1728
1729 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
1730                             struct mpath_info *pinfo)
1731 {
1732         struct sta_info *next_hop_sta = rcu_dereference(mpath->next_hop);
1733
1734         if (next_hop_sta)
1735                 memcpy(next_hop, next_hop_sta->sta.addr, ETH_ALEN);
1736         else
1737                 eth_zero_addr(next_hop);
1738
1739         memset(pinfo, 0, sizeof(*pinfo));
1740
1741         pinfo->generation = mpath->sdata->u.mesh.mesh_paths_generation;
1742
1743         pinfo->filled = MPATH_INFO_FRAME_QLEN |
1744                         MPATH_INFO_SN |
1745                         MPATH_INFO_METRIC |
1746                         MPATH_INFO_EXPTIME |
1747                         MPATH_INFO_DISCOVERY_TIMEOUT |
1748                         MPATH_INFO_DISCOVERY_RETRIES |
1749                         MPATH_INFO_FLAGS;
1750
1751         pinfo->frame_qlen = mpath->frame_queue.qlen;
1752         pinfo->sn = mpath->sn;
1753         pinfo->metric = mpath->metric;
1754         if (time_before(jiffies, mpath->exp_time))
1755                 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
1756         pinfo->discovery_timeout =
1757                         jiffies_to_msecs(mpath->discovery_timeout);
1758         pinfo->discovery_retries = mpath->discovery_retries;
1759         if (mpath->flags & MESH_PATH_ACTIVE)
1760                 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
1761         if (mpath->flags & MESH_PATH_RESOLVING)
1762                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
1763         if (mpath->flags & MESH_PATH_SN_VALID)
1764                 pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
1765         if (mpath->flags & MESH_PATH_FIXED)
1766                 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
1767         if (mpath->flags & MESH_PATH_RESOLVED)
1768                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVED;
1769 }
1770
1771 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
1772                                u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
1773
1774 {
1775         struct ieee80211_sub_if_data *sdata;
1776         struct mesh_path *mpath;
1777
1778         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1779
1780         rcu_read_lock();
1781         mpath = mesh_path_lookup(sdata, dst);
1782         if (!mpath) {
1783                 rcu_read_unlock();
1784                 return -ENOENT;
1785         }
1786         memcpy(dst, mpath->dst, ETH_ALEN);
1787         mpath_set_pinfo(mpath, next_hop, pinfo);
1788         rcu_read_unlock();
1789         return 0;
1790 }
1791
1792 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
1793                                 int idx, u8 *dst, u8 *next_hop,
1794                                 struct mpath_info *pinfo)
1795 {
1796         struct ieee80211_sub_if_data *sdata;
1797         struct mesh_path *mpath;
1798
1799         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1800
1801         rcu_read_lock();
1802         mpath = mesh_path_lookup_by_idx(sdata, idx);
1803         if (!mpath) {
1804                 rcu_read_unlock();
1805                 return -ENOENT;
1806         }
1807         memcpy(dst, mpath->dst, ETH_ALEN);
1808         mpath_set_pinfo(mpath, next_hop, pinfo);
1809         rcu_read_unlock();
1810         return 0;
1811 }
1812
1813 static void mpp_set_pinfo(struct mesh_path *mpath, u8 *mpp,
1814                           struct mpath_info *pinfo)
1815 {
1816         memset(pinfo, 0, sizeof(*pinfo));
1817         memcpy(mpp, mpath->mpp, ETH_ALEN);
1818
1819         pinfo->generation = mpath->sdata->u.mesh.mpp_paths_generation;
1820 }
1821
1822 static int ieee80211_get_mpp(struct wiphy *wiphy, struct net_device *dev,
1823                              u8 *dst, u8 *mpp, struct mpath_info *pinfo)
1824
1825 {
1826         struct ieee80211_sub_if_data *sdata;
1827         struct mesh_path *mpath;
1828
1829         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1830
1831         rcu_read_lock();
1832         mpath = mpp_path_lookup(sdata, dst);
1833         if (!mpath) {
1834                 rcu_read_unlock();
1835                 return -ENOENT;
1836         }
1837         memcpy(dst, mpath->dst, ETH_ALEN);
1838         mpp_set_pinfo(mpath, mpp, pinfo);
1839         rcu_read_unlock();
1840         return 0;
1841 }
1842
1843 static int ieee80211_dump_mpp(struct wiphy *wiphy, struct net_device *dev,
1844                               int idx, u8 *dst, u8 *mpp,
1845                               struct mpath_info *pinfo)
1846 {
1847         struct ieee80211_sub_if_data *sdata;
1848         struct mesh_path *mpath;
1849
1850         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1851
1852         rcu_read_lock();
1853         mpath = mpp_path_lookup_by_idx(sdata, idx);
1854         if (!mpath) {
1855                 rcu_read_unlock();
1856                 return -ENOENT;
1857         }
1858         memcpy(dst, mpath->dst, ETH_ALEN);
1859         mpp_set_pinfo(mpath, mpp, pinfo);
1860         rcu_read_unlock();
1861         return 0;
1862 }
1863
1864 static int ieee80211_get_mesh_config(struct wiphy *wiphy,
1865                                 struct net_device *dev,
1866                                 struct mesh_config *conf)
1867 {
1868         struct ieee80211_sub_if_data *sdata;
1869         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1870
1871         memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
1872         return 0;
1873 }
1874
1875 static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
1876 {
1877         return (mask >> (parm-1)) & 0x1;
1878 }
1879
1880 static int copy_mesh_setup(struct ieee80211_if_mesh *ifmsh,
1881                 const struct mesh_setup *setup)
1882 {
1883         u8 *new_ie;
1884         const u8 *old_ie;
1885         struct ieee80211_sub_if_data *sdata = container_of(ifmsh,
1886                                         struct ieee80211_sub_if_data, u.mesh);
1887
1888         /* allocate information elements */
1889         new_ie = NULL;
1890         old_ie = ifmsh->ie;
1891
1892         if (setup->ie_len) {
1893                 new_ie = kmemdup(setup->ie, setup->ie_len,
1894                                 GFP_KERNEL);
1895                 if (!new_ie)
1896                         return -ENOMEM;
1897         }
1898         ifmsh->ie_len = setup->ie_len;
1899         ifmsh->ie = new_ie;
1900         kfree(old_ie);
1901
1902         /* now copy the rest of the setup parameters */
1903         ifmsh->mesh_id_len = setup->mesh_id_len;
1904         memcpy(ifmsh->mesh_id, setup->mesh_id, ifmsh->mesh_id_len);
1905         ifmsh->mesh_sp_id = setup->sync_method;
1906         ifmsh->mesh_pp_id = setup->path_sel_proto;
1907         ifmsh->mesh_pm_id = setup->path_metric;
1908         ifmsh->user_mpm = setup->user_mpm;
1909         ifmsh->mesh_auth_id = setup->auth_id;
1910         ifmsh->security = IEEE80211_MESH_SEC_NONE;
1911         ifmsh->userspace_handles_dfs = setup->userspace_handles_dfs;
1912         if (setup->is_authenticated)
1913                 ifmsh->security |= IEEE80211_MESH_SEC_AUTHED;
1914         if (setup->is_secure)
1915                 ifmsh->security |= IEEE80211_MESH_SEC_SECURED;
1916
1917         /* mcast rate setting in Mesh Node */
1918         memcpy(sdata->vif.bss_conf.mcast_rate, setup->mcast_rate,
1919                                                 sizeof(setup->mcast_rate));
1920         sdata->vif.bss_conf.basic_rates = setup->basic_rates;
1921
1922         sdata->vif.bss_conf.beacon_int = setup->beacon_interval;
1923         sdata->vif.bss_conf.dtim_period = setup->dtim_period;
1924
1925         return 0;
1926 }
1927
1928 static int ieee80211_update_mesh_config(struct wiphy *wiphy,
1929                                         struct net_device *dev, u32 mask,
1930                                         const struct mesh_config *nconf)
1931 {
1932         struct mesh_config *conf;
1933         struct ieee80211_sub_if_data *sdata;
1934         struct ieee80211_if_mesh *ifmsh;
1935
1936         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1937         ifmsh = &sdata->u.mesh;
1938
1939         /* Set the config options which we are interested in setting */
1940         conf = &(sdata->u.mesh.mshcfg);
1941         if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1942                 conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1943         if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1944                 conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1945         if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1946                 conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1947         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1948                 conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1949         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1950                 conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1951         if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1952                 conf->dot11MeshTTL = nconf->dot11MeshTTL;
1953         if (_chg_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask))
1954                 conf->element_ttl = nconf->element_ttl;
1955         if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask)) {
1956                 if (ifmsh->user_mpm)
1957                         return -EBUSY;
1958                 conf->auto_open_plinks = nconf->auto_open_plinks;
1959         }
1960         if (_chg_mesh_attr(NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR, mask))
1961                 conf->dot11MeshNbrOffsetMaxNeighbor =
1962                         nconf->dot11MeshNbrOffsetMaxNeighbor;
1963         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1964                 conf->dot11MeshHWMPmaxPREQretries =
1965                         nconf->dot11MeshHWMPmaxPREQretries;
1966         if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1967                 conf->path_refresh_time = nconf->path_refresh_time;
1968         if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1969                 conf->min_discovery_timeout = nconf->min_discovery_timeout;
1970         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1971                 conf->dot11MeshHWMPactivePathTimeout =
1972                         nconf->dot11MeshHWMPactivePathTimeout;
1973         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1974                 conf->dot11MeshHWMPpreqMinInterval =
1975                         nconf->dot11MeshHWMPpreqMinInterval;
1976         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL, mask))
1977                 conf->dot11MeshHWMPperrMinInterval =
1978                         nconf->dot11MeshHWMPperrMinInterval;
1979         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1980                            mask))
1981                 conf->dot11MeshHWMPnetDiameterTraversalTime =
1982                         nconf->dot11MeshHWMPnetDiameterTraversalTime;
1983         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
1984                 conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
1985                 ieee80211_mesh_root_setup(ifmsh);
1986         }
1987         if (_chg_mesh_attr(NL80211_MESHCONF_GATE_ANNOUNCEMENTS, mask)) {
1988                 /* our current gate announcement implementation rides on root
1989                  * announcements, so require this ifmsh to also be a root node
1990                  * */
1991                 if (nconf->dot11MeshGateAnnouncementProtocol &&
1992                     !(conf->dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)) {
1993                         conf->dot11MeshHWMPRootMode = IEEE80211_PROACTIVE_RANN;
1994                         ieee80211_mesh_root_setup(ifmsh);
1995                 }
1996                 conf->dot11MeshGateAnnouncementProtocol =
1997                         nconf->dot11MeshGateAnnouncementProtocol;
1998         }
1999         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_RANN_INTERVAL, mask))
2000                 conf->dot11MeshHWMPRannInterval =
2001                         nconf->dot11MeshHWMPRannInterval;
2002         if (_chg_mesh_attr(NL80211_MESHCONF_FORWARDING, mask))
2003                 conf->dot11MeshForwarding = nconf->dot11MeshForwarding;
2004         if (_chg_mesh_attr(NL80211_MESHCONF_RSSI_THRESHOLD, mask)) {
2005                 /* our RSSI threshold implementation is supported only for
2006                  * devices that report signal in dBm.
2007                  */
2008                 if (!ieee80211_hw_check(&sdata->local->hw, SIGNAL_DBM))
2009                         return -ENOTSUPP;
2010                 conf->rssi_threshold = nconf->rssi_threshold;
2011         }
2012         if (_chg_mesh_attr(NL80211_MESHCONF_HT_OPMODE, mask)) {
2013                 conf->ht_opmode = nconf->ht_opmode;
2014                 sdata->vif.bss_conf.ht_operation_mode = nconf->ht_opmode;
2015                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
2016         }
2017         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT, mask))
2018                 conf->dot11MeshHWMPactivePathToRootTimeout =
2019                         nconf->dot11MeshHWMPactivePathToRootTimeout;
2020         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOT_INTERVAL, mask))
2021                 conf->dot11MeshHWMProotInterval =
2022                         nconf->dot11MeshHWMProotInterval;
2023         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL, mask))
2024                 conf->dot11MeshHWMPconfirmationInterval =
2025                         nconf->dot11MeshHWMPconfirmationInterval;
2026         if (_chg_mesh_attr(NL80211_MESHCONF_POWER_MODE, mask)) {
2027                 conf->power_mode = nconf->power_mode;
2028                 ieee80211_mps_local_status_update(sdata);
2029         }
2030         if (_chg_mesh_attr(NL80211_MESHCONF_AWAKE_WINDOW, mask))
2031                 conf->dot11MeshAwakeWindowDuration =
2032                         nconf->dot11MeshAwakeWindowDuration;
2033         if (_chg_mesh_attr(NL80211_MESHCONF_PLINK_TIMEOUT, mask))
2034                 conf->plink_timeout = nconf->plink_timeout;
2035         if (_chg_mesh_attr(NL80211_MESHCONF_CONNECTED_TO_GATE, mask))
2036                 conf->dot11MeshConnectedToMeshGate =
2037                         nconf->dot11MeshConnectedToMeshGate;
2038         ieee80211_mbss_info_change_notify(sdata, BSS_CHANGED_BEACON);
2039         return 0;
2040 }
2041
2042 static int ieee80211_join_mesh(struct wiphy *wiphy, struct net_device *dev,
2043                                const struct mesh_config *conf,
2044                                const struct mesh_setup *setup)
2045 {
2046         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2047         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2048         int err;
2049
2050         memcpy(&ifmsh->mshcfg, conf, sizeof(struct mesh_config));
2051         err = copy_mesh_setup(ifmsh, setup);
2052         if (err)
2053                 return err;
2054
2055         sdata->control_port_over_nl80211 = setup->control_port_over_nl80211;
2056
2057         /* can mesh use other SMPS modes? */
2058         sdata->smps_mode = IEEE80211_SMPS_OFF;
2059         sdata->needed_rx_chains = sdata->local->rx_chains;
2060
2061         mutex_lock(&sdata->local->mtx);
2062         err = ieee80211_vif_use_channel(sdata, &setup->chandef,
2063                                         IEEE80211_CHANCTX_SHARED);
2064         mutex_unlock(&sdata->local->mtx);
2065         if (err)
2066                 return err;
2067
2068         return ieee80211_start_mesh(sdata);
2069 }
2070
2071 static int ieee80211_leave_mesh(struct wiphy *wiphy, struct net_device *dev)
2072 {
2073         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2074
2075         ieee80211_stop_mesh(sdata);
2076         mutex_lock(&sdata->local->mtx);
2077         ieee80211_vif_release_channel(sdata);
2078         mutex_unlock(&sdata->local->mtx);
2079
2080         return 0;
2081 }
2082 #endif
2083
2084 static int ieee80211_change_bss(struct wiphy *wiphy,
2085                                 struct net_device *dev,
2086                                 struct bss_parameters *params)
2087 {
2088         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2089         struct ieee80211_supported_band *sband;
2090         u32 changed = 0;
2091
2092         if (!sdata_dereference(sdata->u.ap.beacon, sdata))
2093                 return -ENOENT;
2094
2095         sband = ieee80211_get_sband(sdata);
2096         if (!sband)
2097                 return -EINVAL;
2098
2099         if (params->use_cts_prot >= 0) {
2100                 sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
2101                 changed |= BSS_CHANGED_ERP_CTS_PROT;
2102         }
2103         if (params->use_short_preamble >= 0) {
2104                 sdata->vif.bss_conf.use_short_preamble =
2105                         params->use_short_preamble;
2106                 changed |= BSS_CHANGED_ERP_PREAMBLE;
2107         }
2108
2109         if (!sdata->vif.bss_conf.use_short_slot &&
2110             sband->band == NL80211_BAND_5GHZ) {
2111                 sdata->vif.bss_conf.use_short_slot = true;
2112                 changed |= BSS_CHANGED_ERP_SLOT;
2113         }
2114
2115         if (params->use_short_slot_time >= 0) {
2116                 sdata->vif.bss_conf.use_short_slot =
2117                         params->use_short_slot_time;
2118                 changed |= BSS_CHANGED_ERP_SLOT;
2119         }
2120
2121         if (params->basic_rates) {
2122                 ieee80211_parse_bitrates(&sdata->vif.bss_conf.chandef,
2123                                          wiphy->bands[sband->band],
2124                                          params->basic_rates,
2125                                          params->basic_rates_len,
2126                                          &sdata->vif.bss_conf.basic_rates);
2127                 changed |= BSS_CHANGED_BASIC_RATES;
2128                 ieee80211_check_rate_mask(sdata);
2129         }
2130
2131         if (params->ap_isolate >= 0) {
2132                 if (params->ap_isolate)
2133                         sdata->flags |= IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
2134                 else
2135                         sdata->flags &= ~IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
2136                 ieee80211_check_fast_rx_iface(sdata);
2137         }
2138
2139         if (params->ht_opmode >= 0) {
2140                 sdata->vif.bss_conf.ht_operation_mode =
2141                         (u16) params->ht_opmode;
2142                 changed |= BSS_CHANGED_HT;
2143         }
2144
2145         if (params->p2p_ctwindow >= 0) {
2146                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow &=
2147                                         ~IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
2148                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
2149                         params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
2150                 changed |= BSS_CHANGED_P2P_PS;
2151         }
2152
2153         if (params->p2p_opp_ps > 0) {
2154                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
2155                                         IEEE80211_P2P_OPPPS_ENABLE_BIT;
2156                 changed |= BSS_CHANGED_P2P_PS;
2157         } else if (params->p2p_opp_ps == 0) {
2158                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow &=
2159                                         ~IEEE80211_P2P_OPPPS_ENABLE_BIT;
2160                 changed |= BSS_CHANGED_P2P_PS;
2161         }
2162
2163         ieee80211_bss_info_change_notify(sdata, changed);
2164
2165         return 0;
2166 }
2167
2168 static int ieee80211_set_txq_params(struct wiphy *wiphy,
2169                                     struct net_device *dev,
2170                                     struct ieee80211_txq_params *params)
2171 {
2172         struct ieee80211_local *local = wiphy_priv(wiphy);
2173         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2174         struct ieee80211_tx_queue_params p;
2175
2176         if (!local->ops->conf_tx)
2177                 return -EOPNOTSUPP;
2178
2179         if (local->hw.queues < IEEE80211_NUM_ACS)
2180                 return -EOPNOTSUPP;
2181
2182         memset(&p, 0, sizeof(p));
2183         p.aifs = params->aifs;
2184         p.cw_max = params->cwmax;
2185         p.cw_min = params->cwmin;
2186         p.txop = params->txop;
2187
2188         /*
2189          * Setting tx queue params disables u-apsd because it's only
2190          * called in master mode.
2191          */
2192         p.uapsd = false;
2193
2194         ieee80211_regulatory_limit_wmm_params(sdata, &p, params->ac);
2195
2196         sdata->tx_conf[params->ac] = p;
2197         if (drv_conf_tx(local, sdata, params->ac, &p)) {
2198                 wiphy_debug(local->hw.wiphy,
2199                             "failed to set TX queue parameters for AC %d\n",
2200                             params->ac);
2201                 return -EINVAL;
2202         }
2203
2204         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
2205
2206         return 0;
2207 }
2208
2209 #ifdef CONFIG_PM
2210 static int ieee80211_suspend(struct wiphy *wiphy,
2211                              struct cfg80211_wowlan *wowlan)
2212 {
2213         return __ieee80211_suspend(wiphy_priv(wiphy), wowlan);
2214 }
2215
2216 static int ieee80211_resume(struct wiphy *wiphy)
2217 {
2218         return __ieee80211_resume(wiphy_priv(wiphy));
2219 }
2220 #else
2221 #define ieee80211_suspend NULL
2222 #define ieee80211_resume NULL
2223 #endif
2224
2225 static int ieee80211_scan(struct wiphy *wiphy,
2226                           struct cfg80211_scan_request *req)
2227 {
2228         struct ieee80211_sub_if_data *sdata;
2229
2230         sdata = IEEE80211_WDEV_TO_SUB_IF(req->wdev);
2231
2232         switch (ieee80211_vif_type_p2p(&sdata->vif)) {
2233         case NL80211_IFTYPE_STATION:
2234         case NL80211_IFTYPE_ADHOC:
2235         case NL80211_IFTYPE_MESH_POINT:
2236         case NL80211_IFTYPE_P2P_CLIENT:
2237         case NL80211_IFTYPE_P2P_DEVICE:
2238                 break;
2239         case NL80211_IFTYPE_P2P_GO:
2240                 if (sdata->local->ops->hw_scan)
2241                         break;
2242                 /*
2243                  * FIXME: implement NoA while scanning in software,
2244                  * for now fall through to allow scanning only when
2245                  * beaconing hasn't been configured yet
2246                  */
2247                 /* fall through */
2248         case NL80211_IFTYPE_AP:
2249                 /*
2250                  * If the scan has been forced (and the driver supports
2251                  * forcing), don't care about being beaconing already.
2252                  * This will create problems to the attached stations (e.g. all
2253                  * the  frames sent while scanning on other channel will be
2254                  * lost)
2255                  */
2256                 if (sdata->u.ap.beacon &&
2257                     (!(wiphy->features & NL80211_FEATURE_AP_SCAN) ||
2258                      !(req->flags & NL80211_SCAN_FLAG_AP)))
2259                         return -EOPNOTSUPP;
2260                 break;
2261         case NL80211_IFTYPE_NAN:
2262         default:
2263                 return -EOPNOTSUPP;
2264         }
2265
2266         return ieee80211_request_scan(sdata, req);
2267 }
2268
2269 static void ieee80211_abort_scan(struct wiphy *wiphy, struct wireless_dev *wdev)
2270 {
2271         ieee80211_scan_cancel(wiphy_priv(wiphy));
2272 }
2273
2274 static int
2275 ieee80211_sched_scan_start(struct wiphy *wiphy,
2276                            struct net_device *dev,
2277                            struct cfg80211_sched_scan_request *req)
2278 {
2279         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2280
2281         if (!sdata->local->ops->sched_scan_start)
2282                 return -EOPNOTSUPP;
2283
2284         return ieee80211_request_sched_scan_start(sdata, req);
2285 }
2286
2287 static int
2288 ieee80211_sched_scan_stop(struct wiphy *wiphy, struct net_device *dev,
2289                           u64 reqid)
2290 {
2291         struct ieee80211_local *local = wiphy_priv(wiphy);
2292
2293         if (!local->ops->sched_scan_stop)
2294                 return -EOPNOTSUPP;
2295
2296         return ieee80211_request_sched_scan_stop(local);
2297 }
2298
2299 static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
2300                           struct cfg80211_auth_request *req)
2301 {
2302         return ieee80211_mgd_auth(IEEE80211_DEV_TO_SUB_IF(dev), req);
2303 }
2304
2305 static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
2306                            struct cfg80211_assoc_request *req)
2307 {
2308         return ieee80211_mgd_assoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
2309 }
2310
2311 static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
2312                             struct cfg80211_deauth_request *req)
2313 {
2314         return ieee80211_mgd_deauth(IEEE80211_DEV_TO_SUB_IF(dev), req);
2315 }
2316
2317 static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
2318                               struct cfg80211_disassoc_request *req)
2319 {
2320         return ieee80211_mgd_disassoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
2321 }
2322
2323 static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2324                                struct cfg80211_ibss_params *params)
2325 {
2326         return ieee80211_ibss_join(IEEE80211_DEV_TO_SUB_IF(dev), params);
2327 }
2328
2329 static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2330 {
2331         return ieee80211_ibss_leave(IEEE80211_DEV_TO_SUB_IF(dev));
2332 }
2333
2334 static int ieee80211_join_ocb(struct wiphy *wiphy, struct net_device *dev,
2335                               struct ocb_setup *setup)
2336 {
2337         return ieee80211_ocb_join(IEEE80211_DEV_TO_SUB_IF(dev), setup);
2338 }
2339
2340 static int ieee80211_leave_ocb(struct wiphy *wiphy, struct net_device *dev)
2341 {
2342         return ieee80211_ocb_leave(IEEE80211_DEV_TO_SUB_IF(dev));
2343 }
2344
2345 static int ieee80211_set_mcast_rate(struct wiphy *wiphy, struct net_device *dev,
2346                                     int rate[NUM_NL80211_BANDS])
2347 {
2348         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2349
2350         memcpy(sdata->vif.bss_conf.mcast_rate, rate,
2351                sizeof(int) * NUM_NL80211_BANDS);
2352
2353         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_MCAST_RATE);
2354
2355         return 0;
2356 }
2357
2358 static int ieee80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
2359 {
2360         struct ieee80211_local *local = wiphy_priv(wiphy);
2361         int err;
2362
2363         if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
2364                 ieee80211_check_fast_xmit_all(local);
2365
2366                 err = drv_set_frag_threshold(local, wiphy->frag_threshold);
2367
2368                 if (err) {
2369                         ieee80211_check_fast_xmit_all(local);
2370                         return err;
2371                 }
2372         }
2373
2374         if ((changed & WIPHY_PARAM_COVERAGE_CLASS) ||
2375             (changed & WIPHY_PARAM_DYN_ACK)) {
2376                 s16 coverage_class;
2377
2378                 coverage_class = changed & WIPHY_PARAM_COVERAGE_CLASS ?
2379                                         wiphy->coverage_class : -1;
2380                 err = drv_set_coverage_class(local, coverage_class);
2381
2382                 if (err)
2383                         return err;
2384         }
2385
2386         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
2387                 err = drv_set_rts_threshold(local, wiphy->rts_threshold);
2388
2389                 if (err)
2390                         return err;
2391         }
2392
2393         if (changed & WIPHY_PARAM_RETRY_SHORT) {
2394                 if (wiphy->retry_short > IEEE80211_MAX_TX_RETRY)
2395                         return -EINVAL;
2396                 local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
2397         }
2398         if (changed & WIPHY_PARAM_RETRY_LONG) {
2399                 if (wiphy->retry_long > IEEE80211_MAX_TX_RETRY)
2400                         return -EINVAL;
2401                 local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
2402         }
2403         if (changed &
2404             (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
2405                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
2406
2407         if (changed & (WIPHY_PARAM_TXQ_LIMIT |
2408                        WIPHY_PARAM_TXQ_MEMORY_LIMIT |
2409                        WIPHY_PARAM_TXQ_QUANTUM))
2410                 ieee80211_txq_set_params(local);
2411
2412         return 0;
2413 }
2414
2415 static int ieee80211_set_tx_power(struct wiphy *wiphy,
2416                                   struct wireless_dev *wdev,
2417                                   enum nl80211_tx_power_setting type, int mbm)
2418 {
2419         struct ieee80211_local *local = wiphy_priv(wiphy);
2420         struct ieee80211_sub_if_data *sdata;
2421         enum nl80211_tx_power_setting txp_type = type;
2422         bool update_txp_type = false;
2423         bool has_monitor = false;
2424
2425         if (wdev) {
2426                 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2427
2428                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
2429                         sdata = rtnl_dereference(local->monitor_sdata);
2430                         if (!sdata)
2431                                 return -EOPNOTSUPP;
2432                 }
2433
2434                 switch (type) {
2435                 case NL80211_TX_POWER_AUTOMATIC:
2436                         sdata->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
2437                         txp_type = NL80211_TX_POWER_LIMITED;
2438                         break;
2439                 case NL80211_TX_POWER_LIMITED:
2440                 case NL80211_TX_POWER_FIXED:
2441                         if (mbm < 0 || (mbm % 100))
2442                                 return -EOPNOTSUPP;
2443                         sdata->user_power_level = MBM_TO_DBM(mbm);
2444                         break;
2445                 }
2446
2447                 if (txp_type != sdata->vif.bss_conf.txpower_type) {
2448                         update_txp_type = true;
2449                         sdata->vif.bss_conf.txpower_type = txp_type;
2450                 }
2451
2452                 ieee80211_recalc_txpower(sdata, update_txp_type);
2453
2454                 return 0;
2455         }
2456
2457         switch (type) {
2458         case NL80211_TX_POWER_AUTOMATIC:
2459                 local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
2460                 txp_type = NL80211_TX_POWER_LIMITED;
2461                 break;
2462         case NL80211_TX_POWER_LIMITED:
2463         case NL80211_TX_POWER_FIXED:
2464                 if (mbm < 0 || (mbm % 100))
2465                         return -EOPNOTSUPP;
2466                 local->user_power_level = MBM_TO_DBM(mbm);
2467                 break;
2468         }
2469
2470         mutex_lock(&local->iflist_mtx);
2471         list_for_each_entry(sdata, &local->interfaces, list) {
2472                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
2473                         has_monitor = true;
2474                         continue;
2475                 }
2476                 sdata->user_power_level = local->user_power_level;
2477                 if (txp_type != sdata->vif.bss_conf.txpower_type)
2478                         update_txp_type = true;
2479                 sdata->vif.bss_conf.txpower_type = txp_type;
2480         }
2481         list_for_each_entry(sdata, &local->interfaces, list) {
2482                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
2483                         continue;
2484                 ieee80211_recalc_txpower(sdata, update_txp_type);
2485         }
2486         mutex_unlock(&local->iflist_mtx);
2487
2488         if (has_monitor) {
2489                 sdata = rtnl_dereference(local->monitor_sdata);
2490                 if (sdata) {
2491                         sdata->user_power_level = local->user_power_level;
2492                         if (txp_type != sdata->vif.bss_conf.txpower_type)
2493                                 update_txp_type = true;
2494                         sdata->vif.bss_conf.txpower_type = txp_type;
2495
2496                         ieee80211_recalc_txpower(sdata, update_txp_type);
2497                 }
2498         }
2499
2500         return 0;
2501 }
2502
2503 static int ieee80211_get_tx_power(struct wiphy *wiphy,
2504                                   struct wireless_dev *wdev,
2505                                   int *dbm)
2506 {
2507         struct ieee80211_local *local = wiphy_priv(wiphy);
2508         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2509
2510         if (local->ops->get_txpower)
2511                 return drv_get_txpower(local, sdata, dbm);
2512
2513         if (!local->use_chanctx)
2514                 *dbm = local->hw.conf.power_level;
2515         else
2516                 *dbm = sdata->vif.bss_conf.txpower;
2517
2518         return 0;
2519 }
2520
2521 static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
2522                                   const u8 *addr)
2523 {
2524         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2525
2526         memcpy(&sdata->u.wds.remote_addr, addr, ETH_ALEN);
2527
2528         return 0;
2529 }
2530
2531 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
2532 {
2533         struct ieee80211_local *local = wiphy_priv(wiphy);
2534
2535         drv_rfkill_poll(local);
2536 }
2537
2538 #ifdef CONFIG_NL80211_TESTMODE
2539 static int ieee80211_testmode_cmd(struct wiphy *wiphy,
2540                                   struct wireless_dev *wdev,
2541                                   void *data, int len)
2542 {
2543         struct ieee80211_local *local = wiphy_priv(wiphy);
2544         struct ieee80211_vif *vif = NULL;
2545
2546         if (!local->ops->testmode_cmd)
2547                 return -EOPNOTSUPP;
2548
2549         if (wdev) {
2550                 struct ieee80211_sub_if_data *sdata;
2551
2552                 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2553                 if (sdata->flags & IEEE80211_SDATA_IN_DRIVER)
2554                         vif = &sdata->vif;
2555         }
2556
2557         return local->ops->testmode_cmd(&local->hw, vif, data, len);
2558 }
2559
2560 static int ieee80211_testmode_dump(struct wiphy *wiphy,
2561                                    struct sk_buff *skb,
2562                                    struct netlink_callback *cb,
2563                                    void *data, int len)
2564 {
2565         struct ieee80211_local *local = wiphy_priv(wiphy);
2566
2567         if (!local->ops->testmode_dump)
2568                 return -EOPNOTSUPP;
2569
2570         return local->ops->testmode_dump(&local->hw, skb, cb, data, len);
2571 }
2572 #endif
2573
2574 int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
2575                                 enum ieee80211_smps_mode smps_mode)
2576 {
2577         struct sta_info *sta;
2578         enum ieee80211_smps_mode old_req;
2579
2580         if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP))
2581                 return -EINVAL;
2582
2583         if (sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2584                 return 0;
2585
2586         old_req = sdata->u.ap.req_smps;
2587         sdata->u.ap.req_smps = smps_mode;
2588
2589         /* AUTOMATIC doesn't mean much for AP - don't allow it */
2590         if (old_req == smps_mode ||
2591             smps_mode == IEEE80211_SMPS_AUTOMATIC)
2592                 return 0;
2593
2594         ht_dbg(sdata,
2595                "SMPS %d requested in AP mode, sending Action frame to %d stations\n",
2596                smps_mode, atomic_read(&sdata->u.ap.num_mcast_sta));
2597
2598         mutex_lock(&sdata->local->sta_mtx);
2599         list_for_each_entry(sta, &sdata->local->sta_list, list) {
2600                 /*
2601                  * Only stations associated to our AP and
2602                  * associated VLANs
2603                  */
2604                 if (sta->sdata->bss != &sdata->u.ap)
2605                         continue;
2606
2607                 /* This station doesn't support MIMO - skip it */
2608                 if (sta_info_tx_streams(sta) == 1)
2609                         continue;
2610
2611                 /*
2612                  * Don't wake up a STA just to send the action frame
2613                  * unless we are getting more restrictive.
2614                  */
2615                 if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
2616                     !ieee80211_smps_is_restrictive(sta->known_smps_mode,
2617                                                    smps_mode)) {
2618                         ht_dbg(sdata, "Won't send SMPS to sleeping STA %pM\n",
2619                                sta->sta.addr);
2620                         continue;
2621                 }
2622
2623                 /*
2624                  * If the STA is not authorized, wait until it gets
2625                  * authorized and the action frame will be sent then.
2626                  */
2627                 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2628                         continue;
2629
2630                 ht_dbg(sdata, "Sending SMPS to %pM\n", sta->sta.addr);
2631                 ieee80211_send_smps_action(sdata, smps_mode, sta->sta.addr,
2632                                            sdata->vif.bss_conf.bssid);
2633         }
2634         mutex_unlock(&sdata->local->sta_mtx);
2635
2636         sdata->smps_mode = smps_mode;
2637         ieee80211_queue_work(&sdata->local->hw, &sdata->recalc_smps);
2638
2639         return 0;
2640 }
2641
2642 int __ieee80211_request_smps_mgd(struct ieee80211_sub_if_data *sdata,
2643                                  enum ieee80211_smps_mode smps_mode)
2644 {
2645         const u8 *ap;
2646         enum ieee80211_smps_mode old_req;
2647         int err;
2648         struct sta_info *sta;
2649         bool tdls_peer_found = false;
2650
2651         lockdep_assert_held(&sdata->wdev.mtx);
2652
2653         if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
2654                 return -EINVAL;
2655
2656         old_req = sdata->u.mgd.req_smps;
2657         sdata->u.mgd.req_smps = smps_mode;
2658
2659         if (old_req == smps_mode &&
2660             smps_mode != IEEE80211_SMPS_AUTOMATIC)
2661                 return 0;
2662
2663         /*
2664          * If not associated, or current association is not an HT
2665          * association, there's no need to do anything, just store
2666          * the new value until we associate.
2667          */
2668         if (!sdata->u.mgd.associated ||
2669             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2670                 return 0;
2671
2672         ap = sdata->u.mgd.associated->bssid;
2673
2674         rcu_read_lock();
2675         list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
2676                 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
2677                     !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2678                         continue;
2679
2680                 tdls_peer_found = true;
2681                 break;
2682         }
2683         rcu_read_unlock();
2684
2685         if (smps_mode == IEEE80211_SMPS_AUTOMATIC) {
2686                 if (tdls_peer_found || !sdata->u.mgd.powersave)
2687                         smps_mode = IEEE80211_SMPS_OFF;
2688                 else
2689                         smps_mode = IEEE80211_SMPS_DYNAMIC;
2690         }
2691
2692         /* send SM PS frame to AP */
2693         err = ieee80211_send_smps_action(sdata, smps_mode,
2694                                          ap, ap);
2695         if (err)
2696                 sdata->u.mgd.req_smps = old_req;
2697         else if (smps_mode != IEEE80211_SMPS_OFF && tdls_peer_found)
2698                 ieee80211_teardown_tdls_peers(sdata);
2699
2700         return err;
2701 }
2702
2703 static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2704                                     bool enabled, int timeout)
2705 {
2706         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2707         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2708
2709         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2710                 return -EOPNOTSUPP;
2711
2712         if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
2713                 return -EOPNOTSUPP;
2714
2715         if (enabled == sdata->u.mgd.powersave &&
2716             timeout == local->dynamic_ps_forced_timeout)
2717                 return 0;
2718
2719         sdata->u.mgd.powersave = enabled;
2720         local->dynamic_ps_forced_timeout = timeout;
2721
2722         /* no change, but if automatic follow powersave */
2723         sdata_lock(sdata);
2724         __ieee80211_request_smps_mgd(sdata, sdata->u.mgd.req_smps);
2725         sdata_unlock(sdata);
2726
2727         if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
2728                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2729
2730         ieee80211_recalc_ps(local);
2731         ieee80211_recalc_ps_vif(sdata);
2732         ieee80211_check_fast_rx_iface(sdata);
2733
2734         return 0;
2735 }
2736
2737 static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
2738                                          struct net_device *dev,
2739                                          s32 rssi_thold, u32 rssi_hyst)
2740 {
2741         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2742         struct ieee80211_vif *vif = &sdata->vif;
2743         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
2744
2745         if (rssi_thold == bss_conf->cqm_rssi_thold &&
2746             rssi_hyst == bss_conf->cqm_rssi_hyst)
2747                 return 0;
2748
2749         if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER &&
2750             !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
2751                 return -EOPNOTSUPP;
2752
2753         bss_conf->cqm_rssi_thold = rssi_thold;
2754         bss_conf->cqm_rssi_hyst = rssi_hyst;
2755         bss_conf->cqm_rssi_low = 0;
2756         bss_conf->cqm_rssi_high = 0;
2757         sdata->u.mgd.last_cqm_event_signal = 0;
2758
2759         /* tell the driver upon association, unless already associated */
2760         if (sdata->u.mgd.associated &&
2761             sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)
2762                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
2763
2764         return 0;
2765 }
2766
2767 static int ieee80211_set_cqm_rssi_range_config(struct wiphy *wiphy,
2768                                                struct net_device *dev,
2769                                                s32 rssi_low, s32 rssi_high)
2770 {
2771         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2772         struct ieee80211_vif *vif = &sdata->vif;
2773         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
2774
2775         if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
2776                 return -EOPNOTSUPP;
2777
2778         bss_conf->cqm_rssi_low = rssi_low;
2779         bss_conf->cqm_rssi_high = rssi_high;
2780         bss_conf->cqm_rssi_thold = 0;
2781         bss_conf->cqm_rssi_hyst = 0;
2782         sdata->u.mgd.last_cqm_event_signal = 0;
2783
2784         /* tell the driver upon association, unless already associated */
2785         if (sdata->u.mgd.associated &&
2786             sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)
2787                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
2788
2789         return 0;
2790 }
2791
2792 static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
2793                                       struct net_device *dev,
2794                                       const u8 *addr,
2795                                       const struct cfg80211_bitrate_mask *mask)
2796 {
2797         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2798         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2799         int i, ret;
2800
2801         if (!ieee80211_sdata_running(sdata))
2802                 return -ENETDOWN;
2803
2804         /*
2805          * If active validate the setting and reject it if it doesn't leave
2806          * at least one basic rate usable, since we really have to be able
2807          * to send something, and if we're an AP we have to be able to do
2808          * so at a basic rate so that all clients can receive it.
2809          */
2810         if (rcu_access_pointer(sdata->vif.chanctx_conf) &&
2811             sdata->vif.bss_conf.chandef.chan) {
2812                 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2813                 enum nl80211_band band = sdata->vif.bss_conf.chandef.chan->band;
2814
2815                 if (!(mask->control[band].legacy & basic_rates))
2816                         return -EINVAL;
2817         }
2818
2819         if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
2820                 ret = drv_set_bitrate_mask(local, sdata, mask);
2821                 if (ret)
2822                         return ret;
2823         }
2824
2825         for (i = 0; i < NUM_NL80211_BANDS; i++) {
2826                 struct ieee80211_supported_band *sband = wiphy->bands[i];
2827                 int j;
2828
2829                 sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
2830                 memcpy(sdata->rc_rateidx_mcs_mask[i], mask->control[i].ht_mcs,
2831                        sizeof(mask->control[i].ht_mcs));
2832                 memcpy(sdata->rc_rateidx_vht_mcs_mask[i],
2833                        mask->control[i].vht_mcs,
2834                        sizeof(mask->control[i].vht_mcs));
2835
2836                 sdata->rc_has_mcs_mask[i] = false;
2837                 sdata->rc_has_vht_mcs_mask[i] = false;
2838                 if (!sband)
2839                         continue;
2840
2841                 for (j = 0; j < IEEE80211_HT_MCS_MASK_LEN; j++) {
2842                         if (~sdata->rc_rateidx_mcs_mask[i][j]) {
2843                                 sdata->rc_has_mcs_mask[i] = true;
2844                                 break;
2845                         }
2846                 }
2847
2848                 for (j = 0; j < NL80211_VHT_NSS_MAX; j++) {
2849                         if (~sdata->rc_rateidx_vht_mcs_mask[i][j]) {
2850                                 sdata->rc_has_vht_mcs_mask[i] = true;
2851                                 break;
2852                         }
2853                 }
2854         }
2855
2856         return 0;
2857 }
2858
2859 static int ieee80211_start_radar_detection(struct wiphy *wiphy,
2860                                            struct net_device *dev,
2861                                            struct cfg80211_chan_def *chandef,
2862                                            u32 cac_time_ms)
2863 {
2864         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2865         struct ieee80211_local *local = sdata->local;
2866         int err;
2867
2868         mutex_lock(&local->mtx);
2869         if (!list_empty(&local->roc_list) || local->scanning) {
2870                 err = -EBUSY;
2871                 goto out_unlock;
2872         }
2873
2874         /* whatever, but channel contexts should not complain about that one */
2875         sdata->smps_mode = IEEE80211_SMPS_OFF;
2876         sdata->needed_rx_chains = local->rx_chains;
2877
2878         err = ieee80211_vif_use_channel(sdata, chandef,
2879                                         IEEE80211_CHANCTX_SHARED);
2880         if (err)
2881                 goto out_unlock;
2882
2883         ieee80211_queue_delayed_work(&sdata->local->hw,
2884                                      &sdata->dfs_cac_timer_work,
2885                                      msecs_to_jiffies(cac_time_ms));
2886
2887  out_unlock:
2888         mutex_unlock(&local->mtx);
2889         return err;
2890 }
2891
2892 static struct cfg80211_beacon_data *
2893 cfg80211_beacon_dup(struct cfg80211_beacon_data *beacon)
2894 {
2895         struct cfg80211_beacon_data *new_beacon;
2896         u8 *pos;
2897         int len;
2898
2899         len = beacon->head_len + beacon->tail_len + beacon->beacon_ies_len +
2900               beacon->proberesp_ies_len + beacon->assocresp_ies_len +
2901               beacon->probe_resp_len + beacon->lci_len + beacon->civicloc_len;
2902
2903         new_beacon = kzalloc(sizeof(*new_beacon) + len, GFP_KERNEL);
2904         if (!new_beacon)
2905                 return NULL;
2906
2907         pos = (u8 *)(new_beacon + 1);
2908         if (beacon->head_len) {
2909                 new_beacon->head_len = beacon->head_len;
2910                 new_beacon->head = pos;
2911                 memcpy(pos, beacon->head, beacon->head_len);
2912                 pos += beacon->head_len;
2913         }
2914         if (beacon->tail_len) {
2915                 new_beacon->tail_len = beacon->tail_len;
2916                 new_beacon->tail = pos;
2917                 memcpy(pos, beacon->tail, beacon->tail_len);
2918                 pos += beacon->tail_len;
2919         }
2920         if (beacon->beacon_ies_len) {
2921                 new_beacon->beacon_ies_len = beacon->beacon_ies_len;
2922                 new_beacon->beacon_ies = pos;
2923                 memcpy(pos, beacon->beacon_ies, beacon->beacon_ies_len);
2924                 pos += beacon->beacon_ies_len;
2925         }
2926         if (beacon->proberesp_ies_len) {
2927                 new_beacon->proberesp_ies_len = beacon->proberesp_ies_len;
2928                 new_beacon->proberesp_ies = pos;
2929                 memcpy(pos, beacon->proberesp_ies, beacon->proberesp_ies_len);
2930                 pos += beacon->proberesp_ies_len;
2931         }
2932         if (beacon->assocresp_ies_len) {
2933                 new_beacon->assocresp_ies_len = beacon->assocresp_ies_len;
2934                 new_beacon->assocresp_ies = pos;
2935                 memcpy(pos, beacon->assocresp_ies, beacon->assocresp_ies_len);
2936                 pos += beacon->assocresp_ies_len;
2937         }
2938         if (beacon->probe_resp_len) {
2939                 new_beacon->probe_resp_len = beacon->probe_resp_len;
2940                 new_beacon->probe_resp = pos;
2941                 memcpy(pos, beacon->probe_resp, beacon->probe_resp_len);
2942                 pos += beacon->probe_resp_len;
2943         }
2944
2945         /* might copy -1, meaning no changes requested */
2946         new_beacon->ftm_responder = beacon->ftm_responder;
2947         if (beacon->lci) {
2948                 new_beacon->lci_len = beacon->lci_len;
2949                 new_beacon->lci = pos;
2950                 memcpy(pos, beacon->lci, beacon->lci_len);
2951                 pos += beacon->lci_len;
2952         }
2953         if (beacon->civicloc) {
2954                 new_beacon->civicloc_len = beacon->civicloc_len;
2955                 new_beacon->civicloc = pos;
2956                 memcpy(pos, beacon->civicloc, beacon->civicloc_len);
2957                 pos += beacon->civicloc_len;
2958         }
2959
2960         return new_beacon;
2961 }
2962
2963 void ieee80211_csa_finish(struct ieee80211_vif *vif)
2964 {
2965         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2966
2967         ieee80211_queue_work(&sdata->local->hw,
2968                              &sdata->csa_finalize_work);
2969 }
2970 EXPORT_SYMBOL(ieee80211_csa_finish);
2971
2972 static int ieee80211_set_after_csa_beacon(struct ieee80211_sub_if_data *sdata,
2973                                           u32 *changed)
2974 {
2975         int err;
2976
2977         switch (sdata->vif.type) {
2978         case NL80211_IFTYPE_AP:
2979                 err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon,
2980                                               NULL);
2981                 kfree(sdata->u.ap.next_beacon);
2982                 sdata->u.ap.next_beacon = NULL;
2983
2984                 if (err < 0)
2985                         return err;
2986                 *changed |= err;
2987                 break;
2988         case NL80211_IFTYPE_ADHOC:
2989                 err = ieee80211_ibss_finish_csa(sdata);
2990                 if (err < 0)
2991                         return err;
2992                 *changed |= err;
2993                 break;
2994 #ifdef CONFIG_MAC80211_MESH
2995         case NL80211_IFTYPE_MESH_POINT:
2996                 err = ieee80211_mesh_finish_csa(sdata);
2997                 if (err < 0)
2998                         return err;
2999                 *changed |= err;
3000                 break;
3001 #endif
3002         default:
3003                 WARN_ON(1);
3004                 return -EINVAL;
3005         }
3006
3007         return 0;
3008 }
3009
3010 static int __ieee80211_csa_finalize(struct ieee80211_sub_if_data *sdata)
3011 {
3012         struct ieee80211_local *local = sdata->local;
3013         u32 changed = 0;
3014         int err;
3015
3016         sdata_assert_lock(sdata);
3017         lockdep_assert_held(&local->mtx);
3018         lockdep_assert_held(&local->chanctx_mtx);
3019
3020         /*
3021          * using reservation isn't immediate as it may be deferred until later
3022          * with multi-vif. once reservation is complete it will re-schedule the
3023          * work with no reserved_chanctx so verify chandef to check if it
3024          * completed successfully
3025          */
3026
3027         if (sdata->reserved_chanctx) {
3028                 /*
3029                  * with multi-vif csa driver may call ieee80211_csa_finish()
3030                  * many times while waiting for other interfaces to use their
3031                  * reservations
3032                  */
3033                 if (sdata->reserved_ready)
3034                         return 0;
3035
3036                 return ieee80211_vif_use_reserved_context(sdata);
3037         }
3038
3039         if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
3040                                         &sdata->csa_chandef))
3041                 return -EINVAL;
3042
3043         sdata->vif.csa_active = false;
3044
3045         err = ieee80211_set_after_csa_beacon(sdata, &changed);
3046         if (err)
3047                 return err;
3048
3049         ieee80211_bss_info_change_notify(sdata, changed);
3050
3051         if (sdata->csa_block_tx) {
3052                 ieee80211_wake_vif_queues(local, sdata,
3053                                           IEEE80211_QUEUE_STOP_REASON_CSA);
3054                 sdata->csa_block_tx = false;
3055         }
3056
3057         err = drv_post_channel_switch(sdata);
3058         if (err)
3059                 return err;
3060
3061         cfg80211_ch_switch_notify(sdata->dev, &sdata->csa_chandef);
3062
3063         return 0;
3064 }
3065
3066 static void ieee80211_csa_finalize(struct ieee80211_sub_if_data *sdata)
3067 {
3068         if (__ieee80211_csa_finalize(sdata)) {
3069                 sdata_info(sdata, "failed to finalize CSA, disconnecting\n");
3070                 cfg80211_stop_iface(sdata->local->hw.wiphy, &sdata->wdev,
3071                                     GFP_KERNEL);
3072         }
3073 }
3074
3075 void ieee80211_csa_finalize_work(struct work_struct *work)
3076 {
3077         struct ieee80211_sub_if_data *sdata =
3078                 container_of(work, struct ieee80211_sub_if_data,
3079                              csa_finalize_work);
3080         struct ieee80211_local *local = sdata->local;
3081
3082         sdata_lock(sdata);
3083         mutex_lock(&local->mtx);
3084         mutex_lock(&local->chanctx_mtx);
3085
3086         /* AP might have been stopped while waiting for the lock. */
3087         if (!sdata->vif.csa_active)
3088                 goto unlock;
3089
3090         if (!ieee80211_sdata_running(sdata))
3091                 goto unlock;
3092
3093         ieee80211_csa_finalize(sdata);
3094
3095 unlock:
3096         mutex_unlock(&local->chanctx_mtx);
3097         mutex_unlock(&local->mtx);
3098         sdata_unlock(sdata);
3099 }
3100
3101 static int ieee80211_set_csa_beacon(struct ieee80211_sub_if_data *sdata,
3102                                     struct cfg80211_csa_settings *params,
3103                                     u32 *changed)
3104 {
3105         struct ieee80211_csa_settings csa = {};
3106         int err;
3107
3108         switch (sdata->vif.type) {
3109         case NL80211_IFTYPE_AP:
3110                 sdata->u.ap.next_beacon =
3111                         cfg80211_beacon_dup(&params->beacon_after);
3112                 if (!sdata->u.ap.next_beacon)
3113                         return -ENOMEM;
3114
3115                 /*
3116                  * With a count of 0, we don't have to wait for any
3117                  * TBTT before switching, so complete the CSA
3118                  * immediately.  In theory, with a count == 1 we
3119                  * should delay the switch until just before the next
3120                  * TBTT, but that would complicate things so we switch
3121                  * immediately too.  If we would delay the switch
3122                  * until the next TBTT, we would have to set the probe
3123                  * response here.
3124                  *
3125                  * TODO: A channel switch with count <= 1 without
3126                  * sending a CSA action frame is kind of useless,
3127                  * because the clients won't know we're changing
3128                  * channels.  The action frame must be implemented
3129                  * either here or in the userspace.
3130                  */
3131                 if (params->count <= 1)
3132                         break;
3133
3134                 if ((params->n_counter_offsets_beacon >
3135                      IEEE80211_MAX_CSA_COUNTERS_NUM) ||
3136                     (params->n_counter_offsets_presp >
3137                      IEEE80211_MAX_CSA_COUNTERS_NUM))
3138                         return -EINVAL;
3139
3140                 csa.counter_offsets_beacon = params->counter_offsets_beacon;
3141                 csa.counter_offsets_presp = params->counter_offsets_presp;
3142                 csa.n_counter_offsets_beacon = params->n_counter_offsets_beacon;
3143                 csa.n_counter_offsets_presp = params->n_counter_offsets_presp;
3144                 csa.count = params->count;
3145
3146                 err = ieee80211_assign_beacon(sdata, &params->beacon_csa, &csa);
3147                 if (err < 0) {
3148                         kfree(sdata->u.ap.next_beacon);
3149                         return err;
3150                 }
3151                 *changed |= err;
3152
3153                 break;
3154         case NL80211_IFTYPE_ADHOC:
3155                 if (!sdata->vif.bss_conf.ibss_joined)
3156                         return -EINVAL;
3157
3158                 if (params->chandef.width != sdata->u.ibss.chandef.width)
3159                         return -EINVAL;
3160
3161                 switch (params->chandef.width) {
3162                 case NL80211_CHAN_WIDTH_40:
3163                         if (cfg80211_get_chandef_type(&params->chandef) !=
3164                             cfg80211_get_chandef_type(&sdata->u.ibss.chandef))
3165                                 return -EINVAL;
3166                 case NL80211_CHAN_WIDTH_5:
3167                 case NL80211_CHAN_WIDTH_10:
3168                 case NL80211_CHAN_WIDTH_20_NOHT:
3169                 case NL80211_CHAN_WIDTH_20:
3170                         break;
3171                 default:
3172                         return -EINVAL;
3173                 }
3174
3175                 /* changes into another band are not supported */
3176                 if (sdata->u.ibss.chandef.chan->band !=
3177                     params->chandef.chan->band)
3178                         return -EINVAL;
3179
3180                 /* see comments in the NL80211_IFTYPE_AP block */
3181                 if (params->count > 1) {
3182                         err = ieee80211_ibss_csa_beacon(sdata, params);
3183                         if (err < 0)
3184                                 return err;
3185                         *changed |= err;
3186                 }
3187
3188                 ieee80211_send_action_csa(sdata, params);
3189
3190                 break;
3191 #ifdef CONFIG_MAC80211_MESH
3192         case NL80211_IFTYPE_MESH_POINT: {
3193                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3194
3195                 if (params->chandef.width != sdata->vif.bss_conf.chandef.width)
3196                         return -EINVAL;
3197
3198                 /* changes into another band are not supported */
3199                 if (sdata->vif.bss_conf.chandef.chan->band !=
3200                     params->chandef.chan->band)
3201                         return -EINVAL;
3202
3203                 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_NONE) {
3204                         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_INIT;
3205                         if (!ifmsh->pre_value)
3206                                 ifmsh->pre_value = 1;
3207                         else
3208                                 ifmsh->pre_value++;
3209                 }
3210
3211                 /* see comments in the NL80211_IFTYPE_AP block */
3212                 if (params->count > 1) {
3213                         err = ieee80211_mesh_csa_beacon(sdata, params);
3214                         if (err < 0) {
3215                                 ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
3216                                 return err;
3217                         }
3218                         *changed |= err;
3219                 }
3220
3221                 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT)
3222                         ieee80211_send_action_csa(sdata, params);
3223
3224                 break;
3225                 }
3226 #endif
3227         default:
3228                 return -EOPNOTSUPP;
3229         }
3230
3231         return 0;
3232 }
3233
3234 static int
3235 __ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3236                            struct cfg80211_csa_settings *params)
3237 {
3238         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3239         struct ieee80211_local *local = sdata->local;
3240         struct ieee80211_channel_switch ch_switch;
3241         struct ieee80211_chanctx_conf *conf;
3242         struct ieee80211_chanctx *chanctx;
3243         u32 changed = 0;
3244         int err;
3245
3246         sdata_assert_lock(sdata);
3247         lockdep_assert_held(&local->mtx);
3248
3249         if (!list_empty(&local->roc_list) || local->scanning)
3250                 return -EBUSY;
3251
3252         if (sdata->wdev.cac_started)
3253                 return -EBUSY;
3254
3255         if (cfg80211_chandef_identical(&params->chandef,
3256                                        &sdata->vif.bss_conf.chandef))
3257                 return -EINVAL;
3258
3259         /* don't allow another channel switch if one is already active. */
3260         if (sdata->vif.csa_active)
3261                 return -EBUSY;
3262
3263         mutex_lock(&local->chanctx_mtx);
3264         conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
3265                                          lockdep_is_held(&local->chanctx_mtx));
3266         if (!conf) {
3267                 err = -EBUSY;
3268                 goto out;
3269         }
3270
3271         chanctx = container_of(conf, struct ieee80211_chanctx, conf);
3272
3273         ch_switch.timestamp = 0;
3274         ch_switch.device_timestamp = 0;
3275         ch_switch.block_tx = params->block_tx;
3276         ch_switch.chandef = params->chandef;
3277         ch_switch.count = params->count;
3278
3279         err = drv_pre_channel_switch(sdata, &ch_switch);
3280         if (err)
3281                 goto out;
3282
3283         err = ieee80211_vif_reserve_chanctx(sdata, &params->chandef,
3284                                             chanctx->mode,
3285                                             params->radar_required);
3286         if (err)
3287                 goto out;
3288
3289         /* if reservation is invalid then this will fail */
3290         err = ieee80211_check_combinations(sdata, NULL, chanctx->mode, 0);
3291         if (err) {
3292                 ieee80211_vif_unreserve_chanctx(sdata);
3293                 goto out;
3294         }
3295
3296         err = ieee80211_set_csa_beacon(sdata, params, &changed);
3297         if (err) {
3298                 ieee80211_vif_unreserve_chanctx(sdata);
3299                 goto out;
3300         }
3301
3302         sdata->csa_chandef = params->chandef;
3303         sdata->csa_block_tx = params->block_tx;
3304         sdata->vif.csa_active = true;
3305
3306         if (sdata->csa_block_tx)
3307                 ieee80211_stop_vif_queues(local, sdata,
3308                                           IEEE80211_QUEUE_STOP_REASON_CSA);
3309
3310         cfg80211_ch_switch_started_notify(sdata->dev, &sdata->csa_chandef,
3311                                           params->count);
3312
3313         if (changed) {
3314                 ieee80211_bss_info_change_notify(sdata, changed);
3315                 drv_channel_switch_beacon(sdata, &params->chandef);
3316         } else {
3317                 /* if the beacon didn't change, we can finalize immediately */
3318                 ieee80211_csa_finalize(sdata);
3319         }
3320
3321 out:
3322         mutex_unlock(&local->chanctx_mtx);
3323         return err;
3324 }
3325
3326 int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3327                              struct cfg80211_csa_settings *params)
3328 {
3329         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3330         struct ieee80211_local *local = sdata->local;
3331         int err;
3332
3333         mutex_lock(&local->mtx);
3334         err = __ieee80211_channel_switch(wiphy, dev, params);
3335         mutex_unlock(&local->mtx);
3336
3337         return err;
3338 }
3339
3340 u64 ieee80211_mgmt_tx_cookie(struct ieee80211_local *local)
3341 {
3342         lockdep_assert_held(&local->mtx);
3343
3344         local->roc_cookie_counter++;
3345
3346         /* wow, you wrapped 64 bits ... more likely a bug */
3347         if (WARN_ON(local->roc_cookie_counter == 0))
3348                 local->roc_cookie_counter++;
3349
3350         return local->roc_cookie_counter;
3351 }
3352
3353 int ieee80211_attach_ack_skb(struct ieee80211_local *local, struct sk_buff *skb,
3354                              u64 *cookie, gfp_t gfp)
3355 {
3356         unsigned long spin_flags;
3357         struct sk_buff *ack_skb;
3358         int id;
3359
3360         ack_skb = skb_copy(skb, gfp);
3361         if (!ack_skb)
3362                 return -ENOMEM;
3363
3364         spin_lock_irqsave(&local->ack_status_lock, spin_flags);
3365         id = idr_alloc(&local->ack_status_frames, ack_skb,
3366                        1, 0x10000, GFP_ATOMIC);
3367         spin_unlock_irqrestore(&local->ack_status_lock, spin_flags);
3368
3369         if (id < 0) {
3370                 kfree_skb(ack_skb);
3371                 return -ENOMEM;
3372         }
3373
3374         IEEE80211_SKB_CB(skb)->ack_frame_id = id;
3375
3376         *cookie = ieee80211_mgmt_tx_cookie(local);
3377         IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
3378
3379         return 0;
3380 }
3381
3382 static void ieee80211_mgmt_frame_register(struct wiphy *wiphy,
3383                                           struct wireless_dev *wdev,
3384                                           u16 frame_type, bool reg)
3385 {
3386         struct ieee80211_local *local = wiphy_priv(wiphy);
3387         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3388
3389         switch (frame_type) {
3390         case IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_REQ:
3391                 if (reg) {
3392                         local->probe_req_reg++;
3393                         sdata->vif.probe_req_reg++;
3394                 } else {
3395                         if (local->probe_req_reg)
3396                                 local->probe_req_reg--;
3397
3398                         if (sdata->vif.probe_req_reg)
3399                                 sdata->vif.probe_req_reg--;
3400                 }
3401
3402                 if (!local->open_count)
3403                         break;
3404
3405                 if (sdata->vif.probe_req_reg == 1)
3406                         drv_config_iface_filter(local, sdata, FIF_PROBE_REQ,
3407                                                 FIF_PROBE_REQ);
3408                 else if (sdata->vif.probe_req_reg == 0)
3409                         drv_config_iface_filter(local, sdata, 0,
3410                                                 FIF_PROBE_REQ);
3411
3412                 ieee80211_configure_filter(local);
3413                 break;
3414         default:
3415                 break;
3416         }
3417 }
3418
3419 static int ieee80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
3420 {
3421         struct ieee80211_local *local = wiphy_priv(wiphy);
3422
3423         if (local->started)
3424                 return -EOPNOTSUPP;
3425
3426         return drv_set_antenna(local, tx_ant, rx_ant);
3427 }
3428
3429 static int ieee80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
3430 {
3431         struct ieee80211_local *local = wiphy_priv(wiphy);
3432
3433         return drv_get_antenna(local, tx_ant, rx_ant);
3434 }
3435
3436 static int ieee80211_set_rekey_data(struct wiphy *wiphy,
3437                                     struct net_device *dev,
3438                                     struct cfg80211_gtk_rekey_data *data)
3439 {
3440         struct ieee80211_local *local = wiphy_priv(wiphy);
3441         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3442
3443         if (!local->ops->set_rekey_data)
3444                 return -EOPNOTSUPP;
3445
3446         drv_set_rekey_data(local, sdata, data);
3447
3448         return 0;
3449 }
3450
3451 static int ieee80211_probe_client(struct wiphy *wiphy, struct net_device *dev,
3452                                   const u8 *peer, u64 *cookie)
3453 {
3454         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3455         struct ieee80211_local *local = sdata->local;
3456         struct ieee80211_qos_hdr *nullfunc;
3457         struct sk_buff *skb;
3458         int size = sizeof(*nullfunc);
3459         __le16 fc;
3460         bool qos;
3461         struct ieee80211_tx_info *info;
3462         struct sta_info *sta;
3463         struct ieee80211_chanctx_conf *chanctx_conf;
3464         enum nl80211_band band;
3465         int ret;
3466
3467         /* the lock is needed to assign the cookie later */
3468         mutex_lock(&local->mtx);
3469
3470         rcu_read_lock();
3471         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3472         if (WARN_ON(!chanctx_conf)) {
3473                 ret = -EINVAL;
3474                 goto unlock;
3475         }
3476         band = chanctx_conf->def.chan->band;
3477         sta = sta_info_get_bss(sdata, peer);
3478         if (sta) {
3479                 qos = sta->sta.wme;
3480         } else {
3481                 ret = -ENOLINK;
3482                 goto unlock;
3483         }
3484
3485         if (qos) {
3486                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3487                                  IEEE80211_STYPE_QOS_NULLFUNC |
3488                                  IEEE80211_FCTL_FROMDS);
3489         } else {
3490                 size -= 2;
3491                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3492                                  IEEE80211_STYPE_NULLFUNC |
3493                                  IEEE80211_FCTL_FROMDS);
3494         }
3495
3496         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
3497         if (!skb) {
3498                 ret = -ENOMEM;
3499                 goto unlock;
3500         }
3501
3502         skb->dev = dev;
3503
3504         skb_reserve(skb, local->hw.extra_tx_headroom);
3505
3506         nullfunc = skb_put(skb, size);
3507         nullfunc->frame_control = fc;
3508         nullfunc->duration_id = 0;
3509         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
3510         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
3511         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
3512         nullfunc->seq_ctrl = 0;
3513
3514         info = IEEE80211_SKB_CB(skb);
3515
3516         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
3517                        IEEE80211_TX_INTFL_NL80211_FRAME_TX;
3518         info->band = band;
3519
3520         skb_set_queue_mapping(skb, IEEE80211_AC_VO);
3521         skb->priority = 7;
3522         if (qos)
3523                 nullfunc->qos_ctrl = cpu_to_le16(7);
3524
3525         ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_ATOMIC);
3526         if (ret) {
3527                 kfree_skb(skb);
3528                 goto unlock;
3529         }
3530
3531         local_bh_disable();
3532         ieee80211_xmit(sdata, sta, skb, 0);
3533         local_bh_enable();
3534
3535         ret = 0;
3536 unlock:
3537         rcu_read_unlock();
3538         mutex_unlock(&local->mtx);
3539
3540         return ret;
3541 }
3542
3543 static int ieee80211_cfg_get_channel(struct wiphy *wiphy,
3544                                      struct wireless_dev *wdev,
3545                                      struct cfg80211_chan_def *chandef)
3546 {
3547         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3548         struct ieee80211_local *local = wiphy_priv(wiphy);
3549         struct ieee80211_chanctx_conf *chanctx_conf;
3550         int ret = -ENODATA;
3551
3552         rcu_read_lock();
3553         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3554         if (chanctx_conf) {
3555                 *chandef = sdata->vif.bss_conf.chandef;
3556                 ret = 0;
3557         } else if (local->open_count > 0 &&
3558                    local->open_count == local->monitors &&
3559                    sdata->vif.type == NL80211_IFTYPE_MONITOR) {
3560                 if (local->use_chanctx)
3561                         *chandef = local->monitor_chandef;
3562                 else
3563                         *chandef = local->_oper_chandef;
3564                 ret = 0;
3565         }
3566         rcu_read_unlock();
3567
3568         return ret;
3569 }
3570
3571 #ifdef CONFIG_PM
3572 static void ieee80211_set_wakeup(struct wiphy *wiphy, bool enabled)
3573 {
3574         drv_set_wakeup(wiphy_priv(wiphy), enabled);
3575 }
3576 #endif
3577
3578 static int ieee80211_set_qos_map(struct wiphy *wiphy,
3579                                  struct net_device *dev,
3580                                  struct cfg80211_qos_map *qos_map)
3581 {
3582         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3583         struct mac80211_qos_map *new_qos_map, *old_qos_map;
3584
3585         if (qos_map) {
3586                 new_qos_map = kzalloc(sizeof(*new_qos_map), GFP_KERNEL);
3587                 if (!new_qos_map)
3588                         return -ENOMEM;
3589                 memcpy(&new_qos_map->qos_map, qos_map, sizeof(*qos_map));
3590         } else {
3591                 /* A NULL qos_map was passed to disable QoS mapping */
3592                 new_qos_map = NULL;
3593         }
3594
3595         old_qos_map = sdata_dereference(sdata->qos_map, sdata);
3596         rcu_assign_pointer(sdata->qos_map, new_qos_map);
3597         if (old_qos_map)
3598                 kfree_rcu(old_qos_map, rcu_head);
3599
3600         return 0;
3601 }
3602
3603 static int ieee80211_set_ap_chanwidth(struct wiphy *wiphy,
3604                                       struct net_device *dev,
3605                                       struct cfg80211_chan_def *chandef)
3606 {
3607         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3608         int ret;
3609         u32 changed = 0;
3610
3611         ret = ieee80211_vif_change_bandwidth(sdata, chandef, &changed);
3612         if (ret == 0)
3613                 ieee80211_bss_info_change_notify(sdata, changed);
3614
3615         return ret;
3616 }
3617
3618 static int ieee80211_add_tx_ts(struct wiphy *wiphy, struct net_device *dev,
3619                                u8 tsid, const u8 *peer, u8 up,
3620                                u16 admitted_time)
3621 {
3622         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3623         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3624         int ac = ieee802_1d_to_ac[up];
3625
3626         if (sdata->vif.type != NL80211_IFTYPE_STATION)
3627                 return -EOPNOTSUPP;
3628
3629         if (!(sdata->wmm_acm & BIT(up)))
3630                 return -EINVAL;
3631
3632         if (ifmgd->tx_tspec[ac].admitted_time)
3633                 return -EBUSY;
3634
3635         if (admitted_time) {
3636                 ifmgd->tx_tspec[ac].admitted_time = 32 * admitted_time;
3637                 ifmgd->tx_tspec[ac].tsid = tsid;
3638                 ifmgd->tx_tspec[ac].up = up;
3639         }
3640
3641         return 0;
3642 }
3643
3644 static int ieee80211_del_tx_ts(struct wiphy *wiphy, struct net_device *dev,
3645                                u8 tsid, const u8 *peer)
3646 {
3647         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3648         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3649         struct ieee80211_local *local = wiphy_priv(wiphy);
3650         int ac;
3651
3652         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3653                 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
3654
3655                 /* skip unused entries */
3656                 if (!tx_tspec->admitted_time)
3657                         continue;
3658
3659                 if (tx_tspec->tsid != tsid)
3660                         continue;
3661
3662                 /* due to this new packets will be reassigned to non-ACM ACs */
3663                 tx_tspec->up = -1;
3664
3665                 /* Make sure that all packets have been sent to avoid to
3666                  * restore the QoS params on packets that are still on the
3667                  * queues.
3668                  */
3669                 synchronize_net();
3670                 ieee80211_flush_queues(local, sdata, false);
3671
3672                 /* restore the normal QoS parameters
3673                  * (unconditionally to avoid races)
3674                  */
3675                 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
3676                 tx_tspec->downgraded = false;
3677                 ieee80211_sta_handle_tspec_ac_params(sdata);
3678
3679                 /* finally clear all the data */
3680                 memset(tx_tspec, 0, sizeof(*tx_tspec));
3681
3682                 return 0;
3683         }
3684
3685         return -ENOENT;
3686 }
3687
3688 void ieee80211_nan_func_terminated(struct ieee80211_vif *vif,
3689                                    u8 inst_id,
3690                                    enum nl80211_nan_func_term_reason reason,
3691                                    gfp_t gfp)
3692 {
3693         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3694         struct cfg80211_nan_func *func;
3695         u64 cookie;
3696
3697         if (WARN_ON(vif->type != NL80211_IFTYPE_NAN))
3698                 return;
3699
3700         spin_lock_bh(&sdata->u.nan.func_lock);
3701
3702         func = idr_find(&sdata->u.nan.function_inst_ids, inst_id);
3703         if (WARN_ON(!func)) {
3704                 spin_unlock_bh(&sdata->u.nan.func_lock);
3705                 return;
3706         }
3707
3708         cookie = func->cookie;
3709         idr_remove(&sdata->u.nan.function_inst_ids, inst_id);
3710
3711         spin_unlock_bh(&sdata->u.nan.func_lock);
3712
3713         cfg80211_free_nan_func(func);
3714
3715         cfg80211_nan_func_terminated(ieee80211_vif_to_wdev(vif), inst_id,
3716                                      reason, cookie, gfp);
3717 }
3718 EXPORT_SYMBOL(ieee80211_nan_func_terminated);
3719
3720 void ieee80211_nan_func_match(struct ieee80211_vif *vif,
3721                               struct cfg80211_nan_match_params *match,
3722                               gfp_t gfp)
3723 {
3724         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3725         struct cfg80211_nan_func *func;
3726
3727         if (WARN_ON(vif->type != NL80211_IFTYPE_NAN))
3728                 return;
3729
3730         spin_lock_bh(&sdata->u.nan.func_lock);
3731
3732         func = idr_find(&sdata->u.nan.function_inst_ids,  match->inst_id);
3733         if (WARN_ON(!func)) {
3734                 spin_unlock_bh(&sdata->u.nan.func_lock);
3735                 return;
3736         }
3737         match->cookie = func->cookie;
3738
3739         spin_unlock_bh(&sdata->u.nan.func_lock);
3740
3741         cfg80211_nan_match(ieee80211_vif_to_wdev(vif), match, gfp);
3742 }
3743 EXPORT_SYMBOL(ieee80211_nan_func_match);
3744
3745 static int ieee80211_set_multicast_to_unicast(struct wiphy *wiphy,
3746                                               struct net_device *dev,
3747                                               const bool enabled)
3748 {
3749         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3750
3751         sdata->u.ap.multicast_to_unicast = enabled;
3752
3753         return 0;
3754 }
3755
3756 void ieee80211_fill_txq_stats(struct cfg80211_txq_stats *txqstats,
3757                               struct txq_info *txqi)
3758 {
3759         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_BACKLOG_BYTES))) {
3760                 txqstats->filled |= BIT(NL80211_TXQ_STATS_BACKLOG_BYTES);
3761                 txqstats->backlog_bytes = txqi->tin.backlog_bytes;
3762         }
3763
3764         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_BACKLOG_PACKETS))) {
3765                 txqstats->filled |= BIT(NL80211_TXQ_STATS_BACKLOG_PACKETS);
3766                 txqstats->backlog_packets = txqi->tin.backlog_packets;
3767         }
3768
3769         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_FLOWS))) {
3770                 txqstats->filled |= BIT(NL80211_TXQ_STATS_FLOWS);
3771                 txqstats->flows = txqi->tin.flows;
3772         }
3773
3774         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_DROPS))) {
3775                 txqstats->filled |= BIT(NL80211_TXQ_STATS_DROPS);
3776                 txqstats->drops = txqi->cstats.drop_count;
3777         }
3778
3779         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_ECN_MARKS))) {
3780                 txqstats->filled |= BIT(NL80211_TXQ_STATS_ECN_MARKS);
3781                 txqstats->ecn_marks = txqi->cstats.ecn_mark;
3782         }
3783
3784         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_OVERLIMIT))) {
3785                 txqstats->filled |= BIT(NL80211_TXQ_STATS_OVERLIMIT);
3786                 txqstats->overlimit = txqi->tin.overlimit;
3787         }
3788
3789         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_COLLISIONS))) {
3790                 txqstats->filled |= BIT(NL80211_TXQ_STATS_COLLISIONS);
3791                 txqstats->collisions = txqi->tin.collisions;
3792         }
3793
3794         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_TX_BYTES))) {
3795                 txqstats->filled |= BIT(NL80211_TXQ_STATS_TX_BYTES);
3796                 txqstats->tx_bytes = txqi->tin.tx_bytes;
3797         }
3798
3799         if (!(txqstats->filled & BIT(NL80211_TXQ_STATS_TX_PACKETS))) {
3800                 txqstats->filled |= BIT(NL80211_TXQ_STATS_TX_PACKETS);
3801                 txqstats->tx_packets = txqi->tin.tx_packets;
3802         }
3803 }
3804
3805 static int ieee80211_get_txq_stats(struct wiphy *wiphy,
3806                                    struct wireless_dev *wdev,
3807                                    struct cfg80211_txq_stats *txqstats)
3808 {
3809         struct ieee80211_local *local = wiphy_priv(wiphy);
3810         struct ieee80211_sub_if_data *sdata;
3811         int ret = 0;
3812
3813         if (!local->ops->wake_tx_queue)
3814                 return 1;
3815
3816         spin_lock_bh(&local->fq.lock);
3817         rcu_read_lock();
3818
3819         if (wdev) {
3820                 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3821                 if (!sdata->vif.txq) {
3822                         ret = 1;
3823                         goto out;
3824                 }
3825                 ieee80211_fill_txq_stats(txqstats, to_txq_info(sdata->vif.txq));
3826         } else {
3827                 /* phy stats */
3828                 txqstats->filled |= BIT(NL80211_TXQ_STATS_BACKLOG_PACKETS) |
3829                                     BIT(NL80211_TXQ_STATS_BACKLOG_BYTES) |
3830                                     BIT(NL80211_TXQ_STATS_OVERLIMIT) |
3831                                     BIT(NL80211_TXQ_STATS_OVERMEMORY) |
3832                                     BIT(NL80211_TXQ_STATS_COLLISIONS) |
3833                                     BIT(NL80211_TXQ_STATS_MAX_FLOWS);
3834                 txqstats->backlog_packets = local->fq.backlog;
3835                 txqstats->backlog_bytes = local->fq.memory_usage;
3836                 txqstats->overlimit = local->fq.overlimit;
3837                 txqstats->overmemory = local->fq.overmemory;
3838                 txqstats->collisions = local->fq.collisions;
3839                 txqstats->max_flows = local->fq.flows_cnt;
3840         }
3841
3842 out:
3843         rcu_read_unlock();
3844         spin_unlock_bh(&local->fq.lock);
3845
3846         return ret;
3847 }
3848
3849 static int
3850 ieee80211_get_ftm_responder_stats(struct wiphy *wiphy,
3851                                   struct net_device *dev,
3852                                   struct cfg80211_ftm_responder_stats *ftm_stats)
3853 {
3854         struct ieee80211_local *local = wiphy_priv(wiphy);
3855         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3856
3857         return drv_get_ftm_responder_stats(local, sdata, ftm_stats);
3858 }
3859
3860 static int
3861 ieee80211_start_pmsr(struct wiphy *wiphy, struct wireless_dev *dev,
3862                      struct cfg80211_pmsr_request *request)
3863 {
3864         struct ieee80211_local *local = wiphy_priv(wiphy);
3865         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(dev);
3866
3867         return drv_start_pmsr(local, sdata, request);
3868 }
3869
3870 static void
3871 ieee80211_abort_pmsr(struct wiphy *wiphy, struct wireless_dev *dev,
3872                      struct cfg80211_pmsr_request *request)
3873 {
3874         struct ieee80211_local *local = wiphy_priv(wiphy);
3875         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(dev);
3876
3877         return drv_abort_pmsr(local, sdata, request);
3878 }
3879
3880 const struct cfg80211_ops mac80211_config_ops = {
3881         .add_virtual_intf = ieee80211_add_iface,
3882         .del_virtual_intf = ieee80211_del_iface,
3883         .change_virtual_intf = ieee80211_change_iface,
3884         .start_p2p_device = ieee80211_start_p2p_device,
3885         .stop_p2p_device = ieee80211_stop_p2p_device,
3886         .add_key = ieee80211_add_key,
3887         .del_key = ieee80211_del_key,
3888         .get_key = ieee80211_get_key,
3889         .set_default_key = ieee80211_config_default_key,
3890         .set_default_mgmt_key = ieee80211_config_default_mgmt_key,
3891         .start_ap = ieee80211_start_ap,
3892         .change_beacon = ieee80211_change_beacon,
3893         .stop_ap = ieee80211_stop_ap,
3894         .add_station = ieee80211_add_station,
3895         .del_station = ieee80211_del_station,
3896         .change_station = ieee80211_change_station,
3897         .get_station = ieee80211_get_station,
3898         .dump_station = ieee80211_dump_station,
3899         .dump_survey = ieee80211_dump_survey,
3900 #ifdef CONFIG_MAC80211_MESH
3901         .add_mpath = ieee80211_add_mpath,
3902         .del_mpath = ieee80211_del_mpath,
3903         .change_mpath = ieee80211_change_mpath,
3904         .get_mpath = ieee80211_get_mpath,
3905         .dump_mpath = ieee80211_dump_mpath,
3906         .get_mpp = ieee80211_get_mpp,
3907         .dump_mpp = ieee80211_dump_mpp,
3908         .update_mesh_config = ieee80211_update_mesh_config,
3909         .get_mesh_config = ieee80211_get_mesh_config,
3910         .join_mesh = ieee80211_join_mesh,
3911         .leave_mesh = ieee80211_leave_mesh,
3912 #endif
3913         .join_ocb = ieee80211_join_ocb,
3914         .leave_ocb = ieee80211_leave_ocb,
3915         .change_bss = ieee80211_change_bss,
3916         .set_txq_params = ieee80211_set_txq_params,
3917         .set_monitor_channel = ieee80211_set_monitor_channel,
3918         .suspend = ieee80211_suspend,
3919         .resume = ieee80211_resume,
3920         .scan = ieee80211_scan,
3921         .abort_scan = ieee80211_abort_scan,
3922         .sched_scan_start = ieee80211_sched_scan_start,
3923         .sched_scan_stop = ieee80211_sched_scan_stop,
3924         .auth = ieee80211_auth,
3925         .assoc = ieee80211_assoc,
3926         .deauth = ieee80211_deauth,
3927         .disassoc = ieee80211_disassoc,
3928         .join_ibss = ieee80211_join_ibss,
3929         .leave_ibss = ieee80211_leave_ibss,
3930         .set_mcast_rate = ieee80211_set_mcast_rate,
3931         .set_wiphy_params = ieee80211_set_wiphy_params,
3932         .set_tx_power = ieee80211_set_tx_power,
3933         .get_tx_power = ieee80211_get_tx_power,
3934         .set_wds_peer = ieee80211_set_wds_peer,
3935         .rfkill_poll = ieee80211_rfkill_poll,
3936         CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
3937         CFG80211_TESTMODE_DUMP(ieee80211_testmode_dump)
3938         .set_power_mgmt = ieee80211_set_power_mgmt,
3939         .set_bitrate_mask = ieee80211_set_bitrate_mask,
3940         .remain_on_channel = ieee80211_remain_on_channel,
3941         .cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
3942         .mgmt_tx = ieee80211_mgmt_tx,
3943         .mgmt_tx_cancel_wait = ieee80211_mgmt_tx_cancel_wait,
3944         .set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
3945         .set_cqm_rssi_range_config = ieee80211_set_cqm_rssi_range_config,
3946         .mgmt_frame_register = ieee80211_mgmt_frame_register,
3947         .set_antenna = ieee80211_set_antenna,
3948         .get_antenna = ieee80211_get_antenna,
3949         .set_rekey_data = ieee80211_set_rekey_data,
3950         .tdls_oper = ieee80211_tdls_oper,
3951         .tdls_mgmt = ieee80211_tdls_mgmt,
3952         .tdls_channel_switch = ieee80211_tdls_channel_switch,
3953         .tdls_cancel_channel_switch = ieee80211_tdls_cancel_channel_switch,
3954         .probe_client = ieee80211_probe_client,
3955         .set_noack_map = ieee80211_set_noack_map,
3956 #ifdef CONFIG_PM
3957         .set_wakeup = ieee80211_set_wakeup,
3958 #endif
3959         .get_channel = ieee80211_cfg_get_channel,
3960         .start_radar_detection = ieee80211_start_radar_detection,
3961         .channel_switch = ieee80211_channel_switch,
3962         .set_qos_map = ieee80211_set_qos_map,
3963         .set_ap_chanwidth = ieee80211_set_ap_chanwidth,
3964         .add_tx_ts = ieee80211_add_tx_ts,
3965         .del_tx_ts = ieee80211_del_tx_ts,
3966         .start_nan = ieee80211_start_nan,
3967         .stop_nan = ieee80211_stop_nan,
3968         .nan_change_conf = ieee80211_nan_change_conf,
3969         .add_nan_func = ieee80211_add_nan_func,
3970         .del_nan_func = ieee80211_del_nan_func,
3971         .set_multicast_to_unicast = ieee80211_set_multicast_to_unicast,
3972         .tx_control_port = ieee80211_tx_control_port,
3973         .get_txq_stats = ieee80211_get_txq_stats,
3974         .get_ftm_responder_stats = ieee80211_get_ftm_responder_stats,
3975         .start_pmsr = ieee80211_start_pmsr,
3976         .abort_pmsr = ieee80211_abort_pmsr,
3977 };