6d2ad2bf3ab5a97f93a54a7ea2428baed6a68460
[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 (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
678                 switch (params->plink_action) {
679                 case PLINK_ACTION_OPEN:
680                         mesh_plink_open(sta);
681                         break;
682                 case PLINK_ACTION_BLOCK:
683                         mesh_plink_block(sta);
684                         break;
685                 }
686         }
687 }
688
689 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
690                                  u8 *mac, struct station_parameters *params)
691 {
692         struct ieee80211_local *local = wiphy_priv(wiphy);
693         struct sta_info *sta;
694         struct ieee80211_sub_if_data *sdata;
695         int err;
696
697         if (dev == local->mdev || params->vlan == local->mdev)
698                 return -EOPNOTSUPP;
699
700         /* Prevent a race with changing the rate control algorithm */
701         if (!netif_running(dev))
702                 return -ENETDOWN;
703
704         if (params->vlan) {
705                 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
706
707                 if (sdata->vif.type != IEEE80211_IF_TYPE_VLAN &&
708                     sdata->vif.type != IEEE80211_IF_TYPE_AP)
709                         return -EINVAL;
710         } else
711                 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
712
713         if (compare_ether_addr(mac, dev->dev_addr) == 0)
714                 return -EINVAL;
715
716         if (is_multicast_ether_addr(mac))
717                 return -EINVAL;
718
719         sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
720         if (!sta)
721                 return -ENOMEM;
722
723         sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
724
725         sta_apply_parameters(local, sta, params);
726
727         rate_control_rate_init(sta, local);
728
729         rcu_read_lock();
730
731         err = sta_info_insert(sta);
732         if (err) {
733                 /* STA has been freed */
734                 rcu_read_unlock();
735                 return err;
736         }
737
738         if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN ||
739             sdata->vif.type == IEEE80211_IF_TYPE_AP)
740                 ieee80211_send_layer2_update(sta);
741
742         rcu_read_unlock();
743
744         return 0;
745 }
746
747 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
748                                  u8 *mac)
749 {
750         struct ieee80211_local *local = wiphy_priv(wiphy);
751         struct ieee80211_sub_if_data *sdata;
752         struct sta_info *sta;
753
754         if (dev == local->mdev)
755                 return -EOPNOTSUPP;
756
757         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
758
759         if (mac) {
760                 rcu_read_lock();
761
762                 /* XXX: get sta belonging to dev */
763                 sta = sta_info_get(local, mac);
764                 if (!sta) {
765                         rcu_read_unlock();
766                         return -ENOENT;
767                 }
768
769                 sta_info_unlink(&sta);
770                 rcu_read_unlock();
771
772                 sta_info_destroy(sta);
773         } else
774                 sta_info_flush(local, sdata);
775
776         return 0;
777 }
778
779 static int ieee80211_change_station(struct wiphy *wiphy,
780                                     struct net_device *dev,
781                                     u8 *mac,
782                                     struct station_parameters *params)
783 {
784         struct ieee80211_local *local = wiphy_priv(wiphy);
785         struct sta_info *sta;
786         struct ieee80211_sub_if_data *vlansdata;
787
788         if (dev == local->mdev || params->vlan == local->mdev)
789                 return -EOPNOTSUPP;
790
791         rcu_read_lock();
792
793         /* XXX: get sta belonging to dev */
794         sta = sta_info_get(local, mac);
795         if (!sta) {
796                 rcu_read_unlock();
797                 return -ENOENT;
798         }
799
800         if (params->vlan && params->vlan != sta->sdata->dev) {
801                 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
802
803                 if (vlansdata->vif.type != IEEE80211_IF_TYPE_VLAN &&
804                     vlansdata->vif.type != IEEE80211_IF_TYPE_AP) {
805                         rcu_read_unlock();
806                         return -EINVAL;
807                 }
808
809                 sta->sdata = vlansdata;
810                 ieee80211_send_layer2_update(sta);
811         }
812
813         sta_apply_parameters(local, sta, params);
814
815         rcu_read_unlock();
816
817         return 0;
818 }
819
820 #ifdef CONFIG_MAC80211_MESH
821 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
822                                  u8 *dst, u8 *next_hop)
823 {
824         struct ieee80211_local *local = wiphy_priv(wiphy);
825         struct ieee80211_sub_if_data *sdata;
826         struct mesh_path *mpath;
827         struct sta_info *sta;
828         int err;
829
830         if (dev == local->mdev)
831                 return -EOPNOTSUPP;
832
833         if (!netif_running(dev))
834                 return -ENETDOWN;
835
836         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
837
838         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
839                 return -ENOTSUPP;
840
841         rcu_read_lock();
842         sta = sta_info_get(local, next_hop);
843         if (!sta) {
844                 rcu_read_unlock();
845                 return -ENOENT;
846         }
847
848         err = mesh_path_add(dst, sdata);
849         if (err) {
850                 rcu_read_unlock();
851                 return err;
852         }
853
854         mpath = mesh_path_lookup(dst, sdata);
855         if (!mpath) {
856                 rcu_read_unlock();
857                 return -ENXIO;
858         }
859         mesh_path_fix_nexthop(mpath, sta);
860
861         rcu_read_unlock();
862         return 0;
863 }
864
865 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
866                                  u8 *dst)
867 {
868         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
869
870         if (dst)
871                 return mesh_path_del(dst, sdata);
872
873         mesh_path_flush(sdata);
874         return 0;
875 }
876
877 static int ieee80211_change_mpath(struct wiphy *wiphy,
878                                     struct net_device *dev,
879                                     u8 *dst, u8 *next_hop)
880 {
881         struct ieee80211_local *local = wiphy_priv(wiphy);
882         struct ieee80211_sub_if_data *sdata;
883         struct mesh_path *mpath;
884         struct sta_info *sta;
885
886         if (dev == local->mdev)
887                 return -EOPNOTSUPP;
888
889         if (!netif_running(dev))
890                 return -ENETDOWN;
891
892         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
893
894         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
895                 return -ENOTSUPP;
896
897         rcu_read_lock();
898
899         sta = sta_info_get(local, next_hop);
900         if (!sta) {
901                 rcu_read_unlock();
902                 return -ENOENT;
903         }
904
905         mpath = mesh_path_lookup(dst, sdata);
906         if (!mpath) {
907                 rcu_read_unlock();
908                 return -ENOENT;
909         }
910
911         mesh_path_fix_nexthop(mpath, sta);
912
913         rcu_read_unlock();
914         return 0;
915 }
916
917 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
918                             struct mpath_info *pinfo)
919 {
920         if (mpath->next_hop)
921                 memcpy(next_hop, mpath->next_hop->addr, ETH_ALEN);
922         else
923                 memset(next_hop, 0, ETH_ALEN);
924
925         pinfo->filled = MPATH_INFO_FRAME_QLEN |
926                         MPATH_INFO_DSN |
927                         MPATH_INFO_METRIC |
928                         MPATH_INFO_EXPTIME |
929                         MPATH_INFO_DISCOVERY_TIMEOUT |
930                         MPATH_INFO_DISCOVERY_RETRIES |
931                         MPATH_INFO_FLAGS;
932
933         pinfo->frame_qlen = mpath->frame_queue.qlen;
934         pinfo->dsn = mpath->dsn;
935         pinfo->metric = mpath->metric;
936         if (time_before(jiffies, mpath->exp_time))
937                 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
938         pinfo->discovery_timeout =
939                         jiffies_to_msecs(mpath->discovery_timeout);
940         pinfo->discovery_retries = mpath->discovery_retries;
941         pinfo->flags = 0;
942         if (mpath->flags & MESH_PATH_ACTIVE)
943                 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
944         if (mpath->flags & MESH_PATH_RESOLVING)
945                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
946         if (mpath->flags & MESH_PATH_DSN_VALID)
947                 pinfo->flags |= NL80211_MPATH_FLAG_DSN_VALID;
948         if (mpath->flags & MESH_PATH_FIXED)
949                 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
950         if (mpath->flags & MESH_PATH_RESOLVING)
951                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
952
953         pinfo->flags = mpath->flags;
954 }
955
956 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
957                                u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
958
959 {
960         struct ieee80211_local *local = wiphy_priv(wiphy);
961         struct ieee80211_sub_if_data *sdata;
962         struct mesh_path *mpath;
963
964         if (dev == local->mdev)
965                 return -EOPNOTSUPP;
966
967         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
968
969         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
970                 return -ENOTSUPP;
971
972         rcu_read_lock();
973         mpath = mesh_path_lookup(dst, sdata);
974         if (!mpath) {
975                 rcu_read_unlock();
976                 return -ENOENT;
977         }
978         memcpy(dst, mpath->dst, ETH_ALEN);
979         mpath_set_pinfo(mpath, next_hop, pinfo);
980         rcu_read_unlock();
981         return 0;
982 }
983
984 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
985                                  int idx, u8 *dst, u8 *next_hop,
986                                  struct mpath_info *pinfo)
987 {
988         struct ieee80211_local *local = wiphy_priv(wiphy);
989         struct ieee80211_sub_if_data *sdata;
990         struct mesh_path *mpath;
991
992         if (dev == local->mdev)
993                 return -EOPNOTSUPP;
994
995         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
996
997         if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
998                 return -ENOTSUPP;
999
1000         rcu_read_lock();
1001         mpath = mesh_path_lookup_by_idx(idx, sdata);
1002         if (!mpath) {
1003                 rcu_read_unlock();
1004                 return -ENOENT;
1005         }
1006         memcpy(dst, mpath->dst, ETH_ALEN);
1007         mpath_set_pinfo(mpath, next_hop, pinfo);
1008         rcu_read_unlock();
1009         return 0;
1010 }
1011 #endif
1012
1013 struct cfg80211_ops mac80211_config_ops = {
1014         .add_virtual_intf = ieee80211_add_iface,
1015         .del_virtual_intf = ieee80211_del_iface,
1016         .change_virtual_intf = ieee80211_change_iface,
1017         .add_key = ieee80211_add_key,
1018         .del_key = ieee80211_del_key,
1019         .get_key = ieee80211_get_key,
1020         .set_default_key = ieee80211_config_default_key,
1021         .add_beacon = ieee80211_add_beacon,
1022         .set_beacon = ieee80211_set_beacon,
1023         .del_beacon = ieee80211_del_beacon,
1024         .add_station = ieee80211_add_station,
1025         .del_station = ieee80211_del_station,
1026         .change_station = ieee80211_change_station,
1027         .get_station = ieee80211_get_station,
1028         .dump_station = ieee80211_dump_station,
1029 #ifdef CONFIG_MAC80211_MESH
1030         .add_mpath = ieee80211_add_mpath,
1031         .del_mpath = ieee80211_del_mpath,
1032         .change_mpath = ieee80211_change_mpath,
1033         .get_mpath = ieee80211_get_mpath,
1034         .dump_mpath = ieee80211_dump_mpath,
1035 #endif
1036 };