if_alg.h 7.3 KB

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