]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
chains: add chain rename support
authorPatrick McHardy <kaber@trash.net>
Fri, 14 Dec 2012 16:39:22 +0000 (17:39 +0100)
committerPatrick McHardy <kaber@trash.net>
Fri, 14 Dec 2012 16:39:22 +0000 (17:39 +0100)
Signed-off-by: Patrick McHardy <kaber@trash.net>
include/netlink.h
include/rule.h
src/evaluate.c
src/netlink.c
src/parser.y
src/rule.c
src/scanner.l

index 22ef489c9f744dcaa74b58aca07d418d82d0b505..8edd1a3916a5b6fdd50bfb3765ad50411c51d4f5 100644 (file)
@@ -57,6 +57,8 @@ extern int netlink_get_rule(struct netlink_ctx *ctx, const struct handle *h);
 
 extern int netlink_add_chain(struct netlink_ctx *ctx, const struct handle *h,
                             const struct chain *chain);
+extern int netlink_rename_chain(struct netlink_ctx *ctx, const struct handle *h,
+                               const char *name);
 extern int netlink_delete_chain(struct netlink_ctx *ctx, const struct handle *h);
 extern int netlink_list_chains(struct netlink_ctx *ctx, const struct handle *h);
 extern int netlink_get_chain(struct netlink_ctx *ctx, const struct handle *h);
index 4a458d31167bfbe232108265a6b67479d22048a4..e468e8e8feb81f0f55769cf7822633c097e6be93 100644 (file)
@@ -190,6 +190,7 @@ extern void set_print(const struct set *set);
  * @CMD_DELETE:                delete object
  * @CMD_LIST:          list container
  * @CMD_FLUSH:         flush container
+ * @CMD_RENAME:                rename object
  */
 enum cmd_ops {
        CMD_INVALID,
@@ -197,6 +198,7 @@ enum cmd_ops {
        CMD_DELETE,
        CMD_LIST,
        CMD_FLUSH,
+       CMD_RENAME,
 };
 
 /**
@@ -229,6 +231,7 @@ enum cmd_obj {
  * @obj:       object type to perform operation on
  * @handle:    handle for operations working without full objects
  * @union:     object
+ * @arg:       argument data
  */
 struct cmd {
        struct list_head        list;
@@ -244,6 +247,7 @@ struct cmd {
                struct chain    *chain;
                struct table    *table;
        };
+       const void              *arg;
 };
 
 extern struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj,
index 906c10022f7678655fbb7b704d208844fa979c49..28681d0d80a3db218af21d4a0da2bfc30a5d72d4 100644 (file)
@@ -1311,6 +1311,7 @@ static int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
                return cmd_evaluate_delete(ctx, cmd);
        case CMD_LIST:
        case CMD_FLUSH:
+       case CMD_RENAME:
                return 0;
        default:
                BUG("invalid command operation %u\n", cmd->op);
index 7e4f49068fda39f11180d8259558b39b6be693f9..53ef2ba9d990321da20d65d619648c5452aba4ee 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
+ * Copyright (c) 2008-2012 Patrick McHardy <kaber@trash.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -100,6 +100,8 @@ struct nfnl_nft_chain *alloc_nft_chain(const struct handle *h)
                memory_allocation_error();
        nfnl_nft_chain_set_family(nlc, h->family);
        nfnl_nft_chain_set_table(nlc, h->table, strlen(h->table) + 1);
+       if (h->handle != 0)
+               nfnl_nft_chain_set_handle(nlc, h->handle);
        if (h->chain != NULL)
                nfnl_nft_chain_set_name(nlc, h->chain, strlen(h->chain) + 1);
        return nlc;
@@ -437,6 +439,24 @@ int netlink_add_chain(struct netlink_ctx *ctx, const struct handle *h,
        return err;
 }
 
+int netlink_rename_chain(struct netlink_ctx *ctx, const struct handle *h,
+                        const char *name)
+{
+       struct nfnl_nft_chain *nlc;
+       int err;
+
+       nlc = alloc_nft_chain(h);
+       nfnl_nft_chain_set_name(nlc, name, strlen(name) + 1);
+       netlink_dump_object(OBJ_CAST(nlc));
+       err = nfnl_nft_chain_add(nf_sock, nlc, 0);
+       nfnl_nft_chain_put(nlc);
+
+       if (err < 0)
+               netlink_io_error(ctx, NULL, "Could not rename chain: %s",
+                                nl_geterror(err));
+       return err;
+}
+
 int netlink_delete_chain(struct netlink_ctx *ctx, const struct handle *h)
 {
        struct nfnl_nft_chain *nlc;
@@ -470,6 +490,7 @@ static void list_chain_cb(struct nl_object *obj, void *arg)
        chain = chain_alloc(nfnl_nft_chain_get_name(nlc));
        chain->handle.family = nfnl_nft_chain_get_family(nlc);
        chain->handle.table  = xstrdup(nfnl_nft_chain_get_table(nlc));
+       chain->handle.handle = nfnl_nft_chain_get_handle(nlc);
        chain->hooknum       = nfnl_nft_chain_get_hooknum(nlc);
        chain->priority      = nfnl_nft_chain_get_priority(nlc);
        list_add_tail(&chain->list, &ctx->list);
index b0d56e5207a9eb1d2cecc8162c0f1291ddc0bc12..2b0700db739962a2af33dd1196d3b33cd669955a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007-2008 Patrick McHardy <kaber@trash.net>
+ * Copyright (c) 2007-2012 Patrick McHardy <kaber@trash.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -169,6 +169,7 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %token DELETE                  "delete"
 %token LIST                    "list"
 %token FLUSH                   "flush"
+%token RENAME                  "rename"
 %token DESCRIBE                        "describe"
 
 %token ACCEPT                  "accept"
@@ -327,8 +328,8 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %type <cmd>                    line
 %destructor { cmd_free($$); }  line
 
-%type <cmd>                    base_cmd add_cmd delete_cmd list_cmd flush_cmd
-%destructor { cmd_free($$); }  base_cmd add_cmd delete_cmd list_cmd flush_cmd
+%type <cmd>                    base_cmd add_cmd delete_cmd list_cmd flush_cmd rename_cmd
+%destructor { cmd_free($$); }  base_cmd add_cmd delete_cmd list_cmd flush_cmd rename_cmd
 
 %type <handle>                 table_spec chain_spec chain_identifier ruleid_spec
 %destructor { handle_free(&$$); } table_spec chain_spec chain_identifier ruleid_spec
@@ -511,6 +512,7 @@ base_cmd            :       /* empty */     add_cmd         { $$ = $1; }
                        |       DELETE          delete_cmd      { $$ = $2; }
                        |       LIST            list_cmd        { $$ = $2; }
                        |       FLUSH           flush_cmd       { $$ = $2; }
+                       |       RENAME          rename_cmd      { $$ = $2; }
                        |       DESCRIBE        primary_expr
                        {
                                expr_describe($2);
@@ -623,6 +625,13 @@ flush_cmd          :       TABLE           table_spec
                        }
                        ;
 
+rename_cmd             :       CHAIN           chain_spec      identifier
+                       {
+                               $$ = cmd_alloc(CMD_RENAME, CMD_OBJ_CHAIN, &$2, NULL);
+                               $$->arg = $3;
+                       }
+                       ;
+
 table_block_alloc      :       /* empty */
                        {
                                $$ = table_alloc();
index 61e16e755efc69da87ea51fe0ee8e0e9ff77d43a..f671117d77fb2d9b4fd412e2e8c1254e1a252763 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
+ * Copyright (c) 2008-2012 Patrick McHardy <kaber@trash.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -332,6 +332,7 @@ void cmd_free(struct cmd *cmd)
                        BUG("invalid command object type %u\n", cmd->obj);
                }
        }
+       xfree(cmd->arg);
        xfree(cmd);
 }
 
