Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * 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 |
Jeff Kirsher | a99421d | 2013-12-06 09:13:39 -0800 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 16 | * |
| 17 | * Authors Mitsuru KANDA <mk@linux-ipv6.org> |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 18 | * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 21 | #define pr_fmt(fmt) "IPv6: " fmt |
| 22 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 23 | #include <linux/icmpv6.h> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 30 | #include <net/ipv6.h> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 31 | #include <net/protocol.h> |
| 32 | #include <net/xfrm.h> |
| 33 | |
Eric Dumazet | 6f0bcf1 | 2010-10-24 21:33:16 +0000 | [diff] [blame] | 34 | static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly; |
| 35 | static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 36 | static DEFINE_MUTEX(tunnel6_mutex); |
| 37 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 38 | int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 39 | { |
Eric Dumazet | 6f0bcf1 | 2010-10-24 21:33:16 +0000 | [diff] [blame] | 40 | struct xfrm6_tunnel __rcu **pprev; |
| 41 | struct xfrm6_tunnel *t; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 42 | int ret = -EEXIST; |
| 43 | int priority = handler->priority; |
| 44 | |
| 45 | mutex_lock(&tunnel6_mutex); |
| 46 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 47 | for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers; |
Eric Dumazet | 6f0bcf1 | 2010-10-24 21:33:16 +0000 | [diff] [blame] | 48 | (t = rcu_dereference_protected(*pprev, |
| 49 | lockdep_is_held(&tunnel6_mutex))) != NULL; |
| 50 | pprev = &t->next) { |
| 51 | if (t->priority > priority) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 52 | break; |
Eric Dumazet | 6f0bcf1 | 2010-10-24 21:33:16 +0000 | [diff] [blame] | 53 | if (t->priority == priority) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 54 | goto err; |
| 55 | } |
| 56 | |
| 57 | handler->next = *pprev; |
Eric Dumazet | 49d61e2 | 2010-09-09 05:33:43 +0000 | [diff] [blame] | 58 | rcu_assign_pointer(*pprev, handler); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 59 | |
| 60 | ret = 0; |
| 61 | |
| 62 | err: |
| 63 | mutex_unlock(&tunnel6_mutex); |
| 64 | |
| 65 | return ret; |
| 66 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 67 | EXPORT_SYMBOL(xfrm6_tunnel_register); |
| 68 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 69 | int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 70 | { |
Eric Dumazet | 6f0bcf1 | 2010-10-24 21:33:16 +0000 | [diff] [blame] | 71 | struct xfrm6_tunnel __rcu **pprev; |
| 72 | struct xfrm6_tunnel *t; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 73 | int ret = -ENOENT; |
| 74 | |
| 75 | mutex_lock(&tunnel6_mutex); |
| 76 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 77 | for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers; |
Eric Dumazet | 6f0bcf1 | 2010-10-24 21:33:16 +0000 | [diff] [blame] | 78 | (t = rcu_dereference_protected(*pprev, |
| 79 | lockdep_is_held(&tunnel6_mutex))) != NULL; |
| 80 | pprev = &t->next) { |
| 81 | if (t == handler) { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 82 | *pprev = handler->next; |
| 83 | ret = 0; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | mutex_unlock(&tunnel6_mutex); |
| 89 | |
| 90 | synchronize_net(); |
| 91 | |
| 92 | return ret; |
| 93 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 94 | EXPORT_SYMBOL(xfrm6_tunnel_deregister); |
| 95 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 96 | #define for_each_tunnel_rcu(head, handler) \ |
| 97 | for (handler = rcu_dereference(head); \ |
| 98 | handler != NULL; \ |
| 99 | handler = rcu_dereference(handler->next)) \ |
| 100 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 101 | static int tunnel6_rcv(struct sk_buff *skb) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 102 | { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 103 | struct xfrm6_tunnel *handler; |
| 104 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 105 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 106 | goto drop; |
| 107 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 108 | for_each_tunnel_rcu(tunnel6_handlers, handler) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 109 | if (!handler->handler(skb)) |
| 110 | return 0; |
| 111 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 112 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 113 | |
| 114 | drop: |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 115 | kfree_skb(skb); |
| 116 | return 0; |
| 117 | } |
| 118 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 119 | static int tunnel46_rcv(struct sk_buff *skb) |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 120 | { |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 121 | struct xfrm6_tunnel *handler; |
| 122 | |
Colin | 8283637 | 2008-05-27 00:04:43 +0800 | [diff] [blame] | 123 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 124 | goto drop; |
| 125 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 126 | for_each_tunnel_rcu(tunnel46_handlers, handler) |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 127 | if (!handler->handler(skb)) |
| 128 | return 0; |
| 129 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 130 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 131 | |
| 132 | drop: |
| 133 | kfree_skb(skb); |
| 134 | return 0; |
| 135 | } |
| 136 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 137 | static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 138 | u8 type, u8 code, int offset, __be32 info) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 139 | { |
| 140 | struct xfrm6_tunnel *handler; |
| 141 | |
Eric Dumazet | 875168a | 2010-08-30 11:07:25 +0000 | [diff] [blame] | 142 | for_each_tunnel_rcu(tunnel6_handlers, handler) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 143 | if (!handler->err_handler(skb, opt, type, code, offset, info)) |
| 144 | break; |
| 145 | } |
| 146 | |
Michal Kubeček | ebac62f | 2015-11-03 08:51:07 +0100 | [diff] [blame] | 147 | static void tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 148 | u8 type, u8 code, int offset, __be32 info) |
| 149 | { |
| 150 | struct xfrm6_tunnel *handler; |
| 151 | |
| 152 | for_each_tunnel_rcu(tunnel46_handlers, handler) |
| 153 | if (!handler->err_handler(skb, opt, type, code, offset, info)) |
| 154 | break; |
| 155 | } |
| 156 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 157 | static const struct inet6_protocol tunnel6_protocol = { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 158 | .handler = tunnel6_rcv, |
| 159 | .err_handler = tunnel6_err, |
| 160 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 161 | }; |
| 162 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 163 | static const struct inet6_protocol tunnel46_protocol = { |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 164 | .handler = tunnel46_rcv, |
Michal Kubeček | ebac62f | 2015-11-03 08:51:07 +0100 | [diff] [blame] | 165 | .err_handler = tunnel46_err, |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 166 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 167 | }; |
| 168 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 169 | static int __init tunnel6_init(void) |
| 170 | { |
| 171 | if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 172 | pr_err("%s: can't add protocol\n", __func__); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 173 | return -EAGAIN; |
| 174 | } |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 175 | if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 176 | pr_err("%s: can't add protocol\n", __func__); |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 177 | inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6); |
| 178 | return -EAGAIN; |
| 179 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static void __exit tunnel6_fini(void) |
| 184 | { |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 185 | if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP)) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 186 | pr_err("%s: can't remove protocol\n", __func__); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 187 | if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6)) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 188 | pr_err("%s: can't remove protocol\n", __func__); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | module_init(tunnel6_init); |
| 192 | module_exit(tunnel6_fini); |
| 193 | MODULE_LICENSE("GPL"); |