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