blob: e4deecba6dd216d546706f57e3e557123af52c71 [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{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070019 struct iphdr *outer_iph = ip_hdr(skb);
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -070020 struct iphdr *inner_iph = ipip_hdr(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070021
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))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070029 IP6_ECN_set_ce(ipv6_hdr(skb));
Miika Komuc82f9632007-02-06 14:27:02 -080030}
31
Herbert Xub59f45d2006-05-27 23:05:54 -070032/* Add encapsulation header.
33 *
Herbert Xuceb1eec82007-10-10 15:45:52 -070034 * The top IP header will be constructed per RFC 2401.
Herbert Xub59f45d2006-05-27 23:05:54 -070035 */
Jamal Hadi Salimeb878e82006-08-31 17:42:59 -070036static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
Herbert Xub59f45d2006-05-27 23:05:54 -070037{
38 struct dst_entry *dst = skb->dst;
Miika Komuc82f9632007-02-06 14:27:02 -080039 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
Herbert Xub59f45d2006-05-27 23:05:54 -070040 struct iphdr *iph, *top_iph;
41 int flags;
42
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070043 iph = ip_hdr(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070044
Herbert Xu7b277b12007-10-10 15:44:06 -070045 skb_set_network_header(skb, -x->props.header_len);
Herbert Xu37fedd32007-10-10 15:44:44 -070046 skb->mac_header = skb->network_header +
47 offsetof(struct iphdr, protocol);
48 skb->transport_header = skb->network_header + sizeof(*iph);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070049 top_iph = ip_hdr(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070050
51 top_iph->ihl = 5;
52 top_iph->version = 4;
53
Herbert Xub59f45d2006-05-27 23:05:54 -070054 flags = x->props.flags;
Miika Komuc82f9632007-02-06 14:27:02 -080055
56 /* DS disclosed */
57 if (xdst->route->ops->family == AF_INET) {
58 top_iph->protocol = IPPROTO_IPIP;
59 top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
60 top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
61 0 : (iph->frag_off & htons(IP_DF));
62 }
63#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
64 else {
65 struct ipv6hdr *ipv6h = (struct ipv6hdr*)iph;
66 top_iph->protocol = IPPROTO_IPV6;
67 top_iph->tos = INET_ECN_encapsulate(iph->tos, ipv6_get_dsfield(ipv6h));
68 top_iph->frag_off = 0;
69 }
70#endif
71
Herbert Xub59f45d2006-05-27 23:05:54 -070072 if (flags & XFRM_STATE_NOECN)
73 IP_ECN_clear(top_iph);
74
Herbert Xub59f45d2006-05-27 23:05:54 -070075 if (!top_iph->frag_off)
76 __ip_select_ident(top_iph, dst->child, 0);
77
78 top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
79
80 top_iph->saddr = x->props.saddr.a4;
81 top_iph->daddr = x->id.daddr.a4;
Herbert Xub59f45d2006-05-27 23:05:54 -070082
Kazunori MIYAZAWAf282d452007-05-29 13:03:17 -070083 skb->protocol = htons(ETH_P_IP);
84
Herbert Xub59f45d2006-05-27 23:05:54 -070085 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
86 return 0;
87}
88
89static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
90{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070091 struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -030092 const unsigned char *old_mac;
Herbert Xub59f45d2006-05-27 23:05:54 -070093 int err = -EINVAL;
94
Stephen Hemminger132adf52007-03-08 20:44:43 -080095 switch (iph->protocol){
Miika Komuc82f9632007-02-06 14:27:02 -080096 case IPPROTO_IPIP:
Patrick McHardye2e01bf2007-02-12 20:27:10 -080097 break;
Miika Komuc82f9632007-02-06 14:27:02 -080098#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
99 case IPPROTO_IPV6:
100 break;
101#endif
102 default:
103 goto out;
104 }
105
Herbert Xub59f45d2006-05-27 23:05:54 -0700106 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
107 goto out;
108
109 if (skb_cloned(skb) &&
110 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
111 goto out;
112
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700113 iph = ip_hdr(skb);
Miika Komuc82f9632007-02-06 14:27:02 -0800114 if (iph->protocol == IPPROTO_IPIP) {
115 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -0700116 ipv4_copy_dscp(iph, ipip_hdr(skb));
Miika Komuc82f9632007-02-06 14:27:02 -0800117 if (!(x->props.flags & XFRM_STATE_NOECN))
118 ipip_ecn_decapsulate(skb);
119 }
120#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
121 else {
122 if (!(x->props.flags & XFRM_STATE_NOECN))
123 ipip6_ecn_decapsulate(iph, skb);
124 skb->protocol = htons(ETH_P_IPV6);
125 }
126#endif
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700127 old_mac = skb_mac_header(skb);
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -0300128 skb_set_mac_header(skb, -skb->mac_len);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700129 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700130 skb_reset_network_header(skb);
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,
Herbert Xu1bfcb102007-10-17 21:31:50 -0700142 .flags = XFRM_MODE_FLAG_TUNNEL,
Herbert Xub59f45d2006-05-27 23:05:54 -0700143};
144
145static int __init xfrm4_tunnel_init(void)
146{
147 return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
148}
149
150static void __exit xfrm4_tunnel_exit(void)
151{
152 int err;
153
154 err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
155 BUG_ON(err);
156}
157
158module_init(xfrm4_tunnel_init);
159module_exit(xfrm4_tunnel_exit);
160MODULE_LICENSE("GPL");
161MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);