Linux Audio

Check our new training course

Open-source upstreaming

Need help get the support for your hardware in upstream Linux?
Loading...
v6.8
  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 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
 37struct af_alg_control {
 38	struct af_alg_iv *iv;
 39	int op;
 40	unsigned int aead_assoclen;
 41};
 42
 43struct 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
 58struct 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 */
 65struct 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 */
 75struct 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 */
 94struct 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 * @state:		Existing state for continuing 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 */
142struct af_alg_ctx {
143	struct list_head tsgl_list;
144
145	void *iv;
146	void *state;
147	size_t aead_assoclen;
148
149	struct crypto_wait wait;
150
151	size_t used;
152	atomic_t rcvused;
153
154	bool more;
155	bool merge;
156	bool enc;
157	bool init;
158
159	unsigned int len;
160
161	unsigned int inflight;
162};
163
164int af_alg_register_type(const struct af_alg_type *type);
165int af_alg_unregister_type(const struct af_alg_type *type);
166
167int af_alg_release(struct socket *sock);
168void af_alg_release_parent(struct sock *sk);
169int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
170
 
171void af_alg_free_sg(struct af_alg_sgl *sgl);
 
 
 
172
173static 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 */
184static 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 */
199static 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 */
210static 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 */
225static inline bool af_alg_readable(struct sock *sk)
226{
227	return PAGE_SIZE <= af_alg_rcvbuf(sk);
228}
229
 
230unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
231void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
232		      size_t dst_offset);
 
 
233void af_alg_wmem_wakeup(struct sock *sk);
234int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
 
235int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
236		   unsigned int ivsize);
 
 
237void af_alg_free_resources(struct af_alg_async_req *areq);
238void af_alg_async_cb(void *data, 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	/* _CRYPTO_IF_ALG_H */
v4.17
 
  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 <linux/atomic.h>
 22#include <net/sock.h>
 23
 24#include <crypto/aead.h>
 25#include <crypto/skcipher.h>
 26
 27#define ALG_MAX_PAGES			16
 28
 29struct crypto_async_request;
 30
 31struct alg_sock {
 32	/* struct sock must be the first member of struct alg_sock */
 33	struct sock sk;
 34
 35	struct sock *parent;
 36
 37	unsigned int refcnt;
 38	unsigned int nokey_refcnt;
 39
 40	const struct af_alg_type *type;
 41	void *private;
 42};
 43
 44struct af_alg_control {
 45	struct af_alg_iv *iv;
 46	int op;
 47	unsigned int aead_assoclen;
 48};
 49
 50struct af_alg_type {
 51	void *(*bind)(const char *name, u32 type, u32 mask);
 52	void (*release)(void *private);
 53	int (*setkey)(void *private, const u8 *key, unsigned int keylen);
 
 54	int (*accept)(void *private, struct sock *sk);
 55	int (*accept_nokey)(void *private, struct sock *sk);
 56	int (*setauthsize)(void *private, unsigned int authsize);
 57
 58	struct proto_ops *ops;
 59	struct proto_ops *ops_nokey;
 60	struct module *owner;
 61	char name[14];
 62};
 63
 64struct af_alg_sgl {
 65	struct scatterlist sg[ALG_MAX_PAGES + 1];
 66	struct page *pages[ALG_MAX_PAGES];
 67	unsigned int npages;
 68};
 69
 70/* TX SGL entry */
 71struct af_alg_tsgl {
 72	struct list_head list;
 73	unsigned int cur;		/* Last processed SG entry */
 74	struct scatterlist sg[0];	/* Array of SGs forming the SGL */
 75};
 76
 77#define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
 78		      sizeof(struct scatterlist) - 1)
 79
 80/* RX SGL entry */
 81struct af_alg_rsgl {
 82	struct af_alg_sgl sgl;
 83	struct list_head list;
 84	size_t sg_num_bytes;		/* Bytes of data in that SGL */
 85};
 86
 87/**
 88 * struct af_alg_async_req - definition of crypto request
 89 * @iocb:		IOCB for AIO operations
 90 * @sk:			Socket the request is associated with
 91 * @first_rsgl:		First RX SG
 92 * @last_rsgl:		Pointer to last RX SG
 93 * @rsgl_list:		Track RX SGs
 94 * @tsgl:		Private, per request TX SGL of buffers to process
 95 * @tsgl_entries:	Number of entries in priv. TX SGL
 96 * @outlen:		Number of output bytes generated by crypto op
 97 * @areqlen:		Length of this data structure
 98 * @cra_u:		Cipher request
 99 */
