]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/tunnel6.c
x86/speculation/mds: Conditionally clear CPU buffers on idle entry
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / tunnel6.c
CommitLineData
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>
67ba4152 18 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
d2acc347
HX
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
34static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
35static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
d2acc347
HX
36static DEFINE_MUTEX(tunnel6_mutex);
37
73d605d1 38int 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
62err:
63 mutex_unlock(&tunnel6_mutex);
64
65 return ret;
66}
d2acc347
HX
67EXPORT_SYMBOL(xfrm6_tunnel_register);
68
73d605d1 69int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
d2acc347 70{
6f0bcf15
ED
71 struct xfrm6_tunnel __rcu **pprev;
72 struct xfrm6_tunnel *t;
d2acc347
HX
73 int ret = -ENOENT;
74
75 mutex_lock(&tunnel6_mutex);
76
73d605d1 77 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
6f0bcf15
ED
78 (t = rcu_dereference_protected(*pprev,
79 lockdep_is_held(&tunnel6_mutex))) != NULL;
80 pprev = &t->next) {
81 if (t == handler) {
d2acc347
HX
82 *pprev = handler->next;
83 ret = 0;
84 break;
85 }
86 }
87
88 mutex_unlock(&tunnel6_mutex);
89
90 synchronize_net();
91
92 return ret;
93}
d2acc347
HX
94EXPORT_SYMBOL(xfrm6_tunnel_deregister);
95
875168a9
ED
96#define for_each_tunnel_rcu(head, handler) \
97 for (handler = rcu_dereference(head); \
98 handler != NULL; \
99 handler = rcu_dereference(handler->next)) \
100
e5bbef20 101static int tunnel6_rcv(struct sk_buff *skb)
d2acc347 102{
d2acc347
HX
103 struct xfrm6_tunnel *handler;
104
50fba2aa
HX
105 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
106 goto drop;
107
875168a9 108 for_each_tunnel_rcu(tunnel6_handlers, handler)
d2acc347
HX
109 if (!handler->handler(skb))
110 return 0;
111
3ffe533c 112 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
50fba2aa
HX
113
114drop:
d2acc347
HX
115 kfree_skb(skb);
116 return 0;
117}
118
e5bbef20 119static int tunnel46_rcv(struct sk_buff *skb)
73d605d1 120{
73d605d1
KM
121 struct xfrm6_tunnel *handler;
122
82836372 123 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
73d605d1
KM
124 goto drop;
125
875168a9 126 for_each_tunnel_rcu(tunnel46_handlers, handler)
73d605d1
KM
127 if (!handler->handler(skb))
128 return 0;
129
3ffe533c 130 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
73d605d1
KM
131
132drop:
133 kfree_skb(skb);
134 return 0;
135}
136
d2acc347 137static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
d5fdd6ba 138 u8 type, u8 code, int offset, __be32 info)
d2acc347
HX
139{
140 struct xfrm6_tunnel *handler;
141
875168a9 142 for_each_tunnel_rcu(tunnel6_handlers, handler)
d2acc347
HX
143 if (!handler->err_handler(skb, opt, type, code, offset, info))
144 break;
145}
146
ebac62fe
MK
147static void tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
148 u8 type, u8 code, int offset, __be32 info)
149{
150 struct xfrm6_tunnel *handler;
151
152 for_each_tunnel_rcu(tunnel46_handlers, handler)
153 if (!handler->err_handler(skb, opt, type, code, offset, info))
154 break;
155}
156
41135cc8 157static const struct inet6_protocol tunnel6_protocol = {
d2acc347
HX
158 .handler = tunnel6_rcv,
159 .err_handler = tunnel6_err,
160 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
161};
162
41135cc8 163static const struct inet6_protocol tunnel46_protocol = {
73d605d1 164 .handler = tunnel46_rcv,
ebac62fe 165 .err_handler = tunnel46_err,
73d605d1
KM
166 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
167};
168
d2acc347
HX
169static int __init tunnel6_init(void)
170{
171 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
f3213831 172 pr_err("%s: can't add protocol\n", __func__);
d2acc347
HX
173 return -EAGAIN;
174 }
73d605d1 175 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
f3213831 176 pr_err("%s: can't add protocol\n", __func__);
73d605d1
KM
177 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
178 return -EAGAIN;
179 }
d2acc347
HX
180 return 0;
181}
182
183static void __exit tunnel6_fini(void)
184{
73d605d1 185 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
f3213831 186 pr_err("%s: can't remove protocol\n", __func__);
d2acc347 187 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
f3213831 188 pr_err("%s: can't remove protocol\n", __func__);
d2acc347
HX
189}
190
191module_init(tunnel6_init);
192module_exit(tunnel6_fini);
193MODULE_LICENSE("GPL");