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 __read_mostly;
18 static struct xfrm_tunnel *tunnel64_handlers __read_mostly;
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;
42 rcu_assign_pointer(*pprev, handler);
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 #define for_each_tunnel_rcu(head, handler) \
77 for (handler = rcu_dereference(head); \
79 handler = rcu_dereference(handler->next)) \
81 static int tunnel4_rcv(struct sk_buff *skb)
83 struct xfrm_tunnel *handler;
85 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
88 for_each_tunnel_rcu(tunnel4_handlers, handler)
89 if (!handler->handler(skb))
92 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
99 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
100 static int tunnel64_rcv(struct sk_buff *skb)
102 struct xfrm_tunnel *handler;
104 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
107 for_each_tunnel_rcu(tunnel64_handlers, handler)
108 if (!handler->handler(skb))
111 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
119 static void tunnel4_err(struct sk_buff *skb, u32 info)
121 struct xfrm_tunnel *handler;
123 for_each_tunnel_rcu(tunnel4_handlers, handler)
124 if (!handler->err_handler(skb, info))
128 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129 static void tunnel64_err(struct sk_buff *skb, u32 info)
131 struct xfrm_tunnel *handler;
133 for_each_tunnel_rcu(tunnel64_handlers, handler)
134 if (!handler->err_handler(skb, info))
139 static const struct net_protocol tunnel4_protocol = {
140 .handler = tunnel4_rcv,
141 .err_handler = tunnel4_err,
146 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 static const struct net_protocol tunnel64_protocol = {
148 .handler = tunnel64_rcv,
149 .err_handler = tunnel64_err,
155 static int __init tunnel4_init(void)
157 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
158 printk(KERN_ERR "tunnel4 init: can't add protocol\n");
161 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
162 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
163 printk(KERN_ERR "tunnel64 init: can't add protocol\n");
164 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
171 static void __exit tunnel4_fini(void)
173 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
174 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
175 printk(KERN_ERR "tunnel64 close: can't remove protocol\n");
177 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
178 printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
181 module_init(tunnel4_init);
182 module_exit(tunnel4_fini);
183 MODULE_LICENSE("GPL");