Merge branch 'master'
[pandora-kernel.git] / net / ipv6 / tunnel6.c
1 /*
2  * Copyright (C)2003,2004 USAGI/WIDE Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Authors      Mitsuru KANDA  <mk@linux-ipv6.org>
19  *              YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
20  */
21
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <net/protocol.h>
28 #include <net/xfrm.h>
29
30 static struct xfrm6_tunnel *tunnel6_handlers;
31 static DEFINE_MUTEX(tunnel6_mutex);
32
33 int xfrm6_tunnel_register(struct xfrm6_tunnel *handler)
34 {
35         struct xfrm6_tunnel **pprev;
36         int ret = -EEXIST;
37         int priority = handler->priority;
38
39         mutex_lock(&tunnel6_mutex);
40
41         for (pprev = &tunnel6_handlers; *pprev; pprev = &(*pprev)->next) {
42                 if ((*pprev)->priority > priority)
43                         break;
44                 if ((*pprev)->priority == priority)
45                         goto err;
46         }
47
48         handler->next = *pprev;
49         *pprev = handler;
50
51         ret = 0;
52
53 err:
54         mutex_unlock(&tunnel6_mutex);
55
56         return ret;
57 }
58
59 EXPORT_SYMBOL(xfrm6_tunnel_register);
60
61 int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler)
62 {
63         struct xfrm6_tunnel **pprev;
64         int ret = -ENOENT;
65
66         mutex_lock(&tunnel6_mutex);
67
68         for (pprev = &tunnel6_handlers; *pprev; pprev = &(*pprev)->next) {
69                 if (*pprev == handler) {
70                         *pprev = handler->next;
71                         ret = 0;
72                         break;
73                 }
74         }
75
76         mutex_unlock(&tunnel6_mutex);
77
78         synchronize_net();
79
80         return ret;
81 }
82
83 EXPORT_SYMBOL(xfrm6_tunnel_deregister);
84
85 static int tunnel6_rcv(struct sk_buff **pskb)
86 {
87         struct sk_buff *skb = *pskb;
88         struct xfrm6_tunnel *handler;
89
90         for (handler = tunnel6_handlers; handler; handler = handler->next)
91                 if (!handler->handler(skb))
92                         return 0;
93
94         kfree_skb(skb);
95         return 0;
96 }
97
98 static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
99                         int type, int code, int offset, __u32 info)
100 {
101         struct xfrm6_tunnel *handler;
102
103         for (handler = tunnel6_handlers; handler; handler = handler->next)
104                 if (!handler->err_handler(skb, opt, type, code, offset, info))
105                         break;
106 }
107
108 static struct inet6_protocol tunnel6_protocol = {
109         .handler        = tunnel6_rcv,
110         .err_handler    = tunnel6_err,
111         .flags          = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
112 };
113
114 static int __init tunnel6_init(void)
115 {
116         if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
117                 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
118                 return -EAGAIN;
119         }
120         return 0;
121 }
122
123 static void __exit tunnel6_fini(void)
124 {
125         if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
126                 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
127 }
128
129 module_init(tunnel6_init);
130 module_exit(tunnel6_fini);
131 MODULE_LICENSE("GPL");