2 * Host AP crypto routines
4 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
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
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
20 #include "ieee80211.h"
22 MODULE_AUTHOR("Jouni Malinen");
23 MODULE_DESCRIPTION("HostAP crypto");
24 MODULE_LICENSE("GPL");
26 struct ieee80211_crypto_alg {
27 struct list_head list;
28 struct ieee80211_crypto_ops *ops;
32 struct ieee80211_crypto {
33 struct list_head algs;
37 static struct ieee80211_crypto *hcrypt;
39 void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee,
42 struct list_head *ptr, *n;
43 struct ieee80211_crypt_data *entry;
45 for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
46 ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
47 entry = list_entry(ptr, struct ieee80211_crypt_data, list);
49 if (atomic_read(&entry->refcnt) != 0 && !force)
55 entry->ops->deinit(entry->priv);
60 void ieee80211_crypt_deinit_handler(unsigned long data)
62 struct ieee80211_device *ieee = (struct ieee80211_device *)data;
65 spin_lock_irqsave(&ieee->lock, flags);
66 ieee80211_crypt_deinit_entries(ieee, 0);
67 if (!list_empty(&ieee->crypt_deinit_list)) {
68 netdev_dbg(ieee->dev, "%s: entries remaining in delayed crypt deletion list\n",
70 ieee->crypt_deinit_timer.expires = jiffies + HZ;
71 add_timer(&ieee->crypt_deinit_timer);
73 spin_unlock_irqrestore(&ieee->lock, flags);
77 void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
78 struct ieee80211_crypt_data **crypt)
80 struct ieee80211_crypt_data *tmp;
89 /* must not run ops->deinit() while there may be pending encrypt or
90 * decrypt operations. Use a list of delayed deinits to avoid needing
94 spin_lock_irqsave(&ieee->lock, flags);
95 list_add(&tmp->list, &ieee->crypt_deinit_list);
96 if (!timer_pending(&ieee->crypt_deinit_timer)) {
97 ieee->crypt_deinit_timer.expires = jiffies + HZ;
98 add_timer(&ieee->crypt_deinit_timer);
100 spin_unlock_irqrestore(&ieee->lock, flags);
103 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
106 struct ieee80211_crypto_alg *alg;
111 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
117 spin_lock_irqsave(&hcrypt->lock, flags);
118 list_add(&alg->list, &hcrypt->algs);
119 spin_unlock_irqrestore(&hcrypt->lock, flags);
121 pr_debug("ieee80211_crypt: registered algorithm '%s'\n",
127 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
130 struct list_head *ptr;
131 struct ieee80211_crypto_alg *del_alg = NULL;
136 spin_lock_irqsave(&hcrypt->lock, flags);
137 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
138 struct ieee80211_crypto_alg *alg =
139 (struct ieee80211_crypto_alg *)ptr;
140 if (alg->ops == ops) {
141 list_del(&alg->list);
146 spin_unlock_irqrestore(&hcrypt->lock, flags);
149 pr_debug("ieee80211_crypt: unregistered algorithm '%s'\n",
154 return del_alg ? 0 : -1;
158 struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
161 struct list_head *ptr;
162 struct ieee80211_crypto_alg *found_alg = NULL;
167 spin_lock_irqsave(&hcrypt->lock, flags);
168 for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
169 struct ieee80211_crypto_alg *alg =
170 (struct ieee80211_crypto_alg *)ptr;
171 if (strcmp(alg->ops->name, name) == 0) {
176 spin_unlock_irqrestore(&hcrypt->lock, flags);
179 return found_alg->ops;
184 static void *ieee80211_crypt_null_init(int keyidx) { return (void *) 1; }
185 static void ieee80211_crypt_null_deinit(void *priv) {}
187 static struct ieee80211_crypto_ops ieee80211_crypt_null = {
189 .init = ieee80211_crypt_null_init,
190 .deinit = ieee80211_crypt_null_deinit,
191 .encrypt_mpdu = NULL,
192 .decrypt_mpdu = NULL,
193 .encrypt_msdu = NULL,
194 .decrypt_msdu = NULL,
197 .extra_prefix_len = 0,
198 .extra_postfix_len = 0,
199 .owner = THIS_MODULE,
202 int __init ieee80211_crypto_init(void)
206 hcrypt = kzalloc(sizeof(*hcrypt), GFP_KERNEL);
210 INIT_LIST_HEAD(&hcrypt->algs);
211 spin_lock_init(&hcrypt->lock);
213 ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
222 void __exit ieee80211_crypto_deinit(void)
224 struct list_head *ptr, *n;
229 for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
230 ptr = n, n = ptr->next) {
231 struct ieee80211_crypto_alg *alg =
232 (struct ieee80211_crypto_alg *)ptr;
234 pr_debug("ieee80211_crypt: unregistered algorithm '%s' (deinit)\n",