From patchwork Wed May 2 22:29:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Sutter X-Patchwork-Id: 907763 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=nwl.cc Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40btJL16pfz9s1d for ; Thu, 3 May 2018 08:30:10 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751755AbeEBWaJ (ORCPT ); Wed, 2 May 2018 18:30:09 -0400 Received: from orbyte.nwl.cc ([151.80.46.58]:44622 "EHLO orbyte.nwl.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751736AbeEBWaJ (ORCPT ); Wed, 2 May 2018 18:30:09 -0400 Received: from localhost ([::1]:59898 helo=tatos) by orbyte.nwl.cc with esmtp (Exim 4.90_1) (envelope-from ) id 1fE0GG-000254-2c; Thu, 03 May 2018 00:30:08 +0200 From: Phil Sutter To: Pablo Neira Ayuso Cc: netfilter-devel@vger.kernel.org Subject: [nft PATCH 05/13] libnftables: Introduce a few helper functions Date: Thu, 3 May 2018 00:29:04 +0200 Message-Id: <20180502222912.17812-6-phil@nwl.cc> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180502222912.17812-1-phil@nwl.cc> References: <20180502222912.17812-1-phil@nwl.cc> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org This adds a bunch of functions for conversion of different values into string (and vice-versa). * log_level_parse(): A simple helper to turn log level string representation into log level value. * nat_etype2str(): Translate nat statement type into string representation. * ct_dir2str(): Convert IP_CT_DIR_* values into string representation. * ct_label2str(): Convert ct_label values into string representation. Signed-off-by: Phil Sutter --- include/ct.h | 2 ++ include/statement.h | 3 +++ src/ct.c | 44 +++++++++++++++++++++++++++++++------------- src/statement.c | 21 +++++++++++++++++++-- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/include/ct.h b/include/ct.h index dadd820f88740..4c5bd804dabfc 100644 --- a/include/ct.h +++ b/include/ct.h @@ -33,6 +33,8 @@ extern void ct_expr_update_type(struct proto_ctx *ctx, struct expr *expr); extern struct stmt *notrack_stmt_alloc(const struct location *loc); extern struct stmt *flow_offload_stmt_alloc(const struct location *loc, const char *table_name); +extern const char *ct_dir2str(int dir); +extern const char *ct_label2str(unsigned long value); extern const struct datatype ct_dir_type; extern const struct datatype ct_state_type; diff --git a/include/statement.h b/include/statement.h index fc80dbd518b35..2c6d0dfa2dd50 100644 --- a/include/statement.h +++ b/include/statement.h @@ -77,6 +77,7 @@ struct log_stmt { }; extern const char *log_level(uint32_t level); +extern int log_level_parse(const char *level); extern struct stmt *log_stmt_alloc(const struct location *loc); @@ -107,6 +108,8 @@ enum nft_nat_etypes { NFT_NAT_REDIR, }; +extern const char *nat_etype2str(enum nft_nat_etypes type); + struct nat_stmt { enum nft_nat_etypes type; struct expr *addr; diff --git a/src/ct.c b/src/ct.c index 2abaa0d581443..a1a91f3ae7644 100644 --- a/src/ct.c +++ b/src/ct.c @@ -64,6 +64,18 @@ static const struct symbol_table ct_dir_tbl = { } }; +const char *ct_dir2str(int dir) +{ + const struct symbolic_constant *s; + + for (s = ct_dir_tbl.symbols; s->identifier != NULL; s++) { + if (dir == (int)s->value) + return s->identifier; + } + + return NULL; +} + const struct datatype ct_dir_type = { .type = TYPE_CT_DIR, .name = "ct_dir", @@ -133,20 +145,30 @@ static struct symbol_table *ct_label_tbl; #define CT_LABEL_BIT_SIZE 128 +const char *ct_label2str(unsigned long value) +{ + const struct symbolic_constant *s; + + for (s = ct_label_tbl->symbols; s->identifier; s++) { + if (value == s->value) + return s->identifier; + } + + return NULL; +} + static void ct_label_type_print(const struct expr *expr, struct output_ctx *octx) { unsigned long bit = mpz_scan1(expr->value, 0); - const struct symbolic_constant *s; + const char *labelstr = ct_label2str(bit); - for (s = ct_label_tbl->symbols; s->identifier != NULL; s++) { - if (bit != s->value) - continue; - nft_print(octx, "\"%s\"", s->identifier); + if (labelstr) { + nft_print(octx, "\"%s\"", labelstr); return; } /* can happen when connlabel.conf is altered after rules were added */ - nft_print(octx, "%ld", (long)mpz_scan1(expr->value, 0)); + nft_print(octx, "%lu", bit); } static struct error_record *ct_label_type_parse(const struct expr *sym, @@ -273,19 +295,15 @@ const struct ct_template ct_templates[__NFT_CT_MAX] = { static void ct_print(enum nft_ct_keys key, int8_t dir, uint8_t nfproto, struct output_ctx *octx) { - const struct symbolic_constant *s; + const char *dirstr = ct_dir2str(dir); const struct proto_desc *desc; nft_print(octx, "ct "); if (dir < 0) goto done; - for (s = ct_dir_tbl.symbols; s->identifier != NULL; s++) { - if (dir == (int)s->value) { - nft_print(octx, "%s ", s->identifier); - break; - } - } + if (dirstr) + nft_print(octx, "%s ", dirstr); switch (key) { case NFT_CT_SRC: diff --git a/src/statement.c b/src/statement.c index 6537bbbd9a20b..8160e0adfce49 100644 --- a/src/statement.c +++ b/src/statement.c @@ -233,6 +233,18 @@ const char *log_level(uint32_t level) return syslog_level[level]; } +int log_level_parse(const char *level) +{ + int i; + + for (i = 0; i <= LOG_DEBUG; i++) { + if (syslog_level[i] && + !strcmp(level, syslog_level[i])) + return i; + } + return -1; +} + static void log_stmt_print(const struct stmt *stmt, struct output_ctx *octx) { nft_print(octx, "log"); @@ -499,7 +511,7 @@ static void print_nf_nat_flags(uint32_t flags, struct output_ctx *octx) nft_print(octx, "%spersistent", delim); } -static void nat_stmt_print(const struct stmt *stmt, struct output_ctx *octx) +const char *nat_etype2str(enum nft_nat_etypes type) { static const char * const nat_types[] = { [NFT_NAT_SNAT] = "snat", @@ -508,7 +520,12 @@ static void nat_stmt_print(const struct stmt *stmt, struct output_ctx *octx) [NFT_NAT_REDIR] = "redirect", }; - nft_print(octx, "%s", nat_types[stmt->nat.type]); + return nat_types[type]; +} + +static void nat_stmt_print(const struct stmt *stmt, struct output_ctx *octx) +{ + nft_print(octx, "%s", nat_etype2str(stmt->nat.type)); if (stmt->nat.addr || stmt->nat.proto) nft_print(octx, " to");