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