]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
netlink: monitor: add a helper function to handle sets referenced by a rule
authorArturo Borrero <arturo.borrero.glez@gmail.com>
Mon, 14 Jul 2014 11:56:46 +0000 (13:56 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 21 Jul 2014 12:20:47 +0000 (14:20 +0200)
This patch adds a helper function to handle lookup expressions with a callback,
so we can make an action for each set referenced by the rule.

Basically this is a refactorization, useful for follow-up patches.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
src/netlink.c

index afad5a463b9e6cf42f511fc7583e8b2c35c00049..c0597971b34459734ebac0d4f497ae9584898ce7 100644 (file)
@@ -1540,6 +1540,42 @@ static uint32_t netlink_msg2nftnl_of(uint32_t msg)
        return 0;
 }
 
+static void nlr_for_each_set(struct nft_rule *nlr,
+                            void (*cb)(struct set *s, void *data),
+                            void *data)
+{
+       struct set *s;
+       uint32_t family;
+       const char *set_name, *table;
+       struct nft_rule_expr *nlre;
+       struct nft_rule_expr_iter *nlrei;
+       const char *name;
+
+       nlrei = nft_rule_expr_iter_create(nlr);
+       if (nlrei == NULL)
+               memory_allocation_error();
+
+       family = nft_rule_attr_get_u32(nlr, NFT_RULE_ATTR_FAMILY);
+       table = nft_rule_attr_get_str(nlr, NFT_RULE_ATTR_TABLE);
+
+       nlre = nft_rule_expr_iter_next(nlrei);
+       while (nlre != NULL) {
+               name = nft_rule_expr_get_str(nlre, NFT_RULE_EXPR_ATTR_NAME);
+               if (strcmp(name, "lookup") != 0)
+                       goto next;
+
+               set_name = nft_rule_expr_get_str(nlre, NFT_EXPR_LOOKUP_SET);
+               s = set_lookup_global(family, table, set_name);
+               if (s == NULL)
+                       goto next;
+
+               cb(s, data);
+next:
+               nlre = nft_rule_expr_iter_next(nlrei);
+       }
+       nft_rule_expr_iter_destroy(nlrei);
+}
+
 static int netlink_events_table_cb(const struct nlmsghdr *nlh, int type,
                                   struct netlink_mon_handler *monh)
 {
@@ -1863,42 +1899,19 @@ out:
        nft_set_free(nls);
 }
 
+static void netlink_events_cache_delset_cb(struct set *s,
+                                          void *data)
+{
+       list_del(&s->list);
+       set_free(s);
+}
+
 static void netlink_events_cache_delsets(struct netlink_mon_handler *monh,
                                         const struct nlmsghdr *nlh)
 {
-       struct set *s;
-       uint32_t family;
-       struct nft_rule_expr *nlre;
-       struct nft_rule_expr_iter *nlrei;
-       const char *expr_name, *set_name, *table;
        struct nft_rule *nlr = netlink_rule_alloc(nlh);
 
-       nlrei = nft_rule_expr_iter_create(nlr);
-       if (nlrei == NULL)
-               memory_allocation_error();
-
-       family = nft_rule_attr_get_u32(nlr, NFT_RULE_ATTR_FAMILY);
-       table = nft_rule_attr_get_str(nlr, NFT_RULE_ATTR_TABLE);
-
-       nlre = nft_rule_expr_iter_next(nlrei);
-       while (nlre != NULL) {
-               expr_name = nft_rule_expr_get_str(nlre,
-                                                 NFT_RULE_EXPR_ATTR_NAME);
-               if (strcmp(expr_name, "lookup") != 0)
-                       goto next;
-
-               set_name = nft_rule_expr_get_str(nlre, NFT_EXPR_LOOKUP_SET);
-               s = set_lookup_global(family, table, set_name);
-               if (s == NULL)
-                       goto next;
-
-               list_del(&s->list);
-               set_free(s);
-next:
-               nlre = nft_rule_expr_iter_next(nlrei);
-       }
-       nft_rule_expr_iter_destroy(nlrei);
-
+       nlr_for_each_set(nlr, netlink_events_cache_delset_cb, NULL);
        nft_rule_free(nlr);
 }