esp6.c 11 KB

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