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.
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/signal.h>
20 #include <linux/security.h>
22 struct alg_type_list {
23 const struct af_alg_type *type;
24 struct list_head list;
27 static atomic_long_t alg_memory_allocated;
29 static struct proto alg_proto = {
32 .memory_allocated = &alg_memory_allocated,
33 .obj_size = sizeof(struct alg_sock),
36 static LIST_HEAD(alg_types);
37 static DECLARE_RWSEM(alg_types_sem);
39 static const struct af_alg_type *alg_get_type(const char *name)
41 const struct af_alg_type *type = ERR_PTR(-ENOENT);
42 struct alg_type_list *node;
44 down_read(&alg_types_sem);
45 list_for_each_entry(node, &alg_types, list) {
46 if (strcmp(node->type->name, name))
49 if (try_module_get(node->type->owner))
53 up_read(&alg_types_sem);
58 int af_alg_register_type(const struct af_alg_type *type)
60 struct alg_type_list *node;
63 down_write(&alg_types_sem);
64 list_for_each_entry(node, &alg_types, list) {
65 if (!strcmp(node->type->name, type->name))
69 node = kmalloc(sizeof(*node), GFP_KERNEL);
74 type->ops->owner = THIS_MODULE;
76 type->ops_nokey->owner = THIS_MODULE;
78 list_add(&node->list, &alg_types);
82 up_write(&alg_types_sem);
86 EXPORT_SYMBOL_GPL(af_alg_register_type);
88 int af_alg_unregister_type(const struct af_alg_type *type)
90 struct alg_type_list *node;
93 down_write(&alg_types_sem);
94 list_for_each_entry(node, &alg_types, list) {
95 if (strcmp(node->type->name, type->name))
98 list_del(&node->list);
103 up_write(&alg_types_sem);
107 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
109 static void alg_do_release(const struct af_alg_type *type, void *private)
114 type->release(private);
115 module_put(type->owner);
118 int af_alg_release(struct socket *sock)
126 EXPORT_SYMBOL_GPL(af_alg_release);
128 void af_alg_release_parent(struct sock *sk)
130 struct alg_sock *ask = alg_sk(sk);
131 unsigned int nokey = atomic_read(&ask->nokey_refcnt);
137 atomic_dec(&ask->nokey_refcnt);
139 if (atomic_dec_and_test(&ask->refcnt))
142 EXPORT_SYMBOL_GPL(af_alg_release_parent);
144 static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
146 const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
147 struct sock *sk = sock->sk;
148 struct alg_sock *ask = alg_sk(sk);
149 struct sockaddr_alg *sa = (void *)uaddr;
150 const struct af_alg_type *type;
154 if (sock->state == SS_CONNECTED)
157 if (addr_len < sizeof(*sa))
160 /* If caller uses non-allowed flag, return error. */
161 if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
164 sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
165 sa->salg_name[sizeof(sa->salg_name) + addr_len - sizeof(*sa) - 1] = 0;
167 type = alg_get_type(sa->salg_type);
168 if (PTR_ERR(type) == -ENOENT) {
169 request_module("algif-%s", sa->salg_type);
170 type = alg_get_type(sa->salg_type);
174 return PTR_ERR(type);
176 private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
177 if (IS_ERR(private)) {
178 module_put(type->owner);
179 return PTR_ERR(private);
184 if (atomic_read(&ask->refcnt))
187 swap(ask->type, type);
188 swap(ask->private, private);
195 alg_do_release(type, private);
200 static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen)
202 struct alg_sock *ask = alg_sk(sk);
203 const struct af_alg_type *type = ask->type;
207 key = sock_kmalloc(sk, keylen, GFP_KERNEL);
212 if (copy_from_sockptr(key, ukey, keylen))
215 err = type->setkey(ask->private, key, keylen);
218 sock_kzfree_s(sk, key, keylen);
223 static int alg_setsockopt(struct socket *sock, int level, int optname,
224 sockptr_t optval, unsigned int optlen)
226 struct sock *sk = sock->sk;
227 struct alg_sock *ask = alg_sk(sk);
228 const struct af_alg_type *type;
232 if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt))
238 if (level != SOL_ALG || !type)
243 if (sock->state == SS_CONNECTED)
248 err = alg_setkey(sk, optval, optlen);
250 case ALG_SET_AEAD_AUTHSIZE:
251 if (sock->state == SS_CONNECTED)
253 if (!type->setauthsize)
255 err = type->setauthsize(ask->private, optlen);
264 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
266 struct alg_sock *ask = alg_sk(sk);
267 const struct af_alg_type *type;
279 sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
284 sock_init_data(newsock, sk2);
285 security_sock_graft(sk2, newsock);
286 security_sk_clone(sk, sk2);
288 err = type->accept(ask->private, sk2);
290 nokey = err == -ENOKEY;
291 if (nokey && type->accept_nokey)
292 err = type->accept_nokey(ask->private, sk2);
297 if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
300 atomic_inc(&ask->nokey_refcnt);
301 atomic_set(&alg_sk(sk2)->nokey_refcnt, 1);
303 alg_sk(sk2)->parent = sk;
304 alg_sk(sk2)->type = type;
306 newsock->ops = type->ops;
307 newsock->state = SS_CONNECTED;
310 newsock->ops = type->ops_nokey;
319 EXPORT_SYMBOL_GPL(af_alg_accept);
321 static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
324 return af_alg_accept(sock->sk, newsock, kern);
327 static const struct proto_ops alg_proto_ops = {
329 .owner = THIS_MODULE,
331 .connect = sock_no_connect,
332 .socketpair = sock_no_socketpair,
333 .getname = sock_no_getname,
334 .ioctl = sock_no_ioctl,
335 .listen = sock_no_listen,
336 .shutdown = sock_no_shutdown,
337 .mmap = sock_no_mmap,
338 .sendpage = sock_no_sendpage,
339 .sendmsg = sock_no_sendmsg,
340 .recvmsg = sock_no_recvmsg,
343 .release = af_alg_release,
344 .setsockopt = alg_setsockopt,
345 .accept = alg_accept,
348 static void alg_sock_destruct(struct sock *sk)
350 struct alg_sock *ask = alg_sk(sk);
352 alg_do_release(ask->type, ask->private);
355 static int alg_create(struct net *net, struct socket *sock, int protocol,
361 if (sock->type != SOCK_SEQPACKET)
362 return -ESOCKTNOSUPPORT;
364 return -EPROTONOSUPPORT;
367 sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
371 sock->ops = &alg_proto_ops;
372 sock_init_data(sock, sk);
374 sk->sk_destruct = alg_sock_destruct;
381 static const struct net_proto_family alg_family = {
383 .create = alg_create,
384 .owner = THIS_MODULE,
387 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
393 n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
397 npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
398 if (WARN_ON(npages == 0))
400 /* Add one extra for linking */
401 sg_init_table(sgl->sg, npages + 1);
403 for (i = 0, len = n; i < npages; i++) {
404 int plen = min_t(int, len, PAGE_SIZE - off);
406 sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
411 sg_mark_end(sgl->sg + npages - 1);
412 sgl->npages = npages;
416 EXPORT_SYMBOL_GPL(af_alg_make_sg);
418 static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
419 struct af_alg_sgl *sgl_new)
421 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
422 sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
425 void af_alg_free_sg(struct af_alg_sgl *sgl)
429 for (i = 0; i < sgl->npages; i++)
430 put_page(sgl->pages[i]);
432 EXPORT_SYMBOL_GPL(af_alg_free_sg);
434 static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
436 struct cmsghdr *cmsg;
438 for_each_cmsghdr(cmsg, msg) {
439 if (!CMSG_OK(msg, cmsg))
441 if (cmsg->cmsg_level != SOL_ALG)
444 switch (cmsg->cmsg_type) {
446 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
448 con->iv = (void *)CMSG_DATA(cmsg);
449 if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
455 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
457 con->op = *(u32 *)CMSG_DATA(cmsg);
460 case ALG_SET_AEAD_ASSOCLEN:
461 if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
463 con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
475 * af_alg_alloc_tsgl - allocate the TX SGL
477 * @sk socket of connection to user space
478 * @return: 0 upon success, < 0 upon error
480 static int af_alg_alloc_tsgl(struct sock *sk)
482 struct alg_sock *ask = alg_sk(sk);
483 struct af_alg_ctx *ctx = ask->private;
484 struct af_alg_tsgl *sgl;
485 struct scatterlist *sg = NULL;
487 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
488 if (!list_empty(&ctx->tsgl_list))
491 if (!sg || sgl->cur >= MAX_SGL_ENTS) {
492 sgl = sock_kmalloc(sk,
493 struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
498 sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
502 sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
504 list_add_tail(&sgl->list, &ctx->tsgl_list);
511 * aead_count_tsgl - Count number of TX SG entries
513 * The counting starts from the beginning of the SGL to @bytes. If
514 * an offset is provided, the counting of the SG entries starts at the offset.
516 * @sk socket of connection to user space
517 * @bytes Count the number of SG entries holding given number of bytes.
518 * @offset Start the counting of SG entries from the given offset.
519 * @return Number of TX SG entries found given the constraints
521 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
523 const struct alg_sock *ask = alg_sk(sk);
524 const struct af_alg_ctx *ctx = ask->private;
525 const struct af_alg_tsgl *sgl;
527 unsigned int sgl_count = 0;
532 list_for_each_entry(sgl, &ctx->tsgl_list, list) {
533 const struct scatterlist *sg = sgl->sg;
535 for (i = 0; i < sgl->cur; i++) {
539 if (offset >= sg[i].length) {
540 offset -= sg[i].length;
541 bytes -= sg[i].length;
545 bytes_count = sg[i].length - offset;
550 /* If we have seen requested number of bytes, stop */
551 if (bytes_count >= bytes)
554 bytes -= bytes_count;
560 EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
563 * aead_pull_tsgl - Release the specified buffers from TX SGL
565 * If @dst is non-null, reassign the pages to dst. The caller must release
566 * the pages. If @dst_offset is given only reassign the pages to @dst starting
567 * at the @dst_offset (byte). The caller must ensure that @dst is large
568 * enough (e.g. by using af_alg_count_tsgl with the same offset).
570 * @sk socket of connection to user space
571 * @used Number of bytes to pull from TX SGL
572 * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
573 * caller must release the buffers in dst.
574 * @dst_offset Reassign the TX SGL from given offset. All buffers before
575 * reaching the offset is released.
577 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
580 struct alg_sock *ask = alg_sk(sk);
581 struct af_alg_ctx *ctx = ask->private;
582 struct af_alg_tsgl *sgl;
583 struct scatterlist *sg;
584 unsigned int i, j = 0;
586 while (!list_empty(&ctx->tsgl_list)) {
587 sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
591 for (i = 0; i < sgl->cur; i++) {
592 size_t plen = min_t(size_t, used, sg[i].length);
593 struct page *page = sg_page(sg + i);
599 * Assumption: caller created af_alg_count_tsgl(len)
603 if (dst_offset >= plen) {
604 /* discard page before offset */
607 /* reassign page to dst after offset */
609 sg_set_page(dst + j, page,
611 sg[i].offset + dst_offset);
617 sg[i].length -= plen;
618 sg[i].offset += plen;
627 sg_assign_page(sg + i, NULL);
630 list_del(&sgl->list);
631 sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
636 ctx->init = ctx->more;
638 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
641 * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
643 * @areq Request holding the TX and RX SGL
645 static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
647 struct sock *sk = areq->sk;
648 struct alg_sock *ask = alg_sk(sk);
649 struct af_alg_ctx *ctx = ask->private;
650 struct af_alg_rsgl *rsgl, *tmp;
651 struct scatterlist *tsgl;
652 struct scatterlist *sg;
655 list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
656 atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
657 af_alg_free_sg(&rsgl->sgl);
658 list_del(&rsgl->list);
659 if (rsgl != &areq->first_rsgl)
660 sock_kfree_s(sk, rsgl, sizeof(*rsgl));
665 for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
668 put_page(sg_page(sg));
671 sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
676 * af_alg_wait_for_wmem - wait for availability of writable memory
678 * @sk socket of connection to user space
679 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
680 * @return 0 when writable memory is available, < 0 upon error
682 static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
684 DEFINE_WAIT_FUNC(wait, woken_wake_function);
685 int err = -ERESTARTSYS;
688 if (flags & MSG_DONTWAIT)
691 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
693 add_wait_queue(sk_sleep(sk), &wait);
695 if (signal_pending(current))
697 timeout = MAX_SCHEDULE_TIMEOUT;
698 if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
703 remove_wait_queue(sk_sleep(sk), &wait);
709 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
711 * @sk socket of connection to user space
713 void af_alg_wmem_wakeup(struct sock *sk)
715 struct socket_wq *wq;
717 if (!af_alg_writable(sk))
721 wq = rcu_dereference(sk->sk_wq);
722 if (skwq_has_sleeper(wq))
723 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
726 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
729 EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
732 * af_alg_wait_for_data - wait for availability of TX data
734 * @sk socket of connection to user space
735 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
736 * @min Set to minimum request size if partial requests are allowed.
737 * @return 0 when writable memory is available, < 0 upon error
739 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
741 DEFINE_WAIT_FUNC(wait, woken_wake_function);
742 struct alg_sock *ask = alg_sk(sk);
743 struct af_alg_ctx *ctx = ask->private;
745 int err = -ERESTARTSYS;
747 if (flags & MSG_DONTWAIT)
750 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
752 add_wait_queue(sk_sleep(sk), &wait);
754 if (signal_pending(current))
756 timeout = MAX_SCHEDULE_TIMEOUT;
757 if (sk_wait_event(sk, &timeout,
758 ctx->init && (!ctx->more ||
759 (min && ctx->used >= min)),
765 remove_wait_queue(sk_sleep(sk), &wait);
767 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
771 EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
774 * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
776 * @sk socket of connection to user space
778 static void af_alg_data_wakeup(struct sock *sk)
780 struct alg_sock *ask = alg_sk(sk);
781 struct af_alg_ctx *ctx = ask->private;
782 struct socket_wq *wq;
788 wq = rcu_dereference(sk->sk_wq);
789 if (skwq_has_sleeper(wq))
790 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
793 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
798 * af_alg_sendmsg - implementation of sendmsg system call handler
800 * The sendmsg system call handler obtains the user data and stores it
801 * in ctx->tsgl_list. This implies allocation of the required numbers of
802 * struct af_alg_tsgl.
804 * In addition, the ctx is filled with the information sent via CMSG.
806 * @sock socket of connection to user space
807 * @msg message from user space
808 * @size size of message from user space
809 * @ivsize the size of the IV for the cipher operation to verify that the
810 * user-space-provided IV has the right size
811 * @return the number of copied data upon success, < 0 upon error
813 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
816 struct sock *sk = sock->sk;
817 struct alg_sock *ask = alg_sk(sk);
818 struct af_alg_ctx *ctx = ask->private;
819 struct af_alg_tsgl *sgl;
820 struct af_alg_control con = {};
826 if (msg->msg_controllen) {
827 err = af_alg_cmsg_send(msg, &con);
843 if (con.iv && con.iv->ivlen != ivsize)
848 if (ctx->init && (init || !ctx->more)) {
857 memcpy(ctx->iv, con.iv->iv, ivsize);
859 ctx->aead_assoclen = con.aead_assoclen;
863 struct scatterlist *sg;
867 /* use the existing memory in an allocated page */
869 sgl = list_entry(ctx->tsgl_list.prev,
870 struct af_alg_tsgl, list);
871 sg = sgl->sg + sgl->cur - 1;
872 len = min_t(size_t, len,
873 PAGE_SIZE - sg->offset - sg->length);
875 err = memcpy_from_msg(page_address(sg_page(sg)) +
876 sg->offset + sg->length,
882 ctx->merge = (sg->offset + sg->length) &
891 if (!af_alg_writable(sk)) {
892 err = af_alg_wait_for_wmem(sk, msg->msg_flags);
897 /* allocate a new page */
898 len = min_t(unsigned long, len, af_alg_sndbuf(sk));
900 err = af_alg_alloc_tsgl(sk);
904 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
908 sg_unmark_end(sg + sgl->cur - 1);
911 unsigned int i = sgl->cur;
913 plen = min_t(size_t, len, PAGE_SIZE);
915 sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
916 if (!sg_page(sg + i)) {
921 err = memcpy_from_msg(page_address(sg_page(sg + i)),
924 __free_page(sg_page(sg + i));
925 sg_assign_page(sg + i, NULL);
935 } while (len && sgl->cur < MAX_SGL_ENTS);
938 sg_mark_end(sg + sgl->cur - 1);
940 ctx->merge = plen & (PAGE_SIZE - 1);
945 ctx->more = msg->msg_flags & MSG_MORE;
948 af_alg_data_wakeup(sk);
951 return copied ?: err;
953 EXPORT_SYMBOL_GPL(af_alg_sendmsg);
956 * af_alg_sendpage - sendpage system call handler
958 * This is a generic implementation of sendpage to fill ctx->tsgl_list.
960 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
961 int offset, size_t size, int flags)
963 struct sock *sk = sock->sk;
964 struct alg_sock *ask = alg_sk(sk);
965 struct af_alg_ctx *ctx = ask->private;
966 struct af_alg_tsgl *sgl;
969 if (flags & MSG_SENDPAGE_NOTLAST)
973 if (!ctx->more && ctx->used)
979 if (!af_alg_writable(sk)) {
980 err = af_alg_wait_for_wmem(sk, flags);
985 err = af_alg_alloc_tsgl(sk);
990 sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
993 sg_unmark_end(sgl->sg + sgl->cur - 1);
995 sg_mark_end(sgl->sg + sgl->cur);
998 sg_set_page(sgl->sg + sgl->cur, page, size, offset);
1003 ctx->more = flags & MSG_MORE;
1006 af_alg_data_wakeup(sk);
1011 EXPORT_SYMBOL_GPL(af_alg_sendpage);
1014 * af_alg_free_resources - release resources required for crypto request
1016 void af_alg_free_resources(struct af_alg_async_req *areq)
1018 struct sock *sk = areq->sk;
1020 af_alg_free_areq_sgls(areq);
1021 sock_kfree_s(sk, areq, areq->areqlen);
1023 EXPORT_SYMBOL_GPL(af_alg_free_resources);
1026 * af_alg_async_cb - AIO callback handler
1028 * This handler cleans up the struct af_alg_async_req upon completion of the
1031 * The number of bytes to be generated with the AIO operation must be set
1032 * in areq->outlen before the AIO callback handler is invoked.
1034 void af_alg_async_cb(struct crypto_async_request *_req, int err)
1036 struct af_alg_async_req *areq = _req->data;
1037 struct sock *sk = areq->sk;
1038 struct kiocb *iocb = areq->iocb;
1039 unsigned int resultlen;
1041 /* Buffer size written by crypto operation. */
1042 resultlen = areq->outlen;
1044 af_alg_free_resources(areq);
1047 iocb->ki_complete(iocb, err ? err : (int)resultlen, 0);
1049 EXPORT_SYMBOL_GPL(af_alg_async_cb);
1052 * af_alg_poll - poll system call handler
1054 __poll_t af_alg_poll(struct file *file, struct socket *sock,
1057 struct sock *sk = sock->sk;
1058 struct alg_sock *ask = alg_sk(sk);
1059 struct af_alg_ctx *ctx = ask->private;
1062 sock_poll_wait(file, sock, wait);
1065 if (!ctx->more || ctx->used)
1066 mask |= EPOLLIN | EPOLLRDNORM;
1068 if (af_alg_writable(sk))
1069 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1073 EXPORT_SYMBOL_GPL(af_alg_poll);
1076 * af_alg_alloc_areq - allocate struct af_alg_async_req
1078 * @sk socket of connection to user space
1079 * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
1080 * @return allocated data structure or ERR_PTR upon error
1082 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
1083 unsigned int areqlen)
1085 struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
1087 if (unlikely(!areq))
1088 return ERR_PTR(-ENOMEM);
1090 areq->areqlen = areqlen;
1092 areq->last_rsgl = NULL;
1093 INIT_LIST_HEAD(&areq->rsgl_list);
1095 areq->tsgl_entries = 0;
1099 EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
1102 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
1105 * @sk socket of connection to user space
1106 * @msg user space message
1107 * @flags flags used to invoke recvmsg with
1108 * @areq instance of the cryptographic request that will hold the RX SGL
1109 * @maxsize maximum number of bytes to be pulled from user space
1110 * @outlen number of bytes in the RX SGL
1111 * @return 0 on success, < 0 upon error
1113 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
1114 struct af_alg_async_req *areq, size_t maxsize,
1117 struct alg_sock *ask = alg_sk(sk);
1118 struct af_alg_ctx *ctx = ask->private;
1121 while (maxsize > len && msg_data_left(msg)) {
1122 struct af_alg_rsgl *rsgl;
1126 /* limit the amount of readable buffers */
1127 if (!af_alg_readable(sk))
1130 seglen = min_t(size_t, (maxsize - len),
1131 msg_data_left(msg));
1133 if (list_empty(&areq->rsgl_list)) {
1134 rsgl = &areq->first_rsgl;
1136 rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
1137 if (unlikely(!rsgl))
1141 rsgl->sgl.npages = 0;
1142 list_add_tail(&rsgl->list, &areq->rsgl_list);
1144 /* make one iovec available as scatterlist */
1145 err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
1147 rsgl->sg_num_bytes = 0;
1151 /* chain the new scatterlist with previous one */
1152 if (areq->last_rsgl)
1153 af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
1155 areq->last_rsgl = rsgl;
1157 atomic_add(err, &ctx->rcvused);
1158 rsgl->sg_num_bytes = err;
1159 iov_iter_advance(&msg->msg_iter, err);
1165 EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
1167 static int __init af_alg_init(void)
1169 int err = proto_register(&alg_proto, 0);
1174 err = sock_register(&alg_family);
1176 goto out_unregister_proto;
1181 out_unregister_proto:
1182 proto_unregister(&alg_proto);
1186 static void __exit af_alg_exit(void)
1188 sock_unregister(PF_ALG);
1189 proto_unregister(&alg_proto);
1192 module_init(af_alg_init);
1193 module_exit(af_alg_exit);
1194 MODULE_LICENSE("GPL");
1195 MODULE_ALIAS_NETPROTO(AF_ALG);