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 ret
= drv_set_key(key
->local
, SET_KEY
, sdata
, sta
, &key
->conf
);
146 spin_lock_bh(&todo_lock
);
147 key
->flags
|= KEY_FLAG_UPLOADED_TO_HARDWARE
;
148 spin_unlock_bh(&todo_lock
);
151 if (ret
&& ret
!= -ENOSPC
&& ret
!= -EOPNOTSUPP
)
152 printk(KERN_ERR
"mac80211-%s: failed to set key "
153 "(%d, %pM) to hardware (%d)\n",
154 wiphy_name(key
->local
->hw
.wiphy
),
155 key
->conf
.keyidx
, sta
? sta
->addr
: bcast_addr
, ret
);
158 static void ieee80211_key_disable_hw_accel(struct ieee80211_key
*key
)
160 struct ieee80211_sub_if_data
*sdata
;
161 struct ieee80211_sta
*sta
;
167 if (!key
|| !key
->local
->ops
->set_key
)
170 spin_lock_bh(&todo_lock
);
171 if (!(key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
)) {
172 spin_unlock_bh(&todo_lock
);
175 spin_unlock_bh(&todo_lock
);
177 sta
= get_sta_for_key(key
);
180 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
181 sdata
= container_of(sdata
->bss
,
182 struct ieee80211_sub_if_data
,
185 ret
= drv_set_key(key
->local
, DISABLE_KEY
, sdata
,
189 printk(KERN_ERR
"mac80211-%s: failed to remove key "
190 "(%d, %pM) from hardware (%d)\n",
191 wiphy_name(key
->local
->hw
.wiphy
),
192 key
->conf
.keyidx
, sta
? sta
->addr
: bcast_addr
, ret
);
194 spin_lock_bh(&todo_lock
);
195 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
196 spin_unlock_bh(&todo_lock
);
199 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
,
202 struct ieee80211_key
*key
= NULL
;
204 if (idx
>= 0 && idx
< NUM_DEFAULT_KEYS
)
205 key
= sdata
->keys
[idx
];
207 rcu_assign_pointer(sdata
->default_key
, key
);
210 add_todo(key
, KEY_FLAG_TODO_DEFKEY
);
213 void ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
217 spin_lock_irqsave(&sdata
->local
->key_lock
, flags
);
218 __ieee80211_set_default_key(sdata
, idx
);
219 spin_unlock_irqrestore(&sdata
->local
->key_lock
, flags
);
223 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
225 struct ieee80211_key
*key
= NULL
;
227 if (idx
>= NUM_DEFAULT_KEYS
&&
228 idx
< NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
)
229 key
= sdata
->keys
[idx
];
231 rcu_assign_pointer(sdata
->default_mgmt_key
, key
);
234 add_todo(key
, KEY_FLAG_TODO_DEFMGMTKEY
);
237 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
,
242 spin_lock_irqsave(&sdata
->local
->key_lock
, flags
);
243 __ieee80211_set_default_mgmt_key(sdata
, idx
);
244 spin_unlock_irqrestore(&sdata
->local
->key_lock
, flags
);
248 static void __ieee80211_key_replace(struct ieee80211_sub_if_data
*sdata
,
249 struct sta_info
*sta
,
250 struct ieee80211_key
*old
,
251 struct ieee80211_key
*new)
253 int idx
, defkey
, defmgmtkey
;
256 list_add(&new->list
, &sdata
->key_list
);
259 rcu_assign_pointer(sta
->key
, new);
261 WARN_ON(new && old
&& new->conf
.keyidx
!= old
->conf
.keyidx
);
264 idx
= old
->conf
.keyidx
;
266 idx
= new->conf
.keyidx
;
268 defkey
= old
&& sdata
->default_key
== old
;
269 defmgmtkey
= old
&& sdata
->default_mgmt_key
== old
;
272 __ieee80211_set_default_key(sdata
, -1);
273 if (defmgmtkey
&& !new)
274 __ieee80211_set_default_mgmt_key(sdata
, -1);
276 rcu_assign_pointer(sdata
->keys
[idx
], new);
278 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
);
279 if (defmgmtkey
&& new)
280 __ieee80211_set_default_mgmt_key(sdata
,
286 * We'll use an empty list to indicate that the key
287 * has already been removed.
289 list_del_init(&old
->list
);
293 struct ieee80211_key
*ieee80211_key_alloc(enum ieee80211_key_alg alg
,
297 size_t seq_len
, const u8
*seq
)
299 struct ieee80211_key
*key
;
302 BUG_ON(idx
< 0 || idx
>= NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
);
304 key
= kzalloc(sizeof(struct ieee80211_key
) + key_len
, GFP_KERNEL
);
309 * Default to software encryption; we'll later upload the
310 * key to the hardware if possible.
316 key
->conf
.keyidx
= idx
;
317 key
->conf
.keylen
= key_len
;
320 key
->conf
.iv_len
= WEP_IV_LEN
;
321 key
->conf
.icv_len
= WEP_ICV_LEN
;
324 key
->conf
.iv_len
= TKIP_IV_LEN
;
325 key
->conf
.icv_len
= TKIP_ICV_LEN
;
327 for (i
= 0; i
< NUM_RX_DATA_QUEUES
; i
++) {
328 key
->u
.tkip
.rx
[i
].iv32
=
329 get_unaligned_le32(&seq
[2]);
330 key
->u
.tkip
.rx
[i
].iv16
=
331 get_unaligned_le16(seq
);
336 key
->conf
.iv_len
= CCMP_HDR_LEN
;
337 key
->conf
.icv_len
= CCMP_MIC_LEN
;
339 for (i
= 0; i
< NUM_RX_DATA_QUEUES
; i
++)
340 for (j
= 0; j
< CCMP_PN_LEN
; j
++)
341 key
->u
.ccmp
.rx_pn
[i
][j
] =
342 seq
[CCMP_PN_LEN
- j
- 1];
346 key
->conf
.iv_len
= 0;
347 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie
);
349 for (j
= 0; j
< 6; j
++)
350 key
->u
.aes_cmac
.rx_pn
[j
] = seq
[6 - j
- 1];
353 memcpy(key
->conf
.key
, key_data
, key_len
);
354 INIT_LIST_HEAD(&key
->list
);
355 INIT_LIST_HEAD(&key
->todo
);
357 if (alg
== ALG_CCMP
) {
359 * Initialize AES key state here as an optimization so that
360 * it does not need to be initialized for every packet.
362 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(key_data
);
363 if (!key
->u
.ccmp
.tfm
) {
369 if (alg
== ALG_AES_CMAC
) {
371 * Initialize AES key state here as an optimization so that
372 * it does not need to be initialized for every packet.
374 key
->u
.aes_cmac
.tfm
=
375 ieee80211_aes_cmac_key_setup(key_data
);
376 if (!key
->u
.aes_cmac
.tfm
) {
385 void ieee80211_key_link(struct ieee80211_key
*key
,
386 struct ieee80211_sub_if_data
*sdata
,
387 struct sta_info
*sta
)
389 struct ieee80211_key
*old_key
;
396 idx
= key
->conf
.keyidx
;
397 key
->local
= sdata
->local
;
403 * some hardware cannot handle TKIP with QoS, so
404 * we indicate whether QoS could be in use.
406 if (test_sta_flags(sta
, WLAN_STA_WME
))
407 key
->conf
.flags
|= IEEE80211_KEY_FLAG_WMM_STA
;
410 * This key is for a specific sta interface,
411 * inform the driver that it should try to store
412 * this key as pairwise key.
414 key
->conf
.flags
|= IEEE80211_KEY_FLAG_PAIRWISE
;
416 if (sdata
->vif
.type
== NL80211_IFTYPE_STATION
) {
420 * We're getting a sta pointer in,
421 * so must be under RCU read lock.
424 /* same here, the AP could be using QoS */
425 ap
= sta_info_get(key
->sdata
, key
->sdata
->u
.mgd
.bssid
);
427 if (test_sta_flags(ap
, WLAN_STA_WME
))
429 IEEE80211_KEY_FLAG_WMM_STA
;
434 spin_lock_irqsave(&sdata
->local
->key_lock
, flags
);
439 old_key
= sdata
->keys
[idx
];
441 __ieee80211_key_replace(sdata
, sta
, old_key
, key
);
443 /* free old key later */
444 add_todo(old_key
, KEY_FLAG_TODO_DELETE
);
446 add_todo(key
, KEY_FLAG_TODO_ADD_DEBUGFS
);
447 if (ieee80211_sdata_running(sdata
))
448 add_todo(key
, KEY_FLAG_TODO_HWACCEL_ADD
);
450 spin_unlock_irqrestore(&sdata
->local
->key_lock
, flags
);
453 static void __ieee80211_key_free(struct ieee80211_key
*key
)
456 * Replace key with nothingness if it was ever used.
459 __ieee80211_key_replace(key
->sdata
, key
->sta
,
462 add_todo(key
, KEY_FLAG_TODO_DELETE
);
465 void ieee80211_key_free(struct ieee80211_key
*key
)
473 /* The key has not been linked yet, simply free it
475 if (key
->conf
.alg
== ALG_CCMP
)
476 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
481 spin_lock_irqsave(&key
->sdata
->local
->key_lock
, flags
);
482 __ieee80211_key_free(key
);
483 spin_unlock_irqrestore(&key
->sdata
->local
->key_lock
, flags
);
487 * To be safe against concurrent manipulations of the list (which shouldn't
488 * actually happen) we need to hold the spinlock. But under the spinlock we
489 * can't actually do much, so we defer processing to the todo list. Then run
490 * the todo list to be sure the operation and possibly previously pending
491 * operations are completed.
493 static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data
*sdata
,
496 struct ieee80211_key
*key
;
501 spin_lock_irqsave(&sdata
->local
->key_lock
, flags
);
502 list_for_each_entry(key
, &sdata
->key_list
, list
)
503 add_todo(key
, todo_flags
);
504 spin_unlock_irqrestore(&sdata
->local
->key_lock
, flags
);
506 ieee80211_key_todo();
509 void ieee80211_enable_keys(struct ieee80211_sub_if_data
*sdata
)
513 if (WARN_ON(!ieee80211_sdata_running(sdata
)))
516 ieee80211_todo_for_each_key(sdata
, KEY_FLAG_TODO_HWACCEL_ADD
);
519 void ieee80211_disable_keys(struct ieee80211_sub_if_data
*sdata
)
523 ieee80211_todo_for_each_key(sdata
, KEY_FLAG_TODO_HWACCEL_REMOVE
);
526 static void __ieee80211_key_destroy(struct ieee80211_key
*key
)
531 ieee80211_key_disable_hw_accel(key
);
533 if (key
->conf
.alg
== ALG_CCMP
)
534 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
535 if (key
->conf
.alg
== ALG_AES_CMAC
)
536 ieee80211_aes_cmac_key_free(key
->u
.aes_cmac
.tfm
);
537 ieee80211_debugfs_key_remove(key
);
542 static void __ieee80211_key_todo(void)
544 struct ieee80211_key
*key
;
549 * NB: sta_info_destroy relies on this!
553 spin_lock_bh(&todo_lock
);
554 while (!list_empty(&todo_list
)) {
555 key
= list_first_entry(&todo_list
, struct ieee80211_key
, todo
);
556 list_del_init(&key
->todo
);
557 todoflags
= key
->flags
& (KEY_FLAG_TODO_ADD_DEBUGFS
|
558 KEY_FLAG_TODO_DEFKEY
|
559 KEY_FLAG_TODO_DEFMGMTKEY
|
560 KEY_FLAG_TODO_HWACCEL_ADD
|
561 KEY_FLAG_TODO_HWACCEL_REMOVE
|
562 KEY_FLAG_TODO_DELETE
);
563 key
->flags
&= ~todoflags
;
564 spin_unlock_bh(&todo_lock
);
568 if (todoflags
& KEY_FLAG_TODO_ADD_DEBUGFS
) {
569 ieee80211_debugfs_key_add(key
);
572 if (todoflags
& KEY_FLAG_TODO_DEFKEY
) {
573 ieee80211_debugfs_key_remove_default(key
->sdata
);
574 ieee80211_debugfs_key_add_default(key
->sdata
);
577 if (todoflags
& KEY_FLAG_TODO_DEFMGMTKEY
) {
578 ieee80211_debugfs_key_remove_mgmt_default(key
->sdata
);
579 ieee80211_debugfs_key_add_mgmt_default(key
->sdata
);
582 if (todoflags
& KEY_FLAG_TODO_HWACCEL_ADD
) {
583 ieee80211_key_enable_hw_accel(key
);
586 if (todoflags
& KEY_FLAG_TODO_HWACCEL_REMOVE
) {
587 ieee80211_key_disable_hw_accel(key
);
590 if (todoflags
& KEY_FLAG_TODO_DELETE
) {
591 __ieee80211_key_destroy(key
);
597 spin_lock_bh(&todo_lock
);
599 spin_unlock_bh(&todo_lock
);
602 void ieee80211_key_todo(void)
604 ieee80211_key_lock();
605 __ieee80211_key_todo();
606 ieee80211_key_unlock();
609 void ieee80211_free_keys(struct ieee80211_sub_if_data
*sdata
)
611 struct ieee80211_key
*key
, *tmp
;
614 ieee80211_key_lock();
616 ieee80211_debugfs_key_remove_default(sdata
);
617 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
619 spin_lock_irqsave(&sdata
->local
->key_lock
, flags
);
620 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
)
621 __ieee80211_key_free(key
);
622 spin_unlock_irqrestore(&sdata
->local
->key_lock
, flags
);
624 __ieee80211_key_todo();
626 ieee80211_key_unlock();