Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / mac80211 / cfg.c
blob22c9619ba7760e0d9e49c8fa8262a08f9deb6c64
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)
39 struct ieee80211_local *local = wiphy_priv(wiphy);
40 enum ieee80211_if_types itype;
42 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
43 return -ENODEV;
45 itype = nl80211_type_to_mac80211_type(type);
46 if (itype == IEEE80211_IF_TYPE_INVALID)
47 return -EINVAL;
49 return ieee80211_if_add(local->mdev, name, NULL, itype);
52 static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
54 struct ieee80211_local *local = wiphy_priv(wiphy);
55 struct net_device *dev;
56 char *name;
58 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
59 return -ENODEV;
61 /* we're under RTNL */
62 dev = __dev_get_by_index(&init_net, ifindex);
63 if (!dev)
64 return 0;
66 name = dev->name;
68 return ieee80211_if_remove(local->mdev, name, -1);
71 static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
72 enum nl80211_iftype type)
74 struct ieee80211_local *local = wiphy_priv(wiphy);
75 struct net_device *dev;
76 enum ieee80211_if_types itype;
77 struct ieee80211_sub_if_data *sdata;
79 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
80 return -ENODEV;
82 /* we're under RTNL */
83 dev = __dev_get_by_index(&init_net, ifindex);
84 if (!dev)
85 return -ENODEV;
87 if (netif_running(dev))
88 return -EBUSY;
90 itype = nl80211_type_to_mac80211_type(type);
91 if (itype == IEEE80211_IF_TYPE_INVALID)
92 return -EINVAL;
94 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
96 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
97 return -EOPNOTSUPP;
99 ieee80211_if_reinit(dev);
100 ieee80211_if_set_type(dev, itype);
102 return 0;
105 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
106 u8 key_idx, u8 *mac_addr,
107 struct key_params *params)
109 struct ieee80211_sub_if_data *sdata;
110 struct sta_info *sta = NULL;
111 enum ieee80211_key_alg alg;
112 int ret;
114 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
116 switch (params->cipher) {
117 case WLAN_CIPHER_SUITE_WEP40:
118 case WLAN_CIPHER_SUITE_WEP104:
119 alg = ALG_WEP;
120 break;
121 case WLAN_CIPHER_SUITE_TKIP:
122 alg = ALG_TKIP;
123 break;
124 case WLAN_CIPHER_SUITE_CCMP:
125 alg = ALG_CCMP;
126 break;
127 default:
128 return -EINVAL;
131 if (mac_addr) {
132 sta = sta_info_get(sdata->local, mac_addr);
133 if (!sta)
134 return -ENOENT;
137 ret = 0;
138 if (!ieee80211_key_alloc(sdata, sta, alg, key_idx,
139 params->key_len, params->key))
140 ret = -ENOMEM;
142 if (sta)
143 sta_info_put(sta);
145 return ret;
148 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
149 u8 key_idx, u8 *mac_addr)
151 struct ieee80211_sub_if_data *sdata;
152 struct sta_info *sta;
153 int ret;
155 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
157 if (mac_addr) {
158 sta = sta_info_get(sdata->local, mac_addr);
159 if (!sta)
160 return -ENOENT;
162 ret = 0;
163 if (sta->key)
164 ieee80211_key_free(sta->key);
165 else
166 ret = -ENOENT;
168 sta_info_put(sta);
169 return ret;
172 if (!sdata->keys[key_idx])
173 return -ENOENT;
175 ieee80211_key_free(sdata->keys[key_idx]);
177 return 0;
180 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
181 u8 key_idx, u8 *mac_addr, void *cookie,
182 void (*callback)(void *cookie,
183 struct key_params *params))
185 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
186 struct sta_info *sta = NULL;
187 u8 seq[6] = {0};
188 struct key_params params;
189 struct ieee80211_key *key;
190 u32 iv32;
191 u16 iv16;
192 int err = -ENOENT;
194 if (mac_addr) {
195 sta = sta_info_get(sdata->local, mac_addr);
196 if (!sta)
197 goto out;
199 key = sta->key;
200 } else
201 key = sdata->keys[key_idx];
203 if (!key)
204 goto out;
206 memset(&params, 0, sizeof(params));
208 switch (key->conf.alg) {
209 case ALG_TKIP:
210 params.cipher = WLAN_CIPHER_SUITE_TKIP;
212 iv32 = key->u.tkip.iv32;
213 iv16 = key->u.tkip.iv16;
215 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
216 sdata->local->ops->get_tkip_seq)
217 sdata->local->ops->get_tkip_seq(
218 local_to_hw(sdata->local),
219 key->conf.hw_key_idx,
220 &iv32, &iv16);
222 seq[0] = iv16 & 0xff;
223 seq[1] = (iv16 >> 8) & 0xff;
224 seq[2] = iv32 & 0xff;
225 seq[3] = (iv32 >> 8) & 0xff;
226 seq[4] = (iv32 >> 16) & 0xff;
227 seq[5] = (iv32 >> 24) & 0xff;
228 params.seq = seq;
229 params.seq_len = 6;
230 break;
231 case ALG_CCMP:
232 params.cipher = WLAN_CIPHER_SUITE_CCMP;
233 seq[0] = key->u.ccmp.tx_pn[5];
234 seq[1] = key->u.ccmp.tx_pn[4];
235 seq[2] = key->u.ccmp.tx_pn[3];
236 seq[3] = key->u.ccmp.tx_pn[2];
237 seq[4] = key->u.ccmp.tx_pn[1];
238 seq[5] = key->u.ccmp.tx_pn[0];
239 params.seq = seq;
240 params.seq_len = 6;
241 break;
242 case ALG_WEP:
243 if (key->conf.keylen == 5)
244 params.cipher = WLAN_CIPHER_SUITE_WEP40;
245 else
246 params.cipher = WLAN_CIPHER_SUITE_WEP104;
247 break;
250 params.key = key->conf.key;
251 params.key_len = key->conf.keylen;
253 callback(cookie, &params);
254 err = 0;
256 out:
257 if (sta)
258 sta_info_put(sta);
259 return err;
262 static int ieee80211_config_default_key(struct wiphy *wiphy,
263 struct net_device *dev,
264 u8 key_idx)
266 struct ieee80211_sub_if_data *sdata;
268 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
269 ieee80211_set_default_key(sdata, key_idx);
271 return 0;
274 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
275 u8 *mac, struct station_stats *stats)
277 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
278 struct sta_info *sta;
280 sta = sta_info_get(local, mac);
281 if (!sta)
282 return -ENOENT;
284 /* XXX: verify sta->dev == dev */
286 stats->filled = STATION_STAT_INACTIVE_TIME |
287 STATION_STAT_RX_BYTES |
288 STATION_STAT_TX_BYTES;
290 stats->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
291 stats->rx_bytes = sta->rx_bytes;
292 stats->tx_bytes = sta->tx_bytes;
294 sta_info_put(sta);
296 return 0;
300 * This handles both adding a beacon and setting new beacon info
302 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
303 struct beacon_parameters *params)
305 struct beacon_data *new, *old;
306 int new_head_len, new_tail_len;
307 int size;
308 int err = -EINVAL;
310 old = sdata->u.ap.beacon;
312 /* head must not be zero-length */
313 if (params->head && !params->head_len)
314 return -EINVAL;
317 * This is a kludge. beacon interval should really be part
318 * of the beacon information.
320 if (params->interval) {
321 sdata->local->hw.conf.beacon_int = params->interval;
322 if (ieee80211_hw_config(sdata->local))
323 return -EINVAL;
325 * We updated some parameter so if below bails out
326 * it's not an error.
328 err = 0;
331 /* Need to have a beacon head if we don't have one yet */
332 if (!params->head && !old)
333 return err;
335 /* sorry, no way to start beaconing without dtim period */
336 if (!params->dtim_period && !old)
337 return err;
339 /* new or old head? */
340 if (params->head)
341 new_head_len = params->head_len;
342 else
343 new_head_len = old->head_len;
345 /* new or old tail? */
346 if (params->tail || !old)
347 /* params->tail_len will be zero for !params->tail */
348 new_tail_len = params->tail_len;
349 else
350 new_tail_len = old->tail_len;
352 size = sizeof(*new) + new_head_len + new_tail_len;
354 new = kzalloc(size, GFP_KERNEL);
355 if (!new)
356 return -ENOMEM;
358 /* start filling the new info now */
360 /* new or old dtim period? */
361 if (params->dtim_period)
362 new->dtim_period = params->dtim_period;
363 else
364 new->dtim_period = old->dtim_period;
367 * pointers go into the block we allocated,
368 * memory is | beacon_data | head | tail |
370 new->head = ((u8 *) new) + sizeof(*new);
371 new->tail = new->head + new_head_len;
372 new->head_len = new_head_len;
373 new->tail_len = new_tail_len;
375 /* copy in head */
376 if (params->head)
377 memcpy(new->head, params->head, new_head_len);
378 else
379 memcpy(new->head, old->head, new_head_len);
381 /* copy in optional tail */
382 if (params->tail)
383 memcpy(new->tail, params->tail, new_tail_len);
384 else
385 if (old)
386 memcpy(new->tail, old->tail, new_tail_len);
388 rcu_assign_pointer(sdata->u.ap.beacon, new);
390 synchronize_rcu();
392 kfree(old);
394 return ieee80211_if_config_beacon(sdata->dev);
397 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
398 struct beacon_parameters *params)
400 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
401 struct beacon_data *old;
403 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
404 return -EINVAL;
406 old = sdata->u.ap.beacon;
408 if (old)
409 return -EALREADY;
411 return ieee80211_config_beacon(sdata, params);
414 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
415 struct beacon_parameters *params)
417 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
418 struct beacon_data *old;
420 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
421 return -EINVAL;
423 old = sdata->u.ap.beacon;
425 if (!old)
426 return -ENOENT;
428 return ieee80211_config_beacon(sdata, params);
431 static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
433 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
434 struct beacon_data *old;
436 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
437 return -EINVAL;
439 old = sdata->u.ap.beacon;
441 if (!old)
442 return -ENOENT;
444 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
445 synchronize_rcu();
446 kfree(old);
448 return ieee80211_if_config_beacon(dev);
451 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
452 struct iapp_layer2_update {
453 u8 da[ETH_ALEN]; /* broadcast */
454 u8 sa[ETH_ALEN]; /* STA addr */
455 __be16 len; /* 6 */
456 u8 dsap; /* 0 */
457 u8 ssap; /* 0 */
458 u8 control;
459 u8 xid_info[3];
460 } __attribute__ ((packed));
462 static void ieee80211_send_layer2_update(struct sta_info *sta)
464 struct iapp_layer2_update *msg;
465 struct sk_buff *skb;
467 /* Send Level 2 Update Frame to update forwarding tables in layer 2
468 * bridge devices */
470 skb = dev_alloc_skb(sizeof(*msg));
471 if (!skb)
472 return;
473 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
475 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
476 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
478 memset(msg->da, 0xff, ETH_ALEN);
479 memcpy(msg->sa, sta->addr, ETH_ALEN);
480 msg->len = htons(6);
481 msg->dsap = 0;
482 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
483 msg->control = 0xaf; /* XID response lsb.1111F101.
484 * F=0 (no poll command; unsolicited frame) */
485 msg->xid_info[0] = 0x81; /* XID format identifier */
486 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
487 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
489 skb->dev = sta->dev;
490 skb->protocol = eth_type_trans(skb, sta->dev);
491 memset(skb->cb, 0, sizeof(skb->cb));
492 netif_rx(skb);
495 static void sta_apply_parameters(struct ieee80211_local *local,
496 struct sta_info *sta,
497 struct station_parameters *params)
499 u32 rates;
500 int i, j;
501 struct ieee80211_hw_mode *mode;
503 if (params->station_flags & STATION_FLAG_CHANGED) {
504 sta->flags &= ~WLAN_STA_AUTHORIZED;
505 if (params->station_flags & STATION_FLAG_AUTHORIZED)
506 sta->flags |= WLAN_STA_AUTHORIZED;
508 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
509 if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE)
510 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
512 sta->flags &= ~WLAN_STA_WME;
513 if (params->station_flags & STATION_FLAG_WME)
514 sta->flags |= WLAN_STA_WME;
517 if (params->aid) {
518 sta->aid = params->aid;
519 if (sta->aid > IEEE80211_MAX_AID)
520 sta->aid = 0; /* XXX: should this be an error? */
523 if (params->listen_interval >= 0)
524 sta->listen_interval = params->listen_interval;
526 if (params->supported_rates) {
527 rates = 0;
528 mode = local->oper_hw_mode;
529 for (i = 0; i < params->supported_rates_len; i++) {
530 int rate = (params->supported_rates[i] & 0x7f) * 5;
531 for (j = 0; j < mode->num_rates; j++) {
532 if (mode->rates[j].rate == rate)
533 rates |= BIT(j);
536 sta->supp_rates = rates;
540 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
541 u8 *mac, struct station_parameters *params)
543 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
544 struct sta_info *sta;
545 struct ieee80211_sub_if_data *sdata;
547 /* Prevent a race with changing the rate control algorithm */
548 if (!netif_running(dev))
549 return -ENETDOWN;
551 /* XXX: get sta belonging to dev */
552 sta = sta_info_get(local, mac);
553 if (sta) {
554 sta_info_put(sta);
555 return -EEXIST;
558 if (params->vlan) {
559 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
561 if (sdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
562 sdata->vif.type != IEEE80211_IF_TYPE_AP)
563 return -EINVAL;
564 } else
565 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
567 sta = sta_info_add(local, dev, mac, GFP_KERNEL);
568 if (!sta)
569 return -ENOMEM;
571 sta->dev = sdata->dev;
572 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN ||
573 sdata->vif.type == IEEE80211_IF_TYPE_AP)
574 ieee80211_send_layer2_update(sta);
576 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
578 sta_apply_parameters(local, sta, params);
580 rate_control_rate_init(sta, local);
582 sta_info_put(sta);
584 return 0;
587 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
588 u8 *mac)
590 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
591 struct sta_info *sta;
593 if (mac) {
594 /* XXX: get sta belonging to dev */
595 sta = sta_info_get(local, mac);
596 if (!sta)
597 return -ENOENT;
599 sta_info_free(sta);
600 sta_info_put(sta);
601 } else
602 sta_info_flush(local, dev);
604 return 0;
607 static int ieee80211_change_station(struct wiphy *wiphy,
608 struct net_device *dev,
609 u8 *mac,
610 struct station_parameters *params)
612 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
613 struct sta_info *sta;
614 struct ieee80211_sub_if_data *vlansdata;
616 /* XXX: get sta belonging to dev */
617 sta = sta_info_get(local, mac);
618 if (!sta)
619 return -ENOENT;
621 if (params->vlan && params->vlan != sta->dev) {
622 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
624 if (vlansdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
625 vlansdata->vif.type != IEEE80211_IF_TYPE_AP)
626 return -EINVAL;
628 sta->dev = params->vlan;
629 ieee80211_send_layer2_update(sta);
632 sta_apply_parameters(local, sta, params);
634 sta_info_put(sta);
636 return 0;
639 struct cfg80211_ops mac80211_config_ops = {
640 .add_virtual_intf = ieee80211_add_iface,
641 .del_virtual_intf = ieee80211_del_iface,
642 .change_virtual_intf = ieee80211_change_iface,
643 .add_key = ieee80211_add_key,
644 .del_key = ieee80211_del_key,
645 .get_key = ieee80211_get_key,
646 .set_default_key = ieee80211_config_default_key,
647 .add_beacon = ieee80211_add_beacon,
648 .set_beacon = ieee80211_set_beacon,
649 .del_beacon = ieee80211_del_beacon,
650 .add_station = ieee80211_add_station,
651 .del_station = ieee80211_del_station,
652 .change_station = ieee80211_change_station,
653 .get_station = ieee80211_get_station,