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