blob: c4b2ccbeba04bb99357687f107ab5b9a27c10df8 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Herbert Xud2acc342006-03-28 01:12:13 -08002/* tunnel4.c: Generic IP tunnel transformer.
3 *
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
5 */
6
7#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/mutex.h>
Simon Horman8afe97e2016-07-07 07:56:12 +020010#include <linux/mpls.h>
Herbert Xud2acc342006-03-28 01:12:13 -080011#include <linux/netdevice.h>
12#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Herbert Xu50fba2a2006-04-04 13:50:45 -070014#include <net/icmp.h>
15#include <net/ip.h>
Herbert Xud2acc342006-03-28 01:12:13 -080016#include <net/protocol.h>
17#include <net/xfrm.h>
18
Eric Dumazetb33eab02010-10-25 21:01:26 +000019static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
20static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
Simon Horman8afe97e2016-07-07 07:56:12 +020021static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly;
Herbert Xud2acc342006-03-28 01:12:13 -080022static DEFINE_MUTEX(tunnel4_mutex);
23
Eric Dumazetb33eab02010-10-25 21:01:26 +000024static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
Pavel Emelyanov358352b2007-11-10 21:48:54 -080025{
Simon Horman8afe97e2016-07-07 07:56:12 +020026 return (family == AF_INET) ? &tunnel4_handlers :
27 (family == AF_INET6) ? &tunnel64_handlers :
28 &tunnelmpls4_handlers;
Pavel Emelyanov358352b2007-11-10 21:48:54 -080029}
30
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080031int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080032{
Eric Dumazetb33eab02010-10-25 21:01:26 +000033 struct xfrm_tunnel __rcu **pprev;
34 struct xfrm_tunnel *t;
35
Herbert Xud2acc342006-03-28 01:12:13 -080036 int ret = -EEXIST;
37 int priority = handler->priority;
38
39 mutex_lock(&tunnel4_mutex);
40
Eric Dumazetb33eab02010-10-25 21:01:26 +000041 for (pprev = fam_handlers(family);
42 (t = rcu_dereference_protected(*pprev,
43 lockdep_is_held(&tunnel4_mutex))) != NULL;
44 pprev = &t->next) {
45 if (t->priority > priority)
Herbert Xud2acc342006-03-28 01:12:13 -080046 break;
Eric Dumazetb33eab02010-10-25 21:01:26 +000047 if (t->priority == priority)
Herbert Xud2acc342006-03-28 01:12:13 -080048 goto err;
49 }
50
51 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000052 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080053
54 ret = 0;
55
56err:
57 mutex_unlock(&tunnel4_mutex);
58
59 return ret;
60}
Herbert Xud2acc342006-03-28 01:12:13 -080061EXPORT_SYMBOL(xfrm4_tunnel_register);
62
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080063int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080064{
Eric Dumazetb33eab02010-10-25 21:01:26 +000065 struct xfrm_tunnel __rcu **pprev;
66 struct xfrm_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080067 int ret = -ENOENT;
68
69 mutex_lock(&tunnel4_mutex);
70
Eric Dumazetb33eab02010-10-25 21:01:26 +000071 for (pprev = fam_handlers(family);
72 (t = rcu_dereference_protected(*pprev,
73 lockdep_is_held(&tunnel4_mutex))) != NULL;
74 pprev = &t->next) {
75 if (t == handler) {
Herbert Xud2acc342006-03-28 01:12:13 -080076 *pprev = handler->next;
77 ret = 0;
78 break;
79 }
80 }
81
82 mutex_unlock(&tunnel4_mutex);
83
84 synchronize_net();
85
86 return ret;
87}
Herbert Xud2acc342006-03-28 01:12:13 -080088EXPORT_SYMBOL(xfrm4_tunnel_deregister);
89
Eric Dumazet875168a2010-08-30 11:07:25 +000090#define for_each_tunnel_rcu(head, handler) \
91 for (handler = rcu_dereference(head); \
92 handler != NULL; \
93 handler = rcu_dereference(handler->next)) \
Stephen Hemminger82695b32018-02-27 15:48:21 -080094
Herbert Xud2acc342006-03-28 01:12:13 -080095static int tunnel4_rcv(struct sk_buff *skb)
96{
97 struct xfrm_tunnel *handler;
98
Herbert Xu50fba2a2006-04-04 13:50:45 -070099 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
100 goto drop;
101
Eric Dumazet875168a2010-08-30 11:07:25 +0000102 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800103 if (!handler->handler(skb))
104 return 0;
105
Herbert Xu50fba2a2006-04-04 13:50:45 -0700106 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
107
108drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800109 kfree_skb(skb);
110 return 0;
111}
112
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000113#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800114static int tunnel64_rcv(struct sk_buff *skb)
115{
116 struct xfrm_tunnel *handler;
117
YOSHIFUJI Hideakibaa2bfb2008-05-30 11:35:03 +0900118 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800119 goto drop;
120
Eric Dumazet875168a2010-08-30 11:07:25 +0000121 for_each_tunnel_rcu(tunnel64_handlers, handler)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800122 if (!handler->handler(skb))
123 return 0;
124
125 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
126
127drop:
128 kfree_skb(skb);
129 return 0;
130}
131#endif
132
Simon Horman8afe97e2016-07-07 07:56:12 +0200133#if IS_ENABLED(CONFIG_MPLS)
134static int tunnelmpls4_rcv(struct sk_buff *skb)
135{
136 struct xfrm_tunnel *handler;
137
138 if (!pskb_may_pull(skb, sizeof(struct mpls_label)))
139 goto drop;
140
141 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
142 if (!handler->handler(skb))
143 return 0;
144
145 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
146
147drop:
148 kfree_skb(skb);
149 return 0;
150}
151#endif
152
Stefano Brivio32bbd872018-11-08 12:19:21 +0100153static int tunnel4_err(struct sk_buff *skb, u32 info)
Herbert Xud2acc342006-03-28 01:12:13 -0800154{
155 struct xfrm_tunnel *handler;
156
Eric Dumazet875168a2010-08-30 11:07:25 +0000157 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800158 if (!handler->err_handler(skb, info))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100159 return 0;
160
161 return -ENOENT;
Herbert Xud2acc342006-03-28 01:12:13 -0800162}
163
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000164#if IS_ENABLED(CONFIG_IPV6)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100165static int tunnel64_err(struct sk_buff *skb, u32 info)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800166{
167 struct xfrm_tunnel *handler;
168
Eric Dumazet875168a2010-08-30 11:07:25 +0000169 for_each_tunnel_rcu(tunnel64_handlers, handler)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800170 if (!handler->err_handler(skb, info))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100171 return 0;
172
173 return -ENOENT;
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800174}
175#endif
176
Simon Horman8afe97e2016-07-07 07:56:12 +0200177#if IS_ENABLED(CONFIG_MPLS)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100178static int tunnelmpls4_err(struct sk_buff *skb, u32 info)
Simon Horman8afe97e2016-07-07 07:56:12 +0200179{
180 struct xfrm_tunnel *handler;
181
182 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
183 if (!handler->err_handler(skb, info))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100184 return 0;
185
186 return -ENOENT;
Simon Horman8afe97e2016-07-07 07:56:12 +0200187}
188#endif
189
Alexey Dobriyan32613092009-09-14 12:21:47 +0000190static const struct net_protocol tunnel4_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800191 .handler = tunnel4_rcv,
192 .err_handler = tunnel4_err,
193 .no_policy = 1,
Pavel Emelyanov4597a0c2008-04-16 01:06:56 -0700194 .netns_ok = 1,
Herbert Xud2acc342006-03-28 01:12:13 -0800195};
196
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000197#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyan32613092009-09-14 12:21:47 +0000198static const struct net_protocol tunnel64_protocol = {
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800199 .handler = tunnel64_rcv,
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800200 .err_handler = tunnel64_err,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800201 .no_policy = 1,
Pavel Emelyanovb0970c42008-04-16 01:17:39 -0700202 .netns_ok = 1,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800203};
204#endif
205
Simon Horman8afe97e2016-07-07 07:56:12 +0200206#if IS_ENABLED(CONFIG_MPLS)
207static const struct net_protocol tunnelmpls4_protocol = {
208 .handler = tunnelmpls4_rcv,
209 .err_handler = tunnelmpls4_err,
210 .no_policy = 1,
211 .netns_ok = 1,
212};
213#endif
214
Herbert Xud2acc342006-03-28 01:12:13 -0800215static int __init tunnel4_init(void)
216{
Simon Horman8afe97e2016-07-07 07:56:12 +0200217 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
Simon Hormanaa9667e2016-07-10 10:20:11 +0900218 goto err;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000219#if IS_ENABLED(CONFIG_IPV6)
Simon Hormanaa9667e2016-07-10 10:20:11 +0900220 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
221 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
222 goto err;
223 }
Simon Horman8afe97e2016-07-07 07:56:12 +0200224#endif
225#if IS_ENABLED(CONFIG_MPLS)
Simon Hormanaa9667e2016-07-10 10:20:11 +0900226 if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) {
227 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
228#if IS_ENABLED(CONFIG_IPV6)
229 inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
230#endif
231 goto err;
232 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800233#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800234 return 0;
Simon Horman8afe97e2016-07-07 07:56:12 +0200235
Simon Hormanaa9667e2016-07-10 10:20:11 +0900236err:
Simon Horman8afe97e2016-07-07 07:56:12 +0200237 pr_err("%s: can't add protocol\n", __func__);
238 return -EAGAIN;
Herbert Xud2acc342006-03-28 01:12:13 -0800239}
240
241static void __exit tunnel4_fini(void)
242{
Simon Horman8afe97e2016-07-07 07:56:12 +0200243#if IS_ENABLED(CONFIG_MPLS)
244 if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS))
245 pr_err("tunnelmpls4 close: can't remove protocol\n");
246#endif
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000247#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800248 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
Joe Perches058bd4d2012-03-11 18:36:11 +0000249 pr_err("tunnel64 close: can't remove protocol\n");
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800250#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800251 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
Joe Perches058bd4d2012-03-11 18:36:11 +0000252 pr_err("tunnel4 close: can't remove protocol\n");
Herbert Xud2acc342006-03-28 01:12:13 -0800253}
254
255module_init(tunnel4_init);
256module_exit(tunnel4_fini);
257MODULE_LICENSE("GPL");