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 #include <linux/icmpv6.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
29 #include <net/protocol.h>
32 static struct xfrm6_tunnel
*tunnel6_handlers
;
33 static struct xfrm6_tunnel
*tunnel46_handlers
;
34 static DEFINE_MUTEX(tunnel6_mutex
);
36 int xfrm6_tunnel_register(struct xfrm6_tunnel
*handler
, unsigned short family
)
38 struct xfrm6_tunnel
**pprev
;
40 int priority
= handler
->priority
;
42 mutex_lock(&tunnel6_mutex
);
44 for (pprev
= (family
== AF_INET6
) ? &tunnel6_handlers
: &tunnel46_handlers
;
45 *pprev
; pprev
= &(*pprev
)->next
) {
46 if ((*pprev
)->priority
> priority
)
48 if ((*pprev
)->priority
== priority
)
52 handler
->next
= *pprev
;
58 mutex_unlock(&tunnel6_mutex
);
63 EXPORT_SYMBOL(xfrm6_tunnel_register
);
65 int xfrm6_tunnel_deregister(struct xfrm6_tunnel
*handler
, unsigned short family
)
67 struct xfrm6_tunnel
**pprev
;
70 mutex_lock(&tunnel6_mutex
);
72 for (pprev
= (family
== AF_INET6
) ? &tunnel6_handlers
: &tunnel46_handlers
;
73 *pprev
; pprev
= &(*pprev
)->next
) {
74 if (*pprev
== handler
) {
75 *pprev
= handler
->next
;
81 mutex_unlock(&tunnel6_mutex
);
88 EXPORT_SYMBOL(xfrm6_tunnel_deregister
);
90 static int tunnel6_rcv(struct sk_buff
**pskb
)
92 struct sk_buff
*skb
= *pskb
;
93 struct xfrm6_tunnel
*handler
;
95 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
98 for (handler
= tunnel6_handlers
; handler
; handler
= handler
->next
)
99 if (!handler
->handler(skb
))
102 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0, skb
->dev
);
109 static int tunnel46_rcv(struct sk_buff
**pskb
)
111 struct sk_buff
*skb
= *pskb
;
112 struct xfrm6_tunnel
*handler
;
114 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
117 for (handler
= tunnel46_handlers
; handler
; handler
= handler
->next
)
118 if (!handler
->handler(skb
))
121 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0, skb
->dev
);
128 static void tunnel6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
129 int type
, int code
, int offset
, __be32 info
)
131 struct xfrm6_tunnel
*handler
;
133 for (handler
= tunnel6_handlers
; handler
; handler
= handler
->next
)
134 if (!handler
->err_handler(skb
, opt
, type
, code
, offset
, info
))
138 static struct inet6_protocol tunnel6_protocol
= {
139 .handler
= tunnel6_rcv
,
140 .err_handler
= tunnel6_err
,
141 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
144 static struct inet6_protocol tunnel46_protocol
= {
145 .handler
= tunnel46_rcv
,
146 .err_handler
= tunnel6_err
,
147 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
150 static int __init
tunnel6_init(void)
152 if (inet6_add_protocol(&tunnel6_protocol
, IPPROTO_IPV6
)) {
153 printk(KERN_ERR
"tunnel6 init(): can't add protocol\n");
156 if (inet6_add_protocol(&tunnel46_protocol
, IPPROTO_IPIP
)) {
157 printk(KERN_ERR
"tunnel6 init(): can't add protocol\n");
158 inet6_del_protocol(&tunnel6_protocol
, IPPROTO_IPV6
);
164 static void __exit
tunnel6_fini(void)
166 if (inet6_del_protocol(&tunnel46_protocol
, IPPROTO_IPIP
))
167 printk(KERN_ERR
"tunnel6 close: can't remove protocol\n");
168 if (inet6_del_protocol(&tunnel6_protocol
, IPPROTO_IPV6
))
169 printk(KERN_ERR
"tunnel6 close: can't remove protocol\n");
172 module_init(tunnel6_init
);
173 module_exit(tunnel6_fini
);
174 MODULE_LICENSE("GPL");