if_alg.h 7.3 KB

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