esp6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <linux/scatterlist.h>
  32. #include <linux/crypto.h>
  33. #include <linux/kernel.h>
  34. #include <linux/pfkeyv2.h>
  35. #include <linux/random.h>
  36. #include <linux/spinlock.h>
  37. #include <net/icmp.h>
  38. #include <net/ipv6.h>
  39. #include <net/protocol.h>
  40. #include <linux/icmpv6.h>
  41. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  42. {
  43. int err;
  44. struct ip_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. /* skb is pure payload to encrypt */
  55. err = -ENOMEM;
  56. /* Round to block size */
  57. clen = skb->len;
  58. alen = esp->auth.icv_trunc_len;
  59. tfm = esp->conf.tfm;
  60. desc.tfm = tfm;
  61. desc.flags = 0;
  62. blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
  63. clen = ALIGN(clen + 2, blksize);
  64. if (esp->conf.padlen)
  65. clen = ALIGN(clen, esp->conf.padlen);
  66. if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0) {
  67. goto error;
  68. }
  69. /* Fill padding... */
  70. tail = skb_tail_pointer(trailer);
  71. do {
  72. int i;
  73. for (i=0; i<clen-skb->len - 2; i++)
  74. tail[i] = i + 1;
  75. } while (0);
  76. tail[clen-skb->len - 2] = (clen - skb->len) - 2;
  77. pskb_put(skb, trailer, clen - skb->len);
  78. skb_push(skb, -skb_network_offset(skb));
  79. esph = ip_esp_hdr(skb);
  80. *(skb_tail_pointer(trailer) - 1) = *skb_mac_header(skb);
  81. *skb_mac_header(skb) = IPPROTO_ESP;
  82. esph->spi = x->id.spi;
  83. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
  84. spin_lock_bh(&x->lock);
  85. if (esp->conf.ivlen) {
  86. if (unlikely(!esp->conf.ivinitted)) {
  87. get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
  88. esp->conf.ivinitted = 1;
  89. }
  90. crypto_blkcipher_set_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
  91. }
  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 unlock;
  98. }
  99. sg_init_table(sg, nfrags);
  100. skb_to_sgvec(skb, sg,
  101. esph->enc_data +
  102. esp->conf.ivlen -
  103. skb->data, clen);
  104. err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
  105. if (unlikely(sg != &esp->sgbuf[0]))
  106. kfree(sg);
  107. } while (0);
  108. if (unlikely(err))
  109. goto unlock;
  110. if (esp->conf.ivlen) {
  111. memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen);
  112. crypto_blkcipher_get_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
  113. }
  114. if (esp->auth.icv_full_len) {
  115. err = esp_mac_digest(esp, skb, (u8 *)esph - skb->data,
  116. sizeof(*esph) + esp->conf.ivlen + clen);
  117. memcpy(pskb_put(skb, trailer, alen), esp->auth.work_icv, alen);
  118. }
  119. unlock:
  120. spin_unlock_bh(&x->lock);
  121. error:
  122. return err;
  123. }
  124. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  125. {
  126. struct ipv6hdr *iph;
  127. struct ip_esp_hdr *esph;
  128. struct esp_data *esp = x->data;
  129. struct crypto_blkcipher *tfm = esp->conf.tfm;
  130. struct blkcipher_desc desc = { .tfm = tfm };
  131. struct sk_buff *trailer;
  132. int blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
  133. int alen = esp->auth.icv_trunc_len;
  134. int elen = skb->len - sizeof(*esph) - esp->conf.ivlen - alen;
  135. int hdr_len = skb_network_header_len(skb);
  136. int nfrags;
  137. int ret = 0;
  138. if (!pskb_may_pull(skb, sizeof(*esph))) {
  139. ret = -EINVAL;
  140. goto out;
  141. }
  142. if (elen <= 0 || (elen & (blksize-1))) {
  143. ret = -EINVAL;
  144. goto out;
  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. spin_lock(&x->lock);
  152. /* If integrity check is required, do this. */
  153. if (esp->auth.icv_full_len) {
  154. u8 sum[alen];
  155. ret = esp_mac_digest(esp, skb, 0, skb->len - alen);
  156. if (ret)
  157. goto unlock;
  158. if (skb_copy_bits(skb, skb->len - alen, sum, alen))
  159. BUG();
  160. if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
  161. xfrm_audit_state_icvfail(x, skb, IPPROTO_ESP);
  162. ret = -EBADMSG;
  163. goto unlock;
  164. }
  165. }
  166. esph = (struct ip_esp_hdr *)skb->data;
  167. iph = ipv6_hdr(skb);
  168. /* Get ivec. This can be wrong, check against another impls. */
  169. if (esp->conf.ivlen)
  170. crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen);
  171. {
  172. struct scatterlist *sg = &esp->sgbuf[0];
  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 unlock;
  178. }
  179. }
  180. sg_init_table(sg, nfrags);
  181. skb_to_sgvec(skb, sg,
  182. sizeof(*esph) + esp->conf.ivlen,
  183. elen);
  184. ret = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
  185. if (unlikely(sg != &esp->sgbuf[0]))
  186. kfree(sg);
  187. }
  188. unlock:
  189. spin_unlock(&x->lock);
  190. if (unlikely(ret))
  191. goto out;
  192. {
  193. u8 nexthdr[2];
  194. u8 padlen;
  195. if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
  196. BUG();
  197. padlen = nexthdr[0];
  198. if (padlen+2 >= elen) {
  199. LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen);
  200. ret = -EINVAL;
  201. goto out;
  202. }
  203. /* ... check padding bits here. Silly. :-) */
  204. /* RFC4303: Drop dummy packets without any error */
  205. if (nexthdr[1] == IPPROTO_NONE) {
  206. ret = -EINVAL;
  207. goto out;
  208. }
  209. pskb_trim(skb, skb->len - alen - padlen - 2);
  210. ret = nexthdr[1];
  211. }
  212. __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen);
  213. skb_set_transport_header(skb, -hdr_len);
  214. out:
  215. return ret;
  216. }
  217. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  218. {
  219. struct esp_data *esp = x->data;
  220. u32 blksize = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4);
  221. u32 align = max_t(u32, blksize, esp->conf.padlen);
  222. u32 rem;
  223. mtu -= x->props.header_len + esp->auth.icv_trunc_len;
  224. rem = mtu & (align - 1);
  225. mtu &= ~(align - 1);
  226. if (x->props.mode != XFRM_MODE_TUNNEL) {
  227. u32 padsize = ((blksize - 1) & 7) + 1;
  228. mtu -= blksize - padsize;
  229. mtu += min_t(u32, blksize - padsize, rem);
  230. }
  231. return mtu - 2;
  232. }
  233. static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  234. int type, int code, int offset, __be32 info)
  235. {
  236. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  237. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  238. struct xfrm_state *x;
  239. if (type != ICMPV6_DEST_UNREACH &&
  240. type != ICMPV6_PKT_TOOBIG)
  241. return;
  242. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
  243. if (!x)
  244. return;
  245. printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
  246. ntohl(esph->spi), NIP6(iph->daddr));
  247. xfrm_state_put(x);
  248. }
  249. static void esp6_destroy(struct xfrm_state *x)
  250. {
  251. struct esp_data *esp = x->data;
  252. if (!esp)
  253. return;
  254. crypto_free_blkcipher(esp->conf.tfm);
  255. esp->conf.tfm = NULL;
  256. kfree(esp->conf.ivec);
  257. esp->conf.ivec = NULL;
  258. crypto_free_hash(esp->auth.tfm);
  259. esp->auth.tfm = NULL;
  260. kfree(esp->auth.work_icv);
  261. esp->auth.work_icv = NULL;
  262. kfree(esp);
  263. }
  264. static int esp6_init_state(struct xfrm_state *x)
  265. {
  266. struct esp_data *esp = NULL;
  267. struct crypto_blkcipher *tfm;
  268. if (x->ealg == NULL)
  269. goto error;
  270. if (x->encap)
  271. goto error;
  272. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  273. if (esp == NULL)
  274. return -ENOMEM;
  275. if (x->aalg) {
  276. struct xfrm_algo_desc *aalg_desc;
  277. struct crypto_hash *hash;
  278. hash = crypto_alloc_hash(x->aalg->alg_name, 0,
  279. CRYPTO_ALG_ASYNC);
  280. if (IS_ERR(hash))
  281. goto error;
  282. esp->auth.tfm = hash;
  283. if (crypto_hash_setkey(hash, x->aalg->alg_key,
  284. (x->aalg->alg_key_len + 7) / 8))
  285. goto error;
  286. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  287. BUG_ON(!aalg_desc);
  288. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  289. crypto_hash_digestsize(hash)) {
  290. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  291. x->aalg->alg_name,
  292. crypto_hash_digestsize(hash),
  293. aalg_desc->uinfo.auth.icv_fullbits/8);
  294. goto error;
  295. }
  296. esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  297. esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  298. esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
  299. if (!esp->auth.work_icv)
  300. goto error;
  301. }
  302. tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC);
  303. if (IS_ERR(tfm))
  304. goto error;
  305. esp->conf.tfm = tfm;
  306. esp->conf.ivlen = crypto_blkcipher_ivsize(tfm);
  307. esp->conf.padlen = 0;
  308. if (esp->conf.ivlen) {
  309. esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
  310. if (unlikely(esp->conf.ivec == NULL))
  311. goto error;
  312. esp->conf.ivinitted = 0;
  313. }
  314. if (crypto_blkcipher_setkey(tfm, x->ealg->alg_key,
  315. (x->ealg->alg_key_len + 7) / 8))
  316. goto error;
  317. x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
  318. switch (x->props.mode) {
  319. case XFRM_MODE_BEET:
  320. case XFRM_MODE_TRANSPORT:
  321. break;
  322. case XFRM_MODE_TUNNEL:
  323. x->props.header_len += sizeof(struct ipv6hdr);
  324. break;
  325. default:
  326. goto error;
  327. }
  328. x->data = esp;
  329. return 0;
  330. error:
  331. x->data = esp;
  332. esp6_destroy(x);
  333. x->data = NULL;
  334. return -EINVAL;
  335. }
  336. static struct xfrm_type esp6_type =
  337. {
  338. .description = "ESP6",
  339. .owner = THIS_MODULE,
  340. .proto = IPPROTO_ESP,
  341. .flags = XFRM_TYPE_REPLAY_PROT,
  342. .init_state = esp6_init_state,
  343. .destructor = esp6_destroy,
  344. .get_mtu = esp6_get_mtu,
  345. .input = esp6_input,
  346. .output = esp6_output,
  347. .hdr_offset = xfrm6_find_1stfragopt,
  348. };
  349. static struct inet6_protocol esp6_protocol = {
  350. .handler = xfrm6_rcv,
  351. .err_handler = esp6_err,
  352. .flags = INET6_PROTO_NOPOLICY,
  353. };
  354. static int __init esp6_init(void)
  355. {
  356. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  357. printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
  358. return -EAGAIN;
  359. }
  360. if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
  361. printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
  362. xfrm_unregister_type(&esp6_type, AF_INET6);
  363. return -EAGAIN;
  364. }
  365. return 0;
  366. }
  367. static void __exit esp6_fini(void)
  368. {
  369. if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
  370. printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
  371. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  372. printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
  373. }
  374. module_init(esp6_init);
  375. module_exit(esp6_fini);
  376. MODULE_LICENSE("GPL");
  377. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);