16#include <linux/netfilter/nf_tables.h>
19#include <libmnl/libmnl.h>
20#include <libnftnl/expr.h>
21#include <libnftnl/rule.h>
28static int nftnl_expr_last_set(
struct nftnl_expr *e, uint16_t type,
29 const void *data, uint32_t data_len)
34 case NFTNL_EXPR_LAST_MSECS:
35 memcpy(&last->msecs, data, data_len);
37 case NFTNL_EXPR_LAST_SET:
38 memcpy(&last->set, data, data_len);
44static const void *nftnl_expr_last_get(
const struct nftnl_expr *e,
45 uint16_t type, uint32_t *data_len)
50 case NFTNL_EXPR_LAST_MSECS:
51 *data_len =
sizeof(last->msecs);
53 case NFTNL_EXPR_LAST_SET:
54 *data_len =
sizeof(last->set);
60static int nftnl_expr_last_cb(
const struct nlattr *attr,
void *data)
62 int type = mnl_attr_get_type(attr);
63 const struct nlattr **tb = data;
65 if (mnl_attr_type_valid(attr, NFTA_LAST_MAX) < 0)
70 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
74 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
84nftnl_expr_last_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
88 if (e->flags & (1 << NFTNL_EXPR_LAST_MSECS))
89 mnl_attr_put_u64(nlh, NFTA_LAST_MSECS, htobe64(last->msecs));
90 if (e->flags & (1 << NFTNL_EXPR_LAST_SET))
91 mnl_attr_put_u32(nlh, NFTA_LAST_SET, htonl(last->set));
95nftnl_expr_last_parse(
struct nftnl_expr *e,
struct nlattr *attr)
98 struct nlattr *tb[NFTA_LAST_MAX + 1] = {};
100 if (mnl_attr_parse_nested(attr, nftnl_expr_last_cb, tb) < 0)
103 if (tb[NFTA_LAST_MSECS]) {
104 last->msecs = be64toh(mnl_attr_get_u64(tb[NFTA_LAST_MSECS]));
105 e->flags |= (1 << NFTNL_EXPR_LAST_MSECS);
107 if (tb[NFTA_LAST_SET]) {
108 last->set = ntohl(mnl_attr_get_u32(tb[NFTA_LAST_SET]));
109 e->flags |= (1 << NFTNL_EXPR_LAST_SET);
115static int nftnl_expr_last_snprintf(
char *buf,
size_t len,
117 const struct nftnl_expr *e)
122 return snprintf(buf, len,
"never ");
124 return snprintf(buf, len,
"%"PRIu64
" ", last->msecs);
127static struct attr_policy last_attr_policy[__NFTNL_EXPR_LAST_MAX] = {
128 [NFTNL_EXPR_LAST_MSECS] = { .maxlen =
sizeof(uint64_t) },
129 [NFTNL_EXPR_LAST_SET] = { .maxlen =
sizeof(uint32_t) },
132struct expr_ops expr_ops_last = {
135 .nftnl_max_attr = __NFTNL_EXPR_LAST_MAX - 1,
136 .attr_policy = last_attr_policy,
137 .set = nftnl_expr_last_set,
138 .get = nftnl_expr_last_get,
139 .parse = nftnl_expr_last_parse,
140 .build = nftnl_expr_last_build,
141 .output = nftnl_expr_last_snprintf,