1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lwtunnel Infrastructure for light weight tunnels like mpls
5 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
8 #include <linux/capability.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/lwtunnel.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
21 #include <net/lwtunnel.h>
22 #include <net/rtnetlink.h>
23 #include <net/ip6_fib.h>
28 static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
30 /* Only lwt encaps implemented without using an interface for
31 * the encap need to return a string here.
34 case LWTUNNEL_ENCAP_MPLS:
36 case LWTUNNEL_ENCAP_ILA:
38 case LWTUNNEL_ENCAP_SEG6:
40 case LWTUNNEL_ENCAP_BPF:
42 case LWTUNNEL_ENCAP_SEG6_LOCAL:
44 case LWTUNNEL_ENCAP_RPL:
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 */
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);
67 EXPORT_SYMBOL_GPL(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,
75 if (num > LWTUNNEL_ENCAP_MAX)
78 return !cmpxchg((const struct lwtunnel_encap_ops **)
82 EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
84 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
85 unsigned int encap_type)
89 if (encap_type == LWTUNNEL_ENCAP_NONE ||
90 encap_type > LWTUNNEL_ENCAP_MAX)
93 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
94 &lwtun_encaps[encap_type],
95 ops, NULL) == ops) ? 0 : -1;
101 EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
103 int lwtunnel_build_state(struct net *net, u16 encap_type,
104 struct nlattr *encap, unsigned int family,
105 const void *cfg, struct lwtunnel_state **lws,
106 struct netlink_ext_ack *extack)
108 const struct lwtunnel_encap_ops *ops;
112 if (encap_type == LWTUNNEL_ENCAP_NONE ||
113 encap_type > LWTUNNEL_ENCAP_MAX) {
114 NL_SET_ERR_MSG_ATTR(extack, encap,
115 "Unknown LWT encapsulation type");
121 ops = rcu_dereference(lwtun_encaps[encap_type]);
122 if (likely(ops && ops->build_state && try_module_get(ops->owner)))
127 ret = ops->build_state(net, encap, family, cfg, lws, extack);
129 module_put(ops->owner);
131 /* don't rely on -EOPNOTSUPP to detect match as build_state
132 * handlers could return it
134 NL_SET_ERR_MSG_ATTR(extack, encap,
135 "LWT encapsulation type not supported");
140 EXPORT_SYMBOL_GPL(lwtunnel_build_state);
142 int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
144 const struct lwtunnel_encap_ops *ops;
147 if (encap_type == LWTUNNEL_ENCAP_NONE ||
148 encap_type > LWTUNNEL_ENCAP_MAX) {
149 NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
154 ops = rcu_dereference(lwtun_encaps[encap_type]);
156 #ifdef CONFIG_MODULES
158 const char *encap_type_str = lwtunnel_encap_str(encap_type);
160 if (encap_type_str) {
162 request_module("rtnl-lwt-%s", encap_type_str);
166 ops = rcu_dereference(lwtun_encaps[encap_type]);
171 ret = ops ? 0 : -EOPNOTSUPP;
173 NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
177 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
179 int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
180 struct netlink_ext_ack *extack)
182 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
183 struct nlattr *nla_entype;
184 struct nlattr *attrs;
188 while (rtnh_ok(rtnh, remaining)) {
189 attrlen = rtnh_attrlen(rtnh);
191 attrs = rtnh_attrs(rtnh);
192 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
195 if (nla_len(nla_entype) < sizeof(u16)) {
196 NL_SET_ERR_MSG(extack, "Invalid RTA_ENCAP_TYPE");
199 encap_type = nla_get_u16(nla_entype);
201 if (lwtunnel_valid_encap_type(encap_type,
206 rtnh = rtnh_next(rtnh, &remaining);
211 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
213 void lwtstate_free(struct lwtunnel_state *lws)
215 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
217 if (ops->destroy_state) {
218 ops->destroy_state(lws);
223 module_put(ops->owner);
225 EXPORT_SYMBOL_GPL(lwtstate_free);
227 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
228 int encap_attr, int encap_type_attr)
230 const struct lwtunnel_encap_ops *ops;
237 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
238 lwtstate->type > LWTUNNEL_ENCAP_MAX)
241 nest = nla_nest_start_noflag(skb, encap_attr);
247 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
248 if (likely(ops && ops->fill_encap))
249 ret = ops->fill_encap(skb, lwtstate);
253 goto nla_put_failure;
254 nla_nest_end(skb, nest);
255 ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
257 goto nla_put_failure;
262 nla_nest_cancel(skb, nest);
264 return (ret == -EOPNOTSUPP ? 0 : ret);
266 EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
268 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
270 const struct lwtunnel_encap_ops *ops;
276 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
277 lwtstate->type > LWTUNNEL_ENCAP_MAX)
281 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
282 if (likely(ops && ops->get_encap_size))
283 ret = nla_total_size(ops->get_encap_size(lwtstate));
288 EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
290 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
292 const struct lwtunnel_encap_ops *ops;
301 if (a->type != b->type)
304 if (a->type == LWTUNNEL_ENCAP_NONE ||
305 a->type > LWTUNNEL_ENCAP_MAX)
309 ops = rcu_dereference(lwtun_encaps[a->type]);
310 if (likely(ops && ops->cmp_encap))
311 ret = ops->cmp_encap(a, b);
316 EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
318 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
320 struct dst_entry *dst = skb_dst(skb);
321 const struct lwtunnel_encap_ops *ops;
322 struct lwtunnel_state *lwtstate;
327 lwtstate = dst->lwtstate;
329 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
330 lwtstate->type > LWTUNNEL_ENCAP_MAX)
335 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
336 if (likely(ops && ops->output))
337 ret = ops->output(net, sk, skb);
340 if (ret == -EOPNOTSUPP)
350 EXPORT_SYMBOL_GPL(lwtunnel_output);
352 int lwtunnel_xmit(struct sk_buff *skb)
354 struct dst_entry *dst = skb_dst(skb);
355 const struct lwtunnel_encap_ops *ops;
356 struct lwtunnel_state *lwtstate;
362 lwtstate = dst->lwtstate;
364 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
365 lwtstate->type > LWTUNNEL_ENCAP_MAX)
370 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
371 if (likely(ops && ops->xmit))
372 ret = ops->xmit(skb);
375 if (ret == -EOPNOTSUPP)
385 EXPORT_SYMBOL_GPL(lwtunnel_xmit);
387 int lwtunnel_input(struct sk_buff *skb)
389 struct dst_entry *dst = skb_dst(skb);
390 const struct lwtunnel_encap_ops *ops;
391 struct lwtunnel_state *lwtstate;
396 lwtstate = dst->lwtstate;
398 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
399 lwtstate->type > LWTUNNEL_ENCAP_MAX)
404 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
405 if (likely(ops && ops->input))
406 ret = ops->input(skb);
409 if (ret == -EOPNOTSUPP)
419 EXPORT_SYMBOL_GPL(lwtunnel_input);