esp6.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright (C)2002 USAGI/WIDE Project
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Authors
  19. *
  20. * Mitsuru KANDA @USAGI : IPv6 Support
  21. * Kazunori MIYAZAWA @USAGI :
  22. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  23. *
  24. * This file is derived from net/ipv4/esp.c
  25. */
  26. #include <crypto/aead.h>
  27. #include <crypto/authenc.h>
  28. #include <linux/err.h>
  29. #include <linux/module.h>
  30. #include <net/ip.h>
  31. #include <net/xfrm.h>
  32. #include <net/esp.h>
  33. #include <linux/scatterlist.h>
  34. #include <linux/kernel.h>
  35. #include <linux/pfkeyv2.h>
  36. #include <linux/random.h>
  37. #include <linux/slab.h>
  38. #include <linux/spinlock.h>
  39. #include <net/icmp.h>
  40. #include <net/ipv6.h>
  41. #include <net/protocol.h>
  42. #include <linux/icmpv6.h>
  43. struct esp_skb_cb {
  44. struct xfrm_skb_cb xfrm;
  45. void *tmp;
  46. };
  47. #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
  48. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu);
  49. /*
  50. * Allocate an AEAD request structure with extra space for SG and IV.
  51. *
  52. * For alignment considerations the IV is placed at the front, followed
  53. * by the request and finally the SG list.
  54. *
  55. * TODO: Use spare space in skb for this where possible.
  56. */
  57. static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags)
  58. {
  59. unsigned int len;
  60. len = crypto_aead_ivsize(aead);
  61. if (len) {
  62. len += crypto_aead_alignmask(aead) &
  63. ~(crypto_tfm_ctx_alignment() - 1);
  64. len = ALIGN(len, crypto_tfm_ctx_alignment());
  65. }
  66. len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
  67. len = ALIGN(len, __alignof__(struct scatterlist));
  68. len += sizeof(struct scatterlist) * nfrags;
  69. return kmalloc(len, GFP_ATOMIC);
  70. }
  71. static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp)
  72. {
  73. return crypto_aead_ivsize(aead) ?
  74. PTR_ALIGN((u8 *)tmp, crypto_aead_alignmask(aead) + 1) : tmp;
  75. }
  76. static inline struct aead_givcrypt_request *esp_tmp_givreq(
  77. struct crypto_aead *aead, u8 *iv)
  78. {
  79. struct aead_givcrypt_request *req;
  80. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  81. crypto_tfm_ctx_alignment());
  82. aead_givcrypt_set_tfm(req, aead);
  83. return req;
  84. }
  85. static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
  86. {
  87. struct aead_request *req;
  88. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  89. crypto_tfm_ctx_alignment());
  90. aead_request_set_tfm(req, aead);
  91. return req;
  92. }
  93. static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
  94. struct aead_request *req)
  95. {
  96. return (void *)ALIGN((unsigned long)(req + 1) +
  97. crypto_aead_reqsize(aead),
  98. __alignof__(struct scatterlist));
  99. }
  100. static inline struct scatterlist *esp_givreq_sg(
  101. struct crypto_aead *aead, struct aead_givcrypt_request *req)
  102. {
  103. return (void *)ALIGN((unsigned long)(req + 1) +
  104. crypto_aead_reqsize(aead),
  105. __alignof__(struct scatterlist));
  106. }
  107. static void esp_output_done(struct crypto_async_request *base, int err)
  108. {
  109. struct sk_buff *skb = base->data;
  110. kfree(ESP_SKB_CB(skb)->tmp);
  111. xfrm_output_resume(skb, err);
  112. }
  113. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  114. {
  115. int err;
  116. struct ip_esp_hdr *esph;
  117. struct crypto_aead *aead;
  118. struct aead_givcrypt_request *req;
  119. struct scatterlist *sg;
  120. struct scatterlist *asg;
  121. struct sk_buff *trailer;
  122. void *tmp;
  123. int blksize;
  124. int clen;
  125. int alen;
  126. int plen;
  127. int tfclen;
  128. int nfrags;
  129. u8 *iv;
  130. u8 *tail;
  131. struct esp_data *esp = x->data;
  132. /* skb is pure payload to encrypt */
  133. err = -ENOMEM;
  134. aead = esp->aead;
  135. alen = crypto_aead_authsize(aead);
  136. tfclen = 0;
  137. if (x->tfcpad) {
  138. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  139. u32 padto;
  140. padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
  141. if (skb->len < padto)
  142. tfclen = padto - skb->len;
  143. }
  144. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  145. clen = ALIGN(skb->len + 2 + tfclen, blksize);
  146. if (esp->padlen)
  147. clen = ALIGN(clen, esp->padlen);
  148. plen = clen - skb->len - tfclen;
  149. err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
  150. if (err < 0)
  151. goto error;
  152. nfrags = err;
  153. tmp = esp_alloc_tmp(aead, nfrags + 1);
  154. if (!tmp)
  155. goto error;
  156. iv = esp_tmp_iv(aead, tmp);
  157. req = esp_tmp_givreq(aead, iv);
  158. asg = esp_givreq_sg(aead, req);
  159. sg = asg + 1;
  160. /* Fill padding... */
  161. tail = skb_tail_pointer(trailer);
  162. if (tfclen) {
  163. memset(tail, 0, tfclen);
  164. tail += tfclen;
  165. }
  166. do {
  167. int i;
  168. for (i = 0; i < plen - 2; i++)
  169. tail[i] = i + 1;
  170. } while (0);
  171. tail[plen - 2] = plen - 2;
  172. tail[plen - 1] = *skb_mac_header(skb);
  173. pskb_put(skb, trailer, clen - skb->len + alen);
  174. skb_push(skb, -skb_network_offset(skb));
  175. esph = ip_esp_hdr(skb);
  176. *skb_mac_header(skb) = IPPROTO_ESP;
  177. esph->spi = x->id.spi;
  178. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
  179. sg_init_table(sg, nfrags);
  180. skb_to_sgvec(skb, sg,
  181. esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
  182. clen + alen);
  183. sg_init_one(asg, esph, sizeof(*esph));
  184. aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
  185. aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
  186. aead_givcrypt_set_assoc(req, asg, sizeof(*esph));
  187. aead_givcrypt_set_giv(req, esph->enc_data,
  188. XFRM_SKB_CB(skb)->seq.output);
  189. ESP_SKB_CB(skb)->tmp = tmp;
  190. err = crypto_aead_givencrypt(req);
  191. if (err == -EINPROGRESS)
  192. goto error;
  193. if (err == -EBUSY)
  194. err = NET_XMIT_DROP;
  195. kfree(tmp);
  196. error:
  197. return err;
  198. }
  199. static int esp_input_done2(struct sk_buff *skb, int err)
  200. {
  201. struct xfrm_state *x = xfrm_input_state(skb);
  202. struct esp_data *esp = x->data;
  203. struct crypto_aead *aead = esp->aead;
  204. int alen = crypto_aead_authsize(aead);
  205. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  206. int elen = skb->len - hlen;
  207. int hdr_len = skb_network_header_len(skb);
  208. int padlen;
  209. u8 nexthdr[2];
  210. kfree(ESP_SKB_CB(skb)->tmp);
  211. if (unlikely(err))
  212. goto out;
  213. if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
  214. BUG();
  215. err = -EINVAL;
  216. padlen = nexthdr[0];
  217. if (padlen + 2 + alen >= elen) {
  218. LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage "
  219. "padlen=%d, elen=%d\n", padlen + 2, elen - alen);
  220. goto out;
  221. }
  222. /* ... check padding bits here. Silly. :-) */
  223. pskb_trim(skb, skb->len - alen - padlen - 2);
  224. __skb_pull(skb, hlen);
  225. skb_set_transport_header(skb, -hdr_len);
  226. err = nexthdr[1];
  227. /* RFC4303: Drop dummy packets without any error */
  228. if (err == IPPROTO_NONE)
  229. err = -EINVAL;
  230. out:
  231. return err;
  232. }
  233. static void esp_input_done(struct crypto_async_request *base, int err)
  234. {
  235. struct sk_buff *skb = base->data;
  236. xfrm_input_resume(skb, esp_input_done2(skb, err));
  237. }
  238. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  239. {
  240. struct ip_esp_hdr *esph;
  241. struct esp_data *esp = x->data;
  242. struct crypto_aead *aead = esp->aead;
  243. struct aead_request *req;
  244. struct sk_buff *trailer;
  245. int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
  246. int nfrags;
  247. int ret = 0;
  248. void *tmp;
  249. u8 *iv;
  250. struct scatterlist *sg;
  251. struct scatterlist *asg;
  252. if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) {
  253. ret = -EINVAL;
  254. goto out;
  255. }
  256. if (elen <= 0) {
  257. ret = -EINVAL;
  258. goto out;
  259. }
  260. if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
  261. ret = -EINVAL;
  262. goto out;
  263. }
  264. ret = -ENOMEM;
  265. tmp = esp_alloc_tmp(aead, nfrags + 1);
  266. if (!tmp)
  267. goto out;
  268. ESP_SKB_CB(skb)->tmp = tmp;
  269. iv = esp_tmp_iv(aead, tmp);
  270. req = esp_tmp_req(aead, iv);
  271. asg = esp_req_sg(aead, req);
  272. sg = asg + 1;
  273. skb->ip_summed = CHECKSUM_NONE;
  274. esph = (struct ip_esp_hdr *)skb->data;
  275. /* Get ivec. This can be wrong, check against another impls. */
  276. iv = esph->enc_data;
  277. sg_init_table(sg, nfrags);
  278. skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
  279. sg_init_one(asg, esph, sizeof(*esph));
  280. aead_request_set_callback(req, 0, esp_input_done, skb);
  281. aead_request_set_crypt(req, sg, sg, elen, iv);
  282. aead_request_set_assoc(req, asg, sizeof(*esph));
  283. ret = crypto_aead_decrypt(req);
  284. if (ret == -EINPROGRESS)
  285. goto out;
  286. ret = esp_input_done2(skb, ret);
  287. out:
  288. return ret;
  289. }
  290. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  291. {
  292. struct esp_data *esp = x->data;
  293. u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
  294. u32 align = max_t(u32, blksize, esp->padlen);
  295. u32 rem;
  296. mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
  297. rem = mtu & (align - 1);
  298. mtu &= ~(align - 1);
  299. if (x->props.mode != XFRM_MODE_TUNNEL) {
  300. u32 padsize = ((blksize - 1) & 7) + 1;
  301. mtu -= blksize - padsize;
  302. mtu += min_t(u32, blksize - padsize, rem);
  303. }
  304. return mtu - 2;
  305. }
  306. static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  307. u8 type, u8 code, int offset, __be32 info)
  308. {
  309. struct net *net = dev_net(skb->dev);
  310. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  311. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  312. struct xfrm_state *x;
  313. if (type != ICMPV6_DEST_UNREACH &&
  314. type != ICMPV6_PKT_TOOBIG)
  315. return;
  316. x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
  317. if (!x)
  318. return;
  319. printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%pI6\n",
  320. ntohl(esph->spi), &iph->daddr);
  321. xfrm_state_put(x);
  322. }
  323. static void esp6_destroy(struct xfrm_state *x)
  324. {
  325. struct esp_data *esp = x->data;
  326. if (!esp)
  327. return;
  328. crypto_free_aead(esp->aead);
  329. kfree(esp);
  330. }
  331. static int esp_init_aead(struct xfrm_state *x)
  332. {
  333. struct esp_data *esp = x->data;
  334. struct crypto_aead *aead;
  335. int err;
  336. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  337. err = PTR_ERR(aead);
  338. if (IS_ERR(aead))
  339. goto error;
  340. esp->aead = aead;
  341. err = crypto_aead_setkey(aead, x->aead->alg_key,
  342. (x->aead->alg_key_len + 7) / 8);
  343. if (err)
  344. goto error;
  345. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  346. if (err)
  347. goto error;
  348. error:
  349. return err;
  350. }
  351. static int esp_init_authenc(struct xfrm_state *x)
  352. {
  353. struct esp_data *esp = x->data;
  354. struct crypto_aead *aead;
  355. struct crypto_authenc_key_param *param;
  356. struct rtattr *rta;
  357. char *key;
  358. char *p;
  359. char authenc_name[CRYPTO_MAX_ALG_NAME];
  360. unsigned int keylen;
  361. int err;
  362. err = -EINVAL;
  363. if (x->ealg == NULL)
  364. goto error;
  365. err = -ENAMETOOLONG;
  366. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
  367. x->aalg ? x->aalg->alg_name : "digest_null",
  368. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  369. goto error;
  370. aead = crypto_alloc_aead(authenc_name, 0, 0);
  371. err = PTR_ERR(aead);
  372. if (IS_ERR(aead))
  373. goto error;
  374. esp->aead = aead;
  375. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  376. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  377. err = -ENOMEM;
  378. key = kmalloc(keylen, GFP_KERNEL);
  379. if (!key)
  380. goto error;
  381. p = key;
  382. rta = (void *)p;
  383. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  384. rta->rta_len = RTA_LENGTH(sizeof(*param));
  385. param = RTA_DATA(rta);
  386. p += RTA_SPACE(sizeof(*param));
  387. if (x->aalg) {
  388. struct xfrm_algo_desc *aalg_desc;
  389. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  390. p += (x->aalg->alg_key_len + 7) / 8;
  391. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  392. BUG_ON(!aalg_desc);
  393. err = -EINVAL;
  394. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  395. crypto_aead_authsize(aead)) {
  396. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  397. x->aalg->alg_name,
  398. crypto_aead_authsize(aead),
  399. aalg_desc->uinfo.auth.icv_fullbits/8);
  400. goto free_key;
  401. }
  402. err = crypto_aead_setauthsize(
  403. aead, x->aalg->alg_trunc_len / 8);
  404. if (err)
  405. goto free_key;
  406. }
  407. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  408. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  409. err = crypto_aead_setkey(aead, key, keylen);
  410. free_key:
  411. kfree(key);
  412. error:
  413. return err;
  414. }
  415. static int esp6_init_state(struct xfrm_state *x)
  416. {
  417. struct esp_data *esp;
  418. struct crypto_aead *aead;
  419. u32 align;
  420. int err;
  421. if (x->encap)
  422. return -EINVAL;
  423. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  424. if (esp == NULL)
  425. return -ENOMEM;
  426. x->data = esp;
  427. if (x->aead)
  428. err = esp_init_aead(x);
  429. else
  430. err = esp_init_authenc(x);
  431. if (err)
  432. goto error;
  433. aead = esp->aead;
  434. esp->padlen = 0;
  435. x->props.header_len = sizeof(struct ip_esp_hdr) +
  436. crypto_aead_ivsize(aead);
  437. switch (x->props.mode) {
  438. case XFRM_MODE_BEET:
  439. if (x->sel.family != AF_INET6)
  440. x->props.header_len += IPV4_BEET_PHMAXLEN +
  441. (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
  442. break;
  443. case XFRM_MODE_TRANSPORT:
  444. break;
  445. case XFRM_MODE_TUNNEL:
  446. x->props.header_len += sizeof(struct ipv6hdr);
  447. break;
  448. default:
  449. goto error;
  450. }
  451. align = ALIGN(crypto_aead_blocksize(aead), 4);
  452. if (esp->padlen)
  453. align = max_t(u32, align, esp->padlen);
  454. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  455. error:
  456. return err;
  457. }
  458. static const struct xfrm_type esp6_type =
  459. {
  460. .description = "ESP6",
  461. .owner = THIS_MODULE,
  462. .proto = IPPROTO_ESP,
  463. .flags = XFRM_TYPE_REPLAY_PROT,
  464. .init_state = esp6_init_state,
  465. .destructor = esp6_destroy,
  466. .get_mtu = esp6_get_mtu,
  467. .input = esp6_input,
  468. .output = esp6_output,
  469. .hdr_offset = xfrm6_find_1stfragopt,
  470. };
  471. static const struct inet6_protocol esp6_protocol = {
  472. .handler = xfrm6_rcv,
  473. .err_handler = esp6_err,
  474. .flags = INET6_PROTO_NOPOLICY,
  475. };
  476. static int __init esp6_init(void)
  477. {
  478. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  479. printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
  480. return -EAGAIN;
  481. }
  482. if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
  483. printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
  484. xfrm_unregister_type(&esp6_type, AF_INET6);
  485. return -EAGAIN;
  486. }
  487. return 0;
  488. }
  489. static void __exit esp6_fini(void)
  490. {
  491. if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
  492. printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
  493. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  494. printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
  495. }
  496. module_init(esp6_init);
  497. module_exit(esp6_fini);
  498. MODULE_LICENSE("GPL");
  499. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);