From patchwork Wed Feb 26 16:10:05 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arturo Borrero X-Patchwork-Id: 324497 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 027792C0040 for ; Thu, 27 Feb 2014 03:10:10 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752126AbaBZQKI (ORCPT ); Wed, 26 Feb 2014 11:10:08 -0500 Received: from smtp3.cica.es ([150.214.5.190]:60332 "EHLO smtp.cica.es" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751679AbaBZQKH (ORCPT ); Wed, 26 Feb 2014 11:10:07 -0500 Received: from localhost (unknown [127.0.0.1]) by smtp.cica.es (Postfix) with ESMTP id 9317F51ED37; Wed, 26 Feb 2014 16:10:06 +0000 (UTC) X-Virus-Scanned: amavisd-new at cica.es Received: from smtp.cica.es ([127.0.0.1]) by localhost (mail.cica.es [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LikP7p-6eZLj; Wed, 26 Feb 2014 17:10:06 +0100 (CET) Received: from nfdev.cica.es (nfdev.cica.es [IPv6:2a00:9ac0:c1ca:31::220]) by smtp.cica.es (Postfix) with ESMTP id 63D2651ECC6; Wed, 26 Feb 2014 17:10:06 +0100 (CET) Subject: [RFC PATCH v2 4/6] netlink: add netlink_delinearize_chain() func To: netfilter-devel@vger.kernel.org From: Arturo Borrero Gonzalez Cc: pablo@netfilter.org Date: Wed, 26 Feb 2014 17:10:05 +0100 Message-ID: <20140226161005.18974.74871.stgit@nfdev.cica.es> In-Reply-To: <20140226160918.18974.64532.stgit@nfdev.cica.es> References: <20140226160918.18974.64532.stgit@nfdev.cica.es> User-Agent: StGit/0.15 MIME-Version: 1.0 Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org Let's make this code reusable. Also, this patch fixes a hidden bug: the table in the chain's handle was being set to the chain name. Signed-off-by: Arturo Borrero Gonzalez --- src/netlink.c | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/src/netlink.c b/src/netlink.c index 74372bf..d2a7804 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -496,25 +496,16 @@ int netlink_delete_chain(struct netlink_ctx *ctx, const struct handle *h, return err; } -static int list_chain_cb(struct nft_chain *nlc, void *arg) +static struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx, + struct nft_chain *nlc) { - struct netlink_ctx *ctx = arg; - const struct handle *h = ctx->data; struct chain *chain; - if ((h->family != nft_chain_attr_get_u32(nlc, NFT_CHAIN_ATTR_FAMILY)) || - strcmp(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_TABLE), h->table) != 0) - return 0; - - if (h->chain && - strcmp(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_NAME), h->chain) != 0) - return 0; - chain = chain_alloc(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_NAME)); chain->handle.family = nft_chain_attr_get_u32(nlc, NFT_CHAIN_ATTR_FAMILY); chain->handle.table = - xstrdup(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_NAME)); + xstrdup(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_TABLE)); chain->handle.handle = nft_chain_attr_get_u64(nlc, NFT_CHAIN_ATTR_HANDLE); @@ -531,6 +522,24 @@ static int list_chain_cb(struct nft_chain *nlc, void *arg) } list_add_tail(&chain->list, &ctx->list); + return chain; +} + +static int list_chain_cb(struct nft_chain *nlc, void *arg) +{ + struct netlink_ctx *ctx = arg; + const struct handle *h = ctx->data; + const char *table = nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_TABLE); + const char *name = nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_NAME); + + if ((h->family != nft_chain_attr_get_u32(nlc, NFT_CHAIN_ATTR_FAMILY)) || + strcmp(table, h->table) != 0) + return 0; + + if (h->chain && strcmp(name, h->chain) != 0) + return 0; + + netlink_delinearize_chain(ctx, nlc); return 0; } @@ -570,25 +579,12 @@ int netlink_get_chain(struct netlink_ctx *ctx, const struct handle *h, const struct location *loc) { struct nft_chain *nlc; - struct chain *chain; int err; nlc = alloc_nft_chain(h); err = mnl_nft_chain_get(nf_sock, nlc, 0); - chain = chain_alloc(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_NAME)); - chain->handle.family = nft_chain_attr_get_u32(nlc, NFT_CHAIN_ATTR_FAMILY); - chain->handle.table = xstrdup(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_TABLE)); - chain->handle.handle = nft_chain_attr_get_u64(nlc, NFT_CHAIN_ATTR_HANDLE); - if (nft_chain_attr_is_set(nlc, NFT_CHAIN_ATTR_TYPE) && - nft_chain_attr_is_set(nlc, NFT_CHAIN_ATTR_HOOKNUM) && - nft_chain_attr_is_set(nlc, NFT_CHAIN_ATTR_PRIO)) { - chain->hooknum = nft_chain_attr_get_u32(nlc, NFT_CHAIN_ATTR_HOOKNUM); - chain->priority = nft_chain_attr_get_u32(nlc, NFT_CHAIN_ATTR_PRIO); - chain->type = xstrdup(nft_chain_attr_get_str(nlc, NFT_CHAIN_ATTR_TYPE)); - } - list_add_tail(&chain->list, &ctx->list); - + netlink_delinearize_chain(ctx, nlc); nft_chain_free(nlc); if (err < 0)