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