]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv6/ah6.c
[IPV6] IPSEC: Support sending with Mobile IPv6 extension headers.
[linux-2.6-omap-h63xx.git] / net / ipv6 / ah6.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/ah.c.
25  */
26
27 #include <linux/module.h>
28 #include <net/ip.h>
29 #include <net/ah.h>
30 #include <linux/crypto.h>
31 #include <linux/pfkeyv2.h>
32 #include <linux/string.h>
33 #include <net/icmp.h>
34 #include <net/ipv6.h>
35 #include <net/protocol.h>
36 #include <net/xfrm.h>
37 #include <asm/scatterlist.h>
38
39 static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
40 {
41         u8 *opt = (u8 *)opthdr;
42         int len = ipv6_optlen(opthdr);
43         int off = 0;
44         int optlen = 0;
45
46         off += 2;
47         len -= 2;
48
49         while (len > 0) {
50
51                 switch (opt[off]) {
52
53                 case IPV6_TLV_PAD0:
54                         optlen = 1;
55                         break;
56                 default:
57                         if (len < 2) 
58                                 goto bad;
59                         optlen = opt[off+1]+2;
60                         if (len < optlen)
61                                 goto bad;
62                         if (opt[off] & 0x20)
63                                 memset(&opt[off+2], 0, opt[off+1]);
64                         break;
65                 }
66
67                 off += optlen;
68                 len -= optlen;
69         }
70         if (len == 0)
71                 return 1;
72
73 bad:
74         return 0;
75 }
76
77 #ifdef CONFIG_IPV6_MIP6
78 /**
79  *      ipv6_rearrange_destopt - rearrange IPv6 destination options header
80  *      @iph: IPv6 header
81  *      @destopt: destionation options header
82  */
83 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
84 {
85         u8 *opt = (u8 *)destopt;
86         int len = ipv6_optlen(destopt);
87         int off = 0;
88         int optlen = 0;
89
90         off += 2;
91         len -= 2;
92
93         while (len > 0) {
94
95                 switch (opt[off]) {
96
97                 case IPV6_TLV_PAD0:
98                         optlen = 1;
99                         break;
100                 default:
101                         if (len < 2)
102                                 goto bad;
103                         optlen = opt[off+1]+2;
104                         if (len < optlen)
105                                 goto bad;
106
107                         /* Rearrange the source address in @iph and the
108                          * addresses in home address option for final source.
109                          * See 11.3.2 of RFC 3775 for details.
110                          */
111                         if (opt[off] == IPV6_TLV_HAO) {
112                                 struct in6_addr final_addr;
113                                 struct ipv6_destopt_hao *hao;
114
115                                 hao = (struct ipv6_destopt_hao *)&opt[off];
116                                 if (hao->length != sizeof(hao->addr)) {
117                                         if (net_ratelimit())
118                                                 printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length);
119                                         goto bad;
120                                 }
121                                 ipv6_addr_copy(&final_addr, &hao->addr);
122                                 ipv6_addr_copy(&hao->addr, &iph->saddr);
123                                 ipv6_addr_copy(&iph->saddr, &final_addr);
124                         }
125                         break;
126                 }
127
128                 off += optlen;
129                 len -= optlen;
130         }
131         if (len == 0)
132                 return;
133
134 bad:
135         return;
136 }
137 #endif
138
139 /**
140  *      ipv6_rearrange_rthdr - rearrange IPv6 routing header
141  *      @iph: IPv6 header
142  *      @rthdr: routing header
143  *
144  *      Rearrange the destination address in @iph and the addresses in @rthdr
145  *      so that they appear in the order they will at the final destination.
146  *      See Appendix A2 of RFC 2402 for details.
147  */
148 static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
149 {
150         int segments, segments_left;
151         struct in6_addr *addrs;
152         struct in6_addr final_addr;
153
154         segments_left = rthdr->segments_left;
155         if (segments_left == 0)
156                 return;
157         rthdr->segments_left = 0; 
158
159         /* The value of rthdr->hdrlen has been verified either by the system
160          * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
161          * packets.  So we can assume that it is even and that segments is
162          * greater than or equal to segments_left.
163          *
164          * For the same reason we can assume that this option is of type 0.
165          */
166         segments = rthdr->hdrlen >> 1;
167
168         addrs = ((struct rt0_hdr *)rthdr)->addr;
169         ipv6_addr_copy(&final_addr, addrs + segments - 1);
170
171         addrs += segments - segments_left;
172         memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
173
174         ipv6_addr_copy(addrs, &iph->daddr);
175         ipv6_addr_copy(&iph->daddr, &final_addr);
176 }
177
178 #ifdef CONFIG_IPV6_MIP6
179 static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
180 #else
181 static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len)
182 #endif
183 {
184         union {
185                 struct ipv6hdr *iph;
186                 struct ipv6_opt_hdr *opth;
187                 struct ipv6_rt_hdr *rth;
188                 char *raw;
189         } exthdr = { .iph = iph };
190         char *end = exthdr.raw + len;
191         int nexthdr = iph->nexthdr;
192
193         exthdr.iph++;
194
195         while (exthdr.raw < end) {
196                 switch (nexthdr) {
197 #ifdef CONFIG_IPV6_MIP6
198                 case NEXTHDR_HOP:
199                         if (!zero_out_mutable_opts(exthdr.opth)) {
200                                 LIMIT_NETDEBUG(
201                                         KERN_WARNING "overrun %sopts\n",
202                                         nexthdr == NEXTHDR_HOP ?
203                                                 "hop" : "dest");
204                                 return -EINVAL;
205                         }
206                         break;
207                 case NEXTHDR_DEST:
208                         if (dir == XFRM_POLICY_OUT)
209                                 ipv6_rearrange_destopt(iph, exthdr.opth);
210                         if (!zero_out_mutable_opts(exthdr.opth)) {
211                                 LIMIT_NETDEBUG(
212                                         KERN_WARNING "overrun %sopts\n",
213                                         nexthdr == NEXTHDR_HOP ?
214                                                 "hop" : "dest");
215                                 return -EINVAL;
216                         }
217                         break;
218 #else
219                 case NEXTHDR_HOP:
220                 case NEXTHDR_DEST:
221                         if (!zero_out_mutable_opts(exthdr.opth)) {
222                                 LIMIT_NETDEBUG(
223                                         KERN_WARNING "overrun %sopts\n",
224                                         nexthdr == NEXTHDR_HOP ?
225                                                 "hop" : "dest");
226                                 return -EINVAL;
227                         }
228                         break;
229 #endif
230
231                 case NEXTHDR_ROUTING:
232                         ipv6_rearrange_rthdr(iph, exthdr.rth);
233                         break;
234
235                 default :
236                         return 0;
237                 }
238
239                 nexthdr = exthdr.opth->nexthdr;
240                 exthdr.raw += ipv6_optlen(exthdr.opth);
241         }
242
243         return 0;
244 }
245
246 static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
247 {
248         int err;
249         int extlen;
250         struct ipv6hdr *top_iph;
251         struct ip_auth_hdr *ah;
252         struct ah_data *ahp;
253         u8 nexthdr;
254         char tmp_base[8];
255         struct {
256 #ifdef CONFIG_IPV6_MIP6
257                 struct in6_addr saddr;
258 #endif
259                 struct in6_addr daddr;
260                 char hdrs[0];
261         } *tmp_ext;
262
263         top_iph = (struct ipv6hdr *)skb->data;
264         top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
265
266         nexthdr = *skb->nh.raw;
267         *skb->nh.raw = IPPROTO_AH;
268
269         /* When there are no extension headers, we only need to save the first
270          * 8 bytes of the base IP header.
271          */
272         memcpy(tmp_base, top_iph, sizeof(tmp_base));
273
274         tmp_ext = NULL;
275         extlen = skb->h.raw - (unsigned char *)(top_iph + 1);
276         if (extlen) {
277                 extlen += sizeof(*tmp_ext);
278                 tmp_ext = kmalloc(extlen, GFP_ATOMIC);
279                 if (!tmp_ext) {
280                         err = -ENOMEM;
281                         goto error;
282                 }
283 #ifdef CONFIG_IPV6_MIP6
284                 memcpy(tmp_ext, &top_iph->saddr, extlen);
285                 err = ipv6_clear_mutable_options(top_iph,
286                                                  extlen - sizeof(*tmp_ext) +
287                                                  sizeof(*top_iph),
288                                                  XFRM_POLICY_OUT);
289 #else
290                 memcpy(tmp_ext, &top_iph->daddr, extlen);
291                 err = ipv6_clear_mutable_options(top_iph,
292                                                  extlen - sizeof(*tmp_ext) +
293                                                  sizeof(*top_iph));
294 #endif
295                 if (err)
296                         goto error_free_iph;
297         }
298
299         ah = (struct ip_auth_hdr *)skb->h.raw;
300         ah->nexthdr = nexthdr;
301
302         top_iph->priority    = 0;
303         top_iph->flow_lbl[0] = 0;
304         top_iph->flow_lbl[1] = 0;
305         top_iph->flow_lbl[2] = 0;
306         top_iph->hop_limit   = 0;
307
308         ahp = x->data;
309         ah->hdrlen  = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + 
310                                    ahp->icv_trunc_len) >> 2) - 2;
311
312         ah->reserved = 0;
313         ah->spi = x->id.spi;
314         ah->seq_no = htonl(++x->replay.oseq);
315         xfrm_aevent_doreplay(x);
316         err = ah_mac_digest(ahp, skb, ah->auth_data);
317         if (err)
318                 goto error_free_iph;
319         memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len);
320
321         err = 0;
322
323         memcpy(top_iph, tmp_base, sizeof(tmp_base));
324         if (tmp_ext) {
325 #ifdef CONFIG_IPV6_MIP6
326                 memcpy(&top_iph->saddr, tmp_ext, extlen);
327 #else
328                 memcpy(&top_iph->daddr, tmp_ext, extlen);
329 #endif
330 error_free_iph:
331                 kfree(tmp_ext);
332         }
333
334 error:
335         return err;
336 }
337
338 static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
339 {
340         /*
341          * Before process AH
342          * [IPv6][Ext1][Ext2][AH][Dest][Payload]
343          * |<-------------->| hdr_len
344          *
345          * To erase AH:
346          * Keeping copy of cleared headers. After AH processing,
347          * Moving the pointer of skb->nh.raw by using skb_pull as long as AH
348          * header length. Then copy back the copy as long as hdr_len
349          * If destination header following AH exists, copy it into after [Ext2].
350          * 
351          * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
352          * There is offset of AH before IPv6 header after the process.
353          */
354
355         struct ipv6_auth_hdr *ah;
356         struct ah_data *ahp;
357         unsigned char *tmp_hdr = NULL;
358         u16 hdr_len;
359         u16 ah_hlen;
360         int nexthdr;
361         int err = -EINVAL;
362
363         if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
364                 goto out;
365
366         /* We are going to _remove_ AH header to keep sockets happy,
367          * so... Later this can change. */
368         if (skb_cloned(skb) &&
369             pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
370                 goto out;
371
372         hdr_len = skb->data - skb->nh.raw;
373         ah = (struct ipv6_auth_hdr*)skb->data;
374         ahp = x->data;
375         nexthdr = ah->nexthdr;
376         ah_hlen = (ah->hdrlen + 2) << 2;
377
378         if (ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_full_len) &&
379             ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len))
380                 goto out;
381
382         if (!pskb_may_pull(skb, ah_hlen))
383                 goto out;
384
385         tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
386         if (!tmp_hdr)
387                 goto out;
388         memcpy(tmp_hdr, skb->nh.raw, hdr_len);
389 #ifdef CONFIG_IPV6_MIP6
390         if (ipv6_clear_mutable_options(skb->nh.ipv6h, hdr_len, XFRM_POLICY_IN))
391                 goto free_out;
392 #else
393         if (ipv6_clear_mutable_options(skb->nh.ipv6h, hdr_len))
394                 goto free_out;
395 #endif
396         skb->nh.ipv6h->priority    = 0;
397         skb->nh.ipv6h->flow_lbl[0] = 0;
398         skb->nh.ipv6h->flow_lbl[1] = 0;
399         skb->nh.ipv6h->flow_lbl[2] = 0;
400         skb->nh.ipv6h->hop_limit   = 0;
401
402         {
403                 u8 auth_data[MAX_AH_AUTH_LEN];
404
405                 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
406                 memset(ah->auth_data, 0, ahp->icv_trunc_len);
407                 skb_push(skb, hdr_len);
408                 err = ah_mac_digest(ahp, skb, ah->auth_data);
409                 if (err)
410                         goto free_out;
411                 err = -EINVAL;
412                 if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
413                         LIMIT_NETDEBUG(KERN_WARNING "ipsec ah authentication error\n");
414                         x->stats.integrity_failed++;
415                         goto free_out;
416                 }
417         }
418
419         skb->h.raw = memcpy(skb->nh.raw += ah_hlen, tmp_hdr, hdr_len);
420         __skb_pull(skb, ah_hlen + hdr_len);
421
422         kfree(tmp_hdr);
423
424         return nexthdr;
425
426 free_out:
427         kfree(tmp_hdr);
428 out:
429         return err;
430 }
431
432 static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 
433                     int type, int code, int offset, __u32 info)
434 {
435         struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
436         struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
437         struct xfrm_state *x;
438
439         if (type != ICMPV6_DEST_UNREACH &&
440             type != ICMPV6_PKT_TOOBIG)
441                 return;
442
443         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
444         if (!x)
445                 return;
446
447         NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/" NIP6_FMT "\n",
448                  ntohl(ah->spi), NIP6(iph->daddr));
449
450         xfrm_state_put(x);
451 }
452
453 static int ah6_init_state(struct xfrm_state *x)
454 {
455         struct ah_data *ahp = NULL;
456         struct xfrm_algo_desc *aalg_desc;
457         struct crypto_hash *tfm;
458
459         if (!x->aalg)
460                 goto error;
461
462         /* null auth can use a zero length key */
463         if (x->aalg->alg_key_len > 512)
464                 goto error;
465
466         if (x->encap)
467                 goto error;
468
469         ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
470         if (ahp == NULL)
471                 return -ENOMEM;
472
473         ahp->key = x->aalg->alg_key;
474         ahp->key_len = (x->aalg->alg_key_len+7)/8;
475         tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC);
476         if (IS_ERR(tfm))
477                 goto error;
478
479         ahp->tfm = tfm;
480         if (crypto_hash_setkey(tfm, ahp->key, ahp->key_len))
481                 goto error;
482         
483         /*
484          * Lookup the algorithm description maintained by xfrm_algo,
485          * verify crypto transform properties, and store information
486          * we need for AH processing.  This lookup cannot fail here
487          * after a successful crypto_alloc_hash().
488          */
489         aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
490         BUG_ON(!aalg_desc);
491
492         if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
493             crypto_hash_digestsize(tfm)) {
494                 printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
495                        x->aalg->alg_name, crypto_hash_digestsize(tfm),
496                        aalg_desc->uinfo.auth.icv_fullbits/8);
497                 goto error;
498         }
499         
500         ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
501         ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
502         
503         BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
504         
505         ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
506         if (!ahp->work_icv)
507                 goto error;
508         
509         x->props.header_len = XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len);
510         if (x->props.mode == XFRM_MODE_TUNNEL)
511                 x->props.header_len += sizeof(struct ipv6hdr);
512         x->data = ahp;
513
514         return 0;
515
516 error:
517         if (ahp) {
518                 kfree(ahp->work_icv);
519                 crypto_free_hash(ahp->tfm);
520                 kfree(ahp);
521         }
522         return -EINVAL;
523 }
524
525 static void ah6_destroy(struct xfrm_state *x)
526 {
527         struct ah_data *ahp = x->data;
528
529         if (!ahp)
530                 return;
531
532         kfree(ahp->work_icv);
533         ahp->work_icv = NULL;
534         crypto_free_hash(ahp->tfm);
535         ahp->tfm = NULL;
536         kfree(ahp);
537 }
538
539 static struct xfrm_type ah6_type =
540 {
541         .description    = "AH6",
542         .owner          = THIS_MODULE,
543         .proto          = IPPROTO_AH,
544         .init_state     = ah6_init_state,
545         .destructor     = ah6_destroy,
546         .input          = ah6_input,
547         .output         = ah6_output,
548         .hdr_offset     = xfrm6_find_1stfragopt,
549 };
550
551 static struct inet6_protocol ah6_protocol = {
552         .handler        =       xfrm6_rcv,
553         .err_handler    =       ah6_err,
554         .flags          =       INET6_PROTO_NOPOLICY,
555 };
556
557 static int __init ah6_init(void)
558 {
559         if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
560                 printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
561                 return -EAGAIN;
562         }
563
564         if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
565                 printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
566                 xfrm_unregister_type(&ah6_type, AF_INET6);
567                 return -EAGAIN;
568         }
569
570         return 0;
571 }
572
573 static void __exit ah6_fini(void)
574 {
575         if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
576                 printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
577
578         if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
579                 printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
580
581 }
582
583 module_init(ah6_init);
584 module_exit(ah6_fini);
585
586 MODULE_LICENSE("GPL");