blob: 4f3cec12aa8575a860f18124f0f4ee7f83cf540d [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
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
Herbert Xu50fba2a2006-04-04 13:50:45 -070022#include <linux/icmpv6.h>
Herbert Xud2acc342006-03-28 01:12:13 -080023#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/mutex.h>
26#include <linux/netdevice.h>
27#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Herbert Xu50fba2a2006-04-04 13:50:45 -070029#include <net/ipv6.h>
Herbert Xud2acc342006-03-28 01:12:13 -080030#include <net/protocol.h>
31#include <net/xfrm.h>
32
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000033static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
34static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
Herbert Xud2acc342006-03-28 01:12:13 -080035static DEFINE_MUTEX(tunnel6_mutex);
36
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080037int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080038{
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000039 struct xfrm6_tunnel __rcu **pprev;
40 struct xfrm6_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080041 int ret = -EEXIST;
42 int priority = handler->priority;
43
44 mutex_lock(&tunnel6_mutex);
45
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080046 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000047 (t = rcu_dereference_protected(*pprev,
48 lockdep_is_held(&tunnel6_mutex))) != NULL;
49 pprev = &t->next) {
50 if (t->priority > priority)
Herbert Xud2acc342006-03-28 01:12:13 -080051 break;
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000052 if (t->priority == priority)
Herbert Xud2acc342006-03-28 01:12:13 -080053 goto err;
54 }
55
56 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000057 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080058
59 ret = 0;
60
61err:
62 mutex_unlock(&tunnel6_mutex);
63
64 return ret;
65}
66
67EXPORT_SYMBOL(xfrm6_tunnel_register);
68
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080069int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080070{
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000071 struct xfrm6_tunnel __rcu **pprev;
72 struct xfrm6_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080073 int ret = -ENOENT;
74
75 mutex_lock(&tunnel6_mutex);
76
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080077 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000078 (t = rcu_dereference_protected(*pprev,
79 lockdep_is_held(&tunnel6_mutex))) != NULL;
80 pprev = &t->next) {
81 if (t == handler) {
Herbert Xud2acc342006-03-28 01:12:13 -080082 *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}
94
95EXPORT_SYMBOL(xfrm6_tunnel_deregister);
96
Eric Dumazet875168a2010-08-30 11:07:25 +000097#define for_each_tunnel_rcu(head, handler) \
98 for (handler = rcu_dereference(head); \
99 handler != NULL; \
100 handler = rcu_dereference(handler->next)) \
101
Herbert Xue5bbef22007-10-15 12:50:28 -0700102static int tunnel6_rcv(struct sk_buff *skb)
Herbert Xud2acc342006-03-28 01:12:13 -0800103{
Herbert Xud2acc342006-03-28 01:12:13 -0800104 struct xfrm6_tunnel *handler;
105
Herbert Xu50fba2a2006-04-04 13:50:45 -0700106 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
107 goto drop;
108
Eric Dumazet875168a2010-08-30 11:07:25 +0000109 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800110 if (!handler->handler(skb))
111 return 0;
112
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000113 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700114
115drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800116 kfree_skb(skb);
117 return 0;
118}
119
Herbert Xue5bbef22007-10-15 12:50:28 -0700120static int tunnel46_rcv(struct sk_buff *skb)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800121{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800122 struct xfrm6_tunnel *handler;
123
Colin82836372008-05-27 00:04:43 +0800124 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800125 goto drop;
126
Eric Dumazet875168a2010-08-30 11:07:25 +0000127 for_each_tunnel_rcu(tunnel46_handlers, handler)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800128 if (!handler->handler(skb))
129 return 0;
130
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000131 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800132
133drop:
134 kfree_skb(skb);
135 return 0;
136}
137
Herbert Xud2acc342006-03-28 01:12:13 -0800138static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700139 u8 type, u8 code, int offset, __be32 info)
Herbert Xud2acc342006-03-28 01:12:13 -0800140{
141 struct xfrm6_tunnel *handler;
142
Eric Dumazet875168a2010-08-30 11:07:25 +0000143 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800144 if (!handler->err_handler(skb, opt, type, code, offset, info))
145 break;
146}
147
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000148static const struct inet6_protocol tunnel6_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800149 .handler = tunnel6_rcv,
150 .err_handler = tunnel6_err,
151 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
152};
153
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000154static const struct inet6_protocol tunnel46_protocol = {
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800155 .handler = tunnel46_rcv,
156 .err_handler = tunnel6_err,
157 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
158};
159
Herbert Xud2acc342006-03-28 01:12:13 -0800160static int __init tunnel6_init(void)
161{
162 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
163 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
164 return -EAGAIN;
165 }
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800166 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
167 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
168 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
169 return -EAGAIN;
170 }
Herbert Xud2acc342006-03-28 01:12:13 -0800171 return 0;
172}
173
174static void __exit tunnel6_fini(void)
175{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800176 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
177 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
Herbert Xud2acc342006-03-28 01:12:13 -0800178 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
179 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
180}
181
182module_init(tunnel6_init);
183module_exit(tunnel6_fini);
184MODULE_LICENSE("GPL");