1/*
2 * af_alg: User-space algorithm interface
3 *
4 * This file provides the user-space API for algorithms.
5 *
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 */
14
15#include <linux/atomic.h>
16#include <crypto/if_alg.h>
17#include <linux/crypto.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/net.h>
23#include <linux/rwsem.h>
24#include <linux/security.h>
25
26struct alg_type_list {
27	const struct af_alg_type *type;
28	struct list_head list;
29};
30
31static atomic_long_t alg_memory_allocated;
32
33static struct proto alg_proto = {
34	.name			= "ALG",
35	.owner			= THIS_MODULE,
36	.memory_allocated	= &alg_memory_allocated,
37	.obj_size		= sizeof(struct alg_sock),
38};
39
40static LIST_HEAD(alg_types);
41static DECLARE_RWSEM(alg_types_sem);
42
43static const struct af_alg_type *alg_get_type(const char *name)
44{
45	const struct af_alg_type *type = ERR_PTR(-ENOENT);
46	struct alg_type_list *node;
47
48	down_read(&alg_types_sem);
49	list_for_each_entry(node, &alg_types, list) {
50		if (strcmp(node->type->name, name))
51			continue;
52
53		if (try_module_get(node->type->owner))
54			type = node->type;
55		break;
56	}
57	up_read(&alg_types_sem);
58
59	return type;
60}
61
62int af_alg_register_type(const struct af_alg_type *type)
63{
64	struct alg_type_list *node;
65	int err = -EEXIST;
66
67	down_write(&alg_types_sem);
68	list_for_each_entry(node, &alg_types, list) {
69		if (!strcmp(node->type->name, type->name))
70			goto unlock;
71	}
72
73	node = kmalloc(sizeof(*node), GFP_KERNEL);
74	err = -ENOMEM;
75	if (!node)
76		goto unlock;
77
78	type->ops->owner = THIS_MODULE;
79	if (type->ops_nokey)
80		type->ops_nokey->owner = THIS_MODULE;
81	node->type = type;
82	list_add(&node->list, &alg_types);
83	err = 0;
84
85unlock:
86	up_write(&alg_types_sem);
87
88	return err;
89}
90EXPORT_SYMBOL_GPL(af_alg_register_type);
91
92int af_alg_unregister_type(const struct af_alg_type *type)
93{
94	struct alg_type_list *node;
95	int err = -ENOENT;
96
97	down_write(&alg_types_sem);
98	list_for_each_entry(node, &alg_types, list) {
99		if (strcmp(node->type->name, type->name))
100			continue;
101
102		list_del(&node->list);
103		kfree(node);
104		err = 0;
105		break;
106	}
107	up_write(&alg_types_sem);
108
109	return err;
110}
111EXPORT_SYMBOL_GPL(af_alg_unregister_type);
112
113static void alg_do_release(const struct af_alg_type *type, void *private)
114{
115	if (!type)
116		return;
117
118	type->release(private);
119	module_put(type->owner);
120}
121
122int af_alg_release(struct socket *sock)
123{
124	if (sock->sk)
125		sock_put(sock->sk);
126	return 0;
127}
128EXPORT_SYMBOL_GPL(af_alg_release);
129
130void af_alg_release_parent(struct sock *sk)
131{
132	struct alg_sock *ask = alg_sk(sk);
133	unsigned int nokey = ask->nokey_refcnt;
134	bool last = nokey && !ask->refcnt;
135
136	sk = ask->parent;
137	ask = alg_sk(sk);
138
139	lock_sock(sk);
140	ask->nokey_refcnt -= nokey;
141	if (!last)
142		last = !--ask->refcnt;
143	release_sock(sk);
144
145	if (last)
146		sock_put(sk);
147}
148EXPORT_SYMBOL_GPL(af_alg_release_parent);
149
150static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
151{
152	struct sock *sk = sock->sk;
153	struct alg_sock *ask = alg_sk(sk);
154	struct sockaddr_alg *sa = (void *)uaddr;
155	const struct af_alg_type *type;
156	void *private;
157	int err;
158
159	if (sock->state == SS_CONNECTED)
160		return -EINVAL;
161
162	if (addr_len != sizeof(*sa))
163		return -EINVAL;
164
165	sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
166	sa->salg_name[sizeof(sa->salg_name) - 1] = 0;
167
168	type = alg_get_type(sa->salg_type);
169	if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
170		request_module("algif-%s", sa->salg_type);
171		type = alg_get_type(sa->salg_type);
172	}
173
174	if (IS_ERR(type))
175		return PTR_ERR(type);
176
177	private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
178	if (IS_ERR(private)) {
179		module_put(type->owner);
180		return PTR_ERR(private);
181	}
182
183	err = -EBUSY;
184	lock_sock(sk);
185	if (ask->refcnt | ask->nokey_refcnt)
186		goto unlock;
187
188	swap(ask->type, type);
189	swap(ask->private, private);
190
191	err = 0;
192
193unlock:
194	release_sock(sk);
195
196	alg_do_release(type, private);
197
198	return err;
199}
200
201static int alg_setkey(struct sock *sk, char __user *ukey,
202		      unsigned int keylen)
203{
204	struct alg_sock *ask = alg_sk(sk);
205	const struct af_alg_type *type = ask->type;
206	u8 *key;
207	int err;
208
209	key = sock_kmalloc(sk, keylen, GFP_KERNEL);
210	if (!key)
211		return -ENOMEM;
212
213	err = -EFAULT;
214	if (copy_from_user(key, ukey, keylen))
215		goto out;
216
217	err = type->setkey(ask->private, key, keylen);
218
219out:
220	sock_kzfree_s(sk, key, keylen);
221
222	return err;
223}
224
225static int alg_setsockopt(struct socket *sock, int level, int optname,
226			  char __user *optval, unsigned int optlen)
227{
228	struct sock *sk = sock->sk;
229	struct alg_sock *ask = alg_sk(sk);
230	const struct af_alg_type *type;
231	int err = -EBUSY;
232
233	lock_sock(sk);
234	if (ask->refcnt)
235		goto unlock;
236
237	type = ask->type;
238
239	err = -ENOPROTOOPT;
240	if (level != SOL_ALG || !type)
241		goto unlock;
242
243	switch (optname) {
244	case ALG_SET_KEY:
245		if (sock->state == SS_CONNECTED)
246			goto unlock;
247		if (!type->setkey)
248			goto unlock;
249
250		err = alg_setkey(sk, optval, optlen);
251		break;
252	case ALG_SET_AEAD_AUTHSIZE:
253		if (sock->state == SS_CONNECTED)
254			goto unlock;
255		if (!type->setauthsize)
256			goto unlock;
257		err = type->setauthsize(ask->private, optlen);
258	}
259
260unlock:
261	release_sock(sk);
262
263	return err;
264}
265
266int af_alg_accept(struct sock *sk, struct socket *newsock)
267{
268	struct alg_sock *ask = alg_sk(sk);
269	const struct af_alg_type *type;
270	struct sock *sk2;
271	unsigned int nokey;
272	int err;
273
274	lock_sock(sk);
275	type = ask->type;
276
277	err = -EINVAL;
278	if (!type)
279		goto unlock;
280
281	sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto);
282	err = -ENOMEM;
283	if (!sk2)
284		goto unlock;
285
286	sock_init_data(newsock, sk2);
287	sock_graft(sk2, newsock);
288	security_sk_clone(sk, sk2);
289
290	err = type->accept(ask->private, sk2);
291
292	nokey = err == -ENOKEY;
293	if (nokey && type->accept_nokey)
294		err = type->accept_nokey(ask->private, sk2);
295
296	if (err)
297		goto unlock;
298
299	sk2->sk_family = PF_ALG;
300
301	if (nokey || !ask->refcnt++)
302		sock_hold(sk);
303	ask->nokey_refcnt += nokey;
304	alg_sk(sk2)->parent = sk;
305	alg_sk(sk2)->type = type;
306	alg_sk(sk2)->nokey_refcnt = nokey;
307
308	newsock->ops = type->ops;
309	newsock->state = SS_CONNECTED;
310
311	if (nokey)
312		newsock->ops = type->ops_nokey;
313
314	err = 0;
315
316unlock:
317	release_sock(sk);
318
319	return err;
320}
321EXPORT_SYMBOL_GPL(af_alg_accept);
322
323static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
324{
325	return af_alg_accept(sock->sk, newsock);
326}
327
328static const struct proto_ops alg_proto_ops = {
329	.family		=	PF_ALG,
330	.owner		=	THIS_MODULE,
331
332	.connect	=	sock_no_connect,
333	.socketpair	=	sock_no_socketpair,
334	.getname	=	sock_no_getname,
335	.ioctl		=	sock_no_ioctl,
336	.listen		=	sock_no_listen,
337	.shutdown	=	sock_no_shutdown,
338	.getsockopt	=	sock_no_getsockopt,
339	.mmap		=	sock_no_mmap,
340	.sendpage	=	sock_no_sendpage,
341	.sendmsg	=	sock_no_sendmsg,
342	.recvmsg	=	sock_no_recvmsg,
343	.poll		=	sock_no_poll,
344
345	.bind		=	alg_bind,
346	.release	=	af_alg_release,
347	.setsockopt	=	alg_setsockopt,
348	.accept		=	alg_accept,
349};
350
351static void alg_sock_destruct(struct sock *sk)
352{
353	struct alg_sock *ask = alg_sk(sk);
354
355	alg_do_release(ask->type, ask->private);
356}
357
358static int alg_create(struct net *net, struct socket *sock, int protocol,
359		      int kern)
360{
361	struct sock *sk;
362	int err;
363
364	if (sock->type != SOCK_SEQPACKET)
365		return -ESOCKTNOSUPPORT;
366	if (protocol != 0)
367		return -EPROTONOSUPPORT;
368
369	err = -ENOMEM;
370	sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto);
371	if (!sk)
372		goto out;
373
374	sock->ops = &alg_proto_ops;
375	sock_init_data(sock, sk);
376
377	sk->sk_family = PF_ALG;
378	sk->sk_destruct = alg_sock_destruct;
379
380	return 0;
381out:
382	return err;
383}
384
385static const struct net_proto_family alg_family = {
386	.family	=	PF_ALG,
387	.create	=	alg_create,
388	.owner	=	THIS_MODULE,
389};
390
391int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
392{
393	size_t off;
394	ssize_t n;
395	int npages, i;
396
397	n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
398	if (n < 0)
399		return n;
400
401	npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
402	if (WARN_ON(npages == 0))
403		return -EINVAL;
404	/* Add one extra for linking */
405	sg_init_table(sgl->sg, npages + 1);
406
407	for (i = 0, len = n; i < npages; i++) {
408		int plen = min_t(int, len, PAGE_SIZE - off);
409
410		sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
411
412		off = 0;
413		len -= plen;
414	}
415	sg_mark_end(sgl->sg + npages - 1);
416	sgl->npages = npages;
417
418	return n;
419}
420EXPORT_SYMBOL_GPL(af_alg_make_sg);
421
422void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new)
423{
424	sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
425	sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
426}
427EXPORT_SYMBOL_GPL(af_alg_link_sg);
428
429void af_alg_free_sg(struct af_alg_sgl *sgl)
430{
431	int i;
432
433	for (i = 0; i < sgl->npages; i++)
434		put_page(sgl->pages[i]);
435}
436EXPORT_SYMBOL_GPL(af_alg_free_sg);
437
438int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
439{
440	struct cmsghdr *cmsg;
441
442	for_each_cmsghdr(cmsg, msg) {
443		if (!CMSG_OK(msg, cmsg))
444			return -EINVAL;
445		if (cmsg->cmsg_level != SOL_ALG)
446			continue;
447
448		switch (cmsg->cmsg_type) {
449		case ALG_SET_IV:
450			if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
451				return -EINVAL;
452			con->iv = (void *)CMSG_DATA(cmsg);
453			if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
454						      sizeof(*con->iv)))
455				return -EINVAL;
456			break;
457
458		case ALG_SET_OP:
459			if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
460				return -EINVAL;
461			con->op = *(u32 *)CMSG_DATA(cmsg);
462			break;
463
464		case ALG_SET_AEAD_ASSOCLEN:
465			if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
466				return -EINVAL;
467			con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
468			break;
469
470		default:
471			return -EINVAL;
472		}
473	}
474
475	return 0;
476}
477EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
478
479int af_alg_wait_for_completion(int err, struct af_alg_completion *completion)
480{
481	switch (err) {
482	case -EINPROGRESS:
483	case -EBUSY:
484		wait_for_completion(&completion->completion);
485		reinit_completion(&completion->completion);
486		err = completion->err;
487		break;
488	};
489
490	return err;
491}
492EXPORT_SYMBOL_GPL(af_alg_wait_for_completion);
493
494void af_alg_complete(struct crypto_async_request *req, int err)
495{
496	struct af_alg_completion *completion = req->data;
497
498	if (err == -EINPROGRESS)
499		return;
500
501	completion->err = err;
502	complete(&completion->completion);
503}
504EXPORT_SYMBOL_GPL(af_alg_complete);
505
506static int __init af_alg_init(void)
507{
508	int err = proto_register(&alg_proto, 0);
509
510	if (err)
511		goto out;
512
513	err = sock_register(&alg_family);
514	if (err != 0)
515		goto out_unregister_proto;
516
517out:
518	return err;
519
520out_unregister_proto:
521	proto_unregister(&alg_proto);
522	goto out;
523}
524
525static void __exit af_alg_exit(void)
526{
527	sock_unregister(PF_ALG);
528	proto_unregister(&alg_proto);
529}
530
531module_init(af_alg_init);
532module_exit(af_alg_exit);
533MODULE_LICENSE("GPL");
534MODULE_ALIAS_NETPROTO(AF_ALG);
535