af_alg.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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 <linux/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. #include <linux/sched/signal.h>
  24. #include <linux/security.h>
  25. struct alg_type_list {
  26. const struct af_alg_type *type;
  27. struct list_head list;
  28. };
  29. static atomic_long_t alg_memory_allocated;
  30. static struct proto alg_proto = {
  31. .name = "ALG",
  32. .owner = THIS_MODULE,
  33. .memory_allocated = &alg_memory_allocated,
  34. .obj_size = sizeof(struct alg_sock),
  35. };
  36. static LIST_HEAD(alg_types);
  37. static DECLARE_RWSEM(alg_types_sem);
  38. static const struct af_alg_type *alg_get_type(const char *name)
  39. {
  40. const struct af_alg_type *type = ERR_PTR(-ENOENT);
  41. struct alg_type_list *node;
  42. down_read(&alg_types_sem);
  43. list_for_each_entry(node, &alg_types, list) {
  44. if (strcmp(node->type->name, name))
  45. continue;
  46. if (try_module_get(node->type->owner))
  47. type = node->type;
  48. break;
  49. }
  50. up_read(&alg_types_sem);
  51. return type;
  52. }
  53. int af_alg_register_type(const struct af_alg_type *type)
  54. {
  55. struct alg_type_list *node;
  56. int err = -EEXIST;
  57. down_write(&alg_types_sem);
  58. list_for_each_entry(node, &alg_types, list) {
  59. if (!strcmp(node->type->name, type->name))
  60. goto unlock;
  61. }
  62. node = kmalloc(sizeof(*node), GFP_KERNEL);
  63. err = -ENOMEM;
  64. if (!node)
  65. goto unlock;
  66. type->ops->owner = THIS_MODULE;
  67. if (type->ops_nokey)
  68. type->ops_nokey->owner = THIS_MODULE;
  69. node->type = type;
  70. list_add(&node->list, &alg_types);
  71. err = 0;
  72. unlock:
  73. up_write(&alg_types_sem);
  74. return err;
  75. }
  76. EXPORT_SYMBOL_GPL(af_alg_register_type);
  77. int af_alg_unregister_type(const struct af_alg_type *type)
  78. {
  79. struct alg_type_list *node;
  80. int err = -ENOENT;
  81. down_write(&alg_types_sem);
  82. list_for_each_entry(node, &alg_types, list) {
  83. if (strcmp(node->type->name, type->name))
  84. continue;
  85. list_del(&node->list);
  86. kfree(node);
  87. err = 0;
  88. break;
  89. }
  90. up_write(&alg_types_sem);
  91. return err;
  92. }
  93. EXPORT_SYMBOL_GPL(af_alg_unregister_type);
  94. static void alg_do_release(const struct af_alg_type *type, void *private)
  95. {
  96. if (!type)
  97. return;
  98. type->release(private);
  99. module_put(type->owner);
  100. }
  101. int af_alg_release(struct socket *sock)
  102. {
  103. if (sock->sk)
  104. sock_put(sock->sk);
  105. return 0;
  106. }
  107. EXPORT_SYMBOL_GPL(af_alg_release);
  108. void af_alg_release_parent(struct sock *sk)
  109. {
  110. struct alg_sock *ask = alg_sk(sk);
  111. unsigned int nokey = ask->nokey_refcnt;
  112. bool last = nokey && !ask->refcnt;
  113. sk = ask->parent;
  114. ask = alg_sk(sk);
  115. lock_sock(sk);
  116. ask->nokey_refcnt -= nokey;
  117. if (!last)
  118. last = !--ask->refcnt;
  119. release_sock(sk);
  120. if (last)
  121. sock_put(sk);
  122. }
  123. EXPORT_SYMBOL_GPL(af_alg_release_parent);
  124. static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  125. {
  126. const u32 allowed = CRYPTO_ALG_KERN_DRIVER_ONLY;
  127. struct sock *sk = sock->sk;
  128. struct alg_sock *ask = alg_sk(sk);
  129. struct sockaddr_alg *sa = (void *)uaddr;
  130. const struct af_alg_type *type;
  131. void *private;
  132. int err;
  133. if (sock->state == SS_CONNECTED)
  134. return -EINVAL;
  135. if (addr_len < sizeof(*sa))
  136. return -EINVAL;
  137. /* If caller uses non-allowed flag, return error. */
  138. if ((sa->salg_feat & ~allowed) || (sa->salg_mask & ~allowed))
  139. return -EINVAL;
  140. sa->salg_type[sizeof(sa->salg_type) - 1] = 0;
  141. sa->salg_name[sizeof(sa->salg_name) + addr_len - sizeof(*sa) - 1] = 0;
  142. type = alg_get_type(sa->salg_type);
  143. if (IS_ERR(type) && PTR_ERR(type) == -ENOENT) {
  144. request_module("algif-%s", sa->salg_type);
  145. type = alg_get_type(sa->salg_type);
  146. }
  147. if (IS_ERR(type))
  148. return PTR_ERR(type);
  149. private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
  150. if (IS_ERR(private)) {
  151. module_put(type->owner);
  152. return PTR_ERR(private);
  153. }
  154. err = -EBUSY;
  155. lock_sock(sk);
  156. if (ask->refcnt | ask->nokey_refcnt)
  157. goto unlock;
  158. swap(ask->type, type);
  159. swap(ask->private, private);
  160. err = 0;
  161. unlock:
  162. release_sock(sk);
  163. alg_do_release(type, private);
  164. return err;
  165. }
  166. static int alg_setkey(struct sock *sk, char __user *ukey,
  167. unsigned int keylen)
  168. {
  169. struct alg_sock *ask = alg_sk(sk);
  170. const struct af_alg_type *type = ask->type;
  171. u8 *key;
  172. int err;
  173. key = sock_kmalloc(sk, keylen, GFP_KERNEL);
  174. if (!key)
  175. return -ENOMEM;
  176. err = -EFAULT;
  177. if (copy_from_user(key, ukey, keylen))
  178. goto out;
  179. err = type->setkey(ask->private, key, keylen);
  180. out:
  181. sock_kzfree_s(sk, key, keylen);
  182. return err;
  183. }
  184. static int alg_setsockopt(struct socket *sock, int level, int optname,
  185. char __user *optval, unsigned int optlen)
  186. {
  187. struct sock *sk = sock->sk;
  188. struct alg_sock *ask = alg_sk(sk);
  189. const struct af_alg_type *type;
  190. int err = -EBUSY;
  191. lock_sock(sk);
  192. if (ask->refcnt)
  193. goto unlock;
  194. type = ask->type;
  195. err = -ENOPROTOOPT;
  196. if (level != SOL_ALG || !type)
  197. goto unlock;
  198. switch (optname) {
  199. case ALG_SET_KEY:
  200. if (sock->state == SS_CONNECTED)
  201. goto unlock;
  202. if (!type->setkey)
  203. goto unlock;
  204. err = alg_setkey(sk, optval, optlen);
  205. break;
  206. case ALG_SET_AEAD_AUTHSIZE:
  207. if (sock->state == SS_CONNECTED)
  208. goto unlock;
  209. if (!type->setauthsize)
  210. goto unlock;
  211. err = type->setauthsize(ask->private, optlen);
  212. }
  213. unlock:
  214. release_sock(sk);
  215. return err;
  216. }
  217. int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern)
  218. {
  219. struct alg_sock *ask = alg_sk(sk);
  220. const struct af_alg_type *type;
  221. struct sock *sk2;
  222. unsigned int nokey;
  223. int err;
  224. lock_sock(sk);
  225. type = ask->type;
  226. err = -EINVAL;
  227. if (!type)
  228. goto unlock;
  229. sk2 = sk_alloc(sock_net(sk), PF_ALG, GFP_KERNEL, &alg_proto, kern);
  230. err = -ENOMEM;
  231. if (!sk2)
  232. goto unlock;
  233. sock_init_data(newsock, sk2);
  234. security_sock_graft(sk2, newsock);
  235. security_sk_clone(sk, sk2);
  236. err = type->accept(ask->private, sk2);
  237. nokey = err == -ENOKEY;
  238. if (nokey && type->accept_nokey)
  239. err = type->accept_nokey(ask->private, sk2);
  240. if (err)
  241. goto unlock;
  242. sk2->sk_family = PF_ALG;
  243. if (nokey || !ask->refcnt++)
  244. sock_hold(sk);
  245. ask->nokey_refcnt += nokey;
  246. alg_sk(sk2)->parent = sk;
  247. alg_sk(sk2)->type = type;
  248. alg_sk(sk2)->nokey_refcnt = nokey;
  249. newsock->ops = type->ops;
  250. newsock->state = SS_CONNECTED;
  251. if (nokey)
  252. newsock->ops = type->ops_nokey;
  253. err = 0;
  254. unlock:
  255. release_sock(sk);
  256. return err;
  257. }
  258. EXPORT_SYMBOL_GPL(af_alg_accept);
  259. static int alg_accept(struct socket *sock, struct socket *newsock, int flags,
  260. bool kern)
  261. {
  262. return af_alg_accept(sock->sk, newsock, kern);
  263. }
  264. static const struct proto_ops alg_proto_ops = {
  265. .family = PF_ALG,
  266. .owner = THIS_MODULE,
  267. .connect = sock_no_connect,
  268. .socketpair = sock_no_socketpair,
  269. .getname = sock_no_getname,
  270. .ioctl = sock_no_ioctl,
  271. .listen = sock_no_listen,
  272. .shutdown = sock_no_shutdown,
  273. .getsockopt = sock_no_getsockopt,
  274. .mmap = sock_no_mmap,
  275. .sendpage = sock_no_sendpage,
  276. .sendmsg = sock_no_sendmsg,
  277. .recvmsg = sock_no_recvmsg,
  278. .bind = alg_bind,
  279. .release = af_alg_release,
  280. .setsockopt = alg_setsockopt,
  281. .accept = alg_accept,
  282. };
  283. static void alg_sock_destruct(struct sock *sk)
  284. {
  285. struct alg_sock *ask = alg_sk(sk);
  286. alg_do_release(ask->type, ask->private);
  287. }
  288. static int alg_create(struct net *net, struct socket *sock, int protocol,
  289. int kern)
  290. {
  291. struct sock *sk;
  292. int err;
  293. if (sock->type != SOCK_SEQPACKET)
  294. return -ESOCKTNOSUPPORT;
  295. if (protocol != 0)
  296. return -EPROTONOSUPPORT;
  297. err = -ENOMEM;
  298. sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto, kern);
  299. if (!sk)
  300. goto out;
  301. sock->ops = &alg_proto_ops;
  302. sock_init_data(sock, sk);
  303. sk->sk_family = PF_ALG;
  304. sk->sk_destruct = alg_sock_destruct;
  305. return 0;
  306. out:
  307. return err;
  308. }
  309. static const struct net_proto_family alg_family = {
  310. .family = PF_ALG,
  311. .create = alg_create,
  312. .owner = THIS_MODULE,
  313. };
  314. int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
  315. {
  316. size_t off;
  317. ssize_t n;
  318. int npages, i;
  319. n = iov_iter_get_pages(iter, sgl->pages, len, ALG_MAX_PAGES, &off);
  320. if (n < 0)
  321. return n;
  322. npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
  323. if (WARN_ON(npages == 0))
  324. return -EINVAL;
  325. /* Add one extra for linking */
  326. sg_init_table(sgl->sg, npages + 1);
  327. for (i = 0, len = n; i < npages; i++) {
  328. int plen = min_t(int, len, PAGE_SIZE - off);
  329. sg_set_page(sgl->sg + i, sgl->pages[i], plen, off);
  330. off = 0;
  331. len -= plen;
  332. }
  333. sg_mark_end(sgl->sg + npages - 1);
  334. sgl->npages = npages;
  335. return n;
  336. }
  337. EXPORT_SYMBOL_GPL(af_alg_make_sg);
  338. void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new)
  339. {
  340. sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
  341. sg_chain(sgl_prev->sg, sgl_prev->npages + 1, sgl_new->sg);
  342. }
  343. EXPORT_SYMBOL_GPL(af_alg_link_sg);
  344. void af_alg_free_sg(struct af_alg_sgl *sgl)
  345. {
  346. int i;
  347. for (i = 0; i < sgl->npages; i++)
  348. put_page(sgl->pages[i]);
  349. }
  350. EXPORT_SYMBOL_GPL(af_alg_free_sg);
  351. int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con)
  352. {
  353. struct cmsghdr *cmsg;
  354. for_each_cmsghdr(cmsg, msg) {
  355. if (!CMSG_OK(msg, cmsg))
  356. return -EINVAL;
  357. if (cmsg->cmsg_level != SOL_ALG)
  358. continue;
  359. switch (cmsg->cmsg_type) {
  360. case ALG_SET_IV:
  361. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*con->iv)))
  362. return -EINVAL;
  363. con->iv = (void *)CMSG_DATA(cmsg);
  364. if (cmsg->cmsg_len < CMSG_LEN(con->iv->ivlen +
  365. sizeof(*con->iv)))
  366. return -EINVAL;
  367. break;
  368. case ALG_SET_OP:
  369. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  370. return -EINVAL;
  371. con->op = *(u32 *)CMSG_DATA(cmsg);
  372. break;
  373. case ALG_SET_AEAD_ASSOCLEN:
  374. if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
  375. return -EINVAL;
  376. con->aead_assoclen = *(u32 *)CMSG_DATA(cmsg);
  377. break;
  378. default:
  379. return -EINVAL;
  380. }
  381. }
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(af_alg_cmsg_send);
  385. /**
  386. * af_alg_alloc_tsgl - allocate the TX SGL
  387. *
  388. * @sk socket of connection to user space
  389. * @return: 0 upon success, < 0 upon error
  390. */
  391. int af_alg_alloc_tsgl(struct sock *sk)
  392. {
  393. struct alg_sock *ask = alg_sk(sk);
  394. struct af_alg_ctx *ctx = ask->private;
  395. struct af_alg_tsgl *sgl;
  396. struct scatterlist *sg = NULL;
  397. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
  398. if (!list_empty(&ctx->tsgl_list))
  399. sg = sgl->sg;
  400. if (!sg || sgl->cur >= MAX_SGL_ENTS) {
  401. sgl = sock_kmalloc(sk, sizeof(*sgl) +
  402. sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
  403. GFP_KERNEL);
  404. if (!sgl)
  405. return -ENOMEM;
  406. sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
  407. sgl->cur = 0;
  408. if (sg)
  409. sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
  410. list_add_tail(&sgl->list, &ctx->tsgl_list);
  411. }
  412. return 0;
  413. }
  414. EXPORT_SYMBOL_GPL(af_alg_alloc_tsgl);
  415. /**
  416. * aead_count_tsgl - Count number of TX SG entries
  417. *
  418. * The counting starts from the beginning of the SGL to @bytes. If
  419. * an offset is provided, the counting of the SG entries starts at the offset.
  420. *
  421. * @sk socket of connection to user space
  422. * @bytes Count the number of SG entries holding given number of bytes.
  423. * @offset Start the counting of SG entries from the given offset.
  424. * @return Number of TX SG entries found given the constraints
  425. */
  426. unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
  427. {
  428. struct alg_sock *ask = alg_sk(sk);
  429. struct af_alg_ctx *ctx = ask->private;
  430. struct af_alg_tsgl *sgl, *tmp;
  431. unsigned int i;
  432. unsigned int sgl_count = 0;
  433. if (!bytes)
  434. return 0;
  435. list_for_each_entry_safe(sgl, tmp, &ctx->tsgl_list, list) {
  436. struct scatterlist *sg = sgl->sg;
  437. for (i = 0; i < sgl->cur; i++) {
  438. size_t bytes_count;
  439. /* Skip offset */
  440. if (offset >= sg[i].length) {
  441. offset -= sg[i].length;
  442. bytes -= sg[i].length;
  443. continue;
  444. }
  445. bytes_count = sg[i].length - offset;
  446. offset = 0;
  447. sgl_count++;
  448. /* If we have seen requested number of bytes, stop */
  449. if (bytes_count >= bytes)
  450. return sgl_count;
  451. bytes -= bytes_count;
  452. }
  453. }
  454. return sgl_count;
  455. }
  456. EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
  457. /**
  458. * aead_pull_tsgl - Release the specified buffers from TX SGL
  459. *
  460. * If @dst is non-null, reassign the pages to dst. The caller must release
  461. * the pages. If @dst_offset is given only reassign the pages to @dst starting
  462. * at the @dst_offset (byte). The caller must ensure that @dst is large
  463. * enough (e.g. by using af_alg_count_tsgl with the same offset).
  464. *
  465. * @sk socket of connection to user space
  466. * @used Number of bytes to pull from TX SGL
  467. * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
  468. * caller must release the buffers in dst.
  469. * @dst_offset Reassign the TX SGL from given offset. All buffers before
  470. * reaching the offset is released.
  471. */
  472. void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
  473. size_t dst_offset)
  474. {
  475. struct alg_sock *ask = alg_sk(sk);
  476. struct af_alg_ctx *ctx = ask->private;
  477. struct af_alg_tsgl *sgl;
  478. struct scatterlist *sg;
  479. unsigned int i, j = 0;
  480. while (!list_empty(&ctx->tsgl_list)) {
  481. sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
  482. list);
  483. sg = sgl->sg;
  484. for (i = 0; i < sgl->cur; i++) {
  485. size_t plen = min_t(size_t, used, sg[i].length);
  486. struct page *page = sg_page(sg + i);
  487. if (!page)
  488. continue;
  489. /*
  490. * Assumption: caller created af_alg_count_tsgl(len)
  491. * SG entries in dst.
  492. */
  493. if (dst) {
  494. if (dst_offset >= plen) {
  495. /* discard page before offset */
  496. dst_offset -= plen;
  497. } else {
  498. /* reassign page to dst after offset */
  499. get_page(page);
  500. sg_set_page(dst + j, page,
  501. plen - dst_offset,
  502. sg[i].offset + dst_offset);
  503. dst_offset = 0;
  504. j++;
  505. }
  506. }
  507. sg[i].length -= plen;
  508. sg[i].offset += plen;
  509. used -= plen;
  510. ctx->used -= plen;
  511. if (sg[i].length)
  512. return;
  513. put_page(page);
  514. sg_assign_page(sg + i, NULL);
  515. }
  516. list_del(&sgl->list);
  517. sock_kfree_s(sk, sgl, sizeof(*sgl) + sizeof(sgl->sg[0]) *
  518. (MAX_SGL_ENTS + 1));
  519. }
  520. if (!ctx->used)
  521. ctx->merge = 0;
  522. }
  523. EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
  524. /**
  525. * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
  526. *
  527. * @areq Request holding the TX and RX SGL
  528. */
  529. void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
  530. {
  531. struct sock *sk = areq->sk;
  532. struct alg_sock *ask = alg_sk(sk);
  533. struct af_alg_ctx *ctx = ask->private;
  534. struct af_alg_rsgl *rsgl, *tmp;
  535. struct scatterlist *tsgl;
  536. struct scatterlist *sg;
  537. unsigned int i;
  538. list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
  539. atomic_sub(rsgl->sg_num_bytes, &ctx->rcvused);
  540. af_alg_free_sg(&rsgl->sgl);
  541. list_del(&rsgl->list);
  542. if (rsgl != &areq->first_rsgl)
  543. sock_kfree_s(sk, rsgl, sizeof(*rsgl));
  544. }
  545. tsgl = areq->tsgl;
  546. if (tsgl) {
  547. for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
  548. if (!sg_page(sg))
  549. continue;
  550. put_page(sg_page(sg));
  551. }
  552. sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
  553. }
  554. }
  555. EXPORT_SYMBOL_GPL(af_alg_free_areq_sgls);
  556. /**
  557. * af_alg_wait_for_wmem - wait for availability of writable memory
  558. *
  559. * @sk socket of connection to user space
  560. * @flags If MSG_DONTWAIT is set, then only report if function would sleep
  561. * @return 0 when writable memory is available, < 0 upon error
  562. */
  563. int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
  564. {
  565. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  566. int err = -ERESTARTSYS;
  567. long timeout;
  568. if (flags & MSG_DONTWAIT)
  569. return -EAGAIN;
  570. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  571. add_wait_queue(sk_sleep(sk), &wait);
  572. for (;;) {
  573. if (signal_pending(current))
  574. break;
  575. timeout = MAX_SCHEDULE_TIMEOUT;
  576. if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
  577. err = 0;
  578. break;
  579. }
  580. }
  581. remove_wait_queue(sk_sleep(sk), &wait);
  582. return err;
  583. }
  584. EXPORT_SYMBOL_GPL(af_alg_wait_for_wmem);
  585. /**
  586. * af_alg_wmem_wakeup - wakeup caller when writable memory is available
  587. *
  588. * @sk socket of connection to user space
  589. */
  590. void af_alg_wmem_wakeup(struct sock *sk)
  591. {
  592. struct socket_wq *wq;
  593. if (!af_alg_writable(sk))
  594. return;
  595. rcu_read_lock();
  596. wq = rcu_dereference(sk->sk_wq);
  597. if (skwq_has_sleeper(wq))
  598. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  599. EPOLLRDNORM |
  600. EPOLLRDBAND);
  601. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  602. rcu_read_unlock();
  603. }
  604. EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
  605. /**
  606. * af_alg_wait_for_data - wait for availability of TX data
  607. *
  608. * @sk socket of connection to user space
  609. * @flags If MSG_DONTWAIT is set, then only report if function would sleep
  610. * @return 0 when writable memory is available, < 0 upon error
  611. */
  612. int af_alg_wait_for_data(struct sock *sk, unsigned flags)
  613. {
  614. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  615. struct alg_sock *ask = alg_sk(sk);
  616. struct af_alg_ctx *ctx = ask->private;
  617. long timeout;
  618. int err = -ERESTARTSYS;
  619. if (flags & MSG_DONTWAIT)
  620. return -EAGAIN;
  621. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  622. add_wait_queue(sk_sleep(sk), &wait);
  623. for (;;) {
  624. if (signal_pending(current))
  625. break;
  626. timeout = MAX_SCHEDULE_TIMEOUT;
  627. if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more),
  628. &wait)) {
  629. err = 0;
  630. break;
  631. }
  632. }
  633. remove_wait_queue(sk_sleep(sk), &wait);
  634. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  635. return err;
  636. }
  637. EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
  638. /**
  639. * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
  640. *
  641. * @sk socket of connection to user space
  642. */
  643. void af_alg_data_wakeup(struct sock *sk)
  644. {
  645. struct alg_sock *ask = alg_sk(sk);
  646. struct af_alg_ctx *ctx = ask->private;
  647. struct socket_wq *wq;
  648. if (!ctx->used)
  649. return;
  650. rcu_read_lock();
  651. wq = rcu_dereference(sk->sk_wq);
  652. if (skwq_has_sleeper(wq))
  653. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  654. EPOLLRDNORM |
  655. EPOLLRDBAND);
  656. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  657. rcu_read_unlock();
  658. }
  659. EXPORT_SYMBOL_GPL(af_alg_data_wakeup);
  660. /**
  661. * af_alg_sendmsg - implementation of sendmsg system call handler
  662. *
  663. * The sendmsg system call handler obtains the user data and stores it
  664. * in ctx->tsgl_list. This implies allocation of the required numbers of
  665. * struct af_alg_tsgl.
  666. *
  667. * In addition, the ctx is filled with the information sent via CMSG.
  668. *
  669. * @sock socket of connection to user space
  670. * @msg message from user space
  671. * @size size of message from user space
  672. * @ivsize the size of the IV for the cipher operation to verify that the
  673. * user-space-provided IV has the right size
  674. * @return the number of copied data upon success, < 0 upon error
  675. */
  676. int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
  677. unsigned int ivsize)
  678. {
  679. struct sock *sk = sock->sk;
  680. struct alg_sock *ask = alg_sk(sk);
  681. struct af_alg_ctx *ctx = ask->private;
  682. struct af_alg_tsgl *sgl;
  683. struct af_alg_control con = {};
  684. long copied = 0;
  685. bool enc = 0;
  686. bool init = 0;
  687. int err = 0;
  688. if (msg->msg_controllen) {
  689. err = af_alg_cmsg_send(msg, &con);
  690. if (err)
  691. return err;
  692. init = 1;
  693. switch (con.op) {
  694. case ALG_OP_ENCRYPT:
  695. enc = 1;
  696. break;
  697. case ALG_OP_DECRYPT:
  698. enc = 0;
  699. break;
  700. default:
  701. return -EINVAL;
  702. }
  703. if (con.iv && con.iv->ivlen != ivsize)
  704. return -EINVAL;
  705. }
  706. lock_sock(sk);
  707. if (!ctx->more && ctx->used) {
  708. err = -EINVAL;
  709. goto unlock;
  710. }
  711. if (init) {
  712. ctx->enc = enc;
  713. if (con.iv)
  714. memcpy(ctx->iv, con.iv->iv, ivsize);
  715. ctx->aead_assoclen = con.aead_assoclen;
  716. }
  717. while (size) {
  718. struct scatterlist *sg;
  719. size_t len = size;
  720. size_t plen;
  721. /* use the existing memory in an allocated page */
  722. if (ctx->merge) {
  723. sgl = list_entry(ctx->tsgl_list.prev,
  724. struct af_alg_tsgl, list);
  725. sg = sgl->sg + sgl->cur - 1;
  726. len = min_t(size_t, len,
  727. PAGE_SIZE - sg->offset - sg->length);
  728. err = memcpy_from_msg(page_address(sg_page(sg)) +
  729. sg->offset + sg->length,
  730. msg, len);
  731. if (err)
  732. goto unlock;
  733. sg->length += len;
  734. ctx->merge = (sg->offset + sg->length) &
  735. (PAGE_SIZE - 1);
  736. ctx->used += len;
  737. copied += len;
  738. size -= len;
  739. continue;
  740. }
  741. if (!af_alg_writable(sk)) {
  742. err = af_alg_wait_for_wmem(sk, msg->msg_flags);
  743. if (err)
  744. goto unlock;
  745. }
  746. /* allocate a new page */
  747. len = min_t(unsigned long, len, af_alg_sndbuf(sk));
  748. err = af_alg_alloc_tsgl(sk);
  749. if (err)
  750. goto unlock;
  751. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
  752. list);
  753. sg = sgl->sg;
  754. if (sgl->cur)
  755. sg_unmark_end(sg + sgl->cur - 1);
  756. do {
  757. unsigned int i = sgl->cur;
  758. plen = min_t(size_t, len, PAGE_SIZE);
  759. sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
  760. if (!sg_page(sg + i)) {
  761. err = -ENOMEM;
  762. goto unlock;
  763. }
  764. err = memcpy_from_msg(page_address(sg_page(sg + i)),
  765. msg, plen);
  766. if (err) {
  767. __free_page(sg_page(sg + i));
  768. sg_assign_page(sg + i, NULL);
  769. goto unlock;
  770. }
  771. sg[i].length = plen;
  772. len -= plen;
  773. ctx->used += plen;
  774. copied += plen;
  775. size -= plen;
  776. sgl->cur++;
  777. } while (len && sgl->cur < MAX_SGL_ENTS);
  778. if (!size)
  779. sg_mark_end(sg + sgl->cur - 1);
  780. ctx->merge = plen & (PAGE_SIZE - 1);
  781. }
  782. err = 0;
  783. ctx->more = msg->msg_flags & MSG_MORE;
  784. unlock:
  785. af_alg_data_wakeup(sk);
  786. release_sock(sk);
  787. return copied ?: err;
  788. }
  789. EXPORT_SYMBOL_GPL(af_alg_sendmsg);
  790. /**
  791. * af_alg_sendpage - sendpage system call handler
  792. *
  793. * This is a generic implementation of sendpage to fill ctx->tsgl_list.
  794. */
  795. ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
  796. int offset, size_t size, int flags)
  797. {
  798. struct sock *sk = sock->sk;
  799. struct alg_sock *ask = alg_sk(sk);
  800. struct af_alg_ctx *ctx = ask->private;
  801. struct af_alg_tsgl *sgl;
  802. int err = -EINVAL;
  803. if (flags & MSG_SENDPAGE_NOTLAST)
  804. flags |= MSG_MORE;
  805. lock_sock(sk);
  806. if (!ctx->more && ctx->used)
  807. goto unlock;
  808. if (!size)
  809. goto done;
  810. if (!af_alg_writable(sk)) {
  811. err = af_alg_wait_for_wmem(sk, flags);
  812. if (err)
  813. goto unlock;
  814. }
  815. err = af_alg_alloc_tsgl(sk);
  816. if (err)
  817. goto unlock;
  818. ctx->merge = 0;
  819. sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
  820. if (sgl->cur)
  821. sg_unmark_end(sgl->sg + sgl->cur - 1);
  822. sg_mark_end(sgl->sg + sgl->cur);
  823. get_page(page);
  824. sg_set_page(sgl->sg + sgl->cur, page, size, offset);
  825. sgl->cur++;
  826. ctx->used += size;
  827. done:
  828. ctx->more = flags & MSG_MORE;
  829. unlock:
  830. af_alg_data_wakeup(sk);
  831. release_sock(sk);
  832. return err ?: size;
  833. }
  834. EXPORT_SYMBOL_GPL(af_alg_sendpage);
  835. /**
  836. * af_alg_free_resources - release resources required for crypto request
  837. */
  838. void af_alg_free_resources(struct af_alg_async_req *areq)
  839. {
  840. struct sock *sk = areq->sk;
  841. af_alg_free_areq_sgls(areq);
  842. sock_kfree_s(sk, areq, areq->areqlen);
  843. }
  844. EXPORT_SYMBOL_GPL(af_alg_free_resources);
  845. /**
  846. * af_alg_async_cb - AIO callback handler
  847. *
  848. * This handler cleans up the struct af_alg_async_req upon completion of the
  849. * AIO operation.
  850. *
  851. * The number of bytes to be generated with the AIO operation must be set
  852. * in areq->outlen before the AIO callback handler is invoked.
  853. */
  854. void af_alg_async_cb(struct crypto_async_request *_req, int err)
  855. {
  856. struct af_alg_async_req *areq = _req->data;
  857. struct sock *sk = areq->sk;
  858. struct kiocb *iocb = areq->iocb;
  859. unsigned int resultlen;
  860. /* Buffer size written by crypto operation. */
  861. resultlen = areq->outlen;
  862. af_alg_free_resources(areq);
  863. sock_put(sk);
  864. iocb->ki_complete(iocb, err ? err : resultlen, 0);
  865. }
  866. EXPORT_SYMBOL_GPL(af_alg_async_cb);
  867. /**
  868. * af_alg_poll - poll system call handler
  869. */
  870. __poll_t af_alg_poll(struct file *file, struct socket *sock,
  871. poll_table *wait)
  872. {
  873. struct sock *sk = sock->sk;
  874. struct alg_sock *ask = alg_sk(sk);
  875. struct af_alg_ctx *ctx = ask->private;
  876. __poll_t mask;
  877. sock_poll_wait(file, sk_sleep(sk), wait);
  878. mask = 0;
  879. if (!ctx->more || ctx->used)
  880. mask |= EPOLLIN | EPOLLRDNORM;
  881. if (af_alg_writable(sk))
  882. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  883. return mask;
  884. }
  885. EXPORT_SYMBOL_GPL(af_alg_poll);
  886. /**
  887. * af_alg_alloc_areq - allocate struct af_alg_async_req
  888. *
  889. * @sk socket of connection to user space
  890. * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
  891. * @return allocated data structure or ERR_PTR upon error
  892. */
  893. struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
  894. unsigned int areqlen)
  895. {
  896. struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
  897. if (unlikely(!areq))
  898. return ERR_PTR(-ENOMEM);
  899. areq->areqlen = areqlen;
  900. areq->sk = sk;
  901. areq->last_rsgl = NULL;
  902. INIT_LIST_HEAD(&areq->rsgl_list);
  903. areq->tsgl = NULL;
  904. areq->tsgl_entries = 0;
  905. return areq;
  906. }
  907. EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
  908. /**
  909. * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
  910. * operation
  911. *
  912. * @sk socket of connection to user space
  913. * @msg user space message
  914. * @flags flags used to invoke recvmsg with
  915. * @areq instance of the cryptographic request that will hold the RX SGL
  916. * @maxsize maximum number of bytes to be pulled from user space
  917. * @outlen number of bytes in the RX SGL
  918. * @return 0 on success, < 0 upon error
  919. */
  920. int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
  921. struct af_alg_async_req *areq, size_t maxsize,
  922. size_t *outlen)
  923. {
  924. struct alg_sock *ask = alg_sk(sk);
  925. struct af_alg_ctx *ctx = ask->private;
  926. size_t len = 0;
  927. while (maxsize > len && msg_data_left(msg)) {
  928. struct af_alg_rsgl *rsgl;
  929. size_t seglen;
  930. int err;
  931. /* limit the amount of readable buffers */
  932. if (!af_alg_readable(sk))
  933. break;
  934. seglen = min_t(size_t, (maxsize - len),
  935. msg_data_left(msg));
  936. if (list_empty(&areq->rsgl_list)) {
  937. rsgl = &areq->first_rsgl;
  938. } else {
  939. rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
  940. if (unlikely(!rsgl))
  941. return -ENOMEM;
  942. }
  943. rsgl->sgl.npages = 0;
  944. list_add_tail(&rsgl->list, &areq->rsgl_list);
  945. /* make one iovec available as scatterlist */
  946. err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
  947. if (err < 0)
  948. return err;
  949. /* chain the new scatterlist with previous one */
  950. if (areq->last_rsgl)
  951. af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
  952. areq->last_rsgl = rsgl;
  953. len += err;
  954. atomic_add(err, &ctx->rcvused);
  955. rsgl->sg_num_bytes = err;
  956. iov_iter_advance(&msg->msg_iter, err);
  957. }
  958. *outlen = len;
  959. return 0;
  960. }
  961. EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
  962. static int __init af_alg_init(void)
  963. {
  964. int err = proto_register(&alg_proto, 0);
  965. if (err)
  966. goto out;
  967. err = sock_register(&alg_family);
  968. if (err != 0)
  969. goto out_unregister_proto;
  970. out:
  971. return err;
  972. out_unregister_proto:
  973. proto_unregister(&alg_proto);
  974. goto out;
  975. }
  976. static void __exit af_alg_exit(void)
  977. {
  978. sock_unregister(PF_ALG);
  979. proto_unregister(&alg_proto);
  980. }
  981. module_init(af_alg_init);
  982. module_exit(af_alg_exit);
  983. MODULE_LICENSE("GPL");
  984. MODULE_ALIAS_NETPROTO(AF_ALG);