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