blob: cc7b9fd5c166430e8f5ab71d93d1ddb0ef405555 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
John W. Linville7e272fc2008-09-24 18:13:14 -04002/*
3 * lib80211 -- common bits for IEEE802.11 drivers
4 *
5 * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
6 *
John W. Linville274bfb82008-10-29 11:35:05 -04007 * Portions copied from old ieee80211 component, w/ original copyright
8 * notices below:
9 *
10 * Host AP crypto routines
11 *
12 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
13 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
14 *
John W. Linville7e272fc2008-09-24 18:13:14 -040015 */
16
Joe Perchese9c02682010-11-16 19:56:49 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
John W. Linville7e272fc2008-09-24 18:13:14 -040019#include <linux/module.h>
John W. Linville2819f8a2008-09-30 17:50:31 -040020#include <linux/ctype.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040021#include <linux/ieee80211.h>
John W. Linville274bfb82008-10-29 11:35:05 -040022#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/string.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040026
27#include <net/lib80211.h>
28
29#define DRV_NAME "lib80211"
30
31#define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
32
33MODULE_DESCRIPTION(DRV_DESCRIPTION);
34MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
35MODULE_LICENSE("GPL");
36
John W. Linville274bfb82008-10-29 11:35:05 -040037struct lib80211_crypto_alg {
38 struct list_head list;
39 struct lib80211_crypto_ops *ops;
40};
41
42static LIST_HEAD(lib80211_crypto_algs);
43static DEFINE_SPINLOCK(lib80211_crypto_lock);
44
Pavel Roskin9d630c72011-07-28 22:50:44 -040045static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
46 int force);
47static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
Kees Cooke99e88a2017-10-16 14:43:17 -070048static void lib80211_crypt_deinit_handler(struct timer_list *t);
Pavel Roskin9d630c72011-07-28 22:50:44 -040049
John W. Linville2ba4b322008-11-11 16:00:06 -050050int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
51 spinlock_t *lock)
52{
53 memset(info, 0, sizeof(*info));
54
55 info->name = name;
56 info->lock = lock;
57
58 INIT_LIST_HEAD(&info->crypt_deinit_list);
Kees Cooke99e88a2017-10-16 14:43:17 -070059 timer_setup(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
60 0);
John W. Linville2ba4b322008-11-11 16:00:06 -050061
62 return 0;
63}
64EXPORT_SYMBOL(lib80211_crypt_info_init);
65
66void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
67{
68 int i;
69
70 lib80211_crypt_quiescing(info);
71 del_timer_sync(&info->crypt_deinit_timer);
72 lib80211_crypt_deinit_entries(info, 1);
73
74 for (i = 0; i < NUM_WEP_KEYS; i++) {
75 struct lib80211_crypt_data *crypt = info->crypt[i];
76 if (crypt) {
77 if (crypt->ops) {
78 crypt->ops->deinit(crypt->priv);
79 module_put(crypt->ops->owner);
80 }
81 kfree(crypt);
82 info->crypt[i] = NULL;
83 }
84 }
85}
86EXPORT_SYMBOL(lib80211_crypt_info_free);
87
Pavel Roskin9d630c72011-07-28 22:50:44 -040088static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
89 int force)
John W. Linville7e272fc2008-09-24 18:13:14 -040090{
John W. Linville274bfb82008-10-29 11:35:05 -040091 struct lib80211_crypt_data *entry, *next;
92 unsigned long flags;
93
94 spin_lock_irqsave(info->lock, flags);
95 list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
96 if (atomic_read(&entry->refcnt) != 0 && !force)
97 continue;
98
99 list_del(&entry->list);
100
101 if (entry->ops) {
102 entry->ops->deinit(entry->priv);
103 module_put(entry->ops->owner);
104 }
105 kfree(entry);
106 }
107 spin_unlock_irqrestore(info->lock, flags);
108}
John W. Linville274bfb82008-10-29 11:35:05 -0400109
110/* After this, crypt_deinit_list won't accept new members */
Pavel Roskin9d630c72011-07-28 22:50:44 -0400111static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
John W. Linville274bfb82008-10-29 11:35:05 -0400112{
113 unsigned long flags;
114
115 spin_lock_irqsave(info->lock, flags);
116 info->crypt_quiesced = 1;
117 spin_unlock_irqrestore(info->lock, flags);
118}
John W. Linville274bfb82008-10-29 11:35:05 -0400119
Kees Cooke99e88a2017-10-16 14:43:17 -0700120static void lib80211_crypt_deinit_handler(struct timer_list *t)
John W. Linville274bfb82008-10-29 11:35:05 -0400121{
Kees Cooke99e88a2017-10-16 14:43:17 -0700122 struct lib80211_crypt_info *info = from_timer(info, t,
123 crypt_deinit_timer);
John W. Linville274bfb82008-10-29 11:35:05 -0400124 unsigned long flags;
125
126 lib80211_crypt_deinit_entries(info, 0);
127
128 spin_lock_irqsave(info->lock, flags);
129 if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
130 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
131 "deletion list\n", info->name);
132 info->crypt_deinit_timer.expires = jiffies + HZ;
133 add_timer(&info->crypt_deinit_timer);
134 }
135 spin_unlock_irqrestore(info->lock, flags);
136}
John W. Linville274bfb82008-10-29 11:35:05 -0400137
138void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
139 struct lib80211_crypt_data **crypt)
140{
141 struct lib80211_crypt_data *tmp;
142 unsigned long flags;
143
144 if (*crypt == NULL)
145 return;
146
147 tmp = *crypt;
148 *crypt = NULL;
149
150 /* must not run ops->deinit() while there may be pending encrypt or
151 * decrypt operations. Use a list of delayed deinits to avoid needing
152 * locking. */
153
154 spin_lock_irqsave(info->lock, flags);
155 if (!info->crypt_quiesced) {
156 list_add(&tmp->list, &info->crypt_deinit_list);
157 if (!timer_pending(&info->crypt_deinit_timer)) {
158 info->crypt_deinit_timer.expires = jiffies + HZ;
159 add_timer(&info->crypt_deinit_timer);
160 }
161 }
162 spin_unlock_irqrestore(info->lock, flags);
163}
164EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
165
166int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
167{
168 unsigned long flags;
169 struct lib80211_crypto_alg *alg;
170
171 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
172 if (alg == NULL)
173 return -ENOMEM;
174
175 alg->ops = ops;
176
177 spin_lock_irqsave(&lib80211_crypto_lock, flags);
178 list_add(&alg->list, &lib80211_crypto_algs);
179 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
180
181 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
182 ops->name);
183
John W. Linville7e272fc2008-09-24 18:13:14 -0400184 return 0;
185}
John W. Linville274bfb82008-10-29 11:35:05 -0400186EXPORT_SYMBOL(lib80211_register_crypto_ops);
John W. Linville7e272fc2008-09-24 18:13:14 -0400187
John W. Linville274bfb82008-10-29 11:35:05 -0400188int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
189{
190 struct lib80211_crypto_alg *alg;
191 unsigned long flags;
192
193 spin_lock_irqsave(&lib80211_crypto_lock, flags);
194 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
195 if (alg->ops == ops)
196 goto found;
197 }
198 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
199 return -EINVAL;
200
201 found:
Joe Perchese9c02682010-11-16 19:56:49 -0800202 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
203 ops->name);
John W. Linville274bfb82008-10-29 11:35:05 -0400204 list_del(&alg->list);
205 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
206 kfree(alg);
207 return 0;
208}
209EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
210
211struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
212{
213 struct lib80211_crypto_alg *alg;
214 unsigned long flags;
215
216 spin_lock_irqsave(&lib80211_crypto_lock, flags);
217 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
218 if (strcmp(alg->ops->name, name) == 0)
219 goto found;
220 }
221 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
222 return NULL;
223
224 found:
225 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
226 return alg->ops;
227}
228EXPORT_SYMBOL(lib80211_get_crypto_ops);
229
230static void *lib80211_crypt_null_init(int keyidx)
231{
232 return (void *)1;
233}
234
235static void lib80211_crypt_null_deinit(void *priv)
John W. Linville7e272fc2008-09-24 18:13:14 -0400236{
237}
238
John W. Linville274bfb82008-10-29 11:35:05 -0400239static struct lib80211_crypto_ops lib80211_crypt_null = {
240 .name = "NULL",
241 .init = lib80211_crypt_null_init,
242 .deinit = lib80211_crypt_null_deinit,
243 .owner = THIS_MODULE,
244};
245
246static int __init lib80211_init(void)
247{
Joe Perchese9c02682010-11-16 19:56:49 -0800248 pr_info(DRV_DESCRIPTION "\n");
John W. Linville274bfb82008-10-29 11:35:05 -0400249 return lib80211_register_crypto_ops(&lib80211_crypt_null);
250}
251
252static void __exit lib80211_exit(void)
253{
254 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
255 BUG_ON(!list_empty(&lib80211_crypto_algs));
256}
257
258module_init(lib80211_init);
259module_exit(lib80211_exit);