• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 crypto_async_request;
25 
26 struct alg_sock {
27 	/* struct sock must be the first member of struct alg_sock */
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 
39 struct af_alg_control {
40 	struct af_alg_iv *iv;
41 	int op;
42 	unsigned int aead_assoclen;
43 };
44 
45 struct 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 
59 struct 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 /* TX SGL entry */
66 struct af_alg_tsgl {
67 	struct list_head list;
68 	unsigned int cur;		/* Last processed SG entry */
69 	struct scatterlist sg[0];	/* Array of SGs forming the SGL */
70 };
71 
72 #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
73 		      sizeof(struct scatterlist) - 1)
74 
75 /* RX SGL entry */
76 struct 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  */
95 struct 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.
138  * @init:		True if metadata has been sent.
139  * @len:		Length of memory allocated for this data structure.
140  * @inflight:		Non-zero when AIO requests are in flight.
141  */
142 struct 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 	unsigned int inflight;
161 };
162 
163 int af_alg_register_type(const struct af_alg_type *type);
164 int af_alg_unregister_type(const struct af_alg_type *type);
165 
166 int af_alg_release(struct socket *sock);
167 void af_alg_release_parent(struct sock *sk);
168 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
169 
170 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
171 void af_alg_free_sg(struct af_alg_sgl *sgl);
172 
alg_sk(struct sock * sk)173 static inline struct alg_sock *alg_sk(struct sock *sk)
174 {
175 	return (struct alg_sock *)sk;
176 }
177 
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  */
af_alg_sndbuf(struct sock * sk)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  */
af_alg_writable(struct sock * sk)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  */
af_alg_rcvbuf(struct sock * sk)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) -
216 		     atomic_read(&ctx->rcvused), 0);
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  */
af_alg_readable(struct sock * sk)225 static inline bool af_alg_readable(struct sock *sk)
226 {
227 	return PAGE_SIZE <= af_alg_rcvbuf(sk);
228 }
229 
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);
233 void af_alg_wmem_wakeup(struct sock *sk);
234 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
235 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
236 		   unsigned int ivsize);
237 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
238 			int offset, size_t size, int flags);
239 void af_alg_free_resources(struct af_alg_async_req *areq);
240 void af_alg_async_cb(struct crypto_async_request *_req, int err);
241 __poll_t af_alg_poll(struct file *file, struct socket *sock,
242 			 poll_table *wait);
243 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
244 					   unsigned int areqlen);
245 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
246 		    struct af_alg_async_req *areq, size_t maxsize,
247 		    size_t *outlen);
248 
249 #endif	/* _CRYPTO_IF_ALG_H */
250