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,
sizeof(last->msecs));
37 case NFTNL_EXPR_LAST_SET:
38 memcpy(&last->set, data,
sizeof(last->set));
46static const void *nftnl_expr_last_get(
const struct nftnl_expr *e,
47 uint16_t type, uint32_t *data_len)
52 case NFTNL_EXPR_LAST_MSECS:
53 *data_len =
sizeof(last->msecs);
55 case NFTNL_EXPR_LAST_SET:
56 *data_len =
sizeof(last->set);
62static int nftnl_expr_last_cb(
const struct nlattr *attr,
void *data)
64 int type = mnl_attr_get_type(attr);
65 const struct nlattr **tb = data;
67 if (mnl_attr_type_valid(attr, NFTA_LAST_MAX) < 0)
72 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
76 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
86nftnl_expr_last_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
90 if (e->flags & (1 << NFTNL_EXPR_LAST_MSECS))
91 mnl_attr_put_u64(nlh, NFTA_LAST_MSECS, htobe64(last->msecs));
92 if (e->flags & (1 << NFTNL_EXPR_LAST_SET))
93 mnl_attr_put_u32(nlh, NFTA_LAST_SET, htonl(last->set));
97nftnl_expr_last_parse(
struct nftnl_expr *e,
struct nlattr *attr)
100 struct nlattr *tb[NFTA_LAST_MAX + 1] = {};
102 if (mnl_attr_parse_nested(attr, nftnl_expr_last_cb, tb) < 0)
105 if (tb[NFTA_LAST_MSECS]) {
106 last->msecs = be64toh(mnl_attr_get_u64(tb[NFTA_LAST_MSECS]));
107 e->flags |= (1 << NFTNL_EXPR_LAST_MSECS);
109 if (tb[NFTA_LAST_SET]) {
110 last->set = ntohl(mnl_attr_get_u32(tb[NFTA_LAST_SET]));
111 e->flags |= (1 << NFTNL_EXPR_LAST_SET);
117static int nftnl_expr_last_snprintf(
char *buf,
size_t len,
119 const struct nftnl_expr *e)
124 return snprintf(buf, len,
"never ");
126 return snprintf(buf, len,
"%"PRIu64
" ", last->msecs);
129struct expr_ops expr_ops_last = {
132 .max_attr = NFTA_LAST_MAX,
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,