summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2017-02-16 14:27:57 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2017-02-16 14:27:57 +0100
commitaea99eefb9e0e77999932ff5912bbc7da2784614 (patch)
tree5bcec783866525b24e3354173d77a8cff6f0ca45 /src
parentef63a897a00671e0dc791ecc3a56f9bdbfee9429 (diff)
rule: add NFTA_RULE_ID attribute
This patch adds the new NFTA_RULE_ID attribute. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src')
-rw-r--r--src/rule.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/rule.c b/src/rule.c
index 02d013b..31cd3ed 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -38,6 +38,7 @@ struct nftnl_rule {
const char *chain;
uint64_t handle;
uint64_t position;
+ uint32_t id;
struct {
void *data;
uint32_t len;
@@ -105,6 +106,7 @@ void nftnl_rule_unset(struct nftnl_rule *r, uint16_t attr)
case NFTNL_RULE_COMPAT_FLAGS:
case NFTNL_RULE_POSITION:
case NFTNL_RULE_FAMILY:
+ case NFTNL_RULE_ID:
break;
case NFTNL_RULE_USERDATA:
xfree(r->user.data);
@@ -120,7 +122,8 @@ static uint32_t nftnl_rule_validate[NFTNL_RULE_MAX + 1] = {
[NFTNL_RULE_COMPAT_PROTO] = sizeof(uint32_t),
[NFTNL_RULE_COMPAT_FLAGS] = sizeof(uint32_t),
[NFTNL_RULE_FAMILY] = sizeof(uint32_t),
- [NFTNL_RULE_POSITION] = sizeof(uint64_t),
+ [NFTNL_RULE_POSITION] = sizeof(uint64_t),
+ [NFTNL_RULE_ID] = sizeof(uint32_t),
};
int nftnl_rule_set_data(struct nftnl_rule *r, uint16_t attr,
@@ -172,6 +175,9 @@ int nftnl_rule_set_data(struct nftnl_rule *r, uint16_t attr,
memcpy(r->user.data, data, data_len);
r->user.len = data_len;
break;
+ case NFTNL_RULE_ID:
+ r->id = *((uint32_t *)data);
+ break;
}
r->flags |= (1 << attr);
return 0;
@@ -233,6 +239,9 @@ const void *nftnl_rule_get_data(const struct nftnl_rule *r, uint16_t attr,
case NFTNL_RULE_USERDATA:
*data_len = r->user.len;
return r->user.data;
+ case NFTNL_RULE_ID:
+ *data_len = sizeof(uint32_t);
+ return &r->id;
}
return NULL;
}
@@ -322,6 +331,8 @@ void nftnl_rule_nlmsg_build_payload(struct nlmsghdr *nlh, struct nftnl_rule *r)
htonl(r->compat.flags));
mnl_attr_nest_end(nlh, nest);
}
+ if (r->flags & (1 << NFTNL_RULE_ID))
+ mnl_attr_put_u32(nlh, NFTA_RULE_ID, htonl(r->id));
}
EXPORT_SYMBOL(nftnl_rule_nlmsg_build_payload);
@@ -361,6 +372,10 @@ static int nftnl_rule_parse_attr_cb(const struct nlattr *attr, void *data)
if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
abi_breakage();
break;
+ case NFTA_RULE_ID:
+ if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ abi_breakage();
+ break;
}
tb[type] = attr;
@@ -484,6 +499,10 @@ int nftnl_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_rule *r)
memcpy(r->user.data, udata, r->user.len);
r->flags |= (1 << NFTNL_RULE_USERDATA);
}
+ if (tb[NFTA_RULE_ID]) {
+ r->id = ntohl(mnl_attr_get_u32(tb[NFTA_RULE_ID]));
+ r->flags |= (1 << NFTNL_RULE_ID);
+ }
r->family = nfg->nfgen_family;
r->flags |= (1 << NFTNL_RULE_FAMILY);
@@ -562,6 +581,13 @@ int nftnl_jansson_parse_rule(struct nftnl_rule *r, json_t *tree,
nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, uval64);
}
+ if (nftnl_jansson_node_exist(root, "id")) {
+ if (nftnl_jansson_parse_val(root, "id", NFTNL_TYPE_U32,
+ &uval32, err) < 0)
+ goto err;
+ nftnl_rule_set_u32(r, NFTNL_RULE_COMPAT_PROTO, uval32);
+ }
+
array = json_object_get(root, "expr");
if (array == NULL) {
err->error = NFTNL_PARSE_EMISSINGNODE;
@@ -692,6 +718,11 @@ static int nftnl_rule_snprintf_json(char *buf, size_t size,
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
}
+ if (r->flags & (1 << NFTNL_RULE_ID)) {
+ ret = snprintf(buf+offset, len, "\"id\":%u,", r->id);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
ret = snprintf(buf+offset, len, "\"expr\":[");
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
@@ -760,6 +791,11 @@ static int nftnl_rule_snprintf_default(char *buf, size_t size,
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
}
+ if (r->flags & (1 << NFTNL_RULE_ID)) {
+ ret = snprintf(buf + offset, len, "%u ", r->id);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
ret = snprintf(buf+offset, len, "\n");
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);