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>
11 #include <linux/slab.h>
14 #include <net/protocol.h>
17 static struct xfrm_tunnel *tunnel4_handlers;
18 static struct xfrm_tunnel *tunnel64_handlers;
19 static DEFINE_MUTEX(tunnel4_mutex);
21 static inline struct xfrm_tunnel **fam_handlers(unsigned short family)
23 return (family == AF_INET) ? &tunnel4_handlers : &tunnel64_handlers;
26 int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
28 struct xfrm_tunnel **pprev;
30 int priority = handler->priority;
32 mutex_lock(&tunnel4_mutex);
34 for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
35 if ((*pprev)->priority > priority)
37 if ((*pprev)->priority == priority)
41 handler->next = *pprev;
47 mutex_unlock(&tunnel4_mutex);
51 EXPORT_SYMBOL(xfrm4_tunnel_register);
53 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
55 struct xfrm_tunnel **pprev;
58 mutex_lock(&tunnel4_mutex);
60 for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
61 if (*pprev == handler) {
62 *pprev = handler->next;
68 mutex_unlock(&tunnel4_mutex);
74 EXPORT_SYMBOL(xfrm4_tunnel_deregister);
76 static int tunnel4_rcv(struct sk_buff *skb)
78 struct xfrm_tunnel *handler;
80 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
83 for (handler = tunnel4_handlers; handler; handler = handler->next)
84 if (!handler->handler(skb))
87 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
94 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
95 static int tunnel64_rcv(struct sk_buff *skb)
97 struct xfrm_tunnel *handler;
99 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
102 for (handler = tunnel64_handlers; handler; handler = handler->next)
103 if (!handler->handler(skb))
106 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
114 static void tunnel4_err(struct sk_buff *skb, u32 info)
116 struct xfrm_tunnel *handler;
118 for (handler = tunnel4_handlers; handler; handler = handler->next)
119 if (!handler->err_handler(skb, info))
123 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
124 static void tunnel64_err(struct sk_buff *skb, u32 info)
126 struct xfrm_tunnel *handler;
128 for (handler = tunnel64_handlers; handler; handler = handler->next)
129 if (!handler->err_handler(skb, info))
134 static const struct net_protocol tunnel4_protocol = {
135 .handler = tunnel4_rcv,
136 .err_handler = tunnel4_err,
141 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
142 static const struct net_protocol tunnel64_protocol = {
143 .handler = tunnel64_rcv,
144 .err_handler = tunnel64_err,
150 static int __init tunnel4_init(void)
152 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
153 printk(KERN_ERR "tunnel4 init: can't add protocol\n");
156 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
157 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
158 printk(KERN_ERR "tunnel64 init: can't add protocol\n");
159 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
166 static void __exit tunnel4_fini(void)
168 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
169 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
170 printk(KERN_ERR "tunnel64 close: can't remove protocol\n");
172 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
173 printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
176 module_init(tunnel4_init);
177 module_exit(tunnel4_fini);
178 MODULE_LICENSE("GPL");