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
;
26 if (IPCB(skb
)->flags
& IPSKB_XFRM_TUNNEL_SIZE
)
29 IPCB(skb
)->flags
|= IPSKB_XFRM_TUNNEL_SIZE
;
31 if (!(ip_hdr(skb
)->frag_off
& htons(IP_DF
)) || skb
->local_df
)
37 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
, htonl(mtu
));
44 static int xfrm4_output_one(struct sk_buff
*skb
)
46 struct dst_entry
*dst
= skb
->dst
;
47 struct xfrm_state
*x
= dst
->xfrm
;
50 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
51 err
= skb_checksum_help(skb
);
56 if (x
->props
.mode
== XFRM_MODE_TUNNEL
) {
57 err
= xfrm4_tunnel_check_size(skb
);
63 spin_lock_bh(&x
->lock
);
64 err
= xfrm_state_check(x
, skb
);
68 err
= x
->mode
->output(x
, skb
);
72 err
= x
->type
->output(x
, skb
);
76 x
->curlft
.bytes
+= skb
->len
;
79 spin_unlock_bh(&x
->lock
);
81 if (!(skb
->dst
= dst_pop(dst
))) {
87 } while (x
&& (x
->props
.mode
!= XFRM_MODE_TUNNEL
));
89 IPCB(skb
)->flags
|= IPSKB_XFRM_TRANSFORMED
;
95 spin_unlock_bh(&x
->lock
);
101 static int xfrm4_output_finish2(struct sk_buff
*skb
)
105 while (likely((err
= xfrm4_output_one(skb
)) == 0)) {
108 err
= nf_hook(PF_INET
, NF_IP_LOCAL_OUT
, &skb
, NULL
,
109 skb
->dst
->dev
, dst_output
);
110 if (unlikely(err
!= 1))
114 return dst_output(skb
);
116 err
= nf_hook(PF_INET
, NF_IP_POST_ROUTING
, &skb
, NULL
,
117 skb
->dst
->dev
, xfrm4_output_finish2
);
118 if (unlikely(err
!= 1))
125 static int xfrm4_output_finish(struct sk_buff
*skb
)
127 struct sk_buff
*segs
;
129 #ifdef CONFIG_NETFILTER
130 if (!skb
->dst
->xfrm
) {
131 IPCB(skb
)->flags
|= IPSKB_REROUTED
;
132 return dst_output(skb
);
136 if (!skb_is_gso(skb
))
137 return xfrm4_output_finish2(skb
);
139 skb
->protocol
= htons(ETH_P_IP
);
140 segs
= skb_gso_segment(skb
, 0);
142 if (unlikely(IS_ERR(segs
)))
143 return PTR_ERR(segs
);
146 struct sk_buff
*nskb
= segs
->next
;
150 err
= xfrm4_output_finish2(segs
);
153 while ((segs
= nskb
)) {
167 int xfrm4_output(struct sk_buff
*skb
)
169 return NF_HOOK_COND(PF_INET
, NF_IP_POST_ROUTING
, skb
, NULL
, skb
->dst
->dev
,
171 !(IPCB(skb
)->flags
& IPSKB_REROUTED
));