Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | #include <linux/config.h> |
| 15 | #include <linux/version.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <asm/string.h> |
| 20 | #include <asm/errno.h> |
| 21 | |
| 22 | #include <net/ieee80211.h> |
| 23 | |
| 24 | MODULE_AUTHOR("Jouni Malinen"); |
| 25 | MODULE_DESCRIPTION("HostAP crypto"); |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | |
| 28 | struct ieee80211_crypto_alg { |
| 29 | struct list_head list; |
| 30 | struct ieee80211_crypto_ops *ops; |
| 31 | }; |
| 32 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 33 | struct ieee80211_crypto { |
| 34 | struct list_head algs; |
| 35 | spinlock_t lock; |
| 36 | }; |
| 37 | |
| 38 | static struct ieee80211_crypto *hcrypt; |
| 39 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 40 | void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 41 | { |
| 42 | struct list_head *ptr, *n; |
| 43 | struct ieee80211_crypt_data *entry; |
James Ketrenos | 20d6471 | 2005-09-21 11:53:43 -0500 | [diff] [blame] | 44 | unsigned long flags; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 45 | |
James Ketrenos | 20d6471 | 2005-09-21 11:53:43 -0500 | [diff] [blame] | 46 | spin_lock_irqsave(&ieee->lock, flags); |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 47 | |
| 48 | if (list_empty(&ieee->crypt_deinit_list)) |
| 49 | goto unlock; |
| 50 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 51 | for (ptr = ieee->crypt_deinit_list.next, n = ptr->next; |
| 52 | ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) { |
| 53 | entry = list_entry(ptr, struct ieee80211_crypt_data, list); |
| 54 | |
| 55 | if (atomic_read(&entry->refcnt) != 0 && !force) |
| 56 | continue; |
| 57 | |
| 58 | list_del(ptr); |
| 59 | |
| 60 | if (entry->ops) { |
| 61 | entry->ops->deinit(entry->priv); |
| 62 | module_put(entry->ops->owner); |
| 63 | } |
| 64 | kfree(entry); |
| 65 | } |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 66 | unlock: |
| 67 | spin_unlock_irqrestore(&ieee->lock, flags); |
| 68 | } |
| 69 | |
| 70 | /* After this, crypt_deinit_list won't accept new members */ |
| 71 | void ieee80211_crypt_quiescing(struct ieee80211_device *ieee) |
| 72 | { |
| 73 | unsigned long flags; |
| 74 | |
| 75 | spin_lock_irqsave(&ieee->lock, flags); |
| 76 | ieee->crypt_quiesced = 1; |
James Ketrenos | 20d6471 | 2005-09-21 11:53:43 -0500 | [diff] [blame] | 77 | spin_unlock_irqrestore(&ieee->lock, flags); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void ieee80211_crypt_deinit_handler(unsigned long data) |
| 81 | { |
| 82 | struct ieee80211_device *ieee = (struct ieee80211_device *)data; |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 83 | unsigned long flags; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 84 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 85 | ieee80211_crypt_deinit_entries(ieee, 0); |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 86 | |
| 87 | spin_lock_irqsave(&ieee->lock, flags); |
| 88 | if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) { |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 89 | printk(KERN_DEBUG "%s: entries remaining in delayed crypt " |
| 90 | "deletion list\n", ieee->dev->name); |
| 91 | ieee->crypt_deinit_timer.expires = jiffies + HZ; |
| 92 | add_timer(&ieee->crypt_deinit_timer); |
| 93 | } |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 94 | spin_unlock_irqrestore(&ieee->lock, flags); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee, |
| 98 | struct ieee80211_crypt_data **crypt) |
| 99 | { |
| 100 | struct ieee80211_crypt_data *tmp; |
| 101 | unsigned long flags; |
| 102 | |
| 103 | if (*crypt == NULL) |
| 104 | return; |
| 105 | |
| 106 | tmp = *crypt; |
| 107 | *crypt = NULL; |
| 108 | |
| 109 | /* must not run ops->deinit() while there may be pending encrypt or |
| 110 | * decrypt operations. Use a list of delayed deinits to avoid needing |
| 111 | * locking. */ |
| 112 | |
| 113 | spin_lock_irqsave(&ieee->lock, flags); |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 114 | if (!ieee->crypt_quiesced) { |
| 115 | list_add(&tmp->list, &ieee->crypt_deinit_list); |
| 116 | if (!timer_pending(&ieee->crypt_deinit_timer)) { |
| 117 | ieee->crypt_deinit_timer.expires = jiffies + HZ; |
| 118 | add_timer(&ieee->crypt_deinit_timer); |
| 119 | } |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 120 | } |
| 121 | spin_unlock_irqrestore(&ieee->lock, flags); |
| 122 | } |
| 123 | |
| 124 | int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops) |
| 125 | { |
| 126 | unsigned long flags; |
| 127 | struct ieee80211_crypto_alg *alg; |
| 128 | |
| 129 | if (hcrypt == NULL) |
| 130 | return -1; |
| 131 | |
| 132 | alg = kmalloc(sizeof(*alg), GFP_KERNEL); |
| 133 | if (alg == NULL) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | memset(alg, 0, sizeof(*alg)); |
| 137 | alg->ops = ops; |
| 138 | |
| 139 | spin_lock_irqsave(&hcrypt->lock, flags); |
| 140 | list_add(&alg->list, &hcrypt->algs); |
| 141 | spin_unlock_irqrestore(&hcrypt->lock, flags); |
| 142 | |
| 143 | printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n", |
| 144 | ops->name); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops) |
| 150 | { |
| 151 | unsigned long flags; |
| 152 | struct list_head *ptr; |
| 153 | struct ieee80211_crypto_alg *del_alg = NULL; |
| 154 | |
| 155 | if (hcrypt == NULL) |
| 156 | return -1; |
| 157 | |
| 158 | spin_lock_irqsave(&hcrypt->lock, flags); |
| 159 | for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) { |
| 160 | struct ieee80211_crypto_alg *alg = |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 161 | (struct ieee80211_crypto_alg *)ptr; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 162 | if (alg->ops == ops) { |
| 163 | list_del(&alg->list); |
| 164 | del_alg = alg; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | spin_unlock_irqrestore(&hcrypt->lock, flags); |
| 169 | |
| 170 | if (del_alg) { |
| 171 | printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm " |
| 172 | "'%s'\n", ops->name); |
| 173 | kfree(del_alg); |
| 174 | } |
| 175 | |
| 176 | return del_alg ? 0 : -1; |
| 177 | } |
| 178 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 179 | struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 180 | { |
| 181 | unsigned long flags; |
| 182 | struct list_head *ptr; |
| 183 | struct ieee80211_crypto_alg *found_alg = NULL; |
| 184 | |
| 185 | if (hcrypt == NULL) |
| 186 | return NULL; |
| 187 | |
| 188 | spin_lock_irqsave(&hcrypt->lock, flags); |
| 189 | for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) { |
| 190 | struct ieee80211_crypto_alg *alg = |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 191 | (struct ieee80211_crypto_alg *)ptr; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 192 | if (strcmp(alg->ops->name, name) == 0) { |
| 193 | found_alg = alg; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | spin_unlock_irqrestore(&hcrypt->lock, flags); |
| 198 | |
| 199 | if (found_alg) |
| 200 | return found_alg->ops; |
| 201 | else |
| 202 | return NULL; |
| 203 | } |
| 204 | |
James Ketrenos | 20d6471 | 2005-09-21 11:53:43 -0500 | [diff] [blame] | 205 | static void *ieee80211_crypt_null_init(struct ieee80211_device *ieee, |
| 206 | int keyidx) |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 207 | { |
| 208 | return (void *)1; |
| 209 | } |
| 210 | static void ieee80211_crypt_null_deinit(void *priv) |
| 211 | { |
| 212 | } |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 213 | |
| 214 | static struct ieee80211_crypto_ops ieee80211_crypt_null = { |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 215 | .name = "NULL", |
| 216 | .init = ieee80211_crypt_null_init, |
| 217 | .deinit = ieee80211_crypt_null_deinit, |
| 218 | .encrypt_mpdu = NULL, |
| 219 | .decrypt_mpdu = NULL, |
| 220 | .encrypt_msdu = NULL, |
| 221 | .decrypt_msdu = NULL, |
| 222 | .set_key = NULL, |
| 223 | .get_key = NULL, |
James Ketrenos | 1264fc0 | 2005-09-21 11:54:53 -0500 | [diff] [blame^] | 224 | .extra_mpdu_prefix_len = 0, |
| 225 | .extra_mpdu_postfix_len = 0, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 226 | .owner = THIS_MODULE, |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 227 | }; |
| 228 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 229 | static int __init ieee80211_crypto_init(void) |
| 230 | { |
| 231 | int ret = -ENOMEM; |
| 232 | |
| 233 | hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL); |
| 234 | if (!hcrypt) |
| 235 | goto out; |
| 236 | |
| 237 | memset(hcrypt, 0, sizeof(*hcrypt)); |
| 238 | INIT_LIST_HEAD(&hcrypt->algs); |
| 239 | spin_lock_init(&hcrypt->lock); |
| 240 | |
| 241 | ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null); |
| 242 | if (ret < 0) { |
| 243 | kfree(hcrypt); |
| 244 | hcrypt = NULL; |
| 245 | } |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 246 | out: |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 250 | static void __exit ieee80211_crypto_deinit(void) |
| 251 | { |
| 252 | struct list_head *ptr, *n; |
| 253 | |
| 254 | if (hcrypt == NULL) |
| 255 | return; |
| 256 | |
| 257 | for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs; |
| 258 | ptr = n, n = ptr->next) { |
| 259 | struct ieee80211_crypto_alg *alg = |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 260 | (struct ieee80211_crypto_alg *)ptr; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 261 | list_del(ptr); |
| 262 | printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm " |
| 263 | "'%s' (deinit)\n", alg->ops->name); |
| 264 | kfree(alg); |
| 265 | } |
| 266 | |
| 267 | kfree(hcrypt); |
| 268 | } |
| 269 | |
| 270 | EXPORT_SYMBOL(ieee80211_crypt_deinit_entries); |
| 271 | EXPORT_SYMBOL(ieee80211_crypt_deinit_handler); |
| 272 | EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit); |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 273 | EXPORT_SYMBOL(ieee80211_crypt_quiescing); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 274 | |
| 275 | EXPORT_SYMBOL(ieee80211_register_crypto_ops); |
| 276 | EXPORT_SYMBOL(ieee80211_unregister_crypto_ops); |
| 277 | EXPORT_SYMBOL(ieee80211_get_crypto_ops); |
| 278 | |
| 279 | module_init(ieee80211_crypto_init); |
| 280 | module_exit(ieee80211_crypto_deinit); |