blob: a2f2e6a5ec5d1662e6ba0b372af47059288cebaf [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 *
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
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070049 iph = ip_hdr(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070050 skb->transport_header = skb->network_header;
Herbert Xub59f45d2006-05-27 23:05:54 -070051
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -070052 skb_push(skb, x->props.header_len);
53 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070054 top_iph = ip_hdr(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070055
56 top_iph->ihl = 5;
57 top_iph->version = 4;
58
Herbert Xub59f45d2006-05-27 23:05:54 -070059 flags = x->props.flags;
Miika Komuc82f9632007-02-06 14:27:02 -080060
61 /* DS disclosed */
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));
67 }
68#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
69 else {
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;
74 }
75#endif
76
Herbert Xub59f45d2006-05-27 23:05:54 -070077 if (flags & XFRM_STATE_NOECN)
78 IP_ECN_clear(top_iph);
79
Herbert Xub59f45d2006-05-27 23:05:54 -070080 if (!top_iph->frag_off)
81 __ip_select_ident(top_iph, dst->child, 0);
82
83 top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
84
85 top_iph->saddr = x->props.saddr.a4;
86 top_iph->daddr = x->id.daddr.a4;
Herbert Xub59f45d2006-05-27 23:05:54 -070087
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{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070094 struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -030095 const unsigned char *old_mac;
Herbert Xub59f45d2006-05-27 23:05:54 -070096 int err = -EINVAL;
97
Stephen Hemminger132adf52007-03-08 20:44:43 -080098 switch (iph->protocol){
Miika Komuc82f9632007-02-06 14:27:02 -080099 case IPPROTO_IPIP:
Patrick McHardye2e01bf2007-02-12 20:27:10 -0800100 break;
Miika Komuc82f9632007-02-06 14:27:02 -0800101#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
102 case IPPROTO_IPV6:
103 break;
104#endif
105 default:
106 goto out;
107 }
108
Herbert Xub59f45d2006-05-27 23:05:54 -0700109 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
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700116 iph = ip_hdr(skb);
Miika Komuc82f9632007-02-06 14:27:02 -0800117 if (iph->protocol == IPPROTO_IPIP) {
118 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -0700119 ipv4_copy_dscp(iph, ipip_hdr(skb));
Miika Komuc82f9632007-02-06 14:27:02 -0800120 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
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700130 old_mac = skb_mac_header(skb);
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -0300131 skb_set_mac_header(skb, -skb->mac_len);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700132 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700133 skb_reset_network_header(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -0700134 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);