Merge upstream 2.6.13-rc1-git1 into 'ieee80211' branch of netdev-2.6.
[pandora-kernel.git] / net / ieee80211 / ieee80211_crypt.c
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 #include <net/ieee80211.h>
23
24 MODULE_AUTHOR("Jouni Malinen");
25 MODULE_DESCRIPTION("HostAP crypto");
26 MODULE_LICENSE("GPL");
27
28 struct ieee80211_crypto_alg {
29         struct list_head list;
30         struct ieee80211_crypto_ops *ops;
31 };
32
33
34 struct ieee80211_crypto {
35         struct list_head algs;
36         spinlock_t lock;
37 };
38
39 static struct ieee80211_crypto *hcrypt;
40
41 void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee,
42                                            int force)
43 {
44         struct list_head *ptr, *n;
45         struct ieee80211_crypt_data *entry;
46
47         for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
48              ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
49                 entry = list_entry(ptr, struct ieee80211_crypt_data, list);
50
51                 if (atomic_read(&entry->refcnt) != 0 && !force)
52                         continue;
53
54                 list_del(ptr);
55
56                 if (entry->ops) {
57                         entry->ops->deinit(entry->priv);
58                         module_put(entry->ops->owner);
59                 }
60                 kfree(entry);
61         }
62 }
63
64 void ieee80211_crypt_deinit_handler(unsigned long data)
65 {
66         struct ieee80211_device *ieee = (struct ieee80211_device *)data;
67         unsigned long flags;
68
69         spin_lock_irqsave(&ieee->lock, flags);
70         ieee80211_crypt_deinit_entries(ieee, 0);
71         if (!list_empty(&ieee->crypt_deinit_list)) {
72                 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
73                        "deletion list\n", ieee->dev->name);
74                 ieee->crypt_deinit_timer.expires = jiffies + HZ;
75                 add_timer(&ieee->crypt_deinit_timer);
76         }
77         spin_unlock_irqrestore(&ieee->lock, flags);
78
79 }
80
81 void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
82                                     struct ieee80211_crypt_data **crypt)
83 {
84         struct ieee80211_crypt_data *tmp;
85         unsigned long flags;
86
87         if (*crypt == NULL)
88                 return;
89
90         tmp = *crypt;
91         *crypt = NULL;
92
93         /* must not run ops->deinit() while there may be pending encrypt or
94          * decrypt operations. Use a list of delayed deinits to avoid needing
95          * locking. */
96
97         spin_lock_irqsave(&ieee->lock, flags);
98         list_add(&tmp->list, &ieee->crypt_deinit_list);
99         if (!timer_pending(&ieee->crypt_deinit_timer)) {
100                 ieee->crypt_deinit_timer.expires = jiffies + HZ;
101                 add_timer(&ieee->crypt_deinit_timer);
102         }
103         spin_unlock_irqrestore(&ieee->lock, flags);
104 }
105
106 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
107 {
108         unsigned long flags;
109         struct ieee80211_crypto_alg *alg;
110
111         if (hcrypt == NULL)
112                 return -1;
113
114         alg = kmalloc(sizeof(*alg), GFP_KERNEL);
115         if (alg == NULL)
116                 return -ENOMEM;
117
118         memset(alg, 0, sizeof(*alg));
119         alg->ops = ops;
120
121         spin_lock_irqsave(&hcrypt->lock, flags);
122         list_add(&alg->list, &hcrypt->algs);
123         spin_unlock_irqrestore(&hcrypt->lock, flags);
124
125         printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
126                ops->name);
127
128         return 0;
129 }
130
131 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
132 {
133         unsigned long flags;
134         struct list_head *ptr;
135         struct ieee80211_crypto_alg *del_alg = NULL;
136
137         if (hcrypt == NULL)
138                 return -1;
139
140         spin_lock_irqsave(&hcrypt->lock, flags);
141         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
142                 struct ieee80211_crypto_alg *alg =
143                         (struct ieee80211_crypto_alg *) ptr;
144                 if (alg->ops == ops) {
145                         list_del(&alg->list);
146                         del_alg = alg;
147                         break;
148                 }
149         }
150         spin_unlock_irqrestore(&hcrypt->lock, flags);
151
152         if (del_alg) {
153                 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
154                        "'%s'\n", ops->name);
155                 kfree(del_alg);
156         }
157
158         return del_alg ? 0 : -1;
159 }
160
161
162 struct ieee80211_crypto_ops * ieee80211_get_crypto_ops(const char *name)
163 {
164         unsigned long flags;
165         struct list_head *ptr;
166         struct ieee80211_crypto_alg *found_alg = NULL;
167
168         if (hcrypt == NULL)
169                 return NULL;
170
171         spin_lock_irqsave(&hcrypt->lock, flags);
172         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
173                 struct ieee80211_crypto_alg *alg =
174                         (struct ieee80211_crypto_alg *) ptr;
175                 if (strcmp(alg->ops->name, name) == 0) {
176                         found_alg = alg;
177                         break;
178                 }
179         }
180         spin_unlock_irqrestore(&hcrypt->lock, flags);
181
182         if (found_alg)
183                 return found_alg->ops;
184         else
185                 return NULL;
186 }
187
188
189 static void * ieee80211_crypt_null_init(int keyidx) { return (void *) 1; }
190 static void ieee80211_crypt_null_deinit(void *priv) {}
191
192 static struct ieee80211_crypto_ops ieee80211_crypt_null = {
193         .name                   = "NULL",
194         .init                   = ieee80211_crypt_null_init,
195         .deinit                 = ieee80211_crypt_null_deinit,
196         .encrypt_mpdu           = NULL,
197         .decrypt_mpdu           = NULL,
198         .encrypt_msdu           = NULL,
199         .decrypt_msdu           = NULL,
200         .set_key                = NULL,
201         .get_key                = NULL,
202         .extra_prefix_len       = 0,
203         .extra_postfix_len      = 0,
204         .owner                  = THIS_MODULE,
205 };
206
207
208 static int __init ieee80211_crypto_init(void)
209 {
210         int ret = -ENOMEM;
211
212         hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
213         if (!hcrypt)
214                 goto out;
215
216         memset(hcrypt, 0, sizeof(*hcrypt));
217         INIT_LIST_HEAD(&hcrypt->algs);
218         spin_lock_init(&hcrypt->lock);
219
220         ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
221         if (ret < 0) {
222                 kfree(hcrypt);
223                 hcrypt = NULL;
224         }
225 out:
226         return ret;
227 }
228
229
230 static void __exit ieee80211_crypto_deinit(void)
231 {
232         struct list_head *ptr, *n;
233
234         if (hcrypt == NULL)
235                 return;
236
237         for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
238              ptr = n, n = ptr->next) {
239                 struct ieee80211_crypto_alg *alg =
240                         (struct ieee80211_crypto_alg *) ptr;
241                 list_del(ptr);
242                 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
243                        "'%s' (deinit)\n", alg->ops->name);
244                 kfree(alg);
245         }
246
247         kfree(hcrypt);
248 }
249
250 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
251 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
252 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
253
254 EXPORT_SYMBOL(ieee80211_register_crypto_ops);
255 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
256 EXPORT_SYMBOL(ieee80211_get_crypto_ops);
257
258 module_init(ieee80211_crypto_init);
259 module_exit(ieee80211_crypto_deinit);