[IPSEC]: Move common output code to xfrm_output
[linux-2.6/kmemtrace.git] / net / ipv6 / xfrm6_output.c
blobf21596f89984ed6952d4e4dfaa62ab19c74e97e0
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>
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.
12 #include <linux/if_ether.h>
13 #include <linux/compiler.h>
14 #include <linux/skbuff.h>
15 #include <linux/icmpv6.h>
16 #include <linux/netfilter_ipv6.h>
17 #include <net/ipv6.h>
18 #include <net/xfrm.h>
20 int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
21 u8 **prevhdr)
23 return ip6_find_1stfragopt(skb, prevhdr);
26 EXPORT_SYMBOL(xfrm6_find_1stfragopt);
28 static int xfrm6_tunnel_check_size(struct sk_buff *skb)
30 int mtu, ret = 0;
31 struct dst_entry *dst = skb->dst;
33 mtu = dst_mtu(dst);
34 if (mtu < IPV6_MIN_MTU)
35 mtu = IPV6_MIN_MTU;
37 if (skb->len > mtu) {
38 skb->dev = dst->dev;
39 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
40 ret = -EMSGSIZE;
43 return ret;
46 static inline int xfrm6_output_one(struct sk_buff *skb)
48 struct dst_entry *dst = skb->dst;
49 struct xfrm_state *x = dst->xfrm;
50 int err;
52 if (x->props.mode == XFRM_MODE_TUNNEL) {
53 err = xfrm6_tunnel_check_size(skb);
54 if (err)
55 goto error_nolock;
58 err = xfrm_output(skb);
59 if (err)
60 goto error_nolock;
62 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
63 err = 0;
65 out_exit:
66 return err;
67 error_nolock:
68 kfree_skb(skb);
69 goto out_exit;
72 static int xfrm6_output_finish2(struct sk_buff *skb)
74 int err;
76 while (likely((err = xfrm6_output_one(skb)) == 0)) {
77 nf_reset(skb);
79 err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, &skb, NULL,
80 skb->dst->dev, dst_output);
81 if (unlikely(err != 1))
82 break;
84 if (!skb->dst->xfrm)
85 return dst_output(skb);
87 err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, &skb, NULL,
88 skb->dst->dev, xfrm6_output_finish2);
89 if (unlikely(err != 1))
90 break;
93 return err;
96 static int xfrm6_output_finish(struct sk_buff *skb)
98 struct sk_buff *segs;
100 if (!skb_is_gso(skb))
101 return xfrm6_output_finish2(skb);
103 skb->protocol = htons(ETH_P_IPV6);
104 segs = skb_gso_segment(skb, 0);
105 kfree_skb(skb);
106 if (unlikely(IS_ERR(segs)))
107 return PTR_ERR(segs);
109 do {
110 struct sk_buff *nskb = segs->next;
111 int err;
113 segs->next = NULL;
114 err = xfrm6_output_finish2(segs);
116 if (unlikely(err)) {
117 while ((segs = nskb)) {
118 nskb = segs->next;
119 segs->next = NULL;
120 kfree_skb(segs);
122 return err;
125 segs = nskb;
126 } while (segs);
128 return 0;
131 int xfrm6_output(struct sk_buff *skb)
133 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev,
134 xfrm6_output_finish);