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;
46 spin_lock_irqsave(&ieee->lock, flags);
48 if (list_empty(&ieee->crypt_deinit_list))
51 for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
52 ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
53 entry = list_entry(ptr, struct ieee80211_crypt_data, list);
55 if (atomic_read(&entry->refcnt) != 0 && !force)
61 entry->ops->deinit(entry->priv);
62 module_put(entry->ops->owner);
67 spin_unlock_irqrestore(&ieee->lock, flags);
70 /* After this, crypt_deinit_list won't accept new members */
71 void ieee80211_crypt_quiescing(struct ieee80211_device *ieee)
75 spin_lock_irqsave(&ieee->lock, flags);
76 ieee->crypt_quiesced = 1;
77 spin_unlock_irqrestore(&ieee->lock, flags);
80 void ieee80211_crypt_deinit_handler(unsigned long data)
82 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
85 ieee80211_crypt_deinit_entries(ieee, 0);
87 spin_lock_irqsave(&ieee->lock, flags);
88 if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) {
89 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
90 "deletion list\n", ieee->dev->name);
91 ieee->crypt_deinit_timer.expires = jiffies + HZ;
92 add_timer(&ieee->crypt_deinit_timer);
94 spin_unlock_irqrestore(&ieee->lock, flags);
97 void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
98 struct ieee80211_crypt_data **crypt)
100 struct ieee80211_crypt_data *tmp;
109 /* must not run ops->deinit() while there may be pending encrypt or
110 * decrypt operations. Use a list of delayed deinits to avoid needing
113 spin_lock_irqsave(&ieee->lock, flags);
114 if (!ieee->crypt_quiesced) {
115 list_add(&tmp->list, &ieee->crypt_deinit_list);
116 if (!timer_pending(&ieee->crypt_deinit_timer)) {
117 ieee->crypt_deinit_timer.expires = jiffies + HZ;
118 add_timer(&ieee->crypt_deinit_timer);
121 spin_unlock_irqrestore(&ieee->lock, flags);
124 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
127 struct ieee80211_crypto_alg *alg;
132 alg = kmalloc(sizeof(*alg), GFP_KERNEL);
136 memset(alg, 0, sizeof(*alg));
139 spin_lock_irqsave(&hcrypt->lock, flags);
140 list_add(&alg->list, &hcrypt->algs);
141 spin_unlock_irqrestore(&hcrypt->lock, flags);
143 printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
149 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
152 struct list_head *ptr;
153 struct ieee80211_crypto_alg *del_alg = NULL;
158 spin_lock_irqsave(&hcrypt->lock, flags);
159 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
160 struct ieee80211_crypto_alg *alg =
161 (struct ieee80211_crypto_alg *)ptr;
162 if (alg->ops == ops) {
163 list_del(&alg->list);
168 spin_unlock_irqrestore(&hcrypt->lock, flags);
171 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
172 "'%s'\n", ops->name);
176 return del_alg ? 0 : -1;
179 struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
182 struct list_head *ptr;
183 struct ieee80211_crypto_alg *found_alg = NULL;
188 spin_lock_irqsave(&hcrypt->lock, flags);
189 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
190 struct ieee80211_crypto_alg *alg =
191 (struct ieee80211_crypto_alg *)ptr;
192 if (strcmp(alg->ops->name, name) == 0) {
197 spin_unlock_irqrestore(&hcrypt->lock, flags);
200 return found_alg->ops;
205 static void *ieee80211_crypt_null_init(int keyidx)
209 static void ieee80211_crypt_null_deinit(void *priv)
213 static struct ieee80211_crypto_ops ieee80211_crypt_null = {
215 .init = ieee80211_crypt_null_init,
216 .deinit = ieee80211_crypt_null_deinit,
217 .encrypt_mpdu = NULL,
218 .decrypt_mpdu = NULL,
219 .encrypt_msdu = NULL,
220 .decrypt_msdu = NULL,
223 .extra_mpdu_prefix_len = 0,
224 .extra_mpdu_postfix_len = 0,
225 .owner = THIS_MODULE,
228 static int __init ieee80211_crypto_init(void)
232 hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
236 memset(hcrypt, 0, sizeof(*hcrypt));
237 INIT_LIST_HEAD(&hcrypt->algs);
238 spin_lock_init(&hcrypt->lock);
240 ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
249 static void __exit ieee80211_crypto_deinit(void)
251 struct list_head *ptr, *n;
256 for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
257 ptr = n, n = ptr->next) {
258 struct ieee80211_crypto_alg *alg =
259 (struct ieee80211_crypto_alg *)ptr;
261 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
262 "'%s' (deinit)\n", alg->ops->name);
269 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
270 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
271 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
272 EXPORT_SYMBOL(ieee80211_crypt_quiescing);
274 EXPORT_SYMBOL(ieee80211_register_crypto_ops);
275 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
276 EXPORT_SYMBOL(ieee80211_get_crypto_ops);
278 module_init(ieee80211_crypto_init);
279 module_exit(ieee80211_crypto_deinit);