blob: 00e8d8b1c9a75fa1a820ac85eba91e3e24750c01 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xud2acc342006-03-28 01:12:13 -08002/*
3 * Copyright (C)2003,2004 USAGI/WIDE Project
4 *
Herbert Xud2acc342006-03-28 01:12:13 -08005 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
Ian Morris67ba4152014-08-24 21:53:10 +01006 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Herbert Xud2acc342006-03-28 01:12:13 -08007 */
8
Joe Perchesf3213832012-05-15 14:11:53 +00009#define pr_fmt(fmt) "IPv6: " fmt
10
Herbert Xu50fba2a2006-04-04 13:50:45 -070011#include <linux/icmpv6.h>
Herbert Xud2acc342006-03-28 01:12:13 -080012#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/netdevice.h>
16#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Herbert Xu50fba2a2006-04-04 13:50:45 -070018#include <net/ipv6.h>
Herbert Xud2acc342006-03-28 01:12:13 -080019#include <net/protocol.h>
20#include <net/xfrm.h>
21
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000022static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
23static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
Vadim Fedorenkof234efa2020-05-20 18:21:37 +030024static struct xfrm6_tunnel __rcu *tunnelmpls6_handlers __read_mostly;
Herbert Xud2acc342006-03-28 01:12:13 -080025static DEFINE_MUTEX(tunnel6_mutex);
26
Vadim Fedorenkof234efa2020-05-20 18:21:37 +030027static inline int xfrm6_tunnel_mpls_supported(void)
28{
29 return IS_ENABLED(CONFIG_MPLS);
30}
31
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080032int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080033{
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000034 struct xfrm6_tunnel __rcu **pprev;
35 struct xfrm6_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080036 int ret = -EEXIST;
37 int priority = handler->priority;
38
39 mutex_lock(&tunnel6_mutex);
40
Vadim Fedorenkof234efa2020-05-20 18:21:37 +030041 switch (family) {
42 case AF_INET6:
43 pprev = &tunnel6_handlers;
44 break;
45 case AF_INET:
46 pprev = &tunnel46_handlers;
47 break;
48 case AF_MPLS:
49 pprev = &tunnelmpls6_handlers;
50 break;
51 default:
52 goto err;
53 }
54
55 for (; (t = rcu_dereference_protected(*pprev,
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000056 lockdep_is_held(&tunnel6_mutex))) != NULL;
57 pprev = &t->next) {
58 if (t->priority > priority)
Herbert Xud2acc342006-03-28 01:12:13 -080059 break;
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000060 if (t->priority == priority)
Herbert Xud2acc342006-03-28 01:12:13 -080061 goto err;
62 }
63
64 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000065 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080066
67 ret = 0;
68
69err:
70 mutex_unlock(&tunnel6_mutex);
71
72 return ret;
73}
Herbert Xud2acc342006-03-28 01:12:13 -080074EXPORT_SYMBOL(xfrm6_tunnel_register);
75
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080076int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080077{
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000078 struct xfrm6_tunnel __rcu **pprev;
79 struct xfrm6_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080080 int ret = -ENOENT;
81
82 mutex_lock(&tunnel6_mutex);
83
Vadim Fedorenkof234efa2020-05-20 18:21:37 +030084 switch (family) {
85 case AF_INET6:
86 pprev = &tunnel6_handlers;
87 break;
88 case AF_INET:
89 pprev = &tunnel46_handlers;
90 break;
91 case AF_MPLS:
92 pprev = &tunnelmpls6_handlers;
93 break;
94 default:
95 goto err;
96 }
97
98 for (; (t = rcu_dereference_protected(*pprev,
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000099 lockdep_is_held(&tunnel6_mutex))) != NULL;
100 pprev = &t->next) {
101 if (t == handler) {
Herbert Xud2acc342006-03-28 01:12:13 -0800102 *pprev = handler->next;
103 ret = 0;
104 break;
105 }
106 }
107
Vadim Fedorenkof234efa2020-05-20 18:21:37 +0300108err:
Herbert Xud2acc342006-03-28 01:12:13 -0800109 mutex_unlock(&tunnel6_mutex);
110
111 synchronize_net();
112
113 return ret;
114}
Herbert Xud2acc342006-03-28 01:12:13 -0800115EXPORT_SYMBOL(xfrm6_tunnel_deregister);
116
Eric Dumazet875168a2010-08-30 11:07:25 +0000117#define for_each_tunnel_rcu(head, handler) \
118 for (handler = rcu_dereference(head); \
119 handler != NULL; \
120 handler = rcu_dereference(handler->next)) \
121
Vadim Fedorenkof234efa2020-05-20 18:21:37 +0300122static int tunnelmpls6_rcv(struct sk_buff *skb)
123{
124 struct xfrm6_tunnel *handler;
125
126 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
127 goto drop;
128
129 for_each_tunnel_rcu(tunnelmpls6_handlers, handler)
130 if (!handler->handler(skb))
131 return 0;
132
133 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
134
135drop:
136 kfree_skb(skb);
137 return 0;
138}
139
Herbert Xue5bbef22007-10-15 12:50:28 -0700140static int tunnel6_rcv(struct sk_buff *skb)
Herbert Xud2acc342006-03-28 01:12:13 -0800141{
Herbert Xud2acc342006-03-28 01:12:13 -0800142 struct xfrm6_tunnel *handler;
143
Herbert Xu50fba2a2006-04-04 13:50:45 -0700144 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
145 goto drop;
146
Eric Dumazet875168a2010-08-30 11:07:25 +0000147 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800148 if (!handler->handler(skb))
149 return 0;
150
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000151 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700152
153drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800154 kfree_skb(skb);
155 return 0;
156}
157
Xin Long86afc702020-07-06 20:01:31 +0800158#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
159static int tunnel6_rcv_cb(struct sk_buff *skb, u8 proto, int err)
160{
161 struct xfrm6_tunnel __rcu *head;
162 struct xfrm6_tunnel *handler;
163 int ret;
164
165 head = (proto == IPPROTO_IPV6) ? tunnel6_handlers : tunnel46_handlers;
166
167 for_each_tunnel_rcu(head, handler) {
168 if (handler->cb_handler) {
169 ret = handler->cb_handler(skb, err);
170 if (ret <= 0)
171 return ret;
172 }
173 }
174
175 return 0;
176}
177
178static const struct xfrm_input_afinfo tunnel6_input_afinfo = {
179 .family = AF_INET6,
180 .is_ipip = true,
181 .callback = tunnel6_rcv_cb,
182};
183#endif
184
Herbert Xue5bbef22007-10-15 12:50:28 -0700185static int tunnel46_rcv(struct sk_buff *skb)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800186{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800187 struct xfrm6_tunnel *handler;
188
Colin82836372008-05-27 00:04:43 +0800189 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800190 goto drop;
191
Eric Dumazet875168a2010-08-30 11:07:25 +0000192 for_each_tunnel_rcu(tunnel46_handlers, handler)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800193 if (!handler->handler(skb))
194 return 0;
195
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000196 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800197
198drop:
199 kfree_skb(skb);
200 return 0;
201}
202
Stefano Brivio32bbd872018-11-08 12:19:21 +0100203static int tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700204 u8 type, u8 code, int offset, __be32 info)
Herbert Xud2acc342006-03-28 01:12:13 -0800205{
206 struct xfrm6_tunnel *handler;
207
Eric Dumazet875168a2010-08-30 11:07:25 +0000208 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800209 if (!handler->err_handler(skb, opt, type, code, offset, info))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100210 return 0;
211
212 return -ENOENT;
Herbert Xud2acc342006-03-28 01:12:13 -0800213}
214
Stefano Brivio32bbd872018-11-08 12:19:21 +0100215static int tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Michal Kubečekebac62f2015-11-03 08:51:07 +0100216 u8 type, u8 code, int offset, __be32 info)
217{
218 struct xfrm6_tunnel *handler;
219
220 for_each_tunnel_rcu(tunnel46_handlers, handler)
221 if (!handler->err_handler(skb, opt, type, code, offset, info))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100222 return 0;
223
224 return -ENOENT;
Michal Kubečekebac62f2015-11-03 08:51:07 +0100225}
226
Vadim Fedorenkof234efa2020-05-20 18:21:37 +0300227static int tunnelmpls6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
228 u8 type, u8 code, int offset, __be32 info)
229{
230 struct xfrm6_tunnel *handler;
231
232 for_each_tunnel_rcu(tunnelmpls6_handlers, handler)
233 if (!handler->err_handler(skb, opt, type, code, offset, info))
234 return 0;
235
236 return -ENOENT;
237}
238
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000239static const struct inet6_protocol tunnel6_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800240 .handler = tunnel6_rcv,
241 .err_handler = tunnel6_err,
242 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
243};
244
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000245static const struct inet6_protocol tunnel46_protocol = {
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800246 .handler = tunnel46_rcv,
Michal Kubečekebac62f2015-11-03 08:51:07 +0100247 .err_handler = tunnel46_err,
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800248 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
249};
250
Vadim Fedorenkof234efa2020-05-20 18:21:37 +0300251static const struct inet6_protocol tunnelmpls6_protocol = {
252 .handler = tunnelmpls6_rcv,
253 .err_handler = tunnelmpls6_err,
254 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
255};
256
Herbert Xud2acc342006-03-28 01:12:13 -0800257static int __init tunnel6_init(void)
258{
259 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000260 pr_err("%s: can't add protocol\n", __func__);
Herbert Xud2acc342006-03-28 01:12:13 -0800261 return -EAGAIN;
262 }
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800263 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000264 pr_err("%s: can't add protocol\n", __func__);
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800265 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
266 return -EAGAIN;
267 }
Vadim Fedorenkof234efa2020-05-20 18:21:37 +0300268 if (xfrm6_tunnel_mpls_supported() &&
269 inet6_add_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS)) {
270 pr_err("%s: can't add protocol\n", __func__);
271 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
272 inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP);
273 return -EAGAIN;
274 }
Xin Long86afc702020-07-06 20:01:31 +0800275#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
276 if (xfrm_input_register_afinfo(&tunnel6_input_afinfo)) {
277 pr_err("%s: can't add input afinfo\n", __func__);
278 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
279 inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP);
280 if (xfrm6_tunnel_mpls_supported())
281 inet6_del_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS);
282 return -EAGAIN;
283 }
284#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800285 return 0;
286}
287
288static void __exit tunnel6_fini(void)
289{
Xin Long86afc702020-07-06 20:01:31 +0800290#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
291 if (xfrm_input_unregister_afinfo(&tunnel6_input_afinfo))
292 pr_err("%s: can't remove input afinfo\n", __func__);
293#endif
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800294 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
Joe Perchesf3213832012-05-15 14:11:53 +0000295 pr_err("%s: can't remove protocol\n", __func__);
Herbert Xud2acc342006-03-28 01:12:13 -0800296 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
Joe Perchesf3213832012-05-15 14:11:53 +0000297 pr_err("%s: can't remove protocol\n", __func__);
Vadim Fedorenkof234efa2020-05-20 18:21:37 +0300298 if (xfrm6_tunnel_mpls_supported() &&
299 inet6_del_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS))
300 pr_err("%s: can't remove protocol\n", __func__);
Herbert Xud2acc342006-03-28 01:12:13 -0800301}
302
303module_init(tunnel6_init);
304module_exit(tunnel6_fini);
305MODULE_LICENSE("GPL");