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 __rcu
*tunnel4_handlers __read_mostly
;
18 static struct xfrm_tunnel __rcu
*tunnel64_handlers __read_mostly
;
19 static DEFINE_MUTEX(tunnel4_mutex
);
21 static inline struct xfrm_tunnel __rcu
**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 __rcu
**pprev
;
29 struct xfrm_tunnel
*t
;
32 int priority
= handler
->priority
;
34 mutex_lock(&tunnel4_mutex
);
36 for (pprev
= fam_handlers(family
);
37 (t
= rcu_dereference_protected(*pprev
,
38 lockdep_is_held(&tunnel4_mutex
))) != NULL
;
40 if (t
->priority
> priority
)
42 if (t
->priority
== priority
)
46 handler
->next
= *pprev
;
47 rcu_assign_pointer(*pprev
, handler
);
52 mutex_unlock(&tunnel4_mutex
);
56 EXPORT_SYMBOL(xfrm4_tunnel_register
);
58 int xfrm4_tunnel_deregister(struct xfrm_tunnel
*handler
, unsigned short family
)
60 struct xfrm_tunnel __rcu
**pprev
;
61 struct xfrm_tunnel
*t
;
64 mutex_lock(&tunnel4_mutex
);
66 for (pprev
= fam_handlers(family
);
67 (t
= rcu_dereference_protected(*pprev
,
68 lockdep_is_held(&tunnel4_mutex
))) != NULL
;
71 *pprev
= handler
->next
;
77 mutex_unlock(&tunnel4_mutex
);
83 EXPORT_SYMBOL(xfrm4_tunnel_deregister
);
85 #define for_each_tunnel_rcu(head, handler) \
86 for (handler = rcu_dereference(head); \
88 handler = rcu_dereference(handler->next)) \
90 static int tunnel4_rcv(struct sk_buff
*skb
)
92 struct xfrm_tunnel
*handler
;
94 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
97 for_each_tunnel_rcu(tunnel4_handlers
, handler
)
98 if (!handler
->handler(skb
))
101 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
108 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
109 static int tunnel64_rcv(struct sk_buff
*skb
)
111 struct xfrm_tunnel
*handler
;
113 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
116 for_each_tunnel_rcu(tunnel64_handlers
, handler
)
117 if (!handler
->handler(skb
))
120 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
128 static void tunnel4_err(struct sk_buff
*skb
, u32 info
)
130 struct xfrm_tunnel
*handler
;
132 for_each_tunnel_rcu(tunnel4_handlers
, handler
)
133 if (!handler
->err_handler(skb
, info
))
137 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
138 static void tunnel64_err(struct sk_buff
*skb
, u32 info
)
140 struct xfrm_tunnel
*handler
;
142 for_each_tunnel_rcu(tunnel64_handlers
, handler
)
143 if (!handler
->err_handler(skb
, info
))
148 static const struct net_protocol tunnel4_protocol
= {
149 .handler
= tunnel4_rcv
,
150 .err_handler
= tunnel4_err
,
155 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
156 static const struct net_protocol tunnel64_protocol
= {
157 .handler
= tunnel64_rcv
,
158 .err_handler
= tunnel64_err
,
164 static int __init
tunnel4_init(void)
166 if (inet_add_protocol(&tunnel4_protocol
, IPPROTO_IPIP
)) {
167 printk(KERN_ERR
"tunnel4 init: can't add protocol\n");
170 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 if (inet_add_protocol(&tunnel64_protocol
, IPPROTO_IPV6
)) {
172 printk(KERN_ERR
"tunnel64 init: can't add protocol\n");
173 inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
);
180 static void __exit
tunnel4_fini(void)
182 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
183 if (inet_del_protocol(&tunnel64_protocol
, IPPROTO_IPV6
))
184 printk(KERN_ERR
"tunnel64 close: can't remove protocol\n");
186 if (inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
))
187 printk(KERN_ERR
"tunnel4 close: can't remove protocol\n");
190 module_init(tunnel4_init
);
191 module_exit(tunnel4_fini
);
192 MODULE_LICENSE("GPL");