1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* xfrm4_protocol.c - Generic xfrm protocol multiplexer.
4 * Copyright (C) 2013 secunet Security Networks AG
13 #include <linux/init.h>
14 #include <linux/mutex.h>
15 #include <linux/skbuff.h>
18 #include <net/protocol.h>
21 static struct xfrm4_protocol __rcu *esp4_handlers __read_mostly;
22 static struct xfrm4_protocol __rcu *ah4_handlers __read_mostly;
23 static struct xfrm4_protocol __rcu *ipcomp4_handlers __read_mostly;
24 static DEFINE_MUTEX(xfrm4_protocol_mutex);
26 static inline struct xfrm4_protocol __rcu **proto_handlers(u8 protocol)
30 return &esp4_handlers;
34 return &ipcomp4_handlers;
40 #define for_each_protocol_rcu(head, handler) \
41 for (handler = rcu_dereference(head); \
43 handler = rcu_dereference(handler->next)) \
45 static int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
48 struct xfrm4_protocol *handler;
49 struct xfrm4_protocol __rcu **head = proto_handlers(protocol);
54 for_each_protocol_rcu(*head, handler)
55 if ((ret = handler->cb_handler(skb, err)) <= 0)
61 int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
65 struct xfrm4_protocol *handler;
66 struct xfrm4_protocol __rcu **head = proto_handlers(nexthdr);
68 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
69 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
70 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
75 for_each_protocol_rcu(*head, handler)
76 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
80 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
85 EXPORT_SYMBOL(xfrm4_rcv_encap);
87 static int xfrm4_esp_rcv(struct sk_buff *skb)
90 struct xfrm4_protocol *handler;
92 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
94 for_each_protocol_rcu(esp4_handlers, handler)
95 if ((ret = handler->handler(skb)) != -EINVAL)
98 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
104 static int xfrm4_esp_err(struct sk_buff *skb, u32 info)
106 struct xfrm4_protocol *handler;
108 for_each_protocol_rcu(esp4_handlers, handler)
109 if (!handler->err_handler(skb, info))
115 static int xfrm4_ah_rcv(struct sk_buff *skb)
118 struct xfrm4_protocol *handler;
120 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
122 for_each_protocol_rcu(ah4_handlers, handler)
123 if ((ret = handler->handler(skb)) != -EINVAL)
126 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
132 static int xfrm4_ah_err(struct sk_buff *skb, u32 info)
134 struct xfrm4_protocol *handler;
136 for_each_protocol_rcu(ah4_handlers, handler)
137 if (!handler->err_handler(skb, info))
143 static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
146 struct xfrm4_protocol *handler;
148 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
150 for_each_protocol_rcu(ipcomp4_handlers, handler)
151 if ((ret = handler->handler(skb)) != -EINVAL)
154 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
160 static int xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
162 struct xfrm4_protocol *handler;
164 for_each_protocol_rcu(ipcomp4_handlers, handler)
165 if (!handler->err_handler(skb, info))
171 static const struct net_protocol esp4_protocol = {
172 .handler = xfrm4_esp_rcv,
173 .err_handler = xfrm4_esp_err,
178 static const struct net_protocol ah4_protocol = {
179 .handler = xfrm4_ah_rcv,
180 .err_handler = xfrm4_ah_err,
185 static const struct net_protocol ipcomp4_protocol = {
186 .handler = xfrm4_ipcomp_rcv,
187 .err_handler = xfrm4_ipcomp_err,
192 static const struct xfrm_input_afinfo xfrm4_input_afinfo = {
194 .callback = xfrm4_rcv_cb,
197 static inline const struct net_protocol *netproto(unsigned char protocol)
201 return &esp4_protocol;
203 return &ah4_protocol;
205 return &ipcomp4_protocol;
211 int xfrm4_protocol_register(struct xfrm4_protocol *handler,
212 unsigned char protocol)
214 struct xfrm4_protocol __rcu **pprev;
215 struct xfrm4_protocol *t;
216 bool add_netproto = false;
218 int priority = handler->priority;
220 if (!proto_handlers(protocol) || !netproto(protocol))
223 mutex_lock(&xfrm4_protocol_mutex);
225 if (!rcu_dereference_protected(*proto_handlers(protocol),
226 lockdep_is_held(&xfrm4_protocol_mutex)))
229 for (pprev = proto_handlers(protocol);
230 (t = rcu_dereference_protected(*pprev,
231 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
233 if (t->priority < priority)
235 if (t->priority == priority)
239 handler->next = *pprev;
240 rcu_assign_pointer(*pprev, handler);
245 mutex_unlock(&xfrm4_protocol_mutex);
248 if (inet_add_protocol(netproto(protocol), protocol)) {
249 pr_err("%s: can't add protocol\n", __func__);
256 EXPORT_SYMBOL(xfrm4_protocol_register);
258 int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
259 unsigned char protocol)
261 struct xfrm4_protocol __rcu **pprev;
262 struct xfrm4_protocol *t;
265 if (!proto_handlers(protocol) || !netproto(protocol))
268 mutex_lock(&xfrm4_protocol_mutex);
270 for (pprev = proto_handlers(protocol);
271 (t = rcu_dereference_protected(*pprev,
272 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
275 *pprev = handler->next;
281 if (!rcu_dereference_protected(*proto_handlers(protocol),
282 lockdep_is_held(&xfrm4_protocol_mutex))) {
283 if (inet_del_protocol(netproto(protocol), protocol) < 0) {
284 pr_err("%s: can't remove protocol\n", __func__);
289 mutex_unlock(&xfrm4_protocol_mutex);
295 EXPORT_SYMBOL(xfrm4_protocol_deregister);
297 void __init xfrm4_protocol_init(void)
299 xfrm_input_register_afinfo(&xfrm4_input_afinfo);
301 EXPORT_SYMBOL(xfrm4_protocol_init);