[PATCH] ieee80211_geo.c: remove frivolous BUG_ON's
[linux-2.6/mini2440.git] / net / ieee80211 / ieee80211_crypt.c
blobcb71d794a7d1711af7289417738a823de830d6f9
1 /*
2 * Host AP crypto routines
4 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. See README and COPYING for
10 * more details.
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <net/ieee80211.h>
21 MODULE_AUTHOR("Jouni Malinen");
22 MODULE_DESCRIPTION("HostAP crypto");
23 MODULE_LICENSE("GPL");
25 struct ieee80211_crypto_alg {
26 struct list_head list;
27 struct ieee80211_crypto_ops *ops;
30 static LIST_HEAD(ieee80211_crypto_algs);
31 static DEFINE_SPINLOCK(ieee80211_crypto_lock);
33 void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force)
35 struct ieee80211_crypt_data *entry, *next;
36 unsigned long flags;
38 spin_lock_irqsave(&ieee->lock, flags);
39 list_for_each_entry_safe(entry, next, &ieee->crypt_deinit_list, list) {
40 if (atomic_read(&entry->refcnt) != 0 && !force)
41 continue;
43 list_del(&entry->list);
45 if (entry->ops) {
46 entry->ops->deinit(entry->priv);
47 module_put(entry->ops->owner);
49 kfree(entry);
51 spin_unlock_irqrestore(&ieee->lock, flags);
54 /* After this, crypt_deinit_list won't accept new members */
55 void ieee80211_crypt_quiescing(struct ieee80211_device *ieee)
57 unsigned long flags;
59 spin_lock_irqsave(&ieee->lock, flags);
60 ieee->crypt_quiesced = 1;
61 spin_unlock_irqrestore(&ieee->lock, flags);
64 void ieee80211_crypt_deinit_handler(unsigned long data)
66 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
67 unsigned long flags;
69 ieee80211_crypt_deinit_entries(ieee, 0);
71 spin_lock_irqsave(&ieee->lock, flags);
72 if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) {
73 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
74 "deletion list\n", ieee->dev->name);
75 ieee->crypt_deinit_timer.expires = jiffies + HZ;
76 add_timer(&ieee->crypt_deinit_timer);
78 spin_unlock_irqrestore(&ieee->lock, flags);
81 void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
82 struct ieee80211_crypt_data **crypt)
84 struct ieee80211_crypt_data *tmp;
85 unsigned long flags;
87 if (*crypt == NULL)
88 return;
90 tmp = *crypt;
91 *crypt = NULL;
93 /* must not run ops->deinit() while there may be pending encrypt or
94 * decrypt operations. Use a list of delayed deinits to avoid needing
95 * locking. */
97 spin_lock_irqsave(&ieee->lock, flags);
98 if (!ieee->crypt_quiesced) {
99 list_add(&tmp->list, &ieee->crypt_deinit_list);
100 if (!timer_pending(&ieee->crypt_deinit_timer)) {
101 ieee->crypt_deinit_timer.expires = jiffies + HZ;
102 add_timer(&ieee->crypt_deinit_timer);
105 spin_unlock_irqrestore(&ieee->lock, flags);
108 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
110 unsigned long flags;
111 struct ieee80211_crypto_alg *alg;
113 alg = kmalloc(sizeof(*alg), GFP_KERNEL);
114 if (alg == NULL)
115 return -ENOMEM;
117 memset(alg, 0, sizeof(*alg));
118 alg->ops = ops;
120 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
121 list_add(&alg->list, &ieee80211_crypto_algs);
122 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
124 printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
125 ops->name);
127 return 0;
130 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
132 struct ieee80211_crypto_alg *alg;
133 unsigned long flags;
135 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
136 list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
137 if (alg->ops == ops)
138 goto found;
140 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
141 return -EINVAL;
143 found:
144 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
145 "'%s'\n", ops->name);
146 list_del(&alg->list);
147 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
148 kfree(alg);
149 return 0;
152 struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
154 struct ieee80211_crypto_alg *alg;
155 unsigned long flags;
157 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
158 list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
159 if (strcmp(alg->ops->name, name) == 0)
160 goto found;
162 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
163 return NULL;
165 found:
166 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
167 return alg->ops;
170 static void *ieee80211_crypt_null_init(int keyidx)
172 return (void *)1;
175 static void ieee80211_crypt_null_deinit(void *priv)
179 static struct ieee80211_crypto_ops ieee80211_crypt_null = {
180 .name = "NULL",
181 .init = ieee80211_crypt_null_init,
182 .deinit = ieee80211_crypt_null_deinit,
183 .owner = THIS_MODULE,
186 static int __init ieee80211_crypto_init(void)
188 return ieee80211_register_crypto_ops(&ieee80211_crypt_null);
191 static void __exit ieee80211_crypto_deinit(void)
193 ieee80211_unregister_crypto_ops(&ieee80211_crypt_null);
194 BUG_ON(!list_empty(&ieee80211_crypto_algs));
197 EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
198 EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
199 EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
200 EXPORT_SYMBOL(ieee80211_crypt_quiescing);
202 EXPORT_SYMBOL(ieee80211_register_crypto_ops);
203 EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
204 EXPORT_SYMBOL(ieee80211_get_crypto_ops);
206 module_init(ieee80211_crypto_init);
207 module_exit(ieee80211_crypto_deinit);