Commit | Line | Data |
---|---|---|
ecdfa446 GKH |
1 | /* |
2 | * Host AP crypto routines | |
3 | * | |
4 | * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi> | |
5 | * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com> | |
6 | * | |
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 | |
10 | * more details. | |
11 | * | |
12 | */ | |
13 | ||
ecdfa446 GKH |
14 | #include <linux/module.h> |
15 | #include <linux/init.h> | |
16 | #include <linux/slab.h> | |
a44325f9 LF |
17 | #include <linux/string.h> |
18 | #include <linux/errno.h> | |
ecdfa446 | 19 | |
94a79942 | 20 | #include "rtllib.h" |
ecdfa446 | 21 | |
94a79942 | 22 | struct rtllib_crypto_alg { |
ecdfa446 | 23 | struct list_head list; |
32c44cb5 | 24 | struct lib80211_crypto_ops *ops; |
ecdfa446 GKH |
25 | }; |
26 | ||
27 | ||
94a79942 | 28 | struct rtllib_crypto { |
ecdfa446 GKH |
29 | struct list_head algs; |
30 | spinlock_t lock; | |
31 | }; | |
32 | ||
94a79942 | 33 | static struct rtllib_crypto *hcrypt; |
ecdfa446 | 34 | |
0ddcf5fd | 35 | void rtllib_crypt_deinit_entries(struct lib80211_crypt_info *info, |
ecdfa446 GKH |
36 | int force) |
37 | { | |
38 | struct list_head *ptr, *n; | |
32c44cb5 | 39 | struct lib80211_crypt_data *entry; |
ecdfa446 | 40 | |
0ddcf5fd SM |
41 | for (ptr = info->crypt_deinit_list.next, n = ptr->next; |
42 | ptr != &info->crypt_deinit_list; ptr = n, n = ptr->next) { | |
32c44cb5 | 43 | entry = list_entry(ptr, struct lib80211_crypt_data, list); |
ecdfa446 GKH |
44 | |
45 | if (atomic_read(&entry->refcnt) != 0 && !force) | |
46 | continue; | |
47 | ||
48 | list_del(ptr); | |
49 | ||
a010a337 | 50 | if (entry->ops) |
ecdfa446 | 51 | entry->ops->deinit(entry->priv); |
ecdfa446 GKH |
52 | kfree(entry); |
53 | } | |
54 | } | |
3b28499c | 55 | EXPORT_SYMBOL(rtllib_crypt_deinit_entries); |
ecdfa446 | 56 | |
94a79942 | 57 | void rtllib_crypt_deinit_handler(unsigned long data) |
ecdfa446 | 58 | { |
0ddcf5fd | 59 | struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data; |
ecdfa446 GKH |
60 | unsigned long flags; |
61 | ||
0ddcf5fd SM |
62 | spin_lock_irqsave(info->lock, flags); |
63 | rtllib_crypt_deinit_entries(info, 0); | |
64 | if (!list_empty(&info->crypt_deinit_list)) { | |
ecdfa446 | 65 | printk(KERN_DEBUG "%s: entries remaining in delayed crypt " |
0ddcf5fd SM |
66 | "deletion list\n", info->name); |
67 | info->crypt_deinit_timer.expires = jiffies + HZ; | |
68 | add_timer(&info->crypt_deinit_timer); | |
ecdfa446 | 69 | } |
0ddcf5fd | 70 | spin_unlock_irqrestore(info->lock, flags); |
ecdfa446 GKH |
71 | |
72 | } | |
3b28499c | 73 | EXPORT_SYMBOL(rtllib_crypt_deinit_handler); |
ecdfa446 | 74 | |
0ddcf5fd SM |
75 | void rtllib_crypt_delayed_deinit(struct lib80211_crypt_info *info, |
76 | struct lib80211_crypt_data **crypt) | |
ecdfa446 | 77 | { |
32c44cb5 | 78 | struct lib80211_crypt_data *tmp; |
ecdfa446 GKH |
79 | unsigned long flags; |
80 | ||
81 | if (*crypt == NULL) | |
82 | return; | |
83 | ||
84 | tmp = *crypt; | |
85 | *crypt = NULL; | |
86 | ||
87 | /* must not run ops->deinit() while there may be pending encrypt or | |
88 | * decrypt operations. Use a list of delayed deinits to avoid needing | |
89 | * locking. */ | |
90 | ||
0ddcf5fd SM |
91 | spin_lock_irqsave(info->lock, flags); |
92 | list_add(&tmp->list, &info->crypt_deinit_list); | |
93 | if (!timer_pending(&info->crypt_deinit_timer)) { | |
94 | info->crypt_deinit_timer.expires = jiffies + HZ; | |
95 | add_timer(&info->crypt_deinit_timer); | |
ecdfa446 | 96 | } |
0ddcf5fd | 97 | spin_unlock_irqrestore(info->lock, flags); |
ecdfa446 | 98 | } |
3b28499c | 99 | EXPORT_SYMBOL(rtllib_crypt_delayed_deinit); |
ecdfa446 | 100 | |
32c44cb5 | 101 | int rtllib_register_crypto_ops(struct lib80211_crypto_ops *ops) |
ecdfa446 GKH |
102 | { |
103 | unsigned long flags; | |
94a79942 | 104 | struct rtllib_crypto_alg *alg; |
ecdfa446 GKH |
105 | |
106 | if (hcrypt == NULL) | |
107 | return -1; | |
108 | ||
929fa2a4 | 109 | alg = kzalloc(sizeof(*alg), GFP_KERNEL); |
ecdfa446 GKH |
110 | if (alg == NULL) |
111 | return -ENOMEM; | |
112 | ||
ecdfa446 GKH |
113 | alg->ops = ops; |
114 | ||
115 | spin_lock_irqsave(&hcrypt->lock, flags); | |
116 | list_add(&alg->list, &hcrypt->algs); | |
117 | spin_unlock_irqrestore(&hcrypt->lock, flags); | |
118 | ||
94a79942 | 119 | printk(KERN_DEBUG "rtllib_crypt: registered algorithm '%s'\n", |
ecdfa446 GKH |
120 | ops->name); |
121 | ||
122 | return 0; | |
123 | } | |
3b28499c | 124 | EXPORT_SYMBOL(rtllib_register_crypto_ops); |
ecdfa446 | 125 | |
32c44cb5 | 126 | int rtllib_unregister_crypto_ops(struct lib80211_crypto_ops *ops) |
ecdfa446 GKH |
127 | { |
128 | unsigned long flags; | |
129 | struct list_head *ptr; | |
94a79942 | 130 | struct rtllib_crypto_alg *del_alg = NULL; |
ecdfa446 GKH |
131 | |
132 | if (hcrypt == NULL) | |
133 | return -1; | |
134 | ||
135 | spin_lock_irqsave(&hcrypt->lock, flags); | |
136 | for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) { | |
94a79942 LF |
137 | struct rtllib_crypto_alg *alg = |
138 | (struct rtllib_crypto_alg *) ptr; | |
ecdfa446 GKH |
139 | if (alg->ops == ops) { |
140 | list_del(&alg->list); | |
141 | del_alg = alg; | |
142 | break; | |
143 | } | |
144 | } | |
145 | spin_unlock_irqrestore(&hcrypt->lock, flags); | |
146 | ||
147 | if (del_alg) { | |
94a79942 | 148 | printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm " |
ecdfa446 GKH |
149 | "'%s'\n", ops->name); |
150 | kfree(del_alg); | |
151 | } | |
152 | ||
153 | return del_alg ? 0 : -1; | |
154 | } | |
3b28499c | 155 | EXPORT_SYMBOL(rtllib_unregister_crypto_ops); |
ecdfa446 GKH |
156 | |
157 | ||
32c44cb5 | 158 | struct lib80211_crypto_ops *rtllib_get_crypto_ops(const char *name) |
ecdfa446 GKH |
159 | { |
160 | unsigned long flags; | |
161 | struct list_head *ptr; | |
94a79942 | 162 | struct rtllib_crypto_alg *found_alg = NULL; |
ecdfa446 GKH |
163 | |
164 | if (hcrypt == NULL) | |
165 | return NULL; | |
166 | ||
167 | spin_lock_irqsave(&hcrypt->lock, flags); | |
168 | for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) { | |
94a79942 LF |
169 | struct rtllib_crypto_alg *alg = |
170 | (struct rtllib_crypto_alg *) ptr; | |
ecdfa446 GKH |
171 | if (strcmp(alg->ops->name, name) == 0) { |
172 | found_alg = alg; | |
173 | break; | |
174 | } | |
175 | } | |
176 | spin_unlock_irqrestore(&hcrypt->lock, flags); | |
177 | ||
178 | if (found_alg) | |
179 | return found_alg->ops; | |
180 | else | |
181 | return NULL; | |
182 | } | |
3b28499c | 183 | EXPORT_SYMBOL(rtllib_get_crypto_ops); |
ecdfa446 GKH |
184 | |
185 | ||
94a79942 LF |
186 | static void * rtllib_crypt_null_init(int keyidx) { return (void *) 1; } |
187 | static void rtllib_crypt_null_deinit(void *priv) {} | |
ecdfa446 | 188 | |
32c44cb5 | 189 | static struct lib80211_crypto_ops rtllib_crypt_null = { |
ecdfa446 | 190 | .name = "NULL", |
94a79942 LF |
191 | .init = rtllib_crypt_null_init, |
192 | .deinit = rtllib_crypt_null_deinit, | |
ecdfa446 GKH |
193 | .encrypt_mpdu = NULL, |
194 | .decrypt_mpdu = NULL, | |
195 | .encrypt_msdu = NULL, | |
196 | .decrypt_msdu = NULL, | |
197 | .set_key = NULL, | |
198 | .get_key = NULL, | |
32c44cb5 SM |
199 | .extra_mpdu_prefix_len = 0, |
200 | .extra_mpdu_postfix_len = 0, | |
201 | .extra_msdu_prefix_len = 0, | |
202 | .extra_msdu_postfix_len = 0, | |
ecdfa446 GKH |
203 | .owner = THIS_MODULE, |
204 | }; | |
205 | ||
206 | ||
94a79942 | 207 | int __init rtllib_crypto_init(void) |
ecdfa446 GKH |
208 | { |
209 | int ret = -ENOMEM; | |
210 | ||
929fa2a4 | 211 | hcrypt = kzalloc(sizeof(*hcrypt), GFP_KERNEL); |
ecdfa446 GKH |
212 | if (!hcrypt) |
213 | goto out; | |
214 | ||
ecdfa446 GKH |
215 | INIT_LIST_HEAD(&hcrypt->algs); |
216 | spin_lock_init(&hcrypt->lock); | |
217 | ||
3b148be0 | 218 | ret = lib80211_register_crypto_ops(&rtllib_crypt_null); |
ecdfa446 GKH |
219 | if (ret < 0) { |
220 | kfree(hcrypt); | |
221 | hcrypt = NULL; | |
222 | } | |
223 | out: | |
224 | return ret; | |
225 | } | |
226 | ||
227 | ||
94a79942 | 228 | void __exit rtllib_crypto_deinit(void) |
ecdfa446 GKH |
229 | { |
230 | struct list_head *ptr, *n; | |
231 | ||
232 | if (hcrypt == NULL) | |
233 | return; | |
234 | ||
235 | for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs; | |
236 | ptr = n, n = ptr->next) { | |
94a79942 LF |
237 | struct rtllib_crypto_alg *alg = |
238 | (struct rtllib_crypto_alg *) ptr; | |
ecdfa446 | 239 | list_del(ptr); |
94a79942 | 240 | printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm " |
ecdfa446 GKH |
241 | "'%s' (deinit)\n", alg->ops->name); |
242 | kfree(alg); | |
243 | } | |
244 | ||
245 | kfree(hcrypt); | |
246 | } | |
d37e0208 SM |
247 | |
248 | module_init(rtllib_crypto_init); | |
249 | module_exit(rtllib_crypto_deinit); | |
250 | ||
251 | MODULE_LICENSE("GPL"); |