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