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/spinlock.h>
16 #include <linux/netfilter_ipv4.h>
17 #include <net/inet_ecn.h>
22 extern int skb_checksum_setup(struct sk_buff *skb);
24 /* Add encapsulation header.
26 * In transport mode, the IP header will be moved forward to make space
27 * for the encapsulation header.
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:
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.
37 static void xfrm4_encap(struct sk_buff *skb)
39 struct dst_entry *dst = skb->dst;
40 struct xfrm_state *x = dst->xfrm;
41 struct iphdr *iph, *top_iph;
47 skb->nh.raw = skb_push(skb, x->props.header_len);
48 top_iph = skb->nh.iph;
51 skb->h.raw += iph->ihl*4;
52 memmove(top_iph, iph, iph->ihl*4);
60 top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
62 flags = x->props.flags;
63 if (flags & XFRM_STATE_NOECN)
64 IP_ECN_clear(top_iph);
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);
71 top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
73 top_iph->saddr = x->props.saddr.a4;
74 top_iph->daddr = x->id.daddr.a4;
75 top_iph->protocol = IPPROTO_IPIP;
77 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
80 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
83 struct dst_entry *dst;
84 struct iphdr *iph = skb->nh.iph;
86 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
89 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
91 if (!(iph->frag_off & htons(IP_DF)) || skb->local_df)
97 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
104 static int xfrm4_output_one(struct sk_buff *skb)
106 struct dst_entry *dst = skb->dst;
107 struct xfrm_state *x = dst->xfrm;
110 err = skb_checksum_setup(skb);
114 if (skb->ip_summed == CHECKSUM_HW) {
115 err = skb_checksum_help(skb, 0);
121 err = xfrm4_tunnel_check_size(skb);
127 spin_lock_bh(&x->lock);
128 err = xfrm_state_check(x, skb);
134 err = x->type->output(x, skb);
138 x->curlft.bytes += skb->len;
141 spin_unlock_bh(&x->lock);
143 if (!(skb->dst = dst_pop(dst))) {
149 } while (x && !x->props.mode);
151 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
157 spin_unlock_bh(&x->lock);
163 static int xfrm4_output_finish2(struct sk_buff *skb)
167 while (likely((err = xfrm4_output_one(skb)) == 0)) {
170 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
171 skb->dst->dev, dst_output);
172 if (unlikely(err != 1))
176 return dst_output(skb);
178 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
179 skb->dst->dev, xfrm4_output_finish2);
180 if (unlikely(err != 1))
187 static int xfrm4_output_finish(struct sk_buff *skb)
189 struct sk_buff *segs;
191 #ifdef CONFIG_NETFILTER
192 if (!skb->dst->xfrm) {
193 IPCB(skb)->flags |= IPSKB_REROUTED;
194 return dst_output(skb);
198 if (!skb_is_gso(skb))
199 return xfrm4_output_finish2(skb);
201 skb->protocol = htons(ETH_P_IP);
202 segs = skb_gso_segment(skb, 0);
204 if (unlikely(IS_ERR(segs)))
205 return PTR_ERR(segs);
208 struct sk_buff *nskb = segs->next;
212 err = xfrm4_output_finish2(segs);
215 while ((segs = nskb)) {
229 int xfrm4_output(struct sk_buff *skb)
231 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
233 !(IPCB(skb)->flags & IPSKB_REROUTED));