Merge branch 'for-3.2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[zen-stable.git] / net / wireless / lib80211.c
bloba55c27b75ee5667a7046ad54855f8ddd90c57b05
1 /*
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
7 * notices below:
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,
45 int force);
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)
51 const char *s = ssid;
52 char *d = buf;
54 ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
55 while (ssid_len--) {
56 if (isprint(*s)) {
57 *d++ = *s++;
58 continue;
61 *d++ = '\\';
62 if (*s == '\0')
63 *d++ = '0';
64 else if (*s == '\n')
65 *d++ = 'n';
66 else if (*s == '\r')
67 *d++ = 'r';
68 else if (*s == '\t')
69 *d++ = 't';
70 else if (*s == '\\')
71 *d++ = '\\';
72 else
73 d += snprintf(d, 3, "%03o", *s);
74 s++;
76 *d = '\0';
77 return buf;
79 EXPORT_SYMBOL(print_ssid);
81 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
82 spinlock_t *lock)
84 memset(info, 0, sizeof(*info));
86 info->name = name;
87 info->lock = lock;
89 INIT_LIST_HEAD(&info->crypt_deinit_list);
90 setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
91 (unsigned long)info);
93 return 0;
95 EXPORT_SYMBOL(lib80211_crypt_info_init);
97 void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
99 int i;
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];
107 if (crypt) {
108 if (crypt->ops) {
109 crypt->ops->deinit(crypt->priv);
110 module_put(crypt->ops->owner);
112 kfree(crypt);
113 info->crypt[i] = NULL;
117 EXPORT_SYMBOL(lib80211_crypt_info_free);
119 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
120 int force)
122 struct lib80211_crypt_data *entry, *next;
123 unsigned long flags;
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)
128 continue;
130 list_del(&entry->list);
132 if (entry->ops) {
133 entry->ops->deinit(entry->priv);
134 module_put(entry->ops->owner);
136 kfree(entry);
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)
144 unsigned long flags;
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;
154 unsigned long flags;
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;
172 unsigned long flags;
174 if (*crypt == NULL)
175 return;
177 tmp = *crypt;
178 *crypt = NULL;
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
182 * locking. */
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)
198 unsigned long flags;
199 struct lib80211_crypto_alg *alg;
201 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
202 if (alg == NULL)
203 return -ENOMEM;
205 alg->ops = ops;
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",
212 ops->name);
214 return 0;
216 EXPORT_SYMBOL(lib80211_register_crypto_ops);
218 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
220 struct lib80211_crypto_alg *alg;
221 unsigned long flags;
223 spin_lock_irqsave(&lib80211_crypto_lock, flags);
224 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
225 if (alg->ops == ops)
226 goto found;
228 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
229 return -EINVAL;
231 found:
232 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
233 ops->name);
234 list_del(&alg->list);
235 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
236 kfree(alg);
237 return 0;
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;
244 unsigned long flags;
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)
249 goto found;
251 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
252 return NULL;
254 found:
255 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
256 return alg->ops;
258 EXPORT_SYMBOL(lib80211_get_crypto_ops);
260 static void *lib80211_crypt_null_init(int keyidx)
262 return (void *)1;
265 static void lib80211_crypt_null_deinit(void *priv)
269 static struct lib80211_crypto_ops lib80211_crypt_null = {
270 .name = "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);