This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / ipv6 / xfrm6_output.c
1 /*
2  * xfrm6_output.c - Common IPsec encapsulation code for IPv6.
3  * Copyright (C) 2002 USAGI/WIDE Project
4  * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
5  * 
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/skbuff.h>
13 #include <linux/spinlock.h>
14 #include <linux/icmpv6.h>
15 #include <net/inet_ecn.h>
16 #include <net/ipv6.h>
17 #include <net/xfrm.h>
18
19 /* Add encapsulation header.
20  *
21  * In transport mode, the IP header and mutable extension headers will be moved
22  * forward to make space for the encapsulation header.
23  *
24  * In tunnel mode, the top IP header will be constructed per RFC 2401.
25  * The following fields in it shall be filled in by x->type->output:
26  *      payload_len
27  *
28  * On exit, skb->h will be set to the start of the encapsulation header to be
29  * filled in by x->type->output and skb->nh will be set to the nextheader field
30  * of the extension header directly preceding the encapsulation header, or in
31  * its absence, that of the top IP header.  The value of skb->data will always
32  * point to the top IP header.
33  */
34 static void xfrm6_encap(struct sk_buff *skb)
35 {
36         struct dst_entry *dst = skb->dst;
37         struct xfrm_state *x = dst->xfrm;
38         struct ipv6hdr *iph, *top_iph;
39
40         skb_push(skb, x->props.header_len);
41         iph = skb->nh.ipv6h;
42
43         if (!x->props.mode) {
44                 u8 *prevhdr;
45                 int hdr_len;
46
47                 hdr_len = ip6_find_1stfragopt(skb, &prevhdr);
48                 skb->nh.raw = prevhdr - x->props.header_len;
49                 skb->h.raw = skb->data + hdr_len;
50                 memmove(skb->data, iph, hdr_len);
51                 return;
52         }
53
54         skb->nh.raw = skb->data;
55         top_iph = skb->nh.ipv6h;
56         skb->nh.raw = &top_iph->nexthdr;
57         skb->h.ipv6h = top_iph + 1;
58
59         top_iph->version = 6;
60         top_iph->priority = iph->priority;
61         if (x->props.flags & XFRM_STATE_NOECN)
62                 IP6_ECN_clear(top_iph);
63         top_iph->flow_lbl[0] = iph->flow_lbl[0];
64         top_iph->flow_lbl[1] = iph->flow_lbl[1];
65         top_iph->flow_lbl[2] = iph->flow_lbl[2];
66         top_iph->nexthdr = IPPROTO_IPV6; 
67         top_iph->hop_limit = iph->hop_limit;
68         ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
69         ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
70 }
71
72 static int xfrm6_tunnel_check_size(struct sk_buff *skb)
73 {
74         int mtu, ret = 0;
75         struct dst_entry *dst = skb->dst;
76
77         mtu = dst_pmtu(dst) - sizeof(struct ipv6hdr);
78         if (mtu < IPV6_MIN_MTU)
79                 mtu = IPV6_MIN_MTU;
80
81         if (skb->len > mtu) {
82                 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
83                 ret = -EMSGSIZE;
84         }
85
86         return ret;
87 }
88
89 int xfrm6_output(struct sk_buff **pskb)
90 {
91         struct sk_buff *skb = *pskb;
92         struct dst_entry *dst = skb->dst;
93         struct xfrm_state *x = dst->xfrm;
94         int err;
95         
96         if (skb->ip_summed == CHECKSUM_HW) {
97                 err = skb_checksum_help(pskb, 0);
98                 skb = *pskb;
99                 if (err)
100                         goto error_nolock;
101         }
102
103         spin_lock_bh(&x->lock);
104         err = xfrm_state_check(x, skb);
105         if (err)
106                 goto error;
107
108         if (x->props.mode) {
109                 err = xfrm6_tunnel_check_size(skb);
110                 if (err)
111                         goto error;
112         }
113
114         xfrm6_encap(skb);
115
116         err = x->type->output(pskb);
117         skb = *pskb;
118         if (err)
119                 goto error;
120
121         x->curlft.bytes += skb->len;
122         x->curlft.packets++;
123
124         spin_unlock_bh(&x->lock);
125
126         skb->nh.raw = skb->data;
127         
128         if (!(skb->dst = dst_pop(dst))) {
129                 err = -EHOSTUNREACH;
130                 goto error_nolock;
131         }
132         err = NET_XMIT_BYPASS;
133
134 out_exit:
135         return err;
136 error:
137         spin_unlock_bh(&x->lock);
138 error_nolock:
139         kfree_skb(skb);
140         goto out_exit;
141 }