[IPSEC]: Move common output code to xfrm_output
[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/netfilter_ipv4.h>
16 #include <net/ip.h>
17 #include <net/xfrm.h>
18 #include <net/icmp.h>
19
20 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
21 {
22         int mtu, ret = 0;
23         struct dst_entry *dst;
24
25         if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
26                 goto out;
27
28         IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
29
30         if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->local_df)
31                 goto out;
32
33         dst = skb->dst;
34         mtu = dst_mtu(dst);
35         if (skb->len > mtu) {
36                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
37                 ret = -EMSGSIZE;
38         }
39 out:
40         return ret;
41 }
42
43 static inline int xfrm4_output_one(struct sk_buff *skb)
44 {
45         struct dst_entry *dst = skb->dst;
46         struct xfrm_state *x = dst->xfrm;
47         int err;
48
49         if (x->props.mode == XFRM_MODE_TUNNEL) {
50                 err = xfrm4_tunnel_check_size(skb);
51                 if (err)
52                         goto error_nolock;
53         }
54
55         err = xfrm_output(skb);
56         if (err)
57                 goto error_nolock;
58
59         IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
60         err = 0;
61
62 out_exit:
63         return err;
64 error_nolock:
65         kfree_skb(skb);
66         goto out_exit;
67 }
68
69 static int xfrm4_output_finish2(struct sk_buff *skb)
70 {
71         int err;
72
73         while (likely((err = xfrm4_output_one(skb)) == 0)) {
74                 nf_reset(skb);
75
76                 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
77                               skb->dst->dev, dst_output);
78                 if (unlikely(err != 1))
79                         break;
80
81                 if (!skb->dst->xfrm)
82                         return dst_output(skb);
83
84                 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
85                               skb->dst->dev, xfrm4_output_finish2);
86                 if (unlikely(err != 1))
87                         break;
88         }
89
90         return err;
91 }
92
93 static int xfrm4_output_finish(struct sk_buff *skb)
94 {
95         struct sk_buff *segs;
96
97 #ifdef CONFIG_NETFILTER
98         if (!skb->dst->xfrm) {
99                 IPCB(skb)->flags |= IPSKB_REROUTED;
100                 return dst_output(skb);
101         }
102 #endif
103
104         if (!skb_is_gso(skb))
105                 return xfrm4_output_finish2(skb);
106
107         skb->protocol = htons(ETH_P_IP);
108         segs = skb_gso_segment(skb, 0);
109         kfree_skb(skb);
110         if (unlikely(IS_ERR(segs)))
111                 return PTR_ERR(segs);
112
113         do {
114                 struct sk_buff *nskb = segs->next;
115                 int err;
116
117                 segs->next = NULL;
118                 err = xfrm4_output_finish2(segs);
119
120                 if (unlikely(err)) {
121                         while ((segs = nskb)) {
122                                 nskb = segs->next;
123                                 segs->next = NULL;
124                                 kfree_skb(segs);
125                         }
126                         return err;
127                 }
128
129                 segs = nskb;
130         } while (segs);
131
132         return 0;
133 }
134
135 int xfrm4_output(struct sk_buff *skb)
136 {
137         return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
138                             xfrm4_output_finish,
139                             !(IPCB(skb)->flags & IPSKB_REROUTED));
140 }