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