]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: add table netlink messages to the batch
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 19 May 2014 10:21:46 +0000 (12:21 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 19 May 2014 10:21:48 +0000 (12:21 +0200)
This patch moves the table messages to the netlink batch that
is sent to kernel-space.

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

index a86da069bc2357a912c9dc7fa044c22acfcb0d54..ed5a718bef07dd2066ed0f6f7da6e646bf912af7 100644 (file)
@@ -48,8 +48,12 @@ int mnl_nft_chain_get(struct mnl_socket *nf_sock, struct nft_chain *nlc,
 
 int mnl_nft_table_add(struct mnl_socket *nf_sock, struct nft_table *nlt,
                      unsigned int flags);
+int mnl_nft_table_batch_add(struct mnl_socket *nf_sock, struct nft_table *nlt,
+                           unsigned int flags, uint32_t seq);
 int mnl_nft_table_delete(struct mnl_socket *nf_sock, struct nft_table *nlt,
-                     unsigned int flags);
+                        unsigned int flags);
+int mnl_nft_table_batch_del(struct mnl_socket *nf_sock, struct nft_table *nlt,
+                           unsigned int flags, uint32_t seq);
 struct nft_table_list *mnl_nft_table_dump(struct mnl_socket *nf_sock,
                                          int family);
 int mnl_nft_table_get(struct mnl_socket *nf_sock, struct nft_table *nlt,
index fc360a4bae0ccdedbf8e13855c08eb34de2dc269..a816106675de2a22c7148e5fb544c463ce692a54 100644 (file)
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -550,6 +550,23 @@ int mnl_nft_table_add(struct mnl_socket *nf_sock, struct nft_table *nlt,
        return nft_mnl_talk(nf_sock, nlh, nlh->nlmsg_len, NULL, NULL);
 }
 
+int mnl_nft_table_batch_add(struct mnl_socket *nf_sock, struct nft_table *nlt,
+                           unsigned int flags, uint32_t seqnum)
+{
+       struct nlmsghdr *nlh;
+
+       nlh = nft_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
+                       NFT_MSG_NEWTABLE,
+                       nft_table_attr_get_u32(nlt, NFT_TABLE_ATTR_FAMILY),
+                       flags, seqnum);
+       nft_table_nlmsg_build_payload(nlh, nlt);
+
+       if (!mnl_nlmsg_batch_next(batch))
+               mnl_batch_page_add();
+
+       return 0;
+}
+
 int mnl_nft_table_delete(struct mnl_socket *nf_sock, struct nft_table *nlt,
                      unsigned int flags)
 {
@@ -564,6 +581,23 @@ int mnl_nft_table_delete(struct mnl_socket *nf_sock, struct nft_table *nlt,
        return nft_mnl_talk(nf_sock, nlh, nlh->nlmsg_len, NULL, NULL);
 }
 
+int mnl_nft_table_batch_del(struct mnl_socket *nf_sock, struct nft_table *nlt,
+                           unsigned int flags, uint32_t seqnum)
+{
+       struct nlmsghdr *nlh;
+
+       nlh = nft_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
+                       NFT_MSG_DELTABLE,
+                       nft_table_attr_get_u32(nlt, NFT_TABLE_ATTR_FAMILY),
+                       NLM_F_ACK, seqnum);
+       nft_table_nlmsg_build_payload(nlh, nlt);
+
+       if (!mnl_nlmsg_batch_next(batch))
+               mnl_batch_page_add();
+
+       return 0;
+}
+
 static int table_cb(const struct nlmsghdr *nlh, void *data)
 {
        struct nft_table_list *nlt_list = data;
index 10a00fa8346b3b5e114230310238df562896f75c..e13076775673576c262991b931201703b59f6f67 100644 (file)
@@ -757,9 +757,10 @@ int netlink_flush_chain(struct netlink_ctx *ctx, const struct handle *h,
        return netlink_del_rule_batch(ctx, h, loc);
 }
 
-int netlink_add_table(struct netlink_ctx *ctx, const struct handle *h,
-                     const struct location *loc, const struct table *table,
-                     bool excl)
+static int netlink_add_table_compat(struct netlink_ctx *ctx,
+                                   const struct handle *h,
+                                   const struct location *loc,
+                                   const struct table *table, bool excl)
 {
        struct nft_table *nlt;
        int err;
@@ -774,8 +775,43 @@ int netlink_add_table(struct netlink_ctx *ctx, const struct handle *h,
        return err;
 }
 
-int netlink_delete_table(struct netlink_ctx *ctx, const struct handle *h,
-                        const struct location *loc)
+static int netlink_add_table_batch(struct netlink_ctx *ctx,
+                                  const struct handle *h,
+                                  const struct location *loc,
+                                  const struct table *table, bool excl)
+{
+       struct nft_table *nlt;
+       int err;
+
+       nlt = alloc_nft_table(h);
+       err = mnl_nft_table_batch_add(nf_sock, nlt, excl ? NLM_F_EXCL : 0,
+                                     ctx->seqnum);
+       nft_table_free(nlt);
+
+       if (err < 0) {
+               netlink_io_error(ctx, loc, "Could not add table: %s",
+                                strerror(errno));
+       }
+       return err;
+}
+
+int netlink_add_table(struct netlink_ctx *ctx, const struct handle *h,
+                     const struct location *loc,
+                     const struct table *table, bool excl)
+{
+       int ret;
+
+       if (ctx->batch_supported)
+               ret = netlink_add_table_batch(ctx, h, loc, table, excl);
+       else
+               ret = netlink_add_table_compat(ctx, h, loc, table, excl);
+
+       return ret;
+}
+
+static int netlink_del_table_compat(struct netlink_ctx *ctx,
+                                   const struct handle *h,
+                                   const struct location *loc)
 {
        struct nft_table *nlt;
        int err;
@@ -790,6 +826,37 @@ int netlink_delete_table(struct netlink_ctx *ctx, const struct handle *h,
        return err;
 }
 
+static int netlink_del_table_batch(struct netlink_ctx *ctx,
+                                  const struct handle *h,
+                                  const struct location *loc)
+{
+       struct nft_table *nlt;
+       int err;
+
+       nlt = alloc_nft_table(h);
+       err = mnl_nft_table_batch_del(nf_sock, nlt, 0, ctx->seqnum);
+       nft_table_free(nlt);
+
+       if (err < 0) {
+               netlink_io_error(ctx, loc, "Could not delete table: %s",
+                                strerror(errno));
+       }
+       return err;
+}
+
+int netlink_delete_table(struct netlink_ctx *ctx, const struct handle *h,
+                        const struct location *loc)
+{
+       int ret;
+
+       if (ctx->batch_supported)
+               ret = netlink_del_table_batch(ctx, h, loc);
+       else
+               ret = netlink_del_table_compat(ctx, h, loc);
+
+       return ret;
+}
+
 void netlink_dump_table(struct nft_table *nlt)
 {
 #ifdef DEBUG