]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
mnl: remove alloc_nftnl_table()
authorPablo Neira Ayuso <pablo@netfilter.org>
Wed, 26 Sep 2018 14:20:08 +0000 (16:20 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 4 Oct 2018 00:13:58 +0000 (02:13 +0200)
The netlink layer sits in between the mnl and the rule layers, remove
it. We can remove alloc_nftnl_table() and consolidate infrastructure in
the src/mnl.c file.

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

index 36109c7ad94ef0417de9a5bdd9f75a8b0bd472a4..44dd90f918142b57e3daba3d4b54d2771b93874f 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <list.h>
 #include <netlink.h>
+#include <rule.h>
 #include <libmnl/libmnl.h>
 
 struct mnl_socket *netlink_open_sock(void);
@@ -42,10 +43,10 @@ int mnl_nft_chain_batch_del(struct nftnl_chain *nlc, struct nftnl_batch *batch,
 struct nftnl_chain_list *mnl_nft_chain_dump(struct netlink_ctx *ctx,
                                            int family);
 
-int mnl_nft_table_batch_add(struct nftnl_table *nlt, struct nftnl_batch *batch,
-                           unsigned int flags, uint32_t seqnum);
-int mnl_nft_table_batch_del(struct nftnl_table *nlt, struct nftnl_batch *batch,
-                           unsigned int flags, uint32_t seqnum);
+int mnl_nft_table_add(struct netlink_ctx *ctx, const struct cmd *cmd,
+                     unsigned int flags);
+int mnl_nft_table_del(struct netlink_ctx *ctx, const struct cmd *cmd);
+
 struct nftnl_table_list *mnl_nft_table_dump(struct netlink_ctx *ctx,
                                            int family);
 
index 4925af04a707c9d0bd92a5d0a15fd33fff74598e..42c3eb902a1edb4decda4701cc6c328d12f2b651 100644 (file)
@@ -57,7 +57,6 @@ struct netlink_ctx {
        struct nft_cache        *cache;
 };
 
-extern struct nftnl_table *alloc_nftnl_table(const struct handle *h);
 extern struct nftnl_chain *alloc_nftnl_chain(const struct handle *h);
 extern struct nftnl_rule *alloc_nftnl_rule(const struct handle *h);
 extern struct nftnl_expr *alloc_nft_expr(const char *name);
@@ -130,10 +129,6 @@ extern int netlink_flush_chain(struct netlink_ctx *ctx, const struct cmd *cmd);
 extern struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx,
                                               const struct nftnl_chain *nlc);
 
-extern int netlink_add_table_batch(struct netlink_ctx *ctx,
-                                  const struct cmd *cmd, uint32_t flags);
-extern int netlink_delete_table_batch(struct netlink_ctx *ctx,
-                                     const struct cmd *cmd);
 extern int netlink_list_tables(struct netlink_ctx *ctx, const struct handle *h);
 extern int netlink_list_table(struct netlink_ctx *ctx, const struct handle *h);
 extern int netlink_flush_table(struct netlink_ctx *ctx, const struct cmd *cmd);
@@ -205,8 +200,6 @@ extern int netlink_io_error(struct netlink_ctx *ctx,
        __netlink_init_error(__FILE__, __LINE__, strerror(errno));
 extern void __noreturn __netlink_init_error(const char *file, int line, const char *reason);
 
-extern int netlink_flush_ruleset(struct netlink_ctx *ctx, const struct cmd *cmd);
-
 extern struct nftnl_ruleset *netlink_dump_ruleset(struct netlink_ctx *ctx,
                                                const struct handle *h,
                                                const struct location *loc);
index 6a6d45ce71db025d8de86c3d6506c9dace5adf4f..8cc4f168829c99e014b89b333c965a2b8ccec64e 100644 (file)
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -468,32 +468,63 @@ err:
 /*
  * Table
  */
-int mnl_nft_table_batch_add(struct nftnl_table *nlt, struct nftnl_batch *batch,
-                           unsigned int flags, uint32_t seqnum)
+int mnl_nft_table_add(struct netlink_ctx *ctx, const struct cmd *cmd,
+                     unsigned int flags)
 {
+       struct nftnl_table *nlt;
        struct nlmsghdr *nlh;
 
-       nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(batch),
+       nlt = nftnl_table_alloc();
+       if (nlt == NULL)
+               memory_allocation_error();
+
+       nftnl_table_set_u32(nlt, NFTNL_TABLE_FAMILY, cmd->handle.family);
+       nftnl_table_set(nlt, NFTNL_TABLE_NAME, cmd->handle.table.name);
+       if (cmd->table)
+               nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, cmd->table->flags);
+       else
+               nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, 0);
+
+       nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch),
                                    NFT_MSG_NEWTABLE,
