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/compiler.h>
12 #include <linux/if_ether.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/netfilter_ipv4.h>
20 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
23 struct dst_entry *dst;
25 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
28 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
30 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->local_df)
36 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
43 static inline int xfrm4_output_one(struct sk_buff *skb)
45 struct dst_entry *dst = skb->dst;
46 struct xfrm_state *x = dst->xfrm;
50 if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
51 err = xfrm4_tunnel_check_size(skb);
56 err = xfrm_output(skb);
61 iph->tot_len = htons(skb->len);
64 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
74 static int xfrm4_output_finish2(struct sk_buff *skb)
78 while (likely((err = xfrm4_output_one(skb)) == 0)) {
81 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, skb, NULL,
82 skb->dst->dev, dst_output);
83 if (unlikely(err != 1))
87 return dst_output(skb);
89 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, skb, NULL,
90 skb->dst->dev, xfrm4_output_finish2);
91 if (unlikely(err != 1))
98 static int xfrm4_output_finish(struct sk_buff *skb)
100 struct sk_buff *segs;
102 #ifdef CONFIG_NETFILTER
103 if (!skb->dst->xfrm) {
104 IPCB(skb)->flags |= IPSKB_REROUTED;
105 return dst_output(skb);
109 if (!skb_is_gso(skb))
110 return xfrm4_output_finish2(skb);
112 skb->protocol = htons(ETH_P_IP);
113 segs = skb_gso_segment(skb, 0);
115 if (unlikely(IS_ERR(segs)))
116 return PTR_ERR(segs);
119 struct sk_buff *nskb = segs->next;
123 err = xfrm4_output_finish2(segs);
126 while ((segs = nskb)) {
140 int xfrm4_output(struct sk_buff *skb)
142 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
144 !(IPCB(skb)->flags & IPSKB_REROUTED));