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);
76 const struct iphdr *iph = ip_hdr(skb);
78 if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
83 for_each_protocol_rcu(*head, handler)
84 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
88 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
94 EXPORT_SYMBOL(xfrm4_rcv_encap);
96 static int xfrm4_esp_rcv(struct sk_buff *skb)
99 struct xfrm4_protocol *handler;
101 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
103 for_each_protocol_rcu(esp4_handlers, handler)
104 if ((ret = handler->handler(skb)) != -EINVAL)
107 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
113 static int xfrm4_esp_err(struct sk_buff *skb, u32 info)
115 struct xfrm4_protocol *handler;
117 for_each_protocol_rcu(esp4_handlers, handler)
118 if (!handler->err_handler(skb, info))
124 static int xfrm4_ah_rcv(struct sk_buff *skb)
127 struct xfrm4_protocol *handler;
129 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
131 for_each_protocol_rcu(ah4_handlers, handler)
132 if ((ret = handler->handler(skb)) != -EINVAL)
135 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
141 static int xfrm4_ah_err(struct sk_buff *skb, u32 info)
143 struct xfrm4_protocol *handler;
145 for_each_protocol_rcu(ah4_handlers, handler)
146 if (!handler->err_handler(skb, info))
152 static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
155 struct xfrm4_protocol *handler;
157 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
159 for_each_protocol_rcu(ipcomp4_handlers, handler)
160 if ((ret = handler->handler(skb)) != -EINVAL)
163 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
169 static int xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
171 struct xfrm4_protocol *handler;
173 for_each_protocol_rcu(ipcomp4_handlers, handler)
174 if (!handler->err_handler(skb, info))
180 static const struct net_protocol esp4_protocol = {
181 .handler = xfrm4_esp_rcv,
182 .err_handler = xfrm4_esp_err,
186 static const struct net_protocol ah4_protocol = {
187 .handler = xfrm4_ah_rcv,
188 .err_handler = xfrm4_ah_err,
192 static const struct net_protocol ipcomp4_protocol = {
193 .handler = xfrm4_ipcomp_rcv,
194 .err_handler = xfrm4_ipcomp_err,
198 static const struct xfrm_input_afinfo xfrm4_input_afinfo = {
200 .callback = xfrm4_rcv_cb,
203 static inline const struct net_protocol *netproto(unsigned char protocol)
207 return &esp4_protocol;
209 return &ah4_protocol;
211 return &ipcomp4_protocol;
217 int xfrm4_protocol_register(struct xfrm4_protocol *handler,
218 unsigned char protocol)
220 struct xfrm4_protocol __rcu **pprev;
221 struct xfrm4_protocol *t;
222 bool add_netproto = false;
224 int priority = handler->priority;
226 if (!proto_handlers(protocol) || !netproto(protocol))
229 mutex_lock(&xfrm4_protocol_mutex);
231 if (!rcu_dereference_protected(*proto_handlers(protocol),
232 lockdep_is_held(&xfrm4_protocol_mutex)))
235 for (pprev = proto_handlers(protocol);
236 (t = rcu_dereference_protected(*pprev,
237 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
239 if (t->priority < priority)
241 if (t->priority == priority)
245 handler->next = *pprev;
246 rcu_assign_pointer(*pprev, handler);
251 mutex_unlock(&xfrm4_protocol_mutex);
254 if (inet_add_protocol(netproto(protocol), protocol)) {
255 pr_err("%s: can't add protocol\n", __func__);
262 EXPORT_SYMBOL(xfrm4_protocol_register);
264 int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
265 unsigned char protocol)
267 struct xfrm4_protocol __rcu **pprev;
268 struct xfrm4_protocol *t;
271 if (!proto_handlers(protocol) || !netproto(protocol))
274 mutex_lock(&xfrm4_protocol_mutex);
276 for (pprev = proto_handlers(protocol);
277 (t = rcu_dereference_protected(*pprev,
278 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
281 *pprev = handler->next;
287 if (!rcu_dereference_protected(*proto_handlers(protocol),
288 lockdep_is_held(&xfrm4_protocol_mutex))) {
289 if (inet_del_protocol(netproto(protocol), protocol) < 0) {
290 pr_err("%s: can't remove protocol\n", __func__);
295 mutex_unlock(&xfrm4_protocol_mutex);
301 EXPORT_SYMBOL(xfrm4_protocol_deregister);
303 void __init xfrm4_protocol_init(void)
305 xfrm_input_register_afinfo(&xfrm4_input_afinfo);