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> |
| 9 | #include <linux/netdevice.h> |
| 10 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 12 | #include <net/icmp.h> |
| 13 | #include <net/ip.h> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 14 | #include <net/protocol.h> |
| 15 | #include <net/xfrm.h> |
| 16 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 17 | static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly; |
| 18 | static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 19 | static DEFINE_MUTEX(tunnel4_mutex); |
| 20 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 21 | static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family) |
Pavel Emelyanov | 358352b | 2007-11-10 21:48:54 -0800 | [diff] [blame] | 22 | { |
| 23 | return (family == AF_INET) ? &tunnel4_handlers : &tunnel64_handlers; |
| 24 | } |
| 25 | |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 26 | int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 27 | { |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 28 | struct xfrm_tunnel __rcu **pprev; |
| 29 | struct xfrm_tunnel *t; |
| 30 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 31 | int ret = -EEXIST; |
| 32 | int priority = handler->priority; |
| 33 | |
| 34 | mutex_lock(&tunnel4_mutex); |
| 35 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 36 | for (pprev = fam_handlers(family); |
| 37 | (t = rcu_dereference_protected(*pprev, |
| 38 | lockdep_is_held(&tunnel4_mutex))) != NULL; |
| 39 | pprev = &t->next) { |
| 40 | if (t->priority > priority) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 41 | break; |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 42 | if (t->priority == priority) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 43 | goto err; |
| 44 | } |
| 45 | |
| 46 | handler->next = *pprev; |
Eric Dumazet | 49d61e2 | 2010-09-09 05:33:43 +0000 | [diff] [blame] | 47 | rcu_assign_pointer(*pprev, handler); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 48 | |
| 49 | ret = 0; |
| 50 | |
| 51 | err: |
| 52 | mutex_unlock(&tunnel4_mutex); |
| 53 | |
| 54 | return ret; |
| 55 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 56 | EXPORT_SYMBOL(xfrm4_tunnel_register); |
| 57 | |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 58 | int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 59 | { |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 60 | struct xfrm_tunnel __rcu **pprev; |
| 61 | struct xfrm_tunnel *t; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 62 | int ret = -ENOENT; |
| 63 | |
| 64 | mutex_lock(&tunnel4_mutex); |
| 65 | |
Eric Dumazet | b33eab0 | 2010-10-25 21:01:26 +0000 | [diff] [blame] | 66 | for (pprev = fam_handlers(family); |
| 67 | (t = rcu_dereference_protected(*pprev, |
| 68 | lockdep_is_held(&tunnel4_mutex))) != NULL; |
| 69 | pprev = &t->next) { |
| 70 | if (t == handler) { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 71 | *pprev = handler->next; |
| 72 | ret = 0; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | mutex_unlock(&tunnel4_mutex); |
| 78 | |
| 79 | synchronize_net(); |
| 80 | |
| 81 | return ret; |
| 82 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 83 | EXPORT_SYMBOL(xfrm4_tunnel_deregister); |
| 84 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 85 | #define for_each_tunnel_rcu(head, handler) \ |
| 86 | for (handler = rcu_dereference(head); \ |
| 87 | handler != NULL; \ |
| 88 | handler = rcu_dereference(handler->next)) \ |
| 89 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 90 | static int tunnel4_rcv(struct sk_buff *skb) |
| 91 | { |
| 92 | struct xfrm_tunnel *handler; |
| 93 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 94 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 95 | goto drop; |
| 96 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 97 | for_each_tunnel_rcu(tunnel4_handlers, handler) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 98 | if (!handler->handler(skb)) |
| 99 | return 0; |
| 100 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 101 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 102 | |
| 103 | drop: |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 104 | kfree_skb(skb); |
| 105 | return 0; |
| 106 | } |
| 107 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 108 | #if IS_ENABLED(CONFIG_IPV6) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 109 | static int tunnel64_rcv(struct sk_buff *skb) |
| 110 | { |
| 111 | struct xfrm_tunnel *handler; |
| 112 | |
YOSHIFUJI Hideaki | baa2bfb | 2008-05-30 11:35:03 +0900 | [diff] [blame] | 113 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 114 | goto drop; |
| 115 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 116 | for_each_tunnel_rcu(tunnel64_handlers, handler) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 117 | if (!handler->handler(skb)) |
| 118 | return 0; |
| 119 | |
| 120 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 121 | |
| 122 | drop: |
| 123 | kfree_skb(skb); |
| 124 | return 0; |
| 125 | } |
| 126 | #endif |
| 127 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 128 | static void tunnel4_err(struct sk_buff *skb, u32 info) |
| 129 | { |
| 130 | struct xfrm_tunnel *handler; |
| 131 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 132 | for_each_tunnel_rcu(tunnel4_handlers, handler) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 133 | if (!handler->err_handler(skb, info)) |
| 134 | break; |
| 135 | } |
| 136 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 137 | #if IS_ENABLED(CONFIG_IPV6) |
Pavel Emelyanov | 99f9332 | 2007-11-10 21:47:39 -0800 | [diff] [blame] | 138 | static void tunnel64_err(struct sk_buff *skb, u32 info) |
| 139 | { |
| 140 | struct xfrm_tunnel *handler; |
| 141 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 142 | for_each_tunnel_rcu(tunnel64_handlers, handler) |
Pavel Emelyanov | 99f9332 | 2007-11-10 21:47:39 -0800 | [diff] [blame] | 143 | if (!handler->err_handler(skb, info)) |
| 144 | break; |
| 145 | } |
| 146 | #endif |
| 147 | |
Alexey Dobriyan | 3261309 | 2009-09-14 12:21:47 +0000 | [diff] [blame] | 148 | static const struct net_protocol tunnel4_protocol = { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 149 | .handler = tunnel4_rcv, |
| 150 | .err_handler = tunnel4_err, |
| 151 | .no_policy = 1, |
Pavel Emelyanov | 4597a0c | 2008-04-16 01:06:56 -0700 | [diff] [blame] | 152 | .netns_ok = 1, |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 155 | #if IS_ENABLED(CONFIG_IPV6) |
Alexey Dobriyan | 3261309 | 2009-09-14 12:21:47 +0000 | [diff] [blame] | 156 | static const struct net_protocol tunnel64_protocol = { |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 157 | .handler = tunnel64_rcv, |
Pavel Emelyanov | 99f9332 | 2007-11-10 21:47:39 -0800 | [diff] [blame] | 158 | .err_handler = tunnel64_err, |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 159 | .no_policy = 1, |
Pavel Emelyanov | b0970c4 | 2008-04-16 01:17:39 -0700 | [diff] [blame] | 160 | .netns_ok = 1, |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 161 | }; |
| 162 | #endif |
| 163 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 164 | static int __init tunnel4_init(void) |
| 165 | { |
| 166 | if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 167 | pr_err("%s: can't add protocol\n", __func__); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 168 | return -EAGAIN; |
| 169 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 170 | #if IS_ENABLED(CONFIG_IPV6) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 171 | if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 172 | pr_err("tunnel64 init: can't add protocol\n"); |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 173 | inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP); |
| 174 | return -EAGAIN; |
| 175 | } |
| 176 | #endif |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static void __exit tunnel4_fini(void) |
| 181 | { |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 182 | #if IS_ENABLED(CONFIG_IPV6) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 183 | if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6)) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 184 | pr_err("tunnel64 close: can't remove protocol\n"); |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 185 | #endif |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 186 | if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP)) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 187 | pr_err("tunnel4 close: can't remove protocol\n"); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | module_init(tunnel4_init); |
| 191 | module_exit(tunnel4_fini); |
| 192 | MODULE_LICENSE("GPL"); |