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_control
{
46 unsigned int aead_assoclen
;
50 void *(*bind
)(const char *name
, u32 type
, u32 mask
);
51 void (*release
)(void *private);
52 int (*setkey
)(void *private, const u8
*key
, unsigned int keylen
);
53 int (*accept
)(void *private, struct sock
*sk
);
54 int (*accept_nokey
)(void *private, struct sock
*sk
);
55 int (*setauthsize
)(void *private, unsigned int authsize
);
57 struct proto_ops
*ops
;
58 struct proto_ops
*ops_nokey
;
64 struct scatterlist sg
[ALG_MAX_PAGES
+ 1];
65 struct page
*pages
[ALG_MAX_PAGES
];
71 struct list_head list
;
72 unsigned int cur
; /* Last processed SG entry */
73 struct scatterlist sg
[0]; /* Array of SGs forming the SGL */
76 #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
77 sizeof(struct scatterlist) - 1)
81 struct af_alg_sgl sgl
;
82 struct list_head list
;
83 size_t sg_num_bytes
; /* Bytes of data in that SGL */
87 * struct af_alg_async_req - definition of crypto request
88 * @iocb: IOCB for AIO operations
89 * @sk: Socket the request is associated with
90 * @first_rsgl: First RX SG
91 * @last_rsgl: Pointer to last RX SG
92 * @rsgl_list: Track RX SGs
93 * @tsgl: Private, per request TX SGL of buffers to process
94 * @tsgl_entries: Number of entries in priv. TX SGL
95 * @outlen: Number of output bytes generated by crypto op
96 * @areqlen: Length of this data structure
97 * @cra_u: Cipher request
99 struct af_alg_async_req
{
103 struct af_alg_rsgl first_rsgl
;
104 struct af_alg_rsgl
*last_rsgl
;
105 struct list_head rsgl_list
;
107 struct scatterlist
*tsgl
;
108 unsigned int tsgl_entries
;
111 unsigned int areqlen
;
114 struct aead_request aead_req
;
115 struct skcipher_request skcipher_req
;
118 /* req ctx trails this struct */
122 * struct af_alg_ctx - definition of the crypto context
124 * The crypto context tracks the input data during the lifetime of an AF_ALG
127 * @tsgl_list: Link to TX SGL
128 * @iv: IV for cipher operation
129 * @aead_assoclen: Length of AAD for AEAD cipher operations
130 * @completion: Work queue for synchronous operation
131 * @used: TX bytes sent to kernel. This variable is used to
132 * ensure that user space cannot cause the kernel
133 * to allocate too much memory in sendmsg operation.
134 * @rcvused: Total RX bytes to be filled by kernel. This variable
135 * is used to ensure user space cannot cause the kernel
136 * to allocate too much memory in a recvmsg operation.
137 * @more: More data to be expected from user space?
138 * @merge: Shall new data from user space be merged into existing
140 * @enc: Cryptographic operation to be performed when
141 * recvmsg is invoked.
142 * @len: Length of memory allocated for this data structure.
145 struct list_head tsgl_list
;
148 size_t aead_assoclen
;
150 struct crypto_wait wait
;
162 int af_alg_register_type(const struct af_alg_type
*type
);
163 int af_alg_unregister_type(const struct af_alg_type
*type
);
165 int af_alg_release(struct socket
*sock
);
166 void af_alg_release_parent(struct sock
*sk
);
167 int af_alg_accept(struct sock
*sk
, struct socket
*newsock
, bool kern
);
169 int af_alg_make_sg(struct af_alg_sgl
*sgl
, struct iov_iter
*iter
, int len
);
170 void af_alg_free_sg(struct af_alg_sgl
*sgl
);
171 void af_alg_link_sg(struct af_alg_sgl
*sgl_prev
, struct af_alg_sgl
*sgl_new
);
173 int af_alg_cmsg_send(struct msghdr
*msg
, struct af_alg_control
*con
);
175 static inline struct alg_sock
*alg_sk(struct sock
*sk
)
177 return (struct alg_sock
*)sk
;
181 * Size of available buffer for sending data from user space to kernel.
183 * @sk socket of connection to user space
184 * @return number of bytes still available
186 static inline int af_alg_sndbuf(struct sock
*sk
)
188 struct alg_sock
*ask
= alg_sk(sk
);
189 struct af_alg_ctx
*ctx
= ask
->private;
191 return max_t(int, max_t(int, sk
->sk_sndbuf
& PAGE_MASK
, PAGE_SIZE
) -
196 * Can the send buffer still be written to?
198 * @sk socket of connection to user space
199 * @return true => writable, false => not writable
201 static inline bool af_alg_writable(struct sock
*sk
)
203 return PAGE_SIZE
<= af_alg_sndbuf(sk
);
207 * Size of available buffer used by kernel for the RX user space operation.
209 * @sk socket of connection to user space
210 * @return number of bytes still available
212 static inline int af_alg_rcvbuf(struct sock
*sk
)
214 struct alg_sock
*ask
= alg_sk(sk
);
215 struct af_alg_ctx
*ctx
= ask
->private;
217 return max_t(int, max_t(int, sk
->sk_rcvbuf
& PAGE_MASK
, PAGE_SIZE
) -
222 * Can the RX buffer still be written to?
224 * @sk socket of connection to user space
225 * @return true => writable, false => not writable
227 static inline bool af_alg_readable(struct sock
*sk
)
229 return PAGE_SIZE
<= af_alg_rcvbuf(sk
);
232 int af_alg_alloc_tsgl(struct sock
*sk
);
233 unsigned int af_alg_count_tsgl(struct sock
*sk
, size_t bytes
, size_t offset
);
234 void af_alg_pull_tsgl(struct sock
*sk
, size_t used
, struct scatterlist
*dst
,
236 void af_alg_free_areq_sgls(struct af_alg_async_req
*areq
);
237 int af_alg_wait_for_wmem(struct sock
*sk
, unsigned int flags
);
238 void af_alg_wmem_wakeup(struct sock
*sk
);
239 int af_alg_wait_for_data(struct sock
*sk
, unsigned flags
);
240 void af_alg_data_wakeup(struct sock
*sk
);
241 int af_alg_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
,
242 unsigned int ivsize
);
243 ssize_t
af_alg_sendpage(struct socket
*sock
, struct page
*page
,
244 int offset
, size_t size
, int flags
);
245 void af_alg_free_resources(struct af_alg_async_req
*areq
);
246 void af_alg_async_cb(struct crypto_async_request
*_req
, int err
);
247 unsigned int af_alg_poll(struct file
*file
, struct socket
*sock
,
249 struct af_alg_async_req
*af_alg_alloc_areq(struct sock
*sk
,
250 unsigned int areqlen
);
251 int af_alg_get_rsgl(struct sock
*sk
, struct msghdr
*msg
, int flags
,
252 struct af_alg_async_req
*areq
, size_t maxsize
,
255 #endif /* _CRYPTO_IF_ALG_H */