kobj: Add basic infrastructure for dealing with namespaces.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / key.c
blob8d4b41787dcf5146b94267a4aff7c8025b4a21c0
1 /*
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 <linux/slab.h>
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "driver-ops.h"
21 #include "debugfs_key.h"
22 #include "aes_ccm.h"
23 #include "aes_cmac.h"
26 /**
27 * DOC: Key handling basics
29 * Key handling in mac80211 is done based on per-interface (sub_if_data)
30 * keys and per-station keys. Since each station belongs to an interface,
31 * each station key also belongs to that interface.
33 * Hardware acceleration is done on a best-effort basis, for each key
34 * that is eligible the hardware is asked to enable that key but if
35 * it cannot do that they key is simply kept for software encryption.
36 * There is currently no way of knowing this except by looking into
37 * debugfs.
39 * All key operations are protected internally so you can call them at
40 * any time.
42 * Within mac80211, key references are, just as STA structure references,
43 * protected by RCU. Note, however, that some things are unprotected,
44 * namely the key->sta dereferences within the hardware acceleration
45 * functions. This means that sta_info_destroy() must flush the key todo
46 * list.
48 * All the direct key list manipulation functions must not sleep because
49 * they can operate on STA info structs that are protected by RCU.
52 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
54 /* key mutex: used to synchronise todo runners */
55 static DEFINE_MUTEX(key_mutex);
56 static DEFINE_SPINLOCK(todo_lock);
57 static LIST_HEAD(todo_list);
59 static void key_todo(struct work_struct *work)
61 ieee80211_key_todo();
64 static DECLARE_WORK(todo_work, key_todo);
66 /**
67 * add_todo - add todo item for a key
69 * @key: key to add to do item for
70 * @flag: todo flag(s)
72 * Must be called with IRQs or softirqs disabled.
74 static void add_todo(struct ieee80211_key *key, u32 flag)
76 if (!key)
77 return;
79 spin_lock(&todo_lock);
80 key->flags |= flag;
82 * Remove again if already on the list so that we move it to the end.
84 if (!list_empty(&key->todo))
85 list_del(&key->todo);
86 list_add_tail(&key->todo, &todo_list);
87 schedule_work(&todo_work);
88 spin_unlock(&todo_lock);
91 /**
92 * ieee80211_key_lock - lock the mac80211 key operation lock
94 * This locks the (global) mac80211 key operation lock, all
95 * key operations must be done under this lock.
97 static void ieee80211_key_lock(void)
99 mutex_lock(&key_mutex);
103 * ieee80211_key_unlock - unlock the mac80211 key operation lock
105 static void ieee80211_key_unlock(void)
107 mutex_unlock(&key_mutex);
110 static void assert_key_lock(void)
112 WARN_ON(!mutex_is_locked(&key_mutex));
115 static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
117 if (key->sta)
118 return &key->sta->sta;
120 return NULL;
123 static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
125 struct ieee80211_sub_if_data *sdata;
126 struct ieee80211_sta *sta;
127 int ret;
129 assert_key_lock();
130 might_sleep();
132 if (!key->local->ops->set_key)
133 return;
135 sta = get_sta_for_key(key);
137 sdata = key->sdata;
138 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
139 sdata = container_of(sdata->bss,
140 struct ieee80211_sub_if_data,
141 u.ap);
143 key->conf.ap_addr = sdata->dev->dev_addr;
144 ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
146 if (!ret) {
147 spin_lock_bh(&todo_lock);
148 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
149 spin_unlock_bh(&todo_lock);
152 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
153 printk(KERN_ERR "mac80211-%s: failed to set key "
154 "(%d, %pM) to hardware (%d)\n",
155 wiphy_name(key->local->hw.wiphy),
156 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
159 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
161 struct ieee80211_sub_if_data *sdata;
162 struct ieee80211_sta *sta;
163 int ret;
165 assert_key_lock();
166 might_sleep();
168 if (!key || !key->local->ops->set_key)
169 return;
171 spin_lock_bh(&todo_lock);
172 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
173 spin_unlock_bh(&todo_lock);
174 return;
176 spin_unlock_bh(&todo_lock);
178 sta = get_sta_for_key(key);
179 sdata = key->sdata;
181 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
182 sdata = container_of(sdata->bss,
183 struct ieee80211_sub_if_data,
184 u.ap);
186 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
187 sta, &key->conf);
189 if (ret)
190 printk(KERN_ERR "mac80211-%s: failed to remove key "
191 "(%d, %pM) from hardware (%d)\n",
192 wiphy_name(key->local->hw.wiphy),
193 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
195 spin_lock_bh(&todo_lock);
196 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
197 spin_unlock_bh(&todo_lock);
200 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
201 int idx)
203 struct ieee80211_key *key = NULL;
205 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
206 key = sdata->keys[idx];
208 rcu_assign_pointer(sdata->default_key, key);
210 if (key)
211 add_todo(key, KEY_FLAG_TODO_DEFKEY);
214 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
216 unsigned long flags;
218 spin_lock_irqsave(&sdata->local->key_lock, flags);
219 __ieee80211_set_default_key(sdata, idx);
220 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
223 static void
224 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
226 struct ieee80211_key *key = NULL;
228 if (idx >= NUM_DEFAULT_KEYS &&
229 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
230 key = sdata->keys[idx];
232 rcu_assign_pointer(sdata->default_mgmt_key, key);
234 if (key)
235 add_todo(key, KEY_FLAG_TODO_DEFMGMTKEY);
238 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
239 int idx)
241 unsigned long flags;
243 spin_lock_irqsave(&sdata->local->key_lock, flags);
244 __ieee80211_set_default_mgmt_key(sdata, idx);
245 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
249 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
250 struct sta_info *sta,
251 struct ieee80211_key *old,
252 struct ieee80211_key *new)
254 int idx, defkey, defmgmtkey;
256 if (new)
257 list_add(&new->list, &sdata->key_list);
259 if (sta) {
260 rcu_assign_pointer(sta->key, new);
261 } else {
262 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
264 if (old)
265 idx = old->conf.keyidx;
266 else
267 idx = new->conf.keyidx;
269 defkey = old && sdata->default_key == old;
270 defmgmtkey = old && sdata->default_mgmt_key == old;
272 if (defkey && !new)
273 __ieee80211_set_default_key(sdata, -1);
274 if (defmgmtkey && !new)
275 __ieee80211_set_default_mgmt_key(sdata, -1);
277 rcu_assign_pointer(sdata->keys[idx], new);
278 if (defkey && new)
279 __ieee80211_set_default_key(sdata, new->conf.keyidx);
280 if (defmgmtkey && new)
281 __ieee80211_set_default_mgmt_key(sdata,
282 new->conf.keyidx);
285 if (old) {
287 * We'll use an empty list to indicate that the key
288 * has already been removed.
290 list_del_init(&old->list);
294 struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
295 int idx,
296 size_t key_len,
297 const u8 *key_data,
298 size_t seq_len, const u8 *seq)
300 struct ieee80211_key *key;
301 int i, j;
303 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
305 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
306 if (!key)
307 return NULL;
310 * Default to software encryption; we'll later upload the
311 * key to the hardware if possible.
313 key->conf.flags = 0;
314 key->flags = 0;
316 key->conf.alg = alg;
317 key->conf.keyidx = idx;
318 key->conf.keylen = key_len;
319 switch (alg) {
320 case ALG_WEP:
321 key->conf.iv_len = WEP_IV_LEN;
322 key->conf.icv_len = WEP_ICV_LEN;
323 break;
324 case ALG_TKIP:
325 key->conf.iv_len = TKIP_IV_LEN;
326 key->conf.icv_len = TKIP_ICV_LEN;
327 if (seq) {
328 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
329 key->u.tkip.rx[i].iv32 =
330 get_unaligned_le32(&seq[2]);
331 key->u.tkip.rx[i].iv16 =
332 get_unaligned_le16(seq);
335 break;
336 case ALG_CCMP:
337 key->conf.iv_len = CCMP_HDR_LEN;
338 key->conf.icv_len = CCMP_MIC_LEN;
339 if (seq) {
340 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
341 for (j = 0; j < CCMP_PN_LEN; j++)
342 key->u.ccmp.rx_pn[i][j] =
343 seq[CCMP_PN_LEN - j - 1];
345 break;
346 case ALG_AES_CMAC:
347 key->conf.iv_len = 0;
348 key->conf.icv_len = sizeof(struct ieee80211_mmie);
349 if (seq)
350 for (j = 0; j < 6; j++)
351 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
352 break;
354 memcpy(key->conf.key, key_data, key_len);
355 INIT_LIST_HEAD(&key->list);
356 INIT_LIST_HEAD(&key->todo);
358 if (alg == ALG_CCMP) {
360 * Initialize AES key state here as an optimization so that
361 * it does not need to be initialized for every packet.
363 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
364 if (!key->u.ccmp.tfm) {
365 kfree(key);
366 return NULL;
370 if (alg == ALG_AES_CMAC) {
372 * Initialize AES key state here as an optimization so that
373 * it does not need to be initialized for every packet.
375 key->u.aes_cmac.tfm =
376 ieee80211_aes_cmac_key_setup(key_data);
377 if (!key->u.aes_cmac.tfm) {
378 kfree(key);
379 return NULL;
383 return key;
386 void ieee80211_key_link(struct ieee80211_key *key,
387 struct ieee80211_sub_if_data *sdata,
388 struct sta_info *sta)
390 struct ieee80211_key *old_key;
391 unsigned long flags;
392 int idx;
394 BUG_ON(!sdata);
395 BUG_ON(!key);
397 idx = key->conf.keyidx;
398 key->local = sdata->local;
399 key->sdata = sdata;
400 key->sta = sta;
402 if (sta) {
404 * some hardware cannot handle TKIP with QoS, so
405 * we indicate whether QoS could be in use.
407 if (test_sta_flags(sta, WLAN_STA_WME))
408 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
411 * This key is for a specific sta interface,
412 * inform the driver that it should try to store
413 * this key as pairwise key.
415 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
416 } else {
417 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
418 struct sta_info *ap;
421 * We're getting a sta pointer in,
422 * so must be under RCU read lock.
425 /* same here, the AP could be using QoS */
426 ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
427 if (ap) {
428 if (test_sta_flags(ap, WLAN_STA_WME))
429 key->conf.flags |=
430 IEEE80211_KEY_FLAG_WMM_STA;
435 spin_lock_irqsave(&sdata->local->key_lock, flags);
437 if (sta)
438 old_key = sta->key;
439 else
440 old_key = sdata->keys[idx];
442 __ieee80211_key_replace(sdata, sta, old_key, key);
444 /* free old key later */
445 add_todo(old_key, KEY_FLAG_TODO_DELETE);
447 add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
448 if (ieee80211_sdata_running(sdata))
449 add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
451 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
454 static void __ieee80211_key_free(struct ieee80211_key *key)
457 * Replace key with nothingness if it was ever used.
459 if (key->sdata)
460 __ieee80211_key_replace(key->sdata, key->sta,
461 key, NULL);
463 add_todo(key, KEY_FLAG_TODO_DELETE);
466 void ieee80211_key_free(struct ieee80211_key *key)
468 unsigned long flags;
470 if (!key)
471 return;
473 if (!key->sdata) {
474 /* The key has not been linked yet, simply free it
475 * and don't Oops */
476 if (key->conf.alg == ALG_CCMP)
477 ieee80211_aes_key_free(key->u.ccmp.tfm);
478 kfree(key);
479 return;
482 spin_lock_irqsave(&key->sdata->local->key_lock, flags);
483 __ieee80211_key_free(key);
484 spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
488 * To be safe against concurrent manipulations of the list (which shouldn't
489 * actually happen) we need to hold the spinlock. But under the spinlock we
490 * can't actually do much, so we defer processing to the todo list. Then run
491 * the todo list to be sure the operation and possibly previously pending
492 * operations are completed.
494 static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
495 u32 todo_flags)
497 struct ieee80211_key *key;
498 unsigned long flags;
500 might_sleep();
502 spin_lock_irqsave(&sdata->local->key_lock, flags);
503 list_for_each_entry(key, &sdata->key_list, list)
504 add_todo(key, todo_flags);
505 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
507 ieee80211_key_todo();
510 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
512 ASSERT_RTNL();
514 if (WARN_ON(!ieee80211_sdata_running(sdata)))
515 return;
517 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
520 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
522 ASSERT_RTNL();
524 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
527 static void __ieee80211_key_destroy(struct ieee80211_key *key)
529 if (!key)
530 return;
532 ieee80211_key_disable_hw_accel(key);
534 if (key->conf.alg == ALG_CCMP)
535 ieee80211_aes_key_free(key->u.ccmp.tfm);
536 if (key->conf.alg == ALG_AES_CMAC)
537 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
538 ieee80211_debugfs_key_remove(key);
540 kfree(key);
543 static void __ieee80211_key_todo(void)
545 struct ieee80211_key *key;
546 bool work_done;
547 u32 todoflags;
550 * NB: sta_info_destroy relies on this!
552 synchronize_rcu();
554 spin_lock_bh(&todo_lock);
555 while (!list_empty(&todo_list)) {
556 key = list_first_entry(&todo_list, struct ieee80211_key, todo);
557 list_del_init(&key->todo);
558 todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
559 KEY_FLAG_TODO_DEFKEY |
560 KEY_FLAG_TODO_DEFMGMTKEY |
561 KEY_FLAG_TODO_HWACCEL_ADD |
562 KEY_FLAG_TODO_HWACCEL_REMOVE |
563 KEY_FLAG_TODO_DELETE);
564 key->flags &= ~todoflags;
565 spin_unlock_bh(&todo_lock);
567 work_done = false;
569 if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
570 ieee80211_debugfs_key_add(key);
571 work_done = true;
573 if (todoflags & KEY_FLAG_TODO_DEFKEY) {
574 ieee80211_debugfs_key_remove_default(key->sdata);
575 ieee80211_debugfs_key_add_default(key->sdata);
576 work_done = true;
578 if (todoflags & KEY_FLAG_TODO_DEFMGMTKEY) {
579 ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
580 ieee80211_debugfs_key_add_mgmt_default(key->sdata);
581 work_done = true;
583 if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
584 ieee80211_key_enable_hw_accel(key);
585 work_done = true;
587 if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
588 ieee80211_key_disable_hw_accel(key);
589 work_done = true;
591 if (todoflags & KEY_FLAG_TODO_DELETE) {
592 __ieee80211_key_destroy(key);
593 work_done = true;
596 WARN_ON(!work_done);
598 spin_lock_bh(&todo_lock);
600 spin_unlock_bh(&todo_lock);
603 void ieee80211_key_todo(void)
605 ieee80211_key_lock();
606 __ieee80211_key_todo();
607 ieee80211_key_unlock();
610 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
612 struct ieee80211_key *key, *tmp;
613 unsigned long flags;
615 ieee80211_key_lock();
617 ieee80211_debugfs_key_remove_default(sdata);
618 ieee80211_debugfs_key_remove_mgmt_default(sdata);
620 spin_lock_irqsave(&sdata->local->key_lock, flags);
621 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
622 __ieee80211_key_free(key);
623 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
625 __ieee80211_key_todo();
627 ieee80211_key_unlock();