dm writecache: fix incorrect flush sequence when doing SSD mode commit
[linux/fpc-iii.git] / net / ipv4 / xfrm4_protocol.c
blob8a4285712808e3c0b3f3d63a78d75b17387ec525
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* xfrm4_protocol.c - Generic xfrm protocol multiplexer.
4 * Copyright (C) 2013 secunet Security Networks AG
6 * Author:
7 * Steffen Klassert <steffen.klassert@secunet.com>
9 * Based on:
10 * net/ipv4/tunnel4.c
13 #include <linux/init.h>
14 #include <linux/mutex.h>
15 #include <linux/skbuff.h>
16 #include <net/icmp.h>
17 #include <net/ip.h>
18 #include <net/protocol.h>
19 #include <net/xfrm.h>
21 static struct xfrm4_protocol __rcu *esp4_handlers __read_mostly;
22 static struct xfrm4_protocol __rcu *ah4_handlers __read_mostly;
23 static struct xfrm4_protocol __rcu *ipcomp4_handlers __read_mostly;
24 static DEFINE_MUTEX(xfrm4_protocol_mutex);
26 static inline struct xfrm4_protocol __rcu **proto_handlers(u8 protocol)
28 switch (protocol) {
29 case IPPROTO_ESP:
30 return &esp4_handlers;
31 case IPPROTO_AH:
32 return &ah4_handlers;
33 case IPPROTO_COMP:
34 return &ipcomp4_handlers;
37 return NULL;
40 #define for_each_protocol_rcu(head, handler) \
41 for (handler = rcu_dereference(head); \
42 handler != NULL; \
43 handler = rcu_dereference(handler->next)) \
45 static int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
47 int ret;
48 struct xfrm4_protocol *handler;
49 struct xfrm4_protocol __rcu **head = proto_handlers(protocol);
51 if (!head)
52 return 0;
54 for_each_protocol_rcu(*head, handler)
55 if ((ret = handler->cb_handler(skb, err)) <= 0)
56 return ret;
58 return 0;
61 int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
62 int encap_type)
64 int ret;
65 struct xfrm4_protocol *handler;
66 struct xfrm4_protocol __rcu **head = proto_handlers(nexthdr);
68 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
69 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
70 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
72 if (!head)
73 goto out;
75 for_each_protocol_rcu(*head, handler)
76 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
77 return ret;
79 out:
80 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
82 kfree_skb(skb);
83 return 0;
85 EXPORT_SYMBOL(xfrm4_rcv_encap);
87 static int xfrm4_esp_rcv(struct sk_buff *skb)
89 int ret;
90 struct xfrm4_protocol *handler;
92 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
94 for_each_protocol_rcu(esp4_handlers, handler)
95 if ((ret = handler->handler(skb)) != -EINVAL)
96 return ret;
98 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
100 kfree_skb(skb);
101 return 0;
104 static int xfrm4_esp_err(struct sk_buff *skb, u32 info)
106 struct xfrm4_protocol *handler;
108 for_each_protocol_rcu(esp4_handlers, handler)
109 if (!handler->err_handler(skb, info))
110 return 0;
112 return -ENOENT;
115 static int xfrm4_ah_rcv(struct sk_buff *skb)
117 int ret;
118 struct xfrm4_protocol *handler;
120 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
122 for_each_protocol_rcu(ah4_handlers, handler)
123 if ((ret = handler->handler(skb)) != -EINVAL)
124 return ret;
126 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
128 kfree_skb(skb);
129 return 0;
132 static int xfrm4_ah_err(struct sk_buff *skb, u32 info)
134 struct xfrm4_protocol *handler;
136 for_each_protocol_rcu(ah4_handlers, handler)
137 if (!handler->err_handler(skb, info))
138 return 0;
140 return -ENOENT;
143 static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
145 int ret;
146 struct xfrm4_protocol *handler;
148 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
150 for_each_protocol_rcu(ipcomp4_handlers, handler)
151 if ((ret = handler->handler(skb)) != -EINVAL)
152 return ret;
154 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
156 kfree_skb(skb);
157 return 0;
160 static int xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
162 struct xfrm4_protocol *handler;
164 for_each_protocol_rcu(ipcomp4_handlers, handler)
165 if (!handler->err_handler(skb, info))
166 return 0;
168 return -ENOENT;
171 static const struct net_protocol esp4_protocol = {
172 .handler = xfrm4_esp_rcv,
173 .err_handler = xfrm4_esp_err,
174 .no_policy = 1,
175 .netns_ok = 1,
178 static const struct net_protocol ah4_protocol = {
179 .handler = xfrm4_ah_rcv,
180 .err_handler = xfrm4_ah_err,
181 .no_policy = 1,
182 .netns_ok = 1,
185 static const struct net_protocol ipcomp4_protocol = {
186 .handler = xfrm4_ipcomp_rcv,
187 .err_handler = xfrm4_ipcomp_err,
188 .no_policy = 1,
189 .netns_ok = 1,
192 static const struct xfrm_input_afinfo xfrm4_input_afinfo = {
193 .family = AF_INET,
194 .callback = xfrm4_rcv_cb,
197 static inline const struct net_protocol *netproto(unsigned char protocol)
199 switch (protocol) {
200 case IPPROTO_ESP:
201 return &esp4_protocol;
202 case IPPROTO_AH:
203 return &ah4_protocol;
204 case IPPROTO_COMP:
205 return &ipcomp4_protocol;
208 return NULL;
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;
217 int ret = -EEXIST;
218 int priority = handler->priority;
220 if (!proto_handlers(protocol) || !netproto(protocol))
221 return -EINVAL;
223 mutex_lock(&xfrm4_protocol_mutex);
225 if (!rcu_dereference_protected(*proto_handlers(protocol),
226 lockdep_is_held(&xfrm4_protocol_mutex)))
227 add_netproto = true;
229 for (pprev = proto_handlers(protocol);
230 (t = rcu_dereference_protected(*pprev,
231 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
232 pprev = &t->next) {
233 if (t->priority < priority)
234 break;
235 if (t->priority == priority)
236 goto err;
239 handler->next = *pprev;
240 rcu_assign_pointer(*pprev, handler);
242 ret = 0;
244 err:
245 mutex_unlock(&xfrm4_protocol_mutex);
247 if (add_netproto) {
248 if (inet_add_protocol(netproto(protocol), protocol)) {
249 pr_err("%s: can't add protocol\n", __func__);
250 ret = -EAGAIN;
254 return ret;
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;
263 int ret = -ENOENT;
265 if (!proto_handlers(protocol) || !netproto(protocol))
266 return -EINVAL;
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;
273 pprev = &t->next) {
274 if (t == handler) {
275 *pprev = handler->next;
276 ret = 0;
277 break;
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__);
285 ret = -EAGAIN;
289 mutex_unlock(&xfrm4_protocol_mutex);
291 synchronize_net();
293 return ret;
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);