2 * xfrm4_mode_tunnel.c - Tunnel mode encapsulation for IPv4.
4 * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/stringify.h>
13 #include <net/inet_ecn.h>
17 static inline void ipip_ecn_decapsulate(struct sk_buff
*skb
)
19 struct iphdr
*outer_iph
= ip_hdr(skb
);
20 struct iphdr
*inner_iph
= ipip_hdr(skb
);
22 if (INET_ECN_is_ce(outer_iph
->tos
))
23 IP_ECN_set_ce(inner_iph
);
26 static inline void ipip6_ecn_decapsulate(struct iphdr
*iph
, struct sk_buff
*skb
)
28 if (INET_ECN_is_ce(iph
->tos
))
29 IP6_ECN_set_ce(ipv6_hdr(skb
));
32 /* Add encapsulation header.
34 * The top IP header will be constructed per RFC 2401. The following fields
35 * in it shall be filled in by x->type->output:
39 * On exit, skb->h will be set to the start of the payload to be processed
40 * by x->type->output and skb->nh will be set to the top IP header.
42 static int xfrm4_tunnel_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
44 struct dst_entry
*dst
= skb
->dst
;
45 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
46 struct iphdr
*iph
, *top_iph
;
50 skb
->h
.raw
= skb
->nh
.raw
;
52 skb_push(skb
, x
->props
.header_len
);
53 skb_reset_network_header(skb
);
54 top_iph
= ip_hdr(skb
);
59 flags
= x
->props
.flags
;
62 if (xdst
->route
->ops
->family
== AF_INET
) {
63 top_iph
->protocol
= IPPROTO_IPIP
;
64 top_iph
->tos
= INET_ECN_encapsulate(iph
->tos
, iph
->tos
);
65 top_iph
->frag_off
= (flags
& XFRM_STATE_NOPMTUDISC
) ?
66 0 : (iph
->frag_off
& htons(IP_DF
));
68 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
70 struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*)iph
;
71 top_iph
->protocol
= IPPROTO_IPV6
;
72 top_iph
->tos
= INET_ECN_encapsulate(iph
->tos
, ipv6_get_dsfield(ipv6h
));
73 top_iph
->frag_off
= 0;
77 if (flags
& XFRM_STATE_NOECN
)
78 IP_ECN_clear(top_iph
);
80 if (!top_iph
->frag_off
)
81 __ip_select_ident(top_iph
, dst
->child
, 0);
83 top_iph
->ttl
= dst_metric(dst
->child
, RTAX_HOPLIMIT
);
85 top_iph
->saddr
= x
->props
.saddr
.a4
;
86 top_iph
->daddr
= x
->id
.daddr
.a4
;
88 memset(&(IPCB(skb
)->opt
), 0, sizeof(struct ip_options
));
92 static int xfrm4_tunnel_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
94 struct iphdr
*iph
= ip_hdr(skb
);
95 const unsigned char *old_mac
;
98 switch (iph
->protocol
){
101 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
109 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
112 if (skb_cloned(skb
) &&
113 (err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
)))
117 if (iph
->protocol
== IPPROTO_IPIP
) {
118 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
119 ipv4_copy_dscp(iph
, ipip_hdr(skb
));
120 if (!(x
->props
.flags
& XFRM_STATE_NOECN
))
121 ipip_ecn_decapsulate(skb
);
123 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
125 if (!(x
->props
.flags
& XFRM_STATE_NOECN
))
126 ipip6_ecn_decapsulate(iph
, skb
);
127 skb
->protocol
= htons(ETH_P_IPV6
);
130 old_mac
= skb_mac_header(skb
);
131 skb_set_mac_header(skb
, -skb
->mac_len
);
132 memmove(skb_mac_header(skb
), old_mac
, skb
->mac_len
);
133 skb_reset_network_header(skb
);
140 static struct xfrm_mode xfrm4_tunnel_mode
= {
141 .input
= xfrm4_tunnel_input
,
142 .output
= xfrm4_tunnel_output
,
143 .owner
= THIS_MODULE
,
144 .encap
= XFRM_MODE_TUNNEL
,
147 static int __init
xfrm4_tunnel_init(void)
149 return xfrm_register_mode(&xfrm4_tunnel_mode
, AF_INET
);
152 static void __exit
xfrm4_tunnel_exit(void)
156 err
= xfrm_unregister_mode(&xfrm4_tunnel_mode
, AF_INET
);
160 module_init(xfrm4_tunnel_init
);
161 module_exit(xfrm4_tunnel_exit
);
162 MODULE_LICENSE("GPL");
163 MODULE_ALIAS_XFRM_MODE(AF_INET
, XFRM_MODE_TUNNEL
);