void nftnl_attr_put_ifname(struct nlmsghdr *nlh, const char *ifname);
char *nftnl_attr_get_ifname(const struct nlattr *attr);
+int nftnl_parse_str_attr(const struct nlattr *tb, int attr,
+ const char **field, uint32_t *flags);
+
#endif
if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_chain_parse_attr_cb, tb) < 0)
return -1;
- if (tb[NFTA_CHAIN_NAME]) {
- if (c->flags & (1 << NFTNL_CHAIN_NAME))
- xfree(c->name);
- c->name = strdup(mnl_attr_get_str(tb[NFTA_CHAIN_NAME]));
- if (!c->name)
- return -1;
- c->flags |= (1 << NFTNL_CHAIN_NAME);
- }
- if (tb[NFTA_CHAIN_TABLE]) {
- if (c->flags & (1 << NFTNL_CHAIN_TABLE))
- xfree(c->table);
- c->table = strdup(mnl_attr_get_str(tb[NFTA_CHAIN_TABLE]));
- if (!c->table)
- return -1;
- c->flags |= (1 << NFTNL_CHAIN_TABLE);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_CHAIN_NAME], NFTNL_CHAIN_NAME,
+ &c->name, &c->flags) < 0)
+ return -1;
+ if (nftnl_parse_str_attr(tb[NFTA_CHAIN_TABLE], NFTNL_CHAIN_TABLE,
+ &c->table, &c->flags) < 0)
+ return -1;
if (tb[NFTA_CHAIN_HOOK]) {
ret = nftnl_chain_parse_hook(tb[NFTA_CHAIN_HOOK], c);
if (ret < 0)
c->handle = be64toh(mnl_attr_get_u64(tb[NFTA_CHAIN_HANDLE]));
c->flags |= (1 << NFTNL_CHAIN_HANDLE);
}
- if (tb[NFTA_CHAIN_TYPE]) {
- if (c->flags & (1 << NFTNL_CHAIN_TYPE))
- xfree(c->type);
- c->type = strdup(mnl_attr_get_str(tb[NFTA_CHAIN_TYPE]));
- if (!c->type)
- return -1;
- c->flags |= (1 << NFTNL_CHAIN_TYPE);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_CHAIN_TYPE], NFTNL_CHAIN_TYPE,
+ &c->type, &c->flags) < 0)
+ return -1;
if (tb[NFTA_CHAIN_FLAGS]) {
c->chain_flags = ntohl(mnl_attr_get_u32(tb[NFTA_CHAIN_FLAGS]));
c->flags |= (1 << NFTNL_CHAIN_FLAGS);
dynset->timeout = be64toh(mnl_attr_get_u64(tb[NFTA_DYNSET_TIMEOUT]));
e->flags |= (1 << NFTNL_EXPR_DYNSET_TIMEOUT);
}
- if (tb[NFTA_DYNSET_SET_NAME]) {
- dynset->set_name =
- strdup(mnl_attr_get_str(tb[NFTA_DYNSET_SET_NAME]));
- if (!dynset->set_name)
- return -1;
- e->flags |= (1 << NFTNL_EXPR_DYNSET_SET_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_DYNSET_SET_NAME],
+ NFTNL_EXPR_DYNSET_SET_NAME,
+ (const char **)&dynset->set_name,
+ &e->flags) < 0)
+ return -1;
if (tb[NFTA_DYNSET_SET_ID]) {
dynset->set_id = ntohl(mnl_attr_get_u32(tb[NFTA_DYNSET_SET_ID]));
e->flags |= (1 << NFTNL_EXPR_DYNSET_SET_ID);
if (mnl_attr_parse_nested(attr, nftnl_expr_flow_cb, tb) < 0)
return -1;
- if (tb[NFTA_FLOW_TABLE_NAME]) {
- flow->table_name =
- strdup(mnl_attr_get_str(tb[NFTA_FLOW_TABLE_NAME]));
- if (!flow->table_name)
- return -1;
- e->flags |= (1 << NFTNL_EXPR_FLOW_TABLE_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_FLOW_TABLE_NAME],
+ NFTNL_EXPR_FLOW_TABLE_NAME,
+ (const char **)&flow->table_name,
+ &e->flags) < 0)
+ return -1;
return ret;
}
if (mnl_attr_parse_nested(attr, nftnl_expr_log_cb, tb) < 0)
return -1;
- if (tb[NFTA_LOG_PREFIX]) {
- if (log->prefix)
- xfree(log->prefix);
-
- log->prefix = strdup(mnl_attr_get_str(tb[NFTA_LOG_PREFIX]));
- if (!log->prefix)
- return -1;
- e->flags |= (1 << NFTNL_EXPR_LOG_PREFIX);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_LOG_PREFIX],
+ NFTNL_EXPR_LOG_PREFIX,
+ &log->prefix, &e->flags) < 0)
+ return -1;
if (tb[NFTA_LOG_GROUP]) {
log->group = ntohs(mnl_attr_get_u16(tb[NFTA_LOG_GROUP]));
e->flags |= (1 << NFTNL_EXPR_LOG_GROUP);
lookup->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_LOOKUP_DREG]));
e->flags |= (1 << NFTNL_EXPR_LOOKUP_DREG);
}
- if (tb[NFTA_LOOKUP_SET]) {
- lookup->set_name =
- strdup(mnl_attr_get_str(tb[NFTA_LOOKUP_SET]));
- if (!lookup->set_name)
- return -1;
- e->flags |= (1 << NFTNL_EXPR_LOOKUP_SET);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_LOOKUP_SET],
+ NFTNL_EXPR_LOOKUP_SET,
+ (const char **)&lookup->set_name,
+ &e->flags) < 0)
+ return -1;
if (tb[NFTA_LOOKUP_SET_ID]) {
lookup->set_id =
ntohl(mnl_attr_get_u32(tb[NFTA_LOOKUP_SET_ID]));
ntohl(mnl_attr_get_u32(tb[NFTA_OBJREF_IMM_TYPE]));
e->flags |= (1 << NFTNL_EXPR_OBJREF_IMM_TYPE);
}
- if (tb[NFTA_OBJREF_IMM_NAME]) {
- objref->imm.name =
- strdup(mnl_attr_get_str(tb[NFTA_OBJREF_IMM_NAME]));
- e->flags |= (1 << NFTNL_EXPR_OBJREF_IMM_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_OBJREF_IMM_NAME],
+ NFTNL_EXPR_OBJREF_IMM_NAME,
+ &objref->imm.name, &e->flags) < 0)
+ return -1;
if (tb[NFTA_OBJREF_SET_SREG]) {
objref->set.sreg =
ntohl(mnl_attr_get_u32(tb[NFTA_OBJREF_SET_SREG]));
e->flags |= (1 << NFTNL_EXPR_OBJREF_SET_SREG);
}
- if (tb[NFTA_OBJREF_SET_NAME]) {
- objref->set.name =
- strdup(mnl_attr_get_str(tb[NFTA_OBJREF_SET_NAME]));
- e->flags |= (1 << NFTNL_EXPR_OBJREF_SET_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_OBJREF_SET_NAME],
+ NFTNL_EXPR_OBJREF_SET_NAME,
+ &objref->set.name, &e->flags) < 0)
+ return -1;
if (tb[NFTA_OBJREF_SET_ID]) {
objref->set.id =
ntohl(mnl_attr_get_u32(tb[NFTA_OBJREF_SET_ID]));
if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_flowtable_parse_attr_cb, tb) < 0)
return -1;
- if (tb[NFTA_FLOWTABLE_NAME]) {
- if (c->flags & (1 << NFTNL_FLOWTABLE_NAME))
- xfree(c->name);
- c->name = strdup(mnl_attr_get_str(tb[NFTA_FLOWTABLE_NAME]));
- if (!c->name)
- return -1;
- c->flags |= (1 << NFTNL_FLOWTABLE_NAME);
- }
- if (tb[NFTA_FLOWTABLE_TABLE]) {
- if (c->flags & (1 << NFTNL_FLOWTABLE_TABLE))
- xfree(c->table);
- c->table = strdup(mnl_attr_get_str(tb[NFTA_FLOWTABLE_TABLE]));
- if (!c->table)
- return -1;
- c->flags |= (1 << NFTNL_FLOWTABLE_TABLE);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_FLOWTABLE_NAME],
+ NFTNL_FLOWTABLE_NAME,
+ &c->name, &c->flags) < 0)
+ return -1;
+ if (nftnl_parse_str_attr(tb[NFTA_FLOWTABLE_TABLE],
+ NFTNL_FLOWTABLE_TABLE,
+ &c->table, &c->flags) < 0)
+ return -1;
if (tb[NFTA_FLOWTABLE_HOOK]) {
ret = nftnl_flowtable_parse_hook(tb[NFTA_FLOWTABLE_HOOK], c);
if (ret < 0)
if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_obj_parse_attr_cb, tb) < 0)
return -1;
- if (tb[NFTA_OBJ_TABLE]) {
- obj->table = strdup(mnl_attr_get_str(tb[NFTA_OBJ_TABLE]));
- obj->flags |= (1 << NFTNL_OBJ_TABLE);
- }
- if (tb[NFTA_OBJ_NAME]) {
- obj->name = strdup(mnl_attr_get_str(tb[NFTA_OBJ_NAME]));
- obj->flags |= (1 << NFTNL_OBJ_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_OBJ_TABLE], NFTNL_OBJ_TABLE,
+ &obj->table, &obj->flags) < 0)
+ return -1;
+ if (nftnl_parse_str_attr(tb[NFTA_OBJ_NAME], NFTNL_OBJ_NAME,
+ &obj->name, &obj->flags) < 0)
+ return -1;
if (tb[NFTA_OBJ_TYPE]) {
uint32_t type = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_TYPE]));
if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_rule_parse_attr_cb, tb) < 0)
return -1;
- if (tb[NFTA_RULE_TABLE]) {
- if (r->flags & (1 << NFTNL_RULE_TABLE))
- xfree(r->table);
- r->table = strdup(mnl_attr_get_str(tb[NFTA_RULE_TABLE]));
- if (!r->table)
- return -1;
- r->flags |= (1 << NFTNL_RULE_TABLE);
- }
- if (tb[NFTA_RULE_CHAIN]) {
- if (r->flags & (1 << NFTNL_RULE_CHAIN))
- xfree(r->chain);
- r->chain = strdup(mnl_attr_get_str(tb[NFTA_RULE_CHAIN]));
- if (!r->chain)
- return -1;
- r->flags |= (1 << NFTNL_RULE_CHAIN);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_RULE_TABLE], NFTNL_RULE_TABLE,
+ &r->table, &r->flags) < 0)
+ return -1;
+ if (nftnl_parse_str_attr(tb[NFTA_RULE_CHAIN], NFTNL_RULE_CHAIN,
+ &r->chain, &r->flags) < 0)
+ return -1;
if (tb[NFTA_RULE_HANDLE]) {
r->handle = be64toh(mnl_attr_get_u64(tb[NFTA_RULE_HANDLE]));
r->flags |= (1 << NFTNL_RULE_HANDLE);
if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_set_parse_attr_cb, tb) < 0)
return -1;
- if (tb[NFTA_SET_TABLE]) {
- if (s->flags & (1 << NFTNL_SET_TABLE))
- xfree(s->table);
- s->table = strdup(mnl_attr_get_str(tb[NFTA_SET_TABLE]));
- if (!s->table)
- return -1;
- s->flags |= (1 << NFTNL_SET_TABLE);
- }
- if (tb[NFTA_SET_NAME]) {
- if (s->flags & (1 << NFTNL_SET_NAME))
- xfree(s->name);
- s->name = strdup(mnl_attr_get_str(tb[NFTA_SET_NAME]));
- if (!s->name)
- return -1;
- s->flags |= (1 << NFTNL_SET_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_SET_TABLE], NFTNL_SET_TABLE,
+ &s->table, &s->flags) < 0)
+ return -1;
+ if (nftnl_parse_str_attr(tb[NFTA_SET_NAME], NFTNL_SET_NAME,
+ &s->name, &s->flags) < 0)
+ return -1;
if (tb[NFTA_SET_HANDLE]) {
s->handle = be64toh(mnl_attr_get_u64(tb[NFTA_SET_HANDLE]));
s->flags |= (1 << NFTNL_SET_HANDLE);
memcpy(e->user.data, udata, e->user.len);
e->flags |= (1 << NFTNL_RULE_USERDATA);
}
- if (tb[NFTA_SET_ELEM_OBJREF]) {
- e->objref = strdup(mnl_attr_get_str(tb[NFTA_SET_ELEM_OBJREF]));
- if (e->objref == NULL) {
- ret = -1;
- goto out_set_elem;
- }
- e->flags |= (1 << NFTNL_SET_ELEM_OBJREF);
+ if (nftnl_parse_str_attr(tb[NFTA_SET_ELEM_OBJREF],
+ NFTNL_SET_ELEM_OBJREF,
+ &e->objref, &e->flags) < 0) {
+ ret = -1;
+ goto out_set_elem;
}
/* Add this new element to this set */
nftnl_set_elem_list_parse_attr_cb, tb) < 0)
return -1;
- if (tb[NFTA_SET_ELEM_LIST_TABLE]) {
- if (s->flags & (1 << NFTNL_SET_TABLE))
- xfree(s->table);
- s->table =
- strdup(mnl_attr_get_str(tb[NFTA_SET_ELEM_LIST_TABLE]));
- if (!s->table)
- return -1;
- s->flags |= (1 << NFTNL_SET_TABLE);
- }
- if (tb[NFTA_SET_ELEM_LIST_SET]) {
- if (s->flags & (1 << NFTNL_SET_NAME))
- xfree(s->name);
- s->name =
- strdup(mnl_attr_get_str(tb[NFTA_SET_ELEM_LIST_SET]));
- if (!s->name)
- return -1;
- s->flags |= (1 << NFTNL_SET_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_SET_ELEM_LIST_TABLE],
+ NFTNL_SET_TABLE,
+ &s->table, &s->flags) < 0)
+ return -1;
+ if (nftnl_parse_str_attr(tb[NFTA_SET_ELEM_LIST_SET],
+ NFTNL_SET_NAME,
+ &s->name, &s->flags) < 0)
+ return -1;
if (tb[NFTA_SET_ELEM_LIST_SET_ID]) {
s->id = ntohl(mnl_attr_get_u32(tb[NFTA_SET_ELEM_LIST_SET_ID]));
s->flags |= (1 << NFTNL_SET_ID);
if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_table_parse_attr_cb, tb) < 0)
return -1;
- if (tb[NFTA_TABLE_NAME]) {
- if (t->flags & (1 << NFTNL_TABLE_NAME))
- xfree(t->name);
- t->name = strdup(mnl_attr_get_str(tb[NFTA_TABLE_NAME]));
- if (!t->name)
- return -1;
- t->flags |= (1 << NFTNL_TABLE_NAME);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_TABLE_NAME], NFTNL_TABLE_NAME,
+ &t->name, &t->flags) < 0)
+ return -1;
if (tb[NFTA_TABLE_FLAGS]) {
t->table_flags = ntohl(mnl_attr_get_u32(tb[NFTA_TABLE_FLAGS]));
t->flags |= (1 << NFTNL_TABLE_FLAGS);
case NFT_JUMP:
if (!tb[NFTA_VERDICT_CHAIN])
abi_breakage();
- t->jump_target = strdup(mnl_attr_get_str(tb[NFTA_VERDICT_CHAIN]));
- if (!t->jump_target)
+ if (nftnl_parse_str_attr(tb[NFTA_VERDICT_CHAIN],
+ NFTNL_TRACE_JUMP_TARGET,
+ (const char **)&t->jump_target,
+ &t->flags) < 0)
return -1;
-
- t->flags |= (1 << NFTNL_TRACE_JUMP_TARGET);
break;
}
return 0;
t->id = ntohl(mnl_attr_get_u32(tb[NFTA_TRACE_ID]));
t->flags |= (1 << NFTNL_TRACE_ID);
- if (tb[NFTA_TRACE_TABLE]) {
- t->table = strdup(mnl_attr_get_str(tb[NFTA_TRACE_TABLE]));
- if (!t->table)
- return -1;
-
- t->flags |= (1 << NFTNL_TRACE_TABLE);
- }
-
- if (tb[NFTA_TRACE_CHAIN]) {
- t->chain = strdup(mnl_attr_get_str(tb[NFTA_TRACE_CHAIN]));
- if (!t->chain)
- return -1;
+ if (nftnl_parse_str_attr(tb[NFTA_TRACE_TABLE], NFTNL_TRACE_TABLE,
+ (const char **)&t->table, &t->flags) < 0)
+ return -1;
- t->flags |= (1 << NFTNL_TRACE_CHAIN);
- }
+ if (nftnl_parse_str_attr(tb[NFTA_TRACE_CHAIN], NFTNL_TRACE_CHAIN,
+ (const char **)&t->chain, &t->flags) < 0)
+ return -1;
if (tb[NFTA_TRACE_IIFTYPE]) {
t->iiftype = ntohs(mnl_attr_get_u16(tb[NFTA_TRACE_IIFTYPE]));
return NULL;
}
}
+
+int nftnl_parse_str_attr(const struct nlattr *tb, int attr,
+ const char **field, uint32_t *flags)
+{
+ if (!tb)
+ return 0;
+
+ if (*flags & (1 << attr))
+ xfree(*field);
+ *field = strdup(mnl_attr_get_str(tb));
+ if (!*field)
+ return -1;
+ *flags |= (1 << attr);
+ return 0;
+}