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 #define MAX_SG_ONSTACK 4
43 int esp6_output(struct sk_buff **pskb)
47 struct dst_entry *dst = (*pskb)->dst;
48 struct xfrm_state *x = dst->xfrm;
49 struct ipv6hdr *iph = NULL, *top_iph;
50 struct ipv6_esp_hdr *esph;
51 struct crypto_tfm *tfm;
53 struct sk_buff *trailer;
61 if ((*pskb)->ip_summed == CHECKSUM_HW) {
62 err = skb_checksum_help(pskb, 0);
67 spin_lock_bh(&x->lock);
68 err = xfrm_check_output(x, *pskb, AF_INET6);
73 /* 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 */
90 /* Round to block size */
94 alen = esp->auth.icv_trunc_len;
96 blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
97 clen = (clen + 2 + blksize-1)&~(blksize-1);
99 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
101 if ((nfrags = skb_cow_data(*pskb, clen-(*pskb)->len+alen, &trailer)) < 0) {
102 if (!x->props.mode && iph) kfree(iph);
106 /* Fill padding... */
109 for (i=0; i<clen-(*pskb)->len - 2; i++)
110 *(u8*)(trailer->tail + i) = i+1;
112 *(u8*)(trailer->tail + clen-(*pskb)->len - 2) = (clen - (*pskb)->len)-2;
113 pskb_put(*pskb, trailer, clen - (*pskb)->len);
116 iph = (*pskb)->nh.ipv6h;
117 top_iph = (struct ipv6hdr*)skb_push(*pskb, x->props.header_len);
118 esph = (struct ipv6_esp_hdr*)(top_iph+1);
119 *(u8*)(trailer->tail - 1) = IPPROTO_IPV6;
120 top_iph->version = 6;
121 top_iph->priority = iph->priority;
122 top_iph->flow_lbl[0] = iph->flow_lbl[0];
123 top_iph->flow_lbl[1] = iph->flow_lbl[1];
124 top_iph->flow_lbl[2] = iph->flow_lbl[2];
125 if (x->props.flags & XFRM_STATE_NOECN)
126 IP6_ECN_clear(top_iph);
127 top_iph->nexthdr = IPPROTO_ESP;
128 top_iph->payload_len = htons((*pskb)->len + alen - sizeof(struct ipv6hdr));
129 top_iph->hop_limit = iph->hop_limit;
130 ipv6_addr_copy(&top_iph->saddr,
131 (struct in6_addr *)&x->props.saddr);
132 ipv6_addr_copy(&top_iph->daddr,
133 (struct in6_addr *)&x->id.daddr);
135 esph = (struct ipv6_esp_hdr*)skb_push(*pskb, x->props.header_len);
136 (*pskb)->h.raw = (unsigned char*)esph;
137 top_iph = (struct ipv6hdr*)skb_push(*pskb, hdr_len);
138 memcpy(top_iph, iph, hdr_len);
140 top_iph->payload_len = htons((*pskb)->len + alen - sizeof(struct ipv6hdr));
141 *(u8*)(trailer->tail - 1) = nexthdr;
144 esph->spi = x->id.spi;
145 esph->seq_no = htonl(++x->replay.oseq);
148 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
151 struct scatterlist sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
152 struct scatterlist *sg = sgbuf;
154 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
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 != sgbuf))
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 sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
260 struct scatterlist *sg = sgbuf;
264 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
265 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
271 skb_to_sgvec(skb, sg, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen, elen);
272 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
273 if (unlikely(sg != sgbuf))
276 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
280 if (padlen+2 >= elen) {
282 printk(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen));
286 /* ... check padding bits here. Silly. :-) */
288 pskb_trim(skb, skb->len - alen - padlen - 2);
289 skb->h.raw = skb_pull(skb, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen);
290 skb->nh.raw += sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
291 memcpy(skb->nh.raw, tmp_hdr, hdr_len);
292 skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
293 ip6_find_1stfragopt(skb, &prevhdr);
294 ret = *prevhdr = nexthdr[1];
303 static u32 esp6_get_max_size(struct xfrm_state *x, int mtu)
305 struct esp_data *esp = x->data;
306 u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
309 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
311 /* The worst case. */
314 if (esp->conf.padlen)
315 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
317 return mtu + x->props.header_len + esp->auth.icv_full_len;
320 void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
321 int type, int code, int offset, __u32 info)
323 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
324 struct ipv6_esp_hdr *esph = (struct ipv6_esp_hdr*)(skb->data+offset);
325 struct xfrm_state *x;
327 if (type != ICMPV6_DEST_UNREACH &&
328 type != ICMPV6_PKT_TOOBIG)
331 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
334 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/"
335 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
336 ntohl(esph->spi), NIP6(iph->daddr));
340 void esp6_destroy(struct xfrm_state *x)
342 struct esp_data *esp = x->data;
348 crypto_free_tfm(esp->conf.tfm);
349 esp->conf.tfm = NULL;
351 if (esp->conf.ivec) {
352 kfree(esp->conf.ivec);
353 esp->conf.ivec = NULL;
356 crypto_free_tfm(esp->auth.tfm);
357 esp->auth.tfm = NULL;
359 if (esp->auth.work_icv) {
360 kfree(esp->auth.work_icv);
361 esp->auth.work_icv = NULL;
366 int esp6_init_state(struct xfrm_state *x, void *args)
368 struct esp_data *esp = NULL;
371 if (x->aalg->alg_key_len == 0 || x->aalg->alg_key_len > 512)
377 esp = kmalloc(sizeof(*esp), GFP_KERNEL);
381 memset(esp, 0, sizeof(*esp));
384 struct xfrm_algo_desc *aalg_desc;
386 esp->auth.key = x->aalg->alg_key;
387 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
388 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
389 if (esp->auth.tfm == NULL)
391 esp->auth.icv = esp_hmac_digest;
393 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
396 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
397 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
398 printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
400 crypto_tfm_alg_digestsize(esp->auth.tfm),
401 aalg_desc->uinfo.auth.icv_fullbits/8);
405 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
406 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
408 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
409 if (!esp->auth.work_icv)
412 esp->conf.key = x->ealg->alg_key;
413 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
414 if (x->props.ealgo == SADB_EALG_NULL)
415 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
417 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
418 if (esp->conf.tfm == NULL)
420 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
421 esp->conf.padlen = 0;
422 if (esp->conf.ivlen) {
423 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
424 if (unlikely(esp->conf.ivec == NULL))
426 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
428 crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len);
429 x->props.header_len = sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
431 x->props.header_len += sizeof(struct ipv6hdr);
438 crypto_free_tfm(esp->auth.tfm);
439 if (esp->auth.work_icv)
440 kfree(esp->auth.work_icv);
442 crypto_free_tfm(esp->conf.tfm);
448 static struct xfrm_type esp6_type =
450 .description = "ESP6",
451 .owner = THIS_MODULE,
452 .proto = IPPROTO_ESP,
453 .init_state = esp6_init_state,
454 .destructor = esp6_destroy,
455 .get_max_size = esp6_get_max_size,
457 .output = esp6_output
460 static struct inet6_protocol esp6_protocol = {
461 .handler = xfrm6_rcv,
462 .err_handler = esp6_err,
463 .flags = INET6_PROTO_NOPOLICY,
466 int __init esp6_init(void)
468 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
469 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
472 if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
473 printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
474 xfrm_unregister_type(&esp6_type, AF_INET6);
481 static void __exit esp6_fini(void)
483 if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
484 printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
485 if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
486 printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
489 module_init(esp6_init);
490 module_exit(esp6_fini);
492 MODULE_LICENSE("GPL");