blob: ecc9bb196abcb332892848086456e9c519b2e29e [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
Christoph Hellwige3305622005-11-09 01:01:04 -050021
Jeff Garzikb4538722005-05-12 22:48:20 -040022MODULE_AUTHOR("Jouni Malinen");
23MODULE_DESCRIPTION("HostAP crypto");
24MODULE_LICENSE("GPL");
25
26struct ieee80211_crypto_alg {
27 struct list_head list;
28 struct ieee80211_crypto_ops *ops;
29};
30
Christoph Hellwige3305622005-11-09 01:01:04 -050031static LIST_HEAD(ieee80211_crypto_algs);
32static DEFINE_SPINLOCK(ieee80211_crypto_lock);
Jeff Garzikb4538722005-05-12 22:48:20 -040033
Jeff Garzik0edd5b42005-09-07 00:48:31 -040034void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force)
Jeff Garzikb4538722005-05-12 22:48:20 -040035{
Christoph Hellwige3305622005-11-09 01:01:04 -050036 struct ieee80211_crypt_data *entry, *next;
James Ketrenos20d64712005-09-21 11:53:43 -050037 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -040038
James Ketrenos20d64712005-09-21 11:53:43 -050039 spin_lock_irqsave(&ieee->lock, flags);
Christoph Hellwige3305622005-11-09 01:01:04 -050040 list_for_each_entry_safe(entry, next, &ieee->crypt_deinit_list, list) {
Jeff Garzikb4538722005-05-12 22:48:20 -040041 if (atomic_read(&entry->refcnt) != 0 && !force)
42 continue;
43
Christoph Hellwige3305622005-11-09 01:01:04 -050044 list_del(&entry->list);
Jeff Garzikb4538722005-05-12 22:48:20 -040045
46 if (entry->ops) {
47 entry->ops->deinit(entry->priv);
48 module_put(entry->ops->owner);
49 }
50 kfree(entry);
51 }
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050052 spin_unlock_irqrestore(&ieee->lock, flags);
53}
54
55/* After this, crypt_deinit_list won't accept new members */
56void ieee80211_crypt_quiescing(struct ieee80211_device *ieee)
57{
58 unsigned long flags;
59
60 spin_lock_irqsave(&ieee->lock, flags);
61 ieee->crypt_quiesced = 1;
James Ketrenos20d64712005-09-21 11:53:43 -050062 spin_unlock_irqrestore(&ieee->lock, flags);
Jeff Garzikb4538722005-05-12 22:48:20 -040063}
64
65void ieee80211_crypt_deinit_handler(unsigned long data)
66{
67 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050068 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -040069
Jeff Garzikb4538722005-05-12 22:48:20 -040070 ieee80211_crypt_deinit_entries(ieee, 0);
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050071
72 spin_lock_irqsave(&ieee->lock, flags);
73 if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) {
Jeff Garzikb4538722005-05-12 22:48:20 -040074 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
75 "deletion list\n", ieee->dev->name);
76 ieee->crypt_deinit_timer.expires = jiffies + HZ;
77 add_timer(&ieee->crypt_deinit_timer);
78 }
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050079 spin_unlock_irqrestore(&ieee->lock, flags);
Jeff Garzikb4538722005-05-12 22:48:20 -040080}
81
82void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
83 struct ieee80211_crypt_data **crypt)
84{
85 struct ieee80211_crypt_data *tmp;
86 unsigned long flags;
87
88 if (*crypt == NULL)
89 return;
90
91 tmp = *crypt;
92 *crypt = NULL;
93
94 /* must not run ops->deinit() while there may be pending encrypt or
95 * decrypt operations. Use a list of delayed deinits to avoid needing
96 * locking. */
97
98 spin_lock_irqsave(&ieee->lock, flags);
James Ketrenos0ad0c3c2005-09-21 11:54:15 -050099 if (!ieee->crypt_quiesced) {
100 list_add(&tmp->list, &ieee->crypt_deinit_list);
101 if (!timer_pending(&ieee->crypt_deinit_timer)) {
102 ieee->crypt_deinit_timer.expires = jiffies + HZ;
103 add_timer(&ieee->crypt_deinit_timer);
104 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400105 }
106 spin_unlock_irqrestore(&ieee->lock, flags);
107}
108
109int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
110{
111 unsigned long flags;
112 struct ieee80211_crypto_alg *alg;
113
Jeff Garzikb4538722005-05-12 22:48:20 -0400114 alg = kmalloc(sizeof(*alg), GFP_KERNEL);
115 if (alg == NULL)
116 return -ENOMEM;
117
118 memset(alg, 0, sizeof(*alg));
119 alg->ops = ops;
120
Christoph Hellwige3305622005-11-09 01:01:04 -0500121 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
122 list_add(&alg->list, &ieee80211_crypto_algs);
123 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
Jeff Garzikb4538722005-05-12 22:48:20 -0400124
125 printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
126 ops->name);
127
128 return 0;
129}
130
131int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
132{
Christoph Hellwige3305622005-11-09 01:01:04 -0500133 struct ieee80211_crypto_alg *alg;
Jeff Garzikb4538722005-05-12 22:48:20 -0400134 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -0400135
Christoph Hellwige3305622005-11-09 01:01:04 -0500136 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
137 list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
138 if (alg->ops == ops)
139 goto found;
Jeff Garzikb4538722005-05-12 22:48:20 -0400140 }
Christoph Hellwige3305622005-11-09 01:01:04 -0500141 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
142 return -EINVAL;
Jeff Garzikb4538722005-05-12 22:48:20 -0400143
Christoph Hellwige3305622005-11-09 01:01:04 -0500144 found:
145 printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
146 "'%s'\n", ops->name);
147 list_del(&alg->list);
148 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
149 kfree(alg);
150 return 0;
Jeff Garzikb4538722005-05-12 22:48:20 -0400151}
152
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400153struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
Jeff Garzikb4538722005-05-12 22:48:20 -0400154{
Christoph Hellwige3305622005-11-09 01:01:04 -0500155 struct ieee80211_crypto_alg *alg;
Jeff Garzikb4538722005-05-12 22:48:20 -0400156 unsigned long flags;
Jeff Garzikb4538722005-05-12 22:48:20 -0400157
Christoph Hellwige3305622005-11-09 01:01:04 -0500158 spin_lock_irqsave(&ieee80211_crypto_lock, flags);
159 list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
160 if (strcmp(alg->ops->name, name) == 0)
161 goto found;
Jeff Garzikb4538722005-05-12 22:48:20 -0400162 }
Christoph Hellwige3305622005-11-09 01:01:04 -0500163 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
164 return NULL;
Jeff Garzikb4538722005-05-12 22:48:20 -0400165
Christoph Hellwige3305622005-11-09 01:01:04 -0500166 found:
167 spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
168 return alg->ops;
Jeff Garzikb4538722005-05-12 22:48:20 -0400169}
170
James Ketrenos6eb6edf2005-09-22 10:34:15 +0000171static void *ieee80211_crypt_null_init(int keyidx)
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400172{
173 return (void *)1;
174}
Christoph Hellwige3305622005-11-09 01:01:04 -0500175
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400176static void ieee80211_crypt_null_deinit(void *priv)
177{
178}
Jeff Garzikb4538722005-05-12 22:48:20 -0400179
180static struct ieee80211_crypto_ops ieee80211_crypt_null = {
James Ketrenos74079fd2005-09-13 17:35:21 -0500181 .name = "NULL",
182 .init = ieee80211_crypt_null_init,
183 .deinit = ieee80211_crypt_null_deinit,
James Ketrenos74079fd2005-09-13 17:35:21 -0500184 .owner = THIS_MODULE,
Jeff Garzikb4538722005-05-12 22:48:20 -0400185};
186
Jeff Garzikb4538722005-05-12 22:48:20 -0400187static int __init ieee80211_crypto_init(void)
188{
Christoph Hellwige3305622005-11-09 01:01:04 -0500189 return ieee80211_register_crypto_ops(&ieee80211_crypt_null);
Jeff Garzikb4538722005-05-12 22:48:20 -0400190}
191
Jeff Garzikb4538722005-05-12 22:48:20 -0400192static void __exit ieee80211_crypto_deinit(void)
193{
Christoph Hellwige3305622005-11-09 01:01:04 -0500194 ieee80211_unregister_crypto_ops(&ieee80211_crypt_null);
195 BUG_ON(!list_empty(&ieee80211_crypto_algs));
Jeff Garzikb4538722005-05-12 22:48:20 -0400196}
197
198EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
199EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
200EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
James Ketrenos0ad0c3c2005-09-21 11:54:15 -0500201EXPORT_SYMBOL(ieee80211_crypt_quiescing);
Jeff Garzikb4538722005-05-12 22:48:20 -0400202
203EXPORT_SYMBOL(ieee80211_register_crypto_ops);
204EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
205EXPORT_SYMBOL(ieee80211_get_crypto_ops);
206
207module_init(ieee80211_crypto_init);
208module_exit(ieee80211_crypto_deinit);