xfrm_algo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * xfrm algorithm interface
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.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. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pfkeyv2.h>
  14. #include <linux/crypto.h>
  15. #include <net/xfrm.h>
  16. #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
  17. #include <net/ah.h>
  18. #endif
  19. #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
  20. #include <net/esp.h>
  21. #endif
  22. #include <asm/scatterlist.h>
  23. /*
  24. * Algorithms supported by IPsec. These entries contain properties which
  25. * are used in key negotiation and xfrm processing, and are used to verify
  26. * that instantiated crypto transforms have correct parameters for IPsec
  27. * purposes.
  28. */
  29. static struct xfrm_algo_desc aalg_list[] = {
  30. {
  31. .name = "digest_null",
  32. .uinfo = {
  33. .auth = {
  34. .icv_truncbits = 0,
  35. .icv_fullbits = 0,
  36. }
  37. },
  38. .desc = {
  39. .sadb_alg_id = SADB_X_AALG_NULL,
  40. .sadb_alg_ivlen = 0,
  41. .sadb_alg_minbits = 0,
  42. .sadb_alg_maxbits = 0
  43. }
  44. },
  45. {
  46. .name = "md5",
  47. .uinfo = {
  48. .auth = {
  49. .icv_truncbits = 96,
  50. .icv_fullbits = 128,
  51. }
  52. },
  53. .desc = {
  54. .sadb_alg_id = SADB_AALG_MD5HMAC,
  55. .sadb_alg_ivlen = 0,
  56. .sadb_alg_minbits = 128,
  57. .sadb_alg_maxbits = 128
  58. }
  59. },
  60. {
  61. .name = "sha1",
  62. .uinfo = {
  63. .auth = {
  64. .icv_truncbits = 96,
  65. .icv_fullbits = 160,
  66. }
  67. },
  68. .desc = {
  69. .sadb_alg_id = SADB_AALG_SHA1HMAC,
  70. .sadb_alg_ivlen = 0,
  71. .sadb_alg_minbits = 160,
  72. .sadb_alg_maxbits = 160
  73. }
  74. },
  75. {
  76. .name = "sha256",
  77. .uinfo = {
  78. .auth = {
  79. .icv_truncbits = 96,
  80. .icv_fullbits = 256,
  81. }
  82. },
  83. .desc = {
  84. .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
  85. .sadb_alg_ivlen = 0,
  86. .sadb_alg_minbits = 256,
  87. .sadb_alg_maxbits = 256
  88. }
  89. },
  90. {
  91. .name = "ripemd160",
  92. .uinfo = {
  93. .auth = {
  94. .icv_truncbits = 96,
  95. .icv_fullbits = 160,
  96. }
  97. },
  98. .desc = {
  99. .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
  100. .sadb_alg_ivlen = 0,
  101. .sadb_alg_minbits = 160,
  102. .sadb_alg_maxbits = 160
  103. }
  104. },
  105. };
  106. static struct xfrm_algo_desc ealg_list[] = {
  107. {
  108. .name = "ecb(cipher_null)",
  109. .compat = "cipher_null",
  110. .uinfo = {
  111. .encr = {
  112. .blockbits = 8,
  113. .defkeybits = 0,
  114. }
  115. },
  116. .desc = {
  117. .sadb_alg_id = SADB_EALG_NULL,
  118. .sadb_alg_ivlen = 0,
  119. .sadb_alg_minbits = 0,
  120. .sadb_alg_maxbits = 0
  121. }
  122. },
  123. {
  124. .name = "cbc(des)",
  125. .compat = "des",
  126. .uinfo = {
  127. .encr = {
  128. .blockbits = 64,
  129. .defkeybits = 64,
  130. }
  131. },
  132. .desc = {
  133. .sadb_alg_id = SADB_EALG_DESCBC,
  134. .sadb_alg_ivlen = 8,
  135. .sadb_alg_minbits = 64,
  136. .sadb_alg_maxbits = 64
  137. }
  138. },
  139. {
  140. .name = "cbc(des3_ede)",
  141. .compat = "des3_ede",
  142. .uinfo = {
  143. .encr = {
  144. .blockbits = 64,
  145. .defkeybits = 192,
  146. }
  147. },
  148. .desc = {
  149. .sadb_alg_id = SADB_EALG_3DESCBC,
  150. .sadb_alg_ivlen = 8,
  151. .sadb_alg_minbits = 192,
  152. .sadb_alg_maxbits = 192
  153. }
  154. },
  155. {
  156. .name = "cbc(cast128)",
  157. .compat = "cast128",
  158. .uinfo = {
  159. .encr = {
  160. .blockbits = 64,
  161. .defkeybits = 128,
  162. }
  163. },
  164. .desc = {
  165. .sadb_alg_id = SADB_X_EALG_CASTCBC,
  166. .sadb_alg_ivlen = 8,
  167. .sadb_alg_minbits = 40,
  168. .sadb_alg_maxbits = 128
  169. }
  170. },
  171. {
  172. .name = "cbc(blowfish)",
  173. .compat = "blowfish",
  174. .uinfo = {
  175. .encr = {
  176. .blockbits = 64,
  177. .defkeybits = 128,
  178. }
  179. },
  180. .desc = {
  181. .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
  182. .sadb_alg_ivlen = 8,
  183. .sadb_alg_minbits = 40,
  184. .sadb_alg_maxbits = 448
  185. }
  186. },
  187. {
  188. .name = "cbc(aes)",
  189. .compat = "aes",
  190. .uinfo = {
  191. .encr = {
  192. .blockbits = 128,
  193. .defkeybits = 128,
  194. }
  195. },
  196. .desc = {
  197. .sadb_alg_id = SADB_X_EALG_AESCBC,
  198. .sadb_alg_ivlen = 8,
  199. .sadb_alg_minbits = 128,
  200. .sadb_alg_maxbits = 256
  201. }
  202. },
  203. {
  204. .name = "cbc(serpent)",
  205. .compat = "serpent",
  206. .uinfo = {
  207. .encr = {
  208. .blockbits = 128,
  209. .defkeybits = 128,
  210. }
  211. },
  212. .desc = {
  213. .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
  214. .sadb_alg_ivlen = 8,
  215. .sadb_alg_minbits = 128,
  216. .sadb_alg_maxbits = 256,
  217. }
  218. },
  219. {
  220. .name = "cbc(twofish)",
  221. .compat = "twofish",
  222. .uinfo = {
  223. .encr = {
  224. .blockbits = 128,
  225. .defkeybits = 128,
  226. }
  227. },
  228. .desc = {
  229. .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
  230. .sadb_alg_ivlen = 8,
  231. .sadb_alg_minbits = 128,
  232. .sadb_alg_maxbits = 256
  233. }
  234. },
  235. };
  236. static struct xfrm_algo_desc calg_list[] = {
  237. {
  238. .name = "deflate",
  239. .uinfo = {
  240. .comp = {
  241. .threshold = 90,
  242. }
  243. },
  244. .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
  245. },
  246. {
  247. .name = "lzs",
  248. .uinfo = {
  249. .comp = {
  250. .threshold = 90,
  251. }
  252. },
  253. .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
  254. },
  255. {
  256. .name = "lzjh",
  257. .uinfo = {
  258. .comp = {
  259. .threshold = 50,
  260. }
  261. },
  262. .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
  263. },
  264. };
  265. static inline int aalg_entries(void)
  266. {
  267. return ARRAY_SIZE(aalg_list);
  268. }
  269. static inline int ealg_entries(void)
  270. {
  271. return ARRAY_SIZE(ealg_list);
  272. }
  273. static inline int calg_entries(void)
  274. {
  275. return ARRAY_SIZE(calg_list);
  276. }
  277. /* Todo: generic iterators */
  278. struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
  279. {
  280. int i;
  281. for (i = 0; i < aalg_entries(); i++) {
  282. if (aalg_list[i].desc.sadb_alg_id == alg_id) {
  283. if (aalg_list[i].available)
  284. return &aalg_list[i];
  285. else
  286. break;
  287. }
  288. }
  289. return NULL;
  290. }
  291. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
  292. struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
  293. {
  294. int i;
  295. for (i = 0; i < ealg_entries(); i++) {
  296. if (ealg_list[i].desc.sadb_alg_id == alg_id) {
  297. if (ealg_list[i].available)
  298. return &ealg_list[i];
  299. else
  300. break;
  301. }
  302. }
  303. return NULL;
  304. }
  305. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
  306. struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
  307. {
  308. int i;
  309. for (i = 0; i < calg_entries(); i++) {
  310. if (calg_list[i].desc.sadb_alg_id == alg_id) {
  311. if (calg_list[i].available)
  312. return &calg_list[i];
  313. else
  314. break;
  315. }
  316. }
  317. return NULL;
  318. }
  319. EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
  320. static struct xfrm_algo_desc *xfrm_get_byname(struct xfrm_algo_desc *list,
  321. int entries, char *name,
  322. int probe)
  323. {
  324. int i, status;
  325. if (!name)
  326. return NULL;
  327. for (i = 0; i < entries; i++) {
  328. if (strcmp(name, list[i].name) &&
  329. (!list[i].compat || strcmp(name, list[i].compat)))
  330. continue;
  331. if (list[i].available)
  332. return &list[i];
  333. if (!probe)
  334. break;
  335. status = crypto_alg_available(name, 0);
  336. if (!status)
  337. break;
  338. list[i].available = status;
  339. return &list[i];
  340. }
  341. return NULL;
  342. }
  343. struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
  344. {
  345. return xfrm_get_byname(aalg_list, aalg_entries(), name, probe);
  346. }
  347. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
  348. struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
  349. {
  350. return xfrm_get_byname(ealg_list, ealg_entries(), name, probe);
  351. }
  352. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
  353. struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
  354. {
  355. return xfrm_get_byname(calg_list, calg_entries(), name, probe);
  356. }
  357. EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
  358. struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
  359. {
  360. if (idx >= aalg_entries())
  361. return NULL;
  362. return &aalg_list[idx];
  363. }
  364. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
  365. struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
  366. {
  367. if (idx >= ealg_entries())
  368. return NULL;
  369. return &ealg_list[idx];
  370. }
  371. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
  372. /*
  373. * Probe for the availability of crypto algorithms, and set the available
  374. * flag for any algorithms found on the system. This is typically called by
  375. * pfkey during userspace SA add, update or register.
  376. */
  377. void xfrm_probe_algs(void)
  378. {
  379. #ifdef CONFIG_CRYPTO
  380. int i, status;
  381. BUG_ON(in_softirq());
  382. for (i = 0; i < aalg_entries(); i++) {
  383. status = crypto_alg_available(aalg_list[i].name, 0);
  384. if (aalg_list[i].available != status)
  385. aalg_list[i].available = status;
  386. }
  387. for (i = 0; i < ealg_entries(); i++) {
  388. status = crypto_alg_available(ealg_list[i].name, 0);
  389. if (ealg_list[i].available != status)
  390. ealg_list[i].available = status;
  391. }
  392. for (i = 0; i < calg_entries(); i++) {
  393. status = crypto_alg_available(calg_list[i].name, 0);
  394. if (calg_list[i].available != status)
  395. calg_list[i].available = status;
  396. }
  397. #endif
  398. }
  399. EXPORT_SYMBOL_GPL(xfrm_probe_algs);
  400. int xfrm_count_auth_supported(void)
  401. {
  402. int i, n;
  403. for (i = 0, n = 0; i < aalg_entries(); i++)
  404. if (aalg_list[i].available)
  405. n++;
  406. return n;
  407. }
  408. EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
  409. int xfrm_count_enc_supported(void)
  410. {
  411. int i, n;
  412. for (i = 0, n = 0; i < ealg_entries(); i++)
  413. if (ealg_list[i].available)
  414. n++;
  415. return n;
  416. }
  417. EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
  418. /* Move to common area: it is shared with AH. */
  419. void skb_icv_walk(const struct sk_buff *skb, struct crypto_tfm *tfm,
  420. int offset, int len, icv_update_fn_t icv_update)
  421. {
  422. int start = skb_headlen(skb);
  423. int i, copy = start - offset;
  424. struct scatterlist sg;
  425. /* Checksum header. */
  426. if (copy > 0) {
  427. if (copy > len)
  428. copy = len;
  429. sg.page = virt_to_page(skb->data + offset);
  430. sg.offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
  431. sg.length = copy;
  432. icv_update(tfm, &sg, 1);
  433. if ((len -= copy) == 0)
  434. return;
  435. offset += copy;
  436. }
  437. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  438. int end;
  439. BUG_TRAP(start <= offset + len);
  440. end = start + skb_shinfo(skb)->frags[i].size;
  441. if ((copy = end - offset) > 0) {
  442. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  443. if (copy > len)
  444. copy = len;
  445. sg.page = frag->page;
  446. sg.offset = frag->page_offset + offset-start;
  447. sg.length = copy;
  448. icv_update(tfm, &sg, 1);
  449. if (!(len -= copy))
  450. return;
  451. offset += copy;
  452. }
  453. start = end;
  454. }
  455. if (skb_shinfo(skb)->frag_list) {
  456. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  457. for (; list; list = list->next) {
  458. int end;
  459. BUG_TRAP(start <= offset + len);
  460. end = start + list->len;
  461. if ((copy = end - offset) > 0) {
  462. if (copy > len)
  463. copy = len;
  464. skb_icv_walk(list, tfm, offset-start, copy, icv_update);
  465. if ((len -= copy) == 0)
  466. return;
  467. offset += copy;
  468. }
  469. start = end;
  470. }
  471. }
  472. BUG_ON(len);
  473. }
  474. EXPORT_SYMBOL_GPL(skb_icv_walk);
  475. #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
  476. /* Looking generic it is not used in another places. */
  477. int
  478. skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  479. {
  480. int start = skb_headlen(skb);
  481. int i, copy = start - offset;
  482. int elt = 0;
  483. if (copy > 0) {
  484. if (copy > len)
  485. copy = len;
  486. sg[elt].page = virt_to_page(skb->data + offset);
  487. sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
  488. sg[elt].length = copy;
  489. elt++;
  490. if ((len -= copy) == 0)
  491. return elt;
  492. offset += copy;
  493. }
  494. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  495. int end;
  496. BUG_TRAP(start <= offset + len);
  497. end = start + skb_shinfo(skb)->frags[i].size;
  498. if ((copy = end - offset) > 0) {
  499. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  500. if (copy > len)
  501. copy = len;
  502. sg[elt].page = frag->page;
  503. sg[elt].offset = frag->page_offset+offset-start;
  504. sg[elt].length = copy;
  505. elt++;
  506. if (!(len -= copy))
  507. return elt;
  508. offset += copy;
  509. }
  510. start = end;
  511. }
  512. if (skb_shinfo(skb)->frag_list) {
  513. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  514. for (; list; list = list->next) {
  515. int end;
  516. BUG_TRAP(start <= offset + len);
  517. end = start + list->len;
  518. if ((copy = end - offset) > 0) {
  519. if (copy > len)
  520. copy = len;
  521. elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
  522. if ((len -= copy) == 0)
  523. return elt;
  524. offset += copy;
  525. }
  526. start = end;
  527. }
  528. }
  529. BUG_ON(len);
  530. return elt;
  531. }
  532. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  533. /* Check that skb data bits are writable. If they are not, copy data
  534. * to newly created private area. If "tailbits" is given, make sure that
  535. * tailbits bytes beyond current end of skb are writable.
  536. *
  537. * Returns amount of elements of scatterlist to load for subsequent
  538. * transformations and pointer to writable trailer skb.
  539. */
  540. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  541. {
  542. int copyflag;
  543. int elt;
  544. struct sk_buff *skb1, **skb_p;
  545. /* If skb is cloned or its head is paged, reallocate
  546. * head pulling out all the pages (pages are considered not writable
  547. * at the moment even if they are anonymous).
  548. */
  549. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  550. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  551. return -ENOMEM;
  552. /* Easy case. Most of packets will go this way. */
  553. if (!skb_shinfo(skb)->frag_list) {
  554. /* A little of trouble, not enough of space for trailer.
  555. * This should not happen, when stack is tuned to generate
  556. * good frames. OK, on miss we reallocate and reserve even more
  557. * space, 128 bytes is fair. */
  558. if (skb_tailroom(skb) < tailbits &&
  559. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  560. return -ENOMEM;
  561. /* Voila! */
  562. *trailer = skb;
  563. return 1;
  564. }
  565. /* Misery. We are in troubles, going to mincer fragments... */
  566. elt = 1;
  567. skb_p = &skb_shinfo(skb)->frag_list;
  568. copyflag = 0;
  569. while ((skb1 = *skb_p) != NULL) {
  570. int ntail = 0;
  571. /* The fragment is partially pulled by someone,
  572. * this can happen on input. Copy it and everything
  573. * after it. */
  574. if (skb_shared(skb1))
  575. copyflag = 1;
  576. /* If the skb is the last, worry about trailer. */
  577. if (skb1->next == NULL && tailbits) {
  578. if (skb_shinfo(skb1)->nr_frags ||
  579. skb_shinfo(skb1)->frag_list ||
  580. skb_tailroom(skb1) < tailbits)
  581. ntail = tailbits + 128;
  582. }
  583. if (copyflag ||
  584. skb_cloned(skb1) ||
  585. ntail ||
  586. skb_shinfo(skb1)->nr_frags ||
  587. skb_shinfo(skb1)->frag_list) {
  588. struct sk_buff *skb2;
  589. /* Fuck, we are miserable poor guys... */
  590. if (ntail == 0)
  591. skb2 = skb_copy(skb1, GFP_ATOMIC);
  592. else
  593. skb2 = skb_copy_expand(skb1,
  594. skb_headroom(skb1),
  595. ntail,
  596. GFP_ATOMIC);
  597. if (unlikely(skb2 == NULL))
  598. return -ENOMEM;
  599. if (skb1->sk)
  600. skb_set_owner_w(skb2, skb1->sk);
  601. /* Looking around. Are we still alive?
  602. * OK, link new skb, drop old one */
  603. skb2->next = skb1->next;
  604. *skb_p = skb2;
  605. kfree_skb(skb1);
  606. skb1 = skb2;
  607. }
  608. elt++;
  609. *trailer = skb1;
  610. skb_p = &skb1->next;
  611. }
  612. return elt;
  613. }
  614. EXPORT_SYMBOL_GPL(skb_cow_data);
  615. void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
  616. {
  617. if (tail != skb) {
  618. skb->data_len += len;
  619. skb->len += len;
  620. }
  621. return skb_put(tail, len);
  622. }
  623. EXPORT_SYMBOL_GPL(pskb_put);
  624. #endif