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