]> err.no Git - linux-2.6/blob - net/ipv4/xfrm4_mode_tunnel.c
bac1a91f0cbe7c7bff2a60af22e9d01f22f726a7
[linux-2.6] / net / ipv4 / xfrm4_mode_tunnel.c
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
17 static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
18 {
19         struct iphdr *outer_iph = ip_hdr(skb);
20         struct iphdr *inner_iph = ipip_hdr(skb);
21
22         if (INET_ECN_is_ce(outer_iph->tos))
23                 IP_ECN_set_ce(inner_iph);
24 }
25
26 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
27 {
28         if (INET_ECN_is_ce(iph->tos))
29                 IP6_ECN_set_ce(ipv6_hdr(skb));
30 }
31
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
38  *
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.
41  */
42 static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
43 {
44         struct dst_entry *dst = skb->dst;
45         struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
46         struct iphdr *iph, *top_iph;
47         int flags;
48
49         iph = ip_hdr(skb);
50         skb->transport_header = skb->network_header;
51
52         skb_set_network_header(skb, -x->props.header_len);
53         top_iph = ip_hdr(skb);
54
55         top_iph->ihl = 5;
56         top_iph->version = 4;
57
58         flags = x->props.flags;
59
60         /* DS disclosed */
61         if (xdst->route->ops->family == AF_INET) {
62                 top_iph->protocol = IPPROTO_IPIP;
63                 top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
64                 top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
65                         0 : (iph->frag_off & htons(IP_DF));
66         }
67 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
68         else {
69                 struct ipv6hdr *ipv6h = (struct ipv6hdr*)iph;
70                 top_iph->protocol = IPPROTO_IPV6;
71                 top_iph->tos = INET_ECN_encapsulate(iph->tos, ipv6_get_dsfield(ipv6h));
72                 top_iph->frag_off = 0;
73         }
74 #endif
75
76         if (flags & XFRM_STATE_NOECN)
77                 IP_ECN_clear(top_iph);
78
79         if (!top_iph->frag_off)
80                 __ip_select_ident(top_iph, dst->child, 0);
81
82         top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
83
84         top_iph->saddr = x->props.saddr.a4;
85         top_iph->daddr = x->id.daddr.a4;
86
87         skb->protocol = htons(ETH_P_IP);
88
89         memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
90         return 0;
91 }
92
93 static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
94 {
95         struct iphdr *iph = ip_hdr(skb);
96         const unsigned char *old_mac;
97         int err = -EINVAL;
98
99         switch (iph->protocol){
100                 case IPPROTO_IPIP:
101                         break;
102 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
103                 case IPPROTO_IPV6:
104                         break;
105 #endif
106                 default:
107                         goto out;
108         }
109
110         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
111                 goto out;
112
113         if (skb_cloned(skb) &&
114             (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
115                 goto out;
116
117         iph = ip_hdr(skb);
118         if (iph->protocol == IPPROTO_IPIP) {
119                 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
120                         ipv4_copy_dscp(iph, ipip_hdr(skb));
121                 if (!(x->props.flags & XFRM_STATE_NOECN))
122                         ipip_ecn_decapsulate(skb);
123         }
124 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
125         else {
126                 if (!(x->props.flags & XFRM_STATE_NOECN))
127                         ipip6_ecn_decapsulate(iph, skb);
128                 skb->protocol = htons(ETH_P_IPV6);
129         }
130 #endif
131         old_mac = skb_mac_header(skb);
132         skb_set_mac_header(skb, -skb->mac_len);
133         memmove(skb_mac_header(skb), old_mac, skb->mac_len);
134         skb_reset_network_header(skb);
135         err = 0;
136
137 out:
138         return err;
139 }
140
141 static struct xfrm_mode xfrm4_tunnel_mode = {
142         .input = xfrm4_tunnel_input,
143         .output = xfrm4_tunnel_output,
144         .owner = THIS_MODULE,
145         .encap = XFRM_MODE_TUNNEL,
146 };
147
148 static int __init xfrm4_tunnel_init(void)
149 {
150         return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
151 }
152
153 static void __exit xfrm4_tunnel_exit(void)
154 {
155         int err;
156
157         err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
158         BUG_ON(err);
159 }
160
161 module_init(xfrm4_tunnel_init);
162 module_exit(xfrm4_tunnel_exit);
163 MODULE_LICENSE("GPL");
164 MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);