blob: 0d0171830620be95473ed86b20a103d5926e8e57 [file] [log] [blame]
Herbert Xud2acc342006-03-28 01:12:13 -08001/* 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>
9#include <linux/netdevice.h>
10#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Herbert Xu50fba2a2006-04-04 13:50:45 -070012#include <net/icmp.h>
13#include <net/ip.h>
Herbert Xud2acc342006-03-28 01:12:13 -080014#include <net/protocol.h>
15#include <net/xfrm.h>
16
Eric Dumazetb33eab02010-10-25 21:01:26 +000017static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
18static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
Herbert Xud2acc342006-03-28 01:12:13 -080019static DEFINE_MUTEX(tunnel4_mutex);
20
Eric Dumazetb33eab02010-10-25 21:01:26 +000021static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
Pavel Emelyanov358352b2007-11-10 21:48:54 -080022{
23 return (family == AF_INET) ? &tunnel4_handlers : &tunnel64_handlers;
24}
25
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080026int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080027{
Eric Dumazetb33eab02010-10-25 21:01:26 +000028 struct xfrm_tunnel __rcu **pprev;
29 struct xfrm_tunnel *t;
30
Herbert Xud2acc342006-03-28 01:12:13 -080031 int ret = -EEXIST;
32 int priority = handler->priority;
33
34 mutex_lock(&tunnel4_mutex);
35
Eric Dumazetb33eab02010-10-25 21:01:26 +000036 for (pprev = fam_handlers(family);
37 (t = rcu_dereference_protected(*pprev,
38 lockdep_is_held(&tunnel4_mutex))) != NULL;
39 pprev = &t->next) {
40 if (t->priority > priority)
Herbert Xud2acc342006-03-28 01:12:13 -080041 break;
Eric Dumazetb33eab02010-10-25 21:01:26 +000042 if (t->priority == priority)
Herbert Xud2acc342006-03-28 01:12:13 -080043 goto err;
44 }
45
46 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000047 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080048
49 ret = 0;
50
51err:
52 mutex_unlock(&tunnel4_mutex);
53
54 return ret;
55}
Herbert Xud2acc342006-03-28 01:12:13 -080056EXPORT_SYMBOL(xfrm4_tunnel_register);
57
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080058int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080059{
Eric Dumazetb33eab02010-10-25 21:01:26 +000060 struct xfrm_tunnel __rcu **pprev;
61 struct xfrm_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -080062 int ret = -ENOENT;
63
64 mutex_lock(&tunnel4_mutex);
65
Eric Dumazetb33eab02010-10-25 21:01:26 +000066 for (pprev = fam_handlers(family);
67 (t = rcu_dereference_protected(*pprev,
68 lockdep_is_held(&tunnel4_mutex))) != NULL;
69 pprev = &t->next) {
70 if (t == handler) {
Herbert Xud2acc342006-03-28 01:12:13 -080071 *pprev = handler->next;
72 ret = 0;
73 break;
74 }
75 }
76
77 mutex_unlock(&tunnel4_mutex);
78
79 synchronize_net();
80
81 return ret;
82}
Herbert Xud2acc342006-03-28 01:12:13 -080083EXPORT_SYMBOL(xfrm4_tunnel_deregister);
84
Eric Dumazet875168a2010-08-30 11:07:25 +000085#define for_each_tunnel_rcu(head, handler) \
86 for (handler = rcu_dereference(head); \
87 handler != NULL; \
88 handler = rcu_dereference(handler->next)) \
89
Herbert Xud2acc342006-03-28 01:12:13 -080090static int tunnel4_rcv(struct sk_buff *skb)
91{
92 struct xfrm_tunnel *handler;
93
Herbert Xu50fba2a2006-04-04 13:50:45 -070094 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
95 goto drop;
96
Eric Dumazet875168a2010-08-30 11:07:25 +000097 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -080098 if (!handler->handler(skb))
99 return 0;
100
Herbert Xu50fba2a2006-04-04 13:50:45 -0700101 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
102
103drop:
Herbert Xud2acc342006-03-28 01:12:13 -0800104 kfree_skb(skb);
105 return 0;
106}
107
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000108#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800109static int tunnel64_rcv(struct sk_buff *skb)
110{
111 struct xfrm_tunnel *handler;
112
YOSHIFUJI Hideakibaa2bfb2008-05-30 11:35:03 +0900113 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800114 goto drop;
115
Eric Dumazet875168a2010-08-30 11:07:25 +0000116 for_each_tunnel_rcu(tunnel64_handlers, handler)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800117 if (!handler->handler(skb))
118 return 0;
119
120 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
121
122drop:
123 kfree_skb(skb);
124 return 0;
125}
126#endif
127
Herbert Xud2acc342006-03-28 01:12:13 -0800128static void tunnel4_err(struct sk_buff *skb, u32 info)
129{
130 struct xfrm_tunnel *handler;
131
Eric Dumazet875168a2010-08-30 11:07:25 +0000132 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800133 if (!handler->err_handler(skb, info))
134 break;
135}
136
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000137#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800138static void tunnel64_err(struct sk_buff *skb, u32 info)
139{
140 struct xfrm_tunnel *handler;
141
Eric Dumazet875168a2010-08-30 11:07:25 +0000142 for_each_tunnel_rcu(tunnel64_handlers, handler)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800143 if (!handler->err_handler(skb, info))
144 break;
145}
146#endif
147
Alexey Dobriyan32613092009-09-14 12:21:47 +0000148static const struct net_protocol tunnel4_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800149 .handler = tunnel4_rcv,
150 .err_handler = tunnel4_err,
151 .no_policy = 1,
Pavel Emelyanov4597a0c2008-04-16 01:06:56 -0700152 .netns_ok = 1,
Herbert Xud2acc342006-03-28 01:12:13 -0800153};
154
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000155#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyan32613092009-09-14 12:21:47 +0000156static const struct net_protocol tunnel64_protocol = {
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800157 .handler = tunnel64_rcv,
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800158 .err_handler = tunnel64_err,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800159 .no_policy = 1,
Pavel Emelyanovb0970c42008-04-16 01:17:39 -0700160 .netns_ok = 1,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800161};
162#endif
163
Herbert Xud2acc342006-03-28 01:12:13 -0800164static int __init tunnel4_init(void)
165{
166 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000167 pr_err("%s: can't add protocol\n", __func__);
Herbert Xud2acc342006-03-28 01:12:13 -0800168 return -EAGAIN;
169 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000170#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800171 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000172 pr_err("tunnel64 init: can't add protocol\n");
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800173 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
174 return -EAGAIN;
175 }
176#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800177 return 0;
178}
179
180static void __exit tunnel4_fini(void)
181{
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000182#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800183 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
Joe Perches058bd4d2012-03-11 18:36:11 +0000184 pr_err("tunnel64 close: can't remove protocol\n");
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800185#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800186 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
Joe Perches058bd4d2012-03-11 18:36:11 +0000187 pr_err("tunnel4 close: can't remove protocol\n");
Herbert Xud2acc342006-03-28 01:12:13 -0800188}
189
190module_init(tunnel4_init);
191module_exit(tunnel4_fini);
192MODULE_LICENSE("GPL");