2 * Copyright (C)2003,2004 USAGI/WIDE Project
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.
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.
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
18 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
19 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
22 #define pr_fmt(fmt) "IPv6: " fmt
24 #include <linux/icmpv6.h>
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>
30 #include <linux/slab.h>
32 #include <net/protocol.h>
35 static struct xfrm6_tunnel __rcu
*tunnel6_handlers __read_mostly
;
36 static struct xfrm6_tunnel __rcu
*tunnel46_handlers __read_mostly
;
37 static DEFINE_MUTEX(tunnel6_mutex
);
39 int xfrm6_tunnel_register(struct xfrm6_tunnel
*handler
, unsigned short family
)
41 struct xfrm6_tunnel __rcu
**pprev
;
42 struct xfrm6_tunnel
*t
;
44 int priority
= handler
->priority
;
46 mutex_lock(&tunnel6_mutex
);
48 for (pprev
= (family
== AF_INET6
) ? &tunnel6_handlers
: &tunnel46_handlers
;
49 (t
= rcu_dereference_protected(*pprev
,
50 lockdep_is_held(&tunnel6_mutex
))) != NULL
;
52 if (t
->priority
> priority
)
54 if (t
->priority
== priority
)
58 handler
->next
= *pprev
;
59 rcu_assign_pointer(*pprev
, handler
);
64 mutex_unlock(&tunnel6_mutex
);
69 EXPORT_SYMBOL(xfrm6_tunnel_register
);
71 int xfrm6_tunnel_deregister(struct xfrm6_tunnel
*handler
, unsigned short family
)
73 struct xfrm6_tunnel __rcu
**pprev
;
74 struct xfrm6_tunnel
*t
;
77 mutex_lock(&tunnel6_mutex
);
79 for (pprev
= (family
== AF_INET6
) ? &tunnel6_handlers
: &tunnel46_handlers
;
80 (t
= rcu_dereference_protected(*pprev
,
81 lockdep_is_held(&tunnel6_mutex
))) != NULL
;
84 *pprev
= handler
->next
;
90 mutex_unlock(&tunnel6_mutex
);
97 EXPORT_SYMBOL(xfrm6_tunnel_deregister
);
99 #define for_each_tunnel_rcu(head, handler) \
100 for (handler = rcu_dereference(head); \
102 handler = rcu_dereference(handler->next)) \
104 static int tunnel6_rcv(struct sk_buff *skb)
106 struct xfrm6_tunnel
*handler
;
108 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
111 for_each_tunnel_rcu(tunnel6_handlers
, handler
)
112 if (!handler
->handler(skb
))
115 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
122 static int tunnel46_rcv(struct sk_buff
*skb
)
124 struct xfrm6_tunnel
*handler
;
126 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
129 for_each_tunnel_rcu(tunnel46_handlers
, handler
)
130 if (!handler
->handler(skb
))
133 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
140 static void tunnel6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
141 u8 type
, u8 code
, int offset
, __be32 info
)
143 struct xfrm6_tunnel
*handler
;
145 for_each_tunnel_rcu(tunnel6_handlers
, handler
)
146 if (!handler
->err_handler(skb
, opt
, type
, code
, offset
, info
))
150 static const struct inet6_protocol tunnel6_protocol
= {
151 .handler
= tunnel6_rcv
,
152 .err_handler
= tunnel6_err
,
153 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
156 static const struct inet6_protocol tunnel46_protocol
= {
157 .handler
= tunnel46_rcv
,
158 .err_handler
= tunnel6_err
,
159 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
162 static int __init
tunnel6_init(void)
164 if (inet6_add_protocol(&tunnel6_protocol
, IPPROTO_IPV6
)) {
165 pr_err("%s: can't add protocol\n", __func__
);
168 if (inet6_add_protocol(&tunnel46_protocol
, IPPROTO_IPIP
)) {
169 pr_err("%s: can't add protocol\n", __func__
);
170 inet6_del_protocol(&tunnel6_protocol
, IPPROTO_IPV6
);
176 static void __exit
tunnel6_fini(void)
178 if (inet6_del_protocol(&tunnel46_protocol
, IPPROTO_IPIP
))
179 pr_err("%s: can't remove protocol\n", __func__
);
180 if (inet6_del_protocol(&tunnel6_protocol
, IPPROTO_IPV6
))
181 pr_err("%s: can't remove protocol\n", __func__
);
184 module_init(tunnel6_init
);
185 module_exit(tunnel6_fini
);
186 MODULE_LICENSE("GPL");