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