octeontx2-pf: Fix error return code in otx2_probe()
[linux/fpc-iii.git] / net / ipv4 / tunnel4.c
blobc4b2ccbeba04bb99357687f107ab5b9a27c10df8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* tunnel4.c: Generic IP tunnel transformer.
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
5 */
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/mpls.h>
11 #include <linux/netdevice.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <net/icmp.h>
15 #include <net/ip.h>
16 #include <net/protocol.h>
17 #include <net/xfrm.h>
19 static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
20 static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
21 static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly;
22 static DEFINE_MUTEX(tunnel4_mutex);
24 static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
26 return (family == AF_INET) ? &tunnel4_handlers :
27 (family == AF_INET6) ? &tunnel64_handlers :
28 &tunnelmpls4_handlers;
31 int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
33 struct xfrm_tunnel __rcu **pprev;
34 struct xfrm_tunnel *t;
36 int ret = -EEXIST;
37 int priority = handler->priority;
39 mutex_lock(&tunnel4_mutex);
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)
46 break;
47 if (t->priority == priority)
48 goto err;
51 handler->next = *pprev;
52 rcu_assign_pointer(*pprev, handler);
54 ret = 0;
56 err:
57 mutex_unlock(&tunnel4_mutex);
59 return ret;
61 EXPORT_SYMBOL(xfrm4_tunnel_register);
63 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
65 struct xfrm_tunnel __rcu **pprev;
66 struct xfrm_tunnel *t;
67 int ret = -ENOENT;
69 mutex_lock(&tunnel4_mutex);
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) {
76 *pprev = handler->next;
77 ret = 0;
78 break;
82 mutex_unlock(&tunnel4_mutex);
84 synchronize_net();
86 return ret;
88 EXPORT_SYMBOL(xfrm4_tunnel_deregister);
90 #define for_each_tunnel_rcu(head, handler) \
91 for (handler = rcu_dereference(head); \
92 handler != NULL; \
93 handler = rcu_dereference(handler->next)) \
95 static int tunnel4_rcv(struct sk_buff *skb)
97 struct xfrm_tunnel *handler;
99 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
100 goto drop;
102 for_each_tunnel_rcu(tunnel4_handlers, handler)
103 if (!handler->handler(skb))
104 return 0;
106 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
108 drop:
109 kfree_skb(skb);
110 return 0;
113 #if IS_ENABLED(CONFIG_IPV6)
114 static int tunnel64_rcv(struct sk_buff *skb)
116 struct xfrm_tunnel *handler;
118 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
119 goto drop;
121 for_each_tunnel_rcu(tunnel64_handlers, handler)
122 if (!handler->handler(skb))
123 return 0;
125 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
127 drop:
128 kfree_skb(skb);
129 return 0;
131 #endif
133 #if IS_ENABLED(CONFIG_MPLS)
134 static int tunnelmpls4_rcv(struct sk_buff *skb)
136 struct xfrm_tunnel *handler;
138 if (!pskb_may_pull(skb, sizeof(struct mpls_label)))
139 goto drop;
141 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
142 if (!handler->handler(skb))
143 return 0;
145 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
147 drop:
148 kfree_skb(skb);
149 return 0;
151 #endif
153 static int tunnel4_err(struct sk_buff *skb, u32 info)
155 struct xfrm_tunnel *handler;
157 for_each_tunnel_rcu(tunnel4_handlers, handler)
158 if (!handler->err_handler(skb, info))
159 return 0;
161 return -ENOENT;
164 #if IS_ENABLED(CONFIG_IPV6)
165 static int tunnel64_err(struct sk_buff *skb, u32 info)
167 struct xfrm_tunnel *handler;
169 for_each_tunnel_rcu(tunnel64_handlers, handler)
170 if (!handler->err_handler(skb, info))
171 return 0;
173 return -ENOENT;
175 #endif
177 #if IS_ENABLED(CONFIG_MPLS)
178 static int tunnelmpls4_err(struct sk_buff *skb, u32 info)
180 struct xfrm_tunnel *handler;
182 for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
183 if (!handler->err_handler(skb, info))
184 return 0;
186 return -ENOENT;
188 #endif
190 static const struct net_protocol tunnel4_protocol = {
191 .handler = tunnel4_rcv,
192 .err_handler = tunnel4_err,
193 .no_policy = 1,
194 .netns_ok = 1,
197 #if IS_ENABLED(CONFIG_IPV6)
198 static const struct net_protocol tunnel64_protocol = {
199 .handler = tunnel64_rcv,
200 .err_handler = tunnel64_err,
201 .no_policy = 1,
202 .netns_ok = 1,
204 #endif
206 #if IS_ENABLED(CONFIG_MPLS)
207 static const struct net_protocol tunnelmpls4_protocol = {
208 .handler = tunnelmpls4_rcv,
209 .err_handler = tunnelmpls4_err,
210 .no_policy = 1,
211 .netns_ok = 1,
213 #endif
215 static int __init tunnel4_init(void)
217 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
218 goto err;
219 #if IS_ENABLED(CONFIG_IPV6)
220 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
221 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
222 goto err;
224 #endif
225 #if IS_ENABLED(CONFIG_MPLS)
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;
233 #endif
234 return 0;
236 err:
237 pr_err("%s: can't add protocol\n", __func__);
238 return -EAGAIN;
241 static void __exit tunnel4_fini(void)
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
247 #if IS_ENABLED(CONFIG_IPV6)
248 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
249 pr_err("tunnel64 close: can't remove protocol\n");
250 #endif
251 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
252 pr_err("tunnel4 close: can't remove protocol\n");
255 module_init(tunnel4_init);
256 module_exit(tunnel4_fini);
257 MODULE_LICENSE("GPL");