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