esp6.c 11 KB

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