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