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 (*accept)(void *private, struct sock *sk);
50 int (*accept_nokey)(void *private, struct sock *sk);
51 int (*setauthsize)(void *private, unsigned int authsize);
52
53 struct proto_ops *ops;
54 struct proto_ops *ops_nokey;
55 struct module *owner;
56 char name[14];
57};
58
59struct af_alg_sgl {
60 struct scatterlist sg[ALG_MAX_PAGES + 1];
61 struct page *pages[ALG_MAX_PAGES];
62 unsigned int npages;
63};
64
65
66struct af_alg_tsgl {
67 struct list_head list;
68 unsigned int cur;
69 struct scatterlist sg[];
70};
71
72#define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
73 sizeof(struct scatterlist) - 1)
74
75
76struct af_alg_rsgl {
77 struct af_alg_sgl sgl;
78 struct list_head list;
79 size_t sg_num_bytes;
80};
81
82
83
84
85
86
87
88
89
90
91
92
93
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
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
141struct af_alg_ctx {
142 struct list_head tsgl_list;
143
144 void *iv;
145 size_t aead_assoclen;
146
147 struct crypto_wait wait;
148
149 size_t used;
150 atomic_t rcvused;
151
152 bool more;
153 bool merge;
154 bool enc;
155 bool init;
156
157 unsigned int len;
158};
159
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);
164void af_alg_release_parent(struct sock *sk);
165int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
166
167int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
168void af_alg_free_sg(struct af_alg_sgl *sgl);
169
170static inline struct alg_sock *alg_sk(struct sock *sk)
171{
172 return (struct alg_sock *)sk;
173}
174
175
176
177
178
179
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
192
193
194
195
196static inline bool af_alg_writable(struct sock *sk)
197{
198 return PAGE_SIZE <= af_alg_sndbuf(sk);
199}
200
201
202
203
204
205
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) -
213 atomic_read(&ctx->rcvused), 0);
214}
215
216
217
218
219
220
221
222static inline bool af_alg_readable(struct sock *sk)
223{
224 return PAGE_SIZE <= af_alg_rcvbuf(sk);
225}
226
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);
230void af_alg_wmem_wakeup(struct sock *sk);
231int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
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);
236void af_alg_free_resources(struct af_alg_async_req *areq);
237void af_alg_async_cb(struct crypto_async_request *_req, int err);
238__poll_t af_alg_poll(struct file *file, struct socket *sock,
239 poll_table *wait);
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
246#endif
247