12 #include <linux/netfilter/nf_tables.h>
15 #include <libmnl/libmnl.h>
16 #include <libnftnl/expr.h>
17 #include <libnftnl/rule.h>
24 static int nftnl_expr_last_set(
struct nftnl_expr *e, uint16_t type,
25 const void *data, uint32_t data_len)
30 case NFTNL_EXPR_LAST_MSECS:
31 memcpy(&last->msecs, data, data_len);
33 case NFTNL_EXPR_LAST_SET:
34 memcpy(&last->set, data, data_len);
40 static const void *nftnl_expr_last_get(
const struct nftnl_expr *e,
41 uint16_t type, uint32_t *data_len)
46 case NFTNL_EXPR_LAST_MSECS:
47 *data_len =
sizeof(last->msecs);
49 case NFTNL_EXPR_LAST_SET:
50 *data_len =
sizeof(last->set);
56 static int nftnl_expr_last_cb(
const struct nlattr *attr,
void *data)
58 int type = mnl_attr_get_type(attr);
59 const struct nlattr **tb = data;
61 if (mnl_attr_type_valid(attr, NFTA_LAST_MAX) < 0)
66 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
70 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
80 nftnl_expr_last_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
84 if (e->flags & (1 << NFTNL_EXPR_LAST_MSECS))
85 mnl_attr_put_u64(nlh, NFTA_LAST_MSECS, htobe64(last->msecs));
86 if (e->flags & (1 << NFTNL_EXPR_LAST_SET))
87 mnl_attr_put_u32(nlh, NFTA_LAST_SET, htonl(last->set));
91 nftnl_expr_last_parse(
struct nftnl_expr *e,
struct nlattr *attr)
94 struct nlattr *tb[NFTA_LAST_MAX + 1] = {};
96 if (mnl_attr_parse_nested(attr, nftnl_expr_last_cb, tb) < 0)
99 if (tb[NFTA_LAST_MSECS]) {
100 last->msecs = be64toh(mnl_attr_get_u64(tb[NFTA_LAST_MSECS]));
101 e->flags |= (1 << NFTNL_EXPR_LAST_MSECS);
103 if (tb[NFTA_LAST_SET]) {
104 last->set = ntohl(mnl_attr_get_u32(tb[NFTA_LAST_SET]));
105 e->flags |= (1 << NFTNL_EXPR_LAST_SET);
111 static int nftnl_expr_last_snprintf(
char *buf,
size_t len,
113 const struct nftnl_expr *e)
118 return snprintf(buf, len,
"never ");
120 return snprintf(buf, len,
"%"PRIu64
" ", last->msecs);
123 static struct attr_policy last_attr_policy[__NFTNL_EXPR_LAST_MAX] = {
124 [NFTNL_EXPR_LAST_MSECS] = { .maxlen =
sizeof(uint64_t) },
125 [NFTNL_EXPR_LAST_SET] = { .maxlen =
sizeof(uint32_t) },
128 struct expr_ops expr_ops_last = {
131 .nftnl_max_attr = __NFTNL_EXPR_LAST_MAX - 1,
132 .attr_policy = last_attr_policy,
133 .set = nftnl_expr_last_set,
134 .get = nftnl_expr_last_get,
135 .parse = nftnl_expr_last_parse,
136 .build = nftnl_expr_last_build,
137 .output = nftnl_expr_last_snprintf,