1 /* xfrm6_protocol.c - Generic xfrm protocol multiplexer for ipv6.
3 * Copyright (C) 2013 secunet Security Networks AG
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;
53 struct xfrm6_protocol __rcu **head = proto_handlers(protocol);
58 for_each_protocol_rcu(*proto_handlers(protocol), handler)
59 if ((ret = handler->cb_handler(skb, err)) <= 0)
64 EXPORT_SYMBOL(xfrm6_rcv_cb);
66 static int xfrm6_esp_rcv(struct sk_buff *skb)
69 struct xfrm6_protocol *handler;
71 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
73 for_each_protocol_rcu(esp6_handlers, handler)
74 if ((ret = handler->handler(skb)) != -EINVAL)
77 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
83 static int xfrm6_esp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
84 u8 type, u8 code, int offset, __be32 info)
86 struct xfrm6_protocol *handler;
88 for_each_protocol_rcu(esp6_handlers, handler)
89 if (!handler->err_handler(skb, opt, type, code, offset, info))
95 static int xfrm6_ah_rcv(struct sk_buff *skb)
98 struct xfrm6_protocol *handler;
100 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
102 for_each_protocol_rcu(ah6_handlers, handler)
103 if ((ret = handler->handler(skb)) != -EINVAL)
106 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
112 static int xfrm6_ah_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
113 u8 type, u8 code, int offset, __be32 info)
115 struct xfrm6_protocol *handler;
117 for_each_protocol_rcu(ah6_handlers, handler)
118 if (!handler->err_handler(skb, opt, type, code, offset, info))
124 static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
127 struct xfrm6_protocol *handler;
129 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
131 for_each_protocol_rcu(ipcomp6_handlers, handler)
132 if ((ret = handler->handler(skb)) != -EINVAL)
135 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
141 static int xfrm6_ipcomp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
142 u8 type, u8 code, int offset, __be32 info)
144 struct xfrm6_protocol *handler;
146 for_each_protocol_rcu(ipcomp6_handlers, handler)
147 if (!handler->err_handler(skb, opt, type, code, offset, info))
153 static const struct inet6_protocol esp6_protocol = {
154 .handler = xfrm6_esp_rcv,
155 .err_handler = xfrm6_esp_err,
156 .flags = INET6_PROTO_NOPOLICY,
159 static const struct inet6_protocol ah6_protocol = {
160 .handler = xfrm6_ah_rcv,
161 .err_handler = xfrm6_ah_err,
162 .flags = INET6_PROTO_NOPOLICY,
165 static const struct inet6_protocol ipcomp6_protocol = {
166 .handler = xfrm6_ipcomp_rcv,
167 .err_handler = xfrm6_ipcomp_err,
168 .flags = INET6_PROTO_NOPOLICY,
171 static const struct xfrm_input_afinfo xfrm6_input_afinfo = {
173 .callback = xfrm6_rcv_cb,
176 static inline const struct inet6_protocol *netproto(unsigned char protocol)
180 return &esp6_protocol;
182 return &ah6_protocol;
184 return &ipcomp6_protocol;
190 int xfrm6_protocol_register(struct xfrm6_protocol *handler,
191 unsigned char protocol)
193 struct xfrm6_protocol __rcu **pprev;
194 struct xfrm6_protocol *t;
195 bool add_netproto = false;
197 int priority = handler->priority;
199 if (!proto_handlers(protocol) || !netproto(protocol))
202 mutex_lock(&xfrm6_protocol_mutex);
204 if (!rcu_dereference_protected(*proto_handlers(protocol),
205 lockdep_is_held(&xfrm6_protocol_mutex)))
208 for (pprev = proto_handlers(protocol);
209 (t = rcu_dereference_protected(*pprev,
210 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
212 if (t->priority < priority)
214 if (t->priority == priority)
218 handler->next = *pprev;
219 rcu_assign_pointer(*pprev, handler);
224 mutex_unlock(&xfrm6_protocol_mutex);
227 if (inet6_add_protocol(netproto(protocol), protocol)) {
228 pr_err("%s: can't add protocol\n", __func__);
235 EXPORT_SYMBOL(xfrm6_protocol_register);
237 int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
238 unsigned char protocol)
240 struct xfrm6_protocol __rcu **pprev;
241 struct xfrm6_protocol *t;
244 if (!proto_handlers(protocol) || !netproto(protocol))
247 mutex_lock(&xfrm6_protocol_mutex);
249 for (pprev = proto_handlers(protocol);
250 (t = rcu_dereference_protected(*pprev,
251 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
254 *pprev = handler->next;
260 if (!rcu_dereference_protected(*proto_handlers(protocol),
261 lockdep_is_held(&xfrm6_protocol_mutex))) {
262 if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
263 pr_err("%s: can't remove protocol\n", __func__);
268 mutex_unlock(&xfrm6_protocol_mutex);
274 EXPORT_SYMBOL(xfrm6_protocol_deregister);
276 int __init xfrm6_protocol_init(void)
278 return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
281 void xfrm6_protocol_fini(void)
283 xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);