mac80211/cfg80211: HT capabilities for NEW_STA
[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 "cfg.h"
17 #include "rate.h"
18 #include "mesh.h"
19
20 static enum ieee80211_if_types
21 nl80211_type_to_mac80211_type(enum nl80211_iftype type)
22 {
23         switch (type) {
24         case NL80211_IFTYPE_UNSPECIFIED:
25                 return IEEE80211_IF_TYPE_STA;
26         case NL80211_IFTYPE_ADHOC:
27                 return IEEE80211_IF_TYPE_IBSS;
28         case NL80211_IFTYPE_STATION:
29                 return IEEE80211_IF_TYPE_STA;
30         case NL80211_IFTYPE_MONITOR:
31                 return IEEE80211_IF_TYPE_MNTR;
32 #ifdef CONFIG_MAC80211_MESH
33         case NL80211_IFTYPE_MESH_POINT:
34                 return IEEE80211_IF_TYPE_MESH_POINT;
35 #endif
36         case NL80211_IFTYPE_WDS:
37                 return IEEE80211_IF_TYPE_WDS;
38         default:
39                 return IEEE80211_IF_TYPE_INVALID;
40         }
41 }
42
43 static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
44                                enum nl80211_iftype type, u32 *flags,
45                                struct vif_params *params)
46 {
47         struct ieee80211_local *local = wiphy_priv(wiphy);
48         enum ieee80211_if_types itype;
49         struct net_device *dev;
50         struct ieee80211_sub_if_data *sdata;
51         int err;
52
53         itype = nl80211_type_to_mac80211_type(type);
54         if (itype == IEEE80211_IF_TYPE_INVALID)
55                 return -EINVAL;
56
57         err = ieee80211_if_add(local, name, &dev, itype, params);
58         if (err || itype != IEEE80211_IF_TYPE_MNTR || !flags)
59                 return err;
60
61         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
62         sdata->u.mntr_flags = *flags;
63         return 0;
64 }
65
66 static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
67 {
68         struct net_device *dev;
69         struct ieee80211_sub_if_data *sdata;
70
71         /* we're under RTNL */
72         dev = __dev_get_by_index(&init_net, ifindex);
73         if (!dev)
74                 return -ENODEV;
75
76         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
77
78         ieee80211_if_remove(sdata);
79
80         return 0;
81 }
82
83 static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
84                                   enum nl80211_iftype type, u32 *flags,
85                                   struct vif_params *params)
86 {
87         struct ieee80211_local *local = wiphy_priv(wiphy);
88         struct net_device *dev;
89         enum ieee80211_if_types itype;
90         struct ieee80211_sub_if_data *sdata;
91         int ret;
92
93         /* we're under RTNL */
94         dev = __dev_get_by_index(&init_net, ifindex);
95         if (!dev)
96                 return -ENODEV;
97
98         itype = nl80211_type_to_mac80211_type(type);
99         if (itype == IEEE80211_IF_TYPE_INVALID)
100                 return -EINVAL;
101
102         if (dev == local->mdev)
103                 return -EOPNOTSUPP;
104
105         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
106
107         ret = ieee80211_if_change_type(sdata, itype);
108         if (ret)
109                 return ret;
110
111         if (ieee80211_vif_is_mesh(&sdata->vif) && params->mesh_id_len)
112                 ieee80211_if_sta_set_mesh_id(&sdata->u.sta,
113                                              params->mesh_id_len,
114                                              params->mesh_id);
115
116         if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR || !flags)
117                 return 0;
118
119         sdata->u.mntr_flags = *flags;
120         return 0;
121 }
122
123 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
124                              u8 key_idx, u8 *mac_addr,
125                              struct key_params *params)
126 {
127         struct ieee80211_local *local = wiphy_priv(wiphy);
128         struct ieee80211_sub_if_data *sdata;
129         struct sta_info *sta = NULL;
130         enum ieee80211_key_alg alg;
131         struct ieee80211_key *key;
132         int err;
133
134         if (dev == local->mdev)
135                 return -EOPNOTSUPP;
136
137         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
138
139         switch (params->cipher) {
140         case WLAN_CIPHER_SUITE_WEP40:
141         case WLAN_CIPHER_SUITE_WEP104:
142                 alg = ALG_WEP;
143                 break;
144         case WLAN_CIPHER_SUITE_TKIP:
145                 alg = ALG_TKIP;
146                 break;
147         case WLAN_CIPHER_SUITE_CCMP:
148                 alg = ALG_CCMP;
149                 break;
150         default:
151                 return -EINVAL;
152         }
153
154         key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key);
155         if (!key)
156                 return -ENOMEM;
157
158         rcu_read_lock();
159
160         if (mac_addr) {
161                 sta = sta_info_get(sdata->local, mac_addr);
162                 if (!sta) {
163                         ieee80211_key_free(key);
164                         err = -ENOENT;
165                         goto out_unlock;
166                 }
167         }
168
169         ieee80211_key_link(key, sdata, sta);
170
171         err = 0;
172  out_unlock:
173         rcu_read_unlock();
174
175         return err;
176 }
177
178 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
179                              u8 key_idx, u8 *mac_addr)
180 {
181         struct ieee80211_local *local = wiphy_priv(wiphy);
182         struct ieee80211_sub_if_data *sdata;
183         struct sta_info *sta;
184         int ret;
185
186         if (dev == local->mdev)
187                 return -EOPNOTSUPP;
188
189         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
190
191         rcu_read_lock();
192
193         if (mac_addr) {
194                 ret = -ENOENT;
195
196                 sta = sta_info_get(sdata->local, mac_addr);
197                 if (!sta)
198                         goto out_unlock;
199
200                 if (sta->key) {
201                         ieee80211_key_free(sta->key);
202                         WARN_ON(sta->key);
203                         ret = 0;
204                 }
205
206                 goto out_unlock;
207         }
208
209         if (!sdata->keys[key_idx]) {
210                 ret = -ENOENT;
211                 goto out_unlock;
212         }
213
214         ieee80211_key_free(sdata->keys[key_idx]);
215         WARN_ON(sdata->keys[key_idx]);
216
217         ret = 0;
218  out_unlock:
219         rcu_read_unlock();
220
221         return ret;
222 }
223
224 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
225                              u8 key_idx, u8 *mac_addr, void *cookie,
226                              void (*callback)(void *cookie,
227                                               struct key_params *params))
228 {
229         struct ieee80211_local *local = wiphy_priv(wiphy);
230         struct ieee80211_sub_if_data *sdata;
231         struct sta_info *sta = NULL;
232         u8 seq[6] = {0};
233         struct key_params params;
234         struct ieee80211_key *key;
235         u32 iv32;
236         u16 iv16;
237         int err = -ENOENT;
238
239         if (dev == local->mdev)
240                 return -EOPNOTSUPP;
241
242         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
243
244         rcu_read_lock();
245
246         if (mac_addr) {
247                 sta = sta_info_get(sdata->local, mac_addr);
248                 if (!sta)
249                         goto out;
250
251                 key = sta->key;
252         } else
253                 key = sdata->keys[key_idx];
254
255         if (!key)
256                 goto out;
257
258         memset(&params, 0, sizeof(params));
259
260         switch (key->conf.alg) {
261         case ALG_TKIP:
262                 params.cipher = WLAN_CIPHER_SUITE_TKIP;
263
264                 iv32 = key->u.tkip.tx.iv32;
265                 iv16 = key->u.tkip.tx.iv16;
266
267                 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
268                     sdata->local->ops->get_tkip_seq)
269                         sdata->local->ops->get_tkip_seq(
270                                 local_to_hw(sdata->local),
271                                 key->conf.hw_key_idx,
272                                 &iv32, &iv16);
273
274                 seq[0] = iv16 & 0xff;
275                 seq[1] = (iv16 >> 8) & 0xff;
276                 seq[2] = iv32 & 0xff;
277                 seq[3] = (iv32 >> 8) & 0xff;
278                 seq[4] = (iv32 >> 16) & 0xff;
279                 seq[5] = (iv32 >> 24) & 0xff;
280                 params.seq = seq;
281                 params.seq_len = 6;
282                 break;
283         case ALG_CCMP:
284                 params.cipher = WLAN_CIPHER_SUITE_CCMP;
285                 seq[0] = key->u.ccmp.tx_pn[5];
286                 seq[1] = key->u.ccmp.tx_pn[4];
287                 seq[2] = key->u.ccmp.tx_pn[3];
288                 seq[3] = key->u.ccmp.tx_pn[2];
289                 seq[4] = key->u.ccmp.tx_pn[1];
290                 seq[5] = key->u.ccmp.tx_pn[0];
291                 params.seq = seq;
292                 params.seq_len = 6;
293                 break;
294         case ALG_WEP:
295                 if (key->conf.keylen == 5)
296                         params.cipher = WLAN_CIPHER_SUITE_WEP40;
297                 else
298                         params.cipher = WLAN_CIPHER_SUITE_WEP104;
299                 break;
300         }
301
302         params.key = key->conf.key;
303         params.key_len = key->conf.keylen;
304
305         callback(cookie, &params);
306         err = 0;
307
308  out:
309         rcu_read_unlock();
310         return err;
311 }
312
313 static int ieee80211_config_default_key(struct wiphy *wiphy,
314                                         struct net_device *dev,
315                                         u8 key_idx)
316 {
317         struct ieee80211_local *local = wiphy_priv(wiphy);
318         struct ieee80211_sub_if_data *sdata;
319
320         if (dev == local->mdev)
321                 return -EOPNOTSUPP;
322
323         rcu_read_lock();
324
325         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
326         ieee80211_set_default_key(sdata, key_idx);
327
328         rcu_read_unlock();
329
330         return 0;
331 }
332
333 static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
334 {
335         struct ieee80211_sub_if_data *sdata = sta->sdata;
336
337         sinfo->filled = STATION_INFO_INACTIVE_TIME |
338                         STATION_INFO_RX_BYTES |
339                         STATION_INFO_TX_BYTES;
340
341         sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
342         sinfo->rx_bytes = sta->rx_bytes;
343         sinfo->tx_bytes = sta->tx_bytes;
344
345         if (ieee80211_vif_is_mesh(&sdata->vif)) {
346 #ifdef CONFIG_MAC80211_MESH
347                 sinfo->filled |= STATION_INFO_LLID |
348                                  STATION_INFO_PLID |
349                                  STATION_INFO_PLINK_STATE;
350
351                 sinfo->llid = le16_to_cpu(sta->llid);
352                 sinfo->plid = le16_to_cpu(sta->plid);
353                 sinfo->plink_state = sta->plink_state;
354 #endif
355         }
356 }
357
358
359 static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
360                                  int idx, u8 *mac, struct station_info *sinfo)
361 {
362         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
363         struct sta_info *sta;
364         int ret = -ENOENT;
365
366         rcu_read_lock();
367
368         sta = sta_info_get_by_idx(local, idx, dev);
369         if (sta) {
370                 ret = 0;
371                 memcpy(mac, sta->addr, ETH_ALEN);
372                 sta_set_sinfo(sta, sinfo);
373         }
374
375         rcu_read_unlock();
376
377         return ret;
378 }
379
380 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
381                                  u8 *mac, struct station_info *sinfo)
382 {
383         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
384         struct sta_info *sta;
385         int ret = -ENOENT;
386
387         rcu_read_lock();
388
389         /* XXX: verify sta->dev == dev */
390
391         sta = sta_info_get(local, mac);
392         if (sta) {
393                 ret = 0;
394                 sta_set_sinfo(sta, sinfo);
395         }
396
397         rcu_read_unlock();
398
399         return ret;
400 }
401
402 /*
403  * This handles both adding a beacon and setting new beacon info
404  */
405 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
406                                    struct beacon_parameters *params)
407 {
408         struct beacon_data *new, *old;
409         int new_head_len, new_tail_len;
410         int size;
411         int err = -EINVAL;
412
413         old = sdata->u.ap.beacon;
414
415         /* head must not be zero-length */
416         if (params->head && !params->head_len)
417                 return -EINVAL;
418
419         /*
420          * This is a kludge. beacon interval should really be part
421          * of the beacon information.
422          */
423         if (params->interval) {
424                 sdata->local->hw.conf.beacon_int = params->interval;
425                 if (ieee80211_hw_config(sdata->local))
426                         return -EINVAL;
427                 /*
428                  * We updated some parameter so if below bails out
429                  * it's not an error.
430                  */
431                 err = 0;
432         }
433
434         /* Need to have a beacon head if we don't have one yet */
435         if (!params->head && !old)
436                 return err;
437
438         /* sorry, no way to start beaconing without dtim period */
439         if (!params->dtim_period && !old)
440                 return err;
441
442         /* new or old head? */
443         if (params->head)
444                 new_head_len = params->head_len;
445         else
446                 new_head_len = old->head_len;
447
448         /* new or old tail? */
449         if (params->tail || !old)
450                 /* params->tail_len will be zero for !params->tail */
451                 new_tail_len = params->tail_len;
452         else
453                 new_tail_len = old->tail_len;
454
455         size = sizeof(*new) + new_head_len + new_tail_len;
456
457         new = kzalloc(size, GFP_KERNEL);
458         if (!new)
459                 return -ENOMEM;
460
461         /* start filling the new info now */
462
463         /* new or old dtim period? */
464         if (params->dtim_period)
465                 new->dtim_period = params->dtim_period;
466         else
467                 new->dtim_period = old->dtim_period;
468
469         /*
470          * pointers go into the block we allocated,
471          * memory is | beacon_data | head | tail |
472          */
473         new->head = ((u8 *) new) + sizeof(*new);
474         new->tail = new->head + new_head_len;
475         new->head_len = new_head_len;
476         new->tail_len = new_tail_len;
477
478         /* copy in head */
479         if (params->head)
480                 memcpy(new->head, params->head, new_head_len);
481         else
482                 memcpy(new->head, old->head, new_head_len);
483
484         /* copy in optional tail */
485         if (params->tail)
486                 memcpy(new->tail, params->tail, new_tail_len);
487         else
488                 if (old)
489                         memcpy(new->tail, old->tail, new_tail_len);
490
491         rcu_assign_pointer(sdata->u.ap.beacon, new);
492
493         synchronize_rcu();
494
495         kfree(old);
496
497         return ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
498 }
499
500 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
501                                 struct beacon_parameters *params)
502 {
503         struct ieee80211_local *local = wiphy_priv(wiphy);
504         struct ieee80211_sub_if_data *sdata;
505         struct beacon_data *old;
506
507         if (dev == local->mdev)
508                 return -EOPNOTSUPP;
509
510         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
511
512         if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
513                 return -EINVAL;
514
515         old = sdata->u.ap.beacon;
516
517         if (old)
518                 return -EALREADY;
519
520         return ieee80211_config_beacon(sdata, params);
521 }
522
523 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
524                                 struct beacon_parameters *params)
525 {
526         struct ieee80211_local *local = wiphy_priv(wiphy);
527         struct ieee80211_sub_if_data *sdata;
528         struct beacon_data *old;
529
530         if (dev == local->mdev)
531                 return -EOPNOTSUPP;
532
533         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
534
535         if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
536                 return -EINVAL;
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_local *local = wiphy_priv(wiphy);
549         struct ieee80211_sub_if_data *sdata;
550         struct beacon_data *old;
551
552         if (dev == local->mdev)
553                 return -EOPNOTSUPP;
554
555         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
556
557         if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
558                 return -EINVAL;
559
560         old = sdata->u.ap.beacon;
561
562         if (!old)
563                 return -ENOENT;
564
565         rcu_assign_pointer(sdata->u.ap.beacon, NULL);
566         synchronize_rcu();
567         kfree(old);
568
569         return ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
570 }
571
572 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
573 struct iapp_layer2_update {
574         u8 da[ETH_ALEN];        /* broadcast */
575         u8 sa[ETH_ALEN];        /* STA addr */
576         __be16 len;             /* 6 */
577         u8 dsap;                /* 0 */
578         u8 ssap;                /* 0 */
579         u8 control;
580         u8 xid_info[3];
581 } __attribute__ ((packed));
582
583 static void ieee80211_send_layer2_update(struct sta_info *sta)
584 {
585         struct iapp_layer2_update *msg;
586         struct sk_buff *skb;
587
588         /* Send Level 2 Update Frame to update forwarding tables in layer 2
589          * bridge devices */
590
591         skb = dev_alloc_skb(sizeof(*msg));
592         if (!skb)
593                 return;
594         msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
595
596         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
597          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
598
599         memset(msg->da, 0xff, ETH_ALEN);
600         memcpy(msg->sa, sta->addr, ETH_ALEN);
601         msg->len = htons(6);
602         msg->dsap = 0;
603         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
604         msg->control = 0xaf;    /* XID response lsb.1111F101.
605                                  * F=0 (no poll command; unsolicited frame) */
606         msg->xid_info[0] = 0x81;        /* XID format identifier */
607         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
608         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
609
610         skb->dev = sta->sdata->dev;
611         skb->protocol = eth_type_trans(skb, sta->sdata->dev);
612         memset(skb->cb, 0, sizeof(skb->cb));
613         netif_rx(skb);
614 }
615
616 static void sta_apply_parameters(struct ieee80211_local *local,
617                                  struct sta_info *sta,
618                                  struct station_parameters *params)
619 {
620         u32 rates;
621         int i, j;
622         struct ieee80211_supported_band *sband;
623         struct ieee80211_sub_if_data *sdata = sta->sdata;
624
625         /*
626          * FIXME: updating the flags is racy when this function is
627          *        called from ieee80211_change_station(), this will
628          *        be resolved in a future patch.
629          */
630
631         if (params->station_flags & STATION_FLAG_CHANGED) {
632                 spin_lock_bh(&sta->lock);
633                 sta->flags &= ~WLAN_STA_AUTHORIZED;
634                 if (params->station_flags & STATION_FLAG_AUTHORIZED)
635                         sta->flags |= WLAN_STA_AUTHORIZED;
636
637                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
638                 if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE)
639                         sta->flags |= WLAN_STA_SHORT_PREAMBLE;
640
641                 sta->flags &= ~WLAN_STA_WME;
642                 if (params->station_flags & STATION_FLAG_WME)
643                         sta->flags |= WLAN_STA_WME;
644                 spin_unlock_bh(&sta->lock);
645         }
646
647         /*
648          * FIXME: updating the following information is racy when this
649          *        function is called from ieee80211_change_station().
650          *        However, all this information should be static so
651          *        maybe we should just reject attemps to change it.
652          */
653
654         if (params->aid) {
655                 sta->aid = params->aid;
656                 if (sta->aid > IEEE80211_MAX_AID)
657                         sta->aid = 0; /* XXX: should this be an error? */
658         }
659
660         if (params->listen_interval >= 0)
661                 sta->listen_interval = params->listen_interval;
662
663         if (params->supported_rates) {
664                 rates = 0;
665                 sband = local->hw.wiphy->bands[local->oper_channel->band];
666
667                 for (i = 0; i < params->supported_rates_len; i++) {
668                         int rate = (params->supported_rates[i] & 0x7f) * 5;
669                         for (j = 0; j < sband->n_bitrates; j++) {
670                                 if (sband->bitrates[j].bitrate == rate)
671                                         rates |= BIT(j);
672                         }
673                 }
674                 sta->supp_rates[local->oper_channel->band] = rates;
675         }
676
677         if (params->ht_capa) {
678                 ieee80211_ht_cap_ie_to_ht_info(params->ht_capa,
679                                                &sta->ht_info);
680         }
681
682         if (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
683                 switch (params->plink_action) {
684                 case PLINK_ACTION_OPEN:
685                         mesh_plink_open(sta);
686                         break;
687                 case PLINK_ACTION_BLOCK:
688                         mesh_plink_block(sta);
689                         break;
690                 }
691         }
692 }
693
694 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
695                                  u8 *mac, struct station_parameters *params)
696 {
697         struct ieee80211_local *local = wiphy_priv(wiphy);
698         struct sta_info *sta;
699         struct ieee80211_sub_if_data *sdata;
700         int err;
701
702         if (dev == local->mdev || params->vlan == local->mdev)
703                 return -EOPNOTSUPP;
704
705         /* Prevent a race with changing the rate control algorithm */
706         if (!netif_running(dev))
707                 return -ENETDOWN;
708
709         if (params->vlan) {
710                 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
711
712                 if (sdata->vif.type != IEEE80211_IF_TYPE_VLAN &&
713                     sdata->vif.type != IEEE80211_IF_TYPE_AP)
714                         return -EINVAL;
715         } else
716                 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
717
718         if (compare_ether_addr(mac, dev->dev_addr) == 0)
719                 return -EINVAL;
720
721         if (is_multicast_ether_addr(mac))
722                 return -EINVAL;
723
724         sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
725         if (!sta)
726                 return -ENOMEM;
727
728         sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
729
730         sta_apply_parameters(local, sta, params);
731
732         rate_control_rate_init(sta, local);
733
734         rcu_read_lock();
735
736         err = sta_info_insert(sta);
737         if (err) {
738                 /* STA has been freed */
739                 rcu_read_unlock();
740                 return err;
741         }
742
743         if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN ||
744             sdata->vif.type == IEEE80211_IF_TYPE_AP)
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         struct sta_info *sta;
758
759         if (dev == local->mdev)
760                 return -EOPNOTSUPP;
761
762         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
763
764         if (mac) {
765                 rcu_read_lock();
766
767                 /* XXX: get sta belonging to dev */
768                 sta = sta_info_get(local, mac);
769                 if (!sta) {
770                         rcu_read_unlock();
771                         return -ENOENT;
772                 }
773
774                 sta_info_unlink(&sta);
775                 rcu_read_unlock();
776
777                 sta_info_destroy(sta);
778         } else
779                 sta_info_flush(local, sdata);
780
781         return 0;
782 }
783
784 static int ieee80211_change_station(struct wiphy *wiphy,
785                                     struct net_device *dev,
786                                     u8 *mac,
787                                     struct station_parameters *params)
788 {
789         struct ieee80211_local *local = wiphy_priv(wiphy);
790         struct sta_info *sta;
791         struct ieee80211_sub_if_data *vlansdata;
792
793         if (dev == local->mdev || params->vlan == local->mdev)
794                 return -EOPNOTSUPP;
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 != IEEE80211_IF_TYPE_VLAN &&
809                     vlansdata->vif.type != IEEE80211_IF_TYPE_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         if (dev == local->mdev)
836                 return -EOPNOTSUPP;
837
838         if (!netif_running(dev))
839                 return -ENETDOWN;
840
841         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
842
843         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
844                 return -ENOTSUPP;
845
846         rcu_read_lock();
847         sta = sta_info_get(local, next_hop);
848         if (!sta) {
849                 rcu_read_unlock();
850                 return -ENOENT;
851         }
852
853         err = mesh_path_add(dst, sdata);
854         if (err) {
855                 rcu_read_unlock();
856                 return err;
857         }
858
859         mpath = mesh_path_lookup(dst, sdata);
860         if (!mpath) {
861                 rcu_read_unlock();
862                 return -ENXIO;
863         }
864         mesh_path_fix_nexthop(mpath, sta);
865
866         rcu_read_unlock();
867         return 0;
868 }
869
870 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
871                                  u8 *dst)
872 {
873         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
874
875         if (dst)
876                 return mesh_path_del(dst, sdata);
877
878         mesh_path_flush(sdata);
879         return 0;
880 }
881
882 static int ieee80211_change_mpath(struct wiphy *wiphy,
883                                     struct net_device *dev,
884                                     u8 *dst, u8 *next_hop)
885 {
886         struct ieee80211_local *local = wiphy_priv(wiphy);
887         struct ieee80211_sub_if_data *sdata;
888         struct mesh_path *mpath;
889         struct sta_info *sta;
890
891         if (dev == local->mdev)
892                 return -EOPNOTSUPP;
893
894         if (!netif_running(dev))
895                 return -ENETDOWN;
896
897         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
898
899         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
900                 return -ENOTSUPP;
901
902         rcu_read_lock();
903
904         sta = sta_info_get(local, next_hop);
905         if (!sta) {
906                 rcu_read_unlock();
907                 return -ENOENT;
908         }
909
910         mpath = mesh_path_lookup(dst, sdata);
911         if (!mpath) {
912                 rcu_read_unlock();
913                 return -ENOENT;
914         }
915
916         mesh_path_fix_nexthop(mpath, sta);
917
918         rcu_read_unlock();
919         return 0;
920 }
921
922 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
923                             struct mpath_info *pinfo)
924 {
925         if (mpath->next_hop)
926                 memcpy(next_hop, mpath->next_hop->addr, ETH_ALEN);
927         else
928                 memset(next_hop, 0, ETH_ALEN);
929
930         pinfo->filled = MPATH_INFO_FRAME_QLEN |
931                         MPATH_INFO_DSN |
932                         MPATH_INFO_METRIC |
933                         MPATH_INFO_EXPTIME |
934                         MPATH_INFO_DISCOVERY_TIMEOUT |
935                         MPATH_INFO_DISCOVERY_RETRIES |
936                         MPATH_INFO_FLAGS;
937
938         pinfo->frame_qlen = mpath->frame_queue.qlen;
939         pinfo->dsn = mpath->dsn;
940         pinfo->metric = mpath->metric;
941         if (time_before(jiffies, mpath->exp_time))
942                 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
943         pinfo->discovery_timeout =
944                         jiffies_to_msecs(mpath->discovery_timeout);
945         pinfo->discovery_retries = mpath->discovery_retries;
946         pinfo->flags = 0;
947         if (mpath->flags & MESH_PATH_ACTIVE)
948                 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
949         if (mpath->flags & MESH_PATH_RESOLVING)
950                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
951         if (mpath->flags & MESH_PATH_DSN_VALID)
952                 pinfo->flags |= NL80211_MPATH_FLAG_DSN_VALID;
953         if (mpath->flags & MESH_PATH_FIXED)
954                 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
955         if (mpath->flags & MESH_PATH_RESOLVING)
956                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
957
958         pinfo->flags = mpath->flags;
959 }
960
961 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
962                                u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
963
964 {
965         struct ieee80211_local *local = wiphy_priv(wiphy);
966         struct ieee80211_sub_if_data *sdata;
967         struct mesh_path *mpath;
968
969         if (dev == local->mdev)
970                 return -EOPNOTSUPP;
971
972         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
973
974         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
975                 return -ENOTSUPP;
976
977         rcu_read_lock();
978         mpath = mesh_path_lookup(dst, sdata);
979         if (!mpath) {
980                 rcu_read_unlock();
981                 return -ENOENT;
982         }
983         memcpy(dst, mpath->dst, ETH_ALEN);
984         mpath_set_pinfo(mpath, next_hop, pinfo);
985         rcu_read_unlock();
986         return 0;
987 }
988
989 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
990                                  int idx, u8 *dst, u8 *next_hop,
991                                  struct mpath_info *pinfo)
992 {
993         struct ieee80211_local *local = wiphy_priv(wiphy);
994         struct ieee80211_sub_if_data *sdata;
995         struct mesh_path *mpath;
996
997         if (dev == local->mdev)
998                 return -EOPNOTSUPP;
999
1000         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1001
1002         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
1003                 return -ENOTSUPP;
1004
1005         rcu_read_lock();
1006         mpath = mesh_path_lookup_by_idx(idx, sdata);
1007         if (!mpath) {
1008                 rcu_read_unlock();
1009                 return -ENOENT;
1010         }
1011         memcpy(dst, mpath->dst, ETH_ALEN);
1012         mpath_set_pinfo(mpath, next_hop, pinfo);
1013         rcu_read_unlock();
1014         return 0;
1015 }
1016 #endif
1017
1018 static int ieee80211_change_bss(struct wiphy *wiphy,
1019                                 struct net_device *dev,
1020                                 struct bss_parameters *params)
1021 {
1022         struct ieee80211_local *local = wiphy_priv(wiphy);
1023         struct ieee80211_sub_if_data *sdata;
1024         u32 changed = 0;
1025
1026         if (dev == local->mdev)
1027                 return -EOPNOTSUPP;
1028
1029         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1030
1031         if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
1032                 return -EINVAL;
1033
1034         if (params->use_cts_prot >= 0) {
1035                 sdata->bss_conf.use_cts_prot = params->use_cts_prot;
1036                 changed |= BSS_CHANGED_ERP_CTS_PROT;
1037         }
1038         if (params->use_short_preamble >= 0) {
1039                 sdata->bss_conf.use_short_preamble =
1040                         params->use_short_preamble;
1041                 changed |= BSS_CHANGED_ERP_PREAMBLE;
1042         }
1043         if (params->use_short_slot_time >= 0) {
1044                 sdata->bss_conf.use_short_slot =
1045                         params->use_short_slot_time;
1046                 changed |= BSS_CHANGED_ERP_SLOT;
1047         }
1048
1049         ieee80211_bss_info_change_notify(sdata, changed);
1050
1051         return 0;
1052 }
1053
1054 struct cfg80211_ops mac80211_config_ops = {
1055         .add_virtual_intf = ieee80211_add_iface,
1056         .del_virtual_intf = ieee80211_del_iface,
1057         .change_virtual_intf = ieee80211_change_iface,
1058         .add_key = ieee80211_add_key,
1059         .del_key = ieee80211_del_key,
1060         .get_key = ieee80211_get_key,
1061         .set_default_key = ieee80211_config_default_key,
1062         .add_beacon = ieee80211_add_beacon,
1063         .set_beacon = ieee80211_set_beacon,
1064         .del_beacon = ieee80211_del_beacon,
1065         .add_station = ieee80211_add_station,
1066         .del_station = ieee80211_del_station,
1067         .change_station = ieee80211_change_station,
1068         .get_station = ieee80211_get_station,
1069         .dump_station = ieee80211_dump_station,
1070 #ifdef CONFIG_MAC80211_MESH
1071         .add_mpath = ieee80211_add_mpath,
1072         .del_mpath = ieee80211_del_mpath,
1073         .change_mpath = ieee80211_change_mpath,
1074         .get_mpath = ieee80211_get_mpath,
1075         .dump_mpath = ieee80211_dump_mpath,
1076 #endif
1077         .change_bss = ieee80211_change_bss,
1078 };