From patchwork Thu May 3 11:00:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pablo Neira Ayuso X-Patchwork-Id: 907964 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=netfilter.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40cBy43nlsz9s4Z for ; Thu, 3 May 2018 21:00:28 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751036AbeECLA2 (ORCPT ); Thu, 3 May 2018 07:00:28 -0400 Received: from mail.us.es ([193.147.175.20]:37080 "EHLO mail.us.es" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751101AbeECLA1 (ORCPT ); Thu, 3 May 2018 07:00:27 -0400 Received: from antivirus1-rhel7.int (unknown [192.168.2.11]) by mail.us.es (Postfix) with ESMTP id CA1508D050D for ; Thu, 3 May 2018 12:59:39 +0200 (CEST) Received: from antivirus1-rhel7.int (localhost [127.0.0.1]) by antivirus1-rhel7.int (Postfix) with ESMTP id B29BB17B3B4 for ; Thu, 3 May 2018 12:59:39 +0200 (CEST) Received: by antivirus1-rhel7.int (Postfix, from userid 99) id A77EA17B3C3; Thu, 3 May 2018 12:59:39 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on antivirus1-rhel7.int X-Spam-Level: X-Spam-Status: No, score=-108.2 required=7.5 tests=ALL_TRUSTED,BAYES_50, SMTPAUTH_US2,USER_IN_WHITELIST autolearn=disabled version=3.4.1 Received: from antivirus1-rhel7.int (localhost [127.0.0.1]) by antivirus1-rhel7.int (Postfix) with ESMTP id 80F3117B3B4 for ; Thu, 3 May 2018 12:59:33 +0200 (CEST) Received: from 192.168.1.97 (192.168.1.97) by antivirus1-rhel7.int (F-Secure/fsigk_smtp/550/antivirus1-rhel7.int); Thu, 03 May 2018 12:59:33 +0200 (CEST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/antivirus1-rhel7.int) Received: from salvia.here (sys.soleta.eu [212.170.55.40]) (Authenticated sender: pneira@us.es) by entrada.int (Postfix) with ESMTPA id 651A541628D3 for ; Thu, 3 May 2018 12:59:33 +0200 (CEST) X-SMTPAUTHUS: auth mail.us.es From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Subject: [PATCH nft 5/5] src: use location to display error messages Date: Thu, 3 May 2018 13:00:12 +0200 Message-Id: <20180503110012.11277-5-pablo@netfilter.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180503110012.11277-1-pablo@netfilter.org> References: <20180503110012.11277-1-pablo@netfilter.org> X-Virus-Scanned: ClamAV using ClamSMTP Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org # nft add chain foo bar Error: Could not process rule: No such file or directory add chain foo bar ^^^ Signed-off-by: Pablo Neira Ayuso --- src/evaluate.c | 156 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 94 insertions(+), 62 deletions(-) diff --git a/src/evaluate.c b/src/evaluate.c index fdc536479785..c5f86395c84b 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -42,8 +43,8 @@ static const char * const byteorder_names[] = { __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args) #define monitor_error(ctx, s1, fmt, args...) \ __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args) -#define cmd_error(ctx, fmt, args...) \ - __stmt_binary_error(ctx, &(ctx->cmd)->location, NULL, fmt, ## args) +#define cmd_error(ctx, loc, fmt, args...) \ + __stmt_binary_error(ctx, loc, NULL, fmt, ## args) static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx, const struct set *set, @@ -190,8 +191,9 @@ static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr) table = table_lookup_global(ctx); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - ctx->cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, (*expr)->identifier); if (set == NULL) @@ -2746,13 +2748,15 @@ static int setelem_evaluate(struct eval_ctx *ctx, struct expr **expr) table = table_lookup_global(ctx); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - ctx->cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, ctx->cmd->handle.set.name); if (set == NULL) - return cmd_error(ctx, "Could not process rule: Set '%s' does not exist", - ctx->cmd->handle.set.name); + return cmd_error(ctx, &ctx->cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); ctx->set = set; expr_set_context(&ctx->ectx, set->key->dtype, set->key->len); @@ -2769,8 +2773,9 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set) table = table_lookup_global(ctx); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - ctx->cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); if (!(set->flags & NFT_SET_INTERVAL) && set->automerge) return set_error(ctx, set, "auto-merge only works with interval sets"); @@ -2831,8 +2836,9 @@ static int flowtable_evaluate(struct eval_ctx *ctx, struct flowtable *ft) table = table_lookup_global(ctx); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - ctx->cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); ft->hooknum = str2hooknum(NFPROTO_NETDEV, ft->hookstr); if (ft->hooknum == NF_INET_NUMHOOKS) @@ -2923,8 +2929,9 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) table = table_lookup_global(ctx); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - ctx->cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); if (chain == NULL) { if (chain_lookup(table, &ctx->cmd->handle) == NULL) { @@ -3087,12 +3094,14 @@ static int cmd_evaluate_get(struct eval_ctx *ctx, struct cmd *cmd) case CMD_OBJ_SETELEM: table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, cmd->handle.set.name); if (set == NULL || set->flags & (NFT_SET_MAP | NFT_SET_EVAL)) - return cmd_error(ctx, "Could not process rule: Set '%s' does not exist", - cmd->handle.set.name); + return cmd_error(ctx, &ctx->cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); return setelem_evaluate(ctx, &cmd->expr); default: @@ -3110,11 +3119,13 @@ static int cmd_evaluate_list_obj(struct eval_ctx *ctx, const struct cmd *cmd, table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); if (obj_lookup(table, cmd->handle.obj.name, obj_type) == NULL) - return cmd_error(ctx, "Could not process rule: Object '%s' does not exist", - cmd->handle.obj.name); + return cmd_error(ctx, &cmd->handle.obj.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; } @@ -3136,47 +3147,56 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd) table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_SET: table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, cmd->handle.set.name); if (set == NULL || set->flags & (NFT_SET_MAP | NFT_SET_EVAL)) - return cmd_error(ctx, "Could not process rule: Set '%s' does not exist", - cmd->handle.set.name); + return cmd_error(ctx, &cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_METER: table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, cmd->handle.set.name); if (set == NULL || !(set->flags & NFT_SET_EVAL)) - return cmd_error(ctx, "Could not process rule: Meter '%s' does not exist", - cmd->handle.set.name); + return cmd_error(ctx, &cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_MAP: table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, cmd->handle.set.name); if (set == NULL || !(set->flags & NFT_SET_MAP)) - return cmd_error(ctx, "Could not process rule: Map '%s' does not exist", - cmd->handle.set.name); + return cmd_error(ctx, &cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_CHAIN: table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); if (chain_lookup(table, &cmd->handle) == NULL) - return cmd_error(ctx, "Could not process rule: Chain '%s' does not exist", - cmd->handle.chain.name); + return cmd_error(ctx, &cmd->handle.chain.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_QUOTA: return cmd_evaluate_list_obj(ctx, cmd, NFT_OBJECT_QUOTA); @@ -3195,8 +3215,9 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd) if (cmd->handle.table.name == NULL) return 0; if (table_lookup(&cmd->handle, ctx->cache) == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_CHAINS: case CMD_OBJ_RULESET: @@ -3225,8 +3246,9 @@ static int cmd_evaluate_reset(struct eval_ctx *ctx, struct cmd *cmd) if (cmd->handle.table.name == NULL) return 0; if (table_lookup(&cmd->handle, ctx->cache) == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; default: BUG("invalid command object type %u\n", cmd->obj); @@ -3258,12 +3280,14 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd) table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, cmd->handle.set.name); if (set == NULL || set->flags & (NFT_SET_MAP | NFT_SET_EVAL)) - return cmd_error(ctx, "Could not process rule: Set '%s' does not exist", - cmd->handle.set.name); + return cmd_error(ctx, &cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_MAP: ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, @@ -3273,12 +3297,14 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd) table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, cmd->handle.set.name); if (set == NULL || !(set->flags & NFT_SET_MAP)) - return cmd_error(ctx, "Could not process rule: Map '%s' does not exist", - cmd->handle.set.name); + return cmd_error(ctx, &ctx->cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; case CMD_OBJ_METER: ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, @@ -3288,12 +3314,14 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd) table = table_lookup(&cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); set = set_lookup(table, cmd->handle.set.name); if (set == NULL || !(set->flags & NFT_SET_EVAL)) - return cmd_error(ctx, "Could not process rule: Meter '%s' does not exist", - cmd->handle.set.name); + return cmd_error(ctx, &ctx->cmd->handle.set.location, + "Could not process rule: %s", + strerror(ENOENT)); return 0; default: BUG("invalid command object type %u\n", cmd->obj); @@ -3315,11 +3343,13 @@ static int cmd_evaluate_rename(struct eval_ctx *ctx, struct cmd *cmd) table = table_lookup(&ctx->cmd->handle, ctx->cache); if (table == NULL) - return cmd_error(ctx, "Could not process rule: Table '%s' does not exist", - ctx->cmd->handle.table.name); + return cmd_error(ctx, &ctx->cmd->handle.table.location, + "Could not process rule: %s", + strerror(ENOENT)); if (chain_lookup(table, &ctx->cmd->handle) == NULL) - return cmd_error(ctx, "Could not process rule: Chain '%s' does not exist", - ctx->cmd->handle.chain.name); + return cmd_error(ctx, &ctx->cmd->handle.chain.location, + "Could not process rule: %s", + strerror(ENOENT)); break; default: BUG("invalid command object type %u\n", cmd->obj); @@ -3429,7 +3459,8 @@ static int cmd_evaluate_monitor(struct eval_ctx *ctx, struct cmd *cmd) static int cmd_evaluate_export(struct eval_ctx *ctx, struct cmd *cmd) { if (cmd->markup->format == __NFT_OUTPUT_NOTSUPP) - return cmd_error(ctx, "this output type is not supported"); + return cmd_error(ctx, &cmd->location, + "this output type is not supported"); return cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, ctx->debug_mask, ctx->octx); @@ -3438,7 +3469,8 @@ static int cmd_evaluate_export(struct eval_ctx *ctx, struct cmd *cmd) static int cmd_evaluate_import(struct eval_ctx *ctx, struct cmd *cmd) { if (cmd->markup->format == __NFT_OUTPUT_NOTSUPP) - return cmd_error(ctx, "this output type not supported"); + return cmd_error(ctx, &cmd->location, + "this output type not supported"); return 0; }