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