2 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
3 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
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.
11 #include <linux/if_ether.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/netfilter_ipv4.h>
21 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
24 struct dst_entry *dst;
26 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
29 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->local_df)
35 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
42 int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
46 err = xfrm4_tunnel_check_size(skb);
50 return xfrm4_extract_header(skb);
53 int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
57 err = x->inner_mode->afinfo->extract_output(x, skb);
61 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
62 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
64 skb->protocol = htons(ETH_P_IP);
66 return x->outer_mode->output2(x, skb);
68 EXPORT_SYMBOL(xfrm4_prepare_output);
70 static inline int xfrm4_output_one(struct sk_buff *skb)
75 err = xfrm_output(skb);
80 iph->tot_len = htons(skb->len);
83 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
93 static int xfrm4_output_finish2(struct sk_buff *skb)
97 while (likely((err = xfrm4_output_one(skb)) == 0)) {
100 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, skb, NULL,
101 skb->dst->dev, dst_output);
102 if (unlikely(err != 1))
106 return dst_output(skb);
108 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, skb, NULL,
109 skb->dst->dev, xfrm4_output_finish2);
110 if (unlikely(err != 1))
117 static int xfrm4_output_finish(struct sk_buff *skb)
119 struct sk_buff *segs;
121 #ifdef CONFIG_NETFILTER
122 if (!skb->dst->xfrm) {
123 IPCB(skb)->flags |= IPSKB_REROUTED;
124 return dst_output(skb);
128 if (!skb_is_gso(skb))
129 return xfrm4_output_finish2(skb);
131 skb->protocol = htons(ETH_P_IP);
132 segs = skb_gso_segment(skb, 0);
134 if (unlikely(IS_ERR(segs)))
135 return PTR_ERR(segs);
138 struct sk_buff *nskb = segs->next;
142 err = xfrm4_output_finish2(segs);
145 while ((segs = nskb)) {
159 int xfrm4_output(struct sk_buff *skb)
161 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
163 !(IPCB(skb)->flags & IPSKB_REROUTED));