Commit | Line | Data |
---|---|---|
d2acc347 HX |
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 | ||
50fba2aa | 22 | #include <linux/icmpv6.h> |
d2acc347 HX |
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> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
50fba2aa | 29 | #include <net/ipv6.h> |
d2acc347 HX |
30 | #include <net/protocol.h> |
31 | #include <net/xfrm.h> | |
32 | ||
3ff2cfa5 ED |
33 | static struct xfrm6_tunnel *tunnel6_handlers __read_mostly; |
34 | static struct xfrm6_tunnel *tunnel46_handlers __read_mostly; | |
d2acc347 HX |
35 | static DEFINE_MUTEX(tunnel6_mutex); |
36 | ||
73d605d1 | 37 | int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family) |
d2acc347 HX |
38 | { |
39 | struct xfrm6_tunnel **pprev; | |
40 | int ret = -EEXIST; | |
41 | int priority = handler->priority; | |
42 | ||
43 | mutex_lock(&tunnel6_mutex); | |
44 | ||
73d605d1 KM |
45 | for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers; |
46 | *pprev; pprev = &(*pprev)->next) { | |
d2acc347 HX |
47 | if ((*pprev)->priority > priority) |
48 | break; | |
49 | if ((*pprev)->priority == priority) | |
50 | goto err; | |
51 | } | |
52 | ||
53 | handler->next = *pprev; | |
54 | *pprev = handler; | |
55 | ||
56 | ret = 0; | |
57 | ||
58 | err: | |
59 | mutex_unlock(&tunnel6_mutex); | |
60 | ||
61 | return ret; | |
62 | } | |
63 | ||
64 | EXPORT_SYMBOL(xfrm6_tunnel_register); | |
65 | ||
73d605d1 | 66 | int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family) |
d2acc347 HX |
67 | { |
68 | struct xfrm6_tunnel **pprev; | |
69 | int ret = -ENOENT; | |
70 | ||
71 | mutex_lock(&tunnel6_mutex); | |
72 | ||
73d605d1 KM |
73 | for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers; |
74 | *pprev; pprev = &(*pprev)->next) { | |
d2acc347 HX |
75 | if (*pprev == handler) { |
76 | *pprev = handler->next; | |
77 | ret = 0; | |
78 | break; | |
79 | } | |
80 | } | |
81 | ||
82 | mutex_unlock(&tunnel6_mutex); | |
83 | ||
84 | synchronize_net(); | |
85 | ||
86 | return ret; | |
87 | } | |
88 | ||
89 | EXPORT_SYMBOL(xfrm6_tunnel_deregister); | |
90 | ||
875168a9 ED |
91 | #define for_each_tunnel_rcu(head, handler) \ |
92 | for (handler = rcu_dereference(head); \ | |
93 | handler != NULL; \ | |
94 | handler = rcu_dereference(handler->next)) \ | |
95 | ||
e5bbef20 | 96 | static int tunnel6_rcv(struct sk_buff *skb) |
d2acc347 | 97 | { |
d2acc347 HX |
98 | struct xfrm6_tunnel *handler; |
99 | ||
50fba2aa HX |
100 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
101 | goto drop; | |
102 | ||
875168a9 | 103 | for_each_tunnel_rcu(tunnel6_handlers, handler) |
d2acc347 HX |
104 | if (!handler->handler(skb)) |
105 | return 0; | |
106 | ||
3ffe533c | 107 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
50fba2aa HX |
108 | |
109 | drop: | |
d2acc347 HX |
110 | kfree_skb(skb); |
111 | return 0; | |
112 | } | |
113 | ||
e5bbef20 | 114 | static int tunnel46_rcv(struct sk_buff *skb) |
73d605d1 | 115 | { |
73d605d1 KM |
116 | struct xfrm6_tunnel *handler; |
117 | ||
82836372 | 118 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
73d605d1 KM |
119 | goto drop; |
120 | ||
875168a9 | 121 | for_each_tunnel_rcu(tunnel46_handlers, handler) |
73d605d1 KM |
122 | if (!handler->handler(skb)) |
123 | return 0; | |
124 | ||
3ffe533c | 125 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
73d605d1 KM |
126 | |
127 | drop: | |
128 | kfree_skb(skb); | |
129 | return 0; | |
130 | } | |
131 | ||
d2acc347 | 132 | static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
d5fdd6ba | 133 | u8 type, u8 code, int offset, __be32 info) |
d2acc347 HX |
134 | { |
135 | struct xfrm6_tunnel *handler; | |
136 | ||
875168a9 | 137 | for_each_tunnel_rcu(tunnel6_handlers, handler) |
d2acc347 HX |
138 | if (!handler->err_handler(skb, opt, type, code, offset, info)) |
139 | break; | |
140 | } | |
141 | ||
41135cc8 | 142 | static const struct inet6_protocol tunnel6_protocol = { |
d2acc347 HX |
143 | .handler = tunnel6_rcv, |
144 | .err_handler = tunnel6_err, | |
145 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | |
146 | }; | |
147 | ||
41135cc8 | 148 | static const struct inet6_protocol tunnel46_protocol = { |
73d605d1 KM |
149 | .handler = tunnel46_rcv, |
150 | .err_handler = tunnel6_err, | |
151 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | |
152 | }; | |
153 | ||
d2acc347 HX |
154 | static int __init tunnel6_init(void) |
155 | { | |
156 | if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) { | |
157 | printk(KERN_ERR "tunnel6 init(): can't add protocol\n"); | |
158 | return -EAGAIN; | |
159 | } | |
73d605d1 KM |
160 | if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) { |
161 | printk(KERN_ERR "tunnel6 init(): can't add protocol\n"); | |
162 | inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6); | |
163 | return -EAGAIN; | |
164 | } | |
d2acc347 HX |
165 | return 0; |
166 | } | |
167 | ||
168 | static void __exit tunnel6_fini(void) | |
169 | { | |
73d605d1 KM |
170 | if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP)) |
171 | printk(KERN_ERR "tunnel6 close: can't remove protocol\n"); | |
d2acc347 HX |
172 | if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6)) |
173 | printk(KERN_ERR "tunnel6 close: can't remove protocol\n"); | |
174 | } | |
175 | ||
176 | module_init(tunnel6_init); | |
177 | module_exit(tunnel6_fini); | |
178 | MODULE_LICENSE("GPL"); |