2 * if_alg: User-space algorithm interface
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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)
13 #ifndef _CRYPTO_IF_ALG_H
14 #define _CRYPTO_IF_ALG_H
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>
24 #include <crypto/aead.h>
25 #include <crypto/skcipher.h>
27 #define ALG_MAX_PAGES 16
29 struct crypto_async_request
;
32 /* struct sock must be the first member of struct alg_sock */
38 unsigned int nokey_refcnt
;
40 const struct af_alg_type
*type
;
44 struct af_alg_control
{
47 unsigned int aead_assoclen
;
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
);
58 struct proto_ops
*ops
;
59 struct proto_ops
*ops_nokey
;
65 struct scatterlist sg
[ALG_MAX_PAGES
+ 1];
66 struct page
*pages
[ALG_MAX_PAGES
];
72 struct list_head list
;
73 unsigned int cur
; /* Last processed SG entry */
74 struct scatterlist sg
[0]; /* Array of SGs forming the SGL */
77 #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
78 sizeof(struct scatterlist) - 1)
82 struct af_alg_sgl sgl
;
83 struct list_head list
;
84 size_t sg_num_bytes
; /* Bytes of data in that SGL */
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
100 struct af_alg_async_req
{
104 struct af_alg_rsgl first_rsgl
;
105 struct af_alg_rsgl
*last_rsgl
;
106 struct list_head rsgl_list
;
108 struct scatterlist
*tsgl
;
109 unsigned int tsgl_entries
;
112 unsigned int areqlen
;
115 struct aead_request aead_req
;
116 struct skcipher_request skcipher_req
;
119 /* req ctx trails this struct */
123 * struct af_alg_ctx - definition of the crypto context
125 * The crypto context tracks the input data during the lifetime of an AF_ALG
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
141 * @enc: Cryptographic operation to be performed when
142 * recvmsg is invoked.
143 * @len: Length of memory allocated for this data structure.
146 struct list_head tsgl_list
;
149 size_t aead_assoclen
;
151 struct crypto_wait wait
;
163 int af_alg_register_type(const struct af_alg_type
*type
);
164 int af_alg_unregister_type(const struct af_alg_type
*type
);
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
);
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
);
173 static inline struct alg_sock
*alg_sk(struct sock
*sk
)
175 return (struct alg_sock
*)sk
;
179 * Size of available buffer for sending data from user space to kernel.
181 * @sk socket of connection to user space
182 * @return number of bytes still available
184 static inline int af_alg_sndbuf(struct sock
*sk
)
186 struct alg_sock
*ask
= alg_sk(sk
);
187 struct af_alg_ctx
*ctx
= ask
->private;
189 return max_t(int, max_t(int, sk
->sk_sndbuf
& PAGE_MASK
, PAGE_SIZE
) -
194 * Can the send buffer still be written to?
196 * @sk socket of connection to user space
197 * @return true => writable, false => not writable
199 static inline bool af_alg_writable(struct sock
*sk
)
201 return PAGE_SIZE
<= af_alg_sndbuf(sk
);
205 * Size of available buffer used by kernel for the RX user space operation.
207 * @sk socket of connection to user space
208 * @return number of bytes still available
210 static inline int af_alg_rcvbuf(struct sock
*sk
)
212 struct alg_sock
*ask
= alg_sk(sk
);
213 struct af_alg_ctx
*ctx
= ask
->private;
215 return max_t(int, max_t(int, sk
->sk_rcvbuf
& PAGE_MASK
, PAGE_SIZE
) -
216 atomic_read(&ctx
->rcvused
), 0);
220 * Can the RX buffer still be written to?
222 * @sk socket of connection to user space
223 * @return true => writable, false => not writable
225 static inline bool af_alg_readable(struct sock
*sk
)
227 return PAGE_SIZE
<= af_alg_rcvbuf(sk
);
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
,
233 void af_alg_wmem_wakeup(struct sock
*sk
);
234 int af_alg_wait_for_data(struct sock
*sk
, unsigned flags
);
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
,
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
,
249 #endif /* _CRYPTO_IF_ALG_H */