Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / net / ipv4 / xfrm4_output.c
1 /*
2  * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
3  * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version
8  * 2 of the License, or (at your option) any later version.
9  */
10
11 #include <linux/compiler.h>
12 #include <linux/if_ether.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/spinlock.h>
16 #include <linux/netfilter_ipv4.h>
17 #include <net/inet_ecn.h>
18 #include <net/ip.h>
19 #include <net/xfrm.h>
20 #include <net/icmp.h>
21
22 extern int skb_checksum_setup(struct sk_buff *skb);
23
24 /* Add encapsulation header.
25  *
26  * In transport mode, the IP header will be moved forward to make space
27  * for the encapsulation header.
28  *
29  * In tunnel mode, the top IP header will be constructed per RFC 2401.
30  * The following fields in it shall be filled in by x->type->output:
31  *      tot_len
32  *      check
33  *
34  * On exit, skb->h will be set to the start of the payload to be processed
35  * by x->type->output and skb->nh will be set to the top IP header.
36  */
37 static void xfrm4_encap(struct sk_buff *skb)
38 {
39         struct dst_entry *dst = skb->dst;
40         struct xfrm_state *x = dst->xfrm;
41         struct iphdr *iph, *top_iph;
42         int flags;
43
44         iph = skb->nh.iph;
45         skb->h.ipiph = iph;
46
47         skb->nh.raw = skb_push(skb, x->props.header_len);
48         top_iph = skb->nh.iph;
49
50         if (!x->props.mode) {
51                 skb->h.raw += iph->ihl*4;
52                 memmove(top_iph, iph, iph->ihl*4);
53                 return;
54         }
55
56         top_iph->ihl = 5;
57         top_iph->version = 4;
58
59         /* DS disclosed */
60         top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
61
62         flags = x->props.flags;
63         if (flags & XFRM_STATE_NOECN)
64                 IP_ECN_clear(top_iph);
65
66         top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
67                 0 : (iph->frag_off & htons(IP_DF));
68         if (!top_iph->frag_off)
69                 __ip_select_ident(top_iph, dst->child, 0);
70
71         top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
72
73         top_iph->saddr = x->props.saddr.a4;
74         top_iph->daddr = x->id.daddr.a4;
75         top_iph->protocol = IPPROTO_IPIP;
76
77         memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
78 }
79
80 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
81 {
82         int mtu, ret = 0;
83         struct dst_entry *dst;
84         struct iphdr *iph = skb->nh.iph;
85
86         if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
87                 goto out;
88
89         IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
90         
91         if (!(iph->frag_off & htons(IP_DF)) || skb->local_df)
92                 goto out;
93
94         dst = skb->dst;
95         mtu = dst_mtu(dst);
96         if (skb->len > mtu) {
97                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
98                 ret = -EMSGSIZE;
99         }
100 out:
101         return ret;
102 }
103
104 static int xfrm4_output_one(struct sk_buff *skb)
105 {
106         struct dst_entry *dst = skb->dst;
107         struct xfrm_state *x = dst->xfrm;
108         int err;
109         
110         err = skb_checksum_setup(skb);
111         if (err)
112                 goto error_nolock;
113
114         if (skb->ip_summed == CHECKSUM_HW) {
115                 err = skb_checksum_help(skb, 0);
116                 if (err)
117                         goto error_nolock;
118         }
119
120         if (x->props.mode) {
121                 err = xfrm4_tunnel_check_size(skb);
122                 if (err)
123                         goto error_nolock;
124         }
125
126         do {
127                 spin_lock_bh(&x->lock);
128                 err = xfrm_state_check(x, skb);
129                 if (err)
130                         goto error;
131
132                 xfrm4_encap(skb);
133
134                 err = x->type->output(x, skb);
135                 if (err)
136                         goto error;
137
138                 x->curlft.bytes += skb->len;
139                 x->curlft.packets++;
140
141                 spin_unlock_bh(&x->lock);
142         
143                 if (!(skb->dst = dst_pop(dst))) {
144                         err = -EHOSTUNREACH;
145                         goto error_nolock;
146                 }
147                 dst = skb->dst;
148                 x = dst->xfrm;
149         } while (x && !x->props.mode);
150
151         IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
152         err = 0;
153
154 out_exit:
155         return err;
156 error:
157         spin_unlock_bh(&x->lock);
158 error_nolock:
159         kfree_skb(skb);
160         goto out_exit;
161 }
162
163 static int xfrm4_output_finish2(struct sk_buff *skb)
164 {
165         int err;
166
167         while (likely((err = xfrm4_output_one(skb)) == 0)) {
168                 nf_reset(skb);
169
170                 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
171                               skb->dst->dev, dst_output);
172                 if (unlikely(err != 1))
173                         break;
174
175                 if (!skb->dst->xfrm)
176                         return dst_output(skb);
177
178                 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
179                               skb->dst->dev, xfrm4_output_finish2);
180                 if (unlikely(err != 1))
181                         break;
182         }
183
184         return err;
185 }
186
187 static int xfrm4_output_finish(struct sk_buff *skb)
188 {
189         struct sk_buff *segs;
190
191 #ifdef CONFIG_NETFILTER
192         if (!skb->dst->xfrm) {
193                 IPCB(skb)->flags |= IPSKB_REROUTED;
194                 return dst_output(skb);
195         }
196 #endif
197
198         if (!skb_is_gso(skb))
199                 return xfrm4_output_finish2(skb);
200
201         skb->protocol = htons(ETH_P_IP);
202         segs = skb_gso_segment(skb, 0);
203         kfree_skb(skb);
204         if (unlikely(IS_ERR(segs)))
205                 return PTR_ERR(segs);
206
207         do {
208                 struct sk_buff *nskb = segs->next;
209                 int err;
210
211                 segs->next = NULL;
212                 err = xfrm4_output_finish2(segs);
213
214                 if (unlikely(err)) {
215                         while ((segs = nskb)) {
216                                 nskb = segs->next;
217                                 segs->next = NULL;
218                                 kfree_skb(segs);
219                         }
220                         return err;
221                 }
222
223                 segs = nskb;
224         } while (segs);
225
226         return 0;
227 }
228
229 int xfrm4_output(struct sk_buff *skb)
230 {
231         return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
232                             xfrm4_output_finish,
233                             !(IPCB(skb)->flags & IPSKB_REROUTED));
234 }