2 * Host AP crypto routines
4 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
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. See README and COPYING for
14 #include <linux/config.h>
15 #include <linux/version.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <asm/string.h>
20 #include <asm/errno.h>
22 #include <net/ieee80211.h>
24 MODULE_AUTHOR("Jouni Malinen");
25 MODULE_DESCRIPTION("HostAP crypto");
26 MODULE_LICENSE("GPL");
28 struct ieee80211_crypto_alg
{
29 struct list_head list
;
30 struct ieee80211_crypto_ops
*ops
;
33 struct ieee80211_crypto
{
34 struct list_head algs
;
38 static struct ieee80211_crypto
*hcrypt
;
40 void ieee80211_crypt_deinit_entries(struct ieee80211_device
*ieee
, int force
)
42 struct list_head
*ptr
, *n
;
43 struct ieee80211_crypt_data
*entry
;
45 for (ptr
= ieee
->crypt_deinit_list
.next
, n
= ptr
->next
;
46 ptr
!= &ieee
->crypt_deinit_list
; ptr
= n
, n
= ptr
->next
) {
47 entry
= list_entry(ptr
, struct ieee80211_crypt_data
, list
);
49 if (atomic_read(&entry
->refcnt
) != 0 && !force
)
55 entry
->ops
->deinit(entry
->priv
);
56 module_put(entry
->ops
->owner
);
62 void ieee80211_crypt_deinit_handler(unsigned long data
)
64 struct ieee80211_device
*ieee
= (struct ieee80211_device
*)data
;
67 spin_lock_irqsave(&ieee
->lock
, flags
);
68 ieee80211_crypt_deinit_entries(ieee
, 0);
69 if (!list_empty(&ieee
->crypt_deinit_list
)) {
70 printk(KERN_DEBUG
"%s: entries remaining in delayed crypt "
71 "deletion list\n", ieee
->dev
->name
);
72 ieee
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
73 add_timer(&ieee
->crypt_deinit_timer
);
75 spin_unlock_irqrestore(&ieee
->lock
, flags
);
79 void ieee80211_crypt_delayed_deinit(struct ieee80211_device
*ieee
,
80 struct ieee80211_crypt_data
**crypt
)
82 struct ieee80211_crypt_data
*tmp
;
91 /* must not run ops->deinit() while there may be pending encrypt or
92 * decrypt operations. Use a list of delayed deinits to avoid needing
95 spin_lock_irqsave(&ieee
->lock
, flags
);
96 list_add(&tmp
->list
, &ieee
->crypt_deinit_list
);
97 if (!timer_pending(&ieee
->crypt_deinit_timer
)) {
98 ieee
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
99 add_timer(&ieee
->crypt_deinit_timer
);
101 spin_unlock_irqrestore(&ieee
->lock
, flags
);
104 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops
*ops
)
107 struct ieee80211_crypto_alg
*alg
;
112 alg
= kmalloc(sizeof(*alg
), GFP_KERNEL
);
116 memset(alg
, 0, sizeof(*alg
));
119 spin_lock_irqsave(&hcrypt
->lock
, flags
);
120 list_add(&alg
->list
, &hcrypt
->algs
);
121 spin_unlock_irqrestore(&hcrypt
->lock
, flags
);
123 printk(KERN_DEBUG
"ieee80211_crypt: registered algorithm '%s'\n",
129 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops
*ops
)
132 struct list_head
*ptr
;
133 struct ieee80211_crypto_alg
*del_alg
= NULL
;
138 spin_lock_irqsave(&hcrypt
->lock
, flags
);
139 for (ptr
= hcrypt
->algs
.next
; ptr
!= &hcrypt
->algs
; ptr
= ptr
->next
) {
140 struct ieee80211_crypto_alg
*alg
=
141 (struct ieee80211_crypto_alg
*)ptr
;
142 if (alg
->ops
== ops
) {
143 list_del(&alg
->list
);
148 spin_unlock_irqrestore(&hcrypt
->lock
, flags
);
151 printk(KERN_DEBUG
"ieee80211_crypt: unregistered algorithm "
152 "'%s'\n", ops
->name
);
156 return del_alg
? 0 : -1;
159 struct ieee80211_crypto_ops
*ieee80211_get_crypto_ops(const char *name
)
162 struct list_head
*ptr
;
163 struct ieee80211_crypto_alg
*found_alg
= NULL
;
168 spin_lock_irqsave(&hcrypt
->lock
, flags
);
169 for (ptr
= hcrypt
->algs
.next
; ptr
!= &hcrypt
->algs
; ptr
= ptr
->next
) {
170 struct ieee80211_crypto_alg
*alg
=
171 (struct ieee80211_crypto_alg
*)ptr
;
172 if (strcmp(alg
->ops
->name
, name
) == 0) {
177 spin_unlock_irqrestore(&hcrypt
->lock
, flags
);
180 return found_alg
->ops
;
185 static void *ieee80211_crypt_null_init(int keyidx
)
189 static void ieee80211_crypt_null_deinit(void *priv
)
193 static struct ieee80211_crypto_ops ieee80211_crypt_null
= {
195 .init
= ieee80211_crypt_null_init
,
196 .deinit
= ieee80211_crypt_null_deinit
,
197 .encrypt_mpdu
= NULL
,
198 .decrypt_mpdu
= NULL
,
199 .encrypt_msdu
= NULL
,
200 .decrypt_msdu
= NULL
,
203 .extra_prefix_len
= 0,
204 .extra_postfix_len
= 0,
205 .owner
= THIS_MODULE
,
208 static int __init
ieee80211_crypto_init(void)
212 hcrypt
= kmalloc(sizeof(*hcrypt
), GFP_KERNEL
);
216 memset(hcrypt
, 0, sizeof(*hcrypt
));
217 INIT_LIST_HEAD(&hcrypt
->algs
);
218 spin_lock_init(&hcrypt
->lock
);
220 ret
= ieee80211_register_crypto_ops(&ieee80211_crypt_null
);
229 static void __exit
ieee80211_crypto_deinit(void)
231 struct list_head
*ptr
, *n
;
236 for (ptr
= hcrypt
->algs
.next
, n
= ptr
->next
; ptr
!= &hcrypt
->algs
;
237 ptr
= n
, n
= ptr
->next
) {
238 struct ieee80211_crypto_alg
*alg
=
239 (struct ieee80211_crypto_alg
*)ptr
;
241 printk(KERN_DEBUG
"ieee80211_crypt: unregistered algorithm "
242 "'%s' (deinit)\n", alg
->ops
->name
);
249 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries
);
250 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler
);
251 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit
);
253 EXPORT_SYMBOL(ieee80211_register_crypto_ops
);
254 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops
);
255 EXPORT_SYMBOL(ieee80211_get_crypto_ops
);
257 module_init(ieee80211_crypto_init
);
258 module_exit(ieee80211_crypto_deinit
);