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>
21 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
24 struct dst_entry *dst;
25 struct iphdr *iph = skb->nh.iph;
27 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
30 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
32 if (!(iph->frag_off & htons(IP_DF)) || skb->local_df)
38 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
45 static int xfrm4_output_one(struct sk_buff *skb)
47 struct dst_entry *dst = skb->dst;
48 struct xfrm_state *x = dst->xfrm;
51 if (skb->ip_summed == CHECKSUM_PARTIAL) {
52 err = skb_checksum_help(skb);
57 if (x->props.mode == XFRM_MODE_TUNNEL) {
58 err = xfrm4_tunnel_check_size(skb);
64 spin_lock_bh(&x->lock);
65 err = xfrm_state_check(x, skb);
69 err = x->mode->output(x, skb);
73 err = x->type->output(x, skb);
77 x->curlft.bytes += skb->len;
80 spin_unlock_bh(&x->lock);
82 if (!(skb->dst = dst_pop(dst))) {
88 } while (x && (x->props.mode != XFRM_MODE_TUNNEL));
90 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
96 spin_unlock_bh(&x->lock);
102 static int xfrm4_output_finish2(struct sk_buff *skb)
106 while (likely((err = xfrm4_output_one(skb)) == 0)) {
109 err = nf_hook(PF_INET, NF_IP_LOCAL_OUT, &skb, NULL,
110 skb->dst->dev, dst_output);
111 if (unlikely(err != 1))
115 return dst_output(skb);
117 err = nf_hook(PF_INET, NF_IP_POST_ROUTING, &skb, NULL,
118 skb->dst->dev, xfrm4_output_finish2);
119 if (unlikely(err != 1))
126 static int xfrm4_output_finish(struct sk_buff *skb)
128 struct sk_buff *segs;
130 #ifdef CONFIG_NETFILTER
131 if (!skb->dst->xfrm) {
132 IPCB(skb)->flags |= IPSKB_REROUTED;
133 return dst_output(skb);
137 if (!skb_is_gso(skb))
138 return xfrm4_output_finish2(skb);
140 skb->protocol = htons(ETH_P_IP);
141 segs = skb_gso_segment(skb, 0);
143 if (unlikely(IS_ERR(segs)))
144 return PTR_ERR(segs);
147 struct sk_buff *nskb = segs->next;
151 err = xfrm4_output_finish2(segs);
154 while ((segs = nskb)) {
168 int xfrm4_output(struct sk_buff *skb)
170 return NF_HOOK_COND(PF_INET, NF_IP_POST_ROUTING, skb, NULL, skb->dst->dev,
172 !(IPCB(skb)->flags & IPSKB_REROUTED));