[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / net / wireless / lib80211.c
blob97d411f7450707294038ad0272d7cc1d1a544e2a
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 #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)
44 const char *s = ssid;
45 char *d = buf;
47 ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
48 while (ssid_len--) {
49 if (isprint(*s)) {
50 *d++ = *s++;
51 continue;
54 *d++ = '\\';
55 if (*s == '\0')
56 *d++ = '0';
57 else if (*s == '\n')
58 *d++ = 'n';
59 else if (*s == '\r')
60 *d++ = 'r';
61 else if (*s == '\t')
62 *d++ = 't';
63 else if (*s == '\\')
64 *d++ = '\\';
65 else
66 d += snprintf(d, 3, "%03o", *s);
67 s++;
69 *d = '\0';
70 return buf;
72 EXPORT_SYMBOL(print_ssid);
74 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
75 spinlock_t *lock)
77 memset(info, 0, sizeof(*info));
79 info->name = name;
80 info->lock = lock;
82 INIT_LIST_HEAD(&info->crypt_deinit_list);
83 setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
84 (unsigned long)info);
86 return 0;
88 EXPORT_SYMBOL(lib80211_crypt_info_init);
90 void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
92 int i;
94 lib80211_crypt_quiescing(info);
95 del_timer_sync(&info->crypt_deinit_timer);
96 lib80211_crypt_deinit_entries(info, 1);
98 for (i = 0; i < NUM_WEP_KEYS; i++) {
99 struct lib80211_crypt_data *crypt = info->crypt[i];
100 if (crypt) {
101 if (crypt->ops) {
102 crypt->ops->deinit(crypt->priv);
103 module_put(crypt->ops->owner);
105 kfree(crypt);
106 info->crypt[i] = NULL;
110 EXPORT_SYMBOL(lib80211_crypt_info_free);
112 void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info, int force)
114 struct lib80211_crypt_data *entry, *next;
115 unsigned long flags;
117 spin_lock_irqsave(info->lock, flags);
118 list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
119 if (atomic_read(&entry->refcnt) != 0 && !force)
120 continue;
122 list_del(&entry->list);
124 if (entry->ops) {
125 entry->ops->deinit(entry->priv);
126 module_put(entry->ops->owner);
128 kfree(entry);
130 spin_unlock_irqrestore(info->lock, flags);
132 EXPORT_SYMBOL(lib80211_crypt_deinit_entries);
134 /* After this, crypt_deinit_list won't accept new members */
135 void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
137 unsigned long flags;
139 spin_lock_irqsave(info->lock, flags);
140 info->crypt_quiesced = 1;
141 spin_unlock_irqrestore(info->lock, flags);
143 EXPORT_SYMBOL(lib80211_crypt_quiescing);
145 void lib80211_crypt_deinit_handler(unsigned long data)
147 struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
148 unsigned long flags;
150 lib80211_crypt_deinit_entries(info, 0);
152 spin_lock_irqsave(info->lock, flags);
153 if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
154 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
155 "deletion list\n", info->name);
156 info->crypt_deinit_timer.expires = jiffies + HZ;
157 add_timer(&info->crypt_deinit_timer);
159 spin_unlock_irqrestore(info->lock, flags);
161 EXPORT_SYMBOL(lib80211_crypt_deinit_handler);
163 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
164 struct lib80211_crypt_data **crypt)
166 struct lib80211_crypt_data *tmp;
167 unsigned long flags;
169 if (*crypt == NULL)
170 return;
172 tmp = *crypt;
173 *crypt = NULL;
175 /* must not run ops->deinit() while there may be pending encrypt or
176 * decrypt operations. Use a list of delayed deinits to avoid needing
177 * locking. */
179 spin_lock_irqsave(info->lock, flags);
180 if (!info->crypt_quiesced) {
181 list_add(&tmp->list, &info->crypt_deinit_list);
182 if (!timer_pending(&info->crypt_deinit_timer)) {
183 info->crypt_deinit_timer.expires = jiffies + HZ;
184 add_timer(&info->crypt_deinit_timer);
187 spin_unlock_irqrestore(info->lock, flags);
189 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
191 int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
193 unsigned long flags;
194 struct lib80211_crypto_alg *alg;
196 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
197 if (alg == NULL)
198 return -ENOMEM;
200 alg->ops = ops;
202 spin_lock_irqsave(&lib80211_crypto_lock, flags);
203 list_add(&alg->list, &lib80211_crypto_algs);
204 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
206 printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
207 ops->name);
209 return 0;
211 EXPORT_SYMBOL(lib80211_register_crypto_ops);
213 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
215 struct lib80211_crypto_alg *alg;
216 unsigned long flags;
218 spin_lock_irqsave(&lib80211_crypto_lock, flags);
219 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
220 if (alg->ops == ops)
221 goto found;
223 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
224 return -EINVAL;
226 found:
227 printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm "
228 "'%s'\n", ops->name);
229 list_del(&alg->list);
230 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
231 kfree(alg);
232 return 0;
234 EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
236 struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
238 struct lib80211_crypto_alg *alg;
239 unsigned long flags;
241 spin_lock_irqsave(&lib80211_crypto_lock, flags);
242 list_for_each_entry(alg, &lib80211_crypto_algs, list) {
243 if (strcmp(alg->ops->name, name) == 0)
244 goto found;
246 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
247 return NULL;
249 found:
250 spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
251 return alg->ops;
253 EXPORT_SYMBOL(lib80211_get_crypto_ops);
255 static void *lib80211_crypt_null_init(int keyidx)
257 return (void *)1;
260 static void lib80211_crypt_null_deinit(void *priv)
264 static struct lib80211_crypto_ops lib80211_crypt_null = {
265 .name = "NULL",
266 .init = lib80211_crypt_null_init,
267 .deinit = lib80211_crypt_null_deinit,
268 .owner = THIS_MODULE,
271 static int __init lib80211_init(void)
273 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
274 return lib80211_register_crypto_ops(&lib80211_crypt_null);
277 static void __exit lib80211_exit(void)
279 lib80211_unregister_crypto_ops(&lib80211_crypt_null);
280 BUG_ON(!list_empty(&lib80211_crypto_algs));
283 module_init(lib80211_init);
284 module_exit(lib80211_exit);