1 /* xfrm6_protocol.c - Generic xfrm protocol multiplexer for ipv6.
3 * Copyright (C) 2013 secunet Security Networks AG
6 * Steffen Klassert <steffen.klassert@secunet.com>
9 * net/ipv4/xfrm4_protocol.c
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>
20 #include <linux/icmpv6.h>
22 #include <net/protocol.h>
25 static struct xfrm6_protocol __rcu *esp6_handlers __read_mostly;
26 static struct xfrm6_protocol __rcu *ah6_handlers __read_mostly;
27 static struct xfrm6_protocol __rcu *ipcomp6_handlers __read_mostly;
28 static DEFINE_MUTEX(xfrm6_protocol_mutex);
30 static inline struct xfrm6_protocol __rcu **proto_handlers(u8 protocol)
34 return &esp6_handlers;
38 return &ipcomp6_handlers;
44 #define for_each_protocol_rcu(head, handler) \
45 for (handler = rcu_dereference(head); \
47 handler = rcu_dereference(handler->next)) \
49 int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
52 struct xfrm6_protocol *handler;
54 for_each_protocol_rcu(*proto_handlers(protocol), handler)
55 if ((ret = handler->cb_handler(skb, err)) <= 0)
60 EXPORT_SYMBOL(xfrm6_rcv_cb);
62 static int xfrm6_esp_rcv(struct sk_buff *skb)
65 struct xfrm6_protocol *handler;
67 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
69 for_each_protocol_rcu(esp6_handlers, handler)
70 if ((ret = handler->handler(skb)) != -EINVAL)
73 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
79 static void xfrm6_esp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
80 u8 type, u8 code, int offset, __be32 info)
82 struct xfrm6_protocol *handler;
84 for_each_protocol_rcu(esp6_handlers, handler)
85 if (!handler->err_handler(skb, opt, type, code, offset, info))
89 static int xfrm6_ah_rcv(struct sk_buff *skb)
92 struct xfrm6_protocol *handler;
94 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
96 for_each_protocol_rcu(ah6_handlers, handler)
97 if ((ret = handler->handler(skb)) != -EINVAL)
100 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
106 static void xfrm6_ah_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
107 u8 type, u8 code, int offset, __be32 info)
109 struct xfrm6_protocol *handler;
111 for_each_protocol_rcu(ah6_handlers, handler)
112 if (!handler->err_handler(skb, opt, type, code, offset, info))
116 static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
119 struct xfrm6_protocol *handler;
121 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
123 for_each_protocol_rcu(ipcomp6_handlers, handler)
124 if ((ret = handler->handler(skb)) != -EINVAL)
127 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
133 static void xfrm6_ipcomp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
134 u8 type, u8 code, int offset, __be32 info)
136 struct xfrm6_protocol *handler;
138 for_each_protocol_rcu(ipcomp6_handlers, handler)
139 if (!handler->err_handler(skb, opt, type, code, offset, info))
143 static const struct inet6_protocol esp6_protocol = {
144 .handler = xfrm6_esp_rcv,
145 .err_handler = xfrm6_esp_err,
146 .flags = INET6_PROTO_NOPOLICY,
149 static const struct inet6_protocol ah6_protocol = {
150 .handler = xfrm6_ah_rcv,
151 .err_handler = xfrm6_ah_err,
152 .flags = INET6_PROTO_NOPOLICY,
155 static const struct inet6_protocol ipcomp6_protocol = {
156 .handler = xfrm6_ipcomp_rcv,
157 .err_handler = xfrm6_ipcomp_err,
158 .flags = INET6_PROTO_NOPOLICY,
161 static struct xfrm_input_afinfo xfrm6_input_afinfo = {
163 .owner = THIS_MODULE,
164 .callback = xfrm6_rcv_cb,
167 static inline const struct inet6_protocol *netproto(unsigned char protocol)
171 return &esp6_protocol;
173 return &ah6_protocol;
175 return &ipcomp6_protocol;
181 int xfrm6_protocol_register(struct xfrm6_protocol *handler,
182 unsigned char protocol)
184 struct xfrm6_protocol __rcu **pprev;
185 struct xfrm6_protocol *t;
186 bool add_netproto = false;
189 int priority = handler->priority;
191 mutex_lock(&xfrm6_protocol_mutex);
193 if (!rcu_dereference_protected(*proto_handlers(protocol),
194 lockdep_is_held(&xfrm6_protocol_mutex)))
197 for (pprev = proto_handlers(protocol);
198 (t = rcu_dereference_protected(*pprev,
199 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
201 if (t->priority < priority)
203 if (t->priority == priority)
207 handler->next = *pprev;
208 rcu_assign_pointer(*pprev, handler);
213 mutex_unlock(&xfrm6_protocol_mutex);
216 if (inet6_add_protocol(netproto(protocol), protocol)) {
217 pr_err("%s: can't add protocol\n", __func__);
224 EXPORT_SYMBOL(xfrm6_protocol_register);
226 int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
227 unsigned char protocol)
229 struct xfrm6_protocol __rcu **pprev;
230 struct xfrm6_protocol *t;
233 mutex_lock(&xfrm6_protocol_mutex);
235 for (pprev = proto_handlers(protocol);
236 (t = rcu_dereference_protected(*pprev,
237 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
240 *pprev = handler->next;
246 if (!rcu_dereference_protected(*proto_handlers(protocol),
247 lockdep_is_held(&xfrm6_protocol_mutex))) {
248 if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
249 pr_err("%s: can't remove protocol\n", __func__);
254 mutex_unlock(&xfrm6_protocol_mutex);
260 EXPORT_SYMBOL(xfrm6_protocol_deregister);
262 int __init xfrm6_protocol_init(void)
264 return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
267 void xfrm6_protocol_fini(void)
269 xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);