This commit was manufactured by cvs2svn to create tag
[linux-2.6.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/config.h>
28 #include <linux/module.h>
29 #include <net/inet_ecn.h>
30 #include <net/ip.h>
31 #include <net/ah.h>
32 #include <linux/crypto.h>
33 #include <linux/pfkeyv2.h>
34 #include <net/icmp.h>
35 #include <net/ipv6.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 static int ipv6_clear_mutable_options(struct sk_buff *skb, u16 *nh_offset, int dir)
78 {
79         u16 offset = sizeof(struct ipv6hdr);
80         struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
81         unsigned int packet_len = skb->tail - skb->nh.raw;
82         u8 nexthdr = skb->nh.ipv6h->nexthdr;
83         u8 nextnexthdr = 0;
84
85         *nh_offset = ((unsigned char *)&skb->nh.ipv6h->nexthdr) - skb->nh.raw;
86
87         while (offset + 1 <= packet_len) {
88
89                 switch (nexthdr) {
90
91                 case NEXTHDR_HOP:
92                         *nh_offset = offset;
93                         offset += ipv6_optlen(exthdr);
94                         if (!zero_out_mutable_opts(exthdr)) {
95                                 LIMIT_NETDEBUG(
96                                 printk(KERN_WARNING "overrun hopopts\n")); 
97                                 return 0;
98                         }
99                         nexthdr = exthdr->nexthdr;
100                         exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
101                         break;
102
103                 case NEXTHDR_ROUTING:
104                         *nh_offset = offset;
105                         offset += ipv6_optlen(exthdr);
106                         ((struct ipv6_rt_hdr*)exthdr)->segments_left = 0; 
107                         nexthdr = exthdr->nexthdr;
108                         exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
109                         break;
110
111                 case NEXTHDR_DEST:
112                         *nh_offset = offset;
113                         offset += ipv6_optlen(exthdr);
114                         if (!zero_out_mutable_opts(exthdr))  {
115                                 LIMIT_NETDEBUG(
116                                         printk(KERN_WARNING "overrun destopt\n")); 
117                                 return 0;
118                         }
119                         nexthdr = exthdr->nexthdr;
120                         exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
121                         break;
122
123                 case NEXTHDR_AUTH:
124                         if (dir == XFRM_POLICY_OUT) {
125                                 memset(((struct ipv6_auth_hdr*)exthdr)->auth_data, 0, 
126                                        (((struct ipv6_auth_hdr*)exthdr)->hdrlen - 1) << 2);
127                         }
128                         if (exthdr->nexthdr == NEXTHDR_DEST) {
129                                 offset += (((struct ipv6_auth_hdr*)exthdr)->hdrlen + 2) << 2;
130                                 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
131                                 nextnexthdr = exthdr->nexthdr;
132                                 if (!zero_out_mutable_opts(exthdr)) {
133                                         LIMIT_NETDEBUG(
134                                                 printk(KERN_WARNING "overrun destopt\n"));
135                                         return 0;
136                                 }
137                         }
138                         return nexthdr;
139                 default :
140                         return nexthdr;
141                 }
142         }
143
144         return nexthdr;
145 }
146
147 int ah6_output(struct sk_buff **pskb)
148 {
149         int err;
150         int hdr_len = sizeof(struct ipv6hdr);
151         struct dst_entry *dst = (*pskb)->dst;
152         struct xfrm_state *x  = dst->xfrm;
153         struct ipv6hdr *iph = NULL;
154         struct ip_auth_hdr *ah;
155         struct ah_data *ahp;
156         u16 nh_offset = 0;
157         u8 nexthdr;
158
159         if ((*pskb)->ip_summed == CHECKSUM_HW) {
160                 err = skb_checksum_help(pskb, 0);
161                 if (err)
162                         goto error_nolock;
163         }
164
165         spin_lock_bh(&x->lock);
166         err = xfrm_state_check(x, *pskb);
167         if (err)
168                 goto error;
169
170         if (x->props.mode) {
171                 err = xfrm6_tunnel_check_size(*pskb);
172                 if (err)
173                         goto error;
174
175                 iph = (*pskb)->nh.ipv6h;
176                 (*pskb)->nh.ipv6h = (struct ipv6hdr*)skb_push(*pskb, x->props.header_len);
177                 (*pskb)->nh.ipv6h->version = 6;
178                 (*pskb)->nh.ipv6h->payload_len = htons((*pskb)->len - sizeof(struct ipv6hdr));
179                 (*pskb)->nh.ipv6h->nexthdr = IPPROTO_AH;
180                 ipv6_addr_copy(&(*pskb)->nh.ipv6h->saddr,
181                                (struct in6_addr *) &x->props.saddr);
182                 ipv6_addr_copy(&(*pskb)->nh.ipv6h->daddr,
183                                (struct in6_addr *) &x->id.daddr);
184                 ah = (struct ip_auth_hdr*)((*pskb)->nh.ipv6h+1);
185                 ah->nexthdr = IPPROTO_IPV6;
186         } else {
187                 hdr_len = (*pskb)->h.raw - (*pskb)->nh.raw;
188                 iph = kmalloc(hdr_len, GFP_ATOMIC);
189                 if (!iph) {
190                         err = -ENOMEM;
191                         goto error;
192                 }
193                 memcpy(iph, (*pskb)->data, hdr_len);
194                 (*pskb)->nh.ipv6h = (struct ipv6hdr*)skb_push(*pskb, x->props.header_len);
195                 memcpy((*pskb)->nh.ipv6h, iph, hdr_len);
196                 nexthdr = ipv6_clear_mutable_options(*pskb, &nh_offset, XFRM_POLICY_OUT);
197                 if (nexthdr == 0)
198                         goto error_free_iph;
199
200                 (*pskb)->nh.raw[nh_offset] = IPPROTO_AH;
201                 (*pskb)->nh.ipv6h->payload_len = htons((*pskb)->len - sizeof(struct ipv6hdr));
202                 ah = (struct ip_auth_hdr*)((*pskb)->nh.raw+hdr_len);
203                 (*pskb)->h.raw = (unsigned char*) ah;
204                 ah->nexthdr = nexthdr;
205         }
206
207         (*pskb)->nh.ipv6h->priority    = 0;
208         (*pskb)->nh.ipv6h->flow_lbl[0] = 0;
209         (*pskb)->nh.ipv6h->flow_lbl[1] = 0;
210         (*pskb)->nh.ipv6h->flow_lbl[2] = 0;
211         (*pskb)->nh.ipv6h->hop_limit    = 0;
212
213         ahp = x->data;
214         ah->hdrlen  = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + 
215                                    ahp->icv_trunc_len) >> 2) - 2;
216
217         ah->reserved = 0;
218         ah->spi = x->id.spi;
219         ah->seq_no = htonl(++x->replay.oseq);
220         ahp->icv(ahp, *pskb, ah->auth_data);
221
222         if (x->props.mode) {
223                 (*pskb)->nh.ipv6h->hop_limit   = iph->hop_limit;
224                 (*pskb)->nh.ipv6h->priority    = iph->priority;         
225                 (*pskb)->nh.ipv6h->flow_lbl[0] = iph->flow_lbl[0];
226                 (*pskb)->nh.ipv6h->flow_lbl[1] = iph->flow_lbl[1];
227                 (*pskb)->nh.ipv6h->flow_lbl[2] = iph->flow_lbl[2];
228                 if (x->props.flags & XFRM_STATE_NOECN)
229                         IP6_ECN_clear((*pskb)->nh.ipv6h);
230         } else {
231                 memcpy((*pskb)->nh.ipv6h, iph, hdr_len);
232                 (*pskb)->nh.raw[nh_offset] = IPPROTO_AH;
233                 (*pskb)->nh.ipv6h->payload_len = htons((*pskb)->len - sizeof(struct ipv6hdr));
234                 kfree (iph);
235         }
236
237         (*pskb)->nh.raw = (*pskb)->data;
238
239         x->curlft.bytes += (*pskb)->len;
240         x->curlft.packets++;
241         spin_unlock_bh(&x->lock);
242         if (((*pskb)->dst = dst_pop(dst)) == NULL) {
243                 err = -EHOSTUNREACH;
244                 goto error_nolock;
245         }
246         return NET_XMIT_BYPASS;
247 error_free_iph:
248         kfree(iph);
249 error:
250         spin_unlock_bh(&x->lock);
251 error_nolock:
252         kfree_skb(*pskb);
253         return err;
254 }
255
256 int ah6_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
257 {
258         /*
259          * Before process AH
260          * [IPv6][Ext1][Ext2][AH][Dest][Payload]
261          * |<-------------->| hdr_len
262          * |<------------------------>| cleared_hlen
263          *
264          * To erase AH:
265          * Keeping copy of cleared headers. After AH processing,
266          * Moving the pointer of skb->nh.raw by using skb_pull as long as AH
267          * header length. Then copy back the copy as long as hdr_len
268          * If destination header following AH exists, copy it into after [Ext2].
269          * 
270          * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
271          * There is offset of AH before IPv6 header after the process.
272          */
273
274         struct ipv6_auth_hdr *ah;
275         struct ah_data *ahp;
276         unsigned char *tmp_hdr = NULL;
277         u16 hdr_len;
278         u16 ah_hlen;
279         u16 cleared_hlen;
280         u16 nh_offset = 0;
281         u8 nexthdr = 0;
282         u8 *prevhdr;
283
284         if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
285                 goto out;
286
287         /* We are going to _remove_ AH header to keep sockets happy,
288          * so... Later this can change. */
289         if (skb_cloned(skb) &&
290             pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
291                 goto out;
292
293         hdr_len = skb->data - skb->nh.raw;
294         cleared_hlen = hdr_len;
295         ah = (struct ipv6_auth_hdr*)skb->data;
296         ahp = x->data;
297         nexthdr = ah->nexthdr;
298         ah_hlen = (ah->hdrlen + 2) << 2;
299         cleared_hlen += ah_hlen;
300
301         if (nexthdr == NEXTHDR_DEST) {
302                 struct ipv6_opt_hdr *dsthdr = (struct ipv6_opt_hdr*)(skb->data + ah_hlen);
303                 cleared_hlen += ipv6_optlen(dsthdr);
304         }
305
306         if (ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_full_len) &&
307             ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len))
308                 goto out;
309
310         if (!pskb_may_pull(skb, ah_hlen))
311                 goto out;
312
313         tmp_hdr = kmalloc(cleared_hlen, GFP_ATOMIC);
314         if (!tmp_hdr)
315                 goto out;
316         memcpy(tmp_hdr, skb->nh.raw, cleared_hlen);
317         ipv6_clear_mutable_options(skb, &nh_offset, XFRM_POLICY_IN);
318         skb->nh.ipv6h->priority    = 0;
319         skb->nh.ipv6h->flow_lbl[0] = 0;
320         skb->nh.ipv6h->flow_lbl[1] = 0;
321         skb->nh.ipv6h->flow_lbl[2] = 0;
322         skb->nh.ipv6h->hop_limit   = 0;
323
324         {
325                 u8 auth_data[MAX_AH_AUTH_LEN];
326
327                 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
328                 memset(ah->auth_data, 0, ahp->icv_trunc_len);
329                 skb_push(skb, skb->data - skb->nh.raw);
330                 ahp->icv(ahp, skb, ah->auth_data);
331                 if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) {
332                         LIMIT_NETDEBUG(
333                                 printk(KERN_WARNING "ipsec ah authentication error\n"));
334                         x->stats.integrity_failed++;
335                         goto free_out;
336                 }
337         }
338
339         skb->nh.raw = skb_pull(skb, ah_hlen);
340         memcpy(skb->nh.raw, tmp_hdr, hdr_len);
341         if (nexthdr == NEXTHDR_DEST) {
342                 memcpy(skb->nh.raw + hdr_len,
343                        tmp_hdr + hdr_len + ah_hlen,
344                        cleared_hlen - hdr_len - ah_hlen);
345         }
346         prevhdr = (u8*)(skb->nh.raw + nh_offset);
347         *prevhdr = nexthdr;
348         skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
349         skb_pull(skb, hdr_len);
350         skb->h.raw = skb->data;
351
352
353         kfree(tmp_hdr);
354
355         return nexthdr;
356
357 free_out:
358         kfree(tmp_hdr);
359 out:
360         return -EINVAL;
361 }
362
363 void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 
364          int type, int code, int offset, __u32 info)
365 {
366         struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
367         struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
368         struct xfrm_state *x;
369
370         if (type != ICMPV6_DEST_UNREACH &&
371             type != ICMPV6_PKT_TOOBIG)
372                 return;
373
374         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
375         if (!x)
376                 return;
377
378         NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/"
379                         "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
380                ntohl(ah->spi), NIP6(iph->daddr)));
381
382         xfrm_state_put(x);
383 }
384
385 static int ah6_init_state(struct xfrm_state *x, void *args)
386 {
387         struct ah_data *ahp = NULL;
388         struct xfrm_algo_desc *aalg_desc;
389
390         if (!x->aalg)
391                 goto error;
392
393         /* null auth can use a zero length key */
394         if (x->aalg->alg_key_len > 512)
395                 goto error;
396
397         ahp = kmalloc(sizeof(*ahp), GFP_KERNEL);
398         if (ahp == NULL)
399                 return -ENOMEM;
400
401         memset(ahp, 0, sizeof(*ahp));
402
403         ahp->key = x->aalg->alg_key;
404         ahp->key_len = (x->aalg->alg_key_len+7)/8;
405         ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
406         if (!ahp->tfm)
407                 goto error;
408         ahp->icv = ah_hmac_digest;
409         
410         /*
411          * Lookup the algorithm description maintained by xfrm_algo,
412          * verify crypto transform properties, and store information
413          * we need for AH processing.  This lookup cannot fail here
414          * after a successful crypto_alloc_tfm().
415          */
416         aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
417         BUG_ON(!aalg_desc);
418
419         if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
420             crypto_tfm_alg_digestsize(ahp->tfm)) {
421                 printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
422                        x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm),
423                        aalg_desc->uinfo.auth.icv_fullbits/8);
424                 goto error;
425         }
426         
427         ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
428         ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
429         
430         BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
431         
432         ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
433         if (!ahp->work_icv)
434                 goto error;
435         
436         x->props.header_len = XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len);
437         if (x->props.mode)
438                 x->props.header_len += sizeof(struct ipv6hdr);
439         x->data = ahp;
440
441         return 0;
442
443 error:
444         if (ahp) {
445                 if (ahp->work_icv)
446                         kfree(ahp->work_icv);
447                 if (ahp->tfm)
448                         crypto_free_tfm(ahp->tfm);
449                 kfree(ahp);
450         }
451         return -EINVAL;
452 }
453
454 static void ah6_destroy(struct xfrm_state *x)
455 {
456         struct ah_data *ahp = x->data;
457
458         if (!ahp)
459                 return;
460
461         if (ahp->work_icv) {
462                 kfree(ahp->work_icv);
463                 ahp->work_icv = NULL;
464         }
465         if (ahp->tfm) {
466                 crypto_free_tfm(ahp->tfm);
467                 ahp->tfm = NULL;
468         }
469         kfree(ahp);
470 }
471
472 static struct xfrm_type ah6_type =
473 {
474         .description    = "AH6",
475         .owner          = THIS_MODULE,
476         .proto          = IPPROTO_AH,
477         .init_state     = ah6_init_state,
478         .destructor     = ah6_destroy,
479         .input          = ah6_input,
480         .output         = ah6_output
481 };
482
483 static struct inet6_protocol ah6_protocol = {
484         .handler        =       xfrm6_rcv,
485         .err_handler    =       ah6_err,
486         .flags          =       INET6_PROTO_NOPOLICY,
487 };
488
489 int __init ah6_init(void)
490 {
491         if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
492                 printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
493                 return -EAGAIN;
494         }
495
496         if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
497                 printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
498                 xfrm_unregister_type(&ah6_type, AF_INET6);
499                 return -EAGAIN;
500         }
501
502         return 0;
503 }
504
505 static void __exit ah6_fini(void)
506 {
507         if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
508                 printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
509
510         if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
511                 printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
512
513 }
514
515 module_init(ah6_init);
516 module_exit(ah6_fini);
517
518 MODULE_LICENSE("GPL");