if_alg.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * if_alg: User-space algorithm interface
  4. *
  5. * Copyright (c) 2010 Herbert Xu <[email protected]>
  6. */
  7. #ifndef _CRYPTO_IF_ALG_H
  8. #define _CRYPTO_IF_ALG_H
  9. #include <linux/compiler.h>
  10. #include <linux/completion.h>
  11. #include <linux/if_alg.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/types.h>
  14. #include <linux/atomic.h>
  15. #include <net/sock.h>
  16. #include <crypto/aead.h>
  17. #include <crypto/skcipher.h>
  18. #define ALG_MAX_PAGES 16
  19. struct crypto_async_request;
  20. struct alg_sock {
  21. /* struct sock must be the first member of struct alg_sock */
  22. struct sock sk;
  23. struct sock *parent;
  24. atomic_t refcnt;
  25. atomic_t nokey_refcnt;
  26. const struct af_alg_type *type;
  27. void *private;
  28. };
  29. struct af_alg_control {
  30. struct af_alg_iv *iv;
  31. int op;
  32. unsigned int aead_assoclen;
  33. };
  34. struct af_alg_type {
  35. void *(*bind)(const char *name, u32 type, u32 mask);
  36. void (*release)(void *private);
  37. int (*setkey)(void *private, const u8 *key, unsigned int keylen);
  38. int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
  39. int (*accept)(void *private, struct sock *sk);
  40. int (*accept_nokey)(void *private, struct sock *sk);
  41. int (*setauthsize)(void *private, unsigned int authsize);
  42. struct proto_ops *ops;
  43. struct proto_ops *ops_nokey;
  44. struct module *owner;
  45. char name[14];
  46. };
  47. struct af_alg_sgl {
  48. struct scatterlist sg[ALG_MAX_PAGES + 1];
  49. struct page *pages[ALG_MAX_PAGES];
  50. unsigned int npages;
  51. };
  52. /* TX SGL entry */
  53. struct af_alg_tsgl {
  54. struct list_head list;
  55. unsigned int cur; /* Last processed SG entry */
  56. struct scatterlist sg[]; /* Array of SGs forming the SGL */
  57. };
  58. #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
  59. sizeof(struct scatterlist) - 1)
  60. /* RX SGL entry */
  61. struct af_alg_rsgl {
  62. struct af_alg_sgl sgl;
  63. struct list_head list;
  64. size_t sg_num_bytes; /* Bytes of data in that SGL */
  65. };
  66. /**
  67. * struct af_alg_async_req - definition of crypto request
  68. * @iocb: IOCB for AIO operations
  69. * @sk: Socket the request is associated with
  70. * @first_rsgl: First RX SG
  71. * @last_rsgl: Pointer to last RX SG
  72. * @rsgl_list: Track RX SGs
  73. * @tsgl: Private, per request TX SGL of buffers to process
  74. * @tsgl_entries: Number of entries in priv. TX SGL
  75. * @outlen: Number of output bytes generated by crypto op
  76. * @areqlen: Length of this data structure
  77. * @cra_u: Cipher request
  78. */
  79. struct af_alg_async_req {
  80. struct kiocb *iocb;
  81. struct sock *sk;
  82. struct af_alg_rsgl first_rsgl;
  83. struct af_alg_rsgl *last_rsgl;
  84. struct list_head rsgl_list;
  85. struct scatterlist *tsgl;
  86. unsigned int tsgl_entries;
  87. unsigned int outlen;
  88. unsigned int areqlen;
  89. union {
  90. struct aead_request aead_req;
  91. struct skcipher_request skcipher_req;
  92. } cra_u;
  93. /* req ctx trails this struct */
  94. };
  95. /**
  96. * struct af_alg_ctx - definition of the crypto context
  97. *
  98. * The crypto context tracks the input data during the lifetime of an AF_ALG
  99. * socket.
  100. *
  101. * @tsgl_list: Link to TX SGL
  102. * @iv: IV for cipher operation
  103. * @aead_assoclen: Length of AAD for AEAD cipher operations
  104. * @completion: Work queue for synchronous operation
  105. * @used: TX bytes sent to kernel. This variable is used to
  106. * ensure that user space cannot cause the kernel
  107. * to allocate too much memory in sendmsg operation.
  108. * @rcvused: Total RX bytes to be filled by kernel. This variable
  109. * is used to ensure user space cannot cause the kernel
  110. * to allocate too much memory in a recvmsg operation.
  111. * @more: More data to be expected from user space?
  112. * @merge: Shall new data from user space be merged into existing
  113. * SG?
  114. * @enc: Cryptographic operation to be performed when
  115. * recvmsg is invoked.
  116. * @init: True if metadata has been sent.
  117. * @len: Length of memory allocated for this data structure.
  118. */
  119. struct af_alg_ctx {
  120. struct list_head tsgl_list;
  121. void *iv;
  122. size_t aead_assoclen;
  123. struct crypto_wait wait;
  124. size_t used;
  125. atomic_t rcvused;
  126. bool more;
  127. bool merge;
  128. bool enc;
  129. bool init;
  130. unsigned int len;
  131. };
  132. int af_alg_register_type(const struct af_alg_type *type);
  133. int af_alg_unregister_type(const struct af_alg_type *type);
  134. int af_alg_release(struct socket *sock);
  135. void af_alg_release_parent(struct sock *sk);
  136. int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
  137. int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
  138. void af_alg_free_sg(struct af_alg_sgl *sgl);
  139. static inline struct alg_sock *alg_sk(struct sock *sk)
  140. {
  141. return (struct alg_sock *)sk;
  142. }
  143. /**
  144. * Size of available buffer for sending data from user space to kernel.
  145. *
  146. * @sk socket of connection to user space
  147. * @return number of bytes still available
  148. */
  149. static inline int af_alg_sndbuf(struct sock *sk)
  150. {
  151. struct alg_sock *ask = alg_sk(sk);
  152. struct af_alg_ctx *ctx = ask->private;
  153. return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
  154. ctx->used, 0);
  155. }
  156. /**
  157. * Can the send buffer still be written to?
  158. *
  159. * @sk socket of connection to user space
  160. * @return true => writable, false => not writable
  161. */
  162. static inline bool af_alg_writable(struct sock *sk)
  163. {
  164. return PAGE_SIZE <= af_alg_sndbuf(sk);
  165. }
  166. /**
  167. * Size of available buffer used by kernel for the RX user space operation.
  168. *
  169. * @sk socket of connection to user space
  170. * @return number of bytes still available
  171. */
  172. static inline int af_alg_rcvbuf(struct sock *sk)
  173. {
  174. struct alg_sock *ask = alg_sk(sk);
  175. struct af_alg_ctx *ctx = ask->private;
  176. return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
  177. atomic_read(&ctx->rcvused), 0);
  178. }
  179. /**
  180. * Can the RX buffer still be written to?
  181. *
  182. * @sk socket of connection to user space
  183. * @return true => writable, false => not writable
  184. */
  185. static inline bool af_alg_readable(struct sock *sk)
  186. {
  187. return PAGE_SIZE <= af_alg_rcvbuf(sk);
  188. }
  189. unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
  190. void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
  191. size_t dst_offset);
  192. void af_alg_wmem_wakeup(struct sock *sk);
  193. int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
  194. int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
  195. unsigned int ivsize);
  196. ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
  197. int offset, size_t size, int flags);
  198. void af_alg_free_resources(struct af_alg_async_req *areq);
  199. void af_alg_async_cb(struct crypto_async_request *_req, int err);
  200. __poll_t af_alg_poll(struct file *file, struct socket *sock,
  201. poll_table *wait);
  202. struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
  203. unsigned int areqlen);
  204. int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
  205. struct af_alg_async_req *areq, size_t maxsize,
  206. size_t *outlen);
  207. #endif /* _CRYPTO_IF_ALG_H */