blob: ec35eaa5c029d3eeaa5a34435658a3d829692234 [file] [log] [blame]
Herbert Xud2acc342006-03-28 01:12:13 -08001/* tunnel4.c: Generic IP tunnel transformer.
2 *
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
4 */
5
6#include <linux/init.h>
7#include <linux/module.h>
8#include <linux/mutex.h>
Simon Horman8afe97e2016-07-07 07:56:12 +02009#include <linux/mpls.h>
Herbert Xud2acc342006-03-28 01:12:13 -080010#include <linux/netdevice.h>
11#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Herbert Xu50fba2a2006-04-04 13:50:45 -070013#include <net/icmp.h>
14#include <net/ip.h>
Herbert Xud2acc342006-03-28 01:12:13 -080015#include <net/protocol.h>
16#include <net/xfrm.h>
17
Eric Dumazetb33eab02010-10-25 21:01:26 +000018static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
19static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
Simon Horman8afe97e2016-07-07 07:56:12 +020020static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly;
Herbert Xud2acc342006-03-28 01:12:13 -080021static DEFINE_MUTEX(tunnel4_mutex);
22
Eric Dumazetb33eab02010-10-25 21:01:26 +000023static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
Pavel Emelyanov358352b2007-11-10 21:48:54 -080024{
Simon Horman8afe97e2016-07-07 07:56:12 +020025 return (family == AF_INET) ? &tunnel4_handlers :
26 (family == AF_INET6) ? &tunnel64_handlers :
27 &tunnelmpls4_handlers;
Pavel Emelyanov358352b2007-11-10 21:48:54 -080028}
29
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080030int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080031{
Eric Dumazetb33eab02010-10-25 21:01:26 +000032 struct xfrm_tunnel __rcu **pprev;
33 struct xfrm_tunnel *t;
34
Herbert Xud2acc342006-03-28 01:12:13 -080035 int ret = -EEXIST;
36 int priority = handler->priority;
37
38 mutex_lock(&tunnel4_mutex);
39
Eric Dumazetb33eab02010-10-25 21:01:26 +000040 for (pprev = fam_handlers(family);
41 (t = rcu_dereference_protected(*pprev,
42 lockdep_is_held(&tunnel4_mutex))) != NULL;
43 pprev = &t->next) {
44 if (t->priority > priority)
Herbert Xud2acc342006-03-28 01:12:13 -080045 break;
Eric Dumazetb33eab02010-10-25 21:01:26 +000046 if (t->priority == priority)
Herbert Xud2acc342006-03-28 01:12:13 -080047 goto err;
48 }
49
50 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000051 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080052
53 ret = 0;
54
55err:
56 mutex_unlock(&tunnel4_mutex);
57
58 return ret;
59}
Herbert Xud2acc342006-03-28 01:12:13 -080060EXPORT_SYMBOL(xfrm4_tunnel_register);
61
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080062int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080063{
Eric Dumazetb33eab02010-10-25 21:01:26 +000064 struct xfrm_tunnel __rcu **pprev;
65 struct xfrm_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080066 int ret = -ENOENT;
67
68 mutex_lock(&tunnel4_mutex);
69
Eric Dumazetb33eab02010-10-25 21:01:26 +000070 for (pprev = fam_handlers(family);
71 (t = rcu_dereference_protected(*pprev,
72 lockdep_is_held(&tunnel4_mutex))) != NULL;
73 pprev = &t->next) {
74 if (t == handler) {
Herbert Xud2acc342006-03-28 01:12:13 -080075 *pprev = handler->next;
76 ret = 0;
77 break;
78 }
79 }
80
81 mutex_unlock(&tunnel4_mutex);
82
83 synchronize_net();
84
85 return ret;
86}
Herbert Xud2acc342006-03-28 01:12:13 -080087EXPORT_SYMBOL(xfrm4_tunnel_deregister);
88
Eric Dumazet875168a2010-08-30 11:07:25 +000089#define for_each_tunnel_rcu(head, handler) \
90 for (handler = rcu_dereference(head); \
91 handler != NULL; \
92 handler = rcu_dereference(handler->next)) \
93
Herbert Xud2acc342006-03-28 01:12:13 -080094static int tunnel4_rcv(struct sk_buff *skb)
95{
96 struct xfrm_tunnel *handler;
97
Herbert Xu50fba2a2006-04-04 13:50:45 -070098 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
99 goto drop;
100
Eric Dumazet875168a2010-08-30 11:07:25 +0000101 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800102 if (!handler->handler(skb))
103 return 0;
104
Herbert Xu50fba2a2006-04-04 13:50:45 -0700105 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
106
107drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800108 kfree_skb(skb);
109 return 0;
110}
111
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000112#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800113static int tunnel64_rcv(struct sk_buff *skb)
114{
115 struct xfrm_tunnel *handler;
116
YOSHIFUJI Hideakibaa2bfb2008-05-30 11:35:03 +0900117 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800118 goto drop;
119
Eric Dumazet875168a2010-08-30 11:07:25 +0000120 for_each_tunnel_rcu(tunnel64_handlers, handler)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800121 if (!handler->handler(skb))
122 return 0;
123
124 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
125
126drop:
127 kfree_skb(skb);
128 return 0;
129}
130#endif
131
Simon Horman8afe97e2016-07-07 07:56:12 +0200132#if IS_ENABLED(CONFIG_MPLS)
133static int tunnelmpls4_rcv(struct sk_buff *skb)
134{
135 struct xfrm_tunnel *handler;
136
137 if (!pskb_may_pull(skb, sizeof(struct mpls_label)))
138 goto drop;
139
140 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
141 if (!handler->handler(skb))
142 return 0;
143
144 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
145
146drop:
147 kfree_skb(skb);
148 return 0;
149}
150#endif
151
Herbert Xud2acc342006-03-28 01:12:13 -0800152static void tunnel4_err(struct sk_buff *skb, u32 info)
153{
154 struct xfrm_tunnel *handler;
155
Eric Dumazet875168a2010-08-30 11:07:25 +0000156 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800157 if (!handler->err_handler(skb, info))
158 break;
159}
160
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000161#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800162static void tunnel64_err(struct sk_buff *skb, u32 info)
163{
164 struct xfrm_tunnel *handler;
165
Eric Dumazet875168a2010-08-30 11:07:25 +0000166 for_each_tunnel_rcu(tunnel64_handlers, handler)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800167 if (!handler->err_handler(skb, info))
168 break;
169}
170#endif
171
Simon Horman8afe97e2016-07-07 07:56:12 +0200172#if IS_ENABLED(CONFIG_MPLS)
173static void tunnelmpls4_err(struct sk_buff *skb, u32 info)
174{
175 struct xfrm_tunnel *handler;
176
177 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
178 if (!handler->err_handler(skb, info))
179 break;
180}
181#endif
182
Alexey Dobriyan32613092009-09-14 12:21:47 +0000183static const struct net_protocol tunnel4_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800184 .handler = tunnel4_rcv,
185 .err_handler = tunnel4_err,
186 .no_policy = 1,
Pavel Emelyanov4597a0c2008-04-16 01:06:56 -0700187 .netns_ok = 1,
Herbert Xud2acc342006-03-28 01:12:13 -0800188};
189
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000190#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyan32613092009-09-14 12:21:47 +0000191static const struct net_protocol tunnel64_protocol = {
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800192 .handler = tunnel64_rcv,
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800193 .err_handler = tunnel64_err,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800194 .no_policy = 1,
Pavel Emelyanovb0970c42008-04-16 01:17:39 -0700195 .netns_ok = 1,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800196};
197#endif
198
Simon Horman8afe97e2016-07-07 07:56:12 +0200199#if IS_ENABLED(CONFIG_MPLS)
200static const struct net_protocol tunnelmpls4_protocol = {
201 .handler = tunnelmpls4_rcv,
202 .err_handler = tunnelmpls4_err,
203 .no_policy = 1,
204 .netns_ok = 1,
205};
206#endif
207
Herbert Xud2acc342006-03-28 01:12:13 -0800208static int __init tunnel4_init(void)
209{
Simon Horman8afe97e2016-07-07 07:56:12 +0200210 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
Simon Hormanaa9667e2016-07-10 10:20:11 +0900211 goto err;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000212#if IS_ENABLED(CONFIG_IPV6)
Simon Hormanaa9667e2016-07-10 10:20:11 +0900213 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
214 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
215 goto err;
216 }
Simon Horman8afe97e2016-07-07 07:56:12 +0200217#endif
218#if IS_ENABLED(CONFIG_MPLS)
Simon Hormanaa9667e2016-07-10 10:20:11 +0900219 if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) {
220 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
221#if IS_ENABLED(CONFIG_IPV6)
222 inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
223#endif
224 goto err;
225 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800226#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800227 return 0;
Simon Horman8afe97e2016-07-07 07:56:12 +0200228
Simon Hormanaa9667e2016-07-10 10:20:11 +0900229err:
Simon Horman8afe97e2016-07-07 07:56:12 +0200230 pr_err("%s: can't add protocol\n", __func__);
231 return -EAGAIN;
Herbert Xud2acc342006-03-28 01:12:13 -0800232}
233
234static void __exit tunnel4_fini(void)
235{
Simon Horman8afe97e2016-07-07 07:56:12 +0200236#if IS_ENABLED(CONFIG_MPLS)
237 if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS))
238 pr_err("tunnelmpls4 close: can't remove protocol\n");
239#endif
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000240#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800241 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
Joe Perches058bd4d2012-03-11 18:36:11 +0000242 pr_err("tunnel64 close: can't remove protocol\n");
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800243#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800244 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
Joe Perches058bd4d2012-03-11 18:36:11 +0000245 pr_err("tunnel4 close: can't remove protocol\n");
Herbert Xud2acc342006-03-28 01:12:13 -0800246}
247
248module_init(tunnel4_init);
249module_exit(tunnel4_fini);
250MODULE_LICENSE("GPL");