1 // SPDX-License-Identifier: GPL-2.0-only
3 * lib80211 -- common bits for IEEE802.11 drivers
5 * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
7 * Portions copied from old ieee80211 component, w/ original copyright
10 * Host AP crypto routines
12 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
13 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/ctype.h>
21 #include <linux/ieee80211.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
27 #include <net/lib80211.h>
29 #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
31 MODULE_DESCRIPTION(DRV_DESCRIPTION);
32 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
33 MODULE_LICENSE("GPL");
35 struct lib80211_crypto_alg {
36 struct list_head list;
37 struct lib80211_crypto_ops *ops;
40 static LIST_HEAD(lib80211_crypto_algs);
41 static DEFINE_SPINLOCK(lib80211_crypto_lock);
43 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
45 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
46 static void lib80211_crypt_deinit_handler(struct timer_list *t);
48 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
51 memset(info, 0, sizeof(*info));
56 INIT_LIST_HEAD(&info->crypt_deinit_list);
57 timer_setup(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
62 EXPORT_SYMBOL(lib80211_crypt_info_init);
64 void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
68 lib80211_crypt_quiescing(info);
69 del_timer_sync(&info->crypt_deinit_timer);
70 lib80211_crypt_deinit_entries(info, 1);
72 for (i = 0; i < NUM_WEP_KEYS; i++) {
73 struct lib80211_crypt_data *crypt = info->crypt[i];
76 crypt->ops->deinit(crypt->priv);
77 module_put(crypt->ops->owner);
80 info->crypt[i] = NULL;
84 EXPORT_SYMBOL(lib80211_crypt_info_free);
86 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
89 struct lib80211_crypt_data *entry, *next;
92 spin_lock_irqsave(info->lock, flags);
93 list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
94 if (atomic_read(&entry->refcnt) != 0 && !force)
97 list_del(&entry->list);
100 entry->ops->deinit(entry->priv);
101 module_put(entry->ops->owner);
105 spin_unlock_irqrestore(info->lock, flags);
108 /* After this, crypt_deinit_list won't accept new members */
109 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
113 spin_lock_irqsave(info->lock, flags);
114 info->crypt_quiesced = 1;
115 spin_unlock_irqrestore(info->lock, flags);
118 static void lib80211_crypt_deinit_handler(struct timer_list *t)
120 struct lib80211_crypt_info *info = from_timer(info, t,
124 lib80211_crypt_deinit_entries(info, 0);
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);
133 spin_unlock_irqrestore(info->lock, flags);
136 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
137 struct lib80211_crypt_data **crypt)
139 struct lib80211_crypt_data *tmp;
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
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);
160 spin_unlock_irqrestore(info->lock, flags);
162 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
164 int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
167 struct lib80211_crypto_alg *alg;
169 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
175 spin_lock_irqsave(&lib80211_crypto_lock, flags);
176 list_add(&alg->list, &lib80211_crypto_algs);
177 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
179 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
184 EXPORT_SYMBOL(lib80211_register_crypto_ops);
186 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
188 struct lib80211_crypto_alg *alg;
191 spin_lock_irqsave(&lib80211_crypto_lock, flags);
192 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
196 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
200 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
202 list_del(&alg->list);
203 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
207 EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
209 struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
211 struct lib80211_crypto_alg *alg;
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)
219 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
223 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
226 EXPORT_SYMBOL(lib80211_get_crypto_ops);
228 static void *lib80211_crypt_null_init(int keyidx)
233 static void lib80211_crypt_null_deinit(void *priv)
237 static struct lib80211_crypto_ops lib80211_crypt_null = {
239 .init = lib80211_crypt_null_init,
240 .deinit = lib80211_crypt_null_deinit,
241 .owner = THIS_MODULE,
244 static int __init lib80211_init(void)
246 pr_info(DRV_DESCRIPTION "\n");
247 return lib80211_register_crypto_ops(&lib80211_crypt_null);
250 static void __exit lib80211_exit(void)
252 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
253 BUG_ON(!list_empty(&lib80211_crypto_algs));
256 module_init(lib80211_init);
257 module_exit(lib80211_exit);