Loading...
1/* xfrm6_protocol.c - Generic xfrm protocol multiplexer for ipv6.
2 *
3 * Copyright (C) 2013 secunet Security Networks AG
4 *
5 * Author:
6 * Steffen Klassert <steffen.klassert@secunet.com>
7 *
8 * Based on:
9 * net/ipv4/xfrm4_protocol.c
10 *
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.
15 */
16
17#include <linux/init.h>
18#include <linux/mutex.h>
19#include <linux/skbuff.h>
20#include <linux/icmpv6.h>
21#include <net/ipv6.h>
22#include <net/protocol.h>
23#include <net/xfrm.h>
24
25static struct xfrm6_protocol __rcu *esp6_handlers __read_mostly;
26static struct xfrm6_protocol __rcu *ah6_handlers __read_mostly;
27static struct xfrm6_protocol __rcu *ipcomp6_handlers __read_mostly;
28static DEFINE_MUTEX(xfrm6_protocol_mutex);
29
30static inline struct xfrm6_protocol __rcu **proto_handlers(u8 protocol)
31{
32 switch (protocol) {
33 case IPPROTO_ESP:
34 return &esp6_handlers;
35 case IPPROTO_AH:
36 return &ah6_handlers;
37 case IPPROTO_COMP:
38 return &ipcomp6_handlers;
39 }
40
41 return NULL;
42}
43
44#define for_each_protocol_rcu(head, handler) \
45 for (handler = rcu_dereference(head); \
46 handler != NULL; \
47 handler = rcu_dereference(handler->next)) \
48
49int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
50{
51 int ret;
52 struct xfrm6_protocol *handler;
53 struct xfrm6_protocol __rcu **head = proto_handlers(protocol);
54
55 if (!head)
56 return 0;
57
58 for_each_protocol_rcu(*proto_handlers(protocol), handler)
59 if ((ret = handler->cb_handler(skb, err)) <= 0)
60 return ret;
61
62 return 0;
63}
64EXPORT_SYMBOL(xfrm6_rcv_cb);
65
66static int xfrm6_esp_rcv(struct sk_buff *skb)
67{
68 int ret;
69 struct xfrm6_protocol *handler;
70
71 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
72
73 for_each_protocol_rcu(esp6_handlers, handler)
74 if ((ret = handler->handler(skb)) != -EINVAL)
75 return ret;
76
77 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
78
79 kfree_skb(skb);
80 return 0;
81}
82
83static void xfrm6_esp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
84 u8 type, u8 code, int offset, __be32 info)
85{
86 struct xfrm6_protocol *handler;
87
88 for_each_protocol_rcu(esp6_handlers, handler)
89 if (!handler->err_handler(skb, opt, type, code, offset, info))
90 break;
91}
92
93static int xfrm6_ah_rcv(struct sk_buff *skb)
94{
95 int ret;
96 struct xfrm6_protocol *handler;
97
98 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
99
100 for_each_protocol_rcu(ah6_handlers, handler)
101 if ((ret = handler->handler(skb)) != -EINVAL)
102 return ret;
103
104 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
105
106 kfree_skb(skb);
107 return 0;
108}
109
110static void xfrm6_ah_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
111 u8 type, u8 code, int offset, __be32 info)
112{
113 struct xfrm6_protocol *handler;
114
115 for_each_protocol_rcu(ah6_handlers, handler)
116 if (!handler->err_handler(skb, opt, type, code, offset, info))
117 break;
118}
119
120static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
121{
122 int ret;
123 struct xfrm6_protocol *handler;
124
125 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
126
127 for_each_protocol_rcu(ipcomp6_handlers, handler)
128 if ((ret = handler->handler(skb)) != -EINVAL)
129 return ret;
130
131 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
132
133 kfree_skb(skb);
134 return 0;
135}
136
137static void xfrm6_ipcomp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
138 u8 type, u8 code, int offset, __be32 info)
139{
140 struct xfrm6_protocol *handler;
141
142 for_each_protocol_rcu(ipcomp6_handlers, handler)
143 if (!handler->err_handler(skb, opt, type, code, offset, info))
144 break;
145}
146
147static const struct inet6_protocol esp6_protocol = {
148 .handler = xfrm6_esp_rcv,
149 .err_handler = xfrm6_esp_err,
150 .flags = INET6_PROTO_NOPOLICY,
151};
152
153static const struct inet6_protocol ah6_protocol = {
154 .handler = xfrm6_ah_rcv,
155 .err_handler = xfrm6_ah_err,
156 .flags = INET6_PROTO_NOPOLICY,
157};
158
159static const struct inet6_protocol ipcomp6_protocol = {
160 .handler = xfrm6_ipcomp_rcv,
161 .err_handler = xfrm6_ipcomp_err,
162 .flags = INET6_PROTO_NOPOLICY,
163};
164
165static const struct xfrm_input_afinfo xfrm6_input_afinfo = {
166 .family = AF_INET6,
167 .callback = xfrm6_rcv_cb,
168};
169
170static inline const struct inet6_protocol *netproto(unsigned char protocol)
171{
172 switch (protocol) {
173 case IPPROTO_ESP:
174 return &esp6_protocol;
175 case IPPROTO_AH:
176 return &ah6_protocol;
177 case IPPROTO_COMP:
178 return &ipcomp6_protocol;
179 }
180
181 return NULL;
182}
183
184int xfrm6_protocol_register(struct xfrm6_protocol *handler,
185 unsigned char protocol)
186{
187 struct xfrm6_protocol __rcu **pprev;
188 struct xfrm6_protocol *t;
189 bool add_netproto = false;
190 int ret = -EEXIST;
191 int priority = handler->priority;
192
193 if (!proto_handlers(protocol) || !netproto(protocol))
194 return -EINVAL;
195
196 mutex_lock(&xfrm6_protocol_mutex);
197
198 if (!rcu_dereference_protected(*proto_handlers(protocol),
199 lockdep_is_held(&xfrm6_protocol_mutex)))
200 add_netproto = true;
201
202 for (pprev = proto_handlers(protocol);
203 (t = rcu_dereference_protected(*pprev,
204 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
205 pprev = &t->next) {
206 if (t->priority < priority)
207 break;
208 if (t->priority == priority)
209 goto err;
210 }
211
212 handler->next = *pprev;
213 rcu_assign_pointer(*pprev, handler);
214
215 ret = 0;
216
217err:
218 mutex_unlock(&xfrm6_protocol_mutex);
219
220 if (add_netproto) {
221 if (inet6_add_protocol(netproto(protocol), protocol)) {
222 pr_err("%s: can't add protocol\n", __func__);
223 ret = -EAGAIN;
224 }
225 }
226
227 return ret;
228}
229EXPORT_SYMBOL(xfrm6_protocol_register);
230
231int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
232 unsigned char protocol)
233{
234 struct xfrm6_protocol __rcu **pprev;
235 struct xfrm6_protocol *t;
236 int ret = -ENOENT;
237
238 if (!proto_handlers(protocol) || !netproto(protocol))
239 return -EINVAL;
240
241 mutex_lock(&xfrm6_protocol_mutex);
242
243 for (pprev = proto_handlers(protocol);
244 (t = rcu_dereference_protected(*pprev,
245 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
246 pprev = &t->next) {
247 if (t == handler) {
248 *pprev = handler->next;
249 ret = 0;
250 break;
251 }
252 }
253
254 if (!rcu_dereference_protected(*proto_handlers(protocol),
255 lockdep_is_held(&xfrm6_protocol_mutex))) {
256 if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
257 pr_err("%s: can't remove protocol\n", __func__);
258 ret = -EAGAIN;
259 }
260 }
261
262 mutex_unlock(&xfrm6_protocol_mutex);
263
264 synchronize_net();
265
266 return ret;
267}
268EXPORT_SYMBOL(xfrm6_protocol_deregister);
269
270int __init xfrm6_protocol_init(void)
271{
272 return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
273}
274
275void xfrm6_protocol_fini(void)
276{
277 xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);
278}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* xfrm6_protocol.c - Generic xfrm protocol multiplexer for ipv6.
3 *
4 * Copyright (C) 2013 secunet Security Networks AG
5 *
6 * Author:
7 * Steffen Klassert <steffen.klassert@secunet.com>
8 *
9 * Based on:
10 * net/ipv4/xfrm4_protocol.c
11 */
12
13#include <linux/init.h>
14#include <linux/mutex.h>
15#include <linux/skbuff.h>
16#include <linux/icmpv6.h>
17#include <net/ip6_route.h>
18#include <net/ipv6.h>
19#include <net/protocol.h>
20#include <net/xfrm.h>
21
22static struct xfrm6_protocol __rcu *esp6_handlers __read_mostly;
23static struct xfrm6_protocol __rcu *ah6_handlers __read_mostly;
24static struct xfrm6_protocol __rcu *ipcomp6_handlers __read_mostly;
25static DEFINE_MUTEX(xfrm6_protocol_mutex);
26
27static inline struct xfrm6_protocol __rcu **proto_handlers(u8 protocol)
28{
29 switch (protocol) {
30 case IPPROTO_ESP:
31 return &esp6_handlers;
32 case IPPROTO_AH:
33 return &ah6_handlers;
34 case IPPROTO_COMP:
35 return &ipcomp6_handlers;
36 }
37
38 return NULL;
39}
40
41#define for_each_protocol_rcu(head, handler) \
42 for (handler = rcu_dereference(head); \
43 handler != NULL; \
44 handler = rcu_dereference(handler->next)) \
45
46static int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
47{
48 int ret;
49 struct xfrm6_protocol *handler;
50 struct xfrm6_protocol __rcu **head = proto_handlers(protocol);
51
52 if (!head)
53 return 0;
54
55 for_each_protocol_rcu(*proto_handlers(protocol), handler)
56 if ((ret = handler->cb_handler(skb, err)) <= 0)
57 return ret;
58
59 return 0;
60}
61
62int xfrm6_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
63 int encap_type)
64{
65 int ret;
66 struct xfrm6_protocol *handler;
67 struct xfrm6_protocol __rcu **head = proto_handlers(nexthdr);
68
69 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
70 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
71 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
72
73 if (!head)
74 goto out;
75
76 if (!skb_dst(skb)) {
77 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
78 int flags = RT6_LOOKUP_F_HAS_SADDR;
79 struct dst_entry *dst;
80 struct flowi6 fl6 = {
81 .flowi6_iif = skb->dev->ifindex,
82 .daddr = ip6h->daddr,
83 .saddr = ip6h->saddr,
84 .flowlabel = ip6_flowinfo(ip6h),
85 .flowi6_mark = skb->mark,
86 .flowi6_proto = ip6h->nexthdr,
87 };
88
89 dst = ip6_route_input_lookup(dev_net(skb->dev), skb->dev, &fl6,
90 skb, flags);
91 if (dst->error)
92 goto drop;
93 skb_dst_set(skb, dst);
94 }
95
96 for_each_protocol_rcu(*head, handler)
97 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
98 return ret;
99
100out:
101 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
102
103drop:
104 kfree_skb(skb);
105 return 0;
106}
107EXPORT_SYMBOL(xfrm6_rcv_encap);
108
109static int xfrm6_esp_rcv(struct sk_buff *skb)
110{
111 int ret;
112 struct xfrm6_protocol *handler;
113
114 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
115
116 for_each_protocol_rcu(esp6_handlers, handler)
117 if ((ret = handler->handler(skb)) != -EINVAL)
118 return ret;
119
120 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
121
122 kfree_skb(skb);
123 return 0;
124}
125
126static int xfrm6_esp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
127 u8 type, u8 code, int offset, __be32 info)
128{
129 struct xfrm6_protocol *handler;
130
131 for_each_protocol_rcu(esp6_handlers, handler)
132 if (!handler->err_handler(skb, opt, type, code, offset, info))
133 return 0;
134
135 return -ENOENT;
136}
137
138static int xfrm6_ah_rcv(struct sk_buff *skb)
139{
140 int ret;
141 struct xfrm6_protocol *handler;
142
143 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
144
145 for_each_protocol_rcu(ah6_handlers, handler)
146 if ((ret = handler->handler(skb)) != -EINVAL)
147 return ret;
148
149 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
150
151 kfree_skb(skb);
152 return 0;
153}
154
155static int xfrm6_ah_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
156 u8 type, u8 code, int offset, __be32 info)
157{
158 struct xfrm6_protocol *handler;
159
160 for_each_protocol_rcu(ah6_handlers, handler)
161 if (!handler->err_handler(skb, opt, type, code, offset, info))
162 return 0;
163
164 return -ENOENT;
165}
166
167static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
168{
169 int ret;
170 struct xfrm6_protocol *handler;
171
172 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
173
174 for_each_protocol_rcu(ipcomp6_handlers, handler)
175 if ((ret = handler->handler(skb)) != -EINVAL)
176 return ret;
177
178 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
179
180 kfree_skb(skb);
181 return 0;
182}
183
184static int xfrm6_ipcomp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
185 u8 type, u8 code, int offset, __be32 info)
186{
187 struct xfrm6_protocol *handler;
188
189 for_each_protocol_rcu(ipcomp6_handlers, handler)
190 if (!handler->err_handler(skb, opt, type, code, offset, info))
191 return 0;
192
193 return -ENOENT;
194}
195
196static const struct inet6_protocol esp6_protocol = {
197 .handler = xfrm6_esp_rcv,
198 .err_handler = xfrm6_esp_err,
199 .flags = INET6_PROTO_NOPOLICY,
200};
201
202static const struct inet6_protocol ah6_protocol = {
203 .handler = xfrm6_ah_rcv,
204 .err_handler = xfrm6_ah_err,
205 .flags = INET6_PROTO_NOPOLICY,
206};
207
208static const struct inet6_protocol ipcomp6_protocol = {
209 .handler = xfrm6_ipcomp_rcv,
210 .err_handler = xfrm6_ipcomp_err,
211 .flags = INET6_PROTO_NOPOLICY,
212};
213
214static const struct xfrm_input_afinfo xfrm6_input_afinfo = {
215 .family = AF_INET6,
216 .callback = xfrm6_rcv_cb,
217};
218
219static inline const struct inet6_protocol *netproto(unsigned char protocol)
220{
221 switch (protocol) {
222 case IPPROTO_ESP:
223 return &esp6_protocol;
224 case IPPROTO_AH:
225 return &ah6_protocol;
226 case IPPROTO_COMP:
227 return &ipcomp6_protocol;
228 }
229
230 return NULL;
231}
232
233int xfrm6_protocol_register(struct xfrm6_protocol *handler,
234 unsigned char protocol)
235{
236 struct xfrm6_protocol __rcu **pprev;
237 struct xfrm6_protocol *t;
238 bool add_netproto = false;
239 int ret = -EEXIST;
240 int priority = handler->priority;
241
242 if (!proto_handlers(protocol) || !netproto(protocol))
243 return -EINVAL;
244
245 mutex_lock(&xfrm6_protocol_mutex);
246
247 if (!rcu_dereference_protected(*proto_handlers(protocol),
248 lockdep_is_held(&xfrm6_protocol_mutex)))
249 add_netproto = true;
250
251 for (pprev = proto_handlers(protocol);
252 (t = rcu_dereference_protected(*pprev,
253 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
254 pprev = &t->next) {
255 if (t->priority < priority)
256 break;
257 if (t->priority == priority)
258 goto err;
259 }
260
261 handler->next = *pprev;
262 rcu_assign_pointer(*pprev, handler);
263
264 ret = 0;
265
266err:
267 mutex_unlock(&xfrm6_protocol_mutex);
268
269 if (add_netproto) {
270 if (inet6_add_protocol(netproto(protocol), protocol)) {
271 pr_err("%s: can't add protocol\n", __func__);
272 ret = -EAGAIN;
273 }
274 }
275
276 return ret;
277}
278EXPORT_SYMBOL(xfrm6_protocol_register);
279
280int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
281 unsigned char protocol)
282{
283 struct xfrm6_protocol __rcu **pprev;
284 struct xfrm6_protocol *t;
285 int ret = -ENOENT;
286
287 if (!proto_handlers(protocol) || !netproto(protocol))
288 return -EINVAL;
289
290 mutex_lock(&xfrm6_protocol_mutex);
291
292 for (pprev = proto_handlers(protocol);
293 (t = rcu_dereference_protected(*pprev,
294 lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
295 pprev = &t->next) {
296 if (t == handler) {
297 *pprev = handler->next;
298 ret = 0;
299 break;
300 }
301 }
302
303 if (!rcu_dereference_protected(*proto_handlers(protocol),
304 lockdep_is_held(&xfrm6_protocol_mutex))) {
305 if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
306 pr_err("%s: can't remove protocol\n", __func__);
307 ret = -EAGAIN;
308 }
309 }
310
311 mutex_unlock(&xfrm6_protocol_mutex);
312
313 synchronize_net();
314
315 return ret;
316}
317EXPORT_SYMBOL(xfrm6_protocol_deregister);
318
319int __init xfrm6_protocol_init(void)
320{
321 return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
322}
323
324void xfrm6_protocol_fini(void)
325{
326 xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);
327}