ALSA: hda/realtek - Change front mic location for Lenovo M710q
[linux/fpc-iii.git] / net / ipv4 / xfrm4_protocol.c
blob8dd0e6ab86065f4e5e17bcd6d119def4677a2b86
1 /* xfrm4_protocol.c - Generic xfrm protocol multiplexer.
3 * Copyright (C) 2013 secunet Security Networks AG
5 * Author:
6 * Steffen Klassert <steffen.klassert@secunet.com>
8 * Based on:
9 * net/ipv4/tunnel4.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 <net/icmp.h>
21 #include <net/ip.h>
22 #include <net/protocol.h>
23 #include <net/xfrm.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)
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;
41 return NULL;
44 #define for_each_protocol_rcu(head, handler) \
45 for (handler = rcu_dereference(head); \
46 handler != NULL; \
47 handler = rcu_dereference(handler->next)) \
49 int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
51 int ret;
52 struct xfrm4_protocol *handler;
53 struct xfrm4_protocol __rcu **head = proto_handlers(protocol);
55 if (!head)
56 return 0;
58 for_each_protocol_rcu(*head, handler)
59 if ((ret = handler->cb_handler(skb, err)) <= 0)
60 return ret;
62 return 0;
64 EXPORT_SYMBOL(xfrm4_rcv_cb);
66 int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
67 int encap_type)
69 int ret;
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);
77 if (!head)
78 goto out;
80 for_each_protocol_rcu(*head, handler)
81 if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
82 return ret;
84 out:
85 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
87 kfree_skb(skb);
88 return 0;
90 EXPORT_SYMBOL(xfrm4_rcv_encap);
92 static int xfrm4_esp_rcv(struct sk_buff *skb)
94 int ret;
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)
101 return ret;
103 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
105 kfree_skb(skb);
106 return 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))
115 break;
118 static int xfrm4_ah_rcv(struct sk_buff *skb)
120 int ret;
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)
127 return ret;
129 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
131 kfree_skb(skb);
132 return 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))
141 break;
144 static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
146 int ret;
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)
153 return ret;
155 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
157 kfree_skb(skb);
158 return 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))
167 break;
170 static const struct net_protocol esp4_protocol = {
171 .handler = xfrm4_esp_rcv,
172 .err_handler = xfrm4_esp_err,
173 .no_policy = 1,
174 .netns_ok = 1,
177 static const struct net_protocol ah4_protocol = {
178 .handler = xfrm4_ah_rcv,
179 .err_handler = xfrm4_ah_err,
180 .no_policy = 1,
181 .netns_ok = 1,
184 static const struct net_protocol ipcomp4_protocol = {
185 .handler = xfrm4_ipcomp_rcv,
186 .err_handler = xfrm4_ipcomp_err,
187 .no_policy = 1,
188 .netns_ok = 1,
191 static const struct xfrm_input_afinfo xfrm4_input_afinfo = {
192 .family = AF_INET,
193 .callback = xfrm4_rcv_cb,
196 static inline const struct net_protocol *netproto(unsigned char protocol)
198 switch (protocol) {
199 case IPPROTO_ESP:
200 return &esp4_protocol;
201 case IPPROTO_AH:
202 return &ah4_protocol;
203 case IPPROTO_COMP:
204 return &ipcomp4_protocol;
207 return NULL;
210 int xfrm4_protocol_register(struct xfrm4_protocol *handler,
211 unsigned char protocol)
213 struct xfrm4_protocol __rcu **pprev;
214 struct xfrm4_protocol *t;
215 bool add_netproto = false;
216 int ret = -EEXIST;
217 int priority = handler->priority;
219 if (!proto_handlers(protocol) || !netproto(protocol))
220 return -EINVAL;
222 mutex_lock(&xfrm4_protocol_mutex);
224 if (!rcu_dereference_protected(*proto_handlers(protocol),
225 lockdep_is_held(&xfrm4_protocol_mutex)))
226 add_netproto = true;
228 for (pprev = proto_handlers(protocol);
229 (t = rcu_dereference_protected(*pprev,
230 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
231 pprev = &t->next) {
232 if (t->priority < priority)
233 break;
234 if (t->priority == priority)
235 goto err;
238 handler->next = *pprev;
239 rcu_assign_pointer(*pprev, handler);
241 ret = 0;
243 err:
244 mutex_unlock(&xfrm4_protocol_mutex);
246 if (add_netproto) {
247 if (inet_add_protocol(netproto(protocol), protocol)) {
248 pr_err("%s: can't add protocol\n", __func__);
249 ret = -EAGAIN;
253 return ret;
255 EXPORT_SYMBOL(xfrm4_protocol_register);
257 int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
258 unsigned char protocol)
260 struct xfrm4_protocol __rcu **pprev;
261 struct xfrm4_protocol *t;
262 int ret = -ENOENT;
264 if (!proto_handlers(protocol) || !netproto(protocol))
265 return -EINVAL;
267 mutex_lock(&xfrm4_protocol_mutex);
269 for (pprev = proto_handlers(protocol);
270 (t = rcu_dereference_protected(*pprev,
271 lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
272 pprev = &t->next) {
273 if (t == handler) {
274 *pprev = handler->next;
275 ret = 0;
276 break;
280 if (!rcu_dereference_protected(*proto_handlers(protocol),
281 lockdep_is_held(&xfrm4_protocol_mutex))) {
282 if (inet_del_protocol(netproto(protocol), protocol) < 0) {
283 pr_err("%s: can't remove protocol\n", __func__);
284 ret = -EAGAIN;
288 mutex_unlock(&xfrm4_protocol_mutex);
290 synchronize_net();
292 return ret;
294 EXPORT_SYMBOL(xfrm4_protocol_deregister);
296 void __init xfrm4_protocol_init(void)
298 xfrm_input_register_afinfo(&xfrm4_input_afinfo);
300 EXPORT_SYMBOL(xfrm4_protocol_init);