-                                   nftnl_table_get_u32(nlt, NFTNL_TABLE_FAMILY),
-                                   flags, seqnum);
+                                   cmd->handle.family,
+                                   flags, ctx->seqnum);
        nftnl_table_nlmsg_build_payload(nlh, nlt);
-       mnl_nft_batch_continue(batch);
+       nftnl_table_free(nlt);
+
+       mnl_nft_batch_continue(ctx->batch);
 
        return 0;
 }
 
-int mnl_nft_table_batch_del(struct nftnl_table *nlt, struct nftnl_batch *batch,
-                           unsigned int flags, uint32_t seqnum)
+int mnl_nft_table_del(struct netlink_ctx *ctx, const struct cmd *cmd)
 {
+       struct nftnl_table *nlt;
        struct nlmsghdr *nlh;
 
-       nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(batch),
+       nlt = nftnl_table_alloc();
+       if (nlt == NULL)
+               memory_allocation_error();
+
+       nftnl_table_set_u32(nlt, NFTNL_TABLE_FAMILY, cmd->handle.family);
+       if (cmd->handle.table.name)
+               nftnl_table_set(nlt, NFTNL_TABLE_NAME, cmd->handle.table.name);
+       if (cmd->handle.handle.id)
+               nftnl_table_set_u64(nlt, NFTNL_TABLE_HANDLE,
+                                   cmd->handle.handle.id);
+
+       nlt = nftnl_table_alloc();
+       if (nlt == NULL)
+               memory_allocation_error();
+
+       nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch),
                                    NFT_MSG_DELTABLE,
-                                   nftnl_table_get_u32(nlt, NFTNL_TABLE_FAMILY),
-                                   NLM_F_ACK, seqnum);
+                                   cmd->handle.family,
+                                   NLM_F_ACK, ctx->seqnum);
        nftnl_table_nlmsg_build_payload(nlh, nlt);
-       mnl_nft_batch_continue(batch);
+       nftnl_table_free(nlt);
+
+       mnl_nft_batch_continue(ctx->batch);
 
        return 0;
 }
index f40678f8c01b246597c036f5743995eb3c68edd3..f84c050102f5057c1a5bce70340180ab79131d25 100644 (file)
@@ -111,23 +111,6 @@ void __noreturn __netlink_init_error(const char *filename, int line,
        exit(NFT_EXIT_NONL);
 }
 
-struct nftnl_table *alloc_nftnl_table(const struct handle *h)
-{
-       struct nftnl_table *nlt;
-
-       nlt = nftnl_table_alloc();
-       if (nlt == NULL)
-               memory_allocation_error();
-
-       nftnl_table_set_u32(nlt, NFTNL_TABLE_FAMILY, h->family);
-       if (h->table.name != NULL)
-               nftnl_table_set(nlt, NFTNL_TABLE_NAME, h->table.name);
-       if (h->handle.id)
-               nftnl_table_set_u64(nlt, NFTNL_TABLE_HANDLE, h->handle.id);
-
-       return nlt;
-}
-
 struct nftnl_chain *alloc_nftnl_chain(const struct handle *h)
 {
        struct nftnl_chain *nlc;
@@ -733,36 +716,6 @@ int netlink_flush_chain(struct netlink_ctx *ctx, const struct cmd *cmd)
        return netlink_del_rule_batch(ctx, cmd);
 }
 
