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>
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>
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.
38 case LWTUNNEL_ENCAP_MPLS:
40 case LWTUNNEL_ENCAP_ILA:
42 case LWTUNNEL_ENCAP_IP6:
43 case LWTUNNEL_ENCAP_IP:
44 case LWTUNNEL_ENCAP_NONE:
45 case __LWTUNNEL_ENCAP_MAX:
46 /* should not have got here */
53 #endif /* CONFIG_MODULES */
55 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
57 struct lwtunnel_state *lws;
59 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
63 EXPORT_SYMBOL(lwtunnel_state_alloc);
65 static const struct lwtunnel_encap_ops __rcu *
66 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
68 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
71 if (num > LWTUNNEL_ENCAP_MAX)
74 return !cmpxchg((const struct lwtunnel_encap_ops **)
78 EXPORT_SYMBOL(lwtunnel_encap_add_ops);
80 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
81 unsigned int encap_type)
85 if (encap_type == LWTUNNEL_ENCAP_NONE ||
86 encap_type > LWTUNNEL_ENCAP_MAX)
89 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
90 &lwtun_encaps[encap_type],
91 ops, NULL) == ops) ? 0 : -1;
97 EXPORT_SYMBOL(lwtunnel_encap_del_ops);
99 int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
100 struct nlattr *encap, unsigned int family,
101 const void *cfg, struct lwtunnel_state **lws)
103 const struct lwtunnel_encap_ops *ops;
106 if (encap_type == LWTUNNEL_ENCAP_NONE ||
107 encap_type > LWTUNNEL_ENCAP_MAX)
112 ops = rcu_dereference(lwtun_encaps[encap_type]);
113 #ifdef CONFIG_MODULES
115 const char *encap_type_str = lwtunnel_encap_str(encap_type);
117 if (encap_type_str) {
119 request_module("rtnl-lwt-%s", encap_type_str);
121 ops = rcu_dereference(lwtun_encaps[encap_type]);
125 if (likely(ops && ops->build_state))
126 ret = ops->build_state(dev, encap, family, cfg, lws);
131 EXPORT_SYMBOL(lwtunnel_build_state);
133 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
135 const struct lwtunnel_encap_ops *ops;
142 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
143 lwtstate->type > LWTUNNEL_ENCAP_MAX)
147 nest = nla_nest_start(skb, RTA_ENCAP);
149 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
150 if (likely(ops && ops->fill_encap))
151 ret = ops->fill_encap(skb, lwtstate);
155 goto nla_put_failure;
156 nla_nest_end(skb, nest);
157 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
159 goto nla_put_failure;
164 nla_nest_cancel(skb, nest);
166 return (ret == -EOPNOTSUPP ? 0 : ret);
168 EXPORT_SYMBOL(lwtunnel_fill_encap);
170 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
172 const struct lwtunnel_encap_ops *ops;
178 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
179 lwtstate->type > LWTUNNEL_ENCAP_MAX)
183 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
184 if (likely(ops && ops->get_encap_size))
185 ret = nla_total_size(ops->get_encap_size(lwtstate));
190 EXPORT_SYMBOL(lwtunnel_get_encap_size);
192 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
194 const struct lwtunnel_encap_ops *ops;
203 if (a->type != b->type)
206 if (a->type == LWTUNNEL_ENCAP_NONE ||
207 a->type > LWTUNNEL_ENCAP_MAX)
211 ops = rcu_dereference(lwtun_encaps[a->type]);
212 if (likely(ops && ops->cmp_encap))
213 ret = ops->cmp_encap(a, b);
218 EXPORT_SYMBOL(lwtunnel_cmp_encap);
220 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
222 struct dst_entry *dst = skb_dst(skb);
223 const struct lwtunnel_encap_ops *ops;
224 struct lwtunnel_state *lwtstate;
229 lwtstate = dst->lwtstate;
231 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
232 lwtstate->type > LWTUNNEL_ENCAP_MAX)
237 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
238 if (likely(ops && ops->output))
239 ret = ops->output(net, sk, skb);
242 if (ret == -EOPNOTSUPP)
252 EXPORT_SYMBOL(lwtunnel_output);
254 int lwtunnel_input(struct sk_buff *skb)
256 struct dst_entry *dst = skb_dst(skb);
257 const struct lwtunnel_encap_ops *ops;
258 struct lwtunnel_state *lwtstate;
263 lwtstate = dst->lwtstate;
265 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
266 lwtstate->type > LWTUNNEL_ENCAP_MAX)
271 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
272 if (likely(ops && ops->input))
273 ret = ops->input(skb);
276 if (ret == -EOPNOTSUPP)
286 EXPORT_SYMBOL(lwtunnel_input);