if_alg.h 7.0 KB

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