Commit | Line | Data |
---|---|---|
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 |
24 | struct 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 |
37 | struct af_alg_control { |
38 | struct af_alg_iv *iv; | |
39 | int op; | |
af8e8073 | 40 | unsigned int aead_assoclen; |
03c8efc1 HX |
41 | }; |
42 | ||
43 | struct 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 | ||
58 | struct af_alg_sgl { | |
c1abe6f5 DH |
59 | struct sg_table sgt; |
60 | struct scatterlist sgl[ALG_MAX_PAGES + 1]; | |
f9e7a5fa | 61 | bool need_unpin; |
03c8efc1 HX |
62 | }; |
63 | ||
2d97591e SM |
64 | /* TX SGL entry */ |
65 | struct af_alg_tsgl { | |
66 | struct list_head list; | |
67 | unsigned int cur; /* Last processed SG entry */ | |
5a8a0765 | 68 | struct scatterlist sg[]; /* Array of SGs forming the SGL */ |
2d97591e SM |
69 | }; |
70 | ||
71 | #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \ | |
72 | sizeof(struct scatterlist) - 1) | |
73 | ||
74 | /* RX SGL entry */ | |
75 | struct af_alg_rsgl { | |
76 | struct af_alg_sgl sgl; | |
77 | struct list_head list; | |
78 | size_t sg_num_bytes; /* Bytes of data in that SGL */ | |
79 | }; | |
80 | ||
81 | /** | |
82 | * struct af_alg_async_req - definition of crypto request | |
83 | * @iocb: IOCB for AIO operations | |
84 | * @sk: Socket the request is associated with | |
85 | * @first_rsgl: First RX SG | |
86 | * @last_rsgl: Pointer to last RX SG | |
87 | * @rsgl_list: Track RX SGs | |
88 | * @tsgl: Private, per request TX SGL of buffers to process | |
89 | * @tsgl_entries: Number of entries in priv. TX SGL | |
90 | * @outlen: Number of output bytes generated by crypto op | |
91 | * @areqlen: Length of this data structure | |
92 | * @cra_u: Cipher request | |
93 | */ | |
94 | struct af_alg_async_req { | |
95 | struct kiocb *iocb; | |
96 | struct sock *sk; | |
97 | ||
98 | struct af_alg_rsgl first_rsgl; | |
99 | struct af_alg_rsgl *last_rsgl; | |
100 | struct list_head rsgl_list; | |
101 | ||
102 | struct scatterlist *tsgl; | |
103 | unsigned int tsgl_entries; | |
104 | ||
105 | unsigned int outlen; | |
106 | unsigned int areqlen; | |
107 | ||
108 | union { | |
109 | struct aead_request aead_req; | |
110 | struct skcipher_request skcipher_req; | |
111 | } cra_u; | |
112 | ||
113 | /* req ctx trails this struct */ | |
114 | }; | |
115 | ||
116 | /** | |
117 | * struct af_alg_ctx - definition of the crypto context | |
118 | * | |
119 | * The crypto context tracks the input data during the lifetime of an AF_ALG | |
120 | * socket. | |
121 | * | |
122 | * @tsgl_list: Link to TX SGL | |
123 | * @iv: IV for cipher operation | |
99bd99d3 | 124 | * @state: Existing state for continuing operation |
2d97591e SM |
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 | 139 | * @len: Length of memory allocated for this data structure. |
67b164a8 | 140 | * @inflight: Non-zero when AIO requests are in flight. |
2d97591e SM |
141 | */ |
142 | struct af_alg_ctx { | |
143 | struct list_head tsgl_list; | |
144 | ||
145 | void *iv; | |
99bd99d3 | 146 | void *state; |
2d97591e SM |
147 | size_t aead_assoclen; |
148 | ||
2c3f8b16 | 149 | struct crypto_wait wait; |
2d97591e SM |
150 | |
151 | size_t used; | |
af955bf1 | 152 | atomic_t rcvused; |
2d97591e SM |
153 | |
154 | bool more; | |
155 | bool merge; | |
156 | bool enc; | |
f3c802a1 | 157 | bool init; |
2d97591e SM |
158 | |
159 | unsigned int len; | |
67b164a8 HX |
160 | |
161 | unsigned int inflight; | |
2d97591e SM |
162 | }; |
163 | ||
03c8efc1 HX |
164 | int af_alg_register_type(const struct af_alg_type *type); |
165 | int af_alg_unregister_type(const struct af_alg_type *type); | |
166 | ||
167 | int af_alg_release(struct socket *sock); | |
c840ac6a | 168 | void af_alg_release_parent(struct sock *sk); |
cdfbabfb | 169 | int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern); |
03c8efc1 | 170 | |
03c8efc1 | 171 | void af_alg_free_sg(struct af_alg_sgl *sgl); |
03c8efc1 | 172 | |
03c8efc1 HX |
173 | static inline struct alg_sock *alg_sk(struct sock *sk) |
174 | { | |
175 | return (struct alg_sock *)sk; | |
176 | } | |
177 | ||
2d97591e SM |
178 | /** |
179 | * Size of available buffer for sending data from user space to kernel. | |
180 | * | |
181 | * @sk socket of connection to user space | |
182 | * @return number of bytes still available | |
183 | */ | |
184 | static inline int af_alg_sndbuf(struct sock *sk) | |
185 | { | |
186 | struct alg_sock *ask = alg_sk(sk); | |
187 | struct af_alg_ctx *ctx = ask->private; | |
188 | ||
189 | return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) - | |
190 | ctx->used, 0); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Can the send buffer still be written to? | |
195 | * | |
196 | * @sk socket of connection to user space | |
197 | * @return true => writable, false => not writable | |
198 | */ | |
199 | static inline bool af_alg_writable(struct sock *sk) | |
200 | { | |
201 | return PAGE_SIZE <= af_alg_sndbuf(sk); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Size of available buffer used by kernel for the RX user space operation. | |
206 | * | |
207 | * @sk socket of connection to user space | |
208 | * @return number of bytes still available | |
209 | */ | |
210 | static inline int af_alg_rcvbuf(struct sock *sk) | |
211 | { | |
212 | struct alg_sock *ask = alg_sk(sk); | |
213 | struct af_alg_ctx *ctx = ask->private; | |
214 | ||
215 | return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) - | |
af955bf1 | 216 | atomic_read(&ctx->rcvused), 0); |
2d97591e SM |
217 | } |
218 | ||
219 | /** | |
220 | * Can the RX buffer still be written to? | |
221 | * | |
222 | * @sk socket of connection to user space | |
223 | * @return true => writable, false => not writable | |
224 | */ | |
225 | static inline bool af_alg_readable(struct sock *sk) | |
226 | { | |
227 | return PAGE_SIZE <= af_alg_rcvbuf(sk); | |
228 | } | |
229 | ||
2d97591e SM |
230 | unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset); |
231 | void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst, | |
232 | size_t dst_offset); | |
2d97591e | 233 | void af_alg_wmem_wakeup(struct sock *sk); |
f3c802a1 | 234 | int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min); |
2d97591e SM |
235 | int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, |
236 | unsigned int ivsize); | |
7d2c3f54 | 237 | void af_alg_free_resources(struct af_alg_async_req *areq); |
255e48eb | 238 | void af_alg_async_cb(void *data, int err); |
a11e1d43 LT |
239 | __poll_t af_alg_poll(struct file *file, struct socket *sock, |
240 | poll_table *wait); | |
2d97591e SM |
241 | struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk, |
242 | unsigned int areqlen); | |
243 | int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags, | |
244 | struct af_alg_async_req *areq, size_t maxsize, | |
245 | size_t *outlen); | |
246 | ||
03c8efc1 | 247 | #endif /* _CRYPTO_IF_ALG_H */ |