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/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <asm/string.h>
19 #include <asm/errno.h>
21 #include <net/ieee80211.h>
23 MODULE_AUTHOR("Jouni Malinen");
24 MODULE_DESCRIPTION("HostAP crypto");
25 MODULE_LICENSE("GPL");
27 struct ieee80211_crypto_alg {
28 struct list_head list;
29 struct ieee80211_crypto_ops *ops;
32 struct ieee80211_crypto {
33 struct list_head algs;
37 static struct ieee80211_crypto *hcrypt;
39 void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force)
41 struct list_head *ptr, *n;
42 struct ieee80211_crypt_data *entry;
45 spin_lock_irqsave(&ieee->lock, flags);
47 if (list_empty(&ieee->crypt_deinit_list))
50 for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
51 ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
52 entry = list_entry(ptr, struct ieee80211_crypt_data, list);
54 if (atomic_read(&entry->refcnt) != 0 && !force)
60 entry->ops->deinit(entry->priv);
61 module_put(entry->ops->owner);
66 spin_unlock_irqrestore(&ieee->lock, flags);
69 /* After this, crypt_deinit_list won't accept new members */
70 void ieee80211_crypt_quiescing(struct ieee80211_device *ieee)
74 spin_lock_irqsave(&ieee->lock, flags);
75 ieee->crypt_quiesced = 1;
76 spin_unlock_irqrestore(&ieee->lock, flags);
79 void ieee80211_crypt_deinit_handler(unsigned long data)
81 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
84 ieee80211_crypt_deinit_entries(ieee, 0);
86 spin_lock_irqsave(&ieee->lock, flags);
87 if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) {
88 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
89 "deletion list\n", ieee->dev->name);
90 ieee->crypt_deinit_timer.expires = jiffies + HZ;
91 add_timer(&ieee->crypt_deinit_timer);
93 spin_unlock_irqrestore(&ieee->lock, flags);
96 void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
97 struct ieee80211_crypt_data **crypt)
99 struct ieee80211_crypt_data *tmp;
108 /* must not run ops->deinit() while there may be pending encrypt or
109 * decrypt operations. Use a list of delayed deinits to avoid needing
112 spin_lock_irqsave(&ieee->lock, flags);
113 if (!ieee->crypt_quiesced) {
114 list_add(&tmp->list, &ieee->crypt_deinit_list);
115 if (!timer_pending(&ieee->crypt_deinit_timer)) {
116 ieee->crypt_deinit_timer.expires = jiffies + HZ;
117 add_timer(&ieee->crypt_deinit_timer);
120 spin_unlock_irqrestore(&ieee->lock, flags);
123 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
126 struct ieee80211_crypto_alg *alg;
131 alg = kmalloc(sizeof(*alg), GFP_KERNEL);
135 memset(alg, 0, sizeof(*alg));
138 spin_lock_irqsave(&hcrypt->lock, flags);
139 list_add(&alg->list, &hcrypt->algs);
140 spin_unlock_irqrestore(&hcrypt->lock, flags);
142 printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
148 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
151 struct list_head *ptr;
152 struct ieee80211_crypto_alg *del_alg = NULL;
157 spin_lock_irqsave(&hcrypt->lock, flags);
158 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
159 struct ieee80211_crypto_alg *alg =
160 (struct ieee80211_crypto_alg *)ptr;
161 if (alg->ops == ops) {
162 list_del(&alg->list);
167 spin_unlock_irqrestore(&hcrypt->lock, flags);
170 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
171 "'%s'\n", ops->name);
175 return del_alg ? 0 : -1;
178 struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
181 struct list_head *ptr;
182 struct ieee80211_crypto_alg *found_alg = NULL;
187 spin_lock_irqsave(&hcrypt->lock, flags);
188 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
189 struct ieee80211_crypto_alg *alg =
190 (struct ieee80211_crypto_alg *)ptr;
191 if (strcmp(alg->ops->name, name) == 0) {
196 spin_unlock_irqrestore(&hcrypt->lock, flags);
199 return found_alg->ops;
204 static void *ieee80211_crypt_null_init(int keyidx)
208 static void ieee80211_crypt_null_deinit(void *priv)
212 static struct ieee80211_crypto_ops ieee80211_crypt_null = {
214 .init = ieee80211_crypt_null_init,
215 .deinit = ieee80211_crypt_null_deinit,
216 .encrypt_mpdu = NULL,
217 .decrypt_mpdu = NULL,
218 .encrypt_msdu = NULL,
219 .decrypt_msdu = NULL,
222 .extra_mpdu_prefix_len = 0,
223 .extra_mpdu_postfix_len = 0,
224 .owner = THIS_MODULE,
227 static int __init ieee80211_crypto_init(void)
231 hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
235 memset(hcrypt, 0, sizeof(*hcrypt));
236 INIT_LIST_HEAD(&hcrypt->algs);
237 spin_lock_init(&hcrypt->lock);
239 ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
248 static void __exit ieee80211_crypto_deinit(void)
250 struct list_head *ptr, *n;
255 for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
256 ptr = n, n = ptr->next) {
257 struct ieee80211_crypto_alg *alg =
258 (struct ieee80211_crypto_alg *)ptr;
260 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
261 "'%s' (deinit)\n", alg->ops->name);
268 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
269 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
270 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
271 EXPORT_SYMBOL(ieee80211_crypt_quiescing);
273 EXPORT_SYMBOL(ieee80211_register_crypto_ops);
274 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
275 EXPORT_SYMBOL(ieee80211_get_crypto_ops);
277 module_init(ieee80211_crypto_init);
278 module_exit(ieee80211_crypto_deinit);