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 static int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
52 struct xfrm4_protocol *handler;
53 struct xfrm4_protocol __rcu **head = proto_handlers(protocol);
58 for_each_protocol_rcu(*head, handler)
59 if ((ret = handler->cb_handler(skb, err)) <= 0)
65 int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
69 struct xfrm4_protocol *handler;
70 struct xfrm4_protocol __rcu **head = proto_handlers(nexthdr);
72 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
73 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
74 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
79 for_each_protocol_rcu(*head, handler)
80 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
84 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
89 EXPORT_SYMBOL(xfrm4_rcv_encap);
91 static int xfrm4_esp_rcv(struct sk_buff *skb)
94 struct xfrm4_protocol *handler;
96 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
98 for_each_protocol_rcu(esp4_handlers, handler)
99 if ((ret = handler->handler(skb)) != -EINVAL)
102 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
108 static int xfrm4_esp_err(struct sk_buff *skb, u32 info)
110 struct xfrm4_protocol *handler;
112 for_each_protocol_rcu(esp4_handlers, handler)
113 if (!handler->err_handler(skb, info))
119 static int xfrm4_ah_rcv(struct sk_buff *skb)
122 struct xfrm4_protocol *handler;
124 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
126 for_each_protocol_rcu(ah4_handlers, handler)
127 if ((ret = handler->handler(skb)) != -EINVAL)
130 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
136 static int xfrm4_ah_err(struct sk_buff *skb, u32 info)
138 struct xfrm4_protocol *handler;
140 for_each_protocol_rcu(ah4_handlers, handler)
141 if (!handler->err_handler(skb, info))
147 static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
150 struct xfrm4_protocol *handler;
152 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
154 for_each_protocol_rcu(ipcomp4_handlers, handler)
155 if ((ret = handler->handler(skb)) != -EINVAL)
158 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
164 static int xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
166 struct xfrm4_protocol *handler;
168 for_each_protocol_rcu(ipcomp4_handlers, handler)
169 if (!handler->err_handler(skb, info))
175 static const struct net_protocol esp4_protocol = {
176 .handler = xfrm4_esp_rcv,
177 .err_handler = xfrm4_esp_err,
182 static const struct net_protocol ah4_protocol = {
183 .handler = xfrm4_ah_rcv,
184 .err_handler = xfrm4_ah_err,
189 static const struct net_protocol ipcomp4_protocol = {
190 .handler = xfrm4_ipcomp_rcv,
191 .err_handler = xfrm4_ipcomp_err,
196 static const struct xfrm_input_afinfo xfrm4_input_afinfo = {
198 .callback = xfrm4_rcv_cb,
201 static inline const struct net_protocol *netproto(unsigned char protocol)
205 return &esp4_protocol;
207 return &ah4_protocol;
209 return &ipcomp4_protocol;
215 int xfrm4_protocol_register(struct xfrm4_protocol *handler,
216 unsigned char protocol)
218 struct xfrm4_protocol __rcu **pprev;
219 struct xfrm4_protocol *t;
220 bool add_netproto = false;
222 int priority = handler->priority;
224 if (!proto_handlers(protocol) || !netproto(protocol))
227 mutex_lock(&xfrm4_protocol_mutex);
229 if (!rcu_dereference_protected(*proto_handlers(protocol),
230 lockdep_is_held(&xfrm4_protocol_mutex)))
233 for (pprev = proto_handlers(protocol);
234 (t = rcu_dereference_protected(*pprev,
235 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
237 if (t->priority < priority)
239 if (t->priority == priority)
243 handler->next = *pprev;
244 rcu_assign_pointer(*pprev, handler);
249 mutex_unlock(&xfrm4_protocol_mutex);
252 if (inet_add_protocol(netproto(protocol), protocol)) {
253 pr_err("%s: can't add protocol\n", __func__);
260 EXPORT_SYMBOL(xfrm4_protocol_register);
262 int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
263 unsigned char protocol)
265 struct xfrm4_protocol __rcu **pprev;
266 struct xfrm4_protocol *t;
269 if (!proto_handlers(protocol) || !netproto(protocol))
272 mutex_lock(&xfrm4_protocol_mutex);
274 for (pprev = proto_handlers(protocol);
275 (t = rcu_dereference_protected(*pprev,
276 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
279 *pprev = handler->next;
285 if (!rcu_dereference_protected(*proto_handlers(protocol),
286 lockdep_is_held(&xfrm4_protocol_mutex))) {
287 if (inet_del_protocol(netproto(protocol), protocol) < 0) {
288 pr_err("%s: can't remove protocol\n", __func__);
293 mutex_unlock(&xfrm4_protocol_mutex);
299 EXPORT_SYMBOL(xfrm4_protocol_deregister);
301 void __init xfrm4_protocol_init(void)
303 xfrm_input_register_afinfo(&xfrm4_input_afinfo);
305 EXPORT_SYMBOL(xfrm4_protocol_init);