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, 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;
46 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
49 itype = nl80211_type_to_mac80211_type(type);
50 if (itype == IEEE80211_IF_TYPE_INVALID)
53 err = ieee80211_if_add(local->mdev, name, &dev, itype, params);
54 if (err || itype != IEEE80211_IF_TYPE_MNTR || !flags)
57 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
58 sdata->u.mntr_flags = *flags;
62 static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
64 struct ieee80211_local *local = wiphy_priv(wiphy);
65 struct net_device *dev;
68 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
71 /* we're under RTNL */
72 dev = __dev_get_by_index(&init_net, ifindex);
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))
93 /* we're under RTNL */
94 dev = __dev_get_by_index(&init_net, ifindex);
98 if (netif_running(dev))
101 itype = nl80211_type_to_mac80211_type(type);
102 if (itype == IEEE80211_IF_TYPE_INVALID)
105 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
107 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
110 ieee80211_if_reinit(dev);
111 ieee80211_if_set_type(dev, itype);
113 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR || !flags)
116 sdata->u.mntr_flags = *flags;
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;
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:
137 case WLAN_CIPHER_SUITE_TKIP:
140 case WLAN_CIPHER_SUITE_CCMP:
147 key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key);
152 sta = sta_info_get(sdata->local, mac_addr);
154 ieee80211_key_free(key);
159 ieee80211_key_link(key, sdata, sta);
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;
175 struct ieee80211_key *key;
177 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
180 sta = sta_info_get(sdata->local, mac_addr);
187 ieee80211_key_free(key);
196 if (!sdata->keys[key_idx])
199 key = sdata->keys[key_idx];
200 ieee80211_key_free(key);
201 WARN_ON(sdata->keys[key_idx]);
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;
214 struct key_params params;
215 struct ieee80211_key *key;
221 sta = sta_info_get(sdata->local, mac_addr);
227 key = sdata->keys[key_idx];
232 memset(¶ms, 0, sizeof(params));
234 switch (key->conf.alg) {
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,
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;
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];
269 if (key->conf.keylen == 5)
270 params.cipher = WLAN_CIPHER_SUITE_WEP40;
272 params.cipher = WLAN_CIPHER_SUITE_WEP104;
276 params.key = key->conf.key;
277 params.key_len = key->conf.keylen;
279 callback(cookie, ¶ms);
288 static int ieee80211_config_default_key(struct wiphy *wiphy,
289 struct net_device *dev,
292 struct ieee80211_sub_if_data *sdata;
294 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
295 ieee80211_set_default_key(sdata, key_idx);
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);
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;
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;
336 old = sdata->u.ap.beacon;
338 /* head must not be zero-length */
339 if (params->head && !params->head_len)
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))
351 * We updated some parameter so if below bails out
357 /* Need to have a beacon head if we don't have one yet */
358 if (!params->head && !old)
361 /* sorry, no way to start beaconing without dtim period */
362 if (!params->dtim_period && !old)
365 /* new or old head? */
367 new_head_len = params->head_len;
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;
376 new_tail_len = old->tail_len;
378 size = sizeof(*new) + new_head_len + new_tail_len;
380 new = kzalloc(size, GFP_KERNEL);
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;
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;
403 memcpy(new->head, params->head, new_head_len);
405 memcpy(new->head, old->head, new_head_len);
407 /* copy in optional tail */
409 memcpy(new->tail, params->tail, new_tail_len);
412 memcpy(new->tail, old->tail, new_tail_len);
414 rcu_assign_pointer(sdata->u.ap.beacon, new);
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)
432 old = sdata->u.ap.beacon;
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)
449 old = sdata->u.ap.beacon;
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)
465 old = sdata->u.ap.beacon;
470 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
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 */
486 } __attribute__ ((packed));
488 static void ieee80211_send_layer2_update(struct sta_info *sta)
490 struct iapp_layer2_update *msg;
493 /* Send Level 2 Update Frame to update forwarding tables in layer 2
496 skb = dev_alloc_skb(sizeof(*msg));
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);
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) */
516 skb->protocol = eth_type_trans(skb, sta->dev);
517 memset(skb->cb, 0, sizeof(skb->cb));
521 static void sta_apply_parameters(struct ieee80211_local *local,
522 struct sta_info *sta,
523 struct station_parameters *params)
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;
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) {
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)
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))
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)
585 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
587 sta = sta_info_add(local, dev, mac, GFP_KERNEL);
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);
607 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
611 struct sta_info *sta;
614 /* XXX: get sta belonging to dev */
615 sta = sta_info_get(local, mac);
622 sta_info_flush(local, dev);
627 static int ieee80211_change_station(struct wiphy *wiphy,
628 struct net_device *dev,
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);
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)
648 sta->dev = params->vlan;
649 ieee80211_send_layer2_update(sta);
652 sta_apply_parameters(local, sta, params);
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,