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>
26 DEFINE_STATIC_KEY_FALSE(nf_hooks_lwtunnel_enabled);
27 EXPORT_SYMBOL_GPL(nf_hooks_lwtunnel_enabled);
31 static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
33 /* Only lwt encaps implemented without using an interface for
34 * the encap need to return a string here.
37 case LWTUNNEL_ENCAP_MPLS:
39 case LWTUNNEL_ENCAP_ILA:
41 case LWTUNNEL_ENCAP_SEG6:
43 case LWTUNNEL_ENCAP_BPF:
45 case LWTUNNEL_ENCAP_SEG6_LOCAL:
47 case LWTUNNEL_ENCAP_RPL:
49 case LWTUNNEL_ENCAP_IOAM6:
51 case LWTUNNEL_ENCAP_IP6:
52 case LWTUNNEL_ENCAP_IP:
53 case LWTUNNEL_ENCAP_NONE:
54 case __LWTUNNEL_ENCAP_MAX:
55 /* should not have got here */
62 #endif /* CONFIG_MODULES */
64 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
66 struct lwtunnel_state *lws;
68 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
72 EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
74 static const struct lwtunnel_encap_ops __rcu *
75 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
77 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
80 if (num > LWTUNNEL_ENCAP_MAX)
83 return !cmpxchg((const struct lwtunnel_encap_ops **)
87 EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
89 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
90 unsigned int encap_type)
94 if (encap_type == LWTUNNEL_ENCAP_NONE ||
95 encap_type > LWTUNNEL_ENCAP_MAX)
98 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
99 &lwtun_encaps[encap_type],
100 ops, NULL) == ops) ? 0 : -1;
106 EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
108 int lwtunnel_build_state(struct net *net, u16 encap_type,
109 struct nlattr *encap, unsigned int family,
110 const void *cfg, struct lwtunnel_state **lws,
111 struct netlink_ext_ack *extack)
113 const struct lwtunnel_encap_ops *ops;
117 if (encap_type == LWTUNNEL_ENCAP_NONE ||
118 encap_type > LWTUNNEL_ENCAP_MAX) {
119 NL_SET_ERR_MSG_ATTR(extack, encap,
120 "Unknown LWT encapsulation type");
126 ops = rcu_dereference(lwtun_encaps[encap_type]);
127 if (likely(ops && ops->build_state && try_module_get(ops->owner)))
132 ret = ops->build_state(net, encap, family, cfg, lws, extack);
134 module_put(ops->owner);
136 /* don't rely on -EOPNOTSUPP to detect match as build_state
137 * handlers could return it
139 NL_SET_ERR_MSG_ATTR(extack, encap,
140 "LWT encapsulation type not supported");
145 EXPORT_SYMBOL_GPL(lwtunnel_build_state);
147 int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
149 const struct lwtunnel_encap_ops *ops;
152 if (encap_type == LWTUNNEL_ENCAP_NONE ||
153 encap_type > LWTUNNEL_ENCAP_MAX) {
154 NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
159 ops = rcu_dereference(lwtun_encaps[encap_type]);
161 #ifdef CONFIG_MODULES
163 const char *encap_type_str = lwtunnel_encap_str(encap_type);
165 if (encap_type_str) {
167 request_module("rtnl-lwt-%s", encap_type_str);
171 ops = rcu_dereference(lwtun_encaps[encap_type]);
176 ret = ops ? 0 : -EOPNOTSUPP;
178 NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
182 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
184 int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
185 struct netlink_ext_ack *extack)
187 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
188 struct nlattr *nla_entype;
189 struct nlattr *attrs;
193 while (rtnh_ok(rtnh, remaining)) {
194 attrlen = rtnh_attrlen(rtnh);
196 attrs = rtnh_attrs(rtnh);
197 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
200 if (nla_len(nla_entype) < sizeof(u16)) {
201 NL_SET_ERR_MSG(extack, "Invalid RTA_ENCAP_TYPE");
204 encap_type = nla_get_u16(nla_entype);
206 if (lwtunnel_valid_encap_type(encap_type,
211 rtnh = rtnh_next(rtnh, &remaining);
216 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
218 void lwtstate_free(struct lwtunnel_state *lws)
220 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
222 if (ops->destroy_state) {
223 ops->destroy_state(lws);
228 module_put(ops->owner);
230 EXPORT_SYMBOL_GPL(lwtstate_free);
232 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
233 int encap_attr, int encap_type_attr)
235 const struct lwtunnel_encap_ops *ops;
242 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
243 lwtstate->type > LWTUNNEL_ENCAP_MAX)
246 nest = nla_nest_start_noflag(skb, encap_attr);
252 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
253 if (likely(ops && ops->fill_encap))
254 ret = ops->fill_encap(skb, lwtstate);
258 goto nla_put_failure;
259 nla_nest_end(skb, nest);
260 ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
262 goto nla_put_failure;
267 nla_nest_cancel(skb, nest);
269 return (ret == -EOPNOTSUPP ? 0 : ret);
271 EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
273 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
275 const struct lwtunnel_encap_ops *ops;
281 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
282 lwtstate->type > LWTUNNEL_ENCAP_MAX)
286 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
287 if (likely(ops && ops->get_encap_size))
288 ret = nla_total_size(ops->get_encap_size(lwtstate));
293 EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
295 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
297 const struct lwtunnel_encap_ops *ops;
306 if (a->type != b->type)
309 if (a->type == LWTUNNEL_ENCAP_NONE ||
310 a->type > LWTUNNEL_ENCAP_MAX)
314 ops = rcu_dereference(lwtun_encaps[a->type]);
315 if (likely(ops && ops->cmp_encap))
316 ret = ops->cmp_encap(a, b);
321 EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
323 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
325 struct dst_entry *dst = skb_dst(skb);
326 const struct lwtunnel_encap_ops *ops;
327 struct lwtunnel_state *lwtstate;
332 lwtstate = dst->lwtstate;
334 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
335 lwtstate->type > LWTUNNEL_ENCAP_MAX)
340 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
341 if (likely(ops && ops->output))
342 ret = ops->output(net, sk, skb);
345 if (ret == -EOPNOTSUPP)
355 EXPORT_SYMBOL_GPL(lwtunnel_output);
357 int lwtunnel_xmit(struct sk_buff *skb)
359 struct dst_entry *dst = skb_dst(skb);
360 const struct lwtunnel_encap_ops *ops;
361 struct lwtunnel_state *lwtstate;
367 lwtstate = dst->lwtstate;
369 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
370 lwtstate->type > LWTUNNEL_ENCAP_MAX)
375 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
376 if (likely(ops && ops->xmit))
377 ret = ops->xmit(skb);
380 if (ret == -EOPNOTSUPP)
390 EXPORT_SYMBOL_GPL(lwtunnel_xmit);
392 int lwtunnel_input(struct sk_buff *skb)
394 struct dst_entry *dst = skb_dst(skb);
395 const struct lwtunnel_encap_ops *ops;
396 struct lwtunnel_state *lwtstate;
401 lwtstate = dst->lwtstate;
403 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
404 lwtstate->type > LWTUNNEL_ENCAP_MAX)
409 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
410 if (likely(ops && ops->input))
411 ret = ops->input(skb);
414 if (ret == -EOPNOTSUPP)
424 EXPORT_SYMBOL_GPL(lwtunnel_input);