2 * mac80211 configuration hooks for cfg80211
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
6 * This file is GPLv2 as found in COPYING.
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"
17 #include "ieee80211_rate.h"
19 static enum ieee80211_if_types
20 nl80211_type_to_mac80211_type(enum nl80211_iftype 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;
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))
45 itype = nl80211_type_to_mac80211_type(type);
46 if (itype == IEEE80211_IF_TYPE_INVALID)
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;
58 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
61 /* we're under RTNL */
62 dev = __dev_get_by_index(&init_net, ifindex);
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))
82 /* we're under RTNL */
83 dev = __dev_get_by_index(&init_net, ifindex);
87 if (netif_running(dev))
90 itype = nl80211_type_to_mac80211_type(type);
91 if (itype == IEEE80211_IF_TYPE_INVALID)
94 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
96 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
99 ieee80211_if_reinit(dev);
100 ieee80211_if_set_type(dev, itype);
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;
114 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
116 switch (params->cipher) {
117 case WLAN_CIPHER_SUITE_WEP40:
118 case WLAN_CIPHER_SUITE_WEP104:
121 case WLAN_CIPHER_SUITE_TKIP:
124 case WLAN_CIPHER_SUITE_CCMP:
132 sta = sta_info_get(sdata->local, mac_addr);
138 if (!ieee80211_key_alloc(sdata, sta, alg, key_idx,
139 params->key_len, params->key))
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;
155 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
158 sta = sta_info_get(sdata->local, mac_addr);
164 ieee80211_key_free(sta->key);
172 if (!sdata->keys[key_idx])
175 ieee80211_key_free(sdata->keys[key_idx]);
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;
188 struct key_params params;
189 struct ieee80211_key *key;
195 sta = sta_info_get(sdata->local, mac_addr);
201 key = sdata->keys[key_idx];
206 memset(¶ms, 0, sizeof(params));
208 switch (key->conf.alg) {
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,
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;
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];
243 if (key->conf.keylen == 5)
244 params.cipher = WLAN_CIPHER_SUITE_WEP40;
246 params.cipher = WLAN_CIPHER_SUITE_WEP104;
250 params.key = key->conf.key;
251 params.key_len = key->conf.keylen;
253 callback(cookie, ¶ms);
262 static int ieee80211_config_default_key(struct wiphy *wiphy,
263 struct net_device *dev,
266 struct ieee80211_sub_if_data *sdata;
268 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
269 ieee80211_set_default_key(sdata, key_idx);
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);
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;
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;
310 old = sdata->u.ap.beacon;
312 /* head must not be zero-length */
313 if (params->head && !params->head_len)
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))
325 * We updated some parameter so if below bails out
331 /* Need to have a beacon head if we don't have one yet */
332 if (!params->head && !old)
335 /* sorry, no way to start beaconing without dtim period */
336 if (!params->dtim_period && !old)
339 /* new or old head? */
341 new_head_len = params->head_len;
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;
350 new_tail_len = old->tail_len;
352 size = sizeof(*new) + new_head_len + new_tail_len;
354 new = kzalloc(size, GFP_KERNEL);
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;
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;
377 memcpy(new->head, params->head, new_head_len);
379 memcpy(new->head, old->head, new_head_len);
381 /* copy in optional tail */
383 memcpy(new->tail, params->tail, new_tail_len);
386 memcpy(new->tail, old->tail, new_tail_len);
388 rcu_assign_pointer(sdata->u.ap.beacon, new);
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)
406 old = sdata->u.ap.beacon;
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)
423 old = sdata->u.ap.beacon;
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)
439 old = sdata->u.ap.beacon;
444 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
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 */
460 } __attribute__ ((packed));
462 static void ieee80211_send_layer2_update(struct sta_info *sta)
464 struct iapp_layer2_update *msg;
467 /* Send Level 2 Update Frame to update forwarding tables in layer 2
470 skb = dev_alloc_skb(sizeof(*msg));
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);
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) */
490 skb->protocol = eth_type_trans(skb, sta->dev);
491 memset(skb->cb, 0, sizeof(skb->cb));
495 static void sta_apply_parameters(struct ieee80211_local *local,
496 struct sta_info *sta,
497 struct station_parameters *params)
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;
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) {
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)
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))
551 /* XXX: get sta belonging to dev */
552 sta = sta_info_get(local, mac);
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)
565 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
567 sta = sta_info_add(local, dev, mac, GFP_KERNEL);
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);
587 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
590 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
591 struct sta_info *sta;
594 /* XXX: get sta belonging to dev */
595 sta = sta_info_get(local, mac);
602 sta_info_flush(local, dev);
607 static int ieee80211_change_station(struct wiphy *wiphy,
608 struct net_device *dev,
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);
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)
628 sta->dev = params->vlan;
629 ieee80211_send_layer2_update(sta);
632 sta_apply_parameters(local, sta, params);
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,