11#include <linux/netfilter/nf_tables.h>
14#include <libmnl/libmnl.h>
15#include <libnftnl/expr.h>
16#include <libnftnl/rule.h>
19 enum nft_tunnel_keys key;
20 enum nft_registers dreg;
23static int nftnl_expr_tunnel_set(
struct nftnl_expr *e, uint16_t type,
24 const void *data, uint32_t data_len)
29 case NFTNL_EXPR_TUNNEL_KEY:
30 memcpy(&tunnel->key, data, data_len);
32 case NFTNL_EXPR_TUNNEL_DREG:
33 memcpy(&tunnel->dreg, data, data_len);
40nftnl_expr_tunnel_get(
const struct nftnl_expr *e, uint16_t type,
46 case NFTNL_EXPR_TUNNEL_KEY:
47 *data_len =
sizeof(tunnel->key);
49 case NFTNL_EXPR_TUNNEL_DREG:
50 *data_len =
sizeof(tunnel->dreg);
56static int nftnl_expr_tunnel_cb(
const struct nlattr *attr,
void *data)
58 const struct nlattr **tb = data;
59 int type = mnl_attr_get_type(attr);
61 if (mnl_attr_type_valid(attr, NFTA_TUNNEL_MAX) < 0)
66 case NFTA_TUNNEL_DREG:
67 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
77nftnl_expr_tunnel_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
81 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_KEY))
82 mnl_attr_put_u32(nlh, NFTA_TUNNEL_KEY, htonl(tunnel->key));
83 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG))
84 mnl_attr_put_u32(nlh, NFTA_TUNNEL_DREG, htonl(tunnel->dreg));
88nftnl_expr_tunnel_parse(
struct nftnl_expr *e,
struct nlattr *attr)
91 struct nlattr *tb[NFTA_TUNNEL_MAX + 1] = {};
93 if (mnl_attr_parse_nested(attr, nftnl_expr_tunnel_cb, tb) < 0)
96 if (tb[NFTA_TUNNEL_KEY]) {
97 tunnel->key = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_KEY]));
98 e->flags |= (1 << NFTNL_EXPR_TUNNEL_KEY);
100 if (tb[NFTA_TUNNEL_DREG]) {
101 tunnel->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_TUNNEL_DREG]));
102 e->flags |= (1 << NFTNL_EXPR_TUNNEL_DREG);
108static const char *tunnel_key2str_array[NFT_TUNNEL_MAX + 1] = {
109 [NFT_TUNNEL_PATH] =
"path",
110 [NFT_TUNNEL_ID] =
"id",
113static const char *tunnel_key2str(uint8_t key)
115 if (key <= NFT_TUNNEL_MAX)
116 return tunnel_key2str_array[key];
122nftnl_expr_tunnel_snprintf(
char *buf,
size_t len,
123 uint32_t flags,
const struct nftnl_expr *e)
127 if (e->flags & (1 << NFTNL_EXPR_TUNNEL_DREG)) {
128 return snprintf(buf, len,
"load %s => reg %u ",
129 tunnel_key2str(tunnel->key), tunnel->dreg);
134static struct attr_policy tunnel_attr_policy[__NFTNL_EXPR_TUNNEL_MAX] = {
135 [NFTNL_EXPR_TUNNEL_KEY] = { .maxlen =
sizeof(uint32_t) },
136 [NFTNL_EXPR_TUNNEL_DREG] = { .maxlen =
sizeof(uint32_t) },
139struct expr_ops expr_ops_tunnel = {
142 .nftnl_max_attr = __NFTNL_EXPR_TUNNEL_MAX - 1,
143 .attr_policy = tunnel_attr_policy,
144 .set = nftnl_expr_tunnel_set,
145 .get = nftnl_expr_tunnel_get,
146 .parse = nftnl_expr_tunnel_parse,
147 .build = nftnl_expr_tunnel_build,
148 .output = nftnl_expr_tunnel_snprintf,