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-2008 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 key operations are protected internally so you can call them at
39 * Within mac80211, key references are, just as STA structure references,
40 * protected by RCU. Note, however, that some things are unprotected,
41 * namely the key->sta dereferences within the hardware acceleration
42 * functions. This means that sta_info_destroy() must flush the key todo
45 * All the direct key list manipulation functions must not sleep because
46 * they can operate on STA info structs that are protected by RCU.
49 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
50 static const u8 zero_addr[ETH_ALEN];
52 /* key mutex: used to synchronise todo runners */
53 static DEFINE_MUTEX(key_mutex);
54 static DEFINE_SPINLOCK(todo_lock);
55 static LIST_HEAD(todo_list);
57 static void key_todo(struct work_struct *work)
62 static DECLARE_WORK(todo_work, key_todo);
65 * add_todo - add todo item for a key
67 * @key: key to add to do item for
70 static void add_todo(struct ieee80211_key *key, u32 flag)
75 spin_lock(&todo_lock);
77 /* only add if not already added */
78 if (list_empty(&key->todo))
79 list_add(&key->todo, &todo_list);
80 schedule_work(&todo_work);
81 spin_unlock(&todo_lock);
85 * ieee80211_key_lock - lock the mac80211 key operation lock
87 * This locks the (global) mac80211 key operation lock, all
88 * key operations must be done under this lock.
90 static void ieee80211_key_lock(void)
92 mutex_lock(&key_mutex);
96 * ieee80211_key_unlock - unlock the mac80211 key operation lock
98 static void ieee80211_key_unlock(void)
100 mutex_unlock(&key_mutex);
103 static void assert_key_lock(void)
105 WARN_ON(!mutex_is_locked(&key_mutex));
108 static const u8 *get_mac_for_key(struct ieee80211_key *key)
110 const u8 *addr = bcast_addr;
113 * If we're an AP we won't ever receive frames with a non-WEP
114 * group key so we tell the driver that by using the zero MAC
115 * address to indicate a transmit-only key.
117 if (key->conf.alg != ALG_WEP &&
118 (key->sdata->vif.type == IEEE80211_IF_TYPE_AP ||
119 key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN))
123 addr = key->sta->addr;
128 static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
132 DECLARE_MAC_BUF(mac);
137 if (!key->local->ops->set_key)
140 addr = get_mac_for_key(key);
142 ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
143 key->sdata->dev->dev_addr, addr,
147 spin_lock(&todo_lock);
148 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
149 spin_unlock(&todo_lock);
152 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
153 printk(KERN_ERR "mac80211-%s: failed to set key "
154 "(%d, %s) to hardware (%d)\n",
155 wiphy_name(key->local->hw.wiphy),
156 key->conf.keyidx, print_mac(mac, addr), ret);
159 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
163 DECLARE_MAC_BUF(mac);
168 if (!key || !key->local->ops->set_key)
171 spin_lock(&todo_lock);
172 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
173 spin_unlock(&todo_lock);
176 spin_unlock(&todo_lock);
178 addr = get_mac_for_key(key);
180 ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
181 key->sdata->dev->dev_addr, addr,
185 printk(KERN_ERR "mac80211-%s: failed to remove key "
186 "(%d, %s) from hardware (%d)\n",
187 wiphy_name(key->local->hw.wiphy),
188 key->conf.keyidx, print_mac(mac, addr), ret);
190 spin_lock(&todo_lock);
191 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
192 spin_unlock(&todo_lock);
195 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
198 struct ieee80211_key *key = NULL;
200 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
201 key = sdata->keys[idx];
203 rcu_assign_pointer(sdata->default_key, key);
206 add_todo(key, KEY_FLAG_TODO_DEFKEY);
209 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
213 spin_lock_irqsave(&sdata->local->sta_lock, flags);
214 __ieee80211_set_default_key(sdata, idx);
215 spin_unlock_irqrestore(&sdata->local->sta_lock, flags);
219 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
220 struct sta_info *sta,
221 struct ieee80211_key *old,
222 struct ieee80211_key *new)
227 list_add(&new->list, &sdata->key_list);
230 rcu_assign_pointer(sta->key, new);
232 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
235 idx = old->conf.keyidx;
237 idx = new->conf.keyidx;
239 defkey = old && sdata->default_key == old;
242 __ieee80211_set_default_key(sdata, -1);
244 rcu_assign_pointer(sdata->keys[idx], new);
246 __ieee80211_set_default_key(sdata, new->conf.keyidx);
251 * We'll use an empty list to indicate that the key
252 * has already been removed.
254 list_del_init(&old->list);
258 struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
263 struct ieee80211_key *key;
265 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
267 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
272 * Default to software encryption; we'll later upload the
273 * key to the hardware if possible.
279 key->conf.keyidx = idx;
280 key->conf.keylen = key_len;
281 memcpy(key->conf.key, key_data, key_len);
282 INIT_LIST_HEAD(&key->list);
283 INIT_LIST_HEAD(&key->todo);
285 if (alg == ALG_CCMP) {
287 * Initialize AES key state here as an optimization so that
288 * it does not need to be initialized for every packet.
290 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
291 if (!key->u.ccmp.tfm) {
300 void ieee80211_key_link(struct ieee80211_key *key,
301 struct ieee80211_sub_if_data *sdata,
302 struct sta_info *sta)
304 struct ieee80211_key *old_key;
311 idx = key->conf.keyidx;
312 key->local = sdata->local;
318 * some hardware cannot handle TKIP with QoS, so
319 * we indicate whether QoS could be in use.
321 if (sta->flags & WLAN_STA_WME)
322 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
324 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
328 * We're getting a sta pointer in,
329 * so must be under RCU read lock.
332 /* same here, the AP could be using QoS */
333 ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
335 if (ap->flags & WLAN_STA_WME)
337 IEEE80211_KEY_FLAG_WMM_STA;
342 spin_lock_irqsave(&sdata->local->sta_lock, flags);
347 old_key = sdata->keys[idx];
349 __ieee80211_key_replace(sdata, sta, old_key, key);
351 spin_unlock_irqrestore(&sdata->local->sta_lock, flags);
353 /* free old key later */
354 add_todo(old_key, KEY_FLAG_TODO_DELETE);
356 add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
357 if (netif_running(sdata->dev))
358 add_todo(key, KEY_FLAG_TODO_HWACCEL);
361 void ieee80211_key_free(struct ieee80211_key *key)
369 * Replace key with nothingness if it was ever used.
372 spin_lock_irqsave(&key->sdata->local->sta_lock, flags);
373 __ieee80211_key_replace(key->sdata, key->sta,
375 spin_unlock_irqrestore(&key->sdata->local->sta_lock, flags);
378 add_todo(key, KEY_FLAG_TODO_DELETE);
381 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
383 struct ieee80211_key *key;
387 if (WARN_ON(!netif_running(sdata->dev)))
390 ieee80211_key_lock();
392 list_for_each_entry(key, &sdata->key_list, list)
393 ieee80211_key_enable_hw_accel(key);
395 ieee80211_key_unlock();
398 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
400 struct ieee80211_key *key;
404 ieee80211_key_lock();
406 list_for_each_entry(key, &sdata->key_list, list)
407 ieee80211_key_disable_hw_accel(key);
409 ieee80211_key_unlock();
412 static void __ieee80211_key_free(struct ieee80211_key *key)
417 ieee80211_key_disable_hw_accel(key);
419 if (key->conf.alg == ALG_CCMP)
420 ieee80211_aes_key_free(key->u.ccmp.tfm);
421 ieee80211_debugfs_key_remove(key);
426 static void __ieee80211_key_todo(void)
428 struct ieee80211_key *key;
433 * NB: sta_info_destroy relies on this!
437 spin_lock(&todo_lock);
438 while (!list_empty(&todo_list)) {
439 key = list_first_entry(&todo_list, struct ieee80211_key, todo);
440 list_del_init(&key->todo);
441 todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
442 KEY_FLAG_TODO_DEFKEY |
443 KEY_FLAG_TODO_HWACCEL |
444 KEY_FLAG_TODO_DELETE);
445 key->flags &= ~todoflags;
446 spin_unlock(&todo_lock);
450 if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
451 ieee80211_debugfs_key_add(key);
454 if (todoflags & KEY_FLAG_TODO_DEFKEY) {
455 ieee80211_debugfs_key_remove_default(key->sdata);
456 ieee80211_debugfs_key_add_default(key->sdata);
459 if (todoflags & KEY_FLAG_TODO_HWACCEL) {
460 ieee80211_key_enable_hw_accel(key);
463 if (todoflags & KEY_FLAG_TODO_DELETE) {
464 __ieee80211_key_free(key);
470 spin_lock(&todo_lock);
472 spin_unlock(&todo_lock);
475 void ieee80211_key_todo(void)
477 ieee80211_key_lock();
478 __ieee80211_key_todo();
479 ieee80211_key_unlock();
482 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
484 struct ieee80211_key *key, *tmp;
487 ieee80211_key_lock();
489 ieee80211_debugfs_key_remove_default(sdata);
491 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
492 ieee80211_key_free(key);
494 __ieee80211_key_todo();
496 ieee80211_key_unlock();