esp6.c 10 KB

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