blob: 33bf8e9c86630e5bb582bcf9dac803e4a26cf27b [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)) \
Stephen Hemminger82695b32018-02-27 15:48:21 -080093
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
Stefano Brivio32bbd872018-11-08 12:19:21 +0100152static int tunnel4_err(struct sk_buff *skb, u32 info)
Herbert Xud2acc342006-03-28 01:12:13 -0800153{
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))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100158 return 0;
159
160 return -ENOENT;
Herbert Xud2acc342006-03-28 01:12:13 -0800161}
162
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000163#if IS_ENABLED(CONFIG_IPV6)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100164static int tunnel64_err(struct sk_buff *skb, u32 info)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800165{
166 struct xfrm_tunnel *handler;
167
Eric Dumazet875168a2010-08-30 11:07:25 +0000168 for_each_tunnel_rcu(tunnel64_handlers, handler)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800169 if (!handler->err_handler(skb, info))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100170 return 0;
171
172 return -ENOENT;
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800173}
174#endif
175
Simon Horman8afe97e2016-07-07 07:56:12 +0200176#if IS_ENABLED(CONFIG_MPLS)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100177static int tunnelmpls4_err(struct sk_buff *skb, u32 info)
Simon Horman8afe97e2016-07-07 07:56:12 +0200178{
179 struct xfrm_tunnel *handler;
180
181 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
182 if (!handler->err_handler(skb, info))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100183 return 0;
184
185 return -ENOENT;
Simon Horman8afe97e2016-07-07 07:56:12 +0200186}
187#endif
188
Alexey Dobriyan32613092009-09-14 12:21:47 +0000189static const struct net_protocol tunnel4_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800190 .handler = tunnel4_rcv,
191 .err_handler = tunnel4_err,
192 .no_policy = 1,
Pavel Emelyanov4597a0c2008-04-16 01:06:56 -0700193 .netns_ok = 1,
Herbert Xud2acc342006-03-28 01:12:13 -0800194};
195
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000196#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyan32613092009-09-14 12:21:47 +0000197static const struct net_protocol tunnel64_protocol = {
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800198 .handler = tunnel64_rcv,
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800199 .err_handler = tunnel64_err,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800200 .no_policy = 1,
Pavel Emelyanovb0970c42008-04-16 01:17:39 -0700201 .netns_ok = 1,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800202};
203#endif
204
Simon Horman8afe97e2016-07-07 07:56:12 +0200205#if IS_ENABLED(CONFIG_MPLS)
206static const struct net_protocol tunnelmpls4_protocol = {
207 .handler = tunnelmpls4_rcv,
208 .err_handler = tunnelmpls4_err,
209 .no_policy = 1,
210 .netns_ok = 1,
211};
212#endif
213
Herbert Xud2acc342006-03-28 01:12:13 -0800214static int __init tunnel4_init(void)
215{
Simon Horman8afe97e2016-07-07 07:56:12 +0200216 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
Simon Hormanaa9667e2016-07-10 10:20:11 +0900217 goto err;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000218#if IS_ENABLED(CONFIG_IPV6)
Simon Hormanaa9667e2016-07-10 10:20:11 +0900219 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
220 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
221 goto err;
222 }
Simon Horman8afe97e2016-07-07 07:56:12 +0200223#endif
224#if IS_ENABLED(CONFIG_MPLS)
Simon Hormanaa9667e2016-07-10 10:20:11 +0900225 if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) {
226 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
227#if IS_ENABLED(CONFIG_IPV6)
228 inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
229#endif
230 goto err;
231 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800232#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800233 return 0;
Simon Horman8afe97e2016-07-07 07:56:12 +0200234
Simon Hormanaa9667e2016-07-10 10:20:11 +0900235err:
Simon Horman8afe97e2016-07-07 07:56:12 +0200236 pr_err("%s: can't add protocol\n", __func__);
237 return -EAGAIN;
Herbert Xud2acc342006-03-28 01:12:13 -0800238}
239
240static void __exit tunnel4_fini(void)
241{
Simon Horman8afe97e2016-07-07 07:56:12 +0200242#if IS_ENABLED(CONFIG_MPLS)
243 if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS))
244 pr_err("tunnelmpls4 close: can't remove protocol\n");
245#endif
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000246#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800247 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
Joe Perches058bd4d2012-03-11 18:36:11 +0000248 pr_err("tunnel64 close: can't remove protocol\n");
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800249#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800250 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
Joe Perches058bd4d2012-03-11 18:36:11 +0000251 pr_err("tunnel4 close: can't remove protocol\n");
Herbert Xud2acc342006-03-28 01:12:13 -0800252}
253
254module_init(tunnel4_init);
255module_exit(tunnel4_fini);
256MODULE_LICENSE("GPL");