blob: ddee0a099a2c8caea19ccabbd957032f59896fdd [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/gfp.h>
Herbert Xub59f45d2006-05-27 23:05:54 -07008#include <linux/init.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/stringify.h>
13#include <net/dst.h>
14#include <net/inet_ecn.h>
15#include <net/ip.h>
16#include <net/xfrm.h>
17
Saurabheb8637c2012-07-17 09:44:49 +000018/* Informational hook. The decap is still done here. */
19static struct xfrm_tunnel __rcu *rcv_notify_handlers __read_mostly;
20static DEFINE_MUTEX(xfrm4_mode_tunnel_input_mutex);
21
22int xfrm4_mode_tunnel_input_register(struct xfrm_tunnel *handler)
23{
24 struct xfrm_tunnel __rcu **pprev;
25 struct xfrm_tunnel *t;
26 int ret = -EEXIST;
27 int priority = handler->priority;
28
29 mutex_lock(&xfrm4_mode_tunnel_input_mutex);
30
31 for (pprev = &rcv_notify_handlers;
32 (t = rcu_dereference_protected(*pprev,
33 lockdep_is_held(&xfrm4_mode_tunnel_input_mutex))) != NULL;
34 pprev = &t->next) {
35 if (t->priority > priority)
36 break;
37 if (t->priority == priority)
38 goto err;
39
40 }
41
42 handler->next = *pprev;
43 rcu_assign_pointer(*pprev, handler);
44
45 ret = 0;
46
47err:
48 mutex_unlock(&xfrm4_mode_tunnel_input_mutex);
49 return ret;
50}
51EXPORT_SYMBOL_GPL(xfrm4_mode_tunnel_input_register);
52
53int xfrm4_mode_tunnel_input_deregister(struct xfrm_tunnel *handler)
54{
55 struct xfrm_tunnel __rcu **pprev;
56 struct xfrm_tunnel *t;
57 int ret = -ENOENT;
58
59 mutex_lock(&xfrm4_mode_tunnel_input_mutex);
60 for (pprev = &rcv_notify_handlers;
61 (t = rcu_dereference_protected(*pprev,
62 lockdep_is_held(&xfrm4_mode_tunnel_input_mutex))) != NULL;
63 pprev = &t->next) {
64 if (t == handler) {
65 *pprev = handler->next;
66 ret = 0;
67 break;
68 }
69 }
70 mutex_unlock(&xfrm4_mode_tunnel_input_mutex);
71 synchronize_net();
72
73 return ret;
74}
75EXPORT_SYMBOL_GPL(xfrm4_mode_tunnel_input_deregister);
76
Herbert Xub59f45d2006-05-27 23:05:54 -070077static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
78{
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -070079 struct iphdr *inner_iph = ipip_hdr(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070080
Herbert Xu227620e2007-11-13 21:41:28 -080081 if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
Herbert Xub59f45d2006-05-27 23:05:54 -070082 IP_ECN_set_ce(inner_iph);
83}
84
85/* Add encapsulation header.
86 *
Herbert Xuceb1eec82007-10-10 15:45:52 -070087 * The top IP header will be constructed per RFC 2401.
Herbert Xub59f45d2006-05-27 23:05:54 -070088 */
Herbert Xu195ad6a2007-12-20 13:53:40 -080089static int xfrm4_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
Herbert Xub59f45d2006-05-27 23:05:54 -070090{
Eric Dumazetadf30902009-06-02 05:19:30 +000091 struct dst_entry *dst = skb_dst(skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080092 struct iphdr *top_iph;
Herbert Xub59f45d2006-05-27 23:05:54 -070093 int flags;
94
Herbert Xu7b277b12007-10-10 15:44:06 -070095 skb_set_network_header(skb, -x->props.header_len);
Herbert Xu37fedd32007-10-10 15:44:44 -070096 skb->mac_header = skb->network_header +
97 offsetof(struct iphdr, protocol);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080098 skb->transport_header = skb->network_header + sizeof(*top_iph);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070099 top_iph = ip_hdr(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -0700100
101 top_iph->ihl = 5;
102 top_iph->version = 4;
103
Eric Dumazetadf30902009-06-02 05:19:30 +0000104 top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
Miika Komuc82f9632007-02-06 14:27:02 -0800105
106 /* DS disclosed */
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800107 top_iph->tos = INET_ECN_encapsulate(XFRM_MODE_SKB_CB(skb)->tos,
108 XFRM_MODE_SKB_CB(skb)->tos);
Miika Komuc82f9632007-02-06 14:27:02 -0800109
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800110 flags = x->props.flags;
Herbert Xub59f45d2006-05-27 23:05:54 -0700111 if (flags & XFRM_STATE_NOECN)
112 IP_ECN_clear(top_iph);
113
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800114 top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
Steffen Klassertfe833fc2008-06-17 16:37:13 -0700115 0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800116 ip_select_ident(top_iph, dst->child, NULL);
Herbert Xub59f45d2006-05-27 23:05:54 -0700117
David S. Miller323e1262010-12-12 21:55:08 -0800118 top_iph->ttl = ip4_dst_hoplimit(dst->child);
Herbert Xub59f45d2006-05-27 23:05:54 -0700119
120 top_iph->saddr = x->props.saddr.a4;
121 top_iph->daddr = x->id.daddr.a4;
Herbert Xub59f45d2006-05-27 23:05:54 -0700122
Herbert Xub59f45d2006-05-27 23:05:54 -0700123 return 0;
124}
125
Saurabheb8637c2012-07-17 09:44:49 +0000126#define for_each_input_rcu(head, handler) \
127 for (handler = rcu_dereference(head); \
128 handler != NULL; \
129 handler = rcu_dereference(handler->next))
130
Herbert Xu195ad6a2007-12-20 13:53:40 -0800131static int xfrm4_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
Herbert Xub59f45d2006-05-27 23:05:54 -0700132{
Saurabheb8637c2012-07-17 09:44:49 +0000133 struct xfrm_tunnel *handler;
Herbert Xub59f45d2006-05-27 23:05:54 -0700134 int err = -EINVAL;
135
Herbert Xu227620e2007-11-13 21:41:28 -0800136 if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP)
137 goto out;
Miika Komuc82f9632007-02-06 14:27:02 -0800138
Herbert Xub59f45d2006-05-27 23:05:54 -0700139 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
140 goto out;
141
Saurabheb8637c2012-07-17 09:44:49 +0000142 for_each_input_rcu(rcv_notify_handlers, handler)
143 handler->handler(skb);
144
Herbert Xub59f45d2006-05-27 23:05:54 -0700145 if (skb_cloned(skb) &&
146 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
147 goto out;
148
Herbert Xu227620e2007-11-13 21:41:28 -0800149 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
150 ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb));
151 if (!(x->props.flags & XFRM_STATE_NOECN))
152 ipip_ecn_decapsulate(skb);
153
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700154 skb_reset_network_header(skb);
Eric Dumazet03606892012-02-23 10:55:02 +0000155 skb_mac_header_rebuild(skb);
156
Herbert Xub59f45d2006-05-27 23:05:54 -0700157 err = 0;
158
159out:
160 return err;
161}
162
163static struct xfrm_mode xfrm4_tunnel_mode = {
Herbert Xu195ad6a2007-12-20 13:53:40 -0800164 .input2 = xfrm4_mode_tunnel_input,
Herbert Xu227620e2007-11-13 21:41:28 -0800165 .input = xfrm_prepare_input,
Herbert Xu195ad6a2007-12-20 13:53:40 -0800166 .output2 = xfrm4_mode_tunnel_output,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800167 .output = xfrm4_prepare_output,
Herbert Xub59f45d2006-05-27 23:05:54 -0700168 .owner = THIS_MODULE,
169 .encap = XFRM_MODE_TUNNEL,
Herbert Xu1bfcb102007-10-17 21:31:50 -0700170 .flags = XFRM_MODE_FLAG_TUNNEL,
Herbert Xub59f45d2006-05-27 23:05:54 -0700171};
172
Herbert Xu195ad6a2007-12-20 13:53:40 -0800173static int __init xfrm4_mode_tunnel_init(void)
Herbert Xub59f45d2006-05-27 23:05:54 -0700174{
175 return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
176}
177
Herbert Xu195ad6a2007-12-20 13:53:40 -0800178static void __exit xfrm4_mode_tunnel_exit(void)
Herbert Xub59f45d2006-05-27 23:05:54 -0700179{
180 int err;
181
182 err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
183 BUG_ON(err);
184}
185
Herbert Xu195ad6a2007-12-20 13:53:40 -0800186module_init(xfrm4_mode_tunnel_init);
187module_exit(xfrm4_mode_tunnel_exit);
Herbert Xub59f45d2006-05-27 23:05:54 -0700188MODULE_LICENSE("GPL");
189MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);