blob: 2c4e4c5c7614bf9ee864a99e095c7c17e46d9b57 [file] [log] [blame]
Herbert Xud2acc342006-03-28 01:12:13 -08001/*
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 Kirshera99421d2013-12-06 09:13:39 -080015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Herbert Xud2acc342006-03-28 01:12:13 -080016 *
17 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
18 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
19 */
20
Joe Perchesf3213832012-05-15 14:11:53 +000021#define pr_fmt(fmt) "IPv6: " fmt
22
Herbert Xu50fba2a2006-04-04 13:50:45 -070023#include <linux/icmpv6.h>
Herbert Xud2acc342006-03-28 01:12:13 -080024#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 Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Herbert Xu50fba2a2006-04-04 13:50:45 -070030#include <net/ipv6.h>
Herbert Xud2acc342006-03-28 01:12:13 -080031#include <net/protocol.h>
32#include <net/xfrm.h>
33
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000034static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
35static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
Herbert Xud2acc342006-03-28 01:12:13 -080036static DEFINE_MUTEX(tunnel6_mutex);
37
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080038int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080039{
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000040 struct xfrm6_tunnel __rcu **pprev;
41 struct xfrm6_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080042 int ret = -EEXIST;
43 int priority = handler->priority;
44
45 mutex_lock(&tunnel6_mutex);
46
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080047 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000048 (t = rcu_dereference_protected(*pprev,
49 lockdep_is_held(&tunnel6_mutex))) != NULL;
50 pprev = &t->next) {
51 if (t->priority > priority)
Herbert Xud2acc342006-03-28 01:12:13 -080052 break;
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000053 if (t->priority == priority)
Herbert Xud2acc342006-03-28 01:12:13 -080054 goto err;
55 }
56
57 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000058 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080059
60 ret = 0;
61
62err:
63 mutex_unlock(&tunnel6_mutex);
64
65 return ret;
66}
67
68EXPORT_SYMBOL(xfrm6_tunnel_register);
69
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080070int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080071{
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000072 struct xfrm6_tunnel __rcu **pprev;
73 struct xfrm6_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080074 int ret = -ENOENT;
75
76 mutex_lock(&tunnel6_mutex);
77
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080078 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000079 (t = rcu_dereference_protected(*pprev,
80 lockdep_is_held(&tunnel6_mutex))) != NULL;
81 pprev = &t->next) {
82 if (t == handler) {
Herbert Xud2acc342006-03-28 01:12:13 -080083 *pprev = handler->next;
84 ret = 0;
85 break;
86 }
87 }
88
89 mutex_unlock(&tunnel6_mutex);
90
91 synchronize_net();
92
93 return ret;
94}
95
96EXPORT_SYMBOL(xfrm6_tunnel_deregister);
97
Eric Dumazet875168a2010-08-30 11:07:25 +000098#define for_each_tunnel_rcu(head, handler) \
99 for (handler = rcu_dereference(head); \
100 handler != NULL; \
101 handler = rcu_dereference(handler->next)) \
102
Herbert Xue5bbef22007-10-15 12:50:28 -0700103static int tunnel6_rcv(struct sk_buff *skb)
Herbert Xud2acc342006-03-28 01:12:13 -0800104{
Herbert Xud2acc342006-03-28 01:12:13 -0800105 struct xfrm6_tunnel *handler;
106
Herbert Xu50fba2a2006-04-04 13:50:45 -0700107 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
108 goto drop;
109
Eric Dumazet875168a2010-08-30 11:07:25 +0000110 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800111 if (!handler->handler(skb))
112 return 0;
113
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000114 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700115
116drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800117 kfree_skb(skb);
118 return 0;
119}
120
Herbert Xue5bbef22007-10-15 12:50:28 -0700121static int tunnel46_rcv(struct sk_buff *skb)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800122{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800123 struct xfrm6_tunnel *handler;
124
Colin82836372008-05-27 00:04:43 +0800125 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800126 goto drop;
127
Eric Dumazet875168a2010-08-30 11:07:25 +0000128 for_each_tunnel_rcu(tunnel46_handlers, handler)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800129 if (!handler->handler(skb))
130 return 0;
131
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000132 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800133
134drop:
135 kfree_skb(skb);
136 return 0;
137}
138
Herbert Xud2acc342006-03-28 01:12:13 -0800139static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700140 u8 type, u8 code, int offset, __be32 info)
Herbert Xud2acc342006-03-28 01:12:13 -0800141{
142 struct xfrm6_tunnel *handler;
143
Eric Dumazet875168a2010-08-30 11:07:25 +0000144 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800145 if (!handler->err_handler(skb, opt, type, code, offset, info))
146 break;
147}
148
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000149static const struct inet6_protocol tunnel6_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800150 .handler = tunnel6_rcv,
151 .err_handler = tunnel6_err,
152 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
153};
154
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000155static const struct inet6_protocol tunnel46_protocol = {
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800156 .handler = tunnel46_rcv,
157 .err_handler = tunnel6_err,
158 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
159};
160
Herbert Xud2acc342006-03-28 01:12:13 -0800161static int __init tunnel6_init(void)
162{
163 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000164 pr_err("%s: can't add protocol\n", __func__);
Herbert Xud2acc342006-03-28 01:12:13 -0800165 return -EAGAIN;
166 }
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800167 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000168 pr_err("%s: can't add protocol\n", __func__);
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800169 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
170 return -EAGAIN;
171 }
Herbert Xud2acc342006-03-28 01:12:13 -0800172 return 0;
173}
174
175static void __exit tunnel6_fini(void)
176{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800177 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
Joe Perchesf3213832012-05-15 14:11:53 +0000178 pr_err("%s: can't remove protocol\n", __func__);
Herbert Xud2acc342006-03-28 01:12:13 -0800179 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
Joe Perchesf3213832012-05-15 14:11:53 +0000180 pr_err("%s: can't remove protocol\n", __func__);
Herbert Xud2acc342006-03-28 01:12:13 -0800181}
182
183module_init(tunnel6_init);
184module_exit(tunnel6_fini);
185MODULE_LICENSE("GPL");