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
;
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)
64 EXPORT_SYMBOL(xfrm4_rcv_cb
);
66 int xfrm4_rcv_encap(struct sk_buff
*skb
, int nexthdr
, __be32 spi
,
70 struct xfrm4_protocol
*handler
;
71 struct xfrm4_protocol __rcu
**head
= proto_handlers(nexthdr
);
73 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
= NULL
;
74 XFRM_SPI_SKB_CB(skb
)->family
= AF_INET
;
75 XFRM_SPI_SKB_CB(skb
)->daddroff
= offsetof(struct iphdr
, daddr
);
80 for_each_protocol_rcu(*head
, handler
)
81 if ((ret
= handler
->input_handler(skb
, nexthdr
, spi
, encap_type
)) != -EINVAL
)
85 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
90 EXPORT_SYMBOL(xfrm4_rcv_encap
);
92 static int xfrm4_esp_rcv(struct sk_buff
*skb
)
95 struct xfrm4_protocol
*handler
;
97 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
= NULL
;
99 for_each_protocol_rcu(esp4_handlers
, handler
)
100 if ((ret
= handler
->handler(skb
)) != -EINVAL
)
103 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
109 static void xfrm4_esp_err(struct sk_buff
*skb
, u32 info
)
111 struct xfrm4_protocol
*handler
;
113 for_each_protocol_rcu(esp4_handlers
, handler
)
114 if (!handler
->err_handler(skb
, info
))
118 static int xfrm4_ah_rcv(struct sk_buff
*skb
)
121 struct xfrm4_protocol
*handler
;
123 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
= NULL
;
125 for_each_protocol_rcu(ah4_handlers
, handler
)
126 if ((ret
= handler
->handler(skb
)) != -EINVAL
)
129 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
135 static void xfrm4_ah_err(struct sk_buff
*skb
, u32 info
)
137 struct xfrm4_protocol
*handler
;
139 for_each_protocol_rcu(ah4_handlers
, handler
)
140 if (!handler
->err_handler(skb
, info
))
144 static int xfrm4_ipcomp_rcv(struct sk_buff
*skb
)
147 struct xfrm4_protocol
*handler
;
149 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
= NULL
;
151 for_each_protocol_rcu(ipcomp4_handlers
, handler
)
152 if ((ret
= handler
->handler(skb
)) != -EINVAL
)
155 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
161 static void xfrm4_ipcomp_err(struct sk_buff
*skb
, u32 info
)
163 struct xfrm4_protocol
*handler
;
165 for_each_protocol_rcu(ipcomp4_handlers
, handler
)
166 if (!handler
->err_handler(skb
, info
))
170 static const struct net_protocol esp4_protocol
= {
171 .handler
= xfrm4_esp_rcv
,
172 .err_handler
= xfrm4_esp_err
,
177 static const struct net_protocol ah4_protocol
= {
178 .handler
= xfrm4_ah_rcv
,
179 .err_handler
= xfrm4_ah_err
,
184 static const struct net_protocol ipcomp4_protocol
= {
185 .handler
= xfrm4_ipcomp_rcv
,
186 .err_handler
= xfrm4_ipcomp_err
,
191 static struct xfrm_input_afinfo xfrm4_input_afinfo
= {
193 .owner
= THIS_MODULE
,
194 .callback
= xfrm4_rcv_cb
,
197 static inline const struct net_protocol
*netproto(unsigned char protocol
)
201 return &esp4_protocol
;
203 return &ah4_protocol
;
205 return &ipcomp4_protocol
;
211 int xfrm4_protocol_register(struct xfrm4_protocol
*handler
,
212 unsigned char protocol
)
214 struct xfrm4_protocol __rcu
**pprev
;
215 struct xfrm4_protocol
*t
;
216 bool add_netproto
= false;
218 int priority
= handler
->priority
;
220 if (!proto_handlers(protocol
) || !netproto(protocol
))
223 mutex_lock(&xfrm4_protocol_mutex
);
225 if (!rcu_dereference_protected(*proto_handlers(protocol
),
226 lockdep_is_held(&xfrm4_protocol_mutex
)))
229 for (pprev
= proto_handlers(protocol
);
230 (t
= rcu_dereference_protected(*pprev
,
231 lockdep_is_held(&xfrm4_protocol_mutex
))) != NULL
;
233 if (t
->priority
< priority
)
235 if (t
->priority
== priority
)
239 handler
->next
= *pprev
;
240 rcu_assign_pointer(*pprev
, handler
);
245 mutex_unlock(&xfrm4_protocol_mutex
);
248 if (inet_add_protocol(netproto(protocol
), protocol
)) {
249 pr_err("%s: can't add protocol\n", __func__
);
256 EXPORT_SYMBOL(xfrm4_protocol_register
);
258 int xfrm4_protocol_deregister(struct xfrm4_protocol
*handler
,
259 unsigned char protocol
)
261 struct xfrm4_protocol __rcu
**pprev
;
262 struct xfrm4_protocol
*t
;
265 if (!proto_handlers(protocol
) || !netproto(protocol
))
268 mutex_lock(&xfrm4_protocol_mutex
);
270 for (pprev
= proto_handlers(protocol
);
271 (t
= rcu_dereference_protected(*pprev
,
272 lockdep_is_held(&xfrm4_protocol_mutex
))) != NULL
;
275 *pprev
= handler
->next
;
281 if (!rcu_dereference_protected(*proto_handlers(protocol
),
282 lockdep_is_held(&xfrm4_protocol_mutex
))) {
283 if (inet_del_protocol(netproto(protocol
), protocol
) < 0) {
284 pr_err("%s: can't remove protocol\n", __func__
);
289 mutex_unlock(&xfrm4_protocol_mutex
);
295 EXPORT_SYMBOL(xfrm4_protocol_deregister
);
297 void __init
xfrm4_protocol_init(void)
299 xfrm_input_register_afinfo(&xfrm4_input_afinfo
);
301 EXPORT_SYMBOL(xfrm4_protocol_init
);