15#include <linux/netfilter/nf_tables.h>
18#include <libmnl/libmnl.h>
19#include <libnftnl/expr.h>
20#include <libnftnl/rule.h>
23 enum nft_tunnel_keys key;
24 enum nft_registers dreg;
27static int nftnl_expr_tunnel_set(
struct nftnl_expr *e, uint16_t type,
28 const void *data, uint32_t data_len)
33 case NFTNL_EXPR_TUNNEL_KEY:
34 memcpy(&tunnel->key, data, data_len);
36 case NFTNL_EXPR_TUNNEL_DREG:
37 memcpy(&tunnel->dreg, data, data_len);
44nftnl_expr_tunnel_get(
const struct nftnl_expr *e, uint16_t type,
50 case NFTNL_EXPR_TUNNEL_KEY:
51 *data_len =
sizeof(tunnel->key);
53 case NFTNL_EXPR_TUNNEL_DREG:
54 *data_len =
sizeof(tunnel->dreg);
60static int nftnl_expr_tunnel_cb(
const struct nlattr *attr,
void *data)
62 const struct nlattr **tb = data;
63 int type = mnl_attr_get_type(attr);
65 if (mnl_attr_type_valid(attr, NFTA_TUNNEL_MAX) < 0)
70 case NFTA_TUNNEL_DREG:
71 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
81nftnl_expr_tunnel_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
85 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_KEY))
86 mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY, htonl(tunnel->key));
87 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG))
88 mnl_attr_put_u32(nlh, NFTA_TUNNEL_DREG, htonl(tunnel->dreg));
92nftnl_expr_tunnel_parse(
struct nftnl_expr *e,
struct nlattr *attr)
95 struct nlattr *tb[NFTA_TUNNEL_MAX + 1] = {};
97 if (mnl_attr_parse_nested(attr, nftnl_expr_tunnel_cb, tb) < 0)
100 if (tb[NFTA_TUNNEL_KEY]) {
101 tunnel->key = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY]));
102 e->flags |= (1 << NFTNL_EXPR_TUNNEL_KEY);
104 if (tb[NFTA_TUNNEL_DREG]) {
105 tunnel->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_DREG]));
106 e->flags |= (1 << NFTNL_EXPR_TUNNEL_DREG);
112static const char *tunnel_key2str_array[NFT_TUNNEL_MAX + 1] = {
113 [NFT_TUNNEL_PATH] =
"path",
114 [NFT_TUNNEL_ID] =
"id",
117static const char *tunnel_key2str(uint8_t key)
119 if (key <= NFT_TUNNEL_MAX)
120 return tunnel_key2str_array[key];
126nftnl_expr_tunnel_snprintf(
char *buf,
size_t len,
127 uint32_t flags,
const struct nftnl_expr *e)
131 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG)) {
132 return snprintf(buf, len,
"load %s => reg %u ",
133 tunnel_key2str(tunnel->key), tunnel->dreg);
138static struct attr_policy tunnel_attr_policy[__NFTNL_EXPR_TUNNEL_MAX] = {
139 [NFTNL_EXPR_TUNNEL_KEY] = { .maxlen =
sizeof(uint32_t) },
140 [NFTNL_EXPR_TUNNEL_DREG] = { .maxlen =
sizeof(uint32_t) },
143struct expr_ops expr_ops_tunnel = {
146 .nftnl_max_attr = __NFTNL_EXPR_TUNNEL_MAX - 1,
147 .attr_policy = tunnel_attr_policy,
148 .set = nftnl_expr_tunnel_set,
149 .get = nftnl_expr_tunnel_get,
150 .parse = nftnl_expr_tunnel_parse,
151 .build = nftnl_expr_tunnel_build,
152 .output = nftnl_expr_tunnel_snprintf,