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