tunnel4.c 6.55 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9
/* tunnel4.c: Generic IP tunnel transformer.
 *
 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/mutex.h>
10
#include <linux/mpls.h>
11 12
#include <linux/netdevice.h>
#include <linux/skbuff.h>
13
#include <linux/slab.h>
14 15
#include <net/icmp.h>
#include <net/ip.h>
16 17 18
#include <net/protocol.h>
#include <net/xfrm.h>

19 20
static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
21
static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly;
22 23
static DEFINE_MUTEX(tunnel4_mutex);

24
static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
25
{
26 27 28
	return (family == AF_INET) ? &tunnel4_handlers :
		(family == AF_INET6) ? &tunnel64_handlers :
		&tunnelmpls4_handlers;
29 30
}

31
int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
32
{
33 34 35
	struct xfrm_tunnel __rcu **pprev;
	struct xfrm_tunnel *t;

36 37 38 39 40
	int ret = -EEXIST;
	int priority = handler->priority;

	mutex_lock(&tunnel4_mutex);

41 42 43 44 45
	for (pprev = fam_handlers(family);
	     (t = rcu_dereference_protected(*pprev,
			lockdep_is_held(&tunnel4_mutex))) != NULL;
	     pprev = &t->next) {
		if (t->priority > priority)
46
			break;
47
		if (t->priority == priority)
48 49 50 51
			goto err;
	}

	handler->next = *pprev;
52
	rcu_assign_pointer(*pprev, handler);
53 54 55 56 57 58 59 60 61 62

	ret = 0;

err:
	mutex_unlock(&tunnel4_mutex);

	return ret;
}
EXPORT_SYMBOL(xfrm4_tunnel_register);

63
int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
64
{
65 66
	struct xfrm_tunnel __rcu **pprev;
	struct xfrm_tunnel *t;
67 68 69 70
	int ret = -ENOENT;

	mutex_lock(&tunnel4_mutex);

71 72 73 74 75
	for (pprev = fam_handlers(family);
	     (t = rcu_dereference_protected(*pprev,
			lockdep_is_held(&tunnel4_mutex))) != NULL;
	     pprev = &t->next) {
		if (t == handler) {
76 77 78 79 80 81 82 83 84 85 86 87 88 89
			*pprev = handler->next;
			ret = 0;
			break;
		}
	}

	mutex_unlock(&tunnel4_mutex);

	synchronize_net();

	return ret;
}
EXPORT_SYMBOL(xfrm4_tunnel_deregister);

90 91 92 93
#define for_each_tunnel_rcu(head, handler)		\
	for (handler = rcu_dereference(head);		\
	     handler != NULL;				\
	     handler = rcu_dereference(handler->next))	\
94

95 96 97 98
static int tunnel4_rcv(struct sk_buff *skb)
{
	struct xfrm_tunnel *handler;

99 100 101
	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
		goto drop;

102
	for_each_tunnel_rcu(tunnel4_handlers, handler)
103 104 105
		if (!handler->handler(skb))
			return 0;

106 107 108
	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);

drop:
109 110 111 112
	kfree_skb(skb);
	return 0;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
static int tunnel4_rcv_cb(struct sk_buff *skb, u8 proto, int err)
{
	struct xfrm_tunnel __rcu *head;
	struct xfrm_tunnel *handler;
	int ret;

	head = (proto == IPPROTO_IPIP) ? tunnel4_handlers : tunnel64_handlers;

	for_each_tunnel_rcu(head, handler) {
		if (handler->cb_handler) {
			ret = handler->cb_handler(skb, err);
			if (ret <= 0)
				return ret;
		}
	}

	return 0;
}

static const struct xfrm_input_afinfo tunnel4_input_afinfo = {
	.family		=	AF_INET,
	.is_ipip	=	true,
	.callback	=	tunnel4_rcv_cb,
};
#endif

140
#if IS_ENABLED(CONFIG_IPV6)
141 142 143 144
static int tunnel64_rcv(struct sk_buff *skb)
{
	struct xfrm_tunnel *handler;

145
	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
146 147
		goto drop;

148
	for_each_tunnel_rcu(tunnel64_handlers, handler)
149 150 151 152 153 154 155 156 157 158 159
		if (!handler->handler(skb))
			return 0;

	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);

drop:
	kfree_skb(skb);
	return 0;
}
#endif

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#if IS_ENABLED(CONFIG_MPLS)
static int tunnelmpls4_rcv(struct sk_buff *skb)
{
	struct xfrm_tunnel *handler;

	if (!pskb_may_pull(skb, sizeof(struct mpls_label)))
		goto drop;

	for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
		if (!handler->handler(skb))
			return 0;

	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);

drop:
	kfree_skb(skb);
	return 0;
}
#endif

180
static int tunnel4_err(struct sk_buff *skb, u32 info)
181 182 183
{
	struct xfrm_tunnel *handler;

184
	for_each_tunnel_rcu(tunnel4_handlers, handler)
185
		if (!handler->err_handler(skb, info))
186 187 188
			return 0;

	return -ENOENT;
189 190
}

191
#if IS_ENABLED(CONFIG_IPV6)
192
static int tunnel64_err(struct sk_buff *skb, u32 info)
193 194 195
{
	struct xfrm_tunnel *handler;

196
	for_each_tunnel_rcu(tunnel64_handlers, handler)
197
		if (!handler->err_handler(skb, info))
198 199 200
			return 0;

	return -ENOENT;
201 202 203
}
#endif

204
#if IS_ENABLED(CONFIG_MPLS)
205
static int tunnelmpls4_err(struct sk_buff *skb, u32 info)
206 207 208 209 210
{
	struct xfrm_tunnel *handler;

	for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
		if (!handler->err_handler(skb, info))
211 212 213
			return 0;

	return -ENOENT;
214 215 216
}
#endif

217
static const struct net_protocol tunnel4_protocol = {
218 219 220
	.handler	=	tunnel4_rcv,
	.err_handler	=	tunnel4_err,
	.no_policy	=	1,
221
	.netns_ok	=	1,
222 223
};

224
#if IS_ENABLED(CONFIG_IPV6)
225
static const struct net_protocol tunnel64_protocol = {
226
	.handler	=	tunnel64_rcv,
227
	.err_handler	=	tunnel64_err,
228
	.no_policy	=	1,
229
	.netns_ok	=	1,
230 231 232
};
#endif

233 234 235 236 237 238 239 240 241
#if IS_ENABLED(CONFIG_MPLS)
static const struct net_protocol tunnelmpls4_protocol = {
	.handler	=	tunnelmpls4_rcv,
	.err_handler	=	tunnelmpls4_err,
	.no_policy	=	1,
	.netns_ok	=	1,
};
#endif

242 243
static int __init tunnel4_init(void)
{
244
	if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
245
		goto err;
246
#if IS_ENABLED(CONFIG_IPV6)
247 248 249 250
	if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
		inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
		goto err;
	}
251 252
#endif
#if IS_ENABLED(CONFIG_MPLS)
253 254 255 256 257 258 259
	if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) {
		inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
#if IS_ENABLED(CONFIG_IPV6)
		inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
#endif
		goto err;
	}
260 261 262 263 264 265 266 267 268 269 270 271
#endif
#if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
	if (xfrm_input_register_afinfo(&tunnel4_input_afinfo)) {
		inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
#if IS_ENABLED(CONFIG_IPV6)
		inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
#endif
#if IS_ENABLED(CONFIG_MPLS)
		inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS);
#endif
		goto err;
	}
272
#endif
273
	return 0;
274

275
err:
276 277
	pr_err("%s: can't add protocol\n", __func__);
	return -EAGAIN;
278 279 280 281
}

static void __exit tunnel4_fini(void)
{
282 283 284 285
#if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
	if (xfrm_input_unregister_afinfo(&tunnel4_input_afinfo))
		pr_err("tunnel4 close: can't remove input afinfo\n");
#endif
286 287 288 289
#if IS_ENABLED(CONFIG_MPLS)
	if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS))
		pr_err("tunnelmpls4 close: can't remove protocol\n");
#endif
290
#if IS_ENABLED(CONFIG_IPV6)
291
	if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
292
		pr_err("tunnel64 close: can't remove protocol\n");
293
#endif
294
	if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
295
		pr_err("tunnel4 close: can't remove protocol\n");
296 297 298 299 300
}

module_init(tunnel4_init);
module_exit(tunnel4_fini);
MODULE_LICENSE("GPL");