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 DEFINE_MUTEX(tunnel4_mutex);
19 int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
21 struct xfrm_tunnel **pprev;
23 int priority = handler->priority;
25 mutex_lock(&tunnel4_mutex);
27 for (pprev = &tunnel4_handlers; *pprev; pprev = &(*pprev)->next) {
28 if ((*pprev)->priority > priority)
30 if ((*pprev)->priority == priority)
34 handler->next = *pprev;
40 mutex_unlock(&tunnel4_mutex);
45 EXPORT_SYMBOL(xfrm4_tunnel_register);
47 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
49 struct xfrm_tunnel **pprev;
52 mutex_lock(&tunnel4_mutex);
54 for (pprev = &tunnel4_handlers; *pprev; pprev = &(*pprev)->next) {
55 if (*pprev == handler) {
56 *pprev = handler->next;
62 mutex_unlock(&tunnel4_mutex);
69 EXPORT_SYMBOL(xfrm4_tunnel_deregister);
71 static int tunnel4_rcv(struct sk_buff *skb)
73 struct xfrm_tunnel *handler;
75 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
78 for (handler = tunnel4_handlers; handler; handler = handler->next)
79 if (!handler->handler(skb))
82 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
89 static void tunnel4_err(struct sk_buff *skb, u32 info)
91 struct xfrm_tunnel *handler;
93 for (handler = tunnel4_handlers; handler; handler = handler->next)
94 if (!handler->err_handler(skb, info))
98 static struct net_protocol tunnel4_protocol = {
99 .handler = tunnel4_rcv,
100 .err_handler = tunnel4_err,
104 static int __init tunnel4_init(void)
106 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
107 printk(KERN_ERR "tunnel4 init: can't add protocol\n");
113 static void __exit tunnel4_fini(void)
115 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
116 printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
119 module_init(tunnel4_init);
120 module_exit(tunnel4_fini);
121 MODULE_LICENSE("GPL");