blob: 801d4781a73b6724ce06f95c3be709bb9cd53174 [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);
Kees Cooke99e88a2017-10-16 14:43:17 -070047static void lib80211_crypt_deinit_handler(struct timer_list *t);
Pavel Roskin9d630c72011-07-28 22:50:44 -040048
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);
Kees Cooke99e88a2017-10-16 14:43:17 -070058 timer_setup(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
59 0);
John W. Linville2ba4b322008-11-11 16:00:06 -050060
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
Kees Cooke99e88a2017-10-16 14:43:17 -0700119static void lib80211_crypt_deinit_handler(struct timer_list *t)
John W. Linville274bfb82008-10-29 11:35:05 -0400120{
Kees Cooke99e88a2017-10-16 14:43:17 -0700121 struct lib80211_crypt_info *info = from_timer(info, t,
122 crypt_deinit_timer);
John W. Linville274bfb82008-10-29 11:35:05 -0400123 unsigned long flags;
124
125 lib80211_crypt_deinit_entries(info, 0);
126
127 spin_lock_irqsave(info->lock, flags);
128 if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
129 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
130 "deletion list\n", info->name);
131 info->crypt_deinit_timer.expires = jiffies + HZ;
132 add_timer(&info->crypt_deinit_timer);
133 }
134 spin_unlock_irqrestore(info->lock, flags);
135}
John W. Linville274bfb82008-10-29 11:35:05 -0400136
137void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
138 struct lib80211_crypt_data **crypt)
139{
140 struct lib80211_crypt_data *tmp;
141 unsigned long flags;
142
143 if (*crypt == NULL)
144 return;
145
146 tmp = *crypt;
147 *crypt = NULL;
148
149 /* must not run ops->deinit() while there may be pending encrypt or
150 * decrypt operations. Use a list of delayed deinits to avoid needing
151 * locking. */
152
153 spin_lock_irqsave(info->lock, flags);
154 if (!info->crypt_quiesced) {
155 list_add(&tmp->list, &info->crypt_deinit_list);
156 if (!timer_pending(&info->crypt_deinit_timer)) {
157 info->crypt_deinit_timer.expires = jiffies + HZ;
158 add_timer(&info->crypt_deinit_timer);
159 }
160 }
161 spin_unlock_irqrestore(info->lock, flags);
162}
163EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
164
165int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
166{
167 unsigned long flags;
168 struct lib80211_crypto_alg *alg;
169
170 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
171 if (alg == NULL)
172 return -ENOMEM;
173
174 alg->ops = ops;
175
176 spin_lock_irqsave(&lib80211_crypto_lock, flags);
177 list_add(&alg->list, &lib80211_crypto_algs);
178 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
179
180 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
181 ops->name);
182
John W. Linville7e272fc2008-09-24 18:13:14 -0400183 return 0;
184}
John W. Linville274bfb82008-10-29 11:35:05 -0400185EXPORT_SYMBOL(lib80211_register_crypto_ops);
John W. Linville7e272fc2008-09-24 18:13:14 -0400186
John W. Linville274bfb82008-10-29 11:35:05 -0400187int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
188{
189 struct lib80211_crypto_alg *alg;
190 unsigned long flags;
191
192 spin_lock_irqsave(&lib80211_crypto_lock, flags);
193 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
194 if (alg->ops == ops)
195 goto found;
196 }
197 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
198 return -EINVAL;
199
200 found:
Joe Perchese9c02682010-11-16 19:56:49 -0800201 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
202 ops->name);
John W. Linville274bfb82008-10-29 11:35:05 -0400203 list_del(&alg->list);
204 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
205 kfree(alg);
206 return 0;
207}
208EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
209
210struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
211{
212 struct lib80211_crypto_alg *alg;
213 unsigned long flags;
214
215 spin_lock_irqsave(&lib80211_crypto_lock, flags);
216 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
217 if (strcmp(alg->ops->name, name) == 0)
218 goto found;
219 }
220 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
221 return NULL;
222
223 found:
224 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
225 return alg->ops;
226}
227EXPORT_SYMBOL(lib80211_get_crypto_ops);
228
229static void *lib80211_crypt_null_init(int keyidx)
230{
231 return (void *)1;
232}
233
234static void lib80211_crypt_null_deinit(void *priv)
John W. Linville7e272fc2008-09-24 18:13:14 -0400235{
236}
237
John W. Linville274bfb82008-10-29 11:35:05 -0400238static struct lib80211_crypto_ops lib80211_crypt_null = {
239 .name = "NULL",
240 .init = lib80211_crypt_null_init,
241 .deinit = lib80211_crypt_null_deinit,
242 .owner = THIS_MODULE,
243};
244
245static int __init lib80211_init(void)
246{
Joe Perchese9c02682010-11-16 19:56:49 -0800247 pr_info(DRV_DESCRIPTION "\n");
John W. Linville274bfb82008-10-29 11:35:05 -0400248 return lib80211_register_crypto_ops(&lib80211_crypt_null);
249}
250
251static void __exit lib80211_exit(void)
252{
253 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
254 BUG_ON(!list_empty(&lib80211_crypto_algs));
255}
256
257module_init(lib80211_init);
258module_exit(lib80211_exit);