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_XFRM:
52 /* module autoload not supported for encap type */
54 case LWTUNNEL_ENCAP_IP6:
55 case LWTUNNEL_ENCAP_IP:
56 case LWTUNNEL_ENCAP_NONE:
57 case __LWTUNNEL_ENCAP_MAX:
58 /* should not have got here */
65 #endif /* CONFIG_MODULES */
67 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
69 struct lwtunnel_state *lws;
71 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
75 EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
77 static const struct lwtunnel_encap_ops __rcu *
78 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
80 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
83 if (num > LWTUNNEL_ENCAP_MAX)
86 return !cmpxchg((const struct lwtunnel_encap_ops **)
90 EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
92 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
93 unsigned int encap_type)
97 if (encap_type == LWTUNNEL_ENCAP_NONE ||
98 encap_type > LWTUNNEL_ENCAP_MAX)
101 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
102 &lwtun_encaps[encap_type],
103 ops, NULL) == ops) ? 0 : -1;
109 EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
111 int lwtunnel_build_state(struct net *net, u16 encap_type,
112 struct nlattr *encap, unsigned int family,
113 const void *cfg, struct lwtunnel_state **lws,
114 struct netlink_ext_ack *extack)
116 const struct lwtunnel_encap_ops *ops;
120 if (encap_type == LWTUNNEL_ENCAP_NONE ||
121 encap_type > LWTUNNEL_ENCAP_MAX) {
122 NL_SET_ERR_MSG_ATTR(extack, encap,
123 "Unknown LWT encapsulation type");
129 ops = rcu_dereference(lwtun_encaps[encap_type]);
130 if (likely(ops && ops->build_state && try_module_get(ops->owner)))
135 ret = ops->build_state(net, encap, family, cfg, lws, extack);
137 module_put(ops->owner);
139 /* don't rely on -EOPNOTSUPP to detect match as build_state
140 * handlers could return it
142 NL_SET_ERR_MSG_ATTR(extack, encap,
143 "LWT encapsulation type not supported");
148 EXPORT_SYMBOL_GPL(lwtunnel_build_state);
150 int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
152 const struct lwtunnel_encap_ops *ops;
155 if (encap_type == LWTUNNEL_ENCAP_NONE ||
156 encap_type > LWTUNNEL_ENCAP_MAX) {
157 NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
162 ops = rcu_dereference(lwtun_encaps[encap_type]);
164 #ifdef CONFIG_MODULES
166 const char *encap_type_str = lwtunnel_encap_str(encap_type);
168 if (encap_type_str) {
170 request_module("rtnl-lwt-%s", encap_type_str);
174 ops = rcu_dereference(lwtun_encaps[encap_type]);
179 ret = ops ? 0 : -EOPNOTSUPP;
181 NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
185 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
187 int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
188 struct netlink_ext_ack *extack)
190 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
191 struct nlattr *nla_entype;
192 struct nlattr *attrs;
196 while (rtnh_ok(rtnh, remaining)) {
197 attrlen = rtnh_attrlen(rtnh);
199 attrs = rtnh_attrs(rtnh);
200 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
203 if (nla_len(nla_entype) < sizeof(u16)) {
204 NL_SET_ERR_MSG(extack, "Invalid RTA_ENCAP_TYPE");
207 encap_type = nla_get_u16(nla_entype);
209 if (lwtunnel_valid_encap_type(encap_type,
214 rtnh = rtnh_next(rtnh, &remaining);
219 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
221 void lwtstate_free(struct lwtunnel_state *lws)
223 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
225 if (ops->destroy_state) {
226 ops->destroy_state(lws);
231 module_put(ops->owner);
233 EXPORT_SYMBOL_GPL(lwtstate_free);
235 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
236 int encap_attr, int encap_type_attr)
238 const struct lwtunnel_encap_ops *ops;
245 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
246 lwtstate->type > LWTUNNEL_ENCAP_MAX)
249 nest = nla_nest_start_noflag(skb, encap_attr);
255 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
256 if (likely(ops && ops->fill_encap))
257 ret = ops->fill_encap(skb, lwtstate);
261 goto nla_put_failure;
262 nla_nest_end(skb, nest);
263 ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
265 goto nla_put_failure;
270 nla_nest_cancel(skb, nest);
272 return (ret == -EOPNOTSUPP ? 0 : ret);
274 EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
276 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
278 const struct lwtunnel_encap_ops *ops;
284 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
285 lwtstate->type > LWTUNNEL_ENCAP_MAX)
289 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
290 if (likely(ops && ops->get_encap_size))
291 ret = nla_total_size(ops->get_encap_size(lwtstate));
296 EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
298 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
300 const struct lwtunnel_encap_ops *ops;
309 if (a->type != b->type)
312 if (a->type == LWTUNNEL_ENCAP_NONE ||
313 a->type > LWTUNNEL_ENCAP_MAX)
317 ops = rcu_dereference(lwtun_encaps[a->type]);
318 if (likely(ops && ops->cmp_encap))
319 ret = ops->cmp_encap(a, b);
324 EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
326 int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
328 struct dst_entry *dst = skb_dst(skb);
329 const struct lwtunnel_encap_ops *ops;
330 struct lwtunnel_state *lwtstate;
335 lwtstate = dst->lwtstate;
337 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
338 lwtstate->type > LWTUNNEL_ENCAP_MAX)
343 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
344 if (likely(ops && ops->output))
345 ret = ops->output(net, sk, skb);
348 if (ret == -EOPNOTSUPP)
358 EXPORT_SYMBOL_GPL(lwtunnel_output);
360 int lwtunnel_xmit(struct sk_buff *skb)
362 struct dst_entry *dst = skb_dst(skb);
363 const struct lwtunnel_encap_ops *ops;
364 struct lwtunnel_state *lwtstate;
370 lwtstate = dst->lwtstate;
372 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
373 lwtstate->type > LWTUNNEL_ENCAP_MAX)
378 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
379 if (likely(ops && ops->xmit))
380 ret = ops->xmit(skb);
383 if (ret == -EOPNOTSUPP)
393 EXPORT_SYMBOL_GPL(lwtunnel_xmit);
395 int lwtunnel_input(struct sk_buff *skb)
397 struct dst_entry *dst = skb_dst(skb);
398 const struct lwtunnel_encap_ops *ops;
399 struct lwtunnel_state *lwtstate;
404 lwtstate = dst->lwtstate;
406 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
407 lwtstate->type > LWTUNNEL_ENCAP_MAX)
412 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
413 if (likely(ops && ops->input))
414 ret = ops->input(skb);
417 if (ret == -EOPNOTSUPP)
427 EXPORT_SYMBOL_GPL(lwtunnel_input);