2 * Copyright (C)2002 USAGI/WIDE Project
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.
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.
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
20 * Mitsuru KANDA @USAGI : IPv6 Support
21 * Kazunori MIYAZAWA @USAGI :
22 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
24 * This file is derived from net/ipv4/esp.c
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <net/inet_ecn.h>
33 #include <asm/scatterlist.h>
34 #include <linux/crypto.h>
35 #include <linux/pfkeyv2.h>
36 #include <linux/random.h>
39 #include <linux/icmpv6.h>
41 int esp6_output(struct sk_buff **pskb)
45 struct dst_entry *dst = (*pskb)->dst;
46 struct xfrm_state *x = dst->xfrm;
47 struct ipv6hdr *iph = NULL, *top_iph;
48 struct ipv6_esp_hdr *esph;
49 struct crypto_tfm *tfm;
51 struct sk_buff *trailer;
59 if ((*pskb)->ip_summed == CHECKSUM_HW) {
60 err = skb_checksum_help(pskb, 0);
65 spin_lock_bh(&x->lock);
66 err = xfrm_state_check(x, *pskb);
71 err = xfrm6_tunnel_check_size(*pskb);
75 /* Strip IP header in transport mode. Save it. */
76 hdr_len = ip6_find_1stfragopt(*pskb, &prevhdr);
78 *prevhdr = IPPROTO_ESP;
79 iph = kmalloc(hdr_len, GFP_ATOMIC);
84 memcpy(iph, (*pskb)->nh.raw, hdr_len);
85 __skb_pull(*pskb, hdr_len);
88 /* Now skb is pure payload to encrypt */
91 /* Round to block size */
95 alen = esp->auth.icv_trunc_len;
97 blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
98 clen = (clen + 2 + blksize-1)&~(blksize-1);
100 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
102 if ((nfrags = skb_cow_data(*pskb, clen-(*pskb)->len+alen, &trailer)) < 0) {
103 if (!x->props.mode && iph) kfree(iph);
107 /* Fill padding... */
110 for (i=0; i<clen-(*pskb)->len - 2; i++)
111 *(u8*)(trailer->tail + i) = i+1;
113 *(u8*)(trailer->tail + clen-(*pskb)->len - 2) = (clen - (*pskb)->len)-2;
114 pskb_put(*pskb, trailer, clen - (*pskb)->len);
117 iph = (*pskb)->nh.ipv6h;
118 top_iph = (struct ipv6hdr*)skb_push(*pskb, x->props.header_len);
119 esph = (struct ipv6_esp_hdr*)(top_iph+1);
120 *(u8*)(trailer->tail - 1) = IPPROTO_IPV6;
121 top_iph->version = 6;
122 top_iph->priority = iph->priority;
123 top_iph->flow_lbl[0] = iph->flow_lbl[0];
124 top_iph->flow_lbl[1] = iph->flow_lbl[1];
125 top_iph->flow_lbl[2] = iph->flow_lbl[2];
126 if (x->props.flags & XFRM_STATE_NOECN)
127 IP6_ECN_clear(top_iph);
128 top_iph->nexthdr = IPPROTO_ESP;
129 top_iph->payload_len = htons((*pskb)->len + alen - sizeof(struct ipv6hdr));
130 top_iph->hop_limit = iph->hop_limit;
131 ipv6_addr_copy(&top_iph->saddr,
132 (struct in6_addr *)&x->props.saddr);
133 ipv6_addr_copy(&top_iph->daddr,
134 (struct in6_addr *)&x->id.daddr);
136 esph = (struct ipv6_esp_hdr*)skb_push(*pskb, x->props.header_len);
137 (*pskb)->h.raw = (unsigned char*)esph;
138 top_iph = (struct ipv6hdr*)skb_push(*pskb, hdr_len);
139 memcpy(top_iph, iph, hdr_len);
141 top_iph->payload_len = htons((*pskb)->len + alen - sizeof(struct ipv6hdr));
142 *(u8*)(trailer->tail - 1) = nexthdr;
145 esph->spi = x->id.spi;
146 esph->seq_no = htonl(++x->replay.oseq);
149 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
152 struct scatterlist *sg = &esp->sgbuf[0];
154 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
155 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
159 skb_to_sgvec(*pskb, sg, esph->enc_data+esp->conf.ivlen-(*pskb)->data, clen);
160 crypto_cipher_encrypt(tfm, sg, sg, clen);
161 if (unlikely(sg != &esp->sgbuf[0]))
165 if (esp->conf.ivlen) {
166 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
167 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
170 if (esp->auth.icv_full_len) {
171 esp->auth.icv(esp, *pskb, (u8*)esph-(*pskb)->data,
172 sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
173 pskb_put(*pskb, trailer, alen);
176 (*pskb)->nh.raw = (*pskb)->data;
178 x->curlft.bytes += (*pskb)->len;
180 spin_unlock_bh(&x->lock);
181 if (((*pskb)->dst = dst_pop(dst)) == NULL) {
185 return NET_XMIT_BYPASS;
188 spin_unlock_bh(&x->lock);
194 int esp6_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
197 struct ipv6_esp_hdr *esph;
198 struct esp_data *esp = x->data;
199 struct sk_buff *trailer;
200 int blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
201 int alen = esp->auth.icv_trunc_len;
202 int elen = skb->len - sizeof(struct ipv6_esp_hdr) - esp->conf.ivlen - alen;
204 int hdr_len = skb->h.raw - skb->nh.raw;
206 unsigned char *tmp_hdr = NULL;
209 if (!pskb_may_pull(skb, sizeof(struct ipv6_esp_hdr))) {
214 if (elen <= 0 || (elen & (blksize-1))) {
219 tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
224 memcpy(tmp_hdr, skb->nh.raw, hdr_len);
226 /* If integrity check is required, do this. */
227 if (esp->auth.icv_full_len) {
228 u8 sum[esp->auth.icv_full_len];
231 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
233 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
236 if (unlikely(memcmp(sum, sum1, alen))) {
237 x->stats.integrity_failed++;
243 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
248 skb->ip_summed = CHECKSUM_NONE;
250 esph = (struct ipv6_esp_hdr*)skb->data;
253 /* Get ivec. This can be wrong, check against another impls. */
255 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
259 struct scatterlist *sg = &esp->sgbuf[0];
263 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
264 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
270 skb_to_sgvec(skb, sg, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen, elen);
271 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
272 if (unlikely(sg != &esp->sgbuf[0]))
275 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
279 if (padlen+2 >= elen) {
281 printk(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen));
285 /* ... check padding bits here. Silly. :-) */
287 pskb_trim(skb, skb->len - alen - padlen - 2);
288 skb->h.raw = skb_pull(skb, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen);
289 skb->nh.raw += sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
290 memcpy(skb->nh.raw, tmp_hdr, hdr_len);
291 skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
292 ip6_find_1stfragopt(skb, &prevhdr);
293 ret = *prevhdr = nexthdr[1];
302 static u32 esp6_get_max_size(struct xfrm_state *x, int mtu)
304 struct esp_data *esp = x->data;
305 u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
308 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
310 /* The worst case. */
313 if (esp->conf.padlen)
314 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
316 return mtu + x->props.header_len + esp->auth.icv_full_len;
319 void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
320 int type, int code, int offset, __u32 info)
322 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
323 struct ipv6_esp_hdr *esph = (struct ipv6_esp_hdr*)(skb->data+offset);
324 struct xfrm_state *x;
326 if (type != ICMPV6_DEST_UNREACH &&
327 type != ICMPV6_PKT_TOOBIG)
330 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
333 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/"
334 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
335 ntohl(esph->spi), NIP6(iph->daddr));
339 void esp6_destroy(struct xfrm_state *x)
341 struct esp_data *esp = x->data;
347 crypto_free_tfm(esp->conf.tfm);
348 esp->conf.tfm = NULL;
350 if (esp->conf.ivec) {
351 kfree(esp->conf.ivec);
352 esp->conf.ivec = NULL;
355 crypto_free_tfm(esp->auth.tfm);
356 esp->auth.tfm = NULL;
358 if (esp->auth.work_icv) {
359 kfree(esp->auth.work_icv);
360 esp->auth.work_icv = NULL;
365 int esp6_init_state(struct xfrm_state *x, void *args)
367 struct esp_data *esp = NULL;
370 if (x->aalg->alg_key_len == 0 || x->aalg->alg_key_len > 512)
376 esp = kmalloc(sizeof(*esp), GFP_KERNEL);
380 memset(esp, 0, sizeof(*esp));
383 struct xfrm_algo_desc *aalg_desc;
385 esp->auth.key = x->aalg->alg_key;
386 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
387 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
388 if (esp->auth.tfm == NULL)
390 esp->auth.icv = esp_hmac_digest;
392 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
395 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
396 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
397 printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
399 crypto_tfm_alg_digestsize(esp->auth.tfm),
400 aalg_desc->uinfo.auth.icv_fullbits/8);
404 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
405 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
407 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
408 if (!esp->auth.work_icv)
411 esp->conf.key = x->ealg->alg_key;
412 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
413 if (x->props.ealgo == SADB_EALG_NULL)
414 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
416 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
417 if (esp->conf.tfm == NULL)
419 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
420 esp->conf.padlen = 0;
421 if (esp->conf.ivlen) {
422 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
423 if (unlikely(esp->conf.ivec == NULL))
425 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
427 crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len);
428 x->props.header_len = sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
430 x->props.header_len += sizeof(struct ipv6hdr);
437 crypto_free_tfm(esp->auth.tfm);
438 if (esp->auth.work_icv)
439 kfree(esp->auth.work_icv);
441 crypto_free_tfm(esp->conf.tfm);
447 static struct xfrm_type esp6_type =
449 .description = "ESP6",
450 .owner = THIS_MODULE,
451 .proto = IPPROTO_ESP,
452 .init_state = esp6_init_state,
453 .destructor = esp6_destroy,
454 .get_max_size = esp6_get_max_size,
456 .output = esp6_output
459 static struct inet6_protocol esp6_protocol = {
460 .handler = xfrm6_rcv,
461 .err_handler = esp6_err,
462 .flags = INET6_PROTO_NOPOLICY,
465 int __init esp6_init(void)
467 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
468 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
471 if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
472 printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
473 xfrm_unregister_type(&esp6_type, AF_INET6);
480 static void __exit esp6_fini(void)
482 if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
483 printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
484 if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
485 printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
488 module_init(esp6_init);
489 module_exit(esp6_fini);
491 MODULE_LICENSE("GPL");