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 DEFINE_MUTEX(tunnel6_mutex
);
26 int xfrm6_tunnel_register(struct xfrm6_tunnel
*handler
, unsigned short family
)
28 struct xfrm6_tunnel __rcu
**pprev
;
29 struct xfrm6_tunnel
*t
;
31 int priority
= handler
->priority
;
33 mutex_lock(&tunnel6_mutex
);
35 for (pprev
= (family
== AF_INET6
) ? &tunnel6_handlers
: &tunnel46_handlers
;
36 (t
= rcu_dereference_protected(*pprev
,
37 lockdep_is_held(&tunnel6_mutex
))) != NULL
;
39 if (t
->priority
> priority
)
41 if (t
->priority
== priority
)
45 handler
->next
= *pprev
;
46 rcu_assign_pointer(*pprev
, handler
);
51 mutex_unlock(&tunnel6_mutex
);
55 EXPORT_SYMBOL(xfrm6_tunnel_register
);
57 int xfrm6_tunnel_deregister(struct xfrm6_tunnel
*handler
, unsigned short family
)
59 struct xfrm6_tunnel __rcu
**pprev
;
60 struct xfrm6_tunnel
*t
;
63 mutex_lock(&tunnel6_mutex
);
65 for (pprev
= (family
== AF_INET6
) ? &tunnel6_handlers
: &tunnel46_handlers
;
66 (t
= rcu_dereference_protected(*pprev
,
67 lockdep_is_held(&tunnel6_mutex
))) != NULL
;
70 *pprev
= handler
->next
;
76 mutex_unlock(&tunnel6_mutex
);
82 EXPORT_SYMBOL(xfrm6_tunnel_deregister
);
84 #define for_each_tunnel_rcu(head, handler) \
85 for (handler = rcu_dereference(head); \
87 handler = rcu_dereference(handler->next)) \
89 static int tunnel6_rcv(struct sk_buff *skb)
91 struct xfrm6_tunnel
*handler
;
93 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
96 for_each_tunnel_rcu(tunnel6_handlers
, handler
)
97 if (!handler
->handler(skb
))
100 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
107 static int tunnel46_rcv(struct sk_buff
*skb
)
109 struct xfrm6_tunnel
*handler
;
111 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
114 for_each_tunnel_rcu(tunnel46_handlers
, handler
)
115 if (!handler
->handler(skb
))
118 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
125 static int tunnel6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
126 u8 type
, u8 code
, int offset
, __be32 info
)
128 struct xfrm6_tunnel
*handler
;
130 for_each_tunnel_rcu(tunnel6_handlers
, handler
)
131 if (!handler
->err_handler(skb
, opt
, type
, code
, offset
, info
))
137 static int tunnel46_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
138 u8 type
, u8 code
, int offset
, __be32 info
)
140 struct xfrm6_tunnel
*handler
;
142 for_each_tunnel_rcu(tunnel46_handlers
, handler
)
143 if (!handler
->err_handler(skb
, opt
, type
, code
, offset
, info
))
149 static const struct inet6_protocol tunnel6_protocol
= {
150 .handler
= tunnel6_rcv
,
151 .err_handler
= tunnel6_err
,
152 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
155 static const struct inet6_protocol tunnel46_protocol
= {
156 .handler
= tunnel46_rcv
,
157 .err_handler
= tunnel46_err
,
158 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
161 static int __init
tunnel6_init(void)
163 if (inet6_add_protocol(&tunnel6_protocol
, IPPROTO_IPV6
)) {
164 pr_err("%s: can't add protocol\n", __func__
);
167 if (inet6_add_protocol(&tunnel46_protocol
, IPPROTO_IPIP
)) {
168 pr_err("%s: can't add protocol\n", __func__
);
169 inet6_del_protocol(&tunnel6_protocol
, IPPROTO_IPV6
);
175 static void __exit
tunnel6_fini(void)
177 if (inet6_del_protocol(&tunnel46_protocol
, IPPROTO_IPIP
))
178 pr_err("%s: can't remove protocol\n", __func__
);
179 if (inet6_del_protocol(&tunnel6_protocol
, IPPROTO_IPV6
))
180 pr_err("%s: can't remove protocol\n", __func__
);
183 module_init(tunnel6_init
);
184 module_exit(tunnel6_fini
);
185 MODULE_LICENSE("GPL");