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 encap_type
= nla_get_u16(nla_entype
);
195 if (lwtunnel_valid_encap_type(encap_type
,
200 rtnh
= rtnh_next(rtnh
, &remaining
);
205 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr
);
207 void lwtstate_free(struct lwtunnel_state
*lws
)
209 const struct lwtunnel_encap_ops
*ops
= lwtun_encaps
[lws
->type
];
211 if (ops
->destroy_state
) {
212 ops
->destroy_state(lws
);
217 module_put(ops
->owner
);
219 EXPORT_SYMBOL_GPL(lwtstate_free
);
221 int lwtunnel_fill_encap(struct sk_buff
*skb
, struct lwtunnel_state
*lwtstate
,
222 int encap_attr
, int encap_type_attr
)
224 const struct lwtunnel_encap_ops
*ops
;
231 if (lwtstate
->type
== LWTUNNEL_ENCAP_NONE
||
232 lwtstate
->type
> LWTUNNEL_ENCAP_MAX
)
235 nest
= nla_nest_start_noflag(skb
, encap_attr
);
241 ops
= rcu_dereference(lwtun_encaps
[lwtstate
->type
]);
242 if (likely(ops
&& ops
->fill_encap
))
243 ret
= ops
->fill_encap(skb
, lwtstate
);
247 goto nla_put_failure
;
248 nla_nest_end(skb
, nest
);
249 ret
= nla_put_u16(skb
, encap_type_attr
, lwtstate
->type
);
251 goto nla_put_failure
;
256 nla_nest_cancel(skb
, nest
);
258 return (ret
== -EOPNOTSUPP
? 0 : ret
);
260 EXPORT_SYMBOL_GPL(lwtunnel_fill_encap
);
262 int lwtunnel_get_encap_size(struct lwtunnel_state
*lwtstate
)
264 const struct lwtunnel_encap_ops
*ops
;
270 if (lwtstate
->type
== LWTUNNEL_ENCAP_NONE
||
271 lwtstate
->type
> LWTUNNEL_ENCAP_MAX
)
275 ops
= rcu_dereference(lwtun_encaps
[lwtstate
->type
]);
276 if (likely(ops
&& ops
->get_encap_size
))
277 ret
= nla_total_size(ops
->get_encap_size(lwtstate
));
282 EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size
);
284 int lwtunnel_cmp_encap(struct lwtunnel_state
*a
, struct lwtunnel_state
*b
)
286 const struct lwtunnel_encap_ops
*ops
;
295 if (a
->type
!= b
->type
)
298 if (a
->type
== LWTUNNEL_ENCAP_NONE
||
299 a
->type
> LWTUNNEL_ENCAP_MAX
)
303 ops
= rcu_dereference(lwtun_encaps
[a
->type
]);
304 if (likely(ops
&& ops
->cmp_encap
))
305 ret
= ops
->cmp_encap(a
, b
);
310 EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap
);
312 int lwtunnel_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
314 struct dst_entry
*dst
= skb_dst(skb
);
315 const struct lwtunnel_encap_ops
*ops
;
316 struct lwtunnel_state
*lwtstate
;
321 lwtstate
= dst
->lwtstate
;
323 if (lwtstate
->type
== LWTUNNEL_ENCAP_NONE
||
324 lwtstate
->type
> LWTUNNEL_ENCAP_MAX
)
329 ops
= rcu_dereference(lwtun_encaps
[lwtstate
->type
]);
330 if (likely(ops
&& ops
->output
))
331 ret
= ops
->output(net
, sk
, skb
);
334 if (ret
== -EOPNOTSUPP
)
344 EXPORT_SYMBOL_GPL(lwtunnel_output
);
346 int lwtunnel_xmit(struct sk_buff
*skb
)
348 struct dst_entry
*dst
= skb_dst(skb
);
349 const struct lwtunnel_encap_ops
*ops
;
350 struct lwtunnel_state
*lwtstate
;
356 lwtstate
= dst
->lwtstate
;
358 if (lwtstate
->type
== LWTUNNEL_ENCAP_NONE
||
359 lwtstate
->type
> LWTUNNEL_ENCAP_MAX
)
364 ops
= rcu_dereference(lwtun_encaps
[lwtstate
->type
]);
365 if (likely(ops
&& ops
->xmit
))
366 ret
= ops
->xmit(skb
);
369 if (ret
== -EOPNOTSUPP
)
379 EXPORT_SYMBOL_GPL(lwtunnel_xmit
);
381 int lwtunnel_input(struct sk_buff
*skb
)
383 struct dst_entry
*dst
= skb_dst(skb
);
384 const struct lwtunnel_encap_ops
*ops
;
385 struct lwtunnel_state
*lwtstate
;
390 lwtstate
= dst
->lwtstate
;
392 if (lwtstate
->type
== LWTUNNEL_ENCAP_NONE
||
393 lwtstate
->type
> LWTUNNEL_ENCAP_MAX
)
398 ops
= rcu_dereference(lwtun_encaps
[lwtstate
->type
]);
399 if (likely(ops
&& ops
->input
))
400 ret
= ops
->input(skb
);
403 if (ret
== -EOPNOTSUPP
)
413 EXPORT_SYMBOL_GPL(lwtunnel_input
);