if_alg.h 6.9 KB

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