USB: UAS: fix disconnect by unplugging a hub
[linux/fpc-iii.git] / crypto / af_alg.c
blob1d4b0157ee5dc099e45bdd4ef247cc8c388bedcc
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * af_alg: User-space algorithm interface
5 * This file provides the user-space API for algorithms.
7 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8 */
10 #include <linux/atomic.h>
11 #include <crypto/if_alg.h>
12 #include <linux/crypto.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/net.h>
18 #include <linux/rwsem.h>
19 #include <linux/sched.h>
20 #include <linux/sched/signal.h>
21 #include <linux/security.h>
23 struct alg_type_list {
24 const struct af_alg_type *type;
25 struct list_head list;
28 static atomic_long_t alg_memory_allocated;
30 static struct proto alg_proto = {
31 .name = "ALG",
32 .owner = THIS_MODULE,
33 .memory_allocated = &alg_memory_allocated,
34 .obj_size = sizeof(struct alg_sock),
37 static LIST_HEAD(alg_types);
38 static DECLARE_RWSEM(alg_types_sem);
40 static const struct af_alg_type *alg_get_type(const char *name)
42 const struct af_alg_type *type = ERR_PTR(-ENOENT);
43 struct alg_type_list *node;
45 down_read(&alg_types_sem);
46 list_for_each_entry(node, &alg_types, list) {
47 if (strcmp(node->type->name, name))
48 continue;
50 if (try_module_get(node->type->owner))
51 type = node->type;
52 break;
54 up_read(&alg_types_sem);
56 return type;
59 int af_alg_register_type(const struct af_alg_type *type)
61 struct alg_type_list *node;
62 int err = -EEXIST;
64 down_write(&alg_types_sem);
65 list_for_each_entry(node, &alg_types, list) {
66 if (!strcmp(node->type->name, type->name))
67 goto unlock;
70 node = kmalloc(sizeof(*node), GFP_KERNEL);
71 err = -ENOMEM;
72 if (!node)
73 goto unlock;
75 type->ops->owner = THIS_MODULE;
76 if (type->ops_nokey)
77 type->ops_nokey->owner = THIS_MODULE;
78 node->type = type;
79 list_add(&node->list, &alg_types);
80 err = 0;
82 unlock:
83 up_write(&alg_types_sem);
85 return err;
87 EXPORT_SYMBOL_GPL(af_alg_register_type);
89 int af_alg_unregister_type(const struct af_alg_type *type)
91 struct alg_type_list *node;
92 int err = -ENOENT;
94 down_write(&alg_types_sem);
95 list_for_each_entry(node, &alg_types, list) {
96 if (strcmp(node->type->name, type->name))
97 continue;
99 list_del(&node->list);
100 kfree(node);
101 err = 0;
102 break;
104 up_write(&alg_types_sem);
106 return err;
108 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
110 static void alg_do_release(const struct af_alg_type *type, void *private)
112 if (!type)
113 return;
115 type->release(private);
116 module_put(type->owner);
119 int af_alg_release(struct socket *sock)
121 if (sock->sk) {
122 sock_put(sock->sk);
123 sock->sk = NULL;
125 return 0;
127 EXPORT_SYMBOL_GPL(af_alg_release);
129 void af_alg_release_parent(struct sock *sk)
131 struct alg_sock *ask = alg_sk(sk);
132 unsigned int nokey = atomic_read(&ask->nokey_refcnt);
134 sk = ask->parent;
135 ask = alg_sk(sk);
137 if (nokey)
138 atomic_dec(&ask->nokey_refcnt);
140 if (atomic_dec_and_test(&ask->refcnt))
141 sock_put(sk);
143 EXPORT_SYMBOL_GPL(af_alg_release_parent);
145 static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
147 const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
148 struct sock *sk = sock->sk;
149 struct alg_sock *ask = alg_sk(sk);
150 struct sockaddr_alg *sa = (void *)uaddr;
151 const struct af_alg_type *type;
152 void *private;
153 int err;
155 if (sock->state == SS_CONNECTED)
156 return -EINVAL;
158 if (addr_len < sizeof(*sa))
159 return -EINVAL;
161 /* If caller uses non-allowed flag, return error. */
162 if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
163 return -EINVAL;
165 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
166 sa->salg_name[sizeof(sa->salg_name) + addr_len - sizeof(*sa) - 1] = 0;
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);
174 if (IS_ERR(type))
175 return PTR_ERR(type);
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);
183 err = -EBUSY;
184 lock_sock(sk);
185 if (atomic_read(&ask->refcnt))
186 goto unlock;
188 swap(ask->type, type);
189 swap(ask->private, private);
191 err = 0;
193 unlock:
194 release_sock(sk);
196 alg_do_release(type, private);
198 return err;
201 static int alg_setkey(struct sock *sk, char __user *ukey,
202 unsigned int keylen)
204 struct alg_sock *ask = alg_sk(sk);
205 const struct af_alg_type *type = ask->type;
206 u8 *key;
207 int err;
209 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
210 if (!key)
211 return -ENOMEM;
213 err = -EFAULT;
214 if (copy_from_user(key, ukey, keylen))
215 goto out;
217 err = type->setkey(ask->private, key, keylen);
219 out:
220 sock_kzfree_s(sk, key, keylen);
222 return err;
225 static int alg_setsockopt(struct socket *sock, int level, int optname,
226 char __user *optval, unsigned int optlen)
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;
233 lock_sock(sk);
234 if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt))
235 goto unlock;
237 type = ask->type;
239 err = -ENOPROTOOPT;
240 if (level != SOL_ALG || !type)
241 goto unlock;
243 switch (optname) {
244 case ALG_SET_KEY:
245 if (sock->state == SS_CONNECTED)
246 goto unlock;
247 if (!type->setkey)
248 goto unlock;
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);
260 unlock:
261 release_sock(sk);
263 return err;
266 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
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;
274 lock_sock(sk);
275 type = ask->type;
277 err = -EINVAL;
278 if (!type)
279 goto unlock;
281 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
282 err = -ENOMEM;
283 if (!sk2)
284 goto unlock;
286 sock_init_data(newsock, sk2);
287 security_sock_graft(sk2, newsock);
288 security_sk_clone(sk, sk2);
290 err = type->accept(ask->private, sk2);
292 nokey = err == -ENOKEY;
293 if (nokey && type->accept_nokey)
294 err = type->accept_nokey(ask->private, sk2);
296 if (err)
297 goto unlock;
299 if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
300 sock_hold(sk);
301 if (nokey) {
302 atomic_inc(&ask->nokey_refcnt);
303 atomic_set(&alg_sk(sk2)->nokey_refcnt, 1);
305 alg_sk(sk2)->parent = sk;
306 alg_sk(sk2)->type = type;
308 newsock->ops = type->ops;
309 newsock->state = SS_CONNECTED;
311 if (nokey)
312 newsock->ops = type->ops_nokey;
314 err = 0;
316 unlock:
317 release_sock(sk);
319 return err;
321 EXPORT_SYMBOL_GPL(af_alg_accept);
323 static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
324 bool kern)
326 return af_alg_accept(sock->sk, newsock, kern);
329 static const struct proto_ops alg_proto_ops = {
330 .family = PF_ALG,
331 .owner = THIS_MODULE,
333 .connect = sock_no_connect,
334 .socketpair = sock_no_socketpair,
335 .getname = sock_no_getname,
336 .ioctl = sock_no_ioctl,
337 .listen = sock_no_listen,
338 .shutdown = sock_no_shutdown,
339 .getsockopt = sock_no_getsockopt,
340 .mmap = sock_no_mmap,
341 .sendpage = sock_no_sendpage,
342 .sendmsg = sock_no_sendmsg,
343 .recvmsg = sock_no_recvmsg,
345 .bind = alg_bind,
346 .release = af_alg_release,
347 .setsockopt = alg_setsockopt,
348 .accept = alg_accept,
351 static void alg_sock_destruct(struct sock *sk)
353 struct alg_sock *ask = alg_sk(sk);
355 alg_do_release(ask->type, ask->private);
358 static int alg_create(struct net *net, struct socket *sock, int protocol,
359 int kern)
361 struct sock *sk;
362 int err;
364 if (sock->type != SOCK_SEQPACKET)
365 return -ESOCKTNOSUPPORT;
366 if (protocol != 0)
367 return -EPROTONOSUPPORT;
369 err = -ENOMEM;
370 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
371 if (!sk)
372 goto out;
374 sock->ops = &alg_proto_ops;
375 sock_init_data(sock, sk);
377 sk->sk_destruct = alg_sock_destruct;
379 return 0;
380 out:
381 return err;
384 static const struct net_proto_family alg_family = {
385 .family = PF_ALG,
386 .create = alg_create,
387 .owner = THIS_MODULE,
390 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
392 size_t off;
393 ssize_t n;
394 int npages, i;
396 n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
397 if (n < 0)
398 return n;
400 npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
401 if (WARN_ON(npages == 0))
402 return -EINVAL;
403 /* Add one extra for linking */
404 sg_init_table(sgl->sg, npages + 1);
406 for (i = 0, len = n; i < npages; i++) {
407 int plen = min_t(int, len, PAGE_SIZE - off);
409 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
411 off = 0;
412 len -= plen;
414 sg_mark_end(sgl->sg + npages - 1);
415 sgl->npages = npages;
417 return n;
419 EXPORT_SYMBOL_GPL(af_alg_make_sg);
421 static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
422 struct af_alg_sgl *sgl_new)
424 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
425 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
428 void af_alg_free_sg(struct af_alg_sgl *sgl)
430 int i;
432 for (i = 0; i < sgl->npages; i++)
433 put_page(sgl->pages[i]);
435 EXPORT_SYMBOL_GPL(af_alg_free_sg);
437 static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
439 struct cmsghdr *cmsg;
441 for_each_cmsghdr(cmsg, msg) {
442 if (!CMSG_OK(msg, cmsg))
443 return -EINVAL;
444 if (cmsg->cmsg_level != SOL_ALG)
445 continue;
447 switch (cmsg->cmsg_type) {
448 case ALG_SET_IV:
449 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
450 return -EINVAL;
451 con->iv = (void *)CMSG_DATA(cmsg);
452 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
453 sizeof(*con->iv)))
454 return -EINVAL;
455 break;
457 case ALG_SET_OP:
458 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
459 return -EINVAL;
460 con->op = *(u32 *)CMSG_DATA(cmsg);
461 break;
463 case ALG_SET_AEAD_ASSOCLEN:
464 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
465 return -EINVAL;
466 con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
467 break;
469 default:
470 return -EINVAL;
474 return 0;
478 * af_alg_alloc_tsgl - allocate the TX SGL
480 * @sk socket of connection to user space
481 * @return: 0 upon success, < 0 upon error
483 static int af_alg_alloc_tsgl(struct sock *sk)
485 struct alg_sock *ask = alg_sk(sk);
486 struct af_alg_ctx *ctx = ask->private;
487 struct af_alg_tsgl *sgl;
488 struct scatterlist *sg = NULL;
490 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
491 if (!list_empty(&ctx->tsgl_list))
492 sg = sgl->sg;
494 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
495 sgl = sock_kmalloc(sk,
496 struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
497 GFP_KERNEL);
498 if (!sgl)
499 return -ENOMEM;
501 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
502 sgl->cur = 0;
504 if (sg)
505 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
507 list_add_tail(&sgl->list, &ctx->tsgl_list);
510 return 0;
514 * aead_count_tsgl - Count number of TX SG entries
516 * The counting starts from the beginning of the SGL to @bytes. If
517 * an offset is provided, the counting of the SG entries starts at the offset.
519 * @sk socket of connection to user space
520 * @bytes Count the number of SG entries holding given number of bytes.
521 * @offset Start the counting of SG entries from the given offset.
522 * @return Number of TX SG entries found given the constraints
524 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
526 const struct alg_sock *ask = alg_sk(sk);
527 const struct af_alg_ctx *ctx = ask->private;
528 const struct af_alg_tsgl *sgl;
529 unsigned int i;
530 unsigned int sgl_count = 0;
532 if (!bytes)
533 return 0;
535 list_for_each_entry(sgl, &ctx->tsgl_list, list) {
536 const struct scatterlist *sg = sgl->sg;
538 for (i = 0; i < sgl->cur; i++) {
539 size_t bytes_count;
541 /* Skip offset */
542 if (offset >= sg[i].length) {
543 offset -= sg[i].length;
544 bytes -= sg[i].length;
545 continue;
548 bytes_count = sg[i].length - offset;
550 offset = 0;
551 sgl_count++;
553 /* If we have seen requested number of bytes, stop */
554 if (bytes_count >= bytes)
555 return sgl_count;
557 bytes -= bytes_count;
561 return sgl_count;
563 EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
566 * aead_pull_tsgl - Release the specified buffers from TX SGL
568 * If @dst is non-null, reassign the pages to dst. The caller must release
569 * the pages. If @dst_offset is given only reassign the pages to @dst starting
570 * at the @dst_offset (byte). The caller must ensure that @dst is large
571 * enough (e.g. by using af_alg_count_tsgl with the same offset).
573 * @sk socket of connection to user space
574 * @used Number of bytes to pull from TX SGL
575 * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
576 * caller must release the buffers in dst.
577 * @dst_offset Reassign the TX SGL from given offset. All buffers before
578 * reaching the offset is released.
580 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
581 size_t dst_offset)
583 struct alg_sock *ask = alg_sk(sk);
584 struct af_alg_ctx *ctx = ask->private;
585 struct af_alg_tsgl *sgl;
586 struct scatterlist *sg;
587 unsigned int i, j = 0;
589 while (!list_empty(&ctx->tsgl_list)) {
590 sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
591 list);
592 sg = sgl->sg;
594 for (i = 0; i < sgl->cur; i++) {
595 size_t plen = min_t(size_t, used, sg[i].length);
596 struct page *page = sg_page(sg + i);
598 if (!page)
599 continue;
602 * Assumption: caller created af_alg_count_tsgl(len)
603 * SG entries in dst.
605 if (dst) {
606 if (dst_offset >= plen) {
607 /* discard page before offset */
608 dst_offset -= plen;
609 } else {
610 /* reassign page to dst after offset */
611 get_page(page);
612 sg_set_page(dst + j, page,
613 plen - dst_offset,
614 sg[i].offset + dst_offset);
615 dst_offset = 0;
616 j++;
620 sg[i].length -= plen;
621 sg[i].offset += plen;
623 used -= plen;
624 ctx->used -= plen;
626 if (sg[i].length)
627 return;
629 put_page(page);
630 sg_assign_page(sg + i, NULL);
633 list_del(&sgl->list);
634 sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
637 if (!ctx->used)
638 ctx->merge = 0;
639 ctx->init = ctx->more;
641 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
644 * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
646 * @areq Request holding the TX and RX SGL
648 static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
650 struct sock *sk = areq->sk;
651 struct alg_sock *ask = alg_sk(sk);
652 struct af_alg_ctx *ctx = ask->private;
653 struct af_alg_rsgl *rsgl, *tmp;
654 struct scatterlist *tsgl;
655 struct scatterlist *sg;
656 unsigned int i;
658 list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
659 atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
660 af_alg_free_sg(&rsgl->sgl);
661 list_del(&rsgl->list);
662 if (rsgl != &areq->first_rsgl)
663 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
666 tsgl = areq->tsgl;
667 if (tsgl) {
668 for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
669 if (!sg_page(sg))
670 continue;
671 put_page(sg_page(sg));
674 sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
679 * af_alg_wait_for_wmem - wait for availability of writable memory
681 * @sk socket of connection to user space
682 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
683 * @return 0 when writable memory is available, < 0 upon error
685 static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
687 DEFINE_WAIT_FUNC(wait, woken_wake_function);
688 int err = -ERESTARTSYS;
689 long timeout;
691 if (flags & MSG_DONTWAIT)
692 return -EAGAIN;
694 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
696 add_wait_queue(sk_sleep(sk), &wait);
697 for (;;) {
698 if (signal_pending(current))
699 break;
700 timeout = MAX_SCHEDULE_TIMEOUT;
701 if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
702 err = 0;
703 break;
706 remove_wait_queue(sk_sleep(sk), &wait);
708 return err;
712 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
714 * @sk socket of connection to user space
716 void af_alg_wmem_wakeup(struct sock *sk)
718 struct socket_wq *wq;
720 if (!af_alg_writable(sk))
721 return;
723 rcu_read_lock();
724 wq = rcu_dereference(sk->sk_wq);
725 if (skwq_has_sleeper(wq))
726 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
727 EPOLLRDNORM |
728 EPOLLRDBAND);
729 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
730 rcu_read_unlock();
732 EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
735 * af_alg_wait_for_data - wait for availability of TX data
737 * @sk socket of connection to user space
738 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
739 * @min Set to minimum request size if partial requests are allowed.
740 * @return 0 when writable memory is available, < 0 upon error
742 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
744 DEFINE_WAIT_FUNC(wait, woken_wake_function);
745 struct alg_sock *ask = alg_sk(sk);
746 struct af_alg_ctx *ctx = ask->private;
747 long timeout;
748 int err = -ERESTARTSYS;
750 if (flags & MSG_DONTWAIT)
751 return -EAGAIN;
753 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
755 add_wait_queue(sk_sleep(sk), &wait);
756 for (;;) {
757 if (signal_pending(current))
758 break;
759 timeout = MAX_SCHEDULE_TIMEOUT;
760 if (sk_wait_event(sk, &timeout,
761 ctx->init && (!ctx->more ||
762 (min && ctx->used >= min)),
763 &wait)) {
764 err = 0;
765 break;
768 remove_wait_queue(sk_sleep(sk), &wait);
770 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
772 return err;
774 EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
777 * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
779 * @sk socket of connection to user space
781 static void af_alg_data_wakeup(struct sock *sk)
783 struct alg_sock *ask = alg_sk(sk);
784 struct af_alg_ctx *ctx = ask->private;
785 struct socket_wq *wq;
787 if (!ctx->used)
788 return;
790 rcu_read_lock();
791 wq = rcu_dereference(sk->sk_wq);
792 if (skwq_has_sleeper(wq))
793 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
794 EPOLLRDNORM |
795 EPOLLRDBAND);
796 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
797 rcu_read_unlock();
801 * af_alg_sendmsg - implementation of sendmsg system call handler
803 * The sendmsg system call handler obtains the user data and stores it
804 * in ctx->tsgl_list. This implies allocation of the required numbers of
805 * struct af_alg_tsgl.
807 * In addition, the ctx is filled with the information sent via CMSG.
809 * @sock socket of connection to user space
810 * @msg message from user space
811 * @size size of message from user space
812 * @ivsize the size of the IV for the cipher operation to verify that the
813 * user-space-provided IV has the right size
814 * @return the number of copied data upon success, < 0 upon error
816 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
817 unsigned int ivsize)
819 struct sock *sk = sock->sk;
820 struct alg_sock *ask = alg_sk(sk);
821 struct af_alg_ctx *ctx = ask->private;
822 struct af_alg_tsgl *sgl;
823 struct af_alg_control con = {};
824 long copied = 0;
825 bool enc = 0;
826 bool init = 0;
827 int err = 0;
829 if (msg->msg_controllen) {
830 err = af_alg_cmsg_send(msg, &con);
831 if (err)
832 return err;
834 init = 1;
835 switch (con.op) {
836 case ALG_OP_ENCRYPT:
837 enc = 1;
838 break;
839 case ALG_OP_DECRYPT:
840 enc = 0;
841 break;
842 default:
843 return -EINVAL;
846 if (con.iv && con.iv->ivlen != ivsize)
847 return -EINVAL;
850 lock_sock(sk);
851 if (ctx->init && !ctx->more) {
852 if (ctx->used) {
853 err = -EINVAL;
854 goto unlock;
857 pr_info_once(
858 "%s sent an empty control message without MSG_MORE.\n",
859 current->comm);
861 ctx->init = true;
863 if (init) {
864 ctx->enc = enc;
865 if (con.iv)
866 memcpy(ctx->iv, con.iv->iv, ivsize);
868 ctx->aead_assoclen = con.aead_assoclen;
871 while (size) {
872 struct scatterlist *sg;
873 size_t len = size;
874 size_t plen;
876 /* use the existing memory in an allocated page */
877 if (ctx->merge) {
878 sgl = list_entry(ctx->tsgl_list.prev,
879 struct af_alg_tsgl, list);
880 sg = sgl->sg + sgl->cur - 1;
881 len = min_t(size_t, len,
882 PAGE_SIZE - sg->offset - sg->length);
884 err = memcpy_from_msg(page_address(sg_page(sg)) +
885 sg->offset + sg->length,
886 msg, len);
887 if (err)
888 goto unlock;
890 sg->length += len;
891 ctx->merge = (sg->offset + sg->length) &
892 (PAGE_SIZE - 1);
894 ctx->used += len;
895 copied += len;
896 size -= len;
897 continue;
900 if (!af_alg_writable(sk)) {
901 err = af_alg_wait_for_wmem(sk, msg->msg_flags);
902 if (err)
903 goto unlock;
906 /* allocate a new page */
907 len = min_t(unsigned long, len, af_alg_sndbuf(sk));
909 err = af_alg_alloc_tsgl(sk);
910 if (err)
911 goto unlock;
913 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
914 list);
915 sg = sgl->sg;
916 if (sgl->cur)
917 sg_unmark_end(sg + sgl->cur - 1);
919 do {
920 unsigned int i = sgl->cur;
922 plen = min_t(size_t, len, PAGE_SIZE);
924 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
925 if (!sg_page(sg + i)) {
926 err = -ENOMEM;
927 goto unlock;
930 err = memcpy_from_msg(page_address(sg_page(sg + i)),
931 msg, plen);
932 if (err) {
933 __free_page(sg_page(sg + i));
934 sg_assign_page(sg + i, NULL);
935 goto unlock;
938 sg[i].length = plen;
939 len -= plen;
940 ctx->used += plen;
941 copied += plen;
942 size -= plen;
943 sgl->cur++;
944 } while (len && sgl->cur < MAX_SGL_ENTS);
946 if (!size)
947 sg_mark_end(sg + sgl->cur - 1);
949 ctx->merge = plen & (PAGE_SIZE - 1);
952 err = 0;
954 ctx->more = msg->msg_flags & MSG_MORE;
956 unlock:
957 af_alg_data_wakeup(sk);
958 release_sock(sk);
960 return copied ?: err;
962 EXPORT_SYMBOL_GPL(af_alg_sendmsg);
965 * af_alg_sendpage - sendpage system call handler
967 * This is a generic implementation of sendpage to fill ctx->tsgl_list.
969 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
970 int offset, size_t size, int flags)
972 struct sock *sk = sock->sk;
973 struct alg_sock *ask = alg_sk(sk);
974 struct af_alg_ctx *ctx = ask->private;
975 struct af_alg_tsgl *sgl;
976 int err = -EINVAL;
978 if (flags & MSG_SENDPAGE_NOTLAST)
979 flags |= MSG_MORE;
981 lock_sock(sk);
982 if (!ctx->more && ctx->used)
983 goto unlock;
985 if (!size)
986 goto done;
988 if (!af_alg_writable(sk)) {
989 err = af_alg_wait_for_wmem(sk, flags);
990 if (err)
991 goto unlock;
994 err = af_alg_alloc_tsgl(sk);
995 if (err)
996 goto unlock;
998 ctx->merge = 0;
999 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
1001 if (sgl->cur)
1002 sg_unmark_end(sgl->sg + sgl->cur - 1);
1004 sg_mark_end(sgl->sg + sgl->cur);
1006 get_page(page);
1007 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
1008 sgl->cur++;
1009 ctx->used += size;
1011 done:
1012 ctx->more = flags & MSG_MORE;
1014 unlock:
1015 af_alg_data_wakeup(sk);
1016 release_sock(sk);
1018 return err ?: size;
1020 EXPORT_SYMBOL_GPL(af_alg_sendpage);
1023 * af_alg_free_resources - release resources required for crypto request
1025 void af_alg_free_resources(struct af_alg_async_req *areq)
1027 struct sock *sk = areq->sk;
1029 af_alg_free_areq_sgls(areq);
1030 sock_kfree_s(sk, areq, areq->areqlen);
1032 EXPORT_SYMBOL_GPL(af_alg_free_resources);
1035 * af_alg_async_cb - AIO callback handler
1037 * This handler cleans up the struct af_alg_async_req upon completion of the
1038 * AIO operation.
1040 * The number of bytes to be generated with the AIO operation must be set
1041 * in areq->outlen before the AIO callback handler is invoked.
1043 void af_alg_async_cb(struct crypto_async_request *_req, int err)
1045 struct af_alg_async_req *areq = _req->data;
1046 struct sock *sk = areq->sk;
1047 struct kiocb *iocb = areq->iocb;
1048 unsigned int resultlen;
1050 /* Buffer size written by crypto operation. */
1051 resultlen = areq->outlen;
1053 af_alg_free_resources(areq);
1054 sock_put(sk);
1056 iocb->ki_complete(iocb, err ? err : (int)resultlen, 0);
1058 EXPORT_SYMBOL_GPL(af_alg_async_cb);
1061 * af_alg_poll - poll system call handler
1063 __poll_t af_alg_poll(struct file *file, struct socket *sock,
1064 poll_table *wait)
1066 struct sock *sk = sock->sk;
1067 struct alg_sock *ask = alg_sk(sk);
1068 struct af_alg_ctx *ctx = ask->private;
1069 __poll_t mask;
1071 sock_poll_wait(file, sock, wait);
1072 mask = 0;
1074 if (!ctx->more || ctx->used)
1075 mask |= EPOLLIN | EPOLLRDNORM;
1077 if (af_alg_writable(sk))
1078 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1080 return mask;
1082 EXPORT_SYMBOL_GPL(af_alg_poll);
1085 * af_alg_alloc_areq - allocate struct af_alg_async_req
1087 * @sk socket of connection to user space
1088 * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
1089 * @return allocated data structure or ERR_PTR upon error
1091 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
1092 unsigned int areqlen)
1094 struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
1096 if (unlikely(!areq))
1097 return ERR_PTR(-ENOMEM);
1099 areq->areqlen = areqlen;
1100 areq->sk = sk;
1101 areq->last_rsgl = NULL;
1102 INIT_LIST_HEAD(&areq->rsgl_list);
1103 areq->tsgl = NULL;
1104 areq->tsgl_entries = 0;
1106 return areq;
1108 EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
1111 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
1112 * operation
1114 * @sk socket of connection to user space
1115 * @msg user space message
1116 * @flags flags used to invoke recvmsg with
1117 * @areq instance of the cryptographic request that will hold the RX SGL
1118 * @maxsize maximum number of bytes to be pulled from user space
1119 * @outlen number of bytes in the RX SGL
1120 * @return 0 on success, < 0 upon error
1122 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
1123 struct af_alg_async_req *areq, size_t maxsize,
1124 size_t *outlen)
1126 struct alg_sock *ask = alg_sk(sk);
1127 struct af_alg_ctx *ctx = ask->private;
1128 size_t len = 0;
1130 while (maxsize > len && msg_data_left(msg)) {
1131 struct af_alg_rsgl *rsgl;
1132 size_t seglen;
1133 int err;
1135 /* limit the amount of readable buffers */
1136 if (!af_alg_readable(sk))
1137 break;
1139 seglen = min_t(size_t, (maxsize - len),
1140 msg_data_left(msg));
1142 if (list_empty(&areq->rsgl_list)) {
1143 rsgl = &areq->first_rsgl;
1144 } else {
1145 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
1146 if (unlikely(!rsgl))
1147 return -ENOMEM;
1150 rsgl->sgl.npages = 0;
1151 list_add_tail(&rsgl->list, &areq->rsgl_list);
1153 /* make one iovec available as scatterlist */
1154 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
1155 if (err < 0) {
1156 rsgl->sg_num_bytes = 0;
1157 return err;
1160 /* chain the new scatterlist with previous one */
1161 if (areq->last_rsgl)
1162 af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
1164 areq->last_rsgl = rsgl;
1165 len += err;
1166 atomic_add(err, &ctx->rcvused);
1167 rsgl->sg_num_bytes = err;
1168 iov_iter_advance(&msg->msg_iter, err);
1171 *outlen = len;
1172 return 0;
1174 EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
1176 static int __init af_alg_init(void)
1178 int err = proto_register(&alg_proto, 0);
1180 if (err)
1181 goto out;
1183 err = sock_register(&alg_family);
1184 if (err != 0)
1185 goto out_unregister_proto;
1187 out:
1188 return err;
1190 out_unregister_proto:
1191 proto_unregister(&alg_proto);
1192 goto out;
1195 static void __exit af_alg_exit(void)
1197 sock_unregister(PF_ALG);
1198 proto_unregister(&alg_proto);
1201 module_init(af_alg_init);
1202 module_exit(af_alg_exit);
1203 MODULE_LICENSE("GPL");
1204 MODULE_ALIAS_NETPROTO(AF_ALG);