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