2 * lib80211 -- common bits for IEEE802.11 drivers
4 * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
6 * Portions copied from old ieee80211 component, w/ original copyright
9 * Host AP crypto routines
11 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
12 * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/ctype.h>
20 #include <linux/ieee80211.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
26 #include <net/lib80211.h>
28 #define DRV_NAME "lib80211"
30 #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
32 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
33 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
34 MODULE_LICENSE("GPL");
36 struct lib80211_crypto_alg
{
37 struct list_head list
;
38 struct lib80211_crypto_ops
*ops
;
41 static LIST_HEAD(lib80211_crypto_algs
);
42 static DEFINE_SPINLOCK(lib80211_crypto_lock
);
44 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info
*info
,
46 static void lib80211_crypt_quiescing(struct lib80211_crypt_info
*info
);
47 static void lib80211_crypt_deinit_handler(struct timer_list
*t
);
49 int lib80211_crypt_info_init(struct lib80211_crypt_info
*info
, char *name
,
52 memset(info
, 0, sizeof(*info
));
57 INIT_LIST_HEAD(&info
->crypt_deinit_list
);
58 timer_setup(&info
->crypt_deinit_timer
, lib80211_crypt_deinit_handler
,
63 EXPORT_SYMBOL(lib80211_crypt_info_init
);
65 void lib80211_crypt_info_free(struct lib80211_crypt_info
*info
)
69 lib80211_crypt_quiescing(info
);
70 del_timer_sync(&info
->crypt_deinit_timer
);
71 lib80211_crypt_deinit_entries(info
, 1);
73 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
74 struct lib80211_crypt_data
*crypt
= info
->crypt
[i
];
77 crypt
->ops
->deinit(crypt
->priv
);
78 module_put(crypt
->ops
->owner
);
81 info
->crypt
[i
] = NULL
;
85 EXPORT_SYMBOL(lib80211_crypt_info_free
);
87 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info
*info
,
90 struct lib80211_crypt_data
*entry
, *next
;
93 spin_lock_irqsave(info
->lock
, flags
);
94 list_for_each_entry_safe(entry
, next
, &info
->crypt_deinit_list
, list
) {
95 if (atomic_read(&entry
->refcnt
) != 0 && !force
)
98 list_del(&entry
->list
);
101 entry
->ops
->deinit(entry
->priv
);
102 module_put(entry
->ops
->owner
);
106 spin_unlock_irqrestore(info
->lock
, flags
);
109 /* After this, crypt_deinit_list won't accept new members */
110 static void lib80211_crypt_quiescing(struct lib80211_crypt_info
*info
)
114 spin_lock_irqsave(info
->lock
, flags
);
115 info
->crypt_quiesced
= 1;
116 spin_unlock_irqrestore(info
->lock
, flags
);
119 static void lib80211_crypt_deinit_handler(struct timer_list
*t
)
121 struct lib80211_crypt_info
*info
= from_timer(info
, t
,
125 lib80211_crypt_deinit_entries(info
, 0);
127 spin_lock_irqsave(info
->lock
, flags
);
128 if (!list_empty(&info
->crypt_deinit_list
) && !info
->crypt_quiesced
) {
129 printk(KERN_DEBUG
"%s: entries remaining in delayed crypt "
130 "deletion list\n", info
->name
);
131 info
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
132 add_timer(&info
->crypt_deinit_timer
);
134 spin_unlock_irqrestore(info
->lock
, flags
);
137 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info
*info
,
138 struct lib80211_crypt_data
**crypt
)
140 struct lib80211_crypt_data
*tmp
;
149 /* must not run ops->deinit() while there may be pending encrypt or
150 * decrypt operations. Use a list of delayed deinits to avoid needing
153 spin_lock_irqsave(info
->lock
, flags
);
154 if (!info
->crypt_quiesced
) {
155 list_add(&tmp
->list
, &info
->crypt_deinit_list
);
156 if (!timer_pending(&info
->crypt_deinit_timer
)) {
157 info
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
158 add_timer(&info
->crypt_deinit_timer
);
161 spin_unlock_irqrestore(info
->lock
, flags
);
163 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit
);
165 int lib80211_register_crypto_ops(struct lib80211_crypto_ops
*ops
)
168 struct lib80211_crypto_alg
*alg
;
170 alg
= kzalloc(sizeof(*alg
), GFP_KERNEL
);
176 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
177 list_add(&alg
->list
, &lib80211_crypto_algs
);
178 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
180 printk(KERN_DEBUG
"lib80211_crypt: registered algorithm '%s'\n",
185 EXPORT_SYMBOL(lib80211_register_crypto_ops
);
187 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops
*ops
)
189 struct lib80211_crypto_alg
*alg
;
192 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
193 list_for_each_entry(alg
, &lib80211_crypto_algs
, list
) {
197 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
201 printk(KERN_DEBUG
"lib80211_crypt: unregistered algorithm '%s'\n",
203 list_del(&alg
->list
);
204 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
208 EXPORT_SYMBOL(lib80211_unregister_crypto_ops
);
210 struct lib80211_crypto_ops
*lib80211_get_crypto_ops(const char *name
)
212 struct lib80211_crypto_alg
*alg
;
215 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
216 list_for_each_entry(alg
, &lib80211_crypto_algs
, list
) {
217 if (strcmp(alg
->ops
->name
, name
) == 0)
220 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
224 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
227 EXPORT_SYMBOL(lib80211_get_crypto_ops
);
229 static void *lib80211_crypt_null_init(int keyidx
)
234 static void lib80211_crypt_null_deinit(void *priv
)
238 static struct lib80211_crypto_ops lib80211_crypt_null
= {
240 .init
= lib80211_crypt_null_init
,
241 .deinit
= lib80211_crypt_null_deinit
,
242 .owner
= THIS_MODULE
,
245 static int __init
lib80211_init(void)
247 pr_info(DRV_DESCRIPTION
"\n");
248 return lib80211_register_crypto_ops(&lib80211_crypt_null
);
251 static void __exit
lib80211_exit(void)
253 lib80211_unregister_crypto_ops(&lib80211_crypt_null
);
254 BUG_ON(!list_empty(&lib80211_crypto_algs
));
257 module_init(lib80211_init
);
258 module_exit(lib80211_exit
);