Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 2 | /* tunnel4.c: Generic IP tunnel transformer. |
| 3 | * |
| 4 | * Copyright (C) 2003 David S. Miller (davem@redhat.com) |
| 5 | */ |
| 6 | |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mutex.h> |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 10 | #include <linux/mpls.h> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 11 | #include <linux/netdevice.h> |
| 12 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 14 | #include <net/icmp.h> |
| 15 | #include <net/ip.h> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 16 | #include <net/protocol.h> |
| 17 | #include <net/xfrm.h> |
| 18 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 19 | static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly; |
| 20 | static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly; |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 21 | static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 22 | static DEFINE_MUTEX(tunnel4_mutex); |
| 23 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 24 | static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family) |
Pavel Emelyanov | 358352b | 2007-11-10 21:48:54 -0800 | [diff] [blame] | 25 | { |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 26 | return (family == AF_INET) ? &tunnel4_handlers : |
| 27 | (family == AF_INET6) ? &tunnel64_handlers : |
| 28 | &tunnelmpls4_handlers; |
Pavel Emelyanov | 358352b | 2007-11-10 21:48:54 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 31 | int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 32 | { |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 33 | struct xfrm_tunnel __rcu **pprev; |
| 34 | struct xfrm_tunnel *t; |
| 35 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 36 | int ret = -EEXIST; |
| 37 | int priority = handler->priority; |
| 38 | |
| 39 | mutex_lock(&tunnel4_mutex); |
| 40 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 41 | for (pprev = fam_handlers(family); |
| 42 | (t = rcu_dereference_protected(*pprev, |
| 43 | lockdep_is_held(&tunnel4_mutex))) != NULL; |
| 44 | pprev = &t->next) { |
| 45 | if (t->priority > priority) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 46 | break; |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 47 | if (t->priority == priority) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 48 | goto err; |
| 49 | } |
| 50 | |
| 51 | handler->next = *pprev; |
Eric Dumazet | 49d61e2 | 2010-09-09 05:33:43 +0000 | [diff] [blame] | 52 | rcu_assign_pointer(*pprev, handler); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 53 | |
| 54 | ret = 0; |
| 55 | |
| 56 | err: |
| 57 | mutex_unlock(&tunnel4_mutex); |
| 58 | |
| 59 | return ret; |
| 60 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 61 | EXPORT_SYMBOL(xfrm4_tunnel_register); |
| 62 | |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 63 | int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 64 | { |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 65 | struct xfrm_tunnel __rcu **pprev; |
| 66 | struct xfrm_tunnel *t; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 67 | int ret = -ENOENT; |
| 68 | |
| 69 | mutex_lock(&tunnel4_mutex); |
| 70 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 71 | for (pprev = fam_handlers(family); |
| 72 | (t = rcu_dereference_protected(*pprev, |
| 73 | lockdep_is_held(&tunnel4_mutex))) != NULL; |
| 74 | pprev = &t->next) { |
| 75 | if (t == handler) { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 76 | *pprev = handler->next; |
| 77 | ret = 0; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | mutex_unlock(&tunnel4_mutex); |
| 83 | |
| 84 | synchronize_net(); |
| 85 | |
| 86 | return ret; |
| 87 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 88 | EXPORT_SYMBOL(xfrm4_tunnel_deregister); |
| 89 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 90 | #define for_each_tunnel_rcu(head, handler) \ |
| 91 | for (handler = rcu_dereference(head); \ |
| 92 | handler != NULL; \ |
| 93 | handler = rcu_dereference(handler->next)) \ |
Stephen Hemminger | 82695b3 | 2018-02-27 15:48:21 -0800 | [diff] [blame] | 94 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 95 | static int tunnel4_rcv(struct sk_buff *skb) |
| 96 | { |
| 97 | struct xfrm_tunnel *handler; |
| 98 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 99 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 100 | goto drop; |
| 101 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 102 | for_each_tunnel_rcu(tunnel4_handlers, handler) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 103 | if (!handler->handler(skb)) |
| 104 | return 0; |
| 105 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 106 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 107 | |
| 108 | drop: |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 109 | kfree_skb(skb); |
| 110 | return 0; |
| 111 | } |
| 112 | |
Xin Long | 6df2db5 | 2020-07-06 20:01:30 +0800 | [diff] [blame] | 113 | #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL) |
| 114 | static int tunnel4_rcv_cb(struct sk_buff *skb, u8 proto, int err) |
| 115 | { |
| 116 | struct xfrm_tunnel __rcu *head; |
| 117 | struct xfrm_tunnel *handler; |
| 118 | int ret; |
| 119 | |
| 120 | head = (proto == IPPROTO_IPIP) ? tunnel4_handlers : tunnel64_handlers; |
| 121 | |
| 122 | for_each_tunnel_rcu(head, handler) { |
| 123 | if (handler->cb_handler) { |
| 124 | ret = handler->cb_handler(skb, err); |
| 125 | if (ret <= 0) |
| 126 | return ret; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static const struct xfrm_input_afinfo tunnel4_input_afinfo = { |
| 134 | .family = AF_INET, |
| 135 | .is_ipip = true, |
| 136 | .callback = tunnel4_rcv_cb, |
| 137 | }; |
| 138 | #endif |
| 139 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 140 | #if IS_ENABLED(CONFIG_IPV6) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 141 | static int tunnel64_rcv(struct sk_buff *skb) |
| 142 | { |
| 143 | struct xfrm_tunnel *handler; |
| 144 | |
YOSHIFUJI Hideaki | baa2bfb | 2008-05-30 11:35:03 +0900 | [diff] [blame] | 145 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 146 | goto drop; |
| 147 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 148 | for_each_tunnel_rcu(tunnel64_handlers, handler) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 149 | if (!handler->handler(skb)) |
| 150 | return 0; |
| 151 | |
| 152 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 153 | |
| 154 | drop: |
| 155 | kfree_skb(skb); |
| 156 | return 0; |
| 157 | } |
| 158 | #endif |
| 159 | |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 160 | #if IS_ENABLED(CONFIG_MPLS) |
| 161 | static int tunnelmpls4_rcv(struct sk_buff *skb) |
| 162 | { |
| 163 | struct xfrm_tunnel *handler; |
| 164 | |
| 165 | if (!pskb_may_pull(skb, sizeof(struct mpls_label))) |
| 166 | goto drop; |
| 167 | |
| 168 | for_each_tunnel_rcu(tunnelmpls4_handlers, handler) |
| 169 | if (!handler->handler(skb)) |
| 170 | return 0; |
| 171 | |
| 172 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 173 | |
| 174 | drop: |
| 175 | kfree_skb(skb); |
| 176 | return 0; |
| 177 | } |
| 178 | #endif |
| 179 | |
Stefano Brivio | 32bbd87 | 2018-11-08 12:19:21 +0100 | [diff] [blame] | 180 | static int tunnel4_err(struct sk_buff *skb, u32 info) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 181 | { |
| 182 | struct xfrm_tunnel *handler; |
| 183 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 184 | for_each_tunnel_rcu(tunnel4_handlers, handler) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 185 | if (!handler->err_handler(skb, info)) |
Stefano Brivio | 32bbd87 | 2018-11-08 12:19:21 +0100 | [diff] [blame] | 186 | return 0; |
| 187 | |
| 188 | return -ENOENT; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 191 | #if IS_ENABLED(CONFIG_IPV6) |
Stefano Brivio | 32bbd87 | 2018-11-08 12:19:21 +0100 | [diff] [blame] | 192 | static int tunnel64_err(struct sk_buff *skb, u32 info) |
Pavel Emelyanov | 99f9332 | 2007-11-10 21:47:39 -0800 | [diff] [blame] | 193 | { |
| 194 | struct xfrm_tunnel *handler; |
| 195 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 196 | for_each_tunnel_rcu(tunnel64_handlers, handler) |
Pavel Emelyanov | 99f9332 | 2007-11-10 21:47:39 -0800 | [diff] [blame] | 197 | if (!handler->err_handler(skb, info)) |
Stefano Brivio | 32bbd87 | 2018-11-08 12:19:21 +0100 | [diff] [blame] | 198 | return 0; |
| 199 | |
| 200 | return -ENOENT; |
Pavel Emelyanov | 99f9332 | 2007-11-10 21:47:39 -0800 | [diff] [blame] | 201 | } |
| 202 | #endif |
| 203 | |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 204 | #if IS_ENABLED(CONFIG_MPLS) |
Stefano Brivio | 32bbd87 | 2018-11-08 12:19:21 +0100 | [diff] [blame] | 205 | static int tunnelmpls4_err(struct sk_buff *skb, u32 info) |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 206 | { |
| 207 | struct xfrm_tunnel *handler; |
| 208 | |
| 209 | for_each_tunnel_rcu(tunnelmpls4_handlers, handler) |
| 210 | if (!handler->err_handler(skb, info)) |
Stefano Brivio | 32bbd87 | 2018-11-08 12:19:21 +0100 | [diff] [blame] | 211 | return 0; |
| 212 | |
| 213 | return -ENOENT; |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 214 | } |
| 215 | #endif |
| 216 | |
Alexey Dobriyan | 3261309 | 2009-09-14 12:21:47 +0000 | [diff] [blame] | 217 | static const struct net_protocol tunnel4_protocol = { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 218 | .handler = tunnel4_rcv, |
| 219 | .err_handler = tunnel4_err, |
| 220 | .no_policy = 1, |
Pavel Emelyanov | 4597a0c | 2008-04-16 01:06:56 -0700 | [diff] [blame] | 221 | .netns_ok = 1, |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 222 | }; |
| 223 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 224 | #if IS_ENABLED(CONFIG_IPV6) |
Alexey Dobriyan | 3261309 | 2009-09-14 12:21:47 +0000 | [diff] [blame] | 225 | static const struct net_protocol tunnel64_protocol = { |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 226 | .handler = tunnel64_rcv, |
Pavel Emelyanov | 99f9332 | 2007-11-10 21:47:39 -0800 | [diff] [blame] | 227 | .err_handler = tunnel64_err, |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 228 | .no_policy = 1, |
Pavel Emelyanov | b0970c4 | 2008-04-16 01:17:39 -0700 | [diff] [blame] | 229 | .netns_ok = 1, |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 230 | }; |
| 231 | #endif |
| 232 | |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 233 | #if IS_ENABLED(CONFIG_MPLS) |
| 234 | static const struct net_protocol tunnelmpls4_protocol = { |
| 235 | .handler = tunnelmpls4_rcv, |
| 236 | .err_handler = tunnelmpls4_err, |
| 237 | .no_policy = 1, |
| 238 | .netns_ok = 1, |
| 239 | }; |
| 240 | #endif |
| 241 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 242 | static int __init tunnel4_init(void) |
| 243 | { |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 244 | if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) |
Simon Horman | aa9667e | 2016-07-10 10:20:11 +0900 | [diff] [blame] | 245 | goto err; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 246 | #if IS_ENABLED(CONFIG_IPV6) |
Simon Horman | aa9667e | 2016-07-10 10:20:11 +0900 | [diff] [blame] | 247 | if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) { |
| 248 | inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP); |
| 249 | goto err; |
| 250 | } |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 251 | #endif |
| 252 | #if IS_ENABLED(CONFIG_MPLS) |
Simon Horman | aa9667e | 2016-07-10 10:20:11 +0900 | [diff] [blame] | 253 | if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) { |
| 254 | inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP); |
| 255 | #if IS_ENABLED(CONFIG_IPV6) |
| 256 | inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6); |
| 257 | #endif |
| 258 | goto err; |
| 259 | } |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 260 | #endif |
Xin Long | 6df2db5 | 2020-07-06 20:01:30 +0800 | [diff] [blame] | 261 | #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL) |
| 262 | if (xfrm_input_register_afinfo(&tunnel4_input_afinfo)) { |
| 263 | inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP); |
| 264 | #if IS_ENABLED(CONFIG_IPV6) |
| 265 | inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6); |
| 266 | #endif |
| 267 | #if IS_ENABLED(CONFIG_MPLS) |
| 268 | inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS); |
| 269 | #endif |
| 270 | goto err; |
| 271 | } |
| 272 | #endif |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 273 | return 0; |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 274 | |
Simon Horman | aa9667e | 2016-07-10 10:20:11 +0900 | [diff] [blame] | 275 | err: |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 276 | pr_err("%s: can't add protocol\n", __func__); |
| 277 | return -EAGAIN; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void __exit tunnel4_fini(void) |
| 281 | { |
Xin Long | 6df2db5 | 2020-07-06 20:01:30 +0800 | [diff] [blame] | 282 | #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL) |
| 283 | if (xfrm_input_unregister_afinfo(&tunnel4_input_afinfo)) |
| 284 | pr_err("tunnel4 close: can't remove input afinfo\n"); |
| 285 | #endif |
Simon Horman | 8afe97e | 2016-07-07 07:56:12 +0200 | [diff] [blame] | 286 | #if IS_ENABLED(CONFIG_MPLS) |
| 287 | if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) |
| 288 | pr_err("tunnelmpls4 close: can't remove protocol\n"); |
| 289 | #endif |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 290 | #if IS_ENABLED(CONFIG_IPV6) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 291 | if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6)) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 292 | pr_err("tunnel64 close: can't remove protocol\n"); |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 293 | #endif |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 294 | if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP)) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 295 | pr_err("tunnel4 close: can't remove protocol\n"); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | module_init(tunnel4_init); |
| 299 | module_exit(tunnel4_fini); |
| 300 | MODULE_LICENSE("GPL"); |