]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
rule: add chain reference counter
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 6 Jul 2015 12:29:33 +0000 (14:29 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 17 Aug 2015 23:13:35 +0000 (01:13 +0200)
When adding declared chains to the cache, we may hold more than one single
reference from struct cmd and the cache.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/rule.h
src/rule.c

index 4e68879442d6bdb09aea8c092883e2f7f71a1bf4..f137a4c80507abc8b08080e380c844504bac9d71 100644 (file)
@@ -110,6 +110,7 @@ enum chain_flags {
  * @list:      list node in table list
  * @handle:    chain handle
  * @location:  location the chain was defined at
+ * @refcnt:    reference counter
  * @flags:     chain flags
  * @hookstr:   unified and human readable hook name (base chains)
  * @hooknum:   hook number (base chains)
@@ -123,6 +124,7 @@ struct chain {
        struct list_head        list;
        struct handle           handle;
        struct location         location;
+       unsigned int            refcnt;
        uint32_t                flags;
        const char              *hookstr;
        unsigned int            hooknum;
@@ -137,6 +139,7 @@ struct chain {
 extern const char *chain_type_name_lookup(const char *name);
 extern const char *chain_hookname_lookup(const char *name);
 extern struct chain *chain_alloc(const char *name);
+extern struct chain *chain_get(struct chain *chain);
 extern void chain_free(struct chain *chain);
 extern void chain_add_hash(struct chain *chain, struct table *table);
 extern struct chain *chain_lookup(const struct table *table,
index 6e1b823a12994624292f2151e79c6de6842baf73..6887a1a7080f0ffcdcabbd72a3c72f6d0e8ae0b3 100644 (file)
@@ -444,6 +444,7 @@ struct chain *chain_alloc(const char *name)
        struct chain *chain;
 
        chain = xzalloc(sizeof(*chain));
+       chain->refcnt = 1;
        init_list_head(&chain->rules);
        init_list_head(&chain->scope.symbols);
        if (name != NULL)
@@ -453,10 +454,18 @@ struct chain *chain_alloc(const char *name)
        return chain;
 }
 
+struct chain *chain_get(struct chain *chain)
+{
+       chain->refcnt++;
+       return chain;
+}
+
 void chain_free(struct chain *chain)
 {
        struct rule *rule, *next;
 
+       if (--chain->refcnt > 0)
+               return;
        list_for_each_entry_safe(rule, next, &chain->rules, list)
                rule_free(rule);
        handle_free(&chain->handle);