powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / net / core / lwtunnel.c
bloba5d4e866ce88b4d055798d9ea55fc905b351fb3d
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>
30 #ifdef CONFIG_MODULES
32 static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
34 /* Only lwt encaps implemented without using an interface for
35 * the encap need to return a string here.
37 switch (encap_type) {
38 case LWTUNNEL_ENCAP_MPLS:
39 return "MPLS";
40 case LWTUNNEL_ENCAP_ILA:
41 return "ILA";
42 case LWTUNNEL_ENCAP_SEG6:
43 return "SEG6";
44 case LWTUNNEL_ENCAP_BPF:
45 return "BPF";
46 case LWTUNNEL_ENCAP_IP6:
47 case LWTUNNEL_ENCAP_IP:
48 case LWTUNNEL_ENCAP_NONE:
49 case __LWTUNNEL_ENCAP_MAX:
50 /* should not have got here */
51 WARN_ON(1);
52 break;
54 return NULL;
57 #endif /* CONFIG_MODULES */
59 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
61 struct lwtunnel_state *lws;
63 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
65 return lws;
67 EXPORT_SYMBOL(lwtunnel_state_alloc);
69 static const struct lwtunnel_encap_ops __rcu *
70 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
72 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
73 unsigned int num)
75 if (num > LWTUNNEL_ENCAP_MAX)
76 return -ERANGE;
78 return !cmpxchg((const struct lwtunnel_encap_ops **)
79 &lwtun_encaps[num],
80 NULL, ops) ? 0 : -1;
82 EXPORT_SYMBOL(lwtunnel_encap_add_ops);
84 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
85 unsigned int encap_type)
87 int ret;
89 if (encap_type == LWTUNNEL_ENCAP_NONE ||
90 encap_type > LWTUNNEL_ENCAP_MAX)
91 return -ERANGE;
93 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
94 &lwtun_encaps[encap_type],
95 ops, NULL) == ops) ? 0 : -1;
97 synchronize_net();
99 return ret;
101 EXPORT_SYMBOL(lwtunnel_encap_del_ops);
103 int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
104 struct nlattr *encap, unsigned int family,
105 const void *cfg, struct lwtunnel_state **lws)
107 const struct lwtunnel_encap_ops *ops;
108 int ret = -EINVAL;
110 if (encap_type == LWTUNNEL_ENCAP_NONE ||
111 encap_type > LWTUNNEL_ENCAP_MAX)
112 return ret;
114 ret = -EOPNOTSUPP;
115 rcu_read_lock();
116 ops = rcu_dereference(lwtun_encaps[encap_type]);
117 #ifdef CONFIG_MODULES
118 if (!ops) {
119 const char *encap_type_str = lwtunnel_encap_str(encap_type);
121 if (encap_type_str) {
122 rcu_read_unlock();
123 request_module("rtnl-lwt-%s", encap_type_str);
124 rcu_read_lock();
125 ops = rcu_dereference(lwtun_encaps[encap_type]);
128 #endif
129 if (likely(ops && ops->build_state))
130 ret = ops->build_state(dev, encap, family, cfg, lws);
131 rcu_read_unlock();
133 return ret;
135 EXPORT_SYMBOL(lwtunnel_build_state);
137 void lwtstate_free(struct lwtunnel_state *lws)
139 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
141 if (ops->destroy_state) {
142 ops->destroy_state(lws);
143 kfree_rcu(lws, rcu);
144 } else {
145 kfree(lws);
148 EXPORT_SYMBOL(lwtstate_free);
150 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
152 const struct lwtunnel_encap_ops *ops;
153 struct nlattr *nest;
154 int ret = -EINVAL;
156 if (!lwtstate)
157 return 0;
159 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
160 lwtstate->type > LWTUNNEL_ENCAP_MAX)
161 return 0;
163 ret = -EOPNOTSUPP;
164 nest = nla_nest_start(skb, RTA_ENCAP);
165 rcu_read_lock();
166 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
167 if (likely(ops && ops->fill_encap))
168 ret = ops->fill_encap(skb, lwtstate);
169 rcu_read_unlock();
171 if (ret)
172 goto nla_put_failure;
173 nla_nest_end(skb, nest);
174 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
175 if (ret)
176 goto nla_put_failure;
178 return 0;
180 nla_put_failure:
181 nla_nest_cancel(skb, nest);
183 return (ret == -EOPNOTSUPP ? 0 : ret);
185 EXPORT_SYMBOL(lwtunnel_fill_encap);
187 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
189 const struct lwtunnel_encap_ops *ops;
190 int ret = 0;
192 if (!lwtstate)
193 return 0;
195 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
196 lwtstate->type > LWTUNNEL_ENCAP_MAX)
197 return 0;
199 rcu_read_lock();
200 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
201 if (likely(ops && ops->get_encap_size))
202 ret = nla_total_size(ops->get_encap_size(lwtstate));
203 rcu_read_unlock();
205 return ret;
207 EXPORT_SYMBOL(lwtunnel_get_encap_size);
209 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
211 const struct lwtunnel_encap_ops *ops;
212 int ret = 0;
214 if (!a && !b)
215 return 0;
217 if (!a || !b)
218 return 1;
220 if (a->type != b->type)
221 return 1;
223 if (a->type == LWTUNNEL_ENCAP_NONE ||
224 a->type > LWTUNNEL_ENCAP_MAX)
225 return 0;
227 rcu_read_lock();
228 ops = rcu_dereference(lwtun_encaps[a->type]);
229 if (likely(ops && ops->cmp_encap))
230 ret = ops->cmp_encap(a, b);
231 rcu_read_unlock();
233 return ret;
235 EXPORT_SYMBOL(lwtunnel_cmp_encap);
237 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
239 struct dst_entry *dst = skb_dst(skb);
240 const struct lwtunnel_encap_ops *ops;
241 struct lwtunnel_state *lwtstate;
242 int ret = -EINVAL;
244 if (!dst)
245 goto drop;
246 lwtstate = dst->lwtstate;
248 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
249 lwtstate->type > LWTUNNEL_ENCAP_MAX)
250 return 0;
252 ret = -EOPNOTSUPP;
253 rcu_read_lock();
254 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
255 if (likely(ops && ops->output))
256 ret = ops->output(net, sk, skb);
257 rcu_read_unlock();
259 if (ret == -EOPNOTSUPP)
260 goto drop;
262 return ret;
264 drop:
265 kfree_skb(skb);
267 return ret;
269 EXPORT_SYMBOL(lwtunnel_output);
271 int lwtunnel_xmit(struct sk_buff *skb)
273 struct dst_entry *dst = skb_dst(skb);
274 const struct lwtunnel_encap_ops *ops;
275 struct lwtunnel_state *lwtstate;
276 int ret = -EINVAL;
278 if (!dst)
279 goto drop;
281 lwtstate = dst->lwtstate;
283 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
284 lwtstate->type > LWTUNNEL_ENCAP_MAX)
285 return 0;
287 ret = -EOPNOTSUPP;
288 rcu_read_lock();
289 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
290 if (likely(ops && ops->xmit))
291 ret = ops->xmit(skb);
292 rcu_read_unlock();
294 if (ret == -EOPNOTSUPP)
295 goto drop;
297 return ret;
299 drop:
300 kfree_skb(skb);
302 return ret;
304 EXPORT_SYMBOL(lwtunnel_xmit);
306 int lwtunnel_input(struct sk_buff *skb)
308 struct dst_entry *dst = skb_dst(skb);
309 const struct lwtunnel_encap_ops *ops;
310 struct lwtunnel_state *lwtstate;
311 int ret = -EINVAL;
313 if (!dst)
314 goto drop;
315 lwtstate = dst->lwtstate;
317 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
318 lwtstate->type > LWTUNNEL_ENCAP_MAX)
319 return 0;
321 ret = -EOPNOTSUPP;
322 rcu_read_lock();
323 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
324 if (likely(ops && ops->input))
325 ret = ops->input(skb);
326 rcu_read_unlock();
328 if (ret == -EOPNOTSUPP)
329 goto drop;
331 return ret;
333 drop:
334 kfree_skb(skb);
336 return ret;
338 EXPORT_SYMBOL(lwtunnel_input);