blob: 459611577d3dfa29f72442dfe1dcdfe4f2c6a502 [file] [log] [blame]
John W. Linville7e272fc2008-09-24 18:13:14 -04001/*
2 * lib80211 -- common bits for IEEE802.11 drivers
3 *
4 * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
5 *
John W. Linville274bfb82008-10-29 11:35:05 -04006 * Portions copied from old ieee80211 component, w/ original copyright
7 * notices below:
8 *
9 * Host AP crypto routines
10 *
11 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
12 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
13 *
John W. Linville7e272fc2008-09-24 18:13:14 -040014 */
15
Joe Perchese9c02682010-11-16 19:56:49 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
John W. Linville7e272fc2008-09-24 18:13:14 -040018#include <linux/module.h>
John W. Linville2819f8a2008-09-30 17:50:31 -040019#include <linux/ctype.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040020#include <linux/ieee80211.h>
John W. Linville274bfb82008-10-29 11:35:05 -040021#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/string.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040025
26#include <net/lib80211.h>
27
28#define DRV_NAME "lib80211"
29
30#define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
31
32MODULE_DESCRIPTION(DRV_DESCRIPTION);
33MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
34MODULE_LICENSE("GPL");
35
John W. Linville274bfb82008-10-29 11:35:05 -040036struct lib80211_crypto_alg {
37 struct list_head list;
38 struct lib80211_crypto_ops *ops;
39};
40
41static LIST_HEAD(lib80211_crypto_algs);
42static DEFINE_SPINLOCK(lib80211_crypto_lock);
43
Pavel Roskin9d630c72011-07-28 22:50:44 -040044static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
45 int force);
46static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
47static void lib80211_crypt_deinit_handler(unsigned long data);
48
John W. Linville2ba4b322008-11-11 16:00:06 -050049int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
50 spinlock_t *lock)
51{
52 memset(info, 0, sizeof(*info));
53
54 info->name = name;
55 info->lock = lock;
56
57 INIT_LIST_HEAD(&info->crypt_deinit_list);
58 setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
59 (unsigned long)info);
60
61 return 0;
62}
63EXPORT_SYMBOL(lib80211_crypt_info_init);
64
65void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
66{
67 int i;
68
69 lib80211_crypt_quiescing(info);
70 del_timer_sync(&info->crypt_deinit_timer);
71 lib80211_crypt_deinit_entries(info, 1);
72
73 for (i = 0; i < NUM_WEP_KEYS; i++) {
74 struct lib80211_crypt_data *crypt = info->crypt[i];
75 if (crypt) {
76 if (crypt->ops) {
77 crypt->ops->deinit(crypt->priv);
78 module_put(crypt->ops->owner);
79 }
80 kfree(crypt);
81 info->crypt[i] = NULL;
82 }
83 }
84}
85EXPORT_SYMBOL(lib80211_crypt_info_free);
86
Pavel Roskin9d630c72011-07-28 22:50:44 -040087static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
88 int force)
John W. Linville7e272fc2008-09-24 18:13:14 -040089{
John W. Linville274bfb82008-10-29 11:35:05 -040090 struct lib80211_crypt_data *entry, *next;
91 unsigned long flags;
92
93 spin_lock_irqsave(info->lock, flags);
94 list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
95 if (atomic_read(&entry->refcnt) != 0 && !force)
96 continue;
97
98 list_del(&entry->list);
99
100 if (entry->ops) {
101 entry->ops->deinit(entry->priv);
102 module_put(entry->ops->owner);
103 }
104 kfree(entry);
105 }
106 spin_unlock_irqrestore(info->lock, flags);
107}
John W. Linville274bfb82008-10-29 11:35:05 -0400108
109/* After this, crypt_deinit_list won't accept new members */
Pavel Roskin9d630c72011-07-28 22:50:44 -0400110static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
John W. Linville274bfb82008-10-29 11:35:05 -0400111{
112 unsigned long flags;
113
114 spin_lock_irqsave(info->lock, flags);
115 info->crypt_quiesced = 1;
116 spin_unlock_irqrestore(info->lock, flags);
117}
John W. Linville274bfb82008-10-29 11:35:05 -0400118
Pavel Roskin9d630c72011-07-28 22:50:44 -0400119static void lib80211_crypt_deinit_handler(unsigned long data)
John W. Linville274bfb82008-10-29 11:35:05 -0400120{
121 struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
122 unsigned long flags;
123
124 lib80211_crypt_deinit_entries(info, 0);
125
126 spin_lock_irqsave(info->lock, flags);
127 if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
128 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
129 "deletion list\n", info->name);
130 info->crypt_deinit_timer.expires = jiffies + HZ;
131 add_timer(&info->crypt_deinit_timer);
132 }
133 spin_unlock_irqrestore(info->lock, flags);
134}
John W. Linville274bfb82008-10-29 11:35:05 -0400135
136void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
137 struct lib80211_crypt_data **crypt)
138{
139 struct lib80211_crypt_data *tmp;
140 unsigned long flags;
141
142 if (*crypt == NULL)
143 return;
144
145 tmp = *crypt;
146 *crypt = NULL;
147
148 /* must not run ops->deinit() while there may be pending encrypt or
149 * decrypt operations. Use a list of delayed deinits to avoid needing
150 * locking. */
151
152 spin_lock_irqsave(info->lock, flags);
153 if (!info->crypt_quiesced) {
154 list_add(&tmp->list, &info->crypt_deinit_list);
155 if (!timer_pending(&info->crypt_deinit_timer)) {
156 info->crypt_deinit_timer.expires = jiffies + HZ;
157 add_timer(&info->crypt_deinit_timer);
158 }
159 }
160 spin_unlock_irqrestore(info->lock, flags);
161}
162EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
163
164int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
165{
166 unsigned long flags;
167 struct lib80211_crypto_alg *alg;
168
169 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
170 if (alg == NULL)
171 return -ENOMEM;
172
173 alg->ops = ops;
174
175 spin_lock_irqsave(&lib80211_crypto_lock, flags);
176 list_add(&alg->list, &lib80211_crypto_algs);
177 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
178
179 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
180 ops->name);
181
John W. Linville7e272fc2008-09-24 18:13:14 -0400182 return 0;
183}
John W. Linville274bfb82008-10-29 11:35:05 -0400184EXPORT_SYMBOL(lib80211_register_crypto_ops);
John W. Linville7e272fc2008-09-24 18:13:14 -0400185
John W. Linville274bfb82008-10-29 11:35:05 -0400186int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
187{
188 struct lib80211_crypto_alg *alg;
189 unsigned long flags;
190
191 spin_lock_irqsave(&lib80211_crypto_lock, flags);
192 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
193 if (alg->ops == ops)
194 goto found;
195 }
196 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
197 return -EINVAL;
198
199 found:
Joe Perchese9c02682010-11-16 19:56:49 -0800200 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
201 ops->name);
John W. Linville274bfb82008-10-29 11:35:05 -0400202 list_del(&alg->list);
203 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
204 kfree(alg);
205 return 0;
206}
207EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
208
209struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
210{
211 struct lib80211_crypto_alg *alg;
212 unsigned long flags;
213
214 spin_lock_irqsave(&lib80211_crypto_lock, flags);
215 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
216 if (strcmp(alg->ops->name, name) == 0)
217 goto found;
218 }
219 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
220 return NULL;
221
222 found:
223 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
224 return alg->ops;
225}
226EXPORT_SYMBOL(lib80211_get_crypto_ops);
227
228static void *lib80211_crypt_null_init(int keyidx)
229{
230 return (void *)1;
231}
232
233static void lib80211_crypt_null_deinit(void *priv)
John W. Linville7e272fc2008-09-24 18:13:14 -0400234{
235}
236
John W. Linville274bfb82008-10-29 11:35:05 -0400237static struct lib80211_crypto_ops lib80211_crypt_null = {
238 .name = "NULL",
239 .init = lib80211_crypt_null_init,
240 .deinit = lib80211_crypt_null_deinit,
241 .owner = THIS_MODULE,
242};
243
244static int __init lib80211_init(void)
245{
Joe Perchese9c02682010-11-16 19:56:49 -0800246 pr_info(DRV_DESCRIPTION "\n");
John W. Linville274bfb82008-10-29 11:35:05 -0400247 return lib80211_register_crypto_ops(&lib80211_crypt_null);
248}
249
250static void __exit lib80211_exit(void)
251{
252 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
253 BUG_ON(!list_empty(&lib80211_crypto_algs));
254}
255
256module_init(lib80211_init);
257module_exit(lib80211_exit);