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