2 * lib80211 -- common bits for IEEE802.11 drivers
4 * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
6 * Portions copied from old ieee80211 component, w/ original copyright
9 * Host AP crypto routines
11 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
12 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/ctype.h>
20 #include <linux/ieee80211.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
26 #include <net/lib80211.h>
28 #define DRV_NAME "lib80211"
30 #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
32 MODULE_DESCRIPTION(DRV_DESCRIPTION);
33 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
34 MODULE_LICENSE("GPL");
36 struct lib80211_crypto_alg {
37 struct list_head list;
38 struct lib80211_crypto_ops *ops;
41 static LIST_HEAD(lib80211_crypto_algs);
42 static DEFINE_SPINLOCK(lib80211_crypto_lock);
44 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
46 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
47 static void lib80211_crypt_deinit_handler(unsigned long data);
49 const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
54 ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
73 d += snprintf(d, 3, "%03o", *s);
79 EXPORT_SYMBOL(print_ssid);
81 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
84 memset(info, 0, sizeof(*info));
89 INIT_LIST_HEAD(&info->crypt_deinit_list);
90 setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
95 EXPORT_SYMBOL(lib80211_crypt_info_init);
97 void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
101 lib80211_crypt_quiescing(info);
102 del_timer_sync(&info->crypt_deinit_timer);
103 lib80211_crypt_deinit_entries(info, 1);
105 for (i = 0; i < NUM_WEP_KEYS; i++) {
106 struct lib80211_crypt_data *crypt = info->crypt[i];
109 crypt->ops->deinit(crypt->priv);
110 module_put(crypt->ops->owner);
113 info->crypt[i] = NULL;
117 EXPORT_SYMBOL(lib80211_crypt_info_free);
119 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
122 struct lib80211_crypt_data *entry, *next;
125 spin_lock_irqsave(info->lock, flags);
126 list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
127 if (atomic_read(&entry->refcnt) != 0 && !force)
130 list_del(&entry->list);
133 entry->ops->deinit(entry->priv);
134 module_put(entry->ops->owner);
138 spin_unlock_irqrestore(info->lock, flags);
141 /* After this, crypt_deinit_list won't accept new members */
142 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
146 spin_lock_irqsave(info->lock, flags);
147 info->crypt_quiesced = 1;
148 spin_unlock_irqrestore(info->lock, flags);
151 static void lib80211_crypt_deinit_handler(unsigned long data)
153 struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
156 lib80211_crypt_deinit_entries(info, 0);
158 spin_lock_irqsave(info->lock, flags);
159 if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
160 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
161 "deletion list\n", info->name);
162 info->crypt_deinit_timer.expires = jiffies + HZ;
163 add_timer(&info->crypt_deinit_timer);
165 spin_unlock_irqrestore(info->lock, flags);
168 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
169 struct lib80211_crypt_data **crypt)
171 struct lib80211_crypt_data *tmp;
180 /* must not run ops->deinit() while there may be pending encrypt or
181 * decrypt operations. Use a list of delayed deinits to avoid needing
184 spin_lock_irqsave(info->lock, flags);
185 if (!info->crypt_quiesced) {
186 list_add(&tmp->list, &info->crypt_deinit_list);
187 if (!timer_pending(&info->crypt_deinit_timer)) {
188 info->crypt_deinit_timer.expires = jiffies + HZ;
189 add_timer(&info->crypt_deinit_timer);
192 spin_unlock_irqrestore(info->lock, flags);
194 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
196 int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
199 struct lib80211_crypto_alg *alg;
201 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
207 spin_lock_irqsave(&lib80211_crypto_lock, flags);
208 list_add(&alg->list, &lib80211_crypto_algs);
209 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
211 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
216 EXPORT_SYMBOL(lib80211_register_crypto_ops);
218 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
220 struct lib80211_crypto_alg *alg;
223 spin_lock_irqsave(&lib80211_crypto_lock, flags);
224 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
228 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
232 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
234 list_del(&alg->list);
235 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
239 EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
241 struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
243 struct lib80211_crypto_alg *alg;
246 spin_lock_irqsave(&lib80211_crypto_lock, flags);
247 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
248 if (strcmp(alg->ops->name, name) == 0)
251 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
255 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
258 EXPORT_SYMBOL(lib80211_get_crypto_ops);
260 static void *lib80211_crypt_null_init(int keyidx)
265 static void lib80211_crypt_null_deinit(void *priv)
269 static struct lib80211_crypto_ops lib80211_crypt_null = {
271 .init = lib80211_crypt_null_init,
272 .deinit = lib80211_crypt_null_deinit,
273 .owner = THIS_MODULE,
276 static int __init lib80211_init(void)
278 pr_info(DRV_DESCRIPTION "\n");
279 return lib80211_register_crypto_ops(&lib80211_crypt_null);
282 static void __exit lib80211_exit(void)
284 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
285 BUG_ON(!list_empty(&lib80211_crypto_algs));
288 module_init(lib80211_init);
289 module_exit(lib80211_exit);