blob: 5ed0a98b2d7680ae2d3a8b169a4e5664842b0444 [file] [log] [blame]
Jeff Garzikb4538722005-05-12 22:48:20 -04001/*
2 * Host AP crypto routines
3 *
4 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. See README and COPYING for
10 * more details.
11 *
12 */
13
Christoph Hellwige3305622005-11-09 01:01:04 -050014#include <linux/errno.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
Christoph Hellwige3305622005-11-09 01:01:04 -050018#include <linux/string.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040019#include <net/ieee80211.h>
20
21MODULE_AUTHOR("Jouni Malinen");
22MODULE_DESCRIPTION("HostAP crypto");
23MODULE_LICENSE("GPL");
24
25struct ieee80211_crypto_alg {
26 struct list_head list;
27 struct ieee80211_crypto_ops *ops;
28};
29
Christoph Hellwige3305622005-11-09 01:01:04 -050030static LIST_HEAD(ieee80211_crypto_algs);
31static DEFINE_SPINLOCK(ieee80211_crypto_lock);
Jeff Garzikb4538722005-05-12 22:48:20 -040032
Jeff Garzik0edd5b42005-09-07 00:48:31 -040033void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force)
Jeff Garzikb4538722005-05-12 22:48:20 -040034{
Zhu Yid6529232006-01-19 16:20:49 +080035 struct ieee80211_crypt_data *entry, *next;
James Ketrenos20d64712005-09-21 11:53:43 -050036 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -040037
James Ketrenos20d64712005-09-21 11:53:43 -050038 spin_lock_irqsave(&ieee->lock, flags);
Zhu Yid6529232006-01-19 16:20:49 +080039 list_for_each_entry_safe(entry, next, &ieee->crypt_deinit_list, list) {
Jeff Garzikb4538722005-05-12 22:48:20 -040040 if (atomic_read(&entry->refcnt) != 0 && !force)
41 continue;
42
Christoph Hellwige3305622005-11-09 01:01:04 -050043 list_del(&entry->list);
Jeff Garzikb4538722005-05-12 22:48:20 -040044
45 if (entry->ops) {
46 entry->ops->deinit(entry->priv);
47 module_put(entry->ops->owner);
48 }
49 kfree(entry);
50 }
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050051 spin_unlock_irqrestore(&ieee->lock, flags);
52}
53
54/* After this, crypt_deinit_list won't accept new members */
55void ieee80211_crypt_quiescing(struct ieee80211_device *ieee)
56{
57 unsigned long flags;
58
59 spin_lock_irqsave(&ieee->lock, flags);
60 ieee->crypt_quiesced = 1;
James Ketrenos20d64712005-09-21 11:53:43 -050061 spin_unlock_irqrestore(&ieee->lock, flags);
Jeff Garzikb4538722005-05-12 22:48:20 -040062}
63
64void ieee80211_crypt_deinit_handler(unsigned long data)
65{
66 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050067 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -040068
Jeff Garzikb4538722005-05-12 22:48:20 -040069 ieee80211_crypt_deinit_entries(ieee, 0);
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050070
71 spin_lock_irqsave(&ieee->lock, flags);
72 if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) {
Jeff Garzikb4538722005-05-12 22:48:20 -040073 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
74 "deletion list\n", ieee->dev->name);
75 ieee->crypt_deinit_timer.expires = jiffies + HZ;
76 add_timer(&ieee->crypt_deinit_timer);
77 }
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050078 spin_unlock_irqrestore(&ieee->lock, flags);
Jeff Garzikb4538722005-05-12 22:48:20 -040079}
80
81void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
82 struct ieee80211_crypt_data **crypt)
83{
84 struct ieee80211_crypt_data *tmp;
85 unsigned long flags;
86
87 if (*crypt == NULL)
88 return;
89
90 tmp = *crypt;
91 *crypt = NULL;
92
93 /* must not run ops->deinit() while there may be pending encrypt or
94 * decrypt operations. Use a list of delayed deinits to avoid needing
95 * locking. */
96
97 spin_lock_irqsave(&ieee->lock, flags);
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050098 if (!ieee->crypt_quiesced) {
99 list_add(&tmp->list, &ieee->crypt_deinit_list);
100 if (!timer_pending(&ieee->crypt_deinit_timer)) {
101 ieee->crypt_deinit_timer.expires = jiffies + HZ;
102 add_timer(&ieee->crypt_deinit_timer);
103 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400104 }
105 spin_unlock_irqrestore(&ieee->lock, flags);
106}
107
108int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
109{
110 unsigned long flags;
111 struct ieee80211_crypto_alg *alg;
112
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700113 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
Jeff Garzikb4538722005-05-12 22:48:20 -0400114 if (alg == NULL)
115 return -ENOMEM;
116
Jeff Garzikb4538722005-05-12 22:48:20 -0400117 alg->ops = ops;
118
Christoph Hellwige3305622005-11-09 01:01:04 -0500119 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
120 list_add(&alg->list, &ieee80211_crypto_algs);
121 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
Jeff Garzikb4538722005-05-12 22:48:20 -0400122
123 printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
124 ops->name);
125
126 return 0;
127}
128
129int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
130{
Christoph Hellwige3305622005-11-09 01:01:04 -0500131 struct ieee80211_crypto_alg *alg;
Jeff Garzikb4538722005-05-12 22:48:20 -0400132 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -0400133
Christoph Hellwige3305622005-11-09 01:01:04 -0500134 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
135 list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
136 if (alg->ops == ops)
137 goto found;
Jeff Garzikb4538722005-05-12 22:48:20 -0400138 }
Christoph Hellwige3305622005-11-09 01:01:04 -0500139 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
140 return -EINVAL;
Jeff Garzikb4538722005-05-12 22:48:20 -0400141
Zhu Yid6529232006-01-19 16:20:49 +0800142 found:
Christoph Hellwige3305622005-11-09 01:01:04 -0500143 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
Zhu Yid6529232006-01-19 16:20:49 +0800144 "'%s'\n", ops->name);
Christoph Hellwige3305622005-11-09 01:01:04 -0500145 list_del(&alg->list);
146 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
147 kfree(alg);
148 return 0;
Jeff Garzikb4538722005-05-12 22:48:20 -0400149}
150
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400151struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
Jeff Garzikb4538722005-05-12 22:48:20 -0400152{
Christoph Hellwige3305622005-11-09 01:01:04 -0500153 struct ieee80211_crypto_alg *alg;
Jeff Garzikb4538722005-05-12 22:48:20 -0400154 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -0400155
Christoph Hellwige3305622005-11-09 01:01:04 -0500156 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
157 list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
158 if (strcmp(alg->ops->name, name) == 0)
159 goto found;
Jeff Garzikb4538722005-05-12 22:48:20 -0400160 }
Christoph Hellwige3305622005-11-09 01:01:04 -0500161 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
162 return NULL;
Jeff Garzikb4538722005-05-12 22:48:20 -0400163
Zhu Yid6529232006-01-19 16:20:49 +0800164 found:
Christoph Hellwige3305622005-11-09 01:01:04 -0500165 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
166 return alg->ops;
Jeff Garzikb4538722005-05-12 22:48:20 -0400167}
168
James Ketrenos6eb6edf2005-09-22 10:34:15 +0000169static void *ieee80211_crypt_null_init(int keyidx)
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400170{
171 return (void *)1;
172}
Christoph Hellwige3305622005-11-09 01:01:04 -0500173
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400174static void ieee80211_crypt_null_deinit(void *priv)
175{
176}
Jeff Garzikb4538722005-05-12 22:48:20 -0400177
178static struct ieee80211_crypto_ops ieee80211_crypt_null = {
James Ketrenos74079fd2005-09-13 17:35:21 -0500179 .name = "NULL",
180 .init = ieee80211_crypt_null_init,
181 .deinit = ieee80211_crypt_null_deinit,
James Ketrenos74079fd2005-09-13 17:35:21 -0500182 .owner = THIS_MODULE,
Jeff Garzikb4538722005-05-12 22:48:20 -0400183};
184
Jeff Garzikb4538722005-05-12 22:48:20 -0400185static int __init ieee80211_crypto_init(void)
186{
Christoph Hellwige3305622005-11-09 01:01:04 -0500187 return ieee80211_register_crypto_ops(&ieee80211_crypt_null);
Jeff Garzikb4538722005-05-12 22:48:20 -0400188}
189
Jeff Garzikb4538722005-05-12 22:48:20 -0400190static void __exit ieee80211_crypto_deinit(void)
191{
Christoph Hellwige3305622005-11-09 01:01:04 -0500192 ieee80211_unregister_crypto_ops(&ieee80211_crypt_null);
193 BUG_ON(!list_empty(&ieee80211_crypto_algs));
Jeff Garzikb4538722005-05-12 22:48:20 -0400194}
195
196EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
197EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
198EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
James Ketrenos0ad0c3c2005-09-21 11:54:15 -0500199EXPORT_SYMBOL(ieee80211_crypt_quiescing);
Jeff Garzikb4538722005-05-12 22:48:20 -0400200
201EXPORT_SYMBOL(ieee80211_register_crypto_ops);
202EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
203EXPORT_SYMBOL(ieee80211_get_crypto_ops);
204
205module_init(ieee80211_crypto_init);
206module_exit(ieee80211_crypto_deinit);