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);
78 * Remove again if already on the list so that we move it to the end.
80 if (!list_empty(&key->todo))
82 list_add_tail(&key->todo, &todo_list);
83 schedule_work(&todo_work);
84 spin_unlock(&todo_lock);
88 * ieee80211_key_lock - lock the mac80211 key operation lock
90 * This locks the (global) mac80211 key operation lock, all
91 * key operations must be done under this lock.
93 static void ieee80211_key_lock(void)
95 mutex_lock(&key_mutex);
99 * ieee80211_key_unlock - unlock the mac80211 key operation lock
101 static void ieee80211_key_unlock(void)
103 mutex_unlock(&key_mutex);
106 static void assert_key_lock(void)
108 WARN_ON(!mutex_is_locked(&key_mutex));
111 static const u8 *get_mac_for_key(struct ieee80211_key *key)
113 const u8 *addr = bcast_addr;
116 * If we're an AP we won't ever receive frames with a non-WEP
117 * group key so we tell the driver that by using the zero MAC
118 * address to indicate a transmit-only key.
120 if (key->conf.alg != ALG_WEP &&
121 (key->sdata->vif.type == IEEE80211_IF_TYPE_AP ||
122 key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN))
126 addr = key->sta->addr;
131 static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
135 DECLARE_MAC_BUF(mac);
140 if (!key->local->ops->set_key)
143 addr = get_mac_for_key(key);
145 ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
146 key->sdata->dev->dev_addr, addr,
150 spin_lock(&todo_lock);
151 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
152 spin_unlock(&todo_lock);
155 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
156 printk(KERN_ERR "mac80211-%s: failed to set key "
157 "(%d, %s) to hardware (%d)\n",
158 wiphy_name(key->local->hw.wiphy),
159 key->conf.keyidx, print_mac(mac, addr), ret);
162 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
166 DECLARE_MAC_BUF(mac);
171 if (!key || !key->local->ops->set_key)
174 spin_lock(&todo_lock);
175 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
176 spin_unlock(&todo_lock);
179 spin_unlock(&todo_lock);
181 addr = get_mac_for_key(key);
183 ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
184 key->sdata->dev->dev_addr, addr,
188 printk(KERN_ERR "mac80211-%s: failed to remove key "
189 "(%d, %s) from hardware (%d)\n",
190 wiphy_name(key->local->hw.wiphy),
191 key->conf.keyidx, print_mac(mac, addr), ret);
193 spin_lock(&todo_lock);
194 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
195 spin_unlock(&todo_lock);
198 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
201 struct ieee80211_key *key = NULL;
203 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
204 key = sdata->keys[idx];
206 rcu_assign_pointer(sdata->default_key, key);
209 add_todo(key, KEY_FLAG_TODO_DEFKEY);
212 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
216 spin_lock_irqsave(&sdata->local->key_lock, flags);
217 __ieee80211_set_default_key(sdata, idx);
218 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
222 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
223 struct sta_info *sta,
224 struct ieee80211_key *old,
225 struct ieee80211_key *new)
230 list_add(&new->list, &sdata->key_list);
233 rcu_assign_pointer(sta->key, new);
235 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
238 idx = old->conf.keyidx;
240 idx = new->conf.keyidx;
242 defkey = old && sdata->default_key == old;
245 __ieee80211_set_default_key(sdata, -1);
247 rcu_assign_pointer(sdata->keys[idx], new);
249 __ieee80211_set_default_key(sdata, new->conf.keyidx);
254 * We'll use an empty list to indicate that the key
255 * has already been removed.
257 list_del_init(&old->list);
261 struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
266 struct ieee80211_key *key;
268 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
270 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
275 * Default to software encryption; we'll later upload the
276 * key to the hardware if possible.
282 key->conf.keyidx = idx;
283 key->conf.keylen = key_len;
284 memcpy(key->conf.key, key_data, key_len);
285 INIT_LIST_HEAD(&key->list);
286 INIT_LIST_HEAD(&key->todo);
288 if (alg == ALG_CCMP) {
290 * Initialize AES key state here as an optimization so that
291 * it does not need to be initialized for every packet.
293 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
294 if (!key->u.ccmp.tfm) {
303 void ieee80211_key_link(struct ieee80211_key *key,
304 struct ieee80211_sub_if_data *sdata,
305 struct sta_info *sta)
307 struct ieee80211_key *old_key;
314 idx = key->conf.keyidx;
315 key->local = sdata->local;
321 * some hardware cannot handle TKIP with QoS, so
322 * we indicate whether QoS could be in use.
324 if (test_sta_flags(sta, WLAN_STA_WME))
325 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
328 * This key is for a specific sta interface,
329 * inform the driver that it should try to store
330 * this key as pairwise key.
332 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
334 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
338 * We're getting a sta pointer in,
339 * so must be under RCU read lock.
342 /* same here, the AP could be using QoS */
343 ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
345 if (test_sta_flags(ap, WLAN_STA_WME))
347 IEEE80211_KEY_FLAG_WMM_STA;
352 spin_lock_irqsave(&sdata->local->key_lock, flags);
357 old_key = sdata->keys[idx];
359 __ieee80211_key_replace(sdata, sta, old_key, key);
361 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
363 /* free old key later */
364 add_todo(old_key, KEY_FLAG_TODO_DELETE);
366 add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
367 if (netif_running(sdata->dev))
368 add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
371 static void __ieee80211_key_free(struct ieee80211_key *key)
374 * Replace key with nothingness if it was ever used.
377 __ieee80211_key_replace(key->sdata, key->sta,
380 add_todo(key, KEY_FLAG_TODO_DELETE);
383 void ieee80211_key_free(struct ieee80211_key *key)
391 /* The key has not been linked yet, simply free it
393 if (key->conf.alg == ALG_CCMP)
394 ieee80211_aes_key_free(key->u.ccmp.tfm);
399 spin_lock_irqsave(&key->sdata->local->key_lock, flags);
400 __ieee80211_key_free(key);
401 spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
405 * To be safe against concurrent manipulations of the list (which shouldn't
406 * actually happen) we need to hold the spinlock. But under the spinlock we
407 * can't actually do much, so we defer processing to the todo list. Then run
408 * the todo list to be sure the operation and possibly previously pending
409 * operations are completed.
411 static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
414 struct ieee80211_key *key;
419 spin_lock_irqsave(&sdata->local->key_lock, flags);
420 list_for_each_entry(key, &sdata->key_list, list)
421 add_todo(key, todo_flags);
422 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
424 ieee80211_key_todo();
427 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
431 if (WARN_ON(!netif_running(sdata->dev)))
434 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
437 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
441 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
444 static void __ieee80211_key_destroy(struct ieee80211_key *key)
449 ieee80211_key_disable_hw_accel(key);
451 if (key->conf.alg == ALG_CCMP)
452 ieee80211_aes_key_free(key->u.ccmp.tfm);
453 ieee80211_debugfs_key_remove(key);
458 static void __ieee80211_key_todo(void)
460 struct ieee80211_key *key;
465 * NB: sta_info_destroy relies on this!
469 spin_lock(&todo_lock);
470 while (!list_empty(&todo_list)) {
471 key = list_first_entry(&todo_list, struct ieee80211_key, todo);
472 list_del_init(&key->todo);
473 todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
474 KEY_FLAG_TODO_DEFKEY |
475 KEY_FLAG_TODO_HWACCEL_ADD |
476 KEY_FLAG_TODO_HWACCEL_REMOVE |
477 KEY_FLAG_TODO_DELETE);
478 key->flags &= ~todoflags;
479 spin_unlock(&todo_lock);
483 if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
484 ieee80211_debugfs_key_add(key);
487 if (todoflags & KEY_FLAG_TODO_DEFKEY) {
488 ieee80211_debugfs_key_remove_default(key->sdata);
489 ieee80211_debugfs_key_add_default(key->sdata);
492 if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
493 ieee80211_key_enable_hw_accel(key);
496 if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
497 ieee80211_key_disable_hw_accel(key);
500 if (todoflags & KEY_FLAG_TODO_DELETE) {
501 __ieee80211_key_destroy(key);
507 spin_lock(&todo_lock);
509 spin_unlock(&todo_lock);
512 void ieee80211_key_todo(void)
514 ieee80211_key_lock();
515 __ieee80211_key_todo();
516 ieee80211_key_unlock();
519 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
521 struct ieee80211_key *key, *tmp;
524 ieee80211_key_lock();
526 ieee80211_debugfs_key_remove_default(sdata);
528 spin_lock_irqsave(&sdata->local->key_lock, flags);
529 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
530 __ieee80211_key_free(key);
531 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
533 __ieee80211_key_todo();
535 ieee80211_key_unlock();