]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/ipv4/tunnel4.c
Merge tag 'for-5.4/block-2019-09-16' of git://git.kernel.dk/linux-block
[mirror_ubuntu-hirsute-kernel.git] / net / ipv4 / tunnel4.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
d2acc347
HX
2/* tunnel4.c: Generic IP tunnel transformer.
3 *
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
5 */
6
7#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/mutex.h>
8afe97e5 10#include <linux/mpls.h>
d2acc347
HX
11#include <linux/netdevice.h>
12#include <linux/skbuff.h>
5a0e3ad6 13#include <linux/slab.h>
50fba2aa
HX
14#include <net/icmp.h>
15#include <net/ip.h>
d2acc347
HX
16#include <net/protocol.h>
17#include <net/xfrm.h>
18
b33eab08
ED
19static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
20static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
8afe97e5 21static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly;
d2acc347
HX
22static DEFINE_MUTEX(tunnel4_mutex);
23
b33eab08 24static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
358352b8 25{
8afe97e5
SH
26 return (family == AF_INET) ? &tunnel4_handlers :
27 (family == AF_INET6) ? &tunnel64_handlers :
28 &tunnelmpls4_handlers;
358352b8
PE
29}
30
c0d56408 31int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
d2acc347 32{
b33eab08
ED
33 struct xfrm_tunnel __rcu **pprev;
34 struct xfrm_tunnel *t;
35
d2acc347
HX
36 int ret = -EEXIST;
37 int priority = handler->priority;
38
39 mutex_lock(&tunnel4_mutex);
40
b33eab08
ED
41 for (pprev = fam_handlers(family);
42 (t = rcu_dereference_protected(*pprev,
43 lockdep_is_held(&tunnel4_mutex))) != NULL;
44 pprev = &t->next) {
45 if (t->priority > priority)
d2acc347 46 break;
b33eab08 47 if (t->priority == priority)
d2acc347
HX
48 goto err;
49 }
50
51 handler->next = *pprev;
49d61e23 52 rcu_assign_pointer(*pprev, handler);
d2acc347
HX
53
54 ret = 0;
55
56err:
57 mutex_unlock(&tunnel4_mutex);
58
59 return ret;
60}
d2acc347
HX
61EXPORT_SYMBOL(xfrm4_tunnel_register);
62
c0d56408 63int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
d2acc347 64{
b33eab08
ED
65 struct xfrm_tunnel __rcu **pprev;
66 struct xfrm_tunnel *t;
d2acc347
HX
67 int ret = -ENOENT;
68
69 mutex_lock(&tunnel4_mutex);
70
b33eab08
ED
71 for (pprev = fam_handlers(family);
72 (t = rcu_dereference_protected(*pprev,
73 lockdep_is_held(&tunnel4_mutex))) != NULL;
74 pprev = &t->next) {
75 if (t == handler) {
d2acc347
HX
76 *pprev = handler->next;
77 ret = 0;
78 break;
79 }
80 }
81
82 mutex_unlock(&tunnel4_mutex);
83
84 synchronize_net();
85
86 return ret;
87}
d2acc347
HX
88EXPORT_SYMBOL(xfrm4_tunnel_deregister);
89
875168a9
ED
90#define for_each_tunnel_rcu(head, handler) \
91 for (handler = rcu_dereference(head); \
92 handler != NULL; \
93 handler = rcu_dereference(handler->next)) \
82695b30 94
d2acc347
HX
95static int tunnel4_rcv(struct sk_buff *skb)
96{
97 struct xfrm_tunnel *handler;
98
50fba2aa
HX
99 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
100 goto drop;
101
875168a9 102 for_each_tunnel_rcu(tunnel4_handlers, handler)
d2acc347
HX
103 if (!handler->handler(skb))
104 return 0;
105
50fba2aa
HX
106 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
107
108drop:
d2acc347
HX
109 kfree_skb(skb);
110 return 0;
111}
112
dfd56b8b 113#if IS_ENABLED(CONFIG_IPV6)
c0d56408
KM
114static int tunnel64_rcv(struct sk_buff *skb)
115{
116 struct xfrm_tunnel *handler;
117
baa2bfb8 118 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
c0d56408
KM
119 goto drop;
120
875168a9 121 for_each_tunnel_rcu(tunnel64_handlers, handler)
c0d56408
KM
122 if (!handler->handler(skb))
123 return 0;
124
125 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
126
127drop:
128 kfree_skb(skb);
129 return 0;
130}
131#endif
132
8afe97e5
SH
133#if IS_ENABLED(CONFIG_MPLS)
134static int tunnelmpls4_rcv(struct sk_buff *skb)
135{
136 struct xfrm_tunnel *handler;
137
138 if (!pskb_may_pull(skb, sizeof(struct mpls_label)))
139 goto drop;
140
141 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
142 if (!handler->handler(skb))
143 return 0;
144
145 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
146
147drop:
148 kfree_skb(skb);
149 return 0;
150}
151#endif
152
32bbd879 153static int tunnel4_err(struct sk_buff *skb, u32 info)
d2acc347
HX
154{
155 struct xfrm_tunnel *handler;
156
875168a9 157 for_each_tunnel_rcu(tunnel4_handlers, handler)
d2acc347 158 if (!handler->err_handler(skb, info))
32bbd879
SB
159 return 0;
160
161 return -ENOENT;
d2acc347
HX
162}
163
dfd56b8b 164#if IS_ENABLED(CONFIG_IPV6)
32bbd879 165static int tunnel64_err(struct sk_buff *skb, u32 info)
99f93326
PE
166{
167 struct xfrm_tunnel *handler;
168
875168a9 169 for_each_tunnel_rcu(tunnel64_handlers, handler)
99f93326 170 if (!handler->err_handler(skb, info))
32bbd879
SB
171 return 0;
172
173 return -ENOENT;
99f93326
PE
174}
175#endif
176
8afe97e5 177#if IS_ENABLED(CONFIG_MPLS)
32bbd879 178static int tunnelmpls4_err(struct sk_buff *skb, u32 info)
8afe97e5
SH
179{
180 struct xfrm_tunnel *handler;
181
182 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
183 if (!handler->err_handler(skb, info))
32bbd879
SB
184 return 0;
185
186 return -ENOENT;
8afe97e5
SH
187}
188#endif
189
32613090 190static const struct net_protocol tunnel4_protocol = {
d2acc347
HX
191 .handler = tunnel4_rcv,
192 .err_handler = tunnel4_err,
193 .no_policy = 1,
4597a0ce 194 .netns_ok = 1,
d2acc347
HX
195};
196
dfd56b8b 197#if IS_ENABLED(CONFIG_IPV6)
32613090 198static const struct net_protocol tunnel64_protocol = {
c0d56408 199 .handler = tunnel64_rcv,
99f93326 200 .err_handler = tunnel64_err,
c0d56408 201 .no_policy = 1,
b0970c42 202 .netns_ok = 1,
c0d56408
KM
203};
204#endif
205
8afe97e5
SH
206#if IS_ENABLED(CONFIG_MPLS)
207static const struct net_protocol tunnelmpls4_protocol = {
208 .handler = tunnelmpls4_rcv,
209 .err_handler = tunnelmpls4_err,
210 .no_policy = 1,
211 .netns_ok = 1,
212};
213#endif
214
d2acc347
HX
215static int __init tunnel4_init(void)
216{
8afe97e5 217 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
aa9667e7 218 goto err;
dfd56b8b 219#if IS_ENABLED(CONFIG_IPV6)
aa9667e7
SH
220 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
221 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
222 goto err;
223 }
8afe97e5
SH
224#endif
225#if IS_ENABLED(CONFIG_MPLS)
aa9667e7
SH
226 if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) {
227 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
228#if IS_ENABLED(CONFIG_IPV6)
229 inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
230#endif
231 goto err;
232 }
c0d56408 233#endif
d2acc347 234 return 0;
8afe97e5 235
aa9667e7 236err:
8afe97e5
SH
237 pr_err("%s: can't add protocol\n", __func__);
238 return -EAGAIN;
d2acc347
HX
239}
240
241static void __exit tunnel4_fini(void)
242{
8afe97e5
SH
243#if IS_ENABLED(CONFIG_MPLS)
244 if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS))
245 pr_err("tunnelmpls4 close: can't remove protocol\n");
246#endif
dfd56b8b 247#if IS_ENABLED(CONFIG_IPV6)
c0d56408 248 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
058bd4d2 249 pr_err("tunnel64 close: can't remove protocol\n");
c0d56408 250#endif
d2acc347 251 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
058bd4d2 252 pr_err("tunnel4 close: can't remove protocol\n");
d2acc347
HX
253}
254
255module_init(tunnel4_init);
256module_exit(tunnel4_fini);
257MODULE_LICENSE("GPL");