doc: ReSTify seccomp_filter.txt
[linux-stable.git] / net / core / lwtunnel.c
blobcfae3d5fe11f10035394aea48fb5332244058a03
1 /*
2 * lwtunnel Infrastructure for light weight tunnels like mpls
4 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/capability.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/skbuff.h>
20 #include <linux/netdevice.h>
21 #include <linux/lwtunnel.h>
22 #include <linux/in.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
26 #include <net/lwtunnel.h>
27 #include <net/rtnetlink.h>
28 #include <net/ip6_fib.h>
29 #include <net/nexthop.h>
31 #ifdef CONFIG_MODULES
33 static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
35 /* Only lwt encaps implemented without using an interface for
36 * the encap need to return a string here.
38 switch (encap_type) {
39 case LWTUNNEL_ENCAP_MPLS:
40 return "MPLS";
41 case LWTUNNEL_ENCAP_ILA:
42 return "ILA";
43 case LWTUNNEL_ENCAP_SEG6:
44 return "SEG6";
45 case LWTUNNEL_ENCAP_BPF:
46 return "BPF";
47 case LWTUNNEL_ENCAP_IP6:
48 case LWTUNNEL_ENCAP_IP:
49 case LWTUNNEL_ENCAP_NONE:
50 case __LWTUNNEL_ENCAP_MAX:
51 /* should not have got here */
52 WARN_ON(1);
53 break;
55 return NULL;
58 #endif /* CONFIG_MODULES */
60 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
62 struct lwtunnel_state *lws;
64 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
66 return lws;
68 EXPORT_SYMBOL(lwtunnel_state_alloc);
70 static const struct lwtunnel_encap_ops __rcu *
71 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
73 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
74 unsigned int num)
76 if (num > LWTUNNEL_ENCAP_MAX)
77 return -ERANGE;
79 return !cmpxchg((const struct lwtunnel_encap_ops **)
80 &lwtun_encaps[num],
81 NULL, ops) ? 0 : -1;
83 EXPORT_SYMBOL(lwtunnel_encap_add_ops);
85 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
86 unsigned int encap_type)
88 int ret;
90 if (encap_type == LWTUNNEL_ENCAP_NONE ||
91 encap_type > LWTUNNEL_ENCAP_MAX)
92 return -ERANGE;
94 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
95 &lwtun_encaps[encap_type],
96 ops, NULL) == ops) ? 0 : -1;
98 synchronize_net();
100 return ret;
102 EXPORT_SYMBOL(lwtunnel_encap_del_ops);
104 int lwtunnel_build_state(u16 encap_type,
105 struct nlattr *encap, unsigned int family,
106 const void *cfg, struct lwtunnel_state **lws)
108 const struct lwtunnel_encap_ops *ops;
109 int ret = -EINVAL;
111 if (encap_type == LWTUNNEL_ENCAP_NONE ||
112 encap_type > LWTUNNEL_ENCAP_MAX)
113 return ret;
115 ret = -EOPNOTSUPP;
116 rcu_read_lock();
117 ops = rcu_dereference(lwtun_encaps[encap_type]);
118 if (likely(ops && ops->build_state && try_module_get(ops->owner))) {
119 ret = ops->build_state(encap, family, cfg, lws);
120 if (ret)
121 module_put(ops->owner);
123 rcu_read_unlock();
125 return ret;
127 EXPORT_SYMBOL(lwtunnel_build_state);
129 int lwtunnel_valid_encap_type(u16 encap_type)
131 const struct lwtunnel_encap_ops *ops;
132 int ret = -EINVAL;
134 if (encap_type == LWTUNNEL_ENCAP_NONE ||
135 encap_type > LWTUNNEL_ENCAP_MAX)
136 return ret;
138 rcu_read_lock();
139 ops = rcu_dereference(lwtun_encaps[encap_type]);
140 rcu_read_unlock();
141 #ifdef CONFIG_MODULES
142 if (!ops) {
143 const char *encap_type_str = lwtunnel_encap_str(encap_type);
145 if (encap_type_str) {
146 __rtnl_unlock();
147 request_module("rtnl-lwt-%s", encap_type_str);
148 rtnl_lock();
150 rcu_read_lock();
151 ops = rcu_dereference(lwtun_encaps[encap_type]);
152 rcu_read_unlock();
155 #endif
156 return ops ? 0 : -EOPNOTSUPP;
158 EXPORT_SYMBOL(lwtunnel_valid_encap_type);
160 int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining)
162 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
163 struct nlattr *nla_entype;
164 struct nlattr *attrs;
165 u16 encap_type;
166 int attrlen;
168 while (rtnh_ok(rtnh, remaining)) {
169 attrlen = rtnh_attrlen(rtnh);
170 if (attrlen > 0) {
171 attrs = rtnh_attrs(rtnh);
172 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
174 if (nla_entype) {
175 encap_type = nla_get_u16(nla_entype);
177 if (lwtunnel_valid_encap_type(encap_type) != 0)
178 return -EOPNOTSUPP;
181 rtnh = rtnh_next(rtnh, &remaining);
184 return 0;
186 EXPORT_SYMBOL(lwtunnel_valid_encap_type_attr);
188 void lwtstate_free(struct lwtunnel_state *lws)
190 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
192 if (ops->destroy_state) {
193 ops->destroy_state(lws);
194 kfree_rcu(lws, rcu);
195 } else {
196 kfree(lws);
198 module_put(ops->owner);
200 EXPORT_SYMBOL(lwtstate_free);
202 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
204 const struct lwtunnel_encap_ops *ops;
205 struct nlattr *nest;
206 int ret;
208 if (!lwtstate)
209 return 0;
211 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
212 lwtstate->type > LWTUNNEL_ENCAP_MAX)
213 return 0;
215 nest = nla_nest_start(skb, RTA_ENCAP);
216 if (!nest)
217 return -EMSGSIZE;
219 ret = -EOPNOTSUPP;
220 rcu_read_lock();
221 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
222 if (likely(ops && ops->fill_encap))
223 ret = ops->fill_encap(skb, lwtstate);
224 rcu_read_unlock();
226 if (ret)
227 goto nla_put_failure;
228 nla_nest_end(skb, nest);
229 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
230 if (ret)
231 goto nla_put_failure;
233 return 0;
235 nla_put_failure:
236 nla_nest_cancel(skb, nest);
238 return (ret == -EOPNOTSUPP ? 0 : ret);
240 EXPORT_SYMBOL(lwtunnel_fill_encap);
242 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
244 const struct lwtunnel_encap_ops *ops;
245 int ret = 0;
247 if (!lwtstate)
248 return 0;
250 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
251 lwtstate->type > LWTUNNEL_ENCAP_MAX)
252 return 0;
254 rcu_read_lock();
255 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
256 if (likely(ops && ops->get_encap_size))
257 ret = nla_total_size(ops->get_encap_size(lwtstate));
258 rcu_read_unlock();
260 return ret;
262 EXPORT_SYMBOL(lwtunnel_get_encap_size);
264 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
266 const struct lwtunnel_encap_ops *ops;
267 int ret = 0;
269 if (!a && !b)
270 return 0;
272 if (!a || !b)
273 return 1;
275 if (a->type != b->type)
276 return 1;
278 if (a->type == LWTUNNEL_ENCAP_NONE ||
279 a->type > LWTUNNEL_ENCAP_MAX)
280 return 0;
282 rcu_read_lock();
283 ops = rcu_dereference(lwtun_encaps[a->type]);
284 if (likely(ops && ops->cmp_encap))
285 ret = ops->cmp_encap(a, b);
286 rcu_read_unlock();
288 return ret;
290 EXPORT_SYMBOL(lwtunnel_cmp_encap);
292 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
294 struct dst_entry *dst = skb_dst(skb);
295 const struct lwtunnel_encap_ops *ops;
296 struct lwtunnel_state *lwtstate;
297 int ret = -EINVAL;
299 if (!dst)
300 goto drop;
301 lwtstate = dst->lwtstate;
303 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
304 lwtstate->type > LWTUNNEL_ENCAP_MAX)
305 return 0;
307 ret = -EOPNOTSUPP;
308 rcu_read_lock();
309 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
310 if (likely(ops && ops->output))
311 ret = ops->output(net, sk, skb);
312 rcu_read_unlock();
314 if (ret == -EOPNOTSUPP)
315 goto drop;
317 return ret;
319 drop:
320 kfree_skb(skb);
322 return ret;
324 EXPORT_SYMBOL(lwtunnel_output);
326 int lwtunnel_xmit(struct sk_buff *skb)
328 struct dst_entry *dst = skb_dst(skb);
329 const struct lwtunnel_encap_ops *ops;
330 struct lwtunnel_state *lwtstate;
331 int ret = -EINVAL;
333 if (!dst)
334 goto drop;
336 lwtstate = dst->lwtstate;
338 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
339 lwtstate->type > LWTUNNEL_ENCAP_MAX)
340 return 0;
342 ret = -EOPNOTSUPP;
343 rcu_read_lock();
344 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
345 if (likely(ops && ops->xmit))
346 ret = ops->xmit(skb);
347 rcu_read_unlock();
349 if (ret == -EOPNOTSUPP)
350 goto drop;
352 return ret;
354 drop:
355 kfree_skb(skb);
357 return ret;
359 EXPORT_SYMBOL(lwtunnel_xmit);
361 int lwtunnel_input(struct sk_buff *skb)
363 struct dst_entry *dst = skb_dst(skb);
364 const struct lwtunnel_encap_ops *ops;
365 struct lwtunnel_state *lwtstate;
366 int ret = -EINVAL;
368 if (!dst)
369 goto drop;
370 lwtstate = dst->lwtstate;
372 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
373 lwtstate->type > LWTUNNEL_ENCAP_MAX)
374 return 0;
376 ret = -EOPNOTSUPP;
377 rcu_read_lock();
378 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
379 if (likely(ops && ops->input))
380 ret = ops->input(skb);
381 rcu_read_unlock();
383 if (ret == -EOPNOTSUPP)
384 goto drop;
386 return ret;
388 drop:
389 kfree_skb(skb);
391 return ret;
393 EXPORT_SYMBOL(lwtunnel_input);