blob: ceb4376f572ae8cd6c7b95845efc38d9820d84bc [file] [log] [blame]
Herbert Xub59f45d2006-05-27 23:05:54 -07001/*
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{
19 struct iphdr *outer_iph = skb->nh.iph;
20 struct iphdr *inner_iph = skb->h.ipiph;
21
22 if (INET_ECN_is_ce(outer_iph->tos))
23 IP_ECN_set_ce(inner_iph);
24}
25
Miika Komuc82f9632007-02-06 14:27:02 -080026static 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(skb->nh.ipv6h);
30}
31
Herbert Xub59f45d2006-05-27 23:05:54 -070032/* 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 */
Jamal Hadi Salimeb878e82006-08-31 17:42:59 -070042static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
Herbert Xub59f45d2006-05-27 23:05:54 -070043{
44 struct dst_entry *dst = skb->dst;
Miika Komuc82f9632007-02-06 14:27:02 -080045 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
Herbert Xub59f45d2006-05-27 23:05:54 -070046 struct iphdr *iph, *top_iph;
47 int flags;
48
49 iph = skb->nh.iph;
50 skb->h.ipiph = iph;
51
52 skb->nh.raw = skb_push(skb, x->props.header_len);
53 top_iph = skb->nh.iph;
54
55 top_iph->ihl = 5;
56 top_iph->version = 4;
57
Herbert Xub59f45d2006-05-27 23:05:54 -070058 flags = x->props.flags;
Miika Komuc82f9632007-02-06 14:27:02 -080059
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
Herbert Xub59f45d2006-05-27 23:05:54 -070076 if (flags & XFRM_STATE_NOECN)
77 IP_ECN_clear(top_iph);
78
Herbert Xub59f45d2006-05-27 23:05:54 -070079 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;
Herbert Xub59f45d2006-05-27 23:05:54 -070086
87 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
88 return 0;
89}
90
91static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
92{
93 struct iphdr *iph = skb->nh.iph;
94 int err = -EINVAL;
95
Miika Komuc82f9632007-02-06 14:27:02 -080096 switch(iph->protocol){
97 case IPPROTO_IPIP:
Patrick McHardye2e01bf2007-02-12 20:27:10 -080098 break;
Miika Komuc82f9632007-02-06 14:27:02 -080099#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
100 case IPPROTO_IPV6:
101 break;
102#endif
103 default:
104 goto out;
105 }
106
Herbert Xub59f45d2006-05-27 23:05:54 -0700107 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
108 goto out;
109
110 if (skb_cloned(skb) &&
111 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
112 goto out;
113
Arnaldo Carvalho de Meloe4396b52007-02-24 18:12:42 -0800114 iph = skb->nh.iph;
Miika Komuc82f9632007-02-06 14:27:02 -0800115 if (iph->protocol == IPPROTO_IPIP) {
116 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
117 ipv4_copy_dscp(iph, skb->h.ipiph);
118 if (!(x->props.flags & XFRM_STATE_NOECN))
119 ipip_ecn_decapsulate(skb);
120 }
121#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
122 else {
123 if (!(x->props.flags & XFRM_STATE_NOECN))
124 ipip6_ecn_decapsulate(iph, skb);
125 skb->protocol = htons(ETH_P_IPV6);
126 }
127#endif
Herbert Xub59f45d2006-05-27 23:05:54 -0700128 skb->mac.raw = memmove(skb->data - skb->mac_len,
129 skb->mac.raw, skb->mac_len);
130 skb->nh.raw = skb->data;
Herbert Xub59f45d2006-05-27 23:05:54 -0700131 err = 0;
132
133out:
134 return err;
135}
136
137static struct xfrm_mode xfrm4_tunnel_mode = {
138 .input = xfrm4_tunnel_input,
139 .output = xfrm4_tunnel_output,
140 .owner = THIS_MODULE,
141 .encap = XFRM_MODE_TUNNEL,
142};
143
144static int __init xfrm4_tunnel_init(void)
145{
146 return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
147}
148
149static void __exit xfrm4_tunnel_exit(void)
150{
151 int err;
152
153 err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
154 BUG_ON(err);
155}
156
157module_init(xfrm4_tunnel_init);
158module_exit(xfrm4_tunnel_exit);
159MODULE_LICENSE("GPL");
160MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);