blob: d9864725d0c6a259ba8a4d35cbe08762de5e53f4 [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 Dumazet3ff2cfa2010-08-30 10:27:10 +000033static struct xfrm6_tunnel *tunnel6_handlers __read_mostly;
34static struct xfrm6_tunnel *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{
39 struct xfrm6_tunnel **pprev;
40 int ret = -EEXIST;
41 int priority = handler->priority;
42
43 mutex_lock(&tunnel6_mutex);
44
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080045 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
46 *pprev; pprev = &(*pprev)->next) {
Herbert Xud2acc342006-03-28 01:12:13 -080047 if ((*pprev)->priority > priority)
48 break;
49 if ((*pprev)->priority == priority)
50 goto err;
51 }
52
53 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000054 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080055
56 ret = 0;
57
58err:
59 mutex_unlock(&tunnel6_mutex);
60
61 return ret;
62}
63
64EXPORT_SYMBOL(xfrm6_tunnel_register);
65
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080066int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080067{
68 struct xfrm6_tunnel **pprev;
69 int ret = -ENOENT;
70
71 mutex_lock(&tunnel6_mutex);
72
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080073 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
74 *pprev; pprev = &(*pprev)->next) {
Herbert Xud2acc342006-03-28 01:12:13 -080075 if (*pprev == handler) {
76 *pprev = handler->next;
77 ret = 0;
78 break;
79 }
80 }
81
82 mutex_unlock(&tunnel6_mutex);
83
84 synchronize_net();
85
86 return ret;
87}
88
89EXPORT_SYMBOL(xfrm6_tunnel_deregister);
90
Eric Dumazet875168a2010-08-30 11:07:25 +000091#define for_each_tunnel_rcu(head, handler) \
92 for (handler = rcu_dereference(head); \
93 handler != NULL; \
94 handler = rcu_dereference(handler->next)) \
95
Herbert Xue5bbef22007-10-15 12:50:28 -070096static int tunnel6_rcv(struct sk_buff *skb)
Herbert Xud2acc342006-03-28 01:12:13 -080097{
Herbert Xud2acc342006-03-28 01:12:13 -080098 struct xfrm6_tunnel *handler;
99
Herbert Xu50fba2a2006-04-04 13:50:45 -0700100 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
101 goto drop;
102
Eric Dumazet875168a2010-08-30 11:07:25 +0000103 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800104 if (!handler->handler(skb))
105 return 0;
106
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000107 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700108
109drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800110 kfree_skb(skb);
111 return 0;
112}
113
Herbert Xue5bbef22007-10-15 12:50:28 -0700114static int tunnel46_rcv(struct sk_buff *skb)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800115{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800116 struct xfrm6_tunnel *handler;
117
Colin82836372008-05-27 00:04:43 +0800118 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800119 goto drop;
120
Eric Dumazet875168a2010-08-30 11:07:25 +0000121 for_each_tunnel_rcu(tunnel46_handlers, handler)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800122 if (!handler->handler(skb))
123 return 0;
124
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000125 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800126
127drop:
128 kfree_skb(skb);
129 return 0;
130}
131
Herbert Xud2acc342006-03-28 01:12:13 -0800132static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700133 u8 type, u8 code, int offset, __be32 info)
Herbert Xud2acc342006-03-28 01:12:13 -0800134{
135 struct xfrm6_tunnel *handler;
136
Eric Dumazet875168a2010-08-30 11:07:25 +0000137 for_each_tunnel_rcu(tunnel6_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800138 if (!handler->err_handler(skb, opt, type, code, offset, info))
139 break;
140}
141
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000142static const struct inet6_protocol tunnel6_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800143 .handler = tunnel6_rcv,
144 .err_handler = tunnel6_err,
145 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
146};
147
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000148static const struct inet6_protocol tunnel46_protocol = {
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800149 .handler = tunnel46_rcv,
150 .err_handler = tunnel6_err,
151 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
152};
153
Herbert Xud2acc342006-03-28 01:12:13 -0800154static int __init tunnel6_init(void)
155{
156 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
157 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
158 return -EAGAIN;
159 }
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800160 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
161 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
162 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
163 return -EAGAIN;
164 }
Herbert Xud2acc342006-03-28 01:12:13 -0800165 return 0;
166}
167
168static void __exit tunnel6_fini(void)
169{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800170 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
171 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
Herbert Xud2acc342006-03-28 01:12:13 -0800172 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
173 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
174}
175
176module_init(tunnel6_init);
177module_exit(tunnel6_fini);
178MODULE_LICENSE("GPL");