1*09c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27e272fcfSJohn W. Linville /*
37e272fcfSJohn W. Linville * lib80211 -- common bits for IEEE802.11 drivers
47e272fcfSJohn W. Linville *
57e272fcfSJohn W. Linville * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
67e272fcfSJohn W. Linville *
7274bfb8dSJohn W. Linville * Portions copied from old ieee80211 component, w/ original copyright
8274bfb8dSJohn W. Linville * notices below:
9274bfb8dSJohn W. Linville *
10274bfb8dSJohn W. Linville * Host AP crypto routines
11274bfb8dSJohn W. Linville *
12274bfb8dSJohn W. Linville * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
13274bfb8dSJohn W. Linville * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
14274bfb8dSJohn W. Linville *
157e272fcfSJohn W. Linville */
167e272fcfSJohn W. Linville
17e9c0268fSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18e9c0268fSJoe Perches
197e272fcfSJohn W. Linville #include <linux/module.h>
202819f8adSJohn W. Linville #include <linux/ctype.h>
217e272fcfSJohn W. Linville #include <linux/ieee80211.h>
22274bfb8dSJohn W. Linville #include <linux/errno.h>
23274bfb8dSJohn W. Linville #include <linux/init.h>
24274bfb8dSJohn W. Linville #include <linux/slab.h>
25274bfb8dSJohn W. Linville #include <linux/string.h>
267e272fcfSJohn W. Linville
277e272fcfSJohn W. Linville #include <net/lib80211.h>
287e272fcfSJohn W. Linville
297e272fcfSJohn W. Linville #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
307e272fcfSJohn W. Linville
317e272fcfSJohn W. Linville MODULE_DESCRIPTION(DRV_DESCRIPTION);
327e272fcfSJohn W. Linville MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
337e272fcfSJohn W. Linville MODULE_LICENSE("GPL");
347e272fcfSJohn W. Linville
35274bfb8dSJohn W. Linville struct lib80211_crypto_alg {
36274bfb8dSJohn W. Linville struct list_head list;
37274bfb8dSJohn W. Linville struct lib80211_crypto_ops *ops;
38274bfb8dSJohn W. Linville };
39274bfb8dSJohn W. Linville
40274bfb8dSJohn W. Linville static LIST_HEAD(lib80211_crypto_algs);
41274bfb8dSJohn W. Linville static DEFINE_SPINLOCK(lib80211_crypto_lock);
42274bfb8dSJohn W. Linville
439d630c77SPavel Roskin static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
449d630c77SPavel Roskin int force);
459d630c77SPavel Roskin static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
46e99e88a9SKees Cook static void lib80211_crypt_deinit_handler(struct timer_list *t);
479d630c77SPavel Roskin
lib80211_crypt_info_init(struct lib80211_crypt_info * info,char * name,spinlock_t * lock)482ba4b32eSJohn W. Linville int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
492ba4b32eSJohn W. Linville spinlock_t *lock)
502ba4b32eSJohn W. Linville {
512ba4b32eSJohn W. Linville memset(info, 0, sizeof(*info));
522ba4b32eSJohn W. Linville
532ba4b32eSJohn W. Linville info->name = name;
542ba4b32eSJohn W. Linville info->lock = lock;
552ba4b32eSJohn W. Linville
562ba4b32eSJohn W. Linville INIT_LIST_HEAD(&info->crypt_deinit_list);
57e99e88a9SKees Cook timer_setup(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
58e99e88a9SKees Cook 0);
592ba4b32eSJohn W. Linville
602ba4b32eSJohn W. Linville return 0;
612ba4b32eSJohn W. Linville }
622ba4b32eSJohn W. Linville EXPORT_SYMBOL(lib80211_crypt_info_init);
632ba4b32eSJohn W. Linville
lib80211_crypt_info_free(struct lib80211_crypt_info * info)642ba4b32eSJohn W. Linville void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
652ba4b32eSJohn W. Linville {
662ba4b32eSJohn W. Linville int i;
672ba4b32eSJohn W. Linville
682ba4b32eSJohn W. Linville lib80211_crypt_quiescing(info);
692ba4b32eSJohn W. Linville del_timer_sync(&info->crypt_deinit_timer);
702ba4b32eSJohn W. Linville lib80211_crypt_deinit_entries(info, 1);
712ba4b32eSJohn W. Linville
722ba4b32eSJohn W. Linville for (i = 0; i < NUM_WEP_KEYS; i++) {
732ba4b32eSJohn W. Linville struct lib80211_crypt_data *crypt = info->crypt[i];
742ba4b32eSJohn W. Linville if (crypt) {
752ba4b32eSJohn W. Linville if (crypt->ops) {
762ba4b32eSJohn W. Linville crypt->ops->deinit(crypt->priv);
772ba4b32eSJohn W. Linville module_put(crypt->ops->owner);
782ba4b32eSJohn W. Linville }
792ba4b32eSJohn W. Linville kfree(crypt);
802ba4b32eSJohn W. Linville info->crypt[i] = NULL;
812ba4b32eSJohn W. Linville }
822ba4b32eSJohn W. Linville }
832ba4b32eSJohn W. Linville }
842ba4b32eSJohn W. Linville EXPORT_SYMBOL(lib80211_crypt_info_free);
852ba4b32eSJohn W. Linville
lib80211_crypt_deinit_entries(struct lib80211_crypt_info * info,int force)869d630c77SPavel Roskin static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
879d630c77SPavel Roskin int force)
887e272fcfSJohn W. Linville {
89274bfb8dSJohn W. Linville struct lib80211_crypt_data *entry, *next;
90274bfb8dSJohn W. Linville unsigned long flags;
91274bfb8dSJohn W. Linville
92274bfb8dSJohn W. Linville spin_lock_irqsave(info->lock, flags);
93274bfb8dSJohn W. Linville list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
94274bfb8dSJohn W. Linville if (atomic_read(&entry->refcnt) != 0 && !force)
95274bfb8dSJohn W. Linville continue;
96274bfb8dSJohn W. Linville
97274bfb8dSJohn W. Linville list_del(&entry->list);
98274bfb8dSJohn W. Linville
99274bfb8dSJohn W. Linville if (entry->ops) {
100274bfb8dSJohn W. Linville entry->ops->deinit(entry->priv);
101274bfb8dSJohn W. Linville module_put(entry->ops->owner);
102274bfb8dSJohn W. Linville }
103274bfb8dSJohn W. Linville kfree(entry);
104274bfb8dSJohn W. Linville }
105274bfb8dSJohn W. Linville spin_unlock_irqrestore(info->lock, flags);
106274bfb8dSJohn W. Linville }
107274bfb8dSJohn W. Linville
108274bfb8dSJohn W. Linville /* After this, crypt_deinit_list won't accept new members */
lib80211_crypt_quiescing(struct lib80211_crypt_info * info)1099d630c77SPavel Roskin static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
110274bfb8dSJohn W. Linville {
111274bfb8dSJohn W. Linville unsigned long flags;
112274bfb8dSJohn W. Linville
113274bfb8dSJohn W. Linville spin_lock_irqsave(info->lock, flags);
114274bfb8dSJohn W. Linville info->crypt_quiesced = 1;
115274bfb8dSJohn W. Linville spin_unlock_irqrestore(info->lock, flags);
116274bfb8dSJohn W. Linville }
117274bfb8dSJohn W. Linville
lib80211_crypt_deinit_handler(struct timer_list * t)118e99e88a9SKees Cook static void lib80211_crypt_deinit_handler(struct timer_list *t)
119274bfb8dSJohn W. Linville {
120e99e88a9SKees Cook struct lib80211_crypt_info *info = from_timer(info, t,
121e99e88a9SKees Cook crypt_deinit_timer);
122274bfb8dSJohn W. Linville unsigned long flags;
123274bfb8dSJohn W. Linville
124274bfb8dSJohn W. Linville lib80211_crypt_deinit_entries(info, 0);
125274bfb8dSJohn W. Linville
126274bfb8dSJohn W. Linville spin_lock_irqsave(info->lock, flags);
127274bfb8dSJohn W. Linville if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
128274bfb8dSJohn W. Linville printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
129274bfb8dSJohn W. Linville "deletion list\n", info->name);
130274bfb8dSJohn W. Linville info->crypt_deinit_timer.expires = jiffies + HZ;
131274bfb8dSJohn W. Linville add_timer(&info->crypt_deinit_timer);
132274bfb8dSJohn W. Linville }
133274bfb8dSJohn W. Linville spin_unlock_irqrestore(info->lock, flags);
134274bfb8dSJohn W. Linville }
135274bfb8dSJohn W. Linville
lib80211_crypt_delayed_deinit(struct lib80211_crypt_info * info,struct lib80211_crypt_data ** crypt)136274bfb8dSJohn W. Linville void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
137274bfb8dSJohn W. Linville struct lib80211_crypt_data **crypt)
138274bfb8dSJohn W. Linville {
139274bfb8dSJohn W. Linville struct lib80211_crypt_data *tmp;
140274bfb8dSJohn W. Linville unsigned long flags;
141274bfb8dSJohn W. Linville
142274bfb8dSJohn W. Linville if (*crypt == NULL)
143274bfb8dSJohn W. Linville return;
144274bfb8dSJohn W. Linville
145274bfb8dSJohn W. Linville tmp = *crypt;
146274bfb8dSJohn W. Linville *crypt = NULL;
147274bfb8dSJohn W. Linville
148274bfb8dSJohn W. Linville /* must not run ops->deinit() while there may be pending encrypt or
149274bfb8dSJohn W. Linville * decrypt operations. Use a list of delayed deinits to avoid needing
150274bfb8dSJohn W. Linville * locking. */
151274bfb8dSJohn W. Linville
152274bfb8dSJohn W. Linville spin_lock_irqsave(info->lock, flags);
153274bfb8dSJohn W. Linville if (!info->crypt_quiesced) {
154274bfb8dSJohn W. Linville list_add(&tmp->list, &info->crypt_deinit_list);
155274bfb8dSJohn W. Linville if (!timer_pending(&info->crypt_deinit_timer)) {
156274bfb8dSJohn W. Linville info->crypt_deinit_timer.expires = jiffies + HZ;
157274bfb8dSJohn W. Linville add_timer(&info->crypt_deinit_timer);
158274bfb8dSJohn W. Linville }
159274bfb8dSJohn W. Linville }
160274bfb8dSJohn W. Linville spin_unlock_irqrestore(info->lock, flags);
161274bfb8dSJohn W. Linville }
162274bfb8dSJohn W. Linville EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
163274bfb8dSJohn W. Linville
lib80211_register_crypto_ops(struct lib80211_crypto_ops * ops)164274bfb8dSJohn W. Linville int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
165274bfb8dSJohn W. Linville {
166274bfb8dSJohn W. Linville unsigned long flags;
167274bfb8dSJohn W. Linville struct lib80211_crypto_alg *alg;
168274bfb8dSJohn W. Linville
169274bfb8dSJohn W. Linville alg = kzalloc(sizeof(*alg), GFP_KERNEL);
170274bfb8dSJohn W. Linville if (alg == NULL)
171274bfb8dSJohn W. Linville return -ENOMEM;
172274bfb8dSJohn W. Linville
173274bfb8dSJohn W. Linville alg->ops = ops;
174274bfb8dSJohn W. Linville
175274bfb8dSJohn W. Linville spin_lock_irqsave(&lib80211_crypto_lock, flags);
176274bfb8dSJohn W. Linville list_add(&alg->list, &lib80211_crypto_algs);
177274bfb8dSJohn W. Linville spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
178274bfb8dSJohn W. Linville
179274bfb8dSJohn W. Linville printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
180274bfb8dSJohn W. Linville ops->name);
181274bfb8dSJohn W. Linville
1827e272fcfSJohn W. Linville return 0;
1837e272fcfSJohn W. Linville }
184274bfb8dSJohn W. Linville EXPORT_SYMBOL(lib80211_register_crypto_ops);
1857e272fcfSJohn W. Linville
lib80211_unregister_crypto_ops(struct lib80211_crypto_ops * ops)186274bfb8dSJohn W. Linville int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
187274bfb8dSJohn W. Linville {
188274bfb8dSJohn W. Linville struct lib80211_crypto_alg *alg;
189274bfb8dSJohn W. Linville unsigned long flags;
190274bfb8dSJohn W. Linville
191274bfb8dSJohn W. Linville spin_lock_irqsave(&lib80211_crypto_lock, flags);
192274bfb8dSJohn W. Linville list_for_each_entry(alg, &lib80211_crypto_algs, list) {
193274bfb8dSJohn W. Linville if (alg->ops == ops)
194274bfb8dSJohn W. Linville goto found;
195274bfb8dSJohn W. Linville }
196274bfb8dSJohn W. Linville spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
197274bfb8dSJohn W. Linville return -EINVAL;
198274bfb8dSJohn W. Linville
199274bfb8dSJohn W. Linville found:
200e9c0268fSJoe Perches printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
201e9c0268fSJoe Perches ops->name);
202274bfb8dSJohn W. Linville list_del(&alg->list);
203274bfb8dSJohn W. Linville spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
204274bfb8dSJohn W. Linville kfree(alg);
205274bfb8dSJohn W. Linville return 0;
206274bfb8dSJohn W. Linville }
207274bfb8dSJohn W. Linville EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
208274bfb8dSJohn W. Linville
lib80211_get_crypto_ops(const char * name)209274bfb8dSJohn W. Linville struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
210274bfb8dSJohn W. Linville {
211274bfb8dSJohn W. Linville struct lib80211_crypto_alg *alg;
212274bfb8dSJohn W. Linville unsigned long flags;
213274bfb8dSJohn W. Linville
214274bfb8dSJohn W. Linville spin_lock_irqsave(&lib80211_crypto_lock, flags);
215274bfb8dSJohn W. Linville list_for_each_entry(alg, &lib80211_crypto_algs, list) {
216274bfb8dSJohn W. Linville if (strcmp(alg->ops->name, name) == 0)
217274bfb8dSJohn W. Linville goto found;
218274bfb8dSJohn W. Linville }
219274bfb8dSJohn W. Linville spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
220274bfb8dSJohn W. Linville return NULL;
221274bfb8dSJohn W. Linville
222274bfb8dSJohn W. Linville found:
223274bfb8dSJohn W. Linville spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
224274bfb8dSJohn W. Linville return alg->ops;
225274bfb8dSJohn W. Linville }
226274bfb8dSJohn W. Linville EXPORT_SYMBOL(lib80211_get_crypto_ops);
227274bfb8dSJohn W. Linville
lib80211_crypt_null_init(int keyidx)228274bfb8dSJohn W. Linville static void *lib80211_crypt_null_init(int keyidx)
229274bfb8dSJohn W. Linville {
230274bfb8dSJohn W. Linville return (void *)1;
231274bfb8dSJohn W. Linville }
232274bfb8dSJohn W. Linville
lib80211_crypt_null_deinit(void * priv)233274bfb8dSJohn W. Linville static void lib80211_crypt_null_deinit(void *priv)
2347e272fcfSJohn W. Linville {
2357e272fcfSJohn W. Linville }
2367e272fcfSJohn W. Linville
237274bfb8dSJohn W. Linville static struct lib80211_crypto_ops lib80211_crypt_null = {
238274bfb8dSJohn W. Linville .name = "NULL",
239274bfb8dSJohn W. Linville .init = lib80211_crypt_null_init,
240274bfb8dSJohn W. Linville .deinit = lib80211_crypt_null_deinit,
241274bfb8dSJohn W. Linville .owner = THIS_MODULE,
242274bfb8dSJohn W. Linville };
243274bfb8dSJohn W. Linville
lib80211_init(void)244274bfb8dSJohn W. Linville static int __init lib80211_init(void)
245274bfb8dSJohn W. Linville {
246e9c0268fSJoe Perches pr_info(DRV_DESCRIPTION "\n");
247274bfb8dSJohn W. Linville return lib80211_register_crypto_ops(&lib80211_crypt_null);
248274bfb8dSJohn W. Linville }
249274bfb8dSJohn W. Linville
lib80211_exit(void)250274bfb8dSJohn W. Linville static void __exit lib80211_exit(void)
251274bfb8dSJohn W. Linville {
252274bfb8dSJohn W. Linville lib80211_unregister_crypto_ops(&lib80211_crypt_null);
253274bfb8dSJohn W. Linville BUG_ON(!list_empty(&lib80211_crypto_algs));
254274bfb8dSJohn W. Linville }
255274bfb8dSJohn W. Linville
256274bfb8dSJohn W. Linville module_init(lib80211_init);
257274bfb8dSJohn W. Linville module_exit(lib80211_exit);
258