1 // SPDX-License-Identifier: GPL-2.0-only
2 /* tunnel4.c: Generic IP tunnel transformer.
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/mpls.h>
11 #include <linux/netdevice.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
16 #include <net/protocol.h>
19 static struct xfrm_tunnel __rcu
*tunnel4_handlers __read_mostly
;
20 static struct xfrm_tunnel __rcu
*tunnel64_handlers __read_mostly
;
21 static struct xfrm_tunnel __rcu
*tunnelmpls4_handlers __read_mostly
;
22 static DEFINE_MUTEX(tunnel4_mutex
);
24 static inline struct xfrm_tunnel __rcu
**fam_handlers(unsigned short family
)
26 return (family
== AF_INET
) ? &tunnel4_handlers
:
27 (family
== AF_INET6
) ? &tunnel64_handlers
:
28 &tunnelmpls4_handlers
;
31 int xfrm4_tunnel_register(struct xfrm_tunnel
*handler
, unsigned short family
)
33 struct xfrm_tunnel __rcu
**pprev
;
34 struct xfrm_tunnel
*t
;
37 int priority
= handler
->priority
;
39 mutex_lock(&tunnel4_mutex
);
41 for (pprev
= fam_handlers(family
);
42 (t
= rcu_dereference_protected(*pprev
,
43 lockdep_is_held(&tunnel4_mutex
))) != NULL
;
45 if (t
->priority
> priority
)
47 if (t
->priority
== priority
)
51 handler
->next
= *pprev
;
52 rcu_assign_pointer(*pprev
, handler
);
57 mutex_unlock(&tunnel4_mutex
);
61 EXPORT_SYMBOL(xfrm4_tunnel_register
);
63 int xfrm4_tunnel_deregister(struct xfrm_tunnel
*handler
, unsigned short family
)
65 struct xfrm_tunnel __rcu
**pprev
;
66 struct xfrm_tunnel
*t
;
69 mutex_lock(&tunnel4_mutex
);
71 for (pprev
= fam_handlers(family
);
72 (t
= rcu_dereference_protected(*pprev
,
73 lockdep_is_held(&tunnel4_mutex
))) != NULL
;
76 *pprev
= handler
->next
;
82 mutex_unlock(&tunnel4_mutex
);
88 EXPORT_SYMBOL(xfrm4_tunnel_deregister
);
90 #define for_each_tunnel_rcu(head, handler) \
91 for (handler = rcu_dereference(head); \
93 handler = rcu_dereference(handler->next)) \
95 static int tunnel4_rcv(struct sk_buff *skb)
97 struct xfrm_tunnel
*handler
;
99 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
102 for_each_tunnel_rcu(tunnel4_handlers
, handler
)
103 if (!handler
->handler(skb
))
106 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
113 #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
114 static int tunnel4_rcv_cb(struct sk_buff
*skb
, u8 proto
, int err
)
116 struct xfrm_tunnel __rcu
*head
;
117 struct xfrm_tunnel
*handler
;
120 head
= (proto
== IPPROTO_IPIP
) ? tunnel4_handlers
: tunnel64_handlers
;
122 for_each_tunnel_rcu(head
, handler
) {
123 if (handler
->cb_handler
) {
124 ret
= handler
->cb_handler(skb
, err
);
133 static const struct xfrm_input_afinfo tunnel4_input_afinfo
= {
136 .callback
= tunnel4_rcv_cb
,
140 #if IS_ENABLED(CONFIG_IPV6)
141 static int tunnel64_rcv(struct sk_buff
*skb
)
143 struct xfrm_tunnel
*handler
;
145 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
148 for_each_tunnel_rcu(tunnel64_handlers
, handler
)
149 if (!handler
->handler(skb
))
152 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
160 #if IS_ENABLED(CONFIG_MPLS)
161 static int tunnelmpls4_rcv(struct sk_buff
*skb
)
163 struct xfrm_tunnel
*handler
;
165 if (!pskb_may_pull(skb
, sizeof(struct mpls_label
)))
168 for_each_tunnel_rcu(tunnelmpls4_handlers
, handler
)
169 if (!handler
->handler(skb
))
172 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
180 static int tunnel4_err(struct sk_buff
*skb
, u32 info
)
182 struct xfrm_tunnel
*handler
;
184 for_each_tunnel_rcu(tunnel4_handlers
, handler
)
185 if (!handler
->err_handler(skb
, info
))
191 #if IS_ENABLED(CONFIG_IPV6)
192 static int tunnel64_err(struct sk_buff
*skb
, u32 info
)
194 struct xfrm_tunnel
*handler
;
196 for_each_tunnel_rcu(tunnel64_handlers
, handler
)
197 if (!handler
->err_handler(skb
, info
))
204 #if IS_ENABLED(CONFIG_MPLS)
205 static int tunnelmpls4_err(struct sk_buff
*skb
, u32 info
)
207 struct xfrm_tunnel
*handler
;
209 for_each_tunnel_rcu(tunnelmpls4_handlers
, handler
)
210 if (!handler
->err_handler(skb
, info
))
217 static const struct net_protocol tunnel4_protocol
= {
218 .handler
= tunnel4_rcv
,
219 .err_handler
= tunnel4_err
,
224 #if IS_ENABLED(CONFIG_IPV6)
225 static const struct net_protocol tunnel64_protocol
= {
226 .handler
= tunnel64_rcv
,
227 .err_handler
= tunnel64_err
,
233 #if IS_ENABLED(CONFIG_MPLS)
234 static const struct net_protocol tunnelmpls4_protocol
= {
235 .handler
= tunnelmpls4_rcv
,
236 .err_handler
= tunnelmpls4_err
,
242 static int __init
tunnel4_init(void)
244 if (inet_add_protocol(&tunnel4_protocol
, IPPROTO_IPIP
))
246 #if IS_ENABLED(CONFIG_IPV6)
247 if (inet_add_protocol(&tunnel64_protocol
, IPPROTO_IPV6
)) {
248 inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
);
252 #if IS_ENABLED(CONFIG_MPLS)
253 if (inet_add_protocol(&tunnelmpls4_protocol
, IPPROTO_MPLS
)) {
254 inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
);
255 #if IS_ENABLED(CONFIG_IPV6)
256 inet_del_protocol(&tunnel64_protocol
, IPPROTO_IPV6
);
261 #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
262 if (xfrm_input_register_afinfo(&tunnel4_input_afinfo
)) {
263 inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
);
264 #if IS_ENABLED(CONFIG_IPV6)
265 inet_del_protocol(&tunnel64_protocol
, IPPROTO_IPV6
);
267 #if IS_ENABLED(CONFIG_MPLS)
268 inet_del_protocol(&tunnelmpls4_protocol
, IPPROTO_MPLS
);
276 pr_err("%s: can't add protocol\n", __func__
);
280 static void __exit
tunnel4_fini(void)
282 #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
283 if (xfrm_input_unregister_afinfo(&tunnel4_input_afinfo
))
284 pr_err("tunnel4 close: can't remove input afinfo\n");
286 #if IS_ENABLED(CONFIG_MPLS)
287 if (inet_del_protocol(&tunnelmpls4_protocol
, IPPROTO_MPLS
))
288 pr_err("tunnelmpls4 close: can't remove protocol\n");
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");
294 if (inet_del_protocol(&tunnel4_protocol
, IPPROTO_IPIP
))
295 pr_err("tunnel4 close: can't remove protocol\n");
298 module_init(tunnel4_init
);
299 module_exit(tunnel4_fini
);
300 MODULE_LICENSE("GPL");