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_NAME "lib80211"
31 #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
33 MODULE_DESCRIPTION(DRV_DESCRIPTION);
34 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
35 MODULE_LICENSE("GPL");
37 struct lib80211_crypto_alg {
38 struct list_head list;
39 struct lib80211_crypto_ops *ops;
42 static LIST_HEAD(lib80211_crypto_algs);
43 static DEFINE_SPINLOCK(lib80211_crypto_lock);
45 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
47 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
48 static void lib80211_crypt_deinit_handler(struct timer_list *t);
50 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
53 memset(info, 0, sizeof(*info));
58 INIT_LIST_HEAD(&info->crypt_deinit_list);
59 timer_setup(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
64 EXPORT_SYMBOL(lib80211_crypt_info_init);
66 void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
70 lib80211_crypt_quiescing(info);
71 del_timer_sync(&info->crypt_deinit_timer);
72 lib80211_crypt_deinit_entries(info, 1);
74 for (i = 0; i < NUM_WEP_KEYS; i++) {
75 struct lib80211_crypt_data *crypt = info->crypt[i];
78 crypt->ops->deinit(crypt->priv);
79 module_put(crypt->ops->owner);
82 info->crypt[i] = NULL;
86 EXPORT_SYMBOL(lib80211_crypt_info_free);
88 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
91 struct lib80211_crypt_data *entry, *next;
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)
99 list_del(&entry->list);
102 entry->ops->deinit(entry->priv);
103 module_put(entry->ops->owner);
107 spin_unlock_irqrestore(info->lock, flags);
110 /* After this, crypt_deinit_list won't accept new members */
111 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
115 spin_lock_irqsave(info->lock, flags);
116 info->crypt_quiesced = 1;
117 spin_unlock_irqrestore(info->lock, flags);
120 static void lib80211_crypt_deinit_handler(struct timer_list *t)
122 struct lib80211_crypt_info *info = from_timer(info, t,
126 lib80211_crypt_deinit_entries(info, 0);
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);
135 spin_unlock_irqrestore(info->lock, flags);
138 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
139 struct lib80211_crypt_data **crypt)
141 struct lib80211_crypt_data *tmp;
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
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);
162 spin_unlock_irqrestore(info->lock, flags);
164 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
166 int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
169 struct lib80211_crypto_alg *alg;
171 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
177 spin_lock_irqsave(&lib80211_crypto_lock, flags);
178 list_add(&alg->list, &lib80211_crypto_algs);
179 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
181 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
186 EXPORT_SYMBOL(lib80211_register_crypto_ops);
188 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
190 struct lib80211_crypto_alg *alg;
193 spin_lock_irqsave(&lib80211_crypto_lock, flags);
194 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
198 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
202 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
204 list_del(&alg->list);
205 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
209 EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
211 struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
213 struct lib80211_crypto_alg *alg;
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)
221 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
225 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
228 EXPORT_SYMBOL(lib80211_get_crypto_ops);
230 static void *lib80211_crypt_null_init(int keyidx)
235 static void lib80211_crypt_null_deinit(void *priv)
239 static struct lib80211_crypto_ops lib80211_crypt_null = {
241 .init = lib80211_crypt_null_init,
242 .deinit = lib80211_crypt_null_deinit,
243 .owner = THIS_MODULE,
246 static int __init lib80211_init(void)
248 pr_info(DRV_DESCRIPTION "\n");
249 return lib80211_register_crypto_ops(&lib80211_crypt_null);
252 static void __exit lib80211_exit(void)
254 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
255 BUG_ON(!list_empty(&lib80211_crypto_algs));
258 module_init(lib80211_init);
259 module_exit(lib80211_exit);