crypto: af_alg: Pin pages rather than ref'ing if appropriate
[linux-2.6-block.git] / include / crypto / if_alg.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
03c8efc1
HX
2/*
3 * if_alg: User-space algorithm interface
4 *
5 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
03c8efc1
HX
6 */
7
8#ifndef _CRYPTO_IF_ALG_H
9#define _CRYPTO_IF_ALG_H
10
11#include <linux/compiler.h>
12#include <linux/completion.h>
13#include <linux/if_alg.h>
b7f080cf 14#include <linux/scatterlist.h>
03c8efc1 15#include <linux/types.h>
af955bf1 16#include <linux/atomic.h>
03c8efc1
HX
17#include <net/sock.h>
18
2d97591e
SM
19#include <crypto/aead.h>
20#include <crypto/skcipher.h>
21
03c8efc1
HX
22#define ALG_MAX_PAGES 16
23
03c8efc1
HX
24struct alg_sock {
25 /* struct sock must be the first member of struct alg_sock */
26 struct sock sk;
27
28 struct sock *parent;
29
34c86f4c
HX
30 atomic_t refcnt;
31 atomic_t nokey_refcnt;
c840ac6a 32
03c8efc1
HX
33 const struct af_alg_type *type;
34 void *private;
35};
36
03c8efc1
HX
37struct af_alg_control {
38 struct af_alg_iv *iv;
39 int op;
af8e8073 40 unsigned int aead_assoclen;
03c8efc1
HX
41};
42
43struct af_alg_type {
44 void *(*bind)(const char *name, u32 type, u32 mask);
45 void (*release)(void *private);
46 int (*setkey)(void *private, const u8 *key, unsigned int keylen);
77ebdabe 47 int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
03c8efc1 48 int (*accept)(void *private, struct sock *sk);
37766586 49 int (*accept_nokey)(void *private, struct sock *sk);
25fb8638 50 int (*setauthsize)(void *private, unsigned int authsize);
03c8efc1
HX
51
52 struct proto_ops *ops;
37766586 53 struct proto_ops *ops_nokey;
03c8efc1
HX
54 struct module *owner;
55 char name[14];
56};
57
58struct af_alg_sgl {
66db3739 59 struct scatterlist sg[ALG_MAX_PAGES + 1];
03c8efc1 60 struct page *pages[ALG_MAX_PAGES];
66db3739 61 unsigned int npages;
f9e7a5fa 62 bool need_unpin;
03c8efc1
HX
63};
64
2d97591e
SM
65/* TX SGL entry */
66struct af_alg_tsgl {
67 struct list_head list;
68 unsigned int cur; /* Last processed SG entry */
5a8a0765 69 struct scatterlist sg[]; /* Array of SGs forming the SGL */
2d97591e
SM
70};
71
72#define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
73 sizeof(struct scatterlist) - 1)
74
75/* RX SGL entry */
76struct af_alg_rsgl {
77 struct af_alg_sgl sgl;
78 struct list_head list;
79 size_t sg_num_bytes; /* Bytes of data in that SGL */
80};
81
82/**
83 * struct af_alg_async_req - definition of crypto request
84 * @iocb: IOCB for AIO operations
85 * @sk: Socket the request is associated with
86 * @first_rsgl: First RX SG
87 * @last_rsgl: Pointer to last RX SG
88 * @rsgl_list: Track RX SGs
89 * @tsgl: Private, per request TX SGL of buffers to process
90 * @tsgl_entries: Number of entries in priv. TX SGL
91 * @outlen: Number of output bytes generated by crypto op
92 * @areqlen: Length of this data structure
93 * @cra_u: Cipher request
94 */
95struct af_alg_async_req {
96 struct kiocb *iocb;
97 struct sock *sk;
98
99 struct af_alg_rsgl first_rsgl;
100 struct af_alg_rsgl *last_rsgl;
101 struct list_head rsgl_list;
102
103 struct scatterlist *tsgl;
104 unsigned int tsgl_entries;
105
106 unsigned int outlen;
107 unsigned int areqlen;
108
109 union {
110 struct aead_request aead_req;
111 struct skcipher_request skcipher_req;
112 } cra_u;
113
114 /* req ctx trails this struct */
115};
116
117/**
118 * struct af_alg_ctx - definition of the crypto context
119 *
120 * The crypto context tracks the input data during the lifetime of an AF_ALG
121 * socket.
122 *
123 * @tsgl_list: Link to TX SGL
124 * @iv: IV for cipher operation
125 * @aead_assoclen: Length of AAD for AEAD cipher operations
126 * @completion: Work queue for synchronous operation
127 * @used: TX bytes sent to kernel. This variable is used to
128 * ensure that user space cannot cause the kernel
129 * to allocate too much memory in sendmsg operation.
130 * @rcvused: Total RX bytes to be filled by kernel. This variable
131 * is used to ensure user space cannot cause the kernel
132 * to allocate too much memory in a recvmsg operation.
133 * @more: More data to be expected from user space?
134 * @merge: Shall new data from user space be merged into existing
135 * SG?
136 * @enc: Cryptographic operation to be performed when
137 * recvmsg is invoked.
f3c802a1 138 * @init: True if metadata has been sent.
2d97591e
SM
139 * @len: Length of memory allocated for this data structure.
140 */
141struct af_alg_ctx {
142 struct list_head tsgl_list;
143
144 void *iv;
145 size_t aead_assoclen;
146
2c3f8b16 147 struct crypto_wait wait;
2d97591e
SM
148
149 size_t used;
af955bf1 150 atomic_t rcvused;
2d97591e
SM
151
152 bool more;
153 bool merge;
154 bool enc;
f3c802a1 155 bool init;
2d97591e
SM
156
157 unsigned int len;
158};
159
03c8efc1
HX
160int af_alg_register_type(const struct af_alg_type *type);
161int af_alg_unregister_type(const struct af_alg_type *type);
162
163int af_alg_release(struct socket *sock);
c840ac6a 164void af_alg_release_parent(struct sock *sk);
cdfbabfb 165int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
03c8efc1 166
1d10eb2f 167int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
03c8efc1 168void af_alg_free_sg(struct af_alg_sgl *sgl);
03c8efc1 169
03c8efc1
HX
170static inline struct alg_sock *alg_sk(struct sock *sk)
171{
172 return (struct alg_sock *)sk;
173}
174
2d97591e
SM
175/**
176 * Size of available buffer for sending data from user space to kernel.
177 *
178 * @sk socket of connection to user space
179 * @return number of bytes still available
180 */
181static inline int af_alg_sndbuf(struct sock *sk)
182{
183 struct alg_sock *ask = alg_sk(sk);
184 struct af_alg_ctx *ctx = ask->private;
185
186 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
187 ctx->used, 0);
188}
189
190/**
191 * Can the send buffer still be written to?
192 *
193 * @sk socket of connection to user space
194 * @return true => writable, false => not writable
195 */
196static inline bool af_alg_writable(struct sock *sk)
197{
198 return PAGE_SIZE <= af_alg_sndbuf(sk);
199}
200
201/**
202 * Size of available buffer used by kernel for the RX user space operation.
203 *
204 * @sk socket of connection to user space
205 * @return number of bytes still available
206 */
207static inline int af_alg_rcvbuf(struct sock *sk)
208{
209 struct alg_sock *ask = alg_sk(sk);
210 struct af_alg_ctx *ctx = ask->private;
211
212 return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
af955bf1 213 atomic_read(&ctx->rcvused), 0);
2d97591e
SM
214}
215
216/**
217 * Can the RX buffer still be written to?
218 *
219 * @sk socket of connection to user space
220 * @return true => writable, false => not writable
221 */
222static inline bool af_alg_readable(struct sock *sk)
223{
224 return PAGE_SIZE <= af_alg_rcvbuf(sk);
225}
226
2d97591e
SM
227unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
228void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
229 size_t dst_offset);
2d97591e 230void af_alg_wmem_wakeup(struct sock *sk);
f3c802a1 231int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
2d97591e
SM
232int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
233 unsigned int ivsize);
234ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
235 int offset, size_t size, int flags);
7d2c3f54 236void af_alg_free_resources(struct af_alg_async_req *areq);
255e48eb 237void af_alg_async_cb(void *data, int err);
a11e1d43
LT
238__poll_t af_alg_poll(struct file *file, struct socket *sock,
239 poll_table *wait);
2d97591e
SM
240struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
241 unsigned int areqlen);
242int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
243 struct af_alg_async_req *areq, size_t maxsize,
244 size_t *outlen);
245
03c8efc1 246#endif /* _CRYPTO_IF_ALG_H */