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_IP6:
45 case LWTUNNEL_ENCAP_IP:
46 case LWTUNNEL_ENCAP_NONE:
47 case __LWTUNNEL_ENCAP_MAX:
48 /* should not have got here */
55 #endif /* CONFIG_MODULES */
57 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
59 struct lwtunnel_state *lws;
61 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
65 EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
67 static const struct lwtunnel_encap_ops __rcu *
68 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
70 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
73 if (num > LWTUNNEL_ENCAP_MAX)
76 return !cmpxchg((const struct lwtunnel_encap_ops **)
80 EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
82 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
83 unsigned int encap_type)
87 if (encap_type == LWTUNNEL_ENCAP_NONE ||
88 encap_type > LWTUNNEL_ENCAP_MAX)
91 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
92 &lwtun_encaps[encap_type],
93 ops, NULL) == ops) ? 0 : -1;
99 EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
101 int lwtunnel_build_state(u16 encap_type,
102 struct nlattr *encap, unsigned int family,
103 const void *cfg, struct lwtunnel_state **lws,
104 struct netlink_ext_ack *extack)
106 const struct lwtunnel_encap_ops *ops;
110 if (encap_type == LWTUNNEL_ENCAP_NONE ||
111 encap_type > LWTUNNEL_ENCAP_MAX) {
112 NL_SET_ERR_MSG_ATTR(extack, encap,
113 "Unknown LWT encapsulation type");
119 ops = rcu_dereference(lwtun_encaps[encap_type]);
120 if (likely(ops && ops->build_state && try_module_get(ops->owner)))
125 ret = ops->build_state(encap, family, cfg, lws, extack);
127 module_put(ops->owner);
129 /* don't rely on -EOPNOTSUPP to detect match as build_state
130 * handlers could return it
132 NL_SET_ERR_MSG_ATTR(extack, encap,
133 "LWT encapsulation type not supported");
138 EXPORT_SYMBOL_GPL(lwtunnel_build_state);
140 int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
142 const struct lwtunnel_encap_ops *ops;
145 if (encap_type == LWTUNNEL_ENCAP_NONE ||
146 encap_type > LWTUNNEL_ENCAP_MAX) {
147 NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
152 ops = rcu_dereference(lwtun_encaps[encap_type]);
154 #ifdef CONFIG_MODULES
156 const char *encap_type_str = lwtunnel_encap_str(encap_type);
158 if (encap_type_str) {
160 request_module("rtnl-lwt-%s", encap_type_str);
164 ops = rcu_dereference(lwtun_encaps[encap_type]);
169 ret = ops ? 0 : -EOPNOTSUPP;
171 NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
175 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
177 int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
178 struct netlink_ext_ack *extack)
180 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
181 struct nlattr *nla_entype;
182 struct nlattr *attrs;
186 while (rtnh_ok(rtnh, remaining)) {
187 attrlen = rtnh_attrlen(rtnh);
189 attrs = rtnh_attrs(rtnh);
190 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
193 if (nla_len(nla_entype) < sizeof(u16)) {
194 NL_SET_ERR_MSG(extack, "Invalid RTA_ENCAP_TYPE");
197 encap_type = nla_get_u16(nla_entype);
199 if (lwtunnel_valid_encap_type(encap_type,
204 rtnh = rtnh_next(rtnh, &remaining);
209 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
211 void lwtstate_free(struct lwtunnel_state *lws)
213 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
215 if (ops->destroy_state) {
216 ops->destroy_state(lws);
221 module_put(ops->owner);
223 EXPORT_SYMBOL_GPL(lwtstate_free);
225 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
226 int encap_attr, int encap_type_attr)
228 const struct lwtunnel_encap_ops *ops;
235 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
236 lwtstate->type > LWTUNNEL_ENCAP_MAX)
239 nest = nla_nest_start_noflag(skb, encap_attr);
245 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
246 if (likely(ops && ops->fill_encap))
247 ret = ops->fill_encap(skb, lwtstate);
251 goto nla_put_failure;
252 nla_nest_end(skb, nest);
253 ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
255 goto nla_put_failure;
260 nla_nest_cancel(skb, nest);
262 return (ret == -EOPNOTSUPP ? 0 : ret);
264 EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
266 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
268 const struct lwtunnel_encap_ops *ops;
274 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
275 lwtstate->type > LWTUNNEL_ENCAP_MAX)
279 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
280 if (likely(ops && ops->get_encap_size))
281 ret = nla_total_size(ops->get_encap_size(lwtstate));
286 EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
288 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
290 const struct lwtunnel_encap_ops *ops;
299 if (a->type != b->type)
302 if (a->type == LWTUNNEL_ENCAP_NONE ||
303 a->type > LWTUNNEL_ENCAP_MAX)
307 ops = rcu_dereference(lwtun_encaps[a->type]);
308 if (likely(ops && ops->cmp_encap))
309 ret = ops->cmp_encap(a, b);
314 EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
316 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
318 struct dst_entry *dst = skb_dst(skb);
319 const struct lwtunnel_encap_ops *ops;
320 struct lwtunnel_state *lwtstate;
325 lwtstate = dst->lwtstate;
327 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
328 lwtstate->type > LWTUNNEL_ENCAP_MAX)
333 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
334 if (likely(ops && ops->output))
335 ret = ops->output(net, sk, skb);
338 if (ret == -EOPNOTSUPP)
348 EXPORT_SYMBOL_GPL(lwtunnel_output);
350 int lwtunnel_xmit(struct sk_buff *skb)
352 struct dst_entry *dst = skb_dst(skb);
353 const struct lwtunnel_encap_ops *ops;
354 struct lwtunnel_state *lwtstate;
360 lwtstate = dst->lwtstate;
362 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
363 lwtstate->type > LWTUNNEL_ENCAP_MAX)
368 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
369 if (likely(ops && ops->xmit))
370 ret = ops->xmit(skb);
373 if (ret == -EOPNOTSUPP)
383 EXPORT_SYMBOL_GPL(lwtunnel_xmit);
385 int lwtunnel_input(struct sk_buff *skb)
387 struct dst_entry *dst = skb_dst(skb);
388 const struct lwtunnel_encap_ops *ops;
389 struct lwtunnel_state *lwtstate;
394 lwtstate = dst->lwtstate;
396 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
397 lwtstate->type > LWTUNNEL_ENCAP_MAX)
402 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
403 if (likely(ops && ops->input))
404 ret = ops->input(skb);
407 if (ret == -EOPNOTSUPP)
417 EXPORT_SYMBOL_GPL(lwtunnel_input);