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