esp6.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. int type, int code, int offset, __be32 info)
  293. {
  294. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  295. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  296. struct xfrm_state *x;
  297. if (type != ICMPV6_DEST_UNREACH &&
  298. type != ICMPV6_PKT_TOOBIG)
  299. return;
  300. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
  301. if (!x)
  302. return;
  303. printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
  304. ntohl(esph->spi), NIP6(iph->daddr));
  305. xfrm_state_put(x);
  306. }
  307. static void esp6_destroy(struct xfrm_state *x)
  308. {
  309. struct esp_data *esp = x->data;
  310. if (!esp)
  311. return;
  312. crypto_free_aead(esp->aead);
  313. kfree(esp);
  314. }
  315. static int esp_init_aead(struct xfrm_state *x)
  316. {
  317. struct esp_data *esp = x->data;
  318. struct crypto_aead *aead;
  319. int err;
  320. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  321. err = PTR_ERR(aead);
  322. if (IS_ERR(aead))
  323. goto error;
  324. esp->aead = aead;
  325. err = crypto_aead_setkey(aead, x->aead->alg_key,
  326. (x->aead->alg_key_len + 7) / 8);
  327. if (err)
  328. goto error;
  329. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  330. if (err)
  331. goto error;
  332. error:
  333. return err;
  334. }
  335. static int esp_init_authenc(struct xfrm_state *x)
  336. {
  337. struct esp_data *esp = x->data;
  338. struct crypto_aead *aead;
  339. struct crypto_authenc_key_param *param;
  340. struct rtattr *rta;
  341. char *key;
  342. char *p;
  343. char authenc_name[CRYPTO_MAX_ALG_NAME];
  344. unsigned int keylen;
  345. int err;
  346. err = -EINVAL;
  347. if (x->ealg == NULL)
  348. goto error;
  349. err = -ENAMETOOLONG;
  350. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
  351. x->aalg ? x->aalg->alg_name : "digest_null",
  352. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  353. goto error;
  354. aead = crypto_alloc_aead(authenc_name, 0, 0);
  355. err = PTR_ERR(aead);
  356. if (IS_ERR(aead))
  357. goto error;
  358. esp->aead = aead;
  359. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  360. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  361. err = -ENOMEM;
  362. key = kmalloc(keylen, GFP_KERNEL);
  363. if (!key)
  364. goto error;
  365. p = key;
  366. rta = (void *)p;
  367. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  368. rta->rta_len = RTA_LENGTH(sizeof(*param));
  369. param = RTA_DATA(rta);
  370. p += RTA_SPACE(sizeof(*param));
  371. if (x->aalg) {
  372. struct xfrm_algo_desc *aalg_desc;
  373. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  374. p += (x->aalg->alg_key_len + 7) / 8;
  375. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  376. BUG_ON(!aalg_desc);
  377. err = -EINVAL;
  378. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  379. crypto_aead_authsize(aead)) {
  380. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  381. x->aalg->alg_name,
  382. crypto_aead_authsize(aead),
  383. aalg_desc->uinfo.auth.icv_fullbits/8);
  384. goto free_key;
  385. }
  386. err = crypto_aead_setauthsize(
  387. aead, aalg_desc->uinfo.auth.icv_truncbits / 8);
  388. if (err)
  389. goto free_key;
  390. }
  391. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  392. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  393. err = crypto_aead_setkey(aead, key, keylen);
  394. free_key:
  395. kfree(key);
  396. error:
  397. return err;
  398. }
  399. static int esp6_init_state(struct xfrm_state *x)
  400. {
  401. struct esp_data *esp;
  402. struct crypto_aead *aead;
  403. u32 align;
  404. int err;
  405. if (x->encap)
  406. return -EINVAL;
  407. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  408. if (esp == NULL)
  409. return -ENOMEM;
  410. x->data = esp;
  411. if (x->aead)
  412. err = esp_init_aead(x);
  413. else
  414. err = esp_init_authenc(x);
  415. if (err)
  416. goto error;
  417. aead = esp->aead;
  418. esp->padlen = 0;
  419. x->props.header_len = sizeof(struct ip_esp_hdr) +
  420. crypto_aead_ivsize(aead);
  421. switch (x->props.mode) {
  422. case XFRM_MODE_BEET:
  423. case XFRM_MODE_TRANSPORT:
  424. break;
  425. case XFRM_MODE_TUNNEL:
  426. x->props.header_len += sizeof(struct ipv6hdr);
  427. break;
  428. default:
  429. goto error;
  430. }
  431. align = ALIGN(crypto_aead_blocksize(aead), 4);
  432. if (esp->padlen)
  433. align = max_t(u32, align, esp->padlen);
  434. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  435. error:
  436. return err;
  437. }
  438. static const struct xfrm_type esp6_type =
  439. {
  440. .description = "ESP6",
  441. .owner = THIS_MODULE,
  442. .proto = IPPROTO_ESP,
  443. .flags = XFRM_TYPE_REPLAY_PROT,
  444. .init_state = esp6_init_state,
  445. .destructor = esp6_destroy,
  446. .get_mtu = esp6_get_mtu,
  447. .input = esp6_input,
  448. .output = esp6_output,
  449. .hdr_offset = xfrm6_find_1stfragopt,
  450. };
  451. static struct inet6_protocol esp6_protocol = {
  452. .handler = xfrm6_rcv,
  453. .err_handler = esp6_err,
  454. .flags = INET6_PROTO_NOPOLICY,
  455. };
  456. static int __init esp6_init(void)
  457. {
  458. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  459. printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
  460. return -EAGAIN;
  461. }
  462. if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
  463. printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
  464. xfrm_unregister_type(&esp6_type, AF_INET6);
  465. return -EAGAIN;
  466. }
  467. return 0;
  468. }
  469. static void __exit esp6_fini(void)
  470. {
  471. if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
  472. printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
  473. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  474. printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
  475. }
  476. module_init(esp6_init);
  477. module_exit(esp6_fini);
  478. MODULE_LICENSE("GPL");
  479. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);