1 /* xfrm4_protocol.c - Generic xfrm protocol multiplexer.
3 * Copyright (C) 2013 secunet Security Networks AG
6 * Steffen Klassert <steffen.klassert@secunet.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/init.h>
18 #include <linux/mutex.h>
19 #include <linux/skbuff.h>
22 #include <net/protocol.h>
25 static struct xfrm4_protocol __rcu *esp4_handlers __read_mostly;
26 static struct xfrm4_protocol __rcu *ah4_handlers __read_mostly;
27 static struct xfrm4_protocol __rcu *ipcomp4_handlers __read_mostly;
28 static DEFINE_MUTEX(xfrm4_protocol_mutex);
30 static inline struct xfrm4_protocol __rcu **proto_handlers(u8 protocol)
34 return &esp4_handlers;
38 return &ipcomp4_handlers;
44 #define for_each_protocol_rcu(head, handler) \
45 for (handler = rcu_dereference(head); \
47 handler = rcu_dereference(handler->next)) \
49 int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
52 struct xfrm4_protocol *handler;
54 for_each_protocol_rcu(*proto_handlers(protocol), handler)
55 if ((ret = handler->cb_handler(skb, err)) <= 0)
60 EXPORT_SYMBOL(xfrm4_rcv_cb);
62 int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
66 struct xfrm4_protocol *handler;
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);
72 for_each_protocol_rcu(*proto_handlers(nexthdr), handler)
73 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
76 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
81 EXPORT_SYMBOL(xfrm4_rcv_encap);
83 static int xfrm4_esp_rcv(struct sk_buff *skb)
86 struct xfrm4_protocol *handler;
88 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
90 for_each_protocol_rcu(esp4_handlers, handler)
91 if ((ret = handler->handler(skb)) != -EINVAL)
94 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
100 static void xfrm4_esp_err(struct sk_buff *skb, u32 info)
102 struct xfrm4_protocol *handler;
104 for_each_protocol_rcu(esp4_handlers, handler)
105 if (!handler->err_handler(skb, info))
109 static int xfrm4_ah_rcv(struct sk_buff *skb)
112 struct xfrm4_protocol *handler;
114 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
116 for_each_protocol_rcu(ah4_handlers, handler)
117 if ((ret = handler->handler(skb)) != -EINVAL)
120 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
126 static void xfrm4_ah_err(struct sk_buff *skb, u32 info)
128 struct xfrm4_protocol *handler;
130 for_each_protocol_rcu(ah4_handlers, handler)
131 if (!handler->err_handler(skb, info))
135 static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
138 struct xfrm4_protocol *handler;
140 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
142 for_each_protocol_rcu(ipcomp4_handlers, handler)
143 if ((ret = handler->handler(skb)) != -EINVAL)
146 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
152 static void xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
154 struct xfrm4_protocol *handler;
156 for_each_protocol_rcu(ipcomp4_handlers, handler)
157 if (!handler->err_handler(skb, info))
161 static const struct net_protocol esp4_protocol = {
162 .handler = xfrm4_esp_rcv,
163 .err_handler = xfrm4_esp_err,
168 static const struct net_protocol ah4_protocol = {
169 .handler = xfrm4_ah_rcv,
170 .err_handler = xfrm4_ah_err,
175 static const struct net_protocol ipcomp4_protocol = {
176 .handler = xfrm4_ipcomp_rcv,
177 .err_handler = xfrm4_ipcomp_err,
182 static struct xfrm_input_afinfo xfrm4_input_afinfo = {
184 .owner = THIS_MODULE,
185 .callback = xfrm4_rcv_cb,
188 static inline const struct net_protocol *netproto(unsigned char protocol)
192 return &esp4_protocol;
194 return &ah4_protocol;
196 return &ipcomp4_protocol;
202 int xfrm4_protocol_register(struct xfrm4_protocol *handler,
203 unsigned char protocol)
205 struct xfrm4_protocol __rcu **pprev;
206 struct xfrm4_protocol *t;
207 bool add_netproto = false;
209 int priority = handler->priority;
211 mutex_lock(&xfrm4_protocol_mutex);
213 if (!rcu_dereference_protected(*proto_handlers(protocol),
214 lockdep_is_held(&xfrm4_protocol_mutex)))
217 for (pprev = proto_handlers(protocol);
218 (t = rcu_dereference_protected(*pprev,
219 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
221 if (t->priority < priority)
223 if (t->priority == priority)
227 handler->next = *pprev;
228 rcu_assign_pointer(*pprev, handler);
233 mutex_unlock(&xfrm4_protocol_mutex);
236 if (inet_add_protocol(netproto(protocol), protocol)) {
237 pr_err("%s: can't add protocol\n", __func__);
244 EXPORT_SYMBOL(xfrm4_protocol_register);
246 int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
247 unsigned char protocol)
249 struct xfrm4_protocol __rcu **pprev;
250 struct xfrm4_protocol *t;
253 mutex_lock(&xfrm4_protocol_mutex);
255 for (pprev = proto_handlers(protocol);
256 (t = rcu_dereference_protected(*pprev,
257 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
260 *pprev = handler->next;
266 if (!rcu_dereference_protected(*proto_handlers(protocol),
267 lockdep_is_held(&xfrm4_protocol_mutex))) {
268 if (inet_del_protocol(netproto(protocol), protocol) < 0) {
269 pr_err("%s: can't remove protocol\n", __func__);
274 mutex_unlock(&xfrm4_protocol_mutex);
280 EXPORT_SYMBOL(xfrm4_protocol_deregister);
282 void __init xfrm4_protocol_init(void)
284 xfrm_input_register_afinfo(&xfrm4_input_afinfo);
286 EXPORT_SYMBOL(xfrm4_protocol_init);