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>
23 #include <crypto/aead.h>
24 #include <crypto/skcipher.h>
26 #define ALG_MAX_PAGES 16
28 struct crypto_async_request
;
31 /* struct sock must be the first member of struct alg_sock */
37 unsigned int nokey_refcnt
;
39 const struct af_alg_type
*type
;
43 struct af_alg_completion
{
44 struct completion completion
;
48 struct af_alg_control
{
51 unsigned int aead_assoclen
;
55 void *(*bind
)(const char *name
, u32 type
, u32 mask
);
56 void (*release
)(void *private);
57 int (*setkey
)(void *private, const u8
*key
, unsigned int keylen
);
58 int (*accept
)(void *private, struct sock
*sk
);
59 int (*accept_nokey
)(void *private, struct sock
*sk
);
60 int (*setauthsize
)(void *private, unsigned int authsize
);
62 struct proto_ops
*ops
;
63 struct proto_ops
*ops_nokey
;
69 struct scatterlist sg
[ALG_MAX_PAGES
+ 1];
70 struct page
*pages
[ALG_MAX_PAGES
];
76 struct list_head list
;
77 unsigned int cur
; /* Last processed SG entry */
78 struct scatterlist sg
[0]; /* Array of SGs forming the SGL */
81 #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
82 sizeof(struct scatterlist) - 1)
86 struct af_alg_sgl sgl
;
87 struct list_head list
;
88 size_t sg_num_bytes
; /* Bytes of data in that SGL */
92 * struct af_alg_async_req - definition of crypto request
93 * @iocb: IOCB for AIO operations
94 * @sk: Socket the request is associated with
95 * @first_rsgl: First RX SG
96 * @last_rsgl: Pointer to last RX SG
97 * @rsgl_list: Track RX SGs
98 * @tsgl: Private, per request TX SGL of buffers to process
99 * @tsgl_entries: Number of entries in priv. TX SGL
100 * @outlen: Number of output bytes generated by crypto op
101 * @areqlen: Length of this data structure
102 * @cra_u: Cipher request
104 struct af_alg_async_req
{
108 struct af_alg_rsgl first_rsgl
;
109 struct af_alg_rsgl
*last_rsgl
;
110 struct list_head rsgl_list
;
112 struct scatterlist
*tsgl
;
113 unsigned int tsgl_entries
;
116 unsigned int areqlen
;
119 struct aead_request aead_req
;
120 struct skcipher_request skcipher_req
;
123 /* req ctx trails this struct */
127 * struct af_alg_ctx - definition of the crypto context
129 * The crypto context tracks the input data during the lifetime of an AF_ALG
132 * @tsgl_list: Link to TX SGL
133 * @iv: IV for cipher operation
134 * @aead_assoclen: Length of AAD for AEAD cipher operations
135 * @completion: Work queue for synchronous operation
136 * @used: TX bytes sent to kernel. This variable is used to
137 * ensure that user space cannot cause the kernel
138 * to allocate too much memory in sendmsg operation.
139 * @rcvused: Total RX bytes to be filled by kernel. This variable
140 * is used to ensure user space cannot cause the kernel
141 * to allocate too much memory in a recvmsg operation.
142 * @more: More data to be expected from user space?
143 * @merge: Shall new data from user space be merged into existing
145 * @enc: Cryptographic operation to be performed when
146 * recvmsg is invoked.
147 * @len: Length of memory allocated for this data structure.
150 struct list_head tsgl_list
;
153 size_t aead_assoclen
;
155 struct af_alg_completion completion
;
167 int af_alg_register_type(const struct af_alg_type
*type
);
168 int af_alg_unregister_type(const struct af_alg_type
*type
);
170 int af_alg_release(struct socket
*sock
);
171 void af_alg_release_parent(struct sock
*sk
);
172 int af_alg_accept(struct sock
*sk
, struct socket
*newsock
, bool kern
);
174 int af_alg_make_sg(struct af_alg_sgl
*sgl
, struct iov_iter
*iter
, int len
);
175 void af_alg_free_sg(struct af_alg_sgl
*sgl
);
176 void af_alg_link_sg(struct af_alg_sgl
*sgl_prev
, struct af_alg_sgl
*sgl_new
);
178 int af_alg_cmsg_send(struct msghdr
*msg
, struct af_alg_control
*con
);
180 int af_alg_wait_for_completion(int err
, struct af_alg_completion
*completion
);
181 void af_alg_complete(struct crypto_async_request
*req
, int err
);
183 static inline struct alg_sock
*alg_sk(struct sock
*sk
)
185 return (struct alg_sock
*)sk
;
188 static inline void af_alg_init_completion(struct af_alg_completion
*completion
)
190 init_completion(&completion
->completion
);
194 * Size of available buffer for sending data from user space to kernel.
196 * @sk socket of connection to user space
197 * @return number of bytes still available
199 static inline int af_alg_sndbuf(struct sock
*sk
)
201 struct alg_sock
*ask
= alg_sk(sk
);
202 struct af_alg_ctx
*ctx
= ask
->private;
204 return max_t(int, max_t(int, sk
->sk_sndbuf
& PAGE_MASK
, PAGE_SIZE
) -
209 * Can the send buffer still be written to?
211 * @sk socket of connection to user space
212 * @return true => writable, false => not writable
214 static inline bool af_alg_writable(struct sock
*sk
)
216 return PAGE_SIZE
<= af_alg_sndbuf(sk
);
220 * Size of available buffer used by kernel for the RX user space operation.
222 * @sk socket of connection to user space
223 * @return number of bytes still available
225 static inline int af_alg_rcvbuf(struct sock
*sk
)
227 struct alg_sock
*ask
= alg_sk(sk
);
228 struct af_alg_ctx
*ctx
= ask
->private;
230 return max_t(int, max_t(int, sk
->sk_rcvbuf
& PAGE_MASK
, PAGE_SIZE
) -
235 * Can the RX buffer still be written to?
237 * @sk socket of connection to user space
238 * @return true => writable, false => not writable
240 static inline bool af_alg_readable(struct sock
*sk
)
242 return PAGE_SIZE
<= af_alg_rcvbuf(sk
);
245 int af_alg_alloc_tsgl(struct sock
*sk
);
246 unsigned int af_alg_count_tsgl(struct sock
*sk
, size_t bytes
, size_t offset
);
247 void af_alg_pull_tsgl(struct sock
*sk
, size_t used
, struct scatterlist
*dst
,
249 void af_alg_free_areq_sgls(struct af_alg_async_req
*areq
);
250 int af_alg_wait_for_wmem(struct sock
*sk
, unsigned int flags
);
251 void af_alg_wmem_wakeup(struct sock
*sk
);
252 int af_alg_wait_for_data(struct sock
*sk
, unsigned flags
);
253 void af_alg_data_wakeup(struct sock
*sk
);
254 int af_alg_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
,
255 unsigned int ivsize
);
256 ssize_t
af_alg_sendpage(struct socket
*sock
, struct page
*page
,
257 int offset
, size_t size
, int flags
);
258 void af_alg_free_resources(struct af_alg_async_req
*areq
);
259 void af_alg_async_cb(struct crypto_async_request
*_req
, int err
);
260 unsigned int af_alg_poll(struct file
*file
, struct socket
*sock
,
262 struct af_alg_async_req
*af_alg_alloc_areq(struct sock
*sk
,
263 unsigned int areqlen
);
264 int af_alg_get_rsgl(struct sock
*sk
, struct msghdr
*msg
, int flags
,
265 struct af_alg_async_req
*areq
, size_t maxsize
,
268 #endif /* _CRYPTO_IF_ALG_H */