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