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/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
22 struct rtllib_crypto_alg {
23 struct list_head list;
24 struct lib80211_crypto_ops *ops;
28 struct rtllib_crypto {
29 struct list_head algs;
33 static struct rtllib_crypto *hcrypt;
35 void rtllib_crypt_deinit_entries(struct lib80211_crypt_info *info,
38 struct list_head *ptr, *n;
39 struct lib80211_crypt_data *entry;
41 for (ptr = info->crypt_deinit_list.next, n = ptr->next;
42 ptr != &info->crypt_deinit_list; ptr = n, n = ptr->next) {
43 entry = list_entry(ptr, struct lib80211_crypt_data, list);
45 if (atomic_read(&entry->refcnt) != 0 && !force)
51 entry->ops->deinit(entry->priv);
55 EXPORT_SYMBOL(rtllib_crypt_deinit_entries);
57 void rtllib_crypt_deinit_handler(unsigned long data)
59 struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
62 spin_lock_irqsave(info->lock, flags);
63 rtllib_crypt_deinit_entries(info, 0);
64 if (!list_empty(&info->crypt_deinit_list)) {
66 "%s: entries remaining in delayed crypt deletion list\n",
68 info->crypt_deinit_timer.expires = jiffies + HZ;
69 add_timer(&info->crypt_deinit_timer);
71 spin_unlock_irqrestore(info->lock, flags);
74 EXPORT_SYMBOL(rtllib_crypt_deinit_handler);
76 void rtllib_crypt_delayed_deinit(struct lib80211_crypt_info *info,
77 struct lib80211_crypt_data **crypt)
79 struct lib80211_crypt_data *tmp;
88 /* must not run ops->deinit() while there may be pending encrypt or
89 * decrypt operations. Use a list of delayed deinits to avoid needing
92 spin_lock_irqsave(info->lock, flags);
93 list_add(&tmp->list, &info->crypt_deinit_list);
94 if (!timer_pending(&info->crypt_deinit_timer)) {
95 info->crypt_deinit_timer.expires = jiffies + HZ;
96 add_timer(&info->crypt_deinit_timer);
98 spin_unlock_irqrestore(info->lock, flags);
100 EXPORT_SYMBOL(rtllib_crypt_delayed_deinit);
102 int rtllib_register_crypto_ops(struct lib80211_crypto_ops *ops)
105 struct rtllib_crypto_alg *alg;
110 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
116 spin_lock_irqsave(&hcrypt->lock, flags);
117 list_add(&alg->list, &hcrypt->algs);
118 spin_unlock_irqrestore(&hcrypt->lock, flags);
120 printk(KERN_DEBUG "rtllib_crypt: registered algorithm '%s'\n",
125 EXPORT_SYMBOL(rtllib_register_crypto_ops);
127 int rtllib_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
130 struct list_head *ptr;
131 struct rtllib_crypto_alg *del_alg = NULL;
136 spin_lock_irqsave(&hcrypt->lock, flags);
137 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
138 struct rtllib_crypto_alg *alg =
139 (struct rtllib_crypto_alg *) ptr;
140 if (alg->ops == ops) {
141 list_del(&alg->list);
146 spin_unlock_irqrestore(&hcrypt->lock, flags);
149 printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm '%s'\n",
154 return del_alg ? 0 : -1;
156 EXPORT_SYMBOL(rtllib_unregister_crypto_ops);
159 struct lib80211_crypto_ops *rtllib_get_crypto_ops(const char *name)
162 struct list_head *ptr;
163 struct rtllib_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 rtllib_crypto_alg *alg =
171 (struct rtllib_crypto_alg *) ptr;
172 if (strcmp(alg->ops->name, name) == 0) {
177 spin_unlock_irqrestore(&hcrypt->lock, flags);
180 return found_alg->ops;
184 EXPORT_SYMBOL(rtllib_get_crypto_ops);
187 static void *rtllib_crypt_null_init(int keyidx) { return (void *) 1; }
188 static void rtllib_crypt_null_deinit(void *priv) {}
190 static struct lib80211_crypto_ops rtllib_crypt_null = {
192 .init = rtllib_crypt_null_init,
193 .deinit = rtllib_crypt_null_deinit,
194 .encrypt_mpdu = NULL,
195 .decrypt_mpdu = NULL,
196 .encrypt_msdu = NULL,
197 .decrypt_msdu = NULL,
200 .extra_mpdu_prefix_len = 0,
201 .extra_mpdu_postfix_len = 0,
202 .extra_msdu_prefix_len = 0,
203 .extra_msdu_postfix_len = 0,
204 .owner = THIS_MODULE,
208 int __init rtllib_crypto_init(void)
212 hcrypt = kzalloc(sizeof(*hcrypt), GFP_KERNEL);
216 INIT_LIST_HEAD(&hcrypt->algs);
217 spin_lock_init(&hcrypt->lock);
219 ret = lib80211_register_crypto_ops(&rtllib_crypt_null);
229 void __exit rtllib_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 rtllib_crypto_alg *alg =
239 (struct rtllib_crypto_alg *) ptr;
242 "rtllib_crypt: unregistered algorithm '%s' (deinit)\n",
250 module_init(rtllib_crypto_init);
251 module_exit(rtllib_crypto_deinit);
253 MODULE_LICENSE("GPL");