1 /* tunnel4.c: Generic IP tunnel transformer.
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/mpls.h>
10 #include <linux/netdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/slab.h>
15 #include <net/protocol.h>
18 static struct xfrm_tunnel __rcu
*tunnel4_handlers __read_mostly
;
19 static struct xfrm_tunnel __rcu
*tunnel64_handlers __read_mostly
;
20 static struct xfrm_tunnel __rcu
*tunnelmpls4_handlers __read_mostly
;
21 static DEFINE_MUTEX(tunnel4_mutex
);
23 static inline struct xfrm_tunnel __rcu
**fam_handlers(unsigned short family
)
25 return (family
== AF_INET
) ? &tunnel4_handlers
:
26 (family
== AF_INET6
) ? &tunnel64_handlers
:
27 &tunnelmpls4_handlers
;
30 int xfrm4_tunnel_register(struct xfrm_tunnel
*handler
, unsigned short family
)
32 struct xfrm_tunnel __rcu
**pprev
;
33 struct xfrm_tunnel
*t
;
36 int priority
= handler
->priority
;
38 mutex_lock(&tunnel4_mutex
);
40 for (pprev
= fam_handlers(family
);
41 (t
= rcu_dereference_protected(*pprev
,
42 lockdep_is_held(&tunnel4_mutex
))) != NULL
;
44 if (t
->priority
> priority
)
46 if (t
->priority
== priority
)
50 handler
->next
= *pprev
;
51 rcu_assign_pointer(*pprev
, handler
);
56 mutex_unlock(&tunnel4_mutex
);
60 EXPORT_SYMBOL(xfrm4_tunnel_register
);
62 int xfrm4_tunnel_deregister(struct xfrm_tunnel
*handler
, unsigned short family
)
64 struct xfrm_tunnel __rcu
**pprev
;
65 struct xfrm_tunnel
*t
;
68 mutex_lock(&tunnel4_mutex
);
70 for (pprev
= fam_handlers(family
);
71 (t
= rcu_dereference_protected(*pprev
,
72 lockdep_is_held(&tunnel4_mutex
))) != NULL
;
75 *pprev
= handler
->next
;
81 mutex_unlock(&tunnel4_mutex
);
87 EXPORT_SYMBOL(xfrm4_tunnel_deregister
);
89 #define for_each_tunnel_rcu(head, handler) \
90 for (handler = rcu_dereference(head); \
92 handler = rcu_dereference(handler->next)) \
94 static int tunnel4_rcv(struct sk_buff
*skb
)
96 struct xfrm_tunnel
*handler
;
98 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
101 for_each_tunnel_rcu(tunnel4_handlers
, handler
)
102 if (!handler
->handler(skb
))
105 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
112 #if IS_ENABLED(CONFIG_IPV6)
113 static int tunnel64_rcv(struct sk_buff
*skb
)
115 struct xfrm_tunnel
*handler
;
117 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
120 for_each_tunnel_rcu(tunnel64_handlers
, handler
)
121 if (!handler
->handler(skb
))
124 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
132 #if IS_ENABLED(CONFIG_MPLS)
133 static int tunnelmpls4_rcv(struct sk_buff
*skb
)
135 struct xfrm_tunnel
*handler
;
137 if (!pskb_may_pull(skb
, sizeof(struct mpls_label
)))
140 for_each_tunnel_rcu(tunnelmpls4_handlers
, handler
)
141 if (!handler
->handler(skb
))
144 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
152 static void tunnel4_err(struct sk_buff
*skb
, u32 info
)
154 struct xfrm_tunnel
*handler
;
156 for_each_tunnel_rcu(tunnel4_handlers
, handler
)
157 if (!handler
->err_handler(skb
, info
))
161 #if IS_ENABLED(CONFIG_IPV6)
162 static void tunnel64_err(struct sk_buff
*skb
, u32 info
)
164 struct xfrm_tunnel
*handler
;
166 for_each_tunnel_rcu(tunnel64_handlers
, handler
)
167 if (!handler
->err_handler(skb
, info
))
172 #if IS_ENABLED(CONFIG_MPLS)
173 static void tunnelmpls4_err(struct sk_buff
*skb
, u32 info
)
175 struct xfrm_tunnel
*handler
;
177 for_each_tunnel_rcu(tunnelmpls4_handlers
, handler
)
178 if (!handler
->err_handler(skb
, info
))
183 static const struct net_protocol tunnel4_protocol
= {
184 .handler
= tunnel4_rcv
,
185 .err_handler
= tunnel4_err
,
190 #if IS_ENABLED(CONFIG_IPV6)
191 static const struct net_protocol tunnel64_protocol
= {
192 .handler
= tunnel64_rcv
,
193 .err_handler
= tunnel64_err
,
199 #if IS_ENABLED(CONFIG_MPLS)
200 static const struct net_protocol tunnelmpls4_protocol
= {
201 .handler
= tunnelmpls4_rcv
,
202 .err_handler
= tunnelmpls4_err
,
208 static int __init
tunnel4_init(void)
210 if (inet_add_protocol(&tunnel4_protocol
, IPPROTO_IPIP
))
212 #if IS_ENABLED(CONFIG_IPV6)
213 if (inet_add_protocol(&tunnel64_protocol
, IPPROTO_IPV6
)) {
214 inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
);
218 #if IS_ENABLED(CONFIG_MPLS)
219 if (inet_add_protocol(&tunnelmpls4_protocol
, IPPROTO_MPLS
)) {
220 inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
);
221 #if IS_ENABLED(CONFIG_IPV6)
222 inet_del_protocol(&tunnel64_protocol
, IPPROTO_IPV6
);
230 pr_err("%s: can't add protocol\n", __func__
);
234 static void __exit
tunnel4_fini(void)
236 #if IS_ENABLED(CONFIG_MPLS)
237 if (inet_del_protocol(&tunnelmpls4_protocol
, IPPROTO_MPLS
))
238 pr_err("tunnelmpls4 close: can't remove protocol\n");
240 #if IS_ENABLED(CONFIG_IPV6)
241 if (inet_del_protocol(&tunnel64_protocol
, IPPROTO_IPV6
))
242 pr_err("tunnel64 close: can't remove protocol\n");
244 if (inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
))
245 pr_err("tunnel4 close: can't remove protocol\n");
248 module_init(tunnel4_init
);
249 module_exit(tunnel4_fini
);
250 MODULE_LICENSE("GPL");