1 /* tunnel4.c: Generic IP tunnel transformer.
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
13 #include <net/protocol.h>
16 static struct xfrm_tunnel
*tunnel4_handlers
;
17 static struct xfrm_tunnel
*tunnel64_handlers
;
18 static DEFINE_MUTEX(tunnel4_mutex
);
20 int xfrm4_tunnel_register(struct xfrm_tunnel
*handler
, unsigned short family
)
22 struct xfrm_tunnel
**pprev
;
24 int priority
= handler
->priority
;
26 mutex_lock(&tunnel4_mutex
);
28 for (pprev
= (family
== AF_INET
) ? &tunnel4_handlers
: &tunnel64_handlers
;
29 *pprev
; pprev
= &(*pprev
)->next
) {
30 if ((*pprev
)->priority
> priority
)
32 if ((*pprev
)->priority
== priority
)
36 handler
->next
= *pprev
;
42 mutex_unlock(&tunnel4_mutex
);
47 EXPORT_SYMBOL(xfrm4_tunnel_register
);
49 int xfrm4_tunnel_deregister(struct xfrm_tunnel
*handler
, unsigned short family
)
51 struct xfrm_tunnel
**pprev
;
54 mutex_lock(&tunnel4_mutex
);
56 for (pprev
= (family
== AF_INET
) ? &tunnel4_handlers
: &tunnel64_handlers
;
57 *pprev
; pprev
= &(*pprev
)->next
) {
58 if (*pprev
== handler
) {
59 *pprev
= handler
->next
;
65 mutex_unlock(&tunnel4_mutex
);
72 EXPORT_SYMBOL(xfrm4_tunnel_deregister
);
74 static int tunnel4_rcv(struct sk_buff
*skb
)
76 struct xfrm_tunnel
*handler
;
78 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
81 for (handler
= tunnel4_handlers
; handler
; handler
= handler
->next
)
82 if (!handler
->handler(skb
))
85 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
92 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
93 static int tunnel64_rcv(struct sk_buff
*skb
)
95 struct xfrm_tunnel
*handler
;
97 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
100 for (handler
= tunnel64_handlers
; handler
; handler
= handler
->next
)
101 if (!handler
->handler(skb
))
104 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
112 static void tunnel4_err(struct sk_buff
*skb
, u32 info
)
114 struct xfrm_tunnel
*handler
;
116 for (handler
= tunnel4_handlers
; handler
; handler
= handler
->next
)
117 if (!handler
->err_handler(skb
, info
))
121 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
122 static void tunnel64_err(struct sk_buff
*skb
, u32 info
)
124 struct xfrm_tunnel
*handler
;
126 for (handler
= tunnel64_handlers
; handler
; handler
= handler
->next
)
127 if (!handler
->err_handler(skb
, info
))
132 static struct net_protocol tunnel4_protocol
= {
133 .handler
= tunnel4_rcv
,
134 .err_handler
= tunnel4_err
,
138 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
139 static struct net_protocol tunnel64_protocol
= {
140 .handler
= tunnel64_rcv
,
141 .err_handler
= tunnel64_err
,
146 static int __init
tunnel4_init(void)
148 if (inet_add_protocol(&tunnel4_protocol
, IPPROTO_IPIP
)) {
149 printk(KERN_ERR
"tunnel4 init: can't add protocol\n");
152 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
153 if (inet_add_protocol(&tunnel64_protocol
, IPPROTO_IPV6
)) {
154 printk(KERN_ERR
"tunnel64 init: can't add protocol\n");
155 inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
);
162 static void __exit
tunnel4_fini(void)
164 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
165 if (inet_del_protocol(&tunnel64_protocol
, IPPROTO_IPV6
))
166 printk(KERN_ERR
"tunnel64 close: can't remove protocol\n");
168 if (inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
))
169 printk(KERN_ERR
"tunnel4 close: can't remove protocol\n");
172 module_init(tunnel4_init
);
173 module_exit(tunnel4_fini
);
174 MODULE_LICENSE("GPL");