blob: 9a17bd2a0a37fdae55aca5aff11324dfa31ac1ca [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 Dumazet6dcd8142010-08-30 07:04:14 +000017static struct xfrm_tunnel *tunnel4_handlers __read_mostly;
18static struct xfrm_tunnel *tunnel64_handlers __read_mostly;
Herbert Xud2acc342006-03-28 01:12:13 -080019static DEFINE_MUTEX(tunnel4_mutex);
20
Pavel Emelyanov358352b2007-11-10 21:48:54 -080021static inline struct xfrm_tunnel **fam_handlers(unsigned short family)
22{
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{
28 struct xfrm_tunnel **pprev;
29 int ret = -EEXIST;
30 int priority = handler->priority;
31
32 mutex_lock(&tunnel4_mutex);
33
Pavel Emelyanov358352b2007-11-10 21:48:54 -080034 for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
Herbert Xud2acc342006-03-28 01:12:13 -080035 if ((*pprev)->priority > priority)
36 break;
37 if ((*pprev)->priority == priority)
38 goto err;
39 }
40
41 handler->next = *pprev;
Eric Dumazet49d61e22010-09-09 05:33:43 +000042 rcu_assign_pointer(*pprev, handler);
Herbert Xud2acc342006-03-28 01:12:13 -080043
44 ret = 0;
45
46err:
47 mutex_unlock(&tunnel4_mutex);
48
49 return ret;
50}
Herbert Xud2acc342006-03-28 01:12:13 -080051EXPORT_SYMBOL(xfrm4_tunnel_register);
52
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080053int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
Herbert Xud2acc342006-03-28 01:12:13 -080054{
55 struct xfrm_tunnel **pprev;
56 int ret = -ENOENT;
57
58 mutex_lock(&tunnel4_mutex);
59
Pavel Emelyanov358352b2007-11-10 21:48:54 -080060 for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
Herbert Xud2acc342006-03-28 01:12:13 -080061 if (*pprev == handler) {
62 *pprev = handler->next;
63 ret = 0;
64 break;
65 }
66 }
67
68 mutex_unlock(&tunnel4_mutex);
69
70 synchronize_net();
71
72 return ret;
73}
Herbert Xud2acc342006-03-28 01:12:13 -080074EXPORT_SYMBOL(xfrm4_tunnel_deregister);
75
Eric Dumazet875168a2010-08-30 11:07:25 +000076#define for_each_tunnel_rcu(head, handler) \
77 for (handler = rcu_dereference(head); \
78 handler != NULL; \
79 handler = rcu_dereference(handler->next)) \
80
Herbert Xud2acc342006-03-28 01:12:13 -080081static int tunnel4_rcv(struct sk_buff *skb)
82{
83 struct xfrm_tunnel *handler;
84
Herbert Xu50fba2a2006-04-04 13:50:45 -070085 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
86 goto drop;
87
Eric Dumazet875168a2010-08-30 11:07:25 +000088 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -080089 if (!handler->handler(skb))
90 return 0;
91
Herbert Xu50fba2a2006-04-04 13:50:45 -070092 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
93
94drop:
Herbert Xud2acc342006-03-28 01:12:13 -080095 kfree_skb(skb);
96 return 0;
97}
98
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080099#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
100static int tunnel64_rcv(struct sk_buff *skb)
101{
102 struct xfrm_tunnel *handler;
103
YOSHIFUJI Hideakibaa2bfb2008-05-30 11:35:03 +0900104 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800105 goto drop;
106
Eric Dumazet875168a2010-08-30 11:07:25 +0000107 for_each_tunnel_rcu(tunnel64_handlers, handler)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800108 if (!handler->handler(skb))
109 return 0;
110
111 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
112
113drop:
114 kfree_skb(skb);
115 return 0;
116}
117#endif
118
Herbert Xud2acc342006-03-28 01:12:13 -0800119static void tunnel4_err(struct sk_buff *skb, u32 info)
120{
121 struct xfrm_tunnel *handler;
122
Eric Dumazet875168a2010-08-30 11:07:25 +0000123 for_each_tunnel_rcu(tunnel4_handlers, handler)
Herbert Xud2acc342006-03-28 01:12:13 -0800124 if (!handler->err_handler(skb, info))
125 break;
126}
127
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800128#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129static void tunnel64_err(struct sk_buff *skb, u32 info)
130{
131 struct xfrm_tunnel *handler;
132
Eric Dumazet875168a2010-08-30 11:07:25 +0000133 for_each_tunnel_rcu(tunnel64_handlers, handler)
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800134 if (!handler->err_handler(skb, info))
135 break;
136}
137#endif
138
Alexey Dobriyan32613092009-09-14 12:21:47 +0000139static const struct net_protocol tunnel4_protocol = {
Herbert Xud2acc342006-03-28 01:12:13 -0800140 .handler = tunnel4_rcv,
141 .err_handler = tunnel4_err,
142 .no_policy = 1,
Pavel Emelyanov4597a0c2008-04-16 01:06:56 -0700143 .netns_ok = 1,
Herbert Xud2acc342006-03-28 01:12:13 -0800144};
145
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800146#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan32613092009-09-14 12:21:47 +0000147static const struct net_protocol tunnel64_protocol = {
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800148 .handler = tunnel64_rcv,
Pavel Emelyanov99f93322007-11-10 21:47:39 -0800149 .err_handler = tunnel64_err,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800150 .no_policy = 1,
Pavel Emelyanovb0970c42008-04-16 01:17:39 -0700151 .netns_ok = 1,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800152};
153#endif
154
Herbert Xud2acc342006-03-28 01:12:13 -0800155static int __init tunnel4_init(void)
156{
157 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
158 printk(KERN_ERR "tunnel4 init: can't add protocol\n");
159 return -EAGAIN;
160 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800161#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
162 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
163 printk(KERN_ERR "tunnel64 init: can't add protocol\n");
164 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
165 return -EAGAIN;
166 }
167#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800168 return 0;
169}
170
171static void __exit tunnel4_fini(void)
172{
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800173#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
174 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
175 printk(KERN_ERR "tunnel64 close: can't remove protocol\n");
176#endif
Herbert Xud2acc342006-03-28 01:12:13 -0800177 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
178 printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
179}
180
181module_init(tunnel4_init);
182module_exit(tunnel4_fini);
183MODULE_LICENSE("GPL");