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_SPI_SKB_CB(skb)->family = AF_INET;
69 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
71 for_each_protocol_rcu(*proto_handlers(nexthdr), handler)
72 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
75 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
80 EXPORT_SYMBOL(xfrm4_rcv_encap);
82 static int xfrm4_esp_rcv(struct sk_buff *skb)
85 struct xfrm4_protocol *handler;
87 for_each_protocol_rcu(esp4_handlers, handler)
88 if ((ret = handler->handler(skb)) != -EINVAL)
91 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
97 static void xfrm4_esp_err(struct sk_buff *skb, u32 info)
99 struct xfrm4_protocol *handler;
101 for_each_protocol_rcu(esp4_handlers, handler)
102 if (!handler->err_handler(skb, info))
106 static int xfrm4_ah_rcv(struct sk_buff *skb)
109 struct xfrm4_protocol *handler;
111 for_each_protocol_rcu(ah4_handlers, handler)
112 if ((ret = handler->handler(skb)) != -EINVAL)
115 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
121 static void xfrm4_ah_err(struct sk_buff *skb, u32 info)
123 struct xfrm4_protocol *handler;
125 for_each_protocol_rcu(ah4_handlers, handler)
126 if (!handler->err_handler(skb, info))
130 static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
133 struct xfrm4_protocol *handler;
135 for_each_protocol_rcu(ipcomp4_handlers, handler)
136 if ((ret = handler->handler(skb)) != -EINVAL)
139 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
145 static void xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
147 struct xfrm4_protocol *handler;
149 for_each_protocol_rcu(ipcomp4_handlers, handler)
150 if (!handler->err_handler(skb, info))
154 static const struct net_protocol esp4_protocol = {
155 .handler = xfrm4_esp_rcv,
156 .err_handler = xfrm4_esp_err,
161 static const struct net_protocol ah4_protocol = {
162 .handler = xfrm4_ah_rcv,
163 .err_handler = xfrm4_ah_err,
168 static const struct net_protocol ipcomp4_protocol = {
169 .handler = xfrm4_ipcomp_rcv,
170 .err_handler = xfrm4_ipcomp_err,
175 static inline const struct net_protocol *netproto(unsigned char protocol)
179 return &esp4_protocol;
181 return &ah4_protocol;
183 return &ipcomp4_protocol;
189 int xfrm4_protocol_register(struct xfrm4_protocol *handler,
190 unsigned char protocol)
192 struct xfrm4_protocol __rcu **pprev;
193 struct xfrm4_protocol *t;
194 bool add_netproto = false;
197 int priority = handler->priority;
199 mutex_lock(&xfrm4_protocol_mutex);
201 if (!rcu_dereference_protected(*proto_handlers(protocol),
202 lockdep_is_held(&xfrm4_protocol_mutex)))
205 for (pprev = proto_handlers(protocol);
206 (t = rcu_dereference_protected(*pprev,
207 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
209 if (t->priority < priority)
211 if (t->priority == priority)
215 handler->next = *pprev;
216 rcu_assign_pointer(*pprev, handler);
221 mutex_unlock(&xfrm4_protocol_mutex);
224 if (inet_add_protocol(netproto(protocol), protocol)) {
225 pr_err("%s: can't add protocol\n", __func__);
232 EXPORT_SYMBOL(xfrm4_protocol_register);
234 int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
235 unsigned char protocol)
237 struct xfrm4_protocol __rcu **pprev;
238 struct xfrm4_protocol *t;
241 mutex_lock(&xfrm4_protocol_mutex);
243 for (pprev = proto_handlers(protocol);
244 (t = rcu_dereference_protected(*pprev,
245 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
248 *pprev = handler->next;
254 if (!rcu_dereference_protected(*proto_handlers(protocol),
255 lockdep_is_held(&xfrm4_protocol_mutex))) {
256 if (inet_del_protocol(netproto(protocol), protocol) < 0) {
257 pr_err("%s: can't remove protocol\n", __func__);
262 mutex_unlock(&xfrm4_protocol_mutex);
268 EXPORT_SYMBOL(xfrm4_protocol_deregister);