]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
rule: add helper function to expand chain rules into commands
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 6 Feb 2023 14:28:40 +0000 (15:28 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 7 Feb 2023 10:38:23 +0000 (11:38 +0100)
This patch adds a helper function to expand chain rules into commands.
This comes in preparation for the follow up patch.

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

index a58fd1f2483a6c07062f7bd73ed1df03f32a79bb..3f239cf8c46af97836b0401e81706f75efc8508d 100644 (file)
@@ -1310,13 +1310,31 @@ void cmd_add_loc(struct cmd *cmd, uint16_t offset, const struct location *loc)
        cmd->num_attrs++;
 }
 
+static void nft_cmd_expand_chain(struct chain *chain, struct list_head *new_cmds)
+{
+       struct rule *rule;
+       struct handle h;
+       struct cmd *new;
+
+       list_for_each_entry(rule, &chain->rules, list) {
+               memset(&h, 0, sizeof(h));
+               handle_merge(&h, &rule->handle);
+               if (chain->flags & CHAIN_F_BINDING) {
+                       rule->handle.chain_id = chain->handle.chain_id;
+                       rule->handle.chain.location = chain->location;
+               }
+               new = cmd_alloc(CMD_ADD, CMD_OBJ_RULE, &h,
+                               &rule->location, rule_get(rule));
+               list_add_tail(&new->list, new_cmds);
+       }
+}
+
 void nft_cmd_expand(struct cmd *cmd)
 {
        struct list_head new_cmds;
        struct flowtable *ft;
        struct table *table;
        struct chain *chain;
-       struct rule *rule;
        struct set *set;
        struct obj *obj;
        struct cmd *new;
@@ -1362,22 +1380,9 @@ void nft_cmd_expand(struct cmd *cmd)
                                        &ft->location, flowtable_get(ft));
                        list_add_tail(&new->list, &new_cmds);
                }
-               list_for_each_entry(chain, &table->chains, list) {
-                       list_for_each_entry(rule, &chain->rules, list) {
-                               memset(&h, 0, sizeof(h));
-                               handle_merge(&h, &rule->handle);
-                               if (chain->flags & CHAIN_F_BINDING) {
-                                       rule->handle.chain_id =
-                                               chain->handle.chain_id;
-                                       rule->handle.chain.location =
-                                               chain->location;
-                               }
-                               new = cmd_alloc(CMD_ADD, CMD_OBJ_RULE, &h,
-                                               &rule->location,
-                                               rule_get(rule));
-                               list_add_tail(&new->list, &new_cmds);
-                       }
-               }
+               list_for_each_entry(chain, &table->chains, list)
+                       nft_cmd_expand_chain(chain, &new_cmds);
+
                list_splice(&new_cmds, &cmd->list);
                break;
        case CMD_OBJ_SET: