/kern_2.6.32/net/ipv6/esp6.c

http://omnia2droid.googlecode.com/ · C · 594 lines · 445 code · 110 blank · 39 comment · 52 complexity · b904a76061439e0ae5673aa657c4a5e7 MD5 · raw file

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