if_alg.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #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 alg_sock {
  20. /* struct sock must be the first member of struct alg_sock */
  21. struct sock sk;
  22. struct sock *parent;
  23. atomic_t refcnt;
  24. atomic_t nokey_refcnt;
  25. const struct af_alg_type *type;
  26. void *private;
  27. };
  28. struct af_alg_control {
  29. struct af_alg_iv *iv;
  30. int op;
  31. unsigned int aead_assoclen;
  32. };
  33. struct af_alg_type {
  34. void *(*bind)(const char *name, u32 type, u32 mask);
  35. void (*release)(void *private);
  36. int (*setkey)(void *private, const u8 *key, unsigned int keylen);
  37. int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
  38. int (*accept)(void *private, struct sock *sk);
  39. int (*accept_nokey)(void *private, struct sock *sk);
  40. int (*setauthsize)(void *private, unsigned int authsize);
  41. struct proto_ops *ops;
  42. struct proto_ops *ops_nokey;
  43. struct module *owner;
  44. char name[14];
  45. };
  46. struct af_alg_sgl {
  47. struct sg_table sgt;
  48. struct scatterlist sgl[ALG_MAX_PAGES + 1];
  49. bool need_unpin;
  50. };
  51. /* TX SGL entry */
  52. struct af_alg_tsgl {
  53. struct list_head list;
  54. unsigned int cur; /* Last processed SG entry */
  55. struct scatterlist sg[]; /* Array of SGs forming the SGL */
  56. };
  57. #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
  58. sizeof(struct scatterlist) - 1)
  59. /* RX SGL entry */
  60. struct af_alg_rsgl {
  61. struct af_alg_sgl sgl;
  62. struct list_head list;
  63. size_t sg_num_bytes; /* Bytes of data in that SGL */
  64. };
  65. /**
  66. * struct af_alg_async_req - definition of crypto request
  67. * @iocb: IOCB for AIO operations
  68. * @sk: Socket the request is associated with
  69. * @first_rsgl: First RX SG
  70. * @last_rsgl: Pointer to last RX SG
  71. * @rsgl_list: Track RX SGs
  72. * @tsgl: Private, per request TX SGL of buffers to process
  73. * @tsgl_entries: Number of entries in priv. TX SGL
  74. * @outlen: Number of output bytes generated by crypto op
  75. * @areqlen: Length of this data structure
  76. * @cra_u: Cipher request
  77. */
  78. struct af_alg_async_req {
  79. struct kiocb *iocb;
  80. struct sock *sk;
  81. struct af_alg_rsgl first_rsgl;
  82. struct af_alg_rsgl *last_rsgl;
  83. struct list_head rsgl_list;
  84. struct scatterlist *tsgl;
  85. unsigned int tsgl_entries;
  86. unsigned int outlen;
  87. unsigned int areqlen;
  88. union {
  89. struct aead_request aead_req;
  90. struct skcipher_request skcipher_req;
  91. } cra_u;
  92. /* req ctx trails this struct */
  93. };
  94. /**
  95. * struct af_alg_ctx - definition of the crypto context
  96. *
  97. * The crypto context tracks the input data during the lifetime of an AF_ALG
  98. * socket.
  99. *
  100. * @tsgl_list: Link to TX SGL
  101. * @iv: IV for cipher operation
  102. * @state: Existing state for continuing 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. * @write: True if we are in the middle of a write.
  117. * @init: True if metadata has been sent.
  118. * @len: Length of memory allocated for this data structure.
  119. * @inflight: Non-zero when AIO requests are in flight.
  120. */
  121. struct af_alg_ctx {
  122. struct list_head tsgl_list;
  123. void *iv;
  124. void *state;
  125. size_t aead_assoclen;
  126. struct crypto_wait wait;
  127. size_t used;
  128. atomic_t rcvused;
  129. bool more:1,
  130. merge:1,
  131. enc:1,
  132. write:1,
  133. init:1;
  134. unsigned int len;
  135. unsigned int inflight;
  136. };
  137. int af_alg_register_type(const struct af_alg_type *type);
  138. int af_alg_unregister_type(const struct af_alg_type *type);
  139. int af_alg_release(struct socket *sock);
  140. void af_alg_release_parent(struct sock *sk);
  141. int af_alg_accept(struct sock *sk, struct socket *newsock,
  142. struct proto_accept_arg *arg);
  143. void af_alg_free_sg(struct af_alg_sgl *sgl);
  144. static inline struct alg_sock *alg_sk(struct sock *sk)
  145. {
  146. return (struct alg_sock *)sk;
  147. }
  148. /**
  149. * Size of available buffer for sending data from user space to kernel.
  150. *
  151. * @sk socket of connection to user space
  152. * @return number of bytes still available
  153. */
  154. static inline int af_alg_sndbuf(struct sock *sk)
  155. {
  156. struct alg_sock *ask = alg_sk(sk);
  157. struct af_alg_ctx *ctx = ask->private;
  158. return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
  159. ctx->used, 0);
  160. }
  161. /**
  162. * Can the send buffer still be written to?
  163. *
  164. * @sk socket of connection to user space
  165. * @return true => writable, false => not writable
  166. */
  167. static inline bool af_alg_writable(struct sock *sk)
  168. {
  169. return PAGE_SIZE <= af_alg_sndbuf(sk);
  170. }
  171. /**
  172. * Size of available buffer used by kernel for the RX user space operation.
  173. *
  174. * @sk socket of connection to user space
  175. * @return number of bytes still available
  176. */
  177. static inline int af_alg_rcvbuf(struct sock *sk)
  178. {
  179. struct alg_sock *ask = alg_sk(sk);
  180. struct af_alg_ctx *ctx = ask->private;
  181. return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
  182. atomic_read(&ctx->rcvused), 0);
  183. }
  184. /**
  185. * Can the RX buffer still be written to?
  186. *
  187. * @sk socket of connection to user space
  188. * @return true => writable, false => not writable
  189. */
  190. static inline bool af_alg_readable(struct sock *sk)
  191. {
  192. return PAGE_SIZE <= af_alg_rcvbuf(sk);
  193. }
  194. unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes);
  195. void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst);
  196. void af_alg_wmem_wakeup(struct sock *sk);
  197. int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
  198. int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
  199. unsigned int ivsize);
  200. void af_alg_free_resources(struct af_alg_async_req *areq);
  201. void af_alg_async_cb(void *data, int err);
  202. __poll_t af_alg_poll(struct file *file, struct socket *sock,
  203. poll_table *wait);
  204. struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
  205. unsigned int areqlen);
  206. int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
  207. struct af_alg_async_req *areq, size_t maxsize,
  208. size_t *outlen);
  209. #endif /* _CRYPTO_IF_ALG_H */