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 #if (LINUX_VERSION_CODE<KERNEL_VERSION(2,6,18))
23 #include<linux/config.h>
26 #include "ieee80211.h"
28 MODULE_AUTHOR("Jouni Malinen");
29 MODULE_DESCRIPTION("HostAP crypto");
30 MODULE_LICENSE("GPL");
32 struct ieee80211_crypto_alg {
33 struct list_head list;
34 struct ieee80211_crypto_ops *ops;
38 struct ieee80211_crypto {
39 struct list_head algs;
43 static struct ieee80211_crypto *hcrypt;
45 void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee,
48 struct list_head *ptr, *n;
49 struct ieee80211_crypt_data *entry;
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);
68 void ieee80211_crypt_deinit_handler(unsigned long data)
70 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
73 spin_lock_irqsave(&ieee->lock, flags);
74 ieee80211_crypt_deinit_entries(ieee, 0);
75 if (!list_empty(&ieee->crypt_deinit_list)) {
76 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
77 "deletion list\n", ieee->dev->name);
78 ieee->crypt_deinit_timer.expires = jiffies + HZ;
79 add_timer(&ieee->crypt_deinit_timer);
81 spin_unlock_irqrestore(&ieee->lock, flags);
85 void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
86 struct ieee80211_crypt_data **crypt)
88 struct ieee80211_crypt_data *tmp;
97 /* must not run ops->deinit() while there may be pending encrypt or
98 * decrypt operations. Use a list of delayed deinits to avoid needing
101 spin_lock_irqsave(&ieee->lock, flags);
102 list_add(&tmp->list, &ieee->crypt_deinit_list);
103 if (!timer_pending(&ieee->crypt_deinit_timer)) {
104 ieee->crypt_deinit_timer.expires = jiffies + HZ;
105 add_timer(&ieee->crypt_deinit_timer);
107 spin_unlock_irqrestore(&ieee->lock, flags);
110 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
113 struct ieee80211_crypto_alg *alg;
118 alg = kmalloc(sizeof(*alg), GFP_KERNEL);
122 memset(alg, 0, sizeof(*alg));
125 spin_lock_irqsave(&hcrypt->lock, flags);
126 list_add(&alg->list, &hcrypt->algs);
127 spin_unlock_irqrestore(&hcrypt->lock, flags);
129 printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
135 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
138 struct list_head *ptr;
139 struct ieee80211_crypto_alg *del_alg = NULL;
144 spin_lock_irqsave(&hcrypt->lock, flags);
145 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
146 struct ieee80211_crypto_alg *alg =
147 (struct ieee80211_crypto_alg *) ptr;
148 if (alg->ops == ops) {
149 list_del(&alg->list);
154 spin_unlock_irqrestore(&hcrypt->lock, flags);
157 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
158 "'%s'\n", ops->name);
162 return del_alg ? 0 : -1;
166 struct ieee80211_crypto_ops * ieee80211_get_crypto_ops(const char *name)
169 struct list_head *ptr;
170 struct ieee80211_crypto_alg *found_alg = NULL;
175 spin_lock_irqsave(&hcrypt->lock, flags);
176 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
177 struct ieee80211_crypto_alg *alg =
178 (struct ieee80211_crypto_alg *) ptr;
179 if (strcmp(alg->ops->name, name) == 0) {
184 spin_unlock_irqrestore(&hcrypt->lock, flags);
187 return found_alg->ops;
193 static void * ieee80211_crypt_null_init(int keyidx) { return (void *) 1; }
194 static void ieee80211_crypt_null_deinit(void *priv) {}
196 static struct ieee80211_crypto_ops ieee80211_crypt_null = {
198 .init = ieee80211_crypt_null_init,
199 .deinit = ieee80211_crypt_null_deinit,
200 .encrypt_mpdu = NULL,
201 .decrypt_mpdu = NULL,
202 .encrypt_msdu = NULL,
203 .decrypt_msdu = NULL,
206 .extra_prefix_len = 0,
207 .extra_postfix_len = 0,
208 .owner = THIS_MODULE,
212 int ieee80211_crypto_init(void)
216 hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
220 memset(hcrypt, 0, sizeof(*hcrypt));
221 INIT_LIST_HEAD(&hcrypt->algs);
222 spin_lock_init(&hcrypt->lock);
224 ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
234 void ieee80211_crypto_deinit(void)
236 struct list_head *ptr, *n;
241 for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
242 ptr = n, n = ptr->next) {
243 struct ieee80211_crypto_alg *alg =
244 (struct ieee80211_crypto_alg *) ptr;
246 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
247 "'%s' (deinit)\n", alg->ops->name);
255 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
256 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
257 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
259 EXPORT_SYMBOL(ieee80211_register_crypto_ops);
260 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
261 EXPORT_SYMBOL(ieee80211_get_crypto_ops);
264 //module_init(ieee80211_crypto_init);
265 //module_exit(ieee80211_crypto_deinit);