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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 //#include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
22 #include "ieee80211.h"
24 MODULE_AUTHOR("Jouni Malinen");
25 MODULE_DESCRIPTION("HostAP crypto");
26 MODULE_LICENSE("GPL");
28 struct ieee80211_crypto_alg
{
29 struct list_head list
;
30 struct ieee80211_crypto_ops
*ops
;
34 struct ieee80211_crypto
{
35 struct list_head algs
;
39 static struct ieee80211_crypto
*hcrypt
;
41 void ieee80211_crypt_deinit_entries(struct ieee80211_device
*ieee
, int force
)
43 struct list_head
*ptr
, *n
;
44 struct ieee80211_crypt_data
*entry
;
46 for (ptr
= ieee
->crypt_deinit_list
.next
, n
= ptr
->next
;
47 ptr
!= &ieee
->crypt_deinit_list
; ptr
= n
, n
= ptr
->next
) {
48 entry
= list_entry(ptr
, struct ieee80211_crypt_data
, list
);
50 if (atomic_read(&entry
->refcnt
) != 0 && !force
)
56 entry
->ops
->deinit(entry
->priv
);
61 void ieee80211_crypt_deinit_handler(unsigned long data
)
63 struct ieee80211_device
*ieee
= (struct ieee80211_device
*)data
;
66 spin_lock_irqsave(&ieee
->lock
, flags
);
67 ieee80211_crypt_deinit_entries(ieee
, 0);
68 if (!list_empty(&ieee
->crypt_deinit_list
)) {
69 pr_debug("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
93 spin_lock_irqsave(&ieee
->lock
, flags
);
94 list_add(&tmp
->list
, &ieee
->crypt_deinit_list
);
95 if (!timer_pending(&ieee
->crypt_deinit_timer
)) {
96 ieee
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
97 add_timer(&ieee
->crypt_deinit_timer
);
99 spin_unlock_irqrestore(&ieee
->lock
, flags
);
102 int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops
*ops
)
105 struct ieee80211_crypto_alg
*alg
;
110 alg
= kzalloc(sizeof(*alg
), GFP_KERNEL
);
116 spin_lock_irqsave(&hcrypt
->lock
, flags
);
117 list_add(&alg
->list
, &hcrypt
->algs
);
118 spin_unlock_irqrestore(&hcrypt
->lock
, flags
);
120 pr_debug("registered algorithm '%s'\n", ops
->name
);
125 int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops
*ops
)
128 struct list_head
*ptr
;
129 struct ieee80211_crypto_alg
*del_alg
= NULL
;
134 spin_lock_irqsave(&hcrypt
->lock
, flags
);
135 for (ptr
= hcrypt
->algs
.next
; ptr
!= &hcrypt
->algs
; ptr
= ptr
->next
) {
136 struct ieee80211_crypto_alg
*alg
=
137 (struct ieee80211_crypto_alg
*) ptr
;
138 if (alg
->ops
== ops
) {
139 list_del(&alg
->list
);
144 spin_unlock_irqrestore(&hcrypt
->lock
, flags
);
147 pr_debug("unregistered algorithm '%s'\n", ops
->name
);
151 return del_alg
? 0 : -1;
155 struct ieee80211_crypto_ops
*ieee80211_get_crypto_ops(const char *name
)
158 struct list_head
*ptr
;
159 struct ieee80211_crypto_alg
*found_alg
= NULL
;
164 spin_lock_irqsave(&hcrypt
->lock
, flags
);
165 for (ptr
= hcrypt
->algs
.next
; ptr
!= &hcrypt
->algs
; ptr
= ptr
->next
) {
166 struct ieee80211_crypto_alg
*alg
=
167 (struct ieee80211_crypto_alg
*) ptr
;
168 if (strcmp(alg
->ops
->name
, name
) == 0) {
173 spin_unlock_irqrestore(&hcrypt
->lock
, flags
);
176 return found_alg
->ops
;
182 static void *ieee80211_crypt_null_init(int keyidx
) { return (void *) 1; }
183 static void ieee80211_crypt_null_deinit(void *priv
) {}
185 static struct ieee80211_crypto_ops ieee80211_crypt_null
= {
187 .init
= ieee80211_crypt_null_init
,
188 .deinit
= ieee80211_crypt_null_deinit
,
189 .encrypt_mpdu
= NULL
,
190 .decrypt_mpdu
= NULL
,
191 .encrypt_msdu
= NULL
,
192 .decrypt_msdu
= NULL
,
195 .extra_prefix_len
= 0,
196 .extra_postfix_len
= 0,
197 .owner
= THIS_MODULE
,
201 int ieee80211_crypto_init(void)
205 hcrypt
= kzalloc(sizeof(*hcrypt
), GFP_KERNEL
);
209 INIT_LIST_HEAD(&hcrypt
->algs
);
210 spin_lock_init(&hcrypt
->lock
);
212 ret
= ieee80211_register_crypto_ops(&ieee80211_crypt_null
);
222 void ieee80211_crypto_deinit(void)
224 struct list_head
*ptr
, *n
;
225 struct ieee80211_crypto_alg
*alg
= NULL
;
230 list_for_each_safe(ptr
, n
, &hcrypt
->algs
) {
231 alg
= list_entry(ptr
, struct ieee80211_crypto_alg
, list
);
234 pr_debug("unregistered algorithm '%s' (deinit)\n",