blob: cdc09efca4420d3b02b15e6b7bd535a3b2dff4cf [file] [log] [blame]
Steffen Klassert33287152014-02-21 08:41:08 +01001/* xfrm4_protocol.c - Generic xfrm protocol multiplexer.
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/tunnel4.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 <net/icmp.h>
21#include <net/ip.h>
22#include <net/protocol.h>
23#include <net/xfrm.h>
24
25static struct xfrm4_protocol __rcu *esp4_handlers __read_mostly;
26static struct xfrm4_protocol __rcu *ah4_handlers __read_mostly;
27static struct xfrm4_protocol __rcu *ipcomp4_handlers __read_mostly;
28static DEFINE_MUTEX(xfrm4_protocol_mutex);
29
30static inline struct xfrm4_protocol __rcu **proto_handlers(u8 protocol)
31{
32 switch (protocol) {
33 case IPPROTO_ESP:
34 return &esp4_handlers;
35 case IPPROTO_AH:
36 return &ah4_handlers;
37 case IPPROTO_COMP:
38 return &ipcomp4_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 xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
50{
51 int ret;
52 struct xfrm4_protocol *handler;
53
54 for_each_protocol_rcu(*proto_handlers(protocol), handler)
55 if ((ret = handler->cb_handler(skb, err)) <= 0)
56 return ret;
57
58 return 0;
59}
60EXPORT_SYMBOL(xfrm4_rcv_cb);
61
62int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
63 int encap_type)
64{
65 int ret;
66 struct xfrm4_protocol *handler;
67
Steffen Klassert70be6c92014-02-21 08:41:09 +010068 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
Steffen Klassert33287152014-02-21 08:41:08 +010069 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
70 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
71
72 for_each_protocol_rcu(*proto_handlers(nexthdr), handler)
73 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
74 return ret;
75
76 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
77
78 kfree_skb(skb);
79 return 0;
80}
81EXPORT_SYMBOL(xfrm4_rcv_encap);
82
83static int xfrm4_esp_rcv(struct sk_buff *skb)
84{
85 int ret;
86 struct xfrm4_protocol *handler;
87
Steffen Klassert70be6c92014-02-21 08:41:09 +010088 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
89
Steffen Klassert33287152014-02-21 08:41:08 +010090 for_each_protocol_rcu(esp4_handlers, handler)
91 if ((ret = handler->handler(skb)) != -EINVAL)
92 return ret;
93
94 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
95
96 kfree_skb(skb);
97 return 0;
98}
99
100static void xfrm4_esp_err(struct sk_buff *skb, u32 info)
101{
102 struct xfrm4_protocol *handler;
103
104 for_each_protocol_rcu(esp4_handlers, handler)
105 if (!handler->err_handler(skb, info))
106 break;
107}
108
109static int xfrm4_ah_rcv(struct sk_buff *skb)
110{
111 int ret;
112 struct xfrm4_protocol *handler;
113
Steffen Klassert70be6c92014-02-21 08:41:09 +0100114 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
115
Steffen Klassert33287152014-02-21 08:41:08 +0100116 for_each_protocol_rcu(ah4_handlers, handler)
117 if ((ret = handler->handler(skb)) != -EINVAL)
118 return ret;;
119
120 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
121
122 kfree_skb(skb);
123 return 0;
124}
125
126static void xfrm4_ah_err(struct sk_buff *skb, u32 info)
127{
128 struct xfrm4_protocol *handler;
129
130 for_each_protocol_rcu(ah4_handlers, handler)
131 if (!handler->err_handler(skb, info))
132 break;
133}
134
135static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
136{
137 int ret;
138 struct xfrm4_protocol *handler;
139
Steffen Klassert70be6c92014-02-21 08:41:09 +0100140 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
141
Steffen Klassert33287152014-02-21 08:41:08 +0100142 for_each_protocol_rcu(ipcomp4_handlers, handler)
143 if ((ret = handler->handler(skb)) != -EINVAL)
144 return ret;
145
146 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
147
148 kfree_skb(skb);
149 return 0;
150}
151
152static void xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
153{
154 struct xfrm4_protocol *handler;
155
156 for_each_protocol_rcu(ipcomp4_handlers, handler)
157 if (!handler->err_handler(skb, info))
158 break;
159}
160
161static const struct net_protocol esp4_protocol = {
162 .handler = xfrm4_esp_rcv,
163 .err_handler = xfrm4_esp_err,
164 .no_policy = 1,
165 .netns_ok = 1,
166};
167
168static const struct net_protocol ah4_protocol = {
169 .handler = xfrm4_ah_rcv,
170 .err_handler = xfrm4_ah_err,
171 .no_policy = 1,
172 .netns_ok = 1,
173};
174
175static const struct net_protocol ipcomp4_protocol = {
176 .handler = xfrm4_ipcomp_rcv,
177 .err_handler = xfrm4_ipcomp_err,
178 .no_policy = 1,
179 .netns_ok = 1,
180};
181
182static inline const struct net_protocol *netproto(unsigned char protocol)
183{
184 switch (protocol) {
185 case IPPROTO_ESP:
186 return &esp4_protocol;
187 case IPPROTO_AH:
188 return &ah4_protocol;
189 case IPPROTO_COMP:
190 return &ipcomp4_protocol;
191 }
192
193 return NULL;
194}
195
196int xfrm4_protocol_register(struct xfrm4_protocol *handler,
197 unsigned char protocol)
198{
199 struct xfrm4_protocol __rcu **pprev;
200 struct xfrm4_protocol *t;
201 bool add_netproto = false;
202
203 int ret = -EEXIST;
204 int priority = handler->priority;
205
206 mutex_lock(&xfrm4_protocol_mutex);
207
208 if (!rcu_dereference_protected(*proto_handlers(protocol),
209 lockdep_is_held(&xfrm4_protocol_mutex)))
210 add_netproto = true;
211
212 for (pprev = proto_handlers(protocol);
213 (t = rcu_dereference_protected(*pprev,
214 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
215 pprev = &t->next) {
216 if (t->priority < priority)
217 break;
218 if (t->priority == priority)
219 goto err;
220 }
221
222 handler->next = *pprev;
223 rcu_assign_pointer(*pprev, handler);
224
225 ret = 0;
226
227err:
228 mutex_unlock(&xfrm4_protocol_mutex);
229
230 if (add_netproto) {
231 if (inet_add_protocol(netproto(protocol), protocol)) {
232 pr_err("%s: can't add protocol\n", __func__);
233 ret = -EAGAIN;
234 }
235 }
236
237 return ret;
238}
239EXPORT_SYMBOL(xfrm4_protocol_register);
240
241int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
242 unsigned char protocol)
243{
244 struct xfrm4_protocol __rcu **pprev;
245 struct xfrm4_protocol *t;
246 int ret = -ENOENT;
247
248 mutex_lock(&xfrm4_protocol_mutex);
249
250 for (pprev = proto_handlers(protocol);
251 (t = rcu_dereference_protected(*pprev,
252 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
253 pprev = &t->next) {
254 if (t == handler) {
255 *pprev = handler->next;
256 ret = 0;
257 break;
258 }
259 }
260
261 if (!rcu_dereference_protected(*proto_handlers(protocol),
262 lockdep_is_held(&xfrm4_protocol_mutex))) {
263 if (inet_del_protocol(netproto(protocol), protocol) < 0) {
264 pr_err("%s: can't remove protocol\n", __func__);
265 ret = -EAGAIN;
266 }
267 }
268
269 mutex_unlock(&xfrm4_protocol_mutex);
270
271 synchronize_net();
272
273 return ret;
274}
275EXPORT_SYMBOL(xfrm4_protocol_deregister);