Merge branch 'misc' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc...
[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/icmpv6.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <net/ipv6.h>
29 #include <net/protocol.h>
30 #include <net/xfrm.h>
31
32 static struct xfrm6_tunnel *tunnel6_handlers;
33 static DEFINE_MUTEX(tunnel6_mutex);
34
35 int xfrm6_tunnel_register(struct xfrm6_tunnel *handler)
36 {
37         struct xfrm6_tunnel **pprev;
38         int ret = -EEXIST;
39         int priority = handler->priority;
40
41         mutex_lock(&tunnel6_mutex);
42
43         for (pprev = &tunnel6_handlers; *pprev; pprev = &(*pprev)->next) {
44                 if ((*pprev)->priority > priority)
45                         break;
46                 if ((*pprev)->priority == priority)
47                         goto err;
48         }
49
50         handler->next = *pprev;
51         *pprev = handler;
52
53         ret = 0;
54
55 err:
56         mutex_unlock(&tunnel6_mutex);
57
58         return ret;
59 }
60
61 EXPORT_SYMBOL(xfrm6_tunnel_register);
62
63 int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler)
64 {
65         struct xfrm6_tunnel **pprev;
66         int ret = -ENOENT;
67
68         mutex_lock(&tunnel6_mutex);
69
70         for (pprev = &tunnel6_handlers; *pprev; pprev = &(*pprev)->next) {
71                 if (*pprev == handler) {
72                         *pprev = handler->next;
73                         ret = 0;
74                         break;
75                 }
76         }
77
78         mutex_unlock(&tunnel6_mutex);
79
80         synchronize_net();
81
82         return ret;
83 }
84
85 EXPORT_SYMBOL(xfrm6_tunnel_deregister);
86
87 static int tunnel6_rcv(struct sk_buff **pskb)
88 {
89         struct sk_buff *skb = *pskb;
90         struct xfrm6_tunnel *handler;
91
92         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
93                 goto drop;
94
95         for (handler = tunnel6_handlers; handler; handler = handler->next)
96                 if (!handler->handler(skb))
97                         return 0;
98
99         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, skb->dev);
100
101 drop:
102         kfree_skb(skb);
103         return 0;
104 }
105
106 static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
107                         int type, int code, int offset, __u32 info)
108 {
109         struct xfrm6_tunnel *handler;
110
111         for (handler = tunnel6_handlers; handler; handler = handler->next)
112                 if (!handler->err_handler(skb, opt, type, code, offset, info))
113                         break;
114 }
115
116 static struct inet6_protocol tunnel6_protocol = {
117         .handler        = tunnel6_rcv,
118         .err_handler    = tunnel6_err,
119         .flags          = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
120 };
121
122 static int __init tunnel6_init(void)
123 {
124         if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
125                 printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
126                 return -EAGAIN;
127         }
128         return 0;
129 }
130
131 static void __exit tunnel6_fini(void)
132 {
133         if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
134                 printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
135 }
136
137 module_init(tunnel6_init);
138 module_exit(tunnel6_fini);
139 MODULE_LICENSE("GPL");