1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * if_alg: User-space algorithm interface
4  *
5  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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 
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 
30 	atomic_t refcnt;
31 	atomic_t nokey_refcnt;
32 
33 	const struct af_alg_type *type;
34 	void *private;
35 };
36 
37 struct af_alg_control {
38 	struct af_alg_iv *iv;
39 	int op;
40 	unsigned int aead_assoclen;
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);
47 	int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
48 	int (*accept)(void *private, struct sock *sk);
49 	int (*accept_nokey)(void *private, struct sock *sk);
50 	int (*setauthsize)(void *private, unsigned int authsize);
51 
52 	struct proto_ops *ops;
53 	struct proto_ops *ops_nokey;
54 	struct module *owner;
55 	char name[14];
56 };
57 
58 struct af_alg_sgl {
59 	struct sg_table sgt;
60 	struct scatterlist sgl[ALG_MAX_PAGES + 1];
61 	bool need_unpin;
62 };
63 
64 /* TX SGL entry */
65 struct af_alg_tsgl {
66 	struct list_head list;
67 	unsigned int cur;		/* Last processed SG entry */
68 	struct scatterlist sg[];	/* Array of SGs forming the SGL */
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
124  * @aead_assoclen:	Length of AAD for AEAD cipher operations
125  * @completion:		Work queue for synchronous operation
126  * @used:		TX bytes sent to kernel. This variable is used to
127  *			ensure that user space cannot cause the kernel
128  *			to allocate too much memory in sendmsg operation.
129  * @rcvused:		Total RX bytes to be filled by kernel. This variable
130  *			is used to ensure user space cannot cause the kernel
131  *			to allocate too much memory in a recvmsg operation.
132  * @more:		More data to be expected from user space?
133  * @merge:		Shall new data from user space be merged into existing
134  *			SG?
135  * @enc:		Cryptographic operation to be performed when
136  *			recvmsg is invoked.
137  * @init:		True if metadata has been sent.
138  * @len:		Length of memory allocated for this data structure.
139  */
140 struct af_alg_ctx {
141 	struct list_head tsgl_list;
142 
143 	void *iv;
144 	size_t aead_assoclen;
145 
146 	struct crypto_wait wait;
147 
148 	size_t used;
149 	atomic_t rcvused;
150 
151 	bool more;
152 	bool merge;
153 	bool enc;
154 	bool init;
155 
156 	unsigned int len;
157 };
158 
159 int af_alg_register_type(const struct af_alg_type *type);
160 int af_alg_unregister_type(const struct af_alg_type *type);
161 
162 int af_alg_release(struct socket *sock);
163 void af_alg_release_parent(struct sock *sk);
164 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
165 
166 void af_alg_free_sg(struct af_alg_sgl *sgl);
167 
alg_sk(struct sock * sk)168 static inline struct alg_sock *alg_sk(struct sock *sk)
169 {
170 	return (struct alg_sock *)sk;
171 }
172 
173 /**
174  * Size of available buffer for sending data from user space to kernel.
175  *
176  * @sk socket of connection to user space
177  * @return number of bytes still available
178  */
af_alg_sndbuf(struct sock * sk)179 static inline int af_alg_sndbuf(struct sock *sk)
180 {
181 	struct alg_sock *ask = alg_sk(sk);
182 	struct af_alg_ctx *ctx = ask->private;
183 
184 	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
185 			  ctx->used, 0);
186 }
187 
188 /**
189  * Can the send buffer still be written to?
190  *
191  * @sk socket of connection to user space
192  * @return true => writable, false => not writable
193  */
af_alg_writable(struct sock * sk)194 static inline bool af_alg_writable(struct sock *sk)
195 {
196 	return PAGE_SIZE <= af_alg_sndbuf(sk);
197 }
198 
199 /**
200  * Size of available buffer used by kernel for the RX user space operation.
201  *
202  * @sk socket of connection to user space
203  * @return number of bytes still available
204  */
af_alg_rcvbuf(struct sock * sk)205 static inline int af_alg_rcvbuf(struct sock *sk)
206 {
207 	struct alg_sock *ask = alg_sk(sk);
208 	struct af_alg_ctx *ctx = ask->private;
209 
210 	return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
211 		     atomic_read(&ctx->rcvused), 0);
212 }
213 
214 /**
215  * Can the RX buffer still be written to?
216  *
217  * @sk socket of connection to user space
218  * @return true => writable, false => not writable
219  */
af_alg_readable(struct sock * sk)220 static inline bool af_alg_readable(struct sock *sk)
221 {
222 	return PAGE_SIZE <= af_alg_rcvbuf(sk);
223 }
224 
225 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
226 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
227 		      size_t dst_offset);
228 void af_alg_wmem_wakeup(struct sock *sk);
229 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
230 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
231 		   unsigned int ivsize);
232 void af_alg_free_resources(struct af_alg_async_req *areq);
233 void af_alg_async_cb(void *data, int err);
234 __poll_t af_alg_poll(struct file *file, struct socket *sock,
235 			 poll_table *wait);
236 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
237 					   unsigned int areqlen);
238 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
239 		    struct af_alg_async_req *areq, size_t maxsize,
240 		    size_t *outlen);
241 
242 #endif	/* _CRYPTO_IF_ALG_H */
243