18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * af_alg: User-space algorithm interface 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This file provides the user-space API for algorithms. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/atomic.h> 118c2ecf20Sopenharmony_ci#include <crypto/if_alg.h> 128c2ecf20Sopenharmony_ci#include <linux/crypto.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/list.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/net.h> 188c2ecf20Sopenharmony_ci#include <linux/rwsem.h> 198c2ecf20Sopenharmony_ci#include <linux/sched.h> 208c2ecf20Sopenharmony_ci#include <linux/sched/signal.h> 218c2ecf20Sopenharmony_ci#include <linux/security.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistruct alg_type_list { 248c2ecf20Sopenharmony_ci const struct af_alg_type *type; 258c2ecf20Sopenharmony_ci struct list_head list; 268c2ecf20Sopenharmony_ci}; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic atomic_long_t alg_memory_allocated; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic struct proto alg_proto = { 318c2ecf20Sopenharmony_ci .name = "ALG", 328c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 338c2ecf20Sopenharmony_ci .memory_allocated = &alg_memory_allocated, 348c2ecf20Sopenharmony_ci .obj_size = sizeof(struct alg_sock), 358c2ecf20Sopenharmony_ci}; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic LIST_HEAD(alg_types); 388c2ecf20Sopenharmony_cistatic DECLARE_RWSEM(alg_types_sem); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic const struct af_alg_type *alg_get_type(const char *name) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci const struct af_alg_type *type = ERR_PTR(-ENOENT); 438c2ecf20Sopenharmony_ci struct alg_type_list *node; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci down_read(&alg_types_sem); 468c2ecf20Sopenharmony_ci list_for_each_entry(node, &alg_types, list) { 478c2ecf20Sopenharmony_ci if (strcmp(node->type->name, name)) 488c2ecf20Sopenharmony_ci continue; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (try_module_get(node->type->owner)) 518c2ecf20Sopenharmony_ci type = node->type; 528c2ecf20Sopenharmony_ci break; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci up_read(&alg_types_sem); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci return type; 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciint af_alg_register_type(const struct af_alg_type *type) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci struct alg_type_list *node; 628c2ecf20Sopenharmony_ci int err = -EEXIST; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci down_write(&alg_types_sem); 658c2ecf20Sopenharmony_ci list_for_each_entry(node, &alg_types, list) { 668c2ecf20Sopenharmony_ci if (!strcmp(node->type->name, type->name)) 678c2ecf20Sopenharmony_ci goto unlock; 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci node = kmalloc(sizeof(*node), GFP_KERNEL); 718c2ecf20Sopenharmony_ci err = -ENOMEM; 728c2ecf20Sopenharmony_ci if (!node) 738c2ecf20Sopenharmony_ci goto unlock; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci type->ops->owner = THIS_MODULE; 768c2ecf20Sopenharmony_ci if (type->ops_nokey) 778c2ecf20Sopenharmony_ci type->ops_nokey->owner = THIS_MODULE; 788c2ecf20Sopenharmony_ci node->type = type; 798c2ecf20Sopenharmony_ci list_add(&node->list, &alg_types); 808c2ecf20Sopenharmony_ci err = 0; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ciunlock: 838c2ecf20Sopenharmony_ci up_write(&alg_types_sem); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci return err; 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_register_type); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ciint af_alg_unregister_type(const struct af_alg_type *type) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci struct alg_type_list *node; 928c2ecf20Sopenharmony_ci int err = -ENOENT; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci down_write(&alg_types_sem); 958c2ecf20Sopenharmony_ci list_for_each_entry(node, &alg_types, list) { 968c2ecf20Sopenharmony_ci if (strcmp(node->type->name, type->name)) 978c2ecf20Sopenharmony_ci continue; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci list_del(&node->list); 1008c2ecf20Sopenharmony_ci kfree(node); 1018c2ecf20Sopenharmony_ci err = 0; 1028c2ecf20Sopenharmony_ci break; 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci up_write(&alg_types_sem); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci return err; 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_unregister_type); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic void alg_do_release(const struct af_alg_type *type, void *private) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci if (!type) 1138c2ecf20Sopenharmony_ci return; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci type->release(private); 1168c2ecf20Sopenharmony_ci module_put(type->owner); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciint af_alg_release(struct socket *sock) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci if (sock->sk) { 1228c2ecf20Sopenharmony_ci sock_put(sock->sk); 1238c2ecf20Sopenharmony_ci sock->sk = NULL; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci return 0; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_release); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_civoid af_alg_release_parent(struct sock *sk) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 1328c2ecf20Sopenharmony_ci unsigned int nokey = atomic_read(&ask->nokey_refcnt); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci sk = ask->parent; 1358c2ecf20Sopenharmony_ci ask = alg_sk(sk); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci if (nokey) 1388c2ecf20Sopenharmony_ci atomic_dec(&ask->nokey_refcnt); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if (atomic_dec_and_test(&ask->refcnt)) 1418c2ecf20Sopenharmony_ci sock_put(sk); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_release_parent); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY; 1488c2ecf20Sopenharmony_ci struct sock *sk = sock->sk; 1498c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 1508c2ecf20Sopenharmony_ci struct sockaddr_alg_new *sa = (void *)uaddr; 1518c2ecf20Sopenharmony_ci const struct af_alg_type *type; 1528c2ecf20Sopenharmony_ci void *private; 1538c2ecf20Sopenharmony_ci int err; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (sock->state == SS_CONNECTED) 1568c2ecf20Sopenharmony_ci return -EINVAL; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci BUILD_BUG_ON(offsetof(struct sockaddr_alg_new, salg_name) != 1598c2ecf20Sopenharmony_ci offsetof(struct sockaddr_alg, salg_name)); 1608c2ecf20Sopenharmony_ci BUILD_BUG_ON(offsetof(struct sockaddr_alg, salg_name) != sizeof(*sa)); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci if (addr_len < sizeof(*sa) + 1) 1638c2ecf20Sopenharmony_ci return -EINVAL; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci /* If caller uses non-allowed flag, return error. */ 1668c2ecf20Sopenharmony_ci if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed)) 1678c2ecf20Sopenharmony_ci return -EINVAL; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci sa->salg_type[sizeof(sa->salg_type) - 1] = 0; 1708c2ecf20Sopenharmony_ci sa->salg_name[addr_len - sizeof(*sa) - 1] = 0; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci type = alg_get_type(sa->salg_type); 1738c2ecf20Sopenharmony_ci if (PTR_ERR(type) == -ENOENT) { 1748c2ecf20Sopenharmony_ci request_module("algif-%s", sa->salg_type); 1758c2ecf20Sopenharmony_ci type = alg_get_type(sa->salg_type); 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci if (IS_ERR(type)) 1798c2ecf20Sopenharmony_ci return PTR_ERR(type); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask); 1828c2ecf20Sopenharmony_ci if (IS_ERR(private)) { 1838c2ecf20Sopenharmony_ci module_put(type->owner); 1848c2ecf20Sopenharmony_ci return PTR_ERR(private); 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci err = -EBUSY; 1888c2ecf20Sopenharmony_ci lock_sock(sk); 1898c2ecf20Sopenharmony_ci if (atomic_read(&ask->refcnt)) 1908c2ecf20Sopenharmony_ci goto unlock; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci swap(ask->type, type); 1938c2ecf20Sopenharmony_ci swap(ask->private, private); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci err = 0; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ciunlock: 1988c2ecf20Sopenharmony_ci release_sock(sk); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci alg_do_release(type, private); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci return err; 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cistatic int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 2088c2ecf20Sopenharmony_ci const struct af_alg_type *type = ask->type; 2098c2ecf20Sopenharmony_ci u8 *key; 2108c2ecf20Sopenharmony_ci int err; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci key = sock_kmalloc(sk, keylen, GFP_KERNEL); 2138c2ecf20Sopenharmony_ci if (!key) 2148c2ecf20Sopenharmony_ci return -ENOMEM; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci err = -EFAULT; 2178c2ecf20Sopenharmony_ci if (copy_from_sockptr(key, ukey, keylen)) 2188c2ecf20Sopenharmony_ci goto out; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci err = type->setkey(ask->private, key, keylen); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ciout: 2238c2ecf20Sopenharmony_ci sock_kzfree_s(sk, key, keylen); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return err; 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic int alg_setsockopt(struct socket *sock, int level, int optname, 2298c2ecf20Sopenharmony_ci sockptr_t optval, unsigned int optlen) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci struct sock *sk = sock->sk; 2328c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 2338c2ecf20Sopenharmony_ci const struct af_alg_type *type; 2348c2ecf20Sopenharmony_ci int err = -EBUSY; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci lock_sock(sk); 2378c2ecf20Sopenharmony_ci if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt)) 2388c2ecf20Sopenharmony_ci goto unlock; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci type = ask->type; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci err = -ENOPROTOOPT; 2438c2ecf20Sopenharmony_ci if (level != SOL_ALG || !type) 2448c2ecf20Sopenharmony_ci goto unlock; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci switch (optname) { 2478c2ecf20Sopenharmony_ci case ALG_SET_KEY: 2488c2ecf20Sopenharmony_ci if (sock->state == SS_CONNECTED) 2498c2ecf20Sopenharmony_ci goto unlock; 2508c2ecf20Sopenharmony_ci if (!type->setkey) 2518c2ecf20Sopenharmony_ci goto unlock; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci err = alg_setkey(sk, optval, optlen); 2548c2ecf20Sopenharmony_ci break; 2558c2ecf20Sopenharmony_ci case ALG_SET_AEAD_AUTHSIZE: 2568c2ecf20Sopenharmony_ci if (sock->state == SS_CONNECTED) 2578c2ecf20Sopenharmony_ci goto unlock; 2588c2ecf20Sopenharmony_ci if (!type->setauthsize) 2598c2ecf20Sopenharmony_ci goto unlock; 2608c2ecf20Sopenharmony_ci err = type->setauthsize(ask->private, optlen); 2618c2ecf20Sopenharmony_ci break; 2628c2ecf20Sopenharmony_ci case ALG_SET_DRBG_ENTROPY: 2638c2ecf20Sopenharmony_ci if (sock->state == SS_CONNECTED) 2648c2ecf20Sopenharmony_ci goto unlock; 2658c2ecf20Sopenharmony_ci if (!type->setentropy) 2668c2ecf20Sopenharmony_ci goto unlock; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci err = type->setentropy(ask->private, optval, optlen); 2698c2ecf20Sopenharmony_ci } 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ciunlock: 2728c2ecf20Sopenharmony_ci release_sock(sk); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci return err; 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ciint af_alg_accept(struct sock *sk, struct socket *newsock, bool kern) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 2808c2ecf20Sopenharmony_ci const struct af_alg_type *type; 2818c2ecf20Sopenharmony_ci struct sock *sk2; 2828c2ecf20Sopenharmony_ci unsigned int nokey; 2838c2ecf20Sopenharmony_ci int err; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci lock_sock(sk); 2868c2ecf20Sopenharmony_ci type = ask->type; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci err = -EINVAL; 2898c2ecf20Sopenharmony_ci if (!type) 2908c2ecf20Sopenharmony_ci goto unlock; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern); 2938c2ecf20Sopenharmony_ci err = -ENOMEM; 2948c2ecf20Sopenharmony_ci if (!sk2) 2958c2ecf20Sopenharmony_ci goto unlock; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci sock_init_data(newsock, sk2); 2988c2ecf20Sopenharmony_ci security_sock_graft(sk2, newsock); 2998c2ecf20Sopenharmony_ci security_sk_clone(sk, sk2); 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci /* 3028c2ecf20Sopenharmony_ci * newsock->ops assigned here to allow type->accept call to override 3038c2ecf20Sopenharmony_ci * them when required. 3048c2ecf20Sopenharmony_ci */ 3058c2ecf20Sopenharmony_ci newsock->ops = type->ops; 3068c2ecf20Sopenharmony_ci err = type->accept(ask->private, sk2); 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci nokey = err == -ENOKEY; 3098c2ecf20Sopenharmony_ci if (nokey && type->accept_nokey) 3108c2ecf20Sopenharmony_ci err = type->accept_nokey(ask->private, sk2); 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci if (err) 3138c2ecf20Sopenharmony_ci goto unlock; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci if (atomic_inc_return_relaxed(&ask->refcnt) == 1) 3168c2ecf20Sopenharmony_ci sock_hold(sk); 3178c2ecf20Sopenharmony_ci if (nokey) { 3188c2ecf20Sopenharmony_ci atomic_inc(&ask->nokey_refcnt); 3198c2ecf20Sopenharmony_ci atomic_set(&alg_sk(sk2)->nokey_refcnt, 1); 3208c2ecf20Sopenharmony_ci } 3218c2ecf20Sopenharmony_ci alg_sk(sk2)->parent = sk; 3228c2ecf20Sopenharmony_ci alg_sk(sk2)->type = type; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci newsock->state = SS_CONNECTED; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci if (nokey) 3278c2ecf20Sopenharmony_ci newsock->ops = type->ops_nokey; 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci err = 0; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ciunlock: 3328c2ecf20Sopenharmony_ci release_sock(sk); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci return err; 3358c2ecf20Sopenharmony_ci} 3368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_accept); 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_cistatic int alg_accept(struct socket *sock, struct socket *newsock, int flags, 3398c2ecf20Sopenharmony_ci bool kern) 3408c2ecf20Sopenharmony_ci{ 3418c2ecf20Sopenharmony_ci return af_alg_accept(sock->sk, newsock, kern); 3428c2ecf20Sopenharmony_ci} 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_cistatic const struct proto_ops alg_proto_ops = { 3458c2ecf20Sopenharmony_ci .family = PF_ALG, 3468c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci .connect = sock_no_connect, 3498c2ecf20Sopenharmony_ci .socketpair = sock_no_socketpair, 3508c2ecf20Sopenharmony_ci .getname = sock_no_getname, 3518c2ecf20Sopenharmony_ci .ioctl = sock_no_ioctl, 3528c2ecf20Sopenharmony_ci .listen = sock_no_listen, 3538c2ecf20Sopenharmony_ci .shutdown = sock_no_shutdown, 3548c2ecf20Sopenharmony_ci .mmap = sock_no_mmap, 3558c2ecf20Sopenharmony_ci .sendpage = sock_no_sendpage, 3568c2ecf20Sopenharmony_ci .sendmsg = sock_no_sendmsg, 3578c2ecf20Sopenharmony_ci .recvmsg = sock_no_recvmsg, 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci .bind = alg_bind, 3608c2ecf20Sopenharmony_ci .release = af_alg_release, 3618c2ecf20Sopenharmony_ci .setsockopt = alg_setsockopt, 3628c2ecf20Sopenharmony_ci .accept = alg_accept, 3638c2ecf20Sopenharmony_ci}; 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_cistatic void alg_sock_destruct(struct sock *sk) 3668c2ecf20Sopenharmony_ci{ 3678c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci alg_do_release(ask->type, ask->private); 3708c2ecf20Sopenharmony_ci} 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_cistatic int alg_create(struct net *net, struct socket *sock, int protocol, 3738c2ecf20Sopenharmony_ci int kern) 3748c2ecf20Sopenharmony_ci{ 3758c2ecf20Sopenharmony_ci struct sock *sk; 3768c2ecf20Sopenharmony_ci int err; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci if (sock->type != SOCK_SEQPACKET) 3798c2ecf20Sopenharmony_ci return -ESOCKTNOSUPPORT; 3808c2ecf20Sopenharmony_ci if (protocol != 0) 3818c2ecf20Sopenharmony_ci return -EPROTONOSUPPORT; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci err = -ENOMEM; 3848c2ecf20Sopenharmony_ci sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern); 3858c2ecf20Sopenharmony_ci if (!sk) 3868c2ecf20Sopenharmony_ci goto out; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci sock->ops = &alg_proto_ops; 3898c2ecf20Sopenharmony_ci sock_init_data(sock, sk); 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci sk->sk_destruct = alg_sock_destruct; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci return 0; 3948c2ecf20Sopenharmony_ciout: 3958c2ecf20Sopenharmony_ci return err; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_cistatic const struct net_proto_family alg_family = { 3998c2ecf20Sopenharmony_ci .family = PF_ALG, 4008c2ecf20Sopenharmony_ci .create = alg_create, 4018c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 4028c2ecf20Sopenharmony_ci}; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ciint af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len) 4058c2ecf20Sopenharmony_ci{ 4068c2ecf20Sopenharmony_ci size_t off; 4078c2ecf20Sopenharmony_ci ssize_t n; 4088c2ecf20Sopenharmony_ci int npages, i; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off); 4118c2ecf20Sopenharmony_ci if (n < 0) 4128c2ecf20Sopenharmony_ci return n; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT; 4158c2ecf20Sopenharmony_ci if (WARN_ON(npages == 0)) 4168c2ecf20Sopenharmony_ci return -EINVAL; 4178c2ecf20Sopenharmony_ci /* Add one extra for linking */ 4188c2ecf20Sopenharmony_ci sg_init_table(sgl->sg, npages + 1); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci for (i = 0, len = n; i < npages; i++) { 4218c2ecf20Sopenharmony_ci int plen = min_t(int, len, PAGE_SIZE - off); 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci sg_set_page(sgl->sg + i, sgl->pages[i], plen, off); 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci off = 0; 4268c2ecf20Sopenharmony_ci len -= plen; 4278c2ecf20Sopenharmony_ci } 4288c2ecf20Sopenharmony_ci sg_mark_end(sgl->sg + npages - 1); 4298c2ecf20Sopenharmony_ci sgl->npages = npages; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci return n; 4328c2ecf20Sopenharmony_ci} 4338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_make_sg); 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistatic void af_alg_link_sg(struct af_alg_sgl *sgl_prev, 4368c2ecf20Sopenharmony_ci struct af_alg_sgl *sgl_new) 4378c2ecf20Sopenharmony_ci{ 4388c2ecf20Sopenharmony_ci sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1); 4398c2ecf20Sopenharmony_ci sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg); 4408c2ecf20Sopenharmony_ci} 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_civoid af_alg_free_sg(struct af_alg_sgl *sgl) 4438c2ecf20Sopenharmony_ci{ 4448c2ecf20Sopenharmony_ci int i; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci for (i = 0; i < sgl->npages; i++) 4478c2ecf20Sopenharmony_ci put_page(sgl->pages[i]); 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_free_sg); 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_cistatic int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con) 4528c2ecf20Sopenharmony_ci{ 4538c2ecf20Sopenharmony_ci struct cmsghdr *cmsg; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci for_each_cmsghdr(cmsg, msg) { 4568c2ecf20Sopenharmony_ci if (!CMSG_OK(msg, cmsg)) 4578c2ecf20Sopenharmony_ci return -EINVAL; 4588c2ecf20Sopenharmony_ci if (cmsg->cmsg_level != SOL_ALG) 4598c2ecf20Sopenharmony_ci continue; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci switch (cmsg->cmsg_type) { 4628c2ecf20Sopenharmony_ci case ALG_SET_IV: 4638c2ecf20Sopenharmony_ci if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv))) 4648c2ecf20Sopenharmony_ci return -EINVAL; 4658c2ecf20Sopenharmony_ci con->iv = (void *)CMSG_DATA(cmsg); 4668c2ecf20Sopenharmony_ci if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen + 4678c2ecf20Sopenharmony_ci sizeof(*con->iv))) 4688c2ecf20Sopenharmony_ci return -EINVAL; 4698c2ecf20Sopenharmony_ci break; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci case ALG_SET_OP: 4728c2ecf20Sopenharmony_ci if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32))) 4738c2ecf20Sopenharmony_ci return -EINVAL; 4748c2ecf20Sopenharmony_ci con->op = *(u32 *)CMSG_DATA(cmsg); 4758c2ecf20Sopenharmony_ci break; 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci case ALG_SET_AEAD_ASSOCLEN: 4788c2ecf20Sopenharmony_ci if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32))) 4798c2ecf20Sopenharmony_ci return -EINVAL; 4808c2ecf20Sopenharmony_ci con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg); 4818c2ecf20Sopenharmony_ci break; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci default: 4848c2ecf20Sopenharmony_ci return -EINVAL; 4858c2ecf20Sopenharmony_ci } 4868c2ecf20Sopenharmony_ci } 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci return 0; 4898c2ecf20Sopenharmony_ci} 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci/** 4928c2ecf20Sopenharmony_ci * af_alg_alloc_tsgl - allocate the TX SGL 4938c2ecf20Sopenharmony_ci * 4948c2ecf20Sopenharmony_ci * @sk socket of connection to user space 4958c2ecf20Sopenharmony_ci * @return: 0 upon success, < 0 upon error 4968c2ecf20Sopenharmony_ci */ 4978c2ecf20Sopenharmony_cistatic int af_alg_alloc_tsgl(struct sock *sk) 4988c2ecf20Sopenharmony_ci{ 4998c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 5008c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 5018c2ecf20Sopenharmony_ci struct af_alg_tsgl *sgl; 5028c2ecf20Sopenharmony_ci struct scatterlist *sg = NULL; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list); 5058c2ecf20Sopenharmony_ci if (!list_empty(&ctx->tsgl_list)) 5068c2ecf20Sopenharmony_ci sg = sgl->sg; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci if (!sg || sgl->cur >= MAX_SGL_ENTS) { 5098c2ecf20Sopenharmony_ci sgl = sock_kmalloc(sk, 5108c2ecf20Sopenharmony_ci struct_size(sgl, sg, (MAX_SGL_ENTS + 1)), 5118c2ecf20Sopenharmony_ci GFP_KERNEL); 5128c2ecf20Sopenharmony_ci if (!sgl) 5138c2ecf20Sopenharmony_ci return -ENOMEM; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci sg_init_table(sgl->sg, MAX_SGL_ENTS + 1); 5168c2ecf20Sopenharmony_ci sgl->cur = 0; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci if (sg) 5198c2ecf20Sopenharmony_ci sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg); 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci list_add_tail(&sgl->list, &ctx->tsgl_list); 5228c2ecf20Sopenharmony_ci } 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci return 0; 5258c2ecf20Sopenharmony_ci} 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci/** 5288c2ecf20Sopenharmony_ci * aead_count_tsgl - Count number of TX SG entries 5298c2ecf20Sopenharmony_ci * 5308c2ecf20Sopenharmony_ci * The counting starts from the beginning of the SGL to @bytes. If 5318c2ecf20Sopenharmony_ci * an offset is provided, the counting of the SG entries starts at the offset. 5328c2ecf20Sopenharmony_ci * 5338c2ecf20Sopenharmony_ci * @sk socket of connection to user space 5348c2ecf20Sopenharmony_ci * @bytes Count the number of SG entries holding given number of bytes. 5358c2ecf20Sopenharmony_ci * @offset Start the counting of SG entries from the given offset. 5368c2ecf20Sopenharmony_ci * @return Number of TX SG entries found given the constraints 5378c2ecf20Sopenharmony_ci */ 5388c2ecf20Sopenharmony_ciunsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset) 5398c2ecf20Sopenharmony_ci{ 5408c2ecf20Sopenharmony_ci const struct alg_sock *ask = alg_sk(sk); 5418c2ecf20Sopenharmony_ci const struct af_alg_ctx *ctx = ask->private; 5428c2ecf20Sopenharmony_ci const struct af_alg_tsgl *sgl; 5438c2ecf20Sopenharmony_ci unsigned int i; 5448c2ecf20Sopenharmony_ci unsigned int sgl_count = 0; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci if (!bytes) 5478c2ecf20Sopenharmony_ci return 0; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci list_for_each_entry(sgl, &ctx->tsgl_list, list) { 5508c2ecf20Sopenharmony_ci const struct scatterlist *sg = sgl->sg; 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci for (i = 0; i < sgl->cur; i++) { 5538c2ecf20Sopenharmony_ci size_t bytes_count; 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci /* Skip offset */ 5568c2ecf20Sopenharmony_ci if (offset >= sg[i].length) { 5578c2ecf20Sopenharmony_ci offset -= sg[i].length; 5588c2ecf20Sopenharmony_ci bytes -= sg[i].length; 5598c2ecf20Sopenharmony_ci continue; 5608c2ecf20Sopenharmony_ci } 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci bytes_count = sg[i].length - offset; 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci offset = 0; 5658c2ecf20Sopenharmony_ci sgl_count++; 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci /* If we have seen requested number of bytes, stop */ 5688c2ecf20Sopenharmony_ci if (bytes_count >= bytes) 5698c2ecf20Sopenharmony_ci return sgl_count; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci bytes -= bytes_count; 5728c2ecf20Sopenharmony_ci } 5738c2ecf20Sopenharmony_ci } 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci return sgl_count; 5768c2ecf20Sopenharmony_ci} 5778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_count_tsgl); 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci/** 5808c2ecf20Sopenharmony_ci * aead_pull_tsgl - Release the specified buffers from TX SGL 5818c2ecf20Sopenharmony_ci * 5828c2ecf20Sopenharmony_ci * If @dst is non-null, reassign the pages to dst. The caller must release 5838c2ecf20Sopenharmony_ci * the pages. If @dst_offset is given only reassign the pages to @dst starting 5848c2ecf20Sopenharmony_ci * at the @dst_offset (byte). The caller must ensure that @dst is large 5858c2ecf20Sopenharmony_ci * enough (e.g. by using af_alg_count_tsgl with the same offset). 5868c2ecf20Sopenharmony_ci * 5878c2ecf20Sopenharmony_ci * @sk socket of connection to user space 5888c2ecf20Sopenharmony_ci * @used Number of bytes to pull from TX SGL 5898c2ecf20Sopenharmony_ci * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The 5908c2ecf20Sopenharmony_ci * caller must release the buffers in dst. 5918c2ecf20Sopenharmony_ci * @dst_offset Reassign the TX SGL from given offset. All buffers before 5928c2ecf20Sopenharmony_ci * reaching the offset is released. 5938c2ecf20Sopenharmony_ci */ 5948c2ecf20Sopenharmony_civoid af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst, 5958c2ecf20Sopenharmony_ci size_t dst_offset) 5968c2ecf20Sopenharmony_ci{ 5978c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 5988c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 5998c2ecf20Sopenharmony_ci struct af_alg_tsgl *sgl; 6008c2ecf20Sopenharmony_ci struct scatterlist *sg; 6018c2ecf20Sopenharmony_ci unsigned int i, j = 0; 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci while (!list_empty(&ctx->tsgl_list)) { 6048c2ecf20Sopenharmony_ci sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl, 6058c2ecf20Sopenharmony_ci list); 6068c2ecf20Sopenharmony_ci sg = sgl->sg; 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci for (i = 0; i < sgl->cur; i++) { 6098c2ecf20Sopenharmony_ci size_t plen = min_t(size_t, used, sg[i].length); 6108c2ecf20Sopenharmony_ci struct page *page = sg_page(sg + i); 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci if (!page) 6138c2ecf20Sopenharmony_ci continue; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci /* 6168c2ecf20Sopenharmony_ci * Assumption: caller created af_alg_count_tsgl(len) 6178c2ecf20Sopenharmony_ci * SG entries in dst. 6188c2ecf20Sopenharmony_ci */ 6198c2ecf20Sopenharmony_ci if (dst) { 6208c2ecf20Sopenharmony_ci if (dst_offset >= plen) { 6218c2ecf20Sopenharmony_ci /* discard page before offset */ 6228c2ecf20Sopenharmony_ci dst_offset -= plen; 6238c2ecf20Sopenharmony_ci } else { 6248c2ecf20Sopenharmony_ci /* reassign page to dst after offset */ 6258c2ecf20Sopenharmony_ci get_page(page); 6268c2ecf20Sopenharmony_ci sg_set_page(dst + j, page, 6278c2ecf20Sopenharmony_ci plen - dst_offset, 6288c2ecf20Sopenharmony_ci sg[i].offset + dst_offset); 6298c2ecf20Sopenharmony_ci dst_offset = 0; 6308c2ecf20Sopenharmony_ci j++; 6318c2ecf20Sopenharmony_ci } 6328c2ecf20Sopenharmony_ci } 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci sg[i].length -= plen; 6358c2ecf20Sopenharmony_ci sg[i].offset += plen; 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci used -= plen; 6388c2ecf20Sopenharmony_ci ctx->used -= plen; 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci if (sg[i].length) 6418c2ecf20Sopenharmony_ci return; 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci put_page(page); 6448c2ecf20Sopenharmony_ci sg_assign_page(sg + i, NULL); 6458c2ecf20Sopenharmony_ci } 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci list_del(&sgl->list); 6488c2ecf20Sopenharmony_ci sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1)); 6498c2ecf20Sopenharmony_ci } 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci if (!ctx->used) 6528c2ecf20Sopenharmony_ci ctx->merge = 0; 6538c2ecf20Sopenharmony_ci ctx->init = ctx->more; 6548c2ecf20Sopenharmony_ci} 6558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_pull_tsgl); 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci/** 6588c2ecf20Sopenharmony_ci * af_alg_free_areq_sgls - Release TX and RX SGLs of the request 6598c2ecf20Sopenharmony_ci * 6608c2ecf20Sopenharmony_ci * @areq Request holding the TX and RX SGL 6618c2ecf20Sopenharmony_ci */ 6628c2ecf20Sopenharmony_cistatic void af_alg_free_areq_sgls(struct af_alg_async_req *areq) 6638c2ecf20Sopenharmony_ci{ 6648c2ecf20Sopenharmony_ci struct sock *sk = areq->sk; 6658c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 6668c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 6678c2ecf20Sopenharmony_ci struct af_alg_rsgl *rsgl, *tmp; 6688c2ecf20Sopenharmony_ci struct scatterlist *tsgl; 6698c2ecf20Sopenharmony_ci struct scatterlist *sg; 6708c2ecf20Sopenharmony_ci unsigned int i; 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) { 6738c2ecf20Sopenharmony_ci atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused); 6748c2ecf20Sopenharmony_ci af_alg_free_sg(&rsgl->sgl); 6758c2ecf20Sopenharmony_ci list_del(&rsgl->list); 6768c2ecf20Sopenharmony_ci if (rsgl != &areq->first_rsgl) 6778c2ecf20Sopenharmony_ci sock_kfree_s(sk, rsgl, sizeof(*rsgl)); 6788c2ecf20Sopenharmony_ci } 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci tsgl = areq->tsgl; 6818c2ecf20Sopenharmony_ci if (tsgl) { 6828c2ecf20Sopenharmony_ci for_each_sg(tsgl, sg, areq->tsgl_entries, i) { 6838c2ecf20Sopenharmony_ci if (!sg_page(sg)) 6848c2ecf20Sopenharmony_ci continue; 6858c2ecf20Sopenharmony_ci put_page(sg_page(sg)); 6868c2ecf20Sopenharmony_ci } 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl)); 6898c2ecf20Sopenharmony_ci } 6908c2ecf20Sopenharmony_ci} 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci/** 6938c2ecf20Sopenharmony_ci * af_alg_wait_for_wmem - wait for availability of writable memory 6948c2ecf20Sopenharmony_ci * 6958c2ecf20Sopenharmony_ci * @sk socket of connection to user space 6968c2ecf20Sopenharmony_ci * @flags If MSG_DONTWAIT is set, then only report if function would sleep 6978c2ecf20Sopenharmony_ci * @return 0 when writable memory is available, < 0 upon error 6988c2ecf20Sopenharmony_ci */ 6998c2ecf20Sopenharmony_cistatic int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags) 7008c2ecf20Sopenharmony_ci{ 7018c2ecf20Sopenharmony_ci DEFINE_WAIT_FUNC(wait, woken_wake_function); 7028c2ecf20Sopenharmony_ci int err = -ERESTARTSYS; 7038c2ecf20Sopenharmony_ci long timeout; 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci if (flags & MSG_DONTWAIT) 7068c2ecf20Sopenharmony_ci return -EAGAIN; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci add_wait_queue(sk_sleep(sk), &wait); 7118c2ecf20Sopenharmony_ci for (;;) { 7128c2ecf20Sopenharmony_ci if (signal_pending(current)) 7138c2ecf20Sopenharmony_ci break; 7148c2ecf20Sopenharmony_ci timeout = MAX_SCHEDULE_TIMEOUT; 7158c2ecf20Sopenharmony_ci if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) { 7168c2ecf20Sopenharmony_ci err = 0; 7178c2ecf20Sopenharmony_ci break; 7188c2ecf20Sopenharmony_ci } 7198c2ecf20Sopenharmony_ci } 7208c2ecf20Sopenharmony_ci remove_wait_queue(sk_sleep(sk), &wait); 7218c2ecf20Sopenharmony_ci 7228c2ecf20Sopenharmony_ci return err; 7238c2ecf20Sopenharmony_ci} 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_ci/** 7268c2ecf20Sopenharmony_ci * af_alg_wmem_wakeup - wakeup caller when writable memory is available 7278c2ecf20Sopenharmony_ci * 7288c2ecf20Sopenharmony_ci * @sk socket of connection to user space 7298c2ecf20Sopenharmony_ci */ 7308c2ecf20Sopenharmony_civoid af_alg_wmem_wakeup(struct sock *sk) 7318c2ecf20Sopenharmony_ci{ 7328c2ecf20Sopenharmony_ci struct socket_wq *wq; 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci if (!af_alg_writable(sk)) 7358c2ecf20Sopenharmony_ci return; 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci rcu_read_lock(); 7388c2ecf20Sopenharmony_ci wq = rcu_dereference(sk->sk_wq); 7398c2ecf20Sopenharmony_ci if (skwq_has_sleeper(wq)) 7408c2ecf20Sopenharmony_ci wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | 7418c2ecf20Sopenharmony_ci EPOLLRDNORM | 7428c2ecf20Sopenharmony_ci EPOLLRDBAND); 7438c2ecf20Sopenharmony_ci sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 7448c2ecf20Sopenharmony_ci rcu_read_unlock(); 7458c2ecf20Sopenharmony_ci} 7468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_wmem_wakeup); 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci/** 7498c2ecf20Sopenharmony_ci * af_alg_wait_for_data - wait for availability of TX data 7508c2ecf20Sopenharmony_ci * 7518c2ecf20Sopenharmony_ci * @sk socket of connection to user space 7528c2ecf20Sopenharmony_ci * @flags If MSG_DONTWAIT is set, then only report if function would sleep 7538c2ecf20Sopenharmony_ci * @min Set to minimum request size if partial requests are allowed. 7548c2ecf20Sopenharmony_ci * @return 0 when writable memory is available, < 0 upon error 7558c2ecf20Sopenharmony_ci */ 7568c2ecf20Sopenharmony_ciint af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min) 7578c2ecf20Sopenharmony_ci{ 7588c2ecf20Sopenharmony_ci DEFINE_WAIT_FUNC(wait, woken_wake_function); 7598c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 7608c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 7618c2ecf20Sopenharmony_ci long timeout; 7628c2ecf20Sopenharmony_ci int err = -ERESTARTSYS; 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci if (flags & MSG_DONTWAIT) 7658c2ecf20Sopenharmony_ci return -EAGAIN; 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci add_wait_queue(sk_sleep(sk), &wait); 7708c2ecf20Sopenharmony_ci for (;;) { 7718c2ecf20Sopenharmony_ci if (signal_pending(current)) 7728c2ecf20Sopenharmony_ci break; 7738c2ecf20Sopenharmony_ci timeout = MAX_SCHEDULE_TIMEOUT; 7748c2ecf20Sopenharmony_ci if (sk_wait_event(sk, &timeout, 7758c2ecf20Sopenharmony_ci ctx->init && (!ctx->more || 7768c2ecf20Sopenharmony_ci (min && ctx->used >= min)), 7778c2ecf20Sopenharmony_ci &wait)) { 7788c2ecf20Sopenharmony_ci err = 0; 7798c2ecf20Sopenharmony_ci break; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci } 7828c2ecf20Sopenharmony_ci remove_wait_queue(sk_sleep(sk), &wait); 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci return err; 7878c2ecf20Sopenharmony_ci} 7888c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_wait_for_data); 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci/** 7918c2ecf20Sopenharmony_ci * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel 7928c2ecf20Sopenharmony_ci * 7938c2ecf20Sopenharmony_ci * @sk socket of connection to user space 7948c2ecf20Sopenharmony_ci */ 7958c2ecf20Sopenharmony_cistatic void af_alg_data_wakeup(struct sock *sk) 7968c2ecf20Sopenharmony_ci{ 7978c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 7988c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 7998c2ecf20Sopenharmony_ci struct socket_wq *wq; 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci if (!ctx->used) 8028c2ecf20Sopenharmony_ci return; 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci rcu_read_lock(); 8058c2ecf20Sopenharmony_ci wq = rcu_dereference(sk->sk_wq); 8068c2ecf20Sopenharmony_ci if (skwq_has_sleeper(wq)) 8078c2ecf20Sopenharmony_ci wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT | 8088c2ecf20Sopenharmony_ci EPOLLRDNORM | 8098c2ecf20Sopenharmony_ci EPOLLRDBAND); 8108c2ecf20Sopenharmony_ci sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); 8118c2ecf20Sopenharmony_ci rcu_read_unlock(); 8128c2ecf20Sopenharmony_ci} 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci/** 8158c2ecf20Sopenharmony_ci * af_alg_sendmsg - implementation of sendmsg system call handler 8168c2ecf20Sopenharmony_ci * 8178c2ecf20Sopenharmony_ci * The sendmsg system call handler obtains the user data and stores it 8188c2ecf20Sopenharmony_ci * in ctx->tsgl_list. This implies allocation of the required numbers of 8198c2ecf20Sopenharmony_ci * struct af_alg_tsgl. 8208c2ecf20Sopenharmony_ci * 8218c2ecf20Sopenharmony_ci * In addition, the ctx is filled with the information sent via CMSG. 8228c2ecf20Sopenharmony_ci * 8238c2ecf20Sopenharmony_ci * @sock socket of connection to user space 8248c2ecf20Sopenharmony_ci * @msg message from user space 8258c2ecf20Sopenharmony_ci * @size size of message from user space 8268c2ecf20Sopenharmony_ci * @ivsize the size of the IV for the cipher operation to verify that the 8278c2ecf20Sopenharmony_ci * user-space-provided IV has the right size 8288c2ecf20Sopenharmony_ci * @return the number of copied data upon success, < 0 upon error 8298c2ecf20Sopenharmony_ci */ 8308c2ecf20Sopenharmony_ciint af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, 8318c2ecf20Sopenharmony_ci unsigned int ivsize) 8328c2ecf20Sopenharmony_ci{ 8338c2ecf20Sopenharmony_ci struct sock *sk = sock->sk; 8348c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 8358c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 8368c2ecf20Sopenharmony_ci struct af_alg_tsgl *sgl; 8378c2ecf20Sopenharmony_ci struct af_alg_control con = {}; 8388c2ecf20Sopenharmony_ci long copied = 0; 8398c2ecf20Sopenharmony_ci bool enc = false; 8408c2ecf20Sopenharmony_ci bool init = false; 8418c2ecf20Sopenharmony_ci int err = 0; 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci if (msg->msg_controllen) { 8448c2ecf20Sopenharmony_ci err = af_alg_cmsg_send(msg, &con); 8458c2ecf20Sopenharmony_ci if (err) 8468c2ecf20Sopenharmony_ci return err; 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci init = true; 8498c2ecf20Sopenharmony_ci switch (con.op) { 8508c2ecf20Sopenharmony_ci case ALG_OP_ENCRYPT: 8518c2ecf20Sopenharmony_ci enc = true; 8528c2ecf20Sopenharmony_ci break; 8538c2ecf20Sopenharmony_ci case ALG_OP_DECRYPT: 8548c2ecf20Sopenharmony_ci enc = false; 8558c2ecf20Sopenharmony_ci break; 8568c2ecf20Sopenharmony_ci default: 8578c2ecf20Sopenharmony_ci return -EINVAL; 8588c2ecf20Sopenharmony_ci } 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_ci if (con.iv && con.iv->ivlen != ivsize) 8618c2ecf20Sopenharmony_ci return -EINVAL; 8628c2ecf20Sopenharmony_ci } 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci lock_sock(sk); 8658c2ecf20Sopenharmony_ci if (ctx->init && !ctx->more) { 8668c2ecf20Sopenharmony_ci if (ctx->used) { 8678c2ecf20Sopenharmony_ci err = -EINVAL; 8688c2ecf20Sopenharmony_ci goto unlock; 8698c2ecf20Sopenharmony_ci } 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci pr_info_once( 8728c2ecf20Sopenharmony_ci "%s sent an empty control message without MSG_MORE.\n", 8738c2ecf20Sopenharmony_ci current->comm); 8748c2ecf20Sopenharmony_ci } 8758c2ecf20Sopenharmony_ci ctx->init = true; 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci if (init) { 8788c2ecf20Sopenharmony_ci ctx->enc = enc; 8798c2ecf20Sopenharmony_ci if (con.iv) 8808c2ecf20Sopenharmony_ci memcpy(ctx->iv, con.iv->iv, ivsize); 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ci ctx->aead_assoclen = con.aead_assoclen; 8838c2ecf20Sopenharmony_ci } 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci while (size) { 8868c2ecf20Sopenharmony_ci struct scatterlist *sg; 8878c2ecf20Sopenharmony_ci size_t len = size; 8888c2ecf20Sopenharmony_ci size_t plen; 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci /* use the existing memory in an allocated page */ 8918c2ecf20Sopenharmony_ci if (ctx->merge) { 8928c2ecf20Sopenharmony_ci sgl = list_entry(ctx->tsgl_list.prev, 8938c2ecf20Sopenharmony_ci struct af_alg_tsgl, list); 8948c2ecf20Sopenharmony_ci sg = sgl->sg + sgl->cur - 1; 8958c2ecf20Sopenharmony_ci len = min_t(size_t, len, 8968c2ecf20Sopenharmony_ci PAGE_SIZE - sg->offset - sg->length); 8978c2ecf20Sopenharmony_ci 8988c2ecf20Sopenharmony_ci err = memcpy_from_msg(page_address(sg_page(sg)) + 8998c2ecf20Sopenharmony_ci sg->offset + sg->length, 9008c2ecf20Sopenharmony_ci msg, len); 9018c2ecf20Sopenharmony_ci if (err) 9028c2ecf20Sopenharmony_ci goto unlock; 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_ci sg->length += len; 9058c2ecf20Sopenharmony_ci ctx->merge = (sg->offset + sg->length) & 9068c2ecf20Sopenharmony_ci (PAGE_SIZE - 1); 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci ctx->used += len; 9098c2ecf20Sopenharmony_ci copied += len; 9108c2ecf20Sopenharmony_ci size -= len; 9118c2ecf20Sopenharmony_ci continue; 9128c2ecf20Sopenharmony_ci } 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci if (!af_alg_writable(sk)) { 9158c2ecf20Sopenharmony_ci err = af_alg_wait_for_wmem(sk, msg->msg_flags); 9168c2ecf20Sopenharmony_ci if (err) 9178c2ecf20Sopenharmony_ci goto unlock; 9188c2ecf20Sopenharmony_ci } 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci /* allocate a new page */ 9218c2ecf20Sopenharmony_ci len = min_t(unsigned long, len, af_alg_sndbuf(sk)); 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci err = af_alg_alloc_tsgl(sk); 9248c2ecf20Sopenharmony_ci if (err) 9258c2ecf20Sopenharmony_ci goto unlock; 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_ci sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, 9288c2ecf20Sopenharmony_ci list); 9298c2ecf20Sopenharmony_ci sg = sgl->sg; 9308c2ecf20Sopenharmony_ci if (sgl->cur) 9318c2ecf20Sopenharmony_ci sg_unmark_end(sg + sgl->cur - 1); 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci do { 9348c2ecf20Sopenharmony_ci unsigned int i = sgl->cur; 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci plen = min_t(size_t, len, PAGE_SIZE); 9378c2ecf20Sopenharmony_ci 9388c2ecf20Sopenharmony_ci sg_assign_page(sg + i, alloc_page(GFP_KERNEL)); 9398c2ecf20Sopenharmony_ci if (!sg_page(sg + i)) { 9408c2ecf20Sopenharmony_ci err = -ENOMEM; 9418c2ecf20Sopenharmony_ci goto unlock; 9428c2ecf20Sopenharmony_ci } 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci err = memcpy_from_msg(page_address(sg_page(sg + i)), 9458c2ecf20Sopenharmony_ci msg, plen); 9468c2ecf20Sopenharmony_ci if (err) { 9478c2ecf20Sopenharmony_ci __free_page(sg_page(sg + i)); 9488c2ecf20Sopenharmony_ci sg_assign_page(sg + i, NULL); 9498c2ecf20Sopenharmony_ci goto unlock; 9508c2ecf20Sopenharmony_ci } 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci sg[i].length = plen; 9538c2ecf20Sopenharmony_ci len -= plen; 9548c2ecf20Sopenharmony_ci ctx->used += plen; 9558c2ecf20Sopenharmony_ci copied += plen; 9568c2ecf20Sopenharmony_ci size -= plen; 9578c2ecf20Sopenharmony_ci sgl->cur++; 9588c2ecf20Sopenharmony_ci } while (len && sgl->cur < MAX_SGL_ENTS); 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci if (!size) 9618c2ecf20Sopenharmony_ci sg_mark_end(sg + sgl->cur - 1); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci ctx->merge = plen & (PAGE_SIZE - 1); 9648c2ecf20Sopenharmony_ci } 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci err = 0; 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_ci ctx->more = msg->msg_flags & MSG_MORE; 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ciunlock: 9718c2ecf20Sopenharmony_ci af_alg_data_wakeup(sk); 9728c2ecf20Sopenharmony_ci release_sock(sk); 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci return copied ?: err; 9758c2ecf20Sopenharmony_ci} 9768c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_sendmsg); 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci/** 9798c2ecf20Sopenharmony_ci * af_alg_sendpage - sendpage system call handler 9808c2ecf20Sopenharmony_ci * 9818c2ecf20Sopenharmony_ci * This is a generic implementation of sendpage to fill ctx->tsgl_list. 9828c2ecf20Sopenharmony_ci */ 9838c2ecf20Sopenharmony_cissize_t af_alg_sendpage(struct socket *sock, struct page *page, 9848c2ecf20Sopenharmony_ci int offset, size_t size, int flags) 9858c2ecf20Sopenharmony_ci{ 9868c2ecf20Sopenharmony_ci struct sock *sk = sock->sk; 9878c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 9888c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 9898c2ecf20Sopenharmony_ci struct af_alg_tsgl *sgl; 9908c2ecf20Sopenharmony_ci int err = -EINVAL; 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_ci if (flags & MSG_SENDPAGE_NOTLAST) 9938c2ecf20Sopenharmony_ci flags |= MSG_MORE; 9948c2ecf20Sopenharmony_ci 9958c2ecf20Sopenharmony_ci lock_sock(sk); 9968c2ecf20Sopenharmony_ci if (!ctx->more && ctx->used) 9978c2ecf20Sopenharmony_ci goto unlock; 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci if (!size) 10008c2ecf20Sopenharmony_ci goto done; 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_ci if (!af_alg_writable(sk)) { 10038c2ecf20Sopenharmony_ci err = af_alg_wait_for_wmem(sk, flags); 10048c2ecf20Sopenharmony_ci if (err) 10058c2ecf20Sopenharmony_ci goto unlock; 10068c2ecf20Sopenharmony_ci } 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci err = af_alg_alloc_tsgl(sk); 10098c2ecf20Sopenharmony_ci if (err) 10108c2ecf20Sopenharmony_ci goto unlock; 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_ci ctx->merge = 0; 10138c2ecf20Sopenharmony_ci sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list); 10148c2ecf20Sopenharmony_ci 10158c2ecf20Sopenharmony_ci if (sgl->cur) 10168c2ecf20Sopenharmony_ci sg_unmark_end(sgl->sg + sgl->cur - 1); 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_ci sg_mark_end(sgl->sg + sgl->cur); 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci get_page(page); 10218c2ecf20Sopenharmony_ci sg_set_page(sgl->sg + sgl->cur, page, size, offset); 10228c2ecf20Sopenharmony_ci sgl->cur++; 10238c2ecf20Sopenharmony_ci ctx->used += size; 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_cidone: 10268c2ecf20Sopenharmony_ci ctx->more = flags & MSG_MORE; 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ciunlock: 10298c2ecf20Sopenharmony_ci af_alg_data_wakeup(sk); 10308c2ecf20Sopenharmony_ci release_sock(sk); 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci return err ?: size; 10338c2ecf20Sopenharmony_ci} 10348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_sendpage); 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci/** 10378c2ecf20Sopenharmony_ci * af_alg_free_resources - release resources required for crypto request 10388c2ecf20Sopenharmony_ci */ 10398c2ecf20Sopenharmony_civoid af_alg_free_resources(struct af_alg_async_req *areq) 10408c2ecf20Sopenharmony_ci{ 10418c2ecf20Sopenharmony_ci struct sock *sk = areq->sk; 10428c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx; 10438c2ecf20Sopenharmony_ci 10448c2ecf20Sopenharmony_ci af_alg_free_areq_sgls(areq); 10458c2ecf20Sopenharmony_ci sock_kfree_s(sk, areq, areq->areqlen); 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci ctx = alg_sk(sk)->private; 10488c2ecf20Sopenharmony_ci ctx->inflight = false; 10498c2ecf20Sopenharmony_ci} 10508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_free_resources); 10518c2ecf20Sopenharmony_ci 10528c2ecf20Sopenharmony_ci/** 10538c2ecf20Sopenharmony_ci * af_alg_async_cb - AIO callback handler 10548c2ecf20Sopenharmony_ci * 10558c2ecf20Sopenharmony_ci * This handler cleans up the struct af_alg_async_req upon completion of the 10568c2ecf20Sopenharmony_ci * AIO operation. 10578c2ecf20Sopenharmony_ci * 10588c2ecf20Sopenharmony_ci * The number of bytes to be generated with the AIO operation must be set 10598c2ecf20Sopenharmony_ci * in areq->outlen before the AIO callback handler is invoked. 10608c2ecf20Sopenharmony_ci */ 10618c2ecf20Sopenharmony_civoid af_alg_async_cb(struct crypto_async_request *_req, int err) 10628c2ecf20Sopenharmony_ci{ 10638c2ecf20Sopenharmony_ci struct af_alg_async_req *areq = _req->data; 10648c2ecf20Sopenharmony_ci struct sock *sk = areq->sk; 10658c2ecf20Sopenharmony_ci struct kiocb *iocb = areq->iocb; 10668c2ecf20Sopenharmony_ci unsigned int resultlen; 10678c2ecf20Sopenharmony_ci 10688c2ecf20Sopenharmony_ci /* Buffer size written by crypto operation. */ 10698c2ecf20Sopenharmony_ci resultlen = areq->outlen; 10708c2ecf20Sopenharmony_ci 10718c2ecf20Sopenharmony_ci af_alg_free_resources(areq); 10728c2ecf20Sopenharmony_ci sock_put(sk); 10738c2ecf20Sopenharmony_ci 10748c2ecf20Sopenharmony_ci iocb->ki_complete(iocb, err ? err : (int)resultlen, 0); 10758c2ecf20Sopenharmony_ci} 10768c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_async_cb); 10778c2ecf20Sopenharmony_ci 10788c2ecf20Sopenharmony_ci/** 10798c2ecf20Sopenharmony_ci * af_alg_poll - poll system call handler 10808c2ecf20Sopenharmony_ci */ 10818c2ecf20Sopenharmony_ci__poll_t af_alg_poll(struct file *file, struct socket *sock, 10828c2ecf20Sopenharmony_ci poll_table *wait) 10838c2ecf20Sopenharmony_ci{ 10848c2ecf20Sopenharmony_ci struct sock *sk = sock->sk; 10858c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 10868c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 10878c2ecf20Sopenharmony_ci __poll_t mask; 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_ci sock_poll_wait(file, sock, wait); 10908c2ecf20Sopenharmony_ci mask = 0; 10918c2ecf20Sopenharmony_ci 10928c2ecf20Sopenharmony_ci if (!ctx->more || ctx->used) 10938c2ecf20Sopenharmony_ci mask |= EPOLLIN | EPOLLRDNORM; 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci if (af_alg_writable(sk)) 10968c2ecf20Sopenharmony_ci mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; 10978c2ecf20Sopenharmony_ci 10988c2ecf20Sopenharmony_ci return mask; 10998c2ecf20Sopenharmony_ci} 11008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_poll); 11018c2ecf20Sopenharmony_ci 11028c2ecf20Sopenharmony_ci/** 11038c2ecf20Sopenharmony_ci * af_alg_alloc_areq - allocate struct af_alg_async_req 11048c2ecf20Sopenharmony_ci * 11058c2ecf20Sopenharmony_ci * @sk socket of connection to user space 11068c2ecf20Sopenharmony_ci * @areqlen size of struct af_alg_async_req + crypto_*_reqsize 11078c2ecf20Sopenharmony_ci * @return allocated data structure or ERR_PTR upon error 11088c2ecf20Sopenharmony_ci */ 11098c2ecf20Sopenharmony_cistruct af_alg_async_req *af_alg_alloc_areq(struct sock *sk, 11108c2ecf20Sopenharmony_ci unsigned int areqlen) 11118c2ecf20Sopenharmony_ci{ 11128c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = alg_sk(sk)->private; 11138c2ecf20Sopenharmony_ci struct af_alg_async_req *areq; 11148c2ecf20Sopenharmony_ci 11158c2ecf20Sopenharmony_ci /* Only one AIO request can be in flight. */ 11168c2ecf20Sopenharmony_ci if (ctx->inflight) 11178c2ecf20Sopenharmony_ci return ERR_PTR(-EBUSY); 11188c2ecf20Sopenharmony_ci 11198c2ecf20Sopenharmony_ci areq = sock_kmalloc(sk, areqlen, GFP_KERNEL); 11208c2ecf20Sopenharmony_ci if (unlikely(!areq)) 11218c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 11228c2ecf20Sopenharmony_ci 11238c2ecf20Sopenharmony_ci ctx->inflight = true; 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_ci areq->areqlen = areqlen; 11268c2ecf20Sopenharmony_ci areq->sk = sk; 11278c2ecf20Sopenharmony_ci areq->last_rsgl = NULL; 11288c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&areq->rsgl_list); 11298c2ecf20Sopenharmony_ci areq->tsgl = NULL; 11308c2ecf20Sopenharmony_ci areq->tsgl_entries = 0; 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci return areq; 11338c2ecf20Sopenharmony_ci} 11348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_alloc_areq); 11358c2ecf20Sopenharmony_ci 11368c2ecf20Sopenharmony_ci/** 11378c2ecf20Sopenharmony_ci * af_alg_get_rsgl - create the RX SGL for the output data from the crypto 11388c2ecf20Sopenharmony_ci * operation 11398c2ecf20Sopenharmony_ci * 11408c2ecf20Sopenharmony_ci * @sk socket of connection to user space 11418c2ecf20Sopenharmony_ci * @msg user space message 11428c2ecf20Sopenharmony_ci * @flags flags used to invoke recvmsg with 11438c2ecf20Sopenharmony_ci * @areq instance of the cryptographic request that will hold the RX SGL 11448c2ecf20Sopenharmony_ci * @maxsize maximum number of bytes to be pulled from user space 11458c2ecf20Sopenharmony_ci * @outlen number of bytes in the RX SGL 11468c2ecf20Sopenharmony_ci * @return 0 on success, < 0 upon error 11478c2ecf20Sopenharmony_ci */ 11488c2ecf20Sopenharmony_ciint af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags, 11498c2ecf20Sopenharmony_ci struct af_alg_async_req *areq, size_t maxsize, 11508c2ecf20Sopenharmony_ci size_t *outlen) 11518c2ecf20Sopenharmony_ci{ 11528c2ecf20Sopenharmony_ci struct alg_sock *ask = alg_sk(sk); 11538c2ecf20Sopenharmony_ci struct af_alg_ctx *ctx = ask->private; 11548c2ecf20Sopenharmony_ci size_t len = 0; 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci while (maxsize > len && msg_data_left(msg)) { 11578c2ecf20Sopenharmony_ci struct af_alg_rsgl *rsgl; 11588c2ecf20Sopenharmony_ci size_t seglen; 11598c2ecf20Sopenharmony_ci int err; 11608c2ecf20Sopenharmony_ci 11618c2ecf20Sopenharmony_ci /* limit the amount of readable buffers */ 11628c2ecf20Sopenharmony_ci if (!af_alg_readable(sk)) 11638c2ecf20Sopenharmony_ci break; 11648c2ecf20Sopenharmony_ci 11658c2ecf20Sopenharmony_ci seglen = min_t(size_t, (maxsize - len), 11668c2ecf20Sopenharmony_ci msg_data_left(msg)); 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ci if (list_empty(&areq->rsgl_list)) { 11698c2ecf20Sopenharmony_ci rsgl = &areq->first_rsgl; 11708c2ecf20Sopenharmony_ci } else { 11718c2ecf20Sopenharmony_ci rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL); 11728c2ecf20Sopenharmony_ci if (unlikely(!rsgl)) 11738c2ecf20Sopenharmony_ci return -ENOMEM; 11748c2ecf20Sopenharmony_ci } 11758c2ecf20Sopenharmony_ci 11768c2ecf20Sopenharmony_ci rsgl->sgl.npages = 0; 11778c2ecf20Sopenharmony_ci list_add_tail(&rsgl->list, &areq->rsgl_list); 11788c2ecf20Sopenharmony_ci 11798c2ecf20Sopenharmony_ci /* make one iovec available as scatterlist */ 11808c2ecf20Sopenharmony_ci err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen); 11818c2ecf20Sopenharmony_ci if (err < 0) { 11828c2ecf20Sopenharmony_ci rsgl->sg_num_bytes = 0; 11838c2ecf20Sopenharmony_ci return err; 11848c2ecf20Sopenharmony_ci } 11858c2ecf20Sopenharmony_ci 11868c2ecf20Sopenharmony_ci /* chain the new scatterlist with previous one */ 11878c2ecf20Sopenharmony_ci if (areq->last_rsgl) 11888c2ecf20Sopenharmony_ci af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl); 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci areq->last_rsgl = rsgl; 11918c2ecf20Sopenharmony_ci len += err; 11928c2ecf20Sopenharmony_ci atomic_add(err, &ctx->rcvused); 11938c2ecf20Sopenharmony_ci rsgl->sg_num_bytes = err; 11948c2ecf20Sopenharmony_ci iov_iter_advance(&msg->msg_iter, err); 11958c2ecf20Sopenharmony_ci } 11968c2ecf20Sopenharmony_ci 11978c2ecf20Sopenharmony_ci *outlen = len; 11988c2ecf20Sopenharmony_ci return 0; 11998c2ecf20Sopenharmony_ci} 12008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(af_alg_get_rsgl); 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_cistatic int __init af_alg_init(void) 12038c2ecf20Sopenharmony_ci{ 12048c2ecf20Sopenharmony_ci int err = proto_register(&alg_proto, 0); 12058c2ecf20Sopenharmony_ci 12068c2ecf20Sopenharmony_ci if (err) 12078c2ecf20Sopenharmony_ci goto out; 12088c2ecf20Sopenharmony_ci 12098c2ecf20Sopenharmony_ci err = sock_register(&alg_family); 12108c2ecf20Sopenharmony_ci if (err != 0) 12118c2ecf20Sopenharmony_ci goto out_unregister_proto; 12128c2ecf20Sopenharmony_ci 12138c2ecf20Sopenharmony_ciout: 12148c2ecf20Sopenharmony_ci return err; 12158c2ecf20Sopenharmony_ci 12168c2ecf20Sopenharmony_ciout_unregister_proto: 12178c2ecf20Sopenharmony_ci proto_unregister(&alg_proto); 12188c2ecf20Sopenharmony_ci goto out; 12198c2ecf20Sopenharmony_ci} 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_cistatic void __exit af_alg_exit(void) 12228c2ecf20Sopenharmony_ci{ 12238c2ecf20Sopenharmony_ci sock_unregister(PF_ALG); 12248c2ecf20Sopenharmony_ci proto_unregister(&alg_proto); 12258c2ecf20Sopenharmony_ci} 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_cimodule_init(af_alg_init); 12288c2ecf20Sopenharmony_cimodule_exit(af_alg_exit); 12298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 12308c2ecf20Sopenharmony_ciMODULE_ALIAS_NETPROTO(AF_ALG); 1231