^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * af_alg: User-space algorithm interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file provides the user-space API for algorithms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/if_alg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct alg_type_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) const struct af_alg_type *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static atomic_long_t alg_memory_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct proto alg_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .name = "ALG",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .memory_allocated = &alg_memory_allocated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .obj_size = sizeof(struct alg_sock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static LIST_HEAD(alg_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static DECLARE_RWSEM(alg_types_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static const struct af_alg_type *alg_get_type(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) const struct af_alg_type *type = ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct alg_type_list *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) down_read(&alg_types_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) list_for_each_entry(node, &alg_types, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (strcmp(node->type->name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (try_module_get(node->type->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) type = node->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) up_read(&alg_types_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int af_alg_register_type(const struct af_alg_type *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct alg_type_list *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) down_write(&alg_types_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) list_for_each_entry(node, &alg_types, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!strcmp(node->type->name, type->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) node = kmalloc(sizeof(*node), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) type->ops->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (type->ops_nokey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) type->ops_nokey->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) node->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) list_add(&node->list, &alg_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) up_write(&alg_types_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) EXPORT_SYMBOL_GPL(af_alg_register_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int af_alg_unregister_type(const struct af_alg_type *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct alg_type_list *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) down_write(&alg_types_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) list_for_each_entry(node, &alg_types, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (strcmp(node->type->name, type->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) list_del(&node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) kfree(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) up_write(&alg_types_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) EXPORT_SYMBOL_GPL(af_alg_unregister_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static void alg_do_release(const struct af_alg_type *type, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) type->release(private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) module_put(type->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int af_alg_release(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (sock->sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) sock_put(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) sock->sk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) EXPORT_SYMBOL_GPL(af_alg_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) void af_alg_release_parent(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned int nokey = atomic_read(&ask->nokey_refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sk = ask->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (nokey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) atomic_dec(&ask->nokey_refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (atomic_dec_and_test(&ask->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) EXPORT_SYMBOL_GPL(af_alg_release_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct sockaddr_alg_new *sa = (void *)uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) const struct af_alg_type *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void *private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (sock->state == SS_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) BUILD_BUG_ON(offsetof(struct sockaddr_alg_new, salg_name) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) offsetof(struct sockaddr_alg, salg_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) BUILD_BUG_ON(offsetof(struct sockaddr_alg, salg_name) != sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (addr_len < sizeof(*sa) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* If caller uses non-allowed flag, return error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) sa->salg_name[addr_len - sizeof(*sa) - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) type = alg_get_type(sa->salg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (PTR_ERR(type) == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) request_module("algif-%s", sa->salg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) type = alg_get_type(sa->salg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (IS_ERR(type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return PTR_ERR(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (IS_ERR(private)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) module_put(type->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return PTR_ERR(private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (atomic_read(&ask->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) swap(ask->type, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) swap(ask->private, private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) alg_do_release(type, private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int alg_setkey(struct sock *sk, sockptr_t ukey, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) const struct af_alg_type *type = ask->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) u8 *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) key = sock_kmalloc(sk, keylen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (copy_from_sockptr(key, ukey, keylen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) err = type->setkey(ask->private, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) sock_kzfree_s(sk, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int alg_setsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) const struct af_alg_type *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (atomic_read(&ask->refcnt) != atomic_read(&ask->nokey_refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) type = ask->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (level != SOL_ALG || !type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) case ALG_SET_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (sock->state == SS_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!type->setkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) err = alg_setkey(sk, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) case ALG_SET_AEAD_AUTHSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (sock->state == SS_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!type->setauthsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) err = type->setauthsize(ask->private, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case ALG_SET_DRBG_ENTROPY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (sock->state == SS_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (!type->setentropy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) err = type->setentropy(ask->private, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) const struct af_alg_type *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct sock *sk2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) unsigned int nokey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) type = ask->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (!type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!sk2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) sock_init_data(newsock, sk2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) security_sock_graft(sk2, newsock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) security_sk_clone(sk, sk2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * newsock->ops assigned here to allow type->accept call to override
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * them when required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) newsock->ops = type->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) err = type->accept(ask->private, sk2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) nokey = err == -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (nokey && type->accept_nokey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) err = type->accept_nokey(ask->private, sk2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (atomic_inc_return_relaxed(&ask->refcnt) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (nokey) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) atomic_inc(&ask->nokey_refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) atomic_set(&alg_sk(sk2)->nokey_refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) alg_sk(sk2)->parent = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) alg_sk(sk2)->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) newsock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (nokey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) newsock->ops = type->ops_nokey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) EXPORT_SYMBOL_GPL(af_alg_accept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) bool kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return af_alg_accept(sock->sk, newsock, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static const struct proto_ops alg_proto_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .family = PF_ALG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) .connect = sock_no_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) .getname = sock_no_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .ioctl = sock_no_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) .shutdown = sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) .sendpage = sock_no_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) .sendmsg = sock_no_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) .recvmsg = sock_no_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) .bind = alg_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) .release = af_alg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) .setsockopt = alg_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) .accept = alg_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static void alg_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) alg_do_release(ask->type, ask->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int alg_create(struct net *net, struct socket *sock, int protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (sock->type != SOCK_SEQPACKET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return -ESOCKTNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (protocol != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) sock->ops = &alg_proto_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) sock_init_data(sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) sk->sk_destruct = alg_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static const struct net_proto_family alg_family = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .family = PF_ALG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .create = alg_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) size_t off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) ssize_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) int npages, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (n < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (WARN_ON(npages == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /* Add one extra for linking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) sg_init_table(sgl->sg, npages + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) for (i = 0, len = n; i < npages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int plen = min_t(int, len, PAGE_SIZE - off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) len -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) sg_mark_end(sgl->sg + npages - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) sgl->npages = npages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) EXPORT_SYMBOL_GPL(af_alg_make_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static void af_alg_link_sg(struct af_alg_sgl *sgl_prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct af_alg_sgl *sgl_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) void af_alg_free_sg(struct af_alg_sgl *sgl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) for (i = 0; i < sgl->npages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) put_page(sgl->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) EXPORT_SYMBOL_GPL(af_alg_free_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct cmsghdr *cmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) for_each_cmsghdr(cmsg, msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (!CMSG_OK(msg, cmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (cmsg->cmsg_level != SOL_ALG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) switch (cmsg->cmsg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) case ALG_SET_IV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) con->iv = (void *)CMSG_DATA(cmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) sizeof(*con->iv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) case ALG_SET_OP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) con->op = *(u32 *)CMSG_DATA(cmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) case ALG_SET_AEAD_ASSOCLEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * af_alg_alloc_tsgl - allocate the TX SGL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * @return: 0 upon success, < 0 upon error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static int af_alg_alloc_tsgl(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct af_alg_tsgl *sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct scatterlist *sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (!list_empty(&ctx->tsgl_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) sg = sgl->sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (!sg || sgl->cur >= MAX_SGL_ENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) sgl = sock_kmalloc(sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (!sgl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) sgl->cur = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) list_add_tail(&sgl->list, &ctx->tsgl_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * aead_count_tsgl - Count number of TX SG entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * The counting starts from the beginning of the SGL to @bytes. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * an offset is provided, the counting of the SG entries starts at the offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * @bytes Count the number of SG entries holding given number of bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * @offset Start the counting of SG entries from the given offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * @return Number of TX SG entries found given the constraints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) const struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) const struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) const struct af_alg_tsgl *sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) unsigned int sgl_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (!bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) list_for_each_entry(sgl, &ctx->tsgl_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) const struct scatterlist *sg = sgl->sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) for (i = 0; i < sgl->cur; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) size_t bytes_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /* Skip offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (offset >= sg[i].length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) offset -= sg[i].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) bytes -= sg[i].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) bytes_count = sg[i].length - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) sgl_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /* If we have seen requested number of bytes, stop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (bytes_count >= bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return sgl_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) bytes -= bytes_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return sgl_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * aead_pull_tsgl - Release the specified buffers from TX SGL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * If @dst is non-null, reassign the pages to dst. The caller must release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * the pages. If @dst_offset is given only reassign the pages to @dst starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * at the @dst_offset (byte). The caller must ensure that @dst is large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * enough (e.g. by using af_alg_count_tsgl with the same offset).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * @used Number of bytes to pull from TX SGL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * caller must release the buffers in dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * @dst_offset Reassign the TX SGL from given offset. All buffers before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * reaching the offset is released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) size_t dst_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct af_alg_tsgl *sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) unsigned int i, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) while (!list_empty(&ctx->tsgl_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) sg = sgl->sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) for (i = 0; i < sgl->cur; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) size_t plen = min_t(size_t, used, sg[i].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) struct page *page = sg_page(sg + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * Assumption: caller created af_alg_count_tsgl(len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * SG entries in dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (dst_offset >= plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /* discard page before offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) dst_offset -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /* reassign page to dst after offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) sg_set_page(dst + j, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) plen - dst_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) sg[i].offset + dst_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) dst_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) sg[i].length -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) sg[i].offset += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) used -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) ctx->used -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (sg[i].length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) sg_assign_page(sg + i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) list_del(&sgl->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) sock_kfree_s(sk, sgl, struct_size(sgl, sg, MAX_SGL_ENTS + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (!ctx->used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) ctx->merge = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) ctx->init = ctx->more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * @areq Request holding the TX and RX SGL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct sock *sk = areq->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) struct af_alg_rsgl *rsgl, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) struct scatterlist *tsgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) af_alg_free_sg(&rsgl->sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) list_del(&rsgl->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (rsgl != &areq->first_rsgl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) sock_kfree_s(sk, rsgl, sizeof(*rsgl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) tsgl = areq->tsgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (tsgl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (!sg_page(sg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) put_page(sg_page(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * af_alg_wait_for_wmem - wait for availability of writable memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * @flags If MSG_DONTWAIT is set, then only report if function would sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) * @return 0 when writable memory is available, < 0 upon error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) static int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) int err = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (flags & MSG_DONTWAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) timeout = MAX_SCHEDULE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * af_alg_wmem_wakeup - wakeup caller when writable memory is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) void af_alg_wmem_wakeup(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) struct socket_wq *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (!af_alg_writable(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) wq = rcu_dereference(sk->sk_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (skwq_has_sleeper(wq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) EPOLLRDNORM |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) EPOLLRDBAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) * af_alg_wait_for_data - wait for availability of TX data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * @flags If MSG_DONTWAIT is set, then only report if function would sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * @min Set to minimum request size if partial requests are allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * @return 0 when writable memory is available, < 0 upon error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) int err = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (flags & MSG_DONTWAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) timeout = MAX_SCHEDULE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (sk_wait_event(sk, &timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ctx->init && (!ctx->more ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) (min && ctx->used >= min)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) &wait)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) static void af_alg_data_wakeup(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct socket_wq *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (!ctx->used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) wq = rcu_dereference(sk->sk_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (skwq_has_sleeper(wq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) EPOLLRDNORM |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) EPOLLRDBAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) * af_alg_sendmsg - implementation of sendmsg system call handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * The sendmsg system call handler obtains the user data and stores it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * in ctx->tsgl_list. This implies allocation of the required numbers of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * struct af_alg_tsgl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * In addition, the ctx is filled with the information sent via CMSG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * @sock socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * @msg message from user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * @size size of message from user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * @ivsize the size of the IV for the cipher operation to verify that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * user-space-provided IV has the right size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * @return the number of copied data upon success, < 0 upon error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) unsigned int ivsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) struct af_alg_tsgl *sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) struct af_alg_control con = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) long copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) bool enc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) bool init = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (msg->msg_controllen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) err = af_alg_cmsg_send(msg, &con);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) init = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) switch (con.op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) case ALG_OP_ENCRYPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) enc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) case ALG_OP_DECRYPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) enc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (con.iv && con.iv->ivlen != ivsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (ctx->init && !ctx->more) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (ctx->used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) pr_info_once(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) "%s sent an empty control message without MSG_MORE.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) ctx->init = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) ctx->enc = enc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (con.iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) memcpy(ctx->iv, con.iv->iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) ctx->aead_assoclen = con.aead_assoclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) while (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) size_t len = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) size_t plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) /* use the existing memory in an allocated page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (ctx->merge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) sgl = list_entry(ctx->tsgl_list.prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) struct af_alg_tsgl, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) sg = sgl->sg + sgl->cur - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) len = min_t(size_t, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) PAGE_SIZE - sg->offset - sg->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) err = memcpy_from_msg(page_address(sg_page(sg)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) sg->offset + sg->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) sg->length += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) ctx->merge = (sg->offset + sg->length) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) ctx->used += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) copied += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) size -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (!af_alg_writable(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) err = af_alg_wait_for_wmem(sk, msg->msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) /* allocate a new page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) len = min_t(unsigned long, len, af_alg_sndbuf(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) err = af_alg_alloc_tsgl(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) sg = sgl->sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (sgl->cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) sg_unmark_end(sg + sgl->cur - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) unsigned int i = sgl->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) plen = min_t(size_t, len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) if (!sg_page(sg + i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) err = memcpy_from_msg(page_address(sg_page(sg + i)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) msg, plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) __free_page(sg_page(sg + i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) sg_assign_page(sg + i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) sg[i].length = plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) len -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) ctx->used += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) copied += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) size -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) sgl->cur++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) } while (len && sgl->cur < MAX_SGL_ENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) sg_mark_end(sg + sgl->cur - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) ctx->merge = plen & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) ctx->more = msg->msg_flags & MSG_MORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) af_alg_data_wakeup(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) return copied ?: err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) EXPORT_SYMBOL_GPL(af_alg_sendmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * af_alg_sendpage - sendpage system call handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * This is a generic implementation of sendpage to fill ctx->tsgl_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) int offset, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct af_alg_tsgl *sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (flags & MSG_SENDPAGE_NOTLAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) flags |= MSG_MORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (!ctx->more && ctx->used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) if (!af_alg_writable(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) err = af_alg_wait_for_wmem(sk, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) err = af_alg_alloc_tsgl(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ctx->merge = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) if (sgl->cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) sg_unmark_end(sgl->sg + sgl->cur - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) sg_mark_end(sgl->sg + sgl->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) sg_set_page(sgl->sg + sgl->cur, page, size, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) sgl->cur++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) ctx->used += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) ctx->more = flags & MSG_MORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) af_alg_data_wakeup(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) return err ?: size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) EXPORT_SYMBOL_GPL(af_alg_sendpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) * af_alg_free_resources - release resources required for crypto request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) void af_alg_free_resources(struct af_alg_async_req *areq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) struct sock *sk = areq->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) af_alg_free_areq_sgls(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) sock_kfree_s(sk, areq, areq->areqlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) EXPORT_SYMBOL_GPL(af_alg_free_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * af_alg_async_cb - AIO callback handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * This handler cleans up the struct af_alg_async_req upon completion of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * AIO operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * The number of bytes to be generated with the AIO operation must be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * in areq->outlen before the AIO callback handler is invoked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) void af_alg_async_cb(struct crypto_async_request *_req, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) struct af_alg_async_req *areq = _req->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) struct sock *sk = areq->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) struct kiocb *iocb = areq->iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) unsigned int resultlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) /* Buffer size written by crypto operation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) resultlen = areq->outlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) af_alg_free_resources(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) iocb->ki_complete(iocb, err ? err : (int)resultlen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) EXPORT_SYMBOL_GPL(af_alg_async_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * af_alg_poll - poll system call handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) __poll_t af_alg_poll(struct file *file, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) __poll_t mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) sock_poll_wait(file, sock, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) if (!ctx->more || ctx->used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) if (af_alg_writable(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) EXPORT_SYMBOL_GPL(af_alg_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * af_alg_alloc_areq - allocate struct af_alg_async_req
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) * @return allocated data structure or ERR_PTR upon error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) unsigned int areqlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (unlikely(!areq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) areq->areqlen = areqlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) areq->sk = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) areq->last_rsgl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) INIT_LIST_HEAD(&areq->rsgl_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) areq->tsgl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) areq->tsgl_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) return areq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) * operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) * @sk socket of connection to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) * @msg user space message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) * @flags flags used to invoke recvmsg with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) * @areq instance of the cryptographic request that will hold the RX SGL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) * @maxsize maximum number of bytes to be pulled from user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) * @outlen number of bytes in the RX SGL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) * @return 0 on success, < 0 upon error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) struct af_alg_async_req *areq, size_t maxsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) size_t *outlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) size_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) while (maxsize > len && msg_data_left(msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) struct af_alg_rsgl *rsgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) size_t seglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) /* limit the amount of readable buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (!af_alg_readable(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) seglen = min_t(size_t, (maxsize - len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) msg_data_left(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) if (list_empty(&areq->rsgl_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) rsgl = &areq->first_rsgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) if (unlikely(!rsgl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) rsgl->sgl.npages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) list_add_tail(&rsgl->list, &areq->rsgl_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) /* make one iovec available as scatterlist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) rsgl->sg_num_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) /* chain the new scatterlist with previous one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) if (areq->last_rsgl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) areq->last_rsgl = rsgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) len += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) atomic_add(err, &ctx->rcvused);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) rsgl->sg_num_bytes = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) iov_iter_advance(&msg->msg_iter, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) *outlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) static int __init af_alg_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) int err = proto_register(&alg_proto, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) err = sock_register(&alg_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) goto out_unregister_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) out_unregister_proto:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) proto_unregister(&alg_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) static void __exit af_alg_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) sock_unregister(PF_ALG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) proto_unregister(&alg_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) module_init(af_alg_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) module_exit(af_alg_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) MODULE_ALIAS_NETPROTO(AF_ALG);