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