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/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
= ask
->nokey_refcnt
;
132 bool last
= nokey
&& !ask
->refcnt
;
139 ask
->nokey_refcnt
-= nokey
;
141 last
= !--ask
->refcnt
;
148 EXPORT_SYMBOL_GPL(af_alg_release_parent
);
150 static int alg_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
152 const u32 allowed
= CRYPTO_ALG_KERN_DRIVER_ONLY
;
153 struct sock
*sk
= sock
->sk
;
154 struct alg_sock
*ask
= alg_sk(sk
);
155 struct sockaddr_alg
*sa
= (void *)uaddr
;
156 const struct af_alg_type
*type
;
160 if (sock
->state
== SS_CONNECTED
)
163 if (addr_len
< sizeof(*sa
))
166 /* If caller uses non-allowed flag, return error. */
167 if ((sa
->salg_feat
& ~allowed
) || (sa
->salg_mask
& ~allowed
))
170 sa
->salg_type
[sizeof(sa
->salg_type
) - 1] = 0;
171 sa
->salg_name
[sizeof(sa
->salg_name
) + addr_len
- sizeof(*sa
) - 1] = 0;
173 type
= alg_get_type(sa
->salg_type
);
174 if (PTR_ERR(type
) == -ENOENT
) {
175 request_module("algif-%s", sa
->salg_type
);
176 type
= alg_get_type(sa
->salg_type
);
180 return PTR_ERR(type
);
182 private = type
->bind(sa
->salg_name
, sa
->salg_feat
, sa
->salg_mask
);
183 if (IS_ERR(private)) {
184 module_put(type
->owner
);
185 return PTR_ERR(private);
190 if (ask
->refcnt
| ask
->nokey_refcnt
)
193 swap(ask
->type
, type
);
194 swap(ask
->private, private);
201 alg_do_release(type
, private);
206 static int alg_setkey(struct sock
*sk
, char __user
*ukey
,
209 struct alg_sock
*ask
= alg_sk(sk
);
210 const struct af_alg_type
*type
= ask
->type
;
214 key
= sock_kmalloc(sk
, keylen
, GFP_KERNEL
);
219 if (copy_from_user(key
, ukey
, keylen
))
222 err
= type
->setkey(ask
->private, key
, keylen
);
225 sock_kzfree_s(sk
, key
, keylen
);
230 static int alg_setsockopt(struct socket
*sock
, int level
, int optname
,
231 char __user
*optval
, unsigned int optlen
)
233 struct sock
*sk
= sock
->sk
;
234 struct alg_sock
*ask
= alg_sk(sk
);
235 const struct af_alg_type
*type
;
245 if (level
!= SOL_ALG
|| !type
)
250 if (sock
->state
== SS_CONNECTED
)
255 err
= alg_setkey(sk
, optval
, optlen
);
257 case ALG_SET_AEAD_AUTHSIZE
:
258 if (sock
->state
== SS_CONNECTED
)
260 if (!type
->setauthsize
)
262 err
= type
->setauthsize(ask
->private, optlen
);
271 int af_alg_accept(struct sock
*sk
, struct socket
*newsock
, bool kern
)
273 struct alg_sock
*ask
= alg_sk(sk
);
274 const struct af_alg_type
*type
;
286 sk2
= sk_alloc(sock_net(sk
), PF_ALG
, GFP_KERNEL
, &alg_proto
, kern
);
291 sock_init_data(newsock
, sk2
);
292 security_sock_graft(sk2
, newsock
);
293 security_sk_clone(sk
, sk2
);
295 err
= type
->accept(ask
->private, sk2
);
297 nokey
= err
== -ENOKEY
;
298 if (nokey
&& type
->accept_nokey
)
299 err
= type
->accept_nokey(ask
->private, sk2
);
304 if (nokey
|| !ask
->refcnt
++)
306 ask
->nokey_refcnt
+= nokey
;
307 alg_sk(sk2
)->parent
= sk
;
308 alg_sk(sk2
)->type
= type
;
309 alg_sk(sk2
)->nokey_refcnt
= nokey
;
311 newsock
->ops
= type
->ops
;
312 newsock
->state
= SS_CONNECTED
;
315 newsock
->ops
= type
->ops_nokey
;
324 EXPORT_SYMBOL_GPL(af_alg_accept
);
326 static int alg_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
329 return af_alg_accept(sock
->sk
, newsock
, kern
);
332 static const struct proto_ops alg_proto_ops
= {
334 .owner
= THIS_MODULE
,
336 .connect
= sock_no_connect
,
337 .socketpair
= sock_no_socketpair
,
338 .getname
= sock_no_getname
,
339 .ioctl
= sock_no_ioctl
,
340 .listen
= sock_no_listen
,
341 .shutdown
= sock_no_shutdown
,
342 .getsockopt
= sock_no_getsockopt
,
343 .mmap
= sock_no_mmap
,
344 .sendpage
= sock_no_sendpage
,
345 .sendmsg
= sock_no_sendmsg
,
346 .recvmsg
= sock_no_recvmsg
,
349 .release
= af_alg_release
,
350 .setsockopt
= alg_setsockopt
,
351 .accept
= alg_accept
,
354 static void alg_sock_destruct(struct sock
*sk
)
356 struct alg_sock
*ask
= alg_sk(sk
);
358 alg_do_release(ask
->type
, ask
->private);
361 static int alg_create(struct net
*net
, struct socket
*sock
, int protocol
,
367 if (sock
->type
!= SOCK_SEQPACKET
)
368 return -ESOCKTNOSUPPORT
;
370 return -EPROTONOSUPPORT
;
373 sk
= sk_alloc(net
, PF_ALG
, GFP_KERNEL
, &alg_proto
, kern
);
377 sock
->ops
= &alg_proto_ops
;
378 sock_init_data(sock
, sk
);
380 sk
->sk_destruct
= alg_sock_destruct
;
387 static const struct net_proto_family alg_family
= {
389 .create
= alg_create
,
390 .owner
= THIS_MODULE
,
393 int af_alg_make_sg(struct af_alg_sgl
*sgl
, struct iov_iter
*iter
, int len
)
399 n
= iov_iter_get_pages(iter
, sgl
->pages
, len
, ALG_MAX_PAGES
, &off
);
403 npages
= (off
+ n
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
404 if (WARN_ON(npages
== 0))
406 /* Add one extra for linking */
407 sg_init_table(sgl
->sg
, npages
+ 1);
409 for (i
= 0, len
= n
; i
< npages
; i
++) {
410 int plen
= min_t(int, len
, PAGE_SIZE
- off
);
412 sg_set_page(sgl
->sg
+ i
, sgl
->pages
[i
], plen
, off
);
417 sg_mark_end(sgl
->sg
+ npages
- 1);
418 sgl
->npages
= npages
;
422 EXPORT_SYMBOL_GPL(af_alg_make_sg
);
424 static void af_alg_link_sg(struct af_alg_sgl
*sgl_prev
,
425 struct af_alg_sgl
*sgl_new
)
427 sg_unmark_end(sgl_prev
->sg
+ sgl_prev
->npages
- 1);
428 sg_chain(sgl_prev
->sg
, sgl_prev
->npages
+ 1, sgl_new
->sg
);
431 void af_alg_free_sg(struct af_alg_sgl
*sgl
)
435 for (i
= 0; i
< sgl
->npages
; i
++)
436 put_page(sgl
->pages
[i
]);
438 EXPORT_SYMBOL_GPL(af_alg_free_sg
);
440 static int af_alg_cmsg_send(struct msghdr
*msg
, struct af_alg_control
*con
)
442 struct cmsghdr
*cmsg
;
444 for_each_cmsghdr(cmsg
, msg
) {
445 if (!CMSG_OK(msg
, cmsg
))
447 if (cmsg
->cmsg_level
!= SOL_ALG
)
450 switch (cmsg
->cmsg_type
) {
452 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*con
->iv
)))
454 con
->iv
= (void *)CMSG_DATA(cmsg
);
455 if (cmsg
->cmsg_len
< CMSG_LEN(con
->iv
->ivlen
+
461 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(u32
)))
463 con
->op
= *(u32
*)CMSG_DATA(cmsg
);
466 case ALG_SET_AEAD_ASSOCLEN
:
467 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(u32
)))
469 con
->aead_assoclen
= *(u32
*)CMSG_DATA(cmsg
);
481 * af_alg_alloc_tsgl - allocate the TX SGL
483 * @sk socket of connection to user space
484 * @return: 0 upon success, < 0 upon error
486 static int af_alg_alloc_tsgl(struct sock
*sk
)
488 struct alg_sock
*ask
= alg_sk(sk
);
489 struct af_alg_ctx
*ctx
= ask
->private;
490 struct af_alg_tsgl
*sgl
;
491 struct scatterlist
*sg
= NULL
;
493 sgl
= list_entry(ctx
->tsgl_list
.prev
, struct af_alg_tsgl
, list
);
494 if (!list_empty(&ctx
->tsgl_list
))
497 if (!sg
|| sgl
->cur
>= MAX_SGL_ENTS
) {
498 sgl
= sock_kmalloc(sk
,
499 struct_size(sgl
, sg
, (MAX_SGL_ENTS
+ 1)),
504 sg_init_table(sgl
->sg
, MAX_SGL_ENTS
+ 1);
508 sg_chain(sg
, MAX_SGL_ENTS
+ 1, sgl
->sg
);
510 list_add_tail(&sgl
->list
, &ctx
->tsgl_list
);
517 * aead_count_tsgl - Count number of TX SG entries
519 * The counting starts from the beginning of the SGL to @bytes. If
520 * an offset is provided, the counting of the SG entries starts at the offset.
522 * @sk socket of connection to user space
523 * @bytes Count the number of SG entries holding given number of bytes.
524 * @offset Start the counting of SG entries from the given offset.
525 * @return Number of TX SG entries found given the constraints
527 unsigned int af_alg_count_tsgl(struct sock
*sk
, size_t bytes
, size_t offset
)
529 const struct alg_sock
*ask
= alg_sk(sk
);
530 const struct af_alg_ctx
*ctx
= ask
->private;
531 const struct af_alg_tsgl
*sgl
;
533 unsigned int sgl_count
= 0;
538 list_for_each_entry(sgl
, &ctx
->tsgl_list
, list
) {
539 const struct scatterlist
*sg
= sgl
->sg
;
541 for (i
= 0; i
< sgl
->cur
; i
++) {
545 if (offset
>= sg
[i
].length
) {
546 offset
-= sg
[i
].length
;
547 bytes
-= sg
[i
].length
;
551 bytes_count
= sg
[i
].length
- offset
;
556 /* If we have seen requested number of bytes, stop */
557 if (bytes_count
>= bytes
)
560 bytes
-= bytes_count
;
566 EXPORT_SYMBOL_GPL(af_alg_count_tsgl
);
569 * aead_pull_tsgl - Release the specified buffers from TX SGL
571 * If @dst is non-null, reassign the pages to dst. The caller must release
572 * the pages. If @dst_offset is given only reassign the pages to @dst starting
573 * at the @dst_offset (byte). The caller must ensure that @dst is large
574 * enough (e.g. by using af_alg_count_tsgl with the same offset).
576 * @sk socket of connection to user space
577 * @used Number of bytes to pull from TX SGL
578 * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
579 * caller must release the buffers in dst.
580 * @dst_offset Reassign the TX SGL from given offset. All buffers before
581 * reaching the offset is released.
583 void af_alg_pull_tsgl(struct sock
*sk
, size_t used
, struct scatterlist
*dst
,
586 struct alg_sock
*ask
= alg_sk(sk
);
587 struct af_alg_ctx
*ctx
= ask
->private;
588 struct af_alg_tsgl
*sgl
;
589 struct scatterlist
*sg
;
590 unsigned int i
, j
= 0;
592 while (!list_empty(&ctx
->tsgl_list
)) {
593 sgl
= list_first_entry(&ctx
->tsgl_list
, struct af_alg_tsgl
,
597 for (i
= 0; i
< sgl
->cur
; i
++) {
598 size_t plen
= min_t(size_t, used
, sg
[i
].length
);
599 struct page
*page
= sg_page(sg
+ i
);
605 * Assumption: caller created af_alg_count_tsgl(len)
609 if (dst_offset
>= plen
) {
610 /* discard page before offset */
613 /* reassign page to dst after offset */
615 sg_set_page(dst
+ j
, page
,
617 sg
[i
].offset
+ dst_offset
);
623 sg
[i
].length
-= plen
;
624 sg
[i
].offset
+= plen
;
633 sg_assign_page(sg
+ i
, NULL
);
636 list_del(&sgl
->list
);
637 sock_kfree_s(sk
, sgl
, struct_size(sgl
, sg
, MAX_SGL_ENTS
+ 1));
643 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl
);
646 * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
648 * @areq Request holding the TX and RX SGL
650 static void af_alg_free_areq_sgls(struct af_alg_async_req
*areq
)
652 struct sock
*sk
= areq
->sk
;
653 struct alg_sock
*ask
= alg_sk(sk
);
654 struct af_alg_ctx
*ctx
= ask
->private;
655 struct af_alg_rsgl
*rsgl
, *tmp
;
656 struct scatterlist
*tsgl
;
657 struct scatterlist
*sg
;
660 list_for_each_entry_safe(rsgl
, tmp
, &areq
->rsgl_list
, list
) {
661 atomic_sub(rsgl
->sg_num_bytes
, &ctx
->rcvused
);
662 af_alg_free_sg(&rsgl
->sgl
);
663 list_del(&rsgl
->list
);
664 if (rsgl
!= &areq
->first_rsgl
)
665 sock_kfree_s(sk
, rsgl
, sizeof(*rsgl
));
670 for_each_sg(tsgl
, sg
, areq
->tsgl_entries
, i
) {
673 put_page(sg_page(sg
));
676 sock_kfree_s(sk
, tsgl
, areq
->tsgl_entries
* sizeof(*tsgl
));
681 * af_alg_wait_for_wmem - wait for availability of writable memory
683 * @sk socket of connection to user space
684 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
685 * @return 0 when writable memory is available, < 0 upon error
687 static int af_alg_wait_for_wmem(struct sock
*sk
, unsigned int flags
)
689 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
690 int err
= -ERESTARTSYS
;
693 if (flags
& MSG_DONTWAIT
)
696 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
698 add_wait_queue(sk_sleep(sk
), &wait
);
700 if (signal_pending(current
))
702 timeout
= MAX_SCHEDULE_TIMEOUT
;
703 if (sk_wait_event(sk
, &timeout
, af_alg_writable(sk
), &wait
)) {
708 remove_wait_queue(sk_sleep(sk
), &wait
);
714 * af_alg_wmem_wakeup - wakeup caller when writable memory is available
716 * @sk socket of connection to user space
718 void af_alg_wmem_wakeup(struct sock
*sk
)
720 struct socket_wq
*wq
;
722 if (!af_alg_writable(sk
))
726 wq
= rcu_dereference(sk
->sk_wq
);
727 if (skwq_has_sleeper(wq
))
728 wake_up_interruptible_sync_poll(&wq
->wait
, EPOLLIN
|
731 sk_wake_async(sk
, SOCK_WAKE_WAITD
, POLL_IN
);
734 EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup
);
737 * af_alg_wait_for_data - wait for availability of TX data
739 * @sk socket of connection to user space
740 * @flags If MSG_DONTWAIT is set, then only report if function would sleep
741 * @return 0 when writable memory is available, < 0 upon error
743 int af_alg_wait_for_data(struct sock
*sk
, unsigned flags
)
745 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
746 struct alg_sock
*ask
= alg_sk(sk
);
747 struct af_alg_ctx
*ctx
= ask
->private;
749 int err
= -ERESTARTSYS
;
751 if (flags
& MSG_DONTWAIT
)
754 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
756 add_wait_queue(sk_sleep(sk
), &wait
);
758 if (signal_pending(current
))
760 timeout
= MAX_SCHEDULE_TIMEOUT
;
761 if (sk_wait_event(sk
, &timeout
, (ctx
->used
|| !ctx
->more
),
767 remove_wait_queue(sk_sleep(sk
), &wait
);
769 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
773 EXPORT_SYMBOL_GPL(af_alg_wait_for_data
);
776 * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
778 * @sk socket of connection to user space
780 static void af_alg_data_wakeup(struct sock
*sk
)
782 struct alg_sock
*ask
= alg_sk(sk
);
783 struct af_alg_ctx
*ctx
= ask
->private;
784 struct socket_wq
*wq
;
790 wq
= rcu_dereference(sk
->sk_wq
);
791 if (skwq_has_sleeper(wq
))
792 wake_up_interruptible_sync_poll(&wq
->wait
, EPOLLOUT
|
795 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
800 * af_alg_sendmsg - implementation of sendmsg system call handler
802 * The sendmsg system call handler obtains the user data and stores it
803 * in ctx->tsgl_list. This implies allocation of the required numbers of
804 * struct af_alg_tsgl.
806 * In addition, the ctx is filled with the information sent via CMSG.
808 * @sock socket of connection to user space
809 * @msg message from user space
810 * @size size of message from user space
811 * @ivsize the size of the IV for the cipher operation to verify that the
812 * user-space-provided IV has the right size
813 * @return the number of copied data upon success, < 0 upon error
815 int af_alg_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
,
818 struct sock
*sk
= sock
->sk
;
819 struct alg_sock
*ask
= alg_sk(sk
);
820 struct af_alg_ctx
*ctx
= ask
->private;
821 struct af_alg_tsgl
*sgl
;
822 struct af_alg_control con
= {};
828 if (msg
->msg_controllen
) {
829 err
= af_alg_cmsg_send(msg
, &con
);
845 if (con
.iv
&& con
.iv
->ivlen
!= ivsize
)
850 if (!ctx
->more
&& ctx
->used
) {
858 memcpy(ctx
->iv
, con
.iv
->iv
, ivsize
);
860 ctx
->aead_assoclen
= con
.aead_assoclen
;
864 struct scatterlist
*sg
;
868 /* use the existing memory in an allocated page */
870 sgl
= list_entry(ctx
->tsgl_list
.prev
,
871 struct af_alg_tsgl
, list
);
872 sg
= sgl
->sg
+ sgl
->cur
- 1;
873 len
= min_t(size_t, len
,
874 PAGE_SIZE
- sg
->offset
- sg
->length
);
876 err
= memcpy_from_msg(page_address(sg_page(sg
)) +
877 sg
->offset
+ sg
->length
,
883 ctx
->merge
= (sg
->offset
+ sg
->length
) &
892 if (!af_alg_writable(sk
)) {
893 err
= af_alg_wait_for_wmem(sk
, msg
->msg_flags
);
898 /* allocate a new page */
899 len
= min_t(unsigned long, len
, af_alg_sndbuf(sk
));
901 err
= af_alg_alloc_tsgl(sk
);
905 sgl
= list_entry(ctx
->tsgl_list
.prev
, struct af_alg_tsgl
,
909 sg_unmark_end(sg
+ sgl
->cur
- 1);
912 unsigned int i
= sgl
->cur
;
914 plen
= min_t(size_t, len
, PAGE_SIZE
);
916 sg_assign_page(sg
+ i
, alloc_page(GFP_KERNEL
));
917 if (!sg_page(sg
+ i
)) {
922 err
= memcpy_from_msg(page_address(sg_page(sg
+ i
)),
925 __free_page(sg_page(sg
+ i
));
926 sg_assign_page(sg
+ i
, NULL
);
936 } while (len
&& sgl
->cur
< MAX_SGL_ENTS
);
939 sg_mark_end(sg
+ sgl
->cur
- 1);
941 ctx
->merge
= plen
& (PAGE_SIZE
- 1);
946 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
949 af_alg_data_wakeup(sk
);
952 return copied
?: err
;
954 EXPORT_SYMBOL_GPL(af_alg_sendmsg
);
957 * af_alg_sendpage - sendpage system call handler
959 * This is a generic implementation of sendpage to fill ctx->tsgl_list.
961 ssize_t
af_alg_sendpage(struct socket
*sock
, struct page
*page
,
962 int offset
, size_t size
, int flags
)
964 struct sock
*sk
= sock
->sk
;
965 struct alg_sock
*ask
= alg_sk(sk
);
966 struct af_alg_ctx
*ctx
= ask
->private;
967 struct af_alg_tsgl
*sgl
;
970 if (flags
& MSG_SENDPAGE_NOTLAST
)
974 if (!ctx
->more
&& ctx
->used
)
980 if (!af_alg_writable(sk
)) {
981 err
= af_alg_wait_for_wmem(sk
, flags
);
986 err
= af_alg_alloc_tsgl(sk
);
991 sgl
= list_entry(ctx
->tsgl_list
.prev
, struct af_alg_tsgl
, list
);
994 sg_unmark_end(sgl
->sg
+ sgl
->cur
- 1);
996 sg_mark_end(sgl
->sg
+ sgl
->cur
);
999 sg_set_page(sgl
->sg
+ sgl
->cur
, page
, size
, offset
);
1004 ctx
->more
= flags
& MSG_MORE
;
1007 af_alg_data_wakeup(sk
);
1012 EXPORT_SYMBOL_GPL(af_alg_sendpage
);
1015 * af_alg_free_resources - release resources required for crypto request
1017 void af_alg_free_resources(struct af_alg_async_req
*areq
)
1019 struct sock
*sk
= areq
->sk
;
1021 af_alg_free_areq_sgls(areq
);
1022 sock_kfree_s(sk
, areq
, areq
->areqlen
);
1024 EXPORT_SYMBOL_GPL(af_alg_free_resources
);
1027 * af_alg_async_cb - AIO callback handler
1029 * This handler cleans up the struct af_alg_async_req upon completion of the
1032 * The number of bytes to be generated with the AIO operation must be set
1033 * in areq->outlen before the AIO callback handler is invoked.
1035 void af_alg_async_cb(struct crypto_async_request
*_req
, int err
)
1037 struct af_alg_async_req
*areq
= _req
->data
;
1038 struct sock
*sk
= areq
->sk
;
1039 struct kiocb
*iocb
= areq
->iocb
;
1040 unsigned int resultlen
;
1042 /* Buffer size written by crypto operation. */
1043 resultlen
= areq
->outlen
;
1045 af_alg_free_resources(areq
);
1048 iocb
->ki_complete(iocb
, err
? err
: (int)resultlen
, 0);
1050 EXPORT_SYMBOL_GPL(af_alg_async_cb
);
1053 * af_alg_poll - poll system call handler
1055 __poll_t
af_alg_poll(struct file
*file
, struct socket
*sock
,
1058 struct sock
*sk
= sock
->sk
;
1059 struct alg_sock
*ask
= alg_sk(sk
);
1060 struct af_alg_ctx
*ctx
= ask
->private;
1063 sock_poll_wait(file
, sock
, wait
);
1066 if (!ctx
->more
|| ctx
->used
)
1067 mask
|= EPOLLIN
| EPOLLRDNORM
;
1069 if (af_alg_writable(sk
))
1070 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
1074 EXPORT_SYMBOL_GPL(af_alg_poll
);
1077 * af_alg_alloc_areq - allocate struct af_alg_async_req
1079 * @sk socket of connection to user space
1080 * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
1081 * @return allocated data structure or ERR_PTR upon error
1083 struct af_alg_async_req
*af_alg_alloc_areq(struct sock
*sk
,
1084 unsigned int areqlen
)
1086 struct af_alg_async_req
*areq
= sock_kmalloc(sk
, areqlen
, GFP_KERNEL
);
1088 if (unlikely(!areq
))
1089 return ERR_PTR(-ENOMEM
);
1091 areq
->areqlen
= areqlen
;
1093 areq
->last_rsgl
= NULL
;
1094 INIT_LIST_HEAD(&areq
->rsgl_list
);
1096 areq
->tsgl_entries
= 0;
1100 EXPORT_SYMBOL_GPL(af_alg_alloc_areq
);
1103 * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
1106 * @sk socket of connection to user space
1107 * @msg user space message
1108 * @flags flags used to invoke recvmsg with
1109 * @areq instance of the cryptographic request that will hold the RX SGL
1110 * @maxsize maximum number of bytes to be pulled from user space
1111 * @outlen number of bytes in the RX SGL
1112 * @return 0 on success, < 0 upon error
1114 int af_alg_get_rsgl(struct sock
*sk
, struct msghdr
*msg
, int flags
,
1115 struct af_alg_async_req
*areq
, size_t maxsize
,
1118 struct alg_sock
*ask
= alg_sk(sk
);
1119 struct af_alg_ctx
*ctx
= ask
->private;
1122 while (maxsize
> len
&& msg_data_left(msg
)) {
1123 struct af_alg_rsgl
*rsgl
;
1127 /* limit the amount of readable buffers */
1128 if (!af_alg_readable(sk
))
1131 seglen
= min_t(size_t, (maxsize
- len
),
1132 msg_data_left(msg
));
1134 if (list_empty(&areq
->rsgl_list
)) {
1135 rsgl
= &areq
->first_rsgl
;
1137 rsgl
= sock_kmalloc(sk
, sizeof(*rsgl
), GFP_KERNEL
);
1138 if (unlikely(!rsgl
))
1142 rsgl
->sgl
.npages
= 0;
1143 list_add_tail(&rsgl
->list
, &areq
->rsgl_list
);
1145 /* make one iovec available as scatterlist */
1146 err
= af_alg_make_sg(&rsgl
->sgl
, &msg
->msg_iter
, seglen
);
1148 rsgl
->sg_num_bytes
= 0;
1152 /* chain the new scatterlist with previous one */
1153 if (areq
->last_rsgl
)
1154 af_alg_link_sg(&areq
->last_rsgl
->sgl
, &rsgl
->sgl
);
1156 areq
->last_rsgl
= rsgl
;
1158 atomic_add(err
, &ctx
->rcvused
);
1159 rsgl
->sg_num_bytes
= err
;
1160 iov_iter_advance(&msg
->msg_iter
, err
);
1166 EXPORT_SYMBOL_GPL(af_alg_get_rsgl
);
1168 static int __init
af_alg_init(void)
1170 int err
= proto_register(&alg_proto
, 0);
1175 err
= sock_register(&alg_family
);
1177 goto out_unregister_proto
;
1182 out_unregister_proto
:
1183 proto_unregister(&alg_proto
);
1187 static void __exit
af_alg_exit(void)
1189 sock_unregister(PF_ALG
);
1190 proto_unregister(&alg_proto
);
1193 module_init(af_alg_init
);
1194 module_exit(af_alg_exit
);
1195 MODULE_LICENSE("GPL");
1196 MODULE_ALIAS_NETPROTO(AF_ALG
);