[IPSEC]: Get rid of ipv6_{auth,esp,comp}_hdr
[deliverable/linux.git] / net / ipv4 / xfrm4_mode_tunnel.c
CommitLineData
b59f45d0
HX
1/*
2 * xfrm4_mode_tunnel.c - Tunnel mode encapsulation for IPv4.
3 *
4 * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
5 */
6
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>
12#include <net/dst.h>
13#include <net/inet_ecn.h>
14#include <net/ip.h>
15#include <net/xfrm.h>
16
17static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
18{
eddc9ec5 19 struct iphdr *outer_iph = ip_hdr(skb);
b0061ce4 20 struct iphdr *inner_iph = ipip_hdr(skb);
b59f45d0
HX
21
22 if (INET_ECN_is_ce(outer_iph->tos))
23 IP_ECN_set_ce(inner_iph);
24}
25
c82f963e
MK
26static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
27{
28 if (INET_ECN_is_ce(iph->tos))
0660e03f 29 IP6_ECN_set_ce(ipv6_hdr(skb));
c82f963e
MK
30}
31
b59f45d0
HX
32/* Add encapsulation header.
33 *
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:
36 * tot_len
37 * check
b59f45d0 38 */
eb878e84 39static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
b59f45d0
HX
40{
41 struct dst_entry *dst = skb->dst;
c82f963e 42 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
b59f45d0
HX
43 struct iphdr *iph, *top_iph;
44 int flags;
45
eddc9ec5 46 iph = ip_hdr(skb);
b59f45d0 47
7b277b1a 48 skb_set_network_header(skb, -x->props.header_len);
37fedd3a
HX
49 skb->mac_header = skb->network_header +
50 offsetof(struct iphdr, protocol);
51 skb->transport_header = skb->network_header + sizeof(*iph);
eddc9ec5 52 top_iph = ip_hdr(skb);
b59f45d0
HX
53
54 top_iph->ihl = 5;
55 top_iph->version = 4;
56
c82f963e
MK
57 flags = x->props.flags;
58
b59f45d0 59 /* DS disclosed */
c82f963e
MK
60 if (xdst->route->ops->family == AF_INET) {
61 top_iph->protocol = IPPROTO_IPIP;
62 top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
63 top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
64 0 : (iph->frag_off & htons(IP_DF));
65 }
66#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
67 else {
68 struct ipv6hdr *ipv6h = (struct ipv6hdr*)iph;
69 top_iph->protocol = IPPROTO_IPV6;
70 top_iph->tos = INET_ECN_encapsulate(iph->tos, ipv6_get_dsfield(ipv6h));
71 top_iph->frag_off = 0;
72 }
73#endif
b59f45d0 74
b59f45d0
HX
75 if (flags & XFRM_STATE_NOECN)
76 IP_ECN_clear(top_iph);
77
b59f45d0
HX
78 if (!top_iph->frag_off)
79 __ip_select_ident(top_iph, dst->child, 0);
80
81 top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
82
83 top_iph->saddr = x->props.saddr.a4;
84 top_iph->daddr = x->id.daddr.a4;
b59f45d0 85
f282d45c
KM
86 skb->protocol = htons(ETH_P_IP);
87
b59f45d0
HX
88 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
89 return 0;
90}
91
92static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
93{
eddc9ec5 94 struct iphdr *iph = ip_hdr(skb);
39f69c6f 95 const unsigned char *old_mac;
b59f45d0
HX
96 int err = -EINVAL;
97
132adf54 98 switch (iph->protocol){
c82f963e 99 case IPPROTO_IPIP:
e2e01bfe 100 break;
c82f963e
MK
101#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
102 case IPPROTO_IPV6:
103 break;
104#endif
105 default:
106 goto out;
107 }
108
b59f45d0
HX
109 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
110 goto out;
111
112 if (skb_cloned(skb) &&
113 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
114 goto out;
115
eddc9ec5 116 iph = ip_hdr(skb);
c82f963e
MK
117 if (iph->protocol == IPPROTO_IPIP) {
118 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
b0061ce4 119 ipv4_copy_dscp(iph, ipip_hdr(skb));
c82f963e
MK
120 if (!(x->props.flags & XFRM_STATE_NOECN))
121 ipip_ecn_decapsulate(skb);
122 }
123#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
124 else {
125 if (!(x->props.flags & XFRM_STATE_NOECN))
126 ipip6_ecn_decapsulate(iph, skb);
127 skb->protocol = htons(ETH_P_IPV6);
128 }
129#endif
98e399f8 130 old_mac = skb_mac_header(skb);
39f69c6f 131 skb_set_mac_header(skb, -skb->mac_len);
98e399f8 132 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
c1d2bbe1 133 skb_reset_network_header(skb);
b59f45d0
HX
134 err = 0;
135
136out:
137 return err;
138}
139
140static 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,
145};
146
147static int __init xfrm4_tunnel_init(void)
148{
149 return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
150}
151
152static void __exit xfrm4_tunnel_exit(void)
153{
154 int err;
155
156 err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
157 BUG_ON(err);
158}
159
160module_init(xfrm4_tunnel_init);
161module_exit(xfrm4_tunnel_exit);
162MODULE_LICENSE("GPL");
163MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);
This page took 0.185255 seconds and 5 git commands to generate.