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 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/ieee80211.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
24 #include <net/lib80211.h>
26 #define DRV_NAME "lib80211"
28 #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
30 MODULE_DESCRIPTION(DRV_DESCRIPTION);
31 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
32 MODULE_LICENSE("GPL");
34 struct lib80211_crypto_alg {
35 struct list_head list;
36 struct lib80211_crypto_ops *ops;
39 static LIST_HEAD(lib80211_crypto_algs);
40 static DEFINE_SPINLOCK(lib80211_crypto_lock);
42 const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
47 ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
66 d += snprintf(d, 3, "%03o", *s);
72 EXPORT_SYMBOL(print_ssid);
74 void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info, int force)
76 struct lib80211_crypt_data *entry, *next;
79 spin_lock_irqsave(info->lock, flags);
80 list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
81 if (atomic_read(&entry->refcnt) != 0 && !force)
84 list_del(&entry->list);
87 entry->ops->deinit(entry->priv);
88 module_put(entry->ops->owner);
92 spin_unlock_irqrestore(info->lock, flags);
94 EXPORT_SYMBOL(lib80211_crypt_deinit_entries);
96 /* After this, crypt_deinit_list won't accept new members */
97 void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
101 spin_lock_irqsave(info->lock, flags);
102 info->crypt_quiesced = 1;
103 spin_unlock_irqrestore(info->lock, flags);
105 EXPORT_SYMBOL(lib80211_crypt_quiescing);
107 void lib80211_crypt_deinit_handler(unsigned long data)
109 struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
112 lib80211_crypt_deinit_entries(info, 0);
114 spin_lock_irqsave(info->lock, flags);
115 if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
116 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
117 "deletion list\n", info->name);
118 info->crypt_deinit_timer.expires = jiffies + HZ;
119 add_timer(&info->crypt_deinit_timer);
121 spin_unlock_irqrestore(info->lock, flags);
123 EXPORT_SYMBOL(lib80211_crypt_deinit_handler);
125 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
126 struct lib80211_crypt_data **crypt)
128 struct lib80211_crypt_data *tmp;
137 /* must not run ops->deinit() while there may be pending encrypt or
138 * decrypt operations. Use a list of delayed deinits to avoid needing
141 spin_lock_irqsave(info->lock, flags);
142 if (!info->crypt_quiesced) {
143 list_add(&tmp->list, &info->crypt_deinit_list);
144 if (!timer_pending(&info->crypt_deinit_timer)) {
145 info->crypt_deinit_timer.expires = jiffies + HZ;
146 add_timer(&info->crypt_deinit_timer);
149 spin_unlock_irqrestore(info->lock, flags);
151 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
153 int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
156 struct lib80211_crypto_alg *alg;
158 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
164 spin_lock_irqsave(&lib80211_crypto_lock, flags);
165 list_add(&alg->list, &lib80211_crypto_algs);
166 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
168 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
173 EXPORT_SYMBOL(lib80211_register_crypto_ops);
175 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
177 struct lib80211_crypto_alg *alg;
180 spin_lock_irqsave(&lib80211_crypto_lock, flags);
181 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
185 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
189 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm "
190 "'%s'\n", ops->name);
191 list_del(&alg->list);
192 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
196 EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
198 struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
200 struct lib80211_crypto_alg *alg;
203 spin_lock_irqsave(&lib80211_crypto_lock, flags);
204 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
205 if (strcmp(alg->ops->name, name) == 0)
208 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
212 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
215 EXPORT_SYMBOL(lib80211_get_crypto_ops);
217 static void *lib80211_crypt_null_init(int keyidx)
222 static void lib80211_crypt_null_deinit(void *priv)
226 static struct lib80211_crypto_ops lib80211_crypt_null = {
228 .init = lib80211_crypt_null_init,
229 .deinit = lib80211_crypt_null_deinit,
230 .owner = THIS_MODULE,
233 static int __init lib80211_init(void)
235 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
236 return lib80211_register_crypto_ops(&lib80211_crypt_null);
239 static void __exit lib80211_exit(void)
241 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
242 BUG_ON(!list_empty(&lib80211_crypto_algs));
245 module_init(lib80211_init);
246 module_exit(lib80211_exit);