esp6.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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);
  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, XFRM_SKB_CB(skb)->seq);
  173. ESP_SKB_CB(skb)->tmp = tmp;
  174. err = crypto_aead_givencrypt(req);
  175. if (err == -EINPROGRESS)
  176. goto error;
  177. if (err == -EBUSY)
  178. err = NET_XMIT_DROP;
  179. kfree(tmp);
  180. error:
  181. return err;
  182. }
  183. static int esp_input_done2(struct sk_buff *skb, int err)
  184. {
  185. struct xfrm_state *x = xfrm_input_state(skb);
  186. struct esp_data *esp = x->data;
  187. struct crypto_aead *aead = esp->aead;
  188. int alen = crypto_aead_authsize(aead);
  189. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  190. int elen = skb->len - hlen;
  191. int hdr_len = skb_network_header_len(skb);
  192. int padlen;
  193. u8 nexthdr[2];
  194. kfree(ESP_SKB_CB(skb)->tmp);
  195. if (unlikely(err))
  196. goto out;
  197. if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
  198. BUG();
  199. err = -EINVAL;
  200. padlen = nexthdr[0];
  201. if (padlen + 2 + alen >= elen) {
  202. LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage "
  203. "padlen=%d, elen=%d\n", padlen + 2, elen - alen);
  204. goto out;
  205. }
  206. /* ... check padding bits here. Silly. :-) */
  207. pskb_trim(skb, skb->len - alen - padlen - 2);
  208. __skb_pull(skb, hlen);
  209. skb_set_transport_header(skb, -hdr_len);
  210. err = nexthdr[1];
  211. /* RFC4303: Drop dummy packets without any error */
  212. if (err == IPPROTO_NONE)
  213. err = -EINVAL;
  214. out:
  215. return err;
  216. }
  217. static void esp_input_done(struct crypto_async_request *base, int err)
  218. {
  219. struct sk_buff *skb = base->data;
  220. xfrm_input_resume(skb, esp_input_done2(skb, err));
  221. }
  222. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  223. {
  224. struct ip_esp_hdr *esph;
  225. struct esp_data *esp = x->data;
  226. struct crypto_aead *aead = esp->aead;
  227. struct aead_request *req;
  228. struct sk_buff *trailer;
  229. int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
  230. int nfrags;
  231. int ret = 0;
  232. void *tmp;
  233. u8 *iv;
  234. struct scatterlist *sg;
  235. struct scatterlist *asg;
  236. if (!pskb_may_pull(skb, sizeof(*esph))) {
  237. ret = -EINVAL;
  238. goto out;
  239. }
  240. if (elen <= 0) {
  241. ret = -EINVAL;
  242. goto out;
  243. }
  244. if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
  245. ret = -EINVAL;
  246. goto out;
  247. }
  248. ret = -ENOMEM;
  249. tmp = esp_alloc_tmp(aead, nfrags + 1);
  250. if (!tmp)
  251. goto out;
  252. ESP_SKB_CB(skb)->tmp = tmp;
  253. iv = esp_tmp_iv(aead, tmp);
  254. req = esp_tmp_req(aead, iv);
  255. asg = esp_req_sg(aead, req);
  256. sg = asg + 1;
  257. skb->ip_summed = CHECKSUM_NONE;
  258. esph = (struct ip_esp_hdr *)skb->data;
  259. /* Get ivec. This can be wrong, check against another impls. */
  260. iv = esph->enc_data;
  261. sg_init_table(sg, nfrags);
  262. skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
  263. sg_init_one(asg, esph, sizeof(*esph));
  264. aead_request_set_callback(req, 0, esp_input_done, skb);
  265. aead_request_set_crypt(req, sg, sg, elen, iv);
  266. aead_request_set_assoc(req, asg, sizeof(*esph));
  267. ret = crypto_aead_decrypt(req);
  268. if (ret == -EINPROGRESS)
  269. goto out;
  270. ret = esp_input_done2(skb, ret);
  271. out:
  272. return ret;
  273. }
  274. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  275. {
  276. struct esp_data *esp = x->data;
  277. u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
  278. u32 align = max_t(u32, blksize, esp->padlen);
  279. u32 rem;
  280. mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
  281. rem = mtu & (align - 1);
  282. mtu &= ~(align - 1);
  283. if (x->props.mode != XFRM_MODE_TUNNEL) {
  284. u32 padsize = ((blksize - 1) & 7) + 1;
  285. mtu -= blksize - padsize;
  286. mtu += min_t(u32, blksize - padsize, rem);
  287. }
  288. return mtu - 2;
  289. }
  290. static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  291. int type, int code, int offset, __be32 info)
  292. {
  293. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  294. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  295. struct xfrm_state *x;
  296. if (type != ICMPV6_DEST_UNREACH &&
  297. type != ICMPV6_PKT_TOOBIG)
  298. return;
  299. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
  300. if (!x)
  301. return;
  302. printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
  303. ntohl(esph->spi), NIP6(iph->daddr));
  304. xfrm_state_put(x);
  305. }
  306. static void esp6_destroy(struct xfrm_state *x)
  307. {
  308. struct esp_data *esp = x->data;
  309. if (!esp)
  310. return;
  311. crypto_free_aead(esp->aead);
  312. kfree(esp);
  313. }
  314. static int esp_init_aead(struct xfrm_state *x)
  315. {
  316. struct esp_data *esp = x->data;
  317. struct crypto_aead *aead;
  318. int err;
  319. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  320. err = PTR_ERR(aead);
  321. if (IS_ERR(aead))
  322. goto error;
  323. esp->aead = aead;
  324. err = crypto_aead_setkey(aead, x->aead->alg_key,
  325. (x->aead->alg_key_len + 7) / 8);
  326. if (err)
  327. goto error;
  328. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  329. if (err)
  330. goto error;
  331. error:
  332. return err;
  333. }
  334. static int esp_init_authenc(struct xfrm_state *x)
  335. {
  336. struct esp_data *esp = x->data;
  337. struct crypto_aead *aead;
  338. struct crypto_authenc_key_param *param;
  339. struct rtattr *rta;
  340. char *key;
  341. char *p;
  342. char authenc_name[CRYPTO_MAX_ALG_NAME];
  343. unsigned int keylen;
  344. int err;
  345. err = -EINVAL;
  346. if (x->ealg == NULL)
  347. goto error;
  348. err = -ENAMETOOLONG;
  349. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
  350. x->aalg ? x->aalg->alg_name : "digest_null",
  351. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  352. goto error;
  353. aead = crypto_alloc_aead(authenc_name, 0, 0);
  354. err = PTR_ERR(aead);
  355. if (IS_ERR(aead))
  356. goto error;
  357. esp->aead = aead;
  358. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  359. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  360. err = -ENOMEM;
  361. key = kmalloc(keylen, GFP_KERNEL);
  362. if (!key)
  363. goto error;
  364. p = key;
  365. rta = (void *)p;
  366. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  367. rta->rta_len = RTA_LENGTH(sizeof(*param));
  368. param = RTA_DATA(rta);
  369. p += RTA_SPACE(sizeof(*param));
  370. if (x->aalg) {
  371. struct xfrm_algo_desc *aalg_desc;
  372. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  373. p += (x->aalg->alg_key_len + 7) / 8;
  374. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  375. BUG_ON(!aalg_desc);
  376. err = -EINVAL;
  377. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  378. crypto_aead_authsize(aead)) {
  379. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  380. x->aalg->alg_name,
  381. crypto_aead_authsize(aead),
  382. aalg_desc->uinfo.auth.icv_fullbits/8);
  383. goto free_key;
  384. }
  385. err = crypto_aead_setauthsize(
  386. aead, aalg_desc->uinfo.auth.icv_truncbits / 8);
  387. if (err)
  388. goto free_key;
  389. }
  390. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  391. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  392. err = crypto_aead_setkey(aead, key, keylen);
  393. free_key:
  394. kfree(key);
  395. error:
  396. return err;
  397. }
  398. static int esp6_init_state(struct xfrm_state *x)
  399. {
  400. struct esp_data *esp;
  401. struct crypto_aead *aead;
  402. u32 align;
  403. int err;
  404. if (x->encap)
  405. return -EINVAL;
  406. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  407. if (esp == NULL)
  408. return -ENOMEM;
  409. x->data = esp;
  410. if (x->aead)
  411. err = esp_init_aead(x);
  412. else
  413. err = esp_init_authenc(x);
  414. if (err)
  415. goto error;
  416. aead = esp->aead;
  417. esp->padlen = 0;
  418. x->props.header_len = sizeof(struct ip_esp_hdr) +
  419. crypto_aead_ivsize(aead);
  420. switch (x->props.mode) {
  421. case XFRM_MODE_BEET:
  422. case XFRM_MODE_TRANSPORT:
  423. break;
  424. case XFRM_MODE_TUNNEL:
  425. x->props.header_len += sizeof(struct ipv6hdr);
  426. break;
  427. default:
  428. goto error;
  429. }
  430. align = ALIGN(crypto_aead_blocksize(aead), 4);
  431. if (esp->padlen)
  432. align = max_t(u32, align, esp->padlen);
  433. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  434. error:
  435. return err;
  436. }
  437. static const struct xfrm_type esp6_type =
  438. {
  439. .description = "ESP6",
  440. .owner = THIS_MODULE,
  441. .proto = IPPROTO_ESP,
  442. .flags = XFRM_TYPE_REPLAY_PROT,
  443. .init_state = esp6_init_state,
  444. .destructor = esp6_destroy,
  445. .get_mtu = esp6_get_mtu,
  446. .input = esp6_input,
  447. .output = esp6_output,
  448. .hdr_offset = xfrm6_find_1stfragopt,
  449. };
  450. static struct inet6_protocol esp6_protocol = {
  451. .handler = xfrm6_rcv,
  452. .err_handler = esp6_err,
  453. .flags = INET6_PROTO_NOPOLICY,
  454. };
  455. static int __init esp6_init(void)
  456. {
  457. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  458. printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
  459. return -EAGAIN;
  460. }
  461. if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
  462. printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
  463. xfrm_unregister_type(&esp6_type, AF_INET6);
  464. return -EAGAIN;
  465. }
  466. return 0;
  467. }
  468. static void __exit esp6_fini(void)
  469. {
  470. if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
  471. printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
  472. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  473. printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
  474. }
  475. module_init(esp6_init);
  476. module_exit(esp6_fini);
  477. MODULE_LICENSE("GPL");
  478. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);