-int netlink_add_table_batch(struct netlink_ctx *ctx, const struct cmd *cmd,
-                           uint32_t flags)
-{
-       struct nftnl_table *nlt;
-       int err;
-
-       nlt = alloc_nftnl_table(&cmd->handle);
-       if (cmd->table != NULL)
-               nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, cmd->table->flags);
-       else
-               nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, 0);
-
-       err = mnl_nft_table_batch_add(nlt, ctx->batch, flags, ctx->seqnum);
-       nftnl_table_free(nlt);
-
-       return err;
-}
-
-int netlink_delete_table_batch(struct netlink_ctx *ctx, const struct cmd *cmd)
-{
-       struct nftnl_table *nlt;
-       int err;
-
-       nlt = alloc_nftnl_table(&cmd->handle);
-       err = mnl_nft_table_batch_del(nlt, ctx->batch, 0, ctx->seqnum);
-       nftnl_table_free(nlt);
-
-       return err;
-}
-
 struct table *netlink_delinearize_table(struct netlink_ctx *ctx,
                                        const struct nftnl_table *nlt)
 {
@@ -1652,18 +1605,6 @@ int netlink_batch_send(struct netlink_ctx *ctx, struct list_head *err_list)
        return mnl_batch_talk(ctx, err_list);
 }
 
-int netlink_flush_ruleset(struct netlink_ctx *ctx, const struct cmd *cmd)
-{
-       struct nftnl_table *nlt;
-       int err;
-
-       nlt = alloc_nftnl_table(&cmd->handle);
-       err = mnl_nft_table_batch_del(nlt, ctx->batch, 0, ctx->seqnum);
-       nftnl_table_free(nlt);
-
-       return err;
-}
-
 struct nftnl_ruleset *netlink_dump_ruleset(struct netlink_ctx *ctx,
                                         const struct handle *h,
                                         const struct location *loc)
index 32b13b19b6e14104a4d64decd4347cf25b9c09b4..81d5c3e9f41f8c4893c0cd17a7fe6d978394a316 100644 (file)
@@ -21,6 +21,7 @@
 #include <utils.h>
 #include <netdb.h>
 #include <netlink.h>
+#include <mnl.h>
 #include <json.h>
 
 #include <libnftnl/common.h>
@@ -1409,7 +1410,7 @@ static int do_command_add(struct netlink_ctx *ctx, struct cmd *cmd, bool excl)
 
        switch (cmd->obj) {
        case CMD_OBJ_TABLE:
-               return netlink_add_table_batch(ctx, cmd, flags);
+               return mnl_nft_table_add(ctx, cmd, flags);
        case CMD_OBJ_CHAIN:
                return netlink_add_chain_batch(ctx, cmd, flags);
        case CMD_OBJ_RULE:
@@ -1492,7 +1493,7 @@ static int do_command_delete(struct netlink_ctx *ctx, struct cmd *cmd)
 {
        switch (cmd->obj) {
        case CMD_OBJ_TABLE:
-               return netlink_delete_table_batch(ctx, cmd);
+               return mnl_nft_table_del(ctx, cmd);
        case CMD_OBJ_CHAIN:
                return netlink_delete_chain_batch(ctx, cmd);
        case CMD_OBJ_RULE:
@@ -2267,7 +2268,7 @@ static int do_command_flush(struct netlink_ctx *ctx, struct cmd *cmd)
        case CMD_OBJ_METER:
                return netlink_flush_setelems(ctx, cmd);
        case CMD_OBJ_RULESET:
-               return netlink_flush_ruleset(ctx, cmd);
+               return mnl_nft_table_del(ctx, cmd);
        default:
                BUG("invalid command object type %u\n", cmd->obj);
        }