1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* xfrm6_protocol.c - Generic xfrm protocol multiplexer for ipv6.
4 * Copyright (C) 2013 secunet Security Networks AG
7 * Steffen Klassert <steffen.klassert@secunet.com>
10 * net/ipv4/xfrm4_protocol.c
13 #include <linux/init.h>
14 #include <linux/mutex.h>
15 #include <linux/skbuff.h>
16 #include <linux/icmpv6.h>
18 #include <net/protocol.h>
21 static struct xfrm6_protocol __rcu *esp6_handlers __read_mostly;
22 static struct xfrm6_protocol __rcu *ah6_handlers __read_mostly;
23 static struct xfrm6_protocol __rcu *ipcomp6_handlers __read_mostly;
24 static DEFINE_MUTEX(xfrm6_protocol_mutex);
26 static inline struct xfrm6_protocol __rcu **proto_handlers(u8 protocol)
30 return &esp6_handlers;
34 return &ipcomp6_handlers;
40 #define for_each_protocol_rcu(head, handler) \
41 for (handler = rcu_dereference(head); \
43 handler = rcu_dereference(handler->next)) \
45 static int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
48 struct xfrm6_protocol *handler;
49 struct xfrm6_protocol __rcu **head = proto_handlers(protocol);
54 for_each_protocol_rcu(*proto_handlers(protocol), handler)
55 if ((ret = handler->cb_handler(skb, err)) <= 0)
61 static int xfrm6_esp_rcv(struct sk_buff *skb)
64 struct xfrm6_protocol *handler;
66 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
68 for_each_protocol_rcu(esp6_handlers, handler)
69 if ((ret = handler->handler(skb)) != -EINVAL)
72 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
78 static int xfrm6_esp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
79 u8 type, u8 code, int offset, __be32 info)
81 struct xfrm6_protocol *handler;
83 for_each_protocol_rcu(esp6_handlers, handler)
84 if (!handler->err_handler(skb, opt, type, code, offset, info))
90 static int xfrm6_ah_rcv(struct sk_buff *skb)
93 struct xfrm6_protocol *handler;
95 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
97 for_each_protocol_rcu(ah6_handlers, handler)
98 if ((ret = handler->handler(skb)) != -EINVAL)
101 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
107 static int xfrm6_ah_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
108 u8 type, u8 code, int offset, __be32 info)
110 struct xfrm6_protocol *handler;
112 for_each_protocol_rcu(ah6_handlers, handler)
113 if (!handler->err_handler(skb, opt, type, code, offset, info))
119 static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
122 struct xfrm6_protocol *handler;
124 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
126 for_each_protocol_rcu(ipcomp6_handlers, handler)
127 if ((ret = handler->handler(skb)) != -EINVAL)
130 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
136 static int xfrm6_ipcomp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
137 u8 type, u8 code, int offset, __be32 info)
139 struct xfrm6_protocol *handler;
141 for_each_protocol_rcu(ipcomp6_handlers, handler)
142 if (!handler->err_handler(skb, opt, type, code, offset, info))
148 static const struct inet6_protocol esp6_protocol = {
149 .handler = xfrm6_esp_rcv,
150 .err_handler = xfrm6_esp_err,
151 .flags = INET6_PROTO_NOPOLICY,
154 static const struct inet6_protocol ah6_protocol = {
155 .handler = xfrm6_ah_rcv,
156 .err_handler = xfrm6_ah_err,
157 .flags = INET6_PROTO_NOPOLICY,
160 static const struct inet6_protocol ipcomp6_protocol = {
161 .handler = xfrm6_ipcomp_rcv,
162 .err_handler = xfrm6_ipcomp_err,
163 .flags = INET6_PROTO_NOPOLICY,
166 static const struct xfrm_input_afinfo xfrm6_input_afinfo = {
168 .callback = xfrm6_rcv_cb,
171 static inline const struct inet6_protocol *netproto(unsigned char protocol)
175 return &esp6_protocol;
177 return &ah6_protocol;
179 return &ipcomp6_protocol;
185 int xfrm6_protocol_register(struct xfrm6_protocol *handler,
186 unsigned char protocol)
188 struct xfrm6_protocol __rcu **pprev;
189 struct xfrm6_protocol *t;
190 bool add_netproto = false;
192 int priority = handler->priority;
194 if (!proto_handlers(protocol) || !netproto(protocol))
197 mutex_lock(&xfrm6_protocol_mutex);
199 if (!rcu_dereference_protected(*proto_handlers(protocol),
200 lockdep_is_held(&xfrm6_protocol_mutex)))
203 for (pprev = proto_handlers(protocol);
204 (t = rcu_dereference_protected(*pprev,
205 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
207 if (t->priority < priority)
209 if (t->priority == priority)
213 handler->next = *pprev;
214 rcu_assign_pointer(*pprev, handler);
219 mutex_unlock(&xfrm6_protocol_mutex);
222 if (inet6_add_protocol(netproto(protocol), protocol)) {
223 pr_err("%s: can't add protocol\n", __func__);
230 EXPORT_SYMBOL(xfrm6_protocol_register);
232 int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
233 unsigned char protocol)
235 struct xfrm6_protocol __rcu **pprev;
236 struct xfrm6_protocol *t;
239 if (!proto_handlers(protocol) || !netproto(protocol))
242 mutex_lock(&xfrm6_protocol_mutex);
244 for (pprev = proto_handlers(protocol);
245 (t = rcu_dereference_protected(*pprev,
246 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
249 *pprev = handler->next;
255 if (!rcu_dereference_protected(*proto_handlers(protocol),
256 lockdep_is_held(&xfrm6_protocol_mutex))) {
257 if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
258 pr_err("%s: can't remove protocol\n", __func__);
263 mutex_unlock(&xfrm6_protocol_mutex);
269 EXPORT_SYMBOL(xfrm6_protocol_deregister);
271 int __init xfrm6_protocol_init(void)
273 return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
276 void xfrm6_protocol_fini(void)
278 xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);