blob: a55c27b75ee5667a7046ad54855f8ddd90c57b05 [file] [log] [blame]
John W. Linville7e272fc2008-09-24 18:13:14 -04001/*
2 * lib80211 -- common bits for IEEE802.11 drivers
3 *
4 * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
5 *
John W. Linville274bfb82008-10-29 11:35:05 -04006 * Portions copied from old ieee80211 component, w/ original copyright
7 * notices below:
8 *
9 * Host AP crypto routines
10 *
11 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
12 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
13 *
John W. Linville7e272fc2008-09-24 18:13:14 -040014 */
15
Joe Perchese9c02682010-11-16 19:56:49 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
John W. Linville7e272fc2008-09-24 18:13:14 -040018#include <linux/module.h>
John W. Linville2819f8a2008-09-30 17:50:31 -040019#include <linux/ctype.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040020#include <linux/ieee80211.h>
John W. Linville274bfb82008-10-29 11:35:05 -040021#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/string.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040025
26#include <net/lib80211.h>
27
28#define DRV_NAME "lib80211"
29
30#define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
31
32MODULE_DESCRIPTION(DRV_DESCRIPTION);
33MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
34MODULE_LICENSE("GPL");
35
John W. Linville274bfb82008-10-29 11:35:05 -040036struct lib80211_crypto_alg {
37 struct list_head list;
38 struct lib80211_crypto_ops *ops;
39};
40
41static LIST_HEAD(lib80211_crypto_algs);
42static DEFINE_SPINLOCK(lib80211_crypto_lock);
43
Pavel Roskin9d630c72011-07-28 22:50:44 -040044static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
45 int force);
46static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
47static void lib80211_crypt_deinit_handler(unsigned long data);
48
John W. Linville9387b7c2008-09-30 20:59:05 -040049const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
John W. Linville7e272fc2008-09-24 18:13:14 -040050{
John W. Linville7e272fc2008-09-24 18:13:14 -040051 const char *s = ssid;
John W. Linville9387b7c2008-09-30 20:59:05 -040052 char *d = buf;
John W. Linville7e272fc2008-09-24 18:13:14 -040053
John W. Linville7e272fc2008-09-24 18:13:14 -040054 ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
55 while (ssid_len--) {
John W. Linville2819f8a2008-09-30 17:50:31 -040056 if (isprint(*s)) {
John W. Linville7e272fc2008-09-24 18:13:14 -040057 *d++ = *s++;
John W. Linville2819f8a2008-09-30 17:50:31 -040058 continue;
John W. Linville7e272fc2008-09-24 18:13:14 -040059 }
John W. Linville2819f8a2008-09-30 17:50:31 -040060
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++;
John W. Linville7e272fc2008-09-24 18:13:14 -040075 }
76 *d = '\0';
John W. Linville9387b7c2008-09-30 20:59:05 -040077 return buf;
John W. Linville7e272fc2008-09-24 18:13:14 -040078}
John W. Linville9387b7c2008-09-30 20:59:05 -040079EXPORT_SYMBOL(print_ssid);
John W. Linville7e272fc2008-09-24 18:13:14 -040080
John W. Linville2ba4b322008-11-11 16:00:06 -050081int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
82 spinlock_t *lock)
83{
84 memset(info, 0, sizeof(*info));
85
86 info->name = name;
87 info->lock = lock;
88
89 INIT_LIST_HEAD(&info->crypt_deinit_list);
90 setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
91 (unsigned long)info);
92
93 return 0;
94}
95EXPORT_SYMBOL(lib80211_crypt_info_init);
96
97void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
98{
99 int i;
100
101 lib80211_crypt_quiescing(info);
102 del_timer_sync(&info->crypt_deinit_timer);
103 lib80211_crypt_deinit_entries(info, 1);
104
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);
111 }
112 kfree(crypt);
113 info->crypt[i] = NULL;
114 }
115 }
116}
117EXPORT_SYMBOL(lib80211_crypt_info_free);
118
Pavel Roskin9d630c72011-07-28 22:50:44 -0400119static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
120 int force)
John W. Linville7e272fc2008-09-24 18:13:14 -0400121{
John W. Linville274bfb82008-10-29 11:35:05 -0400122 struct lib80211_crypt_data *entry, *next;
123 unsigned long flags;
124
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;
129
130 list_del(&entry->list);
131
132 if (entry->ops) {
133 entry->ops->deinit(entry->priv);
134 module_put(entry->ops->owner);
135 }
136 kfree(entry);
137 }
138 spin_unlock_irqrestore(info->lock, flags);
139}
John W. Linville274bfb82008-10-29 11:35:05 -0400140
141/* After this, crypt_deinit_list won't accept new members */
Pavel Roskin9d630c72011-07-28 22:50:44 -0400142static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
John W. Linville274bfb82008-10-29 11:35:05 -0400143{
144 unsigned long flags;
145
146 spin_lock_irqsave(info->lock, flags);
147 info->crypt_quiesced = 1;
148 spin_unlock_irqrestore(info->lock, flags);
149}
John W. Linville274bfb82008-10-29 11:35:05 -0400150
Pavel Roskin9d630c72011-07-28 22:50:44 -0400151static void lib80211_crypt_deinit_handler(unsigned long data)
John W. Linville274bfb82008-10-29 11:35:05 -0400152{
153 struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
154 unsigned long flags;
155
156 lib80211_crypt_deinit_entries(info, 0);
157
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);
164 }
165 spin_unlock_irqrestore(info->lock, flags);
166}
John W. Linville274bfb82008-10-29 11:35:05 -0400167
168void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
169 struct lib80211_crypt_data **crypt)
170{
171 struct lib80211_crypt_data *tmp;
172 unsigned long flags;
173
174 if (*crypt == NULL)
175 return;
176
177 tmp = *crypt;
178 *crypt = NULL;
179
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. */
183
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);
190 }
191 }
192 spin_unlock_irqrestore(info->lock, flags);
193}
194EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
195
196int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
197{
198 unsigned long flags;
199 struct lib80211_crypto_alg *alg;
200
201 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
202 if (alg == NULL)
203 return -ENOMEM;
204
205 alg->ops = ops;
206
207 spin_lock_irqsave(&lib80211_crypto_lock, flags);
208 list_add(&alg->list, &lib80211_crypto_algs);
209 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
210
211 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
212 ops->name);
213
John W. Linville7e272fc2008-09-24 18:13:14 -0400214 return 0;
215}
John W. Linville274bfb82008-10-29 11:35:05 -0400216EXPORT_SYMBOL(lib80211_register_crypto_ops);
John W. Linville7e272fc2008-09-24 18:13:14 -0400217
John W. Linville274bfb82008-10-29 11:35:05 -0400218int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
219{
220 struct lib80211_crypto_alg *alg;
221 unsigned long flags;
222
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;
227 }
228 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
229 return -EINVAL;
230
231 found:
Joe Perchese9c02682010-11-16 19:56:49 -0800232 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
233 ops->name);
John W. Linville274bfb82008-10-29 11:35:05 -0400234 list_del(&alg->list);
235 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
236 kfree(alg);
237 return 0;
238}
239EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
240
241struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
242{
243 struct lib80211_crypto_alg *alg;
244 unsigned long flags;
245
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;
250 }
251 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
252 return NULL;
253
254 found:
255 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
256 return alg->ops;
257}
258EXPORT_SYMBOL(lib80211_get_crypto_ops);
259
260static void *lib80211_crypt_null_init(int keyidx)
261{
262 return (void *)1;
263}
264
265static void lib80211_crypt_null_deinit(void *priv)
John W. Linville7e272fc2008-09-24 18:13:14 -0400266{
267}
268
John W. Linville274bfb82008-10-29 11:35:05 -0400269static 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,
274};
275
276static int __init lib80211_init(void)
277{
Joe Perchese9c02682010-11-16 19:56:49 -0800278 pr_info(DRV_DESCRIPTION "\n");
John W. Linville274bfb82008-10-29 11:35:05 -0400279 return lib80211_register_crypto_ops(&lib80211_crypt_null);
280}
281
282static void __exit lib80211_exit(void)
283{
284 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
285 BUG_ON(!list_empty(&lib80211_crypto_algs));
286}
287
288module_init(lib80211_init);
289module_exit(lib80211_exit);