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 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 14 | #include <linux/errno.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 18 | #include <linux/string.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 19 | #include <net/ieee80211.h> |
| 20 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 21 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 22 | MODULE_AUTHOR("Jouni Malinen"); |
| 23 | MODULE_DESCRIPTION("HostAP crypto"); |
| 24 | MODULE_LICENSE("GPL"); |
| 25 | |
| 26 | struct ieee80211_crypto_alg { |
| 27 | struct list_head list; |
| 28 | struct ieee80211_crypto_ops *ops; |
| 29 | }; |
| 30 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 31 | static LIST_HEAD(ieee80211_crypto_algs); |
| 32 | static DEFINE_SPINLOCK(ieee80211_crypto_lock); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 33 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 34 | void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 35 | { |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 36 | struct ieee80211_crypt_data *entry, *next; |
James Ketrenos | 20d6471 | 2005-09-21 11:53:43 -0500 | [diff] [blame] | 37 | unsigned long flags; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 38 | |
James Ketrenos | 20d6471 | 2005-09-21 11:53:43 -0500 | [diff] [blame] | 39 | spin_lock_irqsave(&ieee->lock, flags); |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 40 | list_for_each_entry_safe(entry, next, &ieee->crypt_deinit_list, list) { |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 41 | if (atomic_read(&entry->refcnt) != 0 && !force) |
| 42 | continue; |
| 43 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 44 | list_del(&entry->list); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 45 | |
| 46 | if (entry->ops) { |
| 47 | entry->ops->deinit(entry->priv); |
| 48 | module_put(entry->ops->owner); |
| 49 | } |
| 50 | kfree(entry); |
| 51 | } |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 52 | spin_unlock_irqrestore(&ieee->lock, flags); |
| 53 | } |
| 54 | |
| 55 | /* After this, crypt_deinit_list won't accept new members */ |
| 56 | void 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 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; |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 68 | unsigned long flags; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 69 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 70 | ieee80211_crypt_deinit_entries(ieee, 0); |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 71 | |
| 72 | spin_lock_irqsave(&ieee->lock, flags); |
| 73 | if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) { |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 74 | 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 Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 79 | spin_unlock_irqrestore(&ieee->lock, flags); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void 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 Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 99 | 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 Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 105 | } |
| 106 | spin_unlock_irqrestore(&ieee->lock, flags); |
| 107 | } |
| 108 | |
| 109 | int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops) |
| 110 | { |
| 111 | unsigned long flags; |
| 112 | struct ieee80211_crypto_alg *alg; |
| 113 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 114 | 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 Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 121 | spin_lock_irqsave(&ieee80211_crypto_lock, flags); |
| 122 | list_add(&alg->list, &ieee80211_crypto_algs); |
| 123 | spin_unlock_irqrestore(&ieee80211_crypto_lock, flags); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 124 | |
| 125 | printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n", |
| 126 | ops->name); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops) |
| 132 | { |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 133 | struct ieee80211_crypto_alg *alg; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 134 | unsigned long flags; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 135 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 136 | 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 Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 140 | } |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 141 | spin_unlock_irqrestore(&ieee80211_crypto_lock, flags); |
| 142 | return -EINVAL; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 143 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 144 | 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 Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 153 | struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 154 | { |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 155 | struct ieee80211_crypto_alg *alg; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 156 | unsigned long flags; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 157 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 158 | 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 Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 162 | } |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 163 | spin_unlock_irqrestore(&ieee80211_crypto_lock, flags); |
| 164 | return NULL; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 165 | |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 166 | found: |
| 167 | spin_unlock_irqrestore(&ieee80211_crypto_lock, flags); |
| 168 | return alg->ops; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 169 | } |
| 170 | |
James Ketrenos | 6eb6edf | 2005-09-22 10:34:15 +0000 | [diff] [blame] | 171 | static void *ieee80211_crypt_null_init(int keyidx) |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 172 | { |
| 173 | return (void *)1; |
| 174 | } |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 175 | |
Jeff Garzik | 0edd5b4 | 2005-09-07 00:48:31 -0400 | [diff] [blame] | 176 | static void ieee80211_crypt_null_deinit(void *priv) |
| 177 | { |
| 178 | } |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 179 | |
| 180 | static struct ieee80211_crypto_ops ieee80211_crypt_null = { |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 181 | .name = "NULL", |
| 182 | .init = ieee80211_crypt_null_init, |
| 183 | .deinit = ieee80211_crypt_null_deinit, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 184 | .owner = THIS_MODULE, |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 185 | }; |
| 186 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 187 | static int __init ieee80211_crypto_init(void) |
| 188 | { |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 189 | return ieee80211_register_crypto_ops(&ieee80211_crypt_null); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 192 | static void __exit ieee80211_crypto_deinit(void) |
| 193 | { |
Christoph Hellwig | e330562 | 2005-11-09 01:01:04 -0500 | [diff] [blame] | 194 | ieee80211_unregister_crypto_ops(&ieee80211_crypt_null); |
| 195 | BUG_ON(!list_empty(&ieee80211_crypto_algs)); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | EXPORT_SYMBOL(ieee80211_crypt_deinit_entries); |
| 199 | EXPORT_SYMBOL(ieee80211_crypt_deinit_handler); |
| 200 | EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit); |
James Ketrenos | 0ad0c3c | 2005-09-21 11:54:15 -0500 | [diff] [blame] | 201 | EXPORT_SYMBOL(ieee80211_crypt_quiescing); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 202 | |
| 203 | EXPORT_SYMBOL(ieee80211_register_crypto_ops); |
| 204 | EXPORT_SYMBOL(ieee80211_unregister_crypto_ops); |
| 205 | EXPORT_SYMBOL(ieee80211_get_crypto_ops); |
| 206 | |
| 207 | module_init(ieee80211_crypto_init); |
| 208 | module_exit(ieee80211_crypto_deinit); |