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 atomic_long_t alg_memory_allocated;
30 static struct proto alg_proto = {
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))
50 if (try_module_get(node->type->owner))
54 up_read(&alg_types_sem);
59 int af_alg_register_type(const struct af_alg_type *type)
61 struct alg_type_list *node;
64 down_write(&alg_types_sem);
65 list_for_each_entry(node, &alg_types, list) {
66 if (!strcmp(node->type->name, type->name))
70 node = kmalloc(sizeof(*node), GFP_KERNEL);
75 type->ops->owner = THIS_MODULE;
77 type->ops_nokey->owner = THIS_MODULE;
79 list_add(&node->list, &alg_types);
83 up_write(&alg_types_sem);
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;
94 down_write(&alg_types_sem);
95 list_for_each_entry(node, &alg_types, list) {
96 if (strcmp(node->type->name, type->name))
99 list_del(&node->list);
104 up_write(&alg_types_sem);
108 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
110 static void alg_do_release(const struct af_alg_type *type, void *private)
115 type->release(private);
116 module_put(type->owner);
119 int af_alg_release(struct socket *sock)
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);
138 atomic_dec(&ask->nokey_refcnt);
140 if (atomic_dec_and_test(&ask->refcnt))
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_new *sa = (void *)uaddr;
151 const struct af_alg_type *type;
155 if (sock->state == SS_CONNECTED)
158 BUILD_BUG_ON(offsetof(struct sockaddr_alg_new, salg_name) !=
159 offsetof(struct sockaddr_alg, salg_name));
160 BUILD_BUG_ON(offsetof(struct sockaddr_alg, salg_name) != sizeof(*sa));
162 if (addr_len < sizeof(*sa) + 1)
165 /* If caller uses non-allowed flag, return error. */
166 if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
169 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
170 sa->salg_name[addr_len - sizeof(*sa) - 1] = 0;
172 type = alg_get_type(sa->salg_type);
173 if (PTR_ERR(type) == -ENOENT) {
174 request_module("algif-%s", sa->salg_type);
175 type = alg_get_type(sa->salg_type);
179 return PTR_ERR(type);
181 private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
182 if (IS_ERR(private)) {
183 module_put(type->owner);
184 return PTR_ERR(private);
189 if (atomic_read(&ask->refcnt))
192 swap(ask->type, type);
193 swap(ask->private, private);
200 alg_do_release(type, private);
205 static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen)
207 struct alg_sock *ask = alg_sk(sk);
208 const struct af_alg_type *type = ask->type;
212 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
217 if (copy_from_sockptr(key, ukey, keylen))
220 err = type->setkey(ask->private, key, keylen);
223 sock_kzfree_s(sk, key, keylen);
228 static int alg_setsockopt(struct socket *sock, int level, int optname,
229 sockptr_t optval, unsigned int optlen)
231 struct sock *sk = sock->sk;
232 struct alg_sock *ask = alg_sk(sk);
233 const struct af_alg_type *type;
237 if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt))
243 if (level != SOL_ALG || !type)
248 if (sock->state == SS_CONNECTED)
253 err = alg_setkey(sk, optval, optlen);
255 case ALG_SET_AEAD_AUTHSIZE:
256 if (sock->state == SS_CONNECTED)
258 if (!type->setauthsize)
260 err = type->setauthsize(ask->private, optlen);
262 case ALG_SET_DRBG_ENTROPY:
263 if (sock->state == SS_CONNECTED)
265 if (!type->setentropy)
268 err = type->setentropy(ask->private, optval, optlen);
277 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
279 struct alg_sock *ask = alg_sk(sk);
280 const struct af_alg_type *type;
292 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
297 sock_init_data(newsock, sk2);
298 security_sock_graft(sk2, newsock);
299 security_sk_clone(sk, sk2);
302 * newsock->ops assigned here to allow type->accept call to override
303 * them when required.
305 newsock->ops = type->ops;
306 err = type->accept(ask->private, sk2);
308 nokey = err == -ENOKEY;
309 if (nokey && type->accept_nokey)
310 err = type->accept_nokey(ask->private, sk2);
315 if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
318 atomic_inc(&ask->nokey_refcnt);
319 atomic_set(&alg_sk(sk2)->nokey_refcnt, 1);
321 alg_sk(sk2)->parent = sk;
322 alg_sk(sk2)->type = type;
324 newsock->state = SS_CONNECTED;
327 newsock->ops = type->ops_nokey;
336 EXPORT_SYMBOL_GPL(af_alg_accept);
338 static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
341 return af_alg_accept(sock->sk, newsock, kern);
344 static const struct proto_ops alg_proto_ops = {
346 .owner = THIS_MODULE,
348 .connect = sock_no_connect,
349 .socketpair = sock_no_socketpair,
350 .getname = sock_no_getname,
351 .ioctl = sock_no_ioctl,
352 .listen = sock_no_listen,
353 .shutdown = sock_no_shutdown,
354 .mmap = sock_no_mmap,
355 .sendpage = sock_no_sendpage,
356 .sendmsg = sock_no_sendmsg,
357 .recvmsg = sock_no_recvmsg,
360 .release = af_alg_release,
361 .setsockopt = alg_setsockopt,
362 .accept = alg_accept,
365 static void alg_sock_destruct(struct sock *sk)
367 struct alg_sock *ask = alg_sk(sk);
369 alg_do_release(ask->type, ask->private);
372 static int alg_create(struct net *net, struct socket *sock, int protocol,
378 if (sock->type != SOCK_SEQPACKET)
379 return -ESOCKTNOSUPPORT;
381 return -EPROTONOSUPPORT;
384 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
388 sock->ops = &alg_proto_ops;
389 sock_init_data(sock, sk);
391 sk->sk_destruct = alg_sock_destruct;
398 static const struct net_proto_family alg_family = {
400 .create = alg_create,
401 .owner = THIS_MODULE,
404 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
410 n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
414 npages = DIV_ROUND_UP(off + n, PAGE_SIZE);
415 if (WARN_ON(npages == 0))
417 /* Add one extra for linking */
418 sg_init_table(sgl->sg, npages + 1);
420 for (i = 0, len = n; i < npages; i++) {
421 int plen = min_t(int, len, PAGE_SIZE - off);
423 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
428 sg_mark_end(sgl->sg + npages - 1);
429 sgl->npages = npages;
433 EXPORT_SYMBOL_GPL(af_alg_make_sg);
435 static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
436 struct af_alg_sgl *sgl_new)
438 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
439 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
442 void af_alg_free_sg(struct af_alg_sgl *sgl)
446 for (i = 0; i < sgl->npages; i++)
447 put_page(sgl->pages[i]);
449 EXPORT_SYMBOL_GPL(af_alg_free_sg);
451 static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
453 struct cmsghdr *cmsg;
455 for_each_cmsghdr(cmsg, msg) {
456 if (!CMSG_OK(msg, cmsg))
458 if (cmsg->cmsg_level != SOL_ALG)
461 switch (cmsg->cmsg_type) {
463 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
465 con->iv = (void *)CMSG_DATA(cmsg);
466 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
472 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
474 con->op = *(u32 *)CMSG_DATA(cmsg);
477 case ALG_SET_AEAD_ASSOCLEN:
478 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
480 con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
492 * af_alg_alloc_tsgl - allocate the TX SGL
494 * @sk: socket of connection to user space
495 * Return: 0 upon success, < 0 upon error
497 static int af_alg_alloc_tsgl(struct sock *sk)
499 struct alg_sock *ask = alg_sk(sk);
500 struct af_alg_ctx *ctx = ask->private;
501 struct af_alg_tsgl *sgl;
502 struct scatterlist *sg = NULL;
504 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
505 if (!list_empty(&ctx->tsgl_list))
508 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
509 sgl = sock_kmalloc(sk,
510 struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
515 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
519 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
521 list_add_tail(&sgl->list, &ctx->tsgl_list);
528 * af_alg_count_tsgl - Count number of TX SG entries
530 * The counting starts from the beginning of the SGL to @bytes. If
531 * an @offset is provided, the counting of the SG entries starts at the @offset.
533 * @sk: socket of connection to user space
534 * @bytes: Count the number of SG entries holding given number of bytes.
535 * @offset: Start the counting of SG entries from the given offset.
536 * Return: Number of TX SG entries found given the constraints
538 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
540 const struct alg_sock *ask = alg_sk(sk);
541 const struct af_alg_ctx *ctx = ask->private;
542 const struct af_alg_tsgl *sgl;
544 unsigned int sgl_count = 0;
549 list_for_each_entry(sgl, &ctx->tsgl_list, list) {
550 const struct scatterlist *sg = sgl->sg;
552 for (i = 0; i < sgl->cur; i++) {
556 if (offset >= sg[i].length) {
557 offset -= sg[i].length;
558 bytes -= sg[i].length;
562 bytes_count = sg[i].length - offset;
567 /* If we have seen requested number of bytes, stop */
568 if (bytes_count >= bytes)
571 bytes -= bytes_count;
577 EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
580 * af_alg_pull_tsgl - Release the specified buffers from TX SGL
582 * If @dst is non-null, reassign the pages to @dst. The caller must release
583 * the pages. If @dst_offset is given only reassign the pages to @dst starting
584 * at the @dst_offset (byte). The caller must ensure that @dst is large
585 * enough (e.g. by using af_alg_count_tsgl with the same offset).
587 * @sk: socket of connection to user space
588 * @used: Number of bytes to pull from TX SGL
589 * @dst: If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
590 * caller must release the buffers in dst.
591 * @dst_offset: Reassign the TX SGL from given offset. All buffers before
592 * reaching the offset is released.
594 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
597 struct alg_sock *ask = alg_sk(sk);
598 struct af_alg_ctx *ctx = ask->private;
599 struct af_alg_tsgl *sgl;
600 struct scatterlist *sg;
601 unsigned int i, j = 0;
603 while (!list_empty(&ctx->tsgl_list)) {
604 sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
608 for (i = 0; i < sgl->cur; i++) {
609 size_t plen = min_t(size_t, used, sg[i].length);
610 struct page *page = sg_page(sg + i);
616 * Assumption: caller created af_alg_count_tsgl(len)
620 if (dst_offset >= plen) {
621 /* discard page before offset */
624 /* reassign page to dst after offset */
626 sg_set_page(dst + j, page,
628 sg[i].offset + dst_offset);
634 sg[i].length -= plen;
635 sg[i].offset += plen;
644 sg_assign_page(sg + i, NULL);
647 list_del(&sgl->list);
648 sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
653 ctx->init = ctx->more;
655 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
658 * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
660 * @areq: Request holding the TX and RX SGL
662 static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
664 struct sock *sk = areq->sk;
665 struct alg_sock *ask = alg_sk(sk);
666 struct af_alg_ctx *ctx = ask->private;
667 struct af_alg_rsgl *rsgl, *tmp;
668 struct scatterlist *tsgl;
669 struct scatterlist *sg;
672 list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
673 atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
674 af_alg_free_sg(&rsgl->sgl);
675 list_del(&rsgl->list);
676 if (rsgl != &areq->first_rsgl)
677 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
682 for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
685 put_page(sg_page(sg));
688 sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
693 * af_alg_wait_for_wmem - wait for availability of writable memory
695 * @sk: socket of connection to user space
696 * @flags: If MSG_DONTWAIT is set, then only report if function would sleep
697 * Return: 0 when writable memory is available, < 0 upon error
699 static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
701 DEFINE_WAIT_FUNC(wait, woken_wake_function);
702 int err = -ERESTARTSYS;
705 if (flags & MSG_DONTWAIT)
708 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
710 add_wait_queue(sk_sleep(sk), &wait);
712 if (signal_pending(current))
714 timeout = MAX_SCHEDULE_TIMEOUT;
715 if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
720 remove_wait_queue(sk_sleep(sk), &wait);
726 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
728 * @sk: socket of connection to user space
730 void af_alg_wmem_wakeup(struct sock *sk)
732 struct socket_wq *wq;
734 if (!af_alg_writable(sk))
738 wq = rcu_dereference(sk->sk_wq);
739 if (skwq_has_sleeper(wq))
740 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
743 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
746 EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
749 * af_alg_wait_for_data - wait for availability of TX data
751 * @sk: socket of connection to user space
752 * @flags: If MSG_DONTWAIT is set, then only report if function would sleep
753 * @min: Set to minimum request size if partial requests are allowed.
754 * Return: 0 when writable memory is available, < 0 upon error
756 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
758 DEFINE_WAIT_FUNC(wait, woken_wake_function);
759 struct alg_sock *ask = alg_sk(sk);
760 struct af_alg_ctx *ctx = ask->private;
762 int err = -ERESTARTSYS;
764 if (flags & MSG_DONTWAIT)
767 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
769 add_wait_queue(sk_sleep(sk), &wait);
771 if (signal_pending(current))
773 timeout = MAX_SCHEDULE_TIMEOUT;
774 if (sk_wait_event(sk, &timeout,
775 ctx->init && (!ctx->more ||
776 (min && ctx->used >= min)),
782 remove_wait_queue(sk_sleep(sk), &wait);
784 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
788 EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
791 * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
793 * @sk: socket of connection to user space
795 static void af_alg_data_wakeup(struct sock *sk)
797 struct alg_sock *ask = alg_sk(sk);
798 struct af_alg_ctx *ctx = ask->private;
799 struct socket_wq *wq;
805 wq = rcu_dereference(sk->sk_wq);
806 if (skwq_has_sleeper(wq))
807 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
810 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
815 * af_alg_sendmsg - implementation of sendmsg system call handler
817 * The sendmsg system call handler obtains the user data and stores it
818 * in ctx->tsgl_list. This implies allocation of the required numbers of
819 * struct af_alg_tsgl.
821 * In addition, the ctx is filled with the information sent via CMSG.
823 * @sock: socket of connection to user space
824 * @msg: message from user space
825 * @size: size of message from user space
826 * @ivsize: the size of the IV for the cipher operation to verify that the
827 * user-space-provided IV has the right size
828 * Return: the number of copied data upon success, < 0 upon error
830 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
833 struct sock *sk = sock->sk;
834 struct alg_sock *ask = alg_sk(sk);
835 struct af_alg_ctx *ctx = ask->private;
836 struct af_alg_tsgl *sgl;
837 struct af_alg_control con = {};
843 if (msg->msg_controllen) {
844 err = af_alg_cmsg_send(msg, &con);
860 if (con.iv && con.iv->ivlen != ivsize)
865 if (ctx->init && !ctx->more) {
872 "%s sent an empty control message without MSG_MORE.\n",
880 memcpy(ctx->iv, con.iv->iv, ivsize);
882 ctx->aead_assoclen = con.aead_assoclen;
886 struct scatterlist *sg;
890 /* use the existing memory in an allocated page */
892 sgl = list_entry(ctx->tsgl_list.prev,
893 struct af_alg_tsgl, list);
894 sg = sgl->sg + sgl->cur - 1;
895 len = min_t(size_t, len,
896 PAGE_SIZE - sg->offset - sg->length);
898 err = memcpy_from_msg(page_address(sg_page(sg)) +
899 sg->offset + sg->length,
905 ctx->merge = (sg->offset + sg->length) &
914 if (!af_alg_writable(sk)) {
915 err = af_alg_wait_for_wmem(sk, msg->msg_flags);
920 /* allocate a new page */
921 len = min_t(unsigned long, len, af_alg_sndbuf(sk));
923 err = af_alg_alloc_tsgl(sk);
927 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
931 sg_unmark_end(sg + sgl->cur - 1);
935 unsigned int i = sgl->cur;
937 plen = min_t(size_t, len, PAGE_SIZE);
939 pg = alloc_page(GFP_KERNEL);
945 sg_assign_page(sg + i, pg);
947 err = memcpy_from_msg(page_address(sg_page(sg + i)),
950 __free_page(sg_page(sg + i));
951 sg_assign_page(sg + i, NULL);
961 } while (len && sgl->cur < MAX_SGL_ENTS);
964 sg_mark_end(sg + sgl->cur - 1);
966 ctx->merge = plen & (PAGE_SIZE - 1);
971 ctx->more = msg->msg_flags & MSG_MORE;
974 af_alg_data_wakeup(sk);
977 return copied ?: err;
979 EXPORT_SYMBOL_GPL(af_alg_sendmsg);
982 * af_alg_sendpage - sendpage system call handler
983 * @sock: socket of connection to user space to write to
984 * @page: data to send
985 * @offset: offset into page to begin sending
986 * @size: length of data
987 * @flags: message send/receive flags
989 * This is a generic implementation of sendpage to fill ctx->tsgl_list.
991 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
992 int offset, size_t size, int flags)
994 struct sock *sk = sock->sk;
995 struct alg_sock *ask = alg_sk(sk);
996 struct af_alg_ctx *ctx = ask->private;
997 struct af_alg_tsgl *sgl;
1000 if (flags & MSG_SENDPAGE_NOTLAST)
1004 if (!ctx->more && ctx->used)
1010 if (!af_alg_writable(sk)) {
1011 err = af_alg_wait_for_wmem(sk, flags);
1016 err = af_alg_alloc_tsgl(sk);
1021 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
1024 sg_unmark_end(sgl->sg + sgl->cur - 1);
1026 sg_mark_end(sgl->sg + sgl->cur);
1029 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
1034 ctx->more = flags & MSG_MORE;
1037 af_alg_data_wakeup(sk);
1042 EXPORT_SYMBOL_GPL(af_alg_sendpage);
1045 * af_alg_free_resources - release resources required for crypto request
1046 * @areq: Request holding the TX and RX SGL
1048 void af_alg_free_resources(struct af_alg_async_req *areq)
1050 struct sock *sk = areq->sk;
1052 af_alg_free_areq_sgls(areq);
1053 sock_kfree_s(sk, areq, areq->areqlen);
1055 EXPORT_SYMBOL_GPL(af_alg_free_resources);
1058 * af_alg_async_cb - AIO callback handler
1059 * @_req: async request info
1060 * @err: if non-zero, error result to be returned via ki_complete();
1061 * otherwise return the AIO output length via ki_complete().
1063 * This handler cleans up the struct af_alg_async_req upon completion of the
1066 * The number of bytes to be generated with the AIO operation must be set
1067 * in areq->outlen before the AIO callback handler is invoked.
1069 void af_alg_async_cb(struct crypto_async_request *_req, int err)
1071 struct af_alg_async_req *areq = _req->data;
1072 struct sock *sk = areq->sk;
1073 struct kiocb *iocb = areq->iocb;
1074 unsigned int resultlen;
1076 /* Buffer size written by crypto operation. */
1077 resultlen = areq->outlen;
1079 af_alg_free_resources(areq);
1082 iocb->ki_complete(iocb, err ? err : (int)resultlen);
1084 EXPORT_SYMBOL_GPL(af_alg_async_cb);
1087 * af_alg_poll - poll system call handler
1088 * @file: file pointer
1089 * @sock: socket to poll
1092 __poll_t af_alg_poll(struct file *file, struct socket *sock,
1095 struct sock *sk = sock->sk;
1096 struct alg_sock *ask = alg_sk(sk);
1097 struct af_alg_ctx *ctx = ask->private;
1100 sock_poll_wait(file, sock, wait);
1103 if (!ctx->more || ctx->used)
1104 mask |= EPOLLIN | EPOLLRDNORM;
1106 if (af_alg_writable(sk))
1107 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1111 EXPORT_SYMBOL_GPL(af_alg_poll);
1114 * af_alg_alloc_areq - allocate struct af_alg_async_req
1116 * @sk: socket of connection to user space
1117 * @areqlen: size of struct af_alg_async_req + crypto_*_reqsize
1118 * Return: allocated data structure or ERR_PTR upon error
1120 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
1121 unsigned int areqlen)
1123 struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
1125 if (unlikely(!areq))
1126 return ERR_PTR(-ENOMEM);
1128 areq->areqlen = areqlen;
1130 areq->last_rsgl = NULL;
1131 INIT_LIST_HEAD(&areq->rsgl_list);
1133 areq->tsgl_entries = 0;
1137 EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
1140 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
1143 * @sk: socket of connection to user space
1144 * @msg: user space message
1145 * @flags: flags used to invoke recvmsg with
1146 * @areq: instance of the cryptographic request that will hold the RX SGL
1147 * @maxsize: maximum number of bytes to be pulled from user space
1148 * @outlen: number of bytes in the RX SGL
1149 * Return: 0 on success, < 0 upon error
1151 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
1152 struct af_alg_async_req *areq, size_t maxsize,
1155 struct alg_sock *ask = alg_sk(sk);
1156 struct af_alg_ctx *ctx = ask->private;
1159 while (maxsize > len && msg_data_left(msg)) {
1160 struct af_alg_rsgl *rsgl;
1164 /* limit the amount of readable buffers */
1165 if (!af_alg_readable(sk))
1168 seglen = min_t(size_t, (maxsize - len),
1169 msg_data_left(msg));
1171 if (list_empty(&areq->rsgl_list)) {
1172 rsgl = &areq->first_rsgl;
1174 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
1175 if (unlikely(!rsgl))
1179 rsgl->sgl.npages = 0;
1180 list_add_tail(&rsgl->list, &areq->rsgl_list);
1182 /* make one iovec available as scatterlist */
1183 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
1185 rsgl->sg_num_bytes = 0;
1189 /* chain the new scatterlist with previous one */
1190 if (areq->last_rsgl)
1191 af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
1193 areq->last_rsgl = rsgl;
1195 atomic_add(err, &ctx->rcvused);
1196 rsgl->sg_num_bytes = err;
1197 iov_iter_advance(&msg->msg_iter, err);
1203 EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
1205 static int __init af_alg_init(void)
1207 int err = proto_register(&alg_proto, 0);
1212 err = sock_register(&alg_family);
1214 goto out_unregister_proto;
1219 out_unregister_proto:
1220 proto_unregister(&alg_proto);
1224 static void __exit af_alg_exit(void)
1226 sock_unregister(PF_ALG);
1227 proto_unregister(&alg_proto);
1230 module_init(af_alg_init);
1231 module_exit(af_alg_exit);
1232 MODULE_LICENSE("GPL");
1233 MODULE_ALIAS_NETPROTO(AF_ALG);