1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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 struct proto alg_proto = {
31 .obj_size = sizeof(struct alg_sock),
34 static LIST_HEAD(alg_types);
35 static DECLARE_RWSEM(alg_types_sem);
37 static const struct af_alg_type *alg_get_type(const char *name)
39 const struct af_alg_type *type = ERR_PTR(-ENOENT);
40 struct alg_type_list *node;
42 down_read(&alg_types_sem);
43 list_for_each_entry(node, &alg_types, list) {
44 if (strcmp(node->type->name, name))
47 if (try_module_get(node->type->owner))
51 up_read(&alg_types_sem);
56 int af_alg_register_type(const struct af_alg_type *type)
58 struct alg_type_list *node;
61 down_write(&alg_types_sem);
62 list_for_each_entry(node, &alg_types, list) {
63 if (!strcmp(node->type->name, type->name))
67 node = kmalloc(sizeof(*node), GFP_KERNEL);
72 type->ops->owner = THIS_MODULE;
74 type->ops_nokey->owner = THIS_MODULE;
76 list_add(&node->list, &alg_types);
80 up_write(&alg_types_sem);
84 EXPORT_SYMBOL_GPL(af_alg_register_type);
86 int af_alg_unregister_type(const struct af_alg_type *type)
88 struct alg_type_list *node;
91 down_write(&alg_types_sem);
92 list_for_each_entry(node, &alg_types, list) {
93 if (strcmp(node->type->name, type->name))
96 list_del(&node->list);
101 up_write(&alg_types_sem);
105 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
107 static void alg_do_release(const struct af_alg_type *type, void *private)
112 type->release(private);
113 module_put(type->owner);
116 int af_alg_release(struct socket *sock)
124 EXPORT_SYMBOL_GPL(af_alg_release);
126 void af_alg_release_parent(struct sock *sk)
128 struct alg_sock *ask = alg_sk(sk);
129 unsigned int nokey = atomic_read(&ask->nokey_refcnt);
135 atomic_dec(&ask->nokey_refcnt);
137 if (atomic_dec_and_test(&ask->refcnt))
140 EXPORT_SYMBOL_GPL(af_alg_release_parent);
142 static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
144 const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
145 struct sock *sk = sock->sk;
146 struct alg_sock *ask = alg_sk(sk);
147 struct sockaddr_alg_new *sa = (void *)uaddr;
148 const struct af_alg_type *type;
152 if (sock->state == SS_CONNECTED)
155 BUILD_BUG_ON(offsetof(struct sockaddr_alg_new, salg_name) !=
156 offsetof(struct sockaddr_alg, salg_name));
157 BUILD_BUG_ON(offsetof(struct sockaddr_alg, salg_name) != sizeof(*sa));
159 if (addr_len < sizeof(*sa) + 1)
162 /* If caller uses non-allowed flag, return error. */
163 if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
166 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
167 sa->salg_name[addr_len - sizeof(*sa) - 1] = 0;
169 type = alg_get_type(sa->salg_type);
170 if (PTR_ERR(type) == -ENOENT) {
171 request_module("algif-%s", sa->salg_type);
172 type = alg_get_type(sa->salg_type);
176 return PTR_ERR(type);
178 private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
179 if (IS_ERR(private)) {
180 module_put(type->owner);
181 return PTR_ERR(private);
186 if (atomic_read(&ask->refcnt))
189 swap(ask->type, type);
190 swap(ask->private, private);
197 alg_do_release(type, private);
202 static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen)
204 struct alg_sock *ask = alg_sk(sk);
205 const struct af_alg_type *type = ask->type;
209 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
214 if (copy_from_sockptr(key, ukey, keylen))
217 err = type->setkey(ask->private, key, keylen);
220 sock_kzfree_s(sk, key, keylen);
225 static int alg_setsockopt(struct socket *sock, int level, int optname,
226 sockptr_t 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;
234 if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt))
240 if (level != SOL_ALG || !type)
245 if (sock->state == SS_CONNECTED)
250 err = alg_setkey(sk, optval, optlen);
252 case ALG_SET_AEAD_AUTHSIZE:
253 if (sock->state == SS_CONNECTED)
255 if (!type->setauthsize)
257 err = type->setauthsize(ask->private, optlen);
259 case ALG_SET_DRBG_ENTROPY:
260 if (sock->state == SS_CONNECTED)
262 if (!type->setentropy)
265 err = type->setentropy(ask->private, optval, optlen);
274 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
276 struct alg_sock *ask = alg_sk(sk);
277 const struct af_alg_type *type;
289 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
294 sock_init_data(newsock, sk2);
295 security_sock_graft(sk2, newsock);
296 security_sk_clone(sk, sk2);
299 * newsock->ops assigned here to allow type->accept call to override
300 * them when required.
302 newsock->ops = type->ops;
303 err = type->accept(ask->private, sk2);
305 nokey = err == -ENOKEY;
306 if (nokey && type->accept_nokey)
307 err = type->accept_nokey(ask->private, sk2);
312 if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
315 atomic_inc(&ask->nokey_refcnt);
316 atomic_set(&alg_sk(sk2)->nokey_refcnt, 1);
318 alg_sk(sk2)->parent = sk;
319 alg_sk(sk2)->type = type;
321 newsock->state = SS_CONNECTED;
324 newsock->ops = type->ops_nokey;
333 EXPORT_SYMBOL_GPL(af_alg_accept);
335 static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
338 return af_alg_accept(sock->sk, newsock, kern);
341 static const struct proto_ops alg_proto_ops = {
343 .owner = THIS_MODULE,
345 .connect = sock_no_connect,
346 .socketpair = sock_no_socketpair,
347 .getname = sock_no_getname,
348 .ioctl = sock_no_ioctl,
349 .listen = sock_no_listen,
350 .shutdown = sock_no_shutdown,
351 .mmap = sock_no_mmap,
352 .sendpage = sock_no_sendpage,
353 .sendmsg = sock_no_sendmsg,
354 .recvmsg = sock_no_recvmsg,
357 .release = af_alg_release,
358 .setsockopt = alg_setsockopt,
359 .accept = alg_accept,
362 static void alg_sock_destruct(struct sock *sk)
364 struct alg_sock *ask = alg_sk(sk);
366 alg_do_release(ask->type, ask->private);
369 static int alg_create(struct net *net, struct socket *sock, int protocol,
375 if (sock->type != SOCK_SEQPACKET)
376 return -ESOCKTNOSUPPORT;
378 return -EPROTONOSUPPORT;
381 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
385 sock->ops = &alg_proto_ops;
386 sock_init_data(sock, sk);
388 sk->sk_destruct = alg_sock_destruct;
395 static const struct net_proto_family alg_family = {
397 .create = alg_create,
398 .owner = THIS_MODULE,
401 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
407 n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
411 npages = DIV_ROUND_UP(off + n, PAGE_SIZE);
412 if (WARN_ON(npages == 0))
414 /* Add one extra for linking */
415 sg_init_table(sgl->sg, npages + 1);
417 for (i = 0, len = n; i < npages; i++) {
418 int plen = min_t(int, len, PAGE_SIZE - off);
420 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
425 sg_mark_end(sgl->sg + npages - 1);
426 sgl->npages = npages;
430 EXPORT_SYMBOL_GPL(af_alg_make_sg);
432 static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
433 struct af_alg_sgl *sgl_new)
435 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
436 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
439 void af_alg_free_sg(struct af_alg_sgl *sgl)
443 for (i = 0; i < sgl->npages; i++)
444 put_page(sgl->pages[i]);
446 EXPORT_SYMBOL_GPL(af_alg_free_sg);
448 static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
450 struct cmsghdr *cmsg;
452 for_each_cmsghdr(cmsg, msg) {
453 if (!CMSG_OK(msg, cmsg))
455 if (cmsg->cmsg_level != SOL_ALG)
458 switch (cmsg->cmsg_type) {
460 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
462 con->iv = (void *)CMSG_DATA(cmsg);
463 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
469 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
471 con->op = *(u32 *)CMSG_DATA(cmsg);
474 case ALG_SET_AEAD_ASSOCLEN:
475 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
477 con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
489 * af_alg_alloc_tsgl - allocate the TX SGL
491 * @sk: socket of connection to user space
492 * Return: 0 upon success, < 0 upon error
494 static int af_alg_alloc_tsgl(struct sock *sk)
496 struct alg_sock *ask = alg_sk(sk);
497 struct af_alg_ctx *ctx = ask->private;
498 struct af_alg_tsgl *sgl;
499 struct scatterlist *sg = NULL;
501 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
502 if (!list_empty(&ctx->tsgl_list))
505 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
506 sgl = sock_kmalloc(sk,
507 struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
512 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
516 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
518 list_add_tail(&sgl->list, &ctx->tsgl_list);
525 * af_alg_count_tsgl - Count number of TX SG entries
527 * The counting starts from the beginning of the SGL to @bytes. If
528 * an @offset is provided, the counting of the SG entries starts at the @offset.
530 * @sk: socket of connection to user space
531 * @bytes: Count the number of SG entries holding given number of bytes.
532 * @offset: Start the counting of SG entries from the given offset.
533 * Return: Number of TX SG entries found given the constraints
535 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
537 const struct alg_sock *ask = alg_sk(sk);
538 const struct af_alg_ctx *ctx = ask->private;
539 const struct af_alg_tsgl *sgl;
541 unsigned int sgl_count = 0;
546 list_for_each_entry(sgl, &ctx->tsgl_list, list) {
547 const struct scatterlist *sg = sgl->sg;
549 for (i = 0; i < sgl->cur; i++) {
553 if (offset >= sg[i].length) {
554 offset -= sg[i].length;
555 bytes -= sg[i].length;
559 bytes_count = sg[i].length - offset;
564 /* If we have seen requested number of bytes, stop */
565 if (bytes_count >= bytes)
568 bytes -= bytes_count;
574 EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
577 * af_alg_pull_tsgl - Release the specified buffers from TX SGL
579 * If @dst is non-null, reassign the pages to @dst. The caller must release
580 * the pages. If @dst_offset is given only reassign the pages to @dst starting
581 * at the @dst_offset (byte). The caller must ensure that @dst is large
582 * enough (e.g. by using af_alg_count_tsgl with the same offset).
584 * @sk: socket of connection to user space
585 * @used: Number of bytes to pull from TX SGL
586 * @dst: If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
587 * caller must release the buffers in dst.
588 * @dst_offset: Reassign the TX SGL from given offset. All buffers before
589 * reaching the offset is released.
591 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
594 struct alg_sock *ask = alg_sk(sk);
595 struct af_alg_ctx *ctx = ask->private;
596 struct af_alg_tsgl *sgl;
597 struct scatterlist *sg;
598 unsigned int i, j = 0;
600 while (!list_empty(&ctx->tsgl_list)) {
601 sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
605 for (i = 0; i < sgl->cur; i++) {
606 size_t plen = min_t(size_t, used, sg[i].length);
607 struct page *page = sg_page(sg + i);
613 * Assumption: caller created af_alg_count_tsgl(len)
617 if (dst_offset >= plen) {
618 /* discard page before offset */
621 /* reassign page to dst after offset */
623 sg_set_page(dst + j, page,
625 sg[i].offset + dst_offset);
631 sg[i].length -= plen;
632 sg[i].offset += plen;
641 sg_assign_page(sg + i, NULL);
644 list_del(&sgl->list);
645 sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
650 ctx->init = ctx->more;
652 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
655 * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
657 * @areq: Request holding the TX and RX SGL
659 static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
661 struct sock *sk = areq->sk;
662 struct alg_sock *ask = alg_sk(sk);
663 struct af_alg_ctx *ctx = ask->private;
664 struct af_alg_rsgl *rsgl, *tmp;
665 struct scatterlist *tsgl;
666 struct scatterlist *sg;
669 list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
670 atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
671 af_alg_free_sg(&rsgl->sgl);
672 list_del(&rsgl->list);
673 if (rsgl != &areq->first_rsgl)
674 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
679 for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
682 put_page(sg_page(sg));
685 sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
690 * af_alg_wait_for_wmem - wait for availability of writable memory
692 * @sk: socket of connection to user space
693 * @flags: If MSG_DONTWAIT is set, then only report if function would sleep
694 * Return: 0 when writable memory is available, < 0 upon error
696 static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
698 DEFINE_WAIT_FUNC(wait, woken_wake_function);
699 int err = -ERESTARTSYS;
702 if (flags & MSG_DONTWAIT)
705 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
707 add_wait_queue(sk_sleep(sk), &wait);
709 if (signal_pending(current))
711 timeout = MAX_SCHEDULE_TIMEOUT;
712 if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
717 remove_wait_queue(sk_sleep(sk), &wait);
723 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
725 * @sk: socket of connection to user space
727 void af_alg_wmem_wakeup(struct sock *sk)
729 struct socket_wq *wq;
731 if (!af_alg_writable(sk))
735 wq = rcu_dereference(sk->sk_wq);
736 if (skwq_has_sleeper(wq))
737 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
740 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
743 EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
746 * af_alg_wait_for_data - wait for availability of TX data
748 * @sk: socket of connection to user space
749 * @flags: If MSG_DONTWAIT is set, then only report if function would sleep
750 * @min: Set to minimum request size if partial requests are allowed.
751 * Return: 0 when writable memory is available, < 0 upon error
753 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
755 DEFINE_WAIT_FUNC(wait, woken_wake_function);
756 struct alg_sock *ask = alg_sk(sk);
757 struct af_alg_ctx *ctx = ask->private;
759 int err = -ERESTARTSYS;
761 if (flags & MSG_DONTWAIT)
764 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
766 add_wait_queue(sk_sleep(sk), &wait);
768 if (signal_pending(current))
770 timeout = MAX_SCHEDULE_TIMEOUT;
771 if (sk_wait_event(sk, &timeout,
772 ctx->init && (!ctx->more ||
773 (min && ctx->used >= min)),
779 remove_wait_queue(sk_sleep(sk), &wait);
781 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
785 EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
788 * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
790 * @sk: socket of connection to user space
792 static void af_alg_data_wakeup(struct sock *sk)
794 struct alg_sock *ask = alg_sk(sk);
795 struct af_alg_ctx *ctx = ask->private;
796 struct socket_wq *wq;
802 wq = rcu_dereference(sk->sk_wq);
803 if (skwq_has_sleeper(wq))
804 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
807 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
812 * af_alg_sendmsg - implementation of sendmsg system call handler
814 * The sendmsg system call handler obtains the user data and stores it
815 * in ctx->tsgl_list. This implies allocation of the required numbers of
816 * struct af_alg_tsgl.
818 * In addition, the ctx is filled with the information sent via CMSG.
820 * @sock: socket of connection to user space
821 * @msg: message from user space
822 * @size: size of message from user space
823 * @ivsize: the size of the IV for the cipher operation to verify that the
824 * user-space-provided IV has the right size
825 * Return: the number of copied data upon success, < 0 upon error
827 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
830 struct sock *sk = sock->sk;
831 struct alg_sock *ask = alg_sk(sk);
832 struct af_alg_ctx *ctx = ask->private;
833 struct af_alg_tsgl *sgl;
834 struct af_alg_control con = {};
840 if (msg->msg_controllen) {
841 err = af_alg_cmsg_send(msg, &con);
857 if (con.iv && con.iv->ivlen != ivsize)
862 if (ctx->init && !ctx->more) {
869 "%s sent an empty control message without MSG_MORE.\n",
877 memcpy(ctx->iv, con.iv->iv, ivsize);
879 ctx->aead_assoclen = con.aead_assoclen;
883 struct scatterlist *sg;
887 /* use the existing memory in an allocated page */
889 sgl = list_entry(ctx->tsgl_list.prev,
890 struct af_alg_tsgl, list);
891 sg = sgl->sg + sgl->cur - 1;
892 len = min_t(size_t, len,
893 PAGE_SIZE - sg->offset - sg->length);
895 err = memcpy_from_msg(page_address(sg_page(sg)) +
896 sg->offset + sg->length,
902 ctx->merge = (sg->offset + sg->length) &
911 if (!af_alg_writable(sk)) {
912 err = af_alg_wait_for_wmem(sk, msg->msg_flags);
917 /* allocate a new page */
918 len = min_t(unsigned long, len, af_alg_sndbuf(sk));
920 err = af_alg_alloc_tsgl(sk);
924 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
928 sg_unmark_end(sg + sgl->cur - 1);
932 unsigned int i = sgl->cur;
934 plen = min_t(size_t, len, PAGE_SIZE);
936 pg = alloc_page(GFP_KERNEL);
942 sg_assign_page(sg + i, pg);
944 err = memcpy_from_msg(page_address(sg_page(sg + i)),
947 __free_page(sg_page(sg + i));
948 sg_assign_page(sg + i, NULL);
958 } while (len && sgl->cur < MAX_SGL_ENTS);
961 sg_mark_end(sg + sgl->cur - 1);
963 ctx->merge = plen & (PAGE_SIZE - 1);
968 ctx->more = msg->msg_flags & MSG_MORE;
971 af_alg_data_wakeup(sk);
974 return copied ?: err;
976 EXPORT_SYMBOL_GPL(af_alg_sendmsg);
979 * af_alg_sendpage - sendpage system call handler
980 * @sock: socket of connection to user space to write to
981 * @page: data to send
982 * @offset: offset into page to begin sending
983 * @size: length of data
984 * @flags: message send/receive flags
986 * This is a generic implementation of sendpage to fill ctx->tsgl_list.
988 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
989 int offset, size_t size, int flags)
991 struct sock *sk = sock->sk;
992 struct alg_sock *ask = alg_sk(sk);
993 struct af_alg_ctx *ctx = ask->private;
994 struct af_alg_tsgl *sgl;
997 if (flags & MSG_SENDPAGE_NOTLAST)
1001 if (!ctx->more && ctx->used)
1007 if (!af_alg_writable(sk)) {
1008 err = af_alg_wait_for_wmem(sk, flags);
1013 err = af_alg_alloc_tsgl(sk);
1018 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
1021 sg_unmark_end(sgl->sg + sgl->cur - 1);
1023 sg_mark_end(sgl->sg + sgl->cur);
1026 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
1031 ctx->more = flags & MSG_MORE;
1034 af_alg_data_wakeup(sk);
1039 EXPORT_SYMBOL_GPL(af_alg_sendpage);
1042 * af_alg_free_resources - release resources required for crypto request
1043 * @areq: Request holding the TX and RX SGL
1045 void af_alg_free_resources(struct af_alg_async_req *areq)
1047 struct sock *sk = areq->sk;
1049 af_alg_free_areq_sgls(areq);
1050 sock_kfree_s(sk, areq, areq->areqlen);
1052 EXPORT_SYMBOL_GPL(af_alg_free_resources);
1055 * af_alg_async_cb - AIO callback handler
1056 * @_req: async request info
1057 * @err: if non-zero, error result to be returned via ki_complete();
1058 * otherwise return the AIO output length via ki_complete().
1060 * This handler cleans up the struct af_alg_async_req upon completion of the
1063 * The number of bytes to be generated with the AIO operation must be set
1064 * in areq->outlen before the AIO callback handler is invoked.
1066 void af_alg_async_cb(struct crypto_async_request *_req, int err)
1068 struct af_alg_async_req *areq = _req->data;
1069 struct sock *sk = areq->sk;
1070 struct kiocb *iocb = areq->iocb;
1071 unsigned int resultlen;
1073 /* Buffer size written by crypto operation. */
1074 resultlen = areq->outlen;
1076 af_alg_free_resources(areq);
1079 iocb->ki_complete(iocb, err ? err : (int)resultlen);
1081 EXPORT_SYMBOL_GPL(af_alg_async_cb);
1084 * af_alg_poll - poll system call handler
1085 * @file: file pointer
1086 * @sock: socket to poll
1089 __poll_t af_alg_poll(struct file *file, struct socket *sock,
1092 struct sock *sk = sock->sk;
1093 struct alg_sock *ask = alg_sk(sk);
1094 struct af_alg_ctx *ctx = ask->private;
1097 sock_poll_wait(file, sock, wait);
1100 if (!ctx->more || ctx->used)
1101 mask |= EPOLLIN | EPOLLRDNORM;
1103 if (af_alg_writable(sk))
1104 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1108 EXPORT_SYMBOL_GPL(af_alg_poll);
1111 * af_alg_alloc_areq - allocate struct af_alg_async_req
1113 * @sk: socket of connection to user space
1114 * @areqlen: size of struct af_alg_async_req + crypto_*_reqsize
1115 * Return: allocated data structure or ERR_PTR upon error
1117 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
1118 unsigned int areqlen)
1120 struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
1122 if (unlikely(!areq))
1123 return ERR_PTR(-ENOMEM);
1125 areq->areqlen = areqlen;
1127 areq->last_rsgl = NULL;
1128 INIT_LIST_HEAD(&areq->rsgl_list);
1130 areq->tsgl_entries = 0;
1134 EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
1137 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
1140 * @sk: socket of connection to user space
1141 * @msg: user space message
1142 * @flags: flags used to invoke recvmsg with
1143 * @areq: instance of the cryptographic request that will hold the RX SGL
1144 * @maxsize: maximum number of bytes to be pulled from user space
1145 * @outlen: number of bytes in the RX SGL
1146 * Return: 0 on success, < 0 upon error
1148 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
1149 struct af_alg_async_req *areq, size_t maxsize,
1152 struct alg_sock *ask = alg_sk(sk);
1153 struct af_alg_ctx *ctx = ask->private;
1156 while (maxsize > len && msg_data_left(msg)) {
1157 struct af_alg_rsgl *rsgl;
1161 /* limit the amount of readable buffers */
1162 if (!af_alg_readable(sk))
1165 seglen = min_t(size_t, (maxsize - len),
1166 msg_data_left(msg));
1168 if (list_empty(&areq->rsgl_list)) {
1169 rsgl = &areq->first_rsgl;
1171 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
1172 if (unlikely(!rsgl))
1176 rsgl->sgl.npages = 0;
1177 list_add_tail(&rsgl->list, &areq->rsgl_list);
1179 /* make one iovec available as scatterlist */
1180 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
1182 rsgl->sg_num_bytes = 0;
1186 /* chain the new scatterlist with previous one */
1187 if (areq->last_rsgl)
1188 af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
1190 areq->last_rsgl = rsgl;
1192 atomic_add(err, &ctx->rcvused);
1193 rsgl->sg_num_bytes = err;
1194 iov_iter_advance(&msg->msg_iter, err);
1200 EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
1202 static int __init af_alg_init(void)
1204 int err = proto_register(&alg_proto, 0);
1209 err = sock_register(&alg_family);
1211 goto out_unregister_proto;
1216 out_unregister_proto:
1217 proto_unregister(&alg_proto);
1221 static void __exit af_alg_exit(void)
1223 sock_unregister(PF_ALG);
1224 proto_unregister(&alg_proto);
1227 module_init(af_alg_init);
1228 module_exit(af_alg_exit);
1229 MODULE_LICENSE("GPL");
1230 MODULE_ALIAS_NETPROTO(AF_ALG);