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 const char *print_ssid(char *buf
, const char *ssid
, u8 ssid_len
)
49 ssid_len
= min_t(u8
, ssid_len
, IEEE80211_MAX_SSID_LEN
);
68 d
+= snprintf(d
, 3, "%03o", *s
);
74 EXPORT_SYMBOL(print_ssid
);
76 int lib80211_crypt_info_init(struct lib80211_crypt_info
*info
, char *name
,
79 memset(info
, 0, sizeof(*info
));
84 INIT_LIST_HEAD(&info
->crypt_deinit_list
);
85 setup_timer(&info
->crypt_deinit_timer
, lib80211_crypt_deinit_handler
,
90 EXPORT_SYMBOL(lib80211_crypt_info_init
);
92 void lib80211_crypt_info_free(struct lib80211_crypt_info
*info
)
96 lib80211_crypt_quiescing(info
);
97 del_timer_sync(&info
->crypt_deinit_timer
);
98 lib80211_crypt_deinit_entries(info
, 1);
100 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
101 struct lib80211_crypt_data
*crypt
= info
->crypt
[i
];
104 crypt
->ops
->deinit(crypt
->priv
);
105 module_put(crypt
->ops
->owner
);
108 info
->crypt
[i
] = NULL
;
112 EXPORT_SYMBOL(lib80211_crypt_info_free
);
114 void lib80211_crypt_deinit_entries(struct lib80211_crypt_info
*info
, int force
)
116 struct lib80211_crypt_data
*entry
, *next
;
119 spin_lock_irqsave(info
->lock
, flags
);
120 list_for_each_entry_safe(entry
, next
, &info
->crypt_deinit_list
, list
) {
121 if (atomic_read(&entry
->refcnt
) != 0 && !force
)
124 list_del(&entry
->list
);
127 entry
->ops
->deinit(entry
->priv
);
128 module_put(entry
->ops
->owner
);
132 spin_unlock_irqrestore(info
->lock
, flags
);
134 EXPORT_SYMBOL(lib80211_crypt_deinit_entries
);
136 /* After this, crypt_deinit_list won't accept new members */
137 void lib80211_crypt_quiescing(struct lib80211_crypt_info
*info
)
141 spin_lock_irqsave(info
->lock
, flags
);
142 info
->crypt_quiesced
= 1;
143 spin_unlock_irqrestore(info
->lock
, flags
);
145 EXPORT_SYMBOL(lib80211_crypt_quiescing
);
147 void lib80211_crypt_deinit_handler(unsigned long data
)
149 struct lib80211_crypt_info
*info
= (struct lib80211_crypt_info
*)data
;
152 lib80211_crypt_deinit_entries(info
, 0);
154 spin_lock_irqsave(info
->lock
, flags
);
155 if (!list_empty(&info
->crypt_deinit_list
) && !info
->crypt_quiesced
) {
156 printk(KERN_DEBUG
"%s: entries remaining in delayed crypt "
157 "deletion list\n", info
->name
);
158 info
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
159 add_timer(&info
->crypt_deinit_timer
);
161 spin_unlock_irqrestore(info
->lock
, flags
);
163 EXPORT_SYMBOL(lib80211_crypt_deinit_handler
);
165 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info
*info
,
166 struct lib80211_crypt_data
**crypt
)
168 struct lib80211_crypt_data
*tmp
;
177 /* must not run ops->deinit() while there may be pending encrypt or
178 * decrypt operations. Use a list of delayed deinits to avoid needing
181 spin_lock_irqsave(info
->lock
, flags
);
182 if (!info
->crypt_quiesced
) {
183 list_add(&tmp
->list
, &info
->crypt_deinit_list
);
184 if (!timer_pending(&info
->crypt_deinit_timer
)) {
185 info
->crypt_deinit_timer
.expires
= jiffies
+ HZ
;
186 add_timer(&info
->crypt_deinit_timer
);
189 spin_unlock_irqrestore(info
->lock
, flags
);
191 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit
);
193 int lib80211_register_crypto_ops(struct lib80211_crypto_ops
*ops
)
196 struct lib80211_crypto_alg
*alg
;
198 alg
= kzalloc(sizeof(*alg
), GFP_KERNEL
);
204 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
205 list_add(&alg
->list
, &lib80211_crypto_algs
);
206 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
208 printk(KERN_DEBUG
"lib80211_crypt: registered algorithm '%s'\n",
213 EXPORT_SYMBOL(lib80211_register_crypto_ops
);
215 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops
*ops
)
217 struct lib80211_crypto_alg
*alg
;
220 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
221 list_for_each_entry(alg
, &lib80211_crypto_algs
, list
) {
225 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
229 printk(KERN_DEBUG
"lib80211_crypt: unregistered algorithm '%s'\n",
231 list_del(&alg
->list
);
232 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
236 EXPORT_SYMBOL(lib80211_unregister_crypto_ops
);
238 struct lib80211_crypto_ops
*lib80211_get_crypto_ops(const char *name
)
240 struct lib80211_crypto_alg
*alg
;
243 spin_lock_irqsave(&lib80211_crypto_lock
, flags
);
244 list_for_each_entry(alg
, &lib80211_crypto_algs
, list
) {
245 if (strcmp(alg
->ops
->name
, name
) == 0)
248 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
252 spin_unlock_irqrestore(&lib80211_crypto_lock
, flags
);
255 EXPORT_SYMBOL(lib80211_get_crypto_ops
);
257 static void *lib80211_crypt_null_init(int keyidx
)
262 static void lib80211_crypt_null_deinit(void *priv
)
266 static struct lib80211_crypto_ops lib80211_crypt_null
= {
268 .init
= lib80211_crypt_null_init
,
269 .deinit
= lib80211_crypt_null_deinit
,
270 .owner
= THIS_MODULE
,
273 static int __init
lib80211_init(void)
275 pr_info(DRV_DESCRIPTION
"\n");
276 return lib80211_register_crypto_ops(&lib80211_crypt_null
);
279 static void __exit
lib80211_exit(void)
281 lib80211_unregister_crypto_ops(&lib80211_crypt_null
);
282 BUG_ON(!list_empty(&lib80211_crypto_algs
));
285 module_init(lib80211_init
);
286 module_exit(lib80211_exit
);