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