Loading...
1/*
2 * Copyright (C)2003,2004 USAGI/WIDE Project
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
18 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
19 */
20
21#define pr_fmt(fmt) "IPv6: " fmt
22
23#include <linux/icmpv6.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/mutex.h>
27#include <linux/netdevice.h>
28#include <linux/skbuff.h>
29#include <linux/slab.h>
30#include <net/ipv6.h>
31#include <net/protocol.h>
32#include <net/xfrm.h>
33
34static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
35static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
36static DEFINE_MUTEX(tunnel6_mutex);
37
38int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
39{
40 struct xfrm6_tunnel __rcu **pprev;
41 struct xfrm6_tunnel *t;
42 int ret = -EEXIST;
43 int priority = handler->priority;
44
45 mutex_lock(&tunnel6_mutex);
46
47 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
48 (t = rcu_dereference_protected(*pprev,
49 lockdep_is_held(&tunnel6_mutex))) != NULL;
50 pprev = &t->next) {
51 if (t->priority > priority)
52 break;
53 if (t->priority == priority)
54 goto err;
55 }
56
57 handler->next = *pprev;
58 rcu_assign_pointer(*pprev, handler);
59
60 ret = 0;
61
62err:
63 mutex_unlock(&tunnel6_mutex);
64
65 return ret;
66}
67EXPORT_SYMBOL(xfrm6_tunnel_register);
68
69int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
70{
71 struct xfrm6_tunnel __rcu **pprev;
72 struct xfrm6_tunnel *t;
73 int ret = -ENOENT;
74
75 mutex_lock(&tunnel6_mutex);
76
77 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
78 (t = rcu_dereference_protected(*pprev,
79 lockdep_is_held(&tunnel6_mutex))) != NULL;
80 pprev = &t->next) {
81 if (t == handler) {
82 *pprev = handler->next;
83 ret = 0;
84 break;
85 }
86 }
87
88 mutex_unlock(&tunnel6_mutex);
89
90 synchronize_net();
91
92 return ret;
93}
94EXPORT_SYMBOL(xfrm6_tunnel_deregister);
95
96#define for_each_tunnel_rcu(head, handler) \
97 for (handler = rcu_dereference(head); \
98 handler != NULL; \
99 handler = rcu_dereference(handler->next)) \
100
101static int tunnel6_rcv(struct sk_buff *skb)
102{
103 struct xfrm6_tunnel *handler;
104
105 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
106 goto drop;
107
108 for_each_tunnel_rcu(tunnel6_handlers, handler)
109 if (!handler->handler(skb))
110 return 0;
111
112 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
113
114drop:
115 kfree_skb(skb);
116 return 0;
117}
118
119static int tunnel46_rcv(struct sk_buff *skb)
120{
121 struct xfrm6_tunnel *handler;
122
123 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
124 goto drop;
125
126 for_each_tunnel_rcu(tunnel46_handlers, handler)
127 if (!handler->handler(skb))
128 return 0;
129
130 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
131
132drop:
133 kfree_skb(skb);
134 return 0;
135}
136
137static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
138 u8 type, u8 code, int offset, __be32 info)
139{
140 struct xfrm6_tunnel *handler;
141
142 for_each_tunnel_rcu(tunnel6_handlers, handler)
143 if (!handler->err_handler(skb, opt, type, code, offset, info))
144 break;
145}
146
147static void tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
148 u8 type, u8 code, int offset, __be32 info)
149{
150 struct xfrm6_tunnel *handler;
151
152 for_each_tunnel_rcu(tunnel46_handlers, handler)
153 if (!handler->err_handler(skb, opt, type, code, offset, info))
154 break;
155}
156
157static const struct inet6_protocol tunnel6_protocol = {
158 .handler = tunnel6_rcv,
159 .err_handler = tunnel6_err,
160 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
161};
162
163static const struct inet6_protocol tunnel46_protocol = {
164 .handler = tunnel46_rcv,
165 .err_handler = tunnel46_err,
166 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
167};
168
169static int __init tunnel6_init(void)
170{
171 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
172 pr_err("%s: can't add protocol\n", __func__);
173 return -EAGAIN;
174 }
175 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
176 pr_err("%s: can't add protocol\n", __func__);
177 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
178 return -EAGAIN;
179 }
180 return 0;
181}
182
183static void __exit tunnel6_fini(void)
184{
185 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
186 pr_err("%s: can't remove protocol\n", __func__);
187 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
188 pr_err("%s: can't remove protocol\n", __func__);
189}
190
191module_init(tunnel6_init);
192module_exit(tunnel6_fini);
193MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C)2003,2004 USAGI/WIDE Project
4 *
5 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
6 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
7 */
8
9#define pr_fmt(fmt) "IPv6: " fmt
10
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>
18#include <net/ipv6.h>
19#include <net/protocol.h>
20#include <net/xfrm.h>
21
22static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
23static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
24static DEFINE_MUTEX(tunnel6_mutex);
25
26int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
27{
28 struct xfrm6_tunnel __rcu **pprev;
29 struct xfrm6_tunnel *t;
30 int ret = -EEXIST;
31 int priority = handler->priority;
32
33 mutex_lock(&tunnel6_mutex);
34
35 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
36 (t = rcu_dereference_protected(*pprev,
37 lockdep_is_held(&tunnel6_mutex))) != NULL;
38 pprev = &t->next) {
39 if (t->priority > priority)
40 break;
41 if (t->priority == priority)
42 goto err;
43 }
44
45 handler->next = *pprev;
46 rcu_assign_pointer(*pprev, handler);
47
48 ret = 0;
49
50err:
51 mutex_unlock(&tunnel6_mutex);
52
53 return ret;
54}
55EXPORT_SYMBOL(xfrm6_tunnel_register);
56
57int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
58{
59 struct xfrm6_tunnel __rcu **pprev;
60 struct xfrm6_tunnel *t;
61 int ret = -ENOENT;
62
63 mutex_lock(&tunnel6_mutex);
64
65 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
66 (t = rcu_dereference_protected(*pprev,
67 lockdep_is_held(&tunnel6_mutex))) != NULL;
68 pprev = &t->next) {
69 if (t == handler) {
70 *pprev = handler->next;
71 ret = 0;
72 break;
73 }
74 }
75
76 mutex_unlock(&tunnel6_mutex);
77
78 synchronize_net();
79
80 return ret;
81}
82EXPORT_SYMBOL(xfrm6_tunnel_deregister);
83
84#define for_each_tunnel_rcu(head, handler) \
85 for (handler = rcu_dereference(head); \
86 handler != NULL; \
87 handler = rcu_dereference(handler->next)) \
88
89static int tunnel6_rcv(struct sk_buff *skb)
90{
91 struct xfrm6_tunnel *handler;
92
93 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
94 goto drop;
95
96 for_each_tunnel_rcu(tunnel6_handlers, handler)
97 if (!handler->handler(skb))
98 return 0;
99
100 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
101
102drop:
103 kfree_skb(skb);
104 return 0;
105}
106
107static int tunnel46_rcv(struct sk_buff *skb)
108{
109 struct xfrm6_tunnel *handler;
110
111 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
112 goto drop;
113
114 for_each_tunnel_rcu(tunnel46_handlers, handler)
115 if (!handler->handler(skb))
116 return 0;
117
118 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
119
120drop:
121 kfree_skb(skb);
122 return 0;
123}
124
125static int tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
126 u8 type, u8 code, int offset, __be32 info)
127{
128 struct xfrm6_tunnel *handler;
129
130 for_each_tunnel_rcu(tunnel6_handlers, handler)
131 if (!handler->err_handler(skb, opt, type, code, offset, info))
132 return 0;
133
134 return -ENOENT;
135}
136
137static int tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
138 u8 type, u8 code, int offset, __be32 info)
139{
140 struct xfrm6_tunnel *handler;
141
142 for_each_tunnel_rcu(tunnel46_handlers, handler)
143 if (!handler->err_handler(skb, opt, type, code, offset, info))
144 return 0;
145
146 return -ENOENT;
147}
148
149static const struct inet6_protocol tunnel6_protocol = {
150 .handler = tunnel6_rcv,
151 .err_handler = tunnel6_err,
152 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
153};
154
155static const struct inet6_protocol tunnel46_protocol = {
156 .handler = tunnel46_rcv,
157 .err_handler = tunnel46_err,
158 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
159};
160
161static int __init tunnel6_init(void)
162{
163 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
164 pr_err("%s: can't add protocol\n", __func__);
165 return -EAGAIN;
166 }
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);
170 return -EAGAIN;
171 }
172 return 0;
173}
174
175static void __exit tunnel6_fini(void)
176{
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__);
181}
182
183module_init(tunnel6_init);
184module_exit(tunnel6_fini);
185MODULE_LICENSE("GPL");