3#include <netlink-private/netlink.h>
4#include <netlink-private/types.h>
5#include <netlink-private/route/nexthop-encap.h>
6#include <linux/lwtunnel.h>
8static struct lwtunnel_encap_type {
10 struct nh_encap_ops *ops;
11} lwtunnel_encap_types[__LWTUNNEL_ENCAP_MAX] = {
12 [LWTUNNEL_ENCAP_NONE] = { .name =
"none" },
13 [LWTUNNEL_ENCAP_MPLS] = { .name =
"mpls", .ops = &mpls_encap_ops },
14 [LWTUNNEL_ENCAP_IP] = { .name =
"ip" },
15 [LWTUNNEL_ENCAP_IP6] = { .name =
"ip6" },
16 [LWTUNNEL_ENCAP_ILA] = { .name =
"ila" },
17 [LWTUNNEL_ENCAP_BPF] = { .name =
"bpf" },
20static const char *nh_encap_type2str(
unsigned int type)
24 if (type > LWTUNNEL_ENCAP_MAX)
27 name = lwtunnel_encap_types[type].name;
29 return name ? name :
"unknown";
32void nh_encap_dump(
struct rtnl_nh_encap *rtnh_encap,
struct nl_dump_params *dp)
38 nh_encap_type2str(rtnh_encap->ops->encap_type));
40 if (rtnh_encap->ops->dump)
41 rtnh_encap->ops->dump(rtnh_encap->priv, dp);
44int nh_encap_build_msg(
struct nl_msg *msg,
struct rtnl_nh_encap *rtnh_encap)
49 if (!rtnh_encap->ops || !rtnh_encap->ops->build_msg) {
50 NL_DBG(2,
"Nexthop encap type not implemented\n");
54 NLA_PUT_U16(msg, RTA_ENCAP_TYPE, rtnh_encap->ops->encap_type);
60 err = rtnh_encap->ops->build_msg(msg, rtnh_encap->priv);
72int nh_encap_parse_msg(
struct nlattr *encap,
struct nlattr *encap_type,
73 struct rtnl_nexthop *rtnh)
77 if (e_type == LWTUNNEL_ENCAP_NONE) {
78 NL_DBG(2,
"RTA_ENCAP_TYPE should not be LWTUNNEL_ENCAP_NONE\n");
81 if (e_type > LWTUNNEL_ENCAP_MAX) {
82 NL_DBG(2,
"Unknown RTA_ENCAP_TYPE: %d\n", e_type);
86 if (!lwtunnel_encap_types[e_type].ops) {
87 NL_DBG(2,
"RTA_ENCAP_TYPE %s is not implemented\n",
88 lwtunnel_encap_types[e_type].name);
89 return -NLE_MSGTYPE_NOSUPPORT;
92 return lwtunnel_encap_types[e_type].ops->parse_msg(encap, rtnh);
95int nh_encap_compare(
struct rtnl_nh_encap *a,
struct rtnl_nh_encap *b)
100 if ((a && !b) || (!a && b) || (a->ops != b->ops))
103 if (!a->ops || !a->ops->compare)
106 return a->ops->compare(a->priv, b->priv);
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.