Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.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 <net/cfg80211.h>
16 #include "ieee80211_i.h"
17 #include "driver-ops.h"
18 #include "cfg.h"
19 #include "rate.h"
20 #include "mesh.h"
21
22 static bool nl80211_type_check(enum nl80211_iftype type)
23 {
24         switch (type) {
25         case NL80211_IFTYPE_ADHOC:
26         case NL80211_IFTYPE_STATION:
27         case NL80211_IFTYPE_MONITOR:
28 #ifdef CONFIG_MAC80211_MESH
29         case NL80211_IFTYPE_MESH_POINT:
30 #endif
31         case NL80211_IFTYPE_AP:
32         case NL80211_IFTYPE_AP_VLAN:
33         case NL80211_IFTYPE_WDS:
34                 return true;
35         default:
36                 return false;
37         }
38 }
39
40 static bool nl80211_params_check(enum nl80211_iftype type,
41                                  struct vif_params *params)
42 {
43         if (!nl80211_type_check(type))
44                 return false;
45
46         return true;
47 }
48
49 static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
50                                enum nl80211_iftype type, u32 *flags,
51                                struct vif_params *params)
52 {
53         struct ieee80211_local *local = wiphy_priv(wiphy);
54         struct net_device *dev;
55         struct ieee80211_sub_if_data *sdata;
56         int err;
57
58         if (!nl80211_params_check(type, params))
59                 return -EINVAL;
60
61         err = ieee80211_if_add(local, name, &dev, type, params);
62         if (err || type != NL80211_IFTYPE_MONITOR || !flags)
63                 return err;
64
65         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
66         sdata->u.mntr_flags = *flags;
67         return 0;
68 }
69
70 static int ieee80211_del_iface(struct wiphy *wiphy, struct net_device *dev)
71 {
72         ieee80211_if_remove(IEEE80211_DEV_TO_SUB_IF(dev));
73
74         return 0;
75 }
76
77 static int ieee80211_change_iface(struct wiphy *wiphy,
78                                   struct net_device *dev,
79                                   enum nl80211_iftype type, u32 *flags,
80                                   struct vif_params *params)
81 {
82         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
83         int ret;
84
85         if (ieee80211_sdata_running(sdata))
86                 return -EBUSY;
87
88         if (!nl80211_params_check(type, params))
89                 return -EINVAL;
90
91         ret = ieee80211_if_change_type(sdata, type);
92         if (ret)
93                 return ret;
94
95         if (ieee80211_vif_is_mesh(&sdata->vif) && params->mesh_id_len)
96                 ieee80211_sdata_set_mesh_id(sdata,
97                                             params->mesh_id_len,
98                                             params->mesh_id);
99
100         if (sdata->vif.type != NL80211_IFTYPE_MONITOR || !flags)
101                 return 0;
102
103         if (type == NL80211_IFTYPE_AP_VLAN &&
104             params && params->use_4addr == 0)
105                 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
106         else if (type == NL80211_IFTYPE_STATION &&
107                  params && params->use_4addr >= 0)
108                 sdata->u.mgd.use_4addr = params->use_4addr;
109
110         sdata->u.mntr_flags = *flags;
111         return 0;
112 }
113
114 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
115                              u8 key_idx, const u8 *mac_addr,
116                              struct key_params *params)
117 {
118         struct ieee80211_sub_if_data *sdata;
119         struct sta_info *sta = NULL;
120         enum ieee80211_key_alg alg;
121         struct ieee80211_key *key;
122         int err;
123
124         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
125
126         switch (params->cipher) {
127         case WLAN_CIPHER_SUITE_WEP40:
128         case WLAN_CIPHER_SUITE_WEP104:
129                 alg = ALG_WEP;
130                 break;
131         case WLAN_CIPHER_SUITE_TKIP:
132                 alg = ALG_TKIP;
133                 break;
134         case WLAN_CIPHER_SUITE_CCMP:
135                 alg = ALG_CCMP;
136                 break;
137         case WLAN_CIPHER_SUITE_AES_CMAC:
138                 alg = ALG_AES_CMAC;
139                 break;
140         default:
141                 return -EINVAL;
142         }
143
144         key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key,
145                                   params->seq_len, params->seq);
146         if (!key)
147                 return -ENOMEM;
148
149         rcu_read_lock();
150
151         if (mac_addr) {
152                 sta = sta_info_get_bss(sdata, mac_addr);
153                 if (!sta) {
154                         ieee80211_key_free(key);
155                         err = -ENOENT;
156                         goto out_unlock;
157                 }
158         }
159
160         ieee80211_key_link(key, sdata, sta);
161
162         err = 0;
163  out_unlock:
164         rcu_read_unlock();
165
166         return err;
167 }
168
169 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
170                              u8 key_idx, const u8 *mac_addr)
171 {
172         struct ieee80211_sub_if_data *sdata;
173         struct sta_info *sta;
174         int ret;
175
176         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
177
178         rcu_read_lock();
179
180         if (mac_addr) {
181                 ret = -ENOENT;
182
183                 sta = sta_info_get_bss(sdata, mac_addr);
184                 if (!sta)
185                         goto out_unlock;
186
187                 if (sta->key) {
188                         ieee80211_key_free(sta->key);
189                         WARN_ON(sta->key);
190                         ret = 0;
191                 }
192
193                 goto out_unlock;
194         }
195
196         if (!sdata->keys[key_idx]) {
197                 ret = -ENOENT;
198                 goto out_unlock;
199         }
200
201         ieee80211_key_free(sdata->keys[key_idx]);
202         WARN_ON(sdata->keys[key_idx]);
203
204         ret = 0;
205  out_unlock:
206         rcu_read_unlock();
207
208         return ret;
209 }
210
211 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
212                              u8 key_idx, const u8 *mac_addr, void *cookie,
213                              void (*callback)(void *cookie,
214                                               struct key_params *params))
215 {
216         struct ieee80211_sub_if_data *sdata;
217         struct sta_info *sta = NULL;
218         u8 seq[6] = {0};
219         struct key_params params;
220         struct ieee80211_key *key;
221         u32 iv32;
222         u16 iv16;
223         int err = -ENOENT;
224
225         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
226
227         rcu_read_lock();
228
229         if (mac_addr) {
230                 sta = sta_info_get_bss(sdata, mac_addr);
231                 if (!sta)
232                         goto out;
233
234                 key = sta->key;
235         } else
236                 key = sdata->keys[key_idx];
237
238         if (!key)
239                 goto out;
240
241         memset(&params, 0, sizeof(params));
242
243         switch (key->conf.alg) {
244         case ALG_TKIP:
245                 params.cipher = WLAN_CIPHER_SUITE_TKIP;
246
247                 iv32 = key->u.tkip.tx.iv32;
248                 iv16 = key->u.tkip.tx.iv16;
249
250                 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
251                         drv_get_tkip_seq(sdata->local,
252                                          key->conf.hw_key_idx,
253                                          &iv32, &iv16);
254
255                 seq[0] = iv16 & 0xff;
256                 seq[1] = (iv16 >> 8) & 0xff;
257                 seq[2] = iv32 & 0xff;
258                 seq[3] = (iv32 >> 8) & 0xff;
259                 seq[4] = (iv32 >> 16) & 0xff;
260                 seq[5] = (iv32 >> 24) & 0xff;
261                 params.seq = seq;
262                 params.seq_len = 6;
263                 break;
264         case ALG_CCMP:
265                 params.cipher = WLAN_CIPHER_SUITE_CCMP;
266                 seq[0] = key->u.ccmp.tx_pn[5];
267                 seq[1] = key->u.ccmp.tx_pn[4];
268                 seq[2] = key->u.ccmp.tx_pn[3];
269                 seq[3] = key->u.ccmp.tx_pn[2];
270                 seq[4] = key->u.ccmp.tx_pn[1];
271                 seq[5] = key->u.ccmp.tx_pn[0];
272                 params.seq = seq;
273                 params.seq_len = 6;
274                 break;
275         case ALG_WEP:
276                 if (key->conf.keylen == 5)
277                         params.cipher = WLAN_CIPHER_SUITE_WEP40;
278                 else
279                         params.cipher = WLAN_CIPHER_SUITE_WEP104;
280                 break;
281         case ALG_AES_CMAC:
282                 params.cipher = WLAN_CIPHER_SUITE_AES_CMAC;
283                 seq[0] = key->u.aes_cmac.tx_pn[5];
284                 seq[1] = key->u.aes_cmac.tx_pn[4];
285                 seq[2] = key->u.aes_cmac.tx_pn[3];
286                 seq[3] = key->u.aes_cmac.tx_pn[2];
287                 seq[4] = key->u.aes_cmac.tx_pn[1];
288                 seq[5] = key->u.aes_cmac.tx_pn[0];
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)
308 {
309         struct ieee80211_sub_if_data *sdata;
310
311         rcu_read_lock();
312
313         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
314         ieee80211_set_default_key(sdata, key_idx);
315
316         rcu_read_unlock();
317
318         return 0;
319 }
320
321 static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
322                                              struct net_device *dev,
323                                              u8 key_idx)
324 {
325         struct ieee80211_sub_if_data *sdata;
326
327         rcu_read_lock();
328
329         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
330         ieee80211_set_default_mgmt_key(sdata, key_idx);
331
332         rcu_read_unlock();
333
334         return 0;
335 }
336
337 static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
338 {
339         struct ieee80211_sub_if_data *sdata = sta->sdata;
340
341         sinfo->generation = sdata->local->sta_generation;
342
343         sinfo->filled = STATION_INFO_INACTIVE_TIME |
344                         STATION_INFO_RX_BYTES |
345                         STATION_INFO_TX_BYTES |
346                         STATION_INFO_RX_PACKETS |
347                         STATION_INFO_TX_PACKETS |
348                         STATION_INFO_TX_BITRATE;
349
350         sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
351         sinfo->rx_bytes = sta->rx_bytes;
352         sinfo->tx_bytes = sta->tx_bytes;
353         sinfo->rx_packets = sta->rx_packets;
354         sinfo->tx_packets = sta->tx_packets;
355
356         if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
357             (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
358                 sinfo->filled |= STATION_INFO_SIGNAL;
359                 sinfo->signal = (s8)sta->last_signal;
360         }
361
362         sinfo->txrate.flags = 0;
363         if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
364                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
365         if (sta->last_tx_rate.flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
366                 sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
367         if (sta->last_tx_rate.flags & IEEE80211_TX_RC_SHORT_GI)
368                 sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
369
370         if (!(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)) {
371                 struct ieee80211_supported_band *sband;
372                 sband = sta->local->hw.wiphy->bands[
373                                 sta->local->hw.conf.channel->band];
374                 sinfo->txrate.legacy =
375                         sband->bitrates[sta->last_tx_rate.idx].bitrate;
376         } else
377                 sinfo->txrate.mcs = sta->last_tx_rate.idx;
378
379         if (ieee80211_vif_is_mesh(&sdata->vif)) {
380 #ifdef CONFIG_MAC80211_MESH
381                 sinfo->filled |= STATION_INFO_LLID |
382                                  STATION_INFO_PLID |
383                                  STATION_INFO_PLINK_STATE;
384
385                 sinfo->llid = le16_to_cpu(sta->llid);
386                 sinfo->plid = le16_to_cpu(sta->plid);
387                 sinfo->plink_state = sta->plink_state;
388 #endif
389         }
390 }
391
392
393 static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
394                                  int idx, u8 *mac, struct station_info *sinfo)
395 {
396         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
397         struct sta_info *sta;
398         int ret = -ENOENT;
399
400         rcu_read_lock();
401
402         sta = sta_info_get_by_idx(sdata, idx);
403         if (sta) {
404                 ret = 0;
405                 memcpy(mac, sta->sta.addr, ETH_ALEN);
406                 sta_set_sinfo(sta, sinfo);
407         }
408
409         rcu_read_unlock();
410
411         return ret;
412 }
413
414 static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
415                                  int idx, struct survey_info *survey)
416 {
417         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
418
419         if (!local->ops->get_survey)
420                 return -EOPNOTSUPP;
421
422         return drv_get_survey(local, idx, survey);
423 }
424
425 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
426                                  u8 *mac, struct station_info *sinfo)
427 {
428         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
429         struct sta_info *sta;
430         int ret = -ENOENT;
431
432         rcu_read_lock();
433
434         sta = sta_info_get_bss(sdata, mac);
435         if (sta) {
436                 ret = 0;
437                 sta_set_sinfo(sta, sinfo);
438         }
439
440         rcu_read_unlock();
441
442         return ret;
443 }
444
445 /*
446  * This handles both adding a beacon and setting new beacon info
447  */
448 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
449                                    struct beacon_parameters *params)
450 {
451         struct beacon_data *new, *old;
452         int new_head_len, new_tail_len;
453         int size;
454         int err = -EINVAL;
455
456         old = sdata->u.ap.beacon;
457
458         /* head must not be zero-length */
459         if (params->head && !params->head_len)
460                 return -EINVAL;
461
462         /*
463          * This is a kludge. beacon interval should really be part
464          * of the beacon information.
465          */
466         if (params->interval &&
467             (sdata->vif.bss_conf.beacon_int != params->interval)) {
468                 sdata->vif.bss_conf.beacon_int = params->interval;
469                 ieee80211_bss_info_change_notify(sdata,
470                                                  BSS_CHANGED_BEACON_INT);
471         }
472
473         /* Need to have a beacon head if we don't have one yet */
474         if (!params->head && !old)
475                 return err;
476
477         /* sorry, no way to start beaconing without dtim period */
478         if (!params->dtim_period && !old)
479                 return err;
480
481         /* new or old head? */
482         if (params->head)
483                 new_head_len = params->head_len;
484         else
485                 new_head_len = old->head_len;
486
487         /* new or old tail? */
488         if (params->tail || !old)
489                 /* params->tail_len will be zero for !params->tail */
490                 new_tail_len = params->tail_len;
491         else
492                 new_tail_len = old->tail_len;
493
494         size = sizeof(*new) + new_head_len + new_tail_len;
495
496         new = kzalloc(size, GFP_KERNEL);
497         if (!new)
498                 return -ENOMEM;
499
500         /* start filling the new info now */
501
502         /* new or old dtim period? */
503         if (params->dtim_period)
504                 new->dtim_period = params->dtim_period;
505         else
506                 new->dtim_period = old->dtim_period;
507
508         /*
509          * pointers go into the block we allocated,
510          * memory is | beacon_data | head | tail |
511          */
512         new->head = ((u8 *) new) + sizeof(*new);
513         new->tail = new->head + new_head_len;
514         new->head_len = new_head_len;
515         new->tail_len = new_tail_len;
516
517         /* copy in head */
518         if (params->head)
519                 memcpy(new->head, params->head, new_head_len);
520         else
521                 memcpy(new->head, old->head, new_head_len);
522
523         /* copy in optional tail */
524         if (params->tail)
525                 memcpy(new->tail, params->tail, new_tail_len);
526         else
527                 if (old)
528                         memcpy(new->tail, old->tail, new_tail_len);
529
530         sdata->vif.bss_conf.dtim_period = new->dtim_period;
531
532         rcu_assign_pointer(sdata->u.ap.beacon, new);
533
534         synchronize_rcu();
535
536         kfree(old);
537
538         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
539                                                 BSS_CHANGED_BEACON);
540         return 0;
541 }
542
543 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
544                                 struct beacon_parameters *params)
545 {
546         struct ieee80211_sub_if_data *sdata;
547         struct beacon_data *old;
548
549         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
550
551         old = sdata->u.ap.beacon;
552
553         if (old)
554                 return -EALREADY;
555
556         return ieee80211_config_beacon(sdata, params);
557 }
558
559 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
560                                 struct beacon_parameters *params)
561 {
562         struct ieee80211_sub_if_data *sdata;
563         struct beacon_data *old;
564
565         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
566
567         old = sdata->u.ap.beacon;
568
569         if (!old)
570                 return -ENOENT;
571
572         return ieee80211_config_beacon(sdata, params);
573 }
574
575 static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
576 {
577         struct ieee80211_sub_if_data *sdata;
578         struct beacon_data *old;
579
580         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
581
582         old = sdata->u.ap.beacon;
583
584         if (!old)
585                 return -ENOENT;
586
587         rcu_assign_pointer(sdata->u.ap.beacon, NULL);
588         synchronize_rcu();
589         kfree(old);
590
591         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
592         return 0;
593 }
594
595 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
596 struct iapp_layer2_update {
597         u8 da[ETH_ALEN];        /* broadcast */
598         u8 sa[ETH_ALEN];        /* STA addr */
599         __be16 len;             /* 6 */
600         u8 dsap;                /* 0 */
601         u8 ssap;                /* 0 */
602         u8 control;
603         u8 xid_info[3];
604 } __attribute__ ((packed));
605
606 static void ieee80211_send_layer2_update(struct sta_info *sta)
607 {
608         struct iapp_layer2_update *msg;
609         struct sk_buff *skb;
610
611         /* Send Level 2 Update Frame to update forwarding tables in layer 2
612          * bridge devices */
613
614         skb = dev_alloc_skb(sizeof(*msg));
615         if (!skb)
616                 return;
617         msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
618
619         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
620          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
621
622         memset(msg->da, 0xff, ETH_ALEN);
623         memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
624         msg->len = htons(6);
625         msg->dsap = 0;
626         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
627         msg->control = 0xaf;    /* XID response lsb.1111F101.
628                                  * F=0 (no poll command; unsolicited frame) */
629         msg->xid_info[0] = 0x81;        /* XID format identifier */
630         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
631         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
632
633         skb->dev = sta->sdata->dev;
634         skb->protocol = eth_type_trans(skb, sta->sdata->dev);
635         memset(skb->cb, 0, sizeof(skb->cb));
636         netif_rx(skb);
637 }
638
639 static void sta_apply_parameters(struct ieee80211_local *local,
640                                  struct sta_info *sta,
641                                  struct station_parameters *params)
642 {
643         u32 rates;
644         int i, j;
645         struct ieee80211_supported_band *sband;
646         struct ieee80211_sub_if_data *sdata = sta->sdata;
647         u32 mask, set;
648
649         sband = local->hw.wiphy->bands[local->oper_channel->band];
650
651         spin_lock_bh(&sta->lock);
652         mask = params->sta_flags_mask;
653         set = params->sta_flags_set;
654
655         if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
656                 sta->flags &= ~WLAN_STA_AUTHORIZED;
657                 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
658                         sta->flags |= WLAN_STA_AUTHORIZED;
659         }
660
661         if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
662                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
663                 if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
664                         sta->flags |= WLAN_STA_SHORT_PREAMBLE;
665         }
666
667         if (mask & BIT(NL80211_STA_FLAG_WME)) {
668                 sta->flags &= ~WLAN_STA_WME;
669                 if (set & BIT(NL80211_STA_FLAG_WME))
670                         sta->flags |= WLAN_STA_WME;
671         }
672
673         if (mask & BIT(NL80211_STA_FLAG_MFP)) {
674                 sta->flags &= ~WLAN_STA_MFP;
675                 if (set & BIT(NL80211_STA_FLAG_MFP))
676                         sta->flags |= WLAN_STA_MFP;
677         }
678         spin_unlock_bh(&sta->lock);
679
680         /*
681          * cfg80211 validates this (1-2007) and allows setting the AID
682          * only when creating a new station entry
683          */
684         if (params->aid)
685                 sta->sta.aid = params->aid;
686
687         /*
688          * FIXME: updating the following information is racy when this
689          *        function is called from ieee80211_change_station().
690          *        However, all this information should be static so
691          *        maybe we should just reject attemps to change it.
692          */
693
694         if (params->listen_interval >= 0)
695                 sta->listen_interval = params->listen_interval;
696
697         if (params->supported_rates) {
698                 rates = 0;
699
700                 for (i = 0; i < params->supported_rates_len; i++) {
701                         int rate = (params->supported_rates[i] & 0x7f) * 5;
702                         for (j = 0; j < sband->n_bitrates; j++) {
703                                 if (sband->bitrates[j].bitrate == rate)
704                                         rates |= BIT(j);
705                         }
706                 }
707                 sta->sta.supp_rates[local->oper_channel->band] = rates;
708         }
709
710         if (params->ht_capa)
711                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
712                                                   params->ht_capa,
713                                                   &sta->sta.ht_cap);
714
715         if (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
716                 switch (params->plink_action) {
717                 case PLINK_ACTION_OPEN:
718                         mesh_plink_open(sta);
719                         break;
720                 case PLINK_ACTION_BLOCK:
721                         mesh_plink_block(sta);
722                         break;
723                 }
724         }
725 }
726
727 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
728                                  u8 *mac, struct station_parameters *params)
729 {
730         struct ieee80211_local *local = wiphy_priv(wiphy);
731         struct sta_info *sta;
732         struct ieee80211_sub_if_data *sdata;
733         int err;
734         int layer2_update;
735
736         if (params->vlan) {
737                 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
738
739                 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
740                     sdata->vif.type != NL80211_IFTYPE_AP)
741                         return -EINVAL;
742         } else
743                 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
744
745         if (compare_ether_addr(mac, sdata->vif.addr) == 0)
746                 return -EINVAL;
747
748         if (is_multicast_ether_addr(mac))
749                 return -EINVAL;
750
751         sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
752         if (!sta)
753                 return -ENOMEM;
754
755         sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
756
757         sta_apply_parameters(local, sta, params);
758
759         rate_control_rate_init(sta);
760
761         layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
762                 sdata->vif.type == NL80211_IFTYPE_AP;
763
764         err = sta_info_insert_rcu(sta);
765         if (err) {
766                 rcu_read_unlock();
767                 return err;
768         }
769
770         if (layer2_update)
771                 ieee80211_send_layer2_update(sta);
772
773         rcu_read_unlock();
774
775         return 0;
776 }
777
778 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
779                                  u8 *mac)
780 {
781         struct ieee80211_local *local = wiphy_priv(wiphy);
782         struct ieee80211_sub_if_data *sdata;
783
784         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
785
786         if (mac)
787                 return sta_info_destroy_addr_bss(sdata, mac);
788
789         sta_info_flush(local, sdata);
790         return 0;
791 }
792
793 static int ieee80211_change_station(struct wiphy *wiphy,
794                                     struct net_device *dev,
795                                     u8 *mac,
796                                     struct station_parameters *params)
797 {
798         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
799         struct ieee80211_local *local = wiphy_priv(wiphy);
800         struct sta_info *sta;
801         struct ieee80211_sub_if_data *vlansdata;
802
803         rcu_read_lock();
804
805         sta = sta_info_get_bss(sdata, mac);
806         if (!sta) {
807                 rcu_read_unlock();
808                 return -ENOENT;
809         }
810
811         if (params->vlan && params->vlan != sta->sdata->dev) {
812                 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
813
814                 if (vlansdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
815                     vlansdata->vif.type != NL80211_IFTYPE_AP) {
816                         rcu_read_unlock();
817                         return -EINVAL;
818                 }
819
820                 if (params->vlan->ieee80211_ptr->use_4addr) {
821                         if (vlansdata->u.vlan.sta) {
822                                 rcu_read_unlock();
823                                 return -EBUSY;
824                         }
825
826                         rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
827                 }
828
829                 sta->sdata = vlansdata;
830                 ieee80211_send_layer2_update(sta);
831         }
832
833         sta_apply_parameters(local, sta, params);
834
835         rcu_read_unlock();
836
837         return 0;
838 }
839
840 #ifdef CONFIG_MAC80211_MESH
841 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
842                                  u8 *dst, u8 *next_hop)
843 {
844         struct ieee80211_sub_if_data *sdata;
845         struct mesh_path *mpath;
846         struct sta_info *sta;
847         int err;
848
849         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
850
851         rcu_read_lock();
852         sta = sta_info_get(sdata, next_hop);
853         if (!sta) {
854                 rcu_read_unlock();
855                 return -ENOENT;
856         }
857
858         err = mesh_path_add(dst, sdata);
859         if (err) {
860                 rcu_read_unlock();
861                 return err;
862         }
863
864         mpath = mesh_path_lookup(dst, sdata);
865         if (!mpath) {
866                 rcu_read_unlock();
867                 return -ENXIO;
868         }
869         mesh_path_fix_nexthop(mpath, sta);
870
871         rcu_read_unlock();
872         return 0;
873 }
874
875 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
876                                  u8 *dst)
877 {
878         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
879
880         if (dst)
881                 return mesh_path_del(dst, sdata);
882
883         mesh_path_flush(sdata);
884         return 0;
885 }
886
887 static int ieee80211_change_mpath(struct wiphy *wiphy,
888                                     struct net_device *dev,
889                                     u8 *dst, u8 *next_hop)
890 {
891         struct ieee80211_sub_if_data *sdata;
892         struct mesh_path *mpath;
893         struct sta_info *sta;
894
895         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
896
897         rcu_read_lock();
898
899         sta = sta_info_get(sdata, next_hop);
900         if (!sta) {
901                 rcu_read_unlock();
902                 return -ENOENT;
903         }
904
905         mpath = mesh_path_lookup(dst, sdata);
906         if (!mpath) {
907                 rcu_read_unlock();
908                 return -ENOENT;
909         }
910
911         mesh_path_fix_nexthop(mpath, sta);
912
913         rcu_read_unlock();
914         return 0;
915 }
916
917 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
918                             struct mpath_info *pinfo)
919 {
920         if (mpath->next_hop)
921                 memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
922         else
923                 memset(next_hop, 0, ETH_ALEN);
924
925         pinfo->generation = mesh_paths_generation;
926
927         pinfo->filled = MPATH_INFO_FRAME_QLEN |
928                         MPATH_INFO_SN |
929                         MPATH_INFO_METRIC |
930                         MPATH_INFO_EXPTIME |
931                         MPATH_INFO_DISCOVERY_TIMEOUT |
932                         MPATH_INFO_DISCOVERY_RETRIES |
933                         MPATH_INFO_FLAGS;
934
935         pinfo->frame_qlen = mpath->frame_queue.qlen;
936         pinfo->sn = mpath->sn;
937         pinfo->metric = mpath->metric;
938         if (time_before(jiffies, mpath->exp_time))
939                 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
940         pinfo->discovery_timeout =
941                         jiffies_to_msecs(mpath->discovery_timeout);
942         pinfo->discovery_retries = mpath->discovery_retries;
943         pinfo->flags = 0;
944         if (mpath->flags & MESH_PATH_ACTIVE)
945                 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
946         if (mpath->flags & MESH_PATH_RESOLVING)
947                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
948         if (mpath->flags & MESH_PATH_SN_VALID)
949                 pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
950         if (mpath->flags & MESH_PATH_FIXED)
951                 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
952         if (mpath->flags & MESH_PATH_RESOLVING)
953                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
954
955         pinfo->flags = mpath->flags;
956 }
957
958 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
959                                u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
960
961 {
962         struct ieee80211_sub_if_data *sdata;
963         struct mesh_path *mpath;
964
965         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
966
967         rcu_read_lock();
968         mpath = mesh_path_lookup(dst, sdata);
969         if (!mpath) {
970                 rcu_read_unlock();
971                 return -ENOENT;
972         }
973         memcpy(dst, mpath->dst, ETH_ALEN);
974         mpath_set_pinfo(mpath, next_hop, pinfo);
975         rcu_read_unlock();
976         return 0;
977 }
978
979 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
980                                  int idx, u8 *dst, u8 *next_hop,
981                                  struct mpath_info *pinfo)
982 {
983         struct ieee80211_sub_if_data *sdata;
984         struct mesh_path *mpath;
985
986         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
987
988         rcu_read_lock();
989         mpath = mesh_path_lookup_by_idx(idx, sdata);
990         if (!mpath) {
991                 rcu_read_unlock();
992                 return -ENOENT;
993         }
994         memcpy(dst, mpath->dst, ETH_ALEN);
995         mpath_set_pinfo(mpath, next_hop, pinfo);
996         rcu_read_unlock();
997         return 0;
998 }
999
1000 static int ieee80211_get_mesh_params(struct wiphy *wiphy,
1001                                 struct net_device *dev,
1002                                 struct mesh_config *conf)
1003 {
1004         struct ieee80211_sub_if_data *sdata;
1005         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1006
1007         memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
1008         return 0;
1009 }
1010
1011 static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
1012 {
1013         return (mask >> (parm-1)) & 0x1;
1014 }
1015
1016 static int ieee80211_set_mesh_params(struct wiphy *wiphy,
1017                                 struct net_device *dev,
1018                                 const struct mesh_config *nconf, u32 mask)
1019 {
1020         struct mesh_config *conf;
1021         struct ieee80211_sub_if_data *sdata;
1022         struct ieee80211_if_mesh *ifmsh;
1023
1024         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1025         ifmsh = &sdata->u.mesh;
1026
1027         /* Set the config options which we are interested in setting */
1028         conf = &(sdata->u.mesh.mshcfg);
1029         if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1030                 conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1031         if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1032                 conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1033         if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1034                 conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1035         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1036                 conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1037         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1038                 conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1039         if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1040                 conf->dot11MeshTTL = nconf->dot11MeshTTL;
1041         if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask))
1042                 conf->auto_open_plinks = nconf->auto_open_plinks;
1043         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1044                 conf->dot11MeshHWMPmaxPREQretries =
1045                         nconf->dot11MeshHWMPmaxPREQretries;
1046         if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1047                 conf->path_refresh_time = nconf->path_refresh_time;
1048         if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1049                 conf->min_discovery_timeout = nconf->min_discovery_timeout;
1050         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1051                 conf->dot11MeshHWMPactivePathTimeout =
1052                         nconf->dot11MeshHWMPactivePathTimeout;
1053         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1054                 conf->dot11MeshHWMPpreqMinInterval =
1055                         nconf->dot11MeshHWMPpreqMinInterval;
1056         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1057                            mask))
1058                 conf->dot11MeshHWMPnetDiameterTraversalTime =
1059                         nconf->dot11MeshHWMPnetDiameterTraversalTime;
1060         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
1061                 conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
1062                 ieee80211_mesh_root_setup(ifmsh);
1063         }
1064         return 0;
1065 }
1066
1067 #endif
1068
1069 static int ieee80211_change_bss(struct wiphy *wiphy,
1070                                 struct net_device *dev,
1071                                 struct bss_parameters *params)
1072 {
1073         struct ieee80211_sub_if_data *sdata;
1074         u32 changed = 0;
1075
1076         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1077
1078         if (params->use_cts_prot >= 0) {
1079                 sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
1080                 changed |= BSS_CHANGED_ERP_CTS_PROT;
1081         }
1082         if (params->use_short_preamble >= 0) {
1083                 sdata->vif.bss_conf.use_short_preamble =
1084                         params->use_short_preamble;
1085                 changed |= BSS_CHANGED_ERP_PREAMBLE;
1086         }
1087
1088         if (!sdata->vif.bss_conf.use_short_slot &&
1089             sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ) {
1090                 sdata->vif.bss_conf.use_short_slot = true;
1091                 changed |= BSS_CHANGED_ERP_SLOT;
1092         }
1093
1094         if (params->use_short_slot_time >= 0) {
1095                 sdata->vif.bss_conf.use_short_slot =
1096                         params->use_short_slot_time;
1097                 changed |= BSS_CHANGED_ERP_SLOT;
1098         }
1099
1100         if (params->basic_rates) {
1101                 int i, j;
1102                 u32 rates = 0;
1103                 struct ieee80211_local *local = wiphy_priv(wiphy);
1104                 struct ieee80211_supported_band *sband =
1105                         wiphy->bands[local->oper_channel->band];
1106
1107                 for (i = 0; i < params->basic_rates_len; i++) {
1108                         int rate = (params->basic_rates[i] & 0x7f) * 5;
1109                         for (j = 0; j < sband->n_bitrates; j++) {
1110                                 if (sband->bitrates[j].bitrate == rate)
1111                                         rates |= BIT(j);
1112                         }
1113                 }
1114                 sdata->vif.bss_conf.basic_rates = rates;
1115                 changed |= BSS_CHANGED_BASIC_RATES;
1116         }
1117
1118         ieee80211_bss_info_change_notify(sdata, changed);
1119
1120         return 0;
1121 }
1122
1123 static int ieee80211_set_txq_params(struct wiphy *wiphy,
1124                                     struct ieee80211_txq_params *params)
1125 {
1126         struct ieee80211_local *local = wiphy_priv(wiphy);
1127         struct ieee80211_tx_queue_params p;
1128
1129         if (!local->ops->conf_tx)
1130                 return -EOPNOTSUPP;
1131
1132         memset(&p, 0, sizeof(p));
1133         p.aifs = params->aifs;
1134         p.cw_max = params->cwmax;
1135         p.cw_min = params->cwmin;
1136         p.txop = params->txop;
1137
1138         /*
1139          * Setting tx queue params disables u-apsd because it's only
1140          * called in master mode.
1141          */
1142         p.uapsd = false;
1143
1144         if (drv_conf_tx(local, params->queue, &p)) {
1145                 printk(KERN_DEBUG "%s: failed to set TX queue "
1146                        "parameters for queue %d\n",
1147                        wiphy_name(local->hw.wiphy), params->queue);
1148                 return -EINVAL;
1149         }
1150
1151         /* enable WMM or activate new settings */
1152         local->hw.conf.flags |= IEEE80211_CONF_QOS;
1153         drv_config(local, IEEE80211_CONF_CHANGE_QOS);
1154
1155         return 0;
1156 }
1157
1158 static int ieee80211_set_channel(struct wiphy *wiphy,
1159                                  struct ieee80211_channel *chan,
1160                                  enum nl80211_channel_type channel_type)
1161 {
1162         struct ieee80211_local *local = wiphy_priv(wiphy);
1163
1164         local->oper_channel = chan;
1165         local->oper_channel_type = channel_type;
1166
1167         return ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
1168 }
1169
1170 #ifdef CONFIG_PM
1171 static int ieee80211_suspend(struct wiphy *wiphy)
1172 {
1173         return __ieee80211_suspend(wiphy_priv(wiphy));
1174 }
1175
1176 static int ieee80211_resume(struct wiphy *wiphy)
1177 {
1178         return __ieee80211_resume(wiphy_priv(wiphy));
1179 }
1180 #else
1181 #define ieee80211_suspend NULL
1182 #define ieee80211_resume NULL
1183 #endif
1184
1185 static int ieee80211_scan(struct wiphy *wiphy,
1186                           struct net_device *dev,
1187                           struct cfg80211_scan_request *req)
1188 {
1189         struct ieee80211_sub_if_data *sdata;
1190
1191         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1192
1193         if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1194             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1195             sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
1196             (sdata->vif.type != NL80211_IFTYPE_AP || sdata->u.ap.beacon))
1197                 return -EOPNOTSUPP;
1198
1199         return ieee80211_request_scan(sdata, req);
1200 }
1201
1202 static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
1203                           struct cfg80211_auth_request *req)
1204 {
1205         return ieee80211_mgd_auth(IEEE80211_DEV_TO_SUB_IF(dev), req);
1206 }
1207
1208 static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
1209                            struct cfg80211_assoc_request *req)
1210 {
1211         return ieee80211_mgd_assoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
1212 }
1213
1214 static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
1215                             struct cfg80211_deauth_request *req,
1216                             void *cookie)
1217 {
1218         return ieee80211_mgd_deauth(IEEE80211_DEV_TO_SUB_IF(dev),
1219                                     req, cookie);
1220 }
1221
1222 static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
1223                               struct cfg80211_disassoc_request *req,
1224                               void *cookie)
1225 {
1226         return ieee80211_mgd_disassoc(IEEE80211_DEV_TO_SUB_IF(dev),
1227                                       req, cookie);
1228 }
1229
1230 static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1231                                struct cfg80211_ibss_params *params)
1232 {
1233         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1234
1235         return ieee80211_ibss_join(sdata, params);
1236 }
1237
1238 static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1239 {
1240         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1241
1242         return ieee80211_ibss_leave(sdata);
1243 }
1244
1245 static int ieee80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1246 {
1247         struct ieee80211_local *local = wiphy_priv(wiphy);
1248         int err;
1249
1250         if (changed & WIPHY_PARAM_COVERAGE_CLASS) {
1251                 err = drv_set_coverage_class(local, wiphy->coverage_class);
1252
1253                 if (err)
1254                         return err;
1255         }
1256
1257         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1258                 err = drv_set_rts_threshold(local, wiphy->rts_threshold);
1259
1260                 if (err)
1261                         return err;
1262         }
1263
1264         if (changed & WIPHY_PARAM_RETRY_SHORT)
1265                 local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
1266         if (changed & WIPHY_PARAM_RETRY_LONG)
1267                 local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
1268         if (changed &
1269             (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
1270                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
1271
1272         return 0;
1273 }
1274
1275 static int ieee80211_set_tx_power(struct wiphy *wiphy,
1276                                   enum tx_power_setting type, int dbm)
1277 {
1278         struct ieee80211_local *local = wiphy_priv(wiphy);
1279         struct ieee80211_channel *chan = local->hw.conf.channel;
1280         u32 changes = 0;
1281
1282         switch (type) {
1283         case TX_POWER_AUTOMATIC:
1284                 local->user_power_level = -1;
1285                 break;
1286         case TX_POWER_LIMITED:
1287                 if (dbm < 0)
1288                         return -EINVAL;
1289                 local->user_power_level = dbm;
1290                 break;
1291         case TX_POWER_FIXED:
1292                 if (dbm < 0)
1293                         return -EINVAL;
1294                 /* TODO: move to cfg80211 when it knows the channel */
1295                 if (dbm > chan->max_power)
1296                         return -EINVAL;
1297                 local->user_power_level = dbm;
1298                 break;
1299         }
1300
1301         ieee80211_hw_config(local, changes);
1302
1303         return 0;
1304 }
1305
1306 static int ieee80211_get_tx_power(struct wiphy *wiphy, int *dbm)
1307 {
1308         struct ieee80211_local *local = wiphy_priv(wiphy);
1309
1310         *dbm = local->hw.conf.power_level;
1311
1312         return 0;
1313 }
1314
1315 static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
1316                                   u8 *addr)
1317 {
1318         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1319
1320         memcpy(&sdata->u.wds.remote_addr, addr, ETH_ALEN);
1321
1322         return 0;
1323 }
1324
1325 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
1326 {
1327         struct ieee80211_local *local = wiphy_priv(wiphy);
1328
1329         drv_rfkill_poll(local);
1330 }
1331
1332 #ifdef CONFIG_NL80211_TESTMODE
1333 static int ieee80211_testmode_cmd(struct wiphy *wiphy, void *data, int len)
1334 {
1335         struct ieee80211_local *local = wiphy_priv(wiphy);
1336
1337         if (!local->ops->testmode_cmd)
1338                 return -EOPNOTSUPP;
1339
1340         return local->ops->testmode_cmd(&local->hw, data, len);
1341 }
1342 #endif
1343
1344 int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata,
1345                              enum ieee80211_smps_mode smps_mode)
1346 {
1347         const u8 *ap;
1348         enum ieee80211_smps_mode old_req;
1349         int err;
1350
1351         old_req = sdata->u.mgd.req_smps;
1352         sdata->u.mgd.req_smps = smps_mode;
1353
1354         if (old_req == smps_mode &&
1355             smps_mode != IEEE80211_SMPS_AUTOMATIC)
1356                 return 0;
1357
1358         /*
1359          * If not associated, or current association is not an HT
1360          * association, there's no need to send an action frame.
1361          */
1362         if (!sdata->u.mgd.associated ||
1363             sdata->local->oper_channel_type == NL80211_CHAN_NO_HT) {
1364                 mutex_lock(&sdata->local->iflist_mtx);
1365                 ieee80211_recalc_smps(sdata->local, sdata);
1366                 mutex_unlock(&sdata->local->iflist_mtx);
1367                 return 0;
1368         }
1369
1370         ap = sdata->u.mgd.associated->bssid;
1371
1372         if (smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1373                 if (sdata->u.mgd.powersave)
1374                         smps_mode = IEEE80211_SMPS_DYNAMIC;
1375                 else
1376                         smps_mode = IEEE80211_SMPS_OFF;
1377         }
1378
1379         /* send SM PS frame to AP */
1380         err = ieee80211_send_smps_action(sdata, smps_mode,
1381                                          ap, ap);
1382         if (err)
1383                 sdata->u.mgd.req_smps = old_req;
1384
1385         return err;
1386 }
1387
1388 static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
1389                                     bool enabled, int timeout)
1390 {
1391         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1392         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1393         struct ieee80211_conf *conf = &local->hw.conf;
1394
1395         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1396                 return -EOPNOTSUPP;
1397
1398         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
1399                 return -EOPNOTSUPP;
1400
1401         if (enabled == sdata->u.mgd.powersave &&
1402             timeout == conf->dynamic_ps_timeout)
1403                 return 0;
1404
1405         sdata->u.mgd.powersave = enabled;
1406         conf->dynamic_ps_timeout = timeout;
1407
1408         /* no change, but if automatic follow powersave */
1409         mutex_lock(&sdata->u.mgd.mtx);
1410         __ieee80211_request_smps(sdata, sdata->u.mgd.req_smps);
1411         mutex_unlock(&sdata->u.mgd.mtx);
1412
1413         if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
1414                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1415
1416         ieee80211_recalc_ps(local, -1);
1417
1418         return 0;
1419 }
1420
1421 static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
1422                                          struct net_device *dev,
1423                                          s32 rssi_thold, u32 rssi_hyst)
1424 {
1425         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1426         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1427         struct ieee80211_vif *vif = &sdata->vif;
1428         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1429
1430         if (rssi_thold == bss_conf->cqm_rssi_thold &&
1431             rssi_hyst == bss_conf->cqm_rssi_hyst)
1432                 return 0;
1433
1434         bss_conf->cqm_rssi_thold = rssi_thold;
1435         bss_conf->cqm_rssi_hyst = rssi_hyst;
1436
1437         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1438                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1439                         return -EOPNOTSUPP;
1440                 return 0;
1441         }
1442
1443         /* tell the driver upon association, unless already associated */
1444         if (sdata->u.mgd.associated)
1445                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
1446
1447         return 0;
1448 }
1449
1450 static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
1451                                       struct net_device *dev,
1452                                       const u8 *addr,
1453                                       const struct cfg80211_bitrate_mask *mask)
1454 {
1455         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1456         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1457         int i;
1458
1459         /*
1460          * This _could_ be supported by providing a hook for
1461          * drivers for this function, but at this point it
1462          * doesn't seem worth bothering.
1463          */
1464         if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
1465                 return -EOPNOTSUPP;
1466
1467
1468         for (i = 0; i < IEEE80211_NUM_BANDS; i++)
1469                 sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
1470
1471         return 0;
1472 }
1473
1474 static int ieee80211_remain_on_channel(struct wiphy *wiphy,
1475                                        struct net_device *dev,
1476                                        struct ieee80211_channel *chan,
1477                                        enum nl80211_channel_type channel_type,
1478                                        unsigned int duration,
1479                                        u64 *cookie)
1480 {
1481         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1482
1483         return ieee80211_wk_remain_on_channel(sdata, chan, channel_type,
1484                                               duration, cookie);
1485 }
1486
1487 static int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
1488                                               struct net_device *dev,
1489                                               u64 cookie)
1490 {
1491         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1492
1493         return ieee80211_wk_cancel_remain_on_channel(sdata, cookie);
1494 }
1495
1496 static int ieee80211_action(struct wiphy *wiphy, struct net_device *dev,
1497                             struct ieee80211_channel *chan,
1498                             enum nl80211_channel_type channel_type,
1499                             const u8 *buf, size_t len, u64 *cookie)
1500 {
1501         return ieee80211_mgd_action(IEEE80211_DEV_TO_SUB_IF(dev), chan,
1502                                     channel_type, buf, len, cookie);
1503 }
1504
1505 struct cfg80211_ops mac80211_config_ops = {
1506         .add_virtual_intf = ieee80211_add_iface,
1507         .del_virtual_intf = ieee80211_del_iface,
1508         .change_virtual_intf = ieee80211_change_iface,
1509         .add_key = ieee80211_add_key,
1510         .del_key = ieee80211_del_key,
1511         .get_key = ieee80211_get_key,
1512         .set_default_key = ieee80211_config_default_key,
1513         .set_default_mgmt_key = ieee80211_config_default_mgmt_key,
1514         .add_beacon = ieee80211_add_beacon,
1515         .set_beacon = ieee80211_set_beacon,
1516         .del_beacon = ieee80211_del_beacon,
1517         .add_station = ieee80211_add_station,
1518         .del_station = ieee80211_del_station,
1519         .change_station = ieee80211_change_station,
1520         .get_station = ieee80211_get_station,
1521         .dump_station = ieee80211_dump_station,
1522         .dump_survey = ieee80211_dump_survey,
1523 #ifdef CONFIG_MAC80211_MESH
1524         .add_mpath = ieee80211_add_mpath,
1525         .del_mpath = ieee80211_del_mpath,
1526         .change_mpath = ieee80211_change_mpath,
1527         .get_mpath = ieee80211_get_mpath,
1528         .dump_mpath = ieee80211_dump_mpath,
1529         .set_mesh_params = ieee80211_set_mesh_params,
1530         .get_mesh_params = ieee80211_get_mesh_params,
1531 #endif
1532         .change_bss = ieee80211_change_bss,
1533         .set_txq_params = ieee80211_set_txq_params,
1534         .set_channel = ieee80211_set_channel,
1535         .suspend = ieee80211_suspend,
1536         .resume = ieee80211_resume,
1537         .scan = ieee80211_scan,
1538         .auth = ieee80211_auth,
1539         .assoc = ieee80211_assoc,
1540         .deauth = ieee80211_deauth,
1541         .disassoc = ieee80211_disassoc,
1542         .join_ibss = ieee80211_join_ibss,
1543         .leave_ibss = ieee80211_leave_ibss,
1544         .set_wiphy_params = ieee80211_set_wiphy_params,
1545         .set_tx_power = ieee80211_set_tx_power,
1546         .get_tx_power = ieee80211_get_tx_power,
1547         .set_wds_peer = ieee80211_set_wds_peer,
1548         .rfkill_poll = ieee80211_rfkill_poll,
1549         CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
1550         .set_power_mgmt = ieee80211_set_power_mgmt,
1551         .set_bitrate_mask = ieee80211_set_bitrate_mask,
1552         .remain_on_channel = ieee80211_remain_on_channel,
1553         .cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
1554         .action = ieee80211_action,
1555         .set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
1556 };