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
*sa
= (void *)uaddr
;
151 const struct af_alg_type
*type
;
155 if (sock
->state
== SS_CONNECTED
)
158 if (addr_len
< sizeof(*sa
))
161 /* If caller uses non-allowed flag, return error. */
162 if ((sa
->salg_feat
& ~allowed
) || (sa
->salg_mask
& ~allowed
))
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
);
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);
185 if (atomic_read(&ask
->refcnt
))
188 swap(ask
->type
, type
);
189 swap(ask
->private, private);
196 alg_do_release(type
, private);
201 static int alg_setkey(struct sock
*sk
, char __user
*ukey
,
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_user(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 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
;
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
);
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
;
281 sk2
= sk_alloc(sock_net(sk
), PF_ALG
, GFP_KERNEL
, &alg_proto
, kern
);
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
);
299 if (atomic_inc_return_relaxed(&ask
->refcnt
) == 1)
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
;
312 newsock
->ops
= type
->ops_nokey
;
321 EXPORT_SYMBOL_GPL(af_alg_accept
);
323 static int alg_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
326 return af_alg_accept(sock
->sk
, newsock
, kern
);
329 static const struct proto_ops alg_proto_ops
= {
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
,
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
,
364 if (sock
->type
!= SOCK_SEQPACKET
)
365 return -ESOCKTNOSUPPORT
;
367 return -EPROTONOSUPPORT
;
370 sk
= sk_alloc(net
, PF_ALG
, GFP_KERNEL
, &alg_proto
, kern
);
374 sock
->ops
= &alg_proto_ops
;
375 sock_init_data(sock
, sk
);
377 sk
->sk_destruct
= alg_sock_destruct
;
384 static const struct net_proto_family alg_family
= {
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
)
396 n
= iov_iter_get_pages(iter
, sgl
->pages
, len
, ALG_MAX_PAGES
, &off
);
400 npages
= (off
+ n
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
401 if (WARN_ON(npages
== 0))
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
);
414 sg_mark_end(sgl
->sg
+ npages
- 1);
415 sgl
->npages
= npages
;
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
)
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
))
444 if (cmsg
->cmsg_level
!= SOL_ALG
)
447 switch (cmsg
->cmsg_type
) {
449 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*con
->iv
)))
451 con
->iv
= (void *)CMSG_DATA(cmsg
);
452 if (cmsg
->cmsg_len
< CMSG_LEN(con
->iv
->ivlen
+
458 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(u32
)))
460 con
->op
= *(u32
*)CMSG_DATA(cmsg
);
463 case ALG_SET_AEAD_ASSOCLEN
:
464 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(u32
)))
466 con
->aead_assoclen
= *(u32
*)CMSG_DATA(cmsg
);
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
))
494 if (!sg
|| sgl
->cur
>= MAX_SGL_ENTS
) {
495 sgl
= sock_kmalloc(sk
,
496 struct_size(sgl
, sg
, (MAX_SGL_ENTS
+ 1)),
501 sg_init_table(sgl
->sg
, MAX_SGL_ENTS
+ 1);
505 sg_chain(sg
, MAX_SGL_ENTS
+ 1, sgl
->sg
);
507 list_add_tail(&sgl
->list
, &ctx
->tsgl_list
);
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
;
530 unsigned int sgl_count
= 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
++) {
542 if (offset
>= sg
[i
].length
) {
543 offset
-= sg
[i
].length
;
544 bytes
-= sg
[i
].length
;
548 bytes_count
= sg
[i
].length
- offset
;
553 /* If we have seen requested number of bytes, stop */
554 if (bytes_count
>= bytes
)
557 bytes
-= bytes_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
,
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
,
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
);
602 * Assumption: caller created af_alg_count_tsgl(len)
606 if (dst_offset
>= plen
) {
607 /* discard page before offset */
610 /* reassign page to dst after offset */
612 sg_set_page(dst
+ j
, page
,
614 sg
[i
].offset
+ dst_offset
);
620 sg
[i
].length
-= plen
;
621 sg
[i
].offset
+= plen
;
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));
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
;
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
));
668 for_each_sg(tsgl
, sg
, areq
->tsgl_entries
, i
) {
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
;
691 if (flags
& MSG_DONTWAIT
)
694 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
696 add_wait_queue(sk_sleep(sk
), &wait
);
698 if (signal_pending(current
))
700 timeout
= MAX_SCHEDULE_TIMEOUT
;
701 if (sk_wait_event(sk
, &timeout
, af_alg_writable(sk
), &wait
)) {
706 remove_wait_queue(sk_sleep(sk
), &wait
);
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
))
724 wq
= rcu_dereference(sk
->sk_wq
);
725 if (skwq_has_sleeper(wq
))
726 wake_up_interruptible_sync_poll(&wq
->wait
, EPOLLIN
|
729 sk_wake_async(sk
, SOCK_WAKE_WAITD
, POLL_IN
);
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;
748 int err
= -ERESTARTSYS
;
750 if (flags
& MSG_DONTWAIT
)
753 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
755 add_wait_queue(sk_sleep(sk
), &wait
);
757 if (signal_pending(current
))
759 timeout
= MAX_SCHEDULE_TIMEOUT
;
760 if (sk_wait_event(sk
, &timeout
,
761 ctx
->init
&& (!ctx
->more
||
762 (min
&& ctx
->used
>= min
)),
768 remove_wait_queue(sk_sleep(sk
), &wait
);
770 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
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
;
791 wq
= rcu_dereference(sk
->sk_wq
);
792 if (skwq_has_sleeper(wq
))
793 wake_up_interruptible_sync_poll(&wq
->wait
, EPOLLOUT
|
796 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
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
,
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
= {};
829 if (msg
->msg_controllen
) {
830 err
= af_alg_cmsg_send(msg
, &con
);
846 if (con
.iv
&& con
.iv
->ivlen
!= ivsize
)
851 if (ctx
->init
&& !ctx
->more
) {
858 "%s sent an empty control message without MSG_MORE.\n",
866 memcpy(ctx
->iv
, con
.iv
->iv
, ivsize
);
868 ctx
->aead_assoclen
= con
.aead_assoclen
;
872 struct scatterlist
*sg
;
876 /* use the existing memory in an allocated page */
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
,
891 ctx
->merge
= (sg
->offset
+ sg
->length
) &
900 if (!af_alg_writable(sk
)) {
901 err
= af_alg_wait_for_wmem(sk
, msg
->msg_flags
);
906 /* allocate a new page */
907 len
= min_t(unsigned long, len
, af_alg_sndbuf(sk
));
909 err
= af_alg_alloc_tsgl(sk
);
913 sgl
= list_entry(ctx
->tsgl_list
.prev
, struct af_alg_tsgl
,
917 sg_unmark_end(sg
+ sgl
->cur
- 1);
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
)) {
930 err
= memcpy_from_msg(page_address(sg_page(sg
+ i
)),
933 __free_page(sg_page(sg
+ i
));
934 sg_assign_page(sg
+ i
, NULL
);
944 } while (len
&& sgl
->cur
< MAX_SGL_ENTS
);
947 sg_mark_end(sg
+ sgl
->cur
- 1);
949 ctx
->merge
= plen
& (PAGE_SIZE
- 1);
954 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
957 af_alg_data_wakeup(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
;
978 if (flags
& MSG_SENDPAGE_NOTLAST
)
982 if (!ctx
->more
&& ctx
->used
)
988 if (!af_alg_writable(sk
)) {
989 err
= af_alg_wait_for_wmem(sk
, flags
);
994 err
= af_alg_alloc_tsgl(sk
);
999 sgl
= list_entry(ctx
->tsgl_list
.prev
, struct af_alg_tsgl
, list
);
1002 sg_unmark_end(sgl
->sg
+ sgl
->cur
- 1);
1004 sg_mark_end(sgl
->sg
+ sgl
->cur
);
1007 sg_set_page(sgl
->sg
+ sgl
->cur
, page
, size
, offset
);
1012 ctx
->more
= flags
& MSG_MORE
;
1015 af_alg_data_wakeup(sk
);
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
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
);
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
,
1066 struct sock
*sk
= sock
->sk
;
1067 struct alg_sock
*ask
= alg_sk(sk
);
1068 struct af_alg_ctx
*ctx
= ask
->private;
1071 sock_poll_wait(file
, sock
, wait
);
1074 if (!ctx
->more
|| ctx
->used
)
1075 mask
|= EPOLLIN
| EPOLLRDNORM
;
1077 if (af_alg_writable(sk
))
1078 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
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
;
1101 areq
->last_rsgl
= NULL
;
1102 INIT_LIST_HEAD(&areq
->rsgl_list
);
1104 areq
->tsgl_entries
= 0;
1108 EXPORT_SYMBOL_GPL(af_alg_alloc_areq
);
1111 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
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
,
1126 struct alg_sock
*ask
= alg_sk(sk
);
1127 struct af_alg_ctx
*ctx
= ask
->private;
1130 while (maxsize
> len
&& msg_data_left(msg
)) {
1131 struct af_alg_rsgl
*rsgl
;
1135 /* limit the amount of readable buffers */
1136 if (!af_alg_readable(sk
))
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
;
1145 rsgl
= sock_kmalloc(sk
, sizeof(*rsgl
), GFP_KERNEL
);
1146 if (unlikely(!rsgl
))
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
);
1156 rsgl
->sg_num_bytes
= 0;
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
;
1166 atomic_add(err
, &ctx
->rcvused
);
1167 rsgl
->sg_num_bytes
= err
;
1168 iov_iter_advance(&msg
->msg_iter
, err
);
1174 EXPORT_SYMBOL_GPL(af_alg_get_rsgl
);
1176 static int __init
af_alg_init(void)
1178 int err
= proto_register(&alg_proto
, 0);
1183 err
= sock_register(&alg_family
);
1185 goto out_unregister_proto
;
1190 out_unregister_proto
:
1191 proto_unregister(&alg_proto
);
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
);