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"
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
39 * All key operations are protected internally so you can call them at
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
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
)
64 static DECLARE_WORK(todo_work
, key_todo
);
67 * add_todo - add todo item for a key
69 * @key: key to add to do item for
72 * Must be called with IRQs or softirqs disabled.
74 static void add_todo(struct ieee80211_key
*key
, u32 flag
)
79 spin_lock(&todo_lock
);
82 * Remove again if already on the list so that we move it to the end.
84 if (!list_empty(&key
->todo
))
86 list_add_tail(&key
->todo
, &todo_list
);
87 schedule_work(&todo_work
);
88 spin_unlock(&todo_lock
);
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
)
118 return &key
->sta
->sta
;
123 static void ieee80211_key_enable_hw_accel(struct ieee80211_key
*key
)
125 struct ieee80211_sub_if_data
*sdata
;
126 struct ieee80211_sta
*sta
;
132 if (!key
->local
->ops
->set_key
)
135 sta
= get_sta_for_key(key
);
138 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
139 sdata
= container_of(sdata
->bss
,
140 struct ieee80211_sub_if_data
,
143 key
->conf
.ap_addr
= sdata
->dev
->dev_addr
;
144 ret
= drv_set_key(key
->local
, SET_KEY
, sdata
, sta
, &key
->conf
);
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
;
168 if (!key
|| !key
->local
->ops
->set_key
)
171 spin_lock_bh(&todo_lock
);
172 if (!(key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
)) {
173 spin_unlock_bh(&todo_lock
);
176 spin_unlock_bh(&todo_lock
);
178 sta
= get_sta_for_key(key
);
181 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
182 sdata
= container_of(sdata
->bss
,
183 struct ieee80211_sub_if_data
,
186 ret
= drv_set_key(key
->local
, DISABLE_KEY
, sdata
,
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
,
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
);
211 add_todo(key
, KEY_FLAG_TODO_DEFKEY
);
214 void ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
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
);
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
);
235 add_todo(key
, KEY_FLAG_TODO_DEFMGMTKEY
);
238 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
,
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
;
257 list_add(&new->list
, &sdata
->key_list
);
260 rcu_assign_pointer(sta
->key
, new);
262 WARN_ON(new && old
&& new->conf
.keyidx
!= old
->conf
.keyidx
);
265 idx
= old
->conf
.keyidx
;
267 idx
= new->conf
.keyidx
;
269 defkey
= old
&& sdata
->default_key
== old
;
270 defmgmtkey
= old
&& sdata
->default_mgmt_key
== old
;
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);
279 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
);
280 if (defmgmtkey
&& new)
281 __ieee80211_set_default_mgmt_key(sdata
,
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
,
298 size_t seq_len
, const u8
*seq
)
300 struct ieee80211_key
*key
;
303 BUG_ON(idx
< 0 || idx
>= NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
);
305 key
= kzalloc(sizeof(struct ieee80211_key
) + key_len
, GFP_KERNEL
);
310 * Default to software encryption; we'll later upload the
311 * key to the hardware if possible.
317 key
->conf
.keyidx
= idx
;
318 key
->conf
.keylen
= key_len
;
321 key
->conf
.iv_len
= WEP_IV_LEN
;
322 key
->conf
.icv_len
= WEP_ICV_LEN
;
325 key
->conf
.iv_len
= TKIP_IV_LEN
;
326 key
->conf
.icv_len
= TKIP_ICV_LEN
;
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
);
337 key
->conf
.iv_len
= CCMP_HDR_LEN
;
338 key
->conf
.icv_len
= CCMP_MIC_LEN
;
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];
347 key
->conf
.iv_len
= 0;
348 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie
);
350 for (j
= 0; j
< 6; j
++)
351 key
->u
.aes_cmac
.rx_pn
[j
] = seq
[6 - j
- 1];
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
) {
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
) {
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
;
397 idx
= key
->conf
.keyidx
;
398 key
->local
= sdata
->local
;
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
;
417 if (sdata
->vif
.type
== NL80211_IFTYPE_STATION
) {
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
);
428 if (test_sta_flags(ap
, WLAN_STA_WME
))
430 IEEE80211_KEY_FLAG_WMM_STA
;
435 spin_lock_irqsave(&sdata
->local
->key_lock
, flags
);
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.
460 __ieee80211_key_replace(key
->sdata
, key
->sta
,
463 add_todo(key
, KEY_FLAG_TODO_DELETE
);
466 void ieee80211_key_free(struct ieee80211_key
*key
)
474 /* The key has not been linked yet, simply free it
476 if (key
->conf
.alg
== ALG_CCMP
)
477 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
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
,
497 struct ieee80211_key
*key
;
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
)
514 if (WARN_ON(!ieee80211_sdata_running(sdata
)))
517 ieee80211_todo_for_each_key(sdata
, KEY_FLAG_TODO_HWACCEL_ADD
);
520 void ieee80211_disable_keys(struct ieee80211_sub_if_data
*sdata
)
524 ieee80211_todo_for_each_key(sdata
, KEY_FLAG_TODO_HWACCEL_REMOVE
);
527 static void __ieee80211_key_destroy(struct ieee80211_key
*key
)
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
);
543 static void __ieee80211_key_todo(void)
545 struct ieee80211_key
*key
;
550 * NB: sta_info_destroy relies on this!
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
);
569 if (todoflags
& KEY_FLAG_TODO_ADD_DEBUGFS
) {
570 ieee80211_debugfs_key_add(key
);
573 if (todoflags
& KEY_FLAG_TODO_DEFKEY
) {
574 ieee80211_debugfs_key_remove_default(key
->sdata
);
575 ieee80211_debugfs_key_add_default(key
->sdata
);
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
);
583 if (todoflags
& KEY_FLAG_TODO_HWACCEL_ADD
) {
584 ieee80211_key_enable_hw_accel(key
);
587 if (todoflags
& KEY_FLAG_TODO_HWACCEL_REMOVE
) {
588 ieee80211_key_disable_hw_accel(key
);
591 if (todoflags
& KEY_FLAG_TODO_DELETE
) {
592 __ieee80211_key_destroy(key
);
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
;
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();