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