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/errno.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <net/ieee80211.h>
22 MODULE_AUTHOR("Jouni Malinen");
23 MODULE_DESCRIPTION("HostAP crypto");
24 MODULE_LICENSE("GPL");
26 struct ieee80211_crypto_alg
{
27 struct list_head list
;
28 struct ieee80211_crypto_ops
*ops
;
31 static LIST_HEAD(ieee80211_crypto_algs
);
32 static DEFINE_SPINLOCK(ieee80211_crypto_lock
);
34 void ieee80211_crypt_deinit_entries(struct ieee80211_device
*ieee
, int force
)
36 struct ieee80211_crypt_data
*entry
, *next
;
39 spin_lock_irqsave(&ieee
->lock
, flags
);
40 list_for_each_entry_safe(entry
, next
, &ieee
->crypt_deinit_list
, list
) {
41 if (atomic_read(&entry
->refcnt
) != 0 && !force
)
44 list_del(&entry
->list
);
47 entry
->ops
->deinit(entry
->priv
);
48 module_put(entry
->ops
->owner
);
52 spin_unlock_irqrestore(&ieee
->lock
, flags
);
55 /* After this, crypt_deinit_list won't accept new members */
56 void ieee80211_crypt_quiescing(struct ieee80211_device
*ieee
)
60 spin_lock_irqsave(&ieee
->lock
, flags
);
61 ieee
->crypt_quiesced
= 1;
62 spin_unlock_irqrestore(&ieee
->lock
, flags
);
65 void ieee80211_crypt_deinit_handler(unsigned long data
)
67 struct ieee80211_device
*ieee
= (struct ieee80211_device
*)data
;
70 ieee80211_crypt_deinit_entries(ieee
, 0);
72 spin_lock_irqsave(&ieee
->lock
, flags
);
73 if (!list_empty(&ieee
->crypt_deinit_list
) && !ieee
->crypt_quiesced
) {
74 printk(KERN_DEBUG
"%s: entries remaining in delayed crypt "
75 "deletion list\n", ieee
->dev
->name
);
76 ieee
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
77 add_timer(&ieee
->crypt_deinit_timer
);
79 spin_unlock_irqrestore(&ieee
->lock
, flags
);
82 void ieee80211_crypt_delayed_deinit(struct ieee80211_device
*ieee
,
83 struct ieee80211_crypt_data
**crypt
)
85 struct ieee80211_crypt_data
*tmp
;
94 /* must not run ops->deinit() while there may be pending encrypt or
95 * decrypt operations. Use a list of delayed deinits to avoid needing
98 spin_lock_irqsave(&ieee
->lock
, flags
);
99 if (!ieee
->crypt_quiesced
) {
100 list_add(&tmp
->list
, &ieee
->crypt_deinit_list
);
101 if (!timer_pending(&ieee
->crypt_deinit_timer
)) {
102 ieee
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
103 add_timer(&ieee
->crypt_deinit_timer
);
106 spin_unlock_irqrestore(&ieee
->lock
, flags
);
109 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops
*ops
)
112 struct ieee80211_crypto_alg
*alg
;
114 alg
= kmalloc(sizeof(*alg
), GFP_KERNEL
);
118 memset(alg
, 0, sizeof(*alg
));
121 spin_lock_irqsave(&ieee80211_crypto_lock
, flags
);
122 list_add(&alg
->list
, &ieee80211_crypto_algs
);
123 spin_unlock_irqrestore(&ieee80211_crypto_lock
, flags
);
125 printk(KERN_DEBUG
"ieee80211_crypt: registered algorithm '%s'\n",
131 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops
*ops
)
133 struct ieee80211_crypto_alg
*alg
;
136 spin_lock_irqsave(&ieee80211_crypto_lock
, flags
);
137 list_for_each_entry(alg
, &ieee80211_crypto_algs
, list
) {
141 spin_unlock_irqrestore(&ieee80211_crypto_lock
, flags
);
145 printk(KERN_DEBUG
"ieee80211_crypt: unregistered algorithm "
146 "'%s'\n", ops
->name
);
147 list_del(&alg
->list
);
148 spin_unlock_irqrestore(&ieee80211_crypto_lock
, flags
);
153 struct ieee80211_crypto_ops
*ieee80211_get_crypto_ops(const char *name
)
155 struct ieee80211_crypto_alg
*alg
;
158 spin_lock_irqsave(&ieee80211_crypto_lock
, flags
);
159 list_for_each_entry(alg
, &ieee80211_crypto_algs
, list
) {
160 if (strcmp(alg
->ops
->name
, name
) == 0)
163 spin_unlock_irqrestore(&ieee80211_crypto_lock
, flags
);
167 spin_unlock_irqrestore(&ieee80211_crypto_lock
, flags
);
171 static void *ieee80211_crypt_null_init(int keyidx
)
176 static void ieee80211_crypt_null_deinit(void *priv
)
180 static struct ieee80211_crypto_ops ieee80211_crypt_null
= {
182 .init
= ieee80211_crypt_null_init
,
183 .deinit
= ieee80211_crypt_null_deinit
,
184 .owner
= THIS_MODULE
,
187 static int __init
ieee80211_crypto_init(void)
189 return ieee80211_register_crypto_ops(&ieee80211_crypt_null
);
192 static void __exit
ieee80211_crypto_deinit(void)
194 ieee80211_unregister_crypto_ops(&ieee80211_crypt_null
);
195 BUG_ON(!list_empty(&ieee80211_crypto_algs
));
198 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries
);
199 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler
);
200 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit
);
201 EXPORT_SYMBOL(ieee80211_crypt_quiescing
);
203 EXPORT_SYMBOL(ieee80211_register_crypto_ops
);
204 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops
);
205 EXPORT_SYMBOL(ieee80211_get_crypto_ops
);
207 module_init(ieee80211_crypto_init
);
208 module_exit(ieee80211_crypto_deinit
);