nl80211/cfg80211: support for mesh, sta dumping
[linux-2.6.git] / net / mac80211 / cfg.c
blob006da6a2e71b04ebf851994b6010aedbc75886fd
1 /*
2 * mac80211 configuration hooks for cfg80211
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
6 * This file is GPLv2 as found in COPYING.
7 */
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 "ieee80211_rate.h"
19 static enum ieee80211_if_types
20 nl80211_type_to_mac80211_type(enum nl80211_iftype type)
22 switch (type) {
23 case NL80211_IFTYPE_UNSPECIFIED:
24 return IEEE80211_IF_TYPE_STA;
25 case NL80211_IFTYPE_ADHOC:
26 return IEEE80211_IF_TYPE_IBSS;
27 case NL80211_IFTYPE_STATION:
28 return IEEE80211_IF_TYPE_STA;
29 case NL80211_IFTYPE_MONITOR:
30 return IEEE80211_IF_TYPE_MNTR;
31 default:
32 return IEEE80211_IF_TYPE_INVALID;
36 static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
37 enum nl80211_iftype type, u32 *flags,
38 struct vif_params *params)
40 struct ieee80211_local *local = wiphy_priv(wiphy);
41 enum ieee80211_if_types itype;
42 struct net_device *dev;
43 struct ieee80211_sub_if_data *sdata;
44 int err;
46 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
47 return -ENODEV;
49 itype = nl80211_type_to_mac80211_type(type);
50 if (itype == IEEE80211_IF_TYPE_INVALID)
51 return -EINVAL;
53 err = ieee80211_if_add(local->mdev, name, &dev, itype);
54 if (err || itype != IEEE80211_IF_TYPE_MNTR || !flags)
55 return err;
57 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
58 sdata->u.mntr_flags = *flags;
59 return 0;
62 static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
64 struct ieee80211_local *local = wiphy_priv(wiphy);
65 struct net_device *dev;
66 char *name;
68 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
69 return -ENODEV;
71 /* we're under RTNL */
72 dev = __dev_get_by_index(&init_net, ifindex);
73 if (!dev)
74 return 0;
76 name = dev->name;
78 return ieee80211_if_remove(local->mdev, name, -1);
81 static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
82 enum nl80211_iftype type, u32 *flags,
83 struct vif_params *params)
85 struct ieee80211_local *local = wiphy_priv(wiphy);
86 struct net_device *dev;
87 enum ieee80211_if_types itype;
88 struct ieee80211_sub_if_data *sdata;
90 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
91 return -ENODEV;
93 /* we're under RTNL */
94 dev = __dev_get_by_index(&init_net, ifindex);
95 if (!dev)
96 return -ENODEV;
98 if (netif_running(dev))
99 return -EBUSY;
101 itype = nl80211_type_to_mac80211_type(type);
102 if (itype == IEEE80211_IF_TYPE_INVALID)
103 return -EINVAL;
105 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
107 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
108 return -EOPNOTSUPP;
110 ieee80211_if_reinit(dev);
111 ieee80211_if_set_type(dev, itype);
113 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR || !flags)
114 return 0;
116 sdata->u.mntr_flags = *flags;
117 return 0;
120 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
121 u8 key_idx, u8 *mac_addr,
122 struct key_params *params)
124 struct ieee80211_sub_if_data *sdata;
125 struct sta_info *sta = NULL;
126 enum ieee80211_key_alg alg;
127 int ret;
128 struct ieee80211_key *key;
130 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
132 switch (params->cipher) {
133 case WLAN_CIPHER_SUITE_WEP40:
134 case WLAN_CIPHER_SUITE_WEP104:
135 alg = ALG_WEP;
136 break;
137 case WLAN_CIPHER_SUITE_TKIP:
138 alg = ALG_TKIP;
139 break;
140 case WLAN_CIPHER_SUITE_CCMP:
141 alg = ALG_CCMP;
142 break;
143 default:
144 return -EINVAL;
147 key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key);
148 if (!key)
149 return -ENOMEM;
151 if (mac_addr) {
152 sta = sta_info_get(sdata->local, mac_addr);
153 if (!sta) {
154 ieee80211_key_free(key);
155 return -ENOENT;
159 ieee80211_key_link(key, sdata, sta);
161 ret = 0;
163 if (sta)
164 sta_info_put(sta);
166 return ret;
169 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
170 u8 key_idx, u8 *mac_addr)
172 struct ieee80211_sub_if_data *sdata;
173 struct sta_info *sta;
174 int ret;
175 struct ieee80211_key *key;
177 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
179 if (mac_addr) {
180 sta = sta_info_get(sdata->local, mac_addr);
181 if (!sta)
182 return -ENOENT;
184 ret = 0;
185 if (sta->key) {
186 key = sta->key;
187 ieee80211_key_free(key);
188 WARN_ON(sta->key);
189 } else
190 ret = -ENOENT;
192 sta_info_put(sta);
193 return ret;
196 if (!sdata->keys[key_idx])
197 return -ENOENT;
199 key = sdata->keys[key_idx];
200 ieee80211_key_free(key);
201 WARN_ON(sdata->keys[key_idx]);
203 return 0;
206 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
207 u8 key_idx, u8 *mac_addr, void *cookie,
208 void (*callback)(void *cookie,
209 struct key_params *params))
211 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
212 struct sta_info *sta = NULL;
213 u8 seq[6] = {0};
214 struct key_params params;
215 struct ieee80211_key *key;
216 u32 iv32;
217 u16 iv16;
218 int err = -ENOENT;
220 if (mac_addr) {
221 sta = sta_info_get(sdata->local, mac_addr);
222 if (!sta)
223 goto out;
225 key = sta->key;
226 } else
227 key = sdata->keys[key_idx];
229 if (!key)
230 goto out;
232 memset(&params, 0, sizeof(params));
234 switch (key->conf.alg) {
235 case ALG_TKIP:
236 params.cipher = WLAN_CIPHER_SUITE_TKIP;
238 iv32 = key->u.tkip.iv32;
239 iv16 = key->u.tkip.iv16;
241 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
242 sdata->local->ops->get_tkip_seq)
243 sdata->local->ops->get_tkip_seq(
244 local_to_hw(sdata->local),
245 key->conf.hw_key_idx,
246 &iv32, &iv16);
248 seq[0] = iv16 & 0xff;
249 seq[1] = (iv16 >> 8) & 0xff;
250 seq[2] = iv32 & 0xff;
251 seq[3] = (iv32 >> 8) & 0xff;
252 seq[4] = (iv32 >> 16) & 0xff;
253 seq[5] = (iv32 >> 24) & 0xff;
254 params.seq = seq;
255 params.seq_len = 6;
256 break;
257 case ALG_CCMP:
258 params.cipher = WLAN_CIPHER_SUITE_CCMP;
259 seq[0] = key->u.ccmp.tx_pn[5];
260 seq[1] = key->u.ccmp.tx_pn[4];
261 seq[2] = key->u.ccmp.tx_pn[3];
262 seq[3] = key->u.ccmp.tx_pn[2];
263 seq[4] = key->u.ccmp.tx_pn[1];
264 seq[5] = key->u.ccmp.tx_pn[0];
265 params.seq = seq;
266 params.seq_len = 6;
267 break;
268 case ALG_WEP:
269 if (key->conf.keylen == 5)
270 params.cipher = WLAN_CIPHER_SUITE_WEP40;
271 else
272 params.cipher = WLAN_CIPHER_SUITE_WEP104;
273 break;
276 params.key = key->conf.key;
277 params.key_len = key->conf.keylen;
279 callback(cookie, &params);
280 err = 0;
282 out:
283 if (sta)
284 sta_info_put(sta);
285 return err;
288 static int ieee80211_config_default_key(struct wiphy *wiphy,
289 struct net_device *dev,
290 u8 key_idx)
292 struct ieee80211_sub_if_data *sdata;
294 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
295 ieee80211_set_default_key(sdata, key_idx);
297 return 0;
300 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
301 u8 *mac, struct station_info *sinfo)
303 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
304 struct sta_info *sta;
306 sta = sta_info_get(local, mac);
307 if (!sta)
308 return -ENOENT;
310 /* XXX: verify sta->dev == dev */
312 sinfo->filled = STATION_INFO_INACTIVE_TIME |
313 STATION_INFO_RX_BYTES |
314 STATION_INFO_TX_BYTES;
316 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
317 sinfo->rx_bytes = sta->rx_bytes;
318 sinfo->tx_bytes = sta->tx_bytes;
320 sta_info_put(sta);
322 return 0;
326 * This handles both adding a beacon and setting new beacon info
328 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
329 struct beacon_parameters *params)
331 struct beacon_data *new, *old;
332 int new_head_len, new_tail_len;
333 int size;
334 int err = -EINVAL;
336 old = sdata->u.ap.beacon;
338 /* head must not be zero-length */
339 if (params->head && !params->head_len)
340 return -EINVAL;
343 * This is a kludge. beacon interval should really be part
344 * of the beacon information.
346 if (params->interval) {
347 sdata->local->hw.conf.beacon_int = params->interval;
348 if (ieee80211_hw_config(sdata->local))
349 return -EINVAL;
351 * We updated some parameter so if below bails out
352 * it's not an error.
354 err = 0;
357 /* Need to have a beacon head if we don't have one yet */
358 if (!params->head && !old)
359 return err;
361 /* sorry, no way to start beaconing without dtim period */
362 if (!params->dtim_period && !old)
363 return err;
365 /* new or old head? */
366 if (params->head)
367 new_head_len = params->head_len;
368 else
369 new_head_len = old->head_len;
371 /* new or old tail? */
372 if (params->tail || !old)
373 /* params->tail_len will be zero for !params->tail */
374 new_tail_len = params->tail_len;
375 else
376 new_tail_len = old->tail_len;
378 size = sizeof(*new) + new_head_len + new_tail_len;
380 new = kzalloc(size, GFP_KERNEL);
381 if (!new)
382 return -ENOMEM;
384 /* start filling the new info now */
386 /* new or old dtim period? */
387 if (params->dtim_period)
388 new->dtim_period = params->dtim_period;
389 else
390 new->dtim_period = old->dtim_period;
393 * pointers go into the block we allocated,
394 * memory is | beacon_data | head | tail |
396 new->head = ((u8 *) new) + sizeof(*new);
397 new->tail = new->head + new_head_len;
398 new->head_len = new_head_len;
399 new->tail_len = new_tail_len;
401 /* copy in head */
402 if (params->head)
403 memcpy(new->head, params->head, new_head_len);
404 else
405 memcpy(new->head, old->head, new_head_len);
407 /* copy in optional tail */
408 if (params->tail)
409 memcpy(new->tail, params->tail, new_tail_len);
410 else
411 if (old)
412 memcpy(new->tail, old->tail, new_tail_len);
414 rcu_assign_pointer(sdata->u.ap.beacon, new);
416 synchronize_rcu();
418 kfree(old);
420 return ieee80211_if_config_beacon(sdata->dev);
423 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
424 struct beacon_parameters *params)
426 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
427 struct beacon_data *old;
429 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
430 return -EINVAL;
432 old = sdata->u.ap.beacon;
434 if (old)
435 return -EALREADY;
437 return ieee80211_config_beacon(sdata, params);
440 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
441 struct beacon_parameters *params)
443 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
444 struct beacon_data *old;
446 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
447 return -EINVAL;
449 old = sdata->u.ap.beacon;
451 if (!old)
452 return -ENOENT;
454 return ieee80211_config_beacon(sdata, params);
457 static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
459 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
460 struct beacon_data *old;
462 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
463 return -EINVAL;
465 old = sdata->u.ap.beacon;
467 if (!old)
468 return -ENOENT;
470 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
471 synchronize_rcu();
472 kfree(old);
474 return ieee80211_if_config_beacon(dev);
477 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
478 struct iapp_layer2_update {
479 u8 da[ETH_ALEN]; /* broadcast */
480 u8 sa[ETH_ALEN]; /* STA addr */
481 __be16 len; /* 6 */
482 u8 dsap; /* 0 */
483 u8 ssap; /* 0 */
484 u8 control;
485 u8 xid_info[3];
486 } __attribute__ ((packed));
488 static void ieee80211_send_layer2_update(struct sta_info *sta)
490 struct iapp_layer2_update *msg;
491 struct sk_buff *skb;
493 /* Send Level 2 Update Frame to update forwarding tables in layer 2
494 * bridge devices */
496 skb = dev_alloc_skb(sizeof(*msg));
497 if (!skb)
498 return;
499 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
501 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
502 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
504 memset(msg->da, 0xff, ETH_ALEN);
505 memcpy(msg->sa, sta->addr, ETH_ALEN);
506 msg->len = htons(6);
507 msg->dsap = 0;
508 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
509 msg->control = 0xaf; /* XID response lsb.1111F101.
510 * F=0 (no poll command; unsolicited frame) */
511 msg->xid_info[0] = 0x81; /* XID format identifier */
512 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
513 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
515 skb->dev = sta->dev;
516 skb->protocol = eth_type_trans(skb, sta->dev);
517 memset(skb->cb, 0, sizeof(skb->cb));
518 netif_rx(skb);
521 static void sta_apply_parameters(struct ieee80211_local *local,
522 struct sta_info *sta,
523 struct station_parameters *params)
525 u32 rates;
526 int i, j;
527 struct ieee80211_supported_band *sband;
529 if (params->station_flags & STATION_FLAG_CHANGED) {
530 sta->flags &= ~WLAN_STA_AUTHORIZED;
531 if (params->station_flags & STATION_FLAG_AUTHORIZED)
532 sta->flags |= WLAN_STA_AUTHORIZED;
534 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
535 if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE)
536 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
538 sta->flags &= ~WLAN_STA_WME;
539 if (params->station_flags & STATION_FLAG_WME)
540 sta->flags |= WLAN_STA_WME;
543 if (params->aid) {
544 sta->aid = params->aid;
545 if (sta->aid > IEEE80211_MAX_AID)
546 sta->aid = 0; /* XXX: should this be an error? */
549 if (params->listen_interval >= 0)
550 sta->listen_interval = params->listen_interval;
552 if (params->supported_rates) {
553 rates = 0;
554 sband = local->hw.wiphy->bands[local->oper_channel->band];
556 for (i = 0; i < params->supported_rates_len; i++) {
557 int rate = (params->supported_rates[i] & 0x7f) * 5;
558 for (j = 0; j < sband->n_bitrates; j++) {
559 if (sband->bitrates[j].bitrate == rate)
560 rates |= BIT(j);
563 sta->supp_rates[local->oper_channel->band] = rates;
567 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
568 u8 *mac, struct station_parameters *params)
570 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
571 struct sta_info *sta;
572 struct ieee80211_sub_if_data *sdata;
574 /* Prevent a race with changing the rate control algorithm */
575 if (!netif_running(dev))
576 return -ENETDOWN;
578 if (params->vlan) {
579 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
581 if (sdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
582 sdata->vif.type != IEEE80211_IF_TYPE_AP)
583 return -EINVAL;
584 } else
585 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
587 sta = sta_info_add(local, dev, mac, GFP_KERNEL);
588 if (IS_ERR(sta))
589 return PTR_ERR(sta);
591 sta->dev = sdata->dev;
592 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN ||
593 sdata->vif.type == IEEE80211_IF_TYPE_AP)
594 ieee80211_send_layer2_update(sta);
596 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
598 sta_apply_parameters(local, sta, params);
600 rate_control_rate_init(sta, local);
602 sta_info_put(sta);
604 return 0;
607 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
608 u8 *mac)
610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
611 struct sta_info *sta;
613 if (mac) {
614 /* XXX: get sta belonging to dev */
615 sta = sta_info_get(local, mac);
616 if (!sta)
617 return -ENOENT;
619 sta_info_free(sta);
620 sta_info_put(sta);
621 } else
622 sta_info_flush(local, dev);
624 return 0;
627 static int ieee80211_change_station(struct wiphy *wiphy,
628 struct net_device *dev,
629 u8 *mac,
630 struct station_parameters *params)
632 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
633 struct sta_info *sta;
634 struct ieee80211_sub_if_data *vlansdata;
636 /* XXX: get sta belonging to dev */
637 sta = sta_info_get(local, mac);
638 if (!sta)
639 return -ENOENT;
641 if (params->vlan && params->vlan != sta->dev) {
642 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
644 if (vlansdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
645 vlansdata->vif.type != IEEE80211_IF_TYPE_AP)
646 return -EINVAL;
648 sta->dev = params->vlan;
649 ieee80211_send_layer2_update(sta);
652 sta_apply_parameters(local, sta, params);
654 sta_info_put(sta);
656 return 0;
659 struct cfg80211_ops mac80211_config_ops = {
660 .add_virtual_intf = ieee80211_add_iface,
661 .del_virtual_intf = ieee80211_del_iface,
662 .change_virtual_intf = ieee80211_change_iface,
663 .add_key = ieee80211_add_key,
664 .del_key = ieee80211_del_key,
665 .get_key = ieee80211_get_key,
666 .set_default_key = ieee80211_config_default_key,
667 .add_beacon = ieee80211_add_beacon,
668 .set_beacon = ieee80211_set_beacon,
669 .del_beacon = ieee80211_del_beacon,
670 .add_station = ieee80211_add_station,
671 .del_station = ieee80211_del_station,
672 .change_station = ieee80211_change_station,
673 .get_station = ieee80211_get_station,