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 #include <linux/module.h>
17 #include <linux/ctype.h>
18 #include <linux/ieee80211.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
24 #include <net/lib80211.h>
26 #define DRV_NAME "lib80211"
28 #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
30 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
31 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
32 MODULE_LICENSE("GPL");
34 struct lib80211_crypto_alg
{
35 struct list_head list
;
36 struct lib80211_crypto_ops
*ops
;
39 static LIST_HEAD(lib80211_crypto_algs
);
40 static DEFINE_SPINLOCK(lib80211_crypto_lock
);
42 const char *print_ssid(char *buf
, const char *ssid
, u8 ssid_len
)
47 ssid_len
= min_t(u8
, ssid_len
, IEEE80211_MAX_SSID_LEN
);
66 d
+= snprintf(d
, 3, "%03o", *s
);
72 EXPORT_SYMBOL(print_ssid
);
74 int lib80211_crypt_info_init(struct lib80211_crypt_info
*info
, char *name
,
77 memset(info
, 0, sizeof(*info
));
82 INIT_LIST_HEAD(&info
->crypt_deinit_list
);
83 setup_timer(&info
->crypt_deinit_timer
, lib80211_crypt_deinit_handler
,
88 EXPORT_SYMBOL(lib80211_crypt_info_init
);
90 void lib80211_crypt_info_free(struct lib80211_crypt_info
*info
)
94 lib80211_crypt_quiescing(info
);
95 del_timer_sync(&info
->crypt_deinit_timer
);
96 lib80211_crypt_deinit_entries(info
, 1);
98 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
99 struct lib80211_crypt_data
*crypt
= info
->crypt
[i
];
102 crypt
->ops
->deinit(crypt
->priv
);
103 module_put(crypt
->ops
->owner
);
106 info
->crypt
[i
] = NULL
;
110 EXPORT_SYMBOL(lib80211_crypt_info_free
);
112 void lib80211_crypt_deinit_entries(struct lib80211_crypt_info
*info
, int force
)
114 struct lib80211_crypt_data
*entry
, *next
;
117 spin_lock_irqsave(info
->lock
, flags
);
118 list_for_each_entry_safe(entry
, next
, &info
->crypt_deinit_list
, list
) {
119 if (atomic_read(&entry
->refcnt
) != 0 && !force
)
122 list_del(&entry
->list
);
125 entry
->ops
->deinit(entry
->priv
);
126 module_put(entry
->ops
->owner
);
130 spin_unlock_irqrestore(info
->lock
, flags
);
132 EXPORT_SYMBOL(lib80211_crypt_deinit_entries
);
134 /* After this, crypt_deinit_list won't accept new members */
135 void lib80211_crypt_quiescing(struct lib80211_crypt_info
*info
)
139 spin_lock_irqsave(info
->lock
, flags
);
140 info
->crypt_quiesced
= 1;
141 spin_unlock_irqrestore(info
->lock
, flags
);
143 EXPORT_SYMBOL(lib80211_crypt_quiescing
);
145 void lib80211_crypt_deinit_handler(unsigned long data
)
147 struct lib80211_crypt_info
*info
= (struct lib80211_crypt_info
*)data
;
150 lib80211_crypt_deinit_entries(info
, 0);
152 spin_lock_irqsave(info
->lock
, flags
);
153 if (!list_empty(&info
->crypt_deinit_list
) && !info
->crypt_quiesced
) {
154 printk(KERN_DEBUG
"%s: entries remaining in delayed crypt "
155 "deletion list\n", info
->name
);
156 info
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
157 add_timer(&info
->crypt_deinit_timer
);
159 spin_unlock_irqrestore(info
->lock
, flags
);
161 EXPORT_SYMBOL(lib80211_crypt_deinit_handler
);
163 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info
*info
,
164 struct lib80211_crypt_data
**crypt
)
166 struct lib80211_crypt_data
*tmp
;
175 /* must not run ops->deinit() while there may be pending encrypt or
176 * decrypt operations. Use a list of delayed deinits to avoid needing
179 spin_lock_irqsave(info
->lock
, flags
);
180 if (!info
->crypt_quiesced
) {
181 list_add(&tmp
->list
, &info
->crypt_deinit_list
);
182 if (!timer_pending(&info
->crypt_deinit_timer
)) {
183 info
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
184 add_timer(&info
->crypt_deinit_timer
);
187 spin_unlock_irqrestore(info
->lock
, flags
);
189 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit
);
191 int lib80211_register_crypto_ops(struct lib80211_crypto_ops
*ops
)
194 struct lib80211_crypto_alg
*alg
;
196 alg
= kzalloc(sizeof(*alg
), GFP_KERNEL
);
202 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
203 list_add(&alg
->list
, &lib80211_crypto_algs
);
204 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
206 printk(KERN_DEBUG
"lib80211_crypt: registered algorithm '%s'\n",
211 EXPORT_SYMBOL(lib80211_register_crypto_ops
);
213 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops
*ops
)
215 struct lib80211_crypto_alg
*alg
;
218 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
219 list_for_each_entry(alg
, &lib80211_crypto_algs
, list
) {
223 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
227 printk(KERN_DEBUG
"lib80211_crypt: unregistered algorithm "
228 "'%s'\n", ops
->name
);
229 list_del(&alg
->list
);
230 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
234 EXPORT_SYMBOL(lib80211_unregister_crypto_ops
);
236 struct lib80211_crypto_ops
*lib80211_get_crypto_ops(const char *name
)
238 struct lib80211_crypto_alg
*alg
;
241 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
242 list_for_each_entry(alg
, &lib80211_crypto_algs
, list
) {
243 if (strcmp(alg
->ops
->name
, name
) == 0)
246 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
250 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
253 EXPORT_SYMBOL(lib80211_get_crypto_ops
);
255 static void *lib80211_crypt_null_init(int keyidx
)
260 static void lib80211_crypt_null_deinit(void *priv
)
264 static struct lib80211_crypto_ops lib80211_crypt_null
= {
266 .init
= lib80211_crypt_null_init
,
267 .deinit
= lib80211_crypt_null_deinit
,
268 .owner
= THIS_MODULE
,
271 static int __init
lib80211_init(void)
273 printk(KERN_INFO DRV_NAME
": " DRV_DESCRIPTION
"\n");
274 return lib80211_register_crypto_ops(&lib80211_crypt_null
);
277 static void __exit
lib80211_exit(void)
279 lib80211_unregister_crypto_ops(&lib80211_crypt_null
);
280 BUG_ON(!list_empty(&lib80211_crypto_algs
));
283 module_init(lib80211_init
);
284 module_exit(lib80211_exit
);