af_alg.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * af_alg: User-space algorithm interface
  3. *
  4. * This file provides the user-space API for algorithms.
  5. *
  6. * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <asm/atomic.h>
  15. #include <crypto/if_alg.h>
  16. #include <linux/crypto.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/net.h>
  22. #include <linux/rwsem.h>
  23. struct alg_type_list {
  24. const struct af_alg_type *type;
  25. struct list_head list;
  26. };
  27. static atomic_long_t alg_memory_allocated;
  28. static struct proto alg_proto = {
  29. .name = "ALG",
  30. .owner = THIS_MODULE,
  31. .memory_allocated = &alg_memory_allocated,
  32. .obj_size = sizeof(struct alg_sock),
  33. };
  34. static LIST_HEAD(alg_types);
  35. static DECLARE_RWSEM(alg_types_sem);
  36. static const struct af_alg_type *alg_get_type(const char *name)
  37. {
  38. const struct af_alg_type *type = ERR_PTR(-ENOENT);
  39. struct alg_type_list *node;
  40. down_read(&alg_types_sem);
  41. list_for_each_entry(node, &alg_types, list) {
  42. if (strcmp(node->type->name, name))
  43. continue;
  44. if (try_module_get(node->type->owner))
  45. type = node->type;
  46. break;
  47. }
  48. up_read(&alg_types_sem);
  49. return type;
  50. }
  51. int af_alg_register_type(const struct af_alg_type *type)
  52. {
  53. struct alg_type_list *node;
  54. int err = -EEXIST;
  55. down_write(&alg_types_sem);
  56. list_for_each_entry(node, &alg_types, list) {
  57. if (!strcmp(node->type->name, type->name))
  58. goto unlock;
  59. }
  60. node = kmalloc(sizeof(*node), GFP_KERNEL);
  61. err = -ENOMEM;
  62. if (!node)
  63. goto unlock;
  64. type->ops->owner = THIS_MODULE;
  65. node->type = type;
  66. list_add(&node->list, &alg_types);
  67. err = 0;
  68. unlock:
  69. up_write(&alg_types_sem);
  70. return err;
  71. }
  72. EXPORT_SYMBOL_GPL(af_alg_register_type);
  73. int af_alg_unregister_type(const struct af_alg_type *type)
  74. {
  75. struct alg_type_list *node;
  76. int err = -ENOENT;
  77. down_write(&alg_types_sem);
  78. list_for_each_entry(node, &alg_types, list) {
  79. if (strcmp(node->type->name, type->name))
  80. continue;
  81. list_del(&node->list);
  82. kfree(node);
  83. err = 0;
  84. break;
  85. }
  86. up_write(&alg_types_sem);
  87. return err;
  88. }
  89. EXPORT_SYMBOL_GPL(af_alg_unregister_type);
  90. static void alg_do_release(const struct af_alg_type *type, void *private)
  91. {
  92. if (!type)
  93. return;
  94. type->release(private);
  95. module_put(type->owner);
  96. }
  97. int af_alg_release(struct socket *sock)
  98. {
  99. if (sock->sk)
  100. sock_put(sock->sk);
  101. return 0;
  102. }
  103. EXPORT_SYMBOL_GPL(af_alg_release);
  104. static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  105. {
  106. struct sock *sk = sock->sk;
  107. struct alg_sock *ask = alg_sk(sk);
  108. struct sockaddr_alg *sa = (void *)uaddr;
  109. const struct af_alg_type *type;
  110. void *private;
  111. if (sock->state == SS_CONNECTED)
  112. return -EINVAL;
  113. if (addr_len != sizeof(*sa))
  114. return -EINVAL;
  115. sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
  116. sa->salg_name[sizeof(sa->salg_name) - 1] = 0;
  117. type = alg_get_type(sa->salg_type);
  118. if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
  119. request_module("algif-%s", sa->salg_type);
  120. type = alg_get_type(sa->salg_type);
  121. }
  122. if (IS_ERR(type))
  123. return PTR_ERR(type);
  124. private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
  125. if (IS_ERR(private)) {
  126. module_put(type->owner);
  127. return PTR_ERR(private);
  128. }
  129. lock_sock(sk);
  130. swap(ask->type, type);
  131. swap(ask->private, private);
  132. release_sock(sk);
  133. alg_do_release(type, private);
  134. return 0;
  135. }
  136. static int alg_setkey(struct sock *sk, char __user *ukey,
  137. unsigned int keylen)
  138. {
  139. struct alg_sock *ask = alg_sk(sk);
  140. const struct af_alg_type *type = ask->type;
  141. u8 *key;
  142. int err;
  143. key = sock_kmalloc(sk, keylen, GFP_KERNEL);
  144. if (!key)
  145. return -ENOMEM;
  146. err = -EFAULT;
  147. if (copy_from_user(key, ukey, keylen))
  148. goto out;
  149. err = type->setkey(ask->private, key, keylen);
  150. out:
  151. sock_kfree_s(sk, key, keylen);
  152. return err;
  153. }
  154. static int alg_setsockopt(struct socket *sock, int level, int optname,
  155. char __user *optval, unsigned int optlen)
  156. {
  157. struct sock *sk = sock->sk;
  158. struct alg_sock *ask = alg_sk(sk);
  159. const struct af_alg_type *type;
  160. int err = -ENOPROTOOPT;
  161. lock_sock(sk);
  162. type = ask->type;
  163. if (level != SOL_ALG || !type)
  164. goto unlock;
  165. switch (optname) {
  166. case ALG_SET_KEY:
  167. if (sock->state == SS_CONNECTED)
  168. goto unlock;
  169. if (!type->setkey)
  170. goto unlock;
  171. err = alg_setkey(sk, optval, optlen);
  172. }
  173. unlock:
  174. release_sock(sk);
  175. return err;
  176. }
  177. int af_alg_accept(struct sock *sk, struct socket *newsock)
  178. {
  179. struct alg_sock *ask = alg_sk(sk);
  180. const struct af_alg_type *type;
  181. struct sock *sk2;
  182. int err;
  183. lock_sock(sk);
  184. type = ask->type;
  185. err = -EINVAL;
  186. if (!type)
  187. goto unlock;
  188. sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto);
  189. err = -ENOMEM;
  190. if (!sk2)
  191. goto unlock;
  192. sock_init_data(newsock, sk2);
  193. sock_graft(sk2, newsock);
  194. err = type->accept(ask->private, sk2);
  195. if (err) {
  196. sk_free(sk2);
  197. goto unlock;
  198. }
  199. sk2->sk_family = PF_ALG;
  200. sock_hold(sk);
  201. alg_sk(sk2)->parent = sk;
  202. alg_sk(sk2)->type = type;
  203. newsock->ops = type->ops;
  204. newsock->state = SS_CONNECTED;
  205. err = 0;
  206. unlock:
  207. release_sock(sk);
  208. return err;
  209. }
  210. EXPORT_SYMBOL_GPL(af_alg_accept);
  211. static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
  212. {
  213. return af_alg_accept(sock->sk, newsock);
  214. }
  215. static const struct proto_ops alg_proto_ops = {
  216. .family = PF_ALG,
  217. .owner = THIS_MODULE,
  218. .connect = sock_no_connect,
  219. .socketpair = sock_no_socketpair,
  220. .getname = sock_no_getname,
  221. .ioctl = sock_no_ioctl,
  222. .listen = sock_no_listen,
  223. .shutdown = sock_no_shutdown,
  224. .getsockopt = sock_no_getsockopt,
  225. .mmap = sock_no_mmap,
  226. .sendpage = sock_no_sendpage,
  227. .sendmsg = sock_no_sendmsg,
  228. .recvmsg = sock_no_recvmsg,
  229. .poll = sock_no_poll,
  230. .bind = alg_bind,
  231. .release = af_alg_release,
  232. .setsockopt = alg_setsockopt,
  233. .accept = alg_accept,
  234. };
  235. static void alg_sock_destruct(struct sock *sk)
  236. {
  237. struct alg_sock *ask = alg_sk(sk);
  238. alg_do_release(ask->type, ask->private);
  239. }
  240. static int alg_create(struct net *net, struct socket *sock, int protocol,
  241. int kern)
  242. {
  243. struct sock *sk;
  244. int err;
  245. if (sock->type != SOCK_SEQPACKET)
  246. return -ESOCKTNOSUPPORT;
  247. if (protocol != 0)
  248. return -EPROTONOSUPPORT;
  249. err = -ENOMEM;
  250. sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto);
  251. if (!sk)
  252. goto out;
  253. sock->ops = &alg_proto_ops;
  254. sock_init_data(sock, sk);
  255. sk->sk_family = PF_ALG;
  256. sk->sk_destruct = alg_sock_destruct;
  257. return 0;
  258. out:
  259. return err;
  260. }
  261. static const struct net_proto_family alg_family = {
  262. .family = PF_ALG,
  263. .create = alg_create,
  264. .owner = THIS_MODULE,
  265. };
  266. int af_alg_make_sg(struct af_alg_sgl *sgl, void __user *addr, int len,
  267. int write)
  268. {
  269. unsigned long from = (unsigned long)addr;
  270. unsigned long npages;
  271. unsigned off;
  272. int err;
  273. int i;
  274. err = -EFAULT;
  275. if (!access_ok(write ? VERIFY_READ : VERIFY_WRITE, addr, len))
  276. goto out;
  277. off = from & ~PAGE_MASK;
  278. npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  279. if (npages > ALG_MAX_PAGES)
  280. npages = ALG_MAX_PAGES;
  281. err = get_user_pages_fast(from, npages, write, sgl->pages);
  282. if (err < 0)
  283. goto out;
  284. npages = err;
  285. err = -EINVAL;
  286. if (WARN_ON(npages == 0))
  287. goto out;
  288. err = 0;
  289. sg_init_table(sgl->sg, npages);
  290. for (i = 0; i < npages; i++) {
  291. int plen = min_t(int, len, PAGE_SIZE - off);
  292. sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
  293. off = 0;
  294. len -= plen;
  295. err += plen;
  296. }
  297. out:
  298. return err;
  299. }
  300. EXPORT_SYMBOL_GPL(af_alg_make_sg);
  301. void af_alg_free_sg(struct af_alg_sgl *sgl)
  302. {
  303. int i;
  304. i = 0;
  305. do {
  306. put_page(sgl->pages[i]);
  307. } while (!sg_is_last(sgl->sg + (i++)));
  308. }
  309. EXPORT_SYMBOL_GPL(af_alg_free_sg);
  310. int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
  311. {
  312. struct cmsghdr *cmsg;
  313. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  314. if (!CMSG_OK(msg, cmsg))
  315. return -EINVAL;
  316. if (cmsg->cmsg_level != SOL_ALG)
  317. continue;
  318. switch(cmsg->cmsg_type) {
  319. case ALG_SET_IV:
  320. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
  321. return -EINVAL;
  322. con->iv = (void *)CMSG_DATA(cmsg);
  323. if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
  324. sizeof(*con->iv)))
  325. return -EINVAL;
  326. break;
  327. case ALG_SET_OP:
  328. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  329. return -EINVAL;
  330. con->op = *(u32 *)CMSG_DATA(cmsg);
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. }
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
  339. int af_alg_wait_for_completion(int err, struct af_alg_completion *completion)
  340. {
  341. switch (err) {
  342. case -EINPROGRESS:
  343. case -EBUSY:
  344. wait_for_completion(&completion->completion);
  345. INIT_COMPLETION(completion->completion);
  346. err = completion->err;
  347. break;
  348. };
  349. return err;
  350. }
  351. EXPORT_SYMBOL_GPL(af_alg_wait_for_completion);
  352. void af_alg_complete(struct crypto_async_request *req, int err)
  353. {
  354. struct af_alg_completion *completion = req->data;
  355. completion->err = err;
  356. complete(&completion->completion);
  357. }
  358. EXPORT_SYMBOL_GPL(af_alg_complete);
  359. static int __init af_alg_init(void)
  360. {
  361. int err = proto_register(&alg_proto, 0);
  362. if (err)
  363. goto out;
  364. err = sock_register(&alg_family);
  365. if (err != 0)
  366. goto out_unregister_proto;
  367. out:
  368. return err;
  369. out_unregister_proto:
  370. proto_unregister(&alg_proto);
  371. goto out;
  372. }
  373. static void __exit af_alg_exit(void)
  374. {
  375. sock_unregister(PF_ALG);
  376. proto_unregister(&alg_proto);
  377. }
  378. module_init(af_alg_init);
  379. module_exit(af_alg_exit);
  380. MODULE_LICENSE("GPL");
  381. MODULE_ALIAS_NETPROTO(AF_ALG);