"Could not fetch rule set generation id: %s\n", nft_strerror(errno));
}
+static struct nft_table *nft_table_alloc(void)
+{
+ struct nftnl_table *nftnl;
+ struct nft_table *table;
+
+ table = malloc(sizeof(struct nft_table));
+ if (!table)
+ return NULL;
+
+ nftnl = nftnl_table_alloc();
+ if (!nftnl) {
+ free(table);
+ return NULL;
+ }
+ table->nftnl = nftnl;
+
+ return table;
+}
+
+static void nft_table_free(struct nft_table *table)
+{
+ nftnl_table_free(table->nftnl);
+ free(table);
+}
+
+static void nft_table_list_free(struct list_head *table_list)
+{
+ struct nft_table *table, *next;
+
+ list_for_each_entry_safe(table, next, table_list, list) {
+ list_del(&table->list);
+ nft_table_free(table);
+ }
+}
+
static int nftnl_table_list_cb(const struct nlmsghdr *nlh, void *data)
{
- struct nftnl_table *t;
- struct nftnl_table_list *list = data;
+ struct list_head *list = data;
+ struct nft_table *t;
- t = nftnl_table_alloc();
- if (t == NULL)
+ t = nft_table_alloc();
+ if (!t)
goto err;
- if (nftnl_table_nlmsg_parse(nlh, t) < 0)
+ if (nftnl_table_nlmsg_parse(nlh, t->nftnl) < 0)
goto out;
- nftnl_table_list_add_tail(t, list);
+ list_add_tail(&t->list, list);
return MNL_CB_OK;
out:
- nftnl_table_free(t);
+ nft_table_free(t);
err:
return MNL_CB_OK;
}
static int fetch_table_cache(struct nft_handle *h)
{
- char buf[16536];
struct nlmsghdr *nlh;
- struct nftnl_table_list *list;
+ char buf[16536];
int i, ret;
- if (h->cache->tables)
- return 0;
-
- list = nftnl_table_list_alloc();
- if (list == NULL)
+ if (!list_empty(&h->cache->tables))
return 0;
nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, h->family,
NLM_F_DUMP, h->seq);
- ret = mnl_talk(h, nlh, nftnl_table_list_cb, list);
+ ret = mnl_talk(h, nlh, nftnl_table_list_cb, &h->cache->tables);
if (ret < 0 && errno == EINTR)
assert(nft_restart(h) >= 0);
- h->cache->tables = list;
-
for (i = 0; i < NFT_TABLE_MAX; i++) {
enum nft_table_type type = h->tables[i].type;
c->table[i].sets = NULL;
}
}
- if (c->tables) {
- nftnl_table_list_free(c->tables);
- c->tables = NULL;
- }
+ if (!list_empty(&c->tables))
+ nft_table_list_free(&c->tables);
return 1;
}
}
}
-struct nftnl_table_list *nftnl_table_list_get(struct nft_handle *h)
+struct list_head *nft_table_list_get(struct nft_handle *h)
{
- return h->cache->tables;
+ return &h->cache->tables;
}
struct nftnl_set_list *
INIT_LIST_HEAD(&h->obj_list);
INIT_LIST_HEAD(&h->err_list);
INIT_LIST_HEAD(&h->cmd_list);
+ INIT_LIST_HEAD(&h->__cache[0].tables);
+ INIT_LIST_HEAD(&h->__cache[1].tables);
INIT_LIST_HEAD(&h->cache_req.chain_list);
return 0;
bool nft_table_find(struct nft_handle *h, const char *tablename)
{
- struct nftnl_table_list_iter *iter;
- struct nftnl_table_list *list;
- struct nftnl_table *t;
+ struct list_head *list;
+ struct nft_table *t;
bool ret = false;
- list = nftnl_table_list_get(h);
- if (list == NULL)
- goto err;
-
- iter = nftnl_table_list_iter_create(list);
- if (iter == NULL)
- goto err;
+ list = nft_table_list_get(h);
- t = nftnl_table_list_iter_next(iter);
- while (t != NULL) {
+ list_for_each_entry(t, list, list) {
const char *this_tablename =
- nftnl_table_get(t, NFTNL_TABLE_NAME);
+ nftnl_table_get(t->nftnl, NFTNL_TABLE_NAME);
if (strcmp(tablename, this_tablename) == 0) {
ret = true;
break;
}
-
- t = nftnl_table_list_iter_next(iter);
}
- nftnl_table_list_iter_destroy(iter);
-
-err:
return ret;
}
int (*func)(struct nft_handle *h, const char *tablename, void *data),
void *data)
{
- struct nftnl_table_list *list;
- struct nftnl_table_list_iter *iter;
- struct nftnl_table *t;
-
- list = nftnl_table_list_get(h);
- if (list == NULL)
- return -1;
+ struct list_head *list;
+ struct nft_table *t;
- iter = nftnl_table_list_iter_create(list);
- if (iter == NULL)
- return -1;
+ list = nft_table_list_get(h);
- t = nftnl_table_list_iter_next(iter);
- while (t != NULL) {
+ list_for_each_entry(t, list, list) {
const char *tablename =
- nftnl_table_get(t, NFTNL_TABLE_NAME);
+ nftnl_table_get(t->nftnl, NFTNL_TABLE_NAME);
func(h, tablename, data);
-
- t = nftnl_table_list_iter_next(iter);
}
- nftnl_table_list_iter_destroy(iter);
return 0;
}
int nft_table_flush(struct nft_handle *h, const char *table)
{
- struct nftnl_table_list_iter *iter;
- struct nftnl_table_list *list;
- struct nftnl_table *t;
+ struct list_head *list;
+ struct nft_table *t;
bool exists = false;
int ret = 0;
nft_fn = nft_table_flush;
- list = nftnl_table_list_get(h);
- if (list == NULL) {
- ret = -1;
- goto err_out;
- }
-
- iter = nftnl_table_list_iter_create(list);
- if (iter == NULL) {
- ret = -1;
- goto err_table_list;
- }
+ list = nft_table_list_get(h);
- t = nftnl_table_list_iter_next(iter);
- while (t != NULL) {
+ list_for_each_entry(t, list, list) {
const char *table_name =
- nftnl_table_get_str(t, NFTNL_TABLE_NAME);
+ nftnl_table_get_str(t->nftnl, NFTNL_TABLE_NAME);
if (strcmp(table_name, table) == 0) {
exists = true;
break;
}
-
- t = nftnl_table_list_iter_next(iter);
}
ret = __nft_table_flush(h, table, exists);
- nftnl_table_list_iter_destroy(iter);
-err_table_list:
-err_out:
+
/* the core expects 1 for success and 0 for error */
return ret == 0 ? 1 : 0;
}