esp6.c 10 KB

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