Loading...
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
24struct crypto_async_request;
25
26struct 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 unsigned int refcnt;
33 unsigned int 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/* TX SGL entry */
66struct 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 */
76struct 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 */
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 /* 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 * @len: Length of memory allocated for this data structure.
139 */
140struct 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
155 unsigned int len;
156};
157
158int af_alg_register_type(const struct af_alg_type *type);
159int af_alg_unregister_type(const struct af_alg_type *type);
160
161int af_alg_release(struct socket *sock);
162void af_alg_release_parent(struct sock *sk);
163int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
164
165int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
166void af_alg_free_sg(struct af_alg_sgl *sgl);
167
168static 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 */
179static 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 */
194static 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 */
205static 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 */
220static inline bool af_alg_readable(struct sock *sk)
221{
222 return PAGE_SIZE <= af_alg_rcvbuf(sk);
223}
224
225unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
226void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
227 size_t dst_offset);
228void af_alg_wmem_wakeup(struct sock *sk);
229int af_alg_wait_for_data(struct sock *sk, unsigned flags);
230int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
231 unsigned int ivsize);
232ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
233 int offset, size_t size, int flags);
234void af_alg_free_resources(struct af_alg_async_req *areq);
235void af_alg_async_cb(struct crypto_async_request *_req, int err);
236__poll_t af_alg_poll(struct file *file, struct socket *sock,
237 poll_table *wait);
238struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
239 unsigned int areqlen);
240int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
241 struct af_alg_async_req *areq, size_t maxsize,
242 size_t *outlen);
243
244#endif /* _CRYPTO_IF_ALG_H */
1/*
2 * if_alg: User-space algorithm interface
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#ifndef _CRYPTO_IF_ALG_H
14#define _CRYPTO_IF_ALG_H
15
16#include <linux/compiler.h>
17#include <linux/completion.h>
18#include <linux/if_alg.h>
19#include <linux/scatterlist.h>
20#include <linux/types.h>
21#include <net/sock.h>
22
23#define ALG_MAX_PAGES 16
24
25struct crypto_async_request;
26
27struct alg_sock {
28 /* struct sock must be the first member of struct alg_sock */
29 struct sock sk;
30
31 struct sock *parent;
32
33 unsigned int refcnt;
34 unsigned int nokey_refcnt;
35
36 const struct af_alg_type *type;
37 void *private;
38};
39
40struct af_alg_completion {
41 struct completion completion;
42 int err;
43};
44
45struct af_alg_control {
46 struct af_alg_iv *iv;
47 int op;
48 unsigned int aead_assoclen;
49};
50
51struct af_alg_type {
52 void *(*bind)(const char *name, u32 type, u32 mask);
53 void (*release)(void *private);
54 int (*setkey)(void *private, const u8 *key, unsigned int keylen);
55 int (*accept)(void *private, struct sock *sk);
56 int (*accept_nokey)(void *private, struct sock *sk);
57 int (*setauthsize)(void *private, unsigned int authsize);
58
59 struct proto_ops *ops;
60 struct proto_ops *ops_nokey;
61 struct module *owner;
62 char name[14];
63};
64
65struct af_alg_sgl {
66 struct scatterlist sg[ALG_MAX_PAGES + 1];
67 struct page *pages[ALG_MAX_PAGES];
68 unsigned int npages;
69};
70
71int af_alg_register_type(const struct af_alg_type *type);
72int af_alg_unregister_type(const struct af_alg_type *type);
73
74int af_alg_release(struct socket *sock);
75void af_alg_release_parent(struct sock *sk);
76int af_alg_accept(struct sock *sk, struct socket *newsock);
77
78int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
79void af_alg_free_sg(struct af_alg_sgl *sgl);
80void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new);
81
82int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con);
83
84int af_alg_wait_for_completion(int err, struct af_alg_completion *completion);
85void af_alg_complete(struct crypto_async_request *req, int err);
86
87static inline struct alg_sock *alg_sk(struct sock *sk)
88{
89 return (struct alg_sock *)sk;
90}
91
92static inline void af_alg_init_completion(struct af_alg_completion *completion)
93{
94 init_completion(&completion->completion);
95}
96
97#endif /* _CRYPTO_IF_ALG_H */