blob: 633ad789effc12d0000c9ec3bb2c76192bfcee6e [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>
Herbert Xu50fba2a2006-04-04 13:50:45 -070028#include <net/ipv6.h>
Herbert Xud2acc342006-03-28 01:12:13 -080029#include <net/protocol.h>
30#include <net/xfrm.h>
31
32static struct xfrm6_tunnel *tunnel6_handlers;
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080033static struct xfrm6_tunnel *tunnel46_handlers;
Herbert Xud2acc342006-03-28 01:12:13 -080034static DEFINE_MUTEX(tunnel6_mutex);
35
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080036int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080037{
38 struct xfrm6_tunnel **pprev;
39 int ret = -EEXIST;
40 int priority = handler->priority;
41
42 mutex_lock(&tunnel6_mutex);
43
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080044 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
45 *pprev; pprev = &(*pprev)->next) {
Herbert Xud2acc342006-03-28 01:12:13 -080046 if ((*pprev)->priority > priority)
47 break;
48 if ((*pprev)->priority == priority)
49 goto err;
50 }
51
52 handler->next = *pprev;
53 *pprev = handler;
54
55 ret = 0;
56
57err:
58 mutex_unlock(&tunnel6_mutex);
59
60 return ret;
61}
62
63EXPORT_SYMBOL(xfrm6_tunnel_register);
64
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080065int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080066{
67 struct xfrm6_tunnel **pprev;
68 int ret = -ENOENT;
69
70 mutex_lock(&tunnel6_mutex);
71
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -080072 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
73 *pprev; pprev = &(*pprev)->next) {
Herbert Xud2acc342006-03-28 01:12:13 -080074 if (*pprev == handler) {
75 *pprev = handler->next;
76 ret = 0;
77 break;
78 }
79 }
80
81 mutex_unlock(&tunnel6_mutex);
82
83 synchronize_net();
84
85 return ret;
86}
87
88EXPORT_SYMBOL(xfrm6_tunnel_deregister);
89
Herbert Xue5bbef22007-10-15 12:50:28 -070090static int tunnel6_rcv(struct sk_buff *skb)
Herbert Xud2acc342006-03-28 01:12:13 -080091{
Herbert Xud2acc342006-03-28 01:12:13 -080092 struct xfrm6_tunnel *handler;
93
Herbert Xu50fba2a2006-04-04 13:50:45 -070094 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
95 goto drop;
96
Herbert Xud2acc342006-03-28 01:12:13 -080097 for (handler = tunnel6_handlers; handler; handler = handler->next)
98 if (!handler->handler(skb))
99 return 0;
100
Herbert Xu50fba2a2006-04-04 13:50:45 -0700101 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, skb->dev);
102
103drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800104 kfree_skb(skb);
105 return 0;
106}
107
Herbert Xue5bbef22007-10-15 12:50:28 -0700108static int tunnel46_rcv(struct sk_buff *skb)
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800109{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800110 struct xfrm6_tunnel *handler;
111
Colin82836372008-05-27 00:04:43 +0800112 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800113 goto drop;
114
115 for (handler = tunnel46_handlers; handler; handler = handler->next)
116 if (!handler->handler(skb))
117 return 0;
118
119 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, skb->dev);
120
121drop:
122 kfree_skb(skb);
123 return 0;
124}
125
Herbert Xud2acc342006-03-28 01:12:13 -0800126static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700127 u8 type, u8 code, int offset, __be32 info)
Herbert Xud2acc342006-03-28 01:12:13 -0800128{
129 struct xfrm6_tunnel *handler;
130
131 for (handler = tunnel6_handlers; handler; handler = handler->next)
132 if (!handler->err_handler(skb, opt, type, code, offset, info))
133 break;
134}
135
136static struct inet6_protocol tunnel6_protocol = {
137 .handler = tunnel6_rcv,
138 .err_handler = tunnel6_err,
139 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
140};
141
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800142static struct inet6_protocol tunnel46_protocol = {
143 .handler = tunnel46_rcv,
144 .err_handler = tunnel6_err,
145 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
146};
147
Herbert Xud2acc342006-03-28 01:12:13 -0800148static int __init tunnel6_init(void)
149{
150 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
151 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
152 return -EAGAIN;
153 }
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800154 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
155 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
156 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
157 return -EAGAIN;
158 }
Herbert Xud2acc342006-03-28 01:12:13 -0800159 return 0;
160}
161
162static void __exit tunnel6_fini(void)
163{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800164 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
165 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
Herbert Xud2acc342006-03-28 01:12:13 -0800166 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
167 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
168}
169
170module_init(tunnel6_init);
171module_exit(tunnel6_fini);
172MODULE_LICENSE("GPL");