added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / staging / rtl8187se / ieee80211 / ieee80211_crypt.c
blob7370296225e19b78942d076cf84fca84e6a0ffb9
1 /*
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
10 * more details.
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>
24 #endif
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;
40 spinlock_t lock;
43 static struct ieee80211_crypto *hcrypt;
45 void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee,
46 int force)
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)
56 continue;
58 list_del(ptr);
60 if (entry->ops) {
61 entry->ops->deinit(entry->priv);
62 module_put(entry->ops->owner);
64 kfree(entry);
68 void ieee80211_crypt_deinit_handler(unsigned long data)
70 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
71 unsigned long flags;
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;
89 unsigned long flags;
91 if (*crypt == NULL)
92 return;
94 tmp = *crypt;
95 *crypt = NULL;
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. */
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)
112 unsigned long flags;
113 struct ieee80211_crypto_alg *alg;
115 if (hcrypt == NULL)
116 return -1;
118 alg = kmalloc(sizeof(*alg), GFP_KERNEL);
119 if (alg == NULL)
120 return -ENOMEM;
122 memset(alg, 0, sizeof(*alg));
123 alg->ops = ops;
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",
130 ops->name);
132 return 0;
135 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
137 unsigned long flags;
138 struct list_head *ptr;
139 struct ieee80211_crypto_alg *del_alg = NULL;
141 if (hcrypt == NULL)
142 return -1;
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;
154 spin_unlock_irqrestore(&hcrypt->lock, flags);
156 if (del_alg) {
157 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
158 "'%s'\n", ops->name);
159 kfree(del_alg);
162 return del_alg ? 0 : -1;
166 struct ieee80211_crypto_ops * ieee80211_get_crypto_ops(const char *name)
168 unsigned long flags;
169 struct list_head *ptr;
170 struct ieee80211_crypto_alg *found_alg = NULL;
172 if (hcrypt == NULL)
173 return 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) {
180 found_alg = alg;
181 break;
184 spin_unlock_irqrestore(&hcrypt->lock, flags);
186 if (found_alg)
187 return found_alg->ops;
188 else
189 return NULL;
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 = {
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,
212 int ieee80211_crypto_init(void)
214 int ret = -ENOMEM;
216 hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
217 if (!hcrypt)
218 goto out;
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);
225 if (ret < 0) {
226 kfree(hcrypt);
227 hcrypt = NULL;
229 out:
230 return ret;
234 void ieee80211_crypto_deinit(void)
236 struct list_head *ptr, *n;
237 struct ieee80211_crypto_alg *alg = NULL;
239 if (hcrypt == NULL)
240 return;
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);
252 kfree(hcrypt);
255 #if 0
256 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
257 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
258 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
260 EXPORT_SYMBOL(ieee80211_register_crypto_ops);
261 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
262 EXPORT_SYMBOL(ieee80211_get_crypto_ops);
263 #endif
265 //module_init(ieee80211_crypto_init);
266 //module_exit(ieee80211_crypto_deinit);