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