patch-2_6_7-vs1_9_1_12
[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 #define MAX_SG_ONSTACK 4
42
43 int esp6_output(struct sk_buff **pskb)
44 {
45         int err;
46         int hdr_len = 0;
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;
52         struct esp_data *esp;
53         struct sk_buff *trailer;
54         int blksize;
55         int clen;
56         int alen;
57         int nfrags;
58         u8 *prevhdr;
59         u8 nexthdr = 0;
60
61         if ((*pskb)->ip_summed == CHECKSUM_HW) {
62                 err = skb_checksum_help(pskb, 0);
63                 if (err)
64                         goto error_nolock;
65         }
66
67         spin_lock_bh(&x->lock);
68         err = xfrm_check_output(x, *pskb, AF_INET6);
69         if (err)
70                 goto error;
71         err = -ENOMEM;
72
73         /* Strip IP header in transport mode. Save it. */
74
75         if (!x->props.mode) {
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
90         /* Round to block size */
91         clen = (*pskb)->len;
92
93         esp = x->data;
94         alen = esp->auth.icv_trunc_len;
95         tfm = esp->conf.tfm;
96         blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
97         clen = (clen + 2 + blksize-1)&~(blksize-1);
98         if (esp->conf.padlen)
99                 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
100
101         if ((nfrags = skb_cow_data(*pskb, clen-(*pskb)->len+alen, &trailer)) < 0) {
102                 if (!x->props.mode && iph) kfree(iph);
103                 goto error;
104         }
105
106         /* Fill padding... */
107         do {
108                 int i;
109                 for (i=0; i<clen-(*pskb)->len - 2; i++)
110                         *(u8*)(trailer->tail + i) = i+1;
111         } while (0);
112         *(u8*)(trailer->tail + clen-(*pskb)->len - 2) = (clen - (*pskb)->len)-2;
113         pskb_put(*pskb, trailer, clen - (*pskb)->len);
114
115         if (x->props.mode) {
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);
134         } else { 
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);
139                 kfree(iph);
140                 top_iph->payload_len = htons((*pskb)->len + alen - sizeof(struct ipv6hdr));
141                 *(u8*)(trailer->tail - 1) = nexthdr;
142         }
143
144         esph->spi = x->id.spi;
145         esph->seq_no = htonl(++x->replay.oseq);
146
147         if (esp->conf.ivlen)
148                 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
149
150         do {
151                 struct scatterlist sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
152                 struct scatterlist *sg = sgbuf;
153
154                 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
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 != sgbuf))
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 sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
260                 struct scatterlist *sg = sgbuf;
261                 u8 padlen;
262                 u8 *prevhdr;
263
264                 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
265                         sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
266                         if (!sg) {
267                                 ret = -ENOMEM;
268                                 goto out;
269                         }
270                 }
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))
274                         kfree(sg);
275
276                 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
277                         BUG();
278
279                 padlen = nexthdr[0];
280                 if (padlen+2 >= elen) {
281                         LIMIT_NETDEBUG(
282                                 printk(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen));
283                         ret = -EINVAL;
284                         goto out;
285                 }
286                 /* ... check padding bits here. Silly. :-) */ 
287
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];
295         }
296
297 out:
298         kfree(tmp_hdr);
299 out_nofree:
300         return ret;
301 }
302
303 static u32 esp6_get_max_size(struct xfrm_state *x, int mtu)
304 {
305         struct esp_data *esp = x->data;
306         u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
307
308         if (x->props.mode) {
309                 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
310         } else {
311                 /* The worst case. */
312                 mtu += 2 + blksize;
313         }
314         if (esp->conf.padlen)
315                 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
316
317         return mtu + x->props.header_len + esp->auth.icv_full_len;
318 }
319
320 void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
321                 int type, int code, int offset, __u32 info)
322 {
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;
326
327         if (type != ICMPV6_DEST_UNREACH && 
328             type != ICMPV6_PKT_TOOBIG)
329                 return;
330
331         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
332         if (!x)
333                 return;
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));
337         xfrm_state_put(x);
338 }
339
340 void esp6_destroy(struct xfrm_state *x)
341 {
342         struct esp_data *esp = x->data;
343
344         if (!esp)
345                 return;
346
347         if (esp->conf.tfm) {
348                 crypto_free_tfm(esp->conf.tfm);
349                 esp->conf.tfm = NULL;
350         }
351         if (esp->conf.ivec) {
352                 kfree(esp->conf.ivec);
353                 esp->conf.ivec = NULL;
354         }
355         if (esp->auth.tfm) {
356                 crypto_free_tfm(esp->auth.tfm);
357                 esp->auth.tfm = NULL;
358         }
359         if (esp->auth.work_icv) {
360                 kfree(esp->auth.work_icv);
361                 esp->auth.work_icv = NULL;
362         }
363         kfree(esp);
364 }
365
366 int esp6_init_state(struct xfrm_state *x, void *args)
367 {
368         struct esp_data *esp = NULL;
369
370         if (x->aalg) {
371                 if (x->aalg->alg_key_len == 0 || x->aalg->alg_key_len > 512)
372                         goto error;
373         }
374         if (x->ealg == NULL)
375                 goto error;
376
377         esp = kmalloc(sizeof(*esp), GFP_KERNEL);
378         if (esp == NULL)
379                 return -ENOMEM;
380
381         memset(esp, 0, sizeof(*esp));
382
383         if (x->aalg) {
384                 struct xfrm_algo_desc *aalg_desc;
385
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)
390                         goto error;
391                 esp->auth.icv = esp_hmac_digest;
392  
393                 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
394                 BUG_ON(!aalg_desc);
395  
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",
399                                         x->aalg->alg_name,
400                                         crypto_tfm_alg_digestsize(esp->auth.tfm),
401                                         aalg_desc->uinfo.auth.icv_fullbits/8);
402                                 goto error;
403                 }
404  
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;
407  
408                 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
409                 if (!esp->auth.work_icv)
410                         goto error;
411         }
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);
416         else
417                 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
418         if (esp->conf.tfm == NULL)
419                 goto error;
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))
425                         goto error;
426                 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
427         }
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;
430         if (x->props.mode)
431                 x->props.header_len += sizeof(struct ipv6hdr);
432         x->data = esp;
433         return 0;
434
435 error:
436         if (esp) {
437                 if (esp->auth.tfm)
438                         crypto_free_tfm(esp->auth.tfm);
439                 if (esp->auth.work_icv)
440                         kfree(esp->auth.work_icv);
441                 if (esp->conf.tfm)
442                         crypto_free_tfm(esp->conf.tfm);
443                 kfree(esp);
444         }
445         return -EINVAL;
446 }
447
448 static struct xfrm_type esp6_type =
449 {
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,
456         .input          = esp6_input,
457         .output         = esp6_output
458 };
459
460 static struct inet6_protocol esp6_protocol = {
461         .handler        =       xfrm6_rcv,
462         .err_handler    =       esp6_err,
463         .flags          =       INET6_PROTO_NOPOLICY,
464 };
465
466 int __init esp6_init(void)
467 {
468         if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
469                 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
470                 return -EAGAIN;
471         }
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);
475                 return -EAGAIN;
476         }
477
478         return 0;
479 }
480
481 static void __exit esp6_fini(void)
482 {
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");
487 }
488
489 module_init(esp6_init);
490 module_exit(esp6_fini);
491
492 MODULE_LICENSE("GPL");