17 #include <arpa/inet.h>
20 #include <libmnl/libmnl.h>
21 #include <linux/netfilter/nf_tables.h>
22 #include <libnftnl/expr.h>
23 #include <libnftnl/rule.h>
26 union nftnl_data_reg data;
27 enum nft_registers sreg;
32 nftnl_expr_cmp_set(
struct nftnl_expr *e, uint16_t type,
33 const void *data, uint32_t data_len)
38 case NFTNL_EXPR_CMP_SREG:
39 memcpy(&cmp->sreg, data, data_len);
41 case NFTNL_EXPR_CMP_OP:
42 memcpy(&cmp->op, data, data_len);
44 case NFTNL_EXPR_CMP_DATA:
45 return nftnl_data_cpy(&cmp->data, data, data_len);
51 nftnl_expr_cmp_get(
const struct nftnl_expr *e, uint16_t type,
57 case NFTNL_EXPR_CMP_SREG:
58 *data_len =
sizeof(cmp->sreg);
60 case NFTNL_EXPR_CMP_OP:
61 *data_len =
sizeof(cmp->op);
63 case NFTNL_EXPR_CMP_DATA:
64 *data_len = cmp->data.len;
65 return &cmp->data.val;
70 static int nftnl_expr_cmp_cb(
const struct nlattr *attr,
void *data)
72 const struct nlattr **tb = data;
73 int type = mnl_attr_get_type(attr);
75 if (mnl_attr_type_valid(attr, NFTA_CMP_MAX) < 0)
81 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
85 if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
95 nftnl_expr_cmp_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
99 if (e->flags & (1 << NFTNL_EXPR_CMP_SREG))
100 mnl_attr_put_u32(nlh, NFTA_CMP_SREG, htonl(cmp->sreg));
101 if (e->flags & (1 << NFTNL_EXPR_CMP_OP))
102 mnl_attr_put_u32(nlh, NFTA_CMP_OP, htonl(cmp->op));
103 if (e->flags & (1 << NFTNL_EXPR_CMP_DATA)) {
106 nest = mnl_attr_nest_start(nlh, NFTA_CMP_DATA);
107 mnl_attr_put(nlh, NFTA_DATA_VALUE, cmp->data.len, cmp->data.val);
108 mnl_attr_nest_end(nlh, nest);
113 nftnl_expr_cmp_parse(
struct nftnl_expr *e,
struct nlattr *attr)
116 struct nlattr *tb[NFTA_CMP_MAX+1] = {};
119 if (mnl_attr_parse_nested(attr, nftnl_expr_cmp_cb, tb) < 0)
122 if (tb[NFTA_CMP_SREG]) {
123 cmp->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_CMP_SREG]));
124 e->flags |= (1 << NFTA_CMP_SREG);
126 if (tb[NFTA_CMP_OP]) {
127 cmp->op = ntohl(mnl_attr_get_u32(tb[NFTA_CMP_OP]));
128 e->flags |= (1 << NFTA_CMP_OP);
130 if (tb[NFTA_CMP_DATA]) {
131 ret = nftnl_parse_data(&cmp->data, tb[NFTA_CMP_DATA], NULL);
132 e->flags |= (1 << NFTA_CMP_DATA);
138 static const char *expr_cmp_str[] = {
140 [NFT_CMP_NEQ] =
"neq",
142 [NFT_CMP_LTE] =
"lte",
144 [NFT_CMP_GTE] =
"gte",
147 static const char *cmp2str(uint32_t op)
149 if (op > NFT_CMP_GTE)
152 return expr_cmp_str[op];
155 static inline int nftnl_str2cmp(
const char *op)
157 if (strcmp(op,
"eq") == 0)
159 else if (strcmp(op,
"neq") == 0)
161 else if (strcmp(op,
"lt") == 0)
163 else if (strcmp(op,
"lte") == 0)
165 else if (strcmp(op,
"gt") == 0)
167 else if (strcmp(op,
"gte") == 0)
176 nftnl_expr_cmp_snprintf(
char *buf,
size_t remain,
177 uint32_t flags,
const struct nftnl_expr *e)
182 ret = snprintf(buf, remain,
"%s reg %u ",
183 cmp2str(cmp->op), cmp->sreg);
184 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
186 ret = nftnl_data_reg_snprintf(buf + offset, remain, &cmp->data,
188 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
193 static struct attr_policy cmp_attr_policy[__NFTNL_EXPR_CMP_MAX] = {
194 [NFTNL_EXPR_CMP_SREG] = { .maxlen =
sizeof(uint32_t) },
195 [NFTNL_EXPR_CMP_OP] = { .maxlen =
sizeof(uint32_t) },
196 [NFTNL_EXPR_CMP_DATA] = { .maxlen = NFT_DATA_VALUE_MAXLEN }
199 struct expr_ops expr_ops_cmp = {
202 .nftnl_max_attr = __NFTNL_EXPR_CMP_MAX - 1,
203 .attr_policy = cmp_attr_policy,
204 .set = nftnl_expr_cmp_set,
205 .get = nftnl_expr_cmp_get,
206 .parse = nftnl_expr_cmp_parse,
207 .build = nftnl_expr_cmp_build,
208 .output = nftnl_expr_cmp_snprintf,