octeontx2-pf: Fix error return code in otx2_probe()
[linux/fpc-iii.git] / net / ipv6 / tunnel6.c
blob21e7b95ddbfafd52083bdff2c8f2d3b3978802fc
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C)2003,2004 USAGI/WIDE Project
5 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
6 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
7 */
9 #define pr_fmt(fmt) "IPv6: " fmt
11 #include <linux/icmpv6.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/slab.h>
18 #include <net/ipv6.h>
19 #include <net/protocol.h>
20 #include <net/xfrm.h>
22 static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
23 static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
24 static DEFINE_MUTEX(tunnel6_mutex);
26 int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
28 struct xfrm6_tunnel __rcu **pprev;
29 struct xfrm6_tunnel *t;
30 int ret = -EEXIST;
31 int priority = handler->priority;
33 mutex_lock(&tunnel6_mutex);
35 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
36 (t = rcu_dereference_protected(*pprev,
37 lockdep_is_held(&tunnel6_mutex))) != NULL;
38 pprev = &t->next) {
39 if (t->priority > priority)
40 break;
41 if (t->priority == priority)
42 goto err;
45 handler->next = *pprev;
46 rcu_assign_pointer(*pprev, handler);
48 ret = 0;
50 err:
51 mutex_unlock(&tunnel6_mutex);
53 return ret;
55 EXPORT_SYMBOL(xfrm6_tunnel_register);
57 int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
59 struct xfrm6_tunnel __rcu **pprev;
60 struct xfrm6_tunnel *t;
61 int ret = -ENOENT;
63 mutex_lock(&tunnel6_mutex);
65 for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
66 (t = rcu_dereference_protected(*pprev,
67 lockdep_is_held(&tunnel6_mutex))) != NULL;
68 pprev = &t->next) {
69 if (t == handler) {
70 *pprev = handler->next;
71 ret = 0;
72 break;
76 mutex_unlock(&tunnel6_mutex);
78 synchronize_net();
80 return ret;
82 EXPORT_SYMBOL(xfrm6_tunnel_deregister);
84 #define for_each_tunnel_rcu(head, handler) \
85 for (handler = rcu_dereference(head); \
86 handler != NULL; \
87 handler = rcu_dereference(handler->next)) \
89 static int tunnel6_rcv(struct sk_buff *skb)
91 struct xfrm6_tunnel *handler;
93 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
94 goto drop;
96 for_each_tunnel_rcu(tunnel6_handlers, handler)
97 if (!handler->handler(skb))
98 return 0;
100 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
102 drop:
103 kfree_skb(skb);
104 return 0;
107 static int tunnel46_rcv(struct sk_buff *skb)
109 struct xfrm6_tunnel *handler;
111 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
112 goto drop;
114 for_each_tunnel_rcu(tunnel46_handlers, handler)
115 if (!handler->handler(skb))
116 return 0;
118 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
120 drop:
121 kfree_skb(skb);
122 return 0;
125 static int tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
126 u8 type, u8 code, int offset, __be32 info)
128 struct xfrm6_tunnel *handler;
130 for_each_tunnel_rcu(tunnel6_handlers, handler)
131 if (!handler->err_handler(skb, opt, type, code, offset, info))
132 return 0;
134 return -ENOENT;
137 static int tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
138 u8 type, u8 code, int offset, __be32 info)
140 struct xfrm6_tunnel *handler;
142 for_each_tunnel_rcu(tunnel46_handlers, handler)
143 if (!handler->err_handler(skb, opt, type, code, offset, info))
144 return 0;
146 return -ENOENT;
149 static const struct inet6_protocol tunnel6_protocol = {
150 .handler = tunnel6_rcv,
151 .err_handler = tunnel6_err,
152 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
155 static const struct inet6_protocol tunnel46_protocol = {
156 .handler = tunnel46_rcv,
157 .err_handler = tunnel46_err,
158 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
161 static int __init tunnel6_init(void)
163 if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
164 pr_err("%s: can't add protocol\n", __func__);
165 return -EAGAIN;
167 if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
168 pr_err("%s: can't add protocol\n", __func__);
169 inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
170 return -EAGAIN;
172 return 0;
175 static void __exit tunnel6_fini(void)
177 if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
178 pr_err("%s: can't remove protocol\n", __func__);
179 if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
180 pr_err("%s: can't remove protocol\n", __func__);
183 module_init(tunnel6_init);
184 module_exit(tunnel6_fini);
185 MODULE_LICENSE("GPL");