11#include <linux/netfilter/nf_tables.h>
14#include <libmnl/libmnl.h>
15#include <libnftnl/expr.h>
16#include <libnftnl/rule.h>
20 enum nft_registers dreg;
24nftnl_expr_rt_set(
struct nftnl_expr *e, uint16_t type,
25 const void *data, uint32_t data_len)
30 case NFTNL_EXPR_RT_KEY:
31 memcpy(&rt->key, data, data_len);
33 case NFTNL_EXPR_RT_DREG:
34 memcpy(&rt->dreg, data, data_len);
41nftnl_expr_rt_get(
const struct nftnl_expr *e, uint16_t type,
47 case NFTNL_EXPR_RT_KEY:
48 *data_len =
sizeof(rt->key);
50 case NFTNL_EXPR_RT_DREG:
51 *data_len =
sizeof(rt->dreg);
57static int nftnl_expr_rt_cb(
const struct nlattr *attr,
void *data)
59 const struct nlattr **tb = data;
60 int type = mnl_attr_get_type(attr);
62 if (mnl_attr_type_valid(attr, NFTA_RT_MAX) < 0)
68 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
78nftnl_expr_rt_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
82 if (e->flags & (1 << NFTNL_EXPR_RT_KEY))
83 mnl_attr_put_u32(nlh, NFTA_RT_KEY, htonl(rt->key));
84 if (e->flags & (1 << NFTNL_EXPR_RT_DREG))
85 mnl_attr_put_u32(nlh, NFTA_RT_DREG, htonl(rt->dreg));
89nftnl_expr_rt_parse(
struct nftnl_expr *e,
struct nlattr *attr)
92 struct nlattr *tb[NFTA_RT_MAX+1] = {};
94 if (mnl_attr_parse_nested(attr, nftnl_expr_rt_cb, tb) < 0)
97 if (tb[NFTA_RT_KEY]) {
98 rt->key = ntohl(mnl_attr_get_u32(tb[NFTA_RT_KEY]));
99 e->flags |= (1 << NFTNL_EXPR_RT_KEY);
101 if (tb[NFTA_RT_DREG]) {
102 rt->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_RT_DREG]));
103 e->flags |= (1 << NFTNL_EXPR_RT_DREG);
109static const char *rt_key2str_array[NFT_RT_MAX + 1] = {
110 [NFT_RT_CLASSID] =
"classid",
111 [NFT_RT_NEXTHOP4] =
"nexthop4",
112 [NFT_RT_NEXTHOP6] =
"nexthop6",
113 [NFT_RT_TCPMSS] =
"tcpmss",
114 [NFT_RT_XFRM] =
"ipsec",
117static const char *rt_key2str(uint8_t key)
119 if (key <= NFT_RT_MAX)
120 return rt_key2str_array[key];
126nftnl_expr_rt_snprintf(
char *buf,
size_t len,
127 uint32_t flags,
const struct nftnl_expr *e)
131 if (e->flags & (1 << NFTNL_EXPR_RT_DREG)) {
132 return snprintf(buf, len,
"load %s => reg %u ",
133 rt_key2str(rt->key), rt->dreg);
138static struct attr_policy rt_attr_policy[__NFTNL_EXPR_RT_MAX] = {
139 [NFTNL_EXPR_RT_KEY] = { .maxlen =
sizeof(uint32_t) },
140 [NFTNL_EXPR_RT_DREG] = { .maxlen =
sizeof(uint32_t) },
143struct expr_ops expr_ops_rt = {
146 .nftnl_max_attr = __NFTNL_EXPR_RT_MAX - 1,
147 .attr_policy = rt_attr_policy,
148 .set = nftnl_expr_rt_set,
149 .get = nftnl_expr_rt_get,
150 .parse = nftnl_expr_rt_parse,
151 .build = nftnl_expr_rt_build,
152 .output = nftnl_expr_rt_snprintf,