1
2
3
4
5
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>
14#include <linux/scatterlist.h>
15#include <linux/types.h>
16#include <linux/atomic.h>
17#include <net/sock.h>
18
19#include <crypto/aead.h>
20#include <crypto/skcipher.h>
21
22#define ALG_MAX_PAGES 16
23
24struct crypto_async_request;
25
26struct alg_sock {
27
28 struct sock sk;
29
30 struct sock *parent;
31
32 atomic_t refcnt;
33 atomic_t nokey_refcnt;
34
35 const struct af_alg_type *type;
36 void *private;
37};
38
39struct af_alg_control {
40 struct af_alg_iv *iv;
41 int op;
42 unsigned int aead_assoclen;
43};
44
45struct af_alg_type {
46 void *(*bind)(const char *name, u32 type, u32 mask);
47 void (*release)(void *private);
48 int (*setkey)(void *private, const u8 *key, unsigned int keylen);
49 int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
50 int (*accept)(void *private, struct sock *sk);
51 int (*accept_nokey)(void *private, struct sock *sk);
52 int (*setauthsize)(void *private, unsigned int authsize);
53
54 struct proto_ops *ops;
55 struct proto_ops *ops_nokey;
56 struct module *owner;
57 char name[14];
58};
59
60struct af_alg_sgl {
61 struct scatterlist sg[ALG_MAX_PAGES + 1];
62 struct page *pages[ALG_MAX_PAGES];
63 unsigned int npages;
64};
65
66
67struct af_alg_tsgl {
68 struct list_head list;
69 unsigned int cur;
70 struct scatterlist sg[];
71};
72
73#define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
74 sizeof(struct scatterlist) - 1)
75
76
77struct af_alg_rsgl {
78 struct af_alg_sgl sgl;
79 struct list_head list;
80 size_t sg_num_bytes;
81};
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96struct af_alg_async_req {
97 struct kiocb *iocb;
98 struct sock *sk;
99
100 struct af_alg_rsgl first_rsgl;
101 struct af_alg_rsgl *last_rsgl;
102 struct list_head rsgl_list;
103
104 struct scatterlist *tsgl;
105 unsigned int tsgl_entries;
106
107 unsigned int outlen;
108 unsigned int areqlen;
109
110 union {
111 struct aead_request aead_req;
112 struct skcipher_request skcipher_req;
113 } cra_u;
114
115
116};
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142struct af_alg_ctx {
143 struct list_head tsgl_list;
144
145 void *iv;
146 size_t aead_assoclen;
147
148 struct crypto_wait wait;
149
150 size_t used;
151 atomic_t rcvused;
152
153 bool more;
154 bool merge;
155 bool enc;
156 bool init;
157
158 unsigned int len;
159};
160
161int af_alg_register_type(const struct af_alg_type *type);
162int af_alg_unregister_type(const struct af_alg_type *type);
163
164int af_alg_release(struct socket *sock);
165void af_alg_release_parent(struct sock *sk);
166int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
167
168int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
169void af_alg_free_sg(struct af_alg_sgl *sgl);
170
171static inline struct alg_sock *alg_sk(struct sock *sk)
172{
173 return (struct alg_sock *)sk;
174}
175
176
177
178
179
180
181
182static inline int af_alg_sndbuf(struct sock *sk)
183{
184 struct alg_sock *ask = alg_sk(sk);
185 struct af_alg_ctx *ctx = ask->private;
186
187 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
188 ctx->used, 0);
189}
190
191
192
193
194
195
196
197static inline bool af_alg_writable(struct sock *sk)
198{
199 return PAGE_SIZE <= af_alg_sndbuf(sk);
200}
201
202
203
204
205
206
207
208static inline int af_alg_rcvbuf(struct sock *sk)
209{
210 struct alg_sock *ask = alg_sk(sk);
211 struct af_alg_ctx *ctx = ask->private;
212
213 return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
214 atomic_read(&ctx->rcvused), 0);
215}
216
217
218
219
220
221
222
223static inline bool af_alg_readable(struct sock *sk)
224{
225 return PAGE_SIZE <= af_alg_rcvbuf(sk);
226}
227
228unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
229void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
230 size_t dst_offset);
231void af_alg_wmem_wakeup(struct sock *sk);
232int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
233int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
234 unsigned int ivsize);
235ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
236 int offset, size_t size, int flags);
237void af_alg_free_resources(struct af_alg_async_req *areq);
238void af_alg_async_cb(struct crypto_async_request *_req, int err);
239__poll_t af_alg_poll(struct file *file, struct socket *sock,
240 poll_table *wait);
241struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
242 unsigned int areqlen);
243int 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
247#endif
248