Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / rtl8192e / rtllib_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/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19
20 #include "rtllib.h"
21
22 struct rtllib_crypto_alg {
23         struct list_head list;
24         struct lib80211_crypto_ops *ops;
25 };
26
27
28 struct rtllib_crypto {
29         struct list_head algs;
30         spinlock_t lock;
31 };
32
33 static struct rtllib_crypto *hcrypt;
34
35 void rtllib_crypt_deinit_entries(struct lib80211_crypt_info *info,
36                                            int force)
37 {
38         struct list_head *ptr, *n;
39         struct lib80211_crypt_data *entry;
40
41         for (ptr = info->crypt_deinit_list.next, n = ptr->next;
42              ptr != &info->crypt_deinit_list; ptr = n, n = ptr->next) {
43                 entry = list_entry(ptr, struct lib80211_crypt_data, list);
44
45                 if (atomic_read(&entry->refcnt) != 0 && !force)
46                         continue;
47
48                 list_del(ptr);
49
50                 if (entry->ops)
51                         entry->ops->deinit(entry->priv);
52                 kfree(entry);
53         }
54 }
55 EXPORT_SYMBOL(rtllib_crypt_deinit_entries);
56
57 void rtllib_crypt_deinit_handler(unsigned long data)
58 {
59         struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
60         unsigned long flags;
61
62         spin_lock_irqsave(info->lock, flags);
63         rtllib_crypt_deinit_entries(info, 0);
64         if (!list_empty(&info->crypt_deinit_list)) {
65                 printk(KERN_DEBUG
66                        "%s: entries remaining in delayed crypt deletion list\n",
67                        info->name);
68                 info->crypt_deinit_timer.expires = jiffies + HZ;
69                 add_timer(&info->crypt_deinit_timer);
70         }
71         spin_unlock_irqrestore(info->lock, flags);
72
73 }
74 EXPORT_SYMBOL(rtllib_crypt_deinit_handler);
75
76 void rtllib_crypt_delayed_deinit(struct lib80211_crypt_info *info,
77                                  struct lib80211_crypt_data **crypt)
78 {
79         struct lib80211_crypt_data *tmp;
80         unsigned long flags;
81
82         if (*crypt == NULL)
83                 return;
84
85         tmp = *crypt;
86         *crypt = NULL;
87
88         /* must not run ops->deinit() while there may be pending encrypt or
89          * decrypt operations. Use a list of delayed deinits to avoid needing
90          * locking.
91          */
92
93         spin_lock_irqsave(info->lock, flags);
94         list_add(&tmp->list, &info->crypt_deinit_list);
95         if (!timer_pending(&info->crypt_deinit_timer)) {
96                 info->crypt_deinit_timer.expires = jiffies + HZ;
97                 add_timer(&info->crypt_deinit_timer);
98         }
99         spin_unlock_irqrestore(info->lock, flags);
100 }
101 EXPORT_SYMBOL(rtllib_crypt_delayed_deinit);
102
103 int rtllib_register_crypto_ops(struct lib80211_crypto_ops *ops)
104 {
105         unsigned long flags;
106         struct rtllib_crypto_alg *alg;
107
108         if (hcrypt == NULL)
109                 return -1;
110
111         alg = kzalloc(sizeof(*alg), GFP_KERNEL);
112         if (alg == NULL)
113                 return -ENOMEM;
114
115         alg->ops = ops;
116
117         spin_lock_irqsave(&hcrypt->lock, flags);
118         list_add(&alg->list, &hcrypt->algs);
119         spin_unlock_irqrestore(&hcrypt->lock, flags);
120
121         printk(KERN_DEBUG "rtllib_crypt: registered algorithm '%s'\n",
122                ops->name);
123
124         return 0;
125 }
126 EXPORT_SYMBOL(rtllib_register_crypto_ops);
127
128 int rtllib_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
129 {
130         unsigned long flags;
131         struct list_head *ptr;
132         struct rtllib_crypto_alg *del_alg = NULL;
133
134         if (hcrypt == NULL)
135                 return -1;
136
137         spin_lock_irqsave(&hcrypt->lock, flags);
138         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
139                 struct rtllib_crypto_alg *alg =
140                         (struct rtllib_crypto_alg *) ptr;
141                 if (alg->ops == ops) {
142                         list_del(&alg->list);
143                         del_alg = alg;
144                         break;
145                 }
146         }
147         spin_unlock_irqrestore(&hcrypt->lock, flags);
148
149         if (del_alg) {
150                 printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm '%s'\n",
151                        ops->name);
152                 kfree(del_alg);
153         }
154
155         return del_alg ? 0 : -1;
156 }
157 EXPORT_SYMBOL(rtllib_unregister_crypto_ops);
158
159
160 struct lib80211_crypto_ops *rtllib_get_crypto_ops(const char *name)
161 {
162         unsigned long flags;
163         struct list_head *ptr;
164         struct rtllib_crypto_alg *found_alg = NULL;
165
166         if (hcrypt == NULL)
167                 return NULL;
168
169         spin_lock_irqsave(&hcrypt->lock, flags);
170         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
171                 struct rtllib_crypto_alg *alg =
172                         (struct rtllib_crypto_alg *) ptr;
173                 if (strcmp(alg->ops->name, name) == 0) {
174                         found_alg = alg;
175                         break;
176                 }
177         }
178         spin_unlock_irqrestore(&hcrypt->lock, flags);
179
180         if (found_alg)
181                 return found_alg->ops;
182         else
183                 return NULL;
184 }
185 EXPORT_SYMBOL(rtllib_get_crypto_ops);
186
187
188 static void *rtllib_crypt_null_init(int keyidx) { return (void *) 1; }
189 static void rtllib_crypt_null_deinit(void *priv) {}
190
191 static struct lib80211_crypto_ops rtllib_crypt_null = {
192         .name                   = "NULL",
193         .init                   = rtllib_crypt_null_init,
194         .deinit                 = rtllib_crypt_null_deinit,
195         .encrypt_mpdu           = NULL,
196         .decrypt_mpdu           = NULL,
197         .encrypt_msdu           = NULL,
198         .decrypt_msdu           = NULL,
199         .set_key                = NULL,
200         .get_key                = NULL,
201         .extra_mpdu_prefix_len  = 0,
202         .extra_mpdu_postfix_len = 0,
203         .extra_msdu_prefix_len  = 0,
204         .extra_msdu_postfix_len = 0,
205         .owner                  = THIS_MODULE,
206 };
207
208
209 int __init rtllib_crypto_init(void)
210 {
211         int ret = -ENOMEM;
212
213         hcrypt = kzalloc(sizeof(*hcrypt), GFP_KERNEL);
214         if (!hcrypt)
215                 goto out;
216
217         INIT_LIST_HEAD(&hcrypt->algs);
218         spin_lock_init(&hcrypt->lock);
219
220         ret = lib80211_register_crypto_ops(&rtllib_crypt_null);
221         if (ret < 0) {
222                 kfree(hcrypt);
223                 hcrypt = NULL;
224         }
225 out:
226         return ret;
227 }
228
229
230 void __exit rtllib_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 rtllib_crypto_alg *alg =
240                         (struct rtllib_crypto_alg *) ptr;
241                 list_del(ptr);
242                 printk(KERN_DEBUG
243                        "rtllib_crypt: unregistered algorithm '%s' (deinit)\n",
244                        alg->ops->name);
245                 kfree(alg);
246         }
247
248         kfree(hcrypt);
249 }
250
251 module_init(rtllib_crypto_init);
252 module_exit(rtllib_crypto_deinit);
253
254 MODULE_LICENSE("GPL");