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)
39 struct ieee80211_local *local = wiphy_priv(wiphy);
40 enum ieee80211_if_types itype;
41 struct net_device *dev;
42 struct ieee80211_sub_if_data *sdata;
45 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
48 itype = nl80211_type_to_mac80211_type(type);
49 if (itype == IEEE80211_IF_TYPE_INVALID)
52 err = ieee80211_if_add(local->mdev, name, &dev, itype);
53 if (err || itype != IEEE80211_IF_TYPE_MNTR || !flags)
56 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
57 sdata->u.mntr_flags = *flags;
61 static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
63 struct ieee80211_local *local = wiphy_priv(wiphy);
64 struct net_device *dev;
67 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
70 /* we're under RTNL */
71 dev = __dev_get_by_index(&init_net, ifindex);
77 return ieee80211_if_remove(local->mdev, name, -1);
80 static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
81 enum nl80211_iftype type, u32 *flags)
83 struct ieee80211_local *local = wiphy_priv(wiphy);
84 struct net_device *dev;
85 enum ieee80211_if_types itype;
86 struct ieee80211_sub_if_data *sdata;
88 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
91 /* we're under RTNL */
92 dev = __dev_get_by_index(&init_net, ifindex);
96 if (netif_running(dev))
99 itype = nl80211_type_to_mac80211_type(type);
100 if (itype == IEEE80211_IF_TYPE_INVALID)
103 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
105 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
108 ieee80211_if_reinit(dev);
109 ieee80211_if_set_type(dev, itype);
111 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR || !flags)
114 sdata->u.mntr_flags = *flags;
118 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
119 u8 key_idx, u8 *mac_addr,
120 struct key_params *params)
122 struct ieee80211_sub_if_data *sdata;
123 struct sta_info *sta = NULL;
124 enum ieee80211_key_alg alg;
126 struct ieee80211_key *key;
128 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
130 switch (params->cipher) {
131 case WLAN_CIPHER_SUITE_WEP40:
132 case WLAN_CIPHER_SUITE_WEP104:
135 case WLAN_CIPHER_SUITE_TKIP:
138 case WLAN_CIPHER_SUITE_CCMP:
145 key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key);
150 sta = sta_info_get(sdata->local, mac_addr);
152 ieee80211_key_free(key);
157 ieee80211_key_link(key, sdata, sta);
167 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
168 u8 key_idx, u8 *mac_addr)
170 struct ieee80211_sub_if_data *sdata;
171 struct sta_info *sta;
173 struct ieee80211_key *key;
175 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
178 sta = sta_info_get(sdata->local, mac_addr);
185 ieee80211_key_free(key);
194 if (!sdata->keys[key_idx])
197 key = sdata->keys[key_idx];
198 ieee80211_key_free(key);
199 WARN_ON(sdata->keys[key_idx]);
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))
209 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
210 struct sta_info *sta = NULL;
212 struct key_params params;
213 struct ieee80211_key *key;
219 sta = sta_info_get(sdata->local, mac_addr);
225 key = sdata->keys[key_idx];
230 memset(¶ms, 0, sizeof(params));
232 switch (key->conf.alg) {
234 params.cipher = WLAN_CIPHER_SUITE_TKIP;
236 iv32 = key->u.tkip.iv32;
237 iv16 = key->u.tkip.iv16;
239 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
240 sdata->local->ops->get_tkip_seq)
241 sdata->local->ops->get_tkip_seq(
242 local_to_hw(sdata->local),
243 key->conf.hw_key_idx,
246 seq[0] = iv16 & 0xff;
247 seq[1] = (iv16 >> 8) & 0xff;
248 seq[2] = iv32 & 0xff;
249 seq[3] = (iv32 >> 8) & 0xff;
250 seq[4] = (iv32 >> 16) & 0xff;
251 seq[5] = (iv32 >> 24) & 0xff;
256 params.cipher = WLAN_CIPHER_SUITE_CCMP;
257 seq[0] = key->u.ccmp.tx_pn[5];
258 seq[1] = key->u.ccmp.tx_pn[4];
259 seq[2] = key->u.ccmp.tx_pn[3];
260 seq[3] = key->u.ccmp.tx_pn[2];
261 seq[4] = key->u.ccmp.tx_pn[1];
262 seq[5] = key->u.ccmp.tx_pn[0];
267 if (key->conf.keylen == 5)
268 params.cipher = WLAN_CIPHER_SUITE_WEP40;
270 params.cipher = WLAN_CIPHER_SUITE_WEP104;
274 params.key = key->conf.key;
275 params.key_len = key->conf.keylen;
277 callback(cookie, ¶ms);
286 static int ieee80211_config_default_key(struct wiphy *wiphy,
287 struct net_device *dev,
290 struct ieee80211_sub_if_data *sdata;
292 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
293 ieee80211_set_default_key(sdata, key_idx);
298 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
299 u8 *mac, struct station_stats *stats)
301 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
302 struct sta_info *sta;
304 sta = sta_info_get(local, mac);
308 /* XXX: verify sta->dev == dev */
310 stats->filled = STATION_STAT_INACTIVE_TIME |
311 STATION_STAT_RX_BYTES |
312 STATION_STAT_TX_BYTES;
314 stats->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
315 stats->rx_bytes = sta->rx_bytes;
316 stats->tx_bytes = sta->tx_bytes;
324 * This handles both adding a beacon and setting new beacon info
326 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
327 struct beacon_parameters *params)
329 struct beacon_data *new, *old;
330 int new_head_len, new_tail_len;
334 old = sdata->u.ap.beacon;
336 /* head must not be zero-length */
337 if (params->head && !params->head_len)
341 * This is a kludge. beacon interval should really be part
342 * of the beacon information.
344 if (params->interval) {
345 sdata->local->hw.conf.beacon_int = params->interval;
346 if (ieee80211_hw_config(sdata->local))
349 * We updated some parameter so if below bails out
355 /* Need to have a beacon head if we don't have one yet */
356 if (!params->head && !old)
359 /* sorry, no way to start beaconing without dtim period */
360 if (!params->dtim_period && !old)
363 /* new or old head? */
365 new_head_len = params->head_len;
367 new_head_len = old->head_len;
369 /* new or old tail? */
370 if (params->tail || !old)
371 /* params->tail_len will be zero for !params->tail */
372 new_tail_len = params->tail_len;
374 new_tail_len = old->tail_len;
376 size = sizeof(*new) + new_head_len + new_tail_len;
378 new = kzalloc(size, GFP_KERNEL);
382 /* start filling the new info now */
384 /* new or old dtim period? */
385 if (params->dtim_period)
386 new->dtim_period = params->dtim_period;
388 new->dtim_period = old->dtim_period;
391 * pointers go into the block we allocated,
392 * memory is | beacon_data | head | tail |
394 new->head = ((u8 *) new) + sizeof(*new);
395 new->tail = new->head + new_head_len;
396 new->head_len = new_head_len;
397 new->tail_len = new_tail_len;
401 memcpy(new->head, params->head, new_head_len);
403 memcpy(new->head, old->head, new_head_len);
405 /* copy in optional tail */
407 memcpy(new->tail, params->tail, new_tail_len);
410 memcpy(new->tail, old->tail, new_tail_len);
412 rcu_assign_pointer(sdata->u.ap.beacon, new);
418 return ieee80211_if_config_beacon(sdata->dev);
421 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
422 struct beacon_parameters *params)
424 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
425 struct beacon_data *old;
427 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
430 old = sdata->u.ap.beacon;
435 return ieee80211_config_beacon(sdata, params);
438 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
439 struct beacon_parameters *params)
441 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
442 struct beacon_data *old;
444 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
447 old = sdata->u.ap.beacon;
452 return ieee80211_config_beacon(sdata, params);
455 static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
457 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
458 struct beacon_data *old;
460 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
463 old = sdata->u.ap.beacon;
468 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
472 return ieee80211_if_config_beacon(dev);
475 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
476 struct iapp_layer2_update {
477 u8 da[ETH_ALEN]; /* broadcast */
478 u8 sa[ETH_ALEN]; /* STA addr */
484 } __attribute__ ((packed));
486 static void ieee80211_send_layer2_update(struct sta_info *sta)
488 struct iapp_layer2_update *msg;
491 /* Send Level 2 Update Frame to update forwarding tables in layer 2
494 skb = dev_alloc_skb(sizeof(*msg));
497 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
499 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
500 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
502 memset(msg->da, 0xff, ETH_ALEN);
503 memcpy(msg->sa, sta->addr, ETH_ALEN);
506 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
507 msg->control = 0xaf; /* XID response lsb.1111F101.
508 * F=0 (no poll command; unsolicited frame) */
509 msg->xid_info[0] = 0x81; /* XID format identifier */
510 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
511 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
514 skb->protocol = eth_type_trans(skb, sta->dev);
515 memset(skb->cb, 0, sizeof(skb->cb));
519 static void sta_apply_parameters(struct ieee80211_local *local,
520 struct sta_info *sta,
521 struct station_parameters *params)
525 struct ieee80211_supported_band *sband;
527 if (params->station_flags & STATION_FLAG_CHANGED) {
528 sta->flags &= ~WLAN_STA_AUTHORIZED;
529 if (params->station_flags & STATION_FLAG_AUTHORIZED)
530 sta->flags |= WLAN_STA_AUTHORIZED;
532 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
533 if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE)
534 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
536 sta->flags &= ~WLAN_STA_WME;
537 if (params->station_flags & STATION_FLAG_WME)
538 sta->flags |= WLAN_STA_WME;
542 sta->aid = params->aid;
543 if (sta->aid > IEEE80211_MAX_AID)
544 sta->aid = 0; /* XXX: should this be an error? */
547 if (params->listen_interval >= 0)
548 sta->listen_interval = params->listen_interval;
550 if (params->supported_rates) {
552 sband = local->hw.wiphy->bands[local->oper_channel->band];
554 for (i = 0; i < params->supported_rates_len; i++) {
555 int rate = (params->supported_rates[i] & 0x7f) * 5;
556 for (j = 0; j < sband->n_bitrates; j++) {
557 if (sband->bitrates[j].bitrate == rate)
561 sta->supp_rates[local->oper_channel->band] = rates;
565 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
566 u8 *mac, struct station_parameters *params)
568 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
569 struct sta_info *sta;
570 struct ieee80211_sub_if_data *sdata;
572 /* Prevent a race with changing the rate control algorithm */
573 if (!netif_running(dev))
577 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
579 if (sdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
580 sdata->vif.type != IEEE80211_IF_TYPE_AP)
583 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
585 sta = sta_info_add(local, dev, mac, GFP_KERNEL);
589 sta->dev = sdata->dev;
590 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN ||
591 sdata->vif.type == IEEE80211_IF_TYPE_AP)
592 ieee80211_send_layer2_update(sta);
594 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
596 sta_apply_parameters(local, sta, params);
598 rate_control_rate_init(sta, local);
605 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
608 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
609 struct sta_info *sta;
612 /* XXX: get sta belonging to dev */
613 sta = sta_info_get(local, mac);
620 sta_info_flush(local, dev);
625 static int ieee80211_change_station(struct wiphy *wiphy,
626 struct net_device *dev,
628 struct station_parameters *params)
630 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
631 struct sta_info *sta;
632 struct ieee80211_sub_if_data *vlansdata;
634 /* XXX: get sta belonging to dev */
635 sta = sta_info_get(local, mac);
639 if (params->vlan && params->vlan != sta->dev) {
640 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
642 if (vlansdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
643 vlansdata->vif.type != IEEE80211_IF_TYPE_AP)
646 sta->dev = params->vlan;
647 ieee80211_send_layer2_update(sta);
650 sta_apply_parameters(local, sta, params);
657 struct cfg80211_ops mac80211_config_ops = {
658 .add_virtual_intf = ieee80211_add_iface,
659 .del_virtual_intf = ieee80211_del_iface,
660 .change_virtual_intf = ieee80211_change_iface,
661 .add_key = ieee80211_add_key,
662 .del_key = ieee80211_del_key,
663 .get_key = ieee80211_get_key,
664 .set_default_key = ieee80211_config_default_key,
665 .add_beacon = ieee80211_add_beacon,
666 .set_beacon = ieee80211_set_beacon,
667 .del_beacon = ieee80211_del_beacon,
668 .add_station = ieee80211_add_station,
669 .del_station = ieee80211_del_station,
670 .change_station = ieee80211_change_station,
671 .get_station = ieee80211_get_station,