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
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>
21
22 #if (LINUX_VERSION_CODE<KERNEL_VERSION(2,6,18))
23 #include<linux/config.h>
24 #endif
25
26 #include "ieee80211.h"
27
28 MODULE_AUTHOR("Jouni Malinen");
29 MODULE_DESCRIPTION("HostAP crypto");
30 MODULE_LICENSE("GPL");
31
32 struct ieee80211_crypto_alg {
33 struct list_head list;
34 struct ieee80211_crypto_ops *ops;
35 };
36
37
38 struct ieee80211_crypto {
39 struct list_head algs;
40 spinlock_t lock;
41 };
42
43 static struct ieee80211_crypto *hcrypt;
44
ieee80211_crypt_deinit_entries(struct ieee80211_device * ieee,int force)45 void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee,
46 int force)
47 {
48 struct list_head *ptr, *n;
49 struct ieee80211_crypt_data *entry;
50
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);
54
55 if (atomic_read(&entry->refcnt) != 0 && !force)
56 continue;
57
58 list_del(ptr);
59
60 if (entry->ops) {
61 entry->ops->deinit(entry->priv);
62 module_put(entry->ops->owner);
63 }
64 kfree(entry);
65 }
66 }
67
ieee80211_crypt_deinit_handler(unsigned long data)68 void ieee80211_crypt_deinit_handler(unsigned long data)
69 {
70 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
71 unsigned long flags;
72
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);
80 }
81 spin_unlock_irqrestore(&ieee->lock, flags);
82
83 }
84
ieee80211_crypt_delayed_deinit(struct ieee80211_device * ieee,struct ieee80211_crypt_data ** crypt)85 void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
86 struct ieee80211_crypt_data **crypt)
87 {
88 struct ieee80211_crypt_data *tmp;
89 unsigned long flags;
90
91 if (*crypt == NULL)
92 return;
93
94 tmp = *crypt;
95 *crypt = NULL;
96
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
99 * locking. */
100
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);
106 }
107 spin_unlock_irqrestore(&ieee->lock, flags);
108 }
109
ieee80211_register_crypto_ops(struct ieee80211_crypto_ops * ops)110 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
111 {
112 unsigned long flags;
113 struct ieee80211_crypto_alg *alg;
114
115 if (hcrypt == NULL)
116 return -1;
117
118 alg = kmalloc(sizeof(*alg), GFP_KERNEL);
119 if (alg == NULL)
120 return -ENOMEM;
121
122 memset(alg, 0, sizeof(*alg));
123 alg->ops = ops;
124
125 spin_lock_irqsave(&hcrypt->lock, flags);
126 list_add(&alg->list, &hcrypt->algs);
127 spin_unlock_irqrestore(&hcrypt->lock, flags);
128
129 printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
130 ops->name);
131
132 return 0;
133 }
134
ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops * ops)135 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
136 {
137 unsigned long flags;
138 struct list_head *ptr;
139 struct ieee80211_crypto_alg *del_alg = NULL;
140
141 if (hcrypt == NULL)
142 return -1;
143
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);
150 del_alg = alg;
151 break;
152 }
153 }
154 spin_unlock_irqrestore(&hcrypt->lock, flags);
155
156 if (del_alg) {
157 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
158 "'%s'\n", ops->name);
159 kfree(del_alg);
160 }
161
162 return del_alg ? 0 : -1;
163 }
164
165
ieee80211_get_crypto_ops(const char * name)166 struct ieee80211_crypto_ops * ieee80211_get_crypto_ops(const char *name)
167 {
168 unsigned long flags;
169 struct list_head *ptr;
170 struct ieee80211_crypto_alg *found_alg = NULL;
171
172 if (hcrypt == NULL)
173 return NULL;
174
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) {
180 found_alg = alg;
181 break;
182 }
183 }
184 spin_unlock_irqrestore(&hcrypt->lock, flags);
185
186 if (found_alg)
187 return found_alg->ops;
188 else
189 return NULL;
190 }
191
192
ieee80211_crypt_null_init(int keyidx)193 static void * ieee80211_crypt_null_init(int keyidx) { return (void *) 1; }
ieee80211_crypt_null_deinit(void * priv)194 static void ieee80211_crypt_null_deinit(void *priv) {}
195
196 static struct ieee80211_crypto_ops ieee80211_crypt_null = {
197 .name = "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,
204 .set_key = NULL,
205 .get_key = NULL,
206 .extra_prefix_len = 0,
207 .extra_postfix_len = 0,
208 .owner = THIS_MODULE,
209 };
210
211
ieee80211_crypto_init(void)212 int ieee80211_crypto_init(void)
213 {
214 int ret = -ENOMEM;
215
216 hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
217 if (!hcrypt)
218 goto out;
219
220 memset(hcrypt, 0, sizeof(*hcrypt));
221 INIT_LIST_HEAD(&hcrypt->algs);
222 spin_lock_init(&hcrypt->lock);
223
224 ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
225 if (ret < 0) {
226 kfree(hcrypt);
227 hcrypt = NULL;
228 }
229 out:
230 return ret;
231 }
232
233
ieee80211_crypto_deinit(void)234 void ieee80211_crypto_deinit(void)
235 {
236 struct list_head *ptr, *n;
237 struct ieee80211_crypto_alg *alg = NULL;
238
239 if (hcrypt == NULL)
240 return;
241
242 list_for_each_safe(ptr, n, &hcrypt->algs) {
243 alg = list_entry(ptr, struct ieee80211_crypto_alg, list);
244 if (alg) {
245 list_del(ptr);
246 printk(KERN_DEBUG
247 "ieee80211_crypt: unregistered algorithm '%s' (deinit)\n",
248 alg->ops->name);
249 kfree(alg);
250 }
251 }
252 kfree(hcrypt);
253 }
254
255 #if 0
256 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
257 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
258 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
259
260 EXPORT_SYMBOL(ieee80211_register_crypto_ops);
261 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
262 EXPORT_SYMBOL(ieee80211_get_crypto_ops);
263 #endif
264
265 //module_init(ieee80211_crypto_init);
266 //module_exit(ieee80211_crypto_deinit);
267