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