esp6.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 <linux/err.h>
  27. #include <linux/module.h>
  28. #include <net/ip.h>
  29. #include <net/xfrm.h>
  30. #include <net/esp.h>
  31. #include <asm/scatterlist.h>
  32. #include <linux/crypto.h>
  33. #include <linux/kernel.h>
  34. #include <linux/pfkeyv2.h>
  35. #include <linux/random.h>
  36. #include <net/icmp.h>
  37. #include <net/ipv6.h>
  38. #include <net/protocol.h>
  39. #include <linux/icmpv6.h>
  40. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  41. {
  42. int err;
  43. int hdr_len;
  44. struct ipv6hdr *top_iph;
  45. struct ipv6_esp_hdr *esph;
  46. struct crypto_blkcipher *tfm;
  47. struct blkcipher_desc desc;
  48. struct esp_data *esp;
  49. struct sk_buff *trailer;
  50. int blksize;
  51. int clen;
  52. int alen;
  53. int nfrags;
  54. esp = x->data;
  55. hdr_len = skb->h.raw - skb->data +
  56. sizeof(*esph) + esp->conf.ivlen;
  57. /* Strip IP+ESP header. */
  58. __skb_pull(skb, hdr_len);
  59. /* Now skb is pure payload to encrypt */
  60. err = -ENOMEM;
  61. /* Round to block size */
  62. clen = skb->len;
  63. alen = esp->auth.icv_trunc_len;
  64. tfm = esp->conf.tfm;
  65. desc.tfm = tfm;
  66. desc.flags = 0;
  67. blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
  68. clen = ALIGN(clen + 2, blksize);
  69. if (esp->conf.padlen)
  70. clen = ALIGN(clen, esp->conf.padlen);
  71. if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0) {
  72. goto error;
  73. }
  74. /* Fill padding... */
  75. do {
  76. int i;
  77. for (i=0; i<clen-skb->len - 2; i++)
  78. *(u8*)(trailer->tail + i) = i+1;
  79. } while (0);
  80. *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
  81. pskb_put(skb, trailer, clen - skb->len);
  82. top_iph = (struct ipv6hdr *)__skb_push(skb, hdr_len);
  83. esph = (struct ipv6_esp_hdr *)skb->h.raw;
  84. top_iph->payload_len = htons(skb->len + alen - sizeof(*top_iph));
  85. *(u8*)(trailer->tail - 1) = *skb->nh.raw;
  86. *skb->nh.raw = IPPROTO_ESP;
  87. esph->spi = x->id.spi;
  88. esph->seq_no = htonl(++x->replay.oseq);
  89. xfrm_aevent_doreplay(x);
  90. if (esp->conf.ivlen)
  91. crypto_blkcipher_set_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
  92. do {
  93. struct scatterlist *sg = &esp->sgbuf[0];
  94. if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
  95. sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
  96. if (!sg)
  97. goto error;
  98. }
  99. skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
  100. err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
  101. if (unlikely(sg != &esp->sgbuf[0]))
  102. kfree(sg);
  103. } while (0);
  104. if (unlikely(err))
  105. goto error;
  106. if (esp->conf.ivlen) {
  107. memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen);
  108. crypto_blkcipher_get_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
  109. }
  110. if (esp->auth.icv_full_len) {
  111. err = esp_mac_digest(esp, skb, (u8 *)esph - skb->data,
  112. sizeof(*esph) + esp->conf.ivlen + clen);
  113. memcpy(pskb_put(skb, trailer, alen), esp->auth.work_icv, alen);
  114. }
  115. error:
  116. return err;
  117. }
  118. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  119. {
  120. struct ipv6hdr *iph;
  121. struct ipv6_esp_hdr *esph;
  122. struct esp_data *esp = x->data;
  123. struct crypto_blkcipher *tfm = esp->conf.tfm;
  124. struct blkcipher_desc desc = { .tfm = tfm };
  125. struct sk_buff *trailer;
  126. int blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
  127. int alen = esp->auth.icv_trunc_len;
  128. int elen = skb->len - sizeof(struct ipv6_esp_hdr) - esp->conf.ivlen - alen;
  129. int hdr_len = skb->h.raw - skb->nh.raw;
  130. int nfrags;
  131. int ret = 0;
  132. if (!pskb_may_pull(skb, sizeof(struct ipv6_esp_hdr))) {
  133. ret = -EINVAL;
  134. goto out;
  135. }
  136. if (elen <= 0 || (elen & (blksize-1))) {
  137. ret = -EINVAL;
  138. goto out;
  139. }
  140. /* If integrity check is required, do this. */
  141. if (esp->auth.icv_full_len) {
  142. u8 sum[alen];
  143. ret = esp_mac_digest(esp, skb, 0, skb->len - alen);
  144. if (ret)
  145. goto out;
  146. if (skb_copy_bits(skb, skb->len - alen, sum, alen))
  147. BUG();
  148. if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
  149. x->stats.integrity_failed++;
  150. ret = -EINVAL;
  151. goto out;
  152. }
  153. }
  154. if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
  155. ret = -EINVAL;
  156. goto out;
  157. }
  158. skb->ip_summed = CHECKSUM_NONE;
  159. esph = (struct ipv6_esp_hdr*)skb->data;
  160. iph = skb->nh.ipv6h;
  161. /* Get ivec. This can be wrong, check against another impls. */
  162. if (esp->conf.ivlen)
  163. crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen);
  164. {
  165. u8 nexthdr[2];
  166. struct scatterlist *sg = &esp->sgbuf[0];
  167. u8 padlen;
  168. if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
  169. sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
  170. if (!sg) {
  171. ret = -ENOMEM;
  172. goto out;
  173. }
  174. }
  175. skb_to_sgvec(skb, sg, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen, elen);
  176. ret = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
  177. if (unlikely(sg != &esp->sgbuf[0]))
  178. kfree(sg);
  179. if (unlikely(ret))
  180. goto out;
  181. if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
  182. BUG();
  183. padlen = nexthdr[0];
  184. if (padlen+2 >= elen) {
  185. LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen);
  186. ret = -EINVAL;
  187. goto out;
  188. }
  189. /* ... check padding bits here. Silly. :-) */
  190. pskb_trim(skb, skb->len - alen - padlen - 2);
  191. ret = nexthdr[1];
  192. }
  193. skb->h.raw = __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen) - hdr_len;
  194. out:
  195. return ret;
  196. }
  197. static u32 esp6_get_max_size(struct xfrm_state *x, int mtu)
  198. {
  199. struct esp_data *esp = x->data;
  200. u32 blksize = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4);
  201. if (x->props.mode) {
  202. mtu = ALIGN(mtu + 2, blksize);
  203. } else {
  204. /* The worst case. */
  205. u32 padsize = ((blksize - 1) & 7) + 1;
  206. mtu = ALIGN(mtu + 2, padsize) + blksize - padsize;
  207. }
  208. if (esp->conf.padlen)
  209. mtu = ALIGN(mtu, esp->conf.padlen);
  210. return mtu + x->props.header_len + esp->auth.icv_trunc_len;
  211. }
  212. static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  213. int type, int code, int offset, __u32 info)
  214. {
  215. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  216. struct ipv6_esp_hdr *esph = (struct ipv6_esp_hdr*)(skb->data+offset);
  217. struct xfrm_state *x;
  218. if (type != ICMPV6_DEST_UNREACH &&
  219. type != ICMPV6_PKT_TOOBIG)
  220. return;
  221. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
  222. if (!x)
  223. return;
  224. printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
  225. ntohl(esph->spi), NIP6(iph->daddr));
  226. xfrm_state_put(x);
  227. }
  228. static void esp6_destroy(struct xfrm_state *x)
  229. {
  230. struct esp_data *esp = x->data;
  231. if (!esp)
  232. return;
  233. crypto_free_blkcipher(esp->conf.tfm);
  234. esp->conf.tfm = NULL;
  235. kfree(esp->conf.ivec);
  236. esp->conf.ivec = NULL;
  237. crypto_free_hash(esp->auth.tfm);
  238. esp->auth.tfm = NULL;
  239. kfree(esp->auth.work_icv);
  240. esp->auth.work_icv = NULL;
  241. kfree(esp);
  242. }
  243. static int esp6_init_state(struct xfrm_state *x)
  244. {
  245. struct esp_data *esp = NULL;
  246. struct crypto_blkcipher *tfm;
  247. /* null auth and encryption can have zero length keys */
  248. if (x->aalg) {
  249. if (x->aalg->alg_key_len > 512)
  250. goto error;
  251. }
  252. if (x->ealg == NULL)
  253. goto error;
  254. if (x->encap)
  255. goto error;
  256. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  257. if (esp == NULL)
  258. return -ENOMEM;
  259. if (x->aalg) {
  260. struct xfrm_algo_desc *aalg_desc;
  261. struct crypto_hash *hash;
  262. esp->auth.key = x->aalg->alg_key;
  263. esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
  264. hash = crypto_alloc_hash(x->aalg->alg_name, 0,
  265. CRYPTO_ALG_ASYNC);
  266. if (IS_ERR(hash))
  267. goto error;
  268. esp->auth.tfm = hash;
  269. if (crypto_hash_setkey(hash, esp->auth.key, esp->auth.key_len))
  270. goto error;
  271. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  272. BUG_ON(!aalg_desc);
  273. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  274. crypto_hash_digestsize(hash)) {
  275. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  276. x->aalg->alg_name,
  277. crypto_hash_digestsize(hash),
  278. aalg_desc->uinfo.auth.icv_fullbits/8);
  279. goto error;
  280. }
  281. esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  282. esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  283. esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
  284. if (!esp->auth.work_icv)
  285. goto error;
  286. }
  287. esp->conf.key = x->ealg->alg_key;
  288. esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
  289. tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC);
  290. if (IS_ERR(tfm))
  291. goto error;
  292. esp->conf.tfm = tfm;
  293. esp->conf.ivlen = crypto_blkcipher_ivsize(tfm);
  294. esp->conf.padlen = 0;
  295. if (esp->conf.ivlen) {
  296. esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
  297. if (unlikely(esp->conf.ivec == NULL))
  298. goto error;
  299. get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
  300. }
  301. if (crypto_blkcipher_setkey(tfm, esp->conf.key, esp->conf.key_len))
  302. goto error;
  303. x->props.header_len = sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
  304. if (x->props.mode)
  305. x->props.header_len += sizeof(struct ipv6hdr);
  306. x->data = esp;
  307. return 0;
  308. error:
  309. x->data = esp;
  310. esp6_destroy(x);
  311. x->data = NULL;
  312. return -EINVAL;
  313. }
  314. static struct xfrm_type esp6_type =
  315. {
  316. .description = "ESP6",
  317. .owner = THIS_MODULE,
  318. .proto = IPPROTO_ESP,
  319. .init_state = esp6_init_state,
  320. .destructor = esp6_destroy,
  321. .get_max_size = esp6_get_max_size,
  322. .input = esp6_input,
  323. .output = esp6_output
  324. };
  325. static struct inet6_protocol esp6_protocol = {
  326. .handler = xfrm6_rcv,
  327. .err_handler = esp6_err,
  328. .flags = INET6_PROTO_NOPOLICY,
  329. };
  330. static int __init esp6_init(void)
  331. {
  332. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  333. printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
  334. return -EAGAIN;
  335. }
  336. if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
  337. printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
  338. xfrm_unregister_type(&esp6_type, AF_INET6);
  339. return -EAGAIN;
  340. }
  341. return 0;
  342. }
  343. static void __exit esp6_fini(void)
  344. {
  345. if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
  346. printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
  347. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  348. printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
  349. }
  350. module_init(esp6_init);
  351. module_exit(esp6_fini);
  352. MODULE_LICENSE("GPL");