| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * 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 | 4 | * |
|---|
| 17 | 5 | * Authors Mitsuru KANDA <mk@linux-ipv6.org> |
|---|
| 18 | 6 | * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> |
|---|
| .. | .. |
|---|
| 33 | 21 | |
|---|
| 34 | 22 | static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly; |
|---|
| 35 | 23 | static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly; |
|---|
| 24 | +static struct xfrm6_tunnel __rcu *tunnelmpls6_handlers __read_mostly; |
|---|
| 36 | 25 | static DEFINE_MUTEX(tunnel6_mutex); |
|---|
| 26 | + |
|---|
| 27 | +static inline int xfrm6_tunnel_mpls_supported(void) |
|---|
| 28 | +{ |
|---|
| 29 | + return IS_ENABLED(CONFIG_MPLS); |
|---|
| 30 | +} |
|---|
| 37 | 31 | |
|---|
| 38 | 32 | int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family) |
|---|
| 39 | 33 | { |
|---|
| .. | .. |
|---|
| 44 | 38 | |
|---|
| 45 | 39 | mutex_lock(&tunnel6_mutex); |
|---|
| 46 | 40 | |
|---|
| 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, |
|---|
| 49 | 56 | lockdep_is_held(&tunnel6_mutex))) != NULL; |
|---|
| 50 | 57 | pprev = &t->next) { |
|---|
| 51 | 58 | if (t->priority > priority) |
|---|
| .. | .. |
|---|
| 74 | 81 | |
|---|
| 75 | 82 | mutex_lock(&tunnel6_mutex); |
|---|
| 76 | 83 | |
|---|
| 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, |
|---|
| 79 | 99 | lockdep_is_held(&tunnel6_mutex))) != NULL; |
|---|
| 80 | 100 | pprev = &t->next) { |
|---|
| 81 | 101 | if (t == handler) { |
|---|
| .. | .. |
|---|
| 85 | 105 | } |
|---|
| 86 | 106 | } |
|---|
| 87 | 107 | |
|---|
| 108 | +err: |
|---|
| 88 | 109 | mutex_unlock(&tunnel6_mutex); |
|---|
| 89 | 110 | |
|---|
| 90 | 111 | synchronize_net(); |
|---|
| .. | .. |
|---|
| 97 | 118 | for (handler = rcu_dereference(head); \ |
|---|
| 98 | 119 | handler != NULL; \ |
|---|
| 99 | 120 | 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 | +} |
|---|
| 100 | 139 | |
|---|
| 101 | 140 | static int tunnel6_rcv(struct sk_buff *skb) |
|---|
| 102 | 141 | { |
|---|
| .. | .. |
|---|
| 116 | 155 | return 0; |
|---|
| 117 | 156 | } |
|---|
| 118 | 157 | |
|---|
| 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 | + |
|---|
| 119 | 185 | static int tunnel46_rcv(struct sk_buff *skb) |
|---|
| 120 | 186 | { |
|---|
| 121 | 187 | struct xfrm6_tunnel *handler; |
|---|
| .. | .. |
|---|
| 134 | 200 | return 0; |
|---|
| 135 | 201 | } |
|---|
| 136 | 202 | |
|---|
| 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, |
|---|
| 138 | 204 | u8 type, u8 code, int offset, __be32 info) |
|---|
| 139 | 205 | { |
|---|
| 140 | 206 | struct xfrm6_tunnel *handler; |
|---|
| 141 | 207 | |
|---|
| 142 | 208 | for_each_tunnel_rcu(tunnel6_handlers, handler) |
|---|
| 143 | 209 | if (!handler->err_handler(skb, opt, type, code, offset, info)) |
|---|
| 144 | | - break; |
|---|
| 210 | + return 0; |
|---|
| 211 | + |
|---|
| 212 | + return -ENOENT; |
|---|
| 145 | 213 | } |
|---|
| 146 | 214 | |
|---|
| 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, |
|---|
| 148 | 216 | u8 type, u8 code, int offset, __be32 info) |
|---|
| 149 | 217 | { |
|---|
| 150 | 218 | struct xfrm6_tunnel *handler; |
|---|
| 151 | 219 | |
|---|
| 152 | 220 | for_each_tunnel_rcu(tunnel46_handlers, handler) |
|---|
| 153 | 221 | 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; |
|---|
| 155 | 237 | } |
|---|
| 156 | 238 | |
|---|
| 157 | 239 | static const struct inet6_protocol tunnel6_protocol = { |
|---|
| .. | .. |
|---|
| 166 | 248 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
|---|
| 167 | 249 | }; |
|---|
| 168 | 250 | |
|---|
| 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 | + |
|---|
| 169 | 257 | static int __init tunnel6_init(void) |
|---|
| 170 | 258 | { |
|---|
| 171 | 259 | if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) { |
|---|
| .. | .. |
|---|
| 177 | 265 | inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6); |
|---|
| 178 | 266 | return -EAGAIN; |
|---|
| 179 | 267 | } |
|---|
| 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 |
|---|
| 180 | 285 | return 0; |
|---|
| 181 | 286 | } |
|---|
| 182 | 287 | |
|---|
| 183 | 288 | static void __exit tunnel6_fini(void) |
|---|
| 184 | 289 | { |
|---|
| 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 |
|---|
| 185 | 294 | if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP)) |
|---|
| 186 | 295 | pr_err("%s: can't remove protocol\n", __func__); |
|---|
| 187 | 296 | if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6)) |
|---|
| 188 | 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__); |
|---|
| 189 | 301 | } |
|---|
| 190 | 302 | |
|---|
| 191 | 303 | module_init(tunnel6_init); |
|---|