100struct af_alg_async_req {
101	struct kiocb *iocb;
102	struct sock *sk;
103
104	struct af_alg_rsgl first_rsgl;
105	struct af_alg_rsgl *last_rsgl;
106	struct list_head rsgl_list;
107
108	struct scatterlist *tsgl;
109	unsigned int tsgl_entries;
110
111	unsigned int outlen;
112	unsigned int areqlen;
113
114	union {
115		struct aead_request aead_req;
116		struct skcipher_request skcipher_req;
117	} cra_u;
118
119	/* req ctx trails this struct */
120};
121
122/**
123 * struct af_alg_ctx - definition of the crypto context
124 *
125 * The crypto context tracks the input data during the lifetime of an AF_ALG
126 * socket.
127 *
128 * @tsgl_list:		Link to TX SGL
129 * @iv:			IV for cipher operation
 
130 * @aead_assoclen:	Length of AAD for AEAD cipher operations
131 * @completion:		Work queue for synchronous operation
132 * @used:		TX bytes sent to kernel. This variable is used to
133 *			ensure that user space cannot cause the kernel
134 *			to allocate too much memory in sendmsg operation.
135 * @rcvused:		Total RX bytes to be filled by kernel. This variable
136 *			is used to ensure user space cannot cause the kernel
137 *			to allocate too much memory in a recvmsg operation.
138 * @more:		More data to be expected from user space?
139 * @merge:		Shall new data from user space be merged into existing
140 *			SG?
141 * @enc:		Cryptographic operation to be performed when
142 *			recvmsg is invoked.
 
143 * @len:		Length of memory allocated for this data structure.
 
144 */
145struct af_alg_ctx {
146	struct list_head tsgl_list;
147
148	void *iv;
 
149	size_t aead_assoclen;
150
151	struct crypto_wait wait;
152
153	size_t used;
154	atomic_t rcvused;
155
156	bool more;
157	bool merge;
158	bool enc;
 
159
160	unsigned int len;
 
 
161};
162
163int af_alg_register_type(const struct af_alg_type *type);
164int af_alg_unregister_type(const struct af_alg_type *type);
165
166int af_alg_release(struct socket *sock);
167void af_alg_release_parent(struct sock *sk);
168int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
169
170int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
171void af_alg_free_sg(struct af_alg_sgl *sgl);
172void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new);
173
174int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con);
175
176static inline struct alg_sock *alg_sk(struct sock *sk)
177{
178	return (struct alg_sock *)sk;
179}
180
181/**
182 * Size of available buffer for sending data from user space to kernel.
183 *
184 * @sk socket of connection to user space
185 * @return number of bytes still available
186 */
187static inline int af_alg_sndbuf(struct sock *sk)
188{
189	struct alg_sock *ask = alg_sk(sk);
190	struct af_alg_ctx *ctx = ask->private;
191
192	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
193			  ctx->used, 0);
194}
195
196/**
197 * Can the send buffer still be written to?
198 *
199 * @sk socket of connection to user space
200 * @return true => writable, false => not writable
201 */
202static inline bool af_alg_writable(struct sock *sk)
203{
204	return PAGE_SIZE <= af_alg_sndbuf(sk);
205}
206
207/**
208 * Size of available buffer used by kernel for the RX user space operation.
209 *
210 * @sk socket of connection to user space
211 * @return number of bytes still available
212 */
213static inline int af_alg_rcvbuf(struct sock *sk)
214{
215	struct alg_sock *ask = alg_sk(sk);
216	struct af_alg_ctx *ctx = ask->private;
217
218	return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
219		     atomic_read(&ctx->rcvused), 0);
220}
221
222/**
223 * Can the RX buffer still be written to?
224 *
225 * @sk socket of connection to user space
226 * @return true => writable, false => not writable
227 */
228static inline bool af_alg_readable(struct sock *sk)
229{
230	return PAGE_SIZE <= af_alg_rcvbuf(sk);
231}
232
233int af_alg_alloc_tsgl(struct sock *sk);
234unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
235void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
236		      size_t dst_offset);
237void af_alg_free_areq_sgls(struct af_alg_async_req *areq);
238int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags);
239void af_alg_wmem_wakeup(struct sock *sk);
240int af_alg_wait_for_data(struct sock *sk, unsigned flags);
241void af_alg_data_wakeup(struct sock *sk);
242int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
243		   unsigned int ivsize);
244ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
245			int offset, size_t size, int flags);
246void af_alg_free_resources(struct af_alg_async_req *areq);
247void af_alg_async_cb(struct crypto_async_request *_req, int err);
248__poll_t af_alg_poll(struct file *file, struct socket *sock,
249			 poll_table *wait);
250struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
251					   unsigned int areqlen);
252int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
253		    struct af_alg_async_req *areq, size_t maxsize,
254		    size_t *outlen);
255
256#endif	/* _CRYPTO_IF_ALG_H */