@@ -524,6 +525,31 @@ static int do_command_flush(struct netlink_ctx *ctx, struct cmd *cmd)
        return 0;
 }
 
+static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd)
+{
+       struct table *table;
+       struct chain *chain;
+       int err;
+
+       table = table_alloc();
+       handle_merge(&table->handle, &cmd->handle);
+       table_add_hash(table);
+
+       switch (cmd->obj) {
+       case CMD_OBJ_CHAIN:
+               err = netlink_get_chain(ctx, &cmd->handle);
+               if (err < 0)
+                       return err;
+               list_splice_tail_init(&ctx->list, &table->chains);
+               chain = chain_lookup(table, &cmd->handle);
+
+               return netlink_rename_chain(ctx, &chain->handle, cmd->arg);
+       default:
+               BUG("invalid command object type %u\n", cmd->obj);
+       }
+       return 0;
+}
+
 int do_command(struct netlink_ctx *ctx, struct cmd *cmd)
 {
        switch (cmd->op) {
@@ -535,6 +561,8 @@ int do_command(struct netlink_ctx *ctx, struct cmd *cmd)
                return do_command_list(ctx, cmd);
        case CMD_FLUSH:
                return do_command_flush(ctx, cmd);
+       case CMD_RENAME:
+               return do_command_rename(ctx, cmd);
        default:
                BUG("invalid command object type %u\n", cmd->obj);
        }
index de2373da45dd11b1f1a2574836afc49501a46585..47397691f26d7abecdfad2c504faa5a7e1c7d4c2 100644 (file)
@@ -245,6 +245,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
 "delete"               { return DELETE; }
 "list"                 { return LIST; }
 "flush"                        { return FLUSH; }
+"rename"               { return RENAME; }
 
 "counter"              { return COUNTER; }