2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/if_ether.h>
13 #include <linux/etherdevice.h>
14 #include <linux/list.h>
15 #include <linux/rcupdate.h>
16 #include <linux/rtnetlink.h>
17 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
19 #include "debugfs_key.h"
24 * DOC: Key handling basics
26 * Key handling in mac80211 is done based on per-interface (sub_if_data)
27 * keys and per-station keys. Since each station belongs to an interface,
28 * each station key also belongs to that interface.
30 * Hardware acceleration is done on a best-effort basis, for each key
31 * that is eligible the hardware is asked to enable that key but if
32 * it cannot do that they key is simply kept for software encryption.
33 * There is currently no way of knowing this except by looking into
36 * All operations here are called under RTNL so no extra locking is
39 * NOTE: This code requires that sta info *destruction* is done under
40 * RTNL, otherwise it can try to access already freed STA structs
41 * when a STA key is being freed.
44 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
45 static const u8 zero_addr[ETH_ALEN];
47 static const u8 *get_mac_for_key(struct ieee80211_key *key)
49 const u8 *addr = bcast_addr;
52 * If we're an AP we won't ever receive frames with a non-WEP
53 * group key so we tell the driver that by using the zero MAC
54 * address to indicate a transmit-only key.
56 if (key->conf.alg != ALG_WEP &&
57 (key->sdata->vif.type == IEEE80211_IF_TYPE_AP ||
58 key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN))
62 addr = key->sta->addr;
67 static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
73 if (!key->local->ops->set_key)
76 addr = get_mac_for_key(key);
78 ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
79 key->sdata->dev->dev_addr, addr,
83 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
85 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
86 printk(KERN_ERR "mac80211-%s: failed to set key "
87 "(%d, %s) to hardware (%d)\n",
88 wiphy_name(key->local->hw.wiphy),
89 key->conf.keyidx, print_mac(mac, addr), ret);
92 static void ieee80211_key_mark_hw_accel_off(struct ieee80211_key *key)
94 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
95 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
96 key->flags |= KEY_FLAG_REMOVE_FROM_HARDWARE;
100 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
104 DECLARE_MAC_BUF(mac);
106 if (!key || !key->local->ops->set_key)
109 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
110 !(key->flags & KEY_FLAG_REMOVE_FROM_HARDWARE))
113 addr = get_mac_for_key(key);
115 ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
116 key->sdata->dev->dev_addr, addr,
120 printk(KERN_ERR "mac80211-%s: failed to remove key "
121 "(%d, %s) from hardware (%d)\n",
122 wiphy_name(key->local->hw.wiphy),
123 key->conf.keyidx, print_mac(mac, addr), ret);
125 key->flags &= ~(KEY_FLAG_UPLOADED_TO_HARDWARE |
126 KEY_FLAG_REMOVE_FROM_HARDWARE);
129 struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
134 struct ieee80211_key *key;
136 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
138 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
143 * Default to software encryption; we'll later upload the
144 * key to the hardware if possible.
150 key->conf.keyidx = idx;
151 key->conf.keylen = key_len;
152 memcpy(key->conf.key, key_data, key_len);
153 INIT_LIST_HEAD(&key->list);
155 if (alg == ALG_CCMP) {
157 * Initialize AES key state here as an optimization so that
158 * it does not need to be initialized for every packet.
160 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
161 if (!key->u.ccmp.tfm) {
162 ieee80211_key_free(key);
170 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
171 struct sta_info *sta,
172 struct ieee80211_key *key,
173 struct ieee80211_key *new)
178 list_add(&new->list, &sdata->key_list);
181 rcu_assign_pointer(sta->key, new);
183 WARN_ON(new && key && new->conf.keyidx != key->conf.keyidx);
186 idx = key->conf.keyidx;
188 idx = new->conf.keyidx;
190 defkey = key && sdata->default_key == key;
193 ieee80211_set_default_key(sdata, -1);
195 rcu_assign_pointer(sdata->keys[idx], new);
197 ieee80211_set_default_key(sdata, new->conf.keyidx);
201 ieee80211_key_mark_hw_accel_off(key);
203 * We'll use an empty list to indicate that the key
204 * has already been removed.
206 list_del_init(&key->list);
210 void ieee80211_key_link(struct ieee80211_key *key,
211 struct ieee80211_sub_if_data *sdata,
212 struct sta_info *sta)
214 struct ieee80211_key *old_key;
223 idx = key->conf.keyidx;
224 key->local = sdata->local;
228 ieee80211_debugfs_key_add(key->local, key);
231 ieee80211_debugfs_key_sta_link(key, sta);
234 * some hardware cannot handle TKIP with QoS, so
235 * we indicate whether QoS could be in use.
237 if (sta->flags & WLAN_STA_WME)
238 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
240 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
245 /* same here, the AP could be using QoS */
246 ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
248 if (ap->flags & WLAN_STA_WME)
250 IEEE80211_KEY_FLAG_WMM_STA;
260 old_key = sdata->keys[idx];
262 __ieee80211_key_replace(sdata, sta, old_key, key);
266 ieee80211_key_free(old_key);
269 if (netif_running(sdata->dev))
270 ieee80211_key_enable_hw_accel(key);
273 void ieee80211_key_free(struct ieee80211_key *key)
283 * Replace key with nothingness.
285 * Because other code may have key reference (RCU protected)
286 * right now, we then wait for a grace period before freeing
288 * An empty list indicates it was never added to the key list
289 * or has been removed already. It may, however, still be in
290 * hardware for acceleration.
292 if (!list_empty(&key->list))
293 __ieee80211_key_replace(key->sdata, key->sta,
297 * Do NOT remove this without looking at sta_info_destroy()
302 * Remove from hwaccel if appropriate, this will
303 * only happen when the key is actually unlinked,
304 * it will already be done when the key was replaced.
306 ieee80211_key_disable_hw_accel(key);
309 if (key->conf.alg == ALG_CCMP)
310 ieee80211_aes_key_free(key->u.ccmp.tfm);
311 ieee80211_debugfs_key_remove(key);
316 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
318 struct ieee80211_key *key = NULL;
320 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
321 key = sdata->keys[idx];
323 if (sdata->default_key != key) {
324 ieee80211_debugfs_key_remove_default(sdata);
326 rcu_assign_pointer(sdata->default_key, key);
328 if (sdata->default_key)
329 ieee80211_debugfs_key_add_default(sdata);
333 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
335 struct ieee80211_key *key, *tmp;
341 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
342 ieee80211_key_free(key);
345 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
347 struct ieee80211_key *key;
352 if (WARN_ON(!netif_running(sdata->dev)))
355 list_for_each_entry(key, &sdata->key_list, list)
356 ieee80211_key_enable_hw_accel(key);
359 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
361 struct ieee80211_key *key;
366 list_for_each_entry(key, &sdata->key_list, list)
367 ieee80211_key_disable_hw_accel(key);