1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C)2003,2004 USAGI/WIDE Project
5 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
6 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
9 #define pr_fmt(fmt) "IPv6: " fmt
11 #include <linux/icmpv6.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/slab.h>
19 #include <net/protocol.h>
22 static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
23 static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
24 static struct xfrm6_tunnel __rcu *tunnelmpls6_handlers __read_mostly;
25 static DEFINE_MUTEX(tunnel6_mutex);
27 static inline int xfrm6_tunnel_mpls_supported(void)
29 return IS_ENABLED(CONFIG_MPLS);
32 int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
34 struct xfrm6_tunnel __rcu **pprev;
35 struct xfrm6_tunnel *t;
37 int priority = handler->priority;
39 mutex_lock(&tunnel6_mutex);
43 pprev = &tunnel6_handlers;
46 pprev = &tunnel46_handlers;
49 pprev = &tunnelmpls6_handlers;
55 for (; (t = rcu_dereference_protected(*pprev,
56 lockdep_is_held(&tunnel6_mutex))) != NULL;
58 if (t->priority > priority)
60 if (t->priority == priority)
64 handler->next = *pprev;
65 rcu_assign_pointer(*pprev, handler);
70 mutex_unlock(&tunnel6_mutex);
74 EXPORT_SYMBOL(xfrm6_tunnel_register);
76 int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
78 struct xfrm6_tunnel __rcu **pprev;
79 struct xfrm6_tunnel *t;
82 mutex_lock(&tunnel6_mutex);
86 pprev = &tunnel6_handlers;
89 pprev = &tunnel46_handlers;
92 pprev = &tunnelmpls6_handlers;
98 for (; (t = rcu_dereference_protected(*pprev,
99 lockdep_is_held(&tunnel6_mutex))) != NULL;
102 *pprev = handler->next;
109 mutex_unlock(&tunnel6_mutex);
115 EXPORT_SYMBOL(xfrm6_tunnel_deregister);
117 #define for_each_tunnel_rcu(head, handler) \
118 for (handler = rcu_dereference(head); \
120 handler = rcu_dereference(handler->next)) \
122 static int tunnelmpls6_rcv(struct sk_buff *skb)
124 struct xfrm6_tunnel *handler;
126 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
129 for_each_tunnel_rcu(tunnelmpls6_handlers, handler)
130 if (!handler->handler(skb))
133 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
140 static int tunnel6_rcv(struct sk_buff *skb)
142 struct xfrm6_tunnel *handler;
144 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
147 for_each_tunnel_rcu(tunnel6_handlers, handler)
148 if (!handler->handler(skb))
151 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
158 #if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
159 static int tunnel6_rcv_cb(struct sk_buff *skb, u8 proto, int err)
161 struct xfrm6_tunnel __rcu *head;
162 struct xfrm6_tunnel *handler;
165 head = (proto == IPPROTO_IPV6) ? tunnel6_handlers : tunnel46_handlers;
167 for_each_tunnel_rcu(head, handler) {
168 if (handler->cb_handler) {
169 ret = handler->cb_handler(skb, err);
178 static const struct xfrm_input_afinfo tunnel6_input_afinfo = {
181 .callback = tunnel6_rcv_cb,
185 static int tunnel46_rcv(struct sk_buff *skb)
187 struct xfrm6_tunnel *handler;
189 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
192 for_each_tunnel_rcu(tunnel46_handlers, handler)
193 if (!handler->handler(skb))
196 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
203 static int tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
204 u8 type, u8 code, int offset, __be32 info)
206 struct xfrm6_tunnel *handler;
208 for_each_tunnel_rcu(tunnel6_handlers, handler)
209 if (!handler->err_handler(skb, opt, type, code, offset, info))
215 static int tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
216 u8 type, u8 code, int offset, __be32 info)
218 struct xfrm6_tunnel *handler;
220 for_each_tunnel_rcu(tunnel46_handlers, handler)
221 if (!handler->err_handler(skb, opt, type, code, offset, info))
227 static int tunnelmpls6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
228 u8 type, u8 code, int offset, __be32 info)
230 struct xfrm6_tunnel *handler;
232 for_each_tunnel_rcu(tunnelmpls6_handlers, handler)
233 if (!handler->err_handler(skb, opt, type, code, offset, info))
239 static const struct inet6_protocol tunnel6_protocol = {
240 .handler = tunnel6_rcv,
241 .err_handler = tunnel6_err,
242 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
245 static const struct inet6_protocol tunnel46_protocol = {
246 .handler = tunnel46_rcv,
247 .err_handler = tunnel46_err,
248 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
251 static const struct inet6_protocol tunnelmpls6_protocol = {
252 .handler = tunnelmpls6_rcv,
253 .err_handler = tunnelmpls6_err,
254 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
257 static int __init tunnel6_init(void)
259 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
260 pr_err("%s: can't add protocol\n", __func__);
263 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
264 pr_err("%s: can't add protocol\n", __func__);
265 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
268 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);
275 #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);
288 static void __exit tunnel6_fini(void)
290 #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__);
294 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
295 pr_err("%s: can't remove protocol\n", __func__);
296 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
297 pr_err("%s: can't remove protocol\n", __func__);
298 if (xfrm6_tunnel_mpls_supported() &&
299 inet6_del_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS))
300 pr_err("%s: can't remove protocol\n", __func__);
303 module_init(tunnel6_init);
304 module_exit(tunnel6_fini);
305 MODULE_LICENSE("GPL");