xref: /openbmc/linux/net/wireless/lib80211.c (revision 275876e2)
1 /*
2  * lib80211 -- common bits for IEEE802.11 drivers
3  *
4  * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
5  *
6  * Portions copied from old ieee80211 component, w/ original copyright
7  * notices below:
8  *
9  * Host AP crypto routines
10  *
11  * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
12  * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
13  *
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
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>
25 
26 #include <net/lib80211.h>
27 
28 #define DRV_NAME        "lib80211"
29 
30 #define DRV_DESCRIPTION	"common routines for IEEE802.11 drivers"
31 
32 MODULE_DESCRIPTION(DRV_DESCRIPTION);
33 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
34 MODULE_LICENSE("GPL");
35 
36 struct lib80211_crypto_alg {
37 	struct list_head list;
38 	struct lib80211_crypto_ops *ops;
39 };
40 
41 static LIST_HEAD(lib80211_crypto_algs);
42 static DEFINE_SPINLOCK(lib80211_crypto_lock);
43 
44 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
45 					  int force);
46 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
47 static void lib80211_crypt_deinit_handler(unsigned long data);
48 
49 const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
50 {
51 	const char *s = ssid;
52 	char *d = buf;
53 
54 	ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
55 	while (ssid_len--) {
56 		if (isprint(*s)) {
57 			*d++ = *s++;
58 			continue;
59 		}
60 
61 		*d++ = '\\';
62 		if (*s == '\0')
63 			*d++ = '0';
64 		else if (*s == '\n')
65 			*d++ = 'n';
66 		else if (*s == '\r')
67 			*d++ = 'r';
68 		else if (*s == '\t')
69 			*d++ = 't';
70 		else if (*s == '\\')
71 			*d++ = '\\';
72 		else
73 			d += snprintf(d, 3, "%03o", *s);
74 		s++;
75 	}
76 	*d = '\0';
77 	return buf;
78 }
79 EXPORT_SYMBOL(print_ssid);
80 
81 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
82 				spinlock_t *lock)
83 {
84 	memset(info, 0, sizeof(*info));
85 
86 	info->name = name;
87 	info->lock = lock;
88 
89 	INIT_LIST_HEAD(&info->crypt_deinit_list);
90 	setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
91 			(unsigned long)info);
92 
93 	return 0;
94 }
95 EXPORT_SYMBOL(lib80211_crypt_info_init);
96 
97 void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
98 {
99 	int i;
100 
101         lib80211_crypt_quiescing(info);
102         del_timer_sync(&info->crypt_deinit_timer);
103         lib80211_crypt_deinit_entries(info, 1);
104 
105         for (i = 0; i < NUM_WEP_KEYS; i++) {
106                 struct lib80211_crypt_data *crypt = info->crypt[i];
107                 if (crypt) {
108                         if (crypt->ops) {
109                                 crypt->ops->deinit(crypt->priv);
110                                 module_put(crypt->ops->owner);
111                         }
112                         kfree(crypt);
113                         info->crypt[i] = NULL;
114                 }
115         }
116 }
117 EXPORT_SYMBOL(lib80211_crypt_info_free);
118 
119 static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
120 					  int force)
121 {
122 	struct lib80211_crypt_data *entry, *next;
123 	unsigned long flags;
124 
125 	spin_lock_irqsave(info->lock, flags);
126 	list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
127 		if (atomic_read(&entry->refcnt) != 0 && !force)
128 			continue;
129 
130 		list_del(&entry->list);
131 
132 		if (entry->ops) {
133 			entry->ops->deinit(entry->priv);
134 			module_put(entry->ops->owner);
135 		}
136 		kfree(entry);
137 	}
138 	spin_unlock_irqrestore(info->lock, flags);
139 }
140 
141 /* After this, crypt_deinit_list won't accept new members */
142 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
143 {
144 	unsigned long flags;
145 
146 	spin_lock_irqsave(info->lock, flags);
147 	info->crypt_quiesced = 1;
148 	spin_unlock_irqrestore(info->lock, flags);
149 }
150 
151 static void lib80211_crypt_deinit_handler(unsigned long data)
152 {
153 	struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
154 	unsigned long flags;
155 
156 	lib80211_crypt_deinit_entries(info, 0);
157 
158 	spin_lock_irqsave(info->lock, flags);
159 	if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
160 		printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
161 		       "deletion list\n", info->name);
162 		info->crypt_deinit_timer.expires = jiffies + HZ;
163 		add_timer(&info->crypt_deinit_timer);
164 	}
165 	spin_unlock_irqrestore(info->lock, flags);
166 }
167 
168 void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
169 				    struct lib80211_crypt_data **crypt)
170 {
171 	struct lib80211_crypt_data *tmp;
172 	unsigned long flags;
173 
174 	if (*crypt == NULL)
175 		return;
176 
177 	tmp = *crypt;
178 	*crypt = NULL;
179 
180 	/* must not run ops->deinit() while there may be pending encrypt or
181 	 * decrypt operations. Use a list of delayed deinits to avoid needing
182 	 * locking. */
183 
184 	spin_lock_irqsave(info->lock, flags);
185 	if (!info->crypt_quiesced) {
186 		list_add(&tmp->list, &info->crypt_deinit_list);
187 		if (!timer_pending(&info->crypt_deinit_timer)) {
188 			info->crypt_deinit_timer.expires = jiffies + HZ;
189 			add_timer(&info->crypt_deinit_timer);
190 		}
191 	}
192 	spin_unlock_irqrestore(info->lock, flags);
193 }
194 EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
195 
196 int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
197 {
198 	unsigned long flags;
199 	struct lib80211_crypto_alg *alg;
200 
201 	alg = kzalloc(sizeof(*alg), GFP_KERNEL);
202 	if (alg == NULL)
203 		return -ENOMEM;
204 
205 	alg->ops = ops;
206 
207 	spin_lock_irqsave(&lib80211_crypto_lock, flags);
208 	list_add(&alg->list, &lib80211_crypto_algs);
209 	spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
210 
211 	printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
212 	       ops->name);
213 
214 	return 0;
215 }
216 EXPORT_SYMBOL(lib80211_register_crypto_ops);
217 
218 int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
219 {
220 	struct lib80211_crypto_alg *alg;
221 	unsigned long flags;
222 
223 	spin_lock_irqsave(&lib80211_crypto_lock, flags);
224 	list_for_each_entry(alg, &lib80211_crypto_algs, list) {
225 		if (alg->ops == ops)
226 			goto found;
227 	}
228 	spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
229 	return -EINVAL;
230 
231       found:
232 	printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
233 	       ops->name);
234 	list_del(&alg->list);
235 	spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
236 	kfree(alg);
237 	return 0;
238 }
239 EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
240 
241 struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
242 {
243 	struct lib80211_crypto_alg *alg;
244 	unsigned long flags;
245 
246 	spin_lock_irqsave(&lib80211_crypto_lock, flags);
247 	list_for_each_entry(alg, &lib80211_crypto_algs, list) {
248 		if (strcmp(alg->ops->name, name) == 0)
249 			goto found;
250 	}
251 	spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
252 	return NULL;
253 
254       found:
255 	spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
256 	return alg->ops;
257 }
258 EXPORT_SYMBOL(lib80211_get_crypto_ops);
259 
260 static void *lib80211_crypt_null_init(int keyidx)
261 {
262 	return (void *)1;
263 }
264 
265 static void lib80211_crypt_null_deinit(void *priv)
266 {
267 }
268 
269 static struct lib80211_crypto_ops lib80211_crypt_null = {
270 	.name = "NULL",
271 	.init = lib80211_crypt_null_init,
272 	.deinit = lib80211_crypt_null_deinit,
273 	.owner = THIS_MODULE,
274 };
275 
276 static int __init lib80211_init(void)
277 {
278 	pr_info(DRV_DESCRIPTION "\n");
279 	return lib80211_register_crypto_ops(&lib80211_crypt_null);
280 }
281 
282 static void __exit lib80211_exit(void)
283 {
284 	lib80211_unregister_crypto_ops(&lib80211_crypt_null);
285 	BUG_ON(!list_empty(&lib80211_crypto_algs));
286 }
287 
288 module_init(lib80211_init);
289 module_exit(lib80211_exit);
290