xref: /openbmc/linux/net/ipv6/tunnel6.c (revision ae213c44)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C)2003,2004 USAGI/WIDE Project
4  *
5  * Authors	Mitsuru KANDA  <mk@linux-ipv6.org>
6  *		YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
7  */
8 
9 #define pr_fmt(fmt) "IPv6: " fmt
10 
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>
21 
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);
25 
26 int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
27 {
28 	struct xfrm6_tunnel __rcu **pprev;
29 	struct xfrm6_tunnel *t;
30 	int ret = -EEXIST;
31 	int priority = handler->priority;
32 
33 	mutex_lock(&tunnel6_mutex);
34 
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;
43 	}
44 
45 	handler->next = *pprev;
46 	rcu_assign_pointer(*pprev, handler);
47 
48 	ret = 0;
49 
50 err:
51 	mutex_unlock(&tunnel6_mutex);
52 
53 	return ret;
54 }
55 EXPORT_SYMBOL(xfrm6_tunnel_register);
56 
57 int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
58 {
59 	struct xfrm6_tunnel __rcu **pprev;
60 	struct xfrm6_tunnel *t;
61 	int ret = -ENOENT;
62 
63 	mutex_lock(&tunnel6_mutex);
64 
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;
73 		}
74 	}
75 
76 	mutex_unlock(&tunnel6_mutex);
77 
78 	synchronize_net();
79 
80 	return ret;
81 }
82 EXPORT_SYMBOL(xfrm6_tunnel_deregister);
83 
84 #define for_each_tunnel_rcu(head, handler)		\
85 	for (handler = rcu_dereference(head);		\
86 	     handler != NULL;				\
87 	     handler = rcu_dereference(handler->next))	\
88 
89 static int tunnel6_rcv(struct sk_buff *skb)
90 {
91 	struct xfrm6_tunnel *handler;
92 
93 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
94 		goto drop;
95 
96 	for_each_tunnel_rcu(tunnel6_handlers, handler)
97 		if (!handler->handler(skb))
98 			return 0;
99 
100 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
101 
102 drop:
103 	kfree_skb(skb);
104 	return 0;
105 }
106 
107 static int tunnel46_rcv(struct sk_buff *skb)
108 {
109 	struct xfrm6_tunnel *handler;
110 
111 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
112 		goto drop;
113 
114 	for_each_tunnel_rcu(tunnel46_handlers, handler)
115 		if (!handler->handler(skb))
116 			return 0;
117 
118 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
119 
120 drop:
121 	kfree_skb(skb);
122 	return 0;
123 }
124 
125 static int tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
126 			u8 type, u8 code, int offset, __be32 info)
127 {
128 	struct xfrm6_tunnel *handler;
129 
130 	for_each_tunnel_rcu(tunnel6_handlers, handler)
131 		if (!handler->err_handler(skb, opt, type, code, offset, info))
132 			return 0;
133 
134 	return -ENOENT;
135 }
136 
137 static int tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
138 			 u8 type, u8 code, int offset, __be32 info)
139 {
140 	struct xfrm6_tunnel *handler;
141 
142 	for_each_tunnel_rcu(tunnel46_handlers, handler)
143 		if (!handler->err_handler(skb, opt, type, code, offset, info))
144 			return 0;
145 
146 	return -ENOENT;
147 }
148 
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,
153 };
154 
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,
159 };
160 
161 static int __init tunnel6_init(void)
162 {
163 	if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
164 		pr_err("%s: can't add protocol\n", __func__);
165 		return -EAGAIN;
166 	}
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;
171 	}
172 	return 0;
173 }
174 
175 static void __exit tunnel6_fini(void)
176 {
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__);
181 }
182 
183 module_init(tunnel6_init);
184 module_exit(tunnel6_fini);
185 MODULE_LICENSE("GPL");
186