hc
2024-09-20 a36159eec6ca17402b0e146b86efaf76568dc353
kernel/net/ipv6/tunnel6.c
....@@ -1,18 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * 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/>.
164 *
175 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
186 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
....@@ -33,7 +21,13 @@
3321
3422 static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
3523 static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
24
+static struct xfrm6_tunnel __rcu *tunnelmpls6_handlers __read_mostly;
3625 static DEFINE_MUTEX(tunnel6_mutex);
26
+
27
+static inline int xfrm6_tunnel_mpls_supported(void)
28
+{
29
+ return IS_ENABLED(CONFIG_MPLS);
30
+}
3731
3832 int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
3933 {
....@@ -44,8 +38,21 @@
4438
4539 mutex_lock(&tunnel6_mutex);
4640
47
- for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
48
- (t = rcu_dereference_protected(*pprev,
41
+ 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,
4956 lockdep_is_held(&tunnel6_mutex))) != NULL;
5057 pprev = &t->next) {
5158 if (t->priority > priority)
....@@ -74,8 +81,21 @@
7481
7582 mutex_lock(&tunnel6_mutex);
7683
77
- for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
78
- (t = rcu_dereference_protected(*pprev,
84
+ 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,
7999 lockdep_is_held(&tunnel6_mutex))) != NULL;
80100 pprev = &t->next) {
81101 if (t == handler) {
....@@ -85,6 +105,7 @@
85105 }
86106 }
87107
108
+err:
88109 mutex_unlock(&tunnel6_mutex);
89110
90111 synchronize_net();
....@@ -97,6 +118,24 @@
97118 for (handler = rcu_dereference(head); \
98119 handler != NULL; \
99120 handler = rcu_dereference(handler->next)) \
121
+
122
+static 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
+
135
+drop:
136
+ kfree_skb(skb);
137
+ return 0;
138
+}
100139
101140 static int tunnel6_rcv(struct sk_buff *skb)
102141 {
....@@ -116,6 +155,33 @@
116155 return 0;
117156 }
118157
158
+#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
159
+static 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
+
178
+static 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
+
119185 static int tunnel46_rcv(struct sk_buff *skb)
120186 {
121187 struct xfrm6_tunnel *handler;
....@@ -134,24 +200,40 @@
134200 return 0;
135201 }
136202
137
-static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
203
+static int tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
138204 u8 type, u8 code, int offset, __be32 info)
139205 {
140206 struct xfrm6_tunnel *handler;
141207
142208 for_each_tunnel_rcu(tunnel6_handlers, handler)
143209 if (!handler->err_handler(skb, opt, type, code, offset, info))
144
- break;
210
+ return 0;
211
+
212
+ return -ENOENT;
145213 }
146214
147
-static void tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
215
+static int tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
148216 u8 type, u8 code, int offset, __be32 info)
149217 {
150218 struct xfrm6_tunnel *handler;
151219
152220 for_each_tunnel_rcu(tunnel46_handlers, handler)
153221 if (!handler->err_handler(skb, opt, type, code, offset, info))
154
- break;
222
+ return 0;
223
+
224
+ return -ENOENT;
225
+}
226
+
227
+static 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;
155237 }
156238
157239 static const struct inet6_protocol tunnel6_protocol = {
....@@ -166,6 +248,12 @@
166248 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
167249 };
168250
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,
255
+};
256
+
169257 static int __init tunnel6_init(void)
170258 {
171259 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
....@@ -177,15 +265,39 @@
177265 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
178266 return -EAGAIN;
179267 }
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);
273
+ return -EAGAIN;
274
+ }
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);
282
+ return -EAGAIN;
283
+ }
284
+#endif
180285 return 0;
181286 }
182287
183288 static void __exit tunnel6_fini(void)
184289 {
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__);
293
+#endif
185294 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
186295 pr_err("%s: can't remove protocol\n", __func__);
187296 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
188297 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__);
189301 }
190302
191303 module_init(tunnel6_init);