Merge to Fedora kernel-2.6.7-1.492
[linux-2.6.git] / net / ipv6 / esp6.c
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
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <net/inet_ecn.h>
30 #include <net/ip.h>
31 #include <net/xfrm.h>
32 #include <net/esp.h>
33 #include <asm/scatterlist.h>
34 #include <linux/crypto.h>
35 #include <linux/pfkeyv2.h>
36 #include <linux/random.h>
37 #include <net/icmp.h>
38 #include <net/ipv6.h>
39 #include <linux/icmpv6.h>
40
41 int esp6_output(struct sk_buff **pskb)
42 {
43         int err;
44         int hdr_len = 0;
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;
50         struct esp_data *esp;
51         struct sk_buff *trailer;
52         int blksize;
53         int clen;
54         int alen;
55         int nfrags;
56         u8 *prevhdr;
57         u8 nexthdr = 0;
58
59         if ((*pskb)->ip_summed == CHECKSUM_HW) {
60                 err = skb_checksum_help(pskb, 0);
61                 if (err)
62                         goto error_nolock;
63         }
64
65         spin_lock_bh(&x->lock);
66         err = xfrm_state_check(x, *pskb);
67         if (err)
68                 goto error;
69
70         if (x->props.mode) {
71                 err = xfrm6_tunnel_check_size(*pskb);
72                 if (err)
73                         goto error;
74         } else {
75                 /* Strip IP header in transport mode. Save it. */
76                 hdr_len = ip6_find_1stfragopt(*pskb, &prevhdr);
77                 nexthdr = *prevhdr;
78                 *prevhdr = IPPROTO_ESP;
79                 iph = kmalloc(hdr_len, GFP_ATOMIC);
80                 if (!iph) {
81                         err = -ENOMEM;
82                         goto error;
83                 }
84                 memcpy(iph, (*pskb)->nh.raw, hdr_len);
85                 __skb_pull(*pskb, hdr_len);
86         }
87
88         /* Now skb is pure payload to encrypt */
89         err = -ENOMEM;
90
91         /* Round to block size */
92         clen = (*pskb)->len;
93
94         esp = x->data;
95         alen = esp->auth.icv_trunc_len;
96         tfm = esp->conf.tfm;
97         blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
98         clen = (clen + 2 + blksize-1)&~(blksize-1);
99         if (esp->conf.padlen)
100                 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
101
102         if ((nfrags = skb_cow_data(*pskb, clen-(*pskb)->len+alen, &trailer)) < 0) {
103                 if (!x->props.mode && iph) kfree(iph);
104                 goto error;
105         }
106
107         /* Fill padding... */
108         do {
109                 int i;
110                 for (i=0; i<clen-(*pskb)->len - 2; i++)
111                         *(u8*)(trailer->tail + i) = i+1;
112         } while (0);
113         *(u8*)(trailer->tail + clen-(*pskb)->len - 2) = (clen - (*pskb)->len)-2;
114         pskb_put(*pskb, trailer, clen - (*pskb)->len);
115
116         if (x->props.mode) {
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);
135         } else { 
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);
140                 kfree(iph);
141                 top_iph->payload_len = htons((*pskb)->len + alen - sizeof(struct ipv6hdr));
142                 *(u8*)(trailer->tail - 1) = nexthdr;
143         }
144
145         esph->spi = x->id.spi;
146         esph->seq_no = htonl(++x->replay.oseq);
147
148         if (esp->conf.ivlen)
149                 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
150
151         do {
152                 struct scatterlist *sg = &esp->sgbuf[0];
153
154                 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
155                         sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
156                         if (!sg)
157                                 goto error;
158                 }
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]))
162                         kfree(sg);
163         } while (0);
164
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));
168         }
169
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);
174         }
175
176         (*pskb)->nh.raw = (*pskb)->data;
177
178         x->curlft.bytes += (*pskb)->len;
179         x->curlft.packets++;
180         spin_unlock_bh(&x->lock);
181         if (((*pskb)->dst = dst_pop(dst)) == NULL) {
182                 err = -EHOSTUNREACH;
183                 goto error_nolock;
184         }
185         return NET_XMIT_BYPASS;
186
187 error:
188         spin_unlock_bh(&x->lock);
189 error_nolock:
190         kfree_skb(*pskb);
191         return err;
192 }
193
194 int esp6_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
195 {
196         struct ipv6hdr *iph;
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;
203
204         int hdr_len = skb->h.raw - skb->nh.raw;
205         int nfrags;
206         unsigned char *tmp_hdr = NULL;
207         int ret = 0;
208
209         if (!pskb_may_pull(skb, sizeof(struct ipv6_esp_hdr))) {
210                 ret = -EINVAL;
211                 goto out_nofree;
212         }
213
214         if (elen <= 0 || (elen & (blksize-1))) {
215                 ret = -EINVAL;
216                 goto out_nofree;
217         }
218
219         tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
220         if (!tmp_hdr) {
221                 ret = -ENOMEM;
222                 goto out_nofree;
223         }
224         memcpy(tmp_hdr, skb->nh.raw, hdr_len);
225
226         /* If integrity check is required, do this. */
227         if (esp->auth.icv_full_len) {
228                 u8 sum[esp->auth.icv_full_len];
229                 u8 sum1[alen];
230
231                 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
232
233                 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
234                         BUG();
235
236                 if (unlikely(memcmp(sum, sum1, alen))) {
237                         x->stats.integrity_failed++;
238                         ret = -EINVAL;
239                         goto out;
240                 }
241         }
242
243         if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
244                 ret = -EINVAL;
245                 goto out;
246         }
247
248         skb->ip_summed = CHECKSUM_NONE;
249
250         esph = (struct ipv6_esp_hdr*)skb->data;
251         iph = skb->nh.ipv6h;
252
253         /* Get ivec. This can be wrong, check against another impls. */
254         if (esp->conf.ivlen)
255                 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
256
257         {
258                 u8 nexthdr[2];
259                 struct scatterlist *sg = &esp->sgbuf[0];
260                 u8 padlen;
261                 u8 *prevhdr;
262
263                 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
264                         sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
265                         if (!sg) {
266                                 ret = -ENOMEM;
267                                 goto out;
268                         }
269                 }
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]))
273                         kfree(sg);
274
275                 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
276                         BUG();
277
278                 padlen = nexthdr[0];
279                 if (padlen+2 >= elen) {
280                         LIMIT_NETDEBUG(
281                                 printk(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen));
282                         ret = -EINVAL;
283                         goto out;
284                 }
285                 /* ... check padding bits here. Silly. :-) */ 
286
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];
294         }
295
296 out:
297         kfree(tmp_hdr);
298 out_nofree:
299         return ret;
300 }
301
302 static u32 esp6_get_max_size(struct xfrm_state *x, int mtu)
303 {
304         struct esp_data *esp = x->data;
305         u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
306
307         if (x->props.mode) {
308                 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
309         } else {
310                 /* The worst case. */
311                 mtu += 2 + blksize;
312         }
313         if (esp->conf.padlen)
314                 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
315
316         return mtu + x->props.header_len + esp->auth.icv_full_len;
317 }
318
319 void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
320                 int type, int code, int offset, __u32 info)
321 {
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;
325
326         if (type != ICMPV6_DEST_UNREACH && 
327             type != ICMPV6_PKT_TOOBIG)
328                 return;
329
330         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
331         if (!x)
332                 return;
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));
336         xfrm_state_put(x);
337 }
338
339 void esp6_destroy(struct xfrm_state *x)
340 {
341         struct esp_data *esp = x->data;
342
343         if (!esp)
344                 return;
345
346         if (esp->conf.tfm) {
347                 crypto_free_tfm(esp->conf.tfm);
348                 esp->conf.tfm = NULL;
349         }
350         if (esp->conf.ivec) {
351                 kfree(esp->conf.ivec);
352                 esp->conf.ivec = NULL;
353         }
354         if (esp->auth.tfm) {
355                 crypto_free_tfm(esp->auth.tfm);
356                 esp->auth.tfm = NULL;
357         }
358         if (esp->auth.work_icv) {
359                 kfree(esp->auth.work_icv);
360                 esp->auth.work_icv = NULL;
361         }
362         kfree(esp);
363 }
364
365 int esp6_init_state(struct xfrm_state *x, void *args)
366 {
367         struct esp_data *esp = NULL;
368
369         if (x->aalg) {
370                 if (x->aalg->alg_key_len == 0 || x->aalg->alg_key_len > 512)
371                         goto error;
372         }
373         if (x->ealg == NULL)
374                 goto error;
375
376         esp = kmalloc(sizeof(*esp), GFP_KERNEL);
377         if (esp == NULL)
378                 return -ENOMEM;
379
380         memset(esp, 0, sizeof(*esp));
381
382         if (x->aalg) {
383                 struct xfrm_algo_desc *aalg_desc;
384
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)
389                         goto error;
390                 esp->auth.icv = esp_hmac_digest;
391  
392                 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
393                 BUG_ON(!aalg_desc);
394  
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",
398                                         x->aalg->alg_name,
399                                         crypto_tfm_alg_digestsize(esp->auth.tfm),
400                                         aalg_desc->uinfo.auth.icv_fullbits/8);
401                                 goto error;
402                 }
403  
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;
406  
407                 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
408                 if (!esp->auth.work_icv)
409                         goto error;
410         }
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);
415         else
416                 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
417         if (esp->conf.tfm == NULL)
418                 goto error;
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))
424                         goto error;
425                 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
426         }
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;
429         if (x->props.mode)
430                 x->props.header_len += sizeof(struct ipv6hdr);
431         x->data = esp;
432         return 0;
433
434 error:
435         if (esp) {
436                 if (esp->auth.tfm)
437                         crypto_free_tfm(esp->auth.tfm);
438                 if (esp->auth.work_icv)
439                         kfree(esp->auth.work_icv);
440                 if (esp->conf.tfm)
441                         crypto_free_tfm(esp->conf.tfm);
442                 kfree(esp);
443         }
444         return -EINVAL;
445 }
446
447 static struct xfrm_type esp6_type =
448 {
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,
455         .input          = esp6_input,
456         .output         = esp6_output
457 };
458
459 static struct inet6_protocol esp6_protocol = {
460         .handler        =       xfrm6_rcv,
461         .err_handler    =       esp6_err,
462         .flags          =       INET6_PROTO_NOPOLICY,
463 };
464
465 int __init esp6_init(void)
466 {
467         if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
468                 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
469                 return -EAGAIN;
470         }
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);
474                 return -EAGAIN;
475         }
476
477         return 0;
478 }
479
480 static void __exit esp6_fini(void)
481 {
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");
486 }
487
488 module_init(esp6_init);
489 module_exit(esp6_fini);
490
491 MODULE_LICENSE("GPL");